[前][次][番号順一覧][スレッド一覧]

ruby-changes:36166

From: ko1 <ko1@a...>
Date: Mon, 3 Nov 2014 13:43:15 +0900 (JST)
Subject: [ruby-changes:36166] ko1:r48247 (trunk): * vm_core.h: change semantics of opt_num and opt_table.

ko1	2014-11-03 13:43:07 +0900 (Mon, 03 Nov 2014)

  New Revision: 48247

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48247

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/compile.c
    trunk/iseq.c
    trunk/proc.c
    trunk/vm_args.c
    trunk/vm_core.h
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 48246)
+++ ChangeLog	(revision 48247)
@@ -1,3 +1,23 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Nov  3 13:38:28 2014  Koichi Sasada  <ko1@a...>
+
+	* 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@g...>
 
 	* NEWS: added period into Matrix section. [ci skip]
Index: vm_core.h
===================================================================
--- vm_core.h	(revision 48246)
+++ vm_core.h	(revision 48247)
@@ -252,7 +252,7 @@ struct rb_iseq_struct { https://github.com/ruby/ruby/blob/trunk/vm_core.h#L252
      * =>
      *
      *  lead_num     = M
-     *  opt_num      = N+1
+     *  opt_num      = N
      *  rest_start   = M+N
      *  post_start   = M+N+(*1)
      *  post_num     = O
@@ -284,7 +284,20 @@ struct rb_iseq_struct { https://github.com/ruby/ruby/blob/trunk/vm_core.h#L284
 	int post_num;
 	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 {
 	    int num;
Index: iseq.c
===================================================================
--- iseq.c	(revision 48246)
+++ iseq.c	(revision 48247)
@@ -143,7 +143,7 @@ iseq_memsize(const void *ptr) https://github.com/ruby/ruby/blob/trunk/iseq.c#L143
 	    if (iseq->catch_table) {
 		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->callinfo_size * sizeof(rb_call_info_t);
 
@@ -1420,7 +1420,7 @@ rb_iseq_disasm(VALUE self) https://github.com/ruby/ruby/blob/trunk/iseq.c#L1420
 		    "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
 		    iseqdat->local_size,
 		    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.post_num,
 		    iseqdat->param.flags.has_block ? iseqdat->param.block_start : -1,
@@ -1437,7 +1437,7 @@ rb_iseq_disasm(VALUE self) https://github.com/ruby/ruby/blob/trunk/iseq.c#L1437
 	    if (iseqdat->param.flags.has_opt) {
 		int argc = iseqdat->param.lead_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,
 			     iseqdat->param.opt_table[i - argc]);
 		}
@@ -1720,7 +1720,7 @@ iseq_data_to_ary(rb_iseq_t *iseq) https://github.com/ruby/ruby/blob/trunk/iseq.c#L1720
 	VALUE arg_opt_labels = rb_ary_new();
 	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]));
 	}
 
@@ -1969,7 +1969,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq https://github.com/ruby/ruby/blob/trunk/iseq.c#L1969
 	    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++) {
 	PARAM_TYPE(opt);
 	if (rb_id2str(PARAM_ID(i))) {
@@ -2122,8 +2122,7 @@ rb_iseq_build_for_ruby2cext( https://github.com/ruby/ruby/blob/trunk/iseq.c#L2122
 	    struct iseq_catch_table_entry, iseq->catch_table->size);
     }
 
-    ALLOC_AND_COPY(iseq->param.opt_table, arg_opt_table,
-		   VALUE, iseq->param.opt_num);
+    ALLOC_AND_COPY(iseq->param.opt_table, arg_opt_table, VALUE, iseq->param.opt_num + 1);
 
     set_relation(iseq, 0);
 
Index: compile.c
===================================================================
--- compile.c	(revision 48246)
+++ compile.c	(revision 48247)
@@ -1112,12 +1112,11 @@ iseq_set_arguments(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L1112
 	    label = NEW_LABEL(nd_line(node_args));
 	    rb_ary_push(labels, (VALUE)label | 1);
 	    ADD_LABEL(optargs, label);
-	    i += 1;
 
 	    iseq->param.opt_num = i;
-	    iseq->param.opt_table = ALLOC_N(VALUE, i);
-	    MEMCPY(iseq->param.opt_table, RARRAY_CONST_PTR(labels), VALUE, i);
-	    for (j = 0; j < i; j++) {
+	    iseq->param.opt_table = ALLOC_N(VALUE, i+1);
+	    MEMCPY(iseq->param.opt_table, RARRAY_CONST_PTR(labels), VALUE, i+1);
+	    for (j = 0; j < i+1; j++) {
 		iseq->param.opt_table[j] &= ~1;
 	    }
 	    rb_ary_clear(labels);
@@ -1230,7 +1229,7 @@ iseq_set_arguments(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L1229
 		iseq->param.size = iseq->param.rest_start + 1;
 	    }
 	    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 {
 		rb_bug("unreachable");
@@ -1687,7 +1686,7 @@ iseq_set_optargs_table(rb_iseq_t *iseq) https://github.com/ruby/ruby/blob/trunk/compile.c#L1686
     int i;
 
     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]);
 	}
     }
@@ -4495,7 +4494,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/compile.c#L4494
 	    if (liseq->param.flags.has_opt) {
 		/* optional arguments */
 		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);
 		    ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level));
 		}
@@ -5896,8 +5895,8 @@ rb_iseq_build_from_ary(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/compile.c#L5895
 	iseq->param.post_num = FIX2INT(arg_post_num);
 	iseq->param.post_start = FIX2INT(arg_post_start);
 	iseq->param.block_start = FIX2INT(arg_block);
-	iseq->param.opt_num = RARRAY_LENINT(arg_opt_labels);
-	iseq->param.opt_table = (VALUE *)ALLOC_N(VALUE, iseq->param.opt_num);
+	iseq->param.opt_num = RARRAY_LENINT(arg_opt_labels) - 1;
+	iseq->param.opt_table = (VALUE *)ALLOC_N(VALUE, iseq->param.opt_num + 1);
 
 	if (iseq->param.flags.has_block) {
 	    iseq->param.size = iseq->param.block_start + 1;
@@ -5909,7 +5908,7 @@ rb_iseq_build_from_ary(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/compile.c#L5908
 	    iseq->param.size = iseq->param.rest_start + 1;
 	}
 	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++) {
Index: proc.c
===================================================================
--- proc.c	(revision 48246)
+++ proc.c	(revision 48247)
@@ -848,10 +848,8 @@ static inline int https://github.com/ruby/ruby/blob/trunk/proc.c#L848
 rb_iseq_min_max_arity(const rb_iseq_t *iseq, int *max)
 {
     *max = iseq->param.flags.has_rest == FALSE ?
-      iseq->param.lead_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.lead_num + iseq->param.opt_num + iseq->param.post_num +
+      (iseq->param.flags.has_kw == TRUE || iseq->param.flags.has_kwrest == TRUE)
       : UNLIMITED_ARGUMENTS;
     return iseq->param.lead_num + iseq->param.post_num + (iseq->param.flags.has_kw && iseq->param.keyword->required_num > 0);
 }
Index: vm_args.c
===================================================================
--- vm_args.c	(revision 48246)
+++ vm_args.c	(revision 48247)
@@ -523,7 +523,7 @@ setup_parameters_complex(rb_thread_t * c https://github.com/ruby/ruby/blob/trunk/vm_args.c#L523
 			 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 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 given_argc;
     struct args_info args_body, *args;
@@ -570,8 +570,8 @@ setup_parameters_complex(rb_thread_t * c https://github.com/ruby/ruby/blob/trunk/vm_args.c#L570
 	break; /* do nothing special */
       case arg_setup_block:
 	if (given_argc == 1 &&
-	    (min_argc > 0 ||
-	     iseq->param.opt_num > 2 || iseq->param.flags.has_kw || iseq->param.flags.has_kwrest) && /* TODO: can be shrink with flags */
+	    (min_argc > 0 || iseq->param.opt_num > 1 ||
+	     iseq->param.flags.has_kw || iseq->param.flags.has_kwrest) &&
 	    !iseq->param.flags.ambiguous_param0 &&
 	    args_check_block_arg0(args, th, msl)) {
 	    given_argc = RARRAY_LENINT(args->rest);
@@ -632,7 +632,7 @@ setup_parameters_complex(rb_thread_t * c https://github.com/ruby/ruby/blob/trunk/vm_args.c#L632
     }
 
     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];
     }
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]