LEFT | RIGHT |
1 /* | 1 /* |
2 * This file compiles an abstract syntax tree (AST) into Python bytecode. | 2 * This file compiles an abstract syntax tree (AST) into Python bytecode. |
3 * | 3 * |
4 * The primary entry point is PyAST_Compile(), which returns a | 4 * The primary entry point is PyAST_Compile(), which returns a |
5 * PyCodeObject. The compiler makes several passes to build the code | 5 * PyCodeObject. The compiler makes several passes to build the code |
6 * object: | 6 * object: |
7 * 1. Checks for future statements. See future.c | 7 * 1. Checks for future statements. See future.c |
8 * 2. Builds a symbol table. See symtable.c. | 8 * 2. Builds a symbol table. See symtable.c. |
9 * 3. Generate code for basic blocks. See compiler_mod() in this file. | 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 | 10 * 4. Assemble the basic blocks into final code. See assemble() in |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "compile.h" | 31 #include "compile.h" |
32 #include "symtable.h" | 32 #include "symtable.h" |
33 #include "opcode.h" | 33 #include "opcode.h" |
34 | 34 |
35 int Py_OptimizeFlag = 0; | 35 int Py_OptimizeFlag = 0; |
36 | 36 |
37 #define DEFAULT_BLOCK_SIZE 16 | 37 #define DEFAULT_BLOCK_SIZE 16 |
38 #define DEFAULT_BLOCKS 8 | 38 #define DEFAULT_BLOCKS 8 |
39 #define DEFAULT_CODE_SIZE 128 | 39 #define DEFAULT_CODE_SIZE 128 |
40 #define DEFAULT_LNOTAB_SIZE 16 | 40 #define DEFAULT_LNOTAB_SIZE 16 |
41 | |
42 #define COMP_GENEXP 0 | |
43 #define COMP_LISTCOMP 1 | |
44 #define COMP_SETCOMP 2 | |
45 #define COMP_DICTCOMP 3 | |
46 | 41 |
47 struct instr { | 42 struct instr { |
48 unsigned i_jabs : 1; | 43 unsigned i_jabs : 1; |
49 unsigned i_jrel : 1; | 44 unsigned i_jrel : 1; |
50 unsigned i_hasarg : 1; | 45 unsigned i_hasarg : 1; |
51 unsigned char i_opcode; | 46 unsigned char i_opcode; |
52 int i_oparg; | 47 int i_oparg; |
53 struct basicblock_ *i_target; /* target block (if jump instruction) */ | 48 struct basicblock_ *i_target; /* target block (if jump instruction) */ |
54 int i_lineno; | 49 int i_lineno; |
55 }; | 50 }; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 */ | 100 */ |
106 PyObject *u_consts; /* all constants */ | 101 PyObject *u_consts; /* all constants */ |
107 PyObject *u_names; /* all names */ | 102 PyObject *u_names; /* all names */ |
108 PyObject *u_varnames; /* local variables */ | 103 PyObject *u_varnames; /* local variables */ |
109 PyObject *u_cellvars; /* cell variables */ | 104 PyObject *u_cellvars; /* cell variables */ |
110 PyObject *u_freevars; /* free variables */ | 105 PyObject *u_freevars; /* free variables */ |
111 | 106 |
112 PyObject *u_private; /* for private name mangling */ | 107 PyObject *u_private; /* for private name mangling */ |
113 | 108 |
114 int u_argcount; /* number of arguments for block */· | 109 int u_argcount; /* number of arguments for block */· |
115 int u_kwonlyargcount; /* number of keyword only arguments for block */ | |
116 /* Pointer to the most recently allocated block. By following b_list | 110 /* Pointer to the most recently allocated block. By following b_list |
117 members, you can reach all early allocated blocks. */ | 111 members, you can reach all early allocated blocks. */ |
118 basicblock *u_blocks; | 112 basicblock *u_blocks; |
119 basicblock *u_curblock; /* pointer to current block */ | 113 basicblock *u_curblock; /* pointer to current block */ |
120 int u_tmpname; /* temporary variables for list comps */ | 114 int u_tmpname; /* temporary variables for list comps */ |
121 | 115 |
122 int u_nfblocks; | 116 int u_nfblocks; |
123 struct fblockinfo u_fblock[CO_MAXBLOCKS]; | 117 struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
124 | 118 |
125 int u_firstlineno; /* the first lineno of the block */ | 119 int u_firstlineno; /* the first lineno of the block */ |
126 int u_lineno; /* the lineno for the current stmt */ | 120 int u_lineno; /* the lineno for the current stmt */ |
127 » int u_lineno_set; /* boolean to indicate whether instr | 121 » bool u_lineno_set; /* boolean to indicate whether instr |
128 has been generated with current lineno */ | 122 has been generated with current lineno */ |
129 }; | 123 }; |
130 | 124 |
131 /* This struct captures the global state of a compilation.·· | 125 /* This struct captures the global state of a compilation.·· |
132 | 126 |
133 The u pointer points to the current compilation unit, while units | 127 The u pointer points to the current compilation unit, while units |
134 for enclosing blocks are stored in c_stack. The u and c_stack are | 128 for enclosing blocks are stored in c_stack. The u and c_stack are |
135 managed by compiler_enter_scope() and compiler_exit_scope(). | 129 managed by compiler_enter_scope() and compiler_exit_scope(). |
136 */ | 130 */ |
137 | 131 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 basicblock *); | 168 basicblock *); |
175 static void compiler_pop_fblock(struct compiler *, enum fblocktype, | 169 static void compiler_pop_fblock(struct compiler *, enum fblocktype, |
176 basicblock *); | 170 basicblock *); |
177 /* Returns true if there is a loop on the fblock stack. */ | 171 /* Returns true if there is a loop on the fblock stack. */ |
178 static int compiler_in_loop(struct compiler *); | 172 static int compiler_in_loop(struct compiler *); |
179 | 173 |
180 static int inplace_binop(struct compiler *, operator_ty); | 174 static int inplace_binop(struct compiler *, operator_ty); |
181 static int expr_constant(expr_ty e); | 175 static int expr_constant(expr_ty e); |
182 | 176 |
183 static int compiler_with(struct compiler *, stmt_ty); | 177 static int compiler_with(struct compiler *, stmt_ty); |
184 static int compiler_call_helper(struct compiler *c, int n, | |
185 asdl_seq *args, | |
186 asdl_seq *keywords, | |
187 expr_ty starargs, | |
188 expr_ty kwargs); | |
189 | 178 |
190 static PyCodeObject *assemble(struct compiler *, int addNone); | 179 static PyCodeObject *assemble(struct compiler *, int addNone); |
191 static PyObject *__doc__; | 180 static PyObject *__doc__; |
192 | 181 |
193 PyObject * | 182 PyObject * |
194 _Py_Mangle(PyObject *privateobj, PyObject *ident) | 183 _Py_Mangle(PyObject *privateobj, PyObject *ident) |
195 { | 184 { |
196 /* Name mangling: __private becomes _classname__private. | 185 /* Name mangling: __private becomes _classname__private. |
197 This is independent from how the name is used. */ | 186 This is independent from how the name is used. */ |
198 » const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident); | 187 » const char *p, *name = PyString_AsString(ident); |
199 » Py_UNICODE *buffer; | 188 » char *buffer; |
200 size_t nlen, plen; | 189 size_t nlen, plen; |
201 » if (privateobj == NULL || !PyUnicode_Check(privateobj) || | 190 » if (privateobj == NULL || !PyString_Check(privateobj) || |
202 name == NULL || name[0] != '_' || name[1] != '_') { | 191 name == NULL || name[0] != '_' || name[1] != '_') { |
203 Py_INCREF(ident); | 192 Py_INCREF(ident); |
204 return ident; | 193 return ident; |
205 } | 194 } |
206 » p = PyUnicode_AS_UNICODE(privateobj); | 195 » p = PyString_AsString(privateobj); |
207 » nlen = Py_UNICODE_strlen(name); | 196 » nlen = strlen(name); |
208 /* Don't mangle __id__ or names with dots. | 197 /* Don't mangle __id__ or names with dots. |
209 | 198 |
210 The only time a name with a dot can occur is when | 199 The only time a name with a dot can occur is when |
211 we are compiling an import statement that has a· | 200 we are compiling an import statement that has a· |
212 package name. | 201 package name. |
213 | 202 |
214 TODO(jhylton): Decide whether we want to support | 203 TODO(jhylton): Decide whether we want to support |
215 mangling of the module name, e.g. __M.X. | 204 mangling of the module name, e.g. __M.X. |
216 */ | 205 */ |
217 if ((name[nlen-1] == '_' && name[nlen-2] == '_')· | 206 if ((name[nlen-1] == '_' && name[nlen-2] == '_')· |
218 » || Py_UNICODE_strchr(name, '.')) { | 207 » || strchr(name, '.')) { |
219 Py_INCREF(ident); | 208 Py_INCREF(ident); |
220 return ident; /* Don't mangle __whatever__ */ | 209 return ident; /* Don't mangle __whatever__ */ |
221 } | 210 } |
222 /* Strip leading underscores from class name */ | 211 /* Strip leading underscores from class name */ |
223 while (*p == '_') | 212 while (*p == '_') |
224 p++; | 213 p++; |
225 » if (*p == 0) { | 214 » if (*p == '\0') { |
226 Py_INCREF(ident); | 215 Py_INCREF(ident); |
227 return ident; /* Don't mangle if class is just underscores */ | 216 return ident; /* Don't mangle if class is just underscores */ |
228 } | 217 } |
229 » plen = Py_UNICODE_strlen(p); | 218 » plen = strlen(p); |
230 | 219 |
231 assert(1 <= PY_SSIZE_T_MAX - nlen); | 220 assert(1 <= PY_SSIZE_T_MAX - nlen); |
232 assert(1 + nlen <= PY_SSIZE_T_MAX - plen); | 221 assert(1 + nlen <= PY_SSIZE_T_MAX - plen); |
233 | 222 |
234 » ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen); | 223 » ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen); |
235 if (!ident) | 224 if (!ident) |
236 return 0; | 225 return 0; |
237 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ | 226 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
238 » buffer = PyUnicode_AS_UNICODE(ident); | 227 » buffer = PyString_AS_STRING(ident); |
239 buffer[0] = '_'; | 228 buffer[0] = '_'; |
240 » Py_UNICODE_strncpy(buffer+1, p, plen); | 229 » strncpy(buffer+1, p, plen); |
241 » Py_UNICODE_strcpy(buffer+1+plen, name); | 230 » strcpy(buffer+1+plen, name); |
242 return ident; | 231 return ident; |
243 } | 232 } |
244 | 233 |
245 static int | 234 static int |
246 compiler_init(struct compiler *c) | 235 compiler_init(struct compiler *c) |
247 { | 236 { |
248 memset(c, 0, sizeof(struct compiler)); | 237 memset(c, 0, sizeof(struct compiler)); |
249 | 238 |
250 c->c_stack = PyList_New(0); | 239 c->c_stack = PyList_New(0); |
251 if (!c->c_stack) | 240 if (!c->c_stack) |
252 return 0; | 241 return 0; |
253 | 242 |
254 return 1; | 243 return 1; |
255 } | 244 } |
256 | 245 |
257 PyCodeObject * | 246 PyCodeObject * |
258 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, | 247 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
259 PyArena *arena) | 248 PyArena *arena) |
260 { | 249 { |
261 struct compiler c; | 250 struct compiler c; |
262 PyCodeObject *co = NULL; | 251 PyCodeObject *co = NULL; |
263 PyCompilerFlags local_flags; | 252 PyCompilerFlags local_flags; |
264 int merged; | 253 int merged; |
265 | 254 |
266 if (!__doc__) { | 255 if (!__doc__) { |
267 » » __doc__ = PyUnicode_InternFromString("__doc__"); | 256 » » __doc__ = PyString_InternFromString("__doc__"); |
268 if (!__doc__) | 257 if (!__doc__) |
269 return NULL; | 258 return NULL; |
270 } | 259 } |
271 | 260 |
272 if (!compiler_init(&c)) | 261 if (!compiler_init(&c)) |
273 return NULL; | 262 return NULL; |
274 c.c_filename = filename; | 263 c.c_filename = filename; |
275 c.c_arena = arena; | 264 c.c_arena = arena; |
276 c.c_future = PyFuture_FromAST(mod, filename); | 265 c.c_future = PyFuture_FromAST(mod, filename); |
277 if (c.c_future == NULL) | 266 if (c.c_future == NULL) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 static PyObject * | 321 static PyObject * |
333 list2dict(PyObject *list) | 322 list2dict(PyObject *list) |
334 { | 323 { |
335 Py_ssize_t i, n; | 324 Py_ssize_t i, n; |
336 PyObject *v, *k; | 325 PyObject *v, *k; |
337 PyObject *dict = PyDict_New(); | 326 PyObject *dict = PyDict_New(); |
338 if (!dict) return NULL; | 327 if (!dict) return NULL; |
339 | 328 |
340 n = PyList_Size(list); | 329 n = PyList_Size(list); |
341 for (i = 0; i < n; i++) { | 330 for (i = 0; i < n; i++) { |
342 » » v = PyLong_FromLong(i); | 331 » » v = PyInt_FromLong(i); |
343 if (!v) { | 332 if (!v) { |
344 Py_DECREF(dict); | 333 Py_DECREF(dict); |
345 return NULL; | 334 return NULL; |
346 } | 335 } |
347 k = PyList_GET_ITEM(list, i); | 336 k = PyList_GET_ITEM(list, i); |
348 k = PyTuple_Pack(2, k, k->ob_type); | 337 k = PyTuple_Pack(2, k, k->ob_type); |
349 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) { | 338 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) { |
350 Py_XDECREF(k); | 339 Py_XDECREF(k); |
351 Py_DECREF(v); | 340 Py_DECREF(v); |
352 Py_DECREF(dict); | 341 Py_DECREF(dict); |
(...skipping 18 matching lines...) Expand all Loading... |
371 { | 360 { |
372 Py_ssize_t pos = 0, i = offset, scope; | 361 Py_ssize_t pos = 0, i = offset, scope; |
373 PyObject *k, *v, *dest = PyDict_New(); | 362 PyObject *k, *v, *dest = PyDict_New(); |
374 | 363 |
375 assert(offset >= 0); | 364 assert(offset >= 0); |
376 if (dest == NULL) | 365 if (dest == NULL) |
377 return NULL; | 366 return NULL; |
378 | 367 |
379 while (PyDict_Next(src, &pos, &k, &v)) { | 368 while (PyDict_Next(src, &pos, &k, &v)) { |
380 /* XXX this should probably be a macro in symtable.h */ | 369 /* XXX this should probably be a macro in symtable.h */ |
381 » » long vi; | 370 » » assert(PyInt_Check(v)); |
382 » » assert(PyLong_Check(v)); | 371 » » scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK; |
383 » » vi = PyLong_AS_LONG(v); | 372 |
384 » » scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; | 373 » » if (scope == scope_type || PyInt_AS_LONG(v) & flag) { |
385 | 374 » » » PyObject *tuple, *item = PyInt_FromLong(i); |
386 » » if (scope == scope_type || vi & flag) { | |
387 » » » PyObject *tuple, *item = PyLong_FromLong(i); | |
388 if (item == NULL) { | 375 if (item == NULL) { |
389 Py_DECREF(dest); | 376 Py_DECREF(dest); |
390 return NULL; | 377 return NULL; |
391 } | 378 } |
392 i++; | 379 i++; |
393 tuple = PyTuple_Pack(2, k, k->ob_type); | 380 tuple = PyTuple_Pack(2, k, k->ob_type); |
394 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) { | 381 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) { |
395 Py_DECREF(item); | 382 Py_DECREF(item); |
396 Py_DECREF(dest); | 383 Py_DECREF(dest); |
397 Py_XDECREF(tuple); | 384 Py_XDECREF(tuple); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 struct compiler_unit *u; | 443 struct compiler_unit *u; |
457 | 444 |
458 u = (struct compiler_unit *)PyObject_Malloc(sizeof( | 445 u = (struct compiler_unit *)PyObject_Malloc(sizeof( |
459 struct compiler_unit)); | 446 struct compiler_unit)); |
460 if (!u) { | 447 if (!u) { |
461 PyErr_NoMemory(); | 448 PyErr_NoMemory(); |
462 return 0; | 449 return 0; |
463 } | 450 } |
464 memset(u, 0, sizeof(struct compiler_unit)); | 451 memset(u, 0, sizeof(struct compiler_unit)); |
465 u->u_argcount = 0; | 452 u->u_argcount = 0; |
466 u->u_kwonlyargcount = 0; | |
467 u->u_ste = PySymtable_Lookup(c->c_st, key); | 453 u->u_ste = PySymtable_Lookup(c->c_st, key); |
468 if (!u->u_ste) { | 454 if (!u->u_ste) { |
469 compiler_unit_free(u); | 455 compiler_unit_free(u); |
470 return 0; | 456 return 0; |
471 } | 457 } |
472 Py_INCREF(name); | 458 Py_INCREF(name); |
473 u->u_name = name; | 459 u->u_name = name; |
474 u->u_varnames = list2dict(u->u_ste->ste_varnames); | 460 u->u_varnames = list2dict(u->u_ste->ste_varnames); |
475 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); | 461 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
476 if (!u->u_varnames || !u->u_cellvars) { | 462 if (!u->u_varnames || !u->u_cellvars) { |
477 compiler_unit_free(u); | 463 compiler_unit_free(u); |
478 return 0; | 464 return 0; |
479 } | 465 } |
480 | 466 |
481 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, | 467 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, |
482 PyDict_Size(u->u_cellvars)); | 468 PyDict_Size(u->u_cellvars)); |
483 if (!u->u_freevars) { | 469 if (!u->u_freevars) { |
484 compiler_unit_free(u); | 470 compiler_unit_free(u); |
485 return 0; | 471 return 0; |
486 } | 472 } |
487 | 473 |
488 u->u_blocks = NULL; | 474 u->u_blocks = NULL; |
489 u->u_tmpname = 0; | 475 u->u_tmpname = 0; |
490 u->u_nfblocks = 0; | 476 u->u_nfblocks = 0; |
491 u->u_firstlineno = lineno; | 477 u->u_firstlineno = lineno; |
492 u->u_lineno = 0; | 478 u->u_lineno = 0; |
493 » u->u_lineno_set = 0; | 479 » u->u_lineno_set = false; |
494 u->u_consts = PyDict_New(); | 480 u->u_consts = PyDict_New(); |
495 if (!u->u_consts) { | 481 if (!u->u_consts) { |
496 compiler_unit_free(u); | 482 compiler_unit_free(u); |
497 return 0; | 483 return 0; |
498 } | 484 } |
499 u->u_names = PyDict_New(); | 485 u->u_names = PyDict_New(); |
500 if (!u->u_names) { | 486 if (!u->u_names) { |
501 compiler_unit_free(u); | 487 compiler_unit_free(u); |
502 return 0; | 488 return 0; |
503 } | 489 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 | 537 |
552 /* Allocate a new "anonymous" local variable. | 538 /* Allocate a new "anonymous" local variable. |
553 Used by list comprehensions and with statements. | 539 Used by list comprehensions and with statements. |
554 */ | 540 */ |
555 | 541 |
556 static PyObject * | 542 static PyObject * |
557 compiler_new_tmpname(struct compiler *c) | 543 compiler_new_tmpname(struct compiler *c) |
558 { | 544 { |
559 char tmpname[256]; | 545 char tmpname[256]; |
560 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname); | 546 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname); |
561 » return PyUnicode_FromString(tmpname); | 547 » return PyString_FromString(tmpname); |
562 } | 548 } |
563 | 549 |
564 /* Allocate a new block and return a pointer to it. | 550 /* Allocate a new block and return a pointer to it. |
565 Returns NULL on error. | 551 Returns NULL on error. |
566 */ | 552 */ |
567 | 553 |
568 static basicblock * | 554 static basicblock * |
569 compiler_new_block(struct compiler *c) | 555 compiler_new_block(struct compiler *c) |
570 { | 556 { |
571 basicblock *b; | 557 basicblock *b; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 - before the "except" clause | 659 - before the "except" clause |
674 - before the "for" and "while" expressions | 660 - before the "for" and "while" expressions |
675 */ | 661 */ |
676 | 662 |
677 static void | 663 static void |
678 compiler_set_lineno(struct compiler *c, int off) | 664 compiler_set_lineno(struct compiler *c, int off) |
679 { | 665 { |
680 basicblock *b; | 666 basicblock *b; |
681 if (c->u->u_lineno_set) | 667 if (c->u->u_lineno_set) |
682 return; | 668 return; |
683 » c->u->u_lineno_set = 1; | 669 » c->u->u_lineno_set = true; |
684 b = c->u->u_curblock; | 670 b = c->u->u_curblock; |
685 b->b_instr[off].i_lineno = c->u->u_lineno; | 671 b->b_instr[off].i_lineno = c->u->u_lineno; |
686 } | 672 } |
687 | 673 |
688 static int | 674 static int |
689 opcode_stack_effect(int opcode, int oparg) | 675 opcode_stack_effect(int opcode, int oparg) |
690 { | 676 { |
691 switch (opcode) { | 677 switch (opcode) { |
692 case POP_TOP: | 678 case POP_TOP: |
693 return -1; | 679 return -1; |
694 case ROT_TWO: | 680 case ROT_TWO: |
695 case ROT_THREE: | 681 case ROT_THREE: |
696 return 0; | 682 return 0; |
697 case DUP_TOP: | 683 case DUP_TOP: |
698 return 1; | 684 return 1; |
699 case ROT_FOUR: | 685 case ROT_FOUR: |
700 return 0; | 686 return 0; |
701 | 687 |
702 case UNARY_POSITIVE: | 688 case UNARY_POSITIVE: |
703 case UNARY_NEGATIVE: | 689 case UNARY_NEGATIVE: |
704 case UNARY_NOT: | 690 case UNARY_NOT: |
| 691 case UNARY_CONVERT: |
705 case UNARY_INVERT: | 692 case UNARY_INVERT: |
706 return 0; | 693 return 0; |
707 | 694 |
708 case SET_ADD: | |
709 case LIST_APPEND: | 695 case LIST_APPEND: |
710 return -1; | 696 return -1; |
711 case MAP_ADD: | |
712 return -2; | |
713 | 697 |
714 case BINARY_POWER: | 698 case BINARY_POWER: |
715 case BINARY_MULTIPLY: | 699 case BINARY_MULTIPLY: |
| 700 case BINARY_DIVIDE: |
716 case BINARY_MODULO: | 701 case BINARY_MODULO: |
717 case BINARY_ADD: | 702 case BINARY_ADD: |
718 case BINARY_SUBTRACT: | 703 case BINARY_SUBTRACT: |
719 case BINARY_SUBSCR: | 704 case BINARY_SUBSCR: |
720 case BINARY_FLOOR_DIVIDE: | 705 case BINARY_FLOOR_DIVIDE: |
721 case BINARY_TRUE_DIVIDE: | 706 case BINARY_TRUE_DIVIDE: |
722 return -1; | 707 return -1; |
723 case INPLACE_FLOOR_DIVIDE: | 708 case INPLACE_FLOOR_DIVIDE: |
724 case INPLACE_TRUE_DIVIDE: | 709 case INPLACE_TRUE_DIVIDE: |
725 return -1; | 710 return -1; |
726 | 711 |
| 712 case SLICE+0: |
| 713 return 1; |
| 714 case SLICE+1: |
| 715 return 0; |
| 716 case SLICE+2: |
| 717 return 0; |
| 718 case SLICE+3: |
| 719 return -1; |
| 720 |
| 721 case STORE_SLICE+0: |
| 722 return -2; |
| 723 case STORE_SLICE+1: |
| 724 return -3; |
| 725 case STORE_SLICE+2: |
| 726 return -3; |
| 727 case STORE_SLICE+3: |
| 728 return -4; |
| 729 |
| 730 case DELETE_SLICE+0: |
| 731 return -1; |
| 732 case DELETE_SLICE+1: |
| 733 return -2; |
| 734 case DELETE_SLICE+2: |
| 735 return -2; |
| 736 case DELETE_SLICE+3: |
| 737 return -3; |
| 738 |
727 case INPLACE_ADD: | 739 case INPLACE_ADD: |
728 case INPLACE_SUBTRACT: | 740 case INPLACE_SUBTRACT: |
729 case INPLACE_MULTIPLY: | 741 case INPLACE_MULTIPLY: |
| 742 case INPLACE_DIVIDE: |
730 case INPLACE_MODULO: | 743 case INPLACE_MODULO: |
731 return -1; | 744 return -1; |
732 case STORE_SUBSCR: | 745 case STORE_SUBSCR: |
733 return -3; | 746 return -3; |
734 case STORE_MAP: | 747 case STORE_MAP: |
735 return -2; | 748 return -2; |
736 case DELETE_SUBSCR: | 749 case DELETE_SUBSCR: |
737 return -2; | 750 return -2; |
738 | 751 |
739 case BINARY_LSHIFT: | 752 case BINARY_LSHIFT: |
740 case BINARY_RSHIFT: | 753 case BINARY_RSHIFT: |
741 case BINARY_AND: | 754 case BINARY_AND: |
742 case BINARY_XOR: | 755 case BINARY_XOR: |
743 case BINARY_OR: | 756 case BINARY_OR: |
744 return -1; | 757 return -1; |
745 case INPLACE_POWER: | 758 case INPLACE_POWER: |
746 return -1; | 759 return -1; |
747 case GET_ITER: | 760 case GET_ITER: |
748 return 0; | 761 return 0; |
749 | 762 |
750 case PRINT_EXPR: | 763 case PRINT_EXPR: |
751 return -1; | 764 return -1; |
752 » » case LOAD_BUILD_CLASS: | 765 » » case PRINT_ITEM: |
753 » » » return 1; | 766 » » » return -1; |
| 767 » » case PRINT_NEWLINE: |
| 768 » » » return 0; |
| 769 » » case PRINT_ITEM_TO: |
| 770 » » » return -2; |
| 771 » » case PRINT_NEWLINE_TO: |
| 772 » » » return -1; |
754 case INPLACE_LSHIFT: | 773 case INPLACE_LSHIFT: |
755 case INPLACE_RSHIFT: | 774 case INPLACE_RSHIFT: |
756 case INPLACE_AND: | 775 case INPLACE_AND: |
757 case INPLACE_XOR: | 776 case INPLACE_XOR: |
758 case INPLACE_OR: | 777 case INPLACE_OR: |
759 return -1; | 778 return -1; |
760 case BREAK_LOOP: | 779 case BREAK_LOOP: |
761 return 0; | 780 return 0; |
762 case WITH_CLEANUP: | 781 case WITH_CLEANUP: |
763 return -1; /* XXX Sometimes more */ | 782 return -1; /* XXX Sometimes more */ |
764 » » case STORE_LOCALS: | 783 » » case LOAD_LOCALS: |
765 » » » return -1; | 784 » » » return 1; |
766 case RETURN_VALUE: | 785 case RETURN_VALUE: |
767 return -1; | 786 return -1; |
768 case IMPORT_STAR: | 787 case IMPORT_STAR: |
769 return -1; | 788 return -1; |
| 789 case EXEC_STMT: |
| 790 return -3; |
770 case YIELD_VALUE: | 791 case YIELD_VALUE: |
771 return 0; | 792 return 0; |
772 | 793 |
773 case POP_BLOCK: | 794 case POP_BLOCK: |
774 return 0; | 795 return 0; |
775 case POP_EXCEPT: | |
776 return 0; /* -3 except if bad bytecode */ | |
777 case END_FINALLY: | 796 case END_FINALLY: |
778 return -1; /* or -2 or -3 if exception occurred */ | 797 return -1; /* or -2 or -3 if exception occurred */ |
| 798 case BUILD_CLASS: |
| 799 return -2; |
779 | 800 |
780 case STORE_NAME: | 801 case STORE_NAME: |
781 return -1; | 802 return -1; |
782 case DELETE_NAME: | 803 case DELETE_NAME: |
783 return 0; | 804 return 0; |
784 case UNPACK_SEQUENCE: | 805 case UNPACK_SEQUENCE: |
785 return oparg-1; | 806 return oparg-1; |
786 case UNPACK_EX: | |
787 return (oparg&0xFF) + (oparg>>8); | |
788 case FOR_ITER: | 807 case FOR_ITER: |
789 return 1; | 808 return 1; |
790 | 809 |
791 case STORE_ATTR: | 810 case STORE_ATTR: |
792 return -2; | 811 return -2; |
793 case DELETE_ATTR: | 812 case DELETE_ATTR: |
794 return -1; | 813 return -1; |
795 case STORE_GLOBAL: | 814 case STORE_GLOBAL: |
796 return -1; | 815 return -1; |
797 case DELETE_GLOBAL: | 816 case DELETE_GLOBAL: |
798 return 0; | 817 return 0; |
799 case DUP_TOPX: | 818 case DUP_TOPX: |
800 return oparg; | 819 return oparg; |
801 case LOAD_CONST: | 820 case LOAD_CONST: |
802 return 1; | 821 return 1; |
803 case LOAD_NAME: | 822 case LOAD_NAME: |
804 return 1; | 823 return 1; |
805 case BUILD_TUPLE: | 824 case BUILD_TUPLE: |
806 case BUILD_LIST: | 825 case BUILD_LIST: |
807 case BUILD_SET: | |
808 return 1-oparg; | 826 return 1-oparg; |
809 case BUILD_MAP: | 827 case BUILD_MAP: |
810 return 1; | 828 return 1; |
811 case LOAD_ATTR: | 829 case LOAD_ATTR: |
812 return 0; | 830 return 0; |
813 case COMPARE_OP: | 831 case COMPARE_OP: |
814 return -1; | 832 return -1; |
815 case IMPORT_NAME: | 833 case IMPORT_NAME: |
816 return 0; | 834 return 0; |
817 case IMPORT_FROM: | 835 case IMPORT_FROM: |
(...skipping 11 matching lines...) Expand all Loading... |
829 | 847 |
830 case LOAD_GLOBAL: | 848 case LOAD_GLOBAL: |
831 return 1; | 849 return 1; |
832 | 850 |
833 case CONTINUE_LOOP: | 851 case CONTINUE_LOOP: |
834 return 0; | 852 return 0; |
835 case SETUP_LOOP: | 853 case SETUP_LOOP: |
836 return 0; | 854 return 0; |
837 case SETUP_EXCEPT: | 855 case SETUP_EXCEPT: |
838 case SETUP_FINALLY: | 856 case SETUP_FINALLY: |
839 » » » return 6; /* can push 3 values for the new exception | 857 » » » return 3; /* actually pushed by an exception */ |
840 » » » » + 3 others for the previous exception state */ | |
841 | 858 |
842 case LOAD_FAST: | 859 case LOAD_FAST: |
843 return 1; | 860 return 1; |
844 case STORE_FAST: | 861 case STORE_FAST: |
845 return -1; | 862 return -1; |
846 case DELETE_FAST: | 863 case DELETE_FAST: |
847 return 0; | 864 return 0; |
848 | 865 |
849 case RAISE_VARARGS: | 866 case RAISE_VARARGS: |
850 return -oparg; | 867 return -oparg; |
851 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256)) | 868 #define NARGS(o) (((o) % 256) + 2*((o) / 256)) |
852 case CALL_FUNCTION: | 869 case CALL_FUNCTION: |
853 return -NARGS(oparg); | 870 return -NARGS(oparg); |
854 case CALL_FUNCTION_VAR: | 871 case CALL_FUNCTION_VAR: |
855 case CALL_FUNCTION_KW: | 872 case CALL_FUNCTION_KW: |
856 return -NARGS(oparg)-1; | 873 return -NARGS(oparg)-1; |
857 case CALL_FUNCTION_VAR_KW: | 874 case CALL_FUNCTION_VAR_KW: |
858 return -NARGS(oparg)-2; | 875 return -NARGS(oparg)-2; |
| 876 #undef NARGS |
859 case MAKE_FUNCTION: | 877 case MAKE_FUNCTION: |
860 » » » return -NARGS(oparg) - ((oparg >> 16) & 0xffff); | 878 » » » return -oparg; |
861 » » case MAKE_CLOSURE: | |
862 » » » return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff); | |
863 #undef NARGS | |
864 case BUILD_SLICE: | 879 case BUILD_SLICE: |
865 if (oparg == 3) | 880 if (oparg == 3) |
866 return -2; | 881 return -2; |
867 else | 882 else |
868 return -1; | 883 return -1; |
869 | 884 |
| 885 case MAKE_CLOSURE: |
| 886 return -oparg; |
870 case LOAD_CLOSURE: | 887 case LOAD_CLOSURE: |
871 return 1; | 888 return 1; |
872 case LOAD_DEREF: | 889 case LOAD_DEREF: |
873 return 1; | 890 return 1; |
874 case STORE_DEREF: | 891 case STORE_DEREF: |
875 return -1; | 892 return -1; |
876 default: | 893 default: |
877 fprintf(stderr, "opcode = %d\n", opcode); | 894 fprintf(stderr, "opcode = %d\n", opcode); |
878 Py_FatalError("opcode_stack_effect()"); | 895 Py_FatalError("opcode_stack_effect()"); |
879 | 896 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 } | 975 } |
959 } | 976 } |
960 else { | 977 else { |
961 t = PyTuple_Pack(2, o, o->ob_type); | 978 t = PyTuple_Pack(2, o, o->ob_type); |
962 } | 979 } |
963 if (t == NULL) | 980 if (t == NULL) |
964 return -1; | 981 return -1; |
965 | 982 |
966 v = PyDict_GetItem(dict, t); | 983 v = PyDict_GetItem(dict, t); |
967 if (!v) { | 984 if (!v) { |
968 if (PyErr_Occurred()) | |
969 return -1; | |
970 arg = PyDict_Size(dict); | 985 arg = PyDict_Size(dict); |
971 » » v = PyLong_FromLong(arg); | 986 » » v = PyInt_FromLong(arg); |
972 if (!v) { | 987 if (!v) { |
973 Py_DECREF(t); | 988 Py_DECREF(t); |
974 return -1; | 989 return -1; |
975 } | 990 } |
976 if (PyDict_SetItem(dict, t, v) < 0) { | 991 if (PyDict_SetItem(dict, t, v) < 0) { |
977 Py_DECREF(t); | 992 Py_DECREF(t); |
978 Py_DECREF(v); | 993 Py_DECREF(v); |
979 return -1; | 994 return -1; |
980 } | 995 } |
981 Py_DECREF(v); | 996 Py_DECREF(v); |
982 } | 997 } |
983 else | 998 else |
984 » » arg = PyLong_AsLong(v); | 999 » » arg = PyInt_AsLong(v); |
985 Py_DECREF(t); | 1000 Py_DECREF(t); |
986 return arg; | 1001 return arg; |
987 } | 1002 } |
988 | 1003 |
989 static int | 1004 static int |
990 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, | 1005 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
991 PyObject *o) | 1006 PyObject *o) |
992 { | 1007 { |
993 int arg = compiler_add_o(c, dict, o); | 1008 int arg = compiler_add_o(c, dict, o); |
994 if (arg < 0) | 1009 if (arg < 0) |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1185 return 1; | 1200 return 1; |
1186 } | 1201 } |
1187 | 1202 |
1188 static PyCodeObject * | 1203 static PyCodeObject * |
1189 compiler_mod(struct compiler *c, mod_ty mod) | 1204 compiler_mod(struct compiler *c, mod_ty mod) |
1190 { | 1205 { |
1191 PyCodeObject *co; | 1206 PyCodeObject *co; |
1192 int addNone = 1; | 1207 int addNone = 1; |
1193 static PyObject *module; | 1208 static PyObject *module; |
1194 if (!module) { | 1209 if (!module) { |
1195 » » module = PyUnicode_InternFromString("<module>"); | 1210 » » module = PyString_InternFromString("<module>"); |
1196 if (!module) | 1211 if (!module) |
1197 return NULL; | 1212 return NULL; |
1198 } | 1213 } |
1199 /* Use 0 for firstlineno initially, will fixup in assemble(). */ | 1214 /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
1200 if (!compiler_enter_scope(c, module, mod, 0)) | 1215 if (!compiler_enter_scope(c, module, mod, 0)) |
1201 return NULL; | 1216 return NULL; |
1202 switch (mod->kind) { | 1217 switch (mod->kind) { |
1203 case Module_kind:· | 1218 case Module_kind:· |
1204 if (!compiler_body(c, mod->v.Module.body)) { | 1219 if (!compiler_body(c, mod->v.Module.body)) { |
1205 compiler_exit_scope(c); | 1220 compiler_exit_scope(c); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1237 | 1252 |
1238 static int | 1253 static int |
1239 get_ref_type(struct compiler *c, PyObject *name) | 1254 get_ref_type(struct compiler *c, PyObject *name) |
1240 { | 1255 { |
1241 int scope = PyST_GetScope(c->u->u_ste, name); | 1256 int scope = PyST_GetScope(c->u->u_ste, name); |
1242 if (scope == 0) { | 1257 if (scope == 0) { |
1243 char buf[350]; | 1258 char buf[350]; |
1244 PyOS_snprintf(buf, sizeof(buf), | 1259 PyOS_snprintf(buf, sizeof(buf), |
1245 "unknown scope for %.100s in %.100s(%s) in %s\n" | 1260 "unknown scope for %.100s in %.100s(%s) in %s\n" |
1246 "symbols: %s\nlocals: %s\nglobals: %s", | 1261 "symbols: %s\nlocals: %s\nglobals: %s", |
1247 » » » PyBytes_AS_STRING(name),· | 1262 » » » PyString_AS_STRING(name),· |
1248 » » » PyBytes_AS_STRING(c->u->u_name),· | 1263 » » » PyString_AS_STRING(c->u->u_name),· |
1249 PyObject_REPR(c->u->u_ste->ste_id), | 1264 PyObject_REPR(c->u->u_ste->ste_id), |
1250 c->c_filename, | 1265 c->c_filename, |
1251 PyObject_REPR(c->u->u_ste->ste_symbols), | 1266 PyObject_REPR(c->u->u_ste->ste_symbols), |
1252 PyObject_REPR(c->u->u_varnames), | 1267 PyObject_REPR(c->u->u_varnames), |
1253 PyObject_REPR(c->u->u_names) | 1268 PyObject_REPR(c->u->u_names) |
1254 ); | 1269 ); |
1255 Py_FatalError(buf); | 1270 Py_FatalError(buf); |
1256 } | 1271 } |
1257 | 1272 |
1258 return scope; | 1273 return scope; |
1259 } | 1274 } |
1260 | 1275 |
1261 static int | 1276 static int |
1262 compiler_lookup_arg(PyObject *dict, PyObject *name) | 1277 compiler_lookup_arg(PyObject *dict, PyObject *name) |
1263 { | 1278 { |
1264 PyObject *k, *v; | 1279 PyObject *k, *v; |
1265 k = PyTuple_Pack(2, name, name->ob_type); | 1280 k = PyTuple_Pack(2, name, name->ob_type); |
1266 if (k == NULL) | 1281 if (k == NULL) |
1267 return -1; | 1282 return -1; |
1268 v = PyDict_GetItem(dict, k); | 1283 v = PyDict_GetItem(dict, k); |
1269 Py_DECREF(k); | 1284 Py_DECREF(k); |
1270 if (v == NULL) | 1285 if (v == NULL) |
1271 return -1; | 1286 return -1; |
1272 return PyLong_AS_LONG(v); | 1287 return PyInt_AS_LONG(v); |
1273 } | 1288 } |
1274 | 1289 |
1275 static int | 1290 static int |
1276 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args) | 1291 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args) |
1277 { | 1292 { |
1278 int i, free = PyCode_GetNumFree(co); | 1293 int i, free = PyCode_GetNumFree(co); |
1279 if (free == 0) { | 1294 if (free == 0) { |
1280 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); | 1295 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); |
1281 ADDOP_I(c, MAKE_FUNCTION, args); | 1296 ADDOP_I(c, MAKE_FUNCTION, args); |
1282 return 1; | 1297 return 1; |
(...skipping 10 matching lines...) Expand all Loading... |
1293 the name will be considered free *and* local in the | 1308 the name will be considered free *and* local in the |
1294 class. It should be handled by the closure, as | 1309 class. It should be handled by the closure, as |
1295 well as by the normal name loookup logic. | 1310 well as by the normal name loookup logic. |
1296 */ | 1311 */ |
1297 reftype = get_ref_type(c, name); | 1312 reftype = get_ref_type(c, name); |
1298 if (reftype == CELL) | 1313 if (reftype == CELL) |
1299 arg = compiler_lookup_arg(c->u->u_cellvars, name); | 1314 arg = compiler_lookup_arg(c->u->u_cellvars, name); |
1300 else /* (reftype == FREE) */ | 1315 else /* (reftype == FREE) */ |
1301 arg = compiler_lookup_arg(c->u->u_freevars, name); | 1316 arg = compiler_lookup_arg(c->u->u_freevars, name); |
1302 if (arg == -1) { | 1317 if (arg == -1) { |
1303 » » » fprintf(stderr, | 1318 » » » printf("lookup %s in %s %d %d\n" |
1304 » » » » "lookup %s in %s %d %d\n" | |
1305 "freevars of %s: %s\n", | 1319 "freevars of %s: %s\n", |
1306 PyObject_REPR(name),· | 1320 PyObject_REPR(name),· |
1307 » » » » PyBytes_AS_STRING(c->u->u_name),· | 1321 » » » » PyString_AS_STRING(c->u->u_name),· |
1308 reftype, arg, | 1322 reftype, arg, |
1309 » » » » _PyUnicode_AsString(co->co_name), | 1323 » » » » PyString_AS_STRING(co->co_name), |
1310 PyObject_REPR(co->co_freevars)); | 1324 PyObject_REPR(co->co_freevars)); |
1311 Py_FatalError("compiler_make_closure()"); | 1325 Py_FatalError("compiler_make_closure()"); |
1312 } | 1326 } |
1313 ADDOP_I(c, LOAD_CLOSURE, arg); | 1327 ADDOP_I(c, LOAD_CLOSURE, arg); |
1314 } | 1328 } |
1315 ADDOP_I(c, BUILD_TUPLE, free); | 1329 ADDOP_I(c, BUILD_TUPLE, free); |
1316 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); | 1330 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); |
1317 ADDOP_I(c, MAKE_CLOSURE, args); | 1331 ADDOP_I(c, MAKE_CLOSURE, args); |
1318 return 1; | 1332 return 1; |
1319 } | 1333 } |
1320 | 1334 |
1321 static int | 1335 static int |
1322 compiler_decorators(struct compiler *c, asdl_seq* decos) | 1336 compiler_decorators(struct compiler *c, asdl_seq* decos) |
1323 { | 1337 { |
1324 int i; | 1338 int i; |
1325 | 1339 |
1326 if (!decos) | 1340 if (!decos) |
1327 return 1; | 1341 return 1; |
1328 | 1342 |
1329 for (i = 0; i < asdl_seq_LEN(decos); i++) { | 1343 for (i = 0; i < asdl_seq_LEN(decos); i++) { |
1330 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); | 1344 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
1331 } | 1345 } |
1332 return 1; | 1346 return 1; |
1333 } | 1347 } |
1334 | 1348 |
1335 static int | 1349 static int |
1336 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, | 1350 compiler_arguments(struct compiler *c, arguments_ty args) |
1337 » asdl_seq *kw_defaults) | 1351 { |
1338 { | 1352 » int i; |
1339 » int i, default_count = 0; | 1353 » int n = asdl_seq_LEN(args->args); |
1340 » for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { | 1354 » /* Correctly handle nested argument lists */ |
1341 » » arg_ty arg = asdl_seq_GET(kwonlyargs, i); | 1355 » for (i = 0; i < n; i++) { |
1342 » » expr_ty default_ = asdl_seq_GET(kw_defaults, i); | 1356 » » expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i); |
1343 » » if (default_) { | 1357 » » if (arg->kind == Tuple_kind) { |
1344 » » » ADDOP_O(c, LOAD_CONST, arg->arg, consts); | 1358 » » » PyObject *id = PyString_FromFormat(".%d", i); |
1345 » » » if (!compiler_visit_expr(c, default_)) { | 1359 » » » if (id == NULL) { |
1346 » » » return -1; | 1360 » » » » return 0; |
1347 } | 1361 } |
1348 » » » default_count++; | 1362 » » » if (!compiler_nameop(c, id, Load)) { |
1349 » » } | 1363 » » » » Py_DECREF(id); |
1350 » } | 1364 » » » » return 0; |
1351 » return default_count; | 1365 » » » } |
1352 } | 1366 » » » Py_DECREF(id); |
1353 | 1367 » » » VISIT(c, expr, arg); |
1354 static int | 1368 » » } |
1355 compiler_visit_argannotation(struct compiler *c, identifier id, | 1369 » } |
1356 expr_ty annotation, PyObject *names) | 1370 » return 1; |
1357 { | |
1358 » if (annotation) { | |
1359 » » VISIT(c, expr, annotation); | |
1360 » » if (PyList_Append(names, id)) | |
1361 » » » return -1; | |
1362 » } | |
1363 » return 0; | |
1364 } | |
1365 | |
1366 static int | |
1367 compiler_visit_argannotations(struct compiler *c, asdl_seq* args, | |
1368 PyObject *names) | |
1369 { | |
1370 » int i, error; | |
1371 » for (i = 0; i < asdl_seq_LEN(args); i++) { | |
1372 » » arg_ty arg = (arg_ty)asdl_seq_GET(args, i); | |
1373 » » error = compiler_visit_argannotation( | |
1374 » » » » c, | |
1375 » » » arg->arg, | |
1376 » » » arg->annotation, | |
1377 » » » names); | |
1378 » » if (error) | |
1379 » » » return error; | |
1380 » } | |
1381 » return 0; | |
1382 } | |
1383 | |
1384 static int | |
1385 compiler_visit_annotations(struct compiler *c, arguments_ty args, | |
1386 expr_ty returns) | |
1387 { | |
1388 » /* Push arg annotations and a list of the argument names. Return the # | |
1389 » of items pushed. The expressions are evaluated out-of-order wrt the· | |
1390 » source code.· | |
1391 »·········· | |
1392 » More than 2^16-1 annotations is a SyntaxError. Returns -1 on error. | |
1393 » */ | |
1394 » static identifier return_str; | |
1395 » PyObject *names; | |
1396 » int len; | |
1397 » names = PyList_New(0); | |
1398 » if (!names) | |
1399 » » return -1; | |
1400 | |
1401 » if (compiler_visit_argannotations(c, args->args, names)) | |
1402 » » goto error; | |
1403 » if (args->varargannotation && | |
1404 » compiler_visit_argannotation(c, args->vararg, | |
1405 » args->varargannotation, names)) | |
1406 » » goto error; | |
1407 » if (compiler_visit_argannotations(c, args->kwonlyargs, names)) | |
1408 » » goto error; | |
1409 » if (args->kwargannotation && | |
1410 » compiler_visit_argannotation(c, args->kwarg, | |
1411 » args->kwargannotation, names)) | |
1412 » » goto error; | |
1413 | |
1414 » if (!return_str) { | |
1415 » » return_str = PyUnicode_InternFromString("return"); | |
1416 » » if (!return_str) | |
1417 » » » goto error; | |
1418 » } | |
1419 » if (compiler_visit_argannotation(c, return_str, returns, names)) { | |
1420 » » goto error; | |
1421 » } | |
1422 | |
1423 » len = PyList_GET_SIZE(names); | |
1424 » if (len > 65534) { | |
1425 » » /* len must fit in 16 bits, and len is incremented below */ | |
1426 » » PyErr_SetString(PyExc_SyntaxError, | |
1427 » » » » "too many annotations"); | |
1428 » » goto error; | |
1429 » }»······ | |
1430 » if (len) { | |
1431 » » /* convert names to a tuple and place on stack */ | |
1432 » » PyObject *elt; | |
1433 » » int i; | |
1434 » » PyObject *s = PyTuple_New(len); | |
1435 » » if (!s) | |
1436 » » » goto error; | |
1437 » » for (i = 0; i < len; i++) { | |
1438 » » » elt = PyList_GET_ITEM(names, i); | |
1439 » » » Py_INCREF(elt); | |
1440 » » » PyTuple_SET_ITEM(s, i, elt); | |
1441 » » } | |
1442 » » ADDOP_O(c, LOAD_CONST, s, consts); | |
1443 » » Py_DECREF(s); | |
1444 » » len++; /* include the just-pushed tuple */ | |
1445 » } | |
1446 » Py_DECREF(names); | |
1447 » return len; | |
1448 | |
1449 error: | |
1450 » Py_DECREF(names); | |
1451 » return -1; | |
1452 } | 1371 } |
1453 | 1372 |
1454 static int | 1373 static int |
1455 compiler_function(struct compiler *c, stmt_ty s) | 1374 compiler_function(struct compiler *c, stmt_ty s) |
1456 { | 1375 { |
1457 PyCodeObject *co; | 1376 PyCodeObject *co; |
1458 PyObject *first_const = Py_None; | 1377 PyObject *first_const = Py_None; |
1459 arguments_ty args = s->v.FunctionDef.args; | 1378 arguments_ty args = s->v.FunctionDef.args; |
1460 expr_ty returns = s->v.FunctionDef.returns; | |
1461 asdl_seq* decos = s->v.FunctionDef.decorator_list; | 1379 asdl_seq* decos = s->v.FunctionDef.decorator_list; |
1462 stmt_ty st; | 1380 stmt_ty st; |
1463 » int i, n, docstring, kw_default_count = 0, arglength; | 1381 » int i, n, docstring; |
1464 » int num_annotations; | |
1465 | 1382 |
1466 assert(s->kind == FunctionDef_kind); | 1383 assert(s->kind == FunctionDef_kind); |
1467 | 1384 |
1468 if (!compiler_decorators(c, decos)) | 1385 if (!compiler_decorators(c, decos)) |
1469 return 0; | 1386 return 0; |
1470 if (args->kwonlyargs) { | |
1471 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, | |
1472 args->kw_defaults); | |
1473 if (res < 0) | |
1474 return 0; | |
1475 kw_default_count = res; | |
1476 } | |
1477 if (args->defaults) | 1387 if (args->defaults) |
1478 VISIT_SEQ(c, expr, args->defaults); | 1388 VISIT_SEQ(c, expr, args->defaults); |
1479 num_annotations = compiler_visit_annotations(c, args, returns); | |
1480 if (num_annotations < 0) | |
1481 return 0; | |
1482 assert((num_annotations & 0xFFFF) == num_annotations); | |
1483 | |
1484 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s, | 1389 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s, |
1485 s->lineno)) | 1390 s->lineno)) |
1486 return 0; | 1391 return 0; |
1487 | 1392 |
1488 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); | 1393 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); |
1489 docstring = compiler_isdocstring(st); | 1394 docstring = compiler_isdocstring(st); |
1490 if (docstring && Py_OptimizeFlag < 2) | 1395 if (docstring && Py_OptimizeFlag < 2) |
1491 first_const = st->v.Expr.value->v.Str.s; | 1396 first_const = st->v.Expr.value->v.Str.s; |
1492 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { | 1397 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { |
1493 compiler_exit_scope(c); | 1398 compiler_exit_scope(c); |
1494 return 0; | 1399 return 0; |
1495 } | 1400 } |
1496 | 1401 |
| 1402 /* unpack nested arguments */ |
| 1403 compiler_arguments(c, args); |
| 1404 |
1497 c->u->u_argcount = asdl_seq_LEN(args->args); | 1405 c->u->u_argcount = asdl_seq_LEN(args->args); |
1498 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); | |
1499 n = asdl_seq_LEN(s->v.FunctionDef.body); | 1406 n = asdl_seq_LEN(s->v.FunctionDef.body); |
1500 /* if there was a docstring, we need to skip the first statement */ | 1407 /* if there was a docstring, we need to skip the first statement */ |
1501 for (i = docstring; i < n; i++) { | 1408 for (i = docstring; i < n; i++) { |
1502 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); | 1409 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); |
1503 VISIT_IN_SCOPE(c, stmt, st); | 1410 VISIT_IN_SCOPE(c, stmt, st); |
1504 } | 1411 } |
1505 co = assemble(c, 1); | 1412 co = assemble(c, 1); |
1506 compiler_exit_scope(c); | 1413 compiler_exit_scope(c); |
1507 if (co == NULL) | 1414 if (co == NULL) |
1508 return 0; | 1415 return 0; |
1509 | 1416 |
1510 » arglength = asdl_seq_LEN(args->defaults); | 1417 » compiler_make_closure(c, co, asdl_seq_LEN(args->defaults)); |
1511 » arglength |= kw_default_count << 8; | |
1512 » arglength |= num_annotations << 16; | |
1513 » compiler_make_closure(c, co, arglength); | |
1514 Py_DECREF(co); | 1418 Py_DECREF(co); |
1515 | 1419 |
1516 /* decorators */ | |
1517 for (i = 0; i < asdl_seq_LEN(decos); i++) { | 1420 for (i = 0; i < asdl_seq_LEN(decos); i++) { |
1518 ADDOP_I(c, CALL_FUNCTION, 1); | 1421 ADDOP_I(c, CALL_FUNCTION, 1); |
1519 } | 1422 } |
1520 | 1423 |
1521 return compiler_nameop(c, s->v.FunctionDef.name, Store); | 1424 return compiler_nameop(c, s->v.FunctionDef.name, Store); |
1522 } | 1425 } |
1523 | 1426 |
1524 static int | 1427 static int |
1525 compiler_class(struct compiler *c, stmt_ty s) | 1428 compiler_class(struct compiler *c, stmt_ty s) |
1526 { | 1429 { |
1527 » static PyObject *locals = NULL; | 1430 » int n, i; |
1528 PyCodeObject *co; | 1431 PyCodeObject *co; |
1529 PyObject *str; | 1432 PyObject *str; |
1530 PySTEntryObject *ste; | |
1531 int err, i; | |
1532 asdl_seq* decos = s->v.ClassDef.decorator_list; | 1433 asdl_seq* decos = s->v.ClassDef.decorator_list; |
1533 | 1434 »······· |
1534 if (!compiler_decorators(c, decos)) | 1435 » if (!compiler_decorators(c, decos)) |
1535 return 0; | 1436 » » return 0; |
1536 | 1437 |
1537 » /* initialize statics */ | 1438 » /* push class name on stack, needed by BUILD_CLASS */ |
1538 » if (locals == NULL) { | 1439 » ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); |
1539 » » locals = PyUnicode_InternFromString("__locals__"); | 1440 » /* push the tuple of base classes on the stack */ |
1540 » » if (locals == NULL) | 1441 » n = asdl_seq_LEN(s->v.ClassDef.bases); |
1541 » » » return 0; | 1442 » if (n > 0) |
1542 » } | 1443 » » VISIT_SEQ(c, expr, s->v.ClassDef.bases); |
1543 | 1444 » ADDOP_I(c, BUILD_TUPLE, n); |
1544 » /* ultimately generate code for: | 1445 » if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, |
1545 » <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) | 1446 » » » » s->lineno)) |
1546 » where: | 1447 » » return 0; |
1547 » <func> is a function/closure created from the class body; | 1448 » Py_XDECREF(c->u->u_private); |
1548 it has a single argument (__locals__) where the dict | 1449 » c->u->u_private = s->v.ClassDef.name; |
1549 » » (or MutableSequence) representing the locals is passed | 1450 » Py_INCREF(c->u->u_private); |
1550 » <name> is the class name | 1451 » str = PyString_InternFromString("__name__"); |
1551 <bases> is the positional arguments and *varargs argument | 1452 » if (!str || !compiler_nameop(c, str, Load)) { |
1552 » <keywords> is the keyword arguments and **kwds argument | 1453 » » Py_XDECREF(str); |
1553 » This borrows from compiler_call. | 1454 » » compiler_exit_scope(c); |
1554 » */ | 1455 » » return 0; |
1555 | 1456 » } |
1556 » /* 0. Create a fake argument named __locals__ */ | 1457 »······· |
1557 » ste = PySymtable_Lookup(c->c_st, s); | 1458 » Py_DECREF(str); |
1558 » if (ste == NULL) | 1459 » str = PyString_InternFromString("__module__"); |
1559 » » return 0; | 1460 » if (!str || !compiler_nameop(c, str, Store)) { |
1560 » assert(PyList_Check(ste->ste_varnames)); | 1461 » » Py_XDECREF(str); |
1561 » err = PyList_Append(ste->ste_varnames, locals); | 1462 » » compiler_exit_scope(c); |
1562 » Py_DECREF(ste); | 1463 » » return 0; |
1563 » if (err < 0) | 1464 » } |
1564 » » return 0; | 1465 » Py_DECREF(str); |
1565 | 1466 |
1566 » /* 1. compile the class body into a code object */ | 1467 » if (!compiler_body(c, s->v.ClassDef.body)) { |
1567 » if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno)) | 1468 » » compiler_exit_scope(c); |
1568 » » return 0; | 1469 » » return 0; |
1569 » /* this block represents what we do in the new scope */ | 1470 » } |
1570 » { | 1471 |
1571 » » /* use the class name for name mangling */ | 1472 » ADDOP_IN_SCOPE(c, LOAD_LOCALS); |
1572 » » Py_INCREF(s->v.ClassDef.name); | 1473 » ADDOP_IN_SCOPE(c, RETURN_VALUE); |
1573 » » Py_XDECREF(c->u->u_private); | 1474 » co = assemble(c, 1); |
1574 » » c->u->u_private = s->v.ClassDef.name; | |
1575 » » /* force it to have one mandatory argument */ | |
1576 » » c->u->u_argcount = 1; | |
1577 » » /* load the first argument (__locals__) ... */ | |
1578 » » ADDOP_I(c, LOAD_FAST, 0); | |
1579 » » /* ... and store it into f_locals */ | |
1580 » » ADDOP_IN_SCOPE(c, STORE_LOCALS); | |
1581 » » /* load (global) __name__ ... */ | |
1582 » » str = PyUnicode_InternFromString("__name__"); | |
1583 » » if (!str || !compiler_nameop(c, str, Load)) { | |
1584 » » » Py_XDECREF(str); | |
1585 » » » compiler_exit_scope(c); | |
1586 » » » return 0; | |
1587 » » } | |
1588 » » Py_DECREF(str); | |
1589 » » /* ... and store it as __module__ */ | |
1590 » » str = PyUnicode_InternFromString("__module__"); | |
1591 » » if (!str || !compiler_nameop(c, str, Store)) { | |
1592 » » » Py_XDECREF(str); | |
1593 » » » compiler_exit_scope(c); | |
1594 » » » return 0; | |
1595 » » } | |
1596 » » Py_DECREF(str); | |
1597 » » /* compile the body proper */ | |
1598 » » if (!compiler_body(c, s->v.ClassDef.body)) { | |
1599 » » » compiler_exit_scope(c); | |
1600 » » » return 0; | |
1601 » » } | |
1602 » » /* return the (empty) __class__ cell */ | |
1603 » » str = PyUnicode_InternFromString("__class__"); | |
1604 » » if (str == NULL) { | |
1605 » » » compiler_exit_scope(c); | |
1606 » » » return 0; | |
1607 » » } | |
1608 » » i = compiler_lookup_arg(c->u->u_cellvars, str); | |
1609 » » Py_DECREF(str); | |
1610 » » if (i == -1) { | |
1611 » » » /* This happens when nobody references the cell */ | |
1612 » » » PyErr_Clear(); | |
1613 » » » /* Return None */ | |
1614 » » » ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
1615 } | |
1616 » » else { | |
1617 » » » /* Return the cell where to store __class__ */ | |
1618 » » » ADDOP_I(c, LOAD_CLOSURE, i); | |
1619 » » } | |
1620 » » ADDOP_IN_SCOPE(c, RETURN_VALUE); | |
1621 » » /* create the code object */ | |
1622 » » co = assemble(c, 1); | |
1623 » } | |
1624 » /* leave the new scope */ | |
1625 compiler_exit_scope(c); | 1475 compiler_exit_scope(c); |
1626 if (co == NULL) | 1476 if (co == NULL) |
1627 return 0; | 1477 return 0; |
1628 | 1478 |
1629 /* 2. load the 'build_class' function */ | |
1630 ADDOP(c, LOAD_BUILD_CLASS); | |
1631 | |
1632 /* 3. load a function (or closure) made from the code object */ | |
1633 compiler_make_closure(c, co, 0); | 1479 compiler_make_closure(c, co, 0); |
1634 Py_DECREF(co); | 1480 Py_DECREF(co); |
1635 | 1481 |
1636 » /* 4. load class name */ | 1482 » ADDOP_I(c, CALL_FUNCTION, 0); |
1637 » ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); | 1483 » ADDOP(c, BUILD_CLASS); |
1638 | 1484 » /* apply decorators */ |
1639 » /* 5. generate the rest of the code for the call */ | 1485 » for (i = 0; i < asdl_seq_LEN(decos); i++) { |
1640 » if (!compiler_call_helper(c, 2, | 1486 » » ADDOP_I(c, CALL_FUNCTION, 1); |
1641 » » » » s->v.ClassDef.bases, | 1487 » } |
1642 » » » » s->v.ClassDef.keywords, | |
1643 » » » » s->v.ClassDef.starargs, | |
1644 » » » » s->v.ClassDef.kwargs)) | |
1645 » » return 0; | |
1646 | |
1647 » /* 6. apply decorators */ | |
1648 for (i = 0; i < asdl_seq_LEN(decos); i++) { | |
1649 ADDOP_I(c, CALL_FUNCTION, 1); | |
1650 } | |
1651 | |
1652 » /* 7. store into <name> */ | |
1653 if (!compiler_nameop(c, s->v.ClassDef.name, Store)) | 1488 if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
1654 return 0; | 1489 return 0; |
1655 return 1; | 1490 return 1; |
1656 } | 1491 } |
1657 | 1492 |
1658 static int | 1493 static int |
1659 compiler_ifexp(struct compiler *c, expr_ty e) | 1494 compiler_ifexp(struct compiler *c, expr_ty e) |
1660 { | 1495 { |
1661 basicblock *end, *next; | 1496 basicblock *end, *next; |
1662 ········ | 1497 ········ |
(...skipping 12 matching lines...) Expand all Loading... |
1675 VISIT(c, expr, e->v.IfExp.orelse); | 1510 VISIT(c, expr, e->v.IfExp.orelse); |
1676 compiler_use_next_block(c, end); | 1511 compiler_use_next_block(c, end); |
1677 return 1; | 1512 return 1; |
1678 } | 1513 } |
1679 | 1514 |
1680 static int | 1515 static int |
1681 compiler_lambda(struct compiler *c, expr_ty e) | 1516 compiler_lambda(struct compiler *c, expr_ty e) |
1682 { | 1517 { |
1683 PyCodeObject *co; | 1518 PyCodeObject *co; |
1684 static identifier name; | 1519 static identifier name; |
1685 int kw_default_count = 0, arglength; | |
1686 arguments_ty args = e->v.Lambda.args; | 1520 arguments_ty args = e->v.Lambda.args; |
1687 assert(e->kind == Lambda_kind); | 1521 assert(e->kind == Lambda_kind); |
1688 | 1522 |
1689 if (!name) { | 1523 if (!name) { |
1690 » » name = PyUnicode_InternFromString("<lambda>"); | 1524 » » name = PyString_InternFromString("<lambda>"); |
1691 if (!name) | 1525 if (!name) |
1692 return 0; | 1526 return 0; |
1693 } | 1527 } |
1694 | 1528 |
1695 if (args->kwonlyargs) { | |
1696 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, | |
1697 args->kw_defaults); | |
1698 if (res < 0) return 0; | |
1699 kw_default_count = res; | |
1700 } | |
1701 if (args->defaults) | 1529 if (args->defaults) |
1702 VISIT_SEQ(c, expr, args->defaults); | 1530 VISIT_SEQ(c, expr, args->defaults); |
1703 if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) | 1531 if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) |
1704 return 0; | 1532 return 0; |
1705 | 1533 |
| 1534 /* unpack nested arguments */ |
| 1535 compiler_arguments(c, args); |
| 1536 ········ |
1706 c->u->u_argcount = asdl_seq_LEN(args->args); | 1537 c->u->u_argcount = asdl_seq_LEN(args->args); |
1707 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); | |
1708 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); | 1538 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
1709 if (c->u->u_ste->ste_generator) { | 1539 if (c->u->u_ste->ste_generator) { |
1710 ADDOP_IN_SCOPE(c, POP_TOP); | 1540 ADDOP_IN_SCOPE(c, POP_TOP); |
1711 } | 1541 } |
1712 else { | 1542 else { |
1713 ADDOP_IN_SCOPE(c, RETURN_VALUE); | 1543 ADDOP_IN_SCOPE(c, RETURN_VALUE); |
1714 } | 1544 } |
1715 co = assemble(c, 1); | 1545 co = assemble(c, 1); |
1716 compiler_exit_scope(c); | 1546 compiler_exit_scope(c); |
1717 if (co == NULL) | 1547 if (co == NULL) |
1718 return 0; | 1548 return 0; |
1719 | 1549 |
1720 » arglength = asdl_seq_LEN(args->defaults); | 1550 » compiler_make_closure(c, co, asdl_seq_LEN(args->defaults)); |
1721 » arglength |= kw_default_count << 8; | |
1722 » compiler_make_closure(c, co, arglength); | |
1723 Py_DECREF(co); | 1551 Py_DECREF(co); |
1724 | 1552 |
| 1553 return 1; |
| 1554 } |
| 1555 |
| 1556 static int |
| 1557 compiler_print(struct compiler *c, stmt_ty s) |
| 1558 { |
| 1559 int i, n; |
| 1560 bool dest; |
| 1561 |
| 1562 assert(s->kind == Print_kind); |
| 1563 n = asdl_seq_LEN(s->v.Print.values); |
| 1564 dest = false; |
| 1565 if (s->v.Print.dest) { |
| 1566 VISIT(c, expr, s->v.Print.dest); |
| 1567 dest = true; |
| 1568 } |
| 1569 for (i = 0; i < n; i++) { |
| 1570 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i); |
| 1571 if (dest) { |
| 1572 ADDOP(c, DUP_TOP); |
| 1573 VISIT(c, expr, e); |
| 1574 ADDOP(c, ROT_TWO); |
| 1575 ADDOP(c, PRINT_ITEM_TO); |
| 1576 } |
| 1577 else { |
| 1578 VISIT(c, expr, e); |
| 1579 ADDOP(c, PRINT_ITEM); |
| 1580 } |
| 1581 } |
| 1582 if (s->v.Print.nl) { |
| 1583 if (dest) |
| 1584 ADDOP(c, PRINT_NEWLINE_TO) |
| 1585 else |
| 1586 ADDOP(c, PRINT_NEWLINE) |
| 1587 } |
| 1588 else if (dest) |
| 1589 ADDOP(c, POP_TOP); |
1725 return 1; | 1590 return 1; |
1726 } | 1591 } |
1727 | 1592 |
1728 static int | 1593 static int |
1729 compiler_if(struct compiler *c, stmt_ty s) | 1594 compiler_if(struct compiler *c, stmt_ty s) |
1730 { | 1595 { |
1731 basicblock *end, *next; | 1596 basicblock *end, *next; |
1732 int constant; | 1597 int constant; |
1733 assert(s->kind == If_kind); | 1598 assert(s->kind == If_kind); |
1734 end = compiler_new_block(c); | 1599 end = compiler_new_block(c); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1776 if (start == NULL || end == NULL || cleanup == NULL) | 1641 if (start == NULL || end == NULL || cleanup == NULL) |
1777 return 0; | 1642 return 0; |
1778 ADDOP_JREL(c, SETUP_LOOP, end); | 1643 ADDOP_JREL(c, SETUP_LOOP, end); |
1779 if (!compiler_push_fblock(c, LOOP, start)) | 1644 if (!compiler_push_fblock(c, LOOP, start)) |
1780 return 0; | 1645 return 0; |
1781 VISIT(c, expr, s->v.For.iter); | 1646 VISIT(c, expr, s->v.For.iter); |
1782 ADDOP(c, GET_ITER); | 1647 ADDOP(c, GET_ITER); |
1783 compiler_use_next_block(c, start); | 1648 compiler_use_next_block(c, start); |
1784 /* for expressions must be traced on each iteration, | 1649 /* for expressions must be traced on each iteration, |
1785 so we need to set an extra line number. */ | 1650 so we need to set an extra line number. */ |
1786 » c->u->u_lineno_set = 0; | 1651 » c->u->u_lineno_set = false; |
1787 ADDOP_JREL(c, FOR_ITER, cleanup); | 1652 ADDOP_JREL(c, FOR_ITER, cleanup); |
1788 VISIT(c, expr, s->v.For.target); | 1653 VISIT(c, expr, s->v.For.target); |
1789 VISIT_SEQ(c, stmt, s->v.For.body); | 1654 VISIT_SEQ(c, stmt, s->v.For.body); |
1790 ADDOP_JABS(c, JUMP_ABSOLUTE, start); | 1655 ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
1791 compiler_use_next_block(c, cleanup); | 1656 compiler_use_next_block(c, cleanup); |
1792 ADDOP(c, POP_BLOCK); | 1657 ADDOP(c, POP_BLOCK); |
1793 compiler_pop_fblock(c, LOOP, start); | 1658 compiler_pop_fblock(c, LOOP, start); |
1794 VISIT_SEQ(c, stmt, s->v.For.orelse); | 1659 VISIT_SEQ(c, stmt, s->v.For.orelse); |
1795 compiler_use_next_block(c, end); | 1660 compiler_use_next_block(c, end); |
1796 return 1; | 1661 return 1; |
(...skipping 27 matching lines...) Expand all Loading... |
1824 else | 1689 else |
1825 orelse = NULL; | 1690 orelse = NULL; |
1826 | 1691 |
1827 ADDOP_JREL(c, SETUP_LOOP, end); | 1692 ADDOP_JREL(c, SETUP_LOOP, end); |
1828 compiler_use_next_block(c, loop); | 1693 compiler_use_next_block(c, loop); |
1829 if (!compiler_push_fblock(c, LOOP, loop)) | 1694 if (!compiler_push_fblock(c, LOOP, loop)) |
1830 return 0; | 1695 return 0; |
1831 if (constant == -1) { | 1696 if (constant == -1) { |
1832 /* while expressions must be traced on each iteration, | 1697 /* while expressions must be traced on each iteration, |
1833 so we need to set an extra line number. */ | 1698 so we need to set an extra line number. */ |
1834 » » c->u->u_lineno_set = 0; | 1699 » » c->u->u_lineno_set = false; |
1835 VISIT(c, expr, s->v.While.test); | 1700 VISIT(c, expr, s->v.While.test); |
1836 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor); | 1701 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor); |
1837 } | 1702 } |
1838 VISIT_SEQ(c, stmt, s->v.While.body); | 1703 VISIT_SEQ(c, stmt, s->v.While.body); |
1839 ADDOP_JABS(c, JUMP_ABSOLUTE, loop); | 1704 ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
1840 | 1705 |
1841 /* XXX should the two POP instructions be in a separate block | 1706 /* XXX should the two POP instructions be in a separate block |
1842 if there is no else clause ? | 1707 if there is no else clause ? |
1843 */ | 1708 */ |
1844 | 1709 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1943 if (!compiler_push_fblock(c, FINALLY_END, end)) | 1808 if (!compiler_push_fblock(c, FINALLY_END, end)) |
1944 return 0; | 1809 return 0; |
1945 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody); | 1810 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody); |
1946 ADDOP(c, END_FINALLY); | 1811 ADDOP(c, END_FINALLY); |
1947 compiler_pop_fblock(c, FINALLY_END, end); | 1812 compiler_pop_fblock(c, FINALLY_END, end); |
1948 | 1813 |
1949 return 1; | 1814 return 1; |
1950 } | 1815 } |
1951 | 1816 |
1952 /* | 1817 /* |
1953 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...": | 1818 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...": |
1954 (The contents of the value stack is shown in [], with the top | 1819 (The contents of the value stack is shown in [], with the top |
1955 at the right; 'tb' is trace-back info, 'val' the exception's | 1820 at the right; 'tb' is trace-back info, 'val' the exception's |
1956 associated value, and 'exc' the exception.) | 1821 associated value, and 'exc' the exception.) |
1957 ··· | 1822 ··· |
1958 Value stack Label Instruction Argument | 1823 Value stack Label Instruction Argument |
1959 [] SETUP_EXCEPT L1 | 1824 [] SETUP_EXCEPT L1 |
1960 [] <code for S> | 1825 [] <code for S> |
1961 [] POP_BLOCK | 1826 [] POP_BLOCK |
1962 [] JUMP_FORWARD L0 | 1827 [] JUMP_FORWARD L0 |
1963 ··· | 1828 ··· |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2000 ADDOP(c, POP_BLOCK); | 1865 ADDOP(c, POP_BLOCK); |
2001 compiler_pop_fblock(c, EXCEPT, body); | 1866 compiler_pop_fblock(c, EXCEPT, body); |
2002 ADDOP_JREL(c, JUMP_FORWARD, orelse); | 1867 ADDOP_JREL(c, JUMP_FORWARD, orelse); |
2003 n = asdl_seq_LEN(s->v.TryExcept.handlers); | 1868 n = asdl_seq_LEN(s->v.TryExcept.handlers); |
2004 compiler_use_next_block(c, except); | 1869 compiler_use_next_block(c, except); |
2005 for (i = 0; i < n; i++) { | 1870 for (i = 0; i < n; i++) { |
2006 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( | 1871 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
2007 s->v.TryExcept.handlers, i); | 1872 s->v.TryExcept.handlers, i); |
2008 if (!handler->v.ExceptHandler.type && i < n-1) | 1873 if (!handler->v.ExceptHandler.type && i < n-1) |
2009 return compiler_error(c, "default 'except:' must be last"); | 1874 return compiler_error(c, "default 'except:' must be last"); |
2010 » » c->u->u_lineno_set = 0; | 1875 » » c->u->u_lineno_set = false; |
2011 c->u->u_lineno = handler->lineno; | 1876 c->u->u_lineno = handler->lineno; |
2012 except = compiler_new_block(c); | 1877 except = compiler_new_block(c); |
2013 if (except == NULL) | 1878 if (except == NULL) |
2014 return 0; | 1879 return 0; |
2015 if (handler->v.ExceptHandler.type) { | 1880 if (handler->v.ExceptHandler.type) { |
2016 ADDOP(c, DUP_TOP); | 1881 ADDOP(c, DUP_TOP); |
2017 VISIT(c, expr, handler->v.ExceptHandler.type); | 1882 VISIT(c, expr, handler->v.ExceptHandler.type); |
2018 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); | 1883 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); |
2019 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); | 1884 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); |
2020 } | 1885 } |
2021 ADDOP(c, POP_TOP); | 1886 ADDOP(c, POP_TOP); |
2022 if (handler->v.ExceptHandler.name) { | 1887 if (handler->v.ExceptHandler.name) { |
2023 basicblock *cleanup_end, *cleanup_body; | 1888 » » » VISIT(c, expr, handler->v.ExceptHandler.name); |
2024 | 1889 » » } |
2025 cleanup_end = compiler_new_block(c); | 1890 » » else { |
2026 cleanup_body = compiler_new_block(c); | 1891 » » » ADDOP(c, POP_TOP); |
2027 if(!(cleanup_end || cleanup_body)) | 1892 » » } |
2028 return 0; | 1893 » » ADDOP(c, POP_TOP); |
2029 | |
2030 compiler_nameop(c, handler->v.ExceptHandler.name, Store); | |
2031 ADDOP(c, POP_TOP); | |
2032 | |
2033 /* | |
2034 try: | |
2035 # body | |
2036 except type as name: | |
2037 try: | |
2038 # body | |
2039 finally: | |
2040 name = None | |
2041 del name | |
2042 */ | |
2043 | |
2044 /* second try: */ | |
2045 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); | |
2046 » compiler_use_next_block(c, cleanup_body); | |
2047 » if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body)) | |
2048 » » return 0; | |
2049 | |
2050 /* second # body */ | |
2051 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); | 1894 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
2052 ADDOP(c, POP_BLOCK); | |
2053 ADDOP(c, POP_EXCEPT); | |
2054 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body); | |
2055 | |
2056 /* finally: */ | |
2057 ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
2058 compiler_use_next_block(c, cleanup_end); | |
2059 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end)) | |
2060 return 0; | |
2061 | |
2062 /* name = None */ | |
2063 ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
2064 compiler_nameop(c, handler->v.ExceptHandler.name, Store); | |
2065 | |
2066 /* del name */ | |
2067 compiler_nameop(c, handler->v.ExceptHandler.name, Del); | |
2068 | |
2069 ADDOP(c, END_FINALLY); | |
2070 compiler_pop_fblock(c, FINALLY_END, cleanup_end); | |
2071 } | |
2072 else { | |
2073 basicblock *cleanup_body; | |
2074 | |
2075 cleanup_body = compiler_new_block(c); | |
2076 if(!cleanup_body) | |
2077 return 0; | |
2078 | |
2079 ADDOP(c, POP_TOP); | |
2080 ADDOP(c, POP_TOP); | |
2081 compiler_use_next_block(c, cleanup_body); | |
2082 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body)) | |
2083 return 0; | |
2084 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); | |
2085 ADDOP(c, POP_EXCEPT); | |
2086 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body); | |
2087 } | |
2088 ADDOP_JREL(c, JUMP_FORWARD, end); | 1895 ADDOP_JREL(c, JUMP_FORWARD, end); |
2089 compiler_use_next_block(c, except); | 1896 compiler_use_next_block(c, except); |
2090 } | 1897 } |
2091 ADDOP(c, END_FINALLY); | 1898 ADDOP(c, END_FINALLY); |
2092 compiler_use_next_block(c, orelse); | 1899 compiler_use_next_block(c, orelse); |
2093 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse); | 1900 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse); |
2094 compiler_use_next_block(c, end); | 1901 compiler_use_next_block(c, end); |
2095 return 1; | 1902 return 1; |
2096 } | 1903 } |
2097 | 1904 |
2098 static int | 1905 static int |
2099 compiler_import_as(struct compiler *c, identifier name, identifier asname) | 1906 compiler_import_as(struct compiler *c, identifier name, identifier asname) |
2100 { | 1907 { |
2101 /* The IMPORT_NAME opcode was already generated. This function | 1908 /* The IMPORT_NAME opcode was already generated. This function |
2102 merely needs to bind the result to a name. | 1909 merely needs to bind the result to a name. |
2103 | 1910 |
2104 If there is a dot in name, we need to split it and emit a· | 1911 If there is a dot in name, we need to split it and emit a· |
2105 LOAD_ATTR for each name. | 1912 LOAD_ATTR for each name. |
2106 */ | 1913 */ |
2107 » const Py_UNICODE *src = PyUnicode_AS_UNICODE(name); | 1914 » const char *src = PyString_AS_STRING(name); |
2108 » const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.'); | 1915 » const char *dot = strchr(src, '.'); |
2109 if (dot) { | 1916 if (dot) { |
2110 /* Consume the base module name to get the first attribute */ | 1917 /* Consume the base module name to get the first attribute */ |
2111 src = dot + 1; | 1918 src = dot + 1; |
2112 while (dot) { | 1919 while (dot) { |
2113 /* NB src is only defined when dot != NULL */ | 1920 /* NB src is only defined when dot != NULL */ |
2114 PyObject *attr; | 1921 PyObject *attr; |
2115 » » » dot = Py_UNICODE_strchr(src, '.'); | 1922 » » » dot = strchr(src, '.'); |
2116 » » » attr = PyUnicode_FromUnicode(src,· | 1923 » » » attr = PyString_FromStringAndSize(src,· |
2117 » » » » » dot ? dot - src : Py_UNICODE_strlen(
src)); | 1924 » » » » » dot ? dot - src : strlen(src)); |
2118 if (!attr) | 1925 if (!attr) |
2119 return -1; | 1926 return -1; |
2120 ADDOP_O(c, LOAD_ATTR, attr, names); | 1927 ADDOP_O(c, LOAD_ATTR, attr, names); |
2121 Py_DECREF(attr); | 1928 Py_DECREF(attr); |
2122 src = dot + 1; | 1929 src = dot + 1; |
2123 } | 1930 } |
2124 } | 1931 } |
2125 return compiler_nameop(c, asname, Store); | 1932 return compiler_nameop(c, asname, Store); |
2126 } | 1933 } |
2127 | 1934 |
2128 static int | 1935 static int |
2129 compiler_import(struct compiler *c, stmt_ty s) | 1936 compiler_import(struct compiler *c, stmt_ty s) |
2130 { | 1937 { |
2131 /* The Import node stores a module name like a.b.c as a single | 1938 /* The Import node stores a module name like a.b.c as a single |
2132 string. This is convenient for all cases except | 1939 string. This is convenient for all cases except |
2133 import a.b.c as d | 1940 import a.b.c as d |
2134 where we need to parse that string to extract the individual | 1941 where we need to parse that string to extract the individual |
2135 module names.·· | 1942 module names.·· |
2136 XXX Perhaps change the representation to make this case simpler? | 1943 XXX Perhaps change the representation to make this case simpler? |
2137 */ | 1944 */ |
2138 int i, n = asdl_seq_LEN(s->v.Import.names); | 1945 int i, n = asdl_seq_LEN(s->v.Import.names); |
2139 | 1946 |
2140 for (i = 0; i < n; i++) { | 1947 for (i = 0; i < n; i++) { |
2141 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); | 1948 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
2142 int r; | 1949 int r; |
2143 PyObject *level; | 1950 PyObject *level; |
2144 | 1951 |
2145 level = PyLong_FromLong(0); | 1952 » » if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMP
ORT)) |
| 1953 » » » level = PyInt_FromLong(0); |
| 1954 » » else |
| 1955 » » » level = PyInt_FromLong(-1); |
| 1956 |
2146 if (level == NULL) | 1957 if (level == NULL) |
2147 return 0; | 1958 return 0; |
2148 | 1959 |
2149 ADDOP_O(c, LOAD_CONST, level, consts); | 1960 ADDOP_O(c, LOAD_CONST, level, consts); |
2150 Py_DECREF(level); | 1961 Py_DECREF(level); |
2151 ADDOP_O(c, LOAD_CONST, Py_None, consts); | 1962 ADDOP_O(c, LOAD_CONST, Py_None, consts); |
2152 ADDOP_NAME(c, IMPORT_NAME, alias->name, names); | 1963 ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
2153 | 1964 |
2154 if (alias->asname) { | 1965 if (alias->asname) { |
2155 r = compiler_import_as(c, alias->name, alias->asname); | 1966 r = compiler_import_as(c, alias->name, alias->asname); |
2156 if (!r) | 1967 if (!r) |
2157 return r; | 1968 return r; |
2158 } | 1969 } |
2159 else { | 1970 else { |
2160 identifier tmp = alias->name; | 1971 identifier tmp = alias->name; |
2161 » » » const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->nam
e); | 1972 » » » const char *base = PyString_AS_STRING(alias->name); |
2162 » » » Py_UNICODE *dot = Py_UNICODE_strchr(base, '.'); | 1973 » » » char *dot = strchr(base, '.'); |
2163 if (dot) | 1974 if (dot) |
2164 » » » » tmp = PyUnicode_FromUnicode(base,· | 1975 » » » » tmp = PyString_FromStringAndSize(base,· |
2165 » » » » » » » dot - base); | 1976 » » » » » » » » dot - base); |
2166 r = compiler_nameop(c, tmp, Store); | 1977 r = compiler_nameop(c, tmp, Store); |
2167 if (dot) { | 1978 if (dot) { |
2168 Py_DECREF(tmp); | 1979 Py_DECREF(tmp); |
2169 } | 1980 } |
2170 if (!r) | 1981 if (!r) |
2171 return r; | 1982 return r; |
2172 } | 1983 } |
2173 } | 1984 } |
2174 return 1; | 1985 return 1; |
2175 } | 1986 } |
2176 | 1987 |
2177 static int | 1988 static int |
2178 compiler_from_import(struct compiler *c, stmt_ty s) | 1989 compiler_from_import(struct compiler *c, stmt_ty s) |
2179 { | 1990 { |
2180 int i, n = asdl_seq_LEN(s->v.ImportFrom.names); | 1991 int i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
2181 | 1992 |
2182 PyObject *names = PyTuple_New(n); | 1993 PyObject *names = PyTuple_New(n); |
2183 PyObject *level; | 1994 PyObject *level; |
2184 ········ | 1995 ········ |
2185 if (!names) | 1996 if (!names) |
2186 return 0; | 1997 return 0; |
2187 | 1998 |
2188 level = PyLong_FromLong(s->v.ImportFrom.level); | 1999 » if (s->v.ImportFrom.level == 0 && c->c_flags && |
| 2000 » !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT)) |
| 2001 » » level = PyInt_FromLong(-1); |
| 2002 » else |
| 2003 » » level = PyInt_FromLong(s->v.ImportFrom.level); |
| 2004 |
2189 if (!level) { | 2005 if (!level) { |
2190 Py_DECREF(names); | 2006 Py_DECREF(names); |
2191 return 0; | 2007 return 0; |
2192 } | 2008 } |
2193 | 2009 |
2194 /* build up the names */ | 2010 /* build up the names */ |
2195 for (i = 0; i < n; i++) { | 2011 for (i = 0; i < n; i++) { |
2196 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i
); | 2012 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i
); |
2197 Py_INCREF(alias->name); | 2013 Py_INCREF(alias->name); |
2198 PyTuple_SET_ITEM(names, i, alias->name); | 2014 PyTuple_SET_ITEM(names, i, alias->name); |
2199 } | 2015 } |
2200 | 2016 |
2201 if (s->lineno > c->c_future->ff_lineno) { | 2017 if (s->lineno > c->c_future->ff_lineno) { |
2202 » » if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, | 2018 » » if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module), |
2203 » » » » » » "__future__")) { | 2019 » » » "__future__")) { |
2204 Py_DECREF(level); | 2020 Py_DECREF(level); |
2205 Py_DECREF(names); | 2021 Py_DECREF(names); |
2206 return compiler_error(c,· | 2022 return compiler_error(c,· |
2207 "from __future__ imports must occur " | 2023 "from __future__ imports must occur " |
2208 "at the beginning of the file"); | 2024 "at the beginning of the file"); |
2209 | 2025 |
2210 } | 2026 } |
2211 } | 2027 } |
2212 | 2028 |
2213 ADDOP_O(c, LOAD_CONST, level, consts); | 2029 ADDOP_O(c, LOAD_CONST, level, consts); |
2214 Py_DECREF(level); | 2030 Py_DECREF(level); |
2215 ADDOP_O(c, LOAD_CONST, names, consts); | 2031 ADDOP_O(c, LOAD_CONST, names, consts); |
2216 Py_DECREF(names); | 2032 Py_DECREF(names); |
2217 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); | 2033 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
2218 for (i = 0; i < n; i++) { | 2034 for (i = 0; i < n; i++) { |
2219 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i
); | 2035 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i
); |
2220 identifier store_name; | 2036 identifier store_name; |
2221 | 2037 |
2222 » » if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') { | 2038 » » if (i == 0 && *PyString_AS_STRING(alias->name) == '*') { |
2223 assert(n == 1); | 2039 assert(n == 1); |
2224 ADDOP(c, IMPORT_STAR); | 2040 ADDOP(c, IMPORT_STAR); |
2225 return 1; | 2041 return 1; |
2226 } | 2042 } |
2227 ···················· | 2043 ···················· |
2228 ADDOP_NAME(c, IMPORT_FROM, alias->name, names); | 2044 ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
2229 store_name = alias->name; | 2045 store_name = alias->name; |
2230 if (alias->asname) | 2046 if (alias->asname) |
2231 store_name = alias->asname; | 2047 store_name = alias->asname; |
2232 | 2048 |
2233 if (!compiler_nameop(c, store_name, Store)) { | 2049 if (!compiler_nameop(c, store_name, Store)) { |
2234 Py_DECREF(names); | 2050 Py_DECREF(names); |
2235 return 0; | 2051 return 0; |
2236 } | 2052 } |
2237 } | 2053 } |
2238 /* remove imported module */ | 2054 /* remove imported module */ |
2239 ADDOP(c, POP_TOP); | 2055 ADDOP(c, POP_TOP); |
2240 return 1; | 2056 return 1; |
2241 } | 2057 } |
2242 | 2058 |
2243 static int | 2059 static int |
2244 compiler_assert(struct compiler *c, stmt_ty s) | 2060 compiler_assert(struct compiler *c, stmt_ty s) |
2245 { | 2061 { |
2246 static PyObject *assertion_error = NULL; | 2062 static PyObject *assertion_error = NULL; |
2247 basicblock *end; | 2063 basicblock *end; |
2248 | 2064 |
2249 if (Py_OptimizeFlag) | 2065 if (Py_OptimizeFlag) |
2250 return 1; | 2066 return 1; |
2251 if (assertion_error == NULL) { | 2067 if (assertion_error == NULL) { |
2252 » » assertion_error = PyUnicode_InternFromString("AssertionError"); | 2068 » » assertion_error = PyString_InternFromString("AssertionError"); |
2253 if (assertion_error == NULL) | 2069 if (assertion_error == NULL) |
2254 return 0; | 2070 return 0; |
2255 } | 2071 } |
2256 if (s->v.Assert.test->kind == Tuple_kind && | 2072 if (s->v.Assert.test->kind == Tuple_kind && |
2257 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { | 2073 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { |
2258 const char* msg = | 2074 const char* msg = |
2259 "assertion is always true, perhaps remove parentheses?"; | 2075 "assertion is always true, perhaps remove parentheses?"; |
2260 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, | 2076 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, |
2261 c->u->u_lineno, NULL, NULL) == -1) | 2077 c->u->u_lineno, NULL, NULL) == -1) |
2262 return 0; | 2078 return 0; |
2263 } | 2079 } |
2264 VISIT(c, expr, s->v.Assert.test); | 2080 VISIT(c, expr, s->v.Assert.test); |
2265 end = compiler_new_block(c); | 2081 end = compiler_new_block(c); |
2266 if (end == NULL) | 2082 if (end == NULL) |
2267 return 0; | 2083 return 0; |
2268 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end); | 2084 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end); |
2269 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); | 2085 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); |
2270 if (s->v.Assert.msg) { | 2086 if (s->v.Assert.msg) { |
2271 VISIT(c, expr, s->v.Assert.msg); | 2087 VISIT(c, expr, s->v.Assert.msg); |
2272 » » ADDOP_I(c, CALL_FUNCTION, 1); | 2088 » » ADDOP_I(c, RAISE_VARARGS, 2); |
2273 » } | 2089 » } |
2274 » ADDOP_I(c, RAISE_VARARGS, 1); | 2090 » else { |
| 2091 » » ADDOP_I(c, RAISE_VARARGS, 1); |
| 2092 » } |
2275 compiler_use_next_block(c, end); | 2093 compiler_use_next_block(c, end); |
2276 return 1; | 2094 return 1; |
2277 } | 2095 } |
2278 | 2096 |
2279 static int | 2097 static int |
2280 compiler_visit_stmt(struct compiler *c, stmt_ty s) | 2098 compiler_visit_stmt(struct compiler *c, stmt_ty s) |
2281 { | 2099 { |
2282 int i, n; | 2100 int i, n; |
2283 | 2101 |
2284 /* Always assign a lineno to the next instruction for a stmt. */ | 2102 /* Always assign a lineno to the next instruction for a stmt. */ |
2285 c->u->u_lineno = s->lineno; | 2103 c->u->u_lineno = s->lineno; |
2286 » c->u->u_lineno_set = 0; | 2104 » c->u->u_lineno_set = false; |
2287 | 2105 |
2288 switch (s->kind) { | 2106 switch (s->kind) { |
2289 case FunctionDef_kind: | 2107 case FunctionDef_kind: |
2290 return compiler_function(c, s); | 2108 return compiler_function(c, s); |
2291 case ClassDef_kind: | 2109 case ClassDef_kind: |
2292 return compiler_class(c, s); | 2110 return compiler_class(c, s); |
2293 case Return_kind: | 2111 case Return_kind: |
2294 if (c->u->u_ste->ste_type != FunctionBlock) | 2112 if (c->u->u_ste->ste_type != FunctionBlock) |
2295 return compiler_error(c, "'return' outside function"); | 2113 return compiler_error(c, "'return' outside function"); |
2296 if (s->v.Return.value) { | 2114 if (s->v.Return.value) { |
(...skipping 11 matching lines...) Expand all Loading... |
2308 VISIT(c, expr, s->v.Assign.value); | 2126 VISIT(c, expr, s->v.Assign.value); |
2309 for (i = 0; i < n; i++) { | 2127 for (i = 0; i < n; i++) { |
2310 if (i < n - 1) | 2128 if (i < n - 1) |
2311 ADDOP(c, DUP_TOP); | 2129 ADDOP(c, DUP_TOP); |
2312 VISIT(c, expr, | 2130 VISIT(c, expr, |
2313 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); | 2131 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
2314 } | 2132 } |
2315 break; | 2133 break; |
2316 case AugAssign_kind: | 2134 case AugAssign_kind: |
2317 return compiler_augassign(c, s); | 2135 return compiler_augassign(c, s); |
| 2136 case Print_kind: |
| 2137 return compiler_print(c, s); |
2318 case For_kind: | 2138 case For_kind: |
2319 return compiler_for(c, s); | 2139 return compiler_for(c, s); |
2320 case While_kind: | 2140 case While_kind: |
2321 return compiler_while(c, s); | 2141 return compiler_while(c, s); |
2322 case If_kind: | 2142 case If_kind: |
2323 return compiler_if(c, s); | 2143 return compiler_if(c, s); |
2324 case Raise_kind: | 2144 case Raise_kind: |
2325 n = 0; | 2145 n = 0; |
2326 » » if (s->v.Raise.exc) { | 2146 » » if (s->v.Raise.type) { |
2327 » » » VISIT(c, expr, s->v.Raise.exc); | 2147 » » » VISIT(c, expr, s->v.Raise.type); |
2328 n++; | 2148 n++; |
2329 if (s->v.Raise.cause) { | 2149 » » » if (s->v.Raise.inst) { |
2330 VISIT(c, expr, s->v.Raise.cause); | 2150 » » » » VISIT(c, expr, s->v.Raise.inst); |
2331 n++; | 2151 » » » » n++; |
2332 } | 2152 » » » » if (s->v.Raise.tback) { |
| 2153 » » » » » VISIT(c, expr, s->v.Raise.tback); |
| 2154 » » » » » n++; |
| 2155 » » » » } |
| 2156 » » » } |
2333 } | 2157 } |
2334 ADDOP_I(c, RAISE_VARARGS, n); | 2158 ADDOP_I(c, RAISE_VARARGS, n); |
2335 break; | 2159 break; |
2336 case TryExcept_kind: | 2160 case TryExcept_kind: |
2337 return compiler_try_except(c, s); | 2161 return compiler_try_except(c, s); |
2338 case TryFinally_kind: | 2162 case TryFinally_kind: |
2339 return compiler_try_finally(c, s); | 2163 return compiler_try_finally(c, s); |
2340 case Assert_kind: | 2164 case Assert_kind: |
2341 return compiler_assert(c, s); | 2165 return compiler_assert(c, s); |
2342 case Import_kind: | 2166 case Import_kind: |
2343 return compiler_import(c, s); | 2167 return compiler_import(c, s); |
2344 case ImportFrom_kind: | 2168 case ImportFrom_kind: |
2345 return compiler_from_import(c, s); | 2169 return compiler_from_import(c, s); |
| 2170 case Exec_kind: |
| 2171 VISIT(c, expr, s->v.Exec.body); |
| 2172 if (s->v.Exec.globals) { |
| 2173 VISIT(c, expr, s->v.Exec.globals); |
| 2174 if (s->v.Exec.locals) { |
| 2175 VISIT(c, expr, s->v.Exec.locals); |
| 2176 } else { |
| 2177 ADDOP(c, DUP_TOP); |
| 2178 } |
| 2179 } else { |
| 2180 ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 2181 ADDOP(c, DUP_TOP); |
| 2182 } |
| 2183 ADDOP(c, EXEC_STMT); |
| 2184 break; |
2346 case Global_kind: | 2185 case Global_kind: |
2347 case Nonlocal_kind: | |
2348 break; | 2186 break; |
2349 case Expr_kind: | 2187 case Expr_kind: |
2350 if (c->c_interactive && c->c_nestlevel <= 1) { | 2188 if (c->c_interactive && c->c_nestlevel <= 1) { |
2351 VISIT(c, expr, s->v.Expr.value); | 2189 VISIT(c, expr, s->v.Expr.value); |
2352 ADDOP(c, PRINT_EXPR); | 2190 ADDOP(c, PRINT_EXPR); |
2353 } | 2191 } |
2354 else if (s->v.Expr.value->kind != Str_kind && | 2192 else if (s->v.Expr.value->kind != Str_kind && |
2355 s->v.Expr.value->kind != Num_kind) { | 2193 s->v.Expr.value->kind != Num_kind) { |
2356 VISIT(c, expr, s->v.Expr.value); | 2194 VISIT(c, expr, s->v.Expr.value); |
2357 ADDOP(c, POP_TOP); | 2195 ADDOP(c, POP_TOP); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2395 binop(struct compiler *c, operator_ty op) | 2233 binop(struct compiler *c, operator_ty op) |
2396 { | 2234 { |
2397 switch (op) { | 2235 switch (op) { |
2398 case Add: | 2236 case Add: |
2399 return BINARY_ADD; | 2237 return BINARY_ADD; |
2400 case Sub: | 2238 case Sub: |
2401 return BINARY_SUBTRACT; | 2239 return BINARY_SUBTRACT; |
2402 case Mult: | 2240 case Mult: |
2403 return BINARY_MULTIPLY; | 2241 return BINARY_MULTIPLY; |
2404 case Div: | 2242 case Div: |
2405 » » return BINARY_TRUE_DIVIDE; | 2243 » » if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION) |
| 2244 » » » return BINARY_TRUE_DIVIDE; |
| 2245 » » else |
| 2246 » » » return BINARY_DIVIDE; |
2406 case Mod: | 2247 case Mod: |
2407 return BINARY_MODULO; | 2248 return BINARY_MODULO; |
2408 case Pow: | 2249 case Pow: |
2409 return BINARY_POWER; | 2250 return BINARY_POWER; |
2410 case LShift: | 2251 case LShift: |
2411 return BINARY_LSHIFT; | 2252 return BINARY_LSHIFT; |
2412 case RShift: | 2253 case RShift: |
2413 return BINARY_RSHIFT; | 2254 return BINARY_RSHIFT; |
2414 case BitOr: | 2255 case BitOr: |
2415 return BINARY_OR; | 2256 return BINARY_OR; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2459 inplace_binop(struct compiler *c, operator_ty op) | 2300 inplace_binop(struct compiler *c, operator_ty op) |
2460 { | 2301 { |
2461 switch (op) { | 2302 switch (op) { |
2462 case Add: | 2303 case Add: |
2463 return INPLACE_ADD; | 2304 return INPLACE_ADD; |
2464 case Sub: | 2305 case Sub: |
2465 return INPLACE_SUBTRACT; | 2306 return INPLACE_SUBTRACT; |
2466 case Mult: | 2307 case Mult: |
2467 return INPLACE_MULTIPLY; | 2308 return INPLACE_MULTIPLY; |
2468 case Div: | 2309 case Div: |
2469 » » return INPLACE_TRUE_DIVIDE; | 2310 » » if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION) |
| 2311 » » » return INPLACE_TRUE_DIVIDE; |
| 2312 » » else |
| 2313 » » » return INPLACE_DIVIDE; |
2470 case Mod: | 2314 case Mod: |
2471 return INPLACE_MODULO; | 2315 return INPLACE_MODULO; |
2472 case Pow: | 2316 case Pow: |
2473 return INPLACE_POWER; | 2317 return INPLACE_POWER; |
2474 case LShift: | 2318 case LShift: |
2475 return INPLACE_LSHIFT; | 2319 return INPLACE_LSHIFT; |
2476 case RShift: | 2320 case RShift: |
2477 return INPLACE_RSHIFT; | 2321 return INPLACE_RSHIFT; |
2478 case BitOr: | 2322 case BitOr: |
2479 return INPLACE_OR; | 2323 return INPLACE_OR; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2527 break; | 2371 break; |
2528 case GLOBAL_EXPLICIT: | 2372 case GLOBAL_EXPLICIT: |
2529 optype = OP_GLOBAL; | 2373 optype = OP_GLOBAL; |
2530 break; | 2374 break; |
2531 default: | 2375 default: |
2532 /* scope can be 0 */ | 2376 /* scope can be 0 */ |
2533 break; | 2377 break; |
2534 } | 2378 } |
2535 | 2379 |
2536 /* XXX Leave assert here, but handle __doc__ and the like better */ | 2380 /* XXX Leave assert here, but handle __doc__ and the like better */ |
2537 » assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_'); | 2381 » assert(scope || PyString_AS_STRING(name)[0] == '_'); |
2538 | 2382 |
2539 switch (optype) { | 2383 switch (optype) { |
2540 case OP_DEREF: | 2384 case OP_DEREF: |
2541 switch (ctx) { | 2385 switch (ctx) { |
2542 case Load: op = LOAD_DEREF; break; | 2386 case Load: op = LOAD_DEREF; break; |
2543 case Store: op = STORE_DEREF; break; | 2387 case Store: op = STORE_DEREF; break; |
2544 case AugLoad: | 2388 case AugLoad: |
2545 case AugStore: | 2389 case AugStore: |
2546 break; | 2390 break; |
2547 case Del: | 2391 case Del: |
2548 PyErr_Format(PyExc_SyntaxError, | 2392 PyErr_Format(PyExc_SyntaxError, |
2549 » » » » "can not delete variable '%S' referenced " | 2393 » » » » "can not delete variable '%s' referenced " |
2550 "in nested scope", | 2394 "in nested scope", |
2551 » » » » name); | 2395 » » » » PyString_AS_STRING(name)); |
2552 Py_DECREF(mangled); | 2396 Py_DECREF(mangled); |
2553 return 0; | 2397 return 0; |
2554 case Param: | 2398 case Param: |
2555 default: | 2399 default: |
2556 PyErr_SetString(PyExc_SystemError, | 2400 PyErr_SetString(PyExc_SystemError, |
2557 "param invalid for deref variable"); | 2401 "param invalid for deref variable"); |
2558 return 0; | 2402 return 0; |
2559 } | 2403 } |
2560 break; | 2404 break; |
2561 case OP_FAST: | 2405 case OP_FAST: |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2640 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); | 2484 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
2641 compiler_use_next_block(c, end); | 2485 compiler_use_next_block(c, end); |
2642 return 1; | 2486 return 1; |
2643 } | 2487 } |
2644 | 2488 |
2645 static int | 2489 static int |
2646 compiler_list(struct compiler *c, expr_ty e) | 2490 compiler_list(struct compiler *c, expr_ty e) |
2647 { | 2491 { |
2648 int n = asdl_seq_LEN(e->v.List.elts); | 2492 int n = asdl_seq_LEN(e->v.List.elts); |
2649 if (e->v.List.ctx == Store) { | 2493 if (e->v.List.ctx == Store) { |
2650 » » int i, seen_star = 0; | 2494 » » ADDOP_I(c, UNPACK_SEQUENCE, n); |
2651 » » for (i = 0; i < n; i++) { | |
2652 » » » expr_ty elt = asdl_seq_GET(e->v.List.elts, i); | |
2653 » » » if (elt->kind == Starred_kind && !seen_star) { | |
2654 » » » » if ((i >= (1 << 8)) || | |
2655 » » » » (n-i-1 >= (INT_MAX >> 8))) | |
2656 » » » » » return compiler_error(c, | |
2657 » » » » » » "too many expressions in " | |
2658 » » » » » » "star-unpacking assignment"); | |
2659 » » » » ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); | |
2660 » » » » seen_star = 1; | |
2661 » » » » asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.v
alue); | |
2662 » » » } else if (elt->kind == Starred_kind) { | |
2663 » » » » return compiler_error(c, | |
2664 » » » » » "two starred expressions in assignment")
; | |
2665 » » » } | |
2666 » » } | |
2667 » » if (!seen_star) { | |
2668 » » » ADDOP_I(c, UNPACK_SEQUENCE, n); | |
2669 » » } | |
2670 } | 2495 } |
2671 VISIT_SEQ(c, expr, e->v.List.elts); | 2496 VISIT_SEQ(c, expr, e->v.List.elts); |
2672 if (e->v.List.ctx == Load) { | 2497 if (e->v.List.ctx == Load) { |
2673 ADDOP_I(c, BUILD_LIST, n); | 2498 ADDOP_I(c, BUILD_LIST, n); |
2674 } | 2499 } |
2675 return 1; | 2500 return 1; |
2676 } | 2501 } |
2677 | 2502 |
2678 static int | 2503 static int |
2679 compiler_tuple(struct compiler *c, expr_ty e) | 2504 compiler_tuple(struct compiler *c, expr_ty e) |
2680 { | 2505 { |
2681 int n = asdl_seq_LEN(e->v.Tuple.elts); | 2506 int n = asdl_seq_LEN(e->v.Tuple.elts); |
2682 if (e->v.Tuple.ctx == Store) { | 2507 if (e->v.Tuple.ctx == Store) { |
2683 » » int i, seen_star = 0; | 2508 » » ADDOP_I(c, UNPACK_SEQUENCE, n); |
2684 » » for (i = 0; i < n; i++) { | |
2685 » » » expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i); | |
2686 » » » if (elt->kind == Starred_kind && !seen_star) { | |
2687 » » » » if ((i >= (1 << 8)) || | |
2688 » » » » (n-i-1 >= (INT_MAX >> 8))) | |
2689 » » » » » return compiler_error(c, | |
2690 » » » » » » "too many expressions in " | |
2691 » » » » » » "star-unpacking assignment"); | |
2692 » » » » ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); | |
2693 » » » » seen_star = 1; | |
2694 » » » » asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.
value); | |
2695 » » » } else if (elt->kind == Starred_kind) { | |
2696 » » » » return compiler_error(c, | |
2697 » » » » » "two starred expressions in assignment")
; | |
2698 » » » } | |
2699 » » } | |
2700 » » if (!seen_star) { | |
2701 » » » ADDOP_I(c, UNPACK_SEQUENCE, n); | |
2702 » » } | |
2703 } | 2509 } |
2704 VISIT_SEQ(c, expr, e->v.Tuple.elts); | 2510 VISIT_SEQ(c, expr, e->v.Tuple.elts); |
2705 if (e->v.Tuple.ctx == Load) { | 2511 if (e->v.Tuple.ctx == Load) { |
2706 ADDOP_I(c, BUILD_TUPLE, n); | 2512 ADDOP_I(c, BUILD_TUPLE, n); |
2707 } | 2513 } |
2708 return 1; | 2514 return 1; |
2709 } | 2515 } |
2710 | 2516 |
2711 static int | 2517 static int |
2712 compiler_compare(struct compiler *c, expr_ty e) | 2518 compiler_compare(struct compiler *c, expr_ty e) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2749 ADDOP(c, ROT_TWO); | 2555 ADDOP(c, ROT_TWO); |
2750 ADDOP(c, POP_TOP); | 2556 ADDOP(c, POP_TOP); |
2751 compiler_use_next_block(c, end); | 2557 compiler_use_next_block(c, end); |
2752 } | 2558 } |
2753 return 1; | 2559 return 1; |
2754 } | 2560 } |
2755 | 2561 |
2756 static int | 2562 static int |
2757 compiler_call(struct compiler *c, expr_ty e) | 2563 compiler_call(struct compiler *c, expr_ty e) |
2758 { | 2564 { |
| 2565 int n, code = 0; |
| 2566 |
2759 VISIT(c, expr, e->v.Call.func); | 2567 VISIT(c, expr, e->v.Call.func); |
2760 » return compiler_call_helper(c, 0, | 2568 » n = asdl_seq_LEN(e->v.Call.args); |
2761 » » » » e->v.Call.args, | 2569 » VISIT_SEQ(c, expr, e->v.Call.args); |
2762 » » » » e->v.Call.keywords, | 2570 » if (e->v.Call.keywords) { |
2763 » » » » e->v.Call.starargs, | 2571 » » VISIT_SEQ(c, keyword, e->v.Call.keywords); |
2764 » » » » e->v.Call.kwargs); | 2572 » » n |= asdl_seq_LEN(e->v.Call.keywords) << 8; |
2765 } | 2573 » } |
2766 | 2574 » if (e->v.Call.starargs) { |
2767 /* shared code between compiler_call and compiler_class */ | 2575 » » VISIT(c, expr, e->v.Call.starargs); |
2768 static int | |
2769 compiler_call_helper(struct compiler *c, | |
2770 » » int n, /* Args already pushed */ | |
2771 » » asdl_seq *args, | |
2772 » » asdl_seq *keywords, | |
2773 » » expr_ty starargs, | |
2774 » » expr_ty kwargs) | |
2775 { | |
2776 » int code = 0; | |
2777 | |
2778 » n += asdl_seq_LEN(args); | |
2779 » VISIT_SEQ(c, expr, args); | |
2780 » if (keywords) { | |
2781 » » VISIT_SEQ(c, keyword, keywords); | |
2782 » » n |= asdl_seq_LEN(keywords) << 8; | |
2783 » } | |
2784 » if (starargs) { | |
2785 » » VISIT(c, expr, starargs); | |
2786 code |= 1; | 2576 code |= 1; |
2787 } | 2577 } |
2788 » if (kwargs) { | 2578 » if (e->v.Call.kwargs) { |
2789 » » VISIT(c, expr, kwargs); | 2579 » » VISIT(c, expr, e->v.Call.kwargs); |
2790 code |= 2; | 2580 code |= 2; |
2791 } | 2581 } |
2792 switch (code) { | 2582 switch (code) { |
2793 case 0: | 2583 case 0: |
2794 ADDOP_I(c, CALL_FUNCTION, n); | 2584 ADDOP_I(c, CALL_FUNCTION, n); |
2795 break; | 2585 break; |
2796 case 1: | 2586 case 1: |
2797 ADDOP_I(c, CALL_FUNCTION_VAR, n); | 2587 ADDOP_I(c, CALL_FUNCTION_VAR, n); |
2798 break; | 2588 break; |
2799 case 2: | 2589 case 2: |
2800 ADDOP_I(c, CALL_FUNCTION_KW, n); | 2590 ADDOP_I(c, CALL_FUNCTION_KW, n); |
2801 break; | 2591 break; |
2802 case 3: | 2592 case 3: |
2803 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n); | 2593 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n); |
2804 break; | 2594 break; |
2805 } | 2595 } |
2806 return 1; | 2596 return 1; |
2807 } | 2597 } |
2808 | 2598 |
2809 | 2599 static int |
2810 /* List and set comprehensions and generator expressions work by creating a | 2600 compiler_listcomp_generator(struct compiler *c, asdl_seq *generators, |
2811 nested function to perform the actual iteration. This means that the | 2601 » » » int gen_index, expr_ty elt) |
2812 iteration variables don't leak into the current scope. | |
2813 The defined function is called immediately following its definition, with the | |
2814 result of that call being the result of the expression. | |
2815 The LC/SC version returns the populated container, while the GE version is | |
2816 flagged in symtable.c as a generator, so it returns the generator object | |
2817 when the function is called. | |
2818 This code *knows* that the loop cannot contain break, continue, or return, | |
2819 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops. | |
2820 | |
2821 Possible cleanups: | |
2822 - iterate over the generator sequence instead of using recursion | |
2823 */ | |
2824 | |
2825 static int | |
2826 compiler_comprehension_generator(struct compiler *c,· | |
2827 » » » » asdl_seq *generators, int gen_index,· | |
2828 » » » » expr_ty elt, expr_ty val, int type) | |
2829 { | 2602 { |
2830 /* generate code for the iterator, then each of the ifs, | 2603 /* generate code for the iterator, then each of the ifs, |
2831 and then write to the element */ | 2604 and then write to the element */ |
2832 | 2605 |
2833 » comprehension_ty gen; | 2606 » comprehension_ty l; |
2834 basicblock *start, *anchor, *skip, *if_cleanup; | 2607 basicblock *start, *anchor, *skip, *if_cleanup; |
2835 int i, n; | 2608 int i, n; |
2836 | 2609 |
2837 start = compiler_new_block(c); | 2610 start = compiler_new_block(c); |
2838 skip = compiler_new_block(c); | 2611 skip = compiler_new_block(c); |
2839 if_cleanup = compiler_new_block(c); | 2612 if_cleanup = compiler_new_block(c); |
2840 anchor = compiler_new_block(c); | 2613 anchor = compiler_new_block(c); |
2841 | 2614 |
2842 if (start == NULL || skip == NULL || if_cleanup == NULL || | 2615 if (start == NULL || skip == NULL || if_cleanup == NULL || |
2843 » anchor == NULL) | 2616 » » anchor == NULL) |
2844 » » return 0; | 2617 » return 0; |
2845 | 2618 |
2846 » gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); | 2619 » l = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 2620 » VISIT(c, expr, l->iter); |
| 2621 » ADDOP(c, GET_ITER); |
| 2622 » compiler_use_next_block(c, start); |
| 2623 » ADDOP_JREL(c, FOR_ITER, anchor); |
| 2624 » NEXT_BLOCK(c); |
| 2625 » VISIT(c, expr, l->target); |
| 2626 |
| 2627 » /* XXX this needs to be cleaned up...a lot! */ |
| 2628 » n = asdl_seq_LEN(l->ifs); |
| 2629 » for (i = 0; i < n; i++) { |
| 2630 » » expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i); |
| 2631 » » VISIT(c, expr, e); |
| 2632 » » ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup); |
| 2633 » » NEXT_BLOCK(c); |
| 2634 » }· |
| 2635 |
| 2636 » if (++gen_index < asdl_seq_LEN(generators)) |
| 2637 » if (!compiler_listcomp_generator(c, generators, gen_index, elt)) |
| 2638 » » return 0; |
| 2639 |
| 2640 » /* only append after the last for generator */ |
| 2641 » if (gen_index >= asdl_seq_LEN(generators)) { |
| 2642 » VISIT(c, expr, elt); |
| 2643 » ADDOP_I(c, LIST_APPEND, gen_index+1); |
| 2644 |
| 2645 » compiler_use_next_block(c, skip); |
| 2646 » } |
| 2647 » compiler_use_next_block(c, if_cleanup); |
| 2648 » ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2649 » compiler_use_next_block(c, anchor); |
2847 ········ | 2650 ········ |
| 2651 return 1; |
| 2652 } |
| 2653 |
| 2654 static int |
| 2655 compiler_listcomp(struct compiler *c, expr_ty e) |
| 2656 { |
| 2657 assert(e->kind == ListComp_kind); |
| 2658 ADDOP_I(c, BUILD_LIST, 0); |
| 2659 return compiler_listcomp_generator(c, e->v.ListComp.generators, 0, |
| 2660 e->v.ListComp.elt); |
| 2661 } |
| 2662 |
| 2663 static int |
| 2664 compiler_genexp_generator(struct compiler *c, |
| 2665 asdl_seq *generators, int gen_index,· |
| 2666 expr_ty elt) |
| 2667 { |
| 2668 /* generate code for the iterator, then each of the ifs, |
| 2669 and then write to the element */ |
| 2670 |
| 2671 comprehension_ty ge; |
| 2672 basicblock *start, *anchor, *skip, *if_cleanup, *end; |
| 2673 int i, n; |
| 2674 |
| 2675 start = compiler_new_block(c); |
| 2676 skip = compiler_new_block(c); |
| 2677 if_cleanup = compiler_new_block(c); |
| 2678 anchor = compiler_new_block(c); |
| 2679 end = compiler_new_block(c); |
| 2680 |
| 2681 if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 2682 anchor == NULL || end == NULL) |
| 2683 return 0; |
| 2684 |
| 2685 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 2686 ADDOP_JREL(c, SETUP_LOOP, end); |
| 2687 if (!compiler_push_fblock(c, LOOP, start)) |
| 2688 return 0; |
| 2689 |
2848 if (gen_index == 0) { | 2690 if (gen_index == 0) { |
2849 /* Receive outermost iter as an implicit argument */ | 2691 /* Receive outermost iter as an implicit argument */ |
2850 c->u->u_argcount = 1; | 2692 c->u->u_argcount = 1; |
2851 ADDOP_I(c, LOAD_FAST, 0); | 2693 ADDOP_I(c, LOAD_FAST, 0); |
2852 } | 2694 } |
2853 else { | 2695 else { |
2854 /* Sub-iter - calculate on the fly */ | 2696 /* Sub-iter - calculate on the fly */ |
2855 » » VISIT(c, expr, gen->iter); | 2697 » » VISIT(c, expr, ge->iter); |
2856 ADDOP(c, GET_ITER); | 2698 ADDOP(c, GET_ITER); |
2857 } | 2699 } |
2858 compiler_use_next_block(c, start); | 2700 compiler_use_next_block(c, start); |
2859 ADDOP_JREL(c, FOR_ITER, anchor); | 2701 ADDOP_JREL(c, FOR_ITER, anchor); |
2860 NEXT_BLOCK(c); | 2702 NEXT_BLOCK(c); |
2861 » VISIT(c, expr, gen->target); | 2703 » VISIT(c, expr, ge->target); |
2862 | 2704 |
2863 /* XXX this needs to be cleaned up...a lot! */ | 2705 /* XXX this needs to be cleaned up...a lot! */ |
2864 » n = asdl_seq_LEN(gen->ifs); | 2706 » n = asdl_seq_LEN(ge->ifs); |
2865 for (i = 0; i < n; i++) { | 2707 for (i = 0; i < n; i++) { |
2866 » » expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); | 2708 » » expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i); |
2867 VISIT(c, expr, e); | 2709 VISIT(c, expr, e); |
2868 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup); | 2710 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup); |
2869 NEXT_BLOCK(c); | 2711 NEXT_BLOCK(c); |
2870 } | 2712 } |
2871 | 2713 |
2872 if (++gen_index < asdl_seq_LEN(generators)) | 2714 if (++gen_index < asdl_seq_LEN(generators)) |
2873 » » if (!compiler_comprehension_generator(c,· | 2715 » » if (!compiler_genexp_generator(c, generators, gen_index, elt)) |
2874 » » » » » » generators, gen_index, | 2716 » » » return 0; |
2875 » » » » » » elt, val, type)) | 2717 |
2876 » » return 0; | 2718 » /* only append after the last 'for' generator */ |
2877 | |
2878 » /* only append after the last for generator */ | |
2879 if (gen_index >= asdl_seq_LEN(generators)) { | 2719 if (gen_index >= asdl_seq_LEN(generators)) { |
2880 » » /* comprehension specific code */ | 2720 » » VISIT(c, expr, elt); |
2881 » » switch (type) { | 2721 » » ADDOP(c, YIELD_VALUE); |
2882 » » case COMP_GENEXP: | 2722 » » ADDOP(c, POP_TOP); |
2883 » » » VISIT(c, expr, elt); | |
2884 » » » ADDOP(c, YIELD_VALUE); | |
2885 » » » ADDOP(c, POP_TOP); | |
2886 » » » break; | |
2887 » » case COMP_LISTCOMP: | |
2888 » » » VISIT(c, expr, elt); | |
2889 » » » ADDOP_I(c, LIST_APPEND, gen_index + 1); | |
2890 » » » break; | |
2891 » » case COMP_SETCOMP: | |
2892 » » » VISIT(c, expr, elt); | |
2893 » » » ADDOP_I(c, SET_ADD, gen_index + 1); | |
2894 » » » break; | |
2895 » » case COMP_DICTCOMP: | |
2896 » » » /* With 'd[k] = v', v is evaluated before k, so we do | |
2897 » » » the same. */ | |
2898 » » » VISIT(c, expr, val); | |
2899 » » » VISIT(c, expr, elt); | |
2900 » » » ADDOP_I(c, MAP_ADD, gen_index + 1); | |
2901 » » » break; | |
2902 » » default: | |
2903 » » » return 0; | |
2904 » » } | |
2905 | 2723 |
2906 compiler_use_next_block(c, skip); | 2724 compiler_use_next_block(c, skip); |
2907 } | 2725 } |
2908 compiler_use_next_block(c, if_cleanup); | 2726 compiler_use_next_block(c, if_cleanup); |
2909 ADDOP_JABS(c, JUMP_ABSOLUTE, start); | 2727 ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
2910 compiler_use_next_block(c, anchor); | 2728 compiler_use_next_block(c, anchor); |
2911 | 2729 » ADDOP(c, POP_BLOCK); |
2912 » return 1; | 2730 » compiler_pop_fblock(c, LOOP, start); |
2913 } | 2731 » compiler_use_next_block(c, end); |
2914 | 2732 |
2915 static int | 2733 » return 1; |
2916 compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name, | 2734 } |
2917 » » asdl_seq *generators, expr_ty elt, expr_ty val) | 2735 |
2918 { | 2736 static int |
2919 » PyCodeObject *co = NULL; | 2737 compiler_genexp(struct compiler *c, expr_ty e) |
2920 » expr_ty outermost_iter; | 2738 { |
2921 | 2739 » static identifier name; |
2922 » outermost_iter = ((comprehension_ty) | 2740 » PyCodeObject *co; |
2923 » » » asdl_seq_GET(generators, 0))->iter; | 2741 » expr_ty outermost_iter = ((comprehension_ty) |
| 2742 » » » » (asdl_seq_GET(e->v.GeneratorExp.generators, |
| 2743 » » » » » 0)))->iter; |
| 2744 |
| 2745 » if (!name) { |
| 2746 » » name = PyString_FromString("<genexpr>"); |
| 2747 » » if (!name) |
| 2748 » » » return 0; |
| 2749 » } |
2924 | 2750 |
2925 if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) | 2751 if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) |
2926 » » goto error; | 2752 » » return 0; |
2927 »······· | 2753 » compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0, |
2928 » if (type != COMP_GENEXP) { | 2754 » » » » e->v.GeneratorExp.elt); |
2929 » » int op; | |
2930 » » switch (type) { | |
2931 » » case COMP_LISTCOMP: | |
2932 » » » op = BUILD_LIST; | |
2933 » » » break; | |
2934 » » case COMP_SETCOMP: | |
2935 » » » op = BUILD_SET; | |
2936 » » » break; | |
2937 » » case COMP_DICTCOMP: | |
2938 » » » op = BUILD_MAP; | |
2939 » » » break; | |
2940 » » default: | |
2941 » » » PyErr_Format(PyExc_SystemError, | |
2942 » » » » "unknown comprehension type %d", type); | |
2943 » » » goto error_in_scope; | |
2944 » » } | |
2945 | |
2946 » » ADDOP_I(c, op, 0); | |
2947 » } | |
2948 »······· | |
2949 » if (!compiler_comprehension_generator(c, generators, 0, elt, | |
2950 » » » » » val, type)) | |
2951 » » goto error_in_scope; | |
2952 »······· | |
2953 » if (type != COMP_GENEXP) { | |
2954 » » ADDOP(c, RETURN_VALUE); | |
2955 » } | |
2956 | |
2957 co = assemble(c, 1); | 2755 co = assemble(c, 1); |
2958 compiler_exit_scope(c); | 2756 compiler_exit_scope(c); |
2959 if (co == NULL) | 2757 if (co == NULL) |
2960 » » goto error; | 2758 » » return 0; |
2961 | 2759 |
2962 » if (!compiler_make_closure(c, co, 0)) | 2760 » compiler_make_closure(c, co, 0); |
2963 » » goto error; | |
2964 Py_DECREF(co); | 2761 Py_DECREF(co); |
2965 | 2762 |
2966 VISIT(c, expr, outermost_iter); | 2763 VISIT(c, expr, outermost_iter); |
2967 ADDOP(c, GET_ITER); | 2764 ADDOP(c, GET_ITER); |
2968 ADDOP_I(c, CALL_FUNCTION, 1); | 2765 ADDOP_I(c, CALL_FUNCTION, 1); |
2969 » return 1; | 2766 |
2970 error_in_scope: | 2767 » return 1; |
2971 » compiler_exit_scope(c); | 2768 } |
2972 error: | |
2973 » Py_XDECREF(co); | |
2974 » return 0; | |
2975 } | |
2976 | |
2977 static int | |
2978 compiler_genexp(struct compiler *c, expr_ty e) | |
2979 { | |
2980 » static identifier name; | |
2981 » if (!name) { | |
2982 » » name = PyUnicode_FromString("<genexpr>"); | |
2983 » » if (!name) | |
2984 » » » return 0; | |
2985 » } | |
2986 » assert(e->kind == GeneratorExp_kind); | |
2987 » return compiler_comprehension(c, e, COMP_GENEXP, name, | |
2988 » » » » e->v.GeneratorExp.generators, | |
2989 » » » » e->v.GeneratorExp.elt, NULL); | |
2990 } | |
2991 | |
2992 static int | |
2993 compiler_listcomp(struct compiler *c, expr_ty e) | |
2994 { | |
2995 » static identifier name; | |
2996 » if (!name) { | |
2997 » » name = PyUnicode_FromString("<listcomp>"); | |
2998 » » if (!name) | |
2999 » » » return 0; | |
3000 » } | |
3001 » assert(e->kind == ListComp_kind); | |
3002 » return compiler_comprehension(c, e, COMP_LISTCOMP, name, | |
3003 » » » » e->v.ListComp.generators, | |
3004 » » » » e->v.ListComp.elt, NULL); | |
3005 } | |
3006 | |
3007 static int | |
3008 compiler_setcomp(struct compiler *c, expr_ty e) | |
3009 { | |
3010 » static identifier name; | |
3011 » if (!name) { | |
3012 » » name = PyUnicode_FromString("<setcomp>"); | |
3013 » » if (!name) | |
3014 » » » return 0; | |
3015 » } | |
3016 » assert(e->kind == SetComp_kind); | |
3017 » return compiler_comprehension(c, e, COMP_SETCOMP, name, | |
3018 » » » » e->v.SetComp.generators, | |
3019 » » » » e->v.SetComp.elt, NULL); | |
3020 } | |
3021 | |
3022 | |
3023 static int | |
3024 compiler_dictcomp(struct compiler *c, expr_ty e) | |
3025 { | |
3026 » static identifier name; | |
3027 » if (!name) { | |
3028 » » name = PyUnicode_FromString("<dictcomp>"); | |
3029 » » if (!name) | |
3030 » » » return 0; | |
3031 » } | |
3032 » assert(e->kind == DictComp_kind); | |
3033 » return compiler_comprehension(c, e, COMP_DICTCOMP, name, | |
3034 » » » » e->v.DictComp.generators, | |
3035 » » » » e->v.DictComp.key, e->v.DictComp.value); | |
3036 } | |
3037 | |
3038 | 2769 |
3039 static int | 2770 static int |
3040 compiler_visit_keyword(struct compiler *c, keyword_ty k) | 2771 compiler_visit_keyword(struct compiler *c, keyword_ty k) |
3041 { | 2772 { |
3042 ADDOP_O(c, LOAD_CONST, k->arg, consts); | 2773 ADDOP_O(c, LOAD_CONST, k->arg, consts); |
3043 VISIT(c, expr, k->value); | 2774 VISIT(c, expr, k->value); |
3044 return 1; | 2775 return 1; |
3045 } | 2776 } |
3046 | 2777 |
3047 /* Test whether expression is constant. For constants, report | 2778 /* Test whether expression is constant. For constants, report |
3048 whether they are true or false. | 2779 whether they are true or false. |
3049 | 2780 |
3050 Return values: 1 for true, 0 for false, -1 for non-constant. | 2781 Return values: 1 for true, 0 for false, -1 for non-constant. |
3051 */ | 2782 */ |
3052 | 2783 |
3053 static int | 2784 static int |
3054 expr_constant(expr_ty e) | 2785 expr_constant(expr_ty e) |
3055 { | 2786 { |
3056 char *id; | |
3057 switch (e->kind) { | 2787 switch (e->kind) { |
3058 case Ellipsis_kind: | |
3059 return 1; | |
3060 case Num_kind: | 2788 case Num_kind: |
3061 return PyObject_IsTrue(e->v.Num.n); | 2789 return PyObject_IsTrue(e->v.Num.n); |
3062 case Str_kind: | 2790 case Str_kind: |
3063 return PyObject_IsTrue(e->v.Str.s); | 2791 return PyObject_IsTrue(e->v.Str.s); |
3064 case Name_kind: | 2792 case Name_kind: |
3065 » » /* optimize away names that can't be reassigned */ | 2793 » » /* __debug__ is not assignable, so we can optimize |
3066 » » id = PyBytes_AS_STRING( | 2794 » » * it away in if and while statements */ |
3067 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL)); | 2795 » » if (strcmp(PyString_AS_STRING(e->v.Name.id), |
3068 » » if (strcmp(id, "True") == 0) return 1; | 2796 » » » "__debug__") == 0) |
3069 » » if (strcmp(id, "False") == 0) return 0; | 2797 » » » return ! Py_OptimizeFlag; |
3070 » » if (strcmp(id, "None") == 0) return 0; | |
3071 » » if (strcmp(id, "__debug__") == 0) | |
3072 » » » return ! Py_OptimizeFlag; | |
3073 /* fall through */ | 2798 /* fall through */ |
3074 default: | 2799 default: |
3075 return -1; | 2800 return -1; |
3076 } | 2801 } |
3077 } | 2802 } |
3078 | 2803 |
3079 /* | 2804 /* |
3080 Implements the with statement from PEP 343. | 2805 Implements the with statement from PEP 343. |
3081 | 2806 |
3082 The semantics outlined in that PEP are as follows:·· | 2807 The semantics outlined in that PEP are as follows:·· |
(...skipping 14 matching lines...) Expand all Loading... |
3097 exc = copy of (exception, instance, traceback) | 2822 exc = copy of (exception, instance, traceback) |
3098 else: | 2823 else: |
3099 exc = (None, None, None) | 2824 exc = (None, None, None) |
3100 exit(*exc) | 2825 exit(*exc) |
3101 */ | 2826 */ |
3102 static int | 2827 static int |
3103 compiler_with(struct compiler *c, stmt_ty s) | 2828 compiler_with(struct compiler *c, stmt_ty s) |
3104 { | 2829 { |
3105 static identifier enter_attr, exit_attr; | 2830 static identifier enter_attr, exit_attr; |
3106 basicblock *block, *finally; | 2831 basicblock *block, *finally; |
3107 identifier tmpvalue = NULL, tmpexit = NULL; | 2832 identifier tmpvalue = NULL; |
3108 | 2833 |
3109 assert(s->kind == With_kind); | 2834 assert(s->kind == With_kind); |
3110 | 2835 |
3111 if (!enter_attr) { | 2836 if (!enter_attr) { |
3112 » enter_attr = PyUnicode_InternFromString("__enter__"); | 2837 » enter_attr = PyString_InternFromString("__enter__"); |
3113 if (!enter_attr) | 2838 if (!enter_attr) |
3114 return 0; | 2839 return 0; |
3115 } | 2840 } |
3116 if (!exit_attr) { | 2841 if (!exit_attr) { |
3117 » exit_attr = PyUnicode_InternFromString("__exit__"); | 2842 » exit_attr = PyString_InternFromString("__exit__"); |
3118 if (!exit_attr) | 2843 if (!exit_attr) |
3119 return 0; | 2844 return 0; |
3120 } | 2845 } |
3121 | 2846 |
3122 block = compiler_new_block(c); | 2847 block = compiler_new_block(c); |
3123 finally = compiler_new_block(c); | 2848 finally = compiler_new_block(c); |
3124 if (!block || !finally) | 2849 if (!block || !finally) |
3125 return 0; | 2850 return 0; |
3126 | 2851 |
3127 if (s->v.With.optional_vars) { | 2852 if (s->v.With.optional_vars) { |
3128 /* Create a temporary variable to hold context.__enter__(). | 2853 /* Create a temporary variable to hold context.__enter__(). |
3129 We need to do this rather than preserving it on the stack | 2854 We need to do this rather than preserving it on the stack |
3130 because SETUP_FINALLY remembers the stack level. | 2855 because SETUP_FINALLY remembers the stack level. |
3131 We need to do the assignment *inside* the try/finally | 2856 We need to do the assignment *inside* the try/finally |
3132 so that context.__exit__() is called when the assignment | 2857 so that context.__exit__() is called when the assignment |
3133 fails. But we need to call context.__enter__() *before* | 2858 fails. But we need to call context.__enter__() *before* |
3134 the try/finally so that if it fails we won't call | 2859 the try/finally so that if it fails we won't call |
3135 context.__exit__(). | 2860 context.__exit__(). |
3136 */ | 2861 */ |
3137 tmpvalue = compiler_new_tmpname(c); | 2862 tmpvalue = compiler_new_tmpname(c); |
3138 if (tmpvalue == NULL) | 2863 if (tmpvalue == NULL) |
3139 return 0; | 2864 return 0; |
3140 PyArena_AddPyObject(c->c_arena, tmpvalue); | 2865 PyArena_AddPyObject(c->c_arena, tmpvalue); |
3141 } | 2866 } |
3142 tmpexit = compiler_new_tmpname(c); | |
3143 if (tmpexit == NULL) | |
3144 return 0; | |
3145 PyArena_AddPyObject(c->c_arena, tmpexit); | |
3146 | 2867 |
3147 /* Evaluate EXPR */ | 2868 /* Evaluate EXPR */ |
3148 VISIT(c, expr, s->v.With.context_expr); | 2869 VISIT(c, expr, s->v.With.context_expr); |
3149 | 2870 |
3150 /* Squirrel away context.__exit__ by stuffing it under context */ | 2871 /* Squirrel away context.__exit__ by stuffing it under context */ |
3151 ADDOP(c, DUP_TOP); | 2872 ADDOP(c, DUP_TOP); |
3152 ADDOP_O(c, LOAD_ATTR, exit_attr, names); | 2873 ADDOP_O(c, LOAD_ATTR, exit_attr, names); |
3153 » if (!compiler_nameop(c, tmpexit, Store)) | 2874 ADDOP(c, ROT_TWO); |
3154 » return 0; | |
3155 | 2875 |
3156 /* Call context.__enter__() */ | 2876 /* Call context.__enter__() */ |
3157 ADDOP_O(c, LOAD_ATTR, enter_attr, names); | 2877 ADDOP_O(c, LOAD_ATTR, enter_attr, names); |
3158 ADDOP_I(c, CALL_FUNCTION, 0); | 2878 ADDOP_I(c, CALL_FUNCTION, 0); |
3159 | 2879 |
3160 if (s->v.With.optional_vars) { | 2880 if (s->v.With.optional_vars) { |
3161 /* Store it in tmpvalue */ | 2881 /* Store it in tmpvalue */ |
3162 if (!compiler_nameop(c, tmpvalue, Store)) | 2882 if (!compiler_nameop(c, tmpvalue, Store)) |
3163 return 0; | 2883 return 0; |
3164 } | 2884 } |
(...skipping 26 matching lines...) Expand all Loading... |
3191 compiler_pop_fblock(c, FINALLY_TRY, block); | 2911 compiler_pop_fblock(c, FINALLY_TRY, block); |
3192 | 2912 |
3193 ADDOP_O(c, LOAD_CONST, Py_None, consts); | 2913 ADDOP_O(c, LOAD_CONST, Py_None, consts); |
3194 compiler_use_next_block(c, finally); | 2914 compiler_use_next_block(c, finally); |
3195 if (!compiler_push_fblock(c, FINALLY_END, finally)) | 2915 if (!compiler_push_fblock(c, FINALLY_END, finally)) |
3196 return 0; | 2916 return 0; |
3197 | 2917 |
3198 /* Finally block starts; context.__exit__ is on the stack under | 2918 /* Finally block starts; context.__exit__ is on the stack under |
3199 the exception or return information. Just issue our magic | 2919 the exception or return information. Just issue our magic |
3200 opcode. */ | 2920 opcode. */ |
3201 if (!compiler_nameop(c, tmpexit, Load) || | |
3202 !compiler_nameop(c, tmpexit, Del)) | |
3203 return 0; | |
3204 ADDOP(c, WITH_CLEANUP); | 2921 ADDOP(c, WITH_CLEANUP); |
3205 | 2922 |
3206 /* Finally block ends. */ | 2923 /* Finally block ends. */ |
3207 ADDOP(c, END_FINALLY); | 2924 ADDOP(c, END_FINALLY); |
3208 compiler_pop_fblock(c, FINALLY_END, finally); | 2925 compiler_pop_fblock(c, FINALLY_END, finally); |
3209 return 1; | 2926 return 1; |
3210 } | 2927 } |
3211 | 2928 |
3212 static int | 2929 static int |
3213 compiler_visit_expr(struct compiler *c, expr_ty e) | 2930 compiler_visit_expr(struct compiler *c, expr_ty e) |
3214 { | 2931 { |
3215 int i, n; | 2932 int i, n; |
3216 | 2933 |
3217 /* If expr e has a different line number than the last expr/stmt, | 2934 /* If expr e has a different line number than the last expr/stmt, |
3218 set a new line number for the next instruction. | 2935 set a new line number for the next instruction. |
3219 */ | 2936 */ |
3220 if (e->lineno > c->u->u_lineno) { | 2937 if (e->lineno > c->u->u_lineno) { |
3221 c->u->u_lineno = e->lineno; | 2938 c->u->u_lineno = e->lineno; |
3222 » » c->u->u_lineno_set = 0; | 2939 » » c->u->u_lineno_set = false; |
3223 } | 2940 } |
3224 switch (e->kind) { | 2941 switch (e->kind) { |
3225 case BoolOp_kind: | 2942 case BoolOp_kind: |
3226 return compiler_boolop(c, e); | 2943 return compiler_boolop(c, e); |
3227 case BinOp_kind: | 2944 case BinOp_kind: |
3228 VISIT(c, expr, e->v.BinOp.left); | 2945 VISIT(c, expr, e->v.BinOp.left); |
3229 VISIT(c, expr, e->v.BinOp.right); | 2946 VISIT(c, expr, e->v.BinOp.right); |
3230 ADDOP(c, binop(c, e->v.BinOp.op)); | 2947 ADDOP(c, binop(c, e->v.BinOp.op)); |
3231 break; | 2948 break; |
3232 case UnaryOp_kind: | 2949 case UnaryOp_kind: |
3233 VISIT(c, expr, e->v.UnaryOp.operand); | 2950 VISIT(c, expr, e->v.UnaryOp.operand); |
3234 ADDOP(c, unaryop(e->v.UnaryOp.op)); | 2951 ADDOP(c, unaryop(e->v.UnaryOp.op)); |
3235 break; | 2952 break; |
3236 case Lambda_kind: | 2953 case Lambda_kind: |
3237 return compiler_lambda(c, e); | 2954 return compiler_lambda(c, e); |
3238 case IfExp_kind: | 2955 case IfExp_kind: |
3239 return compiler_ifexp(c, e); | 2956 return compiler_ifexp(c, e); |
3240 case Dict_kind: | 2957 case Dict_kind: |
3241 n = asdl_seq_LEN(e->v.Dict.values); | 2958 n = asdl_seq_LEN(e->v.Dict.values); |
3242 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n)); | 2959 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n)); |
3243 for (i = 0; i < n; i++) { | 2960 for (i = 0; i < n; i++) { |
3244 VISIT(c, expr,· | 2961 VISIT(c, expr,· |
3245 (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); | 2962 (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
3246 VISIT(c, expr,· | 2963 VISIT(c, expr,· |
3247 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); | 2964 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
3248 ADDOP(c, STORE_MAP); | 2965 ADDOP(c, STORE_MAP); |
3249 } | 2966 } |
3250 break; | 2967 break; |
3251 » case Set_kind: | 2968 » case ListComp_kind: |
3252 » » n = asdl_seq_LEN(e->v.Set.elts); | 2969 » » return compiler_listcomp(c, e); |
3253 » » VISIT_SEQ(c, expr, e->v.Set.elts); | |
3254 » » ADDOP_I(c, BUILD_SET, n); | |
3255 » » break; | |
3256 case GeneratorExp_kind: | 2970 case GeneratorExp_kind: |
3257 return compiler_genexp(c, e); | 2971 return compiler_genexp(c, e); |
3258 case ListComp_kind: | |
3259 return compiler_listcomp(c, e); | |
3260 case SetComp_kind: | |
3261 return compiler_setcomp(c, e); | |
3262 case DictComp_kind: | |
3263 return compiler_dictcomp(c, e); | |
3264 case Yield_kind: | 2972 case Yield_kind: |
3265 if (c->u->u_ste->ste_type != FunctionBlock) | 2973 if (c->u->u_ste->ste_type != FunctionBlock) |
3266 return compiler_error(c, "'yield' outside function"); | 2974 return compiler_error(c, "'yield' outside function"); |
3267 if (e->v.Yield.value) { | 2975 if (e->v.Yield.value) { |
3268 VISIT(c, expr, e->v.Yield.value); | 2976 VISIT(c, expr, e->v.Yield.value); |
3269 } | 2977 } |
3270 else { | 2978 else { |
3271 ADDOP_O(c, LOAD_CONST, Py_None, consts); | 2979 ADDOP_O(c, LOAD_CONST, Py_None, consts); |
3272 } | 2980 } |
3273 ADDOP(c, YIELD_VALUE); | 2981 ADDOP(c, YIELD_VALUE); |
3274 break; | 2982 break; |
3275 case Compare_kind: | 2983 case Compare_kind: |
3276 return compiler_compare(c, e); | 2984 return compiler_compare(c, e); |
3277 case Call_kind: | 2985 case Call_kind: |
3278 return compiler_call(c, e); | 2986 return compiler_call(c, e); |
| 2987 case Repr_kind: |
| 2988 VISIT(c, expr, e->v.Repr.value); |
| 2989 ADDOP(c, UNARY_CONVERT); |
| 2990 break; |
3279 case Num_kind: | 2991 case Num_kind: |
3280 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts); | 2992 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts); |
3281 break; | 2993 break; |
3282 case Str_kind: | 2994 case Str_kind: |
3283 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts); | 2995 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts); |
3284 break; | |
3285 case Bytes_kind: | |
3286 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts); | |
3287 break; | |
3288 case Ellipsis_kind: | |
3289 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts); | |
3290 break; | 2996 break; |
3291 /* The following exprs can be assignment targets. */ | 2997 /* The following exprs can be assignment targets. */ |
3292 case Attribute_kind: | 2998 case Attribute_kind: |
3293 if (e->v.Attribute.ctx != AugStore) | 2999 if (e->v.Attribute.ctx != AugStore) |
3294 VISIT(c, expr, e->v.Attribute.value); | 3000 VISIT(c, expr, e->v.Attribute.value); |
3295 switch (e->v.Attribute.ctx) { | 3001 switch (e->v.Attribute.ctx) { |
3296 case AugLoad: | 3002 case AugLoad: |
3297 ADDOP(c, DUP_TOP); | 3003 ADDOP(c, DUP_TOP); |
3298 /* Fall through to load */ | 3004 /* Fall through to load */ |
3299 case Load: | 3005 case Load: |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3336 VISIT(c, expr, e->v.Subscript.value); | 3042 VISIT(c, expr, e->v.Subscript.value); |
3337 VISIT_SLICE(c, e->v.Subscript.slice, Del); | 3043 VISIT_SLICE(c, e->v.Subscript.slice, Del); |
3338 break; | 3044 break; |
3339 case Param: | 3045 case Param: |
3340 default: | 3046 default: |
3341 PyErr_SetString(PyExc_SystemError, | 3047 PyErr_SetString(PyExc_SystemError, |
3342 "param invalid in subscript expression"); | 3048 "param invalid in subscript expression"); |
3343 return 0; | 3049 return 0; |
3344 } | 3050 } |
3345 break; | 3051 break; |
3346 case Starred_kind: | |
3347 switch (e->v.Starred.ctx) { | |
3348 case Store: | |
3349 /* In all legitimate cases, the Starred node was already
replaced | |
3350 * by compiler_list/compiler_tuple. XXX: is that okay? *
/ | |
3351 return compiler_error(c, | |
3352 "starred assignment target must be in a list or
tuple"); | |
3353 default: | |
3354 return compiler_error(c,· | |
3355 "can use starred expression only as assignment t
arget"); | |
3356 } | |
3357 break; | |
3358 case Name_kind: | 3052 case Name_kind: |
3359 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); | 3053 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
3360 /* child nodes of List and Tuple will have expr_context set */ | 3054 /* child nodes of List and Tuple will have expr_context set */ |
3361 case List_kind: | 3055 case List_kind: |
3362 return compiler_list(c, e); | 3056 return compiler_list(c, e); |
3363 case Tuple_kind: | 3057 case Tuple_kind: |
3364 return compiler_tuple(c, e); | 3058 return compiler_tuple(c, e); |
3365 } | 3059 } |
3366 return 1; | 3060 return 1; |
3367 } | 3061 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3529 | 3223 |
3530 if (s->v.Slice.step) { | 3224 if (s->v.Slice.step) { |
3531 n++; | 3225 n++; |
3532 VISIT(c, expr, s->v.Slice.step); | 3226 VISIT(c, expr, s->v.Slice.step); |
3533 } | 3227 } |
3534 ADDOP_I(c, BUILD_SLICE, n); | 3228 ADDOP_I(c, BUILD_SLICE, n); |
3535 return 1; | 3229 return 1; |
3536 } | 3230 } |
3537 | 3231 |
3538 static int | 3232 static int |
| 3233 compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 3234 { |
| 3235 int op = 0, slice_offset = 0, stack_count = 0; |
| 3236 |
| 3237 assert(s->v.Slice.step == NULL); |
| 3238 if (s->v.Slice.lower) { |
| 3239 slice_offset++; |
| 3240 stack_count++; |
| 3241 if (ctx != AugStore)· |
| 3242 VISIT(c, expr, s->v.Slice.lower); |
| 3243 } |
| 3244 if (s->v.Slice.upper) { |
| 3245 slice_offset += 2; |
| 3246 stack_count++; |
| 3247 if (ctx != AugStore)· |
| 3248 VISIT(c, expr, s->v.Slice.upper); |
| 3249 } |
| 3250 |
| 3251 if (ctx == AugLoad) { |
| 3252 switch (stack_count) { |
| 3253 case 0: ADDOP(c, DUP_TOP); break; |
| 3254 case 1: ADDOP_I(c, DUP_TOPX, 2); break; |
| 3255 case 2: ADDOP_I(c, DUP_TOPX, 3); break; |
| 3256 } |
| 3257 } |
| 3258 else if (ctx == AugStore) { |
| 3259 switch (stack_count) { |
| 3260 case 0: ADDOP(c, ROT_TWO); break; |
| 3261 case 1: ADDOP(c, ROT_THREE); break; |
| 3262 case 2: ADDOP(c, ROT_FOUR); break; |
| 3263 } |
| 3264 } |
| 3265 |
| 3266 switch (ctx) { |
| 3267 case AugLoad: /* fall through to Load */ |
| 3268 case Load: op = SLICE; break; |
| 3269 case AugStore:/* fall through to Store */ |
| 3270 case Store: op = STORE_SLICE; break; |
| 3271 case Del: op = DELETE_SLICE; break; |
| 3272 case Param: |
| 3273 default: |
| 3274 PyErr_SetString(PyExc_SystemError, |
| 3275 "param invalid in simple slice"); |
| 3276 return 0; |
| 3277 } |
| 3278 |
| 3279 ADDOP(c, op + slice_offset); |
| 3280 return 1; |
| 3281 } |
| 3282 |
| 3283 static int |
3539 compiler_visit_nested_slice(struct compiler *c, slice_ty s,· | 3284 compiler_visit_nested_slice(struct compiler *c, slice_ty s,· |
3540 expr_context_ty ctx) | 3285 expr_context_ty ctx) |
3541 { | 3286 { |
3542 switch (s->kind) { | 3287 switch (s->kind) { |
| 3288 case Ellipsis_kind: |
| 3289 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts); |
| 3290 break; |
3543 case Slice_kind: | 3291 case Slice_kind: |
3544 return compiler_slice(c, s, ctx); | 3292 return compiler_slice(c, s, ctx); |
3545 case Index_kind: | 3293 case Index_kind: |
3546 VISIT(c, expr, s->v.Index.value); | 3294 VISIT(c, expr, s->v.Index.value); |
3547 break; | 3295 break; |
3548 case ExtSlice_kind: | 3296 case ExtSlice_kind: |
3549 default: | 3297 default: |
3550 PyErr_SetString(PyExc_SystemError, | 3298 PyErr_SetString(PyExc_SystemError, |
3551 "extended slice invalid in nested slice"); | 3299 "extended slice invalid in nested slice"); |
3552 return 0; | 3300 return 0; |
3553 } | 3301 } |
3554 return 1; | 3302 return 1; |
3555 } | 3303 } |
3556 | 3304 |
3557 static int | 3305 static int |
3558 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) | 3306 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
3559 { | 3307 { |
3560 char * kindname = NULL; | 3308 char * kindname = NULL; |
3561 switch (s->kind) { | 3309 switch (s->kind) { |
3562 case Index_kind: | 3310 case Index_kind: |
3563 kindname = "index"; | 3311 kindname = "index"; |
3564 if (ctx != AugStore) { | 3312 if (ctx != AugStore) { |
3565 VISIT(c, expr, s->v.Index.value); | 3313 VISIT(c, expr, s->v.Index.value); |
3566 } | 3314 } |
3567 break; | 3315 break; |
| 3316 case Ellipsis_kind: |
| 3317 kindname = "ellipsis"; |
| 3318 if (ctx != AugStore) { |
| 3319 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts); |
| 3320 } |
| 3321 break; |
3568 case Slice_kind: | 3322 case Slice_kind: |
3569 kindname = "slice"; | 3323 kindname = "slice"; |
| 3324 if (!s->v.Slice.step)· |
| 3325 return compiler_simple_slice(c, s, ctx); |
3570 if (ctx != AugStore) { | 3326 if (ctx != AugStore) { |
3571 if (!compiler_slice(c, s, ctx)) | 3327 if (!compiler_slice(c, s, ctx)) |
3572 return 0; | 3328 return 0; |
3573 } | 3329 } |
3574 break; | 3330 break; |
3575 case ExtSlice_kind: | 3331 case ExtSlice_kind: |
3576 kindname = "extended slice"; | 3332 kindname = "extended slice"; |
3577 if (ctx != AugStore) { | 3333 if (ctx != AugStore) { |
3578 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims); | 3334 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims); |
3579 for (i = 0; i < n; i++) { | 3335 for (i = 0; i < n; i++) { |
3580 slice_ty sub = (slice_ty)asdl_seq_GET( | 3336 slice_ty sub = (slice_ty)asdl_seq_GET( |
3581 s->v.ExtSlice.dims, i); | 3337 s->v.ExtSlice.dims, i); |
3582 if (!compiler_visit_nested_slice(c, sub, ctx)) | 3338 if (!compiler_visit_nested_slice(c, sub, ctx)) |
3583 return 0; | 3339 return 0; |
3584 } | 3340 } |
3585 ADDOP_I(c, BUILD_TUPLE, n); | 3341 ADDOP_I(c, BUILD_TUPLE, n); |
3586 } | 3342 } |
3587 break; | 3343 break; |
3588 default: | 3344 default: |
3589 PyErr_Format(PyExc_SystemError, | 3345 PyErr_Format(PyExc_SystemError, |
3590 "invalid subscript kind %d", s->kind); | 3346 "invalid subscript kind %d", s->kind); |
3591 return 0; | 3347 return 0; |
3592 } | 3348 } |
3593 return compiler_handle_subscr(c, kindname, ctx); | 3349 return compiler_handle_subscr(c, kindname, ctx); |
3594 } | 3350 } |
| 3351 |
3595 | 3352 |
3596 /* End of the compiler section, beginning of the assembler section */ | 3353 /* End of the compiler section, beginning of the assembler section */ |
3597 | 3354 |
3598 /* do depth-first search of basic block graph, starting with block. | 3355 /* do depth-first search of basic block graph, starting with block. |
3599 post records the block indices in post-order. | 3356 post records the block indices in post-order. |
3600 | 3357 |
3601 XXX must handle implicit jumps from one block to next | 3358 XXX must handle implicit jumps from one block to next |
3602 */ | 3359 */ |
3603 | 3360 |
3604 struct assembler { | 3361 struct assembler { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3678 if (!entryblock) | 3435 if (!entryblock) |
3679 return 0; | 3436 return 0; |
3680 return stackdepth_walk(c, entryblock, 0, 0); | 3437 return stackdepth_walk(c, entryblock, 0, 0); |
3681 } | 3438 } |
3682 | 3439 |
3683 static int | 3440 static int |
3684 assemble_init(struct assembler *a, int nblocks, int firstlineno) | 3441 assemble_init(struct assembler *a, int nblocks, int firstlineno) |
3685 { | 3442 { |
3686 memset(a, 0, sizeof(struct assembler)); | 3443 memset(a, 0, sizeof(struct assembler)); |
3687 a->a_lineno = firstlineno; | 3444 a->a_lineno = firstlineno; |
3688 » a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); | 3445 » a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
3689 if (!a->a_bytecode) | 3446 if (!a->a_bytecode) |
3690 return 0; | 3447 return 0; |
3691 » a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); | 3448 » a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
3692 if (!a->a_lnotab) | 3449 if (!a->a_lnotab) |
3693 return 0; | 3450 return 0; |
3694 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) { | 3451 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) { |
3695 PyErr_NoMemory(); | 3452 PyErr_NoMemory(); |
3696 return 0; | 3453 return 0; |
3697 } | 3454 } |
3698 a->a_postorder = (basicblock **)PyObject_Malloc( | 3455 a->a_postorder = (basicblock **)PyObject_Malloc( |
3699 sizeof(basicblock *) * nblocks); | 3456 sizeof(basicblock *) * nblocks); |
3700 if (!a->a_postorder) { | 3457 if (!a->a_postorder) { |
3701 PyErr_NoMemory(); | 3458 PyErr_NoMemory(); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3794 | 3551 |
3795 assert(d_bytecode >= 0); | 3552 assert(d_bytecode >= 0); |
3796 assert(d_lineno >= 0); | 3553 assert(d_lineno >= 0); |
3797 | 3554 |
3798 if(d_bytecode == 0 && d_lineno == 0) | 3555 if(d_bytecode == 0 && d_lineno == 0) |
3799 return 1; | 3556 return 1; |
3800 | 3557 |
3801 if (d_bytecode > 255) { | 3558 if (d_bytecode > 255) { |
3802 int j, nbytes, ncodes = d_bytecode / 255; | 3559 int j, nbytes, ncodes = d_bytecode / 255; |
3803 nbytes = a->a_lnotab_off + 2 * ncodes; | 3560 nbytes = a->a_lnotab_off + 2 * ncodes; |
3804 » » len = PyBytes_GET_SIZE(a->a_lnotab); | 3561 » » len = PyString_GET_SIZE(a->a_lnotab); |
3805 if (nbytes >= len) { | 3562 if (nbytes >= len) { |
3806 if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) | 3563 if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
3807 len = nbytes; | 3564 len = nbytes; |
3808 else if (len <= INT_MAX / 2) | 3565 else if (len <= INT_MAX / 2) |
3809 len *= 2; | 3566 len *= 2; |
3810 else { | 3567 else { |
3811 PyErr_NoMemory(); | 3568 PyErr_NoMemory(); |
3812 return 0; | 3569 return 0; |
3813 } | 3570 } |
3814 » » » if (_PyBytes_Resize(&a->a_lnotab, len) < 0) | 3571 » » » if (_PyString_Resize(&a->a_lnotab, len) < 0) |
3815 return 0; | 3572 return 0; |
3816 } | 3573 } |
3817 lnotab = (unsigned char *) | 3574 lnotab = (unsigned char *) |
3818 » » » PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; | 3575 » » » PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
3819 for (j = 0; j < ncodes; j++) { | 3576 for (j = 0; j < ncodes; j++) { |
3820 *lnotab++ = 255; | 3577 *lnotab++ = 255; |
3821 *lnotab++ = 0; | 3578 *lnotab++ = 0; |
3822 } | 3579 } |
3823 d_bytecode -= ncodes * 255; | 3580 d_bytecode -= ncodes * 255; |
3824 a->a_lnotab_off += ncodes * 2; | 3581 a->a_lnotab_off += ncodes * 2; |
3825 } | 3582 } |
3826 assert(d_bytecode <= 255); | 3583 assert(d_bytecode <= 255); |
3827 if (d_lineno > 255) { | 3584 if (d_lineno > 255) { |
3828 int j, nbytes, ncodes = d_lineno / 255; | 3585 int j, nbytes, ncodes = d_lineno / 255; |
3829 nbytes = a->a_lnotab_off + 2 * ncodes; | 3586 nbytes = a->a_lnotab_off + 2 * ncodes; |
3830 » » len = PyBytes_GET_SIZE(a->a_lnotab); | 3587 » » len = PyString_GET_SIZE(a->a_lnotab); |
3831 if (nbytes >= len) { | 3588 if (nbytes >= len) { |
3832 if ((len <= INT_MAX / 2) && len * 2 < nbytes) | 3589 if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
3833 len = nbytes; | 3590 len = nbytes; |
3834 else if (len <= INT_MAX / 2) | 3591 else if (len <= INT_MAX / 2) |
3835 len *= 2; | 3592 len *= 2; |
3836 else { | 3593 else { |
3837 PyErr_NoMemory(); | 3594 PyErr_NoMemory(); |
3838 return 0; | 3595 return 0; |
3839 } | 3596 } |
3840 » » » if (_PyBytes_Resize(&a->a_lnotab, len) < 0) | 3597 » » » if (_PyString_Resize(&a->a_lnotab, len) < 0) |
3841 return 0; | 3598 return 0; |
3842 } | 3599 } |
3843 lnotab = (unsigned char *) | 3600 lnotab = (unsigned char *) |
3844 » » » PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; | 3601 » » » PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
3845 *lnotab++ = d_bytecode; | 3602 *lnotab++ = d_bytecode; |
3846 *lnotab++ = 255; | 3603 *lnotab++ = 255; |
3847 d_bytecode = 0; | 3604 d_bytecode = 0; |
3848 for (j = 1; j < ncodes; j++) { | 3605 for (j = 1; j < ncodes; j++) { |
3849 *lnotab++ = 0; | 3606 *lnotab++ = 0; |
3850 *lnotab++ = 255; | 3607 *lnotab++ = 255; |
3851 } | 3608 } |
3852 d_lineno -= ncodes * 255; | 3609 d_lineno -= ncodes * 255; |
3853 a->a_lnotab_off += ncodes * 2; | 3610 a->a_lnotab_off += ncodes * 2; |
3854 } | 3611 } |
3855 | 3612 |
3856 » len = PyBytes_GET_SIZE(a->a_lnotab); | 3613 » len = PyString_GET_SIZE(a->a_lnotab); |
3857 if (a->a_lnotab_off + 2 >= len) { | 3614 if (a->a_lnotab_off + 2 >= len) { |
3858 » » if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) | 3615 » » if (_PyString_Resize(&a->a_lnotab, len * 2) < 0) |
3859 return 0; | 3616 return 0; |
3860 } | 3617 } |
3861 lnotab = (unsigned char *) | 3618 lnotab = (unsigned char *) |
3862 » » » PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; | 3619 » » » PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
3863 | 3620 |
3864 a->a_lnotab_off += 2; | 3621 a->a_lnotab_off += 2; |
3865 if (d_bytecode) { | 3622 if (d_bytecode) { |
3866 *lnotab++ = d_bytecode; | 3623 *lnotab++ = d_bytecode; |
3867 *lnotab++ = d_lineno; | 3624 *lnotab++ = d_lineno; |
3868 } | 3625 } |
3869 else { /* First line of a block; def stmt, etc. */ | 3626 else { /* First line of a block; def stmt, etc. */ |
3870 *lnotab++ = 0; | 3627 *lnotab++ = 0; |
3871 *lnotab++ = d_lineno; | 3628 *lnotab++ = d_lineno; |
3872 } | 3629 } |
3873 a->a_lineno = i->i_lineno; | 3630 a->a_lineno = i->i_lineno; |
3874 a->a_lineno_off = a->a_offset; | 3631 a->a_lineno_off = a->a_offset; |
3875 return 1; | 3632 return 1; |
3876 } | 3633 } |
3877 | 3634 |
3878 /* assemble_emit() | 3635 /* assemble_emit() |
3879 Extend the bytecode with a new instruction. | 3636 Extend the bytecode with a new instruction. |
3880 Update lnotab if necessary. | 3637 Update lnotab if necessary. |
3881 */ | 3638 */ |
3882 | 3639 |
3883 static int | 3640 static int |
3884 assemble_emit(struct assembler *a, struct instr *i) | 3641 assemble_emit(struct assembler *a, struct instr *i) |
3885 { | 3642 { |
3886 int size, arg = 0, ext = 0; | 3643 int size, arg = 0, ext = 0; |
3887 » Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); | 3644 » Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode); |
3888 char *code; | 3645 char *code; |
3889 | 3646 |
3890 size = instrsize(i); | 3647 size = instrsize(i); |
3891 if (i->i_hasarg) { | 3648 if (i->i_hasarg) { |
3892 arg = i->i_oparg; | 3649 arg = i->i_oparg; |
3893 ext = arg >> 16; | 3650 ext = arg >> 16; |
3894 } | 3651 } |
3895 if (i->i_lineno && !assemble_lnotab(a, i)) | 3652 if (i->i_lineno && !assemble_lnotab(a, i)) |
3896 return 0; | 3653 return 0; |
3897 if (a->a_offset + size >= len) { | 3654 if (a->a_offset + size >= len) { |
3898 if (len > PY_SSIZE_T_MAX / 2) | 3655 if (len > PY_SSIZE_T_MAX / 2) |
3899 return 0; | 3656 return 0; |
3900 » » if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) | 3657 » » if (_PyString_Resize(&a->a_bytecode, len * 2) < 0) |
3901 return 0; | 3658 return 0; |
3902 } | 3659 } |
3903 » code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; | 3660 » code = PyString_AS_STRING(a->a_bytecode) + a->a_offset; |
3904 a->a_offset += size; | 3661 a->a_offset += size; |
3905 if (size == 6) { | 3662 if (size == 6) { |
3906 assert(i->i_hasarg); | 3663 assert(i->i_hasarg); |
3907 *code++ = (char)EXTENDED_ARG; | 3664 *code++ = (char)EXTENDED_ARG; |
3908 *code++ = ext & 0xff; | 3665 *code++ = ext & 0xff; |
3909 *code++ = ext >> 8; | 3666 *code++ = ext >> 8; |
3910 arg &= 0xffff; | 3667 arg &= 0xffff; |
3911 } | 3668 } |
3912 *code++ = i->i_opcode; | 3669 *code++ = i->i_opcode; |
3913 if (i->i_hasarg) { | 3670 if (i->i_hasarg) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3984 static PyObject * | 3741 static PyObject * |
3985 dict_keys_inorder(PyObject *dict, int offset) | 3742 dict_keys_inorder(PyObject *dict, int offset) |
3986 { | 3743 { |
3987 PyObject *tuple, *k, *v; | 3744 PyObject *tuple, *k, *v; |
3988 Py_ssize_t i, pos = 0, size = PyDict_Size(dict); | 3745 Py_ssize_t i, pos = 0, size = PyDict_Size(dict); |
3989 | 3746 |
3990 tuple = PyTuple_New(size); | 3747 tuple = PyTuple_New(size); |
3991 if (tuple == NULL) | 3748 if (tuple == NULL) |
3992 return NULL; | 3749 return NULL; |
3993 while (PyDict_Next(dict, &pos, &k, &v)) { | 3750 while (PyDict_Next(dict, &pos, &k, &v)) { |
3994 » » i = PyLong_AS_LONG(v); | 3751 » » i = PyInt_AS_LONG(v); |
3995 /* The keys of the dictionary are tuples. (see compiler_add_o) | 3752 /* The keys of the dictionary are tuples. (see compiler_add_o) |
3996 The object we want is always first, though. */ | 3753 The object we want is always first, though. */ |
3997 k = PyTuple_GET_ITEM(k, 0); | 3754 k = PyTuple_GET_ITEM(k, 0); |
3998 Py_INCREF(k); | 3755 Py_INCREF(k); |
3999 assert((i - offset) < size); | 3756 assert((i - offset) < size); |
4000 assert((i - offset) >= 0); | 3757 assert((i - offset) >= 0); |
4001 PyTuple_SET_ITEM(tuple, i - offset, k); | 3758 PyTuple_SET_ITEM(tuple, i - offset, k); |
4002 } | 3759 } |
4003 return tuple; | 3760 return tuple; |
4004 } | 3761 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4066 varnames = dict_keys_inorder(c->u->u_varnames, 0); | 3823 varnames = dict_keys_inorder(c->u->u_varnames, 0); |
4067 if (!consts || !names || !varnames) | 3824 if (!consts || !names || !varnames) |
4068 goto error; | 3825 goto error; |
4069 ······ | 3826 ······ |
4070 cellvars = dict_keys_inorder(c->u->u_cellvars, 0); | 3827 cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
4071 if (!cellvars) | 3828 if (!cellvars) |
4072 goto error; | 3829 goto error; |
4073 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); | 3830 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); |
4074 if (!freevars) | 3831 if (!freevars) |
4075 goto error; | 3832 goto error; |
4076 » filename = PyUnicode_DecodeFSDefault(c->c_filename); | 3833 » filename = PyString_FromString(c->c_filename); |
4077 if (!filename) | 3834 if (!filename) |
4078 goto error; | 3835 goto error; |
4079 | 3836 |
4080 nlocals = PyDict_Size(c->u->u_varnames); | 3837 nlocals = PyDict_Size(c->u->u_varnames); |
4081 flags = compute_code_flags(c); | 3838 flags = compute_code_flags(c); |
4082 if (flags < 0) | 3839 if (flags < 0) |
4083 goto error; | 3840 goto error; |
4084 | 3841 |
4085 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); | 3842 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
4086 if (!bytecode) | 3843 if (!bytecode) |
4087 goto error; | 3844 goto error; |
4088 | 3845 |
4089 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ | 3846 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
4090 if (!tmp) | 3847 if (!tmp) |
4091 goto error; | 3848 goto error; |
4092 Py_DECREF(consts); | 3849 Py_DECREF(consts); |
4093 consts = tmp; | 3850 consts = tmp; |
4094 | 3851 |
4095 » co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount, | 3852 » co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags, |
4096 » » » nlocals, stackdepth(c), flags, | |
4097 bytecode, consts, names, varnames, | 3853 bytecode, consts, names, varnames, |
4098 freevars, cellvars, | 3854 freevars, cellvars, |
4099 filename, c->u->u_name, | 3855 filename, c->u->u_name, |
4100 c->u->u_firstlineno, | 3856 c->u->u_firstlineno, |
4101 a->a_lnotab); | 3857 a->a_lnotab); |
4102 error: | 3858 error: |
4103 Py_XDECREF(consts); | 3859 Py_XDECREF(consts); |
4104 Py_XDECREF(names); | 3860 Py_XDECREF(names); |
4105 Py_XDECREF(varnames); | 3861 Py_XDECREF(varnames); |
4106 Py_XDECREF(filename); | 3862 Py_XDECREF(filename); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4187 assemble_jump_offsets(&a, c); | 3943 assemble_jump_offsets(&a, c); |
4188 | 3944 |
4189 /* Emit code in reverse postorder from dfs. */ | 3945 /* Emit code in reverse postorder from dfs. */ |
4190 for (i = a.a_nblocks - 1; i >= 0; i--) { | 3946 for (i = a.a_nblocks - 1; i >= 0; i--) { |
4191 b = a.a_postorder[i]; | 3947 b = a.a_postorder[i]; |
4192 for (j = 0; j < b->b_iused; j++) | 3948 for (j = 0; j < b->b_iused; j++) |
4193 if (!assemble_emit(&a, &b->b_instr[j])) | 3949 if (!assemble_emit(&a, &b->b_instr[j])) |
4194 goto error; | 3950 goto error; |
4195 } | 3951 } |
4196 | 3952 |
4197 » if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) | 3953 » if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
4198 goto error; | 3954 goto error; |
4199 » if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0) | 3955 » if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0) |
4200 goto error; | 3956 goto error; |
4201 | 3957 |
4202 co = makecode(c, &a); | 3958 co = makecode(c, &a); |
4203 error: | 3959 error: |
4204 assemble_free(&a); | 3960 assemble_free(&a); |
4205 return co; | 3961 return co; |
4206 } | 3962 } |
LEFT | RIGHT |