From ce55b4c0e0062cd59eb3d5e05ffc5d75bdcde85a Mon Sep 17 00:00:00 2001 From: ko1 Date: Sat, 6 Jan 2007 00:24:59 +0000 Subject: * insns.def : support direct method dispatch with "send" or "funcall". This means that "obj.send :m" skips "BasicObject#send" invocation (method frame creation, etc) and "obj.m" invokes directly. If you make backtrace, there are no enties of "send" method. * compile.c (iseq_specialized_instruction) : fix to support above * eval.c : ditto (remove "static" from rb_f_send and rb_f_funcall * yarvcore.c : ditto (add a external IDs for compiler) * yarvcore.h : ditto (add a VM_CALL_SEND_BIT macro) * yarvtest/test_method.rb : add tests for above changes * eval.c : remove unused "Kernel#send" declaration git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11488 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 19 ++++++++++++++++ compile.c | 5 +++++ eval.c | 7 +++--- insns.def | 28 ++++++++++++++++++++++- yarvcore.c | 11 +++++++++ yarvcore.h | 21 ++++++++++++------ yarvtest/test_method.rb | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 138 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1c5ecf3f06..22596e6382 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +Sat Jan 6 09:10:52 2007 Koichi Sasada + + * insns.def : support direct method dispatch with "send" or "funcall". + This means that "obj.send :m" skips "BasicObject#send" invocation + (method frame creation, etc) and "obj.m" invokes directly. + If you make backtrace, there are no enties of "send" method. + + * compile.c (iseq_specialized_instruction) : fix to support above + + * eval.c : ditto (remove "static" from rb_f_send and rb_f_funcall + + * yarvcore.c : ditto (add a external IDs for compiler) + + * yarvcore.h : ditto (add a VM_CALL_SEND_BIT macro) + + * yarvtest/test_method.rb : add tests for above changes + + * eval.c : remove unused "Kernel#send" declaration + Sat Jan 6 08:29:17 2007 Masaki Suketa * ext/win32ole/win32ole.c (Init_win32ole): add diff --git a/compile.c b/compile.c index 198a4c21d2..0233bd2368 100644 --- a/compile.c +++ b/compile.c @@ -1671,6 +1671,11 @@ iseq_specialized_instruction(yarv_iseq_t *iseq, INSN *iobj) } } } + + if (mid == idSend || mid == id__send__ || mid == id__send || + mid == idFuncall || mid == id__send_bang) { + OPERAND_AT(iobj, 3) |= INT2FIX(VM_CALL_SEND_BIT); + } } return COMPILE_OK; } diff --git a/eval.c b/eval.c index 07034794ae..b1cc31bca3 100644 --- a/eval.c +++ b/eval.c @@ -1709,7 +1709,7 @@ send_funcall(int argc, VALUE *argv, VALUE recv, int scope) * k.send :hello, "gentle", "readers" #=> "Hello gentle readers" */ -static VALUE +VALUE rb_f_send(int argc, VALUE *argv, VALUE recv) { int scope = NOEX_PUBLIC; @@ -1736,7 +1736,7 @@ rb_f_send(int argc, VALUE *argv, VALUE recv) * 1.funcall(:puts, "hello") # prints "foo" */ -static VALUE +VALUE rb_f_funcall(int argc, VALUE *argv, VALUE recv) { return send_funcall(argc, argv, recv, NOEX_NOSUPER | NOEX_PRIVATE); @@ -2894,13 +2894,12 @@ Init_eval() rb_define_global_function("global_variables", rb_f_global_variables, 0); /* in variable.c */ rb_define_global_function("local_variables", rb_f_local_variables, 0); - rb_define_method(rb_mKernel, "send", rb_f_send, -1); - rb_define_method(rb_cBasicObject, "send", rb_f_send, -1); rb_define_method(rb_cBasicObject, "__send__", rb_f_send, -1); rb_define_method(rb_cBasicObject, "__send", rb_f_send, -1); rb_define_method(rb_cBasicObject, "funcall", rb_f_funcall, -1); rb_define_method(rb_cBasicObject, "__send!", rb_f_funcall, -1); + rb_define_method(rb_mKernel, "instance_eval", rb_obj_instance_eval, -1); rb_define_method(rb_mKernel, "instance_exec", rb_obj_instance_exec, -1); diff --git a/insns.def b/insns.def index 201e0db937..0e5ec71185 100644 --- a/insns.def +++ b/insns.def @@ -1146,7 +1146,7 @@ defineclass */ DEFINE_INSN send -(ID id, num_t op_argc, ISEQ blockiseq, num_t op_flag, IC ic) +(ID op_id, num_t op_argc, ISEQ blockiseq, num_t op_flag, IC ic) (...) (VALUE val) // inc += - (op_argc + ((op_flag & VM_CALL_ARGS_BLOCKARG_BIT) ? 1 : 0)); { @@ -1156,6 +1156,7 @@ send yarv_block_t *blockptr = 0; num_t num = op_argc; num_t flag = op_flag; + ID id = op_id; macro_eval_setup_send_arguments(num, blockptr, flag, blockiseq); @@ -1172,6 +1173,31 @@ send mn = eval_method_search(id, klass, ic); + if ((flag & VM_CALL_SEND_BIT) && mn && nd_type(mn->nd_body) == NODE_CFUNC) { + NODE *node = mn->nd_body; + extern VALUE rb_f_funcall(int argc, VALUE *argv, VALUE recv); + extern VALUE rb_f_send(int argc, VALUE *argv, VALUE recv); + + if (node->nd_cfnc == rb_f_funcall || node->nd_cfnc == rb_f_send) { + int i; + id = rb_to_id(TOPN(num - 1)); + + /* shift arguments */ + for (i=1; ind_cfnc == rb_f_funcall) { + flag |= VM_CALL_FCALL_BIT; + } + } + #if CURRENT_INSN_send || CURRENT_INSN_send_SC_xx_ax #if !YARV_AOT_COMPILED if (0) { diff --git a/yarvcore.c b/yarvcore.c index d6b59c527d..3b510495e6 100644 --- a/yarvcore.c +++ b/yarvcore.c @@ -59,6 +59,11 @@ ID idEnd; ID idBitblt; ID idAnswer; ID idSvarPlaceholder; +ID idSend; +ID id__send__; +ID id__send; +ID idFuncall; +ID id__send_bang; unsigned long yarvGlobalStateVersion = 1; @@ -996,6 +1001,12 @@ Init_yarvcore(void) idAnswer = rb_intern("the_answer_to_life_the_universe_and_everything"); idSvarPlaceholder = rb_intern("#svar"); + idSend = rb_intern("send"); + id__send__ = rb_intern("__send__"); + id__send = rb_intern("__send"); + idFuncall = rb_intern("funcall"); + id__send_bang = rb_intern("__send!"); + #if TEST_AOT_COMPILE Init_compiled(); #endif diff --git a/yarvcore.h b/yarvcore.h index 5da48a95c1..dcb008db75 100644 --- a/yarvcore.h +++ b/yarvcore.h @@ -143,6 +143,12 @@ extern ID idEnd; extern ID idBitblt; extern ID idAnswer; extern ID idSvarPlaceholder; +extern ID idSend; +extern ID id__send__; +extern ID id__send; +extern ID idFuncall; +extern ID id__send_bang; + extern unsigned long yarvGlobalStateVersion; @@ -534,13 +540,14 @@ typedef struct { /* used by compile time and send insn */ -#define VM_CALL_ARGS_SPLAT_BIT 0x01 -#define VM_CALL_ARGS_BLOCKARG_BIT 0x02 -#define VM_CALL_FCALL_BIT 0x04 -#define VM_CALL_VCALL_BIT 0x08 -#define VM_CALL_TAILCALL_BIT 0x10 -#define VM_CALL_TAILRECURSION_BIT 0x20 -#define VM_CALL_SUPER_BIT 0x40 +#define VM_CALL_ARGS_SPLAT_BIT (0x01 << 1) +#define VM_CALL_ARGS_BLOCKARG_BIT (0x01 << 2) +#define VM_CALL_FCALL_BIT (0x01 << 3) +#define VM_CALL_VCALL_BIT (0x01 << 4) +#define VM_CALL_TAILCALL_BIT (0x01 << 5) +#define VM_CALL_TAILRECURSION_BIT (0x01 << 6) +#define VM_CALL_SUPER_BIT (0x01 << 7) +#define VM_CALL_SEND_BIT (0x01 << 8) /* inline method cache */ #define NEW_INLINE_CACHE_ENTRY() NEW_WHILE(Qundef, 0, 0) diff --git a/yarvtest/test_method.rb b/yarvtest/test_method.rb index c2ef4c99de..60e7a9dd4e 100644 --- a/yarvtest/test_method.rb +++ b/yarvtest/test_method.rb @@ -535,5 +535,64 @@ class TestMethod < YarvTestBase C.new.m } end + + def test_send + ae %q{ + $r = [] + class C + def m *args + $r << "C#m #{args.inspect} #{block_given?}" + end + end + + obj = C.new + obj.send :m + obj.send :m, :x + obj.send :m, :x, :y + obj.send(:m){} + obj.send(:m, :x){} + $r + } + end + + def test_send_with_private + ae %q{ + begin + def m + end + self.send :m + rescue NoMethodError + :ok + else + :ng + end + } + ae %q{ + begin + def m + end + send :m + rescue NoMethodError + :ng + else + :ok + end + } + end + + def test_funcall + ae %q{ + $r = [] + def m *args + $r << "m() #{args.inspect} #{block_given?}" + end + + funcall :m + funcall :m, :x + funcall :m, :x, :y + funcall(:m){} + funcall(:m, :x){} + } + end end -- cgit v1.2.3