Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions Python/compile.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -171,6 +171,24 @@ struct instr{
struct basicblock_ *i_except; /* target block when exception is raised */
};

/* One arg*/
#define INSTR_SET_OP1(I, OP, ARG) \
do{\
assert(HAS_ARG(OP)); \
struct instr *_instr__ptr_ = (I); \
_instr__ptr_->i_opcode = (OP); \
_instr__ptr_->i_oparg = (ARG); \
} while (0);

/* No args*/
#define INSTR_SET_OP0(I, OP) \
do{\
assert(!HAS_ARG(OP)); \
struct instr *_instr__ptr_ = (I); \
_instr__ptr_->i_opcode = (OP); \
_instr__ptr_->i_oparg = 0; \
} while (0);

typedef struct exceptstack{
struct basicblock_ *handlers[CO_MAXBLOCKS+1];
int depth;
Expand DownExpand Up@@ -218,7 +236,8 @@ instr_size(struct instr *instruction)
{
int opcode = instruction->i_opcode;
assert(!IS_PSEUDO_OPCODE(opcode));
int oparg = HAS_ARG(opcode) ? instruction->i_oparg : 0;
int oparg = instruction->i_oparg;
assert(HAS_ARG(opcode) || oparg == 0);
int extended_args = (0xFFFFFF < oparg) + (0xFFFF < oparg) + (0xFF < oparg);
int caches = _PyOpcode_Caches[opcode];
return extended_args + 1 + caches;
Expand All@@ -229,7 +248,8 @@ write_instr(_Py_CODEUNIT *codestr, struct instr *instruction, int ilen)
{
int opcode = instruction->i_opcode;
assert(!IS_PSEUDO_OPCODE(opcode));
int oparg = HAS_ARG(opcode) ? instruction->i_oparg : 0;
int oparg = instruction->i_oparg;
assert(HAS_ARG(opcode) || oparg == 0);
int caches = _PyOpcode_Caches[opcode];
switch (ilen - caches){
case 4:
Expand DownExpand Up@@ -7598,7 +7618,7 @@ convert_exception_handlers_to_nops(basicblock *entryblock){
for (int i = 0; i < b->b_iused; i++){
struct instr *instr = &b->b_instr[i];
if (is_block_push(instr) || instr->i_opcode == POP_BLOCK){
instr->i_opcode = NOP;
INSTR_SET_OP0(instr, NOP);
}
}
}
Expand DownExpand Up@@ -8723,7 +8743,7 @@ remove_redundant_jumps(cfg_builder *g){
}
if (last->i_target == b->b_next){
assert(b->b_next->b_iused);
last->i_opcode = NOP;
INSTR_SET_OP0(last, NOP);
}
}
}
Expand DownExpand Up@@ -8999,10 +9019,9 @@ fold_tuple_on_constants(PyObject *const_cache,
}
Py_DECREF(newconst);
for (int i = 0; i < n; i++){
inst[i].i_opcode = NOP;
INSTR_SET_OP0(&inst[i], NOP);
}
inst[n].i_opcode = LOAD_CONST;
inst[n].i_oparg = (int)index;
INSTR_SET_OP1(&inst[n], LOAD_CONST, (int)index);
return 0;
}

Expand DownExpand Up@@ -9099,7 +9118,7 @@ swaptimize(basicblock *block, int *ix)
}
// NOP out any unused instructions:
while (0 <= current){
instructions[current--].i_opcode = NOP;
INSTR_SET_OP0(&instructions[current--], NOP);
}
PyMem_Free(stack);
*ix += len - 1;
Expand DownExpand Up@@ -9165,7 +9184,7 @@ apply_static_swaps(basicblock *block, int i)
}
}
// Success!
swap->i_opcode = NOP;
INSTR_SET_OP0(swap, NOP);
struct instr temp = block->b_instr[j];
block->b_instr[j] = block->b_instr[k];
block->b_instr[k] = temp;
Expand DownExpand Up@@ -9202,7 +9221,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
assert(PyDict_CheckExact(const_cache));
assert(PyList_CheckExact(consts));
struct instr nop;
nop.i_opcode = NOP;
INSTR_SET_OP0(&nop, NOP);
struct instr *target;
for (int i = 0; i < bb->b_iused; i++){
struct instr *inst = &bb->b_instr[i];
Expand DownExpand Up@@ -9236,13 +9255,13 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
if (is_true == -1){
goto error;
}
inst->i_opcode = NOP;
INSTR_SET_OP0(inst, NOP);
jump_if_true = nextop == POP_JUMP_IF_TRUE;
if (is_true == jump_if_true){
bb->b_instr[i+1].i_opcode = JUMP;
}
else{
bb->b_instr[i+1].i_opcode = NOP;
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
}
break;
case JUMP_IF_FALSE_OR_POP:
Expand All@@ -9261,8 +9280,8 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
bb->b_instr[i+1].i_opcode = JUMP;
}
else{
inst->i_opcode = NOP;
bb->b_instr[i+1].i_opcode = NOP;
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
}
break;
case IS_OP:
Expand All@@ -9273,8 +9292,8 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
int jump_op = i+2 < bb->b_iused ? bb->b_instr[i+2].i_opcode : 0;
if (Py_IsNone(cnt) && (jump_op == POP_JUMP_IF_FALSE || jump_op == POP_JUMP_IF_TRUE)){
unsigned char nextarg = bb->b_instr[i+1].i_oparg;
inst->i_opcode = NOP;
bb->b_instr[i+1].i_opcode = NOP;
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
bb->b_instr[i+2].i_opcode = nextarg ^ (jump_op == POP_JUMP_IF_FALSE) ?
POP_JUMP_IF_NOT_NONE : POP_JUMP_IF_NONE;
}
Expand All@@ -9292,12 +9311,12 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg){
switch(oparg){
case 1:
inst->i_opcode = NOP;
bb->b_instr[i+1].i_opcode = NOP;
INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
continue;
case 2:
case 3:
inst->i_opcode = NOP;
INSTR_SET_OP0(inst, NOP);
bb->b_instr[i+1].i_opcode = SWAP;
continue;
}
Expand DownExpand Up@@ -9406,7 +9425,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
break;
case SWAP:
if (oparg == 1){
inst->i_opcode = NOP;
INSTR_SET_OP0(inst, NOP);
break;
}
if (swaptimize(bb, &i)){
Expand All@@ -9418,8 +9437,7 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
break;
case PUSH_NULL:
if (nextop == LOAD_GLOBAL && (inst[1].i_opcode & 1) == 0){
inst->i_opcode = NOP;
inst->i_oparg = 0;
INSTR_SET_OP0(inst, NOP);
inst[1].i_oparg |= 1;
}
break;
Expand DownExpand Up@@ -9448,7 +9466,7 @@ inline_small_exit_blocks(basicblock *bb){
}
basicblock *target = last->i_target;
if (basicblock_exits_scope(target) && target->b_iused <= MAX_COPY_SIZE){
last->i_opcode = NOP;
INSTR_SET_OP0(last, NOP);
if (basicblock_append_instructions(bb, target) < 0){
return -1;
}
Expand Down