This commit was manufactured by cvs2svn to create tag 'r16a1'.
[python/dscho.git] / Python / compile.c
blob5aaf15e15f4d1750573a426dcecfd0a1ebf0a3e2
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Compile an expression node to intermediate code */
34 /* XXX TO DO:
35 XXX add __doc__ attribute == co_doc to code object attributes?
36 XXX (it's currently the first item of the co_const tuple)
37 XXX Generate simple jump for break/return outside 'try...finally'
38 XXX Allow 'continue' inside try-finally
39 XXX New opcode for loading the initial index for a for loop
40 XXX other JAR tricks?
43 #ifndef NO_PRIVATE_NAME_MANGLING
44 #define PRIVATE_NAME_MANGLING
45 #endif
47 #include "Python.h"
49 #include "node.h"
50 #include "token.h"
51 #include "graminit.h"
52 #include "compile.h"
53 #include "opcode.h"
54 #include "structmember.h"
56 #include <ctype.h>
58 /* Three symbols from graminit.h are also defined in Python.h, with
59 Py_ prefixes to their names. Python.h can't include graminit.h
60 (which defines too many confusing symbols), but we can check here
61 that they haven't changed (which is very unlikely, but possible). */
62 #if Py_single_input != single_input
63 #error "single_input has changed -- update Py_single_input in Python.h"
64 #endif
65 #if Py_file_input != file_input
66 #error "file_input has changed -- update Py_file_input in Python.h"
67 #endif
68 #if Py_eval_input != eval_input
69 #error "eval_input has changed -- update Py_eval_input in Python.h"
70 #endif
72 int Py_OptimizeFlag = 0;
74 #define OP_DELETE 0
75 #define OP_ASSIGN 1
76 #define OP_APPLY 2
78 #define OFF(x) offsetof(PyCodeObject, x)
80 static struct memberlist code_memberlist[] = {
81 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
82 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
83 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
84 {"co_flags", T_INT, OFF(co_flags), READONLY},
85 {"co_code", T_OBJECT, OFF(co_code), READONLY},
86 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
87 {"co_names", T_OBJECT, OFF(co_names), READONLY},
88 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
89 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
90 {"co_name", T_OBJECT, OFF(co_name), READONLY},
91 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
92 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
93 {NULL} /* Sentinel */
96 static PyObject *
97 code_getattr(co, name)
98 PyCodeObject *co;
99 char *name;
101 return PyMember_Get((char *)co, code_memberlist, name);
104 static void
105 code_dealloc(co)
106 PyCodeObject *co;
108 Py_XDECREF(co->co_code);
109 Py_XDECREF(co->co_consts);
110 Py_XDECREF(co->co_names);
111 Py_XDECREF(co->co_varnames);
112 Py_XDECREF(co->co_filename);
113 Py_XDECREF(co->co_name);
114 Py_XDECREF(co->co_lnotab);
115 PyMem_DEL(co);
118 static PyObject *
119 code_repr(co)
120 PyCodeObject *co;
122 char buf[500];
123 int lineno = -1;
124 unsigned char *p;
125 char *filename = "???";
126 char *name = "???";
128 _PyCode_GETCODEPTR(co, &p);
129 if (*p == SET_LINENO)
130 lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
131 if (co->co_filename && PyString_Check(co->co_filename))
132 filename = PyString_AsString(co->co_filename);
133 if (co->co_name && PyString_Check(co->co_name))
134 name = PyString_AsString(co->co_name);
135 sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
136 name, (long)co, filename, lineno);
137 return PyString_FromString(buf);
140 static int
141 code_compare(co, cp)
142 PyCodeObject *co, *cp;
144 int cmp;
145 cmp = co->co_argcount - cp->co_argcount;
146 if (cmp) return cmp;
147 cmp = co->co_nlocals - cp->co_nlocals;
148 if (cmp) return cmp;
149 cmp = co->co_flags - cp->co_flags;
150 if (cmp) return cmp;
151 cmp = PyObject_Compare(co->co_code, cp->co_code);
152 if (cmp) return cmp;
153 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
154 if (cmp) return cmp;
155 cmp = PyObject_Compare(co->co_names, cp->co_names);
156 if (cmp) return cmp;
157 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
158 return cmp;
161 static long
162 code_hash(co)
163 PyCodeObject *co;
165 long h, h1, h2, h3, h4;
166 h1 = PyObject_Hash(co->co_code);
167 if (h1 == -1) return -1;
168 h2 = PyObject_Hash(co->co_consts);
169 if (h2 == -1) return -1;
170 h3 = PyObject_Hash(co->co_names);
171 if (h3 == -1) return -1;
172 h4 = PyObject_Hash(co->co_varnames);
173 if (h4 == -1) return -1;
174 h = h1 ^ h2 ^ h3 ^ h4 ^
175 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
176 if (h == -1) h = -2;
177 return h;
180 PyTypeObject PyCode_Type = {
181 PyObject_HEAD_INIT(&PyType_Type)
183 "code",
184 sizeof(PyCodeObject),
186 (destructor)code_dealloc, /*tp_dealloc*/
187 0, /*tp_print*/
188 (getattrfunc)code_getattr, /*tp_getattr*/
189 0, /*tp_setattr*/
190 (cmpfunc)code_compare, /*tp_compare*/
191 (reprfunc)code_repr, /*tp_repr*/
192 0, /*tp_as_number*/
193 0, /*tp_as_sequence*/
194 0, /*tp_as_mapping*/
195 (hashfunc)code_hash, /*tp_hash*/
198 #define NAME_CHARS \
199 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
201 PyCodeObject *
202 PyCode_New(argcount, nlocals, stacksize, flags,
203 code, consts, names, varnames, filename, name,
204 firstlineno, lnotab)
205 int argcount;
206 int nlocals;
207 int stacksize;
208 int flags;
209 PyObject *code;
210 PyObject *consts;
211 PyObject *names;
212 PyObject *varnames;
213 PyObject *filename;
214 PyObject *name;
215 int firstlineno;
216 PyObject *lnotab;
218 PyCodeObject *co;
219 int i;
220 PyBufferProcs *pb;
221 /* Check argument types */
222 if (argcount < 0 || nlocals < 0 ||
223 code == NULL ||
224 consts == NULL || !PyTuple_Check(consts) ||
225 names == NULL || !PyTuple_Check(names) ||
226 varnames == NULL || !PyTuple_Check(varnames) ||
227 name == NULL || !PyString_Check(name) ||
228 filename == NULL || !PyString_Check(filename) ||
229 lnotab == NULL || !PyString_Check(lnotab)) {
230 PyErr_BadInternalCall();
231 return NULL;
233 pb = code->ob_type->tp_as_buffer;
234 if (pb == NULL ||
235 pb->bf_getreadbuffer == NULL ||
236 pb->bf_getsegcount == NULL ||
237 (*pb->bf_getsegcount)(code, NULL) != 1)
239 PyErr_BadInternalCall();
240 return NULL;
242 /* Make sure names and varnames are all strings, & intern them */
243 for (i = PyTuple_Size(names); --i >= 0; ) {
244 PyObject *v = PyTuple_GetItem(names, i);
245 if (v == NULL || !PyString_Check(v)) {
246 PyErr_BadInternalCall();
247 return NULL;
249 PyString_InternInPlace(&PyTuple_GET_ITEM(names, i));
251 for (i = PyTuple_Size(varnames); --i >= 0; ) {
252 PyObject *v = PyTuple_GetItem(varnames, i);
253 if (v == NULL || !PyString_Check(v)) {
254 PyErr_BadInternalCall();
255 return NULL;
257 PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i));
259 /* Intern selected string constants */
260 for (i = PyTuple_Size(consts); --i >= 0; ) {
261 PyObject *v = PyTuple_GetItem(consts, i);
262 char *p;
263 if (!PyString_Check(v))
264 continue;
265 p = PyString_AsString(v);
266 if ((int)strspn(p, NAME_CHARS)
267 != PyString_Size(v))
268 continue;
269 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
271 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
272 if (co != NULL) {
273 co->co_argcount = argcount;
274 co->co_nlocals = nlocals;
275 co->co_stacksize = stacksize;
276 co->co_flags = flags;
277 Py_INCREF(code);
278 co->co_code = code;
279 Py_INCREF(consts);
280 co->co_consts = consts;
281 Py_INCREF(names);
282 co->co_names = names;
283 Py_INCREF(varnames);
284 co->co_varnames = varnames;
285 Py_INCREF(filename);
286 co->co_filename = filename;
287 Py_INCREF(name);
288 co->co_name = name;
289 co->co_firstlineno = firstlineno;
290 Py_INCREF(lnotab);
291 co->co_lnotab = lnotab;
293 return co;
297 /* Data structure used internally */
299 struct compiling {
300 PyObject *c_code; /* string */
301 PyObject *c_consts; /* list of objects */
302 PyObject *c_names; /* list of strings (names) */
303 PyObject *c_globals; /* dictionary (value=None) */
304 PyObject *c_locals; /* dictionary (value=localID) */
305 PyObject *c_varnames; /* list (inverse of c_locals) */
306 int c_nlocals; /* index of next local */
307 int c_argcount; /* number of top-level arguments */
308 int c_flags; /* same as co_flags */
309 int c_nexti; /* index into c_code */
310 int c_errors; /* counts errors occurred */
311 int c_infunction; /* set when compiling a function */
312 int c_interactive; /* generating code for interactive command */
313 int c_loops; /* counts nested loops */
314 int c_begin; /* begin of current loop, for 'continue' */
315 int c_block[CO_MAXBLOCKS]; /* stack of block types */
316 int c_nblocks; /* current block stack level */
317 char *c_filename; /* filename of current node */
318 char *c_name; /* name of object (e.g. function) */
319 int c_lineno; /* Current line number */
320 int c_stacklevel; /* Current stack level */
321 int c_maxstacklevel; /* Maximum stack level */
322 int c_firstlineno;
323 PyObject *c_lnotab; /* Table mapping address to line number */
324 int c_last_addr, c_last_line, c_lnotab_next;
325 #ifdef PRIVATE_NAME_MANGLING
326 char *c_private; /* for private name mangling */
327 #endif
331 /* Error message including line number */
333 static void
334 com_error(c, exc, msg)
335 struct compiling *c;
336 PyObject *exc;
337 char *msg;
339 int n = strlen(msg);
340 PyObject *v;
341 char buffer[30];
342 char *s;
343 c->c_errors++;
344 if (c->c_lineno <= 1) {
345 /* Unknown line number or single interactive command */
346 PyErr_SetString(exc, msg);
347 return;
349 sprintf(buffer, " (line %d)", c->c_lineno);
350 v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer));
351 if (v == NULL)
352 return; /* MemoryError, too bad */
353 s = PyString_AS_STRING((PyStringObject *)v);
354 strcpy(s, msg);
355 strcat(s, buffer);
356 PyErr_SetObject(exc, v);
357 Py_DECREF(v);
361 /* Interface to the block stack */
363 static void
364 block_push(c, type)
365 struct compiling *c;
366 int type;
368 if (c->c_nblocks >= CO_MAXBLOCKS) {
369 com_error(c, PyExc_SystemError,
370 "too many statically nested blocks");
372 else {
373 c->c_block[c->c_nblocks++] = type;
377 static void
378 block_pop(c, type)
379 struct compiling *c;
380 int type;
382 if (c->c_nblocks > 0)
383 c->c_nblocks--;
384 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
385 com_error(c, PyExc_SystemError, "bad block pop");
390 /* Prototype forward declarations */
392 static int com_init Py_PROTO((struct compiling *, char *));
393 static void com_free Py_PROTO((struct compiling *));
394 static void com_push Py_PROTO((struct compiling *, int));
395 static void com_pop Py_PROTO((struct compiling *, int));
396 static void com_done Py_PROTO((struct compiling *));
397 static void com_node Py_PROTO((struct compiling *, struct _node *));
398 static void com_factor Py_PROTO((struct compiling *, struct _node *));
399 static void com_addbyte Py_PROTO((struct compiling *, int));
400 static void com_addint Py_PROTO((struct compiling *, int));
401 static void com_addoparg Py_PROTO((struct compiling *, int, int));
402 static void com_addfwref Py_PROTO((struct compiling *, int, int *));
403 static void com_backpatch Py_PROTO((struct compiling *, int));
404 static int com_add Py_PROTO((struct compiling *, PyObject *, PyObject *));
405 static int com_addconst Py_PROTO((struct compiling *, PyObject *));
406 static int com_addname Py_PROTO((struct compiling *, PyObject *));
407 static void com_addopname Py_PROTO((struct compiling *, int, node *));
408 static void com_list Py_PROTO((struct compiling *, node *, int));
409 static int com_argdefs Py_PROTO((struct compiling *, node *));
410 static int com_newlocal Py_PROTO((struct compiling *, char *));
411 static PyCodeObject *icompile Py_PROTO((struct _node *, struct compiling *));
412 static PyCodeObject *jcompile Py_PROTO((struct _node *, char *,
413 struct compiling *));
414 static PyObject *parsestrplus Py_PROTO((node *));
415 static PyObject *parsestr Py_PROTO((char *));
417 static int
418 com_init(c, filename)
419 struct compiling *c;
420 char *filename;
422 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
423 1000)) == NULL)
424 goto fail_3;
425 if ((c->c_consts = PyList_New(0)) == NULL)
426 goto fail_2;
427 if ((c->c_names = PyList_New(0)) == NULL)
428 goto fail_1;
429 if ((c->c_globals = PyDict_New()) == NULL)
430 goto fail_0;
431 if ((c->c_locals = PyDict_New()) == NULL)
432 goto fail_00;
433 if ((c->c_varnames = PyList_New(0)) == NULL)
434 goto fail_000;
435 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
436 1000)) == NULL)
437 goto fail_0000;
438 c->c_nlocals = 0;
439 c->c_argcount = 0;
440 c->c_flags = 0;
441 c->c_nexti = 0;
442 c->c_errors = 0;
443 c->c_infunction = 0;
444 c->c_interactive = 0;
445 c->c_loops = 0;
446 c->c_begin = 0;
447 c->c_nblocks = 0;
448 c->c_filename = filename;
449 c->c_name = "?";
450 c->c_lineno = 0;
451 c->c_stacklevel = 0;
452 c->c_maxstacklevel = 0;
453 c->c_firstlineno = 0;
454 c->c_last_addr = 0;
455 c->c_last_line = 0;
456 c-> c_lnotab_next = 0;
457 return 1;
459 fail_0000:
460 Py_DECREF(c->c_varnames);
461 fail_000:
462 Py_DECREF(c->c_locals);
463 fail_00:
464 Py_DECREF(c->c_globals);
465 fail_0:
466 Py_DECREF(c->c_names);
467 fail_1:
468 Py_DECREF(c->c_consts);
469 fail_2:
470 Py_DECREF(c->c_code);
471 fail_3:
472 return 0;
475 static void
476 com_free(c)
477 struct compiling *c;
479 Py_XDECREF(c->c_code);
480 Py_XDECREF(c->c_consts);
481 Py_XDECREF(c->c_names);
482 Py_XDECREF(c->c_globals);
483 Py_XDECREF(c->c_locals);
484 Py_XDECREF(c->c_varnames);
485 Py_XDECREF(c->c_lnotab);
488 static void
489 com_push(c, n)
490 struct compiling *c;
491 int n;
493 c->c_stacklevel += n;
494 if (c->c_stacklevel > c->c_maxstacklevel)
495 c->c_maxstacklevel = c->c_stacklevel;
498 static void
499 com_pop(c, n)
500 struct compiling *c;
501 int n;
503 if (c->c_stacklevel < n) {
504 /* fprintf(stderr,
505 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
506 c->c_filename, c->c_lineno,
507 c->c_nexti, c->c_stacklevel, n); */
508 c->c_stacklevel = 0;
510 else
511 c->c_stacklevel -= n;
514 static void
515 com_done(c)
516 struct compiling *c;
518 if (c->c_code != NULL)
519 _PyString_Resize(&c->c_code, c->c_nexti);
520 if (c->c_lnotab != NULL)
521 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
524 static void
525 com_addbyte(c, byte)
526 struct compiling *c;
527 int byte;
529 int len;
530 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
531 if (byte < 0 || byte > 255) {
533 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
534 fatal("com_addbyte: byte out of range");
536 com_error(c, PyExc_SystemError,
537 "com_addbyte: byte out of range");
539 if (c->c_code == NULL)
540 return;
541 len = PyString_Size(c->c_code);
542 if (c->c_nexti >= len) {
543 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
544 c->c_errors++;
545 return;
548 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
551 static void
552 com_addint(c, x)
553 struct compiling *c;
554 int x;
556 com_addbyte(c, x & 0xff);
557 com_addbyte(c, x >> 8); /* XXX x should be positive */
560 static void
561 com_add_lnotab(c, addr, line)
562 struct compiling *c;
563 int addr;
564 int line;
566 int size;
567 char *p;
568 if (c->c_lnotab == NULL)
569 return;
570 size = PyString_Size(c->c_lnotab);
571 if (c->c_lnotab_next+2 > size) {
572 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
573 c->c_errors++;
574 return;
577 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
578 *p++ = addr;
579 *p++ = line;
580 c->c_lnotab_next += 2;
583 static void
584 com_set_lineno(c, lineno)
585 struct compiling *c;
586 int lineno;
588 c->c_lineno = lineno;
589 if (c->c_firstlineno == 0) {
590 c->c_firstlineno = c->c_last_line = lineno;
592 else {
593 int incr_addr = c->c_nexti - c->c_last_addr;
594 int incr_line = lineno - c->c_last_line;
595 while (incr_addr > 0 || incr_line > 0) {
596 int trunc_addr = incr_addr;
597 int trunc_line = incr_line;
598 if (trunc_addr > 255)
599 trunc_addr = 255;
600 if (trunc_line > 255)
601 trunc_line = 255;
602 com_add_lnotab(c, trunc_addr, trunc_line);
603 incr_addr -= trunc_addr;
604 incr_line -= trunc_line;
606 c->c_last_addr = c->c_nexti;
607 c->c_last_line = lineno;
611 static void
612 com_addoparg(c, op, arg)
613 struct compiling *c;
614 int op;
615 int arg;
617 if (op == SET_LINENO) {
618 com_set_lineno(c, arg);
619 if (Py_OptimizeFlag)
620 return;
622 com_addbyte(c, op);
623 com_addint(c, arg);
626 static void
627 com_addfwref(c, op, p_anchor)
628 struct compiling *c;
629 int op;
630 int *p_anchor;
632 /* Compile a forward reference for backpatching */
633 int here;
634 int anchor;
635 com_addbyte(c, op);
636 here = c->c_nexti;
637 anchor = *p_anchor;
638 *p_anchor = here;
639 com_addint(c, anchor == 0 ? 0 : here - anchor);
642 static void
643 com_backpatch(c, anchor)
644 struct compiling *c;
645 int anchor; /* Must be nonzero */
647 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
648 int target = c->c_nexti;
649 int dist;
650 int prev;
651 for (;;) {
652 /* Make the JUMP instruction at anchor point to target */
653 prev = code[anchor] + (code[anchor+1] << 8);
654 dist = target - (anchor+2);
655 code[anchor] = dist & 0xff;
656 code[anchor+1] = dist >> 8;
657 if (!prev)
658 break;
659 anchor -= prev;
663 /* Handle literals and names uniformly */
665 static int
666 com_add(c, list, v)
667 struct compiling *c;
668 PyObject *list;
669 PyObject *v;
671 int n = PyList_Size(list);
672 int i;
673 /* XXX This is quadratic in the number of names per compilation unit.
674 XXX Should use a dictionary. */
675 for (i = n; --i >= 0; ) {
676 PyObject *w = PyList_GetItem(list, i);
677 if (v->ob_type == w->ob_type && PyObject_Compare(v, w) == 0)
678 return i;
680 /* Check for error from PyObject_Compare */
681 if (PyErr_Occurred() || PyList_Append(list, v) != 0)
682 c->c_errors++;
683 return n;
686 static int
687 com_addconst(c, v)
688 struct compiling *c;
689 PyObject *v;
691 return com_add(c, c->c_consts, v);
694 static int
695 com_addname(c, v)
696 struct compiling *c;
697 PyObject *v;
699 return com_add(c, c->c_names, v);
702 #ifdef PRIVATE_NAME_MANGLING
703 static int
704 com_mangle(c, name, buffer, maxlen)
705 struct compiling *c;
706 char *name;
707 char *buffer;
708 int maxlen;
710 /* Name mangling: __private becomes _classname__private.
711 This is independent from how the name is used. */
712 char *p;
713 int nlen, plen;
714 nlen = strlen(name);
715 if (nlen+2 >= maxlen)
716 return 0; /* Don't mangle __extremely_long_names */
717 if (name[nlen-1] == '_' && name[nlen-2] == '_')
718 return 0; /* Don't mangle __whatever__ */
719 p = c->c_private;
720 /* Strip leading underscores from class name */
721 while (*p == '_')
722 p++;
723 if (*p == '\0')
724 return 0; /* Don't mangle if class is just underscores */
725 plen = strlen(p);
726 if (plen + nlen >= maxlen)
727 plen = maxlen-nlen-2; /* Truncate class name if too long */
728 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
729 buffer[0] = '_';
730 strncpy(buffer+1, p, plen);
731 strcpy(buffer+1+plen, name);
732 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
733 return 1;
735 #endif
737 static void
738 com_addopnamestr(c, op, name)
739 struct compiling *c;
740 int op;
741 char *name;
743 PyObject *v;
744 int i;
745 #ifdef PRIVATE_NAME_MANGLING
746 char buffer[256];
747 if (name != NULL && name[0] == '_' && name[1] == '_' &&
748 c->c_private != NULL &&
749 com_mangle(c, name, buffer, (int)sizeof(buffer)))
750 name = buffer;
751 #endif
752 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
753 c->c_errors++;
754 i = 255;
756 else {
757 i = com_addname(c, v);
758 Py_DECREF(v);
760 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
761 switch (op) {
762 case LOAD_NAME:
763 case STORE_NAME:
764 case DELETE_NAME:
765 if (PyDict_GetItemString(c->c_globals, name) != NULL) {
766 switch (op) {
767 case LOAD_NAME: op = LOAD_GLOBAL; break;
768 case STORE_NAME: op = STORE_GLOBAL; break;
769 case DELETE_NAME: op = DELETE_GLOBAL; break;
773 com_addoparg(c, op, i);
776 static void
777 com_addopname(c, op, n)
778 struct compiling *c;
779 int op;
780 node *n;
782 char *name;
783 char buffer[1000];
784 /* XXX it is possible to write this code without the 1000
785 chars on the total length of dotted names, I just can't be
786 bothered right now */
787 if (TYPE(n) == STAR)
788 name = "*";
789 else if (TYPE(n) == dotted_name) {
790 char *p = buffer;
791 int i;
792 name = buffer;
793 for (i = 0; i < NCH(n); i += 2) {
794 char *s = STR(CHILD(n, i));
795 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
796 com_error(c, PyExc_MemoryError,
797 "dotted_name too long");
798 name = NULL;
799 break;
801 if (p != buffer)
802 *p++ = '.';
803 strcpy(p, s);
804 p = strchr(p, '\0');
807 else {
808 REQ(n, NAME);
809 name = STR(n);
811 com_addopnamestr(c, op, name);
814 static PyObject *
815 parsenumber(co, s)
816 struct compiling *co;
817 char *s;
819 extern double atof Py_PROTO((const char *));
820 char *end;
821 long x;
822 double dx;
823 #ifndef WITHOUT_COMPLEX
824 Py_complex c;
825 int imflag;
826 #endif
828 errno = 0;
829 end = s + strlen(s) - 1;
830 #ifndef WITHOUT_COMPLEX
831 imflag = *end == 'j' || *end == 'J';
832 #endif
833 if (*end == 'l' || *end == 'L')
834 return PyLong_FromString(s, (char **)0, 0);
835 if (s[0] == '0')
836 x = (long) PyOS_strtoul(s, &end, 0);
837 else
838 x = PyOS_strtol(s, &end, 0);
839 if (*end == '\0') {
840 if (errno != 0) {
841 com_error(co, PyExc_OverflowError,
842 "integer literal too large");
843 return NULL;
845 return PyInt_FromLong(x);
847 /* XXX Huge floats may silently fail */
848 #ifndef WITHOUT_COMPLEX
849 if (imflag) {
850 c.real = 0.;
851 PyFPE_START_PROTECT("atof", return 0)
852 c.imag = atof(s);
853 PyFPE_END_PROTECT(c)
854 return PyComplex_FromCComplex(c);
856 else
857 #endif
859 PyFPE_START_PROTECT("atof", return 0)
860 dx = atof(s);
861 PyFPE_END_PROTECT(dx)
862 return PyFloat_FromDouble(dx);
866 static PyObject *
867 parsestr(s)
868 char *s;
870 PyObject *v;
871 int len;
872 char *buf;
873 char *p;
874 char *end;
875 int c;
876 int first = *s;
877 int quote = first;
878 int rawmode = 0;
879 int unicode = 0;
880 if (isalpha(quote) || quote == '_') {
881 if (quote == 'u' || quote == 'U') {
882 quote = *++s;
883 unicode = 1;
885 if (quote == 'r' || quote == 'R') {
886 quote = *++s;
887 rawmode = 1;
890 if (quote != '\'' && quote != '\"') {
891 PyErr_BadInternalCall();
892 return NULL;
894 s++;
895 len = strlen(s);
896 if (s[--len] != quote) {
897 PyErr_BadInternalCall();
898 return NULL;
900 if (len >= 4 && s[0] == quote && s[1] == quote) {
901 s += 2;
902 len -= 2;
903 if (s[--len] != quote || s[--len] != quote) {
904 PyErr_BadInternalCall();
905 return NULL;
908 if (unicode) {
909 if (rawmode)
910 return PyUnicode_DecodeRawUnicodeEscape(
911 s, len, NULL);
912 else
913 return PyUnicode_DecodeUnicodeEscape(
914 s, len, NULL);
916 else if (rawmode || strchr(s, '\\') == NULL) {
917 return PyString_FromStringAndSize(s, len);
919 v = PyString_FromStringAndSize((char *)NULL, len);
920 p = buf = PyString_AsString(v);
921 end = s + len;
922 while (s < end) {
923 if (*s != '\\') {
924 *p++ = *s++;
925 continue;
927 s++;
928 switch (*s++) {
929 /* XXX This assumes ASCII! */
930 case '\n': break;
931 case '\\': *p++ = '\\'; break;
932 case '\'': *p++ = '\''; break;
933 case '\"': *p++ = '\"'; break;
934 case 'b': *p++ = '\b'; break;
935 case 'f': *p++ = '\014'; break; /* FF */
936 case 't': *p++ = '\t'; break;
937 case 'n': *p++ = '\n'; break;
938 case 'r': *p++ = '\r'; break;
939 case 'v': *p++ = '\013'; break; /* VT */
940 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
941 case '0': case '1': case '2': case '3':
942 case '4': case '5': case '6': case '7':
943 c = s[-1] - '0';
944 if ('0' <= *s && *s <= '7') {
945 c = (c<<3) + *s++ - '0';
946 if ('0' <= *s && *s <= '7')
947 c = (c<<3) + *s++ - '0';
949 *p++ = c;
950 break;
951 case 'x':
952 if (isxdigit(Py_CHARMASK(*s))) {
953 unsigned int x = 0;
954 do {
955 c = Py_CHARMASK(*s);
956 s++;
957 x = (x<<4) & ~0xF;
958 if (isdigit(c))
959 x += c - '0';
960 else if (islower(c))
961 x += 10 + c - 'a';
962 else
963 x += 10 + c - 'A';
964 } while (isxdigit(Py_CHARMASK(*s)));
965 *p++ = x;
966 break;
968 /* FALLTHROUGH */
969 default: *p++ = '\\'; *p++ = s[-1]; break;
972 _PyString_Resize(&v, (int)(p - buf));
973 return v;
976 static PyObject *
977 parsestrplus(n)
978 node *n;
980 PyObject *v;
981 int i;
982 REQ(CHILD(n, 0), STRING);
983 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
984 /* String literal concatenation */
985 for (i = 1; i < NCH(n) && v != NULL; i++) {
986 PyString_ConcatAndDel(&v, parsestr(STR(CHILD(n, i))));
989 return v;
992 static void
993 com_list_constructor(c, n)
994 struct compiling *c;
995 node *n;
997 int len;
998 int i;
999 if (TYPE(n) != testlist)
1000 REQ(n, exprlist);
1001 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
1002 len = (NCH(n) + 1) / 2;
1003 for (i = 0; i < NCH(n); i += 2)
1004 com_node(c, CHILD(n, i));
1005 com_addoparg(c, BUILD_LIST, len);
1006 com_pop(c, len-1);
1009 static void
1010 com_dictmaker(c, n)
1011 struct compiling *c;
1012 node *n;
1014 int i;
1015 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1016 for (i = 0; i+2 < NCH(n); i += 4) {
1017 /* We must arrange things just right for STORE_SUBSCR.
1018 It wants the stack to look like (value) (dict) (key) */
1019 com_addbyte(c, DUP_TOP);
1020 com_push(c, 1);
1021 com_node(c, CHILD(n, i+2)); /* value */
1022 com_addbyte(c, ROT_TWO);
1023 com_node(c, CHILD(n, i)); /* key */
1024 com_addbyte(c, STORE_SUBSCR);
1025 com_pop(c, 3);
1029 static void
1030 com_atom(c, n)
1031 struct compiling *c;
1032 node *n;
1034 node *ch;
1035 PyObject *v;
1036 int i;
1037 REQ(n, atom);
1038 ch = CHILD(n, 0);
1039 switch (TYPE(ch)) {
1040 case LPAR:
1041 if (TYPE(CHILD(n, 1)) == RPAR) {
1042 com_addoparg(c, BUILD_TUPLE, 0);
1043 com_push(c, 1);
1045 else
1046 com_node(c, CHILD(n, 1));
1047 break;
1048 case LSQB:
1049 if (TYPE(CHILD(n, 1)) == RSQB) {
1050 com_addoparg(c, BUILD_LIST, 0);
1051 com_push(c, 1);
1053 else
1054 com_list_constructor(c, CHILD(n, 1));
1055 break;
1056 case LBRACE: /* '{' [dictmaker] '}' */
1057 com_addoparg(c, BUILD_MAP, 0);
1058 com_push(c, 1);
1059 if (TYPE(CHILD(n, 1)) != RBRACE)
1060 com_dictmaker(c, CHILD(n, 1));
1061 break;
1062 case BACKQUOTE:
1063 com_node(c, CHILD(n, 1));
1064 com_addbyte(c, UNARY_CONVERT);
1065 break;
1066 case NUMBER:
1067 if ((v = parsenumber(c, STR(ch))) == NULL) {
1068 i = 255;
1070 else {
1071 i = com_addconst(c, v);
1072 Py_DECREF(v);
1074 com_addoparg(c, LOAD_CONST, i);
1075 com_push(c, 1);
1076 break;
1077 case STRING:
1078 v = parsestrplus(n);
1079 if (v == NULL) {
1080 c->c_errors++;
1081 i = 255;
1083 else {
1084 i = com_addconst(c, v);
1085 Py_DECREF(v);
1087 com_addoparg(c, LOAD_CONST, i);
1088 com_push(c, 1);
1089 break;
1090 case NAME:
1091 com_addopname(c, LOAD_NAME, ch);
1092 com_push(c, 1);
1093 break;
1094 default:
1095 /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
1096 com_error(c, PyExc_SystemError,
1097 "com_atom: unexpected node type");
1101 static void
1102 com_slice(c, n, op)
1103 struct compiling *c;
1104 node *n;
1105 int op;
1107 if (NCH(n) == 1) {
1108 com_addbyte(c, op);
1110 else if (NCH(n) == 2) {
1111 if (TYPE(CHILD(n, 0)) != COLON) {
1112 com_node(c, CHILD(n, 0));
1113 com_addbyte(c, op+1);
1115 else {
1116 com_node(c, CHILD(n, 1));
1117 com_addbyte(c, op+2);
1119 com_pop(c, 1);
1121 else {
1122 com_node(c, CHILD(n, 0));
1123 com_node(c, CHILD(n, 2));
1124 com_addbyte(c, op+3);
1125 com_pop(c, 2);
1129 static void
1130 com_argument(c, n, pkeywords)
1131 struct compiling *c;
1132 node *n; /* argument */
1133 PyObject **pkeywords;
1135 node *m;
1136 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
1137 if (NCH(n) == 1) {
1138 if (*pkeywords != NULL) {
1139 com_error(c, PyExc_SyntaxError,
1140 "non-keyword arg after keyword arg");
1142 else {
1143 com_node(c, CHILD(n, 0));
1145 return;
1147 m = n;
1148 do {
1149 m = CHILD(m, 0);
1150 } while (NCH(m) == 1);
1151 if (TYPE(m) != NAME) {
1152 com_error(c, PyExc_SyntaxError,
1153 "keyword can't be an expression");
1155 else {
1156 PyObject *v = PyString_InternFromString(STR(m));
1157 if (v != NULL && *pkeywords == NULL)
1158 *pkeywords = PyDict_New();
1159 if (v == NULL || *pkeywords == NULL)
1160 c->c_errors++;
1161 else {
1162 if (PyDict_GetItem(*pkeywords, v) != NULL)
1163 com_error(c, PyExc_SyntaxError,
1164 "duplicate keyword argument");
1165 else
1166 if (PyDict_SetItem(*pkeywords, v, v) != 0)
1167 c->c_errors++;
1168 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1169 com_push(c, 1);
1170 Py_DECREF(v);
1173 com_node(c, CHILD(n, 2));
1176 static void
1177 com_call_function(c, n)
1178 struct compiling *c;
1179 node *n; /* EITHER arglist OR ')' */
1181 if (TYPE(n) == RPAR) {
1182 com_addoparg(c, CALL_FUNCTION, 0);
1184 else {
1185 PyObject *keywords = NULL;
1186 int i, na, nk;
1187 int lineno = n->n_lineno;
1188 int star_flag = 0;
1189 int starstar_flag = 0;
1190 int opcode;
1191 REQ(n, arglist);
1192 na = 0;
1193 nk = 0;
1194 for (i = 0; i < NCH(n); i += 2) {
1195 node *ch = CHILD(n, i);
1196 if (TYPE(ch) == STAR ||
1197 TYPE(ch) == DOUBLESTAR)
1198 break;
1199 if (ch->n_lineno != lineno) {
1200 lineno = ch->n_lineno;
1201 com_addoparg(c, SET_LINENO, lineno);
1203 com_argument(c, ch, &keywords);
1204 if (keywords == NULL)
1205 na++;
1206 else
1207 nk++;
1209 Py_XDECREF(keywords);
1210 while (i < NCH(n)) {
1211 node *tok = CHILD(n, i);
1212 node *ch = CHILD(n, i+1);
1213 i += 3;
1214 switch (TYPE(tok)) {
1215 case STAR: star_flag = 1; break;
1216 case DOUBLESTAR: starstar_flag = 1; break;
1218 com_node(c, ch);
1220 if (na > 255 || nk > 255) {
1221 com_error(c, PyExc_SyntaxError,
1222 "more than 255 arguments");
1224 if (star_flag || starstar_flag)
1225 opcode = CALL_FUNCTION_VAR - 1 +
1226 star_flag + (starstar_flag << 1);
1227 else
1228 opcode = CALL_FUNCTION;
1229 com_addoparg(c, opcode, na | (nk << 8));
1230 com_pop(c, na + 2*nk + star_flag + starstar_flag);
1234 static void
1235 com_select_member(c, n)
1236 struct compiling *c;
1237 node *n;
1239 com_addopname(c, LOAD_ATTR, n);
1242 static void
1243 com_sliceobj(c, n)
1244 struct compiling *c;
1245 node *n;
1247 int i=0;
1248 int ns=2; /* number of slice arguments */
1249 node *ch;
1251 /* first argument */
1252 if (TYPE(CHILD(n,i)) == COLON) {
1253 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1254 com_push(c, 1);
1255 i++;
1257 else {
1258 com_node(c, CHILD(n,i));
1259 i++;
1260 REQ(CHILD(n,i),COLON);
1261 i++;
1263 /* second argument */
1264 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1265 com_node(c, CHILD(n,i));
1266 i++;
1268 else {
1269 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1270 com_push(c, 1);
1272 /* remaining arguments */
1273 for (; i < NCH(n); i++) {
1274 ns++;
1275 ch=CHILD(n,i);
1276 REQ(ch, sliceop);
1277 if (NCH(ch) == 1) {
1278 /* right argument of ':' missing */
1279 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1280 com_push(c, 1);
1282 else
1283 com_node(c, CHILD(ch,1));
1285 com_addoparg(c, BUILD_SLICE, ns);
1286 com_pop(c, 1 + (ns == 3));
1289 static void
1290 com_subscript(c, n)
1291 struct compiling *c;
1292 node *n;
1294 node *ch;
1295 REQ(n, subscript);
1296 ch = CHILD(n,0);
1297 /* check for rubber index */
1298 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
1299 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
1300 com_push(c, 1);
1302 else {
1303 /* check for slice */
1304 if ((TYPE(ch) == COLON || NCH(n) > 1))
1305 com_sliceobj(c, n);
1306 else {
1307 REQ(ch, test);
1308 com_node(c, ch);
1313 static void
1314 com_subscriptlist(c, n, assigning)
1315 struct compiling *c;
1316 node *n;
1317 int assigning;
1319 int i, op;
1320 REQ(n, subscriptlist);
1321 /* Check to make backward compatible slice behavior for '[i:j]' */
1322 if (NCH(n) == 1) {
1323 node *sub = CHILD(n, 0); /* subscript */
1324 /* Make it is a simple slice.
1325 Should have exactly one colon. */
1326 if ((TYPE(CHILD(sub, 0)) == COLON
1327 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1328 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1330 if (assigning == OP_APPLY)
1331 op = SLICE;
1332 else
1333 op = ((assigning == OP_ASSIGN) ?
1334 STORE_SLICE : DELETE_SLICE);
1335 com_slice(c, sub, op);
1336 if (op == STORE_SLICE)
1337 com_pop(c, 2);
1338 else if (op == DELETE_SLICE)
1339 com_pop(c, 1);
1340 return;
1343 /* Else normal subscriptlist. Compile each subscript. */
1344 for (i = 0; i < NCH(n); i += 2)
1345 com_subscript(c, CHILD(n, i));
1346 /* Put multiple subscripts into a tuple */
1347 if (NCH(n) > 1) {
1348 i = (NCH(n)+1) / 2;
1349 com_addoparg(c, BUILD_TUPLE, i);
1350 com_pop(c, i-1);
1352 if (assigning == OP_APPLY) {
1353 op = BINARY_SUBSCR;
1354 i = 1;
1356 else if (assigning == OP_ASSIGN) {
1357 op = STORE_SUBSCR;
1358 i = 3;
1360 else {
1361 op = DELETE_SUBSCR;
1362 i = 2;
1364 com_addbyte(c, op);
1365 com_pop(c, i);
1368 static void
1369 com_apply_trailer(c, n)
1370 struct compiling *c;
1371 node *n;
1373 REQ(n, trailer);
1374 switch (TYPE(CHILD(n, 0))) {
1375 case LPAR:
1376 com_call_function(c, CHILD(n, 1));
1377 break;
1378 case DOT:
1379 com_select_member(c, CHILD(n, 1));
1380 break;
1381 case LSQB:
1382 com_subscriptlist(c, CHILD(n, 1), OP_APPLY);
1383 break;
1384 default:
1385 com_error(c, PyExc_SystemError,
1386 "com_apply_trailer: unknown trailer type");
1390 static void
1391 com_power(c, n)
1392 struct compiling *c;
1393 node *n;
1395 int i;
1396 REQ(n, power);
1397 com_atom(c, CHILD(n, 0));
1398 for (i = 1; i < NCH(n); i++) {
1399 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1400 com_factor(c, CHILD(n, i+1));
1401 com_addbyte(c, BINARY_POWER);
1402 com_pop(c, 1);
1403 break;
1405 else
1406 com_apply_trailer(c, CHILD(n, i));
1410 static void
1411 com_factor(c, n)
1412 struct compiling *c;
1413 node *n;
1415 REQ(n, factor);
1416 if (TYPE(CHILD(n, 0)) == PLUS) {
1417 com_factor(c, CHILD(n, 1));
1418 com_addbyte(c, UNARY_POSITIVE);
1420 else if (TYPE(CHILD(n, 0)) == MINUS) {
1421 com_factor(c, CHILD(n, 1));
1422 com_addbyte(c, UNARY_NEGATIVE);
1424 else if (TYPE(CHILD(n, 0)) == TILDE) {
1425 com_factor(c, CHILD(n, 1));
1426 com_addbyte(c, UNARY_INVERT);
1428 else {
1429 com_power(c, CHILD(n, 0));
1433 static void
1434 com_term(c, n)
1435 struct compiling *c;
1436 node *n;
1438 int i;
1439 int op;
1440 REQ(n, term);
1441 com_factor(c, CHILD(n, 0));
1442 for (i = 2; i < NCH(n); i += 2) {
1443 com_factor(c, CHILD(n, i));
1444 switch (TYPE(CHILD(n, i-1))) {
1445 case STAR:
1446 op = BINARY_MULTIPLY;
1447 break;
1448 case SLASH:
1449 op = BINARY_DIVIDE;
1450 break;
1451 case PERCENT:
1452 op = BINARY_MODULO;
1453 break;
1454 default:
1455 com_error(c, PyExc_SystemError,
1456 "com_term: operator not *, / or %");
1457 op = 255;
1459 com_addbyte(c, op);
1460 com_pop(c, 1);
1464 static void
1465 com_arith_expr(c, n)
1466 struct compiling *c;
1467 node *n;
1469 int i;
1470 int op;
1471 REQ(n, arith_expr);
1472 com_term(c, CHILD(n, 0));
1473 for (i = 2; i < NCH(n); i += 2) {
1474 com_term(c, CHILD(n, i));
1475 switch (TYPE(CHILD(n, i-1))) {
1476 case PLUS:
1477 op = BINARY_ADD;
1478 break;
1479 case MINUS:
1480 op = BINARY_SUBTRACT;
1481 break;
1482 default:
1483 com_error(c, PyExc_SystemError,
1484 "com_arith_expr: operator not + or -");
1485 op = 255;
1487 com_addbyte(c, op);
1488 com_pop(c, 1);
1492 static void
1493 com_shift_expr(c, n)
1494 struct compiling *c;
1495 node *n;
1497 int i;
1498 int op;
1499 REQ(n, shift_expr);
1500 com_arith_expr(c, CHILD(n, 0));
1501 for (i = 2; i < NCH(n); i += 2) {
1502 com_arith_expr(c, CHILD(n, i));
1503 switch (TYPE(CHILD(n, i-1))) {
1504 case LEFTSHIFT:
1505 op = BINARY_LSHIFT;
1506 break;
1507 case RIGHTSHIFT:
1508 op = BINARY_RSHIFT;
1509 break;
1510 default:
1511 com_error(c, PyExc_SystemError,
1512 "com_shift_expr: operator not << or >>");
1513 op = 255;
1515 com_addbyte(c, op);
1516 com_pop(c, 1);
1520 static void
1521 com_and_expr(c, n)
1522 struct compiling *c;
1523 node *n;
1525 int i;
1526 int op;
1527 REQ(n, and_expr);
1528 com_shift_expr(c, CHILD(n, 0));
1529 for (i = 2; i < NCH(n); i += 2) {
1530 com_shift_expr(c, CHILD(n, i));
1531 if (TYPE(CHILD(n, i-1)) == AMPER) {
1532 op = BINARY_AND;
1534 else {
1535 com_error(c, PyExc_SystemError,
1536 "com_and_expr: operator not &");
1537 op = 255;
1539 com_addbyte(c, op);
1540 com_pop(c, 1);
1544 static void
1545 com_xor_expr(c, n)
1546 struct compiling *c;
1547 node *n;
1549 int i;
1550 int op;
1551 REQ(n, xor_expr);
1552 com_and_expr(c, CHILD(n, 0));
1553 for (i = 2; i < NCH(n); i += 2) {
1554 com_and_expr(c, CHILD(n, i));
1555 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1556 op = BINARY_XOR;
1558 else {
1559 com_error(c, PyExc_SystemError,
1560 "com_xor_expr: operator not ^");
1561 op = 255;
1563 com_addbyte(c, op);
1564 com_pop(c, 1);
1568 static void
1569 com_expr(c, n)
1570 struct compiling *c;
1571 node *n;
1573 int i;
1574 int op;
1575 REQ(n, expr);
1576 com_xor_expr(c, CHILD(n, 0));
1577 for (i = 2; i < NCH(n); i += 2) {
1578 com_xor_expr(c, CHILD(n, i));
1579 if (TYPE(CHILD(n, i-1)) == VBAR) {
1580 op = BINARY_OR;
1582 else {
1583 com_error(c, PyExc_SystemError,
1584 "com_expr: expr operator not |");
1585 op = 255;
1587 com_addbyte(c, op);
1588 com_pop(c, 1);
1592 static enum cmp_op
1593 cmp_type(n)
1594 node *n;
1596 REQ(n, comp_op);
1597 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1598 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1599 if (NCH(n) == 1) {
1600 n = CHILD(n, 0);
1601 switch (TYPE(n)) {
1602 case LESS: return LT;
1603 case GREATER: return GT;
1604 case EQEQUAL: /* == */
1605 case EQUAL: return EQ;
1606 case LESSEQUAL: return LE;
1607 case GREATEREQUAL: return GE;
1608 case NOTEQUAL: return NE; /* <> or != */
1609 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1610 if (strcmp(STR(n), "is") == 0) return IS;
1613 else if (NCH(n) == 2) {
1614 switch (TYPE(CHILD(n, 0))) {
1615 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1616 return NOT_IN;
1617 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1618 return IS_NOT;
1621 return BAD;
1624 static void
1625 com_comparison(c, n)
1626 struct compiling *c;
1627 node *n;
1629 int i;
1630 enum cmp_op op;
1631 int anchor;
1632 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1633 com_expr(c, CHILD(n, 0));
1634 if (NCH(n) == 1)
1635 return;
1637 /****************************************************************
1638 The following code is generated for all but the last
1639 comparison in a chain:
1641 label: on stack: opcode: jump to:
1643 a <code to load b>
1644 a, b DUP_TOP
1645 a, b, b ROT_THREE
1646 b, a, b COMPARE_OP
1647 b, 0-or-1 JUMP_IF_FALSE L1
1648 b, 1 POP_TOP
1651 We are now ready to repeat this sequence for the next
1652 comparison in the chain.
1654 For the last we generate:
1656 b <code to load c>
1657 b, c COMPARE_OP
1658 0-or-1
1660 If there were any jumps to L1 (i.e., there was more than one
1661 comparison), we generate:
1663 0-or-1 JUMP_FORWARD L2
1664 L1: b, 0 ROT_TWO
1665 0, b POP_TOP
1667 L2: 0-or-1
1668 ****************************************************************/
1670 anchor = 0;
1672 for (i = 2; i < NCH(n); i += 2) {
1673 com_expr(c, CHILD(n, i));
1674 if (i+2 < NCH(n)) {
1675 com_addbyte(c, DUP_TOP);
1676 com_push(c, 1);
1677 com_addbyte(c, ROT_THREE);
1679 op = cmp_type(CHILD(n, i-1));
1680 if (op == BAD) {
1681 com_error(c, PyExc_SystemError,
1682 "com_comparison: unknown comparison op");
1684 com_addoparg(c, COMPARE_OP, op);
1685 com_pop(c, 1);
1686 if (i+2 < NCH(n)) {
1687 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1688 com_addbyte(c, POP_TOP);
1689 com_pop(c, 1);
1693 if (anchor) {
1694 int anchor2 = 0;
1695 com_addfwref(c, JUMP_FORWARD, &anchor2);
1696 com_backpatch(c, anchor);
1697 com_addbyte(c, ROT_TWO);
1698 com_addbyte(c, POP_TOP);
1699 com_backpatch(c, anchor2);
1703 static void
1704 com_not_test(c, n)
1705 struct compiling *c;
1706 node *n;
1708 REQ(n, not_test); /* 'not' not_test | comparison */
1709 if (NCH(n) == 1) {
1710 com_comparison(c, CHILD(n, 0));
1712 else {
1713 com_not_test(c, CHILD(n, 1));
1714 com_addbyte(c, UNARY_NOT);
1718 static void
1719 com_and_test(c, n)
1720 struct compiling *c;
1721 node *n;
1723 int i;
1724 int anchor;
1725 REQ(n, and_test); /* not_test ('and' not_test)* */
1726 anchor = 0;
1727 i = 0;
1728 for (;;) {
1729 com_not_test(c, CHILD(n, i));
1730 if ((i += 2) >= NCH(n))
1731 break;
1732 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1733 com_addbyte(c, POP_TOP);
1734 com_pop(c, 1);
1736 if (anchor)
1737 com_backpatch(c, anchor);
1740 static void
1741 com_test(c, n)
1742 struct compiling *c;
1743 node *n;
1745 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
1746 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
1747 PyObject *v;
1748 int i;
1749 int ndefs = com_argdefs(c, CHILD(n, 0));
1750 v = (PyObject *) icompile(CHILD(n, 0), c);
1751 if (v == NULL) {
1752 c->c_errors++;
1753 i = 255;
1755 else {
1756 i = com_addconst(c, v);
1757 Py_DECREF(v);
1759 com_addoparg(c, LOAD_CONST, i);
1760 com_push(c, 1);
1761 com_addoparg(c, MAKE_FUNCTION, ndefs);
1762 com_pop(c, ndefs);
1764 else {
1765 int anchor = 0;
1766 int i = 0;
1767 for (;;) {
1768 com_and_test(c, CHILD(n, i));
1769 if ((i += 2) >= NCH(n))
1770 break;
1771 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1772 com_addbyte(c, POP_TOP);
1773 com_pop(c, 1);
1775 if (anchor)
1776 com_backpatch(c, anchor);
1780 static void
1781 com_list(c, n, toplevel)
1782 struct compiling *c;
1783 node *n;
1784 int toplevel; /* If nonzero, *always* build a tuple */
1786 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
1787 if (NCH(n) == 1 && !toplevel) {
1788 com_node(c, CHILD(n, 0));
1790 else {
1791 int i;
1792 int len;
1793 len = (NCH(n) + 1) / 2;
1794 for (i = 0; i < NCH(n); i += 2)
1795 com_node(c, CHILD(n, i));
1796 com_addoparg(c, BUILD_TUPLE, len);
1797 com_pop(c, len-1);
1802 /* Begin of assignment compilation */
1804 static void com_assign_name Py_PROTO((struct compiling *, node *, int));
1805 static void com_assign Py_PROTO((struct compiling *, node *, int));
1807 static void
1808 com_assign_attr(c, n, assigning)
1809 struct compiling *c;
1810 node *n;
1811 int assigning;
1813 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
1814 com_pop(c, assigning ? 2 : 1);
1817 static void
1818 com_assign_trailer(c, n, assigning)
1819 struct compiling *c;
1820 node *n;
1821 int assigning;
1823 REQ(n, trailer);
1824 switch (TYPE(CHILD(n, 0))) {
1825 case LPAR: /* '(' [exprlist] ')' */
1826 com_error(c, PyExc_SyntaxError,
1827 "can't assign to function call");
1828 break;
1829 case DOT: /* '.' NAME */
1830 com_assign_attr(c, CHILD(n, 1), assigning);
1831 break;
1832 case LSQB: /* '[' subscriptlist ']' */
1833 com_subscriptlist(c, CHILD(n, 1), assigning);
1834 break;
1835 default:
1836 com_error(c, PyExc_SystemError, "unknown trailer type");
1840 static void
1841 com_assign_tuple(c, n, assigning)
1842 struct compiling *c;
1843 node *n;
1844 int assigning;
1846 int i;
1847 if (TYPE(n) != testlist)
1848 REQ(n, exprlist);
1849 if (assigning) {
1850 i = (NCH(n)+1)/2;
1851 com_addoparg(c, UNPACK_TUPLE, i);
1852 com_push(c, i-1);
1854 for (i = 0; i < NCH(n); i += 2)
1855 com_assign(c, CHILD(n, i), assigning);
1858 static void
1859 com_assign_list(c, n, assigning)
1860 struct compiling *c;
1861 node *n;
1862 int assigning;
1864 int i;
1865 if (assigning) {
1866 i = (NCH(n)+1)/2;
1867 com_addoparg(c, UNPACK_LIST, i);
1868 com_push(c, i-1);
1870 for (i = 0; i < NCH(n); i += 2)
1871 com_assign(c, CHILD(n, i), assigning);
1874 static void
1875 com_assign_name(c, n, assigning)
1876 struct compiling *c;
1877 node *n;
1878 int assigning;
1880 REQ(n, NAME);
1881 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
1882 if (assigning)
1883 com_pop(c, 1);
1886 static void
1887 com_assign(c, n, assigning)
1888 struct compiling *c;
1889 node *n;
1890 int assigning;
1892 /* Loop to avoid trivial recursion */
1893 for (;;) {
1894 switch (TYPE(n)) {
1896 case exprlist:
1897 case testlist:
1898 if (NCH(n) > 1) {
1899 com_assign_tuple(c, n, assigning);
1900 return;
1902 n = CHILD(n, 0);
1903 break;
1905 case test:
1906 case and_test:
1907 case not_test:
1908 case comparison:
1909 case expr:
1910 case xor_expr:
1911 case and_expr:
1912 case shift_expr:
1913 case arith_expr:
1914 case term:
1915 case factor:
1916 if (NCH(n) > 1) {
1917 com_error(c, PyExc_SyntaxError,
1918 "can't assign to operator");
1919 return;
1921 n = CHILD(n, 0);
1922 break;
1924 case power: /* atom trailer* ('**' power)* */
1925 /* ('+'|'-'|'~') factor | atom trailer* */
1926 if (TYPE(CHILD(n, 0)) != atom) {
1927 com_error(c, PyExc_SyntaxError,
1928 "can't assign to operator");
1929 return;
1931 if (NCH(n) > 1) { /* trailer or exponent present */
1932 int i;
1933 com_node(c, CHILD(n, 0));
1934 for (i = 1; i+1 < NCH(n); i++) {
1935 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1936 com_error(c, PyExc_SyntaxError,
1937 "can't assign to operator");
1938 return;
1940 com_apply_trailer(c, CHILD(n, i));
1941 } /* NB i is still alive */
1942 com_assign_trailer(c,
1943 CHILD(n, i), assigning);
1944 return;
1946 n = CHILD(n, 0);
1947 break;
1949 case atom:
1950 switch (TYPE(CHILD(n, 0))) {
1951 case LPAR:
1952 n = CHILD(n, 1);
1953 if (TYPE(n) == RPAR) {
1954 /* XXX Should allow () = () ??? */
1955 com_error(c, PyExc_SyntaxError,
1956 "can't assign to ()");
1957 return;
1959 break;
1960 case LSQB:
1961 n = CHILD(n, 1);
1962 if (TYPE(n) == RSQB) {
1963 com_error(c, PyExc_SyntaxError,
1964 "can't assign to []");
1965 return;
1967 com_assign_list(c, n, assigning);
1968 return;
1969 case NAME:
1970 com_assign_name(c, CHILD(n, 0), assigning);
1971 return;
1972 default:
1973 com_error(c, PyExc_SyntaxError,
1974 "can't assign to literal");
1975 return;
1977 break;
1979 case lambdef:
1980 com_error(c, PyExc_SyntaxError,
1981 "can't assign to lambda");
1982 return;
1984 default:
1985 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
1986 com_error(c, PyExc_SystemError,
1987 "com_assign: bad node");
1988 return;
1994 /* Forward */ static node *get_rawdocstring Py_PROTO((node *));
1996 static void
1997 com_expr_stmt(c, n)
1998 struct compiling *c;
1999 node *n;
2001 REQ(n, expr_stmt); /* testlist ('=' testlist)* */
2002 /* Forget it if we have just a doc string here */
2003 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
2004 return;
2005 com_node(c, CHILD(n, NCH(n)-1));
2006 if (NCH(n) == 1) {
2007 if (c->c_interactive)
2008 com_addbyte(c, PRINT_EXPR);
2009 else
2010 com_addbyte(c, POP_TOP);
2011 com_pop(c, 1);
2013 else {
2014 int i;
2015 for (i = 0; i < NCH(n)-2; i+=2) {
2016 if (i+2 < NCH(n)-2) {
2017 com_addbyte(c, DUP_TOP);
2018 com_push(c, 1);
2020 com_assign(c, CHILD(n, i), OP_ASSIGN);
2025 static void
2026 com_assert_stmt(c, n)
2027 struct compiling *c;
2028 node *n;
2030 int a = 0, b = 0;
2031 int i;
2032 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2033 /* Generate code like for
2035 if __debug__:
2036 if not <test>:
2037 raise AssertionError [, <message>]
2039 where <message> is the second test, if present.
2041 if (Py_OptimizeFlag)
2042 return;
2043 com_addopnamestr(c, LOAD_GLOBAL, "__debug__");
2044 com_push(c, 1);
2045 com_addfwref(c, JUMP_IF_FALSE, &a);
2046 com_addbyte(c, POP_TOP);
2047 com_pop(c, 1);
2048 com_node(c, CHILD(n, 1));
2049 com_addfwref(c, JUMP_IF_TRUE, &b);
2050 com_addbyte(c, POP_TOP);
2051 com_pop(c, 1);
2052 /* Raise that exception! */
2053 com_addopnamestr(c, LOAD_GLOBAL, "AssertionError");
2054 com_push(c, 1);
2055 i = NCH(n)/2; /* Either 2 or 4 */
2056 if (i > 1)
2057 com_node(c, CHILD(n, 3));
2058 com_addoparg(c, RAISE_VARARGS, i);
2059 com_pop(c, i);
2060 /* The interpreter does not fall through */
2061 /* All jumps converge here */
2062 com_backpatch(c, a);
2063 com_backpatch(c, b);
2064 com_addbyte(c, POP_TOP);
2067 static void
2068 com_print_stmt(c, n)
2069 struct compiling *c;
2070 node *n;
2072 int i;
2073 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2074 for (i = 1; i < NCH(n); i += 2) {
2075 com_node(c, CHILD(n, i));
2076 com_addbyte(c, PRINT_ITEM);
2077 com_pop(c, 1);
2079 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
2080 com_addbyte(c, PRINT_NEWLINE);
2081 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2084 static void
2085 com_return_stmt(c, n)
2086 struct compiling *c;
2087 node *n;
2089 REQ(n, return_stmt); /* 'return' [testlist] */
2090 if (!c->c_infunction) {
2091 com_error(c, PyExc_SyntaxError, "'return' outside function");
2093 if (NCH(n) < 2) {
2094 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2095 com_push(c, 1);
2097 else
2098 com_node(c, CHILD(n, 1));
2099 com_addbyte(c, RETURN_VALUE);
2100 com_pop(c, 1);
2103 static void
2104 com_raise_stmt(c, n)
2105 struct compiling *c;
2106 node *n;
2108 int i;
2109 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2110 if (NCH(n) > 1) {
2111 com_node(c, CHILD(n, 1));
2112 if (NCH(n) > 3) {
2113 com_node(c, CHILD(n, 3));
2114 if (NCH(n) > 5)
2115 com_node(c, CHILD(n, 5));
2118 i = NCH(n)/2;
2119 com_addoparg(c, RAISE_VARARGS, i);
2120 com_pop(c, i);
2123 static void
2124 com_import_stmt(c, n)
2125 struct compiling *c;
2126 node *n;
2128 int i;
2129 REQ(n, import_stmt);
2130 /* 'import' dotted_name (',' dotted_name)* |
2131 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2132 if (STR(CHILD(n, 0))[0] == 'f') {
2133 /* 'from' dotted_name 'import' ... */
2134 REQ(CHILD(n, 1), dotted_name);
2135 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
2136 com_push(c, 1);
2137 for (i = 3; i < NCH(n); i += 2)
2138 com_addopname(c, IMPORT_FROM, CHILD(n, i));
2139 com_addbyte(c, POP_TOP);
2140 com_pop(c, 1);
2142 else {
2143 /* 'import' ... */
2144 for (i = 1; i < NCH(n); i += 2) {
2145 REQ(CHILD(n, i), dotted_name);
2146 com_addopname(c, IMPORT_NAME, CHILD(n, i));
2147 com_push(c, 1);
2148 com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
2149 com_pop(c, 1);
2154 static void
2155 com_global_stmt(c, n)
2156 struct compiling *c;
2157 node *n;
2159 int i;
2160 REQ(n, global_stmt);
2161 /* 'global' NAME (',' NAME)* */
2162 for (i = 1; i < NCH(n); i += 2) {
2163 char *s = STR(CHILD(n, i));
2164 #ifdef PRIVATE_NAME_MANGLING
2165 char buffer[256];
2166 if (s != NULL && s[0] == '_' && s[1] == '_' &&
2167 c->c_private != NULL &&
2168 com_mangle(c, s, buffer, (int)sizeof(buffer)))
2169 s = buffer;
2170 #endif
2171 if (PyDict_GetItemString(c->c_locals, s) != NULL) {
2172 com_error(c, PyExc_SyntaxError,
2173 "name is local and global");
2175 else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0)
2176 c->c_errors++;
2180 static int
2181 com_newlocal_o(c, nameval)
2182 struct compiling *c;
2183 PyObject *nameval;
2185 int i;
2186 PyObject *ival;
2187 if (PyList_Size(c->c_varnames) != c->c_nlocals) {
2188 /* This is usually caused by an error on a previous call */
2189 if (c->c_errors == 0) {
2190 com_error(c, PyExc_SystemError,
2191 "mixed up var name/index");
2193 return 0;
2195 ival = PyInt_FromLong(i = c->c_nlocals++);
2196 if (ival == NULL)
2197 c->c_errors++;
2198 else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0)
2199 c->c_errors++;
2200 else if (PyList_Append(c->c_varnames, nameval) != 0)
2201 c->c_errors++;
2202 Py_XDECREF(ival);
2203 return i;
2206 static int
2207 com_addlocal_o(c, nameval)
2208 struct compiling *c;
2209 PyObject *nameval;
2211 PyObject *ival = PyDict_GetItem(c->c_locals, nameval);
2212 if (ival != NULL)
2213 return PyInt_AsLong(ival);
2214 return com_newlocal_o(c, nameval);
2217 static int
2218 com_newlocal(c, name)
2219 struct compiling *c;
2220 char *name;
2222 PyObject *nameval = PyString_InternFromString(name);
2223 int i;
2224 if (nameval == NULL) {
2225 c->c_errors++;
2226 return 0;
2228 i = com_newlocal_o(c, nameval);
2229 Py_DECREF(nameval);
2230 return i;
2233 static void
2234 com_exec_stmt(c, n)
2235 struct compiling *c;
2236 node *n;
2238 REQ(n, exec_stmt);
2239 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2240 com_node(c, CHILD(n, 1));
2241 if (NCH(n) >= 4)
2242 com_node(c, CHILD(n, 3));
2243 else {
2244 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2245 com_push(c, 1);
2247 if (NCH(n) >= 6)
2248 com_node(c, CHILD(n, 5));
2249 else {
2250 com_addbyte(c, DUP_TOP);
2251 com_push(c, 1);
2253 com_addbyte(c, EXEC_STMT);
2254 com_pop(c, 3);
2257 static int
2258 is_constant_false(c, n)
2259 struct compiling *c;
2260 node *n;
2262 PyObject *v;
2263 int i;
2265 /* Label to avoid tail recursion */
2266 next:
2267 switch (TYPE(n)) {
2269 case suite:
2270 if (NCH(n) == 1) {
2271 n = CHILD(n, 0);
2272 goto next;
2274 /* Fall through */
2275 case file_input:
2276 for (i = 0; i < NCH(n); i++) {
2277 node *ch = CHILD(n, i);
2278 if (TYPE(ch) == stmt) {
2279 n = ch;
2280 goto next;
2283 break;
2285 case stmt:
2286 case simple_stmt:
2287 case small_stmt:
2288 n = CHILD(n, 0);
2289 goto next;
2291 case expr_stmt:
2292 case testlist:
2293 case test:
2294 case and_test:
2295 case not_test:
2296 case comparison:
2297 case expr:
2298 case xor_expr:
2299 case and_expr:
2300 case shift_expr:
2301 case arith_expr:
2302 case term:
2303 case factor:
2304 case power:
2305 case atom:
2306 if (NCH(n) == 1) {
2307 n = CHILD(n, 0);
2308 goto next;
2310 break;
2312 case NAME:
2313 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2314 return 1;
2315 break;
2317 case NUMBER:
2318 v = parsenumber(c, STR(n));
2319 if (v == NULL) {
2320 PyErr_Clear();
2321 break;
2323 i = PyObject_IsTrue(v);
2324 Py_DECREF(v);
2325 return i == 0;
2327 case STRING:
2328 v = parsestr(STR(n));
2329 if (v == NULL) {
2330 PyErr_Clear();
2331 break;
2333 i = PyObject_IsTrue(v);
2334 Py_DECREF(v);
2335 return i == 0;
2338 return 0;
2341 static void
2342 com_if_stmt(c, n)
2343 struct compiling *c;
2344 node *n;
2346 int i;
2347 int anchor = 0;
2348 REQ(n, if_stmt);
2349 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2350 for (i = 0; i+3 < NCH(n); i+=4) {
2351 int a = 0;
2352 node *ch = CHILD(n, i+1);
2353 if (is_constant_false(c, ch))
2354 continue;
2355 if (i > 0)
2356 com_addoparg(c, SET_LINENO, ch->n_lineno);
2357 com_node(c, ch);
2358 com_addfwref(c, JUMP_IF_FALSE, &a);
2359 com_addbyte(c, POP_TOP);
2360 com_pop(c, 1);
2361 com_node(c, CHILD(n, i+3));
2362 com_addfwref(c, JUMP_FORWARD, &anchor);
2363 com_backpatch(c, a);
2364 /* We jump here with an extra entry which we now pop */
2365 com_addbyte(c, POP_TOP);
2367 if (i+2 < NCH(n))
2368 com_node(c, CHILD(n, i+2));
2369 if (anchor)
2370 com_backpatch(c, anchor);
2373 static void
2374 com_while_stmt(c, n)
2375 struct compiling *c;
2376 node *n;
2378 int break_anchor = 0;
2379 int anchor = 0;
2380 int save_begin = c->c_begin;
2381 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2382 com_addfwref(c, SETUP_LOOP, &break_anchor);
2383 block_push(c, SETUP_LOOP);
2384 c->c_begin = c->c_nexti;
2385 com_addoparg(c, SET_LINENO, n->n_lineno);
2386 com_node(c, CHILD(n, 1));
2387 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2388 com_addbyte(c, POP_TOP);
2389 com_pop(c, 1);
2390 c->c_loops++;
2391 com_node(c, CHILD(n, 3));
2392 c->c_loops--;
2393 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2394 c->c_begin = save_begin;
2395 com_backpatch(c, anchor);
2396 /* We jump here with one entry more on the stack */
2397 com_addbyte(c, POP_TOP);
2398 com_addbyte(c, POP_BLOCK);
2399 block_pop(c, SETUP_LOOP);
2400 if (NCH(n) > 4)
2401 com_node(c, CHILD(n, 6));
2402 com_backpatch(c, break_anchor);
2405 static void
2406 com_for_stmt(c, n)
2407 struct compiling *c;
2408 node *n;
2410 PyObject *v;
2411 int break_anchor = 0;
2412 int anchor = 0;
2413 int save_begin = c->c_begin;
2414 REQ(n, for_stmt);
2415 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2416 com_addfwref(c, SETUP_LOOP, &break_anchor);
2417 block_push(c, SETUP_LOOP);
2418 com_node(c, CHILD(n, 3));
2419 v = PyInt_FromLong(0L);
2420 if (v == NULL)
2421 c->c_errors++;
2422 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
2423 com_push(c, 1);
2424 Py_XDECREF(v);
2425 c->c_begin = c->c_nexti;
2426 com_addoparg(c, SET_LINENO, n->n_lineno);
2427 com_addfwref(c, FOR_LOOP, &anchor);
2428 com_push(c, 1);
2429 com_assign(c, CHILD(n, 1), OP_ASSIGN);
2430 c->c_loops++;
2431 com_node(c, CHILD(n, 5));
2432 c->c_loops--;
2433 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2434 c->c_begin = save_begin;
2435 com_backpatch(c, anchor);
2436 com_pop(c, 2); /* FOR_LOOP has popped these */
2437 com_addbyte(c, POP_BLOCK);
2438 block_pop(c, SETUP_LOOP);
2439 if (NCH(n) > 8)
2440 com_node(c, CHILD(n, 8));
2441 com_backpatch(c, break_anchor);
2444 /* Code generated for "try: S finally: Sf" is as follows:
2446 SETUP_FINALLY L
2447 <code for S>
2448 POP_BLOCK
2449 LOAD_CONST <nil>
2450 L: <code for Sf>
2451 END_FINALLY
2453 The special instructions use the block stack. Each block
2454 stack entry contains the instruction that created it (here
2455 SETUP_FINALLY), the level of the value stack at the time the
2456 block stack entry was created, and a label (here L).
2458 SETUP_FINALLY:
2459 Pushes the current value stack level and the label
2460 onto the block stack.
2461 POP_BLOCK:
2462 Pops en entry from the block stack, and pops the value
2463 stack until its level is the same as indicated on the
2464 block stack. (The label is ignored.)
2465 END_FINALLY:
2466 Pops a variable number of entries from the *value* stack
2467 and re-raises the exception they specify. The number of
2468 entries popped depends on the (pseudo) exception type.
2470 The block stack is unwound when an exception is raised:
2471 when a SETUP_FINALLY entry is found, the exception is pushed
2472 onto the value stack (and the exception condition is cleared),
2473 and the interpreter jumps to the label gotten from the block
2474 stack.
2476 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2477 (The contents of the value stack is shown in [], with the top
2478 at the right; 'tb' is trace-back info, 'val' the exception's
2479 associated value, and 'exc' the exception.)
2481 Value stack Label Instruction Argument
2482 [] SETUP_EXCEPT L1
2483 [] <code for S>
2484 [] POP_BLOCK
2485 [] JUMP_FORWARD L0
2487 [tb, val, exc] L1: DUP )
2488 [tb, val, exc, exc] <evaluate E1> )
2489 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2490 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2491 [tb, val, exc, 1] POP )
2492 [tb, val, exc] POP
2493 [tb, val] <assign to V1> (or POP if no V1)
2494 [tb] POP
2495 [] <code for S1>
2496 JUMP_FORWARD L0
2498 [tb, val, exc, 0] L2: POP
2499 [tb, val, exc] DUP
2500 .............................etc.......................
2502 [tb, val, exc, 0] Ln+1: POP
2503 [tb, val, exc] END_FINALLY # re-raise exception
2505 [] L0: <next statement>
2507 Of course, parts are not generated if Vi or Ei is not present.
2510 static void
2511 com_try_except(c, n)
2512 struct compiling *c;
2513 node *n;
2515 int except_anchor = 0;
2516 int end_anchor = 0;
2517 int else_anchor = 0;
2518 int i;
2519 node *ch;
2521 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2522 block_push(c, SETUP_EXCEPT);
2523 com_node(c, CHILD(n, 2));
2524 com_addbyte(c, POP_BLOCK);
2525 block_pop(c, SETUP_EXCEPT);
2526 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2527 com_backpatch(c, except_anchor);
2528 for (i = 3;
2529 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2530 i += 3) {
2531 /* except_clause: 'except' [expr [',' var]] */
2532 if (except_anchor == 0) {
2533 com_error(c, PyExc_SyntaxError,
2534 "default 'except:' must be last");
2535 break;
2537 except_anchor = 0;
2538 com_push(c, 3); /* tb, val, exc pushed by exception */
2539 com_addoparg(c, SET_LINENO, ch->n_lineno);
2540 if (NCH(ch) > 1) {
2541 com_addbyte(c, DUP_TOP);
2542 com_push(c, 1);
2543 com_node(c, CHILD(ch, 1));
2544 com_addoparg(c, COMPARE_OP, EXC_MATCH);
2545 com_pop(c, 1);
2546 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
2547 com_addbyte(c, POP_TOP);
2548 com_pop(c, 1);
2550 com_addbyte(c, POP_TOP);
2551 com_pop(c, 1);
2552 if (NCH(ch) > 3)
2553 com_assign(c, CHILD(ch, 3), OP_ASSIGN);
2554 else {
2555 com_addbyte(c, POP_TOP);
2556 com_pop(c, 1);
2558 com_addbyte(c, POP_TOP);
2559 com_pop(c, 1);
2560 com_node(c, CHILD(n, i+2));
2561 com_addfwref(c, JUMP_FORWARD, &end_anchor);
2562 if (except_anchor) {
2563 com_backpatch(c, except_anchor);
2564 /* We come in with [tb, val, exc, 0] on the
2565 stack; one pop and it's the same as
2566 expected at the start of the loop */
2567 com_addbyte(c, POP_TOP);
2570 /* We actually come in here with [tb, val, exc] but the
2571 END_FINALLY will zap those and jump around.
2572 The c_stacklevel does not reflect them so we need not pop
2573 anything. */
2574 com_addbyte(c, END_FINALLY);
2575 com_backpatch(c, else_anchor);
2576 if (i < NCH(n))
2577 com_node(c, CHILD(n, i+2));
2578 com_backpatch(c, end_anchor);
2581 static void
2582 com_try_finally(c, n)
2583 struct compiling *c;
2584 node *n;
2586 int finally_anchor = 0;
2587 node *ch;
2589 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
2590 block_push(c, SETUP_FINALLY);
2591 com_node(c, CHILD(n, 2));
2592 com_addbyte(c, POP_BLOCK);
2593 block_pop(c, SETUP_FINALLY);
2594 block_push(c, END_FINALLY);
2595 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2596 /* While the generated code pushes only one item,
2597 the try-finally handling can enter here with
2598 up to three items. OK, here are the details:
2599 3 for an exception, 2 for RETURN, 1 for BREAK. */
2600 com_push(c, 3);
2601 com_backpatch(c, finally_anchor);
2602 ch = CHILD(n, NCH(n)-1);
2603 com_addoparg(c, SET_LINENO, ch->n_lineno);
2604 com_node(c, ch);
2605 com_addbyte(c, END_FINALLY);
2606 block_pop(c, END_FINALLY);
2607 com_pop(c, 3); /* Matches the com_push above */
2610 static void
2611 com_try_stmt(c, n)
2612 struct compiling *c;
2613 node *n;
2615 REQ(n, try_stmt);
2616 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2617 | 'try' ':' suite 'finally' ':' suite */
2618 if (TYPE(CHILD(n, 3)) != except_clause)
2619 com_try_finally(c, n);
2620 else
2621 com_try_except(c, n);
2624 static node *
2625 get_rawdocstring(n)
2626 node *n;
2628 int i;
2630 /* Label to avoid tail recursion */
2631 next:
2632 switch (TYPE(n)) {
2634 case suite:
2635 if (NCH(n) == 1) {
2636 n = CHILD(n, 0);
2637 goto next;
2639 /* Fall through */
2640 case file_input:
2641 for (i = 0; i < NCH(n); i++) {
2642 node *ch = CHILD(n, i);
2643 if (TYPE(ch) == stmt) {
2644 n = ch;
2645 goto next;
2648 break;
2650 case stmt:
2651 case simple_stmt:
2652 case small_stmt:
2653 n = CHILD(n, 0);
2654 goto next;
2656 case expr_stmt:
2657 case testlist:
2658 case test:
2659 case and_test:
2660 case not_test:
2661 case comparison:
2662 case expr:
2663 case xor_expr:
2664 case and_expr:
2665 case shift_expr:
2666 case arith_expr:
2667 case term:
2668 case factor:
2669 case power:
2670 if (NCH(n) == 1) {
2671 n = CHILD(n, 0);
2672 goto next;
2674 break;
2676 case atom:
2677 if (TYPE(CHILD(n, 0)) == STRING)
2678 return n;
2679 break;
2682 return NULL;
2685 static PyObject *
2686 get_docstring(n)
2687 node *n;
2689 /* Don't generate doc-strings if run with -OO */
2690 if (Py_OptimizeFlag > 1)
2691 return NULL;
2692 n = get_rawdocstring(n);
2693 if (n == NULL)
2694 return NULL;
2695 return parsestrplus(n);
2698 static void
2699 com_suite(c, n)
2700 struct compiling *c;
2701 node *n;
2703 REQ(n, suite);
2704 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2705 if (NCH(n) == 1) {
2706 com_node(c, CHILD(n, 0));
2708 else {
2709 int i;
2710 for (i = 0; i < NCH(n); i++) {
2711 node *ch = CHILD(n, i);
2712 if (TYPE(ch) == stmt)
2713 com_node(c, ch);
2718 /* ARGSUSED */
2719 static void
2720 com_continue_stmt(c, n)
2721 struct compiling *c;
2722 node *n; /* Not used, but passed for consistency */
2724 int i = c->c_nblocks;
2725 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
2726 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2728 else {
2729 com_error(c, PyExc_SyntaxError,
2730 "'continue' not properly in loop");
2732 /* XXX Could allow it inside a 'finally' clause
2733 XXX if we could pop the exception still on the stack */
2736 static int
2737 com_argdefs(c, n)
2738 struct compiling *c;
2739 node *n;
2741 int i, nch, nargs, ndefs;
2742 if (TYPE(n) == lambdef) {
2743 /* lambdef: 'lambda' [varargslist] ':' test */
2744 n = CHILD(n, 1);
2746 else {
2747 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
2748 n = CHILD(n, 2);
2749 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
2750 n = CHILD(n, 1);
2752 if (TYPE(n) != varargslist)
2753 return 0;
2754 /* varargslist:
2755 (fpdef ['=' test] ',')* '*' ....... |
2756 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2757 nch = NCH(n);
2758 nargs = 0;
2759 ndefs = 0;
2760 for (i = 0; i < nch; i++) {
2761 int t;
2762 if (TYPE(CHILD(n, i)) == STAR ||
2763 TYPE(CHILD(n, i)) == DOUBLESTAR)
2764 break;
2765 nargs++;
2766 i++;
2767 if (i >= nch)
2768 t = RPAR; /* Anything except EQUAL or COMMA */
2769 else
2770 t = TYPE(CHILD(n, i));
2771 if (t == EQUAL) {
2772 i++;
2773 ndefs++;
2774 com_node(c, CHILD(n, i));
2775 i++;
2776 if (i >= nch)
2777 break;
2778 t = TYPE(CHILD(n, i));
2780 else {
2781 /* Treat "(a=1, b)" as an error */
2782 if (ndefs)
2783 com_error(c, PyExc_SyntaxError,
2784 "non-default argument follows default argument");
2786 if (t != COMMA)
2787 break;
2789 return ndefs;
2792 static void
2793 com_funcdef(c, n)
2794 struct compiling *c;
2795 node *n;
2797 PyObject *v;
2798 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
2799 v = (PyObject *)icompile(n, c);
2800 if (v == NULL)
2801 c->c_errors++;
2802 else {
2803 int i = com_addconst(c, v);
2804 int ndefs = com_argdefs(c, n);
2805 com_addoparg(c, LOAD_CONST, i);
2806 com_push(c, 1);
2807 com_addoparg(c, MAKE_FUNCTION, ndefs);
2808 com_pop(c, ndefs);
2809 com_addopname(c, STORE_NAME, CHILD(n, 1));
2810 com_pop(c, 1);
2811 Py_DECREF(v);
2815 static void
2816 com_bases(c, n)
2817 struct compiling *c;
2818 node *n;
2820 int i;
2821 REQ(n, testlist);
2822 /* testlist: test (',' test)* [','] */
2823 for (i = 0; i < NCH(n); i += 2)
2824 com_node(c, CHILD(n, i));
2825 i = (NCH(n)+1) / 2;
2826 com_addoparg(c, BUILD_TUPLE, i);
2827 com_pop(c, i-1);
2830 static void
2831 com_classdef(c, n)
2832 struct compiling *c;
2833 node *n;
2835 int i;
2836 PyObject *v;
2837 REQ(n, classdef);
2838 /* classdef: class NAME ['(' testlist ')'] ':' suite */
2839 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
2840 c->c_errors++;
2841 return;
2843 /* Push the class name on the stack */
2844 i = com_addconst(c, v);
2845 com_addoparg(c, LOAD_CONST, i);
2846 com_push(c, 1);
2847 Py_DECREF(v);
2848 /* Push the tuple of base classes on the stack */
2849 if (TYPE(CHILD(n, 2)) != LPAR) {
2850 com_addoparg(c, BUILD_TUPLE, 0);
2851 com_push(c, 1);
2853 else
2854 com_bases(c, CHILD(n, 3));
2855 v = (PyObject *)icompile(n, c);
2856 if (v == NULL)
2857 c->c_errors++;
2858 else {
2859 i = com_addconst(c, v);
2860 com_addoparg(c, LOAD_CONST, i);
2861 com_push(c, 1);
2862 com_addoparg(c, MAKE_FUNCTION, 0);
2863 com_addoparg(c, CALL_FUNCTION, 0);
2864 com_addbyte(c, BUILD_CLASS);
2865 com_pop(c, 2);
2866 com_addopname(c, STORE_NAME, CHILD(n, 1));
2867 Py_DECREF(v);
2871 static void
2872 com_node(c, n)
2873 struct compiling *c;
2874 node *n;
2876 switch (TYPE(n)) {
2878 /* Definition nodes */
2880 case funcdef:
2881 com_funcdef(c, n);
2882 break;
2883 case classdef:
2884 com_classdef(c, n);
2885 break;
2887 /* Trivial parse tree nodes */
2889 case stmt:
2890 case small_stmt:
2891 case flow_stmt:
2892 com_node(c, CHILD(n, 0));
2893 break;
2895 case simple_stmt:
2896 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2897 com_addoparg(c, SET_LINENO, n->n_lineno);
2899 int i;
2900 for (i = 0; i < NCH(n)-1; i += 2)
2901 com_node(c, CHILD(n, i));
2903 break;
2905 case compound_stmt:
2906 com_addoparg(c, SET_LINENO, n->n_lineno);
2907 com_node(c, CHILD(n, 0));
2908 break;
2910 /* Statement nodes */
2912 case expr_stmt:
2913 com_expr_stmt(c, n);
2914 break;
2915 case print_stmt:
2916 com_print_stmt(c, n);
2917 break;
2918 case del_stmt: /* 'del' exprlist */
2919 com_assign(c, CHILD(n, 1), OP_DELETE);
2920 break;
2921 case pass_stmt:
2922 break;
2923 case break_stmt:
2924 if (c->c_loops == 0) {
2925 com_error(c, PyExc_SyntaxError,
2926 "'break' outside loop");
2928 com_addbyte(c, BREAK_LOOP);
2929 break;
2930 case continue_stmt:
2931 com_continue_stmt(c, n);
2932 break;
2933 case return_stmt:
2934 com_return_stmt(c, n);
2935 break;
2936 case raise_stmt:
2937 com_raise_stmt(c, n);
2938 break;
2939 case import_stmt:
2940 com_import_stmt(c, n);
2941 break;
2942 case global_stmt:
2943 com_global_stmt(c, n);
2944 break;
2945 case exec_stmt:
2946 com_exec_stmt(c, n);
2947 break;
2948 case assert_stmt:
2949 com_assert_stmt(c, n);
2950 break;
2951 case if_stmt:
2952 com_if_stmt(c, n);
2953 break;
2954 case while_stmt:
2955 com_while_stmt(c, n);
2956 break;
2957 case for_stmt:
2958 com_for_stmt(c, n);
2959 break;
2960 case try_stmt:
2961 com_try_stmt(c, n);
2962 break;
2963 case suite:
2964 com_suite(c, n);
2965 break;
2967 /* Expression nodes */
2969 case testlist:
2970 com_list(c, n, 0);
2971 break;
2972 case test:
2973 com_test(c, n);
2974 break;
2975 case and_test:
2976 com_and_test(c, n);
2977 break;
2978 case not_test:
2979 com_not_test(c, n);
2980 break;
2981 case comparison:
2982 com_comparison(c, n);
2983 break;
2984 case exprlist:
2985 com_list(c, n, 0);
2986 break;
2987 case expr:
2988 com_expr(c, n);
2989 break;
2990 case xor_expr:
2991 com_xor_expr(c, n);
2992 break;
2993 case and_expr:
2994 com_and_expr(c, n);
2995 break;
2996 case shift_expr:
2997 com_shift_expr(c, n);
2998 break;
2999 case arith_expr:
3000 com_arith_expr(c, n);
3001 break;
3002 case term:
3003 com_term(c, n);
3004 break;
3005 case factor:
3006 com_factor(c, n);
3007 break;
3008 case power:
3009 com_power(c, n);
3010 break;
3011 case atom:
3012 com_atom(c, n);
3013 break;
3015 default:
3016 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
3017 com_error(c, PyExc_SystemError,
3018 "com_node: unexpected node type");
3022 static void com_fplist Py_PROTO((struct compiling *, node *));
3024 static void
3025 com_fpdef(c, n)
3026 struct compiling *c;
3027 node *n;
3029 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3030 if (TYPE(CHILD(n, 0)) == LPAR)
3031 com_fplist(c, CHILD(n, 1));
3032 else {
3033 com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
3034 com_pop(c, 1);
3038 static void
3039 com_fplist(c, n)
3040 struct compiling *c;
3041 node *n;
3043 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
3044 if (NCH(n) == 1) {
3045 com_fpdef(c, CHILD(n, 0));
3047 else {
3048 int i = (NCH(n)+1)/2;
3049 com_addoparg(c, UNPACK_TUPLE, i);
3050 com_push(c, i-1);
3051 for (i = 0; i < NCH(n); i += 2)
3052 com_fpdef(c, CHILD(n, i));
3056 static void
3057 com_arglist(c, n)
3058 struct compiling *c;
3059 node *n;
3061 int nch, i;
3062 int complex = 0;
3063 char nbuf[10];
3064 REQ(n, varargslist);
3065 /* varargslist:
3066 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3067 nch = NCH(n);
3068 /* Enter all arguments in table of locals */
3069 for (i = 0; i < nch; i++) {
3070 node *ch = CHILD(n, i);
3071 node *fp;
3072 char *name;
3073 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3074 break;
3075 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3076 fp = CHILD(ch, 0);
3077 if (TYPE(fp) == NAME)
3078 name = STR(fp);
3079 else {
3080 name = nbuf;
3081 sprintf(nbuf, ".%d", i);
3082 complex = 1;
3084 com_newlocal(c, name);
3085 c->c_argcount++;
3086 if (++i >= nch)
3087 break;
3088 ch = CHILD(n, i);
3089 if (TYPE(ch) == EQUAL)
3090 i += 2;
3091 else
3092 REQ(ch, COMMA);
3094 /* Handle *arguments */
3095 if (i < nch) {
3096 node *ch;
3097 ch = CHILD(n, i);
3098 if (TYPE(ch) != DOUBLESTAR) {
3099 REQ(ch, STAR);
3100 ch = CHILD(n, i+1);
3101 if (TYPE(ch) == NAME) {
3102 c->c_flags |= CO_VARARGS;
3103 i += 3;
3104 com_newlocal(c, STR(ch));
3108 /* Handle **keywords */
3109 if (i < nch) {
3110 node *ch;
3111 ch = CHILD(n, i);
3112 if (TYPE(ch) != DOUBLESTAR) {
3113 REQ(ch, STAR);
3114 ch = CHILD(n, i+1);
3115 REQ(ch, STAR);
3116 ch = CHILD(n, i+2);
3118 else
3119 ch = CHILD(n, i+1);
3120 REQ(ch, NAME);
3121 c->c_flags |= CO_VARKEYWORDS;
3122 com_newlocal(c, STR(ch));
3124 if (complex) {
3125 /* Generate code for complex arguments only after
3126 having counted the simple arguments */
3127 int ilocal = 0;
3128 for (i = 0; i < nch; i++) {
3129 node *ch = CHILD(n, i);
3130 node *fp;
3131 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3132 break;
3133 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3134 fp = CHILD(ch, 0);
3135 if (TYPE(fp) != NAME) {
3136 com_addoparg(c, LOAD_FAST, ilocal);
3137 com_push(c, 1);
3138 com_fpdef(c, ch);
3140 ilocal++;
3141 if (++i >= nch)
3142 break;
3143 ch = CHILD(n, i);
3144 if (TYPE(ch) == EQUAL)
3145 i += 2;
3146 else
3147 REQ(ch, COMMA);
3152 static void
3153 com_file_input(c, n)
3154 struct compiling *c;
3155 node *n;
3157 int i;
3158 PyObject *doc;
3159 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
3160 doc = get_docstring(n);
3161 if (doc != NULL) {
3162 int i = com_addconst(c, doc);
3163 Py_DECREF(doc);
3164 com_addoparg(c, LOAD_CONST, i);
3165 com_push(c, 1);
3166 com_addopnamestr(c, STORE_NAME, "__doc__");
3167 com_pop(c, 1);
3169 for (i = 0; i < NCH(n); i++) {
3170 node *ch = CHILD(n, i);
3171 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3172 com_node(c, ch);
3176 /* Top-level compile-node interface */
3178 static void
3179 compile_funcdef(c, n)
3180 struct compiling *c;
3181 node *n;
3183 PyObject *doc;
3184 node *ch;
3185 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3186 c->c_name = STR(CHILD(n, 1));
3187 doc = get_docstring(CHILD(n, 4));
3188 if (doc != NULL) {
3189 (void) com_addconst(c, doc);
3190 Py_DECREF(doc);
3192 else
3193 (void) com_addconst(c, Py_None); /* No docstring */
3194 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3195 ch = CHILD(ch, 1); /* ')' | varargslist */
3196 if (TYPE(ch) == varargslist)
3197 com_arglist(c, ch);
3198 c->c_infunction = 1;
3199 com_node(c, CHILD(n, 4));
3200 c->c_infunction = 0;
3201 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3202 com_push(c, 1);
3203 com_addbyte(c, RETURN_VALUE);
3204 com_pop(c, 1);
3207 static void
3208 compile_lambdef(c, n)
3209 struct compiling *c;
3210 node *n;
3212 node *ch;
3213 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
3214 c->c_name = "<lambda>";
3216 ch = CHILD(n, 1);
3217 (void) com_addconst(c, Py_None); /* No docstring */
3218 if (TYPE(ch) == varargslist) {
3219 com_arglist(c, ch);
3220 ch = CHILD(n, 3);
3222 else
3223 ch = CHILD(n, 2);
3224 com_node(c, ch);
3225 com_addbyte(c, RETURN_VALUE);
3226 com_pop(c, 1);
3229 static void
3230 compile_classdef(c, n)
3231 struct compiling *c;
3232 node *n;
3234 node *ch;
3235 PyObject *doc;
3236 REQ(n, classdef);
3237 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3238 c->c_name = STR(CHILD(n, 1));
3239 #ifdef PRIVATE_NAME_MANGLING
3240 c->c_private = c->c_name;
3241 #endif
3242 ch = CHILD(n, NCH(n)-1); /* The suite */
3243 doc = get_docstring(ch);
3244 if (doc != NULL) {
3245 int i = com_addconst(c, doc);
3246 Py_DECREF(doc);
3247 com_addoparg(c, LOAD_CONST, i);
3248 com_push(c, 1);
3249 com_addopnamestr(c, STORE_NAME, "__doc__");
3250 com_pop(c, 1);
3252 else
3253 (void) com_addconst(c, Py_None);
3254 com_node(c, ch);
3255 com_addbyte(c, LOAD_LOCALS);
3256 com_push(c, 1);
3257 com_addbyte(c, RETURN_VALUE);
3258 com_pop(c, 1);
3261 static void
3262 compile_node(c, n)
3263 struct compiling *c;
3264 node *n;
3266 com_addoparg(c, SET_LINENO, n->n_lineno);
3268 switch (TYPE(n)) {
3270 case single_input: /* One interactive command */
3271 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
3272 c->c_interactive++;
3273 n = CHILD(n, 0);
3274 if (TYPE(n) != NEWLINE)
3275 com_node(c, n);
3276 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3277 com_push(c, 1);
3278 com_addbyte(c, RETURN_VALUE);
3279 com_pop(c, 1);
3280 c->c_interactive--;
3281 break;
3283 case file_input: /* A whole file, or built-in function exec() */
3284 com_file_input(c, n);
3285 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3286 com_push(c, 1);
3287 com_addbyte(c, RETURN_VALUE);
3288 com_pop(c, 1);
3289 break;
3291 case eval_input: /* Built-in function input() */
3292 com_node(c, CHILD(n, 0));
3293 com_addbyte(c, RETURN_VALUE);
3294 com_pop(c, 1);
3295 break;
3297 case lambdef: /* anonymous function definition */
3298 compile_lambdef(c, n);
3299 break;
3301 case funcdef: /* A function definition */
3302 compile_funcdef(c, n);
3303 break;
3305 case classdef: /* A class definition */
3306 compile_classdef(c, n);
3307 break;
3309 default:
3310 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
3311 com_error(c, PyExc_SystemError,
3312 "compile_node: unexpected node type");
3316 /* Optimization for local variables in functions (and *only* functions).
3318 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
3319 instructions that refer to local variables with LOAD_FAST etc.
3320 The latter instructions are much faster because they don't need to
3321 look up the variable name in a dictionary.
3323 To find all local variables, we check all STORE_NAME, IMPORT_FROM
3324 and DELETE_NAME instructions. This yields all local variables,
3325 function definitions, class definitions and import statements.
3326 Argument names have already been entered into the list by the
3327 special processing for the argument list.
3329 All remaining LOAD_NAME instructions must refer to non-local (global
3330 or builtin) variables, so are replaced by LOAD_GLOBAL.
3332 There are two problems: 'from foo import *' and 'exec' may introduce
3333 local variables that we can't know while compiling. If this is the
3334 case, we can still optimize bona fide locals (since those
3335 statements will be surrounded by fast_2_locals() and
3336 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
3338 NB: this modifies the string object c->c_code! */
3340 static void
3341 optimize(c)
3342 struct compiling *c;
3344 unsigned char *next_instr, *cur_instr;
3345 int opcode;
3346 int oparg = 0;
3347 PyObject *name;
3348 PyObject *error_type, *error_value, *error_traceback;
3350 #define NEXTOP() (*next_instr++)
3351 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
3352 #define GETITEM(v, i) (PyList_GetItem((v), (i)))
3353 #define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
3355 PyErr_Fetch(&error_type, &error_value, &error_traceback);
3357 c->c_flags |= CO_OPTIMIZED;
3359 next_instr = (unsigned char *) PyString_AsString(c->c_code);
3360 for (;;) {
3361 opcode = NEXTOP();
3362 if (opcode == STOP_CODE)
3363 break;
3364 if (HAS_ARG(opcode))
3365 oparg = NEXTARG();
3366 switch (opcode) {
3367 case STORE_NAME:
3368 case DELETE_NAME:
3369 case IMPORT_FROM:
3370 com_addlocal_o(c, GETNAMEOBJ(oparg));
3371 break;
3372 case EXEC_STMT:
3373 c->c_flags &= ~CO_OPTIMIZED;
3374 break;
3378 if (PyDict_GetItemString(c->c_locals, "*") != NULL)
3379 c->c_flags &= ~CO_OPTIMIZED;
3381 next_instr = (unsigned char *) PyString_AsString(c->c_code);
3382 for (;;) {
3383 cur_instr = next_instr;
3384 opcode = NEXTOP();
3385 if (opcode == STOP_CODE)
3386 break;
3387 if (HAS_ARG(opcode))
3388 oparg = NEXTARG();
3389 if (opcode == LOAD_NAME ||
3390 opcode == STORE_NAME ||
3391 opcode == DELETE_NAME) {
3392 PyObject *v;
3393 int i;
3394 name = GETNAMEOBJ(oparg);
3395 v = PyDict_GetItem(c->c_locals, name);
3396 if (v == NULL) {
3397 if (opcode == LOAD_NAME &&
3398 (c->c_flags&CO_OPTIMIZED))
3399 cur_instr[0] = LOAD_GLOBAL;
3400 continue;
3402 i = PyInt_AsLong(v);
3403 switch (opcode) {
3404 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
3405 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
3406 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
3408 cur_instr[1] = i & 0xff;
3409 cur_instr[2] = (i>>8) & 0xff;
3413 if (c->c_errors == 0)
3414 PyErr_Restore(error_type, error_value, error_traceback);
3417 PyCodeObject *
3418 PyNode_Compile(n, filename)
3419 node *n;
3420 char *filename;
3422 return jcompile(n, filename, NULL);
3425 static PyCodeObject *
3426 icompile(n, base)
3427 node *n;
3428 struct compiling *base;
3430 return jcompile(n, base->c_filename, base);
3433 static PyCodeObject *
3434 jcompile(n, filename, base)
3435 node *n;
3436 char *filename;
3437 struct compiling *base;
3439 struct compiling sc;
3440 PyCodeObject *co;
3441 if (!com_init(&sc, filename))
3442 return NULL;
3443 #ifdef PRIVATE_NAME_MANGLING
3444 if (base)
3445 sc.c_private = base->c_private;
3446 else
3447 sc.c_private = NULL;
3448 #endif
3449 compile_node(&sc, n);
3450 com_done(&sc);
3451 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
3452 optimize(&sc);
3453 sc.c_flags |= CO_NEWLOCALS;
3455 else if (TYPE(n) == classdef)
3456 sc.c_flags |= CO_NEWLOCALS;
3457 co = NULL;
3458 if (sc.c_errors == 0) {
3459 PyObject *consts, *names, *varnames, *filename, *name;
3460 consts = PyList_AsTuple(sc.c_consts);
3461 names = PyList_AsTuple(sc.c_names);
3462 varnames = PyList_AsTuple(sc.c_varnames);
3463 filename = PyString_InternFromString(sc.c_filename);
3464 name = PyString_InternFromString(sc.c_name);
3465 if (!PyErr_Occurred())
3466 co = PyCode_New(sc.c_argcount,
3467 sc.c_nlocals,
3468 sc.c_maxstacklevel,
3469 sc.c_flags,
3470 sc.c_code,
3471 consts,
3472 names,
3473 varnames,
3474 filename,
3475 name,
3476 sc.c_firstlineno,
3477 sc.c_lnotab);
3478 Py_XDECREF(consts);
3479 Py_XDECREF(names);
3480 Py_XDECREF(varnames);
3481 Py_XDECREF(filename);
3482 Py_XDECREF(name);
3484 com_free(&sc);
3485 return co;
3489 PyCode_Addr2Line(co, addrq)
3490 PyCodeObject *co;
3491 int addrq;
3493 int size = PyString_Size(co->co_lnotab) / 2;
3494 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
3495 int line = co->co_firstlineno;
3496 int addr = 0;
3497 while (--size >= 0) {
3498 addr += *p++;
3499 if (addr > addrq)
3500 break;
3501 line += *p++;
3503 return line;