Skip to content

Commit f1f655e

Browse files
committed
turn cfg_builder_addop_i into a macro
1 parent 8d9a06f commit f1f655e

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

Python/compile.c

+27-29
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,6 @@ typedef struct {
433433

434434
static int basicblock_next_instr(basicblock *);
435435

436-
static int cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, struct location loc);
437-
438436
static void compiler_free(struct compiler *);
439437
static int compiler_error(struct compiler *, const char *, ...);
440438
static int compiler_warn(struct compiler *, const char *, ...);
@@ -1269,6 +1267,17 @@ basicblock_addop(basicblock *b, int opcode, int oparg,
12691267
(!HAS_TARGET(opcode) && target == NO_TARGET));
12701268
assert(!(oparg != NO_OPARG && target != NO_TARGET));
12711269

1270+
if (oparg != NO_OPARG) {
1271+
/* oparg value is unsigned, but a signed C int is usually used to store
1272+
it in the C code (like Python/ceval.c).
1273+
1274+
Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1275+
1276+
The argument of a concrete bytecode instruction is limited to 8-bit.
1277+
EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1278+
1279+
oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1280+
}
12721281
int off = basicblock_next_instr(b);
12731282
if (off < 0) {
12741283
return 0;
@@ -1299,7 +1308,11 @@ cfg_builder_addop(cfg_builder *g, int opcode, int oparg, basicblock *target,
12991308

13001309
/* Add an instruction with no arg. Returns 0 on failure, 1 on success. */
13011310
#define CFG_BUILDER_ADDOP_NOARG(G, OP, LOC) \
1302-
cfg_builder_addop(G, OP, NO_OPARG, NO_TARGET, LOC)
1311+
cfg_builder_addop(G, OP, NO_OPARG, NO_TARGET, LOC)
1312+
1313+
/* Add an instruction with an integer arg. Returns 0 on failure, 1 on success. */
1314+
#define CFG_BUILDER_ADDOP_I(G, OP, ARG, LOC) \
1315+
cfg_builder_addop(G, OP, ARG, NO_TARGET, LOC)
13031316

13041317
/* Add a jump instruction. Returns 0 on faiure, 1 on success. */
13051318
#define CFG_BUILDER_ADDOP_J(G, OP, T, LOC) \
@@ -1462,7 +1475,7 @@ compiler_addop_load_const(struct compiler *c, PyObject *o)
14621475
Py_ssize_t arg = compiler_add_const(c, o);
14631476
if (arg < 0)
14641477
return 0;
1465-
return cfg_builder_addop_i(CFG_BUILDER(c), LOAD_CONST, arg, COMPILER_LOC(c));
1478+
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), LOAD_CONST, arg, COMPILER_LOC(c));
14661479
}
14671480

14681481
static int
@@ -1472,7 +1485,8 @@ compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
14721485
Py_ssize_t arg = dict_add_o(dict, o);
14731486
if (arg < 0)
14741487
return 0;
1475-
return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
1488+
assert(HAS_ARG(opcode));
1489+
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
14761490
}
14771491

14781492
static int
@@ -1496,28 +1510,9 @@ compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
14961510
arg <<= 1;
14971511
arg |= 1;
14981512
}
1499-
return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
1513+
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
15001514
}
15011515

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);
1518-
}
1519-
1520-
15211516
#define ADDOP(C, OP) { \
15221517
if (!CFG_BUILDER_ADDOP_NOARG(CFG_BUILDER(C), (OP), COMPILER_LOC(C))) \
15231518
return 0; \
@@ -1568,12 +1563,14 @@ cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, struct locatio
15681563
}
15691564

15701565
#define ADDOP_I(C, OP, O) { \
1571-
if (!cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), COMPILER_LOC(C))) \
1566+
assert(HAS_ARG(OP)); \
1567+
if (!CFG_BUILDER_ADDOP_I(CFG_BUILDER(C), (OP), (O), COMPILER_LOC(C))) \
15721568
return 0; \
15731569
}
15741570

15751571
#define ADDOP_I_NOLINE(C, OP, O) { \
1576-
if (!cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), NO_LOCATION)) \
1572+
assert(HAS_ARG(OP)); \
1573+
if (!CFG_BUILDER_ADDOP_I(CFG_BUILDER(C), (OP), (O), NO_LOCATION)) \
15771574
return 0; \
15781575
}
15791576

@@ -4236,7 +4233,8 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
42364233
if (op == LOAD_GLOBAL) {
42374234
arg <<= 1;
42384235
}
4239-
return cfg_builder_addop_i(CFG_BUILDER(c), op, arg, COMPILER_LOC(c));
4236+
assert(HAS_ARG(op));
4237+
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), op, arg, COMPILER_LOC(c));
42404238
}
42414239

42424240
static int
@@ -6713,7 +6711,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
67136711
pc->fail_pop = NULL;
67146712
pc->fail_pop_size = 0;
67156713
pc->on_top = 0;
6716-
if (!cfg_builder_addop_i(CFG_BUILDER(c), COPY, 1, COMPILER_LOC(c)) ||
6714+
if (!CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), COPY, 1, COMPILER_LOC(c)) ||
67176715
!compiler_pattern(c, alt, pc)) {
67186716
goto error;
67196717
}

0 commit comments

Comments
 (0)