From fbebd502f9f374d1eef31c63c10c7d8adcd63280 Mon Sep 17 00:00:00 2001 From: ko1 Date: Sun, 2 Nov 2014 18:02:55 +0000 Subject: * rewrite method/block parameter fitting logic to optimize keyword arguments/parameters and a splat argument. [Feature #10440] (Details are described in this ticket) Most of complex part is moved to vm_args.c. Now, ISeq#to_a does not catch up new instruction format. * vm_core.h: change iseq data structures. * introduce rb_call_info_kw_arg_t to represent keyword arguments. * add rb_call_info_t::kw_arg. * rename rb_iseq_t::arg_post_len to rb_iseq_t::arg_post_num. * rename rb_iseq_t::arg_keywords to arg_keyword_num. * rename rb_iseq_t::arg_keyword to rb_iseq_t::arg_keyword_bits. to represent keyword bitmap parameter index. This bitmap parameter shows that which keyword parameters are given or not given (0 for given). It is refered by `checkkeyword' instruction described bellow. * rename rb_iseq_t::arg_keyword_check to rb_iseq_t::arg_keyword_rest to represent keyword rest parameter index. * add rb_iseq_t::arg_keyword_default_values to represent default keyword values. * rename VM_CALL_ARGS_SKIP_SETUP to VM_CALL_ARGS_SIMPLE to represent (ci->flag & (SPLAT|BLOCKARG)) && ci->blockiseq == NULL && ci->kw_arg == NULL. * vm_insnhelper.c, vm_args.c: rewrite with refactoring. * rewrite splat argument code. * rewrite keyword arguments/parameters code. * merge method and block parameter fitting code into one code base. * vm.c, vm_eval.c: catch up these changes. * compile.c (new_callinfo): callinfo requires kw_arg parameter. * compile.c (compile_array_): check the last argument Hash object or not. If Hash object and all keys are Symbol literals, they are compiled to keyword arguments. * insns.def (checkkeyword): add new instruction. This instruction check the availability of corresponding keyword. For example, a method "def foo k1: 'v1'; end" is cimpiled to the following instructions. 0000 checkkeyword 2, 0 # check k1 is given. 0003 branchif 9 # if given, jump to address #9 0005 putstring "v1" 0007 setlocal_OP__WC__0 3 # k1 = 'v1' 0009 trace 8 0011 putnil 0012 trace 16 0014 leave * insns.def (opt_send_simple): removed and add new instruction "opt_send_without_block". * parse.y (new_args_tail_gen): reorder variables. Before this patch, a method "def foo(k1: 1, kr1:, k2: 2, **krest, &b)" has parameter variables "k1, kr1, k2, &b, internal_id, krest", but this patch reorders to "kr1, k1, k2, internal_id, krest, &b". (locate a block variable at last) * parse.y (vtable_pop): added. This function remove latest `n' variables from vtable. * iseq.c: catch up iseq data changes. * proc.c: ditto. * class.c (keyword_error): export as rb_keyword_error(). * common.mk: depend vm_args.c for vm.o. * hash.c (rb_hash_has_key): export. * internal.h: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48239 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- parse.y | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 9 deletions(-) (limited to 'parse.y') diff --git a/parse.y b/parse.y index 18c08db394..861ff4945a 100644 --- a/parse.y +++ b/parse.y @@ -174,6 +174,15 @@ vtable_add(struct vtable *tbl, ID id) tbl->tbl[tbl->pos++] = id; } +#ifndef RIPPER +static void +vtable_pop(struct vtable *tbl, int n) +{ + if (tbl->pos < n) rb_bug("vtable_pop: unreachable"); + tbl->pos -= n; +} +#endif + static int vtable_included(const struct vtable * tbl, ID id) { @@ -9614,25 +9623,62 @@ new_args_tail_gen(struct parser_params *parser, NODE *k, ID kr, ID b) { int saved_line = ruby_sourceline; struct rb_args_info *args; - NODE *kw_rest_arg = 0; NODE *node; - int check = 0; args = ZALLOC(struct rb_args_info); node = NEW_NODE(NODE_ARGS, 0, 0, args); args->block_arg = b; args->kw_args = k; - if (k && !kr) { - check = 1; - kr = internal_id(); + + if (k) { + /* + * def foo(k1: 1, kr1:, k2: 2, **krest, &b) + * variable order: k1, kr1, k2, &b, internal_id, krest + * #=> + * variable order: kr1, k1, k2, internal_id, krest, &b + */ + ID kw_bits; + NODE *kwn = k; + struct vtable *required_kw_vars = vtable_alloc(NULL); + struct vtable *kw_vars = vtable_alloc(NULL); + int i; + + while (kwn) { + NODE *val_node = kwn->nd_body->nd_value; + ID vid = kwn->nd_body->nd_vid; + + if (val_node == (NODE *)-1) { + vtable_add(required_kw_vars, vid); + } + else { + vtable_add(kw_vars, vid); + } + + kwn = kwn->nd_next; + } + + vtable_pop(lvtbl->args, vtable_size(required_kw_vars) + vtable_size(kw_vars) + (b != 0)); + + for (i=0; itbl[i]); + for (i=0; itbl[i]); + vtable_free(required_kw_vars); + vtable_free(kw_vars); + + kw_bits = internal_id(); + arg_var(kw_bits); + if (kr) arg_var(kr); + if (b) arg_var(b); + + args->kw_rest_arg = NEW_DVAR(kw_bits); + args->kw_rest_arg->nd_cflag = kr; } - if (kr) { + else if (kr) { + if (b) vtable_pop(lvtbl->args, 1); /* reorder */ arg_var(kr); - kw_rest_arg = NEW_DVAR(kr); - kw_rest_arg->nd_cflag = check; + if (b) arg_var(b); + args->kw_rest_arg = NEW_DVAR(kr); } - args->kw_rest_arg = kw_rest_arg; ruby_sourceline = saved_line; return node; -- cgit v1.2.3