1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* vm_core.h: change semantics of opt_num and opt_table.

`opt_num' was the number of optional parameters + 1.
  `opt_table' has "opt_num" entries.
  Change them to:
  `opt_num' is the number of optional parameters.
  `opt_talbe' has "opt_num + 1" entries.
  This change simplify parameter fitting logics.
* compile.c: catch up this change.
* iseq.c: ditto.
* proc.c: ditto.
* vm_args.c: ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48247 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2014-11-03 04:43:07 +00:00
parent 888e16c51e
commit ef25be7391
6 changed files with 56 additions and 27 deletions

View file

@ -1,3 +1,23 @@
Mon Nov 3 13:38:28 2014 Koichi Sasada <ko1@atdot.net>
* vm_core.h: change semantics of opt_num and opt_table.
`opt_num' was the number of optional parameters + 1.
`opt_table' has "opt_num" entries.
Change them to:
`opt_num' is the number of optional parameters.
`opt_talbe' has "opt_num + 1" entries.
This change simplify parameter fitting logics.
* compile.c: catch up this change.
* iseq.c: ditto.
* proc.c: ditto.
* vm_args.c: ditto.
Mon Nov 3 11:47:44 2014 SHIBATA Hiroshi <shibata.hiroshi@gmail.com> Mon Nov 3 11:47:44 2014 SHIBATA Hiroshi <shibata.hiroshi@gmail.com>
* NEWS: added period into Matrix section. [ci skip] * NEWS: added period into Matrix section. [ci skip]

View file

@ -1112,12 +1112,11 @@ iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *optargs, NODE *node_args)
label = NEW_LABEL(nd_line(node_args)); label = NEW_LABEL(nd_line(node_args));
rb_ary_push(labels, (VALUE)label | 1); rb_ary_push(labels, (VALUE)label | 1);
ADD_LABEL(optargs, label); ADD_LABEL(optargs, label);
i += 1;
iseq->param.opt_num = i; iseq->param.opt_num = i;
iseq->param.opt_table = ALLOC_N(VALUE, i); iseq->param.opt_table = ALLOC_N(VALUE, i+1);
MEMCPY(iseq->param.opt_table, RARRAY_CONST_PTR(labels), VALUE, i); MEMCPY(iseq->param.opt_table, RARRAY_CONST_PTR(labels), VALUE, i+1);
for (j = 0; j < i; j++) { for (j = 0; j < i+1; j++) {
iseq->param.opt_table[j] &= ~1; iseq->param.opt_table[j] &= ~1;
} }
rb_ary_clear(labels); rb_ary_clear(labels);
@ -1230,7 +1229,7 @@ iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *optargs, NODE *node_args)
iseq->param.size = iseq->param.rest_start + 1; iseq->param.size = iseq->param.rest_start + 1;
} }
else if (iseq->param.flags.has_opt) { else if (iseq->param.flags.has_opt) {
iseq->param.size = iseq->param.lead_num + iseq->param.opt_num - 1; iseq->param.size = iseq->param.lead_num + iseq->param.opt_num;
} }
else { else {
rb_bug("unreachable"); rb_bug("unreachable");
@ -1687,7 +1686,7 @@ iseq_set_optargs_table(rb_iseq_t *iseq)
int i; int i;
if (iseq->param.flags.has_opt) { if (iseq->param.flags.has_opt) {
for (i = 0; i < iseq->param.opt_num; i++) { for (i = 0; i < iseq->param.opt_num + 1; i++) {
iseq->param.opt_table[i] = label_get_position((LABEL *)iseq->param.opt_table[i]); iseq->param.opt_table[i] = label_get_position((LABEL *)iseq->param.opt_table[i]);
} }
} }
@ -4495,7 +4494,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
if (liseq->param.flags.has_opt) { if (liseq->param.flags.has_opt) {
/* optional arguments */ /* optional arguments */
int j; int j;
for (j = 0; j < liseq->param.opt_num - 1; j++) { for (j = 0; j < liseq->param.opt_num; j++) {
int idx = liseq->local_size - (i + j); int idx = liseq->local_size - (i + j);
ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level)); ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level));
} }
@ -5896,8 +5895,8 @@ rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE locals, VALUE args,
iseq->param.post_num = FIX2INT(arg_post_num); iseq->param.post_num = FIX2INT(arg_post_num);
iseq->param.post_start = FIX2INT(arg_post_start); iseq->param.post_start = FIX2INT(arg_post_start);
iseq->param.block_start = FIX2INT(arg_block); iseq->param.block_start = FIX2INT(arg_block);
iseq->param.opt_num = RARRAY_LENINT(arg_opt_labels); iseq->param.opt_num = RARRAY_LENINT(arg_opt_labels) - 1;
iseq->param.opt_table = (VALUE *)ALLOC_N(VALUE, iseq->param.opt_num); iseq->param.opt_table = (VALUE *)ALLOC_N(VALUE, iseq->param.opt_num + 1);
if (iseq->param.flags.has_block) { if (iseq->param.flags.has_block) {
iseq->param.size = iseq->param.block_start + 1; iseq->param.size = iseq->param.block_start + 1;
@ -5909,7 +5908,7 @@ rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE locals, VALUE args,
iseq->param.size = iseq->param.rest_start + 1; iseq->param.size = iseq->param.rest_start + 1;
} }
else { else {
iseq->param.size = iseq->param.lead_num + (iseq->param.opt_num - (iseq->param.flags.has_opt == TRUE)); iseq->param.size = iseq->param.lead_num + iseq->param.opt_num;
} }
for (i=0; i<RARRAY_LEN(arg_opt_labels); i++) { for (i=0; i<RARRAY_LEN(arg_opt_labels); i++) {

13
iseq.c
View file

@ -143,7 +143,7 @@ iseq_memsize(const void *ptr)
if (iseq->catch_table) { if (iseq->catch_table) {
size += iseq_catch_table_bytes(iseq->catch_table->size); size += iseq_catch_table_bytes(iseq->catch_table->size);
} }
size += iseq->param.opt_num * sizeof(VALUE); size += (iseq->param.opt_num + 1) * sizeof(VALUE);
size += iseq->is_size * sizeof(union iseq_inline_storage_entry); size += iseq->is_size * sizeof(union iseq_inline_storage_entry);
size += iseq->callinfo_size * sizeof(rb_call_info_t); size += iseq->callinfo_size * sizeof(rb_call_info_t);
@ -1420,7 +1420,7 @@ rb_iseq_disasm(VALUE self)
"[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n", "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
iseqdat->local_size, iseqdat->local_size,
iseqdat->param.lead_num, iseqdat->param.lead_num,
iseqdat->param.opt_num - (iseqdat->param.flags.has_opt == TRUE), iseqdat->param.opt_num,
iseqdat->param.flags.has_rest ? iseqdat->param.rest_start : -1, iseqdat->param.flags.has_rest ? iseqdat->param.rest_start : -1,
iseqdat->param.post_num, iseqdat->param.post_num,
iseqdat->param.flags.has_block ? iseqdat->param.block_start : -1, iseqdat->param.flags.has_block ? iseqdat->param.block_start : -1,
@ -1437,7 +1437,7 @@ rb_iseq_disasm(VALUE self)
if (iseqdat->param.flags.has_opt) { if (iseqdat->param.flags.has_opt) {
int argc = iseqdat->param.lead_num; int argc = iseqdat->param.lead_num;
int opts = iseqdat->param.opt_num; int opts = iseqdat->param.opt_num;
if (i >= argc && i < argc + opts - 1) { if (i >= argc && i < argc + opts) {
snprintf(opti, sizeof(opti), "Opt=%"PRIdVALUE, snprintf(opti, sizeof(opti), "Opt=%"PRIdVALUE,
iseqdat->param.opt_table[i - argc]); iseqdat->param.opt_table[i - argc]);
} }
@ -1720,7 +1720,7 @@ iseq_data_to_ary(rb_iseq_t *iseq)
VALUE arg_opt_labels = rb_ary_new(); VALUE arg_opt_labels = rb_ary_new();
int j; int j;
for (j=0; j<iseq->param.opt_num; j++) { for (j=0; j < iseq->param.opt_num; j++) {
rb_ary_push(arg_opt_labels, register_label(labels_table, iseq->param.opt_table[j])); rb_ary_push(arg_opt_labels, register_label(labels_table, iseq->param.opt_table[j]));
} }
@ -1969,7 +1969,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
rb_ary_push(args, PARAM(i, req)); rb_ary_push(args, PARAM(i, req));
} }
} }
r = iseq->param.lead_num + iseq->param.opt_num - 1; r = iseq->param.lead_num + iseq->param.opt_num;
for (; i < r; i++) { for (; i < r; i++) {
PARAM_TYPE(opt); PARAM_TYPE(opt);
if (rb_id2str(PARAM_ID(i))) { if (rb_id2str(PARAM_ID(i))) {
@ -2122,8 +2122,7 @@ rb_iseq_build_for_ruby2cext(
struct iseq_catch_table_entry, iseq->catch_table->size); struct iseq_catch_table_entry, iseq->catch_table->size);
} }
ALLOC_AND_COPY(iseq->param.opt_table, arg_opt_table, ALLOC_AND_COPY(iseq->param.opt_table, arg_opt_table, VALUE, iseq->param.opt_num + 1);
VALUE, iseq->param.opt_num);
set_relation(iseq, 0); set_relation(iseq, 0);

6
proc.c
View file

@ -848,10 +848,8 @@ static inline int
rb_iseq_min_max_arity(const rb_iseq_t *iseq, int *max) rb_iseq_min_max_arity(const rb_iseq_t *iseq, int *max)
{ {
*max = iseq->param.flags.has_rest == FALSE ? *max = iseq->param.flags.has_rest == FALSE ?
iseq->param.lead_num + iseq->param.post_num + iseq->param.lead_num + iseq->param.opt_num + iseq->param.post_num +
iseq->param.opt_num - (iseq->param.flags.has_opt == TRUE) + (iseq->param.flags.has_kw == TRUE || iseq->param.flags.has_kwrest == TRUE)
(iseq->param.flags.has_kw == TRUE) +
(iseq->param.flags.has_kwrest == TRUE)
: UNLIMITED_ARGUMENTS; : UNLIMITED_ARGUMENTS;
return iseq->param.lead_num + iseq->param.post_num + (iseq->param.flags.has_kw && iseq->param.keyword->required_num > 0); return iseq->param.lead_num + iseq->param.post_num + (iseq->param.flags.has_kw && iseq->param.keyword->required_num > 0);
} }

View file

@ -523,7 +523,7 @@ setup_parameters_complex(rb_thread_t * const th, const rb_iseq_t * const iseq, r
VALUE * const locals, const enum arg_setup_type arg_setup_type) VALUE * const locals, const enum arg_setup_type arg_setup_type)
{ {
const int min_argc = iseq->param.lead_num + iseq->param.post_num; const int min_argc = iseq->param.lead_num + iseq->param.post_num;
const int max_argc = (iseq->param.flags.has_rest == FALSE) ? min_argc + (iseq->param.opt_num - (iseq->param.flags.has_opt == TRUE)) : UNLIMITED_ARGUMENTS; const int max_argc = (iseq->param.flags.has_rest == FALSE) ? min_argc + iseq->param.opt_num : UNLIMITED_ARGUMENTS;
int opt_pc = 0; int opt_pc = 0;
int given_argc; int given_argc;
struct args_info args_body, *args; struct args_info args_body, *args;
@ -570,8 +570,8 @@ setup_parameters_complex(rb_thread_t * const th, const rb_iseq_t * const iseq, r
break; /* do nothing special */ break; /* do nothing special */
case arg_setup_block: case arg_setup_block:
if (given_argc == 1 && if (given_argc == 1 &&
(min_argc > 0 || (min_argc > 0 || iseq->param.opt_num > 1 ||
iseq->param.opt_num > 2 || iseq->param.flags.has_kw || iseq->param.flags.has_kwrest) && /* TODO: can be shrink with flags */ iseq->param.flags.has_kw || iseq->param.flags.has_kwrest) &&
!iseq->param.flags.ambiguous_param0 && !iseq->param.flags.ambiguous_param0 &&
args_check_block_arg0(args, th, msl)) { args_check_block_arg0(args, th, msl)) {
given_argc = RARRAY_LENINT(args->rest); given_argc = RARRAY_LENINT(args->rest);
@ -632,7 +632,7 @@ setup_parameters_complex(rb_thread_t * const th, const rb_iseq_t * const iseq, r
} }
if (iseq->param.flags.has_opt) { if (iseq->param.flags.has_opt) {
int opt = args_setup_opt_parameters(args, iseq->param.opt_num - 1, locals + iseq->param.lead_num); int opt = args_setup_opt_parameters(args, iseq->param.opt_num, locals + iseq->param.lead_num);
opt_pc = (int)iseq->param.opt_table[opt]; opt_pc = (int)iseq->param.opt_table[opt];
} }

View file

@ -252,7 +252,7 @@ struct rb_iseq_struct {
* => * =>
* *
* lead_num = M * lead_num = M
* opt_num = N+1 * opt_num = N
* rest_start = M+N * rest_start = M+N
* post_start = M+N+(*1) * post_start = M+N+(*1)
* post_num = O * post_num = O
@ -284,7 +284,20 @@ struct rb_iseq_struct {
int post_num; int post_num;
int block_start; int block_start;
VALUE *opt_table; VALUE *opt_table; /* (opt_num + 1) entries. */
/* opt_num and opt_table:
*
* def foo o1=e1, o2=e2, ..., oN=eN
* #=>
* # prologue code
* A1: e1
* A2: e2
* ...
* AN: eN
* AL: body
* opt_num = N
* opt_table = [A1, A2, ..., AN, AL]
*/
struct rb_iseq_param_keyword { struct rb_iseq_param_keyword {
int num; int num;