Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(29894)

Delta Between Two Patch Sets: Python/compile.c

Issue 27095: Simplify MAKE_FUNCTION
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: Created 3 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode. 2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 * 3 *
4 * The primary entry point is PyAST_Compile(), which returns a 4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code 5 * PyCodeObject. The compiler makes several passes to build the code
6 * object: 6 * object:
7 * 1. Checks for future statements. See future.c 7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c. 8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate code for basic blocks. See compiler_mod() in this file. 9 * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 * 4. Assemble the basic blocks into final code. See assemble() in 10 * 4. Assemble the basic blocks into final code. See assemble() in
(...skipping 11 matching lines...) Expand all
22 */ 22 */
23 23
24 #include "Python.h" 24 #include "Python.h"
25 25
26 #include "Python-ast.h" 26 #include "Python-ast.h"
27 #include "node.h" 27 #include "node.h"
28 #include "ast.h" 28 #include "ast.h"
29 #include "code.h" 29 #include "code.h"
30 #include "symtable.h" 30 #include "symtable.h"
31 #include "opcode.h" 31 #include "opcode.h"
32 #include "wordcode_helpers.h"
32 33
33 #define DEFAULT_BLOCK_SIZE 16 34 #define DEFAULT_BLOCK_SIZE 16
34 #define DEFAULT_BLOCKS 8 35 #define DEFAULT_BLOCKS 8
35 #define DEFAULT_CODE_SIZE 128 36 #define DEFAULT_CODE_SIZE 128
36 #define DEFAULT_LNOTAB_SIZE 16 37 #define DEFAULT_LNOTAB_SIZE 16
37 38
38 #define COMP_GENEXP 0 39 #define COMP_GENEXP 0
39 #define COMP_LISTCOMP 1 40 #define COMP_LISTCOMP 1
40 #define COMP_SETCOMP 2 41 #define COMP_SETCOMP 2
41 #define COMP_DICTCOMP 3 42 #define COMP_DICTCOMP 3
42 43
43 struct instr { 44 struct instr {
44 unsigned i_jabs : 1; 45 unsigned i_jabs : 1;
45 unsigned i_jrel : 1; 46 unsigned i_jrel : 1;
46 unsigned i_hasarg : 1;
47 unsigned char i_opcode; 47 unsigned char i_opcode;
48 int i_oparg; 48 int i_oparg;
49 struct basicblock_ *i_target; /* target block (if jump instruction) */ 49 struct basicblock_ *i_target; /* target block (if jump instruction) */
50 int i_lineno; 50 int i_lineno;
51 }; 51 };
52 52
53 typedef struct basicblock_ { 53 typedef struct basicblock_ {
54 /* Each basicblock in a compilation unit is linked via b_list in the 54 /* Each basicblock in a compilation unit is linked via b_list in the
55 reverse order that the block are allocated. b_list points to the next 55 reverse order that the block are allocated. b_list points to the next
56 block, not to be confused with b_next, which is next by control flow. */ 56 block, not to be confused with b_next, which is next by control flow. */
(...skipping 916 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 return 1-oparg; 973 return 1-oparg;
974 case BUILD_LIST_UNPACK: 974 case BUILD_LIST_UNPACK:
975 case BUILD_TUPLE_UNPACK: 975 case BUILD_TUPLE_UNPACK:
976 case BUILD_SET_UNPACK: 976 case BUILD_SET_UNPACK:
977 case BUILD_MAP_UNPACK: 977 case BUILD_MAP_UNPACK:
978 return 1 - oparg; 978 return 1 - oparg;
979 case BUILD_MAP_UNPACK_WITH_CALL: 979 case BUILD_MAP_UNPACK_WITH_CALL:
980 return 1 - (oparg & 0xFF); 980 return 1 - (oparg & 0xFF);
981 case BUILD_MAP: 981 case BUILD_MAP:
982 return 1 - 2*oparg; 982 return 1 - 2*oparg;
983 case BUILD_CONST_KEY_MAP:
984 return -oparg;
983 case LOAD_ATTR: 985 case LOAD_ATTR:
984 return 0; 986 return 0;
985 case COMPARE_OP: 987 case COMPARE_OP:
986 return -1; 988 return -1;
987 case IMPORT_NAME: 989 case IMPORT_NAME:
988 return -1; 990 return -1;
989 case IMPORT_FROM: 991 case IMPORT_FROM:
990 return 1; 992 return 1;
991 993
992 case JUMP_FORWARD: 994 case JUMP_FORWARD:
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1072 /* Add an opcode with no argument. 1074 /* Add an opcode with no argument.
1073 Returns 0 on failure, 1 on success. 1075 Returns 0 on failure, 1 on success.
1074 */ 1076 */
1075 1077
1076 static int 1078 static int
1077 compiler_addop(struct compiler *c, int opcode) 1079 compiler_addop(struct compiler *c, int opcode)
1078 { 1080 {
1079 basicblock *b; 1081 basicblock *b;
1080 struct instr *i; 1082 struct instr *i;
1081 int off; 1083 int off;
1084 assert(!HAS_ARG(opcode));
1082 off = compiler_next_instr(c, c->u->u_curblock); 1085 off = compiler_next_instr(c, c->u->u_curblock);
1083 if (off < 0) 1086 if (off < 0)
1084 return 0; 1087 return 0;
1085 b = c->u->u_curblock; 1088 b = c->u->u_curblock;
1086 i = &b->b_instr[off]; 1089 i = &b->b_instr[off];
1087 i->i_opcode = opcode; 1090 i->i_opcode = opcode;
1088 i->i_hasarg = 0; 1091 i->i_oparg = 0;
1089 if (opcode == RETURN_VALUE) 1092 if (opcode == RETURN_VALUE)
1090 b->b_return = 1; 1093 b->b_return = 1;
1091 compiler_set_lineno(c, off); 1094 compiler_set_lineno(c, off);
1092 return 1; 1095 return 1;
1093 } 1096 }
1094 1097
1095 static Py_ssize_t 1098 static Py_ssize_t
1096 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) 1099 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1097 { 1100 {
1098 PyObject *t, *v; 1101 PyObject *t, *v;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1160 compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) 1163 compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1161 { 1164 {
1162 struct instr *i; 1165 struct instr *i;
1163 int off; 1166 int off;
1164 1167
1165 /* oparg value is unsigned, but a signed C int is usually used to store 1168 /* oparg value is unsigned, but a signed C int is usually used to store
1166 it in the C code (like Python/ceval.c). 1169 it in the C code (like Python/ceval.c).
1167 1170
1168 Limit to 32-bit signed C int (rather than INT_MAX) for portability. 1171 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1169 1172
1170 The argument of a concrete bytecode instruction is limited to 16-bit. 1173 The argument of a concrete bytecode instruction is limited to 8-bit.
1171 EXTENDED_ARG is used for 32-bit arguments. */ 1174 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1175 assert(HAS_ARG(opcode));
1172 assert(0 <= oparg && oparg <= 2147483647); 1176 assert(0 <= oparg && oparg <= 2147483647);
1173 1177
1174 off = compiler_next_instr(c, c->u->u_curblock); 1178 off = compiler_next_instr(c, c->u->u_curblock);
1175 if (off < 0) 1179 if (off < 0)
1176 return 0; 1180 return 0;
1177 i = &c->u->u_curblock->b_instr[off]; 1181 i = &c->u->u_curblock->b_instr[off];
1178 i->i_opcode = opcode; 1182 i->i_opcode = opcode;
1179 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); 1183 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1180 i->i_hasarg = 1;
1181 compiler_set_lineno(c, off); 1184 compiler_set_lineno(c, off);
1182 return 1; 1185 return 1;
1183 } 1186 }
1184 1187
1185 static int 1188 static int
1186 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) 1189 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1187 { 1190 {
1188 struct instr *i; 1191 struct instr *i;
1189 int off; 1192 int off;
1190 1193
1194 assert(HAS_ARG(opcode));
1191 assert(b != NULL); 1195 assert(b != NULL);
1192 off = compiler_next_instr(c, c->u->u_curblock); 1196 off = compiler_next_instr(c, c->u->u_curblock);
1193 if (off < 0) 1197 if (off < 0)
1194 return 0; 1198 return 0;
1195 i = &c->u->u_curblock->b_instr[off]; 1199 i = &c->u->u_curblock->b_instr[off];
1196 i->i_opcode = opcode; 1200 i->i_opcode = opcode;
1197 i->i_target = b; 1201 i->i_target = b;
1198 i->i_hasarg = 1;
1199 if (absolute) 1202 if (absolute)
1200 i->i_jabs = 1; 1203 i->i_jabs = 1;
1201 else 1204 else
1202 i->i_jrel = 1; 1205 i->i_jrel = 1;
1203 compiler_set_lineno(c, off); 1206 compiler_set_lineno(c, off);
1204 return 1; 1207 return 1;
1205 } 1208 }
1206 1209
1207 /* NEXT_BLOCK() creates an implicit jump from the current block 1210 /* NEXT_BLOCK() creates an implicit jump from the current block
1208 to the new block. 1211 to the new block.
(...skipping 14 matching lines...) Expand all
1223 #define ADDOP_IN_SCOPE(C, OP) { \ 1226 #define ADDOP_IN_SCOPE(C, OP) { \
1224 if (!compiler_addop((C), (OP))) { \ 1227 if (!compiler_addop((C), (OP))) { \
1225 compiler_exit_scope(c); \ 1228 compiler_exit_scope(c); \
1226 return 0; \ 1229 return 0; \
1227 } \ 1230 } \
1228 } 1231 }
1229 1232
1230 #define ADDOP_O(C, OP, O, TYPE) { \ 1233 #define ADDOP_O(C, OP, O, TYPE) { \
1231 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ 1234 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1232 return 0; \ 1235 return 0; \
1236 }
1237
1238 /* Same as ADDOP_O, but steals a reference. */
1239 #define ADDOP_N(C, OP, O, TYPE) { \
1240 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1241 Py_DECREF((O)); \
1242 return 0; \
1243 } \
1244 Py_DECREF((O)); \
1233 } 1245 }
1234 1246
1235 #define ADDOP_NAME(C, OP, O, TYPE) { \ 1247 #define ADDOP_NAME(C, OP, O, TYPE) { \
1236 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ 1248 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1237 return 0; \ 1249 return 0; \
1238 } 1250 }
1239 1251
1240 #define ADDOP_I(C, OP, O) { \ 1252 #define ADDOP_I(C, OP, O) { \
1241 if (!compiler_addop_i((C), (OP), (O))) \ 1253 if (!compiler_addop_i((C), (OP), (O))) \
1242 return 0; \ 1254 return 0; \
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1298 static int 1310 static int
1299 compiler_isdocstring(stmt_ty s) 1311 compiler_isdocstring(stmt_ty s)
1300 { 1312 {
1301 if (s->kind != Expr_kind) 1313 if (s->kind != Expr_kind)
1302 return 0; 1314 return 0;
1303 if (s->v.Expr.value->kind == Str_kind) 1315 if (s->v.Expr.value->kind == Str_kind)
1304 return 1; 1316 return 1;
1305 if (s->v.Expr.value->kind == Constant_kind) 1317 if (s->v.Expr.value->kind == Constant_kind)
1306 return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value); 1318 return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value);
1307 return 0; 1319 return 0;
1320 }
1321
1322 static int
1323 is_const(expr_ty e)
1324 {
1325 switch (e->kind) {
1326 case Constant_kind:
1327 case Num_kind:
1328 case Str_kind:
1329 case Bytes_kind:
1330 case Ellipsis_kind:
1331 case NameConstant_kind:
1332 return 1;
1333 default:
1334 return 0;
1335 }
1336 }
1337
1338 static PyObject *
1339 get_const_value(expr_ty e)
1340 {
1341 switch (e->kind) {
1342 case Constant_kind:
1343 return e->v.Constant.value;
1344 case Num_kind:
1345 return e->v.Num.n;
1346 case Str_kind:
1347 return e->v.Str.s;
1348 case Bytes_kind:
1349 return e->v.Bytes.s;
1350 case Ellipsis_kind:
1351 return Py_Ellipsis;
1352 case NameConstant_kind:
1353 return e->v.NameConstant.value;
1354 default:
1355 assert(!is_const(e));
1356 return NULL;
1357 }
1308 } 1358 }
1309 1359
1310 /* Compile a sequence of statements, checking for a docstring. */ 1360 /* Compile a sequence of statements, checking for a docstring. */
1311 1361
1312 static int 1362 static int
1313 compiler_body(struct compiler *c, asdl_seq *stmts) 1363 compiler_body(struct compiler *c, asdl_seq *stmts)
1314 { 1364 {
1315 int i = 0; 1365 int i = 0;
1316 stmt_ty st; 1366 stmt_ty st;
1317 1367
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
1414 if (k == NULL) 1464 if (k == NULL)
1415 return -1; 1465 return -1;
1416 v = PyDict_GetItem(dict, k); 1466 v = PyDict_GetItem(dict, k);
1417 Py_DECREF(k); 1467 Py_DECREF(k);
1418 if (v == NULL) 1468 if (v == NULL)
1419 return -1; 1469 return -1;
1420 return PyLong_AS_LONG(v); 1470 return PyLong_AS_LONG(v);
1421 } 1471 }
1422 1472
1423 static int 1473 static int
1424 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyO bject *qualname) 1474 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, Py Object *qualname)
1425 { 1475 {
1426 Py_ssize_t i, free = PyCode_GetNumFree(co); 1476 Py_ssize_t i, free = PyCode_GetNumFree(co);
1427 if (qualname == NULL) 1477 if (qualname == NULL)
1428 qualname = co->co_name; 1478 qualname = co->co_name;
1429 1479
1430 if (free == 0) { 1480 if (free) {
1431 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1481 for (i = 0; i < free; ++i) {
1432 ADDOP_O(c, LOAD_CONST, qualname, consts); 1482 /* Bypass com_addop_varname because it will generate
1433 ADDOP_I(c, MAKE_FUNCTION, args); 1483 LOAD_DEREF but LOAD_CLOSURE is needed.
1434 return 1; 1484 */
1435 } 1485 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1436 for (i = 0; i < free; ++i) { 1486 int arg, reftype;
1437 /* Bypass com_addop_varname because it will generate 1487
1438 LOAD_DEREF but LOAD_CLOSURE is needed. 1488 /* Special case: If a class contains a method with a
1439 */ 1489 free variable that has the same name as a method,
1440 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); 1490 the name will be considered free *and* local in the
1441 int arg, reftype; 1491 class. It should be handled by the closure, as
1442 1492 well as by the normal name loookup logic.
1443 /* Special case: If a class contains a method with a 1493 */
1444 free variable that has the same name as a method, 1494 reftype = get_ref_type(c, name);
1445 the name will be considered free *and* local in the 1495 if (reftype == CELL)
1446 class. It should be handled by the closure, as 1496 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1447 well as by the normal name loookup logic. 1497 else /* (reftype == FREE) */
1448 */ 1498 arg = compiler_lookup_arg(c->u->u_freevars, name);
1449 reftype = get_ref_type(c, name); 1499 if (arg == -1) {
1450 if (reftype == CELL) 1500 fprintf(stderr,
1451 arg = compiler_lookup_arg(c->u->u_cellvars, name); 1501 "lookup %s in %s %d %d\n"
1452 else /* (reftype == FREE) */ 1502 "freevars of %s: %s\n",
1453 arg = compiler_lookup_arg(c->u->u_freevars, name); 1503 PyUnicode_AsUTF8(PyObject_Repr(name)),
1454 if (arg == -1) { 1504 PyUnicode_AsUTF8(c->u->u_name),
1455 fprintf(stderr, 1505 reftype, arg,
1456 "lookup %s in %s %d %d\n" 1506 PyUnicode_AsUTF8(co->co_name),
1457 "freevars of %s: %s\n", 1507 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1458 PyUnicode_AsUTF8(PyObject_Repr(name)), 1508 Py_FatalError("compiler_make_closure()");
1459 PyUnicode_AsUTF8(c->u->u_name), 1509 }
1460 reftype, arg, 1510 ADDOP_I(c, LOAD_CLOSURE, arg);
1461 PyUnicode_AsUTF8(co->co_name), 1511 }
1462 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); 1512 flags |= 0x08;
1463 Py_FatalError("compiler_make_closure()"); 1513 ADDOP_I(c, BUILD_TUPLE, free);
1464 } 1514 }
1465 ADDOP_I(c, LOAD_CLOSURE, arg);
1466 }
1467 ADDOP_I(c, BUILD_TUPLE, free);
1468 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1515 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1469 ADDOP_O(c, LOAD_CONST, qualname, consts); 1516 ADDOP_O(c, LOAD_CONST, qualname, consts);
1470 ADDOP_I(c, MAKE_FUNCTION, args | 0x08); 1517 ADDOP_I(c, MAKE_FUNCTION, flags);
1471 return 1; 1518 return 1;
1472 } 1519 }
1473 1520
1474 static int 1521 static int
1475 compiler_decorators(struct compiler *c, asdl_seq* decos) 1522 compiler_decorators(struct compiler *c, asdl_seq* decos)
1476 { 1523 {
1477 int i; 1524 int i;
1478 1525
1479 if (!decos) 1526 if (!decos)
1480 return 1; 1527 return 1;
1481 1528
1482 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1529 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1483 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); 1530 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1484 } 1531 }
1485 return 1; 1532 return 1;
1486 } 1533 }
1487 1534
1488 static int 1535 static Py_ssize_t
1489 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, 1536 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1490 asdl_seq *kw_defaults) 1537 asdl_seq *kw_defaults)
1491 { 1538 {
1492 int i, default_count = 0; 1539 int i;
1540 PyObject *keys = NULL;
1541
1493 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { 1542 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1494 arg_ty arg = asdl_seq_GET(kwonlyargs, i); 1543 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1495 expr_ty default_ = asdl_seq_GET(kw_defaults, i); 1544 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1496 if (default_) { 1545 if (default_) {
1497 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); 1546 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1498 if (!mangled) { 1547 if (!mangled) {
1499 return -1; 1548 goto error;
1500 } 1549 }
1501 ADDOP_O(c, LOAD_CONST, mangled, consts); 1550 if (keys == NULL) {
1551 keys = PyList_New(1);
1552 if (keys == NULL) {
1553 Py_DECREF(mangled);
1554 return -1;
1555 }
1556 PyList_SET_ITEM(keys, 0, mangled);
1557 }
1558 else {
1559 int res = PyList_Append(keys, mangled);
1560 Py_DECREF(mangled);
1561 if (res == -1) {
1562 goto error;
1563 }
1564 }
1565 if (!compiler_visit_expr(c, default_)) {
1566 goto error;
1567 }
1568 }
1569 }
1570 if (keys != NULL) {
1571 Py_ssize_t default_count = PyList_Size(keys);
1572 PyObject *keys_tuple = PyList_AsTuple(keys);
1573 Py_DECREF(keys);
1574 if (keys_tuple == NULL) {
1575 return -1;
1576 }
1577 ADDOP_N(c, LOAD_CONST, keys_tuple, consts);
1578 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
1579 return default_count;
1580 }
1581 else {
1582 return 0;
1583 }
1584
1585 error:
1586 Py_XDECREF(keys);
1587 return -1;
1588 }
1589
1590 static int
1591 compiler_visit_argannotation(struct compiler *c, identifier id,
1592 expr_ty annotation, PyObject *names)
1593 {
1594 if (annotation) {
1595 PyObject *mangled;
1596 VISIT(c, expr, annotation);
1597 mangled = _Py_Mangle(c->u->u_private, id);
1598 if (!mangled)
1599 return 0;
1600 if (PyList_Append(names, mangled) < 0) {
1502 Py_DECREF(mangled); 1601 Py_DECREF(mangled);
1503 if (!compiler_visit_expr(c, default_)) { 1602 return 0;
1504 return -1; 1603 }
1505 }
1506 default_count++;
1507 }
1508 }
1509 if (default_count > 0) {
1510 ADDOP_I(c, BUILD_MAP, default_count);
1511 }
1512 return default_count;
1513 }
1514
1515 static int
1516 compiler_visit_argannotation(struct compiler *c, identifier id,
1517 expr_ty annotation)
1518 {
1519 if (annotation) {
1520 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
1521 if (!mangled) {
1522 return -1;
1523 }
1524 ADDOP_O(c, LOAD_CONST, mangled, consts);
1525 Py_DECREF(mangled); 1604 Py_DECREF(mangled);
1526 VISIT(c, expr, annotation); 1605 }
1527 return 1; 1606 return 1;
1528 } 1607 }
1529 return 0; 1608
1530 } 1609 static int
1531 1610 compiler_visit_argannotations(struct compiler *c, asdl_seq *args,
1532 static Py_ssize_t 1611 PyObject *names)
1533 compiler_visit_argannotations(struct compiler *c, asdl_seq* args)
1534 { 1612 {
1535 int i; 1613 int i;
1536 Py_ssize_t annonum = 0, arglen = asdl_seq_LEN(args); 1614 for (i = 0; i < asdl_seq_LEN(args); i++) {
1537 for (i = 0; i < arglen; i++) {
1538 arg_ty arg = (arg_ty)asdl_seq_GET(args, i); 1615 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1539 Py_ssize_t annoret = compiler_visit_argannotation( 1616 if (!compiler_visit_argannotation(
1540 c, 1617 c,
1541 arg->arg, 1618 arg->arg,
1542 arg->annotation); 1619 arg->annotation,
1543 if (annoret == -1) { 1620 names))
1544 return -1; 1621 return 0;
1545 } 1622 }
1546 annonum += annoret; 1623 return 1;
1547 }
1548
1549 return annonum;
1550 } 1624 }
1551 1625
1552 static Py_ssize_t 1626 static Py_ssize_t
1553 compiler_visit_annotations(struct compiler *c, arguments_ty args, 1627 compiler_visit_annotations(struct compiler *c, arguments_ty args,
1554 expr_ty returns) 1628 expr_ty returns)
1555 { 1629 {
1556 /* Push arg annotation dict. Return # of items pushed. 1630 /* Push arg annotation dict. Return # of items pushed.
1557 The expressions are evaluated out-of-order wrt the source code. 1631 The expressions are evaluated out-of-order wrt the source code.
1558 1632
1559 Returns -1 on error. 1633 Returns -1 on error.
1560 */ 1634 */
1561 static identifier return_str; 1635 static identifier return_str;
1562 Py_ssize_t len = 0, annoret; 1636 PyObject *names;
1563 1637 Py_ssize_t len;
1564 annoret = compiler_visit_argannotations(c, args->args); 1638 names = PyList_New(0);
1565 if (annoret == -1) { 1639 if (!names)
1566 return -1; 1640 return -1;
1567 } 1641
1568 len += annoret; 1642 if (!compiler_visit_argannotations(c, args->args, names))
1569 1643 goto error;
1570 if (args->vararg && args->vararg->annotation) { 1644 if (args->vararg && args->vararg->annotation &&
1571 annoret = compiler_visit_argannotation(c, args->vararg->arg, 1645 !compiler_visit_argannotation(c, args->vararg->arg,
1572 args->vararg->annotation); 1646 args->vararg->annotation, names))
1573 if (annoret == -1) { 1647 goto error;
1574 return -1; 1648 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
1575 } 1649 goto error;
1576 len += annoret; 1650 if (args->kwarg && args->kwarg->annotation &&
1577 } 1651 !compiler_visit_argannotation(c, args->kwarg->arg,
1578 1652 args->kwarg->annotation, names))
1579 annoret = compiler_visit_argannotations(c, args->kwonlyargs); 1653 goto error;
1580 if (annoret == -1) {
1581 return -1;
1582 }
1583 len += annoret;
1584
1585 if (args->kwarg && args->kwarg->annotation) {
1586 annoret = compiler_visit_argannotation(c, args->kwarg->arg,
1587 args->kwarg->annotation);
1588 if (annoret == -1) {
1589 return -1;
1590 }
1591 len += annoret;
1592 }
1593 1654
1594 if (!return_str) { 1655 if (!return_str) {
1595 return_str = PyUnicode_InternFromString("return"); 1656 return_str = PyUnicode_InternFromString("return");
1596 if (!return_str) { 1657 if (!return_str)
1597 return -1; 1658 goto error;
1598 } 1659 }
1599 } 1660 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
1600 1661 goto error;
1601 annoret = compiler_visit_argannotation(c, return_str, returns); 1662 }
1602 if (annoret == -1) { 1663
1603 return -1; 1664 len = PyList_GET_SIZE(names);
1604 } 1665 if (len) {
1605 len += annoret; 1666 PyObject *keytuple = PyList_AsTuple(names);
1606 1667 Py_DECREF(names);
1607 if (len != 0) { 1668 if (keytuple == NULL) {
1608 ADDOP_I(c, BUILD_MAP, len); 1669 return -1;
1670 }
1671 ADDOP_N(c, LOAD_CONST, keytuple, consts);
1672 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
1673 }
1674 else {
1675 Py_DECREF(names);
1609 } 1676 }
1610 return len; 1677 return len;
1678
1679 error:
1680 Py_DECREF(names);
1681 return -1;
1682 }
1683
1684 static Py_ssize_t
1685 compiler_default_arguments(struct compiler *c, arguments_ty args)
1686 {
1687 Py_ssize_t funcflags = 0;
1688 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
1689 VISIT_SEQ(c, expr, args->defaults);
1690 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1691 funcflags |= 0x01;
1692 }
1693 if (args->kwonlyargs) {
1694 Py_ssize_t res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1695 args->kw_defaults);
1696 if (res < 0) {
1697 return -1;
1698 }
1699 else if (res > 0) {
1700 funcflags |= 0x02;
1701 }
1702 }
1703 return funcflags;
1611 } 1704 }
1612 1705
1613 static int 1706 static int
1614 compiler_function(struct compiler *c, stmt_ty s, int is_async) 1707 compiler_function(struct compiler *c, stmt_ty s, int is_async)
1615 { 1708 {
1616 PyCodeObject *co; 1709 PyCodeObject *co;
1617 PyObject *qualname, *first_const = Py_None; 1710 PyObject *qualname, *first_const = Py_None;
1618 arguments_ty args; 1711 arguments_ty args;
1619 expr_ty returns; 1712 expr_ty returns;
1620 identifier name; 1713 identifier name;
1621 asdl_seq* decos; 1714 asdl_seq* decos;
1622 asdl_seq *body; 1715 asdl_seq *body;
1623 stmt_ty st; 1716 stmt_ty st;
1624 Py_ssize_t i, n, funcflags = 0; 1717 Py_ssize_t i, n, funcflags;
1625 int docstring; 1718 int docstring;
1626 int num_annotations; 1719 int num_annotations;
1627 int scope_type; 1720 int scope_type;
1628
1629 1721
1630 if (is_async) { 1722 if (is_async) {
1631 assert(s->kind == AsyncFunctionDef_kind); 1723 assert(s->kind == AsyncFunctionDef_kind);
1632 1724
1633 args = s->v.AsyncFunctionDef.args; 1725 args = s->v.AsyncFunctionDef.args;
1634 returns = s->v.AsyncFunctionDef.returns; 1726 returns = s->v.AsyncFunctionDef.returns;
1635 decos = s->v.AsyncFunctionDef.decorator_list; 1727 decos = s->v.AsyncFunctionDef.decorator_list;
1636 name = s->v.AsyncFunctionDef.name; 1728 name = s->v.AsyncFunctionDef.name;
1637 body = s->v.AsyncFunctionDef.body; 1729 body = s->v.AsyncFunctionDef.body;
1638 1730
1639 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; 1731 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1640 } else { 1732 } else {
1641 assert(s->kind == FunctionDef_kind); 1733 assert(s->kind == FunctionDef_kind);
1642 1734
1643 args = s->v.FunctionDef.args; 1735 args = s->v.FunctionDef.args;
1644 returns = s->v.FunctionDef.returns; 1736 returns = s->v.FunctionDef.returns;
1645 decos = s->v.FunctionDef.decorator_list; 1737 decos = s->v.FunctionDef.decorator_list;
1646 name = s->v.FunctionDef.name; 1738 name = s->v.FunctionDef.name;
1647 body = s->v.FunctionDef.body; 1739 body = s->v.FunctionDef.body;
1648 1740
1649 scope_type = COMPILER_SCOPE_FUNCTION; 1741 scope_type = COMPILER_SCOPE_FUNCTION;
1650 } 1742 }
1651 1743
1652 if (!compiler_decorators(c, decos)) 1744 if (!compiler_decorators(c, decos))
1653 return 0; 1745 return 0;
1654 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { 1746
1655 VISIT_SEQ(c, expr, args->defaults); 1747 funcflags = compiler_default_arguments(c, args);
1656 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); 1748 if (funcflags == -1) {
1657 funcflags |= 0x01; 1749 return 0;
1658 } 1750 }
1659 if (args->kwonlyargs) { 1751
1660 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1661 args->kw_defaults);
1662 if (res < 0) {
1663 return 0;
1664 }
1665 else if (res > 0) {
1666 funcflags |= 0x02;
1667 }
1668 }
1669 num_annotations = compiler_visit_annotations(c, args, returns); 1752 num_annotations = compiler_visit_annotations(c, args, returns);
1670 if (num_annotations < 0) { 1753 if (num_annotations < 0) {
1671 return 0; 1754 return 0;
1672 } 1755 }
1673 else if (num_annotations > 0) { 1756 else if (num_annotations > 0) {
1674 funcflags |= 0x04; 1757 funcflags |= 0x04;
1675 } 1758 }
1676 1759
1677 if (!compiler_enter_scope(c, name, 1760 if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
1678 scope_type, (void *)s, 1761 return 0;
1679 s->lineno)) 1762 }
1680 return 0;
1681 1763
1682 st = (stmt_ty)asdl_seq_GET(body, 0); 1764 st = (stmt_ty)asdl_seq_GET(body, 0);
1683 docstring = compiler_isdocstring(st); 1765 docstring = compiler_isdocstring(st);
1684 if (docstring && c->c_optimize < 2) { 1766 if (docstring && c->c_optimize < 2) {
1685 if (st->v.Expr.value->kind == Constant_kind) 1767 if (st->v.Expr.value->kind == Constant_kind)
1686 first_const = st->v.Expr.value->v.Constant.value; 1768 first_const = st->v.Expr.value->v.Constant.value;
1687 else 1769 else
1688 first_const = st->v.Expr.value->v.Str.s; 1770 first_const = st->v.Expr.value->v.Str.s;
1689 } 1771 }
1690 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { 1772 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
1865 compiler_use_next_block(c, end); 1947 compiler_use_next_block(c, end);
1866 return 1; 1948 return 1;
1867 } 1949 }
1868 1950
1869 static int 1951 static int
1870 compiler_lambda(struct compiler *c, expr_ty e) 1952 compiler_lambda(struct compiler *c, expr_ty e)
1871 { 1953 {
1872 PyCodeObject *co; 1954 PyCodeObject *co;
1873 PyObject *qualname; 1955 PyObject *qualname;
1874 static identifier name; 1956 static identifier name;
1875 Py_ssize_t funcflags = 0; 1957 Py_ssize_t funcflags;
1876 arguments_ty args = e->v.Lambda.args; 1958 arguments_ty args = e->v.Lambda.args;
1877 assert(e->kind == Lambda_kind); 1959 assert(e->kind == Lambda_kind);
1878 1960
1879 if (!name) { 1961 if (!name) {
1880 name = PyUnicode_InternFromString("<lambda>"); 1962 name = PyUnicode_InternFromString("<lambda>");
1881 if (!name) 1963 if (!name)
1882 return 0; 1964 return 0;
1883 } 1965 }
1884 1966
1885 if (args->defaults && asdl_seq_LEN(args->defaults) > 0){ 1967 funcflags = compiler_default_arguments(c, args);
1886 VISIT_SEQ(c, expr, args->defaults); 1968 if (funcflags == -1) {
1887 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); 1969 return 0;
1888 funcflags |= 0x01; 1970 }
1889 } 1971
1890 if (args->kwonlyargs) {
1891 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1892 args->kw_defaults);
1893 if (res < 0) {
1894 return 0;
1895 }
1896 else if (res > 0) {
1897 funcflags |= 0x02;
1898 }
1899 }
1900 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, 1972 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
1901 (void *)e, e->lineno)) 1973 (void *)e, e->lineno))
1902 return 0; 1974 return 0;
1903 1975
1904 /* Make None the first constant, so the lambda can't have a 1976 /* Make None the first constant, so the lambda can't have a
1905 docstring. */ 1977 docstring. */
1906 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0) 1978 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1907 return 0; 1979 return 0;
1908 1980
1909 c->u->u_argcount = asdl_seq_LEN(args->args); 1981 c->u->u_argcount = asdl_seq_LEN(args->args);
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after
2599 2671
2600 static int 2672 static int
2601 compiler_visit_stmt_expr(struct compiler *c, expr_ty value) 2673 compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
2602 { 2674 {
2603 if (c->c_interactive && c->c_nestlevel <= 1) { 2675 if (c->c_interactive && c->c_nestlevel <= 1) {
2604 VISIT(c, expr, value); 2676 VISIT(c, expr, value);
2605 ADDOP(c, PRINT_EXPR); 2677 ADDOP(c, PRINT_EXPR);
2606 return 1; 2678 return 1;
2607 } 2679 }
2608 2680
2609 switch (value->kind) 2681 if (is_const(value)) {
2610 {
2611 case Str_kind:
2612 case Num_kind:
2613 case Ellipsis_kind:
2614 case Bytes_kind:
2615 case NameConstant_kind:
2616 case Constant_kind:
2617 /* ignore constant statement */ 2682 /* ignore constant statement */
2618 return 1; 2683 return 1;
2619
2620 default:
2621 break;
2622 } 2684 }
2623 2685
2624 VISIT(c, expr, value); 2686 VISIT(c, expr, value);
2625 ADDOP(c, POP_TOP); 2687 ADDOP(c, POP_TOP);
2626 return 1; 2688 return 1;
2627 } 2689 }
2628 2690
2629 static int 2691 static int
2630 compiler_visit_stmt(struct compiler *c, stmt_ty s) 2692 compiler_visit_stmt(struct compiler *c, stmt_ty s)
2631 { 2693 {
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
3091 } 3153 }
3092 3154
3093 static int 3155 static int
3094 compiler_set(struct compiler *c, expr_ty e) 3156 compiler_set(struct compiler *c, expr_ty e)
3095 { 3157 {
3096 return starunpack_helper(c, e->v.Set.elts, BUILD_SET, 3158 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3097 BUILD_SET, BUILD_SET_UNPACK); 3159 BUILD_SET, BUILD_SET_UNPACK);
3098 } 3160 }
3099 3161
3100 static int 3162 static int
3163 are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3164 {
3165 Py_ssize_t i;
3166 for (i = begin; i < end; i++) {
3167 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3168 if (key == NULL || !is_const(key))
3169 return 0;
3170 }
3171 return 1;
3172 }
3173
3174 static int
3175 compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end )
3176 {
3177 Py_ssize_t i, n = end - begin;
3178 PyObject *keys, *key;
3179 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3180 for (i = begin; i < end; i++) {
3181 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3182 }
3183 keys = PyTuple_New(n);
3184 if (keys == NULL) {
3185 return 0;
3186 }
3187 for (i = begin; i < end; i++) {
3188 key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3189 Py_INCREF(key);
3190 PyTuple_SET_ITEM(keys, i - begin, key);
3191 }
3192 ADDOP_N(c, LOAD_CONST, keys, consts);
3193 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3194 }
3195 else {
3196 for (i = begin; i < end; i++) {
3197 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3198 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3199 }
3200 ADDOP_I(c, BUILD_MAP, n);
3201 }
3202 return 1;
3203 }
3204
3205 static int
3101 compiler_dict(struct compiler *c, expr_ty e) 3206 compiler_dict(struct compiler *c, expr_ty e)
3102 { 3207 {
3103 Py_ssize_t i, n, elements; 3208 Py_ssize_t i, n, elements;
3104 int containers; 3209 int containers;
3105 int is_unpacking = 0; 3210 int is_unpacking = 0;
3106 n = asdl_seq_LEN(e->v.Dict.values); 3211 n = asdl_seq_LEN(e->v.Dict.values);
3107 containers = 0; 3212 containers = 0;
3108 elements = 0; 3213 elements = 0;
3109 for (i = 0; i < n; i++) { 3214 for (i = 0; i < n; i++) {
3110 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; 3215 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3111 if (elements == 0xFFFF || (elements && is_unpacking)) { 3216 if (elements == 0xFFFF || (elements && is_unpacking)) {
3112 ADDOP_I(c, BUILD_MAP, elements); 3217 if (!compiler_subdict(c, e, i - elements, i))
3218 return 0;
3113 containers++; 3219 containers++;
3114 elements = 0; 3220 elements = 0;
3115 } 3221 }
3116 if (is_unpacking) { 3222 if (is_unpacking) {
3117 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); 3223 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3118 containers++; 3224 containers++;
3119 } 3225 }
3120 else { 3226 else {
3121 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3122 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3123 elements++; 3227 elements++;
3124 } 3228 }
3125 } 3229 }
3126 if (elements || containers == 0) { 3230 if (elements || containers == 0) {
3127 ADDOP_I(c, BUILD_MAP, elements); 3231 if (!compiler_subdict(c, e, n - elements, n))
3232 return 0;
3128 containers++; 3233 containers++;
3129 } 3234 }
3130 /* If there is more than one dict, they need to be merged into a new 3235 /* If there is more than one dict, they need to be merged into a new
3131 * dict. If there is one dict and it's an unpacking, then it needs 3236 * dict. If there is one dict and it's an unpacking, then it needs
3132 * to be copied into a new dict." */ 3237 * to be copied into a new dict." */
3133 while (containers > 1 || is_unpacking) { 3238 while (containers > 1 || is_unpacking) {
3134 int oparg = containers < 255 ? containers : 255; 3239 int oparg = containers < 255 ? containers : 255;
3135 ADDOP_I(c, BUILD_MAP_UNPACK, oparg); 3240 ADDOP_I(c, BUILD_MAP_UNPACK, oparg);
3136 containers -= (oparg - 1); 3241 containers -= (oparg - 1);
3137 is_unpacking = 0; 3242 is_unpacking = 0;
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
3258 return 0; 3363 return 0;
3259 } 3364 }
3260 if (e->v.FormattedValue.format_spec) { 3365 if (e->v.FormattedValue.format_spec) {
3261 /* Evaluate the format spec, and update our opcode arg. */ 3366 /* Evaluate the format spec, and update our opcode arg. */
3262 VISIT(c, expr, e->v.FormattedValue.format_spec); 3367 VISIT(c, expr, e->v.FormattedValue.format_spec);
3263 oparg |= FVS_HAVE_SPEC; 3368 oparg |= FVS_HAVE_SPEC;
3264 } 3369 }
3265 3370
3266 /* And push our opcode and oparg */ 3371 /* And push our opcode and oparg */
3267 ADDOP_I(c, FORMAT_VALUE, oparg); 3372 ADDOP_I(c, FORMAT_VALUE, oparg);
3373 return 1;
3374 }
3375
3376 static int
3377 compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ ssize_t end)
3378 {
3379 Py_ssize_t i, n = end - begin;
3380 keyword_ty kw;
3381 PyObject *keys, *key;
3382 assert(n > 0);
3383 if (n > 1) {
3384 for (i = begin; i < end; i++) {
3385 kw = asdl_seq_GET(keywords, i);
3386 VISIT(c, expr, kw->value);
3387 }
3388 keys = PyTuple_New(n);
3389 if (keys == NULL) {
3390 return 0;
3391 }
3392 for (i = begin; i < end; i++) {
3393 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
3394 Py_INCREF(key);
3395 PyTuple_SET_ITEM(keys, i - begin, key);
3396 }
3397 ADDOP_N(c, LOAD_CONST, keys, consts);
3398 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3399 }
3400 else {
3401 /* a for loop only executes once */
3402 for (i = begin; i < end; i++) {
3403 kw = asdl_seq_GET(keywords, i);
3404 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3405 VISIT(c, expr, kw->value);
3406 }
3407 ADDOP_I(c, BUILD_MAP, n);
3408 }
3268 return 1; 3409 return 1;
3269 } 3410 }
3270 3411
3271 /* shared code between compiler_call and compiler_class */ 3412 /* shared code between compiler_call and compiler_class */
3272 static int 3413 static int
3273 compiler_call_helper(struct compiler *c, 3414 compiler_call_helper(struct compiler *c,
3274 int n, /* Args already pushed */ 3415 int n, /* Args already pushed */
3275 asdl_seq *args, 3416 asdl_seq *args,
3276 asdl_seq *keywords) 3417 asdl_seq *keywords)
3277 { 3418 {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
3327 } 3468 }
3328 3469
3329 /* Same dance again for keyword arguments */ 3470 /* Same dance again for keyword arguments */
3330 nseen = 0; /* the number of keyword arguments on the stack following */ 3471 nseen = 0; /* the number of keyword arguments on the stack following */
3331 nelts = asdl_seq_LEN(keywords); 3472 nelts = asdl_seq_LEN(keywords);
3332 for (i = 0; i < nelts; i++) { 3473 for (i = 0; i < nelts; i++) {
3333 keyword_ty kw = asdl_seq_GET(keywords, i); 3474 keyword_ty kw = asdl_seq_GET(keywords, i);
3334 if (kw->arg == NULL) { 3475 if (kw->arg == NULL) {
3335 /* A keyword argument unpacking. */ 3476 /* A keyword argument unpacking. */
3336 if (nseen) { 3477 if (nseen) {
3337 ADDOP_I(c, BUILD_MAP, nseen); 3478 if (nsubkwargs) {
3479 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3480 return 0;
3481 nsubkwargs++;
3482 }
3483 else {
3484 Py_ssize_t j;
3485 for (j = 0; j < nseen; j++) {
3486 VISIT(c, keyword, asdl_seq_GET(keywords, j));
3487 }
3488 nkw = nseen;
3489 }
3338 nseen = 0; 3490 nseen = 0;
3339 nsubkwargs++;
3340 } 3491 }
3341 VISIT(c, expr, kw->value); 3492 VISIT(c, expr, kw->value);
3342 nsubkwargs++; 3493 nsubkwargs++;
3343 } 3494 }
3344 else if (nsubkwargs) { 3495 else {
3345 /* A keyword argument and we already have a dict. */
3346 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3347 VISIT(c, expr, kw->value);
3348 nseen++; 3496 nseen++;
3349 } 3497 }
3498 }
3499 if (nseen) {
3500 if (nsubkwargs) {
3501 /* Pack up any trailing keyword arguments. */
3502 if (!compiler_subkwargs(c, keywords, nelts - nseen, nelts))
3503 return 0;
3504 nsubkwargs++;
3505 }
3350 else { 3506 else {
3351 /* keyword argument */ 3507 VISIT_SEQ(c, keyword, keywords);
3352 VISIT(c, keyword, kw) 3508 nkw = nseen;
3353 nkw++; 3509 }
3354 }
3355 }
3356 if (nseen) {
3357 /* Pack up any trailing keyword arguments. */
3358 ADDOP_I(c, BUILD_MAP, nseen);
3359 nsubkwargs++;
3360 } 3510 }
3361 if (nsubkwargs) { 3511 if (nsubkwargs) {
3362 code |= 2; 3512 code |= 2;
3363 if (nsubkwargs > 1) { 3513 if (nsubkwargs > 1) {
3364 /* Pack it all up */ 3514 /* Pack it all up */
3365 int function_pos = n + (code & 1) + nkw + 1; 3515 int function_pos = n + (code & 1) + 2 * nkw + 1;
3366 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8)); 3516 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8));
3367 } 3517 }
3368 } 3518 }
3369 assert(n < 1<<8); 3519 assert(n < 1<<8);
3370 assert(nkw < 1<<24); 3520 assert(nkw < 1<<24);
3371 n |= nkw << 8; 3521 n |= nkw << 8;
3372 3522
3373 switch (code) { 3523 switch (code) {
3374 case 0: 3524 case 0:
3375 ADDOP_I(c, CALL_FUNCTION, n); 3525 ADDOP_I(c, CALL_FUNCTION, n);
(...skipping 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after
4393 4543
4394 static void 4544 static void
4395 assemble_free(struct assembler *a) 4545 assemble_free(struct assembler *a)
4396 { 4546 {
4397 Py_XDECREF(a->a_bytecode); 4547 Py_XDECREF(a->a_bytecode);
4398 Py_XDECREF(a->a_lnotab); 4548 Py_XDECREF(a->a_lnotab);
4399 if (a->a_postorder) 4549 if (a->a_postorder)
4400 PyObject_Free(a->a_postorder); 4550 PyObject_Free(a->a_postorder);
4401 } 4551 }
4402 4552
4403 /* Return the size of a basic block in bytes. */
4404
4405 static int
4406 instrsize(struct instr *instr)
4407 {
4408 if (!instr->i_hasarg)
4409 return 1; /* 1 byte for the opcode*/
4410 if (instr->i_oparg > 0xffff)
4411 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (opa rg) + 2(oparg extended) */
4412 return 3; /* 1 (opcode) + 2 (oparg) */
4413 }
4414
4415 static int 4553 static int
4416 blocksize(basicblock *b) 4554 blocksize(basicblock *b)
4417 { 4555 {
4418 int i; 4556 int i;
4419 int size = 0; 4557 int size = 0;
4420 4558
4421 for (i = 0; i < b->b_iused; i++) 4559 for (i = 0; i < b->b_iused; i++)
4422 size += instrsize(&b->b_instr[i]); 4560 size += instrsize(b->b_instr[i].i_oparg);
4423 return size; 4561 return size;
4424 } 4562 }
4425 4563
4426 /* Appends a pair to the end of the line number table, a_lnotab, representing 4564 /* Appends a pair to the end of the line number table, a_lnotab, representing
4427 the instruction's bytecode offset and line number. See 4565 the instruction's bytecode offset and line number. See
4428 Objects/lnotab_notes.txt for the description of the line number table. */ 4566 Objects/lnotab_notes.txt for the description of the line number table. */
4429 4567
4430 static int 4568 static int
4431 assemble_lnotab(struct assembler *a, struct instr *i) 4569 assemble_lnotab(struct assembler *a, struct instr *i)
4432 { 4570 {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
4532 } 4670 }
4533 4671
4534 /* assemble_emit() 4672 /* assemble_emit()
4535 Extend the bytecode with a new instruction. 4673 Extend the bytecode with a new instruction.
4536 Update lnotab if necessary. 4674 Update lnotab if necessary.
4537 */ 4675 */
4538 4676
4539 static int 4677 static int
4540 assemble_emit(struct assembler *a, struct instr *i) 4678 assemble_emit(struct assembler *a, struct instr *i)
4541 { 4679 {
4542 int size, arg = 0, ext = 0; 4680 int size, arg = 0;
4543 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); 4681 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
4544 char *code; 4682 char *code;
4545 4683
4546 size = instrsize(i); 4684 arg = i->i_oparg;
4547 if (i->i_hasarg) { 4685 size = instrsize(arg);
4548 arg = i->i_oparg;
4549 ext = arg >> 16;
4550 }
4551 if (i->i_lineno && !assemble_lnotab(a, i)) 4686 if (i->i_lineno && !assemble_lnotab(a, i))
4552 return 0; 4687 return 0;
4553 if (a->a_offset + size >= len) { 4688 if (a->a_offset + size >= len) {
4554 if (len > PY_SSIZE_T_MAX / 2) 4689 if (len > PY_SSIZE_T_MAX / 2)
4555 return 0; 4690 return 0;
4556 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) 4691 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
4557 return 0; 4692 return 0;
4558 } 4693 }
4559 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; 4694 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
4560 a->a_offset += size; 4695 a->a_offset += size;
4561 if (size == 6) { 4696 write_op_arg((unsigned char*)code, i->i_opcode, arg, size);
4562 assert(i->i_hasarg);
4563 *code++ = (char)EXTENDED_ARG;
4564 *code++ = ext & 0xff;
4565 *code++ = ext >> 8;
4566 arg &= 0xffff;
4567 }
4568 *code++ = i->i_opcode;
4569 if (i->i_hasarg) {
4570 assert(size == 3 || size == 6);
4571 *code++ = arg & 0xff;
4572 *code++ = arg >> 8;
4573 }
4574 return 1; 4697 return 1;
4575 } 4698 }
4576 4699
4577 static void 4700 static void
4578 assemble_jump_offsets(struct assembler *a, struct compiler *c) 4701 assemble_jump_offsets(struct assembler *a, struct compiler *c)
4579 { 4702 {
4580 basicblock *b; 4703 basicblock *b;
4581 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count; 4704 int bsize, totsize, extended_arg_recompile;
4582 int i; 4705 int i;
4583 4706
4584 /* Compute the size of each block and fixup jump args. 4707 /* Compute the size of each block and fixup jump args.
4585 Replace block pointer with position in bytecode. */ 4708 Replace block pointer with position in bytecode. */
4586 do { 4709 do {
4587 totsize = 0; 4710 totsize = 0;
4588 for (i = a->a_nblocks - 1; i >= 0; i--) { 4711 for (i = a->a_nblocks - 1; i >= 0; i--) {
4589 b = a->a_postorder[i]; 4712 b = a->a_postorder[i];
4590 bsize = blocksize(b); 4713 bsize = blocksize(b);
4591 b->b_offset = totsize; 4714 b->b_offset = totsize;
4592 totsize += bsize; 4715 totsize += bsize;
4593 } 4716 }
4594 last_extended_arg_count = extended_arg_count; 4717 extended_arg_recompile = 0;
4595 extended_arg_count = 0;
4596 for (b = c->u->u_blocks; b != NULL; b = b->b_list) { 4718 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4597 bsize = b->b_offset; 4719 bsize = b->b_offset;
4598 for (i = 0; i < b->b_iused; i++) { 4720 for (i = 0; i < b->b_iused; i++) {
4599 struct instr *instr = &b->b_instr[i]; 4721 struct instr *instr = &b->b_instr[i];
4722 int isize = instrsize(instr->i_oparg);
4600 /* Relative jumps are computed relative to 4723 /* Relative jumps are computed relative to
4601 the instruction pointer after fetching 4724 the instruction pointer after fetching
4602 the jump instruction. 4725 the jump instruction.
4603 */ 4726 */
4604 bsize += instrsize(instr); 4727 bsize += isize;
4605 if (instr->i_jabs) 4728 if (instr->i_jabs || instr->i_jrel) {
4606 instr->i_oparg = instr->i_target->b_offset; 4729 instr->i_oparg = instr->i_target->b_offset;
4607 else if (instr->i_jrel) { 4730 if (instr->i_jrel) {
4608 int delta = instr->i_target->b_offset - bsize; 4731 instr->i_oparg -= bsize;
4609 instr->i_oparg = delta; 4732 }
4733 if (instrsize(instr->i_oparg) != isize) {
4734 extended_arg_recompile = 1;
4735 }
4610 } 4736 }
4611 else
4612 continue;
4613 if (instr->i_oparg > 0xffff)
4614 extended_arg_count++;
4615 } 4737 }
4616 } 4738 }
4617 4739
4618 /* XXX: This is an awful hack that could hurt performance, but 4740 /* XXX: This is an awful hack that could hurt performance, but
4619 on the bright side it should work until we come up 4741 on the bright side it should work until we come up
4620 with a better solution. 4742 with a better solution.
4621 4743
4622 The issue is that in the first loop blocksize() is called 4744 The issue is that in the first loop blocksize() is called
4623 which calls instrsize() which requires i_oparg be set 4745 which calls instrsize() which requires i_oparg be set
4624 appropriately. There is a bootstrap problem because 4746 appropriately. There is a bootstrap problem because
4625 i_oparg is calculated in the second loop above. 4747 i_oparg is calculated in the second loop above.
4626 4748
4627 So we loop until we stop seeing new EXTENDED_ARGs. 4749 So we loop until we stop seeing new EXTENDED_ARGs.
4628 The only EXTENDED_ARGs that could be popping up are 4750 The only EXTENDED_ARGs that could be popping up are
4629 ones in jump instructions. So this should converge 4751 ones in jump instructions. So this should converge
4630 fairly quickly. 4752 fairly quickly.
4631 */ 4753 */
4632 } while (last_extended_arg_count != extended_arg_count); 4754 } while (extended_arg_recompile);
4633 } 4755 }
4634 4756
4635 static PyObject * 4757 static PyObject *
4636 dict_keys_inorder(PyObject *dict, Py_ssize_t offset) 4758 dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
4637 { 4759 {
4638 PyObject *tuple, *k, *v; 4760 PyObject *tuple, *k, *v;
4639 Py_ssize_t i, pos = 0, size = PyDict_Size(dict); 4761 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
4640 4762
4641 tuple = PyTuple_New(size); 4763 tuple = PyTuple_New(size);
4642 if (tuple == NULL) 4764 if (tuple == NULL)
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
4768 /* For debugging purposes only */ 4890 /* For debugging purposes only */
4769 #if 0 4891 #if 0
4770 static void 4892 static void
4771 dump_instr(const struct instr *i) 4893 dump_instr(const struct instr *i)
4772 { 4894 {
4773 const char *jrel = i->i_jrel ? "jrel " : ""; 4895 const char *jrel = i->i_jrel ? "jrel " : "";
4774 const char *jabs = i->i_jabs ? "jabs " : ""; 4896 const char *jabs = i->i_jabs ? "jabs " : "";
4775 char arg[128]; 4897 char arg[128];
4776 4898
4777 *arg = '\0'; 4899 *arg = '\0';
4778 if (i->i_hasarg) 4900 if (HAS_ARG(i->i_opcode)) {
4779 sprintf(arg, "arg: %d ", i->i_oparg); 4901 sprintf(arg, "arg: %d ", i->i_oparg);
4780 4902 }
4781 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", 4903 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4782 i->i_lineno, i->i_opcode, arg, jabs, jrel); 4904 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4783 } 4905 }
4784 4906
4785 static void 4907 static void
4786 dump_basicblock(const basicblock *b) 4908 dump_basicblock(const basicblock *b)
4787 { 4909 {
4788 const char *seen = b->b_seen ? "seen " : ""; 4910 const char *seen = b->b_seen ? "seen " : "";
4789 const char *b_return = b->b_return ? "return " : ""; 4911 const char *b_return = b->b_return ? "return " : "";
4790 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", 4912 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
4858 return co; 4980 return co;
4859 } 4981 }
4860 4982
4861 #undef PyAST_Compile 4983 #undef PyAST_Compile
4862 PyAPI_FUNC(PyCodeObject *) 4984 PyAPI_FUNC(PyCodeObject *)
4863 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4985 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4864 PyArena *arena) 4986 PyArena *arena)
4865 { 4987 {
4866 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4988 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4867 } 4989 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+