Skip to content

Commit 18a6967

Browse files
authored
1 parent a694b82 commit 18a6967

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

Python/compile.c

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,24 @@ struct instr {
171171
struct basicblock_ *i_except; /* target block when exception is raised */
172172
};
173173

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+
174192
typedef struct exceptstack {
175193
struct basicblock_ *handlers[CO_MAXBLOCKS+1];
176194
int depth;
@@ -218,7 +236,8 @@ instr_size(struct instr *instruction)
218236
{
219237
int opcode = instruction->i_opcode;
220238
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);
222241
int extended_args = (0xFFFFFF < oparg) + (0xFFFF < oparg) + (0xFF < oparg);
223242
int caches = _PyOpcode_Caches[opcode];
224243
return extended_args + 1 + caches;
@@ -229,7 +248,8 @@ write_instr(_Py_CODEUNIT *codestr, struct instr *instruction, int ilen)
229248
{
230249
int opcode = instruction->i_opcode;
231250
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);
233253
int caches = _PyOpcode_Caches[opcode];
234254
switch (ilen - caches) {
235255
case 4:
@@ -7598,7 +7618,7 @@ convert_exception_handlers_to_nops(basicblock *entryblock) {
75987618
for (int i = 0; i < b->b_iused; i++) {
75997619
struct instr *instr = &b->b_instr[i];
76007620
if (is_block_push(instr) || instr->i_opcode == POP_BLOCK) {
7601-
instr->i_opcode = NOP;
7621+
INSTR_SET_OP0(instr, NOP);
76027622
}
76037623
}
76047624
}
@@ -8723,7 +8743,7 @@ remove_redundant_jumps(cfg_builder *g) {
87238743
}
87248744
if (last->i_target == b->b_next) {
87258745
assert(b->b_next->b_iused);
8726-
last->i_opcode = NOP;
8746+
INSTR_SET_OP0(last, NOP);
87278747
}
87288748
}
87298749
}
@@ -8999,10 +9019,9 @@ fold_tuple_on_constants(PyObject *const_cache,
89999019
}
90009020
Py_DECREF(newconst);
90019021
for (int i = 0; i < n; i++) {
9002-
inst[i].i_opcode = NOP;
9022+
INSTR_SET_OP0(&inst[i], NOP);
90039023
}
9004-
inst[n].i_opcode = LOAD_CONST;
9005-
inst[n].i_oparg = (int)index;
9024+
INSTR_SET_OP1(&inst[n], LOAD_CONST, (int)index);
90069025
return 0;
90079026
}
90089027

@@ -9099,7 +9118,7 @@ swaptimize(basicblock *block, int *ix)
90999118
}
91009119
// NOP out any unused instructions:
91019120
while (0 <= current) {
9102-
instructions[current--].i_opcode = NOP;
9121+
INSTR_SET_OP0(&instructions[current--], NOP);
91039122
}
91049123
PyMem_Free(stack);
91059124
*ix += len - 1;
@@ -9165,7 +9184,7 @@ apply_static_swaps(basicblock *block, int i)
91659184
}
91669185
}
91679186
// Success!
9168-
swap->i_opcode = NOP;
9187+
INSTR_SET_OP0(swap, NOP);
91699188
struct instr temp = block->b_instr[j];
91709189
block->b_instr[j] = block->b_instr[k];
91719190
block->b_instr[k] = temp;
@@ -9202,7 +9221,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
92029221
assert(PyDict_CheckExact(const_cache));
92039222
assert(PyList_CheckExact(consts));
92049223
struct instr nop;
9205-
nop.i_opcode = NOP;
9224+
INSTR_SET_OP0(&nop, NOP);
92069225
struct instr *target;
92079226
for (int i = 0; i < bb->b_iused; i++) {
92089227
struct instr *inst = &bb->b_instr[i];
@@ -9236,13 +9255,13 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
92369255
if (is_true == -1) {
92379256
goto error;
92389257
}
9239-
inst->i_opcode = NOP;
9258+
INSTR_SET_OP0(inst, NOP);
92409259
jump_if_true = nextop == POP_JUMP_IF_TRUE;
92419260
if (is_true == jump_if_true) {
92429261
bb->b_instr[i+1].i_opcode = JUMP;
92439262
}
92449263
else {
9245-
bb->b_instr[i+1].i_opcode = NOP;
9264+
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
92469265
}
92479266
break;
92489267
case JUMP_IF_FALSE_OR_POP:
@@ -9261,8 +9280,8 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
92619280
bb->b_instr[i+1].i_opcode = JUMP;
92629281
}
92639282
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);
92669285
}
92679286
break;
92689287
case IS_OP:
@@ -9273,8 +9292,8 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
92739292
int jump_op = i+2 < bb->b_iused ? bb->b_instr[i+2].i_opcode : 0;
92749293
if (Py_IsNone(cnt) && (jump_op == POP_JUMP_IF_FALSE || jump_op == POP_JUMP_IF_TRUE)) {
92759294
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);
92789297
bb->b_instr[i+2].i_opcode = nextarg ^ (jump_op == POP_JUMP_IF_FALSE) ?
92799298
POP_JUMP_IF_NOT_NONE : POP_JUMP_IF_NONE;
92809299
}
@@ -9292,12 +9311,12 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
92929311
if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
92939312
switch(oparg) {
92949313
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);
92979316
continue;
92989317
case 2:
92999318
case 3:
9300-
inst->i_opcode = NOP;
9319+
INSTR_SET_OP0(inst, NOP);
93019320
bb->b_instr[i+1].i_opcode = SWAP;
93029321
continue;
93039322
}
@@ -9406,7 +9425,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
94069425
break;
94079426
case SWAP:
94089427
if (oparg == 1) {
9409-
inst->i_opcode = NOP;
9428+
INSTR_SET_OP0(inst, NOP);
94109429
break;
94119430
}
94129431
if (swaptimize(bb, &i)) {
@@ -9418,8 +9437,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
94189437
break;
94199438
case PUSH_NULL:
94209439
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);
94239441
inst[1].i_oparg |= 1;
94249442
}
94259443
break;
@@ -9448,7 +9466,7 @@ inline_small_exit_blocks(basicblock *bb) {
94489466
}
94499467
basicblock *target = last->i_target;
94509468
if (basicblock_exits_scope(target) && target->b_iused <= MAX_COPY_SIZE) {
9451-
last->i_opcode = NOP;
9469+
INSTR_SET_OP0(last, NOP);
94529470
if (basicblock_append_instructions(bb, target) < 0) {
94539471
return -1;
94549472
}

0 commit comments

Comments
 (0)