Skip to content

Commit af31e92

Browse files
committed
add struct location to reduce boilerplate
1 parent 996be64 commit af31e92

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

Python/compile.c

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,25 @@ basicblock_next_instr(basicblock *b)
966966
(new).i_end_lineno = (old).i_end_lineno; \
967967
(new).i_end_col_offset = (old).i_end_col_offset;
968968

969+
970+
struct location {
971+
int lineno;
972+
int end_lineno;
973+
int col_offset;
974+
int end_col_offset;
975+
};
976+
977+
#define NO_LOCATION ((struct location){-1, 0, 0, 0})
978+
979+
/* current compiler unit's location */
980+
#define CU_LOCATION(CU) \
981+
((struct location){ \
982+
(CU)->u_lineno, \
983+
(CU)->u_end_lineno, \
984+
(CU)->u_col_offset, \
985+
(CU)->u_end_col_offset, \
986+
})
987+
969988
/* Return the stack effect of opcode with argument oparg.
970989
971990
Some opcodes have different stack effect when jump to the target and
@@ -1268,8 +1287,7 @@ compiler_use_new_implicit_block_if_needed(struct compiler *c)
12681287
*/
12691288

12701289
static int
1271-
basicblock_addop(basicblock *b, int opcode, int lineno,
1272-
int end_lineno, int col_offset, int end_col_offset)
1290+
basicblock_addop(basicblock *b, int opcode, struct location loc)
12731291
{
12741292
assert(IS_WITHIN_OPCODE_RANGE(opcode));
12751293
assert(!IS_ASSEMBLER_OPCODE(opcode));
@@ -1282,10 +1300,10 @@ basicblock_addop(basicblock *b, int opcode, int lineno,
12821300
struct instr *i = &b->b_instr[off];
12831301
i->i_opcode = opcode;
12841302
i->i_oparg = 0;
1285-
i->i_lineno = lineno;
1286-
i->i_end_lineno = end_lineno;
1287-
i->i_col_offset = col_offset;
1288-
i->i_end_col_offset = end_col_offset;
1303+
i->i_lineno = loc.lineno;
1304+
i->i_end_lineno = loc.end_lineno;
1305+
i->i_col_offset = loc.col_offset;
1306+
i->i_end_col_offset = loc.end_col_offset;
12891307

12901308
return 1;
12911309
}
@@ -1296,13 +1314,9 @@ compiler_addop(struct compiler *c, int opcode, bool line)
12961314
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
12971315
return -1;
12981316
}
1299-
int lineno = line ? c->u->u_lineno : -1;
1300-
int end_lineno = line ? c->u->u_end_lineno : 0;
1301-
int col_offset = line ? c->u->u_col_offset : 0;
1302-
int end_col_offset = line ? c->u->u_end_col_offset : 0;
13031317

1304-
return basicblock_addop(c->u->u_curblock, opcode,
1305-
lineno, end_lineno, col_offset, end_col_offset);
1318+
struct location loc = line ? CU_LOCATION(c->u) : NO_LOCATION;
1319+
return basicblock_addop(c->u->u_curblock, opcode, loc);
13061320
}
13071321

13081322
static Py_ssize_t
@@ -1496,8 +1510,7 @@ compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
14961510

14971511
static int
14981512
basicblock_addop_i(basicblock *b, int opcode, Py_ssize_t oparg,
1499-
int lineno, int end_lineno,
1500-
int col_offset, int end_col_offset)
1513+
struct location loc)
15011514
{
15021515
/* oparg value is unsigned, but a signed C int is usually used to store
15031516
it in the C code (like Python/ceval.c).
@@ -1518,10 +1531,10 @@ basicblock_addop_i(basicblock *b, int opcode, Py_ssize_t oparg,
15181531
struct instr *i = &b->b_instr[off];
15191532
i->i_opcode = opcode;
15201533
i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1521-
i->i_lineno = lineno;
1522-
i->i_end_lineno = end_lineno;
1523-
i->i_col_offset = col_offset;
1524-
i->i_end_col_offset = end_col_offset;
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;
15251538

15261539
return 1;
15271540
}
@@ -1532,20 +1545,13 @@ compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg, bool line)
15321545
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
15331546
return -1;
15341547
}
1535-
int lineno = line ? c->u->u_lineno : -1;
1536-
int end_lineno = line ? c->u->u_end_lineno : 0;
1537-
int col_offset = line ? c->u->u_col_offset : 0;
1538-
int end_col_offset = line ? c->u->u_end_col_offset : 0;
1539-
1540-
return basicblock_addop_i(c->u->u_curblock, opcode, oparg,
1541-
lineno, end_lineno, col_offset, end_col_offset);
1548+
struct location loc = line ? CU_LOCATION(c->u) : NO_LOCATION;
1549+
return basicblock_addop_i(c->u->u_curblock, opcode, oparg, loc);
15421550
}
15431551

15441552
static int
15451553
basicblock_add_jump(basicblock *b, int opcode,
1546-
int lineno, int end_lineno,
1547-
int col_offset, int end_col_offset,
1548-
basicblock *target)
1554+
struct location loc, basicblock *target)
15491555
{
15501556
assert(IS_WITHIN_OPCODE_RANGE(opcode));
15511557
assert(!IS_ASSEMBLER_OPCODE(opcode));
@@ -1559,10 +1565,10 @@ basicblock_add_jump(basicblock *b, int opcode,
15591565
}
15601566
i->i_opcode = opcode;
15611567
i->i_target = target;
1562-
i->i_lineno = lineno;
1563-
i->i_end_lineno = end_lineno;
1564-
i->i_col_offset = col_offset;
1565-
i->i_end_col_offset = end_col_offset;
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;
15661572

15671573
return 1;
15681574
}
@@ -1573,14 +1579,8 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *target, bool line)
15731579
if (compiler_use_new_implicit_block_if_needed(c) < 0) {
15741580
return -1;
15751581
}
1576-
int lineno = line ? c->u->u_lineno : -1;
1577-
int end_lineno = line ? c->u->u_end_lineno : 0;
1578-
int col_offset = line ? c->u->u_col_offset : 0;
1579-
int end_col_offset = line ? c->u->u_end_col_offset : 0;
1580-
1581-
return basicblock_add_jump(c->u->u_curblock, opcode,
1582-
lineno, end_lineno, col_offset, end_col_offset,
1583-
target);
1582+
struct location loc = line ? CU_LOCATION(c->u) : NO_LOCATION;
1583+
return basicblock_add_jump(c->u->u_curblock, opcode, loc, target);
15841584
}
15851585

15861586
#define ADDOP(C, OP) { \
@@ -7455,7 +7455,7 @@ push_cold_blocks_to_end(struct compiler *c, basicblock *entry, int code_flags) {
74557455
if (explicit_jump == NULL) {
74567456
return -1;
74577457
}
7458-
basicblock_add_jump(explicit_jump, JUMP, -1, 0, 0, 0, b->b_next);
7458+
basicblock_add_jump(explicit_jump, JUMP, NO_LOCATION, b->b_next);
74597459

74607460
explicit_jump->b_cold = 1;
74617461
explicit_jump->b_next = b->b_next;

0 commit comments

Comments
 (0)