* compile.c (iseq_compile_each): "a[*b] += 1" dumps core.

a patch from Yusuke ENDOH <mame AT tsg.ne.jp>.  [ruby-dev:32354]
* bootstraptest/test_syntax.rb: add a test for above.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14032 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2007-11-27 06:45:01 +00:00
parent b0986a8f82
commit 1bed0ccca8
3 changed files with 105 additions and 75 deletions

View File

@ -1,3 +1,10 @@
Tue Nov 27 15:40:05 2007 Koichi Sasada <ko1@atdot.net>
* compile.c (iseq_compile_each): "a[*b] += 1" dumps core.
a patch from Yusuke ENDOH <mame AT tsg.ne.jp>. [ruby-dev:32354]
* bootstraptest/test_syntax.rb: add a test for above.
Tue Nov 27 12:47:23 2007 Koichi Sasada <ko1@atdot.net> Tue Nov 27 12:47:23 2007 Koichi Sasada <ko1@atdot.net>
* compile.c, insns.def: change return value of "defined?" * compile.c, insns.def: change return value of "defined?"

View File

@ -608,3 +608,7 @@ assert_equal '3', %q{
p(1, (redo; 2)) p(1, (redo; 2))
end) end)
} }
assert_equal '1', %q{
a = [0]
a[*a]+=1
}

169
compile.c
View File

@ -3299,41 +3299,46 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
ID2SYM(node->nd_vid)); ID2SYM(node->nd_vid));
break; break;
} }
case NODE_OP_ASGN1:{ case NODE_OP_ASGN1: {
DECL_ANCHOR(args); DECL_ANCHOR(args);
int argc; VALUE argc;
ID id = node->nd_mid; unsigned long flag = 0;
ID id = node->nd_mid;
/* /*
* a[x] (op)= y * a[x] (op)= y
* *
* eval a # a * eval a # a
* eval x # a x * eval x # a x
* dupn 2 # a x a x * dupn 2 # a x a x
* send :[] # a x a[x] * send :[] # a x a[x]
* eval y # a x a[x] y * eval y # a x a[x] y
* send op # a x a[x]+y * send op # a x a[x]+y
* send []= # ret * send []= # ret
*/ */
/* /*
* nd_recv[nd_args->nd_body] (nd_mid)= nd_args->nd_head; * nd_recv[nd_args->nd_body] (nd_mid)= nd_args->nd_head;
* NODE_OP_ASGN nd_recv * NODE_OP_ASGN nd_recv
* nd_args->nd_head * nd_args->nd_head
* nd_args->nd_body * nd_args->nd_body
* nd_mid * nd_mid
*/ */
INIT_ANCHOR(args); COMPILE(ret, "NODE_OP_ASGN1 recv", node->nd_recv);
COMPILE(ret, "NODE_OP_ASGN1 recv", node->nd_recv); if (nd_type(node->nd_args->nd_body) != NODE_ZARRAY) {
argc = compile_array(iseq, args, node->nd_args->nd_body, Qfalse); INIT_ANCHOR(args);
POP_ELEMENT(args); argc = setup_args(iseq, args, node->nd_args->nd_body, &flag);
ADD_SEQ(ret, args); ADD_SEQ(ret, args);
ADD_INSN1(ret, nd_line(node), dupn, INT2FIX(argc+1)); }
ADD_SEND(ret, nd_line(node), ID2SYM(idAREF), INT2FIX(argc)); else {
argc = FIX2INT(0);
}
ADD_INSN1(ret, nd_line(node), dupn, INT2FIX(FIX2INT(argc)+1));
ADD_SEND_R(ret, nd_line(node), ID2SYM(idAREF), argc, Qfalse, LONG2FIX(flag));
if (id == 0 || id == 1) { if (id == 0 || id == 1) {
/* 0: or, 1: and /* 0: or, 1: and
a[x] ||= y a[x] ||= y
unless/if a[x] unless/if a[x]
@ -3341,53 +3346,67 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
else else
nil nil
end end
*/ */
LABEL *label = NEW_LABEL(nd_line(node)); LABEL *label = NEW_LABEL(nd_line(node));
LABEL *lfin = NEW_LABEL(nd_line(node)); LABEL *lfin = NEW_LABEL(nd_line(node));
if (id == 0) { if (id == 0) {
/* or */ /* or */
ADD_INSN(ret, nd_line(node), dup); ADD_INSN(ret, nd_line(node), dup);
ADD_INSNL(ret, nd_line(node), branchif, label); ADD_INSNL(ret, nd_line(node), branchif, label);
ADD_INSN(ret, nd_line(node), pop); ADD_INSN(ret, nd_line(node), pop);
} }
else { else {
/* and */ /* and */
ADD_INSNL(ret, nd_line(node), branchunless, label); ADD_INSNL(ret, nd_line(node), branchunless, label);
} }
COMPILE(ret, "NODE_OP_ASGN1 args->head: ", COMPILE(ret, "NODE_OP_ASGN1 args->head: ", node->nd_args->nd_head);
node->nd_args->nd_head); if (flag & VM_CALL_ARGS_SPLAT_BIT) {
ADD_SEND(ret, nd_line(node), ID2SYM(idASET), ADD_INSN1(ret, nd_line(node), newarray, INT2FIX(1));
INT2FIX(argc + 1)); ADD_INSN(ret, nd_line(node), concatarray);
ADD_INSNL(ret, nd_line(node), jump, lfin); ADD_SEND_R(ret, nd_line(node), ID2SYM(idASET),
ADD_LABEL(ret, label); argc, Qfalse, LONG2FIX(flag));
if (id == 0) { /* or */ }
ADD_INSN(ret, nd_line(node), swap); else {
ADD_INSN(ret, nd_line(node), pop); ADD_SEND_R(ret, nd_line(node), ID2SYM(idASET),
ADD_INSN(ret, nd_line(node), swap); INT2FIX(FIX2INT(argc) + 1), Qfalse, LONG2FIX(flag));
ADD_INSN(ret, nd_line(node), pop); }
} ADD_INSNL(ret, nd_line(node), jump, lfin);
else if (id == 1) { /* and */ ADD_LABEL(ret, label);
ADD_INSN(ret, nd_line(node), pop); if (id == 0) { /* or */
ADD_INSN(ret, nd_line(node), pop); ADD_INSN(ret, nd_line(node), swap);
ADD_INSN(ret, nd_line(node), putnil); ADD_INSN(ret, nd_line(node), pop);
} ADD_INSN(ret, nd_line(node), swap);
ADD_LABEL(ret, lfin); ADD_INSN(ret, nd_line(node), pop);
} }
else { else if (id == 1) { /* and */
COMPILE(ret, "NODE_OP_ASGN1 args->head: ", ADD_INSN(ret, nd_line(node), pop);
node->nd_args->nd_head); ADD_INSN(ret, nd_line(node), pop);
ADD_SEND(ret, nd_line(node), ID2SYM(id), INT2FIX(1)); ADD_INSN(ret, nd_line(node), putnil);
ADD_SEND(ret, nd_line(node), ID2SYM(idASET), }
INT2FIX(argc + 1)); ADD_LABEL(ret, lfin);
} }
else {
COMPILE(ret, "NODE_OP_ASGN1 args->head: ", node->nd_args->nd_head);
ADD_SEND(ret, nd_line(node), ID2SYM(id), INT2FIX(1));
if (flag & VM_CALL_ARGS_SPLAT_BIT) {
ADD_INSN1(ret, nd_line(node), newarray, INT2FIX(1));
ADD_INSN(ret, nd_line(node), concatarray);
ADD_SEND_R(ret, nd_line(node), ID2SYM(idASET),
argc, Qfalse, LONG2FIX(flag));
}
else {
ADD_SEND_R(ret, nd_line(node), ID2SYM(idASET),
INT2FIX(FIX2INT(argc) + 1), Qfalse, LONG2FIX(flag));
}
}
if (poped) { if (poped) {
ADD_INSN(ret, nd_line(node), pop); ADD_INSN(ret, nd_line(node), pop);
} }
break; break;
} }
case NODE_OP_ASGN2:{ case NODE_OP_ASGN2:{
ID atype = node->nd_next->nd_mid; ID atype = node->nd_next->nd_mid;
@ -3663,7 +3682,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
for (j=0; j<post_len; j++) { for (j=0; j<post_len; j++) {
int idx = liseq->local_size - (post_start + j); int idx = liseq->local_size - (post_start + j);
ADD_INSN1(args, nd_line(node), getlocal, INT2FIX(idx)); ADD_INSN1(args, nd_line(node), getlocal, INT2FIX(idx));
} }
ADD_INSN1(args, nd_line(node), newarray, INT2FIX(j)); ADD_INSN1(args, nd_line(node), newarray, INT2FIX(j));
ADD_INSN (args, nd_line(node), concatarray); ADD_INSN (args, nd_line(node), concatarray);
/* argc is setteled at above */ /* argc is setteled at above */