diff --git a/gc.c b/gc.c index bf47be625a..d509e60fd3 100644 --- a/gc.c +++ b/gc.c @@ -1805,10 +1805,7 @@ rb_objspace_set_event_hook(const rb_event_flag_t event) static void gc_event_hook_body(rb_execution_context_t *ec, rb_objspace_t *objspace, const rb_event_flag_t event, VALUE data) { - /* increment PC because source line is calculated with PC-1 */ - ec->cfp->pc++; EXEC_EVENT_HOOK(ec, event, ec->cfp->self, 0, 0, 0, data); - ec->cfp->pc--; } #define gc_event_hook_available_p(objspace) ((objspace)->flags.has_hook) @@ -9955,4 +9952,3 @@ ruby_xrealloc2(void *ptr, size_t n, size_t new_size) #endif return ruby_xrealloc2_body(ptr, n, new_size); } - diff --git a/insns.def b/insns.def index d9a49356a6..8c2f674149 100644 --- a/insns.def +++ b/insns.def @@ -43,8 +43,6 @@ * sp_inc: Used to dynamically calculate sp increase in `insn_stack_increase`. - * handles_frame: If it is true, VM moves pc before insn body. - - Attributes can access operands, but not stack (push/pop) variables. - An instruction's body is a pure C block, copied verbatimly into @@ -366,6 +364,7 @@ concatstrings // attr rb_snum_t sp_inc = 1 - num; { val = rb_str_concat_literals(num, STACK_ADDR_FROM_TOP(num)); + POPN(num); } /* push the result of to_s. */ @@ -401,6 +400,7 @@ toregexp const VALUE ary = rb_ary_tmp_new_from_values(0, cnt, STACK_ADDR_FROM_TOP(cnt)); val = rb_reg_new_ary(ary, (int)opt); rb_ary_clear(ary); + POPN(cnt); } /* intern str to Symbol and push it. */ @@ -422,6 +422,7 @@ newarray // attr rb_snum_t sp_inc = 1 - num; { val = rb_ary_new4(num, STACK_ADDR_FROM_TOP(num)); + POPN(num); } /* dup array */ @@ -450,7 +451,7 @@ expandarray (...) // attr rb_snum_t sp_inc = num - 1 + (flag & 1 ? 1 : 0); { - vm_expandarray(GET_SP(), ary, num, (int)flag); + vm_expandarray(GET_CFP(), ary, num, (int)flag); } /* concat two arrays */ @@ -487,6 +488,7 @@ newhash if (num) { rb_hash_bulk_insert(num, STACK_ADDR_FROM_TOP(num), val); + POPN(num); } } @@ -536,6 +538,7 @@ dupn void *dst = GET_SP(); void *src = STACK_ADDR_FROM_TOP(n); + INC_SP(n); /* alloca */ MEMCPY(dst, src, VALUE, n); } @@ -598,7 +601,7 @@ setn (VALUE val) // attr rb_snum_t sp_inc = 0; { - TOPN(n) = val; + TOPN(n - 1) = val; } /* empty current stack */ @@ -609,7 +612,7 @@ adjuststack (...) // attr rb_snum_t sp_inc = -(rb_snum_t)n; { - /* none */ + POPN(n); } /**********************************************************/ @@ -687,7 +690,6 @@ defineclass (ID id, ISEQ class_iseq, rb_num_t flags) (VALUE cbase, VALUE super) (VALUE val) -// attr bool handles_frame = true; { VALUE klass = vm_find_or_create_class_by_id(id, flags, cbase, super); @@ -714,7 +716,6 @@ send (CALL_INFO ci, CALL_CACHE cc, ISEQ blockiseq) (...) (VALUE val) -// attr bool handles_frame = true; // attr rb_snum_t sp_inc = - (int)(ci->orig_argc + ((ci->flag & VM_CALL_ARGS_BLOCKARG) ? 1 : 0)); { struct rb_calling_info calling; @@ -750,6 +751,7 @@ opt_newarray_max // attr rb_snum_t sp_inc = 1 - num; { val = vm_opt_newarray_max(num, STACK_ADDR_FROM_TOP(num)); + POPN(num); } DEFINE_INSN @@ -760,6 +762,7 @@ opt_newarray_min // attr rb_snum_t sp_inc = 1 - num; { val = vm_opt_newarray_min(num, STACK_ADDR_FROM_TOP(num)); + POPN(num); } /* Invoke method without block */ @@ -768,7 +771,6 @@ opt_send_without_block (CALL_INFO ci, CALL_CACHE cc) (...) (VALUE val) -// attr bool handles_frame = true; // attr rb_snum_t sp_inc = -ci->orig_argc; { struct rb_calling_info calling; @@ -783,7 +785,6 @@ invokesuper (CALL_INFO ci, CALL_CACHE cc, ISEQ blockiseq) (...) (VALUE val) -// attr bool handles_frame = true; // attr rb_snum_t sp_inc = - (int)(ci->orig_argc + ((ci->flag & VM_CALL_ARGS_BLOCKARG) ? 1 : 0)); { struct rb_calling_info calling; @@ -801,7 +802,6 @@ invokeblock (CALL_INFO ci) (...) (VALUE val) -// attr bool handles_frame = true; // attr rb_snum_t sp_inc = 1 - ci->orig_argc; { struct rb_calling_info calling; @@ -828,7 +828,6 @@ leave () (VALUE val) (VALUE val) -// attr bool handles_frame = true; { if (OPT_CHECKED_RUN) { const VALUE *const bp = vm_base_ptr(reg_cfp); @@ -990,6 +989,11 @@ opt_plus val = vm_opt_plus(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1004,6 +1008,11 @@ opt_minus val = vm_opt_minus(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1018,6 +1027,11 @@ opt_mult val = vm_opt_mult(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1032,6 +1046,11 @@ opt_div val = vm_opt_div(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1046,6 +1065,11 @@ opt_mod val = vm_opt_mod(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1060,6 +1084,11 @@ opt_eq val = opt_eq_func(recv, obj, ci, cc); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1075,7 +1104,9 @@ opt_neq if (val == Qundef) { #ifndef MJIT_HEADER - ADD_PC(2); /* !!! */ + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); #endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } @@ -1091,6 +1122,11 @@ opt_lt val = vm_opt_lt(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1105,6 +1141,11 @@ opt_le val = vm_opt_le(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1119,6 +1160,11 @@ opt_gt val = vm_opt_gt(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1133,6 +1179,11 @@ opt_ge val = vm_opt_ge(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1147,6 +1198,11 @@ opt_ltlt val = vm_opt_ltlt(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1161,6 +1217,11 @@ opt_aref val = vm_opt_aref(recv, obj); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1175,6 +1236,12 @@ opt_aset val = vm_opt_aset(recv, obj, set); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + PUSH(obj); + PUSH(set); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1193,9 +1260,10 @@ opt_aset_with } else { #ifndef MJIT_HEADER - TOPN(0) = rb_str_resurrect(key); - PUSH(val); - ADD_PC(1); /* !!! */ + PUSH(recv); + PUSH(rb_str_resurrect(key)); + PUSH(val); + ADD_PC(-WIDTH_OF_opt_send_without_block); #endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } @@ -1212,8 +1280,9 @@ opt_aref_with if (val == Qundef) { #ifndef MJIT_HEADER - PUSH(rb_str_resurrect(key)); - ADD_PC(1); /* !!! */ + PUSH(recv); + PUSH(rb_str_resurrect(key)); + ADD_PC(-WIDTH_OF_opt_send_without_block); #endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } @@ -1229,6 +1298,10 @@ opt_length val = vm_opt_length(recv, BOP_LENGTH); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1243,6 +1316,10 @@ opt_size val = vm_opt_length(recv, BOP_SIZE); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1257,6 +1334,10 @@ opt_empty_p val = vm_opt_empty_p(recv); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1271,6 +1352,10 @@ opt_succ val = vm_opt_succ(recv); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1285,6 +1370,10 @@ opt_not val = vm_opt_not(ci, cc, recv); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(recv); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1309,6 +1398,11 @@ opt_regexpmatch2 val = vm_opt_regexpmatch2(obj2, obj1); if (val == Qundef) { +#ifndef MJIT_HEADER + PUSH(obj2); + PUSH(obj1); + ADD_PC(-WIDTH_OF_opt_send_without_block); +#endif DISPATCH_ORIGINAL_INSN(opt_send_without_block); } } @@ -1319,7 +1413,6 @@ opt_call_c_function (rb_insn_func_t funcptr) () () -// attr bool handles_frame = true; { reg_cfp = (funcptr)(ec, reg_cfp); diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb index b6ec2f14eb..edb356d3dd 100644 --- a/test/ruby/test_settracefunc.rb +++ b/test/ruby/test_settracefunc.rb @@ -1901,4 +1901,17 @@ class TestSetTraceFunc < Test::Unit::TestCase assert_equal ["c-call", base_line + 35], events[9] # Thread#set_trace_func assert_equal nil, events[10] end + + def test_lineno_in_optimized_insn + actual, _, _ = EnvUtil.invoke_ruby [], <<-EOF.gsub(/^.*?: */, ""), true + 1: class String + 2: def -@ + 3: puts caller_locations(1, 1)[0].lineno + 4: end + 5: end + 6: + 7: -"" + EOF + assert_equal "7\n", actual, '[Bug #14809]' + end end diff --git a/tool/ruby_vm/models/bare_instructions.rb b/tool/ruby_vm/models/bare_instructions.rb index 7e573240b5..297a93f5b2 100755 --- a/tool/ruby_vm/models/bare_instructions.rb +++ b/tool/ruby_vm/models/bare_instructions.rb @@ -101,10 +101,6 @@ class RubyVM::BareInstructions }.join end - def handles_frame? - /\b(false|0)\b/ !~ @attrs['handles_frame'].expr.expr - end - def inspect sprintf "#<%s %s@%s:%d>", self.class.name, @name, @loc[0], @loc[1] end @@ -129,7 +125,6 @@ class RubyVM::BareInstructions generate_attribute 'rb_num_t', 'retn', rets.size generate_attribute 'rb_num_t', 'width', width generate_attribute 'rb_snum_t', 'sp_inc', rets.size - pops.size - generate_attribute 'bool', 'handles_frame', false end def typesplit a diff --git a/tool/ruby_vm/views/_insn_entry.erb b/tool/ruby_vm/views/_insn_entry.erb index 2118dece86..836ca32b6b 100644 --- a/tool/ruby_vm/views/_insn_entry.erb +++ b/tool/ruby_vm/views/_insn_entry.erb @@ -29,28 +29,17 @@ INSN_ENTRY(<%= insn.name %>) <%= pop[:name] %> = <%= insn.cast_from_VALUE pop, "TOPN(#{i})"%>; % end DEBUG_ENTER_INSN(INSN_ATTR(name)); -% if insn.handles_frame? ADD_PC(INSN_ATTR(width)); POPN(INSN_ATTR(popn)); -% end COLLECT_USAGE_INSN(INSN_ATTR(bin)); % insn.opes.each_with_index do |ope, i| COLLECT_USAGE_OPERAND(INSN_ATTR(bin), <%= i %>, <%= ope[:name] %>); % end <%= render_c_expr insn.expr -%> CHECK_VM_STACK_OVERFLOW_FOR_INSN(VM_REG_CFP, INSN_ATTR(retn)); -% if insn.handles_frame? -% insn.rets.reverse_each do |ret| +% insn.rets.each do |ret| PUSH(<%= insn.cast_to_VALUE ret %>); % end -% else - ADJ_SP(INSN_ATTR(sp_inc)); -% insn.rets.reverse_each.with_index do |ret, i| - TOPN(<%= i %>) = <%= insn.cast_to_VALUE ret %>; -% end - ADD_PC(INSN_ATTR(width)); - PREFETCH(GET_PC()); -% end END_INSN(<%= insn.name %>); # undef INSN_ATTR # undef NAME_OF_CURRENT_INSN diff --git a/tool/ruby_vm/views/_mjit_compile_insn_body.erb b/tool/ruby_vm/views/_mjit_compile_insn_body.erb index 9880689a84..5851313bf7 100644 --- a/tool/ruby_vm/views/_mjit_compile_insn_body.erb +++ b/tool/ruby_vm/views/_mjit_compile_insn_body.erb @@ -77,31 +77,6 @@ } fprintf(f, " goto cancel;\n"); % else -% if insn.handles_frame? -% # If insn.handles_frame? is true, cfp->sp might be changed inside insns (like vm_caller_setup_arg_block) -% # and thus we need to use cfp->sp, even when local_stack_p is TRUE. When insn.handles_frame? is true, -% # cfp->sp should be available too because _mjit_compile_pc_and_sp.erb sets it. fprintf(f, <%= to_cstr.call(line) %>); -% else -% # If local_stack_p is TRUE and insn.handles_frame? is false, stack values are only available in local variables -% # for stack. So we need to replace those macros if local_stack_p is TRUE here. -% case line -% when /\bGET_SP\(\)/ -% # reg_cfp->sp - fprintf(f, <%= to_cstr.call(line.sub(/\bGET_SP\(\)/, '%s')) %>, (status->local_stack_p ? "(stack + stack_size)" : "GET_SP()")); -% when /\bSTACK_ADDR_FROM_TOP\((?[^)]+)\)/ -% # #define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n)) -% num = Regexp.last_match[:num] - fprintf(f, <%= to_cstr.call(line.sub(/\bSTACK_ADDR_FROM_TOP\(([^)]+)\)/, '%s')) %>, - (status->local_stack_p ? "stack + (stack_size - (<%= num %>))" : "STACK_ADDR_FROM_TOP(<%= num %>)")); -% when /\bTOPN\((?[^)]+)\)/ -% # #define TOPN(n) (*(GET_SP()-(n)-1)) -% num = Regexp.last_match[:num] - fprintf(f, <%= to_cstr.call(line.sub(/\bTOPN\(([^)]+)\)/, '%s')) %>, - (status->local_stack_p ? "*(stack + (stack_size - (<%= num %>) - 1))" : "TOPN(<%= num %>)")); -% else - fprintf(f, <%= to_cstr.call(line) %>); -% end -% end % end % end diff --git a/tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb b/tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb index 26037e1191..1230ca508e 100644 --- a/tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb +++ b/tool/ruby_vm/views/_mjit_compile_pc_and_sp.erb @@ -8,33 +8,19 @@ % # JIT: Move pc so that catch table lookup condition is met. If the ISeq might not catch an exception, % # the pc motion is optimized away and thus pc should be set properly before `goto cancel`. if (body->catch_except_p) { -% if insn.handles_frame? fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", next_pos); /* ADD_PC(INSN_ATTR(width)); */ -% else - fprintf(f, " reg_cfp->pc = original_body_iseq + %d;\n", pos); -% end } % % # JIT: move sp to use or preserve stack variables if (status->local_stack_p) { -% # sp motion is optimized away for `handles_frame? #=> false` case. -% # Thus sp should be set properly before `goto cancel`. -% if insn.handles_frame? % # JIT-only behavior (pushing JIT's local variables to VM's stack): - { rb_snum_t i, push_size; push_size = -<%= insn.call_attribute('sp_inc') %> + <%= insn.rets.size %> - <%= insn.pops.size %>; fprintf(f, " reg_cfp->sp = (VALUE *)reg_cfp->bp + %ld + 1;\n", push_size); /* POPN(INSN_ATTR(popn)); */ for (i = 0; i < push_size; i++) { /* TODO: use memcpy? */ fprintf(f, " *(reg_cfp->sp + %ld) = stack[%ld];\n", i - push_size, (rb_snum_t)b->stack_size - push_size + i); } - } -% end } else { -% if insn.handles_frame? fprintf(f, " reg_cfp->sp = (VALUE *)reg_cfp->bp + %d;\n", b->stack_size + 1 - <%= insn.pops.size %>); /* POPN(INSN_ATTR(popn)); */ -% else - fprintf(f, " reg_cfp->sp = (VALUE *)reg_cfp->bp + %d;\n", b->stack_size + 1); -% end } diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 3fac8eb1fa..07c22b03b3 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1237,11 +1237,11 @@ vm_throw(const rb_execution_context_t *ec, rb_control_frame_t *reg_cfp, } static inline void -vm_expandarray(VALUE *sp, VALUE ary, rb_num_t num, int flag) +vm_expandarray(rb_control_frame_t *cfp, VALUE ary, rb_num_t num, int flag) { int is_splat = flag & 0x01; rb_num_t space_size = num + is_splat; - VALUE *base = sp - 1; + VALUE *base = cfp->sp; const VALUE *ptr; rb_num_t len; const VALUE obj = ary; @@ -1256,6 +1256,8 @@ vm_expandarray(VALUE *sp, VALUE ary, rb_num_t num, int flag) len = (rb_num_t)RARRAY_LEN(ary); } + cfp->sp += space_size; + if (flag & 0x02) { /* post: ..., nil ,ary[-1], ..., ary[0..-num] # top */ rb_num_t i = 0, j; diff --git a/vm_insnhelper.h b/vm_insnhelper.h index 25bd92f6fd..18e6139a03 100644 --- a/vm_insnhelper.h +++ b/vm_insnhelper.h @@ -111,8 +111,6 @@ enum vm_regan_acttype { #define INC_SP(x) (VM_REG_SP += (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x)))) #define DEC_SP(x) (VM_REG_SP -= (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x)))) #define SET_SV(x) (*GET_SP() = (x)) - /* set current stack value as x */ -#define ADJ_SP(x) INC_SP(x) /* instruction sequence C struct */ #define GET_ISEQ() (GET_CFP()->iseq) @@ -192,6 +190,9 @@ enum vm_regan_acttype { #define GET_BLOCK_HANDLER() (GET_LEP()[VM_ENV_DATA_INDEX_SPECVAL]) +#define WIDTH_OF_opt_send_without_block \ + ((rb_snum_t)attr_width_opt_send_without_block(0, 0)) + /**********************************************************/ /* deal with control flow 3: exception */ /**********************************************************/