1 | /* |
2 | * This file compiles an abstract syntax tree (AST) into Python bytecode. |
3 | * |
4 | * The primary entry point is _PyAST_Compile(), which returns a |
5 | * PyCodeObject. The compiler makes several passes to build the code |
6 | * object: |
7 | * 1. Checks for future statements. See future.c |
8 | * 2. Builds a symbol table. See symtable.c. |
9 | * 3. Generate code for basic blocks. See compiler_mod() in this file. |
10 | * 4. Assemble the basic blocks into final code. See assemble() in |
11 | * this file. |
12 | * 5. Optimize the byte code (peephole optimizations). |
13 | * |
14 | * Note that compiler_mod() suggests module, but the module ast type |
15 | * (mod_ty) has cases for expressions and interactive statements. |
16 | * |
17 | * CAUTION: The VISIT_* macros abort the current function when they |
18 | * encounter a problem. So don't invoke them when there is memory |
19 | * which needs to be released. Code blocks are OK, as the compiler |
20 | * structure takes care of releasing those. Use the arena to manage |
21 | * objects. |
22 | */ |
23 | |
24 | #include <stdbool.h> |
25 | |
26 | #include "Python.h" |
27 | #include "pycore_ast.h" // _PyAST_GetDocString() |
28 | #include "pycore_compile.h" // _PyFuture_FromAST() |
29 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
30 | #include "pycore_long.h" // _PyLong_GetZero() |
31 | #include "pycore_symtable.h" // PySTEntryObject |
32 | |
33 | #define NEED_OPCODE_JUMP_TABLES |
34 | #include "opcode.h" // EXTENDED_ARG |
35 | #include "wordcode_helpers.h" // instrsize() |
36 | |
37 | |
38 | #define DEFAULT_BLOCK_SIZE 16 |
39 | #define DEFAULT_BLOCKS 8 |
40 | #define DEFAULT_CODE_SIZE 128 |
41 | #define DEFAULT_LNOTAB_SIZE 16 |
42 | |
43 | #define COMP_GENEXP 0 |
44 | #define COMP_LISTCOMP 1 |
45 | #define COMP_SETCOMP 2 |
46 | #define COMP_DICTCOMP 3 |
47 | |
48 | /* A soft limit for stack use, to avoid excessive |
49 | * memory use for large constants, etc. |
50 | * |
51 | * The value 30 is plucked out of thin air. |
52 | * Code that could use more stack than this is |
53 | * rare, so the exact value is unimportant. |
54 | */ |
55 | #define STACK_USE_GUIDELINE 30 |
56 | |
57 | /* If we exceed this limit, it should |
58 | * be considered a compiler bug. |
59 | * Currently it should be impossible |
60 | * to exceed STACK_USE_GUIDELINE * 100, |
61 | * as 100 is the maximum parse depth. |
62 | * For performance reasons we will |
63 | * want to reduce this to a |
64 | * few hundred in the future. |
65 | * |
66 | * NOTE: Whatever MAX_ALLOWED_STACK_USE is |
67 | * set to, it should never restrict what Python |
68 | * we can write, just how we compile it. |
69 | */ |
70 | #define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100) |
71 | |
72 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
73 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
74 | && (c->u->u_ste->ste_type == ModuleBlock)) |
75 | |
76 | struct instr { |
77 | unsigned char i_opcode; |
78 | int i_oparg; |
79 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
80 | int i_lineno; |
81 | }; |
82 | |
83 | #define LOG_BITS_PER_INT 5 |
84 | #define MASK_LOW_LOG_BITS 31 |
85 | |
86 | static inline int |
87 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
88 | /* Is the relevant bit set in the relevant word? */ |
89 | /* 256 bits fit into 8 32-bits words. |
90 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
91 | * Bit within word is the low bits of bitindex. |
92 | */ |
93 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
94 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
95 | } |
96 | |
97 | static inline int |
98 | is_relative_jump(struct instr *i) |
99 | { |
100 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
101 | } |
102 | |
103 | static inline int |
104 | is_jump(struct instr *i) |
105 | { |
106 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
107 | } |
108 | |
109 | typedef struct basicblock_ { |
110 | /* Each basicblock in a compilation unit is linked via b_list in the |
111 | reverse order that the block are allocated. b_list points to the next |
112 | block, not to be confused with b_next, which is next by control flow. */ |
113 | struct basicblock_ *b_list; |
114 | /* number of instructions used */ |
115 | int b_iused; |
116 | /* length of instruction array (b_instr) */ |
117 | int b_ialloc; |
118 | /* pointer to an array of instructions, initially NULL */ |
119 | struct instr *b_instr; |
120 | /* If b_next is non-NULL, it is a pointer to the next |
121 | block reached by normal control flow. */ |
122 | struct basicblock_ *b_next; |
123 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
124 | unsigned b_return : 1; |
125 | /* Number of predecssors that a block has. */ |
126 | int b_predecessors; |
127 | /* Basic block has no fall through (it ends with a return, raise or jump) */ |
128 | unsigned b_nofallthrough : 1; |
129 | /* Basic block exits scope (it ends with a return or raise) */ |
130 | unsigned b_exit : 1; |
131 | /* Used by compiler passes to mark whether they have visited a basic block. */ |
132 | unsigned b_visited : 1; |
133 | /* depth of stack upon entry of block, computed by stackdepth() */ |
134 | int b_startdepth; |
135 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
136 | int b_offset; |
137 | } basicblock; |
138 | |
139 | /* fblockinfo tracks the current frame block. |
140 | |
141 | A frame block is used to handle loops, try/except, and try/finally. |
142 | It's called a frame block to distinguish it from a basic block in the |
143 | compiler IR. |
144 | */ |
145 | |
146 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
147 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER, |
148 | ASYNC_COMPREHENSION_GENERATOR }; |
149 | |
150 | struct fblockinfo { |
151 | enum fblocktype fb_type; |
152 | basicblock *fb_block; |
153 | /* (optional) type-specific exit or cleanup block */ |
154 | basicblock *fb_exit; |
155 | /* (optional) additional information required for unwinding */ |
156 | void *fb_datum; |
157 | }; |
158 | |
159 | enum { |
160 | COMPILER_SCOPE_MODULE, |
161 | COMPILER_SCOPE_CLASS, |
162 | COMPILER_SCOPE_FUNCTION, |
163 | COMPILER_SCOPE_ASYNC_FUNCTION, |
164 | COMPILER_SCOPE_LAMBDA, |
165 | COMPILER_SCOPE_COMPREHENSION, |
166 | }; |
167 | |
168 | /* The following items change on entry and exit of code blocks. |
169 | They must be saved and restored when returning to a block. |
170 | */ |
171 | struct compiler_unit { |
172 | PySTEntryObject *u_ste; |
173 | |
174 | PyObject *u_name; |
175 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
176 | int u_scope_type; |
177 | |
178 | /* The following fields are dicts that map objects to |
179 | the index of them in co_XXX. The index is used as |
180 | the argument for opcodes that refer to those collections. |
181 | */ |
182 | PyObject *u_consts; /* all constants */ |
183 | PyObject *u_names; /* all names */ |
184 | PyObject *u_varnames; /* local variables */ |
185 | PyObject *u_cellvars; /* cell variables */ |
186 | PyObject *u_freevars; /* free variables */ |
187 | |
188 | PyObject *u_private; /* for private name mangling */ |
189 | |
190 | Py_ssize_t u_argcount; /* number of arguments for block */ |
191 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
192 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
193 | /* Pointer to the most recently allocated block. By following b_list |
194 | members, you can reach all early allocated blocks. */ |
195 | basicblock *u_blocks; |
196 | basicblock *u_curblock; /* pointer to current block */ |
197 | |
198 | int u_nfblocks; |
199 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
200 | |
201 | int u_firstlineno; /* the first lineno of the block */ |
202 | int u_lineno; /* the lineno for the current stmt */ |
203 | int u_col_offset; /* the offset of the current stmt */ |
204 | int u_end_lineno; /* the end line of the current stmt */ |
205 | int u_end_col_offset; /* the end offset of the current stmt */ |
206 | }; |
207 | |
208 | /* This struct captures the global state of a compilation. |
209 | |
210 | The u pointer points to the current compilation unit, while units |
211 | for enclosing blocks are stored in c_stack. The u and c_stack are |
212 | managed by compiler_enter_scope() and compiler_exit_scope(). |
213 | |
214 | Note that we don't track recursion levels during compilation - the |
215 | task of detecting and rejecting excessive levels of nesting is |
216 | handled by the symbol analysis pass. |
217 | |
218 | */ |
219 | |
220 | struct compiler { |
221 | PyObject *c_filename; |
222 | struct symtable *c_st; |
223 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
224 | PyCompilerFlags *c_flags; |
225 | |
226 | int c_optimize; /* optimization level */ |
227 | int c_interactive; /* true if in interactive mode */ |
228 | int c_nestlevel; |
229 | PyObject *c_const_cache; /* Python dict holding all constants, |
230 | including names tuple */ |
231 | struct compiler_unit *u; /* compiler state for current block */ |
232 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
233 | PyArena *c_arena; /* pointer to memory allocation arena */ |
234 | }; |
235 | |
236 | typedef struct { |
237 | // A list of strings corresponding to name captures. It is used to track: |
238 | // - Repeated name assignments in the same pattern. |
239 | // - Different name assignments in alternatives. |
240 | // - The order of name assignments in alternatives. |
241 | PyObject *stores; |
242 | // If 0, any name captures against our subject will raise. |
243 | int allow_irrefutable; |
244 | // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop |
245 | // i items off of the stack. The end result looks like this (with each block |
246 | // falling through to the next): |
247 | // fail_pop[4]: POP_TOP |
248 | // fail_pop[3]: POP_TOP |
249 | // fail_pop[2]: POP_TOP |
250 | // fail_pop[1]: POP_TOP |
251 | // fail_pop[0]: NOP |
252 | basicblock **fail_pop; |
253 | // The current length of fail_pop. |
254 | Py_ssize_t fail_pop_size; |
255 | // The number of items on top of the stack that need to *stay* on top of the |
256 | // stack. Variable captures go beneath these. All of them will be popped on |
257 | // failure. |
258 | Py_ssize_t on_top; |
259 | } pattern_context; |
260 | |
261 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
262 | static void compiler_free(struct compiler *); |
263 | static basicblock *compiler_new_block(struct compiler *); |
264 | static int compiler_next_instr(basicblock *); |
265 | static int compiler_addop(struct compiler *, int); |
266 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
267 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
268 | static int compiler_addop_j_noline(struct compiler *, int, basicblock *); |
269 | static int compiler_error(struct compiler *, const char *, ...); |
270 | static int compiler_warn(struct compiler *, const char *, ...); |
271 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
272 | |
273 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
274 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
275 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
276 | static int compiler_visit_expr(struct compiler *, expr_ty); |
277 | static int compiler_augassign(struct compiler *, stmt_ty); |
278 | static int compiler_annassign(struct compiler *, stmt_ty); |
279 | static int compiler_subscript(struct compiler *, expr_ty); |
280 | static int compiler_slice(struct compiler *, expr_ty); |
281 | |
282 | static int inplace_binop(operator_ty); |
283 | static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t); |
284 | |
285 | |
286 | static int compiler_with(struct compiler *, stmt_ty, int); |
287 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
288 | static int compiler_async_for(struct compiler *, stmt_ty); |
289 | static int compiler_call_helper(struct compiler *c, int n, |
290 | asdl_expr_seq *args, |
291 | asdl_keyword_seq *keywords); |
292 | static int compiler_try_except(struct compiler *, stmt_ty); |
293 | static int compiler_set_qualname(struct compiler *); |
294 | |
295 | static int compiler_sync_comprehension_generator( |
296 | struct compiler *c, |
297 | asdl_comprehension_seq *generators, int gen_index, |
298 | int depth, |
299 | expr_ty elt, expr_ty val, int type); |
300 | |
301 | static int compiler_async_comprehension_generator( |
302 | struct compiler *c, |
303 | asdl_comprehension_seq *generators, int gen_index, |
304 | int depth, |
305 | expr_ty elt, expr_ty val, int type); |
306 | |
307 | static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *); |
308 | static int compiler_match(struct compiler *, stmt_ty); |
309 | static int compiler_pattern_subpattern(struct compiler *, pattern_ty, |
310 | pattern_context *); |
311 | |
312 | static PyCodeObject *assemble(struct compiler *, int addNone); |
313 | static PyObject *__doc__, *__annotations__; |
314 | |
315 | #define CAPSULE_NAME "compile.c compiler unit" |
316 | |
317 | PyObject * |
318 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
319 | { |
320 | /* Name mangling: __private becomes _classname__private. |
321 | This is independent from how the name is used. */ |
322 | PyObject *result; |
323 | size_t nlen, plen, ipriv; |
324 | Py_UCS4 maxchar; |
325 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
326 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
327 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
328 | Py_INCREF(ident); |
329 | return ident; |
330 | } |
331 | nlen = PyUnicode_GET_LENGTH(ident); |
332 | plen = PyUnicode_GET_LENGTH(privateobj); |
333 | /* Don't mangle __id__ or names with dots. |
334 | |
335 | The only time a name with a dot can occur is when |
336 | we are compiling an import statement that has a |
337 | package name. |
338 | |
339 | TODO(jhylton): Decide whether we want to support |
340 | mangling of the module name, e.g. __M.X. |
341 | */ |
342 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
343 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
344 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
345 | Py_INCREF(ident); |
346 | return ident; /* Don't mangle __whatever__ */ |
347 | } |
348 | /* Strip leading underscores from class name */ |
349 | ipriv = 0; |
350 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
351 | ipriv++; |
352 | if (ipriv == plen) { |
353 | Py_INCREF(ident); |
354 | return ident; /* Don't mangle if class is just underscores */ |
355 | } |
356 | plen -= ipriv; |
357 | |
358 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
359 | PyErr_SetString(PyExc_OverflowError, |
360 | "private identifier too large to be mangled" ); |
361 | return NULL; |
362 | } |
363 | |
364 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
365 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
366 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
367 | |
368 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
369 | if (!result) |
370 | return 0; |
371 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
372 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
373 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
374 | Py_DECREF(result); |
375 | return NULL; |
376 | } |
377 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
378 | Py_DECREF(result); |
379 | return NULL; |
380 | } |
381 | assert(_PyUnicode_CheckConsistency(result, 1)); |
382 | return result; |
383 | } |
384 | |
385 | static int |
386 | compiler_init(struct compiler *c) |
387 | { |
388 | memset(c, 0, sizeof(struct compiler)); |
389 | |
390 | c->c_const_cache = PyDict_New(); |
391 | if (!c->c_const_cache) { |
392 | return 0; |
393 | } |
394 | |
395 | c->c_stack = PyList_New(0); |
396 | if (!c->c_stack) { |
397 | Py_CLEAR(c->c_const_cache); |
398 | return 0; |
399 | } |
400 | |
401 | return 1; |
402 | } |
403 | |
404 | PyCodeObject * |
405 | _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
406 | int optimize, PyArena *arena) |
407 | { |
408 | struct compiler c; |
409 | PyCodeObject *co = NULL; |
410 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
411 | int merged; |
412 | |
413 | if (!__doc__) { |
414 | __doc__ = PyUnicode_InternFromString("__doc__" ); |
415 | if (!__doc__) |
416 | return NULL; |
417 | } |
418 | if (!__annotations__) { |
419 | __annotations__ = PyUnicode_InternFromString("__annotations__" ); |
420 | if (!__annotations__) |
421 | return NULL; |
422 | } |
423 | if (!compiler_init(&c)) |
424 | return NULL; |
425 | Py_INCREF(filename); |
426 | c.c_filename = filename; |
427 | c.c_arena = arena; |
428 | c.c_future = _PyFuture_FromAST(mod, filename); |
429 | if (c.c_future == NULL) |
430 | goto finally; |
431 | if (!flags) { |
432 | flags = &local_flags; |
433 | } |
434 | merged = c.c_future->ff_features | flags->cf_flags; |
435 | c.c_future->ff_features = merged; |
436 | flags->cf_flags = merged; |
437 | c.c_flags = flags; |
438 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
439 | c.c_nestlevel = 0; |
440 | |
441 | _PyASTOptimizeState state; |
442 | state.optimize = c.c_optimize; |
443 | state.ff_features = merged; |
444 | |
445 | if (!_PyAST_Optimize(mod, arena, &state)) { |
446 | goto finally; |
447 | } |
448 | |
449 | c.c_st = _PySymtable_Build(mod, filename, c.c_future); |
450 | if (c.c_st == NULL) { |
451 | if (!PyErr_Occurred()) |
452 | PyErr_SetString(PyExc_SystemError, "no symtable" ); |
453 | goto finally; |
454 | } |
455 | |
456 | co = compiler_mod(&c, mod); |
457 | |
458 | finally: |
459 | compiler_free(&c); |
460 | assert(co || PyErr_Occurred()); |
461 | return co; |
462 | } |
463 | |
464 | static void |
465 | compiler_free(struct compiler *c) |
466 | { |
467 | if (c->c_st) |
468 | _PySymtable_Free(c->c_st); |
469 | if (c->c_future) |
470 | PyObject_Free(c->c_future); |
471 | Py_XDECREF(c->c_filename); |
472 | Py_DECREF(c->c_const_cache); |
473 | Py_DECREF(c->c_stack); |
474 | } |
475 | |
476 | static PyObject * |
477 | list2dict(PyObject *list) |
478 | { |
479 | Py_ssize_t i, n; |
480 | PyObject *v, *k; |
481 | PyObject *dict = PyDict_New(); |
482 | if (!dict) return NULL; |
483 | |
484 | n = PyList_Size(list); |
485 | for (i = 0; i < n; i++) { |
486 | v = PyLong_FromSsize_t(i); |
487 | if (!v) { |
488 | Py_DECREF(dict); |
489 | return NULL; |
490 | } |
491 | k = PyList_GET_ITEM(list, i); |
492 | if (PyDict_SetItem(dict, k, v) < 0) { |
493 | Py_DECREF(v); |
494 | Py_DECREF(dict); |
495 | return NULL; |
496 | } |
497 | Py_DECREF(v); |
498 | } |
499 | return dict; |
500 | } |
501 | |
502 | /* Return new dict containing names from src that match scope(s). |
503 | |
504 | src is a symbol table dictionary. If the scope of a name matches |
505 | either scope_type or flag is set, insert it into the new dict. The |
506 | values are integers, starting at offset and increasing by one for |
507 | each key. |
508 | */ |
509 | |
510 | static PyObject * |
511 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
512 | { |
513 | Py_ssize_t i = offset, scope, num_keys, key_i; |
514 | PyObject *k, *v, *dest = PyDict_New(); |
515 | PyObject *sorted_keys; |
516 | |
517 | assert(offset >= 0); |
518 | if (dest == NULL) |
519 | return NULL; |
520 | |
521 | /* Sort the keys so that we have a deterministic order on the indexes |
522 | saved in the returned dictionary. These indexes are used as indexes |
523 | into the free and cell var storage. Therefore if they aren't |
524 | deterministic, then the generated bytecode is not deterministic. |
525 | */ |
526 | sorted_keys = PyDict_Keys(src); |
527 | if (sorted_keys == NULL) |
528 | return NULL; |
529 | if (PyList_Sort(sorted_keys) != 0) { |
530 | Py_DECREF(sorted_keys); |
531 | return NULL; |
532 | } |
533 | num_keys = PyList_GET_SIZE(sorted_keys); |
534 | |
535 | for (key_i = 0; key_i < num_keys; key_i++) { |
536 | /* XXX this should probably be a macro in symtable.h */ |
537 | long vi; |
538 | k = PyList_GET_ITEM(sorted_keys, key_i); |
539 | v = PyDict_GetItemWithError(src, k); |
540 | assert(v && PyLong_Check(v)); |
541 | vi = PyLong_AS_LONG(v); |
542 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
543 | |
544 | if (scope == scope_type || vi & flag) { |
545 | PyObject *item = PyLong_FromSsize_t(i); |
546 | if (item == NULL) { |
547 | Py_DECREF(sorted_keys); |
548 | Py_DECREF(dest); |
549 | return NULL; |
550 | } |
551 | i++; |
552 | if (PyDict_SetItem(dest, k, item) < 0) { |
553 | Py_DECREF(sorted_keys); |
554 | Py_DECREF(item); |
555 | Py_DECREF(dest); |
556 | return NULL; |
557 | } |
558 | Py_DECREF(item); |
559 | } |
560 | } |
561 | Py_DECREF(sorted_keys); |
562 | return dest; |
563 | } |
564 | |
565 | static void |
566 | compiler_unit_check(struct compiler_unit *u) |
567 | { |
568 | basicblock *block; |
569 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
570 | assert(!_PyMem_IsPtrFreed(block)); |
571 | if (block->b_instr != NULL) { |
572 | assert(block->b_ialloc > 0); |
573 | assert(block->b_iused >= 0); |
574 | assert(block->b_ialloc >= block->b_iused); |
575 | } |
576 | else { |
577 | assert (block->b_iused == 0); |
578 | assert (block->b_ialloc == 0); |
579 | } |
580 | } |
581 | } |
582 | |
583 | static void |
584 | compiler_unit_free(struct compiler_unit *u) |
585 | { |
586 | basicblock *b, *next; |
587 | |
588 | compiler_unit_check(u); |
589 | b = u->u_blocks; |
590 | while (b != NULL) { |
591 | if (b->b_instr) |
592 | PyObject_Free((void *)b->b_instr); |
593 | next = b->b_list; |
594 | PyObject_Free((void *)b); |
595 | b = next; |
596 | } |
597 | Py_CLEAR(u->u_ste); |
598 | Py_CLEAR(u->u_name); |
599 | Py_CLEAR(u->u_qualname); |
600 | Py_CLEAR(u->u_consts); |
601 | Py_CLEAR(u->u_names); |
602 | Py_CLEAR(u->u_varnames); |
603 | Py_CLEAR(u->u_freevars); |
604 | Py_CLEAR(u->u_cellvars); |
605 | Py_CLEAR(u->u_private); |
606 | PyObject_Free(u); |
607 | } |
608 | |
609 | static int |
610 | compiler_enter_scope(struct compiler *c, identifier name, |
611 | int scope_type, void *key, int lineno) |
612 | { |
613 | struct compiler_unit *u; |
614 | basicblock *block; |
615 | |
616 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
617 | struct compiler_unit)); |
618 | if (!u) { |
619 | PyErr_NoMemory(); |
620 | return 0; |
621 | } |
622 | u->u_scope_type = scope_type; |
623 | u->u_argcount = 0; |
624 | u->u_posonlyargcount = 0; |
625 | u->u_kwonlyargcount = 0; |
626 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
627 | if (!u->u_ste) { |
628 | compiler_unit_free(u); |
629 | return 0; |
630 | } |
631 | Py_INCREF(name); |
632 | u->u_name = name; |
633 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
634 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
635 | if (!u->u_varnames || !u->u_cellvars) { |
636 | compiler_unit_free(u); |
637 | return 0; |
638 | } |
639 | if (u->u_ste->ste_needs_class_closure) { |
640 | /* Cook up an implicit __class__ cell. */ |
641 | _Py_IDENTIFIER(__class__); |
642 | PyObject *name; |
643 | int res; |
644 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
645 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
646 | name = _PyUnicode_FromId(&PyId___class__); |
647 | if (!name) { |
648 | compiler_unit_free(u); |
649 | return 0; |
650 | } |
651 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
652 | if (res < 0) { |
653 | compiler_unit_free(u); |
654 | return 0; |
655 | } |
656 | } |
657 | |
658 | u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, |
659 | PyDict_GET_SIZE(u->u_cellvars)); |
660 | if (!u->u_freevars) { |
661 | compiler_unit_free(u); |
662 | return 0; |
663 | } |
664 | |
665 | u->u_blocks = NULL; |
666 | u->u_nfblocks = 0; |
667 | u->u_firstlineno = lineno; |
668 | u->u_lineno = 0; |
669 | u->u_col_offset = 0; |
670 | u->u_end_lineno = 0; |
671 | u->u_end_col_offset = 0; |
672 | u->u_consts = PyDict_New(); |
673 | if (!u->u_consts) { |
674 | compiler_unit_free(u); |
675 | return 0; |
676 | } |
677 | u->u_names = PyDict_New(); |
678 | if (!u->u_names) { |
679 | compiler_unit_free(u); |
680 | return 0; |
681 | } |
682 | |
683 | u->u_private = NULL; |
684 | |
685 | /* Push the old compiler_unit on the stack. */ |
686 | if (c->u) { |
687 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
688 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
689 | Py_XDECREF(capsule); |
690 | compiler_unit_free(u); |
691 | return 0; |
692 | } |
693 | Py_DECREF(capsule); |
694 | u->u_private = c->u->u_private; |
695 | Py_XINCREF(u->u_private); |
696 | } |
697 | c->u = u; |
698 | |
699 | c->c_nestlevel++; |
700 | |
701 | block = compiler_new_block(c); |
702 | if (block == NULL) |
703 | return 0; |
704 | c->u->u_curblock = block; |
705 | |
706 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
707 | if (!compiler_set_qualname(c)) |
708 | return 0; |
709 | } |
710 | |
711 | return 1; |
712 | } |
713 | |
714 | static void |
715 | compiler_exit_scope(struct compiler *c) |
716 | { |
717 | // Don't call PySequence_DelItem() with an exception raised |
718 | PyObject *exc_type, *exc_val, *exc_tb; |
719 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
720 | |
721 | c->c_nestlevel--; |
722 | compiler_unit_free(c->u); |
723 | /* Restore c->u to the parent unit. */ |
724 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
725 | if (n >= 0) { |
726 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
727 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
728 | assert(c->u); |
729 | /* we are deleting from a list so this really shouldn't fail */ |
730 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
731 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
732 | "stack item" , NULL); |
733 | } |
734 | compiler_unit_check(c->u); |
735 | } |
736 | else { |
737 | c->u = NULL; |
738 | } |
739 | |
740 | PyErr_Restore(exc_type, exc_val, exc_tb); |
741 | } |
742 | |
743 | static int |
744 | compiler_set_qualname(struct compiler *c) |
745 | { |
746 | _Py_static_string(dot, "." ); |
747 | _Py_static_string(dot_locals, ".<locals>" ); |
748 | Py_ssize_t stack_size; |
749 | struct compiler_unit *u = c->u; |
750 | PyObject *name, *base, *dot_str, *dot_locals_str; |
751 | |
752 | base = NULL; |
753 | stack_size = PyList_GET_SIZE(c->c_stack); |
754 | assert(stack_size >= 1); |
755 | if (stack_size > 1) { |
756 | int scope, force_global = 0; |
757 | struct compiler_unit *parent; |
758 | PyObject *mangled, *capsule; |
759 | |
760 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
761 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
762 | assert(parent); |
763 | |
764 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
765 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
766 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
767 | assert(u->u_name); |
768 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
769 | if (!mangled) |
770 | return 0; |
771 | scope = _PyST_GetScope(parent->u_ste, mangled); |
772 | Py_DECREF(mangled); |
773 | assert(scope != GLOBAL_IMPLICIT); |
774 | if (scope == GLOBAL_EXPLICIT) |
775 | force_global = 1; |
776 | } |
777 | |
778 | if (!force_global) { |
779 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
780 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
781 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
782 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
783 | if (dot_locals_str == NULL) |
784 | return 0; |
785 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
786 | if (base == NULL) |
787 | return 0; |
788 | } |
789 | else { |
790 | Py_INCREF(parent->u_qualname); |
791 | base = parent->u_qualname; |
792 | } |
793 | } |
794 | } |
795 | |
796 | if (base != NULL) { |
797 | dot_str = _PyUnicode_FromId(&dot); |
798 | if (dot_str == NULL) { |
799 | Py_DECREF(base); |
800 | return 0; |
801 | } |
802 | name = PyUnicode_Concat(base, dot_str); |
803 | Py_DECREF(base); |
804 | if (name == NULL) |
805 | return 0; |
806 | PyUnicode_Append(&name, u->u_name); |
807 | if (name == NULL) |
808 | return 0; |
809 | } |
810 | else { |
811 | Py_INCREF(u->u_name); |
812 | name = u->u_name; |
813 | } |
814 | u->u_qualname = name; |
815 | |
816 | return 1; |
817 | } |
818 | |
819 | |
820 | /* Allocate a new block and return a pointer to it. |
821 | Returns NULL on error. |
822 | */ |
823 | |
824 | static basicblock * |
825 | compiler_new_block(struct compiler *c) |
826 | { |
827 | basicblock *b; |
828 | struct compiler_unit *u; |
829 | |
830 | u = c->u; |
831 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
832 | if (b == NULL) { |
833 | PyErr_NoMemory(); |
834 | return NULL; |
835 | } |
836 | /* Extend the singly linked list of blocks with new block. */ |
837 | b->b_list = u->u_blocks; |
838 | u->u_blocks = b; |
839 | return b; |
840 | } |
841 | |
842 | static basicblock * |
843 | compiler_next_block(struct compiler *c) |
844 | { |
845 | basicblock *block = compiler_new_block(c); |
846 | if (block == NULL) |
847 | return NULL; |
848 | c->u->u_curblock->b_next = block; |
849 | c->u->u_curblock = block; |
850 | return block; |
851 | } |
852 | |
853 | static basicblock * |
854 | compiler_use_next_block(struct compiler *c, basicblock *block) |
855 | { |
856 | assert(block != NULL); |
857 | c->u->u_curblock->b_next = block; |
858 | c->u->u_curblock = block; |
859 | return block; |
860 | } |
861 | |
862 | static basicblock * |
863 | compiler_copy_block(struct compiler *c, basicblock *block) |
864 | { |
865 | /* Cannot copy a block if it has a fallthrough, since |
866 | * a block can only have one fallthrough predecessor. |
867 | */ |
868 | assert(block->b_nofallthrough); |
869 | basicblock *result = compiler_new_block(c); |
870 | if (result == NULL) { |
871 | return NULL; |
872 | } |
873 | for (int i = 0; i < block->b_iused; i++) { |
874 | int n = compiler_next_instr(result); |
875 | if (n < 0) { |
876 | return NULL; |
877 | } |
878 | result->b_instr[n] = block->b_instr[i]; |
879 | } |
880 | result->b_exit = block->b_exit; |
881 | result->b_nofallthrough = 1; |
882 | return result; |
883 | } |
884 | |
885 | /* Returns the offset of the next instruction in the current block's |
886 | b_instr array. Resizes the b_instr as necessary. |
887 | Returns -1 on failure. |
888 | */ |
889 | |
890 | static int |
891 | compiler_next_instr(basicblock *b) |
892 | { |
893 | assert(b != NULL); |
894 | if (b->b_instr == NULL) { |
895 | b->b_instr = (struct instr *)PyObject_Calloc( |
896 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
897 | if (b->b_instr == NULL) { |
898 | PyErr_NoMemory(); |
899 | return -1; |
900 | } |
901 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
902 | } |
903 | else if (b->b_iused == b->b_ialloc) { |
904 | struct instr *tmp; |
905 | size_t oldsize, newsize; |
906 | oldsize = b->b_ialloc * sizeof(struct instr); |
907 | newsize = oldsize << 1; |
908 | |
909 | if (oldsize > (SIZE_MAX >> 1)) { |
910 | PyErr_NoMemory(); |
911 | return -1; |
912 | } |
913 | |
914 | if (newsize == 0) { |
915 | PyErr_NoMemory(); |
916 | return -1; |
917 | } |
918 | b->b_ialloc <<= 1; |
919 | tmp = (struct instr *)PyObject_Realloc( |
920 | (void *)b->b_instr, newsize); |
921 | if (tmp == NULL) { |
922 | PyErr_NoMemory(); |
923 | return -1; |
924 | } |
925 | b->b_instr = tmp; |
926 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
927 | } |
928 | return b->b_iused++; |
929 | } |
930 | |
931 | /* Set the line number and column offset for the following instructions. |
932 | |
933 | The line number is reset in the following cases: |
934 | - when entering a new scope |
935 | - on each statement |
936 | - on each expression and sub-expression |
937 | - before the "except" and "finally" clauses |
938 | */ |
939 | |
940 | #define SET_LOC(c, x) \ |
941 | (c)->u->u_lineno = (x)->lineno; \ |
942 | (c)->u->u_col_offset = (x)->col_offset; \ |
943 | (c)->u->u_end_lineno = (x)->end_lineno; \ |
944 | (c)->u->u_end_col_offset = (x)->end_col_offset; |
945 | |
946 | /* Return the stack effect of opcode with argument oparg. |
947 | |
948 | Some opcodes have different stack effect when jump to the target and |
949 | when not jump. The 'jump' parameter specifies the case: |
950 | |
951 | * 0 -- when not jump |
952 | * 1 -- when jump |
953 | * -1 -- maximal |
954 | */ |
955 | static int |
956 | stack_effect(int opcode, int oparg, int jump) |
957 | { |
958 | switch (opcode) { |
959 | case NOP: |
960 | case EXTENDED_ARG: |
961 | return 0; |
962 | |
963 | /* Stack manipulation */ |
964 | case POP_TOP: |
965 | return -1; |
966 | case ROT_TWO: |
967 | case ROT_THREE: |
968 | case ROT_FOUR: |
969 | return 0; |
970 | case DUP_TOP: |
971 | return 1; |
972 | case DUP_TOP_TWO: |
973 | return 2; |
974 | |
975 | /* Unary operators */ |
976 | case UNARY_POSITIVE: |
977 | case UNARY_NEGATIVE: |
978 | case UNARY_NOT: |
979 | case UNARY_INVERT: |
980 | return 0; |
981 | |
982 | case SET_ADD: |
983 | case LIST_APPEND: |
984 | return -1; |
985 | case MAP_ADD: |
986 | return -2; |
987 | |
988 | /* Binary operators */ |
989 | case BINARY_POWER: |
990 | case BINARY_MULTIPLY: |
991 | case BINARY_MATRIX_MULTIPLY: |
992 | case BINARY_MODULO: |
993 | case BINARY_ADD: |
994 | case BINARY_SUBTRACT: |
995 | case BINARY_SUBSCR: |
996 | case BINARY_FLOOR_DIVIDE: |
997 | case BINARY_TRUE_DIVIDE: |
998 | return -1; |
999 | case INPLACE_FLOOR_DIVIDE: |
1000 | case INPLACE_TRUE_DIVIDE: |
1001 | return -1; |
1002 | |
1003 | case INPLACE_ADD: |
1004 | case INPLACE_SUBTRACT: |
1005 | case INPLACE_MULTIPLY: |
1006 | case INPLACE_MATRIX_MULTIPLY: |
1007 | case INPLACE_MODULO: |
1008 | return -1; |
1009 | case STORE_SUBSCR: |
1010 | return -3; |
1011 | case DELETE_SUBSCR: |
1012 | return -2; |
1013 | |
1014 | case BINARY_LSHIFT: |
1015 | case BINARY_RSHIFT: |
1016 | case BINARY_AND: |
1017 | case BINARY_XOR: |
1018 | case BINARY_OR: |
1019 | return -1; |
1020 | case INPLACE_POWER: |
1021 | return -1; |
1022 | case GET_ITER: |
1023 | return 0; |
1024 | |
1025 | case PRINT_EXPR: |
1026 | return -1; |
1027 | case LOAD_BUILD_CLASS: |
1028 | return 1; |
1029 | case INPLACE_LSHIFT: |
1030 | case INPLACE_RSHIFT: |
1031 | case INPLACE_AND: |
1032 | case INPLACE_XOR: |
1033 | case INPLACE_OR: |
1034 | return -1; |
1035 | |
1036 | case SETUP_WITH: |
1037 | /* 1 in the normal flow. |
1038 | * Restore the stack position and push 6 values before jumping to |
1039 | * the handler if an exception be raised. */ |
1040 | return jump ? 6 : 1; |
1041 | case RETURN_VALUE: |
1042 | return -1; |
1043 | case IMPORT_STAR: |
1044 | return -1; |
1045 | case SETUP_ANNOTATIONS: |
1046 | return 0; |
1047 | case YIELD_VALUE: |
1048 | return 0; |
1049 | case YIELD_FROM: |
1050 | return -1; |
1051 | case POP_BLOCK: |
1052 | return 0; |
1053 | case POP_EXCEPT: |
1054 | return -3; |
1055 | |
1056 | case STORE_NAME: |
1057 | return -1; |
1058 | case DELETE_NAME: |
1059 | return 0; |
1060 | case UNPACK_SEQUENCE: |
1061 | return oparg-1; |
1062 | case UNPACK_EX: |
1063 | return (oparg&0xFF) + (oparg>>8); |
1064 | case FOR_ITER: |
1065 | /* -1 at end of iterator, 1 if continue iterating. */ |
1066 | return jump > 0 ? -1 : 1; |
1067 | |
1068 | case STORE_ATTR: |
1069 | return -2; |
1070 | case DELETE_ATTR: |
1071 | return -1; |
1072 | case STORE_GLOBAL: |
1073 | return -1; |
1074 | case DELETE_GLOBAL: |
1075 | return 0; |
1076 | case LOAD_CONST: |
1077 | return 1; |
1078 | case LOAD_NAME: |
1079 | return 1; |
1080 | case BUILD_TUPLE: |
1081 | case BUILD_LIST: |
1082 | case BUILD_SET: |
1083 | case BUILD_STRING: |
1084 | return 1-oparg; |
1085 | case BUILD_MAP: |
1086 | return 1 - 2*oparg; |
1087 | case BUILD_CONST_KEY_MAP: |
1088 | return -oparg; |
1089 | case LOAD_ATTR: |
1090 | return 0; |
1091 | case COMPARE_OP: |
1092 | case IS_OP: |
1093 | case CONTAINS_OP: |
1094 | return -1; |
1095 | case JUMP_IF_NOT_EXC_MATCH: |
1096 | return -2; |
1097 | case IMPORT_NAME: |
1098 | return -1; |
1099 | case IMPORT_FROM: |
1100 | return 1; |
1101 | |
1102 | /* Jumps */ |
1103 | case JUMP_FORWARD: |
1104 | case JUMP_ABSOLUTE: |
1105 | return 0; |
1106 | |
1107 | case JUMP_IF_TRUE_OR_POP: |
1108 | case JUMP_IF_FALSE_OR_POP: |
1109 | return jump ? 0 : -1; |
1110 | |
1111 | case POP_JUMP_IF_FALSE: |
1112 | case POP_JUMP_IF_TRUE: |
1113 | return -1; |
1114 | |
1115 | case LOAD_GLOBAL: |
1116 | return 1; |
1117 | |
1118 | /* Exception handling */ |
1119 | case SETUP_FINALLY: |
1120 | /* 0 in the normal flow. |
1121 | * Restore the stack position and push 6 values before jumping to |
1122 | * the handler if an exception be raised. */ |
1123 | return jump ? 6 : 0; |
1124 | case RERAISE: |
1125 | return -3; |
1126 | |
1127 | case WITH_EXCEPT_START: |
1128 | return 1; |
1129 | |
1130 | case LOAD_FAST: |
1131 | return 1; |
1132 | case STORE_FAST: |
1133 | return -1; |
1134 | case DELETE_FAST: |
1135 | return 0; |
1136 | |
1137 | case RAISE_VARARGS: |
1138 | return -oparg; |
1139 | |
1140 | /* Functions and calls */ |
1141 | case CALL_FUNCTION: |
1142 | return -oparg; |
1143 | case CALL_METHOD: |
1144 | return -oparg-1; |
1145 | case CALL_FUNCTION_KW: |
1146 | return -oparg-1; |
1147 | case CALL_FUNCTION_EX: |
1148 | return -1 - ((oparg & 0x01) != 0); |
1149 | case MAKE_FUNCTION: |
1150 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
1151 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
1152 | case BUILD_SLICE: |
1153 | if (oparg == 3) |
1154 | return -2; |
1155 | else |
1156 | return -1; |
1157 | |
1158 | /* Closures */ |
1159 | case LOAD_CLOSURE: |
1160 | return 1; |
1161 | case LOAD_DEREF: |
1162 | case LOAD_CLASSDEREF: |
1163 | return 1; |
1164 | case STORE_DEREF: |
1165 | return -1; |
1166 | case DELETE_DEREF: |
1167 | return 0; |
1168 | |
1169 | /* Iterators and generators */ |
1170 | case GET_AWAITABLE: |
1171 | return 0; |
1172 | case SETUP_ASYNC_WITH: |
1173 | /* 0 in the normal flow. |
1174 | * Restore the stack position to the position before the result |
1175 | * of __aenter__ and push 6 values before jumping to the handler |
1176 | * if an exception be raised. */ |
1177 | return jump ? -1 + 6 : 0; |
1178 | case BEFORE_ASYNC_WITH: |
1179 | return 1; |
1180 | case GET_AITER: |
1181 | return 0; |
1182 | case GET_ANEXT: |
1183 | return 1; |
1184 | case GET_YIELD_FROM_ITER: |
1185 | return 0; |
1186 | case END_ASYNC_FOR: |
1187 | return -7; |
1188 | case FORMAT_VALUE: |
1189 | /* If there's a fmt_spec on the stack, we go from 2->1, |
1190 | else 1->1. */ |
1191 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
1192 | case LOAD_METHOD: |
1193 | return 1; |
1194 | case LOAD_ASSERTION_ERROR: |
1195 | return 1; |
1196 | case LIST_TO_TUPLE: |
1197 | return 0; |
1198 | case GEN_START: |
1199 | return -1; |
1200 | case LIST_EXTEND: |
1201 | case SET_UPDATE: |
1202 | case DICT_MERGE: |
1203 | case DICT_UPDATE: |
1204 | return -1; |
1205 | case COPY_DICT_WITHOUT_KEYS: |
1206 | return 0; |
1207 | case MATCH_CLASS: |
1208 | return -1; |
1209 | case GET_LEN: |
1210 | case MATCH_MAPPING: |
1211 | case MATCH_SEQUENCE: |
1212 | return 1; |
1213 | case MATCH_KEYS: |
1214 | return 2; |
1215 | case ROT_N: |
1216 | return 0; |
1217 | default: |
1218 | return PY_INVALID_STACK_EFFECT; |
1219 | } |
1220 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
1221 | } |
1222 | |
1223 | int |
1224 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
1225 | { |
1226 | return stack_effect(opcode, oparg, jump); |
1227 | } |
1228 | |
1229 | int |
1230 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
1231 | { |
1232 | return stack_effect(opcode, oparg, -1); |
1233 | } |
1234 | |
1235 | /* Add an opcode with no argument. |
1236 | Returns 0 on failure, 1 on success. |
1237 | */ |
1238 | |
1239 | static int |
1240 | compiler_addop_line(struct compiler *c, int opcode, int line) |
1241 | { |
1242 | basicblock *b; |
1243 | struct instr *i; |
1244 | int off; |
1245 | assert(!HAS_ARG(opcode)); |
1246 | off = compiler_next_instr(c->u->u_curblock); |
1247 | if (off < 0) |
1248 | return 0; |
1249 | b = c->u->u_curblock; |
1250 | i = &b->b_instr[off]; |
1251 | i->i_opcode = opcode; |
1252 | i->i_oparg = 0; |
1253 | if (opcode == RETURN_VALUE) |
1254 | b->b_return = 1; |
1255 | i->i_lineno = line; |
1256 | return 1; |
1257 | } |
1258 | |
1259 | static int |
1260 | compiler_addop(struct compiler *c, int opcode) |
1261 | { |
1262 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
1263 | } |
1264 | |
1265 | static int |
1266 | compiler_addop_noline(struct compiler *c, int opcode) |
1267 | { |
1268 | return compiler_addop_line(c, opcode, -1); |
1269 | } |
1270 | |
1271 | |
1272 | static Py_ssize_t |
1273 | compiler_add_o(PyObject *dict, PyObject *o) |
1274 | { |
1275 | PyObject *v; |
1276 | Py_ssize_t arg; |
1277 | |
1278 | v = PyDict_GetItemWithError(dict, o); |
1279 | if (!v) { |
1280 | if (PyErr_Occurred()) { |
1281 | return -1; |
1282 | } |
1283 | arg = PyDict_GET_SIZE(dict); |
1284 | v = PyLong_FromSsize_t(arg); |
1285 | if (!v) { |
1286 | return -1; |
1287 | } |
1288 | if (PyDict_SetItem(dict, o, v) < 0) { |
1289 | Py_DECREF(v); |
1290 | return -1; |
1291 | } |
1292 | Py_DECREF(v); |
1293 | } |
1294 | else |
1295 | arg = PyLong_AsLong(v); |
1296 | return arg; |
1297 | } |
1298 | |
1299 | // Merge const *o* recursively and return constant key object. |
1300 | static PyObject* |
1301 | merge_consts_recursive(struct compiler *c, PyObject *o) |
1302 | { |
1303 | // None and Ellipsis are singleton, and key is the singleton. |
1304 | // No need to merge object and key. |
1305 | if (o == Py_None || o == Py_Ellipsis) { |
1306 | Py_INCREF(o); |
1307 | return o; |
1308 | } |
1309 | |
1310 | PyObject *key = _PyCode_ConstantKey(o); |
1311 | if (key == NULL) { |
1312 | return NULL; |
1313 | } |
1314 | |
1315 | // t is borrowed reference |
1316 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
1317 | if (t != key) { |
1318 | // o is registered in c_const_cache. Just use it. |
1319 | Py_XINCREF(t); |
1320 | Py_DECREF(key); |
1321 | return t; |
1322 | } |
1323 | |
1324 | // We registered o in c_const_cache. |
1325 | // When o is a tuple or frozenset, we want to merge its |
1326 | // items too. |
1327 | if (PyTuple_CheckExact(o)) { |
1328 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
1329 | for (Py_ssize_t i = 0; i < len; i++) { |
1330 | PyObject *item = PyTuple_GET_ITEM(o, i); |
1331 | PyObject *u = merge_consts_recursive(c, item); |
1332 | if (u == NULL) { |
1333 | Py_DECREF(key); |
1334 | return NULL; |
1335 | } |
1336 | |
1337 | // See _PyCode_ConstantKey() |
1338 | PyObject *v; // borrowed |
1339 | if (PyTuple_CheckExact(u)) { |
1340 | v = PyTuple_GET_ITEM(u, 1); |
1341 | } |
1342 | else { |
1343 | v = u; |
1344 | } |
1345 | if (v != item) { |
1346 | Py_INCREF(v); |
1347 | PyTuple_SET_ITEM(o, i, v); |
1348 | Py_DECREF(item); |
1349 | } |
1350 | |
1351 | Py_DECREF(u); |
1352 | } |
1353 | } |
1354 | else if (PyFrozenSet_CheckExact(o)) { |
1355 | // *key* is tuple. And its first item is frozenset of |
1356 | // constant keys. |
1357 | // See _PyCode_ConstantKey() for detail. |
1358 | assert(PyTuple_CheckExact(key)); |
1359 | assert(PyTuple_GET_SIZE(key) == 2); |
1360 | |
1361 | Py_ssize_t len = PySet_GET_SIZE(o); |
1362 | if (len == 0) { // empty frozenset should not be re-created. |
1363 | return key; |
1364 | } |
1365 | PyObject *tuple = PyTuple_New(len); |
1366 | if (tuple == NULL) { |
1367 | Py_DECREF(key); |
1368 | return NULL; |
1369 | } |
1370 | Py_ssize_t i = 0, pos = 0; |
1371 | PyObject *item; |
1372 | Py_hash_t hash; |
1373 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
1374 | PyObject *k = merge_consts_recursive(c, item); |
1375 | if (k == NULL) { |
1376 | Py_DECREF(tuple); |
1377 | Py_DECREF(key); |
1378 | return NULL; |
1379 | } |
1380 | PyObject *u; |
1381 | if (PyTuple_CheckExact(k)) { |
1382 | u = PyTuple_GET_ITEM(k, 1); |
1383 | Py_INCREF(u); |
1384 | Py_DECREF(k); |
1385 | } |
1386 | else { |
1387 | u = k; |
1388 | } |
1389 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
1390 | i++; |
1391 | } |
1392 | |
1393 | // Instead of rewriting o, we create new frozenset and embed in the |
1394 | // key tuple. Caller should get merged frozenset from the key tuple. |
1395 | PyObject *new = PyFrozenSet_New(tuple); |
1396 | Py_DECREF(tuple); |
1397 | if (new == NULL) { |
1398 | Py_DECREF(key); |
1399 | return NULL; |
1400 | } |
1401 | assert(PyTuple_GET_ITEM(key, 1) == o); |
1402 | Py_DECREF(o); |
1403 | PyTuple_SET_ITEM(key, 1, new); |
1404 | } |
1405 | |
1406 | return key; |
1407 | } |
1408 | |
1409 | static Py_ssize_t |
1410 | compiler_add_const(struct compiler *c, PyObject *o) |
1411 | { |
1412 | PyObject *key = merge_consts_recursive(c, o); |
1413 | if (key == NULL) { |
1414 | return -1; |
1415 | } |
1416 | |
1417 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
1418 | Py_DECREF(key); |
1419 | return arg; |
1420 | } |
1421 | |
1422 | static int |
1423 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
1424 | { |
1425 | Py_ssize_t arg = compiler_add_const(c, o); |
1426 | if (arg < 0) |
1427 | return 0; |
1428 | return compiler_addop_i(c, LOAD_CONST, arg); |
1429 | } |
1430 | |
1431 | static int |
1432 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
1433 | PyObject *o) |
1434 | { |
1435 | Py_ssize_t arg = compiler_add_o(dict, o); |
1436 | if (arg < 0) |
1437 | return 0; |
1438 | return compiler_addop_i(c, opcode, arg); |
1439 | } |
1440 | |
1441 | static int |
1442 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
1443 | PyObject *o) |
1444 | { |
1445 | Py_ssize_t arg; |
1446 | |
1447 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
1448 | if (!mangled) |
1449 | return 0; |
1450 | arg = compiler_add_o(dict, mangled); |
1451 | Py_DECREF(mangled); |
1452 | if (arg < 0) |
1453 | return 0; |
1454 | return compiler_addop_i(c, opcode, arg); |
1455 | } |
1456 | |
1457 | /* Add an opcode with an integer argument. |
1458 | Returns 0 on failure, 1 on success. |
1459 | */ |
1460 | |
1461 | static int |
1462 | compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno) |
1463 | { |
1464 | struct instr *i; |
1465 | int off; |
1466 | |
1467 | /* oparg value is unsigned, but a signed C int is usually used to store |
1468 | it in the C code (like Python/ceval.c). |
1469 | |
1470 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
1471 | |
1472 | The argument of a concrete bytecode instruction is limited to 8-bit. |
1473 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
1474 | assert(HAS_ARG(opcode)); |
1475 | assert(0 <= oparg && oparg <= 2147483647); |
1476 | |
1477 | off = compiler_next_instr(c->u->u_curblock); |
1478 | if (off < 0) |
1479 | return 0; |
1480 | i = &c->u->u_curblock->b_instr[off]; |
1481 | i->i_opcode = opcode; |
1482 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
1483 | i->i_lineno = lineno; |
1484 | return 1; |
1485 | } |
1486 | |
1487 | static int |
1488 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
1489 | { |
1490 | return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno); |
1491 | } |
1492 | |
1493 | static int |
1494 | compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg) |
1495 | { |
1496 | return compiler_addop_i_line(c, opcode, oparg, -1); |
1497 | } |
1498 | |
1499 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
1500 | { |
1501 | assert(HAS_ARG(opcode)); |
1502 | assert(b != NULL); |
1503 | assert(target != NULL); |
1504 | |
1505 | int off = compiler_next_instr(b); |
1506 | struct instr *i = &b->b_instr[off]; |
1507 | if (off < 0) { |
1508 | return 0; |
1509 | } |
1510 | i->i_opcode = opcode; |
1511 | i->i_target = target; |
1512 | i->i_lineno = lineno; |
1513 | return 1; |
1514 | } |
1515 | |
1516 | static int |
1517 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
1518 | { |
1519 | return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b); |
1520 | } |
1521 | |
1522 | static int |
1523 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
1524 | { |
1525 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
1526 | } |
1527 | |
1528 | /* NEXT_BLOCK() creates an implicit jump from the current block |
1529 | to the new block. |
1530 | |
1531 | The returns inside this macro make it impossible to decref objects |
1532 | created in the local function. Local objects should use the arena. |
1533 | */ |
1534 | #define NEXT_BLOCK(C) { \ |
1535 | if (compiler_next_block((C)) == NULL) \ |
1536 | return 0; \ |
1537 | } |
1538 | |
1539 | #define ADDOP(C, OP) { \ |
1540 | if (!compiler_addop((C), (OP))) \ |
1541 | return 0; \ |
1542 | } |
1543 | |
1544 | #define ADDOP_NOLINE(C, OP) { \ |
1545 | if (!compiler_addop_noline((C), (OP))) \ |
1546 | return 0; \ |
1547 | } |
1548 | |
1549 | #define ADDOP_IN_SCOPE(C, OP) { \ |
1550 | if (!compiler_addop((C), (OP))) { \ |
1551 | compiler_exit_scope(c); \ |
1552 | return 0; \ |
1553 | } \ |
1554 | } |
1555 | |
1556 | #define ADDOP_LOAD_CONST(C, O) { \ |
1557 | if (!compiler_addop_load_const((C), (O))) \ |
1558 | return 0; \ |
1559 | } |
1560 | |
1561 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
1562 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
1563 | PyObject *__new_const = (O); \ |
1564 | if (__new_const == NULL) { \ |
1565 | return 0; \ |
1566 | } \ |
1567 | if (!compiler_addop_load_const((C), __new_const)) { \ |
1568 | Py_DECREF(__new_const); \ |
1569 | return 0; \ |
1570 | } \ |
1571 | Py_DECREF(__new_const); \ |
1572 | } |
1573 | |
1574 | #define ADDOP_O(C, OP, O, TYPE) { \ |
1575 | assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST */ \ |
1576 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
1577 | return 0; \ |
1578 | } |
1579 | |
1580 | /* Same as ADDOP_O, but steals a reference. */ |
1581 | #define ADDOP_N(C, OP, O, TYPE) { \ |
1582 | assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST_NEW */ \ |
1583 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
1584 | Py_DECREF((O)); \ |
1585 | return 0; \ |
1586 | } \ |
1587 | Py_DECREF((O)); \ |
1588 | } |
1589 | |
1590 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
1591 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
1592 | return 0; \ |
1593 | } |
1594 | |
1595 | #define ADDOP_I(C, OP, O) { \ |
1596 | if (!compiler_addop_i((C), (OP), (O))) \ |
1597 | return 0; \ |
1598 | } |
1599 | |
1600 | #define ADDOP_I_NOLINE(C, OP, O) { \ |
1601 | if (!compiler_addop_i_noline((C), (OP), (O))) \ |
1602 | return 0; \ |
1603 | } |
1604 | |
1605 | #define ADDOP_JUMP(C, OP, O) { \ |
1606 | if (!compiler_addop_j((C), (OP), (O))) \ |
1607 | return 0; \ |
1608 | } |
1609 | |
1610 | /* Add a jump with no line number. |
1611 | * Used for artificial jumps that have no corresponding |
1612 | * token in the source code. */ |
1613 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
1614 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
1615 | return 0; \ |
1616 | } |
1617 | |
1618 | #define ADDOP_COMPARE(C, CMP) { \ |
1619 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
1620 | return 0; \ |
1621 | } |
1622 | |
1623 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
1624 | the ASDL name to synthesize the name of the C type and the visit function. |
1625 | */ |
1626 | |
1627 | #define VISIT(C, TYPE, V) {\ |
1628 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
1629 | return 0; \ |
1630 | } |
1631 | |
1632 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
1633 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
1634 | compiler_exit_scope(c); \ |
1635 | return 0; \ |
1636 | } \ |
1637 | } |
1638 | |
1639 | #define VISIT_SLICE(C, V, CTX) {\ |
1640 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
1641 | return 0; \ |
1642 | } |
1643 | |
1644 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
1645 | int _i; \ |
1646 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
1647 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
1648 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
1649 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
1650 | return 0; \ |
1651 | } \ |
1652 | } |
1653 | |
1654 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
1655 | int _i; \ |
1656 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
1657 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
1658 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
1659 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
1660 | compiler_exit_scope(c); \ |
1661 | return 0; \ |
1662 | } \ |
1663 | } \ |
1664 | } |
1665 | |
1666 | #define RETURN_IF_FALSE(X) \ |
1667 | if (!(X)) { \ |
1668 | return 0; \ |
1669 | } |
1670 | |
1671 | /* Search if variable annotations are present statically in a block. */ |
1672 | |
1673 | static int |
1674 | find_ann(asdl_stmt_seq *stmts) |
1675 | { |
1676 | int i, j, res = 0; |
1677 | stmt_ty st; |
1678 | |
1679 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
1680 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
1681 | switch (st->kind) { |
1682 | case AnnAssign_kind: |
1683 | return 1; |
1684 | case For_kind: |
1685 | res = find_ann(st->v.For.body) || |
1686 | find_ann(st->v.For.orelse); |
1687 | break; |
1688 | case AsyncFor_kind: |
1689 | res = find_ann(st->v.AsyncFor.body) || |
1690 | find_ann(st->v.AsyncFor.orelse); |
1691 | break; |
1692 | case While_kind: |
1693 | res = find_ann(st->v.While.body) || |
1694 | find_ann(st->v.While.orelse); |
1695 | break; |
1696 | case If_kind: |
1697 | res = find_ann(st->v.If.body) || |
1698 | find_ann(st->v.If.orelse); |
1699 | break; |
1700 | case With_kind: |
1701 | res = find_ann(st->v.With.body); |
1702 | break; |
1703 | case AsyncWith_kind: |
1704 | res = find_ann(st->v.AsyncWith.body); |
1705 | break; |
1706 | case Try_kind: |
1707 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
1708 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
1709 | st->v.Try.handlers, j); |
1710 | if (find_ann(handler->v.ExceptHandler.body)) { |
1711 | return 1; |
1712 | } |
1713 | } |
1714 | res = find_ann(st->v.Try.body) || |
1715 | find_ann(st->v.Try.finalbody) || |
1716 | find_ann(st->v.Try.orelse); |
1717 | break; |
1718 | default: |
1719 | res = 0; |
1720 | } |
1721 | if (res) { |
1722 | break; |
1723 | } |
1724 | } |
1725 | return res; |
1726 | } |
1727 | |
1728 | /* |
1729 | * Frame block handling functions |
1730 | */ |
1731 | |
1732 | static int |
1733 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
1734 | basicblock *exit, void *datum) |
1735 | { |
1736 | struct fblockinfo *f; |
1737 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
1738 | return compiler_error(c, "too many statically nested blocks" ); |
1739 | } |
1740 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
1741 | f->fb_type = t; |
1742 | f->fb_block = b; |
1743 | f->fb_exit = exit; |
1744 | f->fb_datum = datum; |
1745 | return 1; |
1746 | } |
1747 | |
1748 | static void |
1749 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
1750 | { |
1751 | struct compiler_unit *u = c->u; |
1752 | assert(u->u_nfblocks > 0); |
1753 | u->u_nfblocks--; |
1754 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
1755 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
1756 | } |
1757 | |
1758 | static int |
1759 | compiler_call_exit_with_nones(struct compiler *c) { |
1760 | ADDOP_LOAD_CONST(c, Py_None); |
1761 | ADDOP(c, DUP_TOP); |
1762 | ADDOP(c, DUP_TOP); |
1763 | ADDOP_I(c, CALL_FUNCTION, 3); |
1764 | return 1; |
1765 | } |
1766 | |
1767 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
1768 | * popping the blocks will be restored afterwards, unless another |
1769 | * return, break or continue is found. In which case, the TOS will |
1770 | * be popped. |
1771 | */ |
1772 | static int |
1773 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
1774 | int preserve_tos) |
1775 | { |
1776 | switch (info->fb_type) { |
1777 | case WHILE_LOOP: |
1778 | case EXCEPTION_HANDLER: |
1779 | case ASYNC_COMPREHENSION_GENERATOR: |
1780 | return 1; |
1781 | |
1782 | case FOR_LOOP: |
1783 | /* Pop the iterator */ |
1784 | if (preserve_tos) { |
1785 | ADDOP(c, ROT_TWO); |
1786 | } |
1787 | ADDOP(c, POP_TOP); |
1788 | return 1; |
1789 | |
1790 | case TRY_EXCEPT: |
1791 | ADDOP(c, POP_BLOCK); |
1792 | return 1; |
1793 | |
1794 | case FINALLY_TRY: |
1795 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
1796 | ADDOP(c, POP_BLOCK); |
1797 | if (preserve_tos) { |
1798 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
1799 | return 0; |
1800 | } |
1801 | } |
1802 | /* Emit the finally block */ |
1803 | VISIT_SEQ(c, stmt, info->fb_datum); |
1804 | if (preserve_tos) { |
1805 | compiler_pop_fblock(c, POP_VALUE, NULL); |
1806 | } |
1807 | /* The finally block should appear to execute after the |
1808 | * statement causing the unwinding, so make the unwinding |
1809 | * instruction artificial */ |
1810 | c->u->u_lineno = -1; |
1811 | return 1; |
1812 | |
1813 | case FINALLY_END: |
1814 | if (preserve_tos) { |
1815 | ADDOP(c, ROT_FOUR); |
1816 | } |
1817 | ADDOP(c, POP_TOP); |
1818 | ADDOP(c, POP_TOP); |
1819 | ADDOP(c, POP_TOP); |
1820 | if (preserve_tos) { |
1821 | ADDOP(c, ROT_FOUR); |
1822 | } |
1823 | ADDOP(c, POP_EXCEPT); |
1824 | return 1; |
1825 | |
1826 | case WITH: |
1827 | case ASYNC_WITH: |
1828 | SET_LOC(c, (stmt_ty)info->fb_datum); |
1829 | ADDOP(c, POP_BLOCK); |
1830 | if (preserve_tos) { |
1831 | ADDOP(c, ROT_TWO); |
1832 | } |
1833 | if(!compiler_call_exit_with_nones(c)) { |
1834 | return 0; |
1835 | } |
1836 | if (info->fb_type == ASYNC_WITH) { |
1837 | ADDOP(c, GET_AWAITABLE); |
1838 | ADDOP_LOAD_CONST(c, Py_None); |
1839 | ADDOP(c, YIELD_FROM); |
1840 | } |
1841 | ADDOP(c, POP_TOP); |
1842 | /* The exit block should appear to execute after the |
1843 | * statement causing the unwinding, so make the unwinding |
1844 | * instruction artificial */ |
1845 | c->u->u_lineno = -1; |
1846 | return 1; |
1847 | |
1848 | case HANDLER_CLEANUP: |
1849 | if (info->fb_datum) { |
1850 | ADDOP(c, POP_BLOCK); |
1851 | } |
1852 | if (preserve_tos) { |
1853 | ADDOP(c, ROT_FOUR); |
1854 | } |
1855 | ADDOP(c, POP_EXCEPT); |
1856 | if (info->fb_datum) { |
1857 | ADDOP_LOAD_CONST(c, Py_None); |
1858 | compiler_nameop(c, info->fb_datum, Store); |
1859 | compiler_nameop(c, info->fb_datum, Del); |
1860 | } |
1861 | return 1; |
1862 | |
1863 | case POP_VALUE: |
1864 | if (preserve_tos) { |
1865 | ADDOP(c, ROT_TWO); |
1866 | } |
1867 | ADDOP(c, POP_TOP); |
1868 | return 1; |
1869 | } |
1870 | Py_UNREACHABLE(); |
1871 | } |
1872 | |
1873 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
1874 | static int |
1875 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
1876 | if (c->u->u_nfblocks == 0) { |
1877 | return 1; |
1878 | } |
1879 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
1880 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
1881 | *loop = top; |
1882 | return 1; |
1883 | } |
1884 | struct fblockinfo copy = *top; |
1885 | c->u->u_nfblocks--; |
1886 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
1887 | return 0; |
1888 | } |
1889 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
1890 | return 0; |
1891 | } |
1892 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
1893 | c->u->u_nfblocks++; |
1894 | return 1; |
1895 | } |
1896 | |
1897 | /* Compile a sequence of statements, checking for a docstring |
1898 | and for annotations. */ |
1899 | |
1900 | static int |
1901 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
1902 | { |
1903 | int i = 0; |
1904 | stmt_ty st; |
1905 | PyObject *docstring; |
1906 | |
1907 | /* Set current line number to the line number of first statement. |
1908 | This way line number for SETUP_ANNOTATIONS will always |
1909 | coincide with the line number of first "real" statement in module. |
1910 | If body is empty, then lineno will be set later in assemble. */ |
1911 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) { |
1912 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
1913 | SET_LOC(c, st); |
1914 | } |
1915 | /* Every annotated class and module should have __annotations__. */ |
1916 | if (find_ann(stmts)) { |
1917 | ADDOP(c, SETUP_ANNOTATIONS); |
1918 | } |
1919 | if (!asdl_seq_LEN(stmts)) |
1920 | return 1; |
1921 | /* if not -OO mode, set docstring */ |
1922 | if (c->c_optimize < 2) { |
1923 | docstring = _PyAST_GetDocString(stmts); |
1924 | if (docstring) { |
1925 | i = 1; |
1926 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
1927 | assert(st->kind == Expr_kind); |
1928 | VISIT(c, expr, st->v.Expr.value); |
1929 | if (!compiler_nameop(c, __doc__, Store)) |
1930 | return 0; |
1931 | } |
1932 | } |
1933 | for (; i < asdl_seq_LEN(stmts); i++) |
1934 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
1935 | return 1; |
1936 | } |
1937 | |
1938 | static PyCodeObject * |
1939 | compiler_mod(struct compiler *c, mod_ty mod) |
1940 | { |
1941 | PyCodeObject *co; |
1942 | int addNone = 1; |
1943 | static PyObject *module; |
1944 | if (!module) { |
1945 | module = PyUnicode_InternFromString("<module>" ); |
1946 | if (!module) |
1947 | return NULL; |
1948 | } |
1949 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
1950 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
1951 | return NULL; |
1952 | switch (mod->kind) { |
1953 | case Module_kind: |
1954 | if (!compiler_body(c, mod->v.Module.body)) { |
1955 | compiler_exit_scope(c); |
1956 | return 0; |
1957 | } |
1958 | break; |
1959 | case Interactive_kind: |
1960 | if (find_ann(mod->v.Interactive.body)) { |
1961 | ADDOP(c, SETUP_ANNOTATIONS); |
1962 | } |
1963 | c->c_interactive = 1; |
1964 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
1965 | break; |
1966 | case Expression_kind: |
1967 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
1968 | addNone = 0; |
1969 | break; |
1970 | default: |
1971 | PyErr_Format(PyExc_SystemError, |
1972 | "module kind %d should not be possible" , |
1973 | mod->kind); |
1974 | return 0; |
1975 | } |
1976 | co = assemble(c, addNone); |
1977 | compiler_exit_scope(c); |
1978 | return co; |
1979 | } |
1980 | |
1981 | /* The test for LOCAL must come before the test for FREE in order to |
1982 | handle classes where name is both local and free. The local var is |
1983 | a method and the free var is a free var referenced within a method. |
1984 | */ |
1985 | |
1986 | static int |
1987 | get_ref_type(struct compiler *c, PyObject *name) |
1988 | { |
1989 | int scope; |
1990 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
1991 | _PyUnicode_EqualToASCIIString(name, "__class__" )) |
1992 | return CELL; |
1993 | scope = _PyST_GetScope(c->u->u_ste, name); |
1994 | if (scope == 0) { |
1995 | PyErr_Format(PyExc_SystemError, |
1996 | "_PyST_GetScope(name=%R) failed: " |
1997 | "unknown scope in unit %S (%R); " |
1998 | "symbols: %R; locals: %R; globals: %R" , |
1999 | name, |
2000 | c->u->u_name, c->u->u_ste->ste_id, |
2001 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
2002 | return -1; |
2003 | } |
2004 | return scope; |
2005 | } |
2006 | |
2007 | static int |
2008 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
2009 | { |
2010 | PyObject *v; |
2011 | v = PyDict_GetItemWithError(dict, name); |
2012 | if (v == NULL) |
2013 | return -1; |
2014 | return PyLong_AS_LONG(v); |
2015 | } |
2016 | |
2017 | static int |
2018 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
2019 | PyObject *qualname) |
2020 | { |
2021 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
2022 | if (qualname == NULL) |
2023 | qualname = co->co_name; |
2024 | |
2025 | if (free) { |
2026 | for (i = 0; i < free; ++i) { |
2027 | /* Bypass com_addop_varname because it will generate |
2028 | LOAD_DEREF but LOAD_CLOSURE is needed. |
2029 | */ |
2030 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
2031 | |
2032 | /* Special case: If a class contains a method with a |
2033 | free variable that has the same name as a method, |
2034 | the name will be considered free *and* local in the |
2035 | class. It should be handled by the closure, as |
2036 | well as by the normal name lookup logic. |
2037 | */ |
2038 | int reftype = get_ref_type(c, name); |
2039 | if (reftype == -1) { |
2040 | return 0; |
2041 | } |
2042 | int arg; |
2043 | if (reftype == CELL) { |
2044 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
2045 | } |
2046 | else { |
2047 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
2048 | } |
2049 | if (arg == -1) { |
2050 | PyErr_Format(PyExc_SystemError, |
2051 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
2052 | "freevars of code %S: %R" , |
2053 | name, |
2054 | reftype, |
2055 | c->u->u_name, |
2056 | co->co_name, |
2057 | co->co_freevars); |
2058 | return 0; |
2059 | } |
2060 | ADDOP_I(c, LOAD_CLOSURE, arg); |
2061 | } |
2062 | flags |= 0x08; |
2063 | ADDOP_I(c, BUILD_TUPLE, free); |
2064 | } |
2065 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
2066 | ADDOP_LOAD_CONST(c, qualname); |
2067 | ADDOP_I(c, MAKE_FUNCTION, flags); |
2068 | return 1; |
2069 | } |
2070 | |
2071 | static int |
2072 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
2073 | { |
2074 | int i; |
2075 | |
2076 | if (!decos) |
2077 | return 1; |
2078 | |
2079 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
2080 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
2081 | } |
2082 | return 1; |
2083 | } |
2084 | |
2085 | static int |
2086 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
2087 | asdl_expr_seq *kw_defaults) |
2088 | { |
2089 | /* Push a dict of keyword-only default values. |
2090 | |
2091 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
2092 | */ |
2093 | int i; |
2094 | PyObject *keys = NULL; |
2095 | |
2096 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
2097 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
2098 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
2099 | if (default_) { |
2100 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
2101 | if (!mangled) { |
2102 | goto error; |
2103 | } |
2104 | if (keys == NULL) { |
2105 | keys = PyList_New(1); |
2106 | if (keys == NULL) { |
2107 | Py_DECREF(mangled); |
2108 | return 0; |
2109 | } |
2110 | PyList_SET_ITEM(keys, 0, mangled); |
2111 | } |
2112 | else { |
2113 | int res = PyList_Append(keys, mangled); |
2114 | Py_DECREF(mangled); |
2115 | if (res == -1) { |
2116 | goto error; |
2117 | } |
2118 | } |
2119 | if (!compiler_visit_expr(c, default_)) { |
2120 | goto error; |
2121 | } |
2122 | } |
2123 | } |
2124 | if (keys != NULL) { |
2125 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
2126 | PyObject *keys_tuple = PyList_AsTuple(keys); |
2127 | Py_DECREF(keys); |
2128 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
2129 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
2130 | assert(default_count > 0); |
2131 | return 1; |
2132 | } |
2133 | else { |
2134 | return -1; |
2135 | } |
2136 | |
2137 | error: |
2138 | Py_XDECREF(keys); |
2139 | return 0; |
2140 | } |
2141 | |
2142 | static int |
2143 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
2144 | { |
2145 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
2146 | return 1; |
2147 | } |
2148 | |
2149 | static int |
2150 | compiler_visit_argannotation(struct compiler *c, identifier id, |
2151 | expr_ty annotation, Py_ssize_t *annotations_len) |
2152 | { |
2153 | if (!annotation) { |
2154 | return 1; |
2155 | } |
2156 | |
2157 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
2158 | if (!mangled) { |
2159 | return 0; |
2160 | } |
2161 | ADDOP_LOAD_CONST(c, mangled); |
2162 | Py_DECREF(mangled); |
2163 | |
2164 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
2165 | VISIT(c, annexpr, annotation) |
2166 | } |
2167 | else { |
2168 | VISIT(c, expr, annotation); |
2169 | } |
2170 | *annotations_len += 2; |
2171 | return 1; |
2172 | } |
2173 | |
2174 | static int |
2175 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
2176 | Py_ssize_t *annotations_len) |
2177 | { |
2178 | int i; |
2179 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
2180 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
2181 | if (!compiler_visit_argannotation( |
2182 | c, |
2183 | arg->arg, |
2184 | arg->annotation, |
2185 | annotations_len)) |
2186 | return 0; |
2187 | } |
2188 | return 1; |
2189 | } |
2190 | |
2191 | static int |
2192 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
2193 | expr_ty returns) |
2194 | { |
2195 | /* Push arg annotation names and values. |
2196 | The expressions are evaluated out-of-order wrt the source code. |
2197 | |
2198 | Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed. |
2199 | */ |
2200 | static identifier return_str; |
2201 | Py_ssize_t annotations_len = 0; |
2202 | |
2203 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
2204 | return 0; |
2205 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
2206 | return 0; |
2207 | if (args->vararg && args->vararg->annotation && |
2208 | !compiler_visit_argannotation(c, args->vararg->arg, |
2209 | args->vararg->annotation, &annotations_len)) |
2210 | return 0; |
2211 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
2212 | return 0; |
2213 | if (args->kwarg && args->kwarg->annotation && |
2214 | !compiler_visit_argannotation(c, args->kwarg->arg, |
2215 | args->kwarg->annotation, &annotations_len)) |
2216 | return 0; |
2217 | |
2218 | if (!return_str) { |
2219 | return_str = PyUnicode_InternFromString("return" ); |
2220 | if (!return_str) |
2221 | return 0; |
2222 | } |
2223 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
2224 | return 0; |
2225 | } |
2226 | |
2227 | if (annotations_len) { |
2228 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
2229 | return 1; |
2230 | } |
2231 | |
2232 | return -1; |
2233 | } |
2234 | |
2235 | static int |
2236 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
2237 | { |
2238 | VISIT_SEQ(c, expr, args->defaults); |
2239 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
2240 | return 1; |
2241 | } |
2242 | |
2243 | static Py_ssize_t |
2244 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
2245 | { |
2246 | Py_ssize_t funcflags = 0; |
2247 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
2248 | if (!compiler_visit_defaults(c, args)) |
2249 | return -1; |
2250 | funcflags |= 0x01; |
2251 | } |
2252 | if (args->kwonlyargs) { |
2253 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
2254 | args->kw_defaults); |
2255 | if (res == 0) { |
2256 | return -1; |
2257 | } |
2258 | else if (res > 0) { |
2259 | funcflags |= 0x02; |
2260 | } |
2261 | } |
2262 | return funcflags; |
2263 | } |
2264 | |
2265 | static int |
2266 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
2267 | { |
2268 | |
2269 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__" )) { |
2270 | compiler_error(c, "cannot assign to __debug__" ); |
2271 | return 1; |
2272 | } |
2273 | if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__" )) { |
2274 | compiler_error(c, "cannot delete __debug__" ); |
2275 | return 1; |
2276 | } |
2277 | return 0; |
2278 | } |
2279 | |
2280 | static int |
2281 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
2282 | { |
2283 | if (arg != NULL) { |
2284 | if (forbidden_name(c, arg->arg, Store)) |
2285 | return 0; |
2286 | } |
2287 | return 1; |
2288 | } |
2289 | |
2290 | static int |
2291 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
2292 | { |
2293 | if (args != NULL) { |
2294 | for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) { |
2295 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
2296 | return 0; |
2297 | } |
2298 | } |
2299 | return 1; |
2300 | } |
2301 | |
2302 | static int |
2303 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
2304 | { |
2305 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
2306 | return 0; |
2307 | if (!compiler_check_debug_args_seq(c, args->args)) |
2308 | return 0; |
2309 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
2310 | return 0; |
2311 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
2312 | return 0; |
2313 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
2314 | return 0; |
2315 | return 1; |
2316 | } |
2317 | |
2318 | static int |
2319 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
2320 | { |
2321 | PyCodeObject *co; |
2322 | PyObject *qualname, *docstring = NULL; |
2323 | arguments_ty args; |
2324 | expr_ty returns; |
2325 | identifier name; |
2326 | asdl_expr_seq* decos; |
2327 | asdl_stmt_seq *body; |
2328 | Py_ssize_t i, funcflags; |
2329 | int annotations; |
2330 | int scope_type; |
2331 | int firstlineno; |
2332 | |
2333 | if (is_async) { |
2334 | assert(s->kind == AsyncFunctionDef_kind); |
2335 | |
2336 | args = s->v.AsyncFunctionDef.args; |
2337 | returns = s->v.AsyncFunctionDef.returns; |
2338 | decos = s->v.AsyncFunctionDef.decorator_list; |
2339 | name = s->v.AsyncFunctionDef.name; |
2340 | body = s->v.AsyncFunctionDef.body; |
2341 | |
2342 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
2343 | } else { |
2344 | assert(s->kind == FunctionDef_kind); |
2345 | |
2346 | args = s->v.FunctionDef.args; |
2347 | returns = s->v.FunctionDef.returns; |
2348 | decos = s->v.FunctionDef.decorator_list; |
2349 | name = s->v.FunctionDef.name; |
2350 | body = s->v.FunctionDef.body; |
2351 | |
2352 | scope_type = COMPILER_SCOPE_FUNCTION; |
2353 | } |
2354 | |
2355 | if (!compiler_check_debug_args(c, args)) |
2356 | return 0; |
2357 | |
2358 | if (!compiler_decorators(c, decos)) |
2359 | return 0; |
2360 | |
2361 | firstlineno = s->lineno; |
2362 | if (asdl_seq_LEN(decos)) { |
2363 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
2364 | } |
2365 | |
2366 | funcflags = compiler_default_arguments(c, args); |
2367 | if (funcflags == -1) { |
2368 | return 0; |
2369 | } |
2370 | |
2371 | annotations = compiler_visit_annotations(c, args, returns); |
2372 | if (annotations == 0) { |
2373 | return 0; |
2374 | } |
2375 | else if (annotations > 0) { |
2376 | funcflags |= 0x04; |
2377 | } |
2378 | |
2379 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
2380 | return 0; |
2381 | } |
2382 | |
2383 | /* if not -OO mode, add docstring */ |
2384 | if (c->c_optimize < 2) { |
2385 | docstring = _PyAST_GetDocString(body); |
2386 | } |
2387 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
2388 | compiler_exit_scope(c); |
2389 | return 0; |
2390 | } |
2391 | |
2392 | c->u->u_argcount = asdl_seq_LEN(args->args); |
2393 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
2394 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
2395 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
2396 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
2397 | } |
2398 | co = assemble(c, 1); |
2399 | qualname = c->u->u_qualname; |
2400 | Py_INCREF(qualname); |
2401 | compiler_exit_scope(c); |
2402 | if (co == NULL) { |
2403 | Py_XDECREF(qualname); |
2404 | Py_XDECREF(co); |
2405 | return 0; |
2406 | } |
2407 | |
2408 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
2409 | Py_DECREF(qualname); |
2410 | Py_DECREF(co); |
2411 | return 0; |
2412 | } |
2413 | Py_DECREF(qualname); |
2414 | Py_DECREF(co); |
2415 | |
2416 | /* decorators */ |
2417 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
2418 | ADDOP_I(c, CALL_FUNCTION, 1); |
2419 | } |
2420 | |
2421 | return compiler_nameop(c, name, Store); |
2422 | } |
2423 | |
2424 | static int |
2425 | compiler_class(struct compiler *c, stmt_ty s) |
2426 | { |
2427 | PyCodeObject *co; |
2428 | PyObject *str; |
2429 | int i, firstlineno; |
2430 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
2431 | |
2432 | if (!compiler_decorators(c, decos)) |
2433 | return 0; |
2434 | |
2435 | firstlineno = s->lineno; |
2436 | if (asdl_seq_LEN(decos)) { |
2437 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
2438 | } |
2439 | |
2440 | /* ultimately generate code for: |
2441 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
2442 | where: |
2443 | <func> is a zero arg function/closure created from the class body. |
2444 | It mutates its locals to build the class namespace. |
2445 | <name> is the class name |
2446 | <bases> is the positional arguments and *varargs argument |
2447 | <keywords> is the keyword arguments and **kwds argument |
2448 | This borrows from compiler_call. |
2449 | */ |
2450 | |
2451 | /* 1. compile the class body into a code object */ |
2452 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
2453 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
2454 | return 0; |
2455 | /* this block represents what we do in the new scope */ |
2456 | { |
2457 | /* use the class name for name mangling */ |
2458 | Py_INCREF(s->v.ClassDef.name); |
2459 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
2460 | /* load (global) __name__ ... */ |
2461 | str = PyUnicode_InternFromString("__name__" ); |
2462 | if (!str || !compiler_nameop(c, str, Load)) { |
2463 | Py_XDECREF(str); |
2464 | compiler_exit_scope(c); |
2465 | return 0; |
2466 | } |
2467 | Py_DECREF(str); |
2468 | /* ... and store it as __module__ */ |
2469 | str = PyUnicode_InternFromString("__module__" ); |
2470 | if (!str || !compiler_nameop(c, str, Store)) { |
2471 | Py_XDECREF(str); |
2472 | compiler_exit_scope(c); |
2473 | return 0; |
2474 | } |
2475 | Py_DECREF(str); |
2476 | assert(c->u->u_qualname); |
2477 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
2478 | str = PyUnicode_InternFromString("__qualname__" ); |
2479 | if (!str || !compiler_nameop(c, str, Store)) { |
2480 | Py_XDECREF(str); |
2481 | compiler_exit_scope(c); |
2482 | return 0; |
2483 | } |
2484 | Py_DECREF(str); |
2485 | /* compile the body proper */ |
2486 | if (!compiler_body(c, s->v.ClassDef.body)) { |
2487 | compiler_exit_scope(c); |
2488 | return 0; |
2489 | } |
2490 | /* The following code is artificial */ |
2491 | c->u->u_lineno = -1; |
2492 | /* Return __classcell__ if it is referenced, otherwise return None */ |
2493 | if (c->u->u_ste->ste_needs_class_closure) { |
2494 | /* Store __classcell__ into class namespace & return it */ |
2495 | str = PyUnicode_InternFromString("__class__" ); |
2496 | if (str == NULL) { |
2497 | compiler_exit_scope(c); |
2498 | return 0; |
2499 | } |
2500 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
2501 | Py_DECREF(str); |
2502 | if (i < 0) { |
2503 | compiler_exit_scope(c); |
2504 | return 0; |
2505 | } |
2506 | assert(i == 0); |
2507 | |
2508 | ADDOP_I(c, LOAD_CLOSURE, i); |
2509 | ADDOP(c, DUP_TOP); |
2510 | str = PyUnicode_InternFromString("__classcell__" ); |
2511 | if (!str || !compiler_nameop(c, str, Store)) { |
2512 | Py_XDECREF(str); |
2513 | compiler_exit_scope(c); |
2514 | return 0; |
2515 | } |
2516 | Py_DECREF(str); |
2517 | } |
2518 | else { |
2519 | /* No methods referenced __class__, so just return None */ |
2520 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
2521 | ADDOP_LOAD_CONST(c, Py_None); |
2522 | } |
2523 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
2524 | /* create the code object */ |
2525 | co = assemble(c, 1); |
2526 | } |
2527 | /* leave the new scope */ |
2528 | compiler_exit_scope(c); |
2529 | if (co == NULL) |
2530 | return 0; |
2531 | |
2532 | /* 2. load the 'build_class' function */ |
2533 | ADDOP(c, LOAD_BUILD_CLASS); |
2534 | |
2535 | /* 3. load a function (or closure) made from the code object */ |
2536 | if (!compiler_make_closure(c, co, 0, NULL)) { |
2537 | Py_DECREF(co); |
2538 | return 0; |
2539 | } |
2540 | Py_DECREF(co); |
2541 | |
2542 | /* 4. load class name */ |
2543 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
2544 | |
2545 | /* 5. generate the rest of the code for the call */ |
2546 | if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords)) |
2547 | return 0; |
2548 | |
2549 | /* 6. apply decorators */ |
2550 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
2551 | ADDOP_I(c, CALL_FUNCTION, 1); |
2552 | } |
2553 | |
2554 | /* 7. store into <name> */ |
2555 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
2556 | return 0; |
2557 | return 1; |
2558 | } |
2559 | |
2560 | /* Return 0 if the expression is a constant value except named singletons. |
2561 | Return 1 otherwise. */ |
2562 | static int |
2563 | check_is_arg(expr_ty e) |
2564 | { |
2565 | if (e->kind != Constant_kind) { |
2566 | return 1; |
2567 | } |
2568 | PyObject *value = e->v.Constant.value; |
2569 | return (value == Py_None |
2570 | || value == Py_False |
2571 | || value == Py_True |
2572 | || value == Py_Ellipsis); |
2573 | } |
2574 | |
2575 | /* Check operands of identity chacks ("is" and "is not"). |
2576 | Emit a warning if any operand is a constant except named singletons. |
2577 | Return 0 on error. |
2578 | */ |
2579 | static int |
2580 | check_compare(struct compiler *c, expr_ty e) |
2581 | { |
2582 | Py_ssize_t i, n; |
2583 | int left = check_is_arg(e->v.Compare.left); |
2584 | n = asdl_seq_LEN(e->v.Compare.ops); |
2585 | for (i = 0; i < n; i++) { |
2586 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
2587 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
2588 | if (op == Is || op == IsNot) { |
2589 | if (!right || !left) { |
2590 | const char *msg = (op == Is) |
2591 | ? "\"is\" with a literal. Did you mean \"==\"?" |
2592 | : "\"is not\" with a literal. Did you mean \"!=\"?" ; |
2593 | return compiler_warn(c, msg); |
2594 | } |
2595 | } |
2596 | left = right; |
2597 | } |
2598 | return 1; |
2599 | } |
2600 | |
2601 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
2602 | { |
2603 | int cmp; |
2604 | switch (op) { |
2605 | case Eq: |
2606 | cmp = Py_EQ; |
2607 | break; |
2608 | case NotEq: |
2609 | cmp = Py_NE; |
2610 | break; |
2611 | case Lt: |
2612 | cmp = Py_LT; |
2613 | break; |
2614 | case LtE: |
2615 | cmp = Py_LE; |
2616 | break; |
2617 | case Gt: |
2618 | cmp = Py_GT; |
2619 | break; |
2620 | case GtE: |
2621 | cmp = Py_GE; |
2622 | break; |
2623 | case Is: |
2624 | ADDOP_I(c, IS_OP, 0); |
2625 | return 1; |
2626 | case IsNot: |
2627 | ADDOP_I(c, IS_OP, 1); |
2628 | return 1; |
2629 | case In: |
2630 | ADDOP_I(c, CONTAINS_OP, 0); |
2631 | return 1; |
2632 | case NotIn: |
2633 | ADDOP_I(c, CONTAINS_OP, 1); |
2634 | return 1; |
2635 | default: |
2636 | Py_UNREACHABLE(); |
2637 | } |
2638 | ADDOP_I(c, COMPARE_OP, cmp); |
2639 | return 1; |
2640 | } |
2641 | |
2642 | |
2643 | |
2644 | static int |
2645 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
2646 | { |
2647 | switch (e->kind) { |
2648 | case UnaryOp_kind: |
2649 | if (e->v.UnaryOp.op == Not) |
2650 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
2651 | /* fallback to general implementation */ |
2652 | break; |
2653 | case BoolOp_kind: { |
2654 | asdl_expr_seq *s = e->v.BoolOp.values; |
2655 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
2656 | assert(n >= 0); |
2657 | int cond2 = e->v.BoolOp.op == Or; |
2658 | basicblock *next2 = next; |
2659 | if (!cond2 != !cond) { |
2660 | next2 = compiler_new_block(c); |
2661 | if (next2 == NULL) |
2662 | return 0; |
2663 | } |
2664 | for (i = 0; i < n; ++i) { |
2665 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
2666 | return 0; |
2667 | } |
2668 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
2669 | return 0; |
2670 | if (next2 != next) |
2671 | compiler_use_next_block(c, next2); |
2672 | return 1; |
2673 | } |
2674 | case IfExp_kind: { |
2675 | basicblock *end, *next2; |
2676 | end = compiler_new_block(c); |
2677 | if (end == NULL) |
2678 | return 0; |
2679 | next2 = compiler_new_block(c); |
2680 | if (next2 == NULL) |
2681 | return 0; |
2682 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
2683 | return 0; |
2684 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
2685 | return 0; |
2686 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
2687 | compiler_use_next_block(c, next2); |
2688 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
2689 | return 0; |
2690 | compiler_use_next_block(c, end); |
2691 | return 1; |
2692 | } |
2693 | case Compare_kind: { |
2694 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
2695 | if (n > 0) { |
2696 | if (!check_compare(c, e)) { |
2697 | return 0; |
2698 | } |
2699 | basicblock *cleanup = compiler_new_block(c); |
2700 | if (cleanup == NULL) |
2701 | return 0; |
2702 | VISIT(c, expr, e->v.Compare.left); |
2703 | for (i = 0; i < n; i++) { |
2704 | VISIT(c, expr, |
2705 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
2706 | ADDOP(c, DUP_TOP); |
2707 | ADDOP(c, ROT_THREE); |
2708 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
2709 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
2710 | NEXT_BLOCK(c); |
2711 | } |
2712 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
2713 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
2714 | ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
2715 | NEXT_BLOCK(c); |
2716 | basicblock *end = compiler_new_block(c); |
2717 | if (end == NULL) |
2718 | return 0; |
2719 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
2720 | compiler_use_next_block(c, cleanup); |
2721 | ADDOP(c, POP_TOP); |
2722 | if (!cond) { |
2723 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
2724 | } |
2725 | compiler_use_next_block(c, end); |
2726 | return 1; |
2727 | } |
2728 | /* fallback to general implementation */ |
2729 | break; |
2730 | } |
2731 | default: |
2732 | /* fallback to general implementation */ |
2733 | break; |
2734 | } |
2735 | |
2736 | /* general implementation */ |
2737 | VISIT(c, expr, e); |
2738 | ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
2739 | NEXT_BLOCK(c); |
2740 | return 1; |
2741 | } |
2742 | |
2743 | static int |
2744 | compiler_ifexp(struct compiler *c, expr_ty e) |
2745 | { |
2746 | basicblock *end, *next; |
2747 | |
2748 | assert(e->kind == IfExp_kind); |
2749 | end = compiler_new_block(c); |
2750 | if (end == NULL) |
2751 | return 0; |
2752 | next = compiler_new_block(c); |
2753 | if (next == NULL) |
2754 | return 0; |
2755 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
2756 | return 0; |
2757 | VISIT(c, expr, e->v.IfExp.body); |
2758 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
2759 | compiler_use_next_block(c, next); |
2760 | VISIT(c, expr, e->v.IfExp.orelse); |
2761 | compiler_use_next_block(c, end); |
2762 | return 1; |
2763 | } |
2764 | |
2765 | static int |
2766 | compiler_lambda(struct compiler *c, expr_ty e) |
2767 | { |
2768 | PyCodeObject *co; |
2769 | PyObject *qualname; |
2770 | static identifier name; |
2771 | Py_ssize_t funcflags; |
2772 | arguments_ty args = e->v.Lambda.args; |
2773 | assert(e->kind == Lambda_kind); |
2774 | |
2775 | if (!compiler_check_debug_args(c, args)) |
2776 | return 0; |
2777 | |
2778 | if (!name) { |
2779 | name = PyUnicode_InternFromString("<lambda>" ); |
2780 | if (!name) |
2781 | return 0; |
2782 | } |
2783 | |
2784 | funcflags = compiler_default_arguments(c, args); |
2785 | if (funcflags == -1) { |
2786 | return 0; |
2787 | } |
2788 | |
2789 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
2790 | (void *)e, e->lineno)) |
2791 | return 0; |
2792 | |
2793 | /* Make None the first constant, so the lambda can't have a |
2794 | docstring. */ |
2795 | if (compiler_add_const(c, Py_None) < 0) |
2796 | return 0; |
2797 | |
2798 | c->u->u_argcount = asdl_seq_LEN(args->args); |
2799 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
2800 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
2801 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
2802 | if (c->u->u_ste->ste_generator) { |
2803 | co = assemble(c, 0); |
2804 | } |
2805 | else { |
2806 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
2807 | co = assemble(c, 1); |
2808 | } |
2809 | qualname = c->u->u_qualname; |
2810 | Py_INCREF(qualname); |
2811 | compiler_exit_scope(c); |
2812 | if (co == NULL) { |
2813 | Py_DECREF(qualname); |
2814 | return 0; |
2815 | } |
2816 | |
2817 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
2818 | Py_DECREF(qualname); |
2819 | Py_DECREF(co); |
2820 | return 0; |
2821 | } |
2822 | Py_DECREF(qualname); |
2823 | Py_DECREF(co); |
2824 | |
2825 | return 1; |
2826 | } |
2827 | |
2828 | static int |
2829 | compiler_if(struct compiler *c, stmt_ty s) |
2830 | { |
2831 | basicblock *end, *next; |
2832 | assert(s->kind == If_kind); |
2833 | end = compiler_new_block(c); |
2834 | if (end == NULL) { |
2835 | return 0; |
2836 | } |
2837 | if (asdl_seq_LEN(s->v.If.orelse)) { |
2838 | next = compiler_new_block(c); |
2839 | if (next == NULL) { |
2840 | return 0; |
2841 | } |
2842 | } |
2843 | else { |
2844 | next = end; |
2845 | } |
2846 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
2847 | return 0; |
2848 | } |
2849 | VISIT_SEQ(c, stmt, s->v.If.body); |
2850 | if (asdl_seq_LEN(s->v.If.orelse)) { |
2851 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
2852 | compiler_use_next_block(c, next); |
2853 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
2854 | } |
2855 | compiler_use_next_block(c, end); |
2856 | return 1; |
2857 | } |
2858 | |
2859 | static int |
2860 | compiler_for(struct compiler *c, stmt_ty s) |
2861 | { |
2862 | basicblock *start, *body, *cleanup, *end; |
2863 | |
2864 | start = compiler_new_block(c); |
2865 | body = compiler_new_block(c); |
2866 | cleanup = compiler_new_block(c); |
2867 | end = compiler_new_block(c); |
2868 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
2869 | return 0; |
2870 | } |
2871 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
2872 | return 0; |
2873 | } |
2874 | VISIT(c, expr, s->v.For.iter); |
2875 | ADDOP(c, GET_ITER); |
2876 | compiler_use_next_block(c, start); |
2877 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
2878 | compiler_use_next_block(c, body); |
2879 | VISIT(c, expr, s->v.For.target); |
2880 | VISIT_SEQ(c, stmt, s->v.For.body); |
2881 | /* Mark jump as artificial */ |
2882 | c->u->u_lineno = -1; |
2883 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
2884 | compiler_use_next_block(c, cleanup); |
2885 | |
2886 | compiler_pop_fblock(c, FOR_LOOP, start); |
2887 | |
2888 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
2889 | compiler_use_next_block(c, end); |
2890 | return 1; |
2891 | } |
2892 | |
2893 | |
2894 | static int |
2895 | compiler_async_for(struct compiler *c, stmt_ty s) |
2896 | { |
2897 | basicblock *start, *except, *end; |
2898 | if (IS_TOP_LEVEL_AWAIT(c)){ |
2899 | c->u->u_ste->ste_coroutine = 1; |
2900 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
2901 | return compiler_error(c, "'async for' outside async function" ); |
2902 | } |
2903 | |
2904 | start = compiler_new_block(c); |
2905 | except = compiler_new_block(c); |
2906 | end = compiler_new_block(c); |
2907 | |
2908 | if (start == NULL || except == NULL || end == NULL) { |
2909 | return 0; |
2910 | } |
2911 | VISIT(c, expr, s->v.AsyncFor.iter); |
2912 | ADDOP(c, GET_AITER); |
2913 | |
2914 | compiler_use_next_block(c, start); |
2915 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
2916 | return 0; |
2917 | } |
2918 | /* SETUP_FINALLY to guard the __anext__ call */ |
2919 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
2920 | ADDOP(c, GET_ANEXT); |
2921 | ADDOP_LOAD_CONST(c, Py_None); |
2922 | ADDOP(c, YIELD_FROM); |
2923 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
2924 | |
2925 | /* Success block for __anext__ */ |
2926 | VISIT(c, expr, s->v.AsyncFor.target); |
2927 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
2928 | /* Mark jump as artificial */ |
2929 | c->u->u_lineno = -1; |
2930 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
2931 | |
2932 | compiler_pop_fblock(c, FOR_LOOP, start); |
2933 | |
2934 | /* Except block for __anext__ */ |
2935 | compiler_use_next_block(c, except); |
2936 | |
2937 | /* Use same line number as the iterator, |
2938 | * as the END_ASYNC_FOR succeeds the `for`, not the body. */ |
2939 | SET_LOC(c, s->v.AsyncFor.iter); |
2940 | ADDOP(c, END_ASYNC_FOR); |
2941 | |
2942 | /* `else` block */ |
2943 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
2944 | |
2945 | compiler_use_next_block(c, end); |
2946 | |
2947 | return 1; |
2948 | } |
2949 | |
2950 | static int |
2951 | compiler_while(struct compiler *c, stmt_ty s) |
2952 | { |
2953 | basicblock *loop, *body, *end, *anchor = NULL; |
2954 | loop = compiler_new_block(c); |
2955 | body = compiler_new_block(c); |
2956 | anchor = compiler_new_block(c); |
2957 | end = compiler_new_block(c); |
2958 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
2959 | return 0; |
2960 | } |
2961 | compiler_use_next_block(c, loop); |
2962 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
2963 | return 0; |
2964 | } |
2965 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
2966 | return 0; |
2967 | } |
2968 | |
2969 | compiler_use_next_block(c, body); |
2970 | VISIT_SEQ(c, stmt, s->v.While.body); |
2971 | SET_LOC(c, s); |
2972 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
2973 | return 0; |
2974 | } |
2975 | |
2976 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
2977 | |
2978 | compiler_use_next_block(c, anchor); |
2979 | if (s->v.While.orelse) { |
2980 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
2981 | } |
2982 | compiler_use_next_block(c, end); |
2983 | |
2984 | return 1; |
2985 | } |
2986 | |
2987 | static int |
2988 | compiler_return(struct compiler *c, stmt_ty s) |
2989 | { |
2990 | int preserve_tos = ((s->v.Return.value != NULL) && |
2991 | (s->v.Return.value->kind != Constant_kind)); |
2992 | if (c->u->u_ste->ste_type != FunctionBlock) |
2993 | return compiler_error(c, "'return' outside function" ); |
2994 | if (s->v.Return.value != NULL && |
2995 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
2996 | { |
2997 | return compiler_error( |
2998 | c, "'return' with value in async generator" ); |
2999 | } |
3000 | if (preserve_tos) { |
3001 | VISIT(c, expr, s->v.Return.value); |
3002 | } else { |
3003 | /* Emit instruction with line number for return value */ |
3004 | if (s->v.Return.value != NULL) { |
3005 | SET_LOC(c, s->v.Return.value); |
3006 | ADDOP(c, NOP); |
3007 | } |
3008 | } |
3009 | if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) { |
3010 | SET_LOC(c, s); |
3011 | ADDOP(c, NOP); |
3012 | } |
3013 | |
3014 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
3015 | return 0; |
3016 | if (s->v.Return.value == NULL) { |
3017 | ADDOP_LOAD_CONST(c, Py_None); |
3018 | } |
3019 | else if (!preserve_tos) { |
3020 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
3021 | } |
3022 | ADDOP(c, RETURN_VALUE); |
3023 | NEXT_BLOCK(c); |
3024 | |
3025 | return 1; |
3026 | } |
3027 | |
3028 | static int |
3029 | compiler_break(struct compiler *c) |
3030 | { |
3031 | struct fblockinfo *loop = NULL; |
3032 | /* Emit instruction with line number */ |
3033 | ADDOP(c, NOP); |
3034 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
3035 | return 0; |
3036 | } |
3037 | if (loop == NULL) { |
3038 | return compiler_error(c, "'break' outside loop" ); |
3039 | } |
3040 | if (!compiler_unwind_fblock(c, loop, 0)) { |
3041 | return 0; |
3042 | } |
3043 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
3044 | NEXT_BLOCK(c); |
3045 | return 1; |
3046 | } |
3047 | |
3048 | static int |
3049 | compiler_continue(struct compiler *c) |
3050 | { |
3051 | struct fblockinfo *loop = NULL; |
3052 | /* Emit instruction with line number */ |
3053 | ADDOP(c, NOP); |
3054 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
3055 | return 0; |
3056 | } |
3057 | if (loop == NULL) { |
3058 | return compiler_error(c, "'continue' not properly in loop" ); |
3059 | } |
3060 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
3061 | NEXT_BLOCK(c) |
3062 | return 1; |
3063 | } |
3064 | |
3065 | |
3066 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
3067 | |
3068 | SETUP_FINALLY L |
3069 | <code for body> |
3070 | POP_BLOCK |
3071 | <code for finalbody> |
3072 | JUMP E |
3073 | L: |
3074 | <code for finalbody> |
3075 | E: |
3076 | |
3077 | The special instructions use the block stack. Each block |
3078 | stack entry contains the instruction that created it (here |
3079 | SETUP_FINALLY), the level of the value stack at the time the |
3080 | block stack entry was created, and a label (here L). |
3081 | |
3082 | SETUP_FINALLY: |
3083 | Pushes the current value stack level and the label |
3084 | onto the block stack. |
3085 | POP_BLOCK: |
3086 | Pops en entry from the block stack. |
3087 | |
3088 | The block stack is unwound when an exception is raised: |
3089 | when a SETUP_FINALLY entry is found, the raised and the caught |
3090 | exceptions are pushed onto the value stack (and the exception |
3091 | condition is cleared), and the interpreter jumps to the label |
3092 | gotten from the block stack. |
3093 | */ |
3094 | |
3095 | static int |
3096 | compiler_try_finally(struct compiler *c, stmt_ty s) |
3097 | { |
3098 | basicblock *body, *end, *exit; |
3099 | |
3100 | body = compiler_new_block(c); |
3101 | end = compiler_new_block(c); |
3102 | exit = compiler_new_block(c); |
3103 | if (body == NULL || end == NULL || exit == NULL) |
3104 | return 0; |
3105 | |
3106 | /* `try` block */ |
3107 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
3108 | compiler_use_next_block(c, body); |
3109 | if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody)) |
3110 | return 0; |
3111 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
3112 | if (!compiler_try_except(c, s)) |
3113 | return 0; |
3114 | } |
3115 | else { |
3116 | VISIT_SEQ(c, stmt, s->v.Try.body); |
3117 | } |
3118 | ADDOP_NOLINE(c, POP_BLOCK); |
3119 | compiler_pop_fblock(c, FINALLY_TRY, body); |
3120 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
3121 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
3122 | /* `finally` block */ |
3123 | compiler_use_next_block(c, end); |
3124 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
3125 | return 0; |
3126 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
3127 | compiler_pop_fblock(c, FINALLY_END, end); |
3128 | ADDOP_I(c, RERAISE, 0); |
3129 | compiler_use_next_block(c, exit); |
3130 | return 1; |
3131 | } |
3132 | |
3133 | /* |
3134 | Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...": |
3135 | (The contents of the value stack is shown in [], with the top |
3136 | at the right; 'tb' is trace-back info, 'val' the exception's |
3137 | associated value, and 'exc' the exception.) |
3138 | |
3139 | Value stack Label Instruction Argument |
3140 | [] SETUP_FINALLY L1 |
3141 | [] <code for S> |
3142 | [] POP_BLOCK |
3143 | [] JUMP_FORWARD L0 |
3144 | |
3145 | [tb, val, exc] L1: DUP ) |
3146 | [tb, val, exc, exc] <evaluate E1> ) |
3147 | [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1 |
3148 | [tb, val, exc] POP |
3149 | [tb, val] <assign to V1> (or POP if no V1) |
3150 | [tb] POP |
3151 | [] <code for S1> |
3152 | JUMP_FORWARD L0 |
3153 | |
3154 | [tb, val, exc] L2: DUP |
3155 | .............................etc....................... |
3156 | |
3157 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
3158 | |
3159 | [] L0: <next statement> |
3160 | |
3161 | Of course, parts are not generated if Vi or Ei is not present. |
3162 | */ |
3163 | static int |
3164 | compiler_try_except(struct compiler *c, stmt_ty s) |
3165 | { |
3166 | basicblock *body, *orelse, *except, *end; |
3167 | Py_ssize_t i, n; |
3168 | |
3169 | body = compiler_new_block(c); |
3170 | except = compiler_new_block(c); |
3171 | orelse = compiler_new_block(c); |
3172 | end = compiler_new_block(c); |
3173 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
3174 | return 0; |
3175 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
3176 | compiler_use_next_block(c, body); |
3177 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
3178 | return 0; |
3179 | VISIT_SEQ(c, stmt, s->v.Try.body); |
3180 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
3181 | ADDOP_NOLINE(c, POP_BLOCK); |
3182 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
3183 | n = asdl_seq_LEN(s->v.Try.handlers); |
3184 | compiler_use_next_block(c, except); |
3185 | /* Runtime will push a block here, so we need to account for that */ |
3186 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
3187 | return 0; |
3188 | for (i = 0; i < n; i++) { |
3189 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
3190 | s->v.Try.handlers, i); |
3191 | SET_LOC(c, handler); |
3192 | if (!handler->v.ExceptHandler.type && i < n-1) |
3193 | return compiler_error(c, "default 'except:' must be last" ); |
3194 | except = compiler_new_block(c); |
3195 | if (except == NULL) |
3196 | return 0; |
3197 | if (handler->v.ExceptHandler.type) { |
3198 | ADDOP(c, DUP_TOP); |
3199 | VISIT(c, expr, handler->v.ExceptHandler.type); |
3200 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
3201 | NEXT_BLOCK(c); |
3202 | } |
3203 | ADDOP(c, POP_TOP); |
3204 | if (handler->v.ExceptHandler.name) { |
3205 | basicblock *cleanup_end, *cleanup_body; |
3206 | |
3207 | cleanup_end = compiler_new_block(c); |
3208 | cleanup_body = compiler_new_block(c); |
3209 | if (cleanup_end == NULL || cleanup_body == NULL) { |
3210 | return 0; |
3211 | } |
3212 | |
3213 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
3214 | ADDOP(c, POP_TOP); |
3215 | |
3216 | /* |
3217 | try: |
3218 | # body |
3219 | except type as name: |
3220 | try: |
3221 | # body |
3222 | finally: |
3223 | name = None # in case body contains "del name" |
3224 | del name |
3225 | */ |
3226 | |
3227 | /* second try: */ |
3228 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
3229 | compiler_use_next_block(c, cleanup_body); |
3230 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name)) |
3231 | return 0; |
3232 | |
3233 | /* second # body */ |
3234 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
3235 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
3236 | /* name = None; del name; # Mark as artificial */ |
3237 | c->u->u_lineno = -1; |
3238 | ADDOP(c, POP_BLOCK); |
3239 | ADDOP(c, POP_EXCEPT); |
3240 | ADDOP_LOAD_CONST(c, Py_None); |
3241 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
3242 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
3243 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
3244 | |
3245 | /* except: */ |
3246 | compiler_use_next_block(c, cleanup_end); |
3247 | |
3248 | /* name = None; del name; # Mark as artificial */ |
3249 | c->u->u_lineno = -1; |
3250 | ADDOP_LOAD_CONST(c, Py_None); |
3251 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
3252 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
3253 | |
3254 | ADDOP_I(c, RERAISE, 1); |
3255 | } |
3256 | else { |
3257 | basicblock *cleanup_body; |
3258 | |
3259 | cleanup_body = compiler_new_block(c); |
3260 | if (!cleanup_body) |
3261 | return 0; |
3262 | |
3263 | ADDOP(c, POP_TOP); |
3264 | ADDOP(c, POP_TOP); |
3265 | compiler_use_next_block(c, cleanup_body); |
3266 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
3267 | return 0; |
3268 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
3269 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
3270 | c->u->u_lineno = -1; |
3271 | ADDOP(c, POP_EXCEPT); |
3272 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
3273 | } |
3274 | compiler_use_next_block(c, except); |
3275 | } |
3276 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
3277 | /* Mark as artificial */ |
3278 | c->u->u_lineno = -1; |
3279 | ADDOP_I(c, RERAISE, 0); |
3280 | compiler_use_next_block(c, orelse); |
3281 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
3282 | compiler_use_next_block(c, end); |
3283 | return 1; |
3284 | } |
3285 | |
3286 | static int |
3287 | compiler_try(struct compiler *c, stmt_ty s) { |
3288 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
3289 | return compiler_try_finally(c, s); |
3290 | else |
3291 | return compiler_try_except(c, s); |
3292 | } |
3293 | |
3294 | |
3295 | static int |
3296 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
3297 | { |
3298 | /* The IMPORT_NAME opcode was already generated. This function |
3299 | merely needs to bind the result to a name. |
3300 | |
3301 | If there is a dot in name, we need to split it and emit a |
3302 | IMPORT_FROM for each name. |
3303 | */ |
3304 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
3305 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
3306 | if (dot == -2) |
3307 | return 0; |
3308 | if (dot != -1) { |
3309 | /* Consume the base module name to get the first attribute */ |
3310 | while (1) { |
3311 | Py_ssize_t pos = dot + 1; |
3312 | PyObject *attr; |
3313 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
3314 | if (dot == -2) |
3315 | return 0; |
3316 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
3317 | if (!attr) |
3318 | return 0; |
3319 | ADDOP_N(c, IMPORT_FROM, attr, names); |
3320 | if (dot == -1) { |
3321 | break; |
3322 | } |
3323 | ADDOP(c, ROT_TWO); |
3324 | ADDOP(c, POP_TOP); |
3325 | } |
3326 | if (!compiler_nameop(c, asname, Store)) { |
3327 | return 0; |
3328 | } |
3329 | ADDOP(c, POP_TOP); |
3330 | return 1; |
3331 | } |
3332 | return compiler_nameop(c, asname, Store); |
3333 | } |
3334 | |
3335 | static int |
3336 | compiler_import(struct compiler *c, stmt_ty s) |
3337 | { |
3338 | /* The Import node stores a module name like a.b.c as a single |
3339 | string. This is convenient for all cases except |
3340 | import a.b.c as d |
3341 | where we need to parse that string to extract the individual |
3342 | module names. |
3343 | XXX Perhaps change the representation to make this case simpler? |
3344 | */ |
3345 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
3346 | |
3347 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
3348 | for (i = 0; i < n; i++) { |
3349 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
3350 | int r; |
3351 | |
3352 | ADDOP_LOAD_CONST(c, zero); |
3353 | ADDOP_LOAD_CONST(c, Py_None); |
3354 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
3355 | |
3356 | if (alias->asname) { |
3357 | r = compiler_import_as(c, alias->name, alias->asname); |
3358 | if (!r) |
3359 | return r; |
3360 | } |
3361 | else { |
3362 | identifier tmp = alias->name; |
3363 | Py_ssize_t dot = PyUnicode_FindChar( |
3364 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
3365 | if (dot != -1) { |
3366 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
3367 | if (tmp == NULL) |
3368 | return 0; |
3369 | } |
3370 | r = compiler_nameop(c, tmp, Store); |
3371 | if (dot != -1) { |
3372 | Py_DECREF(tmp); |
3373 | } |
3374 | if (!r) |
3375 | return r; |
3376 | } |
3377 | } |
3378 | return 1; |
3379 | } |
3380 | |
3381 | static int |
3382 | compiler_from_import(struct compiler *c, stmt_ty s) |
3383 | { |
3384 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
3385 | PyObject *names; |
3386 | static PyObject *empty_string; |
3387 | |
3388 | if (!empty_string) { |
3389 | empty_string = PyUnicode_FromString("" ); |
3390 | if (!empty_string) |
3391 | return 0; |
3392 | } |
3393 | |
3394 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
3395 | |
3396 | names = PyTuple_New(n); |
3397 | if (!names) |
3398 | return 0; |
3399 | |
3400 | /* build up the names */ |
3401 | for (i = 0; i < n; i++) { |
3402 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
3403 | Py_INCREF(alias->name); |
3404 | PyTuple_SET_ITEM(names, i, alias->name); |
3405 | } |
3406 | |
3407 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
3408 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__" )) { |
3409 | Py_DECREF(names); |
3410 | return compiler_error(c, "from __future__ imports must occur " |
3411 | "at the beginning of the file" ); |
3412 | } |
3413 | ADDOP_LOAD_CONST_NEW(c, names); |
3414 | |
3415 | if (s->v.ImportFrom.module) { |
3416 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
3417 | } |
3418 | else { |
3419 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
3420 | } |
3421 | for (i = 0; i < n; i++) { |
3422 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
3423 | identifier store_name; |
3424 | |
3425 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
3426 | assert(n == 1); |
3427 | ADDOP(c, IMPORT_STAR); |
3428 | return 1; |
3429 | } |
3430 | |
3431 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
3432 | store_name = alias->name; |
3433 | if (alias->asname) |
3434 | store_name = alias->asname; |
3435 | |
3436 | if (!compiler_nameop(c, store_name, Store)) { |
3437 | return 0; |
3438 | } |
3439 | } |
3440 | /* remove imported module */ |
3441 | ADDOP(c, POP_TOP); |
3442 | return 1; |
3443 | } |
3444 | |
3445 | static int |
3446 | compiler_assert(struct compiler *c, stmt_ty s) |
3447 | { |
3448 | basicblock *end; |
3449 | |
3450 | /* Always emit a warning if the test is a non-zero length tuple */ |
3451 | if ((s->v.Assert.test->kind == Tuple_kind && |
3452 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || |
3453 | (s->v.Assert.test->kind == Constant_kind && |
3454 | PyTuple_Check(s->v.Assert.test->v.Constant.value) && |
3455 | PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) |
3456 | { |
3457 | if (!compiler_warn(c, "assertion is always true, " |
3458 | "perhaps remove parentheses?" )) |
3459 | { |
3460 | return 0; |
3461 | } |
3462 | } |
3463 | if (c->c_optimize) |
3464 | return 1; |
3465 | end = compiler_new_block(c); |
3466 | if (end == NULL) |
3467 | return 0; |
3468 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
3469 | return 0; |
3470 | ADDOP(c, LOAD_ASSERTION_ERROR); |
3471 | if (s->v.Assert.msg) { |
3472 | VISIT(c, expr, s->v.Assert.msg); |
3473 | ADDOP_I(c, CALL_FUNCTION, 1); |
3474 | } |
3475 | ADDOP_I(c, RAISE_VARARGS, 1); |
3476 | compiler_use_next_block(c, end); |
3477 | return 1; |
3478 | } |
3479 | |
3480 | static int |
3481 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
3482 | { |
3483 | if (c->c_interactive && c->c_nestlevel <= 1) { |
3484 | VISIT(c, expr, value); |
3485 | ADDOP(c, PRINT_EXPR); |
3486 | return 1; |
3487 | } |
3488 | |
3489 | if (value->kind == Constant_kind) { |
3490 | /* ignore constant statement */ |
3491 | ADDOP(c, NOP); |
3492 | return 1; |
3493 | } |
3494 | |
3495 | VISIT(c, expr, value); |
3496 | /* Mark POP_TOP as artificial */ |
3497 | c->u->u_lineno = -1; |
3498 | ADDOP(c, POP_TOP); |
3499 | return 1; |
3500 | } |
3501 | |
3502 | static int |
3503 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
3504 | { |
3505 | Py_ssize_t i, n; |
3506 | |
3507 | /* Always assign a lineno to the next instruction for a stmt. */ |
3508 | SET_LOC(c, s); |
3509 | |
3510 | switch (s->kind) { |
3511 | case FunctionDef_kind: |
3512 | return compiler_function(c, s, 0); |
3513 | case ClassDef_kind: |
3514 | return compiler_class(c, s); |
3515 | case Return_kind: |
3516 | return compiler_return(c, s); |
3517 | case Delete_kind: |
3518 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
3519 | break; |
3520 | case Assign_kind: |
3521 | n = asdl_seq_LEN(s->v.Assign.targets); |
3522 | VISIT(c, expr, s->v.Assign.value); |
3523 | for (i = 0; i < n; i++) { |
3524 | if (i < n - 1) |
3525 | ADDOP(c, DUP_TOP); |
3526 | VISIT(c, expr, |
3527 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
3528 | } |
3529 | break; |
3530 | case AugAssign_kind: |
3531 | return compiler_augassign(c, s); |
3532 | case AnnAssign_kind: |
3533 | return compiler_annassign(c, s); |
3534 | case For_kind: |
3535 | return compiler_for(c, s); |
3536 | case While_kind: |
3537 | return compiler_while(c, s); |
3538 | case If_kind: |
3539 | return compiler_if(c, s); |
3540 | case Match_kind: |
3541 | return compiler_match(c, s); |
3542 | case Raise_kind: |
3543 | n = 0; |
3544 | if (s->v.Raise.exc) { |
3545 | VISIT(c, expr, s->v.Raise.exc); |
3546 | n++; |
3547 | if (s->v.Raise.cause) { |
3548 | VISIT(c, expr, s->v.Raise.cause); |
3549 | n++; |
3550 | } |
3551 | } |
3552 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
3553 | NEXT_BLOCK(c); |
3554 | break; |
3555 | case Try_kind: |
3556 | return compiler_try(c, s); |
3557 | case Assert_kind: |
3558 | return compiler_assert(c, s); |
3559 | case Import_kind: |
3560 | return compiler_import(c, s); |
3561 | case ImportFrom_kind: |
3562 | return compiler_from_import(c, s); |
3563 | case Global_kind: |
3564 | case Nonlocal_kind: |
3565 | break; |
3566 | case Expr_kind: |
3567 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
3568 | case Pass_kind: |
3569 | ADDOP(c, NOP); |
3570 | break; |
3571 | case Break_kind: |
3572 | return compiler_break(c); |
3573 | case Continue_kind: |
3574 | return compiler_continue(c); |
3575 | case With_kind: |
3576 | return compiler_with(c, s, 0); |
3577 | case AsyncFunctionDef_kind: |
3578 | return compiler_function(c, s, 1); |
3579 | case AsyncWith_kind: |
3580 | return compiler_async_with(c, s, 0); |
3581 | case AsyncFor_kind: |
3582 | return compiler_async_for(c, s); |
3583 | } |
3584 | |
3585 | return 1; |
3586 | } |
3587 | |
3588 | static int |
3589 | unaryop(unaryop_ty op) |
3590 | { |
3591 | switch (op) { |
3592 | case Invert: |
3593 | return UNARY_INVERT; |
3594 | case Not: |
3595 | return UNARY_NOT; |
3596 | case UAdd: |
3597 | return UNARY_POSITIVE; |
3598 | case USub: |
3599 | return UNARY_NEGATIVE; |
3600 | default: |
3601 | PyErr_Format(PyExc_SystemError, |
3602 | "unary op %d should not be possible" , op); |
3603 | return 0; |
3604 | } |
3605 | } |
3606 | |
3607 | static int |
3608 | binop(operator_ty op) |
3609 | { |
3610 | switch (op) { |
3611 | case Add: |
3612 | return BINARY_ADD; |
3613 | case Sub: |
3614 | return BINARY_SUBTRACT; |
3615 | case Mult: |
3616 | return BINARY_MULTIPLY; |
3617 | case MatMult: |
3618 | return BINARY_MATRIX_MULTIPLY; |
3619 | case Div: |
3620 | return BINARY_TRUE_DIVIDE; |
3621 | case Mod: |
3622 | return BINARY_MODULO; |
3623 | case Pow: |
3624 | return BINARY_POWER; |
3625 | case LShift: |
3626 | return BINARY_LSHIFT; |
3627 | case RShift: |
3628 | return BINARY_RSHIFT; |
3629 | case BitOr: |
3630 | return BINARY_OR; |
3631 | case BitXor: |
3632 | return BINARY_XOR; |
3633 | case BitAnd: |
3634 | return BINARY_AND; |
3635 | case FloorDiv: |
3636 | return BINARY_FLOOR_DIVIDE; |
3637 | default: |
3638 | PyErr_Format(PyExc_SystemError, |
3639 | "binary op %d should not be possible" , op); |
3640 | return 0; |
3641 | } |
3642 | } |
3643 | |
3644 | static int |
3645 | inplace_binop(operator_ty op) |
3646 | { |
3647 | switch (op) { |
3648 | case Add: |
3649 | return INPLACE_ADD; |
3650 | case Sub: |
3651 | return INPLACE_SUBTRACT; |
3652 | case Mult: |
3653 | return INPLACE_MULTIPLY; |
3654 | case MatMult: |
3655 | return INPLACE_MATRIX_MULTIPLY; |
3656 | case Div: |
3657 | return INPLACE_TRUE_DIVIDE; |
3658 | case Mod: |
3659 | return INPLACE_MODULO; |
3660 | case Pow: |
3661 | return INPLACE_POWER; |
3662 | case LShift: |
3663 | return INPLACE_LSHIFT; |
3664 | case RShift: |
3665 | return INPLACE_RSHIFT; |
3666 | case BitOr: |
3667 | return INPLACE_OR; |
3668 | case BitXor: |
3669 | return INPLACE_XOR; |
3670 | case BitAnd: |
3671 | return INPLACE_AND; |
3672 | case FloorDiv: |
3673 | return INPLACE_FLOOR_DIVIDE; |
3674 | default: |
3675 | PyErr_Format(PyExc_SystemError, |
3676 | "inplace binary op %d should not be possible" , op); |
3677 | return 0; |
3678 | } |
3679 | } |
3680 | |
3681 | static int |
3682 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
3683 | { |
3684 | int op, scope; |
3685 | Py_ssize_t arg; |
3686 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
3687 | |
3688 | PyObject *dict = c->u->u_names; |
3689 | PyObject *mangled; |
3690 | |
3691 | assert(!_PyUnicode_EqualToASCIIString(name, "None" ) && |
3692 | !_PyUnicode_EqualToASCIIString(name, "True" ) && |
3693 | !_PyUnicode_EqualToASCIIString(name, "False" )); |
3694 | |
3695 | if (forbidden_name(c, name, ctx)) |
3696 | return 0; |
3697 | |
3698 | mangled = _Py_Mangle(c->u->u_private, name); |
3699 | if (!mangled) |
3700 | return 0; |
3701 | |
3702 | op = 0; |
3703 | optype = OP_NAME; |
3704 | scope = _PyST_GetScope(c->u->u_ste, mangled); |
3705 | switch (scope) { |
3706 | case FREE: |
3707 | dict = c->u->u_freevars; |
3708 | optype = OP_DEREF; |
3709 | break; |
3710 | case CELL: |
3711 | dict = c->u->u_cellvars; |
3712 | optype = OP_DEREF; |
3713 | break; |
3714 | case LOCAL: |
3715 | if (c->u->u_ste->ste_type == FunctionBlock) |
3716 | optype = OP_FAST; |
3717 | break; |
3718 | case GLOBAL_IMPLICIT: |
3719 | if (c->u->u_ste->ste_type == FunctionBlock) |
3720 | optype = OP_GLOBAL; |
3721 | break; |
3722 | case GLOBAL_EXPLICIT: |
3723 | optype = OP_GLOBAL; |
3724 | break; |
3725 | default: |
3726 | /* scope can be 0 */ |
3727 | break; |
3728 | } |
3729 | |
3730 | /* XXX Leave assert here, but handle __doc__ and the like better */ |
3731 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
3732 | |
3733 | switch (optype) { |
3734 | case OP_DEREF: |
3735 | switch (ctx) { |
3736 | case Load: |
3737 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
3738 | break; |
3739 | case Store: op = STORE_DEREF; break; |
3740 | case Del: op = DELETE_DEREF; break; |
3741 | } |
3742 | break; |
3743 | case OP_FAST: |
3744 | switch (ctx) { |
3745 | case Load: op = LOAD_FAST; break; |
3746 | case Store: op = STORE_FAST; break; |
3747 | case Del: op = DELETE_FAST; break; |
3748 | } |
3749 | ADDOP_N(c, op, mangled, varnames); |
3750 | return 1; |
3751 | case OP_GLOBAL: |
3752 | switch (ctx) { |
3753 | case Load: op = LOAD_GLOBAL; break; |
3754 | case Store: op = STORE_GLOBAL; break; |
3755 | case Del: op = DELETE_GLOBAL; break; |
3756 | } |
3757 | break; |
3758 | case OP_NAME: |
3759 | switch (ctx) { |
3760 | case Load: op = LOAD_NAME; break; |
3761 | case Store: op = STORE_NAME; break; |
3762 | case Del: op = DELETE_NAME; break; |
3763 | } |
3764 | break; |
3765 | } |
3766 | |
3767 | assert(op); |
3768 | arg = compiler_add_o(dict, mangled); |
3769 | Py_DECREF(mangled); |
3770 | if (arg < 0) |
3771 | return 0; |
3772 | return compiler_addop_i(c, op, arg); |
3773 | } |
3774 | |
3775 | static int |
3776 | compiler_boolop(struct compiler *c, expr_ty e) |
3777 | { |
3778 | basicblock *end; |
3779 | int jumpi; |
3780 | Py_ssize_t i, n; |
3781 | asdl_expr_seq *s; |
3782 | |
3783 | assert(e->kind == BoolOp_kind); |
3784 | if (e->v.BoolOp.op == And) |
3785 | jumpi = JUMP_IF_FALSE_OR_POP; |
3786 | else |
3787 | jumpi = JUMP_IF_TRUE_OR_POP; |
3788 | end = compiler_new_block(c); |
3789 | if (end == NULL) |
3790 | return 0; |
3791 | s = e->v.BoolOp.values; |
3792 | n = asdl_seq_LEN(s) - 1; |
3793 | assert(n >= 0); |
3794 | for (i = 0; i < n; ++i) { |
3795 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
3796 | ADDOP_JUMP(c, jumpi, end); |
3797 | basicblock *next = compiler_new_block(c); |
3798 | if (next == NULL) { |
3799 | return 0; |
3800 | } |
3801 | compiler_use_next_block(c, next); |
3802 | } |
3803 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
3804 | compiler_use_next_block(c, end); |
3805 | return 1; |
3806 | } |
3807 | |
3808 | static int |
3809 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
3810 | int build, int add, int extend, int tuple) |
3811 | { |
3812 | Py_ssize_t n = asdl_seq_LEN(elts); |
3813 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
3814 | PyObject *folded = PyTuple_New(n); |
3815 | if (folded == NULL) { |
3816 | return 0; |
3817 | } |
3818 | PyObject *val; |
3819 | for (Py_ssize_t i = 0; i < n; i++) { |
3820 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
3821 | Py_INCREF(val); |
3822 | PyTuple_SET_ITEM(folded, i, val); |
3823 | } |
3824 | if (tuple) { |
3825 | ADDOP_LOAD_CONST_NEW(c, folded); |
3826 | } else { |
3827 | if (add == SET_ADD) { |
3828 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
3829 | if (folded == NULL) { |
3830 | return 0; |
3831 | } |
3832 | } |
3833 | ADDOP_I(c, build, pushed); |
3834 | ADDOP_LOAD_CONST_NEW(c, folded); |
3835 | ADDOP_I(c, extend, 1); |
3836 | } |
3837 | return 1; |
3838 | } |
3839 | |
3840 | int big = n+pushed > STACK_USE_GUIDELINE; |
3841 | int seen_star = 0; |
3842 | for (Py_ssize_t i = 0; i < n; i++) { |
3843 | expr_ty elt = asdl_seq_GET(elts, i); |
3844 | if (elt->kind == Starred_kind) { |
3845 | seen_star = 1; |
3846 | } |
3847 | } |
3848 | if (!seen_star && !big) { |
3849 | for (Py_ssize_t i = 0; i < n; i++) { |
3850 | expr_ty elt = asdl_seq_GET(elts, i); |
3851 | VISIT(c, expr, elt); |
3852 | } |
3853 | if (tuple) { |
3854 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
3855 | } else { |
3856 | ADDOP_I(c, build, n+pushed); |
3857 | } |
3858 | return 1; |
3859 | } |
3860 | int sequence_built = 0; |
3861 | if (big) { |
3862 | ADDOP_I(c, build, pushed); |
3863 | sequence_built = 1; |
3864 | } |
3865 | for (Py_ssize_t i = 0; i < n; i++) { |
3866 | expr_ty elt = asdl_seq_GET(elts, i); |
3867 | if (elt->kind == Starred_kind) { |
3868 | if (sequence_built == 0) { |
3869 | ADDOP_I(c, build, i+pushed); |
3870 | sequence_built = 1; |
3871 | } |
3872 | VISIT(c, expr, elt->v.Starred.value); |
3873 | ADDOP_I(c, extend, 1); |
3874 | } |
3875 | else { |
3876 | VISIT(c, expr, elt); |
3877 | if (sequence_built) { |
3878 | ADDOP_I(c, add, 1); |
3879 | } |
3880 | } |
3881 | } |
3882 | assert(sequence_built); |
3883 | if (tuple) { |
3884 | ADDOP(c, LIST_TO_TUPLE); |
3885 | } |
3886 | return 1; |
3887 | } |
3888 | |
3889 | static int |
3890 | unpack_helper(struct compiler *c, asdl_expr_seq *elts) |
3891 | { |
3892 | Py_ssize_t n = asdl_seq_LEN(elts); |
3893 | int seen_star = 0; |
3894 | for (Py_ssize_t i = 0; i < n; i++) { |
3895 | expr_ty elt = asdl_seq_GET(elts, i); |
3896 | if (elt->kind == Starred_kind && !seen_star) { |
3897 | if ((i >= (1 << 8)) || |
3898 | (n-i-1 >= (INT_MAX >> 8))) |
3899 | return compiler_error(c, |
3900 | "too many expressions in " |
3901 | "star-unpacking assignment" ); |
3902 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
3903 | seen_star = 1; |
3904 | } |
3905 | else if (elt->kind == Starred_kind) { |
3906 | return compiler_error(c, |
3907 | "multiple starred expressions in assignment" ); |
3908 | } |
3909 | } |
3910 | if (!seen_star) { |
3911 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
3912 | } |
3913 | return 1; |
3914 | } |
3915 | |
3916 | static int |
3917 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
3918 | { |
3919 | Py_ssize_t n = asdl_seq_LEN(elts); |
3920 | RETURN_IF_FALSE(unpack_helper(c, elts)); |
3921 | for (Py_ssize_t i = 0; i < n; i++) { |
3922 | expr_ty elt = asdl_seq_GET(elts, i); |
3923 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
3924 | } |
3925 | return 1; |
3926 | } |
3927 | |
3928 | static int |
3929 | compiler_list(struct compiler *c, expr_ty e) |
3930 | { |
3931 | asdl_expr_seq *elts = e->v.List.elts; |
3932 | if (e->v.List.ctx == Store) { |
3933 | return assignment_helper(c, elts); |
3934 | } |
3935 | else if (e->v.List.ctx == Load) { |
3936 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
3937 | LIST_APPEND, LIST_EXTEND, 0); |
3938 | } |
3939 | else |
3940 | VISIT_SEQ(c, expr, elts); |
3941 | return 1; |
3942 | } |
3943 | |
3944 | static int |
3945 | compiler_tuple(struct compiler *c, expr_ty e) |
3946 | { |
3947 | asdl_expr_seq *elts = e->v.Tuple.elts; |
3948 | if (e->v.Tuple.ctx == Store) { |
3949 | return assignment_helper(c, elts); |
3950 | } |
3951 | else if (e->v.Tuple.ctx == Load) { |
3952 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
3953 | LIST_APPEND, LIST_EXTEND, 1); |
3954 | } |
3955 | else |
3956 | VISIT_SEQ(c, expr, elts); |
3957 | return 1; |
3958 | } |
3959 | |
3960 | static int |
3961 | compiler_set(struct compiler *c, expr_ty e) |
3962 | { |
3963 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
3964 | SET_ADD, SET_UPDATE, 0); |
3965 | } |
3966 | |
3967 | static int |
3968 | are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
3969 | { |
3970 | Py_ssize_t i; |
3971 | for (i = begin; i < end; i++) { |
3972 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
3973 | if (key == NULL || key->kind != Constant_kind) |
3974 | return 0; |
3975 | } |
3976 | return 1; |
3977 | } |
3978 | |
3979 | static int |
3980 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
3981 | { |
3982 | Py_ssize_t i, n = end - begin; |
3983 | PyObject *keys, *key; |
3984 | int big = n*2 > STACK_USE_GUIDELINE; |
3985 | if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) { |
3986 | for (i = begin; i < end; i++) { |
3987 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
3988 | } |
3989 | keys = PyTuple_New(n); |
3990 | if (keys == NULL) { |
3991 | return 0; |
3992 | } |
3993 | for (i = begin; i < end; i++) { |
3994 | key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value; |
3995 | Py_INCREF(key); |
3996 | PyTuple_SET_ITEM(keys, i - begin, key); |
3997 | } |
3998 | ADDOP_LOAD_CONST_NEW(c, keys); |
3999 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
4000 | return 1; |
4001 | } |
4002 | if (big) { |
4003 | ADDOP_I(c, BUILD_MAP, 0); |
4004 | } |
4005 | for (i = begin; i < end; i++) { |
4006 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
4007 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
4008 | if (big) { |
4009 | ADDOP_I(c, MAP_ADD, 1); |
4010 | } |
4011 | } |
4012 | if (!big) { |
4013 | ADDOP_I(c, BUILD_MAP, n); |
4014 | } |
4015 | return 1; |
4016 | } |
4017 | |
4018 | static int |
4019 | compiler_dict(struct compiler *c, expr_ty e) |
4020 | { |
4021 | Py_ssize_t i, n, elements; |
4022 | int have_dict; |
4023 | int is_unpacking = 0; |
4024 | n = asdl_seq_LEN(e->v.Dict.values); |
4025 | have_dict = 0; |
4026 | elements = 0; |
4027 | for (i = 0; i < n; i++) { |
4028 | is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; |
4029 | if (is_unpacking) { |
4030 | if (elements) { |
4031 | if (!compiler_subdict(c, e, i - elements, i)) { |
4032 | return 0; |
4033 | } |
4034 | if (have_dict) { |
4035 | ADDOP_I(c, DICT_UPDATE, 1); |
4036 | } |
4037 | have_dict = 1; |
4038 | elements = 0; |
4039 | } |
4040 | if (have_dict == 0) { |
4041 | ADDOP_I(c, BUILD_MAP, 0); |
4042 | have_dict = 1; |
4043 | } |
4044 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
4045 | ADDOP_I(c, DICT_UPDATE, 1); |
4046 | } |
4047 | else { |
4048 | if (elements*2 > STACK_USE_GUIDELINE) { |
4049 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
4050 | return 0; |
4051 | } |
4052 | if (have_dict) { |
4053 | ADDOP_I(c, DICT_UPDATE, 1); |
4054 | } |
4055 | have_dict = 1; |
4056 | elements = 0; |
4057 | } |
4058 | else { |
4059 | elements++; |
4060 | } |
4061 | } |
4062 | } |
4063 | if (elements) { |
4064 | if (!compiler_subdict(c, e, n - elements, n)) { |
4065 | return 0; |
4066 | } |
4067 | if (have_dict) { |
4068 | ADDOP_I(c, DICT_UPDATE, 1); |
4069 | } |
4070 | have_dict = 1; |
4071 | } |
4072 | if (!have_dict) { |
4073 | ADDOP_I(c, BUILD_MAP, 0); |
4074 | } |
4075 | return 1; |
4076 | } |
4077 | |
4078 | static int |
4079 | compiler_compare(struct compiler *c, expr_ty e) |
4080 | { |
4081 | Py_ssize_t i, n; |
4082 | |
4083 | if (!check_compare(c, e)) { |
4084 | return 0; |
4085 | } |
4086 | VISIT(c, expr, e->v.Compare.left); |
4087 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
4088 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
4089 | if (n == 0) { |
4090 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
4091 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
4092 | } |
4093 | else { |
4094 | basicblock *cleanup = compiler_new_block(c); |
4095 | if (cleanup == NULL) |
4096 | return 0; |
4097 | for (i = 0; i < n; i++) { |
4098 | VISIT(c, expr, |
4099 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
4100 | ADDOP(c, DUP_TOP); |
4101 | ADDOP(c, ROT_THREE); |
4102 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
4103 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
4104 | NEXT_BLOCK(c); |
4105 | } |
4106 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
4107 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
4108 | basicblock *end = compiler_new_block(c); |
4109 | if (end == NULL) |
4110 | return 0; |
4111 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
4112 | compiler_use_next_block(c, cleanup); |
4113 | ADDOP(c, ROT_TWO); |
4114 | ADDOP(c, POP_TOP); |
4115 | compiler_use_next_block(c, end); |
4116 | } |
4117 | return 1; |
4118 | } |
4119 | |
4120 | static PyTypeObject * |
4121 | infer_type(expr_ty e) |
4122 | { |
4123 | switch (e->kind) { |
4124 | case Tuple_kind: |
4125 | return &PyTuple_Type; |
4126 | case List_kind: |
4127 | case ListComp_kind: |
4128 | return &PyList_Type; |
4129 | case Dict_kind: |
4130 | case DictComp_kind: |
4131 | return &PyDict_Type; |
4132 | case Set_kind: |
4133 | case SetComp_kind: |
4134 | return &PySet_Type; |
4135 | case GeneratorExp_kind: |
4136 | return &PyGen_Type; |
4137 | case Lambda_kind: |
4138 | return &PyFunction_Type; |
4139 | case JoinedStr_kind: |
4140 | case FormattedValue_kind: |
4141 | return &PyUnicode_Type; |
4142 | case Constant_kind: |
4143 | return Py_TYPE(e->v.Constant.value); |
4144 | default: |
4145 | return NULL; |
4146 | } |
4147 | } |
4148 | |
4149 | static int |
4150 | check_caller(struct compiler *c, expr_ty e) |
4151 | { |
4152 | switch (e->kind) { |
4153 | case Constant_kind: |
4154 | case Tuple_kind: |
4155 | case List_kind: |
4156 | case ListComp_kind: |
4157 | case Dict_kind: |
4158 | case DictComp_kind: |
4159 | case Set_kind: |
4160 | case SetComp_kind: |
4161 | case GeneratorExp_kind: |
4162 | case JoinedStr_kind: |
4163 | case FormattedValue_kind: |
4164 | return compiler_warn(c, "'%.200s' object is not callable; " |
4165 | "perhaps you missed a comma?" , |
4166 | infer_type(e)->tp_name); |
4167 | default: |
4168 | return 1; |
4169 | } |
4170 | } |
4171 | |
4172 | static int |
4173 | check_subscripter(struct compiler *c, expr_ty e) |
4174 | { |
4175 | PyObject *v; |
4176 | |
4177 | switch (e->kind) { |
4178 | case Constant_kind: |
4179 | v = e->v.Constant.value; |
4180 | if (!(v == Py_None || v == Py_Ellipsis || |
4181 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
4182 | PyAnySet_Check(v))) |
4183 | { |
4184 | return 1; |
4185 | } |
4186 | /* fall through */ |
4187 | case Set_kind: |
4188 | case SetComp_kind: |
4189 | case GeneratorExp_kind: |
4190 | case Lambda_kind: |
4191 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
4192 | "perhaps you missed a comma?" , |
4193 | infer_type(e)->tp_name); |
4194 | default: |
4195 | return 1; |
4196 | } |
4197 | } |
4198 | |
4199 | static int |
4200 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
4201 | { |
4202 | PyObject *v; |
4203 | |
4204 | PyTypeObject *index_type = infer_type(s); |
4205 | if (index_type == NULL |
4206 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
4207 | || index_type == &PySlice_Type) { |
4208 | return 1; |
4209 | } |
4210 | |
4211 | switch (e->kind) { |
4212 | case Constant_kind: |
4213 | v = e->v.Constant.value; |
4214 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
4215 | return 1; |
4216 | } |
4217 | /* fall through */ |
4218 | case Tuple_kind: |
4219 | case List_kind: |
4220 | case ListComp_kind: |
4221 | case JoinedStr_kind: |
4222 | case FormattedValue_kind: |
4223 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
4224 | "not %.200s; " |
4225 | "perhaps you missed a comma?" , |
4226 | infer_type(e)->tp_name, |
4227 | index_type->tp_name); |
4228 | default: |
4229 | return 1; |
4230 | } |
4231 | } |
4232 | |
4233 | // Return 1 if the method call was optimized, -1 if not, and 0 on error. |
4234 | static int |
4235 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
4236 | { |
4237 | Py_ssize_t argsl, i; |
4238 | expr_ty meth = e->v.Call.func; |
4239 | asdl_expr_seq *args = e->v.Call.args; |
4240 | |
4241 | /* Check that the call node is an attribute access, and that |
4242 | the call doesn't have keyword parameters. */ |
4243 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
4244 | asdl_seq_LEN(e->v.Call.keywords)) { |
4245 | return -1; |
4246 | } |
4247 | /* Check that there aren't too many arguments */ |
4248 | argsl = asdl_seq_LEN(args); |
4249 | if (argsl >= STACK_USE_GUIDELINE) { |
4250 | return -1; |
4251 | } |
4252 | /* Check that there are no *varargs types of arguments. */ |
4253 | for (i = 0; i < argsl; i++) { |
4254 | expr_ty elt = asdl_seq_GET(args, i); |
4255 | if (elt->kind == Starred_kind) { |
4256 | return -1; |
4257 | } |
4258 | } |
4259 | |
4260 | /* Alright, we can optimize the code. */ |
4261 | VISIT(c, expr, meth->v.Attribute.value); |
4262 | int old_lineno = c->u->u_lineno; |
4263 | c->u->u_lineno = meth->end_lineno; |
4264 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
4265 | VISIT_SEQ(c, expr, e->v.Call.args); |
4266 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
4267 | c->u->u_lineno = old_lineno; |
4268 | return 1; |
4269 | } |
4270 | |
4271 | static int |
4272 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
4273 | { |
4274 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
4275 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
4276 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
4277 | if (key->arg == NULL) { |
4278 | continue; |
4279 | } |
4280 | if (forbidden_name(c, key->arg, Store)) { |
4281 | return -1; |
4282 | } |
4283 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
4284 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
4285 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
4286 | SET_LOC(c, other); |
4287 | compiler_error(c, "keyword argument repeated: %U" , key->arg); |
4288 | return -1; |
4289 | } |
4290 | } |
4291 | } |
4292 | return 0; |
4293 | } |
4294 | |
4295 | static int |
4296 | compiler_call(struct compiler *c, expr_ty e) |
4297 | { |
4298 | int ret = maybe_optimize_method_call(c, e); |
4299 | if (ret >= 0) { |
4300 | return ret; |
4301 | } |
4302 | if (!check_caller(c, e->v.Call.func)) { |
4303 | return 0; |
4304 | } |
4305 | VISIT(c, expr, e->v.Call.func); |
4306 | return compiler_call_helper(c, 0, |
4307 | e->v.Call.args, |
4308 | e->v.Call.keywords); |
4309 | } |
4310 | |
4311 | static int |
4312 | compiler_joined_str(struct compiler *c, expr_ty e) |
4313 | { |
4314 | |
4315 | Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values); |
4316 | if (value_count > STACK_USE_GUIDELINE) { |
4317 | ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("" , 0)); |
4318 | PyObject *join = _PyUnicode_FromASCII("join" , 4); |
4319 | if (join == NULL) { |
4320 | return 0; |
4321 | } |
4322 | ADDOP_NAME(c, LOAD_METHOD, join, names); |
4323 | Py_DECREF(join); |
4324 | ADDOP_I(c, BUILD_LIST, 0); |
4325 | for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) { |
4326 | VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i)); |
4327 | ADDOP_I(c, LIST_APPEND, 1); |
4328 | } |
4329 | ADDOP_I(c, CALL_METHOD, 1); |
4330 | } |
4331 | else { |
4332 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
4333 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) { |
4334 | ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); |
4335 | } |
4336 | } |
4337 | return 1; |
4338 | } |
4339 | |
4340 | /* Used to implement f-strings. Format a single value. */ |
4341 | static int |
4342 | compiler_formatted_value(struct compiler *c, expr_ty e) |
4343 | { |
4344 | /* Our oparg encodes 2 pieces of information: the conversion |
4345 | character, and whether or not a format_spec was provided. |
4346 | |
4347 | Convert the conversion char to 3 bits: |
4348 | : 000 0x0 FVC_NONE The default if nothing specified. |
4349 | !s : 001 0x1 FVC_STR |
4350 | !r : 010 0x2 FVC_REPR |
4351 | !a : 011 0x3 FVC_ASCII |
4352 | |
4353 | next bit is whether or not we have a format spec: |
4354 | yes : 100 0x4 |
4355 | no : 000 0x0 |
4356 | */ |
4357 | |
4358 | int conversion = e->v.FormattedValue.conversion; |
4359 | int oparg; |
4360 | |
4361 | /* The expression to be formatted. */ |
4362 | VISIT(c, expr, e->v.FormattedValue.value); |
4363 | |
4364 | switch (conversion) { |
4365 | case 's': oparg = FVC_STR; break; |
4366 | case 'r': oparg = FVC_REPR; break; |
4367 | case 'a': oparg = FVC_ASCII; break; |
4368 | case -1: oparg = FVC_NONE; break; |
4369 | default: |
4370 | PyErr_Format(PyExc_SystemError, |
4371 | "Unrecognized conversion character %d" , conversion); |
4372 | return 0; |
4373 | } |
4374 | if (e->v.FormattedValue.format_spec) { |
4375 | /* Evaluate the format spec, and update our opcode arg. */ |
4376 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
4377 | oparg |= FVS_HAVE_SPEC; |
4378 | } |
4379 | |
4380 | /* And push our opcode and oparg */ |
4381 | ADDOP_I(c, FORMAT_VALUE, oparg); |
4382 | |
4383 | return 1; |
4384 | } |
4385 | |
4386 | static int |
4387 | compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
4388 | { |
4389 | Py_ssize_t i, n = end - begin; |
4390 | keyword_ty kw; |
4391 | PyObject *keys, *key; |
4392 | assert(n > 0); |
4393 | int big = n*2 > STACK_USE_GUIDELINE; |
4394 | if (n > 1 && !big) { |
4395 | for (i = begin; i < end; i++) { |
4396 | kw = asdl_seq_GET(keywords, i); |
4397 | VISIT(c, expr, kw->value); |
4398 | } |
4399 | keys = PyTuple_New(n); |
4400 | if (keys == NULL) { |
4401 | return 0; |
4402 | } |
4403 | for (i = begin; i < end; i++) { |
4404 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
4405 | Py_INCREF(key); |
4406 | PyTuple_SET_ITEM(keys, i - begin, key); |
4407 | } |
4408 | ADDOP_LOAD_CONST_NEW(c, keys); |
4409 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
4410 | return 1; |
4411 | } |
4412 | if (big) { |
4413 | ADDOP_I_NOLINE(c, BUILD_MAP, 0); |
4414 | } |
4415 | for (i = begin; i < end; i++) { |
4416 | kw = asdl_seq_GET(keywords, i); |
4417 | ADDOP_LOAD_CONST(c, kw->arg); |
4418 | VISIT(c, expr, kw->value); |
4419 | if (big) { |
4420 | ADDOP_I_NOLINE(c, MAP_ADD, 1); |
4421 | } |
4422 | } |
4423 | if (!big) { |
4424 | ADDOP_I(c, BUILD_MAP, n); |
4425 | } |
4426 | return 1; |
4427 | } |
4428 | |
4429 | /* shared code between compiler_call and compiler_class */ |
4430 | static int |
4431 | compiler_call_helper(struct compiler *c, |
4432 | int n, /* Args already pushed */ |
4433 | asdl_expr_seq *args, |
4434 | asdl_keyword_seq *keywords) |
4435 | { |
4436 | Py_ssize_t i, nseen, nelts, nkwelts; |
4437 | |
4438 | if (validate_keywords(c, keywords) == -1) { |
4439 | return 0; |
4440 | } |
4441 | |
4442 | nelts = asdl_seq_LEN(args); |
4443 | nkwelts = asdl_seq_LEN(keywords); |
4444 | |
4445 | if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) { |
4446 | goto ex_call; |
4447 | } |
4448 | for (i = 0; i < nelts; i++) { |
4449 | expr_ty elt = asdl_seq_GET(args, i); |
4450 | if (elt->kind == Starred_kind) { |
4451 | goto ex_call; |
4452 | } |
4453 | } |
4454 | for (i = 0; i < nkwelts; i++) { |
4455 | keyword_ty kw = asdl_seq_GET(keywords, i); |
4456 | if (kw->arg == NULL) { |
4457 | goto ex_call; |
4458 | } |
4459 | } |
4460 | |
4461 | /* No * or ** args, so can use faster calling sequence */ |
4462 | for (i = 0; i < nelts; i++) { |
4463 | expr_ty elt = asdl_seq_GET(args, i); |
4464 | assert(elt->kind != Starred_kind); |
4465 | VISIT(c, expr, elt); |
4466 | } |
4467 | if (nkwelts) { |
4468 | PyObject *names; |
4469 | VISIT_SEQ(c, keyword, keywords); |
4470 | names = PyTuple_New(nkwelts); |
4471 | if (names == NULL) { |
4472 | return 0; |
4473 | } |
4474 | for (i = 0; i < nkwelts; i++) { |
4475 | keyword_ty kw = asdl_seq_GET(keywords, i); |
4476 | Py_INCREF(kw->arg); |
4477 | PyTuple_SET_ITEM(names, i, kw->arg); |
4478 | } |
4479 | ADDOP_LOAD_CONST_NEW(c, names); |
4480 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
4481 | return 1; |
4482 | } |
4483 | else { |
4484 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
4485 | return 1; |
4486 | } |
4487 | |
4488 | ex_call: |
4489 | |
4490 | /* Do positional arguments. */ |
4491 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
4492 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
4493 | } |
4494 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
4495 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
4496 | return 0; |
4497 | } |
4498 | /* Then keyword arguments */ |
4499 | if (nkwelts) { |
4500 | /* Has a new dict been pushed */ |
4501 | int have_dict = 0; |
4502 | |
4503 | nseen = 0; /* the number of keyword arguments on the stack following */ |
4504 | for (i = 0; i < nkwelts; i++) { |
4505 | keyword_ty kw = asdl_seq_GET(keywords, i); |
4506 | if (kw->arg == NULL) { |
4507 | /* A keyword argument unpacking. */ |
4508 | if (nseen) { |
4509 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
4510 | return 0; |
4511 | } |
4512 | if (have_dict) { |
4513 | ADDOP_I(c, DICT_MERGE, 1); |
4514 | } |
4515 | have_dict = 1; |
4516 | nseen = 0; |
4517 | } |
4518 | if (!have_dict) { |
4519 | ADDOP_I(c, BUILD_MAP, 0); |
4520 | have_dict = 1; |
4521 | } |
4522 | VISIT(c, expr, kw->value); |
4523 | ADDOP_I(c, DICT_MERGE, 1); |
4524 | } |
4525 | else { |
4526 | nseen++; |
4527 | } |
4528 | } |
4529 | if (nseen) { |
4530 | /* Pack up any trailing keyword arguments. */ |
4531 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
4532 | return 0; |
4533 | } |
4534 | if (have_dict) { |
4535 | ADDOP_I(c, DICT_MERGE, 1); |
4536 | } |
4537 | have_dict = 1; |
4538 | } |
4539 | assert(have_dict); |
4540 | } |
4541 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
4542 | return 1; |
4543 | } |
4544 | |
4545 | |
4546 | /* List and set comprehensions and generator expressions work by creating a |
4547 | nested function to perform the actual iteration. This means that the |
4548 | iteration variables don't leak into the current scope. |
4549 | The defined function is called immediately following its definition, with the |
4550 | result of that call being the result of the expression. |
4551 | The LC/SC version returns the populated container, while the GE version is |
4552 | flagged in symtable.c as a generator, so it returns the generator object |
4553 | when the function is called. |
4554 | |
4555 | Possible cleanups: |
4556 | - iterate over the generator sequence instead of using recursion |
4557 | */ |
4558 | |
4559 | |
4560 | static int |
4561 | compiler_comprehension_generator(struct compiler *c, |
4562 | asdl_comprehension_seq *generators, int gen_index, |
4563 | int depth, |
4564 | expr_ty elt, expr_ty val, int type) |
4565 | { |
4566 | comprehension_ty gen; |
4567 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
4568 | if (gen->is_async) { |
4569 | return compiler_async_comprehension_generator( |
4570 | c, generators, gen_index, depth, elt, val, type); |
4571 | } else { |
4572 | return compiler_sync_comprehension_generator( |
4573 | c, generators, gen_index, depth, elt, val, type); |
4574 | } |
4575 | } |
4576 | |
4577 | static int |
4578 | compiler_sync_comprehension_generator(struct compiler *c, |
4579 | asdl_comprehension_seq *generators, int gen_index, |
4580 | int depth, |
4581 | expr_ty elt, expr_ty val, int type) |
4582 | { |
4583 | /* generate code for the iterator, then each of the ifs, |
4584 | and then write to the element */ |
4585 | |
4586 | comprehension_ty gen; |
4587 | basicblock *start, *anchor, *skip, *if_cleanup; |
4588 | Py_ssize_t i, n; |
4589 | |
4590 | start = compiler_new_block(c); |
4591 | skip = compiler_new_block(c); |
4592 | if_cleanup = compiler_new_block(c); |
4593 | anchor = compiler_new_block(c); |
4594 | |
4595 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
4596 | anchor == NULL) |
4597 | return 0; |
4598 | |
4599 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
4600 | |
4601 | if (gen_index == 0) { |
4602 | /* Receive outermost iter as an implicit argument */ |
4603 | c->u->u_argcount = 1; |
4604 | ADDOP_I(c, LOAD_FAST, 0); |
4605 | } |
4606 | else { |
4607 | /* Sub-iter - calculate on the fly */ |
4608 | /* Fast path for the temporary variable assignment idiom: |
4609 | for y in [f(x)] |
4610 | */ |
4611 | asdl_expr_seq *elts; |
4612 | switch (gen->iter->kind) { |
4613 | case List_kind: |
4614 | elts = gen->iter->v.List.elts; |
4615 | break; |
4616 | case Tuple_kind: |
4617 | elts = gen->iter->v.Tuple.elts; |
4618 | break; |
4619 | default: |
4620 | elts = NULL; |
4621 | } |
4622 | if (asdl_seq_LEN(elts) == 1) { |
4623 | expr_ty elt = asdl_seq_GET(elts, 0); |
4624 | if (elt->kind != Starred_kind) { |
4625 | VISIT(c, expr, elt); |
4626 | start = NULL; |
4627 | } |
4628 | } |
4629 | if (start) { |
4630 | VISIT(c, expr, gen->iter); |
4631 | ADDOP(c, GET_ITER); |
4632 | } |
4633 | } |
4634 | if (start) { |
4635 | depth++; |
4636 | compiler_use_next_block(c, start); |
4637 | ADDOP_JUMP(c, FOR_ITER, anchor); |
4638 | NEXT_BLOCK(c); |
4639 | } |
4640 | VISIT(c, expr, gen->target); |
4641 | |
4642 | /* XXX this needs to be cleaned up...a lot! */ |
4643 | n = asdl_seq_LEN(gen->ifs); |
4644 | for (i = 0; i < n; i++) { |
4645 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
4646 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
4647 | return 0; |
4648 | NEXT_BLOCK(c); |
4649 | } |
4650 | |
4651 | if (++gen_index < asdl_seq_LEN(generators)) |
4652 | if (!compiler_comprehension_generator(c, |
4653 | generators, gen_index, depth, |
4654 | elt, val, type)) |
4655 | return 0; |
4656 | |
4657 | /* only append after the last for generator */ |
4658 | if (gen_index >= asdl_seq_LEN(generators)) { |
4659 | /* comprehension specific code */ |
4660 | switch (type) { |
4661 | case COMP_GENEXP: |
4662 | VISIT(c, expr, elt); |
4663 | ADDOP(c, YIELD_VALUE); |
4664 | ADDOP(c, POP_TOP); |
4665 | break; |
4666 | case COMP_LISTCOMP: |
4667 | VISIT(c, expr, elt); |
4668 | ADDOP_I(c, LIST_APPEND, depth + 1); |
4669 | break; |
4670 | case COMP_SETCOMP: |
4671 | VISIT(c, expr, elt); |
4672 | ADDOP_I(c, SET_ADD, depth + 1); |
4673 | break; |
4674 | case COMP_DICTCOMP: |
4675 | /* With '{k: v}', k is evaluated before v, so we do |
4676 | the same. */ |
4677 | VISIT(c, expr, elt); |
4678 | VISIT(c, expr, val); |
4679 | ADDOP_I(c, MAP_ADD, depth + 1); |
4680 | break; |
4681 | default: |
4682 | return 0; |
4683 | } |
4684 | |
4685 | compiler_use_next_block(c, skip); |
4686 | } |
4687 | compiler_use_next_block(c, if_cleanup); |
4688 | if (start) { |
4689 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
4690 | compiler_use_next_block(c, anchor); |
4691 | } |
4692 | |
4693 | return 1; |
4694 | } |
4695 | |
4696 | static int |
4697 | compiler_async_comprehension_generator(struct compiler *c, |
4698 | asdl_comprehension_seq *generators, int gen_index, |
4699 | int depth, |
4700 | expr_ty elt, expr_ty val, int type) |
4701 | { |
4702 | comprehension_ty gen; |
4703 | basicblock *start, *if_cleanup, *except; |
4704 | Py_ssize_t i, n; |
4705 | start = compiler_new_block(c); |
4706 | except = compiler_new_block(c); |
4707 | if_cleanup = compiler_new_block(c); |
4708 | |
4709 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
4710 | return 0; |
4711 | } |
4712 | |
4713 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
4714 | |
4715 | if (gen_index == 0) { |
4716 | /* Receive outermost iter as an implicit argument */ |
4717 | c->u->u_argcount = 1; |
4718 | ADDOP_I(c, LOAD_FAST, 0); |
4719 | } |
4720 | else { |
4721 | /* Sub-iter - calculate on the fly */ |
4722 | VISIT(c, expr, gen->iter); |
4723 | ADDOP(c, GET_AITER); |
4724 | } |
4725 | |
4726 | compiler_use_next_block(c, start); |
4727 | /* Runtime will push a block here, so we need to account for that */ |
4728 | if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start, |
4729 | NULL, NULL)) { |
4730 | return 0; |
4731 | } |
4732 | |
4733 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
4734 | ADDOP(c, GET_ANEXT); |
4735 | ADDOP_LOAD_CONST(c, Py_None); |
4736 | ADDOP(c, YIELD_FROM); |
4737 | ADDOP(c, POP_BLOCK); |
4738 | VISIT(c, expr, gen->target); |
4739 | |
4740 | n = asdl_seq_LEN(gen->ifs); |
4741 | for (i = 0; i < n; i++) { |
4742 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
4743 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
4744 | return 0; |
4745 | NEXT_BLOCK(c); |
4746 | } |
4747 | |
4748 | depth++; |
4749 | if (++gen_index < asdl_seq_LEN(generators)) |
4750 | if (!compiler_comprehension_generator(c, |
4751 | generators, gen_index, depth, |
4752 | elt, val, type)) |
4753 | return 0; |
4754 | |
4755 | /* only append after the last for generator */ |
4756 | if (gen_index >= asdl_seq_LEN(generators)) { |
4757 | /* comprehension specific code */ |
4758 | switch (type) { |
4759 | case COMP_GENEXP: |
4760 | VISIT(c, expr, elt); |
4761 | ADDOP(c, YIELD_VALUE); |
4762 | ADDOP(c, POP_TOP); |
4763 | break; |
4764 | case COMP_LISTCOMP: |
4765 | VISIT(c, expr, elt); |
4766 | ADDOP_I(c, LIST_APPEND, depth + 1); |
4767 | break; |
4768 | case COMP_SETCOMP: |
4769 | VISIT(c, expr, elt); |
4770 | ADDOP_I(c, SET_ADD, depth + 1); |
4771 | break; |
4772 | case COMP_DICTCOMP: |
4773 | /* With '{k: v}', k is evaluated before v, so we do |
4774 | the same. */ |
4775 | VISIT(c, expr, elt); |
4776 | VISIT(c, expr, val); |
4777 | ADDOP_I(c, MAP_ADD, depth + 1); |
4778 | break; |
4779 | default: |
4780 | return 0; |
4781 | } |
4782 | } |
4783 | compiler_use_next_block(c, if_cleanup); |
4784 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
4785 | |
4786 | compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start); |
4787 | |
4788 | compiler_use_next_block(c, except); |
4789 | ADDOP(c, END_ASYNC_FOR); |
4790 | |
4791 | return 1; |
4792 | } |
4793 | |
4794 | static int |
4795 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
4796 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
4797 | expr_ty val) |
4798 | { |
4799 | PyCodeObject *co = NULL; |
4800 | comprehension_ty outermost; |
4801 | PyObject *qualname = NULL; |
4802 | int is_async_generator = 0; |
4803 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
4804 | |
4805 | |
4806 | int is_async_function = c->u->u_ste->ste_coroutine; |
4807 | |
4808 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
4809 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
4810 | (void *)e, e->lineno)) |
4811 | { |
4812 | goto error; |
4813 | } |
4814 | SET_LOC(c, e); |
4815 | |
4816 | is_async_generator = c->u->u_ste->ste_coroutine; |
4817 | |
4818 | if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) { |
4819 | compiler_error(c, "asynchronous comprehension outside of " |
4820 | "an asynchronous function" ); |
4821 | goto error_in_scope; |
4822 | } |
4823 | |
4824 | if (type != COMP_GENEXP) { |
4825 | int op; |
4826 | switch (type) { |
4827 | case COMP_LISTCOMP: |
4828 | op = BUILD_LIST; |
4829 | break; |
4830 | case COMP_SETCOMP: |
4831 | op = BUILD_SET; |
4832 | break; |
4833 | case COMP_DICTCOMP: |
4834 | op = BUILD_MAP; |
4835 | break; |
4836 | default: |
4837 | PyErr_Format(PyExc_SystemError, |
4838 | "unknown comprehension type %d" , type); |
4839 | goto error_in_scope; |
4840 | } |
4841 | |
4842 | ADDOP_I(c, op, 0); |
4843 | } |
4844 | |
4845 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
4846 | val, type)) |
4847 | goto error_in_scope; |
4848 | |
4849 | if (type != COMP_GENEXP) { |
4850 | ADDOP(c, RETURN_VALUE); |
4851 | } |
4852 | |
4853 | co = assemble(c, 1); |
4854 | qualname = c->u->u_qualname; |
4855 | Py_INCREF(qualname); |
4856 | compiler_exit_scope(c); |
4857 | if (top_level_await && is_async_generator){ |
4858 | c->u->u_ste->ste_coroutine = 1; |
4859 | } |
4860 | if (co == NULL) |
4861 | goto error; |
4862 | |
4863 | if (!compiler_make_closure(c, co, 0, qualname)) { |
4864 | goto error; |
4865 | } |
4866 | Py_DECREF(qualname); |
4867 | Py_DECREF(co); |
4868 | |
4869 | VISIT(c, expr, outermost->iter); |
4870 | |
4871 | if (outermost->is_async) { |
4872 | ADDOP(c, GET_AITER); |
4873 | } else { |
4874 | ADDOP(c, GET_ITER); |
4875 | } |
4876 | |
4877 | ADDOP_I(c, CALL_FUNCTION, 1); |
4878 | |
4879 | if (is_async_generator && type != COMP_GENEXP) { |
4880 | ADDOP(c, GET_AWAITABLE); |
4881 | ADDOP_LOAD_CONST(c, Py_None); |
4882 | ADDOP(c, YIELD_FROM); |
4883 | } |
4884 | |
4885 | return 1; |
4886 | error_in_scope: |
4887 | compiler_exit_scope(c); |
4888 | error: |
4889 | Py_XDECREF(qualname); |
4890 | Py_XDECREF(co); |
4891 | return 0; |
4892 | } |
4893 | |
4894 | static int |
4895 | compiler_genexp(struct compiler *c, expr_ty e) |
4896 | { |
4897 | static identifier name; |
4898 | if (!name) { |
4899 | name = PyUnicode_InternFromString("<genexpr>" ); |
4900 | if (!name) |
4901 | return 0; |
4902 | } |
4903 | assert(e->kind == GeneratorExp_kind); |
4904 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
4905 | e->v.GeneratorExp.generators, |
4906 | e->v.GeneratorExp.elt, NULL); |
4907 | } |
4908 | |
4909 | static int |
4910 | compiler_listcomp(struct compiler *c, expr_ty e) |
4911 | { |
4912 | static identifier name; |
4913 | if (!name) { |
4914 | name = PyUnicode_InternFromString("<listcomp>" ); |
4915 | if (!name) |
4916 | return 0; |
4917 | } |
4918 | assert(e->kind == ListComp_kind); |
4919 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
4920 | e->v.ListComp.generators, |
4921 | e->v.ListComp.elt, NULL); |
4922 | } |
4923 | |
4924 | static int |
4925 | compiler_setcomp(struct compiler *c, expr_ty e) |
4926 | { |
4927 | static identifier name; |
4928 | if (!name) { |
4929 | name = PyUnicode_InternFromString("<setcomp>" ); |
4930 | if (!name) |
4931 | return 0; |
4932 | } |
4933 | assert(e->kind == SetComp_kind); |
4934 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
4935 | e->v.SetComp.generators, |
4936 | e->v.SetComp.elt, NULL); |
4937 | } |
4938 | |
4939 | |
4940 | static int |
4941 | compiler_dictcomp(struct compiler *c, expr_ty e) |
4942 | { |
4943 | static identifier name; |
4944 | if (!name) { |
4945 | name = PyUnicode_InternFromString("<dictcomp>" ); |
4946 | if (!name) |
4947 | return 0; |
4948 | } |
4949 | assert(e->kind == DictComp_kind); |
4950 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
4951 | e->v.DictComp.generators, |
4952 | e->v.DictComp.key, e->v.DictComp.value); |
4953 | } |
4954 | |
4955 | |
4956 | static int |
4957 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
4958 | { |
4959 | VISIT(c, expr, k->value); |
4960 | return 1; |
4961 | } |
4962 | |
4963 | /* Test whether expression is constant. For constants, report |
4964 | whether they are true or false. |
4965 | |
4966 | Return values: 1 for true, 0 for false, -1 for non-constant. |
4967 | */ |
4968 | |
4969 | static int |
4970 | compiler_with_except_finish(struct compiler *c) { |
4971 | basicblock *exit; |
4972 | exit = compiler_new_block(c); |
4973 | if (exit == NULL) |
4974 | return 0; |
4975 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
4976 | NEXT_BLOCK(c); |
4977 | ADDOP_I(c, RERAISE, 1); |
4978 | compiler_use_next_block(c, exit); |
4979 | ADDOP(c, POP_TOP); |
4980 | ADDOP(c, POP_TOP); |
4981 | ADDOP(c, POP_TOP); |
4982 | ADDOP(c, POP_EXCEPT); |
4983 | ADDOP(c, POP_TOP); |
4984 | return 1; |
4985 | } |
4986 | |
4987 | /* |
4988 | Implements the async with statement. |
4989 | |
4990 | The semantics outlined in that PEP are as follows: |
4991 | |
4992 | async with EXPR as VAR: |
4993 | BLOCK |
4994 | |
4995 | It is implemented roughly as: |
4996 | |
4997 | context = EXPR |
4998 | exit = context.__aexit__ # not calling it |
4999 | value = await context.__aenter__() |
5000 | try: |
5001 | VAR = value # if VAR present in the syntax |
5002 | BLOCK |
5003 | finally: |
5004 | if an exception was raised: |
5005 | exc = copy of (exception, instance, traceback) |
5006 | else: |
5007 | exc = (None, None, None) |
5008 | if not (await exit(*exc)): |
5009 | raise |
5010 | */ |
5011 | static int |
5012 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
5013 | { |
5014 | basicblock *block, *final, *exit; |
5015 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
5016 | |
5017 | assert(s->kind == AsyncWith_kind); |
5018 | if (IS_TOP_LEVEL_AWAIT(c)){ |
5019 | c->u->u_ste->ste_coroutine = 1; |
5020 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
5021 | return compiler_error(c, "'async with' outside async function" ); |
5022 | } |
5023 | |
5024 | block = compiler_new_block(c); |
5025 | final = compiler_new_block(c); |
5026 | exit = compiler_new_block(c); |
5027 | if (!block || !final || !exit) |
5028 | return 0; |
5029 | |
5030 | /* Evaluate EXPR */ |
5031 | VISIT(c, expr, item->context_expr); |
5032 | |
5033 | ADDOP(c, BEFORE_ASYNC_WITH); |
5034 | ADDOP(c, GET_AWAITABLE); |
5035 | ADDOP_LOAD_CONST(c, Py_None); |
5036 | ADDOP(c, YIELD_FROM); |
5037 | |
5038 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
5039 | |
5040 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
5041 | compiler_use_next_block(c, block); |
5042 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) { |
5043 | return 0; |
5044 | } |
5045 | |
5046 | if (item->optional_vars) { |
5047 | VISIT(c, expr, item->optional_vars); |
5048 | } |
5049 | else { |
5050 | /* Discard result from context.__aenter__() */ |
5051 | ADDOP(c, POP_TOP); |
5052 | } |
5053 | |
5054 | pos++; |
5055 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
5056 | /* BLOCK code */ |
5057 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
5058 | else if (!compiler_async_with(c, s, pos)) |
5059 | return 0; |
5060 | |
5061 | compiler_pop_fblock(c, ASYNC_WITH, block); |
5062 | ADDOP(c, POP_BLOCK); |
5063 | /* End of body; start the cleanup */ |
5064 | |
5065 | /* For successful outcome: |
5066 | * call __exit__(None, None, None) |
5067 | */ |
5068 | SET_LOC(c, s); |
5069 | if(!compiler_call_exit_with_nones(c)) |
5070 | return 0; |
5071 | ADDOP(c, GET_AWAITABLE); |
5072 | ADDOP_LOAD_CONST(c, Py_None); |
5073 | ADDOP(c, YIELD_FROM); |
5074 | |
5075 | ADDOP(c, POP_TOP); |
5076 | |
5077 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
5078 | |
5079 | /* For exceptional outcome: */ |
5080 | compiler_use_next_block(c, final); |
5081 | ADDOP(c, WITH_EXCEPT_START); |
5082 | ADDOP(c, GET_AWAITABLE); |
5083 | ADDOP_LOAD_CONST(c, Py_None); |
5084 | ADDOP(c, YIELD_FROM); |
5085 | compiler_with_except_finish(c); |
5086 | |
5087 | compiler_use_next_block(c, exit); |
5088 | return 1; |
5089 | } |
5090 | |
5091 | |
5092 | /* |
5093 | Implements the with statement from PEP 343. |
5094 | with EXPR as VAR: |
5095 | BLOCK |
5096 | is implemented as: |
5097 | <code for EXPR> |
5098 | SETUP_WITH E |
5099 | <code to store to VAR> or POP_TOP |
5100 | <code for BLOCK> |
5101 | LOAD_CONST (None, None, None) |
5102 | CALL_FUNCTION_EX 0 |
5103 | JUMP_FORWARD EXIT |
5104 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
5105 | POP_JUMP_IF_TRUE T: |
5106 | RERAISE |
5107 | T: POP_TOP * 3 (remove exception from stack) |
5108 | POP_EXCEPT |
5109 | POP_TOP |
5110 | EXIT: |
5111 | */ |
5112 | |
5113 | static int |
5114 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
5115 | { |
5116 | basicblock *block, *final, *exit; |
5117 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
5118 | |
5119 | assert(s->kind == With_kind); |
5120 | |
5121 | block = compiler_new_block(c); |
5122 | final = compiler_new_block(c); |
5123 | exit = compiler_new_block(c); |
5124 | if (!block || !final || !exit) |
5125 | return 0; |
5126 | |
5127 | /* Evaluate EXPR */ |
5128 | VISIT(c, expr, item->context_expr); |
5129 | /* Will push bound __exit__ */ |
5130 | ADDOP_JUMP(c, SETUP_WITH, final); |
5131 | |
5132 | /* SETUP_WITH pushes a finally block. */ |
5133 | compiler_use_next_block(c, block); |
5134 | if (!compiler_push_fblock(c, WITH, block, final, s)) { |
5135 | return 0; |
5136 | } |
5137 | |
5138 | if (item->optional_vars) { |
5139 | VISIT(c, expr, item->optional_vars); |
5140 | } |
5141 | else { |
5142 | /* Discard result from context.__enter__() */ |
5143 | ADDOP(c, POP_TOP); |
5144 | } |
5145 | |
5146 | pos++; |
5147 | if (pos == asdl_seq_LEN(s->v.With.items)) |
5148 | /* BLOCK code */ |
5149 | VISIT_SEQ(c, stmt, s->v.With.body) |
5150 | else if (!compiler_with(c, s, pos)) |
5151 | return 0; |
5152 | |
5153 | |
5154 | /* Mark all following code as artificial */ |
5155 | c->u->u_lineno = -1; |
5156 | ADDOP(c, POP_BLOCK); |
5157 | compiler_pop_fblock(c, WITH, block); |
5158 | |
5159 | /* End of body; start the cleanup. */ |
5160 | |
5161 | /* For successful outcome: |
5162 | * call __exit__(None, None, None) |
5163 | */ |
5164 | SET_LOC(c, s); |
5165 | if (!compiler_call_exit_with_nones(c)) |
5166 | return 0; |
5167 | ADDOP(c, POP_TOP); |
5168 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
5169 | |
5170 | /* For exceptional outcome: */ |
5171 | compiler_use_next_block(c, final); |
5172 | ADDOP(c, WITH_EXCEPT_START); |
5173 | compiler_with_except_finish(c); |
5174 | |
5175 | compiler_use_next_block(c, exit); |
5176 | return 1; |
5177 | } |
5178 | |
5179 | static int |
5180 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
5181 | { |
5182 | switch (e->kind) { |
5183 | case NamedExpr_kind: |
5184 | VISIT(c, expr, e->v.NamedExpr.value); |
5185 | ADDOP(c, DUP_TOP); |
5186 | VISIT(c, expr, e->v.NamedExpr.target); |
5187 | break; |
5188 | case BoolOp_kind: |
5189 | return compiler_boolop(c, e); |
5190 | case BinOp_kind: |
5191 | VISIT(c, expr, e->v.BinOp.left); |
5192 | VISIT(c, expr, e->v.BinOp.right); |
5193 | ADDOP(c, binop(e->v.BinOp.op)); |
5194 | break; |
5195 | case UnaryOp_kind: |
5196 | VISIT(c, expr, e->v.UnaryOp.operand); |
5197 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
5198 | break; |
5199 | case Lambda_kind: |
5200 | return compiler_lambda(c, e); |
5201 | case IfExp_kind: |
5202 | return compiler_ifexp(c, e); |
5203 | case Dict_kind: |
5204 | return compiler_dict(c, e); |
5205 | case Set_kind: |
5206 | return compiler_set(c, e); |
5207 | case GeneratorExp_kind: |
5208 | return compiler_genexp(c, e); |
5209 | case ListComp_kind: |
5210 | return compiler_listcomp(c, e); |
5211 | case SetComp_kind: |
5212 | return compiler_setcomp(c, e); |
5213 | case DictComp_kind: |
5214 | return compiler_dictcomp(c, e); |
5215 | case Yield_kind: |
5216 | if (c->u->u_ste->ste_type != FunctionBlock) |
5217 | return compiler_error(c, "'yield' outside function" ); |
5218 | if (e->v.Yield.value) { |
5219 | VISIT(c, expr, e->v.Yield.value); |
5220 | } |
5221 | else { |
5222 | ADDOP_LOAD_CONST(c, Py_None); |
5223 | } |
5224 | ADDOP(c, YIELD_VALUE); |
5225 | break; |
5226 | case YieldFrom_kind: |
5227 | if (c->u->u_ste->ste_type != FunctionBlock) |
5228 | return compiler_error(c, "'yield' outside function" ); |
5229 | |
5230 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
5231 | return compiler_error(c, "'yield from' inside async function" ); |
5232 | |
5233 | VISIT(c, expr, e->v.YieldFrom.value); |
5234 | ADDOP(c, GET_YIELD_FROM_ITER); |
5235 | ADDOP_LOAD_CONST(c, Py_None); |
5236 | ADDOP(c, YIELD_FROM); |
5237 | break; |
5238 | case Await_kind: |
5239 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
5240 | if (c->u->u_ste->ste_type != FunctionBlock){ |
5241 | return compiler_error(c, "'await' outside function" ); |
5242 | } |
5243 | |
5244 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
5245 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
5246 | return compiler_error(c, "'await' outside async function" ); |
5247 | } |
5248 | } |
5249 | |
5250 | VISIT(c, expr, e->v.Await.value); |
5251 | ADDOP(c, GET_AWAITABLE); |
5252 | ADDOP_LOAD_CONST(c, Py_None); |
5253 | ADDOP(c, YIELD_FROM); |
5254 | break; |
5255 | case Compare_kind: |
5256 | return compiler_compare(c, e); |
5257 | case Call_kind: |
5258 | return compiler_call(c, e); |
5259 | case Constant_kind: |
5260 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
5261 | break; |
5262 | case JoinedStr_kind: |
5263 | return compiler_joined_str(c, e); |
5264 | case FormattedValue_kind: |
5265 | return compiler_formatted_value(c, e); |
5266 | /* The following exprs can be assignment targets. */ |
5267 | case Attribute_kind: |
5268 | VISIT(c, expr, e->v.Attribute.value); |
5269 | switch (e->v.Attribute.ctx) { |
5270 | case Load: |
5271 | { |
5272 | int old_lineno = c->u->u_lineno; |
5273 | c->u->u_lineno = e->end_lineno; |
5274 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
5275 | c->u->u_lineno = old_lineno; |
5276 | break; |
5277 | } |
5278 | case Store: |
5279 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) { |
5280 | return 0; |
5281 | } |
5282 | int old_lineno = c->u->u_lineno; |
5283 | c->u->u_lineno = e->end_lineno; |
5284 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
5285 | c->u->u_lineno = old_lineno; |
5286 | break; |
5287 | case Del: |
5288 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
5289 | break; |
5290 | } |
5291 | break; |
5292 | case Subscript_kind: |
5293 | return compiler_subscript(c, e); |
5294 | case Starred_kind: |
5295 | switch (e->v.Starred.ctx) { |
5296 | case Store: |
5297 | /* In all legitimate cases, the Starred node was already replaced |
5298 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
5299 | return compiler_error(c, |
5300 | "starred assignment target must be in a list or tuple" ); |
5301 | default: |
5302 | return compiler_error(c, |
5303 | "can't use starred expression here" ); |
5304 | } |
5305 | break; |
5306 | case Slice_kind: |
5307 | return compiler_slice(c, e); |
5308 | case Name_kind: |
5309 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
5310 | /* child nodes of List and Tuple will have expr_context set */ |
5311 | case List_kind: |
5312 | return compiler_list(c, e); |
5313 | case Tuple_kind: |
5314 | return compiler_tuple(c, e); |
5315 | } |
5316 | return 1; |
5317 | } |
5318 | |
5319 | static int |
5320 | compiler_visit_expr(struct compiler *c, expr_ty e) |
5321 | { |
5322 | int old_lineno = c->u->u_lineno; |
5323 | int old_end_lineno = c->u->u_end_lineno; |
5324 | int old_col_offset = c->u->u_col_offset; |
5325 | int old_end_col_offset = c->u->u_end_col_offset; |
5326 | SET_LOC(c, e); |
5327 | int res = compiler_visit_expr1(c, e); |
5328 | c->u->u_lineno = old_lineno; |
5329 | c->u->u_end_lineno = old_end_lineno; |
5330 | c->u->u_col_offset = old_col_offset; |
5331 | c->u->u_end_col_offset = old_end_col_offset; |
5332 | return res; |
5333 | } |
5334 | |
5335 | static int |
5336 | compiler_augassign(struct compiler *c, stmt_ty s) |
5337 | { |
5338 | assert(s->kind == AugAssign_kind); |
5339 | expr_ty e = s->v.AugAssign.target; |
5340 | |
5341 | int old_lineno = c->u->u_lineno; |
5342 | int old_end_lineno = c->u->u_end_lineno; |
5343 | int old_col_offset = c->u->u_col_offset; |
5344 | int old_end_col_offset = c->u->u_end_col_offset; |
5345 | SET_LOC(c, e); |
5346 | |
5347 | switch (e->kind) { |
5348 | case Attribute_kind: |
5349 | VISIT(c, expr, e->v.Attribute.value); |
5350 | ADDOP(c, DUP_TOP); |
5351 | int old_lineno = c->u->u_lineno; |
5352 | c->u->u_lineno = e->end_lineno; |
5353 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
5354 | c->u->u_lineno = old_lineno; |
5355 | break; |
5356 | case Subscript_kind: |
5357 | VISIT(c, expr, e->v.Subscript.value); |
5358 | VISIT(c, expr, e->v.Subscript.slice); |
5359 | ADDOP(c, DUP_TOP_TWO); |
5360 | ADDOP(c, BINARY_SUBSCR); |
5361 | break; |
5362 | case Name_kind: |
5363 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
5364 | return 0; |
5365 | break; |
5366 | default: |
5367 | PyErr_Format(PyExc_SystemError, |
5368 | "invalid node type (%d) for augmented assignment" , |
5369 | e->kind); |
5370 | return 0; |
5371 | } |
5372 | |
5373 | c->u->u_lineno = old_lineno; |
5374 | c->u->u_end_lineno = old_end_lineno; |
5375 | c->u->u_col_offset = old_col_offset; |
5376 | c->u->u_end_col_offset = old_end_col_offset; |
5377 | |
5378 | VISIT(c, expr, s->v.AugAssign.value); |
5379 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
5380 | |
5381 | SET_LOC(c, e); |
5382 | |
5383 | switch (e->kind) { |
5384 | case Attribute_kind: |
5385 | c->u->u_lineno = e->end_lineno; |
5386 | ADDOP(c, ROT_TWO); |
5387 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
5388 | break; |
5389 | case Subscript_kind: |
5390 | ADDOP(c, ROT_THREE); |
5391 | ADDOP(c, STORE_SUBSCR); |
5392 | break; |
5393 | case Name_kind: |
5394 | return compiler_nameop(c, e->v.Name.id, Store); |
5395 | default: |
5396 | Py_UNREACHABLE(); |
5397 | } |
5398 | return 1; |
5399 | } |
5400 | |
5401 | static int |
5402 | check_ann_expr(struct compiler *c, expr_ty e) |
5403 | { |
5404 | VISIT(c, expr, e); |
5405 | ADDOP(c, POP_TOP); |
5406 | return 1; |
5407 | } |
5408 | |
5409 | static int |
5410 | check_annotation(struct compiler *c, stmt_ty s) |
5411 | { |
5412 | /* Annotations of complex targets does not produce anything |
5413 | under annotations future */ |
5414 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
5415 | return 1; |
5416 | } |
5417 | |
5418 | /* Annotations are only evaluated in a module or class. */ |
5419 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
5420 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
5421 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
5422 | } |
5423 | return 1; |
5424 | } |
5425 | |
5426 | static int |
5427 | check_ann_subscr(struct compiler *c, expr_ty e) |
5428 | { |
5429 | /* We check that everything in a subscript is defined at runtime. */ |
5430 | switch (e->kind) { |
5431 | case Slice_kind: |
5432 | if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) { |
5433 | return 0; |
5434 | } |
5435 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
5436 | return 0; |
5437 | } |
5438 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
5439 | return 0; |
5440 | } |
5441 | return 1; |
5442 | case Tuple_kind: { |
5443 | /* extended slice */ |
5444 | asdl_expr_seq *elts = e->v.Tuple.elts; |
5445 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
5446 | for (i = 0; i < n; i++) { |
5447 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
5448 | return 0; |
5449 | } |
5450 | } |
5451 | return 1; |
5452 | } |
5453 | default: |
5454 | return check_ann_expr(c, e); |
5455 | } |
5456 | } |
5457 | |
5458 | static int |
5459 | compiler_annassign(struct compiler *c, stmt_ty s) |
5460 | { |
5461 | expr_ty targ = s->v.AnnAssign.target; |
5462 | PyObject* mangled; |
5463 | |
5464 | assert(s->kind == AnnAssign_kind); |
5465 | |
5466 | /* We perform the actual assignment first. */ |
5467 | if (s->v.AnnAssign.value) { |
5468 | VISIT(c, expr, s->v.AnnAssign.value); |
5469 | VISIT(c, expr, targ); |
5470 | } |
5471 | switch (targ->kind) { |
5472 | case Name_kind: |
5473 | if (forbidden_name(c, targ->v.Name.id, Store)) |
5474 | return 0; |
5475 | /* If we have a simple name in a module or class, store annotation. */ |
5476 | if (s->v.AnnAssign.simple && |
5477 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
5478 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
5479 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
5480 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
5481 | } |
5482 | else { |
5483 | VISIT(c, expr, s->v.AnnAssign.annotation); |
5484 | } |
5485 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
5486 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
5487 | ADDOP_LOAD_CONST_NEW(c, mangled); |
5488 | ADDOP(c, STORE_SUBSCR); |
5489 | } |
5490 | break; |
5491 | case Attribute_kind: |
5492 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
5493 | return 0; |
5494 | if (!s->v.AnnAssign.value && |
5495 | !check_ann_expr(c, targ->v.Attribute.value)) { |
5496 | return 0; |
5497 | } |
5498 | break; |
5499 | case Subscript_kind: |
5500 | if (!s->v.AnnAssign.value && |
5501 | (!check_ann_expr(c, targ->v.Subscript.value) || |
5502 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
5503 | return 0; |
5504 | } |
5505 | break; |
5506 | default: |
5507 | PyErr_Format(PyExc_SystemError, |
5508 | "invalid node type (%d) for annotated assignment" , |
5509 | targ->kind); |
5510 | return 0; |
5511 | } |
5512 | /* Annotation is evaluated last. */ |
5513 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
5514 | return 0; |
5515 | } |
5516 | return 1; |
5517 | } |
5518 | |
5519 | /* Raises a SyntaxError and returns 0. |
5520 | If something goes wrong, a different exception may be raised. |
5521 | */ |
5522 | |
5523 | static int |
5524 | compiler_error(struct compiler *c, const char *format, ...) |
5525 | { |
5526 | va_list vargs; |
5527 | #ifdef HAVE_STDARG_PROTOTYPES |
5528 | va_start(vargs, format); |
5529 | #else |
5530 | va_start(vargs); |
5531 | #endif |
5532 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
5533 | va_end(vargs); |
5534 | if (msg == NULL) { |
5535 | return 0; |
5536 | } |
5537 | PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
5538 | if (loc == NULL) { |
5539 | Py_INCREF(Py_None); |
5540 | loc = Py_None; |
5541 | } |
5542 | PyObject *args = Py_BuildValue("O(OiiOii)" , msg, c->c_filename, |
5543 | c->u->u_lineno, c->u->u_col_offset + 1, loc, |
5544 | c->u->u_end_lineno, c->u->u_end_col_offset + 1); |
5545 | Py_DECREF(msg); |
5546 | if (args == NULL) { |
5547 | goto exit; |
5548 | } |
5549 | PyErr_SetObject(PyExc_SyntaxError, args); |
5550 | exit: |
5551 | Py_DECREF(loc); |
5552 | Py_XDECREF(args); |
5553 | return 0; |
5554 | } |
5555 | |
5556 | /* Emits a SyntaxWarning and returns 1 on success. |
5557 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
5558 | and returns 0. |
5559 | */ |
5560 | static int |
5561 | compiler_warn(struct compiler *c, const char *format, ...) |
5562 | { |
5563 | va_list vargs; |
5564 | #ifdef HAVE_STDARG_PROTOTYPES |
5565 | va_start(vargs, format); |
5566 | #else |
5567 | va_start(vargs); |
5568 | #endif |
5569 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
5570 | va_end(vargs); |
5571 | if (msg == NULL) { |
5572 | return 0; |
5573 | } |
5574 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
5575 | c->u->u_lineno, NULL, NULL) < 0) |
5576 | { |
5577 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
5578 | /* Replace the SyntaxWarning exception with a SyntaxError |
5579 | to get a more accurate error report */ |
5580 | PyErr_Clear(); |
5581 | assert(PyUnicode_AsUTF8(msg) != NULL); |
5582 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
5583 | } |
5584 | Py_DECREF(msg); |
5585 | return 0; |
5586 | } |
5587 | Py_DECREF(msg); |
5588 | return 1; |
5589 | } |
5590 | |
5591 | static int |
5592 | compiler_subscript(struct compiler *c, expr_ty e) |
5593 | { |
5594 | expr_context_ty ctx = e->v.Subscript.ctx; |
5595 | int op = 0; |
5596 | |
5597 | if (ctx == Load) { |
5598 | if (!check_subscripter(c, e->v.Subscript.value)) { |
5599 | return 0; |
5600 | } |
5601 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
5602 | return 0; |
5603 | } |
5604 | } |
5605 | |
5606 | switch (ctx) { |
5607 | case Load: op = BINARY_SUBSCR; break; |
5608 | case Store: op = STORE_SUBSCR; break; |
5609 | case Del: op = DELETE_SUBSCR; break; |
5610 | } |
5611 | assert(op); |
5612 | VISIT(c, expr, e->v.Subscript.value); |
5613 | VISIT(c, expr, e->v.Subscript.slice); |
5614 | ADDOP(c, op); |
5615 | return 1; |
5616 | } |
5617 | |
5618 | static int |
5619 | compiler_slice(struct compiler *c, expr_ty s) |
5620 | { |
5621 | int n = 2; |
5622 | assert(s->kind == Slice_kind); |
5623 | |
5624 | /* only handles the cases where BUILD_SLICE is emitted */ |
5625 | if (s->v.Slice.lower) { |
5626 | VISIT(c, expr, s->v.Slice.lower); |
5627 | } |
5628 | else { |
5629 | ADDOP_LOAD_CONST(c, Py_None); |
5630 | } |
5631 | |
5632 | if (s->v.Slice.upper) { |
5633 | VISIT(c, expr, s->v.Slice.upper); |
5634 | } |
5635 | else { |
5636 | ADDOP_LOAD_CONST(c, Py_None); |
5637 | } |
5638 | |
5639 | if (s->v.Slice.step) { |
5640 | n++; |
5641 | VISIT(c, expr, s->v.Slice.step); |
5642 | } |
5643 | ADDOP_I(c, BUILD_SLICE, n); |
5644 | return 1; |
5645 | } |
5646 | |
5647 | |
5648 | // PEP 634: Structural Pattern Matching |
5649 | |
5650 | // To keep things simple, all compiler_pattern_* and pattern_helper_* routines |
5651 | // follow the convention of consuming TOS (the subject for the given pattern) |
5652 | // and calling jump_to_fail_pop on failure (no match). |
5653 | |
5654 | // When calling into these routines, it's important that pc->on_top be kept |
5655 | // updated to reflect the current number of items that we are using on the top |
5656 | // of the stack: they will be popped on failure, and any name captures will be |
5657 | // stored *underneath* them on success. This lets us defer all names stores |
5658 | // until the *entire* pattern matches. |
5659 | |
5660 | #define WILDCARD_CHECK(N) \ |
5661 | ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name) |
5662 | |
5663 | #define WILDCARD_STAR_CHECK(N) \ |
5664 | ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name) |
5665 | |
5666 | // Limit permitted subexpressions, even if the parser & AST validator let them through |
5667 | #define MATCH_VALUE_EXPR(N) \ |
5668 | ((N)->kind == Constant_kind || (N)->kind == Attribute_kind) |
5669 | |
5670 | // Allocate or resize pc->fail_pop to allow for n items to be popped on failure. |
5671 | static int |
5672 | ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n) |
5673 | { |
5674 | Py_ssize_t size = n + 1; |
5675 | if (size <= pc->fail_pop_size) { |
5676 | return 1; |
5677 | } |
5678 | Py_ssize_t needed = sizeof(basicblock*) * size; |
5679 | basicblock **resized = PyObject_Realloc(pc->fail_pop, needed); |
5680 | if (resized == NULL) { |
5681 | PyErr_NoMemory(); |
5682 | return 0; |
5683 | } |
5684 | pc->fail_pop = resized; |
5685 | while (pc->fail_pop_size < size) { |
5686 | basicblock *new_block; |
5687 | RETURN_IF_FALSE(new_block = compiler_new_block(c)); |
5688 | pc->fail_pop[pc->fail_pop_size++] = new_block; |
5689 | } |
5690 | return 1; |
5691 | } |
5692 | |
5693 | // Use op to jump to the correct fail_pop block. |
5694 | static int |
5695 | jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op) |
5696 | { |
5697 | // Pop any items on the top of the stack, plus any objects we were going to |
5698 | // capture on success: |
5699 | Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores); |
5700 | RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops)); |
5701 | ADDOP_JUMP(c, op, pc->fail_pop[pops]); |
5702 | NEXT_BLOCK(c); |
5703 | return 1; |
5704 | } |
5705 | |
5706 | // Build all of the fail_pop blocks and reset fail_pop. |
5707 | static int |
5708 | emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc) |
5709 | { |
5710 | if (!pc->fail_pop_size) { |
5711 | assert(pc->fail_pop == NULL); |
5712 | NEXT_BLOCK(c); |
5713 | return 1; |
5714 | } |
5715 | while (--pc->fail_pop_size) { |
5716 | compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]); |
5717 | if (!compiler_addop(c, POP_TOP)) { |
5718 | pc->fail_pop_size = 0; |
5719 | PyObject_Free(pc->fail_pop); |
5720 | pc->fail_pop = NULL; |
5721 | return 0; |
5722 | } |
5723 | } |
5724 | compiler_use_next_block(c, pc->fail_pop[0]); |
5725 | PyObject_Free(pc->fail_pop); |
5726 | pc->fail_pop = NULL; |
5727 | return 1; |
5728 | } |
5729 | |
5730 | static int |
5731 | compiler_error_duplicate_store(struct compiler *c, identifier n) |
5732 | { |
5733 | return compiler_error(c, "multiple assignments to name %R in pattern" , n); |
5734 | } |
5735 | |
5736 | static int |
5737 | pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) |
5738 | { |
5739 | if (n == NULL) { |
5740 | ADDOP(c, POP_TOP); |
5741 | return 1; |
5742 | } |
5743 | if (forbidden_name(c, n, Store)) { |
5744 | return 0; |
5745 | } |
5746 | // Can't assign to the same name twice: |
5747 | int duplicate = PySequence_Contains(pc->stores, n); |
5748 | if (duplicate < 0) { |
5749 | return 0; |
5750 | } |
5751 | if (duplicate) { |
5752 | return compiler_error_duplicate_store(c, n); |
5753 | } |
5754 | // Rotate this object underneath any items we need to preserve: |
5755 | ADDOP_I(c, ROT_N, pc->on_top + PyList_GET_SIZE(pc->stores) + 1); |
5756 | return !PyList_Append(pc->stores, n); |
5757 | } |
5758 | |
5759 | |
5760 | static int |
5761 | pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts) |
5762 | { |
5763 | Py_ssize_t n = asdl_seq_LEN(elts); |
5764 | int seen_star = 0; |
5765 | for (Py_ssize_t i = 0; i < n; i++) { |
5766 | pattern_ty elt = asdl_seq_GET(elts, i); |
5767 | if (elt->kind == MatchStar_kind && !seen_star) { |
5768 | if ((i >= (1 << 8)) || |
5769 | (n-i-1 >= (INT_MAX >> 8))) |
5770 | return compiler_error(c, |
5771 | "too many expressions in " |
5772 | "star-unpacking sequence pattern" ); |
5773 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
5774 | seen_star = 1; |
5775 | } |
5776 | else if (elt->kind == MatchStar_kind) { |
5777 | return compiler_error(c, |
5778 | "multiple starred expressions in sequence pattern" ); |
5779 | } |
5780 | } |
5781 | if (!seen_star) { |
5782 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
5783 | } |
5784 | return 1; |
5785 | } |
5786 | |
5787 | static int |
5788 | pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns, |
5789 | Py_ssize_t star, pattern_context *pc) |
5790 | { |
5791 | RETURN_IF_FALSE(pattern_unpack_helper(c, patterns)); |
5792 | Py_ssize_t size = asdl_seq_LEN(patterns); |
5793 | // We've now got a bunch of new subjects on the stack. They need to remain |
5794 | // there after each subpattern match: |
5795 | pc->on_top += size; |
5796 | for (Py_ssize_t i = 0; i < size; i++) { |
5797 | // One less item to keep track of each time we loop through: |
5798 | pc->on_top--; |
5799 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
5800 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
5801 | } |
5802 | return 1; |
5803 | } |
5804 | |
5805 | // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of |
5806 | // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a |
5807 | // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc. |
5808 | static int |
5809 | pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns, |
5810 | Py_ssize_t star, pattern_context *pc) |
5811 | { |
5812 | // We need to keep the subject around for extracting elements: |
5813 | pc->on_top++; |
5814 | Py_ssize_t size = asdl_seq_LEN(patterns); |
5815 | for (Py_ssize_t i = 0; i < size; i++) { |
5816 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
5817 | if (WILDCARD_CHECK(pattern)) { |
5818 | continue; |
5819 | } |
5820 | if (i == star) { |
5821 | assert(WILDCARD_STAR_CHECK(pattern)); |
5822 | continue; |
5823 | } |
5824 | ADDOP(c, DUP_TOP); |
5825 | if (i < star) { |
5826 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
5827 | } |
5828 | else { |
5829 | // The subject may not support negative indexing! Compute a |
5830 | // nonnegative index: |
5831 | ADDOP(c, GET_LEN); |
5832 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i)); |
5833 | ADDOP(c, BINARY_SUBTRACT); |
5834 | } |
5835 | ADDOP(c, BINARY_SUBSCR); |
5836 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
5837 | } |
5838 | // Pop the subject, we're done with it: |
5839 | pc->on_top--; |
5840 | ADDOP(c, POP_TOP); |
5841 | return 1; |
5842 | } |
5843 | |
5844 | // Like compiler_pattern, but turn off checks for irrefutability. |
5845 | static int |
5846 | compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc) |
5847 | { |
5848 | int allow_irrefutable = pc->allow_irrefutable; |
5849 | pc->allow_irrefutable = 1; |
5850 | RETURN_IF_FALSE(compiler_pattern(c, p, pc)); |
5851 | pc->allow_irrefutable = allow_irrefutable; |
5852 | return 1; |
5853 | } |
5854 | |
5855 | static int |
5856 | compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) |
5857 | { |
5858 | assert(p->kind == MatchAs_kind); |
5859 | if (p->v.MatchAs.pattern == NULL) { |
5860 | // An irrefutable match: |
5861 | if (!pc->allow_irrefutable) { |
5862 | if (p->v.MatchAs.name) { |
5863 | const char *e = "name capture %R makes remaining patterns unreachable" ; |
5864 | return compiler_error(c, e, p->v.MatchAs.name); |
5865 | } |
5866 | const char *e = "wildcard makes remaining patterns unreachable" ; |
5867 | return compiler_error(c, e); |
5868 | } |
5869 | return pattern_helper_store_name(c, p->v.MatchAs.name, pc); |
5870 | } |
5871 | // Need to make a copy for (possibly) storing later: |
5872 | pc->on_top++; |
5873 | ADDOP(c, DUP_TOP); |
5874 | RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); |
5875 | // Success! Store it: |
5876 | pc->on_top--; |
5877 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); |
5878 | return 1; |
5879 | } |
5880 | |
5881 | static int |
5882 | compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc) |
5883 | { |
5884 | assert(p->kind == MatchStar_kind); |
5885 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc)); |
5886 | return 1; |
5887 | } |
5888 | |
5889 | static int |
5890 | validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns) |
5891 | { |
5892 | // Any errors will point to the pattern rather than the arg name as the |
5893 | // parser is only supplying identifiers rather than Name or keyword nodes |
5894 | Py_ssize_t nattrs = asdl_seq_LEN(attrs); |
5895 | for (Py_ssize_t i = 0; i < nattrs; i++) { |
5896 | identifier attr = ((identifier)asdl_seq_GET(attrs, i)); |
5897 | SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i))); |
5898 | if (forbidden_name(c, attr, Store)) { |
5899 | return -1; |
5900 | } |
5901 | for (Py_ssize_t j = i + 1; j < nattrs; j++) { |
5902 | identifier other = ((identifier)asdl_seq_GET(attrs, j)); |
5903 | if (!PyUnicode_Compare(attr, other)) { |
5904 | SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j))); |
5905 | compiler_error(c, "attribute name repeated in class pattern: %U" , attr); |
5906 | return -1; |
5907 | } |
5908 | } |
5909 | } |
5910 | return 0; |
5911 | } |
5912 | |
5913 | static int |
5914 | compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) |
5915 | { |
5916 | assert(p->kind == MatchClass_kind); |
5917 | asdl_pattern_seq *patterns = p->v.MatchClass.patterns; |
5918 | asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs; |
5919 | asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns; |
5920 | Py_ssize_t nargs = asdl_seq_LEN(patterns); |
5921 | Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs); |
5922 | Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns); |
5923 | if (nattrs != nkwd_patterns) { |
5924 | // AST validator shouldn't let this happen, but if it does, |
5925 | // just fail, don't crash out of the interpreter |
5926 | const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern" ; |
5927 | return compiler_error(c, e, nattrs, nkwd_patterns); |
5928 | } |
5929 | if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) { |
5930 | const char *e = "too many sub-patterns in class pattern %R" ; |
5931 | return compiler_error(c, e, p->v.MatchClass.cls); |
5932 | } |
5933 | if (nattrs) { |
5934 | RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns)); |
5935 | SET_LOC(c, p); |
5936 | } |
5937 | VISIT(c, expr, p->v.MatchClass.cls); |
5938 | PyObject *attr_names; |
5939 | RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs)); |
5940 | Py_ssize_t i; |
5941 | for (i = 0; i < nattrs; i++) { |
5942 | PyObject *name = asdl_seq_GET(kwd_attrs, i); |
5943 | Py_INCREF(name); |
5944 | PyTuple_SET_ITEM(attr_names, i, name); |
5945 | } |
5946 | ADDOP_LOAD_CONST_NEW(c, attr_names); |
5947 | ADDOP_I(c, MATCH_CLASS, nargs); |
5948 | // TOS is now a tuple of (nargs + nattrs) attributes. Preserve it: |
5949 | pc->on_top++; |
5950 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
5951 | for (i = 0; i < nargs + nattrs; i++) { |
5952 | pattern_ty pattern; |
5953 | if (i < nargs) { |
5954 | // Positional: |
5955 | pattern = asdl_seq_GET(patterns, i); |
5956 | } |
5957 | else { |
5958 | // Keyword: |
5959 | pattern = asdl_seq_GET(kwd_patterns, i - nargs); |
5960 | } |
5961 | if (WILDCARD_CHECK(pattern)) { |
5962 | continue; |
5963 | } |
5964 | // Get the i-th attribute, and match it against the i-th pattern: |
5965 | ADDOP(c, DUP_TOP); |
5966 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
5967 | ADDOP(c, BINARY_SUBSCR); |
5968 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
5969 | } |
5970 | // Success! Pop the tuple of attributes: |
5971 | pc->on_top--; |
5972 | ADDOP(c, POP_TOP); |
5973 | return 1; |
5974 | } |
5975 | |
5976 | static int |
5977 | compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc) |
5978 | { |
5979 | assert(p->kind == MatchMapping_kind); |
5980 | asdl_expr_seq *keys = p->v.MatchMapping.keys; |
5981 | asdl_pattern_seq *patterns = p->v.MatchMapping.patterns; |
5982 | Py_ssize_t size = asdl_seq_LEN(keys); |
5983 | Py_ssize_t npatterns = asdl_seq_LEN(patterns); |
5984 | if (size != npatterns) { |
5985 | // AST validator shouldn't let this happen, but if it does, |
5986 | // just fail, don't crash out of the interpreter |
5987 | const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern" ; |
5988 | return compiler_error(c, e, size, npatterns); |
5989 | } |
5990 | // We have a double-star target if "rest" is set |
5991 | PyObject *star_target = p->v.MatchMapping.rest; |
5992 | // We need to keep the subject on top during the mapping and length checks: |
5993 | pc->on_top++; |
5994 | ADDOP(c, MATCH_MAPPING); |
5995 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
5996 | if (!size && !star_target) { |
5997 | // If the pattern is just "{}", we're done! Pop the subject: |
5998 | pc->on_top--; |
5999 | ADDOP(c, POP_TOP); |
6000 | return 1; |
6001 | } |
6002 | if (size) { |
6003 | // If the pattern has any keys in it, perform a length check: |
6004 | ADDOP(c, GET_LEN); |
6005 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
6006 | ADDOP_COMPARE(c, GtE); |
6007 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
6008 | } |
6009 | if (INT_MAX < size - 1) { |
6010 | return compiler_error(c, "too many sub-patterns in mapping pattern" ); |
6011 | } |
6012 | // Collect all of the keys into a tuple for MATCH_KEYS and |
6013 | // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals: |
6014 | |
6015 | // Maintaining a set of Constant_kind kind keys allows us to raise a |
6016 | // SyntaxError in the case of duplicates. |
6017 | PyObject *seen = PySet_New(NULL); |
6018 | if (seen == NULL) { |
6019 | return 0; |
6020 | } |
6021 | |
6022 | // NOTE: goto error on failure in the loop below to avoid leaking `seen` |
6023 | for (Py_ssize_t i = 0; i < size; i++) { |
6024 | expr_ty key = asdl_seq_GET(keys, i); |
6025 | if (key == NULL) { |
6026 | const char *e = "can't use NULL keys in MatchMapping " |
6027 | "(set 'rest' parameter instead)" ; |
6028 | SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i))); |
6029 | compiler_error(c, e); |
6030 | goto error; |
6031 | } |
6032 | |
6033 | if (key->kind == Constant_kind) { |
6034 | int in_seen = PySet_Contains(seen, key->v.Constant.value); |
6035 | if (in_seen < 0) { |
6036 | goto error; |
6037 | } |
6038 | if (in_seen) { |
6039 | const char *e = "mapping pattern checks duplicate key (%R)" ; |
6040 | compiler_error(c, e, key->v.Constant.value); |
6041 | goto error; |
6042 | } |
6043 | if (PySet_Add(seen, key->v.Constant.value)) { |
6044 | goto error; |
6045 | } |
6046 | } |
6047 | |
6048 | else if (key->kind != Attribute_kind) { |
6049 | const char *e = "mapping pattern keys may only match literals and attribute lookups" ; |
6050 | compiler_error(c, e); |
6051 | goto error; |
6052 | } |
6053 | if (!compiler_visit_expr(c, key)) { |
6054 | goto error; |
6055 | } |
6056 | } |
6057 | |
6058 | // all keys have been checked; there are no duplicates |
6059 | Py_DECREF(seen); |
6060 | |
6061 | ADDOP_I(c, BUILD_TUPLE, size); |
6062 | ADDOP(c, MATCH_KEYS); |
6063 | // There's now a tuple of keys and a tuple of values on top of the subject: |
6064 | pc->on_top += 2; |
6065 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
6066 | // So far so good. Use that tuple of values on the stack to match |
6067 | // sub-patterns against: |
6068 | for (Py_ssize_t i = 0; i < size; i++) { |
6069 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
6070 | if (WILDCARD_CHECK(pattern)) { |
6071 | continue; |
6072 | } |
6073 | ADDOP(c, DUP_TOP); |
6074 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
6075 | ADDOP(c, BINARY_SUBSCR); |
6076 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
6077 | } |
6078 | // If we get this far, it's a match! We're done with the tuple of values, |
6079 | // and whatever happens next should consume the tuple of keys underneath it: |
6080 | pc->on_top -= 2; |
6081 | ADDOP(c, POP_TOP); |
6082 | if (star_target) { |
6083 | // If we have a starred name, bind a dict of remaining items to it: |
6084 | ADDOP(c, COPY_DICT_WITHOUT_KEYS); |
6085 | RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc)); |
6086 | } |
6087 | else { |
6088 | // Otherwise, we don't care about this tuple of keys anymore: |
6089 | ADDOP(c, POP_TOP); |
6090 | } |
6091 | // Pop the subject: |
6092 | pc->on_top--; |
6093 | ADDOP(c, POP_TOP); |
6094 | return 1; |
6095 | |
6096 | error: |
6097 | Py_DECREF(seen); |
6098 | return 0; |
6099 | } |
6100 | |
6101 | static int |
6102 | compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) |
6103 | { |
6104 | assert(p->kind == MatchOr_kind); |
6105 | basicblock *end; |
6106 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
6107 | Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns); |
6108 | assert(size > 1); |
6109 | // We're going to be messing with pc. Keep the original info handy: |
6110 | pattern_context old_pc = *pc; |
6111 | Py_INCREF(pc->stores); |
6112 | // control is the list of names bound by the first alternative. It is used |
6113 | // for checking different name bindings in alternatives, and for correcting |
6114 | // the order in which extracted elements are placed on the stack. |
6115 | PyObject *control = NULL; |
6116 | // NOTE: We can't use returning macros anymore! goto error on error. |
6117 | for (Py_ssize_t i = 0; i < size; i++) { |
6118 | pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i); |
6119 | SET_LOC(c, alt); |
6120 | PyObject *pc_stores = PyList_New(0); |
6121 | if (pc_stores == NULL) { |
6122 | goto error; |
6123 | } |
6124 | Py_SETREF(pc->stores, pc_stores); |
6125 | // An irrefutable sub-pattern must be last, if it is allowed at all: |
6126 | pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable; |
6127 | pc->fail_pop = NULL; |
6128 | pc->fail_pop_size = 0; |
6129 | pc->on_top = 0; |
6130 | if (!compiler_addop(c, DUP_TOP) || !compiler_pattern(c, alt, pc)) { |
6131 | goto error; |
6132 | } |
6133 | // Success! |
6134 | Py_ssize_t nstores = PyList_GET_SIZE(pc->stores); |
6135 | if (!i) { |
6136 | // This is the first alternative, so save its stores as a "control" |
6137 | // for the others (they can't bind a different set of names, and |
6138 | // might need to be reordered): |
6139 | assert(control == NULL); |
6140 | control = pc->stores; |
6141 | Py_INCREF(control); |
6142 | } |
6143 | else if (nstores != PyList_GET_SIZE(control)) { |
6144 | goto diff; |
6145 | } |
6146 | else if (nstores) { |
6147 | // There were captures. Check to see if we differ from control: |
6148 | Py_ssize_t icontrol = nstores; |
6149 | while (icontrol--) { |
6150 | PyObject *name = PyList_GET_ITEM(control, icontrol); |
6151 | Py_ssize_t istores = PySequence_Index(pc->stores, name); |
6152 | if (istores < 0) { |
6153 | PyErr_Clear(); |
6154 | goto diff; |
6155 | } |
6156 | if (icontrol != istores) { |
6157 | // Reorder the names on the stack to match the order of the |
6158 | // names in control. There's probably a better way of doing |
6159 | // this; the current solution is potentially very |
6160 | // inefficient when each alternative subpattern binds lots |
6161 | // of names in different orders. It's fine for reasonable |
6162 | // cases, though. |
6163 | assert(istores < icontrol); |
6164 | Py_ssize_t rotations = istores + 1; |
6165 | // Perform the same rotation on pc->stores: |
6166 | PyObject *rotated = PyList_GetSlice(pc->stores, 0, |
6167 | rotations); |
6168 | if (rotated == NULL || |
6169 | PyList_SetSlice(pc->stores, 0, rotations, NULL) || |
6170 | PyList_SetSlice(pc->stores, icontrol - istores, |
6171 | icontrol - istores, rotated)) |
6172 | { |
6173 | Py_XDECREF(rotated); |
6174 | goto error; |
6175 | } |
6176 | Py_DECREF(rotated); |
6177 | // That just did: |
6178 | // rotated = pc_stores[:rotations] |
6179 | // del pc_stores[:rotations] |
6180 | // pc_stores[icontrol-istores:icontrol-istores] = rotated |
6181 | // Do the same thing to the stack, using several ROT_Ns: |
6182 | while (rotations--) { |
6183 | if (!compiler_addop_i(c, ROT_N, icontrol + 1)) { |
6184 | goto error; |
6185 | } |
6186 | } |
6187 | } |
6188 | } |
6189 | } |
6190 | assert(control); |
6191 | if (!compiler_addop_j(c, JUMP_FORWARD, end) || |
6192 | !compiler_next_block(c) || |
6193 | !emit_and_reset_fail_pop(c, pc)) |
6194 | { |
6195 | goto error; |
6196 | } |
6197 | } |
6198 | Py_DECREF(pc->stores); |
6199 | *pc = old_pc; |
6200 | Py_INCREF(pc->stores); |
6201 | // Need to NULL this for the PyObject_Free call in the error block. |
6202 | old_pc.fail_pop = NULL; |
6203 | // No match. Pop the remaining copy of the subject and fail: |
6204 | if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP_FORWARD)) { |
6205 | goto error; |
6206 | } |
6207 | compiler_use_next_block(c, end); |
6208 | Py_ssize_t nstores = PyList_GET_SIZE(control); |
6209 | // There's a bunch of stuff on the stack between any where the new stores |
6210 | // are and where they need to be: |
6211 | // - The other stores. |
6212 | // - A copy of the subject. |
6213 | // - Anything else that may be on top of the stack. |
6214 | // - Any previous stores we've already stashed away on the stack. |
6215 | Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores); |
6216 | for (Py_ssize_t i = 0; i < nstores; i++) { |
6217 | // Rotate this capture to its proper place on the stack: |
6218 | if (!compiler_addop_i(c, ROT_N, nrots)) { |
6219 | goto error; |
6220 | } |
6221 | // Update the list of previous stores with this new name, checking for |
6222 | // duplicates: |
6223 | PyObject *name = PyList_GET_ITEM(control, i); |
6224 | int dupe = PySequence_Contains(pc->stores, name); |
6225 | if (dupe < 0) { |
6226 | goto error; |
6227 | } |
6228 | if (dupe) { |
6229 | compiler_error_duplicate_store(c, name); |
6230 | goto error; |
6231 | } |
6232 | if (PyList_Append(pc->stores, name)) { |
6233 | goto error; |
6234 | } |
6235 | } |
6236 | Py_DECREF(old_pc.stores); |
6237 | Py_DECREF(control); |
6238 | // NOTE: Returning macros are safe again. |
6239 | // Pop the copy of the subject: |
6240 | ADDOP(c, POP_TOP); |
6241 | return 1; |
6242 | diff: |
6243 | compiler_error(c, "alternative patterns bind different names" ); |
6244 | error: |
6245 | PyObject_Free(old_pc.fail_pop); |
6246 | Py_DECREF(old_pc.stores); |
6247 | Py_XDECREF(control); |
6248 | return 0; |
6249 | } |
6250 | |
6251 | |
6252 | static int |
6253 | compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc) |
6254 | { |
6255 | assert(p->kind == MatchSequence_kind); |
6256 | asdl_pattern_seq *patterns = p->v.MatchSequence.patterns; |
6257 | Py_ssize_t size = asdl_seq_LEN(patterns); |
6258 | Py_ssize_t star = -1; |
6259 | int only_wildcard = 1; |
6260 | int star_wildcard = 0; |
6261 | // Find a starred name, if it exists. There may be at most one: |
6262 | for (Py_ssize_t i = 0; i < size; i++) { |
6263 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
6264 | if (pattern->kind == MatchStar_kind) { |
6265 | if (star >= 0) { |
6266 | const char *e = "multiple starred names in sequence pattern" ; |
6267 | return compiler_error(c, e); |
6268 | } |
6269 | star_wildcard = WILDCARD_STAR_CHECK(pattern); |
6270 | only_wildcard &= star_wildcard; |
6271 | star = i; |
6272 | continue; |
6273 | } |
6274 | only_wildcard &= WILDCARD_CHECK(pattern); |
6275 | } |
6276 | // We need to keep the subject on top during the sequence and length checks: |
6277 | pc->on_top++; |
6278 | ADDOP(c, MATCH_SEQUENCE); |
6279 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
6280 | if (star < 0) { |
6281 | // No star: len(subject) == size |
6282 | ADDOP(c, GET_LEN); |
6283 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
6284 | ADDOP_COMPARE(c, Eq); |
6285 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
6286 | } |
6287 | else if (size > 1) { |
6288 | // Star: len(subject) >= size - 1 |
6289 | ADDOP(c, GET_LEN); |
6290 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1)); |
6291 | ADDOP_COMPARE(c, GtE); |
6292 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
6293 | } |
6294 | // Whatever comes next should consume the subject: |
6295 | pc->on_top--; |
6296 | if (only_wildcard) { |
6297 | // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. |
6298 | ADDOP(c, POP_TOP); |
6299 | } |
6300 | else if (star_wildcard) { |
6301 | RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc)); |
6302 | } |
6303 | else { |
6304 | RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc)); |
6305 | } |
6306 | return 1; |
6307 | } |
6308 | |
6309 | static int |
6310 | compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc) |
6311 | { |
6312 | assert(p->kind == MatchValue_kind); |
6313 | expr_ty value = p->v.MatchValue.value; |
6314 | if (!MATCH_VALUE_EXPR(value)) { |
6315 | const char *e = "patterns may only match literals and attribute lookups" ; |
6316 | return compiler_error(c, e); |
6317 | } |
6318 | VISIT(c, expr, value); |
6319 | ADDOP_COMPARE(c, Eq); |
6320 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
6321 | return 1; |
6322 | } |
6323 | |
6324 | static int |
6325 | compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc) |
6326 | { |
6327 | assert(p->kind == MatchSingleton_kind); |
6328 | ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value); |
6329 | ADDOP_COMPARE(c, Is); |
6330 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
6331 | return 1; |
6332 | } |
6333 | |
6334 | static int |
6335 | compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) |
6336 | { |
6337 | SET_LOC(c, p); |
6338 | switch (p->kind) { |
6339 | case MatchValue_kind: |
6340 | return compiler_pattern_value(c, p, pc); |
6341 | case MatchSingleton_kind: |
6342 | return compiler_pattern_singleton(c, p, pc); |
6343 | case MatchSequence_kind: |
6344 | return compiler_pattern_sequence(c, p, pc); |
6345 | case MatchMapping_kind: |
6346 | return compiler_pattern_mapping(c, p, pc); |
6347 | case MatchClass_kind: |
6348 | return compiler_pattern_class(c, p, pc); |
6349 | case MatchStar_kind: |
6350 | return compiler_pattern_star(c, p, pc); |
6351 | case MatchAs_kind: |
6352 | return compiler_pattern_as(c, p, pc); |
6353 | case MatchOr_kind: |
6354 | return compiler_pattern_or(c, p, pc); |
6355 | } |
6356 | // AST validator shouldn't let this happen, but if it does, |
6357 | // just fail, don't crash out of the interpreter |
6358 | const char *e = "invalid match pattern node in AST (kind=%d)" ; |
6359 | return compiler_error(c, e, p->kind); |
6360 | } |
6361 | |
6362 | static int |
6363 | compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) |
6364 | { |
6365 | VISIT(c, expr, s->v.Match.subject); |
6366 | basicblock *end; |
6367 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
6368 | Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); |
6369 | assert(cases > 0); |
6370 | match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
6371 | int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases; |
6372 | for (Py_ssize_t i = 0; i < cases - has_default; i++) { |
6373 | m = asdl_seq_GET(s->v.Match.cases, i); |
6374 | SET_LOC(c, m->pattern); |
6375 | // Only copy the subject if we're *not* on the last case: |
6376 | if (i != cases - has_default - 1) { |
6377 | ADDOP(c, DUP_TOP); |
6378 | } |
6379 | RETURN_IF_FALSE(pc->stores = PyList_New(0)); |
6380 | // Irrefutable cases must be either guarded, last, or both: |
6381 | pc->allow_irrefutable = m->guard != NULL || i == cases - 1; |
6382 | pc->fail_pop = NULL; |
6383 | pc->fail_pop_size = 0; |
6384 | pc->on_top = 0; |
6385 | // NOTE: Can't use returning macros here (they'll leak pc->stores)! |
6386 | if (!compiler_pattern(c, m->pattern, pc)) { |
6387 | Py_DECREF(pc->stores); |
6388 | return 0; |
6389 | } |
6390 | assert(!pc->on_top); |
6391 | // It's a match! Store all of the captured names (they're on the stack). |
6392 | Py_ssize_t nstores = PyList_GET_SIZE(pc->stores); |
6393 | for (Py_ssize_t n = 0; n < nstores; n++) { |
6394 | PyObject *name = PyList_GET_ITEM(pc->stores, n); |
6395 | if (!compiler_nameop(c, name, Store)) { |
6396 | Py_DECREF(pc->stores); |
6397 | return 0; |
6398 | } |
6399 | } |
6400 | Py_DECREF(pc->stores); |
6401 | // NOTE: Returning macros are safe again. |
6402 | if (m->guard) { |
6403 | RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0)); |
6404 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0)); |
6405 | } |
6406 | // Success! Pop the subject off, we're done with it: |
6407 | if (i != cases - has_default - 1) { |
6408 | ADDOP(c, POP_TOP); |
6409 | } |
6410 | VISIT_SEQ(c, stmt, m->body); |
6411 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
6412 | // If the pattern fails to match, we want the line number of the |
6413 | // cleanup to be associated with the failed pattern, not the last line |
6414 | // of the body |
6415 | SET_LOC(c, m->pattern); |
6416 | RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc)); |
6417 | } |
6418 | if (has_default) { |
6419 | // A trailing "case _" is common, and lets us save a bit of redundant |
6420 | // pushing and popping in the loop above: |
6421 | m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
6422 | SET_LOC(c, m->pattern); |
6423 | if (cases == 1) { |
6424 | // No matches. Done with the subject: |
6425 | ADDOP(c, POP_TOP); |
6426 | } |
6427 | else { |
6428 | // Show line coverage for default case (it doesn't create bytecode) |
6429 | ADDOP(c, NOP); |
6430 | } |
6431 | if (m->guard) { |
6432 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0)); |
6433 | } |
6434 | VISIT_SEQ(c, stmt, m->body); |
6435 | } |
6436 | compiler_use_next_block(c, end); |
6437 | return 1; |
6438 | } |
6439 | |
6440 | static int |
6441 | compiler_match(struct compiler *c, stmt_ty s) |
6442 | { |
6443 | pattern_context pc; |
6444 | pc.fail_pop = NULL; |
6445 | int result = compiler_match_inner(c, s, &pc); |
6446 | PyObject_Free(pc.fail_pop); |
6447 | return result; |
6448 | } |
6449 | |
6450 | #undef WILDCARD_CHECK |
6451 | #undef WILDCARD_STAR_CHECK |
6452 | |
6453 | /* End of the compiler section, beginning of the assembler section */ |
6454 | |
6455 | /* do depth-first search of basic block graph, starting with block. |
6456 | post records the block indices in post-order. |
6457 | |
6458 | XXX must handle implicit jumps from one block to next |
6459 | */ |
6460 | |
6461 | struct assembler { |
6462 | PyObject *a_bytecode; /* string containing bytecode */ |
6463 | int a_offset; /* offset into bytecode */ |
6464 | int a_nblocks; /* number of reachable blocks */ |
6465 | PyObject *a_lnotab; /* string containing lnotab */ |
6466 | int a_lnotab_off; /* offset into lnotab */ |
6467 | int a_prevlineno; /* lineno of last emitted line in line table */ |
6468 | int a_lineno; /* lineno of last emitted instruction */ |
6469 | int a_lineno_start; /* bytecode start offset of current lineno */ |
6470 | basicblock *a_entry; |
6471 | }; |
6472 | |
6473 | Py_LOCAL_INLINE(void) |
6474 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
6475 | { |
6476 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
6477 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
6478 | assert(b->b_startdepth < 0); |
6479 | b->b_startdepth = depth; |
6480 | *(*sp)++ = b; |
6481 | } |
6482 | } |
6483 | |
6484 | /* Find the flow path that needs the largest stack. We assume that |
6485 | * cycles in the flow graph have no net effect on the stack depth. |
6486 | */ |
6487 | static int |
6488 | stackdepth(struct compiler *c) |
6489 | { |
6490 | basicblock *b, *entryblock = NULL; |
6491 | basicblock **stack, **sp; |
6492 | int nblocks = 0, maxdepth = 0; |
6493 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
6494 | b->b_startdepth = INT_MIN; |
6495 | entryblock = b; |
6496 | nblocks++; |
6497 | } |
6498 | assert(entryblock!= NULL); |
6499 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
6500 | if (!stack) { |
6501 | PyErr_NoMemory(); |
6502 | return -1; |
6503 | } |
6504 | |
6505 | sp = stack; |
6506 | if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) { |
6507 | stackdepth_push(&sp, entryblock, 1); |
6508 | } else { |
6509 | stackdepth_push(&sp, entryblock, 0); |
6510 | } |
6511 | while (sp != stack) { |
6512 | b = *--sp; |
6513 | int depth = b->b_startdepth; |
6514 | assert(depth >= 0); |
6515 | basicblock *next = b->b_next; |
6516 | for (int i = 0; i < b->b_iused; i++) { |
6517 | struct instr *instr = &b->b_instr[i]; |
6518 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
6519 | if (effect == PY_INVALID_STACK_EFFECT) { |
6520 | PyErr_Format(PyExc_SystemError, |
6521 | "compiler stack_effect(opcode=%d, arg=%i) failed" , |
6522 | instr->i_opcode, instr->i_oparg); |
6523 | return -1; |
6524 | } |
6525 | int new_depth = depth + effect; |
6526 | if (new_depth > maxdepth) { |
6527 | maxdepth = new_depth; |
6528 | } |
6529 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
6530 | if (is_jump(instr)) { |
6531 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
6532 | assert(effect != PY_INVALID_STACK_EFFECT); |
6533 | int target_depth = depth + effect; |
6534 | if (target_depth > maxdepth) { |
6535 | maxdepth = target_depth; |
6536 | } |
6537 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
6538 | stackdepth_push(&sp, instr->i_target, target_depth); |
6539 | } |
6540 | depth = new_depth; |
6541 | if (instr->i_opcode == JUMP_ABSOLUTE || |
6542 | instr->i_opcode == JUMP_FORWARD || |
6543 | instr->i_opcode == RETURN_VALUE || |
6544 | instr->i_opcode == RAISE_VARARGS || |
6545 | instr->i_opcode == RERAISE) |
6546 | { |
6547 | /* remaining code is dead */ |
6548 | next = NULL; |
6549 | break; |
6550 | } |
6551 | } |
6552 | if (next != NULL) { |
6553 | assert(b->b_nofallthrough == 0); |
6554 | stackdepth_push(&sp, next, depth); |
6555 | } |
6556 | } |
6557 | PyObject_Free(stack); |
6558 | return maxdepth; |
6559 | } |
6560 | |
6561 | static int |
6562 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
6563 | { |
6564 | memset(a, 0, sizeof(struct assembler)); |
6565 | a->a_prevlineno = a->a_lineno = firstlineno; |
6566 | a->a_lnotab = NULL; |
6567 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
6568 | if (a->a_bytecode == NULL) { |
6569 | goto error; |
6570 | } |
6571 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
6572 | if (a->a_lnotab == NULL) { |
6573 | goto error; |
6574 | } |
6575 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
6576 | PyErr_NoMemory(); |
6577 | goto error; |
6578 | } |
6579 | return 1; |
6580 | error: |
6581 | Py_XDECREF(a->a_bytecode); |
6582 | Py_XDECREF(a->a_lnotab); |
6583 | return 0; |
6584 | } |
6585 | |
6586 | static void |
6587 | assemble_free(struct assembler *a) |
6588 | { |
6589 | Py_XDECREF(a->a_bytecode); |
6590 | Py_XDECREF(a->a_lnotab); |
6591 | } |
6592 | |
6593 | static int |
6594 | blocksize(basicblock *b) |
6595 | { |
6596 | int i; |
6597 | int size = 0; |
6598 | |
6599 | for (i = 0; i < b->b_iused; i++) |
6600 | size += instrsize(b->b_instr[i].i_oparg); |
6601 | return size; |
6602 | } |
6603 | |
6604 | static int |
6605 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
6606 | { |
6607 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
6608 | if (a->a_lnotab_off + 2 >= len) { |
6609 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
6610 | return 0; |
6611 | } |
6612 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
6613 | lnotab += a->a_lnotab_off; |
6614 | a->a_lnotab_off += 2; |
6615 | *lnotab++ = bdelta; |
6616 | *lnotab++ = ldelta; |
6617 | return 1; |
6618 | } |
6619 | |
6620 | /* Appends a range to the end of the line number table. See |
6621 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
6622 | |
6623 | static int |
6624 | assemble_line_range(struct assembler *a) |
6625 | { |
6626 | int ldelta, bdelta; |
6627 | bdelta = (a->a_offset - a->a_lineno_start) * sizeof(_Py_CODEUNIT); |
6628 | if (bdelta == 0) { |
6629 | return 1; |
6630 | } |
6631 | if (a->a_lineno < 0) { |
6632 | ldelta = -128; |
6633 | } |
6634 | else { |
6635 | ldelta = a->a_lineno - a->a_prevlineno; |
6636 | a->a_prevlineno = a->a_lineno; |
6637 | while (ldelta > 127) { |
6638 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
6639 | return 0; |
6640 | } |
6641 | ldelta -= 127; |
6642 | } |
6643 | while (ldelta < -127) { |
6644 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
6645 | return 0; |
6646 | } |
6647 | ldelta += 127; |
6648 | } |
6649 | } |
6650 | assert(-128 <= ldelta && ldelta < 128); |
6651 | while (bdelta > 254) { |
6652 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
6653 | return 0; |
6654 | } |
6655 | ldelta = a->a_lineno < 0 ? -128 : 0; |
6656 | bdelta -= 254; |
6657 | } |
6658 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
6659 | return 0; |
6660 | } |
6661 | a->a_lineno_start = a->a_offset; |
6662 | return 1; |
6663 | } |
6664 | |
6665 | static int |
6666 | assemble_lnotab(struct assembler *a, struct instr *i) |
6667 | { |
6668 | if (i->i_lineno == a->a_lineno) { |
6669 | return 1; |
6670 | } |
6671 | if (!assemble_line_range(a)) { |
6672 | return 0; |
6673 | } |
6674 | a->a_lineno = i->i_lineno; |
6675 | return 1; |
6676 | } |
6677 | |
6678 | |
6679 | /* assemble_emit() |
6680 | Extend the bytecode with a new instruction. |
6681 | Update lnotab if necessary. |
6682 | */ |
6683 | |
6684 | static int |
6685 | assemble_emit(struct assembler *a, struct instr *i) |
6686 | { |
6687 | int size, arg = 0; |
6688 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
6689 | _Py_CODEUNIT *code; |
6690 | |
6691 | arg = i->i_oparg; |
6692 | size = instrsize(arg); |
6693 | if (i->i_lineno && !assemble_lnotab(a, i)) |
6694 | return 0; |
6695 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
6696 | if (len > PY_SSIZE_T_MAX / 2) |
6697 | return 0; |
6698 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
6699 | return 0; |
6700 | } |
6701 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
6702 | a->a_offset += size; |
6703 | write_op_arg(code, i->i_opcode, arg, size); |
6704 | return 1; |
6705 | } |
6706 | |
6707 | static void |
6708 | normalize_jumps(struct assembler *a) |
6709 | { |
6710 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
6711 | b->b_visited = 0; |
6712 | } |
6713 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
6714 | b->b_visited = 1; |
6715 | if (b->b_iused == 0) { |
6716 | continue; |
6717 | } |
6718 | struct instr *last = &b->b_instr[b->b_iused-1]; |
6719 | if (last->i_opcode == JUMP_ABSOLUTE) { |
6720 | if (last->i_target->b_visited == 0) { |
6721 | last->i_opcode = JUMP_FORWARD; |
6722 | } |
6723 | } |
6724 | if (last->i_opcode == JUMP_FORWARD) { |
6725 | if (last->i_target->b_visited == 1) { |
6726 | last->i_opcode = JUMP_ABSOLUTE; |
6727 | } |
6728 | } |
6729 | } |
6730 | } |
6731 | |
6732 | static void |
6733 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
6734 | { |
6735 | basicblock *b; |
6736 | int bsize, totsize, extended_arg_recompile; |
6737 | int i; |
6738 | |
6739 | /* Compute the size of each block and fixup jump args. |
6740 | Replace block pointer with position in bytecode. */ |
6741 | do { |
6742 | totsize = 0; |
6743 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
6744 | bsize = blocksize(b); |
6745 | b->b_offset = totsize; |
6746 | totsize += bsize; |
6747 | } |
6748 | extended_arg_recompile = 0; |
6749 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
6750 | bsize = b->b_offset; |
6751 | for (i = 0; i < b->b_iused; i++) { |
6752 | struct instr *instr = &b->b_instr[i]; |
6753 | int isize = instrsize(instr->i_oparg); |
6754 | /* Relative jumps are computed relative to |
6755 | the instruction pointer after fetching |
6756 | the jump instruction. |
6757 | */ |
6758 | bsize += isize; |
6759 | if (is_jump(instr)) { |
6760 | instr->i_oparg = instr->i_target->b_offset; |
6761 | if (is_relative_jump(instr)) { |
6762 | instr->i_oparg -= bsize; |
6763 | } |
6764 | if (instrsize(instr->i_oparg) != isize) { |
6765 | extended_arg_recompile = 1; |
6766 | } |
6767 | } |
6768 | } |
6769 | } |
6770 | |
6771 | /* XXX: This is an awful hack that could hurt performance, but |
6772 | on the bright side it should work until we come up |
6773 | with a better solution. |
6774 | |
6775 | The issue is that in the first loop blocksize() is called |
6776 | which calls instrsize() which requires i_oparg be set |
6777 | appropriately. There is a bootstrap problem because |
6778 | i_oparg is calculated in the second loop above. |
6779 | |
6780 | So we loop until we stop seeing new EXTENDED_ARGs. |
6781 | The only EXTENDED_ARGs that could be popping up are |
6782 | ones in jump instructions. So this should converge |
6783 | fairly quickly. |
6784 | */ |
6785 | } while (extended_arg_recompile); |
6786 | } |
6787 | |
6788 | static PyObject * |
6789 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
6790 | { |
6791 | PyObject *tuple, *k, *v; |
6792 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
6793 | |
6794 | tuple = PyTuple_New(size); |
6795 | if (tuple == NULL) |
6796 | return NULL; |
6797 | while (PyDict_Next(dict, &pos, &k, &v)) { |
6798 | i = PyLong_AS_LONG(v); |
6799 | Py_INCREF(k); |
6800 | assert((i - offset) < size); |
6801 | assert((i - offset) >= 0); |
6802 | PyTuple_SET_ITEM(tuple, i - offset, k); |
6803 | } |
6804 | return tuple; |
6805 | } |
6806 | |
6807 | static PyObject * |
6808 | consts_dict_keys_inorder(PyObject *dict) |
6809 | { |
6810 | PyObject *consts, *k, *v; |
6811 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
6812 | |
6813 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
6814 | if (consts == NULL) |
6815 | return NULL; |
6816 | while (PyDict_Next(dict, &pos, &k, &v)) { |
6817 | i = PyLong_AS_LONG(v); |
6818 | /* The keys of the dictionary can be tuples wrapping a constant. |
6819 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
6820 | * the object we want is always second. */ |
6821 | if (PyTuple_CheckExact(k)) { |
6822 | k = PyTuple_GET_ITEM(k, 1); |
6823 | } |
6824 | Py_INCREF(k); |
6825 | assert(i < size); |
6826 | assert(i >= 0); |
6827 | PyList_SET_ITEM(consts, i, k); |
6828 | } |
6829 | return consts; |
6830 | } |
6831 | |
6832 | static int |
6833 | compute_code_flags(struct compiler *c) |
6834 | { |
6835 | PySTEntryObject *ste = c->u->u_ste; |
6836 | int flags = 0; |
6837 | if (ste->ste_type == FunctionBlock) { |
6838 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
6839 | if (ste->ste_nested) |
6840 | flags |= CO_NESTED; |
6841 | if (ste->ste_generator && !ste->ste_coroutine) |
6842 | flags |= CO_GENERATOR; |
6843 | if (!ste->ste_generator && ste->ste_coroutine) |
6844 | flags |= CO_COROUTINE; |
6845 | if (ste->ste_generator && ste->ste_coroutine) |
6846 | flags |= CO_ASYNC_GENERATOR; |
6847 | if (ste->ste_varargs) |
6848 | flags |= CO_VARARGS; |
6849 | if (ste->ste_varkeywords) |
6850 | flags |= CO_VARKEYWORDS; |
6851 | } |
6852 | |
6853 | /* (Only) inherit compilerflags in PyCF_MASK */ |
6854 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
6855 | |
6856 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
6857 | ste->ste_coroutine && |
6858 | !ste->ste_generator) { |
6859 | flags |= CO_COROUTINE; |
6860 | } |
6861 | |
6862 | return flags; |
6863 | } |
6864 | |
6865 | // Merge *obj* with constant cache. |
6866 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
6867 | static int |
6868 | merge_const_one(struct compiler *c, PyObject **obj) |
6869 | { |
6870 | PyObject *key = _PyCode_ConstantKey(*obj); |
6871 | if (key == NULL) { |
6872 | return 0; |
6873 | } |
6874 | |
6875 | // t is borrowed reference |
6876 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
6877 | Py_DECREF(key); |
6878 | if (t == NULL) { |
6879 | return 0; |
6880 | } |
6881 | if (t == key) { // obj is new constant. |
6882 | return 1; |
6883 | } |
6884 | |
6885 | if (PyTuple_CheckExact(t)) { |
6886 | // t is still borrowed reference |
6887 | t = PyTuple_GET_ITEM(t, 1); |
6888 | } |
6889 | |
6890 | Py_INCREF(t); |
6891 | Py_DECREF(*obj); |
6892 | *obj = t; |
6893 | return 1; |
6894 | } |
6895 | |
6896 | static PyCodeObject * |
6897 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
6898 | { |
6899 | PyCodeObject *co = NULL; |
6900 | PyObject *names = NULL; |
6901 | PyObject *varnames = NULL; |
6902 | PyObject *name = NULL; |
6903 | PyObject *freevars = NULL; |
6904 | PyObject *cellvars = NULL; |
6905 | Py_ssize_t nlocals; |
6906 | int nlocals_int; |
6907 | int flags; |
6908 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
6909 | |
6910 | names = dict_keys_inorder(c->u->u_names, 0); |
6911 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
6912 | if (!names || !varnames) { |
6913 | goto error; |
6914 | } |
6915 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
6916 | if (!cellvars) |
6917 | goto error; |
6918 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
6919 | if (!freevars) |
6920 | goto error; |
6921 | |
6922 | if (!merge_const_one(c, &names) || |
6923 | !merge_const_one(c, &varnames) || |
6924 | !merge_const_one(c, &cellvars) || |
6925 | !merge_const_one(c, &freevars)) |
6926 | { |
6927 | goto error; |
6928 | } |
6929 | |
6930 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
6931 | assert(nlocals < INT_MAX); |
6932 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
6933 | |
6934 | flags = compute_code_flags(c); |
6935 | if (flags < 0) |
6936 | goto error; |
6937 | |
6938 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
6939 | if (consts == NULL) { |
6940 | goto error; |
6941 | } |
6942 | if (!merge_const_one(c, &consts)) { |
6943 | Py_DECREF(consts); |
6944 | goto error; |
6945 | } |
6946 | |
6947 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
6948 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
6949 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
6950 | maxdepth = stackdepth(c); |
6951 | if (maxdepth < 0) { |
6952 | Py_DECREF(consts); |
6953 | goto error; |
6954 | } |
6955 | if (maxdepth > MAX_ALLOWED_STACK_USE) { |
6956 | PyErr_Format(PyExc_SystemError, |
6957 | "excessive stack use: stack is %d deep" , |
6958 | maxdepth); |
6959 | Py_DECREF(consts); |
6960 | goto error; |
6961 | } |
6962 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
6963 | posonlyargcount, kwonlyargcount, nlocals_int, |
6964 | maxdepth, flags, a->a_bytecode, consts, names, |
6965 | varnames, freevars, cellvars, c->c_filename, |
6966 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
6967 | Py_DECREF(consts); |
6968 | error: |
6969 | Py_XDECREF(names); |
6970 | Py_XDECREF(varnames); |
6971 | Py_XDECREF(name); |
6972 | Py_XDECREF(freevars); |
6973 | Py_XDECREF(cellvars); |
6974 | return co; |
6975 | } |
6976 | |
6977 | |
6978 | /* For debugging purposes only */ |
6979 | #if 0 |
6980 | static void |
6981 | dump_instr(struct instr *i) |
6982 | { |
6983 | const char *jrel = (is_relative_jump(i)) ? "jrel " : "" ; |
6984 | const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "" ; |
6985 | |
6986 | char arg[128]; |
6987 | |
6988 | *arg = '\0'; |
6989 | if (HAS_ARG(i->i_opcode)) { |
6990 | sprintf(arg, "arg: %d " , i->i_oparg); |
6991 | } |
6992 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n" , |
6993 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
6994 | } |
6995 | |
6996 | static void |
6997 | dump_basicblock(const basicblock *b) |
6998 | { |
6999 | const char *b_return = b->b_return ? "return " : "" ; |
7000 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n" , |
7001 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
7002 | if (b->b_instr) { |
7003 | int i; |
7004 | for (i = 0; i < b->b_iused; i++) { |
7005 | fprintf(stderr, " [%02d] " , i); |
7006 | dump_instr(b->b_instr + i); |
7007 | } |
7008 | } |
7009 | } |
7010 | #endif |
7011 | |
7012 | |
7013 | static int |
7014 | normalize_basic_block(basicblock *bb); |
7015 | |
7016 | static int |
7017 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts); |
7018 | |
7019 | static int |
7020 | trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts); |
7021 | |
7022 | /* Duplicates exit BBs, so that line numbers can be propagated to them */ |
7023 | static int |
7024 | duplicate_exits_without_lineno(struct compiler *c); |
7025 | |
7026 | static int |
7027 | extend_block(basicblock *bb); |
7028 | |
7029 | static int |
7030 | insert_generator_prefix(struct compiler *c, basicblock *entryblock) { |
7031 | |
7032 | int flags = compute_code_flags(c); |
7033 | if (flags < 0) { |
7034 | return -1; |
7035 | } |
7036 | int kind; |
7037 | if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
7038 | if (flags & CO_COROUTINE) { |
7039 | kind = 1; |
7040 | } |
7041 | else if (flags & CO_ASYNC_GENERATOR) { |
7042 | kind = 2; |
7043 | } |
7044 | else { |
7045 | kind = 0; |
7046 | } |
7047 | } |
7048 | else { |
7049 | return 0; |
7050 | } |
7051 | if (compiler_next_instr(entryblock) < 0) { |
7052 | return -1; |
7053 | } |
7054 | for (int i = entryblock->b_iused-1; i > 0; i--) { |
7055 | entryblock->b_instr[i] = entryblock->b_instr[i-1]; |
7056 | } |
7057 | entryblock->b_instr[0].i_opcode = GEN_START; |
7058 | entryblock->b_instr[0].i_oparg = kind; |
7059 | entryblock->b_instr[0].i_lineno = -1; |
7060 | entryblock->b_instr[0].i_target = NULL; |
7061 | return 0; |
7062 | } |
7063 | |
7064 | /* Make sure that all returns have a line number, even if early passes |
7065 | * have failed to propagate a correct line number. |
7066 | * The resulting line number may not be correct according to PEP 626, |
7067 | * but should be "good enough", and no worse than in older versions. */ |
7068 | static void |
7069 | guarantee_lineno_for_exits(struct assembler *a, int firstlineno) { |
7070 | int lineno = firstlineno; |
7071 | assert(lineno > 0); |
7072 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
7073 | if (b->b_iused == 0) { |
7074 | continue; |
7075 | } |
7076 | struct instr *last = &b->b_instr[b->b_iused-1]; |
7077 | if (last->i_lineno < 0) { |
7078 | if (last->i_opcode == RETURN_VALUE) { |
7079 | for (int i = 0; i < b->b_iused; i++) { |
7080 | assert(b->b_instr[i].i_lineno < 0); |
7081 | |
7082 | b->b_instr[i].i_lineno = lineno; |
7083 | } |
7084 | } |
7085 | } |
7086 | else { |
7087 | lineno = last->i_lineno; |
7088 | } |
7089 | } |
7090 | } |
7091 | |
7092 | static void |
7093 | propagate_line_numbers(struct assembler *a); |
7094 | |
7095 | static PyCodeObject * |
7096 | assemble(struct compiler *c, int addNone) |
7097 | { |
7098 | basicblock *b, *entryblock; |
7099 | struct assembler a; |
7100 | int j, nblocks; |
7101 | PyCodeObject *co = NULL; |
7102 | PyObject *consts = NULL; |
7103 | |
7104 | /* Make sure every block that falls off the end returns None. |
7105 | XXX NEXT_BLOCK() isn't quite right, because if the last |
7106 | block ends with a jump or return b_next shouldn't set. |
7107 | */ |
7108 | if (!c->u->u_curblock->b_return) { |
7109 | c->u->u_lineno = -1; |
7110 | if (addNone) |
7111 | ADDOP_LOAD_CONST(c, Py_None); |
7112 | ADDOP(c, RETURN_VALUE); |
7113 | } |
7114 | |
7115 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
7116 | if (normalize_basic_block(b)) { |
7117 | return NULL; |
7118 | } |
7119 | } |
7120 | |
7121 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
7122 | if (extend_block(b)) { |
7123 | return NULL; |
7124 | } |
7125 | } |
7126 | |
7127 | nblocks = 0; |
7128 | entryblock = NULL; |
7129 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
7130 | nblocks++; |
7131 | entryblock = b; |
7132 | } |
7133 | assert(entryblock != NULL); |
7134 | |
7135 | if (insert_generator_prefix(c, entryblock)) { |
7136 | goto error; |
7137 | } |
7138 | |
7139 | /* Set firstlineno if it wasn't explicitly set. */ |
7140 | if (!c->u->u_firstlineno) { |
7141 | if (entryblock->b_instr && entryblock->b_instr->i_lineno) |
7142 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
7143 | else |
7144 | c->u->u_firstlineno = 1; |
7145 | } |
7146 | |
7147 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
7148 | goto error; |
7149 | a.a_entry = entryblock; |
7150 | a.a_nblocks = nblocks; |
7151 | |
7152 | consts = consts_dict_keys_inorder(c->u->u_consts); |
7153 | if (consts == NULL) { |
7154 | goto error; |
7155 | } |
7156 | |
7157 | if (optimize_cfg(c, &a, consts)) { |
7158 | goto error; |
7159 | } |
7160 | if (duplicate_exits_without_lineno(c)) { |
7161 | return NULL; |
7162 | } |
7163 | if (trim_unused_consts(c, &a, consts)) { |
7164 | goto error; |
7165 | } |
7166 | propagate_line_numbers(&a); |
7167 | guarantee_lineno_for_exits(&a, c->u->u_firstlineno); |
7168 | |
7169 | /* Order of basic blocks must have been determined by now */ |
7170 | normalize_jumps(&a); |
7171 | |
7172 | /* Can't modify the bytecode after computing jump offsets. */ |
7173 | assemble_jump_offsets(&a, c); |
7174 | |
7175 | /* Emit code. */ |
7176 | for(b = entryblock; b != NULL; b = b->b_next) { |
7177 | for (j = 0; j < b->b_iused; j++) |
7178 | if (!assemble_emit(&a, &b->b_instr[j])) |
7179 | goto error; |
7180 | } |
7181 | if (!assemble_line_range(&a)) { |
7182 | return 0; |
7183 | } |
7184 | |
7185 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) { |
7186 | goto error; |
7187 | } |
7188 | if (!merge_const_one(c, &a.a_lnotab)) { |
7189 | goto error; |
7190 | } |
7191 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) { |
7192 | goto error; |
7193 | } |
7194 | if (!merge_const_one(c, &a.a_bytecode)) { |
7195 | goto error; |
7196 | } |
7197 | |
7198 | co = makecode(c, &a, consts); |
7199 | error: |
7200 | Py_XDECREF(consts); |
7201 | assemble_free(&a); |
7202 | return co; |
7203 | } |
7204 | |
7205 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
7206 | with LOAD_CONST (c1, c2, ... cn). |
7207 | The consts table must still be in list form so that the |
7208 | new constant (c1, c2, ... cn) can be appended. |
7209 | Called with codestr pointing to the first LOAD_CONST. |
7210 | */ |
7211 | static int |
7212 | fold_tuple_on_constants(struct compiler *c, |
7213 | struct instr *inst, |
7214 | int n, PyObject *consts) |
7215 | { |
7216 | /* Pre-conditions */ |
7217 | assert(PyList_CheckExact(consts)); |
7218 | assert(inst[n].i_opcode == BUILD_TUPLE); |
7219 | assert(inst[n].i_oparg == n); |
7220 | |
7221 | for (int i = 0; i < n; i++) { |
7222 | if (inst[i].i_opcode != LOAD_CONST) { |
7223 | return 0; |
7224 | } |
7225 | } |
7226 | |
7227 | /* Buildup new tuple of constants */ |
7228 | PyObject *newconst = PyTuple_New(n); |
7229 | if (newconst == NULL) { |
7230 | return -1; |
7231 | } |
7232 | for (int i = 0; i < n; i++) { |
7233 | int arg = inst[i].i_oparg; |
7234 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
7235 | Py_INCREF(constant); |
7236 | PyTuple_SET_ITEM(newconst, i, constant); |
7237 | } |
7238 | if (merge_const_one(c, &newconst) == 0) { |
7239 | Py_DECREF(newconst); |
7240 | return -1; |
7241 | } |
7242 | |
7243 | Py_ssize_t index; |
7244 | for (index = 0; index < PyList_GET_SIZE(consts); index++) { |
7245 | if (PyList_GET_ITEM(consts, index) == newconst) { |
7246 | break; |
7247 | } |
7248 | } |
7249 | if (index == PyList_GET_SIZE(consts)) { |
7250 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
7251 | Py_DECREF(newconst); |
7252 | PyErr_SetString(PyExc_OverflowError, "too many constants" ); |
7253 | return -1; |
7254 | } |
7255 | if (PyList_Append(consts, newconst)) { |
7256 | Py_DECREF(newconst); |
7257 | return -1; |
7258 | } |
7259 | } |
7260 | Py_DECREF(newconst); |
7261 | for (int i = 0; i < n; i++) { |
7262 | inst[i].i_opcode = NOP; |
7263 | } |
7264 | inst[n].i_opcode = LOAD_CONST; |
7265 | inst[n].i_oparg = (int)index; |
7266 | return 0; |
7267 | } |
7268 | |
7269 | |
7270 | // Eliminate n * ROT_N(n). |
7271 | static void |
7272 | fold_rotations(struct instr *inst, int n) |
7273 | { |
7274 | for (int i = 0; i < n; i++) { |
7275 | int rot; |
7276 | switch (inst[i].i_opcode) { |
7277 | case ROT_N: |
7278 | rot = inst[i].i_oparg; |
7279 | break; |
7280 | case ROT_FOUR: |
7281 | rot = 4; |
7282 | break; |
7283 | case ROT_THREE: |
7284 | rot = 3; |
7285 | break; |
7286 | case ROT_TWO: |
7287 | rot = 2; |
7288 | break; |
7289 | default: |
7290 | return; |
7291 | } |
7292 | if (rot != n) { |
7293 | return; |
7294 | } |
7295 | } |
7296 | for (int i = 0; i < n; i++) { |
7297 | inst[i].i_opcode = NOP; |
7298 | } |
7299 | } |
7300 | |
7301 | // Attempt to eliminate jumps to jumps by updating inst to jump to |
7302 | // target->i_target using the provided opcode. Return whether or not the |
7303 | // optimization was successful. |
7304 | static bool |
7305 | jump_thread(struct instr *inst, struct instr *target, int opcode) |
7306 | { |
7307 | assert(is_jump(inst)); |
7308 | assert(is_jump(target)); |
7309 | // bpo-45773: If inst->i_target == target->i_target, then nothing actually |
7310 | // changes (and we fall into an infinite loop): |
7311 | if (inst->i_lineno == target->i_lineno && |
7312 | inst->i_target != target->i_target) |
7313 | { |
7314 | inst->i_target = target->i_target; |
7315 | inst->i_opcode = opcode; |
7316 | return true; |
7317 | } |
7318 | return false; |
7319 | } |
7320 | |
7321 | /* Maximum size of basic block that should be copied in optimizer */ |
7322 | #define MAX_COPY_SIZE 4 |
7323 | |
7324 | /* Optimization */ |
7325 | static int |
7326 | optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts) |
7327 | { |
7328 | assert(PyList_CheckExact(consts)); |
7329 | struct instr nop; |
7330 | nop.i_opcode = NOP; |
7331 | struct instr *target; |
7332 | for (int i = 0; i < bb->b_iused; i++) { |
7333 | struct instr *inst = &bb->b_instr[i]; |
7334 | int oparg = inst->i_oparg; |
7335 | int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0; |
7336 | if (is_jump(inst)) { |
7337 | /* Skip over empty basic blocks. */ |
7338 | while (inst->i_target->b_iused == 0) { |
7339 | inst->i_target = inst->i_target->b_next; |
7340 | } |
7341 | target = &inst->i_target->b_instr[0]; |
7342 | } |
7343 | else { |
7344 | target = &nop; |
7345 | } |
7346 | switch (inst->i_opcode) { |
7347 | /* Remove LOAD_CONST const; conditional jump */ |
7348 | case LOAD_CONST: |
7349 | { |
7350 | PyObject* cnt; |
7351 | int is_true; |
7352 | int jump_if_true; |
7353 | switch(nextop) { |
7354 | case POP_JUMP_IF_FALSE: |
7355 | case POP_JUMP_IF_TRUE: |
7356 | cnt = PyList_GET_ITEM(consts, oparg); |
7357 | is_true = PyObject_IsTrue(cnt); |
7358 | if (is_true == -1) { |
7359 | goto error; |
7360 | } |
7361 | inst->i_opcode = NOP; |
7362 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
7363 | if (is_true == jump_if_true) { |
7364 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
7365 | bb->b_nofallthrough = 1; |
7366 | } |
7367 | else { |
7368 | bb->b_instr[i+1].i_opcode = NOP; |
7369 | } |
7370 | break; |
7371 | case JUMP_IF_FALSE_OR_POP: |
7372 | case JUMP_IF_TRUE_OR_POP: |
7373 | cnt = PyList_GET_ITEM(consts, oparg); |
7374 | is_true = PyObject_IsTrue(cnt); |
7375 | if (is_true == -1) { |
7376 | goto error; |
7377 | } |
7378 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
7379 | if (is_true == jump_if_true) { |
7380 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
7381 | bb->b_nofallthrough = 1; |
7382 | } |
7383 | else { |
7384 | inst->i_opcode = NOP; |
7385 | bb->b_instr[i+1].i_opcode = NOP; |
7386 | } |
7387 | break; |
7388 | } |
7389 | break; |
7390 | } |
7391 | |
7392 | /* Try to fold tuples of constants. |
7393 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
7394 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
7395 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
7396 | case BUILD_TUPLE: |
7397 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
7398 | switch(oparg) { |
7399 | case 1: |
7400 | inst->i_opcode = NOP; |
7401 | bb->b_instr[i+1].i_opcode = NOP; |
7402 | break; |
7403 | case 2: |
7404 | inst->i_opcode = ROT_TWO; |
7405 | bb->b_instr[i+1].i_opcode = NOP; |
7406 | break; |
7407 | case 3: |
7408 | inst->i_opcode = ROT_THREE; |
7409 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
7410 | } |
7411 | break; |
7412 | } |
7413 | if (i >= oparg) { |
7414 | if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) { |
7415 | goto error; |
7416 | } |
7417 | } |
7418 | break; |
7419 | |
7420 | /* Simplify conditional jump to conditional jump where the |
7421 | result of the first test implies the success of a similar |
7422 | test or the failure of the opposite test. |
7423 | Arises in code like: |
7424 | "a and b or c" |
7425 | "(a and b) and c" |
7426 | "(a or b) or c" |
7427 | "(a or b) and c" |
7428 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
7429 | --> x:JUMP_IF_FALSE_OR_POP z |
7430 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
7431 | --> x:POP_JUMP_IF_FALSE y+1 |
7432 | where y+1 is the instruction following the second test. |
7433 | */ |
7434 | case JUMP_IF_FALSE_OR_POP: |
7435 | switch (target->i_opcode) { |
7436 | case POP_JUMP_IF_FALSE: |
7437 | i -= jump_thread(inst, target, POP_JUMP_IF_FALSE); |
7438 | break; |
7439 | case JUMP_ABSOLUTE: |
7440 | case JUMP_FORWARD: |
7441 | case JUMP_IF_FALSE_OR_POP: |
7442 | i -= jump_thread(inst, target, JUMP_IF_FALSE_OR_POP); |
7443 | break; |
7444 | case JUMP_IF_TRUE_OR_POP: |
7445 | case POP_JUMP_IF_TRUE: |
7446 | if (inst->i_lineno == target->i_lineno) { |
7447 | // We don't need to bother checking for loops here, |
7448 | // since a block's b_next cannot point to itself: |
7449 | assert(inst->i_target != inst->i_target->b_next); |
7450 | inst->i_opcode = POP_JUMP_IF_FALSE; |
7451 | inst->i_target = inst->i_target->b_next; |
7452 | --i; |
7453 | } |
7454 | break; |
7455 | } |
7456 | break; |
7457 | case JUMP_IF_TRUE_OR_POP: |
7458 | switch (target->i_opcode) { |
7459 | case POP_JUMP_IF_TRUE: |
7460 | i -= jump_thread(inst, target, POP_JUMP_IF_TRUE); |
7461 | break; |
7462 | case JUMP_ABSOLUTE: |
7463 | case JUMP_FORWARD: |
7464 | case JUMP_IF_TRUE_OR_POP: |
7465 | i -= jump_thread(inst, target, JUMP_IF_TRUE_OR_POP); |
7466 | break; |
7467 | case JUMP_IF_FALSE_OR_POP: |
7468 | case POP_JUMP_IF_FALSE: |
7469 | if (inst->i_lineno == target->i_lineno) { |
7470 | // We don't need to bother checking for loops here, |
7471 | // since a block's b_next cannot point to itself: |
7472 | assert(inst->i_target != inst->i_target->b_next); |
7473 | inst->i_opcode = POP_JUMP_IF_TRUE; |
7474 | inst->i_target = inst->i_target->b_next; |
7475 | --i; |
7476 | } |
7477 | break; |
7478 | } |
7479 | break; |
7480 | case POP_JUMP_IF_FALSE: |
7481 | switch (target->i_opcode) { |
7482 | case JUMP_ABSOLUTE: |
7483 | case JUMP_FORWARD: |
7484 | i -= jump_thread(inst, target, POP_JUMP_IF_FALSE); |
7485 | } |
7486 | break; |
7487 | case POP_JUMP_IF_TRUE: |
7488 | switch (target->i_opcode) { |
7489 | case JUMP_ABSOLUTE: |
7490 | case JUMP_FORWARD: |
7491 | i -= jump_thread(inst, target, POP_JUMP_IF_TRUE); |
7492 | } |
7493 | break; |
7494 | case JUMP_ABSOLUTE: |
7495 | case JUMP_FORWARD: |
7496 | switch (target->i_opcode) { |
7497 | case JUMP_ABSOLUTE: |
7498 | case JUMP_FORWARD: |
7499 | i -= jump_thread(inst, target, JUMP_ABSOLUTE); |
7500 | } |
7501 | break; |
7502 | case FOR_ITER: |
7503 | if (target->i_opcode == JUMP_FORWARD) { |
7504 | i -= jump_thread(inst, target, FOR_ITER); |
7505 | } |
7506 | break; |
7507 | case ROT_N: |
7508 | switch (oparg) { |
7509 | case 0: |
7510 | case 1: |
7511 | inst->i_opcode = NOP; |
7512 | continue; |
7513 | case 2: |
7514 | inst->i_opcode = ROT_TWO; |
7515 | break; |
7516 | case 3: |
7517 | inst->i_opcode = ROT_THREE; |
7518 | break; |
7519 | case 4: |
7520 | inst->i_opcode = ROT_FOUR; |
7521 | break; |
7522 | } |
7523 | if (i >= oparg - 1) { |
7524 | fold_rotations(inst - oparg + 1, oparg); |
7525 | } |
7526 | break; |
7527 | } |
7528 | } |
7529 | return 0; |
7530 | error: |
7531 | return -1; |
7532 | } |
7533 | |
7534 | /* If this block ends with an unconditional jump to an exit block, |
7535 | * then remove the jump and extend this block with the target. |
7536 | */ |
7537 | static int |
7538 | extend_block(basicblock *bb) { |
7539 | if (bb->b_iused == 0) { |
7540 | return 0; |
7541 | } |
7542 | struct instr *last = &bb->b_instr[bb->b_iused-1]; |
7543 | if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) { |
7544 | return 0; |
7545 | } |
7546 | if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) { |
7547 | basicblock *to_copy = last->i_target; |
7548 | last->i_opcode = NOP; |
7549 | for (int i = 0; i < to_copy->b_iused; i++) { |
7550 | int index = compiler_next_instr(bb); |
7551 | if (index < 0) { |
7552 | return -1; |
7553 | } |
7554 | bb->b_instr[index] = to_copy->b_instr[i]; |
7555 | } |
7556 | bb->b_exit = 1; |
7557 | } |
7558 | return 0; |
7559 | } |
7560 | |
7561 | static void |
7562 | clean_basic_block(basicblock *bb, int prev_lineno) { |
7563 | /* Remove NOPs when legal to do so. */ |
7564 | int dest = 0; |
7565 | for (int src = 0; src < bb->b_iused; src++) { |
7566 | int lineno = bb->b_instr[src].i_lineno; |
7567 | if (bb->b_instr[src].i_opcode == NOP) { |
7568 | /* Eliminate no-op if it doesn't have a line number */ |
7569 | if (lineno < 0) { |
7570 | continue; |
7571 | } |
7572 | /* or, if the previous instruction had the same line number. */ |
7573 | if (prev_lineno == lineno) { |
7574 | continue; |
7575 | } |
7576 | /* or, if the next instruction has same line number or no line number */ |
7577 | if (src < bb->b_iused - 1) { |
7578 | int next_lineno = bb->b_instr[src+1].i_lineno; |
7579 | if (next_lineno < 0 || next_lineno == lineno) { |
7580 | bb->b_instr[src+1].i_lineno = lineno; |
7581 | continue; |
7582 | } |
7583 | } |
7584 | else { |
7585 | basicblock* next = bb->b_next; |
7586 | while (next && next->b_iused == 0) { |
7587 | next = next->b_next; |
7588 | } |
7589 | /* or if last instruction in BB and next BB has same line number */ |
7590 | if (next) { |
7591 | if (lineno == next->b_instr[0].i_lineno) { |
7592 | continue; |
7593 | } |
7594 | } |
7595 | } |
7596 | |
7597 | } |
7598 | if (dest != src) { |
7599 | bb->b_instr[dest] = bb->b_instr[src]; |
7600 | } |
7601 | dest++; |
7602 | prev_lineno = lineno; |
7603 | } |
7604 | assert(dest <= bb->b_iused); |
7605 | bb->b_iused = dest; |
7606 | } |
7607 | |
7608 | static int |
7609 | normalize_basic_block(basicblock *bb) { |
7610 | /* Mark blocks as exit and/or nofallthrough. |
7611 | Raise SystemError if CFG is malformed. */ |
7612 | for (int i = 0; i < bb->b_iused; i++) { |
7613 | switch(bb->b_instr[i].i_opcode) { |
7614 | case RETURN_VALUE: |
7615 | case RAISE_VARARGS: |
7616 | case RERAISE: |
7617 | bb->b_exit = 1; |
7618 | bb->b_nofallthrough = 1; |
7619 | break; |
7620 | case JUMP_ABSOLUTE: |
7621 | case JUMP_FORWARD: |
7622 | bb->b_nofallthrough = 1; |
7623 | /* fall through */ |
7624 | case POP_JUMP_IF_FALSE: |
7625 | case POP_JUMP_IF_TRUE: |
7626 | case JUMP_IF_FALSE_OR_POP: |
7627 | case JUMP_IF_TRUE_OR_POP: |
7628 | case FOR_ITER: |
7629 | if (i != bb->b_iused-1) { |
7630 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph." ); |
7631 | return -1; |
7632 | } |
7633 | /* Skip over empty basic blocks. */ |
7634 | while (bb->b_instr[i].i_target->b_iused == 0) { |
7635 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
7636 | } |
7637 | |
7638 | } |
7639 | } |
7640 | return 0; |
7641 | } |
7642 | |
7643 | static int |
7644 | mark_reachable(struct assembler *a) { |
7645 | basicblock **stack, **sp; |
7646 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
7647 | if (stack == NULL) { |
7648 | return -1; |
7649 | } |
7650 | a->a_entry->b_predecessors = 1; |
7651 | *sp++ = a->a_entry; |
7652 | while (sp > stack) { |
7653 | basicblock *b = *(--sp); |
7654 | if (b->b_next && !b->b_nofallthrough) { |
7655 | if (b->b_next->b_predecessors == 0) { |
7656 | *sp++ = b->b_next; |
7657 | } |
7658 | b->b_next->b_predecessors++; |
7659 | } |
7660 | for (int i = 0; i < b->b_iused; i++) { |
7661 | basicblock *target; |
7662 | if (is_jump(&b->b_instr[i])) { |
7663 | target = b->b_instr[i].i_target; |
7664 | if (target->b_predecessors == 0) { |
7665 | *sp++ = target; |
7666 | } |
7667 | target->b_predecessors++; |
7668 | } |
7669 | } |
7670 | } |
7671 | PyObject_Free(stack); |
7672 | return 0; |
7673 | } |
7674 | |
7675 | static void |
7676 | eliminate_empty_basic_blocks(basicblock *entry) { |
7677 | /* Eliminate empty blocks */ |
7678 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
7679 | basicblock *next = b->b_next; |
7680 | if (next) { |
7681 | while (next->b_iused == 0 && next->b_next) { |
7682 | next = next->b_next; |
7683 | } |
7684 | b->b_next = next; |
7685 | } |
7686 | } |
7687 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
7688 | if (b->b_iused == 0) { |
7689 | continue; |
7690 | } |
7691 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
7692 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
7693 | while (target->b_iused == 0) { |
7694 | target = target->b_next; |
7695 | } |
7696 | b->b_instr[b->b_iused-1].i_target = target; |
7697 | } |
7698 | } |
7699 | } |
7700 | |
7701 | |
7702 | /* If an instruction has no line number, but it's predecessor in the BB does, |
7703 | * then copy the line number. If a successor block has no line number, and only |
7704 | * one predecessor, then inherit the line number. |
7705 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
7706 | * Also reduces the size of the line number table, |
7707 | * but has no impact on the generated line number events. |
7708 | */ |
7709 | static void |
7710 | propagate_line_numbers(struct assembler *a) { |
7711 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
7712 | if (b->b_iused == 0) { |
7713 | continue; |
7714 | } |
7715 | int prev_lineno = -1; |
7716 | for (int i = 0; i < b->b_iused; i++) { |
7717 | if (b->b_instr[i].i_lineno < 0) { |
7718 | b->b_instr[i].i_lineno = prev_lineno; |
7719 | } |
7720 | else { |
7721 | prev_lineno = b->b_instr[i].i_lineno; |
7722 | } |
7723 | } |
7724 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
7725 | assert(b->b_next->b_iused); |
7726 | if (b->b_next->b_instr[0].i_lineno < 0) { |
7727 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
7728 | } |
7729 | } |
7730 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
7731 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
7732 | /* Note: Only actual jumps, not exception handlers */ |
7733 | case SETUP_ASYNC_WITH: |
7734 | case SETUP_WITH: |
7735 | case SETUP_FINALLY: |
7736 | continue; |
7737 | } |
7738 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
7739 | if (target->b_predecessors == 1) { |
7740 | if (target->b_instr[0].i_lineno < 0) { |
7741 | target->b_instr[0].i_lineno = prev_lineno; |
7742 | } |
7743 | } |
7744 | } |
7745 | } |
7746 | } |
7747 | |
7748 | /* Perform optimizations on a control flow graph. |
7749 | The consts object should still be in list form to allow new constants |
7750 | to be appended. |
7751 | |
7752 | All transformations keep the code size the same or smaller. |
7753 | For those that reduce size, the gaps are initially filled with |
7754 | NOPs. Later those NOPs are removed. |
7755 | */ |
7756 | |
7757 | static int |
7758 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts) |
7759 | { |
7760 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
7761 | if (optimize_basic_block(c, b, consts)) { |
7762 | return -1; |
7763 | } |
7764 | clean_basic_block(b, -1); |
7765 | assert(b->b_predecessors == 0); |
7766 | } |
7767 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
7768 | if (extend_block(b)) { |
7769 | return -1; |
7770 | } |
7771 | } |
7772 | if (mark_reachable(a)) { |
7773 | return -1; |
7774 | } |
7775 | /* Delete unreachable instructions */ |
7776 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
7777 | if (b->b_predecessors == 0) { |
7778 | b->b_iused = 0; |
7779 | b->b_nofallthrough = 0; |
7780 | } |
7781 | } |
7782 | basicblock *pred = NULL; |
7783 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
7784 | int prev_lineno = -1; |
7785 | if (pred && pred->b_iused) { |
7786 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
7787 | } |
7788 | clean_basic_block(b, prev_lineno); |
7789 | pred = b->b_nofallthrough ? NULL : b; |
7790 | } |
7791 | eliminate_empty_basic_blocks(a->a_entry); |
7792 | /* Delete jump instructions made redundant by previous step. If a non-empty |
7793 | block ends with a jump instruction, check if the next non-empty block |
7794 | reached through normal flow control is the target of that jump. If it |
7795 | is, then the jump instruction is redundant and can be deleted. |
7796 | */ |
7797 | int maybe_empty_blocks = 0; |
7798 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
7799 | if (b->b_iused > 0) { |
7800 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
7801 | if (b_last_instr->i_opcode == JUMP_ABSOLUTE || |
7802 | b_last_instr->i_opcode == JUMP_FORWARD) { |
7803 | if (b_last_instr->i_target == b->b_next) { |
7804 | assert(b->b_next->b_iused); |
7805 | b->b_nofallthrough = 0; |
7806 | b_last_instr->i_opcode = NOP; |
7807 | clean_basic_block(b, -1); |
7808 | maybe_empty_blocks = 1; |
7809 | } |
7810 | } |
7811 | } |
7812 | } |
7813 | if (maybe_empty_blocks) { |
7814 | eliminate_empty_basic_blocks(a->a_entry); |
7815 | } |
7816 | return 0; |
7817 | } |
7818 | |
7819 | // Remove trailing unused constants. |
7820 | static int |
7821 | trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts) |
7822 | { |
7823 | assert(PyList_CheckExact(consts)); |
7824 | |
7825 | // The first constant may be docstring; keep it always. |
7826 | int max_const_index = 0; |
7827 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
7828 | for (int i = 0; i < b->b_iused; i++) { |
7829 | if (b->b_instr[i].i_opcode == LOAD_CONST && |
7830 | b->b_instr[i].i_oparg > max_const_index) { |
7831 | max_const_index = b->b_instr[i].i_oparg; |
7832 | } |
7833 | } |
7834 | } |
7835 | if (max_const_index+1 < PyList_GET_SIZE(consts)) { |
7836 | //fprintf(stderr, "removing trailing consts: max=%d, size=%d\n", |
7837 | // max_const_index, (int)PyList_GET_SIZE(consts)); |
7838 | if (PyList_SetSlice(consts, max_const_index+1, |
7839 | PyList_GET_SIZE(consts), NULL) < 0) { |
7840 | return 1; |
7841 | } |
7842 | } |
7843 | return 0; |
7844 | } |
7845 | |
7846 | static inline int |
7847 | is_exit_without_lineno(basicblock *b) { |
7848 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
7849 | } |
7850 | |
7851 | /* PEP 626 mandates that the f_lineno of a frame is correct |
7852 | * after a frame terminates. It would be prohibitively expensive |
7853 | * to continuously update the f_lineno field at runtime, |
7854 | * so we make sure that all exiting instruction (raises and returns) |
7855 | * have a valid line number, allowing us to compute f_lineno lazily. |
7856 | * We can do this by duplicating the exit blocks without line number |
7857 | * so that none have more than one predecessor. We can then safely |
7858 | * copy the line number from the sole predecessor block. |
7859 | */ |
7860 | static int |
7861 | duplicate_exits_without_lineno(struct compiler *c) |
7862 | { |
7863 | /* Copy all exit blocks without line number that are targets of a jump. |
7864 | */ |
7865 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
7866 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
7867 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
7868 | /* Note: Only actual jumps, not exception handlers */ |
7869 | case SETUP_ASYNC_WITH: |
7870 | case SETUP_WITH: |
7871 | case SETUP_FINALLY: |
7872 | continue; |
7873 | } |
7874 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
7875 | if (is_exit_without_lineno(target) && target->b_predecessors > 1) { |
7876 | basicblock *new_target = compiler_copy_block(c, target); |
7877 | if (new_target == NULL) { |
7878 | return -1; |
7879 | } |
7880 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
7881 | b->b_instr[b->b_iused-1].i_target = new_target; |
7882 | target->b_predecessors--; |
7883 | new_target->b_predecessors = 1; |
7884 | new_target->b_next = target->b_next; |
7885 | target->b_next = new_target; |
7886 | } |
7887 | } |
7888 | } |
7889 | /* Eliminate empty blocks */ |
7890 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
7891 | while (b->b_next && b->b_next->b_iused == 0) { |
7892 | b->b_next = b->b_next->b_next; |
7893 | } |
7894 | } |
7895 | /* Any remaining reachable exit blocks without line number can only be reached by |
7896 | * fall through, and thus can only have a single predecessor */ |
7897 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
7898 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
7899 | if (is_exit_without_lineno(b->b_next)) { |
7900 | assert(b->b_next->b_iused > 0); |
7901 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
7902 | } |
7903 | } |
7904 | } |
7905 | return 0; |
7906 | } |
7907 | |
7908 | |
7909 | /* Retained for API compatibility. |
7910 | * Optimization is now done in optimize_cfg */ |
7911 | |
7912 | PyObject * |
7913 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
7914 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
7915 | { |
7916 | Py_INCREF(code); |
7917 | return code; |
7918 | } |
7919 | |