12#include "ruby/internal/config.h"
22#include "internal/array.h"
23#include "internal/compile.h"
24#include "internal/complex.h"
25#include "internal/encoding.h"
26#include "internal/error.h"
27#include "internal/gc.h"
28#include "internal/hash.h"
29#include "internal/numeric.h"
30#include "internal/object.h"
31#include "internal/rational.h"
32#include "internal/re.h"
33#include "internal/symbol.h"
34#include "internal/thread.h"
35#include "internal/variable.h"
40#include "vm_callinfo.h"
46#include "insns_info.inc"
48#undef RUBY_UNTYPED_DATA_WARNING
49#define RUBY_UNTYPED_DATA_WARNING 0
51#define FIXNUM_INC(n, i) ((n)+(INT2FIX(i)&~FIXNUM_FLAG))
52#define FIXNUM_OR(n, i) ((n)|INT2FIX(i))
86 unsigned int rescued: 2;
87 unsigned int unremovable: 1;
92 enum ruby_vminsn_type insn_id;
122 const void *ensure_node;
127const ID rb_iseq_shared_exc_local_tbl[] = {idERROR_INFO};
147#define compile_debug CPDEBUG
149#define compile_debug ISEQ_COMPILE_DATA(iseq)->option->debug_level
154#define compile_debug_print_indent(level) \
155 ruby_debug_print_indent((level), compile_debug, gl_node_level * 2)
157#define debugp(header, value) (void) \
158 (compile_debug_print_indent(1) && \
159 ruby_debug_print_value(1, compile_debug, (header), (value)))
161#define debugi(header, id) (void) \
162 (compile_debug_print_indent(1) && \
163 ruby_debug_print_id(1, compile_debug, (header), (id)))
165#define debugp_param(header, value) (void) \
166 (compile_debug_print_indent(1) && \
167 ruby_debug_print_value(1, compile_debug, (header), (value)))
169#define debugp_verbose(header, value) (void) \
170 (compile_debug_print_indent(2) && \
171 ruby_debug_print_value(2, compile_debug, (header), (value)))
173#define debugp_verbose_node(header, value) (void) \
174 (compile_debug_print_indent(10) && \
175 ruby_debug_print_value(10, compile_debug, (header), (value)))
177#define debug_node_start(node) ((void) \
178 (compile_debug_print_indent(1) && \
179 (ruby_debug_print_node(1, CPDEBUG, "", (const NODE *)(node)), gl_node_level)), \
182#define debug_node_end() gl_node_level --
186#define debugi(header, id) ((void)0)
187#define debugp(header, value) ((void)0)
188#define debugp_verbose(header, value) ((void)0)
189#define debugp_verbose_node(header, value) ((void)0)
190#define debugp_param(header, value) ((void)0)
191#define debug_node_start(node) ((void)0)
192#define debug_node_end() ((void)0)
195#if CPDEBUG > 1 || CPDEBUG < 0
197#define printf ruby_debug_printf
198#define debugs if (compile_debug_print_indent(1)) ruby_debug_printf
199#define debug_compile(msg, v) ((void)(compile_debug_print_indent(1) && fputs((msg), stderr)), (v))
201#define debugs if(0)printf
202#define debug_compile(msg, v) (v)
205#define LVAR_ERRINFO (1)
208#define NEW_LABEL(l) new_label_body(iseq, (l))
209#define LABEL_FORMAT "<L%03d>"
211#define NEW_ISEQ(node, name, type, line_no) \
212 new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
214#define NEW_CHILD_ISEQ(node, name, type, line_no) \
215 new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
218#define ADD_SEQ(seq1, seq2) \
219 APPEND_LIST((seq1), (seq2))
222#define ADD_INSN(seq, line_node, insn) \
223 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line_node), BIN(insn), 0))
226#define INSERT_BEFORE_INSN(next, line_node, insn) \
227 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) new_insn_body(iseq, (line_node), BIN(insn), 0))
230#define INSERT_AFTER_INSN(prev, line_node, insn) \
231 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) new_insn_body(iseq, (line_node), BIN(insn), 0))
234#define ADD_INSN1(seq, line_node, insn, op1) \
235 ADD_ELEM((seq), (LINK_ELEMENT *) \
236 new_insn_body(iseq, (line_node), BIN(insn), 1, (VALUE)(op1)))
239#define INSERT_BEFORE_INSN1(next, line_node, insn, op1) \
240 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) \
241 new_insn_body(iseq, (line_node), BIN(insn), 1, (VALUE)(op1)))
244#define INSERT_AFTER_INSN1(prev, line_node, insn, op1) \
245 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) \
246 new_insn_body(iseq, (line_node), BIN(insn), 1, (VALUE)(op1)))
248#define LABEL_REF(label) ((label)->refcnt++)
251#define ADD_INSNL(seq, line_node, insn, label) (ADD_INSN1(seq, line_node, insn, label), LABEL_REF(label))
253#define ADD_INSN2(seq, line_node, insn, op1, op2) \
254 ADD_ELEM((seq), (LINK_ELEMENT *) \
255 new_insn_body(iseq, (line_node), BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
257#define ADD_INSN3(seq, line_node, insn, op1, op2, op3) \
258 ADD_ELEM((seq), (LINK_ELEMENT *) \
259 new_insn_body(iseq, (line_node), BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
262#define ADD_SEND(seq, line_node, id, argc) \
263 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
265#define ADD_SEND_WITH_FLAG(seq, line_node, id, argc, flag) \
266 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)(flag), NULL)
268#define ADD_SEND_WITH_BLOCK(seq, line_node, id, argc, block) \
269 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
271#define ADD_CALL_RECEIVER(seq, line_node) \
272 ADD_INSN((seq), (line_node), putself)
274#define ADD_CALL(seq, line_node, id, argc) \
275 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
277#define ADD_CALL_WITH_BLOCK(seq, line_node, id, argc, block) \
278 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
280#define ADD_SEND_R(seq, line_node, id, argc, block, flag, keywords) \
281 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (line_node), (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
283#define ADD_TRACE(seq, event) \
284 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), 0))
285#define ADD_TRACE_WITH_DATA(seq, event, data) \
286 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), (data)))
288static void iseq_add_getlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
289static void iseq_add_setlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
291#define ADD_GETLOCAL(seq, line_node, idx, level) iseq_add_getlocal(iseq, (seq), (line_node), (idx), (level))
292#define ADD_SETLOCAL(seq, line_node, idx, level) iseq_add_setlocal(iseq, (seq), (line_node), (idx), (level))
295#define ADD_LABEL(seq, label) \
296 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
298#define APPEND_LABEL(seq, before, label) \
299 APPEND_ELEM((seq), (before), (LINK_ELEMENT *) (label))
301#define ADD_ADJUST(seq, line_node, label) \
302 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), nd_line(line_node)))
304#define ADD_ADJUST_RESTORE(seq, label) \
305 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
307#define LABEL_UNREMOVABLE(label) \
308 ((label) ? (LABEL_REF(label), (label)->unremovable=1) : 0)
309#define ADD_CATCH_ENTRY(type, ls, le, iseqv, lc) do { \
310 VALUE _e = rb_ary_new3(5, (type), \
311 (VALUE)(ls) | 1, (VALUE)(le) | 1, \
312 (VALUE)(iseqv), (VALUE)(lc) | 1); \
313 LABEL_UNREMOVABLE(ls); \
316 if (NIL_P(ISEQ_COMPILE_DATA(iseq)->catch_table_ary)) \
317 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, rb_ary_hidden_new(3)); \
318 rb_ary_push(ISEQ_COMPILE_DATA(iseq)->catch_table_ary, freeze_hide_obj(_e)); \
322#define COMPILE(anchor, desc, node) \
323 (debug_compile("== " desc "\n", \
324 iseq_compile_each(iseq, (anchor), (node), 0)))
327#define COMPILE_POPPED(anchor, desc, node) \
328 (debug_compile("== " desc "\n", \
329 iseq_compile_each(iseq, (anchor), (node), 1)))
332#define COMPILE_(anchor, desc, node, popped) \
333 (debug_compile("== " desc "\n", \
334 iseq_compile_each(iseq, (anchor), (node), (popped))))
336#define COMPILE_RECV(anchor, desc, node, recv) \
337 (private_recv_p(node) ? \
338 (ADD_INSN(anchor, node, putself), VM_CALL_FCALL) : \
339 COMPILE(anchor, desc, recv) ? 0 : -1)
341#define OPERAND_AT(insn, idx) \
342 (((INSN*)(insn))->operands[(idx)])
344#define INSN_OF(insn) \
345 (((INSN*)(insn))->insn_id)
347#define IS_INSN(link) ((link)->type == ISEQ_ELEMENT_INSN)
348#define IS_LABEL(link) ((link)->type == ISEQ_ELEMENT_LABEL)
349#define IS_ADJUST(link) ((link)->type == ISEQ_ELEMENT_ADJUST)
350#define IS_TRACE(link) ((link)->type == ISEQ_ELEMENT_TRACE)
351#define IS_INSN_ID(iobj, insn) (INSN_OF(iobj) == BIN(insn))
352#define IS_NEXT_INSN_ID(link, insn) \
353 ((link)->next && IS_INSN((link)->next) && IS_INSN_ID((link)->next, insn))
361append_compile_error(const
rb_iseq_t *iseq,
int line, const
char *fmt, ...)
363 VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
364 VALUE file = rb_iseq_path(iseq);
369 err = rb_syntax_error_append(err, file, line, -1, NULL, fmt, args);
371 if (
NIL_P(err_info)) {
372 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err);
375 else if (!err_info) {
386compile_bug(
rb_iseq_t *iseq,
int line,
const char *fmt, ...)
390 rb_report_bug_valist(rb_iseq_path(iseq), line, fmt, args);
396#define COMPILE_ERROR append_compile_error
398#define ERROR_ARGS_AT(n) iseq, nd_line(n),
399#define ERROR_ARGS ERROR_ARGS_AT(node)
401#define EXPECT_NODE(prefix, node, ndtype, errval) \
403 const NODE *error_node = (node); \
404 enum node_type error_type = nd_type(error_node); \
405 if (error_type != (ndtype)) { \
406 COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
407 prefix ": " #ndtype " is expected, but %s", \
408 ruby_node_name(error_type)); \
413#define EXPECT_NODE_NONULL(prefix, parent, ndtype, errval) \
415 COMPILE_ERROR(ERROR_ARGS_AT(parent) \
416 prefix ": must be " #ndtype ", but 0"); \
420#define UNKNOWN_NODE(prefix, node, errval) \
422 const NODE *error_node = (node); \
423 COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
424 ruby_node_name(nd_type(error_node))); \
431#define CHECK(sub) if (!(sub)) {BEFORE_RETURN;return COMPILE_NG;}
432#define NO_CHECK(sub) (void)(sub)
437#define DECL_ANCHOR(name) \
438 LINK_ANCHOR name[1] = {{{ISEQ_ELEMENT_ANCHOR,},}}
439#define INIT_ANCHOR(name) \
440 (name->last = &name->anchor)
443freeze_hide_obj(
VALUE obj)
446 RBASIC_CLEAR_CLASS(obj);
450#include "optinsn.inc"
451#if OPT_INSTRUCTIONS_UNIFICATION
452#include "optunifs.inc"
457#define ISEQ_ARG iseq,
458#define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
461#define ISEQ_ARG_DECLARE
465#define gl_node_level ISEQ_COMPILE_DATA(iseq)->node_level
471static int insn_data_length(
INSN *iobj);
472static int calc_sp_depth(
int depth,
INSN *iobj);
474static INSN *new_insn_body(
rb_iseq_t *iseq,
const NODE *
const line_node,
enum ruby_vminsn_type insn_id,
int argc, ...);
487static int iseq_set_exception_local_table(
rb_iseq_t *iseq);
491static int iseq_set_exception_table(
rb_iseq_t *iseq);
492static int iseq_set_optargs_table(
rb_iseq_t *iseq);
495static int compile_hash(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped);
502verify_list(ISEQ_ARG_DECLARE
const char *info,
LINK_ANCHOR *
const anchor)
508 if (!compile_debug)
return;
510 list = anchor->anchor.next;
511 plist = &anchor->anchor;
513 if (plist != list->prev) {
520 if (anchor->last != plist && anchor->last != 0) {
525 rb_bug(
"list verify error: %08x (%s)", flag, info);
530#define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
537 VALUE *original = rb_iseq_original_iseq(iseq);
539 while (i < ISEQ_BODY(iseq)->iseq_size) {
540 VALUE insn = original[i];
541 const char *types = insn_op_types(insn);
543 for (
int j=0; types[j]; j++) {
544 if (types[j] == TS_CALLDATA) {
548 if (cc != vm_cc_empty()) {
550 rb_bug(
"call cache is not initialized by vm_cc_empty()");
557 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
558 struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
561 if (cc != NULL && cc != vm_cc_empty()) {
563 rb_bug(
"call cache is not initialized by vm_cc_empty()");
575 elem->prev = anchor->last;
576 anchor->last->next = elem;
578 verify_list(
"add", anchor);
588 elem->next = before->next;
589 elem->next->prev = elem;
591 if (before == anchor->last) anchor->last = elem;
592 verify_list(
"add", anchor);
595#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
596#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
600branch_coverage_valid_p(
rb_iseq_t *iseq,
int first_line)
602 if (!ISEQ_COVERAGE(iseq))
return 0;
603 if (!ISEQ_BRANCH_COVERAGE(iseq))
return 0;
604 if (first_line <= 0)
return 0;
611 const int first_lineno = nd_first_lineno(node), first_column = nd_first_column(node);
612 const int last_lineno = nd_last_lineno(node), last_column = nd_last_column(node);
614 if (!branch_coverage_valid_p(iseq, first_lineno))
return Qundef;
626 VALUE branch_base = rb_hash_aref(structure, key);
629 if (
NIL_P(branch_base)) {
630 branch_base = rb_ary_hidden_new(6);
631 rb_hash_aset(structure, key, branch_base);
632 rb_ary_push(branch_base,
ID2SYM(rb_intern(
type)));
633 rb_ary_push(branch_base,
INT2FIX(first_lineno));
634 rb_ary_push(branch_base,
INT2FIX(first_column));
635 rb_ary_push(branch_base,
INT2FIX(last_lineno));
636 rb_ary_push(branch_base,
INT2FIX(last_column));
637 branches = rb_hash_new();
638 rb_obj_hide(branches);
639 rb_ary_push(branch_base, branches);
649generate_dummy_line_node(
int lineno,
int node_id)
652 nd_set_line(&dummy, lineno);
653 nd_set_node_id(&dummy, node_id);
660 const int first_lineno = nd_first_lineno(node), first_column = nd_first_column(node);
661 const int last_lineno = nd_last_lineno(node), last_column = nd_last_column(node);
663 if (!branch_coverage_valid_p(iseq, first_lineno))
return;
674 VALUE branch = rb_hash_aref(branches, key);
678 branch = rb_ary_hidden_new(6);
679 rb_hash_aset(branches, key, branch);
681 rb_ary_push(branch,
INT2FIX(first_lineno));
682 rb_ary_push(branch,
INT2FIX(first_column));
683 rb_ary_push(branch,
INT2FIX(last_lineno));
684 rb_ary_push(branch,
INT2FIX(last_column));
687 rb_ary_push(branch,
LONG2FIX(counter_idx));
688 rb_ary_push(counters,
INT2FIX(0));
694 ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
696 NODE dummy_line_node = generate_dummy_line_node(last_lineno, nd_node_id(node));
697 ADD_INSN(seq, &dummy_line_node, nop);
700#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
703validate_label(st_data_t name, st_data_t label, st_data_t arg)
707 if (!lobj->link.next) {
709 COMPILE_ERROR(iseq, lobj->position,
710 "%"PRIsVALUE
": undefined label",
720 st_foreach(labels_table, validate_label, (st_data_t)iseq);
721 st_free_table(labels_table);
725get_nd_recv(
const NODE *node)
727 switch (nd_type(node)) {
729 return RNODE_CALL(node)->nd_recv;
731 return RNODE_OPCALL(node)->nd_recv;
735 return RNODE_QCALL(node)->nd_recv;
739 return RNODE_ATTRASGN(node)->nd_recv;
741 return RNODE_OP_ASGN1(node)->nd_recv;
743 return RNODE_OP_ASGN2(node)->nd_recv;
745 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
750get_node_call_nd_mid(
const NODE *node)
752 switch (nd_type(node)) {
754 return RNODE_CALL(node)->nd_mid;
756 return RNODE_OPCALL(node)->nd_mid;
758 return RNODE_FCALL(node)->nd_mid;
760 return RNODE_QCALL(node)->nd_mid;
762 return RNODE_VCALL(node)->nd_mid;
764 return RNODE_ATTRASGN(node)->nd_mid;
766 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
771get_nd_args(
const NODE *node)
773 switch (nd_type(node)) {
775 return RNODE_CALL(node)->nd_args;
777 return RNODE_OPCALL(node)->nd_args;
779 return RNODE_FCALL(node)->nd_args;
781 return RNODE_QCALL(node)->nd_args;
785 return RNODE_ATTRASGN(node)->nd_args;
787 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
792get_node_colon_nd_mid(
const NODE *node)
794 switch (nd_type(node)) {
796 return RNODE_COLON2(node)->nd_mid;
798 return RNODE_COLON3(node)->nd_mid;
800 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
805get_nd_vid(
const NODE *node)
807 switch (nd_type(node)) {
809 return RNODE_LASGN(node)->nd_vid;
811 return RNODE_DASGN(node)->nd_vid;
813 return RNODE_IASGN(node)->nd_vid;
815 return RNODE_CVASGN(node)->nd_vid;
817 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
823get_nd_value(
const NODE *node)
825 switch (nd_type(node)) {
827 return RNODE_LASGN(node)->nd_value;
829 return RNODE_DASGN(node)->nd_value;
831 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
841 (*ifunc->func)(iseq, ret, ifunc->data);
843 NODE dummy_line_node = generate_dummy_line_node(ISEQ_COMPILE_DATA(iseq)->last_line, -1);
844 ADD_INSN(ret, &dummy_line_node, leave);
846 CHECK(iseq_setup_insn(iseq, ret));
847 return iseq_setup(iseq, ret);
856 if (IMEMO_TYPE_P(node, imemo_ifunc)) {
857 rb_raise(rb_eArgError,
"unexpected imemo_ifunc");
861 NO_CHECK(COMPILE(ret,
"nil", node));
862 iseq_set_local_table(iseq, 0);
865 else if (nd_type_p(node, NODE_SCOPE)) {
867 iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl);
868 iseq_set_arguments(iseq, ret, (
NODE *)RNODE_SCOPE(node)->nd_args);
870 switch (ISEQ_BODY(iseq)->
type) {
871 case ISEQ_TYPE_BLOCK:
873 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
874 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
876 start->rescued = LABEL_RESCUE_BEG;
877 end->rescued = LABEL_RESCUE_END;
880 NODE dummy_line_node = generate_dummy_line_node(ISEQ_BODY(iseq)->location.first_lineno, -1);
881 ADD_INSN (ret, &dummy_line_node, nop);
882 ADD_LABEL(ret, start);
883 CHECK(COMPILE(ret,
"block body", RNODE_SCOPE(node)->nd_body));
886 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
889 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
890 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
893 case ISEQ_TYPE_CLASS:
896 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
898 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
901 case ISEQ_TYPE_METHOD:
903 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
905 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
906 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
908 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
912 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
919#define INVALID_ISEQ_TYPE(type) \
920 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
921 switch (ISEQ_BODY(iseq)->
type) {
922 case INVALID_ISEQ_TYPE(
METHOD);
923 case INVALID_ISEQ_TYPE(CLASS);
924 case INVALID_ISEQ_TYPE(BLOCK);
925 case INVALID_ISEQ_TYPE(EVAL);
926 case INVALID_ISEQ_TYPE(MAIN);
927 case INVALID_ISEQ_TYPE(TOP);
928#undef INVALID_ISEQ_TYPE
929 case ISEQ_TYPE_RESCUE:
930 iseq_set_exception_local_table(iseq);
931 CHECK(COMPILE(ret,
"rescue", node));
933 case ISEQ_TYPE_ENSURE:
934 iseq_set_exception_local_table(iseq);
935 CHECK(COMPILE_POPPED(ret,
"ensure", node));
937 case ISEQ_TYPE_PLAIN:
938 CHECK(COMPILE(ret,
"ensure", node));
941 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
944 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
949 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
950 NODE dummy_line_node = generate_dummy_line_node(0, -1);
951 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
952 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
955 NODE dummy_line_node = generate_dummy_line_node(ISEQ_COMPILE_DATA(iseq)->last_line, -1);
956 ADD_INSN(ret, &dummy_line_node, leave);
960 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
961 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
962 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
963 validate_labels(iseq, labels_table);
966 CHECK(iseq_setup_insn(iseq, ret));
967 return iseq_setup(iseq, ret);
978 CHECK(rb_translate_prism(parser, iseq, scope_node, ret));
980 CHECK(iseq_setup_insn(iseq, ret));
981 return iseq_setup(iseq, ret);
985rb_iseq_translate_threaded_code(
rb_iseq_t *iseq)
987#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
988 const void *
const *table = rb_vm_get_insns_address_table();
990 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
992 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
993 int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
994 int len = insn_len(insn);
995 encoded[i] = (
VALUE)table[insn];
1002 rb_yjit_live_iseq_count++;
1009rb_iseq_original_iseq(
const rb_iseq_t *iseq)
1011 VALUE *original_code;
1013 if (ISEQ_ORIGINAL_ISEQ(iseq))
return ISEQ_ORIGINAL_ISEQ(iseq);
1014 original_code = ISEQ_ORIGINAL_ISEQ_ALLOC(iseq, ISEQ_BODY(iseq)->iseq_size);
1015 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
1017#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1021 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1022 const void *addr = (
const void *)original_code[i];
1023 const int insn = rb_vm_insn_addr2insn(addr);
1025 original_code[i] = insn;
1026 i += insn_len(insn);
1030 return original_code;
1043#if defined(__sparc) && SIZEOF_VOIDP == 4 && defined(__GNUC__)
1044 #define STRICT_ALIGNMENT
1050#if defined(__OpenBSD__)
1051 #include <sys/endian.h>
1052 #ifdef __STRICT_ALIGNMENT
1053 #define STRICT_ALIGNMENT
1057#ifdef STRICT_ALIGNMENT
1058 #if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
1059 #define ALIGNMENT_SIZE SIZEOF_LONG_LONG
1061 #define ALIGNMENT_SIZE SIZEOF_VALUE
1063 #define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
1064 #define ALIGNMENT_SIZE_MASK PADDING_SIZE_MAX
1067 #define PADDING_SIZE_MAX 0
1070#ifdef STRICT_ALIGNMENT
1073calc_padding(
void *ptr,
size_t size)
1078 mis = (size_t)ptr & ALIGNMENT_SIZE_MASK;
1080 padding = ALIGNMENT_SIZE - mis;
1086#if ALIGNMENT_SIZE > SIZEOF_VALUE
1087 if (size ==
sizeof(
VALUE) && padding ==
sizeof(
VALUE)) {
1101#ifdef STRICT_ALIGNMENT
1102 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1104 const size_t padding = 0;
1107 if (size >= INT_MAX - padding) rb_memerror();
1108 if (storage->pos + size + padding > storage->size) {
1109 unsigned int alloc_size = storage->size;
1111 while (alloc_size < size + PADDING_SIZE_MAX) {
1112 if (alloc_size >= INT_MAX / 2) rb_memerror();
1115 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
1117 storage = *arena = storage->next;
1120 storage->size = alloc_size;
1121#ifdef STRICT_ALIGNMENT
1122 padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1126#ifdef STRICT_ALIGNMENT
1127 storage->pos += (int)padding;
1130 ptr = (
void *)&storage->buff[storage->pos];
1131 storage->pos += (int)size;
1136compile_data_alloc(
rb_iseq_t *iseq,
size_t size)
1139 return compile_data_alloc_with_arena(arena, size);
1143compile_data_alloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1146 return compile_data_alloc(iseq, size);
1150compile_data_calloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1153 void *p = compile_data_alloc(iseq, size);
1162 return (
INSN *)compile_data_alloc_with_arena(arena,
sizeof(
INSN));
1166compile_data_alloc_label(
rb_iseq_t *iseq)
1168 return (
LABEL *)compile_data_alloc(iseq,
sizeof(
LABEL));
1172compile_data_alloc_adjust(
rb_iseq_t *iseq)
1174 return (
ADJUST *)compile_data_alloc(iseq,
sizeof(
ADJUST));
1178compile_data_alloc_trace(
rb_iseq_t *iseq)
1180 return (
TRACE *)compile_data_alloc(iseq,
sizeof(
TRACE));
1189 elem2->next = elem1->next;
1190 elem2->prev = elem1;
1191 elem1->next = elem2;
1193 elem2->next->prev = elem2;
1203 elem2->prev = elem1->prev;
1204 elem2->next = elem1;
1205 elem1->prev = elem2;
1207 elem2->prev->next = elem2;
1217 elem2->prev = elem1->prev;
1218 elem2->next = elem1->next;
1220 elem1->prev->next = elem2;
1223 elem1->next->prev = elem2;
1230 elem->prev->next = elem->next;
1232 elem->next->prev = elem->prev;
1239 return anchor->anchor.next;
1245 return anchor->last;
1252 switch (elem->type) {
1253 case ISEQ_ELEMENT_INSN:
1254 case ISEQ_ELEMENT_ADJUST:
1264LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1266 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1267 if (first_insn != NULL &&
1268 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1277LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1279 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1297 if (anc2->anchor.next) {
1298 anc1->last->next = anc2->anchor.next;
1299 anc2->anchor.next->prev = anc1->last;
1300 anc1->last = anc2->last;
1302 verify_list(
"append", anc1);
1305#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1314 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1315 (
void *)anchor->anchor.next, (
void *)anchor->last);
1317 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1318 (
void *)list->prev, (
int)list->type);
1323 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1324 verify_list(
"debug list", anchor);
1327#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1330#define debug_list(anc, cur) ((void)0)
1336 TRACE *trace = compile_data_alloc_trace(iseq);
1338 trace->link.type = ISEQ_ELEMENT_TRACE;
1339 trace->link.next = NULL;
1340 trace->event = event;
1347new_label_body(
rb_iseq_t *iseq,
long line)
1349 LABEL *labelobj = compile_data_alloc_label(iseq);
1351 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1352 labelobj->link.next = 0;
1354 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1355 labelobj->sc_state = 0;
1357 labelobj->refcnt = 0;
1359 labelobj->rescued = LABEL_RESCUE_NONE;
1360 labelobj->unremovable = 0;
1367 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1368 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1369 adjust->link.next = 0;
1370 adjust->label = label;
1371 adjust->line_no = line;
1372 LABEL_UNREMOVABLE(label);
1379 const char *types = insn_op_types(insn->insn_id);
1380 for (
int j = 0; types[j]; j++) {
1381 char type = types[j];
1388 func(OPERAND_AT(insn, j), data);
1397iseq_insn_each_object_write_barrier(
VALUE obj,
VALUE iseq)
1404 int insn_id,
int argc,
VALUE *argv)
1406 INSN *iobj = compile_data_alloc_insn(iseq);
1410 iobj->link.type = ISEQ_ELEMENT_INSN;
1411 iobj->link.next = 0;
1412 iobj->insn_id = insn_id;
1413 iobj->insn_info.line_no = nd_line(line_node);
1414 iobj->insn_info.node_id = nd_node_id(line_node);
1415 iobj->insn_info.events = 0;
1416 iobj->operands = argv;
1417 iobj->operand_size = argc;
1420 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1426new_insn_body(
rb_iseq_t *iseq,
const NODE *
const line_node,
enum ruby_vminsn_type insn_id,
int argc, ...)
1428 VALUE *operands = 0;
1432 va_start(argv, argc);
1433 operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
1434 for (i = 0; i < argc; i++) {
1440 return new_insn_core(iseq, line_node, insn_id, argc, operands);
1446 VM_ASSERT(argc >= 0);
1448 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT)) &&
1449 kw_arg == NULL && !has_blockiseq) {
1450 flag |= VM_CALL_ARGS_SIMPLE;
1454 flag |= VM_CALL_KWARG;
1455 argc += kw_arg->keyword_len;
1458 ISEQ_BODY(iseq)->ci_size++;
1459 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1467 VALUE *operands = compile_data_calloc2(iseq,
sizeof(
VALUE), 2);
1470 operands[1] = (
VALUE)blockiseq;
1474 INSN *insn = new_insn_core(iseq, line_node, BIN(send), 2, operands);
1488 ast.frozen_string_literal = -1;
1489 ast.coverage_enabled = -1;
1490 ast.script_lines = ISEQ_BODY(iseq)->variable.script_lines;
1492 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1493 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1494 ret_iseq = rb_iseq_new_with_opt(&ast, name,
1495 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1497 isolated_depth ? isolated_depth + 1 : 0,
1498 type, ISEQ_COMPILE_DATA(iseq)->option);
1499 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1509 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1510 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1511 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1512 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1513 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1521 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1522 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1523 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1524 set_catch_except_p((
rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1547 while (pos < body->iseq_size) {
1548 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1549 if (insn == BIN(
throw)) {
1550 set_catch_except_p(iseq);
1553 pos += insn_len(insn);
1559 for (i = 0; i < ct->size; i++) {
1561 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1562 if (entry->type != CATCH_TYPE_BREAK
1563 && entry->type != CATCH_TYPE_NEXT
1564 && entry->type != CATCH_TYPE_REDO) {
1566 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1573iseq_insert_nop_between_end_and_cont(
rb_iseq_t *iseq)
1575 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1576 if (
NIL_P(catch_table_ary))
return;
1577 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1579 for (i = 0; i < tlen; i++) {
1585 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1587 if (ct != CATCH_TYPE_BREAK
1588 && ct != CATCH_TYPE_NEXT
1589 && ct != CATCH_TYPE_REDO) {
1591 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1593 NODE dummy_line_node = generate_dummy_line_node(0, -1);
1594 INSN *nop = new_insn_core(iseq, &dummy_line_node, BIN(nop), 0, 0);
1595 ELEM_INSERT_NEXT(end, &nop->link);
1606 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1611 if (compile_debug > 5)
1612 dump_disasm_list(FIRST_ELEMENT(anchor));
1614 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1615 iseq_optimize(iseq, anchor);
1617 if (compile_debug > 5)
1618 dump_disasm_list(FIRST_ELEMENT(anchor));
1620 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1621 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1622 iseq_insns_unification(iseq, anchor);
1623 if (compile_debug > 5)
1624 dump_disasm_list(FIRST_ELEMENT(anchor));
1627 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1628 iseq_insert_nop_between_end_and_cont(iseq);
1629 if (compile_debug > 5)
1630 dump_disasm_list(FIRST_ELEMENT(anchor));
1638 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1641 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1642 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1643 if (compile_debug > 5)
1644 dump_disasm_list(FIRST_ELEMENT(anchor));
1646 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1647 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1649 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1650 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1652 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1653 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1655 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1657 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1659 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1661 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1662 xfree(ISEQ_BODY(iseq)->catch_table);
1663 ISEQ_BODY(iseq)->catch_table = NULL;
1666#if VM_INSN_INFO_TABLE_IMPL == 2
1667 if (ISEQ_BODY(iseq)->insns_info.succ_index_table == NULL) {
1668 debugs(
"[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
1669 rb_iseq_insns_info_encode_positions(iseq);
1673 if (compile_debug > 1) {
1674 VALUE str = rb_iseq_disasm(iseq);
1677 verify_call_cache(iseq);
1678 debugs(
"[compile step: finish]\n");
1684iseq_set_exception_local_table(
rb_iseq_t *iseq)
1686 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1687 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1695 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1697 iseq = ISEQ_BODY(iseq)->parent_iseq;
1703get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1707 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1708 if (ISEQ_BODY(iseq)->local_table[i] == id) {
1718 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1721 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1722 "get_local_var_idx: %d", idx);
1729get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1731 int lv = 0, idx = -1;
1732 const rb_iseq_t *
const topmost_iseq = iseq;
1735 idx = get_dyna_var_idx_at_raw(iseq,
id);
1739 iseq = ISEQ_BODY(iseq)->parent_iseq;
1744 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1745 "get_dyna_var_idx: -1");
1749 *ls = ISEQ_BODY(iseq)->local_table_size;
1754iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1758 iseq = ISEQ_BODY(iseq)->parent_iseq;
1761 body = ISEQ_BODY(iseq);
1762 if (body->local_iseq == iseq &&
1763 body->
param.flags.has_block &&
1764 body->local_table_size - body->
param.block_start == idx) {
1773iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1776 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1777 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1788access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1790 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1792 if (isolated_depth && level >= isolated_depth) {
1793 if (
id == rb_intern(
"yield")) {
1794 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1797 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable `%s' from isolated Proc", rb_id2name(
id));
1801 for (
int i=0; i<level; i++) {
1803 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1806 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1809 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1810 if (write && !val) {
1811 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1815 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1818 iseq = ISEQ_BODY(iseq)->parent_iseq;
1823iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1825 for (
int i=0; i<level; i++) {
1826 iseq = ISEQ_BODY(iseq)->parent_iseq;
1829 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1837 if (iseq_local_block_param_p(iseq, idx, level)) {
1838 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1841 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1843 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1849 if (iseq_local_block_param_p(iseq, idx, level)) {
1850 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1853 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1855 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
1864 if (body->
param.flags.has_opt ||
1865 body->
param.flags.has_post ||
1866 body->
param.flags.has_rest ||
1867 body->
param.flags.has_block ||
1868 body->
param.flags.has_kw ||
1869 body->
param.flags.has_kwrest) {
1871 if (body->
param.flags.has_block) {
1872 body->
param.size = body->
param.block_start + 1;
1874 else if (body->
param.flags.has_kwrest) {
1875 body->
param.size = body->
param.keyword->rest_start + 1;
1877 else if (body->
param.flags.has_kw) {
1878 body->
param.size = body->
param.keyword->bits_start + 1;
1880 else if (body->
param.flags.has_post) {
1883 else if (body->
param.flags.has_rest) {
1884 body->
param.size = body->
param.rest_start + 1;
1886 else if (body->
param.flags.has_opt) {
1904 struct rb_iseq_param_keyword *keyword;
1905 const VALUE default_values = rb_ary_hidden_new(1);
1907 int kw = 0, rkw = 0, di = 0, i;
1909 body->
param.flags.has_kw = TRUE;
1910 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
1914 node = node->nd_next;
1917 keyword->bits_start = arg_size++;
1919 node = args->kw_args;
1921 const NODE *val_node = get_nd_value(node->nd_body);
1924 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
1928 switch (nd_type(val_node)) {
1930 dv = RNODE_LIT(val_node)->nd_lit;
1942 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
1946 keyword->num = ++di;
1947 rb_ary_push(default_values, dv);
1950 node = node->nd_next;
1955 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
1956 keyword->rest_start = arg_size++;
1957 body->
param.flags.has_kwrest = TRUE;
1959 keyword->required_num = rkw;
1960 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
1965 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
1967 if (dv == complex_mark) dv =
Qundef;
1974 keyword->default_values = dvs;
1982 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
1986 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
1992 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
1994 body->
param.flags.ruby2_keywords = args->ruby2_keywords;
1995 body->
param.lead_num = arg_size = (int)args->pre_args_num;
1996 if (body->
param.lead_num > 0) body->
param.flags.has_lead = TRUE;
1997 debugs(
" - argc: %d\n", body->
param.lead_num);
1999 rest_id = args->rest_arg;
2000 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2004 block_id = args->block_arg;
2006 if (args->opt_args) {
2009 VALUE labels = rb_ary_hidden_new(1);
2014 label = NEW_LABEL(nd_line(RNODE(node)));
2015 rb_ary_push(labels, (
VALUE)label | 1);
2016 ADD_LABEL(optargs, label);
2017 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2018 node = node->nd_next;
2023 label = NEW_LABEL(nd_line(node_args));
2024 rb_ary_push(labels, (
VALUE)label | 1);
2025 ADD_LABEL(optargs, label);
2030 for (j = 0; j < i+1; j++) {
2033 rb_ary_clear(labels);
2035 body->
param.flags.has_opt = TRUE;
2036 body->
param.opt_num = i;
2037 body->
param.opt_table = opt_table;
2042 body->
param.rest_start = arg_size++;
2043 body->
param.flags.has_rest = TRUE;
2044 assert(body->
param.rest_start != -1);
2047 if (args->first_post_arg) {
2048 body->
param.post_start = arg_size;
2049 body->
param.post_num = args->post_args_num;
2050 body->
param.flags.has_post = TRUE;
2051 arg_size += args->post_args_num;
2053 if (body->
param.flags.has_rest) {
2054 body->
param.post_start = body->
param.rest_start + 1;
2058 if (args->kw_args) {
2059 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2061 else if (args->kw_rest_arg) {
2062 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2063 keyword->rest_start = arg_size++;
2064 body->
param.keyword = keyword;
2065 body->
param.flags.has_kwrest = TRUE;
2067 else if (args->no_kwarg) {
2068 body->
param.flags.accepts_no_kwarg = TRUE;
2072 body->
param.block_start = arg_size++;
2073 body->
param.flags.has_block = TRUE;
2076 iseq_calc_param_size(iseq);
2077 body->
param.size = arg_size;
2079 if (args->pre_init) {
2080 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2082 if (args->post_init) {
2083 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2086 if (body->type == ISEQ_TYPE_BLOCK) {
2087 if (body->
param.flags.has_opt == FALSE &&
2088 body->
param.flags.has_post == FALSE &&
2089 body->
param.flags.has_rest == FALSE &&
2090 body->
param.flags.has_kw == FALSE &&
2091 body->
param.flags.has_kwrest == FALSE) {
2093 if (body->
param.lead_num == 1 && last_comma == 0) {
2095 body->
param.flags.ambiguous_param0 = TRUE;
2107 unsigned int size = tbl ? tbl->size : 0;
2112 ISEQ_BODY(iseq)->local_table = ids;
2114 ISEQ_BODY(iseq)->local_table_size = size;
2116 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2128 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2131 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2134 else if (tlit != tval) {
2144 long x =
FIX2LONG(rb_big_cmp(lit, val));
2152 return rb_float_cmp(lit, val);
2155 const struct RRational *rat1 = RRATIONAL(val);
2156 const struct RRational *rat2 = RRATIONAL(lit);
2157 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2160 const struct RComplex *comp1 = RCOMPLEX(val);
2161 const struct RComplex *comp2 = RCOMPLEX(lit);
2162 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2165 return rb_reg_equal(val, lit) ? 0 : -1;
2173rb_iseq_cdhash_hash(
VALUE a)
2175 switch (OBJ_BUILTIN_TYPE(a)) {
2178 return (st_index_t)a;
2186 return rb_rational_hash(a);
2188 return rb_complex_hash(a);
2198 rb_iseq_cdhash_hash,
2212 rb_hash_aset(data->hash, key,
INT2FIX(lobj->position - (data->pos+data->len)));
2220 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2227 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2229 if (rb_id_table_lookup(tbl,
id,&val)) {
2234 tbl = rb_id_table_create(1);
2235 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2237 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2238 rb_id_table_insert(tbl,
id,val);
2242#define BADINSN_DUMP(anchor, list, dest) \
2243 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2245#define BADINSN_ERROR \
2246 (xfree(generated_iseq), \
2247 xfree(insns_info), \
2248 BADINSN_DUMP(anchor, list, NULL), \
2254 int stack_max = 0, sp = 0, line = 0;
2257 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2258 if (IS_LABEL(list)) {
2264 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2265 switch (list->type) {
2266 case ISEQ_ELEMENT_INSN:
2274 sp = calc_sp_depth(sp, iobj);
2276 BADINSN_DUMP(anchor, list, NULL);
2277 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2278 "argument stack underflow (%d)", sp);
2281 if (sp > stack_max) {
2285 line = iobj->insn_info.line_no;
2287 operands = iobj->operands;
2288 insn = iobj->insn_id;
2289 types = insn_op_types(insn);
2290 len = insn_len(insn);
2293 if (iobj->operand_size !=
len - 1) {
2295 BADINSN_DUMP(anchor, list, NULL);
2296 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2297 "operand size miss! (%d for %d)",
2298 iobj->operand_size,
len - 1);
2302 for (j = 0; types[j]; j++) {
2303 if (types[j] == TS_OFFSET) {
2307 BADINSN_DUMP(anchor, list, NULL);
2308 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2309 "unknown label: "LABEL_FORMAT, lobj->label_no);
2312 if (lobj->sp == -1) {
2315 else if (lobj->sp != sp) {
2316 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2317 RSTRING_PTR(rb_iseq_path(iseq)), line,
2318 lobj->label_no, lobj->sp, sp);
2324 case ISEQ_ELEMENT_LABEL:
2327 if (lobj->sp == -1) {
2331 if (lobj->sp != sp) {
2332 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2333 RSTRING_PTR(rb_iseq_path(iseq)), line,
2334 lobj->label_no, lobj->sp, sp);
2340 case ISEQ_ELEMENT_TRACE:
2345 case ISEQ_ELEMENT_ADJUST:
2350 sp = adjust->label ? adjust->label->sp : 0;
2351 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2352 BADINSN_DUMP(anchor, list, NULL);
2353 COMPILE_ERROR(iseq, adjust->line_no,
2354 "iseq_set_sequence: adjust bug %d < %d",
2361 BADINSN_DUMP(anchor, list, NULL);
2362 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2371 int insns_info_index,
int code_index,
const INSN *iobj)
2373 if (insns_info_index == 0 ||
2374 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2375#ifdef USE_ISEQ_NODE_ID
2376 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2378 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2379 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2380#ifdef USE_ISEQ_NODE_ID
2381 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2383 insns_info[insns_info_index].events = iobj->insn_info.events;
2384 positions[insns_info_index] = code_index;
2392 int insns_info_index,
int code_index,
const ADJUST *adjust)
2394 insns_info[insns_info_index].line_no = adjust->line_no;
2395 insns_info[insns_info_index].events = 0;
2396 positions[insns_info_index] = code_index;
2401array_to_idlist(
VALUE arr)
2406 for (
int i = 0; i < size; i++) {
2415idlist_to_array(
const ID *ids)
2417 VALUE arr = rb_ary_new();
2419 rb_ary_push(arr,
ID2SYM(*ids++));
2432 unsigned int *positions;
2434 VALUE *generated_iseq;
2438 int insn_num, code_index, insns_info_index, sp = 0;
2439 int stack_max = fix_sp_depth(iseq, anchor);
2441 if (stack_max < 0)
return COMPILE_NG;
2444 insn_num = code_index = 0;
2445 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2446 switch (list->type) {
2447 case ISEQ_ELEMENT_INSN:
2451 sp = calc_sp_depth(sp, iobj);
2453 events = iobj->insn_info.events |= events;
2454 if (ISEQ_COVERAGE(iseq)) {
2455 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2456 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2457 int line = iobj->insn_info.line_no - 1;
2458 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2462 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2463 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2464 rb_ary_push(ISEQ_PC2BRANCHINDEX(iseq),
Qnil);
2469 code_index += insn_data_length(iobj);
2474 case ISEQ_ELEMENT_LABEL:
2477 lobj->position = code_index;
2478 if (lobj->sp != sp) {
2479 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2480 RSTRING_PTR(rb_iseq_path(iseq)),
2481 lobj->label_no, lobj->sp, sp);
2486 case ISEQ_ELEMENT_TRACE:
2489 events |= trace->event;
2490 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2493 case ISEQ_ELEMENT_ADJUST:
2496 if (adjust->line_no != -1) {
2498 sp = adjust->label ? adjust->label->sp : 0;
2499 if (orig_sp - sp > 0) {
2500 if (orig_sp - sp > 1) code_index++;
2514 positions =
ALLOC_N(
unsigned int, insn_num);
2515 if (ISEQ_IS_SIZE(body)) {
2519 body->is_entries = NULL;
2522 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2529 iseq_bits_t * mark_offset_bits;
2530 int code_size = code_index;
2532 iseq_bits_t tmp[1] = {0};
2533 bool needs_bitmap =
false;
2535 if (ISEQ_MBITS_BUFLEN(code_index) == 1) {
2536 mark_offset_bits = tmp;
2539 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(code_index));
2542 list = FIRST_ELEMENT(anchor);
2543 insns_info_index = code_index = sp = 0;
2546 switch (list->type) {
2547 case ISEQ_ELEMENT_INSN:
2555 sp = calc_sp_depth(sp, iobj);
2557 operands = iobj->operands;
2558 insn = iobj->insn_id;
2559 generated_iseq[code_index] = insn;
2560 types = insn_op_types(insn);
2561 len = insn_len(insn);
2563 for (j = 0; types[j]; j++) {
2564 char type = types[j];
2572 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2577 VALUE map = operands[j];
2580 data.pos = code_index;
2584 rb_hash_rehash(map);
2585 freeze_hide_obj(map);
2586 generated_iseq[code_index + 1 + j] = map;
2587 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2589 needs_bitmap =
true;
2594 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2599 VALUE v = operands[j];
2600 generated_iseq[code_index + 1 + j] = v;
2604 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2605 needs_bitmap =
true;
2612 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2613 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2614 if (UNLIKELY(ic_index >= body->ic_size)) {
2615 BADINSN_DUMP(anchor, &iobj->link, 0);
2616 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2617 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2618 ic_index, ISEQ_IS_SIZE(body));
2621 ic->
segments = array_to_idlist(operands[j]);
2623 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2628 unsigned int ic_index =
FIX2UINT(operands[j]);
2630 IVC cache = ((
IVC)&body->is_entries[ic_index]);
2632 if (insn == BIN(setinstancevariable)) {
2633 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2636 cache->iv_set_name = 0;
2639 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
2644 unsigned int ic_index =
FIX2UINT(operands[j]);
2645 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2646 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2647 BADINSN_DUMP(anchor, &iobj->link, 0);
2648 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2649 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2650 ic_index, ISEQ_IS_SIZE(body));
2652 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2659 assert(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2660 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2662 cd->cc = vm_cc_empty();
2663 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2667 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2670 generated_iseq[code_index + 1 + j] = operands[j];
2673 generated_iseq[code_index + 1 + j] = operands[j];
2676 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2677 "unknown operand type: %c",
type);
2681 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2685 case ISEQ_ELEMENT_LABEL:
2688 if (lobj->sp != sp) {
2689 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2690 RSTRING_PTR(rb_iseq_path(iseq)),
2691 lobj->label_no, lobj->sp, sp);
2696 case ISEQ_ELEMENT_ADJUST:
2701 if (adjust->label) {
2702 sp = adjust->label->sp;
2708 if (adjust->line_no != -1) {
2709 const int diff = orig_sp - sp;
2711 if (insns_info_index == 0) {
2712 COMPILE_ERROR(iseq, adjust->line_no,
2713 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2715 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2718 generated_iseq[code_index++] = BIN(adjuststack);
2719 generated_iseq[code_index++] = orig_sp - sp;
2721 else if (diff == 1) {
2722 generated_iseq[code_index++] = BIN(pop);
2724 else if (diff < 0) {
2725 int label_no = adjust->label ? adjust->label->label_no : -1;
2726 xfree(generated_iseq);
2729 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
2730 xfree(mark_offset_bits);
2732 debug_list(anchor, list);
2733 COMPILE_ERROR(iseq, adjust->line_no,
2734 "iseq_set_sequence: adjust bug to %d %d < %d",
2735 label_no, orig_sp, sp);
2748 body->iseq_encoded = (
void *)generated_iseq;
2749 body->iseq_size = code_index;
2750 body->stack_max = stack_max;
2752 if (ISEQ_MBITS_BUFLEN(body->iseq_size) == 1) {
2753 body->mark_bits.single = mark_offset_bits[0];
2757 body->mark_bits.list = mark_offset_bits;
2760 body->mark_bits.list = 0;
2761 ruby_xfree(mark_offset_bits);
2766 body->insns_info.body = insns_info;
2767 body->insns_info.positions = positions;
2770 body->insns_info.body = insns_info;
2771 REALLOC_N(positions,
unsigned int, insns_info_index);
2772 body->insns_info.positions = positions;
2773 body->insns_info.size = insns_info_index;
2779label_get_position(
LABEL *lobj)
2781 return lobj->position;
2785label_get_sp(
LABEL *lobj)
2791iseq_set_exception_table(
rb_iseq_t *iseq)
2793 const VALUE *tptr, *ptr;
2794 unsigned int tlen, i;
2797 ISEQ_BODY(iseq)->catch_table = NULL;
2799 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
2800 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
2808 for (i = 0; i < table->size; i++) {
2810 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
2811 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
2812 entry->start = label_get_position((
LABEL *)(ptr[1] & ~1));
2813 entry->end = label_get_position((
LABEL *)(ptr[2] & ~1));
2820 entry->cont = label_get_position(lobj);
2821 entry->sp = label_get_sp(lobj);
2824 if (entry->type == CATCH_TYPE_RESCUE ||
2825 entry->type == CATCH_TYPE_BREAK ||
2826 entry->type == CATCH_TYPE_NEXT) {
2834 ISEQ_BODY(iseq)->catch_table = table;
2835 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
2856 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
2858 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
2859 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
2860 opt_table[i] = label_get_position((
LABEL *)opt_table[i]);
2867get_destination_insn(
INSN *iobj)
2873 list = lobj->link.next;
2875 switch (list->type) {
2876 case ISEQ_ELEMENT_INSN:
2877 case ISEQ_ELEMENT_ADJUST:
2879 case ISEQ_ELEMENT_LABEL:
2882 case ISEQ_ELEMENT_TRACE:
2885 events |= trace->event;
2893 if (list && IS_INSN(list)) {
2895 iobj->insn_info.events |= events;
2901get_next_insn(
INSN *iobj)
2906 if (IS_INSN(list) || IS_ADJUST(list)) {
2915get_prev_insn(
INSN *iobj)
2920 if (IS_INSN(list) || IS_ADJUST(list)) {
2929unref_destination(
INSN *iobj,
int pos)
2931 LABEL *lobj = (
LABEL *)OPERAND_AT(iobj, pos);
2933 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
2937replace_destination(
INSN *dobj,
INSN *nobj)
2939 VALUE n = OPERAND_AT(nobj, 0);
2944 OPERAND_AT(dobj, 0) = n;
2945 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
2949find_destination(
INSN *i)
2951 int pos,
len = insn_len(i->insn_id);
2952 for (pos = 0; pos <
len; ++pos) {
2953 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
2954 return (
LABEL *)OPERAND_AT(i, pos);
2964 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
2967 unref_counts =
ALLOCA_N(
int, nlabels);
2968 MEMZERO(unref_counts,
int, nlabels);
2973 if (IS_INSN_ID(i, leave)) {
2977 else if ((lab = find_destination((
INSN *)i)) != 0) {
2978 if (lab->unremovable)
break;
2979 unref_counts[lab->label_no]++;
2982 else if (IS_LABEL(i)) {
2984 if (lab->unremovable)
return 0;
2985 if (lab->refcnt > unref_counts[lab->label_no]) {
2986 if (i == first)
return 0;
2991 else if (IS_TRACE(i)) {
2994 else if (IS_ADJUST(i)) {
2996 if (dest && dest->unremovable)
return 0;
2999 }
while ((i = i->next) != 0);
3004 VALUE insn = INSN_OF(i);
3005 int pos,
len = insn_len(insn);
3006 for (pos = 0; pos <
len; ++pos) {
3007 switch (insn_op_types(insn)[pos]) {
3009 unref_destination((
INSN *)i, pos);
3018 }
while ((i != end) && (i = i->next) != 0);
3025 switch (OPERAND_AT(iobj, 0)) {
3027 ELEM_REMOVE(&iobj->link);
3030 ELEM_REMOVE(&iobj->link);
3033 iobj->insn_id = BIN(adjuststack);
3039is_frozen_putstring(
INSN *insn,
VALUE *op)
3041 if (IS_INSN_ID(insn, putstring)) {
3042 *op = OPERAND_AT(insn, 0);
3045 else if (IS_INSN_ID(insn, putobject)) {
3046 *op = OPERAND_AT(insn, 0);
3077 INSN *niobj, *ciobj, *dup = 0;
3081 switch (INSN_OF(iobj)) {
3082 case BIN(putstring):
3088 case BIN(putobject):
3091 default:
return FALSE;
3094 ciobj = (
INSN *)get_next_insn(iobj);
3095 if (IS_INSN_ID(ciobj, jump)) {
3096 ciobj = (
INSN *)get_next_insn((
INSN*)OPERAND_AT(ciobj, 0));
3098 if (IS_INSN_ID(ciobj, dup)) {
3099 ciobj = (
INSN *)get_next_insn(dup = ciobj);
3101 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3102 niobj = (
INSN *)get_next_insn(ciobj);
3107 switch (INSN_OF(niobj)) {
3109 if (OPERAND_AT(ciobj, 0) ==
type) {
3110 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3113 case BIN(branchunless):
3114 if (OPERAND_AT(ciobj, 0) !=
type) {
3115 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3121 line = ciobj->insn_info.line_no;
3122 node_id = ciobj->insn_info.node_id;
3123 NODE dummy_line_node = generate_dummy_line_node(line, node_id);
3125 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3126 dest = (
LABEL *)niobj->link.next;
3129 dest = NEW_LABEL(line);
3130 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3133 INSERT_AFTER_INSN1(iobj, &dummy_line_node, jump, dest);
3135 if (!dup) INSERT_AFTER_INSN(iobj, &dummy_line_node, pop);
3142 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3143 vm_ci_flag(ci) | add,
3153 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3167 optimize_checktype(iseq, iobj);
3169 if (IS_INSN_ID(iobj, jump)) {
3170 INSN *niobj, *diobj, *piobj;
3171 diobj = (
INSN *)get_destination_insn(iobj);
3172 niobj = (
INSN *)get_next_insn(iobj);
3174 if (diobj == niobj) {
3181 unref_destination(iobj, 0);
3182 ELEM_REMOVE(&iobj->link);
3185 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3186 IS_INSN_ID(diobj, jump) &&
3187 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3188 diobj->insn_info.events == 0) {
3199 replace_destination(iobj, diobj);
3200 remove_unreachable_chunk(iseq, iobj->link.next);
3203 else if (IS_INSN_ID(diobj, leave)) {
3216 unref_destination(iobj, 0);
3217 iobj->insn_id = BIN(leave);
3218 iobj->operand_size = 0;
3219 iobj->insn_info = diobj->insn_info;
3222 else if (IS_INSN(iobj->link.prev) &&
3223 (piobj = (
INSN *)iobj->link.prev) &&
3224 (IS_INSN_ID(piobj, branchif) ||
3225 IS_INSN_ID(piobj, branchunless))) {
3226 INSN *pdiobj = (
INSN *)get_destination_insn(piobj);
3227 if (niobj == pdiobj) {
3228 int refcnt = IS_LABEL(piobj->link.next) ?
3229 ((
LABEL *)piobj->link.next)->refcnt : 0;
3244 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3245 ? BIN(branchunless) : BIN(branchif);
3246 replace_destination(piobj, iobj);
3248 ELEM_REMOVE(&iobj->link);
3255 else if (diobj == pdiobj) {
3269 NODE dummy_line_node = generate_dummy_line_node(iobj->insn_info.line_no, iobj->insn_info.node_id);
3270 INSN *popiobj = new_insn_core(iseq, &dummy_line_node, BIN(pop), 0, 0);
3271 ELEM_REPLACE(&piobj->link, &popiobj->link);
3274 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3288 if (IS_INSN_ID(iobj, newrange)) {
3289 INSN *
const range = iobj;
3291 VALUE str_beg, str_end;
3293 if ((end = (
INSN *)get_prev_insn(range)) != 0 &&
3294 is_frozen_putstring(end, &str_end) &&
3295 (beg = (
INSN *)get_prev_insn(end)) != 0 &&
3296 is_frozen_putstring(beg, &str_beg)) {
3297 int excl =
FIX2INT(OPERAND_AT(range, 0));
3300 ELEM_REMOVE(&beg->link);
3301 ELEM_REMOVE(&end->link);
3302 range->insn_id = BIN(putobject);
3303 OPERAND_AT(range, 0) = lit_range;
3308 if (IS_INSN_ID(iobj, leave)) {
3309 remove_unreachable_chunk(iseq, iobj->link.next);
3321 if (IS_INSN_ID(iobj, duparray)) {
3323 if (IS_INSN(next) && IS_INSN_ID(next, concatarray)) {
3324 iobj->insn_id = BIN(putobject);
3328 if (IS_INSN_ID(iobj, branchif) ||
3329 IS_INSN_ID(iobj, branchnil) ||
3330 IS_INSN_ID(iobj, branchunless)) {
3339 INSN *nobj = (
INSN *)get_destination_insn(iobj);
3361 int stop_optimization =
3362 ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq) &&
3363 nobj->link.type == ISEQ_ELEMENT_INSN &&
3364 nobj->insn_info.events;
3365 if (!stop_optimization) {
3366 INSN *pobj = (
INSN *)iobj->link.prev;
3369 if (!IS_INSN(&pobj->link))
3371 else if (IS_INSN_ID(pobj, dup))
3376 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3377 replace_destination(iobj, nobj);
3379 else if (prev_dup && IS_INSN_ID(nobj, dup) &&
3380 !!(nobj = (
INSN *)nobj->link.next) &&
3382 nobj->insn_id == iobj->insn_id) {
3398 replace_destination(iobj, nobj);
3426 if (prev_dup && IS_INSN(pobj->link.prev)) {
3427 pobj = (
INSN *)pobj->link.prev;
3429 if (IS_INSN_ID(pobj, putobject)) {
3430 cond = (IS_INSN_ID(iobj, branchif) ?
3431 OPERAND_AT(pobj, 0) !=
Qfalse :
3432 IS_INSN_ID(iobj, branchunless) ?
3433 OPERAND_AT(pobj, 0) ==
Qfalse :
3436 else if (IS_INSN_ID(pobj, putstring) ||
3437 IS_INSN_ID(pobj, duparray) ||
3438 IS_INSN_ID(pobj, newarray)) {
3439 cond = IS_INSN_ID(iobj, branchif);
3441 else if (IS_INSN_ID(pobj, putnil)) {
3442 cond = !IS_INSN_ID(iobj, branchif);
3445 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3446 ELEM_REMOVE(iobj->link.prev);
3448 else if (!iseq_pop_newarray(iseq, pobj)) {
3449 NODE dummy_line_node = generate_dummy_line_node(pobj->insn_info.line_no, pobj->insn_info.node_id);
3450 pobj = new_insn_core(iseq, &dummy_line_node, BIN(pop), 0, NULL);
3451 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3455 NODE dummy_line_node = generate_dummy_line_node(pobj->insn_info.line_no, pobj->insn_info.node_id);
3456 pobj = new_insn_core(iseq, &dummy_line_node, BIN(putnil), 0, NULL);
3457 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3459 iobj->insn_id = BIN(jump);
3463 unref_destination(iobj, 0);
3464 ELEM_REMOVE(&iobj->link);
3469 nobj = (
INSN *)get_destination_insn(nobj);
3474 if (IS_INSN_ID(iobj, pop)) {
3482 if (IS_INSN(prev)) {
3483 enum ruby_vminsn_type previ = ((
INSN *)prev)->insn_id;
3484 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3485 previ == BIN(putself) || previ == BIN(putstring) ||
3486 previ == BIN(dup) ||
3487 previ == BIN(getlocal) ||
3488 previ == BIN(getblockparam) ||
3489 previ == BIN(getblockparamproxy) ||
3490 previ == BIN(getinstancevariable) ||
3491 previ == BIN(duparray)) {
3495 ELEM_REMOVE(&iobj->link);
3497 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (
INSN*)prev)) {
3498 ELEM_REMOVE(&iobj->link);
3500 else if (previ == BIN(concatarray)) {
3502 NODE dummy_line_node = generate_dummy_line_node(piobj->insn_info.line_no, piobj->insn_info.node_id);
3503 INSERT_BEFORE_INSN1(piobj, &dummy_line_node, splatarray,
Qfalse);
3504 INSN_OF(piobj) = BIN(pop);
3506 else if (previ == BIN(concatstrings)) {
3507 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3511 ELEM_REMOVE(&iobj->link);
3512 INSN_OF(prev) = BIN(adjuststack);
3518 if (IS_INSN_ID(iobj, newarray) ||
3519 IS_INSN_ID(iobj, duparray) ||
3520 IS_INSN_ID(iobj, expandarray) ||
3521 IS_INSN_ID(iobj, concatarray) ||
3522 IS_INSN_ID(iobj, splatarray) ||
3532 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3538 if (IS_INSN_ID(iobj, newarray)) {
3540 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3541 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3543 op1 = OPERAND_AT(iobj, 0);
3544 op2 = OPERAND_AT(next, 0);
3555 INSN_OF(iobj) = BIN(swap);
3556 iobj->operand_size = 0;
3565 INSN_OF(iobj) = BIN(opt_reverse);
3569 NODE dummy_line_node = generate_dummy_line_node(iobj->insn_info.line_no, iobj->insn_info.node_id);
3571 INSN_OF(iobj) = BIN(opt_reverse);
3572 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3582 for (; diff > 0; diff--) {
3583 INSERT_BEFORE_INSN(iobj, &dummy_line_node, pop);
3594 for (; diff < 0; diff++) {
3595 INSERT_BEFORE_INSN(iobj, &dummy_line_node, putnil);
3602 if (IS_INSN_ID(iobj, duparray)) {
3611 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
3612 INSN_OF(iobj) = BIN(putobject);
3616 if (IS_INSN_ID(iobj, anytostring)) {
3624 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
3625 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
3630 if (IS_INSN_ID(iobj, putstring) ||
3631 (IS_INSN_ID(iobj, putobject) && RB_TYPE_P(OPERAND_AT(iobj, 0),
T_STRING))) {
3638 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
3639 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
3640 INSN *next = (
INSN *)iobj->link.next;
3641 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
3642 ELEM_REMOVE(&next->link);
3644 ELEM_REMOVE(&iobj->link);
3648 if (IS_INSN_ID(iobj, concatstrings)) {
3657 if (IS_INSN(next) && IS_INSN_ID(next, jump))
3658 next = get_destination_insn(jump = (
INSN *)next);
3659 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
3660 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
3661 OPERAND_AT(iobj, 0) =
INT2FIX(n);
3663 LABEL *label = ((
LABEL *)OPERAND_AT(jump, 0));
3664 if (!--label->refcnt) {
3665 ELEM_REMOVE(&label->link);
3668 label = NEW_LABEL(0);
3669 OPERAND_AT(jump, 0) = (
VALUE)label;
3672 ELEM_INSERT_NEXT(next, &label->link);
3673 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
3681 if (do_tailcallopt &&
3682 (IS_INSN_ID(iobj, send) ||
3683 IS_INSN_ID(iobj, opt_aref_with) ||
3684 IS_INSN_ID(iobj, opt_aset_with) ||
3685 IS_INSN_ID(iobj, invokesuper))) {
3694 if (iobj->link.next) {
3697 if (!IS_INSN(next)) {
3701 switch (INSN_OF(next)) {
3710 next = get_destination_insn((
INSN *)next);
3724 if (IS_INSN_ID(piobj, send) ||
3725 IS_INSN_ID(piobj, invokesuper)) {
3726 if (OPERAND_AT(piobj, 1) == 0) {
3727 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3728 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3733 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3734 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3740 if (IS_INSN_ID(iobj, dup)) {
3741 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
3752 if (IS_NEXT_INSN_ID(set1, setlocal)) {
3754 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3755 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3757 ELEM_REMOVE(&iobj->link);
3770 else if (IS_NEXT_INSN_ID(set1, dup) &&
3771 IS_NEXT_INSN_ID(set1->next, setlocal)) {
3772 set2 = set1->next->next;
3773 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3774 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3775 ELEM_REMOVE(set1->next);
3789 if (IS_INSN_ID(iobj, getlocal)) {
3791 if (IS_NEXT_INSN_ID(niobj, dup)) {
3792 niobj = niobj->next;
3794 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
3796 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
3797 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
3813 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
3814 if (IS_TRACE(iobj->link.next)) {
3815 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
3816 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
3818 if (iobj == (
INSN *)list && bf->argc == 0 && (iseq->body->builtin_attrs & BUILTIN_ATTR_LEAF)) {
3819 iseq->body->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_INLINE;
3832 if (IS_INSN_ID(iobj, getblockparam)) {
3833 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
3834 iobj->insn_id = BIN(getblockparamproxy);
3838 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
Qtrue) {
3850 if (IS_NEXT_INSN_ID(niobj, send)) {
3851 niobj = niobj->next;
3852 unsigned int flag = vm_ci_flag((
const struct rb_callinfo *)OPERAND_AT(niobj, 0));
3853 if ((flag & VM_CALL_ARGS_SPLAT) && !(flag & (VM_CALL_KW_SPLAT|VM_CALL_ARGS_BLOCKARG))) {
3854 OPERAND_AT(iobj, 0) =
Qfalse;
3856 }
else if (IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable)) {
3857 niobj = niobj->next;
3859 if (IS_NEXT_INSN_ID(niobj, send)) {
3860 niobj = niobj->next;
3861 unsigned int flag = vm_ci_flag((
const struct rb_callinfo *)OPERAND_AT(niobj, 0));
3863 if ((flag & VM_CALL_ARGS_SPLAT)) {
3875 if ((flag & VM_CALL_ARGS_BLOCKARG) && !(flag & VM_CALL_KW_SPLAT)) {
3876 OPERAND_AT(iobj, 0) =
Qfalse;
3890 else if (!(flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT)) {
3891 OPERAND_AT(iobj, 0) =
Qfalse;
3895 else if (IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
3896 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) {
3897 niobj = niobj->next;
3912 if (IS_NEXT_INSN_ID(niobj, send)) {
3913 niobj = niobj->next;
3914 unsigned int flag = vm_ci_flag((
const struct rb_callinfo *)OPERAND_AT(niobj, 0));
3916 if ((flag & VM_CALL_ARGS_SPLAT) && (flag & VM_CALL_KW_SPLAT) && (flag & VM_CALL_ARGS_BLOCKARG)) {
3917 OPERAND_AT(iobj, 0) =
Qfalse;
3922 else if (IS_NEXT_INSN_ID(niobj, getblockparamproxy)) {
3923 niobj = niobj->next;
3925 if (IS_NEXT_INSN_ID(niobj, send)) {
3926 niobj = niobj->next;
3927 unsigned int flag = vm_ci_flag((
const struct rb_callinfo *)OPERAND_AT(niobj, 0));
3940 if ((flag & VM_CALL_ARGS_BLOCKARG) & (flag & VM_CALL_ARGS_SPLAT) && !(flag & VM_CALL_KW_SPLAT)) {
3941 OPERAND_AT(iobj, 0) =
Qfalse;
3945 else if (IS_NEXT_INSN_ID(niobj, duphash)) {
3946 niobj = niobj->next;
3959 if (IS_NEXT_INSN_ID(niobj, send)) {
3960 niobj = niobj->next;
3961 unsigned int flag = vm_ci_flag((
const struct rb_callinfo *)OPERAND_AT(niobj, 0));
3963 if ((flag & VM_CALL_ARGS_SPLAT) && (flag & VM_CALL_KW_SPLAT) &&
3964 (flag & VM_CALL_KW_SPLAT_MUT) && !(flag & VM_CALL_ARGS_BLOCKARG)) {
3965 OPERAND_AT(iobj, 0) =
Qfalse;
3968 else if (IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
3969 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) {
3970 niobj = niobj->next;
3985 if (IS_NEXT_INSN_ID(niobj, send)) {
3986 niobj = niobj->next;
3987 unsigned int flag = vm_ci_flag((
const struct rb_callinfo *)OPERAND_AT(niobj, 0));
3989 if ((flag & VM_CALL_ARGS_SPLAT) && (flag & VM_CALL_KW_SPLAT) &&
3990 (flag & VM_CALL_KW_SPLAT_MUT) && (flag & VM_CALL_ARGS_BLOCKARG)) {
3991 OPERAND_AT(iobj, 0) =
Qfalse;
4002insn_set_specialized_instruction(
rb_iseq_t *iseq,
INSN *iobj,
int insn_id)
4004 iobj->insn_id = insn_id;
4005 iobj->operand_size = insn_len(insn_id) - 1;
4008 if (insn_id == BIN(opt_neq)) {
4009 VALUE original_ci = iobj->operands[0];
4010 iobj->operand_size = 2;
4011 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
4012 iobj->operands[0] = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4013 iobj->operands[1] = original_ci;
4022 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4023 IS_INSN(iobj->link.next)) {
4027 INSN *niobj = (
INSN *)iobj->link.next;
4028 if (IS_INSN_ID(niobj, send)) {
4030 if ((vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE) && vm_ci_argc(ci) == 0) {
4031 switch (vm_ci_mid(ci)) {
4036 VALUE num = iobj->operands[0];
4037 iobj->insn_id = BIN(opt_newarray_send);
4038 iobj->operands = compile_data_calloc2(iseq, insn_len(iobj->insn_id) - 1,
sizeof(
VALUE));
4039 iobj->operands[0] = num;
4040 iobj->operands[1] = rb_id2sym(vm_ci_mid(ci));
4041 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4042 ELEM_REMOVE(&niobj->link);
4050 if (IS_INSN_ID(iobj, send)) {
4054#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4055 if (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE) {
4056 switch (vm_ci_argc(ci)) {
4058 switch (vm_ci_mid(ci)) {
4059 case idLength: SP_INSN(length);
return COMPILE_OK;
4060 case idSize: SP_INSN(size);
return COMPILE_OK;
4061 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4062 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4063 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4064 case idNot: SP_INSN(not);
return COMPILE_OK;
4068 switch (vm_ci_mid(ci)) {
4069 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4070 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4071 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4072 case idDIV: SP_INSN(div);
return COMPILE_OK;
4073 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4074 case idEq: SP_INSN(eq);
return COMPILE_OK;
4075 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4076 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4077 case idLT: SP_INSN(lt);
return COMPILE_OK;
4078 case idLE: SP_INSN(le);
return COMPILE_OK;
4079 case idGT: SP_INSN(gt);
return COMPILE_OK;
4080 case idGE: SP_INSN(ge);
return COMPILE_OK;
4081 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4082 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4083 case idAnd: SP_INSN(and);
return COMPILE_OK;
4084 case idOr: SP_INSN(or);
return COMPILE_OK;
4088 switch (vm_ci_mid(ci)) {
4089 case idASET: SP_INSN(aset);
return COMPILE_OK;
4095 if ((vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) == 0 && blockiseq == NULL) {
4096 iobj->insn_id = BIN(opt_send_without_block);
4097 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4108 switch (ISEQ_BODY(iseq)->
type) {
4110 case ISEQ_TYPE_EVAL:
4111 case ISEQ_TYPE_MAIN:
4113 case ISEQ_TYPE_RESCUE:
4114 case ISEQ_TYPE_ENSURE:
4126 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4127 const int do_tailcallopt = tailcallable_p(iseq) &&
4128 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4129 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4130 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4131 int rescue_level = 0;
4132 int tailcallopt = do_tailcallopt;
4134 list = FIRST_ELEMENT(anchor);
4136 int do_block_optimization = 0;
4138 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK && !ISEQ_COMPILE_DATA(iseq)->catch_except_p) {
4139 do_block_optimization = 1;
4143 if (IS_INSN(list)) {
4144 if (do_peepholeopt) {
4145 iseq_peephole_optimize(iseq, list, tailcallopt);
4148 iseq_specialized_instruction(iseq, (
INSN *)list);
4151 insn_operands_unification((
INSN *)list);
4154 if (do_block_optimization) {
4156 if (IS_INSN_ID(item, jump)) {
4157 do_block_optimization = 0;
4161 if (IS_LABEL(list)) {
4162 switch (((
LABEL *)list)->rescued) {
4163 case LABEL_RESCUE_BEG:
4165 tailcallopt = FALSE;
4167 case LABEL_RESCUE_END:
4168 if (!--rescue_level) tailcallopt = do_tailcallopt;
4175 if (do_block_optimization) {
4177 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop)) {
4184#if OPT_INSTRUCTIONS_UNIFICATION
4192 VALUE *operands = 0, *ptr = 0;
4196 for (i = 0; i < size; i++) {
4197 iobj = (
INSN *)list;
4198 argc += iobj->operand_size;
4203 ptr = operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
4208 for (i = 0; i < size; i++) {
4209 iobj = (
INSN *)list;
4210 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4211 ptr += iobj->operand_size;
4215 NODE dummy_line_node = generate_dummy_line_node(iobj->insn_info.line_no, iobj->insn_info.node_id);
4216 return new_insn_core(iseq, &dummy_line_node, insn_id, argc, operands);
4228#if OPT_INSTRUCTIONS_UNIFICATION
4234 list = FIRST_ELEMENT(anchor);
4236 if (IS_INSN(list)) {
4237 iobj = (
INSN *)list;
4239 if (unified_insns_data[
id] != 0) {
4240 const int *
const *entry = unified_insns_data[id];
4241 for (j = 1; j < (intptr_t)entry[0]; j++) {
4242 const int *unified = entry[j];
4244 for (k = 2; k < unified[1]; k++) {
4246 ((
INSN *)li)->insn_id != unified[k]) {
4253 new_unified_insn(iseq, unified[0], unified[1] - 1,
4258 niobj->link.next = li;
4277all_string_result_p(
const NODE *node)
4279 if (!node)
return FALSE;
4280 switch (nd_type(node)) {
4281 case NODE_STR:
case NODE_DSTR:
4283 case NODE_IF:
case NODE_UNLESS:
4284 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4285 if (all_string_result_p(RNODE_IF(node)->nd_body))
4286 return all_string_result_p(RNODE_IF(node)->nd_else);
4288 case NODE_AND:
case NODE_OR:
4289 if (!RNODE_AND(node)->nd_2nd)
4290 return all_string_result_p(RNODE_AND(node)->nd_1st);
4291 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4293 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4302 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4303 VALUE lit = RNODE_DSTR(node)->nd_lit;
4307 debugp_param(
"nd_lit", lit);
4311 COMPILE_ERROR(ERROR_ARGS
"dstr: must be string: %s",
4312 rb_builtin_type_name(
TYPE(lit)));
4315 lit = rb_fstring(lit);
4316 ADD_INSN1(ret, node, putobject, lit);
4318 if (RSTRING_LEN(lit) == 0) first_lit = LAST_ELEMENT(ret);
4322 const NODE *
const head = list->nd_head;
4323 if (nd_type_p(head, NODE_STR)) {
4324 lit = rb_fstring(RNODE_STR(head)->nd_lit);
4325 ADD_INSN1(ret, head, putobject, lit);
4330 CHECK(COMPILE(ret,
"each string", head));
4335 if (
NIL_P(lit) && first_lit) {
4336 ELEM_REMOVE(first_lit);
4347 while (node && nd_type_p(node, NODE_BLOCK)) {
4348 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4349 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4350 node = RNODE_BLOCK(node)->nd_next;
4353 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4362 if (!RNODE_DSTR(node)->nd_next) {
4363 VALUE lit = rb_fstring(RNODE_DSTR(node)->nd_lit);
4364 ADD_INSN1(ret, node, putstring, lit);
4368 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt));
4369 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4379 if (!RNODE_DREGX(node)->nd_next) {
4380 VALUE match = RNODE_DREGX(node)->nd_lit;
4383 ADD_INSN1(ret, node, putobject, match);
4390 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt));
4391 ADD_INSN2(ret, node, toregexp,
INT2FIX(RNODE_DREGX(node)->nd_cflag),
INT2FIX(cnt));
4394 ADD_INSN(ret, node, pop);
4404 const int line = nd_line(node);
4405 LABEL *lend = NEW_LABEL(line);
4406 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4407 + VM_SVAR_FLIPFLOP_START;
4410 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4411 ADD_INSNL(ret, node, branchif, lend);
4414 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
4415 ADD_INSNL(ret, node, branchunless, else_label);
4416 ADD_INSN1(ret, node, putobject,
Qtrue);
4417 ADD_INSN1(ret, node, setspecial, key);
4419 ADD_INSNL(ret, node, jump, then_label);
4423 ADD_LABEL(ret, lend);
4424 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
4425 ADD_INSNL(ret, node, branchunless, then_label);
4426 ADD_INSN1(ret, node, putobject,
Qfalse);
4427 ADD_INSN1(ret, node, setspecial, key);
4428 ADD_INSNL(ret, node, jump, then_label);
4437#define COMPILE_SINGLE 2
4444 LABEL *label = NEW_LABEL(nd_line(cond));
4445 if (!then_label) then_label = label;
4446 else if (!else_label) else_label = label;
4448 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
4450 if (LIST_INSN_SIZE_ONE(seq)) {
4451 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
4452 if (insn->insn_id == BIN(jump) && (
LABEL *)(insn->operands[0]) == label)
4455 if (!label->refcnt) {
4456 return COMPILE_SINGLE;
4458 ADD_LABEL(seq, label);
4468 DECL_ANCHOR(ignore);
4471 switch (nd_type(cond)) {
4473 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
4474 cond = RNODE_AND(cond)->nd_2nd;
4475 if (ok == COMPILE_SINGLE) {
4476 INIT_ANCHOR(ignore);
4478 then_label = NEW_LABEL(nd_line(cond));
4482 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
4483 cond = RNODE_OR(cond)->nd_2nd;
4484 if (ok == COMPILE_SINGLE) {
4485 INIT_ANCHOR(ignore);
4487 else_label = NEW_LABEL(nd_line(cond));
4496 ADD_INSNL(ret, cond, jump, then_label);
4501 ADD_INSNL(ret, cond, jump, else_label);
4507 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
4508 ADD_INSNL(ret, cond, jump, then_label);
4511 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
4514 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
4517 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse));
4521 DECL_ANCHOR(cond_seq);
4522 INIT_ANCHOR(cond_seq);
4524 CHECK(COMPILE(cond_seq,
"branch condition", cond));
4526 if (LIST_INSN_SIZE_ONE(cond_seq)) {
4527 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
4528 if (insn->insn_id == BIN(putobject)) {
4529 if (
RTEST(insn->operands[0])) {
4530 ADD_INSNL(ret, cond, jump, then_label);
4535 ADD_INSNL(ret, cond, jump, else_label);
4540 ADD_SEQ(ret, cond_seq);
4545 ADD_INSNL(ret, cond, branchunless, else_label);
4546 ADD_INSNL(ret, cond, jump, then_label);
4553keyword_node_p(
const NODE *
const node)
4555 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
4560 const NODE *
const root_node,
4568 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
4569 const NODE *node = RNODE_HASH(root_node)->nd_head;
4573 const NODE *key_node = RNODE_LIST(node)->nd_head;
4576 assert(nd_type_p(node, NODE_LIST));
4577 if (key_node && nd_type_p(key_node, NODE_LIT) &&
SYMBOL_P(RNODE_LIT(key_node)->nd_lit)) {
4582 *flag |= VM_CALL_KW_SPLAT;
4583 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4588 *flag |= VM_CALL_KW_SPLAT_MUT;
4593 node = RNODE_LIST(node)->nd_next;
4594 node = RNODE_LIST(node)->nd_next;
4598 node = RNODE_HASH(root_node)->nd_head;
4600 int len = (int)RNODE_LIST(node)->as.nd_alen / 2;
4603 VALUE *keywords = kw_arg->keywords;
4605 kw_arg->references = 0;
4606 kw_arg->keyword_len =
len;
4608 *kw_arg_ptr = kw_arg;
4610 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4611 const NODE *key_node = RNODE_LIST(node)->nd_head;
4612 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
4613 keywords[i] = RNODE_LIT(key_node)->nd_lit;
4614 NO_CHECK(COMPILE(ret,
"keyword values", val_node));
4628 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
4630 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
4633 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
4634 *kwnode_ptr = RNODE_LIST(node)->nd_head;
4637 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
4638 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
4646static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq)
4648 switch (nd_type(node)) {
4655 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal;
4664 switch (nd_type(node)) {
4672 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
4674 VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq),
INT2FIX((
int)nd_line(node)));
4675 lit = rb_str_dup(RNODE_STR(node)->nd_lit);
4676 rb_ivar_set(lit, id_debug_created_info, rb_obj_freeze(debug_info));
4680 return rb_fstring(RNODE_STR(node)->nd_lit);
4683 return RNODE_LIT(node)->nd_lit;
4690 const NODE *line_node = node;
4692 if (nd_type_p(node, NODE_ZLIST)) {
4694 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
4699 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
4702 for (; node; node = RNODE_LIST(node)->nd_next) {
4703 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
4745 const int max_stack_len = 0x100;
4746 const int min_tmp_ary_len = 0x40;
4748 int first_chunk = 1;
4751#define FLUSH_CHUNK(newarrayinsn) \
4753 ADD_INSN1(ret, line_node, newarrayinsn, INT2FIX(stack_len)); \
4754 if (!first_chunk) ADD_INSN(ret, line_node, concatarray); \
4755 first_chunk = stack_len = 0; \
4762 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq)) {
4764 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
4765 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq); node_tmp = RNODE_LIST(node_tmp)->nd_next)
4768 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
4770 VALUE ary = rb_ary_hidden_new(count);
4773 for (; count; count--, node = RNODE_LIST(node)->nd_next)
4774 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
4778 FLUSH_CHUNK(newarray);
4780 ADD_INSN1(ret, line_node, duparray, ary);
4784 ADD_INSN1(ret, line_node, putobject, ary);
4785 ADD_INSN(ret, line_node, concatarray);
4792 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
4794 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
4797 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
4800 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
4802 FLUSH_CHUNK(newarraykwsplat);
4807 if (stack_len >= max_stack_len) FLUSH_CHUNK(newarray);
4811 FLUSH_CHUNK(newarray);
4820 if (static_literal_node_p(node, iseq)) {
4821 VALUE ary = rb_ary_hidden_new(1);
4822 rb_ary_push(ary, static_literal_value(node, iseq));
4825 ADD_INSN1(ret, node, duparray, ary);
4828 CHECK(COMPILE_(ret,
"array element", node, FALSE));
4829 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
4836static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
4838 return RNODE_LIST(node)->nd_head && static_literal_node_p(RNODE_LIST(node)->nd_head, iseq) && static_literal_node_p(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
4844 const NODE *line_node = node;
4846 node = RNODE_HASH(node)->nd_head;
4848 if (!node || nd_type_p(node, NODE_ZLIST)) {
4850 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
4855 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
4858 for (; node; node = RNODE_LIST(node)->nd_next) {
4859 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
4882 const int max_stack_len = 0x100;
4883 const int min_tmp_hash_len = 0x800;
4885 int first_chunk = 1;
4886 DECL_ANCHOR(anchor);
4887 INIT_ANCHOR(anchor);
4890#define FLUSH_CHUNK() \
4892 if (first_chunk) { \
4893 APPEND_LIST(ret, anchor); \
4894 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
4897 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
4898 ADD_INSN(ret, line_node, swap); \
4899 APPEND_LIST(ret, anchor); \
4900 ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
4902 INIT_ANCHOR(anchor); \
4903 first_chunk = stack_len = 0; \
4910 if (static_literal_node_pair_p(node, iseq)) {
4912 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
4913 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
4916 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
4918 VALUE ary = rb_ary_hidden_new(count);
4921 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4923 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
4924 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
4925 rb_ary_cat(ary, elem, 2);
4929 hash = rb_obj_hide(hash);
4935 ADD_INSN1(ret, line_node, duphash, hash);
4939 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4940 ADD_INSN(ret, line_node, swap);
4942 ADD_INSN1(ret, line_node, putobject, hash);
4944 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
4951 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4954 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
4957 if (RNODE_LIST(node)->nd_head) {
4959 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
4960 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
4964 if (stack_len >= max_stack_len) FLUSH_CHUNK();
4970 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
4971 int empty_kw = nd_type_p(kw, NODE_LIT) && RB_TYPE_P(RNODE_LIT(kw)->nd_lit,
T_HASH);
4972 int first_kw = first_chunk && stack_len == 0;
4973 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
4974 int only_kw = last_kw && first_kw;
4977 if (only_kw && method_call_keywords) {
4985 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
4987 else if (first_kw) {
4991 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
4998 if (only_kw && method_call_keywords) {
5004 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5011 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5012 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5013 else ADD_INSN(ret, line_node, swap);
5015 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5017 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5032rb_node_case_when_optimizable_literal(
const NODE *
const node)
5034 switch (nd_type(node)) {
5036 VALUE v = RNODE_LIT(node)->nd_lit;
5057 return rb_fstring(RNODE_STR(node)->nd_lit);
5064 LABEL *l1,
int only_special_literals,
VALUE literals)
5067 const NODE *val = RNODE_LIST(vals)->nd_head;
5068 VALUE lit = rb_node_case_when_optimizable_literal(val);
5071 only_special_literals = 0;
5073 else if (
NIL_P(rb_hash_lookup(literals, lit))) {
5074 rb_hash_aset(literals, lit, (
VALUE)(l1) | 1);
5077 if (nd_type_p(val, NODE_STR)) {
5078 debugp_param(
"nd_lit", RNODE_STR(val)->nd_lit);
5079 lit = rb_fstring(RNODE_STR(val)->nd_lit);
5080 ADD_INSN1(cond_seq, val, putobject, lit);
5084 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5088 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5089 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5090 ADD_INSNL(cond_seq, val, branchif, l1);
5091 vals = RNODE_LIST(vals)->nd_next;
5093 return only_special_literals;
5098 LABEL *l1,
int only_special_literals,
VALUE literals)
5100 const NODE *line_node = vals;
5102 switch (nd_type(vals)) {
5104 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5108 ADD_INSN (cond_seq, line_node, dup);
5109 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5110 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5111 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5112 ADD_INSNL(cond_seq, line_node, branchif, l1);
5115 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5116 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5119 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5120 ADD_INSN (cond_seq, line_node, dup);
5121 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5122 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5123 ADD_INSNL(cond_seq, line_node, branchif, l1);
5126 ADD_INSN (cond_seq, line_node, dup);
5127 CHECK(COMPILE(cond_seq,
"when val", vals));
5128 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5129 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5130 ADD_INSNL(cond_seq, line_node, branchif, l1);
5223 const NODE *line_node;
5238add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc,
INSN *before_insn)
5241 rb_bug(
"no masgn_state");
5250 memo->before_insn = before_insn;
5251 memo->line_node = line_node;
5252 memo->argn = state->num_args + 1;
5253 memo->num_args = argc;
5254 state->num_args += argc;
5255 memo->lhs_pos = lhs_pos;
5257 if (!state->first_memo) {
5258 state->first_memo = memo;
5261 state->last_memo->next = memo;
5263 state->last_memo = memo;
5273 switch (nd_type(node)) {
5274 case NODE_ATTRASGN: {
5276 const NODE *line_node = node;
5278 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5281 iobj = (
INSN *)get_prev_insn((
INSN *)insn_element);
5283 ELEM_REMOVE(LAST_ELEMENT(pre));
5285 pre->last = iobj->link.prev;
5288 int argc = vm_ci_argc(ci) + 1;
5289 ci = ci_argc_set(iseq, ci, argc);
5290 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5294 ADD_INSN(lhs, line_node, swap);
5297 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5300 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (
INSN *)LAST_ELEMENT(lhs))) {
5305 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5306 int argc = vm_ci_argc(ci);
5307 ci = ci_argc_set(iseq, ci, argc - 1);
5308 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5310 INSERT_BEFORE_INSN1(iobj, line_node, newarray,
INT2FIX(1));
5311 INSERT_BEFORE_INSN(iobj, line_node, concatarray);
5313 ADD_INSN(lhs, line_node, pop);
5315 ADD_INSN(lhs, line_node, pop);
5317 for (
int i=0; i < argc; i++) {
5318 ADD_INSN(post, line_node, pop);
5323 DECL_ANCHOR(nest_rhs);
5324 INIT_ANCHOR(nest_rhs);
5325 DECL_ANCHOR(nest_lhs);
5326 INIT_ANCHOR(nest_lhs);
5328 int prev_level = state->lhs_level;
5329 bool prev_nested = state->nested;
5331 state->lhs_level = lhs_pos - 1;
5332 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5333 state->lhs_level = prev_level;
5334 state->nested = prev_nested;
5336 ADD_SEQ(lhs, nest_rhs);
5337 ADD_SEQ(lhs, nest_lhs);
5341 if (!RNODE_CDECL(node)->nd_vid) {
5345 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5348 iobj = (
INSN *)insn_element;
5351 ELEM_REMOVE(insn_element);
5352 pre->last = iobj->link.prev;
5355 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (
INSN *)LAST_ELEMENT(lhs))) {
5359 ADD_INSN(post, node, pop);
5364 DECL_ANCHOR(anchor);
5365 INIT_ANCHOR(anchor);
5366 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
5367 ELEM_REMOVE(FIRST_ELEMENT(anchor));
5368 ADD_SEQ(lhs, anchor);
5379 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
5380 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
5387 const NODE *rhsn,
const NODE *orig_lhsn)
5390 const int memsize = numberof(mem);
5392 int llen = 0, rlen = 0;
5394 const NODE *lhsn = orig_lhsn;
5396#define MEMORY(v) { \
5398 if (memindex == memsize) return 0; \
5399 for (i=0; i<memindex; i++) { \
5400 if (mem[i] == (v)) return 0; \
5402 mem[memindex++] = (v); \
5405 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
5410 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
5411 switch (nd_type(ln)) {
5416 MEMORY(get_nd_vid(ln));
5421 lhsn = RNODE_LIST(lhsn)->nd_next;
5427 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
5430 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
5432 rhsn = RNODE_LIST(rhsn)->nd_next;
5437 for (i=0; i<llen-rlen; i++) {
5438 ADD_INSN(ret, orig_lhsn, putnil);
5442 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
5449 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
5450 const NODE *splatn = RNODE_MASGN(node)->nd_args;
5451 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
5452 const NODE *lhsn_count = lhsn;
5453 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
5458 while (lhsn_count) {
5460 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
5463 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
5465 lhsn = RNODE_LIST(lhsn)->nd_next;
5469 if (nd_type_p(splatn, NODE_POSTARG)) {
5471 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
5472 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
5473 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
5475 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
5477 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
5479 if (NODE_NAMED_REST_P(restn)) {
5480 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
5483 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
5485 postn = RNODE_LIST(postn)->nd_next;
5490 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
5494 if (!state->nested) {
5495 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
5499 ADD_INSN(rhs, node, dup);
5501 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
5508 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
5510 state.lhs_level = popped ? 0 : 1;
5513 state.first_memo = NULL;
5514 state.last_memo = NULL;
5524 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
5528 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
5529 for (
int i = 0; i < memo->num_args; i++) {
5530 INSERT_BEFORE_INSN1(memo->before_insn, memo->line_node, topn, topn_arg);
5532 tmp_memo = memo->next;
5541 if (!popped && state.num_args >= 1) {
5543 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
5553 VALUE arr = rb_ary_new();
5555 switch (nd_type(node)) {
5557 rb_ary_unshift(arr,
ID2SYM(RNODE_CONST(node)->nd_vid));
5560 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON3(node)->nd_mid));
5561 rb_ary_unshift(arr,
ID2SYM(idNULL));
5564 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON2(node)->nd_mid));
5565 node = RNODE_COLON2(node)->nd_head;
5574compile_const_prefix(
rb_iseq_t *iseq,
const NODE *
const node,
5577 switch (nd_type(node)) {
5579 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
5580 ADD_INSN1(body, node, putobject,
Qtrue);
5581 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
5584 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
5585 ADD_INSN(body, node, pop);
5586 ADD_INSN1(body, node, putobject, rb_cObject);
5587 ADD_INSN1(body, node, putobject,
Qtrue);
5588 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
5591 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
5592 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
5593 ADD_INSN1(body, node, putobject,
Qfalse);
5594 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
5597 CHECK(COMPILE(pref,
"const colon2 prefix", node));
5606 if (nd_type_p(cpath, NODE_COLON3)) {
5608 ADD_INSN1(ret, cpath, putobject, rb_cObject);
5609 return VM_DEFINECLASS_FLAG_SCOPED;
5611 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
5613 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
5614 return VM_DEFINECLASS_FLAG_SCOPED;
5618 ADD_INSN1(ret, cpath, putspecialobject,
5619 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5625private_recv_p(
const NODE *node)
5627 NODE *recv = get_nd_recv(node);
5628 if (recv && nd_type_p(recv, NODE_SELF)) {
5629 return RNODE_SELF(recv)->nd_state != 0;
5639compile_call(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
const enum node_type
type,
const NODE *
const line_node,
int popped,
bool assume_receiver);
5646 enum defined_type expr_type = DEFINED_NOT_DEFINED;
5647 enum node_type
type;
5648 const int line = nd_line(node);
5649 const NODE *line_node = node;
5651 switch (
type = nd_type(node)) {
5655 expr_type = DEFINED_NIL;
5658 expr_type = DEFINED_SELF;
5661 expr_type = DEFINED_TRUE;
5664 expr_type = DEFINED_FALSE;
5668 const NODE *vals = node;
5671 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
5674 lfinish[1] = NEW_LABEL(line);
5676 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5677 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
5686 expr_type = DEFINED_EXPR;
5692 expr_type = DEFINED_LVAR;
5695#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
5697 ADD_INSN3(ret, line_node, definedivar,
5698 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
5702 ADD_INSN(ret, line_node, putnil);
5703 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
5704 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
5708 ADD_INSN(ret, line_node, putnil);
5709 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
5710 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
5714 ADD_INSN(ret, line_node, putnil);
5715 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
5716 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
5720 lfinish[1] = NEW_LABEL(line);
5722 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
5723 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5724 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
5727 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
5728 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
5731 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
5732 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
5736 ADD_INSN1(ret, line_node, putobject, rb_cObject);
5737 ADD_INSN3(ret, line_node, defined,
5738 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
5746 case NODE_ATTRASGN:{
5747 const int explicit_receiver =
5748 (
type == NODE_CALL ||
type == NODE_OPCALL ||
5749 (
type == NODE_ATTRASGN && !private_recv_p(node)));
5751 if (get_nd_args(node) || explicit_receiver) {
5753 lfinish[1] = NEW_LABEL(line);
5756 lfinish[2] = NEW_LABEL(line);
5759 if (get_nd_args(node)) {
5760 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
5761 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5763 if (explicit_receiver) {
5764 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
5765 switch (nd_type(get_nd_recv(node))) {
5771 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
5772 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
5775 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5776 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
5780 ADD_INSN(ret, line_node, dup);
5782 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
5783 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
5786 ADD_INSN(ret, line_node, putself);
5788 ADD_INSN(ret, line_node, dup);
5790 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
5791 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
5797 ADD_INSN(ret, line_node, putnil);
5798 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
5799 PUSH_VAL(DEFINED_YIELD));
5804 ADD_INSN(ret, line_node, putnil);
5805 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
5806 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
5807 PUSH_VAL(DEFINED_GVAR));
5812 ADD_INSN(ret, line_node, putnil);
5813 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
5814 PUSH_VAL(DEFINED_ZSUPER));
5820 case NODE_OP_ASGN_OR:
5821 case NODE_OP_ASGN_AND:
5829 expr_type = DEFINED_ASGN;
5833 assert(expr_type != DEFINED_NOT_DEFINED);
5836 VALUE str = rb_iseq_defined_string(expr_type);
5837 ADD_INSN1(ret, line_node, putobject, str);
5840 ADD_INSN1(ret, line_node, putobject,
Qtrue);
5847 NODE dummy_line_node = generate_dummy_line_node(0, -1);
5848 ADD_INSN(ret, &dummy_line_node, putnil);
5849 iseq_set_exception_local_table(iseq);
5857 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
5859 int line = nd_line(node);
5860 LABEL *lstart = NEW_LABEL(line);
5861 LABEL *lend = NEW_LABEL(line);
5864 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
5865 rescue = new_child_iseq_with_callback(iseq, ifunc,
5867 ISEQ_BODY(iseq)->location.label),
5868 iseq, ISEQ_TYPE_RESCUE, 0);
5869 lstart->rescued = LABEL_RESCUE_BEG;
5870 lend->rescued = LABEL_RESCUE_END;
5871 APPEND_LABEL(ret, lcur, lstart);
5872 ADD_LABEL(ret, lend);
5873 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
5880 const int line = nd_line(node);
5881 const NODE *line_node = node;
5882 if (!RNODE_DEFINED(node)->nd_head) {
5883 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
5884 ADD_INSN1(ret, line_node, putobject, str);
5889 lfinish[0] = NEW_LABEL(line);
5892 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr);
5894 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, line_node, BIN(putnil), 0)->link);
5895 ADD_INSN(ret, line_node, swap);
5897 ADD_LABEL(ret, lfinish[2]);
5899 ADD_INSN(ret, line_node, pop);
5900 ADD_LABEL(ret, lfinish[1]);
5902 ADD_LABEL(ret, lfinish[0]);
5908make_name_for_block(
const rb_iseq_t *orig_iseq)
5913 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
5914 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
5915 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
5918 iseq = ISEQ_BODY(iseq)->parent_iseq;
5923 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
5926 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
5935 enl->ensure_node = node;
5936 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
5938 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
5948 while (erange->next != 0) {
5949 erange = erange->next;
5953 ne->end = erange->end;
5954 erange->end = lstart;
5960can_add_ensure_iseq(
const rb_iseq_t *iseq)
5963 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
5965 if (e->ensure_node)
return false;
5975 assert(can_add_ensure_iseq(iseq));
5978 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
5980 DECL_ANCHOR(ensure);
5982 INIT_ANCHOR(ensure);
5984 if (enlp->erange != NULL) {
5985 DECL_ANCHOR(ensure_part);
5986 LABEL *lstart = NEW_LABEL(0);
5987 LABEL *lend = NEW_LABEL(0);
5988 INIT_ANCHOR(ensure_part);
5990 add_ensure_range(iseq, enlp->erange, lstart, lend);
5992 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
5993 ADD_LABEL(ensure_part, lstart);
5994 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
5995 ADD_LABEL(ensure_part, lend);
5996 ADD_SEQ(ensure, ensure_part);
6005 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6006 ADD_SEQ(ret, ensure);
6011check_keyword(
const NODE *node)
6015 if (nd_type_p(node, NODE_LIST)) {
6016 while (RNODE_LIST(node)->nd_next) {
6017 node = RNODE_LIST(node)->nd_next;
6019 node = RNODE_LIST(node)->nd_head;
6022 return keyword_node_p(node);
6027keyword_node_single_splat_p(
NODE *kwnode)
6031 NODE *node = RNODE_HASH(kwnode)->nd_head;
6032 return RNODE_LIST(node)->nd_head == NULL &&
6033 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6040 if (!argn)
return 0;
6042 NODE *kwnode = NULL;
6044 switch (nd_type(argn)) {
6047 int len = compile_args(iseq, args, argn, &kwnode);
6048 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6051 if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6055 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6063 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6064 ADD_INSN1(args, argn, splatarray, RBOOL(dup_rest));
6065 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6066 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6069 case NODE_ARGSCAT: {
6070 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6071 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, 1, NULL, NULL);
6073 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6074 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6075 if (kwnode) rest_len--;
6076 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6079 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6080 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6083 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6084 ADD_INSN1(args, argn, splatarray,
Qtrue);
6088 ADD_INSN1(args, argn, splatarray,
Qfalse);
6089 ADD_INSN(args, argn, concatarray);
6095 *flag_ptr |= VM_CALL_KW_SPLAT;
6096 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6097 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6103 case NODE_ARGSPUSH: {
6104 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6105 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, 1, NULL, NULL);
6107 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6108 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6109 if (kwnode) rest_len--;
6110 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6111 ADD_INSN1(args, argn, newarray,
INT2FIX(1));
6112 ADD_INSN(args, argn, concatarray);
6115 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6116 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6119 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6120 ADD_INSN1(args, argn, newarray,
INT2FIX(1));
6121 ADD_INSN(args, argn, concatarray);
6127 *flag_ptr |= VM_CALL_KW_SPLAT;
6128 if (!keyword_node_single_splat_p(kwnode)) {
6129 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6131 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6138 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
6148 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
6149 unsigned int dup_rest = 1;
6150 DECL_ANCHOR(arg_block);
6151 INIT_ANCHOR(arg_block);
6152 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
6154 *flag |= VM_CALL_ARGS_BLOCKARG;
6156 if (LIST_INSN_SIZE_ONE(arg_block)) {
6158 if (IS_INSN(elem)) {
6160 if (iobj->insn_id == BIN(getblockparam)) {
6161 iobj->insn_id = BIN(getblockparamproxy);
6166 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, dup_rest, flag, keywords));
6167 ADD_SEQ(args, arg_block);
6170 ret =
INT2FIX(setup_args_core(iseq, args, argn, 0, flag, keywords));
6178 const NODE *body = ptr;
6179 int line = nd_line(body);
6181 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
6183 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6184 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
6186 iseq_set_local_table(iseq, 0);
6194 int line = nd_line(node);
6195 const NODE *line_node = node;
6196 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
6198#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
6199 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
6203 ADD_INSN(ret, line_node, dup);
6204 ADD_INSNL(ret, line_node, branchunless, fail_label);
6206 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6208 if (RNODE_BLOCK(vars)->nd_next) {
6209 ADD_INSN(ret, line_node, dup);
6212 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6214 cap = new_insn_send(iseq, line_node, idAREF,
INT2FIX(1),
6217#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
6218 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
6223 ADD_INSNL(nom, line_node, jump, end_label);
6224 ADD_LABEL(nom, fail_label);
6226 ADD_INSN(nom, line_node, pop);
6227 ADD_INSN(nom, line_node, putnil);
6229 ADD_LABEL(nom, end_label);
6230 (nom->last->next = cap->link.next)->prev = nom->last;
6231 (cap->link.next = nom->anchor.next)->prev = &cap->link;
6236 ADD_INSNL(ret, line_node, jump, end_label);
6237 ADD_LABEL(ret, fail_label);
6238 ADD_INSN(ret, line_node, pop);
6239 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6241 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6243 ((
INSN*)last)->insn_id = BIN(putnil);
6244 ((
INSN*)last)->operand_size = 0;
6246 ADD_LABEL(ret, end_label);
6250optimizable_range_item_p(
const NODE *n)
6252 if (!n)
return FALSE;
6253 switch (nd_type(n)) {
6266 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
6267 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
6269 const int line = nd_line(node);
6270 const NODE *line_node = node;
6271 DECL_ANCHOR(cond_seq);
6272 LABEL *then_label, *else_label, *end_label;
6275 INIT_ANCHOR(cond_seq);
6276 then_label = NEW_LABEL(line);
6277 else_label = NEW_LABEL(line);
6280 compile_branch_condition(iseq, cond_seq, RNODE_IF(node)->nd_cond, then_label, else_label);
6281 ADD_SEQ(ret, cond_seq);
6283 if (then_label->refcnt && else_label->refcnt) {
6284 branches = decl_branch_base(iseq, node,
type == NODE_IF ?
"if" :
"unless");
6287 if (then_label->refcnt) {
6288 ADD_LABEL(ret, then_label);
6290 DECL_ANCHOR(then_seq);
6291 INIT_ANCHOR(then_seq);
6292 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
6294 if (else_label->refcnt) {
6295 add_trace_branch_coverage(
6298 node_body ? node_body : node,
6300 type == NODE_IF ?
"then" :
"else",
6302 end_label = NEW_LABEL(line);
6303 ADD_INSNL(then_seq, line_node, jump, end_label);
6305 ADD_INSN(then_seq, line_node, pop);
6308 ADD_SEQ(ret, then_seq);
6311 if (else_label->refcnt) {
6312 ADD_LABEL(ret, else_label);
6314 DECL_ANCHOR(else_seq);
6315 INIT_ANCHOR(else_seq);
6316 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
6318 if (then_label->refcnt) {
6319 add_trace_branch_coverage(
6322 node_else ? node_else : node,
6324 type == NODE_IF ?
"else" :
"then",
6327 ADD_SEQ(ret, else_seq);
6331 ADD_LABEL(ret, end_label);
6341 const NODE *node = orig_node;
6342 LABEL *endlabel, *elselabel;
6344 DECL_ANCHOR(body_seq);
6345 DECL_ANCHOR(cond_seq);
6346 int only_special_literals = 1;
6347 VALUE literals = rb_hash_new();
6349 enum node_type
type;
6350 const NODE *line_node;
6355 INIT_ANCHOR(body_seq);
6356 INIT_ANCHOR(cond_seq);
6358 RHASH_TBL_RAW(literals)->type = &cdhash_type;
6360 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
6362 branches = decl_branch_base(iseq, node,
"case");
6364 node = RNODE_CASE(node)->nd_body;
6365 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
6366 type = nd_type(node);
6367 line = nd_line(node);
6370 endlabel = NEW_LABEL(line);
6371 elselabel = NEW_LABEL(line);
6375 while (
type == NODE_WHEN) {
6378 l1 = NEW_LABEL(line);
6379 ADD_LABEL(body_seq, l1);
6380 ADD_INSN(body_seq, line_node, pop);
6381 add_trace_branch_coverage(
6384 RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node,
6388 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
6389 ADD_INSNL(body_seq, line_node, jump, endlabel);
6391 vals = RNODE_WHEN(node)->nd_head;
6393 switch (nd_type(vals)) {
6395 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
6396 if (only_special_literals < 0)
return COMPILE_NG;
6401 only_special_literals = 0;
6402 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
6405 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
6409 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
6412 node = RNODE_WHEN(node)->nd_next;
6416 type = nd_type(node);
6417 line = nd_line(node);
6422 ADD_LABEL(cond_seq, elselabel);
6423 ADD_INSN(cond_seq, line_node, pop);
6424 add_trace_branch_coverage(iseq, cond_seq, node, branch_id,
"else", branches);
6425 CHECK(COMPILE_(cond_seq,
"else", node, popped));
6426 ADD_INSNL(cond_seq, line_node, jump, endlabel);
6429 debugs(
"== else (implicit)\n");
6430 ADD_LABEL(cond_seq, elselabel);
6431 ADD_INSN(cond_seq, orig_node, pop);
6432 add_trace_branch_coverage(iseq, cond_seq, orig_node, branch_id,
"else", branches);
6434 ADD_INSN(cond_seq, orig_node, putnil);
6436 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
6439 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
6440 ADD_INSN(ret, orig_node, dup);
6441 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
6443 LABEL_REF(elselabel);
6446 ADD_SEQ(ret, cond_seq);
6447 ADD_SEQ(ret, body_seq);
6448 ADD_LABEL(ret, endlabel);
6457 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
6459 DECL_ANCHOR(body_seq);
6463 branches = decl_branch_base(iseq, orig_node,
"case");
6465 INIT_ANCHOR(body_seq);
6466 endlabel = NEW_LABEL(nd_line(node));
6468 while (node && nd_type_p(node, NODE_WHEN)) {
6469 const int line = nd_line(node);
6470 LABEL *l1 = NEW_LABEL(line);
6471 ADD_LABEL(body_seq, l1);
6472 add_trace_branch_coverage(
6475 RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node,
6479 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
6480 ADD_INSNL(body_seq, node, jump, endlabel);
6482 vals = RNODE_WHEN(node)->nd_head;
6484 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
6486 switch (nd_type(vals)) {
6490 val = RNODE_LIST(vals)->nd_head;
6491 lnext = NEW_LABEL(nd_line(val));
6492 debug_compile(
"== when2\n", (
void)0);
6493 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
6494 ADD_LABEL(ret, lnext);
6495 vals = RNODE_LIST(vals)->nd_next;
6501 ADD_INSN(ret, vals, putnil);
6502 CHECK(COMPILE(ret,
"when2/cond splat", vals));
6503 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
6504 ADD_INSNL(ret, vals, branchif, l1);
6507 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
6509 node = RNODE_WHEN(node)->nd_next;
6512 add_trace_branch_coverage(
6515 node ? node : orig_node,
6519 CHECK(COMPILE_(ret,
"else", node, popped));
6520 ADD_INSNL(ret, orig_node, jump, endlabel);
6522 ADD_SEQ(ret, body_seq);
6523 ADD_LABEL(ret, endlabel);
6527static int iseq_compile_pattern_match(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
LABEL *unmatched,
bool in_single_pattern,
bool in_alt_pattern,
int base_index,
bool use_deconstructed_cache);
6529static int iseq_compile_pattern_constant(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
LABEL *match_failed,
bool in_single_pattern,
int base_index);
6530static int iseq_compile_array_deconstruct(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
LABEL *deconstruct,
LABEL *deconstructed,
LABEL *match_failed,
LABEL *type_error,
bool in_single_pattern,
int base_index,
bool use_deconstructed_cache);
6531static int iseq_compile_pattern_set_general_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
6532static int iseq_compile_pattern_set_length_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
VALUE pattern_length,
int base_index);
6533static int iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
6535#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
6536#define CASE3_BI_OFFSET_ERROR_STRING 1
6537#define CASE3_BI_OFFSET_KEY_ERROR_P 2
6538#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
6539#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
6542iseq_compile_pattern_each(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
LABEL *matched,
LABEL *unmatched,
bool in_single_pattern,
bool in_alt_pattern,
int base_index,
bool use_deconstructed_cache)
6544 const int line = nd_line(node);
6545 const NODE *line_node = node;
6547 switch (nd_type(node)) {
6601 const NODE *args = RNODE_ARYPTN(node)->pre_args;
6602 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
6603 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
6605 const int min_argc = pre_args_num + post_args_num;
6606 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
6607 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
6609 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
6611 match_failed = NEW_LABEL(line);
6612 type_error = NEW_LABEL(line);
6613 deconstruct = NEW_LABEL(line);
6614 deconstructed = NEW_LABEL(line);
6617 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
6618 ADD_INSN(ret, line_node, swap);
6624 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
6626 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
6628 ADD_INSN(ret, line_node, dup);
6629 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6630 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
6631 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
6632 if (in_single_pattern) {
6633 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
6634 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
6635 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
6636 INT2FIX(min_argc), base_index + 1 ));
6638 ADD_INSNL(ret, line_node, branchunless, match_failed);
6640 for (i = 0; i < pre_args_num; i++) {
6641 ADD_INSN(ret, line_node, dup);
6642 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
6643 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
6644 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6645 args = RNODE_LIST(args)->nd_next;
6648 if (RNODE_ARYPTN(node)->rest_arg) {
6649 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
6650 ADD_INSN(ret, line_node, dup);
6651 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
6652 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
6653 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6654 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
6655 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
6656 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
6657 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
6659 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6662 if (post_args_num > 0) {
6663 ADD_INSN(ret, line_node, dup);
6664 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6665 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
6666 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
6667 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
6668 ADD_INSN(ret, line_node, pop);
6673 args = RNODE_ARYPTN(node)->post_args;
6674 for (i = 0; i < post_args_num; i++) {
6675 ADD_INSN(ret, line_node, dup);
6677 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
6678 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6679 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6681 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
6682 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6683 args = RNODE_LIST(args)->nd_next;
6686 ADD_INSN(ret, line_node, pop);
6688 ADD_INSN(ret, line_node, pop);
6690 ADD_INSNL(ret, line_node, jump, matched);
6691 ADD_INSN(ret, line_node, putnil);
6693 ADD_INSN(ret, line_node, putnil);
6696 ADD_LABEL(ret, type_error);
6697 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6699 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
6700 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
6701 ADD_INSN(ret, line_node, pop);
6703 ADD_LABEL(ret, match_failed);
6704 ADD_INSN(ret, line_node, pop);
6706 ADD_INSN(ret, line_node, pop);
6708 ADD_INSNL(ret, line_node, jump, unmatched);
6761 const NODE *args = RNODE_FNDPTN(node)->args;
6762 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
6764 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
6765 match_failed = NEW_LABEL(line);
6766 type_error = NEW_LABEL(line);
6767 deconstruct = NEW_LABEL(line);
6768 deconstructed = NEW_LABEL(line);
6770 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
6772 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
6774 ADD_INSN(ret, line_node, dup);
6775 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6776 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
6777 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
6778 if (in_single_pattern) {
6779 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node, rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)"),
INT2FIX(args_num), base_index + 1 ));
6781 ADD_INSNL(ret, line_node, branchunless, match_failed);
6784 LABEL *while_begin = NEW_LABEL(nd_line(node));
6785 LABEL *next_loop = NEW_LABEL(nd_line(node));
6786 LABEL *find_succeeded = NEW_LABEL(line);
6787 LABEL *find_failed = NEW_LABEL(nd_line(node));
6790 ADD_INSN(ret, line_node, dup);
6791 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6793 ADD_INSN(ret, line_node, dup);
6794 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
6795 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
6797 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
6799 ADD_LABEL(ret, while_begin);
6801 ADD_INSN(ret, line_node, dup);
6802 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
6803 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
6804 ADD_INSNL(ret, line_node, branchunless, find_failed);
6806 for (j = 0; j < args_num; j++) {
6807 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6808 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
6810 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
6811 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6813 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
6815 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
6816 args = RNODE_LIST(args)->nd_next;
6819 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
6820 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6821 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
6822 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
6823 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
6824 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_FNDPTN(node)->pre_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
6826 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
6827 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6828 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
6829 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
6830 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6831 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6832 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
6833 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_FNDPTN(node)->post_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
6835 ADD_INSNL(ret, line_node, jump, find_succeeded);
6837 ADD_LABEL(ret, next_loop);
6838 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
6839 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6840 ADD_INSNL(ret, line_node, jump, while_begin);
6842 ADD_LABEL(ret, find_failed);
6843 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
6844 if (in_single_pattern) {
6845 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6846 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
6847 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
6848 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
6849 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
6851 ADD_INSN1(ret, line_node, putobject,
Qfalse);
6852 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
6854 ADD_INSN(ret, line_node, pop);
6855 ADD_INSN(ret, line_node, pop);
6857 ADD_INSNL(ret, line_node, jump, match_failed);
6858 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
6860 ADD_LABEL(ret, find_succeeded);
6861 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
6864 ADD_INSN(ret, line_node, pop);
6865 ADD_INSNL(ret, line_node, jump, matched);
6866 ADD_INSN(ret, line_node, putnil);
6868 ADD_LABEL(ret, type_error);
6869 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6871 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
6872 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
6873 ADD_INSN(ret, line_node, pop);
6875 ADD_LABEL(ret, match_failed);
6876 ADD_INSN(ret, line_node, pop);
6877 ADD_INSNL(ret, line_node, jump, unmatched);
6941 LABEL *match_failed, *type_error;
6944 match_failed = NEW_LABEL(line);
6945 type_error = NEW_LABEL(line);
6947 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
6948 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
6949 keys = rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
6951 rb_ary_push(keys, RNODE_LIT(RNODE_LIST(kw_args)->nd_head)->nd_lit);
6952 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
6956 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
6958 ADD_INSN(ret, line_node, dup);
6959 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
6960 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
6961 if (in_single_pattern) {
6962 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
6964 ADD_INSNL(ret, line_node, branchunless, match_failed);
6967 ADD_INSN(ret, line_node, putnil);
6970 ADD_INSN1(ret, line_node, duparray, keys);
6973 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
6975 ADD_INSN(ret, line_node, dup);
6977 ADD_INSNL(ret, line_node, branchunless, type_error);
6979 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
6980 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
6983 if (RNODE_HSHPTN(node)->nd_pkwargs) {
6987 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
6989 DECL_ANCHOR(match_values);
6990 INIT_ANCHOR(match_values);
6991 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
6992 for (i = 0; i < keys_num; i++) {
6993 NODE *key_node = RNODE_LIST(args)->nd_head;
6994 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
6997 if (!nd_type_p(key_node, NODE_LIT)) {
6998 UNKNOWN_NODE(
"NODE_IN", key_node, COMPILE_NG);
7000 key = RNODE_LIT(key_node)->nd_lit;
7002 ADD_INSN(ret, line_node, dup);
7003 ADD_INSN1(ret, line_node, putobject, key);
7004 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
7005 if (in_single_pattern) {
7006 LABEL *match_succeeded;
7007 match_succeeded = NEW_LABEL(line);
7009 ADD_INSN(ret, line_node, dup);
7010 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7012 ADD_INSN1(ret, line_node, putobject,
rb_str_freeze(rb_sprintf(
"key not found: %+"PRIsVALUE, key)));
7013 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
7014 ADD_INSN1(ret, line_node, putobject,
Qtrue);
7015 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
7016 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7017 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
7018 ADD_INSN1(ret, line_node, putobject, key);
7019 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
7021 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
7023 ADD_LABEL(ret, match_succeeded);
7025 ADD_INSNL(ret, line_node, branchunless, match_failed);
7027 ADD_INSN(match_values, line_node, dup);
7028 ADD_INSN1(match_values, line_node, putobject, key);
7029 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
7030 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7031 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
7033 ADD_SEQ(ret, match_values);
7037 ADD_INSN(ret, line_node, dup);
7038 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7039 if (in_single_pattern) {
7040 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
7042 ADD_INSNL(ret, line_node, branchunless, match_failed);
7045 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7046 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
7047 ADD_INSN(ret, line_node, dup);
7048 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7049 if (in_single_pattern) {
7050 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
7052 ADD_INSNL(ret, line_node, branchunless, match_failed);
7055 ADD_INSN(ret, line_node, dup);
7056 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7060 ADD_INSN(ret, line_node, pop);
7061 ADD_INSNL(ret, line_node, jump, matched);
7062 ADD_INSN(ret, line_node, putnil);
7064 ADD_LABEL(ret, type_error);
7065 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7067 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
7068 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7069 ADD_INSN(ret, line_node, pop);
7071 ADD_LABEL(ret, match_failed);
7072 ADD_INSN(ret, line_node, pop);
7073 ADD_INSNL(ret, line_node, jump, unmatched);
7100 CHECK(COMPILE(ret,
"case in literal", node));
7101 if (in_single_pattern) {
7102 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7104 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7105 if (in_single_pattern) {
7106 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
7108 ADD_INSNL(ret, line_node, branchif, matched);
7109 ADD_INSNL(ret, line_node, jump, unmatched);
7113 ID id = RNODE_LASGN(node)->nd_vid;
7114 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
7116 if (in_alt_pattern) {
7117 const char *name = rb_id2name(
id);
7118 if (name && strlen(name) > 0 && name[0] !=
'_') {
7119 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7125 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
7126 ADD_INSNL(ret, line_node, jump, matched);
7131 ID id = RNODE_DASGN(node)->nd_vid;
7133 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
7135 if (in_alt_pattern) {
7136 const char *name = rb_id2name(
id);
7137 if (name && strlen(name) > 0 && name[0] !=
'_') {
7138 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7145 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
7149 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
7150 ADD_INSNL(ret, line_node, jump, matched);
7155 LABEL *match_failed;
7156 match_failed = unmatched;
7157 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7158 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
7159 if (in_single_pattern) {
7160 LABEL *match_succeeded;
7161 match_succeeded = NEW_LABEL(line);
7163 ADD_INSN(ret, line_node, dup);
7164 if (nd_type_p(node, NODE_IF)) {
7165 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7168 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
7171 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
7172 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7173 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7174 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7176 ADD_INSN(ret, line_node, pop);
7177 ADD_INSN(ret, line_node, pop);
7179 ADD_LABEL(ret, match_succeeded);
7181 if (nd_type_p(node, NODE_IF)) {
7182 ADD_INSNL(ret, line_node, branchunless, match_failed);
7185 ADD_INSNL(ret, line_node, branchif, match_failed);
7187 ADD_INSNL(ret, line_node, jump, matched);
7192 LABEL *match_failed;
7193 match_failed = NEW_LABEL(line);
7195 n = RNODE_HASH(node)->nd_head;
7196 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
7197 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7201 ADD_INSN(ret, line_node, dup);
7202 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(n)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 , use_deconstructed_cache));
7203 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head, matched, match_failed, in_single_pattern, in_alt_pattern, base_index,
false));
7204 ADD_INSN(ret, line_node, putnil);
7206 ADD_LABEL(ret, match_failed);
7207 ADD_INSN(ret, line_node, pop);
7208 ADD_INSNL(ret, line_node, jump, unmatched);
7212 LABEL *match_succeeded, *fin;
7213 match_succeeded = NEW_LABEL(line);
7214 fin = NEW_LABEL(line);
7216 ADD_INSN(ret, line_node, dup);
7217 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_1st, match_succeeded, fin, in_single_pattern,
true, base_index + 1 , use_deconstructed_cache));
7218 ADD_LABEL(ret, match_succeeded);
7219 ADD_INSN(ret, line_node, pop);
7220 ADD_INSNL(ret, line_node, jump, matched);
7221 ADD_INSN(ret, line_node, putnil);
7222 ADD_LABEL(ret, fin);
7223 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
7227 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
7233iseq_compile_pattern_match(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
LABEL *unmatched,
bool in_single_pattern,
bool in_alt_pattern,
int base_index,
bool use_deconstructed_cache)
7235 LABEL *fin = NEW_LABEL(nd_line(node));
7236 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7237 ADD_LABEL(ret, fin);
7242iseq_compile_pattern_constant(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
LABEL *match_failed,
bool in_single_pattern,
int base_index)
7244 const NODE *line_node = node;
7246 if (RNODE_ARYPTN(node)->nd_pconst) {
7247 ADD_INSN(ret, line_node, dup);
7248 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
7249 if (in_single_pattern) {
7250 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7252 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7253 if (in_single_pattern) {
7254 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
7256 ADD_INSNL(ret, line_node, branchunless, match_failed);
7263iseq_compile_array_deconstruct(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
LABEL *deconstruct,
LABEL *deconstructed,
LABEL *match_failed,
LABEL *type_error,
bool in_single_pattern,
int base_index,
bool use_deconstructed_cache)
7265 const NODE *line_node = node;
7269 if (use_deconstructed_cache) {
7271 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7272 ADD_INSNL(ret, line_node, branchnil, deconstruct);
7275 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7276 ADD_INSNL(ret, line_node, branchunless, match_failed);
7279 ADD_INSN(ret, line_node, pop);
7280 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
7281 ADD_INSNL(ret, line_node, jump, deconstructed);
7284 ADD_INSNL(ret, line_node, jump, deconstruct);
7287 ADD_LABEL(ret, deconstruct);
7288 ADD_INSN(ret, line_node, dup);
7289 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
7290 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7293 if (use_deconstructed_cache) {
7294 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
7297 if (in_single_pattern) {
7298 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
7301 ADD_INSNL(ret, line_node, branchunless, match_failed);
7303 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
7306 if (use_deconstructed_cache) {
7307 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7310 ADD_INSN(ret, line_node, dup);
7312 ADD_INSNL(ret, line_node, branchunless, type_error);
7314 ADD_LABEL(ret, deconstructed);
7330 const int line = nd_line(node);
7331 const NODE *line_node = node;
7332 LABEL *match_succeeded = NEW_LABEL(line);
7334 ADD_INSN(ret, line_node, dup);
7335 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7337 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7338 ADD_INSN1(ret, line_node, putobject, errmsg);
7339 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7340 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7341 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7343 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7344 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7346 ADD_INSN(ret, line_node, pop);
7347 ADD_INSN(ret, line_node, pop);
7348 ADD_LABEL(ret, match_succeeded);
7364 const int line = nd_line(node);
7365 const NODE *line_node = node;
7366 LABEL *match_succeeded = NEW_LABEL(line);
7368 ADD_INSN(ret, line_node, dup);
7369 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7371 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7372 ADD_INSN1(ret, line_node, putobject, errmsg);
7373 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7374 ADD_INSN(ret, line_node, dup);
7375 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7376 ADD_INSN1(ret, line_node, putobject, pattern_length);
7377 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
7378 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7380 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7381 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
7383 ADD_INSN(ret, line_node, pop);
7384 ADD_INSN(ret, line_node, pop);
7385 ADD_LABEL(ret, match_succeeded);
7391iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
7401 const int line = nd_line(node);
7402 const NODE *line_node = node;
7403 LABEL *match_succeeded = NEW_LABEL(line);
7405 ADD_INSN(ret, line_node, dup);
7406 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7408 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7409 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
7410 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7411 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
7412 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
7413 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7415 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7416 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7418 ADD_INSN(ret, line_node, pop);
7419 ADD_INSN(ret, line_node, pop);
7421 ADD_LABEL(ret, match_succeeded);
7422 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7423 ADD_INSN(ret, line_node, pop);
7424 ADD_INSN(ret, line_node, pop);
7432 const NODE *pattern;
7433 const NODE *node = orig_node;
7434 LABEL *endlabel, *elselabel;
7436 DECL_ANCHOR(body_seq);
7437 DECL_ANCHOR(cond_seq);
7439 enum node_type
type;
7440 const NODE *line_node;
7443 bool single_pattern;
7446 INIT_ANCHOR(body_seq);
7447 INIT_ANCHOR(cond_seq);
7449 branches = decl_branch_base(iseq, node,
"case");
7451 node = RNODE_CASE3(node)->nd_body;
7452 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
7453 type = nd_type(node);
7454 line = nd_line(node);
7456 single_pattern = !RNODE_IN(node)->nd_next;
7458 endlabel = NEW_LABEL(line);
7459 elselabel = NEW_LABEL(line);
7461 if (single_pattern) {
7463 ADD_INSN(head, line_node, putnil);
7464 ADD_INSN(head, line_node, putnil);
7465 ADD_INSN1(head, line_node, putobject,
Qfalse);
7466 ADD_INSN(head, line_node, putnil);
7468 ADD_INSN(head, line_node, putnil);
7470 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
7474 while (
type == NODE_IN) {
7478 ADD_INSN(body_seq, line_node, putnil);
7480 l1 = NEW_LABEL(line);
7481 ADD_LABEL(body_seq, l1);
7482 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
7483 add_trace_branch_coverage(
7486 RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node,
7490 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
7491 ADD_INSNL(body_seq, line_node, jump, endlabel);
7493 pattern = RNODE_IN(node)->nd_head;
7495 int pat_line = nd_line(pattern);
7496 LABEL *next_pat = NEW_LABEL(pat_line);
7497 ADD_INSN (cond_seq, pattern, dup);
7499 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
7500 ADD_LABEL(cond_seq, next_pat);
7501 LABEL_UNREMOVABLE(next_pat);
7504 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7508 node = RNODE_IN(node)->nd_next;
7512 type = nd_type(node);
7513 line = nd_line(node);
7518 ADD_LABEL(cond_seq, elselabel);
7519 ADD_INSN(cond_seq, line_node, pop);
7520 ADD_INSN(cond_seq, line_node, pop);
7521 add_trace_branch_coverage(iseq, cond_seq, node, branch_id,
"else", branches);
7522 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7523 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7524 ADD_INSN(cond_seq, line_node, putnil);
7526 ADD_INSN(cond_seq, line_node, putnil);
7530 debugs(
"== else (implicit)\n");
7531 ADD_LABEL(cond_seq, elselabel);
7532 add_trace_branch_coverage(iseq, cond_seq, orig_node, branch_id,
"else", branches);
7533 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7535 if (single_pattern) {
7543 LABEL *key_error, *fin;
7546 key_error = NEW_LABEL(line);
7547 fin = NEW_LABEL(line);
7550 kw_arg->references = 0;
7551 kw_arg->keyword_len = 2;
7552 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
7553 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
7555 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
7556 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
7558 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7559 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
7560 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
7561 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
7562 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
7563 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
7564 ADD_INSNL(cond_seq, orig_node, jump, fin);
7566 ADD_LABEL(cond_seq, key_error);
7568 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7569 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
7570 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
7571 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
7572 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
7573 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
7574 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
7575 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
7576 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
7578 ADD_LABEL(cond_seq, fin);
7582 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
7583 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
7585 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
7587 ADD_INSN(cond_seq, orig_node, putnil);
7589 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7590 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
7592 ADD_INSN(cond_seq, line_node, putnil);
7596 ADD_SEQ(ret, cond_seq);
7597 ADD_SEQ(ret, body_seq);
7598 ADD_LABEL(ret, endlabel);
7602#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
7603#undef CASE3_BI_OFFSET_ERROR_STRING
7604#undef CASE3_BI_OFFSET_KEY_ERROR_P
7605#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
7606#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
7611 const int line = (int)nd_line(node);
7612 const NODE *line_node = node;
7614 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
7615 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
7616 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
7617 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
7622 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
7623 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
7624 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
7625 LABEL *end_label = NEW_LABEL(line);
7626 LABEL *adjust_label = NEW_LABEL(line);
7628 LABEL *next_catch_label = NEW_LABEL(line);
7629 LABEL *tmp_label = NULL;
7631 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
7632 push_ensure_entry(iseq, &enl, NULL, NULL);
7634 if (RNODE_WHILE(node)->nd_state == 1) {
7635 ADD_INSNL(ret, line_node, jump, next_label);
7638 tmp_label = NEW_LABEL(line);
7639 ADD_INSNL(ret, line_node, jump, tmp_label);
7641 ADD_LABEL(ret, adjust_label);
7642 ADD_INSN(ret, line_node, putnil);
7643 ADD_LABEL(ret, next_catch_label);
7644 ADD_INSN(ret, line_node, pop);
7645 ADD_INSNL(ret, line_node, jump, next_label);
7646 if (tmp_label) ADD_LABEL(ret, tmp_label);
7648 ADD_LABEL(ret, redo_label);
7649 branches = decl_branch_base(iseq, node,
type == NODE_WHILE ?
"while" :
"until");
7650 add_trace_branch_coverage(
7653 RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node,
7657 CHECK(COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body));
7658 ADD_LABEL(ret, next_label);
7660 if (
type == NODE_WHILE) {
7661 compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
7662 redo_label, end_label);
7666 compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
7667 end_label, redo_label);
7670 ADD_LABEL(ret, end_label);
7671 ADD_ADJUST_RESTORE(ret, adjust_label);
7673 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
7675 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
7679 ADD_INSN(ret, line_node, putnil);
7682 ADD_LABEL(ret, break_label);
7685 ADD_INSN(ret, line_node, pop);
7688 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
7690 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
7692 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
7693 ISEQ_COMPILE_DATA(iseq)->redo_label);
7695 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
7696 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
7697 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
7698 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
7699 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
7706 const int line = nd_line(node);
7707 const NODE *line_node = node;
7708 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
7709 LABEL *retry_label = NEW_LABEL(line);
7710 LABEL *retry_end_l = NEW_LABEL(line);
7713 ADD_LABEL(ret, retry_label);
7714 if (nd_type_p(node, NODE_FOR)) {
7715 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
7717 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
7718 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
7719 ISEQ_TYPE_BLOCK, line);
7720 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
7723 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
7724 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
7725 ISEQ_TYPE_BLOCK, line);
7726 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
7740 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
7741 while (INSN_OF(iobj) != BIN(send) && INSN_OF(iobj) != BIN(invokesuper)) {
7742 iobj = (
INSN*) get_prev_insn(iobj);
7744 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
7748 if (&iobj->link == LAST_ELEMENT(ret)) {
7754 ADD_INSN(ret, line_node, pop);
7757 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
7759 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
7769 const NODE *line_node = node;
7770 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
7771 LABEL *not_single = NEW_LABEL(nd_line(var));
7772 LABEL *not_ary = NEW_LABEL(nd_line(var));
7773 CHECK(COMPILE(ret,
"for var", var));
7774 ADD_INSN(ret, line_node, dup);
7775 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
7776 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7777 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
7778 ADD_INSNL(ret, line_node, branchunless, not_single);
7779 ADD_INSN(ret, line_node, dup);
7780 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7781 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
7782 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
7783 ADD_INSN(ret, line_node, swap);
7784 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
7785 ADD_INSN(ret, line_node, dup);
7786 ADD_INSNL(ret, line_node, branchunless, not_ary);
7787 ADD_INSN(ret, line_node, swap);
7788 ADD_LABEL(ret, not_ary);
7789 ADD_INSN(ret, line_node, pop);
7790 ADD_LABEL(ret, not_single);
7797 const NODE *line_node = node;
7798 unsigned long throw_flag = 0;
7800 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7802 LABEL *splabel = NEW_LABEL(0);
7803 ADD_LABEL(ret, splabel);
7804 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
7805 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
7806 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
7807 add_ensure_iseq(ret, iseq, 0);
7808 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7809 ADD_ADJUST_RESTORE(ret, splabel);
7812 ADD_INSN(ret, line_node, putnil);
7819 if (!ISEQ_COMPILE_DATA(ip)) {
7824 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7825 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7827 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7830 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7831 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
7835 ip = ISEQ_BODY(ip)->parent_iseq;
7840 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
7841 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
7843 ADD_INSN(ret, line_node, pop);
7847 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
7856 const NODE *line_node = node;
7857 unsigned long throw_flag = 0;
7859 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7860 LABEL *splabel = NEW_LABEL(0);
7861 debugs(
"next in while loop\n");
7862 ADD_LABEL(ret, splabel);
7863 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
7864 add_ensure_iseq(ret, iseq, 0);
7865 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
7866 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7867 ADD_ADJUST_RESTORE(ret, splabel);
7869 ADD_INSN(ret, line_node, putnil);
7872 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
7873 LABEL *splabel = NEW_LABEL(0);
7874 debugs(
"next in block\n");
7875 ADD_LABEL(ret, splabel);
7876 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
7877 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
7878 add_ensure_iseq(ret, iseq, 0);
7879 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7880 ADD_ADJUST_RESTORE(ret, splabel);
7881 splabel->unremovable = FALSE;
7884 ADD_INSN(ret, line_node, putnil);
7891 if (!ISEQ_COMPILE_DATA(ip)) {
7896 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7897 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7901 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7904 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7905 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
7909 ip = ISEQ_BODY(ip)->parent_iseq;
7912 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
7913 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
7916 ADD_INSN(ret, line_node, pop);
7920 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
7930 const NODE *line_node = node;
7932 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
7933 LABEL *splabel = NEW_LABEL(0);
7934 debugs(
"redo in while");
7935 ADD_LABEL(ret, splabel);
7936 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
7937 add_ensure_iseq(ret, iseq, 0);
7938 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
7939 ADD_ADJUST_RESTORE(ret, splabel);
7941 ADD_INSN(ret, line_node, putnil);
7944 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
7945 LABEL *splabel = NEW_LABEL(0);
7947 debugs(
"redo in block");
7948 ADD_LABEL(ret, splabel);
7949 add_ensure_iseq(ret, iseq, 0);
7950 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
7951 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7952 ADD_ADJUST_RESTORE(ret, splabel);
7955 ADD_INSN(ret, line_node, putnil);
7962 if (!ISEQ_COMPILE_DATA(ip)) {
7967 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7970 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7973 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7974 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
7978 ip = ISEQ_BODY(ip)->parent_iseq;
7981 ADD_INSN(ret, line_node, putnil);
7982 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
7985 ADD_INSN(ret, line_node, pop);
7989 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
7999 const NODE *line_node = node;
8001 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
8002 ADD_INSN(ret, line_node, putnil);
8003 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
8006 ADD_INSN(ret, line_node, pop);
8010 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
8019 const int line = nd_line(node);
8020 const NODE *line_node = node;
8021 LABEL *lstart = NEW_LABEL(line);
8022 LABEL *lend = NEW_LABEL(line);
8023 LABEL *lcont = NEW_LABEL(line);
8024 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
8026 ISEQ_BODY(iseq)->location.label),
8027 ISEQ_TYPE_RESCUE, line);
8029 lstart->rescued = LABEL_RESCUE_BEG;
8030 lend->rescued = LABEL_RESCUE_END;
8031 ADD_LABEL(ret, lstart);
8033 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
8034 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
8036 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
8038 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
8040 ADD_LABEL(ret, lend);
8041 if (RNODE_RESCUE(node)->nd_else) {
8042 ADD_INSN(ret, line_node, pop);
8043 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
8045 ADD_INSN(ret, line_node, nop);
8046 ADD_LABEL(ret, lcont);
8049 ADD_INSN(ret, line_node, pop);
8053 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
8054 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
8061 const int line = nd_line(node);
8062 const NODE *line_node = node;
8063 const NODE *resq = node;
8065 LABEL *label_miss, *label_hit;
8068 label_miss = NEW_LABEL(line);
8069 label_hit = NEW_LABEL(line);
8071 narg = RNODE_RESBODY(resq)->nd_args;
8073 switch (nd_type(narg)) {
8076 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8077 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
8078 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8079 ADD_INSNL(ret, line_node, branchif, label_hit);
8080 narg = RNODE_LIST(narg)->nd_next;
8086 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8087 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
8088 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
8089 ADD_INSNL(ret, line_node, branchif, label_hit);
8092 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
8096 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8098 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8099 ADD_INSNL(ret, line_node, branchif, label_hit);
8101 ADD_INSNL(ret, line_node, jump, label_miss);
8102 ADD_LABEL(ret, label_hit);
8105 if (nd_type(RNODE_RESBODY(resq)->nd_body) == NODE_BEGIN && RNODE_BEGIN(RNODE_RESBODY(resq)->nd_body)->nd_body == NULL) {
8107 int lineno = nd_line(RNODE_RESBODY(resq)->nd_body);
8108 NODE dummy_line_node = generate_dummy_line_node(lineno, -1);
8109 ADD_INSN(ret, &dummy_line_node, putnil);
8112 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
8115 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
8116 ADD_INSN(ret, line_node, nop);
8118 ADD_INSN(ret, line_node, leave);
8119 ADD_LABEL(ret, label_miss);
8120 resq = RNODE_RESBODY(resq)->nd_head;
8128 const int line = nd_line(node);
8129 const NODE *line_node = node;
8131 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
8133 ISEQ_TYPE_ENSURE, line);
8134 LABEL *lstart = NEW_LABEL(line);
8135 LABEL *lend = NEW_LABEL(line);
8136 LABEL *lcont = NEW_LABEL(line);
8144 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
8146 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
8151 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
8153 ADD_LABEL(ret, lstart);
8154 CHECK(COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
8155 ADD_LABEL(ret, lend);
8157 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
8158 ADD_LABEL(ret, lcont);
8159 if (last_leave) ADD_INSN(ret, line_node, pop);
8161 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
8162 if (lstart->link.next != &lend->link) {
8164 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
8166 erange = erange->next;
8170 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
8177 const NODE *line_node = node;
8180 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8182 enum rb_iseq_type t =
type;
8183 const NODE *retval = RNODE_RETURN(node)->nd_stts;
8186 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
8187 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
8188 t = ISEQ_BODY(is)->type;
8192 case ISEQ_TYPE_MAIN:
8194 rb_warn(
"argument of top-level return is ignored");
8198 type = ISEQ_TYPE_METHOD;
8205 if (
type == ISEQ_TYPE_METHOD) {
8206 splabel = NEW_LABEL(0);
8207 ADD_LABEL(ret, splabel);
8208 ADD_ADJUST(ret, line_node, 0);
8211 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
8213 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8214 add_ensure_iseq(ret, iseq, 1);
8216 ADD_INSN(ret, line_node, leave);
8217 ADD_ADJUST_RESTORE(ret, splabel);
8220 ADD_INSN(ret, line_node, putnil);
8224 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
8226 ADD_INSN(ret, line_node, pop);
8236 CHECK(COMPILE_(ret,
"nd_body", node, popped));
8238 if (!popped && !all_string_result_p(node)) {
8239 const NODE *line_node = node;
8240 const unsigned int flag = VM_CALL_FCALL;
8244 ADD_INSN(ret, line_node, dup);
8245 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
8246 ADD_INSN(ret, line_node, anytostring);
8254 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8256 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
8257 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8263 LABEL *else_label = NEW_LABEL(nd_line(line_node));
8266 br = decl_branch_base(iseq, node,
"&.");
8268 ADD_INSN(recv, line_node, dup);
8269 ADD_INSNL(recv, line_node, branchnil, else_label);
8270 add_trace_branch_coverage(iseq, recv, node, 0,
"then", br);
8278 if (!else_label)
return;
8279 end_label = NEW_LABEL(nd_line(line_node));
8280 ADD_INSNL(ret, line_node, jump, end_label);
8281 ADD_LABEL(ret, else_label);
8282 add_trace_branch_coverage(iseq, ret, node, 1,
"else", branches);
8283 ADD_LABEL(ret, end_label);
8292 if (get_nd_recv(node) && nd_type_p(get_nd_recv(node), NODE_STR) &&
8293 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
8294 get_nd_args(node) == NULL &&
8295 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8296 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8297 VALUE str = rb_fstring(RNODE_STR(get_nd_recv(node))->nd_lit);
8298 if (get_node_call_nd_mid(node) == idUMinus) {
8299 ADD_INSN2(ret, line_node, opt_str_uminus, str,
8300 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
8303 ADD_INSN2(ret, line_node, opt_str_freeze, str,
8304 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
8308 ADD_INSN(ret, line_node, pop);
8315 if (get_node_call_nd_mid(node) == idAREF && !private_recv_p(node) && get_nd_args(node) &&
8316 nd_type_p(get_nd_args(node), NODE_LIST) && RNODE_LIST(get_nd_args(node))->as.nd_alen == 1 &&
8317 nd_type_p(RNODE_LIST(get_nd_args(node))->nd_head, NODE_STR) &&
8318 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8319 !ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
8320 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8321 VALUE str = rb_fstring(RNODE_STR(RNODE_LIST(get_nd_args(node))->nd_head)->nd_lit);
8322 CHECK(COMPILE(ret,
"recv", get_nd_recv(node)));
8323 ADD_INSN2(ret, line_node, opt_aref_with, str,
8324 new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
8327 ADD_INSN(ret, line_node, pop);
8335iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
8337 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
8341iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
8344 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
8345 for (i=0; table[i].index != -1; i++) {
8346 if (strcmp(table[i].name, name) == 0) {
8354iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
8356 const char *name = rb_id2name(mid);
8357 static const char prefix[] =
"__builtin_";
8358 const size_t prefix_len =
sizeof(prefix) - 1;
8363 switch (nd_type(recv)) {
8365 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
8370 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
8380 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
8381 return &name[prefix_len];
8390delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
8397 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
8398 unsigned int start=0;
8403 argc + start <= ISEQ_BODY(iseq)->local_table_size;
8407 for (
unsigned int i=start; i-start<argc; i++) {
8408 if (IS_INSN(elem) &&
8409 INSN_OF(elem) == BIN(getlocal)) {
8410 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
8411 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
8413 if (local_level == 0) {
8414 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
8416 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
8417 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
8418 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
8419 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
8443 *pstart_index = start;
8457 if (!node)
goto no_arg;
8459 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
8460 const NODE *next = RNODE_LIST(node)->nd_next;
8462 node = RNODE_LIST(node)->nd_head;
8463 if (!node)
goto no_arg;
8464 if (!nd_type_p(node, NODE_LIT))
goto bad_arg;
8466 symbol = RNODE_LIT(node)->nd_lit;
8467 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
8470 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
8471 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
8473 else if (strcmp(RSTRING_PTR(
string),
"no_gc") == 0) {
8474 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_NO_GC;
8483 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
8486 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
8489 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %s", RSTRING_PTR(
string));
8492 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
8498 if (!node)
goto no_arg;
8499 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
8500 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
8501 node = RNODE_LIST(node)->nd_head;
8502 if (!node)
goto no_arg;
8503 if (!nd_type_p(node, NODE_LIT))
goto bad_arg;
8504 VALUE name = RNODE_LIT(node)->nd_lit;
8505 if (!
SYMBOL_P(name))
goto non_symbol_arg;
8507 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
8511 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
8514 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
8517 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
8518 rb_builtin_class_name(name));
8521 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
8527 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
8528 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
8529 return RNODE_IF(node)->nd_body;
8532 rb_bug(
"mandatory_node: can't find mandatory node");
8537compile_builtin_mandatory_only_method(
rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
8541 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
8544 rb_node_init(RNODE(&args_node), NODE_ARGS);
8545 args_node.nd_ainfo = args;
8548 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
8549 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
8553 tbl->size = table_size;
8558 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
8559 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
8562 for (; i<table_size; i++) {
8563 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
8567 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
8568 scope_node.nd_tbl = tbl;
8569 scope_node.nd_body = mandatory_node(iseq, node);
8570 scope_node.nd_args = &args_node;
8573 .root = RNODE(&scope_node),
8574 .frozen_string_literal = -1,
8575 .coverage_enabled = -1,
8576 .script_lines = ISEQ_BODY(iseq)->variable.script_lines,
8579 ISEQ_BODY(iseq)->mandatory_only_iseq =
8580 rb_iseq_new_with_opt(&ast, rb_iseq_base_label(iseq),
8581 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
8582 nd_line(line_node), NULL, 0,
8583 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option);
8593 NODE *args_node = get_nd_args(node);
8595 if (parent_block != NULL) {
8596 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
8600# define BUILTIN_INLINE_PREFIX "_bi"
8601 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
8602 bool cconst =
false;
8607 if (strcmp(
"cstmt!", builtin_func) == 0 ||
8608 strcmp(
"cexpr!", builtin_func) == 0) {
8611 else if (strcmp(
"cconst!", builtin_func) == 0) {
8614 else if (strcmp(
"cinit!", builtin_func) == 0) {
8616 GET_VM()->builtin_inline_index++;
8619 else if (strcmp(
"attr!", builtin_func) == 0) {
8620 return compile_builtin_attr(iseq, args_node);
8622 else if (strcmp(
"arg!", builtin_func) == 0) {
8623 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
8625 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
8627 rb_bug(
"mandatory_only? should be in if condition");
8629 else if (!LIST_INSN_SIZE_ZERO(ret)) {
8630 rb_bug(
"mandatory_only? should be put on top");
8633 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8634 return compile_builtin_mandatory_only_method(iseq, node, line_node);
8637 rb_bug(
"can't find builtin function:%s", builtin_func);
8640 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
8644 if (GET_VM()->builtin_inline_index == INT_MAX) {
8645 rb_bug(
"builtin inline function index overflow:%s", builtin_func);
8647 int inline_index = GET_VM()->builtin_inline_index++;
8648 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
8649 builtin_func = inline_func;
8655 typedef VALUE(*builtin_func0)(
void *,
VALUE);
8656 VALUE const_val = (*(builtin_func0)bf->func_ptr)(NULL,
Qnil);
8657 ADD_INSN1(ret, line_node, putobject, const_val);
8663 unsigned int flag = 0;
8665 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
8667 if (
FIX2INT(argc) != bf->argc) {
8668 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
8669 builtin_func, bf->argc,
FIX2INT(argc));
8673 unsigned int start_index;
8674 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
8675 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
8679 ADD_INSN1(ret, line_node, invokebuiltin, bf);
8682 if (popped) ADD_INSN(ret, line_node, pop);
8688compile_call(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
const enum node_type
type,
const NODE *
const line_node,
int popped,
bool assume_receiver)
8696 ID mid = get_node_call_nd_mid(node);
8698 unsigned int flag = 0;
8700 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8701 LABEL *else_label = NULL;
8704 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
8709 if (nd_type_p(node, NODE_VCALL)) {
8714 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
8716 if (mid == id_bitblt) {
8717 ADD_INSN(ret, line_node, bitblt);
8720 else if (mid == id_answer) {
8721 ADD_INSN(ret, line_node, answer);
8733 if (nd_type_p(node, NODE_FCALL) &&
8734 (mid == goto_id || mid == label_id)) {
8737 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
8740 if (!labels_table) {
8741 labels_table = st_init_numtable();
8742 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
8744 if (nd_type_p(node->nd_args->nd_head, NODE_LIT) &&
8745 SYMBOL_P(node->nd_args->nd_head->nd_lit)) {
8747 label_name = node->nd_args->nd_head->nd_lit;
8748 if (!st_lookup(labels_table, (st_data_t)label_name, &data)) {
8749 label = NEW_LABEL(nd_line(line_node));
8750 label->position = nd_line(line_node);
8751 st_insert(labels_table, (st_data_t)label_name, (st_data_t)label);
8754 label = (
LABEL *)data;
8758 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
8762 if (mid == goto_id) {
8763 ADD_INSNL(ret, line_node, jump, label);
8766 ADD_LABEL(ret, label);
8773 const char *builtin_func;
8774 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
8775 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
8776 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
8780 if (!assume_receiver) {
8781 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
8784 if (mid == idCall &&
8785 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
8786 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
8787 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
8789 else if (private_recv_p(node)) {
8790 ADD_INSN(recv, node, putself);
8791 flag |= VM_CALL_FCALL;
8794 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
8797 if (
type == NODE_QCALL) {
8798 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
8801 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
8802 ADD_CALL_RECEIVER(recv, line_node);
8807 if (
type != NODE_VCALL) {
8808 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
8809 CHECK(!
NIL_P(argc));
8818 debugp_param(
"call args argc", argc);
8819 debugp_param(
"call method",
ID2SYM(mid));
8821 switch ((
int)
type) {
8823 flag |= VM_CALL_VCALL;
8826 flag |= VM_CALL_FCALL;
8829 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
8830 ADD_INSN(ret, line_node, splatkw);
8832 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
8834 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
8836 ADD_INSN(ret, line_node, pop);
8844 const int line = nd_line(node);
8846 unsigned int flag = 0;
8848 ID id = RNODE_OP_ASGN1(node)->nd_mid;
8850 int keyword_len = 0;
8877 ADD_INSN(ret, node, putnil);
8879 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
8880 CHECK(asgnflag != -1);
8881 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
8885 case NODE_BLOCK_PASS:
8889 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, &keywords);
8890 if (flag & VM_CALL_KW_SPLAT) {
8892 ADD_INSN(ret, node, splatkw);
8896 ADD_INSN(ret, node, dup);
8897 ADD_INSN(ret, node, splatkw);
8898 ADD_INSN(ret, node, pop);
8901 CHECK(!
NIL_P(argc));
8903 int dup_argn =
FIX2INT(argc) + 1 + boff;
8905 keyword_len = keywords->keyword_len;
8906 dup_argn += keyword_len;
8908 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
8910 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_KW_SPLAT_MUT), keywords);
8912 if (
id == idOROP ||
id == idANDOP) {
8921 LABEL *label = NEW_LABEL(line);
8922 LABEL *lfin = NEW_LABEL(line);
8924 ADD_INSN(ret, node, dup);
8926 ADD_INSNL(ret, node, branchif, label);
8929 ADD_INSNL(ret, node, branchunless, label);
8931 ADD_INSN(ret, node, pop);
8933 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
8935 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
8937 if (flag & VM_CALL_ARGS_SPLAT) {
8938 if (flag & VM_CALL_KW_SPLAT) {
8939 ADD_INSN1(ret, node, topn,
INT2FIX(2 + boff));
8940 ADD_INSN(ret, node, swap);
8941 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
8942 ADD_INSN(ret, node, concatarray);
8943 ADD_INSN1(ret, node, setn,
INT2FIX(2 + boff));
8944 ADD_INSN(ret, node, pop);
8947 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
8949 ADD_INSN1(ret, node, dupn,
INT2FIX(3));
8950 ADD_INSN(ret, node, swap);
8951 ADD_INSN(ret, node, pop);
8953 ADD_INSN(ret, node, concatarray);
8955 ADD_INSN1(ret, node, setn,
INT2FIX(3));
8956 ADD_INSN(ret, node, pop);
8957 ADD_INSN(ret, node, pop);
8960 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), keywords);
8962 else if (flag & VM_CALL_KW_SPLAT) {
8964 ADD_INSN1(ret, node, topn,
INT2FIX(2));
8965 ADD_INSN(ret, node, swap);
8966 ADD_INSN1(ret, node, setn,
INT2FIX(3));
8967 ADD_INSN(ret, node, pop);
8969 ADD_INSN(ret, node, swap);
8970 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), keywords);
8972 else if (keyword_len) {
8973 ADD_INSN1(ret, node, opt_reverse,
INT2FIX(keyword_len+boff+1));
8974 ADD_INSN1(ret, node, opt_reverse,
INT2FIX(keyword_len+boff+0));
8975 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), keywords);
8979 ADD_INSN(ret, node, swap);
8980 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), keywords);
8982 ADD_INSN(ret, node, pop);
8983 ADD_INSNL(ret, node, jump, lfin);
8984 ADD_LABEL(ret, label);
8986 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
8988 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
8989 ADD_LABEL(ret, lfin);
8992 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
8993 ADD_SEND(ret, node,
id,
INT2FIX(1));
8995 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
8997 if (flag & VM_CALL_ARGS_SPLAT) {
8998 if (flag & VM_CALL_KW_SPLAT) {
8999 ADD_INSN1(ret, node, topn,
INT2FIX(2 + boff));
9000 ADD_INSN(ret, node, swap);
9001 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
9002 ADD_INSN(ret, node, concatarray);
9003 ADD_INSN1(ret, node, setn,
INT2FIX(2 + boff));
9004 ADD_INSN(ret, node, pop);
9007 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
9009 ADD_INSN1(ret, node, dupn,
INT2FIX(3));
9010 ADD_INSN(ret, node, swap);
9011 ADD_INSN(ret, node, pop);
9013 ADD_INSN(ret, node, concatarray);
9015 ADD_INSN1(ret, node, setn,
INT2FIX(3));
9016 ADD_INSN(ret, node, pop);
9017 ADD_INSN(ret, node, pop);
9020 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), keywords);
9022 else if (flag & VM_CALL_KW_SPLAT) {
9024 ADD_INSN1(ret, node, topn,
INT2FIX(2));
9025 ADD_INSN(ret, node, swap);
9026 ADD_INSN1(ret, node, setn,
INT2FIX(3));
9027 ADD_INSN(ret, node, pop);
9029 ADD_INSN(ret, node, swap);
9030 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), keywords);
9032 else if (keyword_len) {
9033 ADD_INSN(ret, node, dup);
9034 ADD_INSN1(ret, node, opt_reverse,
INT2FIX(keyword_len+boff+2));
9035 ADD_INSN1(ret, node, opt_reverse,
INT2FIX(keyword_len+boff+1));
9036 ADD_INSN(ret, node, pop);
9037 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), keywords);
9041 ADD_INSN(ret, node, swap);
9042 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), keywords);
9044 ADD_INSN(ret, node, pop);
9052 const int line = nd_line(node);
9053 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
9054 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
9056 LABEL *lfin = NEW_LABEL(line);
9057 LABEL *lcfin = NEW_LABEL(line);
9112 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
9113 CHECK(asgnflag != -1);
9114 if (RNODE_OP_ASGN2(node)->nd_aid) {
9115 lskip = NEW_LABEL(line);
9116 ADD_INSN(ret, node, dup);
9117 ADD_INSNL(ret, node, branchnil, lskip);
9119 ADD_INSN(ret, node, dup);
9120 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
9122 if (atype == idOROP || atype == idANDOP) {
9124 ADD_INSN(ret, node, dup);
9126 if (atype == idOROP) {
9127 ADD_INSNL(ret, node, branchif, lcfin);
9130 ADD_INSNL(ret, node, branchunless, lcfin);
9133 ADD_INSN(ret, node, pop);
9135 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9137 ADD_INSN(ret, node, swap);
9138 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9140 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9141 ADD_INSNL(ret, node, jump, lfin);
9143 ADD_LABEL(ret, lcfin);
9145 ADD_INSN(ret, node, swap);
9148 ADD_LABEL(ret, lfin);
9151 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9152 ADD_SEND(ret, node, atype,
INT2FIX(1));
9154 ADD_INSN(ret, node, swap);
9155 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9157 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9159 if (lskip && popped) {
9160 ADD_LABEL(ret, lskip);
9162 ADD_INSN(ret, node, pop);
9163 if (lskip && !popped) {
9164 ADD_LABEL(ret, lskip);
9172 const int line = nd_line(node);
9177 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
9179 ADD_INSN1(ret, node, putobject, rb_cObject);
9182 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
9185 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
9186 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
9189 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
9191 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
9192 lassign = NEW_LABEL(line);
9193 ADD_INSN(ret, node, dup);
9194 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
9196 ADD_INSNL(ret, node, branchunless, lassign);
9198 ADD_INSN(ret, node, dup);
9199 ADD_INSN1(ret, node, putobject,
Qtrue);
9200 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
9202 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
9203 lfin = NEW_LABEL(line);
9204 if (!popped) ADD_INSN(ret, node, dup);
9205 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
9206 ADD_INSNL(ret, node, branchif, lfin);
9208 ADD_INSNL(ret, node, branchunless, lfin);
9210 if (!popped) ADD_INSN(ret, node, pop);
9211 if (lassign) ADD_LABEL(ret, lassign);
9212 CHECK(COMPILE(ret,
"NODE_OP_CDECL#nd_value", RNODE_OP_CDECL(node)->nd_value));
9215 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9217 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
9218 ADD_INSN(ret, node, swap);
9220 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9221 ADD_LABEL(ret, lfin);
9222 if (!popped) ADD_INSN(ret, node, swap);
9223 ADD_INSN(ret, node, pop);
9226 CHECK(COMPILE(ret,
"NODE_OP_CDECL#nd_value", RNODE_OP_CDECL(node)->nd_value));
9228 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
9230 ADD_INSN(ret, node, swap);
9232 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9233 ADD_INSN(ret, node, swap);
9235 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9243 const int line = nd_line(node);
9244 LABEL *lfin = NEW_LABEL(line);
9247 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
9251 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse);
9252 lassign = lfinish[1];
9254 lassign = NEW_LABEL(line);
9256 ADD_INSNL(ret, node, branchunless, lassign);
9259 lassign = NEW_LABEL(line);
9262 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
9265 ADD_INSN(ret, node, dup);
9268 if (
type == NODE_OP_ASGN_AND) {
9269 ADD_INSNL(ret, node, branchunless, lfin);
9272 ADD_INSNL(ret, node, branchif, lfin);
9276 ADD_INSN(ret, node, pop);
9279 ADD_LABEL(ret, lassign);
9280 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
9281 ADD_LABEL(ret, lfin);
9291 unsigned int flag = 0;
9293 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9296 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9297 if (
type == NODE_SUPER) {
9298 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
9299 CHECK(!
NIL_P(vargc));
9301 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9302 ADD_INSN(args, node, splatkw);
9308 const rb_iseq_t *liseq = body->local_iseq;
9310 const struct rb_iseq_param_keyword *
const local_kwd = local_body->
param.keyword;
9311 int lvar_level = get_lvar_level(iseq);
9313 argc = local_body->
param.lead_num;
9316 for (i = 0; i < local_body->
param.lead_num; i++) {
9317 int idx = local_body->local_table_size - i;
9318 ADD_GETLOCAL(args, node, idx, lvar_level);
9321 if (local_body->
param.flags.has_opt) {
9324 for (j = 0; j < local_body->
param.opt_num; j++) {
9325 int idx = local_body->local_table_size - (i + j);
9326 ADD_GETLOCAL(args, node, idx, lvar_level);
9331 if (local_body->
param.flags.has_rest) {
9333 int idx = local_body->local_table_size - local_body->
param.rest_start;
9334 ADD_GETLOCAL(args, node, idx, lvar_level);
9335 ADD_INSN1(args, node, splatarray,
Qfalse);
9337 argc = local_body->
param.rest_start + 1;
9338 flag |= VM_CALL_ARGS_SPLAT;
9340 if (local_body->
param.flags.has_post) {
9342 int post_len = local_body->
param.post_num;
9343 int post_start = local_body->
param.post_start;
9345 if (local_body->
param.flags.has_rest) {
9347 for (j=0; j<post_len; j++) {
9348 int idx = local_body->local_table_size - (post_start + j);
9349 ADD_GETLOCAL(args, node, idx, lvar_level);
9351 ADD_INSN1(args, node, newarray,
INT2FIX(j));
9352 ADD_INSN (args, node, concatarray);
9357 for (j=0; j<post_len; j++) {
9358 int idx = local_body->local_table_size - (post_start + j);
9359 ADD_GETLOCAL(args, node, idx, lvar_level);
9361 argc = post_len + post_start;
9365 if (local_body->
param.flags.has_kw) {
9366 int local_size = local_body->local_table_size;
9369 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9371 if (local_body->
param.flags.has_kwrest) {
9372 int idx = local_body->local_table_size - local_kwd->rest_start;
9373 ADD_GETLOCAL(args, node, idx, lvar_level);
9374 if (local_kwd->num > 0) {
9375 ADD_SEND (args, node, rb_intern(
"dup"),
INT2FIX(0));
9376 flag |= VM_CALL_KW_SPLAT_MUT;
9380 ADD_INSN1(args, node, newhash,
INT2FIX(0));
9381 flag |= VM_CALL_KW_SPLAT_MUT;
9383 for (i = 0; i < local_kwd->num; ++i) {
9384 ID id = local_kwd->table[i];
9385 int idx = local_size - get_local_var_idx(liseq,
id);
9386 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
9387 ADD_GETLOCAL(args, node, idx, lvar_level);
9389 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
9390 flag |= VM_CALL_KW_SPLAT;
9392 else if (local_body->
param.flags.has_kwrest) {
9393 int idx = local_body->local_table_size - local_kwd->rest_start;
9394 ADD_GETLOCAL(args, node, idx, lvar_level);
9396 flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
9400 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
9401 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
9402 ADD_INSN(ret, node, putself);
9404 ADD_INSN2(ret, node, invokesuper,
9405 new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL),
9409 ADD_INSN(ret, node, pop);
9419 unsigned int flag = 0;
9424 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
9426 case ISEQ_TYPE_MAIN:
9427 case ISEQ_TYPE_CLASS:
9428 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
9433 if (RNODE_YIELD(node)->nd_head) {
9434 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
9435 CHECK(!
NIL_P(argc));
9442 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
9445 ADD_INSN(ret, node, pop);
9450 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
9451 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
9453 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
9466 switch ((
int)
type) {
9468 ADD_INSN1(recv, node, putobject, RNODE_MATCH(node)->nd_lit);
9469 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
9473 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
9474 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
9477 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
9478 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
9484 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
9486 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
9487 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
9491 ADD_INSN(ret, node, pop);
9502 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
9503 (segments = collect_const_segments(iseq, node))) {
9504 ISEQ_BODY(iseq)->ic_size++;
9505 ADD_INSN1(ret, node, opt_getconstant_path, segments);
9515 CHECK(compile_const_prefix(iseq, node, pref, body));
9516 if (LIST_INSN_SIZE_ZERO(pref)) {
9517 ADD_INSN(ret, node, putnil);
9528 ADD_CALL_RECEIVER(ret, node);
9529 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
9530 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
9533 ADD_INSN(ret, node, pop);
9541 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
9544 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
9545 ISEQ_BODY(iseq)->ic_size++;
9546 VALUE segments = rb_ary_new_from_args(2,
ID2SYM(idNULL),
ID2SYM(RNODE_COLON3(node)->nd_mid));
9547 ADD_INSN1(ret, node, opt_getconstant_path, segments);
9551 ADD_INSN1(ret, node, putobject, rb_cObject);
9552 ADD_INSN1(ret, node, putobject,
Qtrue);
9553 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
9557 ADD_INSN(ret, node, pop);
9566 const NODE *b = RNODE_DOT2(node)->nd_beg;
9567 const NODE *e = RNODE_DOT2(node)->nd_end;
9569 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
9571 VALUE bv = nd_type_p(b, NODE_LIT) ? RNODE_LIT(b)->nd_lit :
Qnil;
9572 VALUE ev = nd_type_p(e, NODE_LIT) ? RNODE_LIT(e)->nd_lit :
Qnil;
9574 ADD_INSN1(ret, node, putobject, val);
9579 CHECK(COMPILE_(ret,
"min", b, popped));
9580 CHECK(COMPILE_(ret,
"max", e, popped));
9582 ADD_INSN1(ret, node, newrange, flag);
9592 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
9593 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
9599 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
9602 ip = ISEQ_BODY(ip)->parent_iseq;
9606 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
9609 ADD_INSN(ret, node, putnil);
9620 LABEL *end_label = NEW_LABEL(nd_line(node));
9621 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
9623 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
9625 COMPILE_ERROR(ERROR_ARGS
"unreachable");
9628 else if (nd_type_p(default_value, NODE_LIT) ||
9629 nd_type_p(default_value, NODE_NIL) ||
9630 nd_type_p(default_value, NODE_TRUE) ||
9631 nd_type_p(default_value, NODE_FALSE)) {
9632 COMPILE_ERROR(ERROR_ARGS
"unreachable");
9640 int kw_bits_idx = body->local_table_size - body->
param.keyword->bits_start;
9641 int keyword_idx = body->
param.keyword->num;
9643 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
9644 ADD_INSNL(ret, node, branchif, end_label);
9645 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
9646 ADD_LABEL(ret, end_label);
9656 unsigned int flag = 0;
9657 ID mid = RNODE_ATTRASGN(node)->nd_mid;
9659 LABEL *else_label = NULL;
9665 if (mid == idASET && !private_recv_p(node) && RNODE_ATTRASGN(node)->nd_args &&
9666 nd_type_p(RNODE_ATTRASGN(node)->nd_args, NODE_LIST) && RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->as.nd_alen == 2 &&
9667 nd_type_p(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_head, NODE_STR) &&
9668 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9669 !ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
9670 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction)
9672 VALUE str = rb_fstring(RNODE_STR(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_head)->nd_lit);
9673 CHECK(COMPILE(ret,
"recv", RNODE_ATTRASGN(node)->nd_recv));
9674 CHECK(COMPILE(ret,
"value", RNODE_LIST(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_next)->nd_head));
9676 ADD_INSN(ret, node, swap);
9677 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9679 ADD_INSN2(ret, node, opt_aset_with, str,
9680 new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
9682 ADD_INSN(ret, node, pop);
9688 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
9689 CHECK(!
NIL_P(argc));
9691 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
9692 CHECK(asgnflag != -1);
9693 flag |= (
unsigned int)asgnflag;
9695 debugp_param(
"argc", argc);
9696 debugp_param(
"nd_mid",
ID2SYM(mid));
9700 mid = rb_id_attrset(mid);
9701 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
9704 ADD_INSN(ret, node, putnil);
9708 if (flag & VM_CALL_ARGS_BLOCKARG) {
9709 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9710 if (flag & VM_CALL_ARGS_SPLAT) {
9711 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
9712 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
9714 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 3));
9715 ADD_INSN (ret, node, pop);
9717 else if (flag & VM_CALL_ARGS_SPLAT) {
9718 ADD_INSN(ret, node, dup);
9719 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
9720 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
9721 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
9722 ADD_INSN (ret, node, pop);
9725 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
9732 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
9733 qcall_branch_end(iseq, ret, else_label, branches, node, node);
9734 ADD_INSN(ret, node, pop);
9751 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
9752 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
9753 debugs(
"node: NODE_NIL(implicit)\n");
9754 NODE dummy_line_node = generate_dummy_line_node(lineno, -1);
9755 ADD_INSN(ret, &dummy_line_node, putnil);
9759 return iseq_compile_each0(iseq, ret, node, popped);
9765 const int line = (int)nd_line(node);
9766 const enum node_type
type = nd_type(node);
9769 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
9773 if (nd_fl_newline(node)) {
9775 ISEQ_COMPILE_DATA(iseq)->last_line = line;
9776 if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
9777 event |= RUBY_EVENT_COVERAGE_LINE;
9779 ADD_TRACE(ret, event);
9783 debug_node_start(node);
9785#define BEFORE_RETURN debug_node_end()
9789 CHECK(compile_block(iseq, ret, node, popped));
9793 CHECK(compile_if(iseq, ret, node, popped,
type));
9796 CHECK(compile_case(iseq, ret, node, popped));
9799 CHECK(compile_case2(iseq, ret, node, popped));
9802 CHECK(compile_case3(iseq, ret, node, popped));
9806 CHECK(compile_loop(iseq, ret, node, popped,
type));
9810 CHECK(compile_iter(iseq, ret, node, popped));
9812 case NODE_FOR_MASGN:
9813 CHECK(compile_for_masgn(iseq, ret, node, popped));
9816 CHECK(compile_break(iseq, ret, node, popped));
9819 CHECK(compile_next(iseq, ret, node, popped));
9822 CHECK(compile_redo(iseq, ret, node, popped));
9825 CHECK(compile_retry(iseq, ret, node, popped));
9828 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
9832 CHECK(compile_rescue(iseq, ret, node, popped));
9835 CHECK(compile_resbody(iseq, ret, node, popped));
9838 CHECK(compile_ensure(iseq, ret, node, popped));
9843 LABEL *end_label = NEW_LABEL(line);
9844 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(node)->nd_1st));
9846 ADD_INSN(ret, node, dup);
9848 if (
type == NODE_AND) {
9849 ADD_INSNL(ret, node, branchunless, end_label);
9852 ADD_INSNL(ret, node, branchif, end_label);
9855 ADD_INSN(ret, node, pop);
9857 CHECK(COMPILE_(ret,
"nd_2nd", RNODE_OR(node)->nd_2nd, popped));
9858 ADD_LABEL(ret, end_label);
9863 compile_massign(iseq, ret, node, popped);
9868 ID id = RNODE_LASGN(node)->nd_vid;
9869 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
9871 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
9872 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
9875 ADD_INSN(ret, node, dup);
9877 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
9882 ID id = RNODE_DASGN(node)->nd_vid;
9883 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
9884 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
9887 ADD_INSN(ret, node, dup);
9890 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
9893 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
9897 ADD_SETLOCAL(ret, node, ls - idx, lv);
9901 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
9904 ADD_INSN(ret, node, dup);
9906 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
9910 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
9912 ADD_INSN(ret, node, dup);
9914 ADD_INSN2(ret, node, setinstancevariable,
9915 ID2SYM(RNODE_IASGN(node)->nd_vid),
9916 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
9920 if (RNODE_CDECL(node)->nd_vid) {
9921 CHECK(COMPILE(ret,
"lvalue", RNODE_CDECL(node)->nd_value));
9924 ADD_INSN(ret, node, dup);
9927 ADD_INSN1(ret, node, putspecialobject,
9928 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
9929 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
9932 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
9933 CHECK(COMPILE(ret,
"lvalue", RNODE_CDECL(node)->nd_value));
9934 ADD_INSN(ret, node, swap);
9937 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9938 ADD_INSN(ret, node, swap);
9941 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
9946 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
9948 ADD_INSN(ret, node, dup);
9950 ADD_INSN2(ret, node, setclassvariable,
9951 ID2SYM(RNODE_CVASGN(node)->nd_vid),
9952 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
9956 CHECK(compile_op_asgn1(iseq, ret, node, popped));
9959 CHECK(compile_op_asgn2(iseq, ret, node, popped));
9962 CHECK(compile_op_cdecl(iseq, ret, node, popped));
9964 case NODE_OP_ASGN_AND:
9965 case NODE_OP_ASGN_OR:
9966 CHECK(compile_op_log(iseq, ret, node, popped,
type));
9970 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
9976 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
9982 CHECK(compile_super(iseq, ret, node, popped,
type));
9985 CHECK(compile_array(iseq, ret, node, popped) >= 0);
9990 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
9995 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
9998 CHECK(compile_return(iseq, ret, node, popped));
10001 CHECK(compile_yield(iseq, ret, node, popped));
10005 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
10011 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
10013 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
10015 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
10016 rb_id2str(RNODE_DVAR(node)->nd_vid));
10019 ADD_GETLOCAL(ret, node, ls - idx, lv);
10024 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
10026 ADD_INSN(ret, node, pop);
10031 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
10033 ADD_INSN2(ret, node, getinstancevariable,
10034 ID2SYM(RNODE_IVAR(node)->nd_vid),
10035 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
10040 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
10042 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10044 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(RNODE_CONST(node)->nd_vid));
10045 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10049 ADD_INSN(ret, node, putnil);
10050 ADD_INSN1(ret, node, putobject,
Qtrue);
10051 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
10055 ADD_INSN(ret, node, pop);
10061 ADD_INSN2(ret, node, getclassvariable,
10062 ID2SYM(RNODE_CVAR(node)->nd_vid),
10063 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
10067 case NODE_NTH_REF:{
10069 if (!RNODE_NTH_REF(node)->nd_nth) {
10070 ADD_INSN(ret, node, putnil);
10073 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
10074 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
10078 case NODE_BACK_REF:{
10080 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
10081 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
10088 CHECK(compile_match(iseq, ret, node, popped,
type));
10091 debugp_param(
"lit", RNODE_LIT(node)->nd_lit);
10093 ADD_INSN1(ret, node, putobject, RNODE_LIT(node)->nd_lit);
10099 debugp_param(
"nd_lit", RNODE_STR(node)->nd_lit);
10101 VALUE lit = RNODE_STR(node)->nd_lit;
10102 if (!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal) {
10103 lit = rb_fstring(lit);
10104 ADD_INSN1(ret, node, putstring, lit);
10108 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
10109 VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq),
INT2FIX(line));
10110 lit = rb_str_dup(lit);
10111 rb_ivar_set(lit, id_debug_created_info, rb_obj_freeze(debug_info));
10115 lit = rb_fstring(lit);
10117 ADD_INSN1(ret, node, putobject, lit);
10124 compile_dstr(iseq, ret, node);
10127 ADD_INSN(ret, node, pop);
10132 ADD_CALL_RECEIVER(ret, node);
10133 VALUE str = rb_fstring(RNODE_XSTR(node)->nd_lit);
10134 ADD_INSN1(ret, node, putobject, str);
10136 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
10139 ADD_INSN(ret, node, pop);
10144 ADD_CALL_RECEIVER(ret, node);
10145 compile_dstr(iseq, ret, node);
10146 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
10149 ADD_INSN(ret, node, pop);
10154 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
10157 compile_dregx(iseq, ret, node, popped);
10160 int ic_index = body->ise_size++;
10162 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
10164 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
10168 ADD_INSN(ret, node, pop);
10172 case NODE_ARGSCAT:{
10174 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
10175 ADD_INSN1(ret, node, splatarray,
Qfalse);
10176 ADD_INSN(ret, node, pop);
10177 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
10178 ADD_INSN1(ret, node, splatarray,
Qfalse);
10179 ADD_INSN(ret, node, pop);
10182 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
10183 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
10184 ADD_INSN(ret, node, concatarray);
10188 case NODE_ARGSPUSH:{
10190 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
10191 ADD_INSN1(ret, node, splatarray,
Qfalse);
10192 ADD_INSN(ret, node, pop);
10193 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
10196 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
10197 CHECK(compile_array_1(iseq, ret, RNODE_ARGSPUSH(node)->nd_body));
10198 ADD_INSN(ret, node, concatarray);
10203 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
10204 ADD_INSN1(ret, node, splatarray,
Qtrue);
10207 ADD_INSN(ret, node, pop);
10212 ID mid = RNODE_DEFN(node)->nd_mid;
10213 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
10215 ISEQ_TYPE_METHOD, line);
10217 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
10218 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
10222 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
10228 ID mid = RNODE_DEFS(node)->nd_mid;
10229 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
10231 ISEQ_TYPE_METHOD, line);
10233 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
10234 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
10235 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
10239 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
10244 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10245 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
10246 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
10247 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
10248 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
10251 ADD_INSN(ret, node, pop);
10256 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10257 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
10258 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
10259 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
10262 ADD_INSN(ret, node, pop);
10267 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10268 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
10269 CHECK(COMPILE(ret,
"undef arg", RNODE_UNDEF(node)->nd_undef));
10270 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
10273 ADD_INSN(ret, node, pop);
10278 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
10279 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
10280 ISEQ_TYPE_CLASS, line);
10281 const int flags = VM_DEFINECLASS_TYPE_CLASS |
10282 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
10283 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
10285 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
10286 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
10290 ADD_INSN(ret, node, pop);
10295 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
10296 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
10297 ISEQ_TYPE_CLASS, line);
10298 const int flags = VM_DEFINECLASS_TYPE_MODULE |
10299 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
10301 ADD_INSN (ret, node, putnil);
10302 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
10306 ADD_INSN(ret, node, pop);
10312 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
10313 ISEQ_TYPE_CLASS, line);
10315 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
10316 ADD_INSN (ret, node, putnil);
10317 CONST_ID(singletonclass,
"singletonclass");
10318 ADD_INSN3(ret, node, defineclass,
10319 ID2SYM(singletonclass), singleton_class,
10320 INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
10324 ADD_INSN(ret, node, pop);
10329 CHECK(compile_colon2(iseq, ret, node, popped));
10332 CHECK(compile_colon3(iseq, ret, node, popped));
10335 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
10338 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
10342 LABEL *lend = NEW_LABEL(line);
10343 LABEL *ltrue = NEW_LABEL(line);
10344 LABEL *lfalse = NEW_LABEL(line);
10345 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
10347 ADD_LABEL(ret, ltrue);
10348 ADD_INSN1(ret, node, putobject,
Qtrue);
10349 ADD_INSNL(ret, node, jump, lend);
10350 ADD_LABEL(ret, lfalse);
10351 ADD_INSN1(ret, node, putobject,
Qfalse);
10352 ADD_LABEL(ret, lend);
10357 ADD_INSN(ret, node, putself);
10363 ADD_INSN(ret, node, putnil);
10369 ADD_INSN1(ret, node, putobject,
Qtrue);
10375 ADD_INSN1(ret, node, putobject,
Qfalse);
10380 CHECK(compile_errinfo(iseq, ret, node, popped));
10384 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue));
10387 case NODE_POSTEXE:{
10391 int is_index = body->ise_size++;
10393 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
10395 new_child_iseq_with_callback(iseq, ifunc,
10396 rb_fstring(make_name_for_block(iseq)), iseq, ISEQ_TYPE_BLOCK, line);
10398 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
10402 ADD_INSN(ret, node, pop);
10407 CHECK(compile_kw_arg(iseq, ret, node, popped));
10410 compile_dstr(iseq, ret, node);
10412 ADD_INSN(ret, node, intern);
10415 ADD_INSN(ret, node, pop);
10419 case NODE_ATTRASGN:
10420 CHECK(compile_attrasgn(iseq, ret, node, popped));
10424 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
10427 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10428 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
10432 ADD_INSN(ret, node, pop);
10437 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
10452insn_data_length(
INSN *iobj)
10454 return insn_len(iobj->insn_id);
10458calc_sp_depth(
int depth,
INSN *insn)
10460 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
10464opobj_inspect(
VALUE obj)
10472 obj = rb_ary_dup(obj);
10484insn_data_to_s_detail(
INSN *iobj)
10486 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
10488 if (iobj->operands) {
10489 const char *types = insn_op_types(iobj->insn_id);
10492 for (j = 0; types[j]; j++) {
10493 char type = types[j];
10499 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
10517 VALUE v = OPERAND_AT(iobj, j);
10532 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
10535 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
10538 rb_str_catf(str,
"<ise:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
10544 if (vm_ci_mid(ci)) rb_str_catf(str,
"%"PRIsVALUE, rb_id2str(vm_ci_mid(ci)));
10545 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
10553 void *func = (
void *)OPERAND_AT(iobj, j);
10556 if (dladdr(func, &info) && info.dli_sname) {
10561 rb_str_catf(str,
"<%p>", func);
10571 if (types[j + 1]) {
10582 dump_disasm_list_with_cursor(link, NULL, NULL);
10593 printf(
"-- raw disasm--------\n");
10596 if (curr) printf(curr == link ?
"*" :
" ");
10597 switch (link->type) {
10598 case ISEQ_ELEMENT_INSN:
10600 iobj = (
INSN *)link;
10601 str = insn_data_to_s_detail(iobj);
10602 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
10603 pos += insn_data_length(iobj);
10606 case ISEQ_ELEMENT_LABEL:
10608 lobj = (
LABEL *)link;
10609 printf(LABEL_FORMAT
" [sp: %d]%s\n", lobj->label_no, lobj->sp,
10610 dest == lobj ?
" <---" :
"");
10613 case ISEQ_ELEMENT_TRACE:
10616 printf(
" trace: %0x\n", trace->event);
10619 case ISEQ_ELEMENT_ADJUST:
10622 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
10631 printf(
"---------------------\n");
10636rb_insn_len(
VALUE insn)
10638 return insn_len(insn);
10642rb_insns_name(
int i)
10644 return insn_name(i);
10648rb_insns_name_array(
void)
10650 VALUE ary = rb_ary_new_capa(VM_INSTRUCTION_SIZE);
10652 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
10653 rb_ary_push(ary, rb_fstring_cstr(insn_name(i)));
10655 return rb_obj_freeze(ary);
10663 obj = rb_to_symbol_type(obj);
10665 if (st_lookup(labels_table, obj, &tmp) == 0) {
10666 label = NEW_LABEL(0);
10667 st_insert(labels_table, obj, (st_data_t)label);
10670 label = (
LABEL *)tmp;
10677get_exception_sym2type(
VALUE sym)
10679 static VALUE symRescue, symEnsure, symRetry;
10680 static VALUE symBreak, symRedo, symNext;
10682 if (symRescue == 0) {
10691 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
10692 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
10693 if (sym == symRetry)
return CATCH_TYPE_RETRY;
10694 if (sym == symBreak)
return CATCH_TYPE_BREAK;
10695 if (sym == symRedo)
return CATCH_TYPE_REDO;
10696 if (sym == symNext)
return CATCH_TYPE_NEXT;
10697 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
10710 LABEL *lstart, *lend, *lcont;
10725 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
10726 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
10727 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
10731 if (
type == CATCH_TYPE_RESCUE ||
10732 type == CATCH_TYPE_BREAK ||
10733 type == CATCH_TYPE_NEXT) {
10739 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
10747insn_make_insn_table(
void)
10751 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
10753 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
10766 if (RB_TYPE_P(op,
T_ARRAY)) {
10767 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
10769 else if (
CLASS_OF(op) == rb_cISeq) {
10776 loaded_iseq = rb_iseqw_to_iseq(iseqw);
10777 return loaded_iseq;
10785 unsigned int flag = 0;
10796 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
10798 if (!
NIL_P(vkw_arg)) {
10801 size_t n = rb_callinfo_kwarg_bytes(
len);
10804 kw_arg->references = 0;
10805 kw_arg->keyword_len =
len;
10806 for (i = 0; i <
len; i++) {
10809 kw_arg->keywords[i] = kw;
10814 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
10820event_name_to_flag(
VALUE sym)
10822#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
10843 int line_no = 0, node_id = -1, insn_idx = 0;
10844 int ret = COMPILE_OK;
10849 static struct st_table *insn_table;
10851 if (insn_table == 0) {
10852 insn_table = insn_make_insn_table();
10855 for (i=0; i<
len; i++) {
10861 ADD_TRACE(anchor, event);
10864 LABEL *label = register_label(iseq, labels_table, obj);
10865 ADD_LABEL(anchor, label);
10871 else if (RB_TYPE_P(obj,
T_ARRAY)) {
10878 node_id =
NUM2INT(rb_ary_entry(node_ids, insn_idx++));
10882 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
10884 COMPILE_ERROR(iseq, line_no,
10885 "unknown instruction: %+"PRIsVALUE, insn);
10890 if (argc != insn_len((
VALUE)insn_id)-1) {
10891 COMPILE_ERROR(iseq, line_no,
10892 "operand size mismatch");
10898 argv = compile_data_calloc2(iseq,
sizeof(
VALUE), argc);
10901 NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
10904 (
enum ruby_vminsn_type)insn_id, argc, argv));
10906 for (j=0; j<argc; j++) {
10907 VALUE op = rb_ary_entry(obj, j+1);
10908 switch (insn_op_type((
VALUE)insn_id, j)) {
10910 LABEL *label = register_label(iseq, labels_table, op);
10911 argv[j] = (
VALUE)label;
10926 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
10937 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
10938 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
10943 VALUE segments = rb_ary_new();
10944 op = rb_to_array_type(op);
10948 sym = rb_to_symbol_type(sym);
10949 rb_ary_push(segments, sym);
10953 argv[j] = segments;
10955 ISEQ_BODY(iseq)->ic_size++;
10960 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
10961 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
10966 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
10967 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
10971 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
10974 argv[j] = rb_to_symbol_type(op);
10981 RHASH_TBL_RAW(map)->type = &cdhash_type;
10982 op = rb_to_array_type(op);
10987 register_label(iseq, labels_table, sym);
10988 rb_hash_aset(map, key, (
VALUE)label | 1);
10997#if SIZEOF_VALUE <= SIZEOF_LONG
11002 argv[j] = (
VALUE)funcptr;
11011 NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
11014 (
enum ruby_vminsn_type)insn_id, argc, NULL));
11018 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
11022 validate_labels(iseq, labels_table);
11023 if (!ret)
return ret;
11024 return iseq_setup(iseq, anchor);
11027#define CHECK_ARRAY(v) rb_to_array_type(v)
11028#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
11033 VALUE val = rb_hash_aref(param, sym);
11038 else if (!
NIL_P(val)) {
11039 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
11045static const struct rb_iseq_param_keyword *
11051 VALUE key, sym, default_val;
11054 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
11056 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
11058 keyword->num =
len;
11059#define SYM(s) ID2SYM(rb_intern_const(#s))
11060 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
11061 i = keyword->bits_start - keyword->num;
11062 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
11066 for (i = 0; i <
len; i++) {
11070 goto default_values;
11073 keyword->required_num++;
11077 default_len =
len - i;
11078 if (default_len == 0) {
11079 keyword->table = ids;
11082 else if (default_len < 0) {
11088 for (j = 0; i <
len; i++, j++) {
11102 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
11105 dvs[j] = default_val;
11108 keyword->table = ids;
11109 keyword->default_values = dvs;
11115iseq_insn_each_object_mark_and_pin(
VALUE obj,
VALUE _)
11124 size_t size =
sizeof(
INSN);
11125 unsigned int pos = 0;
11128#ifdef STRICT_ALIGNMENT
11129 size_t padding = calc_padding((
void *)&storage->buff[pos], size);
11131 const size_t padding = 0;
11133 size_t offset = pos + size + padding;
11134 if (offset > storage->size || offset > storage->pos) {
11136 storage = storage->next;
11139#ifdef STRICT_ALIGNMENT
11140 pos += (int)padding;
11143 iobj = (
INSN *)&storage->buff[pos];
11145 if (iobj->operands) {
11146 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_pin, (
VALUE)0);
11157#define SYM(s) ID2SYM(rb_intern_const(#s))
11159 unsigned int arg_size, local_size, stack_max;
11161 struct st_table *labels_table = st_init_numtable();
11163 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
11164 VALUE keywords = rb_hash_aref(params, SYM(keyword));
11166 DECL_ANCHOR(anchor);
11167 INIT_ANCHOR(anchor);
11170 ISEQ_BODY(iseq)->local_table_size =
len;
11171 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
11173 for (i = 0; i <
len; i++) {
11176 if (sym_arg_rest == lv) {
11184#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
11185 if (INT_PARAM(lead_num)) {
11186 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
11188 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
11189 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
11190 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
11191 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
11194#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
11196 INT_PARAM(arg_size);
11197 INT_PARAM(local_size);
11198 INT_PARAM(stack_max);
11203#ifdef USE_ISEQ_NODE_ID
11204 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
11205 if (!RB_TYPE_P(node_ids,
T_ARRAY)) {
11210 if (RB_TYPE_P(arg_opt_labels,
T_ARRAY)) {
11212 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
11214 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
11217 for (i = 0; i <
len; i++) {
11219 LABEL *label = register_label(iseq, labels_table, ent);
11220 opt_table[i] = (
VALUE)label;
11223 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
11224 ISEQ_BODY(iseq)->param.opt_table = opt_table;
11227 else if (!
NIL_P(arg_opt_labels)) {
11228 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
11232 if (RB_TYPE_P(keywords,
T_ARRAY)) {
11233 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
11235 else if (!
NIL_P(keywords)) {
11236 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
11240 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
11241 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
11244 if (int_param(&i, params, SYM(kwrest))) {
11245 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
11246 if (keyword == NULL) {
11247 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
11249 keyword->rest_start = i;
11250 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
11253 iseq_calc_param_size(iseq);
11256 iseq_build_from_ary_exception(iseq, labels_table, exception);
11259 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
11261 ISEQ_BODY(iseq)->param.size = arg_size;
11262 ISEQ_BODY(iseq)->local_table_size = local_size;
11263 ISEQ_BODY(iseq)->stack_max = stack_max;
11273 while (body->type == ISEQ_TYPE_BLOCK ||
11274 body->type == ISEQ_TYPE_RESCUE ||
11275 body->type == ISEQ_TYPE_ENSURE ||
11276 body->type == ISEQ_TYPE_EVAL ||
11277 body->type == ISEQ_TYPE_MAIN
11281 for (i = 0; i < body->local_table_size; i++) {
11282 if (body->local_table[i] ==
id) {
11286 iseq = body->parent_iseq;
11287 body = ISEQ_BODY(iseq);
11300 for (i=0; i<body->local_table_size; i++) {
11301 if (body->local_table[i] ==
id) {
11311#ifndef IBF_ISEQ_DEBUG
11312#define IBF_ISEQ_DEBUG 0
11315#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
11316#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
11319typedef uint32_t ibf_offset_t;
11320#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
11322#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
11324#define IBF_DEVEL_VERSION 4
11325#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
11327#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
11330static const char IBF_ENDIAN_MARK =
11331#ifdef WORDS_BIGENDIAN
11340 uint32_t major_version;
11341 uint32_t minor_version;
11343 uint32_t extra_size;
11345 uint32_t iseq_list_size;
11346 uint32_t global_object_list_size;
11347 ibf_offset_t iseq_list_offset;
11348 ibf_offset_t global_object_list_offset;
11369 unsigned int obj_list_size;
11370 ibf_offset_t obj_list_offset;
11389pinned_list_mark(
void *ptr)
11393 for (i = 0; i < list->size; i++) {
11394 if (list->buffer[i]) {
11395 rb_gc_mark(list->buffer[i]);
11407 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
11411pinned_list_fetch(
VALUE list,
long offset)
11417 if (offset >= ptr->size) {
11418 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
11421 return ptr->buffer[offset];
11425pinned_list_store(
VALUE list,
long offset,
VALUE object)
11431 if (offset >= ptr->size) {
11432 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
11439pinned_list_new(
long size)
11441 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
11442 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
11443 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
11449ibf_dump_pos(
struct ibf_dump *dump)
11451 long pos = RSTRING_LEN(dump->current_buffer->str);
11452#if SIZEOF_LONG > SIZEOF_INT
11453 if (pos >= UINT_MAX) {
11457 return (
unsigned int)pos;
11461ibf_dump_align(
struct ibf_dump *dump,
size_t align)
11463 ibf_offset_t pos = ibf_dump_pos(dump);
11465 static const char padding[
sizeof(
VALUE)];
11466 size_t size = align - ((size_t)pos % align);
11467#if SIZEOF_LONG > SIZEOF_INT
11468 if (pos + size >= UINT_MAX) {
11472 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
11473 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
11475 rb_str_cat(dump->current_buffer->str, padding, size);
11480ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
11482 ibf_offset_t pos = ibf_dump_pos(dump);
11483 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
11489ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
11491 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
11495ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
11497 VALUE str = dump->current_buffer->str;
11498 char *ptr = RSTRING_PTR(str);
11499 if ((
unsigned long)(size + offset) > (
unsigned long)RSTRING_LEN(str))
11500 rb_bug(
"ibf_dump_overwrite: overflow");
11501 memcpy(ptr + offset, buff, size);
11505ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
11507 ibf_offset_t beg = *offset;
11509 return load->current_buffer->buff + beg;
11513ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
11515 void *buff = ruby_xmalloc2(x, y);
11516 size_t size = x * y;
11517 memcpy(buff, load->current_buffer->buff + offset, size);
11521#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
11523#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
11524#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
11525#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
11526#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
11527#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
11530ibf_table_lookup(
struct st_table *table, st_data_t key)
11534 if (st_lookup(table, key, &val)) {
11543ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
11545 int index = ibf_table_lookup(table, key);
11548 index = (int)table->num_entries;
11549 st_insert(table, key, (st_data_t)index);
11557static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
11563ibf_dump_object_table_new(
void)
11565 st_table *obj_table = st_init_numtable();
11566 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
11574 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
11580 if (
id == 0 || rb_id2name(
id) == NULL) {
11583 return ibf_dump_object(dump, rb_id2sym(
id));
11587ibf_load_id(
const struct ibf_load *load,
const ID id_index)
11589 if (id_index == 0) {
11592 VALUE sym = ibf_load_object(load, id_index);
11598static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
11603 if (iseq == NULL) {
11607 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
11611static unsigned char
11612ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
11614 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
11615 return (
unsigned char)load->current_buffer->buff[(*offset)++];
11631 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
11632 ibf_dump_write(dump, &x,
sizeof(
VALUE));
11636 enum { max_byte_length =
sizeof(
VALUE) + 1 };
11638 unsigned char bytes[max_byte_length];
11641 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
11642 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
11648 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
11651 ibf_dump_write(dump, bytes + max_byte_length - n, n);
11655ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
11657 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
11658 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
11660 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
11661 *offset +=
sizeof(
VALUE);
11666 enum { max_byte_length =
sizeof(
VALUE) + 1 };
11668 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
11669 const unsigned char c = buffer[*offset];
11673 c == 0 ? 9 : ntz_int32(c) + 1;
11676 if (*offset + n > load->current_buffer->size) {
11681 for (i = 1; i < n; i++) {
11683 x |= (
VALUE)buffer[*offset + i];
11697 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
11699 size_t len = strlen(bf->name);
11700 ibf_dump_write_small_value(dump, (
VALUE)
len);
11701 ibf_dump_write(dump, bf->name,
len);
11705ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
11707 int i = (int)ibf_load_small_value(load, offset);
11708 int len = (int)ibf_load_small_value(load, offset);
11709 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
11712 fprintf(stderr,
"%.*s!!\n",
len, name);
11716 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
11717 if (strncmp(table[i].name, name,
len) != 0) {
11718 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
11729 const int iseq_size = body->iseq_size;
11731 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
11733 ibf_offset_t offset = ibf_dump_pos(dump);
11735 for (code_index=0; code_index<iseq_size;) {
11736 const VALUE insn = orig_code[code_index++];
11737 const char *types = insn_op_types(insn);
11742 ibf_dump_write_small_value(dump, insn);
11745 for (op_index=0; types[op_index]; op_index++, code_index++) {
11746 VALUE op = orig_code[code_index];
11749 switch (types[op_index]) {
11752 wv = ibf_dump_object(dump, op);
11761 wv = ibf_dump_object(dump, arr);
11769 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
11777 wv = ibf_dump_id(dump, (
ID)op);
11789 ibf_dump_write_small_value(dump, wv);
11792 assert(insn_len(insn) == op_index+1);
11799ibf_load_code(
const struct ibf_load *load,
rb_iseq_t *iseq, ibf_offset_t bytecode_offset, ibf_offset_t bytecode_size,
unsigned int iseq_size)
11802 unsigned int code_index;
11803 ibf_offset_t reading_pos = bytecode_offset;
11807 struct rb_call_data *cd_entries = load_body->call_data;
11810 iseq_bits_t * mark_offset_bits;
11812 iseq_bits_t tmp[1] = {0};
11814 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
11815 mark_offset_bits = tmp;
11818 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
11820 bool needs_bitmap =
false;
11822 for (code_index=0; code_index<iseq_size;) {
11824 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
11825 const char *types = insn_op_types(insn);
11831 for (op_index=0; types[op_index]; op_index++, code_index++) {
11832 const char operand_type = types[op_index];
11833 switch (operand_type) {
11836 VALUE op = ibf_load_small_value(load, &reading_pos);
11837 VALUE v = ibf_load_object(load, op);
11838 code[code_index] = v;
11841 ISEQ_MBITS_SET(mark_offset_bits, code_index);
11842 needs_bitmap =
true;
11848 VALUE op = ibf_load_small_value(load, &reading_pos);
11849 VALUE v = ibf_load_object(load, op);
11850 v = rb_hash_dup(v);
11851 RHASH_TBL_RAW(v)->type = &cdhash_type;
11853 freeze_hide_obj(v);
11858 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
11860 code[code_index] = v;
11861 ISEQ_MBITS_SET(mark_offset_bits, code_index);
11863 needs_bitmap =
true;
11868 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
11870 code[code_index] = v;
11873 ISEQ_MBITS_SET(mark_offset_bits, code_index);
11874 needs_bitmap =
true;
11880 VALUE op = ibf_load_small_value(load, &reading_pos);
11881 VALUE arr = ibf_load_object(load, op);
11883 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
11884 ic->
segments = array_to_idlist(arr);
11886 code[code_index] = (
VALUE)ic;
11893 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11895 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
11896 code[code_index] = (
VALUE)ic;
11898 if (operand_type == TS_IVC) {
11901 if (insn == BIN(setinstancevariable)) {
11902 ID iv_name = (
ID)code[code_index - 1];
11903 cache->iv_set_name = iv_name;
11906 cache->iv_set_name = 0;
11909 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
11916 code[code_index] = (
VALUE)cd_entries++;
11921 VALUE op = ibf_load_small_value(load, &reading_pos);
11922 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
11929 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
11932 code[code_index] = ibf_load_small_value(load, &reading_pos);
11936 if (insn_len(insn) != op_index+1) {
11941 load_body->iseq_encoded = code;
11942 load_body->iseq_size = code_index;
11944 if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
11945 load_body->mark_bits.single = mark_offset_bits[0];
11948 if (needs_bitmap) {
11949 load_body->mark_bits.list = mark_offset_bits;
11952 load_body->mark_bits.list = 0;
11953 ruby_xfree(mark_offset_bits);
11957 assert(code_index == iseq_size);
11958 assert(reading_pos == bytecode_offset + bytecode_size);
11965 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
11968 IBF_W_ALIGN(
VALUE);
11969 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
11972 return ibf_dump_pos(dump);
11977ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
11981 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
11992 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
11995 struct rb_iseq_param_keyword dump_kw = *kw;
11996 int dv_num = kw->num - kw->required_num;
12001 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
12002 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
12004 dump_kw.table = IBF_W(ids,
ID, kw->num);
12005 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
12006 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
12007 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
12014static const struct rb_iseq_param_keyword *
12015ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
12017 if (param_keyword_offset) {
12018 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
12019 ID *ids = IBF_R(kw->table,
ID, kw->num);
12020 int dv_num = kw->num - kw->required_num;
12021 VALUE *dvs = IBF_R(kw->default_values,
VALUE, dv_num);
12024 for (i=0; i<kw->num; i++) {
12025 ids[i] = ibf_load_id(load, ids[i]);
12027 for (i=0; i<dv_num; i++) {
12028 dvs[i] = ibf_load_object(load, dvs[i]);
12032 kw->default_values = dvs;
12043 ibf_offset_t offset = ibf_dump_pos(dump);
12047 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
12048 ibf_dump_write_small_value(dump, entries[i].line_no);
12049#ifdef USE_ISEQ_NODE_ID
12050 ibf_dump_write_small_value(dump, entries[i].node_id);
12052 ibf_dump_write_small_value(dump, entries[i].events);
12059ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
12061 ibf_offset_t reading_pos = body_offset;
12065 for (i = 0; i < size; i++) {
12066 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
12067#ifdef USE_ISEQ_NODE_ID
12068 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
12070 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
12077ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
12079 ibf_offset_t offset = ibf_dump_pos(dump);
12081 unsigned int last = 0;
12083 for (i = 0; i < size; i++) {
12084 ibf_dump_write_small_value(dump, positions[i] - last);
12085 last = positions[i];
12091static unsigned int *
12092ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
12094 ibf_offset_t reading_pos = positions_offset;
12095 unsigned int *positions =
ALLOC_N(
unsigned int, size);
12097 unsigned int last = 0;
12099 for (i = 0; i < size; i++) {
12100 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
12101 last = positions[i];
12111 const int size = body->local_table_size;
12115 for (i=0; i<size; i++) {
12116 table[i] = ibf_dump_id(dump, body->local_table[i]);
12120 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
12124ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
12127 ID *table = IBF_R(local_table_offset,
ID, size);
12130 for (i=0; i<size; i++) {
12131 table[i] = ibf_load_id(load, table[i]);
12146 int *iseq_indices =
ALLOCA_N(
int, table->size);
12149 for (i=0; i<table->size; i++) {
12150 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
12153 const ibf_offset_t offset = ibf_dump_pos(dump);
12155 for (i=0; i<table->size; i++) {
12156 ibf_dump_write_small_value(dump, iseq_indices[i]);
12157 ibf_dump_write_small_value(dump, table->entries[i].type);
12158 ibf_dump_write_small_value(dump, table->entries[i].start);
12159 ibf_dump_write_small_value(dump, table->entries[i].end);
12160 ibf_dump_write_small_value(dump, table->entries[i].cont);
12161 ibf_dump_write_small_value(dump, table->entries[i].sp);
12166 return ibf_dump_pos(dump);
12171ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size)
12174 struct iseq_catch_table *table = ruby_xmalloc(iseq_catch_table_bytes(size));
12175 table->size = size;
12177 ibf_offset_t reading_pos = catch_table_offset;
12180 for (i=0; i<table->size; i++) {
12181 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12182 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
12183 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12184 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12185 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12186 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12188 table->entries[i].iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)iseq_index);
12201 const unsigned int ci_size = body->ci_size;
12204 ibf_offset_t offset = ibf_dump_pos(dump);
12208 for (i = 0; i < ci_size; i++) {
12211 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
12212 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
12213 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
12217 int len = kwarg->keyword_len;
12218 ibf_dump_write_small_value(dump,
len);
12219 for (
int j=0; j<
len; j++) {
12220 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
12221 ibf_dump_write_small_value(dump, keyword);
12225 ibf_dump_write_small_value(dump, 0);
12230 ibf_dump_write_small_value(dump, (
VALUE)-1);
12248static enum rb_id_table_iterator_result
12249store_outer_variable(
ID id,
VALUE val,
void *dump)
12254 pair->name = rb_id2str(
id);
12256 return ID_TABLE_CONTINUE;
12260outer_variable_cmp(
const void *a,
const void *b,
void *arg)
12270 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
12272 ibf_offset_t offset = ibf_dump_pos(dump);
12274 size_t size = ovs ? rb_id_table_size(ovs) : 0;
12275 ibf_dump_write_small_value(dump, (
VALUE)size);
12284 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
12286 for (
size_t i = 0; i < size; ++i) {
12287 ID id = ovlist->pairs[i].id;
12288 ID val = ovlist->pairs[i].val;
12289 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
12290 ibf_dump_write_small_value(dump, val);
12299ibf_load_ci_entries(
const struct ibf_load *load,
12300 ibf_offset_t ci_entries_offset,
12301 unsigned int ci_size,
12304 ibf_offset_t reading_pos = ci_entries_offset;
12311 for (i = 0; i < ci_size; i++) {
12312 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
12313 if (mid_index != (
VALUE)-1) {
12314 ID mid = ibf_load_id(load, mid_index);
12315 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12316 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12319 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
12322 kwarg->references = 0;
12323 kwarg->keyword_len = kwlen;
12324 for (
int j=0; j<kwlen; j++) {
12325 VALUE keyword = ibf_load_small_value(load, &reading_pos);
12326 kwarg->keywords[j] = ibf_load_object(load, keyword);
12330 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
12332 cds[i].cc = vm_cc_empty();
12343ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
12345 ibf_offset_t reading_pos = outer_variables_offset;
12349 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
12351 if (table_size > 0) {
12352 tbl = rb_id_table_create(table_size);
12355 for (
size_t i = 0; i < table_size; i++) {
12356 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
12357 VALUE value = ibf_load_small_value(load, &reading_pos);
12358 if (!key) key = rb_make_temporary_id(i);
12359 rb_id_table_insert(tbl, key, value);
12368 assert(dump->current_buffer == &dump->global_buffer);
12370 unsigned int *positions;
12374 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
12375 const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
12376 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
12378#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12379 ibf_offset_t iseq_start = ibf_dump_pos(dump);
12384 buffer.obj_table = ibf_dump_object_table_new();
12385 dump->current_buffer = &buffer;
12388 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
12389 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
12390 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
12391 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
12392 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
12394 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
12395 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
12396 ruby_xfree(positions);
12398 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
12399 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
12400 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
12401 const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
12402 const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
12403 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
12404 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
12405 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
12407#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12408 ibf_offset_t local_obj_list_offset;
12409 unsigned int local_obj_list_size;
12411 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
12414 ibf_offset_t body_offset = ibf_dump_pos(dump);
12417 unsigned int param_flags =
12418 (body->
param.flags.has_lead << 0) |
12419 (body->
param.flags.has_opt << 1) |
12420 (body->
param.flags.has_rest << 2) |
12421 (body->
param.flags.has_post << 3) |
12422 (body->
param.flags.has_kw << 4) |
12423 (body->
param.flags.has_kwrest << 5) |
12424 (body->
param.flags.has_block << 6) |
12425 (body->
param.flags.ambiguous_param0 << 7) |
12426 (body->
param.flags.accepts_no_kwarg << 8) |
12427 (body->
param.flags.ruby2_keywords << 9);
12429#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12430# define IBF_BODY_OFFSET(x) (x)
12432# define IBF_BODY_OFFSET(x) (body_offset - (x))
12435 ibf_dump_write_small_value(dump, body->type);
12436 ibf_dump_write_small_value(dump, body->iseq_size);
12437 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
12438 ibf_dump_write_small_value(dump, bytecode_size);
12439 ibf_dump_write_small_value(dump, param_flags);
12440 ibf_dump_write_small_value(dump, body->
param.size);
12441 ibf_dump_write_small_value(dump, body->
param.lead_num);
12442 ibf_dump_write_small_value(dump, body->
param.opt_num);
12443 ibf_dump_write_small_value(dump, body->
param.rest_start);
12444 ibf_dump_write_small_value(dump, body->
param.post_start);
12445 ibf_dump_write_small_value(dump, body->
param.post_num);
12446 ibf_dump_write_small_value(dump, body->
param.block_start);
12447 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
12448 ibf_dump_write_small_value(dump, param_keyword_offset);
12449 ibf_dump_write_small_value(dump, location_pathobj_index);
12450 ibf_dump_write_small_value(dump, location_base_label_index);
12451 ibf_dump_write_small_value(dump, location_label_index);
12452 ibf_dump_write_small_value(dump, body->location.first_lineno);
12453 ibf_dump_write_small_value(dump, body->location.node_id);
12454 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
12455 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
12456 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
12457 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
12458 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
12459 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
12460 ibf_dump_write_small_value(dump, body->insns_info.size);
12461 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
12462 ibf_dump_write_small_value(dump, catch_table_size);
12463 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
12464 ibf_dump_write_small_value(dump, parent_iseq_index);
12465 ibf_dump_write_small_value(dump, local_iseq_index);
12466 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
12467 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
12468 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
12469 ibf_dump_write_small_value(dump, body->variable.flip_count);
12470 ibf_dump_write_small_value(dump, body->local_table_size);
12471 ibf_dump_write_small_value(dump, body->ivc_size);
12472 ibf_dump_write_small_value(dump, body->icvarc_size);
12473 ibf_dump_write_small_value(dump, body->ise_size);
12474 ibf_dump_write_small_value(dump, body->ic_size);
12475 ibf_dump_write_small_value(dump, body->ci_size);
12476 ibf_dump_write_small_value(dump, body->stack_max);
12477 ibf_dump_write_small_value(dump, body->builtin_attrs);
12479#undef IBF_BODY_OFFSET
12481#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12482 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
12484 dump->current_buffer = saved_buffer;
12485 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
12487 ibf_offset_t offset = ibf_dump_pos(dump);
12488 ibf_dump_write_small_value(dump, iseq_start);
12489 ibf_dump_write_small_value(dump, iseq_length_bytes);
12490 ibf_dump_write_small_value(dump, body_offset);
12492 ibf_dump_write_small_value(dump, local_obj_list_offset);
12493 ibf_dump_write_small_value(dump, local_obj_list_size);
12495 st_free_table(buffer.obj_table);
12499 return body_offset;
12504ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
12506 VALUE str = ibf_load_object(load, str_index);
12508 str = rb_fstring(str);
12518 ibf_offset_t reading_pos = offset;
12520#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12522 load->current_buffer = &load->global_buffer;
12524 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12525 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12526 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12529 buffer.buff = load->global_buffer.buff + iseq_start;
12530 buffer.size = iseq_length_bytes;
12531 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12532 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12533 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
12535 load->current_buffer = &buffer;
12536 reading_pos = body_offset;
12539#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12540# define IBF_BODY_OFFSET(x) (x)
12542# define IBF_BODY_OFFSET(x) (offset - (x))
12545 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12546 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12547 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12548 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12549 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12550 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12551 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
12552 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
12553 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
12554 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
12555 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
12556 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
12557 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12558 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12559 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
12560 const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
12561 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
12562 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
12563 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
12564 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
12565 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
12566 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
12567 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
12568 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12569 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12570 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12571 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12572 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12573 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12574 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12575 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12576 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12577 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12578 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12579 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
12580 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12582 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12583 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12584 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12585 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12587 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12588 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12589 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12592 VALUE path = ibf_load_object(load, location_pathobj_index);
12597 realpath = path = rb_fstring(path);
12599 else if (RB_TYPE_P(path,
T_ARRAY)) {
12600 VALUE pathobj = path;
12606 if (!
NIL_P(realpath)) {
12607 if (!RB_TYPE_P(realpath,
T_STRING)) {
12608 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
12609 "(%x), path=%+"PRIsVALUE,
12610 realpath,
TYPE(realpath), path);
12612 realpath = rb_fstring(realpath);
12618 rb_iseq_pathobj_set(iseq, path, realpath);
12623 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
12625#undef IBF_BODY_OFFSET
12627 load_body->type =
type;
12628 load_body->stack_max = stack_max;
12629 load_body->
param.flags.has_lead = (param_flags >> 0) & 1;
12630 load_body->
param.flags.has_opt = (param_flags >> 1) & 1;
12631 load_body->
param.flags.has_rest = (param_flags >> 2) & 1;
12632 load_body->
param.flags.has_post = (param_flags >> 3) & 1;
12633 load_body->
param.flags.has_kw = FALSE;
12634 load_body->
param.flags.has_kwrest = (param_flags >> 5) & 1;
12635 load_body->
param.flags.has_block = (param_flags >> 6) & 1;
12636 load_body->
param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
12637 load_body->
param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
12638 load_body->
param.flags.ruby2_keywords = (param_flags >> 9) & 1;
12639 load_body->
param.size = param_size;
12640 load_body->
param.lead_num = param_lead_num;
12641 load_body->
param.opt_num = param_opt_num;
12642 load_body->
param.rest_start = param_rest_start;
12643 load_body->
param.post_start = param_post_start;
12644 load_body->
param.post_num = param_post_num;
12645 load_body->
param.block_start = param_block_start;
12646 load_body->local_table_size = local_table_size;
12647 load_body->ci_size = ci_size;
12648 load_body->insns_info.size = insns_info_size;
12650 ISEQ_COVERAGE_SET(iseq,
Qnil);
12651 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
12652 load_body->variable.flip_count = variable_flip_count;
12653 load_body->variable.script_lines =
Qnil;
12655 load_body->location.first_lineno = location_first_lineno;
12656 load_body->location.node_id = location_node_id;
12657 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
12658 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
12659 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
12660 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
12661 load_body->builtin_attrs = builtin_attrs;
12663 load_body->ivc_size = ivc_size;
12664 load_body->icvarc_size = icvarc_size;
12665 load_body->ise_size = ise_size;
12666 load_body->ic_size = ic_size;
12668 if (ISEQ_IS_SIZE(load_body)) {
12672 load_body->is_entries = NULL;
12674 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
12675 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
12676 load_body->
param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
12677 load_body->
param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
12678 load_body->
param.flags.has_kw = (param_flags >> 4) & 1;
12679 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
12680 load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
12681 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
12682 load_body->catch_table = ibf_load_catch_table(load, catch_table_offset, catch_table_size);
12683 load_body->parent_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)parent_iseq_index);
12684 load_body->local_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)local_iseq_index);
12685 load_body->mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
12687 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
12688#if VM_INSN_INFO_TABLE_IMPL == 2
12689 rb_iseq_insns_info_encode_positions(iseq);
12692 rb_iseq_translate_threaded_code(iseq);
12694#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12695 load->current_buffer = &load->global_buffer;
12698 RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
12699 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
12701#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12702 load->current_buffer = saved_buffer;
12704 verify_call_cache(iseq);
12707 rb_vm_pop_frame_no_int(ec);
12717ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
12722 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
12723 rb_ary_push(args->offset_list,
UINT2NUM(offset));
12725 return ST_CONTINUE;
12731 VALUE offset_list = rb_ary_hidden_new(dump->iseq_table->num_entries);
12735 args.offset_list = offset_list;
12737 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
12740 st_index_t size = dump->iseq_table->num_entries;
12741 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
12743 for (i = 0; i < size; i++) {
12747 ibf_dump_align(dump,
sizeof(ibf_offset_t));
12748 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
12749 header->iseq_list_size = (
unsigned int)size;
12752#define IBF_OBJECT_INTERNAL FL_PROMOTED0
12761 unsigned int type: 5;
12762 unsigned int special_const: 1;
12763 unsigned int frozen: 1;
12764 unsigned int internal: 1;
12767enum ibf_object_class_index {
12768 IBF_OBJECT_CLASS_OBJECT,
12769 IBF_OBJECT_CLASS_ARRAY,
12770 IBF_OBJECT_CLASS_STANDARD_ERROR,
12771 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
12772 IBF_OBJECT_CLASS_TYPE_ERROR,
12773 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
12783 long keyval[FLEX_ARY_LEN];
12796 BDIGIT digits[FLEX_ARY_LEN];
12799enum ibf_object_data_type {
12800 IBF_OBJECT_DATA_ENCODING,
12811#define IBF_ALIGNED_OFFSET(align, offset) \
12812 ((((offset) - 1) / (align) + 1) * (align))
12813#define IBF_OBJBODY(type, offset) (const type *)\
12814 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
12817ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
12819 if (offset >= load->current_buffer->size) {
12820 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
12822 return load->current_buffer->buff + offset;
12825NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
12828ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
12831 rb_raw_obj_info(buff,
sizeof(buff), obj);
12840 rb_raise(rb_eArgError,
"unsupported");
12847 enum ibf_object_class_index cindex;
12848 if (obj == rb_cObject) {
12849 cindex = IBF_OBJECT_CLASS_OBJECT;
12852 cindex = IBF_OBJECT_CLASS_ARRAY;
12855 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
12858 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
12861 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
12864 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
12867 rb_obj_info_dump(obj);
12869 rb_bug(
"unsupported class");
12871 ibf_dump_write_small_value(dump, (
VALUE)cindex);
12877 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
12880 case IBF_OBJECT_CLASS_OBJECT:
12882 case IBF_OBJECT_CLASS_ARRAY:
12884 case IBF_OBJECT_CLASS_STANDARD_ERROR:
12886 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
12888 case IBF_OBJECT_CLASS_TYPE_ERROR:
12890 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
12894 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
12902 (void)IBF_W(&dbl,
double, 1);
12908 const double *dblp = IBF_OBJBODY(
double, offset);
12915 long encindex = (long)rb_enc_get_index(obj);
12916 long len = RSTRING_LEN(obj);
12917 const char *ptr = RSTRING_PTR(obj);
12919 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
12920 rb_encoding *enc = rb_enc_from_index((
int)encindex);
12921 const char *enc_name = rb_enc_name(enc);
12922 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
12925 ibf_dump_write_small_value(dump, encindex);
12926 ibf_dump_write_small_value(dump,
len);
12927 IBF_WP(ptr,
char,
len);
12933 ibf_offset_t reading_pos = offset;
12935 int encindex = (int)ibf_load_small_value(load, &reading_pos);
12936 const long len = (long)ibf_load_small_value(load, &reading_pos);
12937 const char *ptr = load->current_buffer->buff + reading_pos;
12939 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
12940 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
12941 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
12945 if (header->frozen && !header->internal) {
12949 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
12951 if (header->internal) rb_obj_hide(str);
12952 if (header->frozen) str = rb_fstring(str);
12963 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
12965 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
12966 ibf_dump_write_small_value(dump, regexp.srcstr);
12973 regexp.option = ibf_load_byte(load, &offset);
12974 regexp.srcstr = ibf_load_small_value(load, &offset);
12976 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
12977 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
12979 if (header->internal) rb_obj_hide(reg);
12980 if (header->frozen) rb_obj_freeze(reg);
12989 ibf_dump_write_small_value(dump,
len);
12990 for (i=0; i<
len; i++) {
12991 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
12992 ibf_dump_write_small_value(dump, index);
12999 ibf_offset_t reading_pos = offset;
13001 const long len = (long)ibf_load_small_value(load, &reading_pos);
13003 VALUE ary = header->internal ? rb_ary_hidden_new(
len) : rb_ary_new_capa(
len);
13006 for (i=0; i<
len; i++) {
13007 const VALUE index = ibf_load_small_value(load, &reading_pos);
13008 rb_ary_push(ary, ibf_load_object(load, index));
13011 if (header->frozen) rb_obj_freeze(ary);
13017ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
13021 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
13022 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
13024 ibf_dump_write_small_value(dump, key_index);
13025 ibf_dump_write_small_value(dump, val_index);
13026 return ST_CONTINUE;
13033 ibf_dump_write_small_value(dump, (
VALUE)
len);
13041 long len = (long)ibf_load_small_value(load, &offset);
13042 VALUE obj = rb_hash_new_with_size(
len);
13045 for (i = 0; i <
len; i++) {
13046 VALUE key_index = ibf_load_small_value(load, &offset);
13047 VALUE val_index = ibf_load_small_value(load, &offset);
13049 VALUE key = ibf_load_object(load, key_index);
13050 VALUE val = ibf_load_object(load, val_index);
13051 rb_hash_aset(obj, key, val);
13053 rb_hash_rehash(obj);
13055 if (header->internal) rb_obj_hide(obj);
13056 if (header->frozen) rb_obj_freeze(obj);
13069 range.class_index = 0;
13072 range.beg = (long)ibf_dump_object(dump, beg);
13073 range.end = (long)ibf_dump_object(dump, end);
13079 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
13088 VALUE beg = ibf_load_object(load, range->beg);
13089 VALUE end = ibf_load_object(load, range->end);
13091 if (header->internal) rb_obj_hide(obj);
13092 if (header->frozen) rb_obj_freeze(obj);
13099 ssize_t
len = BIGNUM_LEN(obj);
13100 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
13101 BDIGIT *d = BIGNUM_DIGITS(obj);
13103 (void)IBF_W(&slen, ssize_t, 1);
13104 IBF_WP(d, BDIGIT,
len);
13111 int sign = bignum->slen > 0;
13112 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
13113 const int big_unpack_flags =
13116 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
13119 if (header->internal) rb_obj_hide(obj);
13120 if (header->frozen) rb_obj_freeze(obj);
13127 if (rb_data_is_encoding(obj)) {
13129 const char *name = rb_enc_name(enc);
13130 long len = strlen(name) + 1;
13132 data[0] = IBF_OBJECT_DATA_ENCODING;
13134 (void)IBF_W(data,
long, 2);
13135 IBF_WP(name,
char,
len);
13138 ibf_dump_object_unsupported(dump, obj);
13145 const long *body = IBF_OBJBODY(
long, offset);
13146 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
13148 const char *data = (
const char *)&body[2];
13151 case IBF_OBJECT_DATA_ENCODING:
13153 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
13158 return ibf_load_object_unsupported(load, header, offset);
13162ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
13165 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
13166 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
13168 (void)IBF_W(data,
long, 2);
13172ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
13175 VALUE a = ibf_load_object(load, nums->a);
13176 VALUE b = ibf_load_object(load, nums->b);
13178 rb_complex_new(a, b) : rb_rational_new(a, b);
13180 if (header->internal) rb_obj_hide(obj);
13181 if (header->frozen) rb_obj_freeze(obj);
13188 ibf_dump_object_string(dump,
rb_sym2str(obj));
13194 ibf_offset_t reading_pos = offset;
13196 int encindex = (int)ibf_load_small_value(load, &reading_pos);
13197 const long len = (long)ibf_load_small_value(load, &reading_pos);
13198 const char *ptr = load->current_buffer->buff + reading_pos;
13200 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
13201 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
13202 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
13205 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
13209typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
13210static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
13211 ibf_dump_object_unsupported,
13212 ibf_dump_object_unsupported,
13213 ibf_dump_object_class,
13214 ibf_dump_object_unsupported,
13215 ibf_dump_object_float,
13216 ibf_dump_object_string,
13217 ibf_dump_object_regexp,
13218 ibf_dump_object_array,
13219 ibf_dump_object_hash,
13220 ibf_dump_object_struct,
13221 ibf_dump_object_bignum,
13222 ibf_dump_object_unsupported,
13223 ibf_dump_object_data,
13224 ibf_dump_object_unsupported,
13225 ibf_dump_object_complex_rational,
13226 ibf_dump_object_complex_rational,
13227 ibf_dump_object_unsupported,
13228 ibf_dump_object_unsupported,
13229 ibf_dump_object_unsupported,
13230 ibf_dump_object_unsupported,
13231 ibf_dump_object_symbol,
13232 ibf_dump_object_unsupported,
13233 ibf_dump_object_unsupported,
13234 ibf_dump_object_unsupported,
13235 ibf_dump_object_unsupported,
13236 ibf_dump_object_unsupported,
13237 ibf_dump_object_unsupported,
13238 ibf_dump_object_unsupported,
13239 ibf_dump_object_unsupported,
13240 ibf_dump_object_unsupported,
13241 ibf_dump_object_unsupported,
13242 ibf_dump_object_unsupported,
13248 unsigned char byte =
13249 (header.type << 0) |
13250 (header.special_const << 5) |
13251 (header.frozen << 6) |
13252 (header.internal << 7);
13258ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
13260 unsigned char byte = ibf_load_byte(load, offset);
13263 header.type = (
byte >> 0) & 0x1f;
13264 header.special_const = (
byte >> 5) & 0x01;
13265 header.frozen = (
byte >> 6) & 0x01;
13266 header.internal = (
byte >> 7) & 0x01;
13275 ibf_offset_t current_offset;
13276 IBF_ZERO(obj_header);
13277 obj_header.type =
TYPE(obj);
13279 IBF_W_ALIGN(ibf_offset_t);
13280 current_offset = ibf_dump_pos(dump);
13285 obj_header.special_const = TRUE;
13286 obj_header.frozen = TRUE;
13287 obj_header.internal = TRUE;
13288 ibf_dump_object_object_header(dump, obj_header);
13289 ibf_dump_write_small_value(dump, obj);
13293 obj_header.special_const = FALSE;
13295 ibf_dump_object_object_header(dump, obj_header);
13296 (*dump_object_functions[obj_header.type])(dump, obj);
13299 return current_offset;
13303static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
13304 ibf_load_object_unsupported,
13305 ibf_load_object_unsupported,
13306 ibf_load_object_class,
13307 ibf_load_object_unsupported,
13308 ibf_load_object_float,
13309 ibf_load_object_string,
13310 ibf_load_object_regexp,
13311 ibf_load_object_array,
13312 ibf_load_object_hash,
13313 ibf_load_object_struct,
13314 ibf_load_object_bignum,
13315 ibf_load_object_unsupported,
13316 ibf_load_object_data,
13317 ibf_load_object_unsupported,
13318 ibf_load_object_complex_rational,
13319 ibf_load_object_complex_rational,
13320 ibf_load_object_unsupported,
13321 ibf_load_object_unsupported,
13322 ibf_load_object_unsupported,
13323 ibf_load_object_unsupported,
13324 ibf_load_object_symbol,
13325 ibf_load_object_unsupported,
13326 ibf_load_object_unsupported,
13327 ibf_load_object_unsupported,
13328 ibf_load_object_unsupported,
13329 ibf_load_object_unsupported,
13330 ibf_load_object_unsupported,
13331 ibf_load_object_unsupported,
13332 ibf_load_object_unsupported,
13333 ibf_load_object_unsupported,
13334 ibf_load_object_unsupported,
13335 ibf_load_object_unsupported,
13339ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
13341 if (object_index == 0) {
13345 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
13347 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
13348 ibf_offset_t offset = offsets[object_index];
13349 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
13352 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
13353 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
13354 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
13355 header.type, header.special_const, header.frozen, header.internal);
13357 if (offset >= load->current_buffer->size) {
13358 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
13361 if (header.special_const) {
13362 ibf_offset_t reading_pos = offset;
13364 obj = ibf_load_small_value(load, &reading_pos);
13367 obj = (*load_object_functions[header.type])(load, &header, offset);
13370 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
13373 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
13374 object_index, obj);
13387ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
13392 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
13393 rb_ary_push(args->offset_list,
UINT2NUM(offset));
13395 return ST_CONTINUE;
13399ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
13401 st_table *obj_table = dump->current_buffer->obj_table;
13402 VALUE offset_list = rb_ary_hidden_new(obj_table->num_entries);
13406 args.offset_list = offset_list;
13408 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
13410 IBF_W_ALIGN(ibf_offset_t);
13411 *obj_list_offset = ibf_dump_pos(dump);
13413 st_index_t size = obj_table->num_entries;
13416 for (i=0; i<size; i++) {
13421 *obj_list_size = (
unsigned int)size;
13425ibf_dump_mark(
void *ptr)
13428 rb_gc_mark(dump->global_buffer.str);
13430 rb_mark_set(dump->global_buffer.obj_table);
13431 rb_mark_set(dump->iseq_table);
13435ibf_dump_free(
void *ptr)
13438 if (dump->global_buffer.obj_table) {
13439 st_free_table(dump->global_buffer.obj_table);
13440 dump->global_buffer.obj_table = 0;
13442 if (dump->iseq_table) {
13443 st_free_table(dump->iseq_table);
13444 dump->iseq_table = 0;
13449ibf_dump_memsize(
const void *ptr)
13453 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
13454 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
13460 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
13461 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
13467 dump->global_buffer.obj_table = NULL;
13468 dump->iseq_table = NULL;
13471 dump->global_buffer.obj_table = ibf_dump_object_table_new();
13472 dump->iseq_table = st_init_numtable();
13474 dump->current_buffer = &dump->global_buffer;
13485 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
13486 ISEQ_BODY(iseq)->local_iseq != iseq) {
13489 if (
RTEST(ISEQ_COVERAGE(iseq))) {
13494 ibf_dump_setup(dump, dump_obj);
13496 ibf_dump_write(dump, &header,
sizeof(header));
13497 ibf_dump_iseq(dump, iseq);
13499 header.magic[0] =
'Y';
13500 header.magic[1] =
'A';
13501 header.magic[2] =
'R';
13502 header.magic[3] =
'B';
13503 header.major_version = IBF_MAJOR_VERSION;
13504 header.minor_version = IBF_MINOR_VERSION;
13505 header.endian = IBF_ENDIAN_MARK;
13507 ibf_dump_iseq_list(dump, &header);
13508 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
13509 header.size = ibf_dump_pos(dump);
13512 VALUE opt_str = opt;
13515 ibf_dump_write(dump, ptr, header.extra_size);
13518 header.extra_size = 0;
13521 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
13523 str = dump->global_buffer.str;
13528static const ibf_offset_t *
13529ibf_iseq_list(
const struct ibf_load *load)
13531 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
13535rb_ibf_load_iseq_complete(
rb_iseq_t *iseq)
13539 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
13542 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
13543 iseq->aux.loader.index, offset,
13544 load->header->size);
13546 ibf_load_iseq_each(load, iseq, offset);
13547 ISEQ_COMPILE_DATA_CLEAR(iseq);
13549 rb_iseq_init_trace(iseq);
13550 load->iseq = prev_src_iseq;
13557 rb_ibf_load_iseq_complete((
rb_iseq_t *)iseq);
13565 int iseq_index = (int)(
VALUE)index_iseq;
13568 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
13569 (
void *)index_iseq, (
void *)load->iseq_list);
13571 if (iseq_index == -1) {
13575 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
13578 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
13586 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
13589 iseq->aux.loader.obj = load->loader_obj;
13590 iseq->aux.loader.index = iseq_index;
13592 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
13593 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
13595 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
13597 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
13599 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
13601 rb_ibf_load_iseq_complete(iseq);
13605 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
13606 (
void *)iseq, (
void *)load->iseq);
13614ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
13617 load->loader_obj = loader_obj;
13618 load->global_buffer.buff = bytes;
13619 load->header = header;
13620 load->global_buffer.size = header->size;
13621 load->global_buffer.obj_list_offset = header->global_object_list_offset;
13622 load->global_buffer.obj_list_size = header->global_object_list_size;
13623 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
13624 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
13627 load->current_buffer = &load->global_buffer;
13629 if (size < header->size) {
13632 if (strncmp(header->magic,
"YARB", 4) != 0) {
13635 if (header->major_version != IBF_MAJOR_VERSION ||
13636 header->minor_version != IBF_MINOR_VERSION) {
13638 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
13640 if (header->endian != IBF_ENDIAN_MARK) {
13646 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
13647 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
13648 header->iseq_list_offset);
13650 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
13651 rb_raise(rb_eArgError,
"unaligned object list offset: %u",
13652 load->global_buffer.obj_list_offset);
13663 if (USE_LAZY_LOAD) {
13664 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
13667 ibf_load_setup_bytes(load, loader_obj,
StringValuePtr(str), RSTRING_LEN(str));
13672ibf_loader_mark(
void *ptr)
13675 rb_gc_mark(load->str);
13676 rb_gc_mark(load->iseq_list);
13677 rb_gc_mark(load->global_buffer.obj_list);
13681ibf_loader_free(
void *ptr)
13688ibf_loader_memsize(
const void *ptr)
13695 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
13696 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
13700rb_iseq_ibf_load(
VALUE str)
13706 ibf_load_setup(load, loader_obj, str);
13707 iseq = ibf_load_iseq(load, 0);
13714rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
13720 ibf_load_setup_bytes(load, loader_obj, bytes, size);
13721 iseq = ibf_load_iseq(load, 0);
13728rb_iseq_ibf_load_extra_data(
VALUE str)
13734 ibf_load_setup(load, loader_obj, str);
13735 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
13740#include "prism_compile.c"
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ALIGNOF
Wraps (or simulates) alignof.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_CLASS
Encountered a new class.
#define RUBY_EVENT_NONE
No events.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define NUM2LL
Old name of RB_NUM2LL.
#define REALLOC_N
Old name of RB_REALLOC_N.
#define ALLOCV
Old name of RB_ALLOCV.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define FIX2UINT
Old name of RB_FIX2UINT.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define FIXABLE
Old name of RB_FIXABLE.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_SET
Old name of RB_FL_SET.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
VALUE rb_eNotImpError
NotImplementedError exception.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_cArray
Array class.
VALUE rb_cNumeric
Numeric class.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cRange
Range class.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define INTEGER_PACK_NEGATIVE
Interprets the input as a signed negative number (unpack only).
#define INTEGER_PACK_LSWORD_FIRST
Stores/interprets the least significant word as the first word.
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
int rb_is_attrset_id(ID id)
Classifies the given ID, then sees if it is an attribute writer.
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
VALUE rb_range_new(VALUE beg, VALUE end, int excl)
Creates a new Range.
int rb_reg_options(VALUE re)
Queries the options of the passed regular expression.
VALUE rb_sym_to_s(VALUE sym)
This is an rb_sym2str() + rb_str_dup() combo.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
int len
Length of the buffer.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
#define rb_long2int
Just another name of rb_long2int_inline.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define ALLOCA_N(type, n)
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RB_ALLOCV(v, n)
Identical to RB_ALLOCV_N(), except that it allocates a number of bytes and returns a void* .
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define Data_Wrap_Struct(klass, mark, free, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define DATA_PTR(obj)
Convenient getter macro.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
#define RHASH_SIZE(h)
Queries the size of the hash.
static VALUE RREGEXP_SRC(VALUE rexp)
Convenient getter function.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
static int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
void rb_p(VALUE obj)
Inspects an object.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Internal header for Complex.
Internal header for Rational.
const ID * segments
A null-terminated list of ids, used to represent a constant's path idNULL is used to represent the ::...
This struct represents the overall parser.
This is the struct that holds necessary info for a struct.
struct rb_iseq_constant_body::@152 param
parameter information
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
@ RUBY_T_MASK
Bitmask of ruby_value_type.