Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ac12e39

Browse files
authored
gh-87092: move all localsplus preparation into separate function called from assembler stage (GH-99869)
1 parent 417206a commit ac12e39

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

Python/compile.c

+41-30
Original file line numberDiff line numberDiff line change
@@ -8730,6 +8730,41 @@ remove_redundant_jumps(cfg_builder *g) {
87308730
return 0;
87318731
}
87328732

8733+
static int
8734+
prepare_localsplus(struct compiler* c, int code_flags)
8735+
{
8736+
assert(PyDict_GET_SIZE(c->u->u_varnames) < INT_MAX);
8737+
assert(PyDict_GET_SIZE(c->u->u_cellvars) < INT_MAX);
8738+
assert(PyDict_GET_SIZE(c->u->u_freevars) < INT_MAX);
8739+
int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
8740+
int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
8741+
int nfreevars = (int)PyDict_GET_SIZE(c->u->u_freevars);
8742+
assert(INT_MAX - nlocals - ncellvars > 0);
8743+
assert(INT_MAX - nlocals - ncellvars - nfreevars > 0);
8744+
int nlocalsplus = nlocals + ncellvars + nfreevars;
8745+
int* cellfixedoffsets = build_cellfixedoffsets(c);
8746+
if (cellfixedoffsets == NULL) {
8747+
return -1;
8748+
}
8749+
8750+
cfg_builder* g = CFG_BUILDER(c);
8751+
8752+
// This must be called before fix_cell_offsets().
8753+
if (insert_prefix_instructions(c, g->g_entryblock, cellfixedoffsets, nfreevars, code_flags)) {
8754+
PyMem_Free(cellfixedoffsets);
8755+
return -1;
8756+
}
8757+
8758+
int numdropped = fix_cell_offsets(c, g->g_entryblock, cellfixedoffsets);
8759+
PyMem_Free(cellfixedoffsets); // At this point we're done with it.
8760+
cellfixedoffsets = NULL;
8761+
if (numdropped < 0) {
8762+
return -1;
8763+
}
8764+
nlocalsplus -= numdropped;
8765+
return nlocalsplus;
8766+
}
8767+
87338768
static PyCodeObject *
87348769
assemble(struct compiler *c, int addNone)
87358770
{
@@ -8751,20 +8786,6 @@ assemble(struct compiler *c, int addNone)
87518786
ADDOP(c, NO_LOCATION, RETURN_VALUE);
87528787
}
87538788

8754-
assert(PyDict_GET_SIZE(c->u->u_varnames) < INT_MAX);
8755-
assert(PyDict_GET_SIZE(c->u->u_cellvars) < INT_MAX);
8756-
assert(PyDict_GET_SIZE(c->u->u_freevars) < INT_MAX);
8757-
int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
8758-
int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
8759-
int nfreevars = (int)PyDict_GET_SIZE(c->u->u_freevars);
8760-
assert(INT_MAX - nlocals - ncellvars > 0);
8761-
assert(INT_MAX - nlocals - ncellvars - nfreevars > 0);
8762-
int nlocalsplus = nlocals + ncellvars + nfreevars;
8763-
int *cellfixedoffsets = build_cellfixedoffsets(c);
8764-
if (cellfixedoffsets == NULL) {
8765-
goto error;
8766-
}
8767-
87688789
int nblocks = 0;
87698790
for (basicblock *b = CFG_BUILDER(c)->g_block_list; b != NULL; b = b->b_list) {
87708791
nblocks++;
@@ -8787,19 +8808,6 @@ assemble(struct compiler *c, int addNone)
87878808
}
87888809
}
87898810

8790-
// This must be called before fix_cell_offsets().
8791-
if (insert_prefix_instructions(c, g->g_entryblock, cellfixedoffsets, nfreevars, code_flags)) {
8792-
goto error;
8793-
}
8794-
8795-
int numdropped = fix_cell_offsets(c, g->g_entryblock, cellfixedoffsets);
8796-
PyMem_Free(cellfixedoffsets); // At this point we're done with it.
8797-
cellfixedoffsets = NULL;
8798-
if (numdropped < 0) {
8799-
goto error;
8800-
}
8801-
nlocalsplus -= numdropped;
8802-
88038811
/** Preprocessing **/
88048812
/* Map labels to targets and mark exception handlers */
88058813
if (translate_jump_labels_to_targets(g->g_entryblock)) {
@@ -8839,6 +8847,12 @@ assemble(struct compiler *c, int addNone)
88398847
}
88408848

88418849
/** Assembly **/
8850+
8851+
int nlocalsplus = prepare_localsplus(c, code_flags);
8852+
if (nlocalsplus < 0) {
8853+
goto error;
8854+
}
8855+
88428856
int maxdepth = stackdepth(g->g_entryblock, code_flags);
88438857
if (maxdepth < 0) {
88448858
goto error;
@@ -8904,9 +8918,6 @@ assemble(struct compiler *c, int addNone)
89048918
error:
89058919
Py_XDECREF(consts);
89068920
assemble_free(&a);
8907-
if (cellfixedoffsets != NULL) {
8908-
PyMem_Free(cellfixedoffsets);
8909-
}
89108921
return co;
89118922
}
89128923

0 commit comments

Comments
 (0)