Visitar URL original
gh-87092: refactor assemble() to a number of separate functions, which do not need the compiler struct by iritkatriel · Pull Request #102562 · python/cpython · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
_PyCompile_CodeGen doesn't need to construct a cfg
  • Loading branch information
iritkatriel committed Mar 9, 2023
commit db4b2b8feebb18b98d72b1cadeb298f462a9d677
43 changes: 33 additions & 10 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -9854,6 +9854,38 @@ instructions_to_cfg(PyObject *instructions, cfg_builder *g)
return ERROR;
}

static PyObject *
instr_sequence_to_instructions(instr_sequence *seq) {
PyObject *instructions = PyList_New(0);
if (instructions == NULL) {
return NULL;
}
for (int i = 0; i < seq->s_used; i++) {
instruction *instr = &seq->s_instrs[i];
location loc = instr->i_loc;
int arg = HAS_TARGET(instr->i_opcode) ?
seq->s_labelmap[instr->i_oparg] : instr->i_oparg;

PyObject *inst_tuple = Py_BuildValue(
"(iiiiii)", instr->i_opcode, arg,
loc.lineno, loc.end_lineno,
loc.col_offset, loc.end_col_offset);
if (inst_tuple == NULL) {
goto error;
}

int res = PyList_Append(instructions, inst_tuple);
Py_DECREF(inst_tuple);
if (res != 0) {
goto error;
}
}
return instructions;
error:
Py_XDECREF(instructions);
return NULL;
}

static PyObject *
cfg_to_instructions(cfg_builder *g)
{
Expand Down Expand Up @@ -9927,19 +9959,10 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
goto finally;
}

cfg_builder g;
if (instr_sequence_to_cfg(INSTR_SEQUENCE(c), &g) < 0) {
goto finally;
}
if (translate_jump_labels_to_targets(g.g_entryblock) < 0) {
goto finally;
}

res = cfg_to_instructions(&g);
res = instr_sequence_to_instructions(INSTR_SEQUENCE(c));

finally:
compiler_exit_scope(c);
cfg_builder_fini(&g);
compiler_free(c);
_PyArena_Free(arena);
return res;
Expand Down