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

parse.y: optimize IDs in ripper

* parse.y: optimize ripper_intern calls, ::, **, -@, +@, <=>, >=,
  <=, ==, ===, !=, =~, !~, <<, >>, and call.

* parse.y: use initialized IDs, warn and warning.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48153 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-10-27 06:23:09 +00:00
parent fc9fffca1d
commit 5a277b4070
4 changed files with 43 additions and 37 deletions

View file

@ -38,6 +38,7 @@ firstline, predefined = __LINE__+1, %[\
to_i to_i
bt bt
bt_locations bt_locations
call
_ UScore _ UScore
"/*NULL*/" NULL "/*NULL*/" NULL

75
parse.y
View file

@ -1225,7 +1225,7 @@ stmt : keyword_alias fitem {lex_state = EXPR_FNAME;} fitem
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call
{ {
value_expr($5); value_expr($5);
$$ = new_attr_op_assign($1, ripper_intern("::"), $3, $4, $5); $$ = new_attr_op_assign($1, ID2SYM(idCOLON2), $3, $4, $5);
} }
| backref tOP_ASGN command_call | backref tOP_ASGN command_call
{ {
@ -1425,7 +1425,7 @@ command : fcall command_args %prec tLOWEST
$$ = NEW_CALL($1, $3, $4); $$ = NEW_CALL($1, $3, $4);
fixpos($$, $1); fixpos($$, $1);
/*% /*%
$$ = dispatch4(command_call, $1, ripper_intern("::"), $3, $4); $$ = dispatch4(command_call, $1, ID2SYM(idCOLON2), $3, $4);
%*/ %*/
} }
| primary_value tCOLON2 operation2 command_args cmd_brace_block | primary_value tCOLON2 operation2 command_args cmd_brace_block
@ -1436,7 +1436,7 @@ command : fcall command_args %prec tLOWEST
$$ = $5; $$ = $5;
fixpos($$, $1); fixpos($$, $1);
/*% /*%
$$ = dispatch4(command_call, $1, ripper_intern("::"), $3, $4); $$ = dispatch4(command_call, $1, ID2SYM(idCOLON2), $3, $4);
$$ = method_add_block($$, $5); $$ = method_add_block($$, $5);
%*/ %*/
} }
@ -1759,7 +1759,7 @@ lhs : user_variable
/*%%%*/ /*%%%*/
$$ = attrset($1, $3); $$ = attrset($1, $3);
/*% /*%
$$ = dispatch3(field, $1, ripper_intern("::"), $3); $$ = dispatch3(field, $1, ID2SYM(idCOLON2), $3);
%*/ %*/
} }
| primary_value '.' tCONSTANT | primary_value '.' tCONSTANT
@ -2019,7 +2019,7 @@ arg : lhs '=' arg
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg
{ {
value_expr($5); value_expr($5);
$$ = new_attr_op_assign($1, ripper_intern("::"), $3, $4, $5); $$ = new_attr_op_assign($1, ID2SYM(idCOLON2), $3, $4, $5);
} }
| primary_value tCOLON2 tCONSTANT tOP_ASGN arg | primary_value tCOLON2 tCONSTANT tOP_ASGN arg
{ {
@ -2126,7 +2126,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tPOW, $3); $$ = call_bin_op($1, tPOW, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern("**"), $3); $$ = dispatch3(binary, $1, ID2SYM(idPow), $3);
%*/ %*/
} }
| tUMINUS_NUM simple_numeric tPOW arg | tUMINUS_NUM simple_numeric tPOW arg
@ -2134,8 +2134,8 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0); $$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0);
/*% /*%
$$ = dispatch3(binary, $2, ripper_intern("**"), $4); $$ = dispatch3(binary, $2, ID2SYM(idPow), $4);
$$ = dispatch2(unary, ripper_intern("-@"), $$); $$ = dispatch2(unary, ID2SYM(idUMinus), $$);
%*/ %*/
} }
| tUPLUS arg | tUPLUS arg
@ -2143,7 +2143,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_uni_op($2, tUPLUS); $$ = call_uni_op($2, tUPLUS);
/*% /*%
$$ = dispatch2(unary, ripper_intern("+@"), $2); $$ = dispatch2(unary, ID2SYM(idUPlus), $2);
%*/ %*/
} }
| tUMINUS arg | tUMINUS arg
@ -2151,7 +2151,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_uni_op($2, tUMINUS); $$ = call_uni_op($2, tUMINUS);
/*% /*%
$$ = dispatch2(unary, ripper_intern("-@"), $2); $$ = dispatch2(unary, ID2SYM(idUMinus), $2);
%*/ %*/
} }
| arg '|' arg | arg '|' arg
@ -2183,7 +2183,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tCMP, $3); $$ = call_bin_op($1, tCMP, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern("<=>"), $3); $$ = dispatch3(binary, $1, ID2SYM(idCmp), $3);
%*/ %*/
} }
| arg '>' arg | arg '>' arg
@ -2199,7 +2199,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tGEQ, $3); $$ = call_bin_op($1, tGEQ, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern(">="), $3); $$ = dispatch3(binary, $1, ID2SYM(idGE), $3);
%*/ %*/
} }
| arg '<' arg | arg '<' arg
@ -2215,7 +2215,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tLEQ, $3); $$ = call_bin_op($1, tLEQ, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern("<="), $3); $$ = dispatch3(binary, $1, ID2SYM(idLE), $3);
%*/ %*/
} }
| arg tEQ arg | arg tEQ arg
@ -2223,7 +2223,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tEQ, $3); $$ = call_bin_op($1, tEQ, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern("=="), $3); $$ = dispatch3(binary, $1, ID2SYM(idEq), $3);
%*/ %*/
} }
| arg tEQQ arg | arg tEQQ arg
@ -2231,7 +2231,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tEQQ, $3); $$ = call_bin_op($1, tEQQ, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern("==="), $3); $$ = dispatch3(binary, $1, ID2SYM(idEqq), $3);
%*/ %*/
} }
| arg tNEQ arg | arg tNEQ arg
@ -2239,7 +2239,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tNEQ, $3); $$ = call_bin_op($1, tNEQ, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern("!="), $3); $$ = dispatch3(binary, $1, ID2SYM(idNeq), $3);
%*/ %*/
} }
| arg tMATCH arg | arg tMATCH arg
@ -2250,7 +2250,7 @@ arg : lhs '=' arg
$$ = reg_named_capture_assign($1->nd_lit, $$); $$ = reg_named_capture_assign($1->nd_lit, $$);
} }
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern("=~"), $3); $$ = dispatch3(binary, $1, ID2SYM(idEqTilde), $3);
%*/ %*/
} }
| arg tNMATCH arg | arg tNMATCH arg
@ -2258,7 +2258,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tNMATCH, $3); $$ = call_bin_op($1, tNMATCH, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern("!~"), $3); $$ = dispatch3(binary, $1, ID2SYM(idNeqTilde), $3);
%*/ %*/
} }
| '!' arg | '!' arg
@ -2282,7 +2282,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tLSHFT, $3); $$ = call_bin_op($1, tLSHFT, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern("<<"), $3); $$ = dispatch3(binary, $1, ID2SYM(idLTLT), $3);
%*/ %*/
} }
| arg tRSHFT arg | arg tRSHFT arg
@ -2290,7 +2290,7 @@ arg : lhs '=' arg
/*%%%*/ /*%%%*/
$$ = call_bin_op($1, tRSHFT, $3); $$ = call_bin_op($1, tRSHFT, $3);
/*% /*%
$$ = dispatch3(binary, $1, ripper_intern(">>"), $3); $$ = dispatch3(binary, $1, ID2SYM(idGTGT), $3);
%*/ %*/
} }
| arg tANDOP arg | arg tANDOP arg
@ -3643,7 +3643,7 @@ method_call : fcall paren_args
/*%%%*/ /*%%%*/
$$ = NEW_CALL($1, $3, 0); $$ = NEW_CALL($1, $3, 0);
/*% /*%
$$ = dispatch3(call, $1, ripper_intern("::"), $3); $$ = dispatch3(call, $1, ID2SYM(idCOLON2), $3);
%*/ %*/
} }
| primary_value '.' | primary_value '.'
@ -3655,11 +3655,11 @@ method_call : fcall paren_args
paren_args paren_args
{ {
/*%%%*/ /*%%%*/
$$ = NEW_CALL($1, rb_intern("call"), $4); $$ = NEW_CALL($1, idCall, $4);
nd_set_line($$, $<num>3); nd_set_line($$, $<num>3);
/*% /*%
$$ = dispatch3(call, $1, ripper_id2sym('.'), $$ = dispatch3(call, $1, ripper_id2sym('.'),
ripper_intern("call")); ID2SYM(idCall));
$$ = method_optarg($$, $4); $$ = method_optarg($$, $4);
%*/ %*/
} }
@ -3672,11 +3672,11 @@ method_call : fcall paren_args
paren_args paren_args
{ {
/*%%%*/ /*%%%*/
$$ = NEW_CALL($1, rb_intern("call"), $4); $$ = NEW_CALL($1, idCall, $4);
nd_set_line($$, $<num>3); nd_set_line($$, $<num>3);
/*% /*%
$$ = dispatch3(call, $1, ripper_intern("::"), $$ = dispatch3(call, $1, ID2SYM(idCOLON2),
ripper_intern("call")); ID2SYM(idCall));
$$ = method_optarg($$, $4); $$ = method_optarg($$, $4);
%*/ %*/
} }
@ -4327,7 +4327,7 @@ numeric : simple_numeric
$$ = $2; $$ = $2;
$$->nd_lit = negate_lit($$->nd_lit); $$->nd_lit = negate_lit($$->nd_lit);
/*% /*%
$$ = dispatch2(unary, ripper_intern("-@"), $2); $$ = dispatch2(unary, ID2SYM(idUMinus), $2);
%*/ %*/
} }
; ;
@ -10774,16 +10774,18 @@ ripper_compile_error(struct parser_params *parser, const char *fmt, ...)
ripper_error_gen(parser); ripper_error_gen(parser);
} }
static ID id_warn, id_warning;
static void static void
ripper_warn0(struct parser_params *parser, const char *fmt) ripper_warn0(struct parser_params *parser, const char *fmt)
{ {
rb_funcall(parser->value, rb_intern("warn"), 1, STR_NEW2(fmt)); rb_funcall(parser->value, id_warn, 1, STR_NEW2(fmt));
} }
static void static void
ripper_warnI(struct parser_params *parser, const char *fmt, int a) ripper_warnI(struct parser_params *parser, const char *fmt, int a)
{ {
rb_funcall(parser->value, rb_intern("warn"), 2, rb_funcall(parser->value, id_warn, 2,
STR_NEW2(fmt), INT2NUM(a)); STR_NEW2(fmt), INT2NUM(a));
} }
@ -10791,7 +10793,7 @@ ripper_warnI(struct parser_params *parser, const char *fmt, int a)
static void static void
ripper_warnS(struct parser_params *parser, const char *fmt, const char *str) ripper_warnS(struct parser_params *parser, const char *fmt, const char *str)
{ {
rb_funcall(parser->value, rb_intern("warn"), 2, rb_funcall(parser->value, id_warn, 2,
STR_NEW2(fmt), STR_NEW2(str)); STR_NEW2(fmt), STR_NEW2(str));
} }
#endif #endif
@ -10799,27 +10801,27 @@ ripper_warnS(struct parser_params *parser, const char *fmt, const char *str)
static void static void
ripper_warnV(struct parser_params *parser, const char *fmt, VALUE v) ripper_warnV(struct parser_params *parser, const char *fmt, VALUE v)
{ {
rb_funcall(parser->value, rb_intern("warn"), 2, rb_funcall(parser->value, id_warn, 2,
STR_NEW2(fmt), v); STR_NEW2(fmt), v);
} }
static void static void
ripper_warning0(struct parser_params *parser, const char *fmt) ripper_warning0(struct parser_params *parser, const char *fmt)
{ {
rb_funcall(parser->value, rb_intern("warning"), 1, STR_NEW2(fmt)); rb_funcall(parser->value, id_warning, 1, STR_NEW2(fmt));
} }
static void static void
ripper_warningS(struct parser_params *parser, const char *fmt, const char *str) ripper_warningS(struct parser_params *parser, const char *fmt, const char *str)
{ {
rb_funcall(parser->value, rb_intern("warning"), 2, rb_funcall(parser->value, id_warning, 2,
STR_NEW2(fmt), STR_NEW2(str)); STR_NEW2(fmt), STR_NEW2(str));
} }
static void static void
ripper_warningV(struct parser_params *parser, const char *fmt, VALUE v) ripper_warningV(struct parser_params *parser, const char *fmt, VALUE v)
{ {
rb_funcall(parser->value, rb_intern("warning"), 2, rb_funcall(parser->value, id_warning, 2,
STR_NEW2(fmt), v); STR_NEW2(fmt), v);
} }
@ -11027,9 +11029,8 @@ Init_ripper(void)
{ {
ripper_init_eventids1(); ripper_init_eventids1();
ripper_init_eventids2(); ripper_init_eventids2();
/* ensure existing in symbol table */ id_warn = rb_intern_const("warn");
(void)rb_intern("||"); id_warning = rb_intern_const("warning");
(void)rb_intern("&&");
InitVM(ripper); InitVM(ripper);
} }

View file

@ -50,6 +50,7 @@ static ID register_static_symid_str(ID, VALUE);
#define tASET RUBY_TOKEN(ASET) #define tASET RUBY_TOKEN(ASET)
#define tLSHFT RUBY_TOKEN(LSHFT) #define tLSHFT RUBY_TOKEN(LSHFT)
#define tRSHFT RUBY_TOKEN(RSHFT) #define tRSHFT RUBY_TOKEN(RSHFT)
#define tCOLON2 RUBY_TOKEN(COLON2)
static const struct { static const struct {
unsigned short token; unsigned short token;
@ -72,6 +73,7 @@ static const struct {
{tASET, "[]="}, {tASET, "[]="},
{tLSHFT, "<<"}, {tLSHFT, "<<"},
{tRSHFT, ">>"}, {tRSHFT, ">>"},
{tCOLON2, "::"},
}; };
#define op_tbl_count numberof(op_tbl) #define op_tbl_count numberof(op_tbl)

View file

@ -80,6 +80,7 @@ enum ruby_method_ids {
idLTLT = RUBY_TOKEN(LSHFT), idLTLT = RUBY_TOKEN(LSHFT),
idLE = RUBY_TOKEN(LEQ), idLE = RUBY_TOKEN(LEQ),
idGT = '>', idGT = '>',
idGTGT = RUBY_TOKEN(RSHFT),
idGE = RUBY_TOKEN(GEQ), idGE = RUBY_TOKEN(GEQ),
idEq = RUBY_TOKEN(EQ), idEq = RUBY_TOKEN(EQ),
idEqq = RUBY_TOKEN(EQQ), idEqq = RUBY_TOKEN(EQQ),
@ -90,6 +91,7 @@ enum ruby_method_ids {
idNeqTilde = RUBY_TOKEN(NMATCH), idNeqTilde = RUBY_TOKEN(NMATCH),
idAREF = RUBY_TOKEN(AREF), idAREF = RUBY_TOKEN(AREF),
idASET = RUBY_TOKEN(ASET), idASET = RUBY_TOKEN(ASET),
idCOLON2 = RUBY_TOKEN(COLON2),
tPRESERVED_ID_BEGIN = <%=op_id_offset + token_op_ids.size - 1%>, tPRESERVED_ID_BEGIN = <%=op_id_offset + token_op_ids.size - 1%>,
% ids[:preserved].each do |token| % ids[:preserved].each do |token|
id<%=token%>, id<%=token%>,