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