1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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
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 */
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
43 #ifndef NO_PRIVATE_NAME_MANGLING
44 #define PRIVATE_NAME_MANGLING
54 #include "structmember.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"
65 #if Py_file_input != file_input
66 #error "file_input has changed -- update Py_file_input in Python.h"
68 #if Py_eval_input != eval_input
69 #error "eval_input has changed -- update Py_eval_input in Python.h"
72 int Py_OptimizeFlag
= 0;
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
},
97 code_getattr(co
, name
)
101 return PyMember_Get((char *)co
, code_memberlist
, name
);
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
);
125 char *filename
= "???";
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
);
142 PyCodeObject
*co
, *cp
;
145 cmp
= co
->co_argcount
- cp
->co_argcount
;
147 cmp
= co
->co_nlocals
- cp
->co_nlocals
;
149 cmp
= co
->co_flags
- cp
->co_flags
;
151 cmp
= PyObject_Compare(co
->co_code
, cp
->co_code
);
153 cmp
= PyObject_Compare(co
->co_consts
, cp
->co_consts
);
155 cmp
= PyObject_Compare(co
->co_names
, cp
->co_names
);
157 cmp
= PyObject_Compare(co
->co_varnames
, cp
->co_varnames
);
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
;
180 PyTypeObject PyCode_Type
= {
181 PyObject_HEAD_INIT(&PyType_Type
)
184 sizeof(PyCodeObject
),
186 (destructor
)code_dealloc
, /*tp_dealloc*/
188 (getattrfunc
)code_getattr
, /*tp_getattr*/
190 (cmpfunc
)code_compare
, /*tp_compare*/
191 (reprfunc
)code_repr
, /*tp_repr*/
193 0, /*tp_as_sequence*/
195 (hashfunc
)code_hash
, /*tp_hash*/
199 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
202 PyCode_New(argcount
, nlocals
, stacksize
, flags
,
203 code
, consts
, names
, varnames
, filename
, name
,
221 /* Check argument types */
222 if (argcount
< 0 || nlocals
< 0 ||
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();
233 pb
= code
->ob_type
->tp_as_buffer
;
235 pb
->bf_getreadbuffer
== NULL
||
236 pb
->bf_getsegcount
== NULL
||
237 (*pb
->bf_getsegcount
)(code
, NULL
) != 1)
239 PyErr_BadInternalCall();
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();
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();
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
);
263 if (!PyString_Check(v
))
265 p
= PyString_AsString(v
);
266 if ((int)strspn(p
, NAME_CHARS
)
269 PyString_InternInPlace(&PyTuple_GET_ITEM(consts
, i
));
271 co
= PyObject_NEW(PyCodeObject
, &PyCode_Type
);
273 co
->co_argcount
= argcount
;
274 co
->co_nlocals
= nlocals
;
275 co
->co_stacksize
= stacksize
;
276 co
->co_flags
= flags
;
280 co
->co_consts
= consts
;
282 co
->co_names
= names
;
284 co
->co_varnames
= varnames
;
286 co
->co_filename
= filename
;
289 co
->co_firstlineno
= firstlineno
;
291 co
->co_lnotab
= lnotab
;
297 /* Data structure used internally */
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 */
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 */
331 /* Error message including line number */
334 com_error(c
, exc
, msg
)
344 if (c
->c_lineno
<= 1) {
345 /* Unknown line number or single interactive command */
346 PyErr_SetString(exc
, msg
);
349 sprintf(buffer
, " (line %d)", c
->c_lineno
);
350 v
= PyString_FromStringAndSize((char *)NULL
, n
+ strlen(buffer
));
352 return; /* MemoryError, too bad */
353 s
= PyString_AS_STRING((PyStringObject
*)v
);
356 PyErr_SetObject(exc
, v
);
361 /* Interface to the block stack */
368 if (c
->c_nblocks
>= CO_MAXBLOCKS
) {
369 com_error(c
, PyExc_SystemError
,
370 "too many statically nested blocks");
373 c
->c_block
[c
->c_nblocks
++] = type
;
382 if (c
->c_nblocks
> 0)
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 *));
418 com_init(c
, filename
)
422 if ((c
->c_code
= PyString_FromStringAndSize((char *)NULL
,
425 if ((c
->c_consts
= PyList_New(0)) == NULL
)
427 if ((c
->c_names
= PyList_New(0)) == NULL
)
429 if ((c
->c_globals
= PyDict_New()) == NULL
)
431 if ((c
->c_locals
= PyDict_New()) == NULL
)
433 if ((c
->c_varnames
= PyList_New(0)) == NULL
)
435 if ((c
->c_lnotab
= PyString_FromStringAndSize((char *)NULL
,
444 c
->c_interactive
= 0;
448 c
->c_filename
= filename
;
452 c
->c_maxstacklevel
= 0;
453 c
->c_firstlineno
= 0;
456 c
-> c_lnotab_next
= 0;
460 Py_DECREF(c
->c_lnotab
);
462 Py_DECREF(c
->c_locals
);
464 Py_DECREF(c
->c_globals
);
466 Py_DECREF(c
->c_names
);
468 Py_DECREF(c
->c_consts
);
470 Py_DECREF(c
->c_code
);
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
);
493 c
->c_stacklevel
+= n
;
494 if (c
->c_stacklevel
> c
->c_maxstacklevel
)
495 c
->c_maxstacklevel
= c
->c_stacklevel
;
503 if (c
->c_stacklevel
< n
) {
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); */
511 c
->c_stacklevel
-= n
;
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
);
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
)
541 len
= PyString_Size(c
->c_code
);
542 if (c
->c_nexti
>= len
) {
543 if (_PyString_Resize(&c
->c_code
, len
+1000) != 0) {
548 PyString_AsString(c
->c_code
)[c
->c_nexti
++] = byte
;
556 com_addbyte(c
, x
& 0xff);
557 com_addbyte(c
, x
>> 8); /* XXX x should be positive */
561 com_add_lnotab(c
, addr
, line
)
568 if (c
->c_lnotab
== NULL
)
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) {
577 p
= PyString_AsString(c
->c_lnotab
) + c
->c_lnotab_next
;
580 c
->c_lnotab_next
+= 2;
584 com_set_lineno(c
, lineno
)
588 c
->c_lineno
= lineno
;
589 if (c
->c_firstlineno
== 0) {
590 c
->c_firstlineno
= c
->c_last_line
= lineno
;
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)
600 if (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
;
612 com_addoparg(c
, op
, arg
)
617 if (op
== SET_LINENO
) {
618 com_set_lineno(c
, arg
);
627 com_addfwref(c
, op
, p_anchor
)
632 /* Compile a forward reference for backpatching */
639 com_addint(c
, anchor
== 0 ? 0 : here
- anchor
);
643 com_backpatch(c
, anchor
)
645 int anchor
; /* Must be nonzero */
647 unsigned char *code
= (unsigned char *) PyString_AsString(c
->c_code
);
648 int target
= c
->c_nexti
;
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;
663 /* Handle literals and names uniformly */
671 int n
= PyList_Size(list
);
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)
680 /* Check for error from PyObject_Compare */
681 if (PyErr_Occurred() || PyList_Append(list
, v
) != 0)
691 return com_add(c
, c
->c_consts
, v
);
699 return com_add(c
, c
->c_names
, v
);
702 #ifdef PRIVATE_NAME_MANGLING
704 com_mangle(c
, name
, buffer
, maxlen
)
710 /* Name mangling: __private becomes _classname__private.
711 This is independent from how the name is used. */
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__ */
720 /* Strip leading underscores from class name */
724 return 0; /* Don't mangle if class is just underscores */
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 */
730 strncpy(buffer
+1, p
, plen
);
731 strcpy(buffer
+1+plen
, name
);
732 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
738 com_addopnamestr(c
, op
, name
)
745 #ifdef PRIVATE_NAME_MANGLING
747 if (name
!= NULL
&& name
[0] == '_' && name
[1] == '_' &&
748 c
->c_private
!= NULL
&&
749 com_mangle(c
, name
, buffer
, (int)sizeof(buffer
)))
752 if (name
== NULL
|| (v
= PyString_InternFromString(name
)) == NULL
) {
757 i
= com_addname(c
, v
);
760 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
765 if (PyDict_GetItemString(c
->c_globals
, name
) != NULL
) {
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
);
777 com_addopname(c
, op
, n
)
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 */
789 else if (TYPE(n
) == dotted_name
) {
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");
811 com_addopnamestr(c
, op
, name
);
816 struct compiling
*co
;
819 extern double atof
Py_PROTO((const char *));
823 #ifndef WITHOUT_COMPLEX
829 end
= s
+ strlen(s
) - 1;
830 #ifndef WITHOUT_COMPLEX
831 imflag
= *end
== 'j' || *end
== 'J';
833 if (*end
== 'l' || *end
== 'L')
834 return PyLong_FromString(s
, (char **)0, 0);
836 x
= (long) PyOS_strtoul(s
, &end
, 0);
838 x
= PyOS_strtol(s
, &end
, 0);
841 com_error(co
, PyExc_OverflowError
,
842 "integer literal too large");
845 return PyInt_FromLong(x
);
847 /* XXX Huge floats may silently fail */
848 #ifndef WITHOUT_COMPLEX
851 PyFPE_START_PROTECT("atof", return 0)
854 return PyComplex_FromCComplex(c
);
859 PyFPE_START_PROTECT("atof", return 0)
861 PyFPE_END_PROTECT(dx
)
862 return PyFloat_FromDouble(dx
);
878 if (isalpha(quote
) || quote
== '_')
880 if (quote
!= '\'' && quote
!= '\"') {
881 PyErr_BadInternalCall();
886 if (s
[--len
] != quote
) {
887 PyErr_BadInternalCall();
890 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
893 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
894 PyErr_BadInternalCall();
898 if (first
!= quote
|| strchr(s
, '\\') == NULL
)
899 return PyString_FromStringAndSize(s
, len
);
900 v
= PyString_FromStringAndSize((char *)NULL
, len
);
901 p
= buf
= PyString_AsString(v
);
910 /* XXX This assumes ASCII! */
912 case '\\': *p
++ = '\\'; break;
913 case '\'': *p
++ = '\''; break;
914 case '\"': *p
++ = '\"'; break;
915 case 'b': *p
++ = '\b'; break;
916 case 'f': *p
++ = '\014'; break; /* FF */
917 case 't': *p
++ = '\t'; break;
918 case 'n': *p
++ = '\n'; break;
919 case 'r': *p
++ = '\r'; break;
920 case 'v': *p
++ = '\013'; break; /* VT */
921 case 'a': *p
++ = '\007'; break; /* BEL, not classic C */
922 case '0': case '1': case '2': case '3':
923 case '4': case '5': case '6': case '7':
925 if ('0' <= *s
&& *s
<= '7') {
926 c
= (c
<<3) + *s
++ - '0';
927 if ('0' <= *s
&& *s
<= '7')
928 c
= (c
<<3) + *s
++ - '0';
933 if (isxdigit(Py_CHARMASK(*s
))) {
945 } while (isxdigit(Py_CHARMASK(*s
)));
950 default: *p
++ = '\\'; *p
++ = s
[-1]; break;
953 _PyString_Resize(&v
, (int)(p
- buf
));
963 REQ(CHILD(n
, 0), STRING
);
964 if ((v
= parsestr(STR(CHILD(n
, 0)))) != NULL
) {
965 /* String literal concatenation */
966 for (i
= 1; i
< NCH(n
) && v
!= NULL
; i
++) {
967 PyString_ConcatAndDel(&v
, parsestr(STR(CHILD(n
, i
))));
974 com_list_constructor(c
, n
)
980 if (TYPE(n
) != testlist
)
982 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
983 len
= (NCH(n
) + 1) / 2;
984 for (i
= 0; i
< NCH(n
); i
+= 2)
985 com_node(c
, CHILD(n
, i
));
986 com_addoparg(c
, BUILD_LIST
, len
);
996 /* dictmaker: test ':' test (',' test ':' value)* [','] */
997 for (i
= 0; i
+2 < NCH(n
); i
+= 4) {
998 /* We must arrange things just right for STORE_SUBSCR.
999 It wants the stack to look like (value) (dict) (key) */
1000 com_addbyte(c
, DUP_TOP
);
1002 com_node(c
, CHILD(n
, i
+2)); /* value */
1003 com_addbyte(c
, ROT_TWO
);
1004 com_node(c
, CHILD(n
, i
)); /* key */
1005 com_addbyte(c
, STORE_SUBSCR
);
1012 struct compiling
*c
;
1022 if (TYPE(CHILD(n
, 1)) == RPAR
) {
1023 com_addoparg(c
, BUILD_TUPLE
, 0);
1027 com_node(c
, CHILD(n
, 1));
1030 if (TYPE(CHILD(n
, 1)) == RSQB
) {
1031 com_addoparg(c
, BUILD_LIST
, 0);
1035 com_list_constructor(c
, CHILD(n
, 1));
1037 case LBRACE
: /* '{' [dictmaker] '}' */
1038 com_addoparg(c
, BUILD_MAP
, 0);
1040 if (TYPE(CHILD(n
, 1)) != RBRACE
)
1041 com_dictmaker(c
, CHILD(n
, 1));
1044 com_node(c
, CHILD(n
, 1));
1045 com_addbyte(c
, UNARY_CONVERT
);
1048 if ((v
= parsenumber(c
, STR(ch
))) == NULL
) {
1052 i
= com_addconst(c
, v
);
1055 com_addoparg(c
, LOAD_CONST
, i
);
1059 v
= parsestrplus(n
);
1065 i
= com_addconst(c
, v
);
1068 com_addoparg(c
, LOAD_CONST
, i
);
1072 com_addopname(c
, LOAD_NAME
, ch
);
1076 /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
1077 com_error(c
, PyExc_SystemError
,
1078 "com_atom: unexpected node type");
1084 struct compiling
*c
;
1091 else if (NCH(n
) == 2) {
1092 if (TYPE(CHILD(n
, 0)) != COLON
) {
1093 com_node(c
, CHILD(n
, 0));
1094 com_addbyte(c
, op
+1);
1097 com_node(c
, CHILD(n
, 1));
1098 com_addbyte(c
, op
+2);
1103 com_node(c
, CHILD(n
, 0));
1104 com_node(c
, CHILD(n
, 2));
1105 com_addbyte(c
, op
+3);
1111 com_argument(c
, n
, pkeywords
)
1112 struct compiling
*c
;
1113 node
*n
; /* argument */
1114 PyObject
**pkeywords
;
1117 REQ(n
, argument
); /* [test '='] test; really [keyword '='] test */
1119 if (*pkeywords
!= NULL
) {
1120 com_error(c
, PyExc_SyntaxError
,
1121 "non-keyword arg after keyword arg");
1124 com_node(c
, CHILD(n
, 0));
1131 } while (NCH(m
) == 1);
1132 if (TYPE(m
) != NAME
) {
1133 com_error(c
, PyExc_SyntaxError
,
1134 "keyword can't be an expression");
1137 PyObject
*v
= PyString_InternFromString(STR(m
));
1138 if (v
!= NULL
&& *pkeywords
== NULL
)
1139 *pkeywords
= PyDict_New();
1140 if (v
== NULL
|| *pkeywords
== NULL
)
1143 if (PyDict_GetItem(*pkeywords
, v
) != NULL
)
1144 com_error(c
, PyExc_SyntaxError
,
1145 "duplicate keyword argument");
1147 if (PyDict_SetItem(*pkeywords
, v
, v
) != 0)
1149 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
1154 com_node(c
, CHILD(n
, 2));
1158 com_call_function(c
, n
)
1159 struct compiling
*c
;
1160 node
*n
; /* EITHER arglist OR ')' */
1162 if (TYPE(n
) == RPAR
) {
1163 com_addoparg(c
, CALL_FUNCTION
, 0);
1166 PyObject
*keywords
= NULL
;
1168 int lineno
= n
->n_lineno
;
1172 for (i
= 0; i
< NCH(n
); i
+= 2) {
1173 node
*ch
= CHILD(n
, i
);
1174 if (ch
->n_lineno
!= lineno
) {
1175 lineno
= ch
->n_lineno
;
1176 com_addoparg(c
, SET_LINENO
, lineno
);
1178 com_argument(c
, ch
, &keywords
);
1179 if (keywords
== NULL
)
1184 Py_XDECREF(keywords
);
1185 if (na
> 255 || nk
> 255) {
1186 com_error(c
, PyExc_SyntaxError
,
1187 "more than 255 arguments");
1189 com_addoparg(c
, CALL_FUNCTION
, na
| (nk
<< 8));
1190 com_pop(c
, na
+ 2*nk
);
1195 com_select_member(c
, n
)
1196 struct compiling
*c
;
1199 com_addopname(c
, LOAD_ATTR
, n
);
1204 struct compiling
*c
;
1208 int ns
=2; /* number of slice arguments */
1211 /* first argument */
1212 if (TYPE(CHILD(n
,i
)) == COLON
) {
1213 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1218 com_node(c
, CHILD(n
,i
));
1220 REQ(CHILD(n
,i
),COLON
);
1223 /* second argument */
1224 if (i
< NCH(n
) && TYPE(CHILD(n
,i
)) == test
) {
1225 com_node(c
, CHILD(n
,i
));
1229 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1232 /* remaining arguments */
1233 for (; i
< NCH(n
); i
++) {
1238 /* right argument of ':' missing */
1239 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1243 com_node(c
, CHILD(ch
,1));
1245 com_addoparg(c
, BUILD_SLICE
, ns
);
1246 com_pop(c
, 1 + (ns
== 3));
1251 struct compiling
*c
;
1257 /* check for rubber index */
1258 if (TYPE(ch
) == DOT
&& TYPE(CHILD(n
,1)) == DOT
) {
1259 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_Ellipsis
));
1263 /* check for slice */
1264 if ((TYPE(ch
) == COLON
|| NCH(n
) > 1))
1274 com_subscriptlist(c
, n
, assigning
)
1275 struct compiling
*c
;
1280 REQ(n
, subscriptlist
);
1281 /* Check to make backward compatible slice behavior for '[i:j]' */
1283 node
*sub
= CHILD(n
, 0); /* subscript */
1284 /* Make it is a simple slice.
1285 Should have exactly one colon. */
1286 if ((TYPE(CHILD(sub
, 0)) == COLON
1287 || (NCH(sub
) > 1 && TYPE(CHILD(sub
, 1)) == COLON
))
1288 && (TYPE(CHILD(sub
,NCH(sub
)-1)) != sliceop
))
1290 if (assigning
== OP_APPLY
)
1293 op
= ((assigning
== OP_ASSIGN
) ?
1294 STORE_SLICE
: DELETE_SLICE
);
1295 com_slice(c
, sub
, op
);
1296 if (op
== STORE_SLICE
)
1298 else if (op
== DELETE_SLICE
)
1303 /* Else normal subscriptlist. Compile each subscript. */
1304 for (i
= 0; i
< NCH(n
); i
+= 2)
1305 com_subscript(c
, CHILD(n
, i
));
1306 /* Put multiple subscripts into a tuple */
1309 com_addoparg(c
, BUILD_TUPLE
, i
);
1312 if (assigning
== OP_APPLY
) {
1316 else if (assigning
== OP_ASSIGN
) {
1329 com_apply_trailer(c
, n
)
1330 struct compiling
*c
;
1334 switch (TYPE(CHILD(n
, 0))) {
1336 com_call_function(c
, CHILD(n
, 1));
1339 com_select_member(c
, CHILD(n
, 1));
1342 com_subscriptlist(c
, CHILD(n
, 1), OP_APPLY
);
1345 com_error(c
, PyExc_SystemError
,
1346 "com_apply_trailer: unknown trailer type");
1352 struct compiling
*c
;
1357 com_atom(c
, CHILD(n
, 0));
1358 for (i
= 1; i
< NCH(n
); i
++) {
1359 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
1360 com_factor(c
, CHILD(n
, i
+1));
1361 com_addbyte(c
, BINARY_POWER
);
1366 com_apply_trailer(c
, CHILD(n
, i
));
1372 struct compiling
*c
;
1376 if (TYPE(CHILD(n
, 0)) == PLUS
) {
1377 com_factor(c
, CHILD(n
, 1));
1378 com_addbyte(c
, UNARY_POSITIVE
);
1380 else if (TYPE(CHILD(n
, 0)) == MINUS
) {
1381 com_factor(c
, CHILD(n
, 1));
1382 com_addbyte(c
, UNARY_NEGATIVE
);
1384 else if (TYPE(CHILD(n
, 0)) == TILDE
) {
1385 com_factor(c
, CHILD(n
, 1));
1386 com_addbyte(c
, UNARY_INVERT
);
1389 com_power(c
, CHILD(n
, 0));
1395 struct compiling
*c
;
1401 com_factor(c
, CHILD(n
, 0));
1402 for (i
= 2; i
< NCH(n
); i
+= 2) {
1403 com_factor(c
, CHILD(n
, i
));
1404 switch (TYPE(CHILD(n
, i
-1))) {
1406 op
= BINARY_MULTIPLY
;
1415 com_error(c
, PyExc_SystemError
,
1416 "com_term: operator not *, / or %");
1425 com_arith_expr(c
, n
)
1426 struct compiling
*c
;
1432 com_term(c
, CHILD(n
, 0));
1433 for (i
= 2; i
< NCH(n
); i
+= 2) {
1434 com_term(c
, CHILD(n
, i
));
1435 switch (TYPE(CHILD(n
, i
-1))) {
1440 op
= BINARY_SUBTRACT
;
1443 com_error(c
, PyExc_SystemError
,
1444 "com_arith_expr: operator not + or -");
1453 com_shift_expr(c
, n
)
1454 struct compiling
*c
;
1460 com_arith_expr(c
, CHILD(n
, 0));
1461 for (i
= 2; i
< NCH(n
); i
+= 2) {
1462 com_arith_expr(c
, CHILD(n
, i
));
1463 switch (TYPE(CHILD(n
, i
-1))) {
1471 com_error(c
, PyExc_SystemError
,
1472 "com_shift_expr: operator not << or >>");
1482 struct compiling
*c
;
1488 com_shift_expr(c
, CHILD(n
, 0));
1489 for (i
= 2; i
< NCH(n
); i
+= 2) {
1490 com_shift_expr(c
, CHILD(n
, i
));
1491 if (TYPE(CHILD(n
, i
-1)) == AMPER
) {
1495 com_error(c
, PyExc_SystemError
,
1496 "com_and_expr: operator not &");
1506 struct compiling
*c
;
1512 com_and_expr(c
, CHILD(n
, 0));
1513 for (i
= 2; i
< NCH(n
); i
+= 2) {
1514 com_and_expr(c
, CHILD(n
, i
));
1515 if (TYPE(CHILD(n
, i
-1)) == CIRCUMFLEX
) {
1519 com_error(c
, PyExc_SystemError
,
1520 "com_xor_expr: operator not ^");
1530 struct compiling
*c
;
1536 com_xor_expr(c
, CHILD(n
, 0));
1537 for (i
= 2; i
< NCH(n
); i
+= 2) {
1538 com_xor_expr(c
, CHILD(n
, i
));
1539 if (TYPE(CHILD(n
, i
-1)) == VBAR
) {
1543 com_error(c
, PyExc_SystemError
,
1544 "com_expr: expr operator not |");
1557 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1558 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1562 case LESS
: return LT
;
1563 case GREATER
: return GT
;
1564 case EQEQUAL
: /* == */
1565 case EQUAL
: return EQ
;
1566 case LESSEQUAL
: return LE
;
1567 case GREATEREQUAL
: return GE
;
1568 case NOTEQUAL
: return NE
; /* <> or != */
1569 case NAME
: if (strcmp(STR(n
), "in") == 0) return IN
;
1570 if (strcmp(STR(n
), "is") == 0) return IS
;
1573 else if (NCH(n
) == 2) {
1574 switch (TYPE(CHILD(n
, 0))) {
1575 case NAME
: if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
1577 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
1585 com_comparison(c
, n
)
1586 struct compiling
*c
;
1592 REQ(n
, comparison
); /* comparison: expr (comp_op expr)* */
1593 com_expr(c
, CHILD(n
, 0));
1597 /****************************************************************
1598 The following code is generated for all but the last
1599 comparison in a chain:
1601 label: on stack: opcode: jump to:
1607 b, 0-or-1 JUMP_IF_FALSE L1
1611 We are now ready to repeat this sequence for the next
1612 comparison in the chain.
1614 For the last we generate:
1620 If there were any jumps to L1 (i.e., there was more than one
1621 comparison), we generate:
1623 0-or-1 JUMP_FORWARD L2
1628 ****************************************************************/
1632 for (i
= 2; i
< NCH(n
); i
+= 2) {
1633 com_expr(c
, CHILD(n
, i
));
1635 com_addbyte(c
, DUP_TOP
);
1637 com_addbyte(c
, ROT_THREE
);
1639 op
= cmp_type(CHILD(n
, i
-1));
1641 com_error(c
, PyExc_SystemError
,
1642 "com_comparison: unknown comparison op");
1644 com_addoparg(c
, COMPARE_OP
, op
);
1647 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
1648 com_addbyte(c
, POP_TOP
);
1655 com_addfwref(c
, JUMP_FORWARD
, &anchor2
);
1656 com_backpatch(c
, anchor
);
1657 com_addbyte(c
, ROT_TWO
);
1658 com_addbyte(c
, POP_TOP
);
1659 com_backpatch(c
, anchor2
);
1665 struct compiling
*c
;
1668 REQ(n
, not_test
); /* 'not' not_test | comparison */
1670 com_comparison(c
, CHILD(n
, 0));
1673 com_not_test(c
, CHILD(n
, 1));
1674 com_addbyte(c
, UNARY_NOT
);
1680 struct compiling
*c
;
1685 REQ(n
, and_test
); /* not_test ('and' not_test)* */
1689 com_not_test(c
, CHILD(n
, i
));
1690 if ((i
+= 2) >= NCH(n
))
1692 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
1693 com_addbyte(c
, POP_TOP
);
1697 com_backpatch(c
, anchor
);
1702 struct compiling
*c
;
1705 REQ(n
, test
); /* and_test ('or' and_test)* | lambdef */
1706 if (NCH(n
) == 1 && TYPE(CHILD(n
, 0)) == lambdef
) {
1709 int ndefs
= com_argdefs(c
, CHILD(n
, 0));
1710 v
= (PyObject
*) icompile(CHILD(n
, 0), c
);
1716 i
= com_addconst(c
, v
);
1719 com_addoparg(c
, LOAD_CONST
, i
);
1721 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
1728 com_and_test(c
, CHILD(n
, i
));
1729 if ((i
+= 2) >= NCH(n
))
1731 com_addfwref(c
, JUMP_IF_TRUE
, &anchor
);
1732 com_addbyte(c
, POP_TOP
);
1736 com_backpatch(c
, anchor
);
1741 com_list(c
, n
, toplevel
)
1742 struct compiling
*c
;
1744 int toplevel
; /* If nonzero, *always* build a tuple */
1746 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
1747 if (NCH(n
) == 1 && !toplevel
) {
1748 com_node(c
, CHILD(n
, 0));
1753 len
= (NCH(n
) + 1) / 2;
1754 for (i
= 0; i
< NCH(n
); i
+= 2)
1755 com_node(c
, CHILD(n
, i
));
1756 com_addoparg(c
, BUILD_TUPLE
, len
);
1762 /* Begin of assignment compilation */
1764 static void com_assign_name
Py_PROTO((struct compiling
*, node
*, int));
1765 static void com_assign
Py_PROTO((struct compiling
*, node
*, int));
1768 com_assign_attr(c
, n
, assigning
)
1769 struct compiling
*c
;
1773 com_addopname(c
, assigning
? STORE_ATTR
: DELETE_ATTR
, n
);
1774 com_pop(c
, assigning
? 2 : 1);
1778 com_assign_trailer(c
, n
, assigning
)
1779 struct compiling
*c
;
1784 switch (TYPE(CHILD(n
, 0))) {
1785 case LPAR
: /* '(' [exprlist] ')' */
1786 com_error(c
, PyExc_SyntaxError
,
1787 "can't assign to function call");
1789 case DOT
: /* '.' NAME */
1790 com_assign_attr(c
, CHILD(n
, 1), assigning
);
1792 case LSQB
: /* '[' subscriptlist ']' */
1793 com_subscriptlist(c
, CHILD(n
, 1), assigning
);
1796 com_error(c
, PyExc_SystemError
, "unknown trailer type");
1801 com_assign_tuple(c
, n
, assigning
)
1802 struct compiling
*c
;
1807 if (TYPE(n
) != testlist
)
1811 com_addoparg(c
, UNPACK_TUPLE
, i
);
1814 for (i
= 0; i
< NCH(n
); i
+= 2)
1815 com_assign(c
, CHILD(n
, i
), assigning
);
1819 com_assign_list(c
, n
, assigning
)
1820 struct compiling
*c
;
1827 com_addoparg(c
, UNPACK_LIST
, i
);
1830 for (i
= 0; i
< NCH(n
); i
+= 2)
1831 com_assign(c
, CHILD(n
, i
), assigning
);
1835 com_assign_name(c
, n
, assigning
)
1836 struct compiling
*c
;
1841 com_addopname(c
, assigning
? STORE_NAME
: DELETE_NAME
, n
);
1847 com_assign(c
, n
, assigning
)
1848 struct compiling
*c
;
1852 /* Loop to avoid trivial recursion */
1859 com_assign_tuple(c
, n
, assigning
);
1877 com_error(c
, PyExc_SyntaxError
,
1878 "can't assign to operator");
1884 case power
: /* atom trailer* ('**' power)* */
1885 /* ('+'|'-'|'~') factor | atom trailer* */
1886 if (TYPE(CHILD(n
, 0)) != atom
) {
1887 com_error(c
, PyExc_SyntaxError
,
1888 "can't assign to operator");
1891 if (NCH(n
) > 1) { /* trailer or exponent present */
1893 com_node(c
, CHILD(n
, 0));
1894 for (i
= 1; i
+1 < NCH(n
); i
++) {
1895 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
1896 com_error(c
, PyExc_SyntaxError
,
1897 "can't assign to operator");
1900 com_apply_trailer(c
, CHILD(n
, i
));
1901 } /* NB i is still alive */
1902 com_assign_trailer(c
,
1903 CHILD(n
, i
), assigning
);
1910 switch (TYPE(CHILD(n
, 0))) {
1913 if (TYPE(n
) == RPAR
) {
1914 /* XXX Should allow () = () ??? */
1915 com_error(c
, PyExc_SyntaxError
,
1916 "can't assign to ()");
1922 if (TYPE(n
) == RSQB
) {
1923 com_error(c
, PyExc_SyntaxError
,
1924 "can't assign to []");
1927 com_assign_list(c
, n
, assigning
);
1930 com_assign_name(c
, CHILD(n
, 0), assigning
);
1933 com_error(c
, PyExc_SyntaxError
,
1934 "can't assign to literal");
1940 com_error(c
, PyExc_SyntaxError
,
1941 "can't assign to lambda");
1945 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
1946 com_error(c
, PyExc_SystemError
,
1947 "com_assign: bad node");
1954 /* Forward */ static node
*get_rawdocstring
Py_PROTO((node
*));
1958 struct compiling
*c
;
1961 REQ(n
, expr_stmt
); /* testlist ('=' testlist)* */
1962 /* Forget it if we have just a doc string here */
1963 if (!c
->c_interactive
&& NCH(n
) == 1 && get_rawdocstring(n
) != NULL
)
1965 com_node(c
, CHILD(n
, NCH(n
)-1));
1967 if (c
->c_interactive
)
1968 com_addbyte(c
, PRINT_EXPR
);
1970 com_addbyte(c
, POP_TOP
);
1975 for (i
= 0; i
< NCH(n
)-2; i
+=2) {
1976 if (i
+2 < NCH(n
)-2) {
1977 com_addbyte(c
, DUP_TOP
);
1980 com_assign(c
, CHILD(n
, i
), OP_ASSIGN
);
1986 com_assert_stmt(c
, n
)
1987 struct compiling
*c
;
1992 REQ(n
, assert_stmt
); /* 'assert' test [',' test] */
1993 /* Generate code like for
1997 raise AssertionError [, <message>]
1999 where <message> is the second test, if present.
2001 if (Py_OptimizeFlag
)
2003 com_addopnamestr(c
, LOAD_GLOBAL
, "__debug__");
2005 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
2006 com_addbyte(c
, POP_TOP
);
2008 com_node(c
, CHILD(n
, 1));
2009 com_addfwref(c
, JUMP_IF_TRUE
, &b
);
2010 com_addbyte(c
, POP_TOP
);
2012 /* Raise that exception! */
2013 com_addopnamestr(c
, LOAD_GLOBAL
, "AssertionError");
2015 i
= NCH(n
)/2; /* Either 2 or 4 */
2017 com_node(c
, CHILD(n
, 3));
2018 com_addoparg(c
, RAISE_VARARGS
, i
);
2020 /* The interpreter does not fall through */
2021 /* All jumps converge here */
2022 com_backpatch(c
, a
);
2023 com_backpatch(c
, b
);
2024 com_addbyte(c
, POP_TOP
);
2028 com_print_stmt(c
, n
)
2029 struct compiling
*c
;
2033 REQ(n
, print_stmt
); /* 'print' (test ',')* [test] */
2034 for (i
= 1; i
< NCH(n
); i
+= 2) {
2035 com_node(c
, CHILD(n
, i
));
2036 com_addbyte(c
, PRINT_ITEM
);
2039 if (TYPE(CHILD(n
, NCH(n
)-1)) != COMMA
)
2040 com_addbyte(c
, PRINT_NEWLINE
);
2041 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2045 com_return_stmt(c
, n
)
2046 struct compiling
*c
;
2049 REQ(n
, return_stmt
); /* 'return' [testlist] */
2050 if (!c
->c_infunction
) {
2051 com_error(c
, PyExc_SyntaxError
, "'return' outside function");
2054 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2058 com_node(c
, CHILD(n
, 1));
2059 com_addbyte(c
, RETURN_VALUE
);
2064 com_raise_stmt(c
, n
)
2065 struct compiling
*c
;
2069 REQ(n
, raise_stmt
); /* 'raise' [test [',' test [',' test]]] */
2071 com_node(c
, CHILD(n
, 1));
2073 com_node(c
, CHILD(n
, 3));
2075 com_node(c
, CHILD(n
, 5));
2079 com_addoparg(c
, RAISE_VARARGS
, i
);
2084 com_import_stmt(c
, n
)
2085 struct compiling
*c
;
2089 REQ(n
, import_stmt
);
2090 /* 'import' dotted_name (',' dotted_name)* |
2091 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2092 if (STR(CHILD(n
, 0))[0] == 'f') {
2093 /* 'from' dotted_name 'import' ... */
2094 REQ(CHILD(n
, 1), dotted_name
);
2095 com_addopname(c
, IMPORT_NAME
, CHILD(n
, 1));
2097 for (i
= 3; i
< NCH(n
); i
+= 2)
2098 com_addopname(c
, IMPORT_FROM
, CHILD(n
, i
));
2099 com_addbyte(c
, POP_TOP
);
2104 for (i
= 1; i
< NCH(n
); i
+= 2) {
2105 REQ(CHILD(n
, i
), dotted_name
);
2106 com_addopname(c
, IMPORT_NAME
, CHILD(n
, i
));
2108 com_addopname(c
, STORE_NAME
, CHILD(CHILD(n
, i
), 0));
2115 com_global_stmt(c
, n
)
2116 struct compiling
*c
;
2120 REQ(n
, global_stmt
);
2121 /* 'global' NAME (',' NAME)* */
2122 for (i
= 1; i
< NCH(n
); i
+= 2) {
2123 char *s
= STR(CHILD(n
, i
));
2124 #ifdef PRIVATE_NAME_MANGLING
2126 if (s
!= NULL
&& s
[0] == '_' && s
[1] == '_' &&
2127 c
->c_private
!= NULL
&&
2128 com_mangle(c
, s
, buffer
, (int)sizeof(buffer
)))
2131 if (PyDict_GetItemString(c
->c_locals
, s
) != NULL
) {
2132 com_error(c
, PyExc_SyntaxError
,
2133 "name is local and global");
2135 else if (PyDict_SetItemString(c
->c_globals
, s
, Py_None
) != 0)
2141 com_newlocal_o(c
, nameval
)
2142 struct compiling
*c
;
2147 if (PyList_Size(c
->c_varnames
) != c
->c_nlocals
) {
2148 /* This is usually caused by an error on a previous call */
2149 if (c
->c_errors
== 0) {
2150 com_error(c
, PyExc_SystemError
,
2151 "mixed up var name/index");
2155 ival
= PyInt_FromLong(i
= c
->c_nlocals
++);
2158 else if (PyDict_SetItem(c
->c_locals
, nameval
, ival
) != 0)
2160 else if (PyList_Append(c
->c_varnames
, nameval
) != 0)
2167 com_addlocal_o(c
, nameval
)
2168 struct compiling
*c
;
2171 PyObject
*ival
= PyDict_GetItem(c
->c_locals
, nameval
);
2173 return PyInt_AsLong(ival
);
2174 return com_newlocal_o(c
, nameval
);
2178 com_newlocal(c
, name
)
2179 struct compiling
*c
;
2182 PyObject
*nameval
= PyString_InternFromString(name
);
2184 if (nameval
== NULL
) {
2188 i
= com_newlocal_o(c
, nameval
);
2195 struct compiling
*c
;
2199 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2200 com_node(c
, CHILD(n
, 1));
2202 com_node(c
, CHILD(n
, 3));
2204 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2208 com_node(c
, CHILD(n
, 5));
2210 com_addbyte(c
, DUP_TOP
);
2213 com_addbyte(c
, EXEC_STMT
);
2218 is_constant_false(c
, n
)
2219 struct compiling
*c
;
2225 /* Label to avoid tail recursion */
2236 for (i
= 0; i
< NCH(n
); i
++) {
2237 node
*ch
= CHILD(n
, i
);
2238 if (TYPE(ch
) == stmt
) {
2273 if (Py_OptimizeFlag
&& strcmp(STR(n
), "__debug__") == 0)
2278 v
= parsenumber(c
, STR(n
));
2283 i
= PyObject_IsTrue(v
);
2288 v
= parsestr(STR(n
));
2293 i
= PyObject_IsTrue(v
);
2303 struct compiling
*c
;
2309 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2310 for (i
= 0; i
+3 < NCH(n
); i
+=4) {
2312 node
*ch
= CHILD(n
, i
+1);
2313 if (is_constant_false(c
, ch
))
2316 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
2318 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
2319 com_addbyte(c
, POP_TOP
);
2321 com_node(c
, CHILD(n
, i
+3));
2322 com_addfwref(c
, JUMP_FORWARD
, &anchor
);
2323 com_backpatch(c
, a
);
2324 /* We jump here with an extra entry which we now pop */
2325 com_addbyte(c
, POP_TOP
);
2328 com_node(c
, CHILD(n
, i
+2));
2330 com_backpatch(c
, anchor
);
2334 com_while_stmt(c
, n
)
2335 struct compiling
*c
;
2338 int break_anchor
= 0;
2340 int save_begin
= c
->c_begin
;
2341 REQ(n
, while_stmt
); /* 'while' test ':' suite ['else' ':' suite] */
2342 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
2343 block_push(c
, SETUP_LOOP
);
2344 c
->c_begin
= c
->c_nexti
;
2345 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
2346 com_node(c
, CHILD(n
, 1));
2347 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2348 com_addbyte(c
, POP_TOP
);
2351 com_node(c
, CHILD(n
, 3));
2353 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
2354 c
->c_begin
= save_begin
;
2355 com_backpatch(c
, anchor
);
2356 /* We jump here with one entry more on the stack */
2357 com_addbyte(c
, POP_TOP
);
2358 com_addbyte(c
, POP_BLOCK
);
2359 block_pop(c
, SETUP_LOOP
);
2361 com_node(c
, CHILD(n
, 6));
2362 com_backpatch(c
, break_anchor
);
2367 struct compiling
*c
;
2371 int break_anchor
= 0;
2373 int save_begin
= c
->c_begin
;
2375 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2376 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
2377 block_push(c
, SETUP_LOOP
);
2378 com_node(c
, CHILD(n
, 3));
2379 v
= PyInt_FromLong(0L);
2382 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
2385 c
->c_begin
= c
->c_nexti
;
2386 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
2387 com_addfwref(c
, FOR_LOOP
, &anchor
);
2389 com_assign(c
, CHILD(n
, 1), OP_ASSIGN
);
2391 com_node(c
, CHILD(n
, 5));
2393 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
2394 c
->c_begin
= save_begin
;
2395 com_backpatch(c
, anchor
);
2396 com_pop(c
, 2); /* FOR_LOOP has popped these */
2397 com_addbyte(c
, POP_BLOCK
);
2398 block_pop(c
, SETUP_LOOP
);
2400 com_node(c
, CHILD(n
, 8));
2401 com_backpatch(c
, break_anchor
);
2404 /* Code generated for "try: S finally: Sf" is as follows:
2413 The special instructions use the block stack. Each block
2414 stack entry contains the instruction that created it (here
2415 SETUP_FINALLY), the level of the value stack at the time the
2416 block stack entry was created, and a label (here L).
2419 Pushes the current value stack level and the label
2420 onto the block stack.
2422 Pops en entry from the block stack, and pops the value
2423 stack until its level is the same as indicated on the
2424 block stack. (The label is ignored.)
2426 Pops a variable number of entries from the *value* stack
2427 and re-raises the exception they specify. The number of
2428 entries popped depends on the (pseudo) exception type.
2430 The block stack is unwound when an exception is raised:
2431 when a SETUP_FINALLY entry is found, the exception is pushed
2432 onto the value stack (and the exception condition is cleared),
2433 and the interpreter jumps to the label gotten from the block
2436 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2437 (The contents of the value stack is shown in [], with the top
2438 at the right; 'tb' is trace-back info, 'val' the exception's
2439 associated value, and 'exc' the exception.)
2441 Value stack Label Instruction Argument
2447 [tb, val, exc] L1: DUP )
2448 [tb, val, exc, exc] <evaluate E1> )
2449 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2450 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2451 [tb, val, exc, 1] POP )
2453 [tb, val] <assign to V1> (or POP if no V1)
2458 [tb, val, exc, 0] L2: POP
2460 .............................etc.......................
2462 [tb, val, exc, 0] Ln+1: POP
2463 [tb, val, exc] END_FINALLY # re-raise exception
2465 [] L0: <next statement>
2467 Of course, parts are not generated if Vi or Ei is not present.
2471 com_try_except(c
, n
)
2472 struct compiling
*c
;
2475 int except_anchor
= 0;
2477 int else_anchor
= 0;
2481 com_addfwref(c
, SETUP_EXCEPT
, &except_anchor
);
2482 block_push(c
, SETUP_EXCEPT
);
2483 com_node(c
, CHILD(n
, 2));
2484 com_addbyte(c
, POP_BLOCK
);
2485 block_pop(c
, SETUP_EXCEPT
);
2486 com_addfwref(c
, JUMP_FORWARD
, &else_anchor
);
2487 com_backpatch(c
, except_anchor
);
2489 i
< NCH(n
) && TYPE(ch
= CHILD(n
, i
)) == except_clause
;
2491 /* except_clause: 'except' [expr [',' var]] */
2492 if (except_anchor
== 0) {
2493 com_error(c
, PyExc_SyntaxError
,
2494 "default 'except:' must be last");
2498 com_push(c
, 3); /* tb, val, exc pushed by exception */
2499 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
2501 com_addbyte(c
, DUP_TOP
);
2503 com_node(c
, CHILD(ch
, 1));
2504 com_addoparg(c
, COMPARE_OP
, EXC_MATCH
);
2506 com_addfwref(c
, JUMP_IF_FALSE
, &except_anchor
);
2507 com_addbyte(c
, POP_TOP
);
2510 com_addbyte(c
, POP_TOP
);
2513 com_assign(c
, CHILD(ch
, 3), OP_ASSIGN
);
2515 com_addbyte(c
, POP_TOP
);
2518 com_addbyte(c
, POP_TOP
);
2520 com_node(c
, CHILD(n
, i
+2));
2521 com_addfwref(c
, JUMP_FORWARD
, &end_anchor
);
2522 if (except_anchor
) {
2523 com_backpatch(c
, except_anchor
);
2524 /* We come in with [tb, val, exc, 0] on the
2525 stack; one pop and it's the same as
2526 expected at the start of the loop */
2527 com_addbyte(c
, POP_TOP
);
2530 /* We actually come in here with [tb, val, exc] but the
2531 END_FINALLY will zap those and jump around.
2532 The c_stacklevel does not reflect them so we need not pop
2534 com_addbyte(c
, END_FINALLY
);
2535 com_backpatch(c
, else_anchor
);
2537 com_node(c
, CHILD(n
, i
+2));
2538 com_backpatch(c
, end_anchor
);
2542 com_try_finally(c
, n
)
2543 struct compiling
*c
;
2546 int finally_anchor
= 0;
2549 com_addfwref(c
, SETUP_FINALLY
, &finally_anchor
);
2550 block_push(c
, SETUP_FINALLY
);
2551 com_node(c
, CHILD(n
, 2));
2552 com_addbyte(c
, POP_BLOCK
);
2553 block_pop(c
, SETUP_FINALLY
);
2554 block_push(c
, END_FINALLY
);
2555 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2556 /* While the generated code pushes only one item,
2557 the try-finally handling can enter here with
2558 up to three items. OK, here are the details:
2559 3 for an exception, 2 for RETURN, 1 for BREAK. */
2561 com_backpatch(c
, finally_anchor
);
2562 ch
= CHILD(n
, NCH(n
)-1);
2563 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
2565 com_addbyte(c
, END_FINALLY
);
2566 block_pop(c
, END_FINALLY
);
2567 com_pop(c
, 3); /* Matches the com_push above */
2572 struct compiling
*c
;
2576 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2577 | 'try' ':' suite 'finally' ':' suite */
2578 if (TYPE(CHILD(n
, 3)) != except_clause
)
2579 com_try_finally(c
, n
);
2581 com_try_except(c
, n
);
2590 /* Label to avoid tail recursion */
2601 for (i
= 0; i
< NCH(n
); i
++) {
2602 node
*ch
= CHILD(n
, i
);
2603 if (TYPE(ch
) == stmt
) {
2637 if (TYPE(CHILD(n
, 0)) == STRING
)
2649 /* Don't generate doc-strings if run with -OO */
2650 if (Py_OptimizeFlag
> 1)
2652 n
= get_rawdocstring(n
);
2655 return parsestrplus(n
);
2660 struct compiling
*c
;
2664 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2666 com_node(c
, CHILD(n
, 0));
2670 for (i
= 0; i
< NCH(n
); i
++) {
2671 node
*ch
= CHILD(n
, i
);
2672 if (TYPE(ch
) == stmt
)
2680 com_continue_stmt(c
, n
)
2681 struct compiling
*c
;
2682 node
*n
; /* Not used, but passed for consistency */
2684 int i
= c
->c_nblocks
;
2685 if (i
-- > 0 && c
->c_block
[i
] == SETUP_LOOP
) {
2686 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
2689 com_error(c
, PyExc_SyntaxError
,
2690 "'continue' not properly in loop");
2692 /* XXX Could allow it inside a 'finally' clause
2693 XXX if we could pop the exception still on the stack */
2698 struct compiling
*c
;
2701 int i
, nch
, nargs
, ndefs
;
2702 if (TYPE(n
) == lambdef
) {
2703 /* lambdef: 'lambda' [varargslist] ':' test */
2707 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ... */
2709 REQ(n
, parameters
); /* parameters: '(' [varargslist] ')' */
2712 if (TYPE(n
) != varargslist
)
2715 (fpdef ['=' test] ',')* '*' ....... |
2716 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2720 for (i
= 0; i
< nch
; i
++) {
2722 if (TYPE(CHILD(n
, i
)) == STAR
||
2723 TYPE(CHILD(n
, i
)) == DOUBLESTAR
)
2728 t
= RPAR
; /* Anything except EQUAL or COMMA */
2730 t
= TYPE(CHILD(n
, i
));
2734 com_node(c
, CHILD(n
, i
));
2738 t
= TYPE(CHILD(n
, i
));
2741 /* Treat "(a=1, b)" as an error */
2743 com_error(c
, PyExc_SyntaxError
,
2744 "non-default argument follows default argument");
2754 struct compiling
*c
;
2758 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
2759 v
= (PyObject
*)icompile(n
, c
);
2763 int i
= com_addconst(c
, v
);
2764 int ndefs
= com_argdefs(c
, n
);
2765 com_addoparg(c
, LOAD_CONST
, i
);
2767 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
2769 com_addopname(c
, STORE_NAME
, CHILD(n
, 1));
2777 struct compiling
*c
;
2782 /* testlist: test (',' test)* [','] */
2783 for (i
= 0; i
< NCH(n
); i
+= 2)
2784 com_node(c
, CHILD(n
, i
));
2786 com_addoparg(c
, BUILD_TUPLE
, i
);
2792 struct compiling
*c
;
2798 /* classdef: class NAME ['(' testlist ')'] ':' suite */
2799 if ((v
= PyString_InternFromString(STR(CHILD(n
, 1)))) == NULL
) {
2803 /* Push the class name on the stack */
2804 i
= com_addconst(c
, v
);
2805 com_addoparg(c
, LOAD_CONST
, i
);
2808 /* Push the tuple of base classes on the stack */
2809 if (TYPE(CHILD(n
, 2)) != LPAR
) {
2810 com_addoparg(c
, BUILD_TUPLE
, 0);
2814 com_bases(c
, CHILD(n
, 3));
2815 v
= (PyObject
*)icompile(n
, c
);
2819 i
= com_addconst(c
, v
);
2820 com_addoparg(c
, LOAD_CONST
, i
);
2822 com_addoparg(c
, MAKE_FUNCTION
, 0);
2823 com_addoparg(c
, CALL_FUNCTION
, 0);
2824 com_addbyte(c
, BUILD_CLASS
);
2826 com_addopname(c
, STORE_NAME
, CHILD(n
, 1));
2833 struct compiling
*c
;
2838 /* Definition nodes */
2847 /* Trivial parse tree nodes */
2852 com_node(c
, CHILD(n
, 0));
2856 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2857 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
2860 for (i
= 0; i
< NCH(n
)-1; i
+= 2)
2861 com_node(c
, CHILD(n
, i
));
2866 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
2867 com_node(c
, CHILD(n
, 0));
2870 /* Statement nodes */
2873 com_expr_stmt(c
, n
);
2876 com_print_stmt(c
, n
);
2878 case del_stmt
: /* 'del' exprlist */
2879 com_assign(c
, CHILD(n
, 1), OP_DELETE
);
2884 if (c
->c_loops
== 0) {
2885 com_error(c
, PyExc_SyntaxError
,
2886 "'break' outside loop");
2888 com_addbyte(c
, BREAK_LOOP
);
2891 com_continue_stmt(c
, n
);
2894 com_return_stmt(c
, n
);
2897 com_raise_stmt(c
, n
);
2900 com_import_stmt(c
, n
);
2903 com_global_stmt(c
, n
);
2906 com_exec_stmt(c
, n
);
2909 com_assert_stmt(c
, n
);
2915 com_while_stmt(c
, n
);
2927 /* Expression nodes */
2942 com_comparison(c
, n
);
2957 com_shift_expr(c
, n
);
2960 com_arith_expr(c
, n
);
2976 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
2977 com_error(c
, PyExc_SystemError
,
2978 "com_node: unexpected node type");
2982 static void com_fplist
Py_PROTO((struct compiling
*, node
*));
2986 struct compiling
*c
;
2989 REQ(n
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
2990 if (TYPE(CHILD(n
, 0)) == LPAR
)
2991 com_fplist(c
, CHILD(n
, 1));
2993 com_addoparg(c
, STORE_FAST
, com_newlocal(c
, STR(CHILD(n
, 0))));
3000 struct compiling
*c
;
3003 REQ(n
, fplist
); /* fplist: fpdef (',' fpdef)* [','] */
3005 com_fpdef(c
, CHILD(n
, 0));
3008 int i
= (NCH(n
)+1)/2;
3009 com_addoparg(c
, UNPACK_TUPLE
, i
);
3011 for (i
= 0; i
< NCH(n
); i
+= 2)
3012 com_fpdef(c
, CHILD(n
, i
));
3018 struct compiling
*c
;
3024 REQ(n
, varargslist
);
3026 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3028 /* Enter all arguments in table of locals */
3029 for (i
= 0; i
< nch
; i
++) {
3030 node
*ch
= CHILD(n
, i
);
3033 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3035 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3037 if (TYPE(fp
) == NAME
)
3041 sprintf(nbuf
, ".%d", i
);
3044 com_newlocal(c
, name
);
3049 if (TYPE(ch
) == EQUAL
)
3054 /* Handle *arguments */
3058 if (TYPE(ch
) != DOUBLESTAR
) {
3061 if (TYPE(ch
) == NAME
) {
3062 c
->c_flags
|= CO_VARARGS
;
3064 com_newlocal(c
, STR(ch
));
3068 /* Handle **keywords */
3072 if (TYPE(ch
) != DOUBLESTAR
) {
3081 c
->c_flags
|= CO_VARKEYWORDS
;
3082 com_newlocal(c
, STR(ch
));
3085 /* Generate code for complex arguments only after
3086 having counted the simple arguments */
3088 for (i
= 0; i
< nch
; i
++) {
3089 node
*ch
= CHILD(n
, i
);
3091 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3093 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3095 if (TYPE(fp
) != NAME
) {
3096 com_addoparg(c
, LOAD_FAST
, ilocal
);
3104 if (TYPE(ch
) == EQUAL
)
3113 com_file_input(c
, n
)
3114 struct compiling
*c
;
3119 REQ(n
, file_input
); /* (NEWLINE | stmt)* ENDMARKER */
3120 doc
= get_docstring(n
);
3122 int i
= com_addconst(c
, doc
);
3124 com_addoparg(c
, LOAD_CONST
, i
);
3126 com_addopnamestr(c
, STORE_NAME
, "__doc__");
3129 for (i
= 0; i
< NCH(n
); i
++) {
3130 node
*ch
= CHILD(n
, i
);
3131 if (TYPE(ch
) != ENDMARKER
&& TYPE(ch
) != NEWLINE
)
3136 /* Top-level compile-node interface */
3139 compile_funcdef(c
, n
)
3140 struct compiling
*c
;
3145 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
3146 c
->c_name
= STR(CHILD(n
, 1));
3147 doc
= get_docstring(CHILD(n
, 4));
3149 (void) com_addconst(c
, doc
);
3153 (void) com_addconst(c
, Py_None
); /* No docstring */
3154 ch
= CHILD(n
, 2); /* parameters: '(' [varargslist] ')' */
3155 ch
= CHILD(ch
, 1); /* ')' | varargslist */
3156 if (TYPE(ch
) == varargslist
)
3158 c
->c_infunction
= 1;
3159 com_node(c
, CHILD(n
, 4));
3160 c
->c_infunction
= 0;
3161 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3163 com_addbyte(c
, RETURN_VALUE
);
3168 compile_lambdef(c
, n
)
3169 struct compiling
*c
;
3173 REQ(n
, lambdef
); /* lambdef: 'lambda' [varargslist] ':' test */
3174 c
->c_name
= "<lambda>";
3177 (void) com_addconst(c
, Py_None
); /* No docstring */
3178 if (TYPE(ch
) == varargslist
) {
3185 com_addbyte(c
, RETURN_VALUE
);
3190 compile_classdef(c
, n
)
3191 struct compiling
*c
;
3197 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3198 c
->c_name
= STR(CHILD(n
, 1));
3199 #ifdef PRIVATE_NAME_MANGLING
3200 c
->c_private
= c
->c_name
;
3202 ch
= CHILD(n
, NCH(n
)-1); /* The suite */
3203 doc
= get_docstring(ch
);
3205 int i
= com_addconst(c
, doc
);
3207 com_addoparg(c
, LOAD_CONST
, i
);
3209 com_addopnamestr(c
, STORE_NAME
, "__doc__");
3213 (void) com_addconst(c
, Py_None
);
3215 com_addbyte(c
, LOAD_LOCALS
);
3217 com_addbyte(c
, RETURN_VALUE
);
3223 struct compiling
*c
;
3226 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3230 case single_input
: /* One interactive command */
3231 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
3234 if (TYPE(n
) != NEWLINE
)
3236 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3238 com_addbyte(c
, RETURN_VALUE
);
3243 case file_input
: /* A whole file, or built-in function exec() */
3244 com_file_input(c
, n
);
3245 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3247 com_addbyte(c
, RETURN_VALUE
);
3251 case eval_input
: /* Built-in function input() */
3252 com_node(c
, CHILD(n
, 0));
3253 com_addbyte(c
, RETURN_VALUE
);
3257 case lambdef
: /* anonymous function definition */
3258 compile_lambdef(c
, n
);
3261 case funcdef
: /* A function definition */
3262 compile_funcdef(c
, n
);
3265 case classdef
: /* A class definition */
3266 compile_classdef(c
, n
);
3270 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
3271 com_error(c
, PyExc_SystemError
,
3272 "compile_node: unexpected node type");
3276 /* Optimization for local variables in functions (and *only* functions).
3278 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
3279 instructions that refer to local variables with LOAD_FAST etc.
3280 The latter instructions are much faster because they don't need to
3281 look up the variable name in a dictionary.
3283 To find all local variables, we check all STORE_NAME, IMPORT_FROM
3284 and DELETE_NAME instructions. This yields all local variables,
3285 function definitions, class definitions and import statements.
3286 Argument names have already been entered into the list by the
3287 special processing for the argument list.
3289 All remaining LOAD_NAME instructions must refer to non-local (global
3290 or builtin) variables, so are replaced by LOAD_GLOBAL.
3292 There are two problems: 'from foo import *' and 'exec' may introduce
3293 local variables that we can't know while compiling. If this is the
3294 case, we can still optimize bona fide locals (since those
3295 statements will be surrounded by fast_2_locals() and
3296 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
3298 NB: this modifies the string object c->c_code! */
3302 struct compiling
*c
;
3304 unsigned char *next_instr
, *cur_instr
;
3308 PyObject
*error_type
, *error_value
, *error_traceback
;
3310 #define NEXTOP() (*next_instr++)
3311 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
3312 #define GETITEM(v, i) (PyList_GetItem((v), (i)))
3313 #define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
3315 PyErr_Fetch(&error_type
, &error_value
, &error_traceback
);
3317 c
->c_flags
|= CO_OPTIMIZED
;
3319 next_instr
= (unsigned char *) PyString_AsString(c
->c_code
);
3322 if (opcode
== STOP_CODE
)
3324 if (HAS_ARG(opcode
))
3330 com_addlocal_o(c
, GETNAMEOBJ(oparg
));
3333 c
->c_flags
&= ~CO_OPTIMIZED
;
3338 if (PyDict_GetItemString(c
->c_locals
, "*") != NULL
)
3339 c
->c_flags
&= ~CO_OPTIMIZED
;
3341 next_instr
= (unsigned char *) PyString_AsString(c
->c_code
);
3343 cur_instr
= next_instr
;
3345 if (opcode
== STOP_CODE
)
3347 if (HAS_ARG(opcode
))
3349 if (opcode
== LOAD_NAME
||
3350 opcode
== STORE_NAME
||
3351 opcode
== DELETE_NAME
) {
3354 name
= GETNAMEOBJ(oparg
);
3355 v
= PyDict_GetItem(c
->c_locals
, name
);
3357 if (opcode
== LOAD_NAME
&&
3358 (c
->c_flags
&CO_OPTIMIZED
))
3359 cur_instr
[0] = LOAD_GLOBAL
;
3362 i
= PyInt_AsLong(v
);
3364 case LOAD_NAME
: cur_instr
[0] = LOAD_FAST
; break;
3365 case STORE_NAME
: cur_instr
[0] = STORE_FAST
; break;
3366 case DELETE_NAME
: cur_instr
[0] = DELETE_FAST
; break;
3368 cur_instr
[1] = i
& 0xff;
3369 cur_instr
[2] = (i
>>8) & 0xff;
3373 if (c
->c_errors
== 0)
3374 PyErr_Restore(error_type
, error_value
, error_traceback
);
3378 PyNode_Compile(n
, filename
)
3382 return jcompile(n
, filename
, NULL
);
3385 static PyCodeObject
*
3388 struct compiling
*base
;
3390 return jcompile(n
, base
->c_filename
, base
);
3393 static PyCodeObject
*
3394 jcompile(n
, filename
, base
)
3397 struct compiling
*base
;
3399 struct compiling sc
;
3401 if (!com_init(&sc
, filename
))
3403 #ifdef PRIVATE_NAME_MANGLING
3405 sc
.c_private
= base
->c_private
;
3407 sc
.c_private
= NULL
;
3409 compile_node(&sc
, n
);
3411 if ((TYPE(n
) == funcdef
|| TYPE(n
) == lambdef
) && sc
.c_errors
== 0) {
3413 sc
.c_flags
|= CO_NEWLOCALS
;
3415 else if (TYPE(n
) == classdef
)
3416 sc
.c_flags
|= CO_NEWLOCALS
;
3418 if (sc
.c_errors
== 0) {
3419 PyObject
*consts
, *names
, *varnames
, *filename
, *name
;
3420 consts
= PyList_AsTuple(sc
.c_consts
);
3421 names
= PyList_AsTuple(sc
.c_names
);
3422 varnames
= PyList_AsTuple(sc
.c_varnames
);
3423 filename
= PyString_InternFromString(sc
.c_filename
);
3424 name
= PyString_InternFromString(sc
.c_name
);
3425 if (!PyErr_Occurred())
3426 co
= PyCode_New(sc
.c_argcount
,
3440 Py_XDECREF(varnames
);
3441 Py_XDECREF(filename
);
3449 PyCode_Addr2Line(co
, addrq
)
3453 int size
= PyString_Size(co
->co_lnotab
) / 2;
3454 unsigned char *p
= (unsigned char*)PyString_AsString(co
->co_lnotab
);
3455 int line
= co
->co_firstlineno
;
3457 while (--size
>= 0) {