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