Permalink
Browse files

better code generation for `||=`; #3138

  • Loading branch information...
matz committed Mar 24, 2016
1 parent 1ce471c commit 3886becec5e44d59a63b84d16416b2f3645fbdac
Showing with 24 additions and 9 deletions.
  1. +23 −2 mrbgems/mruby-compiler/core/codegen.c
  2. +1 −7 mrbgems/mruby-compiler/core/parse.y
@@ -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);
@@ -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) */

0 comments on commit 3886bec

Please sign in to comment.