Skip to content

Commit

Permalink
turn cfg_builder_addop_i into a macro
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Jul 28, 2022
1 parent 8d9a06f commit f1f655e
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,6 @@ typedef struct {

static int basicblock_next_instr(basicblock *);

static int cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, struct location loc);

static void compiler_free(struct compiler *);
static int compiler_error(struct compiler *, const char *, ...);
static int compiler_warn(struct compiler *, const char *, ...);
Expand Down Expand Up @@ -1269,6 +1267,17 @@ basicblock_addop(basicblock *b, int opcode, int oparg,
(!HAS_TARGET(opcode) && target == NO_TARGET));
assert(!(oparg != NO_OPARG && target != NO_TARGET));

if (oparg != NO_OPARG) {
/* oparg value is unsigned, but a signed C int is usually used to store
it in the C code (like Python/ceval.c).
Limit to 32-bit signed C int (rather than INT_MAX) for portability.
The argument of a concrete bytecode instruction is limited to 8-bit.
EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */

oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
}
int off = basicblock_next_instr(b);
if (off < 0) {
return 0;
Expand Down Expand Up @@ -1299,7 +1308,11 @@ cfg_builder_addop(cfg_builder *g, int opcode, int oparg, basicblock *target,

/* Add an instruction with no arg. Returns 0 on failure, 1 on success. */
#define CFG_BUILDER_ADDOP_NOARG(G, OP, LOC) \
cfg_builder_addop(G, OP, NO_OPARG, NO_TARGET, LOC)
cfg_builder_addop(G, OP, NO_OPARG, NO_TARGET, LOC)

/* Add an instruction with an integer arg. Returns 0 on failure, 1 on success. */
#define CFG_BUILDER_ADDOP_I(G, OP, ARG, LOC) \
cfg_builder_addop(G, OP, ARG, NO_TARGET, LOC)

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

static int
Expand All @@ -1472,7 +1485,8 @@ compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Py_ssize_t arg = dict_add_o(dict, o);
if (arg < 0)
return 0;
return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
assert(HAS_ARG(opcode));
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
}

static int
Expand All @@ -1496,28 +1510,9 @@ compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
arg <<= 1;
arg |= 1;
}
return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), opcode, arg, COMPILER_LOC(c));
}

/* Add an opcode with an integer argument.
Returns 0 on failure, 1 on success.
*/
static int
cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, struct location loc)
{
/* oparg value is unsigned, but a signed C int is usually used to store
it in the C code (like Python/ceval.c).
Limit to 32-bit signed C int (rather than INT_MAX) for portability.
The argument of a concrete bytecode instruction is limited to 8-bit.
EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */

int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
return cfg_builder_addop(g, opcode, oparg_, NULL, loc);
}


#define ADDOP(C, OP) { \
if (!CFG_BUILDER_ADDOP_NOARG(CFG_BUILDER(C), (OP), COMPILER_LOC(C))) \
return 0; \
Expand Down Expand Up @@ -1568,12 +1563,14 @@ cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, struct locatio
}

#define ADDOP_I(C, OP, O) { \
if (!cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), COMPILER_LOC(C))) \
assert(HAS_ARG(OP)); \
if (!CFG_BUILDER_ADDOP_I(CFG_BUILDER(C), (OP), (O), COMPILER_LOC(C))) \
return 0; \
}

#define ADDOP_I_NOLINE(C, OP, O) { \
if (!cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), NO_LOCATION)) \
assert(HAS_ARG(OP)); \
if (!CFG_BUILDER_ADDOP_I(CFG_BUILDER(C), (OP), (O), NO_LOCATION)) \
return 0; \
}

Expand Down Expand Up @@ -4236,7 +4233,8 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
if (op == LOAD_GLOBAL) {
arg <<= 1;
}
return cfg_builder_addop_i(CFG_BUILDER(c), op, arg, COMPILER_LOC(c));
assert(HAS_ARG(op));
return CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), op, arg, COMPILER_LOC(c));
}

static int
Expand Down Expand Up @@ -6713,7 +6711,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
pc->fail_pop = NULL;
pc->fail_pop_size = 0;
pc->on_top = 0;
if (!cfg_builder_addop_i(CFG_BUILDER(c), COPY, 1, COMPILER_LOC(c)) ||
if (!CFG_BUILDER_ADDOP_I(CFG_BUILDER(c), COPY, 1, COMPILER_LOC(c)) ||
!compiler_pattern(c, alt, pc)) {
goto error;
}
Expand Down

0 comments on commit f1f655e

Please sign in to comment.