History

add hg and python

cinap_lenrek committed 15 years ago

Commit 4bb9e221 parent 987265c3706 files · +1228772 −-233Browse files

Showing the changes to sys/src/cmd/python/Python/compile.c (1 of 3706 files). Show all

Unified · Split

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