diff --git a/opt_insn_unif.def b/opt_insn_unif.def index c81d72cdb1..1ac613dbea 100644 --- a/opt_insn_unif.def +++ b/opt_insn_unif.def @@ -1,29 +1,29 @@ -# -# a definition of instruction unification -# -# - -__END__ - -putobject putobject -putobject putstring -putobject setlocal -putobject setdynamic - -putstring putstring -putstring putobject -putstring setlocal -putstring setdynamic - -# putnil end - -dup setlocal - -# from tarai -getlocal getlocal -# getlocal send - -# from tak, ackermann -getlocal putobject - - +# +# a definition of instruction unification +# +# + +__END__ + +putobject putobject +putobject putstring +putobject setlocal +putobject setdynamic + +putstring putstring +putstring putobject +putstring setlocal +putstring setdynamic + +# putnil end + +dup setlocal + +# from tarai +getlocal getlocal +# getlocal send + +# from tak, ackermann +getlocal putobject + + diff --git a/opt_operand.def b/opt_operand.def index fc265cb3cf..5ca1d74028 100644 --- a/opt_operand.def +++ b/opt_operand.def @@ -1,59 +1,59 @@ -# -# configration file for operand union optimization -# -# format: -# [insn name] op1, op2 ... -# -# wildcard: * -# - -__END__ - -getlocal 2 -getlocal 3 -getlocal 4 - -setlocal 2 -setlocal 3 -setlocal 4 - -getdynamic *, 0 -getdynamic 1, 0 -getdynamic 2, 0 -getdynamic 3, 0 -getdynamic 4, 0 - -setdynamic *, 0 -setdynamic 1, 0 -setdynamic 2, 0 -setdynamic 3, 0 -setdynamic 4, 0 - -putobject INT2FIX(0) -putobject INT2FIX(1) -putobject Qtrue -putobject Qfalse - -# CALL -send *, *, Qfalse, 0, * -send *, 0, Qfalse, 0, * -send *, 1, Qfalse, 0, * -send *, 2, Qfalse, 0, * -send *, 3, Qfalse, 0, * - -# FCALL -send *, *, Qfalse, 0x04, * -send *, 0, Qfalse, 0x04, * -send *, 1, Qfalse, 0x04, * -send *, 2, Qfalse, 0x04, * -send *, 3, Qfalse, 0x04, * - -# VCALL -send *, 0, Qfalse, 0x0c, * - - -__END__ - - - - +# +# configration file for operand union optimization +# +# format: +# [insn name] op1, op2 ... +# +# wildcard: * +# + +__END__ + +getlocal 2 +getlocal 3 +getlocal 4 + +setlocal 2 +setlocal 3 +setlocal 4 + +getdynamic *, 0 +getdynamic 1, 0 +getdynamic 2, 0 +getdynamic 3, 0 +getdynamic 4, 0 + +setdynamic *, 0 +setdynamic 1, 0 +setdynamic 2, 0 +setdynamic 3, 0 +setdynamic 4, 0 + +putobject INT2FIX(0) +putobject INT2FIX(1) +putobject Qtrue +putobject Qfalse + +# CALL +send *, *, Qfalse, 0, * +send *, 0, Qfalse, 0, * +send *, 1, Qfalse, 0, * +send *, 2, Qfalse, 0, * +send *, 3, Qfalse, 0, * + +# FCALL +send *, *, Qfalse, 0x04, * +send *, 0, Qfalse, 0x04, * +send *, 1, Qfalse, 0x04, * +send *, 2, Qfalse, 0x04, * +send *, 3, Qfalse, 0x04, * + +# VCALL +send *, 0, Qfalse, 0x0c, * + + +__END__ + + + + diff --git a/proc.c b/proc.c index f0cd3f3733..b23e159f4c 100644 --- a/proc.c +++ b/proc.c @@ -1,1610 +1,1610 @@ -/********************************************************************** - - proc.c - Proc, Bindng, Env - - $Author: ko1 $ - $Date: $ - created at: Wed Jan 17 12:13:14 2007 - - Copyright (C) 2004-2007 Koichi Sasada - -**********************************************************************/ - -#include "eval_intern.h" -#include "gc.h" - -struct METHOD { - VALUE klass, rklass; - VALUE recv; - ID id, oid; - NODE *body; -}; - -VALUE rb_cUnboundMethod; -VALUE rb_cMethod; -VALUE rb_cBinding; -VALUE rb_cProc; -VALUE rb_cEnv; - -static VALUE bmcall(VALUE, VALUE); -static int method_arity(VALUE); -static VALUE rb_obj_is_method(VALUE m); - -/* Env */ - -static void -env_free(void *ptr) -{ - yarv_env_t *env; - FREE_REPORT_ENTER("env"); - if (ptr) { - env = ptr; - FREE_UNLESS_NULL(env->env); - ruby_xfree(ptr); - } - FREE_REPORT_LEAVE("env"); -} - -static void -env_mark(void *ptr) -{ - yarv_env_t *env; - MARK_REPORT_ENTER("env"); - if (ptr) { - env = ptr; - if (env->env) { - /* TODO: should mark more restricted range */ - GC_INFO("env->env\n"); - rb_gc_mark_locations(env->env, env->env + env->env_size); - } - GC_INFO("env->prev_envval\n"); - MARK_UNLESS_NULL(env->prev_envval); - - if (env->block.iseq) { - if (BUILTIN_TYPE(env->block.iseq) == T_NODE) { - MARK_UNLESS_NULL((VALUE)env->block.iseq); - } - else { - MARK_UNLESS_NULL(env->block.iseq->self); - } - } - } - MARK_REPORT_LEAVE("env"); -} - -VALUE -yarv_env_alloc(void) -{ - VALUE obj; - yarv_env_t *env; - obj = Data_Make_Struct(rb_cEnv, yarv_env_t, env_mark, env_free, env); - env->env = 0; - env->prev_envval = 0; - env->block.iseq = 0; - return obj; -} - -/* Proc */ - -static void -proc_free(void *ptr) -{ - FREE_REPORT_ENTER("proc"); - if (ptr) { - ruby_xfree(ptr); - } - FREE_REPORT_LEAVE("proc"); -} - -static void -proc_mark(void *ptr) -{ - yarv_proc_t *proc; - MARK_REPORT_ENTER("proc"); - if (ptr) { - proc = ptr; - MARK_UNLESS_NULL(proc->envval); - MARK_UNLESS_NULL(proc->blockprocval); - MARK_UNLESS_NULL((VALUE)proc->special_cref_stack); - if (proc->block.iseq && YARV_IFUNC_P(proc->block.iseq)) { - MARK_UNLESS_NULL((VALUE)(proc->block.iseq)); - } - } - MARK_REPORT_LEAVE("proc"); -} - -static VALUE -proc_alloc(VALUE klass) -{ - VALUE obj; - yarv_proc_t *proc; - obj = Data_Make_Struct(klass, yarv_proc_t, proc_mark, proc_free, proc); - MEMZERO(proc, yarv_proc_t, 1); - return obj; -} - -VALUE -yarv_proc_alloc(void) -{ - proc_alloc(rb_cProc); -} - -VALUE -yarv_obj_is_proc(VALUE proc) -{ - if (TYPE(proc) == T_DATA && - RDATA(proc)->dfree == (RUBY_DATA_FUNC) proc_free) { - return Qtrue; - } - else { - return Qfalse; - } -} - -static VALUE -proc_dup(VALUE self) -{ - VALUE procval = proc_alloc(rb_cProc); - yarv_proc_t *src, *dst; - GetProcPtr(self, src); - GetProcPtr(procval, dst); - - dst->block = src->block; - dst->envval = src->envval; - dst->safe_level = dst->safe_level; - dst->special_cref_stack = src->special_cref_stack; - - return procval; -} - -VALUE yarv_proc_dup(VALUE self) -{ - return proc_dup(self); -} - -static VALUE -proc_clone(VALUE self) -{ - VALUE procval = proc_dup(self); - CLONESETUP(procval, self); - return procval; -} - -/* Binding */ - -static void -binding_free(void *ptr) -{ - yarv_binding_t *bind; - FREE_REPORT_ENTER("binding"); - if (ptr) { - bind = ptr; - ruby_xfree(ptr); - } - FREE_REPORT_LEAVE("binding"); -} - -static void -binding_mark(void *ptr) -{ - yarv_binding_t *bind; - MARK_REPORT_ENTER("binding"); - if (ptr) { - bind = ptr; - MARK_UNLESS_NULL(bind->env); - MARK_UNLESS_NULL((VALUE)bind->cref_stack); - } - MARK_REPORT_LEAVE("binding"); -} - -static VALUE -binding_alloc(VALUE klass) -{ - VALUE obj; - yarv_binding_t *bind; - obj = Data_Make_Struct(klass, yarv_binding_t, - binding_mark, binding_free, bind); - MEMZERO(bind, yarv_binding_t, 1); - return obj; -} - -static VALUE -binding_dup(VALUE self) -{ - VALUE bindval = binding_alloc(rb_cBinding); - yarv_binding_t *src, *dst; - GetBindingPtr(self, src); - GetBindingPtr(bindval, dst); - dst->env = src->env; - dst->cref_stack = src->cref_stack; - return bindval; -} - -static VALUE -binding_clone(VALUE self) -{ - VALUE bindval = binding_dup(self); - CLONESETUP(bindval, self); - return bindval; -} - -VALUE -rb_binding_new(void) -{ - yarv_thread_t *th = GET_THREAD(); - yarv_control_frame_t *cfp = th_get_ruby_level_cfp(th, th->cfp); - VALUE bindval = binding_alloc(rb_cBinding); - yarv_binding_t *bind; - - GetBindingPtr(bindval, bind); - bind->env = th_make_env_object(th, cfp); - bind->cref_stack = ruby_cref(); - return bindval; -} - -/* - * call-seq: - * binding -> a_binding - * - * Returns a +Binding+ object, describing the variable and - * method bindings at the point of call. This object can be used when - * calling +eval+ to execute the evaluated command in this - * environment. Also see the description of class +Binding+. - * - * def getBinding(param) - * return binding - * end - * b = getBinding("hello") - * eval("param", b) #=> "hello" - */ - -static VALUE -rb_f_binding(VALUE self) -{ - return rb_binding_new(); -} - -/* - * call-seq: - * binding.eval(string [, filename [,lineno]]) => obj - * - * Evaluates the Ruby expression(s) in string, in the - * binding's context. If the optional filename and - * lineno parameters are present, they will be used when - * reporting syntax errors. - * - * def getBinding(param) - * return binding - * end - * b = getBinding("hello") - * b.eval("param") #=> "hello" - */ - -static VALUE -bind_eval(int argc, VALUE *argv, VALUE bind) -{ - UNSUPPORTED(bind_eval); - return Qnil; -} - -#define PROC_TSHIFT (FL_USHIFT+1) -#define PROC_TMASK (FL_USER1|FL_USER2|FL_USER3) -#define PROC_TMAX (PROC_TMASK >> PROC_TSHIFT) -#define PROC_NOSAFE FL_USER4 - -#define SAFE_LEVEL_MAX PROC_TMASK - -static VALUE -proc_new(VALUE klass, int is_lambda) -{ - VALUE procval = Qnil; - yarv_thread_t *th = GET_THREAD(); - yarv_control_frame_t *cfp = th->cfp; - yarv_block_t *block; - - if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0 && - !YARV_CLASS_SPECIAL_P(cfp->lfp[0])) { - block = GC_GUARDED_PTR_REF(cfp->lfp[0]); - } - else { - cfp = YARV_PREVIOUS_CONTROL_FRAME(cfp); - if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0 && - !YARV_CLASS_SPECIAL_P(cfp->lfp[0])) { - block = GC_GUARDED_PTR_REF(cfp->lfp[0]); - - if (is_lambda) { - rb_warn("tried to create Proc object without a block"); - } - } - else { - rb_raise(rb_eArgError, - "tried to create Proc object without a block"); - } - } - - cfp = YARV_PREVIOUS_CONTROL_FRAME(cfp); - procval = th_make_proc(th, cfp, block); - - if (is_lambda) { - yarv_proc_t *proc; - GetProcPtr(procval, proc); - proc->is_lambda = Qtrue; - } - return procval; -} - -/* - * call-seq: - * Proc.new {|...| block } => a_proc - * Proc.new => a_proc - * - * Creates a new Proc object, bound to the current - * context. Proc::new may be called without a block only - * within a method with an attached block, in which case that block is - * converted to the Proc object. - * - * def proc_from - * Proc.new - * end - * proc = proc_from { "hello" } - * proc.call #=> "hello" - */ - -static VALUE -rb_proc_s_new(VALUE klass) -{ - return proc_new(klass, Qfalse); -} - -/* - * call-seq: - * proc { |...| block } => a_proc - * - * Equivalent to Proc.new. - */ - -VALUE -rb_block_proc(void) -{ - return proc_new(rb_cProc, Qfalse); -} - -VALUE -rb_block_lambda(void) -{ - return proc_new(rb_cProc, Qtrue); -} - -VALUE -rb_f_lambda(void) -{ - rb_warn("rb_f_lambda() is deprecated; use rb_block_proc() instead"); - return rb_block_lambda(); -} - -/* - * call-seq: - * lambda { |...| block } => a_proc - * - * Equivalent to Proc.new, except the resulting Proc objects - * check the number of parameters passed when called. - */ - -static VALUE -proc_lambda(void) -{ - return rb_block_lambda(); -} - -VALUE -proc_invoke(VALUE self, VALUE args, VALUE alt_self, VALUE alt_klass) -{ - yarv_proc_t *proc; - GetProcPtr(self, proc); - - /* ignore self and klass */ - return th_invoke_proc(GET_THREAD(), proc, proc->block.self, - RARRAY_LEN(args), RARRAY_PTR(args)); -} - -/* CHECKME: are the argument checking semantics correct? */ - -/* - * call-seq: - * prc.call(params,...) => obj - * prc[params,...] => obj - * - * Invokes the block, setting the block's parameters to the values in - * params using something close to method calling semantics. - * Generates a warning if multiple values are passed to a proc that - * expects just one (previously this silently converted the parameters - * to an array). - * - * For procs created using Kernel.proc, generates an - * error if the wrong number of parameters - * are passed to a proc with multiple parameters. For procs created using - * Proc.new, extra parameters are silently discarded. - * - * Returns the value of the last expression evaluated in the block. See - * also Proc#yield. - * - * a_proc = Proc.new {|a, *b| b.collect {|i| i*a }} - * a_proc.call(9, 1, 2, 3) #=> [9, 18, 27] - * a_proc[9, 1, 2, 3] #=> [9, 18, 27] - * a_proc = Proc.new {|a,b| a} - * a_proc.call(1,2,3) - * - * produces: - * - * prog.rb:5: wrong number of arguments (3 for 2) (ArgumentError) - * from prog.rb:4:in `call' - * from prog.rb:5 - */ - -static VALUE -proc_call(int argc, VALUE *argv, VALUE procval) -{ - yarv_proc_t *proc; - GetProcPtr(procval, proc); - return th_invoke_proc(GET_THREAD(), proc, proc->block.self, argc, argv); -} - -static VALUE -proc_yield(int argc, VALUE *argv, VALUE procval) -{ - yarv_proc_t *proc; - GetProcPtr(procval, proc); - return th_invoke_proc(GET_THREAD(), proc, proc->block.self, argc, argv); -} - -VALUE -rb_proc_call(VALUE proc, VALUE args) -{ - return proc_invoke(proc, args, Qundef, 0); -} - -/* - * call-seq: - * prc.arity -> fixnum - * - * Returns the number of arguments that would not be ignored. If the block - * is declared to take no arguments, returns 0. If the block is known - * to take exactly n arguments, returns n. If the block has optional - * arguments, return -n-1, where n is the number of mandatory - * arguments. A proc with no argument declarations - * is the same a block declaring || as its arguments. - * - * Proc.new {}.arity #=> 0 - * Proc.new {||}.arity #=> 0 - * Proc.new {|a|}.arity #=> 1 - * Proc.new {|a,b|}.arity #=> 2 - * Proc.new {|a,b,c|}.arity #=> 3 - * Proc.new {|*a|}.arity #=> -1 - * Proc.new {|a,*b|}.arity #=> -2 - */ - -static VALUE -proc_arity(VALUE self) -{ - yarv_proc_t *proc; - yarv_iseq_t *iseq; - GetProcPtr(self, proc); - iseq = proc->block.iseq; - if (iseq && BUILTIN_TYPE(iseq) != T_NODE) { - if (iseq->arg_rest == 0 && iseq->arg_opts == 0) { - return INT2FIX(iseq->argc); - } - else { - return INT2FIX(-iseq->argc - 1); - } - } - else { - return INT2FIX(-1); - } -} - -int -rb_proc_arity(VALUE proc) -{ - return FIX2INT(proc_arity(proc)); -} - -/* - * call-seq: - * prc == other_proc => true or false - * - * Return true if prc is the same object as - * other_proc, or if they are both procs with the same body. - */ - -static VALUE -proc_eq(VALUE self, VALUE other) -{ - if (self == other) { - return Qtrue; - } - else { - if (TYPE(other) == T_DATA && - RBASIC(other)->klass == rb_cProc && - CLASS_OF(self) == CLASS_OF(other)) { - yarv_proc_t *p1, *p2; - GetProcPtr(self, p1); - GetProcPtr(other, p2); - if (p1->block.iseq == p2->block.iseq && p1->envval == p2->envval) { - return Qtrue; - } - } - } - return Qfalse; -} - -/* - * call-seq: - * prc.hash => integer - * - * Return hash value corresponding to proc body. - */ - -static VALUE -proc_hash(VALUE self) -{ - int hash; - yarv_proc_t *proc; - GetProcPtr(self, proc); - hash = (long)proc->block.iseq; - hash ^= (long)proc->envval; - hash ^= (long)proc->block.lfp >> 16; - return INT2FIX(hash); -} - -/* - * call-seq: - * prc.to_s => string - * - * Shows the unique identifier for this proc, along with - * an indication of where the proc was defined. - */ - -static VALUE -proc_to_s(VALUE self) -{ - VALUE str = 0; - yarv_proc_t *proc; - char *cname = rb_obj_classname(self); - yarv_iseq_t *iseq; - - GetProcPtr(self, proc); - iseq = proc->block.iseq; - - if (YARV_NORMAL_ISEQ_P(iseq)) { - int line_no = 0; - - if (iseq->insn_info_tbl) { - line_no = iseq->insn_info_tbl[0].line_no; - } - str = rb_sprintf("#<%s:%lx@%s:%d>", cname, self, - RSTRING_PTR(iseq->file_name), - line_no); - } - else { - str = rb_sprintf("#<%s:%p>", cname, proc->block.iseq); - } - - if (OBJ_TAINTED(self)) { - OBJ_TAINT(str); - } - return str; -} - -/* - * call-seq: - * prc.to_proc -> prc - * - * Part of the protocol for converting objects to Proc - * objects. Instances of class Proc simply return - * themselves. - */ - -static VALUE -proc_to_proc(VALUE self) -{ - return self; -} - -/* - * call-seq: - * prc.binding => binding - * - * Returns the binding associated with prc. Note that - * Kernel#eval accepts either a Proc or a - * Binding object as its second parameter. - * - * def fred(param) - * proc {} - * end - * - * b = fred(99) - * eval("param", b.binding) #=> 99 - * eval("param", b) #=> 99 - */ - -void -bm_mark(struct METHOD *data) -{ - rb_gc_mark(data->rklass); - rb_gc_mark(data->klass); - rb_gc_mark(data->recv); - rb_gc_mark((VALUE)data->body); -} - -NODE *rb_get_method_body(VALUE klass, ID id, ID *idp); - -static VALUE -mnew(VALUE klass, VALUE obj, ID id, VALUE mklass) -{ - VALUE method; - NODE *body; - struct METHOD *data; - VALUE rklass = klass; - ID oid = id; - - again: - if ((body = rb_get_method_body(klass, id, 0)) == 0) { - print_undef(rklass, oid); - } - - klass = body->nd_clss; - body = body->nd_body; - - if (nd_type(body) == NODE_ZSUPER) { - klass = RCLASS(klass)->super; - goto again; - } - - while (rklass != klass && - (FL_TEST(rklass, FL_SINGLETON) || TYPE(rklass) == T_ICLASS)) { - rklass = RCLASS(rklass)->super; - } - if (TYPE(klass) == T_ICLASS) - klass = RBASIC(klass)->klass; - method = Data_Make_Struct(mklass, struct METHOD, bm_mark, -1, data); - data->klass = klass; - data->recv = obj; - - data->id = id; - data->body = body; - data->rklass = rklass; - data->oid = oid; - OBJ_INFECT(method, klass); - - return method; -} - - -/********************************************************************** - * - * Document-class : Method - * - * Method objects are created by Object#method, and are - * associated with a particular object (not just with a class). They - * may be used to invoke the method within the object, and as a block - * associated with an iterator. They may also be unbound from one - * object (creating an UnboundMethod) and bound to - * another. - * - * class Thing - * def square(n) - * n*n - * end - * end - * thing = Thing.new - * meth = thing.method(:square) - * - * meth.call(9) #=> 81 - * [ 1, 2, 3 ].collect(&meth) #=> [1, 4, 9] - * - */ - -/* - * call-seq: - * meth == other_meth => true or false - * - * Two method objects are equal if that are bound to the same - * object and contain the same body. - */ - - -static VALUE -method_eq(method, other) - VALUE method, other; -{ - struct METHOD *m1, *m2; - - if (TYPE(other) != T_DATA - || RDATA(other)->dmark != (RUBY_DATA_FUNC) bm_mark) - return Qfalse; - if (CLASS_OF(method) != CLASS_OF(other)) - return Qfalse; - - Data_Get_Struct(method, struct METHOD, m1); - Data_Get_Struct(other, struct METHOD, m2); - - if (m1->klass != m2->klass || m1->rklass != m2->rklass || - m1->recv != m2->recv || m1->body != m2->body) - return Qfalse; - - return Qtrue; -} - -/* - * call-seq: - * meth.hash => integer - * - * Return a hash value corresponding to the method object. - */ - -static VALUE -method_hash(method) - VALUE method; -{ - struct METHOD *m; - long hash; - - Data_Get_Struct(method, struct METHOD, m); - hash = (long)m->klass; - hash ^= (long)m->rklass; - hash ^= (long)m->recv; - hash ^= (long)m->body; - - return INT2FIX(hash); -} - -/* - * call-seq: - * meth.unbind => unbound_method - * - * Dissociates meth from it's current receiver. The resulting - * UnboundMethod can subsequently be bound to a new object - * of the same class (see UnboundMethod). - */ - -static VALUE -method_unbind(obj) - VALUE obj; -{ - VALUE method; - struct METHOD *orig, *data; - - Data_Get_Struct(obj, struct METHOD, orig); - method = - Data_Make_Struct(rb_cUnboundMethod, struct METHOD, bm_mark, free, - data); - data->klass = orig->klass; - data->recv = Qundef; - data->id = orig->id; - data->body = orig->body; - data->rklass = orig->rklass; - data->oid = orig->oid; - OBJ_INFECT(method, obj); - - return method; -} - -/* - * call-seq: - * meth.receiver => object - * - * Returns the bound receiver of the method object. - */ - -static VALUE -method_receiver(VALUE obj) -{ - struct METHOD *data; - - Data_Get_Struct(obj, struct METHOD, data); - return data->recv; -} - -/* - * call-seq: - * meth.name => string - * - * Returns the name of the method. - */ - -static VALUE -method_name(VALUE obj) -{ - struct METHOD *data; - - Data_Get_Struct(obj, struct METHOD, data); - return rb_str_new2(rb_id2name(data->id)); -} - -/* - * call-seq: - * meth.owner => class_or_module - * - * Returns the class or module that defines the method. - */ - -static VALUE -method_owner(VALUE obj) -{ - struct METHOD *data; - - Data_Get_Struct(obj, struct METHOD, data); - return data->klass; -} - -/* - * call-seq: - * obj.method(sym) => method - * - * Looks up the named method as a receiver in obj, returning a - * Method object (or raising NameError). The - * Method object acts as a closure in obj's object - * instance, so instance variables and the value of self - * remain available. - * - * class Demo - * def initialize(n) - * @iv = n - * end - * def hello() - * "Hello, @iv = #{@iv}" - * end - * end - * - * k = Demo.new(99) - * m = k.method(:hello) - * m.call #=> "Hello, @iv = 99" - * - * l = Demo.new('Fred') - * m = l.method("hello") - * m.call #=> "Hello, @iv = Fred" - */ - -VALUE -rb_obj_method(obj, vid) - VALUE obj; - VALUE vid; -{ - return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod); -} - -/* - * call-seq: - * mod.instance_method(symbol) => unbound_method - * - * Returns an +UnboundMethod+ representing the given - * instance method in _mod_. - * - * class Interpreter - * def do_a() print "there, "; end - * def do_d() print "Hello "; end - * def do_e() print "!\n"; end - * def do_v() print "Dave"; end - * Dispatcher = { - * ?a => instance_method(:do_a), - * ?d => instance_method(:do_d), - * ?e => instance_method(:do_e), - * ?v => instance_method(:do_v) - * } - * def interpret(string) - * string.each_byte {|b| Dispatcher[b].bind(self).call } - * end - * end - * - * - * interpreter = Interpreter.new - * interpreter.interpret('dave') - * - * produces: - * - * Hello there, Dave! - */ - -static VALUE -rb_mod_method(mod, vid) - VALUE mod; - VALUE vid; -{ - return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod); -} - -/* - * call-seq: - * define_method(symbol, method) => new_method - * define_method(symbol) { block } => proc - * - * Defines an instance method in the receiver. The _method_ - * parameter can be a +Proc+ or +Method+ object. - * If a block is specified, it is used as the method body. This block - * is evaluated using instance_eval, a point that is - * tricky to demonstrate because define_method is private. - * (This is why we resort to the +send+ hack in this example.) - * - * class A - * def fred - * puts "In Fred" - * end - * def create_method(name, &block) - * self.class.send(:define_method, name, &block) - * end - * define_method(:wilma) { puts "Charge it!" } - * end - * class B < A - * define_method(:barney, instance_method(:fred)) - * end - * a = B.new - * a.barney - * a.wilma - * a.create_method(:betty) { p self } - * a.betty - * - * produces: - * - * In Fred - * Charge it! - * # - */ - -VALUE yarv_proc_dup(VALUE self); - -static VALUE -rb_mod_define_method(int argc, VALUE *argv, VALUE mod) -{ - ID id; - VALUE body; - NODE *node; - int noex = NOEX_PUBLIC; - - if (argc == 1) { - id = rb_to_id(argv[0]); - body = rb_block_lambda(); - } - else if (argc == 2) { - id = rb_to_id(argv[0]); - body = argv[1]; - if (!rb_obj_is_method(body) && !yarv_obj_is_proc(body)) { - rb_raise(rb_eTypeError, - "wrong argument type %s (expected Proc/Method)", - rb_obj_classname(body)); - } - } - else { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc); - } - - if (RDATA(body)->dmark == (RUBY_DATA_FUNC) bm_mark) { - struct METHOD *method = (struct METHOD *)DATA_PTR(body); - VALUE rklass = method->rklass; - if (rklass != mod) { - if (FL_TEST(rklass, FL_SINGLETON)) { - rb_raise(rb_eTypeError, - "can't bind singleton method to a different class"); - } - if (!RTEST(rb_class_inherited_p(mod, rklass))) { - rb_raise(rb_eTypeError, - "bind argument must be a subclass of %s", - rb_class2name(rklass)); - } - } - node = method->body; - } - else if (yarv_obj_is_proc(body)) { - yarv_proc_t *proc; - body = yarv_proc_dup(body); - GetProcPtr(body, proc); - if (BUILTIN_TYPE(proc->block.iseq) != T_NODE) { - proc->block.iseq->defined_method_id = id; - proc->block.iseq->klass = mod; - proc->is_lambda = Qtrue; - } - node = NEW_BMETHOD(body); - } - else { - /* type error */ - rb_raise(rb_eTypeError, "wrong argument type (expected Proc/Method)"); - } - - /* TODO: visibility */ - - rb_add_method(mod, id, node, noex); - return body; -} - - -/* - * MISSING: documentation - */ - -static VALUE -method_clone(self) - VALUE self; -{ - VALUE clone; - struct METHOD *orig, *data; - - Data_Get_Struct(self, struct METHOD, orig); - clone = - Data_Make_Struct(CLASS_OF(self), struct METHOD, bm_mark, free, data); - CLONESETUP(clone, self); - *data = *orig; - - return clone; -} - -/* - * call-seq: - * meth.call(args, ...) => obj - * meth[args, ...] => obj - * - * Invokes the meth with the specified arguments, returning the - * method's return value. - * - * m = 12.method("+") - * m.call(3) #=> 15 - * m.call(20) #=> 32 - */ - -VALUE -rb_method_call(int argc, VALUE *argv, VALUE method) -{ - VALUE result = Qnil; /* OK */ - struct METHOD *data; - int state; - volatile int safe = -1; - - Data_Get_Struct(method, struct METHOD, data); - if (data->recv == Qundef) { - rb_raise(rb_eTypeError, "can't call unbound method; bind first"); - } - PUSH_TAG(PROT_NONE); - if (OBJ_TAINTED(method)) { - safe = rb_safe_level(); - if (rb_safe_level() < 4) { - rb_set_safe_level_force(4); - } - } - if ((state = EXEC_TAG()) == 0) { - PASS_PASSED_BLOCK(); - result = th_call0(GET_THREAD(), - data->klass, data->recv, data->id, data->oid, - argc, argv, data->body, 0); - } - POP_TAG(); - if (safe >= 0) - rb_set_safe_level_force(safe); - if (state) - JUMP_TAG(state); - return result; -} - -/********************************************************************** - * - * Document-class: UnboundMethod - * - * Ruby supports two forms of objectified methods. Class - * Method is used to represent methods that are associated - * with a particular object: these method objects are bound to that - * object. Bound method objects for an object can be created using - * Object#method. - * - * Ruby also supports unbound methods; methods objects that are not - * associated with a particular object. These can be created either by - * calling Module#instance_method or by calling - * unbind on a bound method object. The result of both of - * these is an UnboundMethod object. - * - * Unbound methods can only be called after they are bound to an - * object. That object must be be a kind_of? the method's original - * class. - * - * class Square - * def area - * @side * @side - * end - * def initialize(side) - * @side = side - * end - * end - * - * area_un = Square.instance_method(:area) - * - * s = Square.new(12) - * area = area_un.bind(s) - * area.call #=> 144 - * - * Unbound methods are a reference to the method at the time it was - * objectified: subsequent changes to the underlying class will not - * affect the unbound method. - * - * class Test - * def test - * :original - * end - * end - * um = Test.instance_method(:test) - * class Test - * def test - * :modified - * end - * end - * t = Test.new - * t.test #=> :modified - * um.bind(t).call #=> :original - * - */ - -/* - * call-seq: - * umeth.bind(obj) -> method - * - * Bind umeth to obj. If Klass was the class - * from which umeth was obtained, - * obj.kind_of?(Klass) must be true. - * - * class A - * def test - * puts "In test, class = #{self.class}" - * end - * end - * class B < A - * end - * class C < B - * end - * - * - * um = B.instance_method(:test) - * bm = um.bind(C.new) - * bm.call - * bm = um.bind(B.new) - * bm.call - * bm = um.bind(A.new) - * bm.call - * - * produces: - * - * In test, class = C - * In test, class = B - * prog.rb:16:in `bind': bind argument must be an instance of B (TypeError) - * from prog.rb:16 - */ - -static VALUE -umethod_bind(method, recv) - VALUE method, recv; -{ - struct METHOD *data, *bound; - - Data_Get_Struct(method, struct METHOD, data); - if (data->rklass != CLASS_OF(recv)) { - if (FL_TEST(data->rklass, FL_SINGLETON)) { - rb_raise(rb_eTypeError, - "singleton method called for a different object"); - } - if (!rb_obj_is_kind_of(recv, data->rklass)) { - rb_raise(rb_eTypeError, "bind argument must be an instance of %s", - rb_class2name(data->rklass)); - } - } - - method = - Data_Make_Struct(rb_cMethod, struct METHOD, bm_mark, free, bound); - *bound = *data; - bound->recv = recv; - bound->rklass = CLASS_OF(recv); - - return method; -} - -int -rb_node_arity(NODE * body) -{ - int n; - - switch (nd_type(body)) { - case NODE_CFUNC: - if (body->nd_argc < 0) - return -1; - return body->nd_argc; - case NODE_ZSUPER: - return -1; - case NODE_ATTRSET: - return 1; - case NODE_IVAR: - return 0; - case NODE_BMETHOD: - return rb_proc_arity(body->nd_cval); - case NODE_SCOPE: - body = body->nd_next; /* skip NODE_SCOPE */ - if (nd_type(body) == NODE_BLOCK) - body = body->nd_head; - if (!body) - return 0; - n = body->nd_frml ? RARRAY_LEN(body->nd_frml) : 0; - if (body->nd_opt || body->nd_rest) - n = -n - 1; - return n; - case YARV_METHOD_NODE:{ - yarv_iseq_t *iseq; - GetISeqPtr((VALUE)body->nd_body, iseq); - if (iseq->arg_rest == 0 && iseq->arg_opts == 0) { - return iseq->argc; - } - else { - return -iseq->argc - 1; - } - } - default: - rb_raise(rb_eArgError, "invalid node 0x%x", nd_type(body)); - } -} - -/* - * call-seq: - * meth.arity => fixnum - * - * Returns an indication of the number of arguments accepted by a - * method. Returns a nonnegative integer for methods that take a fixed - * number of arguments. For Ruby methods that take a variable number of - * arguments, returns -n-1, where n is the number of required - * arguments. For methods written in C, returns -1 if the call takes a - * variable number of arguments. - * - * class C - * def one; end - * def two(a); end - * def three(*a); end - * def four(a, b); end - * def five(a, b, *c); end - * def six(a, b, *c, &d); end - * end - * c = C.new - * c.method(:one).arity #=> 0 - * c.method(:two).arity #=> 1 - * c.method(:three).arity #=> -1 - * c.method(:four).arity #=> 2 - * c.method(:five).arity #=> -3 - * c.method(:six).arity #=> -3 - * - * "cat".method(:size).arity #=> 0 - * "cat".method(:replace).arity #=> 1 - * "cat".method(:squeeze).arity #=> -1 - * "cat".method(:count).arity #=> -1 - */ - -static VALUE -method_arity_m(method) - VALUE method; -{ - int n = method_arity(method); - return INT2FIX(n); -} - -static int -method_arity(method) - VALUE method; -{ - struct METHOD *data; - - Data_Get_Struct(method, struct METHOD, data); - return rb_node_arity(data->body); -} - -int -rb_mod_method_arity(mod, id) - VALUE mod; - ID id; -{ - NODE *node = rb_method_node(mod, id); - return rb_node_arity(node); -} - -int -rb_obj_method_arity(obj, id) - VALUE obj; - ID id; -{ - return rb_mod_method_arity(CLASS_OF(obj), id); -} - -/* - * call-seq: - * meth.to_s => string - * meth.inspect => string - * - * Show the name of the underlying method. - * - * "cat".method(:count).inspect #=> "#" - */ - -static VALUE -method_inspect(VALUE method) -{ - struct METHOD *data; - VALUE str; - const char *s; - char *sharp = "#"; - - Data_Get_Struct(method, struct METHOD, data); - str = rb_str_buf_new2("#<"); - s = rb_obj_classname(method); - rb_str_buf_cat2(str, s); - rb_str_buf_cat2(str, ": "); - - if (FL_TEST(data->klass, FL_SINGLETON)) { - VALUE v = rb_iv_get(data->klass, "__attached__"); - - if (data->recv == Qundef) { - rb_str_buf_append(str, rb_inspect(data->klass)); - } - else if (data->recv == v) { - rb_str_buf_append(str, rb_inspect(v)); - sharp = "."; - } - else { - rb_str_buf_append(str, rb_inspect(data->recv)); - rb_str_buf_cat2(str, "("); - rb_str_buf_append(str, rb_inspect(v)); - rb_str_buf_cat2(str, ")"); - sharp = "."; - } - } - else { - rb_str_buf_cat2(str, rb_class2name(data->rklass)); - if (data->rklass != data->klass) { - rb_str_buf_cat2(str, "("); - rb_str_buf_cat2(str, rb_class2name(data->klass)); - rb_str_buf_cat2(str, ")"); - } - } - rb_str_buf_cat2(str, sharp); - rb_str_buf_cat2(str, rb_id2name(data->oid)); - rb_str_buf_cat2(str, ">"); - - return str; -} - -static VALUE -mproc(VALUE method) -{ - return rb_funcall(Qnil, rb_intern("proc"), 0); -} - -static VALUE -bmcall(VALUE args, VALUE method) -{ - volatile VALUE a; - if (CLASS_OF(args) != rb_cArray) { - args = rb_ary_new3(1, args); - } - - a = args; - return rb_method_call(RARRAY_LEN(a), RARRAY_PTR(a), method); -} - -VALUE -rb_proc_new( - VALUE (*func)(ANYARGS), /* VALUE yieldarg[, VALUE procarg] */ - VALUE val) -{ - yarv_proc_t *proc; - VALUE procval = rb_iterate((VALUE(*)(VALUE))mproc, 0, func, val); - GetProcPtr(procval, proc); - ((NODE*)proc->block.iseq)->u3.state = 1; - return procval; -} - -/* - * call-seq: - * meth.to_proc => prc - * - * Returns a Proc object corresponding to this method. - */ - -static VALUE -method_proc(VALUE method) -{ - VALUE proc; - /* - * class Method - * def to_proc - * proc{|*args| - * self.call(*args) - * } - * end - * end - */ - proc = rb_iterate((VALUE (*)(VALUE))mproc, 0, bmcall, method); - return proc; -} - -static VALUE -rb_obj_is_method(VALUE m) -{ - if (TYPE(m) == T_DATA && RDATA(m)->dmark == (RUBY_DATA_FUNC) bm_mark) { - return Qtrue; - } - return Qfalse; -} - -/* - * call_seq: - * local_jump_error.exit_value => obj - * - * Returns the exit value associated with this +LocalJumpError+. - */ -static VALUE -localjump_xvalue(VALUE exc) -{ - return rb_iv_get(exc, "@exit_value"); -} - -/* - * call-seq: - * local_jump_error.reason => symbol - * - * The reason this block was terminated: - * :break, :redo, :retry, :next, :return, or :noreason. - */ - -static VALUE -localjump_reason(VALUE exc) -{ - return rb_iv_get(exc, "@reason"); -} - - -/* - * Proc objects are blocks of code that have been bound to - * a set of local variables. Once bound, the code may be called in - * different contexts and still access those variables. - * - * def gen_times(factor) - * return Proc.new {|n| n*factor } - * end - * - * times3 = gen_times(3) - * times5 = gen_times(5) - * - * times3.call(12) #=> 36 - * times5.call(5) #=> 25 - * times3.call(times5.call(4)) #=> 60 - * - */ - -void -Init_Proc() -{ - /* Env */ - rb_cVM = rb_define_class("VM", rb_cObject); /* TODO: should be moved to suitable place */ - rb_cEnv = rb_define_class_under(rb_cVM, "Env", rb_cObject); - rb_undef_alloc_func(rb_cEnv); - - /* Proc */ - rb_cProc = rb_define_class("Proc", rb_cObject); - rb_undef_alloc_func(rb_cProc); - rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, 0); - rb_define_method(rb_cProc, "call", proc_call, -1); - rb_define_method(rb_cProc, "[]", proc_call, -1); - rb_define_method(rb_cProc, "yield", proc_yield, -1); - rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0); - rb_define_method(rb_cProc, "arity", proc_arity, 0); - rb_define_method(rb_cProc, "clone", proc_clone, 0); - rb_define_method(rb_cProc, "dup", proc_dup, 0); - rb_define_method(rb_cProc, "==", proc_eq, 1); - rb_define_method(rb_cProc, "eql?", proc_eq, 1); - rb_define_method(rb_cProc, "hash", proc_hash, 0); - rb_define_method(rb_cProc, "to_s", proc_to_s, 0); - - /* Binding */ - rb_cBinding = rb_define_class("Binding", rb_cObject); - rb_undef_alloc_func(rb_cBinding); - rb_undef_method(CLASS_OF(rb_cBinding), "new"); - rb_define_method(rb_cBinding, "clone", binding_clone, 0); - rb_define_method(rb_cBinding, "dup", binding_dup, 0); - rb_define_global_function("binding", rb_f_binding, 0); - - rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError); - rb_define_method(rb_eLocalJumpError, "exit_value", localjump_xvalue, 0); - rb_define_method(rb_eLocalJumpError, "reason", localjump_reason, 0); - - /* Exceptions */ - exception_error = rb_exc_new2(rb_eFatal, "exception reentered"); - rb_register_mark_object(exception_error); - - rb_eSysStackError = rb_define_class("SystemStackError", rb_eException); - sysstack_error = rb_exc_new2(rb_eSysStackError, "stack level too deep"); - OBJ_TAINT(sysstack_error); - rb_register_mark_object(sysstack_error); - - /* utility functions */ - rb_define_global_function("proc", rb_block_proc, 0); - rb_define_global_function("lambda", proc_lambda, 0); - - /* Method */ - rb_cMethod = rb_define_class("Method", rb_cObject); - rb_undef_alloc_func(rb_cMethod); - rb_undef_method(CLASS_OF(rb_cMethod), "new"); - rb_define_method(rb_cMethod, "==", method_eq, 1); - rb_define_method(rb_cMethod, "eql?", method_eq, 1); - rb_define_method(rb_cMethod, "hash", method_hash, 0); - rb_define_method(rb_cMethod, "clone", method_clone, 0); - rb_define_method(rb_cMethod, "call", rb_method_call, -1); - rb_define_method(rb_cMethod, "[]", rb_method_call, -1); - rb_define_method(rb_cMethod, "arity", method_arity_m, 0); - rb_define_method(rb_cMethod, "inspect", method_inspect, 0); - rb_define_method(rb_cMethod, "to_s", method_inspect, 0); - rb_define_method(rb_cMethod, "to_proc", method_proc, 0); - rb_define_method(rb_cMethod, "receiver", method_receiver, 0); - rb_define_method(rb_cMethod, "name", method_name, 0); - rb_define_method(rb_cMethod, "owner", method_owner, 0); - rb_define_method(rb_cMethod, "unbind", method_unbind, 0); - rb_define_method(rb_mKernel, "method", rb_obj_method, 1); - - /* UnboundMethod */ - rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cObject); - rb_undef_alloc_func(rb_cUnboundMethod); - rb_undef_method(CLASS_OF(rb_cUnboundMethod), "new"); - rb_define_method(rb_cUnboundMethod, "==", method_eq, 1); - rb_define_method(rb_cUnboundMethod, "eql?", method_eq, 1); - rb_define_method(rb_cUnboundMethod, "hash", method_hash, 0); - rb_define_method(rb_cUnboundMethod, "clone", method_clone, 0); - rb_define_method(rb_cUnboundMethod, "arity", method_arity_m, 0); - rb_define_method(rb_cUnboundMethod, "inspect", method_inspect, 0); - rb_define_method(rb_cUnboundMethod, "to_s", method_inspect, 0); - rb_define_method(rb_cUnboundMethod, "name", method_name, 0); - rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0); - rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1); - - /* Module#*_method */ - rb_define_method(rb_cModule, "instance_method", rb_mod_method, 1); - rb_define_private_method(rb_cModule, "define_method", - rb_mod_define_method, -1); -} - -/* - * Objects of class Binding encapsulate the execution - * context at some particular place in the code and retain this context - * for future use. The variables, methods, value of self, - * and possibly an iterator block that can be accessed in this context - * are all retained. Binding objects can be created using - * Kernel#binding, and are made available to the callback - * of Kernel#set_trace_func. - * - * These binding objects can be passed as the second argument of the - * Kernel#eval method, establishing an environment for the - * evaluation. - * - * class Demo - * def initialize(n) - * @secret = n - * end - * def getBinding - * return binding() - * end - * end - * - * k1 = Demo.new(99) - * b1 = k1.getBinding - * k2 = Demo.new(-3) - * b2 = k2.getBinding - * - * eval("@secret", b1) #=> 99 - * eval("@secret", b2) #=> -3 - * eval("@secret") #=> nil - * - * Binding objects have no class-specific methods. - * - */ - -void -Init_Binding() -{ - -} - +/********************************************************************** + + proc.c - Proc, Bindng, Env + + $Author$ + $Date$ + created at: Wed Jan 17 12:13:14 2007 + + Copyright (C) 2004-2007 Koichi Sasada + +**********************************************************************/ + +#include "eval_intern.h" +#include "gc.h" + +struct METHOD { + VALUE klass, rklass; + VALUE recv; + ID id, oid; + NODE *body; +}; + +VALUE rb_cUnboundMethod; +VALUE rb_cMethod; +VALUE rb_cBinding; +VALUE rb_cProc; +VALUE rb_cEnv; + +static VALUE bmcall(VALUE, VALUE); +static int method_arity(VALUE); +static VALUE rb_obj_is_method(VALUE m); + +/* Env */ + +static void +env_free(void *ptr) +{ + yarv_env_t *env; + FREE_REPORT_ENTER("env"); + if (ptr) { + env = ptr; + FREE_UNLESS_NULL(env->env); + ruby_xfree(ptr); + } + FREE_REPORT_LEAVE("env"); +} + +static void +env_mark(void *ptr) +{ + yarv_env_t *env; + MARK_REPORT_ENTER("env"); + if (ptr) { + env = ptr; + if (env->env) { + /* TODO: should mark more restricted range */ + GC_INFO("env->env\n"); + rb_gc_mark_locations(env->env, env->env + env->env_size); + } + GC_INFO("env->prev_envval\n"); + MARK_UNLESS_NULL(env->prev_envval); + + if (env->block.iseq) { + if (BUILTIN_TYPE(env->block.iseq) == T_NODE) { + MARK_UNLESS_NULL((VALUE)env->block.iseq); + } + else { + MARK_UNLESS_NULL(env->block.iseq->self); + } + } + } + MARK_REPORT_LEAVE("env"); +} + +VALUE +yarv_env_alloc(void) +{ + VALUE obj; + yarv_env_t *env; + obj = Data_Make_Struct(rb_cEnv, yarv_env_t, env_mark, env_free, env); + env->env = 0; + env->prev_envval = 0; + env->block.iseq = 0; + return obj; +} + +/* Proc */ + +static void +proc_free(void *ptr) +{ + FREE_REPORT_ENTER("proc"); + if (ptr) { + ruby_xfree(ptr); + } + FREE_REPORT_LEAVE("proc"); +} + +static void +proc_mark(void *ptr) +{ + yarv_proc_t *proc; + MARK_REPORT_ENTER("proc"); + if (ptr) { + proc = ptr; + MARK_UNLESS_NULL(proc->envval); + MARK_UNLESS_NULL(proc->blockprocval); + MARK_UNLESS_NULL((VALUE)proc->special_cref_stack); + if (proc->block.iseq && YARV_IFUNC_P(proc->block.iseq)) { + MARK_UNLESS_NULL((VALUE)(proc->block.iseq)); + } + } + MARK_REPORT_LEAVE("proc"); +} + +static VALUE +proc_alloc(VALUE klass) +{ + VALUE obj; + yarv_proc_t *proc; + obj = Data_Make_Struct(klass, yarv_proc_t, proc_mark, proc_free, proc); + MEMZERO(proc, yarv_proc_t, 1); + return obj; +} + +VALUE +yarv_proc_alloc(void) +{ + proc_alloc(rb_cProc); +} + +VALUE +yarv_obj_is_proc(VALUE proc) +{ + if (TYPE(proc) == T_DATA && + RDATA(proc)->dfree == (RUBY_DATA_FUNC) proc_free) { + return Qtrue; + } + else { + return Qfalse; + } +} + +static VALUE +proc_dup(VALUE self) +{ + VALUE procval = proc_alloc(rb_cProc); + yarv_proc_t *src, *dst; + GetProcPtr(self, src); + GetProcPtr(procval, dst); + + dst->block = src->block; + dst->envval = src->envval; + dst->safe_level = dst->safe_level; + dst->special_cref_stack = src->special_cref_stack; + + return procval; +} + +VALUE yarv_proc_dup(VALUE self) +{ + return proc_dup(self); +} + +static VALUE +proc_clone(VALUE self) +{ + VALUE procval = proc_dup(self); + CLONESETUP(procval, self); + return procval; +} + +/* Binding */ + +static void +binding_free(void *ptr) +{ + yarv_binding_t *bind; + FREE_REPORT_ENTER("binding"); + if (ptr) { + bind = ptr; + ruby_xfree(ptr); + } + FREE_REPORT_LEAVE("binding"); +} + +static void +binding_mark(void *ptr) +{ + yarv_binding_t *bind; + MARK_REPORT_ENTER("binding"); + if (ptr) { + bind = ptr; + MARK_UNLESS_NULL(bind->env); + MARK_UNLESS_NULL((VALUE)bind->cref_stack); + } + MARK_REPORT_LEAVE("binding"); +} + +static VALUE +binding_alloc(VALUE klass) +{ + VALUE obj; + yarv_binding_t *bind; + obj = Data_Make_Struct(klass, yarv_binding_t, + binding_mark, binding_free, bind); + MEMZERO(bind, yarv_binding_t, 1); + return obj; +} + +static VALUE +binding_dup(VALUE self) +{ + VALUE bindval = binding_alloc(rb_cBinding); + yarv_binding_t *src, *dst; + GetBindingPtr(self, src); + GetBindingPtr(bindval, dst); + dst->env = src->env; + dst->cref_stack = src->cref_stack; + return bindval; +} + +static VALUE +binding_clone(VALUE self) +{ + VALUE bindval = binding_dup(self); + CLONESETUP(bindval, self); + return bindval; +} + +VALUE +rb_binding_new(void) +{ + yarv_thread_t *th = GET_THREAD(); + yarv_control_frame_t *cfp = th_get_ruby_level_cfp(th, th->cfp); + VALUE bindval = binding_alloc(rb_cBinding); + yarv_binding_t *bind; + + GetBindingPtr(bindval, bind); + bind->env = th_make_env_object(th, cfp); + bind->cref_stack = ruby_cref(); + return bindval; +} + +/* + * call-seq: + * binding -> a_binding + * + * Returns a +Binding+ object, describing the variable and + * method bindings at the point of call. This object can be used when + * calling +eval+ to execute the evaluated command in this + * environment. Also see the description of class +Binding+. + * + * def getBinding(param) + * return binding + * end + * b = getBinding("hello") + * eval("param", b) #=> "hello" + */ + +static VALUE +rb_f_binding(VALUE self) +{ + return rb_binding_new(); +} + +/* + * call-seq: + * binding.eval(string [, filename [,lineno]]) => obj + * + * Evaluates the Ruby expression(s) in string, in the + * binding's context. If the optional filename and + * lineno parameters are present, they will be used when + * reporting syntax errors. + * + * def getBinding(param) + * return binding + * end + * b = getBinding("hello") + * b.eval("param") #=> "hello" + */ + +static VALUE +bind_eval(int argc, VALUE *argv, VALUE bind) +{ + UNSUPPORTED(bind_eval); + return Qnil; +} + +#define PROC_TSHIFT (FL_USHIFT+1) +#define PROC_TMASK (FL_USER1|FL_USER2|FL_USER3) +#define PROC_TMAX (PROC_TMASK >> PROC_TSHIFT) +#define PROC_NOSAFE FL_USER4 + +#define SAFE_LEVEL_MAX PROC_TMASK + +static VALUE +proc_new(VALUE klass, int is_lambda) +{ + VALUE procval = Qnil; + yarv_thread_t *th = GET_THREAD(); + yarv_control_frame_t *cfp = th->cfp; + yarv_block_t *block; + + if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0 && + !YARV_CLASS_SPECIAL_P(cfp->lfp[0])) { + block = GC_GUARDED_PTR_REF(cfp->lfp[0]); + } + else { + cfp = YARV_PREVIOUS_CONTROL_FRAME(cfp); + if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0 && + !YARV_CLASS_SPECIAL_P(cfp->lfp[0])) { + block = GC_GUARDED_PTR_REF(cfp->lfp[0]); + + if (is_lambda) { + rb_warn("tried to create Proc object without a block"); + } + } + else { + rb_raise(rb_eArgError, + "tried to create Proc object without a block"); + } + } + + cfp = YARV_PREVIOUS_CONTROL_FRAME(cfp); + procval = th_make_proc(th, cfp, block); + + if (is_lambda) { + yarv_proc_t *proc; + GetProcPtr(procval, proc); + proc->is_lambda = Qtrue; + } + return procval; +} + +/* + * call-seq: + * Proc.new {|...| block } => a_proc + * Proc.new => a_proc + * + * Creates a new Proc object, bound to the current + * context. Proc::new may be called without a block only + * within a method with an attached block, in which case that block is + * converted to the Proc object. + * + * def proc_from + * Proc.new + * end + * proc = proc_from { "hello" } + * proc.call #=> "hello" + */ + +static VALUE +rb_proc_s_new(VALUE klass) +{ + return proc_new(klass, Qfalse); +} + +/* + * call-seq: + * proc { |...| block } => a_proc + * + * Equivalent to Proc.new. + */ + +VALUE +rb_block_proc(void) +{ + return proc_new(rb_cProc, Qfalse); +} + +VALUE +rb_block_lambda(void) +{ + return proc_new(rb_cProc, Qtrue); +} + +VALUE +rb_f_lambda(void) +{ + rb_warn("rb_f_lambda() is deprecated; use rb_block_proc() instead"); + return rb_block_lambda(); +} + +/* + * call-seq: + * lambda { |...| block } => a_proc + * + * Equivalent to Proc.new, except the resulting Proc objects + * check the number of parameters passed when called. + */ + +static VALUE +proc_lambda(void) +{ + return rb_block_lambda(); +} + +VALUE +proc_invoke(VALUE self, VALUE args, VALUE alt_self, VALUE alt_klass) +{ + yarv_proc_t *proc; + GetProcPtr(self, proc); + + /* ignore self and klass */ + return th_invoke_proc(GET_THREAD(), proc, proc->block.self, + RARRAY_LEN(args), RARRAY_PTR(args)); +} + +/* CHECKME: are the argument checking semantics correct? */ + +/* + * call-seq: + * prc.call(params,...) => obj + * prc[params,...] => obj + * + * Invokes the block, setting the block's parameters to the values in + * params using something close to method calling semantics. + * Generates a warning if multiple values are passed to a proc that + * expects just one (previously this silently converted the parameters + * to an array). + * + * For procs created using Kernel.proc, generates an + * error if the wrong number of parameters + * are passed to a proc with multiple parameters. For procs created using + * Proc.new, extra parameters are silently discarded. + * + * Returns the value of the last expression evaluated in the block. See + * also Proc#yield. + * + * a_proc = Proc.new {|a, *b| b.collect {|i| i*a }} + * a_proc.call(9, 1, 2, 3) #=> [9, 18, 27] + * a_proc[9, 1, 2, 3] #=> [9, 18, 27] + * a_proc = Proc.new {|a,b| a} + * a_proc.call(1,2,3) + * + * produces: + * + * prog.rb:5: wrong number of arguments (3 for 2) (ArgumentError) + * from prog.rb:4:in `call' + * from prog.rb:5 + */ + +static VALUE +proc_call(int argc, VALUE *argv, VALUE procval) +{ + yarv_proc_t *proc; + GetProcPtr(procval, proc); + return th_invoke_proc(GET_THREAD(), proc, proc->block.self, argc, argv); +} + +static VALUE +proc_yield(int argc, VALUE *argv, VALUE procval) +{ + yarv_proc_t *proc; + GetProcPtr(procval, proc); + return th_invoke_proc(GET_THREAD(), proc, proc->block.self, argc, argv); +} + +VALUE +rb_proc_call(VALUE proc, VALUE args) +{ + return proc_invoke(proc, args, Qundef, 0); +} + +/* + * call-seq: + * prc.arity -> fixnum + * + * Returns the number of arguments that would not be ignored. If the block + * is declared to take no arguments, returns 0. If the block is known + * to take exactly n arguments, returns n. If the block has optional + * arguments, return -n-1, where n is the number of mandatory + * arguments. A proc with no argument declarations + * is the same a block declaring || as its arguments. + * + * Proc.new {}.arity #=> 0 + * Proc.new {||}.arity #=> 0 + * Proc.new {|a|}.arity #=> 1 + * Proc.new {|a,b|}.arity #=> 2 + * Proc.new {|a,b,c|}.arity #=> 3 + * Proc.new {|*a|}.arity #=> -1 + * Proc.new {|a,*b|}.arity #=> -2 + */ + +static VALUE +proc_arity(VALUE self) +{ + yarv_proc_t *proc; + yarv_iseq_t *iseq; + GetProcPtr(self, proc); + iseq = proc->block.iseq; + if (iseq && BUILTIN_TYPE(iseq) != T_NODE) { + if (iseq->arg_rest == 0 && iseq->arg_opts == 0) { + return INT2FIX(iseq->argc); + } + else { + return INT2FIX(-iseq->argc - 1); + } + } + else { + return INT2FIX(-1); + } +} + +int +rb_proc_arity(VALUE proc) +{ + return FIX2INT(proc_arity(proc)); +} + +/* + * call-seq: + * prc == other_proc => true or false + * + * Return true if prc is the same object as + * other_proc, or if they are both procs with the same body. + */ + +static VALUE +proc_eq(VALUE self, VALUE other) +{ + if (self == other) { + return Qtrue; + } + else { + if (TYPE(other) == T_DATA && + RBASIC(other)->klass == rb_cProc && + CLASS_OF(self) == CLASS_OF(other)) { + yarv_proc_t *p1, *p2; + GetProcPtr(self, p1); + GetProcPtr(other, p2); + if (p1->block.iseq == p2->block.iseq && p1->envval == p2->envval) { + return Qtrue; + } + } + } + return Qfalse; +} + +/* + * call-seq: + * prc.hash => integer + * + * Return hash value corresponding to proc body. + */ + +static VALUE +proc_hash(VALUE self) +{ + int hash; + yarv_proc_t *proc; + GetProcPtr(self, proc); + hash = (long)proc->block.iseq; + hash ^= (long)proc->envval; + hash ^= (long)proc->block.lfp >> 16; + return INT2FIX(hash); +} + +/* + * call-seq: + * prc.to_s => string + * + * Shows the unique identifier for this proc, along with + * an indication of where the proc was defined. + */ + +static VALUE +proc_to_s(VALUE self) +{ + VALUE str = 0; + yarv_proc_t *proc; + char *cname = rb_obj_classname(self); + yarv_iseq_t *iseq; + + GetProcPtr(self, proc); + iseq = proc->block.iseq; + + if (YARV_NORMAL_ISEQ_P(iseq)) { + int line_no = 0; + + if (iseq->insn_info_tbl) { + line_no = iseq->insn_info_tbl[0].line_no; + } + str = rb_sprintf("#<%s:%lx@%s:%d>", cname, self, + RSTRING_PTR(iseq->file_name), + line_no); + } + else { + str = rb_sprintf("#<%s:%p>", cname, proc->block.iseq); + } + + if (OBJ_TAINTED(self)) { + OBJ_TAINT(str); + } + return str; +} + +/* + * call-seq: + * prc.to_proc -> prc + * + * Part of the protocol for converting objects to Proc + * objects. Instances of class Proc simply return + * themselves. + */ + +static VALUE +proc_to_proc(VALUE self) +{ + return self; +} + +/* + * call-seq: + * prc.binding => binding + * + * Returns the binding associated with prc. Note that + * Kernel#eval accepts either a Proc or a + * Binding object as its second parameter. + * + * def fred(param) + * proc {} + * end + * + * b = fred(99) + * eval("param", b.binding) #=> 99 + * eval("param", b) #=> 99 + */ + +void +bm_mark(struct METHOD *data) +{ + rb_gc_mark(data->rklass); + rb_gc_mark(data->klass); + rb_gc_mark(data->recv); + rb_gc_mark((VALUE)data->body); +} + +NODE *rb_get_method_body(VALUE klass, ID id, ID *idp); + +static VALUE +mnew(VALUE klass, VALUE obj, ID id, VALUE mklass) +{ + VALUE method; + NODE *body; + struct METHOD *data; + VALUE rklass = klass; + ID oid = id; + + again: + if ((body = rb_get_method_body(klass, id, 0)) == 0) { + print_undef(rklass, oid); + } + + klass = body->nd_clss; + body = body->nd_body; + + if (nd_type(body) == NODE_ZSUPER) { + klass = RCLASS(klass)->super; + goto again; + } + + while (rklass != klass && + (FL_TEST(rklass, FL_SINGLETON) || TYPE(rklass) == T_ICLASS)) { + rklass = RCLASS(rklass)->super; + } + if (TYPE(klass) == T_ICLASS) + klass = RBASIC(klass)->klass; + method = Data_Make_Struct(mklass, struct METHOD, bm_mark, -1, data); + data->klass = klass; + data->recv = obj; + + data->id = id; + data->body = body; + data->rklass = rklass; + data->oid = oid; + OBJ_INFECT(method, klass); + + return method; +} + + +/********************************************************************** + * + * Document-class : Method + * + * Method objects are created by Object#method, and are + * associated with a particular object (not just with a class). They + * may be used to invoke the method within the object, and as a block + * associated with an iterator. They may also be unbound from one + * object (creating an UnboundMethod) and bound to + * another. + * + * class Thing + * def square(n) + * n*n + * end + * end + * thing = Thing.new + * meth = thing.method(:square) + * + * meth.call(9) #=> 81 + * [ 1, 2, 3 ].collect(&meth) #=> [1, 4, 9] + * + */ + +/* + * call-seq: + * meth == other_meth => true or false + * + * Two method objects are equal if that are bound to the same + * object and contain the same body. + */ + + +static VALUE +method_eq(method, other) + VALUE method, other; +{ + struct METHOD *m1, *m2; + + if (TYPE(other) != T_DATA + || RDATA(other)->dmark != (RUBY_DATA_FUNC) bm_mark) + return Qfalse; + if (CLASS_OF(method) != CLASS_OF(other)) + return Qfalse; + + Data_Get_Struct(method, struct METHOD, m1); + Data_Get_Struct(other, struct METHOD, m2); + + if (m1->klass != m2->klass || m1->rklass != m2->rklass || + m1->recv != m2->recv || m1->body != m2->body) + return Qfalse; + + return Qtrue; +} + +/* + * call-seq: + * meth.hash => integer + * + * Return a hash value corresponding to the method object. + */ + +static VALUE +method_hash(method) + VALUE method; +{ + struct METHOD *m; + long hash; + + Data_Get_Struct(method, struct METHOD, m); + hash = (long)m->klass; + hash ^= (long)m->rklass; + hash ^= (long)m->recv; + hash ^= (long)m->body; + + return INT2FIX(hash); +} + +/* + * call-seq: + * meth.unbind => unbound_method + * + * Dissociates meth from it's current receiver. The resulting + * UnboundMethod can subsequently be bound to a new object + * of the same class (see UnboundMethod). + */ + +static VALUE +method_unbind(obj) + VALUE obj; +{ + VALUE method; + struct METHOD *orig, *data; + + Data_Get_Struct(obj, struct METHOD, orig); + method = + Data_Make_Struct(rb_cUnboundMethod, struct METHOD, bm_mark, free, + data); + data->klass = orig->klass; + data->recv = Qundef; + data->id = orig->id; + data->body = orig->body; + data->rklass = orig->rklass; + data->oid = orig->oid; + OBJ_INFECT(method, obj); + + return method; +} + +/* + * call-seq: + * meth.receiver => object + * + * Returns the bound receiver of the method object. + */ + +static VALUE +method_receiver(VALUE obj) +{ + struct METHOD *data; + + Data_Get_Struct(obj, struct METHOD, data); + return data->recv; +} + +/* + * call-seq: + * meth.name => string + * + * Returns the name of the method. + */ + +static VALUE +method_name(VALUE obj) +{ + struct METHOD *data; + + Data_Get_Struct(obj, struct METHOD, data); + return rb_str_new2(rb_id2name(data->id)); +} + +/* + * call-seq: + * meth.owner => class_or_module + * + * Returns the class or module that defines the method. + */ + +static VALUE +method_owner(VALUE obj) +{ + struct METHOD *data; + + Data_Get_Struct(obj, struct METHOD, data); + return data->klass; +} + +/* + * call-seq: + * obj.method(sym) => method + * + * Looks up the named method as a receiver in obj, returning a + * Method object (or raising NameError). The + * Method object acts as a closure in obj's object + * instance, so instance variables and the value of self + * remain available. + * + * class Demo + * def initialize(n) + * @iv = n + * end + * def hello() + * "Hello, @iv = #{@iv}" + * end + * end + * + * k = Demo.new(99) + * m = k.method(:hello) + * m.call #=> "Hello, @iv = 99" + * + * l = Demo.new('Fred') + * m = l.method("hello") + * m.call #=> "Hello, @iv = Fred" + */ + +VALUE +rb_obj_method(obj, vid) + VALUE obj; + VALUE vid; +{ + return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod); +} + +/* + * call-seq: + * mod.instance_method(symbol) => unbound_method + * + * Returns an +UnboundMethod+ representing the given + * instance method in _mod_. + * + * class Interpreter + * def do_a() print "there, "; end + * def do_d() print "Hello "; end + * def do_e() print "!\n"; end + * def do_v() print "Dave"; end + * Dispatcher = { + * ?a => instance_method(:do_a), + * ?d => instance_method(:do_d), + * ?e => instance_method(:do_e), + * ?v => instance_method(:do_v) + * } + * def interpret(string) + * string.each_byte {|b| Dispatcher[b].bind(self).call } + * end + * end + * + * + * interpreter = Interpreter.new + * interpreter.interpret('dave') + * + * produces: + * + * Hello there, Dave! + */ + +static VALUE +rb_mod_method(mod, vid) + VALUE mod; + VALUE vid; +{ + return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod); +} + +/* + * call-seq: + * define_method(symbol, method) => new_method + * define_method(symbol) { block } => proc + * + * Defines an instance method in the receiver. The _method_ + * parameter can be a +Proc+ or +Method+ object. + * If a block is specified, it is used as the method body. This block + * is evaluated using instance_eval, a point that is + * tricky to demonstrate because define_method is private. + * (This is why we resort to the +send+ hack in this example.) + * + * class A + * def fred + * puts "In Fred" + * end + * def create_method(name, &block) + * self.class.send(:define_method, name, &block) + * end + * define_method(:wilma) { puts "Charge it!" } + * end + * class B < A + * define_method(:barney, instance_method(:fred)) + * end + * a = B.new + * a.barney + * a.wilma + * a.create_method(:betty) { p self } + * a.betty + * + * produces: + * + * In Fred + * Charge it! + * # + */ + +VALUE yarv_proc_dup(VALUE self); + +static VALUE +rb_mod_define_method(int argc, VALUE *argv, VALUE mod) +{ + ID id; + VALUE body; + NODE *node; + int noex = NOEX_PUBLIC; + + if (argc == 1) { + id = rb_to_id(argv[0]); + body = rb_block_lambda(); + } + else if (argc == 2) { + id = rb_to_id(argv[0]); + body = argv[1]; + if (!rb_obj_is_method(body) && !yarv_obj_is_proc(body)) { + rb_raise(rb_eTypeError, + "wrong argument type %s (expected Proc/Method)", + rb_obj_classname(body)); + } + } + else { + rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc); + } + + if (RDATA(body)->dmark == (RUBY_DATA_FUNC) bm_mark) { + struct METHOD *method = (struct METHOD *)DATA_PTR(body); + VALUE rklass = method->rklass; + if (rklass != mod) { + if (FL_TEST(rklass, FL_SINGLETON)) { + rb_raise(rb_eTypeError, + "can't bind singleton method to a different class"); + } + if (!RTEST(rb_class_inherited_p(mod, rklass))) { + rb_raise(rb_eTypeError, + "bind argument must be a subclass of %s", + rb_class2name(rklass)); + } + } + node = method->body; + } + else if (yarv_obj_is_proc(body)) { + yarv_proc_t *proc; + body = yarv_proc_dup(body); + GetProcPtr(body, proc); + if (BUILTIN_TYPE(proc->block.iseq) != T_NODE) { + proc->block.iseq->defined_method_id = id; + proc->block.iseq->klass = mod; + proc->is_lambda = Qtrue; + } + node = NEW_BMETHOD(body); + } + else { + /* type error */ + rb_raise(rb_eTypeError, "wrong argument type (expected Proc/Method)"); + } + + /* TODO: visibility */ + + rb_add_method(mod, id, node, noex); + return body; +} + + +/* + * MISSING: documentation + */ + +static VALUE +method_clone(self) + VALUE self; +{ + VALUE clone; + struct METHOD *orig, *data; + + Data_Get_Struct(self, struct METHOD, orig); + clone = + Data_Make_Struct(CLASS_OF(self), struct METHOD, bm_mark, free, data); + CLONESETUP(clone, self); + *data = *orig; + + return clone; +} + +/* + * call-seq: + * meth.call(args, ...) => obj + * meth[args, ...] => obj + * + * Invokes the meth with the specified arguments, returning the + * method's return value. + * + * m = 12.method("+") + * m.call(3) #=> 15 + * m.call(20) #=> 32 + */ + +VALUE +rb_method_call(int argc, VALUE *argv, VALUE method) +{ + VALUE result = Qnil; /* OK */ + struct METHOD *data; + int state; + volatile int safe = -1; + + Data_Get_Struct(method, struct METHOD, data); + if (data->recv == Qundef) { + rb_raise(rb_eTypeError, "can't call unbound method; bind first"); + } + PUSH_TAG(PROT_NONE); + if (OBJ_TAINTED(method)) { + safe = rb_safe_level(); + if (rb_safe_level() < 4) { + rb_set_safe_level_force(4); + } + } + if ((state = EXEC_TAG()) == 0) { + PASS_PASSED_BLOCK(); + result = th_call0(GET_THREAD(), + data->klass, data->recv, data->id, data->oid, + argc, argv, data->body, 0); + } + POP_TAG(); + if (safe >= 0) + rb_set_safe_level_force(safe); + if (state) + JUMP_TAG(state); + return result; +} + +/********************************************************************** + * + * Document-class: UnboundMethod + * + * Ruby supports two forms of objectified methods. Class + * Method is used to represent methods that are associated + * with a particular object: these method objects are bound to that + * object. Bound method objects for an object can be created using + * Object#method. + * + * Ruby also supports unbound methods; methods objects that are not + * associated with a particular object. These can be created either by + * calling Module#instance_method or by calling + * unbind on a bound method object. The result of both of + * these is an UnboundMethod object. + * + * Unbound methods can only be called after they are bound to an + * object. That object must be be a kind_of? the method's original + * class. + * + * class Square + * def area + * @side * @side + * end + * def initialize(side) + * @side = side + * end + * end + * + * area_un = Square.instance_method(:area) + * + * s = Square.new(12) + * area = area_un.bind(s) + * area.call #=> 144 + * + * Unbound methods are a reference to the method at the time it was + * objectified: subsequent changes to the underlying class will not + * affect the unbound method. + * + * class Test + * def test + * :original + * end + * end + * um = Test.instance_method(:test) + * class Test + * def test + * :modified + * end + * end + * t = Test.new + * t.test #=> :modified + * um.bind(t).call #=> :original + * + */ + +/* + * call-seq: + * umeth.bind(obj) -> method + * + * Bind umeth to obj. If Klass was the class + * from which umeth was obtained, + * obj.kind_of?(Klass) must be true. + * + * class A + * def test + * puts "In test, class = #{self.class}" + * end + * end + * class B < A + * end + * class C < B + * end + * + * + * um = B.instance_method(:test) + * bm = um.bind(C.new) + * bm.call + * bm = um.bind(B.new) + * bm.call + * bm = um.bind(A.new) + * bm.call + * + * produces: + * + * In test, class = C + * In test, class = B + * prog.rb:16:in `bind': bind argument must be an instance of B (TypeError) + * from prog.rb:16 + */ + +static VALUE +umethod_bind(method, recv) + VALUE method, recv; +{ + struct METHOD *data, *bound; + + Data_Get_Struct(method, struct METHOD, data); + if (data->rklass != CLASS_OF(recv)) { + if (FL_TEST(data->rklass, FL_SINGLETON)) { + rb_raise(rb_eTypeError, + "singleton method called for a different object"); + } + if (!rb_obj_is_kind_of(recv, data->rklass)) { + rb_raise(rb_eTypeError, "bind argument must be an instance of %s", + rb_class2name(data->rklass)); + } + } + + method = + Data_Make_Struct(rb_cMethod, struct METHOD, bm_mark, free, bound); + *bound = *data; + bound->recv = recv; + bound->rklass = CLASS_OF(recv); + + return method; +} + +int +rb_node_arity(NODE * body) +{ + int n; + + switch (nd_type(body)) { + case NODE_CFUNC: + if (body->nd_argc < 0) + return -1; + return body->nd_argc; + case NODE_ZSUPER: + return -1; + case NODE_ATTRSET: + return 1; + case NODE_IVAR: + return 0; + case NODE_BMETHOD: + return rb_proc_arity(body->nd_cval); + case NODE_SCOPE: + body = body->nd_next; /* skip NODE_SCOPE */ + if (nd_type(body) == NODE_BLOCK) + body = body->nd_head; + if (!body) + return 0; + n = body->nd_frml ? RARRAY_LEN(body->nd_frml) : 0; + if (body->nd_opt || body->nd_rest) + n = -n - 1; + return n; + case YARV_METHOD_NODE:{ + yarv_iseq_t *iseq; + GetISeqPtr((VALUE)body->nd_body, iseq); + if (iseq->arg_rest == 0 && iseq->arg_opts == 0) { + return iseq->argc; + } + else { + return -iseq->argc - 1; + } + } + default: + rb_raise(rb_eArgError, "invalid node 0x%x", nd_type(body)); + } +} + +/* + * call-seq: + * meth.arity => fixnum + * + * Returns an indication of the number of arguments accepted by a + * method. Returns a nonnegative integer for methods that take a fixed + * number of arguments. For Ruby methods that take a variable number of + * arguments, returns -n-1, where n is the number of required + * arguments. For methods written in C, returns -1 if the call takes a + * variable number of arguments. + * + * class C + * def one; end + * def two(a); end + * def three(*a); end + * def four(a, b); end + * def five(a, b, *c); end + * def six(a, b, *c, &d); end + * end + * c = C.new + * c.method(:one).arity #=> 0 + * c.method(:two).arity #=> 1 + * c.method(:three).arity #=> -1 + * c.method(:four).arity #=> 2 + * c.method(:five).arity #=> -3 + * c.method(:six).arity #=> -3 + * + * "cat".method(:size).arity #=> 0 + * "cat".method(:replace).arity #=> 1 + * "cat".method(:squeeze).arity #=> -1 + * "cat".method(:count).arity #=> -1 + */ + +static VALUE +method_arity_m(method) + VALUE method; +{ + int n = method_arity(method); + return INT2FIX(n); +} + +static int +method_arity(method) + VALUE method; +{ + struct METHOD *data; + + Data_Get_Struct(method, struct METHOD, data); + return rb_node_arity(data->body); +} + +int +rb_mod_method_arity(mod, id) + VALUE mod; + ID id; +{ + NODE *node = rb_method_node(mod, id); + return rb_node_arity(node); +} + +int +rb_obj_method_arity(obj, id) + VALUE obj; + ID id; +{ + return rb_mod_method_arity(CLASS_OF(obj), id); +} + +/* + * call-seq: + * meth.to_s => string + * meth.inspect => string + * + * Show the name of the underlying method. + * + * "cat".method(:count).inspect #=> "#" + */ + +static VALUE +method_inspect(VALUE method) +{ + struct METHOD *data; + VALUE str; + const char *s; + char *sharp = "#"; + + Data_Get_Struct(method, struct METHOD, data); + str = rb_str_buf_new2("#<"); + s = rb_obj_classname(method); + rb_str_buf_cat2(str, s); + rb_str_buf_cat2(str, ": "); + + if (FL_TEST(data->klass, FL_SINGLETON)) { + VALUE v = rb_iv_get(data->klass, "__attached__"); + + if (data->recv == Qundef) { + rb_str_buf_append(str, rb_inspect(data->klass)); + } + else if (data->recv == v) { + rb_str_buf_append(str, rb_inspect(v)); + sharp = "."; + } + else { + rb_str_buf_append(str, rb_inspect(data->recv)); + rb_str_buf_cat2(str, "("); + rb_str_buf_append(str, rb_inspect(v)); + rb_str_buf_cat2(str, ")"); + sharp = "."; + } + } + else { + rb_str_buf_cat2(str, rb_class2name(data->rklass)); + if (data->rklass != data->klass) { + rb_str_buf_cat2(str, "("); + rb_str_buf_cat2(str, rb_class2name(data->klass)); + rb_str_buf_cat2(str, ")"); + } + } + rb_str_buf_cat2(str, sharp); + rb_str_buf_cat2(str, rb_id2name(data->oid)); + rb_str_buf_cat2(str, ">"); + + return str; +} + +static VALUE +mproc(VALUE method) +{ + return rb_funcall(Qnil, rb_intern("proc"), 0); +} + +static VALUE +bmcall(VALUE args, VALUE method) +{ + volatile VALUE a; + if (CLASS_OF(args) != rb_cArray) { + args = rb_ary_new3(1, args); + } + + a = args; + return rb_method_call(RARRAY_LEN(a), RARRAY_PTR(a), method); +} + +VALUE +rb_proc_new( + VALUE (*func)(ANYARGS), /* VALUE yieldarg[, VALUE procarg] */ + VALUE val) +{ + yarv_proc_t *proc; + VALUE procval = rb_iterate((VALUE(*)(VALUE))mproc, 0, func, val); + GetProcPtr(procval, proc); + ((NODE*)proc->block.iseq)->u3.state = 1; + return procval; +} + +/* + * call-seq: + * meth.to_proc => prc + * + * Returns a Proc object corresponding to this method. + */ + +static VALUE +method_proc(VALUE method) +{ + VALUE proc; + /* + * class Method + * def to_proc + * proc{|*args| + * self.call(*args) + * } + * end + * end + */ + proc = rb_iterate((VALUE (*)(VALUE))mproc, 0, bmcall, method); + return proc; +} + +static VALUE +rb_obj_is_method(VALUE m) +{ + if (TYPE(m) == T_DATA && RDATA(m)->dmark == (RUBY_DATA_FUNC) bm_mark) { + return Qtrue; + } + return Qfalse; +} + +/* + * call_seq: + * local_jump_error.exit_value => obj + * + * Returns the exit value associated with this +LocalJumpError+. + */ +static VALUE +localjump_xvalue(VALUE exc) +{ + return rb_iv_get(exc, "@exit_value"); +} + +/* + * call-seq: + * local_jump_error.reason => symbol + * + * The reason this block was terminated: + * :break, :redo, :retry, :next, :return, or :noreason. + */ + +static VALUE +localjump_reason(VALUE exc) +{ + return rb_iv_get(exc, "@reason"); +} + + +/* + * Proc objects are blocks of code that have been bound to + * a set of local variables. Once bound, the code may be called in + * different contexts and still access those variables. + * + * def gen_times(factor) + * return Proc.new {|n| n*factor } + * end + * + * times3 = gen_times(3) + * times5 = gen_times(5) + * + * times3.call(12) #=> 36 + * times5.call(5) #=> 25 + * times3.call(times5.call(4)) #=> 60 + * + */ + +void +Init_Proc() +{ + /* Env */ + rb_cVM = rb_define_class("VM", rb_cObject); /* TODO: should be moved to suitable place */ + rb_cEnv = rb_define_class_under(rb_cVM, "Env", rb_cObject); + rb_undef_alloc_func(rb_cEnv); + + /* Proc */ + rb_cProc = rb_define_class("Proc", rb_cObject); + rb_undef_alloc_func(rb_cProc); + rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, 0); + rb_define_method(rb_cProc, "call", proc_call, -1); + rb_define_method(rb_cProc, "[]", proc_call, -1); + rb_define_method(rb_cProc, "yield", proc_yield, -1); + rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0); + rb_define_method(rb_cProc, "arity", proc_arity, 0); + rb_define_method(rb_cProc, "clone", proc_clone, 0); + rb_define_method(rb_cProc, "dup", proc_dup, 0); + rb_define_method(rb_cProc, "==", proc_eq, 1); + rb_define_method(rb_cProc, "eql?", proc_eq, 1); + rb_define_method(rb_cProc, "hash", proc_hash, 0); + rb_define_method(rb_cProc, "to_s", proc_to_s, 0); + + /* Binding */ + rb_cBinding = rb_define_class("Binding", rb_cObject); + rb_undef_alloc_func(rb_cBinding); + rb_undef_method(CLASS_OF(rb_cBinding), "new"); + rb_define_method(rb_cBinding, "clone", binding_clone, 0); + rb_define_method(rb_cBinding, "dup", binding_dup, 0); + rb_define_global_function("binding", rb_f_binding, 0); + + rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError); + rb_define_method(rb_eLocalJumpError, "exit_value", localjump_xvalue, 0); + rb_define_method(rb_eLocalJumpError, "reason", localjump_reason, 0); + + /* Exceptions */ + exception_error = rb_exc_new2(rb_eFatal, "exception reentered"); + rb_register_mark_object(exception_error); + + rb_eSysStackError = rb_define_class("SystemStackError", rb_eException); + sysstack_error = rb_exc_new2(rb_eSysStackError, "stack level too deep"); + OBJ_TAINT(sysstack_error); + rb_register_mark_object(sysstack_error); + + /* utility functions */ + rb_define_global_function("proc", rb_block_proc, 0); + rb_define_global_function("lambda", proc_lambda, 0); + + /* Method */ + rb_cMethod = rb_define_class("Method", rb_cObject); + rb_undef_alloc_func(rb_cMethod); + rb_undef_method(CLASS_OF(rb_cMethod), "new"); + rb_define_method(rb_cMethod, "==", method_eq, 1); + rb_define_method(rb_cMethod, "eql?", method_eq, 1); + rb_define_method(rb_cMethod, "hash", method_hash, 0); + rb_define_method(rb_cMethod, "clone", method_clone, 0); + rb_define_method(rb_cMethod, "call", rb_method_call, -1); + rb_define_method(rb_cMethod, "[]", rb_method_call, -1); + rb_define_method(rb_cMethod, "arity", method_arity_m, 0); + rb_define_method(rb_cMethod, "inspect", method_inspect, 0); + rb_define_method(rb_cMethod, "to_s", method_inspect, 0); + rb_define_method(rb_cMethod, "to_proc", method_proc, 0); + rb_define_method(rb_cMethod, "receiver", method_receiver, 0); + rb_define_method(rb_cMethod, "name", method_name, 0); + rb_define_method(rb_cMethod, "owner", method_owner, 0); + rb_define_method(rb_cMethod, "unbind", method_unbind, 0); + rb_define_method(rb_mKernel, "method", rb_obj_method, 1); + + /* UnboundMethod */ + rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cObject); + rb_undef_alloc_func(rb_cUnboundMethod); + rb_undef_method(CLASS_OF(rb_cUnboundMethod), "new"); + rb_define_method(rb_cUnboundMethod, "==", method_eq, 1); + rb_define_method(rb_cUnboundMethod, "eql?", method_eq, 1); + rb_define_method(rb_cUnboundMethod, "hash", method_hash, 0); + rb_define_method(rb_cUnboundMethod, "clone", method_clone, 0); + rb_define_method(rb_cUnboundMethod, "arity", method_arity_m, 0); + rb_define_method(rb_cUnboundMethod, "inspect", method_inspect, 0); + rb_define_method(rb_cUnboundMethod, "to_s", method_inspect, 0); + rb_define_method(rb_cUnboundMethod, "name", method_name, 0); + rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0); + rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1); + + /* Module#*_method */ + rb_define_method(rb_cModule, "instance_method", rb_mod_method, 1); + rb_define_private_method(rb_cModule, "define_method", + rb_mod_define_method, -1); +} + +/* + * Objects of class Binding encapsulate the execution + * context at some particular place in the code and retain this context + * for future use. The variables, methods, value of self, + * and possibly an iterator block that can be accessed in this context + * are all retained. Binding objects can be created using + * Kernel#binding, and are made available to the callback + * of Kernel#set_trace_func. + * + * These binding objects can be passed as the second argument of the + * Kernel#eval method, establishing an environment for the + * evaluation. + * + * class Demo + * def initialize(n) + * @secret = n + * end + * def getBinding + * return binding() + * end + * end + * + * k1 = Demo.new(99) + * b1 = k1.getBinding + * k2 = Demo.new(-3) + * b2 = k2.getBinding + * + * eval("@secret", b1) #=> 99 + * eval("@secret", b2) #=> -3 + * eval("@secret") #=> nil + * + * Binding objects have no class-specific methods. + * + */ + +void +Init_Binding() +{ + +} + diff --git a/template/insns.inc.tmpl b/template/insns.inc.tmpl index b5c17b3635..05af3e81f3 100644 --- a/template/insns.inc.tmpl +++ b/template/insns.inc.tmpl @@ -1,21 +1,21 @@ -/** -*-c-*- - This file contains YARV instructions list. - - ---- - This file is auto generated by insns2vm.rb - DO NOT TOUCH! - - If you want to fix something, you must edit 'template/insns.inc.tmpl' - or insns2vm.rb - */ - - -/* BIN : Basic Instruction Name */ -#define BIN(n) YARVINSN_##n - -enum{ -<%= insns %> -}; - -#define YARV_MAX_INSTRUCTION_SIZE <%= @insns.size %> - +/** -*-c-*- + This file contains YARV instructions list. + + ---- + This file is auto generated by insns2vm.rb + DO NOT TOUCH! + + If you want to fix something, you must edit 'template/insns.inc.tmpl' + or insns2vm.rb + */ + + +/* BIN : Basic Instruction Name */ +#define BIN(n) YARVINSN_##n + +enum{ +<%= insns %> +}; + +#define YARV_MAX_INSTRUCTION_SIZE <%= @insns.size %> + diff --git a/template/insns_info.inc.tmpl b/template/insns_info.inc.tmpl index 8f63cea9ea..5ac92f82fd 100644 --- a/template/insns_info.inc.tmpl +++ b/template/insns_info.inc.tmpl @@ -1,77 +1,77 @@ -/** -*-c-*- - This file contains instruction information for yarv instruction sequence. - - ---- - This file is auto generated by insns2vm.rb - DO NOT TOUCH! - - If you want to fix something, you must edit 'template/insns_info.inc.tmpl' - or insns2vm.rb - */ - -<%= insn_type_chars %> - -static char *insn_name_info[] = { -<%= insn_names %> -}; - -static char *insn_operand_info[] = { -<%= operands_info %> -}; - -static int insn_len_info[] = { -<%= operands_num_info %> -}; - -static int insn_stack_push_num_info[] = { -<%= stack_num_info %> -}; - -static int -insn_stack_increase(int depth, int insn, VALUE *opes) -{ - switch(insn){ -<%= stack_increase %> - default: - rb_bug("insn_sp_increase: unreachable"); - } - return 0; -} - -/* some utilities */ - -static int -insn_len(int insn) -{ - return insn_len_info[insn]; -} - -static char * -insn_name(int insn) -{ - return insn_name_info[insn]; -} - -static char * -insn_op_types(int insn) -{ - return insn_operand_info[insn]; -} - -static int -insn_op_type(int insn, int pos) -{ - int len = insn_len(insn) - 1; - if(pos < len){ - return insn_operand_info[insn][pos]; - } - else{ - return 0; - } -} - -static int -insn_ret_num(int insn) -{ - return insn_stack_push_num_info[insn]; -} +/** -*-c-*- + This file contains instruction information for yarv instruction sequence. + + ---- + This file is auto generated by insns2vm.rb + DO NOT TOUCH! + + If you want to fix something, you must edit 'template/insns_info.inc.tmpl' + or insns2vm.rb + */ + +<%= insn_type_chars %> + +static char *insn_name_info[] = { +<%= insn_names %> +}; + +static char *insn_operand_info[] = { +<%= operands_info %> +}; + +static int insn_len_info[] = { +<%= operands_num_info %> +}; + +static int insn_stack_push_num_info[] = { +<%= stack_num_info %> +}; + +static int +insn_stack_increase(int depth, int insn, VALUE *opes) +{ + switch(insn){ +<%= stack_increase %> + default: + rb_bug("insn_sp_increase: unreachable"); + } + return 0; +} + +/* some utilities */ + +static int +insn_len(int insn) +{ + return insn_len_info[insn]; +} + +static char * +insn_name(int insn) +{ + return insn_name_info[insn]; +} + +static char * +insn_op_types(int insn) +{ + return insn_operand_info[insn]; +} + +static int +insn_op_type(int insn, int pos) +{ + int len = insn_len(insn) - 1; + if(pos < len){ + return insn_operand_info[insn][pos]; + } + else{ + return 0; + } +} + +static int +insn_ret_num(int insn) +{ + return insn_stack_push_num_info[insn]; +} diff --git a/template/insnstbl.html b/template/insnstbl.html index f63bc3218d..1a3613d897 100644 --- a/template/insnstbl.html +++ b/template/insnstbl.html @@ -1,39 +1,39 @@ - - - - - - - - - - YARV: Yet another RubyVM / Instruction Table - - - - - -

YARV: Instruction Table

- - - - - - - - - <%= tbl %> -
typeIndexInstructionOperandsStacks
- -
- SASADA Koichi / ko1 at atdot.net -
- - - - - - + + + + + + + + + + YARV: Yet another RubyVM / Instruction Table + + + + + +

YARV: Instruction Table

+ + + + + + + + + <%= tbl %> +
typeIndexInstructionOperandsStacks
+ +
+ SASADA Koichi / ko1 at atdot.net +
+ + + + + + diff --git a/template/minsns.inc.tmpl b/template/minsns.inc.tmpl index ed9247a126..f994c9b53b 100644 --- a/template/minsns.inc.tmpl +++ b/template/minsns.inc.tmpl @@ -1,14 +1,14 @@ -/** -*-c-*- - This file contains YARV instructions list, to define YARVCore::Instructions. - - ---- - This file is auto generated by insns2vm.rb - DO NOT TOUCH! - - If you want to fix something, you must edit 'template/minsns.inc.tmpl' - or insns2vm.rb - */ - -<%= defs %> - - +/** -*-c-*- + This file contains YARV instructions list, to define YARVCore::Instructions. + + ---- + This file is auto generated by insns2vm.rb + DO NOT TOUCH! + + If you want to fix something, you must edit 'template/minsns.inc.tmpl' + or insns2vm.rb + */ + +<%= defs %> + + diff --git a/template/opt_sc.inc.tmpl b/template/opt_sc.inc.tmpl index 69a5b36750..fc1c2f7184 100644 --- a/template/opt_sc.inc.tmpl +++ b/template/opt_sc.inc.tmpl @@ -1,32 +1,32 @@ -/* -*-c-*- *********************************************************/ -/*******************************************************************/ -/*******************************************************************/ -/** - This file is for threaded code. - - ---- - This file is auto generated by insns2vm.rb - DO NOT TOUCH! - - If you want to fix something, you must edit 'template/opt_sc.inc.tmpl' - or rb/insns2vm.rb - */ - -#define SC_STATE_SIZE 6 - -#define SCS_XX 1 -#define SCS_AX 2 -#define SCS_BX 3 -#define SCS_AB 4 -#define SCS_BA 5 - -#define SC_ERROR 0xffffffff - -static VALUE sc_insn_info[][SC_STATE_SIZE] = { -<%= sc_insn_info %> -}; - -static VALUE sc_insn_next[] = { -<%= sc_insn_next %> -}; - +/* -*-c-*- *********************************************************/ +/*******************************************************************/ +/*******************************************************************/ +/** + This file is for threaded code. + + ---- + This file is auto generated by insns2vm.rb + DO NOT TOUCH! + + If you want to fix something, you must edit 'template/opt_sc.inc.tmpl' + or rb/insns2vm.rb + */ + +#define SC_STATE_SIZE 6 + +#define SCS_XX 1 +#define SCS_AX 2 +#define SCS_BX 3 +#define SCS_AB 4 +#define SCS_BA 5 + +#define SC_ERROR 0xffffffff + +static VALUE sc_insn_info[][SC_STATE_SIZE] = { +<%= sc_insn_info %> +}; + +static VALUE sc_insn_next[] = { +<%= sc_insn_next %> +}; + diff --git a/template/optinsn.inc.tmpl b/template/optinsn.inc.tmpl index c268a97cef..c7828fe960 100644 --- a/template/optinsn.inc.tmpl +++ b/template/optinsn.inc.tmpl @@ -1,30 +1,30 @@ -/* -*-c-*- *********************************************************/ -/*******************************************************************/ -/*******************************************************************/ -/** - This file is for threaded code. - - ---- - This file is auto generated by insns2vm.rb - DO NOT TOUCH! - - If you want to fix something, you must edit 'template/optinsn.inc.tmpl' - or rb/insns2vm.rb - */ - -static INSN * -insn_operands_unification(INSN *insnobj){ -#ifdef OPT_OPERANDS_UNIFICATION - /* optimize rule */ - switch(insnobj->insn_id){ - -<%= rule %> - - default: - /* do nothing */; - break; - } -#endif - return insnobj; -} - +/* -*-c-*- *********************************************************/ +/*******************************************************************/ +/*******************************************************************/ +/** + This file is for threaded code. + + ---- + This file is auto generated by insns2vm.rb + DO NOT TOUCH! + + If you want to fix something, you must edit 'template/optinsn.inc.tmpl' + or rb/insns2vm.rb + */ + +static INSN * +insn_operands_unification(INSN *insnobj){ +#ifdef OPT_OPERANDS_UNIFICATION + /* optimize rule */ + switch(insnobj->insn_id){ + +<%= rule %> + + default: + /* do nothing */; + break; + } +#endif + return insnobj; +} + diff --git a/template/optunifs.inc.tmpl b/template/optunifs.inc.tmpl index d2b0e7699e..8a3ae3e897 100644 --- a/template/optunifs.inc.tmpl +++ b/template/optunifs.inc.tmpl @@ -1,35 +1,35 @@ -/* -*-c-*- *********************************************************/ -/*******************************************************************/ -/*******************************************************************/ -/** - This file is for threaded code. - - ---- - This file is auto generated by insns2vm.rb - DO NOT TOUCH! - - If you want to fix something, you must edit 'template/optunifs.inc.tmpl' - or rb/insns2vm.rb - */ - -/* - static int UNIFIED_insn_name_1[] = {id, size, ...}; - static int UNIFIED_insn_name_2[] = {id, size, ...}; - ... - - static *int UNIFIED_insn_name[] = {size, - UNIFIED_insn_name_1, - UNIFIED_insn_name_2, ...}; - ... - - static **int unified_insns_data[] = { - UNIFIED_insn_nameA, - UNIFIED_insn_nameB, ...}; - */ - -<%= unif_insns_each %> -<%= unif_insns %> -<%= unif_insns_data %> - -#undef GET_INSN_NAME - +/* -*-c-*- *********************************************************/ +/*******************************************************************/ +/*******************************************************************/ +/** + This file is for threaded code. + + ---- + This file is auto generated by insns2vm.rb + DO NOT TOUCH! + + If you want to fix something, you must edit 'template/optunifs.inc.tmpl' + or rb/insns2vm.rb + */ + +/* + static int UNIFIED_insn_name_1[] = {id, size, ...}; + static int UNIFIED_insn_name_2[] = {id, size, ...}; + ... + + static *int UNIFIED_insn_name[] = {size, + UNIFIED_insn_name_1, + UNIFIED_insn_name_2, ...}; + ... + + static **int unified_insns_data[] = { + UNIFIED_insn_nameA, + UNIFIED_insn_nameB, ...}; + */ + +<%= unif_insns_each %> +<%= unif_insns %> +<%= unif_insns_data %> + +#undef GET_INSN_NAME + diff --git a/template/vm.inc.tmpl b/template/vm.inc.tmpl index a641501697..8b49beb114 100644 --- a/template/vm.inc.tmpl +++ b/template/vm.inc.tmpl @@ -1,28 +1,28 @@ -/* -*-c-*- *********************************************************/ -/*******************************************************************/ -/*******************************************************************/ -/** - This file is VM main loop. - - ---- - This file is auto generated by insns2vm.rb - DO NOT TOUCH! - - If you want to fix something, you must edit 'insns.c' - */ - -<%= -ret = '' -offset = 15 -line_no = 0 -vm_body.each_line{|line| - if line =~ /^\#line __CURRENT_LINE__/ - ret << line.sub(/__CURRENT_LINE__/, "#{line_no+offset}") - else - ret < - +/* -*-c-*- *********************************************************/ +/*******************************************************************/ +/*******************************************************************/ +/** + This file is VM main loop. + + ---- + This file is auto generated by insns2vm.rb + DO NOT TOUCH! + + If you want to fix something, you must edit 'insns.c' + */ + +<%= +ret = '' +offset = 15 +line_no = 0 +vm_body.each_line{|line| + if line =~ /^\#line __CURRENT_LINE__/ + ret << line.sub(/__CURRENT_LINE__/, "#{line_no+offset}") + else + ret < + diff --git a/template/vmtc.inc.tmpl b/template/vmtc.inc.tmpl index e8f3c3cc11..244f7767c0 100644 --- a/template/vmtc.inc.tmpl +++ b/template/vmtc.inc.tmpl @@ -1,18 +1,18 @@ -/* -*-c-*- *********************************************************/ -/*******************************************************************/ -/*******************************************************************/ -/** - This file is for threaded code. - - ---- - This file is auto generated by insns2vm.rb - DO NOT TOUCH! - - If you want to fix something, you must edit 'template/vmtc.inc.tmpl' - or insns2vm.rb - */ - -static const void *insns_address_table[] = { -<%= insns_table %> -}; - +/* -*-c-*- *********************************************************/ +/*******************************************************************/ +/*******************************************************************/ +/** + This file is for threaded code. + + ---- + This file is auto generated by insns2vm.rb + DO NOT TOUCH! + + If you want to fix something, you must edit 'template/vmtc.inc.tmpl' + or insns2vm.rb + */ + +static const void *insns_address_table[] = { +<%= insns_table %> +}; + diff --git a/template/yarvarch.en b/template/yarvarch.en index 55fd3e1ae8..7a76e25b7e 100644 --- a/template/yarvarch.en +++ b/template/yarvarch.en @@ -1,7 +1,7 @@ -#title YARV: Yet another RubyVM - Software Architecture - -maybe writing. - -* YARV instruction set - -<%= d %> +#title YARV: Yet another RubyVM - Software Architecture + +maybe writing. + +* YARV instruction set + +<%= d %> diff --git a/template/yarvarch.ja b/template/yarvarch.ja index 05b19363aa..c332c20811 100644 --- a/template/yarvarch.ja +++ b/template/yarvarch.ja @@ -1,454 +1,454 @@ -#title YARVアーキテクチャ -#set author 日本 Ruby の会 ささだこういち - - -- 2005-03-03(Thu) 00:31:12 +0900 いろいろと書き直し - ----- - -* これは? - -[[YARV: Yet Another RubyVM|http://www.atdot.net/yarv]] の 設計メモです。 - - -YARV は、Ruby プログラムのための次の機能を提供します。 - -- Compiler -- VM Generator -- VM (Virtual Machine) -- Assembler -- Dis-Assembler -- (experimental) JIT Compiler -- (experimental) AOT Compiler - - -現在の YARV は Ruby インタプリタの拡張ライブラリとして実装しています。こ -れにより、Ruby インタプリタの必要な機能(パーサ、オブジェクト管理、既存 -の拡張ライブラリ)などがほぼそのまま利用できます。 - -ただし、いくつかのパッチを Ruby インタプリタに当てなければなりません。 - -今後は、Ruby 本体のインタプリタ部分(eval.c)を置き換えることを目指して -開発を継続する予定です。 - - -* Compiler (compile.h, compile.c) - -コンパイラは、Ruby インタプリタのパーサによって生成された構文木(RNode -データによる木)を YARV 命令列に変換します。YARV 命令については後述しま -す。 - -とくに難しいことはしていませんが、スコープなどの開始時にローカル変数の初 -期化などを行い、あとは構文木を辿り変換していきます。 - -変換中は Ruby の Array オブジェクトに YARV 命令オブジェクト、およびオペ -ランドを格納していき、最後に実行できる形に変換します。コンパイラでは、コ -ンパイル中に生成するメモリ領域の管理が問題になることがありますが、YARV -の場合、Ruby インタプリタがすべて面倒をみてくれるのでこの部分は非常に楽 -に作ることができました(ガーベージコレクタによって自動的にメモリ管理をし -てくれるため)。 - -YARV 命令は、命令を示す識別子、オペランドなど、すべて 1 word (マシンで -表現できる自然な値。C 言語ではポインタのサイズ。Ruby インタプリタ用語で -は VALUE のサイズ)で表現されます。そのため、YARV 命令はいわゆる「バイト -コード」ではありません。そのため、YARV の説明などでは「命令列」という用 -語を使っています。 - -1 word であるため、メモリの利用効率は多少悪くなりますが、アクセス速度な -どを考慮すると、本方式が一番いいと考えております。たとえばオペランドをコ -ンスタントプールに格納し、インデックスのみをオペランドで示すことも可能で -すが、間接アクセスになってしまうので性能に影響が出るため、却下しました。 - - -* VM Generator (rb/insns2vm.rb, insns.def) - -rb/insns2vm.rb というスクリプトは、insns.def というファイルを読み込み、 -VM のために必要なファイルを生成します。具体的には、命令を実行する部分を -生成しますが、ほかにもコンパイルに必要な情報、最適化に必要な情報、やアセ -ンブラ、逆アセンブラに必要な情報を示すファイルも生成します。 - - -** 命令記述 - -insns.def には、各命令がどのような命令であるかを記述します。具体的には次 -の情報を記述します。 - -- 命令の名前 -- その命令のカテゴリ、コメント(英語、日本語) -- オペランドの名前 -- その命令実行前にスタックからポップする値 -- その命令実行後にスタックにプッシュする値 -- その命令のロジック(C 言語で記述) - -たとえば、スタックに self をおく putself という命令は次のように記述しま -す。 - -#code -/** - @c put - @e put self. - @j self を置く。 - */ -DEFINE_INSN -putself -() -() -(VALUE val) -{ - val = GET_SELF(); -} -#end - -この場合、オペランドと、スタックからポップする値は無いことになります。命 -令終了後、self をスタックトップに置きたいわけですが、それは val という、 -スタックにプッシュする値として宣言しておいた変数に代入しておくことで、こ -れを変換するとスタックトップに置く C プログラムが生成されます。 - -細かいフォーマットは insns.def の冒頭を参照してください。そんなに難しく -ないと思います。 - -insnhelper.h というファイルに、命令ロジックを記述するために必要なマクロ -が定義されています。また、VM の内部構造に関する定義は vm.h というファイ -ルにあります。 - - -* VM (Virtual Machine, vm.h, vm.c) - -VM は、実際にコンパイルした結果生成される YARV 命令列を実行します。まさ -に、この部分が YARV のキモになり、将来的には eval.c をこの VM で置き換え -たいと考えています。 - -現在の Ruby インタプリタで実行できるすべてのことが、この VM で実現できる -ように作っています(現段階ではまだ完全ではありませんが、そうなるべきです)。 - -VM は、単純なスタックマシンとして実装しています。スレッドひとつにスタッ -クひとつを保持します。スタックの領域はヒープから取得するので、柔軟な領域 -設定が可能です。 - - -** レジスタ - -VM は 5 つの仮想的なレジスタによって制御されます。 - -- PC (Program Counter) -- SP (Stack Pointer) -- CFP (Control Frame Pointer) -- LFP (Local Frame Pointer) -- DFP (Dynamic Frame Pointer) - -PC は現在実行中の命令列の位置を示します。SP はスタックトップの位置を示し -ます。CFP、LFP、DFP はそれぞれフレームの情報を示します。詳細は後述します。 - - -** スタックフレーム - -obsolete (update soon) - - -** フレームデザインについての補足 - -Lisp の処理系などをかんがえると、わざわざブロックローカルフレームとメソ -ッドローカルフレームのようなものを用意するのは奇異に見えるかもしれません。 -あるフレームを、入れ子構造にして、ローカル変数のアクセスはその入れ子を外 -側に辿れば必ずたどり着くことができるからです(つまり、lfp は必要ない)。 - -しかし、Ruby ではいくつか状況が違います。まず、メソッドローカルな情報が -あること、具体的にはブロックとself(callee からみると reciever)です。こ -の情報をそれぞれのフレームにもたせるのは無駄です。 - -また、Ruby2.0 からはブロックローカル変数はなくなります(ブロックローカル -引数は残るので、構造自体はあまり変わりません)。そのため、メソッドローカ -ル変数へのアクセスが頻発することが予想されます。 - -このとき、メソッドローカル変数へのアクセスのたびにフレーム(スコープ)の -リストをたどるのは無駄であると判断し、明示的にメソッドローカルスコープと -ブロックフレームを分離し、ブロックフレームからはメソッドローカルフレーム -が lfpレジスタによって容易にアクセスできるようにしました。 - - -** メソッド呼び出しについて - -メソッド呼び出しは、YARV 命令列で記述されたメソッドか、C で記述されたメ -ソッドかによってディスパッチ手法が変わります。 - -YARV 命令列であった場合、上述したスタックフレームを作成して命令を継続し -ます。とくに VM の関数を再帰呼び出すすることは行ないません。 - -C で記述されたメソッドだった場合、単純にその関数を呼び出します(ただし、 -バックトレースを正しく生成するためにメソッド呼び出しの情報を付加してから -行ないます)。 - -このため、VM 用スタックを別途用意したものの、プログラムによってはマシン -スタックを使い切ってしまう可能性があります(C -> Ruby -> C -> ... という -呼び出しが続いた場合)。これは、現在では避けられない仕様となっています。 - - -** 例外 - -例外は、Java の JVM と同様に例外テーブルを用意することで実現します。例外 -が発生したら、当該フレームを、例外テーブルを検査します。そこで、例外が発 -生したときの PC の値に合致するエントリがあった場合、そのエントリに従って -動作します。もしエントリが見つからなかった場合、スタックを撒き戻してまた -同様にそのスコープの例外テーブルを検査します。 - -また、break、return(ブロック中)、retry なども同様の仕組みで実現します。 - -*** 例外テーブル - -例外テーブルエントリは具体的には次の情報が格納されています。 - -- 対象とする PC の範囲 -- 対象とする例外の種類 -- もし対象となったときにジャンプする先(種類による) -- もし対象となったときに起動するブロックの iseq - - -*** rescue - -rescue 節はブロックとして実現しています。$! の値を唯一の引数として持ちま -す。 - -#code -begin -rescue A -rescue B -rescue C -end -#end - -は、次のような Ruby スクリプトに変換されます。 - -#code -{|err| - case err - when A === err - when B === err - when C === err - else - raise # yarv の命令では throw - end -} -#end - - -*** ensure - -正常系(例外が発生しなかった場合)と異常系(例外が発生したときなど)の2 -種類の命令列が生成されます。正常系では、ただの連続したコード領域としてコ -ンパイルされます。また、異常系ではブロックとして実装します。最後は必ず -throw 命令で締めることになります。 - - -*** break, return(ブロック中)、retry - -break 文、ブロック中の return 文、retry 文は throw 命令としてコンパイル -されます。どこまで戻るかは、break をフックする例外テーブルのエントリが判 -断します。 - - -** 定数の検索 - -定数という名前なのに、Ruby ではコンパイル時に決定しません。というか、い -つまでも再定義可能になっています。 - -定数アクセスのためのRuby記述は次のようになります。 - -#code -Ruby表現: -expr::ID::...::ID -#end - -これは、yarv命令セットでは次のようになります。 - -#code -(expr) -getconstant ID -... -getconstant ID -#end - - -*** 定数検索パス - -もし expr が nil だった場合、定数検索パスに従って定数を検索します。この -挙動は今後 Ruby 2.0 に向けて変更される場合があります。 - -+ クラス、モジュールの動的ネスト関係(プログラムの字面上)をルートまで辿る -+ 継承関係をルート(Object)まで辿る - -このため、クラス、モジュールの動的ネスト関係を保存しなければなりません。 -このために、thread_object には klass_nest_stack というものを用意しました。 -これは、現在のネストの情報を保存します。 - -メソッド定義時、その現在のネスト情報をメソッド定義時に(dupして)加える -ことで、そのメソッドの実行時、そのネスト情報を参照することが可能になりま -す。 - -トップレベルでは、その情報はないことになります。 - -クラス/モジュール定義文実行時は、現在の情報そのものを参照することになり -ます。これは、クラススコープ突入時、その情報をクラス定義文にコピーします -(すでにコピーされていれば、これを行いません)。 - -これにより、動的なネスト情報を統一的に扱うことができます。 - - -** 最適化手法 - -YARV では高速化を目的としているので、さまざまな最適化手法を利用していま -す。詳細は割愛しますが、以下に述べる最適化などを行なっております。 - - -*** threaded code - -GCC の C 言語拡張である値としてのラベルを利用して direct threaded code -を実現しています。 - - -*** Peephole optimization - -いくつかの簡単な最適化をしています。 - - -*** inline method cache - -命令列の中にメソッド検索結果を埋め込みます。 - - -*** inline constant cache - -命令列の中に定数検索結果を埋め込みます。 - - -*** ブロックと Proc オブジェクトの分離 - -ブロック付きメソッド呼び出しが行なわれたときにはすぐにはブロックを Proc -オブジェクトとして生成しません。これにより、必要ない Proc オブジェクトの -生成を抑えています。 - -Proc メソッドは、実際に必要になった時点で作られ、そのときに環境(スコー -プ上に確保された変数など)をヒープに保存します。 - - -*** 特化命令 - -Fixnum 同士の加算などを正直に関数呼び出しによって行なうと、コストがかか -るので、これらのプリミティブな操作を行なうためのメソッド呼び出しは専用命 -令を用意しました。 - - -*** 命令融合 - -複数の命令を 1 命令に変換します。融合命令は opt_insn_unif.def の記述によ -り自動的に生成されます。 - - -*** オペランド融合 - -複数のオペランドを含めた命令を生成します。融合命令は opt_operand.def の -記述によって自動的に生成されます。 - - -*** stack caching - -スタックトップを仮想レジスタに保持するようにします。現在は 2 個の仮想レ -ジスタを想定し、5状態のスタックキャッシングを行ないます。スタックキャッ -シングする命令は自動的に生成されます。 - - -*** JIT Compile - -機械語を切り貼りします。非常に実験的なコードものしか作っておりません。ほ -とんどのプログラムは動きません。 - - -*** AOT Compile - -YARV 命令列を C 言語に変換します。まだ十分な最適化を行なえておりませんが、 -それなりに動きます。rb/aotc.rb がコンパイラです。 - - -* Assembler (rb/yasm.rb) - -YARV 命令列のアセンブラを用意しました。使い方は rb/yasm.rb を参照してく -ださい(まだ、例示してある生成手法のすべてをサポートしているわけではあり -ません)。 - - -* Dis-Assembler (disasm.c) - -YARV 命令列を示すオブジェクト YARVCore::InstructionSequence には disasm -メソッドがあります。これは、命令列を逆アセンブルした文字列を返します。 - - -* YARV 命令セット - -<%= d %> - -* その他 - -** テスト - -test/test_* がテストケースです。一応、ミスなく動くはずです。逆にいうと、 -このテストに記述されている例ではきちんと動作するということです。 - - -** ベンチマーク - -benchmark/bm_* にベンチマークプログラムがおいてあります。 - - -** 今後の予定 - -まだまだやらなければいけないこと、未実装部分がたくさんありますんでやって -いかなければなりません。一番大きな目標は eval.c を置き換えることでしょう -か。 - - -*** Verifier - -YARV 命令列は、ミスがあっても動かしてしまうため危険である可能性がありま -す。そのため、スタックの利用状態をきちんと事前に検証するようなベリファイ -アを用意しなければならないと考えています。 - - -*** Compiled File の構想 - -Ruby プログラムをこの命令セットにシリアライズしたデータ構造をファイルに -出力できるようにしたいと考えています。これを利用して一度コンパイルした命 -令列をファイルに保存しておけば、次回ロード時にはコンパイルの手間、コスト -を省くことができます。 - - -**** 全体構成 - -次のようなファイル構成を考えていますが、まだ未定です。 - -#code -u4 : 4 byte unsigned storage -u2 : 2 byte unsigned storage -u1 : 1 byte unsigned storage - -every storages are little endian :-) - -CompiledFile{ - u4 magic; - - u2 major; - u2 minor; - - u4 character_code; - - u4 constants_pool_count; - ConstantEntry constants_pool[constants_pool_count]; - - u4 block_count; - blockEntry blocks[block_count]; - - u4 method_count; - MethodEntry methods[method_count]; -} -#end - -Java classfile のパクリ。 - +#title YARVアーキテクチャ +#set author 日本 Ruby の会 ささだこういち + + +- 2005-03-03(Thu) 00:31:12 +0900 いろいろと書き直し + +---- + +* これは? + +[[YARV: Yet Another RubyVM|http://www.atdot.net/yarv]] の 設計メモです。 + + +YARV は、Ruby プログラムのための次の機能を提供します。 + +- Compiler +- VM Generator +- VM (Virtual Machine) +- Assembler +- Dis-Assembler +- (experimental) JIT Compiler +- (experimental) AOT Compiler + + +現在の YARV は Ruby インタプリタの拡張ライブラリとして実装しています。こ +れにより、Ruby インタプリタの必要な機能(パーサ、オブジェクト管理、既存 +の拡張ライブラリ)などがほぼそのまま利用できます。 + +ただし、いくつかのパッチを Ruby インタプリタに当てなければなりません。 + +今後は、Ruby 本体のインタプリタ部分(eval.c)を置き換えることを目指して +開発を継続する予定です。 + + +* Compiler (compile.h, compile.c) + +コンパイラは、Ruby インタプリタのパーサによって生成された構文木(RNode +データによる木)を YARV 命令列に変換します。YARV 命令については後述しま +す。 + +とくに難しいことはしていませんが、スコープなどの開始時にローカル変数の初 +期化などを行い、あとは構文木を辿り変換していきます。 + +変換中は Ruby の Array オブジェクトに YARV 命令オブジェクト、およびオペ +ランドを格納していき、最後に実行できる形に変換します。コンパイラでは、コ +ンパイル中に生成するメモリ領域の管理が問題になることがありますが、YARV +の場合、Ruby インタプリタがすべて面倒をみてくれるのでこの部分は非常に楽 +に作ることができました(ガーベージコレクタによって自動的にメモリ管理をし +てくれるため)。 + +YARV 命令は、命令を示す識別子、オペランドなど、すべて 1 word (マシンで +表現できる自然な値。C 言語ではポインタのサイズ。Ruby インタプリタ用語で +は VALUE のサイズ)で表現されます。そのため、YARV 命令はいわゆる「バイト +コード」ではありません。そのため、YARV の説明などでは「命令列」という用 +語を使っています。 + +1 word であるため、メモリの利用効率は多少悪くなりますが、アクセス速度な +どを考慮すると、本方式が一番いいと考えております。たとえばオペランドをコ +ンスタントプールに格納し、インデックスのみをオペランドで示すことも可能で +すが、間接アクセスになってしまうので性能に影響が出るため、却下しました。 + + +* VM Generator (rb/insns2vm.rb, insns.def) + +rb/insns2vm.rb というスクリプトは、insns.def というファイルを読み込み、 +VM のために必要なファイルを生成します。具体的には、命令を実行する部分を +生成しますが、ほかにもコンパイルに必要な情報、最適化に必要な情報、やアセ +ンブラ、逆アセンブラに必要な情報を示すファイルも生成します。 + + +** 命令記述 + +insns.def には、各命令がどのような命令であるかを記述します。具体的には次 +の情報を記述します。 + +- 命令の名前 +- その命令のカテゴリ、コメント(英語、日本語) +- オペランドの名前 +- その命令実行前にスタックからポップする値 +- その命令実行後にスタックにプッシュする値 +- その命令のロジック(C 言語で記述) + +たとえば、スタックに self をおく putself という命令は次のように記述しま +す。 + +#code +/** + @c put + @e put self. + @j self を置く。 + */ +DEFINE_INSN +putself +() +() +(VALUE val) +{ + val = GET_SELF(); +} +#end + +この場合、オペランドと、スタックからポップする値は無いことになります。命 +令終了後、self をスタックトップに置きたいわけですが、それは val という、 +スタックにプッシュする値として宣言しておいた変数に代入しておくことで、こ +れを変換するとスタックトップに置く C プログラムが生成されます。 + +細かいフォーマットは insns.def の冒頭を参照してください。そんなに難しく +ないと思います。 + +insnhelper.h というファイルに、命令ロジックを記述するために必要なマクロ +が定義されています。また、VM の内部構造に関する定義は vm.h というファイ +ルにあります。 + + +* VM (Virtual Machine, vm.h, vm.c) + +VM は、実際にコンパイルした結果生成される YARV 命令列を実行します。まさ +に、この部分が YARV のキモになり、将来的には eval.c をこの VM で置き換え +たいと考えています。 + +現在の Ruby インタプリタで実行できるすべてのことが、この VM で実現できる +ように作っています(現段階ではまだ完全ではありませんが、そうなるべきです)。 + +VM は、単純なスタックマシンとして実装しています。スレッドひとつにスタッ +クひとつを保持します。スタックの領域はヒープから取得するので、柔軟な領域 +設定が可能です。 + + +** レジスタ + +VM は 5 つの仮想的なレジスタによって制御されます。 + +- PC (Program Counter) +- SP (Stack Pointer) +- CFP (Control Frame Pointer) +- LFP (Local Frame Pointer) +- DFP (Dynamic Frame Pointer) + +PC は現在実行中の命令列の位置を示します。SP はスタックトップの位置を示し +ます。CFP、LFP、DFP はそれぞれフレームの情報を示します。詳細は後述します。 + + +** スタックフレーム + +obsolete (update soon) + + +** フレームデザインについての補足 + +Lisp の処理系などをかんがえると、わざわざブロックローカルフレームとメソ +ッドローカルフレームのようなものを用意するのは奇異に見えるかもしれません。 +あるフレームを、入れ子構造にして、ローカル変数のアクセスはその入れ子を外 +側に辿れば必ずたどり着くことができるからです(つまり、lfp は必要ない)。 + +しかし、Ruby ではいくつか状況が違います。まず、メソッドローカルな情報が +あること、具体的にはブロックとself(callee からみると reciever)です。こ +の情報をそれぞれのフレームにもたせるのは無駄です。 + +また、Ruby2.0 からはブロックローカル変数はなくなります(ブロックローカル +引数は残るので、構造自体はあまり変わりません)。そのため、メソッドローカ +ル変数へのアクセスが頻発することが予想されます。 + +このとき、メソッドローカル変数へのアクセスのたびにフレーム(スコープ)の +リストをたどるのは無駄であると判断し、明示的にメソッドローカルスコープと +ブロックフレームを分離し、ブロックフレームからはメソッドローカルフレーム +が lfpレジスタによって容易にアクセスできるようにしました。 + + +** メソッド呼び出しについて + +メソッド呼び出しは、YARV 命令列で記述されたメソッドか、C で記述されたメ +ソッドかによってディスパッチ手法が変わります。 + +YARV 命令列であった場合、上述したスタックフレームを作成して命令を継続し +ます。とくに VM の関数を再帰呼び出すすることは行ないません。 + +C で記述されたメソッドだった場合、単純にその関数を呼び出します(ただし、 +バックトレースを正しく生成するためにメソッド呼び出しの情報を付加してから +行ないます)。 + +このため、VM 用スタックを別途用意したものの、プログラムによってはマシン +スタックを使い切ってしまう可能性があります(C -> Ruby -> C -> ... という +呼び出しが続いた場合)。これは、現在では避けられない仕様となっています。 + + +** 例外 + +例外は、Java の JVM と同様に例外テーブルを用意することで実現します。例外 +が発生したら、当該フレームを、例外テーブルを検査します。そこで、例外が発 +生したときの PC の値に合致するエントリがあった場合、そのエントリに従って +動作します。もしエントリが見つからなかった場合、スタックを撒き戻してまた +同様にそのスコープの例外テーブルを検査します。 + +また、break、return(ブロック中)、retry なども同様の仕組みで実現します。 + +*** 例外テーブル + +例外テーブルエントリは具体的には次の情報が格納されています。 + +- 対象とする PC の範囲 +- 対象とする例外の種類 +- もし対象となったときにジャンプする先(種類による) +- もし対象となったときに起動するブロックの iseq + + +*** rescue + +rescue 節はブロックとして実現しています。$! の値を唯一の引数として持ちま +す。 + +#code +begin +rescue A +rescue B +rescue C +end +#end + +は、次のような Ruby スクリプトに変換されます。 + +#code +{|err| + case err + when A === err + when B === err + when C === err + else + raise # yarv の命令では throw + end +} +#end + + +*** ensure + +正常系(例外が発生しなかった場合)と異常系(例外が発生したときなど)の2 +種類の命令列が生成されます。正常系では、ただの連続したコード領域としてコ +ンパイルされます。また、異常系ではブロックとして実装します。最後は必ず +throw 命令で締めることになります。 + + +*** break, return(ブロック中)、retry + +break 文、ブロック中の return 文、retry 文は throw 命令としてコンパイル +されます。どこまで戻るかは、break をフックする例外テーブルのエントリが判 +断します。 + + +** 定数の検索 + +定数という名前なのに、Ruby ではコンパイル時に決定しません。というか、い +つまでも再定義可能になっています。 + +定数アクセスのためのRuby記述は次のようになります。 + +#code +Ruby表現: +expr::ID::...::ID +#end + +これは、yarv命令セットでは次のようになります。 + +#code +(expr) +getconstant ID +... +getconstant ID +#end + + +*** 定数検索パス + +もし expr が nil だった場合、定数検索パスに従って定数を検索します。この +挙動は今後 Ruby 2.0 に向けて変更される場合があります。 + ++ クラス、モジュールの動的ネスト関係(プログラムの字面上)をルートまで辿る ++ 継承関係をルート(Object)まで辿る + +このため、クラス、モジュールの動的ネスト関係を保存しなければなりません。 +このために、thread_object には klass_nest_stack というものを用意しました。 +これは、現在のネストの情報を保存します。 + +メソッド定義時、その現在のネスト情報をメソッド定義時に(dupして)加える +ことで、そのメソッドの実行時、そのネスト情報を参照することが可能になりま +す。 + +トップレベルでは、その情報はないことになります。 + +クラス/モジュール定義文実行時は、現在の情報そのものを参照することになり +ます。これは、クラススコープ突入時、その情報をクラス定義文にコピーします +(すでにコピーされていれば、これを行いません)。 + +これにより、動的なネスト情報を統一的に扱うことができます。 + + +** 最適化手法 + +YARV では高速化を目的としているので、さまざまな最適化手法を利用していま +す。詳細は割愛しますが、以下に述べる最適化などを行なっております。 + + +*** threaded code + +GCC の C 言語拡張である値としてのラベルを利用して direct threaded code +を実現しています。 + + +*** Peephole optimization + +いくつかの簡単な最適化をしています。 + + +*** inline method cache + +命令列の中にメソッド検索結果を埋め込みます。 + + +*** inline constant cache + +命令列の中に定数検索結果を埋め込みます。 + + +*** ブロックと Proc オブジェクトの分離 + +ブロック付きメソッド呼び出しが行なわれたときにはすぐにはブロックを Proc +オブジェクトとして生成しません。これにより、必要ない Proc オブジェクトの +生成を抑えています。 + +Proc メソッドは、実際に必要になった時点で作られ、そのときに環境(スコー +プ上に確保された変数など)をヒープに保存します。 + + +*** 特化命令 + +Fixnum 同士の加算などを正直に関数呼び出しによって行なうと、コストがかか +るので、これらのプリミティブな操作を行なうためのメソッド呼び出しは専用命 +令を用意しました。 + + +*** 命令融合 + +複数の命令を 1 命令に変換します。融合命令は opt_insn_unif.def の記述によ +り自動的に生成されます。 + + +*** オペランド融合 + +複数のオペランドを含めた命令を生成します。融合命令は opt_operand.def の +記述によって自動的に生成されます。 + + +*** stack caching + +スタックトップを仮想レジスタに保持するようにします。現在は 2 個の仮想レ +ジスタを想定し、5状態のスタックキャッシングを行ないます。スタックキャッ +シングする命令は自動的に生成されます。 + + +*** JIT Compile + +機械語を切り貼りします。非常に実験的なコードものしか作っておりません。ほ +とんどのプログラムは動きません。 + + +*** AOT Compile + +YARV 命令列を C 言語に変換します。まだ十分な最適化を行なえておりませんが、 +それなりに動きます。rb/aotc.rb がコンパイラです。 + + +* Assembler (rb/yasm.rb) + +YARV 命令列のアセンブラを用意しました。使い方は rb/yasm.rb を参照してく +ださい(まだ、例示してある生成手法のすべてをサポートしているわけではあり +ません)。 + + +* Dis-Assembler (disasm.c) + +YARV 命令列を示すオブジェクト YARVCore::InstructionSequence には disasm +メソッドがあります。これは、命令列を逆アセンブルした文字列を返します。 + + +* YARV 命令セット + +<%= d %> + +* その他 + +** テスト + +test/test_* がテストケースです。一応、ミスなく動くはずです。逆にいうと、 +このテストに記述されている例ではきちんと動作するということです。 + + +** ベンチマーク + +benchmark/bm_* にベンチマークプログラムがおいてあります。 + + +** 今後の予定 + +まだまだやらなければいけないこと、未実装部分がたくさんありますんでやって +いかなければなりません。一番大きな目標は eval.c を置き換えることでしょう +か。 + + +*** Verifier + +YARV 命令列は、ミスがあっても動かしてしまうため危険である可能性がありま +す。そのため、スタックの利用状態をきちんと事前に検証するようなベリファイ +アを用意しなければならないと考えています。 + + +*** Compiled File の構想 + +Ruby プログラムをこの命令セットにシリアライズしたデータ構造をファイルに +出力できるようにしたいと考えています。これを利用して一度コンパイルした命 +令列をファイルに保存しておけば、次回ロード時にはコンパイルの手間、コスト +を省くことができます。 + + +**** 全体構成 + +次のようなファイル構成を考えていますが、まだ未定です。 + +#code +u4 : 4 byte unsigned storage +u2 : 2 byte unsigned storage +u1 : 1 byte unsigned storage + +every storages are little endian :-) + +CompiledFile{ + u4 magic; + + u2 major; + u2 minor; + + u4 character_code; + + u4 constants_pool_count; + ConstantEntry constants_pool[constants_pool_count]; + + u4 block_count; + blockEntry blocks[block_count]; + + u4 method_count; + MethodEntry methods[method_count]; +} +#end + +Java classfile のパクリ。 + diff --git a/template/yasmdata.rb.tmpl b/template/yasmdata.rb.tmpl index 40cbfdcaf7..2e005deb64 100644 --- a/template/yasmdata.rb.tmpl +++ b/template/yasmdata.rb.tmpl @@ -1,20 +1,20 @@ -# -*-ruby-*- -# - -module YARVCore - class InstructionSequence - class Instruction - InsnID2NO = { -<%= insn_id2no %> - } - - def self.id2insn_no id - if InsnID2NO.has_key? id - InsnID2NO[id] - end - end - end - end -end - - +# -*-ruby-*- +# + +module YARVCore + class InstructionSequence + class Instruction + InsnID2NO = { +<%= insn_id2no %> + } + + def self.id2insn_no id + if InsnID2NO.has_key? id + InsnID2NO[id] + end + end + end + end +end + + diff --git a/vm_macro.def b/vm_macro.def index dd7fd3daa8..8a2eb83dbb 100644 --- a/vm_macro.def +++ b/vm_macro.def @@ -1,330 +1,330 @@ -/* -*- c -*- */ -/* do not use C++ style comment */ -/* */ - - -MACRO macro_eval_setup_send_arguments(num, blockptr, flag, blockiseq) -{ - if (flag & VM_CALL_ARGS_BLOCKARG_BIT) { - yarv_proc_t *po; - VALUE proc; - - proc = TOPN(0); - if (proc != Qnil) { - if (!yarv_obj_is_proc(proc)) { - proc = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc"); - if (!yarv_obj_is_proc(proc)) { - rb_raise(rb_eTypeError, - "wrong argument type %s (expected Proc)", - rb_obj_classname(proc)); - } - } - GetProcPtr(proc, po); - blockptr = &po->block; - GET_BLOCK_PTR_IN_CFP(reg_cfp)->proc = proc; - } - INC_SP(-1); - } - else if (blockiseq) { - blockptr = GET_BLOCK_PTR_IN_CFP(reg_cfp); - blockptr->iseq = blockiseq; - blockptr->proc = 0; - } - - /* expand top of stack? */ - if (flag & VM_CALL_ARGS_SPLAT_BIT) { - VALUE ary = TOPN(0); - VALUE *ptr, *dst; - int i; - VALUE tmp = rb_check_convert_type(ary, T_ARRAY, "Array", "to_splat"); - - if (NIL_P(tmp)) { - tmp = rb_ary_new3(1, ary); - } - ary = tmp; - - ptr = RARRAY_PTR(ary); - dst = GET_SP() - 1; - for (i = 0; i < RARRAY_LEN(ary); i++) { - dst[i] = ptr[i]; - } - num += i - 1; - INC_SP(i - 1); - } -} - - -MACRO macro_eval_invoke_cfunc(num, id, recv, klass, mn, blockptr) -{ - yarv_control_frame_t *cfp = - push_frame(th, 0, FRAME_MAGIC_CFUNC, - recv, (VALUE) blockptr, 0, GET_SP(), 0, 1); - cfp->callee_id = id; /* TODO */ - cfp->method_id = id; - cfp->method_klass = klass; - - reg_cfp->sp -= num + 1; - - val = call_cfunc(mn->nd_cfnc, recv, mn->nd_argc, num, reg_cfp->sp + 1); - if (reg_cfp != th->cfp + 1) { - SDR2(reg_cfp); - SDR2(th->cfp-5); - rb_bug("cfp consistency error - send"); - th->cfp = reg_cfp; - } - pop_frame(th); -} - -MACRO macro_eval_invoke_func(niseqval, recv, klass, blockptr, num) -{ - yarv_iseq_t *niseq; - VALUE *sp = GET_SP(); - VALUE *rsp = sp - num - 1; - int opt_pc = 0, clear_local_size, i; - - /* TODO: eliminate it */ - GetISeqPtr(niseqval, niseq); - - clear_local_size = niseq->local_size - num; - /* set arguments */ - if (niseq->arg_simple) { - if (niseq->argc != num) { - rb_raise(rb_eArgError, "wrong number of arguments (%lu for %d)", - (unsigned long)num, niseq->argc); - } - } - else { - /* check optional arguments */ - if (niseq->arg_opts) { - int iseq_argc = niseq->argc; - int opts = niseq->arg_opts - 1; - - if (num < iseq_argc || - (niseq->arg_rest == 0 && num > iseq_argc + opts)) { - if (0) { - printf("num: %lu, iseq_argc: %d, opts: %d\n", - (unsigned long)num, iseq_argc, opts); - } - rb_raise(rb_eArgError, - "wrong number of arguments (%lu for %d)", - (unsigned long)num, iseq_argc); - } - - if (0) { - printf("num: %lu, opts: %d, iseq_argc: %d\n", - (unsigned long)num, opts, iseq_argc); - } - if (num - iseq_argc < opts) { - opt_pc = niseq->arg_opt_tbl[num - iseq_argc]; - sp += opts - (num - iseq_argc); - num += opts - (num - iseq_argc); - clear_local_size = niseq->local_size - (iseq_argc + opts); - } - else { - opt_pc = niseq->arg_opt_tbl[opts]; - } - } - /* check rest */ - if (niseq->arg_rest == -1) { - if (niseq->arg_opts) { - num = niseq->argc + niseq->arg_opts; - } - else { - num = niseq->argc; - } - sp = &rsp[1 + num + 1]; - } - else if (niseq->arg_rest != 0) { - int rest = niseq->arg_rest - 1; - int pack_size = num - rest; - if (0) { - printf("num: %lu, rest: %d, ps: %d\n", - (unsigned long)num, niseq->arg_rest, pack_size); - } - if (pack_size < 0) { - rb_raise(rb_eArgError, - "wrong number of arguments (%lu for %d)", - (unsigned long)num, rest - niseq->arg_opts); - } - - /* - * def m(x,y,z,*a) => - * x, y, z, a, b, c => x, y, z, [a,b,c], - */ - rsp[1 + rest] = rb_ary_new4(pack_size, &rsp[1 + rest]); - sp = &rsp[2 + rest]; - num = rest + 1; - clear_local_size = niseq->local_size - rest - 1; - } - - /* block argument */ - if (niseq->arg_block != 0) { - VALUE arg_block_val = Qnil; - - if (!((niseq->arg_rest && num == niseq->arg_rest) || - (niseq->arg_opts - && num == niseq->argc + niseq->arg_opts - 1) - || num == niseq->argc)) { - rb_raise(rb_eArgError, - "wrong number of arguments (%lu for %d)", - (unsigned long)num, niseq->argc); - } - - if (blockptr) { - /* make Proc object */ - if (blockptr->proc == 0) { - yarv_proc_t *proc; - reg_cfp->sp = sp; - arg_block_val = th_make_proc(th, GET_CFP(), blockptr); - GetProcPtr(arg_block_val, proc); - blockptr = &proc->block; - } - else { - arg_block_val = blockptr->proc; - } - } - - rsp[1 + niseq->arg_block - 1] = arg_block_val; - sp++; - clear_local_size--; - } - } - /* stack overflow check */ - if (CHECK_STACK_OVERFLOW(th, GET_CFP(), niseq->stack_max + 0x100)) { - rb_exc_raise(sysstack_error); - } - - for (i = 0; i < clear_local_size; i++) { - *sp++ = Qnil; - } - - { - if (0 && (flag & VM_CALL_TAILCALL_BIT)) { - th->cfp++; - push_frame(th, niseq, FRAME_MAGIC_METHOD, - recv, (VALUE) blockptr, - niseq->iseq_encoded + opt_pc, sp, 0, 0); - } - else if (0 && - (flag & VM_CALL_TAILRECURSION_BIT) && niseq == GET_ISEQ()) { - /* do nothing */ - GET_CFP()->self = recv; - SET_LFP(sp); - SET_DFP(sp); - *sp++ = (VALUE) blockptr; - reg_cfp->sp = sp; - reg_cfp->bp = sp; - SET_PC(niseq->iseq_encoded + opt_pc); - } - else { - push_frame(th, niseq, - FRAME_MAGIC_METHOD, recv, (VALUE) blockptr, - niseq->iseq_encoded + opt_pc, sp, 0, 0); - reg_cfp->sp = rsp; - } - RESTORE_REGS(); - } -} - -MACRO macro_eval_invoke_method(recv, klass, id, num, mn, blockptr) -{ - /* method missing */ - if (mn == 0) { - /* temporarily */ - if (id == idMethodMissing) { - rb_bug("method missing"); - } - else { - int stat = 0; - if (flag & VM_CALL_VCALL_BIT) { - stat |= NOEX_VCALL; - } - if (flag & VM_CALL_SUPER_BIT) { - stat |= NOEX_SUPER; - } - val = eval_method_missing(th, id, recv, num, blockptr, stat); - } - } - else if (!(flag & VM_CALL_FCALL_BIT) && - (mn->nd_noex & NOEX_MASK) & NOEX_PRIVATE) { - int stat = NOEX_PRIVATE; - if (flag & VM_CALL_VCALL_BIT) { - stat |= NOEX_VCALL; - } - val = eval_method_missing(th, id, recv, num, blockptr, stat); - } - else if ((mn->nd_noex & NOEX_MASK) & NOEX_PROTECTED) { - VALUE defined_class = mn->nd_clss; - - if (TYPE(defined_class) == T_ICLASS) { - defined_class = RBASIC(defined_class)->klass; - } - if (!rb_obj_is_kind_of(GET_SELF(), rb_class_real(defined_class))) { - val = - eval_method_missing(th, id, recv, num, blockptr, - NOEX_PROTECTED); - } - else { - goto INSN_LABEL(normal_method_dispatch); - } - } - else { - NODE *node; - INSN_LABEL(normal_method_dispatch): - - node = mn->nd_body; - switch (nd_type(node)) { - case YARV_METHOD_NODE:{ - macro_eval_invoke_func(node->nd_body, recv, klass, - blockptr, num); - NEXT_INSN(); - } - case NODE_CFUNC:{ - macro_eval_invoke_cfunc(num, id, recv, klass, node, blockptr); - break; - } - case NODE_ATTRSET:{ - val = rb_ivar_set(recv, node->nd_vid, TOPN(0)); - POPN(2); - break; - } - case NODE_IVAR:{ - val = rb_ivar_get(recv, node->nd_vid); - POP(); - break; - } - case NODE_BMETHOD:{ - VALUE *argv = GET_SP() - num; - val = th_invoke_bmethod(th, id, node->nd_cval, - recv, klass, num, argv); - INC_SP(-num-1); - break; - } - case NODE_ZSUPER:{ - klass = RCLASS(mn->nd_clss)->super; - mn = rb_method_node(klass, id); - - if (mn != 0) { - goto INSN_LABEL(normal_method_dispatch); - } - else { - goto LABEL_IS_SC(start_method_dispatch); - } - } - case NODE_SCOPE:{ - dpi(id); - SDR(); - rb_bug("eval_invoke_method: NODE_SCOPE should not be appear"); - /* unreachable */ - break; - } - default:{ - printf("node: %s\n", node_name(nd_type(node))); - rb_bug("eval_invoke_method: unreachable"); - /* unreachable */ - break; - } - } - } -} - +/* -*- c -*- */ +/* do not use C++ style comment */ +/* */ + + +MACRO macro_eval_setup_send_arguments(num, blockptr, flag, blockiseq) +{ + if (flag & VM_CALL_ARGS_BLOCKARG_BIT) { + yarv_proc_t *po; + VALUE proc; + + proc = TOPN(0); + if (proc != Qnil) { + if (!yarv_obj_is_proc(proc)) { + proc = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc"); + if (!yarv_obj_is_proc(proc)) { + rb_raise(rb_eTypeError, + "wrong argument type %s (expected Proc)", + rb_obj_classname(proc)); + } + } + GetProcPtr(proc, po); + blockptr = &po->block; + GET_BLOCK_PTR_IN_CFP(reg_cfp)->proc = proc; + } + INC_SP(-1); + } + else if (blockiseq) { + blockptr = GET_BLOCK_PTR_IN_CFP(reg_cfp); + blockptr->iseq = blockiseq; + blockptr->proc = 0; + } + + /* expand top of stack? */ + if (flag & VM_CALL_ARGS_SPLAT_BIT) { + VALUE ary = TOPN(0); + VALUE *ptr, *dst; + int i; + VALUE tmp = rb_check_convert_type(ary, T_ARRAY, "Array", "to_splat"); + + if (NIL_P(tmp)) { + tmp = rb_ary_new3(1, ary); + } + ary = tmp; + + ptr = RARRAY_PTR(ary); + dst = GET_SP() - 1; + for (i = 0; i < RARRAY_LEN(ary); i++) { + dst[i] = ptr[i]; + } + num += i - 1; + INC_SP(i - 1); + } +} + + +MACRO macro_eval_invoke_cfunc(num, id, recv, klass, mn, blockptr) +{ + yarv_control_frame_t *cfp = + push_frame(th, 0, FRAME_MAGIC_CFUNC, + recv, (VALUE) blockptr, 0, GET_SP(), 0, 1); + cfp->callee_id = id; /* TODO */ + cfp->method_id = id; + cfp->method_klass = klass; + + reg_cfp->sp -= num + 1; + + val = call_cfunc(mn->nd_cfnc, recv, mn->nd_argc, num, reg_cfp->sp + 1); + if (reg_cfp != th->cfp + 1) { + SDR2(reg_cfp); + SDR2(th->cfp-5); + rb_bug("cfp consistency error - send"); + th->cfp = reg_cfp; + } + pop_frame(th); +} + +MACRO macro_eval_invoke_func(niseqval, recv, klass, blockptr, num) +{ + yarv_iseq_t *niseq; + VALUE *sp = GET_SP(); + VALUE *rsp = sp - num - 1; + int opt_pc = 0, clear_local_size, i; + + /* TODO: eliminate it */ + GetISeqPtr(niseqval, niseq); + + clear_local_size = niseq->local_size - num; + /* set arguments */ + if (niseq->arg_simple) { + if (niseq->argc != num) { + rb_raise(rb_eArgError, "wrong number of arguments (%lu for %d)", + (unsigned long)num, niseq->argc); + } + } + else { + /* check optional arguments */ + if (niseq->arg_opts) { + int iseq_argc = niseq->argc; + int opts = niseq->arg_opts - 1; + + if (num < iseq_argc || + (niseq->arg_rest == 0 && num > iseq_argc + opts)) { + if (0) { + printf("num: %lu, iseq_argc: %d, opts: %d\n", + (unsigned long)num, iseq_argc, opts); + } + rb_raise(rb_eArgError, + "wrong number of arguments (%lu for %d)", + (unsigned long)num, iseq_argc); + } + + if (0) { + printf("num: %lu, opts: %d, iseq_argc: %d\n", + (unsigned long)num, opts, iseq_argc); + } + if (num - iseq_argc < opts) { + opt_pc = niseq->arg_opt_tbl[num - iseq_argc]; + sp += opts - (num - iseq_argc); + num += opts - (num - iseq_argc); + clear_local_size = niseq->local_size - (iseq_argc + opts); + } + else { + opt_pc = niseq->arg_opt_tbl[opts]; + } + } + /* check rest */ + if (niseq->arg_rest == -1) { + if (niseq->arg_opts) { + num = niseq->argc + niseq->arg_opts; + } + else { + num = niseq->argc; + } + sp = &rsp[1 + num + 1]; + } + else if (niseq->arg_rest != 0) { + int rest = niseq->arg_rest - 1; + int pack_size = num - rest; + if (0) { + printf("num: %lu, rest: %d, ps: %d\n", + (unsigned long)num, niseq->arg_rest, pack_size); + } + if (pack_size < 0) { + rb_raise(rb_eArgError, + "wrong number of arguments (%lu for %d)", + (unsigned long)num, rest - niseq->arg_opts); + } + + /* + * def m(x,y,z,*a) => + * x, y, z, a, b, c => x, y, z, [a,b,c], + */ + rsp[1 + rest] = rb_ary_new4(pack_size, &rsp[1 + rest]); + sp = &rsp[2 + rest]; + num = rest + 1; + clear_local_size = niseq->local_size - rest - 1; + } + + /* block argument */ + if (niseq->arg_block != 0) { + VALUE arg_block_val = Qnil; + + if (!((niseq->arg_rest && num == niseq->arg_rest) || + (niseq->arg_opts + && num == niseq->argc + niseq->arg_opts - 1) + || num == niseq->argc)) { + rb_raise(rb_eArgError, + "wrong number of arguments (%lu for %d)", + (unsigned long)num, niseq->argc); + } + + if (blockptr) { + /* make Proc object */ + if (blockptr->proc == 0) { + yarv_proc_t *proc; + reg_cfp->sp = sp; + arg_block_val = th_make_proc(th, GET_CFP(), blockptr); + GetProcPtr(arg_block_val, proc); + blockptr = &proc->block; + } + else { + arg_block_val = blockptr->proc; + } + } + + rsp[1 + niseq->arg_block - 1] = arg_block_val; + sp++; + clear_local_size--; + } + } + /* stack overflow check */ + if (CHECK_STACK_OVERFLOW(th, GET_CFP(), niseq->stack_max + 0x100)) { + rb_exc_raise(sysstack_error); + } + + for (i = 0; i < clear_local_size; i++) { + *sp++ = Qnil; + } + + { + if (0 && (flag & VM_CALL_TAILCALL_BIT)) { + th->cfp++; + push_frame(th, niseq, FRAME_MAGIC_METHOD, + recv, (VALUE) blockptr, + niseq->iseq_encoded + opt_pc, sp, 0, 0); + } + else if (0 && + (flag & VM_CALL_TAILRECURSION_BIT) && niseq == GET_ISEQ()) { + /* do nothing */ + GET_CFP()->self = recv; + SET_LFP(sp); + SET_DFP(sp); + *sp++ = (VALUE) blockptr; + reg_cfp->sp = sp; + reg_cfp->bp = sp; + SET_PC(niseq->iseq_encoded + opt_pc); + } + else { + push_frame(th, niseq, + FRAME_MAGIC_METHOD, recv, (VALUE) blockptr, + niseq->iseq_encoded + opt_pc, sp, 0, 0); + reg_cfp->sp = rsp; + } + RESTORE_REGS(); + } +} + +MACRO macro_eval_invoke_method(recv, klass, id, num, mn, blockptr) +{ + /* method missing */ + if (mn == 0) { + /* temporarily */ + if (id == idMethodMissing) { + rb_bug("method missing"); + } + else { + int stat = 0; + if (flag & VM_CALL_VCALL_BIT) { + stat |= NOEX_VCALL; + } + if (flag & VM_CALL_SUPER_BIT) { + stat |= NOEX_SUPER; + } + val = eval_method_missing(th, id, recv, num, blockptr, stat); + } + } + else if (!(flag & VM_CALL_FCALL_BIT) && + (mn->nd_noex & NOEX_MASK) & NOEX_PRIVATE) { + int stat = NOEX_PRIVATE; + if (flag & VM_CALL_VCALL_BIT) { + stat |= NOEX_VCALL; + } + val = eval_method_missing(th, id, recv, num, blockptr, stat); + } + else if ((mn->nd_noex & NOEX_MASK) & NOEX_PROTECTED) { + VALUE defined_class = mn->nd_clss; + + if (TYPE(defined_class) == T_ICLASS) { + defined_class = RBASIC(defined_class)->klass; + } + if (!rb_obj_is_kind_of(GET_SELF(), rb_class_real(defined_class))) { + val = + eval_method_missing(th, id, recv, num, blockptr, + NOEX_PROTECTED); + } + else { + goto INSN_LABEL(normal_method_dispatch); + } + } + else { + NODE *node; + INSN_LABEL(normal_method_dispatch): + + node = mn->nd_body; + switch (nd_type(node)) { + case YARV_METHOD_NODE:{ + macro_eval_invoke_func(node->nd_body, recv, klass, + blockptr, num); + NEXT_INSN(); + } + case NODE_CFUNC:{ + macro_eval_invoke_cfunc(num, id, recv, klass, node, blockptr); + break; + } + case NODE_ATTRSET:{ + val = rb_ivar_set(recv, node->nd_vid, TOPN(0)); + POPN(2); + break; + } + case NODE_IVAR:{ + val = rb_ivar_get(recv, node->nd_vid); + POP(); + break; + } + case NODE_BMETHOD:{ + VALUE *argv = GET_SP() - num; + val = th_invoke_bmethod(th, id, node->nd_cval, + recv, klass, num, argv); + INC_SP(-num-1); + break; + } + case NODE_ZSUPER:{ + klass = RCLASS(mn->nd_clss)->super; + mn = rb_method_node(klass, id); + + if (mn != 0) { + goto INSN_LABEL(normal_method_dispatch); + } + else { + goto LABEL_IS_SC(start_method_dispatch); + } + } + case NODE_SCOPE:{ + dpi(id); + SDR(); + rb_bug("eval_invoke_method: NODE_SCOPE should not be appear"); + /* unreachable */ + break; + } + default:{ + printf("node: %s\n", node_name(nd_type(node))); + rb_bug("eval_invoke_method: unreachable"); + /* unreachable */ + break; + } + } + } +} + diff --git a/vm_opts.h.base b/vm_opts.h.base index 188b6d5d0a..9635dc8ac9 100644 --- a/vm_opts.h.base +++ b/vm_opts.h.base @@ -1,47 +1,47 @@ -/*-*-c-*-*/ -/********************************************************************** - - vm_opts.h.base - VM optimize option - - $Author: $ - $Date: $ - - Copyright (C) 2004-2006 Koichi Sasada - -**********************************************************************/ - - -#ifndef VM_OPTS_H_INCLUDED -#define VM_OPTS_H_INCLUDED - -/* C compiler depend */ -#define OPT_DIRECT_THREADED_CODE 1 -#define OPT_CALL_THREADED_CODE 0 - -/* architecture independent */ - -/* VM running option */ -#define OPT_CHECKED_RUN 1 - -/* at compile */ -#define OPT_INLINE_CONST_CACHE 1 -#define OPT_PEEPHOLE_OPTIMIZATION 1 -#define OPT_SPECIALISED_INSTRUCTION 1 - -/* at runtime */ -#define OPT_INLINE_METHOD_CACHE 1 -#define OPT_BLOCKINLINING 0 - -/* architecture independent, affects generated code */ -#define OPT_OPERANDS_UNIFICATION 0 -#define OPT_INSTRUCTIONS_UNIFICATION 0 - -/* code generation parameter */ -#define OPT_UNIFY_ALL_COMBINATION 0 -#define OPT_STACK_CACHING 0 - -/* misc */ -#define SUPPORT_JOKE 0 - -#endif /* VM_OPTS_H_INCLUDED */ - +/*-*-c-*-*/ +/********************************************************************** + + vm_opts.h.base - VM optimize option + + $Author$ + $Date$ + + Copyright (C) 2004-2006 Koichi Sasada + +**********************************************************************/ + + +#ifndef VM_OPTS_H_INCLUDED +#define VM_OPTS_H_INCLUDED + +/* C compiler depend */ +#define OPT_DIRECT_THREADED_CODE 1 +#define OPT_CALL_THREADED_CODE 0 + +/* architecture independent */ + +/* VM running option */ +#define OPT_CHECKED_RUN 1 + +/* at compile */ +#define OPT_INLINE_CONST_CACHE 1 +#define OPT_PEEPHOLE_OPTIMIZATION 1 +#define OPT_SPECIALISED_INSTRUCTION 1 + +/* at runtime */ +#define OPT_INLINE_METHOD_CACHE 1 +#define OPT_BLOCKINLINING 0 + +/* architecture independent, affects generated code */ +#define OPT_OPERANDS_UNIFICATION 0 +#define OPT_INSTRUCTIONS_UNIFICATION 0 + +/* code generation parameter */ +#define OPT_UNIFY_ALL_COMBINATION 0 +#define OPT_STACK_CACHING 0 + +/* misc */ +#define SUPPORT_JOKE 0 + +#endif /* VM_OPTS_H_INCLUDED */ +