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