Skip to content

Commit d347ba7

Browse files
committed
turn cfg_builder_addop_i into a macro
1 parent 0fc800b commit d347ba7

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

Python/compile.c

+20-29
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ struct location {
134134
int end_col_offset;
135135
};
136136

137-
138137
#define LOCATION(LNO, END_LNO, COL, END_COL) \
139138
((const struct location){(LNO), (END_LNO), (COL), (END_COL)})
140139

@@ -433,8 +432,6 @@ typedef struct {
433432

434433
static int basicblock_next_instr(basicblock *);
435434

436-
static int cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, struct location loc);
437-
438435
static void compiler_free(struct compiler *);
439436
static int compiler_error(struct compiler *, const char *, ...);
440437
static int compiler_warn(struct compiler *, const char *, ...);
@@ -1299,7 +1296,19 @@ cfg_builder_addop(cfg_builder *g, int opcode, int oparg, basicblock *target,
12991296

13001297
/* Add an instruction with no arg. Returns 0 on failure, 1 on success. */
13011298
#define CFG_BUILDER_ADDOP_NOARG(G, OP, LOC) \
1302-
cfg_builder_addop(G, OP, NO_OPARG, NO_TARGET, LOC)
1299+
cfg_builder_addop(G, OP, NO_OPARG, NO_TARGET, LOC)
1300+
1301+
/* Add an instructon with integer arg. Returns 0 on failure and 1 on success.
1302+
* oparg value is unsigned, but a signed C int is usually used to store
1303+
* it in the C code (like Python/ceval.c).
1304+
*
1305+
* Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1306+
*
1307+
* The argument of a concrete bytecode instruction is limited to 8-bit.
1308+
* EXTENDED_ARG is used for 16, 24, and 32-bit arguments.
1309+
*/
1310+
#define CFG_BUILDER_ADDOP_I(G, OP, ARG, LOC) \
1311+
cfg_builder_addop(G, OP, Py_SAFE_DOWNCAST(ARG, Py_ssize_t, int), NULL, LOC)
13031312

13041313
/* Add a jump instruction. Returns 0 on faiure, 1 on success. */
13051314
#define CFG_BUILDER_ADDOP_J(G, OP, T, LOC) \
@@ -1462,7 +1471,7 @@ compiler_addop_load_const(struct compiler *c, PyObject *o)
14621471
Py_ssize_t arg = compiler_add_const(c, o);
14631472
if (arg < 0)
14641473
return 0;
1465-
return cfg_builder_addop_i(CFG_BUILDER(c), LOAD_CONST, arg, COMPILER_LOC(c));
1474+
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), LOAD_CONST, arg, COMPILER_LOC(c));
14661475
}
14671476

14681477
static int
@@ -1472,7 +1481,7 @@ compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
14721481
Py_ssize_t arg = dict_add_o(dict, o);
14731482
if (arg < 0)
14741483
return 0;
1475-
return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
1484+
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
14761485
}
14771486

14781487
static int
@@ -1496,25 +1505,7 @@ compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
14961505
arg <<= 1;
14971506
arg |= 1;
14981507
}
1499-
return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
1500-
}
1501-
1502-
/* Add an opcode with an integer argument.
1503-
Returns 0 on failure, 1 on success.
1504-
*/
1505-
static int
1506-
cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, struct location loc)
1507-
{
1508-
/* oparg value is unsigned, but a signed C int is usually used to store
1509-
it in the C code (like Python/ceval.c).
1510-
1511-
Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1512-
1513-
The argument of a concrete bytecode instruction is limited to 8-bit.
1514-
EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1515-
1516-
int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1517-
return cfg_builder_addop(g, opcode, oparg_, NULL, loc);
1508+
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
15181509
}
15191510

15201511

@@ -1568,12 +1559,12 @@ cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, struct locatio
15681559
}
15691560

15701561
#define ADDOP_I(C, OP, O) { \
1571-
if (!cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), COMPILER_LOC(C))) \
1562+
if (!CFG_BUILDER_ADDOP_I(CFG_BUILDER(C), (OP), (O), COMPILER_LOC(C))) \
15721563
return 0; \
15731564
}
15741565

15751566
#define ADDOP_I_NOLINE(C, OP, O) { \
1576-
if (!cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), NO_LOCATION)) \
1567+
if (!CFG_BUILDER_ADDOP_I(CFG_BUILDER(C), (OP), (O), NO_LOCATION)) \
15771568
return 0; \
15781569
}
15791570

@@ -4236,7 +4227,7 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
42364227
if (op == LOAD_GLOBAL) {
42374228
arg <<= 1;
42384229
}
4239-
return cfg_builder_addop_i(CFG_BUILDER(c), op, arg, COMPILER_LOC(c));
4230+
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), op, arg, COMPILER_LOC(c));
42404231
}
42414232

42424233
static int
@@ -6713,7 +6704,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
67136704
pc->fail_pop = NULL;
67146705
pc->fail_pop_size = 0;
67156706
pc->on_top = 0;
6716-
if (!cfg_builder_addop_i(CFG_BUILDER(c), COPY, 1, COMPILER_LOC(c)) ||
6707+
if (!CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), COPY, 1, COMPILER_LOC(c)) ||
67176708
!compiler_pattern(c, alt, pc)) {
67186709
goto error;
67196710
}

0 commit comments

Comments
 (0)