67
67
return ERROR; \
68
68
}
69
69
70
+ #define RETURN_IF_ERROR_IN_SCOPE (C , CALL ) { \
71
+ if ((CALL) < 0) { \
72
+ compiler_exit_scope((C)); \
73
+ return ERROR; \
74
+ } \
75
+ }
76
+
70
77
#define IS_TOP_LEVEL_AWAIT (C ) ( \
71
78
((C)->c_flags.cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
72
79
&& ((C)->u->u_ste->ste_type == ModuleBlock))
@@ -291,8 +298,6 @@ typedef struct {
291
298
Py_ssize_t on_top ;
292
299
} pattern_context ;
293
300
294
- static int codegen_addop_i (instr_sequence * seq , int opcode , Py_ssize_t oparg , location loc );
295
-
296
301
static void compiler_free (struct compiler * );
297
302
static int compiler_error (struct compiler * , location loc , const char * , ...);
298
303
static int compiler_warn (struct compiler * , location loc , const char * , ...);
@@ -685,6 +690,29 @@ compiler_set_qualname(struct compiler *c)
685
690
return SUCCESS ;
686
691
}
687
692
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
+
688
716
static int
689
717
codegen_addop_noarg (instr_sequence * seq , int opcode , location loc )
690
718
{
@@ -693,6 +721,12 @@ codegen_addop_noarg(instr_sequence *seq, int opcode, location loc)
693
721
return _PyInstructionSequence_Addop (seq , opcode , 0 , loc );
694
722
}
695
723
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
+
696
730
static Py_ssize_t
697
731
dict_add_o (PyObject * dict , PyObject * o )
698
732
{
@@ -854,24 +888,57 @@ compiler_add_const(struct compiler *c, PyObject *o)
854
888
}
855
889
856
890
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 )
858
892
{
859
893
Py_ssize_t arg = compiler_add_const (c , o );
860
894
if (arg < 0 ) {
861
895
return ERROR ;
862
896
}
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); \
864
918
}
865
919
866
920
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 )
869
923
{
870
924
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); \
875
942
}
876
943
877
944
#define LOAD_METHOD -1
@@ -880,8 +947,8 @@ compiler_addop_o(struct compiler *c, location loc,
880
947
#define LOAD_ZERO_SUPER_METHOD -4
881
948
882
949
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 )
885
952
{
886
953
PyObject * mangled = compiler_maybe_mangle (c , o );
887
954
if (!mangled ) {
@@ -918,25 +985,12 @@ compiler_addop_name(struct compiler *c, location loc,
918
985
arg <<= 2 ;
919
986
arg |= 1 ;
920
987
}
921
- return codegen_addop_i (INSTR_SEQUENCE (c ), opcode , arg , loc );
988
+ ADDOP_I (c , loc , opcode , arg );
989
+ return SUCCESS ;
922
990
}
923
991
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)))
940
994
941
995
static int
942
996
codegen_addop_j (instr_sequence * seq , location loc ,
@@ -948,49 +1002,6 @@ codegen_addop_j(instr_sequence *seq, location loc,
948
1002
return _PyInstructionSequence_Addop (seq , opcode , target .id , loc );
949
1003
}
950
1004
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
-
994
1005
#define ADDOP_JUMP (C , LOC , OP , O ) \
995
1006
RETURN_IF_ERROR(codegen_addop_j(INSTR_SEQUENCE(C), (LOC), (OP), (O)))
996
1007
@@ -2279,7 +2290,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
2279
2290
Py_DECREF (type_params_name );
2280
2291
RETURN_IF_ERROR_IN_SCOPE (c , compiler_type_params (c , type_params ));
2281
2292
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 );
2283
2294
}
2284
2295
}
2285
2296
@@ -2300,10 +2311,8 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
2300
2311
}
2301
2312
2302
2313
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 );
2307
2316
2308
2317
c -> u -> u_metadata .u_argcount = num_typeparam_args ;
2309
2318
PyCodeObject * co = optimize_and_assemble (c , 0 );
@@ -2393,12 +2402,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2393
2402
// We can't use compiler_nameop here because we need to generate a
2394
2403
// STORE_DEREF in a class namespace, and compiler_nameop() won't do
2395
2404
// 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 );
2402
2406
}
2403
2407
/* compile the body proper */
2404
2408
if (compiler_body (c , loc , s -> v .ClassDef .body ) < 0 ) {
@@ -2527,9 +2531,7 @@ compiler_class(struct compiler *c, stmt_ty s)
2527
2531
_Py_DECLARE_STR (type_params , ".type_params" );
2528
2532
_Py_DECLARE_STR (generic_base , ".generic_base" );
2529
2533
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 );
2533
2535
RETURN_IF_ERROR_IN_SCOPE (c , compiler_nameop (c , loc , & _Py_STR (generic_base ), Store ));
2534
2536
2535
2537
Py_ssize_t original_len = asdl_seq_LEN (s -> v .ClassDef .bases );
@@ -2630,9 +2632,7 @@ compiler_typealias(struct compiler *c, stmt_ty s)
2630
2632
return ERROR ;
2631
2633
}
2632
2634
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 );
2636
2636
RETURN_IF_ERROR_IN_SCOPE (c , compiler_type_params (c , type_params ));
2637
2637
}
2638
2638
else {
@@ -2713,8 +2713,8 @@ check_compare(struct compiler *c, expr_ty e)
2713
2713
return SUCCESS ;
2714
2714
}
2715
2715
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 )
2718
2718
{
2719
2719
int cmp ;
2720
2720
switch (op ) {
@@ -4040,6 +4040,13 @@ addop_yield(struct compiler *c, location loc) {
4040
4040
return SUCCESS ;
4041
4041
}
4042
4042
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
+
4043
4050
static int
4044
4051
compiler_nameop (struct compiler * c , location loc ,
4045
4052
identifier name , expr_context_ty ctx )
@@ -4119,8 +4126,7 @@ compiler_nameop(struct compiler *c, location loc,
4119
4126
else if (SYMTABLE_ENTRY (c )-> ste_can_see_class_scope ) {
4120
4127
op = LOAD_FROM_DICT_OR_DEREF ;
4121
4128
// 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 ) {
4124
4130
goto error ;
4125
4131
}
4126
4132
}
@@ -4146,8 +4152,7 @@ compiler_nameop(struct compiler *c, location loc,
4146
4152
if (SYMTABLE_ENTRY (c )-> ste_can_see_class_scope && scope == GLOBAL_IMPLICIT ) {
4147
4153
op = LOAD_FROM_DICT_OR_GLOBALS ;
4148
4154
// 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 ) {
4151
4156
goto error ;
4152
4157
}
4153
4158
} else {
@@ -4181,7 +4186,8 @@ compiler_nameop(struct compiler *c, location loc,
4181
4186
if (op == LOAD_GLOBAL ) {
4182
4187
arg <<= 1 ;
4183
4188
}
4184
- return codegen_addop_i (INSTR_SEQUENCE (c ), op , arg , loc );
4189
+ ADDOP_I (c , loc , op , arg );
4190
+ return SUCCESS ;
4185
4191
4186
4192
error :
4187
4193
Py_DECREF (mangled );
0 commit comments