@@ -171,6 +171,24 @@ struct instr {
171
171
struct basicblock_ * i_except ; /* target block when exception is raised */
172
172
};
173
173
174
+ /* One arg*/
175
+ #define INSTR_SET_OP1 (I , OP , ARG ) \
176
+ do { \
177
+ assert(HAS_ARG(OP)); \
178
+ struct instr *_instr__ptr_ = (I); \
179
+ _instr__ptr_->i_opcode = (OP); \
180
+ _instr__ptr_->i_oparg = (ARG); \
181
+ } while (0);
182
+
183
+ /* No args*/
184
+ #define INSTR_SET_OP0 (I , OP ) \
185
+ do { \
186
+ assert(!HAS_ARG(OP)); \
187
+ struct instr *_instr__ptr_ = (I); \
188
+ _instr__ptr_->i_opcode = (OP); \
189
+ _instr__ptr_->i_oparg = 0; \
190
+ } while (0);
191
+
174
192
typedef struct exceptstack {
175
193
struct basicblock_ * handlers [CO_MAXBLOCKS + 1 ];
176
194
int depth ;
@@ -218,7 +236,8 @@ instr_size(struct instr *instruction)
218
236
{
219
237
int opcode = instruction -> i_opcode ;
220
238
assert (!IS_PSEUDO_OPCODE (opcode ));
221
- int oparg = HAS_ARG (opcode ) ? instruction -> i_oparg : 0 ;
239
+ int oparg = instruction -> i_oparg ;
240
+ assert (HAS_ARG (opcode ) || oparg == 0 );
222
241
int extended_args = (0xFFFFFF < oparg ) + (0xFFFF < oparg ) + (0xFF < oparg );
223
242
int caches = _PyOpcode_Caches [opcode ];
224
243
return extended_args + 1 + caches ;
@@ -229,7 +248,8 @@ write_instr(_Py_CODEUNIT *codestr, struct instr *instruction, int ilen)
229
248
{
230
249
int opcode = instruction -> i_opcode ;
231
250
assert (!IS_PSEUDO_OPCODE (opcode ));
232
- int oparg = HAS_ARG (opcode ) ? instruction -> i_oparg : 0 ;
251
+ int oparg = instruction -> i_oparg ;
252
+ assert (HAS_ARG (opcode ) || oparg == 0 );
233
253
int caches = _PyOpcode_Caches [opcode ];
234
254
switch (ilen - caches ) {
235
255
case 4 :
@@ -7598,7 +7618,7 @@ convert_exception_handlers_to_nops(basicblock *entryblock) {
7598
7618
for (int i = 0 ; i < b -> b_iused ; i ++ ) {
7599
7619
struct instr * instr = & b -> b_instr [i ];
7600
7620
if (is_block_push (instr ) || instr -> i_opcode == POP_BLOCK ) {
7601
- instr -> i_opcode = NOP ;
7621
+ INSTR_SET_OP0 ( instr , NOP ) ;
7602
7622
}
7603
7623
}
7604
7624
}
@@ -8723,7 +8743,7 @@ remove_redundant_jumps(cfg_builder *g) {
8723
8743
}
8724
8744
if (last -> i_target == b -> b_next ) {
8725
8745
assert (b -> b_next -> b_iused );
8726
- last -> i_opcode = NOP ;
8746
+ INSTR_SET_OP0 ( last , NOP ) ;
8727
8747
}
8728
8748
}
8729
8749
}
@@ -8999,10 +9019,9 @@ fold_tuple_on_constants(PyObject *const_cache,
8999
9019
}
9000
9020
Py_DECREF (newconst );
9001
9021
for (int i = 0 ; i < n ; i ++ ) {
9002
- inst [i ]. i_opcode = NOP ;
9022
+ INSTR_SET_OP0 ( & inst [i ], NOP ) ;
9003
9023
}
9004
- inst [n ].i_opcode = LOAD_CONST ;
9005
- inst [n ].i_oparg = (int )index ;
9024
+ INSTR_SET_OP1 (& inst [n ], LOAD_CONST , (int )index );
9006
9025
return 0 ;
9007
9026
}
9008
9027
@@ -9099,7 +9118,7 @@ swaptimize(basicblock *block, int *ix)
9099
9118
}
9100
9119
// NOP out any unused instructions:
9101
9120
while (0 <= current ) {
9102
- instructions [current -- ]. i_opcode = NOP ;
9121
+ INSTR_SET_OP0 ( & instructions [current -- ], NOP ) ;
9103
9122
}
9104
9123
PyMem_Free (stack );
9105
9124
* ix += len - 1 ;
@@ -9165,7 +9184,7 @@ apply_static_swaps(basicblock *block, int i)
9165
9184
}
9166
9185
}
9167
9186
// Success!
9168
- swap -> i_opcode = NOP ;
9187
+ INSTR_SET_OP0 ( swap , NOP ) ;
9169
9188
struct instr temp = block -> b_instr [j ];
9170
9189
block -> b_instr [j ] = block -> b_instr [k ];
9171
9190
block -> b_instr [k ] = temp ;
@@ -9202,7 +9221,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
9202
9221
assert (PyDict_CheckExact (const_cache ));
9203
9222
assert (PyList_CheckExact (consts ));
9204
9223
struct instr nop ;
9205
- nop . i_opcode = NOP ;
9224
+ INSTR_SET_OP0 ( & nop , NOP ) ;
9206
9225
struct instr * target ;
9207
9226
for (int i = 0 ; i < bb -> b_iused ; i ++ ) {
9208
9227
struct instr * inst = & bb -> b_instr [i ];
@@ -9236,13 +9255,13 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
9236
9255
if (is_true == -1 ) {
9237
9256
goto error ;
9238
9257
}
9239
- inst -> i_opcode = NOP ;
9258
+ INSTR_SET_OP0 ( inst , NOP ) ;
9240
9259
jump_if_true = nextop == POP_JUMP_IF_TRUE ;
9241
9260
if (is_true == jump_if_true ) {
9242
9261
bb -> b_instr [i + 1 ].i_opcode = JUMP ;
9243
9262
}
9244
9263
else {
9245
- bb -> b_instr [i + 1 ]. i_opcode = NOP ;
9264
+ INSTR_SET_OP0 ( & bb -> b_instr [i + 1 ], NOP ) ;
9246
9265
}
9247
9266
break ;
9248
9267
case JUMP_IF_FALSE_OR_POP :
@@ -9261,8 +9280,8 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
9261
9280
bb -> b_instr [i + 1 ].i_opcode = JUMP ;
9262
9281
}
9263
9282
else {
9264
- inst -> i_opcode = NOP ;
9265
- bb -> b_instr [i + 1 ]. i_opcode = NOP ;
9283
+ INSTR_SET_OP0 ( inst , NOP ) ;
9284
+ INSTR_SET_OP0 ( & bb -> b_instr [i + 1 ], NOP ) ;
9266
9285
}
9267
9286
break ;
9268
9287
case IS_OP :
@@ -9273,8 +9292,8 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
9273
9292
int jump_op = i + 2 < bb -> b_iused ? bb -> b_instr [i + 2 ].i_opcode : 0 ;
9274
9293
if (Py_IsNone (cnt ) && (jump_op == POP_JUMP_IF_FALSE || jump_op == POP_JUMP_IF_TRUE )) {
9275
9294
unsigned char nextarg = bb -> b_instr [i + 1 ].i_oparg ;
9276
- inst -> i_opcode = NOP ;
9277
- bb -> b_instr [i + 1 ]. i_opcode = NOP ;
9295
+ INSTR_SET_OP0 ( inst , NOP ) ;
9296
+ INSTR_SET_OP0 ( & bb -> b_instr [i + 1 ], NOP ) ;
9278
9297
bb -> b_instr [i + 2 ].i_opcode = nextarg ^ (jump_op == POP_JUMP_IF_FALSE ) ?
9279
9298
POP_JUMP_IF_NOT_NONE : POP_JUMP_IF_NONE ;
9280
9299
}
@@ -9292,12 +9311,12 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
9292
9311
if (nextop == UNPACK_SEQUENCE && oparg == bb -> b_instr [i + 1 ].i_oparg ) {
9293
9312
switch (oparg ) {
9294
9313
case 1 :
9295
- inst -> i_opcode = NOP ;
9296
- bb -> b_instr [i + 1 ]. i_opcode = NOP ;
9314
+ INSTR_SET_OP0 ( inst , NOP ) ;
9315
+ INSTR_SET_OP0 ( & bb -> b_instr [i + 1 ], NOP ) ;
9297
9316
continue ;
9298
9317
case 2 :
9299
9318
case 3 :
9300
- inst -> i_opcode = NOP ;
9319
+ INSTR_SET_OP0 ( inst , NOP ) ;
9301
9320
bb -> b_instr [i + 1 ].i_opcode = SWAP ;
9302
9321
continue ;
9303
9322
}
@@ -9406,7 +9425,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
9406
9425
break ;
9407
9426
case SWAP :
9408
9427
if (oparg == 1 ) {
9409
- inst -> i_opcode = NOP ;
9428
+ INSTR_SET_OP0 ( inst , NOP ) ;
9410
9429
break ;
9411
9430
}
9412
9431
if (swaptimize (bb , & i )) {
@@ -9418,8 +9437,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
9418
9437
break ;
9419
9438
case PUSH_NULL :
9420
9439
if (nextop == LOAD_GLOBAL && (inst [1 ].i_opcode & 1 ) == 0 ) {
9421
- inst -> i_opcode = NOP ;
9422
- inst -> i_oparg = 0 ;
9440
+ INSTR_SET_OP0 (inst , NOP );
9423
9441
inst [1 ].i_oparg |= 1 ;
9424
9442
}
9425
9443
break ;
@@ -9448,7 +9466,7 @@ inline_small_exit_blocks(basicblock *bb) {
9448
9466
}
9449
9467
basicblock * target = last -> i_target ;
9450
9468
if (basicblock_exits_scope (target ) && target -> b_iused <= MAX_COPY_SIZE ) {
9451
- last -> i_opcode = NOP ;
9469
+ INSTR_SET_OP0 ( last , NOP ) ;
9452
9470
if (basicblock_append_instructions (bb , target ) < 0 ) {
9453
9471
return -1 ;
9454
9472
}
0 commit comments