| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2c9f889 commit 9c18e81
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,7 @@ extern "C" { | |||
| 13 | 13 | #define ROT_THREE 3 | |
| 14 | 14 | #define DUP_TOP 4 | |
| 15 | 15 | #define ROT_FOUR 5 | |
| 16 | + #define NOP 9 | ||
| 16 | 17 | ||
| 17 | 18 | #define UNARY_POSITIVE 10 | |
| 18 | 19 | #define UNARY_NEGATIVE 11 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,6 +49,7 @@ def jabs_op(name, op): | |||
| 49 | 49 | def_op('DUP_TOP', 4) | |
| 50 | 50 | def_op('ROT_FOUR', 5) | |
| 51 | 51 | ||
| 52 | + def_op('NOP', 9) | ||
| 52 | 53 | def_op('UNARY_POSITIVE', 10) | |
| 53 | 54 | def_op('UNARY_NEGATIVE', 11) | |
| 54 | 55 | def_op('UNARY_NOT', 12) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -849,6 +849,9 @@ eval_frame(PyFrameObject *f) | |||
| 849 | 849 | ||
| 850 | 850 | /* case STOP_CODE: this is an error! */ | |
| 851 | 851 | ||
| 852 | + case NOP: | ||
| 853 | + goto fast_next_opcode; | ||
| 854 | + | ||
| 852 | 855 | case LOAD_FAST: | |
| 853 | 856 | x = GETLOCAL(oparg); | |
| 854 | 857 | if (x != NULL) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -392,6 +392,33 @@ optimize_code(PyObject *code, PyObject* consts) | |||
| 392 | 392 | opcode = codestr[i]; | |
| 393 | 393 | switch (opcode) { | |
| 394 | 394 | ||
| 395 | + /* Replace UNARY_NOT JUMP_IF_FALSE with NOP JUMP_IF_TRUE */ | ||
| 396 | + case UNARY_NOT: | ||
| 397 | + if (codestr[i+1] != JUMP_IF_FALSE || | ||
| 398 | + codestr[i+4] != POP_TOP || | ||
| 399 | + !ISBASICBLOCK(blocks,i,5)) | ||
| 400 | + continue; | ||
| 401 | + tgt = GETJUMPTGT(codestr, (i+1)); | ||
| 402 | + if (codestr[tgt] != POP_TOP) | ||
| 403 | + continue; | ||
| 404 | + codestr[i] = NOP; | ||
| 405 | + codestr[i+1] = JUMP_IF_TRUE; | ||
| 406 | + break; | ||
| 407 | + | ||
| 408 | + /* not a is b --> a is not b | ||
| 409 | + not a in b --> a not in b | ||
| 410 | + not a is not b --> a is b | ||
| 411 | + not a not in b --> a in b */ | ||
| 412 | + case COMPARE_OP: | ||
| 413 | + j = GETARG(codestr, i); | ||
| 414 | + if (j < 6 || j > 9 || | ||
| 415 | + codestr[i+3] != UNARY_NOT || | ||
| 416 | + !ISBASICBLOCK(blocks,i,4)) | ||
| 417 | + continue; | ||
| 418 | + SETARG(codestr, i, (j^1)); | ||
| 419 | + codestr[i+3] = NOP; | ||
| 420 | + break; | ||
| 421 | + | ||
| 395 | 422 | /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP. | |
| 396 | 423 | Note, only the first opcode is changed, the others still | |
| 397 | 424 | perform normally if they happen to be jump targets. */ | |
@@ -418,8 +445,8 @@ optimize_code(PyObject *code, PyObject* consts) | |||
| 418 | 445 | codestr[i] = ROT_TWO; | |
| 419 | 446 | codestr[i+1] = JUMP_FORWARD; | |
| 420 | 447 | SETARG(codestr, i+1, 2); | |
| 421 | - codestr[i+4] = DUP_TOP; /* Filler codes used as NOPs */ | ||
| 422 | - codestr[i+5] = POP_TOP; | ||
| 448 | + codestr[i+4] = NOP; | ||
| 449 | + codestr[i+5] = NOP; | ||
| 423 | 450 | continue; | |
| 424 | 451 | } | |
| 425 | 452 | if (GETARG(codestr, i) == 3 && \ | |
@@ -428,7 +455,7 @@ optimize_code(PyObject *code, PyObject* consts) | |||
| 428 | 455 | codestr[i+1] = ROT_TWO; | |
| 429 | 456 | codestr[i+2] = JUMP_FORWARD; | |
| 430 | 457 | SETARG(codestr, i+2, 1); | |
| 431 | - codestr[i+5] = DUP_TOP; | ||
| 458 | + codestr[i+5] = NOP; | ||
| 432 | 459 | } | |
| 433 | 460 | break; | |
| 434 | 461 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments