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). See peephole.c
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 "Python.h"
25
26 #include "Python-ast.h"
27 #include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */
28 #include "ast.h"
29 #include "code.h"
30 #include "symtable.h"
31 #include "opcode.h"
32 #include "wordcode_helpers.h"
33
34 #define DEFAULT_BLOCK_SIZE 16
35 #define DEFAULT_BLOCKS 8
36 #define DEFAULT_CODE_SIZE 128
37 #define DEFAULT_LNOTAB_SIZE 16
38
39 #define COMP_GENEXP 0
40 #define COMP_LISTCOMP 1
41 #define COMP_SETCOMP 2
42 #define COMP_DICTCOMP 3
43
44 struct instr {
45 unsigned i_jabs : 1;
46 unsigned i_jrel : 1;
47 unsigned char i_opcode;
48 int i_oparg;
49 struct basicblock_ *i_target; /* target block (if jump instruction) */
50 int i_lineno;
51 };
52
53 typedef struct basicblock_ {
54 /* Each basicblock in a compilation unit is linked via b_list in the
55 reverse order that the block are allocated. b_list points to the next
56 block, not to be confused with b_next, which is next by control flow. */
57 struct basicblock_ *b_list;
58 /* number of instructions used */
59 int b_iused;
60 /* length of instruction array (b_instr) */
61 int b_ialloc;
62 /* pointer to an array of instructions, initially NULL */
63 struct instr *b_instr;
64 /* If b_next is non-NULL, it is a pointer to the next
65 block reached by normal control flow. */
66 struct basicblock_ *b_next;
67 /* b_seen is used to perform a DFS of basicblocks. */
68 unsigned b_seen : 1;
69 /* b_return is true if a RETURN_VALUE opcode is inserted. */
70 unsigned b_return : 1;
71 /* depth of stack upon entry of block, computed by stackdepth() */
72 int b_startdepth;
73 /* instruction offset for block, computed by assemble_jump_offsets() */
74 int b_offset;
75 } basicblock;
76
77 /* fblockinfo tracks the current frame block.
78
79 A frame block is used to handle loops, try/except, and try/finally.
80 It's called a frame block to distinguish it from a basic block in the
81 compiler IR.
82 */
83
84 enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_TRY2, FINALLY_END,
85 WITH, ASYNC_WITH, HANDLER_CLEANUP };
86
87 struct fblockinfo {
88 enum fblocktype fb_type;
89 basicblock *fb_block;
90 /* (optional) type-specific exit or cleanup block */
91 basicblock *fb_exit;
92 };
93
94 enum {
95 COMPILER_SCOPE_MODULE,
96 COMPILER_SCOPE_CLASS,
97 COMPILER_SCOPE_FUNCTION,
98 COMPILER_SCOPE_ASYNC_FUNCTION,
99 COMPILER_SCOPE_LAMBDA,
100 COMPILER_SCOPE_COMPREHENSION,
101 };
102
103 /* The following items change on entry and exit of code blocks.
104 They must be saved and restored when returning to a block.
105 */
106 struct compiler_unit {
107 PySTEntryObject *u_ste;
108
109 PyObject *u_name;
110 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
111 int u_scope_type;
112
113 /* The following fields are dicts that map objects to
114 the index of them in co_XXX. The index is used as
115 the argument for opcodes that refer to those collections.
116 */
117 PyObject *u_consts; /* all constants */
118 PyObject *u_names; /* all names */
119 PyObject *u_varnames; /* local variables */
120 PyObject *u_cellvars; /* cell variables */
121 PyObject *u_freevars; /* free variables */
122
123 PyObject *u_private; /* for private name mangling */
124
125 Py_ssize_t u_argcount; /* number of arguments for block */
126 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
127 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
128 /* Pointer to the most recently allocated block. By following b_list
129 members, you can reach all early allocated blocks. */
130 basicblock *u_blocks;
131 basicblock *u_curblock; /* pointer to current block */
132
133 int u_nfblocks;
134 struct fblockinfo u_fblock[CO_MAXBLOCKS];
135
136 int u_firstlineno; /* the first lineno of the block */
137 int u_lineno; /* the lineno for the current stmt */
138 int u_col_offset; /* the offset of the current stmt */
139 int u_lineno_set; /* boolean to indicate whether instr
140 has been generated with current lineno */
141 };
142
143 /* This struct captures the global state of a compilation.
144
145 The u pointer points to the current compilation unit, while units
146 for enclosing blocks are stored in c_stack. The u and c_stack are
147 managed by compiler_enter_scope() and compiler_exit_scope().
148
149 Note that we don't track recursion levels during compilation - the
150 task of detecting and rejecting excessive levels of nesting is
151 handled by the symbol analysis pass.
152
153 */
154
155 struct compiler {
156 PyObject *c_filename;
157 struct symtable *c_st;
158 PyFutureFeatures *c_future; /* pointer to module's __future__ */
159 PyCompilerFlags *c_flags;
160
161 int c_optimize; /* optimization level */
162 int c_interactive; /* true if in interactive mode */
163 int c_nestlevel;
164 int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode
165 if this value is different from zero.
166 This can be used to temporarily visit
167 nodes without emitting bytecode to
168 check only errors. */
169
170 PyObject *c_const_cache; /* Python dict holding all constants,
171 including names tuple */
172 struct compiler_unit *u; /* compiler state for current block */
173 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
174 PyArena *c_arena; /* pointer to memory allocation arena */
175 };
176
177 static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
178 static void compiler_free(struct compiler *);
179 static basicblock *compiler_new_block(struct compiler *);
180 static int compiler_next_instr(struct compiler *, basicblock *);
181 static int compiler_addop(struct compiler *, int);
182 static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
183 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
184 static int compiler_error(struct compiler *, const char *);
185 static int compiler_warn(struct compiler *, const char *, ...);
186 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
187
188 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
189 static int compiler_visit_stmt(struct compiler *, stmt_ty);
190 static int compiler_visit_keyword(struct compiler *, keyword_ty);
191 static int compiler_visit_expr(struct compiler *, expr_ty);
192 static int compiler_augassign(struct compiler *, stmt_ty);
193 static int compiler_annassign(struct compiler *, stmt_ty);
194 static int compiler_visit_slice(struct compiler *, slice_ty,
195 expr_context_ty);
196
197 static int inplace_binop(struct compiler *, operator_ty);
198 static int expr_constant(expr_ty);
199
200 static int compiler_with(struct compiler *, stmt_ty, int);
201 static int compiler_async_with(struct compiler *, stmt_ty, int);
202 static int compiler_async_for(struct compiler *, stmt_ty);
203 static int compiler_call_helper(struct compiler *c, int n,
204 asdl_seq *args,
205 asdl_seq *keywords);
206 static int compiler_try_except(struct compiler *, stmt_ty);
207 static int compiler_set_qualname(struct compiler *);
208
209 static int compiler_sync_comprehension_generator(
210 struct compiler *c,
211 asdl_seq *generators, int gen_index,
212 expr_ty elt, expr_ty val, int type);
213
214 static int compiler_async_comprehension_generator(
215 struct compiler *c,
216 asdl_seq *generators, int gen_index,
217 expr_ty elt, expr_ty val, int type);
218
219 static PyCodeObject *assemble(struct compiler *, int addNone);
220 static PyObject *__doc__, *__annotations__;
221
222 #define CAPSULE_NAME "compile.c compiler unit"
223
224 PyObject *
_Py_Mangle(PyObject * privateobj,PyObject * ident)225 _Py_Mangle(PyObject *privateobj, PyObject *ident)
226 {
227 /* Name mangling: __private becomes _classname__private.
228 This is independent from how the name is used. */
229 PyObject *result;
230 size_t nlen, plen, ipriv;
231 Py_UCS4 maxchar;
232 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
233 PyUnicode_READ_CHAR(ident, 0) != '_' ||
234 PyUnicode_READ_CHAR(ident, 1) != '_') {
235 Py_INCREF(ident);
236 return ident;
237 }
238 nlen = PyUnicode_GET_LENGTH(ident);
239 plen = PyUnicode_GET_LENGTH(privateobj);
240 /* Don't mangle __id__ or names with dots.
241
242 The only time a name with a dot can occur is when
243 we are compiling an import statement that has a
244 package name.
245
246 TODO(jhylton): Decide whether we want to support
247 mangling of the module name, e.g. __M.X.
248 */
249 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
250 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
251 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
252 Py_INCREF(ident);
253 return ident; /* Don't mangle __whatever__ */
254 }
255 /* Strip leading underscores from class name */
256 ipriv = 0;
257 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
258 ipriv++;
259 if (ipriv == plen) {
260 Py_INCREF(ident);
261 return ident; /* Don't mangle if class is just underscores */
262 }
263 plen -= ipriv;
264
265 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
266 PyErr_SetString(PyExc_OverflowError,
267 "private identifier too large to be mangled");
268 return NULL;
269 }
270
271 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
272 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
273 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
274
275 result = PyUnicode_New(1 + nlen + plen, maxchar);
276 if (!result)
277 return 0;
278 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
279 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
280 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
281 Py_DECREF(result);
282 return NULL;
283 }
284 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
285 Py_DECREF(result);
286 return NULL;
287 }
288 assert(_PyUnicode_CheckConsistency(result, 1));
289 return result;
290 }
291
292 static int
compiler_init(struct compiler * c)293 compiler_init(struct compiler *c)
294 {
295 memset(c, 0, sizeof(struct compiler));
296
297 c->c_const_cache = PyDict_New();
298 if (!c->c_const_cache) {
299 return 0;
300 }
301
302 c->c_stack = PyList_New(0);
303 if (!c->c_stack) {
304 Py_CLEAR(c->c_const_cache);
305 return 0;
306 }
307
308 return 1;
309 }
310
311 PyCodeObject *
PyAST_CompileObject(mod_ty mod,PyObject * filename,PyCompilerFlags * flags,int optimize,PyArena * arena)312 PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
313 int optimize, PyArena *arena)
314 {
315 struct compiler c;
316 PyCodeObject *co = NULL;
317 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
318 int merged;
319 PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
320
321 if (!__doc__) {
322 __doc__ = PyUnicode_InternFromString("__doc__");
323 if (!__doc__)
324 return NULL;
325 }
326 if (!__annotations__) {
327 __annotations__ = PyUnicode_InternFromString("__annotations__");
328 if (!__annotations__)
329 return NULL;
330 }
331 if (!compiler_init(&c))
332 return NULL;
333 Py_INCREF(filename);
334 c.c_filename = filename;
335 c.c_arena = arena;
336 c.c_future = PyFuture_FromASTObject(mod, filename);
337 if (c.c_future == NULL)
338 goto finally;
339 if (!flags) {
340 flags = &local_flags;
341 }
342 merged = c.c_future->ff_features | flags->cf_flags;
343 c.c_future->ff_features = merged;
344 flags->cf_flags = merged;
345 c.c_flags = flags;
346 c.c_optimize = (optimize == -1) ? config->optimization_level : optimize;
347 c.c_nestlevel = 0;
348 c.c_do_not_emit_bytecode = 0;
349
350 if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {
351 goto finally;
352 }
353
354 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
355 if (c.c_st == NULL) {
356 if (!PyErr_Occurred())
357 PyErr_SetString(PyExc_SystemError, "no symtable");
358 goto finally;
359 }
360
361 co = compiler_mod(&c, mod);
362
363 finally:
364 compiler_free(&c);
365 assert(co || PyErr_Occurred());
366 return co;
367 }
368
369 PyCodeObject *
PyAST_CompileEx(mod_ty mod,const char * filename_str,PyCompilerFlags * flags,int optimize,PyArena * arena)370 PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
371 int optimize, PyArena *arena)
372 {
373 PyObject *filename;
374 PyCodeObject *co;
375 filename = PyUnicode_DecodeFSDefault(filename_str);
376 if (filename == NULL)
377 return NULL;
378 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
379 Py_DECREF(filename);
380 return co;
381
382 }
383
384 PyCodeObject *
PyNode_Compile(struct _node * n,const char * filename)385 PyNode_Compile(struct _node *n, const char *filename)
386 {
387 PyCodeObject *co = NULL;
388 mod_ty mod;
389 PyArena *arena = PyArena_New();
390 if (!arena)
391 return NULL;
392 mod = PyAST_FromNode(n, NULL, filename, arena);
393 if (mod)
394 co = PyAST_Compile(mod, filename, NULL, arena);
395 PyArena_Free(arena);
396 return co;
397 }
398
399 static void
compiler_free(struct compiler * c)400 compiler_free(struct compiler *c)
401 {
402 if (c->c_st)
403 PySymtable_Free(c->c_st);
404 if (c->c_future)
405 PyObject_Free(c->c_future);
406 Py_XDECREF(c->c_filename);
407 Py_DECREF(c->c_const_cache);
408 Py_DECREF(c->c_stack);
409 }
410
411 static PyObject *
list2dict(PyObject * list)412 list2dict(PyObject *list)
413 {
414 Py_ssize_t i, n;
415 PyObject *v, *k;
416 PyObject *dict = PyDict_New();
417 if (!dict) return NULL;
418
419 n = PyList_Size(list);
420 for (i = 0; i < n; i++) {
421 v = PyLong_FromSsize_t(i);
422 if (!v) {
423 Py_DECREF(dict);
424 return NULL;
425 }
426 k = PyList_GET_ITEM(list, i);
427 if (PyDict_SetItem(dict, k, v) < 0) {
428 Py_DECREF(v);
429 Py_DECREF(dict);
430 return NULL;
431 }
432 Py_DECREF(v);
433 }
434 return dict;
435 }
436
437 /* Return new dict containing names from src that match scope(s).
438
439 src is a symbol table dictionary. If the scope of a name matches
440 either scope_type or flag is set, insert it into the new dict. The
441 values are integers, starting at offset and increasing by one for
442 each key.
443 */
444
445 static PyObject *
dictbytype(PyObject * src,int scope_type,int flag,Py_ssize_t offset)446 dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
447 {
448 Py_ssize_t i = offset, scope, num_keys, key_i;
449 PyObject *k, *v, *dest = PyDict_New();
450 PyObject *sorted_keys;
451
452 assert(offset >= 0);
453 if (dest == NULL)
454 return NULL;
455
456 /* Sort the keys so that we have a deterministic order on the indexes
457 saved in the returned dictionary. These indexes are used as indexes
458 into the free and cell var storage. Therefore if they aren't
459 deterministic, then the generated bytecode is not deterministic.
460 */
461 sorted_keys = PyDict_Keys(src);
462 if (sorted_keys == NULL)
463 return NULL;
464 if (PyList_Sort(sorted_keys) != 0) {
465 Py_DECREF(sorted_keys);
466 return NULL;
467 }
468 num_keys = PyList_GET_SIZE(sorted_keys);
469
470 for (key_i = 0; key_i < num_keys; key_i++) {
471 /* XXX this should probably be a macro in symtable.h */
472 long vi;
473 k = PyList_GET_ITEM(sorted_keys, key_i);
474 v = PyDict_GetItem(src, k);
475 assert(PyLong_Check(v));
476 vi = PyLong_AS_LONG(v);
477 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
478
479 if (scope == scope_type || vi & flag) {
480 PyObject *item = PyLong_FromSsize_t(i);
481 if (item == NULL) {
482 Py_DECREF(sorted_keys);
483 Py_DECREF(dest);
484 return NULL;
485 }
486 i++;
487 if (PyDict_SetItem(dest, k, item) < 0) {
488 Py_DECREF(sorted_keys);
489 Py_DECREF(item);
490 Py_DECREF(dest);
491 return NULL;
492 }
493 Py_DECREF(item);
494 }
495 }
496 Py_DECREF(sorted_keys);
497 return dest;
498 }
499
500 static void
compiler_unit_check(struct compiler_unit * u)501 compiler_unit_check(struct compiler_unit *u)
502 {
503 basicblock *block;
504 for (block = u->u_blocks; block != NULL; block = block->b_list) {
505 assert((uintptr_t)block != 0xcbcbcbcbU);
506 assert((uintptr_t)block != 0xfbfbfbfbU);
507 assert((uintptr_t)block != 0xdbdbdbdbU);
508 if (block->b_instr != NULL) {
509 assert(block->b_ialloc > 0);
510 assert(block->b_iused > 0);
511 assert(block->b_ialloc >= block->b_iused);
512 }
513 else {
514 assert (block->b_iused == 0);
515 assert (block->b_ialloc == 0);
516 }
517 }
518 }
519
520 static void
compiler_unit_free(struct compiler_unit * u)521 compiler_unit_free(struct compiler_unit *u)
522 {
523 basicblock *b, *next;
524
525 compiler_unit_check(u);
526 b = u->u_blocks;
527 while (b != NULL) {
528 if (b->b_instr)
529 PyObject_Free((void *)b->b_instr);
530 next = b->b_list;
531 PyObject_Free((void *)b);
532 b = next;
533 }
534 Py_CLEAR(u->u_ste);
535 Py_CLEAR(u->u_name);
536 Py_CLEAR(u->u_qualname);
537 Py_CLEAR(u->u_consts);
538 Py_CLEAR(u->u_names);
539 Py_CLEAR(u->u_varnames);
540 Py_CLEAR(u->u_freevars);
541 Py_CLEAR(u->u_cellvars);
542 Py_CLEAR(u->u_private);
543 PyObject_Free(u);
544 }
545
546 static int
compiler_enter_scope(struct compiler * c,identifier name,int scope_type,void * key,int lineno)547 compiler_enter_scope(struct compiler *c, identifier name,
548 int scope_type, void *key, int lineno)
549 {
550 struct compiler_unit *u;
551 basicblock *block;
552
553 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
554 struct compiler_unit));
555 if (!u) {
556 PyErr_NoMemory();
557 return 0;
558 }
559 memset(u, 0, sizeof(struct compiler_unit));
560 u->u_scope_type = scope_type;
561 u->u_argcount = 0;
562 u->u_posonlyargcount = 0;
563 u->u_kwonlyargcount = 0;
564 u->u_ste = PySymtable_Lookup(c->c_st, key);
565 if (!u->u_ste) {
566 compiler_unit_free(u);
567 return 0;
568 }
569 Py_INCREF(name);
570 u->u_name = name;
571 u->u_varnames = list2dict(u->u_ste->ste_varnames);
572 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
573 if (!u->u_varnames || !u->u_cellvars) {
574 compiler_unit_free(u);
575 return 0;
576 }
577 if (u->u_ste->ste_needs_class_closure) {
578 /* Cook up an implicit __class__ cell. */
579 _Py_IDENTIFIER(__class__);
580 PyObject *name;
581 int res;
582 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
583 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
584 name = _PyUnicode_FromId(&PyId___class__);
585 if (!name) {
586 compiler_unit_free(u);
587 return 0;
588 }
589 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero);
590 if (res < 0) {
591 compiler_unit_free(u);
592 return 0;
593 }
594 }
595
596 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
597 PyDict_GET_SIZE(u->u_cellvars));
598 if (!u->u_freevars) {
599 compiler_unit_free(u);
600 return 0;
601 }
602
603 u->u_blocks = NULL;
604 u->u_nfblocks = 0;
605 u->u_firstlineno = lineno;
606 u->u_lineno = 0;
607 u->u_col_offset = 0;
608 u->u_lineno_set = 0;
609 u->u_consts = PyDict_New();
610 if (!u->u_consts) {
611 compiler_unit_free(u);
612 return 0;
613 }
614 u->u_names = PyDict_New();
615 if (!u->u_names) {
616 compiler_unit_free(u);
617 return 0;
618 }
619
620 u->u_private = NULL;
621
622 /* Push the old compiler_unit on the stack. */
623 if (c->u) {
624 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
625 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
626 Py_XDECREF(capsule);
627 compiler_unit_free(u);
628 return 0;
629 }
630 Py_DECREF(capsule);
631 u->u_private = c->u->u_private;
632 Py_XINCREF(u->u_private);
633 }
634 c->u = u;
635
636 c->c_nestlevel++;
637
638 block = compiler_new_block(c);
639 if (block == NULL)
640 return 0;
641 c->u->u_curblock = block;
642
643 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
644 if (!compiler_set_qualname(c))
645 return 0;
646 }
647
648 return 1;
649 }
650
651 static void
compiler_exit_scope(struct compiler * c)652 compiler_exit_scope(struct compiler *c)
653 {
654 Py_ssize_t n;
655 PyObject *capsule;
656
657 c->c_nestlevel--;
658 compiler_unit_free(c->u);
659 /* Restore c->u to the parent unit. */
660 n = PyList_GET_SIZE(c->c_stack) - 1;
661 if (n >= 0) {
662 capsule = PyList_GET_ITEM(c->c_stack, n);
663 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
664 assert(c->u);
665 /* we are deleting from a list so this really shouldn't fail */
666 if (PySequence_DelItem(c->c_stack, n) < 0)
667 Py_FatalError("compiler_exit_scope()");
668 compiler_unit_check(c->u);
669 }
670 else
671 c->u = NULL;
672
673 }
674
675 static int
compiler_set_qualname(struct compiler * c)676 compiler_set_qualname(struct compiler *c)
677 {
678 _Py_static_string(dot, ".");
679 _Py_static_string(dot_locals, ".<locals>");
680 Py_ssize_t stack_size;
681 struct compiler_unit *u = c->u;
682 PyObject *name, *base, *dot_str, *dot_locals_str;
683
684 base = NULL;
685 stack_size = PyList_GET_SIZE(c->c_stack);
686 assert(stack_size >= 1);
687 if (stack_size > 1) {
688 int scope, force_global = 0;
689 struct compiler_unit *parent;
690 PyObject *mangled, *capsule;
691
692 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
693 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
694 assert(parent);
695
696 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
697 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
698 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
699 assert(u->u_name);
700 mangled = _Py_Mangle(parent->u_private, u->u_name);
701 if (!mangled)
702 return 0;
703 scope = PyST_GetScope(parent->u_ste, mangled);
704 Py_DECREF(mangled);
705 assert(scope != GLOBAL_IMPLICIT);
706 if (scope == GLOBAL_EXPLICIT)
707 force_global = 1;
708 }
709
710 if (!force_global) {
711 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
712 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
713 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
714 dot_locals_str = _PyUnicode_FromId(&dot_locals);
715 if (dot_locals_str == NULL)
716 return 0;
717 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
718 if (base == NULL)
719 return 0;
720 }
721 else {
722 Py_INCREF(parent->u_qualname);
723 base = parent->u_qualname;
724 }
725 }
726 }
727
728 if (base != NULL) {
729 dot_str = _PyUnicode_FromId(&dot);
730 if (dot_str == NULL) {
731 Py_DECREF(base);
732 return 0;
733 }
734 name = PyUnicode_Concat(base, dot_str);
735 Py_DECREF(base);
736 if (name == NULL)
737 return 0;
738 PyUnicode_Append(&name, u->u_name);
739 if (name == NULL)
740 return 0;
741 }
742 else {
743 Py_INCREF(u->u_name);
744 name = u->u_name;
745 }
746 u->u_qualname = name;
747
748 return 1;
749 }
750
751
752 /* Allocate a new block and return a pointer to it.
753 Returns NULL on error.
754 */
755
756 static basicblock *
compiler_new_block(struct compiler * c)757 compiler_new_block(struct compiler *c)
758 {
759 basicblock *b;
760 struct compiler_unit *u;
761
762 u = c->u;
763 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
764 if (b == NULL) {
765 PyErr_NoMemory();
766 return NULL;
767 }
768 memset((void *)b, 0, sizeof(basicblock));
769 /* Extend the singly linked list of blocks with new block. */
770 b->b_list = u->u_blocks;
771 u->u_blocks = b;
772 return b;
773 }
774
775 static basicblock *
compiler_next_block(struct compiler * c)776 compiler_next_block(struct compiler *c)
777 {
778 basicblock *block = compiler_new_block(c);
779 if (block == NULL)
780 return NULL;
781 c->u->u_curblock->b_next = block;
782 c->u->u_curblock = block;
783 return block;
784 }
785
786 static basicblock *
compiler_use_next_block(struct compiler * c,basicblock * block)787 compiler_use_next_block(struct compiler *c, basicblock *block)
788 {
789 assert(block != NULL);
790 c->u->u_curblock->b_next = block;
791 c->u->u_curblock = block;
792 return block;
793 }
794
795 /* Returns the offset of the next instruction in the current block's
796 b_instr array. Resizes the b_instr as necessary.
797 Returns -1 on failure.
798 */
799
800 static int
compiler_next_instr(struct compiler * c,basicblock * b)801 compiler_next_instr(struct compiler *c, basicblock *b)
802 {
803 assert(b != NULL);
804 if (b->b_instr == NULL) {
805 b->b_instr = (struct instr *)PyObject_Malloc(
806 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
807 if (b->b_instr == NULL) {
808 PyErr_NoMemory();
809 return -1;
810 }
811 b->b_ialloc = DEFAULT_BLOCK_SIZE;
812 memset((char *)b->b_instr, 0,
813 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
814 }
815 else if (b->b_iused == b->b_ialloc) {
816 struct instr *tmp;
817 size_t oldsize, newsize;
818 oldsize = b->b_ialloc * sizeof(struct instr);
819 newsize = oldsize << 1;
820
821 if (oldsize > (SIZE_MAX >> 1)) {
822 PyErr_NoMemory();
823 return -1;
824 }
825
826 if (newsize == 0) {
827 PyErr_NoMemory();
828 return -1;
829 }
830 b->b_ialloc <<= 1;
831 tmp = (struct instr *)PyObject_Realloc(
832 (void *)b->b_instr, newsize);
833 if (tmp == NULL) {
834 PyErr_NoMemory();
835 return -1;
836 }
837 b->b_instr = tmp;
838 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
839 }
840 return b->b_iused++;
841 }
842
843 /* Set the i_lineno member of the instruction at offset off if the
844 line number for the current expression/statement has not
845 already been set. If it has been set, the call has no effect.
846
847 The line number is reset in the following cases:
848 - when entering a new scope
849 - on each statement
850 - on each expression that start a new line
851 - before the "except" and "finally" clauses
852 - before the "for" and "while" expressions
853 */
854
855 static void
compiler_set_lineno(struct compiler * c,int off)856 compiler_set_lineno(struct compiler *c, int off)
857 {
858 basicblock *b;
859 if (c->u->u_lineno_set)
860 return;
861 c->u->u_lineno_set = 1;
862 b = c->u->u_curblock;
863 b->b_instr[off].i_lineno = c->u->u_lineno;
864 }
865
866 /* Return the stack effect of opcode with argument oparg.
867
868 Some opcodes have different stack effect when jump to the target and
869 when not jump. The 'jump' parameter specifies the case:
870
871 * 0 -- when not jump
872 * 1 -- when jump
873 * -1 -- maximal
874 */
875 /* XXX Make the stack effect of WITH_CLEANUP_START and
876 WITH_CLEANUP_FINISH deterministic. */
877 static int
stack_effect(int opcode,int oparg,int jump)878 stack_effect(int opcode, int oparg, int jump)
879 {
880 switch (opcode) {
881 case NOP:
882 case EXTENDED_ARG:
883 return 0;
884
885 /* Stack manipulation */
886 case POP_TOP:
887 return -1;
888 case ROT_TWO:
889 case ROT_THREE:
890 case ROT_FOUR:
891 return 0;
892 case DUP_TOP:
893 return 1;
894 case DUP_TOP_TWO:
895 return 2;
896
897 /* Unary operators */
898 case UNARY_POSITIVE:
899 case UNARY_NEGATIVE:
900 case UNARY_NOT:
901 case UNARY_INVERT:
902 return 0;
903
904 case SET_ADD:
905 case LIST_APPEND:
906 return -1;
907 case MAP_ADD:
908 return -2;
909
910 /* Binary operators */
911 case BINARY_POWER:
912 case BINARY_MULTIPLY:
913 case BINARY_MATRIX_MULTIPLY:
914 case BINARY_MODULO:
915 case BINARY_ADD:
916 case BINARY_SUBTRACT:
917 case BINARY_SUBSCR:
918 case BINARY_FLOOR_DIVIDE:
919 case BINARY_TRUE_DIVIDE:
920 return -1;
921 case INPLACE_FLOOR_DIVIDE:
922 case INPLACE_TRUE_DIVIDE:
923 return -1;
924
925 case INPLACE_ADD:
926 case INPLACE_SUBTRACT:
927 case INPLACE_MULTIPLY:
928 case INPLACE_MATRIX_MULTIPLY:
929 case INPLACE_MODULO:
930 return -1;
931 case STORE_SUBSCR:
932 return -3;
933 case DELETE_SUBSCR:
934 return -2;
935
936 case BINARY_LSHIFT:
937 case BINARY_RSHIFT:
938 case BINARY_AND:
939 case BINARY_XOR:
940 case BINARY_OR:
941 return -1;
942 case INPLACE_POWER:
943 return -1;
944 case GET_ITER:
945 return 0;
946
947 case PRINT_EXPR:
948 return -1;
949 case LOAD_BUILD_CLASS:
950 return 1;
951 case INPLACE_LSHIFT:
952 case INPLACE_RSHIFT:
953 case INPLACE_AND:
954 case INPLACE_XOR:
955 case INPLACE_OR:
956 return -1;
957
958 case SETUP_WITH:
959 /* 1 in the normal flow.
960 * Restore the stack position and push 6 values before jumping to
961 * the handler if an exception be raised. */
962 return jump ? 6 : 1;
963 case WITH_CLEANUP_START:
964 return 2; /* or 1, depending on TOS */
965 case WITH_CLEANUP_FINISH:
966 /* Pop a variable number of values pushed by WITH_CLEANUP_START
967 * + __exit__ or __aexit__. */
968 return -3;
969 case RETURN_VALUE:
970 return -1;
971 case IMPORT_STAR:
972 return -1;
973 case SETUP_ANNOTATIONS:
974 return 0;
975 case YIELD_VALUE:
976 return 0;
977 case YIELD_FROM:
978 return -1;
979 case POP_BLOCK:
980 return 0;
981 case POP_EXCEPT:
982 return -3;
983 case END_FINALLY:
984 case POP_FINALLY:
985 /* Pop 6 values when an exception was raised. */
986 return -6;
987
988 case STORE_NAME:
989 return -1;
990 case DELETE_NAME:
991 return 0;
992 case UNPACK_SEQUENCE:
993 return oparg-1;
994 case UNPACK_EX:
995 return (oparg&0xFF) + (oparg>>8);
996 case FOR_ITER:
997 /* -1 at end of iterator, 1 if continue iterating. */
998 return jump > 0 ? -1 : 1;
999
1000 case STORE_ATTR:
1001 return -2;
1002 case DELETE_ATTR:
1003 return -1;
1004 case STORE_GLOBAL:
1005 return -1;
1006 case DELETE_GLOBAL:
1007 return 0;
1008 case LOAD_CONST:
1009 return 1;
1010 case LOAD_NAME:
1011 return 1;
1012 case BUILD_TUPLE:
1013 case BUILD_LIST:
1014 case BUILD_SET:
1015 case BUILD_STRING:
1016 return 1-oparg;
1017 case BUILD_LIST_UNPACK:
1018 case BUILD_TUPLE_UNPACK:
1019 case BUILD_TUPLE_UNPACK_WITH_CALL:
1020 case BUILD_SET_UNPACK:
1021 case BUILD_MAP_UNPACK:
1022 case BUILD_MAP_UNPACK_WITH_CALL:
1023 return 1 - oparg;
1024 case BUILD_MAP:
1025 return 1 - 2*oparg;
1026 case BUILD_CONST_KEY_MAP:
1027 return -oparg;
1028 case LOAD_ATTR:
1029 return 0;
1030 case COMPARE_OP:
1031 return -1;
1032 case IMPORT_NAME:
1033 return -1;
1034 case IMPORT_FROM:
1035 return 1;
1036
1037 /* Jumps */
1038 case JUMP_FORWARD:
1039 case JUMP_ABSOLUTE:
1040 return 0;
1041
1042 case JUMP_IF_TRUE_OR_POP:
1043 case JUMP_IF_FALSE_OR_POP:
1044 return jump ? 0 : -1;
1045
1046 case POP_JUMP_IF_FALSE:
1047 case POP_JUMP_IF_TRUE:
1048 return -1;
1049
1050 case LOAD_GLOBAL:
1051 return 1;
1052
1053 /* Exception handling */
1054 case SETUP_FINALLY:
1055 /* 0 in the normal flow.
1056 * Restore the stack position and push 6 values before jumping to
1057 * the handler if an exception be raised. */
1058 return jump ? 6 : 0;
1059 case BEGIN_FINALLY:
1060 /* Actually pushes 1 value, but count 6 for balancing with
1061 * END_FINALLY and POP_FINALLY.
1062 * This is the main reason of using this opcode instead of
1063 * "LOAD_CONST None". */
1064 return 6;
1065 case CALL_FINALLY:
1066 return jump ? 1 : 0;
1067
1068 case LOAD_FAST:
1069 return 1;
1070 case STORE_FAST:
1071 return -1;
1072 case DELETE_FAST:
1073 return 0;
1074
1075 case RAISE_VARARGS:
1076 return -oparg;
1077
1078 /* Functions and calls */
1079 case CALL_FUNCTION:
1080 return -oparg;
1081 case CALL_METHOD:
1082 return -oparg-1;
1083 case CALL_FUNCTION_KW:
1084 return -oparg-1;
1085 case CALL_FUNCTION_EX:
1086 return -1 - ((oparg & 0x01) != 0);
1087 case MAKE_FUNCTION:
1088 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1089 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
1090 case BUILD_SLICE:
1091 if (oparg == 3)
1092 return -2;
1093 else
1094 return -1;
1095
1096 /* Closures */
1097 case LOAD_CLOSURE:
1098 return 1;
1099 case LOAD_DEREF:
1100 case LOAD_CLASSDEREF:
1101 return 1;
1102 case STORE_DEREF:
1103 return -1;
1104 case DELETE_DEREF:
1105 return 0;
1106
1107 /* Iterators and generators */
1108 case GET_AWAITABLE:
1109 return 0;
1110 case SETUP_ASYNC_WITH:
1111 /* 0 in the normal flow.
1112 * Restore the stack position to the position before the result
1113 * of __aenter__ and push 6 values before jumping to the handler
1114 * if an exception be raised. */
1115 return jump ? -1 + 6 : 0;
1116 case BEFORE_ASYNC_WITH:
1117 return 1;
1118 case GET_AITER:
1119 return 0;
1120 case GET_ANEXT:
1121 return 1;
1122 case GET_YIELD_FROM_ITER:
1123 return 0;
1124 case END_ASYNC_FOR:
1125 return -7;
1126 case FORMAT_VALUE:
1127 /* If there's a fmt_spec on the stack, we go from 2->1,
1128 else 1->1. */
1129 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
1130 case LOAD_METHOD:
1131 return 1;
1132 default:
1133 return PY_INVALID_STACK_EFFECT;
1134 }
1135 return PY_INVALID_STACK_EFFECT; /* not reachable */
1136 }
1137
1138 int
PyCompile_OpcodeStackEffectWithJump(int opcode,int oparg,int jump)1139 PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1140 {
1141 return stack_effect(opcode, oparg, jump);
1142 }
1143
1144 int
PyCompile_OpcodeStackEffect(int opcode,int oparg)1145 PyCompile_OpcodeStackEffect(int opcode, int oparg)
1146 {
1147 return stack_effect(opcode, oparg, -1);
1148 }
1149
1150 /* Add an opcode with no argument.
1151 Returns 0 on failure, 1 on success.
1152 */
1153
1154 static int
compiler_addop(struct compiler * c,int opcode)1155 compiler_addop(struct compiler *c, int opcode)
1156 {
1157 basicblock *b;
1158 struct instr *i;
1159 int off;
1160 assert(!HAS_ARG(opcode));
1161 if (c->c_do_not_emit_bytecode) {
1162 return 1;
1163 }
1164 off = compiler_next_instr(c, c->u->u_curblock);
1165 if (off < 0)
1166 return 0;
1167 b = c->u->u_curblock;
1168 i = &b->b_instr[off];
1169 i->i_opcode = opcode;
1170 i->i_oparg = 0;
1171 if (opcode == RETURN_VALUE)
1172 b->b_return = 1;
1173 compiler_set_lineno(c, off);
1174 return 1;
1175 }
1176
1177 static Py_ssize_t
compiler_add_o(struct compiler * c,PyObject * dict,PyObject * o)1178 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1179 {
1180 PyObject *v;
1181 Py_ssize_t arg;
1182
1183 v = PyDict_GetItemWithError(dict, o);
1184 if (!v) {
1185 if (PyErr_Occurred()) {
1186 return -1;
1187 }
1188 arg = PyDict_GET_SIZE(dict);
1189 v = PyLong_FromSsize_t(arg);
1190 if (!v) {
1191 return -1;
1192 }
1193 if (PyDict_SetItem(dict, o, v) < 0) {
1194 Py_DECREF(v);
1195 return -1;
1196 }
1197 Py_DECREF(v);
1198 }
1199 else
1200 arg = PyLong_AsLong(v);
1201 return arg;
1202 }
1203
1204 // Merge const *o* recursively and return constant key object.
1205 static PyObject*
merge_consts_recursive(struct compiler * c,PyObject * o)1206 merge_consts_recursive(struct compiler *c, PyObject *o)
1207 {
1208 // None and Ellipsis are singleton, and key is the singleton.
1209 // No need to merge object and key.
1210 if (o == Py_None || o == Py_Ellipsis) {
1211 Py_INCREF(o);
1212 return o;
1213 }
1214
1215 PyObject *key = _PyCode_ConstantKey(o);
1216 if (key == NULL) {
1217 return NULL;
1218 }
1219
1220 // t is borrowed reference
1221 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1222 if (t != key) {
1223 // o is registered in c_const_cache. Just use it.
1224 Py_XINCREF(t);
1225 Py_DECREF(key);
1226 return t;
1227 }
1228
1229 // We registered o in c_const_cache.
1230 // When o is a tuple or frozenset, we want to merge its
1231 // items too.
1232 if (PyTuple_CheckExact(o)) {
1233 Py_ssize_t len = PyTuple_GET_SIZE(o);
1234 for (Py_ssize_t i = 0; i < len; i++) {
1235 PyObject *item = PyTuple_GET_ITEM(o, i);
1236 PyObject *u = merge_consts_recursive(c, item);
1237 if (u == NULL) {
1238 Py_DECREF(key);
1239 return NULL;
1240 }
1241
1242 // See _PyCode_ConstantKey()
1243 PyObject *v; // borrowed
1244 if (PyTuple_CheckExact(u)) {
1245 v = PyTuple_GET_ITEM(u, 1);
1246 }
1247 else {
1248 v = u;
1249 }
1250 if (v != item) {
1251 Py_INCREF(v);
1252 PyTuple_SET_ITEM(o, i, v);
1253 Py_DECREF(item);
1254 }
1255
1256 Py_DECREF(u);
1257 }
1258 }
1259 else if (PyFrozenSet_CheckExact(o)) {
1260 // *key* is tuple. And its first item is frozenset of
1261 // constant keys.
1262 // See _PyCode_ConstantKey() for detail.
1263 assert(PyTuple_CheckExact(key));
1264 assert(PyTuple_GET_SIZE(key) == 2);
1265
1266 Py_ssize_t len = PySet_GET_SIZE(o);
1267 if (len == 0) { // empty frozenset should not be re-created.
1268 return key;
1269 }
1270 PyObject *tuple = PyTuple_New(len);
1271 if (tuple == NULL) {
1272 Py_DECREF(key);
1273 return NULL;
1274 }
1275 Py_ssize_t i = 0, pos = 0;
1276 PyObject *item;
1277 Py_hash_t hash;
1278 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1279 PyObject *k = merge_consts_recursive(c, item);
1280 if (k == NULL) {
1281 Py_DECREF(tuple);
1282 Py_DECREF(key);
1283 return NULL;
1284 }
1285 PyObject *u;
1286 if (PyTuple_CheckExact(k)) {
1287 u = PyTuple_GET_ITEM(k, 1);
1288 Py_INCREF(u);
1289 Py_DECREF(k);
1290 }
1291 else {
1292 u = k;
1293 }
1294 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1295 i++;
1296 }
1297
1298 // Instead of rewriting o, we create new frozenset and embed in the
1299 // key tuple. Caller should get merged frozenset from the key tuple.
1300 PyObject *new = PyFrozenSet_New(tuple);
1301 Py_DECREF(tuple);
1302 if (new == NULL) {
1303 Py_DECREF(key);
1304 return NULL;
1305 }
1306 assert(PyTuple_GET_ITEM(key, 1) == o);
1307 Py_DECREF(o);
1308 PyTuple_SET_ITEM(key, 1, new);
1309 }
1310
1311 return key;
1312 }
1313
1314 static Py_ssize_t
compiler_add_const(struct compiler * c,PyObject * o)1315 compiler_add_const(struct compiler *c, PyObject *o)
1316 {
1317 if (c->c_do_not_emit_bytecode) {
1318 return 0;
1319 }
1320
1321 PyObject *key = merge_consts_recursive(c, o);
1322 if (key == NULL) {
1323 return -1;
1324 }
1325
1326 Py_ssize_t arg = compiler_add_o(c, c->u->u_consts, key);
1327 Py_DECREF(key);
1328 return arg;
1329 }
1330
1331 static int
compiler_addop_load_const(struct compiler * c,PyObject * o)1332 compiler_addop_load_const(struct compiler *c, PyObject *o)
1333 {
1334 if (c->c_do_not_emit_bytecode) {
1335 return 1;
1336 }
1337
1338 Py_ssize_t arg = compiler_add_const(c, o);
1339 if (arg < 0)
1340 return 0;
1341 return compiler_addop_i(c, LOAD_CONST, arg);
1342 }
1343
1344 static int
compiler_addop_o(struct compiler * c,int opcode,PyObject * dict,PyObject * o)1345 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1346 PyObject *o)
1347 {
1348 if (c->c_do_not_emit_bytecode) {
1349 return 1;
1350 }
1351
1352 Py_ssize_t arg = compiler_add_o(c, dict, o);
1353 if (arg < 0)
1354 return 0;
1355 return compiler_addop_i(c, opcode, arg);
1356 }
1357
1358 static int
compiler_addop_name(struct compiler * c,int opcode,PyObject * dict,PyObject * o)1359 compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1360 PyObject *o)
1361 {
1362 Py_ssize_t arg;
1363
1364 if (c->c_do_not_emit_bytecode) {
1365 return 1;
1366 }
1367
1368 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1369 if (!mangled)
1370 return 0;
1371 arg = compiler_add_o(c, dict, mangled);
1372 Py_DECREF(mangled);
1373 if (arg < 0)
1374 return 0;
1375 return compiler_addop_i(c, opcode, arg);
1376 }
1377
1378 /* Add an opcode with an integer argument.
1379 Returns 0 on failure, 1 on success.
1380 */
1381
1382 static int
compiler_addop_i(struct compiler * c,int opcode,Py_ssize_t oparg)1383 compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1384 {
1385 struct instr *i;
1386 int off;
1387
1388 if (c->c_do_not_emit_bytecode) {
1389 return 1;
1390 }
1391
1392 /* oparg value is unsigned, but a signed C int is usually used to store
1393 it in the C code (like Python/ceval.c).
1394
1395 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1396
1397 The argument of a concrete bytecode instruction is limited to 8-bit.
1398 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1399 assert(HAS_ARG(opcode));
1400 assert(0 <= oparg && oparg <= 2147483647);
1401
1402 off = compiler_next_instr(c, c->u->u_curblock);
1403 if (off < 0)
1404 return 0;
1405 i = &c->u->u_curblock->b_instr[off];
1406 i->i_opcode = opcode;
1407 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1408 compiler_set_lineno(c, off);
1409 return 1;
1410 }
1411
1412 static int
compiler_addop_j(struct compiler * c,int opcode,basicblock * b,int absolute)1413 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1414 {
1415 struct instr *i;
1416 int off;
1417
1418 if (c->c_do_not_emit_bytecode) {
1419 return 1;
1420 }
1421
1422 assert(HAS_ARG(opcode));
1423 assert(b != NULL);
1424 off = compiler_next_instr(c, c->u->u_curblock);
1425 if (off < 0)
1426 return 0;
1427 i = &c->u->u_curblock->b_instr[off];
1428 i->i_opcode = opcode;
1429 i->i_target = b;
1430 if (absolute)
1431 i->i_jabs = 1;
1432 else
1433 i->i_jrel = 1;
1434 compiler_set_lineno(c, off);
1435 return 1;
1436 }
1437
1438 /* NEXT_BLOCK() creates an implicit jump from the current block
1439 to the new block.
1440
1441 The returns inside this macro make it impossible to decref objects
1442 created in the local function. Local objects should use the arena.
1443 */
1444 #define NEXT_BLOCK(C) { \
1445 if (compiler_next_block((C)) == NULL) \
1446 return 0; \
1447 }
1448
1449 #define ADDOP(C, OP) { \
1450 if (!compiler_addop((C), (OP))) \
1451 return 0; \
1452 }
1453
1454 #define ADDOP_IN_SCOPE(C, OP) { \
1455 if (!compiler_addop((C), (OP))) { \
1456 compiler_exit_scope(c); \
1457 return 0; \
1458 } \
1459 }
1460
1461 #define ADDOP_LOAD_CONST(C, O) { \
1462 if (!compiler_addop_load_const((C), (O))) \
1463 return 0; \
1464 }
1465
1466 /* Same as ADDOP_LOAD_CONST, but steals a reference. */
1467 #define ADDOP_LOAD_CONST_NEW(C, O) { \
1468 PyObject *__new_const = (O); \
1469 if (__new_const == NULL) { \
1470 return 0; \
1471 } \
1472 if (!compiler_addop_load_const((C), __new_const)) { \
1473 Py_DECREF(__new_const); \
1474 return 0; \
1475 } \
1476 Py_DECREF(__new_const); \
1477 }
1478
1479 #define ADDOP_O(C, OP, O, TYPE) { \
1480 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1481 return 0; \
1482 }
1483
1484 /* Same as ADDOP_O, but steals a reference. */
1485 #define ADDOP_N(C, OP, O, TYPE) { \
1486 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1487 Py_DECREF((O)); \
1488 return 0; \
1489 } \
1490 Py_DECREF((O)); \
1491 }
1492
1493 #define ADDOP_NAME(C, OP, O, TYPE) { \
1494 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1495 return 0; \
1496 }
1497
1498 #define ADDOP_I(C, OP, O) { \
1499 if (!compiler_addop_i((C), (OP), (O))) \
1500 return 0; \
1501 }
1502
1503 #define ADDOP_JABS(C, OP, O) { \
1504 if (!compiler_addop_j((C), (OP), (O), 1)) \
1505 return 0; \
1506 }
1507
1508 #define ADDOP_JREL(C, OP, O) { \
1509 if (!compiler_addop_j((C), (OP), (O), 0)) \
1510 return 0; \
1511 }
1512
1513 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1514 the ASDL name to synthesize the name of the C type and the visit function.
1515 */
1516
1517 #define VISIT(C, TYPE, V) {\
1518 if (!compiler_visit_ ## TYPE((C), (V))) \
1519 return 0; \
1520 }
1521
1522 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1523 if (!compiler_visit_ ## TYPE((C), (V))) { \
1524 compiler_exit_scope(c); \
1525 return 0; \
1526 } \
1527 }
1528
1529 #define VISIT_SLICE(C, V, CTX) {\
1530 if (!compiler_visit_slice((C), (V), (CTX))) \
1531 return 0; \
1532 }
1533
1534 #define VISIT_SEQ(C, TYPE, SEQ) { \
1535 int _i; \
1536 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1537 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1538 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1539 if (!compiler_visit_ ## TYPE((C), elt)) \
1540 return 0; \
1541 } \
1542 }
1543
1544 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1545 int _i; \
1546 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1547 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1548 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1549 if (!compiler_visit_ ## TYPE((C), elt)) { \
1550 compiler_exit_scope(c); \
1551 return 0; \
1552 } \
1553 } \
1554 }
1555
1556 /* These macros allows to check only for errors and not emmit bytecode
1557 * while visiting nodes.
1558 */
1559
1560 #define BEGIN_DO_NOT_EMIT_BYTECODE { \
1561 c->c_do_not_emit_bytecode++;
1562
1563 #define END_DO_NOT_EMIT_BYTECODE \
1564 c->c_do_not_emit_bytecode--; \
1565 }
1566
1567 /* Search if variable annotations are present statically in a block. */
1568
1569 static int
find_ann(asdl_seq * stmts)1570 find_ann(asdl_seq *stmts)
1571 {
1572 int i, j, res = 0;
1573 stmt_ty st;
1574
1575 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1576 st = (stmt_ty)asdl_seq_GET(stmts, i);
1577 switch (st->kind) {
1578 case AnnAssign_kind:
1579 return 1;
1580 case For_kind:
1581 res = find_ann(st->v.For.body) ||
1582 find_ann(st->v.For.orelse);
1583 break;
1584 case AsyncFor_kind:
1585 res = find_ann(st->v.AsyncFor.body) ||
1586 find_ann(st->v.AsyncFor.orelse);
1587 break;
1588 case While_kind:
1589 res = find_ann(st->v.While.body) ||
1590 find_ann(st->v.While.orelse);
1591 break;
1592 case If_kind:
1593 res = find_ann(st->v.If.body) ||
1594 find_ann(st->v.If.orelse);
1595 break;
1596 case With_kind:
1597 res = find_ann(st->v.With.body);
1598 break;
1599 case AsyncWith_kind:
1600 res = find_ann(st->v.AsyncWith.body);
1601 break;
1602 case Try_kind:
1603 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1604 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1605 st->v.Try.handlers, j);
1606 if (find_ann(handler->v.ExceptHandler.body)) {
1607 return 1;
1608 }
1609 }
1610 res = find_ann(st->v.Try.body) ||
1611 find_ann(st->v.Try.finalbody) ||
1612 find_ann(st->v.Try.orelse);
1613 break;
1614 default:
1615 res = 0;
1616 }
1617 if (res) {
1618 break;
1619 }
1620 }
1621 return res;
1622 }
1623
1624 /*
1625 * Frame block handling functions
1626 */
1627
1628 static int
compiler_push_fblock(struct compiler * c,enum fblocktype t,basicblock * b,basicblock * exit)1629 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
1630 basicblock *exit)
1631 {
1632 struct fblockinfo *f;
1633 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
1634 PyErr_SetString(PyExc_SyntaxError,
1635 "too many statically nested blocks");
1636 return 0;
1637 }
1638 f = &c->u->u_fblock[c->u->u_nfblocks++];
1639 f->fb_type = t;
1640 f->fb_block = b;
1641 f->fb_exit = exit;
1642 return 1;
1643 }
1644
1645 static void
compiler_pop_fblock(struct compiler * c,enum fblocktype t,basicblock * b)1646 compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1647 {
1648 struct compiler_unit *u = c->u;
1649 assert(u->u_nfblocks > 0);
1650 u->u_nfblocks--;
1651 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1652 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1653 }
1654
1655 /* Unwind a frame block. If preserve_tos is true, the TOS before
1656 * popping the blocks will be restored afterwards.
1657 */
1658 static int
compiler_unwind_fblock(struct compiler * c,struct fblockinfo * info,int preserve_tos)1659 compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1660 int preserve_tos)
1661 {
1662 switch (info->fb_type) {
1663 case WHILE_LOOP:
1664 return 1;
1665
1666 case FINALLY_END:
1667 info->fb_exit = NULL;
1668 ADDOP_I(c, POP_FINALLY, preserve_tos);
1669 if (preserve_tos) {
1670 ADDOP(c, ROT_TWO);
1671 }
1672 ADDOP(c, POP_TOP);
1673 return 1;
1674
1675 case FOR_LOOP:
1676 /* Pop the iterator */
1677 if (preserve_tos) {
1678 ADDOP(c, ROT_TWO);
1679 }
1680 ADDOP(c, POP_TOP);
1681 return 1;
1682
1683 case EXCEPT:
1684 ADDOP(c, POP_BLOCK);
1685 return 1;
1686
1687 case FINALLY_TRY:
1688 ADDOP(c, POP_BLOCK);
1689 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);
1690 return 1;
1691
1692 case FINALLY_TRY2:
1693 ADDOP(c, POP_BLOCK);
1694 if (preserve_tos) {
1695 ADDOP(c, ROT_TWO);
1696 ADDOP(c, POP_TOP);
1697 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);
1698 }
1699 else {
1700 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);
1701 ADDOP(c, POP_TOP);
1702 }
1703 return 1;
1704
1705 case WITH:
1706 case ASYNC_WITH:
1707 ADDOP(c, POP_BLOCK);
1708 if (preserve_tos) {
1709 ADDOP(c, ROT_TWO);
1710 }
1711 ADDOP(c, BEGIN_FINALLY);
1712 ADDOP(c, WITH_CLEANUP_START);
1713 if (info->fb_type == ASYNC_WITH) {
1714 ADDOP(c, GET_AWAITABLE);
1715 ADDOP_LOAD_CONST(c, Py_None);
1716 ADDOP(c, YIELD_FROM);
1717 }
1718 ADDOP(c, WITH_CLEANUP_FINISH);
1719 ADDOP_I(c, POP_FINALLY, 0);
1720 return 1;
1721
1722 case HANDLER_CLEANUP:
1723 if (preserve_tos) {
1724 ADDOP(c, ROT_FOUR);
1725 }
1726 if (info->fb_exit) {
1727 ADDOP(c, POP_BLOCK);
1728 ADDOP(c, POP_EXCEPT);
1729 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);
1730 }
1731 else {
1732 ADDOP(c, POP_EXCEPT);
1733 }
1734 return 1;
1735 }
1736 Py_UNREACHABLE();
1737 }
1738
1739 /* Compile a sequence of statements, checking for a docstring
1740 and for annotations. */
1741
1742 static int
compiler_body(struct compiler * c,asdl_seq * stmts)1743 compiler_body(struct compiler *c, asdl_seq *stmts)
1744 {
1745 int i = 0;
1746 stmt_ty st;
1747 PyObject *docstring;
1748
1749 /* Set current line number to the line number of first statement.
1750 This way line number for SETUP_ANNOTATIONS will always
1751 coincide with the line number of first "real" statement in module.
1752 If body is empty, then lineno will be set later in assemble. */
1753 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
1754 !c->u->u_lineno && asdl_seq_LEN(stmts)) {
1755 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1756 c->u->u_lineno = st->lineno;
1757 }
1758 /* Every annotated class and module should have __annotations__. */
1759 if (find_ann(stmts)) {
1760 ADDOP(c, SETUP_ANNOTATIONS);
1761 }
1762 if (!asdl_seq_LEN(stmts))
1763 return 1;
1764 /* if not -OO mode, set docstring */
1765 if (c->c_optimize < 2) {
1766 docstring = _PyAST_GetDocString(stmts);
1767 if (docstring) {
1768 i = 1;
1769 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1770 assert(st->kind == Expr_kind);
1771 VISIT(c, expr, st->v.Expr.value);
1772 if (!compiler_nameop(c, __doc__, Store))
1773 return 0;
1774 }
1775 }
1776 for (; i < asdl_seq_LEN(stmts); i++)
1777 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1778 return 1;
1779 }
1780
1781 static PyCodeObject *
compiler_mod(struct compiler * c,mod_ty mod)1782 compiler_mod(struct compiler *c, mod_ty mod)
1783 {
1784 PyCodeObject *co;
1785 int addNone = 1;
1786 static PyObject *module;
1787 if (!module) {
1788 module = PyUnicode_InternFromString("<module>");
1789 if (!module)
1790 return NULL;
1791 }
1792 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1793 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
1794 return NULL;
1795 switch (mod->kind) {
1796 case Module_kind:
1797 if (!compiler_body(c, mod->v.Module.body)) {
1798 compiler_exit_scope(c);
1799 return 0;
1800 }
1801 break;
1802 case Interactive_kind:
1803 if (find_ann(mod->v.Interactive.body)) {
1804 ADDOP(c, SETUP_ANNOTATIONS);
1805 }
1806 c->c_interactive = 1;
1807 VISIT_SEQ_IN_SCOPE(c, stmt,
1808 mod->v.Interactive.body);
1809 break;
1810 case Expression_kind:
1811 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1812 addNone = 0;
1813 break;
1814 case Suite_kind:
1815 PyErr_SetString(PyExc_SystemError,
1816 "suite should not be possible");
1817 return 0;
1818 default:
1819 PyErr_Format(PyExc_SystemError,
1820 "module kind %d should not be possible",
1821 mod->kind);
1822 return 0;
1823 }
1824 co = assemble(c, addNone);
1825 compiler_exit_scope(c);
1826 return co;
1827 }
1828
1829 /* The test for LOCAL must come before the test for FREE in order to
1830 handle classes where name is both local and free. The local var is
1831 a method and the free var is a free var referenced within a method.
1832 */
1833
1834 static int
get_ref_type(struct compiler * c,PyObject * name)1835 get_ref_type(struct compiler *c, PyObject *name)
1836 {
1837 int scope;
1838 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1839 _PyUnicode_EqualToASCIIString(name, "__class__"))
1840 return CELL;
1841 scope = PyST_GetScope(c->u->u_ste, name);
1842 if (scope == 0) {
1843 char buf[350];
1844 PyOS_snprintf(buf, sizeof(buf),
1845 "unknown scope for %.100s in %.100s(%s)\n"
1846 "symbols: %s\nlocals: %s\nglobals: %s",
1847 PyUnicode_AsUTF8(name),
1848 PyUnicode_AsUTF8(c->u->u_name),
1849 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1850 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1851 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1852 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
1853 );
1854 Py_FatalError(buf);
1855 }
1856
1857 return scope;
1858 }
1859
1860 static int
compiler_lookup_arg(PyObject * dict,PyObject * name)1861 compiler_lookup_arg(PyObject *dict, PyObject *name)
1862 {
1863 PyObject *v;
1864 v = PyDict_GetItem(dict, name);
1865 if (v == NULL)
1866 return -1;
1867 return PyLong_AS_LONG(v);
1868 }
1869
1870 static int
compiler_make_closure(struct compiler * c,PyCodeObject * co,Py_ssize_t flags,PyObject * qualname)1871 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
1872 {
1873 Py_ssize_t i, free = PyCode_GetNumFree(co);
1874 if (qualname == NULL)
1875 qualname = co->co_name;
1876
1877 if (free) {
1878 for (i = 0; i < free; ++i) {
1879 /* Bypass com_addop_varname because it will generate
1880 LOAD_DEREF but LOAD_CLOSURE is needed.
1881 */
1882 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1883 int arg, reftype;
1884
1885 /* Special case: If a class contains a method with a
1886 free variable that has the same name as a method,
1887 the name will be considered free *and* local in the
1888 class. It should be handled by the closure, as
1889 well as by the normal name loookup logic.
1890 */
1891 reftype = get_ref_type(c, name);
1892 if (reftype == CELL)
1893 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1894 else /* (reftype == FREE) */
1895 arg = compiler_lookup_arg(c->u->u_freevars, name);
1896 if (arg == -1) {
1897 fprintf(stderr,
1898 "lookup %s in %s %d %d\n"
1899 "freevars of %s: %s\n",
1900 PyUnicode_AsUTF8(PyObject_Repr(name)),
1901 PyUnicode_AsUTF8(c->u->u_name),
1902 reftype, arg,
1903 PyUnicode_AsUTF8(co->co_name),
1904 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1905 Py_FatalError("compiler_make_closure()");
1906 }
1907 ADDOP_I(c, LOAD_CLOSURE, arg);
1908 }
1909 flags |= 0x08;
1910 ADDOP_I(c, BUILD_TUPLE, free);
1911 }
1912 ADDOP_LOAD_CONST(c, (PyObject*)co);
1913 ADDOP_LOAD_CONST(c, qualname);
1914 ADDOP_I(c, MAKE_FUNCTION, flags);
1915 return 1;
1916 }
1917
1918 static int
compiler_decorators(struct compiler * c,asdl_seq * decos)1919 compiler_decorators(struct compiler *c, asdl_seq* decos)
1920 {
1921 int i;
1922
1923 if (!decos)
1924 return 1;
1925
1926 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1927 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1928 }
1929 return 1;
1930 }
1931
1932 static int
compiler_visit_kwonlydefaults(struct compiler * c,asdl_seq * kwonlyargs,asdl_seq * kw_defaults)1933 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1934 asdl_seq *kw_defaults)
1935 {
1936 /* Push a dict of keyword-only default values.
1937
1938 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1939 */
1940 int i;
1941 PyObject *keys = NULL;
1942
1943 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1944 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1945 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1946 if (default_) {
1947 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1948 if (!mangled) {
1949 goto error;
1950 }
1951 if (keys == NULL) {
1952 keys = PyList_New(1);
1953 if (keys == NULL) {
1954 Py_DECREF(mangled);
1955 return 0;
1956 }
1957 PyList_SET_ITEM(keys, 0, mangled);
1958 }
1959 else {
1960 int res = PyList_Append(keys, mangled);
1961 Py_DECREF(mangled);
1962 if (res == -1) {
1963 goto error;
1964 }
1965 }
1966 if (!compiler_visit_expr(c, default_)) {
1967 goto error;
1968 }
1969 }
1970 }
1971 if (keys != NULL) {
1972 Py_ssize_t default_count = PyList_GET_SIZE(keys);
1973 PyObject *keys_tuple = PyList_AsTuple(keys);
1974 Py_DECREF(keys);
1975 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
1976 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
1977 assert(default_count > 0);
1978 return 1;
1979 }
1980 else {
1981 return -1;
1982 }
1983
1984 error:
1985 Py_XDECREF(keys);
1986 return 0;
1987 }
1988
1989 static int
compiler_visit_annexpr(struct compiler * c,expr_ty annotation)1990 compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
1991 {
1992 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
1993 return 1;
1994 }
1995
1996 static int
compiler_visit_argannotation(struct compiler * c,identifier id,expr_ty annotation,PyObject * names)1997 compiler_visit_argannotation(struct compiler *c, identifier id,
1998 expr_ty annotation, PyObject *names)
1999 {
2000 if (annotation) {
2001 PyObject *mangled;
2002 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2003 VISIT(c, annexpr, annotation)
2004 }
2005 else {
2006 VISIT(c, expr, annotation);
2007 }
2008 mangled = _Py_Mangle(c->u->u_private, id);
2009 if (!mangled)
2010 return 0;
2011 if (PyList_Append(names, mangled) < 0) {
2012 Py_DECREF(mangled);
2013 return 0;
2014 }
2015 Py_DECREF(mangled);
2016 }
2017 return 1;
2018 }
2019
2020 static int
compiler_visit_argannotations(struct compiler * c,asdl_seq * args,PyObject * names)2021 compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
2022 PyObject *names)
2023 {
2024 int i;
2025 for (i = 0; i < asdl_seq_LEN(args); i++) {
2026 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
2027 if (!compiler_visit_argannotation(
2028 c,
2029 arg->arg,
2030 arg->annotation,
2031 names))
2032 return 0;
2033 }
2034 return 1;
2035 }
2036
2037 static int
compiler_visit_annotations(struct compiler * c,arguments_ty args,expr_ty returns)2038 compiler_visit_annotations(struct compiler *c, arguments_ty args,
2039 expr_ty returns)
2040 {
2041 /* Push arg annotation dict.
2042 The expressions are evaluated out-of-order wrt the source code.
2043
2044 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2045 */
2046 static identifier return_str;
2047 PyObject *names;
2048 Py_ssize_t len;
2049 names = PyList_New(0);
2050 if (!names)
2051 return 0;
2052
2053 if (!compiler_visit_argannotations(c, args->args, names))
2054 goto error;
2055 if (!compiler_visit_argannotations(c, args->posonlyargs, names))
2056 goto error;
2057 if (args->vararg && args->vararg->annotation &&
2058 !compiler_visit_argannotation(c, args->vararg->arg,
2059 args->vararg->annotation, names))
2060 goto error;
2061 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
2062 goto error;
2063 if (args->kwarg && args->kwarg->annotation &&
2064 !compiler_visit_argannotation(c, args->kwarg->arg,
2065 args->kwarg->annotation, names))
2066 goto error;
2067
2068 if (!return_str) {
2069 return_str = PyUnicode_InternFromString("return");
2070 if (!return_str)
2071 goto error;
2072 }
2073 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
2074 goto error;
2075 }
2076
2077 len = PyList_GET_SIZE(names);
2078 if (len) {
2079 PyObject *keytuple = PyList_AsTuple(names);
2080 Py_DECREF(names);
2081 ADDOP_LOAD_CONST_NEW(c, keytuple);
2082 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
2083 return 1;
2084 }
2085 else {
2086 Py_DECREF(names);
2087 return -1;
2088 }
2089
2090 error:
2091 Py_DECREF(names);
2092 return 0;
2093 }
2094
2095 static int
compiler_visit_defaults(struct compiler * c,arguments_ty args)2096 compiler_visit_defaults(struct compiler *c, arguments_ty args)
2097 {
2098 VISIT_SEQ(c, expr, args->defaults);
2099 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2100 return 1;
2101 }
2102
2103 static Py_ssize_t
compiler_default_arguments(struct compiler * c,arguments_ty args)2104 compiler_default_arguments(struct compiler *c, arguments_ty args)
2105 {
2106 Py_ssize_t funcflags = 0;
2107 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
2108 if (!compiler_visit_defaults(c, args))
2109 return -1;
2110 funcflags |= 0x01;
2111 }
2112 if (args->kwonlyargs) {
2113 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
2114 args->kw_defaults);
2115 if (res == 0) {
2116 return -1;
2117 }
2118 else if (res > 0) {
2119 funcflags |= 0x02;
2120 }
2121 }
2122 return funcflags;
2123 }
2124
2125 static int
compiler_function(struct compiler * c,stmt_ty s,int is_async)2126 compiler_function(struct compiler *c, stmt_ty s, int is_async)
2127 {
2128 PyCodeObject *co;
2129 PyObject *qualname, *docstring = NULL;
2130 arguments_ty args;
2131 expr_ty returns;
2132 identifier name;
2133 asdl_seq* decos;
2134 asdl_seq *body;
2135 Py_ssize_t i, funcflags;
2136 int annotations;
2137 int scope_type;
2138 int firstlineno;
2139
2140 if (is_async) {
2141 assert(s->kind == AsyncFunctionDef_kind);
2142
2143 args = s->v.AsyncFunctionDef.args;
2144 returns = s->v.AsyncFunctionDef.returns;
2145 decos = s->v.AsyncFunctionDef.decorator_list;
2146 name = s->v.AsyncFunctionDef.name;
2147 body = s->v.AsyncFunctionDef.body;
2148
2149 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2150 } else {
2151 assert(s->kind == FunctionDef_kind);
2152
2153 args = s->v.FunctionDef.args;
2154 returns = s->v.FunctionDef.returns;
2155 decos = s->v.FunctionDef.decorator_list;
2156 name = s->v.FunctionDef.name;
2157 body = s->v.FunctionDef.body;
2158
2159 scope_type = COMPILER_SCOPE_FUNCTION;
2160 }
2161
2162 if (!compiler_decorators(c, decos))
2163 return 0;
2164
2165 firstlineno = s->lineno;
2166 if (asdl_seq_LEN(decos)) {
2167 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2168 }
2169
2170 funcflags = compiler_default_arguments(c, args);
2171 if (funcflags == -1) {
2172 return 0;
2173 }
2174
2175 annotations = compiler_visit_annotations(c, args, returns);
2176 if (annotations == 0) {
2177 return 0;
2178 }
2179 else if (annotations > 0) {
2180 funcflags |= 0x04;
2181 }
2182
2183 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
2184 return 0;
2185 }
2186
2187 /* if not -OO mode, add docstring */
2188 if (c->c_optimize < 2) {
2189 docstring = _PyAST_GetDocString(body);
2190 }
2191 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
2192 compiler_exit_scope(c);
2193 return 0;
2194 }
2195
2196 c->u->u_argcount = asdl_seq_LEN(args->args);
2197 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
2198 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2199 VISIT_SEQ_IN_SCOPE(c, stmt, body);
2200 co = assemble(c, 1);
2201 qualname = c->u->u_qualname;
2202 Py_INCREF(qualname);
2203 compiler_exit_scope(c);
2204 if (co == NULL) {
2205 Py_XDECREF(qualname);
2206 Py_XDECREF(co);
2207 return 0;
2208 }
2209
2210 compiler_make_closure(c, co, funcflags, qualname);
2211 Py_DECREF(qualname);
2212 Py_DECREF(co);
2213
2214 /* decorators */
2215 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2216 ADDOP_I(c, CALL_FUNCTION, 1);
2217 }
2218
2219 return compiler_nameop(c, name, Store);
2220 }
2221
2222 static int
compiler_class(struct compiler * c,stmt_ty s)2223 compiler_class(struct compiler *c, stmt_ty s)
2224 {
2225 PyCodeObject *co;
2226 PyObject *str;
2227 int i, firstlineno;
2228 asdl_seq* decos = s->v.ClassDef.decorator_list;
2229
2230 if (!compiler_decorators(c, decos))
2231 return 0;
2232
2233 firstlineno = s->lineno;
2234 if (asdl_seq_LEN(decos)) {
2235 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2236 }
2237
2238 /* ultimately generate code for:
2239 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2240 where:
2241 <func> is a function/closure created from the class body;
2242 it has a single argument (__locals__) where the dict
2243 (or MutableSequence) representing the locals is passed
2244 <name> is the class name
2245 <bases> is the positional arguments and *varargs argument
2246 <keywords> is the keyword arguments and **kwds argument
2247 This borrows from compiler_call.
2248 */
2249
2250 /* 1. compile the class body into a code object */
2251 if (!compiler_enter_scope(c, s->v.ClassDef.name,
2252 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
2253 return 0;
2254 /* this block represents what we do in the new scope */
2255 {
2256 /* use the class name for name mangling */
2257 Py_INCREF(s->v.ClassDef.name);
2258 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
2259 /* load (global) __name__ ... */
2260 str = PyUnicode_InternFromString("__name__");
2261 if (!str || !compiler_nameop(c, str, Load)) {
2262 Py_XDECREF(str);
2263 compiler_exit_scope(c);
2264 return 0;
2265 }
2266 Py_DECREF(str);
2267 /* ... and store it as __module__ */
2268 str = PyUnicode_InternFromString("__module__");
2269 if (!str || !compiler_nameop(c, str, Store)) {
2270 Py_XDECREF(str);
2271 compiler_exit_scope(c);
2272 return 0;
2273 }
2274 Py_DECREF(str);
2275 assert(c->u->u_qualname);
2276 ADDOP_LOAD_CONST(c, c->u->u_qualname);
2277 str = PyUnicode_InternFromString("__qualname__");
2278 if (!str || !compiler_nameop(c, str, Store)) {
2279 Py_XDECREF(str);
2280 compiler_exit_scope(c);
2281 return 0;
2282 }
2283 Py_DECREF(str);
2284 /* compile the body proper */
2285 if (!compiler_body(c, s->v.ClassDef.body)) {
2286 compiler_exit_scope(c);
2287 return 0;
2288 }
2289 /* Return __classcell__ if it is referenced, otherwise return None */
2290 if (c->u->u_ste->ste_needs_class_closure) {
2291 /* Store __classcell__ into class namespace & return it */
2292 str = PyUnicode_InternFromString("__class__");
2293 if (str == NULL) {
2294 compiler_exit_scope(c);
2295 return 0;
2296 }
2297 i = compiler_lookup_arg(c->u->u_cellvars, str);
2298 Py_DECREF(str);
2299 if (i < 0) {
2300 compiler_exit_scope(c);
2301 return 0;
2302 }
2303 assert(i == 0);
2304
2305 ADDOP_I(c, LOAD_CLOSURE, i);
2306 ADDOP(c, DUP_TOP);
2307 str = PyUnicode_InternFromString("__classcell__");
2308 if (!str || !compiler_nameop(c, str, Store)) {
2309 Py_XDECREF(str);
2310 compiler_exit_scope(c);
2311 return 0;
2312 }
2313 Py_DECREF(str);
2314 }
2315 else {
2316 /* No methods referenced __class__, so just return None */
2317 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
2318 ADDOP_LOAD_CONST(c, Py_None);
2319 }
2320 ADDOP_IN_SCOPE(c, RETURN_VALUE);
2321 /* create the code object */
2322 co = assemble(c, 1);
2323 }
2324 /* leave the new scope */
2325 compiler_exit_scope(c);
2326 if (co == NULL)
2327 return 0;
2328
2329 /* 2. load the 'build_class' function */
2330 ADDOP(c, LOAD_BUILD_CLASS);
2331
2332 /* 3. load a function (or closure) made from the code object */
2333 compiler_make_closure(c, co, 0, NULL);
2334 Py_DECREF(co);
2335
2336 /* 4. load class name */
2337 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
2338
2339 /* 5. generate the rest of the code for the call */
2340 if (!compiler_call_helper(c, 2,
2341 s->v.ClassDef.bases,
2342 s->v.ClassDef.keywords))
2343 return 0;
2344
2345 /* 6. apply decorators */
2346 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2347 ADDOP_I(c, CALL_FUNCTION, 1);
2348 }
2349
2350 /* 7. store into <name> */
2351 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2352 return 0;
2353 return 1;
2354 }
2355
2356 /* Return 0 if the expression is a constant value except named singletons.
2357 Return 1 otherwise. */
2358 static int
check_is_arg(expr_ty e)2359 check_is_arg(expr_ty e)
2360 {
2361 if (e->kind != Constant_kind) {
2362 return 1;
2363 }
2364 PyObject *value = e->v.Constant.value;
2365 return (value == Py_None
2366 || value == Py_False
2367 || value == Py_True
2368 || value == Py_Ellipsis);
2369 }
2370
2371 /* Check operands of identity chacks ("is" and "is not").
2372 Emit a warning if any operand is a constant except named singletons.
2373 Return 0 on error.
2374 */
2375 static int
check_compare(struct compiler * c,expr_ty e)2376 check_compare(struct compiler *c, expr_ty e)
2377 {
2378 Py_ssize_t i, n;
2379 int left = check_is_arg(e->v.Compare.left);
2380 n = asdl_seq_LEN(e->v.Compare.ops);
2381 for (i = 0; i < n; i++) {
2382 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2383 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2384 if (op == Is || op == IsNot) {
2385 if (!right || !left) {
2386 const char *msg = (op == Is)
2387 ? "\"is\" with a literal. Did you mean \"==\"?"
2388 : "\"is not\" with a literal. Did you mean \"!=\"?";
2389 return compiler_warn(c, msg);
2390 }
2391 }
2392 left = right;
2393 }
2394 return 1;
2395 }
2396
2397 static int
cmpop(cmpop_ty op)2398 cmpop(cmpop_ty op)
2399 {
2400 switch (op) {
2401 case Eq:
2402 return PyCmp_EQ;
2403 case NotEq:
2404 return PyCmp_NE;
2405 case Lt:
2406 return PyCmp_LT;
2407 case LtE:
2408 return PyCmp_LE;
2409 case Gt:
2410 return PyCmp_GT;
2411 case GtE:
2412 return PyCmp_GE;
2413 case Is:
2414 return PyCmp_IS;
2415 case IsNot:
2416 return PyCmp_IS_NOT;
2417 case In:
2418 return PyCmp_IN;
2419 case NotIn:
2420 return PyCmp_NOT_IN;
2421 default:
2422 return PyCmp_BAD;
2423 }
2424 }
2425
2426 static int
compiler_jump_if(struct compiler * c,expr_ty e,basicblock * next,int cond)2427 compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2428 {
2429 switch (e->kind) {
2430 case UnaryOp_kind:
2431 if (e->v.UnaryOp.op == Not)
2432 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2433 /* fallback to general implementation */
2434 break;
2435 case BoolOp_kind: {
2436 asdl_seq *s = e->v.BoolOp.values;
2437 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2438 assert(n >= 0);
2439 int cond2 = e->v.BoolOp.op == Or;
2440 basicblock *next2 = next;
2441 if (!cond2 != !cond) {
2442 next2 = compiler_new_block(c);
2443 if (next2 == NULL)
2444 return 0;
2445 }
2446 for (i = 0; i < n; ++i) {
2447 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2448 return 0;
2449 }
2450 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2451 return 0;
2452 if (next2 != next)
2453 compiler_use_next_block(c, next2);
2454 return 1;
2455 }
2456 case IfExp_kind: {
2457 basicblock *end, *next2;
2458 end = compiler_new_block(c);
2459 if (end == NULL)
2460 return 0;
2461 next2 = compiler_new_block(c);
2462 if (next2 == NULL)
2463 return 0;
2464 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2465 return 0;
2466 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2467 return 0;
2468 ADDOP_JREL(c, JUMP_FORWARD, end);
2469 compiler_use_next_block(c, next2);
2470 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2471 return 0;
2472 compiler_use_next_block(c, end);
2473 return 1;
2474 }
2475 case Compare_kind: {
2476 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2477 if (n > 0) {
2478 if (!check_compare(c, e)) {
2479 return 0;
2480 }
2481 basicblock *cleanup = compiler_new_block(c);
2482 if (cleanup == NULL)
2483 return 0;
2484 VISIT(c, expr, e->v.Compare.left);
2485 for (i = 0; i < n; i++) {
2486 VISIT(c, expr,
2487 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2488 ADDOP(c, DUP_TOP);
2489 ADDOP(c, ROT_THREE);
2490 ADDOP_I(c, COMPARE_OP,
2491 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
2492 ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup);
2493 NEXT_BLOCK(c);
2494 }
2495 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2496 ADDOP_I(c, COMPARE_OP,
2497 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
2498 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2499 basicblock *end = compiler_new_block(c);
2500 if (end == NULL)
2501 return 0;
2502 ADDOP_JREL(c, JUMP_FORWARD, end);
2503 compiler_use_next_block(c, cleanup);
2504 ADDOP(c, POP_TOP);
2505 if (!cond) {
2506 ADDOP_JREL(c, JUMP_FORWARD, next);
2507 }
2508 compiler_use_next_block(c, end);
2509 return 1;
2510 }
2511 /* fallback to general implementation */
2512 break;
2513 }
2514 default:
2515 /* fallback to general implementation */
2516 break;
2517 }
2518
2519 /* general implementation */
2520 VISIT(c, expr, e);
2521 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2522 return 1;
2523 }
2524
2525 static int
compiler_ifexp(struct compiler * c,expr_ty e)2526 compiler_ifexp(struct compiler *c, expr_ty e)
2527 {
2528 basicblock *end, *next;
2529
2530 assert(e->kind == IfExp_kind);
2531 end = compiler_new_block(c);
2532 if (end == NULL)
2533 return 0;
2534 next = compiler_new_block(c);
2535 if (next == NULL)
2536 return 0;
2537 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2538 return 0;
2539 VISIT(c, expr, e->v.IfExp.body);
2540 ADDOP_JREL(c, JUMP_FORWARD, end);
2541 compiler_use_next_block(c, next);
2542 VISIT(c, expr, e->v.IfExp.orelse);
2543 compiler_use_next_block(c, end);
2544 return 1;
2545 }
2546
2547 static int
compiler_lambda(struct compiler * c,expr_ty e)2548 compiler_lambda(struct compiler *c, expr_ty e)
2549 {
2550 PyCodeObject *co;
2551 PyObject *qualname;
2552 static identifier name;
2553 Py_ssize_t funcflags;
2554 arguments_ty args = e->v.Lambda.args;
2555 assert(e->kind == Lambda_kind);
2556
2557 if (!name) {
2558 name = PyUnicode_InternFromString("<lambda>");
2559 if (!name)
2560 return 0;
2561 }
2562
2563 funcflags = compiler_default_arguments(c, args);
2564 if (funcflags == -1) {
2565 return 0;
2566 }
2567
2568 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
2569 (void *)e, e->lineno))
2570 return 0;
2571
2572 /* Make None the first constant, so the lambda can't have a
2573 docstring. */
2574 if (compiler_add_const(c, Py_None) < 0)
2575 return 0;
2576
2577 c->u->u_argcount = asdl_seq_LEN(args->args);
2578 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
2579 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2580 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2581 if (c->u->u_ste->ste_generator) {
2582 co = assemble(c, 0);
2583 }
2584 else {
2585 ADDOP_IN_SCOPE(c, RETURN_VALUE);
2586 co = assemble(c, 1);
2587 }
2588 qualname = c->u->u_qualname;
2589 Py_INCREF(qualname);
2590 compiler_exit_scope(c);
2591 if (co == NULL)
2592 return 0;
2593
2594 compiler_make_closure(c, co, funcflags, qualname);
2595 Py_DECREF(qualname);
2596 Py_DECREF(co);
2597
2598 return 1;
2599 }
2600
2601 static int
compiler_if(struct compiler * c,stmt_ty s)2602 compiler_if(struct compiler *c, stmt_ty s)
2603 {
2604 basicblock *end, *next;
2605 int constant;
2606 assert(s->kind == If_kind);
2607 end = compiler_new_block(c);
2608 if (end == NULL)
2609 return 0;
2610
2611 constant = expr_constant(s->v.If.test);
2612 /* constant = 0: "if 0"
2613 * constant = 1: "if 1", "if 2", ...
2614 * constant = -1: rest */
2615 if (constant == 0) {
2616 BEGIN_DO_NOT_EMIT_BYTECODE
2617 VISIT_SEQ(c, stmt, s->v.If.body);
2618 END_DO_NOT_EMIT_BYTECODE
2619 if (s->v.If.orelse) {
2620 VISIT_SEQ(c, stmt, s->v.If.orelse);
2621 }
2622 } else if (constant == 1) {
2623 VISIT_SEQ(c, stmt, s->v.If.body);
2624 if (s->v.If.orelse) {
2625 BEGIN_DO_NOT_EMIT_BYTECODE
2626 VISIT_SEQ(c, stmt, s->v.If.orelse);
2627 END_DO_NOT_EMIT_BYTECODE
2628 }
2629 } else {
2630 if (asdl_seq_LEN(s->v.If.orelse)) {
2631 next = compiler_new_block(c);
2632 if (next == NULL)
2633 return 0;
2634 }
2635 else
2636 next = end;
2637 if (!compiler_jump_if(c, s->v.If.test, next, 0))
2638 return 0;
2639 VISIT_SEQ(c, stmt, s->v.If.body);
2640 if (asdl_seq_LEN(s->v.If.orelse)) {
2641 ADDOP_JREL(c, JUMP_FORWARD, end);
2642 compiler_use_next_block(c, next);
2643 VISIT_SEQ(c, stmt, s->v.If.orelse);
2644 }
2645 }
2646 compiler_use_next_block(c, end);
2647 return 1;
2648 }
2649
2650 static int
compiler_for(struct compiler * c,stmt_ty s)2651 compiler_for(struct compiler *c, stmt_ty s)
2652 {
2653 basicblock *start, *cleanup, *end;
2654
2655 start = compiler_new_block(c);
2656 cleanup = compiler_new_block(c);
2657 end = compiler_new_block(c);
2658 if (start == NULL || end == NULL || cleanup == NULL)
2659 return 0;
2660
2661 if (!compiler_push_fblock(c, FOR_LOOP, start, end))
2662 return 0;
2663
2664 VISIT(c, expr, s->v.For.iter);
2665 ADDOP(c, GET_ITER);
2666 compiler_use_next_block(c, start);
2667 ADDOP_JREL(c, FOR_ITER, cleanup);
2668 VISIT(c, expr, s->v.For.target);
2669 VISIT_SEQ(c, stmt, s->v.For.body);
2670 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2671 compiler_use_next_block(c, cleanup);
2672
2673 compiler_pop_fblock(c, FOR_LOOP, start);
2674
2675 VISIT_SEQ(c, stmt, s->v.For.orelse);
2676 compiler_use_next_block(c, end);
2677 return 1;
2678 }
2679
2680
2681 static int
compiler_async_for(struct compiler * c,stmt_ty s)2682 compiler_async_for(struct compiler *c, stmt_ty s)
2683 {
2684 basicblock *start, *except, *end;
2685 if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){
2686 c->u->u_ste->ste_coroutine = 1;
2687 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
2688 return compiler_error(c, "'async for' outside async function");
2689 }
2690
2691 start = compiler_new_block(c);
2692 except = compiler_new_block(c);
2693 end = compiler_new_block(c);
2694
2695 if (start == NULL || except == NULL || end == NULL)
2696 return 0;
2697
2698 VISIT(c, expr, s->v.AsyncFor.iter);
2699 ADDOP(c, GET_AITER);
2700
2701 compiler_use_next_block(c, start);
2702 if (!compiler_push_fblock(c, FOR_LOOP, start, end))
2703 return 0;
2704
2705 /* SETUP_FINALLY to guard the __anext__ call */
2706 ADDOP_JREL(c, SETUP_FINALLY, except);
2707 ADDOP(c, GET_ANEXT);
2708 ADDOP_LOAD_CONST(c, Py_None);
2709 ADDOP(c, YIELD_FROM);
2710 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
2711
2712 /* Success block for __anext__ */
2713 VISIT(c, expr, s->v.AsyncFor.target);
2714 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2715 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2716
2717 compiler_pop_fblock(c, FOR_LOOP, start);
2718
2719 /* Except block for __anext__ */
2720 compiler_use_next_block(c, except);
2721 ADDOP(c, END_ASYNC_FOR);
2722
2723 /* `else` block */
2724 VISIT_SEQ(c, stmt, s->v.For.orelse);
2725
2726 compiler_use_next_block(c, end);
2727
2728 return 1;
2729 }
2730
2731 static int
compiler_while(struct compiler * c,stmt_ty s)2732 compiler_while(struct compiler *c, stmt_ty s)
2733 {
2734 basicblock *loop, *orelse, *end, *anchor = NULL;
2735 int constant = expr_constant(s->v.While.test);
2736
2737 if (constant == 0) {
2738 BEGIN_DO_NOT_EMIT_BYTECODE
2739 // Push a dummy block so the VISIT_SEQ knows that we are
2740 // inside a while loop so it can correctly evaluate syntax
2741 // errors.
2742 if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL)) {
2743 return 0;
2744 }
2745 VISIT_SEQ(c, stmt, s->v.While.body);
2746 // Remove the dummy block now that is not needed.
2747 compiler_pop_fblock(c, WHILE_LOOP, NULL);
2748 END_DO_NOT_EMIT_BYTECODE
2749 if (s->v.While.orelse) {
2750 VISIT_SEQ(c, stmt, s->v.While.orelse);
2751 }
2752 return 1;
2753 }
2754 loop = compiler_new_block(c);
2755 end = compiler_new_block(c);
2756 if (constant == -1) {
2757 anchor = compiler_new_block(c);
2758 if (anchor == NULL)
2759 return 0;
2760 }
2761 if (loop == NULL || end == NULL)
2762 return 0;
2763 if (s->v.While.orelse) {
2764 orelse = compiler_new_block(c);
2765 if (orelse == NULL)
2766 return 0;
2767 }
2768 else
2769 orelse = NULL;
2770
2771 compiler_use_next_block(c, loop);
2772 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end))
2773 return 0;
2774 if (constant == -1) {
2775 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2776 return 0;
2777 }
2778 VISIT_SEQ(c, stmt, s->v.While.body);
2779 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2780
2781 /* XXX should the two POP instructions be in a separate block
2782 if there is no else clause ?
2783 */
2784
2785 if (constant == -1)
2786 compiler_use_next_block(c, anchor);
2787 compiler_pop_fblock(c, WHILE_LOOP, loop);
2788
2789 if (orelse != NULL) /* what if orelse is just pass? */
2790 VISIT_SEQ(c, stmt, s->v.While.orelse);
2791 compiler_use_next_block(c, end);
2792
2793 return 1;
2794 }
2795
2796 static int
compiler_return(struct compiler * c,stmt_ty s)2797 compiler_return(struct compiler *c, stmt_ty s)
2798 {
2799 int preserve_tos = ((s->v.Return.value != NULL) &&
2800 (s->v.Return.value->kind != Constant_kind));
2801 if (c->u->u_ste->ste_type != FunctionBlock)
2802 return compiler_error(c, "'return' outside function");
2803 if (s->v.Return.value != NULL &&
2804 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2805 {
2806 return compiler_error(
2807 c, "'return' with value in async generator");
2808 }
2809 if (preserve_tos) {
2810 VISIT(c, expr, s->v.Return.value);
2811 }
2812 for (int depth = c->u->u_nfblocks; depth--;) {
2813 struct fblockinfo *info = &c->u->u_fblock[depth];
2814
2815 if (!compiler_unwind_fblock(c, info, preserve_tos))
2816 return 0;
2817 }
2818 if (s->v.Return.value == NULL) {
2819 ADDOP_LOAD_CONST(c, Py_None);
2820 }
2821 else if (!preserve_tos) {
2822 VISIT(c, expr, s->v.Return.value);
2823 }
2824 ADDOP(c, RETURN_VALUE);
2825
2826 return 1;
2827 }
2828
2829 static int
compiler_break(struct compiler * c)2830 compiler_break(struct compiler *c)
2831 {
2832 for (int depth = c->u->u_nfblocks; depth--;) {
2833 struct fblockinfo *info = &c->u->u_fblock[depth];
2834
2835 if (!compiler_unwind_fblock(c, info, 0))
2836 return 0;
2837 if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) {
2838 ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit);
2839 return 1;
2840 }
2841 }
2842 return compiler_error(c, "'break' outside loop");
2843 }
2844
2845 static int
compiler_continue(struct compiler * c)2846 compiler_continue(struct compiler *c)
2847 {
2848 for (int depth = c->u->u_nfblocks; depth--;) {
2849 struct fblockinfo *info = &c->u->u_fblock[depth];
2850
2851 if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) {
2852 ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block);
2853 return 1;
2854 }
2855 if (!compiler_unwind_fblock(c, info, 0))
2856 return 0;
2857 }
2858 return compiler_error(c, "'continue' not properly in loop");
2859 }
2860
2861
2862 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
2863
2864 SETUP_FINALLY L
2865 <code for body>
2866 POP_BLOCK
2867 BEGIN_FINALLY
2868 L:
2869 <code for finalbody>
2870 END_FINALLY
2871
2872 The special instructions use the block stack. Each block
2873 stack entry contains the instruction that created it (here
2874 SETUP_FINALLY), the level of the value stack at the time the
2875 block stack entry was created, and a label (here L).
2876
2877 SETUP_FINALLY:
2878 Pushes the current value stack level and the label
2879 onto the block stack.
2880 POP_BLOCK:
2881 Pops en entry from the block stack.
2882 BEGIN_FINALLY
2883 Pushes NULL onto the value stack.
2884 END_FINALLY:
2885 Pops 1 (NULL or int) or 6 entries from the *value* stack and restore
2886 the raised and the caught exceptions they specify.
2887
2888 The block stack is unwound when an exception is raised:
2889 when a SETUP_FINALLY entry is found, the raised and the caught
2890 exceptions are pushed onto the value stack (and the exception
2891 condition is cleared), and the interpreter jumps to the label
2892 gotten from the block stack.
2893 */
2894
2895 static int
compiler_try_finally(struct compiler * c,stmt_ty s)2896 compiler_try_finally(struct compiler *c, stmt_ty s)
2897 {
2898 basicblock *start, *newcurblock, *body, *end;
2899 int break_finally = 1;
2900
2901 body = compiler_new_block(c);
2902 end = compiler_new_block(c);
2903 if (body == NULL || end == NULL)
2904 return 0;
2905
2906 start = c->u->u_curblock;
2907
2908 /* `finally` block. Compile it first to determine if any of "break",
2909 "continue" or "return" are used in it. */
2910 compiler_use_next_block(c, end);
2911 if (!compiler_push_fblock(c, FINALLY_END, end, end))
2912 return 0;
2913 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
2914 ADDOP(c, END_FINALLY);
2915 break_finally = (c->u->u_fblock[c->u->u_nfblocks - 1].fb_exit == NULL);
2916 if (break_finally) {
2917 /* Pops a placeholder. See below */
2918 ADDOP(c, POP_TOP);
2919 }
2920 compiler_pop_fblock(c, FINALLY_END, end);
2921
2922 newcurblock = c->u->u_curblock;
2923 c->u->u_curblock = start;
2924 start->b_next = NULL;
2925
2926 /* `try` block */
2927 c->u->u_lineno_set = 0;
2928 c->u->u_lineno = s->lineno;
2929 c->u->u_col_offset = s->col_offset;
2930 if (break_finally) {
2931 /* Pushes a placeholder for the value of "return" in the "try" block
2932 to balance the stack for "break", "continue" and "return" in
2933 the "finally" block. */
2934 ADDOP_LOAD_CONST(c, Py_None);
2935 }
2936 ADDOP_JREL(c, SETUP_FINALLY, end);
2937 compiler_use_next_block(c, body);
2938 if (!compiler_push_fblock(c, break_finally ? FINALLY_TRY2 : FINALLY_TRY, body, end))
2939 return 0;
2940 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2941 if (!compiler_try_except(c, s))
2942 return 0;
2943 }
2944 else {
2945 VISIT_SEQ(c, stmt, s->v.Try.body);
2946 }
2947 ADDOP(c, POP_BLOCK);
2948 ADDOP(c, BEGIN_FINALLY);
2949 compiler_pop_fblock(c, break_finally ? FINALLY_TRY2 : FINALLY_TRY, body);
2950
2951 c->u->u_curblock->b_next = end;
2952 c->u->u_curblock = newcurblock;
2953
2954 return 1;
2955 }
2956
2957 /*
2958 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
2959 (The contents of the value stack is shown in [], with the top
2960 at the right; 'tb' is trace-back info, 'val' the exception's
2961 associated value, and 'exc' the exception.)
2962
2963 Value stack Label Instruction Argument
2964 [] SETUP_FINALLY L1
2965 [] <code for S>
2966 [] POP_BLOCK
2967 [] JUMP_FORWARD L0
2968
2969 [tb, val, exc] L1: DUP )
2970 [tb, val, exc, exc] <evaluate E1> )
2971 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2972 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2973 [tb, val, exc] POP
2974 [tb, val] <assign to V1> (or POP if no V1)
2975 [tb] POP
2976 [] <code for S1>
2977 JUMP_FORWARD L0
2978
2979 [tb, val, exc] L2: DUP
2980 .............................etc.......................
2981
2982 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2983
2984 [] L0: <next statement>
2985
2986 Of course, parts are not generated if Vi or Ei is not present.
2987 */
2988 static int
compiler_try_except(struct compiler * c,stmt_ty s)2989 compiler_try_except(struct compiler *c, stmt_ty s)
2990 {
2991 basicblock *body, *orelse, *except, *end;
2992 Py_ssize_t i, n;
2993
2994 body = compiler_new_block(c);
2995 except = compiler_new_block(c);
2996 orelse = compiler_new_block(c);
2997 end = compiler_new_block(c);
2998 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2999 return 0;
3000 ADDOP_JREL(c, SETUP_FINALLY, except);
3001 compiler_use_next_block(c, body);
3002 if (!compiler_push_fblock(c, EXCEPT, body, NULL))
3003 return 0;
3004 VISIT_SEQ(c, stmt, s->v.Try.body);
3005 ADDOP(c, POP_BLOCK);
3006 compiler_pop_fblock(c, EXCEPT, body);
3007 ADDOP_JREL(c, JUMP_FORWARD, orelse);
3008 n = asdl_seq_LEN(s->v.Try.handlers);
3009 compiler_use_next_block(c, except);
3010 for (i = 0; i < n; i++) {
3011 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
3012 s->v.Try.handlers, i);
3013 if (!handler->v.ExceptHandler.type && i < n-1)
3014 return compiler_error(c, "default 'except:' must be last");
3015 c->u->u_lineno_set = 0;
3016 c->u->u_lineno = handler->lineno;
3017 c->u->u_col_offset = handler->col_offset;
3018 except = compiler_new_block(c);
3019 if (except == NULL)
3020 return 0;
3021 if (handler->v.ExceptHandler.type) {
3022 ADDOP(c, DUP_TOP);
3023 VISIT(c, expr, handler->v.ExceptHandler.type);
3024 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
3025 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
3026 }
3027 ADDOP(c, POP_TOP);
3028 if (handler->v.ExceptHandler.name) {
3029 basicblock *cleanup_end, *cleanup_body;
3030
3031 cleanup_end = compiler_new_block(c);
3032 cleanup_body = compiler_new_block(c);
3033 if (cleanup_end == NULL || cleanup_body == NULL) {
3034 return 0;
3035 }
3036
3037 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3038 ADDOP(c, POP_TOP);
3039
3040 /*
3041 try:
3042 # body
3043 except type as name:
3044 try:
3045 # body
3046 finally:
3047 name = None # in case body contains "del name"
3048 del name
3049 */
3050
3051 /* second try: */
3052 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
3053 compiler_use_next_block(c, cleanup_body);
3054 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end))
3055 return 0;
3056
3057 /* second # body */
3058 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3059 ADDOP(c, POP_BLOCK);
3060 ADDOP(c, BEGIN_FINALLY);
3061 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3062
3063 /* finally: */
3064 compiler_use_next_block(c, cleanup_end);
3065 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL))
3066 return 0;
3067
3068 /* name = None; del name */
3069 ADDOP_LOAD_CONST(c, Py_None);
3070 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3071 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
3072
3073 ADDOP(c, END_FINALLY);
3074 ADDOP(c, POP_EXCEPT);
3075 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
3076 }
3077 else {
3078 basicblock *cleanup_body;
3079
3080 cleanup_body = compiler_new_block(c);
3081 if (!cleanup_body)
3082 return 0;
3083
3084 ADDOP(c, POP_TOP);
3085 ADDOP(c, POP_TOP);
3086 compiler_use_next_block(c, cleanup_body);
3087 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL))
3088 return 0;
3089 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3090 ADDOP(c, POP_EXCEPT);
3091 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3092 }
3093 ADDOP_JREL(c, JUMP_FORWARD, end);
3094 compiler_use_next_block(c, except);
3095 }
3096 ADDOP(c, END_FINALLY);
3097 compiler_use_next_block(c, orelse);
3098 VISIT_SEQ(c, stmt, s->v.Try.orelse);
3099 compiler_use_next_block(c, end);
3100 return 1;
3101 }
3102
3103 static int
compiler_try(struct compiler * c,stmt_ty s)3104 compiler_try(struct compiler *c, stmt_ty s) {
3105 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3106 return compiler_try_finally(c, s);
3107 else
3108 return compiler_try_except(c, s);
3109 }
3110
3111
3112 static int
compiler_import_as(struct compiler * c,identifier name,identifier asname)3113 compiler_import_as(struct compiler *c, identifier name, identifier asname)
3114 {
3115 /* The IMPORT_NAME opcode was already generated. This function
3116 merely needs to bind the result to a name.
3117
3118 If there is a dot in name, we need to split it and emit a
3119 IMPORT_FROM for each name.
3120 */
3121 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3122 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
3123 if (dot == -2)
3124 return 0;
3125 if (dot != -1) {
3126 /* Consume the base module name to get the first attribute */
3127 while (1) {
3128 Py_ssize_t pos = dot + 1;
3129 PyObject *attr;
3130 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
3131 if (dot == -2)
3132 return 0;
3133 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
3134 if (!attr)
3135 return 0;
3136 ADDOP_N(c, IMPORT_FROM, attr, names);
3137 if (dot == -1) {
3138 break;
3139 }
3140 ADDOP(c, ROT_TWO);
3141 ADDOP(c, POP_TOP);
3142 }
3143 if (!compiler_nameop(c, asname, Store)) {
3144 return 0;
3145 }
3146 ADDOP(c, POP_TOP);
3147 return 1;
3148 }
3149 return compiler_nameop(c, asname, Store);
3150 }
3151
3152 static int
compiler_import(struct compiler * c,stmt_ty s)3153 compiler_import(struct compiler *c, stmt_ty s)
3154 {
3155 /* The Import node stores a module name like a.b.c as a single
3156 string. This is convenient for all cases except
3157 import a.b.c as d
3158 where we need to parse that string to extract the individual
3159 module names.
3160 XXX Perhaps change the representation to make this case simpler?
3161 */
3162 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
3163
3164 for (i = 0; i < n; i++) {
3165 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3166 int r;
3167
3168 ADDOP_LOAD_CONST(c, _PyLong_Zero);
3169 ADDOP_LOAD_CONST(c, Py_None);
3170 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
3171
3172 if (alias->asname) {
3173 r = compiler_import_as(c, alias->name, alias->asname);
3174 if (!r)
3175 return r;
3176 }
3177 else {
3178 identifier tmp = alias->name;
3179 Py_ssize_t dot = PyUnicode_FindChar(
3180 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
3181 if (dot != -1) {
3182 tmp = PyUnicode_Substring(alias->name, 0, dot);
3183 if (tmp == NULL)
3184 return 0;
3185 }
3186 r = compiler_nameop(c, tmp, Store);
3187 if (dot != -1) {
3188 Py_DECREF(tmp);
3189 }
3190 if (!r)
3191 return r;
3192 }
3193 }
3194 return 1;
3195 }
3196
3197 static int
compiler_from_import(struct compiler * c,stmt_ty s)3198 compiler_from_import(struct compiler *c, stmt_ty s)
3199 {
3200 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
3201 PyObject *names;
3202 static PyObject *empty_string;
3203
3204 if (!empty_string) {
3205 empty_string = PyUnicode_FromString("");
3206 if (!empty_string)
3207 return 0;
3208 }
3209
3210 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
3211
3212 names = PyTuple_New(n);
3213 if (!names)
3214 return 0;
3215
3216 /* build up the names */
3217 for (i = 0; i < n; i++) {
3218 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3219 Py_INCREF(alias->name);
3220 PyTuple_SET_ITEM(names, i, alias->name);
3221 }
3222
3223 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
3224 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
3225 Py_DECREF(names);
3226 return compiler_error(c, "from __future__ imports must occur "
3227 "at the beginning of the file");
3228 }
3229 ADDOP_LOAD_CONST_NEW(c, names);
3230
3231 if (s->v.ImportFrom.module) {
3232 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3233 }
3234 else {
3235 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3236 }
3237 for (i = 0; i < n; i++) {
3238 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3239 identifier store_name;
3240
3241 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
3242 assert(n == 1);
3243 ADDOP(c, IMPORT_STAR);
3244 return 1;
3245 }
3246
3247 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3248 store_name = alias->name;
3249 if (alias->asname)
3250 store_name = alias->asname;
3251
3252 if (!compiler_nameop(c, store_name, Store)) {
3253 return 0;
3254 }
3255 }
3256 /* remove imported module */
3257 ADDOP(c, POP_TOP);
3258 return 1;
3259 }
3260
3261 static int
compiler_assert(struct compiler * c,stmt_ty s)3262 compiler_assert(struct compiler *c, stmt_ty s)
3263 {
3264 static PyObject *assertion_error = NULL;
3265 basicblock *end;
3266
3267 if (c->c_optimize)
3268 return 1;
3269 if (assertion_error == NULL) {
3270 assertion_error = PyUnicode_InternFromString("AssertionError");
3271 if (assertion_error == NULL)
3272 return 0;
3273 }
3274 if (s->v.Assert.test->kind == Tuple_kind &&
3275 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
3276 {
3277 if (!compiler_warn(c, "assertion is always true, "
3278 "perhaps remove parentheses?"))
3279 {
3280 return 0;
3281 }
3282 }
3283 end = compiler_new_block(c);
3284 if (end == NULL)
3285 return 0;
3286 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3287 return 0;
3288 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
3289 if (s->v.Assert.msg) {
3290 VISIT(c, expr, s->v.Assert.msg);
3291 ADDOP_I(c, CALL_FUNCTION, 1);
3292 }
3293 ADDOP_I(c, RAISE_VARARGS, 1);
3294 compiler_use_next_block(c, end);
3295 return 1;
3296 }
3297
3298 static int
compiler_visit_stmt_expr(struct compiler * c,expr_ty value)3299 compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3300 {
3301 if (c->c_interactive && c->c_nestlevel <= 1) {
3302 VISIT(c, expr, value);
3303 ADDOP(c, PRINT_EXPR);
3304 return 1;
3305 }
3306
3307 if (value->kind == Constant_kind) {
3308 /* ignore constant statement */
3309 return 1;
3310 }
3311
3312 VISIT(c, expr, value);
3313 ADDOP(c, POP_TOP);
3314 return 1;
3315 }
3316
3317 static int
compiler_visit_stmt(struct compiler * c,stmt_ty s)3318 compiler_visit_stmt(struct compiler *c, stmt_ty s)
3319 {
3320 Py_ssize_t i, n;
3321
3322 /* Always assign a lineno to the next instruction for a stmt. */
3323 c->u->u_lineno = s->lineno;
3324 c->u->u_col_offset = s->col_offset;
3325 c->u->u_lineno_set = 0;
3326
3327 switch (s->kind) {
3328 case FunctionDef_kind:
3329 return compiler_function(c, s, 0);
3330 case ClassDef_kind:
3331 return compiler_class(c, s);
3332 case Return_kind:
3333 return compiler_return(c, s);
3334 case Delete_kind:
3335 VISIT_SEQ(c, expr, s->v.Delete.targets)
3336 break;
3337 case Assign_kind:
3338 n = asdl_seq_LEN(s->v.Assign.targets);
3339 VISIT(c, expr, s->v.Assign.value);
3340 for (i = 0; i < n; i++) {
3341 if (i < n - 1)
3342 ADDOP(c, DUP_TOP);
3343 VISIT(c, expr,
3344 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3345 }
3346 break;
3347 case AugAssign_kind:
3348 return compiler_augassign(c, s);
3349 case AnnAssign_kind:
3350 return compiler_annassign(c, s);
3351 case For_kind:
3352 return compiler_for(c, s);
3353 case While_kind:
3354 return compiler_while(c, s);
3355 case If_kind:
3356 return compiler_if(c, s);
3357 case Raise_kind:
3358 n = 0;
3359 if (s->v.Raise.exc) {
3360 VISIT(c, expr, s->v.Raise.exc);
3361 n++;
3362 if (s->v.Raise.cause) {
3363 VISIT(c, expr, s->v.Raise.cause);
3364 n++;
3365 }
3366 }
3367 ADDOP_I(c, RAISE_VARARGS, (int)n);
3368 break;
3369 case Try_kind:
3370 return compiler_try(c, s);
3371 case Assert_kind:
3372 return compiler_assert(c, s);
3373 case Import_kind:
3374 return compiler_import(c, s);
3375 case ImportFrom_kind:
3376 return compiler_from_import(c, s);
3377 case Global_kind:
3378 case Nonlocal_kind:
3379 break;
3380 case Expr_kind:
3381 return compiler_visit_stmt_expr(c, s->v.Expr.value);
3382 case Pass_kind:
3383 break;
3384 case Break_kind:
3385 return compiler_break(c);
3386 case Continue_kind:
3387 return compiler_continue(c);
3388 case With_kind:
3389 return compiler_with(c, s, 0);
3390 case AsyncFunctionDef_kind:
3391 return compiler_function(c, s, 1);
3392 case AsyncWith_kind:
3393 return compiler_async_with(c, s, 0);
3394 case AsyncFor_kind:
3395 return compiler_async_for(c, s);
3396 }
3397
3398 return 1;
3399 }
3400
3401 static int
unaryop(unaryop_ty op)3402 unaryop(unaryop_ty op)
3403 {
3404 switch (op) {
3405 case Invert:
3406 return UNARY_INVERT;
3407 case Not:
3408 return UNARY_NOT;
3409 case UAdd:
3410 return UNARY_POSITIVE;
3411 case USub:
3412 return UNARY_NEGATIVE;
3413 default:
3414 PyErr_Format(PyExc_SystemError,
3415 "unary op %d should not be possible", op);
3416 return 0;
3417 }
3418 }
3419
3420 static int
binop(struct compiler * c,operator_ty op)3421 binop(struct compiler *c, operator_ty op)
3422 {
3423 switch (op) {
3424 case Add:
3425 return BINARY_ADD;
3426 case Sub:
3427 return BINARY_SUBTRACT;
3428 case Mult:
3429 return BINARY_MULTIPLY;
3430 case MatMult:
3431 return BINARY_MATRIX_MULTIPLY;
3432 case Div:
3433 return BINARY_TRUE_DIVIDE;
3434 case Mod:
3435 return BINARY_MODULO;
3436 case Pow:
3437 return BINARY_POWER;
3438 case LShift:
3439 return BINARY_LSHIFT;
3440 case RShift:
3441 return BINARY_RSHIFT;
3442 case BitOr:
3443 return BINARY_OR;
3444 case BitXor:
3445 return BINARY_XOR;
3446 case BitAnd:
3447 return BINARY_AND;
3448 case FloorDiv:
3449 return BINARY_FLOOR_DIVIDE;
3450 default:
3451 PyErr_Format(PyExc_SystemError,
3452 "binary op %d should not be possible", op);
3453 return 0;
3454 }
3455 }
3456
3457 static int
inplace_binop(struct compiler * c,operator_ty op)3458 inplace_binop(struct compiler *c, operator_ty op)
3459 {
3460 switch (op) {
3461 case Add:
3462 return INPLACE_ADD;
3463 case Sub:
3464 return INPLACE_SUBTRACT;
3465 case Mult:
3466 return INPLACE_MULTIPLY;
3467 case MatMult:
3468 return INPLACE_MATRIX_MULTIPLY;
3469 case Div:
3470 return INPLACE_TRUE_DIVIDE;
3471 case Mod:
3472 return INPLACE_MODULO;
3473 case Pow:
3474 return INPLACE_POWER;
3475 case LShift:
3476 return INPLACE_LSHIFT;
3477 case RShift:
3478 return INPLACE_RSHIFT;
3479 case BitOr:
3480 return INPLACE_OR;
3481 case BitXor:
3482 return INPLACE_XOR;
3483 case BitAnd:
3484 return INPLACE_AND;
3485 case FloorDiv:
3486 return INPLACE_FLOOR_DIVIDE;
3487 default:
3488 PyErr_Format(PyExc_SystemError,
3489 "inplace binary op %d should not be possible", op);
3490 return 0;
3491 }
3492 }
3493
3494 static int
compiler_nameop(struct compiler * c,identifier name,expr_context_ty ctx)3495 compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3496 {
3497 int op, scope;
3498 Py_ssize_t arg;
3499 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
3500
3501 PyObject *dict = c->u->u_names;
3502 PyObject *mangled;
3503 /* XXX AugStore isn't used anywhere! */
3504
3505 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3506 !_PyUnicode_EqualToASCIIString(name, "True") &&
3507 !_PyUnicode_EqualToASCIIString(name, "False"));
3508
3509 mangled = _Py_Mangle(c->u->u_private, name);
3510 if (!mangled)
3511 return 0;
3512
3513 op = 0;
3514 optype = OP_NAME;
3515 scope = PyST_GetScope(c->u->u_ste, mangled);
3516 switch (scope) {
3517 case FREE:
3518 dict = c->u->u_freevars;
3519 optype = OP_DEREF;
3520 break;
3521 case CELL:
3522 dict = c->u->u_cellvars;
3523 optype = OP_DEREF;
3524 break;
3525 case LOCAL:
3526 if (c->u->u_ste->ste_type == FunctionBlock)
3527 optype = OP_FAST;
3528 break;
3529 case GLOBAL_IMPLICIT:
3530 if (c->u->u_ste->ste_type == FunctionBlock)
3531 optype = OP_GLOBAL;
3532 break;
3533 case GLOBAL_EXPLICIT:
3534 optype = OP_GLOBAL;
3535 break;
3536 default:
3537 /* scope can be 0 */
3538 break;
3539 }
3540
3541 /* XXX Leave assert here, but handle __doc__ and the like better */
3542 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
3543
3544 switch (optype) {
3545 case OP_DEREF:
3546 switch (ctx) {
3547 case Load:
3548 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3549 break;
3550 case Store:
3551 op = STORE_DEREF;
3552 break;
3553 case AugLoad:
3554 case AugStore:
3555 break;
3556 case Del: op = DELETE_DEREF; break;
3557 case Param:
3558 default:
3559 PyErr_SetString(PyExc_SystemError,
3560 "param invalid for deref variable");
3561 return 0;
3562 }
3563 break;
3564 case OP_FAST:
3565 switch (ctx) {
3566 case Load: op = LOAD_FAST; break;
3567 case Store:
3568 op = STORE_FAST;
3569 break;
3570 case Del: op = DELETE_FAST; break;
3571 case AugLoad:
3572 case AugStore:
3573 break;
3574 case Param:
3575 default:
3576 PyErr_SetString(PyExc_SystemError,
3577 "param invalid for local variable");
3578 return 0;
3579 }
3580 ADDOP_N(c, op, mangled, varnames);
3581 return 1;
3582 case OP_GLOBAL:
3583 switch (ctx) {
3584 case Load: op = LOAD_GLOBAL; break;
3585 case Store:
3586 op = STORE_GLOBAL;
3587 break;
3588 case Del: op = DELETE_GLOBAL; break;
3589 case AugLoad:
3590 case AugStore:
3591 break;
3592 case Param:
3593 default:
3594 PyErr_SetString(PyExc_SystemError,
3595 "param invalid for global variable");
3596 return 0;
3597 }
3598 break;
3599 case OP_NAME:
3600 switch (ctx) {
3601 case Load: op = LOAD_NAME; break;
3602 case Store:
3603 op = STORE_NAME;
3604 break;
3605 case Del: op = DELETE_NAME; break;
3606 case AugLoad:
3607 case AugStore:
3608 break;
3609 case Param:
3610 default:
3611 PyErr_SetString(PyExc_SystemError,
3612 "param invalid for name variable");
3613 return 0;
3614 }
3615 break;
3616 }
3617
3618 assert(op);
3619 arg = compiler_add_o(c, dict, mangled);
3620 Py_DECREF(mangled);
3621 if (arg < 0)
3622 return 0;
3623 return compiler_addop_i(c, op, arg);
3624 }
3625
3626 static int
compiler_boolop(struct compiler * c,expr_ty e)3627 compiler_boolop(struct compiler *c, expr_ty e)
3628 {
3629 basicblock *end;
3630 int jumpi;
3631 Py_ssize_t i, n;
3632 asdl_seq *s;
3633
3634 assert(e->kind == BoolOp_kind);
3635 if (e->v.BoolOp.op == And)
3636 jumpi = JUMP_IF_FALSE_OR_POP;
3637 else
3638 jumpi = JUMP_IF_TRUE_OR_POP;
3639 end = compiler_new_block(c);
3640 if (end == NULL)
3641 return 0;
3642 s = e->v.BoolOp.values;
3643 n = asdl_seq_LEN(s) - 1;
3644 assert(n >= 0);
3645 for (i = 0; i < n; ++i) {
3646 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3647 ADDOP_JABS(c, jumpi, end);
3648 }
3649 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3650 compiler_use_next_block(c, end);
3651 return 1;
3652 }
3653
3654 static int
starunpack_helper(struct compiler * c,asdl_seq * elts,int single_op,int inner_op,int outer_op)3655 starunpack_helper(struct compiler *c, asdl_seq *elts,
3656 int single_op, int inner_op, int outer_op)
3657 {
3658 Py_ssize_t n = asdl_seq_LEN(elts);
3659 Py_ssize_t i, nsubitems = 0, nseen = 0;
3660 for (i = 0; i < n; i++) {
3661 expr_ty elt = asdl_seq_GET(elts, i);
3662 if (elt->kind == Starred_kind) {
3663 if (nseen) {
3664 ADDOP_I(c, inner_op, nseen);
3665 nseen = 0;
3666 nsubitems++;
3667 }
3668 VISIT(c, expr, elt->v.Starred.value);
3669 nsubitems++;
3670 }
3671 else {
3672 VISIT(c, expr, elt);
3673 nseen++;
3674 }
3675 }
3676 if (nsubitems) {
3677 if (nseen) {
3678 ADDOP_I(c, inner_op, nseen);
3679 nsubitems++;
3680 }
3681 ADDOP_I(c, outer_op, nsubitems);
3682 }
3683 else
3684 ADDOP_I(c, single_op, nseen);
3685 return 1;
3686 }
3687
3688 static int
assignment_helper(struct compiler * c,asdl_seq * elts)3689 assignment_helper(struct compiler *c, asdl_seq *elts)
3690 {
3691 Py_ssize_t n = asdl_seq_LEN(elts);
3692 Py_ssize_t i;
3693 int seen_star = 0;
3694 for (i = 0; i < n; i++) {
3695 expr_ty elt = asdl_seq_GET(elts, i);
3696 if (elt->kind == Starred_kind && !seen_star) {
3697 if ((i >= (1 << 8)) ||
3698 (n-i-1 >= (INT_MAX >> 8)))
3699 return compiler_error(c,
3700 "too many expressions in "
3701 "star-unpacking assignment");
3702 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3703 seen_star = 1;
3704 asdl_seq_SET(elts, i, elt->v.Starred.value);
3705 }
3706 else if (elt->kind == Starred_kind) {
3707 return compiler_error(c,
3708 "two starred expressions in assignment");
3709 }
3710 }
3711 if (!seen_star) {
3712 ADDOP_I(c, UNPACK_SEQUENCE, n);
3713 }
3714 VISIT_SEQ(c, expr, elts);
3715 return 1;
3716 }
3717
3718 static int
compiler_list(struct compiler * c,expr_ty e)3719 compiler_list(struct compiler *c, expr_ty e)
3720 {
3721 asdl_seq *elts = e->v.List.elts;
3722 if (e->v.List.ctx == Store) {
3723 return assignment_helper(c, elts);
3724 }
3725 else if (e->v.List.ctx == Load) {
3726 return starunpack_helper(c, elts,
3727 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
3728 }
3729 else
3730 VISIT_SEQ(c, expr, elts);
3731 return 1;
3732 }
3733
3734 static int
compiler_tuple(struct compiler * c,expr_ty e)3735 compiler_tuple(struct compiler *c, expr_ty e)
3736 {
3737 asdl_seq *elts = e->v.Tuple.elts;
3738 if (e->v.Tuple.ctx == Store) {
3739 return assignment_helper(c, elts);
3740 }
3741 else if (e->v.Tuple.ctx == Load) {
3742 return starunpack_helper(c, elts,
3743 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3744 }
3745 else
3746 VISIT_SEQ(c, expr, elts);
3747 return 1;
3748 }
3749
3750 static int
compiler_set(struct compiler * c,expr_ty e)3751 compiler_set(struct compiler *c, expr_ty e)
3752 {
3753 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3754 BUILD_SET, BUILD_SET_UNPACK);
3755 }
3756
3757 static int
are_all_items_const(asdl_seq * seq,Py_ssize_t begin,Py_ssize_t end)3758 are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3759 {
3760 Py_ssize_t i;
3761 for (i = begin; i < end; i++) {
3762 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3763 if (key == NULL || key->kind != Constant_kind)
3764 return 0;
3765 }
3766 return 1;
3767 }
3768
3769 static int
compiler_subdict(struct compiler * c,expr_ty e,Py_ssize_t begin,Py_ssize_t end)3770 compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3771 {
3772 Py_ssize_t i, n = end - begin;
3773 PyObject *keys, *key;
3774 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3775 for (i = begin; i < end; i++) {
3776 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3777 }
3778 keys = PyTuple_New(n);
3779 if (keys == NULL) {
3780 return 0;
3781 }
3782 for (i = begin; i < end; i++) {
3783 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
3784 Py_INCREF(key);
3785 PyTuple_SET_ITEM(keys, i - begin, key);
3786 }
3787 ADDOP_LOAD_CONST_NEW(c, keys);
3788 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3789 }
3790 else {
3791 for (i = begin; i < end; i++) {
3792 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3793 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3794 }
3795 ADDOP_I(c, BUILD_MAP, n);
3796 }
3797 return 1;
3798 }
3799
3800 static int
compiler_dict(struct compiler * c,expr_ty e)3801 compiler_dict(struct compiler *c, expr_ty e)
3802 {
3803 Py_ssize_t i, n, elements;
3804 int containers;
3805 int is_unpacking = 0;
3806 n = asdl_seq_LEN(e->v.Dict.values);
3807 containers = 0;
3808 elements = 0;
3809 for (i = 0; i < n; i++) {
3810 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3811 if (elements == 0xFFFF || (elements && is_unpacking)) {
3812 if (!compiler_subdict(c, e, i - elements, i))
3813 return 0;
3814 containers++;
3815 elements = 0;
3816 }
3817 if (is_unpacking) {
3818 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3819 containers++;
3820 }
3821 else {
3822 elements++;
3823 }
3824 }
3825 if (elements || containers == 0) {
3826 if (!compiler_subdict(c, e, n - elements, n))
3827 return 0;
3828 containers++;
3829 }
3830 /* If there is more than one dict, they need to be merged into a new
3831 * dict. If there is one dict and it's an unpacking, then it needs
3832 * to be copied into a new dict." */
3833 if (containers > 1 || is_unpacking) {
3834 ADDOP_I(c, BUILD_MAP_UNPACK, containers);
3835 }
3836 return 1;
3837 }
3838
3839 static int
compiler_compare(struct compiler * c,expr_ty e)3840 compiler_compare(struct compiler *c, expr_ty e)
3841 {
3842 Py_ssize_t i, n;
3843
3844 if (!check_compare(c, e)) {
3845 return 0;
3846 }
3847 VISIT(c, expr, e->v.Compare.left);
3848 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3849 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3850 if (n == 0) {
3851 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3852 ADDOP_I(c, COMPARE_OP,
3853 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0))));
3854 }
3855 else {
3856 basicblock *cleanup = compiler_new_block(c);
3857 if (cleanup == NULL)
3858 return 0;
3859 for (i = 0; i < n; i++) {
3860 VISIT(c, expr,
3861 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
3862 ADDOP(c, DUP_TOP);
3863 ADDOP(c, ROT_THREE);
3864 ADDOP_I(c, COMPARE_OP,
3865 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
3866 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3867 NEXT_BLOCK(c);
3868 }
3869 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
3870 ADDOP_I(c, COMPARE_OP,
3871 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
3872 basicblock *end = compiler_new_block(c);
3873 if (end == NULL)
3874 return 0;
3875 ADDOP_JREL(c, JUMP_FORWARD, end);
3876 compiler_use_next_block(c, cleanup);
3877 ADDOP(c, ROT_TWO);
3878 ADDOP(c, POP_TOP);
3879 compiler_use_next_block(c, end);
3880 }
3881 return 1;
3882 }
3883
3884 static PyTypeObject *
infer_type(expr_ty e)3885 infer_type(expr_ty e)
3886 {
3887 switch (e->kind) {
3888 case Tuple_kind:
3889 return &PyTuple_Type;
3890 case List_kind:
3891 case ListComp_kind:
3892 return &PyList_Type;
3893 case Dict_kind:
3894 case DictComp_kind:
3895 return &PyDict_Type;
3896 case Set_kind:
3897 case SetComp_kind:
3898 return &PySet_Type;
3899 case GeneratorExp_kind:
3900 return &PyGen_Type;
3901 case Lambda_kind:
3902 return &PyFunction_Type;
3903 case JoinedStr_kind:
3904 case FormattedValue_kind:
3905 return &PyUnicode_Type;
3906 case Constant_kind:
3907 return e->v.Constant.value->ob_type;
3908 default:
3909 return NULL;
3910 }
3911 }
3912
3913 static int
check_caller(struct compiler * c,expr_ty e)3914 check_caller(struct compiler *c, expr_ty e)
3915 {
3916 switch (e->kind) {
3917 case Constant_kind:
3918 case Tuple_kind:
3919 case List_kind:
3920 case ListComp_kind:
3921 case Dict_kind:
3922 case DictComp_kind:
3923 case Set_kind:
3924 case SetComp_kind:
3925 case GeneratorExp_kind:
3926 case JoinedStr_kind:
3927 case FormattedValue_kind:
3928 return compiler_warn(c, "'%.200s' object is not callable; "
3929 "perhaps you missed a comma?",
3930 infer_type(e)->tp_name);
3931 default:
3932 return 1;
3933 }
3934 }
3935
3936 static int
check_subscripter(struct compiler * c,expr_ty e)3937 check_subscripter(struct compiler *c, expr_ty e)
3938 {
3939 PyObject *v;
3940
3941 switch (e->kind) {
3942 case Constant_kind:
3943 v = e->v.Constant.value;
3944 if (!(v == Py_None || v == Py_Ellipsis ||
3945 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
3946 PyAnySet_Check(v)))
3947 {
3948 return 1;
3949 }
3950 /* fall through */
3951 case Set_kind:
3952 case SetComp_kind:
3953 case GeneratorExp_kind:
3954 case Lambda_kind:
3955 return compiler_warn(c, "'%.200s' object is not subscriptable; "
3956 "perhaps you missed a comma?",
3957 infer_type(e)->tp_name);
3958 default:
3959 return 1;
3960 }
3961 }
3962
3963 static int
check_index(struct compiler * c,expr_ty e,slice_ty s)3964 check_index(struct compiler *c, expr_ty e, slice_ty s)
3965 {
3966 PyObject *v;
3967
3968 if (s->kind != Index_kind) {
3969 return 1;
3970 }
3971 PyTypeObject *index_type = infer_type(s->v.Index.value);
3972 if (index_type == NULL
3973 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
3974 || index_type == &PySlice_Type) {
3975 return 1;
3976 }
3977
3978 switch (e->kind) {
3979 case Constant_kind:
3980 v = e->v.Constant.value;
3981 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
3982 return 1;
3983 }
3984 /* fall through */
3985 case Tuple_kind:
3986 case List_kind:
3987 case ListComp_kind:
3988 case JoinedStr_kind:
3989 case FormattedValue_kind:
3990 return compiler_warn(c, "%.200s indices must be integers or slices, "
3991 "not %.200s; "
3992 "perhaps you missed a comma?",
3993 infer_type(e)->tp_name,
3994 index_type->tp_name);
3995 default:
3996 return 1;
3997 }
3998 }
3999
4000 // Return 1 if the method call was optimized, -1 if not, and 0 on error.
4001 static int
maybe_optimize_method_call(struct compiler * c,expr_ty e)4002 maybe_optimize_method_call(struct compiler *c, expr_ty e)
4003 {
4004 Py_ssize_t argsl, i;
4005 expr_ty meth = e->v.Call.func;
4006 asdl_seq *args = e->v.Call.args;
4007
4008 /* Check that the call node is an attribute access, and that
4009 the call doesn't have keyword parameters. */
4010 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4011 asdl_seq_LEN(e->v.Call.keywords))
4012 return -1;
4013
4014 /* Check that there are no *varargs types of arguments. */
4015 argsl = asdl_seq_LEN(args);
4016 for (i = 0; i < argsl; i++) {
4017 expr_ty elt = asdl_seq_GET(args, i);
4018 if (elt->kind == Starred_kind) {
4019 return -1;
4020 }
4021 }
4022
4023 /* Alright, we can optimize the code. */
4024 VISIT(c, expr, meth->v.Attribute.value);
4025 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4026 VISIT_SEQ(c, expr, e->v.Call.args);
4027 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4028 return 1;
4029 }
4030
4031 static int
compiler_call(struct compiler * c,expr_ty e)4032 compiler_call(struct compiler *c, expr_ty e)
4033 {
4034 int ret = maybe_optimize_method_call(c, e);
4035 if (ret >= 0) {
4036 return ret;
4037 }
4038 if (!check_caller(c, e->v.Call.func)) {
4039 return 0;
4040 }
4041 VISIT(c, expr, e->v.Call.func);
4042 return compiler_call_helper(c, 0,
4043 e->v.Call.args,
4044 e->v.Call.keywords);
4045 }
4046
4047 static int
compiler_joined_str(struct compiler * c,expr_ty e)4048 compiler_joined_str(struct compiler *c, expr_ty e)
4049 {
4050 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4051 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4052 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4053 return 1;
4054 }
4055
4056 /* Used to implement f-strings. Format a single value. */
4057 static int
compiler_formatted_value(struct compiler * c,expr_ty e)4058 compiler_formatted_value(struct compiler *c, expr_ty e)
4059 {
4060 /* Our oparg encodes 2 pieces of information: the conversion
4061 character, and whether or not a format_spec was provided.
4062
4063 Convert the conversion char to 3 bits:
4064 : 000 0x0 FVC_NONE The default if nothing specified.
4065 !s : 001 0x1 FVC_STR
4066 !r : 010 0x2 FVC_REPR
4067 !a : 011 0x3 FVC_ASCII
4068
4069 next bit is whether or not we have a format spec:
4070 yes : 100 0x4
4071 no : 000 0x0
4072 */
4073
4074 int conversion = e->v.FormattedValue.conversion;
4075 int oparg;
4076
4077 /* The expression to be formatted. */
4078 VISIT(c, expr, e->v.FormattedValue.value);
4079
4080 switch (conversion) {
4081 case 's': oparg = FVC_STR; break;
4082 case 'r': oparg = FVC_REPR; break;
4083 case 'a': oparg = FVC_ASCII; break;
4084 case -1: oparg = FVC_NONE; break;
4085 default:
4086 PyErr_Format(PyExc_SystemError,
4087 "Unrecognized conversion character %d", conversion);
4088 return 0;
4089 }
4090 if (e->v.FormattedValue.format_spec) {
4091 /* Evaluate the format spec, and update our opcode arg. */
4092 VISIT(c, expr, e->v.FormattedValue.format_spec);
4093 oparg |= FVS_HAVE_SPEC;
4094 }
4095
4096 /* And push our opcode and oparg */
4097 ADDOP_I(c, FORMAT_VALUE, oparg);
4098
4099 return 1;
4100 }
4101
4102 static int
compiler_subkwargs(struct compiler * c,asdl_seq * keywords,Py_ssize_t begin,Py_ssize_t end)4103 compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
4104 {
4105 Py_ssize_t i, n = end - begin;
4106 keyword_ty kw;
4107 PyObject *keys, *key;
4108 assert(n > 0);
4109 if (n > 1) {
4110 for (i = begin; i < end; i++) {
4111 kw = asdl_seq_GET(keywords, i);
4112 VISIT(c, expr, kw->value);
4113 }
4114 keys = PyTuple_New(n);
4115 if (keys == NULL) {
4116 return 0;
4117 }
4118 for (i = begin; i < end; i++) {
4119 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4120 Py_INCREF(key);
4121 PyTuple_SET_ITEM(keys, i - begin, key);
4122 }
4123 ADDOP_LOAD_CONST_NEW(c, keys);
4124 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4125 }
4126 else {
4127 /* a for loop only executes once */
4128 for (i = begin; i < end; i++) {
4129 kw = asdl_seq_GET(keywords, i);
4130 ADDOP_LOAD_CONST(c, kw->arg);
4131 VISIT(c, expr, kw->value);
4132 }
4133 ADDOP_I(c, BUILD_MAP, n);
4134 }
4135 return 1;
4136 }
4137
4138 /* shared code between compiler_call and compiler_class */
4139 static int
compiler_call_helper(struct compiler * c,int n,asdl_seq * args,asdl_seq * keywords)4140 compiler_call_helper(struct compiler *c,
4141 int n, /* Args already pushed */
4142 asdl_seq *args,
4143 asdl_seq *keywords)
4144 {
4145 Py_ssize_t i, nseen, nelts, nkwelts;
4146 int mustdictunpack = 0;
4147
4148 /* the number of tuples and dictionaries on the stack */
4149 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
4150
4151 nelts = asdl_seq_LEN(args);
4152 nkwelts = asdl_seq_LEN(keywords);
4153
4154 for (i = 0; i < nkwelts; i++) {
4155 keyword_ty kw = asdl_seq_GET(keywords, i);
4156 if (kw->arg == NULL) {
4157 mustdictunpack = 1;
4158 break;
4159 }
4160 }
4161
4162 nseen = n; /* the number of positional arguments on the stack */
4163 for (i = 0; i < nelts; i++) {
4164 expr_ty elt = asdl_seq_GET(args, i);
4165 if (elt->kind == Starred_kind) {
4166 /* A star-arg. If we've seen positional arguments,
4167 pack the positional arguments into a tuple. */
4168 if (nseen) {
4169 ADDOP_I(c, BUILD_TUPLE, nseen);
4170 nseen = 0;
4171 nsubargs++;
4172 }
4173 VISIT(c, expr, elt->v.Starred.value);
4174 nsubargs++;
4175 }
4176 else {
4177 VISIT(c, expr, elt);
4178 nseen++;
4179 }
4180 }
4181
4182 /* Same dance again for keyword arguments */
4183 if (nsubargs || mustdictunpack) {
4184 if (nseen) {
4185 /* Pack up any trailing positional arguments. */
4186 ADDOP_I(c, BUILD_TUPLE, nseen);
4187 nsubargs++;
4188 }
4189 if (nsubargs > 1) {
4190 /* If we ended up with more than one stararg, we need
4191 to concatenate them into a single sequence. */
4192 ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
4193 }
4194 else if (nsubargs == 0) {
4195 ADDOP_I(c, BUILD_TUPLE, 0);
4196 }
4197 nseen = 0; /* the number of keyword arguments on the stack following */
4198 for (i = 0; i < nkwelts; i++) {
4199 keyword_ty kw = asdl_seq_GET(keywords, i);
4200 if (kw->arg == NULL) {
4201 /* A keyword argument unpacking. */
4202 if (nseen) {
4203 if (!compiler_subkwargs(c, keywords, i - nseen, i))
4204 return 0;
4205 nsubkwargs++;
4206 nseen = 0;
4207 }
4208 VISIT(c, expr, kw->value);
4209 nsubkwargs++;
4210 }
4211 else {
4212 nseen++;
4213 }
4214 }
4215 if (nseen) {
4216 /* Pack up any trailing keyword arguments. */
4217 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
4218 return 0;
4219 nsubkwargs++;
4220 }
4221 if (nsubkwargs > 1) {
4222 /* Pack it all up */
4223 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
4224 }
4225 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
4226 return 1;
4227 }
4228 else if (nkwelts) {
4229 PyObject *names;
4230 VISIT_SEQ(c, keyword, keywords);
4231 names = PyTuple_New(nkwelts);
4232 if (names == NULL) {
4233 return 0;
4234 }
4235 for (i = 0; i < nkwelts; i++) {
4236 keyword_ty kw = asdl_seq_GET(keywords, i);
4237 Py_INCREF(kw->arg);
4238 PyTuple_SET_ITEM(names, i, kw->arg);
4239 }
4240 ADDOP_LOAD_CONST_NEW(c, names);
4241 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4242 return 1;
4243 }
4244 else {
4245 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4246 return 1;
4247 }
4248 }
4249
4250
4251 /* List and set comprehensions and generator expressions work by creating a
4252 nested function to perform the actual iteration. This means that the
4253 iteration variables don't leak into the current scope.
4254 The defined function is called immediately following its definition, with the
4255 result of that call being the result of the expression.
4256 The LC/SC version returns the populated container, while the GE version is
4257 flagged in symtable.c as a generator, so it returns the generator object
4258 when the function is called.
4259
4260 Possible cleanups:
4261 - iterate over the generator sequence instead of using recursion
4262 */
4263
4264
4265 static int
compiler_comprehension_generator(struct compiler * c,asdl_seq * generators,int gen_index,expr_ty elt,expr_ty val,int type)4266 compiler_comprehension_generator(struct compiler *c,
4267 asdl_seq *generators, int gen_index,
4268 expr_ty elt, expr_ty val, int type)
4269 {
4270 comprehension_ty gen;
4271 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4272 if (gen->is_async) {
4273 return compiler_async_comprehension_generator(
4274 c, generators, gen_index, elt, val, type);
4275 } else {
4276 return compiler_sync_comprehension_generator(
4277 c, generators, gen_index, elt, val, type);
4278 }
4279 }
4280
4281 static int
compiler_sync_comprehension_generator(struct compiler * c,asdl_seq * generators,int gen_index,expr_ty elt,expr_ty val,int type)4282 compiler_sync_comprehension_generator(struct compiler *c,
4283 asdl_seq *generators, int gen_index,
4284 expr_ty elt, expr_ty val, int type)
4285 {
4286 /* generate code for the iterator, then each of the ifs,
4287 and then write to the element */
4288
4289 comprehension_ty gen;
4290 basicblock *start, *anchor, *skip, *if_cleanup;
4291 Py_ssize_t i, n;
4292
4293 start = compiler_new_block(c);
4294 skip = compiler_new_block(c);
4295 if_cleanup = compiler_new_block(c);
4296 anchor = compiler_new_block(c);
4297
4298 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4299 anchor == NULL)
4300 return 0;
4301
4302 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4303
4304 if (gen_index == 0) {
4305 /* Receive outermost iter as an implicit argument */
4306 c->u->u_argcount = 1;
4307 ADDOP_I(c, LOAD_FAST, 0);
4308 }
4309 else {
4310 /* Sub-iter - calculate on the fly */
4311 VISIT(c, expr, gen->iter);
4312 ADDOP(c, GET_ITER);
4313 }
4314 compiler_use_next_block(c, start);
4315 ADDOP_JREL(c, FOR_ITER, anchor);
4316 NEXT_BLOCK(c);
4317 VISIT(c, expr, gen->target);
4318
4319 /* XXX this needs to be cleaned up...a lot! */
4320 n = asdl_seq_LEN(gen->ifs);
4321 for (i = 0; i < n; i++) {
4322 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
4323 if (!compiler_jump_if(c, e, if_cleanup, 0))
4324 return 0;
4325 NEXT_BLOCK(c);
4326 }
4327
4328 if (++gen_index < asdl_seq_LEN(generators))
4329 if (!compiler_comprehension_generator(c,
4330 generators, gen_index,
4331 elt, val, type))
4332 return 0;
4333
4334 /* only append after the last for generator */
4335 if (gen_index >= asdl_seq_LEN(generators)) {
4336 /* comprehension specific code */
4337 switch (type) {
4338 case COMP_GENEXP:
4339 VISIT(c, expr, elt);
4340 ADDOP(c, YIELD_VALUE);
4341 ADDOP(c, POP_TOP);
4342 break;
4343 case COMP_LISTCOMP:
4344 VISIT(c, expr, elt);
4345 ADDOP_I(c, LIST_APPEND, gen_index + 1);
4346 break;
4347 case COMP_SETCOMP:
4348 VISIT(c, expr, elt);
4349 ADDOP_I(c, SET_ADD, gen_index + 1);
4350 break;
4351 case COMP_DICTCOMP:
4352 /* With '{k: v}', k is evaluated before v, so we do
4353 the same. */
4354 VISIT(c, expr, elt);
4355 VISIT(c, expr, val);
4356 ADDOP_I(c, MAP_ADD, gen_index + 1);
4357 break;
4358 default:
4359 return 0;
4360 }
4361
4362 compiler_use_next_block(c, skip);
4363 }
4364 compiler_use_next_block(c, if_cleanup);
4365 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
4366 compiler_use_next_block(c, anchor);
4367
4368 return 1;
4369 }
4370
4371 static int
compiler_async_comprehension_generator(struct compiler * c,asdl_seq * generators,int gen_index,expr_ty elt,expr_ty val,int type)4372 compiler_async_comprehension_generator(struct compiler *c,
4373 asdl_seq *generators, int gen_index,
4374 expr_ty elt, expr_ty val, int type)
4375 {
4376 comprehension_ty gen;
4377 basicblock *start, *if_cleanup, *except;
4378 Py_ssize_t i, n;
4379 start = compiler_new_block(c);
4380 except = compiler_new_block(c);
4381 if_cleanup = compiler_new_block(c);
4382
4383 if (start == NULL || if_cleanup == NULL || except == NULL) {
4384 return 0;
4385 }
4386
4387 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4388
4389 if (gen_index == 0) {
4390 /* Receive outermost iter as an implicit argument */
4391 c->u->u_argcount = 1;
4392 ADDOP_I(c, LOAD_FAST, 0);
4393 }
4394 else {
4395 /* Sub-iter - calculate on the fly */
4396 VISIT(c, expr, gen->iter);
4397 ADDOP(c, GET_AITER);
4398 }
4399
4400 compiler_use_next_block(c, start);
4401
4402 ADDOP_JREL(c, SETUP_FINALLY, except);
4403 ADDOP(c, GET_ANEXT);
4404 ADDOP_LOAD_CONST(c, Py_None);
4405 ADDOP(c, YIELD_FROM);
4406 ADDOP(c, POP_BLOCK);
4407 VISIT(c, expr, gen->target);
4408
4409 n = asdl_seq_LEN(gen->ifs);
4410 for (i = 0; i < n; i++) {
4411 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
4412 if (!compiler_jump_if(c, e, if_cleanup, 0))
4413 return 0;
4414 NEXT_BLOCK(c);
4415 }
4416
4417 if (++gen_index < asdl_seq_LEN(generators))
4418 if (!compiler_comprehension_generator(c,
4419 generators, gen_index,
4420 elt, val, type))
4421 return 0;
4422
4423 /* only append after the last for generator */
4424 if (gen_index >= asdl_seq_LEN(generators)) {
4425 /* comprehension specific code */
4426 switch (type) {
4427 case COMP_GENEXP:
4428 VISIT(c, expr, elt);
4429 ADDOP(c, YIELD_VALUE);
4430 ADDOP(c, POP_TOP);
4431 break;
4432 case COMP_LISTCOMP:
4433 VISIT(c, expr, elt);
4434 ADDOP_I(c, LIST_APPEND, gen_index + 1);
4435 break;
4436 case COMP_SETCOMP:
4437 VISIT(c, expr, elt);
4438 ADDOP_I(c, SET_ADD, gen_index + 1);
4439 break;
4440 case COMP_DICTCOMP:
4441 /* With '{k: v}', k is evaluated before v, so we do
4442 the same. */
4443 VISIT(c, expr, elt);
4444 VISIT(c, expr, val);
4445 ADDOP_I(c, MAP_ADD, gen_index + 1);
4446 break;
4447 default:
4448 return 0;
4449 }
4450 }
4451 compiler_use_next_block(c, if_cleanup);
4452 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
4453
4454 compiler_use_next_block(c, except);
4455 ADDOP(c, END_ASYNC_FOR);
4456
4457 return 1;
4458 }
4459
4460 static int
compiler_comprehension(struct compiler * c,expr_ty e,int type,identifier name,asdl_seq * generators,expr_ty elt,expr_ty val)4461 compiler_comprehension(struct compiler *c, expr_ty e, int type,
4462 identifier name, asdl_seq *generators, expr_ty elt,
4463 expr_ty val)
4464 {
4465 PyCodeObject *co = NULL;
4466 comprehension_ty outermost;
4467 PyObject *qualname = NULL;
4468 int is_async_function = c->u->u_ste->ste_coroutine;
4469 int is_async_generator = 0;
4470
4471 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
4472
4473 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4474 (void *)e, e->lineno))
4475 {
4476 goto error;
4477 }
4478
4479 is_async_generator = c->u->u_ste->ste_coroutine;
4480
4481 if (is_async_generator && !is_async_function && type != COMP_GENEXP) {
4482 compiler_error(c, "asynchronous comprehension outside of "
4483 "an asynchronous function");
4484 goto error_in_scope;
4485 }
4486
4487 if (type != COMP_GENEXP) {
4488 int op;
4489 switch (type) {
4490 case COMP_LISTCOMP:
4491 op = BUILD_LIST;
4492 break;
4493 case COMP_SETCOMP:
4494 op = BUILD_SET;
4495 break;
4496 case COMP_DICTCOMP:
4497 op = BUILD_MAP;
4498 break;
4499 default:
4500 PyErr_Format(PyExc_SystemError,
4501 "unknown comprehension type %d", type);
4502 goto error_in_scope;
4503 }
4504
4505 ADDOP_I(c, op, 0);
4506 }
4507
4508 if (!compiler_comprehension_generator(c, generators, 0, elt,
4509 val, type))
4510 goto error_in_scope;
4511
4512 if (type != COMP_GENEXP) {
4513 ADDOP(c, RETURN_VALUE);
4514 }
4515
4516 co = assemble(c, 1);
4517 qualname = c->u->u_qualname;
4518 Py_INCREF(qualname);
4519 compiler_exit_scope(c);
4520 if (co == NULL)
4521 goto error;
4522
4523 if (!compiler_make_closure(c, co, 0, qualname))
4524 goto error;
4525 Py_DECREF(qualname);
4526 Py_DECREF(co);
4527
4528 VISIT(c, expr, outermost->iter);
4529
4530 if (outermost->is_async) {
4531 ADDOP(c, GET_AITER);
4532 } else {
4533 ADDOP(c, GET_ITER);
4534 }
4535
4536 ADDOP_I(c, CALL_FUNCTION, 1);
4537
4538 if (is_async_generator && type != COMP_GENEXP) {
4539 ADDOP(c, GET_AWAITABLE);
4540 ADDOP_LOAD_CONST(c, Py_None);
4541 ADDOP(c, YIELD_FROM);
4542 }
4543
4544 return 1;
4545 error_in_scope:
4546 compiler_exit_scope(c);
4547 error:
4548 Py_XDECREF(qualname);
4549 Py_XDECREF(co);
4550 return 0;
4551 }
4552
4553 static int
compiler_genexp(struct compiler * c,expr_ty e)4554 compiler_genexp(struct compiler *c, expr_ty e)
4555 {
4556 static identifier name;
4557 if (!name) {
4558 name = PyUnicode_InternFromString("<genexpr>");
4559 if (!name)
4560 return 0;
4561 }
4562 assert(e->kind == GeneratorExp_kind);
4563 return compiler_comprehension(c, e, COMP_GENEXP, name,
4564 e->v.GeneratorExp.generators,
4565 e->v.GeneratorExp.elt, NULL);
4566 }
4567
4568 static int
compiler_listcomp(struct compiler * c,expr_ty e)4569 compiler_listcomp(struct compiler *c, expr_ty e)
4570 {
4571 static identifier name;
4572 if (!name) {
4573 name = PyUnicode_InternFromString("<listcomp>");
4574 if (!name)
4575 return 0;
4576 }
4577 assert(e->kind == ListComp_kind);
4578 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4579 e->v.ListComp.generators,
4580 e->v.ListComp.elt, NULL);
4581 }
4582
4583 static int
compiler_setcomp(struct compiler * c,expr_ty e)4584 compiler_setcomp(struct compiler *c, expr_ty e)
4585 {
4586 static identifier name;
4587 if (!name) {
4588 name = PyUnicode_InternFromString("<setcomp>");
4589 if (!name)
4590 return 0;
4591 }
4592 assert(e->kind == SetComp_kind);
4593 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4594 e->v.SetComp.generators,
4595 e->v.SetComp.elt, NULL);
4596 }
4597
4598
4599 static int
compiler_dictcomp(struct compiler * c,expr_ty e)4600 compiler_dictcomp(struct compiler *c, expr_ty e)
4601 {
4602 static identifier name;
4603 if (!name) {
4604 name = PyUnicode_InternFromString("<dictcomp>");
4605 if (!name)
4606 return 0;
4607 }
4608 assert(e->kind == DictComp_kind);
4609 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4610 e->v.DictComp.generators,
4611 e->v.DictComp.key, e->v.DictComp.value);
4612 }
4613
4614
4615 static int
compiler_visit_keyword(struct compiler * c,keyword_ty k)4616 compiler_visit_keyword(struct compiler *c, keyword_ty k)
4617 {
4618 VISIT(c, expr, k->value);
4619 return 1;
4620 }
4621
4622 /* Test whether expression is constant. For constants, report
4623 whether they are true or false.
4624
4625 Return values: 1 for true, 0 for false, -1 for non-constant.
4626 */
4627
4628 static int
expr_constant(expr_ty e)4629 expr_constant(expr_ty e)
4630 {
4631 if (e->kind == Constant_kind) {
4632 return PyObject_IsTrue(e->v.Constant.value);
4633 }
4634 return -1;
4635 }
4636
4637
4638 /*
4639 Implements the async with statement.
4640
4641 The semantics outlined in that PEP are as follows:
4642
4643 async with EXPR as VAR:
4644 BLOCK
4645
4646 It is implemented roughly as:
4647
4648 context = EXPR
4649 exit = context.__aexit__ # not calling it
4650 value = await context.__aenter__()
4651 try:
4652 VAR = value # if VAR present in the syntax
4653 BLOCK
4654 finally:
4655 if an exception was raised:
4656 exc = copy of (exception, instance, traceback)
4657 else:
4658 exc = (None, None, None)
4659 if not (await exit(*exc)):
4660 raise
4661 */
4662 static int
compiler_async_with(struct compiler * c,stmt_ty s,int pos)4663 compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4664 {
4665 basicblock *block, *finally;
4666 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4667
4668 assert(s->kind == AsyncWith_kind);
4669 if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){
4670 c->u->u_ste->ste_coroutine = 1;
4671 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
4672 return compiler_error(c, "'async with' outside async function");
4673 }
4674
4675 block = compiler_new_block(c);
4676 finally = compiler_new_block(c);
4677 if (!block || !finally)
4678 return 0;
4679
4680 /* Evaluate EXPR */
4681 VISIT(c, expr, item->context_expr);
4682
4683 ADDOP(c, BEFORE_ASYNC_WITH);
4684 ADDOP(c, GET_AWAITABLE);
4685 ADDOP_LOAD_CONST(c, Py_None);
4686 ADDOP(c, YIELD_FROM);
4687
4688 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
4689
4690 /* SETUP_ASYNC_WITH pushes a finally block. */
4691 compiler_use_next_block(c, block);
4692 if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) {
4693 return 0;
4694 }
4695
4696 if (item->optional_vars) {
4697 VISIT(c, expr, item->optional_vars);
4698 }
4699 else {
4700 /* Discard result from context.__aenter__() */
4701 ADDOP(c, POP_TOP);
4702 }
4703
4704 pos++;
4705 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4706 /* BLOCK code */
4707 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4708 else if (!compiler_async_with(c, s, pos))
4709 return 0;
4710
4711 /* End of try block; start the finally block */
4712 ADDOP(c, POP_BLOCK);
4713 ADDOP(c, BEGIN_FINALLY);
4714 compiler_pop_fblock(c, ASYNC_WITH, block);
4715
4716 compiler_use_next_block(c, finally);
4717 if (!compiler_push_fblock(c, FINALLY_END, finally, NULL))
4718 return 0;
4719
4720 /* Finally block starts; context.__exit__ is on the stack under
4721 the exception or return information. Just issue our magic
4722 opcode. */
4723 ADDOP(c, WITH_CLEANUP_START);
4724
4725 ADDOP(c, GET_AWAITABLE);
4726 ADDOP_LOAD_CONST(c, Py_None);
4727 ADDOP(c, YIELD_FROM);
4728
4729 ADDOP(c, WITH_CLEANUP_FINISH);
4730
4731 /* Finally block ends. */
4732 ADDOP(c, END_FINALLY);
4733 compiler_pop_fblock(c, FINALLY_END, finally);
4734 return 1;
4735 }
4736
4737
4738 /*
4739 Implements the with statement from PEP 343.
4740
4741 The semantics outlined in that PEP are as follows:
4742
4743 with EXPR as VAR:
4744 BLOCK
4745
4746 It is implemented roughly as:
4747
4748 context = EXPR
4749 exit = context.__exit__ # not calling it
4750 value = context.__enter__()
4751 try:
4752 VAR = value # if VAR present in the syntax
4753 BLOCK
4754 finally:
4755 if an exception was raised:
4756 exc = copy of (exception, instance, traceback)
4757 else:
4758 exc = (None, None, None)
4759 exit(*exc)
4760 */
4761 static int
compiler_with(struct compiler * c,stmt_ty s,int pos)4762 compiler_with(struct compiler *c, stmt_ty s, int pos)
4763 {
4764 basicblock *block, *finally;
4765 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
4766
4767 assert(s->kind == With_kind);
4768
4769 block = compiler_new_block(c);
4770 finally = compiler_new_block(c);
4771 if (!block || !finally)
4772 return 0;
4773
4774 /* Evaluate EXPR */
4775 VISIT(c, expr, item->context_expr);
4776 ADDOP_JREL(c, SETUP_WITH, finally);
4777
4778 /* SETUP_WITH pushes a finally block. */
4779 compiler_use_next_block(c, block);
4780 if (!compiler_push_fblock(c, WITH, block, finally)) {
4781 return 0;
4782 }
4783
4784 if (item->optional_vars) {
4785 VISIT(c, expr, item->optional_vars);
4786 }
4787 else {
4788 /* Discard result from context.__enter__() */
4789 ADDOP(c, POP_TOP);
4790 }
4791
4792 pos++;
4793 if (pos == asdl_seq_LEN(s->v.With.items))
4794 /* BLOCK code */
4795 VISIT_SEQ(c, stmt, s->v.With.body)
4796 else if (!compiler_with(c, s, pos))
4797 return 0;
4798
4799 /* End of try block; start the finally block */
4800 ADDOP(c, POP_BLOCK);
4801 ADDOP(c, BEGIN_FINALLY);
4802 compiler_pop_fblock(c, WITH, block);
4803
4804 compiler_use_next_block(c, finally);
4805 if (!compiler_push_fblock(c, FINALLY_END, finally, NULL))
4806 return 0;
4807
4808 /* Finally block starts; context.__exit__ is on the stack under
4809 the exception or return information. Just issue our magic
4810 opcode. */
4811 ADDOP(c, WITH_CLEANUP_START);
4812 ADDOP(c, WITH_CLEANUP_FINISH);
4813
4814 /* Finally block ends. */
4815 ADDOP(c, END_FINALLY);
4816 compiler_pop_fblock(c, FINALLY_END, finally);
4817 return 1;
4818 }
4819
4820 static int
compiler_visit_expr1(struct compiler * c,expr_ty e)4821 compiler_visit_expr1(struct compiler *c, expr_ty e)
4822 {
4823 switch (e->kind) {
4824 case NamedExpr_kind:
4825 VISIT(c, expr, e->v.NamedExpr.value);
4826 ADDOP(c, DUP_TOP);
4827 VISIT(c, expr, e->v.NamedExpr.target);
4828 break;
4829 case BoolOp_kind:
4830 return compiler_boolop(c, e);
4831 case BinOp_kind:
4832 VISIT(c, expr, e->v.BinOp.left);
4833 VISIT(c, expr, e->v.BinOp.right);
4834 ADDOP(c, binop(c, e->v.BinOp.op));
4835 break;
4836 case UnaryOp_kind:
4837 VISIT(c, expr, e->v.UnaryOp.operand);
4838 ADDOP(c, unaryop(e->v.UnaryOp.op));
4839 break;
4840 case Lambda_kind:
4841 return compiler_lambda(c, e);
4842 case IfExp_kind:
4843 return compiler_ifexp(c, e);
4844 case Dict_kind:
4845 return compiler_dict(c, e);
4846 case Set_kind:
4847 return compiler_set(c, e);
4848 case GeneratorExp_kind:
4849 return compiler_genexp(c, e);
4850 case ListComp_kind:
4851 return compiler_listcomp(c, e);
4852 case SetComp_kind:
4853 return compiler_setcomp(c, e);
4854 case DictComp_kind:
4855 return compiler_dictcomp(c, e);
4856 case Yield_kind:
4857 if (c->u->u_ste->ste_type != FunctionBlock)
4858 return compiler_error(c, "'yield' outside function");
4859 if (e->v.Yield.value) {
4860 VISIT(c, expr, e->v.Yield.value);
4861 }
4862 else {
4863 ADDOP_LOAD_CONST(c, Py_None);
4864 }
4865 ADDOP(c, YIELD_VALUE);
4866 break;
4867 case YieldFrom_kind:
4868 if (c->u->u_ste->ste_type != FunctionBlock)
4869 return compiler_error(c, "'yield' outside function");
4870
4871 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4872 return compiler_error(c, "'yield from' inside async function");
4873
4874 VISIT(c, expr, e->v.YieldFrom.value);
4875 ADDOP(c, GET_YIELD_FROM_ITER);
4876 ADDOP_LOAD_CONST(c, Py_None);
4877 ADDOP(c, YIELD_FROM);
4878 break;
4879 case Await_kind:
4880 if (!(c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT)){
4881 if (c->u->u_ste->ste_type != FunctionBlock){
4882 return compiler_error(c, "'await' outside function");
4883 }
4884
4885 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
4886 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
4887 return compiler_error(c, "'await' outside async function");
4888 }
4889 }
4890
4891 VISIT(c, expr, e->v.Await.value);
4892 ADDOP(c, GET_AWAITABLE);
4893 ADDOP_LOAD_CONST(c, Py_None);
4894 ADDOP(c, YIELD_FROM);
4895 break;
4896 case Compare_kind:
4897 return compiler_compare(c, e);
4898 case Call_kind:
4899 return compiler_call(c, e);
4900 case Constant_kind:
4901 ADDOP_LOAD_CONST(c, e->v.Constant.value);
4902 break;
4903 case JoinedStr_kind:
4904 return compiler_joined_str(c, e);
4905 case FormattedValue_kind:
4906 return compiler_formatted_value(c, e);
4907 /* The following exprs can be assignment targets. */
4908 case Attribute_kind:
4909 if (e->v.Attribute.ctx != AugStore)
4910 VISIT(c, expr, e->v.Attribute.value);
4911 switch (e->v.Attribute.ctx) {
4912 case AugLoad:
4913 ADDOP(c, DUP_TOP);
4914 /* Fall through */
4915 case Load:
4916 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
4917 break;
4918 case AugStore:
4919 ADDOP(c, ROT_TWO);
4920 /* Fall through */
4921 case Store:
4922 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
4923 break;
4924 case Del:
4925 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
4926 break;
4927 case Param:
4928 default:
4929 PyErr_SetString(PyExc_SystemError,
4930 "param invalid in attribute expression");
4931 return 0;
4932 }
4933 break;
4934 case Subscript_kind:
4935 switch (e->v.Subscript.ctx) {
4936 case AugLoad:
4937 VISIT(c, expr, e->v.Subscript.value);
4938 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
4939 break;
4940 case Load:
4941 if (!check_subscripter(c, e->v.Subscript.value)) {
4942 return 0;
4943 }
4944 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
4945 return 0;
4946 }
4947 VISIT(c, expr, e->v.Subscript.value);
4948 VISIT_SLICE(c, e->v.Subscript.slice, Load);
4949 break;
4950 case AugStore:
4951 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
4952 break;
4953 case Store:
4954 VISIT(c, expr, e->v.Subscript.value);
4955 VISIT_SLICE(c, e->v.Subscript.slice, Store);
4956 break;
4957 case Del:
4958 VISIT(c, expr, e->v.Subscript.value);
4959 VISIT_SLICE(c, e->v.Subscript.slice, Del);
4960 break;
4961 case Param:
4962 default:
4963 PyErr_SetString(PyExc_SystemError,
4964 "param invalid in subscript expression");
4965 return 0;
4966 }
4967 break;
4968 case Starred_kind:
4969 switch (e->v.Starred.ctx) {
4970 case Store:
4971 /* In all legitimate cases, the Starred node was already replaced
4972 * by compiler_list/compiler_tuple. XXX: is that okay? */
4973 return compiler_error(c,
4974 "starred assignment target must be in a list or tuple");
4975 default:
4976 return compiler_error(c,
4977 "can't use starred expression here");
4978 }
4979 case Name_kind:
4980 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
4981 /* child nodes of List and Tuple will have expr_context set */
4982 case List_kind:
4983 return compiler_list(c, e);
4984 case Tuple_kind:
4985 return compiler_tuple(c, e);
4986 }
4987 return 1;
4988 }
4989
4990 static int
compiler_visit_expr(struct compiler * c,expr_ty e)4991 compiler_visit_expr(struct compiler *c, expr_ty e)
4992 {
4993 /* If expr e has a different line number than the last expr/stmt,
4994 set a new line number for the next instruction.
4995 */
4996 int old_lineno = c->u->u_lineno;
4997 int old_col_offset = c->u->u_col_offset;
4998 if (e->lineno != c->u->u_lineno) {
4999 c->u->u_lineno = e->lineno;
5000 c->u->u_lineno_set = 0;
5001 }
5002 /* Updating the column offset is always harmless. */
5003 c->u->u_col_offset = e->col_offset;
5004
5005 int res = compiler_visit_expr1(c, e);
5006
5007 if (old_lineno != c->u->u_lineno) {
5008 c->u->u_lineno = old_lineno;
5009 c->u->u_lineno_set = 0;
5010 }
5011 c->u->u_col_offset = old_col_offset;
5012 return res;
5013 }
5014
5015 static int
compiler_augassign(struct compiler * c,stmt_ty s)5016 compiler_augassign(struct compiler *c, stmt_ty s)
5017 {
5018 expr_ty e = s->v.AugAssign.target;
5019 expr_ty auge;
5020
5021 assert(s->kind == AugAssign_kind);
5022
5023 switch (e->kind) {
5024 case Attribute_kind:
5025 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
5026 AugLoad, e->lineno, e->col_offset,
5027 e->end_lineno, e->end_col_offset, c->c_arena);
5028 if (auge == NULL)
5029 return 0;
5030 VISIT(c, expr, auge);
5031 VISIT(c, expr, s->v.AugAssign.value);
5032 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
5033 auge->v.Attribute.ctx = AugStore;
5034 VISIT(c, expr, auge);
5035 break;
5036 case Subscript_kind:
5037 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
5038 AugLoad, e->lineno, e->col_offset,
5039 e->end_lineno, e->end_col_offset, c->c_arena);
5040 if (auge == NULL)
5041 return 0;
5042 VISIT(c, expr, auge);
5043 VISIT(c, expr, s->v.AugAssign.value);
5044 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
5045 auge->v.Subscript.ctx = AugStore;
5046 VISIT(c, expr, auge);
5047 break;
5048 case Name_kind:
5049 if (!compiler_nameop(c, e->v.Name.id, Load))
5050 return 0;
5051 VISIT(c, expr, s->v.AugAssign.value);
5052 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
5053 return compiler_nameop(c, e->v.Name.id, Store);
5054 default:
5055 PyErr_Format(PyExc_SystemError,
5056 "invalid node type (%d) for augmented assignment",
5057 e->kind);
5058 return 0;
5059 }
5060 return 1;
5061 }
5062
5063 static int
check_ann_expr(struct compiler * c,expr_ty e)5064 check_ann_expr(struct compiler *c, expr_ty e)
5065 {
5066 VISIT(c, expr, e);
5067 ADDOP(c, POP_TOP);
5068 return 1;
5069 }
5070
5071 static int
check_annotation(struct compiler * c,stmt_ty s)5072 check_annotation(struct compiler *c, stmt_ty s)
5073 {
5074 /* Annotations are only evaluated in a module or class. */
5075 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5076 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5077 return check_ann_expr(c, s->v.AnnAssign.annotation);
5078 }
5079 return 1;
5080 }
5081
5082 static int
check_ann_slice(struct compiler * c,slice_ty sl)5083 check_ann_slice(struct compiler *c, slice_ty sl)
5084 {
5085 switch(sl->kind) {
5086 case Index_kind:
5087 return check_ann_expr(c, sl->v.Index.value);
5088 case Slice_kind:
5089 if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) {
5090 return 0;
5091 }
5092 if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) {
5093 return 0;
5094 }
5095 if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) {
5096 return 0;
5097 }
5098 break;
5099 default:
5100 PyErr_SetString(PyExc_SystemError,
5101 "unexpected slice kind");
5102 return 0;
5103 }
5104 return 1;
5105 }
5106
5107 static int
check_ann_subscr(struct compiler * c,slice_ty sl)5108 check_ann_subscr(struct compiler *c, slice_ty sl)
5109 {
5110 /* We check that everything in a subscript is defined at runtime. */
5111 Py_ssize_t i, n;
5112
5113 switch (sl->kind) {
5114 case Index_kind:
5115 case Slice_kind:
5116 if (!check_ann_slice(c, sl)) {
5117 return 0;
5118 }
5119 break;
5120 case ExtSlice_kind:
5121 n = asdl_seq_LEN(sl->v.ExtSlice.dims);
5122 for (i = 0; i < n; i++) {
5123 slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i);
5124 switch (subsl->kind) {
5125 case Index_kind:
5126 case Slice_kind:
5127 if (!check_ann_slice(c, subsl)) {
5128 return 0;
5129 }
5130 break;
5131 case ExtSlice_kind:
5132 default:
5133 PyErr_SetString(PyExc_SystemError,
5134 "extended slice invalid in nested slice");
5135 return 0;
5136 }
5137 }
5138 break;
5139 default:
5140 PyErr_Format(PyExc_SystemError,
5141 "invalid subscript kind %d", sl->kind);
5142 return 0;
5143 }
5144 return 1;
5145 }
5146
5147 static int
compiler_annassign(struct compiler * c,stmt_ty s)5148 compiler_annassign(struct compiler *c, stmt_ty s)
5149 {
5150 expr_ty targ = s->v.AnnAssign.target;
5151 PyObject* mangled;
5152
5153 assert(s->kind == AnnAssign_kind);
5154
5155 /* We perform the actual assignment first. */
5156 if (s->v.AnnAssign.value) {
5157 VISIT(c, expr, s->v.AnnAssign.value);
5158 VISIT(c, expr, targ);
5159 }
5160 switch (targ->kind) {
5161 case Name_kind:
5162 /* If we have a simple name in a module or class, store annotation. */
5163 if (s->v.AnnAssign.simple &&
5164 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5165 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
5166 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5167 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5168 }
5169 else {
5170 VISIT(c, expr, s->v.AnnAssign.annotation);
5171 }
5172 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
5173 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
5174 ADDOP_LOAD_CONST_NEW(c, mangled);
5175 ADDOP(c, STORE_SUBSCR);
5176 }
5177 break;
5178 case Attribute_kind:
5179 if (!s->v.AnnAssign.value &&
5180 !check_ann_expr(c, targ->v.Attribute.value)) {
5181 return 0;
5182 }
5183 break;
5184 case Subscript_kind:
5185 if (!s->v.AnnAssign.value &&
5186 (!check_ann_expr(c, targ->v.Subscript.value) ||
5187 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5188 return 0;
5189 }
5190 break;
5191 default:
5192 PyErr_Format(PyExc_SystemError,
5193 "invalid node type (%d) for annotated assignment",
5194 targ->kind);
5195 return 0;
5196 }
5197 /* Annotation is evaluated last. */
5198 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5199 return 0;
5200 }
5201 return 1;
5202 }
5203
5204 /* Raises a SyntaxError and returns 0.
5205 If something goes wrong, a different exception may be raised.
5206 */
5207
5208 static int
compiler_error(struct compiler * c,const char * errstr)5209 compiler_error(struct compiler *c, const char *errstr)
5210 {
5211 PyObject *loc;
5212 PyObject *u = NULL, *v = NULL;
5213
5214 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5215 if (!loc) {
5216 Py_INCREF(Py_None);
5217 loc = Py_None;
5218 }
5219 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
5220 c->u->u_col_offset + 1, loc);
5221 if (!u)
5222 goto exit;
5223 v = Py_BuildValue("(zO)", errstr, u);
5224 if (!v)
5225 goto exit;
5226 PyErr_SetObject(PyExc_SyntaxError, v);
5227 exit:
5228 Py_DECREF(loc);
5229 Py_XDECREF(u);
5230 Py_XDECREF(v);
5231 return 0;
5232 }
5233
5234 /* Emits a SyntaxWarning and returns 1 on success.
5235 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5236 and returns 0.
5237 */
5238 static int
compiler_warn(struct compiler * c,const char * format,...)5239 compiler_warn(struct compiler *c, const char *format, ...)
5240 {
5241 va_list vargs;
5242 #ifdef HAVE_STDARG_PROTOTYPES
5243 va_start(vargs, format);
5244 #else
5245 va_start(vargs);
5246 #endif
5247 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5248 va_end(vargs);
5249 if (msg == NULL) {
5250 return 0;
5251 }
5252 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5253 c->u->u_lineno, NULL, NULL) < 0)
5254 {
5255 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
5256 /* Replace the SyntaxWarning exception with a SyntaxError
5257 to get a more accurate error report */
5258 PyErr_Clear();
5259 assert(PyUnicode_AsUTF8(msg) != NULL);
5260 compiler_error(c, PyUnicode_AsUTF8(msg));
5261 }
5262 Py_DECREF(msg);
5263 return 0;
5264 }
5265 Py_DECREF(msg);
5266 return 1;
5267 }
5268
5269 static int
compiler_handle_subscr(struct compiler * c,const char * kind,expr_context_ty ctx)5270 compiler_handle_subscr(struct compiler *c, const char *kind,
5271 expr_context_ty ctx)
5272 {
5273 int op = 0;
5274
5275 /* XXX this code is duplicated */
5276 switch (ctx) {
5277 case AugLoad: /* fall through to Load */
5278 case Load: op = BINARY_SUBSCR; break;
5279 case AugStore:/* fall through to Store */
5280 case Store: op = STORE_SUBSCR; break;
5281 case Del: op = DELETE_SUBSCR; break;
5282 case Param:
5283 PyErr_Format(PyExc_SystemError,
5284 "invalid %s kind %d in subscript\n",
5285 kind, ctx);
5286 return 0;
5287 }
5288 if (ctx == AugLoad) {
5289 ADDOP(c, DUP_TOP_TWO);
5290 }
5291 else if (ctx == AugStore) {
5292 ADDOP(c, ROT_THREE);
5293 }
5294 ADDOP(c, op);
5295 return 1;
5296 }
5297
5298 static int
compiler_slice(struct compiler * c,slice_ty s,expr_context_ty ctx)5299 compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
5300 {
5301 int n = 2;
5302 assert(s->kind == Slice_kind);
5303
5304 /* only handles the cases where BUILD_SLICE is emitted */
5305 if (s->v.Slice.lower) {
5306 VISIT(c, expr, s->v.Slice.lower);
5307 }
5308 else {
5309 ADDOP_LOAD_CONST(c, Py_None);
5310 }
5311
5312 if (s->v.Slice.upper) {
5313 VISIT(c, expr, s->v.Slice.upper);
5314 }
5315 else {
5316 ADDOP_LOAD_CONST(c, Py_None);
5317 }
5318
5319 if (s->v.Slice.step) {
5320 n++;
5321 VISIT(c, expr, s->v.Slice.step);
5322 }
5323 ADDOP_I(c, BUILD_SLICE, n);
5324 return 1;
5325 }
5326
5327 static int
compiler_visit_nested_slice(struct compiler * c,slice_ty s,expr_context_ty ctx)5328 compiler_visit_nested_slice(struct compiler *c, slice_ty s,
5329 expr_context_ty ctx)
5330 {
5331 switch (s->kind) {
5332 case Slice_kind:
5333 return compiler_slice(c, s, ctx);
5334 case Index_kind:
5335 VISIT(c, expr, s->v.Index.value);
5336 break;
5337 case ExtSlice_kind:
5338 default:
5339 PyErr_SetString(PyExc_SystemError,
5340 "extended slice invalid in nested slice");
5341 return 0;
5342 }
5343 return 1;
5344 }
5345
5346 static int
compiler_visit_slice(struct compiler * c,slice_ty s,expr_context_ty ctx)5347 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
5348 {
5349 const char * kindname = NULL;
5350 switch (s->kind) {
5351 case Index_kind:
5352 kindname = "index";
5353 if (ctx != AugStore) {
5354 VISIT(c, expr, s->v.Index.value);
5355 }
5356 break;
5357 case Slice_kind:
5358 kindname = "slice";
5359 if (ctx != AugStore) {
5360 if (!compiler_slice(c, s, ctx))
5361 return 0;
5362 }
5363 break;
5364 case ExtSlice_kind:
5365 kindname = "extended slice";
5366 if (ctx != AugStore) {
5367 Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
5368 for (i = 0; i < n; i++) {
5369 slice_ty sub = (slice_ty)asdl_seq_GET(
5370 s->v.ExtSlice.dims, i);
5371 if (!compiler_visit_nested_slice(c, sub, ctx))
5372 return 0;
5373 }
5374 ADDOP_I(c, BUILD_TUPLE, n);
5375 }
5376 break;
5377 default:
5378 PyErr_Format(PyExc_SystemError,
5379 "invalid subscript kind %d", s->kind);
5380 return 0;
5381 }
5382 return compiler_handle_subscr(c, kindname, ctx);
5383 }
5384
5385 /* End of the compiler section, beginning of the assembler section */
5386
5387 /* do depth-first search of basic block graph, starting with block.
5388 post records the block indices in post-order.
5389
5390 XXX must handle implicit jumps from one block to next
5391 */
5392
5393 struct assembler {
5394 PyObject *a_bytecode; /* string containing bytecode */
5395 int a_offset; /* offset into bytecode */
5396 int a_nblocks; /* number of reachable blocks */
5397 basicblock **a_postorder; /* list of blocks in dfs postorder */
5398 PyObject *a_lnotab; /* string containing lnotab */
5399 int a_lnotab_off; /* offset into lnotab */
5400 int a_lineno; /* last lineno of emitted instruction */
5401 int a_lineno_off; /* bytecode offset of last lineno */
5402 };
5403
5404 static void
dfs(struct compiler * c,basicblock * b,struct assembler * a,int end)5405 dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
5406 {
5407 int i, j;
5408
5409 /* Get rid of recursion for normal control flow.
5410 Since the number of blocks is limited, unused space in a_postorder
5411 (from a_nblocks to end) can be used as a stack for still not ordered
5412 blocks. */
5413 for (j = end; b && !b->b_seen; b = b->b_next) {
5414 b->b_seen = 1;
5415 assert(a->a_nblocks < j);
5416 a->a_postorder[--j] = b;
5417 }
5418 while (j < end) {
5419 b = a->a_postorder[j++];
5420 for (i = 0; i < b->b_iused; i++) {
5421 struct instr *instr = &b->b_instr[i];
5422 if (instr->i_jrel || instr->i_jabs)
5423 dfs(c, instr->i_target, a, j);
5424 }
5425 assert(a->a_nblocks < j);
5426 a->a_postorder[a->a_nblocks++] = b;
5427 }
5428 }
5429
5430 Py_LOCAL_INLINE(void)
stackdepth_push(basicblock *** sp,basicblock * b,int depth)5431 stackdepth_push(basicblock ***sp, basicblock *b, int depth)
5432 {
5433 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
5434 if (b->b_startdepth < depth) {
5435 assert(b->b_startdepth < 0);
5436 b->b_startdepth = depth;
5437 *(*sp)++ = b;
5438 }
5439 }
5440
5441 /* Find the flow path that needs the largest stack. We assume that
5442 * cycles in the flow graph have no net effect on the stack depth.
5443 */
5444 static int
stackdepth(struct compiler * c)5445 stackdepth(struct compiler *c)
5446 {
5447 basicblock *b, *entryblock = NULL;
5448 basicblock **stack, **sp;
5449 int nblocks = 0, maxdepth = 0;
5450 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5451 b->b_startdepth = INT_MIN;
5452 entryblock = b;
5453 nblocks++;
5454 }
5455 if (!entryblock)
5456 return 0;
5457 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5458 if (!stack) {
5459 PyErr_NoMemory();
5460 return -1;
5461 }
5462
5463 sp = stack;
5464 stackdepth_push(&sp, entryblock, 0);
5465 while (sp != stack) {
5466 b = *--sp;
5467 int depth = b->b_startdepth;
5468 assert(depth >= 0);
5469 basicblock *next = b->b_next;
5470 for (int i = 0; i < b->b_iused; i++) {
5471 struct instr *instr = &b->b_instr[i];
5472 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5473 if (effect == PY_INVALID_STACK_EFFECT) {
5474 fprintf(stderr, "opcode = %d\n", instr->i_opcode);
5475 Py_FatalError("PyCompile_OpcodeStackEffect()");
5476 }
5477 int new_depth = depth + effect;
5478 if (new_depth > maxdepth) {
5479 maxdepth = new_depth;
5480 }
5481 assert(depth >= 0); /* invalid code or bug in stackdepth() */
5482 if (instr->i_jrel || instr->i_jabs) {
5483 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5484 assert(effect != PY_INVALID_STACK_EFFECT);
5485 int target_depth = depth + effect;
5486 if (target_depth > maxdepth) {
5487 maxdepth = target_depth;
5488 }
5489 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
5490 if (instr->i_opcode == CALL_FINALLY) {
5491 assert(instr->i_target->b_startdepth >= 0);
5492 assert(instr->i_target->b_startdepth >= target_depth);
5493 depth = new_depth;
5494 continue;
5495 }
5496 stackdepth_push(&sp, instr->i_target, target_depth);
5497 }
5498 depth = new_depth;
5499 if (instr->i_opcode == JUMP_ABSOLUTE ||
5500 instr->i_opcode == JUMP_FORWARD ||
5501 instr->i_opcode == RETURN_VALUE ||
5502 instr->i_opcode == RAISE_VARARGS)
5503 {
5504 /* remaining code is dead */
5505 next = NULL;
5506 break;
5507 }
5508 }
5509 if (next != NULL) {
5510 stackdepth_push(&sp, next, depth);
5511 }
5512 }
5513 PyObject_Free(stack);
5514 return maxdepth;
5515 }
5516
5517 static int
assemble_init(struct assembler * a,int nblocks,int firstlineno)5518 assemble_init(struct assembler *a, int nblocks, int firstlineno)
5519 {
5520 memset(a, 0, sizeof(struct assembler));
5521 a->a_lineno = firstlineno;
5522 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5523 if (!a->a_bytecode)
5524 return 0;
5525 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5526 if (!a->a_lnotab)
5527 return 0;
5528 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
5529 PyErr_NoMemory();
5530 return 0;
5531 }
5532 a->a_postorder = (basicblock **)PyObject_Malloc(
5533 sizeof(basicblock *) * nblocks);
5534 if (!a->a_postorder) {
5535 PyErr_NoMemory();
5536 return 0;
5537 }
5538 return 1;
5539 }
5540
5541 static void
assemble_free(struct assembler * a)5542 assemble_free(struct assembler *a)
5543 {
5544 Py_XDECREF(a->a_bytecode);
5545 Py_XDECREF(a->a_lnotab);
5546 if (a->a_postorder)
5547 PyObject_Free(a->a_postorder);
5548 }
5549
5550 static int
blocksize(basicblock * b)5551 blocksize(basicblock *b)
5552 {
5553 int i;
5554 int size = 0;
5555
5556 for (i = 0; i < b->b_iused; i++)
5557 size += instrsize(b->b_instr[i].i_oparg);
5558 return size;
5559 }
5560
5561 /* Appends a pair to the end of the line number table, a_lnotab, representing
5562 the instruction's bytecode offset and line number. See
5563 Objects/lnotab_notes.txt for the description of the line number table. */
5564
5565 static int
assemble_lnotab(struct assembler * a,struct instr * i)5566 assemble_lnotab(struct assembler *a, struct instr *i)
5567 {
5568 int d_bytecode, d_lineno;
5569 Py_ssize_t len;
5570 unsigned char *lnotab;
5571
5572 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
5573 d_lineno = i->i_lineno - a->a_lineno;
5574
5575 assert(d_bytecode >= 0);
5576
5577 if(d_bytecode == 0 && d_lineno == 0)
5578 return 1;
5579
5580 if (d_bytecode > 255) {
5581 int j, nbytes, ncodes = d_bytecode / 255;
5582 nbytes = a->a_lnotab_off + 2 * ncodes;
5583 len = PyBytes_GET_SIZE(a->a_lnotab);
5584 if (nbytes >= len) {
5585 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5586 len = nbytes;
5587 else if (len <= INT_MAX / 2)
5588 len *= 2;
5589 else {
5590 PyErr_NoMemory();
5591 return 0;
5592 }
5593 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5594 return 0;
5595 }
5596 lnotab = (unsigned char *)
5597 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5598 for (j = 0; j < ncodes; j++) {
5599 *lnotab++ = 255;
5600 *lnotab++ = 0;
5601 }
5602 d_bytecode -= ncodes * 255;
5603 a->a_lnotab_off += ncodes * 2;
5604 }
5605 assert(0 <= d_bytecode && d_bytecode <= 255);
5606
5607 if (d_lineno < -128 || 127 < d_lineno) {
5608 int j, nbytes, ncodes, k;
5609 if (d_lineno < 0) {
5610 k = -128;
5611 /* use division on positive numbers */
5612 ncodes = (-d_lineno) / 128;
5613 }
5614 else {
5615 k = 127;
5616 ncodes = d_lineno / 127;
5617 }
5618 d_lineno -= ncodes * k;
5619 assert(ncodes >= 1);
5620 nbytes = a->a_lnotab_off + 2 * ncodes;
5621 len = PyBytes_GET_SIZE(a->a_lnotab);
5622 if (nbytes >= len) {
5623 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5624 len = nbytes;
5625 else if (len <= INT_MAX / 2)
5626 len *= 2;
5627 else {
5628 PyErr_NoMemory();
5629 return 0;
5630 }
5631 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5632 return 0;
5633 }
5634 lnotab = (unsigned char *)
5635 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5636 *lnotab++ = d_bytecode;
5637 *lnotab++ = k;
5638 d_bytecode = 0;
5639 for (j = 1; j < ncodes; j++) {
5640 *lnotab++ = 0;
5641 *lnotab++ = k;
5642 }
5643 a->a_lnotab_off += ncodes * 2;
5644 }
5645 assert(-128 <= d_lineno && d_lineno <= 127);
5646
5647 len = PyBytes_GET_SIZE(a->a_lnotab);
5648 if (a->a_lnotab_off + 2 >= len) {
5649 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5650 return 0;
5651 }
5652 lnotab = (unsigned char *)
5653 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5654
5655 a->a_lnotab_off += 2;
5656 if (d_bytecode) {
5657 *lnotab++ = d_bytecode;
5658 *lnotab++ = d_lineno;
5659 }
5660 else { /* First line of a block; def stmt, etc. */
5661 *lnotab++ = 0;
5662 *lnotab++ = d_lineno;
5663 }
5664 a->a_lineno = i->i_lineno;
5665 a->a_lineno_off = a->a_offset;
5666 return 1;
5667 }
5668
5669 /* assemble_emit()
5670 Extend the bytecode with a new instruction.
5671 Update lnotab if necessary.
5672 */
5673
5674 static int
assemble_emit(struct assembler * a,struct instr * i)5675 assemble_emit(struct assembler *a, struct instr *i)
5676 {
5677 int size, arg = 0;
5678 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
5679 _Py_CODEUNIT *code;
5680
5681 arg = i->i_oparg;
5682 size = instrsize(arg);
5683 if (i->i_lineno && !assemble_lnotab(a, i))
5684 return 0;
5685 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
5686 if (len > PY_SSIZE_T_MAX / 2)
5687 return 0;
5688 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5689 return 0;
5690 }
5691 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
5692 a->a_offset += size;
5693 write_op_arg(code, i->i_opcode, arg, size);
5694 return 1;
5695 }
5696
5697 static void
assemble_jump_offsets(struct assembler * a,struct compiler * c)5698 assemble_jump_offsets(struct assembler *a, struct compiler *c)
5699 {
5700 basicblock *b;
5701 int bsize, totsize, extended_arg_recompile;
5702 int i;
5703
5704 /* Compute the size of each block and fixup jump args.
5705 Replace block pointer with position in bytecode. */
5706 do {
5707 totsize = 0;
5708 for (i = a->a_nblocks - 1; i >= 0; i--) {
5709 b = a->a_postorder[i];
5710 bsize = blocksize(b);
5711 b->b_offset = totsize;
5712 totsize += bsize;
5713 }
5714 extended_arg_recompile = 0;
5715 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5716 bsize = b->b_offset;
5717 for (i = 0; i < b->b_iused; i++) {
5718 struct instr *instr = &b->b_instr[i];
5719 int isize = instrsize(instr->i_oparg);
5720 /* Relative jumps are computed relative to
5721 the instruction pointer after fetching
5722 the jump instruction.
5723 */
5724 bsize += isize;
5725 if (instr->i_jabs || instr->i_jrel) {
5726 instr->i_oparg = instr->i_target->b_offset;
5727 if (instr->i_jrel) {
5728 instr->i_oparg -= bsize;
5729 }
5730 instr->i_oparg *= sizeof(_Py_CODEUNIT);
5731 if (instrsize(instr->i_oparg) != isize) {
5732 extended_arg_recompile = 1;
5733 }
5734 }
5735 }
5736 }
5737
5738 /* XXX: This is an awful hack that could hurt performance, but
5739 on the bright side it should work until we come up
5740 with a better solution.
5741
5742 The issue is that in the first loop blocksize() is called
5743 which calls instrsize() which requires i_oparg be set
5744 appropriately. There is a bootstrap problem because
5745 i_oparg is calculated in the second loop above.
5746
5747 So we loop until we stop seeing new EXTENDED_ARGs.
5748 The only EXTENDED_ARGs that could be popping up are
5749 ones in jump instructions. So this should converge
5750 fairly quickly.
5751 */
5752 } while (extended_arg_recompile);
5753 }
5754
5755 static PyObject *
dict_keys_inorder(PyObject * dict,Py_ssize_t offset)5756 dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
5757 {
5758 PyObject *tuple, *k, *v;
5759 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5760
5761 tuple = PyTuple_New(size);
5762 if (tuple == NULL)
5763 return NULL;
5764 while (PyDict_Next(dict, &pos, &k, &v)) {
5765 i = PyLong_AS_LONG(v);
5766 Py_INCREF(k);
5767 assert((i - offset) < size);
5768 assert((i - offset) >= 0);
5769 PyTuple_SET_ITEM(tuple, i - offset, k);
5770 }
5771 return tuple;
5772 }
5773
5774 static PyObject *
consts_dict_keys_inorder(PyObject * dict)5775 consts_dict_keys_inorder(PyObject *dict)
5776 {
5777 PyObject *consts, *k, *v;
5778 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5779
5780 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
5781 if (consts == NULL)
5782 return NULL;
5783 while (PyDict_Next(dict, &pos, &k, &v)) {
5784 i = PyLong_AS_LONG(v);
5785 /* The keys of the dictionary can be tuples wrapping a contant.
5786 * (see compiler_add_o and _PyCode_ConstantKey). In that case
5787 * the object we want is always second. */
5788 if (PyTuple_CheckExact(k)) {
5789 k = PyTuple_GET_ITEM(k, 1);
5790 }
5791 Py_INCREF(k);
5792 assert(i < size);
5793 assert(i >= 0);
5794 PyList_SET_ITEM(consts, i, k);
5795 }
5796 return consts;
5797 }
5798
5799 static int
compute_code_flags(struct compiler * c)5800 compute_code_flags(struct compiler *c)
5801 {
5802 PySTEntryObject *ste = c->u->u_ste;
5803 int flags = 0;
5804 if (ste->ste_type == FunctionBlock) {
5805 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
5806 if (ste->ste_nested)
5807 flags |= CO_NESTED;
5808 if (ste->ste_generator && !ste->ste_coroutine)
5809 flags |= CO_GENERATOR;
5810 if (!ste->ste_generator && ste->ste_coroutine)
5811 flags |= CO_COROUTINE;
5812 if (ste->ste_generator && ste->ste_coroutine)
5813 flags |= CO_ASYNC_GENERATOR;
5814 if (ste->ste_varargs)
5815 flags |= CO_VARARGS;
5816 if (ste->ste_varkeywords)
5817 flags |= CO_VARKEYWORDS;
5818 }
5819
5820 /* (Only) inherit compilerflags in PyCF_MASK */
5821 flags |= (c->c_flags->cf_flags & PyCF_MASK);
5822
5823 if ((c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) &&
5824 ste->ste_coroutine &&
5825 !ste->ste_generator) {
5826 flags |= CO_COROUTINE;
5827 }
5828
5829 return flags;
5830 }
5831
5832 // Merge *tuple* with constant cache.
5833 // Unlike merge_consts_recursive(), this function doesn't work recursively.
5834 static int
merge_const_tuple(struct compiler * c,PyObject ** tuple)5835 merge_const_tuple(struct compiler *c, PyObject **tuple)
5836 {
5837 assert(PyTuple_CheckExact(*tuple));
5838
5839 PyObject *key = _PyCode_ConstantKey(*tuple);
5840 if (key == NULL) {
5841 return 0;
5842 }
5843
5844 // t is borrowed reference
5845 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
5846 Py_DECREF(key);
5847 if (t == NULL) {
5848 return 0;
5849 }
5850 if (t == key) { // tuple is new constant.
5851 return 1;
5852 }
5853
5854 PyObject *u = PyTuple_GET_ITEM(t, 1);
5855 Py_INCREF(u);
5856 Py_DECREF(*tuple);
5857 *tuple = u;
5858 return 1;
5859 }
5860
5861 static PyCodeObject *
makecode(struct compiler * c,struct assembler * a)5862 makecode(struct compiler *c, struct assembler *a)
5863 {
5864 PyObject *tmp;
5865 PyCodeObject *co = NULL;
5866 PyObject *consts = NULL;
5867 PyObject *names = NULL;
5868 PyObject *varnames = NULL;
5869 PyObject *name = NULL;
5870 PyObject *freevars = NULL;
5871 PyObject *cellvars = NULL;
5872 PyObject *bytecode = NULL;
5873 Py_ssize_t nlocals;
5874 int nlocals_int;
5875 int flags;
5876 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
5877
5878 consts = consts_dict_keys_inorder(c->u->u_consts);
5879 names = dict_keys_inorder(c->u->u_names, 0);
5880 varnames = dict_keys_inorder(c->u->u_varnames, 0);
5881 if (!consts || !names || !varnames)
5882 goto error;
5883
5884 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5885 if (!cellvars)
5886 goto error;
5887 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
5888 if (!freevars)
5889 goto error;
5890
5891 if (!merge_const_tuple(c, &names) ||
5892 !merge_const_tuple(c, &varnames) ||
5893 !merge_const_tuple(c, &cellvars) ||
5894 !merge_const_tuple(c, &freevars))
5895 {
5896 goto error;
5897 }
5898
5899 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
5900 assert(nlocals < INT_MAX);
5901 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5902
5903 flags = compute_code_flags(c);
5904 if (flags < 0)
5905 goto error;
5906
5907 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
5908 if (!bytecode)
5909 goto error;
5910
5911 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5912 if (!tmp)
5913 goto error;
5914 Py_DECREF(consts);
5915 consts = tmp;
5916 if (!merge_const_tuple(c, &consts)) {
5917 goto error;
5918 }
5919
5920 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
5921 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
5922 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
5923 maxdepth = stackdepth(c);
5924 if (maxdepth < 0) {
5925 goto error;
5926 }
5927 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
5928 posonlyargcount, kwonlyargcount, nlocals_int,
5929 maxdepth, flags, bytecode, consts, names,
5930 varnames, freevars, cellvars, c->c_filename,
5931 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
5932 error:
5933 Py_XDECREF(consts);
5934 Py_XDECREF(names);
5935 Py_XDECREF(varnames);
5936 Py_XDECREF(name);
5937 Py_XDECREF(freevars);
5938 Py_XDECREF(cellvars);
5939 Py_XDECREF(bytecode);
5940 return co;
5941 }
5942
5943
5944 /* For debugging purposes only */
5945 #if 0
5946 static void
5947 dump_instr(const struct instr *i)
5948 {
5949 const char *jrel = i->i_jrel ? "jrel " : "";
5950 const char *jabs = i->i_jabs ? "jabs " : "";
5951 char arg[128];
5952
5953 *arg = '\0';
5954 if (HAS_ARG(i->i_opcode)) {
5955 sprintf(arg, "arg: %d ", i->i_oparg);
5956 }
5957 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5958 i->i_lineno, i->i_opcode, arg, jabs, jrel);
5959 }
5960
5961 static void
5962 dump_basicblock(const basicblock *b)
5963 {
5964 const char *seen = b->b_seen ? "seen " : "";
5965 const char *b_return = b->b_return ? "return " : "";
5966 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
5967 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
5968 if (b->b_instr) {
5969 int i;
5970 for (i = 0; i < b->b_iused; i++) {
5971 fprintf(stderr, " [%02d] ", i);
5972 dump_instr(b->b_instr + i);
5973 }
5974 }
5975 }
5976 #endif
5977
5978 static PyCodeObject *
assemble(struct compiler * c,int addNone)5979 assemble(struct compiler *c, int addNone)
5980 {
5981 basicblock *b, *entryblock;
5982 struct assembler a;
5983 int i, j, nblocks;
5984 PyCodeObject *co = NULL;
5985
5986 /* Make sure every block that falls off the end returns None.
5987 XXX NEXT_BLOCK() isn't quite right, because if the last
5988 block ends with a jump or return b_next shouldn't set.
5989 */
5990 if (!c->u->u_curblock->b_return) {
5991 NEXT_BLOCK(c);
5992 if (addNone)
5993 ADDOP_LOAD_CONST(c, Py_None);
5994 ADDOP(c, RETURN_VALUE);
5995 }
5996
5997 nblocks = 0;
5998 entryblock = NULL;
5999 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6000 nblocks++;
6001 entryblock = b;
6002 }
6003
6004 /* Set firstlineno if it wasn't explicitly set. */
6005 if (!c->u->u_firstlineno) {
6006 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
6007 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
6008 else
6009 c->u->u_firstlineno = 1;
6010 }
6011 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6012 goto error;
6013 dfs(c, entryblock, &a, nblocks);
6014
6015 /* Can't modify the bytecode after computing jump offsets. */
6016 assemble_jump_offsets(&a, c);
6017
6018 /* Emit code in reverse postorder from dfs. */
6019 for (i = a.a_nblocks - 1; i >= 0; i--) {
6020 b = a.a_postorder[i];
6021 for (j = 0; j < b->b_iused; j++)
6022 if (!assemble_emit(&a, &b->b_instr[j]))
6023 goto error;
6024 }
6025
6026 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
6027 goto error;
6028 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
6029 goto error;
6030
6031 co = makecode(c, &a);
6032 error:
6033 assemble_free(&a);
6034 return co;
6035 }
6036
6037 #undef PyAST_Compile
6038 PyCodeObject *
PyAST_Compile(mod_ty mod,const char * filename,PyCompilerFlags * flags,PyArena * arena)6039 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6040 PyArena *arena)
6041 {
6042 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6043 }
6044