Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
6 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/codegen.c

    r331 r439  
    99#include <stdlib.h>
    1010#include <string.h>
     11#include <math.h>
    1112#include <mruby.h>
    1213#include <mruby/compile.h>
     
    2425#endif
    2526
     27#define MAXARG_S (1<<16)
     28
    2629typedef mrb_ast_node node;
    2730typedef struct mrb_parser_state parser_state;
     
    3740struct loopinfo {
    3841  enum looptype type;
    39   int pc1, pc2, pc3, acc;
     42  int pc0, pc1, pc2, pc3, acc;
    4043  int ensure_level;
    4144  struct loopinfo *prev;
     
    5154  node *lv;
    5255
    53   int sp;
    54   int pc;
    55   int lastlabel;
     56  uint16_t sp;
     57  uint16_t pc;
     58  uint16_t lastpc;
     59  uint16_t lastlabel;
    5660  int ainfo:15;
    5761  mrb_bool mscope:1;
     
    5963  struct loopinfo *loop;
    6064  int ensure_level;
    61   char const *filename;
     65  mrb_sym filename_sym;
    6266  uint16_t lineno;
    6367
    6468  mrb_code *iseq;
    6569  uint16_t *lines;
    66   int icapa;
     70  uint32_t icapa;
    6771
    6872  mrb_irep *irep;
    69   size_t pcapa;
    70   size_t scapa;
    71   size_t rcapa;
     73  uint32_t pcapa, scapa, rcapa;
    7274
    7375  uint16_t nlocals;
     
    101103    codegen_scope *tmp = s->prev;
    102104    mrb_free(s->mrb, s->iseq);
     105    mrb_free(s->mrb, s->lines);
    103106    mrb_pool_close(s->mpool);
    104107    s = tmp;
    105108  }
    106109#ifndef MRB_DISABLE_STDIO
    107   if (s->filename && s->lineno) {
    108     fprintf(stderr, "codegen error:%s:%d: %s\n", s->filename, s->lineno, message);
     110  if (s->filename_sym && s->lineno) {
     111    const char *filename = mrb_sym_name_len(s->mrb, s->filename_sym, NULL);
     112    fprintf(stderr, "codegen error:%s:%d: %s\n", filename, s->lineno, message);
    109113  }
    110114  else {
     
    125129
    126130static void*
    127 codegen_malloc(codegen_scope *s, size_t len)
    128 {
    129   void *p = mrb_malloc_simple(s->mrb, len);
    130 
    131   if (!p) codegen_error(s, "mrb_malloc");
    132   return p;
    133 }
    134 
    135 static void*
    136131codegen_realloc(codegen_scope *s, void *p, size_t len)
    137132{
     
    145140new_label(codegen_scope *s)
    146141{
    147   s->lastlabel = s->pc;
    148   return s->pc;
    149 }
    150 
    151 static inline int
    152 genop(codegen_scope *s, mrb_code i)
    153 {
    154   if (s->pc == s->icapa) {
     142  return s->lastlabel = s->pc;
     143}
     144
     145static void
     146emit_B(codegen_scope *s, uint32_t pc, uint8_t i)
     147{
     148  if (pc >= MAXARG_S || s->icapa >= MAXARG_S) {
     149    codegen_error(s, "too big code block");
     150  }
     151  if (pc >= s->icapa) {
    155152    s->icapa *= 2;
     153    if (s->icapa > MAXARG_S) {
     154      s->icapa = MAXARG_S;
     155    }
    156156    s->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->icapa);
    157157    if (s->lines) {
    158       s->lines = (uint16_t*)codegen_realloc(s, s->lines, sizeof(short)*s->icapa);
    159       s->irep->lines = s->lines;
    160     }
    161   }
    162   s->iseq[s->pc] = i;
     158      s->lines = (uint16_t*)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->icapa);
     159    }
     160  }
    163161  if (s->lines) {
    164     s->lines[s->pc] = s->lineno;
    165   }
    166   return s->pc++;
     162    if (s->lineno > 0 || pc == 0)
     163      s->lines[pc] = s->lineno;
     164    else
     165      s->lines[pc] = s->lines[pc-1];
     166  }
     167  s->iseq[pc] = i;
     168}
     169
     170static void
     171emit_S(codegen_scope *s, int pc, uint16_t i)
     172{
     173  uint8_t hi = i>>8;
     174  uint8_t lo = i&0xff;
     175
     176  emit_B(s, pc,   hi);
     177  emit_B(s, pc+1, lo);
     178}
     179
     180static void
     181gen_B(codegen_scope *s, uint8_t i)
     182{
     183  emit_B(s, s->pc, i);
     184  s->pc++;
     185}
     186
     187static void
     188gen_S(codegen_scope *s, uint16_t i)
     189{
     190  emit_S(s, s->pc, i);
     191  s->pc += 2;
     192}
     193
     194static void
     195genop_0(codegen_scope *s, mrb_code i)
     196{
     197  s->lastpc = s->pc;
     198  gen_B(s, i);
     199}
     200
     201static void
     202genop_1(codegen_scope *s, mrb_code i, uint16_t a)
     203{
     204  s->lastpc = s->pc;
     205  if (a > 0xff) {
     206    gen_B(s, OP_EXT1);
     207    gen_B(s, i);
     208    gen_S(s, a);
     209  }
     210  else {
     211    gen_B(s, i);
     212    gen_B(s, (uint8_t)a);
     213  }
     214}
     215
     216static void
     217genop_2(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b)
     218{
     219  s->lastpc = s->pc;
     220  if (a > 0xff && b > 0xff) {
     221    gen_B(s, OP_EXT3);
     222    gen_B(s, i);
     223    gen_S(s, a);
     224    gen_S(s, b);
     225  }
     226  else if (b > 0xff) {
     227    gen_B(s, OP_EXT2);
     228    gen_B(s, i);
     229    gen_B(s, (uint8_t)a);
     230    gen_S(s, b);
     231  }
     232  else if (a > 0xff) {
     233    gen_B(s, OP_EXT1);
     234    gen_B(s, i);
     235    gen_S(s, a);
     236    gen_B(s, (uint8_t)b);
     237  }
     238  else {
     239    gen_B(s, i);
     240    gen_B(s, (uint8_t)a);
     241    gen_B(s, (uint8_t)b);
     242  }
     243}
     244
     245static void
     246genop_3(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b, uint8_t c)
     247{
     248  genop_2(s, i, a, b);
     249  gen_B(s, c);
     250}
     251
     252static void
     253genop_2S(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b)
     254{
     255  genop_1(s, i, a);
     256  gen_S(s, b);
     257}
     258
     259static void
     260genop_W(codegen_scope *s, mrb_code i, uint32_t a)
     261{
     262  uint8_t a1 = (a>>16) & 0xff;
     263  uint8_t a2 = (a>>8) & 0xff;
     264  uint8_t a3 = a & 0xff;
     265
     266  s->lastpc = s->pc;
     267  gen_B(s, i);
     268  gen_B(s, a1);
     269  gen_B(s, a2);
     270  gen_B(s, a3);
    167271}
    168272
     
    178282}
    179283
    180 static int
    181 genop_peep(codegen_scope *s, mrb_code i, int val)
    182 {
    183   /* peephole optimization */
    184   if (!no_optimize(s) && s->lastlabel != s->pc && s->pc > 0) {
    185     mrb_code i0 = s->iseq[s->pc-1];
    186     int c1 = GET_OPCODE(i);
    187     int c0 = GET_OPCODE(i0);
    188 
    189     switch (c1) {
     284static
     285mrb_bool
     286on_eval(codegen_scope *s)
     287{
     288  if (s && s->parser && s->parser->on_eval)
     289    return TRUE;
     290  return FALSE;
     291}
     292
     293struct mrb_insn_data
     294mrb_decode_insn(const mrb_code *pc)
     295{
     296  struct mrb_insn_data data = { 0 };
     297  mrb_code insn = READ_B();
     298  uint16_t a = 0;
     299  uint16_t b = 0;
     300  uint8_t  c = 0;
     301
     302  switch (insn) {
     303#define FETCH_Z() /* empty */
     304#define OPCODE(i,x) case OP_ ## i: FETCH_ ## x (); break;
     305#include "mruby/ops.h"
     306#undef OPCODE
     307  }
     308  switch (insn) {
     309  case OP_EXT1:
     310    insn = READ_B();
     311    switch (insn) {
     312#define OPCODE(i,x) case OP_ ## i: FETCH_ ## x ## _1 (); break;
     313#include "mruby/ops.h"
     314#undef OPCODE
     315    }
     316    break;
     317  case OP_EXT2:
     318    insn = READ_B();
     319    switch (insn) {
     320#define OPCODE(i,x) case OP_ ## i: FETCH_ ## x ## _2 (); break;
     321#include "mruby/ops.h"
     322#undef OPCODE
     323    }
     324    break;
     325  case OP_EXT3:
     326    insn = READ_B();
     327    switch (insn) {
     328#define OPCODE(i,x) case OP_ ## i: FETCH_ ## x ## _3 (); break;
     329#include "mruby/ops.h"
     330#undef OPCODE
     331    }
     332    break;
     333  default:
     334    break;
     335  }
     336  data.insn = insn;
     337  data.a = a;
     338  data.b = b;
     339  data.c = c;
     340  return data;
     341}
     342
     343static struct mrb_insn_data
     344mrb_last_insn(codegen_scope *s)
     345{
     346  if (s->pc == s->lastpc) {
     347    struct mrb_insn_data data;
     348
     349    data.insn = OP_NOP;
     350    return data;
     351  }
     352  return mrb_decode_insn(&s->iseq[s->lastpc]);
     353}
     354
     355static mrb_bool
     356no_peephole(codegen_scope *s)
     357{
     358  return no_optimize(s) || s->lastlabel == s->pc || s->pc == 0 || s->pc == s->lastpc;
     359}
     360
     361static uint16_t
     362genjmp(codegen_scope *s, mrb_code i, uint16_t pc)
     363{
     364  uint16_t pos;
     365
     366  s->lastpc = s->pc;
     367  gen_B(s, i);
     368  pos = s->pc;
     369  gen_S(s, pc);
     370  return pos;
     371}
     372
     373static uint16_t
     374genjmp2(codegen_scope *s, mrb_code i, uint16_t a, int pc, int val)
     375{
     376  uint16_t pos;
     377
     378  if (!no_peephole(s) && !val) {
     379    struct mrb_insn_data data = mrb_last_insn(s);
     380
     381    if (data.insn == OP_MOVE && data.a == a) {
     382      s->pc = s->lastpc;
     383      a = data.b;
     384    }
     385  }
     386
     387  s->lastpc = s->pc;
     388  if (a > 0xff) {
     389    gen_B(s, OP_EXT1);
     390    gen_B(s, i);
     391    gen_S(s, a);
     392    pos = s->pc;
     393    gen_S(s, pc);
     394  }
     395  else {
     396    gen_B(s, i);
     397    gen_B(s, (uint8_t)a);
     398    pos = s->pc;
     399    gen_S(s, pc);
     400  }
     401  return pos;
     402}
     403
     404static void
     405gen_move(codegen_scope *s, uint16_t dst, uint16_t src, int nopeep)
     406{
     407  if (no_peephole(s)) {
     408  normal:
     409    genop_2(s, OP_MOVE, dst, src);
     410    if (on_eval(s)) {
     411      genop_0(s, OP_NOP);
     412    }
     413    return;
     414  }
     415  else {
     416    struct mrb_insn_data data = mrb_last_insn(s);
     417
     418    switch (data.insn) {
    190419    case OP_MOVE:
    191       if (GETARG_A(i) == GETARG_B(i)) {
    192         /* skip useless OP_MOVE */
    193         return 0;
    194       }
    195       if (val) break;
    196       switch (c0) {
    197       case OP_MOVE:
    198         if (GETARG_A(i) == GETARG_A(i0)) {
    199           /* skip overriden OP_MOVE */
    200           s->pc--;
    201           s->iseq[s->pc] = i;
    202         }
    203         if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i) == GETARG_B(i0)) {
    204           /* skip swapping OP_MOVE */
    205           return 0;
    206         }
    207         if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
    208           s->pc--;
    209           return genop_peep(s, MKOP_AB(OP_MOVE, GETARG_A(i), GETARG_B(i0)), val);
    210         }
    211         break;
    212       case OP_LOADI:
    213         if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
    214           s->iseq[s->pc-1] = MKOP_AsBx(OP_LOADI, GETARG_A(i), GETARG_sBx(i0));
    215           return 0;
    216         }
    217         break;
    218       case OP_ARRAY:
    219       case OP_HASH:
    220       case OP_RANGE:
    221       case OP_AREF:
    222       case OP_GETUPVAR:
    223         if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
    224           s->iseq[s->pc-1] = MKOP_ABC(c0, GETARG_A(i), GETARG_B(i0), GETARG_C(i0));
    225           return 0;
    226         }
    227         break;
    228       case OP_LOADSYM:
    229       case OP_GETGLOBAL:
    230       case OP_GETIV:
    231       case OP_GETCV:
    232       case OP_GETCONST:
    233       case OP_GETSPECIAL:
    234       case OP_LOADL:
    235       case OP_STRING:
    236         if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
    237           s->iseq[s->pc-1] = MKOP_ABx(c0, GETARG_A(i), GETARG_Bx(i0));
    238           return 0;
    239         }
    240         break;
    241       case OP_SCLASS:
    242         if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
    243           s->iseq[s->pc-1] = MKOP_AB(c0, GETARG_A(i), GETARG_B(i0));
    244           return 0;
    245         }
    246         break;
    247       case OP_LOADNIL:
    248       case OP_LOADSELF:
    249       case OP_LOADT:
    250       case OP_LOADF:
    251       case OP_OCLASS:
    252         if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) {
    253           s->iseq[s->pc-1] = MKOP_A(c0, GETARG_A(i));
    254           return 0;
    255         }
    256         break;
    257       default:
    258         break;
    259       }
     420      if (dst == src) return;             /* remove useless MOVE */
     421      if (data.b == dst && data.a == src) /* skip swapping MOVE */
     422        return;
     423      goto normal;
     424    case OP_LOADNIL: case OP_LOADSELF: case OP_LOADT: case OP_LOADF:
     425    case OP_LOADI__1:
     426    case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3:
     427    case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7:
     428      if (nopeep || data.a != src || data.a < s->nlocals) goto normal;
     429      s->pc = s->lastpc;
     430      genop_1(s, data.insn, dst);
    260431      break;
    261     case OP_SETIV:
    262     case OP_SETCV:
    263     case OP_SETCONST:
    264     case OP_SETMCNST:
    265     case OP_SETGLOBAL:
    266       if (val) break;
    267       if (c0 == OP_MOVE) {
    268         if (GETARG_A(i) == GETARG_A(i0)) {
    269           s->iseq[s->pc-1] = MKOP_ABx(c1, GETARG_B(i0), GETARG_Bx(i));
    270           return 0;
    271         }
    272       }
    273       break;
    274     case OP_SETUPVAR:
    275       if (val) break;
    276       if (c0 == OP_MOVE) {
    277         if (GETARG_A(i) == GETARG_A(i0)) {
    278           s->iseq[s->pc-1] = MKOP_ABC(c1, GETARG_B(i0), GETARG_B(i), GETARG_C(i));
    279           return 0;
    280         }
    281       }
    282       break;
    283     case OP_EPOP:
    284       if (c0 == OP_EPOP) {
    285         s->iseq[s->pc-1] = MKOP_A(OP_EPOP, GETARG_A(i0)+GETARG_A(i));
    286         return 0;
    287       }
    288       break;
    289     case OP_POPERR:
    290       if (c0 == OP_POPERR) {
    291         s->iseq[s->pc-1] = MKOP_A(OP_POPERR, GETARG_A(i0)+GETARG_A(i));
    292         return 0;
    293       }
    294       break;
    295     case OP_RETURN:
    296       switch (c0) {
    297       case OP_RETURN:
    298         return 0;
    299       case OP_MOVE:
    300         if (GETARG_A(i0) >= s->nlocals) {
    301           s->iseq[s->pc-1] = MKOP_AB(OP_RETURN, GETARG_B(i0), OP_R_NORMAL);
    302           return 0;
    303         }
    304         break;
    305       case OP_SETIV:
    306       case OP_SETCV:
    307       case OP_SETCONST:
    308       case OP_SETMCNST:
    309       case OP_SETUPVAR:
    310       case OP_SETGLOBAL:
    311         s->pc--;
    312         genop_peep(s, i0, NOVAL);
    313         i0 = s->iseq[s->pc-1];
    314         return genop(s, MKOP_AB(OP_RETURN, GETARG_A(i0), OP_R_NORMAL));
    315 #if 0
    316       case OP_SEND:
    317         if (GETARG_B(i) == OP_R_NORMAL && GETARG_A(i) == GETARG_A(i0)) {
    318           s->iseq[s->pc-1] = MKOP_ABC(OP_TAILCALL, GETARG_A(i0), GETARG_B(i0), GETARG_C(i0));
    319           return;
    320         }
    321         break;
    322 #endif
    323       default:
    324         break;
    325       }
    326       break;
    327     case OP_ADD:
    328     case OP_SUB:
    329       if (c0 == OP_LOADI) {
    330         int c = GETARG_sBx(i0);
    331 
    332         if (c1 == OP_SUB) c = -c;
    333         if (c > 127 || c < -127) break;
    334         if (0 <= c)
    335           s->iseq[s->pc-1] = MKOP_ABC(OP_ADDI, GETARG_A(i), GETARG_B(i), c);
    336         else
    337           s->iseq[s->pc-1] = MKOP_ABC(OP_SUBI, GETARG_A(i), GETARG_B(i), -c);
    338         return 0;
    339       }
    340     case OP_STRCAT:
    341       if (c0 == OP_STRING) {
    342         mrb_value v = s->irep->pool[GETARG_Bx(i0)];
    343 
    344         if (mrb_string_p(v) && RSTRING_LEN(v) == 0) {
    345           s->pc--;
    346           return 0;
    347         }
    348       }
    349       if (c0 == OP_LOADNIL) {
    350         if (GETARG_B(i) == GETARG_A(i0)) {
    351           s->pc--;
    352           return 0;
    353         }
    354       }
    355       break;
    356     case OP_JMPIF:
    357     case OP_JMPNOT:
    358       if (c0 == OP_MOVE && GETARG_A(i) == GETARG_A(i0)) {
    359         s->iseq[s->pc-1] = MKOP_AsBx(c1, GETARG_B(i0), GETARG_sBx(i));
    360         return s->pc-1;
    361       }
     432    case OP_LOADI: case OP_LOADINEG: case OP_LOADL: case OP_LOADSYM:
     433    case OP_GETGV: case OP_GETSV: case OP_GETIV: case OP_GETCV:
     434    case OP_GETCONST: case OP_STRING:
     435    case OP_LAMBDA: case OP_BLOCK: case OP_METHOD: case OP_BLKPUSH:
     436      if (nopeep || data.a != src || data.a < s->nlocals) goto normal;
     437      s->pc = s->lastpc;
     438      genop_2(s, data.insn, dst, data.b);
    362439      break;
    363440    default:
     441      goto normal;
     442    }
     443  }
     444}
     445
     446static void
     447gen_return(codegen_scope *s, uint8_t op, uint16_t src)
     448{
     449  if (no_peephole(s)) {
     450    genop_1(s, op, src);
     451  }
     452  else {
     453    struct mrb_insn_data data = mrb_last_insn(s);
     454
     455    if (data.insn == OP_MOVE && src == data.a) {
     456      s->pc = s->lastpc;
     457      genop_1(s, op, data.b);
     458    }
     459    else if (data.insn != OP_RETURN) {
     460      genop_1(s, op, src);
     461    }
     462  }
     463}
     464
     465static void
     466gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst)
     467{
     468  if (no_peephole(s)) {
     469  normal:
     470    genop_1(s, op, dst);
     471    return;
     472  }
     473  else {
     474    struct mrb_insn_data data = mrb_last_insn(s);
     475
     476    switch (data.insn) {
     477    case OP_LOADI__1:
     478      if (op == OP_ADD) op = OP_SUB;
     479      else op = OP_ADD;
     480      data.b = 1;
     481      goto replace;
     482    case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3:
     483    case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7:
     484      data.b = data.insn - OP_LOADI_0;
     485      /* fall through */
     486    case OP_LOADI:
     487    replace:
     488      if (data.b >= 128) goto normal;
     489      s->pc = s->lastpc;
     490      if (op == OP_ADD) {
     491        genop_2(s, OP_ADDI, dst, (uint8_t)data.b);
     492      }
     493      else {
     494        genop_2(s, OP_SUBI, dst, (uint8_t)data.b);
     495      }
    364496      break;
    365     }
    366   }
    367   return genop(s, i);
     497    default:
     498      goto normal;
     499    }
     500  }
     501}
     502
     503static int
     504dispatch(codegen_scope *s, uint16_t pos0)
     505{
     506  uint16_t newpos;
     507
     508  s->lastlabel = s->pc;
     509  newpos = PEEK_S(s->iseq+pos0);
     510  emit_S(s, pos0, s->pc);
     511  return newpos;
    368512}
    369513
    370514static void
    371 scope_error(codegen_scope *s)
    372 {
    373   exit(EXIT_FAILURE);
    374 }
    375 
    376 static inline void
    377 dispatch(codegen_scope *s, int pc)
    378 {
    379   int diff = s->pc - pc;
    380   mrb_code i = s->iseq[pc];
    381   int c = GET_OPCODE(i);
    382 
    383   s->lastlabel = s->pc;
    384   switch (c) {
    385   case OP_JMP:
    386   case OP_JMPIF:
    387   case OP_JMPNOT:
    388   case OP_ONERR:
    389     break;
    390   default:
    391 #ifndef MRB_DISABLE_STDIO
    392     fprintf(stderr, "bug: dispatch on non JMP op\n");
    393 #endif
    394     scope_error(s);
    395     break;
    396   }
    397   if (diff > MAXARG_sBx) {
    398     codegen_error(s, "too distant jump address");
    399   }
    400   s->iseq[pc] = MKOP_AsBx(c, GETARG_A(i), diff);
    401 }
    402 
    403 static void
    404 dispatch_linked(codegen_scope *s, int pc)
    405 {
    406   mrb_code i;
    407   int pos;
    408 
    409   if (!pc) return;
     515dispatch_linked(codegen_scope *s, uint16_t pos)
     516{
     517  if (pos==0) return;
    410518  for (;;) {
    411     i = s->iseq[pc];
    412     pos = GETARG_sBx(i);
    413     dispatch(s, pc);
    414     if (!pos) break;
    415     pc = pos;
     519    pos = dispatch(s, pos);
     520    if (pos==0) break;
    416521  }
    417522}
     
    419524#define nregs_update do {if (s->sp > s->nregs) s->nregs = s->sp;} while (0)
    420525static void
    421 push_(codegen_scope *s)
    422 {
    423   if (s->sp > 511) {
    424     codegen_error(s, "too complex expression");
    425   }
    426   s->sp++;
    427   nregs_update;
    428 }
    429 
    430 static void
    431 push_n_(codegen_scope *s, size_t n)
    432 {
    433   if (s->sp+n > 511) {
     526push_n_(codegen_scope *s, int n)
     527{
     528  if (s->sp+n >= 0xffff) {
    434529    codegen_error(s, "too complex expression");
    435530  }
     
    438533}
    439534
    440 #define push() push_(s)
     535static void
     536pop_n_(codegen_scope *s, int n)
     537{
     538  if ((int)s->sp-n < 0) {
     539    codegen_error(s, "stack pointer underflow");
     540  }
     541  s->sp-=n;
     542}
     543
     544#define push() push_n_(s,1)
    441545#define push_n(n) push_n_(s,n)
    442 #define pop_(s) ((s)->sp--)
    443 #define pop() pop_(s)
    444 #define pop_n(n) (s->sp-=(n))
     546#define pop() pop_n_(s,1)
     547#define pop_n(n) pop_n_(s,n)
    445548#define cursp() (s->sp)
    446549
     
    448551new_lit(codegen_scope *s, mrb_value val)
    449552{
    450   size_t i;
     553  int i;
    451554  mrb_value *pv;
    452555
     
    457560      pv = &s->irep->pool[i];
    458561
    459       if (mrb_type(*pv) != MRB_TT_STRING) continue;
     562      if (!mrb_string_p(*pv)) continue;
    460563      if ((len = RSTRING_LEN(*pv)) != RSTRING_LEN(val)) continue;
    461564      if (memcmp(RSTRING_PTR(*pv), RSTRING_PTR(val), len) == 0)
     
    463566    }
    464567    break;
     568#ifndef MRB_WITHOUT_FLOAT
    465569  case MRB_TT_FLOAT:
    466570    for (i=0; i<s->irep->plen; i++) {
     571      mrb_float f1, f2;
    467572      pv = &s->irep->pool[i];
    468       if (mrb_type(*pv) != MRB_TT_FLOAT) continue;
    469       if (mrb_float(*pv) == mrb_float(val)) return i;
    470     }
    471     break;
     573      if (!mrb_float_p(*pv)) continue;
     574      f1 = mrb_float(*pv);
     575      f2 = mrb_float(val);
     576      if (f1 == f2 && !signbit(f1) == !signbit(f2)) return i;
     577    }
     578    break;
     579#endif
    472580  case MRB_TT_FIXNUM:
    473581    for (i=0; i<s->irep->plen; i++) {
     
    492600  switch (mrb_type(val)) {
    493601  case MRB_TT_STRING:
    494     *pv = mrb_str_pool(s->mrb, val);
    495     break;
    496 
     602    *pv = mrb_str_pool(s->mrb, RSTRING_PTR(val), RSTRING_LEN(val), RSTR_NOFREE_P(RSTRING(val)));
     603    break;
     604
     605#ifndef MRB_WITHOUT_FLOAT
    497606  case MRB_TT_FLOAT:
    498607#ifdef MRB_WORD_BOXING
     
    500609    break;
    501610#endif
     611#endif
    502612  case MRB_TT_FIXNUM:
    503613    *pv = val;
     
    511621}
    512622
    513 /* method symbols should be fit in 9 bits */
    514 #define MAXMSYMLEN 512
    515623/* maximum symbol numbers */
    516 #define MAXSYMLEN 65536
     624#define MAXSYMLEN 0x10000
    517625
    518626static int
    519 new_msym(codegen_scope *s, mrb_sym sym)
    520 {
    521   size_t i, len;
     627new_sym(codegen_scope *s, mrb_sym sym)
     628{
     629  int i, len;
    522630
    523631  mrb_assert(s->irep);
    524632
    525633  len = s->irep->slen;
    526   if (len > MAXMSYMLEN) len = MAXMSYMLEN;
    527634  for (i=0; i<len; i++) {
    528635    if (s->irep->syms[i] == sym) return i;
    529     if (s->irep->syms[i] == 0) break;
    530   }
    531   if (i == MAXMSYMLEN) {
    532     codegen_error(s, "too many symbols (max " MRB_STRINGIZE(MAXMSYMLEN) ")");
    533   }
    534   s->irep->syms[i] = sym;
    535   if (i == s->irep->slen) s->irep->slen++;
    536   return i;
    537 }
    538 
    539 static int
    540 new_sym(codegen_scope *s, mrb_sym sym)
    541 {
    542   size_t i;
    543 
    544   for (i=0; i<s->irep->slen; i++) {
    545     if (s->irep->syms[i] == sym) return i;
    546   }
    547   if (s->irep->slen == MAXSYMLEN) {
    548     codegen_error(s, "too many symbols (max " MRB_STRINGIZE(MAXSYMLEN) ")");
    549   }
    550 
    551   if (s->irep->slen > MAXMSYMLEN/2 && s->scapa == MAXMSYMLEN) {
    552     s->scapa = MAXSYMLEN;
    553     s->irep->syms = (mrb_sym *)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*MAXSYMLEN);
    554     for (i = s->irep->slen; i < MAXMSYMLEN; i++) {
    555       static const mrb_sym mrb_sym_zero = { 0 };
    556       s->irep->syms[i] = mrb_sym_zero;
    557     }
    558     s->irep->slen = MAXMSYMLEN;
     636  }
     637  if (s->irep->slen >= s->scapa) {
     638    s->scapa *= 2;
     639    s->irep->syms = (mrb_sym*)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*s->scapa);
    559640  }
    560641  s->irep->syms[s->irep->slen] = sym;
     
    574655}
    575656
    576 #define sym(x) ((mrb_sym)(intptr_t)(x))
    577 #define lv_name(lv) sym((lv)->car)
     657#define nint(x) ((int)(intptr_t)(x))
     658#define nchar(x) ((char)(intptr_t)(x))
     659#define nsym(x) ((mrb_sym)(intptr_t)(x))
     660
     661#define lv_name(lv) nsym((lv)->car)
     662
    578663static int
    579664lv_idx(codegen_scope *s, mrb_sym id)
     
    597682  struct loopinfo *lp;
    598683  node *n2;
    599   mrb_code c;
    600684
    601685  /* generate receiver */
     
    603687  /* generate loop-block */
    604688  s = scope_new(s->mrb, s, NULL);
    605   if (s == NULL) {
    606     raise_error(prev, "unexpected scope");
    607   }
    608689
    609690  push();                       /* push for a block parameter */
     
    611692  /* generate loop variable */
    612693  n2 = tree->car;
    613   genop(s, MKOP_Ax(OP_ENTER, 0x40000));
     694  genop_W(s, OP_ENTER, 0x40000);
    614695  if (n2->car && !n2->car->cdr && !n2->cdr) {
    615696    gen_assignment(s, n2->car->car, 1, NOVAL);
     
    625706  codegen(s, tree->cdr->cdr->car, VAL);
    626707  pop();
    627   if (s->pc > 0) {
    628     c = s->iseq[s->pc-1];
    629     if (GET_OPCODE(c) != OP_RETURN || GETARG_B(c) != OP_R_NORMAL || s->pc == s->lastlabel)
    630       genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
    631   }
     708  gen_return(s, OP_RETURN, cursp());
    632709  loop_pop(s, NOVAL);
    633710  scope_finish(s);
    634711  s = prev;
    635   genop(s, MKOP_Abc(OP_LAMBDA, cursp(), s->irep->rlen-1, OP_L_BLOCK));
     712  genop_2(s, OP_BLOCK, cursp(), s->irep->rlen-1);
     713  push();pop(); /* space for a block */
    636714  pop();
    637   idx = new_msym(s, mrb_intern_lit(s->mrb, "each"));
    638   genop(s, MKOP_ABC(OP_SENDB, cursp(), idx, 0));
     715  idx = new_sym(s, mrb_intern_lit(s->mrb, "each"));
     716  genop_3(s, OP_SENDB, cursp(), idx, 0);
    639717}
    640718
     
    642720lambda_body(codegen_scope *s, node *tree, int blk)
    643721{
    644   mrb_code c;
    645722  codegen_scope *parent = s;
    646723  s = scope_new(s->mrb, s, tree->car);
    647   if (s == NULL) {
    648     raise_error(parent, "unexpected scope");
    649   }
    650724
    651725  s->mscope = !blk;
     
    653727  if (blk) {
    654728    struct loopinfo *lp = loop_push(s, LOOP_BLOCK);
    655     lp->pc1 = new_label(s);
     729    lp->pc0 = new_label(s);
    656730  }
    657731  tree = tree->cdr;
    658   if (tree->car) {
     732  if (tree->car == NULL) {
     733    genop_W(s, OP_ENTER, 0);
     734  }
     735  else {
    659736    mrb_aspec a;
    660737    int ma, oa, ra, pa, ka, kd, ba;
    661738    int pos, i;
    662     node *n, *opt;
    663 
     739    node *opt;
     740    node *margs, *pargs;
     741    node *tail;
     742
     743    /* mandatory arguments */
    664744    ma = node_len(tree->car->car);
    665     n = tree->car->car;
    666     while (n) {
    667       n = n->cdr;
    668     }
     745    margs = tree->car->car;
     746    tail = tree->car->cdr->cdr->cdr->cdr;
     747
     748    /* optional arguments */
    669749    oa = node_len(tree->car->cdr->car);
     750    /* rest argument? */
    670751    ra = tree->car->cdr->cdr->car ? 1 : 0;
     752    /* mandatory arugments after rest argument */
    671753    pa = node_len(tree->car->cdr->cdr->cdr->car);
    672     ka = kd = 0;
    673     ba = tree->car->cdr->cdr->cdr->cdr ? 1 : 0;
     754    pargs = tree->car->cdr->cdr->cdr->car;
     755    /* keyword arguments */
     756    ka = tail? node_len(tail->cdr->car) : 0;
     757    /* keyword dictionary? */
     758    kd = tail && tail->cdr->cdr->car? 1 : 0;
     759    /* block argument? */
     760    ba = tail && tail->cdr->cdr->cdr->car ? 1 : 0;
    674761
    675762    if (ma > 0x1f || oa > 0x1f || pa > 0x1f || ka > 0x1f) {
    676763      codegen_error(s, "too many formal arguments");
    677764    }
    678     a = ((mrb_aspec)(ma & 0x1f) << 18)
    679       | ((mrb_aspec)(oa & 0x1f) << 13)
    680       | ((ra & 1) << 12)
    681       | ((pa & 0x1f) << 7)
    682       | ((ka & 0x1f) << 2)
    683       | ((kd & 1)<< 1)
    684       | (ba & 1);
    685     s->ainfo = (((ma+oa) & 0x3f) << 6) /* (12bits = 6:1:5) */
    686       | ((ra & 1) << 5)
    687       | (pa & 0x1f);
    688     genop(s, MKOP_Ax(OP_ENTER, a));
     765    a = MRB_ARGS_REQ(ma)
     766      | MRB_ARGS_OPT(oa)
     767      | (ra? MRB_ARGS_REST() : 0)
     768      | MRB_ARGS_POST(pa)
     769      | MRB_ARGS_KEY(ka, kd)
     770      | (ba? MRB_ARGS_BLOCK() : 0);
     771    s->ainfo = (((ma+oa) & 0x3f) << 7) /* (12bits = 5:1:5:1) */
     772      | ((ra & 0x1) << 6)
     773      | ((pa & 0x1f) << 1)
     774      | ((ka | kd) != 0 ? 0x01 : 0x00);
     775    genop_W(s, OP_ENTER, a);
     776    /* generate jump table for optional arguments initializer */
    689777    pos = new_label(s);
    690778    for (i=0; i<oa; i++) {
    691779      new_label(s);
    692       genop(s, MKOP_sBx(OP_JMP, 0));
     780      genjmp(s, OP_JMP, 0);
    693781    }
    694782    if (oa > 0) {
    695       genop(s, MKOP_sBx(OP_JMP, 0));
     783      genjmp(s, OP_JMP, 0);
    696784    }
    697785    opt = tree->car->cdr->car;
     
    700788      int idx;
    701789
    702       dispatch(s, pos+i);
     790      dispatch(s, pos+i*3+1);
    703791      codegen(s, opt->car->cdr, VAL);
    704       idx = lv_idx(s, (mrb_sym)(intptr_t)opt->car->car);
    705792      pop();
    706       genop_peep(s, MKOP_AB(OP_MOVE, idx, cursp()), NOVAL);
     793      idx = lv_idx(s, nsym(opt->car->car));
     794      gen_move(s, idx, cursp(), 0);
    707795      i++;
    708796      opt = opt->cdr;
    709797    }
    710798    if (oa > 0) {
    711       dispatch(s, pos+i);
    712     }
    713   }
     799      dispatch(s, pos+i*3+1);
     800    }
     801
     802    /* keyword arguments */
     803    if (tail) {
     804      node *kwds = tail->cdr->car;
     805      int kwrest = 0;
     806
     807      if (tail->cdr->cdr->car) {
     808        kwrest = 1;
     809      }
     810      mrb_assert(nint(tail->car) == NODE_ARGS_TAIL);
     811      mrb_assert(node_len(tail) == 4);
     812
     813      while (kwds) {
     814        int jmpif_key_p, jmp_def_set = -1;
     815        node *kwd = kwds->car, *def_arg = kwd->cdr->cdr->car;
     816        mrb_sym kwd_sym = nsym(kwd->cdr->car);
     817
     818        mrb_assert(nint(kwd->car) == NODE_KW_ARG);
     819
     820        if (def_arg) {
     821          genop_2(s, OP_KEY_P, lv_idx(s, kwd_sym), new_sym(s, kwd_sym));
     822          jmpif_key_p = genjmp2(s, OP_JMPIF, lv_idx(s, kwd_sym), 0, 0);
     823          codegen(s, def_arg, VAL);
     824          pop();
     825          gen_move(s, lv_idx(s, kwd_sym), cursp(), 0);
     826          jmp_def_set = genjmp(s, OP_JMP, 0);
     827          dispatch(s, jmpif_key_p);
     828        }
     829        genop_2(s, OP_KARG, lv_idx(s, kwd_sym), new_sym(s, kwd_sym));
     830        if (jmp_def_set != -1) {
     831          dispatch(s, jmp_def_set);
     832        }
     833        i++;
     834
     835        kwds = kwds->cdr;
     836      }
     837      if (tail->cdr->car && !kwrest) {
     838        genop_0(s, OP_KEYEND);
     839      }
     840    }
     841
     842    /* argument destructuring */
     843    if (margs) {
     844      node *n = margs;
     845
     846      pos = 1;
     847      while (n) {
     848        if (nint(n->car->car) == NODE_MASGN) {
     849          gen_vmassignment(s, n->car->cdr->car, pos, NOVAL);
     850        }
     851        pos++;
     852        n = n->cdr;
     853      }
     854    }
     855    if (pargs) {
     856      node *n = margs;
     857
     858      pos = ma+oa+ra+1;
     859      while (n) {
     860        if (nint(n->car->car) == NODE_MASGN) {
     861          gen_vmassignment(s, n->car->cdr->car, pos, NOVAL);
     862        }
     863        pos++;
     864        n = n->cdr;
     865      }
     866    }
     867  }
     868
    714869  codegen(s, tree->cdr->car, VAL);
    715870  pop();
    716871  if (s->pc > 0) {
    717     c = s->iseq[s->pc-1];
    718     if (GET_OPCODE(c) != OP_RETURN || GETARG_B(c) != OP_R_NORMAL || s->pc == s->lastlabel) {
    719       if (s->nregs == 0) {
    720         genop(s, MKOP_A(OP_LOADNIL, 0));
    721         genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
    722       }
    723       else {
    724         genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
    725       }
    726     }
     872    gen_return(s, OP_RETURN, cursp());
    727873  }
    728874  if (blk) {
     
    737883{
    738884  codegen_scope *scope = scope_new(s->mrb, s, tree->car);
    739   if (scope == NULL) {
    740     raise_error(s, "unexpected scope");
    741   }
    742885
    743886  codegen(scope, tree->cdr, VAL);
     887  gen_return(scope, OP_RETURN, scope->sp-1);
    744888  if (!s->iseq) {
    745     genop(scope, MKOP_A(OP_STOP, 0));
    746   }
    747   else if (!val) {
    748     genop(scope, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
    749   }
    750   else {
    751     if (scope->nregs == 0) {
    752       genop(scope, MKOP_A(OP_LOADNIL, 0));
    753       genop(scope, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL));
    754     }
    755     else {
    756       genop_peep(scope, MKOP_AB(OP_RETURN, scope->sp-1, OP_R_NORMAL), NOVAL);
    757     }
     889    genop_0(scope, OP_STOP);
    758890  }
    759891  scope_finish(scope);
     
    769901{
    770902  while (t) {
    771     if ((intptr_t)t->car->car == NODE_SPLAT) return FALSE;
     903    if (nint(t->car->car) == NODE_SPLAT) return FALSE;
    772904    t = t->cdr;
    773905  }
     
    782914  char *name2;
    783915
    784   name = mrb_sym2name_len(s->mrb, a, &len);
     916  name = mrb_sym_name_len(s->mrb, a, &len);
    785917  name2 = (char *)codegen_palloc(s,
    786918                                 (size_t)len
     
    805937
    806938  while (t) {
    807     is_splat = (intptr_t)t->car->car == NODE_SPLAT; /* splat mode */
     939    is_splat = nint(t->car->car) == NODE_SPLAT; /* splat mode */
    808940    if (
    809941      n+extra >= CALL_MAXARGS - 1 /* need to subtract one because vm.c expects an array if n == CALL_MAXARGS */
    810942      || is_splat) {
    811943      if (val) {
    812         if (is_splat && n == 0 && (intptr_t)t->car->cdr->car == NODE_ARRAY) {
     944        if (is_splat && n == 0 && nint(t->car->cdr->car) == NODE_ARRAY) {
    813945          codegen(s, t->car->cdr, VAL);
    814946          pop();
     
    816948        else {
    817949          pop_n(n);
    818           genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), n));
     950          if (n == 0 && is_splat) {
     951            genop_1(s, OP_LOADNIL, cursp());
     952          }
     953          else {
     954            genop_2(s, OP_ARRAY, cursp(), n);
     955          }
    819956          push();
    820957          codegen(s, t->car, VAL);
    821958          pop(); pop();
    822959          if (is_splat) {
    823             genop(s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));
     960            genop_1(s, OP_ARYCAT, cursp());
    824961          }
    825962          else {
    826             genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));
     963            genop_1(s, OP_ARYPUSH, cursp());
    827964          }
    828965        }
     
    832969          codegen(s, t->car, VAL);
    833970          pop(); pop();
    834           if ((intptr_t)t->car->car == NODE_SPLAT) {
    835             genop(s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));
     971          if (nint(t->car->car) == NODE_SPLAT) {
     972            genop_1(s, OP_ARYCAT, cursp());
    836973          }
    837974          else {
    838             genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));
     975            genop_1(s, OP_ARYPUSH, cursp());
    839976          }
    840977          t = t->cdr;
     
    860997gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe)
    861998{
    862   mrb_sym sym = name ? name : sym(tree->cdr->car);
    863   int idx, skip = 0;
     999  mrb_sym sym = name ? name : nsym(tree->cdr->car);
     1000  int skip = 0;
    8641001  int n = 0, noop = 0, sendv = 0, blk = 0;
    8651002
     
    8671004  if (safe) {
    8681005    int recv = cursp()-1;
    869     genop(s, MKOP_A(OP_LOADNIL, cursp()));
    870     push();
    871     genop(s, MKOP_AB(OP_MOVE, cursp(), recv));
    872     push(); pop();              /* space for a block */
    873     pop();
    874     idx = new_msym(s, mrb_intern_lit(s->mrb, "=="));
    875     genop(s, MKOP_ABC(OP_EQ, cursp(), idx, 1));
    876     skip = genop(s, MKOP_AsBx(OP_JMPIF, cursp(), 0));
    877   }
    878   idx = new_msym(s, sym);
     1006    gen_move(s, cursp(), recv, 1);
     1007    skip = genjmp2(s, OP_JMPNIL, cursp(), 0, val);
     1008  }
    8791009  tree = tree->cdr->cdr->car;
    8801010  if (tree) {
     
    8851015    }
    8861016  }
    887   if (sp) {
     1017  if (sp) {                     /* last argument pushed (attr=) */
    8881018    if (sendv) {
     1019      gen_move(s, cursp(), sp, 0);
    8891020      pop();
    890       genop(s, MKOP_AB(OP_ARYPUSH, cursp(), sp));
     1021      genop_1(s, OP_ARYPUSH, cursp());
    8911022      push();
    8921023    }
    8931024    else {
    894       genop(s, MKOP_AB(OP_MOVE, cursp(), sp));
     1025      gen_move(s, cursp(), sp, 0);
    8951026      push();
    8961027      n++;
     
    9011032    codegen(s, tree->cdr, VAL);
    9021033    pop();
    903   }
    904   else {
    905     blk = cursp();
     1034    blk = 1;
    9061035  }
    9071036  push();pop();
     
    9091038  {
    9101039    mrb_int symlen;
    911     const char *symname = mrb_sym2name_len(s->mrb, sym, &symlen);
     1040    const char *symname = mrb_sym_name_len(s->mrb, sym, &symlen);
    9121041
    9131042    if (!noop && symlen == 1 && symname[0] == '+' && n == 1)  {
    914       genop_peep(s, MKOP_ABC(OP_ADD, cursp(), idx, n), val);
     1043      gen_addsub(s, OP_ADD, cursp());
    9151044    }
    9161045    else if (!noop && symlen == 1 && symname[0] == '-' && n == 1)  {
    917       genop_peep(s, MKOP_ABC(OP_SUB, cursp(), idx, n), val);
     1046      gen_addsub(s, OP_SUB, cursp());
    9181047    }
    9191048    else if (!noop && symlen == 1 && symname[0] == '*' && n == 1)  {
    920       genop(s, MKOP_ABC(OP_MUL, cursp(), idx, n));
     1049      genop_1(s, OP_MUL, cursp());
    9211050    }
    9221051    else if (!noop && symlen == 1 && symname[0] == '/' && n == 1)  {
    923       genop(s, MKOP_ABC(OP_DIV, cursp(), idx, n));
     1052      genop_1(s, OP_DIV, cursp());
    9241053    }
    9251054    else if (!noop && symlen == 1 && symname[0] == '<' && n == 1)  {
    926       genop(s, MKOP_ABC(OP_LT, cursp(), idx, n));
     1055      genop_1(s, OP_LT, cursp());
    9271056    }
    9281057    else if (!noop && symlen == 2 && symname[0] == '<' && symname[1] == '=' && n == 1)  {
    929       genop(s, MKOP_ABC(OP_LE, cursp(), idx, n));
     1058      genop_1(s, OP_LE, cursp());
    9301059    }
    9311060    else if (!noop && symlen == 1 && symname[0] == '>' && n == 1)  {
    932       genop(s, MKOP_ABC(OP_GT, cursp(), idx, n));
     1061      genop_1(s, OP_GT, cursp());
    9331062    }
    9341063    else if (!noop && symlen == 2 && symname[0] == '>' && symname[1] == '=' && n == 1)  {
    935       genop(s, MKOP_ABC(OP_GE, cursp(), idx, n));
     1064      genop_1(s, OP_GE, cursp());
    9361065    }
    9371066    else if (!noop && symlen == 2 && symname[0] == '=' && symname[1] == '=' && n == 1)  {
    938       genop(s, MKOP_ABC(OP_EQ, cursp(), idx, n));
     1067      genop_1(s, OP_EQ, cursp());
    9391068    }
    9401069    else {
    941       if (sendv) n = CALL_MAXARGS;
    942       if (blk > 0) {                   /* no block */
    943         genop(s, MKOP_ABC(OP_SEND, cursp(), idx, n));
     1070      int idx = new_sym(s, sym);
     1071
     1072      if (sendv) {
     1073        genop_2(s, blk ? OP_SENDVB : OP_SENDV, cursp(), idx);
    9441074      }
    9451075      else {
    946         genop(s, MKOP_ABC(OP_SENDB, cursp(), idx, n));
     1076        genop_3(s, blk ? OP_SENDB : OP_SEND, cursp(), idx, n);
    9471077      }
    9481078    }
     
    9601090{
    9611091  int idx;
    962   int type = (intptr_t)tree->car;
     1092  int type = nint(tree->car);
    9631093
    9641094  tree = tree->cdr;
    9651095  switch (type) {
    9661096  case NODE_GVAR:
    967     idx = new_sym(s, sym(tree));
    968     genop_peep(s, MKOP_ABx(OP_SETGLOBAL, sp, idx), val);
    969     break;
     1097    idx = new_sym(s, nsym(tree));
     1098    genop_2(s, OP_SETGV, sp, idx);
     1099    break;
     1100  case NODE_ARG:
    9701101  case NODE_LVAR:
    971     idx = lv_idx(s, sym(tree));
     1102    idx = lv_idx(s, nsym(tree));
    9721103    if (idx > 0) {
    9731104      if (idx != sp) {
    974         genop_peep(s, MKOP_AB(OP_MOVE, idx, sp), val);
     1105        gen_move(s, idx, sp, val);
     1106        if (val && on_eval(s)) genop_0(s, OP_NOP);
    9751107      }
    9761108      break;
     
    9811113
    9821114      while (up) {
    983         idx = lv_idx(up, sym(tree));
     1115        idx = lv_idx(up, nsym(tree));
    9841116        if (idx > 0) {
    985           genop_peep(s, MKOP_ABC(OP_SETUPVAR, sp, idx, lv), val);
     1117          genop_3(s, OP_SETUPVAR, sp, idx, lv);
    9861118          break;
    9871119        }
     
    9911123    }
    9921124    break;
     1125  case NODE_NVAR:
     1126    idx = nint(tree);
     1127    codegen_error(s, "Can't assign to numbered parameter");
     1128    break;
    9931129  case NODE_IVAR:
    994     idx = new_sym(s, sym(tree));
    995     genop_peep(s, MKOP_ABx(OP_SETIV, sp, idx), val);
     1130    idx = new_sym(s, nsym(tree));
     1131    genop_2(s, OP_SETIV, sp, idx);
    9961132    break;
    9971133  case NODE_CVAR:
    998     idx = new_sym(s, sym(tree));
    999     genop_peep(s, MKOP_ABx(OP_SETCV, sp, idx), val);
     1134    idx = new_sym(s, nsym(tree));
     1135    genop_2(s, OP_SETCV, sp, idx);
    10001136    break;
    10011137  case NODE_CONST:
    1002     idx = new_sym(s, sym(tree));
    1003     genop_peep(s, MKOP_ABx(OP_SETCONST, sp, idx), val);
     1138    idx = new_sym(s, nsym(tree));
     1139    genop_2(s, OP_SETCONST, sp, idx);
    10041140    break;
    10051141  case NODE_COLON2:
    1006     idx = new_sym(s, sym(tree->cdr));
    1007     genop_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), NOVAL);
     1142    gen_move(s, cursp(), sp, 0);
    10081143    push();
    10091144    codegen(s, tree->car, VAL);
    10101145    pop_n(2);
    1011     genop_peep(s, MKOP_ABx(OP_SETMCNST, cursp(), idx), val);
     1146    idx = new_sym(s, nsym(tree->cdr));
     1147    genop_2(s, OP_SETMCNST, sp, idx);
    10121148    break;
    10131149
     
    10151151  case NODE_SCALL:
    10161152    push();
    1017     gen_call(s, tree, attrsym(s, sym(tree->cdr->car)), sp, NOVAL,
     1153    gen_call(s, tree, attrsym(s, nsym(tree->cdr->car)), sp, NOVAL,
    10181154             type == NODE_SCALL);
    10191155    pop();
    10201156    if (val) {
    1021       genop_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), val);
     1157      gen_move(s, cursp(), sp, 0);
    10221158    }
    10231159    break;
     
    10501186    n = 0;
    10511187    while (t) {
    1052       genop(s, MKOP_ABC(OP_AREF, cursp(), rhs, n));
    1053       gen_assignment(s, t->car, cursp(), NOVAL);
     1188      int sp = cursp();
     1189
     1190      genop_3(s, OP_AREF, sp, rhs, n);
     1191      push();
     1192      gen_assignment(s, t->car, sp, NOVAL);
     1193      pop();
    10541194      n++;
    10551195      t = t->cdr;
     
    10651205      }
    10661206    }
    1067     if (val) {
    1068       genop(s, MKOP_AB(OP_MOVE, cursp(), rhs));
    1069     }
    1070     else {
    1071       pop();
    1072     }
    1073     push_n(post);
    1074     pop_n(post);
    1075     genop(s, MKOP_ABC(OP_APOST, cursp(), n, post));
     1207    gen_move(s, cursp(), rhs, val);
     1208    push_n(post+1);
     1209    pop_n(post+1);
     1210    genop_3(s, OP_APOST, cursp(), n, post);
    10761211    n = 1;
    1077     if (t->car) {              /* rest */
     1212    if (t->car && t->car != (node*)-1) { /* rest */
    10781213      gen_assignment(s, t->car, cursp(), NOVAL);
    10791214    }
     
    10861221      }
    10871222    }
    1088     if (!val) {
    1089       push();
     1223    if (val) {
     1224      gen_move(s, cursp(), rhs, 0);
    10901225    }
    10911226  }
     
    10931228
    10941229static void
    1095 gen_send_intern(codegen_scope *s)
     1230gen_intern(codegen_scope *s)
    10961231{
    10971232  pop();
    1098   genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "intern")), 0));
     1233  genop_1(s, OP_INTERN, cursp());
    10991234  push();
    11001235}
     1236
    11011237static void
    11021238gen_literal_array(codegen_scope *s, node *tree, mrb_bool sym, int val)
     
    11061242
    11071243    while (tree) {
    1108       switch ((intptr_t)tree->car->car) {
     1244      switch (nint(tree->car->car)) {
    11091245      case NODE_STR:
    1110         if ((tree->cdr == NULL) && ((intptr_t)tree->car->cdr->cdr == 0))
     1246        if ((tree->cdr == NULL) && (nint(tree->car->cdr->cdr) == 0))
    11111247          break;
    11121248        /* fall through */
     
    11211257          ++i;
    11221258          if (sym)
    1123             gen_send_intern(s);
     1259            gen_intern(s);
    11241260        }
    11251261        break;
    11261262      }
    1127       if (j >= 2) {
     1263      while (j >= 2) {
    11281264        pop(); pop();
    1129         genop_peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL);
     1265        genop_1(s, OP_STRCAT, cursp());
    11301266        push();
    1131         j = 1;
     1267        j--;
    11321268      }
    11331269      tree = tree->cdr;
     
    11361272      ++i;
    11371273      if (sym)
    1138         gen_send_intern(s);
     1274        gen_intern(s);
    11391275    }
    11401276    pop_n(i);
    1141     genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), i));
     1277    genop_2(s, OP_ARRAY, cursp(), i);
    11421278    push();
    11431279  }
    11441280  else {
    11451281    while (tree) {
    1146       switch ((intptr_t)tree->car->car) {
     1282      switch (nint(tree->car->car)) {
    11471283      case NODE_BEGIN: case NODE_BLOCK:
    11481284        codegen(s, tree->car, NOVAL);
     
    11581294  int idx = new_lit(s, mrb_str_new_cstr(s->mrb, msg));
    11591295
    1160   genop(s, MKOP_ABx(OP_ERR, 1, idx));
    1161 }
    1162 
     1296  genop_1(s, OP_ERR, idx);
     1297}
     1298
     1299#ifndef MRB_WITHOUT_FLOAT
    11631300static double
    11641301readint_float(codegen_scope *s, const char *p, int base)
     
    11861323  return f;
    11871324}
     1325#endif
    11881326
    11891327static mrb_int
     
    12331371gen_retval(codegen_scope *s, node *tree)
    12341372{
    1235   if ((intptr_t)tree->car == NODE_SPLAT) {
    1236     genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), 0));
    1237     push();
     1373  if (nint(tree->car) == NODE_SPLAT) {
    12381374    codegen(s, tree, VAL);
    1239     pop(); pop();
    1240     genop(s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));
     1375    pop();
     1376    genop_1(s, OP_ARYDUP, cursp());
    12411377  }
    12421378  else {
     
    12541390  if (!tree) {
    12551391    if (val) {
    1256       genop(s, MKOP_A(OP_LOADNIL, cursp()));
     1392      genop_1(s, OP_LOADNIL, cursp());
    12571393      push();
    12581394    }
     
    12651401  }
    12661402  if (s->irep && s->filename_index != tree->filename_index) {
    1267     s->irep->filename = mrb_parser_get_filename(s->parser, s->filename_index);
    1268     mrb_debug_info_append_file(s->mrb, s->irep, s->debug_start_pos, s->pc);
     1403    mrb_sym fname = mrb_parser_get_filename(s->parser, s->filename_index);
     1404    const char *filename = mrb_sym_name_len(s->mrb, fname, NULL);
     1405
     1406    mrb_debug_info_append_file(s->mrb, s->irep->debug_info,
     1407                               filename, s->lines, s->debug_start_pos, s->pc);
    12691408    s->debug_start_pos = s->pc;
    12701409    s->filename_index = tree->filename_index;
    1271     s->filename = mrb_parser_get_filename(s->parser, tree->filename_index);
    1272   }
    1273 
    1274   nt = (intptr_t)tree->car;
     1410    s->filename_sym = mrb_parser_get_filename(s->parser, tree->filename_index);
     1411  }
     1412
     1413  nt = nint(tree->car);
    12751414  s->lineno = tree->lineno;
    12761415  tree = tree->cdr;
     
    12781417  case NODE_BEGIN:
    12791418    if (val && !tree) {
    1280       genop(s, MKOP_A(OP_LOADNIL, cursp()));
     1419      genop_1(s, OP_LOADNIL, cursp());
    12811420      push();
    12821421    }
     
    12891428  case NODE_RESCUE:
    12901429    {
    1291       int onerr, noexc, exend, pos1, pos2, tmp;
     1430      int noexc, exend, pos1, pos2, tmp;
    12921431      struct loopinfo *lp;
    12931432
    12941433      if (tree->car == NULL) goto exit;
    1295       onerr = genop(s, MKOP_Bx(OP_ONERR, 0));
    12961434      lp = loop_push(s, LOOP_BEGIN);
    1297       lp->pc1 = onerr;
     1435      lp->pc0 = new_label(s);
     1436      lp->pc1 = genjmp(s, OP_ONERR, 0);
    12981437      codegen(s, tree->car, VAL);
    12991438      pop();
    13001439      lp->type = LOOP_RESCUE;
    1301       noexc = genop(s, MKOP_Bx(OP_JMP, 0));
    1302       dispatch(s, onerr);
     1440      noexc = genjmp(s, OP_JMP, 0);
     1441      dispatch(s, lp->pc1);
    13031442      tree = tree->cdr;
    13041443      exend = 0;
     
    13081447        int exc = cursp();
    13091448
    1310         genop(s, MKOP_ABC(OP_RESCUE, exc, 0, 0));
     1449        genop_1(s, OP_EXCEPT, exc);
    13111450        push();
    13121451        while (n2) {
     
    13171456          pos2 = 0;
    13181457          do {
    1319             if (n4 && n4->car && (intptr_t)n4->car->car == NODE_SPLAT) {
     1458            if (n4 && n4->car && nint(n4->car->car) == NODE_SPLAT) {
    13201459              codegen(s, n4->car, VAL);
    1321               genop(s, MKOP_AB(OP_MOVE, cursp(), exc));
     1460              gen_move(s, cursp(), exc, 0);
     1461              push_n(2); pop_n(2); /* space for one arg and a block */
    13221462              pop();
    1323               genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "__case_eqq")), 1));
     1463              genop_3(s, OP_SEND, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "__case_eqq")), 1);
    13241464            }
    13251465            else {
     
    13281468              }
    13291469              else {
    1330                 genop(s, MKOP_ABx(OP_GETCONST, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "StandardError"))));
     1470                genop_2(s, OP_GETCONST, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "StandardError")));
    13311471                push();
    13321472              }
    13331473              pop();
    1334               genop(s, MKOP_ABC(OP_RESCUE, exc, cursp(), 1));
     1474              genop_2(s, OP_RESCUE, exc, cursp());
    13351475            }
    1336             tmp = genop(s, MKOP_AsBx(OP_JMPIF, cursp(), pos2));
     1476            tmp = genjmp2(s, OP_JMPIF, cursp(), pos2, val);
    13371477            pos2 = tmp;
    13381478            if (n4) {
     
    13401480            }
    13411481          } while (n4);
    1342           pos1 = genop(s, MKOP_sBx(OP_JMP, 0));
     1482          pos1 = genjmp(s, OP_JMP, 0);
    13431483          dispatch_linked(s, pos2);
    13441484
     
    13511491            if (val) pop();
    13521492          }
    1353           tmp = genop(s, MKOP_sBx(OP_JMP, exend));
     1493          tmp = genjmp(s, OP_JMP, exend);
    13541494          exend = tmp;
    13551495          n2 = n2->cdr;
     
    13581498        if (pos1) {
    13591499          dispatch(s, pos1);
    1360           genop(s, MKOP_A(OP_RAISE, exc));
     1500          genop_1(s, OP_RAISE, exc);
    13611501        }
    13621502      }
     
    13641504      tree = tree->cdr;
    13651505      dispatch(s, noexc);
    1366       genop(s, MKOP_A(OP_POPERR, 1));
     1506      genop_1(s, OP_POPERR, 1);
    13671507      if (tree->car) {
    13681508        codegen(s, tree->car, val);
     
    13781518  case NODE_ENSURE:
    13791519    if (!tree->cdr || !tree->cdr->cdr ||
    1380         ((intptr_t)tree->cdr->cdr->car == NODE_BEGIN &&
     1520        (nint(tree->cdr->cdr->car) == NODE_BEGIN &&
    13811521         tree->cdr->cdr->cdr)) {
    13821522      int idx;
    1383       int epush = s->pc;
    1384 
    1385       genop(s, MKOP_Bx(OP_EPUSH, 0));
     1523
    13861524      s->ensure_level++;
     1525      idx = scope_body(s, tree->cdr, NOVAL);
     1526      genop_1(s, OP_EPUSH, idx);
    13871527      codegen(s, tree->car, val);
    1388       idx = scope_body(s, tree->cdr, NOVAL);
    1389       s->iseq[epush] = MKOP_Bx(OP_EPUSH, idx);
    13901528      s->ensure_level--;
    1391       genop_peep(s, MKOP_A(OP_EPOP, 1), NOVAL);
     1529      genop_1(s, OP_EPOP, 1);
    13921530    }
    13931531    else {                      /* empty ensure ignored */
     
    14001538      int idx = lambda_body(s, tree, 1);
    14011539
    1402       genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_LAMBDA));
     1540      genop_2(s, OP_LAMBDA, cursp(), idx);
    14031541      push();
    14041542    }
     
    14091547      int idx = lambda_body(s, tree, 1);
    14101548
    1411       genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_BLOCK));
     1549      genop_2(s, OP_BLOCK, cursp(), idx);
    14121550      push();
    14131551    }
     
    14161554  case NODE_IF:
    14171555    {
    1418       int pos1, pos2;
    1419       node *e = tree->cdr->cdr->car;
     1556      int pos1, pos2, nil_p = FALSE;
     1557      node *elsepart = tree->cdr->cdr->car;
    14201558
    14211559      if (!tree->car) {
    1422         codegen(s, e, val);
     1560        codegen(s, elsepart, val);
    14231561        goto exit;
    14241562      }
    1425       switch ((intptr_t)tree->car->car) {
     1563      switch (nint(tree->car->car)) {
    14261564      case NODE_TRUE:
    14271565      case NODE_INT:
     
    14311569      case NODE_FALSE:
    14321570      case NODE_NIL:
    1433         codegen(s, e, val);
     1571        codegen(s, elsepart, val);
    14341572        goto exit;
    1435       }
    1436       codegen(s, tree->car, VAL);
     1573      case NODE_CALL:
     1574        {
     1575          node *n = tree->car->cdr;
     1576          mrb_sym mid = nsym(n->cdr->car);
     1577          mrb_sym mnil = mrb_intern_lit(s->mrb, "nil?");
     1578          if (mid == mnil && n->cdr->cdr->car == NULL) {
     1579            nil_p = TRUE;
     1580            codegen(s, n->car, VAL);
     1581          }
     1582        }
     1583        break;
     1584      }
     1585      if (!nil_p) {
     1586        codegen(s, tree->car, VAL);
     1587      }
    14371588      pop();
    1438       pos1 = genop_peep(s, MKOP_AsBx(OP_JMPNOT, cursp(), 0), NOVAL);
    1439 
    1440       codegen(s, tree->cdr->car, val);
    1441       if (e) {
     1589      if (val || tree->cdr->car) {
     1590        if (nil_p) {
     1591          pos2 = genjmp2(s, OP_JMPNIL, cursp(), 0, val);
     1592          pos1 = genjmp(s, OP_JMP, 0);
     1593          dispatch(s, pos2);
     1594        }
     1595        else {
     1596          pos1 = genjmp2(s, OP_JMPNOT, cursp(), 0, val);
     1597        }
     1598        codegen(s, tree->cdr->car, val);
    14421599        if (val) pop();
    1443         pos2 = genop(s, MKOP_sBx(OP_JMP, 0));
    1444         dispatch(s, pos1);
    1445         codegen(s, e, val);
    1446         dispatch(s, pos2);
    1447       }
    1448       else {
    1449         if (val) {
    1450           pop();
    1451           pos2 = genop(s, MKOP_sBx(OP_JMP, 0));
     1600        if (elsepart || val) {
     1601          pos2 = genjmp(s, OP_JMP, 0);
    14521602          dispatch(s, pos1);
    1453           genop(s, MKOP_A(OP_LOADNIL, cursp()));
     1603          codegen(s, elsepart, val);
    14541604          dispatch(s, pos2);
    1455           push();
    14561605        }
    14571606        else {
    14581607          dispatch(s, pos1);
     1608        }
     1609      }
     1610      else {                    /* empty then-part */
     1611        if (elsepart) {
     1612          if (nil_p) {
     1613            pos1 = genjmp2(s, OP_JMPNIL, cursp(), 0, val);
     1614          }
     1615          else {
     1616            pos1 = genjmp2(s, OP_JMPIF, cursp(), 0, val);
     1617          }
     1618          codegen(s, elsepart, val);
     1619          dispatch(s, pos1);
     1620        }
     1621        else if (val && !nil_p) {
     1622          genop_1(s, OP_LOADNIL, cursp());
     1623          push();
    14591624        }
    14601625      }
     
    14681633      codegen(s, tree->car, VAL);
    14691634      pop();
    1470       pos = genop(s, MKOP_AsBx(OP_JMPNOT, cursp(), 0));
     1635      pos = genjmp2(s, OP_JMPNOT, cursp(), 0, val);
    14711636      codegen(s, tree->cdr, val);
    14721637      dispatch(s, pos);
     
    14801645      codegen(s, tree->car, VAL);
    14811646      pop();
    1482       pos = genop(s, MKOP_AsBx(OP_JMPIF, cursp(), 0));
     1647      pos = genjmp2(s, OP_JMPIF, cursp(), 0, val);
    14831648      codegen(s, tree->cdr, val);
    14841649      dispatch(s, pos);
     
    14901655      struct loopinfo *lp = loop_push(s, LOOP_NORMAL);
    14911656
    1492       lp->pc1 = genop(s, MKOP_sBx(OP_JMP, 0));
     1657      lp->pc0 = new_label(s);
     1658      lp->pc1 = genjmp(s, OP_JMP, 0);
    14931659      lp->pc2 = new_label(s);
    14941660      codegen(s, tree->cdr, NOVAL);
     
    14961662      codegen(s, tree->car, VAL);
    14971663      pop();
    1498       genop(s, MKOP_AsBx(OP_JMPIF, cursp(), lp->pc2 - s->pc));
     1664      genjmp2(s, OP_JMPIF, cursp(), lp->pc2, NOVAL);
    14991665
    15001666      loop_pop(s, val);
     
    15061672      struct loopinfo *lp = loop_push(s, LOOP_NORMAL);
    15071673
    1508       lp->pc1 = genop(s, MKOP_sBx(OP_JMP, 0));
     1674      lp->pc0 = new_label(s);
     1675      lp->pc1 = genjmp(s, OP_JMP, 0);
    15091676      lp->pc2 = new_label(s);
    15101677      codegen(s, tree->cdr, NOVAL);
     
    15121679      codegen(s, tree->car, VAL);
    15131680      pop();
    1514       genop(s, MKOP_AsBx(OP_JMPNOT, cursp(), lp->pc2 - s->pc));
     1681      genjmp2(s, OP_JMPNOT, cursp(), lp->pc2, NOVAL);
    15151682
    15161683      loop_pop(s, val);
     
    15411708          codegen(s, n->car, VAL);
    15421709          if (head) {
    1543             genop(s, MKOP_AB(OP_MOVE, cursp(), head));
    1544             pop();
    1545             if ((intptr_t)n->car->car == NODE_SPLAT) {
    1546               genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "__case_eqq")), 1));
     1710            gen_move(s, cursp(), head, 0);
     1711            push(); push(); pop(); pop(); pop();
     1712            if (nint(n->car->car) == NODE_SPLAT) {
     1713              genop_3(s, OP_SEND, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "__case_eqq")), 1);
    15471714            }
    15481715            else {
    1549               genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "===")), 1));
     1716              genop_3(s, OP_SEND, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "===")), 1);
    15501717            }
    15511718          }
     
    15531720            pop();
    15541721          }
    1555           tmp = genop(s, MKOP_AsBx(OP_JMPIF, cursp(), pos2));
     1722          tmp = genjmp2(s, OP_JMPIF, cursp(), pos2, NOVAL);
    15561723          pos2 = tmp;
    15571724          n = n->cdr;
    15581725        }
    15591726        if (tree->car->car) {
    1560           pos1 = genop(s, MKOP_sBx(OP_JMP, 0));
     1727          pos1 = genjmp(s, OP_JMP, 0);
    15611728          dispatch_linked(s, pos2);
    15621729        }
    15631730        codegen(s, tree->car->cdr, val);
    15641731        if (val) pop();
    1565         tmp = genop(s, MKOP_sBx(OP_JMP, pos3));
     1732        tmp = genjmp(s, OP_JMP, pos3);
    15661733        pos3 = tmp;
    15671734        if (pos1) dispatch(s, pos1);
     
    15701737      if (val) {
    15711738        int pos = cursp();
    1572         genop(s, MKOP_A(OP_LOADNIL, cursp()));
     1739        genop_1(s, OP_LOADNIL, cursp());
    15731740        if (pos3) dispatch_linked(s, pos3);
    15741741        if (head) pop();
    15751742        if (cursp() != pos) {
    1576           genop(s, MKOP_AB(OP_MOVE, cursp(), pos));
     1743          gen_move(s, cursp(), pos, 0);
    15771744        }
    15781745        push();
     
    16061773    if (val) {
    16071774      pop(); pop();
    1608       genop(s, MKOP_ABC(OP_RANGE, cursp(), cursp(), FALSE));
     1775      genop_1(s, OP_RANGE_INC, cursp());
    16091776      push();
    16101777    }
     
    16161783    if (val) {
    16171784      pop(); pop();
    1618       genop(s, MKOP_ABC(OP_RANGE, cursp(), cursp(), TRUE));
     1785      genop_1(s, OP_RANGE_EXC, cursp());
    16191786      push();
    16201787    }
     
    16231790  case NODE_COLON2:
    16241791    {
    1625       int sym = new_sym(s, sym(tree->cdr));
     1792      int sym = new_sym(s, nsym(tree->cdr));
    16261793
    16271794      codegen(s, tree->car, VAL);
    16281795      pop();
    1629       genop(s, MKOP_ABx(OP_GETMCNST, cursp(), sym));
     1796      genop_2(s, OP_GETMCNST, cursp(), sym);
    16301797      if (val) push();
    16311798    }
     
    16341801  case NODE_COLON3:
    16351802    {
    1636       int sym = new_sym(s, sym(tree));
    1637 
    1638       genop(s, MKOP_A(OP_OCLASS, cursp()));
    1639       genop(s, MKOP_ABx(OP_GETMCNST, cursp(), sym));
     1803      int sym = new_sym(s, nsym(tree));
     1804
     1805      genop_1(s, OP_OCLASS, cursp());
     1806      genop_2(s, OP_GETMCNST, cursp(), sym);
    16401807      if (val) push();
    16411808    }
     
    16501817        if (val) {
    16511818          pop_n(n);
    1652           genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), n));
     1819          genop_2(s, OP_ARRAY, cursp(), n);
    16531820          push();
    16541821        }
     
    16611828
    16621829  case NODE_HASH:
     1830  case NODE_KW_HASH:
    16631831    {
    16641832      int len = 0;
     
    16661834
    16671835      while (tree) {
    1668         codegen(s, tree->car->car, val);
    1669         codegen(s, tree->car->cdr, val);
    1670         len++;
     1836        if (nint(tree->car->car->car) == NODE_KW_REST_ARGS) {
     1837          if (len > 0) {
     1838            pop_n(len*2);
     1839            if (!update) {
     1840              genop_2(s, OP_HASH, cursp(), len);
     1841            }
     1842            else {
     1843              pop();
     1844              genop_2(s, OP_HASHADD, cursp(), len);
     1845            }
     1846            push();
     1847          }
     1848          codegen(s, tree->car->cdr, VAL);
     1849          if (len > 0 || update) {
     1850            pop(); pop();
     1851            genop_1(s, OP_HASHCAT, cursp());
     1852            push();
     1853          }
     1854          update = TRUE;
     1855          len = 0;
     1856        }
     1857        else {
     1858          codegen(s, tree->car->car, val);
     1859          codegen(s, tree->car->cdr, val);
     1860          len++;
     1861        }
    16711862        tree = tree->cdr;
    1672         if (val && len == 126) {
     1863        if (val && len == 255) {
    16731864          pop_n(len*2);
    1674           genop(s, MKOP_ABC(OP_HASH, cursp(), cursp(), len));
    1675           if (update) {
     1865          if (!update) {
     1866            genop_2(s, OP_HASH, cursp(), len);
     1867          }
     1868          else {
    16761869            pop();
    1677             genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "__update")), 1));
     1870            genop_2(s, OP_HASHADD, cursp(), len);
    16781871          }
    16791872          push();
     
    16841877      if (val) {
    16851878        pop_n(len*2);
    1686         genop(s, MKOP_ABC(OP_HASH, cursp(), cursp(), len));
    1687         if (update) {
     1879        if (!update) {
     1880          genop_2(s, OP_HASH, cursp(), len);
     1881        }
     1882        else {
    16881883          pop();
    1689           genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "__update")), 1));
     1884          if (len > 0) {
     1885            genop_2(s, OP_HASHADD, cursp(), len);
     1886          }
    16901887        }
    16911888        push();
     
    17101907      int rhs = cursp();
    17111908
    1712       if ((intptr_t)t->car == NODE_ARRAY && t->cdr && nosplat(t->cdr)) {
     1909      if (nint(t->car) == NODE_ARRAY && t->cdr && nosplat(t->cdr)) {
    17131910        /* fixed rhs */
    17141911        t = t->cdr;
     
    17281925            }
    17291926            else {
    1730               genop(s, MKOP_A(OP_LOADNIL, rhs+n));
     1927              genop_1(s, OP_LOADNIL, rhs+n);
    17311928              gen_assignment(s, t->car, rhs+n, NOVAL);
    17321929            }
     
    17521949              rn = len - post - n;
    17531950            }
    1754             genop(s, MKOP_ABC(OP_ARRAY, cursp(), rhs+n, rn));
     1951            genop_3(s, OP_ARRAY2, cursp(), rhs+n, rn);
    17551952            gen_assignment(s, t->car, cursp(), NOVAL);
    17561953            n += rn;
     
    17671964        pop_n(len);
    17681965        if (val) {
    1769           genop(s, MKOP_ABC(OP_ARRAY, rhs, rhs, len));
     1966          genop_2(s, OP_ARRAY, rhs, len);
    17701967          push();
    17711968        }
     
    17841981  case NODE_OP_ASGN:
    17851982    {
    1786       mrb_sym sym = sym(tree->cdr->car);
     1983      mrb_sym sym = nsym(tree->cdr->car);
    17871984      mrb_int len;
    1788       const char *name = mrb_sym2name_len(s->mrb, sym, &len);
     1985      const char *name = mrb_sym_name_len(s->mrb, sym, &len);
    17891986      int idx, callargs = -1, vsp = -1;
    17901987
    17911988      if ((len == 2 && name[0] == '|' && name[1] == '|') &&
    1792           ((intptr_t)tree->car->car == NODE_CONST ||
    1793            (intptr_t)tree->car->car == NODE_CVAR)) {
     1989          (nint(tree->car->car) == NODE_CONST ||
     1990           nint(tree->car->car) == NODE_CVAR)) {
    17941991        int onerr, noexc, exc;
    17951992        struct loopinfo *lp;
    17961993
    1797         onerr = genop(s, MKOP_Bx(OP_ONERR, 0));
     1994        onerr = genjmp(s, OP_ONERR, 0);
    17981995        lp = loop_push(s, LOOP_BEGIN);
    17991996        lp->pc1 = onerr;
     
    18011998        codegen(s, tree->car, VAL);
    18021999        lp->type = LOOP_RESCUE;
    1803         genop(s, MKOP_A(OP_POPERR, 1));
    1804         noexc = genop(s, MKOP_Bx(OP_JMP, 0));
     2000        genop_1(s, OP_POPERR, 1);
     2001        noexc = genjmp(s, OP_JMP, 0);
    18052002        dispatch(s, onerr);
    1806         genop(s, MKOP_ABC(OP_RESCUE, exc, 0, 0));
    1807         genop(s, MKOP_A(OP_LOADF, exc));
     2003        genop_1(s, OP_EXCEPT, exc);
     2004        genop_1(s, OP_LOADF, exc);
    18082005        dispatch(s, noexc);
    18092006        loop_pop(s, NOVAL);
    18102007      }
    1811       else if ((intptr_t)tree->car->car == NODE_CALL) {
     2008      else if (nint(tree->car->car) == NODE_CALL) {
    18122009        node *n = tree->car->cdr;
     2010        int base, i, nargs = 0;
     2011        callargs = 0;
    18132012
    18142013        if (val) {
     
    18172016        }
    18182017        codegen(s, n->car, VAL);   /* receiver */
    1819         idx = new_msym(s, sym(n->cdr->car));
     2018        idx = new_sym(s, nsym(n->cdr->car));
     2019        base = cursp()-1;
    18202020        if (n->cdr->cdr->car) {
    1821           int base = cursp()-1;
    1822           int nargs = gen_values(s, n->cdr->cdr->car->car, VAL, 1);
    1823 
    1824           /* copy receiver and arguments */
     2021          nargs = gen_values(s, n->cdr->cdr->car->car, VAL, 1);
    18252022          if (nargs >= 0) {
    1826             int i;
    1827 
    1828             genop(s, MKOP_AB(OP_MOVE, cursp(), base));
    1829             for (i=0; i<nargs; i++) {
    1830               genop(s, MKOP_AB(OP_MOVE, cursp()+i+1, base+i+1));
    1831             }
    1832             push_n(nargs+1);
    1833             pop_n(nargs+1);
    18342023            callargs = nargs;
    18352024          }
    1836           else {
    1837             /* varargs */
     2025          else { /* varargs */
    18382026            push();
    1839             genop(s, MKOP_AB(OP_MOVE, cursp(), base));
    1840             genop(s, MKOP_AB(OP_MOVE, cursp()+1, base+1));
     2027            nargs = 1;
    18412028            callargs = CALL_MAXARGS;
    18422029          }
    1843           genop(s, MKOP_ABC(OP_SEND, cursp(), idx, callargs));
    1844         }
    1845         else {
    1846           genop(s, MKOP_AB(OP_MOVE, cursp(), cursp()-1));
    1847           genop(s, MKOP_ABC(OP_SEND, cursp(), idx, 0));
    1848           callargs = 0;
    1849         }
     2030        }
     2031        /* copy receiver and arguments */
     2032        gen_move(s, cursp(), base, 1);
     2033        for (i=0; i<nargs; i++) {
     2034          gen_move(s, cursp()+i+1, base+i+1, 1);
     2035        }
     2036        push_n(nargs+2);pop_n(nargs+2); /* space for receiver, arguments and a block */
     2037        genop_3(s, OP_SEND, cursp(), idx, callargs);
    18502038        push();
    18512039      }
     
    18612049        if (val) {
    18622050          if (vsp >= 0) {
    1863             genop(s, MKOP_AB(OP_MOVE, vsp, cursp()));
    1864           }
    1865           pos = genop(s, MKOP_AsBx(name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), 0));
     2051            gen_move(s, vsp, cursp(), 1);
     2052          }
     2053          pos = genjmp2(s, name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), 0, val);
    18662054        }
    18672055        else {
    1868           pos = genop_peep(s, MKOP_AsBx(name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), 0), NOVAL);
     2056          pos = genjmp2(s, name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), 0, val);
    18692057        }
    18702058        codegen(s, tree->cdr->cdr->car, VAL);
    18712059        pop();
    18722060        if (val && vsp >= 0) {
    1873           genop(s, MKOP_AB(OP_MOVE, vsp, cursp()));
    1874         }
    1875         if ((intptr_t)tree->car->car == NODE_CALL) {
    1876           mrb_sym m = sym(tree->car->cdr->cdr->car);
    1877           mrb_sym m2 = attrsym(s, m);
    1878 
    1879           idx = new_msym(s, m2);
    1880           pop();
     2061          gen_move(s, vsp, cursp(), 1);
     2062        }
     2063        if (nint(tree->car->car) == NODE_CALL) {
    18812064          if (callargs == CALL_MAXARGS) {
    1882             genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));
    18832065            pop();
    1884             genop(s, MKOP_ABC(OP_SEND, cursp(), idx, callargs));
     2066            genop_1(s, OP_ARYPUSH, cursp());
    18852067          }
    18862068          else {
    18872069            pop_n(callargs);
    1888             genop(s, MKOP_ABC(OP_SEND, cursp(), idx, callargs+1));
    1889           }
     2070            callargs++;
     2071          }
     2072          pop();
     2073          idx = new_sym(s, attrsym(s, nsym(tree->car->cdr->cdr->car)));
     2074          genop_3(s, OP_SEND, cursp(), idx, callargs);
    18902075        }
    18912076        else {
     
    18992084      pop(); pop();
    19002085
    1901       idx = new_msym(s, sym);
    19022086      if (len == 1 && name[0] == '+')  {
    1903         genop_peep(s, MKOP_ABC(OP_ADD, cursp(), idx, 1), val);
     2087        gen_addsub(s, OP_ADD, cursp());
    19042088      }
    19052089      else if (len == 1 && name[0] == '-')  {
    1906         genop_peep(s, MKOP_ABC(OP_SUB, cursp(), idx, 1), val);
     2090        gen_addsub(s, OP_SUB, cursp());
    19072091      }
    19082092      else if (len == 1 && name[0] == '*')  {
    1909         genop(s, MKOP_ABC(OP_MUL, cursp(), idx, 1));
     2093        genop_1(s, OP_MUL, cursp());
    19102094      }
    19112095      else if (len == 1 && name[0] == '/')  {
    1912         genop(s, MKOP_ABC(OP_DIV, cursp(), idx, 1));
     2096        genop_1(s, OP_DIV, cursp());
    19132097      }
    19142098      else if (len == 1 && name[0] == '<')  {
    1915         genop(s, MKOP_ABC(OP_LT, cursp(), idx, 1));
     2099        genop_1(s, OP_LT, cursp());
    19162100      }
    19172101      else if (len == 2 && name[0] == '<' && name[1] == '=')  {
    1918         genop(s, MKOP_ABC(OP_LE, cursp(), idx, 1));
     2102        genop_1(s, OP_LE, cursp());
    19192103      }
    19202104      else if (len == 1 && name[0] == '>')  {
    1921         genop(s, MKOP_ABC(OP_GT, cursp(), idx, 1));
     2105        genop_1(s, OP_GT, cursp());
    19222106      }
    19232107      else if (len == 2 && name[0] == '>' && name[1] == '=')  {
    1924         genop(s, MKOP_ABC(OP_GE, cursp(), idx, 1));
     2108        genop_1(s, OP_GE, cursp());
    19252109      }
    19262110      else {
    1927         genop(s, MKOP_ABC(OP_SEND, cursp(), idx, 1));
     2111        idx = new_sym(s, sym);
     2112        genop_3(s, OP_SEND, cursp(), idx, 1);
    19282113      }
    19292114      if (callargs < 0) {
     
    19322117      else {
    19332118        if (val && vsp >= 0) {
    1934           genop(s, MKOP_AB(OP_MOVE, vsp, cursp()));
     2119          gen_move(s, vsp, cursp(), 0);
    19352120        }
    19362121        if (callargs == CALL_MAXARGS) {
    19372122          pop();
    1938           genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));
     2123          genop_1(s, OP_ARYPUSH, cursp());
    19392124        }
    19402125        else {
     
    19432128        }
    19442129        pop();
    1945         idx = new_msym(s, attrsym(s,sym(tree->car->cdr->cdr->car)));
    1946         genop(s, MKOP_ABC(OP_SEND, cursp(), idx, callargs));
     2130        idx = new_sym(s, attrsym(s,nsym(tree->car->cdr->cdr->car)));
     2131        genop_3(s, OP_SEND, cursp(), idx, callargs);
    19472132      }
    19482133    }
     
    19612146        if (!s2) break;
    19622147      }
    1963       genop(s, MKOP_ABx(OP_ARGARY, cursp(), (lv & 0xf)));
     2148      genop_2S(s, OP_ARGARY, cursp(), (lv & 0xf));
    19642149      push(); push();         /* ARGARY pushes two values */
    19652150      pop(); pop();
     
    19792164      }
    19802165      else {
    1981         genop(s, MKOP_A(OP_LOADNIL, cursp()));
     2166        genop_1(s, OP_LOADNIL, cursp());
    19822167        push(); pop();
    19832168      }
    19842169      pop_n(n+1);
    19852170      if (sendv) n = CALL_MAXARGS;
    1986       genop(s, MKOP_ABC(OP_SUPER, cursp(), 0, n));
     2171      genop_2(s, OP_SUPER, cursp(), n);
    19872172      if (val) push();
    19882173    }
     
    20012186      }
    20022187      if (s2) ainfo = s2->ainfo;
    2003       genop(s, MKOP_ABx(OP_ARGARY, cursp(), (ainfo<<4)|(lv & 0xf)));
     2188      genop_2S(s, OP_ARGARY, cursp(), (ainfo<<4)|(lv & 0xf));
    20042189      push(); push(); pop();    /* ARGARY pushes two values */
    20052190      if (tree && tree->cdr) {
     
    20082193      }
    20092194      pop(); pop();
    2010       genop(s, MKOP_ABC(OP_SUPER, cursp(), 0, CALL_MAXARGS));
     2195      genop_2(s, OP_SUPER, cursp(), CALL_MAXARGS);
    20112196      if (val) push();
    20122197    }
     
    20182203    }
    20192204    else {
    2020       genop(s, MKOP_A(OP_LOADNIL, cursp()));
     2205      genop_1(s, OP_LOADNIL, cursp());
    20212206    }
    20222207    if (s->loop) {
    2023       genop(s, MKOP_AB(OP_RETURN, cursp(), OP_R_RETURN));
     2208      gen_return(s, OP_RETURN_BLK, cursp());
    20242209    }
    20252210    else {
    2026       genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
     2211      gen_return(s, OP_RETURN, cursp());
    20272212    }
    20282213    if (val) push();
     
    20492234        }
    20502235      }
     2236      push();pop(); /* space for a block */
    20512237      pop_n(n+1);
    2052       genop(s, MKOP_ABx(OP_BLKPUSH, cursp(), (ainfo<<4)|(lv & 0xf)));
     2238      genop_2S(s, OP_BLKPUSH, cursp(), (ainfo<<4)|(lv & 0xf));
    20532239      if (sendv) n = CALL_MAXARGS;
    2054       genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "call")), n));
     2240      genop_3(s, OP_SEND, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "call")), n);
    20552241      if (val) push();
    20562242    }
     
    20682254    else if (s->loop->type == LOOP_NORMAL) {
    20692255      if (s->ensure_level > s->loop->ensure_level) {
    2070         genop_peep(s, MKOP_A(OP_EPOP, s->ensure_level - s->loop->ensure_level), NOVAL);
     2256        genop_1(s, OP_EPOP, s->ensure_level - s->loop->ensure_level);
    20712257      }
    20722258      codegen(s, tree, NOVAL);
    2073       genop(s, MKOP_sBx(OP_JMP, s->loop->pc1 - s->pc));
     2259      genjmp(s, OP_JMP, s->loop->pc0);
    20742260    }
    20752261    else {
     
    20792265      }
    20802266      else {
    2081         genop(s, MKOP_A(OP_LOADNIL, cursp()));
    2082       }
    2083       genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
     2267        genop_1(s, OP_LOADNIL, cursp());
     2268      }
     2269      gen_return(s, OP_RETURN, cursp());
    20842270    }
    20852271    if (val) push();
     
    20922278    else {
    20932279      if (s->ensure_level > s->loop->ensure_level) {
    2094         genop_peep(s, MKOP_A(OP_EPOP, s->ensure_level - s->loop->ensure_level), NOVAL);
    2095       }
    2096       genop(s, MKOP_sBx(OP_JMP, s->loop->pc2 - s->pc));
     2280        genop_1(s, OP_EPOP, s->ensure_level - s->loop->ensure_level);
     2281      }
     2282      genjmp(s, OP_JMP, s->loop->pc2);
    20972283    }
    20982284    if (val) push();
     
    21212307        else {
    21222308          if (n > 0) {
    2123             while (n--) {
    2124               genop_peep(s, MKOP_A(OP_POPERR, 1), NOVAL);
    2125             }
     2309            genop_1(s, OP_POPERR, n);
    21262310          }
    21272311          if (s->ensure_level > lp->ensure_level) {
    2128             genop_peep(s, MKOP_A(OP_EPOP, s->ensure_level - lp->ensure_level), NOVAL);
    2129           }
    2130           genop(s, MKOP_sBx(OP_JMP, lp->pc1 - s->pc));
     2312            genop_1(s, OP_EPOP, s->ensure_level - lp->ensure_level);
     2313          }
     2314          genjmp(s, OP_JMP, lp->pc0);
    21312315        }
    21322316      }
     
    21372321  case NODE_LVAR:
    21382322    if (val) {
    2139       int idx = lv_idx(s, sym(tree));
     2323      int idx = lv_idx(s, nsym(tree));
    21402324
    21412325      if (idx > 0) {
    2142         genop_peep(s, MKOP_AB(OP_MOVE, cursp(), idx), NOVAL);
     2326        gen_move(s, cursp(), idx, val);
     2327        if (val && on_eval(s)) genop_0(s, OP_NOP);
    21432328      }
    21442329      else {
     
    21472332
    21482333        while (up) {
    2149           idx = lv_idx(up, sym(tree));
     2334          idx = lv_idx(up, nsym(tree));
    21502335          if (idx > 0) {
    2151             genop(s, MKOP_ABC(OP_GETUPVAR, cursp(), idx, lv));
     2336            genop_3(s, OP_GETUPVAR, cursp(), idx, lv);
    21522337            break;
    21532338          }
     
    21602345    break;
    21612346
     2347  case NODE_NVAR:
     2348    if (val) {
     2349      int idx = nint(tree);
     2350
     2351      gen_move(s, cursp(), idx, val);
     2352      if (val && on_eval(s)) genop_0(s, OP_NOP);
     2353
     2354      push();
     2355    }
     2356    break;
     2357
    21622358  case NODE_GVAR:
    2163     if (val) {
    2164       int sym = new_sym(s, sym(tree));
    2165 
    2166       genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
    2167       push();
     2359    {
     2360      int sym = new_sym(s, nsym(tree));
     2361
     2362      genop_2(s, OP_GETGV, cursp(), sym);
     2363      if (val) push();
    21682364    }
    21692365    break;
    21702366
    21712367  case NODE_IVAR:
    2172     if (val) {
    2173       int sym = new_sym(s, sym(tree));
    2174 
    2175       genop(s, MKOP_ABx(OP_GETIV, cursp(), sym));
    2176       push();
     2368    {
     2369      int sym = new_sym(s, nsym(tree));
     2370
     2371      genop_2(s, OP_GETIV, cursp(), sym);
     2372      if (val) push();
    21772373    }
    21782374    break;
    21792375
    21802376  case NODE_CVAR:
    2181     if (val) {
    2182       int sym = new_sym(s, sym(tree));
    2183 
    2184       genop(s, MKOP_ABx(OP_GETCV, cursp(), sym));
    2185       push();
     2377    {
     2378      int sym = new_sym(s, nsym(tree));
     2379
     2380      genop_2(s, OP_GETCV, cursp(), sym);
     2381      if (val) push();
    21862382    }
    21872383    break;
     
    21892385  case NODE_CONST:
    21902386    {
    2191       int sym = new_sym(s, sym(tree));
    2192 
    2193       genop(s, MKOP_ABx(OP_GETCONST, cursp(), sym));
    2194       if (val) {
    2195         push();
    2196       }
    2197     }
    2198     break;
    2199 
    2200   case NODE_DEFINED:
    2201     codegen(s, tree, VAL);
     2387      int sym = new_sym(s, nsym(tree));
     2388
     2389      genop_2(s, OP_GETCONST, cursp(), sym);
     2390      if (val) push();
     2391    }
    22022392    break;
    22032393
    22042394  case NODE_BACK_REF:
    22052395    if (val) {
    2206       char buf[3];
    2207       int sym;
    2208 
    2209       buf[0] = '$';
    2210       buf[1] = (char)(intptr_t)tree;
    2211       buf[2] = 0;
    2212       sym = new_sym(s, mrb_intern_cstr(s->mrb, buf));
    2213       genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
     2396      char buf[] = {'$', nchar(tree)};
     2397      int sym = new_sym(s, mrb_intern(s->mrb, buf, sizeof(buf)));
     2398
     2399      genop_2(s, OP_GETGV, cursp(), sym);
    22142400      push();
    22152401    }
     
    22222408      int sym;
    22232409
    2224       str = mrb_format(mrb, "$%S", mrb_fixnum_value((mrb_int)(intptr_t)tree));
     2410      str = mrb_format(mrb, "$%d", nint(tree));
    22252411      sym = new_sym(s, mrb_intern_str(mrb, str));
    2226       genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
     2412      genop_2(s, OP_GETGV, cursp(), sym);
    22272413      push();
    22282414    }
     
    22342420
    22352421  case NODE_BLOCK_ARG:
    2236     codegen(s, tree, VAL);
     2422    codegen(s, tree, val);
    22372423    break;
    22382424
     
    22402426    if (val) {
    22412427      char *p = (char*)tree->car;
    2242       int base = (intptr_t)tree->cdr->car;
     2428      int base = nint(tree->cdr->car);
    22432429      mrb_int i;
    2244       mrb_code co;
    22452430      mrb_bool overflow;
    22462431
    22472432      i = readint_mrb_int(s, p, base, FALSE, &overflow);
     2433#ifndef MRB_WITHOUT_FLOAT
    22482434      if (overflow) {
    22492435        double f = readint_float(s, p, base);
    22502436        int off = new_lit(s, mrb_float_value(s->mrb, f));
    22512437
    2252         genop(s, MKOP_ABx(OP_LOADL, cursp(), off));
    2253       }
    2254       else {
    2255         if (i < MAXARG_sBx && i > -MAXARG_sBx) {
    2256           co = MKOP_AsBx(OP_LOADI, cursp(), i);
    2257         }
     2438        genop_2(s, OP_LOADL, cursp(), off);
     2439      }
     2440      else
     2441#endif
     2442      {
     2443        if (i == -1) genop_1(s, OP_LOADI__1, cursp());
     2444        else if (i < 0) genop_2(s, OP_LOADINEG, cursp(), (uint16_t)-i);
     2445        else if (i < 8) genop_1(s, OP_LOADI_0 + (uint8_t)i, cursp());
     2446        else if (i <= 0xffff) genop_2(s, OP_LOADI, cursp(), (uint16_t)i);
    22582447        else {
    22592448          int off = new_lit(s, mrb_fixnum_value(i));
    2260           co = MKOP_ABx(OP_LOADL, cursp(), off);
    2261         }
    2262         genop(s, co);
     2449          genop_2(s, OP_LOADL, cursp(), off);
     2450        }
    22632451      }
    22642452      push();
     
    22662454    break;
    22672455
     2456#ifndef MRB_WITHOUT_FLOAT
    22682457  case NODE_FLOAT:
    22692458    if (val) {
     
    22722461      int off = new_lit(s, mrb_float_value(s->mrb, f));
    22732462
    2274       genop(s, MKOP_ABx(OP_LOADL, cursp(), off));
     2463      genop_2(s, OP_LOADL, cursp(), off);
    22752464      push();
    22762465    }
    22772466    break;
     2467#endif
    22782468
    22792469  case NODE_NEGATE:
    22802470    {
    2281       nt = (intptr_t)tree->car;
    2282       tree = tree->cdr;
     2471      nt = nint(tree->car);
    22832472      switch (nt) {
     2473#ifndef MRB_WITHOUT_FLOAT
    22842474      case NODE_FLOAT:
    22852475        if (val) {
    2286           char *p = (char*)tree;
     2476          char *p = (char*)tree->cdr;
    22872477          mrb_float f = mrb_float_read(p, NULL);
    22882478          int off = new_lit(s, mrb_float_value(s->mrb, -f));
    22892479
    2290           genop(s, MKOP_ABx(OP_LOADL, cursp(), off));
     2480          genop_2(s, OP_LOADL, cursp(), off);
    22912481          push();
    22922482        }
    22932483        break;
     2484#endif
    22942485
    22952486      case NODE_INT:
    22962487        if (val) {
    2297           char *p = (char*)tree->car;
    2298           int base = (intptr_t)tree->cdr->car;
     2488          char *p = (char*)tree->cdr->car;
     2489          int base = nint(tree->cdr->cdr->car);
    22992490          mrb_int i;
    2300           mrb_code co;
    23012491          mrb_bool overflow;
    23022492
    23032493          i = readint_mrb_int(s, p, base, TRUE, &overflow);
     2494#ifndef MRB_WITHOUT_FLOAT
    23042495          if (overflow) {
    23052496            double f = readint_float(s, p, base);
    23062497            int off = new_lit(s, mrb_float_value(s->mrb, -f));
    23072498
    2308             genop(s, MKOP_ABx(OP_LOADL, cursp(), off));
     2499            genop_2(s, OP_LOADL, cursp(), off);
    23092500          }
    23102501          else {
    2311             if (i < MAXARG_sBx && i > -MAXARG_sBx) {
    2312               co = MKOP_AsBx(OP_LOADI, cursp(), i);
     2502#endif
     2503            if (i == -1) genop_1(s, OP_LOADI__1, cursp());
     2504            else if (i >= -0xffff) {
     2505              genop_2(s, OP_LOADINEG, cursp(), (uint16_t)-i);
    23132506            }
    23142507            else {
    23152508              int off = new_lit(s, mrb_fixnum_value(i));
    2316               co = MKOP_ABx(OP_LOADL, cursp(), off);
     2509              genop_2(s, OP_LOADL, cursp(), off);
    23172510            }
    2318             genop(s, co);
    2319           }
     2511#ifndef MRB_WITHOUT_FLOAT
     2512          }
     2513#endif
    23202514          push();
    23212515        }
     
    23242518      default:
    23252519        if (val) {
    2326           int sym = new_msym(s, mrb_intern_lit(s->mrb, "-"));
    2327 
    2328           genop(s, MKOP_ABx(OP_LOADI, cursp(), 0));
     2520          int sym = new_sym(s, mrb_intern_lit(s->mrb, "-@"));
     2521          codegen(s, tree, VAL);
     2522          pop();
     2523          genop_3(s, OP_SEND, cursp(), sym, 0);
    23292524          push();
    2330           codegen(s, tree, VAL);
    2331           pop(); pop();
    2332           genop(s, MKOP_ABC(OP_SUB, cursp(), sym, 2));
    23332525        }
    23342526        else {
     
    23482540
    23492541      mrb_gc_arena_restore(s->mrb, ai);
    2350       genop(s, MKOP_ABx(OP_STRING, cursp(), off));
     2542      genop_2(s, OP_STRING, cursp(), off);
    23512543      push();
    23522544    }
     
    23612553
    23622554      if (!n) {
    2363         genop(s, MKOP_A(OP_LOADNIL, cursp()));
     2555        genop_1(s, OP_LOADNIL, cursp());
    23642556        push();
    23652557        break;
     
    23702562        codegen(s, n->car, VAL);
    23712563        pop(); pop();
    2372         genop_peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL);
     2564        genop_1(s, OP_STRCAT, cursp());
    23732565        push();
    23742566        n = n->cdr;
     
    23792571
    23802572      while (n) {
    2381         if ((intptr_t)n->car->car != NODE_STR) {
     2573        if (nint(n->car->car) != NODE_STR) {
    23822574          codegen(s, n->car, NOVAL);
    23832575        }
     
    24012593      int sym = new_sym(s, mrb_intern_lit(s->mrb, "Kernel"));
    24022594
    2403       genop(s, MKOP_A(OP_LOADSELF, cursp()));
     2595      genop_1(s, OP_LOADSELF, cursp());
    24042596      push();
    24052597      codegen(s, tree->car, VAL);
    24062598      n = tree->cdr;
    24072599      while (n) {
    2408         if ((intptr_t)n->car->car == NODE_XSTR) {
     2600        if (nint(n->car->car) == NODE_XSTR) {
    24092601          n->car->car = (struct mrb_ast_node*)(intptr_t)NODE_STR;
    24102602          mrb_assert(!n->cdr); /* must be the end */
     
    24122604        codegen(s, n->car, VAL);
    24132605        pop(); pop();
    2414         genop_peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL);
     2606        genop_1(s, OP_STRCAT, cursp());
    24152607        push();
    24162608        n = n->cdr;
     
    24192611      pop_n(3);
    24202612      sym = new_sym(s, mrb_intern_lit(s->mrb, "`"));
    2421       genop(s, MKOP_ABC(OP_SEND, cursp(), sym, 1));
     2613      genop_3(s, OP_SEND, cursp(), sym, 1);
    24222614      if (val) push();
    24232615      mrb_gc_arena_restore(s->mrb, ai);
     
    24332625      int sym;
    24342626
    2435       genop(s, MKOP_A(OP_LOADSELF, cursp()));
     2627      genop_1(s, OP_LOADSELF, cursp());
    24362628      push();
    2437       genop(s, MKOP_ABx(OP_STRING, cursp(), off));
     2629      genop_2(s, OP_STRING, cursp(), off);
    24382630      push(); push();
    24392631      pop_n(3);
    24402632      sym = new_sym(s, mrb_intern_lit(s->mrb, "`"));
    2441       genop(s, MKOP_ABC(OP_SEND, cursp(), sym, 1));
     2633      genop_3(s, OP_SEND, cursp(), sym, 1);
    24422634      if (val) push();
    24432635      mrb_gc_arena_restore(s->mrb, ai);
     
    24552647      int argc = 1;
    24562648
    2457       genop(s, MKOP_A(OP_OCLASS, cursp()));
    2458       genop(s, MKOP_ABx(OP_GETMCNST, cursp(), sym));
     2649      genop_1(s, OP_OCLASS, cursp());
     2650      genop_2(s, OP_GETMCNST, cursp(), sym);
    24592651      push();
    2460       genop(s, MKOP_ABx(OP_STRING, cursp(), off));
     2652      genop_2(s, OP_STRING, cursp(), off);
     2653      push();
    24612654      if (p2 || p3) {
     2655        if (p2) { /* opt */
     2656          off = new_lit(s, mrb_str_new_cstr(s->mrb, p2));
     2657          genop_2(s, OP_STRING, cursp(), off);
     2658        }
     2659        else {
     2660          genop_1(s, OP_LOADNIL, cursp());
     2661        }
    24622662        push();
    2463         if (p2) {
    2464           off = new_lit(s, mrb_str_new_cstr(s->mrb, p2));
    2465           genop(s, MKOP_ABx(OP_STRING, cursp(), off));
    2466         }
    2467         else {
    2468           genop(s, MKOP_A(OP_LOADNIL, cursp()));
    2469         }
    24702663        argc++;
    2471         if (p3) {
     2664        if (p3) { /* enc */
     2665          off = new_lit(s, mrb_str_new(s->mrb, p3, 1));
     2666          genop_2(s, OP_STRING, cursp(), off);
    24722667          push();
    2473           off = new_lit(s, mrb_str_new(s->mrb, p3, 1));
    2474           genop(s, MKOP_ABx(OP_STRING, cursp(), off));
    24752668          argc++;
    2476           pop();
    2477         }
    2478         pop();
    2479       }
    2480       pop();
     2669        }
     2670      }
     2671      push(); /* space for a block */
     2672      pop_n(argc+2);
    24812673      sym = new_sym(s, mrb_intern_lit(s->mrb, "compile"));
    2482       genop(s, MKOP_ABC(OP_SEND, cursp(), sym, argc));
     2674      genop_3(s, OP_SEND, cursp(), sym, argc);
    24832675      mrb_gc_arena_restore(s->mrb, ai);
    24842676      push();
     
    24952687      char *p;
    24962688
    2497       genop(s, MKOP_A(OP_OCLASS, cursp()));
    2498       genop(s, MKOP_ABx(OP_GETMCNST, cursp(), sym));
     2689      genop_1(s, OP_OCLASS, cursp());
     2690      genop_2(s, OP_GETMCNST, cursp(), sym);
    24992691      push();
    25002692      codegen(s, n->car, VAL);
     
    25032695        codegen(s, n->car, VAL);
    25042696        pop(); pop();
    2505         genop_peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL);
     2697        genop_1(s, OP_STRCAT, cursp());
    25062698        push();
    25072699        n = n->cdr;
    25082700      }
    25092701      n = tree->cdr->cdr;
    2510       if (n->car) {
     2702      if (n->car) { /* tail */
    25112703        p = (char*)n->car;
    25122704        off = new_lit(s, mrb_str_new_cstr(s->mrb, p));
    25132705        codegen(s, tree->car, VAL);
    2514         genop(s, MKOP_ABx(OP_STRING, cursp(), off));
     2706        genop_2(s, OP_STRING, cursp(), off);
    25152707        pop();
    2516         genop_peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL);
    2517       }
    2518       if (n->cdr->car) {
     2708        genop_1(s, OP_STRCAT, cursp());
     2709        push();
     2710      }
     2711      if (n->cdr->car) { /* opt */
    25192712        char *p2 = (char*)n->cdr->car;
    2520 
     2713        off = new_lit(s, mrb_str_new_cstr(s->mrb, p2));
     2714        genop_2(s, OP_STRING, cursp(), off);
    25212715        push();
     2716        argc++;
     2717      }
     2718      if (n->cdr->cdr) { /* enc */
     2719        char *p2 = (char*)n->cdr->cdr;
    25222720        off = new_lit(s, mrb_str_new_cstr(s->mrb, p2));
    2523         genop(s, MKOP_ABx(OP_STRING, cursp(), off));
     2721        genop_2(s, OP_STRING, cursp(), off);
     2722        push();
    25242723        argc++;
    25252724      }
    2526       if (n->cdr->cdr) {
    2527         char *p2 = (char*)n->cdr->cdr;
    2528 
    2529         push();
    2530         off = new_lit(s, mrb_str_new_cstr(s->mrb, p2));
    2531         genop(s, MKOP_ABx(OP_STRING, cursp(), off));
    2532         argc++;
    2533       }
    2534       pop_n(argc);
     2725      push(); /* space for a block */
     2726      pop_n(argc+2);
    25352727      sym = new_sym(s, mrb_intern_lit(s->mrb, "compile"));
    2536       genop(s, MKOP_ABC(OP_SEND, cursp(), sym, argc));
     2728      genop_3(s, OP_SEND, cursp(), sym, argc);
    25372729      mrb_gc_arena_restore(s->mrb, ai);
    25382730      push();
     
    25422734
    25432735      while (n) {
    2544         if ((intptr_t)n->car->car != NODE_STR) {
     2736        if (nint(n->car->car) != NODE_STR) {
    25452737          codegen(s, n->car, NOVAL);
    25462738        }
     
    25522744  case NODE_SYM:
    25532745    if (val) {
    2554       int sym = new_sym(s, sym(tree));
    2555 
    2556       genop(s, MKOP_ABx(OP_LOADSYM, cursp(), sym));
     2746      int sym = new_sym(s, nsym(tree));
     2747
     2748      genop_2(s, OP_LOADSYM, cursp(), sym);
    25572749      push();
    25582750    }
     
    25622754    codegen(s, tree, val);
    25632755    if (val) {
    2564       gen_send_intern(s);
     2756      gen_intern(s);
    25652757    }
    25662758    break;
     
    25682760  case NODE_SELF:
    25692761    if (val) {
    2570       genop(s, MKOP_A(OP_LOADSELF, cursp()));
     2762      genop_1(s, OP_LOADSELF, cursp());
    25712763      push();
    25722764    }
     
    25752767  case NODE_NIL:
    25762768    if (val) {
    2577       genop(s, MKOP_A(OP_LOADNIL, cursp()));
     2769      genop_1(s, OP_LOADNIL, cursp());
    25782770      push();
    25792771    }
     
    25822774  case NODE_TRUE:
    25832775    if (val) {
    2584       genop(s, MKOP_A(OP_LOADT, cursp()));
     2776      genop_1(s, OP_LOADT, cursp());
    25852777      push();
    25862778    }
     
    25892781  case NODE_FALSE:
    25902782    if (val) {
    2591       genop(s, MKOP_A(OP_LOADF, cursp()));
     2783      genop_1(s, OP_LOADF, cursp());
    25922784      push();
    25932785    }
     
    25962788  case NODE_ALIAS:
    25972789    {
    2598       int a = new_msym(s, sym(tree->car));
    2599       int b = new_msym(s, sym(tree->cdr));
    2600       int c = new_msym(s, mrb_intern_lit(s->mrb, "alias_method"));
    2601 
    2602       genop(s, MKOP_A(OP_TCLASS, cursp()));
    2603       push();
    2604       genop(s, MKOP_ABx(OP_LOADSYM, cursp(), a));
    2605       push();
    2606       genop(s, MKOP_ABx(OP_LOADSYM, cursp(), b));
    2607       push();
    2608       genop(s, MKOP_A(OP_LOADNIL, cursp()));
    2609       push();
    2610       pop_n(4);
    2611       genop(s, MKOP_ABC(OP_SEND, cursp(), c, 2));
     2790      int a = new_sym(s, nsym(tree->car));
     2791      int b = new_sym(s, nsym(tree->cdr));
     2792
     2793      genop_2(s, OP_ALIAS, a, b);
    26122794      if (val) {
     2795        genop_1(s, OP_LOADNIL, cursp());
    26132796        push();
    26142797      }
     
    26182801  case NODE_UNDEF:
    26192802    {
    2620       int undef = new_msym(s, mrb_intern_lit(s->mrb, "undef_method"));
    2621       int num = 0;
    26222803      node *t = tree;
    26232804
    2624       genop(s, MKOP_A(OP_TCLASS, cursp()));
    2625       push();
    26262805      while (t) {
    2627         int symbol;
    2628         if (num >= CALL_MAXARGS - 1) {
    2629           pop_n(num);
    2630           genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), num));
    2631           while (t) {
    2632             symbol = new_msym(s, sym(t->car));
    2633             push();
    2634             genop(s, MKOP_ABx(OP_LOADSYM, cursp(), symbol));
    2635             pop();
    2636             genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));
    2637             t = t->cdr;
    2638           }
    2639           num = CALL_MAXARGS;
    2640           break;
    2641         }
    2642         symbol = new_msym(s, sym(t->car));
    2643         genop(s, MKOP_ABx(OP_LOADSYM, cursp(), symbol));
    2644         push();
     2806        int symbol = new_sym(s, nsym(t->car));
     2807        genop_1(s, OP_UNDEF, symbol);
    26452808        t = t->cdr;
    2646         num++;
    2647       }
    2648       pop();
    2649       if (num < CALL_MAXARGS) {
    2650         pop_n(num);
    2651       }
    2652       genop(s, MKOP_ABC(OP_SEND, cursp(), undef, num));
     2809      }
    26532810      if (val) {
     2811        genop_1(s, OP_LOADNIL, cursp());
    26542812        push();
    26552813      }
     
    26602818    {
    26612819      int idx;
     2820      node *body;
    26622821
    26632822      if (tree->car->car == (node*)0) {
    2664         genop(s, MKOP_A(OP_LOADNIL, cursp()));
     2823        genop_1(s, OP_LOADNIL, cursp());
    26652824        push();
    26662825      }
    26672826      else if (tree->car->car == (node*)1) {
    2668         genop(s, MKOP_A(OP_OCLASS, cursp()));
     2827        genop_1(s, OP_OCLASS, cursp());
    26692828        push();
    26702829      }
     
    26762835      }
    26772836      else {
    2678         genop(s, MKOP_A(OP_LOADNIL, cursp()));
     2837        genop_1(s, OP_LOADNIL, cursp());
    26792838        push();
    26802839      }
    26812840      pop(); pop();
    2682       idx = new_msym(s, sym(tree->car->cdr));
    2683       genop(s, MKOP_AB(OP_CLASS, cursp(), idx));
    2684       idx = scope_body(s, tree->cdr->cdr->car, val);
    2685       genop(s, MKOP_ABx(OP_EXEC, cursp(), idx));
     2841      idx = new_sym(s, nsym(tree->car->cdr));
     2842      genop_2(s, OP_CLASS, cursp(), idx);
     2843      body = tree->cdr->cdr->car;
     2844      if (nint(body->cdr->car) == NODE_BEGIN && body->cdr->cdr == NULL) {
     2845        genop_1(s, OP_LOADNIL, cursp());
     2846      }
     2847      else {
     2848        idx = scope_body(s, body, val);
     2849        genop_2(s, OP_EXEC, cursp(), idx);
     2850      }
    26862851      if (val) {
    26872852        push();
     
    26952860
    26962861      if (tree->car->car == (node*)0) {
    2697         genop(s, MKOP_A(OP_LOADNIL, cursp()));
     2862        genop_1(s, OP_LOADNIL, cursp());
    26982863        push();
    26992864      }
    27002865      else if (tree->car->car == (node*)1) {
    2701         genop(s, MKOP_A(OP_OCLASS, cursp()));
     2866        genop_1(s, OP_OCLASS, cursp());
    27022867        push();
    27032868      }
     
    27062871      }
    27072872      pop();
    2708       idx = new_msym(s, sym(tree->car->cdr));
    2709       genop(s, MKOP_AB(OP_MODULE, cursp(), idx));
    2710       idx = scope_body(s, tree->cdr->car, val);
    2711       genop(s, MKOP_ABx(OP_EXEC, cursp(), idx));
     2873      idx = new_sym(s, nsym(tree->car->cdr));
     2874      genop_2(s, OP_MODULE, cursp(), idx);
     2875      if (nint(tree->cdr->car->cdr->car) == NODE_BEGIN &&
     2876          tree->cdr->car->cdr->cdr == NULL) {
     2877        genop_1(s, OP_LOADNIL, cursp());
     2878      }
     2879      else {
     2880        idx = scope_body(s, tree->cdr->car, val);
     2881        genop_2(s, OP_EXEC, cursp(), idx);
     2882      }
    27122883      if (val) {
    27132884        push();
     
    27222893      codegen(s, tree->car, VAL);
    27232894      pop();
    2724       genop(s, MKOP_AB(OP_SCLASS, cursp(), cursp()));
    2725       idx = scope_body(s, tree->cdr->car, val);
    2726       genop(s, MKOP_ABx(OP_EXEC, cursp(), idx));
     2895      genop_1(s, OP_SCLASS, cursp());
     2896      if (nint(tree->cdr->car->cdr->car) == NODE_BEGIN &&
     2897          tree->cdr->car->cdr->cdr == NULL) {
     2898        genop_1(s, OP_LOADNIL, cursp());
     2899      }
     2900      else {
     2901        idx = scope_body(s, tree->cdr->car, val);
     2902        genop_2(s, OP_EXEC, cursp(), idx);
     2903      }
    27272904      if (val) {
    27282905        push();
     
    27332910  case NODE_DEF:
    27342911    {
    2735       int sym = new_msym(s, sym(tree->car));
     2912      int sym = new_sym(s, nsym(tree->car));
    27362913      int idx = lambda_body(s, tree->cdr, 0);
    27372914
    2738       genop(s, MKOP_A(OP_TCLASS, cursp()));
     2915      genop_1(s, OP_TCLASS, cursp());
    27392916      push();
    2740       genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_METHOD));
     2917      genop_2(s, OP_METHOD, cursp(), idx);
    27412918      push(); pop();
    27422919      pop();
    2743       genop(s, MKOP_AB(OP_METHOD, cursp(), sym));
     2920      genop_2(s, OP_DEF, cursp(), sym);
    27442921      if (val) {
    2745         genop(s, MKOP_ABx(OP_LOADSYM, cursp(), sym));
     2922        genop_2(s, OP_LOADSYM, cursp(), sym);
    27462923        push();
    27472924      }
     
    27522929    {
    27532930      node *recv = tree->car;
    2754       int sym = new_msym(s, sym(tree->cdr->car));
     2931      int sym = new_sym(s, nsym(tree->cdr->car));
    27552932      int idx = lambda_body(s, tree->cdr->cdr, 0);
    27562933
    27572934      codegen(s, recv, VAL);
    27582935      pop();
    2759       genop(s, MKOP_AB(OP_SCLASS, cursp(), cursp()));
     2936      genop_1(s, OP_SCLASS, cursp());
    27602937      push();
    2761       genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_METHOD));
     2938      genop_2(s, OP_METHOD, cursp(), idx);
    27622939      pop();
    2763       genop(s, MKOP_AB(OP_METHOD, cursp(), sym));
     2940      genop_2(s, OP_DEF, cursp(), sym);
    27642941      if (val) {
    2765         genop(s, MKOP_ABx(OP_LOADSYM, cursp(), sym));
     2942        genop_2(s, OP_LOADSYM, cursp(), sym);
    27662943        push();
    27672944      }
     
    28022979  codegen_scope *p = (codegen_scope *)mrb_pool_alloc(pool, sizeof(codegen_scope));
    28032980
    2804   if (!p) return NULL;
     2981  if (!p) {
     2982    if (prev)
     2983      codegen_error(prev, "unexpected scope");
     2984    return NULL;
     2985  }
    28052986  *p = codegen_scope_zero;
    28062987  p->mrb = mrb;
     
    28253006  p->irep->plen = 0;
    28263007
    2827   p->scapa = MAXMSYMLEN;
     3008  p->scapa = 256;
    28283009  p->irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*p->scapa);
    28293010  p->irep->slen = 0;
     
    28503031  p->ai = mrb_gc_arena_save(mrb);
    28513032
    2852   p->filename = prev->filename;
    2853   if (p->filename) {
     3033  p->filename_sym = prev->filename_sym;
     3034  if (p->filename_sym) {
    28543035    p->lines = (uint16_t*)mrb_malloc(mrb, sizeof(short)*p->icapa);
    28553036  }
     
    28583039  /* debug setting */
    28593040  p->debug_start_pos = 0;
    2860   if (p->filename) {
     3041  if (p->filename_sym) {
    28613042    mrb_debug_info_alloc(mrb, p->irep);
    2862     p->irep->filename = p->filename;
    2863     p->irep->lines = p->lines;
    28643043  }
    28653044  else {
     
    28793058  mrb_state *mrb = s->mrb;
    28803059  mrb_irep *irep = s->irep;
    2881   size_t fname_len;
    2882   char *fname;
    2883 
     3060
     3061  if (s->nlocals >= 0x3ff) {
     3062    codegen_error(s, "too many local variables");
     3063  }
    28843064  irep->flags = 0;
    28853065  if (s->iseq) {
    28863066    irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc);
    28873067    irep->ilen = s->pc;
    2888     if (s->lines) {
    2889       irep->lines = (uint16_t *)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->pc);
    2890     }
    2891     else {
    2892       irep->lines = 0;
    2893     }
    28943068  }
    28953069  irep->pool = (mrb_value*)codegen_realloc(s, irep->pool, sizeof(mrb_value)*irep->plen);
    28963070  irep->syms = (mrb_sym*)codegen_realloc(s, irep->syms, sizeof(mrb_sym)*irep->slen);
    28973071  irep->reps = (mrb_irep**)codegen_realloc(s, irep->reps, sizeof(mrb_irep*)*irep->rlen);
    2898   if (s->filename) {
    2899     irep->filename = mrb_parser_get_filename(s->parser, s->filename_index);
    2900     mrb_debug_info_append_file(mrb, irep, s->debug_start_pos, s->pc);
    2901 
    2902     fname_len = strlen(s->filename);
    2903     fname = (char*)codegen_malloc(s, fname_len + 1);
    2904     memcpy(fname, s->filename, fname_len);
    2905     fname[fname_len] = '\0';
    2906     irep->filename = fname;
    2907     irep->own_filename = TRUE;
    2908   }
     3072  if (s->filename_sym) {
     3073    mrb_sym fname = mrb_parser_get_filename(s->parser, s->filename_index);
     3074    const char *filename = mrb_sym_name_len(s->mrb, fname, NULL);
     3075
     3076    mrb_debug_info_append_file(s->mrb, s->irep->debug_info,
     3077                               filename, s->lines, s->debug_start_pos, s->pc);
     3078  }
     3079  mrb_free(s->mrb, s->lines);
    29093080
    29103081  irep->nlocals = s->nlocals;
     
    29213092
    29223093  p->type = t;
    2923   p->pc1 = p->pc2 = p->pc3 = 0;
     3094  p->pc0 = p->pc1 = p->pc2 = p->pc3 = 0;
    29243095  p->prev = s->loop;
    29253096  p->ensure_level = s->ensure_level;
     
    29393110  else {
    29403111    struct loopinfo *loop;
     3112    int n = 0;
    29413113
    29423114    if (tree) {
     
    29453117
    29463118    loop = s->loop;
    2947     while (loop && loop->type == LOOP_BEGIN) {
    2948       genop_peep(s, MKOP_A(OP_POPERR, 1), NOVAL);
    2949       loop = loop->prev;
    2950     }
    2951     while (loop && loop->type == LOOP_RESCUE) {
    2952       loop = loop->prev;
     3119    while (loop) {
     3120      if (loop->type == LOOP_BEGIN) {
     3121        n++;
     3122        loop = loop->prev;
     3123      }
     3124      else if (loop->type == LOOP_RESCUE) {
     3125        loop = loop->prev;
     3126      }
     3127      else{
     3128        break;
     3129      }
    29533130    }
    29543131    if (!loop) {
     
    29563133      return;
    29573134    }
     3135    if (n > 0) {
     3136      genop_1(s, OP_POPERR, n);
     3137    }
    29583138
    29593139    if (loop->type == LOOP_NORMAL) {
     
    29613141
    29623142      if (s->ensure_level > s->loop->ensure_level) {
    2963         genop_peep(s, MKOP_A(OP_EPOP, s->ensure_level - s->loop->ensure_level), NOVAL);
     3143        genop_1(s, OP_EPOP, s->ensure_level - s->loop->ensure_level);
    29643144      }
    29653145      if (tree) {
    2966         genop_peep(s, MKOP_AB(OP_MOVE, loop->acc, cursp()), NOVAL);
    2967       }
    2968       tmp = genop(s, MKOP_sBx(OP_JMP, loop->pc3));
     3146        gen_move(s, loop->acc, cursp(), 0);
     3147      }
     3148      tmp = genjmp(s, OP_JMP, loop->pc3);
    29693149      loop->pc3 = tmp;
    29703150    }
    29713151    else {
    29723152      if (!tree) {
    2973         genop(s, MKOP_A(OP_LOADNIL, cursp()));
    2974       }
    2975       genop(s, MKOP_AB(OP_RETURN, cursp(), OP_R_BREAK));
     3153        genop_1(s, OP_LOADNIL, cursp());
     3154      }
     3155      gen_return(s, OP_BREAK, cursp());
    29763156    }
    29773157  }
     
    29813161loop_pop(codegen_scope *s, int val)
    29823162{
     3163  if (val) {
     3164    genop_1(s, OP_LOADNIL, cursp());
     3165  }
    29833166  dispatch_linked(s, s->loop->pc3);
    2984   if (val) {
    2985     genop(s, MKOP_A(OP_LOADNIL, cursp()));
    2986   }
    29873167  s->loop = s->loop->prev;
    29883168  if (val) push();
    29893169}
    29903170
    2991 MRB_API struct RProc*
    2992 mrb_generate_code(mrb_state *mrb, parser_state *p)
     3171static struct RProc*
     3172generate_code(mrb_state *mrb, parser_state *p, int val)
    29933173{
    29943174  codegen_scope *scope = scope_new(mrb, 0, 0);
     
    29963176  struct mrb_jmpbuf *prev_jmp = mrb->jmp;
    29973177
    2998   if (!scope) {
    2999     return NULL;
    3000   }
    30013178  scope->mrb = mrb;
    30023179  scope->parser = p;
    3003   scope->filename = p->filename;
     3180  scope->filename_sym = p->filename_sym;
    30043181  scope->filename_index = p->current_filename_index;
    30053182
    30063183  MRB_TRY(&scope->jmp) {
    3007     mrb->jmp = &scope->jmp; 
     3184    mrb->jmp = &scope->jmp;
    30083185    /* prepare irep */
    3009     codegen(scope, p->tree, NOVAL);
     3186    codegen(scope, p->tree, val);
    30103187    proc = mrb_proc_new(mrb, scope->irep);
    30113188    mrb_irep_decref(mrb, scope->irep);
    30123189    mrb_pool_close(scope->mpool);
    30133190    proc->c = NULL;
     3191    if (mrb->c->cibase && mrb->c->cibase->proc == proc->upper) {
     3192      proc->upper = NULL;
     3193    }
    30143194    mrb->jmp = prev_jmp;
    30153195    return proc;
     
    30233203  MRB_END_EXC(&scope->jmp);
    30243204}
     3205
     3206MRB_API struct RProc*
     3207mrb_generate_code(mrb_state *mrb, parser_state *p)
     3208{
     3209  return generate_code(mrb, p, VAL);
     3210}
     3211
     3212void
     3213mrb_irep_remove_lv(mrb_state *mrb, mrb_irep *irep)
     3214{
     3215  int i;
     3216
     3217  if (irep->lv) {
     3218    mrb_free(mrb, irep->lv);
     3219    irep->lv = NULL;
     3220  }
     3221
     3222  for (i = 0; i < irep->rlen; ++i) {
     3223    mrb_irep_remove_lv(mrb, irep->reps[i]);
     3224  }
     3225}
     3226
     3227#undef OPCODE
     3228#define Z 1
     3229#define S 3
     3230#define W 4
     3231/* instruction sizes */
     3232uint8_t mrb_insn_size[] = {
     3233#define B 2
     3234#define BB 3
     3235#define BBB 4
     3236#define BS 4
     3237#define SB 4
     3238#define OPCODE(_,x) x,
     3239#include "mruby/ops.h"
     3240#undef OPCODE
     3241#undef B
     3242#undef BB
     3243#undef BS
     3244#undef SB
     3245#undef BBB
     3246};
     3247/* EXT1 instruction sizes */
     3248uint8_t mrb_insn_size1[] = {
     3249#define B 3
     3250#define BB 4
     3251#define BBB 5
     3252#define BS 5
     3253#define SB 5
     3254#define OPCODE(_,x) x,
     3255#include "mruby/ops.h"
     3256#undef OPCODE
     3257#undef B
     3258};
     3259/* EXT2 instruction sizes */
     3260uint8_t mrb_insn_size2[] = {
     3261#define B 2
     3262#define OPCODE(_,x) x,
     3263#include "mruby/ops.h"
     3264#undef OPCODE
     3265#undef BB
     3266#undef BBB
     3267#undef BS
     3268#undef SB
     3269};
     3270/* EXT3 instruction sizes */
     3271#define BB 5
     3272#define BBB 6
     3273#define BS 4
     3274#define SB 5
     3275uint8_t mrb_insn_size3[] = {
     3276#define OPCODE(_,x) x,
     3277#include "mruby/ops.h"
     3278};
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/keywords

    r270 r439  
    11%{
    22struct kwtable {const char *name; int id[2]; enum mrb_lex_state_enum state;};
    3 const struct kwtable *mrb_reserved_word(const char *, unsigned int);
    4 static const struct kwtable *reserved_word(const char *, unsigned int);
    5 #define mrb_reserved_word(str, len) reserved_word(str, len)
    63%}
    74
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/lex.def

    r331 r439  
    1 /* ANSI-C code produced by gperf version 3.0.4 */
     1/* ANSI-C code produced by gperf version 3.1 */
    22/* Command-line: gperf -L ANSI-C -C -p -j1 -i 1 -g -o -t -N mrb_reserved_word -k'1,3,$' /home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords  */
    33
     
    2626      && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))
    2727/* The character set is not based on ISO-646.  */
    28 #error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gnu-gperf@gnu.org>."
     28#error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gperf@gnu.org>."
    2929#endif
    3030
     
    3232
    3333struct kwtable {const char *name; int id[2]; enum mrb_lex_state_enum state;};
    34 const struct kwtable *mrb_reserved_word(const char *, unsigned int);
    35 static const struct kwtable *reserved_word(const char *, unsigned int);
    36 #define mrb_reserved_word(str, len) reserved_word(str, len)
    37 #line 8 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     34#line 5 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    3835struct kwtable;
    3936
     
    5350#endif
    5451static unsigned int
    55 hash (register const char *str, register unsigned int len)
     52hash (register const char *str, register size_t len)
    5653{
    5754  static const unsigned char asso_values[] =
     
    8481      51, 51, 51, 51, 51, 51
    8582    };
    86   register int hval = len;
     83  register unsigned int hval = len;
    8784
    8885  switch (hval)
     
    9996}
    10097
    101 #ifdef __GNUC__
    102 __inline
    103 #if defined __GNUC_STDC_INLINE__ || defined __GNUC_GNU_INLINE__
    104 __attribute__ ((__gnu_inline__))
    105 #endif
    106 #endif
    10798const struct kwtable *
    108 mrb_reserved_word (register const char *str, register unsigned int len)
     99mrb_reserved_word (register const char *str, register size_t len)
    109100{
    110101  static const struct kwtable wordlist[] =
    111102    {
    112103      {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
     104#line 15 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     105      {"break",        {keyword_break,       keyword_break},       EXPR_MID},
     106#line 20 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     107      {"else",         {keyword_else,        keyword_else},        EXPR_BEG},
     108#line 30 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     109      {"nil",          {keyword_nil,         keyword_nil},         EXPR_END},
     110#line 23 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     111      {"ensure",       {keyword_ensure,      keyword_ensure},      EXPR_BEG},
     112#line 22 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     113      {"end",          {keyword_end,         keyword_end},         EXPR_END},
     114#line 39 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     115      {"then",         {keyword_then,        keyword_then},        EXPR_BEG},
     116#line 31 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     117      {"not",          {keyword_not,         keyword_not},         EXPR_ARG},
     118#line 24 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     119      {"false",        {keyword_false,       keyword_false},       EXPR_END},
     120#line 37 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     121      {"self",         {keyword_self,        keyword_self},        EXPR_END},
     122#line 21 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     123      {"elsif",        {keyword_elsif,       keyword_elsif},       EXPR_VALUE},
     124#line 34 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     125      {"rescue",       {keyword_rescue,      modifier_rescue},     EXPR_MID},
     126#line 40 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     127      {"true",         {keyword_true,        keyword_true},        EXPR_END},
     128#line 43 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     129      {"until",        {keyword_until,       modifier_until},      EXPR_VALUE},
     130#line 42 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     131      {"unless",       {keyword_unless,      modifier_unless},     EXPR_VALUE},
     132#line 36 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     133      {"return",       {keyword_return,      keyword_return},      EXPR_MID},
    113134#line 18 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    114       {"break",        {keyword_break,       keyword_break},       EXPR_MID},
    115 #line 23 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    116       {"else",         {keyword_else,        keyword_else},        EXPR_BEG},
     135      {"def",          {keyword_def,         keyword_def},         EXPR_FNAME},
     136#line 13 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     137      {"and",          {keyword_and,         keyword_and},         EXPR_VALUE},
     138#line 19 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     139      {"do",           {keyword_do,          keyword_do},          EXPR_BEG},
     140#line 46 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     141      {"yield",        {keyword_yield,       keyword_yield},       EXPR_ARG},
     142#line 25 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     143      {"for",          {keyword_for,         keyword_for},         EXPR_VALUE},
     144#line 41 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     145      {"undef",        {keyword_undef,       keyword_undef},       EXPR_FNAME},
     146#line 32 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     147      {"or",           {keyword_or,          keyword_or},          EXPR_VALUE},
     148#line 27 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     149      {"in",           {keyword_in,          keyword_in},          EXPR_VALUE},
     150#line 44 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     151      {"when",         {keyword_when,        keyword_when},        EXPR_VALUE},
     152#line 35 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     153      {"retry",        {keyword_retry,       keyword_retry},       EXPR_END},
     154#line 26 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     155      {"if",           {keyword_if,          modifier_if},         EXPR_VALUE},
     156#line 16 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     157      {"case",         {keyword_case,        keyword_case},        EXPR_VALUE},
    117158#line 33 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    118       {"nil",          {keyword_nil,         keyword_nil},         EXPR_END},
    119 #line 26 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    120       {"ensure",       {keyword_ensure,      keyword_ensure},      EXPR_BEG},
    121 #line 25 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    122       {"end",          {keyword_end,         keyword_end},         EXPR_END},
    123 #line 42 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    124       {"then",         {keyword_then,        keyword_then},        EXPR_BEG},
    125 #line 34 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    126       {"not",          {keyword_not,         keyword_not},         EXPR_ARG},
    127 #line 27 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    128       {"false",        {keyword_false,       keyword_false},       EXPR_END},
    129 #line 40 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    130       {"self",         {keyword_self,        keyword_self},        EXPR_END},
    131 #line 24 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    132       {"elsif",        {keyword_elsif,       keyword_elsif},       EXPR_VALUE},
    133 #line 37 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    134       {"rescue",       {keyword_rescue,      modifier_rescue},     EXPR_MID},
    135 #line 43 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    136       {"true",         {keyword_true,        keyword_true},        EXPR_END},
    137 #line 46 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    138       {"until",        {keyword_until,       modifier_until},      EXPR_VALUE},
    139 #line 45 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    140       {"unless",       {keyword_unless,      modifier_unless},     EXPR_VALUE},
    141 #line 39 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    142       {"return",       {keyword_return,      keyword_return},      EXPR_MID},
    143 #line 21 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    144       {"def",          {keyword_def,         keyword_def},         EXPR_FNAME},
    145 #line 16 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    146       {"and",          {keyword_and,         keyword_and},         EXPR_VALUE},
    147 #line 22 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    148       {"do",           {keyword_do,          keyword_do},          EXPR_BEG},
    149 #line 49 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    150       {"yield",        {keyword_yield,       keyword_yield},       EXPR_ARG},
     159      {"redo",         {keyword_redo,        keyword_redo},        EXPR_END},
     160#line 29 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     161      {"next",         {keyword_next,        keyword_next},        EXPR_MID},
     162#line 38 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     163      {"super",        {keyword_super,       keyword_super},       EXPR_ARG},
    151164#line 28 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    152       {"for",          {keyword_for,         keyword_for},         EXPR_VALUE},
    153 #line 44 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    154       {"undef",        {keyword_undef,       keyword_undef},       EXPR_FNAME},
    155 #line 35 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    156       {"or",           {keyword_or,          keyword_or},          EXPR_VALUE},
    157 #line 30 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    158       {"in",           {keyword_in,          keyword_in},          EXPR_VALUE},
    159 #line 47 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    160       {"when",         {keyword_when,        keyword_when},        EXPR_VALUE},
    161 #line 38 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    162       {"retry",        {keyword_retry,       keyword_retry},       EXPR_END},
    163 #line 29 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    164       {"if",           {keyword_if,          modifier_if},         EXPR_VALUE},
    165 #line 19 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    166       {"case",         {keyword_case,        keyword_case},        EXPR_VALUE},
    167 #line 36 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    168       {"redo",         {keyword_redo,        keyword_redo},        EXPR_END},
    169 #line 32 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    170       {"next",         {keyword_next,        keyword_next},        EXPR_MID},
    171 #line 41 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    172       {"super",        {keyword_super,       keyword_super},       EXPR_ARG},
    173 #line 31 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    174165      {"module",       {keyword_module,      keyword_module},      EXPR_VALUE},
    175 #line 17 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     166#line 14 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    176167      {"begin",        {keyword_begin,       keyword_begin},       EXPR_BEG},
     168#line 9 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     169      {"__LINE__",     {keyword__LINE__,     keyword__LINE__},     EXPR_END},
     170#line 8 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     171      {"__FILE__",     {keyword__FILE__,     keyword__FILE__},     EXPR_END},
     172#line 7 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     173      {"__ENCODING__", {keyword__ENCODING__, keyword__ENCODING__}, EXPR_END},
     174#line 11 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     175      {"END",          {keyword_END,         keyword_END},         EXPR_END},
    177176#line 12 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    178       {"__LINE__",     {keyword__LINE__,     keyword__LINE__},     EXPR_END},
    179 #line 11 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    180       {"__FILE__",     {keyword__FILE__,     keyword__FILE__},     EXPR_END},
     177      {"alias",        {keyword_alias,       keyword_alias},       EXPR_FNAME},
    181178#line 10 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    182       {"__ENCODING__", {keyword__ENCODING__, keyword__ENCODING__}, EXPR_END},
    183 #line 14 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    184       {"END",          {keyword_END,         keyword_END},         EXPR_END},
    185 #line 15 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    186       {"alias",        {keyword_alias,       keyword_alias},       EXPR_FNAME},
    187 #line 13 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    188179      {"BEGIN",        {keyword_BEGIN,       keyword_BEGIN},       EXPR_END},
    189180      {""},
    190 #line 20 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     181#line 17 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    191182      {"class",        {keyword_class,       keyword_class},       EXPR_CLASS},
    192183      {""}, {""},
    193 #line 48 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     184#line 45 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    194185      {"while",        {keyword_while,       modifier_while},      EXPR_VALUE}
    195186    };
     
    197188  if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
    198189    {
    199       register int key = hash (str, len);
    200 
    201       if (key <= MAX_HASH_VALUE && key >= 0)
     190      register unsigned int key = hash (str, len);
     191
     192      if (key <= MAX_HASH_VALUE)
    202193        {
    203194          register const char *s = wordlist[key].name;
     
    209200  return 0;
    210201}
    211 #line 50 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
    212 
     202#line 47 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"
     203
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/node.h

    r331 r439  
    1010enum node_type {
    1111  NODE_METHOD,
    12   NODE_FBODY,
    13   NODE_CFUNC,
    1412  NODE_SCOPE,
    1513  NODE_BLOCK,
     
    1715  NODE_CASE,
    1816  NODE_WHEN,
    19   NODE_OPT_N,
    2017  NODE_WHILE,
    2118  NODE_UNTIL,
     
    4138  NODE_SCALL,
    4239  NODE_FCALL,
    43   NODE_VCALL,
    4440  NODE_SUPER,
    4541  NODE_ZSUPER,
     
    4743  NODE_ZARRAY,
    4844  NODE_HASH,
     45  NODE_KW_HASH,
    4946  NODE_RETURN,
    5047  NODE_YIELD,
     
    5552  NODE_CONST,
    5653  NODE_CVAR,
     54  NODE_NVAR,
    5755  NODE_NTH_REF,
    5856  NODE_BACK_REF,
    5957  NODE_MATCH,
    60   NODE_MATCH2,
    61   NODE_MATCH3,
    6258  NODE_INT,
    6359  NODE_FLOAT,
     
    7268  NODE_DREGX,
    7369  NODE_DREGX_ONCE,
    74   NODE_LIST,
    7570  NODE_ARG,
    76   NODE_ARGSCAT,
    77   NODE_ARGSPUSH,
     71  NODE_ARGS_TAIL,
     72  NODE_KW_ARG,
     73  NODE_KW_REST_ARGS,
    7874  NODE_SPLAT,
    7975  NODE_TO_ARY,
     
    8985  NODE_COLON2,
    9086  NODE_COLON3,
    91   NODE_CREF,
    9287  NODE_DOT2,
    9388  NODE_DOT3,
    94   NODE_FLIP2,
    95   NODE_FLIP3,
    96   NODE_ATTRSET,
    9789  NODE_SELF,
    9890  NODE_NIL,
     
    10092  NODE_FALSE,
    10193  NODE_DEFINED,
    102   NODE_NEWLINE,
    10394  NODE_POSTEXE,
    104   NODE_ALLOCA,
    105   NODE_DMETHOD,
    106   NODE_BMETHOD,
    107   NODE_MEMO,
    108   NODE_IFUNC,
    10995  NODE_DSYM,
    110   NODE_ATTRASGN,
    11196  NODE_HEREDOC,
    11297  NODE_LITERAL_DELIM,
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/parse.y

    r331 r439  
    1111#endif
    1212#define YYERROR_VERBOSE 1
    13 /*
    14  * Force yacc to use our memory management.  This is a little evil because
    15  * the macros assume that "parser_state *p" is in scope
    16  */
    17 #define YYMALLOC(n)    mrb_malloc(p->mrb, (n))
    18 #define YYFREE(o)      mrb_free(p->mrb, (o))
    19 #define YYSTACK_USE_ALLOCA 0
     13#define YYSTACK_USE_ALLOCA 1
    2014
    2115#include <ctype.h>
     
    2822#include <mruby/error.h>
    2923#include <mruby/throw.h>
     24#include <mruby/string.h>
    3025#include "node.h"
    3126
     
    7772#define intn(x) ((int)(intptr_t)(x))
    7873
     74#define NUM_SUFFIX_R   (1<<0)
     75#define NUM_SUFFIX_I   (1<<1)
     76
    7977static inline mrb_sym
    8078intern_cstr_gen(parser_state *p, const char *s)
     
    9189#define intern(s,len) intern_gen(p,(s),(len))
    9290
    93 static inline mrb_sym
    94 intern_gen_c(parser_state *p, const char c)
    95 {
    96   return mrb_intern(p->mrb, &c, 1);
    97 }
    98 #define intern_c(c) intern_gen_c(p,(c))
     91#define intern_lit(s) mrb_intern_lit(p->mrb, s)
    9992
    10093static void
     
    134127  c->lineno = p->lineno;
    135128  c->filename_index = p->current_filename_index;
     129  /* beginning of next partial file; need to point the previous file */
     130  if (p->lineno == 0 && p->current_filename_index > 0) {
     131    c->filename_index-- ;
     132  }
    136133  return c;
    137134}
     
    186183
    187184  if (!a) return b;
     185  if (!b) return a;
    188186  while (c->cdr) {
    189187    c = c->cdr;
    190188  }
    191   if (b) {
    192     c->cdr = b;
    193   }
     189  c->cdr = b;
    194190  return a;
    195191}
     
    216212#undef strdup
    217213#define strdup(s) parser_strdup(p, s)
     214
     215static void
     216dump_int(uint16_t i, char *s)
     217{
     218  char *p = s;
     219  char *t = s;
     220
     221  while (i > 0) {
     222    *p++ = (i % 10)+'0';
     223    i /= 10;
     224  }
     225  if (p == s) *p++ = '0';
     226  *p = 0;
     227  p--;  /* point the last char */
     228  while (t < p) {
     229    char c = *t;
     230    *t++ = *p;
     231    *p-- = c;
     232  }
     233}
    218234
    219235/* xxx ----------------------------- */
     
    280296}
    281297
     298static void
     299local_add_blk(parser_state *p, mrb_sym blk)
     300{
     301  /* allocate register for block */
     302  local_add_f(p, blk ? blk : mrb_intern_lit(p->mrb, "&"));
     303}
     304
     305static void
     306local_add_kw(parser_state *p, mrb_sym kwd)
     307{
     308  /* allocate register for keywords hash */
     309  local_add_f(p, kwd ? kwd : mrb_intern_lit(p->mrb, "**"));
     310}
     311
    282312static node*
    283313locals_node(parser_state *p)
    284314{
    285315  return p->locals ? p->locals->car : NULL;
     316}
     317
     318static void
     319nvars_nest(parser_state *p)
     320{
     321  p->nvars = cons(nint(0), p->nvars);
     322}
     323
     324static void
     325nvars_block(parser_state *p)
     326{
     327  p->nvars = cons(nint(-2), p->nvars);
     328}
     329
     330static void
     331nvars_unnest(parser_state *p)
     332{
     333  p->nvars = p->nvars->cdr;
    286334}
    287335
     
    569617}
    570618
     619/* (:kw_hash (k . v) (k . v)...) */
     620static node*
     621new_kw_hash(parser_state *p, node *a)
     622{
     623  return cons((node*)NODE_KW_HASH, a);
     624}
     625
    571626/* (:sym . a) */
    572627static node*
     
    613668}
    614669
     670/* (:nvar . a) */
     671static node*
     672new_nvar(parser_state *p, int num)
     673{
     674  int nvars = intn(p->nvars->car);
     675
     676  p->nvars->car = nint(nvars > num ? nvars : num);
     677  return cons((node*)NODE_NVAR, nint(num));
     678}
     679
    615680/* (:const . a) */
    616681static node*
     
    672737}
    673738
    674 /* (m o r m2 b) */
     739static void
     740local_add_margs(parser_state *p, node *n)
     741{
     742  while (n) {
     743    if (n->car->car == (node*)NODE_MASGN) {
     744      node *t = n->car->cdr->cdr;
     745
     746      n->car->cdr->cdr = NULL;
     747      while (t) {
     748        local_add_f(p, sym(t->car));
     749        t = t->cdr;
     750      }
     751      local_add_margs(p, n->car->cdr->car->car);
     752      local_add_margs(p, n->car->cdr->car->cdr->cdr->car);
     753    }
     754    n = n->cdr;
     755  }
     756}
     757
     758static void
     759local_add_lv(parser_state *p, node *lv)
     760{
     761  while (lv) {
     762    local_add_f(p, sym(lv->car));
     763    lv = lv->cdr;
     764  }
     765}
     766
     767/* (m o r m2 tail) */
    675768/* m: (a b c) */
    676769/* o: ((a . e1) (b . e2)) */
     
    679772/* b: a */
    680773static node*
    681 new_args(parser_state *p, node *m, node *opt, mrb_sym rest, node *m2, mrb_sym blk)
     774new_args(parser_state *p, node *m, node *opt, mrb_sym rest, node *m2, node *tail)
    682775{
    683776  node *n;
    684777
    685   n = cons(m2, nsym(blk));
     778  local_add_margs(p, m);
     779  local_add_margs(p, m2);
     780  n = cons(m2, tail);
    686781  n = cons(nsym(rest), n);
    687782  n = cons(opt, n);
     783  while (opt) {
     784    /* opt: (sym . (opt . lv)) -> (sym . opt) */
     785    local_add_lv(p, opt->car->cdr->cdr);
     786    opt->car->cdr = opt->car->cdr->car;
     787    opt = opt->cdr;
     788  }
    688789  return cons(m, n);
    689790}
    690791
     792/* (:args_tail keywords rest_keywords_sym block_sym) */
     793static node*
     794new_args_tail(parser_state *p, node *kws, node *kwrest, mrb_sym blk)
     795{
     796  node *k;
     797
     798  if (kws || kwrest) {
     799    local_add_kw(p, (kwrest && kwrest->cdr)? sym(kwrest->cdr) : 0);
     800  }
     801
     802  local_add_blk(p, blk);
     803
     804  /* allocate register for keywords arguments */
     805  /* order is for Proc#parameters */
     806  for (k = kws; k; k = k->cdr) {
     807    if (!k->car->cdr->cdr->car) { /* allocate required keywords */
     808      local_add_f(p, sym(k->car->cdr->car));
     809    }
     810  }
     811  for (k = kws; k; k = k->cdr) {
     812    if (k->car->cdr->cdr->car) { /* allocate keywords with default */
     813      local_add_lv(p, k->car->cdr->cdr->car->cdr);
     814      k->car->cdr->cdr->car = k->car->cdr->cdr->car->car;
     815      local_add_f(p, sym(k->car->cdr->car));
     816    }
     817  }
     818
     819  return list4((node*)NODE_ARGS_TAIL, kws, kwrest, nsym(blk));
     820}
     821
     822/* (:kw_arg kw_sym def_arg) */
     823static node*
     824new_kw_arg(parser_state *p, mrb_sym kw, node *def_arg)
     825{
     826  mrb_assert(kw);
     827  return list3((node*)NODE_KW_ARG, nsym(kw), def_arg);
     828}
     829
    691830/* (:block_arg . a) */
    692831static node*
     
    696835}
    697836
     837static node*
     838setup_numparams(parser_state *p, node *a)
     839{
     840  int nvars = intn(p->nvars->car);
     841  if (nvars > 0) {
     842    int i;
     843    mrb_sym sym;
     844    // m || opt || rest || tail
     845    if (a && (a->car || (a->cdr && a->cdr->car) || (a->cdr->cdr && a->cdr->cdr->car) || (a->cdr->cdr->cdr->cdr && a->cdr->cdr->cdr->cdr->car))) {
     846      yyerror(p, "ordinary parameter is defined");
     847    }
     848    else if (p->locals) {
     849      /* p->locals should not be NULL unless error happens before the point */
     850      node* args = 0;
     851      for (i = nvars; i > 0; i--) {
     852        char buf[3];
     853
     854        buf[0] = '_';
     855        buf[1] = i+'0';
     856        buf[2] = '\0';
     857        sym = intern_cstr(buf);
     858        args = cons(new_arg(p, sym), args);
     859        p->locals->car = cons(nsym(sym), p->locals->car);
     860      }
     861      a = new_args(p, args, 0, 0, 0, 0);
     862    }
     863  }
     864  return a;
     865}
     866
    698867/* (:block arg body) */
    699868static node*
    700869new_block(parser_state *p, node *a, node *b)
    701870{
     871  a = setup_numparams(p, a);
    702872  return list4((node*)NODE_BLOCK, locals_node(p), a, b);
    703873}
     
    726896}
    727897
     898/* (:masgn mlhs mrhs) no check */
     899static node*
     900new_masgn_param(parser_state *p, node *a, node *b)
     901{
     902  return cons((node*)NODE_MASGN, cons(a, b));
     903}
     904
    728905/* (:asgn lhs rhs) */
    729906static node*
     
    734911}
    735912
     913static node*
     914new_imaginary(parser_state *p, node *imaginary)
     915{
     916  return new_call(p, new_const(p, intern_lit("Kernel")), intern_lit("Complex"), list1(list2(list3((node*)NODE_INT, (node*)strdup("0"), nint(10)), imaginary)), 1);
     917}
     918
     919static node*
     920new_rational(parser_state *p, node *rational)
     921{
     922  return new_call(p, new_const(p, intern_lit("Kernel")), intern_lit("Rational"), list1(list1(rational)), 1);
     923}
     924
    736925/* (:int . i) */
    737926static node*
    738 new_int(parser_state *p, const char *s, int base)
    739 {
    740   return list3((node*)NODE_INT, (node*)strdup(s), nint(base));
    741 }
    742 
     927new_int(parser_state *p, const char *s, int base, int suffix)
     928{
     929  node* result = list3((node*)NODE_INT, (node*)strdup(s), nint(base));
     930  if (suffix & NUM_SUFFIX_R) {
     931    result = new_rational(p, result);
     932  }
     933  if (suffix & NUM_SUFFIX_I) {
     934    result = new_imaginary(p, result);
     935  }
     936  return result;
     937}
     938
     939#ifndef MRB_WITHOUT_FLOAT
    743940/* (:float . i) */
    744941static node*
    745 new_float(parser_state *p, const char *s)
    746 {
    747   return cons((node*)NODE_FLOAT, (node*)strdup(s));
    748 }
     942new_float(parser_state *p, const char *s, int suffix)
     943{
     944  node* result = cons((node*)NODE_FLOAT, (node*)strdup(s));
     945  if (suffix & NUM_SUFFIX_R) {
     946    result = new_rational(p, result);
     947  }
     948  if (suffix & NUM_SUFFIX_I) {
     949    result = new_imaginary(p, result);
     950  }
     951  return result;
     952}
     953#endif
    749954
    750955/* (:str . (s . len)) */
    751956static node*
    752 new_str(parser_state *p, const char *s, int len)
     957new_str(parser_state *p, const char *s, size_t len)
    753958{
    754959  return cons((node*)NODE_STR, cons((node*)strndup(s, len), nint(len)));
     
    762967}
    763968
     969static int
     970string_node_p(node *n)
     971{
     972  return (int)((enum node_type)(intptr_t)n->car == NODE_STR);
     973}
     974
     975static node*
     976composite_string_node(parser_state *p, node *a, node *b)
     977{
     978  size_t newlen = (size_t)a->cdr + (size_t)b->cdr;
     979  char *str = (char*)mrb_pool_realloc(p->pool, a->car, (size_t)a->cdr + 1, newlen + 1);
     980  memcpy(str + (size_t)a->cdr, b->car, (size_t)b->cdr);
     981  str[newlen] = '\0';
     982  a->car = (node*)str;
     983  a->cdr = (node*)newlen;
     984  cons_free(b);
     985  return a;
     986}
     987
     988static node*
     989concat_string(parser_state *p, node *a, node *b)
     990{
     991  if (string_node_p(a)) {
     992    if (string_node_p(b)) {
     993      /* a == NODE_STR && b == NODE_STR */
     994      composite_string_node(p, a->cdr, b->cdr);
     995      cons_free(b);
     996      return a;
     997    }
     998    else {
     999      /* a == NODE_STR && b == NODE_DSTR */
     1000
     1001      if (string_node_p(b->cdr->car)) {
     1002        /* a == NODE_STR && b->[NODE_STR, ...] */
     1003        composite_string_node(p, a->cdr, b->cdr->car->cdr);
     1004        cons_free(b->cdr->car);
     1005        b->cdr->car = a;
     1006        return b;
     1007      }
     1008    }
     1009  }
     1010  else {
     1011    node *c; /* last node of a */
     1012    for (c = a; c->cdr != NULL; c = c->cdr) ;
     1013
     1014    if (string_node_p(b)) {
     1015      /* a == NODE_DSTR && b == NODE_STR */
     1016      if (string_node_p(c->car)) {
     1017        /* a->[..., NODE_STR] && b == NODE_STR */
     1018        composite_string_node(p, c->car->cdr, b->cdr);
     1019        cons_free(b);
     1020        return a;
     1021      }
     1022
     1023      push(a, b);
     1024      return a;
     1025    }
     1026    else {
     1027      /* a == NODE_DSTR && b == NODE_DSTR */
     1028      if (string_node_p(c->car) && string_node_p(b->cdr->car)) {
     1029        /* a->[..., NODE_STR] && b->[NODE_STR, ...] */
     1030        node *d = b->cdr;
     1031        cons_free(b);
     1032        composite_string_node(p, c->car->cdr, d->car->cdr);
     1033        cons_free(d->car);
     1034        c->cdr = d->cdr;
     1035        cons_free(d);
     1036        return a;
     1037      }
     1038      else {
     1039        c->cdr = b->cdr;
     1040        cons_free(b);
     1041        return a;
     1042      }
     1043    }
     1044  }
     1045
     1046  return new_dstr(p, list2(a, b));
     1047}
     1048
    7641049/* (:str . (s . len)) */
    7651050static node*
     
    7801065new_dsym(parser_state *p, node *a)
    7811066{
    782   return cons((node*)NODE_DSYM, new_dstr(p, a));
     1067  return cons((node*)NODE_DSYM, a);
    7831068}
    7841069
     
    10241309  if (p->parsing_heredoc == NULL) {
    10251310    p->lstate = EXPR_BEG;
    1026     p->cmd_start = TRUE;
    10271311    end_strterm(p);
    10281312    p->lex_strterm = p->lex_strterm_before_heredoc;
    10291313    p->lex_strterm_before_heredoc = NULL;
    1030     p->heredoc_end_now = TRUE;
    10311314  }
    10321315  else {
     
    11051388        keyword__ENCODING__
    11061389
    1107 %token <id>  tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL
     1390%token <id>  tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL_TAG
    11081391%token <nd>  tINTEGER tFLOAT tCHAR tXSTRING tREGEXP
    1109 %token <nd>  tSTRING tSTRING_PART tSTRING_MID tLABEL_END
     1392%token <nd>  tSTRING tSTRING_PART tSTRING_MID
    11101393%token <nd>  tNTH_REF tBACK_REF
    11111394%token <num> tREGEXP_END
    1112 
    1113 %type <nd> singleton string string_rep string_interp xstring regexp
     1395%token <num> tNUMPARAM
     1396
     1397%type <nd> singleton string string_fragment string_rep string_interp xstring regexp
    11141398%type <nd> literal numeric cpath symbol
    11151399%type <nd> top_compstmt top_stmts top_stmt
     
    11221406%type <nd> command_asgn command_rhs mrhs superclass block_call block_command
    11231407%type <nd> f_block_optarg f_block_opt
    1124 %type <nd> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
     1408%type <nd> f_arglist f_args f_arg f_arg_item f_optarg f_margs
    11251409%type <nd> assoc_list assocs assoc undef_list backref for_var
    11261410%type <nd> block_param opt_block_param block_param_def f_opt
     
    11321416%type <nd> heredoc words symbols
    11331417%type <num> call_op call_op2     /* 0:'&.', 1:'.', 2:'::' */
     1418
     1419%type <nd> args_tail opt_args_tail f_kwarg f_kw f_kwrest
     1420%type <nd> f_block_kwarg f_block_kw block_args_tail opt_block_args_tail
     1421%type <id> f_label
    11341422
    11351423%token tUPLUS             /* unary+ */
     
    11581446%token tLBRACE_ARG        /* { */
    11591447%token tSTAR              /* * */
     1448%token tDSTAR             /* ** */
    11601449%token tAMPER             /* & */
    11611450%token tLAMBDA            /* -> */
     
    11791468%right '=' tOP_ASGN
    11801469%left modifier_rescue
    1181 %right '?' ':'
     1470%right '?' ':' tLABEL_TAG
    11821471%nonassoc tDOT2 tDOT3
    11831472%left  tOROP
     
    12371526                    {
    12381527                      $<nd>$ = local_switch(p);
     1528                      nvars_block(p);
    12391529                    }
    12401530                  '{' top_compstmt '}'
     
    12421532                      yyerror(p, "BEGIN not supported");
    12431533                      local_resume(p, $<nd>2);
     1534                      nvars_unnest(p);
    12441535                      $$ = 0;
    12451536                    }
     
    13591650                      $$ = new_op_asgn(p, $1, $2, $3);
    13601651                    }
    1361                 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
    1362                     {
    1363                       $$ = new_op_asgn(p, new_call(p, $1, intern("[]",2), $3, '.'), $5, $6);
     1652                | primary_value '[' opt_call_args ']' tOP_ASGN command_rhs
     1653                    {
     1654                      $$ = new_op_asgn(p, new_call(p, $1, intern_lit("[]"), $3, '.'), $5, $6);
    13641655                    }
    13651656                | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
     
    13851676                      $$ = new_begin(p, 0);
    13861677                    }
    1387                 ;
     1678                ;
    13881679
    13891680command_rhs     : command_call   %prec tOP_ASGN
     
    14391730                    {
    14401731                      local_nest(p);
     1732                      nvars_nest(p);
    14411733                    }
    14421734                  opt_block_param
     
    14461738                      $$ = new_block(p, $3, $4);
    14471739                      local_unnest(p);
     1740                      nvars_unnest(p);
    14481741                    }
    14491742                ;
     
    15881881                      assignable(p, $1);
    15891882                    }
    1590                 | primary_value '[' opt_call_args rbracket
    1591                     {
    1592                       $$ = new_call(p, $1, intern("[]",2), $3, '.');
     1883                | primary_value '[' opt_call_args ']'
     1884                    {
     1885                      $$ = new_call(p, $1, intern_lit("[]"), $3, '.');
    15931886                    }
    15941887                | primary_value call_op tIDENTIFIER
     
    16271920                      assignable(p, $1);
    16281921                    }
    1629                 | primary_value '[' opt_call_args rbracket
    1630                     {
    1631                       $$ = new_call(p, $1, intern("[]",2), $3, '.');
     1922                | primary_value '[' opt_call_args ']'
     1923                    {
     1924                      $$ = new_call(p, $1, intern_lit("[]"), $3, '.');
    16321925                    }
    16331926                | primary_value call_op tIDENTIFIER
     
    16591952                      backref_error(p, $1);
    16601953                      $$ = 0;
     1954                    }
     1955                | tNUMPARAM
     1956                    {
     1957                      yyerror(p, "can't assign to numbered parameter");
    16611958                    }
    16621959                ;
     
    17132010                ;
    17142011
    1715 op              : '|'           { $$ = intern_c('|');   }
    1716                 | '^'           { $$ = intern_c('^');   }
    1717                 | '&'           { $$ = intern_c('&');   }
    1718                 | tCMP          { $$ = intern("<=>",3); }
    1719                 | tEQ           { $$ = intern("==",2);  }
    1720                 | tEQQ          { $$ = intern("===",3); }
    1721                 | tMATCH        { $$ = intern("=~",2);  }
    1722                 | tNMATCH       { $$ = intern("!~",2);  }
    1723                 | '>'           { $$ = intern_c('>');   }
    1724                 | tGEQ          { $$ = intern(">=",2);  }
    1725                 | '<'           { $$ = intern_c('<');   }
    1726                 | tLEQ          { $$ = intern("<=",2);  }
    1727                 | tNEQ          { $$ = intern("!=",2);  }
    1728                 | tLSHFT        { $$ = intern("<<",2);  }
    1729                 | tRSHFT        { $$ = intern(">>",2);  }
    1730                 | '+'           { $$ = intern_c('+');   }
    1731                 | '-'           { $$ = intern_c('-');   }
    1732                 | '*'           { $$ = intern_c('*');   }
    1733                 | tSTAR         { $$ = intern_c('*');   }
    1734                 | '/'           { $$ = intern_c('/');   }
    1735                 | '%'           { $$ = intern_c('%');   }
    1736                 | tPOW          { $$ = intern("**",2);  }
    1737                 | '!'           { $$ = intern_c('!');   }
    1738                 | '~'           { $$ = intern_c('~');   }
    1739                 | tUPLUS        { $$ = intern("+@",2);  }
    1740                 | tUMINUS       { $$ = intern("-@",2);  }
    1741                 | tAREF         { $$ = intern("[]",2);  }
    1742                 | tASET         { $$ = intern("[]=",3); }
    1743                 | '`'           { $$ = intern_c('`');   }
     2012op              : '|'           { $$ = intern_lit("|");   }
     2013                | '^'           { $$ = intern_lit("^");   }
     2014                | '&'           { $$ = intern_lit("&");   }
     2015                | tCMP          { $$ = intern_lit("<=>"); }
     2016                | tEQ           { $$ = intern_lit("==");  }
     2017                | tEQQ          { $$ = intern_lit("==="); }
     2018                | tMATCH        { $$ = intern_lit("=~");  }
     2019                | tNMATCH       { $$ = intern_lit("!~");  }
     2020                | '>'           { $$ = intern_lit(">");   }
     2021                | tGEQ          { $$ = intern_lit(">=");  }
     2022                | '<'           { $$ = intern_lit("<");   }
     2023                | tLEQ          { $$ = intern_lit("<=");  }
     2024                | tNEQ          { $$ = intern_lit("!=");  }
     2025                | tLSHFT        { $$ = intern_lit("<<");  }
     2026                | tRSHFT        { $$ = intern_lit(">>");  }
     2027                | '+'           { $$ = intern_lit("+");   }
     2028                | '-'           { $$ = intern_lit("-");   }
     2029                | '*'           { $$ = intern_lit("*");   }
     2030                | tSTAR         { $$ = intern_lit("*");   }
     2031                | '/'           { $$ = intern_lit("/");   }
     2032                | '%'           { $$ = intern_lit("%");   }
     2033                | tPOW          { $$ = intern_lit("**");  }
     2034                | tDSTAR        { $$ = intern_lit("**");  }
     2035                | '!'           { $$ = intern_lit("!");   }
     2036                | '~'           { $$ = intern_lit("~");   }
     2037                | tUPLUS        { $$ = intern_lit("+@");  }
     2038                | tUMINUS       { $$ = intern_lit("-@");  }
     2039                | tAREF         { $$ = intern_lit("[]");  }
     2040                | tASET         { $$ = intern_lit("[]="); }
     2041                | '`'           { $$ = intern_lit("`");   }
    17442042                ;
    17452043
     
    17662064                      $$ = new_op_asgn(p, $1, $2, $3);
    17672065                    }
    1768                 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
    1769                     {
    1770                       $$ = new_op_asgn(p, new_call(p, $1, intern("[]",2), $3, '.'), $5, $6);
     2066                | primary_value '[' opt_call_args ']' tOP_ASGN arg_rhs
     2067                    {
     2068                      $$ = new_op_asgn(p, new_call(p, $1, intern_lit("[]"), $3, '.'), $5, $6);
    17712069                    }
    17722070                | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
     
    19252223                      $$ = new_if(p, cond($1), $3, $6);
    19262224                    }
     2225                | arg '?' arg opt_nl tLABEL_TAG arg
     2226                    {
     2227                      $$ = new_if(p, cond($1), $3, $6);
     2228                    }
    19272229                | primary
    19282230                    {
     
    19392241                | args comma assocs trailer
    19402242                    {
    1941                       $$ = push($1, new_hash(p, $3));
     2243                      $$ = push($1, new_kw_hash(p, $3));
    19422244                    }
    19432245                | assocs trailer
    19442246                    {
    1945                       $$ = cons(new_hash(p, $1), 0);
     2247                      $$ = cons(new_kw_hash(p, $1), 0);
    19462248                      NODE_LINENO($$, $1);
    19472249                    }
     
    19602262                ;
    19612263
    1962 paren_args      : '(' opt_call_args rparen
     2264paren_args      : '(' opt_call_args ')'
    19632265                    {
    19642266                      $$ = $2;
     
    19712273
    19722274opt_call_args   : none
    1973                 | call_args
    1974                 | args ','
     2275                | call_args opt_terms
     2276                | args comma
    19752277                    {
    19762278                      $$ = cons($1,0);
    19772279                      NODE_LINENO($$, $1);
    19782280                    }
    1979                 | args comma assocs ','
    1980                     {
    1981                       $$ = cons(push($1, new_hash(p, $3)), 0);
     2281                | args comma assocs comma
     2282                    {
     2283                      $$ = cons(push($1, new_kw_hash(p, $3)), 0);
    19822284                      NODE_LINENO($$, $1);
    19832285                    }
    1984                 | assocs ','
    1985                     {
    1986                       $$ = cons(list1(new_hash(p, $1)), 0);
     2286                | assocs comma
     2287                    {
     2288                      $$ = cons(list1(new_kw_hash(p, $1)), 0);
    19872289                      NODE_LINENO($$, $1);
    19882290                    }
     
    20022304                | assocs opt_block_arg
    20032305                    {
    2004                       $$ = cons(list1(new_hash(p, $1)), $2);
     2306                      $$ = cons(list1(new_kw_hash(p, $1)), $2);
    20052307                      NODE_LINENO($$, $1);
    20062308                    }
    20072309                | args comma assocs opt_block_arg
    20082310                    {
    2009                       $$ = cons(push($1, new_hash(p, $3)), $4);
     2311                      $$ = cons(push($1, new_kw_hash(p, $3)), $4);
    20102312                      NODE_LINENO($$, $1);
    20112313                    }
     
    20452347
    20462348comma           : ','
    2047                 | ','  heredoc_bodies
     2349                | ','  opt_nl heredoc_bodies
    20482350                ;
    20492351
     
    20962398                | var_ref
    20972399                | backref
     2400                | tNUMPARAM
     2401                    {
     2402                      $$ = new_nvar(p, $1);
     2403                    }
    20982404                | tFID
    20992405                    {
     
    22482554                        yyerror(p, "class definition in method body");
    22492555                      $<nd>$ = local_switch(p);
     2556                      nvars_block(p);
    22502557                    }
    22512558                  bodystmt
     
    22552562                      SET_LINENO($$, $1);
    22562563                      local_resume(p, $<nd>4);
     2564                      nvars_unnest(p);
    22572565                    }
    22582566                | keyword_class
     
    22652573                    {
    22662574                      $<nd>$ = cons(local_switch(p), nint(p->in_single));
     2575                      nvars_block(p);
    22672576                      p->in_single = 0;
    22682577                    }
     
    22732582                      SET_LINENO($$, $1);
    22742583                      local_resume(p, $<nd>6->car);
     2584                      nvars_unnest(p);
    22752585                      p->in_def = $<num>4;
    22762586                      p->in_single = intn($<nd>6->cdr);
     
    22822592                        yyerror(p, "module definition in method body");
    22832593                      $<nd>$ = local_switch(p);
     2594                      nvars_block(p);
    22842595                    }
    22852596                  bodystmt
     
    22892600                      SET_LINENO($$, $1);
    22902601                      local_resume(p, $<nd>3);
     2602                      nvars_unnest(p);
    22912603                    }
    22922604                | keyword_def fname
     
    22982610                      p->in_def++;
    22992611                      $<nd>$ = local_switch(p);
     2612                      nvars_block(p);
    23002613                    }
    23012614                  f_arglist
     
    23062619                      SET_LINENO($$, $1);
    23072620                      local_resume(p, $<nd>4);
     2621                      nvars_unnest(p);
    23082622                      p->in_def--;
    23092623                      p->cmdarg_stack = $<stack>3;
     
    23202634                      p->lstate = EXPR_ENDFN; /* force for args */
    23212635                      $<nd>$ = local_switch(p);
     2636                      nvars_block(p);
    23222637                    }
    23232638                  f_arglist
     
    23282643                      SET_LINENO($$, $1);
    23292644                      local_resume(p, $<nd>6);
     2645                      nvars_unnest(p);
    23302646                      p->in_single--;
    23312647                      p->cmdarg_stack = $<stack>4;
     
    23882704                ;
    23892705
    2390 f_marg          : f_norm_arg
    2391                     {
    2392                       $$ = new_arg(p, $1);
    2393                     }
    2394                 | tLPAREN f_margs rparen
    2395                     {
    2396                       $$ = new_masgn(p, $2, 0);
    2397                     }
    2398                 ;
    2399 
    2400 f_marg_list     : f_marg
    2401                     {
    2402                       $$ = list1($1);
    2403                     }
    2404                 | f_marg_list ',' f_marg
    2405                     {
    2406                       $$ = push($1, $3);
    2407                     }
    2408                 ;
    2409 
    2410 f_margs         : f_marg_list
     2706f_margs         : f_arg
    24112707                    {
    24122708                      $$ = list3($1,0,0);
    24132709                    }
    2414                 | f_marg_list ',' tSTAR f_norm_arg
     2710                | f_arg ',' tSTAR f_norm_arg
    24152711                    {
    24162712                      $$ = list3($1, new_arg(p, $4), 0);
    24172713                    }
    2418                 | f_marg_list ',' tSTAR f_norm_arg ',' f_marg_list
     2714                | f_arg ',' tSTAR f_norm_arg ',' f_arg
    24192715                    {
    24202716                      $$ = list3($1, new_arg(p, $4), $6);
    24212717                    }
    2422                 | f_marg_list ',' tSTAR
    2423                     {
     2718                | f_arg ',' tSTAR
     2719                    {
     2720                      local_add_f(p, 0);
    24242721                      $$ = list3($1, (node*)-1, 0);
    24252722                    }
    2426                 | f_marg_list ',' tSTAR ',' f_marg_list
     2723                | f_arg ',' tSTAR ',' f_arg
    24272724                    {
    24282725                      $$ = list3($1, (node*)-1, $5);
     
    24322729                      $$ = list3(0, new_arg(p, $2), 0);
    24332730                    }
    2434                 | tSTAR f_norm_arg ',' f_marg_list
     2731                | tSTAR f_norm_arg ',' f_arg
    24352732                    {
    24362733                      $$ = list3(0, new_arg(p, $2), $4);
     
    24382735                | tSTAR
    24392736                    {
     2737                      local_add_f(p, 0);
    24402738                      $$ = list3(0, (node*)-1, 0);
    24412739                    }
    2442                 | tSTAR ',' f_marg_list
    2443                     {
    2444                       $$ = list3(0, (node*)-1, $3);
    2445                     }
    2446                 ;
    2447 
    2448 block_param     : f_arg ',' f_block_optarg ',' f_rest_arg opt_f_block_arg
     2740                | tSTAR ','
     2741                    {
     2742                      local_add_f(p, 0);
     2743                    }
     2744                  f_arg
     2745                    {
     2746                      $$ = list3(0, (node*)-1, $4);
     2747                    }
     2748                ;
     2749
     2750block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
     2751                    {
     2752                      $$ = new_args_tail(p, $1, $3, $4);
     2753                    }
     2754                | f_block_kwarg opt_f_block_arg
     2755                    {
     2756                      $$ = new_args_tail(p, $1, 0, $2);
     2757                    }
     2758                | f_kwrest opt_f_block_arg
     2759                    {
     2760                      $$ = new_args_tail(p, 0, $1, $2);
     2761                    }
     2762                | f_block_arg
     2763                    {
     2764                      $$ = new_args_tail(p, 0, 0, $1);
     2765                    }
     2766                ;
     2767
     2768opt_block_args_tail : ',' block_args_tail
     2769                    {
     2770                      $$ = $2;
     2771                    }
     2772                | /* none */
     2773                    {
     2774                      $$ = new_args_tail(p, 0, 0, 0);
     2775                    }
     2776                ;
     2777
     2778block_param     : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
    24492779                    {
    24502780                      $$ = new_args(p, $1, $3, $5, 0, $6);
    24512781                    }
    2452                 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
     2782                | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
    24532783                    {
    24542784                      $$ = new_args(p, $1, $3, $5, $7, $8);
    24552785                    }
    2456                 | f_arg ',' f_block_optarg opt_f_block_arg
     2786                | f_arg ',' f_block_optarg opt_block_args_tail
    24572787                    {
    24582788                      $$ = new_args(p, $1, $3, 0, 0, $4);
    24592789                    }
    2460                 | f_arg ',' f_block_optarg ',' f_arg opt_f_block_arg
     2790                | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
    24612791                    {
    24622792                      $$ = new_args(p, $1, $3, 0, $5, $6);
    24632793                    }
    2464                 | f_arg ',' f_rest_arg opt_f_block_arg
     2794                | f_arg ',' f_rest_arg opt_block_args_tail
    24652795                    {
    24662796                      $$ = new_args(p, $1, 0, $3, 0, $4);
    24672797                    }
    2468                 | f_arg ','
    2469                     {
    2470                       $$ = new_args(p, $1, 0, 0, 0, 0);
    2471                     }
    2472                 | f_arg ',' f_rest_arg ',' f_arg opt_f_block_arg
     2798                | f_arg ',' opt_block_args_tail
     2799                    {
     2800                      $$ = new_args(p, $1, 0, 0, 0, $3);
     2801                    }
     2802                | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
    24732803                    {
    24742804                      $$ = new_args(p, $1, 0, $3, $5, $6);
    24752805                    }
    2476                 | f_arg opt_f_block_arg
     2806                | f_arg opt_block_args_tail
    24772807                    {
    24782808                      $$ = new_args(p, $1, 0, 0, 0, $2);
    24792809                    }
    2480                 | f_block_optarg ',' f_rest_arg opt_f_block_arg
     2810                | f_block_optarg ',' f_rest_arg opt_block_args_tail
    24812811                    {
    24822812                      $$ = new_args(p, 0, $1, $3, 0, $4);
    24832813                    }
    2484                 | f_block_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
     2814                | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
    24852815                    {
    24862816                      $$ = new_args(p, 0, $1, $3, $5, $6);
    24872817                    }
    2488                 | f_block_optarg opt_f_block_arg
     2818                | f_block_optarg opt_block_args_tail
    24892819                    {
    24902820                      $$ = new_args(p, 0, $1, 0, 0, $2);
    24912821                    }
    2492                 | f_block_optarg ',' f_arg opt_f_block_arg
     2822                | f_block_optarg ',' f_arg opt_block_args_tail
    24932823                    {
    24942824                      $$ = new_args(p, 0, $1, 0, $3, $4);
    24952825                    }
    2496                 | f_rest_arg opt_f_block_arg
     2826                | f_rest_arg opt_block_args_tail
    24972827                    {
    24982828                      $$ = new_args(p, 0, 0, $1, 0, $2);
    24992829                    }
    2500                 | f_rest_arg ',' f_arg opt_f_block_arg
     2830                | f_rest_arg ',' f_arg opt_block_args_tail
    25012831                    {
    25022832                      $$ = new_args(p, 0, 0, $1, $3, $4);
    25032833                    }
    2504                 | f_block_arg
     2834                | block_args_tail
    25052835                    {
    25062836                      $$ = new_args(p, 0, 0, 0, 0, $1);
     
    25092839
    25102840opt_block_param : none
     2841                    {
     2842                      local_add_blk(p, 0);
     2843                      $$ = 0;
     2844                    }
    25112845                | block_param_def
    2512                     {
     2846                   {
    25132847                      p->cmd_start = TRUE;
    25142848                      $$ = $1;
     
    25162850                ;
    25172851
    2518 block_param_def : '|' opt_bv_decl '|'
     2852block_param_def : '|' {local_add_blk(p, 0);} opt_bv_decl '|'
    25192853                    {
    25202854                      $$ = 0;
     
    25222856                | tOROP
    25232857                    {
     2858                      local_add_blk(p, 0);
    25242859                      $$ = 0;
    25252860                    }
     
    25672902                      $$ = $2;
    25682903                    }
    2569                 | keyword_do_LAMBDA compstmt keyword_end
     2904                | keyword_do_LAMBDA bodystmt keyword_end
    25702905                    {
    25712906                      $$ = $2;
     
    25762911                    {
    25772912                      local_nest(p);
     2913                      nvars_nest(p);
    25782914                    }
    25792915                  opt_block_param
    2580                   compstmt
     2916                  bodystmt
    25812917                  keyword_end
    25822918                    {
    25832919                      $$ = new_block(p,$3,$4);
    25842920                      local_unnest(p);
     2921                      nvars_unnest(p);
    25852922                    }
    25862923                ;
     
    26302967                | primary_value call_op paren_args
    26312968                    {
    2632                       $$ = new_call(p, $1, intern("call",4), $3, $2);
     2969                      $$ = new_call(p, $1, intern_lit("call"), $3, $2);
    26332970                    }
    26342971                | primary_value tCOLON2 paren_args
    26352972                    {
    2636                       $$ = new_call(p, $1, intern("call",4), $3, tCOLON2);
     2973                      $$ = new_call(p, $1, intern_lit("call"), $3, tCOLON2);
    26372974                    }
    26382975                | keyword_super paren_args
     
    26442981                      $$ = new_zsuper(p);
    26452982                    }
    2646                 | primary_value '[' opt_call_args rbracket
    2647                     {
    2648                       $$ = new_call(p, $1, intern("[]",2), $3, '.');
     2983                | primary_value '[' opt_call_args ']'
     2984                    {
     2985                      $$ = new_call(p, $1, intern_lit("[]"), $3, '.');
    26492986                    }
    26502987                ;
     
    26532990                    {
    26542991                      local_nest(p);
     2992                      nvars_nest(p);
    26552993                      $<num>$ = p->lineno;
    26562994                    }
     
    26612999                      SET_LINENO($$, $<num>2);
    26623000                      local_unnest(p);
     3001                      nvars_unnest(p);
    26633002                    }
    26643003                | keyword_do
    26653004                    {
    26663005                      local_nest(p);
     3006                      nvars_nest(p);
    26673007                      $<num>$ = p->lineno;
    26683008                    }
    26693009                  opt_block_param
    2670                   compstmt keyword_end
     3010                  bodystmt keyword_end
    26713011                    {
    26723012                      $$ = new_block(p,$3,$4);
    26733013                      SET_LINENO($$, $<num>2);
    26743014                      local_unnest(p);
     3015                      nvars_unnest(p);
    26753016                    }
    26763017                ;
     
    27343075                ;
    27353076
    2736 string          : tCHAR
     3077string          : string_fragment
     3078                | string string_fragment
     3079                    {
     3080                      $$ = concat_string(p, $1, $2);
     3081                    }
     3082                ;
     3083
     3084string_fragment : tCHAR
    27373085                | tSTRING
    27383086                | tSTRING_BEG tSTRING
     
    28543202symbol          : basic_symbol
    28553203                    {
     3204                      p->lstate = EXPR_ENDARG;
    28563205                      $$ = new_sym(p, $1);
    28573206                    }
    28583207                | tSYMBEG tSTRING_BEG string_rep tSTRING
    28593208                    {
    2860                       p->lstate = EXPR_END;
    2861                       $$ = new_dsym(p, push($3, $4));
     3209                      p->lstate = EXPR_ENDARG;
     3210                      $$ = new_dsym(p, new_dstr(p, push($3, $4)));
    28623211                    }
    28633212                ;
     
    28653214basic_symbol    : tSYMBEG sym
    28663215                    {
    2867                       p->lstate = EXPR_END;
    28683216                      $$ = $2;
    28693217                    }
     
    29323280                      assignable(p, $1);
    29333281                    }
     3282                | tNUMPARAM
     3283                    {
     3284                      yyerror(p, "can't assign to numbered parameter");
     3285                    }
    29343286                ;
    29353287
     
    29563308                | keyword__FILE__
    29573309                    {
    2958                       const char *fn = p->filename;
     3310                      const char *fn = mrb_sym_name_len(p->mrb, p->filename_sym, NULL);
    29593311                      if (!fn) {
    29603312                        fn = "(null)";
     
    29663318                      char buf[16];
    29673319
    2968                       snprintf(buf, sizeof(buf), "%d", p->lineno);
    2969                       $$ = new_int(p, buf, 10);
     3320                      dump_int(p->lineno, buf);
     3321                      $$ = new_int(p, buf, 10, 0);
     3322                    }
     3323                | keyword__ENCODING__
     3324                    {
     3325#ifdef MRB_UTF8_STRING
     3326                      const char *enc = "UTF-8";
     3327#else
     3328                      const char *enc = "ASCII-8BIT";
     3329#endif
     3330                      $$ = new_str(p, enc, strlen(enc));
    29703331                    }
    29713332                ;
     
    30073368                ;
    30083369
    3009 f_args          : f_arg ',' f_optarg ',' f_rest_arg opt_f_block_arg
     3370f_label         : tIDENTIFIER tLABEL_TAG
     3371                    {
     3372                      local_nest(p);
     3373                    }
     3374                ;
     3375
     3376f_kw            : f_label arg
     3377                    {
     3378                      void_expr_error(p, $2);
     3379                      $$ = new_kw_arg(p, $1, cons($2, locals_node(p)));
     3380                      local_unnest(p);
     3381                    }
     3382                | f_label
     3383                    {
     3384                      $$ = new_kw_arg(p, $1, 0);
     3385                      local_unnest(p);
     3386                    }
     3387                ;
     3388
     3389f_block_kw      : f_label primary_value
     3390                    {
     3391                      $$ = new_kw_arg(p, $1, cons($2, locals_node(p)));
     3392                      local_unnest(p);
     3393                    }
     3394                | f_label
     3395                    {
     3396                      $$ = new_kw_arg(p, $1, 0);
     3397                      local_unnest(p);
     3398                    }
     3399                ;
     3400
     3401f_block_kwarg   : f_block_kw
     3402                    {
     3403                      $$ = list1($1);
     3404                    }
     3405                | f_block_kwarg ',' f_block_kw
     3406                    {
     3407                      $$ = push($1, $3);
     3408                    }
     3409                ;
     3410
     3411f_kwarg         : f_kw
     3412                    {
     3413                      $$ = list1($1);
     3414                    }
     3415                | f_kwarg ',' f_kw
     3416                    {
     3417                      $$ = push($1, $3);
     3418                    }
     3419                ;
     3420
     3421kwrest_mark     : tPOW
     3422                | tDSTAR
     3423                ;
     3424
     3425f_kwrest        : kwrest_mark tIDENTIFIER
     3426                    {
     3427                      $$ = cons((node*)NODE_KW_REST_ARGS, nsym($2));
     3428                    }
     3429                | kwrest_mark
     3430                    {
     3431                      $$ = cons((node*)NODE_KW_REST_ARGS, 0);
     3432                    }
     3433                ;
     3434
     3435args_tail       : f_kwarg ',' f_kwrest opt_f_block_arg
     3436                    {
     3437                      $$ = new_args_tail(p, $1, $3, $4);
     3438                    }
     3439                | f_kwarg opt_f_block_arg
     3440                    {
     3441                      $$ = new_args_tail(p, $1, 0, $2);
     3442                    }
     3443                | f_kwrest opt_f_block_arg
     3444                    {
     3445                      $$ = new_args_tail(p, 0, $1, $2);
     3446                    }
     3447                | f_block_arg
     3448                    {
     3449                      $$ = new_args_tail(p, 0, 0, $1);
     3450                    }
     3451                ;
     3452
     3453opt_args_tail   : ',' args_tail
     3454                    {
     3455                      $$ = $2;
     3456                    }
     3457                | /* none */
     3458                    {
     3459                      $$ = new_args_tail(p, 0, 0, 0);
     3460                    }
     3461                ;
     3462
     3463f_args          : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
    30103464                    {
    30113465                      $$ = new_args(p, $1, $3, $5, 0, $6);
    30123466                    }
    3013                 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
     3467                | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
    30143468                    {
    30153469                      $$ = new_args(p, $1, $3, $5, $7, $8);
    30163470                    }
    3017                 | f_arg ',' f_optarg opt_f_block_arg
     3471                | f_arg ',' f_optarg opt_args_tail
    30183472                    {
    30193473                      $$ = new_args(p, $1, $3, 0, 0, $4);
    30203474                    }
    3021                 | f_arg ',' f_optarg ',' f_arg opt_f_block_arg
     3475                | f_arg ',' f_optarg ',' f_arg opt_args_tail
    30223476                    {
    30233477                      $$ = new_args(p, $1, $3, 0, $5, $6);
    30243478                    }
    3025                 | f_arg ',' f_rest_arg opt_f_block_arg
     3479                | f_arg ',' f_rest_arg opt_args_tail
    30263480                    {
    30273481                      $$ = new_args(p, $1, 0, $3, 0, $4);
    30283482                    }
    3029                 | f_arg ',' f_rest_arg ',' f_arg opt_f_block_arg
     3483                | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
    30303484                    {
    30313485                      $$ = new_args(p, $1, 0, $3, $5, $6);
    30323486                    }
    3033                 | f_arg opt_f_block_arg
     3487                | f_arg opt_args_tail
    30343488                    {
    30353489                      $$ = new_args(p, $1, 0, 0, 0, $2);
    30363490                    }
    3037                 | f_optarg ',' f_rest_arg opt_f_block_arg
     3491                | f_optarg ',' f_rest_arg opt_args_tail
    30383492                    {
    30393493                      $$ = new_args(p, 0, $1, $3, 0, $4);
    30403494                    }
    3041                 | f_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
     3495                | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
    30423496                    {
    30433497                      $$ = new_args(p, 0, $1, $3, $5, $6);
    30443498                    }
    3045                 | f_optarg opt_f_block_arg
     3499                | f_optarg opt_args_tail
    30463500                    {
    30473501                      $$ = new_args(p, 0, $1, 0, 0, $2);
    30483502                    }
    3049                 | f_optarg ',' f_arg opt_f_block_arg
     3503                | f_optarg ',' f_arg opt_args_tail
    30503504                    {
    30513505                      $$ = new_args(p, 0, $1, 0, $3, $4);
    30523506                    }
    3053                 | f_rest_arg opt_f_block_arg
     3507                | f_rest_arg opt_args_tail
    30543508                    {
    30553509                      $$ = new_args(p, 0, 0, $1, 0, $2);
    30563510                    }
    3057                 | f_rest_arg ',' f_arg opt_f_block_arg
     3511                | f_rest_arg ',' f_arg opt_args_tail
    30583512                    {
    30593513                      $$ = new_args(p, 0, 0, $1, $3, $4);
    30603514                    }
    3061                 | f_block_arg
     3515                | args_tail
    30623516                    {
    30633517                      $$ = new_args(p, 0, 0, 0, 0, $1);
     
    30653519                | /* none */
    30663520                    {
    3067                       local_add_f(p, 0);
     3521                      local_add_f(p, mrb_intern_lit(p->mrb, "&"));
    30683522                      $$ = new_args(p, 0, 0, 0, 0, 0);
    30693523                    }
     
    30903544                      $$ = 0;
    30913545                    }
     3546                | tNUMPARAM
     3547                    {
     3548                      yyerror(p, "formal argument cannot be a numbered parameter");
     3549                      $$ = 0;
     3550                    }
    30923551                ;
    30933552
     
    31073566                      $$ = new_arg(p, $1);
    31083567                    }
    3109                 | tLPAREN f_margs rparen
    3110                     {
    3111                       $$ = new_masgn(p, $2, 0);
     3568                | tLPAREN
     3569                    {
     3570                      $<nd>$ = local_switch(p);
     3571                    }
     3572                  f_margs rparen
     3573                    {
     3574                      $$ = new_masgn_param(p, $3, p->locals->car);
     3575                      local_resume(p, $<nd>2);
     3576                      local_add_f(p, 0);
    31123577                    }
    31133578                ;
     
    31263591                    {
    31273592                      local_add_f(p, $1);
     3593                      local_nest(p);
    31283594                      $$ = $1;
    31293595                    }
     
    31333599                    {
    31343600                      void_expr_error(p, $2);
    3135                       $$ = cons(nsym($1), $2);
     3601                      $$ = cons(nsym($1), cons($2, locals_node(p)));
     3602                      local_unnest(p);
    31363603                    }
    31373604                ;
     
    31403607                    {
    31413608                      void_expr_error(p, $2);
    3142                       $$ = cons(nsym($1), $2);
     3609                      $$ = cons(nsym($1), cons($2, locals_node(p)));
     3610                      local_unnest(p);
    31433611                    }
    31443612                ;
     
    31753643                | restarg_mark
    31763644                    {
    3177                       local_add_f(p, 0);
     3645                      local_add_f(p, mrb_intern_lit(p->mrb, "*"));
    31783646                      $$ = -1;
    31793647                    }
     
    31863654f_block_arg     : blkarg_mark tIDENTIFIER
    31873655                    {
    3188                       local_add_f(p, $2);
    31893656                      $$ = $2;
    31903657                    }
     
    31973664                | none
    31983665                    {
    3199                       local_add_f(p, 0);
    32003666                      $$ = 0;
    32013667                    }
     
    32443710                      NODE_LINENO($$, $1);
    32453711                    }
    3246                 | assocs ',' assoc
     3712                | assocs comma assoc
    32473713                    {
    32483714                      $$ = push($1, $3);
    32493715                    }
     3716                ;
     3717
     3718label_tag       : tLABEL_TAG
     3719                | tLABEL_TAG heredoc_bodies
    32503720                ;
    32513721
     
    32563726                      $$ = cons($1, $3);
    32573727                    }
    3258                 | tLABEL arg
     3728                | tIDENTIFIER label_tag arg
     3729                    {
     3730                      void_expr_error(p, $3);
     3731                      $$ = cons(new_sym(p, $1), $3);
     3732                    }
     3733                | string_fragment label_tag arg
     3734                    {
     3735                      void_expr_error(p, $3);
     3736                      if ($1->car == (node*)NODE_DSTR) {
     3737                        $$ = cons(new_dsym(p, $1), $3);
     3738                      }
     3739                      else {
     3740                        $$ = cons(new_sym(p, new_strsym(p, $1)), $3);
     3741                      }
     3742                    }
     3743                | tDSTAR arg
    32593744                    {
    32603745                      void_expr_error(p, $2);
    3261                       $$ = cons(new_sym(p, $1), $2);
    3262                     }
    3263                 | tLABEL_END arg
    3264                     {
    3265                       void_expr_error(p, $2);
    3266                       $$ = cons(new_sym(p, new_strsym(p, $1)), $2);
    3267                     }
    3268                 | tSTRING_BEG tLABEL_END arg
    3269                     {
    3270                       void_expr_error(p, $3);
    3271                       $$ = cons(new_sym(p, new_strsym(p, $2)), $3);
    3272                     }
    3273                 | tSTRING_BEG string_rep tLABEL_END arg
    3274                     {
    3275                       void_expr_error(p, $4);
    3276                       $$ = cons(new_dsym(p, push($2, $3)), $4);
     3746                      $$ = cons(cons((node*)NODE_KW_REST_ARGS, 0), $2);
    32773747                    }
    32783748                ;
     
    33233793                ;
    33243794
    3325 rparen          : opt_nl ')'
    3326                 ;
    3327 
    3328 rbracket        : opt_nl ']'
     3795rparen          : opt_terms ')'
    33293796                ;
    33303797
    33313798trailer         : /* none */
    3332                 | nl
     3799                | terms
    33333800                | comma
    33343801                ;
     
    33623829{
    33633830  char* c;
    3364   int n;
     3831  size_t n;
    33653832
    33663833  if (! p->capture_errors) {
    33673834#ifndef MRB_DISABLE_STDIO
    3368     if (p->filename) {
    3369       fprintf(stderr, "%s:%d:%d: %s\n", p->filename, p->lineno, p->column, s);
     3835    if (p->filename_sym) {
     3836      const char *filename = mrb_sym_name_len(p->mrb, p->filename_sym, NULL);
     3837      fprintf(stderr, "%s:%d:%d: %s\n", filename, p->lineno, p->column, s);
    33703838    }
    33713839    else {
     
    33863854
    33873855static void
    3388 yyerror_i(parser_state *p, const char *fmt, int i)
     3856yyerror_c(parser_state *p, const char *msg, char c)
    33893857{
    33903858  char buf[256];
    33913859
    3392   snprintf(buf, sizeof(buf), fmt, i);
     3860  strncpy(buf, msg, sizeof(buf) - 2);
     3861  buf[sizeof(buf) - 2] = '\0';
     3862  strncat(buf, &c, 1);
    33933863  yyerror(p, buf);
    33943864}
     
    33983868{
    33993869  char* c;
    3400   int n;
     3870  size_t n;
    34013871
    34023872  if (! p->capture_errors) {
    34033873#ifndef MRB_DISABLE_STDIO
    3404     if (p->filename) {
    3405       fprintf(stderr, "%s:%d:%d: %s\n", p->filename, p->lineno, p->column, s);
     3874    if (p->filename_sym) {
     3875      const char *filename = mrb_sym_name_len(p->mrb, p->filename_sym, NULL);
     3876      fprintf(stderr, "%s:%d:%d: warning: %s\n", filename, p->lineno, p->column, s);
    34063877    }
    34073878    else {
    3408       fprintf(stderr, "line %d:%d: %s\n", p->lineno, p->column, s);
     3879      fprintf(stderr, "line %d:%d: warning: %s\n", p->lineno, p->column, s);
    34093880    }
    34103881#endif
     
    34283899
    34293900static void
    3430 yywarning_s(parser_state *p, const char *fmt, const char *s)
     3901yywarning_s(parser_state *p, const char *msg, const char *s)
    34313902{
    34323903  char buf[256];
    34333904
    3434   snprintf(buf, sizeof(buf), fmt, s);
     3905  strncpy(buf, msg, sizeof(buf) - 1);
     3906  buf[sizeof(buf) - 1] = '\0';
     3907  strncat(buf, ": ", sizeof(buf) - strlen(buf) - 1);
     3908  strncat(buf, s, sizeof(buf) - strlen(buf) - 1);
    34353909  yywarning(p, buf);
    34363910}
     
    34413915  int c;
    34423916
    3443   c = (int)(intptr_t)n->car;
     3917  c = intn(n->car);
    34443918
    34453919  if (c == NODE_NTH_REF) {
    3446     yyerror_i(p, "can't set variable $%" MRB_PRId, (mrb_int)(intptr_t)n->cdr);
     3920    yyerror_c(p, "can't set variable $", (char)intn(n->cdr)+'0');
    34473921  }
    34483922  else if (c == NODE_BACK_REF) {
    3449     yyerror_i(p, "can't set variable $%c", (int)(intptr_t)n->cdr);
     3923    yyerror_c(p, "can't set variable $", (char)intn(n->cdr));
    34503924  }
    34513925  else {
    3452     mrb_bug(p->mrb, "Internal error in backref_error() : n=>car == %S", mrb_fixnum_value(c));
     3926    mrb_bug(p->mrb, "Internal error in backref_error() : n=>car == %d", c);
    34533927  }
    34543928}
     
    34603934
    34613935  if (n == NULL) return;
    3462   c = (int)(intptr_t)n->car;
     3936  c = intn(n->car);
    34633937  switch (c) {
    34643938  case NODE_BREAK:
     
    34713945  case NODE_AND:
    34723946  case NODE_OR:
    3473     void_expr_error(p, n->cdr->car);
    3474     void_expr_error(p, n->cdr->cdr);
     3947    if (n->cdr) {
     3948      void_expr_error(p, n->cdr->car);
     3949      void_expr_error(p, n->cdr->cdr);
     3950    }
    34753951    break;
    34763952  case NODE_BEGIN:
     
    34923968
    34933969static inline int
     3970nextc0(parser_state *p)
     3971{
     3972  int c;
     3973#ifndef MRB_DISABLE_STDIO
     3974  if (p->f) {
     3975    if (feof(p->f)) return -1;
     3976    c = fgetc(p->f);
     3977    if (c == EOF) return -1;
     3978  }
     3979  else
     3980#endif
     3981    if (!p->s || p->s >= p->send) {
     3982      return -1;
     3983    }
     3984    else {
     3985      c = (unsigned char)*p->s++;
     3986    }
     3987  return c;
     3988}
     3989
     3990static inline int
    34943991nextc(parser_state *p)
    34953992{
     
    34993996    node *tmp;
    35003997
    3501     c = (int)(intptr_t)p->pb->car;
     3998    c = intn(p->pb->car);
    35023999    tmp = p->pb;
    35034000    p->pb = p->pb->cdr;
     
    35054002  }
    35064003  else {
    3507 #ifndef MRB_DISABLE_STDIO
    3508     if (p->f) {
    3509       if (feof(p->f)) goto eof;
    3510       c = fgetc(p->f);
    3511       if (c == EOF) goto eof;
    3512     }
    3513     else
    3514 #endif
    3515       if (!p->s || p->s >= p->send) {
    3516         goto eof;
    3517       }
    3518       else {
    3519         c = (unsigned char)*p->s++;
    3520       }
     4004    c = nextc0(p);
     4005    if (c < 0) goto eof;
    35214006  }
    35224007  if (c >= 0) {
     
    35244009  }
    35254010  if (c == '\r') {
    3526     c = nextc(p);
    3527     if (c != '\n') {
    3528       pushback(p, c);
    3529       return '\r';
    3530     }
    3531     return c;
     4011    const int lf = nextc0(p);
     4012    if (lf == '\n') {
     4013      return '\n';
     4014    }
     4015    if (lf > 0) pushback(p, lf);
    35324016  }
    35334017  return c;
     
    35484032    p->column--;
    35494033  }
    3550   p->pb = cons((node*)(intptr_t)c, p->pb);
     4034  p->pb = cons(nint(c), p->pb);
    35514035}
    35524036
     
    35734057    if (c0 == -1) return c0;    /* do not skip partial EOF */
    35744058    if (c0 >= 0) --p->column;
    3575     list = push(list, (node*)(intptr_t)c0);
     4059    list = push(list, nint(c0));
    35764060  } while(n--);
    35774061  if (p->pb) {
     
    35944078peeks(parser_state *p, const char *s)
    35954079{
    3596   int len = strlen(s);
     4080  size_t len = strlen(s);
    35974081
    35984082#ifndef MRB_DISABLE_STDIO
     
    36304114    s++;
    36314115    if (peeks(p, s)) {
    3632       int len = strlen(s);
     4116      size_t len = strlen(s);
    36334117
    36344118      while (len--) {
     
    37524236#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
    37534237
    3754 static int
     4238static int32_t
    37554239scan_oct(const int *start, int len, int *retlen)
    37564240{
    37574241  const int *s = start;
    3758   int retval = 0;
     4242  int32_t retval = 0;
    37594243
    37604244  /* mrb_assert(len <= 3) */
     
    37634247    retval |= *s++ - '0';
    37644248  }
    3765   *retlen = s - start;
     4249  *retlen = (int)(s - start);
    37664250
    37674251  return retval;
     
    37694253
    37704254static int32_t
    3771 scan_hex(const int *start, int len, int *retlen)
     4255scan_hex(parser_state *p, const int *start, int len, int *retlen)
    37724256{
    37734257  static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF";
    37744258  const int *s = start;
    3775   int32_t retval = 0;
     4259  uint32_t retval = 0;
    37764260  char *tmp;
    37774261
     
    37824266    s++;
    37834267  }
    3784   *retlen = s - start;
    3785 
    3786   return retval;
     4268  *retlen = (int)(s - start);
     4269
     4270  return (int32_t)retval;
    37874271}
    37884272
     
    37904274read_escape_unicode(parser_state *p, int limit)
    37914275{
    3792   int32_t c;
    37934276  int buf[9];
    37944277  int i;
     4278  int32_t hex;
    37954279
    37964280  /* Look for opening brace */
    37974281  i = 0;
    37984282  buf[0] = nextc(p);
    3799   if (buf[0] < 0) goto eof;
     4283  if (buf[0] < 0) {
     4284  eof:
     4285    yyerror(p, "invalid escape character syntax");
     4286    return -1;
     4287  }
    38004288  if (ISXDIGIT(buf[0])) {
    38014289    /* \uxxxx form */
     
    38124300    pushback(p, buf[0]);
    38134301  }
    3814   c = scan_hex(buf, i, &i);
    3815   if (i == 0) {
    3816   eof:
    3817     yyerror(p, "Invalid escape character syntax");
     4302  hex = scan_hex(p, buf, i, &i);
     4303  if (i == 0 || hex > 0x10FFFF || (hex & 0xFFFFF800) == 0xD800) {
     4304    yyerror(p, "invalid Unicode code point");
    38184305    return -1;
    38194306  }
    3820   if (c < 0 || c > 0x10FFFF || (c & 0xFFFFF800) == 0xD800) {
    3821     yyerror(p, "Invalid Unicode code point");
    3822     return -1;
    3823   }
    3824   return c;
     4307  return hex;
    38254308}
    38264309
     
    38884371      }
    38894372    }
    3890     c = scan_hex(buf, i, &i);
    38914373    if (i == 0) {
    3892       yyerror(p, "Invalid escape character syntax");
    3893       return 0;
    3894     }
    3895   }
    3896   return c;
     4374      yyerror(p, "invalid hex escape");
     4375      return -1;
     4376    }
     4377    return scan_hex(p, buf, i, &i);
     4378  }
    38974379
    38984380  case 'u':     /* Unicode */
     
    39614443  int c;
    39624444  string_type type = (string_type)(intptr_t)p->lex_strterm->car;
    3963   int nest_level = (intptr_t)p->lex_strterm->cdr->car;
    3964   int beg = (intptr_t)p->lex_strterm->cdr->cdr->car;
    3965   int end = (intptr_t)p->lex_strterm->cdr->cdr->cdr;
     4445  int nest_level = intn(p->lex_strterm->cdr->car);
     4446  int beg = intn(p->lex_strterm->cdr->cdr->car);
     4447  int end = intn(p->lex_strterm->cdr->cdr->cdr);
    39664448  parser_heredoc_info *hinf = (type & STR_FUNC_HEREDOC) ? parsing_heredoc_inf(p) : NULL;
    3967   int cmd_state = p->cmd_start;
    39684449
    39694450  if (beg == 0) beg = -3;       /* should never happen */
     
    39904471        }
    39914472        if ((len-1 == hinf->term_len) && (strncmp(s, hinf->term, len-1) == 0)) {
    3992           if (c < 0) {
    3993             p->parsing_heredoc = NULL;
    3994           }
    3995           else {
    3996             return tHEREDOC_END;
    3997           }
     4473          return tHEREDOC_END;
    39984474        }
    39994475      }
    40004476      if (c < 0) {
    40014477        char buf[256];
    4002         snprintf(buf, sizeof(buf), "can't find heredoc delimiter \"%s\" anywhere before EOF", hinf->term);
    4003         yyerror(p, buf);
     4478        const char s1[] = "can't find heredoc delimiter \"";
     4479        const char s2[] = "\" anywhere before EOF";
     4480
     4481        if (sizeof(s1)+sizeof(s2)+strlen(hinf->term)+1 >= sizeof(buf)) {
     4482          yyerror(p, "can't find heredoc delimiter anywhere before EOF");
     4483        } else {
     4484          strcpy(buf, s1);
     4485          strcat(buf, hinf->term);
     4486          strcat(buf, s2);
     4487          yyerror(p, buf);
     4488        }
    40044489        return 0;
    40054490      }
     
    40134498    else if (c == beg) {
    40144499      nest_level++;
    4015       p->lex_strterm->cdr->car = (node*)(intptr_t)nest_level;
     4500      p->lex_strterm->cdr->car = nint(nest_level);
    40164501    }
    40174502    else if (c == end) {
    40184503      nest_level--;
    4019       p->lex_strterm->cdr->car = (node*)(intptr_t)nest_level;
     4504      p->lex_strterm->cdr->car = nint(nest_level);
    40204505    }
    40214506    else if (c == '\\') {
     
    41194604
    41204605  tokfix(p);
    4121   p->lstate = EXPR_END;
     4606  p->lstate = EXPR_ENDARG;
    41224607  end_strterm(p);
    41234608
     
    41454630      case 'u': f |= 16; break;
    41464631      case 'n': f |= 32; break;
     4632      case 'o': break;
    41474633      default: tokadd(p, re_opt); break;
    41484634      }
     
    41514637    if (toklen(p)) {
    41524638      char msg[128];
     4639
     4640      strcpy(msg, "unknown regexp option");
    41534641      tokfix(p);
    4154       snprintf(msg, sizeof(msg), "unknown regexp option%s - %s",
    4155           toklen(p) > 1 ? "s" : "", tok(p));
     4642      if (toklen(p) > 1) {
     4643        strcat(msg, "s");
     4644      }
     4645      strcat(msg, " - ");
     4646      strncat(msg, tok(p), sizeof(msg) - strlen(msg) - 1);
    41564647      yyerror(p, msg);
    41574648    }
     
    41804671  }
    41814672  pylval.nd = new_str(p, tok(p), toklen(p));
    4182   if (IS_LABEL_POSSIBLE()) {
    4183     if (IS_LABEL_SUFFIX(0)) {
    4184       p->lstate = EXPR_BEG;
    4185       nextc(p);
    4186       return tLABEL_END;
    4187     }
    4188   }
    41894673
    41904674  return tSTRING;
    41914675}
    41924676
     4677static int
     4678number_literal_suffix(parser_state *p)
     4679{
     4680  int c, result = 0;
     4681  node *list = 0;
     4682  int column = p->column;
     4683  int mask = NUM_SUFFIX_R|NUM_SUFFIX_I;
     4684
     4685  while ((c = nextc(p)) != -1) {
     4686    list = push(list, (node*)(intptr_t)c);
     4687
     4688    if ((mask & NUM_SUFFIX_I) && c == 'i') {
     4689      result |= (mask & NUM_SUFFIX_I);
     4690      mask &= ~NUM_SUFFIX_I;
     4691      /* r after i, rational of complex is disallowed */
     4692      mask &= ~NUM_SUFFIX_R;
     4693      continue;
     4694    }
     4695    if ((mask & NUM_SUFFIX_R) && c == 'r') {
     4696      result |= (mask & NUM_SUFFIX_R);
     4697      mask &= ~NUM_SUFFIX_R;
     4698      continue;
     4699    }
     4700    if (!ISASCII(c) || ISALPHA(c) || c == '_') {
     4701      p->column = column;
     4702      if (p->pb) {
     4703        p->pb = append((node*)list, p->pb);
     4704      }
     4705      else {
     4706        p->pb = list;
     4707      }
     4708      return 0;
     4709    }
     4710    pushback(p, c);
     4711    break;
     4712  }
     4713  return result;
     4714}
    41934715
    41944716static int
     
    43114833  case -2:      /* end of a file */
    43124834  case '\n':
    4313     maybe_heredoc:
     4835  maybe_heredoc:
    43144836    heredoc_treat_nextline(p);
    4315   switch (p->lstate) {
    4316   case EXPR_BEG:
    4317   case EXPR_FNAME:
    4318   case EXPR_DOT:
    4319   case EXPR_CLASS:
    4320   case EXPR_VALUE:
    4321     p->lineno++;
    43224837    p->column = 0;
     4838    switch (p->lstate) {
     4839    case EXPR_BEG:
     4840    case EXPR_FNAME:
     4841    case EXPR_DOT:
     4842    case EXPR_CLASS:
     4843    case EXPR_VALUE:
     4844      p->lineno++;
     4845      if (p->parsing_heredoc != NULL) {
     4846        if (p->lex_strterm) {
     4847          return parse_string(p);
     4848        }
     4849      }
     4850      goto retry;
     4851    default:
     4852      break;
     4853    }
    43234854    if (p->parsing_heredoc != NULL) {
    4324       if (p->lex_strterm) {
    4325         return parse_string(p);
    4326       }
    4327     }
    4328     goto retry;
    4329   default:
    4330     break;
    4331   }
    4332   if (p->parsing_heredoc != NULL) {
     4855      return '\n';
     4856    }
     4857    while ((c = nextc(p))) {
     4858      switch (c) {
     4859      case ' ': case '\t': case '\f': case '\r':
     4860      case '\13': /* '\v' */
     4861        space_seen = 1;
     4862        break;
     4863      case '#': /* comment as a whitespace */
     4864        pushback(p, '#');
     4865        p->lineno++;
     4866        goto retry;
     4867      case '.':
     4868        if (!peek(p, '.')) {
     4869          pushback(p, '.');
     4870          p->lineno++;
     4871          goto retry;
     4872        }
     4873        pushback(p, c);
     4874        goto normal_newline;
     4875      case '&':
     4876        if (peek(p, '.')) {
     4877          pushback(p, '&');
     4878          p->lineno++;
     4879          goto retry;
     4880        }
     4881        pushback(p, c);
     4882        goto normal_newline;
     4883      case -1:                  /* EOF */
     4884      case -2:                  /* end of a file */
     4885        goto normal_newline;
     4886      default:
     4887        pushback(p, c);
     4888        goto normal_newline;
     4889      }
     4890    }
     4891  normal_newline:
     4892    p->cmd_start = TRUE;
     4893    p->lstate = EXPR_BEG;
    43334894    return '\n';
    4334   }
    4335   while ((c = nextc(p))) {
    4336     switch (c) {
    4337     case ' ': case '\t': case '\f': case '\r':
    4338     case '\13': /* '\v' */
    4339       space_seen = 1;
    4340       break;
    4341     case '.':
    4342       if ((c = nextc(p)) != '.') {
    4343         pushback(p, c);
    4344         pushback(p, '.');
    4345         goto retry;
    4346       }
    4347     case -1:                  /* EOF */
    4348     case -2:                  /* end of a file */
    4349       goto normal_newline;
    4350     default:
    4351       pushback(p, c);
    4352       goto normal_newline;
    4353     }
    4354   }
    4355   normal_newline:
    4356   p->cmd_start = TRUE;
    4357   p->lstate = EXPR_BEG;
    4358   return '\n';
    43594895
    43604896  case '*':
    43614897    if ((c = nextc(p)) == '*') {
    43624898      if ((c = nextc(p)) == '=') {
    4363         pylval.id = intern("**",2);
     4899        pylval.id = intern_lit("**");
    43644900        p->lstate = EXPR_BEG;
    43654901        return tOP_ASGN;
    43664902      }
    43674903      pushback(p, c);
    4368       c = tPOW;
     4904      if (IS_SPCARG(c)) {
     4905        yywarning(p, "'**' interpreted as argument prefix");
     4906        c = tDSTAR;
     4907      }
     4908      else if (IS_BEG()) {
     4909        c = tDSTAR;
     4910      }
     4911      else {
     4912        c = tPOW; /* "**", "argument prefix" */
     4913      }
    43694914    }
    43704915    else {
    43714916      if (c == '=') {
    4372         pylval.id = intern_c('*');
     4917        pylval.id = intern_lit("*");
    43734918        p->lstate = EXPR_BEG;
    43744919        return tOP_ASGN;
     
    44865031    if (c == '<') {
    44875032      if ((c = nextc(p)) == '=') {
    4488         pylval.id = intern("<<",2);
     5033        pylval.id = intern_lit("<<");
    44895034        p->lstate = EXPR_BEG;
    44905035        return tOP_ASGN;
     
    45085053    if (c == '>') {
    45095054      if ((c = nextc(p)) == '=') {
    4510         pylval.id = intern(">>",2);
     5055        pylval.id = intern_lit(">>");
    45115056        p->lstate = EXPR_BEG;
    45125057        return tOP_ASGN;
     
    45795124        if (c2) {
    45805125          char buf[256];
    4581           snprintf(buf, sizeof(buf), "invalid character syntax; use ?\\%c", c2);
     5126          char cc[] = { (char)c2, '\0' };
     5127
     5128          strcpy(buf, "invalid character syntax; use ?\\");
     5129          strncat(buf, cc, 2);
    45825130          yyerror(p, buf);
    45835131        }
     
    45905138    newtok(p);
    45915139    /* need support UTF-8 if configured */
    4592     if ((isalnum(c) || c == '_')) {
     5140    if ((ISALNUM(c) || c == '_')) {
    45935141      int c2 = nextc(p);
    45945142      pushback(p, c2);
    4595       if ((isalnum(c2) || c2 == '_')) {
     5143      if ((ISALNUM(c2) || c2 == '_')) {
    45965144        goto ternary;
    45975145      }
     
    46065154    tokfix(p);
    46075155    pylval.nd = new_str(p, tok(p), toklen(p));
    4608     p->lstate = EXPR_END;
     5156    p->lstate = EXPR_ENDARG;
    46095157    return tCHAR;
    46105158
     
    46135161      p->lstate = EXPR_BEG;
    46145162      if ((c = nextc(p)) == '=') {
    4615         pylval.id = intern("&&",2);
     5163        pylval.id = intern_lit("&&");
    46165164        p->lstate = EXPR_BEG;
    46175165        return tOP_ASGN;
     
    46255173    }
    46265174    else if (c == '=') {
    4627       pylval.id = intern_c('&');
     5175      pylval.id = intern_lit("&");
    46285176      p->lstate = EXPR_BEG;
    46295177      return tOP_ASGN;
     
    46525200      p->lstate = EXPR_BEG;
    46535201      if ((c = nextc(p)) == '=') {
    4654         pylval.id = intern("||",2);
     5202        pylval.id = intern_lit("||");
    46555203        p->lstate = EXPR_BEG;
    46565204        return tOP_ASGN;
     
    46605208    }
    46615209    if (c == '=') {
    4662       pylval.id = intern_c('|');
     5210      pylval.id = intern_lit("|");
    46635211      p->lstate = EXPR_BEG;
    46645212      return tOP_ASGN;
     
    46845232    }
    46855233    if (c == '=') {
    4686       pylval.id = intern_c('+');
     5234      pylval.id = intern_lit("+");
    46875235      p->lstate = EXPR_BEG;
    46885236      return tOP_ASGN;
     
    47125260    }
    47135261    if (c == '=') {
    4714       pylval.id = intern_c('-');
     5262      pylval.id = intern_lit("-");
    47155263      p->lstate = EXPR_BEG;
    47165264      return tOP_ASGN;
     
    47535301  {
    47545302    int is_float, seen_point, seen_e, nondigit;
     5303    int suffix = 0;
    47555304
    47565305    is_float = seen_point = seen_e = nondigit = 0;
    4757     p->lstate = EXPR_END;
     5306    p->lstate = EXPR_ENDARG;
    47585307    newtok(p);
    47595308    if (c == '-' || c == '+') {
     
    47865335        }
    47875336        else if (nondigit) goto trailing_uc;
    4788         pylval.nd = new_int(p, tok(p), 16);
     5337        suffix = number_literal_suffix(p);
     5338        pylval.nd = new_int(p, tok(p), 16, suffix);
    47895339        return tINTEGER;
    47905340      }
     
    48105360        }
    48115361        else if (nondigit) goto trailing_uc;
    4812         pylval.nd = new_int(p, tok(p), 2);
     5362        suffix = number_literal_suffix(p);
     5363        pylval.nd = new_int(p, tok(p), 2, suffix);
    48135364        return tINTEGER;
    48145365      }
     
    48345385        }
    48355386        else if (nondigit) goto trailing_uc;
    4836         pylval.nd = new_int(p, tok(p), 10);
     5387        suffix = number_literal_suffix(p);
     5388        pylval.nd = new_int(p, tok(p), 10, suffix);
    48375389        return tINTEGER;
    48385390      }
     
    48675419          tokfix(p);
    48685420          if (nondigit) goto trailing_uc;
    4869           pylval.nd = new_int(p, tok(p), 8);
     5421          suffix = number_literal_suffix(p);
     5422          pylval.nd = new_int(p, tok(p), 8, suffix);
    48705423          return tINTEGER;
    48715424        }
     
    48845437      else {
    48855438        pushback(p, c);
    4886         pylval.nd = new_int(p, "0", 10);
     5439        suffix = number_literal_suffix(p);
     5440        pylval.nd = new_int(p, "0", 10, suffix);
    48875441        return tINTEGER;
    48885442      }
     
    49525506    if (nondigit) {
    49535507      trailing_uc:
    4954       yyerror_i(p, "trailing '%c' in number", nondigit);
     5508      yyerror_c(p, "trailing non digit in number: ", (char)nondigit);
    49555509    }
    49565510    tokfix(p);
    49575511    if (is_float) {
     5512#ifdef MRB_WITHOUT_FLOAT
     5513      yywarning_s(p, "floating point numbers are not supported", tok(p));
     5514      pylval.nd = new_int(p, "0", 10, 0);
     5515      return tINTEGER;
     5516#else
    49585517      double d;
    49595518      char *endp;
     
    49625521      d = mrb_float_read(tok(p), &endp);
    49635522      if (d == 0 && endp == tok(p)) {
    4964         yywarning_s(p, "corrupted float value %s", tok(p));
     5523        yywarning_s(p, "corrupted float value", tok(p));
    49655524      }
    49665525      else if (errno == ERANGE) {
    4967         yywarning_s(p, "float %s out of range", tok(p));
     5526        yywarning_s(p, "float out of range", tok(p));
    49685527        errno = 0;
    49695528      }
    4970       pylval.nd = new_float(p, tok(p));
     5529      suffix = number_literal_suffix(p);
     5530      pylval.nd = new_float(p, tok(p), suffix);
    49715531      return tFLOAT;
    4972     }
    4973     pylval.nd = new_int(p, tok(p), 10);
     5532#endif
     5533    }
     5534    suffix = number_literal_suffix(p);
     5535    pylval.nd = new_int(p, tok(p), 10, suffix);
    49745536    return tINTEGER;
    49755537  }
     
    49855547      p->lstate = EXPR_ENDFN;
    49865548    else
    4987       p->lstate = EXPR_ENDARG;
     5549      p->lstate = EXPR_END;
    49885550    return c;
    49895551
     
    49985560      return tCOLON2;
    49995561    }
    5000     if (IS_END() || ISSPACE(c)) {
     5562    if (!space_seen && IS_END()) {
    50015563      pushback(p, c);
    50025564      p->lstate = EXPR_BEG;
    5003       return ':';
     5565      return tLABEL_TAG;
     5566    }
     5567    if (!ISSPACE(c) || IS_BEG()) {
     5568      pushback(p, c);
     5569      p->lstate = EXPR_FNAME;
     5570      return tSYMBEG;
    50045571    }
    50055572    pushback(p, c);
    5006     p->lstate = EXPR_FNAME;
    5007     return tSYMBEG;
     5573    p->lstate = EXPR_BEG;
     5574    return ':';
    50085575
    50095576  case '/':
     
    50135580    }
    50145581    if ((c = nextc(p)) == '=') {
    5015       pylval.id = intern_c('/');
     5582      pylval.id = intern_lit("/");
    50165583      p->lstate = EXPR_BEG;
    50175584      return tOP_ASGN;
     
    50325599  case '^':
    50335600    if ((c = nextc(p)) == '=') {
    5034       pylval.id = intern_c('^');
     5601      pylval.id = intern_lit("^");
    50355602      p->lstate = EXPR_BEG;
    50365603      return tOP_ASGN;
     
    50705637    }
    50715638    else if (IS_SPCARG(-1)) {
     5639      c = tLPAREN_ARG;
     5640    }
     5641    else if (p->lstate == EXPR_END && space_seen) {
    50725642      c = tLPAREN_ARG;
    50735643    }
     
    51475717      else {
    51485718        term = nextc(p);
    5149         if (isalnum(term)) {
     5719        if (ISALNUM(term)) {
    51505720          yyerror(p, "unknown type of %string");
    51515721          return 0;
     
    52065776    }
    52075777    if ((c = nextc(p)) == '=') {
    5208       pylval.id = intern_c('%');
     5778      pylval.id = intern_lit("%");
    52095779      p->lstate = EXPR_BEG;
    52105780      return tOP_ASGN;
     
    52605830      tokadd(p, c);
    52615831      tokfix(p);
    5262       pylval.id = intern_cstr(tok(p));
     5832      pylval.id = intern(tok(p), toklen(p));
    52635833      return tGVAR;
    52645834
     
    52705840      gvar:
    52715841      tokfix(p);
    5272       pylval.id = intern_cstr(tok(p));
     5842      pylval.id = intern(tok(p), toklen(p));
    52735843      return tGVAR;
    52745844
     
    52915861        tokadd(p, c);
    52925862        c = nextc(p);
    5293       } while (c >= 0 && isdigit(c));
     5863      } while (c >= 0 && ISDIGIT(c));
    52945864      pushback(p, c);
    52955865      if (last_state == EXPR_FNAME) goto gvar;
     
    52985868        unsigned long n = strtoul(tok(p), NULL, 10);
    52995869        if (n > INT_MAX) {
    5300           yyerror_i(p, "capture group index must be <= %d", INT_MAX);
     5870          yyerror(p, "capture group index must be <= " MRB_STRINGIZE(INT_MAX));
    53015871          return 0;
    53025872        }
     
    53335903        return 0;
    53345904      }
    5335       else if (isdigit(c)) {
     5905      else if (ISDIGIT(c)) {
    53365906        if (p->tidx == 1) {
    5337           yyerror_i(p, "'@%c' is not allowed as an instance variable name", c);
     5907          yyerror_c(p, "wrong instance variable name: @", c);
    53385908        }
    53395909        else {
    5340           yyerror_i(p, "'@@%c' is not allowed as a class variable name", c);
     5910          yyerror_c(p, "wrong class variable name: @@", c);
    53415911        }
    53425912        return 0;
     
    53545924    default:
    53555925      if (!identchar(c)) {
    5356         yyerror_i(p,  "Invalid char '\\x%02X' in expression", c);
     5926        char buf[36];
     5927        const char s[] = "Invalid char in expression: 0x";
     5928        const char hexdigits[] = "0123456789ABCDEF";
     5929
     5930        strcpy(buf, s);
     5931        buf[sizeof(s)-1] = hexdigits[(c & 0xf0) >> 4];
     5932        buf[sizeof(s)]   = hexdigits[(c & 0x0f)];
     5933        buf[sizeof(s)+1] = 0;
     5934        yyerror(p, buf);
    53575935        goto retry;
    53585936      }
     
    54005978      break;
    54015979
     5980    case '_':
     5981      if (toklen(p) == 2 && ISDIGIT(tok(p)[1]) && p->nvars) {
     5982        int n = tok(p)[1] - '0';
     5983        int nvar;
     5984
     5985        if (n > 0) {
     5986          node *nvars = p->nvars->cdr;
     5987
     5988          while (nvars) {
     5989            nvar = intn(nvars->car);
     5990            if (nvar == -2) break; /* top of the scope */
     5991            if (nvar > 0) {
     5992              yywarning(p, "numbered parameter used in outer block");
     5993              break;
     5994            }
     5995            nvars->car = nint(-1);
     5996            nvars = nvars->cdr;
     5997          }
     5998          nvar = intn(p->nvars->car);
     5999          if (nvar == -1) {
     6000            yywarning(p, "numbered parameter used in inner block");
     6001          }
     6002          if (nvar >= -1) {
     6003            pylval.num = n;
     6004            p->lstate = EXPR_END;
     6005            return tNUMPARAM;
     6006          }
     6007          else {
     6008            yywarning(p, "identifier for numbered parameter; consider another name");
     6009          }
     6010        }
     6011      }
     6012      /* fall through */
    54026013    default:
    54036014      if (toklast(p) == '!' || toklast(p) == '?') {
     
    54066017      else {
    54076018        if (p->lstate == EXPR_FNAME) {
     6019          if ((c = nextc(p)) == '=' && !peek(p, '~') && !peek(p, '>') &&
     6020              (!peek(p, '=') || (peek_n(p, '>', 1)))) {
     6021            result = tIDENTIFIER;
     6022            tokadd(p, c);
     6023            tokfix(p);
     6024          }
     6025          else {
     6026            pushback(p, c);
     6027          }
    54086028          if ((c = nextc(p)) == '=' && !peek(p, '~') && !peek(p, '>') &&
    54096029              (!peek(p, '=') || (peek_n(p, '>', 1)))) {
     
    54266046      if (IS_LABEL_POSSIBLE()) {
    54276047        if (IS_LABEL_SUFFIX(0)) {
    5428           p->lstate = EXPR_BEG;
    5429           nextc(p);
     6048          p->lstate = EXPR_END;
    54306049          tokfix(p);
    5431           pylval.id = intern_cstr(tok(p));
    5432           return tLABEL;
     6050          pylval.id = intern(tok(p), toklen(p));
     6051          return tIDENTIFIER;
    54336052        }
    54346053      }
     
    54886107    }
    54896108    {
    5490       mrb_sym ident = intern_cstr(tok(p));
     6109      mrb_sym ident = intern(tok(p), toklen(p));
    54916110
    54926111      pylval.id = ident;
    5493 #if 0
    5494       if (last_state != EXPR_DOT && islower(tok(p)[0]) && lvar_defined(ident)) {
     6112      if (last_state != EXPR_DOT && ISLOWER(tok(p)[0]) && local_var_p(p, ident)) {
    54956113        p->lstate = EXPR_END;
    54966114      }
    5497 #endif
    54986115    }
    54996116    return result;
     
    55246141  p->capture_errors = cxt->capture_errors;
    55256142  p->no_optimize = cxt->no_optimize;
     6143  p->on_eval = cxt->on_eval;
    55266144  if (cxt->partial_hook) {
    55276145    p->cxt = cxt;
     
    55366154
    55376155  if (!cxt) return;
    5538   if ((int)(intptr_t)p->tree->car != NODE_SCOPE) return;
     6156  if (intn(p->tree->car) != NODE_SCOPE) return;
    55396157  n0 = n = p->tree->cdr->car;
    55406158  while (n) {
     
    55596177
    55606178  MRB_TRY(p->jmp) {
    5561     int n;
     6179    int n = 1;
    55626180
    55636181    p->cmd_start = TRUE;
     
    56756293{
    56766294  if (s) {
    5677     int len = strlen(s);
     6295    size_t len = strlen(s);
    56786296    char *p = (char *)mrb_malloc(mrb, len + 1);
    56796297
     
    57026320
    57036321  sym = mrb_intern_cstr(p->mrb, f);
    5704   p->filename = mrb_sym2name_len(p->mrb, sym, NULL);
     6322  p->filename_sym = sym;
    57056323  p->lineno = (p->filename_table_length > 0)? 0 : 1;
    57066324
    57076325  for (i = 0; i < p->filename_table_length; ++i) {
    57086326    if (p->filename_table[i] == sym) {
    5709       p->current_filename_index = i;
     6327      p->current_filename_index = (int)i;
    57106328      return;
    57116329    }
    57126330  }
    57136331
     6332  if (p->filename_table_length == UINT16_MAX) {
     6333    yyerror(p, "too many files to compile");
     6334    return;
     6335  }
    57146336  p->current_filename_index = p->filename_table_length++;
    57156337
    57166338  new_table = (mrb_sym*)parser_palloc(p, sizeof(mrb_sym) * p->filename_table_length);
    57176339  if (p->filename_table) {
    5718     memmove(new_table, p->filename_table, sizeof(mrb_sym) * p->filename_table_length);
     6340    memmove(new_table, p->filename_table, sizeof(mrb_sym) * p->current_filename_index);
    57196341  }
    57206342  p->filename_table = new_table;
     
    57226344}
    57236345
    5724 MRB_API char const*
     6346MRB_API mrb_sym
    57256347mrb_parser_get_filename(struct mrb_parser_state* p, uint16_t idx) {
    5726   if (idx >= p->filename_table_length) { return NULL; }
     6348  if (idx >= p->filename_table_length) return 0;
    57276349  else {
    5728     return mrb_sym2name_len(p->mrb, p->filename_table[idx], NULL);
     6350    return p->filename_table[idx];
    57296351  }
    57306352}
     
    57476369
    57486370MRB_API parser_state*
    5749 mrb_parse_nstring(mrb_state *mrb, const char *s, int len, mrbc_context *c)
     6371mrb_parse_nstring(mrb_state *mrb, const char *s, size_t len, mrbc_context *c)
    57506372{
    57516373  parser_state *p;
     
    57786400  }
    57796401  if (!p->tree || p->nerr) {
     6402    if (c) c->parser_nerr = p->nerr;
    57806403    if (p->capture_errors) {
    57816404      char buf[256];
    5782       int n;
    5783 
    5784       n = snprintf(buf, sizeof(buf), "line %d: %s\n",
    5785           p->error_buffer[0].lineno, p->error_buffer[0].message);
    5786       mrb->exc = mrb_obj_ptr(mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, n));
     6405
     6406      strcpy(buf, "line ");
     6407      dump_int(p->error_buffer[0].lineno, buf+5);
     6408      strcat(buf, ": ");
     6409      strncat(buf, p->error_buffer[0].message, sizeof(buf) - strlen(buf) - 1);
     6410      mrb->exc = mrb_obj_ptr(mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, strlen(buf)));
    57876411      mrb_parser_free(p);
    57886412      return mrb_undef_value();
     
    58176441    }
    58186442  }
    5819   proc->target_class = target;
     6443  MRB_PROC_SET_TARGET_CLASS(proc, target);
    58206444  if (mrb->c->ci) {
    58216445    mrb->c->ci->target_class = target;
     
    58416465
    58426466MRB_API mrb_value
    5843 mrb_load_nstring_cxt(mrb_state *mrb, const char *s, int len, mrbc_context *c)
     6467mrb_load_nstring_cxt(mrb_state *mrb, const char *s, size_t len, mrbc_context *c)
    58446468{
    58456469  return mrb_load_exec(mrb, mrb_parse_nstring(mrb, s, len, c), c);
     
    58476471
    58486472MRB_API mrb_value
    5849 mrb_load_nstring(mrb_state *mrb, const char *s, int len)
     6473mrb_load_nstring(mrb_state *mrb, const char *s, size_t len)
    58506474{
    58516475  return mrb_load_nstring_cxt(mrb, s, len, NULL);
     
    58856509}
    58866510
     6511static void
     6512dump_args(mrb_state *mrb, node *n, int offset)
     6513{
     6514  if (n->car) {
     6515    dump_prefix(n, offset+1);
     6516    printf("mandatory args:\n");
     6517    dump_recur(mrb, n->car, offset+2);
     6518  }
     6519  n = n->cdr;
     6520  if (n->car) {
     6521    dump_prefix(n, offset+1);
     6522    printf("optional args:\n");
     6523    {
     6524      node *n2 = n->car;
     6525
     6526      while (n2) {
     6527        dump_prefix(n2, offset+2);
     6528        printf("%s=\n", mrb_sym_name(mrb, sym(n2->car->car)));
     6529        mrb_parser_dump(mrb, n2->car->cdr, offset+3);
     6530        n2 = n2->cdr;
     6531      }
     6532    }
     6533  }
     6534  n = n->cdr;
     6535  if (n->car) {
     6536    dump_prefix(n, offset+1);
     6537    printf("rest=*%s\n", mrb_sym_name(mrb, sym(n->car)));
     6538  }
     6539  n = n->cdr;
     6540  if (n->car) {
     6541    dump_prefix(n, offset+1);
     6542    printf("post mandatory args:\n");
     6543    dump_recur(mrb, n->car, offset+2);
     6544  }
     6545
     6546  n = n->cdr;
     6547  if (n) {
     6548    mrb_assert(intn(n->car) == NODE_ARGS_TAIL);
     6549    mrb_parser_dump(mrb, n, offset);
     6550  }
     6551}
     6552
     6553/*
     6554 * This function restores the GC arena on return.
     6555 * For this reason, if a process that further generates an object is
     6556 * performed at the caller, the string pointer returned as the return
     6557 * value may become invalid.
     6558 */
     6559static const char*
     6560str_dump(mrb_state *mrb, const char *str, int len)
     6561{
     6562  mrb_int ai = mrb_gc_arena_save(mrb);
     6563  mrb_value s;
     6564# if INT_MAX > MRB_INT_MAX / 4
     6565  /* check maximum length with "\xNN" charactor */
     6566  if (len > MRB_INT_MAX / 4) {
     6567    len = MRB_INT_MAX / 4;
     6568  }
     6569# endif
     6570  s = mrb_str_new(mrb, str, (mrb_int)len);
     6571  s = mrb_str_dump(mrb, s);
     6572  mrb_gc_arena_restore(mrb, ai);
     6573  return RSTRING_PTR(s);
     6574}
    58876575#endif
    58886576
     
    58966584  again:
    58976585  dump_prefix(tree, offset);
    5898   nodetype = (int)(intptr_t)tree->car;
     6586  nodetype = intn(tree->car);
    58996587  tree = tree->cdr;
    59006588  switch (nodetype) {
     
    59566644
    59576645  case NODE_LAMBDA:
    5958     printf("NODE_BLOCK:\n");
     6646    printf("NODE_LAMBDA:\n");
     6647    dump_prefix(tree, offset);
    59596648    goto block;
    59606649
     
    59646653    tree = tree->cdr;
    59656654    if (tree->car) {
    5966       node *n = tree->car;
    5967 
    5968       if (n->car) {
    5969         dump_prefix(n, offset+1);
    5970         printf("mandatory args:\n");
    5971         dump_recur(mrb, n->car, offset+2);
    5972       }
    5973       n = n->cdr;
    5974       if (n->car) {
    5975         dump_prefix(n, offset+1);
    5976         printf("optional args:\n");
    5977         {
    5978           node *n2 = n->car;
    5979 
    5980           while (n2) {
    5981             dump_prefix(n2, offset+2);
    5982             printf("%s=", mrb_sym2name(mrb, sym(n2->car->car)));
    5983             mrb_parser_dump(mrb, n2->car->cdr, 0);
    5984             n2 = n2->cdr;
    5985           }
    5986         }
    5987       }
    5988       n = n->cdr;
    5989       if (n->car) {
    5990         dump_prefix(n, offset+1);
    5991         printf("rest=*%s\n", mrb_sym2name(mrb, sym(n->car)));
    5992       }
    5993       n = n->cdr;
    5994       if (n->car) {
    5995         dump_prefix(n, offset+1);
    5996         printf("post mandatory args:\n");
    5997         dump_recur(mrb, n->car, offset+2);
    5998       }
    5999       if (n->cdr) {
    6000         dump_prefix(n, offset+1);
    6001         printf("blk=&%s\n", mrb_sym2name(mrb, sym(n->cdr)));
    6002       }
     6655      dump_args(mrb, tree->car, offset+1);
    60036656    }
    60046657    dump_prefix(tree, offset+1);
     
    61236776          if (n2->car) {
    61246777            if (!first_lval) printf(", ");
    6125             printf("%s", mrb_sym2name(mrb, sym(n2->car)));
     6778            printf("%s", mrb_sym_name(mrb, sym(n2->car)));
    61266779            first_lval = FALSE;
    61276780          }
     
    61516804    dump_prefix(tree, offset+1);
    61526805    printf("method='%s' (%d)\n",
    6153         mrb_sym2name(mrb, sym(tree->cdr->car)),
    6154         (int)(intptr_t)tree->cdr->car);
     6806        mrb_sym_dump(mrb, sym(tree->cdr->car)),
     6807        intn(tree->cdr->car));
    61556808    tree = tree->cdr->cdr->car;
    61566809    if (tree) {
     
    61826835    mrb_parser_dump(mrb, tree->car, offset+1);
    61836836    dump_prefix(tree, offset+1);
    6184     printf("::%s\n", mrb_sym2name(mrb, sym(tree->cdr)));
     6837    printf("::%s\n", mrb_sym_name(mrb, sym(tree->cdr)));
    61856838    break;
    61866839
    61876840  case NODE_COLON3:
    6188     printf("NODE_COLON3: ::%s\n", mrb_sym2name(mrb, sym(tree)));
     6841    printf("NODE_COLON3: ::%s\n", mrb_sym_name(mrb, sym(tree)));
    61896842    break;
    61906843
     
    61966849  case NODE_HASH:
    61976850    printf("NODE_HASH:\n");
     6851    while (tree) {
     6852      dump_prefix(tree, offset+1);
     6853      printf("key:\n");
     6854      mrb_parser_dump(mrb, tree->car->car, offset+2);
     6855      dump_prefix(tree, offset+1);
     6856      printf("value:\n");
     6857      mrb_parser_dump(mrb, tree->car->cdr, offset+2);
     6858      tree = tree->cdr;
     6859    }
     6860    break;
     6861
     6862  case NODE_KW_HASH:
     6863    printf("NODE_KW_HASH:\n");
    61986864    while (tree) {
    61996865      dump_prefix(tree, offset+1);
     
    62696935    tree = tree->cdr;
    62706936    dump_prefix(tree, offset+1);
    6271     printf("op='%s' (%d)\n", mrb_sym2name(mrb, sym(tree->car)), (int)(intptr_t)tree->car);
     6937    printf("op='%s' (%d)\n", mrb_sym_name(mrb, sym(tree->car)), intn(tree->car));
    62726938    tree = tree->cdr;
    62736939    mrb_parser_dump(mrb, tree->car, offset+1);
     
    63216987
    63226988  case NODE_LVAR:
    6323     printf("NODE_LVAR %s\n", mrb_sym2name(mrb, sym(tree)));
     6989    printf("NODE_LVAR %s\n", mrb_sym_name(mrb, sym(tree)));
    63246990    break;
    63256991
    63266992  case NODE_GVAR:
    6327     printf("NODE_GVAR %s\n", mrb_sym2name(mrb, sym(tree)));
     6993    printf("NODE_GVAR %s\n", mrb_sym_name(mrb, sym(tree)));
    63286994    break;
    63296995
    63306996  case NODE_IVAR:
    6331     printf("NODE_IVAR %s\n", mrb_sym2name(mrb, sym(tree)));
     6997    printf("NODE_IVAR %s\n", mrb_sym_name(mrb, sym(tree)));
    63326998    break;
    63336999
    63347000  case NODE_CVAR:
    6335     printf("NODE_CVAR %s\n", mrb_sym2name(mrb, sym(tree)));
     7001    printf("NODE_CVAR %s\n", mrb_sym_name(mrb, sym(tree)));
     7002    break;
     7003
     7004  case NODE_NVAR:
     7005    printf("NODE_NVAR %d\n", intn(tree));
    63367006    break;
    63377007
    63387008  case NODE_CONST:
    6339     printf("NODE_CONST %s\n", mrb_sym2name(mrb, sym(tree)));
     7009    printf("NODE_CONST %s\n", mrb_sym_name(mrb, sym(tree)));
    63407010    break;
    63417011
     
    63517021
    63527022  case NODE_BACK_REF:
    6353     printf("NODE_BACK_REF: $%c\n", (int)(intptr_t)tree);
     7023    printf("NODE_BACK_REF: $%c\n", intn(tree));
    63547024    break;
    63557025
    63567026  case NODE_NTH_REF:
    6357     printf("NODE_NTH_REF: $%" MRB_PRId "\n", (mrb_int)(intptr_t)tree);
     7027    printf("NODE_NTH_REF: $%d\n", intn(tree));
    63587028    break;
    63597029
    63607030  case NODE_ARG:
    6361     printf("NODE_ARG %s\n", mrb_sym2name(mrb, sym(tree)));
     7031    printf("NODE_ARG %s\n", mrb_sym_name(mrb, sym(tree)));
    63627032    break;
    63637033
     
    63687038
    63697039  case NODE_INT:
    6370     printf("NODE_INT %s base %d\n", (char*)tree->car, (int)(intptr_t)tree->cdr->car);
     7040    printf("NODE_INT %s base %d\n", (char*)tree->car, intn(tree->cdr->car));
    63717041    break;
    63727042
     
    63767046
    63777047  case NODE_NEGATE:
    6378     printf("NODE_NEGATE\n");
     7048    printf("NODE_NEGATE:\n");
    63797049    mrb_parser_dump(mrb, tree, offset+1);
    63807050    break;
    63817051
    63827052  case NODE_STR:
    6383     printf("NODE_STR \"%s\" len %d\n", (char*)tree->car, (int)(intptr_t)tree->cdr);
     7053    printf("NODE_STR %s len %d\n", str_dump(mrb, (char*)tree->car, intn(tree->cdr)), intn(tree->cdr));
    63847054    break;
    63857055
    63867056  case NODE_DSTR:
    6387     printf("NODE_DSTR\n");
     7057    printf("NODE_DSTR:\n");
    63887058    dump_recur(mrb, tree, offset+1);
    63897059    break;
    63907060
    63917061  case NODE_XSTR:
    6392     printf("NODE_XSTR \"%s\" len %d\n", (char*)tree->car, (int)(intptr_t)tree->cdr);
     7062    printf("NODE_XSTR %s len %d\n", str_dump(mrb, (char*)tree->car, intn(tree->cdr)), intn(tree->cdr));
    63937063    break;
    63947064
    63957065  case NODE_DXSTR:
    6396     printf("NODE_DXSTR\n");
     7066    printf("NODE_DXSTR:\n");
    63977067    dump_recur(mrb, tree, offset+1);
    63987068    break;
     
    64037073
    64047074  case NODE_DREGX:
    6405     printf("NODE_DREGX\n");
     7075    printf("NODE_DREGX:\n");
    64067076    dump_recur(mrb, tree->car, offset+1);
    64077077    dump_prefix(tree, offset);
     
    64187088
    64197089  case NODE_SYM:
    6420     printf("NODE_SYM :%s (%d)\n", mrb_sym2name(mrb, sym(tree)),
    6421            (int)(intptr_t)tree);
     7090    printf("NODE_SYM :%s (%d)\n", mrb_sym_dump(mrb, sym(tree)),
     7091           intn(tree));
     7092    break;
     7093
     7094  case NODE_DSYM:
     7095    printf("NODE_DSYM:\n");
     7096    mrb_parser_dump(mrb, tree, offset+1);
     7097    break;
     7098
     7099  case NODE_WORDS:
     7100    printf("NODE_WORDS:\n");
     7101    dump_recur(mrb, tree, offset+1);
     7102    break;
     7103
     7104  case NODE_SYMBOLS:
     7105    printf("NODE_SYMBOLS:\n");
     7106    dump_recur(mrb, tree, offset+1);
     7107    break;
     7108
     7109  case NODE_LITERAL_DELIM:
     7110    printf("NODE_LITERAL_DELIM\n");
    64227111    break;
    64237112
     
    64407129  case NODE_ALIAS:
    64417130    printf("NODE_ALIAS %s %s:\n",
    6442         mrb_sym2name(mrb, sym(tree->car)),
    6443         mrb_sym2name(mrb, sym(tree->cdr)));
     7131        mrb_sym_dump(mrb, sym(tree->car)),
     7132        mrb_sym_dump(mrb, sym(tree->cdr)));
    64447133    break;
    64457134
     
    64497138      node *t = tree;
    64507139      while (t) {
    6451         printf(" %s", mrb_sym2name(mrb, sym(t->car)));
     7140        printf(" %s", mrb_sym_dump(mrb, sym(t->car)));
    64527141        t = t->cdr;
    64537142      }
     
    64607149    if (tree->car->car == (node*)0) {
    64617150      dump_prefix(tree, offset+1);
    6462       printf(":%s\n", mrb_sym2name(mrb, sym(tree->car->cdr)));
     7151      printf(":%s\n", mrb_sym_name(mrb, sym(tree->car->cdr)));
    64637152    }
    64647153    else if (tree->car->car == (node*)1) {
    64657154      dump_prefix(tree, offset+1);
    6466       printf("::%s\n", mrb_sym2name(mrb, sym(tree->car->cdr)));
     7155      printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr)));
    64677156    }
    64687157    else {
    64697158      mrb_parser_dump(mrb, tree->car->car, offset+1);
    64707159      dump_prefix(tree, offset+1);
    6471       printf("::%s\n", mrb_sym2name(mrb, sym(tree->car->cdr)));
     7160      printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr)));
    64727161    }
    64737162    if (tree->cdr->car) {
     
    64857174    if (tree->car->car == (node*)0) {
    64867175      dump_prefix(tree, offset+1);
    6487       printf(":%s\n", mrb_sym2name(mrb, sym(tree->car->cdr)));
     7176      printf(":%s\n", mrb_sym_name(mrb, sym(tree->car->cdr)));
    64887177    }
    64897178    else if (tree->car->car == (node*)1) {
    64907179      dump_prefix(tree, offset+1);
    6491       printf("::%s\n", mrb_sym2name(mrb, sym(tree->car->cdr)));
     7180      printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr)));
    64927181    }
    64937182    else {
    64947183      mrb_parser_dump(mrb, tree->car->car, offset+1);
    64957184      dump_prefix(tree, offset+1);
    6496       printf("::%s\n", mrb_sym2name(mrb, sym(tree->car->cdr)));
     7185      printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr)));
    64977186    }
    64987187    dump_prefix(tree, offset+1);
     
    65127201    printf("NODE_DEF:\n");
    65137202    dump_prefix(tree, offset+1);
    6514     printf("%s\n", mrb_sym2name(mrb, sym(tree->car)));
     7203    printf("%s\n", mrb_sym_dump(mrb, sym(tree->car)));
    65157204    tree = tree->cdr;
    65167205    {
     
    65257214          if (n2->car) {
    65267215            if (!first_lval) printf(", ");
    6527             printf("%s", mrb_sym2name(mrb, sym(n2->car)));
     7216            printf("%s", mrb_sym_name(mrb, sym(n2->car)));
    65287217            first_lval = FALSE;
    65297218          }
     
    65357224    tree = tree->cdr;
    65367225    if (tree->car) {
    6537       node *n = tree->car;
    6538 
    6539       if (n->car) {
    6540         dump_prefix(n, offset+1);
    6541         printf("mandatory args:\n");
    6542         dump_recur(mrb, n->car, offset+2);
    6543       }
    6544       n = n->cdr;
    6545       if (n->car) {
    6546         dump_prefix(n, offset+1);
    6547         printf("optional args:\n");
    6548         {
    6549           node *n2 = n->car;
    6550 
    6551           while (n2) {
    6552             dump_prefix(n2, offset+2);
    6553             printf("%s=", mrb_sym2name(mrb, sym(n2->car->car)));
    6554             mrb_parser_dump(mrb, n2->car->cdr, 0);
    6555             n2 = n2->cdr;
    6556           }
    6557         }
    6558       }
    6559       n = n->cdr;
    6560       if (n->car) {
    6561         dump_prefix(n, offset+1);
    6562         printf("rest=*%s\n", mrb_sym2name(mrb, sym(n->car)));
    6563       }
    6564       n = n->cdr;
    6565       if (n->car) {
    6566         dump_prefix(n, offset+1);
    6567         printf("post mandatory args:\n");
    6568         dump_recur(mrb, n->car, offset+2);
    6569       }
    6570       if (n->cdr) {
    6571         dump_prefix(n, offset+1);
    6572         printf("blk=&%s\n", mrb_sym2name(mrb, sym(n->cdr)));
    6573       }
     7226      dump_args(mrb, tree->car, offset);
    65747227    }
    65757228    mrb_parser_dump(mrb, tree->cdr->car, offset+1);
     
    65817234    tree = tree->cdr;
    65827235    dump_prefix(tree, offset+1);
    6583     printf(":%s\n", mrb_sym2name(mrb, sym(tree->car)));
     7236    printf(":%s\n", mrb_sym_dump(mrb, sym(tree->car)));
    65847237    tree = tree->cdr->cdr;
    65857238    if (tree->car) {
    6586       node *n = tree->car;
    6587 
    6588       if (n->car) {
    6589         dump_prefix(n, offset+1);
    6590         printf("mandatory args:\n");
    6591         dump_recur(mrb, n->car, offset+2);
    6592       }
    6593       n = n->cdr;
    6594       if (n->car) {
    6595         dump_prefix(n, offset+1);
    6596         printf("optional args:\n");
    6597         {
    6598           node *n2 = n->car;
    6599 
    6600           while (n2) {
    6601             dump_prefix(n2, offset+2);
    6602             printf("%s=", mrb_sym2name(mrb, sym(n2->car->car)));
    6603             mrb_parser_dump(mrb, n2->car->cdr, 0);
    6604             n2 = n2->cdr;
    6605           }
    6606         }
    6607       }
    6608       n = n->cdr;
    6609       if (n->car) {
    6610         dump_prefix(n, offset+1);
    6611         printf("rest=*%s\n", mrb_sym2name(mrb, sym(n->car)));
    6612       }
    6613       n = n->cdr;
    6614       if (n->car) {
    6615         dump_prefix(n, offset+1);
    6616         printf("post mandatory args:\n");
    6617         dump_recur(mrb, n->car, offset+2);
    6618       }
    6619       n = n->cdr;
    6620       if (n) {
    6621         dump_prefix(n, offset+1);
    6622         printf("blk=&%s\n", mrb_sym2name(mrb, sym(n)));
    6623       }
     7239      dump_args(mrb, tree->car, offset+1);
    66247240    }
    66257241    tree = tree->cdr;
     
    66377253    break;
    66387254
     7255  case NODE_ARGS_TAIL:
     7256    printf("NODE_ARGS_TAIL:\n");
     7257    {
     7258      node *kws = tree->car;
     7259
     7260      while (kws) {
     7261        mrb_parser_dump(mrb, kws->car, offset+1);
     7262        kws = kws->cdr;
     7263      }
     7264    }
     7265    tree = tree->cdr;
     7266    if (tree->car) {
     7267      mrb_assert(intn(tree->car->car) == NODE_KW_REST_ARGS);
     7268      mrb_parser_dump(mrb, tree->car, offset+1);
     7269    }
     7270    tree = tree->cdr;
     7271    if (tree->car) {
     7272      dump_prefix(tree, offset+1);
     7273      printf("block='%s'\n", mrb_sym_name(mrb, sym(tree->car)));
     7274    }
     7275    break;
     7276
     7277  case NODE_KW_ARG:
     7278    printf("NODE_KW_ARG %s:\n", mrb_sym_name(mrb, sym(tree->car)));
     7279    mrb_parser_dump(mrb, tree->cdr->car, offset + 1);
     7280    break;
     7281
     7282  case NODE_KW_REST_ARGS:
     7283    printf("NODE_KW_REST_ARGS %s\n", mrb_sym_name(mrb, sym(tree)));
     7284    break;
     7285
    66397286  default:
    66407287    printf("node type: %d (0x%x)\n", nodetype, (unsigned)nodetype);
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/mrbgem.rake

    r331 r439  
    2424    end
    2525  end
    26   file objfile("#{current_build_dir}/core/y.tab") => lex_def
    2726
    2827  # Parser
    29   file "#{current_build_dir}/core/y.tab.c" => ["#{current_dir}/core/parse.y"] do |t|
     28  file "#{current_build_dir}/core/y.tab.c" => ["#{current_dir}/core/parse.y", lex_def] do |t|
     29    mkdir_p File.dirname t.name
    3030    yacc.run t.name, t.prerequisites.first
    3131  end
     
    3636  end
    3737
    38   file libfile("#{build.build_dir}/lib/libmruby_core") => core_objs
     38  file build.libmruby_core_static => core_objs
    3939  build.libmruby << core_objs
    4040end
Note: See TracChangeset for help on using the changeset viewer.