]>
Commit | Line | Data |
---|---|---|
c8042e10 DM |
1 | /*\r |
2 | * This file compiles an abstract syntax tree (AST) into Python bytecode.\r | |
3 | *\r | |
4 | * The primary entry point is PyAST_Compile(), which returns a\r | |
5 | * PyCodeObject. The compiler makes several passes to build the code\r | |
6 | * object:\r | |
7 | * 1. Checks for future statements. See future.c\r | |
8 | * 2. Builds a symbol table. See symtable.c.\r | |
9 | * 3. Generate code for basic blocks. See compiler_mod() in this file.\r | |
10 | * 4. Assemble the basic blocks into final code. See assemble() in\r | |
11 | * this file.\r | |
12 | * 5. Optimize the byte code (peephole optimizations). See peephole.c\r | |
13 | *\r | |
14 | * Note that compiler_mod() suggests module, but the module ast type\r | |
15 | * (mod_ty) has cases for expressions and interactive statements.\r | |
16 | *\r | |
17 | * CAUTION: The VISIT_* macros abort the current function when they\r | |
18 | * encounter a problem. So don't invoke them when there is memory\r | |
19 | * which needs to be released. Code blocks are OK, as the compiler\r | |
20 | * structure takes care of releasing those. Use the arena to manage\r | |
21 | * objects.\r | |
22 | */\r | |
23 | \r | |
24 | #include "Python.h"\r | |
25 | \r | |
26 | #include "Python-ast.h"\r | |
27 | #include "node.h"\r | |
28 | #include "pyarena.h"\r | |
29 | #include "ast.h"\r | |
30 | #include "code.h"\r | |
31 | #include "compile.h"\r | |
32 | #include "symtable.h"\r | |
33 | #include "opcode.h"\r | |
34 | \r | |
35 | int Py_OptimizeFlag = 0;\r | |
36 | \r | |
37 | #define DEFAULT_BLOCK_SIZE 16\r | |
38 | #define DEFAULT_BLOCKS 8\r | |
39 | #define DEFAULT_CODE_SIZE 128\r | |
40 | #define DEFAULT_LNOTAB_SIZE 16\r | |
41 | \r | |
42 | #define COMP_GENEXP 0\r | |
43 | #define COMP_SETCOMP 1\r | |
44 | #define COMP_DICTCOMP 2\r | |
45 | \r | |
46 | struct instr {\r | |
47 | unsigned i_jabs : 1;\r | |
48 | unsigned i_jrel : 1;\r | |
49 | unsigned i_hasarg : 1;\r | |
50 | unsigned char i_opcode;\r | |
51 | int i_oparg;\r | |
52 | struct basicblock_ *i_target; /* target block (if jump instruction) */\r | |
53 | int i_lineno;\r | |
54 | };\r | |
55 | \r | |
56 | typedef struct basicblock_ {\r | |
57 | /* Each basicblock in a compilation unit is linked via b_list in the\r | |
58 | reverse order that the block are allocated. b_list points to the next\r | |
59 | block, not to be confused with b_next, which is next by control flow. */\r | |
60 | struct basicblock_ *b_list;\r | |
61 | /* number of instructions used */\r | |
62 | int b_iused;\r | |
63 | /* length of instruction array (b_instr) */\r | |
64 | int b_ialloc;\r | |
65 | /* pointer to an array of instructions, initially NULL */\r | |
66 | struct instr *b_instr;\r | |
67 | /* If b_next is non-NULL, it is a pointer to the next\r | |
68 | block reached by normal control flow. */\r | |
69 | struct basicblock_ *b_next;\r | |
70 | /* b_seen is used to perform a DFS of basicblocks. */\r | |
71 | unsigned b_seen : 1;\r | |
72 | /* b_return is true if a RETURN_VALUE opcode is inserted. */\r | |
73 | unsigned b_return : 1;\r | |
74 | /* depth of stack upon entry of block, computed by stackdepth() */\r | |
75 | int b_startdepth;\r | |
76 | /* instruction offset for block, computed by assemble_jump_offsets() */\r | |
77 | int b_offset;\r | |
78 | } basicblock;\r | |
79 | \r | |
80 | /* fblockinfo tracks the current frame block.\r | |
81 | \r | |
82 | A frame block is used to handle loops, try/except, and try/finally.\r | |
83 | It's called a frame block to distinguish it from a basic block in the\r | |
84 | compiler IR.\r | |
85 | */\r | |
86 | \r | |
87 | enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };\r | |
88 | \r | |
89 | struct fblockinfo {\r | |
90 | enum fblocktype fb_type;\r | |
91 | basicblock *fb_block;\r | |
92 | };\r | |
93 | \r | |
94 | /* The following items change on entry and exit of code blocks.\r | |
95 | They must be saved and restored when returning to a block.\r | |
96 | */\r | |
97 | struct compiler_unit {\r | |
98 | PySTEntryObject *u_ste;\r | |
99 | \r | |
100 | PyObject *u_name;\r | |
101 | /* The following fields are dicts that map objects to\r | |
102 | the index of them in co_XXX. The index is used as\r | |
103 | the argument for opcodes that refer to those collections.\r | |
104 | */\r | |
105 | PyObject *u_consts; /* all constants */\r | |
106 | PyObject *u_names; /* all names */\r | |
107 | PyObject *u_varnames; /* local variables */\r | |
108 | PyObject *u_cellvars; /* cell variables */\r | |
109 | PyObject *u_freevars; /* free variables */\r | |
110 | \r | |
111 | PyObject *u_private; /* for private name mangling */\r | |
112 | \r | |
113 | int u_argcount; /* number of arguments for block */\r | |
114 | /* Pointer to the most recently allocated block. By following b_list\r | |
115 | members, you can reach all early allocated blocks. */\r | |
116 | basicblock *u_blocks;\r | |
117 | basicblock *u_curblock; /* pointer to current block */\r | |
118 | \r | |
119 | int u_nfblocks;\r | |
120 | struct fblockinfo u_fblock[CO_MAXBLOCKS];\r | |
121 | \r | |
122 | int u_firstlineno; /* the first lineno of the block */\r | |
123 | int u_lineno; /* the lineno for the current stmt */\r | |
124 | bool u_lineno_set; /* boolean to indicate whether instr\r | |
125 | has been generated with current lineno */\r | |
126 | };\r | |
127 | \r | |
128 | /* This struct captures the global state of a compilation.\r | |
129 | \r | |
130 | The u pointer points to the current compilation unit, while units\r | |
131 | for enclosing blocks are stored in c_stack. The u and c_stack are\r | |
132 | managed by compiler_enter_scope() and compiler_exit_scope().\r | |
133 | */\r | |
134 | \r | |
135 | struct compiler {\r | |
136 | const char *c_filename;\r | |
137 | struct symtable *c_st;\r | |
138 | PyFutureFeatures *c_future; /* pointer to module's __future__ */\r | |
139 | PyCompilerFlags *c_flags;\r | |
140 | \r | |
141 | int c_interactive; /* true if in interactive mode */\r | |
142 | int c_nestlevel;\r | |
143 | \r | |
144 | struct compiler_unit *u; /* compiler state for current block */\r | |
145 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */\r | |
146 | PyArena *c_arena; /* pointer to memory allocation arena */\r | |
147 | };\r | |
148 | \r | |
149 | static int compiler_enter_scope(struct compiler *, identifier, void *, int);\r | |
150 | static void compiler_free(struct compiler *);\r | |
151 | static basicblock *compiler_new_block(struct compiler *);\r | |
152 | static int compiler_next_instr(struct compiler *, basicblock *);\r | |
153 | static int compiler_addop(struct compiler *, int);\r | |
154 | static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);\r | |
155 | static int compiler_addop_i(struct compiler *, int, int);\r | |
156 | static int compiler_addop_j(struct compiler *, int, basicblock *, int);\r | |
157 | static basicblock *compiler_use_new_block(struct compiler *);\r | |
158 | static int compiler_error(struct compiler *, const char *);\r | |
159 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty);\r | |
160 | \r | |
161 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty);\r | |
162 | static int compiler_visit_stmt(struct compiler *, stmt_ty);\r | |
163 | static int compiler_visit_keyword(struct compiler *, keyword_ty);\r | |
164 | static int compiler_visit_expr(struct compiler *, expr_ty);\r | |
165 | static int compiler_augassign(struct compiler *, stmt_ty);\r | |
166 | static int compiler_visit_slice(struct compiler *, slice_ty,\r | |
167 | expr_context_ty);\r | |
168 | \r | |
169 | static int compiler_push_fblock(struct compiler *, enum fblocktype,\r | |
170 | basicblock *);\r | |
171 | static void compiler_pop_fblock(struct compiler *, enum fblocktype,\r | |
172 | basicblock *);\r | |
173 | /* Returns true if there is a loop on the fblock stack. */\r | |
174 | static int compiler_in_loop(struct compiler *);\r | |
175 | \r | |
176 | static int inplace_binop(struct compiler *, operator_ty);\r | |
177 | static int expr_constant(expr_ty e);\r | |
178 | \r | |
179 | static int compiler_with(struct compiler *, stmt_ty);\r | |
180 | \r | |
181 | static PyCodeObject *assemble(struct compiler *, int addNone);\r | |
182 | static PyObject *__doc__;\r | |
183 | \r | |
184 | #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"\r | |
185 | \r | |
186 | PyObject *\r | |
187 | _Py_Mangle(PyObject *privateobj, PyObject *ident)\r | |
188 | {\r | |
189 | /* Name mangling: __private becomes _classname__private.\r | |
190 | This is independent from how the name is used. */\r | |
191 | const char *p, *name = PyString_AsString(ident);\r | |
192 | char *buffer;\r | |
193 | size_t nlen, plen;\r | |
194 | if (privateobj == NULL || !PyString_Check(privateobj) ||\r | |
195 | name == NULL || name[0] != '_' || name[1] != '_') {\r | |
196 | Py_INCREF(ident);\r | |
197 | return ident;\r | |
198 | }\r | |
199 | p = PyString_AsString(privateobj);\r | |
200 | nlen = strlen(name);\r | |
201 | /* Don't mangle __id__ or names with dots.\r | |
202 | \r | |
203 | The only time a name with a dot can occur is when\r | |
204 | we are compiling an import statement that has a\r | |
205 | package name.\r | |
206 | \r | |
207 | TODO(jhylton): Decide whether we want to support\r | |
208 | mangling of the module name, e.g. __M.X.\r | |
209 | */\r | |
210 | if ((name[nlen-1] == '_' && name[nlen-2] == '_')\r | |
211 | || strchr(name, '.')) {\r | |
212 | Py_INCREF(ident);\r | |
213 | return ident; /* Don't mangle __whatever__ */\r | |
214 | }\r | |
215 | /* Strip leading underscores from class name */\r | |
216 | while (*p == '_')\r | |
217 | p++;\r | |
218 | if (*p == '\0') {\r | |
219 | Py_INCREF(ident);\r | |
220 | return ident; /* Don't mangle if class is just underscores */\r | |
221 | }\r | |
222 | plen = strlen(p);\r | |
223 | \r | |
224 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) {\r | |
225 | PyErr_SetString(PyExc_OverflowError,\r | |
226 | "private identifier too large to be mangled");\r | |
227 | return NULL;\r | |
228 | }\r | |
229 | \r | |
230 | ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);\r | |
231 | if (!ident)\r | |
232 | return 0;\r | |
233 | /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */\r | |
234 | buffer = PyString_AS_STRING(ident);\r | |
235 | buffer[0] = '_';\r | |
236 | strncpy(buffer+1, p, plen);\r | |
237 | strcpy(buffer+1+plen, name);\r | |
238 | return ident;\r | |
239 | }\r | |
240 | \r | |
241 | static int\r | |
242 | compiler_init(struct compiler *c)\r | |
243 | {\r | |
244 | memset(c, 0, sizeof(struct compiler));\r | |
245 | \r | |
246 | c->c_stack = PyList_New(0);\r | |
247 | if (!c->c_stack)\r | |
248 | return 0;\r | |
249 | \r | |
250 | return 1;\r | |
251 | }\r | |
252 | \r | |
253 | PyCodeObject *\r | |
254 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,\r | |
255 | PyArena *arena)\r | |
256 | {\r | |
257 | struct compiler c;\r | |
258 | PyCodeObject *co = NULL;\r | |
259 | PyCompilerFlags local_flags;\r | |
260 | int merged;\r | |
261 | \r | |
262 | if (!__doc__) {\r | |
263 | __doc__ = PyString_InternFromString("__doc__");\r | |
264 | if (!__doc__)\r | |
265 | return NULL;\r | |
266 | }\r | |
267 | \r | |
268 | if (!compiler_init(&c))\r | |
269 | return NULL;\r | |
270 | c.c_filename = filename;\r | |
271 | c.c_arena = arena;\r | |
272 | c.c_future = PyFuture_FromAST(mod, filename);\r | |
273 | if (c.c_future == NULL)\r | |
274 | goto finally;\r | |
275 | if (!flags) {\r | |
276 | local_flags.cf_flags = 0;\r | |
277 | flags = &local_flags;\r | |
278 | }\r | |
279 | merged = c.c_future->ff_features | flags->cf_flags;\r | |
280 | c.c_future->ff_features = merged;\r | |
281 | flags->cf_flags = merged;\r | |
282 | c.c_flags = flags;\r | |
283 | c.c_nestlevel = 0;\r | |
284 | \r | |
285 | c.c_st = PySymtable_Build(mod, filename, c.c_future);\r | |
286 | if (c.c_st == NULL) {\r | |
287 | if (!PyErr_Occurred())\r | |
288 | PyErr_SetString(PyExc_SystemError, "no symtable");\r | |
289 | goto finally;\r | |
290 | }\r | |
291 | \r | |
292 | co = compiler_mod(&c, mod);\r | |
293 | \r | |
294 | finally:\r | |
295 | compiler_free(&c);\r | |
296 | assert(co || PyErr_Occurred());\r | |
297 | return co;\r | |
298 | }\r | |
299 | \r | |
300 | PyCodeObject *\r | |
301 | PyNode_Compile(struct _node *n, const char *filename)\r | |
302 | {\r | |
303 | PyCodeObject *co = NULL;\r | |
304 | mod_ty mod;\r | |
305 | PyArena *arena = PyArena_New();\r | |
306 | if (!arena)\r | |
307 | return NULL;\r | |
308 | mod = PyAST_FromNode(n, NULL, filename, arena);\r | |
309 | if (mod)\r | |
310 | co = PyAST_Compile(mod, filename, NULL, arena);\r | |
311 | PyArena_Free(arena);\r | |
312 | return co;\r | |
313 | }\r | |
314 | \r | |
315 | static void\r | |
316 | compiler_free(struct compiler *c)\r | |
317 | {\r | |
318 | if (c->c_st)\r | |
319 | PySymtable_Free(c->c_st);\r | |
320 | if (c->c_future)\r | |
321 | PyObject_Free(c->c_future);\r | |
322 | Py_DECREF(c->c_stack);\r | |
323 | }\r | |
324 | \r | |
325 | static PyObject *\r | |
326 | list2dict(PyObject *list)\r | |
327 | {\r | |
328 | Py_ssize_t i, n;\r | |
329 | PyObject *v, *k;\r | |
330 | PyObject *dict = PyDict_New();\r | |
331 | if (!dict) return NULL;\r | |
332 | \r | |
333 | n = PyList_Size(list);\r | |
334 | for (i = 0; i < n; i++) {\r | |
335 | v = PyInt_FromLong(i);\r | |
336 | if (!v) {\r | |
337 | Py_DECREF(dict);\r | |
338 | return NULL;\r | |
339 | }\r | |
340 | k = PyList_GET_ITEM(list, i);\r | |
341 | k = PyTuple_Pack(2, k, k->ob_type);\r | |
342 | if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {\r | |
343 | Py_XDECREF(k);\r | |
344 | Py_DECREF(v);\r | |
345 | Py_DECREF(dict);\r | |
346 | return NULL;\r | |
347 | }\r | |
348 | Py_DECREF(k);\r | |
349 | Py_DECREF(v);\r | |
350 | }\r | |
351 | return dict;\r | |
352 | }\r | |
353 | \r | |
354 | /* Return new dict containing names from src that match scope(s).\r | |
355 | \r | |
356 | src is a symbol table dictionary. If the scope of a name matches\r | |
357 | either scope_type or flag is set, insert it into the new dict. The\r | |
358 | values are integers, starting at offset and increasing by one for\r | |
359 | each key.\r | |
360 | */\r | |
361 | \r | |
362 | static PyObject *\r | |
363 | dictbytype(PyObject *src, int scope_type, int flag, int offset)\r | |
364 | {\r | |
365 | Py_ssize_t i = offset, scope, num_keys, key_i;\r | |
366 | PyObject *k, *v, *dest = PyDict_New();\r | |
367 | PyObject *sorted_keys;\r | |
368 | \r | |
369 | assert(offset >= 0);\r | |
370 | if (dest == NULL)\r | |
371 | return NULL;\r | |
372 | \r | |
373 | /* Sort the keys so that we have a deterministic order on the indexes\r | |
374 | saved in the returned dictionary. These indexes are used as indexes\r | |
375 | into the free and cell var storage. Therefore if they aren't\r | |
376 | deterministic, then the generated bytecode is not deterministic.\r | |
377 | */\r | |
378 | sorted_keys = PyDict_Keys(src);\r | |
379 | if (sorted_keys == NULL)\r | |
380 | return NULL;\r | |
381 | if (PyList_Sort(sorted_keys) != 0) {\r | |
382 | Py_DECREF(sorted_keys);\r | |
383 | return NULL;\r | |
384 | }\r | |
385 | num_keys = PyList_GET_SIZE(sorted_keys);\r | |
386 | \r | |
387 | for (key_i = 0; key_i < num_keys; key_i++) {\r | |
388 | k = PyList_GET_ITEM(sorted_keys, key_i);\r | |
389 | v = PyDict_GetItem(src, k);\r | |
390 | /* XXX this should probably be a macro in symtable.h */\r | |
391 | assert(PyInt_Check(v));\r | |
392 | scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;\r | |
393 | \r | |
394 | if (scope == scope_type || PyInt_AS_LONG(v) & flag) {\r | |
395 | PyObject *tuple, *item = PyInt_FromLong(i);\r | |
396 | if (item == NULL) {\r | |
397 | Py_DECREF(sorted_keys);\r | |
398 | Py_DECREF(dest);\r | |
399 | return NULL;\r | |
400 | }\r | |
401 | i++;\r | |
402 | tuple = PyTuple_Pack(2, k, k->ob_type);\r | |
403 | if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {\r | |
404 | Py_DECREF(sorted_keys);\r | |
405 | Py_DECREF(item);\r | |
406 | Py_DECREF(dest);\r | |
407 | Py_XDECREF(tuple);\r | |
408 | return NULL;\r | |
409 | }\r | |
410 | Py_DECREF(item);\r | |
411 | Py_DECREF(tuple);\r | |
412 | }\r | |
413 | }\r | |
414 | Py_DECREF(sorted_keys);\r | |
415 | return dest;\r | |
416 | }\r | |
417 | \r | |
418 | static void\r | |
419 | compiler_unit_check(struct compiler_unit *u)\r | |
420 | {\r | |
421 | basicblock *block;\r | |
422 | for (block = u->u_blocks; block != NULL; block = block->b_list) {\r | |
423 | assert((void *)block != (void *)0xcbcbcbcb);\r | |
424 | assert((void *)block != (void *)0xfbfbfbfb);\r | |
425 | assert((void *)block != (void *)0xdbdbdbdb);\r | |
426 | if (block->b_instr != NULL) {\r | |
427 | assert(block->b_ialloc > 0);\r | |
428 | assert(block->b_iused > 0);\r | |
429 | assert(block->b_ialloc >= block->b_iused);\r | |
430 | }\r | |
431 | else {\r | |
432 | assert (block->b_iused == 0);\r | |
433 | assert (block->b_ialloc == 0);\r | |
434 | }\r | |
435 | }\r | |
436 | }\r | |
437 | \r | |
438 | static void\r | |
439 | compiler_unit_free(struct compiler_unit *u)\r | |
440 | {\r | |
441 | basicblock *b, *next;\r | |
442 | \r | |
443 | compiler_unit_check(u);\r | |
444 | b = u->u_blocks;\r | |
445 | while (b != NULL) {\r | |
446 | if (b->b_instr)\r | |
447 | PyObject_Free((void *)b->b_instr);\r | |
448 | next = b->b_list;\r | |
449 | PyObject_Free((void *)b);\r | |
450 | b = next;\r | |
451 | }\r | |
452 | Py_CLEAR(u->u_ste);\r | |
453 | Py_CLEAR(u->u_name);\r | |
454 | Py_CLEAR(u->u_consts);\r | |
455 | Py_CLEAR(u->u_names);\r | |
456 | Py_CLEAR(u->u_varnames);\r | |
457 | Py_CLEAR(u->u_freevars);\r | |
458 | Py_CLEAR(u->u_cellvars);\r | |
459 | Py_CLEAR(u->u_private);\r | |
460 | PyObject_Free(u);\r | |
461 | }\r | |
462 | \r | |
463 | static int\r | |
464 | compiler_enter_scope(struct compiler *c, identifier name, void *key,\r | |
465 | int lineno)\r | |
466 | {\r | |
467 | struct compiler_unit *u;\r | |
468 | \r | |
469 | u = (struct compiler_unit *)PyObject_Malloc(sizeof(\r | |
470 | struct compiler_unit));\r | |
471 | if (!u) {\r | |
472 | PyErr_NoMemory();\r | |
473 | return 0;\r | |
474 | }\r | |
475 | memset(u, 0, sizeof(struct compiler_unit));\r | |
476 | u->u_argcount = 0;\r | |
477 | u->u_ste = PySymtable_Lookup(c->c_st, key);\r | |
478 | if (!u->u_ste) {\r | |
479 | compiler_unit_free(u);\r | |
480 | return 0;\r | |
481 | }\r | |
482 | Py_INCREF(name);\r | |
483 | u->u_name = name;\r | |
484 | u->u_varnames = list2dict(u->u_ste->ste_varnames);\r | |
485 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);\r | |
486 | if (!u->u_varnames || !u->u_cellvars) {\r | |
487 | compiler_unit_free(u);\r | |
488 | return 0;\r | |
489 | }\r | |
490 | \r | |
491 | u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,\r | |
492 | PyDict_Size(u->u_cellvars));\r | |
493 | if (!u->u_freevars) {\r | |
494 | compiler_unit_free(u);\r | |
495 | return 0;\r | |
496 | }\r | |
497 | \r | |
498 | u->u_blocks = NULL;\r | |
499 | u->u_nfblocks = 0;\r | |
500 | u->u_firstlineno = lineno;\r | |
501 | u->u_lineno = 0;\r | |
502 | u->u_lineno_set = false;\r | |
503 | u->u_consts = PyDict_New();\r | |
504 | if (!u->u_consts) {\r | |
505 | compiler_unit_free(u);\r | |
506 | return 0;\r | |
507 | }\r | |
508 | u->u_names = PyDict_New();\r | |
509 | if (!u->u_names) {\r | |
510 | compiler_unit_free(u);\r | |
511 | return 0;\r | |
512 | }\r | |
513 | \r | |
514 | u->u_private = NULL;\r | |
515 | \r | |
516 | /* Push the old compiler_unit on the stack. */\r | |
517 | if (c->u) {\r | |
518 | PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);\r | |
519 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {\r | |
520 | Py_XDECREF(capsule);\r | |
521 | compiler_unit_free(u);\r | |
522 | return 0;\r | |
523 | }\r | |
524 | Py_DECREF(capsule);\r | |
525 | u->u_private = c->u->u_private;\r | |
526 | Py_XINCREF(u->u_private);\r | |
527 | }\r | |
528 | c->u = u;\r | |
529 | \r | |
530 | c->c_nestlevel++;\r | |
531 | if (compiler_use_new_block(c) == NULL)\r | |
532 | return 0;\r | |
533 | \r | |
534 | return 1;\r | |
535 | }\r | |
536 | \r | |
537 | static void\r | |
538 | compiler_exit_scope(struct compiler *c)\r | |
539 | {\r | |
540 | int n;\r | |
541 | PyObject *capsule;\r | |
542 | \r | |
543 | c->c_nestlevel--;\r | |
544 | compiler_unit_free(c->u);\r | |
545 | /* Restore c->u to the parent unit. */\r | |
546 | n = PyList_GET_SIZE(c->c_stack) - 1;\r | |
547 | if (n >= 0) {\r | |
548 | capsule = PyList_GET_ITEM(c->c_stack, n);\r | |
549 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);\r | |
550 | assert(c->u);\r | |
551 | /* we are deleting from a list so this really shouldn't fail */\r | |
552 | if (PySequence_DelItem(c->c_stack, n) < 0)\r | |
553 | Py_FatalError("compiler_exit_scope()");\r | |
554 | compiler_unit_check(c->u);\r | |
555 | }\r | |
556 | else\r | |
557 | c->u = NULL;\r | |
558 | \r | |
559 | }\r | |
560 | \r | |
561 | /* Allocate a new block and return a pointer to it.\r | |
562 | Returns NULL on error.\r | |
563 | */\r | |
564 | \r | |
565 | static basicblock *\r | |
566 | compiler_new_block(struct compiler *c)\r | |
567 | {\r | |
568 | basicblock *b;\r | |
569 | struct compiler_unit *u;\r | |
570 | \r | |
571 | u = c->u;\r | |
572 | b = (basicblock *)PyObject_Malloc(sizeof(basicblock));\r | |
573 | if (b == NULL) {\r | |
574 | PyErr_NoMemory();\r | |
575 | return NULL;\r | |
576 | }\r | |
577 | memset((void *)b, 0, sizeof(basicblock));\r | |
578 | /* Extend the singly linked list of blocks with new block. */\r | |
579 | b->b_list = u->u_blocks;\r | |
580 | u->u_blocks = b;\r | |
581 | return b;\r | |
582 | }\r | |
583 | \r | |
584 | static basicblock *\r | |
585 | compiler_use_new_block(struct compiler *c)\r | |
586 | {\r | |
587 | basicblock *block = compiler_new_block(c);\r | |
588 | if (block == NULL)\r | |
589 | return NULL;\r | |
590 | c->u->u_curblock = block;\r | |
591 | return block;\r | |
592 | }\r | |
593 | \r | |
594 | static basicblock *\r | |
595 | compiler_next_block(struct compiler *c)\r | |
596 | {\r | |
597 | basicblock *block = compiler_new_block(c);\r | |
598 | if (block == NULL)\r | |
599 | return NULL;\r | |
600 | c->u->u_curblock->b_next = block;\r | |
601 | c->u->u_curblock = block;\r | |
602 | return block;\r | |
603 | }\r | |
604 | \r | |
605 | static basicblock *\r | |
606 | compiler_use_next_block(struct compiler *c, basicblock *block)\r | |
607 | {\r | |
608 | assert(block != NULL);\r | |
609 | c->u->u_curblock->b_next = block;\r | |
610 | c->u->u_curblock = block;\r | |
611 | return block;\r | |
612 | }\r | |
613 | \r | |
614 | /* Returns the offset of the next instruction in the current block's\r | |
615 | b_instr array. Resizes the b_instr as necessary.\r | |
616 | Returns -1 on failure.\r | |
617 | */\r | |
618 | \r | |
619 | static int\r | |
620 | compiler_next_instr(struct compiler *c, basicblock *b)\r | |
621 | {\r | |
622 | assert(b != NULL);\r | |
623 | if (b->b_instr == NULL) {\r | |
624 | b->b_instr = (struct instr *)PyObject_Malloc(\r | |
625 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE);\r | |
626 | if (b->b_instr == NULL) {\r | |
627 | PyErr_NoMemory();\r | |
628 | return -1;\r | |
629 | }\r | |
630 | b->b_ialloc = DEFAULT_BLOCK_SIZE;\r | |
631 | memset((char *)b->b_instr, 0,\r | |
632 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE);\r | |
633 | }\r | |
634 | else if (b->b_iused == b->b_ialloc) {\r | |
635 | struct instr *tmp;\r | |
636 | size_t oldsize, newsize;\r | |
637 | oldsize = b->b_ialloc * sizeof(struct instr);\r | |
638 | newsize = oldsize << 1;\r | |
639 | \r | |
640 | if (oldsize > (PY_SIZE_MAX >> 1)) {\r | |
641 | PyErr_NoMemory();\r | |
642 | return -1;\r | |
643 | }\r | |
644 | \r | |
645 | if (newsize == 0) {\r | |
646 | PyErr_NoMemory();\r | |
647 | return -1;\r | |
648 | }\r | |
649 | b->b_ialloc <<= 1;\r | |
650 | tmp = (struct instr *)PyObject_Realloc(\r | |
651 | (void *)b->b_instr, newsize);\r | |
652 | if (tmp == NULL) {\r | |
653 | PyErr_NoMemory();\r | |
654 | return -1;\r | |
655 | }\r | |
656 | b->b_instr = tmp;\r | |
657 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);\r | |
658 | }\r | |
659 | return b->b_iused++;\r | |
660 | }\r | |
661 | \r | |
662 | /* Set the i_lineno member of the instruction at offset off if the\r | |
663 | line number for the current expression/statement has not\r | |
664 | already been set. If it has been set, the call has no effect.\r | |
665 | \r | |
666 | The line number is reset in the following cases:\r | |
667 | - when entering a new scope\r | |
668 | - on each statement\r | |
669 | - on each expression that start a new line\r | |
670 | - before the "except" clause\r | |
671 | - before the "for" and "while" expressions\r | |
672 | */\r | |
673 | \r | |
674 | static void\r | |
675 | compiler_set_lineno(struct compiler *c, int off)\r | |
676 | {\r | |
677 | basicblock *b;\r | |
678 | if (c->u->u_lineno_set)\r | |
679 | return;\r | |
680 | c->u->u_lineno_set = true;\r | |
681 | b = c->u->u_curblock;\r | |
682 | b->b_instr[off].i_lineno = c->u->u_lineno;\r | |
683 | }\r | |
684 | \r | |
685 | static int\r | |
686 | opcode_stack_effect(int opcode, int oparg)\r | |
687 | {\r | |
688 | switch (opcode) {\r | |
689 | case POP_TOP:\r | |
690 | return -1;\r | |
691 | case ROT_TWO:\r | |
692 | case ROT_THREE:\r | |
693 | return 0;\r | |
694 | case DUP_TOP:\r | |
695 | return 1;\r | |
696 | case ROT_FOUR:\r | |
697 | return 0;\r | |
698 | \r | |
699 | case UNARY_POSITIVE:\r | |
700 | case UNARY_NEGATIVE:\r | |
701 | case UNARY_NOT:\r | |
702 | case UNARY_CONVERT:\r | |
703 | case UNARY_INVERT:\r | |
704 | return 0;\r | |
705 | \r | |
706 | case SET_ADD:\r | |
707 | case LIST_APPEND:\r | |
708 | return -1;\r | |
709 | \r | |
710 | case MAP_ADD:\r | |
711 | return -2;\r | |
712 | \r | |
713 | case BINARY_POWER:\r | |
714 | case BINARY_MULTIPLY:\r | |
715 | case BINARY_DIVIDE:\r | |
716 | case BINARY_MODULO:\r | |
717 | case BINARY_ADD:\r | |
718 | case BINARY_SUBTRACT:\r | |
719 | case BINARY_SUBSCR:\r | |
720 | case BINARY_FLOOR_DIVIDE:\r | |
721 | case BINARY_TRUE_DIVIDE:\r | |
722 | return -1;\r | |
723 | case INPLACE_FLOOR_DIVIDE:\r | |
724 | case INPLACE_TRUE_DIVIDE:\r | |
725 | return -1;\r | |
726 | \r | |
727 | case SLICE+0:\r | |
728 | return 0;\r | |
729 | case SLICE+1:\r | |
730 | return -1;\r | |
731 | case SLICE+2:\r | |
732 | return -1;\r | |
733 | case SLICE+3:\r | |
734 | return -2;\r | |
735 | \r | |
736 | case STORE_SLICE+0:\r | |
737 | return -2;\r | |
738 | case STORE_SLICE+1:\r | |
739 | return -3;\r | |
740 | case STORE_SLICE+2:\r | |
741 | return -3;\r | |
742 | case STORE_SLICE+3:\r | |
743 | return -4;\r | |
744 | \r | |
745 | case DELETE_SLICE+0:\r | |
746 | return -1;\r | |
747 | case DELETE_SLICE+1:\r | |
748 | return -2;\r | |
749 | case DELETE_SLICE+2:\r | |
750 | return -2;\r | |
751 | case DELETE_SLICE+3:\r | |
752 | return -3;\r | |
753 | \r | |
754 | case INPLACE_ADD:\r | |
755 | case INPLACE_SUBTRACT:\r | |
756 | case INPLACE_MULTIPLY:\r | |
757 | case INPLACE_DIVIDE:\r | |
758 | case INPLACE_MODULO:\r | |
759 | return -1;\r | |
760 | case STORE_SUBSCR:\r | |
761 | return -3;\r | |
762 | case STORE_MAP:\r | |
763 | return -2;\r | |
764 | case DELETE_SUBSCR:\r | |
765 | return -2;\r | |
766 | \r | |
767 | case BINARY_LSHIFT:\r | |
768 | case BINARY_RSHIFT:\r | |
769 | case BINARY_AND:\r | |
770 | case BINARY_XOR:\r | |
771 | case BINARY_OR:\r | |
772 | return -1;\r | |
773 | case INPLACE_POWER:\r | |
774 | return -1;\r | |
775 | case GET_ITER:\r | |
776 | return 0;\r | |
777 | \r | |
778 | case PRINT_EXPR:\r | |
779 | return -1;\r | |
780 | case PRINT_ITEM:\r | |
781 | return -1;\r | |
782 | case PRINT_NEWLINE:\r | |
783 | return 0;\r | |
784 | case PRINT_ITEM_TO:\r | |
785 | return -2;\r | |
786 | case PRINT_NEWLINE_TO:\r | |
787 | return -1;\r | |
788 | case INPLACE_LSHIFT:\r | |
789 | case INPLACE_RSHIFT:\r | |
790 | case INPLACE_AND:\r | |
791 | case INPLACE_XOR:\r | |
792 | case INPLACE_OR:\r | |
793 | return -1;\r | |
794 | case BREAK_LOOP:\r | |
795 | return 0;\r | |
796 | case SETUP_WITH:\r | |
797 | return 4;\r | |
798 | case WITH_CLEANUP:\r | |
799 | return -1; /* XXX Sometimes more */\r | |
800 | case LOAD_LOCALS:\r | |
801 | return 1;\r | |
802 | case RETURN_VALUE:\r | |
803 | return -1;\r | |
804 | case IMPORT_STAR:\r | |
805 | return -1;\r | |
806 | case EXEC_STMT:\r | |
807 | return -3;\r | |
808 | case YIELD_VALUE:\r | |
809 | return 0;\r | |
810 | \r | |
811 | case POP_BLOCK:\r | |
812 | return 0;\r | |
813 | case END_FINALLY:\r | |
814 | return -3; /* or -1 or -2 if no exception occurred or\r | |
815 | return/break/continue */\r | |
816 | case BUILD_CLASS:\r | |
817 | return -2;\r | |
818 | \r | |
819 | case STORE_NAME:\r | |
820 | return -1;\r | |
821 | case DELETE_NAME:\r | |
822 | return 0;\r | |
823 | case UNPACK_SEQUENCE:\r | |
824 | return oparg-1;\r | |
825 | case FOR_ITER:\r | |
826 | return 1; /* or -1, at end of iterator */\r | |
827 | \r | |
828 | case STORE_ATTR:\r | |
829 | return -2;\r | |
830 | case DELETE_ATTR:\r | |
831 | return -1;\r | |
832 | case STORE_GLOBAL:\r | |
833 | return -1;\r | |
834 | case DELETE_GLOBAL:\r | |
835 | return 0;\r | |
836 | case DUP_TOPX:\r | |
837 | return oparg;\r | |
838 | case LOAD_CONST:\r | |
839 | return 1;\r | |
840 | case LOAD_NAME:\r | |
841 | return 1;\r | |
842 | case BUILD_TUPLE:\r | |
843 | case BUILD_LIST:\r | |
844 | case BUILD_SET:\r | |
845 | return 1-oparg;\r | |
846 | case BUILD_MAP:\r | |
847 | return 1;\r | |
848 | case LOAD_ATTR:\r | |
849 | return 0;\r | |
850 | case COMPARE_OP:\r | |
851 | return -1;\r | |
852 | case IMPORT_NAME:\r | |
853 | return -1;\r | |
854 | case IMPORT_FROM:\r | |
855 | return 1;\r | |
856 | \r | |
857 | case JUMP_FORWARD:\r | |
858 | case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */\r | |
859 | case JUMP_IF_FALSE_OR_POP: /* "" */\r | |
860 | case JUMP_ABSOLUTE:\r | |
861 | return 0;\r | |
862 | \r | |
863 | case POP_JUMP_IF_FALSE:\r | |
864 | case POP_JUMP_IF_TRUE:\r | |
865 | return -1;\r | |
866 | \r | |
867 | case LOAD_GLOBAL:\r | |
868 | return 1;\r | |
869 | \r | |
870 | case CONTINUE_LOOP:\r | |
871 | return 0;\r | |
872 | case SETUP_LOOP:\r | |
873 | case SETUP_EXCEPT:\r | |
874 | case SETUP_FINALLY:\r | |
875 | return 0;\r | |
876 | \r | |
877 | case LOAD_FAST:\r | |
878 | return 1;\r | |
879 | case STORE_FAST:\r | |
880 | return -1;\r | |
881 | case DELETE_FAST:\r | |
882 | return 0;\r | |
883 | \r | |
884 | case RAISE_VARARGS:\r | |
885 | return -oparg;\r | |
886 | #define NARGS(o) (((o) % 256) + 2*((o) / 256))\r | |
887 | case CALL_FUNCTION:\r | |
888 | return -NARGS(oparg);\r | |
889 | case CALL_FUNCTION_VAR:\r | |
890 | case CALL_FUNCTION_KW:\r | |
891 | return -NARGS(oparg)-1;\r | |
892 | case CALL_FUNCTION_VAR_KW:\r | |
893 | return -NARGS(oparg)-2;\r | |
894 | #undef NARGS\r | |
895 | case MAKE_FUNCTION:\r | |
896 | return -oparg;\r | |
897 | case BUILD_SLICE:\r | |
898 | if (oparg == 3)\r | |
899 | return -2;\r | |
900 | else\r | |
901 | return -1;\r | |
902 | \r | |
903 | case MAKE_CLOSURE:\r | |
904 | return -oparg-1;\r | |
905 | case LOAD_CLOSURE:\r | |
906 | return 1;\r | |
907 | case LOAD_DEREF:\r | |
908 | return 1;\r | |
909 | case STORE_DEREF:\r | |
910 | return -1;\r | |
911 | default:\r | |
912 | fprintf(stderr, "opcode = %d\n", opcode);\r | |
913 | Py_FatalError("opcode_stack_effect()");\r | |
914 | \r | |
915 | }\r | |
916 | return 0; /* not reachable */\r | |
917 | }\r | |
918 | \r | |
919 | /* Add an opcode with no argument.\r | |
920 | Returns 0 on failure, 1 on success.\r | |
921 | */\r | |
922 | \r | |
923 | static int\r | |
924 | compiler_addop(struct compiler *c, int opcode)\r | |
925 | {\r | |
926 | basicblock *b;\r | |
927 | struct instr *i;\r | |
928 | int off;\r | |
929 | off = compiler_next_instr(c, c->u->u_curblock);\r | |
930 | if (off < 0)\r | |
931 | return 0;\r | |
932 | b = c->u->u_curblock;\r | |
933 | i = &b->b_instr[off];\r | |
934 | i->i_opcode = opcode;\r | |
935 | i->i_hasarg = 0;\r | |
936 | if (opcode == RETURN_VALUE)\r | |
937 | b->b_return = 1;\r | |
938 | compiler_set_lineno(c, off);\r | |
939 | return 1;\r | |
940 | }\r | |
941 | \r | |
942 | static int\r | |
943 | compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)\r | |
944 | {\r | |
945 | PyObject *t, *v;\r | |
946 | Py_ssize_t arg;\r | |
947 | double d;\r | |
948 | \r | |
949 | /* necessary to make sure types aren't coerced (e.g., int and long) */\r | |
950 | /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */\r | |
951 | if (PyFloat_Check(o)) {\r | |
952 | d = PyFloat_AS_DOUBLE(o);\r | |
953 | /* all we need is to make the tuple different in either the 0.0\r | |
954 | * or -0.0 case from all others, just to avoid the "coercion".\r | |
955 | */\r | |
956 | if (d == 0.0 && copysign(1.0, d) < 0.0)\r | |
957 | t = PyTuple_Pack(3, o, o->ob_type, Py_None);\r | |
958 | else\r | |
959 | t = PyTuple_Pack(2, o, o->ob_type);\r | |
960 | }\r | |
961 | #ifndef WITHOUT_COMPLEX\r | |
962 | else if (PyComplex_Check(o)) {\r | |
963 | Py_complex z;\r | |
964 | int real_negzero, imag_negzero;\r | |
965 | /* For the complex case we must make complex(x, 0.)\r | |
966 | different from complex(x, -0.) and complex(0., y)\r | |
967 | different from complex(-0., y), for any x and y.\r | |
968 | All four complex zeros must be distinguished.*/\r | |
969 | z = PyComplex_AsCComplex(o);\r | |
970 | real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;\r | |
971 | imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;\r | |
972 | if (real_negzero && imag_negzero) {\r | |
973 | t = PyTuple_Pack(5, o, o->ob_type,\r | |
974 | Py_None, Py_None, Py_None);\r | |
975 | }\r | |
976 | else if (imag_negzero) {\r | |
977 | t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);\r | |
978 | }\r | |
979 | else if (real_negzero) {\r | |
980 | t = PyTuple_Pack(3, o, o->ob_type, Py_None);\r | |
981 | }\r | |
982 | else {\r | |
983 | t = PyTuple_Pack(2, o, o->ob_type);\r | |
984 | }\r | |
985 | }\r | |
986 | #endif /* WITHOUT_COMPLEX */\r | |
987 | else {\r | |
988 | t = PyTuple_Pack(2, o, o->ob_type);\r | |
989 | }\r | |
990 | if (t == NULL)\r | |
991 | return -1;\r | |
992 | \r | |
993 | v = PyDict_GetItem(dict, t);\r | |
994 | if (!v) {\r | |
995 | arg = PyDict_Size(dict);\r | |
996 | v = PyInt_FromLong(arg);\r | |
997 | if (!v) {\r | |
998 | Py_DECREF(t);\r | |
999 | return -1;\r | |
1000 | }\r | |
1001 | if (PyDict_SetItem(dict, t, v) < 0) {\r | |
1002 | Py_DECREF(t);\r | |
1003 | Py_DECREF(v);\r | |
1004 | return -1;\r | |
1005 | }\r | |
1006 | Py_DECREF(v);\r | |
1007 | }\r | |
1008 | else\r | |
1009 | arg = PyInt_AsLong(v);\r | |
1010 | Py_DECREF(t);\r | |
1011 | return arg;\r | |
1012 | }\r | |
1013 | \r | |
1014 | static int\r | |
1015 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,\r | |
1016 | PyObject *o)\r | |
1017 | {\r | |
1018 | int arg = compiler_add_o(c, dict, o);\r | |
1019 | if (arg < 0)\r | |
1020 | return 0;\r | |
1021 | return compiler_addop_i(c, opcode, arg);\r | |
1022 | }\r | |
1023 | \r | |
1024 | static int\r | |
1025 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,\r | |
1026 | PyObject *o)\r | |
1027 | {\r | |
1028 | int arg;\r | |
1029 | PyObject *mangled = _Py_Mangle(c->u->u_private, o);\r | |
1030 | if (!mangled)\r | |
1031 | return 0;\r | |
1032 | arg = compiler_add_o(c, dict, mangled);\r | |
1033 | Py_DECREF(mangled);\r | |
1034 | if (arg < 0)\r | |
1035 | return 0;\r | |
1036 | return compiler_addop_i(c, opcode, arg);\r | |
1037 | }\r | |
1038 | \r | |
1039 | /* Add an opcode with an integer argument.\r | |
1040 | Returns 0 on failure, 1 on success.\r | |
1041 | */\r | |
1042 | \r | |
1043 | static int\r | |
1044 | compiler_addop_i(struct compiler *c, int opcode, int oparg)\r | |
1045 | {\r | |
1046 | struct instr *i;\r | |
1047 | int off;\r | |
1048 | off = compiler_next_instr(c, c->u->u_curblock);\r | |
1049 | if (off < 0)\r | |
1050 | return 0;\r | |
1051 | i = &c->u->u_curblock->b_instr[off];\r | |
1052 | i->i_opcode = opcode;\r | |
1053 | i->i_oparg = oparg;\r | |
1054 | i->i_hasarg = 1;\r | |
1055 | compiler_set_lineno(c, off);\r | |
1056 | return 1;\r | |
1057 | }\r | |
1058 | \r | |
1059 | static int\r | |
1060 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)\r | |
1061 | {\r | |
1062 | struct instr *i;\r | |
1063 | int off;\r | |
1064 | \r | |
1065 | assert(b != NULL);\r | |
1066 | off = compiler_next_instr(c, c->u->u_curblock);\r | |
1067 | if (off < 0)\r | |
1068 | return 0;\r | |
1069 | i = &c->u->u_curblock->b_instr[off];\r | |
1070 | i->i_opcode = opcode;\r | |
1071 | i->i_target = b;\r | |
1072 | i->i_hasarg = 1;\r | |
1073 | if (absolute)\r | |
1074 | i->i_jabs = 1;\r | |
1075 | else\r | |
1076 | i->i_jrel = 1;\r | |
1077 | compiler_set_lineno(c, off);\r | |
1078 | return 1;\r | |
1079 | }\r | |
1080 | \r | |
1081 | /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd\r | |
1082 | like to find better names.) NEW_BLOCK() creates a new block and sets\r | |
1083 | it as the current block. NEXT_BLOCK() also creates an implicit jump\r | |
1084 | from the current block to the new block.\r | |
1085 | */\r | |
1086 | \r | |
1087 | /* The returns inside these macros make it impossible to decref objects\r | |
1088 | created in the local function. Local objects should use the arena.\r | |
1089 | */\r | |
1090 | \r | |
1091 | \r | |
1092 | #define NEW_BLOCK(C) { \\r | |
1093 | if (compiler_use_new_block((C)) == NULL) \\r | |
1094 | return 0; \\r | |
1095 | }\r | |
1096 | \r | |
1097 | #define NEXT_BLOCK(C) { \\r | |
1098 | if (compiler_next_block((C)) == NULL) \\r | |
1099 | return 0; \\r | |
1100 | }\r | |
1101 | \r | |
1102 | #define ADDOP(C, OP) { \\r | |
1103 | if (!compiler_addop((C), (OP))) \\r | |
1104 | return 0; \\r | |
1105 | }\r | |
1106 | \r | |
1107 | #define ADDOP_IN_SCOPE(C, OP) { \\r | |
1108 | if (!compiler_addop((C), (OP))) { \\r | |
1109 | compiler_exit_scope(c); \\r | |
1110 | return 0; \\r | |
1111 | } \\r | |
1112 | }\r | |
1113 | \r | |
1114 | #define ADDOP_O(C, OP, O, TYPE) { \\r | |
1115 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \\r | |
1116 | return 0; \\r | |
1117 | }\r | |
1118 | \r | |
1119 | #define ADDOP_NAME(C, OP, O, TYPE) { \\r | |
1120 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \\r | |
1121 | return 0; \\r | |
1122 | }\r | |
1123 | \r | |
1124 | #define ADDOP_I(C, OP, O) { \\r | |
1125 | if (!compiler_addop_i((C), (OP), (O))) \\r | |
1126 | return 0; \\r | |
1127 | }\r | |
1128 | \r | |
1129 | #define ADDOP_JABS(C, OP, O) { \\r | |
1130 | if (!compiler_addop_j((C), (OP), (O), 1)) \\r | |
1131 | return 0; \\r | |
1132 | }\r | |
1133 | \r | |
1134 | #define ADDOP_JREL(C, OP, O) { \\r | |
1135 | if (!compiler_addop_j((C), (OP), (O), 0)) \\r | |
1136 | return 0; \\r | |
1137 | }\r | |
1138 | \r | |
1139 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use\r | |
1140 | the ASDL name to synthesize the name of the C type and the visit function.\r | |
1141 | */\r | |
1142 | \r | |
1143 | #define VISIT(C, TYPE, V) {\\r | |
1144 | if (!compiler_visit_ ## TYPE((C), (V))) \\r | |
1145 | return 0; \\r | |
1146 | }\r | |
1147 | \r | |
1148 | #define VISIT_IN_SCOPE(C, TYPE, V) {\\r | |
1149 | if (!compiler_visit_ ## TYPE((C), (V))) { \\r | |
1150 | compiler_exit_scope(c); \\r | |
1151 | return 0; \\r | |
1152 | } \\r | |
1153 | }\r | |
1154 | \r | |
1155 | #define VISIT_SLICE(C, V, CTX) {\\r | |
1156 | if (!compiler_visit_slice((C), (V), (CTX))) \\r | |
1157 | return 0; \\r | |
1158 | }\r | |
1159 | \r | |
1160 | #define VISIT_SEQ(C, TYPE, SEQ) { \\r | |
1161 | int _i; \\r | |
1162 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \\r | |
1163 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \\r | |
1164 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \\r | |
1165 | if (!compiler_visit_ ## TYPE((C), elt)) \\r | |
1166 | return 0; \\r | |
1167 | } \\r | |
1168 | }\r | |
1169 | \r | |
1170 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \\r | |
1171 | int _i; \\r | |
1172 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \\r | |
1173 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \\r | |
1174 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \\r | |
1175 | if (!compiler_visit_ ## TYPE((C), elt)) { \\r | |
1176 | compiler_exit_scope(c); \\r | |
1177 | return 0; \\r | |
1178 | } \\r | |
1179 | } \\r | |
1180 | }\r | |
1181 | \r | |
1182 | static int\r | |
1183 | compiler_isdocstring(stmt_ty s)\r | |
1184 | {\r | |
1185 | if (s->kind != Expr_kind)\r | |
1186 | return 0;\r | |
1187 | return s->v.Expr.value->kind == Str_kind;\r | |
1188 | }\r | |
1189 | \r | |
1190 | /* Compile a sequence of statements, checking for a docstring. */\r | |
1191 | \r | |
1192 | static int\r | |
1193 | compiler_body(struct compiler *c, asdl_seq *stmts)\r | |
1194 | {\r | |
1195 | int i = 0;\r | |
1196 | stmt_ty st;\r | |
1197 | \r | |
1198 | if (!asdl_seq_LEN(stmts))\r | |
1199 | return 1;\r | |
1200 | st = (stmt_ty)asdl_seq_GET(stmts, 0);\r | |
1201 | if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {\r | |
1202 | /* don't generate docstrings if -OO */\r | |
1203 | i = 1;\r | |
1204 | VISIT(c, expr, st->v.Expr.value);\r | |
1205 | if (!compiler_nameop(c, __doc__, Store))\r | |
1206 | return 0;\r | |
1207 | }\r | |
1208 | for (; i < asdl_seq_LEN(stmts); i++)\r | |
1209 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));\r | |
1210 | return 1;\r | |
1211 | }\r | |
1212 | \r | |
1213 | static PyCodeObject *\r | |
1214 | compiler_mod(struct compiler *c, mod_ty mod)\r | |
1215 | {\r | |
1216 | PyCodeObject *co;\r | |
1217 | int addNone = 1;\r | |
1218 | static PyObject *module;\r | |
1219 | if (!module) {\r | |
1220 | module = PyString_InternFromString("<module>");\r | |
1221 | if (!module)\r | |
1222 | return NULL;\r | |
1223 | }\r | |
1224 | /* Use 0 for firstlineno initially, will fixup in assemble(). */\r | |
1225 | if (!compiler_enter_scope(c, module, mod, 0))\r | |
1226 | return NULL;\r | |
1227 | switch (mod->kind) {\r | |
1228 | case Module_kind:\r | |
1229 | if (!compiler_body(c, mod->v.Module.body)) {\r | |
1230 | compiler_exit_scope(c);\r | |
1231 | return 0;\r | |
1232 | }\r | |
1233 | break;\r | |
1234 | case Interactive_kind:\r | |
1235 | c->c_interactive = 1;\r | |
1236 | VISIT_SEQ_IN_SCOPE(c, stmt,\r | |
1237 | mod->v.Interactive.body);\r | |
1238 | break;\r | |
1239 | case Expression_kind:\r | |
1240 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);\r | |
1241 | addNone = 0;\r | |
1242 | break;\r | |
1243 | case Suite_kind:\r | |
1244 | PyErr_SetString(PyExc_SystemError,\r | |
1245 | "suite should not be possible");\r | |
1246 | return 0;\r | |
1247 | default:\r | |
1248 | PyErr_Format(PyExc_SystemError,\r | |
1249 | "module kind %d should not be possible",\r | |
1250 | mod->kind);\r | |
1251 | return 0;\r | |
1252 | }\r | |
1253 | co = assemble(c, addNone);\r | |
1254 | compiler_exit_scope(c);\r | |
1255 | return co;\r | |
1256 | }\r | |
1257 | \r | |
1258 | /* The test for LOCAL must come before the test for FREE in order to\r | |
1259 | handle classes where name is both local and free. The local var is\r | |
1260 | a method and the free var is a free var referenced within a method.\r | |
1261 | */\r | |
1262 | \r | |
1263 | static int\r | |
1264 | get_ref_type(struct compiler *c, PyObject *name)\r | |
1265 | {\r | |
1266 | int scope = PyST_GetScope(c->u->u_ste, name);\r | |
1267 | if (scope == 0) {\r | |
1268 | char buf[350];\r | |
1269 | PyOS_snprintf(buf, sizeof(buf),\r | |
1270 | "unknown scope for %.100s in %.100s(%s) in %s\n"\r | |
1271 | "symbols: %s\nlocals: %s\nglobals: %s",\r | |
1272 | PyString_AS_STRING(name),\r | |
1273 | PyString_AS_STRING(c->u->u_name),\r | |
1274 | PyString_AS_STRING(PyObject_Repr(c->u->u_ste->ste_id)),\r | |
1275 | c->c_filename,\r | |
1276 | PyString_AS_STRING(PyObject_Repr(c->u->u_ste->ste_symbols)),\r | |
1277 | PyString_AS_STRING(PyObject_Repr(c->u->u_varnames)),\r | |
1278 | PyString_AS_STRING(PyObject_Repr(c->u->u_names))\r | |
1279 | );\r | |
1280 | Py_FatalError(buf);\r | |
1281 | }\r | |
1282 | \r | |
1283 | return scope;\r | |
1284 | }\r | |
1285 | \r | |
1286 | static int\r | |
1287 | compiler_lookup_arg(PyObject *dict, PyObject *name)\r | |
1288 | {\r | |
1289 | PyObject *k, *v;\r | |
1290 | k = PyTuple_Pack(2, name, name->ob_type);\r | |
1291 | if (k == NULL)\r | |
1292 | return -1;\r | |
1293 | v = PyDict_GetItem(dict, k);\r | |
1294 | Py_DECREF(k);\r | |
1295 | if (v == NULL)\r | |
1296 | return -1;\r | |
1297 | return PyInt_AS_LONG(v);\r | |
1298 | }\r | |
1299 | \r | |
1300 | static int\r | |
1301 | compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)\r | |
1302 | {\r | |
1303 | int i, free = PyCode_GetNumFree(co);\r | |
1304 | if (free == 0) {\r | |
1305 | ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);\r | |
1306 | ADDOP_I(c, MAKE_FUNCTION, args);\r | |
1307 | return 1;\r | |
1308 | }\r | |
1309 | for (i = 0; i < free; ++i) {\r | |
1310 | /* Bypass com_addop_varname because it will generate\r | |
1311 | LOAD_DEREF but LOAD_CLOSURE is needed.\r | |
1312 | */\r | |
1313 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);\r | |
1314 | int arg, reftype;\r | |
1315 | \r | |
1316 | /* Special case: If a class contains a method with a\r | |
1317 | free variable that has the same name as a method,\r | |
1318 | the name will be considered free *and* local in the\r | |
1319 | class. It should be handled by the closure, as\r | |
1320 | well as by the normal name loookup logic.\r | |
1321 | */\r | |
1322 | reftype = get_ref_type(c, name);\r | |
1323 | if (reftype == CELL)\r | |
1324 | arg = compiler_lookup_arg(c->u->u_cellvars, name);\r | |
1325 | else /* (reftype == FREE) */\r | |
1326 | arg = compiler_lookup_arg(c->u->u_freevars, name);\r | |
1327 | if (arg == -1) {\r | |
1328 | printf("lookup %s in %s %d %d\n"\r | |
1329 | "freevars of %s: %s\n",\r | |
1330 | PyString_AS_STRING(PyObject_Repr(name)),\r | |
1331 | PyString_AS_STRING(c->u->u_name),\r | |
1332 | reftype, arg,\r | |
1333 | PyString_AS_STRING(co->co_name),\r | |
1334 | PyString_AS_STRING(PyObject_Repr(co->co_freevars)));\r | |
1335 | Py_FatalError("compiler_make_closure()");\r | |
1336 | }\r | |
1337 | ADDOP_I(c, LOAD_CLOSURE, arg);\r | |
1338 | }\r | |
1339 | ADDOP_I(c, BUILD_TUPLE, free);\r | |
1340 | ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);\r | |
1341 | ADDOP_I(c, MAKE_CLOSURE, args);\r | |
1342 | return 1;\r | |
1343 | }\r | |
1344 | \r | |
1345 | static int\r | |
1346 | compiler_decorators(struct compiler *c, asdl_seq* decos)\r | |
1347 | {\r | |
1348 | int i;\r | |
1349 | \r | |
1350 | if (!decos)\r | |
1351 | return 1;\r | |
1352 | \r | |
1353 | for (i = 0; i < asdl_seq_LEN(decos); i++) {\r | |
1354 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));\r | |
1355 | }\r | |
1356 | return 1;\r | |
1357 | }\r | |
1358 | \r | |
1359 | static int\r | |
1360 | compiler_arguments(struct compiler *c, arguments_ty args)\r | |
1361 | {\r | |
1362 | int i;\r | |
1363 | int n = asdl_seq_LEN(args->args);\r | |
1364 | /* Correctly handle nested argument lists */\r | |
1365 | for (i = 0; i < n; i++) {\r | |
1366 | expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);\r | |
1367 | if (arg->kind == Tuple_kind) {\r | |
1368 | PyObject *id = PyString_FromFormat(".%d", i);\r | |
1369 | if (id == NULL) {\r | |
1370 | return 0;\r | |
1371 | }\r | |
1372 | if (!compiler_nameop(c, id, Load)) {\r | |
1373 | Py_DECREF(id);\r | |
1374 | return 0;\r | |
1375 | }\r | |
1376 | Py_DECREF(id);\r | |
1377 | VISIT(c, expr, arg);\r | |
1378 | }\r | |
1379 | }\r | |
1380 | return 1;\r | |
1381 | }\r | |
1382 | \r | |
1383 | static int\r | |
1384 | compiler_function(struct compiler *c, stmt_ty s)\r | |
1385 | {\r | |
1386 | PyCodeObject *co;\r | |
1387 | PyObject *first_const = Py_None;\r | |
1388 | arguments_ty args = s->v.FunctionDef.args;\r | |
1389 | asdl_seq* decos = s->v.FunctionDef.decorator_list;\r | |
1390 | stmt_ty st;\r | |
1391 | int i, n, docstring;\r | |
1392 | \r | |
1393 | assert(s->kind == FunctionDef_kind);\r | |
1394 | \r | |
1395 | if (!compiler_decorators(c, decos))\r | |
1396 | return 0;\r | |
1397 | if (args->defaults)\r | |
1398 | VISIT_SEQ(c, expr, args->defaults);\r | |
1399 | if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,\r | |
1400 | s->lineno))\r | |
1401 | return 0;\r | |
1402 | \r | |
1403 | st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);\r | |
1404 | docstring = compiler_isdocstring(st);\r | |
1405 | if (docstring && Py_OptimizeFlag < 2)\r | |
1406 | first_const = st->v.Expr.value->v.Str.s;\r | |
1407 | if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {\r | |
1408 | compiler_exit_scope(c);\r | |
1409 | return 0;\r | |
1410 | }\r | |
1411 | \r | |
1412 | /* unpack nested arguments */\r | |
1413 | compiler_arguments(c, args);\r | |
1414 | \r | |
1415 | c->u->u_argcount = asdl_seq_LEN(args->args);\r | |
1416 | n = asdl_seq_LEN(s->v.FunctionDef.body);\r | |
1417 | /* if there was a docstring, we need to skip the first statement */\r | |
1418 | for (i = docstring; i < n; i++) {\r | |
1419 | st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);\r | |
1420 | VISIT_IN_SCOPE(c, stmt, st);\r | |
1421 | }\r | |
1422 | co = assemble(c, 1);\r | |
1423 | compiler_exit_scope(c);\r | |
1424 | if (co == NULL)\r | |
1425 | return 0;\r | |
1426 | \r | |
1427 | compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));\r | |
1428 | Py_DECREF(co);\r | |
1429 | \r | |
1430 | for (i = 0; i < asdl_seq_LEN(decos); i++) {\r | |
1431 | ADDOP_I(c, CALL_FUNCTION, 1);\r | |
1432 | }\r | |
1433 | \r | |
1434 | return compiler_nameop(c, s->v.FunctionDef.name, Store);\r | |
1435 | }\r | |
1436 | \r | |
1437 | static int\r | |
1438 | compiler_class(struct compiler *c, stmt_ty s)\r | |
1439 | {\r | |
1440 | int n, i;\r | |
1441 | PyCodeObject *co;\r | |
1442 | PyObject *str;\r | |
1443 | asdl_seq* decos = s->v.ClassDef.decorator_list;\r | |
1444 | \r | |
1445 | if (!compiler_decorators(c, decos))\r | |
1446 | return 0;\r | |
1447 | \r | |
1448 | /* push class name on stack, needed by BUILD_CLASS */\r | |
1449 | ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);\r | |
1450 | /* push the tuple of base classes on the stack */\r | |
1451 | n = asdl_seq_LEN(s->v.ClassDef.bases);\r | |
1452 | if (n > 0)\r | |
1453 | VISIT_SEQ(c, expr, s->v.ClassDef.bases);\r | |
1454 | ADDOP_I(c, BUILD_TUPLE, n);\r | |
1455 | if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,\r | |
1456 | s->lineno))\r | |
1457 | return 0;\r | |
1458 | Py_XDECREF(c->u->u_private);\r | |
1459 | c->u->u_private = s->v.ClassDef.name;\r | |
1460 | Py_INCREF(c->u->u_private);\r | |
1461 | str = PyString_InternFromString("__name__");\r | |
1462 | if (!str || !compiler_nameop(c, str, Load)) {\r | |
1463 | Py_XDECREF(str);\r | |
1464 | compiler_exit_scope(c);\r | |
1465 | return 0;\r | |
1466 | }\r | |
1467 | \r | |
1468 | Py_DECREF(str);\r | |
1469 | str = PyString_InternFromString("__module__");\r | |
1470 | if (!str || !compiler_nameop(c, str, Store)) {\r | |
1471 | Py_XDECREF(str);\r | |
1472 | compiler_exit_scope(c);\r | |
1473 | return 0;\r | |
1474 | }\r | |
1475 | Py_DECREF(str);\r | |
1476 | \r | |
1477 | if (!compiler_body(c, s->v.ClassDef.body)) {\r | |
1478 | compiler_exit_scope(c);\r | |
1479 | return 0;\r | |
1480 | }\r | |
1481 | \r | |
1482 | ADDOP_IN_SCOPE(c, LOAD_LOCALS);\r | |
1483 | ADDOP_IN_SCOPE(c, RETURN_VALUE);\r | |
1484 | co = assemble(c, 1);\r | |
1485 | compiler_exit_scope(c);\r | |
1486 | if (co == NULL)\r | |
1487 | return 0;\r | |
1488 | \r | |
1489 | compiler_make_closure(c, co, 0);\r | |
1490 | Py_DECREF(co);\r | |
1491 | \r | |
1492 | ADDOP_I(c, CALL_FUNCTION, 0);\r | |
1493 | ADDOP(c, BUILD_CLASS);\r | |
1494 | /* apply decorators */\r | |
1495 | for (i = 0; i < asdl_seq_LEN(decos); i++) {\r | |
1496 | ADDOP_I(c, CALL_FUNCTION, 1);\r | |
1497 | }\r | |
1498 | if (!compiler_nameop(c, s->v.ClassDef.name, Store))\r | |
1499 | return 0;\r | |
1500 | return 1;\r | |
1501 | }\r | |
1502 | \r | |
1503 | static int\r | |
1504 | compiler_ifexp(struct compiler *c, expr_ty e)\r | |
1505 | {\r | |
1506 | basicblock *end, *next;\r | |
1507 | \r | |
1508 | assert(e->kind == IfExp_kind);\r | |
1509 | end = compiler_new_block(c);\r | |
1510 | if (end == NULL)\r | |
1511 | return 0;\r | |
1512 | next = compiler_new_block(c);\r | |
1513 | if (next == NULL)\r | |
1514 | return 0;\r | |
1515 | VISIT(c, expr, e->v.IfExp.test);\r | |
1516 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);\r | |
1517 | VISIT(c, expr, e->v.IfExp.body);\r | |
1518 | ADDOP_JREL(c, JUMP_FORWARD, end);\r | |
1519 | compiler_use_next_block(c, next);\r | |
1520 | VISIT(c, expr, e->v.IfExp.orelse);\r | |
1521 | compiler_use_next_block(c, end);\r | |
1522 | return 1;\r | |
1523 | }\r | |
1524 | \r | |
1525 | static int\r | |
1526 | compiler_lambda(struct compiler *c, expr_ty e)\r | |
1527 | {\r | |
1528 | PyCodeObject *co;\r | |
1529 | static identifier name;\r | |
1530 | arguments_ty args = e->v.Lambda.args;\r | |
1531 | assert(e->kind == Lambda_kind);\r | |
1532 | \r | |
1533 | if (!name) {\r | |
1534 | name = PyString_InternFromString("<lambda>");\r | |
1535 | if (!name)\r | |
1536 | return 0;\r | |
1537 | }\r | |
1538 | \r | |
1539 | if (args->defaults)\r | |
1540 | VISIT_SEQ(c, expr, args->defaults);\r | |
1541 | if (!compiler_enter_scope(c, name, (void *)e, e->lineno))\r | |
1542 | return 0;\r | |
1543 | \r | |
1544 | /* unpack nested arguments */\r | |
1545 | compiler_arguments(c, args);\r | |
1546 | \r | |
1547 | /* Make None the first constant, so the lambda can't have a\r | |
1548 | docstring. */\r | |
1549 | if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)\r | |
1550 | return 0;\r | |
1551 | \r | |
1552 | c->u->u_argcount = asdl_seq_LEN(args->args);\r | |
1553 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);\r | |
1554 | if (c->u->u_ste->ste_generator) {\r | |
1555 | ADDOP_IN_SCOPE(c, POP_TOP);\r | |
1556 | }\r | |
1557 | else {\r | |
1558 | ADDOP_IN_SCOPE(c, RETURN_VALUE);\r | |
1559 | }\r | |
1560 | co = assemble(c, 1);\r | |
1561 | compiler_exit_scope(c);\r | |
1562 | if (co == NULL)\r | |
1563 | return 0;\r | |
1564 | \r | |
1565 | compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));\r | |
1566 | Py_DECREF(co);\r | |
1567 | \r | |
1568 | return 1;\r | |
1569 | }\r | |
1570 | \r | |
1571 | static int\r | |
1572 | compiler_print(struct compiler *c, stmt_ty s)\r | |
1573 | {\r | |
1574 | int i, n;\r | |
1575 | bool dest;\r | |
1576 | \r | |
1577 | assert(s->kind == Print_kind);\r | |
1578 | n = asdl_seq_LEN(s->v.Print.values);\r | |
1579 | dest = false;\r | |
1580 | if (s->v.Print.dest) {\r | |
1581 | VISIT(c, expr, s->v.Print.dest);\r | |
1582 | dest = true;\r | |
1583 | }\r | |
1584 | for (i = 0; i < n; i++) {\r | |
1585 | expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);\r | |
1586 | if (dest) {\r | |
1587 | ADDOP(c, DUP_TOP);\r | |
1588 | VISIT(c, expr, e);\r | |
1589 | ADDOP(c, ROT_TWO);\r | |
1590 | ADDOP(c, PRINT_ITEM_TO);\r | |
1591 | }\r | |
1592 | else {\r | |
1593 | VISIT(c, expr, e);\r | |
1594 | ADDOP(c, PRINT_ITEM);\r | |
1595 | }\r | |
1596 | }\r | |
1597 | if (s->v.Print.nl) {\r | |
1598 | if (dest)\r | |
1599 | ADDOP(c, PRINT_NEWLINE_TO)\r | |
1600 | else\r | |
1601 | ADDOP(c, PRINT_NEWLINE)\r | |
1602 | }\r | |
1603 | else if (dest)\r | |
1604 | ADDOP(c, POP_TOP);\r | |
1605 | return 1;\r | |
1606 | }\r | |
1607 | \r | |
1608 | static int\r | |
1609 | compiler_if(struct compiler *c, stmt_ty s)\r | |
1610 | {\r | |
1611 | basicblock *end, *next;\r | |
1612 | int constant;\r | |
1613 | assert(s->kind == If_kind);\r | |
1614 | end = compiler_new_block(c);\r | |
1615 | if (end == NULL)\r | |
1616 | return 0;\r | |
1617 | \r | |
1618 | constant = expr_constant(s->v.If.test);\r | |
1619 | /* constant = 0: "if 0"\r | |
1620 | * constant = 1: "if 1", "if 2", ...\r | |
1621 | * constant = -1: rest */\r | |
1622 | if (constant == 0) {\r | |
1623 | if (s->v.If.orelse)\r | |
1624 | VISIT_SEQ(c, stmt, s->v.If.orelse);\r | |
1625 | } else if (constant == 1) {\r | |
1626 | VISIT_SEQ(c, stmt, s->v.If.body);\r | |
1627 | } else {\r | |
1628 | if (s->v.If.orelse) {\r | |
1629 | next = compiler_new_block(c);\r | |
1630 | if (next == NULL)\r | |
1631 | return 0;\r | |
1632 | }\r | |
1633 | else\r | |
1634 | next = end;\r | |
1635 | VISIT(c, expr, s->v.If.test);\r | |
1636 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);\r | |
1637 | VISIT_SEQ(c, stmt, s->v.If.body);\r | |
1638 | ADDOP_JREL(c, JUMP_FORWARD, end);\r | |
1639 | if (s->v.If.orelse) {\r | |
1640 | compiler_use_next_block(c, next);\r | |
1641 | VISIT_SEQ(c, stmt, s->v.If.orelse);\r | |
1642 | }\r | |
1643 | }\r | |
1644 | compiler_use_next_block(c, end);\r | |
1645 | return 1;\r | |
1646 | }\r | |
1647 | \r | |
1648 | static int\r | |
1649 | compiler_for(struct compiler *c, stmt_ty s)\r | |
1650 | {\r | |
1651 | basicblock *start, *cleanup, *end;\r | |
1652 | \r | |
1653 | start = compiler_new_block(c);\r | |
1654 | cleanup = compiler_new_block(c);\r | |
1655 | end = compiler_new_block(c);\r | |
1656 | if (start == NULL || end == NULL || cleanup == NULL)\r | |
1657 | return 0;\r | |
1658 | ADDOP_JREL(c, SETUP_LOOP, end);\r | |
1659 | if (!compiler_push_fblock(c, LOOP, start))\r | |
1660 | return 0;\r | |
1661 | VISIT(c, expr, s->v.For.iter);\r | |
1662 | ADDOP(c, GET_ITER);\r | |
1663 | compiler_use_next_block(c, start);\r | |
1664 | ADDOP_JREL(c, FOR_ITER, cleanup);\r | |
1665 | VISIT(c, expr, s->v.For.target);\r | |
1666 | VISIT_SEQ(c, stmt, s->v.For.body);\r | |
1667 | ADDOP_JABS(c, JUMP_ABSOLUTE, start);\r | |
1668 | compiler_use_next_block(c, cleanup);\r | |
1669 | ADDOP(c, POP_BLOCK);\r | |
1670 | compiler_pop_fblock(c, LOOP, start);\r | |
1671 | VISIT_SEQ(c, stmt, s->v.For.orelse);\r | |
1672 | compiler_use_next_block(c, end);\r | |
1673 | return 1;\r | |
1674 | }\r | |
1675 | \r | |
1676 | static int\r | |
1677 | compiler_while(struct compiler *c, stmt_ty s)\r | |
1678 | {\r | |
1679 | basicblock *loop, *orelse, *end, *anchor = NULL;\r | |
1680 | int constant = expr_constant(s->v.While.test);\r | |
1681 | \r | |
1682 | if (constant == 0) {\r | |
1683 | if (s->v.While.orelse)\r | |
1684 | VISIT_SEQ(c, stmt, s->v.While.orelse);\r | |
1685 | return 1;\r | |
1686 | }\r | |
1687 | loop = compiler_new_block(c);\r | |
1688 | end = compiler_new_block(c);\r | |
1689 | if (constant == -1) {\r | |
1690 | anchor = compiler_new_block(c);\r | |
1691 | if (anchor == NULL)\r | |
1692 | return 0;\r | |
1693 | }\r | |
1694 | if (loop == NULL || end == NULL)\r | |
1695 | return 0;\r | |
1696 | if (s->v.While.orelse) {\r | |
1697 | orelse = compiler_new_block(c);\r | |
1698 | if (orelse == NULL)\r | |
1699 | return 0;\r | |
1700 | }\r | |
1701 | else\r | |
1702 | orelse = NULL;\r | |
1703 | \r | |
1704 | ADDOP_JREL(c, SETUP_LOOP, end);\r | |
1705 | compiler_use_next_block(c, loop);\r | |
1706 | if (!compiler_push_fblock(c, LOOP, loop))\r | |
1707 | return 0;\r | |
1708 | if (constant == -1) {\r | |
1709 | VISIT(c, expr, s->v.While.test);\r | |
1710 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);\r | |
1711 | }\r | |
1712 | VISIT_SEQ(c, stmt, s->v.While.body);\r | |
1713 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop);\r | |
1714 | \r | |
1715 | /* XXX should the two POP instructions be in a separate block\r | |
1716 | if there is no else clause ?\r | |
1717 | */\r | |
1718 | \r | |
1719 | if (constant == -1)\r | |
1720 | compiler_use_next_block(c, anchor);\r | |
1721 | ADDOP(c, POP_BLOCK);\r | |
1722 | compiler_pop_fblock(c, LOOP, loop);\r | |
1723 | if (orelse != NULL) /* what if orelse is just pass? */\r | |
1724 | VISIT_SEQ(c, stmt, s->v.While.orelse);\r | |
1725 | compiler_use_next_block(c, end);\r | |
1726 | \r | |
1727 | return 1;\r | |
1728 | }\r | |
1729 | \r | |
1730 | static int\r | |
1731 | compiler_continue(struct compiler *c)\r | |
1732 | {\r | |
1733 | static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";\r | |
1734 | static const char IN_FINALLY_ERROR_MSG[] =\r | |
1735 | "'continue' not supported inside 'finally' clause";\r | |
1736 | int i;\r | |
1737 | \r | |
1738 | if (!c->u->u_nfblocks)\r | |
1739 | return compiler_error(c, LOOP_ERROR_MSG);\r | |
1740 | i = c->u->u_nfblocks - 1;\r | |
1741 | switch (c->u->u_fblock[i].fb_type) {\r | |
1742 | case LOOP:\r | |
1743 | ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);\r | |
1744 | break;\r | |
1745 | case EXCEPT:\r | |
1746 | case FINALLY_TRY:\r | |
1747 | while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {\r | |
1748 | /* Prevent continue anywhere under a finally\r | |
1749 | even if hidden in a sub-try or except. */\r | |
1750 | if (c->u->u_fblock[i].fb_type == FINALLY_END)\r | |
1751 | return compiler_error(c, IN_FINALLY_ERROR_MSG);\r | |
1752 | }\r | |
1753 | if (i == -1)\r | |
1754 | return compiler_error(c, LOOP_ERROR_MSG);\r | |
1755 | ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);\r | |
1756 | break;\r | |
1757 | case FINALLY_END:\r | |
1758 | return compiler_error(c, IN_FINALLY_ERROR_MSG);\r | |
1759 | }\r | |
1760 | \r | |
1761 | return 1;\r | |
1762 | }\r | |
1763 | \r | |
1764 | /* Code generated for "try: <body> finally: <finalbody>" is as follows:\r | |
1765 | \r | |
1766 | SETUP_FINALLY L\r | |
1767 | <code for body>\r | |
1768 | POP_BLOCK\r | |
1769 | LOAD_CONST <None>\r | |
1770 | L: <code for finalbody>\r | |
1771 | END_FINALLY\r | |
1772 | \r | |
1773 | The special instructions use the block stack. Each block\r | |
1774 | stack entry contains the instruction that created it (here\r | |
1775 | SETUP_FINALLY), the level of the value stack at the time the\r | |
1776 | block stack entry was created, and a label (here L).\r | |
1777 | \r | |
1778 | SETUP_FINALLY:\r | |
1779 | Pushes the current value stack level and the label\r | |
1780 | onto the block stack.\r | |
1781 | POP_BLOCK:\r | |
1782 | Pops en entry from the block stack, and pops the value\r | |
1783 | stack until its level is the same as indicated on the\r | |
1784 | block stack. (The label is ignored.)\r | |
1785 | END_FINALLY:\r | |
1786 | Pops a variable number of entries from the *value* stack\r | |
1787 | and re-raises the exception they specify. The number of\r | |
1788 | entries popped depends on the (pseudo) exception type.\r | |
1789 | \r | |
1790 | The block stack is unwound when an exception is raised:\r | |
1791 | when a SETUP_FINALLY entry is found, the exception is pushed\r | |
1792 | onto the value stack (and the exception condition is cleared),\r | |
1793 | and the interpreter jumps to the label gotten from the block\r | |
1794 | stack.\r | |
1795 | */\r | |
1796 | \r | |
1797 | static int\r | |
1798 | compiler_try_finally(struct compiler *c, stmt_ty s)\r | |
1799 | {\r | |
1800 | basicblock *body, *end;\r | |
1801 | body = compiler_new_block(c);\r | |
1802 | end = compiler_new_block(c);\r | |
1803 | if (body == NULL || end == NULL)\r | |
1804 | return 0;\r | |
1805 | \r | |
1806 | ADDOP_JREL(c, SETUP_FINALLY, end);\r | |
1807 | compiler_use_next_block(c, body);\r | |
1808 | if (!compiler_push_fblock(c, FINALLY_TRY, body))\r | |
1809 | return 0;\r | |
1810 | VISIT_SEQ(c, stmt, s->v.TryFinally.body);\r | |
1811 | ADDOP(c, POP_BLOCK);\r | |
1812 | compiler_pop_fblock(c, FINALLY_TRY, body);\r | |
1813 | \r | |
1814 | ADDOP_O(c, LOAD_CONST, Py_None, consts);\r | |
1815 | compiler_use_next_block(c, end);\r | |
1816 | if (!compiler_push_fblock(c, FINALLY_END, end))\r | |
1817 | return 0;\r | |
1818 | VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);\r | |
1819 | ADDOP(c, END_FINALLY);\r | |
1820 | compiler_pop_fblock(c, FINALLY_END, end);\r | |
1821 | \r | |
1822 | return 1;\r | |
1823 | }\r | |
1824 | \r | |
1825 | /*\r | |
1826 | Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":\r | |
1827 | (The contents of the value stack is shown in [], with the top\r | |
1828 | at the right; 'tb' is trace-back info, 'val' the exception's\r | |
1829 | associated value, and 'exc' the exception.)\r | |
1830 | \r | |
1831 | Value stack Label Instruction Argument\r | |
1832 | [] SETUP_EXCEPT L1\r | |
1833 | [] <code for S>\r | |
1834 | [] POP_BLOCK\r | |
1835 | [] JUMP_FORWARD L0\r | |
1836 | \r | |
1837 | [tb, val, exc] L1: DUP )\r | |
1838 | [tb, val, exc, exc] <evaluate E1> )\r | |
1839 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1\r | |
1840 | [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )\r | |
1841 | [tb, val, exc] POP\r | |
1842 | [tb, val] <assign to V1> (or POP if no V1)\r | |
1843 | [tb] POP\r | |
1844 | [] <code for S1>\r | |
1845 | JUMP_FORWARD L0\r | |
1846 | \r | |
1847 | [tb, val, exc] L2: DUP\r | |
1848 | .............................etc.......................\r | |
1849 | \r | |
1850 | [tb, val, exc] Ln+1: END_FINALLY # re-raise exception\r | |
1851 | \r | |
1852 | [] L0: <next statement>\r | |
1853 | \r | |
1854 | Of course, parts are not generated if Vi or Ei is not present.\r | |
1855 | */\r | |
1856 | static int\r | |
1857 | compiler_try_except(struct compiler *c, stmt_ty s)\r | |
1858 | {\r | |
1859 | basicblock *body, *orelse, *except, *end;\r | |
1860 | int i, n;\r | |
1861 | \r | |
1862 | body = compiler_new_block(c);\r | |
1863 | except = compiler_new_block(c);\r | |
1864 | orelse = compiler_new_block(c);\r | |
1865 | end = compiler_new_block(c);\r | |
1866 | if (body == NULL || except == NULL || orelse == NULL || end == NULL)\r | |
1867 | return 0;\r | |
1868 | ADDOP_JREL(c, SETUP_EXCEPT, except);\r | |
1869 | compiler_use_next_block(c, body);\r | |
1870 | if (!compiler_push_fblock(c, EXCEPT, body))\r | |
1871 | return 0;\r | |
1872 | VISIT_SEQ(c, stmt, s->v.TryExcept.body);\r | |
1873 | ADDOP(c, POP_BLOCK);\r | |
1874 | compiler_pop_fblock(c, EXCEPT, body);\r | |
1875 | ADDOP_JREL(c, JUMP_FORWARD, orelse);\r | |
1876 | n = asdl_seq_LEN(s->v.TryExcept.handlers);\r | |
1877 | compiler_use_next_block(c, except);\r | |
1878 | for (i = 0; i < n; i++) {\r | |
1879 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(\r | |
1880 | s->v.TryExcept.handlers, i);\r | |
1881 | if (!handler->v.ExceptHandler.type && i < n-1)\r | |
1882 | return compiler_error(c, "default 'except:' must be last");\r | |
1883 | c->u->u_lineno_set = false;\r | |
1884 | c->u->u_lineno = handler->lineno;\r | |
1885 | except = compiler_new_block(c);\r | |
1886 | if (except == NULL)\r | |
1887 | return 0;\r | |
1888 | if (handler->v.ExceptHandler.type) {\r | |
1889 | ADDOP(c, DUP_TOP);\r | |
1890 | VISIT(c, expr, handler->v.ExceptHandler.type);\r | |
1891 | ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);\r | |
1892 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);\r | |
1893 | }\r | |
1894 | ADDOP(c, POP_TOP);\r | |
1895 | if (handler->v.ExceptHandler.name) {\r | |
1896 | VISIT(c, expr, handler->v.ExceptHandler.name);\r | |
1897 | }\r | |
1898 | else {\r | |
1899 | ADDOP(c, POP_TOP);\r | |
1900 | }\r | |
1901 | ADDOP(c, POP_TOP);\r | |
1902 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);\r | |
1903 | ADDOP_JREL(c, JUMP_FORWARD, end);\r | |
1904 | compiler_use_next_block(c, except);\r | |
1905 | }\r | |
1906 | ADDOP(c, END_FINALLY);\r | |
1907 | compiler_use_next_block(c, orelse);\r | |
1908 | VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);\r | |
1909 | compiler_use_next_block(c, end);\r | |
1910 | return 1;\r | |
1911 | }\r | |
1912 | \r | |
1913 | static int\r | |
1914 | compiler_import_as(struct compiler *c, identifier name, identifier asname)\r | |
1915 | {\r | |
1916 | /* The IMPORT_NAME opcode was already generated. This function\r | |
1917 | merely needs to bind the result to a name.\r | |
1918 | \r | |
1919 | If there is a dot in name, we need to split it and emit a\r | |
1920 | LOAD_ATTR for each name.\r | |
1921 | */\r | |
1922 | const char *src = PyString_AS_STRING(name);\r | |
1923 | const char *dot = strchr(src, '.');\r | |
1924 | if (dot) {\r | |
1925 | /* Consume the base module name to get the first attribute */\r | |
1926 | src = dot + 1;\r | |
1927 | while (dot) {\r | |
1928 | /* NB src is only defined when dot != NULL */\r | |
1929 | PyObject *attr;\r | |
1930 | dot = strchr(src, '.');\r | |
1931 | attr = PyString_FromStringAndSize(src,\r | |
1932 | dot ? dot - src : strlen(src));\r | |
1933 | if (!attr)\r | |
1934 | return -1;\r | |
1935 | ADDOP_O(c, LOAD_ATTR, attr, names);\r | |
1936 | Py_DECREF(attr);\r | |
1937 | src = dot + 1;\r | |
1938 | }\r | |
1939 | }\r | |
1940 | return compiler_nameop(c, asname, Store);\r | |
1941 | }\r | |
1942 | \r | |
1943 | static int\r | |
1944 | compiler_import(struct compiler *c, stmt_ty s)\r | |
1945 | {\r | |
1946 | /* The Import node stores a module name like a.b.c as a single\r | |
1947 | string. This is convenient for all cases except\r | |
1948 | import a.b.c as d\r | |
1949 | where we need to parse that string to extract the individual\r | |
1950 | module names.\r | |
1951 | XXX Perhaps change the representation to make this case simpler?\r | |
1952 | */\r | |
1953 | int i, n = asdl_seq_LEN(s->v.Import.names);\r | |
1954 | \r | |
1955 | for (i = 0; i < n; i++) {\r | |
1956 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);\r | |
1957 | int r;\r | |
1958 | PyObject *level;\r | |
1959 | \r | |
1960 | if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))\r | |
1961 | level = PyInt_FromLong(0);\r | |
1962 | else\r | |
1963 | level = PyInt_FromLong(-1);\r | |
1964 | \r | |
1965 | if (level == NULL)\r | |
1966 | return 0;\r | |
1967 | \r | |
1968 | ADDOP_O(c, LOAD_CONST, level, consts);\r | |
1969 | Py_DECREF(level);\r | |
1970 | ADDOP_O(c, LOAD_CONST, Py_None, consts);\r | |
1971 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names);\r | |
1972 | \r | |
1973 | if (alias->asname) {\r | |
1974 | r = compiler_import_as(c, alias->name, alias->asname);\r | |
1975 | if (!r)\r | |
1976 | return r;\r | |
1977 | }\r | |
1978 | else {\r | |
1979 | identifier tmp = alias->name;\r | |
1980 | const char *base = PyString_AS_STRING(alias->name);\r | |
1981 | char *dot = strchr(base, '.');\r | |
1982 | if (dot)\r | |
1983 | tmp = PyString_FromStringAndSize(base,\r | |
1984 | dot - base);\r | |
1985 | r = compiler_nameop(c, tmp, Store);\r | |
1986 | if (dot) {\r | |
1987 | Py_DECREF(tmp);\r | |
1988 | }\r | |
1989 | if (!r)\r | |
1990 | return r;\r | |
1991 | }\r | |
1992 | }\r | |
1993 | return 1;\r | |
1994 | }\r | |
1995 | \r | |
1996 | static int\r | |
1997 | compiler_from_import(struct compiler *c, stmt_ty s)\r | |
1998 | {\r | |
1999 | int i, n = asdl_seq_LEN(s->v.ImportFrom.names);\r | |
2000 | \r | |
2001 | PyObject *names = PyTuple_New(n);\r | |
2002 | PyObject *level;\r | |
2003 | static PyObject *empty_string;\r | |
2004 | \r | |
2005 | if (!empty_string) {\r | |
2006 | empty_string = PyString_FromString("");\r | |
2007 | if (!empty_string)\r | |
2008 | return 0;\r | |
2009 | }\r | |
2010 | \r | |
2011 | if (!names)\r | |
2012 | return 0;\r | |
2013 | \r | |
2014 | if (s->v.ImportFrom.level == 0 && c->c_flags &&\r | |
2015 | !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))\r | |
2016 | level = PyInt_FromLong(-1);\r | |
2017 | else\r | |
2018 | level = PyInt_FromLong(s->v.ImportFrom.level);\r | |
2019 | \r | |
2020 | if (!level) {\r | |
2021 | Py_DECREF(names);\r | |
2022 | return 0;\r | |
2023 | }\r | |
2024 | \r | |
2025 | /* build up the names */\r | |
2026 | for (i = 0; i < n; i++) {\r | |
2027 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);\r | |
2028 | Py_INCREF(alias->name);\r | |
2029 | PyTuple_SET_ITEM(names, i, alias->name);\r | |
2030 | }\r | |
2031 | \r | |
2032 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&\r | |
2033 | !strcmp(PyString_AS_STRING(s->v.ImportFrom.module), "__future__")) {\r | |
2034 | Py_DECREF(level);\r | |
2035 | Py_DECREF(names);\r | |
2036 | return compiler_error(c, "from __future__ imports must occur "\r | |
2037 | "at the beginning of the file");\r | |
2038 | }\r | |
2039 | \r | |
2040 | ADDOP_O(c, LOAD_CONST, level, consts);\r | |
2041 | Py_DECREF(level);\r | |
2042 | ADDOP_O(c, LOAD_CONST, names, consts);\r | |
2043 | Py_DECREF(names);\r | |
2044 | if (s->v.ImportFrom.module) {\r | |
2045 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);\r | |
2046 | }\r | |
2047 | else {\r | |
2048 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names);\r | |
2049 | }\r | |
2050 | for (i = 0; i < n; i++) {\r | |
2051 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);\r | |
2052 | identifier store_name;\r | |
2053 | \r | |
2054 | if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {\r | |
2055 | assert(n == 1);\r | |
2056 | ADDOP(c, IMPORT_STAR);\r | |
2057 | return 1;\r | |
2058 | }\r | |
2059 | \r | |
2060 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names);\r | |
2061 | store_name = alias->name;\r | |
2062 | if (alias->asname)\r | |
2063 | store_name = alias->asname;\r | |
2064 | \r | |
2065 | if (!compiler_nameop(c, store_name, Store)) {\r | |
2066 | Py_DECREF(names);\r | |
2067 | return 0;\r | |
2068 | }\r | |
2069 | }\r | |
2070 | /* remove imported module */\r | |
2071 | ADDOP(c, POP_TOP);\r | |
2072 | return 1;\r | |
2073 | }\r | |
2074 | \r | |
2075 | static int\r | |
2076 | compiler_assert(struct compiler *c, stmt_ty s)\r | |
2077 | {\r | |
2078 | static PyObject *assertion_error = NULL;\r | |
2079 | basicblock *end;\r | |
2080 | \r | |
2081 | if (Py_OptimizeFlag)\r | |
2082 | return 1;\r | |
2083 | if (assertion_error == NULL) {\r | |
2084 | assertion_error = PyString_InternFromString("AssertionError");\r | |
2085 | if (assertion_error == NULL)\r | |
2086 | return 0;\r | |
2087 | }\r | |
2088 | if (s->v.Assert.test->kind == Tuple_kind &&\r | |
2089 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {\r | |
2090 | const char* msg =\r | |
2091 | "assertion is always true, perhaps remove parentheses?";\r | |
2092 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,\r | |
2093 | c->u->u_lineno, NULL, NULL) == -1)\r | |
2094 | return 0;\r | |
2095 | }\r | |
2096 | VISIT(c, expr, s->v.Assert.test);\r | |
2097 | end = compiler_new_block(c);\r | |
2098 | if (end == NULL)\r | |
2099 | return 0;\r | |
2100 | ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);\r | |
2101 | ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);\r | |
2102 | if (s->v.Assert.msg) {\r | |
2103 | VISIT(c, expr, s->v.Assert.msg);\r | |
2104 | ADDOP_I(c, CALL_FUNCTION, 1);\r | |
2105 | }\r | |
2106 | ADDOP_I(c, RAISE_VARARGS, 1);\r | |
2107 | compiler_use_next_block(c, end);\r | |
2108 | return 1;\r | |
2109 | }\r | |
2110 | \r | |
2111 | static int\r | |
2112 | compiler_visit_stmt(struct compiler *c, stmt_ty s)\r | |
2113 | {\r | |
2114 | int i, n;\r | |
2115 | \r | |
2116 | /* Always assign a lineno to the next instruction for a stmt. */\r | |
2117 | c->u->u_lineno = s->lineno;\r | |
2118 | c->u->u_lineno_set = false;\r | |
2119 | \r | |
2120 | switch (s->kind) {\r | |
2121 | case FunctionDef_kind:\r | |
2122 | return compiler_function(c, s);\r | |
2123 | case ClassDef_kind:\r | |
2124 | return compiler_class(c, s);\r | |
2125 | case Return_kind:\r | |
2126 | if (c->u->u_ste->ste_type != FunctionBlock)\r | |
2127 | return compiler_error(c, "'return' outside function");\r | |
2128 | if (s->v.Return.value) {\r | |
2129 | VISIT(c, expr, s->v.Return.value);\r | |
2130 | }\r | |
2131 | else\r | |
2132 | ADDOP_O(c, LOAD_CONST, Py_None, consts);\r | |
2133 | ADDOP(c, RETURN_VALUE);\r | |
2134 | break;\r | |
2135 | case Delete_kind:\r | |
2136 | VISIT_SEQ(c, expr, s->v.Delete.targets)\r | |
2137 | break;\r | |
2138 | case Assign_kind:\r | |
2139 | n = asdl_seq_LEN(s->v.Assign.targets);\r | |
2140 | VISIT(c, expr, s->v.Assign.value);\r | |
2141 | for (i = 0; i < n; i++) {\r | |
2142 | if (i < n - 1)\r | |
2143 | ADDOP(c, DUP_TOP);\r | |
2144 | VISIT(c, expr,\r | |
2145 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));\r | |
2146 | }\r | |
2147 | break;\r | |
2148 | case AugAssign_kind:\r | |
2149 | return compiler_augassign(c, s);\r | |
2150 | case Print_kind:\r | |
2151 | return compiler_print(c, s);\r | |
2152 | case For_kind:\r | |
2153 | return compiler_for(c, s);\r | |
2154 | case While_kind:\r | |
2155 | return compiler_while(c, s);\r | |
2156 | case If_kind:\r | |
2157 | return compiler_if(c, s);\r | |
2158 | case Raise_kind:\r | |
2159 | n = 0;\r | |
2160 | if (s->v.Raise.type) {\r | |
2161 | VISIT(c, expr, s->v.Raise.type);\r | |
2162 | n++;\r | |
2163 | if (s->v.Raise.inst) {\r | |
2164 | VISIT(c, expr, s->v.Raise.inst);\r | |
2165 | n++;\r | |
2166 | if (s->v.Raise.tback) {\r | |
2167 | VISIT(c, expr, s->v.Raise.tback);\r | |
2168 | n++;\r | |
2169 | }\r | |
2170 | }\r | |
2171 | }\r | |
2172 | ADDOP_I(c, RAISE_VARARGS, n);\r | |
2173 | break;\r | |
2174 | case TryExcept_kind:\r | |
2175 | return compiler_try_except(c, s);\r | |
2176 | case TryFinally_kind:\r | |
2177 | return compiler_try_finally(c, s);\r | |
2178 | case Assert_kind:\r | |
2179 | return compiler_assert(c, s);\r | |
2180 | case Import_kind:\r | |
2181 | return compiler_import(c, s);\r | |
2182 | case ImportFrom_kind:\r | |
2183 | return compiler_from_import(c, s);\r | |
2184 | case Exec_kind:\r | |
2185 | VISIT(c, expr, s->v.Exec.body);\r | |
2186 | if (s->v.Exec.globals) {\r | |
2187 | VISIT(c, expr, s->v.Exec.globals);\r | |
2188 | if (s->v.Exec.locals) {\r | |
2189 | VISIT(c, expr, s->v.Exec.locals);\r | |
2190 | } else {\r | |
2191 | ADDOP(c, DUP_TOP);\r | |
2192 | }\r | |
2193 | } else {\r | |
2194 | ADDOP_O(c, LOAD_CONST, Py_None, consts);\r | |
2195 | ADDOP(c, DUP_TOP);\r | |
2196 | }\r | |
2197 | ADDOP(c, EXEC_STMT);\r | |
2198 | break;\r | |
2199 | case Global_kind:\r | |
2200 | break;\r | |
2201 | case Expr_kind:\r | |
2202 | if (c->c_interactive && c->c_nestlevel <= 1) {\r | |
2203 | VISIT(c, expr, s->v.Expr.value);\r | |
2204 | ADDOP(c, PRINT_EXPR);\r | |
2205 | }\r | |
2206 | else if (s->v.Expr.value->kind != Str_kind &&\r | |
2207 | s->v.Expr.value->kind != Num_kind) {\r | |
2208 | VISIT(c, expr, s->v.Expr.value);\r | |
2209 | ADDOP(c, POP_TOP);\r | |
2210 | }\r | |
2211 | break;\r | |
2212 | case Pass_kind:\r | |
2213 | break;\r | |
2214 | case Break_kind:\r | |
2215 | if (!compiler_in_loop(c))\r | |
2216 | return compiler_error(c, "'break' outside loop");\r | |
2217 | ADDOP(c, BREAK_LOOP);\r | |
2218 | break;\r | |
2219 | case Continue_kind:\r | |
2220 | return compiler_continue(c);\r | |
2221 | case With_kind:\r | |
2222 | return compiler_with(c, s);\r | |
2223 | }\r | |
2224 | return 1;\r | |
2225 | }\r | |
2226 | \r | |
2227 | static int\r | |
2228 | unaryop(unaryop_ty op)\r | |
2229 | {\r | |
2230 | switch (op) {\r | |
2231 | case Invert:\r | |
2232 | return UNARY_INVERT;\r | |
2233 | case Not:\r | |
2234 | return UNARY_NOT;\r | |
2235 | case UAdd:\r | |
2236 | return UNARY_POSITIVE;\r | |
2237 | case USub:\r | |
2238 | return UNARY_NEGATIVE;\r | |
2239 | default:\r | |
2240 | PyErr_Format(PyExc_SystemError,\r | |
2241 | "unary op %d should not be possible", op);\r | |
2242 | return 0;\r | |
2243 | }\r | |
2244 | }\r | |
2245 | \r | |
2246 | static int\r | |
2247 | binop(struct compiler *c, operator_ty op)\r | |
2248 | {\r | |
2249 | switch (op) {\r | |
2250 | case Add:\r | |
2251 | return BINARY_ADD;\r | |
2252 | case Sub:\r | |
2253 | return BINARY_SUBTRACT;\r | |
2254 | case Mult:\r | |
2255 | return BINARY_MULTIPLY;\r | |
2256 | case Div:\r | |
2257 | if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)\r | |
2258 | return BINARY_TRUE_DIVIDE;\r | |
2259 | else\r | |
2260 | return BINARY_DIVIDE;\r | |
2261 | case Mod:\r | |
2262 | return BINARY_MODULO;\r | |
2263 | case Pow:\r | |
2264 | return BINARY_POWER;\r | |
2265 | case LShift:\r | |
2266 | return BINARY_LSHIFT;\r | |
2267 | case RShift:\r | |
2268 | return BINARY_RSHIFT;\r | |
2269 | case BitOr:\r | |
2270 | return BINARY_OR;\r | |
2271 | case BitXor:\r | |
2272 | return BINARY_XOR;\r | |
2273 | case BitAnd:\r | |
2274 | return BINARY_AND;\r | |
2275 | case FloorDiv:\r | |
2276 | return BINARY_FLOOR_DIVIDE;\r | |
2277 | default:\r | |
2278 | PyErr_Format(PyExc_SystemError,\r | |
2279 | "binary op %d should not be possible", op);\r | |
2280 | return 0;\r | |
2281 | }\r | |
2282 | }\r | |
2283 | \r | |
2284 | static int\r | |
2285 | cmpop(cmpop_ty op)\r | |
2286 | {\r | |
2287 | switch (op) {\r | |
2288 | case Eq:\r | |
2289 | return PyCmp_EQ;\r | |
2290 | case NotEq:\r | |
2291 | return PyCmp_NE;\r | |
2292 | case Lt:\r | |
2293 | return PyCmp_LT;\r | |
2294 | case LtE:\r | |
2295 | return PyCmp_LE;\r | |
2296 | case Gt:\r | |
2297 | return PyCmp_GT;\r | |
2298 | case GtE:\r | |
2299 | return PyCmp_GE;\r | |
2300 | case Is:\r | |
2301 | return PyCmp_IS;\r | |
2302 | case IsNot:\r | |
2303 | return PyCmp_IS_NOT;\r | |
2304 | case In:\r | |
2305 | return PyCmp_IN;\r | |
2306 | case NotIn:\r | |
2307 | return PyCmp_NOT_IN;\r | |
2308 | default:\r | |
2309 | return PyCmp_BAD;\r | |
2310 | }\r | |
2311 | }\r | |
2312 | \r | |
2313 | static int\r | |
2314 | inplace_binop(struct compiler *c, operator_ty op)\r | |
2315 | {\r | |
2316 | switch (op) {\r | |
2317 | case Add:\r | |
2318 | return INPLACE_ADD;\r | |
2319 | case Sub:\r | |
2320 | return INPLACE_SUBTRACT;\r | |
2321 | case Mult:\r | |
2322 | return INPLACE_MULTIPLY;\r | |
2323 | case Div:\r | |
2324 | if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)\r | |
2325 | return INPLACE_TRUE_DIVIDE;\r | |
2326 | else\r | |
2327 | return INPLACE_DIVIDE;\r | |
2328 | case Mod:\r | |
2329 | return INPLACE_MODULO;\r | |
2330 | case Pow:\r | |
2331 | return INPLACE_POWER;\r | |
2332 | case LShift:\r | |
2333 | return INPLACE_LSHIFT;\r | |
2334 | case RShift:\r | |
2335 | return INPLACE_RSHIFT;\r | |
2336 | case BitOr:\r | |
2337 | return INPLACE_OR;\r | |
2338 | case BitXor:\r | |
2339 | return INPLACE_XOR;\r | |
2340 | case BitAnd:\r | |
2341 | return INPLACE_AND;\r | |
2342 | case FloorDiv:\r | |
2343 | return INPLACE_FLOOR_DIVIDE;\r | |
2344 | default:\r | |
2345 | PyErr_Format(PyExc_SystemError,\r | |
2346 | "inplace binary op %d should not be possible", op);\r | |
2347 | return 0;\r | |
2348 | }\r | |
2349 | }\r | |
2350 | \r | |
2351 | static int\r | |
2352 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)\r | |
2353 | {\r | |
2354 | int op, scope, arg;\r | |
2355 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;\r | |
2356 | \r | |
2357 | PyObject *dict = c->u->u_names;\r | |
2358 | PyObject *mangled;\r | |
2359 | /* XXX AugStore isn't used anywhere! */\r | |
2360 | \r | |
2361 | mangled = _Py_Mangle(c->u->u_private, name);\r | |
2362 | if (!mangled)\r | |
2363 | return 0;\r | |
2364 | \r | |
2365 | op = 0;\r | |
2366 | optype = OP_NAME;\r | |
2367 | scope = PyST_GetScope(c->u->u_ste, mangled);\r | |
2368 | switch (scope) {\r | |
2369 | case FREE:\r | |
2370 | dict = c->u->u_freevars;\r | |
2371 | optype = OP_DEREF;\r | |
2372 | break;\r | |
2373 | case CELL:\r | |
2374 | dict = c->u->u_cellvars;\r | |
2375 | optype = OP_DEREF;\r | |
2376 | break;\r | |
2377 | case LOCAL:\r | |
2378 | if (c->u->u_ste->ste_type == FunctionBlock)\r | |
2379 | optype = OP_FAST;\r | |
2380 | break;\r | |
2381 | case GLOBAL_IMPLICIT:\r | |
2382 | if (c->u->u_ste->ste_type == FunctionBlock &&\r | |
2383 | !c->u->u_ste->ste_unoptimized)\r | |
2384 | optype = OP_GLOBAL;\r | |
2385 | break;\r | |
2386 | case GLOBAL_EXPLICIT:\r | |
2387 | optype = OP_GLOBAL;\r | |
2388 | break;\r | |
2389 | default:\r | |
2390 | /* scope can be 0 */\r | |
2391 | break;\r | |
2392 | }\r | |
2393 | \r | |
2394 | /* XXX Leave assert here, but handle __doc__ and the like better */\r | |
2395 | assert(scope || PyString_AS_STRING(name)[0] == '_');\r | |
2396 | \r | |
2397 | switch (optype) {\r | |
2398 | case OP_DEREF:\r | |
2399 | switch (ctx) {\r | |
2400 | case Load: op = LOAD_DEREF; break;\r | |
2401 | case Store: op = STORE_DEREF; break;\r | |
2402 | case AugLoad:\r | |
2403 | case AugStore:\r | |
2404 | break;\r | |
2405 | case Del:\r | |
2406 | PyErr_Format(PyExc_SyntaxError,\r | |
2407 | "can not delete variable '%s' referenced "\r | |
2408 | "in nested scope",\r | |
2409 | PyString_AS_STRING(name));\r | |
2410 | Py_DECREF(mangled);\r | |
2411 | return 0;\r | |
2412 | case Param:\r | |
2413 | default:\r | |
2414 | PyErr_SetString(PyExc_SystemError,\r | |
2415 | "param invalid for deref variable");\r | |
2416 | return 0;\r | |
2417 | }\r | |
2418 | break;\r | |
2419 | case OP_FAST:\r | |
2420 | switch (ctx) {\r | |
2421 | case Load: op = LOAD_FAST; break;\r | |
2422 | case Store: op = STORE_FAST; break;\r | |
2423 | case Del: op = DELETE_FAST; break;\r | |
2424 | case AugLoad:\r | |
2425 | case AugStore:\r | |
2426 | break;\r | |
2427 | case Param:\r | |
2428 | default:\r | |
2429 | PyErr_SetString(PyExc_SystemError,\r | |
2430 | "param invalid for local variable");\r | |
2431 | return 0;\r | |
2432 | }\r | |
2433 | ADDOP_O(c, op, mangled, varnames);\r | |
2434 | Py_DECREF(mangled);\r | |
2435 | return 1;\r | |
2436 | case OP_GLOBAL:\r | |
2437 | switch (ctx) {\r | |
2438 | case Load: op = LOAD_GLOBAL; break;\r | |
2439 | case Store: op = STORE_GLOBAL; break;\r | |
2440 | case Del: op = DELETE_GLOBAL; break;\r | |
2441 | case AugLoad:\r | |
2442 | case AugStore:\r | |
2443 | break;\r | |
2444 | case Param:\r | |
2445 | default:\r | |
2446 | PyErr_SetString(PyExc_SystemError,\r | |
2447 | "param invalid for global variable");\r | |
2448 | return 0;\r | |
2449 | }\r | |
2450 | break;\r | |
2451 | case OP_NAME:\r | |
2452 | switch (ctx) {\r | |
2453 | case Load: op = LOAD_NAME; break;\r | |
2454 | case Store: op = STORE_NAME; break;\r | |
2455 | case Del: op = DELETE_NAME; break;\r | |
2456 | case AugLoad:\r | |
2457 | case AugStore:\r | |
2458 | break;\r | |
2459 | case Param:\r | |
2460 | default:\r | |
2461 | PyErr_SetString(PyExc_SystemError,\r | |
2462 | "param invalid for name variable");\r | |
2463 | return 0;\r | |
2464 | }\r | |
2465 | break;\r | |
2466 | }\r | |
2467 | \r | |
2468 | assert(op);\r | |
2469 | arg = compiler_add_o(c, dict, mangled);\r | |
2470 | Py_DECREF(mangled);\r | |
2471 | if (arg < 0)\r | |
2472 | return 0;\r | |
2473 | return compiler_addop_i(c, op, arg);\r | |
2474 | }\r | |
2475 | \r | |
2476 | static int\r | |
2477 | compiler_boolop(struct compiler *c, expr_ty e)\r | |
2478 | {\r | |
2479 | basicblock *end;\r | |
2480 | int jumpi, i, n;\r | |
2481 | asdl_seq *s;\r | |
2482 | \r | |
2483 | assert(e->kind == BoolOp_kind);\r | |
2484 | if (e->v.BoolOp.op == And)\r | |
2485 | jumpi = JUMP_IF_FALSE_OR_POP;\r | |
2486 | else\r | |
2487 | jumpi = JUMP_IF_TRUE_OR_POP;\r | |
2488 | end = compiler_new_block(c);\r | |
2489 | if (end == NULL)\r | |
2490 | return 0;\r | |
2491 | s = e->v.BoolOp.values;\r | |
2492 | n = asdl_seq_LEN(s) - 1;\r | |
2493 | assert(n >= 0);\r | |
2494 | for (i = 0; i < n; ++i) {\r | |
2495 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));\r | |
2496 | ADDOP_JABS(c, jumpi, end);\r | |
2497 | }\r | |
2498 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));\r | |
2499 | compiler_use_next_block(c, end);\r | |
2500 | return 1;\r | |
2501 | }\r | |
2502 | \r | |
2503 | static int\r | |
2504 | compiler_list(struct compiler *c, expr_ty e)\r | |
2505 | {\r | |
2506 | int n = asdl_seq_LEN(e->v.List.elts);\r | |
2507 | if (e->v.List.ctx == Store) {\r | |
2508 | ADDOP_I(c, UNPACK_SEQUENCE, n);\r | |
2509 | }\r | |
2510 | VISIT_SEQ(c, expr, e->v.List.elts);\r | |
2511 | if (e->v.List.ctx == Load) {\r | |
2512 | ADDOP_I(c, BUILD_LIST, n);\r | |
2513 | }\r | |
2514 | return 1;\r | |
2515 | }\r | |
2516 | \r | |
2517 | static int\r | |
2518 | compiler_tuple(struct compiler *c, expr_ty e)\r | |
2519 | {\r | |
2520 | int n = asdl_seq_LEN(e->v.Tuple.elts);\r | |
2521 | if (e->v.Tuple.ctx == Store) {\r | |
2522 | ADDOP_I(c, UNPACK_SEQUENCE, n);\r | |
2523 | }\r | |
2524 | VISIT_SEQ(c, expr, e->v.Tuple.elts);\r | |
2525 | if (e->v.Tuple.ctx == Load) {\r | |
2526 | ADDOP_I(c, BUILD_TUPLE, n);\r | |
2527 | }\r | |
2528 | return 1;\r | |
2529 | }\r | |
2530 | \r | |
2531 | static int\r | |
2532 | compiler_compare(struct compiler *c, expr_ty e)\r | |
2533 | {\r | |
2534 | int i, n;\r | |
2535 | basicblock *cleanup = NULL;\r | |
2536 | \r | |
2537 | /* XXX the logic can be cleaned up for 1 or multiple comparisons */\r | |
2538 | VISIT(c, expr, e->v.Compare.left);\r | |
2539 | n = asdl_seq_LEN(e->v.Compare.ops);\r | |
2540 | assert(n > 0);\r | |
2541 | if (n > 1) {\r | |
2542 | cleanup = compiler_new_block(c);\r | |
2543 | if (cleanup == NULL)\r | |
2544 | return 0;\r | |
2545 | VISIT(c, expr,\r | |
2546 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));\r | |
2547 | }\r | |
2548 | for (i = 1; i < n; i++) {\r | |
2549 | ADDOP(c, DUP_TOP);\r | |
2550 | ADDOP(c, ROT_THREE);\r | |
2551 | ADDOP_I(c, COMPARE_OP,\r | |
2552 | cmpop((cmpop_ty)(asdl_seq_GET(\r | |
2553 | e->v.Compare.ops, i - 1))));\r | |
2554 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);\r | |
2555 | NEXT_BLOCK(c);\r | |
2556 | if (i < (n - 1))\r | |
2557 | VISIT(c, expr,\r | |
2558 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));\r | |
2559 | }\r | |
2560 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));\r | |
2561 | ADDOP_I(c, COMPARE_OP,\r | |
2562 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));\r | |
2563 | if (n > 1) {\r | |
2564 | basicblock *end = compiler_new_block(c);\r | |
2565 | if (end == NULL)\r | |
2566 | return 0;\r | |
2567 | ADDOP_JREL(c, JUMP_FORWARD, end);\r | |
2568 | compiler_use_next_block(c, cleanup);\r | |
2569 | ADDOP(c, ROT_TWO);\r | |
2570 | ADDOP(c, POP_TOP);\r | |
2571 | compiler_use_next_block(c, end);\r | |
2572 | }\r | |
2573 | return 1;\r | |
2574 | }\r | |
2575 | \r | |
2576 | static int\r | |
2577 | compiler_call(struct compiler *c, expr_ty e)\r | |
2578 | {\r | |
2579 | int n, code = 0;\r | |
2580 | \r | |
2581 | VISIT(c, expr, e->v.Call.func);\r | |
2582 | n = asdl_seq_LEN(e->v.Call.args);\r | |
2583 | VISIT_SEQ(c, expr, e->v.Call.args);\r | |
2584 | if (e->v.Call.keywords) {\r | |
2585 | VISIT_SEQ(c, keyword, e->v.Call.keywords);\r | |
2586 | n |= asdl_seq_LEN(e->v.Call.keywords) << 8;\r | |
2587 | }\r | |
2588 | if (e->v.Call.starargs) {\r | |
2589 | VISIT(c, expr, e->v.Call.starargs);\r | |
2590 | code |= 1;\r | |
2591 | }\r | |
2592 | if (e->v.Call.kwargs) {\r | |
2593 | VISIT(c, expr, e->v.Call.kwargs);\r | |
2594 | code |= 2;\r | |
2595 | }\r | |
2596 | switch (code) {\r | |
2597 | case 0:\r | |
2598 | ADDOP_I(c, CALL_FUNCTION, n);\r | |
2599 | break;\r | |
2600 | case 1:\r | |
2601 | ADDOP_I(c, CALL_FUNCTION_VAR, n);\r | |
2602 | break;\r | |
2603 | case 2:\r | |
2604 | ADDOP_I(c, CALL_FUNCTION_KW, n);\r | |
2605 | break;\r | |
2606 | case 3:\r | |
2607 | ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);\r | |
2608 | break;\r | |
2609 | }\r | |
2610 | return 1;\r | |
2611 | }\r | |
2612 | \r | |
2613 | static int\r | |
2614 | compiler_listcomp_generator(struct compiler *c, asdl_seq *generators,\r | |
2615 | int gen_index, expr_ty elt)\r | |
2616 | {\r | |
2617 | /* generate code for the iterator, then each of the ifs,\r | |
2618 | and then write to the element */\r | |
2619 | \r | |
2620 | comprehension_ty l;\r | |
2621 | basicblock *start, *anchor, *skip, *if_cleanup;\r | |
2622 | int i, n;\r | |
2623 | \r | |
2624 | start = compiler_new_block(c);\r | |
2625 | skip = compiler_new_block(c);\r | |
2626 | if_cleanup = compiler_new_block(c);\r | |
2627 | anchor = compiler_new_block(c);\r | |
2628 | \r | |
2629 | if (start == NULL || skip == NULL || if_cleanup == NULL ||\r | |
2630 | anchor == NULL)\r | |
2631 | return 0;\r | |
2632 | \r | |
2633 | l = (comprehension_ty)asdl_seq_GET(generators, gen_index);\r | |
2634 | VISIT(c, expr, l->iter);\r | |
2635 | ADDOP(c, GET_ITER);\r | |
2636 | compiler_use_next_block(c, start);\r | |
2637 | ADDOP_JREL(c, FOR_ITER, anchor);\r | |
2638 | NEXT_BLOCK(c);\r | |
2639 | VISIT(c, expr, l->target);\r | |
2640 | \r | |
2641 | /* XXX this needs to be cleaned up...a lot! */\r | |
2642 | n = asdl_seq_LEN(l->ifs);\r | |
2643 | for (i = 0; i < n; i++) {\r | |
2644 | expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);\r | |
2645 | VISIT(c, expr, e);\r | |
2646 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);\r | |
2647 | NEXT_BLOCK(c);\r | |
2648 | }\r | |
2649 | \r | |
2650 | if (++gen_index < asdl_seq_LEN(generators))\r | |
2651 | if (!compiler_listcomp_generator(c, generators, gen_index, elt))\r | |
2652 | return 0;\r | |
2653 | \r | |
2654 | /* only append after the last for generator */\r | |
2655 | if (gen_index >= asdl_seq_LEN(generators)) {\r | |
2656 | VISIT(c, expr, elt);\r | |
2657 | ADDOP_I(c, LIST_APPEND, gen_index+1);\r | |
2658 | \r | |
2659 | compiler_use_next_block(c, skip);\r | |
2660 | }\r | |
2661 | compiler_use_next_block(c, if_cleanup);\r | |
2662 | ADDOP_JABS(c, JUMP_ABSOLUTE, start);\r | |
2663 | compiler_use_next_block(c, anchor);\r | |
2664 | \r | |
2665 | return 1;\r | |
2666 | }\r | |
2667 | \r | |
2668 | static int\r | |
2669 | compiler_listcomp(struct compiler *c, expr_ty e)\r | |
2670 | {\r | |
2671 | assert(e->kind == ListComp_kind);\r | |
2672 | ADDOP_I(c, BUILD_LIST, 0);\r | |
2673 | return compiler_listcomp_generator(c, e->v.ListComp.generators, 0,\r | |
2674 | e->v.ListComp.elt);\r | |
2675 | }\r | |
2676 | \r | |
2677 | /* Dict and set comprehensions and generator expressions work by creating a\r | |
2678 | nested function to perform the actual iteration. This means that the\r | |
2679 | iteration variables don't leak into the current scope.\r | |
2680 | The defined function is called immediately following its definition, with the\r | |
2681 | result of that call being the result of the expression.\r | |
2682 | The LC/SC version returns the populated container, while the GE version is\r | |
2683 | flagged in symtable.c as a generator, so it returns the generator object\r | |
2684 | when the function is called.\r | |
2685 | This code *knows* that the loop cannot contain break, continue, or return,\r | |
2686 | so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.\r | |
2687 | \r | |
2688 | Possible cleanups:\r | |
2689 | - iterate over the generator sequence instead of using recursion\r | |
2690 | */\r | |
2691 | \r | |
2692 | static int\r | |
2693 | compiler_comprehension_generator(struct compiler *c,\r | |
2694 | asdl_seq *generators, int gen_index,\r | |
2695 | expr_ty elt, expr_ty val, int type)\r | |
2696 | {\r | |
2697 | /* generate code for the iterator, then each of the ifs,\r | |
2698 | and then write to the element */\r | |
2699 | \r | |
2700 | comprehension_ty gen;\r | |
2701 | basicblock *start, *anchor, *skip, *if_cleanup;\r | |
2702 | int i, n;\r | |
2703 | \r | |
2704 | start = compiler_new_block(c);\r | |
2705 | skip = compiler_new_block(c);\r | |
2706 | if_cleanup = compiler_new_block(c);\r | |
2707 | anchor = compiler_new_block(c);\r | |
2708 | \r | |
2709 | if (start == NULL || skip == NULL || if_cleanup == NULL ||\r | |
2710 | anchor == NULL)\r | |
2711 | return 0;\r | |
2712 | \r | |
2713 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);\r | |
2714 | \r | |
2715 | if (gen_index == 0) {\r | |
2716 | /* Receive outermost iter as an implicit argument */\r | |
2717 | c->u->u_argcount = 1;\r | |
2718 | ADDOP_I(c, LOAD_FAST, 0);\r | |
2719 | }\r | |
2720 | else {\r | |
2721 | /* Sub-iter - calculate on the fly */\r | |
2722 | VISIT(c, expr, gen->iter);\r | |
2723 | ADDOP(c, GET_ITER);\r | |
2724 | }\r | |
2725 | compiler_use_next_block(c, start);\r | |
2726 | ADDOP_JREL(c, FOR_ITER, anchor);\r | |
2727 | NEXT_BLOCK(c);\r | |
2728 | VISIT(c, expr, gen->target);\r | |
2729 | \r | |
2730 | /* XXX this needs to be cleaned up...a lot! */\r | |
2731 | n = asdl_seq_LEN(gen->ifs);\r | |
2732 | for (i = 0; i < n; i++) {\r | |
2733 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);\r | |
2734 | VISIT(c, expr, e);\r | |
2735 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);\r | |
2736 | NEXT_BLOCK(c);\r | |
2737 | }\r | |
2738 | \r | |
2739 | if (++gen_index < asdl_seq_LEN(generators))\r | |
2740 | if (!compiler_comprehension_generator(c,\r | |
2741 | generators, gen_index,\r | |
2742 | elt, val, type))\r | |
2743 | return 0;\r | |
2744 | \r | |
2745 | /* only append after the last for generator */\r | |
2746 | if (gen_index >= asdl_seq_LEN(generators)) {\r | |
2747 | /* comprehension specific code */\r | |
2748 | switch (type) {\r | |
2749 | case COMP_GENEXP:\r | |
2750 | VISIT(c, expr, elt);\r | |
2751 | ADDOP(c, YIELD_VALUE);\r | |
2752 | ADDOP(c, POP_TOP);\r | |
2753 | break;\r | |
2754 | case COMP_SETCOMP:\r | |
2755 | VISIT(c, expr, elt);\r | |
2756 | ADDOP_I(c, SET_ADD, gen_index + 1);\r | |
2757 | break;\r | |
2758 | case COMP_DICTCOMP:\r | |
2759 | /* With 'd[k] = v', v is evaluated before k, so we do\r | |
2760 | the same. */\r | |
2761 | VISIT(c, expr, val);\r | |
2762 | VISIT(c, expr, elt);\r | |
2763 | ADDOP_I(c, MAP_ADD, gen_index + 1);\r | |
2764 | break;\r | |
2765 | default:\r | |
2766 | return 0;\r | |
2767 | }\r | |
2768 | \r | |
2769 | compiler_use_next_block(c, skip);\r | |
2770 | }\r | |
2771 | compiler_use_next_block(c, if_cleanup);\r | |
2772 | ADDOP_JABS(c, JUMP_ABSOLUTE, start);\r | |
2773 | compiler_use_next_block(c, anchor);\r | |
2774 | \r | |
2775 | return 1;\r | |
2776 | }\r | |
2777 | \r | |
2778 | static int\r | |
2779 | compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,\r | |
2780 | asdl_seq *generators, expr_ty elt, expr_ty val)\r | |
2781 | {\r | |
2782 | PyCodeObject *co = NULL;\r | |
2783 | expr_ty outermost_iter;\r | |
2784 | \r | |
2785 | outermost_iter = ((comprehension_ty)\r | |
2786 | asdl_seq_GET(generators, 0))->iter;\r | |
2787 | \r | |
2788 | if (!compiler_enter_scope(c, name, (void *)e, e->lineno))\r | |
2789 | goto error;\r | |
2790 | \r | |
2791 | if (type != COMP_GENEXP) {\r | |
2792 | int op;\r | |
2793 | switch (type) {\r | |
2794 | case COMP_SETCOMP:\r | |
2795 | op = BUILD_SET;\r | |
2796 | break;\r | |
2797 | case COMP_DICTCOMP:\r | |
2798 | op = BUILD_MAP;\r | |
2799 | break;\r | |
2800 | default:\r | |
2801 | PyErr_Format(PyExc_SystemError,\r | |
2802 | "unknown comprehension type %d", type);\r | |
2803 | goto error_in_scope;\r | |
2804 | }\r | |
2805 | \r | |
2806 | ADDOP_I(c, op, 0);\r | |
2807 | }\r | |
2808 | \r | |
2809 | if (!compiler_comprehension_generator(c, generators, 0, elt,\r | |
2810 | val, type))\r | |
2811 | goto error_in_scope;\r | |
2812 | \r | |
2813 | if (type != COMP_GENEXP) {\r | |
2814 | ADDOP(c, RETURN_VALUE);\r | |
2815 | }\r | |
2816 | \r | |
2817 | co = assemble(c, 1);\r | |
2818 | compiler_exit_scope(c);\r | |
2819 | if (co == NULL)\r | |
2820 | goto error;\r | |
2821 | \r | |
2822 | if (!compiler_make_closure(c, co, 0))\r | |
2823 | goto error;\r | |
2824 | Py_DECREF(co);\r | |
2825 | \r | |
2826 | VISIT(c, expr, outermost_iter);\r | |
2827 | ADDOP(c, GET_ITER);\r | |
2828 | ADDOP_I(c, CALL_FUNCTION, 1);\r | |
2829 | return 1;\r | |
2830 | error_in_scope:\r | |
2831 | compiler_exit_scope(c);\r | |
2832 | error:\r | |
2833 | Py_XDECREF(co);\r | |
2834 | return 0;\r | |
2835 | }\r | |
2836 | \r | |
2837 | static int\r | |
2838 | compiler_genexp(struct compiler *c, expr_ty e)\r | |
2839 | {\r | |
2840 | static identifier name;\r | |
2841 | if (!name) {\r | |
2842 | name = PyString_FromString("<genexpr>");\r | |
2843 | if (!name)\r | |
2844 | return 0;\r | |
2845 | }\r | |
2846 | assert(e->kind == GeneratorExp_kind);\r | |
2847 | return compiler_comprehension(c, e, COMP_GENEXP, name,\r | |
2848 | e->v.GeneratorExp.generators,\r | |
2849 | e->v.GeneratorExp.elt, NULL);\r | |
2850 | }\r | |
2851 | \r | |
2852 | static int\r | |
2853 | compiler_setcomp(struct compiler *c, expr_ty e)\r | |
2854 | {\r | |
2855 | static identifier name;\r | |
2856 | if (!name) {\r | |
2857 | name = PyString_FromString("<setcomp>");\r | |
2858 | if (!name)\r | |
2859 | return 0;\r | |
2860 | }\r | |
2861 | assert(e->kind == SetComp_kind);\r | |
2862 | return compiler_comprehension(c, e, COMP_SETCOMP, name,\r | |
2863 | e->v.SetComp.generators,\r | |
2864 | e->v.SetComp.elt, NULL);\r | |
2865 | }\r | |
2866 | \r | |
2867 | static int\r | |
2868 | compiler_dictcomp(struct compiler *c, expr_ty e)\r | |
2869 | {\r | |
2870 | static identifier name;\r | |
2871 | if (!name) {\r | |
2872 | name = PyString_FromString("<dictcomp>");\r | |
2873 | if (!name)\r | |
2874 | return 0;\r | |
2875 | }\r | |
2876 | assert(e->kind == DictComp_kind);\r | |
2877 | return compiler_comprehension(c, e, COMP_DICTCOMP, name,\r | |
2878 | e->v.DictComp.generators,\r | |
2879 | e->v.DictComp.key, e->v.DictComp.value);\r | |
2880 | }\r | |
2881 | \r | |
2882 | static int\r | |
2883 | compiler_visit_keyword(struct compiler *c, keyword_ty k)\r | |
2884 | {\r | |
2885 | ADDOP_O(c, LOAD_CONST, k->arg, consts);\r | |
2886 | VISIT(c, expr, k->value);\r | |
2887 | return 1;\r | |
2888 | }\r | |
2889 | \r | |
2890 | /* Test whether expression is constant. For constants, report\r | |
2891 | whether they are true or false.\r | |
2892 | \r | |
2893 | Return values: 1 for true, 0 for false, -1 for non-constant.\r | |
2894 | */\r | |
2895 | \r | |
2896 | static int\r | |
2897 | expr_constant(expr_ty e)\r | |
2898 | {\r | |
2899 | switch (e->kind) {\r | |
2900 | case Num_kind:\r | |
2901 | return PyObject_IsTrue(e->v.Num.n);\r | |
2902 | case Str_kind:\r | |
2903 | return PyObject_IsTrue(e->v.Str.s);\r | |
2904 | case Name_kind:\r | |
2905 | /* __debug__ is not assignable, so we can optimize\r | |
2906 | * it away in if and while statements */\r | |
2907 | if (strcmp(PyString_AS_STRING(e->v.Name.id),\r | |
2908 | "__debug__") == 0)\r | |
2909 | return ! Py_OptimizeFlag;\r | |
2910 | /* fall through */\r | |
2911 | default:\r | |
2912 | return -1;\r | |
2913 | }\r | |
2914 | }\r | |
2915 | \r | |
2916 | /*\r | |
2917 | Implements the with statement from PEP 343.\r | |
2918 | \r | |
2919 | The semantics outlined in that PEP are as follows:\r | |
2920 | \r | |
2921 | with EXPR as VAR:\r | |
2922 | BLOCK\r | |
2923 | \r | |
2924 | It is implemented roughly as:\r | |
2925 | \r | |
2926 | context = EXPR\r | |
2927 | exit = context.__exit__ # not calling it\r | |
2928 | value = context.__enter__()\r | |
2929 | try:\r | |
2930 | VAR = value # if VAR present in the syntax\r | |
2931 | BLOCK\r | |
2932 | finally:\r | |
2933 | if an exception was raised:\r | |
2934 | exc = copy of (exception, instance, traceback)\r | |
2935 | else:\r | |
2936 | exc = (None, None, None)\r | |
2937 | exit(*exc)\r | |
2938 | */\r | |
2939 | static int\r | |
2940 | compiler_with(struct compiler *c, stmt_ty s)\r | |
2941 | {\r | |
2942 | basicblock *block, *finally;\r | |
2943 | \r | |
2944 | assert(s->kind == With_kind);\r | |
2945 | \r | |
2946 | block = compiler_new_block(c);\r | |
2947 | finally = compiler_new_block(c);\r | |
2948 | if (!block || !finally)\r | |
2949 | return 0;\r | |
2950 | \r | |
2951 | /* Evaluate EXPR */\r | |
2952 | VISIT(c, expr, s->v.With.context_expr);\r | |
2953 | ADDOP_JREL(c, SETUP_WITH, finally);\r | |
2954 | \r | |
2955 | /* SETUP_WITH pushes a finally block. */\r | |
2956 | compiler_use_next_block(c, block);\r | |
2957 | /* Note that the block is actually called SETUP_WITH in ceval.c, but\r | |
2958 | functions the same as SETUP_FINALLY except that exceptions are\r | |
2959 | normalized. */\r | |
2960 | if (!compiler_push_fblock(c, FINALLY_TRY, block)) {\r | |
2961 | return 0;\r | |
2962 | }\r | |
2963 | \r | |
2964 | if (s->v.With.optional_vars) {\r | |
2965 | VISIT(c, expr, s->v.With.optional_vars);\r | |
2966 | }\r | |
2967 | else {\r | |
2968 | /* Discard result from context.__enter__() */\r | |
2969 | ADDOP(c, POP_TOP);\r | |
2970 | }\r | |
2971 | \r | |
2972 | /* BLOCK code */\r | |
2973 | VISIT_SEQ(c, stmt, s->v.With.body);\r | |
2974 | \r | |
2975 | /* End of try block; start the finally block */\r | |
2976 | ADDOP(c, POP_BLOCK);\r | |
2977 | compiler_pop_fblock(c, FINALLY_TRY, block);\r | |
2978 | \r | |
2979 | ADDOP_O(c, LOAD_CONST, Py_None, consts);\r | |
2980 | compiler_use_next_block(c, finally);\r | |
2981 | if (!compiler_push_fblock(c, FINALLY_END, finally))\r | |
2982 | return 0;\r | |
2983 | \r | |
2984 | /* Finally block starts; context.__exit__ is on the stack under\r | |
2985 | the exception or return information. Just issue our magic\r | |
2986 | opcode. */\r | |
2987 | ADDOP(c, WITH_CLEANUP);\r | |
2988 | \r | |
2989 | /* Finally block ends. */\r | |
2990 | ADDOP(c, END_FINALLY);\r | |
2991 | compiler_pop_fblock(c, FINALLY_END, finally);\r | |
2992 | return 1;\r | |
2993 | }\r | |
2994 | \r | |
2995 | static int\r | |
2996 | compiler_visit_expr(struct compiler *c, expr_ty e)\r | |
2997 | {\r | |
2998 | int i, n;\r | |
2999 | \r | |
3000 | /* If expr e has a different line number than the last expr/stmt,\r | |
3001 | set a new line number for the next instruction.\r | |
3002 | */\r | |
3003 | if (e->lineno > c->u->u_lineno) {\r | |
3004 | c->u->u_lineno = e->lineno;\r | |
3005 | c->u->u_lineno_set = false;\r | |
3006 | }\r | |
3007 | switch (e->kind) {\r | |
3008 | case BoolOp_kind:\r | |
3009 | return compiler_boolop(c, e);\r | |
3010 | case BinOp_kind:\r | |
3011 | VISIT(c, expr, e->v.BinOp.left);\r | |
3012 | VISIT(c, expr, e->v.BinOp.right);\r | |
3013 | ADDOP(c, binop(c, e->v.BinOp.op));\r | |
3014 | break;\r | |
3015 | case UnaryOp_kind:\r | |
3016 | VISIT(c, expr, e->v.UnaryOp.operand);\r | |
3017 | ADDOP(c, unaryop(e->v.UnaryOp.op));\r | |
3018 | break;\r | |
3019 | case Lambda_kind:\r | |
3020 | return compiler_lambda(c, e);\r | |
3021 | case IfExp_kind:\r | |
3022 | return compiler_ifexp(c, e);\r | |
3023 | case Dict_kind:\r | |
3024 | n = asdl_seq_LEN(e->v.Dict.values);\r | |
3025 | ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));\r | |
3026 | for (i = 0; i < n; i++) {\r | |
3027 | VISIT(c, expr,\r | |
3028 | (expr_ty)asdl_seq_GET(e->v.Dict.values, i));\r | |
3029 | VISIT(c, expr,\r | |
3030 | (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));\r | |
3031 | ADDOP(c, STORE_MAP);\r | |
3032 | }\r | |
3033 | break;\r | |
3034 | case Set_kind:\r | |
3035 | n = asdl_seq_LEN(e->v.Set.elts);\r | |
3036 | VISIT_SEQ(c, expr, e->v.Set.elts);\r | |
3037 | ADDOP_I(c, BUILD_SET, n);\r | |
3038 | break;\r | |
3039 | case ListComp_kind:\r | |
3040 | return compiler_listcomp(c, e);\r | |
3041 | case SetComp_kind:\r | |
3042 | return compiler_setcomp(c, e);\r | |
3043 | case DictComp_kind:\r | |
3044 | return compiler_dictcomp(c, e);\r | |
3045 | case GeneratorExp_kind:\r | |
3046 | return compiler_genexp(c, e);\r | |
3047 | case Yield_kind:\r | |
3048 | if (c->u->u_ste->ste_type != FunctionBlock)\r | |
3049 | return compiler_error(c, "'yield' outside function");\r | |
3050 | if (e->v.Yield.value) {\r | |
3051 | VISIT(c, expr, e->v.Yield.value);\r | |
3052 | }\r | |
3053 | else {\r | |
3054 | ADDOP_O(c, LOAD_CONST, Py_None, consts);\r | |
3055 | }\r | |
3056 | ADDOP(c, YIELD_VALUE);\r | |
3057 | break;\r | |
3058 | case Compare_kind:\r | |
3059 | return compiler_compare(c, e);\r | |
3060 | case Call_kind:\r | |
3061 | return compiler_call(c, e);\r | |
3062 | case Repr_kind:\r | |
3063 | VISIT(c, expr, e->v.Repr.value);\r | |
3064 | ADDOP(c, UNARY_CONVERT);\r | |
3065 | break;\r | |
3066 | case Num_kind:\r | |
3067 | ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);\r | |
3068 | break;\r | |
3069 | case Str_kind:\r | |
3070 | ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);\r | |
3071 | break;\r | |
3072 | /* The following exprs can be assignment targets. */\r | |
3073 | case Attribute_kind:\r | |
3074 | if (e->v.Attribute.ctx != AugStore)\r | |
3075 | VISIT(c, expr, e->v.Attribute.value);\r | |
3076 | switch (e->v.Attribute.ctx) {\r | |
3077 | case AugLoad:\r | |
3078 | ADDOP(c, DUP_TOP);\r | |
3079 | /* Fall through to load */\r | |
3080 | case Load:\r | |
3081 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);\r | |
3082 | break;\r | |
3083 | case AugStore:\r | |
3084 | ADDOP(c, ROT_TWO);\r | |
3085 | /* Fall through to save */\r | |
3086 | case Store:\r | |
3087 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);\r | |
3088 | break;\r | |
3089 | case Del:\r | |
3090 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);\r | |
3091 | break;\r | |
3092 | case Param:\r | |
3093 | default:\r | |
3094 | PyErr_SetString(PyExc_SystemError,\r | |
3095 | "param invalid in attribute expression");\r | |
3096 | return 0;\r | |
3097 | }\r | |
3098 | break;\r | |
3099 | case Subscript_kind:\r | |
3100 | switch (e->v.Subscript.ctx) {\r | |
3101 | case AugLoad:\r | |
3102 | VISIT(c, expr, e->v.Subscript.value);\r | |
3103 | VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);\r | |
3104 | break;\r | |
3105 | case Load:\r | |
3106 | VISIT(c, expr, e->v.Subscript.value);\r | |
3107 | VISIT_SLICE(c, e->v.Subscript.slice, Load);\r | |
3108 | break;\r | |
3109 | case AugStore:\r | |
3110 | VISIT_SLICE(c, e->v.Subscript.slice, AugStore);\r | |
3111 | break;\r | |
3112 | case Store:\r | |
3113 | VISIT(c, expr, e->v.Subscript.value);\r | |
3114 | VISIT_SLICE(c, e->v.Subscript.slice, Store);\r | |
3115 | break;\r | |
3116 | case Del:\r | |
3117 | VISIT(c, expr, e->v.Subscript.value);\r | |
3118 | VISIT_SLICE(c, e->v.Subscript.slice, Del);\r | |
3119 | break;\r | |
3120 | case Param:\r | |
3121 | default:\r | |
3122 | PyErr_SetString(PyExc_SystemError,\r | |
3123 | "param invalid in subscript expression");\r | |
3124 | return 0;\r | |
3125 | }\r | |
3126 | break;\r | |
3127 | case Name_kind:\r | |
3128 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);\r | |
3129 | /* child nodes of List and Tuple will have expr_context set */\r | |
3130 | case List_kind:\r | |
3131 | return compiler_list(c, e);\r | |
3132 | case Tuple_kind:\r | |
3133 | return compiler_tuple(c, e);\r | |
3134 | }\r | |
3135 | return 1;\r | |
3136 | }\r | |
3137 | \r | |
3138 | static int\r | |
3139 | compiler_augassign(struct compiler *c, stmt_ty s)\r | |
3140 | {\r | |
3141 | expr_ty e = s->v.AugAssign.target;\r | |
3142 | expr_ty auge;\r | |
3143 | \r | |
3144 | assert(s->kind == AugAssign_kind);\r | |
3145 | \r | |
3146 | switch (e->kind) {\r | |
3147 | case Attribute_kind:\r | |
3148 | auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,\r | |
3149 | AugLoad, e->lineno, e->col_offset, c->c_arena);\r | |
3150 | if (auge == NULL)\r | |
3151 | return 0;\r | |
3152 | VISIT(c, expr, auge);\r | |
3153 | VISIT(c, expr, s->v.AugAssign.value);\r | |
3154 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op));\r | |
3155 | auge->v.Attribute.ctx = AugStore;\r | |
3156 | VISIT(c, expr, auge);\r | |
3157 | break;\r | |
3158 | case Subscript_kind:\r | |
3159 | auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,\r | |
3160 | AugLoad, e->lineno, e->col_offset, c->c_arena);\r | |
3161 | if (auge == NULL)\r | |
3162 | return 0;\r | |
3163 | VISIT(c, expr, auge);\r | |
3164 | VISIT(c, expr, s->v.AugAssign.value);\r | |
3165 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op));\r | |
3166 | auge->v.Subscript.ctx = AugStore;\r | |
3167 | VISIT(c, expr, auge);\r | |
3168 | break;\r | |
3169 | case Name_kind:\r | |
3170 | if (!compiler_nameop(c, e->v.Name.id, Load))\r | |
3171 | return 0;\r | |
3172 | VISIT(c, expr, s->v.AugAssign.value);\r | |
3173 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op));\r | |
3174 | return compiler_nameop(c, e->v.Name.id, Store);\r | |
3175 | default:\r | |
3176 | PyErr_Format(PyExc_SystemError,\r | |
3177 | "invalid node type (%d) for augmented assignment",\r | |
3178 | e->kind);\r | |
3179 | return 0;\r | |
3180 | }\r | |
3181 | return 1;\r | |
3182 | }\r | |
3183 | \r | |
3184 | static int\r | |
3185 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)\r | |
3186 | {\r | |
3187 | struct fblockinfo *f;\r | |
3188 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) {\r | |
3189 | PyErr_SetString(PyExc_SystemError,\r | |
3190 | "too many statically nested blocks");\r | |
3191 | return 0;\r | |
3192 | }\r | |
3193 | f = &c->u->u_fblock[c->u->u_nfblocks++];\r | |
3194 | f->fb_type = t;\r | |
3195 | f->fb_block = b;\r | |
3196 | return 1;\r | |
3197 | }\r | |
3198 | \r | |
3199 | static void\r | |
3200 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)\r | |
3201 | {\r | |
3202 | struct compiler_unit *u = c->u;\r | |
3203 | assert(u->u_nfblocks > 0);\r | |
3204 | u->u_nfblocks--;\r | |
3205 | assert(u->u_fblock[u->u_nfblocks].fb_type == t);\r | |
3206 | assert(u->u_fblock[u->u_nfblocks].fb_block == b);\r | |
3207 | }\r | |
3208 | \r | |
3209 | static int\r | |
3210 | compiler_in_loop(struct compiler *c) {\r | |
3211 | int i;\r | |
3212 | struct compiler_unit *u = c->u;\r | |
3213 | for (i = 0; i < u->u_nfblocks; ++i) {\r | |
3214 | if (u->u_fblock[i].fb_type == LOOP)\r | |
3215 | return 1;\r | |
3216 | }\r | |
3217 | return 0;\r | |
3218 | }\r | |
3219 | /* Raises a SyntaxError and returns 0.\r | |
3220 | If something goes wrong, a different exception may be raised.\r | |
3221 | */\r | |
3222 | \r | |
3223 | static int\r | |
3224 | compiler_error(struct compiler *c, const char *errstr)\r | |
3225 | {\r | |
3226 | PyObject *loc;\r | |
3227 | PyObject *u = NULL, *v = NULL;\r | |
3228 | \r | |
3229 | loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);\r | |
3230 | if (!loc) {\r | |
3231 | Py_INCREF(Py_None);\r | |
3232 | loc = Py_None;\r | |
3233 | }\r | |
3234 | u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,\r | |
3235 | Py_None, loc);\r | |
3236 | if (!u)\r | |
3237 | goto exit;\r | |
3238 | v = Py_BuildValue("(zO)", errstr, u);\r | |
3239 | if (!v)\r | |
3240 | goto exit;\r | |
3241 | PyErr_SetObject(PyExc_SyntaxError, v);\r | |
3242 | exit:\r | |
3243 | Py_DECREF(loc);\r | |
3244 | Py_XDECREF(u);\r | |
3245 | Py_XDECREF(v);\r | |
3246 | return 0;\r | |
3247 | }\r | |
3248 | \r | |
3249 | static int\r | |
3250 | compiler_handle_subscr(struct compiler *c, const char *kind,\r | |
3251 | expr_context_ty ctx)\r | |
3252 | {\r | |
3253 | int op = 0;\r | |
3254 | \r | |
3255 | /* XXX this code is duplicated */\r | |
3256 | switch (ctx) {\r | |
3257 | case AugLoad: /* fall through to Load */\r | |
3258 | case Load: op = BINARY_SUBSCR; break;\r | |
3259 | case AugStore:/* fall through to Store */\r | |
3260 | case Store: op = STORE_SUBSCR; break;\r | |
3261 | case Del: op = DELETE_SUBSCR; break;\r | |
3262 | case Param:\r | |
3263 | PyErr_Format(PyExc_SystemError,\r | |
3264 | "invalid %s kind %d in subscript\n",\r | |
3265 | kind, ctx);\r | |
3266 | return 0;\r | |
3267 | }\r | |
3268 | if (ctx == AugLoad) {\r | |
3269 | ADDOP_I(c, DUP_TOPX, 2);\r | |
3270 | }\r | |
3271 | else if (ctx == AugStore) {\r | |
3272 | ADDOP(c, ROT_THREE);\r | |
3273 | }\r | |
3274 | ADDOP(c, op);\r | |
3275 | return 1;\r | |
3276 | }\r | |
3277 | \r | |
3278 | static int\r | |
3279 | compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)\r | |
3280 | {\r | |
3281 | int n = 2;\r | |
3282 | assert(s->kind == Slice_kind);\r | |
3283 | \r | |
3284 | /* only handles the cases where BUILD_SLICE is emitted */\r | |
3285 | if (s->v.Slice.lower) {\r | |
3286 | VISIT(c, expr, s->v.Slice.lower);\r | |
3287 | }\r | |
3288 | else {\r | |
3289 | ADDOP_O(c, LOAD_CONST, Py_None, consts);\r | |
3290 | }\r | |
3291 | \r | |
3292 | if (s->v.Slice.upper) {\r | |
3293 | VISIT(c, expr, s->v.Slice.upper);\r | |
3294 | }\r | |
3295 | else {\r | |
3296 | ADDOP_O(c, LOAD_CONST, Py_None, consts);\r | |
3297 | }\r | |
3298 | \r | |
3299 | if (s->v.Slice.step) {\r | |
3300 | n++;\r | |
3301 | VISIT(c, expr, s->v.Slice.step);\r | |
3302 | }\r | |
3303 | ADDOP_I(c, BUILD_SLICE, n);\r | |
3304 | return 1;\r | |
3305 | }\r | |
3306 | \r | |
3307 | static int\r | |
3308 | compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)\r | |
3309 | {\r | |
3310 | int op = 0, slice_offset = 0, stack_count = 0;\r | |
3311 | \r | |
3312 | assert(s->v.Slice.step == NULL);\r | |
3313 | if (s->v.Slice.lower) {\r | |
3314 | slice_offset++;\r | |
3315 | stack_count++;\r | |
3316 | if (ctx != AugStore)\r | |
3317 | VISIT(c, expr, s->v.Slice.lower);\r | |
3318 | }\r | |
3319 | if (s->v.Slice.upper) {\r | |
3320 | slice_offset += 2;\r | |
3321 | stack_count++;\r | |
3322 | if (ctx != AugStore)\r | |
3323 | VISIT(c, expr, s->v.Slice.upper);\r | |
3324 | }\r | |
3325 | \r | |
3326 | if (ctx == AugLoad) {\r | |
3327 | switch (stack_count) {\r | |
3328 | case 0: ADDOP(c, DUP_TOP); break;\r | |
3329 | case 1: ADDOP_I(c, DUP_TOPX, 2); break;\r | |
3330 | case 2: ADDOP_I(c, DUP_TOPX, 3); break;\r | |
3331 | }\r | |
3332 | }\r | |
3333 | else if (ctx == AugStore) {\r | |
3334 | switch (stack_count) {\r | |
3335 | case 0: ADDOP(c, ROT_TWO); break;\r | |
3336 | case 1: ADDOP(c, ROT_THREE); break;\r | |
3337 | case 2: ADDOP(c, ROT_FOUR); break;\r | |
3338 | }\r | |
3339 | }\r | |
3340 | \r | |
3341 | switch (ctx) {\r | |
3342 | case AugLoad: /* fall through to Load */\r | |
3343 | case Load: op = SLICE; break;\r | |
3344 | case AugStore:/* fall through to Store */\r | |
3345 | case Store: op = STORE_SLICE; break;\r | |
3346 | case Del: op = DELETE_SLICE; break;\r | |
3347 | case Param:\r | |
3348 | default:\r | |
3349 | PyErr_SetString(PyExc_SystemError,\r | |
3350 | "param invalid in simple slice");\r | |
3351 | return 0;\r | |
3352 | }\r | |
3353 | \r | |
3354 | ADDOP(c, op + slice_offset);\r | |
3355 | return 1;\r | |
3356 | }\r | |
3357 | \r | |
3358 | static int\r | |
3359 | compiler_visit_nested_slice(struct compiler *c, slice_ty s,\r | |
3360 | expr_context_ty ctx)\r | |
3361 | {\r | |
3362 | switch (s->kind) {\r | |
3363 | case Ellipsis_kind:\r | |
3364 | ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);\r | |
3365 | break;\r | |
3366 | case Slice_kind:\r | |
3367 | return compiler_slice(c, s, ctx);\r | |
3368 | case Index_kind:\r | |
3369 | VISIT(c, expr, s->v.Index.value);\r | |
3370 | break;\r | |
3371 | case ExtSlice_kind:\r | |
3372 | default:\r | |
3373 | PyErr_SetString(PyExc_SystemError,\r | |
3374 | "extended slice invalid in nested slice");\r | |
3375 | return 0;\r | |
3376 | }\r | |
3377 | return 1;\r | |
3378 | }\r | |
3379 | \r | |
3380 | static int\r | |
3381 | compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)\r | |
3382 | {\r | |
3383 | char * kindname = NULL;\r | |
3384 | switch (s->kind) {\r | |
3385 | case Index_kind:\r | |
3386 | kindname = "index";\r | |
3387 | if (ctx != AugStore) {\r | |
3388 | VISIT(c, expr, s->v.Index.value);\r | |
3389 | }\r | |
3390 | break;\r | |
3391 | case Ellipsis_kind:\r | |
3392 | kindname = "ellipsis";\r | |
3393 | if (ctx != AugStore) {\r | |
3394 | ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);\r | |
3395 | }\r | |
3396 | break;\r | |
3397 | case Slice_kind:\r | |
3398 | kindname = "slice";\r | |
3399 | if (!s->v.Slice.step)\r | |
3400 | return compiler_simple_slice(c, s, ctx);\r | |
3401 | if (ctx != AugStore) {\r | |
3402 | if (!compiler_slice(c, s, ctx))\r | |
3403 | return 0;\r | |
3404 | }\r | |
3405 | break;\r | |
3406 | case ExtSlice_kind:\r | |
3407 | kindname = "extended slice";\r | |
3408 | if (ctx != AugStore) {\r | |
3409 | int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);\r | |
3410 | for (i = 0; i < n; i++) {\r | |
3411 | slice_ty sub = (slice_ty)asdl_seq_GET(\r | |
3412 | s->v.ExtSlice.dims, i);\r | |
3413 | if (!compiler_visit_nested_slice(c, sub, ctx))\r | |
3414 | return 0;\r | |
3415 | }\r | |
3416 | ADDOP_I(c, BUILD_TUPLE, n);\r | |
3417 | }\r | |
3418 | break;\r | |
3419 | default:\r | |
3420 | PyErr_Format(PyExc_SystemError,\r | |
3421 | "invalid subscript kind %d", s->kind);\r | |
3422 | return 0;\r | |
3423 | }\r | |
3424 | return compiler_handle_subscr(c, kindname, ctx);\r | |
3425 | }\r | |
3426 | \r | |
3427 | \r | |
3428 | /* End of the compiler section, beginning of the assembler section */\r | |
3429 | \r | |
3430 | /* do depth-first search of basic block graph, starting with block.\r | |
3431 | post records the block indices in post-order.\r | |
3432 | \r | |
3433 | XXX must handle implicit jumps from one block to next\r | |
3434 | */\r | |
3435 | \r | |
3436 | struct assembler {\r | |
3437 | PyObject *a_bytecode; /* string containing bytecode */\r | |
3438 | int a_offset; /* offset into bytecode */\r | |
3439 | int a_nblocks; /* number of reachable blocks */\r | |
3440 | basicblock **a_postorder; /* list of blocks in dfs postorder */\r | |
3441 | PyObject *a_lnotab; /* string containing lnotab */\r | |
3442 | int a_lnotab_off; /* offset into lnotab */\r | |
3443 | int a_lineno; /* last lineno of emitted instruction */\r | |
3444 | int a_lineno_off; /* bytecode offset of last lineno */\r | |
3445 | };\r | |
3446 | \r | |
3447 | static void\r | |
3448 | dfs(struct compiler *c, basicblock *b, struct assembler *a)\r | |
3449 | {\r | |
3450 | int i;\r | |
3451 | struct instr *instr = NULL;\r | |
3452 | \r | |
3453 | if (b->b_seen)\r | |
3454 | return;\r | |
3455 | b->b_seen = 1;\r | |
3456 | if (b->b_next != NULL)\r | |
3457 | dfs(c, b->b_next, a);\r | |
3458 | for (i = 0; i < b->b_iused; i++) {\r | |
3459 | instr = &b->b_instr[i];\r | |
3460 | if (instr->i_jrel || instr->i_jabs)\r | |
3461 | dfs(c, instr->i_target, a);\r | |
3462 | }\r | |
3463 | a->a_postorder[a->a_nblocks++] = b;\r | |
3464 | }\r | |
3465 | \r | |
3466 | static int\r | |
3467 | stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)\r | |
3468 | {\r | |
3469 | int i, target_depth;\r | |
3470 | struct instr *instr;\r | |
3471 | if (b->b_seen || b->b_startdepth >= depth)\r | |
3472 | return maxdepth;\r | |
3473 | b->b_seen = 1;\r | |
3474 | b->b_startdepth = depth;\r | |
3475 | for (i = 0; i < b->b_iused; i++) {\r | |
3476 | instr = &b->b_instr[i];\r | |
3477 | depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);\r | |
3478 | if (depth > maxdepth)\r | |
3479 | maxdepth = depth;\r | |
3480 | assert(depth >= 0); /* invalid code or bug in stackdepth() */\r | |
3481 | if (instr->i_jrel || instr->i_jabs) {\r | |
3482 | target_depth = depth;\r | |
3483 | if (instr->i_opcode == FOR_ITER) {\r | |
3484 | target_depth = depth-2;\r | |
3485 | }\r | |
3486 | else if (instr->i_opcode == SETUP_FINALLY ||\r | |
3487 | instr->i_opcode == SETUP_EXCEPT) {\r | |
3488 | target_depth = depth+3;\r | |
3489 | if (target_depth > maxdepth)\r | |
3490 | maxdepth = target_depth;\r | |
3491 | }\r | |
3492 | else if (instr->i_opcode == JUMP_IF_TRUE_OR_POP ||\r | |
3493 | instr->i_opcode == JUMP_IF_FALSE_OR_POP)\r | |
3494 | depth = depth - 1;\r | |
3495 | maxdepth = stackdepth_walk(c, instr->i_target,\r | |
3496 | target_depth, maxdepth);\r | |
3497 | if (instr->i_opcode == JUMP_ABSOLUTE ||\r | |
3498 | instr->i_opcode == JUMP_FORWARD) {\r | |
3499 | goto out; /* remaining code is dead */\r | |
3500 | }\r | |
3501 | }\r | |
3502 | }\r | |
3503 | if (b->b_next)\r | |
3504 | maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);\r | |
3505 | out:\r | |
3506 | b->b_seen = 0;\r | |
3507 | return maxdepth;\r | |
3508 | }\r | |
3509 | \r | |
3510 | /* Find the flow path that needs the largest stack. We assume that\r | |
3511 | * cycles in the flow graph have no net effect on the stack depth.\r | |
3512 | */\r | |
3513 | static int\r | |
3514 | stackdepth(struct compiler *c)\r | |
3515 | {\r | |
3516 | basicblock *b, *entryblock;\r | |
3517 | entryblock = NULL;\r | |
3518 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) {\r | |
3519 | b->b_seen = 0;\r | |
3520 | b->b_startdepth = INT_MIN;\r | |
3521 | entryblock = b;\r | |
3522 | }\r | |
3523 | if (!entryblock)\r | |
3524 | return 0;\r | |
3525 | return stackdepth_walk(c, entryblock, 0, 0);\r | |
3526 | }\r | |
3527 | \r | |
3528 | static int\r | |
3529 | assemble_init(struct assembler *a, int nblocks, int firstlineno)\r | |
3530 | {\r | |
3531 | memset(a, 0, sizeof(struct assembler));\r | |
3532 | a->a_lineno = firstlineno;\r | |
3533 | a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);\r | |
3534 | if (!a->a_bytecode)\r | |
3535 | return 0;\r | |
3536 | a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);\r | |
3537 | if (!a->a_lnotab)\r | |
3538 | return 0;\r | |
3539 | if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {\r | |
3540 | PyErr_NoMemory();\r | |
3541 | return 0;\r | |
3542 | }\r | |
3543 | a->a_postorder = (basicblock **)PyObject_Malloc(\r | |
3544 | sizeof(basicblock *) * nblocks);\r | |
3545 | if (!a->a_postorder) {\r | |
3546 | PyErr_NoMemory();\r | |
3547 | return 0;\r | |
3548 | }\r | |
3549 | return 1;\r | |
3550 | }\r | |
3551 | \r | |
3552 | static void\r | |
3553 | assemble_free(struct assembler *a)\r | |
3554 | {\r | |
3555 | Py_XDECREF(a->a_bytecode);\r | |
3556 | Py_XDECREF(a->a_lnotab);\r | |
3557 | if (a->a_postorder)\r | |
3558 | PyObject_Free(a->a_postorder);\r | |
3559 | }\r | |
3560 | \r | |
3561 | /* Return the size of a basic block in bytes. */\r | |
3562 | \r | |
3563 | static int\r | |
3564 | instrsize(struct instr *instr)\r | |
3565 | {\r | |
3566 | if (!instr->i_hasarg)\r | |
3567 | return 1; /* 1 byte for the opcode*/\r | |
3568 | if (instr->i_oparg > 0xffff)\r | |
3569 | return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */\r | |
3570 | return 3; /* 1 (opcode) + 2 (oparg) */\r | |
3571 | }\r | |
3572 | \r | |
3573 | static int\r | |
3574 | blocksize(basicblock *b)\r | |
3575 | {\r | |
3576 | int i;\r | |
3577 | int size = 0;\r | |
3578 | \r | |
3579 | for (i = 0; i < b->b_iused; i++)\r | |
3580 | size += instrsize(&b->b_instr[i]);\r | |
3581 | return size;\r | |
3582 | }\r | |
3583 | \r | |
3584 | /* Appends a pair to the end of the line number table, a_lnotab, representing\r | |
3585 | the instruction's bytecode offset and line number. See\r | |
3586 | Objects/lnotab_notes.txt for the description of the line number table. */\r | |
3587 | \r | |
3588 | static int\r | |
3589 | assemble_lnotab(struct assembler *a, struct instr *i)\r | |
3590 | {\r | |
3591 | int d_bytecode, d_lineno;\r | |
3592 | int len;\r | |
3593 | unsigned char *lnotab;\r | |
3594 | \r | |
3595 | d_bytecode = a->a_offset - a->a_lineno_off;\r | |
3596 | d_lineno = i->i_lineno - a->a_lineno;\r | |
3597 | \r | |
3598 | assert(d_bytecode >= 0);\r | |
3599 | assert(d_lineno >= 0);\r | |
3600 | \r | |
3601 | if(d_bytecode == 0 && d_lineno == 0)\r | |
3602 | return 1;\r | |
3603 | \r | |
3604 | if (d_bytecode > 255) {\r | |
3605 | int j, nbytes, ncodes = d_bytecode / 255;\r | |
3606 | nbytes = a->a_lnotab_off + 2 * ncodes;\r | |
3607 | len = PyString_GET_SIZE(a->a_lnotab);\r | |
3608 | if (nbytes >= len) {\r | |
3609 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes))\r | |
3610 | len = nbytes;\r | |
3611 | else if (len <= INT_MAX / 2)\r | |
3612 | len *= 2;\r | |
3613 | else {\r | |
3614 | PyErr_NoMemory();\r | |
3615 | return 0;\r | |
3616 | }\r | |
3617 | if (_PyString_Resize(&a->a_lnotab, len) < 0)\r | |
3618 | return 0;\r | |
3619 | }\r | |
3620 | lnotab = (unsigned char *)\r | |
3621 | PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;\r | |
3622 | for (j = 0; j < ncodes; j++) {\r | |
3623 | *lnotab++ = 255;\r | |
3624 | *lnotab++ = 0;\r | |
3625 | }\r | |
3626 | d_bytecode -= ncodes * 255;\r | |
3627 | a->a_lnotab_off += ncodes * 2;\r | |
3628 | }\r | |
3629 | assert(d_bytecode <= 255);\r | |
3630 | if (d_lineno > 255) {\r | |
3631 | int j, nbytes, ncodes = d_lineno / 255;\r | |
3632 | nbytes = a->a_lnotab_off + 2 * ncodes;\r | |
3633 | len = PyString_GET_SIZE(a->a_lnotab);\r | |
3634 | if (nbytes >= len) {\r | |
3635 | if ((len <= INT_MAX / 2) && len * 2 < nbytes)\r | |
3636 | len = nbytes;\r | |
3637 | else if (len <= INT_MAX / 2)\r | |
3638 | len *= 2;\r | |
3639 | else {\r | |
3640 | PyErr_NoMemory();\r | |
3641 | return 0;\r | |
3642 | }\r | |
3643 | if (_PyString_Resize(&a->a_lnotab, len) < 0)\r | |
3644 | return 0;\r | |
3645 | }\r | |
3646 | lnotab = (unsigned char *)\r | |
3647 | PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;\r | |
3648 | *lnotab++ = d_bytecode;\r | |
3649 | *lnotab++ = 255;\r | |
3650 | d_bytecode = 0;\r | |
3651 | for (j = 1; j < ncodes; j++) {\r | |
3652 | *lnotab++ = 0;\r | |
3653 | *lnotab++ = 255;\r | |
3654 | }\r | |
3655 | d_lineno -= ncodes * 255;\r | |
3656 | a->a_lnotab_off += ncodes * 2;\r | |
3657 | }\r | |
3658 | \r | |
3659 | len = PyString_GET_SIZE(a->a_lnotab);\r | |
3660 | if (a->a_lnotab_off + 2 >= len) {\r | |
3661 | if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)\r | |
3662 | return 0;\r | |
3663 | }\r | |
3664 | lnotab = (unsigned char *)\r | |
3665 | PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;\r | |
3666 | \r | |
3667 | a->a_lnotab_off += 2;\r | |
3668 | if (d_bytecode) {\r | |
3669 | *lnotab++ = d_bytecode;\r | |
3670 | *lnotab++ = d_lineno;\r | |
3671 | }\r | |
3672 | else { /* First line of a block; def stmt, etc. */\r | |
3673 | *lnotab++ = 0;\r | |
3674 | *lnotab++ = d_lineno;\r | |
3675 | }\r | |
3676 | a->a_lineno = i->i_lineno;\r | |
3677 | a->a_lineno_off = a->a_offset;\r | |
3678 | return 1;\r | |
3679 | }\r | |
3680 | \r | |
3681 | /* assemble_emit()\r | |
3682 | Extend the bytecode with a new instruction.\r | |
3683 | Update lnotab if necessary.\r | |
3684 | */\r | |
3685 | \r | |
3686 | static int\r | |
3687 | assemble_emit(struct assembler *a, struct instr *i)\r | |
3688 | {\r | |
3689 | int size, arg = 0, ext = 0;\r | |
3690 | Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);\r | |
3691 | char *code;\r | |
3692 | \r | |
3693 | size = instrsize(i);\r | |
3694 | if (i->i_hasarg) {\r | |
3695 | arg = i->i_oparg;\r | |
3696 | ext = arg >> 16;\r | |
3697 | }\r | |
3698 | if (i->i_lineno && !assemble_lnotab(a, i))\r | |
3699 | return 0;\r | |
3700 | if (a->a_offset + size >= len) {\r | |
3701 | if (len > PY_SSIZE_T_MAX / 2)\r | |
3702 | return 0;\r | |
3703 | if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)\r | |
3704 | return 0;\r | |
3705 | }\r | |
3706 | code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;\r | |
3707 | a->a_offset += size;\r | |
3708 | if (size == 6) {\r | |
3709 | assert(i->i_hasarg);\r | |
3710 | *code++ = (char)EXTENDED_ARG;\r | |
3711 | *code++ = ext & 0xff;\r | |
3712 | *code++ = ext >> 8;\r | |
3713 | arg &= 0xffff;\r | |
3714 | }\r | |
3715 | *code++ = i->i_opcode;\r | |
3716 | if (i->i_hasarg) {\r | |
3717 | assert(size == 3 || size == 6);\r | |
3718 | *code++ = arg & 0xff;\r | |
3719 | *code++ = arg >> 8;\r | |
3720 | }\r | |
3721 | return 1;\r | |
3722 | }\r | |
3723 | \r | |
3724 | static void\r | |
3725 | assemble_jump_offsets(struct assembler *a, struct compiler *c)\r | |
3726 | {\r | |
3727 | basicblock *b;\r | |
3728 | int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;\r | |
3729 | int i;\r | |
3730 | \r | |
3731 | /* Compute the size of each block and fixup jump args.\r | |
3732 | Replace block pointer with position in bytecode. */\r | |
3733 | do {\r | |
3734 | totsize = 0;\r | |
3735 | for (i = a->a_nblocks - 1; i >= 0; i--) {\r | |
3736 | b = a->a_postorder[i];\r | |
3737 | bsize = blocksize(b);\r | |
3738 | b->b_offset = totsize;\r | |
3739 | totsize += bsize;\r | |
3740 | }\r | |
3741 | last_extended_arg_count = extended_arg_count;\r | |
3742 | extended_arg_count = 0;\r | |
3743 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) {\r | |
3744 | bsize = b->b_offset;\r | |
3745 | for (i = 0; i < b->b_iused; i++) {\r | |
3746 | struct instr *instr = &b->b_instr[i];\r | |
3747 | /* Relative jumps are computed relative to\r | |
3748 | the instruction pointer after fetching\r | |
3749 | the jump instruction.\r | |
3750 | */\r | |
3751 | bsize += instrsize(instr);\r | |
3752 | if (instr->i_jabs)\r | |
3753 | instr->i_oparg = instr->i_target->b_offset;\r | |
3754 | else if (instr->i_jrel) {\r | |
3755 | int delta = instr->i_target->b_offset - bsize;\r | |
3756 | instr->i_oparg = delta;\r | |
3757 | }\r | |
3758 | else\r | |
3759 | continue;\r | |
3760 | if (instr->i_oparg > 0xffff)\r | |
3761 | extended_arg_count++;\r | |
3762 | }\r | |
3763 | }\r | |
3764 | \r | |
3765 | /* XXX: This is an awful hack that could hurt performance, but\r | |
3766 | on the bright side it should work until we come up\r | |
3767 | with a better solution.\r | |
3768 | \r | |
3769 | The issue is that in the first loop blocksize() is called\r | |
3770 | which calls instrsize() which requires i_oparg be set\r | |
3771 | appropriately. There is a bootstrap problem because\r | |
3772 | i_oparg is calculated in the second loop above.\r | |
3773 | \r | |
3774 | So we loop until we stop seeing new EXTENDED_ARGs.\r | |
3775 | The only EXTENDED_ARGs that could be popping up are\r | |
3776 | ones in jump instructions. So this should converge\r | |
3777 | fairly quickly.\r | |
3778 | */\r | |
3779 | } while (last_extended_arg_count != extended_arg_count);\r | |
3780 | }\r | |
3781 | \r | |
3782 | static PyObject *\r | |
3783 | dict_keys_inorder(PyObject *dict, int offset)\r | |
3784 | {\r | |
3785 | PyObject *tuple, *k, *v;\r | |
3786 | Py_ssize_t i, pos = 0, size = PyDict_Size(dict);\r | |
3787 | \r | |
3788 | tuple = PyTuple_New(size);\r | |
3789 | if (tuple == NULL)\r | |
3790 | return NULL;\r | |
3791 | while (PyDict_Next(dict, &pos, &k, &v)) {\r | |
3792 | i = PyInt_AS_LONG(v);\r | |
3793 | /* The keys of the dictionary are tuples. (see compiler_add_o)\r | |
3794 | The object we want is always first, though. */\r | |
3795 | k = PyTuple_GET_ITEM(k, 0);\r | |
3796 | Py_INCREF(k);\r | |
3797 | assert((i - offset) < size);\r | |
3798 | assert((i - offset) >= 0);\r | |
3799 | PyTuple_SET_ITEM(tuple, i - offset, k);\r | |
3800 | }\r | |
3801 | return tuple;\r | |
3802 | }\r | |
3803 | \r | |
3804 | static int\r | |
3805 | compute_code_flags(struct compiler *c)\r | |
3806 | {\r | |
3807 | PySTEntryObject *ste = c->u->u_ste;\r | |
3808 | int flags = 0, n;\r | |
3809 | if (ste->ste_type != ModuleBlock)\r | |
3810 | flags |= CO_NEWLOCALS;\r | |
3811 | if (ste->ste_type == FunctionBlock) {\r | |
3812 | if (!ste->ste_unoptimized)\r | |
3813 | flags |= CO_OPTIMIZED;\r | |
3814 | if (ste->ste_nested)\r | |
3815 | flags |= CO_NESTED;\r | |
3816 | if (ste->ste_generator)\r | |
3817 | flags |= CO_GENERATOR;\r | |
3818 | if (ste->ste_varargs)\r | |
3819 | flags |= CO_VARARGS;\r | |
3820 | if (ste->ste_varkeywords)\r | |
3821 | flags |= CO_VARKEYWORDS;\r | |
3822 | }\r | |
3823 | \r | |
3824 | /* (Only) inherit compilerflags in PyCF_MASK */\r | |
3825 | flags |= (c->c_flags->cf_flags & PyCF_MASK);\r | |
3826 | \r | |
3827 | n = PyDict_Size(c->u->u_freevars);\r | |
3828 | if (n < 0)\r | |
3829 | return -1;\r | |
3830 | if (n == 0) {\r | |
3831 | n = PyDict_Size(c->u->u_cellvars);\r | |
3832 | if (n < 0)\r | |
3833 | return -1;\r | |
3834 | if (n == 0) {\r | |
3835 | flags |= CO_NOFREE;\r | |
3836 | }\r | |
3837 | }\r | |
3838 | \r | |
3839 | return flags;\r | |
3840 | }\r | |
3841 | \r | |
3842 | static PyCodeObject *\r | |
3843 | makecode(struct compiler *c, struct assembler *a)\r | |
3844 | {\r | |
3845 | PyObject *tmp;\r | |
3846 | PyCodeObject *co = NULL;\r | |
3847 | PyObject *consts = NULL;\r | |
3848 | PyObject *names = NULL;\r | |
3849 | PyObject *varnames = NULL;\r | |
3850 | PyObject *filename = NULL;\r | |
3851 | PyObject *name = NULL;\r | |
3852 | PyObject *freevars = NULL;\r | |
3853 | PyObject *cellvars = NULL;\r | |
3854 | PyObject *bytecode = NULL;\r | |
3855 | int nlocals, flags;\r | |
3856 | \r | |
3857 | tmp = dict_keys_inorder(c->u->u_consts, 0);\r | |
3858 | if (!tmp)\r | |
3859 | goto error;\r | |
3860 | consts = PySequence_List(tmp); /* optimize_code requires a list */\r | |
3861 | Py_DECREF(tmp);\r | |
3862 | \r | |
3863 | names = dict_keys_inorder(c->u->u_names, 0);\r | |
3864 | varnames = dict_keys_inorder(c->u->u_varnames, 0);\r | |
3865 | if (!consts || !names || !varnames)\r | |
3866 | goto error;\r | |
3867 | \r | |
3868 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0);\r | |
3869 | if (!cellvars)\r | |
3870 | goto error;\r | |
3871 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));\r | |
3872 | if (!freevars)\r | |
3873 | goto error;\r | |
3874 | filename = PyString_FromString(c->c_filename);\r | |
3875 | if (!filename)\r | |
3876 | goto error;\r | |
3877 | \r | |
3878 | nlocals = PyDict_Size(c->u->u_varnames);\r | |
3879 | flags = compute_code_flags(c);\r | |
3880 | if (flags < 0)\r | |
3881 | goto error;\r | |
3882 | \r | |
3883 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);\r | |
3884 | if (!bytecode)\r | |
3885 | goto error;\r | |
3886 | \r | |
3887 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */\r | |
3888 | if (!tmp)\r | |
3889 | goto error;\r | |
3890 | Py_DECREF(consts);\r | |
3891 | consts = tmp;\r | |
3892 | \r | |
3893 | co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,\r | |
3894 | bytecode, consts, names, varnames,\r | |
3895 | freevars, cellvars,\r | |
3896 | filename, c->u->u_name,\r | |
3897 | c->u->u_firstlineno,\r | |
3898 | a->a_lnotab);\r | |
3899 | error:\r | |
3900 | Py_XDECREF(consts);\r | |
3901 | Py_XDECREF(names);\r | |
3902 | Py_XDECREF(varnames);\r | |
3903 | Py_XDECREF(filename);\r | |
3904 | Py_XDECREF(name);\r | |
3905 | Py_XDECREF(freevars);\r | |
3906 | Py_XDECREF(cellvars);\r | |
3907 | Py_XDECREF(bytecode);\r | |
3908 | return co;\r | |
3909 | }\r | |
3910 | \r | |
3911 | \r | |
3912 | /* For debugging purposes only */\r | |
3913 | #if 0\r | |
3914 | static void\r | |
3915 | dump_instr(const struct instr *i)\r | |
3916 | {\r | |
3917 | const char *jrel = i->i_jrel ? "jrel " : "";\r | |
3918 | const char *jabs = i->i_jabs ? "jabs " : "";\r | |
3919 | char arg[128];\r | |
3920 | \r | |
3921 | *arg = '\0';\r | |
3922 | if (i->i_hasarg)\r | |
3923 | sprintf(arg, "arg: %d ", i->i_oparg);\r | |
3924 | \r | |
3925 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",\r | |
3926 | i->i_lineno, i->i_opcode, arg, jabs, jrel);\r | |
3927 | }\r | |
3928 | \r | |
3929 | static void\r | |
3930 | dump_basicblock(const basicblock *b)\r | |
3931 | {\r | |
3932 | const char *seen = b->b_seen ? "seen " : "";\r | |
3933 | const char *b_return = b->b_return ? "return " : "";\r | |
3934 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",\r | |
3935 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);\r | |
3936 | if (b->b_instr) {\r | |
3937 | int i;\r | |
3938 | for (i = 0; i < b->b_iused; i++) {\r | |
3939 | fprintf(stderr, " [%02d] ", i);\r | |
3940 | dump_instr(b->b_instr + i);\r | |
3941 | }\r | |
3942 | }\r | |
3943 | }\r | |
3944 | #endif\r | |
3945 | \r | |
3946 | static PyCodeObject *\r | |
3947 | assemble(struct compiler *c, int addNone)\r | |
3948 | {\r | |
3949 | basicblock *b, *entryblock;\r | |
3950 | struct assembler a;\r | |
3951 | int i, j, nblocks;\r | |
3952 | PyCodeObject *co = NULL;\r | |
3953 | \r | |
3954 | /* Make sure every block that falls off the end returns None.\r | |
3955 | XXX NEXT_BLOCK() isn't quite right, because if the last\r | |
3956 | block ends with a jump or return b_next shouldn't set.\r | |
3957 | */\r | |
3958 | if (!c->u->u_curblock->b_return) {\r | |
3959 | NEXT_BLOCK(c);\r | |
3960 | if (addNone)\r | |
3961 | ADDOP_O(c, LOAD_CONST, Py_None, consts);\r | |
3962 | ADDOP(c, RETURN_VALUE);\r | |
3963 | }\r | |
3964 | \r | |
3965 | nblocks = 0;\r | |
3966 | entryblock = NULL;\r | |
3967 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) {\r | |
3968 | nblocks++;\r | |
3969 | entryblock = b;\r | |
3970 | }\r | |
3971 | \r | |
3972 | /* Set firstlineno if it wasn't explicitly set. */\r | |
3973 | if (!c->u->u_firstlineno) {\r | |
3974 | if (entryblock && entryblock->b_instr)\r | |
3975 | c->u->u_firstlineno = entryblock->b_instr->i_lineno;\r | |
3976 | else\r | |
3977 | c->u->u_firstlineno = 1;\r | |
3978 | }\r | |
3979 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno))\r | |
3980 | goto error;\r | |
3981 | dfs(c, entryblock, &a);\r | |
3982 | \r | |
3983 | /* Can't modify the bytecode after computing jump offsets. */\r | |
3984 | assemble_jump_offsets(&a, c);\r | |
3985 | \r | |
3986 | /* Emit code in reverse postorder from dfs. */\r | |
3987 | for (i = a.a_nblocks - 1; i >= 0; i--) {\r | |
3988 | b = a.a_postorder[i];\r | |
3989 | for (j = 0; j < b->b_iused; j++)\r | |
3990 | if (!assemble_emit(&a, &b->b_instr[j]))\r | |
3991 | goto error;\r | |
3992 | }\r | |
3993 | \r | |
3994 | if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)\r | |
3995 | goto error;\r | |
3996 | if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)\r | |
3997 | goto error;\r | |
3998 | \r | |
3999 | co = makecode(c, &a);\r | |
4000 | error:\r | |
4001 | assemble_free(&a);\r | |
4002 | return co;\r | |
4003 | }\r |