summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-compiler
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-08-01 20:31:19 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2021-08-01 20:31:19 +0900
commit9dc801a79810760b6911b2856e20b27e04e64986 (patch)
tree647bc4e545214517d36965d070f45a7c41ccc4b7 /mrbgems/mruby-compiler
parentb1cb6c3c699c52b5c00d27388f22c4a95149b03f (diff)
downloadmruby-9dc801a79810760b6911b2856e20b27e04e64986.tar.gz
mruby-9dc801a79810760b6911b2856e20b27e04e64986.zip
codegen.c: peephole optimize OP_JMPxxx.
Diffstat (limited to 'mrbgems/mruby-compiler')
-rw-r--r--mrbgems/mruby-compiler/core/codegen.c35
1 files changed, 32 insertions, 3 deletions
diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c
index 70b95d7e3..8cc3bad7a 100644
--- a/mrbgems/mruby-compiler/core/codegen.c
+++ b/mrbgems/mruby-compiler/core/codegen.c
@@ -524,9 +524,38 @@ genjmp2(codegen_scope *s, mrb_code i, uint16_t a, uint32_t pc, int val)
if (!no_peephole(s) && !val) {
struct mrb_insn_data data = mrb_last_insn(s);
- if (data.insn == OP_MOVE && data.a == a) {
- rewind_pc(s);
- a = data.b;
+ switch (data.insn) {
+ case OP_MOVE:
+ if (data.a == a && data.a > s->nlocals) {
+ rewind_pc(s);
+ a = data.b;
+ }
+ break;
+ case OP_LOADNIL:
+ case OP_LOADF:
+ if (data.a == a || data.a > s->nlocals) {
+ s->pc = addr_pc(s, data.addr);
+ if (i == OP_JMPNOT || (i == OP_JMPNIL && data.insn == OP_LOADNIL)) {
+ return genjmp(s, OP_JMP, pc);
+ }
+ else { /* OP_JMPIF */
+ return JMPLINK_START;
+ }
+ }
+ break;
+ case OP_LOADT: case OP_LOADI: case OP_LOADINEG: case OP_LOADI__1:
+ case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3:
+ case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7:
+ if (data.a == a || data.a > s->nlocals) {
+ s->pc = addr_pc(s, data.addr);
+ if (i == OP_JMPIF) {
+ return genjmp(s, OP_JMP, pc);
+ }
+ else { /* OP_JMPNOT and OP_JMPNIL */
+ return JMPLINK_START;
+ }
+ }
+ break;
}
}