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:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-eval/src/eval.c

    r331 r439  
    1313get_closure_irep(mrb_state *mrb, int level)
    1414{
    15   struct mrb_context *c = mrb->c;
    16   struct REnv *e = c->ci[-1].proc->env;
    17   struct RProc *proc;
    18 
    19   if (level == 0) {
    20     proc = c->ci[-1].proc;
    21     if (MRB_PROC_CFUNC_P(proc)) {
    22       return NULL;
    23     }
    24     return proc->body.irep;
    25   }
    26 
    27   while (--level) {
    28     e = (struct REnv*)e->c;
    29     if (!e) return NULL;
    30   }
    31 
    32   if (!e) return NULL;
    33   if (!MRB_ENV_STACK_SHARED_P(e)) return NULL;
    34   c = e->cxt.c;
    35   proc = c->cibase[e->cioff].proc;
    36 
    37   if (!proc || MRB_PROC_CFUNC_P(proc)) {
     15  struct RProc *proc = mrb->c->ci[-1].proc;
     16
     17  while (level--) {
     18    if (!proc) return NULL;
     19    proc = proc->upper;
     20  }
     21  if (!proc) return NULL;
     22  if (MRB_PROC_CFUNC_P(proc)) {
    3823    return NULL;
    3924  }
     
    4126}
    4227
    43 static inline mrb_code
     28/* search for irep lev above the bottom */
     29static mrb_irep*
     30search_irep(mrb_irep *top, int bnest, int lev, mrb_irep *bottom)
     31{
     32  int i;
     33
     34  for (i=0; i<top->rlen; i++) {
     35    mrb_irep* tmp = top->reps[i];
     36
     37    if (tmp == bottom) return top;
     38    tmp = search_irep(tmp, bnest-1, lev, bottom);
     39    if (tmp) {
     40      if (bnest == lev) return top;
     41      return tmp;
     42    }
     43  }
     44  return NULL;
     45}
     46
     47static uint16_t
    4448search_variable(mrb_state *mrb, mrb_sym vsym, int bnest)
    4549{
     
    4953
    5054  for (level = 0; (virep = get_closure_irep(mrb, level)); level++) {
    51     if (!virep || virep->lv == NULL) {
     55    if (virep->lv == NULL) {
    5256      continue;
    5357    }
    5458    for (pos = 0; pos < virep->nlocals - 1; pos++) {
    5559      if (vsym == virep->lv[pos].name) {
    56         return (MKARG_B(pos + 1) | MKARG_C(level + bnest));
    57       }
    58     }
    59   }
    60 
     60        return (pos+1)<<8 | (level+bnest);
     61      }
     62    }
     63  }
     64
     65  return 0;
     66}
     67
     68static int
     69irep_argc(mrb_irep *irep)
     70{
     71  mrb_code c;
     72
     73  c = irep->iseq[0];
     74  if (c == OP_ENTER) {
     75    mrb_aspec ax = PEEK_W(irep->iseq+1);
     76    /* extra 1 means a slot for block */
     77    return MRB_ASPEC_REQ(ax)+MRB_ASPEC_OPT(ax)+MRB_ASPEC_REST(ax)+MRB_ASPEC_POST(ax)+1;
     78  }
    6179  return 0;
    6280}
     
    7189}
    7290
     91extern uint8_t mrb_insn_size[];
     92extern uint8_t mrb_insn_size1[];
     93extern uint8_t mrb_insn_size2[];
     94extern uint8_t mrb_insn_size3[];
     95
    7396static void
    74 patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest)
    75 {
    76   size_t i;
    77   mrb_code c;
    78   int argc = 0;
    79 
    80   for (i = 0; i < irep->ilen; i++) {
    81     c = irep->iseq[i];
    82     switch(GET_OPCODE(c)){
    83     case OP_ENTER:
    84       {
    85         mrb_aspec ax = GETARG_Ax(c);
    86         /* extra 1 means a slot for block */
    87         argc = MRB_ASPEC_REQ(ax)+MRB_ASPEC_OPT(ax)+MRB_ASPEC_REST(ax)+MRB_ASPEC_POST(ax)+1;
    88       }
    89       break;
    90 
     97patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest, mrb_irep *top)
     98{
     99  int i;
     100  uint32_t a;
     101  uint16_t b;
     102  uint8_t c;
     103  mrb_code insn;
     104  int argc = irep_argc(irep);
     105  mrb_code *iseq = (mrb_code *)irep->iseq;
     106
     107  mrb_assert((irep->flags & MRB_ISEQ_NO_FREE) == 0);
     108
     109  for (i = 0; i < irep->ilen; ) {
     110    insn = iseq[i];
     111    switch(insn){
    91112    case OP_EPUSH:
    92       patch_irep(mrb, irep->reps[GETARG_Bx(c)], bnest + 1);
     113      a = PEEK_B(iseq+i+1);
     114      patch_irep(mrb, irep->reps[a], bnest + 1, top);
    93115      break;
    94116
    95117    case OP_LAMBDA:
    96       {
    97         int arg_c = GETARG_c(c);
    98         if (arg_c & OP_L_CAPTURE) {
    99           patch_irep(mrb, irep->reps[GETARG_b(c)], bnest + 1);
    100         }
    101       }
     118    case OP_BLOCK:
     119      a = PEEK_B(iseq+i+1);
     120      b = PEEK_B(iseq+i+2);
     121      patch_irep(mrb, irep->reps[b], bnest + 1, top);
    102122      break;
    103123
    104124    case OP_SEND:
    105       if (GETARG_C(c) != 0) {
     125      b = PEEK_B(iseq+i+2);
     126      c = PEEK_B(iseq+i+3);
     127      if (c != 0) {
    106128        break;
    107129      }
    108       {
    109         mrb_code arg = search_variable(mrb, irep->syms[GETARG_B(c)], bnest);
     130      else {
     131        uint16_t arg = search_variable(mrb, irep->syms[b], bnest);
    110132        if (arg != 0) {
    111133          /* must replace */
    112           irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg;
     134          iseq[i] = OP_GETUPVAR;
     135          iseq[i+2] = arg >> 8;
     136          iseq[i+3] = arg & 0xff;
    113137        }
    114138      }
     
    116140
    117141    case OP_MOVE:
     142      a = PEEK_B(iseq+i+1);
     143      b = PEEK_B(iseq+i+2);
    118144      /* src part */
    119       if (potential_upvar_p(irep->lv, GETARG_B(c), argc, irep->nlocals)) {
    120         mrb_code arg = search_variable(mrb, irep->lv[GETARG_B(c) - 1].name, bnest);
     145      if (potential_upvar_p(irep->lv, b, argc, irep->nlocals)) {
     146        uint16_t arg = search_variable(mrb, irep->lv[b - 1].name, bnest);
    121147        if (arg != 0) {
    122148          /* must replace */
    123           irep->iseq[i] = MKOPCODE(OP_GETUPVAR) | MKARG_A(GETARG_A(c)) | arg;
     149          iseq[i] = insn = OP_GETUPVAR;
     150          iseq[i+2] = arg >> 8;
     151          iseq[i+3] = arg & 0xff;
    124152        }
    125153      }
    126154      /* dst part */
    127       if (potential_upvar_p(irep->lv, GETARG_A(c), argc, irep->nlocals)) {
    128         mrb_code arg = search_variable(mrb, irep->lv[GETARG_A(c) - 1].name, bnest);
     155      if (potential_upvar_p(irep->lv, a, argc, irep->nlocals)) {
     156        uint16_t arg = search_variable(mrb, irep->lv[a - 1].name, bnest);
    129157        if (arg != 0) {
    130158          /* must replace */
    131           irep->iseq[i] = MKOPCODE(OP_SETUPVAR) | MKARG_A(GETARG_B(c)) | arg;
    132         }
    133       }
    134       break;
    135 
    136     case OP_STOP:
    137       if (mrb->c->ci->acc >= 0) {
    138         irep->iseq[i] = MKOP_AB(OP_RETURN, irep->nlocals, OP_R_NORMAL);
    139       }
    140       break;
    141     }
     159          iseq[i] = insn = OP_SETUPVAR;
     160          iseq[i+1] = (mrb_code)b;
     161          iseq[i+2] = arg >> 8;
     162          iseq[i+3] = arg & 0xff;
     163        }
     164      }
     165      break;
     166
     167    case OP_GETUPVAR:
     168      a = PEEK_B(iseq+i+1);
     169      b = PEEK_B(iseq+i+2);
     170      c = PEEK_B(iseq+i+3);
     171      {
     172        int lev = c+1;
     173        mrb_irep *tmp = search_irep(top, bnest, lev, irep);
     174        if (potential_upvar_p(tmp->lv, b, irep_argc(tmp), tmp->nlocals)) {
     175          uint16_t arg = search_variable(mrb, tmp->lv[b-1].name, bnest);
     176          if (arg != 0) {
     177            /* must replace */
     178            iseq[i] = OP_GETUPVAR;
     179            iseq[i+2] = arg >> 8;
     180            iseq[i+3] = arg & 0xff;
     181          }
     182        }
     183      }
     184      break;
     185
     186    case OP_SETUPVAR:
     187      a = PEEK_B(iseq+i+1);
     188      b = PEEK_B(iseq+i+2);
     189      c = PEEK_B(iseq+i+3);
     190      {
     191        int lev = c+1;
     192        mrb_irep *tmp = search_irep(top, bnest, lev, irep);
     193        if (potential_upvar_p(tmp->lv, b, irep_argc(tmp), tmp->nlocals)) {
     194          uint16_t arg = search_variable(mrb, tmp->lv[b-1].name, bnest);
     195          if (arg != 0) {
     196            /* must replace */
     197            iseq[i] = OP_SETUPVAR;
     198            iseq[i+1] = a;
     199            iseq[i+2] = arg >> 8;
     200            iseq[i+3] = arg & 0xff;
     201          }
     202        }
     203      }
     204      break;
     205
     206    case OP_EXT1:
     207      insn = PEEK_B(iseq+i+1);
     208      i += mrb_insn_size1[insn]+1;
     209      continue;
     210    case OP_EXT2:
     211      insn = PEEK_B(iseq+i+1);
     212      i += mrb_insn_size2[insn]+1;
     213      continue;
     214    case OP_EXT3:
     215      insn = PEEK_B(iseq+i+1);
     216      i += mrb_insn_size3[insn]+1;
     217      continue;
     218    }
     219    i+=mrb_insn_size[insn];
    142220  }
    143221}
     
    146224
    147225static struct RProc*
    148 create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, const char *file, mrb_int line)
     226create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line)
    149227{
    150228  mrbc_context *cxt;
     
    152230  struct RProc *proc;
    153231  struct REnv *e;
    154   struct mrb_context *c = mrb->c;
     232  mrb_callinfo *ci; /* callinfo of eval caller */
     233  struct RClass *target_class = NULL;
     234  int bidx;
    155235
    156236  if (!mrb_nil_p(binding)) {
     
    159239
    160240  cxt = mrbc_context_new(mrb);
    161   cxt->lineno = line;
     241  cxt->lineno = (uint16_t)line;
    162242
    163243  mrbc_filename(mrb, cxt, file ? file : "(eval)");
    164244  cxt->capture_errors = TRUE;
    165245  cxt->no_optimize = TRUE;
     246  cxt->on_eval = TRUE;
    166247
    167248  p = mrb_parse_nstring(mrb, s, len, cxt);
     
    177258
    178259    if (file) {
    179       str = mrb_format(mrb, " file %S line %S: %S",
    180                        mrb_str_new_cstr(mrb, file),
    181                        mrb_fixnum_value(p->error_buffer[0].lineno),
    182                        mrb_str_new_cstr(mrb, p->error_buffer[0].message));
     260      str = mrb_format(mrb, "file %s line %d: %s",
     261                       file,
     262                       p->error_buffer[0].lineno,
     263                       p->error_buffer[0].message);
    183264    }
    184265    else {
    185       str = mrb_format(mrb, " line %S: %S",
    186                        mrb_fixnum_value(p->error_buffer[0].lineno),
    187                        mrb_str_new_cstr(mrb, p->error_buffer[0].message));
     266      str = mrb_format(mrb, "line %d: %s",
     267                       p->error_buffer[0].lineno,
     268                       p->error_buffer[0].message);
    188269    }
    189270    mrb_parser_free(p);
     
    199280    mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error");
    200281  }
    201   if (c->ci[-1].proc->target_class) {
    202     proc->target_class = c->ci[-1].proc->target_class;
    203   }
    204   e = c->ci[-1].proc->env;
    205   if (!e) e = c->ci[-1].env;
    206   e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, (struct RClass*)e);
    207   e->cxt.c = c;
    208   e->cioff = c->ci - c->cibase;
    209   e->stack = c->ci->stackent;
    210   MRB_SET_ENV_STACK_LEN(e, c->ci->proc->body.irep->nlocals);
    211   c->ci->target_class = proc->target_class;
    212   c->ci->env = 0;
    213   proc->env = e;
    214   patch_irep(mrb, proc->body.irep, 0);
     282  if (mrb->c->ci > mrb->c->cibase) {
     283    ci = &mrb->c->ci[-1];
     284  }
     285  else {
     286    ci = mrb->c->cibase;
     287  }
     288  if (ci->proc) {
     289    target_class = MRB_PROC_TARGET_CLASS(ci->proc);
     290  }
     291  if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
     292    if (ci->env) {
     293      e = ci->env;
     294    }
     295    else {
     296      e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV,
     297                                      (struct RClass*)target_class);
     298      e->mid = ci->mid;
     299      e->stack = ci[1].stackent;
     300      e->cxt = mrb->c;
     301      MRB_ENV_SET_STACK_LEN(e, ci->proc->body.irep->nlocals);
     302      bidx = ci->argc;
     303      if (ci->argc < 0) bidx = 2;
     304      else bidx += 1;
     305      MRB_ENV_SET_BIDX(e, bidx);
     306      ci->env = e;
     307    }
     308    proc->e.env = e;
     309    proc->flags |= MRB_PROC_ENVSET;
     310    mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e);
     311  }
     312  proc->upper = ci->proc;
     313  mrb->c->ci->target_class = target_class;
     314  patch_irep(mrb, proc->body.irep, 0, proc->body.irep);
     315  /* mrb_codedump_all(mrb, proc); */
    215316
    216317  mrb_parser_free(p);
     
    223324exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc)
    224325{
     326  /* no argument passed from eval() */
     327  mrb->c->ci->argc = 0;
    225328  if (mrb->c->ci->acc < 0) {
    226     mrb_value ret = mrb_top_run(mrb, proc, mrb->c->stack[0], 0);
     329    ptrdiff_t cioff = mrb->c->ci - mrb->c->cibase;
     330    mrb_value ret = mrb_top_run(mrb, proc, self, 0);
    227331    if (mrb->exc) {
    228332      mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
    229333    }
     334    mrb->c->ci = mrb->c->cibase + cioff;
    230335    return ret;
    231336  }
     337  /* clear block */
     338  mrb->c->stack[1] = mrb_nil_value();
    232339  return mrb_exec_irep(mrb, self, proc);
    233340}
     
    256363  mrb_int argc; mrb_value *argv;
    257364
    258   mrb_get_args(mrb, "*&", &argv, &argc, &b);
     365  mrb_get_args(mrb, "*!&", &argv, &argc, &b);
    259366
    260367  if (mrb_nil_p(b)) {
     
    269376    cv = mrb_singleton_class(mrb, self);
    270377    proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
    271     proc->target_class = mrb_class_ptr(cv);
    272     mrb->c->ci->env = NULL;
     378    MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv));
    273379    mrb_assert(!MRB_PROC_CFUNC_P(proc));
     380    mrb->c->ci->target_class = mrb_class_ptr(cv);
    274381    return exec_irep(mrb, self, proc);
    275382  }
     
    284391{
    285392  mrb_define_module_function(mrb, mrb->kernel_module, "eval", f_eval, MRB_ARGS_ARG(1, 3));
    286   mrb_define_method(mrb, mrb->kernel_module, "instance_eval", f_instance_eval, MRB_ARGS_ARG(1, 2));
     393  mrb_define_method(mrb, mrb_class_get(mrb, "BasicObject"), "instance_eval", f_instance_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK());
    287394}
    288395
Note: See TracChangeset for help on using the changeset viewer.