Skip to content

Commit f8a0767

Browse files
committed
merge basicblock_addop, basicblock_addop_i and basicblock_add_jump into one functions to reduce unnecessary repetition
1 parent af31e92 commit f8a0767

File tree

1 file changed

+28
-59
lines changed

1 file changed

+28
-59
lines changed

Python/compile.c

+28-59
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@
105105
(IS_VIRTUAL_JUMP_OPCODE(opcode) || \
106106
is_bit_set_in_table(_PyOpcode_Jump, opcode))
107107

108+
#define IS_BLOCK_PUSH_OPCODE(opcode) \
109+
((opcode) == SETUP_FINALLY || \
110+
(opcode) == SETUP_WITH || \
111+
(opcode) == SETUP_CLEANUP)
112+
108113
/* opcodes which are not emitted in codegen stage, only by the assembler */
109114
#define IS_ASSEMBLER_OPCODE(opcode) \
110115
((opcode) == JUMP_FORWARD || \
@@ -191,7 +196,7 @@ static inline int
191196
is_block_push(struct instr *instr)
192197
{
193198
int opcode = instr->i_opcode;
194-
return opcode == SETUP_FINALLY || opcode == SETUP_WITH || opcode == SETUP_CLEANUP;
199+
return IS_BLOCK_PUSH_OPCODE(opcode);
195200
}
196201

197202
static inline int
@@ -1287,19 +1292,26 @@ compiler_use_new_implicit_block_if_needed(struct compiler *c)
12871292
*/
12881293

12891294
static int
1290-
basicblock_addop(basicblock *b, int opcode, struct location loc)
1295+
basicblock_addop(basicblock *b, int opcode, int oparg,
1296+
basicblock *target, struct location loc)
12911297
{
12921298
assert(IS_WITHIN_OPCODE_RANGE(opcode));
12931299
assert(!IS_ASSEMBLER_OPCODE(opcode));
1294-
assert(!HAS_ARG(opcode) || IS_ARTIFICIAL(opcode));
1300+
assert(HAS_ARG(opcode) || oparg == 0);
1301+
assert(0 <= oparg && oparg <= 2147483647);
1302+
assert((target == NULL) ||
1303+
IS_JUMP_OPCODE(opcode) ||
1304+
IS_BLOCK_PUSH_OPCODE(opcode));
1305+
assert(oparg == 0 || target == NULL);
12951306

12961307
int off = basicblock_next_instr(b);
12971308
if (off < 0) {
12981309
return 0;
12991310
}
13001311
struct instr *i = &b->b_instr[off];
13011312
i->i_opcode = opcode;
1302-
i->i_oparg = 0;
1313+
i->i_oparg = oparg;
1314+
i->i_target = target;
13031315
i->i_lineno = loc.lineno;
13041316
i->i_end_lineno = loc.end_lineno;
13051317
i->i_col_offset = loc.col_offset;
@@ -1311,12 +1323,13 @@ basicblock_addop(basicblock *b, int opcode, struct location loc)
13111323
static int
13121324
compiler_addop(struct compiler *c, int opcode, bool line)
13131325
{
1326+
assert(!HAS_ARG(opcode) || IS_ARTIFICIAL(opcode));
13141327
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
13151328
return -1;
13161329
}
13171330

13181331
struct location loc = line ? CU_LOCATION(c->u) : NO_LOCATION;
1319-
return basicblock_addop(c->u->u_curblock, opcode, loc);
1332+
return basicblock_addop(c->u->u_curblock, opcode, 0, NULL, loc);
13201333
}
13211334

13221335
static Py_ssize_t
@@ -1507,11 +1520,12 @@ compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
15071520
/* Add an opcode with an integer argument.
15081521
Returns 0 on failure, 1 on success.
15091522
*/
1510-
15111523
static int
1512-
basicblock_addop_i(basicblock *b, int opcode, Py_ssize_t oparg,
1513-
struct location loc)
1524+
compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg, bool line)
15141525
{
1526+
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
1527+
return -1;
1528+
}
15151529
/* oparg value is unsigned, but a signed C int is usually used to store
15161530
it in the C code (like Python/ceval.c).
15171531
@@ -1520,57 +1534,10 @@ basicblock_addop_i(basicblock *b, int opcode, Py_ssize_t oparg,
15201534
The argument of a concrete bytecode instruction is limited to 8-bit.
15211535
EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
15221536

1523-
assert(IS_WITHIN_OPCODE_RANGE(opcode));
1524-
assert(!IS_ASSEMBLER_OPCODE(opcode));
1525-
assert(0 <= oparg && oparg <= 2147483647);
1526-
1527-
int off = basicblock_next_instr(b);
1528-
if (off < 0) {
1529-
return 0;
1530-
}
1531-
struct instr *i = &b->b_instr[off];
1532-
i->i_opcode = opcode;
1533-
i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1534-
i->i_lineno = loc.lineno;
1535-
i->i_end_lineno = loc.end_lineno;
1536-
i->i_col_offset = loc.col_offset;
1537-
i->i_end_col_offset = loc.end_col_offset;
1537+
int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
15381538

1539-
return 1;
1540-
}
1541-
1542-
static int
1543-
compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg, bool line)
1544-
{
1545-
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
1546-
return -1;
1547-
}
15481539
struct location loc = line ? CU_LOCATION(c->u) : NO_LOCATION;
1549-
return basicblock_addop_i(c->u->u_curblock, opcode, oparg, loc);
1550-
}
1551-
1552-
static int
1553-
basicblock_add_jump(basicblock *b, int opcode,
1554-
struct location loc, basicblock *target)
1555-
{
1556-
assert(IS_WITHIN_OPCODE_RANGE(opcode));
1557-
assert(!IS_ASSEMBLER_OPCODE(opcode));
1558-
assert(HAS_ARG(opcode) || IS_VIRTUAL_OPCODE(opcode));
1559-
assert(target != NULL);
1560-
1561-
int off = basicblock_next_instr(b);
1562-
struct instr *i = &b->b_instr[off];
1563-
if (off < 0) {
1564-
return 0;
1565-
}
1566-
i->i_opcode = opcode;
1567-
i->i_target = target;
1568-
i->i_lineno = loc.lineno;
1569-
i->i_end_lineno = loc.end_lineno;
1570-
i->i_col_offset = loc.col_offset;
1571-
i->i_end_col_offset = loc.end_col_offset;
1572-
1573-
return 1;
1540+
return basicblock_addop(c->u->u_curblock, opcode, oparg_, NULL, loc);
15741541
}
15751542

15761543
static int
@@ -1580,7 +1547,9 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *target, bool line)
15801547
return -1;
15811548
}
15821549
struct location loc = line ? CU_LOCATION(c->u) : NO_LOCATION;
1583-
return basicblock_add_jump(c->u->u_curblock, opcode, loc, target);
1550+
assert(target != NULL);
1551+
assert(IS_JUMP_OPCODE(opcode) || IS_BLOCK_PUSH_OPCODE(opcode));
1552+
return basicblock_addop(c->u->u_curblock, opcode, 0, target, loc);
15841553
}
15851554

15861555
#define ADDOP(C, OP) { \
@@ -7455,7 +7424,7 @@ push_cold_blocks_to_end(struct compiler *c, basicblock *entry, int code_flags) {
74557424
if (explicit_jump == NULL) {
74567425
return -1;
74577426
}
7458-
basicblock_add_jump(explicit_jump, JUMP, NO_LOCATION, b->b_next);
7427+
basicblock_addop(explicit_jump, JUMP, 0, b->b_next, NO_LOCATION);
74597428

74607429
explicit_jump->b_cold = 1;
74617430
explicit_jump->b_next = b->b_next;

0 commit comments

Comments
 (0)