Skip to content

Commit

Permalink
better code generation for ||=; #3138
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Mar 24, 2016
1 parent 1ce471c commit 3886bec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
25 changes: 23 additions & 2 deletions mrbgems/mruby-compiler/core/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1712,14 +1712,35 @@ codegen(codegen_scope *s, node *tree, int val)
const char *name = mrb_sym2name_len(s->mrb, sym, &len);
int idx;

codegen(s, tree->car, VAL);
if ((len == 2 && name[0] == '|' && name[1] == '|') &&
((intptr_t)tree->car->car == NODE_CONST ||
(intptr_t)tree->car->car == NODE_CVAR)) {
int onerr, noexc, exc;
struct loopinfo *lp;

onerr = genop(s, MKOP_Bx(OP_ONERR, 0));
lp = loop_push(s, LOOP_BEGIN);
lp->pc1 = onerr;
exc = cursp();
codegen(s, tree->car, VAL);
lp->type = LOOP_RESCUE;
genop(s, MKOP_A(OP_POPERR, 1));
noexc = genop(s, MKOP_Bx(OP_JMP, 0));
dispatch(s, onerr);
genop(s, MKOP_A(OP_RESCUE, exc));
genop(s, MKOP_A(OP_LOADF, exc));
dispatch(s, noexc);
}
else {
codegen(s, tree->car, VAL);
}
if (len == 2 &&
((name[0] == '|' && name[1] == '|') ||
(name[0] == '&' && name[1] == '&'))) {
int pos;

pop();
pos = genop_peep(s, MKOP_AsBx(name[0] == '|' ? OP_JMPIF : OP_JMPNOT, cursp(), 0), NOVAL);
pos = genop_peep(s, MKOP_AsBx(name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), 0), NOVAL);
codegen(s, tree->cdr->cdr->car, VAL);
pop();
gen_assignment(s, tree->car, cursp(), val);
Expand Down
8 changes: 1 addition & 7 deletions mrbgems/mruby-compiler/core/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,7 @@ new_masgn(parser_state *p, node *a, node *b)
static node*
new_op_asgn(parser_state *p, node *a, mrb_sym op, node *b)
{
node *n = list4((node*)NODE_OP_ASGN, a, nsym(op), b);
if (op == mrb_intern_lit(p->mrb, "||") &&
((intptr_t)a->car == NODE_CONST || (intptr_t)a->car == NODE_CVAR)) {
return new_rescue(p, n, list1(list3(list1(new_const(p, mrb_intern_lit(p->mrb, "NameError"))),
0, new_asgn(p, a, b))), NULL);
}
return n;
return list4((node*)NODE_OP_ASGN, a, nsym(op), b);
}

/* (:int . i) */
Expand Down

0 comments on commit 3886bec

Please sign in to comment.