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

ruby-changes:44243

From: nobu <ko1@a...>
Date: Sat, 1 Oct 2016 22:17:57 +0900 (JST)
Subject: [ruby-changes:44243] nobu:r56316 (trunk): compile.c: literal range

nobu	2016-10-01 22:17:51 +0900 (Sat, 01 Oct 2016)

  New Revision: 56316

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

  Log:
    compile.c: literal range
    
    * compile.c (iseq_compile_each): move numeric literal range
      optimization from fixup_nodes() in parse.y.

  Modified files:
    trunk/ChangeLog
    trunk/compile.c
    trunk/parse.y
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56315)
+++ ChangeLog	(revision 56316)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Oct  1 22:17:49 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* compile.c (iseq_compile_each): move numeric literal range
+	  optimization from fixup_nodes() in parse.y.
+
 Sat Oct  1 19:19:34 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* compile.c (compile_flip_flop): simplify generated code.
Index: parse.y
===================================================================
--- parse.y	(revision 56315)
+++ parse.y	(revision 56316)
@@ -274,7 +274,6 @@ struct parser_params { https://github.com/ruby/ruby/blob/trunk/parse.y#L274
     int heredoc_indent;
     int heredoc_line_indent;
     char *tokenbuf;
-    NODE *deferred_nodes;
     struct local_vars *lvtbl;
     int line_count;
     int ruby_sourceline;	/* current line no. */
@@ -369,7 +368,6 @@ static int parser_yyerror(struct parser_ https://github.com/ruby/ruby/blob/trunk/parse.y#L368
 #define heredoc_indent		(parser->heredoc_indent)
 #define heredoc_line_indent	(parser->heredoc_line_indent)
 #define command_start		(parser->command_start)
-#define deferred_nodes		(parser->deferred_nodes)
 #define lex_gets_ptr		(parser->lex.gets_ptr)
 #define lex_gets		(parser->lex.gets)
 #define lvtbl			(parser->lvtbl)
@@ -517,11 +515,6 @@ static NODE *match_op_gen(struct parser_ https://github.com/ruby/ruby/blob/trunk/parse.y#L515
 static ID  *local_tbl_gen(struct parser_params*);
 #define local_tbl() local_tbl_gen(parser)
 
-static NODE *deferred_dots_gen(struct parser_params*,NODE*);
-#define deferred_dots(n) deferred_dots_gen(parser, (n))
-
-static void fixup_nodes(NODE **);
-
 static VALUE reg_compile_gen(struct parser_params*, VALUE, int);
 #define reg_compile(str,options) reg_compile_gen(parser, (str), (options))
 static void reg_fragment_setenc_gen(struct parser_params*, VALUE, int);
@@ -1026,7 +1019,6 @@ top_compstmt	: top_stmts opt_terms https://github.com/ruby/ruby/blob/trunk/parse.y#L1019
 		    {
 		    /*%%%*/
 			void_stmts($1);
-			fixup_nodes(&deferred_nodes);
 		    /*%
 		    %*/
 			$$ = $1;
@@ -1123,7 +1115,6 @@ compstmt	: stmts opt_terms https://github.com/ruby/ruby/blob/trunk/parse.y#L1115
 		    {
 		    /*%%%*/
 			void_stmts($1);
-			fixup_nodes(&deferred_nodes);
 		    /*%
 		    %*/
 			$$ = $1;
@@ -2082,7 +2073,6 @@ arg		: lhs '=' arg_rhs https://github.com/ruby/ruby/blob/trunk/parse.y#L2073
 			value_expr($1);
 			value_expr($3);
 			$$ = NEW_DOT2($1, $3);
-			deferred_dots($$);
 		    /*%
 			$$ = dispatch2(dot2, $1, $3);
 		    %*/
@@ -2093,7 +2083,6 @@ arg		: lhs '=' arg_rhs https://github.com/ruby/ruby/blob/trunk/parse.y#L2083
 			value_expr($1);
 			value_expr($3);
 			$$ = NEW_DOT3($1, $3);
-			deferred_dots($$);
 		    /*%
 			$$ = dispatch2(dot3, $1, $3);
 		    %*/
@@ -7204,7 +7193,6 @@ parser_prepare(struct parser_params *par https://github.com/ruby/ruby/blob/trunk/parse.y#L7193
     }
     pushback(c);
     parser->enc = rb_enc_get(lex_lastline);
-    deferred_nodes = 0;
     parser->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
 }
 
@@ -8969,18 +8957,6 @@ match_op_gen(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8957
     return NEW_CALL(node1, tMATCH, NEW_LIST(node2));
 }
 
-static NODE *
-deferred_dots_gen(struct parser_params *parser, NODE *n)
-{
-    NODE *b = n->nd_beg;
-    NODE *e = n->nd_end;
-    if (b && nd_type(b) == NODE_LIT && FIXNUM_P(b->nd_lit) &&
-	e && nd_type(e) == NODE_LIT && FIXNUM_P(e->nd_lit)) {
-	deferred_nodes = list_append(deferred_nodes, n);
-    }
-    return n;
-}
-
 # if WARN_PAST_SCOPE
 static int
 past_dvar_p(struct parser_params *parser, ID id)
@@ -9796,35 +9772,6 @@ warning_unless_e_option(struct parser_pa https://github.com/ruby/ruby/blob/trunk/parse.y#L9772
     if (!e_option_supplied(parser)) parser_warning(node, str);
 }
 
-static void
-fixup_nodes(NODE **rootnode)
-{
-    NODE *node, *next, *head;
-
-    for (node = *rootnode; node; node = next) {
-	enum node_type type;
-	VALUE val;
-
-	next = node->nd_next;
-	head = node->nd_head;
-	rb_gc_force_recycle((VALUE)node);
-	*rootnode = next;
-	switch (type = nd_type(head)) {
-	  case NODE_DOT2:
-	  case NODE_DOT3:
-	    val = rb_range_new(head->nd_beg->nd_lit, head->nd_end->nd_lit,
-			       type == NODE_DOT3);
-	    rb_gc_force_recycle((VALUE)head->nd_beg);
-	    rb_gc_force_recycle((VALUE)head->nd_end);
-	    nd_set_type(head, NODE_LIT);
-	    head->nd_lit = val;
-	    break;
-	  default:
-	    break;
-	}
-    }
-}
-
 static NODE *cond0(struct parser_params*,NODE*);
 
 static NODE*
@@ -10825,7 +10772,6 @@ parser_mark(void *ptr) https://github.com/ruby/ruby/blob/trunk/parse.y#L10772
     struct parser_params *parser = (struct parser_params*)ptr;
 
     rb_gc_mark((VALUE)lex_strterm);
-    rb_gc_mark((VALUE)deferred_nodes);
     rb_gc_mark(lex_input);
     rb_gc_mark(lex_lastline);
     rb_gc_mark(lex_nextline);
Index: compile.c
===================================================================
--- compile.c	(revision 56315)
+++ compile.c	(revision 56316)
@@ -3873,6 +3873,12 @@ compile_named_capture_assign(rb_iseq_t * https://github.com/ruby/ruby/blob/trunk/compile.c#L3873
     ADD_LABEL(ret, end_label);
 }
 
+static int
+number_literal_p(NODE *n)
+{
+    return (n && nd_type(n) == NODE_LIT && RB_INTEGER_TYPE_P(n->nd_lit));
+}
+
 /**
   compile each node
 
@@ -5923,7 +5929,16 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/compile.c#L5929
       }
       case NODE_DOT2:
       case NODE_DOT3:{
-	VALUE flag = type == NODE_DOT2 ? INT2FIX(0) : INT2FIX(1);
+	int excl = type == NODE_DOT3;
+	VALUE flag = INT2FIX(excl);
+	NODE *b = node->nd_beg;
+	NODE *e = node->nd_end;
+	if (number_literal_p(b) && number_literal_p(e)) {
+	    VALUE val = rb_range_new(b->nd_lit, e->nd_lit, excl);
+	    iseq_add_mark_object_compile_time(iseq, val);
+	    ADD_INSN1(ret, line, putobject, val);
+	    break;
+	}
 	COMPILE(ret, "min", (NODE *) node->nd_beg);
 	COMPILE(ret, "max", (NODE *) node->nd_end);
 	if (poped) {

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

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