blob: f39c794ae3ef86e9b6e1a5892525c27eac97618f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011/* Compile an expression node to intermediate code */
12
Guido van Rossum3f5da241990-12-20 15:06:42 +000013/* XXX TO DO:
Guido van Rossum8b993a91997-01-17 21:04:03 +000014 XXX add __doc__ attribute == co_doc to code object attributes?
15 XXX (it's currently the first item of the co_const tuple)
Guido van Rossum3f5da241990-12-20 15:06:42 +000016 XXX Generate simple jump for break/return outside 'try...finally'
Guido van Rossum8b993a91997-01-17 21:04:03 +000017 XXX Allow 'continue' inside try-finally
Guido van Rossum8b993a91997-01-17 21:04:03 +000018 XXX New opcode for loading the initial index for a for loop
Guido van Rossum681d79a1995-07-18 14:51:37 +000019 XXX other JAR tricks?
Guido van Rossum3f5da241990-12-20 15:06:42 +000020*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000021
Guido van Rossum8ff077b1996-08-24 06:21:31 +000022#ifndef NO_PRIVATE_NAME_MANGLING
23#define PRIVATE_NAME_MANGLING
24#endif
25
Guido van Rossum79f25d91997-04-29 20:08:16 +000026#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000027
Guido van Rossum10dc2e81990-11-18 17:27:39 +000028#include "node.h"
29#include "token.h"
30#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
32#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000033#include "structmember.h"
34
35#include <ctype.h>
Jack Jansen41aa8e52000-07-03 21:39:47 +000036#ifdef HAVE_LIMITS_H
37#include <limits.h>
38#endif
Guido van Rossum282914b1991-04-04 10:42:56 +000039
Guido van Rossumb05a5c71997-05-07 17:46:13 +000040/* Three symbols from graminit.h are also defined in Python.h, with
41 Py_ prefixes to their names. Python.h can't include graminit.h
42 (which defines too many confusing symbols), but we can check here
43 that they haven't changed (which is very unlikely, but possible). */
44#if Py_single_input != single_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000045 #error "single_input has changed -- update Py_single_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000046#endif
47#if Py_file_input != file_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000048 #error "file_input has changed -- update Py_file_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000049#endif
50#if Py_eval_input != eval_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000051 #error "eval_input has changed -- update Py_eval_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000052#endif
53
Guido van Rossum8e793d91997-03-03 19:13:14 +000054int Py_OptimizeFlag = 0;
55
Guido van Rossum8861b741996-07-30 16:49:37 +000056#define OP_DELETE 0
57#define OP_ASSIGN 1
58#define OP_APPLY 2
59
Guido van Rossum79f25d91997-04-29 20:08:16 +000060#define OFF(x) offsetof(PyCodeObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000061
62static struct memberlist code_memberlist[] = {
Guido van Rossum681d79a1995-07-18 14:51:37 +000063 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
64 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
Guido van Rossum8b993a91997-01-17 21:04:03 +000065 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000066 {"co_flags", T_INT, OFF(co_flags), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000067 {"co_code", T_OBJECT, OFF(co_code), READONLY},
68 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
69 {"co_names", T_OBJECT, OFF(co_names), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000070 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000071 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000072 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossumda4eb5c1997-01-24 03:43:35 +000073 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
74 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000075 {NULL} /* Sentinel */
76};
77
Guido van Rossum79f25d91997-04-29 20:08:16 +000078static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +000079code_getattr(co, name)
Guido van Rossum79f25d91997-04-29 20:08:16 +000080 PyCodeObject *co;
Guido van Rossum3f5da241990-12-20 15:06:42 +000081 char *name;
82{
Guido van Rossum79f25d91997-04-29 20:08:16 +000083 return PyMember_Get((char *)co, code_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000084}
Guido van Rossum10dc2e81990-11-18 17:27:39 +000085
86static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000087code_dealloc(co)
Guido van Rossum79f25d91997-04-29 20:08:16 +000088 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +000089{
Guido van Rossum79f25d91997-04-29 20:08:16 +000090 Py_XDECREF(co->co_code);
91 Py_XDECREF(co->co_consts);
92 Py_XDECREF(co->co_names);
Guido van Rossum275558c1997-07-25 20:13:49 +000093 Py_XDECREF(co->co_varnames);
Guido van Rossum79f25d91997-04-29 20:08:16 +000094 Py_XDECREF(co->co_filename);
95 Py_XDECREF(co->co_name);
Guido van Rossum275558c1997-07-25 20:13:49 +000096 Py_XDECREF(co->co_lnotab);
Guido van Rossumb18618d2000-05-03 23:44:39 +000097 PyObject_DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +000098}
99
Guido van Rossum79f25d91997-04-29 20:08:16 +0000100static PyObject *
Guido van Rossum2dff9911992-09-03 20:50:59 +0000101code_repr(co)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000102 PyCodeObject *co;
Guido van Rossum2dff9911992-09-03 20:50:59 +0000103{
104 char buf[500];
105 int lineno = -1;
Guido van Rossum2dff9911992-09-03 20:50:59 +0000106 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +0000107 char *name = "???";
Guido van Rossumd076c731998-10-07 19:42:25 +0000108
Guido van Rossuma396a882000-04-07 01:21:36 +0000109 if (co->co_firstlineno != 0)
110 lineno = co->co_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000111 if (co->co_filename && PyString_Check(co->co_filename))
112 filename = PyString_AsString(co->co_filename);
113 if (co->co_name && PyString_Check(co->co_name))
114 name = PyString_AsString(co->co_name);
Fred Drake615ae552000-06-30 16:20:13 +0000115 sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
116 name, co, filename, lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000117 return PyString_FromString(buf);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000118}
119
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000120static int
121code_compare(co, cp)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000122 PyCodeObject *co, *cp;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000123{
124 int cmp;
Guido van Rossum44679592000-04-10 16:20:31 +0000125 cmp = PyObject_Compare(co->co_name, cp->co_name);
126 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000127 cmp = co->co_argcount - cp->co_argcount;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000128 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000129 cmp = co->co_nlocals - cp->co_nlocals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000130 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000131 cmp = co->co_flags - cp->co_flags;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000132 if (cmp) return cmp;
Guido van Rossumd076c731998-10-07 19:42:25 +0000133 cmp = PyObject_Compare(co->co_code, cp->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000134 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000135 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000136 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000137 cmp = PyObject_Compare(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000138 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000139 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000140 return cmp;
141}
142
143static long
144code_hash(co)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000145 PyCodeObject *co;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000146{
Guido van Rossum44679592000-04-10 16:20:31 +0000147 long h, h0, h1, h2, h3, h4;
148 h0 = PyObject_Hash(co->co_name);
149 if (h0 == -1) return -1;
Guido van Rossumd076c731998-10-07 19:42:25 +0000150 h1 = PyObject_Hash(co->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000151 if (h1 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000152 h2 = PyObject_Hash(co->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000153 if (h2 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000154 h3 = PyObject_Hash(co->co_names);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000155 if (h3 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000156 h4 = PyObject_Hash(co->co_varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000157 if (h4 == -1) return -1;
Guido van Rossum44679592000-04-10 16:20:31 +0000158 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^
Guido van Rossum681d79a1995-07-18 14:51:37 +0000159 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000160 if (h == -1) h = -2;
161 return h;
162}
163
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164PyTypeObject PyCode_Type = {
165 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000166 0,
167 "code",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 sizeof(PyCodeObject),
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000169 0,
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000170 (destructor)code_dealloc, /*tp_dealloc*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000171 0, /*tp_print*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000172 (getattrfunc)code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000173 0, /*tp_setattr*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000174 (cmpfunc)code_compare, /*tp_compare*/
175 (reprfunc)code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000176 0, /*tp_as_number*/
177 0, /*tp_as_sequence*/
178 0, /*tp_as_mapping*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000179 (hashfunc)code_hash, /*tp_hash*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000180};
181
Guido van Rossum644a12b1997-04-09 19:24:53 +0000182#define NAME_CHARS \
183 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
184
Guido van Rossum79f25d91997-04-29 20:08:16 +0000185PyCodeObject *
186PyCode_New(argcount, nlocals, stacksize, flags,
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000187 code, consts, names, varnames, filename, name,
188 firstlineno, lnotab)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000189 int argcount;
190 int nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000191 int stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000192 int flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000193 PyObject *code;
194 PyObject *consts;
195 PyObject *names;
196 PyObject *varnames;
197 PyObject *filename;
198 PyObject *name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000199 int firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000200 PyObject *lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000201{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000203 int i;
Guido van Rossumd076c731998-10-07 19:42:25 +0000204 PyBufferProcs *pb;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000205 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000206 if (argcount < 0 || nlocals < 0 ||
Guido van Rossumd076c731998-10-07 19:42:25 +0000207 code == NULL ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208 consts == NULL || !PyTuple_Check(consts) ||
209 names == NULL || !PyTuple_Check(names) ||
210 varnames == NULL || !PyTuple_Check(varnames) ||
211 name == NULL || !PyString_Check(name) ||
212 filename == NULL || !PyString_Check(filename) ||
213 lnotab == NULL || !PyString_Check(lnotab)) {
214 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000215 return NULL;
216 }
Guido van Rossumd076c731998-10-07 19:42:25 +0000217 pb = code->ob_type->tp_as_buffer;
218 if (pb == NULL ||
219 pb->bf_getreadbuffer == NULL ||
220 pb->bf_getsegcount == NULL ||
221 (*pb->bf_getsegcount)(code, NULL) != 1)
222 {
223 PyErr_BadInternalCall();
224 return NULL;
225 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000226 /* Make sure names and varnames are all strings, & intern them */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000227 for (i = PyTuple_Size(names); --i >= 0; ) {
228 PyObject *v = PyTuple_GetItem(names, i);
229 if (v == NULL || !PyString_Check(v)) {
230 PyErr_BadInternalCall();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000231 return NULL;
232 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000233 PyString_InternInPlace(&PyTuple_GET_ITEM(names, i));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000234 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000235 for (i = PyTuple_Size(varnames); --i >= 0; ) {
236 PyObject *v = PyTuple_GetItem(varnames, i);
237 if (v == NULL || !PyString_Check(v)) {
238 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000239 return NULL;
240 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000241 PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i));
242 }
243 /* Intern selected string constants */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244 for (i = PyTuple_Size(consts); --i >= 0; ) {
245 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000246 char *p;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000247 if (!PyString_Check(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000248 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000249 p = PyString_AsString(v);
Guido van Rossum582acec2000-06-28 22:07:35 +0000250 if (strspn(p, NAME_CHARS)
251 != (size_t)PyString_Size(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000252 continue;
253 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000254 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000255 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000256 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000257 co->co_argcount = argcount;
258 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000259 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000260 co->co_flags = flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000261 Py_INCREF(code);
Guido van Rossumd076c731998-10-07 19:42:25 +0000262 co->co_code = code;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000263 Py_INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000264 co->co_consts = consts;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000265 Py_INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000266 co->co_names = names;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000267 Py_INCREF(varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000268 co->co_varnames = varnames;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000269 Py_INCREF(filename);
Guido van Rossuma082ce41991-06-04 19:41:56 +0000270 co->co_filename = filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000271 Py_INCREF(name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000272 co->co_name = name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000273 co->co_firstlineno = firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000274 Py_INCREF(lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000275 co->co_lnotab = lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000276 }
277 return co;
278}
279
280
281/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000282
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000283struct compiling {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000284 PyObject *c_code; /* string */
285 PyObject *c_consts; /* list of objects */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000286 PyObject *c_const_dict; /* inverse of c_consts */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000287 PyObject *c_names; /* list of strings (names) */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000288 PyObject *c_name_dict; /* inverse of c_names */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000289 PyObject *c_globals; /* dictionary (value=None) */
290 PyObject *c_locals; /* dictionary (value=localID) */
291 PyObject *c_varnames; /* list (inverse of c_locals) */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000292 int c_nlocals; /* index of next local */
293 int c_argcount; /* number of top-level arguments */
294 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000295 int c_nexti; /* index into c_code */
296 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000297 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000298 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000299 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000300 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000301 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000302 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000303 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000304 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000305 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000306 int c_stacklevel; /* Current stack level */
307 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000308 int c_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309 PyObject *c_lnotab; /* Table mapping address to line number */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000310 int c_last_addr, c_last_line, c_lnotab_next;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000311#ifdef PRIVATE_NAME_MANGLING
312 char *c_private; /* for private name mangling */
313#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000314};
315
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000316
Guido van Rossum452a9831996-09-17 14:32:04 +0000317/* Error message including line number */
318
319static void
320com_error(c, exc, msg)
321 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000322 PyObject *exc;
Guido van Rossum452a9831996-09-17 14:32:04 +0000323 char *msg;
324{
Guido van Rossum582acec2000-06-28 22:07:35 +0000325 size_t n = strlen(msg);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000326 PyObject *v;
Guido van Rossum452a9831996-09-17 14:32:04 +0000327 char buffer[30];
328 char *s;
Guido van Rossum635abd21997-01-06 22:56:52 +0000329 c->c_errors++;
Guido van Rossum452a9831996-09-17 14:32:04 +0000330 if (c->c_lineno <= 1) {
331 /* Unknown line number or single interactive command */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000332 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000333 return;
334 }
335 sprintf(buffer, " (line %d)", c->c_lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000336 v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer));
Guido van Rossum452a9831996-09-17 14:32:04 +0000337 if (v == NULL)
338 return; /* MemoryError, too bad */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000339 s = PyString_AS_STRING((PyStringObject *)v);
Guido van Rossum452a9831996-09-17 14:32:04 +0000340 strcpy(s, msg);
341 strcat(s, buffer);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000342 PyErr_SetObject(exc, v);
343 Py_DECREF(v);
Guido van Rossum452a9831996-09-17 14:32:04 +0000344}
345
346
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000347/* Interface to the block stack */
348
349static void
350block_push(c, type)
351 struct compiling *c;
352 int type;
353{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000354 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000355 com_error(c, PyExc_SystemError,
356 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000357 }
358 else {
359 c->c_block[c->c_nblocks++] = type;
360 }
361}
362
363static void
364block_pop(c, type)
365 struct compiling *c;
366 int type;
367{
368 if (c->c_nblocks > 0)
369 c->c_nblocks--;
370 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000371 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000372 }
373}
374
375
Guido van Rossum681d79a1995-07-18 14:51:37 +0000376/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000377
Tim Petersdbd9ba62000-07-09 03:09:57 +0000378static int com_init(struct compiling *, char *);
379static void com_free(struct compiling *);
380static void com_push(struct compiling *, int);
381static void com_pop(struct compiling *, int);
382static void com_done(struct compiling *);
383static void com_node(struct compiling *, struct _node *);
384static void com_factor(struct compiling *, struct _node *);
385static void com_addbyte(struct compiling *, int);
386static void com_addint(struct compiling *, int);
387static void com_addoparg(struct compiling *, int, int);
388static void com_addfwref(struct compiling *, int, int *);
389static void com_backpatch(struct compiling *, int);
390static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
391static int com_addconst(struct compiling *, PyObject *);
392static int com_addname(struct compiling *, PyObject *);
393static void com_addopname(struct compiling *, int, node *);
394static void com_list(struct compiling *, node *, int);
395static int com_argdefs(struct compiling *, node *);
396static int com_newlocal(struct compiling *, char *);
397static PyCodeObject *icompile(struct _node *, struct compiling *);
398static PyCodeObject *jcompile(struct _node *, char *,
399 struct compiling *);
400static PyObject *parsestrplus(node *);
401static PyObject *parsestr(char *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000402
403static int
Guido van Rossum590baa41993-11-30 13:40:46 +0000404com_init(c, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000405 struct compiling *c;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000406 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000407{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000408 memset((void *)c, '\0', sizeof(struct compiling));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
410 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000411 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000412 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000413 goto fail;
414 if ((c->c_const_dict = PyDict_New()) == NULL)
415 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000416 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000417 goto fail;
418 if ((c->c_name_dict = PyDict_New()) == NULL)
419 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 if ((c->c_globals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000421 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000422 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000423 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000424 if ((c->c_varnames = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000425 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000426 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
427 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000428 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000429 c->c_nlocals = 0;
430 c->c_argcount = 0;
431 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000432 c->c_nexti = 0;
433 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000434 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000435 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000436 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000437 c->c_begin = 0;
438 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000439 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000440 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000441 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000442 c->c_stacklevel = 0;
443 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000444 c->c_firstlineno = 0;
445 c->c_last_addr = 0;
446 c->c_last_line = 0;
447 c-> c_lnotab_next = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000448 return 1;
449
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000450 fail:
451 com_free(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000452 return 0;
453}
454
455static void
456com_free(c)
457 struct compiling *c;
458{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000459 Py_XDECREF(c->c_code);
460 Py_XDECREF(c->c_consts);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000461 Py_XDECREF(c->c_const_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462 Py_XDECREF(c->c_names);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000463 Py_XDECREF(c->c_name_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 Py_XDECREF(c->c_globals);
465 Py_XDECREF(c->c_locals);
466 Py_XDECREF(c->c_varnames);
467 Py_XDECREF(c->c_lnotab);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000468}
469
470static void
Guido van Rossum8b993a91997-01-17 21:04:03 +0000471com_push(c, n)
472 struct compiling *c;
473 int n;
474{
475 c->c_stacklevel += n;
476 if (c->c_stacklevel > c->c_maxstacklevel)
477 c->c_maxstacklevel = c->c_stacklevel;
478}
479
480static void
481com_pop(c, n)
482 struct compiling *c;
483 int n;
484{
485 if (c->c_stacklevel < n) {
Guido van Rossum90f827c1998-08-25 18:22:17 +0000486 /* fprintf(stderr,
Guido van Rossum8b993a91997-01-17 21:04:03 +0000487 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
488 c->c_filename, c->c_lineno,
Guido van Rossum90f827c1998-08-25 18:22:17 +0000489 c->c_nexti, c->c_stacklevel, n); */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000490 c->c_stacklevel = 0;
491 }
492 else
493 c->c_stacklevel -= n;
494}
495
496static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000497com_done(c)
498 struct compiling *c;
499{
500 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000501 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000502 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000503 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000504}
505
506static void
507com_addbyte(c, byte)
508 struct compiling *c;
509 int byte;
510{
511 int len;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000512 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000513 if (byte < 0 || byte > 255) {
Guido van Rossum01cfd441991-10-20 20:12:38 +0000514 /*
Guido van Rossum3f5da241990-12-20 15:06:42 +0000515 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000516 fatal("com_addbyte: byte out of range");
Guido van Rossum01cfd441991-10-20 20:12:38 +0000517 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000518 com_error(c, PyExc_SystemError,
519 "com_addbyte: byte out of range");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000520 }
521 if (c->c_code == NULL)
522 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000523 len = PyString_Size(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000524 if (c->c_nexti >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000525 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000526 c->c_errors++;
527 return;
528 }
529 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000531}
532
533static void
534com_addint(c, x)
535 struct compiling *c;
536 int x;
537{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000538 com_addbyte(c, x & 0xff);
539 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000540}
541
542static void
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000543com_add_lnotab(c, addr, line)
544 struct compiling *c;
545 int addr;
546 int line;
547{
548 int size;
549 char *p;
550 if (c->c_lnotab == NULL)
551 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 size = PyString_Size(c->c_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000553 if (c->c_lnotab_next+2 > size) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000554 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000555 c->c_errors++;
556 return;
557 }
558 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000560 *p++ = addr;
561 *p++ = line;
562 c->c_lnotab_next += 2;
563}
564
565static void
566com_set_lineno(c, lineno)
567 struct compiling *c;
568 int lineno;
569{
570 c->c_lineno = lineno;
571 if (c->c_firstlineno == 0) {
572 c->c_firstlineno = c->c_last_line = lineno;
573 }
574 else {
575 int incr_addr = c->c_nexti - c->c_last_addr;
576 int incr_line = lineno - c->c_last_line;
577 while (incr_addr > 0 || incr_line > 0) {
578 int trunc_addr = incr_addr;
579 int trunc_line = incr_line;
580 if (trunc_addr > 255)
581 trunc_addr = 255;
582 if (trunc_line > 255)
583 trunc_line = 255;
584 com_add_lnotab(c, trunc_addr, trunc_line);
585 incr_addr -= trunc_addr;
586 incr_line -= trunc_line;
587 }
588 c->c_last_addr = c->c_nexti;
589 c->c_last_line = lineno;
590 }
591}
592
593static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000594com_addoparg(c, op, arg)
595 struct compiling *c;
596 int op;
597 int arg;
598{
Guido van Rossum8e793d91997-03-03 19:13:14 +0000599 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000600 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000601 if (Py_OptimizeFlag)
602 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000603 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000604 com_addbyte(c, op);
605 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000606}
607
608static void
609com_addfwref(c, op, p_anchor)
610 struct compiling *c;
611 int op;
612 int *p_anchor;
613{
614 /* Compile a forward reference for backpatching */
615 int here;
616 int anchor;
617 com_addbyte(c, op);
618 here = c->c_nexti;
619 anchor = *p_anchor;
620 *p_anchor = here;
621 com_addint(c, anchor == 0 ? 0 : here - anchor);
622}
623
624static void
625com_backpatch(c, anchor)
626 struct compiling *c;
627 int anchor; /* Must be nonzero */
628{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000630 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000631 int dist;
632 int prev;
633 for (;;) {
634 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000635 prev = code[anchor] + (code[anchor+1] << 8);
636 dist = target - (anchor+2);
637 code[anchor] = dist & 0xff;
638 code[anchor+1] = dist >> 8;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000639 if (!prev)
640 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000641 anchor -= prev;
642 }
643}
644
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000645/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000646
647static int
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000648com_add(c, list, dict, v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000649 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000650 PyObject *list;
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000651 PyObject *dict;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000653{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000654 PyObject *w, *t, *np=NULL;
655 long n;
656
657 t = Py_BuildValue("(OO)", v, v->ob_type);
658 if (t == NULL)
659 goto fail;
660 w = PyDict_GetItem(dict, t);
661 if (w != NULL) {
662 n = PyInt_AsLong(w);
663 } else {
664 n = PyList_Size(list);
665 np = PyInt_FromLong(n);
666 if (np == NULL)
667 goto fail;
668 if (PyList_Append(list, v) != 0)
669 goto fail;
670 if (PyDict_SetItem(dict, t, np) != 0)
671 goto fail;
672 Py_DECREF(np);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000673 }
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000674 Py_DECREF(t);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000675 return n;
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000676 fail:
677 Py_XDECREF(np);
678 Py_XDECREF(t);
679 c->c_errors++;
680 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000681}
682
683static int
684com_addconst(c, v)
685 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000687{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000688 return com_add(c, c->c_consts, c->c_const_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000689}
690
691static int
692com_addname(c, v)
693 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000695{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000696 return com_add(c, c->c_names, c->c_name_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000697}
698
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000699#ifdef PRIVATE_NAME_MANGLING
700static int
701com_mangle(c, name, buffer, maxlen)
702 struct compiling *c;
703 char *name;
704 char *buffer;
Guido van Rossum582acec2000-06-28 22:07:35 +0000705 size_t maxlen;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000706{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000707 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000708 This is independent from how the name is used. */
709 char *p;
Guido van Rossum582acec2000-06-28 22:07:35 +0000710 size_t nlen, plen;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000711 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000712 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000713 return 0; /* Don't mangle __extremely_long_names */
714 if (name[nlen-1] == '_' && name[nlen-2] == '_')
715 return 0; /* Don't mangle __whatever__ */
716 p = c->c_private;
717 /* Strip leading underscores from class name */
718 while (*p == '_')
719 p++;
720 if (*p == '\0')
721 return 0; /* Don't mangle if class is just underscores */
722 plen = strlen(p);
723 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000724 plen = maxlen-nlen-2; /* Truncate class name if too long */
725 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000726 buffer[0] = '_';
727 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000728 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000729 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
730 return 1;
731}
732#endif
733
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000734static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000735com_addopnamestr(c, op, name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000736 struct compiling *c;
737 int op;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000738 char *name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000739{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000740 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000741 int i;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000742#ifdef PRIVATE_NAME_MANGLING
743 char buffer[256];
744 if (name != NULL && name[0] == '_' && name[1] == '_' &&
745 c->c_private != NULL &&
Guido van Rossum582acec2000-06-28 22:07:35 +0000746 com_mangle(c, name, buffer, sizeof(buffer)))
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000747 name = buffer;
748#endif
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000749 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000750 c->c_errors++;
751 i = 255;
752 }
753 else {
754 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000756 }
Guido van Rossumc5e96291991-12-10 13:53:51 +0000757 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
758 switch (op) {
759 case LOAD_NAME:
760 case STORE_NAME:
761 case DELETE_NAME:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 if (PyDict_GetItemString(c->c_globals, name) != NULL) {
Guido van Rossumc5e96291991-12-10 13:53:51 +0000763 switch (op) {
764 case LOAD_NAME: op = LOAD_GLOBAL; break;
765 case STORE_NAME: op = STORE_GLOBAL; break;
766 case DELETE_NAME: op = DELETE_GLOBAL; break;
767 }
768 }
769 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000770 com_addoparg(c, op, i);
771}
772
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000773static void
774com_addopname(c, op, n)
775 struct compiling *c;
776 int op;
777 node *n;
778{
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000779 char *name;
780 char buffer[1000];
781 /* XXX it is possible to write this code without the 1000
782 chars on the total length of dotted names, I just can't be
783 bothered right now */
784 if (TYPE(n) == STAR)
785 name = "*";
786 else if (TYPE(n) == dotted_name) {
787 char *p = buffer;
788 int i;
789 name = buffer;
790 for (i = 0; i < NCH(n); i += 2) {
791 char *s = STR(CHILD(n, i));
792 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +0000794 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +0000795 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000796 break;
797 }
798 if (p != buffer)
799 *p++ = '.';
800 strcpy(p, s);
801 p = strchr(p, '\0');
802 }
803 }
804 else {
805 REQ(n, NAME);
806 name = STR(n);
807 }
808 com_addopnamestr(c, op, name);
809}
810
Guido van Rossum79f25d91997-04-29 20:08:16 +0000811static PyObject *
Guido van Rossum452a9831996-09-17 14:32:04 +0000812parsenumber(co, s)
813 struct compiling *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000814 char *s;
815{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000816 extern double atof(const char *);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000817 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000818 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000819 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +0000820#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +0000821 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +0000822 int imflag;
823#endif
824
Guido van Rossum282914b1991-04-04 10:42:56 +0000825 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000826 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +0000827#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +0000828 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +0000829#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000830 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +0000831 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000832 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000834 else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000835 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +0000836 if (*end == '\0') {
837 if (errno != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000838 com_error(co, PyExc_OverflowError,
Guido van Rossum452a9831996-09-17 14:32:04 +0000839 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000840 return NULL;
841 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +0000843 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000844 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +0000845#ifndef WITHOUT_COMPLEX
846 if (imflag) {
847 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000848 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +0000849 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +0000850 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000851 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +0000852 }
Guido van Rossumac1fc951997-10-08 15:23:55 +0000853 else
Guido van Rossum50564e81996-01-12 01:13:16 +0000854#endif
Guido van Rossumac1fc951997-10-08 15:23:55 +0000855 {
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000856 PyFPE_START_PROTECT("atof", return 0)
857 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +0000858 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000859 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000860 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000861}
862
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863static PyObject *
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000864parsestr(s)
865 char *s;
866{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000867 PyObject *v;
Guido van Rossum582acec2000-06-28 22:07:35 +0000868 size_t len;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000869 char *buf;
870 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000871 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000872 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +0000873 int first = *s;
874 int quote = first;
Guido van Rossum5aa88f02000-03-10 23:01:36 +0000875 int rawmode = 0;
876 int unicode = 0;
877 if (isalpha(quote) || quote == '_') {
878 if (quote == 'u' || quote == 'U') {
879 quote = *++s;
880 unicode = 1;
881 }
882 if (quote == 'r' || quote == 'R') {
883 quote = *++s;
884 rawmode = 1;
885 }
886 }
Guido van Rossum8054fad1993-10-26 15:19:44 +0000887 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000888 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000889 return NULL;
890 }
891 s++;
892 len = strlen(s);
Guido van Rossum582acec2000-06-28 22:07:35 +0000893 if (len > INT_MAX) {
894 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
895 return NULL;
896 }
Guido van Rossum8054fad1993-10-26 15:19:44 +0000897 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000898 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000899 return NULL;
900 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000901 if (len >= 4 && s[0] == quote && s[1] == quote) {
902 s += 2;
903 len -= 2;
904 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000905 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000906 return NULL;
907 }
908 }
Guido van Rossumfdc8bdb2000-05-01 17:54:56 +0000909 if (unicode || Py_UnicodeFlag) {
Guido van Rossum5aa88f02000-03-10 23:01:36 +0000910 if (rawmode)
911 return PyUnicode_DecodeRawUnicodeEscape(
912 s, len, NULL);
913 else
914 return PyUnicode_DecodeUnicodeEscape(
915 s, len, NULL);
916 }
917 else if (rawmode || strchr(s, '\\') == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000918 return PyString_FromStringAndSize(s, len);
Guido van Rossum5aa88f02000-03-10 23:01:36 +0000919 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000920 v = PyString_FromStringAndSize((char *)NULL, len);
921 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000922 end = s + len;
923 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000924 if (*s != '\\') {
925 *p++ = *s++;
926 continue;
927 }
928 s++;
929 switch (*s++) {
930 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000931 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000932 case '\\': *p++ = '\\'; break;
933 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000934 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000935 case 'b': *p++ = '\b'; break;
936 case 'f': *p++ = '\014'; break; /* FF */
937 case 't': *p++ = '\t'; break;
938 case 'n': *p++ = '\n'; break;
939 case 'r': *p++ = '\r'; break;
940 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000941 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
942 case '0': case '1': case '2': case '3':
943 case '4': case '5': case '6': case '7':
944 c = s[-1] - '0';
945 if ('0' <= *s && *s <= '7') {
946 c = (c<<3) + *s++ - '0';
947 if ('0' <= *s && *s <= '7')
948 c = (c<<3) + *s++ - '0';
949 }
950 *p++ = c;
951 break;
952 case 'x':
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000953 if (isxdigit(Py_CHARMASK(*s))) {
Guido van Rossumed1100f1997-10-20 23:24:07 +0000954 unsigned int x = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000955 do {
Guido van Rossumed1100f1997-10-20 23:24:07 +0000956 c = Py_CHARMASK(*s);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000957 s++;
Guido van Rossumed1100f1997-10-20 23:24:07 +0000958 x = (x<<4) & ~0xF;
959 if (isdigit(c))
960 x += c - '0';
961 else if (islower(c))
962 x += 10 + c - 'a';
963 else
964 x += 10 + c - 'A';
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000965 } while (isxdigit(Py_CHARMASK(*s)));
Guido van Rossumed1100f1997-10-20 23:24:07 +0000966 *p++ = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000967 break;
968 }
969 /* FALLTHROUGH */
970 default: *p++ = '\\'; *p++ = s[-1]; break;
971 }
972 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000973 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000974 return v;
975}
976
Guido van Rossum79f25d91997-04-29 20:08:16 +0000977static PyObject *
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000978parsestrplus(n)
979 node *n;
980{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000981 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000982 int i;
983 REQ(CHILD(n, 0), STRING);
984 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
985 /* String literal concatenation */
Fred Drake4e998bc2000-04-13 14:10:44 +0000986 for (i = 1; i < NCH(n); i++) {
987 PyObject *s;
988 s = parsestr(STR(CHILD(n, i)));
989 if (s == NULL)
990 goto onError;
991 if (PyString_Check(v) && PyString_Check(s)) {
992 PyString_ConcatAndDel(&v, s);
993 if (v == NULL)
994 goto onError;
995 }
996 else {
997 PyObject *temp;
998 temp = PyUnicode_Concat(v, s);
999 Py_DECREF(s);
1000 if (temp == NULL)
1001 goto onError;
1002 Py_DECREF(v);
1003 v = temp;
1004 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001005 }
1006 }
1007 return v;
Fred Drake4e998bc2000-04-13 14:10:44 +00001008
1009 onError:
1010 Py_XDECREF(v);
1011 return NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001012}
1013
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001014static void
1015com_list_constructor(c, n)
1016 struct compiling *c;
1017 node *n;
1018{
1019 int len;
1020 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001021 if (TYPE(n) != testlist)
1022 REQ(n, exprlist);
1023 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
1024 len = (NCH(n) + 1) / 2;
1025 for (i = 0; i < NCH(n); i += 2)
1026 com_node(c, CHILD(n, i));
1027 com_addoparg(c, BUILD_LIST, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001028 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001029}
1030
1031static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001032com_dictmaker(c, n)
1033 struct compiling *c;
1034 node *n;
1035{
1036 int i;
1037 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1038 for (i = 0; i+2 < NCH(n); i += 4) {
1039 /* We must arrange things just right for STORE_SUBSCR.
1040 It wants the stack to look like (value) (dict) (key) */
1041 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001042 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001043 com_node(c, CHILD(n, i+2)); /* value */
1044 com_addbyte(c, ROT_TWO);
1045 com_node(c, CHILD(n, i)); /* key */
1046 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001047 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001048 }
1049}
1050
1051static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001052com_atom(c, n)
1053 struct compiling *c;
1054 node *n;
1055{
1056 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001057 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001058 int i;
1059 REQ(n, atom);
1060 ch = CHILD(n, 0);
1061 switch (TYPE(ch)) {
1062 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001063 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001064 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001065 com_push(c, 1);
1066 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001067 else
1068 com_node(c, CHILD(n, 1));
1069 break;
1070 case LSQB:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001071 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001072 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001073 com_push(c, 1);
1074 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001075 else
1076 com_list_constructor(c, CHILD(n, 1));
1077 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001078 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001079 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001080 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001081 if (TYPE(CHILD(n, 1)) != RBRACE)
1082 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001083 break;
1084 case BACKQUOTE:
1085 com_node(c, CHILD(n, 1));
1086 com_addbyte(c, UNARY_CONVERT);
1087 break;
1088 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001089 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001090 i = 255;
1091 }
1092 else {
1093 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001094 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001095 }
1096 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001097 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001098 break;
1099 case STRING:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001100 v = parsestrplus(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001101 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001102 c->c_errors++;
1103 i = 255;
1104 }
1105 else {
1106 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001107 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001108 }
1109 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001110 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001111 break;
1112 case NAME:
1113 com_addopname(c, LOAD_NAME, ch);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001114 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001115 break;
1116 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001117 /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118 com_error(c, PyExc_SystemError,
1119 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001120 }
1121}
1122
1123static void
1124com_slice(c, n, op)
1125 struct compiling *c;
1126 node *n;
1127 int op;
1128{
1129 if (NCH(n) == 1) {
1130 com_addbyte(c, op);
1131 }
1132 else if (NCH(n) == 2) {
1133 if (TYPE(CHILD(n, 0)) != COLON) {
1134 com_node(c, CHILD(n, 0));
1135 com_addbyte(c, op+1);
1136 }
1137 else {
1138 com_node(c, CHILD(n, 1));
1139 com_addbyte(c, op+2);
1140 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001141 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001142 }
1143 else {
1144 com_node(c, CHILD(n, 0));
1145 com_node(c, CHILD(n, 2));
1146 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001147 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001148 }
1149}
1150
Guido van Rossum635abd21997-01-06 22:56:52 +00001151static void
1152com_argument(c, n, pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001153 struct compiling *c;
1154 node *n; /* argument */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001155 PyObject **pkeywords;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001156{
1157 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001158 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001159 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001160 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001161 com_error(c, PyExc_SyntaxError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001162 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001163 }
1164 else {
1165 com_node(c, CHILD(n, 0));
1166 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001167 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001168 }
1169 m = n;
1170 do {
1171 m = CHILD(m, 0);
1172 } while (NCH(m) == 1);
1173 if (TYPE(m) != NAME) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 com_error(c, PyExc_SyntaxError,
1175 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001176 }
1177 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001178 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001179 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180 *pkeywords = PyDict_New();
Guido van Rossum635abd21997-01-06 22:56:52 +00001181 if (v == NULL || *pkeywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001182 c->c_errors++;
1183 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184 if (PyDict_GetItem(*pkeywords, v) != NULL)
1185 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001186 "duplicate keyword argument");
1187 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001188 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001189 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001190 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001191 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001192 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001193 }
1194 }
1195 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001196}
1197
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001198static void
1199com_call_function(c, n)
1200 struct compiling *c;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001201 node *n; /* EITHER arglist OR ')' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001202{
1203 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001204 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001205 }
1206 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001207 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001208 int i, na, nk;
Guido van Rossumca906051998-12-10 16:56:22 +00001209 int lineno = n->n_lineno;
Jeremy Hylton76901512000-03-28 23:49:17 +00001210 int star_flag = 0;
1211 int starstar_flag = 0;
1212 int opcode;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001213 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001214 na = 0;
1215 nk = 0;
1216 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumca906051998-12-10 16:56:22 +00001217 node *ch = CHILD(n, i);
Jeremy Hylton76901512000-03-28 23:49:17 +00001218 if (TYPE(ch) == STAR ||
1219 TYPE(ch) == DOUBLESTAR)
1220 break;
Guido van Rossumca906051998-12-10 16:56:22 +00001221 if (ch->n_lineno != lineno) {
1222 lineno = ch->n_lineno;
1223 com_addoparg(c, SET_LINENO, lineno);
1224 }
1225 com_argument(c, ch, &keywords);
Guido van Rossum635abd21997-01-06 22:56:52 +00001226 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001227 na++;
1228 else
1229 nk++;
1230 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001231 Py_XDECREF(keywords);
Jeremy Hylton76901512000-03-28 23:49:17 +00001232 while (i < NCH(n)) {
1233 node *tok = CHILD(n, i);
1234 node *ch = CHILD(n, i+1);
1235 i += 3;
1236 switch (TYPE(tok)) {
1237 case STAR: star_flag = 1; break;
1238 case DOUBLESTAR: starstar_flag = 1; break;
1239 }
1240 com_node(c, ch);
1241 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001242 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001243 com_error(c, PyExc_SyntaxError,
1244 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001245 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001246 if (star_flag || starstar_flag)
Jeremy Hyltone4fb9582000-03-29 00:10:44 +00001247 opcode = CALL_FUNCTION_VAR - 1 +
Jeremy Hylton76901512000-03-28 23:49:17 +00001248 star_flag + (starstar_flag << 1);
1249 else
1250 opcode = CALL_FUNCTION;
1251 com_addoparg(c, opcode, na | (nk << 8));
1252 com_pop(c, na + 2*nk + star_flag + starstar_flag);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001253 }
1254}
1255
1256static void
1257com_select_member(c, n)
1258 struct compiling *c;
1259 node *n;
1260{
1261 com_addopname(c, LOAD_ATTR, n);
1262}
1263
1264static void
Guido van Rossum8861b741996-07-30 16:49:37 +00001265com_sliceobj(c, n)
1266 struct compiling *c;
1267 node *n;
1268{
1269 int i=0;
1270 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001271 node *ch;
1272
1273 /* first argument */
1274 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001275 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001276 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001277 i++;
1278 }
1279 else {
1280 com_node(c, CHILD(n,i));
1281 i++;
1282 REQ(CHILD(n,i),COLON);
1283 i++;
1284 }
1285 /* second argument */
1286 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1287 com_node(c, CHILD(n,i));
1288 i++;
1289 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001290 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001291 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001292 com_push(c, 1);
1293 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001294 /* remaining arguments */
1295 for (; i < NCH(n); i++) {
1296 ns++;
1297 ch=CHILD(n,i);
1298 REQ(ch, sliceop);
1299 if (NCH(ch) == 1) {
1300 /* right argument of ':' missing */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001301 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001302 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001303 }
1304 else
1305 com_node(c, CHILD(ch,1));
1306 }
1307 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001308 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001309}
1310
1311static void
1312com_subscript(c, n)
1313 struct compiling *c;
1314 node *n;
1315{
1316 node *ch;
1317 REQ(n, subscript);
1318 ch = CHILD(n,0);
1319 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001320 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001321 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001322 com_push(c, 1);
1323 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001324 else {
1325 /* check for slice */
1326 if ((TYPE(ch) == COLON || NCH(n) > 1))
1327 com_sliceobj(c, n);
1328 else {
1329 REQ(ch, test);
1330 com_node(c, ch);
1331 }
1332 }
1333}
1334
1335static void
1336com_subscriptlist(c, n, assigning)
1337 struct compiling *c;
1338 node *n;
1339 int assigning;
1340{
1341 int i, op;
1342 REQ(n, subscriptlist);
1343 /* Check to make backward compatible slice behavior for '[i:j]' */
1344 if (NCH(n) == 1) {
1345 node *sub = CHILD(n, 0); /* subscript */
1346 /* Make it is a simple slice.
Guido van Rossum8b993a91997-01-17 21:04:03 +00001347 Should have exactly one colon. */
Guido van Rossum8861b741996-07-30 16:49:37 +00001348 if ((TYPE(CHILD(sub, 0)) == COLON
1349 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
Guido van Rossum8b993a91997-01-17 21:04:03 +00001350 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1351 {
Guido van Rossum8861b741996-07-30 16:49:37 +00001352 if (assigning == OP_APPLY)
1353 op = SLICE;
1354 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001355 op = ((assigning == OP_ASSIGN) ?
1356 STORE_SLICE : DELETE_SLICE);
Guido van Rossum8861b741996-07-30 16:49:37 +00001357 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001358 if (op == STORE_SLICE)
1359 com_pop(c, 2);
1360 else if (op == DELETE_SLICE)
1361 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001362 return;
1363 }
1364 }
1365 /* Else normal subscriptlist. Compile each subscript. */
1366 for (i = 0; i < NCH(n); i += 2)
1367 com_subscript(c, CHILD(n, i));
1368 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001369 if (NCH(n) > 1) {
1370 i = (NCH(n)+1) / 2;
1371 com_addoparg(c, BUILD_TUPLE, i);
1372 com_pop(c, i-1);
1373 }
1374 if (assigning == OP_APPLY) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001375 op = BINARY_SUBSCR;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001376 i = 1;
1377 }
1378 else if (assigning == OP_ASSIGN) {
1379 op = STORE_SUBSCR;
1380 i = 3;
1381 }
1382 else {
1383 op = DELETE_SUBSCR;
1384 i = 2;
1385 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001386 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001387 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001388}
1389
1390static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001391com_apply_trailer(c, n)
1392 struct compiling *c;
1393 node *n;
1394{
1395 REQ(n, trailer);
1396 switch (TYPE(CHILD(n, 0))) {
1397 case LPAR:
1398 com_call_function(c, CHILD(n, 1));
1399 break;
1400 case DOT:
1401 com_select_member(c, CHILD(n, 1));
1402 break;
1403 case LSQB:
Guido van Rossum8861b741996-07-30 16:49:37 +00001404 com_subscriptlist(c, CHILD(n, 1), OP_APPLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001405 break;
1406 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001407 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001408 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001409 }
1410}
1411
1412static void
Guido van Rossum50564e81996-01-12 01:13:16 +00001413com_power(c, n)
1414 struct compiling *c;
1415 node *n;
1416{
1417 int i;
1418 REQ(n, power);
1419 com_atom(c, CHILD(n, 0));
1420 for (i = 1; i < NCH(n); i++) {
1421 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1422 com_factor(c, CHILD(n, i+1));
1423 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001424 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001425 break;
1426 }
1427 else
1428 com_apply_trailer(c, CHILD(n, i));
1429 }
1430}
1431
1432static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001433com_factor(c, n)
1434 struct compiling *c;
1435 node *n;
1436{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001437 REQ(n, factor);
1438 if (TYPE(CHILD(n, 0)) == PLUS) {
1439 com_factor(c, CHILD(n, 1));
1440 com_addbyte(c, UNARY_POSITIVE);
1441 }
1442 else if (TYPE(CHILD(n, 0)) == MINUS) {
1443 com_factor(c, CHILD(n, 1));
1444 com_addbyte(c, UNARY_NEGATIVE);
1445 }
Guido van Rossum7928cd71991-10-24 14:59:31 +00001446 else if (TYPE(CHILD(n, 0)) == TILDE) {
1447 com_factor(c, CHILD(n, 1));
1448 com_addbyte(c, UNARY_INVERT);
1449 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001450 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001451 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001452 }
1453}
1454
1455static void
1456com_term(c, n)
1457 struct compiling *c;
1458 node *n;
1459{
1460 int i;
1461 int op;
1462 REQ(n, term);
1463 com_factor(c, CHILD(n, 0));
1464 for (i = 2; i < NCH(n); i += 2) {
1465 com_factor(c, CHILD(n, i));
1466 switch (TYPE(CHILD(n, i-1))) {
1467 case STAR:
1468 op = BINARY_MULTIPLY;
1469 break;
1470 case SLASH:
1471 op = BINARY_DIVIDE;
1472 break;
1473 case PERCENT:
1474 op = BINARY_MODULO;
1475 break;
1476 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001477 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001478 "com_term: operator not *, / or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001479 op = 255;
1480 }
1481 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001482 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001483 }
1484}
1485
1486static void
1487com_arith_expr(c, n)
1488 struct compiling *c;
1489 node *n;
1490{
1491 int i;
1492 int op;
1493 REQ(n, arith_expr);
1494 com_term(c, CHILD(n, 0));
1495 for (i = 2; i < NCH(n); i += 2) {
1496 com_term(c, CHILD(n, i));
1497 switch (TYPE(CHILD(n, i-1))) {
1498 case PLUS:
1499 op = BINARY_ADD;
1500 break;
1501 case MINUS:
1502 op = BINARY_SUBTRACT;
1503 break;
1504 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001505 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001506 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001507 op = 255;
1508 }
1509 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001510 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001511 }
1512}
1513
1514static void
1515com_shift_expr(c, n)
1516 struct compiling *c;
1517 node *n;
1518{
1519 int i;
1520 int op;
1521 REQ(n, shift_expr);
1522 com_arith_expr(c, CHILD(n, 0));
1523 for (i = 2; i < NCH(n); i += 2) {
1524 com_arith_expr(c, CHILD(n, i));
1525 switch (TYPE(CHILD(n, i-1))) {
1526 case LEFTSHIFT:
1527 op = BINARY_LSHIFT;
1528 break;
1529 case RIGHTSHIFT:
1530 op = BINARY_RSHIFT;
1531 break;
1532 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001534 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001535 op = 255;
1536 }
1537 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001538 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001539 }
1540}
1541
1542static void
1543com_and_expr(c, n)
1544 struct compiling *c;
1545 node *n;
1546{
1547 int i;
1548 int op;
1549 REQ(n, and_expr);
1550 com_shift_expr(c, CHILD(n, 0));
1551 for (i = 2; i < NCH(n); i += 2) {
1552 com_shift_expr(c, CHILD(n, i));
1553 if (TYPE(CHILD(n, i-1)) == AMPER) {
1554 op = BINARY_AND;
1555 }
1556 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001557 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001558 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001559 op = 255;
1560 }
1561 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001562 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001563 }
1564}
1565
1566static void
1567com_xor_expr(c, n)
1568 struct compiling *c;
1569 node *n;
1570{
1571 int i;
1572 int op;
1573 REQ(n, xor_expr);
1574 com_and_expr(c, CHILD(n, 0));
1575 for (i = 2; i < NCH(n); i += 2) {
1576 com_and_expr(c, CHILD(n, i));
1577 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1578 op = BINARY_XOR;
1579 }
1580 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001581 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001582 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001583 op = 255;
1584 }
1585 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001586 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001587 }
1588}
1589
1590static void
1591com_expr(c, n)
1592 struct compiling *c;
1593 node *n;
1594{
1595 int i;
1596 int op;
1597 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001598 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001599 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001600 com_xor_expr(c, CHILD(n, i));
1601 if (TYPE(CHILD(n, i-1)) == VBAR) {
1602 op = BINARY_OR;
1603 }
1604 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001605 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001606 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001607 op = 255;
1608 }
1609 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001610 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001611 }
1612}
1613
1614static enum cmp_op
1615cmp_type(n)
1616 node *n;
1617{
1618 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00001619 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001620 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1621 if (NCH(n) == 1) {
1622 n = CHILD(n, 0);
1623 switch (TYPE(n)) {
1624 case LESS: return LT;
1625 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001626 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001627 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001628 case LESSEQUAL: return LE;
1629 case GREATEREQUAL: return GE;
1630 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001631 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1632 if (strcmp(STR(n), "is") == 0) return IS;
1633 }
1634 }
1635 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001636 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001637 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1638 return NOT_IN;
1639 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1640 return IS_NOT;
1641 }
1642 }
1643 return BAD;
1644}
1645
1646static void
1647com_comparison(c, n)
1648 struct compiling *c;
1649 node *n;
1650{
1651 int i;
1652 enum cmp_op op;
1653 int anchor;
1654 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1655 com_expr(c, CHILD(n, 0));
1656 if (NCH(n) == 1)
1657 return;
1658
1659 /****************************************************************
1660 The following code is generated for all but the last
1661 comparison in a chain:
1662
1663 label: on stack: opcode: jump to:
1664
1665 a <code to load b>
1666 a, b DUP_TOP
1667 a, b, b ROT_THREE
1668 b, a, b COMPARE_OP
1669 b, 0-or-1 JUMP_IF_FALSE L1
1670 b, 1 POP_TOP
1671 b
1672
1673 We are now ready to repeat this sequence for the next
1674 comparison in the chain.
1675
1676 For the last we generate:
1677
1678 b <code to load c>
1679 b, c COMPARE_OP
1680 0-or-1
1681
1682 If there were any jumps to L1 (i.e., there was more than one
1683 comparison), we generate:
1684
1685 0-or-1 JUMP_FORWARD L2
1686 L1: b, 0 ROT_TWO
1687 0, b POP_TOP
1688 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00001689 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001690 ****************************************************************/
1691
1692 anchor = 0;
1693
1694 for (i = 2; i < NCH(n); i += 2) {
1695 com_expr(c, CHILD(n, i));
1696 if (i+2 < NCH(n)) {
1697 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001698 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001699 com_addbyte(c, ROT_THREE);
1700 }
1701 op = cmp_type(CHILD(n, i-1));
1702 if (op == BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001703 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001704 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001705 }
1706 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001707 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001708 if (i+2 < NCH(n)) {
1709 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1710 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001711 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001712 }
1713 }
1714
1715 if (anchor) {
1716 int anchor2 = 0;
1717 com_addfwref(c, JUMP_FORWARD, &anchor2);
1718 com_backpatch(c, anchor);
1719 com_addbyte(c, ROT_TWO);
1720 com_addbyte(c, POP_TOP);
1721 com_backpatch(c, anchor2);
1722 }
1723}
1724
1725static void
1726com_not_test(c, n)
1727 struct compiling *c;
1728 node *n;
1729{
1730 REQ(n, not_test); /* 'not' not_test | comparison */
1731 if (NCH(n) == 1) {
1732 com_comparison(c, CHILD(n, 0));
1733 }
1734 else {
1735 com_not_test(c, CHILD(n, 1));
1736 com_addbyte(c, UNARY_NOT);
1737 }
1738}
1739
1740static void
1741com_and_test(c, n)
1742 struct compiling *c;
1743 node *n;
1744{
1745 int i;
1746 int anchor;
1747 REQ(n, and_test); /* not_test ('and' not_test)* */
1748 anchor = 0;
1749 i = 0;
1750 for (;;) {
1751 com_not_test(c, CHILD(n, i));
1752 if ((i += 2) >= NCH(n))
1753 break;
1754 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1755 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001756 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001757 }
1758 if (anchor)
1759 com_backpatch(c, anchor);
1760}
1761
1762static void
1763com_test(c, n)
1764 struct compiling *c;
1765 node *n;
1766{
Guido van Rossum8b993a91997-01-17 21:04:03 +00001767 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00001768 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001769 PyObject *v;
Guido van Rossum57531fe1993-11-30 14:57:42 +00001770 int i;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001771 int ndefs = com_argdefs(c, CHILD(n, 0));
Guido van Rossum79f25d91997-04-29 20:08:16 +00001772 v = (PyObject *) icompile(CHILD(n, 0), c);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001773 if (v == NULL) {
1774 c->c_errors++;
1775 i = 255;
1776 }
1777 else {
1778 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001779 Py_DECREF(v);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001780 }
1781 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001782 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001783 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001784 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001785 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00001786 else {
1787 int anchor = 0;
1788 int i = 0;
1789 for (;;) {
1790 com_and_test(c, CHILD(n, i));
1791 if ((i += 2) >= NCH(n))
1792 break;
1793 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1794 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001795 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001796 }
1797 if (anchor)
1798 com_backpatch(c, anchor);
1799 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001800}
1801
1802static void
Guido van Rossum288a60f1991-12-16 13:05:10 +00001803com_list(c, n, toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001804 struct compiling *c;
1805 node *n;
Guido van Rossum288a60f1991-12-16 13:05:10 +00001806 int toplevel; /* If nonzero, *always* build a tuple */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001807{
1808 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001809 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001810 com_node(c, CHILD(n, 0));
1811 }
1812 else {
1813 int i;
1814 int len;
1815 len = (NCH(n) + 1) / 2;
1816 for (i = 0; i < NCH(n); i += 2)
1817 com_node(c, CHILD(n, i));
1818 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001819 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001820 }
1821}
1822
1823
1824/* Begin of assignment compilation */
1825
Tim Petersdbd9ba62000-07-09 03:09:57 +00001826static void com_assign_name(struct compiling *, node *, int);
1827static void com_assign(struct compiling *, node *, int);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001828
1829static void
1830com_assign_attr(c, n, assigning)
1831 struct compiling *c;
1832 node *n;
1833 int assigning;
1834{
1835 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001836 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001837}
1838
1839static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001840com_assign_trailer(c, n, assigning)
1841 struct compiling *c;
1842 node *n;
1843 int assigning;
1844{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001845 REQ(n, trailer);
1846 switch (TYPE(CHILD(n, 0))) {
1847 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001848 com_error(c, PyExc_SyntaxError,
1849 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001850 break;
1851 case DOT: /* '.' NAME */
1852 com_assign_attr(c, CHILD(n, 1), assigning);
1853 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001854 case LSQB: /* '[' subscriptlist ']' */
1855 com_subscriptlist(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001856 break;
1857 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001858 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001859 }
1860}
1861
1862static void
1863com_assign_tuple(c, n, assigning)
1864 struct compiling *c;
1865 node *n;
1866 int assigning;
1867{
1868 int i;
1869 if (TYPE(n) != testlist)
1870 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001871 if (assigning) {
1872 i = (NCH(n)+1)/2;
1873 com_addoparg(c, UNPACK_TUPLE, i);
1874 com_push(c, i-1);
1875 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001876 for (i = 0; i < NCH(n); i += 2)
1877 com_assign(c, CHILD(n, i), assigning);
1878}
1879
1880static void
1881com_assign_list(c, n, assigning)
1882 struct compiling *c;
1883 node *n;
1884 int assigning;
1885{
1886 int i;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001887 if (assigning) {
1888 i = (NCH(n)+1)/2;
1889 com_addoparg(c, UNPACK_LIST, i);
1890 com_push(c, i-1);
1891 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001892 for (i = 0; i < NCH(n); i += 2)
1893 com_assign(c, CHILD(n, i), assigning);
1894}
1895
1896static void
1897com_assign_name(c, n, assigning)
1898 struct compiling *c;
1899 node *n;
1900 int assigning;
1901{
1902 REQ(n, NAME);
1903 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001904 if (assigning)
1905 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001906}
1907
1908static void
1909com_assign(c, n, assigning)
1910 struct compiling *c;
1911 node *n;
1912 int assigning;
1913{
1914 /* Loop to avoid trivial recursion */
1915 for (;;) {
1916 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001917
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001918 case exprlist:
1919 case testlist:
1920 if (NCH(n) > 1) {
1921 com_assign_tuple(c, n, assigning);
1922 return;
1923 }
1924 n = CHILD(n, 0);
1925 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001926
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001927 case test:
1928 case and_test:
1929 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001930 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001931 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00001932 case xor_expr:
1933 case and_expr:
1934 case shift_expr:
1935 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001936 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00001937 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001938 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001939 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001940 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001941 return;
1942 }
1943 n = CHILD(n, 0);
1944 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001945
Guido van Rossum50564e81996-01-12 01:13:16 +00001946 case power: /* atom trailer* ('**' power)* */
1947/* ('+'|'-'|'~') factor | atom trailer* */
1948 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001949 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001950 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001951 return;
1952 }
Guido van Rossum50564e81996-01-12 01:13:16 +00001953 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001954 int i;
1955 com_node(c, CHILD(n, 0));
1956 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00001957 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001958 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001959 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00001960 return;
1961 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001962 com_apply_trailer(c, CHILD(n, i));
1963 } /* NB i is still alive */
1964 com_assign_trailer(c,
1965 CHILD(n, i), assigning);
1966 return;
1967 }
1968 n = CHILD(n, 0);
1969 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001970
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001971 case atom:
1972 switch (TYPE(CHILD(n, 0))) {
1973 case LPAR:
1974 n = CHILD(n, 1);
1975 if (TYPE(n) == RPAR) {
1976 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001977 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001978 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001979 return;
1980 }
1981 break;
1982 case LSQB:
1983 n = CHILD(n, 1);
1984 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001985 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001986 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001987 return;
1988 }
1989 com_assign_list(c, n, assigning);
1990 return;
1991 case NAME:
1992 com_assign_name(c, CHILD(n, 0), assigning);
1993 return;
1994 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001995 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001996 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001997 return;
1998 }
1999 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002000
2001 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002002 com_error(c, PyExc_SyntaxError,
2003 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002004 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002005
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002006 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002007 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002008 com_error(c, PyExc_SystemError,
2009 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002010 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002011
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002012 }
2013 }
2014}
Guido van Rossum7c531111997-03-11 18:42:21 +00002015
Tim Petersdbd9ba62000-07-09 03:09:57 +00002016/* Forward */ static node *get_rawdocstring(node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002017
2018static void
2019com_expr_stmt(c, n)
2020 struct compiling *c;
2021 node *n;
2022{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002023 REQ(n, expr_stmt); /* testlist ('=' testlist)* */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002024 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00002025 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002026 return;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002027 com_node(c, CHILD(n, NCH(n)-1));
2028 if (NCH(n) == 1) {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002029 if (c->c_interactive)
2030 com_addbyte(c, PRINT_EXPR);
2031 else
2032 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002033 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002034 }
2035 else {
2036 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002037 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002038 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002039 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002040 com_push(c, 1);
2041 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002042 com_assign(c, CHILD(n, i), OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002043 }
2044 }
2045}
2046
2047static void
Guido van Rossum228d7f31997-04-02 05:24:36 +00002048com_assert_stmt(c, n)
2049 struct compiling *c;
2050 node *n;
2051{
2052 int a = 0, b = 0;
2053 int i;
2054 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2055 /* Generate code like for
2056
2057 if __debug__:
2058 if not <test>:
2059 raise AssertionError [, <message>]
2060
2061 where <message> is the second test, if present.
2062 */
2063 if (Py_OptimizeFlag)
2064 return;
2065 com_addopnamestr(c, LOAD_GLOBAL, "__debug__");
2066 com_push(c, 1);
2067 com_addfwref(c, JUMP_IF_FALSE, &a);
2068 com_addbyte(c, POP_TOP);
2069 com_pop(c, 1);
2070 com_node(c, CHILD(n, 1));
2071 com_addfwref(c, JUMP_IF_TRUE, &b);
2072 com_addbyte(c, POP_TOP);
2073 com_pop(c, 1);
2074 /* Raise that exception! */
2075 com_addopnamestr(c, LOAD_GLOBAL, "AssertionError");
2076 com_push(c, 1);
2077 i = NCH(n)/2; /* Either 2 or 4 */
2078 if (i > 1)
2079 com_node(c, CHILD(n, 3));
2080 com_addoparg(c, RAISE_VARARGS, i);
2081 com_pop(c, i);
2082 /* The interpreter does not fall through */
2083 /* All jumps converge here */
2084 com_backpatch(c, a);
2085 com_backpatch(c, b);
2086 com_addbyte(c, POP_TOP);
2087}
2088
2089static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002090com_print_stmt(c, n)
2091 struct compiling *c;
2092 node *n;
2093{
2094 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002095 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2096 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002097 com_node(c, CHILD(n, i));
2098 com_addbyte(c, PRINT_ITEM);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002099 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002100 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002101 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002102 com_addbyte(c, PRINT_NEWLINE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002103 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002104}
2105
2106static void
2107com_return_stmt(c, n)
2108 struct compiling *c;
2109 node *n;
2110{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002111 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002112 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002113 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002114 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002115 if (NCH(n) < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002116 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002117 com_push(c, 1);
2118 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002119 else
2120 com_node(c, CHILD(n, 1));
2121 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002122 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002123}
2124
2125static void
2126com_raise_stmt(c, n)
2127 struct compiling *c;
2128 node *n;
2129{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002130 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002131 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2132 if (NCH(n) > 1) {
2133 com_node(c, CHILD(n, 1));
2134 if (NCH(n) > 3) {
2135 com_node(c, CHILD(n, 3));
2136 if (NCH(n) > 5)
2137 com_node(c, CHILD(n, 5));
2138 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002139 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002140 i = NCH(n)/2;
2141 com_addoparg(c, RAISE_VARARGS, i);
2142 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002143}
2144
2145static void
2146com_import_stmt(c, n)
2147 struct compiling *c;
2148 node *n;
2149{
2150 int i;
2151 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002152 /* 'import' dotted_name (',' dotted_name)* |
2153 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002154 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002155 /* 'from' dotted_name 'import' ... */
2156 REQ(CHILD(n, 1), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002157 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002158 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002159 for (i = 3; i < NCH(n); i += 2)
2160 com_addopname(c, IMPORT_FROM, CHILD(n, i));
2161 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002162 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002163 }
2164 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002165 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002166 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002167 REQ(CHILD(n, i), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002168 com_addopname(c, IMPORT_NAME, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002169 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002170 com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002171 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002172 }
2173 }
2174}
2175
2176static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00002177com_global_stmt(c, n)
2178 struct compiling *c;
2179 node *n;
2180{
2181 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002182 REQ(n, global_stmt);
2183 /* 'global' NAME (',' NAME)* */
2184 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002185 char *s = STR(CHILD(n, i));
Guido van Rossumfe2236f1996-08-24 07:29:04 +00002186#ifdef PRIVATE_NAME_MANGLING
2187 char buffer[256];
2188 if (s != NULL && s[0] == '_' && s[1] == '_' &&
2189 c->c_private != NULL &&
Guido van Rossum582acec2000-06-28 22:07:35 +00002190 com_mangle(c, s, buffer, sizeof(buffer)))
Guido van Rossumfe2236f1996-08-24 07:29:04 +00002191 s = buffer;
2192#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00002193 if (PyDict_GetItemString(c->c_locals, s) != NULL) {
2194 com_error(c, PyExc_SyntaxError,
2195 "name is local and global");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002196 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002197 else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002198 c->c_errors++;
2199 }
2200}
2201
Guido van Rossum681d79a1995-07-18 14:51:37 +00002202static int
2203com_newlocal_o(c, nameval)
2204 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002205 PyObject *nameval;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002206{
2207 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002208 PyObject *ival;
2209 if (PyList_Size(c->c_varnames) != c->c_nlocals) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002210 /* This is usually caused by an error on a previous call */
2211 if (c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002212 com_error(c, PyExc_SystemError,
2213 "mixed up var name/index");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002214 }
2215 return 0;
2216 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002217 ival = PyInt_FromLong(i = c->c_nlocals++);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002218 if (ival == NULL)
2219 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002220 else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002221 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002222 else if (PyList_Append(c->c_varnames, nameval) != 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002223 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002224 Py_XDECREF(ival);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002225 return i;
2226}
2227
2228static int
2229com_addlocal_o(c, nameval)
2230 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002231 PyObject *nameval;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002232{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002233 PyObject *ival = PyDict_GetItem(c->c_locals, nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002234 if (ival != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002235 return PyInt_AsLong(ival);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002236 return com_newlocal_o(c, nameval);
2237}
2238
2239static int
2240com_newlocal(c, name)
2241 struct compiling *c;
2242 char *name;
2243{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002244 PyObject *nameval = PyString_InternFromString(name);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002245 int i;
2246 if (nameval == NULL) {
2247 c->c_errors++;
2248 return 0;
2249 }
2250 i = com_newlocal_o(c, nameval);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002251 Py_DECREF(nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002252 return i;
2253}
2254
Guido van Rossumc5e96291991-12-10 13:53:51 +00002255static void
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002256com_exec_stmt(c, n)
2257 struct compiling *c;
2258 node *n;
2259{
2260 REQ(n, exec_stmt);
2261 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2262 com_node(c, CHILD(n, 1));
2263 if (NCH(n) >= 4)
2264 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002265 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002266 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002267 com_push(c, 1);
2268 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002269 if (NCH(n) >= 6)
2270 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002271 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002272 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002273 com_push(c, 1);
2274 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002275 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002276 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002277}
2278
Guido van Rossum7c531111997-03-11 18:42:21 +00002279static int
2280is_constant_false(c, n)
2281 struct compiling *c;
2282 node *n;
2283{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002284 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00002285 int i;
2286
2287 /* Label to avoid tail recursion */
2288 next:
2289 switch (TYPE(n)) {
2290
2291 case suite:
2292 if (NCH(n) == 1) {
2293 n = CHILD(n, 0);
2294 goto next;
2295 }
2296 /* Fall through */
2297 case file_input:
2298 for (i = 0; i < NCH(n); i++) {
2299 node *ch = CHILD(n, i);
2300 if (TYPE(ch) == stmt) {
2301 n = ch;
2302 goto next;
2303 }
2304 }
2305 break;
2306
2307 case stmt:
2308 case simple_stmt:
2309 case small_stmt:
2310 n = CHILD(n, 0);
2311 goto next;
2312
2313 case expr_stmt:
2314 case testlist:
2315 case test:
2316 case and_test:
2317 case not_test:
2318 case comparison:
2319 case expr:
2320 case xor_expr:
2321 case and_expr:
2322 case shift_expr:
2323 case arith_expr:
2324 case term:
2325 case factor:
2326 case power:
2327 case atom:
2328 if (NCH(n) == 1) {
2329 n = CHILD(n, 0);
2330 goto next;
2331 }
2332 break;
2333
2334 case NAME:
2335 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2336 return 1;
2337 break;
2338
2339 case NUMBER:
2340 v = parsenumber(c, STR(n));
2341 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002342 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002343 break;
2344 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002345 i = PyObject_IsTrue(v);
2346 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002347 return i == 0;
2348
2349 case STRING:
Guido van Rossum6af0c001997-03-11 21:25:55 +00002350 v = parsestr(STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00002351 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002352 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002353 break;
2354 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002355 i = PyObject_IsTrue(v);
2356 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002357 return i == 0;
2358
2359 }
2360 return 0;
2361}
2362
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002363static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002364com_if_stmt(c, n)
2365 struct compiling *c;
2366 node *n;
2367{
2368 int i;
2369 int anchor = 0;
2370 REQ(n, if_stmt);
2371 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2372 for (i = 0; i+3 < NCH(n); i+=4) {
2373 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002374 node *ch = CHILD(n, i+1);
Guido van Rossum7c531111997-03-11 18:42:21 +00002375 if (is_constant_false(c, ch))
2376 continue;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002377 if (i > 0)
2378 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00002379 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002380 com_addfwref(c, JUMP_IF_FALSE, &a);
2381 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002382 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002383 com_node(c, CHILD(n, i+3));
2384 com_addfwref(c, JUMP_FORWARD, &anchor);
2385 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002386 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002387 com_addbyte(c, POP_TOP);
2388 }
2389 if (i+2 < NCH(n))
2390 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00002391 if (anchor)
2392 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002393}
2394
2395static void
2396com_while_stmt(c, n)
2397 struct compiling *c;
2398 node *n;
2399{
2400 int break_anchor = 0;
2401 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002402 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002403 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2404 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002405 block_push(c, SETUP_LOOP);
2406 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002407 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002408 com_node(c, CHILD(n, 1));
2409 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2410 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002411 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002412 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002413 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002414 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002415 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2416 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002417 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002418 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002419 com_addbyte(c, POP_TOP);
2420 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002421 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002422 if (NCH(n) > 4)
2423 com_node(c, CHILD(n, 6));
2424 com_backpatch(c, break_anchor);
2425}
2426
2427static void
2428com_for_stmt(c, n)
2429 struct compiling *c;
2430 node *n;
2431{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002432 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002433 int break_anchor = 0;
2434 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002435 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002436 REQ(n, for_stmt);
2437 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2438 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002439 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002440 com_node(c, CHILD(n, 3));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002441 v = PyInt_FromLong(0L);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002442 if (v == NULL)
2443 c->c_errors++;
2444 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002445 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002446 Py_XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002447 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002448 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002449 com_addfwref(c, FOR_LOOP, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002450 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00002451 com_assign(c, CHILD(n, 1), OP_ASSIGN);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002452 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002453 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002454 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002455 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2456 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002457 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002458 com_pop(c, 2); /* FOR_LOOP has popped these */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002459 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002460 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002461 if (NCH(n) > 8)
2462 com_node(c, CHILD(n, 8));
2463 com_backpatch(c, break_anchor);
2464}
2465
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002466/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002467
2468 SETUP_FINALLY L
2469 <code for S>
2470 POP_BLOCK
2471 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002472 L: <code for Sf>
2473 END_FINALLY
2474
2475 The special instructions use the block stack. Each block
2476 stack entry contains the instruction that created it (here
2477 SETUP_FINALLY), the level of the value stack at the time the
2478 block stack entry was created, and a label (here L).
2479
2480 SETUP_FINALLY:
2481 Pushes the current value stack level and the label
2482 onto the block stack.
2483 POP_BLOCK:
2484 Pops en entry from the block stack, and pops the value
2485 stack until its level is the same as indicated on the
2486 block stack. (The label is ignored.)
2487 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002488 Pops a variable number of entries from the *value* stack
2489 and re-raises the exception they specify. The number of
2490 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002491
2492 The block stack is unwound when an exception is raised:
2493 when a SETUP_FINALLY entry is found, the exception is pushed
2494 onto the value stack (and the exception condition is cleared),
2495 and the interpreter jumps to the label gotten from the block
2496 stack.
2497
2498 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00002499 (The contents of the value stack is shown in [], with the top
2500 at the right; 'tb' is trace-back info, 'val' the exception's
2501 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002502
2503 Value stack Label Instruction Argument
2504 [] SETUP_EXCEPT L1
2505 [] <code for S>
2506 [] POP_BLOCK
2507 [] JUMP_FORWARD L0
2508
Guido van Rossum3f5da241990-12-20 15:06:42 +00002509 [tb, val, exc] L1: DUP )
2510 [tb, val, exc, exc] <evaluate E1> )
2511 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2512 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2513 [tb, val, exc, 1] POP )
2514 [tb, val, exc] POP
2515 [tb, val] <assign to V1> (or POP if no V1)
2516 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002517 [] <code for S1>
2518 JUMP_FORWARD L0
2519
Guido van Rossum3f5da241990-12-20 15:06:42 +00002520 [tb, val, exc, 0] L2: POP
2521 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002522 .............................etc.......................
2523
Guido van Rossum3f5da241990-12-20 15:06:42 +00002524 [tb, val, exc, 0] Ln+1: POP
2525 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002526
2527 [] L0: <next statement>
2528
2529 Of course, parts are not generated if Vi or Ei is not present.
2530*/
2531
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002532static void
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002533com_try_except(c, n)
2534 struct compiling *c;
2535 node *n;
2536{
2537 int except_anchor = 0;
2538 int end_anchor = 0;
2539 int else_anchor = 0;
2540 int i;
2541 node *ch;
2542
2543 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2544 block_push(c, SETUP_EXCEPT);
2545 com_node(c, CHILD(n, 2));
2546 com_addbyte(c, POP_BLOCK);
2547 block_pop(c, SETUP_EXCEPT);
2548 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2549 com_backpatch(c, except_anchor);
2550 for (i = 3;
2551 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2552 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002553 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002554 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002555 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002556 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002557 break;
2558 }
2559 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00002560 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002561 com_addoparg(c, SET_LINENO, ch->n_lineno);
2562 if (NCH(ch) > 1) {
2563 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002564 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002565 com_node(c, CHILD(ch, 1));
2566 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002567 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002568 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
2569 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002570 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002571 }
2572 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002573 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002574 if (NCH(ch) > 3)
Guido van Rossum8861b741996-07-30 16:49:37 +00002575 com_assign(c, CHILD(ch, 3), OP_ASSIGN);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002576 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002577 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002578 com_pop(c, 1);
2579 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002580 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002581 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002582 com_node(c, CHILD(n, i+2));
2583 com_addfwref(c, JUMP_FORWARD, &end_anchor);
2584 if (except_anchor) {
2585 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002586 /* We come in with [tb, val, exc, 0] on the
2587 stack; one pop and it's the same as
2588 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002589 com_addbyte(c, POP_TOP);
2590 }
2591 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002592 /* We actually come in here with [tb, val, exc] but the
2593 END_FINALLY will zap those and jump around.
2594 The c_stacklevel does not reflect them so we need not pop
2595 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002596 com_addbyte(c, END_FINALLY);
2597 com_backpatch(c, else_anchor);
2598 if (i < NCH(n))
2599 com_node(c, CHILD(n, i+2));
2600 com_backpatch(c, end_anchor);
2601}
2602
2603static void
2604com_try_finally(c, n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002605 struct compiling *c;
2606 node *n;
2607{
2608 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002609 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00002610
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002611 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
2612 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002613 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002614 com_addbyte(c, POP_BLOCK);
2615 block_pop(c, SETUP_FINALLY);
2616 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002617 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002618 /* While the generated code pushes only one item,
2619 the try-finally handling can enter here with
2620 up to three items. OK, here are the details:
2621 3 for an exception, 2 for RETURN, 1 for BREAK. */
2622 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002623 com_backpatch(c, finally_anchor);
2624 ch = CHILD(n, NCH(n)-1);
2625 com_addoparg(c, SET_LINENO, ch->n_lineno);
2626 com_node(c, ch);
2627 com_addbyte(c, END_FINALLY);
2628 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002629 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002630}
2631
2632static void
2633com_try_stmt(c, n)
2634 struct compiling *c;
2635 node *n;
2636{
2637 REQ(n, try_stmt);
2638 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2639 | 'try' ':' suite 'finally' ':' suite */
2640 if (TYPE(CHILD(n, 3)) != except_clause)
2641 com_try_finally(c, n);
2642 else
2643 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002644}
2645
Guido van Rossum8b993a91997-01-17 21:04:03 +00002646static node *
2647get_rawdocstring(n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002648 node *n;
2649{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002650 int i;
2651
Guido van Rossum8b993a91997-01-17 21:04:03 +00002652 /* Label to avoid tail recursion */
2653 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002654 switch (TYPE(n)) {
2655
2656 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002657 if (NCH(n) == 1) {
2658 n = CHILD(n, 0);
2659 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002660 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002661 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002662 case file_input:
2663 for (i = 0; i < NCH(n); i++) {
2664 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002665 if (TYPE(ch) == stmt) {
2666 n = ch;
2667 goto next;
2668 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002669 }
2670 break;
2671
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002672 case stmt:
2673 case simple_stmt:
2674 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002675 n = CHILD(n, 0);
2676 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002677
2678 case expr_stmt:
2679 case testlist:
2680 case test:
2681 case and_test:
2682 case not_test:
2683 case comparison:
2684 case expr:
2685 case xor_expr:
2686 case and_expr:
2687 case shift_expr:
2688 case arith_expr:
2689 case term:
2690 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00002691 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002692 if (NCH(n) == 1) {
2693 n = CHILD(n, 0);
2694 goto next;
2695 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002696 break;
2697
2698 case atom:
2699 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002700 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002701 break;
2702
2703 }
2704 return NULL;
2705}
2706
Guido van Rossum79f25d91997-04-29 20:08:16 +00002707static PyObject *
Guido van Rossum8b993a91997-01-17 21:04:03 +00002708get_docstring(n)
2709 node *n;
2710{
Guido van Rossum541563e1999-01-28 15:08:09 +00002711 /* Don't generate doc-strings if run with -OO */
2712 if (Py_OptimizeFlag > 1)
2713 return NULL;
Guido van Rossum8b993a91997-01-17 21:04:03 +00002714 n = get_rawdocstring(n);
2715 if (n == NULL)
2716 return NULL;
2717 return parsestrplus(n);
2718}
2719
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002720static void
2721com_suite(c, n)
2722 struct compiling *c;
2723 node *n;
2724{
2725 REQ(n, suite);
2726 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2727 if (NCH(n) == 1) {
2728 com_node(c, CHILD(n, 0));
2729 }
2730 else {
2731 int i;
2732 for (i = 0; i < NCH(n); i++) {
2733 node *ch = CHILD(n, i);
2734 if (TYPE(ch) == stmt)
2735 com_node(c, ch);
2736 }
2737 }
2738}
2739
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002740/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002741static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002742com_continue_stmt(c, n)
2743 struct compiling *c;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002744 node *n; /* Not used, but passed for consistency */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002745{
2746 int i = c->c_nblocks;
2747 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
2748 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2749 }
2750 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002751 com_error(c, PyExc_SyntaxError,
2752 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002753 }
2754 /* XXX Could allow it inside a 'finally' clause
2755 XXX if we could pop the exception still on the stack */
2756}
2757
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002758static int
Guido van Rossum681d79a1995-07-18 14:51:37 +00002759com_argdefs(c, n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002760 struct compiling *c;
2761 node *n;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002762{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002763 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002764 if (TYPE(n) == lambdef) {
2765 /* lambdef: 'lambda' [varargslist] ':' test */
2766 n = CHILD(n, 1);
2767 }
2768 else {
2769 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
2770 n = CHILD(n, 2);
2771 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
2772 n = CHILD(n, 1);
2773 }
2774 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002775 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002776 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002777 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002778 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2779 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002780 nargs = 0;
2781 ndefs = 0;
2782 for (i = 0; i < nch; i++) {
2783 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002784 if (TYPE(CHILD(n, i)) == STAR ||
2785 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002786 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002787 nargs++;
2788 i++;
2789 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00002790 t = RPAR; /* Anything except EQUAL or COMMA */
2791 else
2792 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002793 if (t == EQUAL) {
2794 i++;
2795 ndefs++;
2796 com_node(c, CHILD(n, i));
2797 i++;
2798 if (i >= nch)
2799 break;
2800 t = TYPE(CHILD(n, i));
2801 }
2802 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00002803 /* Treat "(a=1, b)" as an error */
2804 if (ndefs)
2805 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00002806 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002807 }
2808 if (t != COMMA)
2809 break;
2810 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002811 return ndefs;
2812}
2813
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002814static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002815com_funcdef(c, n)
2816 struct compiling *c;
2817 node *n;
2818{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002819 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002820 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002821 v = (PyObject *)icompile(n, c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002822 if (v == NULL)
2823 c->c_errors++;
2824 else {
2825 int i = com_addconst(c, v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002826 int ndefs = com_argdefs(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002827 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002828 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002829 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002830 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002831 com_addopname(c, STORE_NAME, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002832 com_pop(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002833 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002834 }
2835}
2836
2837static void
Guido van Rossum25831651993-05-19 14:50:45 +00002838com_bases(c, n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002839 struct compiling *c;
2840 node *n;
2841{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002842 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002843 REQ(n, testlist);
2844 /* testlist: test (',' test)* [','] */
2845 for (i = 0; i < NCH(n); i += 2)
2846 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002847 i = (NCH(n)+1) / 2;
2848 com_addoparg(c, BUILD_TUPLE, i);
2849 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00002850}
2851
2852static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002853com_classdef(c, n)
2854 struct compiling *c;
2855 node *n;
2856{
Guido van Rossum25831651993-05-19 14:50:45 +00002857 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002858 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002859 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00002860 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00002861 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00002862 c->c_errors++;
2863 return;
2864 }
2865 /* Push the class name on the stack */
2866 i = com_addconst(c, v);
2867 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002868 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002869 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00002870 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002871 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00002872 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002873 com_push(c, 1);
2874 }
Guido van Rossum25831651993-05-19 14:50:45 +00002875 else
2876 com_bases(c, CHILD(n, 3));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002877 v = (PyObject *)icompile(n, c);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002878 if (v == NULL)
2879 c->c_errors++;
2880 else {
Guido van Rossum25831651993-05-19 14:50:45 +00002881 i = com_addconst(c, v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002882 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002883 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002884 com_addoparg(c, MAKE_FUNCTION, 0);
2885 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002886 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002887 com_pop(c, 2);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002888 com_addopname(c, STORE_NAME, CHILD(n, 1));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002889 Py_DECREF(v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002890 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002891}
2892
2893static void
2894com_node(c, n)
2895 struct compiling *c;
2896 node *n;
2897{
2898 switch (TYPE(n)) {
2899
2900 /* Definition nodes */
2901
2902 case funcdef:
2903 com_funcdef(c, n);
2904 break;
2905 case classdef:
2906 com_classdef(c, n);
2907 break;
2908
2909 /* Trivial parse tree nodes */
2910
2911 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002912 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002913 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002914 com_node(c, CHILD(n, 0));
2915 break;
2916
2917 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002918 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2919 com_addoparg(c, SET_LINENO, n->n_lineno);
2920 {
2921 int i;
2922 for (i = 0; i < NCH(n)-1; i += 2)
2923 com_node(c, CHILD(n, i));
2924 }
2925 break;
2926
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002927 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002928 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002929 com_node(c, CHILD(n, 0));
2930 break;
2931
2932 /* Statement nodes */
2933
2934 case expr_stmt:
2935 com_expr_stmt(c, n);
2936 break;
2937 case print_stmt:
2938 com_print_stmt(c, n);
2939 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002940 case del_stmt: /* 'del' exprlist */
Guido van Rossum8861b741996-07-30 16:49:37 +00002941 com_assign(c, CHILD(n, 1), OP_DELETE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002942 break;
2943 case pass_stmt:
2944 break;
2945 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002946 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002947 com_error(c, PyExc_SyntaxError,
2948 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002949 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002950 com_addbyte(c, BREAK_LOOP);
2951 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002952 case continue_stmt:
2953 com_continue_stmt(c, n);
2954 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002955 case return_stmt:
2956 com_return_stmt(c, n);
2957 break;
2958 case raise_stmt:
2959 com_raise_stmt(c, n);
2960 break;
2961 case import_stmt:
2962 com_import_stmt(c, n);
2963 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002964 case global_stmt:
2965 com_global_stmt(c, n);
2966 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002967 case exec_stmt:
2968 com_exec_stmt(c, n);
2969 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00002970 case assert_stmt:
2971 com_assert_stmt(c, n);
2972 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002973 case if_stmt:
2974 com_if_stmt(c, n);
2975 break;
2976 case while_stmt:
2977 com_while_stmt(c, n);
2978 break;
2979 case for_stmt:
2980 com_for_stmt(c, n);
2981 break;
2982 case try_stmt:
2983 com_try_stmt(c, n);
2984 break;
2985 case suite:
2986 com_suite(c, n);
2987 break;
2988
2989 /* Expression nodes */
2990
2991 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002992 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002993 break;
2994 case test:
2995 com_test(c, n);
2996 break;
2997 case and_test:
2998 com_and_test(c, n);
2999 break;
3000 case not_test:
3001 com_not_test(c, n);
3002 break;
3003 case comparison:
3004 com_comparison(c, n);
3005 break;
3006 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003007 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003008 break;
3009 case expr:
3010 com_expr(c, n);
3011 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00003012 case xor_expr:
3013 com_xor_expr(c, n);
3014 break;
3015 case and_expr:
3016 com_and_expr(c, n);
3017 break;
3018 case shift_expr:
3019 com_shift_expr(c, n);
3020 break;
3021 case arith_expr:
3022 com_arith_expr(c, n);
3023 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003024 case term:
3025 com_term(c, n);
3026 break;
3027 case factor:
3028 com_factor(c, n);
3029 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00003030 case power:
3031 com_power(c, n);
3032 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003033 case atom:
3034 com_atom(c, n);
3035 break;
3036
3037 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003038 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00003039 com_error(c, PyExc_SystemError,
3040 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003041 }
3042}
3043
Tim Petersdbd9ba62000-07-09 03:09:57 +00003044static void com_fplist(struct compiling *, node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003045
3046static void
3047com_fpdef(c, n)
3048 struct compiling *c;
3049 node *n;
3050{
3051 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3052 if (TYPE(CHILD(n, 0)) == LPAR)
3053 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003054 else {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003055 com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003056 com_pop(c, 1);
3057 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003058}
3059
3060static void
3061com_fplist(c, n)
3062 struct compiling *c;
3063 node *n;
3064{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00003065 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003066 if (NCH(n) == 1) {
3067 com_fpdef(c, CHILD(n, 0));
3068 }
3069 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003070 int i = (NCH(n)+1)/2;
3071 com_addoparg(c, UNPACK_TUPLE, i);
3072 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003073 for (i = 0; i < NCH(n); i += 2)
3074 com_fpdef(c, CHILD(n, i));
3075 }
3076}
3077
3078static void
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003079com_arglist(c, n)
3080 struct compiling *c;
3081 node *n;
3082{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003083 int nch, i;
3084 int complex = 0;
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003085 char nbuf[10];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003086 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003087 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003088 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003089 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003090 /* Enter all arguments in table of locals */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003091 for (i = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003092 node *ch = CHILD(n, i);
3093 node *fp;
3094 char *name;
Peter Schneider-Kamp11384c62000-07-13 06:15:04 +00003095 PyObject *nameval;
Guido van Rossum50564e81996-01-12 01:13:16 +00003096 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003097 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003098 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3099 fp = CHILD(ch, 0);
3100 if (TYPE(fp) == NAME)
3101 name = STR(fp);
3102 else {
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003103 name = nbuf;
3104 sprintf(nbuf, ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003105 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003106 }
Peter Schneider-Kamp11384c62000-07-13 06:15:04 +00003107 nameval = PyString_InternFromString(name);
3108 if (nameval == NULL) {
3109 c->c_errors++;
3110 }
Peter Schneider-Kamp9a5086c2000-07-13 06:24:29 +00003111 if (PyDict_GetItem(c->c_locals, nameval)) {
Peter Schneider-Kamp11384c62000-07-13 06:15:04 +00003112 com_error(c, PyExc_SyntaxError,"duplicate argument in function definition");
3113 }
3114 com_newlocal_o(c, nameval);
3115 Py_DECREF(nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003116 c->c_argcount++;
3117 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003118 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003119 ch = CHILD(n, i);
3120 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003121 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003122 else
3123 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003124 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003125 /* Handle *arguments */
3126 if (i < nch) {
3127 node *ch;
3128 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003129 if (TYPE(ch) != DOUBLESTAR) {
3130 REQ(ch, STAR);
3131 ch = CHILD(n, i+1);
3132 if (TYPE(ch) == NAME) {
3133 c->c_flags |= CO_VARARGS;
3134 i += 3;
3135 com_newlocal(c, STR(ch));
3136 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003137 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003138 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003139 /* Handle **keywords */
3140 if (i < nch) {
3141 node *ch;
3142 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003143 if (TYPE(ch) != DOUBLESTAR) {
3144 REQ(ch, STAR);
3145 ch = CHILD(n, i+1);
3146 REQ(ch, STAR);
3147 ch = CHILD(n, i+2);
3148 }
3149 else
3150 ch = CHILD(n, i+1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003151 REQ(ch, NAME);
3152 c->c_flags |= CO_VARKEYWORDS;
3153 com_newlocal(c, STR(ch));
3154 }
3155 if (complex) {
3156 /* Generate code for complex arguments only after
3157 having counted the simple arguments */
3158 int ilocal = 0;
3159 for (i = 0; i < nch; i++) {
3160 node *ch = CHILD(n, i);
3161 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003162 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003163 break;
3164 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3165 fp = CHILD(ch, 0);
3166 if (TYPE(fp) != NAME) {
3167 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003168 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003169 com_fpdef(c, ch);
3170 }
3171 ilocal++;
3172 if (++i >= nch)
3173 break;
3174 ch = CHILD(n, i);
3175 if (TYPE(ch) == EQUAL)
3176 i += 2;
3177 else
3178 REQ(ch, COMMA);
3179 }
3180 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003181}
3182
3183static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003184com_file_input(c, n)
3185 struct compiling *c;
3186 node *n;
3187{
3188 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003189 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003190 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003191 doc = get_docstring(n);
3192 if (doc != NULL) {
3193 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003194 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003195 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003196 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003197 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003198 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003199 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003200 for (i = 0; i < NCH(n); i++) {
3201 node *ch = CHILD(n, i);
3202 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3203 com_node(c, ch);
3204 }
3205}
3206
3207/* Top-level compile-node interface */
3208
3209static void
3210compile_funcdef(c, n)
3211 struct compiling *c;
3212 node *n;
3213{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003214 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003215 node *ch;
3216 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003217 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003218 doc = get_docstring(CHILD(n, 4));
3219 if (doc != NULL) {
3220 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003221 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003222 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003223 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003224 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003225 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3226 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003227 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003228 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003229 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003230 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003231 c->c_infunction = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003232 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003233 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003234 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003235 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003236}
3237
3238static void
Guido van Rossum590baa41993-11-30 13:40:46 +00003239compile_lambdef(c, n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003240 struct compiling *c;
3241 node *n;
3242{
Guido van Rossum590baa41993-11-30 13:40:46 +00003243 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003244 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003245 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003246
3247 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003248 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003249 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003250 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003251 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003252 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003253 else
3254 ch = CHILD(n, 2);
3255 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003256 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003257 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003258}
3259
3260static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003261compile_classdef(c, n)
3262 struct compiling *c;
3263 node *n;
3264{
3265 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003266 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003267 REQ(n, classdef);
3268 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3269 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003270#ifdef PRIVATE_NAME_MANGLING
3271 c->c_private = c->c_name;
3272#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003273 ch = CHILD(n, NCH(n)-1); /* The suite */
3274 doc = get_docstring(ch);
3275 if (doc != NULL) {
3276 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003277 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003278 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003279 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003280 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003281 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003282 }
3283 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003284 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003285 com_node(c, ch);
3286 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003287 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003288 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003289 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003290}
3291
3292static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003293compile_node(c, n)
3294 struct compiling *c;
3295 node *n;
3296{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003297 com_addoparg(c, SET_LINENO, n->n_lineno);
3298
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003299 switch (TYPE(n)) {
3300
Guido van Rossum4c417781991-01-21 16:09:22 +00003301 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003302 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003303 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003304 n = CHILD(n, 0);
3305 if (TYPE(n) != NEWLINE)
3306 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003307 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003308 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003309 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003310 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003311 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003312 break;
3313
Guido van Rossum4c417781991-01-21 16:09:22 +00003314 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003315 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003316 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003317 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003318 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003319 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003320 break;
3321
Guido van Rossum590baa41993-11-30 13:40:46 +00003322 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003323 com_node(c, CHILD(n, 0));
3324 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003325 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003326 break;
3327
Guido van Rossum590baa41993-11-30 13:40:46 +00003328 case lambdef: /* anonymous function definition */
3329 compile_lambdef(c, n);
3330 break;
3331
Guido van Rossum4c417781991-01-21 16:09:22 +00003332 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003333 compile_funcdef(c, n);
3334 break;
3335
Guido van Rossum4c417781991-01-21 16:09:22 +00003336 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003337 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003338 break;
3339
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003340 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003341 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00003342 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003343 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003344 }
3345}
3346
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003347/* Optimization for local variables in functions (and *only* functions).
Guido van Rossum282914b1991-04-04 10:42:56 +00003348
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003349 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
3350 instructions that refer to local variables with LOAD_FAST etc.
3351 The latter instructions are much faster because they don't need to
3352 look up the variable name in a dictionary.
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003353
Guido van Rossum681d79a1995-07-18 14:51:37 +00003354 To find all local variables, we check all STORE_NAME, IMPORT_FROM
3355 and DELETE_NAME instructions. This yields all local variables,
3356 function definitions, class definitions and import statements.
3357 Argument names have already been entered into the list by the
3358 special processing for the argument list.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003359
3360 All remaining LOAD_NAME instructions must refer to non-local (global
3361 or builtin) variables, so are replaced by LOAD_GLOBAL.
3362
3363 There are two problems: 'from foo import *' and 'exec' may introduce
3364 local variables that we can't know while compiling. If this is the
Guido van Rossum681d79a1995-07-18 14:51:37 +00003365 case, we can still optimize bona fide locals (since those
3366 statements will be surrounded by fast_2_locals() and
3367 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003368
Guido van Rossum681d79a1995-07-18 14:51:37 +00003369 NB: this modifies the string object c->c_code! */
Guido van Rossum282914b1991-04-04 10:42:56 +00003370
3371static void
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003372optimize(c)
3373 struct compiling *c;
Guido van Rossum282914b1991-04-04 10:42:56 +00003374{
Guido van Rossum0a697f61991-04-16 08:39:12 +00003375 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00003376 int opcode;
Guido van Rossum80bb9651996-12-05 23:27:02 +00003377 int oparg = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003378 PyObject *name;
3379 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003380
Guido van Rossum282914b1991-04-04 10:42:56 +00003381#define NEXTOP() (*next_instr++)
3382#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossum79f25d91997-04-29 20:08:16 +00003383#define GETITEM(v, i) (PyList_GetItem((v), (i)))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003384#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
3385
Guido van Rossum79f25d91997-04-29 20:08:16 +00003386 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003387
3388 c->c_flags |= CO_OPTIMIZED;
Guido van Rossum282914b1991-04-04 10:42:56 +00003389
Guido van Rossum79f25d91997-04-29 20:08:16 +00003390 next_instr = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003391 for (;;) {
3392 opcode = NEXTOP();
3393 if (opcode == STOP_CODE)
3394 break;
3395 if (HAS_ARG(opcode))
3396 oparg = NEXTARG();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003397 switch (opcode) {
3398 case STORE_NAME:
3399 case DELETE_NAME:
3400 case IMPORT_FROM:
3401 com_addlocal_o(c, GETNAMEOBJ(oparg));
3402 break;
3403 case EXEC_STMT:
3404 c->c_flags &= ~CO_OPTIMIZED;
3405 break;
Guido van Rossum282914b1991-04-04 10:42:56 +00003406 }
3407 }
3408
Guido van Rossum79f25d91997-04-29 20:08:16 +00003409 if (PyDict_GetItemString(c->c_locals, "*") != NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003410 c->c_flags &= ~CO_OPTIMIZED;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003411
Guido van Rossum79f25d91997-04-29 20:08:16 +00003412 next_instr = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003413 for (;;) {
3414 cur_instr = next_instr;
3415 opcode = NEXTOP();
3416 if (opcode == STOP_CODE)
3417 break;
3418 if (HAS_ARG(opcode))
3419 oparg = NEXTARG();
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003420 if (opcode == LOAD_NAME ||
3421 opcode == STORE_NAME ||
3422 opcode == DELETE_NAME) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003423 PyObject *v;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003424 int i;
Guido van Rossum282914b1991-04-04 10:42:56 +00003425 name = GETNAMEOBJ(oparg);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003426 v = PyDict_GetItem(c->c_locals, name);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003427 if (v == NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003428 if (opcode == LOAD_NAME &&
3429 (c->c_flags&CO_OPTIMIZED))
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003430 cur_instr[0] = LOAD_GLOBAL;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003431 continue;
Guido van Rossum83163251991-08-16 08:58:43 +00003432 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003433 i = PyInt_AsLong(v);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003434 switch (opcode) {
3435 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
3436 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
3437 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
3438 }
3439 cur_instr[1] = i & 0xff;
3440 cur_instr[2] = (i>>8) & 0xff;
Guido van Rossum282914b1991-04-04 10:42:56 +00003441 }
3442 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003443
Guido van Rossum681d79a1995-07-18 14:51:37 +00003444 if (c->c_errors == 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +00003445 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum282914b1991-04-04 10:42:56 +00003446}
3447
Guido van Rossum79f25d91997-04-29 20:08:16 +00003448PyCodeObject *
3449PyNode_Compile(n, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003450 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003451 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003452{
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003453 return jcompile(n, filename, NULL);
3454}
3455
Guido van Rossum79f25d91997-04-29 20:08:16 +00003456static PyCodeObject *
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003457icompile(n, base)
3458 node *n;
3459 struct compiling *base;
3460{
3461 return jcompile(n, base->c_filename, base);
3462}
3463
Guido van Rossum79f25d91997-04-29 20:08:16 +00003464static PyCodeObject *
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003465jcompile(n, filename, base)
3466 node *n;
3467 char *filename;
3468 struct compiling *base;
3469{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003470 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003471 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00003472 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003473 return NULL;
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003474#ifdef PRIVATE_NAME_MANGLING
3475 if (base)
3476 sc.c_private = base->c_private;
3477 else
3478 sc.c_private = NULL;
3479#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003480 compile_node(&sc, n);
3481 com_done(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003482 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003483 optimize(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003484 sc.c_flags |= CO_NEWLOCALS;
3485 }
3486 else if (TYPE(n) == classdef)
3487 sc.c_flags |= CO_NEWLOCALS;
Guido van Rossum9bfef441993-03-29 10:43:31 +00003488 co = NULL;
3489 if (sc.c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003490 PyObject *consts, *names, *varnames, *filename, *name;
3491 consts = PyList_AsTuple(sc.c_consts);
3492 names = PyList_AsTuple(sc.c_names);
3493 varnames = PyList_AsTuple(sc.c_varnames);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003494 filename = PyString_InternFromString(sc.c_filename);
3495 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003496 if (!PyErr_Occurred())
3497 co = PyCode_New(sc.c_argcount,
Guido van Rossum681d79a1995-07-18 14:51:37 +00003498 sc.c_nlocals,
Guido van Rossum8b993a91997-01-17 21:04:03 +00003499 sc.c_maxstacklevel,
Guido van Rossum681d79a1995-07-18 14:51:37 +00003500 sc.c_flags,
3501 sc.c_code,
3502 consts,
3503 names,
3504 varnames,
3505 filename,
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003506 name,
3507 sc.c_firstlineno,
3508 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003509 Py_XDECREF(consts);
3510 Py_XDECREF(names);
3511 Py_XDECREF(varnames);
3512 Py_XDECREF(filename);
3513 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00003514 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003515 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003516 return co;
3517}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003518
3519int
3520PyCode_Addr2Line(co, addrq)
3521 PyCodeObject *co;
3522 int addrq;
3523{
3524 int size = PyString_Size(co->co_lnotab) / 2;
Guido van Rossum2174dcb1999-09-15 22:48:09 +00003525 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003526 int line = co->co_firstlineno;
3527 int addr = 0;
3528 while (--size >= 0) {
3529 addr += *p++;
3530 if (addr > addrq)
3531 break;
3532 line += *p++;
3533 }
3534 return line;
3535}