/Python/compile.c
C | 4071 lines | 3303 code | 365 blank | 403 comment | 693 complexity | 90a995028856bda463af1ecde267e58a MD5 | raw file
Large files files are truncated, but you can click here to view the full file
1/* 2 * This file compiles an abstract syntax tree (AST) into Python bytecode. 3 * 4 * The primary entry point is PyAST_Compile(), which returns a 5 * PyCodeObject. The compiler makes several passes to build the code 6 * object: 7 * 1. Checks for future statements. See future.c 8 * 2. Builds a symbol table. See symtable.c. 9 * 3. Generate code for basic blocks. See compiler_mod() in this file. 10 * 4. Assemble the basic blocks into final code. See assemble() in 11 * this file. 12 * 5. Optimize the byte code (peephole optimizations). See peephole.c 13 * 14 * Note that compiler_mod() suggests module, but the module ast type 15 * (mod_ty) has cases for expressions and interactive statements. 16 * 17 * CAUTION: The VISIT_* macros abort the current function when they 18 * encounter a problem. So don't invoke them when there is memory 19 * which needs to be released. Code blocks are OK, as the compiler 20 * structure takes care of releasing those. Use the arena to manage 21 * objects. 22 */ 23 24#include "Python.h" 25 26#include "Python-ast.h" 27#include "node.h" 28#include "pyarena.h" 29#include "ast.h" 30#include "code.h" 31#include "compile.h" 32#include "symtable.h" 33#include "opcode.h" 34 35#define DEFAULT_BLOCK_SIZE 16 36#define DEFAULT_BLOCKS 8 37#define DEFAULT_CODE_SIZE 128 38#define DEFAULT_LNOTAB_SIZE 16 39 40struct instr { 41 unsigned i_jabs : 1; 42 unsigned i_jrel : 1; 43 unsigned i_hasarg : 1; 44 unsigned char i_opcode; 45 int i_oparg; 46 struct basicblock_ *i_target; /* target block (if jump instruction) */ 47 int i_lineno; 48}; 49 50typedef struct basicblock_ { 51 /* Each basicblock in a compilation unit is linked via b_list in the 52 reverse order that the block are allocated. b_list points to the next 53 block, not to be confused with b_next, which is next by control flow. */ 54 struct basicblock_ *b_list; 55 /* number of instructions used */ 56 int b_iused; 57 /* length of instruction array (b_instr) */ 58 int b_ialloc; 59 /* pointer to an array of instructions, initially NULL */ 60 struct instr *b_instr; 61 /* If b_next is non-NULL, it is a pointer to the next 62 block reached by normal control flow. */ 63 struct basicblock_ *b_next; 64 /* b_seen is used to perform a DFS of basicblocks. */ 65 unsigned b_seen : 1; 66 /* b_return is true if a RETURN_VALUE opcode is inserted. */ 67 unsigned b_return : 1; 68 /* depth of stack upon entry of block, computed by stackdepth() */ 69 int b_startdepth; 70 /* instruction offset for block, computed by assemble_jump_offsets() */ 71 int b_offset; 72} basicblock; 73 74/* fblockinfo tracks the current frame block. 75 76A frame block is used to handle loops, try/except, and try/finally. 77It's called a frame block to distinguish it from a basic block in the 78compiler IR. 79*/ 80 81enum fblocktype { FOR_LOOP, WHILE_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END }; 82 83struct fblockinfo { 84 enum fblocktype fb_type; 85 basicblock *fb_block; 86 basicblock *fb_target; 87}; 88 89/* The following items change on entry and exit of code blocks. 90 They must be saved and restored when returning to a block. 91*/ 92struct compiler_unit { 93 PySTEntryObject *u_ste; 94 95 PyObject *u_name; 96 /* The following fields are dicts that map objects to 97 the index of them in co_XXX. The index is used as 98 the argument for opcodes that refer to those collections. 99 */ 100 PyObject *u_consts; /* all constants */ 101 PyObject *u_names; /* all names */ 102 PyObject *u_varnames; /* local variables */ 103 PyObject *u_cellvars; /* cell variables */ 104 PyObject *u_freevars; /* free variables */ 105 106 PyObject *u_private; /* for private name mangling */ 107 108 int u_argcount; /* number of arguments for block */ 109 /* Pointer to the most recently allocated block. By following b_list 110 members, you can reach all early allocated blocks. */ 111 basicblock *u_blocks; 112 basicblock *u_curblock; /* pointer to current block */ 113 int u_tmpname; /* temporary variables for list comps */ 114 115 int u_nfblocks; 116 struct fblockinfo u_fblock[CO_MAXBLOCKS]; 117 118 int u_firstlineno; /* the first lineno of the block */ 119 int u_lineno; /* the lineno for the current stmt */ 120 bool u_lineno_set; /* boolean to indicate whether instr 121 has been generated with current lineno */ 122 bool u_uses_exec; /* Whether this code object uses exec */ 123}; 124 125/* This struct captures the global state of a compilation. 126 127The u pointer points to the current compilation unit, while units 128for enclosing blocks are stored in c_stack. The u and c_stack are 129managed by compiler_enter_scope() and compiler_exit_scope(). 130*/ 131 132struct compiler { 133 const char *c_filename; 134 struct symtable *c_st; 135 PyFutureFeatures *c_future; /* pointer to module's __future__ */ 136 PyCompilerFlags *c_flags; 137 138 int c_interactive; /* true if in interactive mode */ 139 int c_nestlevel; 140 141 struct compiler_unit *u; /* compiler state for current block */ 142 PyObject *c_stack; /* Python list holding compiler_unit ptrs */ 143 char *c_encoding; /* source encoding (a borrowed reference) */ 144 PyArena *c_arena; /* pointer to memory allocation arena */ 145}; 146 147static int compiler_enter_scope(struct compiler *, identifier, void *, int); 148static void compiler_free(struct compiler *); 149static basicblock *compiler_new_block(struct compiler *); 150static int compiler_next_instr(struct compiler *, basicblock *); 151static int compiler_addop(struct compiler *, int); 152static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *); 153static int compiler_addop_i(struct compiler *, int, int); 154static int compiler_addop_j(struct compiler *, int, basicblock *, int); 155static basicblock *compiler_use_new_block(struct compiler *); 156static int compiler_error(struct compiler *, const char *); 157static int compiler_nameop(struct compiler *, identifier, expr_context_ty); 158static int compiler_load_global(struct compiler *, const char *); 159 160static PyCodeObject *compiler_mod(struct compiler *, mod_ty); 161static int compiler_visit_stmt(struct compiler *, stmt_ty); 162static int compiler_visit_keyword(struct compiler *, keyword_ty); 163static int compiler_visit_expr(struct compiler *, expr_ty); 164static int compiler_augassign(struct compiler *, stmt_ty); 165static int compiler_visit_slice(struct compiler *, slice_ty, 166 expr_context_ty); 167 168/* top is the basic block representing the top of the loop or try. target 169 is really only relevant for loops, and indicates where a break instruction 170 should jump to. */ 171static int compiler_push_fblock(struct compiler *, enum fblocktype, 172 basicblock *top, basicblock *target); 173static void compiler_pop_fblock(struct compiler *, enum fblocktype, 174 basicblock *top, basicblock *target); 175/* Returns true if there is a loop on the fblock stack. */ 176static int compiler_in_loop(struct compiler *); 177 178static int inplace_binop(struct compiler *, operator_ty); 179static int expr_constant(expr_ty e); 180 181static int compiler_with(struct compiler *, stmt_ty); 182 183static PyCodeObject *assemble(struct compiler *, int addNone); 184static PyObject *__doc__; 185 186PyObject * 187_Py_Mangle(PyObject *privateobj, PyObject *ident) 188{ 189 /* Name mangling: __private becomes _classname__private. 190 This is independent from how the name is used. */ 191 const char *p, *name = PyString_AsString(ident); 192 char *buffer; 193 size_t nlen, plen; 194 if (privateobj == NULL || !PyString_Check(privateobj) || 195 name == NULL || name[0] != '_' || name[1] != '_') { 196 Py_INCREF(ident); 197 return ident; 198 } 199 p = PyString_AsString(privateobj); 200 nlen = strlen(name); 201 /* Don't mangle __id__ or names with dots. 202 203 The only time a name with a dot can occur is when 204 we are compiling an import statement that has a 205 package name. 206 207 TODO(jhylton): Decide whether we want to support 208 mangling of the module name, e.g. __M.X. 209 */ 210 if ((name[nlen-1] == '_' && name[nlen-2] == '_') 211 || strchr(name, '.')) { 212 Py_INCREF(ident); 213 return ident; /* Don't mangle __whatever__ */ 214 } 215 /* Strip leading underscores from class name */ 216 while (*p == '_') 217 p++; 218 if (*p == '\0') { 219 Py_INCREF(ident); 220 return ident; /* Don't mangle if class is just underscores */ 221 } 222 plen = strlen(p); 223 224 assert(1 <= PY_SSIZE_T_MAX - nlen); 225 assert(1 + nlen <= PY_SSIZE_T_MAX - plen); 226 227 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen); 228 if (!ident) 229 return 0; 230 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ 231 buffer = PyString_AS_STRING(ident); 232 buffer[0] = '_'; 233 strncpy(buffer+1, p, plen); 234 strcpy(buffer+1+plen, name); 235 return ident; 236} 237 238static int 239compiler_init(struct compiler *c) 240{ 241 memset(c, 0, sizeof(struct compiler)); 242 243 c->c_stack = PyList_New(0); 244 if (!c->c_stack) 245 return 0; 246 247 return 1; 248} 249 250PyCodeObject * 251PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 252 PyArena *arena) 253{ 254 struct compiler c; 255 PyCodeObject *co = NULL; 256 PyCompilerFlags local_flags; 257 int merged; 258 259 if (!__doc__) { 260 __doc__ = PyString_InternFromString("__doc__"); 261 if (!__doc__) 262 return NULL; 263 } 264 265 if (!compiler_init(&c)) 266 return NULL; 267 c.c_filename = filename; 268 c.c_arena = arena; 269 c.c_future = PyFuture_FromAST(mod, filename); 270 if (c.c_future == NULL) 271 goto finally; 272 if (!flags) { 273 local_flags.cf_flags = 0; 274 flags = &local_flags; 275 } 276 merged = c.c_future->ff_features | flags->cf_flags; 277 c.c_future->ff_features = merged; 278 flags->cf_flags = merged; 279 c.c_flags = flags; 280 c.c_nestlevel = 0; 281 282 c.c_st = PySymtable_Build(mod, filename, c.c_future); 283 if (c.c_st == NULL) { 284 if (!PyErr_Occurred()) 285 PyErr_SetString(PyExc_SystemError, "no symtable"); 286 goto finally; 287 } 288 289 /* XXX initialize to NULL for now, need to handle */ 290 c.c_encoding = NULL; 291 292 co = compiler_mod(&c, mod); 293 294 finally: 295 compiler_free(&c); 296 assert(co || PyErr_Occurred()); 297 return co; 298} 299 300PyCodeObject * 301PyNode_Compile(struct _node *n, const char *filename) 302{ 303 PyCodeObject *co = NULL; 304 mod_ty mod; 305 PyArena *arena = PyArena_New(); 306 if (!arena) 307 return NULL; 308 mod = PyAST_FromNode(n, NULL, filename, arena); 309 if (mod) 310 co = PyAST_Compile(mod, filename, NULL, arena); 311 PyArena_Free(arena); 312 return co; 313} 314 315static void 316compiler_free(struct compiler *c) 317{ 318 if (c->c_st) 319 PySymtable_Free(c->c_st); 320 if (c->c_future) 321 PyObject_Free(c->c_future); 322 Py_DECREF(c->c_stack); 323} 324 325static PyObject * 326list2dict(PyObject *list) 327{ 328 Py_ssize_t i, n; 329 PyObject *v, *k; 330 PyObject *dict = PyDict_New(); 331 if (!dict) return NULL; 332 333 n = PyList_Size(list); 334 for (i = 0; i < n; i++) { 335 v = PyInt_FromLong(i); 336 if (!v) { 337 Py_DECREF(dict); 338 return NULL; 339 } 340 k = PyList_GET_ITEM(list, i); 341 k = PyTuple_Pack(2, k, k->ob_type); 342 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) { 343 Py_XDECREF(k); 344 Py_DECREF(v); 345 Py_DECREF(dict); 346 return NULL; 347 } 348 Py_DECREF(k); 349 Py_DECREF(v); 350 } 351 return dict; 352} 353 354/* Return new dict containing names from src that match scope(s). 355 356src is a symbol table dictionary. If the scope of a name matches 357either scope_type or flag is set, insert it into the new dict. The 358values are integers, starting at offset and increasing by one for 359each key. 360*/ 361 362static PyObject * 363dictbytype(PyObject *src, int scope_type, int flag, int offset) 364{ 365 Py_ssize_t pos = 0, i = offset, scope; 366 PyObject *k, *v, *dest = PyDict_New(); 367 368 assert(offset >= 0); 369 if (dest == NULL) 370 return NULL; 371 372 while (PyDict_Next(src, &pos, &k, &v)) { 373 /* XXX this should probably be a macro in symtable.h */ 374 assert(PyInt_Check(v)); 375 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK; 376 377 if (scope == scope_type || PyInt_AS_LONG(v) & flag) { 378 PyObject *tuple, *item = PyInt_FromLong(i); 379 if (item == NULL) { 380 Py_DECREF(dest); 381 return NULL; 382 } 383 i++; 384 tuple = PyTuple_Pack(2, k, k->ob_type); 385 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) { 386 Py_DECREF(item); 387 Py_DECREF(dest); 388 Py_XDECREF(tuple); 389 return NULL; 390 } 391 Py_DECREF(item); 392 Py_DECREF(tuple); 393 } 394 } 395 return dest; 396} 397 398static void 399compiler_unit_check(struct compiler_unit *u) 400{ 401 basicblock *block; 402 for (block = u->u_blocks; block != NULL; block = block->b_list) { 403 assert((void *)block != (void *)0xcbcbcbcb); 404 assert((void *)block != (void *)0xfbfbfbfb); 405 assert((void *)block != (void *)0xdbdbdbdb); 406 if (block->b_instr != NULL) { 407 assert(block->b_ialloc > 0); 408 assert(block->b_iused > 0); 409 assert(block->b_ialloc >= block->b_iused); 410 } 411 else { 412 assert (block->b_iused == 0); 413 assert (block->b_ialloc == 0); 414 } 415 } 416} 417 418static void 419compiler_unit_free(struct compiler_unit *u) 420{ 421 basicblock *b, *next; 422 423 compiler_unit_check(u); 424 b = u->u_blocks; 425 while (b != NULL) { 426 if (b->b_instr) 427 PyObject_Free((void *)b->b_instr); 428 next = b->b_list; 429 PyObject_Free((void *)b); 430 b = next; 431 } 432 Py_CLEAR(u->u_ste); 433 Py_CLEAR(u->u_name); 434 Py_CLEAR(u->u_consts); 435 Py_CLEAR(u->u_names); 436 Py_CLEAR(u->u_varnames); 437 Py_CLEAR(u->u_freevars); 438 Py_CLEAR(u->u_cellvars); 439 Py_CLEAR(u->u_private); 440 PyObject_Free(u); 441} 442 443static int 444compiler_enter_scope(struct compiler *c, identifier name, void *key, 445 int lineno) 446{ 447 struct compiler_unit *u; 448 449 u = (struct compiler_unit *)PyObject_Malloc(sizeof( 450 struct compiler_unit)); 451 if (!u) { 452 PyErr_NoMemory(); 453 return 0; 454 } 455 memset(u, 0, sizeof(struct compiler_unit)); 456 u->u_argcount = 0; 457 u->u_ste = PySymtable_Lookup(c->c_st, key); 458 if (!u->u_ste) { 459 compiler_unit_free(u); 460 return 0; 461 } 462 Py_INCREF(name); 463 u->u_name = name; 464 u->u_varnames = list2dict(u->u_ste->ste_varnames); 465 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); 466 if (!u->u_varnames || !u->u_cellvars) { 467 compiler_unit_free(u); 468 return 0; 469 } 470 471 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, 472 PyDict_Size(u->u_cellvars)); 473 if (!u->u_freevars) { 474 compiler_unit_free(u); 475 return 0; 476 } 477 478 u->u_blocks = NULL; 479 u->u_tmpname = 0; 480 u->u_nfblocks = 0; 481 u->u_firstlineno = lineno; 482 u->u_lineno = 0; 483 u->u_lineno_set = false; 484 u->u_uses_exec = false; 485 u->u_consts = PyDict_New(); 486 if (!u->u_consts) { 487 compiler_unit_free(u); 488 return 0; 489 } 490 u->u_names = PyDict_New(); 491 if (!u->u_names) { 492 compiler_unit_free(u); 493 return 0; 494 } 495 496 u->u_private = NULL; 497 498 /* Push the old compiler_unit on the stack. */ 499 if (c->u) { 500 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL); 501 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) { 502 Py_XDECREF(wrapper); 503 compiler_unit_free(u); 504 return 0; 505 } 506 Py_DECREF(wrapper); 507 u->u_private = c->u->u_private; 508 Py_XINCREF(u->u_private); 509 } 510 c->u = u; 511 512 c->c_nestlevel++; 513 if (compiler_use_new_block(c) == NULL) 514 return 0; 515 516 return 1; 517} 518 519static void 520compiler_exit_scope(struct compiler *c) 521{ 522 int n; 523 PyObject *wrapper; 524 525 c->c_nestlevel--; 526 compiler_unit_free(c->u); 527 /* Restore c->u to the parent unit. */ 528 n = PyList_GET_SIZE(c->c_stack) - 1; 529 if (n >= 0) { 530 wrapper = PyList_GET_ITEM(c->c_stack, n); 531 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper); 532 assert(c->u); 533 /* we are deleting from a list so this really shouldn't fail */ 534 if (PySequence_DelItem(c->c_stack, n) < 0) 535 Py_FatalError("compiler_exit_scope()"); 536 compiler_unit_check(c->u); 537 } 538 else 539 c->u = NULL; 540 541} 542 543/* Allocate a new "anonymous" local variable. 544 Used by list comprehensions and with statements. 545*/ 546 547static PyObject * 548compiler_new_tmpname(struct compiler *c) 549{ 550 char tmpname[256]; 551 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname); 552 return PyString_FromString(tmpname); 553} 554 555/* Allocate a new block and return a pointer to it. 556 Returns NULL on error. 557*/ 558 559static basicblock * 560compiler_new_block(struct compiler *c) 561{ 562 basicblock *b; 563 struct compiler_unit *u; 564 565 u = c->u; 566 b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); 567 if (b == NULL) { 568 PyErr_NoMemory(); 569 return NULL; 570 } 571 memset((void *)b, 0, sizeof(basicblock)); 572 /* Extend the singly linked list of blocks with new block. */ 573 b->b_list = u->u_blocks; 574 u->u_blocks = b; 575 return b; 576} 577 578static basicblock * 579compiler_use_new_block(struct compiler *c) 580{ 581 basicblock *block = compiler_new_block(c); 582 if (block == NULL) 583 return NULL; 584 c->u->u_curblock = block; 585 return block; 586} 587 588static basicblock * 589compiler_use_next_block(struct compiler *c, basicblock *block) 590{ 591 assert(block != NULL); 592 593 c->u->u_curblock->b_next = block; 594 c->u->u_curblock = block; 595 return block; 596} 597 598static basicblock * 599compiler_next_block(struct compiler *c) 600{ 601 basicblock *block = compiler_new_block(c); 602 if (block == NULL) 603 return NULL; 604 605 return compiler_use_next_block(c, block); 606} 607/* Returns the offset of the next instruction in the current block's 608 b_instr array. Resizes the b_instr as necessary. 609 Returns -1 on failure. 610*/ 611 612static int 613compiler_next_instr(struct compiler *c, basicblock *b) 614{ 615 assert(b != NULL); 616 if (b->b_instr == NULL) { 617 b->b_instr = (struct instr *)PyObject_Malloc( 618 sizeof(struct instr) * DEFAULT_BLOCK_SIZE); 619 if (b->b_instr == NULL) { 620 PyErr_NoMemory(); 621 return -1; 622 } 623 b->b_ialloc = DEFAULT_BLOCK_SIZE; 624 memset((char *)b->b_instr, 0, 625 sizeof(struct instr) * DEFAULT_BLOCK_SIZE); 626 } 627 else if (b->b_iused == b->b_ialloc) { 628 struct instr *tmp; 629 size_t oldsize, newsize; 630 oldsize = b->b_ialloc * sizeof(struct instr); 631 newsize = oldsize << 1; 632 633 if (oldsize > (PY_SIZE_MAX >> 1)) { 634 PyErr_NoMemory(); 635 return -1; 636 } 637 638 if (newsize == 0) { 639 PyErr_NoMemory(); 640 return -1; 641 } 642 b->b_ialloc <<= 1; 643 tmp = (struct instr *)PyObject_Realloc( 644 (void *)b->b_instr, newsize); 645 if (tmp == NULL) { 646 PyErr_NoMemory(); 647 return -1; 648 } 649 b->b_instr = tmp; 650 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); 651 } 652 return b->b_iused++; 653} 654 655/* Set the i_lineno member of the instruction at offset off if the 656 line number for the current expression/statement has not 657 already been set. If it has been set, the call has no effect. 658 659 The line number is reset in the following cases: 660 - when entering a new scope 661 - on each statement 662 - on each expression that start a new line 663 - before the "except" clause 664 - before the "for" and "while" expressions 665*/ 666 667static void 668compiler_set_lineno(struct compiler *c, int off) 669{ 670 basicblock *b; 671 if (c->u->u_lineno_set) 672 return; 673 c->u->u_lineno_set = true; 674 b = c->u->u_curblock; 675 b->b_instr[off].i_lineno = c->u->u_lineno; 676} 677 678int 679_Py_OpcodeStackEffect(int opcode, int oparg) 680{ 681 switch (opcode) { 682 case POP_TOP: 683 return -1; 684 case ROT_TWO: 685 case ROT_THREE: 686 return 0; 687 case DUP_TOP: 688 return 1; 689 case ROT_FOUR: 690 return 0; 691 692 case UNARY_POSITIVE: 693 case UNARY_NEGATIVE: 694 case UNARY_NOT: 695 case UNARY_CONVERT: 696 case UNARY_INVERT: 697 return 0; 698 699 case LIST_APPEND: 700 return -2; 701 702 case BINARY_POWER: 703 case BINARY_MULTIPLY: 704 case BINARY_DIVIDE: 705 case BINARY_MODULO: 706 case BINARY_ADD: 707 case BINARY_SUBTRACT: 708 case BINARY_SUBSCR: 709 case BINARY_FLOOR_DIVIDE: 710 case BINARY_TRUE_DIVIDE: 711 return -1; 712 case INPLACE_FLOOR_DIVIDE: 713 case INPLACE_TRUE_DIVIDE: 714 return -1; 715 716 case SLICE_NONE: 717 return 0; 718 case SLICE_LEFT: 719 return -1; 720 case SLICE_RIGHT: 721 return -1; 722 case SLICE_BOTH: 723 return -2; 724 725 case STORE_SLICE_NONE: 726 return -2; 727 case STORE_SLICE_LEFT: 728 return -3; 729 case STORE_SLICE_RIGHT: 730 return -3; 731 case STORE_SLICE_BOTH: 732 return -4; 733 734 case DELETE_SLICE_NONE: 735 return -1; 736 case DELETE_SLICE_LEFT: 737 return -2; 738 case DELETE_SLICE_RIGHT: 739 return -2; 740 case DELETE_SLICE_BOTH: 741 return -3; 742 743 case INPLACE_ADD: 744 case INPLACE_SUBTRACT: 745 case INPLACE_MULTIPLY: 746 case INPLACE_DIVIDE: 747 case INPLACE_MODULO: 748 return -1; 749 case STORE_SUBSCR: 750 return -3; 751 case STORE_MAP: 752 return -2; 753 case DELETE_SUBSCR: 754 return -2; 755 756 case BINARY_LSHIFT: 757 case BINARY_RSHIFT: 758 case BINARY_AND: 759 case BINARY_XOR: 760 case BINARY_OR: 761 return -1; 762 case INPLACE_POWER: 763 return -1; 764 case GET_ITER: 765 return 0; 766 767 case INPLACE_LSHIFT: 768 case INPLACE_RSHIFT: 769 case INPLACE_AND: 770 case INPLACE_XOR: 771 case INPLACE_OR: 772 return -1; 773 case BREAK_LOOP: 774 return 0; 775 case WITH_CLEANUP: 776 return -1; 777 case RETURN_VALUE: 778 return -1; 779 case YIELD_VALUE: 780 return 0; 781 782 case POP_BLOCK: 783 return 0; 784 case END_FINALLY: 785 return -3; 786 787 case STORE_NAME: 788 return -1; 789 case DELETE_NAME: 790 return 0; 791 case UNPACK_SEQUENCE: 792 return oparg-1; 793 case FOR_ITER: 794 return 1; 795 796 case STORE_ATTR: 797 return -2; 798 case DELETE_ATTR: 799 return -1; 800 case STORE_GLOBAL: 801 return -1; 802 case DELETE_GLOBAL: 803 return 0; 804 case DUP_TOP_TWO: 805 return 2; 806 case DUP_TOP_THREE: 807 return 3; 808 case LOAD_CONST: 809 return 1; 810 case LOAD_NAME: 811 return 1; 812 case BUILD_TUPLE: 813 case BUILD_LIST: 814 return 1-oparg; 815 case BUILD_MAP: 816 return 1; 817 case LOAD_ATTR: 818 return 0; 819 case LOAD_METHOD: 820 return 1; /* Maybe set top, push one. */ 821 case COMPARE_OP: 822 return -1; 823 case IMPORT_NAME: 824 return -2; /* Pop three, push one. */ 825 826 case JUMP_FORWARD: 827 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */ 828 case JUMP_IF_FALSE_OR_POP: /* "" */ 829 case JUMP_ABSOLUTE: 830 return 0; 831 832 case POP_JUMP_IF_FALSE: 833 case POP_JUMP_IF_TRUE: 834 return -1; 835 836 case LOAD_GLOBAL: 837 return 1; 838 839 case CONTINUE_LOOP: 840 return 0; 841 case SETUP_LOOP: 842 return 0; 843 case SETUP_EXCEPT: 844 case SETUP_FINALLY: 845 return 3; /* actually pushed by an exception */ 846 847 case LOAD_FAST: 848 return 1; 849 case STORE_FAST: 850 return -1; 851 case DELETE_FAST: 852 return 0; 853 854 case RAISE_VARARGS_ZERO: 855 return 0; 856 case RAISE_VARARGS_ONE: 857 return -1; 858 case RAISE_VARARGS_TWO: 859 return -2; 860 case RAISE_VARARGS_THREE: 861 return -3; 862#define NARGS(o) (((o) % 256) + 2*((o) / 256)) 863 case CALL_FUNCTION: 864 return -NARGS(oparg); 865 case CALL_METHOD: 866 return -NARGS(oparg)-1; 867 case CALL_FUNCTION_VAR: 868 case CALL_FUNCTION_KW: 869 return -NARGS(oparg)-1; 870 case CALL_FUNCTION_VAR_KW: 871 return -NARGS(oparg)-2; 872#undef NARGS 873 case BUILD_SLICE_TWO: 874 return -1; 875 case BUILD_SLICE_THREE: 876 return -2; 877 case MAKE_CLOSURE: 878 return -(oparg + 1); 879 case LOAD_CLOSURE: 880 return 1; 881 case LOAD_DEREF: 882 return 1; 883 case STORE_DEREF: 884 return -1; 885 default: 886 fprintf(stderr, "opcode = %d\n", opcode); 887 Py_FatalError("_Py_OpcodeStackEffect()"); 888 889 } 890 return 0; /* not reachable */ 891} 892 893/* Add an opcode with no argument. 894 Returns 0 on failure, 1 on success. 895*/ 896 897static int 898compiler_addop(struct compiler *c, int opcode) 899{ 900 basicblock *b; 901 struct instr *i; 902 int off; 903 off = compiler_next_instr(c, c->u->u_curblock); 904 if (off < 0) 905 return 0; 906 b = c->u->u_curblock; 907 i = &b->b_instr[off]; 908 i->i_opcode = opcode; 909 i->i_hasarg = 0; 910 if (opcode == RETURN_VALUE) 911 b->b_return = 1; 912 compiler_set_lineno(c, off); 913 return 1; 914} 915 916/* Adds 'o' to the mapping stored in 'dict' (if it wasn't already 917 * there) so it can be looked up by opcodes like LOAD_CONST and 918 * LOAD_NAME. This function derives a tuple from o to avoid pairs of 919 * values like 0.0 and -0.0 or 0 and 0.0 that compare equal but need 920 * to be kept separate. 'o' is mapped to a small integer, and its 921 * mapping is returned. */ 922static int 923compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) 924{ 925 PyObject *t, *v; 926 Py_ssize_t arg; 927 unsigned char *p, *q; 928 Py_complex z; 929 double d; 930 int real_part_zero, imag_part_zero; 931 932 /* necessary to make sure types aren't coerced (e.g., int and long) */ 933 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */ 934 if (PyFloat_Check(o)) { 935 d = PyFloat_AS_DOUBLE(o); 936 p = (unsigned char*) &d; 937 /* all we need is to make the tuple different in either the 0.0 938 * or -0.0 case from all others, just to avoid the "coercion". 939 */ 940 if (*p==0 && p[sizeof(double)-1]==0) 941 t = PyTuple_Pack(3, o, o->ob_type, Py_None); 942 else 943 t = PyTuple_Pack(2, o, o->ob_type); 944 } 945 else if (PyComplex_Check(o)) { 946 /* complex case is even messier: we need to make complex(x, 947 0.) different from complex(x, -0.) and complex(0., y) 948 different from complex(-0., y), for any x and y. In 949 particular, all four complex zeros should be 950 distinguished.*/ 951 z = PyComplex_AsCComplex(o); 952 p = (unsigned char*) &(z.real); 953 q = (unsigned char*) &(z.imag); 954 /* all that matters here is that on IEEE platforms 955 real_part_zero will be true if z.real == 0., and false if 956 z.real == -0. In fact, real_part_zero will also be true 957 for some other rarely occurring nonzero floats, but this 958 doesn't matter. Similar comments apply to 959 imag_part_zero. */ 960 real_part_zero = *p==0 && p[sizeof(double)-1]==0; 961 imag_part_zero = *q==0 && q[sizeof(double)-1]==0; 962 if (real_part_zero && imag_part_zero) { 963 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True); 964 } 965 else if (real_part_zero && !imag_part_zero) { 966 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False); 967 } 968 else if (!real_part_zero && imag_part_zero) { 969 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True); 970 } 971 else { 972 t = PyTuple_Pack(2, o, o->ob_type); 973 } 974 } 975 else { 976 t = PyTuple_Pack(2, o, o->ob_type); 977 } 978 if (t == NULL) 979 return -1; 980 981 v = PyDict_GetItem(dict, t); 982 if (!v) { 983 arg = PyDict_Size(dict); 984 v = PyInt_FromLong(arg); 985 if (!v) { 986 Py_DECREF(t); 987 return -1; 988 } 989 if (PyDict_SetItem(dict, t, v) < 0) { 990 Py_DECREF(t); 991 Py_DECREF(v); 992 return -1; 993 } 994 Py_DECREF(v); 995 } 996 else 997 arg = PyInt_AsLong(v); 998 Py_DECREF(t); 999 return arg; 1000} 1001 1002/* Adds opcode(o) as the next instruction in the current basic block. 1003 Because opcodes only take integer arguments, we give 'o' a number 1004 unique within 'dict' and store the mapping in 'dict'. 'dict' is one 1005 of c->u->u_{consts,names,varnames} and must match the code::co_list 1006 that 'opcode' looks in. 1007*/ 1008static int 1009compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, 1010 PyObject *o) 1011{ 1012 int arg = compiler_add_o(c, dict, o); 1013 if (arg < 0) 1014 return 0; 1015 return compiler_addop_i(c, opcode, arg); 1016} 1017 1018/* Like compiler_addop_o, but name-mangles 'o' if appropriate. */ 1019static int 1020compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, 1021 PyObject *o) 1022{ 1023 int arg; 1024 PyObject *mangled = _Py_Mangle(c->u->u_private, o); 1025 if (!mangled) 1026 return 0; 1027 arg = compiler_add_o(c, dict, mangled); 1028 Py_DECREF(mangled); 1029 if (arg < 0) 1030 return 0; 1031 return compiler_addop_i(c, opcode, arg); 1032} 1033 1034/* Add an opcode with an integer argument. 1035 Returns 0 on failure, 1 on success. 1036*/ 1037 1038static int 1039compiler_addop_i(struct compiler *c, int opcode, int oparg) 1040{ 1041 struct instr *i; 1042 int off; 1043 off = compiler_next_instr(c, c->u->u_curblock); 1044 if (off < 0) 1045 return 0; 1046 i = &c->u->u_curblock->b_instr[off]; 1047 i->i_opcode = opcode; 1048 i->i_oparg = oparg; 1049 i->i_hasarg = 1; 1050 compiler_set_lineno(c, off); 1051 return 1; 1052} 1053 1054/* Adds a jump opcode whose target is 'b'. This works for both 1055 conditional and unconditional jumps. 1056*/ 1057static int 1058compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) 1059{ 1060 struct instr *i; 1061 int off; 1062 1063 assert(b != NULL); 1064 off = compiler_next_instr(c, c->u->u_curblock); 1065 if (off < 0) 1066 return 0; 1067 i = &c->u->u_curblock->b_instr[off]; 1068 i->i_opcode = opcode; 1069 i->i_target = b; 1070 i->i_hasarg = 1; 1071 if (absolute) 1072 i->i_jabs = 1; 1073 else 1074 i->i_jrel = 1; 1075 compiler_set_lineno(c, off); 1076 return 1; 1077} 1078 1079/* NEXT_BLOCK() creates a new block, creates an implicit jump from the 1080 current block to the new block, and sets the new block as the 1081 current block. 1082*/ 1083 1084/* The returns inside these macros make it impossible to decref objects 1085 created in the local function. Local objects should use the arena. 1086*/ 1087 1088 1089#define NEXT_BLOCK(C) { \ 1090 if (compiler_next_block((C)) == NULL) \ 1091 return 0; \ 1092} 1093 1094#define ADDOP(C, OP) { \ 1095 if (!compiler_addop((C), (OP))) \ 1096 return 0; \ 1097} 1098 1099#define ADDOP_IN_SCOPE(C, OP) { \ 1100 if (!compiler_addop((C), (OP))) { \ 1101 compiler_exit_scope(c); \ 1102 return 0; \ 1103 } \ 1104} 1105 1106#define ADDOP_O(C, OP, O, TYPE) { \ 1107 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ 1108 return 0; \ 1109} 1110 1111#define ADDOP_NAME(C, OP, O, TYPE) { \ 1112 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ 1113 return 0; \ 1114} 1115 1116#define ADDOP_I(C, OP, O) { \ 1117 if (!compiler_addop_i((C), (OP), (O))) \ 1118 return 0; \ 1119} 1120 1121#define ADDOP_JABS(C, OP, O) { \ 1122 if (!compiler_addop_j((C), (OP), (O), 1)) \ 1123 return 0; \ 1124} 1125 1126#define ADDOP_JREL(C, OP, O) { \ 1127 if (!compiler_addop_j((C), (OP), (O), 0)) \ 1128 return 0; \ 1129} 1130 1131/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use 1132 the ASDL name to synthesize the name of the C type and the visit function. 1133*/ 1134 1135#define VISIT(C, TYPE, V) {\ 1136 if (!compiler_visit_ ## TYPE((C), (V))) \ 1137 return 0; \ 1138} 1139 1140#define VISIT_IN_SCOPE(C, TYPE, V) {\ 1141 if (!compiler_visit_ ## TYPE((C), (V))) { \ 1142 compiler_exit_scope(c); \ 1143 return 0; \ 1144 } \ 1145} 1146 1147#define VISIT_SLICE(C, V, CTX) {\ 1148 if (!compiler_visit_slice((C), (V), (CTX))) \ 1149 return 0; \ 1150} 1151 1152#define VISIT_SEQ(C, TYPE, SEQ) { \ 1153 int _i; \ 1154 asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 1155 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ 1156 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ 1157 if (!compiler_visit_ ## TYPE((C), elt)) \ 1158 return 0; \ 1159 } \ 1160} 1161 1162#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ 1163 int _i; \ 1164 asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 1165 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ 1166 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ 1167 if (!compiler_visit_ ## TYPE((C), elt)) { \ 1168 compiler_exit_scope(c); \ 1169 return 0; \ 1170 } \ 1171 } \ 1172} 1173 1174static int 1175compiler_isdocstring(stmt_ty s) 1176{ 1177 if (s->kind != Expr_kind) 1178 return 0; 1179 return s->v.Expr.value->kind == Str_kind; 1180} 1181 1182/* Compile a sequence of statements, checking for a docstring. */ 1183 1184static int 1185compiler_body(struct compiler *c, asdl_seq *stmts) 1186{ 1187 int i = 0; 1188 stmt_ty st; 1189 1190 if (!asdl_seq_LEN(stmts)) 1191 return 1; 1192 st = (stmt_ty)asdl_seq_GET(stmts, 0); 1193 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) { 1194 /* don't generate docstrings if -OO */ 1195 i = 1; 1196 VISIT(c, expr, st->v.Expr.value); 1197 if (!compiler_nameop(c, __doc__, Store)) 1198 return 0; 1199 } 1200 for (; i < asdl_seq_LEN(stmts); i++) 1201 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); 1202 return 1; 1203} 1204 1205static PyCodeObject * 1206compiler_mod(struct compiler *c, mod_ty mod) 1207{ 1208 PyCodeObject *co; 1209 int addNone = 1; 1210 static PyObject *module; 1211 if (!module) { 1212 module = PyString_InternFromString("<module>"); 1213 if (!module) 1214 return NULL; 1215 } 1216 /* Use 0 for firstlineno initially, will fixup in assemble(). */ 1217 if (!compiler_enter_scope(c, module, mod, 0)) 1218 return NULL; 1219 switch (mod->kind) { 1220 case Module_kind: 1221 if (!compiler_body(c, mod->v.Module.body)) { 1222 compiler_exit_scope(c); 1223 return 0; 1224 } 1225 break; 1226 case Interactive_kind: 1227 c->c_interactive = 1; 1228 VISIT_SEQ_IN_SCOPE(c, stmt, 1229 mod->v.Interactive.body); 1230 break; 1231 case Expression_kind: 1232 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); 1233 addNone = 0; 1234 break; 1235 case Suite_kind: 1236 PyErr_SetString(PyExc_SystemError, 1237 "suite should not be possible"); 1238 return 0; 1239 default: 1240 PyErr_Format(PyExc_SystemError, 1241 "module kind %d should not be possible", 1242 mod->kind); 1243 return 0; 1244 } 1245 co = assemble(c, addNone); 1246 compiler_exit_scope(c); 1247 return co; 1248} 1249 1250/* The test for LOCAL must come before the test for FREE in order to 1251 handle classes where name is both local and free. The local var is 1252 a method and the free var is a free var referenced within a method. 1253*/ 1254 1255static int 1256get_ref_type(struct compiler *c, PyObject *name) 1257{ 1258 int scope = PyST_GetScope(c->u->u_ste, name); 1259 if (scope == 0) { 1260 char buf[350]; 1261 PyOS_snprintf(buf, sizeof(buf), 1262 "unknown scope for %.100s in %.100s(%s) in %s\n" 1263 "symbols: %s\nlocals: %s\nglobals: %s\n", 1264 PyString_AS_STRING(name), 1265 PyString_AS_STRING(c->u->u_name), 1266 PyObject_REPR(c->u->u_ste->ste_id), 1267 c->c_filename, 1268 PyObject_REPR(c->u->u_ste->ste_symbols), 1269 PyObject_REPR(c->u->u_varnames), 1270 PyObject_REPR(c->u->u_names) 1271 ); 1272 Py_FatalError(buf); 1273 } 1274 1275 return scope; 1276} 1277 1278static int 1279compiler_lookup_arg(PyObject *dict, PyObject *name) 1280{ 1281 PyObject *k, *v; 1282 k = PyTuple_Pack(2, name, name->ob_type); 1283 if (k == NULL) 1284 return -1; 1285 v = PyDict_GetItem(dict, k); 1286 Py_DECREF(k); 1287 if (v == NULL) 1288 return -1; 1289 return PyInt_AS_LONG(v); 1290} 1291 1292static int 1293compiler_make_closure(struct compiler *c, PyCodeObject *co, asdl_seq *defaults) 1294{ 1295 int i, free = PyCode_GetNumFree(co), ndefaults = 0; 1296 if (free == 0) { 1297 if (!compiler_load_global(c, "#@make_function")) 1298 return 0; 1299 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1300 if (defaults) 1301 VISIT_SEQ(c, expr, defaults); 1302 ADDOP_I(c, CALL_FUNCTION, asdl_seq_LEN(defaults) + 1); 1303 return 1; 1304 } 1305 if (defaults) { 1306 ndefaults = asdl_seq_LEN(defaults); 1307 VISIT_SEQ(c, expr, defaults); 1308 } 1309 for (i = 0; i < free; ++i) { 1310 /* Bypass com_addop_varname because it will generate 1311 LOAD_DEREF but LOAD_CLOSURE is needed. 1312 */ 1313 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); 1314 int arg, reftype; 1315 1316 /* Special case: If a class contains a method with a 1317 free variable that has the same name as a method, 1318 the name will be considered free *and* local in the 1319 class. It should be handled by the closure, as 1320 well as by the normal name loookup logic. 1321 */ 1322 reftype = get_ref_type(c, name); 1323 if (reftype == CELL) 1324 arg = compiler_lookup_arg(c->u->u_cellvars, name); 1325 else /* (reftype == FREE) */ 1326 arg = compiler_lookup_arg(c->u->u_freevars, name); 1327 if (arg == -1) { 1328 printf("lookup %s in %s %d %d\n" 1329 "freevars of %s: %s\n", 1330 PyObject_REPR(name), 1331 PyString_AS_STRING(c->u->u_name), 1332 reftype, arg, 1333 PyString_AS_STRING(co->co_name), 1334 PyObject_REPR(co->co_freevars)); 1335 Py_FatalError("compiler_make_closure()"); 1336 } 1337 ADDOP_I(c, LOAD_CLOSURE, arg); 1338 } 1339 ADDOP_I(c, BUILD_TUPLE, free); 1340 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1341 ADDOP_I(c, MAKE_CLOSURE, ndefaults); 1342 return 1; 1343} 1344 1345static int 1346compiler_decorators(struct compiler *c, asdl_seq* decos) 1347{ 1348 int i; 1349 1350 if (!decos) 1351 return 1; 1352 1353 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1354 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); 1355 } 1356 return 1; 1357} 1358 1359static int 1360compiler_arguments(struct compiler *c, arguments_ty args) 1361{ 1362 int i; 1363 int n = asdl_seq_LEN(args->args); 1364 /* Correctly handle nested argument lists */ 1365 for (i = 0; i < n; i++) { 1366 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i); 1367 if (arg->kind == Tuple_kind) { 1368 PyObject *id = PyString_FromFormat(".%d", i); 1369 if (id == NULL) { 1370 return 0; 1371 } 1372 if (!compiler_nameop(c, id, Load)) { 1373 Py_DECREF(id); 1374 return 0; 1375 } 1376 Py_DECREF(id); 1377 VISIT(c, expr, arg); 1378 } 1379 } 1380 return 1; 1381} 1382 1383static int 1384compiler_function(struct compiler *c, stmt_ty s) 1385{ 1386 PyCodeObject *co; 1387 PyObject *first_const = Py_None; 1388 arguments_ty args = s->v.FunctionDef.args; 1389 asdl_seq* decos = s->v.FunctionDef.decorator_list; 1390 stmt_ty st; 1391 int i, n, docstring; 1392 1393 assert(s->kind == FunctionDef_kind); 1394 1395 if (!compiler_decorators(c, decos)) 1396 return 0; 1397 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s, 1398 s->lineno)) 1399 return 0; 1400 1401 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); 1402 docstring = compiler_isdocstring(st); 1403 if (docstring && Py_OptimizeFlag < 2) 1404 first_const = st->v.Expr.value->v.Str.s; 1405 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { 1406 compiler_exit_scope(c); 1407 return 0; 1408 } 1409 1410 /* unpack nested arguments */ 1411 compiler_arguments(c, args); 1412 1413 c->u->u_argcount = asdl_seq_LEN(args->args); 1414 n = asdl_seq_LEN(s->v.FunctionDef.body); 1415 /* if there was a docstring, we need to skip the first statement */ 1416 for (i = docstring; i < n; i++) { 1417 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); 1418 VISIT_IN_SCOPE(c, stmt, st); 1419 } 1420 co = assemble(c, 1); 1421 compiler_exit_scope(c); 1422 if (co == NULL) 1423 return 0; 1424 1425 compiler_make_closure(c, co, args->defaults); 1426 Py_DECREF(co); 1427 1428 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1429 ADDOP_I(c, CALL_FUNCTION, 1); 1430 } 1431 1432 return compiler_nameop(c, s->v.FunctionDef.name, Store); 1433} 1434 1435static int 1436compiler_class(struct compiler *c, stmt_ty s) 1437{ 1438 int n, i; 1439 PyCodeObject *co; 1440 PyObject *str; 1441 asdl_seq* decos = s->v.ClassDef.decorator_list; 1442 1443 if (!compiler_decorators(c, decos)) 1444 return 0; 1445 1446 if (!compiler_load_global(c, "#@buildclass")) 1447 return 0; 1448 1449 /* push class name on stack, needed by #@buildclass */ 1450 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); 1451 /* push the tuple of base classes on the stack */ 1452 n = asdl_seq_LEN(s->v.ClassDef.bases); 1453 if (n > 0) 1454 VISIT_SEQ(c, expr, s->v.ClassDef.bases); 1455 ADDOP_I(c, BUILD_TUPLE, n); 1456 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, 1457 s->lineno)) 1458 return 0; 1459 Py_XDECREF(c->u->u_private); 1460 c->u->u_private = s->v.ClassDef.name; 1461 Py_INCREF(c->u->u_private); 1462 str = PyString_InternFromString("__name__"); 1463 if (!str || !compiler_nameop(c, str, Load)) { 1464 Py_XDECREF(str); 1465 compiler_exit_scope(c); 1466 return 0; 1467 } 1468 1469 Py_DECREF(str); 1470 str = PyString_InternFromString("__module__"); 1471 if (!str || !compiler_nameop(c, str, Store)) { 1472 Py_XDECREF(str); 1473 compiler_exit_scope(c); 1474 return 0; 1475 } 1476 Py_DECREF(str); 1477 1478 if (!compiler_body(c, s->v.ClassDef.body)) { 1479 compiler_exit_scope(c); 1480 return 0; 1481 } 1482 1483 if (!compiler_load_global(c, "#@locals")) 1484 return 0; 1485 ADDOP_I(c, CALL_FUNCTION, 0); 1486 1487 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1488 co = assemble(c, 1); 1489 compiler_exit_scope(c); 1490 if (co == NULL) 1491 return 0; 1492 1493 compiler_make_closure(c, co, NULL); 1494 Py_DECREF(co); 1495 1496 ADDOP_I(c, CALL_FUNCTION, 0); 1497 1498 /* Call #@buildclass */ 1499 ADDOP_I(c, CALL_FUNCTION, 3); 1500 1501 /* apply decorators */ 1502 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1503 ADDOP_I(c, CALL_FUNCTION, 1); 1504 } 1505 if (!compiler_nameop(c, s->v.ClassDef.name, Store)) 1506 return 0; 1507 return 1; 1508} 1509 1510static int 1511compiler_exec(struct compiler *c, stmt_ty s) 1512{ 1513 if (!compiler_load_global(c, "#@exec")) 1514 return 0; 1515 1516 VISIT(c, expr, s->v.Exec.body); 1517 if (s->v.Exec.globals) { 1518 VISIT(c, expr, s->v.Exec.globals); 1519 if (s->v.Exec.locals) { 1520 VISIT(c, expr, s->v.Exec.locals); 1521 } else { 1522 ADDOP(c, DUP_TOP); 1523 } 1524 } else { 1525 ADDOP_O(c, LOAD_CONST, Py_None, consts); 1526 ADDOP(c, DUP_TOP); 1527 } 1528 ADDOP_I(c, CALL_FUNCTION, 3); 1529 ADDOP(c, POP_TOP); 1530 c->u->u_uses_exec = true; 1531 return 1; 1532} 1533 1534static int 1535compiler_ifexp(struct compiler *c, expr_ty e) 1536{ 1537 basicblock *end, *next; 1538 1539 assert(e->kind == IfExp_kind); 1540 end = compiler_new_block(c); 1541 if (end == NULL) 1542 return 0; 1543 next = compiler_new_block(c); 1544 if (next == NULL) 1545 return 0; 1546 VISIT(c, expr, e->v.IfExp.test); 1547 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next); 1548 VISIT(c, expr, e->v.IfExp.body); 1549 ADDOP_JREL(c, JUMP_FORWARD, end); 1550 compiler_use_next_block(c, next); 1551 VISIT(c, expr, e->v.IfExp.orelse); 1552 compiler_use_next_block(c, end); 1553 return 1; 1554} 1555 1556static int 1557compiler_lambda(struct compiler *c, expr_ty e) 1558{ 1559 PyCodeObject *co; 1560 static identifier name; 1561 arguments_ty args = e->v.Lambda.args; 1562 assert(e->kind == Lambda_kind); 1563 1564 if (!name) { 1565 name = PyString_InternFromString("<lambda>"); 1566 if (!name) 1567 return 0; 1568 } 1569 1570 if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) 1571 return 0; 1572 1573 /* unpack nested arguments */ 1574 compiler_arguments(c, args); 1575 1576 c->u->u_argcount = asdl_seq_LEN(args->args); 1577 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); 1578 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1579 co = assemble(c, 1); 1580 compiler_exit_scope(c); 1581 if (co == NULL) 1582 return 0; 1583 1584 compiler_make_closure(c, co, args->defaults); 1585 Py_DECREF(co); 1586 1587 return 1; 1588} 1589 1590static int 1591compiler_print(struct compiler *c, stmt_ty s) 1592{ 1593 int i, n, kwargs = 0; 1594 PyObject *str; 1595 1596 assert(s->kind == Print_kind); 1597 n = asdl_seq_LEN(s->v.Print.values); 1598 1599 if (!compiler_load_global(c, "#@print_stmt")) 1600 return 0; 1601 1602 for (i = 0; i < n; i++) { 1603 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i); 1604 VISIT(c, expr, e); 1605 } 1606 if (!s->v.Print.nl) { 1607 str = PyString_InternFromString("end"); 1608 if (!str) 1609 return 0; 1610 ADDOP_O(c, LOAD_CONST, str, consts); 1611 Py_DECREF(str); 1612 str = PyString_FromString(""); 1613 if (!str) 1614 return 0; 1615 ADDOP_O(c, LOAD_CONST, str, consts); 1616 Py_DECREF(str); 1617 kwargs++; 1618 } 1619 if (s->v.Print.dest) { 1620 str = PyString_InternFromString("file"); 1621 if (!str) 1622 return 0; 1623 ADDOP_O(c, LOAD_CONST, str, consts); 1624 Py_DECREF(str); 1625 VISIT(c, expr, s->v.Print.dest); 1626 kwargs++; 1627 } 1628 ADDOP_I(c, CALL_FUNCTION, n | (kwargs << 8)); 1629 ADDOP(c, POP_TOP); 1630 return 1; 1631} 1632 1633static int 1634compiler_if(struct compiler *c, stmt_ty s) 1635{ 1636 basicblock *end, *next; 1637 int constant; 1638 assert(s->kind == If_kind); 1639 end = compiler_new_block(c); 1640 if (end == NULL) 1641 return 0; 1642 1643 constant = expr_constant(s->v.If.test); 1644 /* constant = 0: "if 0" 1645 * constant = 1: "if 1", "if 2", ... 1646 * constant = -1: rest */ 1647 if (constant == 0) { 1648 if (s->v.If.orelse) 1649 VISIT_SEQ(c, stmt, s->v.If.orelse); 1650 } else if (constant == 1) { 1651 VISIT_SEQ(c, stmt, s->v.If.body); 1652 } else { 1653 if (s->v.If.orelse) { 1654 next = compiler_new_block(c); 1655 if (next == NULL) 1656 return 0; 1657 } 1658 else 1659 next = end; 1660 VISIT(c, expr, s->v.If.test); 1661 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next); 1662 VISIT_SEQ(c, stmt, s->v.If.body); 1663 ADDOP_JREL(c, JUMP_FORWARD, end); 1664 if (s->v.If.orelse) { 1665 compiler_use_next_block(c, next); 1666 VISIT_SEQ(c, stmt, s->v.If.orelse); 1667 } 1668 } 1669 compiler_use_next_block(c, end); 1670 return 1; 1671} 1672 1673static int 1674compiler_for(struct compiler *c, stmt_ty s) 1675{ 1676 basicblock *start, *cleanup, *end; 1677 1678 start = compiler_new_block(c); 1679 cleanup = compiler_new_block(c); 1680 end = compiler_new_block(c); 1681 if (start == NULL || end == NULL || cleanup == NULL) 1682 return 0; 1683 if (c->u->u_ste->ste_blockstack) 1684 ADDOP_JREL(c, SETUP_LOOP, end); 1685 if (!compiler_push_fblock(c, FOR_LOOP, start, end)) 1686 return 0; 1687 VISIT(c, expr, s->v.For.iter); 1688 ADDOP(c, GET_ITER); 1689 compiler_use_next_block(c, start); 1690 ADDOP_JREL(c, FOR_ITER, cleanup); 1691 VISIT(c, expr, s->v.For.target); 1692 VISIT_SEQ(c, stmt, s->v.For.body); 1693 ADDOP_JABS(c, JUMP_ABSOLUTE, start); 1694 compiler_use_next_block(c, cleanup); 1695 if (c->u->u_ste->ste_blockstack) 1696 ADDOP(c, POP_BLOCK); 1697 compiler_pop_fblock(c, FOR_LOOP, start, end); 1698 VISIT_SEQ(c, stmt, s->v.For.orelse); 1699 compiler_use_next_block(c, end); 1700 return 1; 1701} 1702 1703static int 1704compiler_while(struct compiler *c, stmt_ty s) 1705{ 1706 basicblock *loop, *orelse, *end, *anchor = NULL; 1707 int constant = expr_constant(s->v.While.test); 1708 1709 if (constant == 0) { 1710 if (s->v.While.orelse) 1711 VISIT_SEQ(c, stmt, s->v.While.orelse); 1712 return 1; 1713 } 1714 loop = compiler_new_block(c); 1715 end = compiler_new_block(c); 1716 if (constant == -1) { 1717 anchor = compiler_new_block(c); 1718 if (anchor == NULL) 1719 return 0; 1720 } 1721 if (loop == NULL || end == NULL) 1722 return 0; 1723 if (s->v.While.orelse) { 1724 orelse = compiler_new_block(c); 1725 if (orelse == NULL) 1726 return 0; 1727 } 1728 else 1729 orelse = NULL; 1730 1731 if (c->u->u_ste->ste_blockstack) 1732 ADDOP_JREL(c, SETUP_LOOP, end); 1733 compiler_use_next_block(c, loop); 1734 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end)) 1735 return 0; 1736 if (constant == -1) { 1737 VISIT(c, expr, s->v.While.test); 1738 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor); 1739 } 1740 VISIT_SEQ(c, stmt, s->v.While.body); 1741 ADDOP_JABS(c, JUMP_ABSOLUTE, loop); 1742 1743 /* XXX should the two POP instructions be in a separate block 1744 if there is no else clause ? 1745 */ 1746 1747 if (constant == -1) { 1748 compiler_use_next_block(c, anchor); 1749 if (c->u->u_ste->ste_blockstack) 1750 ADDOP(c, POP_BLOCK); 1751 } 1752 compiler_pop_fblock(c, WHILE_LOOP, loop, end); 1753 if (orelse != NULL) { /* what if orelse is just pass? */ 1754 compiler_use_next_block(c, orelse); 1755 VISIT_SEQ(c, stmt, s->v.While.orelse); 1756 } 1757 compiler_use_next_block(c, end); 1758 1759 return 1; 1760} 1761 1762static int 1763compiler_break(struct compiler *c) 1764{ 1765 if (!compiler_in_loop(c)) 1766 return compiler_error(c, "'break' outside loop"); 1767 if (c->u->u_ste->ste_blockstack) { 1768 ADDOP(c, BREAK_LOOP); 1769 } else { 1770 int top = c->u->u_nfblocks - 1; 1771 assert(c->u->u_fblock[top].fb_target); 1772 if (c->u->u_fblock[top].fb_type == FOR_LOOP) { 1773 ADDOP(c, POP_TOP); 1774 } else { 1775 assert(c->u->u_fblock[top].fb_type == WHILE_LOOP); 1776 } 1777 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[top].fb_target); 1778 } 1779 return 1; 1780} 1781 1782static int 1783compiler_continue(struct compiler *c) 1784{ 1785 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop"; 1786 static const char IN_FINALLY_ERROR_MSG[] = 1787 "'continue' not supported inside 'finally' clause"; 1788 int i; 1789 1790 if (!c->u->u_nfblocks) 1791 return compiler_error(c, LOOP_ERROR_MSG); 1792 i = c->u->u_nfblocks - 1; 1793 switch (c->u->u_fblock[i].fb_type) { 1794 case FOR_LOOP: 1795 case WHILE_LOOP: 1796 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block); 1797 break; 1798 case EXCEPT: 1799 case FINALLY_TRY: 1800 while (--i >= 0 && !(c->u->u_fblock[i].fb_type == FOR_LOOP || 1801 c->u->u_fblock[i].fb_type == WHILE_LOOP)) { 1802 /* Prevent continue anywhere under a finally 1803 even if hidden in a sub-try or except. */ 1804 if (c->u->u_fblock[i].fb_type == FINALLY_END) 1805 return compiler_error(c, IN_FINALLY_ERROR_MSG); 1806 } 1807 if (i == -1) 1808 return compiler_error(c, LOOP_ERROR_MSG); 1809 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block); 1810 break; 1811 case FINALLY_END: 1812 return compiler_error(c, IN_FINALLY_ERROR_MSG); 1813 } 1814 1815 return 1; 1816} 1817 1818/* Code generated for "try: <body> finally: <finalbody>" is as follows: 1819 1820 SETUP_FINALLY L 1821 <code for body> 1822 POP_BLOCK 1823 LOAD_CONST <None> 1824 L: <code for finalbody> 1825 END_FINALLY 1826 1827 The special instructions use the block stack. Each block 1828 stack entry contains the instruction that created it (here 1829 SETUP_FINALLY), the level of the value stack at the time the 1830 block stack entry was created, and a label (here L). 1831 1832 SETUP_FINALLY: 1833 Pushes the current value stack level and the label 1834 onto the block stack. 1835 POP_BLOCK: 1836 Pops en entry from the block stack, and pops the value 1837 stack until its level is the same as indicated on the 1838 block stack. (The label is ignored.) 1839 END_FINALLY: 1840 Pops a variable number of entries from the *value* stack 1841 and re-raises the exception they specify. The number of 1842 entries popped depends on the (pseudo) exception type. 1843 1844 The block stack is unwound when an exception is raised: 1845 when a SETUP_FINALLY entry is found, the exception is pushed 1846 onto the value stack (and the exception condition is cleared), 1847 and the interpreter jumps to the label gotten from the block 1848 stack. 1849*/ 1850 1851static int 1852compiler_try_finally(struct compiler *c, stmt_ty s) 1853{ 1854 basicblock *body, *end; 1855 body = compiler_new_block(c); 1856 end = compiler_new_block(c); 1857 if (body == NULL || end == NULL) 1858 return 0; 1859 1860 ADDOP_JREL(c, SETUP_FINALLY, end); 1861 compiler_use_next_block(c, body); 1862 if (!compiler_push_fblock(c, FINALLY_TRY, body, end)) 1863 return 0; 1864 VISIT_SEQ(c, stmt, s->v.TryFinally.body); 1865 ADDOP(c, POP_BLOCK); 1866 compiler_pop_fblock(c, FINALLY_TRY, body, end); 1867 1868 ADDOP_O(c, LOAD_CONST, Py_None, consts); 1869 ADDOP(c, DUP_TOP); 1870 ADDOP(c, DUP_TOP); 1871 compiler_use_next_block(c, end); 1872 if (!compiler_push_fblock(c, FINALLY_END, end, NULL)) 1873 return 0; 1874 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody); 1875 ADDOP(c, END_FINALLY); 1876 compiler_pop_fblock(c, FINALLY_END, end, NULL); 1877 1878 return 1; 1879} 1880 1881/* 1882 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...": 1883 (The contents of the value stack is shown in [], with the top 1884 at the right; 'tb' is trace-back info, 'val' the exception's 1885 associated value, and 'exc' the exception.) 1886 1887 Value stack Label Instruction Argument 1888 [] SETUP_EXCEPT L1 1889 [] <code for S> 1890 [] POP_BLOCK 1891 [] JUMP_FORWARD L0 1892 1893 [tb, val, exc] L1: DUP ) 1894 [tb, val, exc, exc] <evaluate E1> ) 1895 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 1896 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 ) 1897 [tb, val, exc] POP 1898 [tb, val] <assign to V1> (or POP if no V1) 1899 [tb] POP 1900 [] <code for S1> 1901 JUMP_FORWARD L0 1902 1903 [tb, val, exc] L2: DUP 1904 .............................etc....................... 1905 1906 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception 1907 1908 [] L0: <next statement> 1909 1910 Of course, parts are not generated if Vi or Ei is not present. 1911*/ 1912static int 1913compiler_try_except(struct compiler *c, stmt_ty s) 1914{ 1915 basicblock *body, *orelse, *except, *end; 1916 int i, n; 1917 1918 body = compiler_new_block(c); 1919 except = compiler_new_block(c); 1920 orelse = compiler_new_block(c); 1921 end = compiler_new_block(c); 1922 if (body == NULL || except == NULL || orelse == NULL || end == NULL) 1923 return 0; 1924 ADDOP_JREL(c, SETUP_EXCEPT, except); 1925 compiler_use_next_block(c, body); 1926 if (!compiler_push_fblock(c, EXCEPT, body, except)) 1927 return 0; 1928 VISIT_SEQ(c, stmt, s->v.TryExcept.body); 1929 ADDOP(c, POP_BLOCK); 1930 compiler_pop_fblock(c, EXCEPT, body, except); 1931 ADDOP_JREL(c, JUMP_FORWARD, orelse); 1932 n = asdl_seq_LEN(s->v.TryExcept.handlers); 1933 compiler_use_next_block(c, except); 1934 for (i = 0; i < n; i++) { 1935 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( 1936 s->v.TryExcept.handlers, i); 1937 if (!handler->v.ExceptHandler.type && i < n-1) 1938 return compiler_error(c, "default 'except:' must be last"); 1939 c->u->u_lineno_set = false; 1940 c->u->u_lineno = handler->lineno; 1941 except = compiler_new_block(c); 1942 if (except == NULL) 1943 return 0; 1944 if (handler->v.ExceptHandler.type) { 1945 ADDOP(c, DUP_TOP); 1946 VISIT(c, expr, handler->v.ExceptHandler.type); 1947 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); 1948 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); 1949 } 1950 ADDOP(c, POP_TOP); 1951 if (handler->v.ExceptHandler.name) { 1952 VISIT(c, expr, handler->v.ExceptHandler.name); 1953 } 1954 else { 1955 ADDOP(c, POP_TOP); 1956 } 1957 ADDOP(c, POP_TOP); 1958 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); 1959 ADDOP_JREL(c, JUMP_FORWARD, end); 1960 compiler_use_next_block(c, except); 1961 } 1962 ADDOP(c, END_FINALLY); 1963 compiler_use_next_block(c, orelse); 1964 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse); 1965 compiler_use_next_block(c, end); 1966 return 1; 1967} 1968 1969static int 1970compiler_import_as(struct compiler *c, identifier name, identifier asname) 1971{ 1972 /* The IMPORT_NAME opcode was already generated. This function 1973 merely needs to bind the result to a name. 1974 1975 If there is a dot in name, we need to split it and emit a 1976 LOAD_ATTR for each name. 1977 */ 1978 const char *src = PyString_AS_STRING(name); 1979 const char *dot = strchr(src, '.'); 1980 if (dot) { 1981 /* Consume the base module name to get the first attribute */ 1982 src = dot + 1; 1983 while (dot) { 1984 /* NB src is only defined when dot != NULL */ 1985 PyObject *attr; 1986 dot = strchr(src, '.'); 1987 attr = PyString_FromStringAndSize(src, 1988 dot ? dot - src : strlen(src)); 1989 if (!attr) 1990 return -1; 1991 ADDOP_O(c, LOAD_ATTR, attr, names); 1992 Py_DECREF(attr); 1993 src = dot + 1; 1994 } 1995 } 1996 return compiler_nameop(c, asname, Store); 1997} 1998 1999static int 2000compiler_import(struct compiler *c, stmt_ty s) 2001{ 2002 /* The Import node stores a module name like a.b.c as a single 2003 string. This is convenient for all cases except 2004 import a.b.c as d 2005 where we need to parse that string to extract the individual 2006 module names. 2007 XXX …
Large files files are truncated, but you can click here to view the full file