Skip to content

Commit a280869

Browse files
committed
remove max_label arg
1 parent e57b158 commit a280869

File tree

5 files changed

+30
-32
lines changed

5 files changed

+30
-32
lines changed

Include/internal/pycore_compile.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ extern int _PyAST_Optimize(
4242
/* Access compiler internals for unit testing */
4343
PyAPI_FUNC(PyObject *) _PyCompile_OptimizeCfg(
4444
PyObject *instructions,
45-
PyObject *consts,
46-
int max_label);
45+
PyObject *consts);
4746

4847

4948
#ifdef __cplusplus

Lib/test/support/bytecode_helper.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __repr__(self):
5151
class InstructionBuilder:
5252
def __init__(self, insts):
5353
self.instructions = []
54-
self.max_label = 0
54+
self.next_label = 0
5555
self.labels_map = {}
5656
self._populate(insts)
5757

@@ -62,8 +62,8 @@ def get_label(self, lbl):
6262
return self.labels_map[lbl]
6363

6464
def _newlabel(self):
65-
self.max_label += 1
66-
return self.max_label
65+
self.next_label += 1
66+
return self.next_label
6767

6868
def _populate(self, insts):
6969
next_label = 0
@@ -100,7 +100,7 @@ class CfgOptimizationTestCase(unittest.TestCase):
100100
def get_optimized(self, insts, consts):
101101
bld = InstructionBuilder(insts)
102102
opt_insts, opt_consts = _testinternalcapi.optimize_cfg(
103-
bld.instructions, consts, bld.max_label)
103+
bld.instructions, consts)
104104

105105
res = []
106106
for inst in opt_insts:

Modules/_testinternalcapi.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -538,17 +538,16 @@ _testinternalcapi.optimize_cfg -> object
538538
539539
instructions: object
540540
consts: object
541-
max_label: int
542541
543542
Apply optimizations to instructions.
544543
[clinic start generated code]*/
545544

546545
static PyObject *
547546
_testinternalcapi_optimize_cfg_impl(PyObject *module, PyObject *instructions,
548-
PyObject *consts, int max_label)
549-
/*[clinic end generated code: output=655b9bca5750037d input=7a47589ae5c4cb84]*/
547+
PyObject *consts)
548+
/*[clinic end generated code: output=5412aeafca683c8b input=6dd279570766d1bd]*/
550549
{
551-
return _PyCompile_OptimizeCfg(instructions, consts, max_label);
550+
return _PyCompile_OptimizeCfg(instructions, consts);
552551
}
553552

554553

Modules/clinic/_testinternalcapi.c.h

+7-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compile.c

+15-10
Original file line numberDiff line numberDiff line change
@@ -9614,31 +9614,36 @@ unpack_instruction(PyObject *instr, int *label, int *opcode, int *oparg,
96149614
}
96159615

96169616
static basicblock *
9617-
instructions_to_cfg(PyObject *instructions, int max_label)
9617+
instructions_to_cfg(PyObject *instructions)
96189618
{
96199619
assert(PyList_Check(instructions));
9620-
assert(max_label >= 0);
9621-
basicblock **label2block = (basicblock **)PyMem_Malloc(sizeof(basicblock *) * (max_label + 1));
9620+
9621+
Py_ssize_t num_insts = PyList_GET_SIZE(instructions);
9622+
Py_ssize_t max_labels = num_insts;
9623+
basicblock **label2block = (basicblock **)PyMem_Malloc(sizeof(basicblock *) * max_labels);
96229624
if (!label2block) {
96239625
PyErr_NoMemory();
96249626
}
9625-
memset(label2block, 0, sizeof(basicblock *) * (max_label + 1));
9627+
memset(label2block, 0, sizeof(basicblock *) * max_labels);
96269628

96279629
basicblock *entryblock = new_basicblock();
96289630
if (entryblock == NULL) {
96299631
goto error;
96309632
}
96319633
basicblock *current_block = entryblock;
96329634

9633-
Py_ssize_t n = PyList_GET_SIZE(instructions);
96349635
bool start_new_block = false;
9635-
for (Py_ssize_t i = 0; i < n; i++) {
9636+
for (Py_ssize_t i = 0; i < num_insts; i++) {
96369637
PyObject *instr_tuple = PyList_GET_ITEM(instructions, i);
96379638
int label, opcode, oparg, target;
96389639
struct location loc;
96399640
if (unpack_instruction(instr_tuple, &label, &opcode, &oparg, &target, &loc) != 0) {
96409641
goto error;
96419642
}
9643+
if (label >= max_labels || target >= max_labels) {
9644+
PyErr_SetString(PyExc_ValueError, "label is out of range");
9645+
return NULL;
9646+
}
96429647
basicblock *new_block = NULL;
96439648
if (label > 0 && label2block[label] != NULL) {
96449649
new_block = label2block[label];
@@ -9653,7 +9658,7 @@ instructions_to_cfg(PyObject *instructions, int max_label)
96539658
goto error;
96549659
}
96559660
if (label > 0) {
9656-
assert(label <= max_label);
9661+
assert(label < max_labels);
96579662
label2block[label] = new_block;
96589663
}
96599664
}
@@ -9676,7 +9681,7 @@ instructions_to_cfg(PyObject *instructions, int max_label)
96769681
if (!basicblock_addop(current_block, opcode, oparg, target_block, &loc)) {
96779682
goto error;
96789683
}
9679-
if (IS_END_OF_BASICBLOCK_OPCODE(opcode)) {
9684+
if (IS_TERMINATOR_OPCODE(opcode)) {
96809685
start_new_block = true;
96819686
}
96829687
}
@@ -9731,13 +9736,13 @@ cfg_to_instructions(basicblock* entryblock)
97319736
}
97329737

97339738
PyObject *
9734-
_PyCompile_OptimizeCfg(PyObject *instructions, PyObject *consts, int max_label)
9739+
_PyCompile_OptimizeCfg(PyObject *instructions, PyObject *consts)
97359740
{
97369741
PyObject *const_cache = PyDict_New();
97379742
if (const_cache == NULL) {
97389743
return NULL;
97399744
}
9740-
basicblock *entryblock = instructions_to_cfg(instructions, max_label);
9745+
basicblock *entryblock = instructions_to_cfg(instructions);
97419746
if (optimize_cfg(entryblock, consts, const_cache)) {
97429747
goto error;
97439748
}

0 commit comments

Comments
 (0)