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

Delta Between Two Patch Sets: Python/compile.c

Issue 23722: During metaclass.__init__, super() of the constructed class does not work
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: Created 3 years 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
« Python/bltinmodule.c ('K') | « Python/bltinmodule.c ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 969 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 return 1; 980 return 1;
981 case LOAD_NAME: 981 case LOAD_NAME:
982 return 1; 982 return 1;
983 case BUILD_TUPLE: 983 case BUILD_TUPLE:
984 case BUILD_LIST: 984 case BUILD_LIST:
985 case BUILD_SET: 985 case BUILD_SET:
986 case BUILD_STRING: 986 case BUILD_STRING:
987 return 1-oparg; 987 return 1-oparg;
988 case BUILD_LIST_UNPACK: 988 case BUILD_LIST_UNPACK:
989 case BUILD_TUPLE_UNPACK: 989 case BUILD_TUPLE_UNPACK:
990 case BUILD_TUPLE_UNPACK_WITH_CALL:
990 case BUILD_SET_UNPACK: 991 case BUILD_SET_UNPACK:
991 case BUILD_MAP_UNPACK: 992 case BUILD_MAP_UNPACK:
992 return 1 - oparg;
993 case BUILD_MAP_UNPACK_WITH_CALL: 993 case BUILD_MAP_UNPACK_WITH_CALL:
994 return 1 - oparg; 994 return 1 - oparg;
995 case BUILD_MAP: 995 case BUILD_MAP:
996 return 1 - 2*oparg; 996 return 1 - 2*oparg;
997 case BUILD_CONST_KEY_MAP: 997 case BUILD_CONST_KEY_MAP:
998 return -oparg; 998 return -oparg;
999 case LOAD_ATTR: 999 case LOAD_ATTR:
1000 return 0; 1000 return 0;
1001 case COMPARE_OP: 1001 case COMPARE_OP:
1002 return -1; 1002 return -1;
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
1515 /* The test for LOCAL must come before the test for FREE in order to 1515 /* The test for LOCAL must come before the test for FREE in order to
1516 handle classes where name is both local and free. The local var is 1516 handle classes where name is both local and free. The local var is
1517 a method and the free var is a free var referenced within a method. 1517 a method and the free var is a free var referenced within a method.
1518 */ 1518 */
1519 1519
1520 static int 1520 static int
1521 get_ref_type(struct compiler *c, PyObject *name) 1521 get_ref_type(struct compiler *c, PyObject *name)
1522 { 1522 {
1523 int scope; 1523 int scope;
1524 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && 1524 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1525 !PyUnicode_CompareWithASCIIString(name, "__class__")) 1525 _PyUnicode_EqualToASCIIString(name, "__class__"))
1526 return CELL; 1526 return CELL;
1527 scope = PyST_GetScope(c->u->u_ste, name); 1527 scope = PyST_GetScope(c->u->u_ste, name);
1528 if (scope == 0) { 1528 if (scope == 0) {
1529 char buf[350]; 1529 char buf[350];
1530 PyOS_snprintf(buf, sizeof(buf), 1530 PyOS_snprintf(buf, sizeof(buf),
1531 "unknown scope for %.100s in %.100s(%s)\n" 1531 "unknown scope for %.100s in %.100s(%s)\n"
1532 "symbols: %s\nlocals: %s\nglobals: %s", 1532 "symbols: %s\nlocals: %s\nglobals: %s",
1533 PyUnicode_AsUTF8(name), 1533 PyUnicode_AsUTF8(name),
1534 PyUnicode_AsUTF8(c->u->u_name), 1534 PyUnicode_AsUTF8(c->u->u_name),
1535 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), 1535 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
1960 Py_XDECREF(str); 1960 Py_XDECREF(str);
1961 compiler_exit_scope(c); 1961 compiler_exit_scope(c);
1962 return 0; 1962 return 0;
1963 } 1963 }
1964 Py_DECREF(str); 1964 Py_DECREF(str);
1965 /* compile the body proper */ 1965 /* compile the body proper */
1966 if (!compiler_body(c, s->v.ClassDef.body)) { 1966 if (!compiler_body(c, s->v.ClassDef.body)) {
1967 compiler_exit_scope(c); 1967 compiler_exit_scope(c);
1968 return 0; 1968 return 0;
1969 } 1969 }
1970 /* Return __classcell__ if it is referenced, otherwise return None */
1970 if (c->u->u_ste->ste_needs_class_closure) { 1971 if (c->u->u_ste->ste_needs_class_closure) {
1971 /* store __classcell__ into class namespace */ 1972 /* Store __classcell__ into class namespace & return it */
1972 str = PyUnicode_InternFromString("__class__"); 1973 str = PyUnicode_InternFromString("__class__");
1973 if (str == NULL) { 1974 if (str == NULL) {
1974 compiler_exit_scope(c); 1975 compiler_exit_scope(c);
1975 return 0; 1976 return 0;
1976 } 1977 }
1977 i = compiler_lookup_arg(c->u->u_cellvars, str); 1978 i = compiler_lookup_arg(c->u->u_cellvars, str);
1978 Py_DECREF(str); 1979 Py_DECREF(str);
1979 if (i < 0) { 1980 if (i < 0) {
1980 compiler_exit_scope(c); 1981 compiler_exit_scope(c);
1981 return 0; 1982 return 0;
1982 } 1983 }
1983 assert(i == 0); 1984 assert(i == 0);
1984 1985
1985 ADDOP_I(c, LOAD_CLOSURE, i); 1986 ADDOP_I(c, LOAD_CLOSURE, i);
1987 ADDOP(c, DUP_TOP);
1986 str = PyUnicode_InternFromString("__classcell__"); 1988 str = PyUnicode_InternFromString("__classcell__");
1987 if (!str || !compiler_nameop(c, str, Store)) { 1989 if (!str || !compiler_nameop(c, str, Store)) {
1988 Py_XDECREF(str); 1990 Py_XDECREF(str);
1989 compiler_exit_scope(c); 1991 compiler_exit_scope(c);
1990 return 0; 1992 return 0;
1991 } 1993 }
1992 Py_DECREF(str); 1994 Py_DECREF(str);
1993 } 1995 }
1994 else { 1996 else {
1995 /* This happens when nobody references the cell. */ 1997 /* No methods referenced __class__, so just return None */
1996 assert(PyDict_Size(c->u->u_cellvars) == 0); 1998 assert(PyDict_Size(c->u->u_cellvars) == 0);
1997 } 1999 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2000 }
2001 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1998 /* create the code object */ 2002 /* create the code object */
1999 co = assemble(c, 1); 2003 co = assemble(c, 1);
2000 } 2004 }
2001 /* leave the new scope */ 2005 /* leave the new scope */
2002 compiler_exit_scope(c); 2006 compiler_exit_scope(c);
2003 if (co == NULL) 2007 if (co == NULL)
2004 return 0; 2008 return 0;
2005 2009
2006 /* 2. load the 'build_class' function */ 2010 /* 2. load the 'build_class' function */
2007 ADDOP(c, LOAD_BUILD_CLASS); 2011 ADDOP(c, LOAD_BUILD_CLASS);
(...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after
2681 } 2685 }
2682 2686
2683 /* build up the names */ 2687 /* build up the names */
2684 for (i = 0; i < n; i++) { 2688 for (i = 0; i < n; i++) {
2685 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); 2689 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2686 Py_INCREF(alias->name); 2690 Py_INCREF(alias->name);
2687 PyTuple_SET_ITEM(names, i, alias->name); 2691 PyTuple_SET_ITEM(names, i, alias->name);
2688 } 2692 }
2689 2693
2690 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && 2694 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2691 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) { 2695 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
2692 Py_DECREF(level); 2696 Py_DECREF(level);
2693 Py_DECREF(names); 2697 Py_DECREF(names);
2694 return compiler_error(c, "from __future__ imports must occur " 2698 return compiler_error(c, "from __future__ imports must occur "
2695 "at the beginning of the file"); 2699 "at the beginning of the file");
2696 } 2700 }
2697 2701
2698 ADDOP_O(c, LOAD_CONST, level, consts); 2702 ADDOP_O(c, LOAD_CONST, level, consts);
2699 Py_DECREF(level); 2703 Py_DECREF(level);
2700 ADDOP_O(c, LOAD_CONST, names, consts); 2704 ADDOP_O(c, LOAD_CONST, names, consts);
2701 Py_DECREF(names); 2705 Py_DECREF(names);
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
3020 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; 3024 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
3021 3025
3022 PyObject *dict = c->u->u_names; 3026 PyObject *dict = c->u->u_names;
3023 PyObject *mangled; 3027 PyObject *mangled;
3024 /* XXX AugStore isn't used anywhere! */ 3028 /* XXX AugStore isn't used anywhere! */
3025 3029
3026 mangled = _Py_Mangle(c->u->u_private, name); 3030 mangled = _Py_Mangle(c->u->u_private, name);
3027 if (!mangled) 3031 if (!mangled)
3028 return 0; 3032 return 0;
3029 3033
3030 assert(PyUnicode_CompareWithASCIIString(name, "None") && 3034 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3031 PyUnicode_CompareWithASCIIString(name, "True") && 3035 !_PyUnicode_EqualToASCIIString(name, "True") &&
3032 PyUnicode_CompareWithASCIIString(name, "False")); 3036 !_PyUnicode_EqualToASCIIString(name, "False"));
3033 3037
3034 op = 0; 3038 op = 0;
3035 optype = OP_NAME; 3039 optype = OP_NAME;
3036 scope = PyST_GetScope(c->u->u_ste, mangled); 3040 scope = PyST_GetScope(c->u->u_ste, mangled);
3037 switch (scope) { 3041 switch (scope) {
3038 case FREE: 3042 case FREE:
3039 dict = c->u->u_freevars; 3043 dict = c->u->u_freevars;
3040 optype = OP_DEREF; 3044 optype = OP_DEREF;
3041 break; 3045 break;
3042 case CELL: 3046 case CELL:
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
3496 } 3500 }
3497 3501
3498 /* shared code between compiler_call and compiler_class */ 3502 /* shared code between compiler_call and compiler_class */
3499 static int 3503 static int
3500 compiler_call_helper(struct compiler *c, 3504 compiler_call_helper(struct compiler *c,
3501 int n, /* Args already pushed */ 3505 int n, /* Args already pushed */
3502 asdl_seq *args, 3506 asdl_seq *args,
3503 asdl_seq *keywords) 3507 asdl_seq *keywords)
3504 { 3508 {
3505 Py_ssize_t i, nseen, nelts, nkwelts; 3509 Py_ssize_t i, nseen, nelts, nkwelts;
3506 int musttupleunpack = 0, mustdictunpack = 0; 3510 int mustdictunpack = 0;
3507 3511
3508 /* the number of tuples and dictionaries on the stack */ 3512 /* the number of tuples and dictionaries on the stack */
3509 Py_ssize_t nsubargs = 0, nsubkwargs = 0; 3513 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3510 3514
3511 nelts = asdl_seq_LEN(args); 3515 nelts = asdl_seq_LEN(args);
3512 nkwelts = asdl_seq_LEN(keywords); 3516 nkwelts = asdl_seq_LEN(keywords);
3513 3517
3514 for (i = 0; i < nkwelts; i++) { 3518 for (i = 0; i < nkwelts; i++) {
3515 keyword_ty kw = asdl_seq_GET(keywords, i); 3519 keyword_ty kw = asdl_seq_GET(keywords, i);
3516 if (kw->arg == NULL) { 3520 if (kw->arg == NULL) {
3517 mustdictunpack = 1; 3521 mustdictunpack = 1;
3518 break; 3522 break;
3519 } 3523 }
3520 } 3524 }
3521 3525
3522 nseen = n; /* the number of positional arguments on the stack */ 3526 nseen = n; /* the number of positional arguments on the stack */
3523 for (i = 0; i < nelts; i++) { 3527 for (i = 0; i < nelts; i++) {
3524 expr_ty elt = asdl_seq_GET(args, i); 3528 expr_ty elt = asdl_seq_GET(args, i);
3525 if (elt->kind == Starred_kind) { 3529 if (elt->kind == Starred_kind) {
3526 /* A star-arg. If we've seen positional arguments, 3530 /* A star-arg. If we've seen positional arguments,
3527 pack the positional arguments into a tuple. */ 3531 pack the positional arguments into a tuple. */
3528 if (nseen) { 3532 if (nseen) {
3529 ADDOP_I(c, BUILD_TUPLE, nseen); 3533 ADDOP_I(c, BUILD_TUPLE, nseen);
3530 nseen = 0; 3534 nseen = 0;
3531 nsubargs++; 3535 nsubargs++;
3532 } 3536 }
3533 VISIT(c, expr, elt->v.Starred.value); 3537 VISIT(c, expr, elt->v.Starred.value);
3534 nsubargs++; 3538 nsubargs++;
3535 musttupleunpack = 1;
3536 } 3539 }
3537 else { 3540 else {
3538 VISIT(c, expr, elt); 3541 VISIT(c, expr, elt);
3539 nseen++; 3542 nseen++;
3540 } 3543 }
3541 } 3544 }
3542 3545
3543 /* Same dance again for keyword arguments */ 3546 /* Same dance again for keyword arguments */
3544 if (musttupleunpack || mustdictunpack) { 3547 if (nsubargs || mustdictunpack) {
3545 if (nseen) { 3548 if (nseen) {
3546 /* Pack up any trailing positional arguments. */ 3549 /* Pack up any trailing positional arguments. */
3547 ADDOP_I(c, BUILD_TUPLE, nseen); 3550 ADDOP_I(c, BUILD_TUPLE, nseen);
3548 nsubargs++; 3551 nsubargs++;
3549 } 3552 }
3550 if (musttupleunpack || nsubargs > 1) { 3553 if (nsubargs > 1) {
3551 /* If we ended up with more than one stararg, we need 3554 /* If we ended up with more than one stararg, we need
3552 to concatenate them into a single sequence. */ 3555 to concatenate them into a single sequence. */
3553 ADDOP_I(c, BUILD_TUPLE_UNPACK, nsubargs); 3556 ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
3554 } 3557 }
3555 else if (nsubargs == 0) { 3558 else if (nsubargs == 0) {
3556 ADDOP_I(c, BUILD_TUPLE, 0); 3559 ADDOP_I(c, BUILD_TUPLE, 0);
3557 } 3560 }
3558 nseen = 0; /* the number of keyword arguments on the stack following */ 3561 nseen = 0; /* the number of keyword arguments on the stack following */
3559 for (i = 0; i < nkwelts; i++) { 3562 for (i = 0; i < nkwelts; i++) {
3560 keyword_ty kw = asdl_seq_GET(keywords, i); 3563 keyword_ty kw = asdl_seq_GET(keywords, i);
3561 if (kw->arg == NULL) { 3564 if (kw->arg == NULL) {
3562 /* A keyword argument unpacking. */ 3565 /* A keyword argument unpacking. */
3563 if (nseen) { 3566 if (nseen) {
3564 if (!compiler_subkwargs(c, keywords, i - nseen, i)) 3567 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3565 return 0; 3568 return 0;
3566 nsubkwargs++; 3569 nsubkwargs++;
3567 nseen = 0; 3570 nseen = 0;
3568 } 3571 }
3569 VISIT(c, expr, kw->value); 3572 VISIT(c, expr, kw->value);
3570 nsubkwargs++; 3573 nsubkwargs++;
3571 } 3574 }
3572 else { 3575 else {
3573 nseen++; 3576 nseen++;
3574 } 3577 }
3575 } 3578 }
3576 if (nseen) { 3579 if (nseen) {
3577 /* Pack up any trailing keyword arguments. */ 3580 /* Pack up any trailing keyword arguments. */
3578 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) 3581 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
3579 return 0; 3582 return 0;
3580 nsubkwargs++; 3583 nsubkwargs++;
3581 } 3584 }
3582 if (mustdictunpack || nsubkwargs > 1) { 3585 if (nsubkwargs > 1) {
3583 /* Pack it all up */ 3586 /* Pack it all up */
3584 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs); 3587 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
3585 } 3588 }
3586 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0); 3589 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
3587 return 1; 3590 return 1;
3588 } 3591 }
3589 else if (nkwelts) { 3592 else if (nkwelts) {
3590 PyObject *names; 3593 PyObject *names;
3591 VISIT_SEQ(c, keyword, keywords); 3594 VISIT_SEQ(c, keyword, keywords);
3592 names = PyTuple_New(nkwelts); 3595 names = PyTuple_New(nkwelts);
(...skipping 962 matching lines...) Expand 10 before | Expand all | Expand 10 after
4555 "invalid subscript kind %d", sl->kind); 4558 "invalid subscript kind %d", sl->kind);
4556 return 0; 4559 return 0;
4557 } 4560 }
4558 return 1; 4561 return 1;
4559 } 4562 }
4560 4563
4561 static int 4564 static int
4562 compiler_annassign(struct compiler *c, stmt_ty s) 4565 compiler_annassign(struct compiler *c, stmt_ty s)
4563 { 4566 {
4564 expr_ty targ = s->v.AnnAssign.target; 4567 expr_ty targ = s->v.AnnAssign.target;
4568 PyObject* mangled;
4565 4569
4566 assert(s->kind == AnnAssign_kind); 4570 assert(s->kind == AnnAssign_kind);
4567 4571
4568 /* We perform the actual assignment first. */ 4572 /* We perform the actual assignment first. */
4569 if (s->v.AnnAssign.value) { 4573 if (s->v.AnnAssign.value) {
4570 VISIT(c, expr, s->v.AnnAssign.value); 4574 VISIT(c, expr, s->v.AnnAssign.value);
4571 VISIT(c, expr, targ); 4575 VISIT(c, expr, targ);
4572 } 4576 }
4573 switch (targ->kind) { 4577 switch (targ->kind) {
4574 case Name_kind: 4578 case Name_kind:
4575 /* If we have a simple name in a module or class, store annotation. */ 4579 /* If we have a simple name in a module or class, store annotation. */
4576 if (s->v.AnnAssign.simple && 4580 if (s->v.AnnAssign.simple &&
4577 (c->u->u_scope_type == COMPILER_SCOPE_MODULE || 4581 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4578 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { 4582 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
4583 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
4584 if (!mangled) {
4585 return 0;
4586 }
4579 VISIT(c, expr, s->v.AnnAssign.annotation); 4587 VISIT(c, expr, s->v.AnnAssign.annotation);
4580 ADDOP_O(c, STORE_ANNOTATION, targ->v.Name.id, names) 4588 /* ADDOP_N decrefs its argument */
4589 ADDOP_N(c, STORE_ANNOTATION, mangled, names);
4581 } 4590 }
4582 break; 4591 break;
4583 case Attribute_kind: 4592 case Attribute_kind:
4584 if (!s->v.AnnAssign.value && 4593 if (!s->v.AnnAssign.value &&
4585 !check_ann_expr(c, targ->v.Attribute.value)) { 4594 !check_ann_expr(c, targ->v.Attribute.value)) {
4586 return 0; 4595 return 0;
4587 } 4596 }
4588 break; 4597 break;
4589 case Subscript_kind: 4598 case Subscript_kind:
4590 if (!s->v.AnnAssign.value && 4599 if (!s->v.AnnAssign.value &&
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
4941 the instruction's bytecode offset and line number. See 4950 the instruction's bytecode offset and line number. See
4942 Objects/lnotab_notes.txt for the description of the line number table. */ 4951 Objects/lnotab_notes.txt for the description of the line number table. */
4943 4952
4944 static int 4953 static int
4945 assemble_lnotab(struct assembler *a, struct instr *i) 4954 assemble_lnotab(struct assembler *a, struct instr *i)
4946 { 4955 {
4947 int d_bytecode, d_lineno; 4956 int d_bytecode, d_lineno;
4948 Py_ssize_t len; 4957 Py_ssize_t len;
4949 unsigned char *lnotab; 4958 unsigned char *lnotab;
4950 4959
4951 d_bytecode = a->a_offset - a->a_lineno_off; 4960 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
4952 d_lineno = i->i_lineno - a->a_lineno; 4961 d_lineno = i->i_lineno - a->a_lineno;
4953 4962
4954 assert(d_bytecode >= 0); 4963 assert(d_bytecode >= 0);
4955 4964
4956 if(d_bytecode == 0 && d_lineno == 0) 4965 if(d_bytecode == 0 && d_lineno == 0)
4957 return 1; 4966 return 1;
4958 4967
4959 if (d_bytecode > 255) { 4968 if (d_bytecode > 255) {
4960 int j, nbytes, ncodes = d_bytecode / 255; 4969 int j, nbytes, ncodes = d_bytecode / 255;
4961 nbytes = a->a_lnotab_off + 2 * ncodes; 4970 nbytes = a->a_lnotab_off + 2 * ncodes;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
5048 /* assemble_emit() 5057 /* assemble_emit()
5049 Extend the bytecode with a new instruction. 5058 Extend the bytecode with a new instruction.
5050 Update lnotab if necessary. 5059 Update lnotab if necessary.
5051 */ 5060 */
5052 5061
5053 static int 5062 static int
5054 assemble_emit(struct assembler *a, struct instr *i) 5063 assemble_emit(struct assembler *a, struct instr *i)
5055 { 5064 {
5056 int size, arg = 0; 5065 int size, arg = 0;
5057 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); 5066 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
5058 char *code; 5067 _Py_CODEUNIT *code;
5059 5068
5060 arg = i->i_oparg; 5069 arg = i->i_oparg;
5061 size = instrsize(arg); 5070 size = instrsize(arg);
5062 if (i->i_lineno && !assemble_lnotab(a, i)) 5071 if (i->i_lineno && !assemble_lnotab(a, i))
5063 return 0; 5072 return 0;
5064 if (a->a_offset + size >= len) { 5073 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
5065 if (len > PY_SSIZE_T_MAX / 2) 5074 if (len > PY_SSIZE_T_MAX / 2)
5066 return 0; 5075 return 0;
5067 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) 5076 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5068 return 0; 5077 return 0;
5069 } 5078 }
5070 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; 5079 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
5071 a->a_offset += size; 5080 a->a_offset += size;
5072 write_op_arg((unsigned char*)code, i->i_opcode, arg, size); 5081 write_op_arg(code, i->i_opcode, arg, size);
5073 return 1; 5082 return 1;
5074 } 5083 }
5075 5084
5076 static void 5085 static void
5077 assemble_jump_offsets(struct assembler *a, struct compiler *c) 5086 assemble_jump_offsets(struct assembler *a, struct compiler *c)
5078 { 5087 {
5079 basicblock *b; 5088 basicblock *b;
5080 int bsize, totsize, extended_arg_recompile; 5089 int bsize, totsize, extended_arg_recompile;
5081 int i; 5090 int i;
5082 5091
(...skipping 16 matching lines...) Expand all
5099 /* Relative jumps are computed relative to 5108 /* Relative jumps are computed relative to
5100 the instruction pointer after fetching 5109 the instruction pointer after fetching
5101 the jump instruction. 5110 the jump instruction.
5102 */ 5111 */
5103 bsize += isize; 5112 bsize += isize;
5104 if (instr->i_jabs || instr->i_jrel) { 5113 if (instr->i_jabs || instr->i_jrel) {
5105 instr->i_oparg = instr->i_target->b_offset; 5114 instr->i_oparg = instr->i_target->b_offset;
5106 if (instr->i_jrel) { 5115 if (instr->i_jrel) {
5107 instr->i_oparg -= bsize; 5116 instr->i_oparg -= bsize;
5108 } 5117 }
5118 instr->i_oparg *= sizeof(_Py_CODEUNIT);
5109 if (instrsize(instr->i_oparg) != isize) { 5119 if (instrsize(instr->i_oparg) != isize) {
5110 extended_arg_recompile = 1; 5120 extended_arg_recompile = 1;
5111 } 5121 }
5112 } 5122 }
5113 } 5123 }
5114 } 5124 }
5115 5125
5116 /* XXX: This is an awful hack that could hurt performance, but 5126 /* XXX: This is an awful hack that could hurt performance, but
5117 on the bright side it should work until we come up 5127 on the bright side it should work until we come up
5118 with a better solution. 5128 with a better solution.
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
5344 /* Emit code in reverse postorder from dfs. */ 5354 /* Emit code in reverse postorder from dfs. */
5345 for (i = a.a_nblocks - 1; i >= 0; i--) { 5355 for (i = a.a_nblocks - 1; i >= 0; i--) {
5346 b = a.a_postorder[i]; 5356 b = a.a_postorder[i];
5347 for (j = 0; j < b->b_iused; j++) 5357 for (j = 0; j < b->b_iused; j++)
5348 if (!assemble_emit(&a, &b->b_instr[j])) 5358 if (!assemble_emit(&a, &b->b_instr[j]))
5349 goto error; 5359 goto error;
5350 } 5360 }
5351 5361
5352 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) 5362 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
5353 goto error; 5363 goto error;
5354 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0) 5364 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
5355 goto error; 5365 goto error;
5356 5366
5357 co = makecode(c, &a); 5367 co = makecode(c, &a);
5358 error: 5368 error:
5359 assemble_free(&a); 5369 assemble_free(&a);
5360 return co; 5370 return co;
5361 } 5371 }
5362 5372
5363 #undef PyAST_Compile 5373 #undef PyAST_Compile
5364 PyAPI_FUNC(PyCodeObject *) 5374 PyAPI_FUNC(PyCodeObject *)
5365 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 5375 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
5366 PyArena *arena) 5376 PyArena *arena)
5367 { 5377 {
5368 return PyAST_CompileEx(mod, filename, flags, -1, arena); 5378 return PyAST_CompileEx(mod, filename, flags, -1, arena);
5369 } 5379 }
LEFTRIGHT

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