Ruby  2.0.0p195(2013-05-14revision40734)
compile.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  compile.c - ruby node tree -> VM instruction sequence
4 
5  $Author: nagachika $
6  created at: 04/01/01 03:42:15 JST
7 
8  Copyright (C) 2004-2007 Koichi Sasada
9 
10 **********************************************************************/
11 
12 #include "ruby/ruby.h"
13 #include "internal.h"
14 #include <math.h>
15 
16 #define USE_INSN_STACK_INCREASE 1
17 #include "vm_core.h"
18 #include "iseq.h"
19 #include "insns.inc"
20 #include "insns_info.inc"
21 
22 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
23 #define FIXNUM_INC(n, i) ((n)+(INT2FIX(i)&~FIXNUM_FLAG))
24 #define FIXNUM_OR(n, i) ((n)|INT2FIX(i))
25 
26 typedef struct iseq_link_element {
27  enum {
32  } type;
35 } LINK_ELEMENT;
36 
37 typedef struct iseq_link_anchor {
40 } LINK_ANCHOR;
41 
42 typedef struct iseq_label_data {
44  int label_no;
45  int position;
46  int sc_state;
47  int set;
48  int sp;
49 } LABEL;
50 
51 typedef struct iseq_insn_data {
53  enum ruby_vminsn_type insn_id;
54  unsigned int line_no;
56  int sc_state;
58 } INSN;
59 
60 typedef struct iseq_adjust_data {
63  int line_no;
64 } ADJUST;
65 
66 struct ensure_range {
69  struct ensure_range *next;
70 };
71 
76 };
77 
91 #ifndef CPDEBUG
92 #define CPDEBUG 0
93 #endif
94 
95 #if CPDEBUG >= 0
96 #define compile_debug CPDEBUG
97 #else
98 #define compile_debug iseq->compile_data->option->debug_level
99 #endif
100 
101 #if CPDEBUG
102 
103 #define compile_debug_print_indent(level) \
104  ruby_debug_print_indent((level), compile_debug, gl_node_level * 2)
105 
106 #define debugp(header, value) (void) \
107  (compile_debug_print_indent(1) && \
108  ruby_debug_print_value(1, compile_debug, (header), (value)))
109 
110 #define debugi(header, id) (void) \
111  (compile_debug_print_indent(1) && \
112  ruby_debug_print_id(1, compile_debug, (header), (id)))
113 
114 #define debugp_param(header, value) (void) \
115  (compile_debug_print_indent(1) && \
116  ruby_debug_print_value(1, compile_debug, (header), (value)))
117 
118 #define debugp_verbose(header, value) (void) \
119  (compile_debug_print_indent(2) && \
120  ruby_debug_print_value(2, compile_debug, (header), (value)))
121 
122 #define debugp_verbose_node(header, value) (void) \
123  (compile_debug_print_indent(10) && \
124  ruby_debug_print_value(10, compile_debug, (header), (value)))
125 
126 #define debug_node_start(node) ((void) \
127  (compile_debug_print_indent(1) && \
128  (ruby_debug_print_node(1, CPDEBUG, "", (NODE *)(node)), gl_node_level)), \
129  gl_node_level++)
130 
131 #define debug_node_end() gl_node_level --
132 
133 #else
134 
135 static inline ID
136 r_id(ID id)
137 {
138  return id;
139 }
140 
141 static inline VALUE
143 {
144  return value;
145 }
146 
147 #define debugi(header, id) r_id(id)
148 #define debugp(header, value) r_value(value)
149 #define debugp_verbose(header, value) r_value(value)
150 #define debugp_verbose_node(header, value) r_value(value)
151 #define debugp_param(header, value) r_value(value)
152 #define debug_node_start(node) ((void)0)
153 #define debug_node_end() ((void)0)
154 #endif
155 
156 #if CPDEBUG > 1 || CPDEBUG < 0
157 #define debugs if (compile_debug_print_indent(1)) ruby_debug_printf
158 #define debug_compile(msg, v) ((void)(compile_debug_print_indent(1) && fputs((msg), stderr)), (v))
159 #else
160 #define debugs if(0)printf
161 #define debug_compile(msg, v) (v)
162 #endif
163 
164 
165 /* create new label */
166 #define NEW_LABEL(l) new_label_body(iseq, (l))
167 
168 #define iseq_path(iseq) \
169  (((rb_iseq_t*)DATA_PTR(iseq))->location.path)
170 
171 #define iseq_absolute_path(iseq) \
172  (((rb_iseq_t*)DATA_PTR(iseq))->location.absolute_path)
173 
174 #define NEW_ISEQVAL(node, name, type, line_no) \
175  new_child_iseq(iseq, (node), (name), 0, (type), (line_no))
176 
177 #define NEW_CHILD_ISEQVAL(node, name, type, line_no) \
178  new_child_iseq(iseq, (node), (name), iseq->self, (type), (line_no))
179 
180 /* add instructions */
181 #define ADD_SEQ(seq1, seq2) \
182  APPEND_LIST((seq1), (seq2))
183 
184 /* add an instruction */
185 #define ADD_INSN(seq, line, insn) \
186  ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line), BIN(insn), 0))
187 
188 /* add an instruction with some operands (1, 2, 3, 5) */
189 #define ADD_INSN1(seq, line, insn, op1) \
190  ADD_ELEM((seq), (LINK_ELEMENT *) \
191  new_insn_body(iseq, (line), BIN(insn), 1, (VALUE)(op1)))
192 
193 /* add an instruction with label operand (alias of ADD_INSN1) */
194 #define ADD_INSNL(seq, line, insn, label) ADD_INSN1(seq, line, insn, label)
195 
196 #define ADD_INSN2(seq, line, insn, op1, op2) \
197  ADD_ELEM((seq), (LINK_ELEMENT *) \
198  new_insn_body(iseq, (line), BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
199 
200 #define ADD_INSN3(seq, line, insn, op1, op2, op3) \
201  ADD_ELEM((seq), (LINK_ELEMENT *) \
202  new_insn_body(iseq, (line), BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
203 
204 /* Specific Insn factory */
205 #define ADD_SEND(seq, line, id, argc) \
206  ADD_SEND_R((seq), (line), (id), (argc), (VALUE)Qfalse, (VALUE)INT2FIX(0))
207 
208 #define ADD_CALL_RECEIVER(seq, line) \
209  ADD_INSN((seq), (line), putself)
210 
211 #define ADD_CALL(seq, line, id, argc) \
212  ADD_SEND_R((seq), (line), (id), (argc), (VALUE)Qfalse, (VALUE)INT2FIX(VM_CALL_FCALL))
213 
214 #define ADD_CALL_WITH_BLOCK(seq, line, id, argc, block) \
215  ADD_SEND_R((seq), (line), (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL))
216 
217 #define ADD_SEND_R(seq, line, id, argc, block, flag) \
218  ADD_ELEM((seq), (LINK_ELEMENT *) \
219  new_insn_send(iseq, (line), \
220  (VALUE)(id), (VALUE)(argc), (VALUE)(block), (VALUE)(flag)))
221 
222 #define ADD_TRACE(seq, line, event) \
223  do { \
224  if ((event) == RUBY_EVENT_LINE && iseq->coverage && \
225  (line) != iseq->compile_data->last_coverable_line) { \
226  RARRAY_PTR(iseq->coverage)[(line) - 1] = INT2FIX(0); \
227  iseq->compile_data->last_coverable_line = (line); \
228  ADD_INSN1((seq), (line), trace, INT2FIX(RUBY_EVENT_COVERAGE)); \
229  } \
230  if (iseq->compile_data->option->trace_instruction) { \
231  ADD_INSN1((seq), (line), trace, INT2FIX(event)); \
232  } \
233  } while (0)
234 
235 /* add label */
236 #define ADD_LABEL(seq, label) \
237  ADD_ELEM((seq), (LINK_ELEMENT *) (label))
238 
239 #define APPEND_LABEL(seq, before, label) \
240  APPEND_ELEM((seq), (before), (LINK_ELEMENT *) (label))
241 
242 #define ADD_ADJUST(seq, line, label) \
243  ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (line)))
244 
245 #define ADD_ADJUST_RESTORE(seq, label) \
246  ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
247 
248 #define ADD_CATCH_ENTRY(type, ls, le, iseqv, lc) \
249  (rb_ary_push(iseq->compile_data->catch_table_ary, \
250  rb_ary_new3(5, (type), \
251  (VALUE)(ls) | 1, (VALUE)(le) | 1, \
252  (VALUE)(iseqv), (VALUE)(lc) | 1)))
253 
254 /* compile node */
255 #define COMPILE(anchor, desc, node) \
256  (debug_compile("== " desc "\n", \
257  iseq_compile_each(iseq, (anchor), (node), 0)))
258 
259 /* compile node, this node's value will be popped */
260 #define COMPILE_POPED(anchor, desc, node) \
261  (debug_compile("== " desc "\n", \
262  iseq_compile_each(iseq, (anchor), (node), 1)))
263 
264 /* compile node, which is popped when 'poped' is true */
265 #define COMPILE_(anchor, desc, node, poped) \
266  (debug_compile("== " desc "\n", \
267  iseq_compile_each(iseq, (anchor), (node), (poped))))
268 
269 #define OPERAND_AT(insn, idx) \
270  (((INSN*)(insn))->operands[(idx)])
271 
272 #define INSN_OF(insn) \
273  (((INSN*)(insn))->insn_id)
274 
275 /* error */
276 #define COMPILE_ERROR(strs) \
277 { \
278  VALUE tmp = GET_THREAD()->errinfo; \
279  if (compile_debug) rb_compile_bug strs; \
280  GET_THREAD()->errinfo = iseq->compile_data->err_info; \
281  rb_compile_error strs; \
282  iseq->compile_data->err_info = GET_THREAD()->errinfo; \
283  GET_THREAD()->errinfo = tmp; \
284  ret = 0; \
285  break; \
286 }
287 
288 #define ERROR_ARGS ruby_sourcefile, nd_line(node),
289 
290 
291 #define COMPILE_OK 1
292 #define COMPILE_NG 0
293 
294 
295 /* leave name uninitialized so that compiler warn if INIT_ANCHOR is
296  * missing */
297 #define DECL_ANCHOR(name) \
298  LINK_ANCHOR *name, name##_body__ = {{0,},}
299 #define INIT_ANCHOR(name) \
300  (name##_body__.last = &name##_body__.anchor, name = &name##_body__)
301 
302 #define hide_obj(obj) do {OBJ_FREEZE(obj); RBASIC(obj)->klass = 0;} while (0)
303 
304 #include "optinsn.inc"
305 #if OPT_INSTRUCTIONS_UNIFICATION
306 #include "optunifs.inc"
307 #endif
308 
309 /* for debug */
310 #if CPDEBUG < 0
311 #define ISEQ_ARG iseq,
312 #define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
313 #else
314 #define ISEQ_ARG
315 #define ISEQ_ARG_DECLARE
316 #endif
317 
318 #if CPDEBUG
319 #define gl_node_level iseq->compile_data->node_level
320 #if 0
321 static void debug_list(ISEQ_ARG_DECLARE LINK_ANCHOR *anchor);
322 #endif
323 #endif
324 
325 static void dump_disasm_list(LINK_ELEMENT *elem);
326 
327 static int insn_data_length(INSN *iobj);
328 static int insn_data_line_no(INSN *iobj);
329 static int calc_sp_depth(int depth, INSN *iobj);
330 
331 static INSN *new_insn_body(rb_iseq_t *iseq, int line_no, int insn_id, int argc, ...);
332 static LABEL *new_label_body(rb_iseq_t *iseq, long line);
333 static ADJUST *new_adjust_body(rb_iseq_t *iseq, LABEL *label, int line);
334 
335 static int iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *anchor, NODE * n, int);
336 static int iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *anchor);
337 static int iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *anchor);
338 static int iseq_insns_unification(rb_iseq_t *iseq, LINK_ANCHOR *anchor);
339 
340 static int iseq_set_local_table(rb_iseq_t *iseq, ID *tbl);
341 static int iseq_set_exception_local_table(rb_iseq_t *iseq);
342 static int iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *anchor, NODE * node);
343 
344 static int iseq_set_sequence_stackcaching(rb_iseq_t *iseq, LINK_ANCHOR *anchor);
345 static int iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *anchor);
346 static int iseq_set_exception_table(rb_iseq_t *iseq);
347 static int iseq_set_optargs_table(rb_iseq_t *iseq);
348 
349 /*
350  * To make Array to LinkedList, use link_anchor
351  */
352 
353 static void
354 verify_list(ISEQ_ARG_DECLARE const char *info, LINK_ANCHOR *anchor)
355 {
356 #if CPDEBUG
357  int flag = 0;
358  LINK_ELEMENT *list, *plist;
359 
360  if (!compile_debug) return;
361 
362  list = anchor->anchor.next;
363  plist = &anchor->anchor;
364  while (list) {
365  if (plist != list->prev) {
366  flag += 1;
367  }
368  plist = list;
369  list = list->next;
370  }
371 
372  if (anchor->last != plist && anchor->last != 0) {
373  flag |= 0x70000;
374  }
375 
376  if (flag != 0) {
377  rb_bug("list verify error: %08x (%s)", flag, info);
378  }
379 #endif
380 }
381 #if CPDEBUG < 0
382 #define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
383 #endif
384 
385 /*
386  * elem1, elem2 => elem1, elem2, elem
387  */
388 static void
390 {
391  elem->prev = anchor->last;
392  anchor->last->next = elem;
393  anchor->last = elem;
394  verify_list("add", anchor);
395 }
396 
397 /*
398  * elem1, before, elem2 => elem1, before, elem, elem2
399  */
400 static void
402 {
403  elem->prev = before;
404  elem->next = before->next;
405  elem->next->prev = elem;
406  before->next = elem;
407  if (before == anchor->last) anchor->last = elem;
408  verify_list("add", anchor);
409 }
410 #if CPDEBUG < 0
411 #define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
412 #define APPEND_ELEM(anchor, before, elem) ADD_ELEM(iseq, (anchor), (before), (elem))
413 #endif
414 
415 static int
417 {
418  if (!SPECIAL_CONST_P(v)) {
419  rb_ary_push(iseq->mark_ary, v);
420  }
421  return COMPILE_OK;
422 }
423 
424 #define ruby_sourcefile RSTRING_PTR(iseq->location.path)
425 
426 static int
428 {
429  if (!SPECIAL_CONST_P(v)) {
430  rb_ary_push(iseq->compile_data->mark_ary, v);
431  }
432  return COMPILE_OK;
433 }
434 
435 static int
437 {
438  rb_iseq_t *iseq = (rb_iseq_t *)arg;
439  LABEL *lobj = (LABEL *)label;
440  if (!lobj->link.next) {
441  do {
442  int ret;
444  "%s: undefined label", rb_id2name((ID)name)));
445  if (ret) break;
446  } while (0);
447  }
448  return ST_CONTINUE;
449 }
450 
451 static void
452 validate_labels(rb_iseq_t *iseq, st_table *labels_table)
453 {
454  st_foreach(labels_table, validate_label, (st_data_t)iseq);
455  if (!NIL_P(iseq->compile_data->err_info)) {
456  rb_exc_raise(iseq->compile_data->err_info);
457  }
458 }
459 
460 VALUE
462 {
463  DECL_ANCHOR(ret);
464  rb_iseq_t *iseq;
465  INIT_ANCHOR(ret);
466  GetISeqPtr(self, iseq);
467 
468  if (node == 0) {
469  COMPILE(ret, "nil", node);
470  iseq_set_local_table(iseq, 0);
471  }
472  else if (nd_type(node) == NODE_SCOPE) {
473  /* iseq type of top, method, class, block */
474  iseq_set_local_table(iseq, node->nd_tbl);
475  iseq_set_arguments(iseq, ret, node->nd_args);
476 
477  switch (iseq->type) {
478  case ISEQ_TYPE_BLOCK:
479  {
480  LABEL *start = iseq->compile_data->start_label = NEW_LABEL(0);
481  LABEL *end = iseq->compile_data->end_label = NEW_LABEL(0);
482 
483  ADD_LABEL(ret, start);
485  COMPILE(ret, "block body", node->nd_body);
487  ADD_LABEL(ret, end);
488 
489  /* wide range catch handler must put at last */
490  ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, 0, start);
491  ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, 0, end);
492  break;
493  }
494  case ISEQ_TYPE_CLASS:
495  {
497  COMPILE(ret, "scoped node", node->nd_body);
498  ADD_TRACE(ret, nd_line(node), RUBY_EVENT_END);
499  break;
500  }
501  case ISEQ_TYPE_METHOD:
502  {
504  COMPILE(ret, "scoped node", node->nd_body);
505  ADD_TRACE(ret, nd_line(node), RUBY_EVENT_RETURN);
506  break;
507  }
508  default: {
509  COMPILE(ret, "scoped node", node->nd_body);
510  break;
511  }
512  }
513  }
514  else {
515  switch (iseq->type) {
516  case ISEQ_TYPE_METHOD:
517  case ISEQ_TYPE_CLASS:
518  case ISEQ_TYPE_BLOCK:
519  case ISEQ_TYPE_EVAL:
520  case ISEQ_TYPE_MAIN:
521  case ISEQ_TYPE_TOP:
522  rb_compile_error(ERROR_ARGS "compile/should not be reached: %s:%d",
523  __FILE__, __LINE__);
524  break;
525  case ISEQ_TYPE_RESCUE:
527  COMPILE(ret, "rescue", node);
528  break;
529  case ISEQ_TYPE_ENSURE:
531  COMPILE_POPED(ret, "ensure", node);
532  break;
533  case ISEQ_TYPE_DEFINED_GUARD:
534  iseq_set_local_table(iseq, 0);
535  COMPILE(ret, "defined guard", node);
536  break;
537  default:
538  rb_bug("unknown scope");
539  }
540  }
541 
542  if (iseq->type == ISEQ_TYPE_RESCUE || iseq->type == ISEQ_TYPE_ENSURE) {
543  ADD_INSN2(ret, 0, getlocal, INT2FIX(2), INT2FIX(0));
544  ADD_INSN1(ret, 0, throw, INT2FIX(0) /* continue throw */ );
545  }
546  else {
547  ADD_INSN(ret, iseq->compile_data->last_line, leave);
548  }
549 
550 #if SUPPORT_JOKE
551  if (iseq->compile_data->labels_table) {
552  validate_labels(iseq, iseq->compile_data->labels_table);
553  }
554 #endif
555  return iseq_setup(iseq, ret);
556 }
557 
558 int
560 {
561 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
562  const void * const *table = rb_vm_get_insns_address_table();
563  unsigned long i;
564 
565  iseq->iseq_encoded = ALLOC_N(VALUE, iseq->iseq_size);
566  MEMCPY(iseq->iseq_encoded, iseq->iseq, VALUE, iseq->iseq_size);
567 
568  for (i = 0; i < iseq->iseq_size; /* */ ) {
569  int insn = (int)iseq->iseq_encoded[i];
570  int len = insn_len(insn);
571  iseq->iseq_encoded[i] = (VALUE)table[insn];
572  i += len;
573  }
574 #else
575  iseq->iseq_encoded = iseq->iseq;
576 #endif
577  return COMPILE_OK;
578 }
579 
580 /*********************************************/
581 /* definition of data structure for compiler */
582 /*********************************************/
583 
584 static void *
586 {
587  void *ptr = 0;
588  struct iseq_compile_data_storage *storage =
590 
591  if (storage->pos + size > storage->size) {
592  unsigned long alloc_size = storage->size * 2;
593 
594  retry:
595  if (alloc_size < size) {
596  alloc_size *= 2;
597  goto retry;
598  }
599  storage->next = (void *)ALLOC_N(char, alloc_size +
600  sizeof(struct
602  storage = iseq->compile_data->storage_current = storage->next;
603  storage->next = 0;
604  storage->pos = 0;
605  storage->size = alloc_size;
606  storage->buff = (char *)(&storage->buff + 1);
607  }
608 
609  ptr = (void *)&storage->buff[storage->pos];
610  storage->pos += size;
611  return ptr;
612 }
613 
614 static INSN *
616 {
617  return (INSN *)compile_data_alloc(iseq, sizeof(INSN));
618 }
619 
620 static LABEL *
622 {
623  return (LABEL *)compile_data_alloc(iseq, sizeof(LABEL));
624 }
625 
626 static ADJUST *
628 {
629  return (ADJUST *)compile_data_alloc(iseq, sizeof(ADJUST));
630 }
631 
632 /*
633  * elem1, elemX => elem1, elem2, elemX
634  */
635 static void
637 {
638  elem2->next = elem1->next;
639  elem2->prev = elem1;
640  elem1->next = elem2;
641  if (elem2->next) {
642  elem2->next->prev = elem2;
643  }
644 }
645 
646 #if 0 /* unused */
647 /*
648  * elemX, elem1 => elemX, elem2, elem1
649  */
650 static void
651 INSERT_ELEM_PREV(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
652 {
653  elem2->prev = elem1->prev;
654  elem2->next = elem1;
655  elem1->prev = elem2;
656  if (elem2->prev) {
657  elem2->prev->next = elem2;
658  }
659 }
660 #endif
661 
662 /*
663  * elemX, elem1, elemY => elemX, elem2, elemY
664  */
665 static void
667 {
668  elem2->prev = elem1->prev;
669  elem2->next = elem1->next;
670  if (elem1->prev) {
671  elem1->prev->next = elem2;
672  }
673  if (elem1->next) {
674  elem1->next->prev = elem2;
675  }
676 }
677 
678 static void
680 {
681  elem->prev->next = elem->next;
682  if (elem->next) {
683  elem->next->prev = elem->prev;
684  }
685 }
686 
687 static LINK_ELEMENT *
689 {
690  return anchor->anchor.next;
691 }
692 
693 #if 0 /* unused */
694 static LINK_ELEMENT *
695 LAST_ELEMENT(LINK_ANCHOR *anchor)
696 {
697  return anchor->last;
698 }
699 #endif
700 
701 static LINK_ELEMENT *
703 {
704  LINK_ELEMENT *elem = anchor->last;
705  anchor->last = anchor->last->prev;
706  anchor->last->next = 0;
707  verify_list("pop", anchor);
708  return elem;
709 }
710 #if CPDEBUG < 0
711 #define POP_ELEMENT(anchor) POP_ELEMENT(iseq, (anchor))
712 #endif
713 
714 #if 0 /* unused */
715 static LINK_ELEMENT *
716 SHIFT_ELEMENT(LINK_ANCHOR *anchor)
717 {
718  LINK_ELEMENT *elem = anchor->anchor.next;
719  if (elem) {
720  anchor->anchor.next = elem->next;
721  }
722  return elem;
723 }
724 #endif
725 
726 #if 0 /* unused */
727 static int
728 LIST_SIZE(LINK_ANCHOR *anchor)
729 {
730  LINK_ELEMENT *elem = anchor->anchor.next;
731  int size = 0;
732  while (elem) {
733  size += 1;
734  elem = elem->next;
735  }
736  return size;
737 }
738 #endif
739 
740 static int
742 {
743  if (anchor->anchor.next == 0) {
744  return 1;
745  }
746  else {
747  return 0;
748  }
749 }
750 
751 /*
752  * anc1: e1, e2, e3
753  * anc2: e4, e5
754  *#=>
755  * anc1: e1, e2, e3, e4, e5
756  * anc2: e4, e5 (broken)
757  */
758 static void
760 {
761  if (anc2->anchor.next) {
762  anc1->last->next = anc2->anchor.next;
763  anc2->anchor.next->prev = anc1->last;
764  anc1->last = anc2->last;
765  }
766  verify_list("append", anc1);
767 }
768 #if CPDEBUG < 0
769 #define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
770 #endif
771 
772 /*
773  * anc1: e1, e2, e3
774  * anc2: e4, e5
775  *#=>
776  * anc1: e4, e5, e1, e2, e3
777  * anc2: e4, e5 (broken)
778  */
779 static void
781 {
782  if (anc2->anchor.next) {
783  LINK_ELEMENT *first = anc1->anchor.next;
784  anc1->anchor.next = anc2->anchor.next;
785  anc1->anchor.next->prev = &anc1->anchor;
786  anc2->last->next = first;
787  if (first) {
788  first->prev = anc2->last;
789  }
790  else {
791  anc1->last = anc2->last;
792  }
793  }
794 
795  verify_list("append", anc1);
796 }
797 #if CPDEBUG < 0
798 #define INSERT_LIST(anc1, anc2) INSERT_LIST(iseq, (anc1), (anc2))
799 #endif
800 
801 #if 0 /* unused */
802 /*
803  * anc1: e1, e2, e3
804  * anc2: e4, e5
805  *#=>
806  * anc1: e4, e5
807  * anc2: e1, e2, e3
808  */
809 static void
810 SWAP_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *anc1, LINK_ANCHOR *anc2)
811 {
812  LINK_ANCHOR tmp = *anc2;
813 
814  /* it has bug */
815  *anc2 = *anc1;
816  *anc1 = tmp;
817 
818  verify_list("swap1", anc1);
819  verify_list("swap2", anc2);
820 }
821 #if CPDEBUG < 0
822 #define SWAP_LIST(anc1, anc2) SWAP_LIST(iseq, (anc1), (anc2))
823 #endif
824 
825 static LINK_ANCHOR *
826 REVERSE_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *anc)
827 {
828  LINK_ELEMENT *first, *last, *elem, *e;
829  first = &anc->anchor;
830  elem = first->next;
831  last = anc->last;
832 
833  if (elem != 0) {
834  anc->anchor.next = last;
835  anc->last = elem;
836  }
837  else {
838  /* null list */
839  return anc;
840  }
841  while (elem) {
842  e = elem->next;
843  elem->next = elem->prev;
844  elem->prev = e;
845  elem = e;
846  }
847 
848  first->next = last;
849  last->prev = first;
850  anc->last->next = 0;
851 
852  verify_list("reverse", anc);
853  return anc;
854 }
855 #if CPDEBUG < 0
856 #define REVERSE_LIST(anc) REVERSE_LIST(iseq, (anc))
857 #endif
858 #endif
859 
860 #if CPDEBUG && 0
861 static void
862 debug_list(ISEQ_ARG_DECLARE LINK_ANCHOR *anchor)
863 {
864  LINK_ELEMENT *list = FIRST_ELEMENT(anchor);
865  printf("----\n");
866  printf("anch: %p, frst: %p, last: %p\n", &anchor->anchor,
867  anchor->anchor.next, anchor->last);
868  while (list) {
869  printf("curr: %p, next: %p, prev: %p, type: %d\n", list, list->next,
870  list->prev, FIX2INT(list->type));
871  list = list->next;
872  }
873  printf("----\n");
874 
875  dump_disasm_list(anchor->anchor.next);
876  verify_list("debug list", anchor);
877 }
878 #if CPDEBUG < 0
879 #define debug_list(anc) debug_list(iseq, (anc))
880 #endif
881 #endif
882 
883 static LABEL *
884 new_label_body(rb_iseq_t *iseq, long line)
885 {
886  LABEL *labelobj = compile_data_alloc_label(iseq);
887 
888  labelobj->link.type = ISEQ_ELEMENT_LABEL;
889  labelobj->link.next = 0;
890 
891  labelobj->label_no = iseq->compile_data->label_no++;
892  labelobj->sc_state = 0;
893  labelobj->sp = -1;
894  return labelobj;
895 }
896 
897 static ADJUST *
898 new_adjust_body(rb_iseq_t *iseq, LABEL *label, int line)
899 {
900  ADJUST *adjust = compile_data_alloc_adjust(iseq);
901  adjust->link.type = ISEQ_ELEMENT_ADJUST;
902  adjust->link.next = 0;
903  adjust->label = label;
904  adjust->line_no = line;
905  return adjust;
906 }
907 
908 static INSN *
909 new_insn_core(rb_iseq_t *iseq, int line_no,
910  int insn_id, int argc, VALUE *argv)
911 {
912  INSN *iobj = compile_data_alloc_insn(iseq);
913  /* printf("insn_id: %d, line: %d\n", insn_id, line_no); */
914 
915  iobj->link.type = ISEQ_ELEMENT_INSN;
916  iobj->link.next = 0;
917  iobj->insn_id = insn_id;
918  iobj->line_no = line_no;
919  iobj->operands = argv;
920  iobj->operand_size = argc;
921  iobj->sc_state = 0;
922  return iobj;
923 }
924 
925 static INSN *
926 new_insn_body(rb_iseq_t *iseq, int line_no, int insn_id, int argc, ...)
927 {
928  VALUE *operands = 0;
929  va_list argv;
930  if (argc > 0) {
931  int i;
932  va_init_list(argv, argc);
933  operands = (VALUE *)compile_data_alloc(iseq, sizeof(VALUE) * argc);
934  for (i = 0; i < argc; i++) {
935  VALUE v = va_arg(argv, VALUE);
936  operands[i] = v;
937  }
938  va_end(argv);
939  }
940  return new_insn_core(iseq, line_no, insn_id, argc, operands);
941 }
942 
943 static rb_call_info_t *
944 new_callinfo(rb_iseq_t *iseq, ID mid, int argc, VALUE block, unsigned long flag)
945 {
947  ci->mid = mid;
948  ci->flag = flag;
949  ci->orig_argc = argc;
950  ci->argc = argc;
951 
952  if (block) {
953  GetISeqPtr(block, ci->blockiseq);
954  }
955  else {
956  ci->blockiseq = 0;
957  if (!(ci->flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG))) {
959  }
960  }
961  ci->vmstat = 0;
962  ci->blockptr = 0;
963  ci->recv = Qundef;
964  ci->call = 0; /* TODO: should set default function? */
965 
966  ci->aux.index = iseq->callinfo_size++;
967 
968  return ci;
969 }
970 
971 static INSN *
972 new_insn_send(rb_iseq_t *iseq, int line_no, VALUE id, VALUE argc, VALUE block, VALUE flag)
973 {
974  VALUE *operands = (VALUE *)compile_data_alloc(iseq, sizeof(VALUE) * 1);
975  operands[0] = (VALUE)new_callinfo(iseq, SYM2ID(id), FIX2INT(argc), block, FIX2INT(flag));
976  return new_insn_core(iseq, line_no, BIN(send), 1, operands);
977 }
978 
979 static VALUE
981  VALUE name, VALUE parent, enum iseq_type type, int line_no)
982 {
983  VALUE ret;
984 
985  debugs("[new_child_iseq]> ---------------------------------------\n");
986  ret = rb_iseq_new_with_opt(node, name,
987  iseq_path(iseq->self), iseq_absolute_path(iseq->self),
988  INT2FIX(line_no), parent, type, iseq->compile_data->option);
989  debugs("[new_child_iseq]< ---------------------------------------\n");
990  iseq_add_mark_object(iseq, ret);
991  return ret;
992 }
993 
994 static int
996 {
997  /* debugs("[compile step 2] (iseq_array_to_linkedlist)\n"); */
998 
999  if (compile_debug > 5)
1001 
1002  debugs("[compile step 3.1 (iseq_optimize)]\n");
1003  iseq_optimize(iseq, anchor);
1004 
1005  if (compile_debug > 5)
1007 
1009  debugs("[compile step 3.2 (iseq_insns_unification)]\n");
1010  iseq_insns_unification(iseq, anchor);
1011  if (compile_debug > 5)
1013  }
1014 
1015  if (iseq->compile_data->option->stack_caching) {
1016  debugs("[compile step 3.3 (iseq_set_sequence_stackcaching)]\n");
1017  iseq_set_sequence_stackcaching(iseq, anchor);
1018  if (compile_debug > 5)
1020  }
1021 
1022  debugs("[compile step 4.1 (iseq_set_sequence)]\n");
1023  iseq_set_sequence(iseq, anchor);
1024  if (compile_debug > 5)
1026 
1027  debugs("[compile step 4.2 (iseq_set_exception_table)]\n");
1029 
1030  debugs("[compile step 4.3 (set_optargs_table)] \n");
1031  iseq_set_optargs_table(iseq);
1032 
1033  debugs("[compile step 5 (iseq_translate_threaded_code)] \n");
1035 
1036  if (compile_debug > 1) {
1037  VALUE str = rb_iseq_disasm(iseq->self);
1038  printf("%s\n", StringValueCStr(str));
1039  fflush(stdout);
1040  }
1041  debugs("[compile step: finish]\n");
1042 
1043  return 0;
1044 }
1045 
1046 static int
1048 {
1049  ID id_dollar_bang;
1050 
1051  CONST_ID(id_dollar_bang, "#$!");
1052  iseq->local_table = (ID *)ALLOC_N(ID, 1);
1053  iseq->local_table_size = 1;
1054  iseq->local_size = iseq->local_table_size + 1;
1055  iseq->local_table[0] = id_dollar_bang;
1056  return COMPILE_OK;
1057 }
1058 
1059 static int
1061 {
1062  int lev = 0;
1063  while (iseq != iseq->local_iseq) {
1064  lev++;
1065  iseq = iseq->parent_iseq;
1066  }
1067  return lev;
1068 }
1069 
1070 static int
1072 {
1073  int i;
1074 
1075  for (i = 0; i < iseq->local_table_size; i++) {
1076  if (iseq->local_table[i] == id) {
1077  return i;
1078  }
1079  }
1080  return -1;
1081 }
1082 
1083 static int
1085 {
1086  int idx = get_dyna_var_idx_at_raw(iseq->local_iseq, id);
1087 
1088  if (idx < 0) {
1089  rb_bug("get_local_var_idx: %d", idx);
1090  }
1091 
1092  return idx;
1093 }
1094 
1095 static int
1096 get_dyna_var_idx(rb_iseq_t *iseq, ID id, int *level, int *ls)
1097 {
1098  int lv = 0, idx = -1;
1099 
1100  while (iseq) {
1101  idx = get_dyna_var_idx_at_raw(iseq, id);
1102  if (idx >= 0) {
1103  break;
1104  }
1105  iseq = iseq->parent_iseq;
1106  lv++;
1107  }
1108 
1109  if (idx < 0) {
1110  rb_bug("get_dyna_var_idx: -1");
1111  }
1112 
1113  *level = lv;
1114  *ls = iseq->local_size;
1115  return idx;
1116 }
1117 
1118 static int
1119 iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *optargs, NODE *node_args)
1120 {
1121  debugs("iseq_set_arguments: %s\n", node_args ? "" : "0");
1122 
1123  if (node_args) {
1124  struct rb_args_info *args = node_args->nd_ainfo;
1125  ID rest_id = 0;
1126  int last_comma = 0;
1127  ID block_id = 0;
1128 
1129  if (nd_type(node_args) != NODE_ARGS) {
1130  rb_bug("iseq_set_arguments: NODE_ARGS is expected, but %s",
1131  ruby_node_name(nd_type(node_args)));
1132  }
1133 
1134 
1135  iseq->argc = (int)args->pre_args_num;
1136  debugs(" - argc: %d\n", iseq->argc);
1137 
1138  rest_id = args->rest_arg;
1139  if (rest_id == 1) {
1140  last_comma = 1;
1141  rest_id = 0;
1142  }
1143  block_id = args->block_arg;
1144 
1145  if (args->first_post_arg) {
1147  iseq->arg_post_len = args->post_args_num;
1148  }
1149 
1150  if (args->opt_args) {
1151  NODE *node = args->opt_args;
1152  LABEL *label;
1153  VALUE labels = rb_ary_tmp_new(1);
1154  int i = 0, j;
1155 
1156  while (node) {
1157  label = NEW_LABEL(nd_line(node));
1158  rb_ary_push(labels, (VALUE)label | 1);
1159  ADD_LABEL(optargs, label);
1160  COMPILE_POPED(optargs, "optarg", node->nd_body);
1161  node = node->nd_next;
1162  i += 1;
1163  }
1164 
1165  /* last label */
1166  label = NEW_LABEL(nd_line(node_args));
1167  rb_ary_push(labels, (VALUE)label | 1);
1168  ADD_LABEL(optargs, label);
1169  i += 1;
1170 
1171  iseq->arg_opts = i;
1172  iseq->arg_opt_table = ALLOC_N(VALUE, i);
1173  MEMCPY(iseq->arg_opt_table, RARRAY_PTR(labels), VALUE, i);
1174  for (j = 0; j < i; j++) {
1175  iseq->arg_opt_table[j] &= ~1;
1176  }
1177  rb_ary_clear(labels);
1178  }
1179  else {
1180  iseq->arg_opts = 0;
1181  }
1182 
1183  if (args->kw_args) {
1184  NODE *node = args->kw_args;
1185  VALUE keywords = rb_ary_tmp_new(1);
1186  int i = 0, j;
1187 
1188  iseq->arg_keyword = get_dyna_var_idx_at_raw(iseq, args->kw_rest_arg->nd_vid);
1189  COMPILE(optargs, "kwarg", args->kw_rest_arg);
1190  while (node) {
1191  rb_ary_push(keywords, INT2FIX(node->nd_body->nd_vid));
1192  COMPILE_POPED(optargs, "kwarg", node); /* nd_type(node) == NODE_KW_ARG */
1193  node = node->nd_next;
1194  i += 1;
1195  }
1196  iseq->arg_keyword_check = (args->kw_rest_arg->nd_vid & ID_SCOPE_MASK) == ID_JUNK;
1197  iseq->arg_keywords = i;
1198  iseq->arg_keyword_table = ALLOC_N(ID, i);
1199  for (j = 0; j < i; j++) {
1200  iseq->arg_keyword_table[j] = FIX2INT(RARRAY_PTR(keywords)[j]);
1201  }
1202  ADD_INSN(optargs, nd_line(args->kw_args), pop);
1203  }
1204  else if (args->kw_rest_arg) {
1205  iseq->arg_keyword = get_dyna_var_idx_at_raw(iseq, args->kw_rest_arg->nd_vid);
1206  COMPILE(optargs, "kwarg", args->kw_rest_arg);
1207  ADD_INSN(optargs, nd_line(args->kw_rest_arg), pop);
1208  }
1209  else {
1210  iseq->arg_keyword = -1;
1211  }
1212 
1213  if (args->pre_init) { /* m_init */
1214  COMPILE_POPED(optargs, "init arguments (m)", args->pre_init);
1215  }
1216  if (args->post_init) { /* p_init */
1217  COMPILE_POPED(optargs, "init arguments (p)", args->post_init);
1218  }
1219 
1220  if (rest_id) {
1221  iseq->arg_rest = get_dyna_var_idx_at_raw(iseq, rest_id);
1222 
1223  if (iseq->arg_rest == -1) {
1224  rb_bug("arg_rest: -1");
1225  }
1226 
1227  if (iseq->arg_post_start == 0) {
1228  iseq->arg_post_start = iseq->arg_rest + 1;
1229  }
1230  }
1231 
1232  if (block_id) {
1233  iseq->arg_block = get_dyna_var_idx_at_raw(iseq, block_id);
1234  }
1235 
1236  if (iseq->arg_opts != 0 || iseq->arg_post_len != 0 ||
1237  iseq->arg_rest != -1 || iseq->arg_block != -1 ||
1238  iseq->arg_keyword != -1) {
1239  iseq->arg_simple = 0;
1240 
1241  /* set arg_size: size of arguments */
1242  if (iseq->arg_keyword != -1) {
1243  iseq->arg_size = iseq->arg_keyword + 1;
1244  }
1245  else if (iseq->arg_block != -1) {
1246  iseq->arg_size = iseq->arg_block + 1;
1247  }
1248  else if (iseq->arg_post_len) {
1249  iseq->arg_size = iseq->arg_post_start + iseq->arg_post_len;
1250  }
1251  else if (iseq->arg_rest != -1) {
1252  iseq->arg_size = iseq->arg_rest + 1;
1253  }
1254  else if (iseq->arg_opts) {
1255  iseq->arg_size = iseq->argc + iseq->arg_opts - 1;
1256  }
1257  else {
1258  iseq->arg_size = iseq->argc;
1259  }
1260  }
1261  else {
1262  iseq->arg_simple = 1;
1263  iseq->arg_size = iseq->argc;
1264  }
1265 
1266  if (iseq->type == ISEQ_TYPE_BLOCK) {
1267  if (iseq->arg_opts == 0 && iseq->arg_post_len == 0 && iseq->arg_rest == -1) {
1268  if (iseq->argc == 1 && last_comma == 0) {
1269  /* {|a|} */
1270  iseq->arg_simple |= 0x02;
1271  }
1272  }
1273  }
1274  }
1275  else {
1276  iseq->arg_simple = 1;
1277  }
1278 
1279  return COMPILE_OK;
1280 }
1281 
1282 static int
1284 {
1285  int size;
1286 
1287  if (tbl) {
1288  size = (int)*tbl;
1289  tbl++;
1290  }
1291  else {
1292  size = 0;
1293  }
1294 
1295  if (size > 0) {
1296  iseq->local_table = (ID *)ALLOC_N(ID, size);
1297  MEMCPY(iseq->local_table, tbl, ID, size);
1298  }
1299 
1300  iseq->local_size = iseq->local_table_size = size;
1301  iseq->local_size += 1;
1302  /*
1303  if (lfp == dfp ) { // top, class, method
1304  dfp[-1]: svar
1305  else { // block
1306  dfp[-1]: cref
1307  }
1308  */
1309 
1310  debugs("iseq_set_local_table: %d, %d\n", iseq->local_size, iseq->local_table_size);
1311  return COMPILE_OK;
1312 }
1313 
1314 static int
1316 {
1317  if (val == lit) return 0;
1318  if (SPECIAL_CONST_P(lit)) {
1319  return val != lit;
1320  }
1321  if (SPECIAL_CONST_P(val) || BUILTIN_TYPE(val) != BUILTIN_TYPE(lit)) {
1322  return -1;
1323  }
1324  if (BUILTIN_TYPE(lit) == T_STRING) {
1325  return rb_str_hash_cmp(lit, val);
1326  }
1327  return !rb_eql(lit, val);
1328 }
1329 
1330 static st_index_t
1332 {
1333  if (SPECIAL_CONST_P(a)) return (st_index_t)a;
1334  if (RB_TYPE_P(a, T_STRING)) return rb_str_hash(a);
1335  {
1336  VALUE hval = rb_hash(a);
1337  return (st_index_t)FIX2LONG(hval);
1338  }
1339 }
1340 
1341 static const struct st_hash_type cdhash_type = {
1342  cdhash_cmp,
1343  cdhash_hash,
1344 };
1345 
1348  int pos;
1349  int len;
1350 };
1351 
1352 static int
1354 {
1355  struct cdhash_set_label_struct *data = (struct cdhash_set_label_struct *)ptr;
1356  LABEL *lobj = (LABEL *)(val & ~1);
1357  rb_hash_aset(data->hash, key, INT2FIX(lobj->position - (data->pos+data->len)));
1358  return ST_CONTINUE;
1359 }
1360 
1364 static int
1366 {
1367  LABEL *lobj;
1368  INSN *iobj;
1369  struct iseq_line_info_entry *line_info_table;
1370  unsigned int last_line = 0;
1371  LINK_ELEMENT *list;
1372  VALUE *generated_iseq;
1373 
1374  int k, pos, sp, stack_max = 0, line = 0;
1375 
1376  /* set label position */
1377  list = FIRST_ELEMENT(anchor);
1378  k = pos = 0;
1379  while (list) {
1380  switch (list->type) {
1381  case ISEQ_ELEMENT_INSN:
1382  {
1383  iobj = (INSN *)list;
1384  line = iobj->line_no;
1385  pos += insn_data_length(iobj);
1386  k++;
1387  break;
1388  }
1389  case ISEQ_ELEMENT_LABEL:
1390  {
1391  lobj = (LABEL *)list;
1392  lobj->position = pos;
1393  lobj->set = TRUE;
1394  break;
1395  }
1396  case ISEQ_ELEMENT_NONE:
1397  {
1398  /* ignore */
1399  break;
1400  }
1401  case ISEQ_ELEMENT_ADJUST:
1402  {
1403  ADJUST *adjust = (ADJUST *)list;
1404  if (adjust->line_no != -1) {
1405  pos += 2 /* insn + 1 operand */;
1406  k++;
1407  }
1408  break;
1409  }
1410  default:
1412  dump_disasm_list(list);
1414  "error: set_sequence");
1415  break;
1416  }
1417  list = list->next;
1418  }
1419 
1420  /* make instruction sequence */
1421  generated_iseq = ALLOC_N(VALUE, pos);
1422  line_info_table = ALLOC_N(struct iseq_line_info_entry, k);
1423  iseq->ic_entries = ALLOC_N(struct iseq_inline_cache_entry, iseq->ic_size);
1424  MEMZERO(iseq->ic_entries, struct iseq_inline_cache_entry, iseq->ic_size);
1426  /* MEMZERO(iseq->callinfo_entries, rb_call_info_t, iseq->callinfo_size); */
1427 
1428  list = FIRST_ELEMENT(anchor);
1429  k = pos = sp = 0;
1430 
1431  while (list) {
1432  switch (list->type) {
1433  case ISEQ_ELEMENT_INSN:
1434  {
1435  int j, len, insn;
1436  const char *types;
1437  VALUE *operands;
1438 
1439  iobj = (INSN *)list;
1440 
1441  /* update sp */
1442  sp = calc_sp_depth(sp, iobj);
1443  if (sp > stack_max) {
1444  stack_max = sp;
1445  }
1446 
1447  /* fprintf(stderr, "insn: %-16s, sp: %d\n", insn_name(iobj->insn_id), sp); */
1448  operands = iobj->operands;
1449  insn = iobj->insn_id;
1450  generated_iseq[pos] = insn;
1451  types = insn_op_types(insn);
1452  len = insn_len(insn);
1453 
1454  /* operand check */
1455  if (iobj->operand_size != len - 1) {
1456  /* printf("operand size miss! (%d, %d)\n", iobj->operand_size, len); */
1457  dump_disasm_list(list);
1459  "operand size miss! (%d for %d)",
1460  iobj->operand_size, len - 1);
1461  xfree(generated_iseq);
1462  xfree(line_info_table);
1463  return 0;
1464  }
1465 
1466  for (j = 0; types[j]; j++) {
1467  char type = types[j];
1468  /* printf("--> [%c - (%d-%d)]\n", type, k, j); */
1469  switch (type) {
1470  case TS_OFFSET:
1471  {
1472  /* label(destination position) */
1473  lobj = (LABEL *)operands[j];
1474  if (!lobj->set) {
1476  "unknown label");
1477  }
1478  if (lobj->sp == -1) {
1479  lobj->sp = sp;
1480  }
1481  generated_iseq[pos + 1 + j] = lobj->position - (pos + len);
1482  break;
1483  }
1484  case TS_CDHASH:
1485  {
1486  VALUE map = operands[j];
1487  struct cdhash_set_label_struct data;
1488  data.hash = map;
1489  data.pos = pos;
1490  data.len = len;
1492 
1493  hide_obj(map);
1494  generated_iseq[pos + 1 + j] = map;
1495  break;
1496  }
1497  case TS_LINDEX:
1498  case TS_NUM: /* ulong */
1499  generated_iseq[pos + 1 + j] = FIX2INT(operands[j]);
1500  break;
1501  case TS_ISEQ: /* iseq */
1502  {
1503  VALUE v = operands[j];
1504  rb_iseq_t *block = 0;
1505  if (v) {
1506  GetISeqPtr(v, block);
1507  }
1508  generated_iseq[pos + 1 + j] = (VALUE)block;
1509  break;
1510  }
1511  case TS_VALUE: /* VALUE */
1512  {
1513  VALUE v = operands[j];
1514  generated_iseq[pos + 1 + j] = v;
1515  /* to mark ruby object */
1516  iseq_add_mark_object(iseq, v);
1517  break;
1518  }
1519  case TS_IC: /* inline cache */
1520  {
1521  int ic_index = FIX2INT(operands[j]);
1522  IC ic = &iseq->ic_entries[ic_index];
1523  if (UNLIKELY(ic_index >= iseq->ic_size)) {
1524  rb_bug("iseq_set_sequence: ic_index overflow: index: %d, size: %d", ic_index, iseq->ic_size);
1525  }
1526  generated_iseq[pos + 1 + j] = (VALUE)ic;
1527  break;
1528  }
1529  case TS_CALLINFO: /* call info */
1530  {
1531  rb_call_info_t *base_ci = (rb_call_info_t *)operands[j];
1532  rb_call_info_t *ci = &iseq->callinfo_entries[base_ci->aux.index];
1533  *ci = *base_ci;
1534 
1535  if (UNLIKELY(base_ci->aux.index >= iseq->callinfo_size)) {
1536  rb_bug("iseq_set_sequence: ci_index overflow: index: %d, size: %d", base_ci->argc, iseq->callinfo_size);
1537  }
1538  generated_iseq[pos + 1 + j] = (VALUE)ci;
1539  break;
1540  }
1541  case TS_ID: /* ID */
1542  generated_iseq[pos + 1 + j] = SYM2ID(operands[j]);
1543  break;
1544  case TS_GENTRY:
1545  {
1546  struct rb_global_entry *entry =
1547  (struct rb_global_entry *)(operands[j] & (~1));
1548  generated_iseq[pos + 1 + j] = (VALUE)entry;
1549  }
1550  break;
1551  default:
1553  "unknown operand type: %c", type);
1554  xfree(generated_iseq);
1555  xfree(line_info_table);
1556  return 0;
1557  }
1558  }
1559  if (last_line != iobj->line_no) {
1560  line_info_table[k].line_no = last_line = iobj->line_no;
1561  line_info_table[k].position = pos;
1562  k++;
1563  }
1564  pos += len;
1565  break;
1566  }
1567  case ISEQ_ELEMENT_LABEL:
1568  {
1569  lobj = (LABEL *)list;
1570  if (lobj->sp == -1) {
1571  lobj->sp = sp;
1572  }
1573  else {
1574  sp = lobj->sp;
1575  }
1576  break;
1577  }
1578  case ISEQ_ELEMENT_ADJUST:
1579  {
1580  ADJUST *adjust = (ADJUST *)list;
1581  int orig_sp = sp;
1582 
1583  if (adjust->label) {
1584  sp = adjust->label->sp;
1585  }
1586  else {
1587  sp = 0;
1588  }
1589 
1590  if (adjust->line_no != -1) {
1591  if (orig_sp - sp > 0) {
1592  if (last_line != (unsigned int)adjust->line_no) {
1593  line_info_table[k].line_no = last_line = adjust->line_no;
1594  line_info_table[k].position = pos;
1595  k++;
1596  }
1597  generated_iseq[pos++] = BIN(adjuststack);
1598  generated_iseq[pos++] = orig_sp - sp;
1599  }
1600  else if (orig_sp - sp == 0) {
1601  /* jump to next insn */
1602  if (last_line != (unsigned int)adjust->line_no) {
1603  line_info_table[k].line_no = last_line = adjust->line_no;
1604  line_info_table[k].position = pos;
1605  k++;
1606  }
1607  generated_iseq[pos++] = BIN(jump);
1608  generated_iseq[pos++] = 0;
1609  }
1610  else {
1611  rb_bug("iseq_set_sequence: adjust bug");
1612  }
1613  }
1614  break;
1615  }
1616  default:
1617  /* ignore */
1618  break;
1619  }
1620  list = list->next;
1621  }
1622 
1623 #if 0 /* XXX */
1624  /* this check need dead code elimination */
1625  if (sp != 1) {
1626  rb_bug("SP is not 0 on %s (%d)\n", RSTRING_PTR(iseq->name), sp);
1627  }
1628 #endif
1629 
1630  iseq->iseq = (void *)generated_iseq;
1631  iseq->iseq_size = pos;
1632  iseq->stack_max = stack_max;
1633 
1634  line_info_table = ruby_xrealloc(line_info_table, k * sizeof(struct iseq_line_info_entry));
1635  iseq->line_info_table = line_info_table;
1636  iseq->line_info_size = k;
1637 
1638  return COMPILE_OK;
1639 }
1640 
1641 static int
1643 {
1644  return lobj->position;
1645 }
1646 
1647 static int
1649 {
1650  return lobj->sp;
1651 }
1652 
1653 static int
1655 {
1656  VALUE *tptr, *ptr;
1657  int tlen, i;
1658  struct iseq_catch_table_entry *entry;
1659 
1660  tlen = (int)RARRAY_LEN(iseq->compile_data->catch_table_ary);
1661  tptr = RARRAY_PTR(iseq->compile_data->catch_table_ary);
1662 
1663  iseq->catch_table = tlen ? ALLOC_N(struct iseq_catch_table_entry, tlen) : 0;
1664  iseq->catch_table_size = tlen;
1665 
1666  for (i = 0; i < tlen; i++) {
1667  ptr = RARRAY_PTR(tptr[i]);
1668  entry = &iseq->catch_table[i];
1669  entry->type = (enum catch_type)(ptr[0] & 0xffff);
1670  entry->start = label_get_position((LABEL *)(ptr[1] & ~1));
1671  entry->end = label_get_position((LABEL *)(ptr[2] & ~1));
1672  entry->iseq = ptr[3];
1673 
1674  /* register iseq as mark object */
1675  if (entry->iseq != 0) {
1676  iseq_add_mark_object(iseq, entry->iseq);
1677  }
1678 
1679  /* stack depth */
1680  if (ptr[4]) {
1681  LABEL *lobj = (LABEL *)(ptr[4] & ~1);
1682  entry->cont = label_get_position(lobj);
1683  entry->sp = label_get_sp(lobj);
1684 
1685  /* TODO: Dirty Hack! Fix me */
1686  if (entry->type == CATCH_TYPE_RESCUE ||
1687  entry->type == CATCH_TYPE_BREAK ||
1688  entry->type == CATCH_TYPE_NEXT) {
1689  entry->sp--;
1690  }
1691  }
1692  else {
1693  entry->cont = 0;
1694  }
1695  }
1696 
1697  iseq->compile_data->catch_table_ary = 0; /* free */
1698  return COMPILE_OK;
1699 }
1700 
1701 /*
1702  * set optional argument table
1703  * def foo(a, b=expr1, c=expr2)
1704  * =>
1705  * b:
1706  * expr1
1707  * c:
1708  * expr2
1709  */
1710 static int
1712 {
1713  int i;
1714 
1715  if (iseq->arg_opts != 0) {
1716  for (i = 0; i < iseq->arg_opts; i++) {
1717  iseq->arg_opt_table[i] =
1718  label_get_position((LABEL *)iseq->arg_opt_table[i]);
1719  }
1720  }
1721  return COMPILE_OK;
1722 }
1723 
1724 static LINK_ELEMENT *
1726 {
1727  LABEL *lobj = (LABEL *)OPERAND_AT(iobj, 0);
1728  LINK_ELEMENT *list;
1729 
1730  list = lobj->link.next;
1731  while (list) {
1732  if (list->type == ISEQ_ELEMENT_INSN || list->type == ISEQ_ELEMENT_ADJUST) {
1733  break;
1734  }
1735  list = list->next;
1736  }
1737  return list;
1738 }
1739 
1740 static LINK_ELEMENT *
1742 {
1743  LINK_ELEMENT *list = iobj->link.next;
1744 
1745  while (list) {
1746  if (list->type == ISEQ_ELEMENT_INSN || list->type == ISEQ_ELEMENT_ADJUST) {
1747  return list;
1748  }
1749  list = list->next;
1750  }
1751  return 0;
1752 }
1753 
1754 static LINK_ELEMENT *
1756 {
1757  LINK_ELEMENT *list = iobj->link.prev;
1758 
1759  while (list) {
1760  if (list->type == ISEQ_ELEMENT_INSN || list->type == ISEQ_ELEMENT_ADJUST) {
1761  return list;
1762  }
1763  list = list->prev;
1764  }
1765  return 0;
1766 }
1767 
1768 static int
1769 iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcallopt)
1770 {
1771  INSN *iobj = (INSN *)list;
1772  again:
1773  if (iobj->insn_id == BIN(jump)) {
1774  INSN *niobj, *diobj, *piobj;
1775  /*
1776  * useless jump elimination:
1777  * jump LABEL1
1778  * ...
1779  * LABEL1:
1780  * jump LABEL2
1781  *
1782  * => in this case, first jump instruction should jump to
1783  * LABEL2 directly
1784  */
1785  diobj = (INSN *)get_destination_insn(iobj);
1786  niobj = (INSN *)get_next_insn(iobj);
1787 
1788  if (diobj == niobj) {
1789  /*
1790  * jump LABEL
1791  * LABEL:
1792  * =>
1793  * LABEL:
1794  */
1795  REMOVE_ELEM(&iobj->link);
1796  }
1797  else if (iobj != diobj && diobj->insn_id == BIN(jump)) {
1798  if (OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0)) {
1799  OPERAND_AT(iobj, 0) = OPERAND_AT(diobj, 0);
1800  goto again;
1801  }
1802  }
1803  else if (diobj->insn_id == BIN(leave)) {
1804  /*
1805  * jump LABEL
1806  * ...
1807  * LABEL:
1808  * leave
1809  * =>
1810  * leave
1811  * ...
1812  * LABEL:
1813  * leave
1814  */
1815  INSN *eiobj = new_insn_core(iseq, iobj->line_no, BIN(leave),
1816  diobj->operand_size, diobj->operands);
1817  INSN *popiobj = new_insn_core(iseq, iobj->line_no,
1818  BIN(pop), 0, 0);
1819  /* replace */
1820  REPLACE_ELEM((LINK_ELEMENT *)iobj, (LINK_ELEMENT *)eiobj);
1821  INSERT_ELEM_NEXT((LINK_ELEMENT *)eiobj, (LINK_ELEMENT *)popiobj);
1822  iobj = popiobj;
1823  }
1824  /*
1825  * useless jump elimination (if/unless destination):
1826  * if L1
1827  * jump L2
1828  * L1:
1829  * ...
1830  * L2:
1831  *
1832  * ==>
1833  * unless L2
1834  * L1:
1835  * ...
1836  * L2:
1837  */
1838  else if ((piobj = (INSN *)get_prev_insn(iobj)) != 0 &&
1839  (piobj->insn_id == BIN(branchif) ||
1840  piobj->insn_id == BIN(branchunless))) {
1841  if (niobj == (INSN *)get_destination_insn(piobj)) {
1842  piobj->insn_id = (piobj->insn_id == BIN(branchif))
1843  ? BIN(branchunless) : BIN(branchif);
1844  OPERAND_AT(piobj, 0) = OPERAND_AT(iobj, 0);
1845  REMOVE_ELEM(&iobj->link);
1846  }
1847  }
1848  }
1849 
1850  if (iobj->insn_id == BIN(branchif) ||
1851  iobj->insn_id == BIN(branchunless)) {
1852  /*
1853  * if L1
1854  * ...
1855  * L1:
1856  * jump L2
1857  * =>
1858  * if L2
1859  */
1860  INSN *nobj = (INSN *)get_destination_insn(iobj);
1861  if (nobj->insn_id == BIN(jump)) {
1862  OPERAND_AT(iobj, 0) = OPERAND_AT(nobj, 0);
1863  }
1864  }
1865 
1866  if (do_tailcallopt && iobj->insn_id == BIN(leave)) {
1867  /*
1868  * send ...
1869  * leave
1870  * =>
1871  * send ..., ... | VM_CALL_TAILCALL, ...
1872  * leave # unreachable
1873  */
1874  INSN *piobj = (INSN *)get_prev_insn((INSN *)list);
1875 
1876  if (piobj->insn_id == BIN(send) || piobj->insn_id == BIN(opt_send_simple)) {
1877  rb_call_info_t *ci = (rb_call_info_t *)piobj->operands[0];
1878  if (ci->blockiseq == 0) {
1879  ci->flag |= VM_CALL_TAILCALL;
1880  }
1881  }
1882  }
1883  return COMPILE_OK;
1884 }
1885 
1886 static int
1888 {
1889  int old_opsize = iobj->operand_size;
1890  iobj->insn_id = insn_id;
1891  iobj->operand_size = insn_len(insn_id) - 1;
1892 
1893  if (iobj->operand_size > old_opsize) {
1894  VALUE *old_operands = iobj->operands;
1895  if (insn_id != BIN(opt_neq)) {
1896  rb_bug("insn_set_specialized_instruction: unknown insn: %d", insn_id);
1897  }
1898  iobj->operands = (VALUE *)compile_data_alloc(iseq, iobj->operand_size * sizeof(VALUE));
1899  iobj->operands[0] = old_operands[0];
1900  iobj->operands[1] = (VALUE)new_callinfo(iseq, idEq, 1, 0, 0);
1901  }
1902 
1903  return COMPILE_OK;
1904 }
1905 
1906 static int
1908 {
1909  if (iobj->insn_id == BIN(send)) {
1910  rb_call_info_t *ci = (rb_call_info_t *)OPERAND_AT(iobj, 0);
1911 
1912 #define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
1913  if (ci->blockiseq == 0 && (ci->flag & ~VM_CALL_ARGS_SKIP_SETUP) == 0) {
1914  switch (ci->orig_argc) {
1915  case 0:
1916  switch (ci->mid) {
1917  case idLength: SP_INSN(length); return COMPILE_OK;
1918  case idSize: SP_INSN(size); return COMPILE_OK;
1919  case idEmptyP: SP_INSN(empty_p);return COMPILE_OK;
1920  case idSucc: SP_INSN(succ); return COMPILE_OK;
1921  case idNot: SP_INSN(not); return COMPILE_OK;
1922  }
1923  break;
1924  case 1:
1925  switch (ci->mid) {
1926  case idPLUS: SP_INSN(plus); return COMPILE_OK;
1927  case idMINUS: SP_INSN(minus); return COMPILE_OK;
1928  case idMULT: SP_INSN(mult); return COMPILE_OK;
1929  case idDIV: SP_INSN(div); return COMPILE_OK;
1930  case idMOD: SP_INSN(mod); return COMPILE_OK;
1931  case idEq: SP_INSN(eq); return COMPILE_OK;
1932  case idNeq: SP_INSN(neq); return COMPILE_OK;
1933  case idLT: SP_INSN(lt); return COMPILE_OK;
1934  case idLE: SP_INSN(le); return COMPILE_OK;
1935  case idGT: SP_INSN(gt); return COMPILE_OK;
1936  case idGE: SP_INSN(ge); return COMPILE_OK;
1937  case idLTLT: SP_INSN(ltlt); return COMPILE_OK;
1938  case idAREF: SP_INSN(aref); return COMPILE_OK;
1939  }
1940  break;
1941  }
1942  }
1943  if (ci->flag & VM_CALL_ARGS_SKIP_SETUP) {
1944  iobj->insn_id = BIN(opt_send_simple);
1945  }
1946  }
1947 #undef SP_INSN
1948 
1949  return COMPILE_OK;
1950 }
1951 
1952 static int
1954 {
1955  LINK_ELEMENT *list;
1956  const int do_peepholeopt = iseq->compile_data->option->peephole_optimization;
1957  const int do_tailcallopt = iseq->compile_data->option->tailcall_optimization;
1958  const int do_si = iseq->compile_data->option->specialized_instruction;
1959  const int do_ou = iseq->compile_data->option->operands_unification;
1960  list = FIRST_ELEMENT(anchor);
1961 
1962  while (list) {
1963  if (list->type == ISEQ_ELEMENT_INSN) {
1964  if (do_peepholeopt) {
1965  iseq_peephole_optimize(iseq, list, do_tailcallopt);
1966  }
1967  if (do_si) {
1968  iseq_specialized_instruction(iseq, (INSN *)list);
1969  }
1970  if (do_ou) {
1971  insn_operands_unification((INSN *)list);
1972  }
1973  }
1974  list = list->next;
1975  }
1976  return COMPILE_OK;
1977 }
1978 
1979 #if OPT_INSTRUCTIONS_UNIFICATION
1980 static INSN *
1981 new_unified_insn(rb_iseq_t *iseq,
1982  int insn_id, int size, LINK_ELEMENT *seq_list)
1983 {
1984  INSN *iobj = 0;
1985  LINK_ELEMENT *list = seq_list;
1986  int i, argc = 0;
1987  VALUE *operands = 0, *ptr = 0;
1988 
1989 
1990  /* count argc */
1991  for (i = 0; i < size; i++) {
1992  iobj = (INSN *)list;
1993  argc += iobj->operand_size;
1994  list = list->next;
1995  }
1996 
1997  if (argc > 0) {
1998  ptr = operands =
1999  (VALUE *)compile_data_alloc(iseq, sizeof(VALUE) * argc);
2000  }
2001 
2002  /* copy operands */
2003  list = seq_list;
2004  for (i = 0; i < size; i++) {
2005  iobj = (INSN *)list;
2006  MEMCPY(ptr, iobj->operands, VALUE, iobj->operand_size);
2007  ptr += iobj->operand_size;
2008  list = list->next;
2009  }
2010 
2011  return new_insn_core(iseq, iobj->line_no, insn_id, argc, operands);
2012 }
2013 #endif
2014 
2015 /*
2016  * This scheme can get more performance if do this optimize with
2017  * label address resolving.
2018  * It's future work (if compile time was bottle neck).
2019  */
2020 static int
2022 {
2023 #if OPT_INSTRUCTIONS_UNIFICATION
2024  LINK_ELEMENT *list;
2025  INSN *iobj, *niobj;
2026  int id, k;
2027  intptr_t j;
2028 
2029  list = FIRST_ELEMENT(anchor);
2030  while (list) {
2031  if (list->type == ISEQ_ELEMENT_INSN) {
2032  iobj = (INSN *)list;
2033  id = iobj->insn_id;
2034  if (unified_insns_data[id] != 0) {
2035  const int *const *entry = unified_insns_data[id];
2036  for (j = 1; j < (intptr_t)entry[0]; j++) {
2037  const int *unified = entry[j];
2038  LINK_ELEMENT *li = list->next;
2039  for (k = 2; k < unified[1]; k++) {
2040  if (li->type != ISEQ_ELEMENT_INSN ||
2041  ((INSN *)li)->insn_id != unified[k]) {
2042  goto miss;
2043  }
2044  li = li->next;
2045  }
2046  /* matched */
2047  niobj =
2048  new_unified_insn(iseq, unified[0], unified[1] - 1,
2049  list);
2050 
2051  /* insert to list */
2052  niobj->link.prev = (LINK_ELEMENT *)iobj->link.prev;
2053  niobj->link.next = li;
2054  if (li) {
2055  li->prev = (LINK_ELEMENT *)niobj;
2056  }
2057 
2058  list->prev->next = (LINK_ELEMENT *)niobj;
2059  list = (LINK_ELEMENT *)niobj;
2060  break;
2061  miss:;
2062  }
2063  }
2064  }
2065  list = list->next;
2066  }
2067 #endif
2068  return COMPILE_OK;
2069 }
2070 
2071 #if OPT_STACK_CACHING
2072 
2073 #define SC_INSN(insn, stat) sc_insn_info[(insn)][(stat)]
2074 #define SC_NEXT(insn) sc_insn_next[(insn)]
2075 
2076 #include "opt_sc.inc"
2077 
2078 static int
2079 insn_set_sc_state(rb_iseq_t *iseq, INSN *iobj, int state)
2080 {
2081  int nstate;
2082  int insn_id;
2083 
2084  insn_id = iobj->insn_id;
2085  iobj->insn_id = SC_INSN(insn_id, state);
2086  nstate = SC_NEXT(iobj->insn_id);
2087 
2088  if (insn_id == BIN(jump) ||
2089  insn_id == BIN(branchif) || insn_id == BIN(branchunless)) {
2090  LABEL *lobj = (LABEL *)OPERAND_AT(iobj, 0);
2091 
2092  if (lobj->sc_state != 0) {
2093  if (lobj->sc_state != nstate) {
2094  dump_disasm_list((LINK_ELEMENT *)iobj);
2095  dump_disasm_list((LINK_ELEMENT *)lobj);
2096  printf("\n-- %d, %d\n", lobj->sc_state, nstate);
2098  "insn_set_sc_state error\n");
2099  return 0;
2100  }
2101  }
2102  else {
2103  lobj->sc_state = nstate;
2104  }
2105  if (insn_id == BIN(jump)) {
2106  nstate = SCS_XX;
2107  }
2108  }
2109  else if (insn_id == BIN(leave)) {
2110  nstate = SCS_XX;
2111  }
2112 
2113  return nstate;
2114 }
2115 
2116 static int
2117 label_set_sc_state(LABEL *lobj, int state)
2118 {
2119  if (lobj->sc_state != 0) {
2120  if (lobj->sc_state != state) {
2121  state = lobj->sc_state;
2122  }
2123  }
2124  else {
2125  lobj->sc_state = state;
2126  }
2127 
2128  return state;
2129 }
2130 
2131 
2132 #endif
2133 
2134 static int
2136 {
2137 #if OPT_STACK_CACHING
2138  LINK_ELEMENT *list;
2139  int state, insn_id;
2140 
2141  /* initialize */
2142  state = SCS_XX;
2143  list = FIRST_ELEMENT(anchor);
2144  /* dump_disasm_list(list); */
2145 
2146  /* for each list element */
2147  while (list) {
2148  redo_point:
2149  switch (list->type) {
2150  case ISEQ_ELEMENT_INSN:
2151  {
2152  INSN *iobj = (INSN *)list;
2153  insn_id = iobj->insn_id;
2154 
2155  /* dump_disasm_list(list); */
2156 
2157  switch (insn_id) {
2158  case BIN(nop):
2159  {
2160  /* exception merge point */
2161  if (state != SCS_AX) {
2162  INSN *rpobj =
2163  new_insn_body(iseq, 0, BIN(reput), 0);
2164 
2165  /* replace this insn */
2166  REPLACE_ELEM(list, (LINK_ELEMENT *)rpobj);
2167  list = (LINK_ELEMENT *)rpobj;
2168  goto redo_point;
2169  }
2170  break;
2171  }
2172  case BIN(swap):
2173  {
2174  if (state == SCS_AB || state == SCS_BA) {
2175  state = (state == SCS_AB ? SCS_BA : SCS_AB);
2176 
2177  REMOVE_ELEM(list);
2178  list = list->next;
2179  goto redo_point;
2180  }
2181  break;
2182  }
2183  case BIN(pop):
2184  {
2185  switch (state) {
2186  case SCS_AX:
2187  case SCS_BX:
2188  state = SCS_XX;
2189  break;
2190  case SCS_AB:
2191  state = SCS_AX;
2192  break;
2193  case SCS_BA:
2194  state = SCS_BX;
2195  break;
2196  case SCS_XX:
2197  goto normal_insn;
2198  default:
2200  "unreachable");
2201  }
2202  /* remove useless pop */
2203  REMOVE_ELEM(list);
2204  list = list->next;
2205  goto redo_point;
2206  }
2207  default:;
2208  /* none */
2209  } /* end of switch */
2210  normal_insn:
2211  state = insn_set_sc_state(iseq, iobj, state);
2212  break;
2213  }
2214  case ISEQ_ELEMENT_LABEL:
2215  {
2216  LABEL *lobj;
2217  lobj = (LABEL *)list;
2218 
2219  state = label_set_sc_state(lobj, state);
2220  }
2221  default:
2222  break;
2223  }
2224  list = list->next;
2225  }
2226 #endif
2227  return COMPILE_OK;
2228 }
2229 
2230 static int
2231 compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE *node, int *cntp)
2232 {
2233  NODE *list = node->nd_next;
2234  VALUE lit = node->nd_lit;
2235  int cnt = 0;
2236 
2237  debugp_param("nd_lit", lit);
2238  if (!NIL_P(lit)) {
2239  hide_obj(lit);
2240  cnt++;
2241  ADD_INSN1(ret, nd_line(node), putobject, lit);
2242  }
2243 
2244  while (list) {
2245  node = list->nd_head;
2246  if (nd_type(node) == NODE_STR) {
2247  hide_obj(node->nd_lit);
2248  ADD_INSN1(ret, nd_line(node), putobject, node->nd_lit);
2249  }
2250  else {
2251  COMPILE(ret, "each string", node);
2252  }
2253  cnt++;
2254  list = list->nd_next;
2255  }
2256  *cntp = cnt;
2257 
2258  return COMPILE_OK;
2259 }
2260 
2261 static int
2263 {
2264  int cnt;
2265  compile_dstr_fragments(iseq, ret, node, &cnt);
2266  ADD_INSN1(ret, nd_line(node), concatstrings, INT2FIX(cnt));
2267  return COMPILE_OK;
2268 }
2269 
2270 static int
2272 {
2273  int cnt;
2274  compile_dstr_fragments(iseq, ret, node, &cnt);
2275  ADD_INSN2(ret, nd_line(node), toregexp, INT2FIX(node->nd_cflag), INT2FIX(cnt));
2276  return COMPILE_OK;
2277 }
2278 
2279 static int
2281  LABEL *then_label, LABEL *else_label)
2282 {
2283  switch (nd_type(cond)) {
2284  case NODE_AND:
2285  {
2286  LABEL *label = NEW_LABEL(nd_line(cond));
2287  compile_branch_condition(iseq, ret, cond->nd_1st, label,
2288  else_label);
2289  ADD_LABEL(ret, label);
2290  compile_branch_condition(iseq, ret, cond->nd_2nd, then_label,
2291  else_label);
2292  break;
2293  }
2294  case NODE_OR:
2295  {
2296  LABEL *label = NEW_LABEL(nd_line(cond));
2297  compile_branch_condition(iseq, ret, cond->nd_1st, then_label,
2298  label);
2299  ADD_LABEL(ret, label);
2300  compile_branch_condition(iseq, ret, cond->nd_2nd, then_label,
2301  else_label);
2302  break;
2303  }
2304  case NODE_LIT: /* NODE_LIT is always not true */
2305  case NODE_TRUE:
2306  case NODE_STR:
2307  /* printf("useless condition eliminate (%s)\n", ruby_node_name(nd_type(cond))); */
2308  ADD_INSNL(ret, nd_line(cond), jump, then_label);
2309  break;
2310  case NODE_FALSE:
2311  case NODE_NIL:
2312  /* printf("useless condition eliminate (%s)\n", ruby_node_name(nd_type(cond))); */
2313  ADD_INSNL(ret, nd_line(cond), jump, else_label);
2314  break;
2315  default:
2316  COMPILE(ret, "branch condition", cond);
2317  ADD_INSNL(ret, nd_line(cond), branchunless, else_label);
2318  ADD_INSNL(ret, nd_line(cond), jump, then_label);
2319  break;
2320  }
2321  return COMPILE_OK;
2322 }
2323 
2328 };
2329 
2330 static int
2331 compile_array_(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE* node_root,
2332  enum compile_array_type_t type, int poped)
2333 {
2334  NODE *node = node_root;
2335  int line = (int)nd_line(node);
2336  int len = 0;
2337 
2338  if (nd_type(node) == NODE_ZARRAY) {
2339  if (!poped) {
2340  switch (type) {
2341  case COMPILE_ARRAY_TYPE_ARRAY: ADD_INSN1(ret, line, newarray, INT2FIX(0)); break;
2342  case COMPILE_ARRAY_TYPE_HASH: ADD_INSN1(ret, line, newhash, INT2FIX(0)); break;
2343  case COMPILE_ARRAY_TYPE_ARGS: /* do nothing */ break;
2344  }
2345  }
2346  }
2347  else {
2348  int opt_p = 1;
2349  int first = 1, i;
2350 
2351  while (node) {
2352  NODE *start_node = node, *end_node;
2353  NODE *kw = 0;
2354  const int max = 0x100;
2355  DECL_ANCHOR(anchor);
2356  INIT_ANCHOR(anchor);
2357 
2358  for (i=0; i<max && node; i++, len++, node = node->nd_next) {
2359  if (CPDEBUG > 0 && nd_type(node) != NODE_ARRAY) {
2360  rb_bug("compile_array: This node is not NODE_ARRAY, but %s", ruby_node_name(nd_type(node)));
2361  }
2362 
2363  if (type == COMPILE_ARRAY_TYPE_HASH && !node->nd_head) {
2364  opt_p = 0;
2365  kw = node->nd_next;
2366  node = kw->nd_next;
2367  kw = kw->nd_head;
2368  break;
2369  }
2370  if (opt_p && nd_type(node->nd_head) != NODE_LIT) {
2371  opt_p = 0;
2372  }
2373 
2374  COMPILE_(anchor, "array element", node->nd_head, poped);
2375  }
2376 
2377  if (opt_p && type != COMPILE_ARRAY_TYPE_ARGS) {
2378  if (!poped) {
2379  VALUE ary = rb_ary_tmp_new(i);
2380 
2381  end_node = node;
2382  node = start_node;
2383 
2384  while (node != end_node) {
2385  rb_ary_push(ary, node->nd_head->nd_lit);
2386  node = node->nd_next;
2387  }
2388  while (node && nd_type(node->nd_head) == NODE_LIT &&
2389  node->nd_next && nd_type(node->nd_next->nd_head) == NODE_LIT) {
2390  rb_ary_push(ary, node->nd_head->nd_lit);
2391  node = node->nd_next;
2392  rb_ary_push(ary, node->nd_head->nd_lit);
2393  node = node->nd_next;
2394  len++;
2395  }
2396 
2397  OBJ_FREEZE(ary);
2398 
2400 
2401  if (first) {
2402  first = 0;
2403  if (type == COMPILE_ARRAY_TYPE_ARRAY) {
2404  ADD_INSN1(ret, line, duparray, ary);
2405  }
2406  else { /* COMPILE_ARRAY_TYPE_HASH */
2407  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2408  ADD_INSN1(ret, line, putobject, ary);
2409  ADD_SEND(ret, line, ID2SYM(id_core_hash_from_ary), INT2FIX(1));
2410  }
2411  }
2412  else {
2413  if (type == COMPILE_ARRAY_TYPE_ARRAY) {
2414  ADD_INSN1(ret, line, putobject, ary);
2415  ADD_INSN(ret, line, concatarray);
2416  }
2417  else {
2418  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2419  ADD_INSN1(ret, line, putobject, ary);
2420  ADD_SEND(ret, line, ID2SYM(id_core_hash_merge_ary), INT2FIX(1));
2421  }
2422  }
2423  }
2424  }
2425  else {
2426  if (!poped) {
2427  switch (type) {
2429  ADD_INSN1(anchor, line, newarray, INT2FIX(i));
2430 
2431  if (first) {
2432  first = 0;
2433  }
2434  else {
2435  ADD_INSN(anchor, line, concatarray);
2436  }
2437 
2438  APPEND_LIST(ret, anchor);
2439  break;
2441  if (first) {
2442  first = 0;
2443  ADD_INSN1(anchor, line, newhash, INT2FIX(i));
2444  APPEND_LIST(ret, anchor);
2445  }
2446  else if (i > 0) {
2447  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2448  ADD_INSN(ret, line, swap);
2449  APPEND_LIST(ret, anchor);
2450  ADD_SEND(ret, line, ID2SYM(id_core_hash_merge_ptr), INT2FIX(i + 1));
2451  }
2452  if (kw) {
2453  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2454  ADD_INSN(ret, line, swap);
2455  COMPILE(ret, "keyword splat", kw);
2456  ADD_SEND(ret, line, ID2SYM(id_core_hash_merge_kwd), INT2FIX(2));
2457  }
2458  break;
2460  APPEND_LIST(ret, anchor);
2461  break;
2462  }
2463  }
2464  else {
2465  /* poped */
2466  APPEND_LIST(ret, anchor);
2467  }
2468  }
2469  }
2470  }
2471  return len;
2472 }
2473 
2474 static VALUE
2476 {
2477  return compile_array_(iseq, ret, node_root, type, 0);
2478 }
2479 
2480 static VALUE
2482 {
2483  switch (nd_type(node)) {
2484  case NODE_LIT: {
2485  VALUE v = node->nd_lit;
2486  double ival;
2487  if (RB_TYPE_P(v, T_FLOAT) &&
2488  modf(RFLOAT_VALUE(v), &ival) == 0.0) {
2489  return FIXABLE(ival) ? LONG2FIX((long)ival) : rb_dbl2big(ival);
2490  }
2491  if (SYMBOL_P(v) || rb_obj_is_kind_of(v, rb_cNumeric)) {
2492  return v;
2493  }
2494  break;
2495  }
2496  case NODE_STR:
2497  return node->nd_lit;
2498  }
2499  return Qundef;
2500 }
2501 
2502 static int
2503 when_vals(rb_iseq_t *iseq, LINK_ANCHOR *cond_seq, NODE *vals, LABEL *l1, int only_special_literals, VALUE literals)
2504 {
2505  while (vals) {
2506  NODE* val = vals->nd_head;
2508 
2509  if (lit == Qundef) {
2510  only_special_literals = 0;
2511  }
2512  else {
2513  if (rb_hash_lookup(literals, lit) != Qnil) {
2514  rb_compile_warning(RSTRING_PTR(iseq->location.path), nd_line(val), "duplicated when clause is ignored");
2515  }
2516  else {
2517  rb_hash_aset(literals, lit, (VALUE)(l1) | 1);
2518  }
2519  }
2520 
2521  ADD_INSN(cond_seq, nd_line(val), dup); /* dup target */
2522 
2523  if (nd_type(val) == NODE_STR) {
2524  debugp_param("nd_lit", val->nd_lit);
2525  OBJ_FREEZE(val->nd_lit);
2526  ADD_INSN1(cond_seq, nd_line(val), putobject, val->nd_lit);
2527  }
2528  else {
2529  COMPILE(cond_seq, "when cond", val);
2530  }
2531 
2532  ADD_INSN1(cond_seq, nd_line(vals), checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2533  ADD_INSNL(cond_seq, nd_line(val), branchif, l1);
2534  vals = vals->nd_next;
2535  }
2536  return only_special_literals;
2537 }
2538 
2539 static int
2541 {
2542  switch (nd_type(node)) {
2543  case NODE_ATTRASGN: {
2544  INSN *iobj;
2545  rb_call_info_t *ci;
2546  VALUE dupidx;
2547 
2548  COMPILE_POPED(ret, "masgn lhs (NODE_ATTRASGN)", node);
2549 
2550  POP_ELEMENT(ret); /* pop pop insn */
2551  iobj = (INSN *)POP_ELEMENT(ret); /* pop send insn */
2552  ci = (rb_call_info_t *)iobj->operands[0];
2553  ci->orig_argc += 1; ci->argc = ci->orig_argc;
2554  dupidx = INT2FIX(ci->orig_argc);
2555 
2556  ADD_INSN1(ret, nd_line(node), topn, dupidx);
2557  ADD_ELEM(ret, (LINK_ELEMENT *)iobj);
2558  ADD_INSN(ret, nd_line(node), pop); /* result */
2559  ADD_INSN(ret, nd_line(node), pop); /* rhs */
2560  break;
2561  }
2562  case NODE_MASGN: {
2563  DECL_ANCHOR(anchor);
2564  INIT_ANCHOR(anchor);
2565  COMPILE_POPED(anchor, "nest masgn lhs", node);
2566  REMOVE_ELEM(FIRST_ELEMENT(anchor));
2567  ADD_SEQ(ret, anchor);
2568  break;
2569  }
2570  default: {
2571  DECL_ANCHOR(anchor);
2572  INIT_ANCHOR(anchor);
2573  COMPILE_POPED(anchor, "masgn lhs", node);
2574  REMOVE_ELEM(FIRST_ELEMENT(anchor));
2575  ADD_SEQ(ret, anchor);
2576  }
2577  }
2578 
2579  return COMPILE_OK;
2580 }
2581 
2582 static void
2584 {
2585  if (lhsn) {
2586  compile_massign_opt_lhs(iseq, ret, lhsn->nd_next);
2587  compile_massign_lhs(iseq, ret, lhsn->nd_head);
2588  }
2589 }
2590 
2591 static int
2593  NODE *rhsn, NODE *orig_lhsn)
2594 {
2595  VALUE mem[64];
2596  const int memsize = numberof(mem);
2597  int memindex = 0;
2598  int llen = 0, rlen = 0;
2599  int i;
2600  NODE *lhsn = orig_lhsn;
2601 
2602 #define MEMORY(v) { \
2603  int i; \
2604  if (memindex == memsize) return 0; \
2605  for (i=0; i<memindex; i++) { \
2606  if (mem[i] == (v)) return 0; \
2607  } \
2608  mem[memindex++] = (v); \
2609 }
2610 
2611  if (rhsn == 0 || nd_type(rhsn) != NODE_ARRAY) {
2612  return 0;
2613  }
2614 
2615  while (lhsn) {
2616  NODE *ln = lhsn->nd_head;
2617  switch (nd_type(ln)) {
2618  case NODE_LASGN:
2619  MEMORY(ln->nd_vid);
2620  break;
2621  case NODE_DASGN:
2622  case NODE_DASGN_CURR:
2623  case NODE_IASGN:
2624  case NODE_IASGN2:
2625  case NODE_CVASGN:
2626  MEMORY(ln->nd_vid);
2627  break;
2628  default:
2629  return 0;
2630  }
2631  lhsn = lhsn->nd_next;
2632  llen++;
2633  }
2634 
2635  while (rhsn) {
2636  if (llen <= rlen) {
2637  COMPILE_POPED(ret, "masgn val (popped)", rhsn->nd_head);
2638  }
2639  else {
2640  COMPILE(ret, "masgn val", rhsn->nd_head);
2641  }
2642  rhsn = rhsn->nd_next;
2643  rlen++;
2644  }
2645 
2646  if (llen > rlen) {
2647  for (i=0; i<llen-rlen; i++) {
2648  ADD_INSN(ret, nd_line(orig_lhsn), putnil);
2649  }
2650  }
2651 
2652  compile_massign_opt_lhs(iseq, ret, orig_lhsn);
2653  return 1;
2654 }
2655 
2656 static int
2657 compile_massign(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE *node, int poped)
2658 {
2659  NODE *rhsn = node->nd_value;
2660  NODE *splatn = node->nd_args;
2661  NODE *lhsn = node->nd_head;
2662  int lhs_splat = (splatn && (VALUE)splatn != (VALUE)-1) ? 1 : 0;
2663 
2664  if (!poped || splatn || !compile_massign_opt(iseq, ret, rhsn, lhsn)) {
2665  int llen = 0;
2666  DECL_ANCHOR(lhsseq);
2667 
2668  INIT_ANCHOR(lhsseq);
2669 
2670  while (lhsn) {
2671  compile_massign_lhs(iseq, lhsseq, lhsn->nd_head);
2672  llen += 1;
2673  lhsn = lhsn->nd_next;
2674  }
2675 
2676  COMPILE(ret, "normal masgn rhs", rhsn);
2677 
2678  if (!poped) {
2679  ADD_INSN(ret, nd_line(node), dup);
2680  }
2681 
2682  ADD_INSN2(ret, nd_line(node), expandarray,
2683  INT2FIX(llen), INT2FIX(lhs_splat));
2684  ADD_SEQ(ret, lhsseq);
2685 
2686  if (lhs_splat) {
2687  if (nd_type(splatn) == NODE_POSTARG) {
2688  /*a, b, *r, p1, p2 */
2689  NODE *postn = splatn->nd_2nd;
2690  NODE *restn = splatn->nd_1st;
2691  int num = (int)postn->nd_alen;
2692  int flag = 0x02 | (((VALUE)restn == (VALUE)-1) ? 0x00 : 0x01);
2693 
2694  ADD_INSN2(ret, nd_line(splatn), expandarray,
2695  INT2FIX(num), INT2FIX(flag));
2696 
2697  if ((VALUE)restn != (VALUE)-1) {
2698  compile_massign_lhs(iseq, ret, restn);
2699  }
2700  while (postn) {
2701  compile_massign_lhs(iseq, ret, postn->nd_head);
2702  postn = postn->nd_next;
2703  }
2704  }
2705  else {
2706  /* a, b, *r */
2707  compile_massign_lhs(iseq, ret, splatn);
2708  }
2709  }
2710  }
2711  return COMPILE_OK;
2712 }
2713 
2714 static int
2716  LINK_ANCHOR *pref, LINK_ANCHOR *body)
2717 {
2718  switch (nd_type(node)) {
2719  case NODE_CONST:
2720  debugi("compile_colon2 - colon", node->nd_vid);
2721  ADD_INSN1(body, nd_line(node), getconstant, ID2SYM(node->nd_vid));
2722  break;
2723  case NODE_COLON3:
2724  debugi("compile_colon2 - colon3", node->nd_mid);
2725  ADD_INSN(body, nd_line(node), pop);
2726  ADD_INSN1(body, nd_line(node), putobject, rb_cObject);
2727  ADD_INSN1(body, nd_line(node), getconstant, ID2SYM(node->nd_mid));
2728  break;
2729  case NODE_COLON2:
2730  compile_colon2(iseq, node->nd_head, pref, body);
2731  debugi("compile_colon2 - colon2", node->nd_mid);
2732  ADD_INSN1(body, nd_line(node), getconstant, ID2SYM(node->nd_mid));
2733  break;
2734  default:
2735  COMPILE(pref, "const colon2 prefix", node);
2736  break;
2737  }
2738  return COMPILE_OK;
2739 }
2740 
2741 static VALUE
2743 {
2744  if (nd_type(cpath) == NODE_COLON3) {
2745  /* toplevel class ::Foo */
2746  ADD_INSN1(ret, nd_line(cpath), putobject, rb_cObject);
2747  return Qfalse;
2748  }
2749  else if (cpath->nd_head) {
2750  /* Bar::Foo */
2751  COMPILE(ret, "nd_else->nd_head", cpath->nd_head);
2752  return Qfalse;
2753  }
2754  else {
2755  /* class at cbase Foo */
2756  ADD_INSN1(ret, nd_line(cpath), putspecialobject,
2758  return Qtrue;
2759  }
2760 }
2761 
2762 #define defined_expr defined_expr0
2763 static int
2765  NODE *node, LABEL **lfinish, VALUE needstr)
2766 {
2767  enum defined_type expr_type = 0;
2768  enum node_type type;
2769 
2770  switch (type = nd_type(node)) {
2771 
2772  /* easy literals */
2773  case NODE_NIL:
2774  expr_type = DEFINED_NIL;
2775  break;
2776  case NODE_SELF:
2777  expr_type = DEFINED_SELF;
2778  break;
2779  case NODE_TRUE:
2780  expr_type = DEFINED_TRUE;
2781  break;
2782  case NODE_FALSE:
2783  expr_type = DEFINED_FALSE;
2784  break;
2785 
2786  case NODE_ARRAY:{
2787  NODE *vals = node;
2788 
2789  do {
2790  defined_expr(iseq, ret, vals->nd_head, lfinish, Qfalse);
2791 
2792  if (!lfinish[1]) {
2793  lfinish[1] = NEW_LABEL(nd_line(node));
2794  }
2795  ADD_INSNL(ret, nd_line(node), branchunless, lfinish[1]);
2796  } while ((vals = vals->nd_next) != NULL);
2797  }
2798  case NODE_STR:
2799  case NODE_LIT:
2800  case NODE_ZARRAY:
2801  case NODE_AND:
2802  case NODE_OR:
2803  default:
2804  expr_type = DEFINED_EXPR;
2805  break;
2806 
2807  /* variables */
2808  case NODE_LVAR:
2809  case NODE_DVAR:
2810  expr_type = DEFINED_LVAR;
2811  break;
2812 
2813  case NODE_IVAR:
2814  ADD_INSN(ret, nd_line(node), putnil);
2815  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_IVAR),
2816  ID2SYM(node->nd_vid), needstr);
2817  return 1;
2818 
2819  case NODE_GVAR:
2820  ADD_INSN(ret, nd_line(node), putnil);
2821  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_GVAR),
2822  ID2SYM(node->nd_entry->id), needstr);
2823  return 1;
2824 
2825  case NODE_CVAR:
2826  ADD_INSN(ret, nd_line(node), putnil);
2827  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_CVAR),
2828  ID2SYM(node->nd_vid), needstr);
2829  return 1;
2830 
2831  case NODE_CONST:
2832  ADD_INSN(ret, nd_line(node), putnil);
2833  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_CONST),
2834  ID2SYM(node->nd_vid), needstr);
2835  return 1;
2836  case NODE_COLON2:
2837  if (!lfinish[1]) {
2838  lfinish[1] = NEW_LABEL(nd_line(node));
2839  }
2840  defined_expr(iseq, ret, node->nd_head, lfinish, Qfalse);
2841  ADD_INSNL(ret, nd_line(node), branchunless, lfinish[1]);
2842 
2843  if (rb_is_const_id(node->nd_mid)) {
2844  COMPILE(ret, "defined/colon2#nd_head", node->nd_head);
2845  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_CONST),
2846  ID2SYM(node->nd_mid), needstr);
2847  }
2848  else {
2849  COMPILE(ret, "defined/colon2#nd_head", node->nd_head);
2850  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_METHOD),
2851  ID2SYM(node->nd_mid), needstr);
2852  }
2853  return 1;
2854  case NODE_COLON3:
2855  ADD_INSN1(ret, nd_line(node), putobject, rb_cObject);
2856  ADD_INSN3(ret, nd_line(node), defined,
2857  INT2FIX(DEFINED_CONST), ID2SYM(node->nd_mid), needstr);
2858  return 1;
2859 
2860  /* method dispatch */
2861  case NODE_CALL:
2862  case NODE_VCALL:
2863  case NODE_FCALL:
2864  case NODE_ATTRASGN:{
2865  int self = TRUE;
2866 
2867  switch (type) {
2868  case NODE_ATTRASGN:
2869  if (node->nd_recv == (NODE *)1) break;
2870  case NODE_CALL:
2871  self = FALSE;
2872  break;
2873  default:
2874  /* through */;
2875  }
2876  if (!lfinish[1]) {
2877  lfinish[1] = NEW_LABEL(nd_line(node));
2878  }
2879  if (node->nd_args) {
2880  defined_expr(iseq, ret, node->nd_args, lfinish, Qfalse);
2881  ADD_INSNL(ret, nd_line(node), branchunless, lfinish[1]);
2882  }
2883  if (!self) {
2884  defined_expr(iseq, ret, node->nd_recv, lfinish, Qfalse);
2885  ADD_INSNL(ret, nd_line(node), branchunless, lfinish[1]);
2886  COMPILE(ret, "defined/recv", node->nd_recv);
2887  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_METHOD),
2888  ID2SYM(node->nd_mid), needstr);
2889  }
2890  else {
2891  ADD_INSN(ret, nd_line(node), putself);
2892  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_FUNC),
2893  ID2SYM(node->nd_mid), needstr);
2894  }
2895  return 1;
2896  }
2897 
2898  case NODE_YIELD:
2899  ADD_INSN(ret, nd_line(node), putnil);
2900  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_YIELD), 0,
2901  needstr);
2902  return 1;
2903 
2904  case NODE_BACK_REF:
2905  case NODE_NTH_REF:
2906  ADD_INSN(ret, nd_line(node), putnil);
2907  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_REF),
2908  INT2FIX((node->nd_nth << 1) | (type == NODE_BACK_REF)),
2909  needstr);
2910  return 1;
2911 
2912  case NODE_SUPER:
2913  case NODE_ZSUPER:
2914  ADD_INSN(ret, nd_line(node), putnil);
2915  ADD_INSN3(ret, nd_line(node), defined, INT2FIX(DEFINED_ZSUPER), 0,
2916  needstr);
2917  return 1;
2918 
2919  case NODE_OP_ASGN1:
2920  case NODE_OP_ASGN2:
2921  case NODE_OP_ASGN_OR:
2922  case NODE_OP_ASGN_AND:
2923  case NODE_MASGN:
2924  case NODE_LASGN:
2925  case NODE_DASGN:
2926  case NODE_DASGN_CURR:
2927  case NODE_GASGN:
2928  case NODE_IASGN:
2929  case NODE_CDECL:
2930  case NODE_CVDECL:
2931  case NODE_CVASGN:
2932  expr_type = DEFINED_ASGN;
2933  break;
2934  }
2935 
2936  if (expr_type) {
2937  if (needstr != Qfalse) {
2938  VALUE str = rb_iseq_defined_string(expr_type);
2939  ADD_INSN1(ret, nd_line(node), putobject, str);
2940  }
2941  else {
2942  ADD_INSN1(ret, nd_line(node), putobject, Qtrue);
2943  }
2944  return 1;
2945  }
2946  return 0;
2947 }
2948 #undef defined_expr
2949 
2950 static int
2951 defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *ret,
2952  NODE *node, LABEL **lfinish, VALUE needstr)
2953 {
2954  LINK_ELEMENT *lcur = ret->last;
2955  int done = defined_expr0(iseq, ret, node, lfinish, needstr);
2956  if (lfinish[1]) {
2957  int line = nd_line(node);
2958  LABEL *lstart = NEW_LABEL(line);
2959  LABEL *lend = NEW_LABEL(line);
2960  VALUE rescue = NEW_CHILD_ISEQVAL(NEW_NIL(),
2961  rb_str_concat(rb_str_new2
2962  ("defined guard in "),
2963  iseq->location.label),
2964  ISEQ_TYPE_DEFINED_GUARD, 0);
2965  APPEND_LABEL(ret, lcur, lstart);
2966  ADD_LABEL(ret, lend);
2967  ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
2968  }
2969  return done;
2970 }
2971 
2972 #define BUFSIZE 0x100
2973 
2974 static VALUE
2976 {
2977  int level = 1;
2978  rb_iseq_t *ip = iseq;
2979 
2980  if (iseq->parent_iseq != 0) {
2981  while (ip->local_iseq != ip) {
2982  if (ip->type == ISEQ_TYPE_BLOCK) {
2983  level++;
2984  }
2985  ip = ip->parent_iseq;
2986  }
2987  }
2988 
2989  if (level == 1) {
2990  return rb_sprintf("block in %s", RSTRING_PTR(ip->location.label));
2991  }
2992  else {
2993  return rb_sprintf("block (%d levels) in %s", level, RSTRING_PTR(ip->location.label));
2994  }
2995 }
2996 
2997 static void
3000  struct ensure_range *er, NODE *node)
3001 {
3002  enl->ensure_node = node;
3003  enl->prev = iseq->compile_data->ensure_node_stack; /* prev */
3004  enl->erange = er;
3005  iseq->compile_data->ensure_node_stack = enl;
3006 }
3007 
3008 static void
3010  LABEL *lstart, LABEL *lend)
3011 {
3012  struct ensure_range *ne =
3013  compile_data_alloc(iseq, sizeof(struct ensure_range));
3014 
3015  while (erange->next != 0) {
3016  erange = erange->next;
3017  }
3018  ne->next = 0;
3019  ne->begin = lend;
3020  ne->end = erange->end;
3021  erange->end = lstart;
3022 
3023  erange->next = ne;
3024 }
3025 
3026 static void
3027 add_ensure_iseq(LINK_ANCHOR *ret, rb_iseq_t *iseq, int is_return)
3028 {
3031  struct iseq_compile_data_ensure_node_stack *prev_enlp = enlp;
3032  DECL_ANCHOR(ensure);
3033 
3034  INIT_ANCHOR(ensure);
3035  while (enlp) {
3036  if (enlp->erange != 0) {
3037  DECL_ANCHOR(ensure_part);
3038  LABEL *lstart = NEW_LABEL(0);
3039  LABEL *lend = NEW_LABEL(0);
3040  INIT_ANCHOR(ensure_part);
3041 
3042  add_ensure_range(iseq, enlp->erange, lstart, lend);
3043 
3044  iseq->compile_data->ensure_node_stack = enlp->prev;
3045  ADD_LABEL(ensure_part, lstart);
3046  COMPILE_POPED(ensure_part, "ensure part", enlp->ensure_node);
3047  ADD_LABEL(ensure_part, lend);
3048  ADD_SEQ(ensure, ensure_part);
3049  }
3050  else {
3051  if (!is_return) {
3052  break;
3053  }
3054  }
3055  enlp = enlp->prev;
3056  }
3057  iseq->compile_data->ensure_node_stack = prev_enlp;
3058  ADD_SEQ(ret, ensure);
3059 }
3060 
3061 static VALUE
3063 {
3064  VALUE argc = INT2FIX(0);
3065  int nsplat = 0;
3066  DECL_ANCHOR(arg_block);
3067  DECL_ANCHOR(args_splat);
3068 
3069  INIT_ANCHOR(arg_block);
3070  INIT_ANCHOR(args_splat);
3071  if (argn && nd_type(argn) == NODE_BLOCK_PASS) {
3072  COMPILE(arg_block, "block", argn->nd_body);
3073  *flag |= VM_CALL_ARGS_BLOCKARG;
3074  argn = argn->nd_head;
3075  }
3076 
3077  setup_argn:
3078  if (argn) {
3079  switch (nd_type(argn)) {
3080  case NODE_SPLAT: {
3081  COMPILE(args, "args (splat)", argn->nd_head);
3082  argc = INT2FIX(1);
3083  nsplat++;
3084  *flag |= VM_CALL_ARGS_SPLAT;
3085  break;
3086  }
3087  case NODE_ARGSCAT:
3088  case NODE_ARGSPUSH: {
3089  int next_is_array = (nd_type(argn->nd_head) == NODE_ARRAY);
3090  DECL_ANCHOR(tmp);
3091 
3092  INIT_ANCHOR(tmp);
3093  COMPILE(tmp, "args (cat: splat)", argn->nd_body);
3094  if (next_is_array && nsplat == 0) {
3095  /* none */
3096  }
3097  else {
3098  if (nd_type(argn) == NODE_ARGSCAT) {
3099  ADD_INSN1(tmp, nd_line(argn), splatarray, Qfalse);
3100  }
3101  else {
3102  ADD_INSN1(tmp, nd_line(argn), newarray, INT2FIX(1));
3103  }
3104  }
3105  INSERT_LIST(args_splat, tmp);
3106  nsplat++;
3107  *flag |= VM_CALL_ARGS_SPLAT;
3108 
3109  if (next_is_array) {
3110  argc = INT2FIX(compile_array(iseq, args, argn->nd_head, COMPILE_ARRAY_TYPE_ARGS) + 1);
3111  }
3112  else {
3113  argn = argn->nd_head;
3114  goto setup_argn;
3115  }
3116  break;
3117  }
3118  case NODE_ARRAY: {
3119  argc = INT2FIX(compile_array(iseq, args, argn, COMPILE_ARRAY_TYPE_ARGS));
3120  break;
3121  }
3122  default: {
3123  rb_bug("setup_arg: unknown node: %s\n", ruby_node_name(nd_type(argn)));
3124  }
3125  }
3126  }
3127 
3128  if (nsplat > 1) {
3129  int i;
3130  for (i=1; i<nsplat; i++) {
3131  ADD_INSN(args_splat, nd_line(args), concatarray);
3132  }
3133  }
3134 
3135  if (!LIST_SIZE_ZERO(args_splat)) {
3136  ADD_SEQ(args, args_splat);
3137  }
3138 
3139  if (*flag & VM_CALL_ARGS_BLOCKARG) {
3140  ADD_SEQ(args, arg_block);
3141  }
3142  return argc;
3143 }
3144 
3145 
3153 static int
3154 iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
3155 {
3156  enum node_type type;
3157  int line;
3158 
3159  if (node == 0) {
3160  if (!poped) {
3161  debugs("node: NODE_NIL(implicit)\n");
3162  ADD_INSN(ret, iseq->compile_data->last_line, putnil);
3163  }
3164  return COMPILE_OK;
3165  }
3166 
3167  iseq->compile_data->last_line = line = (int)nd_line(node);
3168  debug_node_start(node);
3169 
3170  type = nd_type(node);
3171 
3172  if (node->flags & NODE_FL_NEWLINE) {
3173  ADD_TRACE(ret, line, RUBY_EVENT_LINE);
3174  }
3175 
3176  switch (type) {
3177  case NODE_BLOCK:{
3178  while (node && nd_type(node) == NODE_BLOCK) {
3179  COMPILE_(ret, "BLOCK body", node->nd_head,
3180  (node->nd_next == 0 && poped == 0) ? 0 : 1);
3181  node = node->nd_next;
3182  }
3183  if (node) {
3184  COMPILE_(ret, "BLOCK next", node->nd_next, poped);
3185  }
3186  break;
3187  }
3188  case NODE_IF:{
3189  DECL_ANCHOR(cond_seq);
3190  DECL_ANCHOR(then_seq);
3191  DECL_ANCHOR(else_seq);
3192  LABEL *then_label, *else_label, *end_label;
3193 
3194  INIT_ANCHOR(cond_seq);
3195  INIT_ANCHOR(then_seq);
3196  INIT_ANCHOR(else_seq);
3197  then_label = NEW_LABEL(line);
3198  else_label = NEW_LABEL(line);
3199  end_label = NEW_LABEL(line);
3200 
3201  compile_branch_condition(iseq, cond_seq, node->nd_cond,
3202  then_label, else_label);
3203  COMPILE_(then_seq, "then", node->nd_body, poped);
3204  COMPILE_(else_seq, "else", node->nd_else, poped);
3205 
3206  ADD_SEQ(ret, cond_seq);
3207 
3208  ADD_LABEL(ret, then_label);
3209  ADD_SEQ(ret, then_seq);
3210  ADD_INSNL(ret, line, jump, end_label);
3211 
3212  ADD_LABEL(ret, else_label);
3213  ADD_SEQ(ret, else_seq);
3214 
3215  ADD_LABEL(ret, end_label);
3216 
3217  break;
3218  }
3219  case NODE_CASE:{
3220  NODE *vals;
3221  NODE *tempnode = node;
3222  LABEL *endlabel, *elselabel;
3223  DECL_ANCHOR(head);
3224  DECL_ANCHOR(body_seq);
3225  DECL_ANCHOR(cond_seq);
3226  int only_special_literals = 1;
3227  VALUE literals = rb_hash_new();
3228 
3229  INIT_ANCHOR(head);
3230  INIT_ANCHOR(body_seq);
3231  INIT_ANCHOR(cond_seq);
3232 
3233  RHASH_TBL(literals)->type = &cdhash_type;
3234 
3235  if (node->nd_head == 0) {
3236  COMPILE_(ret, "when", node->nd_body, poped);
3237  break;
3238  }
3239  COMPILE(head, "case base", node->nd_head);
3240 
3241  node = node->nd_body;
3242  type = nd_type(node);
3243  line = nd_line(node);
3244 
3245  if (type != NODE_WHEN) {
3246  COMPILE_ERROR((ERROR_ARGS "NODE_CASE: unexpected node. must be NODE_WHEN, but %s", ruby_node_name(type)));
3247  }
3248 
3249  endlabel = NEW_LABEL(line);
3250  elselabel = NEW_LABEL(line);
3251 
3252  ADD_SEQ(ret, head); /* case VAL */
3253 
3254  while (type == NODE_WHEN) {
3255  LABEL *l1;
3256 
3257  l1 = NEW_LABEL(line);
3258  ADD_LABEL(body_seq, l1);
3259  ADD_INSN(body_seq, line, pop);
3260  COMPILE_(body_seq, "when body", node->nd_body, poped);
3261  ADD_INSNL(body_seq, line, jump, endlabel);
3262 
3263  vals = node->nd_head;
3264  if (vals) {
3265  switch (nd_type(vals)) {
3266  case NODE_ARRAY:
3267  only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
3268  break;
3269  case NODE_SPLAT:
3270  case NODE_ARGSCAT:
3271  case NODE_ARGSPUSH:
3272  only_special_literals = 0;
3273  ADD_INSN (cond_seq, nd_line(vals), dup);
3274  COMPILE(cond_seq, "when/cond splat", vals);
3275  ADD_INSN1(cond_seq, nd_line(vals), checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
3276  ADD_INSNL(cond_seq, nd_line(vals), branchif, l1);
3277  break;
3278  default:
3279  rb_bug("NODE_CASE: unknown node (%s)",
3280  ruby_node_name(nd_type(vals)));
3281  }
3282  }
3283  else {
3284  rb_bug("NODE_CASE: must be NODE_ARRAY, but 0");
3285  }
3286 
3287  node = node->nd_next;
3288  if (!node) {
3289  break;
3290  }
3291  type = nd_type(node);
3292  line = nd_line(node);
3293  }
3294  /* else */
3295  if (node) {
3296  ADD_LABEL(cond_seq, elselabel);
3297  ADD_INSN(cond_seq, line, pop);
3298  COMPILE_(cond_seq, "else", node, poped);
3299  ADD_INSNL(cond_seq, line, jump, endlabel);
3300  }
3301  else {
3302  debugs("== else (implicit)\n");
3303  ADD_LABEL(cond_seq, elselabel);
3304  ADD_INSN(cond_seq, nd_line(tempnode), pop);
3305  if (!poped) {
3306  ADD_INSN(cond_seq, nd_line(tempnode), putnil);
3307  }
3308  ADD_INSNL(cond_seq, nd_line(tempnode), jump, endlabel);
3309  }
3310 
3311  if (only_special_literals) {
3312  iseq_add_mark_object(iseq, literals);
3313 
3314  ADD_INSN(ret, nd_line(tempnode), dup);
3315  ADD_INSN2(ret, nd_line(tempnode), opt_case_dispatch, literals, elselabel);
3316  }
3317 
3318  ADD_SEQ(ret, cond_seq);
3319  ADD_SEQ(ret, body_seq);
3320  ADD_LABEL(ret, endlabel);
3321  break;
3322  }
3323  case NODE_WHEN:{
3324  NODE *vals;
3325  NODE *val;
3326  NODE *orig_node = node;
3327  LABEL *endlabel;
3328  DECL_ANCHOR(body_seq);
3329 
3330  INIT_ANCHOR(body_seq);
3331  endlabel = NEW_LABEL(line);
3332 
3333  while (node && nd_type(node) == NODE_WHEN) {
3334  LABEL *l1 = NEW_LABEL(line = nd_line(node));
3335  ADD_LABEL(body_seq, l1);
3336  COMPILE_(body_seq, "when", node->nd_body, poped);
3337  ADD_INSNL(body_seq, line, jump, endlabel);
3338 
3339  vals = node->nd_head;
3340  if (!vals) {
3341  rb_bug("NODE_WHEN: must be NODE_ARRAY, but 0");
3342  }
3343  switch (nd_type(vals)) {
3344  case NODE_ARRAY:
3345  while (vals) {
3346  val = vals->nd_head;
3347  COMPILE(ret, "when2", val);
3348  ADD_INSNL(ret, nd_line(val), branchif, l1);
3349  vals = vals->nd_next;
3350  }
3351  break;
3352  case NODE_SPLAT:
3353  case NODE_ARGSCAT:
3354  case NODE_ARGSPUSH:
3355  ADD_INSN(ret, nd_line(vals), putnil);
3356  COMPILE(ret, "when2/cond splat", vals);
3358  ADD_INSNL(ret, nd_line(vals), branchif, l1);
3359  break;
3360  default:
3361  rb_bug("NODE_WHEN: unknown node (%s)",
3362  ruby_node_name(nd_type(vals)));
3363  }
3364  node = node->nd_next;
3365  }
3366  /* else */
3367  COMPILE_(ret, "else", node, poped);
3368  ADD_INSNL(ret, nd_line(orig_node), jump, endlabel);
3369 
3370  ADD_SEQ(ret, body_seq);
3371  ADD_LABEL(ret, endlabel);
3372 
3373  break;
3374  }
3375  case NODE_OPT_N:
3376  case NODE_WHILE:
3377  case NODE_UNTIL:{
3378  LABEL *prev_start_label = iseq->compile_data->start_label;
3379  LABEL *prev_end_label = iseq->compile_data->end_label;
3380  LABEL *prev_redo_label = iseq->compile_data->redo_label;
3381  int prev_loopval_popped = iseq->compile_data->loopval_popped;
3382 
3384 
3385  LABEL *next_label = iseq->compile_data->start_label = NEW_LABEL(line); /* next */
3386  LABEL *redo_label = iseq->compile_data->redo_label = NEW_LABEL(line); /* redo */
3387  LABEL *break_label = iseq->compile_data->end_label = NEW_LABEL(line); /* break */
3388  LABEL *end_label = NEW_LABEL(line);
3389 
3390  LABEL *next_catch_label = NEW_LABEL(line);
3391  LABEL *tmp_label = NULL;
3392 
3393  iseq->compile_data->loopval_popped = 0;
3394  push_ensure_entry(iseq, &enl, 0, 0);
3395 
3396  if (type == NODE_OPT_N || node->nd_state == 1) {
3397  ADD_INSNL(ret, line, jump, next_label);
3398  }
3399  else {
3400  tmp_label = NEW_LABEL(line);
3401  ADD_INSNL(ret, line, jump, tmp_label);
3402  }
3403  ADD_INSN(ret, line, putnil);
3404  ADD_LABEL(ret, next_catch_label);
3405  ADD_INSN(ret, line, pop);
3406  ADD_INSNL(ret, line, jump, next_label);
3407  if (tmp_label) ADD_LABEL(ret, tmp_label);
3408 
3409  ADD_LABEL(ret, redo_label);
3410  COMPILE_POPED(ret, "while body", node->nd_body);
3411  ADD_LABEL(ret, next_label); /* next */
3412 
3413  if (type == NODE_WHILE) {
3414  compile_branch_condition(iseq, ret, node->nd_cond,
3415  redo_label, end_label);
3416  }
3417  else if (type == NODE_UNTIL) {
3418  /* untile */
3419  compile_branch_condition(iseq, ret, node->nd_cond,
3420  end_label, redo_label);
3421  }
3422  else {
3423  ADD_CALL_RECEIVER(ret, line);
3424  ADD_CALL(ret, line, ID2SYM(idGets), INT2FIX(0));
3425  ADD_INSNL(ret, line, branchif, redo_label);
3426  /* opt_n */
3427  }
3428 
3429  ADD_LABEL(ret, end_label);
3430 
3431  if (node->nd_state == Qundef) {
3432  /* ADD_INSN(ret, line, putundef); */
3433  rb_bug("unsupported: putundef");
3434  }
3435  else {
3436  ADD_INSN(ret, line, putnil);
3437  }
3438 
3439  ADD_LABEL(ret, break_label); /* break */
3440 
3441  if (poped) {
3442  ADD_INSN(ret, line, pop);
3443  }
3444 
3445  ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label,
3446  0, break_label);
3447  ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, 0,
3448  next_catch_label);
3449  ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, 0,
3450  iseq->compile_data->redo_label);
3451 
3452  iseq->compile_data->start_label = prev_start_label;
3453  iseq->compile_data->end_label = prev_end_label;
3454  iseq->compile_data->redo_label = prev_redo_label;
3455  iseq->compile_data->loopval_popped = prev_loopval_popped;
3457  break;
3458  }
3459  case NODE_ITER:
3460  case NODE_FOR:{
3461  VALUE prevblock = iseq->compile_data->current_block;
3462  LABEL *retry_label = NEW_LABEL(line);
3463  LABEL *retry_end_l = NEW_LABEL(line);
3464 
3465  ADD_LABEL(ret, retry_label);
3466  if (nd_type(node) == NODE_FOR) {
3467  COMPILE(ret, "iter caller (for)", node->nd_iter);
3468 
3469  iseq->compile_data->current_block =
3470  NEW_CHILD_ISEQVAL(node->nd_body, make_name_for_block(iseq),
3471  ISEQ_TYPE_BLOCK, line);
3472 
3473  ADD_SEND_R(ret, line, ID2SYM(idEach), INT2FIX(0),
3474  iseq->compile_data->current_block, INT2FIX(0));
3475  }
3476  else {
3477  iseq->compile_data->current_block =
3478  NEW_CHILD_ISEQVAL(node->nd_body, make_name_for_block(iseq),
3479  ISEQ_TYPE_BLOCK, line);
3480  COMPILE(ret, "iter caller", node->nd_iter);
3481  }
3482  ADD_LABEL(ret, retry_end_l);
3483 
3484  if (poped) {
3485  ADD_INSN(ret, line, pop);
3486  }
3487 
3488  iseq->compile_data->current_block = prevblock;
3489 
3490  ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, 0, retry_end_l);
3491 
3492  break;
3493  }
3494  case NODE_BREAK:{
3495  unsigned long level = 0;
3496 
3497  if (iseq->compile_data->redo_label != 0) {
3498  /* while/until */
3499  LABEL *splabel = NEW_LABEL(0);
3500  ADD_LABEL(ret, splabel);
3501  ADD_ADJUST(ret, line, iseq->compile_data->redo_label);
3502  COMPILE_(ret, "break val (while/until)", node->nd_stts, iseq->compile_data->loopval_popped);
3503  add_ensure_iseq(ret, iseq, 0);
3504  ADD_INSNL(ret, line, jump, iseq->compile_data->end_label);
3505  ADD_ADJUST_RESTORE(ret, splabel);
3506 
3507  if (!poped) {
3508  ADD_INSN(ret, line, putnil);
3509  }
3510  }
3511  else if (iseq->type == ISEQ_TYPE_BLOCK) {
3512  break_by_insn:
3513  /* escape from block */
3514  COMPILE(ret, "break val (block)", node->nd_stts);
3515  ADD_INSN1(ret, line, throw, INT2FIX(level | 0x02) /* TAG_BREAK */ );
3516  if (poped) {
3517  ADD_INSN(ret, line, pop);
3518  }
3519  }
3520  else if (iseq->type == ISEQ_TYPE_EVAL) {
3521  break_in_eval:
3522  COMPILE_ERROR((ERROR_ARGS "Can't escape from eval with break"));
3523  }
3524  else {
3525  rb_iseq_t *ip = iseq->parent_iseq;
3526  while (ip) {
3527  if (!ip->compile_data) {
3528  ip = 0;
3529  break;
3530  }
3531 
3532  level++;
3533  if (ip->compile_data->redo_label != 0) {
3534  level = 0x8000;
3535  if (ip->compile_data->loopval_popped == 0) {
3536  /* need value */
3537  level |= 0x4000;
3538  }
3539  goto break_by_insn;
3540  }
3541  else if (ip->type == ISEQ_TYPE_BLOCK) {
3542  level <<= 16;
3543  goto break_by_insn;
3544  }
3545  else if (ip->type == ISEQ_TYPE_EVAL) {
3546  goto break_in_eval;
3547  }
3548 
3549  ip = ip->parent_iseq;
3550  }
3551  COMPILE_ERROR((ERROR_ARGS "Invalid break"));
3552  }
3553  break;
3554  }
3555  case NODE_NEXT:{
3556  unsigned long level = 0;
3557 
3558  if (iseq->compile_data->redo_label != 0) {
3559  LABEL *splabel = NEW_LABEL(0);
3560  debugs("next in while loop\n");
3561  ADD_LABEL(ret, splabel);
3562  COMPILE(ret, "next val/valid syntax?", node->nd_stts);
3563  add_ensure_iseq(ret, iseq, 0);
3564  ADD_ADJUST(ret, line, iseq->compile_data->redo_label);
3565  ADD_INSNL(ret, line, jump, iseq->compile_data->start_label);
3566  ADD_ADJUST_RESTORE(ret, splabel);
3567  if (!poped) {
3568  ADD_INSN(ret, line, putnil);
3569  }
3570  }
3571  else if (iseq->compile_data->end_label) {
3572  LABEL *splabel = NEW_LABEL(0);
3573  debugs("next in block\n");
3574  ADD_LABEL(ret, splabel);
3575  ADD_ADJUST(ret, line, iseq->compile_data->start_label);
3576  COMPILE(ret, "next val", node->nd_stts);
3577  add_ensure_iseq(ret, iseq, 0);
3578  ADD_INSNL(ret, line, jump, iseq->compile_data->end_label);
3579  ADD_ADJUST_RESTORE(ret, splabel);
3580 
3581  if (!poped) {
3582  ADD_INSN(ret, line, putnil);
3583  }
3584  }
3585  else if (iseq->type == ISEQ_TYPE_EVAL) {
3586  next_in_eval:
3587  COMPILE_ERROR((ERROR_ARGS "Can't escape from eval with next"));
3588  }
3589  else {
3590  rb_iseq_t *ip;
3591  ip = iseq;
3592  while (ip) {
3593  if (!ip->compile_data) {
3594  ip = 0;
3595  break;
3596  }
3597 
3598  level = 0x8000 | 0x4000;
3599  if (ip->compile_data->redo_label != 0) {
3600  /* while loop */
3601  break;
3602  }
3603  else if (ip->type == ISEQ_TYPE_BLOCK) {
3604  break;
3605  }
3606  else if (ip->type == ISEQ_TYPE_EVAL) {
3607  goto next_in_eval;
3608  }
3609 
3610  ip = ip->parent_iseq;
3611  }
3612  if (ip != 0) {
3613  COMPILE(ret, "next val", node->nd_stts);
3614  ADD_INSN1(ret, line, throw, INT2FIX(level | 0x03) /* TAG_NEXT */ );
3615 
3616  if (poped) {
3617  ADD_INSN(ret, line, pop);
3618  }
3619  }
3620  else {
3621  COMPILE_ERROR((ERROR_ARGS "Invalid next"));
3622  }
3623  }
3624  break;
3625  }
3626  case NODE_REDO:{
3627  if (iseq->compile_data->redo_label) {
3628  LABEL *splabel = NEW_LABEL(0);
3629  debugs("redo in while");
3630  ADD_LABEL(ret, splabel);
3631  ADD_ADJUST(ret, line, iseq->compile_data->redo_label);
3632  add_ensure_iseq(ret, iseq, 0);
3633  ADD_INSNL(ret, line, jump, iseq->compile_data->redo_label);
3634  ADD_ADJUST_RESTORE(ret, splabel);
3635  if (!poped) {
3636  ADD_INSN(ret, line, putnil);
3637  }
3638  }
3639  else if (iseq->type == ISEQ_TYPE_EVAL) {
3640  redo_in_eval:
3641  COMPILE_ERROR((ERROR_ARGS "Can't escape from eval with redo"));
3642  }
3643  else if (iseq->compile_data->start_label) {
3644  LABEL *splabel = NEW_LABEL(0);
3645 
3646  debugs("redo in block");
3647  ADD_LABEL(ret, splabel);
3648  add_ensure_iseq(ret, iseq, 0);
3649  ADD_ADJUST(ret, line, iseq->compile_data->start_label);
3650  ADD_INSNL(ret, line, jump, iseq->compile_data->start_label);
3651  ADD_ADJUST_RESTORE(ret, splabel);
3652 
3653  if (!poped) {
3654  ADD_INSN(ret, line, putnil);
3655  }
3656  }
3657  else {
3658  rb_iseq_t *ip;
3659  unsigned long level;
3660  level = 0x8000 | 0x4000;
3661  ip = iseq;
3662  while (ip) {
3663  if (!ip->compile_data) {
3664  ip = 0;
3665  break;
3666  }
3667 
3668  if (ip->compile_data->redo_label != 0) {
3669  break;
3670  }
3671  else if (ip->type == ISEQ_TYPE_BLOCK) {
3672  break;
3673  }
3674  else if (ip->type == ISEQ_TYPE_EVAL) {
3675  goto redo_in_eval;
3676  }
3677 
3678  ip = ip->parent_iseq;
3679  }
3680  if (ip != 0) {
3681  ADD_INSN(ret, line, putnil);
3682  ADD_INSN1(ret, line, throw, INT2FIX(level | 0x05) /* TAG_REDO */ );
3683 
3684  if (poped) {
3685  ADD_INSN(ret, line, pop);
3686  }
3687  }
3688  else {
3689  COMPILE_ERROR((ERROR_ARGS "Invalid redo"));
3690  }
3691  }
3692  break;
3693  }
3694  case NODE_RETRY:{
3695  if (iseq->type == ISEQ_TYPE_RESCUE) {
3696  ADD_INSN(ret, line, putnil);
3697  ADD_INSN1(ret, line, throw, INT2FIX(0x04) /* TAG_RETRY */ );
3698 
3699  if (poped) {
3700  ADD_INSN(ret, line, pop);
3701  }
3702  }
3703  else {
3704  COMPILE_ERROR((ERROR_ARGS "Invalid retry"));
3705  }
3706  break;
3707  }
3708  case NODE_BEGIN:{
3709  COMPILE_(ret, "NODE_BEGIN", node->nd_body, poped);
3710  break;
3711  }
3712  case NODE_RESCUE:{
3713  LABEL *lstart = NEW_LABEL(line);
3714  LABEL *lend = NEW_LABEL(line);
3715  LABEL *lcont = NEW_LABEL(line);
3716  VALUE rescue = NEW_CHILD_ISEQVAL(
3717  node->nd_resq,
3718  rb_str_concat(rb_str_new2("rescue in "), iseq->location.label),
3719  ISEQ_TYPE_RESCUE, line);
3720 
3721  ADD_LABEL(ret, lstart);
3722  COMPILE(ret, "rescue head", node->nd_head);
3723  ADD_LABEL(ret, lend);
3724  if (node->nd_else) {
3725  ADD_INSN(ret, line, pop);
3726  COMPILE(ret, "rescue else", node->nd_else);
3727  }
3728  ADD_INSN(ret, line, nop);
3729  ADD_LABEL(ret, lcont);
3730 
3731  if (poped) {
3732  ADD_INSN(ret, line, pop);
3733  }
3734 
3735  /* register catch entry */
3736  ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
3737  ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, 0, lstart);
3738  break;
3739  }
3740  case NODE_RESBODY:{
3741  NODE *resq = node;
3742  NODE *narg;
3743  LABEL *label_miss, *label_hit;
3744 
3745  while (resq) {
3746  label_miss = NEW_LABEL(line);
3747  label_hit = NEW_LABEL(line);
3748 
3749  narg = resq->nd_args;
3750  if (narg) {
3751  switch (nd_type(narg)) {
3752  case NODE_ARRAY:
3753  while (narg) {
3754  ADD_INSN2(ret, line, getlocal, INT2FIX(2), INT2FIX(0));
3755  COMPILE(ret, "rescue arg", narg->nd_head);
3756  ADD_INSN1(ret, line, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
3757  ADD_INSNL(ret, line, branchif, label_hit);
3758  narg = narg->nd_next;
3759  }
3760  break;
3761  case NODE_SPLAT:
3762  case NODE_ARGSCAT:
3763  case NODE_ARGSPUSH:
3764  ADD_INSN2(ret, line, getlocal, INT2FIX(2), INT2FIX(0));
3765  COMPILE(ret, "rescue/cond splat", narg);
3767  ADD_INSNL(ret, line, branchif, label_hit);
3768  break;
3769  default:
3770  rb_bug("NODE_RESBODY: unknown node (%s)",
3771  ruby_node_name(nd_type(narg)));
3772  }
3773  }
3774  else {
3775  ADD_INSN2(ret, line, getlocal, INT2FIX(2), INT2FIX(0));
3776  ADD_INSN1(ret, line, putobject, rb_eStandardError);
3777  ADD_INSN1(ret, line, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
3778  ADD_INSNL(ret, line, branchif, label_hit);
3779  }
3780  ADD_INSNL(ret, line, jump, label_miss);
3781  ADD_LABEL(ret, label_hit);
3782  COMPILE(ret, "resbody body", resq->nd_body);
3784  ADD_INSN(ret, line, nop);
3785  }
3786  ADD_INSN(ret, line, leave);
3787  ADD_LABEL(ret, label_miss);
3788  resq = resq->nd_head;
3789  }
3790  break;
3791  }
3792  case NODE_ENSURE:{
3793  DECL_ANCHOR(ensr);
3794  VALUE ensure = NEW_CHILD_ISEQVAL(node->nd_ensr,
3796  ("ensure in "),
3797  iseq->location.label),
3798  ISEQ_TYPE_ENSURE, line);
3799  LABEL *lstart = NEW_LABEL(line);
3800  LABEL *lend = NEW_LABEL(line);
3801  LABEL *lcont = NEW_LABEL(line);
3802  struct ensure_range er;
3804  struct ensure_range *erange;
3805 
3806  INIT_ANCHOR(ensr);
3807  COMPILE_POPED(ensr, "ensure ensr", node->nd_ensr);
3808 
3809  er.begin = lstart;
3810  er.end = lend;
3811  er.next = 0;
3812  push_ensure_entry(iseq, &enl, &er, node->nd_ensr);
3813 
3814  ADD_LABEL(ret, lstart);
3815  COMPILE_(ret, "ensure head", node->nd_head, poped);
3816  ADD_LABEL(ret, lend);
3817  if (ensr->anchor.next == 0) {
3818  ADD_INSN(ret, line, nop);
3819  }
3820  else {
3821  ADD_SEQ(ret, ensr);
3822  }
3823  ADD_LABEL(ret, lcont);
3824 
3825  erange = iseq->compile_data->ensure_node_stack->erange;
3826  while (erange) {
3827  ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
3828  ensure, lcont);
3829  erange = erange->next;
3830  }
3831 
3832  iseq->compile_data->ensure_node_stack = enl.prev;
3833  break;
3834  }
3835 
3836  case NODE_AND:
3837  case NODE_OR:{
3838  LABEL *end_label = NEW_LABEL(line);
3839  COMPILE(ret, "nd_1st", node->nd_1st);
3840  if (!poped) {
3841  ADD_INSN(ret, line, dup);
3842  }
3843  if (type == NODE_AND) {
3844  ADD_INSNL(ret, line, branchunless, end_label);
3845  }
3846  else {
3847  ADD_INSNL(ret, line, branchif, end_label);
3848  }
3849  if (!poped) {
3850  ADD_INSN(ret, line, pop);
3851  }
3852  COMPILE_(ret, "nd_2nd", node->nd_2nd, poped);
3853  ADD_LABEL(ret, end_label);
3854  break;
3855  }
3856 
3857  case NODE_MASGN:{
3858  compile_massign(iseq, ret, node, poped);
3859  break;
3860  }
3861 
3862  case NODE_LASGN:{
3863  ID id = node->nd_vid;
3864  int idx = iseq->local_iseq->local_size - get_local_var_idx(iseq, id);
3865 
3866  debugs("lvar: %s idx: %d\n", rb_id2name(id), idx);
3867  COMPILE(ret, "rvalue", node->nd_value);
3868 
3869  if (!poped) {
3870  ADD_INSN(ret, line, dup);
3871  }
3872  ADD_INSN2(ret, line, setlocal, INT2FIX(idx), INT2FIX(get_lvar_level(iseq)));
3873 
3874  break;
3875  }
3876  case NODE_DASGN:
3877  case NODE_DASGN_CURR:{
3878  int idx, lv, ls;
3879  COMPILE(ret, "dvalue", node->nd_value);
3880  debugp_param("dassn id", rb_str_new2(rb_id2name(node->nd_vid) ? rb_id2name(node->nd_vid) : "*"));
3881 
3882  if (!poped) {
3883  ADD_INSN(ret, line, dup);
3884  }
3885 
3886  idx = get_dyna_var_idx(iseq, node->nd_vid, &lv, &ls);
3887 
3888  if (idx < 0) {
3889  rb_bug("NODE_DASGN(_CURR): unknown id (%s)", rb_id2name(node->nd_vid));
3890  }
3891 
3892  ADD_INSN2(ret, line, setlocal, INT2FIX(ls - idx), INT2FIX(lv));
3893  break;
3894  }
3895  case NODE_GASGN:{
3896  COMPILE(ret, "lvalue", node->nd_value);
3897 
3898  if (!poped) {
3899  ADD_INSN(ret, line, dup);
3900  }
3901  ADD_INSN1(ret, line, setglobal,
3902  ((VALUE)node->nd_entry | 1));
3903  break;
3904  }
3905  case NODE_IASGN:
3906  case NODE_IASGN2:{
3907  COMPILE(ret, "lvalue", node->nd_value);
3908  if (!poped) {
3909  ADD_INSN(ret, line, dup);
3910  }
3911  ADD_INSN2(ret, line, setinstancevariable,
3912  ID2SYM(node->nd_vid), INT2FIX(iseq->ic_size++));
3913  break;
3914  }
3915  case NODE_CDECL:{
3916  COMPILE(ret, "lvalue", node->nd_value);
3917 
3918  if (!poped) {
3919  ADD_INSN(ret, line, dup);
3920  }
3921 
3922  if (node->nd_vid) {
3923  ADD_INSN1(ret, line, putspecialobject,
3925  ADD_INSN1(ret, line, setconstant, ID2SYM(node->nd_vid));
3926  }
3927  else {
3928  compile_cpath(ret, iseq, node->nd_else);
3929  ADD_INSN1(ret, line, setconstant, ID2SYM(node->nd_else->nd_mid));
3930  }
3931  break;
3932  }
3933  case NODE_CVASGN:{
3934  COMPILE(ret, "cvasgn val", node->nd_value);
3935  if (!poped) {
3936  ADD_INSN(ret, line, dup);
3937  }
3938  ADD_INSN1(ret, line, setclassvariable,
3939  ID2SYM(node->nd_vid));
3940  break;
3941  }
3942  case NODE_OP_ASGN1: {
3943  DECL_ANCHOR(args);
3944  VALUE argc;
3945  VALUE flag = 0;
3946  ID id = node->nd_mid;
3947  int boff = 0;
3948 
3949  /*
3950  * a[x] (op)= y
3951  *
3952  * nil # nil
3953  * eval a # nil a
3954  * eval x # nil a x
3955  * dupn 2 # nil a x a x
3956  * send :[] # nil a x a[x]
3957  * eval y # nil a x a[x] y
3958  * send op # nil a x ret
3959  * setn 3 # ret a x ret
3960  * send []= # ret ?
3961  * pop # ret
3962  */
3963 
3964  /*
3965  * nd_recv[nd_args->nd_body] (nd_mid)= nd_args->nd_head;
3966  * NODE_OP_ASGN nd_recv
3967  * nd_args->nd_head
3968  * nd_args->nd_body
3969  * nd_mid
3970  */
3971 
3972  if (!poped) {
3973  ADD_INSN(ret, line, putnil);
3974  }
3975  COMPILE(ret, "NODE_OP_ASGN1 recv", node->nd_recv);
3976  switch (nd_type(node->nd_args->nd_head)) {
3977  case NODE_ZARRAY:
3978  argc = INT2FIX(0);
3979  break;
3980  case NODE_BLOCK_PASS:
3981  boff = 1;
3982  default:
3983  INIT_ANCHOR(args);
3984  argc = setup_args(iseq, args, node->nd_args->nd_head, &flag);
3985  ADD_SEQ(ret, args);
3986  }
3987  ADD_INSN1(ret, line, dupn, FIXNUM_INC(argc, 1 + boff));
3988  ADD_SEND_R(ret, line, ID2SYM(idAREF), argc, Qfalse, LONG2FIX(flag));
3989 
3990  if (id == 0 || id == 1) {
3991  /* 0: or, 1: and
3992  a[x] ||= y
3993 
3994  unless/if a[x]
3995  a[x]= y
3996  else
3997  nil
3998  end
3999  */
4000  LABEL *label = NEW_LABEL(line);
4001  LABEL *lfin = NEW_LABEL(line);
4002 
4003  ADD_INSN(ret, line, dup);
4004  if (id == 0) {
4005  /* or */
4006  ADD_INSNL(ret, line, branchif, label);
4007  }
4008  else {
4009  /* and */
4010  ADD_INSNL(ret, line, branchunless, label);
4011  }
4012  ADD_INSN(ret, line, pop);
4013 
4014  COMPILE(ret, "NODE_OP_ASGN1 args->body: ", node->nd_args->nd_body);
4015  if (!poped) {
4016  ADD_INSN1(ret, line, setn, FIXNUM_INC(argc, 2+boff));
4017  }
4018  if (flag & VM_CALL_ARGS_SPLAT) {
4019  ADD_INSN1(ret, line, newarray, INT2FIX(1));
4020  if (boff > 0) {
4021  ADD_INSN1(ret, line, dupn, INT2FIX(3));
4022  ADD_INSN(ret, line, swap);
4023  ADD_INSN(ret, line, pop);
4024  }
4025  ADD_INSN(ret, line, concatarray);
4026  if (boff > 0) {
4027  ADD_INSN1(ret, line, setn, INT2FIX(3));
4028  ADD_INSN(ret, line, pop);
4029  ADD_INSN(ret, line, pop);
4030  }
4031  ADD_SEND_R(ret, line, ID2SYM(idASET),
4032  argc, Qfalse, LONG2FIX(flag));
4033  }
4034  else {
4035  if (boff > 0)
4036  ADD_INSN(ret, line, swap);
4037  ADD_SEND_R(ret, line, ID2SYM(idASET),
4038  FIXNUM_INC(argc, 1), Qfalse, LONG2FIX(flag));
4039  }
4040  ADD_INSN(ret, line, pop);
4041  ADD_INSNL(ret, line, jump, lfin);
4042  ADD_LABEL(ret, label);
4043  if (!poped) {
4044  ADD_INSN1(ret, line, setn, FIXNUM_INC(argc, 2+boff));
4045  }
4046  ADD_INSN1(ret, line, adjuststack, FIXNUM_INC(argc, 2+boff));
4047  ADD_LABEL(ret, lfin);
4048  }
4049  else {
4050  COMPILE(ret, "NODE_OP_ASGN1 args->body: ", node->nd_args->nd_body);
4051  ADD_SEND(ret, line, ID2SYM(id), INT2FIX(1));
4052  if (!poped) {
4053  ADD_INSN1(ret, line, setn, FIXNUM_INC(argc, 2+boff));
4054  }
4055  if (flag & VM_CALL_ARGS_SPLAT) {
4056  ADD_INSN1(ret, line, newarray, INT2FIX(1));
4057  if (boff > 0) {
4058  ADD_INSN1(ret, line, dupn, INT2FIX(3));
4059  ADD_INSN(ret, line, swap);
4060  ADD_INSN(ret, line, pop);
4061  }
4062  ADD_INSN(ret, line, concatarray);
4063  if (boff > 0) {
4064  ADD_INSN1(ret, line, setn, INT2FIX(3));
4065  ADD_INSN(ret, line, pop);
4066  ADD_INSN(ret, line, pop);
4067  }
4068  ADD_SEND_R(ret, line, ID2SYM(idASET),
4069  argc, Qfalse, LONG2FIX(flag));
4070  }
4071  else {
4072  if (boff > 0)
4073  ADD_INSN(ret, line, swap);
4074  ADD_SEND_R(ret, line, ID2SYM(idASET),
4075  FIXNUM_INC(argc, 1), Qfalse, LONG2FIX(flag));
4076  }
4077  ADD_INSN(ret, line, pop);
4078  }
4079 
4080  break;
4081  }
4082  case NODE_OP_ASGN2:{
4083  ID atype = node->nd_next->nd_mid;
4084  LABEL *lfin = NEW_LABEL(line);
4085  LABEL *lcfin = NEW_LABEL(line);
4086  /*
4087  class C; attr_accessor :c; end
4088  r = C.new
4089  r.a &&= v # asgn2
4090 
4091  eval r # r
4092  dup # r r
4093  eval r.a # r o
4094 
4095  # or
4096  dup # r o o
4097  if lcfin # r o
4098  pop # r
4099  eval v # r v
4100  swap # v r
4101  topn 1 # v r v
4102  send a= # v ?
4103  jump lfin # v ?
4104 
4105  lcfin: # r o
4106  swap # o r
4107 
4108  lfin: # o ?
4109  pop # o
4110 
4111  # and
4112  dup # r o o
4113  unless lcfin
4114  pop # r
4115  eval v # r v
4116  swap # v r
4117  topn 1 # v r v
4118  send a= # v ?
4119  jump lfin # v ?
4120 
4121  # others
4122  eval v # r o v
4123  send ?? # r w
4124  send a= # w
4125 
4126  */
4127 
4128  COMPILE(ret, "NODE_OP_ASGN2#recv", node->nd_recv);
4129  ADD_INSN(ret, line, dup);
4130  ADD_SEND(ret, line, ID2SYM(node->nd_next->nd_vid),
4131  INT2FIX(0));
4132 
4133  if (atype == 0 || atype == 1) { /* 0: OR or 1: AND */
4134  ADD_INSN(ret, line, dup);
4135  if (atype == 0) {
4136  ADD_INSNL(ret, line, branchif, lcfin);
4137  }
4138  else {
4139  ADD_INSNL(ret, line, branchunless, lcfin);
4140  }
4141  ADD_INSN(ret, line, pop);
4142  COMPILE(ret, "NODE_OP_ASGN2 val", node->nd_value);
4143  ADD_INSN(ret, line, swap);
4144  ADD_INSN1(ret, line, topn, INT2FIX(1));
4145  ADD_SEND(ret, line, ID2SYM(node->nd_next->nd_aid),
4146  INT2FIX(1));
4147  ADD_INSNL(ret, line, jump, lfin);
4148 
4149  ADD_LABEL(ret, lcfin);
4150  ADD_INSN(ret, line, swap);
4151 
4152  ADD_LABEL(ret, lfin);
4153  ADD_INSN(ret, line, pop);
4154  if (poped) {
4155  /* we can apply more optimize */
4156  ADD_INSN(ret, line, pop);
4157  }
4158  }
4159  else {
4160  COMPILE(ret, "NODE_OP_ASGN2 val", node->nd_value);
4161  ADD_SEND(ret, line, ID2SYM(node->nd_next->nd_mid),
4162  INT2FIX(1));
4163  if (!poped) {
4164  ADD_INSN(ret, line, swap);
4165  ADD_INSN1(ret, line, topn, INT2FIX(1));
4166  }
4167  ADD_SEND(ret, line, ID2SYM(node->nd_next->nd_aid),
4168  INT2FIX(1));
4169  ADD_INSN(ret, line, pop);
4170  }
4171  break;
4172  }
4173  case NODE_OP_CDECL: {
4174  LABEL *lfin = 0;
4175  LABEL *lassign = 0;
4176  ID mid;
4177 
4178  switch (nd_type(node->nd_head)) {
4179  case NODE_COLON3:
4180  ADD_INSN1(ret, line, putobject, rb_cObject);
4181  break;
4182  case NODE_COLON2:
4183  COMPILE(ret, "NODE_OP_CDECL/colon2#nd_head", node->nd_head->nd_head);
4184  break;
4185  default:
4186  do {
4187  COMPILE_ERROR((ERROR_ARGS "%s: invalid node in NODE_OP_CDECL",
4188  ruby_node_name(nd_type(node->nd_head))));
4189  } while (0);
4190  return COMPILE_NG;
4191  }
4192  mid = node->nd_head->nd_mid;
4193  /* cref */
4194  if (node->nd_aid == 0) {
4195  lassign = NEW_LABEL(line);
4196  ADD_INSN(ret, line, dup); /* cref cref */
4197  ADD_INSN3(ret, line, defined, INT2FIX(DEFINED_CONST),
4198  ID2SYM(mid), Qfalse); /* cref bool */
4199  ADD_INSNL(ret, line, branchunless, lassign); /* cref */
4200  }
4201  ADD_INSN(ret, line, dup); /* cref cref */
4202  ADD_INSN1(ret, line, getconstant, ID2SYM(mid)); /* cref obj */
4203 
4204  if (node->nd_aid == 0 || node->nd_aid == 1) {
4205  lfin = NEW_LABEL(line);
4206  if (!poped) ADD_INSN(ret, line, dup); /* cref [obj] obj */
4207  if (node->nd_aid == 0)
4208  ADD_INSNL(ret, line, branchif, lfin);
4209  else
4210  ADD_INSNL(ret, line, branchunless, lfin);
4211  /* cref [obj] */
4212  if (!poped) ADD_INSN(ret, line, pop); /* cref */
4213  if (lassign) ADD_LABEL(ret, lassign);
4214  COMPILE(ret, "NODE_OP_CDECL#nd_value", node->nd_value);
4215  /* cref value */
4216  if (poped)
4217  ADD_INSN1(ret, line, topn, INT2FIX(1)); /* cref value cref */
4218  else {
4219  ADD_INSN1(ret, line, dupn, INT2FIX(2)); /* cref value cref value */
4220  ADD_INSN(ret, line, swap); /* cref value value cref */
4221  }
4222  ADD_INSN1(ret, line, setconstant, ID2SYM(mid)); /* cref [value] */
4223  ADD_LABEL(ret, lfin); /* cref [value] */
4224  if (!poped) ADD_INSN(ret, line, swap); /* [value] cref */
4225  ADD_INSN(ret, line, pop); /* [value] */
4226  }
4227  else {
4228  COMPILE(ret, "NODE_OP_CDECL#nd_value", node->nd_value);
4229  /* cref obj value */
4230  ADD_CALL(ret, line, ID2SYM(node->nd_aid), INT2FIX(1));
4231  /* cref value */
4232  ADD_INSN(ret, line, swap); /* value cref */
4233  if (!poped) {
4234  ADD_INSN1(ret, line, topn, INT2FIX(1)); /* value cref value */
4235  ADD_INSN(ret, line, swap); /* value value cref */
4236  }
4237  ADD_INSN1(ret, line, setconstant, ID2SYM(mid));
4238  }
4239  break;
4240  }
4241  case NODE_OP_ASGN_AND:
4242  case NODE_OP_ASGN_OR:{
4243  LABEL *lfin = NEW_LABEL(line);
4244  LABEL *lassign;
4245 
4246  if (nd_type(node) == NODE_OP_ASGN_OR) {
4247  LABEL *lfinish[2];
4248  lfinish[0] = lfin;
4249  lfinish[1] = 0;
4250  defined_expr(iseq, ret, node->nd_head, lfinish, Qfalse);
4251  lassign = lfinish[1];
4252  if (!lassign) {
4253  lassign = NEW_LABEL(line);
4254  }
4255  ADD_INSNL(ret, line, branchunless, lassign);
4256  }
4257  else {
4258  lassign = NEW_LABEL(line);
4259  }
4260 
4261  COMPILE(ret, "NODE_OP_ASGN_AND/OR#nd_head", node->nd_head);
4262  ADD_INSN(ret, line, dup);
4263 
4264  if (nd_type(node) == NODE_OP_ASGN_AND) {
4265  ADD_INSNL(ret, line, branchunless, lfin);
4266  }
4267  else {
4268  ADD_INSNL(ret, line, branchif, lfin);
4269  }
4270 
4271  ADD_INSN(ret, line, pop);
4272  ADD_LABEL(ret, lassign);
4273  COMPILE(ret, "NODE_OP_ASGN_AND/OR#nd_value", node->nd_value);
4274  ADD_LABEL(ret, lfin);
4275 
4276  if (poped) {
4277  /* we can apply more optimize */
4278  ADD_INSN(ret, line, pop);
4279  }
4280  break;
4281  }
4282  case NODE_CALL:
4283  case NODE_FCALL:
4284  case NODE_VCALL:{ /* VCALL: variable or call */
4285  /*
4286  call: obj.method(...)
4287  fcall: func(...)
4288  vcall: func
4289  */
4290  DECL_ANCHOR(recv);
4291  DECL_ANCHOR(args);
4292  ID mid = node->nd_mid;
4293  VALUE argc;
4294  VALUE flag = 0;
4295  VALUE parent_block = iseq->compile_data->current_block;
4297 
4298  INIT_ANCHOR(recv);
4299  INIT_ANCHOR(args);
4300 #if SUPPORT_JOKE
4301  if (nd_type(node) == NODE_VCALL) {
4302  ID id_bitblt;
4303  ID id_answer;
4304 
4305  CONST_ID(id_bitblt, "bitblt");
4306  CONST_ID(id_answer, "the_answer_to_life_the_universe_and_everything");
4307 
4308  if (mid == id_bitblt) {
4309  ADD_INSN(ret, line, bitblt);
4310  break;
4311  }
4312  else if (mid == id_answer) {
4313  ADD_INSN(ret, line, answer);
4314  break;
4315  }
4316  }
4317  /* only joke */
4318  {
4319  ID goto_id;
4320  ID label_id;
4321 
4322  CONST_ID(goto_id, "__goto__");
4323  CONST_ID(label_id, "__label__");
4324 
4325  if (nd_type(node) == NODE_FCALL &&
4326  (mid == goto_id || mid == label_id)) {
4327  LABEL *label;
4328  st_data_t data;
4329  st_table *labels_table = iseq->compile_data->labels_table;
4330  ID label_name;
4331 
4332  if (!labels_table) {
4333  labels_table = st_init_numtable();
4334  iseq->compile_data->labels_table = labels_table;
4335  }
4336  if (nd_type(node->nd_args->nd_head) == NODE_LIT &&
4337  SYMBOL_P(node->nd_args->nd_head->nd_lit)) {
4338 
4339  label_name = SYM2ID(node->nd_args->nd_head->nd_lit);
4340  if (!st_lookup(labels_table, (st_data_t)label_name, &data)) {
4341  label = NEW_LABEL(line);
4342  label->position = line;
4343  st_insert(labels_table, (st_data_t)label_name, (st_data_t)label);
4344  }
4345  else {
4346  label = (LABEL *)data;
4347  }
4348  }
4349  else {
4350  COMPILE_ERROR((ERROR_ARGS "invalid goto/label format"));
4351  }
4352 
4353 
4354  if (mid == goto_id) {
4355  ADD_INSNL(ret, line, jump, label);
4356  }
4357  else {
4358  ADD_LABEL(ret, label);
4359  }
4360  break;
4361  }
4362  }
4363 #endif
4364  /* receiver */
4365  if (type == NODE_CALL) {
4366  COMPILE(recv, "recv", node->nd_recv);
4367  }
4368  else if (type == NODE_FCALL || type == NODE_VCALL) {
4369  ADD_CALL_RECEIVER(recv, line);
4370  }
4371 
4372  /* args */
4373  if (nd_type(node) != NODE_VCALL) {
4374  argc = setup_args(iseq, args, node->nd_args, &flag);
4375  }
4376  else {
4377  argc = INT2FIX(0);
4378  }
4379 
4380  ADD_SEQ(ret, recv);
4381  ADD_SEQ(ret, args);
4382 
4383  debugp_param("call args argc", argc);
4384  debugp_param("call method", ID2SYM(mid));
4385 
4386  switch (nd_type(node)) {
4387  case NODE_VCALL:
4388  flag |= VM_CALL_VCALL;
4389  /* VCALL is funcall, so fall through */
4390  case NODE_FCALL:
4391  flag |= VM_CALL_FCALL;
4392  }
4393 
4394  ADD_SEND_R(ret, line, ID2SYM(mid),
4395  argc, parent_block, LONG2FIX(flag));
4396 
4397  if (poped) {
4398  ADD_INSN(ret, line, pop);
4399  }
4400  break;
4401  }
4402  case NODE_SUPER:
4403  case NODE_ZSUPER:{
4404  DECL_ANCHOR(args);
4405  int argc;
4406  VALUE flag = 0;
4407  VALUE parent_block = iseq->compile_data->current_block;
4408 
4409  INIT_ANCHOR(args);
4411  if (nd_type(node) == NODE_SUPER) {
4412  VALUE vargc = setup_args(iseq, args, node->nd_args, &flag);
4413  argc = FIX2INT(vargc);
4414  }
4415  else {
4416  /* NODE_ZSUPER */
4417  int i;
4418  rb_iseq_t *liseq = iseq->local_iseq;
4419  int lvar_level = get_lvar_level(iseq);
4420 
4421  argc = liseq->argc;
4422 
4423  /* normal arguments */
4424  for (i = 0; i < liseq->argc; i++) {
4425  int idx = liseq->local_size - i;
4426  ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level));
4427  }
4428 
4429  if (!liseq->arg_simple) {
4430  if (liseq->arg_opts) {
4431  /* optional arguments */
4432  int j;
4433  for (j = 0; j < liseq->arg_opts - 1; j++) {
4434  int idx = liseq->local_size - (i + j);
4435  ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level));
4436  }
4437  i += j;
4438  argc = i;
4439  }
4440 
4441  if (liseq->arg_rest != -1) {
4442  /* rest argument */
4443  int idx = liseq->local_size - liseq->arg_rest;
4444  ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level));
4445  argc = liseq->arg_rest + 1;
4446  flag |= VM_CALL_ARGS_SPLAT;
4447  }
4448 
4449  if (liseq->arg_post_len) {
4450  /* post arguments */
4451  int post_len = liseq->arg_post_len;
4452  int post_start = liseq->arg_post_start;
4453 
4454  if (liseq->arg_rest != -1) {
4455  int j;
4456  for (j=0; j<post_len; j++) {
4457  int idx = liseq->local_size - (post_start + j);
4458  ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level));
4459  }
4460  ADD_INSN1(args, line, newarray, INT2FIX(j));
4461  ADD_INSN (args, line, concatarray);
4462  /* argc is setteled at above */
4463  }
4464  else {
4465  int j;
4466  for (j=0; j<post_len; j++) {
4467  int idx = liseq->local_size - (post_start + j);
4468  ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level));
4469  }
4470  argc = post_len + post_start;
4471  }
4472  }
4473 
4474  if (liseq->arg_keyword > 0) {
4475  int local_size = liseq->local_size;
4476  int idx = local_size - liseq->arg_keyword;
4477  argc++;
4478  ADD_INSN1(args, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4479  ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level));
4480  ADD_SEND (args, line, ID2SYM(rb_intern("dup")), INT2FIX(0));
4481  for (i = 0; i < liseq->arg_keywords; ++i) {
4482  ID id = liseq->arg_keyword_table[i];
4483  idx = local_size - get_local_var_idx(liseq, id);
4484  ADD_INSN1(args, line, putobject, ID2SYM(id));
4485  ADD_INSN2(args, line, getlocal, INT2FIX(idx), INT2FIX(lvar_level));
4486  }
4487  ADD_SEND(args, line, ID2SYM(id_core_hash_merge_ptr), INT2FIX(i * 2 + 1));
4488  if (liseq->arg_rest != -1) {
4489  ADD_INSN1(args, line, newarray, INT2FIX(1));
4490  ADD_INSN (args, line, concatarray);
4491  --argc;
4492  }
4493  }
4494  }
4495  }
4496 
4497  /* dummy receiver */
4498  ADD_INSN1(ret, line, putobject, nd_type(node) == NODE_ZSUPER ? Qfalse : Qtrue);
4499  ADD_SEQ(ret, args);
4500  ADD_INSN1(ret, line, invokesuper, new_callinfo(iseq, 0, argc, parent_block,
4501  flag | VM_CALL_SUPER | VM_CALL_FCALL));
4502 
4503  if (poped) {
4504  ADD_INSN(ret, line, pop);
4505  }
4506  break;
4507  }
4508  case NODE_ARRAY:{
4509  compile_array_(iseq, ret, node, COMPILE_ARRAY_TYPE_ARRAY, poped);
4510  break;
4511  }
4512  case NODE_ZARRAY:{
4513  if (!poped) {
4514  ADD_INSN1(ret, line, newarray, INT2FIX(0));
4515  }
4516  break;
4517  }
4518  case NODE_VALUES:{
4519  NODE *n = node;
4520  while (n) {
4521  COMPILE(ret, "values item", n->nd_head);
4522  n = n->nd_next;
4523  }
4524  ADD_INSN1(ret, line, newarray, INT2FIX(node->nd_alen));
4525  if (poped) {
4526  ADD_INSN(ret, line, pop);
4527  }
4528  break;
4529  }
4530  case NODE_HASH:{
4531  DECL_ANCHOR(list);
4532  int type = node->nd_head ? nd_type(node->nd_head) : NODE_ZARRAY;
4533 
4534  INIT_ANCHOR(list);
4535  switch (type) {
4536  case NODE_ARRAY:
4537  compile_array(iseq, list, node->nd_head, COMPILE_ARRAY_TYPE_HASH);
4538  ADD_SEQ(ret, list);
4539  break;
4540 
4541  case NODE_ZARRAY:
4542  ADD_INSN1(ret, line, newhash, INT2FIX(0));
4543  break;
4544 
4545  default:
4546  rb_bug("can't make hash with this node: %s", ruby_node_name(type));
4547  }
4548 
4549  if (poped) {
4550  ADD_INSN(ret, line, pop);
4551  }
4552  break;
4553  }
4554  case NODE_RETURN:{
4555  rb_iseq_t *is = iseq;
4556 
4557  if (is) {
4558  if (is->type == ISEQ_TYPE_TOP) {
4559  COMPILE_ERROR((ERROR_ARGS "Invalid return"));
4560  }
4561  else {
4562  LABEL *splabel = 0;
4563 
4564  if (is->type == ISEQ_TYPE_METHOD) {
4565  splabel = NEW_LABEL(0);
4566  ADD_LABEL(ret, splabel);
4567  ADD_ADJUST(ret, line, 0);
4568  }
4569 
4570  COMPILE(ret, "return nd_stts (return val)", node->nd_stts);
4571 
4572  if (is->type == ISEQ_TYPE_METHOD) {
4573  add_ensure_iseq(ret, iseq, 1);
4574  ADD_TRACE(ret, line, RUBY_EVENT_RETURN);
4575  ADD_INSN(ret, line, leave);
4576  ADD_ADJUST_RESTORE(ret, splabel);
4577 
4578  if (!poped) {
4579  ADD_INSN(ret, line, putnil);
4580  }
4581  }
4582  else {
4583  ADD_INSN1(ret, line, throw, INT2FIX(0x01) /* TAG_RETURN */ );
4584  if (poped) {
4585  ADD_INSN(ret, line, pop);
4586  }
4587  }
4588  }
4589  }
4590  break;
4591  }
4592  case NODE_YIELD:{
4593  DECL_ANCHOR(args);
4594  VALUE argc;
4595  VALUE flag = 0;
4596 
4597  INIT_ANCHOR(args);
4598  if (iseq->type == ISEQ_TYPE_TOP) {
4599  COMPILE_ERROR((ERROR_ARGS "Invalid yield"));
4600  }
4601 
4602  if (node->nd_head) {
4603  argc = setup_args(iseq, args, node->nd_head, &flag);
4604  }
4605  else {
4606  argc = INT2FIX(0);
4607  }
4608 
4609  ADD_SEQ(ret, args);
4610  ADD_INSN1(ret, line, invokeblock, new_callinfo(iseq, 0, FIX2INT(argc), 0, flag));
4611 
4612  if (poped) {
4613  ADD_INSN(ret, line, pop);
4614  }
4615  break;
4616  }
4617  case NODE_LVAR:{
4618  if (!poped) {
4619  ID id = node->nd_vid;
4620  int idx = iseq->local_iseq->local_size - get_local_var_idx(iseq, id);
4621 
4622  debugs("id: %s idx: %d\n", rb_id2name(id), idx);
4623  ADD_INSN2(ret, line, getlocal, INT2FIX(idx), INT2FIX(get_lvar_level(iseq)));
4624  }
4625  break;
4626  }
4627  case NODE_DVAR:{
4628  int lv, idx, ls;
4629  debugi("nd_vid", node->nd_vid);
4630  if (!poped) {
4631  idx = get_dyna_var_idx(iseq, node->nd_vid, &lv, &ls);
4632  if (idx < 0) {
4633  rb_bug("unknown dvar (%s)", rb_id2name(node->nd_vid));
4634  }
4635  ADD_INSN2(ret, line, getlocal, INT2FIX(ls - idx), INT2FIX(lv));
4636  }
4637  break;
4638  }
4639  case NODE_GVAR:{
4640  ADD_INSN1(ret, line, getglobal,
4641  ((VALUE)node->nd_entry | 1));
4642  if (poped) {
4643  ADD_INSN(ret, line, pop);
4644  }
4645  break;
4646  }
4647  case NODE_IVAR:{
4648  debugi("nd_vid", node->nd_vid);
4649  if (!poped) {
4650  ADD_INSN2(ret, line, getinstancevariable,
4651  ID2SYM(node->nd_vid), INT2FIX(iseq->ic_size++));
4652  }
4653  break;
4654  }
4655  case NODE_CONST:{
4656  debugi("nd_vid", node->nd_vid);
4657 
4658  if (iseq->compile_data->option->inline_const_cache) {
4659  LABEL *lend = NEW_LABEL(line);
4660  int ic_index = iseq->ic_size++;
4661 
4662  ADD_INSN2(ret, line, getinlinecache, lend, INT2FIX(ic_index));
4663  ADD_INSN1(ret, line, getconstant, ID2SYM(node->nd_vid));
4664  ADD_INSN1(ret, line, setinlinecache, INT2FIX(ic_index));
4665  ADD_LABEL(ret, lend);
4666  }
4667  else {
4668  ADD_INSN(ret, line, putnil);
4669  ADD_INSN1(ret, line, getconstant, ID2SYM(node->nd_vid));
4670  }
4671 
4672  if (poped) {
4673  ADD_INSN(ret, line, pop);
4674  }
4675  break;
4676  }
4677  case NODE_CVAR:{
4678  if (!poped) {
4679  ADD_INSN1(ret, line, getclassvariable,
4680  ID2SYM(node->nd_vid));
4681  }
4682  break;
4683  }
4684  case NODE_NTH_REF:{
4685  if (!poped) {
4686  ADD_INSN2(ret, line, getspecial, INT2FIX(1) /* '~' */,
4687  INT2FIX(node->nd_nth << 1));
4688  }
4689  break;
4690  }
4691  case NODE_BACK_REF:{
4692  if (!poped) {
4693  ADD_INSN2(ret, line, getspecial, INT2FIX(1) /* '~' */,
4694  INT2FIX(0x01 | (node->nd_nth << 1)));
4695  }
4696  break;
4697  }
4698  case NODE_MATCH:
4699  case NODE_MATCH2:
4700  case NODE_MATCH3:{
4701  DECL_ANCHOR(recv);
4702  DECL_ANCHOR(val);
4703 
4704  INIT_ANCHOR(recv);
4705  INIT_ANCHOR(val);
4706  switch (nd_type(node)) {
4707  case NODE_MATCH:
4708  ADD_INSN1(recv, line, putobject, node->nd_lit);
4709  ADD_INSN2(val, line, getspecial, INT2FIX(0),
4710  INT2FIX(0));
4711  break;
4712  case NODE_MATCH2:
4713  COMPILE(recv, "receiver", node->nd_recv);
4714  COMPILE(val, "value", node->nd_value);
4715  break;
4716  case NODE_MATCH3:
4717  COMPILE(recv, "receiver", node->nd_value);
4718  COMPILE(val, "value", node->nd_recv);
4719  break;
4720  }
4721 
4723  /* TODO: detect by node */
4724  if (recv->last == recv->anchor.next &&
4725  INSN_OF(recv->last) == BIN(putobject) &&
4726  nd_type(node) == NODE_MATCH2) {
4727  ADD_SEQ(ret, val);
4728  ADD_INSN1(ret, line, opt_regexpmatch1,
4729  OPERAND_AT(recv->last, 0));
4730  }
4731  else {
4732  ADD_SEQ(ret, recv);
4733  ADD_SEQ(ret, val);
4734  ADD_INSN(ret, line, opt_regexpmatch2);
4735  }
4736  }
4737  else {
4738  ADD_SEQ(ret, recv);
4739  ADD_SEQ(ret, val);
4740  ADD_SEND(ret, line, ID2SYM(idEqTilde), INT2FIX(1));
4741  }
4742 
4743  if (poped) {
4744  ADD_INSN(ret, line, pop);
4745  }
4746  break;
4747  }
4748  case NODE_LIT:{
4749  debugp_param("lit", node->nd_lit);
4750  if (!poped) {
4751  ADD_INSN1(ret, line, putobject, node->nd_lit);
4752  }
4753  break;
4754  }
4755  case NODE_STR:{
4756  debugp_param("nd_lit", node->nd_lit);
4757  if (!poped) {
4758  OBJ_FREEZE(node->nd_lit);
4759  ADD_INSN1(ret, line, putstring, node->nd_lit);
4760  }
4761  break;
4762  }
4763  case NODE_DSTR:{
4764  compile_dstr(iseq, ret, node);
4765 
4766  if (poped) {
4767  ADD_INSN(ret, line, pop);
4768  }
4769  break;
4770  }
4771  case NODE_XSTR:{
4772  OBJ_FREEZE(node->nd_lit);
4773  ADD_CALL_RECEIVER(ret, line);
4774  ADD_INSN1(ret, line, putobject, node->nd_lit);
4775  ADD_CALL(ret, line, ID2SYM(idBackquote), INT2FIX(1));
4776 
4777  if (poped) {
4778  ADD_INSN(ret, line, pop);
4779  }
4780  break;
4781  }
4782  case NODE_DXSTR:{
4783  ADD_CALL_RECEIVER(ret, line);
4784  compile_dstr(iseq, ret, node);
4785  ADD_CALL(ret, line, ID2SYM(idBackquote), INT2FIX(1));
4786 
4787  if (poped) {
4788  ADD_INSN(ret, line, pop);
4789  }
4790  break;
4791  }
4792  case NODE_EVSTR:{
4793  COMPILE(ret, "nd_body", node->nd_body);
4794 
4795  if (poped) {
4796  ADD_INSN(ret, line, pop);
4797  }
4798  else {
4799  ADD_INSN(ret, line, tostring);
4800  }
4801  break;
4802  }
4803  case NODE_DREGX:{
4804  compile_dregx(iseq, ret, node);
4805 
4806  if (poped) {
4807  ADD_INSN(ret, line, pop);
4808  }
4809  break;
4810  }
4811  case NODE_DREGX_ONCE:{
4812  /* TODO: once? */
4813  LABEL *lend = NEW_LABEL(line);
4814  int ic_index = iseq->ic_size++;
4815 
4816  ADD_INSN2(ret, line, onceinlinecache, lend, INT2FIX(ic_index));
4817  ADD_INSN(ret, line, pop);
4818 
4819  compile_dregx(iseq, ret, node);
4820 
4821  ADD_INSN1(ret, line, setinlinecache, INT2FIX(ic_index));
4822  ADD_LABEL(ret, lend);
4823 
4824  if (poped) {
4825  ADD_INSN(ret, line, pop);
4826  }
4827  break;
4828  }
4829  case NODE_ARGSCAT:{
4830  if (poped) {
4831  COMPILE(ret, "argscat head", node->nd_head);
4832  ADD_INSN1(ret, line, splatarray, Qfalse);
4833  ADD_INSN(ret, line, pop);
4834  COMPILE(ret, "argscat body", node->nd_body);
4835  ADD_INSN1(ret, line, splatarray, Qfalse);
4836  ADD_INSN(ret, line, pop);
4837  }
4838  else {
4839  COMPILE(ret, "argscat head", node->nd_head);
4840  COMPILE(ret, "argscat body", node->nd_body);
4841  ADD_INSN(ret, line, concatarray);
4842  }
4843  break;
4844  }
4845  case NODE_ARGSPUSH:{
4846  if (poped) {
4847  COMPILE(ret, "arsgpush head", node->nd_head);
4848  ADD_INSN1(ret, line, splatarray, Qfalse);
4849  ADD_INSN(ret, line, pop);
4850  COMPILE_(ret, "argspush body", node->nd_body, poped);
4851  }
4852  else {
4853  COMPILE(ret, "arsgpush head", node->nd_head);
4854  COMPILE_(ret, "argspush body", node->nd_body, poped);
4855  ADD_INSN1(ret, line, newarray, INT2FIX(1));
4856  ADD_INSN(ret, line, concatarray);
4857  }
4858  break;
4859  }
4860  case NODE_SPLAT:{
4861  COMPILE(ret, "splat", node->nd_head);
4862  ADD_INSN1(ret, line, splatarray, Qtrue);
4863 
4864  if (poped) {
4865  ADD_INSN(ret, line, pop);
4866  }
4867  break;
4868  }
4869  case NODE_DEFN:{
4870  VALUE iseqval = NEW_ISEQVAL(node->nd_defn,
4871  rb_str_dup(rb_id2str(node->nd_mid)),
4872  ISEQ_TYPE_METHOD, line);
4873 
4874  debugp_param("defn/iseq", iseqval);
4875 
4876  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4877  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
4878  ADD_INSN1(ret, line, putobject, ID2SYM(node->nd_mid));
4879  ADD_INSN1(ret, line, putiseq, iseqval);
4880  ADD_SEND (ret, line, ID2SYM(id_core_define_method), INT2FIX(3));
4881 
4882  if (poped) {
4883  ADD_INSN(ret, line, pop);
4884  }
4885 
4886  debugp_param("defn", iseqval);
4887  break;
4888  }
4889  case NODE_DEFS:{
4890  VALUE iseqval = NEW_ISEQVAL(node->nd_defn,
4891  rb_str_dup(rb_id2str(node->nd_mid)),
4892  ISEQ_TYPE_METHOD, line);
4893 
4894  debugp_param("defs/iseq", iseqval);
4895 
4896  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4897  COMPILE(ret, "defs: recv", node->nd_recv);
4898  ADD_INSN1(ret, line, putobject, ID2SYM(node->nd_mid));
4899  ADD_INSN1(ret, line, putiseq, iseqval);
4901 
4902  if (poped) {
4903  ADD_INSN(ret, line, pop);
4904  }
4905  break;
4906  }
4907  case NODE_ALIAS:{
4908  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4909  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
4910  COMPILE(ret, "alias arg1", node->u1.node);
4911  COMPILE(ret, "alias arg2", node->u2.node);
4913 
4914  if (poped) {
4915  ADD_INSN(ret, line, pop);
4916  }
4917  break;
4918  }
4919  case NODE_VALIAS:{
4920  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4921  ADD_INSN1(ret, line, putobject, ID2SYM(node->u1.id));
4922  ADD_INSN1(ret, line, putobject, ID2SYM(node->u2.id));
4924 
4925  if (poped) {
4926  ADD_INSN(ret, line, pop);
4927  }
4928  break;
4929  }
4930  case NODE_UNDEF:{
4931  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4932  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
4933  COMPILE(ret, "undef arg", node->u2.node);
4934  ADD_SEND(ret, line, ID2SYM(id_core_undef_method), INT2FIX(2));
4935 
4936  if (poped) {
4937  ADD_INSN(ret, line, pop);
4938  }
4939  break;
4940  }
4941  case NODE_CLASS:{
4942  VALUE iseqval =
4944  node->nd_body,
4945  rb_sprintf("<class:%s>", rb_id2name(node->nd_cpath->nd_mid)),
4946  ISEQ_TYPE_CLASS, line);
4947  VALUE noscope = compile_cpath(ret, iseq, node->nd_cpath);
4948  int flags = VM_DEFINECLASS_TYPE_CLASS;
4949  if (!noscope) flags |= VM_DEFINECLASS_FLAG_SCOPED;
4950  if (node->nd_super) flags |= VM_DEFINECLASS_FLAG_HAS_SUPERCLASS;
4951  COMPILE(ret, "super", node->nd_super);
4952  ADD_INSN3(ret, line, defineclass,
4953  ID2SYM(node->nd_cpath->nd_mid), iseqval, INT2FIX(flags));
4954 
4955  if (poped) {
4956  ADD_INSN(ret, line, pop);
4957  }
4958  break;
4959  }
4960  case NODE_MODULE:{
4961  VALUE iseqval = NEW_CHILD_ISEQVAL(
4962  node->nd_body,
4963  rb_sprintf("<module:%s>", rb_id2name(node->nd_cpath->nd_mid)),
4964  ISEQ_TYPE_CLASS, line);
4965 
4966  VALUE noscope = compile_cpath(ret, iseq, node->nd_cpath);
4967  int flags = VM_DEFINECLASS_TYPE_MODULE;
4968  if (!noscope) flags |= VM_DEFINECLASS_FLAG_SCOPED;
4969  ADD_INSN (ret, line, putnil); /* dummy */
4970  ADD_INSN3(ret, line, defineclass,
4971  ID2SYM(node->nd_cpath->nd_mid), iseqval, INT2FIX(flags));
4972  if (poped) {
4973  ADD_INSN(ret, line, pop);
4974  }
4975  break;
4976  }
4977  case NODE_SCLASS:{
4978  ID singletonclass;
4979  VALUE iseqval =
4980  NEW_ISEQVAL(node->nd_body, rb_str_new2("singleton class"),
4981  ISEQ_TYPE_CLASS, line);
4982 
4983  COMPILE(ret, "sclass#recv", node->nd_recv);
4984  ADD_INSN (ret, line, putnil);
4985  CONST_ID(singletonclass, "singletonclass");
4986  ADD_INSN3(ret, line, defineclass,
4987  ID2SYM(singletonclass), iseqval,
4989 
4990  if (poped) {
4991  ADD_INSN(ret, line, pop);
4992  }
4993  break;
4994  }
4995  case NODE_COLON2:{
4996  if (rb_is_const_id(node->nd_mid)) {
4997  /* constant */
4998  LABEL *lend = NEW_LABEL(line);
4999  int ic_index = iseq->ic_size++;
5000 
5001  DECL_ANCHOR(pref);
5002  DECL_ANCHOR(body);
5003 
5004  INIT_ANCHOR(pref);
5005  INIT_ANCHOR(body);
5006  compile_colon2(iseq, node, pref, body);
5007  if (LIST_SIZE_ZERO(pref)) {
5008  if (iseq->compile_data->option->inline_const_cache) {
5009  ADD_INSN2(ret, line, getinlinecache, lend, INT2FIX(ic_index));
5010  }
5011  else {
5012  ADD_INSN(ret, line, putnil);
5013  }
5014 
5015  ADD_SEQ(ret, body);
5016 
5017  if (iseq->compile_data->option->inline_const_cache) {
5018  ADD_INSN1(ret, line, setinlinecache, INT2FIX(ic_index));
5019  ADD_LABEL(ret, lend);
5020  }
5021  }
5022  else {
5023  ADD_SEQ(ret, pref);
5024  ADD_SEQ(ret, body);
5025  }
5026  }
5027  else {
5028  /* function call */
5029  ADD_CALL_RECEIVER(ret, line);
5030  COMPILE(ret, "colon2#nd_head", node->nd_head);
5031  ADD_CALL(ret, line, ID2SYM(node->nd_mid),
5032  INT2FIX(1));
5033  }
5034  if (poped) {
5035  ADD_INSN(ret, line, pop);
5036  }
5037  break;
5038  }
5039  case NODE_COLON3:{
5040  LABEL *lend = NEW_LABEL(line);
5041  int ic_index = iseq->ic_size++;
5042 
5043  debugi("colon3#nd_mid", node->nd_mid);
5044 
5045  /* add cache insn */
5046  if (iseq->compile_data->option->inline_const_cache) {
5047  ADD_INSN2(ret, line, getinlinecache, lend, INT2FIX(ic_index));
5048  ADD_INSN(ret, line, pop);
5049  }
5050 
5051  ADD_INSN1(ret, line, putobject, rb_cObject);
5052  ADD_INSN1(ret, line, getconstant, ID2SYM(node->nd_mid));
5053 
5054  if (iseq->compile_data->option->inline_const_cache) {
5055  ADD_INSN1(ret, line, setinlinecache, INT2FIX(ic_index));
5056  ADD_LABEL(ret, lend);
5057  }
5058 
5059  if (poped) {
5060  ADD_INSN(ret, line, pop);
5061  }
5062  break;
5063  }
5064  case NODE_DOT2:
5065  case NODE_DOT3:{
5066  VALUE flag = type == NODE_DOT2 ? INT2FIX(0) : INT2FIX(1);
5067  COMPILE(ret, "min", (NODE *) node->nd_beg);
5068  COMPILE(ret, "max", (NODE *) node->nd_end);
5069  if (poped) {
5070  ADD_INSN(ret, line, pop);
5071  ADD_INSN(ret, line, pop);
5072  }
5073  else {
5074  ADD_INSN1(ret, line, newrange, flag);
5075  }
5076  break;
5077  }
5078  case NODE_FLIP2:
5079  case NODE_FLIP3:{
5080  LABEL *lend = NEW_LABEL(line);
5081  LABEL *lfin = NEW_LABEL(line);
5082  LABEL *ltrue = NEW_LABEL(line);
5083  rb_iseq_t *local_iseq = iseq->local_iseq;
5084  rb_num_t cnt;
5085  VALUE key;
5086 
5087  cnt = local_iseq->flip_cnt++ + DEFAULT_SPECIAL_VAR_COUNT;
5088  key = INT2FIX(cnt);
5089 
5090  ADD_INSN2(ret, line, getspecial, key, INT2FIX(0));
5091  ADD_INSNL(ret, line, branchif, lend);
5092 
5093  /* *flip == 0 */
5094  COMPILE(ret, "flip2 beg", node->nd_beg);
5095  ADD_INSN(ret, line, dup);
5096  ADD_INSNL(ret, line, branchunless, lfin);
5097  if (nd_type(node) == NODE_FLIP3) {
5098  ADD_INSN(ret, line, dup);
5099  ADD_INSN1(ret, line, setspecial, key);
5100  ADD_INSNL(ret, line, jump, lfin);
5101  }
5102  else {
5103  ADD_INSN1(ret, line, setspecial, key);
5104  }
5105 
5106  /* *flip == 1 */
5107  ADD_LABEL(ret, lend);
5108  COMPILE(ret, "flip2 end", node->nd_end);
5109  ADD_INSNL(ret, line, branchunless, ltrue);
5110  ADD_INSN1(ret, line, putobject, Qfalse);
5111  ADD_INSN1(ret, line, setspecial, key);
5112 
5113  ADD_LABEL(ret, ltrue);
5114  ADD_INSN1(ret, line, putobject, Qtrue);
5115 
5116  ADD_LABEL(ret, lfin);
5117  break;
5118  }
5119  case NODE_SELF:{
5120  if (!poped) {
5121  ADD_INSN(ret, line, putself);
5122  }
5123  break;
5124  }
5125  case NODE_NIL:{
5126  if (!poped) {
5127  ADD_INSN(ret, line, putnil);
5128  }
5129  break;
5130  }
5131  case NODE_TRUE:{
5132  if (!poped) {
5133  ADD_INSN1(ret, line, putobject, Qtrue);
5134  }
5135  break;
5136  }
5137  case NODE_FALSE:{
5138  if (!poped) {
5139  ADD_INSN1(ret, line, putobject, Qfalse);
5140  }
5141  break;
5142  }
5143  case NODE_ERRINFO:{
5144  if (!poped) {
5145  if (iseq->type == ISEQ_TYPE_RESCUE) {
5146  ADD_INSN2(ret, line, getlocal, INT2FIX(2), INT2FIX(0));
5147  }
5148  else {
5149  rb_iseq_t *ip = iseq;
5150  int level = 0;
5151  while (ip) {
5152  if (ip->type == ISEQ_TYPE_RESCUE) {
5153  break;
5154  }
5155  ip = ip->parent_iseq;
5156  level++;
5157  }
5158  if (ip) {
5159  ADD_INSN2(ret, line, getlocal, INT2FIX(2), INT2FIX(level));
5160  }
5161  else {
5162  ADD_INSN(ret, line, putnil);
5163  }
5164  }
5165  }
5166  break;
5167  }
5168  case NODE_DEFINED:{
5169  if (poped) break;
5170  if (!node->nd_head) {
5172  ADD_INSN1(ret, nd_line(node), putobject, str);
5173  }
5174  else {
5175  LABEL *lfinish[2];
5176  lfinish[0] = NEW_LABEL(line);
5177  lfinish[1] = 0;
5178  ADD_INSN(ret, line, putnil);
5179  defined_expr(iseq, ret, node->nd_head, lfinish, Qtrue);
5180  ADD_INSN(ret, line, swap);
5181  ADD_INSN(ret, line, pop);
5182  if (lfinish[1]) {
5183  ADD_LABEL(ret, lfinish[1]);
5184  }
5185  ADD_LABEL(ret, lfinish[0]);
5186  }
5187  break;
5188  }
5189  case NODE_POSTEXE:{
5190  LABEL *lend = NEW_LABEL(line);
5191  VALUE block = NEW_CHILD_ISEQVAL(node->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
5192  int ic_index = iseq->ic_size++;
5193 
5194  ADD_INSN2(ret, line, onceinlinecache, lend, INT2FIX(ic_index));
5195  ADD_INSN(ret, line, pop);
5196 
5197  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5198  ADD_INSN1(ret, line, putiseq, block);
5199  ADD_SEND (ret, line, ID2SYM(id_core_set_postexe), INT2FIX(1));
5200 
5201  ADD_INSN1(ret, line, setinlinecache, INT2FIX(ic_index));
5202  ADD_LABEL(ret, lend);
5203 
5204  if (poped) {
5205  ADD_INSN(ret, line, pop);
5206  }
5207  break;
5208  }
5209  case NODE_KW_ARG:{
5210  LABEL *default_label = NEW_LABEL(line);
5211  LABEL *end_label = NEW_LABEL(line);
5212  int idx, lv, ls;
5213  ID id = node->nd_body->nd_vid;
5214 
5215  ADD_INSN(ret, line, dup);
5216  ADD_INSN1(ret, line, putobject, ID2SYM(id));
5217  ADD_SEND(ret, line, ID2SYM(rb_intern("key?")), INT2FIX(1));
5218  ADD_INSNL(ret, line, branchunless, default_label);
5219  ADD_INSN(ret, line, dup);
5220  ADD_INSN1(ret, line, putobject, ID2SYM(id));
5221  ADD_SEND(ret, line, ID2SYM(rb_intern("delete")), INT2FIX(1));
5222  switch (nd_type(node->nd_body)) {
5223  case NODE_LASGN:
5224  idx = iseq->local_iseq->local_size - get_local_var_idx(iseq, id);
5225  ADD_INSN2(ret, line, setlocal, INT2FIX(idx), INT2FIX(get_lvar_level(iseq)));
5226  break;
5227  case NODE_DASGN:
5228  case NODE_DASGN_CURR:
5229  idx = get_dyna_var_idx(iseq, id, &lv, &ls);
5230  ADD_INSN2(ret, line, setlocal, INT2FIX(ls - idx), INT2FIX(lv));
5231  break;
5232  default:
5233  rb_bug("iseq_compile_each (NODE_KW_ARG): unknown node: %s", ruby_node_name(nd_type(node->nd_body)));
5234  }
5235  ADD_INSNL(ret, line, jump, end_label);
5236  ADD_LABEL(ret, default_label);
5237  COMPILE_POPED(ret, "keyword default argument", node->nd_body);
5238  ADD_LABEL(ret, end_label);
5239  break;
5240  }
5241  case NODE_DSYM:{
5242  compile_dstr(iseq, ret, node);
5243  if (!poped) {
5244  ADD_SEND(ret, line, ID2SYM(idIntern), INT2FIX(0));
5245  }
5246  else {
5247  ADD_INSN(ret, line, pop);
5248  }
5249  break;
5250  }
5251  case NODE_ATTRASGN:{
5252  DECL_ANCHOR(recv);
5253  DECL_ANCHOR(args);
5254  VALUE flag = 0;
5255  VALUE argc;
5256 
5257  INIT_ANCHOR(recv);
5258  INIT_ANCHOR(args);
5259  argc = setup_args(iseq, args, node->nd_args, &flag);
5260 
5261  if (node->nd_recv == (NODE *) 1) {
5262  flag |= VM_CALL_FCALL;
5263  ADD_INSN(recv, line, putself);
5264  }
5265  else {
5266  COMPILE(recv, "recv", node->nd_recv);
5267  }
5268 
5269  debugp_param("argc", argc);
5270  debugp_param("nd_mid", ID2SYM(node->nd_mid));
5271 
5272  if (!poped) {
5273  ADD_INSN(ret, line, putnil);
5274  ADD_SEQ(ret, recv);
5275  ADD_SEQ(ret, args);
5276 
5277  if (flag & VM_CALL_ARGS_BLOCKARG) {
5278  ADD_INSN1(ret, line, topn, INT2FIX(1));
5279  if (flag & VM_CALL_ARGS_SPLAT) {
5280  ADD_INSN1(ret, line, putobject, INT2FIX(-1));
5281  ADD_SEND(ret, line, ID2SYM(idAREF), INT2FIX(1));
5282  }
5283  ADD_INSN1(ret, line, setn, FIXNUM_INC(argc, 3));
5284  ADD_INSN (ret, line, pop);
5285  }
5286  else if (flag & VM_CALL_ARGS_SPLAT) {
5287  ADD_INSN(ret, line, dup);
5288  ADD_INSN1(ret, line, putobject, INT2FIX(-1));
5289  ADD_SEND(ret, line, ID2SYM(idAREF), INT2FIX(1));
5290  ADD_INSN1(ret, line, setn, FIXNUM_INC(argc, 2));
5291  ADD_INSN (ret, line, pop);
5292  }
5293  else {
5294  ADD_INSN1(ret, line, setn, FIXNUM_INC(argc, 1));
5295  }
5296  }
5297  else {
5298  ADD_SEQ(ret, recv);
5299  ADD_SEQ(ret, args);
5300  }
5301  ADD_SEND_R(ret, line, ID2SYM(node->nd_mid), argc, 0, LONG2FIX(flag));
5302  ADD_INSN(ret, line, pop);
5303 
5304  break;
5305  }
5306  case NODE_PRELUDE:{
5307  COMPILE_POPED(ret, "prelude", node->nd_head);
5308  COMPILE_(ret, "body", node->nd_body, poped);
5309  break;
5310  }
5311  case NODE_LAMBDA:{
5312  /* compile same as lambda{...} */
5313  VALUE block = NEW_CHILD_ISEQVAL(node->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
5314  VALUE argc = INT2FIX(0);
5315  ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5316  ADD_CALL_WITH_BLOCK(ret, line, ID2SYM(idLambda), argc, block);
5317 
5318  if (poped) {
5319  ADD_INSN(ret, line, pop);
5320  }
5321  break;
5322  }
5323  default:
5324  rb_bug("iseq_compile_each: unknown node: %s", ruby_node_name(type));
5325  return COMPILE_NG;
5326  }
5327 
5328  debug_node_end();
5329  return COMPILE_OK;
5330 }
5331 
5332 /***************************/
5333 /* instruction information */
5334 /***************************/
5335 
5336 static int
5338 {
5339  return insn_len(iobj->insn_id);
5340 }
5341 
5342 static int
5343 calc_sp_depth(int depth, INSN *insn)
5344 {
5345  return insn_stack_increase(depth, insn->insn_id, insn->operands);
5346 }
5347 
5348 static int
5350 {
5351  return insn_len(iobj->line_no);
5352 }
5353 
5354 static VALUE
5356 {
5357  VALUE str = rb_sprintf("%-16s", insn_name(iobj->insn_id));
5358 
5359  if (iobj->operands) {
5360  const char *types = insn_op_types(iobj->insn_id);
5361  int j;
5362 
5363  for (j = 0; types[j]; j++) {
5364  char type = types[j];
5365  printf("str: %"PRIxVALUE", type: %c\n", str, type);
5366 
5367  switch (type) {
5368  case TS_OFFSET: /* label(destination position) */
5369  {
5370  LABEL *lobj = (LABEL *)OPERAND_AT(iobj, j);
5371  rb_str_catf(str, "<L%03d>", lobj->label_no);
5372  break;
5373  }
5374  break;
5375  case TS_ISEQ: /* iseq */
5376  {
5377  rb_iseq_t *iseq = (rb_iseq_t *)OPERAND_AT(iobj, j);
5378  VALUE val = Qnil;
5379  if (0 && iseq) { /* TODO: invalidate now */
5380  val = iseq->self;
5381  }
5382  rb_str_concat(str, rb_inspect(val));
5383  }
5384  break;
5385  case TS_LINDEX:
5386  case TS_NUM: /* ulong */
5387  case TS_VALUE: /* VALUE */
5388  {
5389  VALUE v = OPERAND_AT(iobj, j);
5390  rb_str_concat(str, rb_inspect(v));
5391  break;
5392  }
5393  case TS_ID: /* ID */
5394  rb_str_concat(str, rb_inspect(OPERAND_AT(iobj, j)));
5395  break;
5396  case TS_GENTRY:
5397  {
5398  struct rb_global_entry *entry = (struct rb_global_entry *)
5399  (OPERAND_AT(iobj, j) & (~1));
5400  rb_str_cat2(str, rb_id2name(entry->id));
5401  break;
5402  }
5403  case TS_IC: /* inline cache */
5404  rb_str_catf(str, "<ic:%d>", FIX2INT(OPERAND_AT(iobj, j)));
5405  break;
5406  case TS_CALLINFO: /* call info */
5407  {
5408  rb_call_info_t *ci = (rb_call_info_t *)OPERAND_AT(iobj, j);
5409  rb_str_catf(str, "<callinfo:%s, %d>", ci->mid ? rb_id2name(ci->mid) : "", ci->orig_argc);
5410  break;
5411  }
5412  case TS_CDHASH: /* case/when condition cache */
5413  rb_str_cat2(str, "<ch>");
5414  break;
5415  default:{
5416  rb_raise(rb_eSyntaxError, "unknown operand type: %c", type);
5417  }
5418  }
5419  if (types[j + 1]) {
5420  rb_str_cat2(str, ", ");
5421  }
5422  }
5423  }
5424  return str;
5425 }
5426 
5427 static void
5429 {
5430  int pos = 0;
5431  INSN *iobj;
5432  LABEL *lobj;
5433  VALUE str;
5434 
5435  printf("-- raw disasm--------\n");
5436 
5437  while (link) {
5438  switch (link->type) {
5439  case ISEQ_ELEMENT_INSN:
5440  {
5441  iobj = (INSN *)link;
5442  str = insn_data_to_s_detail(iobj);
5443  printf("%04d %-65s(%4d)\n", pos, StringValueCStr(str), insn_data_line_no(iobj));
5444  pos += insn_data_length(iobj);
5445  break;
5446  }
5447  case ISEQ_ELEMENT_LABEL:
5448  {
5449  lobj = (LABEL *)link;
5450  printf("<L%03d>\n", lobj->label_no);
5451  break;
5452  }
5453  case ISEQ_ELEMENT_NONE:
5454  {
5455  printf("[none]\n");
5456  break;
5457  }
5458  case ISEQ_ELEMENT_ADJUST:
5459  {
5460  ADJUST *adjust = (ADJUST *)link;
5461  printf("adjust: [label: %d]\n", adjust->label->label_no);
5462  break;
5463  }
5464  default:
5465  /* ignore */
5466  rb_raise(rb_eSyntaxError, "dump_disasm_list error: %ld\n", FIX2LONG(link->type));
5467  }
5468  link = link->next;
5469  }
5470  printf("---------------------\n");
5471 }
5472 
5473 const char *
5475 {
5476  return insn_name_info[i];
5477 }
5478 
5479 VALUE
5481 {
5482  VALUE ary = rb_ary_new();
5483  int i;
5484  for (i = 0; i < numberof(insn_name_info); i++) {
5485  rb_ary_push(ary, rb_obj_freeze(rb_str_new2(insn_name_info[i])));
5486  }
5487  return rb_obj_freeze(ary);
5488 }
5489 
5490 static LABEL *
5491 register_label(rb_iseq_t *iseq, struct st_table *labels_table, VALUE obj)
5492 {
5493  LABEL *label = 0;
5494  st_data_t tmp;
5495  obj = rb_convert_type(obj, T_SYMBOL, "Symbol", "to_sym");
5496 
5497  if (st_lookup(labels_table, obj, &tmp) == 0) {
5498  label = NEW_LABEL(0);
5499  st_insert(labels_table, obj, (st_data_t)label);
5500  }
5501  else {
5502  label = (LABEL *)tmp;
5503  }
5504  return label;
5505 }
5506 
5507 static VALUE
5509 {
5510 #undef rb_intern
5511 #define rb_intern(str) rb_intern_const(str)
5513  static VALUE symRescue, symEnsure, symRetry;
5514  static VALUE symBreak, symRedo, symNext;
5515 
5516  if (symRescue == 0) {
5517  symRescue = ID2SYM(rb_intern("rescue"));
5518  symEnsure = ID2SYM(rb_intern("ensure"));
5519  symRetry = ID2SYM(rb_intern("retry"));
5520  symBreak = ID2SYM(rb_intern("break"));
5521  symRedo = ID2SYM(rb_intern("redo"));
5522  symNext = ID2SYM(rb_intern("next"));
5523  }
5524 
5525  if (sym == symRescue) return CATCH_TYPE_RESCUE;
5526  if (sym == symEnsure) return CATCH_TYPE_ENSURE;
5527  if (sym == symRetry) return CATCH_TYPE_RETRY;
5528  if (sym == symBreak) return CATCH_TYPE_BREAK;
5529  if (sym == symRedo) return CATCH_TYPE_REDO;
5530  if (sym == symNext) return CATCH_TYPE_NEXT;
5531  sym_inspect = rb_inspect(sym);
5532  rb_raise(rb_eSyntaxError, "invalid exception symbol: %s",
5533  StringValuePtr(sym_inspect));
5534  return 0;
5535 }
5536 
5537 static int
5539  VALUE exception)
5540 {
5541  int i;
5542 
5543  for (i=0; i<RARRAY_LEN(exception); i++) {
5544  VALUE v, type, *ptr, eiseqval;
5545  LABEL *lstart, *lend, *lcont;
5546  int sp;
5547 
5548  RB_GC_GUARD(v) = rb_convert_type(RARRAY_PTR(exception)[i], T_ARRAY,
5549  "Array", "to_ary");
5550  if (RARRAY_LEN(v) != 6) {
5551  rb_raise(rb_eSyntaxError, "wrong exception entry");
5552  }
5553  ptr = RARRAY_PTR(v);
5554  type = get_exception_sym2type(ptr[0]);
5555  if (ptr[1] == Qnil) {
5556  eiseqval = 0;
5557  }
5558  else {
5559  eiseqval = rb_iseq_load(ptr[1], iseq->self, Qnil);
5560  }
5561 
5562  lstart = register_label(iseq, labels_table, ptr[2]);
5563  lend = register_label(iseq, labels_table, ptr[3]);
5564  lcont = register_label(iseq, labels_table, ptr[4]);
5565  sp = NUM2INT(ptr[5]);
5566 
5567  (void)sp;
5568 
5569  ADD_CATCH_ENTRY(type, lstart, lend, eiseqval, lcont);
5570  }
5571  return COMPILE_OK;
5572 }
5573 
5574 static struct st_table *
5576 {
5577  struct st_table *table;
5578  int i;
5579  table = st_init_numtable();
5580 
5581  for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
5582  st_insert(table, ID2SYM(rb_intern(insn_name(i))), i);
5583  }
5584 
5585  return table;
5586 }
5587 
5588 static VALUE
5590 {
5591  VALUE iseqval;
5592  if (RB_TYPE_P(op, T_ARRAY)) {
5593  iseqval = rb_iseq_load(op, iseq->self, Qnil);
5594  }
5595  else if (CLASS_OF(op) == rb_cISeq) {
5596  iseqval = op;
5597  }
5598  else {
5599  rb_raise(rb_eSyntaxError, "ISEQ is required");
5600  }
5601  iseq_add_mark_object(iseq, iseqval);
5602  return iseqval;
5603 }
5604 
5605 static int
5607  VALUE body, struct st_table *labels_table)
5608 {
5609  /* TODO: body should be frozen */
5610  VALUE *ptr = RARRAY_PTR(body);
5611  long i, len = RARRAY_LEN(body);
5612  int j;
5613  int line_no = 0;
5614 
5615  /*
5616  * index -> LABEL *label
5617  */
5618  static struct st_table *insn_table;
5619 
5620  if (insn_table == 0) {
5621  insn_table = insn_make_insn_table();
5622  }
5623 
5624  for (i=0; i<len; i++) {
5625  VALUE obj = ptr[i];
5626 
5627  if (SYMBOL_P(obj)) {
5628  LABEL *label = register_label(iseq, labels_table, obj);
5629  ADD_LABEL(anchor, label);
5630  }
5631  else if (FIXNUM_P(obj)) {
5632  line_no = NUM2INT(obj);
5633  }
5634  else if (RB_TYPE_P(obj, T_ARRAY)) {
5635  VALUE *argv = 0;
5636  int argc = RARRAY_LENINT(obj) - 1;
5637  st_data_t insn_id;
5638  VALUE insn;
5639 
5640  insn = (argc < 0) ? Qnil : RARRAY_PTR(obj)[0];
5641  if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
5642  /* TODO: exception */
5643  RB_GC_GUARD(insn) = rb_inspect(insn);
5644  rb_compile_error(RSTRING_PTR(iseq->location.path), line_no,
5645  "unknown instruction: %s", RSTRING_PTR(insn));
5646  }
5647 
5648  if (argc != insn_len((VALUE)insn_id)-1) {
5649  rb_compile_error(RSTRING_PTR(iseq->location.path), line_no,
5650  "operand size mismatch");
5651  }
5652 
5653  if (argc > 0) {
5654  argv = compile_data_alloc(iseq, sizeof(VALUE) * argc);
5655  for (j=0; j<argc; j++) {
5656  VALUE op = rb_ary_entry(obj, j+1);
5657  switch (insn_op_type((VALUE)insn_id, j)) {
5658  case TS_OFFSET: {
5659  LABEL *label = register_label(iseq, labels_table, op);
5660  argv[j] = (VALUE)label;
5661  break;
5662  }
5663  case TS_LINDEX:
5664  case TS_NUM:
5665  (void)NUM2INT(op);
5666  argv[j] = op;
5667  break;
5668  case TS_VALUE:
5669  argv[j] = op;
5670  iseq_add_mark_object(iseq, op);
5671  break;
5672  case TS_ISEQ:
5673  {
5674  if (op != Qnil) {
5675  argv[j] = iseq_build_load_iseq(iseq, op);
5676  }
5677  else {
5678  argv[j] = 0;
5679  }
5680  }
5681  break;
5682  case TS_GENTRY:
5683  op = rb_convert_type(op, T_SYMBOL, "Symbol", "to_sym");
5684  argv[j] = (VALUE)rb_global_entry(SYM2ID(op));
5685  break;
5686  case TS_IC:
5687  argv[j] = op;
5688  if (NUM2INT(op) >= iseq->ic_size) {
5689  iseq->ic_size = NUM2INT(op) + 1;
5690  }
5691  break;
5692  case TS_CALLINFO:
5693  {
5694  ID mid = 0;
5695  int orig_argc = 0;
5696  VALUE block = 0;
5697  unsigned long flag = 0;
5698 
5699  if (!NIL_P(op)) {
5700  VALUE vmid = rb_hash_aref(op, ID2SYM(rb_intern("mid")));
5701  VALUE vflag = rb_hash_aref(op, ID2SYM(rb_intern("flag")));
5702  VALUE vorig_argc = rb_hash_aref(op, ID2SYM(rb_intern("orig_argc")));
5703  VALUE vblock = rb_hash_aref(op, ID2SYM(rb_intern("block")));
5704 
5705  if (!NIL_P(vmid)) mid = SYM2ID(vmid);
5706  if (!NIL_P(vflag)) flag = NUM2ULONG(vflag);
5707  if (!NIL_P(vorig_argc)) orig_argc = FIX2INT(vorig_argc);
5708  if (!NIL_P(vblock)) block = iseq_build_load_iseq(iseq, vblock);
5709  }
5710  argv[j] = (VALUE)new_callinfo(iseq, mid, orig_argc, block, flag);
5711  }
5712  break;
5713  case TS_ID:
5714  argv[j] = rb_convert_type(op, T_SYMBOL,
5715  "Symbol", "to_sym");
5716  break;
5717  case TS_CDHASH:
5718  {
5719  int i;
5720  op = rb_convert_type(op, T_ARRAY, "Array", "to_ary");
5721  op = rb_ary_dup(op);
5722  for (i=0; i<RARRAY_LEN(op); i+=2) {
5723  VALUE sym = rb_ary_entry(op, i+1);
5724  LABEL *label =
5725  register_label(iseq, labels_table, sym);
5726  rb_ary_store(op, i+1, (VALUE)label | 1);
5727  }
5728  argv[j] = op;
5730  }
5731  break;
5732  default:
5733  rb_raise(rb_eSyntaxError, "unknown operand: %c", insn_op_type((VALUE)insn_id, j));
5734  }
5735  }
5736  }
5737  ADD_ELEM(anchor,
5738  (LINK_ELEMENT*)new_insn_core(iseq, line_no,
5739  (enum ruby_vminsn_type)insn_id, argc, argv));
5740  }
5741  else {
5742  rb_raise(rb_eTypeError, "unexpected object for instruction");
5743  }
5744  }
5745  validate_labels(iseq, labels_table);
5746  st_free_table(labels_table);
5747  iseq_setup(iseq, anchor);
5748  return COMPILE_OK;
5749 }
5750 
5751 #define CHECK_ARRAY(v) rb_convert_type((v), T_ARRAY, "Array", "to_ary")
5752 #define CHECK_STRING(v) rb_convert_type((v), T_STRING, "String", "to_str")
5753 #define CHECK_SYMBOL(v) rb_convert_type((v), T_SYMBOL, "Symbol", "to_sym")
5754 static inline VALUE CHECK_INTEGER(VALUE v) {(void)NUM2LONG(v); return v;}
5755 
5756 VALUE
5758  VALUE exception, VALUE body)
5759 {
5760  int i;
5761  ID *tbl;
5762  struct st_table *labels_table = st_init_numtable();
5763  DECL_ANCHOR(anchor);
5764  INIT_ANCHOR(anchor);
5765 
5766  iseq->local_table_size = RARRAY_LENINT(locals);
5767  iseq->local_table = tbl = (ID *)ALLOC_N(ID, iseq->local_table_size);
5768  iseq->local_size = iseq->local_table_size + 1;
5769 
5770  for (i=0; i<RARRAY_LEN(locals); i++) {
5771  VALUE lv = RARRAY_PTR(locals)[i];
5772  tbl[i] = FIXNUM_P(lv) ? (ID)FIX2LONG(lv) : SYM2ID(CHECK_SYMBOL(lv));
5773  }
5774 
5775  /* args */
5776  if (FIXNUM_P(args)) {
5777  iseq->arg_size = iseq->argc = FIX2INT(args);
5778  iseq->arg_simple = 1;
5779  }
5780  else {
5781  int i = 0;
5782  VALUE argc = CHECK_INTEGER(rb_ary_entry(args, i++));
5783  VALUE arg_opt_labels = CHECK_ARRAY(rb_ary_entry(args, i++));
5784  VALUE arg_post_len = CHECK_INTEGER(rb_ary_entry(args, i++));
5785  VALUE arg_post_start = CHECK_INTEGER(rb_ary_entry(args, i++));
5786  VALUE arg_rest = CHECK_INTEGER(rb_ary_entry(args, i++));
5787  VALUE arg_block = CHECK_INTEGER(rb_ary_entry(args, i++));
5788  VALUE arg_simple = CHECK_INTEGER(rb_ary_entry(args, i++));
5789 
5790  iseq->argc = FIX2INT(argc);
5791  iseq->arg_rest = FIX2INT(arg_rest);
5792  iseq->arg_post_len = FIX2INT(arg_post_len);
5793  iseq->arg_post_start = FIX2INT(arg_post_start);
5794  iseq->arg_block = FIX2INT(arg_block);
5795  iseq->arg_opts = RARRAY_LENINT(arg_opt_labels);
5796  iseq->arg_opt_table = (VALUE *)ALLOC_N(VALUE, iseq->arg_opts);
5797 
5798  if (iseq->arg_block != -1) {
5799  iseq->arg_size = iseq->arg_block + 1;
5800  }
5801  else if (iseq->arg_post_len) {
5802  iseq->arg_size = iseq->arg_post_start + iseq->arg_post_len;
5803  }
5804  else if (iseq->arg_rest != -1) {
5805  iseq->arg_size = iseq->arg_rest + 1;
5806  }
5807  else {
5808  iseq->arg_size = iseq->argc + (iseq->arg_opts ? iseq->arg_opts - 1 : 0);
5809  }
5810 
5811  for (i=0; i<RARRAY_LEN(arg_opt_labels); i++) {
5812  iseq->arg_opt_table[i] =
5813  (VALUE)register_label(iseq, labels_table,
5814  rb_ary_entry(arg_opt_labels, i));
5815  }
5816 
5817  iseq->arg_simple = NUM2INT(arg_simple);
5818  }
5819 
5820  /* exception */
5821  iseq_build_from_ary_exception(iseq, labels_table, exception);
5822 
5823  /* body */
5824  iseq_build_from_ary_body(iseq, anchor, body, labels_table);
5825  return iseq->self;
5826 }
5827 
5828 /* for parser */
5829 
5830 int
5832 {
5833  rb_thread_t *th = GET_THREAD();
5834  rb_iseq_t *iseq;
5835  if (th->base_block && (iseq = th->base_block->iseq)) {
5836  while (iseq->type == ISEQ_TYPE_BLOCK ||
5837  iseq->type == ISEQ_TYPE_RESCUE ||
5838  iseq->type == ISEQ_TYPE_ENSURE ||
5839  iseq->type == ISEQ_TYPE_EVAL ||
5840  iseq->type == ISEQ_TYPE_MAIN
5841  ) {
5842  int i;
5843 
5844  for (i = 0; i < iseq->local_table_size; i++) {
5845  if (iseq->local_table[i] == id) {
5846  return 1;
5847  }
5848  }
5849  iseq = iseq->parent_iseq;
5850  }
5851  }
5852  return 0;
5853 }
5854 
5855 int
5857 {
5858  rb_thread_t *th = GET_THREAD();
5859  rb_iseq_t *iseq;
5860 
5861  if (th->base_block && th->base_block->iseq) {
5862  int i;
5863  iseq = th->base_block->iseq->local_iseq;
5864 
5865  for (i=0; i<iseq->local_table_size; i++) {
5866  if (iseq->local_table[i] == id) {
5867  return 1;
5868  }
5869  }
5870  }
5871  return 0;
5872 }
5873 
5874 int
5876 {
5877  return GET_THREAD()->parse_in_eval > 0;
5878 }
5879 
5880 int
5882 {
5883  return GET_THREAD()->parse_in_eval < 0;
5884 }
5885