ruby-changes:50735
From: naruse <ko1@a...>
Date: Sun, 25 Mar 2018 01:00:33 +0900 (JST)
Subject: [ruby-changes:50735] naruse:r62911 (ruby_2_5): merge revision(s) 61587, 61617, 61618: [Backport #14273]
naruse 2018-03-25 01:00:27 +0900 (Sun, 25 Mar 2018) New Revision: 62911 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62911 Log: merge revision(s) 61587,61617,61618: [Backport #14273] compile.c: next label * compile.c (compile_next): label for jump to the end of block is removable. compile.c: remove more unreachable chunk * compile.c (remove_unreachable_chunk): remove beyond labels to be removed. compile.c: fix stack consistency error * compile.c (iseq_peephole_optimize): fix stack consistency error from return in loop, by adding extra `pop` when replacing `jump` with `leave`, which is never reached but needed to adjust sp calculation. [ruby-core:84589] [Bug #14273] Modified directories: branches/ruby_2_5/ Modified files: branches/ruby_2_5/compile.c branches/ruby_2_5/test/ruby/test_syntax.rb branches/ruby_2_5/version.h Index: ruby_2_5/version.h =================================================================== --- ruby_2_5/version.h (revision 62910) +++ ruby_2_5/version.h (revision 62911) @@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_5/version.h#L1 #define RUBY_VERSION "2.5.1" -#define RUBY_RELEASE_DATE "2018-03-24" -#define RUBY_PATCHLEVEL 52 +#define RUBY_RELEASE_DATE "2018-03-25" +#define RUBY_PATCHLEVEL 53 #define RUBY_RELEASE_YEAR 2018 #define RUBY_RELEASE_MONTH 3 -#define RUBY_RELEASE_DAY 24 +#define RUBY_RELEASE_DAY 25 #include "ruby/version.h" Index: ruby_2_5/test/ruby/test_syntax.rb =================================================================== --- ruby_2_5/test/ruby/test_syntax.rb (revision 62910) +++ ruby_2_5/test/ruby/test_syntax.rb (revision 62911) @@ -1134,6 +1134,15 @@ eom https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/ruby/test_syntax.rb#L1134 assert_equal(:begin, result) end + def test_return_in_loop + obj = Object.new + def obj.test + x = nil + return until x unless x + end + assert_nil obj.test + end + private def not_label(x) @result = x; @not_label ||= nil end Index: ruby_2_5/compile.c =================================================================== --- ruby_2_5/compile.c (revision 62910) +++ ruby_2_5/compile.c (revision 62911) @@ -320,8 +320,8 @@ static void iseq_add_setlocal(rb_iseq_t https://github.com/ruby/ruby/blob/trunk/ruby_2_5/compile.c#L320 (VALUE)(ls) | 1, (VALUE)(le) | 1, \ (VALUE)(iseqv), (VALUE)(lc) | 1); \ LABEL_UNREMOVABLE(ls); \ - LABEL_UNREMOVABLE(le); \ - LABEL_UNREMOVABLE(lc); \ + LABEL_REF(le); \ + LABEL_REF(lc); \ rb_ary_push(ISEQ_COMPILE_DATA(iseq)->catch_table_ary, freeze_hide_obj(_e)); \ } while (0) @@ -2295,33 +2295,58 @@ replace_destination(INSN *dobj, INSN *no https://github.com/ruby/ruby/blob/trunk/ruby_2_5/compile.c#L2295 if (!dl->refcnt) ELEM_REMOVE(&dl->link); } +static LABEL* +find_destination(INSN *i) +{ + int pos, len = insn_len(i->insn_id); + for (pos = 0; pos < len; ++pos) { + if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) { + return (LABEL *)OPERAND_AT(i, pos); + } + } + return 0; +} + static int remove_unreachable_chunk(rb_iseq_t *iseq, LINK_ELEMENT *i) { LINK_ELEMENT *first = i, *end; + int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no; if (!i) return 0; - while (i) { + unref_counts = ALLOCA_N(int, nlabels); + MEMZERO(unref_counts, int, nlabels); + end = i; + do { + LABEL *lab; if (IS_INSN(i)) { - if (IS_INSN_ID(i, jump) || IS_INSN_ID(i, leave)) { + if (IS_INSN_ID(i, leave)) { + end = i; break; } + else if ((lab = find_destination((INSN *)i)) != 0) { + if (lab->unremovable) break; + unref_counts[lab->label_no]++; + } } else if (IS_LABEL(i)) { - if (((LABEL *)i)->unremovable) return 0; - if (((LABEL *)i)->refcnt > 0) { + lab = (LABEL *)i; + if (lab->unremovable) return 0; + if (lab->refcnt > unref_counts[lab->label_no]) { if (i == first) return 0; - i = i->prev; break; } + continue; } else if (IS_TRACE(i)) { /* do nothing */ } - else return 0; - i = i->next; - } - end = i; + else if (IS_ADJUST(i)) { + LABEL *dest = ((ADJUST *)i)->label; + if (dest && dest->unremovable) return 0; + } + end = i; + } while ((i = i->next) != 0); i = first; do { if (IS_INSN(i)) { @@ -2416,6 +2441,7 @@ iseq_peephole_optimize(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/ruby_2_5/compile.c#L2441 goto again; } else if (IS_INSN_ID(diobj, leave)) { + INSN *pop; /* * jump LABEL * ... @@ -2423,6 +2449,7 @@ iseq_peephole_optimize(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/ruby_2_5/compile.c#L2449 * leave * => * leave + * pop * ... * LABEL: * leave @@ -2432,6 +2459,9 @@ iseq_peephole_optimize(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/ruby_2_5/compile.c#L2459 iobj->insn_id = BIN(leave); iobj->operand_size = 0; iobj->insn_info = diobj->insn_info; + /* adjust stack depth */ + pop = new_insn_body(iseq, diobj->insn_info.line_no, BIN(pop), 0); + ELEM_INSERT_NEXT(&iobj->link, &pop->link); goto again; } else if ((piobj = (INSN *)get_prev_insn(iobj)) != 0 && @@ -5064,6 +5094,7 @@ compile_next(rb_iseq_t *iseq, LINK_ANCHO https://github.com/ruby/ruby/blob/trunk/ruby_2_5/compile.c#L5094 add_ensure_iseq(ret, iseq, 0); ADD_INSNL(ret, line, jump, ISEQ_COMPILE_DATA(iseq)->end_label); ADD_ADJUST_RESTORE(ret, splabel); + splabel->unremovable = FALSE; if (!popped) { ADD_INSN(ret, line, putnil); Index: ruby_2_5 =================================================================== --- ruby_2_5 (revision 62910) +++ ruby_2_5 (revision 62911) Property changes on: ruby_2_5 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /trunk:r61587,61617-61618 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/