Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2f2dc7

Browse files
authored
gh-121404: rename compiler_addop* to codegen_addop*, and replace direct usages by the macros (#123043)
1 parent a07cf4c commit e2f2dc7

File tree

1 file changed

+102
-96
lines changed

1 file changed

+102
-96
lines changed

Python/compile.c

Lines changed: 102 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@
6767
return ERROR; \
6868
}
6969

70+
#define RETURN_IF_ERROR_IN_SCOPE(C, CALL) { \
71+
if ((CALL) < 0) { \
72+
compiler_exit_scope((C)); \
73+
return ERROR; \
74+
} \
75+
}
76+
7077
#define IS_TOP_LEVEL_AWAIT(C) ( \
7178
((C)->c_flags.cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
7279
&& ((C)->u->u_ste->ste_type == ModuleBlock))
@@ -291,8 +298,6 @@ typedef struct {
291298
Py_ssize_t on_top;
292299
} pattern_context;
293300

294-
static int codegen_addop_i(instr_sequence *seq, int opcode, Py_ssize_t oparg, location loc);
295-
296301
static void compiler_free(struct compiler *);
297302
static int compiler_error(struct compiler *, location loc, const char *, ...);
298303
static int compiler_warn(struct compiler *, location loc, const char *, ...);
@@ -685,6 +690,29 @@ compiler_set_qualname(struct compiler *c)
685690
return SUCCESS;
686691
}
687692

693+
/* Add an opcode with an integer argument */
694+
static int
695+
codegen_addop_i(instr_sequence *seq, int opcode, Py_ssize_t oparg, location loc)
696+
{
697+
/* oparg value is unsigned, but a signed C int is usually used to store
698+
it in the C code (like Python/ceval.c).
699+
700+
Limit to 32-bit signed C int (rather than INT_MAX) for portability.
701+
702+
The argument of a concrete bytecode instruction is limited to 8-bit.
703+
EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
704+
705+
int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
706+
assert(!IS_ASSEMBLER_OPCODE(opcode));
707+
return _PyInstructionSequence_Addop(seq, opcode, oparg_, loc);
708+
}
709+
710+
#define ADDOP_I(C, LOC, OP, O) \
711+
RETURN_IF_ERROR(codegen_addop_i(INSTR_SEQUENCE(C), (OP), (O), (LOC)))
712+
713+
#define ADDOP_I_IN_SCOPE(C, LOC, OP, O) \
714+
RETURN_IF_ERROR_IN_SCOPE(C, codegen_addop_i(INSTR_SEQUENCE(C), (OP), (O), (LOC)));
715+
688716
static int
689717
codegen_addop_noarg(instr_sequence *seq, int opcode, location loc)
690718
{
@@ -693,6 +721,12 @@ codegen_addop_noarg(instr_sequence *seq, int opcode, location loc)
693721
return _PyInstructionSequence_Addop(seq, opcode, 0, loc);
694722
}
695723

724+
#define ADDOP(C, LOC, OP) \
725+
RETURN_IF_ERROR(codegen_addop_noarg(INSTR_SEQUENCE(C), (OP), (LOC)))
726+
727+
#define ADDOP_IN_SCOPE(C, LOC, OP) \
728+
RETURN_IF_ERROR_IN_SCOPE((C), codegen_addop_noarg(INSTR_SEQUENCE(C), (OP), (LOC)))
729+
696730
static Py_ssize_t
697731
dict_add_o(PyObject *dict, PyObject *o)
698732
{
@@ -854,24 +888,57 @@ compiler_add_const(struct compiler *c, PyObject *o)
854888
}
855889

856890
static int
857-
compiler_addop_load_const(struct compiler *c, location loc, PyObject *o)
891+
codegen_addop_load_const(struct compiler *c, location loc, PyObject *o)
858892
{
859893
Py_ssize_t arg = compiler_add_const(c, o);
860894
if (arg < 0) {
861895
return ERROR;
862896
}
863-
return codegen_addop_i(INSTR_SEQUENCE(c), LOAD_CONST, arg, loc);
897+
ADDOP_I(c, loc, LOAD_CONST, arg);
898+
return SUCCESS;
899+
}
900+
901+
#define ADDOP_LOAD_CONST(C, LOC, O) \
902+
RETURN_IF_ERROR(codegen_addop_load_const((C), (LOC), (O)))
903+
904+
#define ADDOP_LOAD_CONST_IN_SCOPE(C, LOC, O) \
905+
RETURN_IF_ERROR_IN_SCOPE((C), codegen_addop_load_const((C), (LOC), (O)))
906+
907+
/* Same as ADDOP_LOAD_CONST, but steals a reference. */
908+
#define ADDOP_LOAD_CONST_NEW(C, LOC, O) { \
909+
PyObject *__new_const = (O); \
910+
if (__new_const == NULL) { \
911+
return ERROR; \
912+
} \
913+
if (codegen_addop_load_const((C), (LOC), __new_const) < 0) { \
914+
Py_DECREF(__new_const); \
915+
return ERROR; \
916+
} \
917+
Py_DECREF(__new_const); \
864918
}
865919

866920
static int
867-
compiler_addop_o(struct compiler *c, location loc,
868-
int opcode, PyObject *dict, PyObject *o)
921+
codegen_addop_o(struct compiler *c, location loc,
922+
int opcode, PyObject *dict, PyObject *o)
869923
{
870924
Py_ssize_t arg = dict_add_o(dict, o);
871-
if (arg < 0) {
872-
return ERROR;
873-
}
874-
return codegen_addop_i(INSTR_SEQUENCE(c), opcode, arg, loc);
925+
RETURN_IF_ERROR(arg);
926+
ADDOP_I(c, loc, opcode, arg);
927+
return SUCCESS;
928+
}
929+
930+
#define ADDOP_N(C, LOC, OP, O, TYPE) { \
931+
assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
932+
int ret = codegen_addop_o((C), (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)); \
933+
Py_DECREF((O)); \
934+
RETURN_IF_ERROR(ret); \
935+
}
936+
937+
#define ADDOP_N_IN_SCOPE(C, LOC, OP, O, TYPE) { \
938+
assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
939+
int ret = codegen_addop_o((C), (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)); \
940+
Py_DECREF((O)); \
941+
RETURN_IF_ERROR_IN_SCOPE((C), ret); \
875942
}
876943

877944
#define LOAD_METHOD -1
@@ -880,8 +947,8 @@ compiler_addop_o(struct compiler *c, location loc,
880947
#define LOAD_ZERO_SUPER_METHOD -4
881948

882949
static int
883-
compiler_addop_name(struct compiler *c, location loc,
884-
int opcode, PyObject *dict, PyObject *o)
950+
codegen_addop_name(struct compiler *c, location loc,
951+
int opcode, PyObject *dict, PyObject *o)
885952
{
886953
PyObject *mangled = compiler_maybe_mangle(c, o);
887954
if (!mangled) {
@@ -918,25 +985,12 @@ compiler_addop_name(struct compiler *c, location loc,
918985
arg <<= 2;
919986
arg |= 1;
920987
}
921-
return codegen_addop_i(INSTR_SEQUENCE(c), opcode, arg, loc);
988+
ADDOP_I(c, loc, opcode, arg);
989+
return SUCCESS;
922990
}
923991

924-
/* Add an opcode with an integer argument */
925-
static int
926-
codegen_addop_i(instr_sequence *seq, int opcode, Py_ssize_t oparg, location loc)
927-
{
928-
/* oparg value is unsigned, but a signed C int is usually used to store
929-
it in the C code (like Python/ceval.c).
930-
931-
Limit to 32-bit signed C int (rather than INT_MAX) for portability.
932-
933-
The argument of a concrete bytecode instruction is limited to 8-bit.
934-
EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
935-
936-
int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
937-
assert(!IS_ASSEMBLER_OPCODE(opcode));
938-
return _PyInstructionSequence_Addop(seq, opcode, oparg_, loc);
939-
}
992+
#define ADDOP_NAME(C, LOC, OP, O, TYPE) \
993+
RETURN_IF_ERROR(codegen_addop_name((C), (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)))
940994

941995
static int
942996
codegen_addop_j(instr_sequence *seq, location loc,
@@ -948,49 +1002,6 @@ codegen_addop_j(instr_sequence *seq, location loc,
9481002
return _PyInstructionSequence_Addop(seq, opcode, target.id, loc);
9491003
}
9501004

951-
#define RETURN_IF_ERROR_IN_SCOPE(C, CALL) { \
952-
if ((CALL) < 0) { \
953-
compiler_exit_scope((C)); \
954-
return ERROR; \
955-
} \
956-
}
957-
958-
#define ADDOP(C, LOC, OP) \
959-
RETURN_IF_ERROR(codegen_addop_noarg(INSTR_SEQUENCE(C), (OP), (LOC)))
960-
961-
#define ADDOP_IN_SCOPE(C, LOC, OP) RETURN_IF_ERROR_IN_SCOPE((C), codegen_addop_noarg(INSTR_SEQUENCE(C), (OP), (LOC)))
962-
963-
#define ADDOP_LOAD_CONST(C, LOC, O) \
964-
RETURN_IF_ERROR(compiler_addop_load_const((C), (LOC), (O)))
965-
966-
/* Same as ADDOP_LOAD_CONST, but steals a reference. */
967-
#define ADDOP_LOAD_CONST_NEW(C, LOC, O) { \
968-
PyObject *__new_const = (O); \
969-
if (__new_const == NULL) { \
970-
return ERROR; \
971-
} \
972-
if (compiler_addop_load_const((C), (LOC), __new_const) < 0) { \
973-
Py_DECREF(__new_const); \
974-
return ERROR; \
975-
} \
976-
Py_DECREF(__new_const); \
977-
}
978-
979-
#define ADDOP_N(C, LOC, OP, O, TYPE) { \
980-
assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
981-
if (compiler_addop_o((C), (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)) < 0) { \
982-
Py_DECREF((O)); \
983-
return ERROR; \
984-
} \
985-
Py_DECREF((O)); \
986-
}
987-
988-
#define ADDOP_NAME(C, LOC, OP, O, TYPE) \
989-
RETURN_IF_ERROR(compiler_addop_name((C), (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)))
990-
991-
#define ADDOP_I(C, LOC, OP, O) \
992-
RETURN_IF_ERROR(codegen_addop_i(INSTR_SEQUENCE(C), (OP), (O), (LOC)))
993-
9941005
#define ADDOP_JUMP(C, LOC, OP, O) \
9951006
RETURN_IF_ERROR(codegen_addop_j(INSTR_SEQUENCE(C), (LOC), (OP), (O)))
9961007

@@ -2279,7 +2290,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
22792290
Py_DECREF(type_params_name);
22802291
RETURN_IF_ERROR_IN_SCOPE(c, compiler_type_params(c, type_params));
22812292
for (int i = 0; i < num_typeparam_args; i++) {
2282-
RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(INSTR_SEQUENCE(c), LOAD_FAST, i, loc));
2293+
ADDOP_I_IN_SCOPE(c, loc, LOAD_FAST, i);
22832294
}
22842295
}
22852296

@@ -2300,10 +2311,8 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
23002311
}
23012312

23022313
if (is_generic) {
2303-
RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(
2304-
INSTR_SEQUENCE(c), SWAP, 2, loc));
2305-
RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(
2306-
INSTR_SEQUENCE(c), CALL_INTRINSIC_2, INTRINSIC_SET_FUNCTION_TYPE_PARAMS, loc));
2314+
ADDOP_I_IN_SCOPE(c, loc, SWAP, 2);
2315+
ADDOP_I_IN_SCOPE(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_FUNCTION_TYPE_PARAMS);
23072316

23082317
c->u->u_metadata.u_argcount = num_typeparam_args;
23092318
PyCodeObject *co = optimize_and_assemble(c, 0);
@@ -2393,12 +2402,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
23932402
// We can't use compiler_nameop here because we need to generate a
23942403
// STORE_DEREF in a class namespace, and compiler_nameop() won't do
23952404
// that by default.
2396-
PyObject *cellvars = c->u->u_metadata.u_cellvars;
2397-
if (compiler_addop_o(c, loc, STORE_DEREF, cellvars,
2398-
&_Py_ID(__classdict__)) < 0) {
2399-
compiler_exit_scope(c);
2400-
return ERROR;
2401-
}
2405+
ADDOP_N_IN_SCOPE(c, loc, STORE_DEREF, &_Py_ID(__classdict__), cellvars);
24022406
}
24032407
/* compile the body proper */
24042408
if (compiler_body(c, loc, s->v.ClassDef.body) < 0) {
@@ -2527,9 +2531,7 @@ compiler_class(struct compiler *c, stmt_ty s)
25272531
_Py_DECLARE_STR(type_params, ".type_params");
25282532
_Py_DECLARE_STR(generic_base, ".generic_base");
25292533
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(type_params), Load));
2530-
RETURN_IF_ERROR_IN_SCOPE(
2531-
c, codegen_addop_i(INSTR_SEQUENCE(c), CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC, loc)
2532-
)
2534+
ADDOP_I_IN_SCOPE(c, loc, CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC);
25332535
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(generic_base), Store));
25342536

25352537
Py_ssize_t original_len = asdl_seq_LEN(s->v.ClassDef.bases);
@@ -2630,9 +2632,7 @@ compiler_typealias(struct compiler *c, stmt_ty s)
26302632
return ERROR;
26312633
}
26322634
Py_DECREF(type_params_name);
2633-
RETURN_IF_ERROR_IN_SCOPE(
2634-
c, compiler_addop_load_const(c, loc, name)
2635-
);
2635+
ADDOP_LOAD_CONST_IN_SCOPE(c, loc, name);
26362636
RETURN_IF_ERROR_IN_SCOPE(c, compiler_type_params(c, type_params));
26372637
}
26382638
else {
@@ -2713,8 +2713,8 @@ check_compare(struct compiler *c, expr_ty e)
27132713
return SUCCESS;
27142714
}
27152715

2716-
static int compiler_addcompare(struct compiler *c, location loc,
2717-
cmpop_ty op)
2716+
static int
2717+
compiler_addcompare(struct compiler *c, location loc, cmpop_ty op)
27182718
{
27192719
int cmp;
27202720
switch (op) {
@@ -4040,6 +4040,13 @@ addop_yield(struct compiler *c, location loc) {
40404040
return SUCCESS;
40414041
}
40424042

4043+
static int
4044+
compiler_load_classdict_freevar(struct compiler *c, location loc)
4045+
{
4046+
ADDOP_N(c, loc, LOAD_DEREF, &_Py_ID(__classdict__), freevars);
4047+
return SUCCESS;
4048+
}
4049+
40434050
static int
40444051
compiler_nameop(struct compiler *c, location loc,
40454052
identifier name, expr_context_ty ctx)
@@ -4119,8 +4126,7 @@ compiler_nameop(struct compiler *c, location loc,
41194126
else if (SYMTABLE_ENTRY(c)->ste_can_see_class_scope) {
41204127
op = LOAD_FROM_DICT_OR_DEREF;
41214128
// First load the classdict
4122-
if (compiler_addop_o(c, loc, LOAD_DEREF,
4123-
c->u->u_metadata.u_freevars, &_Py_ID(__classdict__)) < 0) {
4129+
if (compiler_load_classdict_freevar(c, loc) < 0) {
41244130
goto error;
41254131
}
41264132
}
@@ -4146,8 +4152,7 @@ compiler_nameop(struct compiler *c, location loc,
41464152
if (SYMTABLE_ENTRY(c)->ste_can_see_class_scope && scope == GLOBAL_IMPLICIT) {
41474153
op = LOAD_FROM_DICT_OR_GLOBALS;
41484154
// First load the classdict
4149-
if (compiler_addop_o(c, loc, LOAD_DEREF,
4150-
c->u->u_metadata.u_freevars, &_Py_ID(__classdict__)) < 0) {
4155+
if (compiler_load_classdict_freevar(c, loc) < 0) {
41514156
goto error;
41524157
}
41534158
} else {
@@ -4181,7 +4186,8 @@ compiler_nameop(struct compiler *c, location loc,
41814186
if (op == LOAD_GLOBAL) {
41824187
arg <<= 1;
41834188
}
4184-
return codegen_addop_i(INSTR_SEQUENCE(c), op, arg, loc);
4189+
ADDOP_I(c, loc, op, arg);
4190+
return SUCCESS;
41854191

41864192
error:
41874193
Py_DECREF(mangled);

0 commit comments

Comments
 (0)