codegen.c 58.6 KB
Newer Older
mimaki's avatar
mimaki committed
1 2
/*
** codegen.c - mruby code generator
roco's avatar
roco committed
3
**
mimaki's avatar
mimaki committed
4 5 6
** See Copyright Notice in mruby.h
*/

mimaki's avatar
mimaki committed
7 8 9
#define CODEGEN_DUMP

#include "mruby.h"
10
#include "mruby/string.h"
11
#include "mruby/irep.h"
12
#include "mruby/compile.h"
Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
13
#include "mruby/numeric.h"
mimaki's avatar
mimaki committed
14
#include "opcode.h"
15
#include "node.h"
mimaki's avatar
mimaki committed
16 17
#include <string.h>
#include <stdlib.h>
18
#include <ctype.h>
mimaki's avatar
mimaki committed
19 20 21 22

typedef mrb_ast_node node;
typedef struct mrb_parser_state parser_state;

23 24 25 26 27 28 29 30
enum looptype {
  LOOP_NORMAL,
  LOOP_BLOCK,
  LOOP_FOR,
  LOOP_BEGIN,
  LOOP_RESCUE,
} type;

mimaki's avatar
mimaki committed
31
struct loopinfo {
32
  enum looptype type;
mimaki's avatar
mimaki committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  int pc1, pc2, pc3, acc;
  int ensure_level;
  struct loopinfo *prev;
};

typedef struct scope {
  mrb_state *mrb;
  mrb_pool *mpool;
  jmp_buf jmp;

  struct scope *prev;

  node *lv;

  int sp;
  int pc;
  int lastlabel;
50 51
  int ainfo:15;
  int mscope:1;
mimaki's avatar
mimaki committed
52 53 54

  struct loopinfo *loop;
  int ensure_level;
55
  char *filename;
56
  short lineno;
mimaki's avatar
mimaki committed
57 58

  mrb_code *iseq;
59
  short *lines;
mimaki's avatar
mimaki committed
60 61
  int icapa;

62
  mrb_irep *irep;
mimaki's avatar
mimaki committed
63
  int pcapa;
64
  int scapa;
mimaki's avatar
mimaki committed
65 66 67

  int nlocals;
  int nregs;
68
  int ai;
mimaki's avatar
mimaki committed
69 70 71 72 73

  int idx;
} codegen_scope;

static codegen_scope* scope_new(mrb_state *mrb, codegen_scope *prev, node *lv);
74
static void scope_finish(codegen_scope *s);
mimaki's avatar
mimaki committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static struct loopinfo *loop_push(codegen_scope *s, enum looptype t);
static void loop_break(codegen_scope *s, node *tree);
static void loop_pop(codegen_scope *s, int val);

static void gen_assignment(codegen_scope *s, node *node, int sp, int val);
static void gen_vmassignment(codegen_scope *s, node *tree, int rhs, int val);

static void codegen(codegen_scope *s, node *tree, int val);

static void
codegen_error(codegen_scope *s, const char *message)
{
  if (!s) return;
  while (s->prev) {
    mrb_pool_close(s->mpool);
    s = s->prev;
  }
  mrb_pool_close(s->mpool);
93
#ifdef ENABLE_STDIO
94 95 96 97 98 99
  if (s->filename && s->lineno) {
    fprintf(stderr, "codegen error:%s:%d: %s\n", s->filename, s->lineno, message);
  }
  else {
    fprintf(stderr, "codegen error: %s\n", message);
  }
100
#endif
mimaki's avatar
mimaki committed
101 102 103 104 105 106 107 108 109 110 111
  longjmp(s->jmp, 1);
}

static void*
codegen_palloc(codegen_scope *s, size_t len)
{
  void *p = mrb_pool_alloc(s->mpool, len);

  if (!p) codegen_error(s, "pool memory allocation");
  return p;
}
roco's avatar
roco committed
112

mimaki's avatar
mimaki committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
void*
codegen_malloc(codegen_scope *s, size_t len)
{
  void *p = mrb_malloc(s->mrb, len);

  if (!p) codegen_error(s, "mrb_malloc");
  return p;
}

void*
codegen_realloc(codegen_scope *s, void *p, size_t len)
{
  p = mrb_realloc(s->mrb, p, len);

  if (!p && len > 0) codegen_error(s, "mrb_realloc");
  return p;
}

static int
new_label(codegen_scope *s)
{
  s->lastlabel = s->pc;
  return s->pc;
}

static inline void
genop(codegen_scope *s, mrb_code i)
{
  if (s->pc == s->icapa) {
    s->icapa *= 2;
143
    s->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->icapa);
144 145 146
    if (s->lines) {
      s->lines = (short*)codegen_realloc(s, s->lines, sizeof(short)*s->icapa);
    }
mimaki's avatar
mimaki committed
147 148
  }
  s->iseq[s->pc] = i;
149 150 151
  if (s->lines) {
    s->lines[s->pc] = s->lineno;
  }
mimaki's avatar
mimaki committed
152 153 154
  s->pc++;
}

155 156 157
#define NOVAL  0
#define VAL    1

mimaki's avatar
mimaki committed
158 159 160
static void
genop_peep(codegen_scope *s, mrb_code i, int val)
{
161
  /* peephole optimization */
162
  if (s->lastlabel != s->pc && s->pc > 0) {
mimaki's avatar
mimaki committed
163 164 165 166 167 168
    mrb_code i0 = s->iseq[s->pc-1];
    int c1 = GET_OPCODE(i);
    int c0 = GET_OPCODE(i0);

    switch (c1) {
    case OP_MOVE:
169 170 171 172
      if (GETARG_A(i) == GETARG_B(i)) {
	/* skip useless OP_MOVE */
	return;
      }
173
      if (val) break;
mimaki's avatar
mimaki committed
174 175
      switch (c0) {
      case OP_MOVE:
roco's avatar
roco committed
176
        if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i) == GETARG_B(i0) && GETARG_A(i) >= s->nlocals) {
177
          /* skip swapping OP_MOVE */
roco's avatar
roco committed
178 179
          return;
        }
180 181 182 183
	if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
	  s->iseq[s->pc-1] = MKOP_AB(OP_MOVE, GETARG_A(i), GETARG_B(i0));
	  return;
	}
roco's avatar
roco committed
184
        break;
mimaki's avatar
mimaki committed
185
      case OP_LOADI:
roco's avatar
roco committed
186 187 188 189 190
        if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
          s->iseq[s->pc-1] = MKOP_AsBx(OP_LOADI, GETARG_A(i), GETARG_sBx(i0));
          return;
        }
        break;
mimaki's avatar
mimaki committed
191 192 193 194 195
      case OP_ARRAY:
      case OP_HASH:
      case OP_RANGE:
      case OP_AREF:
      case OP_GETUPVAR:
roco's avatar
roco committed
196 197 198 199 200
        if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
          s->iseq[s->pc-1] = MKOP_ABC(c0, GETARG_A(i), GETARG_B(i0), GETARG_C(i0));
          return;
        }
        break;
mimaki's avatar
mimaki committed
201 202 203 204 205 206 207 208
      case OP_LOADSYM:
      case OP_GETGLOBAL:
      case OP_GETIV:
      case OP_GETCV:
      case OP_GETCONST:
      case OP_GETSPECIAL:
      case OP_LOADL:
      case OP_STRING:
roco's avatar
roco committed
209 210 211 212 213
        if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
          s->iseq[s->pc-1] = MKOP_ABx(c0, GETARG_A(i), GETARG_Bx(i0));
          return;
        }
        break;
mimaki's avatar
mimaki committed
214
      case OP_SCLASS:
roco's avatar
roco committed
215 216 217 218 219
        if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
          s->iseq[s->pc-1] = MKOP_AB(c0, GETARG_A(i), GETARG_B(i0));
          return;
        }
        break;
mimaki's avatar
mimaki committed
220 221 222 223 224
      case OP_LOADNIL:
      case OP_LOADSELF:
      case OP_LOADT:
      case OP_LOADF:
      case OP_OCLASS:
roco's avatar
roco committed
225 226 227 228 229
        if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
          s->iseq[s->pc-1] = MKOP_A(c0, GETARG_A(i));
          return;
        }
        break;
230 231
      default:
	break;
mimaki's avatar
mimaki committed
232 233 234 235 236 237
      }
      break;
    case OP_SETIV:
    case OP_SETCV:
    case OP_SETCONST:
    case OP_SETMCNST:
238
    case OP_SETGLOBAL:
239
      if (val) break;
Masaki Muranaka's avatar
Masaki Muranaka committed
240
      if (c0 == OP_MOVE) {
roco's avatar
roco committed
241 242 243 244
        if (GETARG_A(i) == GETARG_A(i0)) {
          s->iseq[s->pc-1] = MKOP_ABx(c1, GETARG_B(i0), GETARG_Bx(i));
          return;
        }
mimaki's avatar
mimaki committed
245 246 247
      }
      break;
    case OP_SETUPVAR:
248
      if (val) break;
Masaki Muranaka's avatar
Masaki Muranaka committed
249
      if (c0 == OP_MOVE) {
roco's avatar
roco committed
250 251 252 253
        if (GETARG_A(i) == GETARG_A(i0)) {
          s->iseq[s->pc-1] = MKOP_ABC(c1, GETARG_B(i0), GETARG_B(i), GETARG_C(i));
          return;
        }
mimaki's avatar
mimaki committed
254 255 256 257
      }
      break;
    case OP_EPOP:
      if (c0 == OP_EPOP) {
roco's avatar
roco committed
258 259
        s->iseq[s->pc-1] = MKOP_A(OP_EPOP, GETARG_A(i0)+GETARG_A(i));
        return;
mimaki's avatar
mimaki committed
260 261 262 263
      }
      break;
    case OP_POPERR:
      if (c0 == OP_POPERR) {
roco's avatar
roco committed
264 265
        s->iseq[s->pc-1] = MKOP_A(OP_POPERR, GETARG_A(i0)+GETARG_A(i));
        return;
mimaki's avatar
mimaki committed
266 267
      }
      break;
268 269
    case OP_RETURN:
      switch (c0) {
270 271
      case OP_RETURN:
	return;
272
      case OP_MOVE:
273
	s->iseq[s->pc-1] = MKOP_AB(OP_RETURN, GETARG_B(i0), OP_R_NORMAL);
274 275 276 277 278 279 280 281 282 283 284 285 286
	return;
      case OP_LOADI:
	s->iseq[s->pc-1] = MKOP_AsBx(OP_LOADI, 0, GETARG_sBx(i0));
	genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
	return;
      case OP_ARRAY:
      case OP_HASH:
      case OP_RANGE:
      case OP_AREF:
      case OP_GETUPVAR:
	s->iseq[s->pc-1] = MKOP_ABC(c0, 0, GETARG_B(i0), GETARG_C(i0));
	genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
	return;
287 288 289 290 291 292 293 294 295 296 297
      case OP_SETIV:
      case OP_SETCV:
      case OP_SETCONST:
      case OP_SETMCNST:
      case OP_SETUPVAR:
      case OP_SETGLOBAL:
	s->pc--;
	genop_peep(s, i0, NOVAL);
	i0 = s->iseq[s->pc-1];
	genop(s, MKOP_AB(OP_RETURN, GETARG_A(i0), OP_R_NORMAL));
	return;
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
      case OP_LOADSYM:
      case OP_GETGLOBAL:
      case OP_GETIV:
      case OP_GETCV:
      case OP_GETCONST:
      case OP_GETSPECIAL:
      case OP_LOADL:
      case OP_STRING:
	s->iseq[s->pc-1] = MKOP_ABx(c0, 0, GETARG_Bx(i0));
	genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
	return;
      case OP_SCLASS:
	s->iseq[s->pc-1] = MKOP_AB(c0, GETARG_A(i), GETARG_B(i0));
	genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
	return;
      case OP_LOADNIL:
      case OP_LOADSELF:
      case OP_LOADT:
      case OP_LOADF:
      case OP_OCLASS:
	s->iseq[s->pc-1] = MKOP_A(c0, 0);
	genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
	return;
      default:
	break;
      }
      break;
325 326 327 328 329 330 331 332 333 334 335 336 337
    case OP_ADD:
    case OP_SUB:
      if (c0 == OP_LOADI) {
	int c = GETARG_sBx(i0);
	
	if (c1 == OP_SUB) c = -c;
	if (c > 127 || c < -127) break;
	if (0 <= c) 
	  s->iseq[s->pc-1] = MKOP_ABC(OP_ADDI, GETARG_A(i), GETARG_B(i), c);
	else
	  s->iseq[s->pc-1] = MKOP_ABC(OP_SUBI, GETARG_A(i), GETARG_B(i), -c);
	return;
      }
338 339
    default:
      break;
mimaki's avatar
mimaki committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    }
  }
  genop(s, i);
}

static void
scope_error(codegen_scope *s)
{
  exit(1);
}

static inline void
dispatch(codegen_scope *s, int pc)
{
  int diff = s->pc - pc;
  mrb_code i = s->iseq[pc];
  int c = GET_OPCODE(i);

  s->lastlabel = s->pc;
  switch (c) {
  case OP_JMP:
  case OP_JMPIF:
  case OP_JMPNOT:
  case OP_ONERR:
    break;
  default:
366
#ifdef ENABLE_STDIO
mimaki's avatar
mimaki committed
367
    fprintf(stderr, "bug: dispatch on non JMP op\n");
368
#endif
mimaki's avatar
mimaki committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    scope_error(s);
  }
  s->iseq[pc] = MKOP_AsBx(c, GETARG_A(i), diff);
}

static void
dispatch_linked(codegen_scope *s, int pc)
{
  mrb_code i;
  int pos;

  if (!pc) return;
  for (;;) {
    i = s->iseq[pc];
    pos = GETARG_sBx(i);
    dispatch(s, pc);
    if (!pos) break;
    pc = pos;
  }
}

#define nregs_update do {if (s->sp > s->nregs) s->nregs = s->sp;} while (0)
static void
push_(codegen_scope *s)
{
  if (s->sp > 511) {
    codegen_error(s, "too complex expression");
  }
  s->sp++;
  nregs_update;
}

#define push() push_(s)
402 403
#define pop_(s) ((s)->sp--)
#define pop() pop_(s)
mimaki's avatar
mimaki committed
404 405 406 407 408 409 410 411
#define pop_n(n) (s->sp-=(n))
#define cursp() (s->sp)

static inline int
new_lit(codegen_scope *s, mrb_value val)
{
  int i;

412 413
  for (i=0; i<s->irep->plen; i++) {
    if (mrb_obj_equal(s->mrb, s->irep->pool[i], val)) return i;
mimaki's avatar
mimaki committed
414
  }
415
  if (s->irep->plen == s->pcapa) {
mimaki's avatar
mimaki committed
416
    s->pcapa *= 2;
417
    s->irep->pool = (mrb_value *)codegen_realloc(s, s->irep->pool, sizeof(mrb_value)*s->pcapa);
mimaki's avatar
mimaki committed
418
  }
419 420 421 422
  s->irep->pool[s->irep->plen] = val;
  i = s->irep->plen++;
  
  return i;
mimaki's avatar
mimaki committed
423 424 425 426 427 428 429
}

static inline int
new_msym(codegen_scope *s, mrb_sym sym)
{
  int i, len;

430
  len = s->irep->slen;
431
  if (len > 256) len = 256;
mimaki's avatar
mimaki committed
432
  for (i=0; i<len; i++) {
433 434
    if (s->irep->syms[i] == sym) return i;
    if (s->irep->syms[i] == 0) break;
mimaki's avatar
mimaki committed
435
  }
436
  if (i == 256) {
mimaki's avatar
mimaki committed
437 438
    codegen_error(s, "too many symbols (max 256)");
  }
439 440
  s->irep->syms[i] = sym;
  if (i == s->irep->slen) s->irep->slen++;
mimaki's avatar
mimaki committed
441 442 443 444 445 446 447 448
  return i;
}

static inline int
new_sym(codegen_scope *s, mrb_sym sym)
{
  int i;

449 450
  for (i=0; i<s->irep->slen; i++) {
    if (s->irep->syms[i] == sym) return i;
mimaki's avatar
mimaki committed
451
  }
452 453 454
  if (s->irep->slen > 125 && s->irep->slen < 256) {
    s->irep->syms = (mrb_sym *)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*65536);
    for (i = 0; i < 256 - s->irep->slen; i++) {
455
      static const mrb_sym mrb_sym_zero = { 0 };
456
      s->irep->syms[i + s->irep->slen] = mrb_sym_zero;
457
    }
458
    s->irep->slen = 256;
mimaki's avatar
mimaki committed
459
  }
460 461
  s->irep->syms[s->irep->slen] = sym;
  return s->irep->slen++;
mimaki's avatar
mimaki committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475
}

static int
node_len(node *tree)
{
  int n = 0;

  while (tree) {
    n++;
    tree = tree->cdr;
  }
  return n;
}

476 477
#define sym(x) ((mrb_sym)(intptr_t)(x))
#define lv_name(lv) sym((lv)->car)
mimaki's avatar
mimaki committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
static int
lv_idx(codegen_scope *s, mrb_sym id)
{
  node *lv = s->lv;
  int n = 1;

  while (lv) {
    if (lv_name(lv) == id) return n;
    n++;
    lv = lv->cdr;
  }
  return 0;
}

static void
for_body(codegen_scope *s, node *tree)
{
  codegen_scope *prev = s;
  int idx, base = s->idx;
  struct loopinfo *lp;
  node *n2;
  mrb_code c;

  // generate receiver
  codegen(s, tree->cdr->car, VAL);
  // generate loop-block
  s = scope_new(s->mrb, s, tree->car);
  idx = s->idx;

  lp = loop_push(s, LOOP_FOR);
  lp->pc1 = new_label(s);

  // generate loop variable
  n2 = tree->car;
  if (n2->car && !n2->car->cdr && !n2->cdr) {
    genop(s, MKOP_Ax(OP_ENTER, 1<<18));
    gen_assignment(s, n2->car->car, 1, NOVAL);
  }
  else {
    genop(s, MKOP_Ax(OP_ENTER, 1<<18));
    gen_vmassignment(s, n2, 1, VAL);
  }
  codegen(s, tree->cdr->cdr->car, VAL);
  pop();
522 523 524 525
  if (s->pc > 0) {
    c = s->iseq[s->pc-1];
    if (GET_OPCODE(c) != OP_RETURN || GETARG_B(c) != OP_R_NORMAL || s->pc == s->lastlabel)
      genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
mimaki's avatar
mimaki committed
526 527
  }
  loop_pop(s, NOVAL);
528
  scope_finish(s);
mimaki's avatar
mimaki committed
529 530 531 532
  s = prev;
  genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx - base, OP_L_BLOCK));
  pop();
  idx = new_msym(s, mrb_intern(s->mrb, "each"));
533
  genop(s, MKOP_ABC(OP_SENDB, cursp(), idx, 0));
mimaki's avatar
mimaki committed
534 535 536 537 538 539 540 541 542 543
}

static int
lambda_body(codegen_scope *s, node *tree, int blk)
{
  int idx, base = s->idx;
  mrb_code c;

  s = scope_new(s->mrb, s, tree->car);
  idx = s->idx;
544
  s->mscope = !blk;
mimaki's avatar
mimaki committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

  if (blk) {
    struct loopinfo *lp = loop_push(s, LOOP_BLOCK);
    lp->pc1 = new_label(s);
  }
  tree = tree->cdr;
  if (tree->car) {
    int ma, oa, ra, pa, ka, kd, ba, a;
    int pos, i;
    node *n, *opt;

    ma = node_len(tree->car->car);
    n = tree->car->car;
    while (n) {
      n = n->cdr;
    }
    oa = node_len(tree->car->cdr->car);
    ra = tree->car->cdr->cdr->car ? 1 : 0;
    pa = node_len(tree->car->cdr->cdr->cdr->car);
    ka = kd = 0;
    ba = tree->car->cdr->cdr->cdr->cdr ? 1 : 0;

    a = ((ma & 0x1f) << 18)
      | ((oa & 0x1f) << 13)
      | ((ra & 1) << 12)
      | ((pa & 0x1f) << 7)
      | ((ka & 0x1f) << 2)
      | ((kd & 1)<< 1)
      | (ba & 1);
    s->ainfo = (((ma+oa) & 0x3f) << 6) /* (12bits = 6:1:5) */
      | ((ra & 1) << 5)
      | (pa & 0x1f);
    genop(s, MKOP_Ax(OP_ENTER, a));
    pos = new_label(s);
    for (i=0; i<oa; i++) {
      new_label(s);
581
      genop(s, MKOP_sBx(OP_JMP, 0));
mimaki's avatar
mimaki committed
582 583
    }
    if (oa > 0) {
584
      genop(s, MKOP_sBx(OP_JMP, 0));
mimaki's avatar
mimaki committed
585 586 587 588 589 590 591 592
    }
    opt = tree->car->cdr->car;
    i = 0;
    while (opt) {
      int idx;

      dispatch(s, pos+i);
      codegen(s, opt->car->cdr, VAL);
593
      idx = lv_idx(s, (mrb_sym)(intptr_t)opt->car->car);
mimaki's avatar
mimaki committed
594 595 596 597 598 599 600 601 602 603 604
      pop();
      genop_peep(s, MKOP_AB(OP_MOVE, idx, cursp()), NOVAL);
      i++;
      opt = opt->cdr;
    }
    if (oa > 0) {
      dispatch(s, pos+i);
    }
  }
  codegen(s, tree->cdr->car, VAL);
  pop();
605 606 607 608 609 610 611 612 613 614
  if (s->pc > 0) {
    c = s->iseq[s->pc-1];
    if (GET_OPCODE(c) != OP_RETURN || GETARG_B(c) != OP_R_NORMAL || s->pc == s->lastlabel) {
      if (s->nregs == 0) {
        genop(s, MKOP_A(OP_LOADNIL, 0));
        genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
      }
      else {
        genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
      }
615
    }
mimaki's avatar
mimaki committed
616 617 618 619
  }
  if (blk) {
    loop_pop(s, NOVAL);
  }
620
  scope_finish(s);
mimaki's avatar
mimaki committed
621 622 623 624 625 626 627 628 629 630

  return idx - base;
}

static int
scope_body(codegen_scope *s, node *tree)
{
  codegen_scope *scope = scope_new(s->mrb, s, tree->car);
  int idx = scope->idx;

631
  codegen(scope, tree->cdr, VAL);
mimaki's avatar
mimaki committed
632 633 634 635
  if (!s->iseq) {
    genop(scope, MKOP_A(OP_STOP, 0));
  }
  else {
636
    if (scope->nregs == 0) {
637 638 639 640 641
      genop(scope, MKOP_A(OP_LOADNIL, 0));
      genop(scope, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
    }
    else {
      genop_peep(scope, MKOP_AB(OP_RETURN, scope->sp, OP_R_NORMAL), NOVAL);
642
    }
mimaki's avatar
mimaki committed
643
  }
644
  scope_finish(scope);
mimaki's avatar
mimaki committed
645 646 647 648 649 650 651 652

  return idx - s->idx;
}

static int
nosplat(node *t)
{
  while (t) {
653
    if ((intptr_t)t->car->car == NODE_SPLAT) return FALSE;
mimaki's avatar
mimaki committed
654 655
    t = t->cdr;
  }
656
  return TRUE;
mimaki's avatar
mimaki committed
657 658 659 660 661
}

static mrb_sym
attrsym(codegen_scope *s, mrb_sym a)
{
662 663
  const char *name;
  int len;
mimaki's avatar
mimaki committed
664 665
  char *name2;

666
  name = mrb_sym2name_len(s->mrb, a, &len);
667
  name2 = (char *)codegen_palloc(s, len+1);
668
  memcpy(name2, name, len);
mimaki's avatar
mimaki committed
669 670 671
  name2[len] = '=';
  name2[len+1] = '\0';

672
  return mrb_intern2(s->mrb, name2, len+1);
mimaki's avatar
mimaki committed
673 674 675
}

static int
676
gen_values(codegen_scope *s, node *t, int val)
mimaki's avatar
mimaki committed
677 678 679 680 681
{
  int n = 0;

  while (t) {
    if ((intptr_t)t->car->car == NODE_SPLAT) { // splat mode
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
      if (val) {
	pop_n(n);
	genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), n));
	push();
	codegen(s, t->car, VAL);
	pop(); pop();
	genop(s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));
	t = t->cdr;
	while (t) {
	  push();
	  codegen(s, t->car, VAL);
	  pop(); pop();
	  if ((intptr_t)t->car->car == NODE_SPLAT) {
	    genop(s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));
	  }
	  else {
	    genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));
	  }
	  t = t->cdr;
	}
      }
      else {
	codegen(s, t->car->cdr, NOVAL);
	t = t->cdr;
	while (t) {
	  codegen(s, t->car, NOVAL);
	  t = t->cdr;
	}
mimaki's avatar
mimaki committed
710 711 712 713
      }
      return -1;
    }
    // normal (no splat) mode
714
    codegen(s, t->car, val);
mimaki's avatar
mimaki committed
715 716 717 718 719 720 721 722 723 724 725
    n++;
    t = t->cdr;
  }
  return n;
}

#define CALL_MAXARGS 127

static void
gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val)
{
726
  mrb_sym sym = name ? name : sym(tree->cdr->car);
mimaki's avatar
mimaki committed
727
  int idx;
728
  int n = 0, noop = 0, sendv = 0, blk = 0;
mimaki's avatar
mimaki committed
729 730 731 732 733

  codegen(s, tree->car, VAL); /* receiver */
  idx = new_msym(s, sym);
  tree = tree->cdr->cdr->car;
  if (tree) {
734
    n = gen_values(s, tree->car, VAL);
mimaki's avatar
mimaki committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
    if (n < 0) {
      n = noop = sendv = 1;
      push();
    }
  }
  if (sp) {
    if (sendv) {
      pop();
      genop(s, MKOP_AB(OP_ARYPUSH, cursp(), sp));
      push();
    }
    else {
      genop(s, MKOP_AB(OP_MOVE, cursp(), sp));
      push();
      n++;
    }
  }
  if (tree && tree->cdr) {
    noop = 1;
    codegen(s, tree->cdr, VAL);
    pop();
  }
  else {
758
    blk = cursp();
mimaki's avatar
mimaki committed
759 760 761
  }
  pop_n(n+1);
  {
762 763
    int len;
    const char *name = mrb_sym2name_len(s->mrb, sym, &len);
mimaki's avatar
mimaki committed
764

765
    if (!noop && len == 1 && name[0] == '+')  {
766
      genop_peep(s, MKOP_ABC(OP_ADD, cursp(), idx, n), val);
mimaki's avatar
mimaki committed
767
    }
768
    else if (!noop && len == 1 && name[0] == '-')  {
769
      genop_peep(s, MKOP_ABC(OP_SUB, cursp(), idx, n), val);
mimaki's avatar
mimaki committed
770
    }
771
    else if (!noop && len == 1 && name[0] == '*')  {
772 773
      genop(s, MKOP_ABC(OP_MUL, cursp(), idx, n));
    }
774
    else if (!noop && len == 1 && name[0] == '/')  {
775 776
      genop(s, MKOP_ABC(OP_DIV, cursp(), idx, n));
    }
777
    else if (!noop && len == 1 && name[0] == '<')  {
mimaki's avatar
mimaki committed
778 779
      genop(s, MKOP_ABC(OP_LT, cursp(), idx, n));
    }
780
    else if (!noop && len == 2 && name[0] == '<' && name[1] == '=')  {
mimaki's avatar
mimaki committed
781 782
      genop(s, MKOP_ABC(OP_LE, cursp(), idx, n));
    }
783
    else if (!noop && len == 1 && name[0] == '>')  {
mimaki's avatar
mimaki committed
784 785
      genop(s, MKOP_ABC(OP_GT, cursp(), idx, n));
    }
786
    else if (!noop && len == 2 && name[0] == '>' && name[1] == '=')  {
mimaki's avatar
mimaki committed
787 788
      genop(s, MKOP_ABC(OP_GE, cursp(), idx, n));
    }
789
    else if (!noop && len == 2 && name[0] == '=' && name[1] == '=')  {
790 791
      genop(s, MKOP_ABC(OP_EQ, cursp(), idx, n));
    }
mimaki's avatar
mimaki committed
792 793
    else {
      if (sendv) n = CALL_MAXARGS;
794 795 796 797 798 799
      if (blk > 0) {		   /* no block */
	genop(s, MKOP_ABC(OP_SEND, cursp(), idx, n));
      }
      else {
	genop(s, MKOP_ABC(OP_SENDB, cursp(), idx, n));
      }
mimaki's avatar
mimaki committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
    }
  }
  if (val) {
    push();
  }
}

static void
gen_assignment(codegen_scope *s, node *node, int sp, int val)
{
  int idx;
  int type = (intptr_t)node->car;

  node = node->cdr;
  switch ((intptr_t)type) {
  case NODE_GVAR:
816
    idx = new_sym(s, sym(node));
mimaki's avatar
mimaki committed
817 818 819
    genop_peep(s, MKOP_ABx(OP_SETGLOBAL, sp, idx), val);
    break;
  case NODE_LVAR:
820
    idx = lv_idx(s, sym(node));
mimaki's avatar
mimaki committed
821 822
    if (idx > 0) {
      if (idx != sp) {
roco's avatar
roco committed
823
        genop_peep(s, MKOP_AB(OP_MOVE, idx, sp), val);
mimaki's avatar
mimaki committed
824 825 826
      }
      break;
    }
roco's avatar
roco committed
827
    else {                      /* upvar */
mimaki's avatar
mimaki committed
828 829 830 831
      int lv = 0;
      codegen_scope *up = s->prev;

      while (up) {
832
        idx = lv_idx(up, sym(node));
roco's avatar
roco committed
833 834 835 836 837 838
        if (idx > 0) {
          genop_peep(s, MKOP_ABC(OP_SETUPVAR, sp, idx, lv), val);
          break;
        }
        lv++;
        up = up->prev;
mimaki's avatar
mimaki committed
839 840 841 842 843
      }
      //      assert(up!=0);
    }
    break;
  case NODE_IVAR:
844
    idx = new_sym(s, sym(node));
mimaki's avatar
mimaki committed
845 846 847
    genop_peep(s, MKOP_ABx(OP_SETIV, sp, idx), val);
    break;
  case NODE_CVAR:
848
    idx = new_sym(s, sym(node));
mimaki's avatar
mimaki committed
849 850 851
    genop_peep(s, MKOP_ABx(OP_SETCV, sp, idx), val);
    break;
  case NODE_CONST:
852
    idx = new_sym(s, sym(node));
mimaki's avatar
mimaki committed
853 854 855
    genop_peep(s, MKOP_ABx(OP_SETCONST, sp, idx), val);
    break;
  case NODE_COLON2:
856
    idx = new_sym(s, sym(node->cdr));
mimaki's avatar
mimaki committed
857 858 859 860 861 862 863 864 865
    genop_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), NOVAL);
    push();
    codegen(s, node->car, VAL);
    pop_n(2);
    genop_peep(s, MKOP_ABx(OP_SETMCNST, cursp(), idx), val);
    break;

  case NODE_CALL:
    push();
866 867 868 869 870
    gen_call(s, node, attrsym(s, sym(node->cdr->car)), sp, NOVAL);
    pop();
    if (val) {
      genop_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), val);
    }
mimaki's avatar
mimaki committed
871 872 873
    break;

  default:
874
#ifdef ENABLE_STDIO
mimaki's avatar
mimaki committed
875
    printf("unknown lhs %d\n", type);
876
#endif
mimaki's avatar
mimaki committed
877 878 879 880 881 882 883 884 885 886 887
    break;
  }
  if (val) push();
}

static void
gen_vmassignment(codegen_scope *s, node *tree, int rhs, int val)
{
  int n = 0, post = 0;
  node *t, *p;

roco's avatar
roco committed
888
  if (tree->car) {              /* pre */
mimaki's avatar
mimaki committed
889 890 891 892 893 894 895 896 897 898 899
    t = tree->car;
    n = 0;
    while (t) {
      genop(s, MKOP_ABC(OP_AREF, cursp(), rhs, n));
      gen_assignment(s, t->car, cursp(), NOVAL);
      n++;
      t = t->cdr;
    }
  }
  t = tree->cdr;
  if (t) {
roco's avatar
roco committed
900
    if (t->cdr) {               /* post count */
mimaki's avatar
mimaki committed
901 902
      p = t->cdr->car;
      while (p) {
roco's avatar
roco committed
903 904
        post++;
        p = p->cdr;
mimaki's avatar
mimaki committed
905 906 907 908 909 910 911 912 913
      }
    }
    if (val) {
      genop(s, MKOP_AB(OP_MOVE, cursp(), rhs));
      push();
    }
    pop();
    genop(s, MKOP_ABC(OP_APOST, cursp(), n, post));
    n = 1;
roco's avatar
roco committed
914
    if (t->car) {               /* rest */
mimaki's avatar
mimaki committed
915 916 917 918 919
      gen_assignment(s, t->car, cursp(), NOVAL);
    }
    if (t->cdr && t->cdr->car) {
      t = t->cdr->car;
      while (t) {
roco's avatar
roco committed
920 921 922
        gen_assignment(s, t->car, cursp()+n, NOVAL);
        t = t->cdr;
        n++;
mimaki's avatar
mimaki committed
923 924 925 926 927 928 929 930 931 932
      }
    }
  }
}

static void
raise_error(codegen_scope *s, const char *msg)
{
  int idx = new_lit(s, mrb_str_new_cstr(s->mrb, msg));

933
  genop(s, MKOP_ABx(OP_ERR, 1, idx));
mimaki's avatar
mimaki committed
934 935
}

936
static double
937 938 939
readint_float(codegen_scope *s, const char *p, int base)
{
  const char *e = p + strlen(p);
940
  double f = 0;
941 942
  int n;

943
  if (*p == '+') p++;
944 945 946
  while (p < e) {
    char c = *p;
    c = tolower((unsigned char)c);
947 948 949 950 951 952 953
    for (n=0; n<base; n++) {
      if (mrb_digitmap[n] == c) {
	f *= base;
	f += n;
	break;
      }
    }
954 955 956
    if (n == base) {
      codegen_error(s, "malformed readint input");
    }
957 958 959 960 961
    p++;
  }
  return f;
}

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
static mrb_int
readint_mrb_int(codegen_scope *s, const char *p, int base, int neg, int *overflow)
{
  const char *e = p + strlen(p);
  mrb_int result = 0;
  int n;

  if (*p == '+') p++;
  while (p < e) {
    char c = *p;
    c = tolower((unsigned char)c);
    for (n=0; n<base; n++) {
      if (mrb_digitmap[n] == c) {
	break;
      }
    }
    if (n == base) {
      codegen_error(s, "malformed readint input");
    }

    if (neg) {
      if ((MRB_INT_MIN + n)/base > result) {
        *overflow = TRUE;
        return 0;
      }
      result *= base;
      result -= n;
    }
    else {
      if ((MRB_INT_MAX - n)/base < result) {
        *overflow = TRUE;
        return 0;
      }
      result *= base;
      result += n;
    }
    p++;
  }
  *overflow = FALSE;
  return result;
}

mimaki's avatar
mimaki committed
1004 1005 1006 1007 1008 1009 1010
static void
codegen(codegen_scope *s, node *tree, int val)
{
  int nt;

  if (!tree) return;
  nt = (intptr_t)tree->car;
1011
  s->lineno = tree->lineno;
mimaki's avatar
mimaki committed
1012 1013 1014
  tree = tree->cdr;
  switch (nt) {
  case NODE_BEGIN:
1015 1016 1017 1018
    if (val && !tree) {
      genop(s, MKOP_A(OP_LOADNIL, cursp()));
      push();
    }
mimaki's avatar
mimaki committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
    while (tree) {
      codegen(s, tree->car, tree->cdr ? NOVAL : val);
      tree = tree->cdr;
    }
    break;

  case NODE_RESCUE:
    {
      int onerr, noexc, exend, pos1, pos2, tmp;
      struct loopinfo *lp;

      onerr = new_label(s);
      genop(s, MKOP_Bx(OP_ONERR, 0));
      lp = loop_push(s, LOOP_BEGIN);
      lp->pc1 = onerr;
      if (tree->car) {
roco's avatar
roco committed
1035
        codegen(s, tree->car, val);
1036
	if (val) pop();
mimaki's avatar
mimaki committed
1037 1038 1039 1040 1041 1042 1043 1044 1045
      }
      lp->type = LOOP_RESCUE;
      noexc = new_label(s);
      genop(s, MKOP_Bx(OP_JMP, 0));
      dispatch(s, onerr);
      tree = tree->cdr;
      exend = 0;
      pos1 = 0;
      if (tree->car) {
roco's avatar
roco committed
1046 1047 1048 1049 1050 1051 1052
        node *n2 = tree->car;
        int exc = cursp();

        genop(s, MKOP_A(OP_RESCUE, exc));
        push();
        while (n2) {
          node *n3 = n2->car;
1053
          node *n4 = n3->car;
roco's avatar
roco committed
1054 1055

          if (pos1) dispatch(s, pos1);
1056 1057 1058
          pos2 = 0;
          do {
            if (n4) {
roco's avatar
roco committed
1059
              codegen(s, n4->car, VAL);
1060 1061 1062
            }
            else {
              genop(s, MKOP_ABx(OP_GETCONST, cursp(), new_msym(s, mrb_intern(s->mrb, "StandardError"))));
roco's avatar
roco committed
1063
              push();
1064 1065
            }
            genop(s, MKOP_AB(OP_MOVE, cursp(), exc));
1066
            pop();
1067 1068 1069 1070 1071
            genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern(s->mrb, "===")), 1));
            tmp = new_label(s);
            genop(s, MKOP_AsBx(OP_JMPIF, cursp(), pos2));
            pos2 = tmp;
            if (n4) {
roco's avatar
roco committed
1072 1073
              n4 = n4->cdr;
            }
1074 1075
          } while (n4);
          pos1 = new_label(s);
Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
1076
          genop(s, MKOP_sBx(OP_JMP, 0));
1077 1078
          dispatch_linked(s, pos2);

roco's avatar
roco committed
1079 1080 1081 1082 1083 1084 1085 1086
          pop();
          if (n3->cdr->car) {
            gen_assignment(s, n3->cdr->car, exc, NOVAL);
          }
          if (n3->cdr->cdr->car) {
            codegen(s, n3->cdr->cdr->car, val);
          }
          tmp = new_label(s);
Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
1087
          genop(s, MKOP_sBx(OP_JMP, exend));
roco's avatar
roco committed
1088 1089 1090 1091 1092 1093 1094 1095
          exend = tmp;
          n2 = n2->cdr;
          push();
        }
        if (pos1) {
          dispatch(s, pos1);
          genop(s, MKOP_A(OP_RAISE, exc));
        }
mimaki's avatar
mimaki committed
1096
      }
1097
      pop();
mimaki's avatar
mimaki committed
1098 1099 1100 1101
      tree = tree->cdr;
      dispatch(s, noexc);
      genop(s, MKOP_A(OP_POPERR, 1));
      if (tree->car) {
roco's avatar
roco committed
1102
        codegen(s, tree->car, val);
mimaki's avatar
mimaki committed
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
      }
      dispatch_linked(s, exend);
      loop_pop(s, NOVAL);
    }
    break;

  case NODE_ENSURE:
    {
      int idx;
      int epush = s->pc;

      genop(s, MKOP_Bx(OP_EPUSH, 0));
      s->ensure_level++;
      codegen(s, tree->car, val);
      idx = scope_body(s, tree->cdr);
      s->iseq[epush] = MKOP_Bx(OP_EPUSH, idx);
      s->ensure_level--;
      genop_peep(s, MKOP_A(OP_EPOP, 1), NOVAL);
    }
    break;

  case NODE_LAMBDA:
    {
      int idx = lambda_body(s, tree, 1);

      genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_LAMBDA));
      push();
    }
    break;

  case NODE_BLOCK:
    {
      int idx = lambda_body(s, tree, 1);

      genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_BLOCK));
      push();
    }
    break;

  case NODE_IF:
    {
      int pos1, pos2;
      node *e = tree->cdr->cdr->car;
roco's avatar
roco committed
1146

mimaki's avatar
mimaki committed
1147 1148 1149 1150 1151 1152
      codegen(s, tree->car, VAL);
      pop();
      pos1 = new_label(s);
      genop(s, MKOP_AsBx(OP_JMPNOT, cursp(), 0));

      codegen(s, tree->cdr->car, val);
1153 1154 1155 1156
      if (val && !(tree->cdr->car)) {
        genop(s, MKOP_A(OP_LOADNIL, cursp()));
        push();
      }
mimaki's avatar
mimaki committed
1157
      if (e) {
1158
        if (val) pop();
roco's avatar
roco committed
1159
        pos2 = new_label(s);
1160 1161
        genop(s, MKOP_sBx(OP_JMP, 0)); 
       dispatch(s, pos1);
roco's avatar
roco committed
1162 1163
        codegen(s, e, val);
        dispatch(s, pos2);
mimaki's avatar
mimaki committed
1164 1165
      }
      else {
roco's avatar
roco committed
1166 1167
        if (val) {
          pop();
1168 1169 1170
	  pos2 = new_label(s);
	  genop(s, MKOP_sBx(OP_JMP, 0));
	  dispatch(s, pos1);
roco's avatar
roco committed
1171
          genop(s, MKOP_A(OP_LOADNIL, cursp()));
1172
	  dispatch(s, pos2);
roco's avatar
roco committed
1173 1174
          push();
        }
1175 1176 1177
	else {
	  dispatch(s, pos1);
	}
mimaki's avatar
mimaki committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
      }
    }
    break;

  case NODE_AND:
    {
      int pos;

      codegen(s, tree->car, VAL);
      pos = new_label(s);
      pop();
      genop(s, MKOP_AsBx(OP_JMPNOT, cursp(), 0));
      codegen(s, tree->cdr, val);
      dispatch(s, pos);
    }
    break;

  case NODE_OR:
    {
      int pos;

      codegen(s, tree->car, VAL);
      pos = new_label(s);
      pop();
      genop(s, MKOP_AsBx(OP_JMPIF, cursp(), 0));
      codegen(s, tree->cdr, val);
      dispatch(s, pos);
    }
    break;

  case NODE_WHILE:
    {
      struct loopinfo *lp = loop_push(s, LOOP_NORMAL);

      lp->pc1 = new_label(s);
1213
      genop(s, MKOP_sBx(OP_JMP, 0));
mimaki's avatar
mimaki committed
1214 1215
      lp->pc2 = new_label(s);
      codegen(s, tree->cdr, NOVAL);
1216 1217 1218 1219 1220
      dispatch(s, lp->pc1);
      codegen(s, tree->car, VAL);
      pop();
      genop(s, MKOP_AsBx(OP_JMPIF, cursp(), lp->pc2 - s->pc));

mimaki's avatar
mimaki committed
1221 1222 1223 1224 1225 1226 1227 1228 1229
      loop_pop(s, val);
    }
    break;

  case NODE_UNTIL:
    {
      struct loopinfo *lp = loop_push(s, LOOP_NORMAL);

      lp->pc1 = new_label(s);
1230
      genop(s, MKOP_sBx(OP_JMP, 0));
mimaki's avatar
mimaki committed
1231 1232
      lp->pc2 = new_label(s);
      codegen(s, tree->cdr, NOVAL);
1233 1234 1235 1236 1237
      dispatch(s, lp->pc1);
      codegen(s, tree->car, VAL);
      pop();
      genop(s, MKOP_AsBx(OP_JMPNOT, cursp(), lp->pc2 - s->pc));

mimaki's avatar
mimaki committed
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
      loop_pop(s, val);
    }
    break;

  case NODE_FOR:
    for_body(s, tree);
    if (val) push();
    break;

  case NODE_CASE:
    {
      int head = 0;
      int pos1, pos2, pos3, tmp;
      node *n;

      pos3 = 0;
      if (tree->car) {
roco's avatar
roco committed
1255 1256
        head = cursp();
        codegen(s, tree->car, VAL);
mimaki's avatar
mimaki committed
1257 1258 1259
      }
      tree = tree->cdr;
      while (tree) {
roco's avatar
roco committed
1260 1261 1262 1263 1264 1265
        n = tree->car->car;
        pos1 = pos2 = 0;
        while (n) {
          codegen(s, n->car, VAL);
          if (head) {
            genop(s, MKOP_AB(OP_MOVE, cursp(), head));
1266
            pop();
roco's avatar
roco committed
1267 1268
            genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern(s->mrb, "===")), 1));
          }
1269 1270 1271
          else {
	    pop();
	  }
roco's avatar
roco committed
1272 1273 1274 1275 1276 1277 1278
          tmp = new_label(s);
          genop(s, MKOP_AsBx(OP_JMPIF, cursp(), pos2));
          pos2 = tmp;
          n = n->cdr;
        }
        if (tree->car->car) {
          pos1 = new_label(s);
1279
          genop(s, MKOP_sBx(OP_JMP, 0));
roco's avatar
roco committed
1280 1281 1282
          dispatch_linked(s, pos2);
        }
        codegen(s, tree->car->cdr, val);
1283
	if (val) pop();
roco's avatar
roco committed
1284
        tmp = new_label(s);
1285
        genop(s, MKOP_sBx(OP_JMP, pos3));
roco's avatar
roco committed
1286 1287 1288
        pos3 = tmp;
        if (pos1) dispatch(s, pos1);
        tree = tree->cdr;
mimaki's avatar
mimaki committed
1289
      }
1290 1291 1292 1293
      if (val) {
	genop(s, MKOP_A(OP_LOADNIL, cursp()));
	push();
      }
1294
      if (pos3) dispatch_linked(s, pos3);
mimaki's avatar
mimaki committed
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    }
    break;

  case NODE_SCOPE:
    scope_body(s, tree);
    break;

  case NODE_FCALL:
  case NODE_CALL:
    gen_call(s, tree, 0, 0, val);
    break;

  case NODE_DOT2:
1308 1309
    codegen(s, tree->car, val);
    codegen(s, tree->cdr, val);
mimaki's avatar
mimaki committed
1310
    if (val) {
1311
      pop(); pop();
mimaki's avatar
mimaki committed
1312 1313 1314 1315 1316 1317
      genop(s, MKOP_ABC(OP_RANGE, cursp(), cursp(), 0));
      push();
    }
    break;

  case NODE_DOT3:
1318 1319
    codegen(s, tree->car, val);
    codegen(s, tree->cdr, val);
mimaki's avatar
mimaki committed
1320
    if (val) {
1321
      pop(); pop();
mimaki's avatar
mimaki committed
1322 1323 1324 1325 1326 1327 1328
      genop(s, MKOP_ABC(OP_RANGE, cursp(), cursp(), 1));
      push();
    }
    break;

  case NODE_COLON2:
    {
1329
      int sym = new_sym(s, sym(tree->cdr));
mimaki's avatar
mimaki committed
1330 1331 1332 1333

      codegen(s, tree->car, VAL);
      pop();
      genop(s, MKOP_ABx(OP_GETMCNST, cursp(), sym));
1334
      if (val) push();
mimaki's avatar
mimaki committed
1335 1336 1337 1338 1339
    }
    break;

  case NODE_COLON3:
    {
1340
      int sym = new_sym(s, sym(tree));
mimaki's avatar
mimaki committed
1341 1342 1343

      genop(s, MKOP_A(OP_OCLASS, cursp()));
      genop(s, MKOP_ABx(OP_GETMCNST, cursp(), sym));
1344
      if (val) push();
mimaki's avatar
mimaki committed
1345 1346 1347 1348 1349 1350 1351
    }
    break;

  case NODE_ARRAY:
    {
      int n;

1352
      n = gen_values(s, tree, val);
mimaki's avatar
mimaki committed
1353
      if (n >= 0) {
roco's avatar
roco committed
1354
        if (val) {
1355
	  pop_n(n);
roco's avatar
roco committed
1356 1357 1358
          genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), n));
          push();
        }
mimaki's avatar
mimaki committed
1359 1360
      }
      else if (val) {
roco's avatar
roco committed
1361
        push();
mimaki's avatar
mimaki committed
1362 1363 1364 1365 1366 1367 1368 1369 1370
      }
    }
    break;

  case NODE_HASH:
    {
      int len = 0;

      while (tree) {
1371 1372
        codegen(s, tree->car->car, val);
        codegen(s, tree->car->cdr, val);
roco's avatar
roco committed
1373 1374
        len++;
        tree = tree->cdr;
mimaki's avatar
mimaki committed
1375 1376
      }
      if (val) {
1377
	pop_n(len*2);
roco's avatar
roco committed
1378 1379
        genop(s, MKOP_ABC(OP_HASH, cursp(), cursp(), len));
        push();
mimaki's avatar
mimaki committed
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
      }
    }
    break;

  case NODE_SPLAT:
    codegen(s, tree, VAL);
    break;

  case NODE_ASGN:
    codegen(s, tree->cdr, VAL);
    pop();
    gen_assignment(s, tree->car, cursp(), val);
    break;

  case NODE_MASGN:
    {
      int len = 0, n = 0, post = 0;
      node *t = tree->cdr, *p;
      int rhs = cursp();

      if ((intptr_t)t->car == NODE_ARRAY && nosplat(t->cdr)) {
roco's avatar
roco committed
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
        // fixed rhs
        t = t->cdr;
        while (t) {
          codegen(s, t->car, VAL);
          len++;
          t = t->cdr;
        }
        tree = tree->car;
        if (tree->car) {                /* pre */
          t = tree->car;
          n = 0;
          while (t) {
            gen_assignment(s, t->car, rhs+n, NOVAL);
            n++;
            t = t->cdr;
          }
        }
        t = tree->cdr;
        if (t) {
          if (t->cdr) {         /* post count */
            p = t->cdr->car;
            while (p) {
              post++;
              p = p->cdr;
            }
          }
          if (t->car) {         /* rest (len - pre - post) */
            int rn = len - post - n;

            genop(s, MKOP_ABC(OP_ARRAY, cursp(), rhs+n, rn));
            gen_assignment(s, t->car, cursp(), NOVAL);
            n += rn;
          }
          if (t->cdr && t->cdr->car) {
            t = t->cdr->car;
            while (n<len) {
              gen_assignment(s, t->car, rhs+n, NOVAL);
              t = t->cdr;
              n++;
            }
          }
        }
        pop_n(len);
        if (val) {
          genop(s, MKOP_ABC(OP_ARRAY, rhs, rhs, len));
          push();
        }
mimaki's avatar
mimaki committed
1448 1449
      }
      else {
roco's avatar
roco committed
1450 1451 1452 1453
        // variable rhs
        codegen(s, t, VAL);
        gen_vmassignment(s, tree->car, rhs, val);
        if (!val) pop();
mimaki's avatar
mimaki committed
1454 1455 1456 1457 1458 1459
      }
    }
    break;

  case NODE_OP_ASGN:
    {
1460
      mrb_sym sym = sym(tree->cdr->car);
1461 1462
      int len;
      const char *name = mrb_sym2name_len(s->mrb, sym, &len);
1463
      int idx;
Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
1464 1465

      codegen(s, tree->car, VAL);
1466 1467 1468
      if (len == 2 &&
	  ((name[0] == '|' && name[1] == '|') ||
	   (name[0] == '&' && name[1] == '&'))) {
Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
	int pos;

	pop();
	pos = new_label(s);
	genop(s, MKOP_AsBx(name[0] == '|' ? OP_JMPIF : OP_JMPNOT, cursp(), 0));
	codegen(s, tree->cdr->cdr->car, VAL);
	pop();
	gen_assignment(s, tree->car, cursp(), val);
	dispatch(s, pos);
	break;
      }
      codegen(s, tree->cdr->cdr->car, VAL);
      pop(); pop();
mimaki's avatar
mimaki committed
1482

Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
1483
      idx = new_msym(s, sym);
1484
      if (len == 1 && name[0] == '+')  {
1485
        genop_peep(s, MKOP_ABC(OP_ADD, cursp(), idx, 1), val);
mimaki's avatar
mimaki committed
1486
      }
1487
      else if (len == 1 && name[0] == '-')  {
1488
        genop_peep(s, MKOP_ABC(OP_SUB, cursp(), idx, 1), val);
mimaki's avatar
mimaki committed
1489
      }
1490
      else if (len == 1 && name[0] == '<')  {
1491
        genop(s, MKOP_ABC(OP_LT, cursp(), idx, 1));
mimaki's avatar
mimaki committed
1492
      }
1493
      else if (len == 2 && name[0] == '<' && name[1] == '=')  {
1494
        genop(s, MKOP_ABC(OP_LE, cursp(), idx, 1));
mimaki's avatar
mimaki committed
1495
      }
1496
      else if (len == 1 && name[0] == '>')  {
1497
        genop(s, MKOP_ABC(OP_GT, cursp(), idx, 1));
mimaki's avatar
mimaki committed
1498
      }
1499
      else if (len == 2 && name[0] == '>' && name[1] == '=')  {
1500
        genop(s, MKOP_ABC(OP_GE, cursp(), idx, 1));
mimaki's avatar
mimaki committed
1501 1502
      }
      else {
1503
	genop(s, MKOP_ABC(OP_SEND, cursp(), idx, 1));
mimaki's avatar
mimaki committed
1504 1505 1506 1507 1508 1509 1510
      }
    }
    gen_assignment(s, tree->car, cursp(), val);
    break;

  case NODE_SUPER:
    {
1511
      int n = 0, noop = 0, sendv = 0;
mimaki's avatar
mimaki committed
1512

1513
      push();			/* room for receiver */
mimaki's avatar
mimaki committed
1514
      if (tree) {
roco's avatar
roco committed
1515
        node *args = tree->car;
1516
	if (args) {
1517
	  n = gen_values(s, args, VAL);
1518 1519 1520 1521 1522
	  if (n < 0) {
	    n = noop = sendv = 1;
	    push();
	  }
	}
mimaki's avatar
mimaki committed
1523 1524
      }
      if (tree && tree->cdr) {
roco's avatar
roco committed
1525 1526
        codegen(s, tree->cdr, VAL);
        pop();
mimaki's avatar
mimaki committed
1527 1528
      }
      else {
roco's avatar
roco committed
1529
        genop(s, MKOP_A(OP_LOADNIL, cursp()));
mimaki's avatar
mimaki committed
1530 1531
      }
      pop_n(n+1);
1532
      if (sendv) n = CALL_MAXARGS;
mimaki's avatar
mimaki committed
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
      genop(s, MKOP_ABC(OP_SUPER, cursp(), 0, n));
      if (val) push();
    }
    break;

  case NODE_ZSUPER:
    {
      codegen_scope *s2 = s;
      int lv = 0, ainfo = 0;

1543
      push(); 			/* room for receiver */
1544
      while (!s2->mscope) {
roco's avatar
roco committed
1545 1546 1547
        lv++;
        s2 = s2->prev;
        if (!s2) break;
mimaki's avatar
mimaki committed
1548 1549 1550
      }
      if (s2) ainfo = s2->ainfo;
      genop(s, MKOP_ABx(OP_ARGARY, cursp(), (ainfo<<4)|(lv & 0xf)));
1551 1552 1553 1554 1555
      if (tree && tree->cdr) {
	push();
        codegen(s, tree->cdr, VAL);
        pop_n(2);
      }
mimaki's avatar
mimaki committed
1556 1557 1558 1559 1560 1561 1562 1563 1564
      pop();
      genop(s, MKOP_ABC(OP_SUPER, cursp(), 0, CALL_MAXARGS));
      if (val) push();
    }
    break;

  case NODE_RETURN:
    codegen(s, tree, VAL);
    pop();
1565
    if (s->loop) {
mimaki's avatar
mimaki committed
1566 1567 1568
      genop(s, MKOP_AB(OP_RETURN, cursp(), OP_R_RETURN));
    }
    else {
1569
      genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
mimaki's avatar
mimaki committed
1570
    }
1571
    push();
mimaki's avatar
mimaki committed
1572 1573 1574 1575 1576 1577 1578 1579
    break;

  case NODE_YIELD:
    {
      codegen_scope *s2 = s;
      int lv = 0, ainfo = 0;
      int n = 0, sendv = 0;

1580
      while (!s2->mscope) {
roco's avatar
roco committed
1581 1582 1583
        lv++;
        s2 = s2->prev;
        if (!s2) break;
mimaki's avatar
mimaki committed
1584 1585 1586 1587 1588
      }
      if (s2) ainfo = s2->ainfo;
      genop(s, MKOP_ABx(OP_BLKPUSH, cursp(), (ainfo<<4)|(lv & 0xf)));
      push();
      if (tree) {
1589
        n = gen_values(s, tree, VAL);
roco's avatar
roco committed
1590 1591 1592 1593
        if (n < 0) {
          n = sendv = 1;
          push();
        }
mimaki's avatar
mimaki committed
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
      }
      pop_n(n+1);
      if (sendv) n = CALL_MAXARGS;
      genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern(s->mrb, "call")), n));
      if (val) push();
    }
    break;

  case NODE_BREAK:
    loop_break(s, tree);
    if (val) push();
    break;

  case NODE_NEXT:
    if (!s->loop) {
      raise_error(s, "unexpected next");
    }
    else if (s->loop->type == LOOP_NORMAL) {
      if (s->ensure_level > s->loop->ensure_level) {
roco's avatar
roco committed
1613
        genop_peep(s, MKOP_A(OP_EPOP, s->ensure_level - s->loop->ensure_level), NOVAL);
mimaki's avatar
mimaki committed
1614 1615 1616 1617 1618
      }
      codegen(s, tree, NOVAL);
      genop(s, MKOP_sBx(OP_JMP, s->loop->pc1 - s->pc));
    }
    else {
1619 1620 1621 1622
      if (tree) {
        codegen(s, tree, VAL);
        pop();
      }
1623
      genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
mimaki's avatar
mimaki committed
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
    }
    if (val) push();
    break;

  case NODE_REDO:
    if (!s->loop) {
      raise_error(s, "unexpected redo");
    }
    else {
      if (s->ensure_level > s->loop->ensure_level) {
roco's avatar
roco committed
1634
        genop_peep(s, MKOP_A(OP_EPOP, s->ensure_level - s->loop->ensure_level), NOVAL);
mimaki's avatar
mimaki committed
1635 1636 1637 1638 1639 1640 1641 1642
      }
      genop(s, MKOP_sBx(OP_JMP, s->loop->pc2 - s->pc));
    }
    break;

  case NODE_RETRY:
    {
      const char *msg = "unexpected retry";
roco's avatar
roco committed
1643

mimaki's avatar
mimaki committed
1644
      if (!s->loop) {
roco's avatar
roco committed
1645
        raise_error(s, msg);
mimaki's avatar
mimaki committed
1646 1647
      }
      else {
roco's avatar
roco committed
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
        struct loopinfo *lp = s->loop;
        int n = 0;

        while (lp && lp->type != LOOP_RESCUE) {
          if (lp->type == LOOP_BEGIN) {
            n++;
          }
          lp = lp->prev;
        }
        if (!lp) {
          raise_error(s, msg);
        }
        else {
          if (n > 0) {
            while (n--) {
              genop_peep(s, MKOP_A(OP_POPERR, 1), NOVAL);
            }
          }
          if (s->ensure_level > lp->ensure_level) {
            genop_peep(s, MKOP_A(OP_EPOP, s->ensure_level - lp->ensure_level), NOVAL);
          }
          genop(s, MKOP_sBx(OP_JMP, lp->pc1 - s->pc));
        }
mimaki's avatar
mimaki committed
1671 1672 1673 1674 1675 1676
      }
    }
    break;

  case NODE_LVAR:
    if (val) {
1677
      int idx = lv_idx(s, sym(tree));
mimaki's avatar
mimaki committed
1678 1679

      if (idx > 0) {
roco's avatar
roco committed
1680
        genop(s, MKOP_AB(OP_MOVE, cursp(), idx));
mimaki's avatar
mimaki committed
1681 1682
      }
      else {
roco's avatar
roco committed
1683 1684 1685 1686
        int lv = 0;
        codegen_scope *up = s->prev;

        while (up) {
1687
          idx = lv_idx(up, sym(tree));
roco's avatar
roco committed
1688 1689 1690 1691 1692 1693 1694
          if (idx > 0) {
            genop(s, MKOP_ABC(OP_GETUPVAR, cursp(), idx, lv));
            break;
          }
          lv++;
          up = up->prev;
        }
mimaki's avatar
mimaki committed
1695 1696 1697 1698 1699 1700 1701
      }
      push();
    }
    break;

  case NODE_GVAR:
    {
1702
      int sym = new_sym(s, sym(tree));
mimaki's avatar
mimaki committed
1703 1704 1705 1706 1707 1708 1709 1710

      genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
      push();
    }
    break;

  case NODE_IVAR:
    {
1711
      int sym = new_sym(s, sym(tree));
mimaki's avatar
mimaki committed
1712 1713 1714 1715 1716 1717 1718 1719

      genop(s, MKOP_ABx(OP_GETIV, cursp(), sym));
      push();
    }
    break;

  case NODE_CVAR:
    {
1720
      int sym = new_sym(s, sym(tree));
mimaki's avatar
mimaki committed
1721 1722 1723 1724 1725 1726 1727 1728

      genop(s, MKOP_ABx(OP_GETCV, cursp(), sym));
      push();
    }
    break;

  case NODE_CONST:
    {
1729
      int sym = new_sym(s, sym(tree));
mimaki's avatar
mimaki committed
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740

      genop(s, MKOP_ABx(OP_GETCONST, cursp(), sym));
      push();
    }
    break;

  case NODE_DEFINED:
    codegen(s, tree, VAL);
    break;

  case NODE_BACK_REF:
1741 1742
    {
      char buf[4];
1743
      int len;
1744 1745
      int sym;

1746
      len = snprintf(buf, sizeof(buf), "$%c", (int)(intptr_t)tree);
1747
      sym = new_sym(s, mrb_intern2(s->mrb, buf, len));
1748 1749 1750
      genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
      push();
    }
mimaki's avatar
mimaki committed
1751 1752 1753
    break;

  case NODE_NTH_REF:
1754 1755
    {
      char buf[4];
1756
      int len;
1757 1758
      int sym;

1759
      len = snprintf(buf, sizeof(buf), "$%d", (int)(intptr_t)tree);
1760
      sym = new_sym(s, mrb_intern2(s->mrb, buf, len));
1761 1762 1763
      genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
      push();
    }
mimaki's avatar
mimaki committed
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
    break;

  case NODE_ARG:
    // should not happen
    break;

  case NODE_BLOCK_ARG:
    codegen(s, tree, VAL);
    break;

  case NODE_INT:
    if (val) {
      char *p = (char*)tree->car;
      int base = (intptr_t)tree->cdr->car;
1778
      mrb_int i;
mimaki's avatar
mimaki committed
1779
      mrb_code co;
1780
      int overflow;
mimaki's avatar
mimaki committed
1781

1782 1783 1784
      i = readint_mrb_int(s, p, base, FALSE, &overflow);
      if (overflow) {
	double f = readint_float(s, p, base);
1785 1786 1787
	int off = new_lit(s, mrb_float_value(f));

	genop(s, MKOP_ABx(OP_LOADL, cursp(), off));
mimaki's avatar
mimaki committed
1788 1789
      }
      else {
1790 1791 1792 1793 1794 1795 1796 1797
	if (i < MAXARG_sBx && i > -MAXARG_sBx) {
	  co = MKOP_AsBx(OP_LOADI, cursp(), i);
	}
	else {
	  int off = new_lit(s, mrb_fixnum_value(i));
	  co = MKOP_ABx(OP_LOADL, cursp(), off);
	}
	genop(s, co);
mimaki's avatar
mimaki committed
1798 1799 1800 1801 1802 1803 1804 1805
      }
      push();
    }
    break;

  case NODE_FLOAT:
    if (val) {
      char *p = (char*)tree;
1806
      mrb_float f = str_to_mrb_float(p);
mimaki's avatar
mimaki committed
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
      int off = new_lit(s, mrb_float_value(f));

      genop(s, MKOP_ABx(OP_LOADL, cursp(), off));
      push();
    }
    break;

  case NODE_NEGATE:
    {
      nt = (intptr_t)tree->car;
      tree = tree->cdr;
      switch (nt) {
      case NODE_FLOAT:
roco's avatar
roco committed
1820 1821
        {
          char *p = (char*)tree;
1822
          mrb_float f = str_to_mrb_float(p);
roco's avatar
roco committed
1823
          int off = new_lit(s, mrb_float_value(-f));
mimaki's avatar
mimaki committed
1824

roco's avatar
roco committed
1825 1826 1827 1828
          genop(s, MKOP_ABx(OP_LOADL, cursp(), off));
          push();
        }
        break;
mimaki's avatar
mimaki committed
1829 1830

      case NODE_INT:
roco's avatar
roco committed
1831 1832 1833
        {
          char *p = (char*)tree->car;
          int base = (intptr_t)tree->cdr->car;
1834
          mrb_int i;
roco's avatar
roco committed
1835
          mrb_code co;
1836
          int overflow;
roco's avatar
roco committed
1837

1838 1839 1840
          i = readint_mrb_int(s, p, base, TRUE, &overflow);
          if (overflow) {
	    double f = readint_float(s, p, base);
1841
	    int off = new_lit(s, mrb_float_value(-f));
1842

1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
	    genop(s, MKOP_ABx(OP_LOADL, cursp(), off));
	  }
	  else {
	    if (i < MAXARG_sBx && i > -MAXARG_sBx) {
	      co = MKOP_AsBx(OP_LOADI, cursp(), i);
	    }
	    else {
	      int off = new_lit(s, mrb_fixnum_value(i));
	      co = MKOP_ABx(OP_LOADL, cursp(), off);
	    }
	    genop(s, co);
	  }
roco's avatar
roco committed
1855 1856 1857
          push();
        }
        break;
mimaki's avatar
mimaki committed
1858 1859

      default:
roco's avatar
roco committed
1860 1861
        {
          int sym = new_msym(s, mrb_intern(s->mrb, "-"));
mimaki's avatar
mimaki committed
1862

roco's avatar
roco committed
1863 1864 1865 1866 1867 1868 1869
          genop(s, MKOP_ABx(OP_LOADI, cursp(), 0));
          push();
          codegen(s, tree, VAL);
          pop(); pop();
          genop(s, MKOP_ABC(OP_SUB, cursp(), sym, 2));
        }
        break;
mimaki's avatar
mimaki committed
1870 1871 1872 1873 1874 1875 1876 1877
      }
    }
    break;

  case NODE_STR:
    if (val) {
      char *p = (char*)tree->car;
      size_t len = (intptr_t)tree->cdr;
1878
      int ai = mrb_gc_arena_save(s->mrb);
mimaki's avatar
mimaki committed
1879 1880
      int off = new_lit(s, mrb_str_new(s->mrb, p, len));

1881
      mrb_gc_arena_restore(s->mrb, ai);
mimaki's avatar
mimaki committed
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
      genop(s, MKOP_ABx(OP_STRING, cursp(), off));
      push();
    }
    break;

  case NODE_DSTR:
    if (val) {
      node *n = tree;

      codegen(s, n->car, VAL);
      n = n->cdr;
      while (n) {
roco's avatar
roco committed
1894 1895 1896 1897 1898
        codegen(s, n->car, VAL);
        pop(); pop();
        genop(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1));
        push();
        n = n->cdr;
mimaki's avatar
mimaki committed
1899 1900 1901 1902 1903 1904
      }
    }
    else {
      node *n = tree;

      while (n) {
roco's avatar
roco committed
1905 1906 1907 1908
        if ((intptr_t)n->car->car != NODE_STR) {
          codegen(s, n->car, NOVAL);
        }
        n = n->cdr;
mimaki's avatar
mimaki committed
1909 1910 1911 1912 1913 1914
      }
    }
    break;

  case NODE_SYM:
    if (val) {
1915
      int sym = new_sym(s, sym(tree));
mimaki's avatar
mimaki committed
1916 1917 1918 1919 1920 1921

      genop(s, MKOP_ABx(OP_LOADSYM, cursp(), sym));
      push();
    }
    break;

1922 1923 1924 1925 1926 1927 1928 1929 1930
  case NODE_DSYM:
    codegen(s, tree, val);
    if (val) {
      pop();
      genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern(s->mrb, "intern")), 0));
      push();
    }
    break;

mimaki's avatar
mimaki committed
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
  case NODE_SELF:
    if (val) {
      genop(s, MKOP_A(OP_LOADSELF, cursp()));
      push();
    }
    break;

  case NODE_NIL:
    if (val) {
      genop(s, MKOP_A(OP_LOADNIL, cursp()));
      push();
    }
    break;

  case NODE_TRUE:
    if (val) {
      genop(s, MKOP_A(OP_LOADT, cursp()));
      push();
    }
    break;

  case NODE_FALSE:
    if (val) {
      genop(s, MKOP_A(OP_LOADF, cursp()));
      push();
    }
    break;

  case NODE_ALIAS:
    {
1961 1962
      int a = new_msym(s, sym(tree->car));
      int b = new_msym(s, sym(tree->cdr));
mimaki's avatar
mimaki committed
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
      int c = new_msym(s, mrb_intern(s->mrb, "alias_method"));

      genop(s, MKOP_A(OP_TCLASS, cursp()));
      push();
      genop(s, MKOP_ABx(OP_LOADSYM, cursp(), a));
      push();
      genop(s, MKOP_ABx(OP_LOADSYM, cursp(), b));
      push();
      genop(s, MKOP_A(OP_LOADNIL, cursp()));
      pop_n(3);
      genop(s, MKOP_ABC(OP_SEND, cursp(), c, 2));
      if (val) {
roco's avatar
roco committed
1975
        push();
mimaki's avatar
mimaki committed
1976 1977 1978 1979 1980 1981 1982
      }
    }
   break;

  case NODE_UNDEF:
    {
      int undef = new_msym(s, mrb_intern(s->mrb, "undef_method"));
1983 1984
      int num = 0;
      node *t = tree;
mimaki's avatar
mimaki committed
1985 1986 1987

      genop(s, MKOP_A(OP_TCLASS, cursp()));
      push();
1988 1989 1990 1991 1992 1993 1994 1995 1996
      while (t) {
        int symbol = new_msym(s, sym(t->car));
        genop(s, MKOP_ABx(OP_LOADSYM, cursp(), symbol));
        push();
        t = t->cdr;
        num++;
      }
      pop_n(num + 1);
      genop(s, MKOP_ABC(OP_SEND, cursp(), undef, num));
mimaki's avatar
mimaki committed
1997
      if (val) {
roco's avatar
roco committed
1998
        push();
mimaki's avatar
mimaki committed
1999 2000 2001 2002 2003 2004 2005 2006
      }
    }
    break;

  case NODE_CLASS:
    {
      int idx;

roco's avatar
roco committed
2007 2008 2009
      if (tree->car->car == (node*)0) {
        genop(s, MKOP_A(OP_LOADNIL, cursp()));
        push();
mimaki's avatar
mimaki committed
2010 2011
      }
      else if (tree->car->car == (node*)1) {
roco's avatar
roco committed
2012 2013
        genop(s, MKOP_A(OP_OCLASS, cursp()));
        push();
mimaki's avatar
mimaki committed
2014 2015
      }
      else {
roco's avatar
roco committed
2016
        codegen(s, tree->car->car, VAL);
mimaki's avatar
mimaki committed
2017 2018
      }
      if (tree->cdr->car) {
roco's avatar
roco committed
2019
        codegen(s, tree->cdr->car, VAL);
mimaki's avatar
mimaki committed
2020 2021
      }
      else {
roco's avatar
roco committed
2022 2023
        genop(s, MKOP_A(OP_LOADNIL, cursp()));
        push();
mimaki's avatar
mimaki committed
2024 2025
      }
      pop(); pop();
2026
      idx = new_msym(s, sym(tree->car->cdr));
mimaki's avatar
mimaki committed
2027 2028 2029 2030
      genop(s, MKOP_AB(OP_CLASS, cursp(), idx));
      idx = scope_body(s, tree->cdr->cdr->car);
      genop(s, MKOP_ABx(OP_EXEC, cursp(), idx));
      if (val) {
roco's avatar
roco committed
2031
        push();
mimaki's avatar
mimaki committed
2032 2033 2034 2035 2036 2037 2038 2039
      }
    }
    break;

  case NODE_MODULE:
    {
      int idx;

roco's avatar
roco committed
2040 2041 2042
      if (tree->car->car == (node*)0) {
        genop(s, MKOP_A(OP_LOADNIL, cursp()));
        push();
mimaki's avatar
mimaki committed
2043 2044
      }
      else if (tree->car->car == (node*)1) {
roco's avatar
roco committed
2045 2046
        genop(s, MKOP_A(OP_OCLASS, cursp()));
        push();
mimaki's avatar
mimaki committed
2047 2048
      }
      else {
roco's avatar
roco committed
2049
        codegen(s, tree->car->car, VAL);
mimaki's avatar
mimaki committed
2050 2051
      }
      pop();
2052
      idx = new_msym(s, sym(tree->car->cdr));
mimaki's avatar
mimaki committed
2053 2054 2055 2056
      genop(s, MKOP_AB(OP_MODULE, cursp(), idx));
      idx = scope_body(s, tree->cdr->car);
      genop(s, MKOP_ABx(OP_EXEC, cursp(), idx));
      if (val) {
roco's avatar
roco committed
2057
        push();
mimaki's avatar
mimaki committed
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
      }
    }
    break;

  case NODE_SCLASS:
    {
      int idx;

      codegen(s, tree->car, VAL);
      pop();
      genop(s, MKOP_AB(OP_SCLASS, cursp(), cursp()));
      idx = scope_body(s, tree->cdr->car);
      genop(s, MKOP_ABx(OP_EXEC, cursp(), idx));
      if (val) {
roco's avatar
roco committed
2072
        push();
mimaki's avatar
mimaki committed
2073 2074 2075 2076 2077 2078
      }
    }
    break;

  case NODE_DEF:
    {
2079
      int sym = new_msym(s, sym(tree->car));
mimaki's avatar
mimaki committed
2080 2081 2082 2083 2084 2085 2086 2087
      int idx = lambda_body(s, tree->cdr, 0);

      genop(s, MKOP_A(OP_TCLASS, cursp()));
      push();
      genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_METHOD));
      pop();
      genop(s, MKOP_AB(OP_METHOD, cursp(), sym));
      if (val) {
roco's avatar
roco committed
2088
        genop(s, MKOP_A(OP_LOADNIL, cursp()));
mimaki's avatar
mimaki committed
2089 2090 2091 2092 2093 2094 2095
      }
    }
    break;

  case NODE_SDEF:
    {
      node *recv = tree->car;
2096
      int sym = new_msym(s, sym(tree->cdr->car));
mimaki's avatar
mimaki committed
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
      int idx = lambda_body(s, tree->cdr->cdr, 0);

      codegen(s, recv, VAL);
      pop();
      genop(s, MKOP_AB(OP_SCLASS, cursp(), cursp()));
      push();
      genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_METHOD));
      pop();
      genop(s, MKOP_AB(OP_METHOD, cursp(), sym));
      if (val) {
roco's avatar
roco committed
2107
        genop(s, MKOP_A(OP_LOADNIL, cursp()));
mimaki's avatar
mimaki committed
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
      }
    }
    break;

  case NODE_POSTEXE:
    codegen(s, tree, NOVAL);
    break;

  default:
    break;
  }
}

static codegen_scope*
scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
{
2124
  static const codegen_scope codegen_scope_zero = { 0 };
mimaki's avatar
mimaki committed
2125
  mrb_pool *pool = mrb_pool_open(mrb);
2126
  codegen_scope *p = (codegen_scope *)mrb_pool_alloc(pool, sizeof(codegen_scope));
mimaki's avatar
mimaki committed
2127

2128
  if (!p) return 0;
2129
  *p = codegen_scope_zero;
mimaki's avatar
mimaki committed
2130 2131 2132 2133 2134
  p->mrb = mrb;
  p->mpool = pool;
  if (!prev) return p;
  p->prev = prev;
  p->ainfo = -1;
2135
  p->mscope = 0;
mimaki's avatar
mimaki committed
2136

2137 2138 2139
  p->irep = mrb_add_irep(mrb);
  p->idx = p->irep->idx;

mimaki's avatar
mimaki committed
2140
  p->icapa = 1024;
2141
  p->iseq = (mrb_code*)mrb_malloc(mrb, sizeof(mrb_code)*p->icapa);
mimaki's avatar
mimaki committed
2142 2143

  p->pcapa = 32;
2144 2145
  p->irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value)*p->pcapa);
  p->irep->plen = 0;
mimaki's avatar
mimaki committed
2146

2147 2148 2149
  p->scapa = 256;
  p->irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*256);
  p->irep->slen = 0;
mimaki's avatar
mimaki committed
2150 2151

  p->lv = lv;
2152
  p->sp += node_len(lv)+1;	/* add self */
mimaki's avatar
mimaki committed
2153
  p->nlocals = p->sp;
2154
  p->ai = mrb_gc_arena_save(mrb);
mimaki's avatar
mimaki committed
2155

2156
  p->filename = prev->filename;
2157 2158 2159
  if (p->filename) {
    p->lines = (short*)mrb_malloc(mrb, sizeof(short)*p->icapa);
  }
2160
  p->lineno = prev->lineno;
mimaki's avatar
mimaki committed
2161 2162 2163 2164
  return p;
}

static void
2165
scope_finish(codegen_scope *s)
mimaki's avatar
mimaki committed
2166 2167
{
  mrb_state *mrb = s->mrb;
2168
  mrb_irep *irep = s->irep;
Christian Mauceri's avatar
Christian Mauceri committed
2169
    
2170
  irep->flags = 0;
mimaki's avatar
mimaki committed
2171
  if (s->iseq) {
2172
    irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc);
mimaki's avatar
mimaki committed
2173
    irep->ilen = s->pc;
2174 2175 2176
    if (s->lines) {
      irep->lines = (short *)codegen_realloc(s, s->lines, sizeof(short)*s->pc);
    }
2177 2178 2179
    else {
      irep->lines = 0;
    }
mimaki's avatar
mimaki committed
2180
  }
2181 2182
  irep->pool = (mrb_value *)codegen_realloc(s, irep->pool, sizeof(mrb_value)*irep->plen);
  irep->syms = (mrb_sym *)codegen_realloc(s, irep->syms, sizeof(mrb_sym)*irep->slen);
2183 2184 2185
  if (s->filename) {
    irep->filename = s->filename;
  }
mimaki's avatar
mimaki committed
2186 2187 2188 2189

  irep->nlocals = s->nlocals;
  irep->nregs = s->nregs;

2190
  mrb_gc_arena_restore(mrb, s->ai);
mimaki's avatar
mimaki committed
2191 2192 2193 2194 2195 2196
  mrb_pool_close(s->mpool);
}

static struct loopinfo*
loop_push(codegen_scope *s, enum looptype t)
{
2197
  struct loopinfo *p = (struct loopinfo *)codegen_palloc(s, sizeof(struct loopinfo));
mimaki's avatar
mimaki committed
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228

  p->type = t;
  p->pc1 = p->pc2 = p->pc3 = 0;
  p->prev = s->loop;
  p->ensure_level = s->ensure_level;
  p->acc = cursp();
  s->loop = p;

  return p;
}

static void
loop_break(codegen_scope *s, node *tree)
{
  if (!s->loop) {
    codegen(s, tree, NOVAL);
    raise_error(s, "unexpected break");
  }
  else {
    struct loopinfo *loop;

    if (tree) {
      codegen(s, tree, VAL);
      pop();
    }

    loop = s->loop;
    while (loop->type == LOOP_BEGIN) {
      genop_peep(s, MKOP_A(OP_POPERR, 1), NOVAL);
      loop = loop->prev;
    }
2229 2230 2231
    while (loop->type == LOOP_RESCUE) {
      loop = loop->prev;
    }
mimaki's avatar
mimaki committed
2232 2233 2234 2235
    if (loop->type == LOOP_NORMAL) {
      int tmp;

      if (s->ensure_level > s->loop->ensure_level) {
roco's avatar
roco committed
2236
        genop_peep(s, MKOP_A(OP_EPOP, s->ensure_level - s->loop->ensure_level), NOVAL);
mimaki's avatar
mimaki committed
2237 2238
      }
      if (tree) {
roco's avatar
roco committed
2239
        genop_peep(s, MKOP_AB(OP_MOVE, loop->acc, cursp()), NOVAL);
mimaki's avatar
mimaki committed
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
      }
      tmp = new_label(s);
      genop(s, MKOP_sBx(OP_JMP, loop->pc3));
      loop->pc3 = tmp;
    }
    else {
      genop(s, MKOP_AB(OP_RETURN, cursp(), OP_R_BREAK));
    }
  }
}

static void
loop_pop(codegen_scope *s, int val)
{
  if (val) {
    genop(s, MKOP_A(OP_LOADNIL, cursp()));
  }
  dispatch_linked(s, s->loop->pc3);
  s->loop = s->loop->prev;
  if (val) push();
}

static void
codedump(mrb_state *mrb, int n)
{
2265
#ifdef ENABLE_STDIO
mimaki's avatar
mimaki committed
2266 2267 2268 2269 2270 2271
  mrb_irep *irep = mrb->irep[n];
  int i;
  mrb_code c;

  if (!irep) return;
  printf("irep %d nregs=%d nlocals=%d pools=%d syms=%d\n", n,
roco's avatar
roco committed
2272
         irep->nregs, irep->nlocals, irep->plen, irep->slen);
mimaki's avatar
mimaki committed
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
  for (i=0; i<irep->ilen; i++) {
    printf("%03d ", i);
    c = irep->iseq[i];
    switch (GET_OPCODE(c)) {
    case OP_NOP:
      printf("OP_NOP\n");
      break;
    case OP_MOVE:
      printf("OP_MOVE\tR%d\tR%d\n", GETARG_A(c), GETARG_B(c));
      break;
    case OP_LOADL:
      printf("OP_LOADL\tR%d\tL(%d)\n", GETARG_A(c), GETARG_Bx(c));
      break;
    case OP_LOADI:
      printf("OP_LOADI\tR%d\t%d\n", GETARG_A(c), GETARG_sBx(c));
      break;
    case OP_LOADSYM:
2290
      printf("OP_LOADSYM\tR%d\t:%s\n", GETARG_A(c),
roco's avatar
roco committed
2291
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
mimaki's avatar
mimaki committed
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
      break;
    case OP_LOADNIL:
      printf("OP_LOADNIL\tR%d\n", GETARG_A(c));
      break;
    case OP_LOADSELF:
      printf("OP_LOADSELF\tR%d\n", GETARG_A(c));
      break;
    case OP_LOADT:
      printf("OP_LOADT\tR%d\n", GETARG_A(c));
      break;
    case OP_LOADF:
      printf("OP_LOADF\tR%d\n", GETARG_A(c));
      break;
    case OP_GETGLOBAL:
2306
      printf("OP_GETGLOBAL\tR%d\t:%s\n", GETARG_A(c),
roco's avatar
roco committed
2307
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
mimaki's avatar
mimaki committed
2308 2309
      break;
    case OP_SETGLOBAL:
2310
      printf("OP_SETGLOBAL\t:%s\tR%d\n",
roco's avatar
roco committed
2311 2312
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
mimaki's avatar
mimaki committed
2313 2314
      break;
    case OP_GETCONST:
2315
      printf("OP_GETCONST\tR%d\t:%s\n", GETARG_A(c),
roco's avatar
roco committed
2316
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
mimaki's avatar
mimaki committed
2317 2318
      break;
    case OP_SETCONST:
2319
      printf("OP_SETCONST\t:%s\tR%d\n",
roco's avatar
roco committed
2320 2321
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
mimaki's avatar
mimaki committed
2322 2323 2324
      break;
    case OP_GETMCNST:
      printf("OP_GETMCNST\tR%d\tR%d::%s\n", GETARG_A(c), GETARG_A(c),
roco's avatar
roco committed
2325
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
mimaki's avatar
mimaki committed
2326 2327 2328
      break;
    case OP_SETMCNST:
      printf("OP_SETMCNST\tR%d::%s\tR%d\n", GETARG_A(c)+1,
roco's avatar
roco committed
2329 2330
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
mimaki's avatar
mimaki committed
2331 2332 2333
      break;
    case OP_GETIV:
      printf("OP_GETIV\tR%d\t%s\n", GETARG_A(c),
roco's avatar
roco committed
2334
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
mimaki's avatar
mimaki committed
2335 2336 2337
      break;
    case OP_SETIV:
      printf("OP_SETIV\t%s\tR%d\n",
roco's avatar
roco committed
2338 2339
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
mimaki's avatar
mimaki committed
2340 2341 2342
      break;
    case OP_GETUPVAR:
      printf("OP_GETUPVAR\tR%d\t%d\t%d\n",
roco's avatar
roco committed
2343
             GETARG_A(c), GETARG_B(c), GETARG_C(c));
mimaki's avatar
mimaki committed
2344 2345 2346
      break;
    case OP_SETUPVAR:
      printf("OP_SETUPVAR\tR%d\t%d\t%d\n",
roco's avatar
roco committed
2347
             GETARG_A(c), GETARG_B(c), GETARG_C(c));
mimaki's avatar
mimaki committed
2348 2349 2350
      break;
    case OP_GETCV:
      printf("OP_GETCV\tR%d\t%s\n", GETARG_A(c),
roco's avatar
roco committed
2351
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]));
mimaki's avatar
mimaki committed
2352 2353 2354
      break;
    case OP_SETCV:
      printf("OP_SETCV\t%s\tR%d\n",
roco's avatar
roco committed
2355 2356
             mrb_sym2name(mrb, irep->syms[GETARG_Bx(c)]),
             GETARG_A(c));
mimaki's avatar
mimaki committed
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367
      break;
    case OP_JMP:
      printf("OP_JMP\t\t%03d\n", i+GETARG_sBx(c));
      break;
    case OP_JMPIF:
      printf("OP_JMPIF\tR%d\t%03d\n", GETARG_A(c), i+GETARG_sBx(c));
      break;
    case OP_JMPNOT:
      printf("OP_JMPNOT\tR%d\t%03d\n", GETARG_A(c), i+GETARG_sBx(c));
      break;
    case OP_SEND:
2368
      printf("OP_SEND\tR%d\t:%s\t%d\n", GETARG_A(c),
roco's avatar
roco committed
2369 2370
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
mimaki's avatar
mimaki committed
2371
      break;
2372 2373 2374 2375 2376
    case OP_SENDB:
      printf("OP_SENDB\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
Yukihiro Matsumoto's avatar
Yukihiro Matsumoto committed
2377 2378 2379 2380 2381
    case OP_TAILCALL:
      printf("OP_TAILCALL\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
mimaki's avatar
mimaki committed
2382 2383
    case OP_SUPER:
      printf("OP_SUPER\tR%d\t%d\n", GETARG_A(c),
roco's avatar
roco committed
2384
             GETARG_C(c));
mimaki's avatar
mimaki committed
2385 2386 2387
      break;
    case OP_ARGARY:
      printf("OP_ARGARY\tR%d\t%d:%d:%d:%d\n", GETARG_A(c),
roco's avatar
roco committed
2388 2389 2390 2391
             (GETARG_Bx(c)>>10)&0x3f,
             (GETARG_Bx(c)>>9)&0x1,
             (GETARG_Bx(c)>>4)&0x1f,
             (GETARG_Bx(c)>>0)&0xf);
mimaki's avatar
mimaki committed
2392 2393 2394 2395
      break;

    case OP_ENTER:
      printf("OP_ENTER\t%d:%d:%d:%d:%d:%d:%d\n",
roco's avatar
roco committed
2396 2397 2398 2399 2400 2401 2402
             (GETARG_Ax(c)>>18)&0x1f,
             (GETARG_Ax(c)>>13)&0x1f,
             (GETARG_Ax(c)>>12)&0x1,
             (GETARG_Ax(c)>>7)&0x1f,
             (GETARG_Ax(c)>>2)&0x1f,
             (GETARG_Ax(c)>>1)&0x1,
             GETARG_Ax(c) & 0x1);
mimaki's avatar
mimaki committed
2403 2404 2405 2406 2407
      break;
    case OP_RETURN:
      printf("OP_RETURN\tR%d", GETARG_A(c));
      switch (GETARG_B(c)) {
      case OP_R_NORMAL:
roco's avatar
roco committed
2408
        printf("\n"); break;
mimaki's avatar
mimaki committed
2409
      case OP_R_RETURN:
roco's avatar
roco committed
2410
        printf("\treturn\n"); break;
mimaki's avatar
mimaki committed
2411
      case OP_R_BREAK:
roco's avatar
roco committed
2412
        printf("\tbreak\n"); break;
mimaki's avatar
mimaki committed
2413
      default:
roco's avatar
roco committed
2414 2415
        printf("\tbroken\n"); break;
        break;
mimaki's avatar
mimaki committed
2416 2417 2418 2419
      }
      break;
    case OP_BLKPUSH:
      printf("OP_BLKPUSH\tR%d\t%d:%d:%d:%d\n", GETARG_A(c),
roco's avatar
roco committed
2420 2421 2422 2423
             (GETARG_Bx(c)>>10)&0x3f,
             (GETARG_Bx(c)>>9)&0x1,
             (GETARG_Bx(c)>>4)&0x1f,
             (GETARG_Bx(c)>>0)&0xf);
mimaki's avatar
mimaki committed
2424 2425 2426
      break;

    case OP_LAMBDA:
2427
      printf("OP_LAMBDA\tR%d\tI(%+d)\t%d\n", GETARG_A(c), GETARG_b(c), GETARG_c(c));
mimaki's avatar
mimaki committed
2428 2429 2430 2431 2432
      break;
    case OP_RANGE:
      printf("OP_RANGE\tR%d\tR%d\t%d\n", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      break;
    case OP_METHOD:
2433
      printf("OP_METHOD\tR%d\t:%s\n", GETARG_A(c),
roco's avatar
roco committed
2434
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]));
mimaki's avatar
mimaki committed
2435 2436 2437
      break;

    case OP_ADD:
2438
      printf("OP_ADD\tR%d\t:%s\t%d\n", GETARG_A(c),
roco's avatar
roco committed
2439 2440
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
mimaki's avatar
mimaki committed
2441
      break;
2442 2443 2444 2445 2446
    case OP_ADDI:
      printf("OP_ADDI\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
mimaki's avatar
mimaki committed
2447
    case OP_SUB:
2448
      printf("OP_SUB\tR%d\t:%s\t%d\n", GETARG_A(c),
roco's avatar
roco committed
2449 2450
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
mimaki's avatar
mimaki committed
2451
      break;
2452 2453 2454 2455 2456
    case OP_SUBI:
      printf("OP_SUBI\tR%d\t:%s\t%d\n", GETARG_A(c),
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
2457
    case OP_MUL:
2458
      printf("OP_MUL\tR%d\t:%s\t%d\n", GETARG_A(c),
2459 2460 2461
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
2462
    case OP_DIV:
2463
      printf("OP_DIV\tR%d\t:%s\t%d\n", GETARG_A(c),
2464 2465 2466
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
mimaki's avatar
mimaki committed
2467
    case OP_LT:
2468
      printf("OP_LT\tR%d\t:%s\t%d\n", GETARG_A(c),
roco's avatar
roco committed
2469 2470
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
mimaki's avatar
mimaki committed
2471 2472
      break;
    case OP_LE:
2473
      printf("OP_LE\tR%d\t:%s\t%d\n", GETARG_A(c),
roco's avatar
roco committed
2474 2475
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
mimaki's avatar
mimaki committed
2476 2477
      break;
    case OP_GT:
2478
      printf("OP_GT\tR%d\t:%s\t%d\n", GETARG_A(c),
roco's avatar
roco committed
2479 2480
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
mimaki's avatar
mimaki committed
2481 2482
      break;
    case OP_GE:
2483
      printf("OP_GE\tR%d\t:%s\t%d\n", GETARG_A(c),
roco's avatar
roco committed
2484 2485
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
mimaki's avatar
mimaki committed
2486
      break;
2487
    case OP_EQ:
2488
      printf("OP_EQ\tR%d\t:%s\t%d\n", GETARG_A(c),
2489 2490 2491
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
             GETARG_C(c));
      break;
mimaki's avatar
mimaki committed
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512

    case OP_STOP:
      printf("OP_STOP\n");
      break;

    case OP_ARRAY:
      printf("OP_ARRAY\tR%d\tR%d\t%d\n", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      break;
    case OP_ARYCAT:
      printf("OP_ARYCAT\tR%d\tR%d\n", GETARG_A(c), GETARG_B(c));
      break;
    case OP_ARYPUSH:
      printf("OP_ARYPUSH\tR%d\tR%d\n", GETARG_A(c), GETARG_B(c));
      break;
    case OP_AREF:
      printf("OP_AREF\tR%d\tR%d\t%d\n", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      break;
    case OP_APOST:
      printf("OP_APOST\tR%d\t%d\t%d\n", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      break;
    case OP_STRING:
2513 2514 2515 2516 2517 2518
      {
	mrb_value s = irep->pool[GETARG_Bx(c)];
	
	s = mrb_str_dump(mrb, s);
	printf("OP_STRING\tR%d\t%s\n", GETARG_A(c), RSTRING_PTR(s));
      }
mimaki's avatar
mimaki committed
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
      break;
    case OP_STRCAT:
      printf("OP_STRCAT\tR%d\tR%d\n", GETARG_A(c), GETARG_B(c));
      break;
    case OP_HASH:
      printf("OP_HASH\tR%d\tR%d\t%d\n", GETARG_A(c), GETARG_B(c), GETARG_C(c));
      break;

    case OP_OCLASS:
      printf("OP_OCLASS\tR%d\n", GETARG_A(c));
      break;
    case OP_CLASS:
2531
      printf("OP_CLASS\tR%d\t:%s\n", GETARG_A(c),
roco's avatar
roco committed
2532
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]));
mimaki's avatar
mimaki committed
2533 2534
      break;
    case OP_MODULE:
2535
      printf("OP_MODULE\tR%d\t:%s\n", GETARG_A(c),
roco's avatar
roco committed
2536
             mrb_sym2name(mrb, irep->syms[GETARG_B(c)]));
mimaki's avatar
mimaki committed
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
      break;
    case OP_EXEC:
      printf("OP_EXEC\tR%d\tI(%d)\n", GETARG_A(c), n+GETARG_Bx(c));
      break;
    case OP_SCLASS:
      printf("OP_SCLASS\tR%d\tR%d\n", GETARG_A(c), GETARG_B(c));
      break;
    case OP_TCLASS:
      printf("OP_TCLASS\tR%d\n", GETARG_A(c));
      break;
    case OP_ERR:
2548
      printf("OP_ERR\tL(%d)\n", GETARG_Bx(c));
mimaki's avatar
mimaki committed
2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570
      break;
    case OP_EPUSH:
      printf("OP_EPUSH\t:I(%d)\n", n+GETARG_Bx(c));
      break;
    case OP_ONERR:
      printf("OP_ONERR\t%03d\n", i+GETARG_sBx(c));
      break;
    case OP_RESCUE:
      printf("OP_RESCUE\tR%d\n", GETARG_A(c));
      break;
    case OP_RAISE:
      printf("OP_RAISE\tR%d\n", GETARG_A(c));
      break;
    case OP_POPERR:
      printf("OP_POPERR\t%d\n", GETARG_A(c));
      break;
    case OP_EPOP:
      printf("OP_EPOP\t%d\n", GETARG_A(c));
      break;

    default:
      printf("OP_unknown %d\t%d\t%d\t%d\n", GET_OPCODE(c),
roco's avatar
roco committed
2571
             GETARG_A(c), GETARG_B(c), GETARG_C(c));
mimaki's avatar
mimaki committed
2572 2573 2574 2575
      break;
    }
  }
  printf("\n");
2576
#endif
mimaki's avatar
mimaki committed
2577 2578 2579 2580 2581
}

void
codedump_all(mrb_state *mrb, int start)
{
2582
  size_t i;
mimaki's avatar
mimaki committed
2583 2584 2585 2586 2587 2588 2589

  for (i=start; i<mrb->irep_len; i++) {
    codedump(mrb, i);
  }
}

static int
2590
codegen_start(mrb_state *mrb, parser_state *p)
mimaki's avatar
mimaki committed
2591 2592 2593 2594 2595 2596 2597
{
  codegen_scope *scope = scope_new(mrb, 0, 0);

  if (!scope) {
    return -1;
  }
  scope->mrb = mrb;
2598
  if (p->filename) {
2599
    scope->filename = p->filename;
2600
  }
mimaki's avatar
mimaki committed
2601 2602 2603 2604
  if (setjmp(scope->jmp) != 0) {
    return -1;
  }
  // prepare irep
2605
  codegen(scope, p->tree, NOVAL);
2606
  mrb_pool_close(scope->mpool);
mimaki's avatar
mimaki committed
2607 2608 2609 2610
  return 0;
}

int
2611
mrb_generate_code(mrb_state *mrb, parser_state *p)
mimaki's avatar
mimaki committed
2612 2613 2614 2615
{
  int start = mrb->irep_len;
  int n;

2616
  n = codegen_start(mrb, p);
mimaki's avatar
mimaki committed
2617 2618 2619 2620
  if (n < 0) return n;

  return start;
}