Clarify portability and main program.
[python/dscho.git] / Python / compile.c
blob19f18e692dba41352e3744054c70c8e9d0a7c842
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_lnotab);
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 long PyOS_strtol Py_PROTO((const char *, char **, int));
820 extern unsigned long PyOS_strtoul Py_PROTO((const char *,
821 char **, int));
822 extern double atof Py_PROTO((const char *));
823 char *end;
824 long x;
825 double dx;
826 #ifndef WITHOUT_COMPLEX
827 Py_complex c;
828 int imflag;
829 #endif
831 errno = 0;
832 end = s + strlen(s) - 1;
833 #ifndef WITHOUT_COMPLEX
834 imflag = *end == 'j' || *end == 'J';
835 #endif
836 if (*end == 'l' || *end == 'L')
837 return PyLong_FromString(s, (char **)0, 0);
838 if (s[0] == '0')
839 x = (long) PyOS_strtoul(s, &end, 0);
840 else
841 x = PyOS_strtol(s, &end, 0);
842 if (*end == '\0') {
843 if (errno != 0) {
844 com_error(co, PyExc_OverflowError,
845 "integer literal too large");
846 return NULL;
848 return PyInt_FromLong(x);
850 /* XXX Huge floats may silently fail */
851 #ifndef WITHOUT_COMPLEX
852 if (imflag) {
853 c.real = 0.;
854 PyFPE_START_PROTECT("atof", return 0)
855 c.imag = atof(s);
856 PyFPE_END_PROTECT(c)
857 return PyComplex_FromCComplex(c);
859 else
860 #endif
862 PyFPE_START_PROTECT("atof", return 0)
863 dx = atof(s);
864 PyFPE_END_PROTECT(dx)
865 return PyFloat_FromDouble(dx);
869 static PyObject *
870 parsestr(s)
871 char *s;
873 PyObject *v;
874 int len;
875 char *buf;
876 char *p;
877 char *end;
878 int c;
879 int first = *s;
880 int quote = first;
881 if (isalpha(quote) || quote == '_')
882 quote = *++s;
883 if (quote != '\'' && quote != '\"') {
884 PyErr_BadInternalCall();
885 return NULL;
887 s++;
888 len = strlen(s);
889 if (s[--len] != quote) {
890 PyErr_BadInternalCall();
891 return NULL;
893 if (len >= 4 && s[0] == quote && s[1] == quote) {
894 s += 2;
895 len -= 2;
896 if (s[--len] != quote || s[--len] != quote) {
897 PyErr_BadInternalCall();
898 return NULL;
901 if (first != quote || strchr(s, '\\') == NULL)
902 return PyString_FromStringAndSize(s, len);
903 v = PyString_FromStringAndSize((char *)NULL, len);
904 p = buf = PyString_AsString(v);
905 end = s + len;
906 while (s < end) {
907 if (*s != '\\') {
908 *p++ = *s++;
909 continue;
911 s++;
912 switch (*s++) {
913 /* XXX This assumes ASCII! */
914 case '\n': break;
915 case '\\': *p++ = '\\'; break;
916 case '\'': *p++ = '\''; break;
917 case '\"': *p++ = '\"'; break;
918 case 'b': *p++ = '\b'; break;
919 case 'f': *p++ = '\014'; break; /* FF */
920 case 't': *p++ = '\t'; break;
921 case 'n': *p++ = '\n'; break;
922 case 'r': *p++ = '\r'; break;
923 case 'v': *p++ = '\013'; break; /* VT */
924 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
925 case '0': case '1': case '2': case '3':
926 case '4': case '5': case '6': case '7':
927 c = s[-1] - '0';
928 if ('0' <= *s && *s <= '7') {
929 c = (c<<3) + *s++ - '0';
930 if ('0' <= *s && *s <= '7')
931 c = (c<<3) + *s++ - '0';
933 *p++ = c;
934 break;
935 case 'x':
936 if (isxdigit(Py_CHARMASK(*s))) {
937 unsigned int x = 0;
938 do {
939 c = Py_CHARMASK(*s);
940 s++;
941 x = (x<<4) & ~0xF;
942 if (isdigit(c))
943 x += c - '0';
944 else if (islower(c))
945 x += 10 + c - 'a';
946 else
947 x += 10 + c - 'A';
948 } while (isxdigit(Py_CHARMASK(*s)));
949 *p++ = x;
950 break;
952 /* FALLTHROUGH */
953 default: *p++ = '\\'; *p++ = s[-1]; break;
956 _PyString_Resize(&v, (int)(p - buf));
957 return v;
960 static PyObject *
961 parsestrplus(n)
962 node *n;
964 PyObject *v;
965 int i;
966 REQ(CHILD(n, 0), STRING);
967 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
968 /* String literal concatenation */
969 for (i = 1; i < NCH(n) && v != NULL; i++) {
970 PyString_ConcatAndDel(&v, parsestr(STR(CHILD(n, i))));
973 return v;
976 static void
977 com_list_constructor(c, n)
978 struct compiling *c;
979 node *n;
981 int len;
982 int i;
983 if (TYPE(n) != testlist)
984 REQ(n, exprlist);
985 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
986 len = (NCH(n) + 1) / 2;
987 for (i = 0; i < NCH(n); i += 2)
988 com_node(c, CHILD(n, i));
989 com_addoparg(c, BUILD_LIST, len);
990 com_pop(c, len-1);
993 static void
994 com_dictmaker(c, n)
995 struct compiling *c;
996 node *n;
998 int i;
999 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1000 for (i = 0; i+2 < NCH(n); i += 4) {
1001 /* We must arrange things just right for STORE_SUBSCR.
1002 It wants the stack to look like (value) (dict) (key) */
1003 com_addbyte(c, DUP_TOP);
1004 com_push(c, 1);
1005 com_node(c, CHILD(n, i+2)); /* value */
1006 com_addbyte(c, ROT_TWO);
1007 com_node(c, CHILD(n, i)); /* key */
1008 com_addbyte(c, STORE_SUBSCR);
1009 com_pop(c, 3);
1013 static void
1014 com_atom(c, n)
1015 struct compiling *c;
1016 node *n;
1018 node *ch;
1019 PyObject *v;
1020 int i;
1021 REQ(n, atom);
1022 ch = CHILD(n, 0);
1023 switch (TYPE(ch)) {
1024 case LPAR:
1025 if (TYPE(CHILD(n, 1)) == RPAR) {
1026 com_addoparg(c, BUILD_TUPLE, 0);
1027 com_push(c, 1);
1029 else
1030 com_node(c, CHILD(n, 1));
1031 break;
1032 case LSQB:
1033 if (TYPE(CHILD(n, 1)) == RSQB) {
1034 com_addoparg(c, BUILD_LIST, 0);
1035 com_push(c, 1);
1037 else
1038 com_list_constructor(c, CHILD(n, 1));
1039 break;
1040 case LBRACE: /* '{' [dictmaker] '}' */
1041 com_addoparg(c, BUILD_MAP, 0);
1042 com_push(c, 1);
1043 if (TYPE(CHILD(n, 1)) != RBRACE)
1044 com_dictmaker(c, CHILD(n, 1));
1045 break;
1046 case BACKQUOTE:
1047 com_node(c, CHILD(n, 1));
1048 com_addbyte(c, UNARY_CONVERT);
1049 break;
1050 case NUMBER:
1051 if ((v = parsenumber(c, STR(ch))) == NULL) {
1052 i = 255;
1054 else {
1055 i = com_addconst(c, v);
1056 Py_DECREF(v);
1058 com_addoparg(c, LOAD_CONST, i);
1059 com_push(c, 1);
1060 break;
1061 case STRING:
1062 v = parsestrplus(n);
1063 if (v == NULL) {
1064 c->c_errors++;
1065 i = 255;
1067 else {
1068 i = com_addconst(c, v);
1069 Py_DECREF(v);
1071 com_addoparg(c, LOAD_CONST, i);
1072 com_push(c, 1);
1073 break;
1074 case NAME:
1075 com_addopname(c, LOAD_NAME, ch);
1076 com_push(c, 1);
1077 break;
1078 default:
1079 /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
1080 com_error(c, PyExc_SystemError,
1081 "com_atom: unexpected node type");
1085 static void
1086 com_slice(c, n, op)
1087 struct compiling *c;
1088 node *n;
1089 int op;
1091 if (NCH(n) == 1) {
1092 com_addbyte(c, op);
1094 else if (NCH(n) == 2) {
1095 if (TYPE(CHILD(n, 0)) != COLON) {
1096 com_node(c, CHILD(n, 0));
1097 com_addbyte(c, op+1);
1099 else {
1100 com_node(c, CHILD(n, 1));
1101 com_addbyte(c, op+2);
1103 com_pop(c, 1);
1105 else {
1106 com_node(c, CHILD(n, 0));
1107 com_node(c, CHILD(n, 2));
1108 com_addbyte(c, op+3);
1109 com_pop(c, 2);
1113 static void
1114 com_argument(c, n, pkeywords)
1115 struct compiling *c;
1116 node *n; /* argument */
1117 PyObject **pkeywords;
1119 node *m;
1120 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
1121 if (NCH(n) == 1) {
1122 if (*pkeywords != NULL) {
1123 com_error(c, PyExc_SyntaxError,
1124 "non-keyword arg after keyword arg");
1126 else {
1127 com_node(c, CHILD(n, 0));
1129 return;
1131 m = n;
1132 do {
1133 m = CHILD(m, 0);
1134 } while (NCH(m) == 1);
1135 if (TYPE(m) != NAME) {
1136 com_error(c, PyExc_SyntaxError,
1137 "keyword can't be an expression");
1139 else {
1140 PyObject *v = PyString_InternFromString(STR(m));
1141 if (v != NULL && *pkeywords == NULL)
1142 *pkeywords = PyDict_New();
1143 if (v == NULL || *pkeywords == NULL)
1144 c->c_errors++;
1145 else {
1146 if (PyDict_GetItem(*pkeywords, v) != NULL)
1147 com_error(c, PyExc_SyntaxError,
1148 "duplicate keyword argument");
1149 else
1150 if (PyDict_SetItem(*pkeywords, v, v) != 0)
1151 c->c_errors++;
1152 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1153 com_push(c, 1);
1154 Py_DECREF(v);
1157 com_node(c, CHILD(n, 2));
1160 static void
1161 com_call_function(c, n)
1162 struct compiling *c;
1163 node *n; /* EITHER arglist OR ')' */
1165 if (TYPE(n) == RPAR) {
1166 com_addoparg(c, CALL_FUNCTION, 0);
1168 else {
1169 PyObject *keywords = NULL;
1170 int i, na, nk;
1171 REQ(n, arglist);
1172 na = 0;
1173 nk = 0;
1174 for (i = 0; i < NCH(n); i += 2) {
1175 com_argument(c, CHILD(n, i), &keywords);
1176 if (keywords == NULL)
1177 na++;
1178 else
1179 nk++;
1181 Py_XDECREF(keywords);
1182 if (na > 255 || nk > 255) {
1183 com_error(c, PyExc_SyntaxError,
1184 "more than 255 arguments");
1186 com_addoparg(c, CALL_FUNCTION, na | (nk << 8));
1187 com_pop(c, na + 2*nk);
1191 static void
1192 com_select_member(c, n)
1193 struct compiling *c;
1194 node *n;
1196 com_addopname(c, LOAD_ATTR, n);
1199 static void
1200 com_sliceobj(c, n)
1201 struct compiling *c;
1202 node *n;
1204 int i=0;
1205 int ns=2; /* number of slice arguments */
1206 node *ch;
1208 /* first argument */
1209 if (TYPE(CHILD(n,i)) == COLON) {
1210 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1211 com_push(c, 1);
1212 i++;
1214 else {
1215 com_node(c, CHILD(n,i));
1216 i++;
1217 REQ(CHILD(n,i),COLON);
1218 i++;
1220 /* second argument */
1221 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1222 com_node(c, CHILD(n,i));
1223 i++;
1225 else {
1226 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1227 com_push(c, 1);
1229 /* remaining arguments */
1230 for (; i < NCH(n); i++) {
1231 ns++;
1232 ch=CHILD(n,i);
1233 REQ(ch, sliceop);
1234 if (NCH(ch) == 1) {
1235 /* right argument of ':' missing */
1236 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1237 com_push(c, 1);
1239 else
1240 com_node(c, CHILD(ch,1));
1242 com_addoparg(c, BUILD_SLICE, ns);
1243 com_pop(c, 1 + (ns == 3));
1246 static void
1247 com_subscript(c, n)
1248 struct compiling *c;
1249 node *n;
1251 node *ch;
1252 REQ(n, subscript);
1253 ch = CHILD(n,0);
1254 /* check for rubber index */
1255 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
1256 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
1257 com_push(c, 1);
1259 else {
1260 /* check for slice */
1261 if ((TYPE(ch) == COLON || NCH(n) > 1))
1262 com_sliceobj(c, n);
1263 else {
1264 REQ(ch, test);
1265 com_node(c, ch);
1270 static void
1271 com_subscriptlist(c, n, assigning)
1272 struct compiling *c;
1273 node *n;
1274 int assigning;
1276 int i, op;
1277 REQ(n, subscriptlist);
1278 /* Check to make backward compatible slice behavior for '[i:j]' */
1279 if (NCH(n) == 1) {
1280 node *sub = CHILD(n, 0); /* subscript */
1281 /* Make it is a simple slice.
1282 Should have exactly one colon. */
1283 if ((TYPE(CHILD(sub, 0)) == COLON
1284 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1285 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1287 if (assigning == OP_APPLY)
1288 op = SLICE;
1289 else
1290 op = ((assigning == OP_ASSIGN) ?
1291 STORE_SLICE : DELETE_SLICE);
1292 com_slice(c, sub, op);
1293 if (op == STORE_SLICE)
1294 com_pop(c, 2);
1295 else if (op == DELETE_SLICE)
1296 com_pop(c, 1);
1297 return;
1300 /* Else normal subscriptlist. Compile each subscript. */
1301 for (i = 0; i < NCH(n); i += 2)
1302 com_subscript(c, CHILD(n, i));
1303 /* Put multiple subscripts into a tuple */
1304 if (NCH(n) > 1) {
1305 i = (NCH(n)+1) / 2;
1306 com_addoparg(c, BUILD_TUPLE, i);
1307 com_pop(c, i-1);
1309 if (assigning == OP_APPLY) {
1310 op = BINARY_SUBSCR;
1311 i = 1;
1313 else if (assigning == OP_ASSIGN) {
1314 op = STORE_SUBSCR;
1315 i = 3;
1317 else {
1318 op = DELETE_SUBSCR;
1319 i = 2;
1321 com_addbyte(c, op);
1322 com_pop(c, i);
1325 static void
1326 com_apply_trailer(c, n)
1327 struct compiling *c;
1328 node *n;
1330 REQ(n, trailer);
1331 switch (TYPE(CHILD(n, 0))) {
1332 case LPAR:
1333 com_call_function(c, CHILD(n, 1));
1334 break;
1335 case DOT:
1336 com_select_member(c, CHILD(n, 1));
1337 break;
1338 case LSQB:
1339 com_subscriptlist(c, CHILD(n, 1), OP_APPLY);
1340 break;
1341 default:
1342 com_error(c, PyExc_SystemError,
1343 "com_apply_trailer: unknown trailer type");
1347 static void
1348 com_power(c, n)
1349 struct compiling *c;
1350 node *n;
1352 int i;
1353 REQ(n, power);
1354 com_atom(c, CHILD(n, 0));
1355 for (i = 1; i < NCH(n); i++) {
1356 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1357 com_factor(c, CHILD(n, i+1));
1358 com_addbyte(c, BINARY_POWER);
1359 com_pop(c, 1);
1360 break;
1362 else
1363 com_apply_trailer(c, CHILD(n, i));
1367 static void
1368 com_factor(c, n)
1369 struct compiling *c;
1370 node *n;
1372 REQ(n, factor);
1373 if (TYPE(CHILD(n, 0)) == PLUS) {
1374 com_factor(c, CHILD(n, 1));
1375 com_addbyte(c, UNARY_POSITIVE);
1377 else if (TYPE(CHILD(n, 0)) == MINUS) {
1378 com_factor(c, CHILD(n, 1));
1379 com_addbyte(c, UNARY_NEGATIVE);
1381 else if (TYPE(CHILD(n, 0)) == TILDE) {
1382 com_factor(c, CHILD(n, 1));
1383 com_addbyte(c, UNARY_INVERT);
1385 else {
1386 com_power(c, CHILD(n, 0));
1390 static void
1391 com_term(c, n)
1392 struct compiling *c;
1393 node *n;
1395 int i;
1396 int op;
1397 REQ(n, term);
1398 com_factor(c, CHILD(n, 0));
1399 for (i = 2; i < NCH(n); i += 2) {
1400 com_factor(c, CHILD(n, i));
1401 switch (TYPE(CHILD(n, i-1))) {
1402 case STAR:
1403 op = BINARY_MULTIPLY;
1404 break;
1405 case SLASH:
1406 op = BINARY_DIVIDE;
1407 break;
1408 case PERCENT:
1409 op = BINARY_MODULO;
1410 break;
1411 default:
1412 com_error(c, PyExc_SystemError,
1413 "com_term: operator not *, / or %");
1414 op = 255;
1416 com_addbyte(c, op);
1417 com_pop(c, 1);
1421 static void
1422 com_arith_expr(c, n)
1423 struct compiling *c;
1424 node *n;
1426 int i;
1427 int op;
1428 REQ(n, arith_expr);
1429 com_term(c, CHILD(n, 0));
1430 for (i = 2; i < NCH(n); i += 2) {
1431 com_term(c, CHILD(n, i));
1432 switch (TYPE(CHILD(n, i-1))) {
1433 case PLUS:
1434 op = BINARY_ADD;
1435 break;
1436 case MINUS:
1437 op = BINARY_SUBTRACT;
1438 break;
1439 default:
1440 com_error(c, PyExc_SystemError,
1441 "com_arith_expr: operator not + or -");
1442 op = 255;
1444 com_addbyte(c, op);
1445 com_pop(c, 1);
1449 static void
1450 com_shift_expr(c, n)
1451 struct compiling *c;
1452 node *n;
1454 int i;
1455 int op;
1456 REQ(n, shift_expr);
1457 com_arith_expr(c, CHILD(n, 0));
1458 for (i = 2; i < NCH(n); i += 2) {
1459 com_arith_expr(c, CHILD(n, i));
1460 switch (TYPE(CHILD(n, i-1))) {
1461 case LEFTSHIFT:
1462 op = BINARY_LSHIFT;
1463 break;
1464 case RIGHTSHIFT:
1465 op = BINARY_RSHIFT;
1466 break;
1467 default:
1468 com_error(c, PyExc_SystemError,
1469 "com_shift_expr: operator not << or >>");
1470 op = 255;
1472 com_addbyte(c, op);
1473 com_pop(c, 1);
1477 static void
1478 com_and_expr(c, n)
1479 struct compiling *c;
1480 node *n;
1482 int i;
1483 int op;
1484 REQ(n, and_expr);
1485 com_shift_expr(c, CHILD(n, 0));
1486 for (i = 2; i < NCH(n); i += 2) {
1487 com_shift_expr(c, CHILD(n, i));
1488 if (TYPE(CHILD(n, i-1)) == AMPER) {
1489 op = BINARY_AND;
1491 else {
1492 com_error(c, PyExc_SystemError,
1493 "com_and_expr: operator not &");
1494 op = 255;
1496 com_addbyte(c, op);
1497 com_pop(c, 1);
1501 static void
1502 com_xor_expr(c, n)
1503 struct compiling *c;
1504 node *n;
1506 int i;
1507 int op;
1508 REQ(n, xor_expr);
1509 com_and_expr(c, CHILD(n, 0));
1510 for (i = 2; i < NCH(n); i += 2) {
1511 com_and_expr(c, CHILD(n, i));
1512 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1513 op = BINARY_XOR;
1515 else {
1516 com_error(c, PyExc_SystemError,
1517 "com_xor_expr: operator not ^");
1518 op = 255;
1520 com_addbyte(c, op);
1521 com_pop(c, 1);
1525 static void
1526 com_expr(c, n)
1527 struct compiling *c;
1528 node *n;
1530 int i;
1531 int op;
1532 REQ(n, expr);
1533 com_xor_expr(c, CHILD(n, 0));
1534 for (i = 2; i < NCH(n); i += 2) {
1535 com_xor_expr(c, CHILD(n, i));
1536 if (TYPE(CHILD(n, i-1)) == VBAR) {
1537 op = BINARY_OR;
1539 else {
1540 com_error(c, PyExc_SystemError,
1541 "com_expr: expr operator not |");
1542 op = 255;
1544 com_addbyte(c, op);
1545 com_pop(c, 1);
1549 static enum cmp_op
1550 cmp_type(n)
1551 node *n;
1553 REQ(n, comp_op);
1554 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1555 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1556 if (NCH(n) == 1) {
1557 n = CHILD(n, 0);
1558 switch (TYPE(n)) {
1559 case LESS: return LT;
1560 case GREATER: return GT;
1561 case EQEQUAL: /* == */
1562 case EQUAL: return EQ;
1563 case LESSEQUAL: return LE;
1564 case GREATEREQUAL: return GE;
1565 case NOTEQUAL: return NE; /* <> or != */
1566 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1567 if (strcmp(STR(n), "is") == 0) return IS;
1570 else if (NCH(n) == 2) {
1571 switch (TYPE(CHILD(n, 0))) {
1572 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1573 return NOT_IN;
1574 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1575 return IS_NOT;
1578 return BAD;
1581 static void
1582 com_comparison(c, n)
1583 struct compiling *c;
1584 node *n;
1586 int i;
1587 enum cmp_op op;
1588 int anchor;
1589 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1590 com_expr(c, CHILD(n, 0));
1591 if (NCH(n) == 1)
1592 return;
1594 /****************************************************************
1595 The following code is generated for all but the last
1596 comparison in a chain:
1598 label: on stack: opcode: jump to:
1600 a <code to load b>
1601 a, b DUP_TOP
1602 a, b, b ROT_THREE
1603 b, a, b COMPARE_OP
1604 b, 0-or-1 JUMP_IF_FALSE L1
1605 b, 1 POP_TOP
1608 We are now ready to repeat this sequence for the next
1609 comparison in the chain.
1611 For the last we generate:
1613 b <code to load c>
1614 b, c COMPARE_OP
1615 0-or-1
1617 If there were any jumps to L1 (i.e., there was more than one
1618 comparison), we generate:
1620 0-or-1 JUMP_FORWARD L2
1621 L1: b, 0 ROT_TWO
1622 0, b POP_TOP
1624 L2: 0-or-1
1625 ****************************************************************/
1627 anchor = 0;
1629 for (i = 2; i < NCH(n); i += 2) {
1630 com_expr(c, CHILD(n, i));
1631 if (i+2 < NCH(n)) {
1632 com_addbyte(c, DUP_TOP);
1633 com_push(c, 1);
1634 com_addbyte(c, ROT_THREE);
1636 op = cmp_type(CHILD(n, i-1));
1637 if (op == BAD) {
1638 com_error(c, PyExc_SystemError,
1639 "com_comparison: unknown comparison op");
1641 com_addoparg(c, COMPARE_OP, op);
1642 com_pop(c, 1);
1643 if (i+2 < NCH(n)) {
1644 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1645 com_addbyte(c, POP_TOP);
1646 com_pop(c, 1);
1650 if (anchor) {
1651 int anchor2 = 0;
1652 com_addfwref(c, JUMP_FORWARD, &anchor2);
1653 com_backpatch(c, anchor);
1654 com_addbyte(c, ROT_TWO);
1655 com_addbyte(c, POP_TOP);
1656 com_backpatch(c, anchor2);
1660 static void
1661 com_not_test(c, n)
1662 struct compiling *c;
1663 node *n;
1665 REQ(n, not_test); /* 'not' not_test | comparison */
1666 if (NCH(n) == 1) {
1667 com_comparison(c, CHILD(n, 0));
1669 else {
1670 com_not_test(c, CHILD(n, 1));
1671 com_addbyte(c, UNARY_NOT);
1675 static void
1676 com_and_test(c, n)
1677 struct compiling *c;
1678 node *n;
1680 int i;
1681 int anchor;
1682 REQ(n, and_test); /* not_test ('and' not_test)* */
1683 anchor = 0;
1684 i = 0;
1685 for (;;) {
1686 com_not_test(c, CHILD(n, i));
1687 if ((i += 2) >= NCH(n))
1688 break;
1689 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1690 com_addbyte(c, POP_TOP);
1691 com_pop(c, 1);
1693 if (anchor)
1694 com_backpatch(c, anchor);
1697 static void
1698 com_test(c, n)
1699 struct compiling *c;
1700 node *n;
1702 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
1703 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
1704 PyObject *v;
1705 int i;
1706 int ndefs = com_argdefs(c, CHILD(n, 0));
1707 v = (PyObject *) icompile(CHILD(n, 0), c);
1708 if (v == NULL) {
1709 c->c_errors++;
1710 i = 255;
1712 else {
1713 i = com_addconst(c, v);
1714 Py_DECREF(v);
1716 com_addoparg(c, LOAD_CONST, i);
1717 com_push(c, 1);
1718 com_addoparg(c, MAKE_FUNCTION, ndefs);
1719 com_pop(c, ndefs);
1721 else {
1722 int anchor = 0;
1723 int i = 0;
1724 for (;;) {
1725 com_and_test(c, CHILD(n, i));
1726 if ((i += 2) >= NCH(n))
1727 break;
1728 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1729 com_addbyte(c, POP_TOP);
1730 com_pop(c, 1);
1732 if (anchor)
1733 com_backpatch(c, anchor);
1737 static void
1738 com_list(c, n, toplevel)
1739 struct compiling *c;
1740 node *n;
1741 int toplevel; /* If nonzero, *always* build a tuple */
1743 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
1744 if (NCH(n) == 1 && !toplevel) {
1745 com_node(c, CHILD(n, 0));
1747 else {
1748 int i;
1749 int len;
1750 len = (NCH(n) + 1) / 2;
1751 for (i = 0; i < NCH(n); i += 2)
1752 com_node(c, CHILD(n, i));
1753 com_addoparg(c, BUILD_TUPLE, len);
1754 com_pop(c, len-1);
1759 /* Begin of assignment compilation */
1761 static void com_assign_name Py_PROTO((struct compiling *, node *, int));
1762 static void com_assign Py_PROTO((struct compiling *, node *, int));
1764 static void
1765 com_assign_attr(c, n, assigning)
1766 struct compiling *c;
1767 node *n;
1768 int assigning;
1770 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
1771 com_pop(c, assigning ? 2 : 1);
1774 static void
1775 com_assign_trailer(c, n, assigning)
1776 struct compiling *c;
1777 node *n;
1778 int assigning;
1780 REQ(n, trailer);
1781 switch (TYPE(CHILD(n, 0))) {
1782 case LPAR: /* '(' [exprlist] ')' */
1783 com_error(c, PyExc_SyntaxError,
1784 "can't assign to function call");
1785 break;
1786 case DOT: /* '.' NAME */
1787 com_assign_attr(c, CHILD(n, 1), assigning);
1788 break;
1789 case LSQB: /* '[' subscriptlist ']' */
1790 com_subscriptlist(c, CHILD(n, 1), assigning);
1791 break;
1792 default:
1793 com_error(c, PyExc_SystemError, "unknown trailer type");
1797 static void
1798 com_assign_tuple(c, n, assigning)
1799 struct compiling *c;
1800 node *n;
1801 int assigning;
1803 int i;
1804 if (TYPE(n) != testlist)
1805 REQ(n, exprlist);
1806 if (assigning) {
1807 i = (NCH(n)+1)/2;
1808 com_addoparg(c, UNPACK_TUPLE, i);
1809 com_push(c, i-1);
1811 for (i = 0; i < NCH(n); i += 2)
1812 com_assign(c, CHILD(n, i), assigning);
1815 static void
1816 com_assign_list(c, n, assigning)
1817 struct compiling *c;
1818 node *n;
1819 int assigning;
1821 int i;
1822 if (assigning) {
1823 i = (NCH(n)+1)/2;
1824 com_addoparg(c, UNPACK_LIST, i);
1825 com_push(c, i-1);
1827 for (i = 0; i < NCH(n); i += 2)
1828 com_assign(c, CHILD(n, i), assigning);
1831 static void
1832 com_assign_name(c, n, assigning)
1833 struct compiling *c;
1834 node *n;
1835 int assigning;
1837 REQ(n, NAME);
1838 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
1839 if (assigning)
1840 com_pop(c, 1);
1843 static void
1844 com_assign(c, n, assigning)
1845 struct compiling *c;
1846 node *n;
1847 int assigning;
1849 /* Loop to avoid trivial recursion */
1850 for (;;) {
1851 switch (TYPE(n)) {
1853 case exprlist:
1854 case testlist:
1855 if (NCH(n) > 1) {
1856 com_assign_tuple(c, n, assigning);
1857 return;
1859 n = CHILD(n, 0);
1860 break;
1862 case test:
1863 case and_test:
1864 case not_test:
1865 case comparison:
1866 case expr:
1867 case xor_expr:
1868 case and_expr:
1869 case shift_expr:
1870 case arith_expr:
1871 case term:
1872 case factor:
1873 if (NCH(n) > 1) {
1874 com_error(c, PyExc_SyntaxError,
1875 "can't assign to operator");
1876 return;
1878 n = CHILD(n, 0);
1879 break;
1881 case power: /* atom trailer* ('**' power)* */
1882 /* ('+'|'-'|'~') factor | atom trailer* */
1883 if (TYPE(CHILD(n, 0)) != atom) {
1884 com_error(c, PyExc_SyntaxError,
1885 "can't assign to operator");
1886 return;
1888 if (NCH(n) > 1) { /* trailer or exponent present */
1889 int i;
1890 com_node(c, CHILD(n, 0));
1891 for (i = 1; i+1 < NCH(n); i++) {
1892 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1893 com_error(c, PyExc_SyntaxError,
1894 "can't assign to operator");
1895 return;
1897 com_apply_trailer(c, CHILD(n, i));
1898 } /* NB i is still alive */
1899 com_assign_trailer(c,
1900 CHILD(n, i), assigning);
1901 return;
1903 n = CHILD(n, 0);
1904 break;
1906 case atom:
1907 switch (TYPE(CHILD(n, 0))) {
1908 case LPAR:
1909 n = CHILD(n, 1);
1910 if (TYPE(n) == RPAR) {
1911 /* XXX Should allow () = () ??? */
1912 com_error(c, PyExc_SyntaxError,
1913 "can't assign to ()");
1914 return;
1916 break;
1917 case LSQB:
1918 n = CHILD(n, 1);
1919 if (TYPE(n) == RSQB) {
1920 com_error(c, PyExc_SyntaxError,
1921 "can't assign to []");
1922 return;
1924 com_assign_list(c, n, assigning);
1925 return;
1926 case NAME:
1927 com_assign_name(c, CHILD(n, 0), assigning);
1928 return;
1929 default:
1930 com_error(c, PyExc_SyntaxError,
1931 "can't assign to literal");
1932 return;
1934 break;
1936 case lambdef:
1937 com_error(c, PyExc_SyntaxError,
1938 "can't assign to lambda");
1939 return;
1941 default:
1942 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
1943 com_error(c, PyExc_SystemError,
1944 "com_assign: bad node");
1945 return;
1951 /* Forward */ static node *get_rawdocstring Py_PROTO((node *));
1953 static void
1954 com_expr_stmt(c, n)
1955 struct compiling *c;
1956 node *n;
1958 REQ(n, expr_stmt); /* testlist ('=' testlist)* */
1959 /* Forget it if we have just a doc string here */
1960 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
1961 return;
1962 com_node(c, CHILD(n, NCH(n)-1));
1963 if (NCH(n) == 1) {
1964 if (c->c_interactive)
1965 com_addbyte(c, PRINT_EXPR);
1966 else
1967 com_addbyte(c, POP_TOP);
1968 com_pop(c, 1);
1970 else {
1971 int i;
1972 for (i = 0; i < NCH(n)-2; i+=2) {
1973 if (i+2 < NCH(n)-2) {
1974 com_addbyte(c, DUP_TOP);
1975 com_push(c, 1);
1977 com_assign(c, CHILD(n, i), OP_ASSIGN);
1982 static void
1983 com_assert_stmt(c, n)
1984 struct compiling *c;
1985 node *n;
1987 int a = 0, b = 0;
1988 int i;
1989 REQ(n, assert_stmt); /* 'assert' test [',' test] */
1990 /* Generate code like for
1992 if __debug__:
1993 if not <test>:
1994 raise AssertionError [, <message>]
1996 where <message> is the second test, if present.
1998 if (Py_OptimizeFlag)
1999 return;
2000 com_addopnamestr(c, LOAD_GLOBAL, "__debug__");
2001 com_push(c, 1);
2002 com_addfwref(c, JUMP_IF_FALSE, &a);
2003 com_addbyte(c, POP_TOP);
2004 com_pop(c, 1);
2005 com_node(c, CHILD(n, 1));
2006 com_addfwref(c, JUMP_IF_TRUE, &b);
2007 com_addbyte(c, POP_TOP);
2008 com_pop(c, 1);
2009 /* Raise that exception! */
2010 com_addopnamestr(c, LOAD_GLOBAL, "AssertionError");
2011 com_push(c, 1);
2012 i = NCH(n)/2; /* Either 2 or 4 */
2013 if (i > 1)
2014 com_node(c, CHILD(n, 3));
2015 com_addoparg(c, RAISE_VARARGS, i);
2016 com_pop(c, i);
2017 /* The interpreter does not fall through */
2018 /* All jumps converge here */
2019 com_backpatch(c, a);
2020 com_backpatch(c, b);
2021 com_addbyte(c, POP_TOP);
2024 static void
2025 com_print_stmt(c, n)
2026 struct compiling *c;
2027 node *n;
2029 int i;
2030 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2031 for (i = 1; i < NCH(n); i += 2) {
2032 com_node(c, CHILD(n, i));
2033 com_addbyte(c, PRINT_ITEM);
2034 com_pop(c, 1);
2036 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
2037 com_addbyte(c, PRINT_NEWLINE);
2038 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2041 static void
2042 com_return_stmt(c, n)
2043 struct compiling *c;
2044 node *n;
2046 REQ(n, return_stmt); /* 'return' [testlist] */
2047 if (!c->c_infunction) {
2048 com_error(c, PyExc_SyntaxError, "'return' outside function");
2050 if (NCH(n) < 2) {
2051 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2052 com_push(c, 1);
2054 else
2055 com_node(c, CHILD(n, 1));
2056 com_addbyte(c, RETURN_VALUE);
2057 com_pop(c, 1);
2060 static void
2061 com_raise_stmt(c, n)
2062 struct compiling *c;
2063 node *n;
2065 int i;
2066 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2067 if (NCH(n) > 1) {
2068 com_node(c, CHILD(n, 1));
2069 if (NCH(n) > 3) {
2070 com_node(c, CHILD(n, 3));
2071 if (NCH(n) > 5)
2072 com_node(c, CHILD(n, 5));
2075 i = NCH(n)/2;
2076 com_addoparg(c, RAISE_VARARGS, i);
2077 com_pop(c, i);
2080 static void
2081 com_import_stmt(c, n)
2082 struct compiling *c;
2083 node *n;
2085 int i;
2086 REQ(n, import_stmt);
2087 /* 'import' dotted_name (',' dotted_name)* |
2088 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2089 if (STR(CHILD(n, 0))[0] == 'f') {
2090 /* 'from' dotted_name 'import' ... */
2091 REQ(CHILD(n, 1), dotted_name);
2092 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
2093 com_push(c, 1);
2094 for (i = 3; i < NCH(n); i += 2)
2095 com_addopname(c, IMPORT_FROM, CHILD(n, i));
2096 com_addbyte(c, POP_TOP);
2097 com_pop(c, 1);
2099 else {
2100 /* 'import' ... */
2101 for (i = 1; i < NCH(n); i += 2) {
2102 REQ(CHILD(n, i), dotted_name);
2103 com_addopname(c, IMPORT_NAME, CHILD(n, i));
2104 com_push(c, 1);
2105 com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
2106 com_pop(c, 1);
2111 static void
2112 com_global_stmt(c, n)
2113 struct compiling *c;
2114 node *n;
2116 int i;
2117 REQ(n, global_stmt);
2118 /* 'global' NAME (',' NAME)* */
2119 for (i = 1; i < NCH(n); i += 2) {
2120 char *s = STR(CHILD(n, i));
2121 #ifdef PRIVATE_NAME_MANGLING
2122 char buffer[256];
2123 if (s != NULL && s[0] == '_' && s[1] == '_' &&
2124 c->c_private != NULL &&
2125 com_mangle(c, s, buffer, (int)sizeof(buffer)))
2126 s = buffer;
2127 #endif
2128 if (PyDict_GetItemString(c->c_locals, s) != NULL) {
2129 com_error(c, PyExc_SyntaxError,
2130 "name is local and global");
2132 else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0)
2133 c->c_errors++;
2137 static int
2138 com_newlocal_o(c, nameval)
2139 struct compiling *c;
2140 PyObject *nameval;
2142 int i;
2143 PyObject *ival;
2144 if (PyList_Size(c->c_varnames) != c->c_nlocals) {
2145 /* This is usually caused by an error on a previous call */
2146 if (c->c_errors == 0) {
2147 com_error(c, PyExc_SystemError,
2148 "mixed up var name/index");
2150 return 0;
2152 ival = PyInt_FromLong(i = c->c_nlocals++);
2153 if (ival == NULL)
2154 c->c_errors++;
2155 else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0)
2156 c->c_errors++;
2157 else if (PyList_Append(c->c_varnames, nameval) != 0)
2158 c->c_errors++;
2159 Py_XDECREF(ival);
2160 return i;
2163 static int
2164 com_addlocal_o(c, nameval)
2165 struct compiling *c;
2166 PyObject *nameval;
2168 PyObject *ival = PyDict_GetItem(c->c_locals, nameval);
2169 if (ival != NULL)
2170 return PyInt_AsLong(ival);
2171 return com_newlocal_o(c, nameval);
2174 static int
2175 com_newlocal(c, name)
2176 struct compiling *c;
2177 char *name;
2179 PyObject *nameval = PyString_InternFromString(name);
2180 int i;
2181 if (nameval == NULL) {
2182 c->c_errors++;
2183 return 0;
2185 i = com_newlocal_o(c, nameval);
2186 Py_DECREF(nameval);
2187 return i;
2190 static void
2191 com_exec_stmt(c, n)
2192 struct compiling *c;
2193 node *n;
2195 REQ(n, exec_stmt);
2196 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2197 com_node(c, CHILD(n, 1));
2198 if (NCH(n) >= 4)
2199 com_node(c, CHILD(n, 3));
2200 else {
2201 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2202 com_push(c, 1);
2204 if (NCH(n) >= 6)
2205 com_node(c, CHILD(n, 5));
2206 else {
2207 com_addbyte(c, DUP_TOP);
2208 com_push(c, 1);
2210 com_addbyte(c, EXEC_STMT);
2211 com_pop(c, 3);
2214 static int
2215 is_constant_false(c, n)
2216 struct compiling *c;
2217 node *n;
2219 PyObject *v;
2220 int i;
2222 /* Label to avoid tail recursion */
2223 next:
2224 switch (TYPE(n)) {
2226 case suite:
2227 if (NCH(n) == 1) {
2228 n = CHILD(n, 0);
2229 goto next;
2231 /* Fall through */
2232 case file_input:
2233 for (i = 0; i < NCH(n); i++) {
2234 node *ch = CHILD(n, i);
2235 if (TYPE(ch) == stmt) {
2236 n = ch;
2237 goto next;
2240 break;
2242 case stmt:
2243 case simple_stmt:
2244 case small_stmt:
2245 n = CHILD(n, 0);
2246 goto next;
2248 case expr_stmt:
2249 case testlist:
2250 case test:
2251 case and_test:
2252 case not_test:
2253 case comparison:
2254 case expr:
2255 case xor_expr:
2256 case and_expr:
2257 case shift_expr:
2258 case arith_expr:
2259 case term:
2260 case factor:
2261 case power:
2262 case atom:
2263 if (NCH(n) == 1) {
2264 n = CHILD(n, 0);
2265 goto next;
2267 break;
2269 case NAME:
2270 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2271 return 1;
2272 break;
2274 case NUMBER:
2275 v = parsenumber(c, STR(n));
2276 if (v == NULL) {
2277 PyErr_Clear();
2278 break;
2280 i = PyObject_IsTrue(v);
2281 Py_DECREF(v);
2282 return i == 0;
2284 case STRING:
2285 v = parsestr(STR(n));
2286 if (v == NULL) {
2287 PyErr_Clear();
2288 break;
2290 i = PyObject_IsTrue(v);
2291 Py_DECREF(v);
2292 return i == 0;
2295 return 0;
2298 static void
2299 com_if_stmt(c, n)
2300 struct compiling *c;
2301 node *n;
2303 int i;
2304 int anchor = 0;
2305 REQ(n, if_stmt);
2306 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2307 for (i = 0; i+3 < NCH(n); i+=4) {
2308 int a = 0;
2309 node *ch = CHILD(n, i+1);
2310 if (is_constant_false(c, ch))
2311 continue;
2312 if (i > 0)
2313 com_addoparg(c, SET_LINENO, ch->n_lineno);
2314 com_node(c, ch);
2315 com_addfwref(c, JUMP_IF_FALSE, &a);
2316 com_addbyte(c, POP_TOP);
2317 com_pop(c, 1);
2318 com_node(c, CHILD(n, i+3));
2319 com_addfwref(c, JUMP_FORWARD, &anchor);
2320 com_backpatch(c, a);
2321 /* We jump here with an extra entry which we now pop */
2322 com_addbyte(c, POP_TOP);
2324 if (i+2 < NCH(n))
2325 com_node(c, CHILD(n, i+2));
2326 if (anchor)
2327 com_backpatch(c, anchor);
2330 static void
2331 com_while_stmt(c, n)
2332 struct compiling *c;
2333 node *n;
2335 int break_anchor = 0;
2336 int anchor = 0;
2337 int save_begin = c->c_begin;
2338 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2339 com_addfwref(c, SETUP_LOOP, &break_anchor);
2340 block_push(c, SETUP_LOOP);
2341 c->c_begin = c->c_nexti;
2342 com_addoparg(c, SET_LINENO, n->n_lineno);
2343 com_node(c, CHILD(n, 1));
2344 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2345 com_addbyte(c, POP_TOP);
2346 com_pop(c, 1);
2347 c->c_loops++;
2348 com_node(c, CHILD(n, 3));
2349 c->c_loops--;
2350 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2351 c->c_begin = save_begin;
2352 com_backpatch(c, anchor);
2353 /* We jump here with one entry more on the stack */
2354 com_addbyte(c, POP_TOP);
2355 com_addbyte(c, POP_BLOCK);
2356 block_pop(c, SETUP_LOOP);
2357 if (NCH(n) > 4)
2358 com_node(c, CHILD(n, 6));
2359 com_backpatch(c, break_anchor);
2362 static void
2363 com_for_stmt(c, n)
2364 struct compiling *c;
2365 node *n;
2367 PyObject *v;
2368 int break_anchor = 0;
2369 int anchor = 0;
2370 int save_begin = c->c_begin;
2371 REQ(n, for_stmt);
2372 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2373 com_addfwref(c, SETUP_LOOP, &break_anchor);
2374 block_push(c, SETUP_LOOP);
2375 com_node(c, CHILD(n, 3));
2376 v = PyInt_FromLong(0L);
2377 if (v == NULL)
2378 c->c_errors++;
2379 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
2380 com_push(c, 1);
2381 Py_XDECREF(v);
2382 c->c_begin = c->c_nexti;
2383 com_addoparg(c, SET_LINENO, n->n_lineno);
2384 com_addfwref(c, FOR_LOOP, &anchor);
2385 com_push(c, 1);
2386 com_assign(c, CHILD(n, 1), OP_ASSIGN);
2387 c->c_loops++;
2388 com_node(c, CHILD(n, 5));
2389 c->c_loops--;
2390 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2391 c->c_begin = save_begin;
2392 com_backpatch(c, anchor);
2393 com_pop(c, 2); /* FOR_LOOP has popped these */
2394 com_addbyte(c, POP_BLOCK);
2395 block_pop(c, SETUP_LOOP);
2396 if (NCH(n) > 8)
2397 com_node(c, CHILD(n, 8));
2398 com_backpatch(c, break_anchor);
2401 /* Code generated for "try: S finally: Sf" is as follows:
2403 SETUP_FINALLY L
2404 <code for S>
2405 POP_BLOCK
2406 LOAD_CONST <nil>
2407 L: <code for Sf>
2408 END_FINALLY
2410 The special instructions use the block stack. Each block
2411 stack entry contains the instruction that created it (here
2412 SETUP_FINALLY), the level of the value stack at the time the
2413 block stack entry was created, and a label (here L).
2415 SETUP_FINALLY:
2416 Pushes the current value stack level and the label
2417 onto the block stack.
2418 POP_BLOCK:
2419 Pops en entry from the block stack, and pops the value
2420 stack until its level is the same as indicated on the
2421 block stack. (The label is ignored.)
2422 END_FINALLY:
2423 Pops a variable number of entries from the *value* stack
2424 and re-raises the exception they specify. The number of
2425 entries popped depends on the (pseudo) exception type.
2427 The block stack is unwound when an exception is raised:
2428 when a SETUP_FINALLY entry is found, the exception is pushed
2429 onto the value stack (and the exception condition is cleared),
2430 and the interpreter jumps to the label gotten from the block
2431 stack.
2433 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2434 (The contents of the value stack is shown in [], with the top
2435 at the right; 'tb' is trace-back info, 'val' the exception's
2436 associated value, and 'exc' the exception.)
2438 Value stack Label Instruction Argument
2439 [] SETUP_EXCEPT L1
2440 [] <code for S>
2441 [] POP_BLOCK
2442 [] JUMP_FORWARD L0
2444 [tb, val, exc] L1: DUP )
2445 [tb, val, exc, exc] <evaluate E1> )
2446 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2447 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2448 [tb, val, exc, 1] POP )
2449 [tb, val, exc] POP
2450 [tb, val] <assign to V1> (or POP if no V1)
2451 [tb] POP
2452 [] <code for S1>
2453 JUMP_FORWARD L0
2455 [tb, val, exc, 0] L2: POP
2456 [tb, val, exc] DUP
2457 .............................etc.......................
2459 [tb, val, exc, 0] Ln+1: POP
2460 [tb, val, exc] END_FINALLY # re-raise exception
2462 [] L0: <next statement>
2464 Of course, parts are not generated if Vi or Ei is not present.
2467 static void
2468 com_try_except(c, n)
2469 struct compiling *c;
2470 node *n;
2472 int except_anchor = 0;
2473 int end_anchor = 0;
2474 int else_anchor = 0;
2475 int i;
2476 node *ch;
2478 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2479 block_push(c, SETUP_EXCEPT);
2480 com_node(c, CHILD(n, 2));
2481 com_addbyte(c, POP_BLOCK);
2482 block_pop(c, SETUP_EXCEPT);
2483 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2484 com_backpatch(c, except_anchor);
2485 for (i = 3;
2486 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2487 i += 3) {
2488 /* except_clause: 'except' [expr [',' var]] */
2489 if (except_anchor == 0) {
2490 com_error(c, PyExc_SyntaxError,
2491 "default 'except:' must be last");
2492 break;
2494 except_anchor = 0;
2495 com_push(c, 3); /* tb, val, exc pushed by exception */
2496 com_addoparg(c, SET_LINENO, ch->n_lineno);
2497 if (NCH(ch) > 1) {
2498 com_addbyte(c, DUP_TOP);
2499 com_push(c, 1);
2500 com_node(c, CHILD(ch, 1));
2501 com_addoparg(c, COMPARE_OP, EXC_MATCH);
2502 com_pop(c, 1);
2503 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
2504 com_addbyte(c, POP_TOP);
2505 com_pop(c, 1);
2507 com_addbyte(c, POP_TOP);
2508 com_pop(c, 1);
2509 if (NCH(ch) > 3)
2510 com_assign(c, CHILD(ch, 3), OP_ASSIGN);
2511 else {
2512 com_addbyte(c, POP_TOP);
2513 com_pop(c, 1);
2515 com_addbyte(c, POP_TOP);
2516 com_pop(c, 1);
2517 com_node(c, CHILD(n, i+2));
2518 com_addfwref(c, JUMP_FORWARD, &end_anchor);
2519 if (except_anchor) {
2520 com_backpatch(c, except_anchor);
2521 /* We come in with [tb, val, exc, 0] on the
2522 stack; one pop and it's the same as
2523 expected at the start of the loop */
2524 com_addbyte(c, POP_TOP);
2527 /* We actually come in here with [tb, val, exc] but the
2528 END_FINALLY will zap those and jump around.
2529 The c_stacklevel does not reflect them so we need not pop
2530 anything. */
2531 com_addbyte(c, END_FINALLY);
2532 com_backpatch(c, else_anchor);
2533 if (i < NCH(n))
2534 com_node(c, CHILD(n, i+2));
2535 com_backpatch(c, end_anchor);
2538 static void
2539 com_try_finally(c, n)
2540 struct compiling *c;
2541 node *n;
2543 int finally_anchor = 0;
2544 node *ch;
2546 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
2547 block_push(c, SETUP_FINALLY);
2548 com_node(c, CHILD(n, 2));
2549 com_addbyte(c, POP_BLOCK);
2550 block_pop(c, SETUP_FINALLY);
2551 block_push(c, END_FINALLY);
2552 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2553 /* While the generated code pushes only one item,
2554 the try-finally handling can enter here with
2555 up to three items. OK, here are the details:
2556 3 for an exception, 2 for RETURN, 1 for BREAK. */
2557 com_push(c, 3);
2558 com_backpatch(c, finally_anchor);
2559 ch = CHILD(n, NCH(n)-1);
2560 com_addoparg(c, SET_LINENO, ch->n_lineno);
2561 com_node(c, ch);
2562 com_addbyte(c, END_FINALLY);
2563 block_pop(c, END_FINALLY);
2564 com_pop(c, 3); /* Matches the com_push above */
2567 static void
2568 com_try_stmt(c, n)
2569 struct compiling *c;
2570 node *n;
2572 REQ(n, try_stmt);
2573 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2574 | 'try' ':' suite 'finally' ':' suite */
2575 if (TYPE(CHILD(n, 3)) != except_clause)
2576 com_try_finally(c, n);
2577 else
2578 com_try_except(c, n);
2581 static node *
2582 get_rawdocstring(n)
2583 node *n;
2585 int i;
2587 /* Label to avoid tail recursion */
2588 next:
2589 switch (TYPE(n)) {
2591 case suite:
2592 if (NCH(n) == 1) {
2593 n = CHILD(n, 0);
2594 goto next;
2596 /* Fall through */
2597 case file_input:
2598 for (i = 0; i < NCH(n); i++) {
2599 node *ch = CHILD(n, i);
2600 if (TYPE(ch) == stmt) {
2601 n = ch;
2602 goto next;
2605 break;
2607 case stmt:
2608 case simple_stmt:
2609 case small_stmt:
2610 n = CHILD(n, 0);
2611 goto next;
2613 case expr_stmt:
2614 case testlist:
2615 case test:
2616 case and_test:
2617 case not_test:
2618 case comparison:
2619 case expr:
2620 case xor_expr:
2621 case and_expr:
2622 case shift_expr:
2623 case arith_expr:
2624 case term:
2625 case factor:
2626 case power:
2627 if (NCH(n) == 1) {
2628 n = CHILD(n, 0);
2629 goto next;
2631 break;
2633 case atom:
2634 if (TYPE(CHILD(n, 0)) == STRING)
2635 return n;
2636 break;
2639 return NULL;
2642 static PyObject *
2643 get_docstring(n)
2644 node *n;
2646 n = get_rawdocstring(n);
2647 if (n == NULL)
2648 return NULL;
2649 return parsestrplus(n);
2652 static void
2653 com_suite(c, n)
2654 struct compiling *c;
2655 node *n;
2657 REQ(n, suite);
2658 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2659 if (NCH(n) == 1) {
2660 com_node(c, CHILD(n, 0));
2662 else {
2663 int i;
2664 for (i = 0; i < NCH(n); i++) {
2665 node *ch = CHILD(n, i);
2666 if (TYPE(ch) == stmt)
2667 com_node(c, ch);
2672 /* ARGSUSED */
2673 static void
2674 com_continue_stmt(c, n)
2675 struct compiling *c;
2676 node *n; /* Not used, but passed for consistency */
2678 int i = c->c_nblocks;
2679 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
2680 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2682 else {
2683 com_error(c, PyExc_SyntaxError,
2684 "'continue' not properly in loop");
2686 /* XXX Could allow it inside a 'finally' clause
2687 XXX if we could pop the exception still on the stack */
2690 static int
2691 com_argdefs(c, n)
2692 struct compiling *c;
2693 node *n;
2695 int i, nch, nargs, ndefs;
2696 if (TYPE(n) == lambdef) {
2697 /* lambdef: 'lambda' [varargslist] ':' test */
2698 n = CHILD(n, 1);
2700 else {
2701 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
2702 n = CHILD(n, 2);
2703 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
2704 n = CHILD(n, 1);
2706 if (TYPE(n) != varargslist)
2707 return 0;
2708 /* varargslist:
2709 (fpdef ['=' test] ',')* '*' ....... |
2710 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2711 nch = NCH(n);
2712 nargs = 0;
2713 ndefs = 0;
2714 for (i = 0; i < nch; i++) {
2715 int t;
2716 if (TYPE(CHILD(n, i)) == STAR ||
2717 TYPE(CHILD(n, i)) == DOUBLESTAR)
2718 break;
2719 nargs++;
2720 i++;
2721 if (i >= nch)
2722 t = RPAR; /* Anything except EQUAL or COMMA */
2723 else
2724 t = TYPE(CHILD(n, i));
2725 if (t == EQUAL) {
2726 i++;
2727 ndefs++;
2728 com_node(c, CHILD(n, i));
2729 i++;
2730 if (i >= nch)
2731 break;
2732 t = TYPE(CHILD(n, i));
2734 else {
2735 /* Treat "(a=1, b)" as an error */
2736 if (ndefs)
2737 com_error(c, PyExc_SyntaxError,
2738 "non-default argument follows default argument");
2740 if (t != COMMA)
2741 break;
2743 return ndefs;
2746 static void
2747 com_funcdef(c, n)
2748 struct compiling *c;
2749 node *n;
2751 PyObject *v;
2752 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
2753 v = (PyObject *)icompile(n, c);
2754 if (v == NULL)
2755 c->c_errors++;
2756 else {
2757 int i = com_addconst(c, v);
2758 int ndefs = com_argdefs(c, n);
2759 com_addoparg(c, LOAD_CONST, i);
2760 com_push(c, 1);
2761 com_addoparg(c, MAKE_FUNCTION, ndefs);
2762 com_pop(c, ndefs);
2763 com_addopname(c, STORE_NAME, CHILD(n, 1));
2764 com_pop(c, 1);
2765 Py_DECREF(v);
2769 static void
2770 com_bases(c, n)
2771 struct compiling *c;
2772 node *n;
2774 int i;
2775 REQ(n, testlist);
2776 /* testlist: test (',' test)* [','] */
2777 for (i = 0; i < NCH(n); i += 2)
2778 com_node(c, CHILD(n, i));
2779 i = (NCH(n)+1) / 2;
2780 com_addoparg(c, BUILD_TUPLE, i);
2781 com_pop(c, i-1);
2784 static void
2785 com_classdef(c, n)
2786 struct compiling *c;
2787 node *n;
2789 int i;
2790 PyObject *v;
2791 REQ(n, classdef);
2792 /* classdef: class NAME ['(' testlist ')'] ':' suite */
2793 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
2794 c->c_errors++;
2795 return;
2797 /* Push the class name on the stack */
2798 i = com_addconst(c, v);
2799 com_addoparg(c, LOAD_CONST, i);
2800 com_push(c, 1);
2801 Py_DECREF(v);
2802 /* Push the tuple of base classes on the stack */
2803 if (TYPE(CHILD(n, 2)) != LPAR) {
2804 com_addoparg(c, BUILD_TUPLE, 0);
2805 com_push(c, 1);
2807 else
2808 com_bases(c, CHILD(n, 3));
2809 v = (PyObject *)icompile(n, c);
2810 if (v == NULL)
2811 c->c_errors++;
2812 else {
2813 i = com_addconst(c, v);
2814 com_addoparg(c, LOAD_CONST, i);
2815 com_push(c, 1);
2816 com_addoparg(c, MAKE_FUNCTION, 0);
2817 com_addoparg(c, CALL_FUNCTION, 0);
2818 com_addbyte(c, BUILD_CLASS);
2819 com_pop(c, 2);
2820 com_addopname(c, STORE_NAME, CHILD(n, 1));
2821 Py_DECREF(v);
2825 static void
2826 com_node(c, n)
2827 struct compiling *c;
2828 node *n;
2830 switch (TYPE(n)) {
2832 /* Definition nodes */
2834 case funcdef:
2835 com_funcdef(c, n);
2836 break;
2837 case classdef:
2838 com_classdef(c, n);
2839 break;
2841 /* Trivial parse tree nodes */
2843 case stmt:
2844 case small_stmt:
2845 case flow_stmt:
2846 com_node(c, CHILD(n, 0));
2847 break;
2849 case simple_stmt:
2850 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2851 com_addoparg(c, SET_LINENO, n->n_lineno);
2853 int i;
2854 for (i = 0; i < NCH(n)-1; i += 2)
2855 com_node(c, CHILD(n, i));
2857 break;
2859 case compound_stmt:
2860 com_addoparg(c, SET_LINENO, n->n_lineno);
2861 com_node(c, CHILD(n, 0));
2862 break;
2864 /* Statement nodes */
2866 case expr_stmt:
2867 com_expr_stmt(c, n);
2868 break;
2869 case print_stmt:
2870 com_print_stmt(c, n);
2871 break;
2872 case del_stmt: /* 'del' exprlist */
2873 com_assign(c, CHILD(n, 1), OP_DELETE);
2874 break;
2875 case pass_stmt:
2876 break;
2877 case break_stmt:
2878 if (c->c_loops == 0) {
2879 com_error(c, PyExc_SyntaxError,
2880 "'break' outside loop");
2882 com_addbyte(c, BREAK_LOOP);
2883 break;
2884 case continue_stmt:
2885 com_continue_stmt(c, n);
2886 break;
2887 case return_stmt:
2888 com_return_stmt(c, n);
2889 break;
2890 case raise_stmt:
2891 com_raise_stmt(c, n);
2892 break;
2893 case import_stmt:
2894 com_import_stmt(c, n);
2895 break;
2896 case global_stmt:
2897 com_global_stmt(c, n);
2898 break;
2899 case exec_stmt:
2900 com_exec_stmt(c, n);
2901 break;
2902 case assert_stmt:
2903 com_assert_stmt(c, n);
2904 break;
2905 case if_stmt:
2906 com_if_stmt(c, n);
2907 break;
2908 case while_stmt:
2909 com_while_stmt(c, n);
2910 break;
2911 case for_stmt:
2912 com_for_stmt(c, n);
2913 break;
2914 case try_stmt:
2915 com_try_stmt(c, n);
2916 break;
2917 case suite:
2918 com_suite(c, n);
2919 break;
2921 /* Expression nodes */
2923 case testlist:
2924 com_list(c, n, 0);
2925 break;
2926 case test:
2927 com_test(c, n);
2928 break;
2929 case and_test:
2930 com_and_test(c, n);
2931 break;
2932 case not_test:
2933 com_not_test(c, n);
2934 break;
2935 case comparison:
2936 com_comparison(c, n);
2937 break;
2938 case exprlist:
2939 com_list(c, n, 0);
2940 break;
2941 case expr:
2942 com_expr(c, n);
2943 break;
2944 case xor_expr:
2945 com_xor_expr(c, n);
2946 break;
2947 case and_expr:
2948 com_and_expr(c, n);
2949 break;
2950 case shift_expr:
2951 com_shift_expr(c, n);
2952 break;
2953 case arith_expr:
2954 com_arith_expr(c, n);
2955 break;
2956 case term:
2957 com_term(c, n);
2958 break;
2959 case factor:
2960 com_factor(c, n);
2961 break;
2962 case power:
2963 com_power(c, n);
2964 break;
2965 case atom:
2966 com_atom(c, n);
2967 break;
2969 default:
2970 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
2971 com_error(c, PyExc_SystemError,
2972 "com_node: unexpected node type");
2976 static void com_fplist Py_PROTO((struct compiling *, node *));
2978 static void
2979 com_fpdef(c, n)
2980 struct compiling *c;
2981 node *n;
2983 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
2984 if (TYPE(CHILD(n, 0)) == LPAR)
2985 com_fplist(c, CHILD(n, 1));
2986 else {
2987 com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
2988 com_pop(c, 1);
2992 static void
2993 com_fplist(c, n)
2994 struct compiling *c;
2995 node *n;
2997 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
2998 if (NCH(n) == 1) {
2999 com_fpdef(c, CHILD(n, 0));
3001 else {
3002 int i = (NCH(n)+1)/2;
3003 com_addoparg(c, UNPACK_TUPLE, i);
3004 com_push(c, i-1);
3005 for (i = 0; i < NCH(n); i += 2)
3006 com_fpdef(c, CHILD(n, i));
3010 static void
3011 com_arglist(c, n)
3012 struct compiling *c;
3013 node *n;
3015 int nch, i;
3016 int complex = 0;
3017 char nbuf[10];
3018 REQ(n, varargslist);
3019 /* varargslist:
3020 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3021 nch = NCH(n);
3022 /* Enter all arguments in table of locals */
3023 for (i = 0; i < nch; i++) {
3024 node *ch = CHILD(n, i);
3025 node *fp;
3026 char *name;
3027 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3028 break;
3029 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3030 fp = CHILD(ch, 0);
3031 if (TYPE(fp) == NAME)
3032 name = STR(fp);
3033 else {
3034 name = nbuf;
3035 sprintf(nbuf, ".%d", i);
3036 complex = 1;
3038 com_newlocal(c, name);
3039 c->c_argcount++;
3040 if (++i >= nch)
3041 break;
3042 ch = CHILD(n, i);
3043 if (TYPE(ch) == EQUAL)
3044 i += 2;
3045 else
3046 REQ(ch, COMMA);
3048 /* Handle *arguments */
3049 if (i < nch) {
3050 node *ch;
3051 ch = CHILD(n, i);
3052 if (TYPE(ch) != DOUBLESTAR) {
3053 REQ(ch, STAR);
3054 ch = CHILD(n, i+1);
3055 if (TYPE(ch) == NAME) {
3056 c->c_flags |= CO_VARARGS;
3057 i += 3;
3058 com_newlocal(c, STR(ch));
3062 /* Handle **keywords */
3063 if (i < nch) {
3064 node *ch;
3065 ch = CHILD(n, i);
3066 if (TYPE(ch) != DOUBLESTAR) {
3067 REQ(ch, STAR);
3068 ch = CHILD(n, i+1);
3069 REQ(ch, STAR);
3070 ch = CHILD(n, i+2);
3072 else
3073 ch = CHILD(n, i+1);
3074 REQ(ch, NAME);
3075 c->c_flags |= CO_VARKEYWORDS;
3076 com_newlocal(c, STR(ch));
3078 if (complex) {
3079 /* Generate code for complex arguments only after
3080 having counted the simple arguments */
3081 int ilocal = 0;
3082 for (i = 0; i < nch; i++) {
3083 node *ch = CHILD(n, i);
3084 node *fp;
3085 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3086 break;
3087 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3088 fp = CHILD(ch, 0);
3089 if (TYPE(fp) != NAME) {
3090 com_addoparg(c, LOAD_FAST, ilocal);
3091 com_push(c, 1);
3092 com_fpdef(c, ch);
3094 ilocal++;
3095 if (++i >= nch)
3096 break;
3097 ch = CHILD(n, i);
3098 if (TYPE(ch) == EQUAL)
3099 i += 2;
3100 else
3101 REQ(ch, COMMA);
3106 static void
3107 com_file_input(c, n)
3108 struct compiling *c;
3109 node *n;
3111 int i;
3112 PyObject *doc;
3113 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
3114 doc = get_docstring(n);
3115 if (doc != NULL) {
3116 int i = com_addconst(c, doc);
3117 Py_DECREF(doc);
3118 com_addoparg(c, LOAD_CONST, i);
3119 com_push(c, 1);
3120 com_addopnamestr(c, STORE_NAME, "__doc__");
3121 com_pop(c, 1);
3123 for (i = 0; i < NCH(n); i++) {
3124 node *ch = CHILD(n, i);
3125 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3126 com_node(c, ch);
3130 /* Top-level compile-node interface */
3132 static void
3133 compile_funcdef(c, n)
3134 struct compiling *c;
3135 node *n;
3137 PyObject *doc;
3138 node *ch;
3139 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3140 c->c_name = STR(CHILD(n, 1));
3141 doc = get_docstring(CHILD(n, 4));
3142 if (doc != NULL) {
3143 (void) com_addconst(c, doc);
3144 Py_DECREF(doc);
3146 else
3147 (void) com_addconst(c, Py_None); /* No docstring */
3148 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3149 ch = CHILD(ch, 1); /* ')' | varargslist */
3150 if (TYPE(ch) == varargslist)
3151 com_arglist(c, ch);
3152 c->c_infunction = 1;
3153 com_node(c, CHILD(n, 4));
3154 c->c_infunction = 0;
3155 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3156 com_push(c, 1);
3157 com_addbyte(c, RETURN_VALUE);
3158 com_pop(c, 1);
3161 static void
3162 compile_lambdef(c, n)
3163 struct compiling *c;
3164 node *n;
3166 node *ch;
3167 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
3168 c->c_name = "<lambda>";
3170 ch = CHILD(n, 1);
3171 (void) com_addconst(c, Py_None); /* No docstring */
3172 if (TYPE(ch) == varargslist) {
3173 com_arglist(c, ch);
3174 ch = CHILD(n, 3);
3176 else
3177 ch = CHILD(n, 2);
3178 com_node(c, ch);
3179 com_addbyte(c, RETURN_VALUE);
3180 com_pop(c, 1);
3183 static void
3184 compile_classdef(c, n)
3185 struct compiling *c;
3186 node *n;
3188 node *ch;
3189 PyObject *doc;
3190 REQ(n, classdef);
3191 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3192 c->c_name = STR(CHILD(n, 1));
3193 #ifdef PRIVATE_NAME_MANGLING
3194 c->c_private = c->c_name;
3195 #endif
3196 ch = CHILD(n, NCH(n)-1); /* The suite */
3197 doc = get_docstring(ch);
3198 if (doc != NULL) {
3199 int i = com_addconst(c, doc);
3200 Py_DECREF(doc);
3201 com_addoparg(c, LOAD_CONST, i);
3202 com_push(c, 1);
3203 com_addopnamestr(c, STORE_NAME, "__doc__");
3204 com_pop(c, 1);
3206 else
3207 (void) com_addconst(c, Py_None);
3208 com_node(c, ch);
3209 com_addbyte(c, LOAD_LOCALS);
3210 com_push(c, 1);
3211 com_addbyte(c, RETURN_VALUE);
3212 com_pop(c, 1);
3215 static void
3216 compile_node(c, n)
3217 struct compiling *c;
3218 node *n;
3220 com_addoparg(c, SET_LINENO, n->n_lineno);
3222 switch (TYPE(n)) {
3224 case single_input: /* One interactive command */
3225 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
3226 c->c_interactive++;
3227 n = CHILD(n, 0);
3228 if (TYPE(n) != NEWLINE)
3229 com_node(c, n);
3230 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3231 com_push(c, 1);
3232 com_addbyte(c, RETURN_VALUE);
3233 com_pop(c, 1);
3234 c->c_interactive--;
3235 break;
3237 case file_input: /* A whole file, or built-in function exec() */
3238 com_file_input(c, n);
3239 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3240 com_push(c, 1);
3241 com_addbyte(c, RETURN_VALUE);
3242 com_pop(c, 1);
3243 break;
3245 case eval_input: /* Built-in function input() */
3246 com_node(c, CHILD(n, 0));
3247 com_addbyte(c, RETURN_VALUE);
3248 com_pop(c, 1);
3249 break;
3251 case lambdef: /* anonymous function definition */
3252 compile_lambdef(c, n);
3253 break;
3255 case funcdef: /* A function definition */
3256 compile_funcdef(c, n);
3257 break;
3259 case classdef: /* A class definition */
3260 compile_classdef(c, n);
3261 break;
3263 default:
3264 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
3265 com_error(c, PyExc_SystemError,
3266 "compile_node: unexpected node type");
3270 /* Optimization for local variables in functions (and *only* functions).
3272 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
3273 instructions that refer to local variables with LOAD_FAST etc.
3274 The latter instructions are much faster because they don't need to
3275 look up the variable name in a dictionary.
3277 To find all local variables, we check all STORE_NAME, IMPORT_FROM
3278 and DELETE_NAME instructions. This yields all local variables,
3279 function definitions, class definitions and import statements.
3280 Argument names have already been entered into the list by the
3281 special processing for the argument list.
3283 All remaining LOAD_NAME instructions must refer to non-local (global
3284 or builtin) variables, so are replaced by LOAD_GLOBAL.
3286 There are two problems: 'from foo import *' and 'exec' may introduce
3287 local variables that we can't know while compiling. If this is the
3288 case, we can still optimize bona fide locals (since those
3289 statements will be surrounded by fast_2_locals() and
3290 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
3292 NB: this modifies the string object c->c_code! */
3294 static void
3295 optimize(c)
3296 struct compiling *c;
3298 unsigned char *next_instr, *cur_instr;
3299 int opcode;
3300 int oparg = 0;
3301 PyObject *name;
3302 PyObject *error_type, *error_value, *error_traceback;
3304 #define NEXTOP() (*next_instr++)
3305 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
3306 #define GETITEM(v, i) (PyList_GetItem((v), (i)))
3307 #define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
3309 PyErr_Fetch(&error_type, &error_value, &error_traceback);
3311 c->c_flags |= CO_OPTIMIZED;
3313 next_instr = (unsigned char *) PyString_AsString(c->c_code);
3314 for (;;) {
3315 opcode = NEXTOP();
3316 if (opcode == STOP_CODE)
3317 break;
3318 if (HAS_ARG(opcode))
3319 oparg = NEXTARG();
3320 switch (opcode) {
3321 case STORE_NAME:
3322 case DELETE_NAME:
3323 case IMPORT_FROM:
3324 com_addlocal_o(c, GETNAMEOBJ(oparg));
3325 break;
3326 case EXEC_STMT:
3327 c->c_flags &= ~CO_OPTIMIZED;
3328 break;
3332 if (PyDict_GetItemString(c->c_locals, "*") != NULL)
3333 c->c_flags &= ~CO_OPTIMIZED;
3335 next_instr = (unsigned char *) PyString_AsString(c->c_code);
3336 for (;;) {
3337 cur_instr = next_instr;
3338 opcode = NEXTOP();
3339 if (opcode == STOP_CODE)
3340 break;
3341 if (HAS_ARG(opcode))
3342 oparg = NEXTARG();
3343 if (opcode == LOAD_NAME ||
3344 opcode == STORE_NAME ||
3345 opcode == DELETE_NAME) {
3346 PyObject *v;
3347 int i;
3348 name = GETNAMEOBJ(oparg);
3349 v = PyDict_GetItem(c->c_locals, name);
3350 if (v == NULL) {
3351 if (opcode == LOAD_NAME &&
3352 (c->c_flags&CO_OPTIMIZED))
3353 cur_instr[0] = LOAD_GLOBAL;
3354 continue;
3356 i = PyInt_AsLong(v);
3357 switch (opcode) {
3358 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
3359 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
3360 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
3362 cur_instr[1] = i & 0xff;
3363 cur_instr[2] = (i>>8) & 0xff;
3367 if (c->c_errors == 0)
3368 PyErr_Restore(error_type, error_value, error_traceback);
3371 PyCodeObject *
3372 PyNode_Compile(n, filename)
3373 node *n;
3374 char *filename;
3376 return jcompile(n, filename, NULL);
3379 static PyCodeObject *
3380 icompile(n, base)
3381 node *n;
3382 struct compiling *base;
3384 return jcompile(n, base->c_filename, base);
3387 static PyCodeObject *
3388 jcompile(n, filename, base)
3389 node *n;
3390 char *filename;
3391 struct compiling *base;
3393 struct compiling sc;
3394 PyCodeObject *co;
3395 if (!com_init(&sc, filename))
3396 return NULL;
3397 #ifdef PRIVATE_NAME_MANGLING
3398 if (base)
3399 sc.c_private = base->c_private;
3400 else
3401 sc.c_private = NULL;
3402 #endif
3403 compile_node(&sc, n);
3404 com_done(&sc);
3405 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
3406 optimize(&sc);
3407 sc.c_flags |= CO_NEWLOCALS;
3409 else if (TYPE(n) == classdef)
3410 sc.c_flags |= CO_NEWLOCALS;
3411 co = NULL;
3412 if (sc.c_errors == 0) {
3413 PyObject *consts, *names, *varnames, *filename, *name;
3414 consts = PyList_AsTuple(sc.c_consts);
3415 names = PyList_AsTuple(sc.c_names);
3416 varnames = PyList_AsTuple(sc.c_varnames);
3417 filename = PyString_InternFromString(sc.c_filename);
3418 name = PyString_InternFromString(sc.c_name);
3419 if (!PyErr_Occurred())
3420 co = PyCode_New(sc.c_argcount,
3421 sc.c_nlocals,
3422 sc.c_maxstacklevel,
3423 sc.c_flags,
3424 sc.c_code,
3425 consts,
3426 names,
3427 varnames,
3428 filename,
3429 name,
3430 sc.c_firstlineno,
3431 sc.c_lnotab);
3432 Py_XDECREF(consts);
3433 Py_XDECREF(names);
3434 Py_XDECREF(varnames);
3435 Py_XDECREF(filename);
3436 Py_XDECREF(name);
3438 com_free(&sc);
3439 return co;
3443 PyCode_Addr2Line(co, addrq)
3444 PyCodeObject *co;
3445 int addrq;
3447 int size = PyString_Size(co->co_lnotab) / 2;
3448 char *p = PyString_AsString(co->co_lnotab);
3449 int line = co->co_firstlineno;
3450 int addr = 0;
3451 while (--size >= 0) {
3452 addr += *p++;
3453 if (addr > addrq)
3454 break;
3455 line += *p++;
3457 return line;