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/io.h"
30 #include "internal/numeric.h"
31 #include "internal/object.h"
32 #include "internal/rational.h"
33 #include "internal/re.h"
34 #include "internal/ruby_parser.h"
35 #include "internal/symbol.h"
36 #include "internal/thread.h"
37 #include "internal/variable.h"
43 #include "vm_callinfo.h"
49 #include "insns_info.inc"
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;
127 const 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, nd_line(line_node), nd_node_id(line_node), BIN(insn), 0))
226 #define ADD_SYNTHETIC_INSN(seq, line_no, node_id, insn) \
227 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line_no), (node_id), BIN(insn), 0))
230 #define INSERT_BEFORE_INSN(next, line_no, node_id, insn) \
231 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
234 #define INSERT_AFTER_INSN(prev, line_no, node_id, insn) \
235 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
238 #define ADD_INSN1(seq, line_node, insn, op1) \
239 ADD_ELEM((seq), (LINK_ELEMENT *) \
240 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 1, (VALUE)(op1)))
243 #define INSERT_BEFORE_INSN1(next, line_no, node_id, insn, op1) \
244 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) \
245 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
248 #define INSERT_AFTER_INSN1(prev, line_no, node_id, insn, op1) \
249 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) \
250 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
252 #define LABEL_REF(label) ((label)->refcnt++)
255 #define ADD_INSNL(seq, line_node, insn, label) (ADD_INSN1(seq, line_node, insn, label), LABEL_REF(label))
257 #define ADD_INSN2(seq, line_node, insn, op1, op2) \
258 ADD_ELEM((seq), (LINK_ELEMENT *) \
259 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
261 #define ADD_INSN3(seq, line_node, insn, op1, op2, op3) \
262 ADD_ELEM((seq), (LINK_ELEMENT *) \
263 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
266 #define ADD_SEND(seq, line_node, id, argc) \
267 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
269 #define ADD_SEND_WITH_FLAG(seq, line_node, id, argc, flag) \
270 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)(flag), NULL)
272 #define ADD_SEND_WITH_BLOCK(seq, line_node, id, argc, block) \
273 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
275 #define ADD_CALL_RECEIVER(seq, line_node) \
276 ADD_INSN((seq), (line_node), putself)
278 #define ADD_CALL(seq, line_node, id, argc) \
279 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
281 #define ADD_CALL_WITH_BLOCK(seq, line_node, id, argc, block) \
282 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
284 #define ADD_SEND_R(seq, line_node, id, argc, block, flag, keywords) \
285 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
287 #define ADD_TRACE(seq, event) \
288 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), 0))
289 #define ADD_TRACE_WITH_DATA(seq, event, data) \
290 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), (data)))
292 static void iseq_add_getlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
293 static void iseq_add_setlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
295 #define ADD_GETLOCAL(seq, line_node, idx, level) iseq_add_getlocal(iseq, (seq), (line_node), (idx), (level))
296 #define ADD_SETLOCAL(seq, line_node, idx, level) iseq_add_setlocal(iseq, (seq), (line_node), (idx), (level))
299 #define ADD_LABEL(seq, label) \
300 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
302 #define APPEND_LABEL(seq, before, label) \
303 APPEND_ELEM((seq), (before), (LINK_ELEMENT *) (label))
305 #define ADD_ADJUST(seq, line_node, label) \
306 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), nd_line(line_node)))
308 #define ADD_ADJUST_RESTORE(seq, label) \
309 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
311 #define LABEL_UNREMOVABLE(label) \
312 ((label) ? (LABEL_REF(label), (label)->unremovable=1) : 0)
313 #define ADD_CATCH_ENTRY(type, ls, le, iseqv, lc) do { \
314 VALUE _e = rb_ary_new3(5, (type), \
315 (VALUE)(ls) | 1, (VALUE)(le) | 1, \
316 (VALUE)(iseqv), (VALUE)(lc) | 1); \
317 LABEL_UNREMOVABLE(ls); \
320 if (NIL_P(ISEQ_COMPILE_DATA(iseq)->catch_table_ary)) \
321 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, rb_ary_hidden_new(3)); \
322 rb_ary_push(ISEQ_COMPILE_DATA(iseq)->catch_table_ary, freeze_hide_obj(_e)); \
326 #define COMPILE(anchor, desc, node) \
327 (debug_compile("== " desc "\n", \
328 iseq_compile_each(iseq, (anchor), (node), 0)))
331 #define COMPILE_POPPED(anchor, desc, node) \
332 (debug_compile("== " desc "\n", \
333 iseq_compile_each(iseq, (anchor), (node), 1)))
336 #define COMPILE_(anchor, desc, node, popped) \
337 (debug_compile("== " desc "\n", \
338 iseq_compile_each(iseq, (anchor), (node), (popped))))
340 #define COMPILE_RECV(anchor, desc, node, recv) \
341 (private_recv_p(node) ? \
342 (ADD_INSN(anchor, node, putself), VM_CALL_FCALL) : \
343 COMPILE(anchor, desc, recv) ? 0 : -1)
345 #define OPERAND_AT(insn, idx) \
346 (((INSN*)(insn))->operands[(idx)])
348 #define INSN_OF(insn) \
349 (((INSN*)(insn))->insn_id)
351 #define IS_INSN(link) ((link)->type == ISEQ_ELEMENT_INSN)
352 #define IS_LABEL(link) ((link)->type == ISEQ_ELEMENT_LABEL)
353 #define IS_ADJUST(link) ((link)->type == ISEQ_ELEMENT_ADJUST)
354 #define IS_TRACE(link) ((link)->type == ISEQ_ELEMENT_TRACE)
355 #define IS_INSN_ID(iobj, insn) (INSN_OF(iobj) == BIN(insn))
356 #define IS_NEXT_INSN_ID(link, insn) \
357 ((link)->next && IS_INSN((link)->next) && IS_INSN_ID((link)->next, insn))
365 append_compile_error(const
rb_iseq_t *iseq,
int line, const
char *fmt, ...)
367 VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
368 VALUE file = rb_iseq_path(iseq);
373 err = rb_syntax_error_append(err, file, line, -1, NULL, fmt, args);
375 if (
NIL_P(err_info)) {
376 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err);
379 else if (!err_info) {
390 compile_bug(
rb_iseq_t *iseq,
int line,
const char *fmt, ...)
394 rb_report_bug_valist(rb_iseq_path(iseq), line, fmt, args);
400 #define COMPILE_ERROR append_compile_error
402 #define ERROR_ARGS_AT(n) iseq, nd_line(n),
403 #define ERROR_ARGS ERROR_ARGS_AT(node)
405 #define EXPECT_NODE(prefix, node, ndtype, errval) \
407 const NODE *error_node = (node); \
408 enum node_type error_type = nd_type(error_node); \
409 if (error_type != (ndtype)) { \
410 COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
411 prefix ": " #ndtype " is expected, but %s", \
412 ruby_node_name(error_type)); \
417 #define EXPECT_NODE_NONULL(prefix, parent, ndtype, errval) \
419 COMPILE_ERROR(ERROR_ARGS_AT(parent) \
420 prefix ": must be " #ndtype ", but 0"); \
424 #define UNKNOWN_NODE(prefix, node, errval) \
426 const NODE *error_node = (node); \
427 COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
428 ruby_node_name(nd_type(error_node))); \
435 #define CHECK(sub) if (!(sub)) {BEFORE_RETURN;return COMPILE_NG;}
436 #define NO_CHECK(sub) (void)(sub)
437 #define BEFORE_RETURN
439 #define DECL_ANCHOR(name) \
440 LINK_ANCHOR name[1] = {{{ISEQ_ELEMENT_ANCHOR,},&name[0].anchor}}
441 #define INIT_ANCHOR(name) \
442 ((name->last = &name->anchor)->next = NULL)
445 freeze_hide_obj(
VALUE obj)
448 RBASIC_CLEAR_CLASS(obj);
452 #include "optinsn.inc"
453 #if OPT_INSTRUCTIONS_UNIFICATION
454 #include "optunifs.inc"
459 #define ISEQ_ARG iseq,
460 #define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
463 #define ISEQ_ARG_DECLARE
467 #define gl_node_level ISEQ_COMPILE_DATA(iseq)->node_level
473 static int insn_data_length(
INSN *iobj);
474 static int calc_sp_depth(
int depth,
INSN *iobj);
476 static INSN *new_insn_body(
rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...);
489 static int iseq_set_exception_local_table(
rb_iseq_t *iseq);
493 static int iseq_set_exception_table(
rb_iseq_t *iseq);
494 static int iseq_set_optargs_table(
rb_iseq_t *iseq);
497 static int compile_hash(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped);
504 verify_list(ISEQ_ARG_DECLARE
const char *info,
LINK_ANCHOR *
const anchor)
510 if (!compile_debug)
return;
512 list = anchor->anchor.next;
513 plist = &anchor->anchor;
515 if (plist != list->prev) {
522 if (anchor->last != plist && anchor->last != 0) {
527 rb_bug(
"list verify error: %08x (%s)", flag, info);
532 #define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
539 VALUE *original = rb_iseq_original_iseq(iseq);
541 while (i < ISEQ_BODY(iseq)->iseq_size) {
542 VALUE insn = original[i];
543 const char *types = insn_op_types(insn);
545 for (
int j=0; types[j]; j++) {
546 if (types[j] == TS_CALLDATA) {
550 if (cc != vm_cc_empty()) {
552 rb_bug(
"call cache is not initialized by vm_cc_empty()");
559 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
560 struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
563 if (cc != NULL && cc != vm_cc_empty()) {
565 rb_bug(
"call cache is not initialized by vm_cc_empty()");
577 elem->prev = anchor->last;
578 anchor->last->next = elem;
580 verify_list(
"add", anchor);
590 elem->next = before->next;
591 elem->next->prev = elem;
593 if (before == anchor->last) anchor->last = elem;
594 verify_list(
"add", anchor);
597 #define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
598 #define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
602 branch_coverage_valid_p(
rb_iseq_t *iseq,
int first_line)
604 if (!ISEQ_COVERAGE(iseq))
return 0;
605 if (!ISEQ_BRANCH_COVERAGE(iseq))
return 0;
606 if (first_line <= 0)
return 0;
610 #define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x)))
615 const int first_lineno = loc->beg_pos.lineno, first_column = loc->beg_pos.column;
616 const int last_lineno = loc->end_pos.lineno, last_column = loc->end_pos.column;
631 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return Qundef;
645 if (
NIL_P(branch_base)) {
646 branch_base = setup_branch(loc,
type, structure, key);
659 generate_dummy_line_node(
int lineno,
int node_id)
662 nd_set_line(&dummy, lineno);
663 nd_set_node_id(&dummy, node_id);
670 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return;
685 branch = setup_branch(loc,
type, branches, key);
695 ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
696 ADD_SYNTHETIC_INSN(seq, loc->end_pos.lineno, node_id, nop);
699 #define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
702 validate_label(st_data_t name, st_data_t label, st_data_t arg)
706 if (!lobj->link.next) {
708 COMPILE_ERROR(iseq, lobj->position,
709 "%"PRIsVALUE
": undefined label",
719 st_foreach(labels_table, validate_label, (st_data_t)iseq);
720 st_free_table(labels_table);
724 get_nd_recv(
const NODE *node)
726 switch (nd_type(node)) {
728 return RNODE_CALL(node)->nd_recv;
730 return RNODE_OPCALL(node)->nd_recv;
734 return RNODE_QCALL(node)->nd_recv;
738 return RNODE_ATTRASGN(node)->nd_recv;
740 return RNODE_OP_ASGN1(node)->nd_recv;
742 return RNODE_OP_ASGN2(node)->nd_recv;
744 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
749 get_node_call_nd_mid(
const NODE *node)
751 switch (nd_type(node)) {
753 return RNODE_CALL(node)->nd_mid;
755 return RNODE_OPCALL(node)->nd_mid;
757 return RNODE_FCALL(node)->nd_mid;
759 return RNODE_QCALL(node)->nd_mid;
761 return RNODE_VCALL(node)->nd_mid;
763 return RNODE_ATTRASGN(node)->nd_mid;
765 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
770 get_nd_args(
const NODE *node)
772 switch (nd_type(node)) {
774 return RNODE_CALL(node)->nd_args;
776 return RNODE_OPCALL(node)->nd_args;
778 return RNODE_FCALL(node)->nd_args;
780 return RNODE_QCALL(node)->nd_args;
784 return RNODE_ATTRASGN(node)->nd_args;
786 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
791 get_node_colon_nd_mid(
const NODE *node)
793 switch (nd_type(node)) {
795 return RNODE_COLON2(node)->nd_mid;
797 return RNODE_COLON3(node)->nd_mid;
799 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
804 get_nd_vid(
const NODE *node)
806 switch (nd_type(node)) {
808 return RNODE_LASGN(node)->nd_vid;
810 return RNODE_DASGN(node)->nd_vid;
812 return RNODE_IASGN(node)->nd_vid;
814 return RNODE_CVASGN(node)->nd_vid;
816 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
821 get_nd_value(
const NODE *node)
823 switch (nd_type(node)) {
825 return RNODE_LASGN(node)->nd_value;
827 return RNODE_DASGN(node)->nd_value;
829 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
834 get_string_value(
const NODE *node)
836 switch (nd_type(node)) {
838 return rb_node_str_string_val(node);
840 return rb_node_file_path_val(node);
842 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
852 (*ifunc->func)(iseq, ret, ifunc->data);
854 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
856 CHECK(iseq_setup_insn(iseq, ret));
857 return iseq_setup(iseq, ret);
860 static bool drop_unreachable_return(
LINK_ANCHOR *ret);
869 NO_CHECK(COMPILE(ret,
"nil", node));
870 iseq_set_local_table(iseq, 0, 0);
873 else if (nd_type_p(node, NODE_SCOPE)) {
875 iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl, (
NODE *)RNODE_SCOPE(node)->nd_args);
876 iseq_set_arguments(iseq, ret, (
NODE *)RNODE_SCOPE(node)->nd_args);
878 switch (ISEQ_BODY(iseq)->
type) {
879 case ISEQ_TYPE_BLOCK:
881 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
882 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
884 start->rescued = LABEL_RESCUE_BEG;
885 end->rescued = LABEL_RESCUE_END;
888 ADD_SYNTHETIC_INSN(ret, ISEQ_BODY(iseq)->location.first_lineno, -1, nop);
889 ADD_LABEL(ret, start);
890 CHECK(COMPILE(ret,
"block body", RNODE_SCOPE(node)->nd_body));
893 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
896 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
897 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
900 case ISEQ_TYPE_CLASS:
903 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
905 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
908 case ISEQ_TYPE_METHOD:
910 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
912 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
913 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
915 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
919 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
926 #define INVALID_ISEQ_TYPE(type) \
927 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
928 switch (ISEQ_BODY(iseq)->
type) {
929 case INVALID_ISEQ_TYPE(
METHOD);
930 case INVALID_ISEQ_TYPE(CLASS);
931 case INVALID_ISEQ_TYPE(BLOCK);
932 case INVALID_ISEQ_TYPE(EVAL);
933 case INVALID_ISEQ_TYPE(MAIN);
934 case INVALID_ISEQ_TYPE(TOP);
935 #undef INVALID_ISEQ_TYPE
936 case ISEQ_TYPE_RESCUE:
937 iseq_set_exception_local_table(iseq);
938 CHECK(COMPILE(ret,
"rescue", node));
940 case ISEQ_TYPE_ENSURE:
941 iseq_set_exception_local_table(iseq);
942 CHECK(COMPILE_POPPED(ret,
"ensure", node));
944 case ISEQ_TYPE_PLAIN:
945 CHECK(COMPILE(ret,
"ensure", node));
948 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
951 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
956 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
957 NODE dummy_line_node = generate_dummy_line_node(0, -1);
958 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
959 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
961 else if (!drop_unreachable_return(ret)) {
962 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
966 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
967 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
968 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
969 validate_labels(iseq, labels_table);
972 CHECK(iseq_setup_insn(iseq, ret));
973 return iseq_setup(iseq, ret);
977 rb_iseq_translate_threaded_code(
rb_iseq_t *iseq)
979 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
980 const void *
const *table = rb_vm_get_insns_address_table();
982 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
984 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
985 int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
986 int len = insn_len(insn);
987 encoded[i] = (
VALUE)table[insn];
994 rb_yjit_live_iseq_count++;
995 rb_yjit_iseq_alloc_count++;
1002 rb_iseq_original_iseq(
const rb_iseq_t *iseq)
1004 VALUE *original_code;
1006 if (ISEQ_ORIGINAL_ISEQ(iseq))
return ISEQ_ORIGINAL_ISEQ(iseq);
1007 original_code = ISEQ_ORIGINAL_ISEQ_ALLOC(iseq, ISEQ_BODY(iseq)->iseq_size);
1008 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
1010 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1014 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1015 const void *addr = (
const void *)original_code[i];
1016 const int insn = rb_vm_insn_addr2insn(addr);
1018 original_code[i] = insn;
1019 i += insn_len(insn);
1023 return original_code;
1036 #if defined(__sparc) && SIZEOF_VOIDP == 4 && defined(__GNUC__)
1037 #define STRICT_ALIGNMENT
1043 #if defined(__OpenBSD__)
1044 #include <sys/endian.h>
1045 #ifdef __STRICT_ALIGNMENT
1046 #define STRICT_ALIGNMENT
1050 #ifdef STRICT_ALIGNMENT
1051 #if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
1052 #define ALIGNMENT_SIZE SIZEOF_LONG_LONG
1054 #define ALIGNMENT_SIZE SIZEOF_VALUE
1056 #define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
1057 #define ALIGNMENT_SIZE_MASK PADDING_SIZE_MAX
1060 #define PADDING_SIZE_MAX 0
1063 #ifdef STRICT_ALIGNMENT
1066 calc_padding(
void *
ptr,
size_t size)
1071 mis = (size_t)
ptr & ALIGNMENT_SIZE_MASK;
1073 padding = ALIGNMENT_SIZE - mis;
1079 #if ALIGNMENT_SIZE > SIZEOF_VALUE
1080 if (size ==
sizeof(
VALUE) && padding ==
sizeof(
VALUE)) {
1094 #ifdef STRICT_ALIGNMENT
1095 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1097 const size_t padding = 0;
1101 if (storage->pos + size + padding > storage->size) {
1102 unsigned int alloc_size = storage->size;
1104 while (alloc_size < size + PADDING_SIZE_MAX) {
1108 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
1110 storage = *arena = storage->next;
1113 storage->size = alloc_size;
1114 #ifdef STRICT_ALIGNMENT
1115 padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1119 #ifdef STRICT_ALIGNMENT
1120 storage->pos += (int)padding;
1123 ptr = (
void *)&storage->buff[storage->pos];
1124 storage->pos += (
int)size;
1129 compile_data_alloc(
rb_iseq_t *iseq,
size_t size)
1132 return compile_data_alloc_with_arena(arena, size);
1135 static inline void *
1136 compile_data_alloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1139 return compile_data_alloc(iseq, size);
1142 static inline void *
1143 compile_data_calloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1146 void *p = compile_data_alloc(iseq, size);
1152 compile_data_alloc_insn(
rb_iseq_t *iseq)
1155 return (
INSN *)compile_data_alloc_with_arena(arena,
sizeof(
INSN));
1159 compile_data_alloc_label(
rb_iseq_t *iseq)
1161 return (
LABEL *)compile_data_alloc(iseq,
sizeof(
LABEL));
1165 compile_data_alloc_adjust(
rb_iseq_t *iseq)
1167 return (
ADJUST *)compile_data_alloc(iseq,
sizeof(
ADJUST));
1171 compile_data_alloc_trace(
rb_iseq_t *iseq)
1173 return (
TRACE *)compile_data_alloc(iseq,
sizeof(
TRACE));
1182 elem2->next = elem1->next;
1183 elem2->prev = elem1;
1184 elem1->next = elem2;
1186 elem2->next->prev = elem2;
1196 elem2->prev = elem1->prev;
1197 elem2->next = elem1;
1198 elem1->prev = elem2;
1200 elem2->prev->next = elem2;
1210 elem2->prev = elem1->prev;
1211 elem2->next = elem1->next;
1213 elem1->prev->next = elem2;
1216 elem1->next->prev = elem2;
1223 elem->prev->next = elem->next;
1225 elem->next->prev = elem->prev;
1232 return anchor->anchor.next;
1238 return anchor->last;
1245 switch (elem->type) {
1246 case ISEQ_ELEMENT_INSN:
1247 case ISEQ_ELEMENT_ADJUST:
1257 LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1259 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1260 if (first_insn != NULL &&
1261 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1270 LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1272 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1290 if (anc2->anchor.next) {
1293 anc1->last->next = anc2->anchor.next;
1294 anc2->anchor.next->prev = anc1->last;
1295 anc1->last = anc2->last;
1300 verify_list(
"append", anc1);
1303 #define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1312 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1313 (
void *)anchor->anchor.next, (
void *)anchor->last);
1315 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1316 (
void *)list->prev, (
int)list->type);
1321 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1322 verify_list(
"debug list", anchor);
1325 #define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1328 #define debug_list(anc, cur) ((void)0)
1334 TRACE *trace = compile_data_alloc_trace(iseq);
1336 trace->link.type = ISEQ_ELEMENT_TRACE;
1337 trace->link.next = NULL;
1338 trace->event = event;
1345 new_label_body(
rb_iseq_t *iseq,
long line)
1347 LABEL *labelobj = compile_data_alloc_label(iseq);
1349 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1350 labelobj->link.next = 0;
1352 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1353 labelobj->sc_state = 0;
1355 labelobj->refcnt = 0;
1357 labelobj->rescued = LABEL_RESCUE_NONE;
1358 labelobj->unremovable = 0;
1359 labelobj->position = -1;
1366 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1367 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1368 adjust->link.next = 0;
1369 adjust->label = label;
1370 adjust->line_no = line;
1371 LABEL_UNREMOVABLE(label);
1378 const char *types = insn_op_types(insn->insn_id);
1379 for (
int j = 0; types[j]; j++) {
1380 char type = types[j];
1387 func(OPERAND_AT(insn, j), data);
1396 iseq_insn_each_object_write_barrier(
VALUE obj,
VALUE iseq)
1402 new_insn_core(
rb_iseq_t *iseq,
int line_no,
int node_id,
int insn_id,
int argc,
VALUE *argv)
1404 INSN *iobj = compile_data_alloc_insn(iseq);
1408 iobj->link.type = ISEQ_ELEMENT_INSN;
1409 iobj->link.next = 0;
1410 iobj->insn_id = insn_id;
1411 iobj->insn_info.line_no = line_no;
1412 iobj->insn_info.node_id = node_id;
1413 iobj->insn_info.events = 0;
1414 iobj->operands = argv;
1415 iobj->operand_size = argc;
1418 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1424 new_insn_body(
rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...)
1426 VALUE *operands = 0;
1430 va_start(argv, argc);
1431 operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
1432 for (i = 0; i < argc; i++) {
1438 return new_insn_core(iseq, line_no, node_id, insn_id, argc, operands);
1444 VM_ASSERT(argc >= 0);
1447 flag |= VM_CALL_KWARG;
1448 argc += kw_arg->keyword_len;
1451 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING))
1452 && !has_blockiseq) {
1453 flag |= VM_CALL_ARGS_SIMPLE;
1456 ISEQ_BODY(iseq)->ci_size++;
1457 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1465 VALUE *operands = compile_data_calloc2(iseq,
sizeof(
VALUE), 2);
1468 operands[1] = (
VALUE)blockiseq;
1475 if (vm_ci_flag((
struct rb_callinfo *)ci) & VM_CALL_FORWARDING) {
1476 insn = new_insn_core(iseq, line_no, node_id, BIN(sendforward), 2, operands);
1479 insn = new_insn_core(iseq, line_no, node_id, BIN(send), 2, operands);
1492 VALUE ast_value = rb_ruby_ast_new(node);
1494 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1495 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1496 ret_iseq = rb_iseq_new_with_opt(ast_value, name,
1497 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1499 isolated_depth ? isolated_depth + 1 : 0,
1500 type, ISEQ_COMPILE_DATA(iseq)->option,
1501 ISEQ_BODY(iseq)->variable.script_lines);
1502 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1512 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1513 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1514 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1515 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1516 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1524 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1525 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1526 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1527 set_catch_except_p((
rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1550 while (pos < body->iseq_size) {
1551 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1552 if (insn == BIN(
throw)) {
1553 set_catch_except_p(iseq);
1556 pos += insn_len(insn);
1562 for (i = 0; i < ct->size; i++) {
1564 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1565 if (entry->type != CATCH_TYPE_BREAK
1566 && entry->type != CATCH_TYPE_NEXT
1567 && entry->type != CATCH_TYPE_REDO) {
1569 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1576 iseq_insert_nop_between_end_and_cont(
rb_iseq_t *iseq)
1578 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1579 if (
NIL_P(catch_table_ary))
return;
1580 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1582 for (i = 0; i < tlen; i++) {
1588 enum rb_catch_type ct = (
enum rb_catch_type)(
ptr[0] & 0xffff);
1590 if (ct != CATCH_TYPE_BREAK
1591 && ct != CATCH_TYPE_NEXT
1592 && ct != CATCH_TYPE_REDO) {
1594 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1596 INSN *nop = new_insn_core(iseq, 0, -1, BIN(nop), 0, 0);
1597 ELEM_INSERT_NEXT(end, &nop->link);
1610 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1615 if (compile_debug > 5)
1616 dump_disasm_list(FIRST_ELEMENT(anchor));
1618 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1619 iseq_optimize(iseq, anchor);
1621 if (compile_debug > 5)
1622 dump_disasm_list(FIRST_ELEMENT(anchor));
1624 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1625 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1626 iseq_insns_unification(iseq, anchor);
1627 if (compile_debug > 5)
1628 dump_disasm_list(FIRST_ELEMENT(anchor));
1631 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1632 iseq_insert_nop_between_end_and_cont(iseq);
1633 if (compile_debug > 5)
1634 dump_disasm_list(FIRST_ELEMENT(anchor));
1642 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1645 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1646 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1647 if (compile_debug > 5)
1648 dump_disasm_list(FIRST_ELEMENT(anchor));
1650 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1651 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1653 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1654 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1656 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1657 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1659 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1661 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1663 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1665 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1666 xfree(ISEQ_BODY(iseq)->catch_table);
1667 ISEQ_BODY(iseq)->catch_table = NULL;
1670 #if VM_INSN_INFO_TABLE_IMPL == 2
1671 if (ISEQ_BODY(iseq)->insns_info.succ_index_table == NULL) {
1672 debugs(
"[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
1673 rb_iseq_insns_info_encode_positions(iseq);
1677 if (compile_debug > 1) {
1678 VALUE str = rb_iseq_disasm(iseq);
1681 verify_call_cache(iseq);
1682 debugs(
"[compile step: finish]\n");
1688 iseq_set_exception_local_table(
rb_iseq_t *iseq)
1690 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1691 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1699 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1701 iseq = ISEQ_BODY(iseq)->parent_iseq;
1707 get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1711 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1712 if (ISEQ_BODY(iseq)->local_table[i] == id) {
1722 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1725 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1726 "get_local_var_idx: %d", idx);
1733 get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1735 int lv = 0, idx = -1;
1736 const rb_iseq_t *
const topmost_iseq = iseq;
1739 idx = get_dyna_var_idx_at_raw(iseq,
id);
1743 iseq = ISEQ_BODY(iseq)->parent_iseq;
1748 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1749 "get_dyna_var_idx: -1");
1753 *ls = ISEQ_BODY(iseq)->local_table_size;
1758 iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1762 iseq = ISEQ_BODY(iseq)->parent_iseq;
1765 body = ISEQ_BODY(iseq);
1766 if (body->local_iseq == iseq &&
1767 body->
param.flags.has_block &&
1768 body->local_table_size - body->
param.block_start == idx) {
1777 iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1780 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1781 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1792 access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1794 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1796 if (isolated_depth && level >= isolated_depth) {
1798 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1801 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable '%s' from isolated Proc",
rb_id2name(
id));
1805 for (
int i=0; i<level; i++) {
1807 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1810 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1813 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1814 if (write && !val) {
1815 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1819 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1822 iseq = ISEQ_BODY(iseq)->parent_iseq;
1827 iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1829 for (
int i=0; i<level; i++) {
1830 iseq = ISEQ_BODY(iseq)->parent_iseq;
1833 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1841 if (iseq_local_block_param_p(iseq, idx, level)) {
1842 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1845 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1847 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1853 if (iseq_local_block_param_p(iseq, idx, level)) {
1854 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1857 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1859 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
1868 if (body->
param.flags.has_opt ||
1869 body->
param.flags.has_post ||
1870 body->
param.flags.has_rest ||
1871 body->
param.flags.has_block ||
1872 body->
param.flags.has_kw ||
1873 body->
param.flags.has_kwrest) {
1875 if (body->
param.flags.has_block) {
1876 body->
param.size = body->
param.block_start + 1;
1878 else if (body->
param.flags.has_kwrest) {
1879 body->
param.size = body->
param.keyword->rest_start + 1;
1881 else if (body->
param.flags.has_kw) {
1882 body->
param.size = body->
param.keyword->bits_start + 1;
1884 else if (body->
param.flags.has_post) {
1887 else if (body->
param.flags.has_rest) {
1888 body->
param.size = body->
param.rest_start + 1;
1890 else if (body->
param.flags.has_opt) {
1908 struct rb_iseq_param_keyword *keyword;
1911 int kw = 0, rkw = 0, di = 0, i;
1913 body->
param.flags.has_kw = TRUE;
1914 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
1918 node = node->nd_next;
1921 keyword->bits_start = arg_size++;
1923 node = args->kw_args;
1925 const NODE *val_node = get_nd_value(node->nd_body);
1928 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
1932 switch (nd_type(val_node)) {
1934 dv = rb_node_sym_string_val(val_node);
1937 dv = rb_node_regx_string_val(val_node);
1940 dv = rb_node_line_lineno_val(val_node);
1943 dv = rb_node_integer_literal_val(val_node);
1946 dv = rb_node_float_literal_val(val_node);
1949 dv = rb_node_rational_literal_val(val_node);
1951 case NODE_IMAGINARY:
1952 dv = rb_node_imaginary_literal_val(val_node);
1955 dv = rb_node_encoding_val(val_node);
1967 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
1971 keyword->num = ++di;
1975 node = node->nd_next;
1980 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
1981 ID kw_id = iseq->body->local_table[arg_size];
1982 keyword->rest_start = arg_size++;
1983 body->
param.flags.has_kwrest = TRUE;
1985 if (kw_id == idPow) body->
param.flags.anon_kwrest = TRUE;
1987 keyword->required_num = rkw;
1988 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
1993 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
1995 if (dv == complex_mark) dv =
Qundef;
1999 keyword->default_values = dvs;
2008 if (!body->
param.flags.use_block) {
2009 body->
param.flags.use_block = 1;
2014 st_data_t key = (st_data_t)
rb_intern_str(body->location.label);
2015 st_insert(vm->unused_block_warning_table, key, 1);
2023 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2027 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2033 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2035 body->
param.flags.ruby2_keywords = args->ruby2_keywords;
2036 body->
param.lead_num = arg_size = (int)args->pre_args_num;
2037 if (body->
param.lead_num > 0) body->
param.flags.has_lead = TRUE;
2038 debugs(
" - argc: %d\n", body->
param.lead_num);
2040 rest_id = args->rest_arg;
2041 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2045 block_id = args->block_arg;
2047 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2049 if (optimized_forward) {
2054 if (args->opt_args) {
2062 label = NEW_LABEL(nd_line(RNODE(node)));
2064 ADD_LABEL(optargs, label);
2065 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2066 node = node->nd_next;
2071 label = NEW_LABEL(nd_line(node_args));
2073 ADD_LABEL(optargs, label);
2078 for (j = 0; j < i+1; j++) {
2083 body->
param.flags.has_opt = TRUE;
2084 body->
param.opt_num = i;
2085 body->
param.opt_table = opt_table;
2090 body->
param.rest_start = arg_size++;
2091 body->
param.flags.has_rest = TRUE;
2092 if (rest_id ==
'*') body->
param.flags.anon_rest = TRUE;
2096 if (args->first_post_arg) {
2097 body->
param.post_start = arg_size;
2098 body->
param.post_num = args->post_args_num;
2099 body->
param.flags.has_post = TRUE;
2100 arg_size += args->post_args_num;
2102 if (body->
param.flags.has_rest) {
2103 body->
param.post_start = body->
param.rest_start + 1;
2107 if (args->kw_args) {
2108 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2110 else if (args->kw_rest_arg && !optimized_forward) {
2111 ID kw_id = iseq->body->local_table[arg_size];
2112 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2113 keyword->rest_start = arg_size++;
2114 body->
param.keyword = keyword;
2115 body->
param.flags.has_kwrest = TRUE;
2117 static ID anon_kwrest = 0;
2118 if (!anon_kwrest) anon_kwrest =
rb_intern(
"**");
2119 if (kw_id == anon_kwrest) body->
param.flags.anon_kwrest = TRUE;
2121 else if (args->no_kwarg) {
2122 body->
param.flags.accepts_no_kwarg = TRUE;
2126 body->
param.block_start = arg_size++;
2127 body->
param.flags.has_block = TRUE;
2128 iseq_set_use_block(iseq);
2132 if (optimized_forward) {
2133 body->
param.flags.use_block = 1;
2134 body->
param.flags.forwardable = TRUE;
2138 iseq_calc_param_size(iseq);
2139 body->
param.size = arg_size;
2141 if (args->pre_init) {
2142 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2144 if (args->post_init) {
2145 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2148 if (body->type == ISEQ_TYPE_BLOCK) {
2149 if (body->
param.flags.has_opt == FALSE &&
2150 body->
param.flags.has_post == FALSE &&
2151 body->
param.flags.has_rest == FALSE &&
2152 body->
param.flags.has_kw == FALSE &&
2153 body->
param.flags.has_kwrest == FALSE) {
2155 if (body->
param.lead_num == 1 && last_comma == 0) {
2157 body->
param.flags.ambiguous_param0 = TRUE;
2169 unsigned int size = tbl ? tbl->size : 0;
2170 unsigned int offset = 0;
2173 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2178 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2186 MEMCPY(ids, tbl->ids + offset,
ID, size);
2187 ISEQ_BODY(iseq)->local_table = ids;
2189 ISEQ_BODY(iseq)->local_table_size = size;
2191 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2203 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2206 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2209 else if (tlit != tval) {
2227 return rb_float_cmp(lit, val);
2230 const struct RRational *rat1 = RRATIONAL(val);
2231 const struct RRational *rat2 = RRATIONAL(lit);
2232 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2235 const struct RComplex *comp1 = RCOMPLEX(val);
2236 const struct RComplex *comp2 = RCOMPLEX(lit);
2237 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2240 return rb_reg_equal(val, lit) ? 0 : -1;
2248 rb_iseq_cdhash_hash(
VALUE a)
2250 switch (OBJ_BUILTIN_TYPE(a)) {
2253 return (st_index_t)a;
2261 return rb_rational_hash(a);
2263 return rb_complex_hash(a);
2273 rb_iseq_cdhash_hash,
2295 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2302 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2304 if (rb_id_table_lookup(tbl,
id,&val)) {
2309 tbl = rb_id_table_create(1);
2310 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2312 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2313 rb_id_table_insert(tbl,
id,val);
2317 #define BADINSN_DUMP(anchor, list, dest) \
2318 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2320 #define BADINSN_ERROR \
2321 (xfree(generated_iseq), \
2322 xfree(insns_info), \
2323 BADINSN_DUMP(anchor, list, NULL), \
2329 int stack_max = 0, sp = 0, line = 0;
2332 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2333 if (IS_LABEL(list)) {
2339 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2340 switch (list->type) {
2341 case ISEQ_ELEMENT_INSN:
2349 sp = calc_sp_depth(sp, iobj);
2351 BADINSN_DUMP(anchor, list, NULL);
2352 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2353 "argument stack underflow (%d)", sp);
2356 if (sp > stack_max) {
2360 line = iobj->insn_info.line_no;
2362 operands = iobj->operands;
2363 insn = iobj->insn_id;
2364 types = insn_op_types(insn);
2365 len = insn_len(insn);
2368 if (iobj->operand_size !=
len - 1) {
2370 BADINSN_DUMP(anchor, list, NULL);
2371 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2372 "operand size miss! (%d for %d)",
2373 iobj->operand_size,
len - 1);
2377 for (j = 0; types[j]; j++) {
2378 if (types[j] == TS_OFFSET) {
2382 BADINSN_DUMP(anchor, list, NULL);
2383 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2384 "unknown label: "LABEL_FORMAT, lobj->label_no);
2387 if (lobj->sp == -1) {
2390 else if (lobj->sp != sp) {
2391 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2393 lobj->label_no, lobj->sp, sp);
2399 case ISEQ_ELEMENT_LABEL:
2402 if (lobj->sp == -1) {
2406 if (lobj->sp != sp) {
2407 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2409 lobj->label_no, lobj->sp, sp);
2415 case ISEQ_ELEMENT_TRACE:
2420 case ISEQ_ELEMENT_ADJUST:
2425 sp = adjust->label ? adjust->label->sp : 0;
2426 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2427 BADINSN_DUMP(anchor, list, NULL);
2428 COMPILE_ERROR(iseq, adjust->line_no,
2429 "iseq_set_sequence: adjust bug %d < %d",
2436 BADINSN_DUMP(anchor, list, NULL);
2437 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2446 int insns_info_index,
int code_index,
const INSN *iobj)
2448 if (insns_info_index == 0 ||
2449 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2450 #ifdef USE_ISEQ_NODE_ID
2451 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2453 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2454 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2455 #ifdef USE_ISEQ_NODE_ID
2456 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2458 insns_info[insns_info_index].events = iobj->insn_info.events;
2459 positions[insns_info_index] = code_index;
2467 int insns_info_index,
int code_index,
const ADJUST *adjust)
2469 insns_info[insns_info_index].line_no = adjust->line_no;
2470 insns_info[insns_info_index].node_id = -1;
2471 insns_info[insns_info_index].events = 0;
2472 positions[insns_info_index] = code_index;
2477 array_to_idlist(
VALUE arr)
2482 for (
int i = 0; i < size; i++) {
2491 idlist_to_array(
const ID *ids)
2508 unsigned int *positions;
2510 VALUE *generated_iseq;
2514 int insn_num, code_index, insns_info_index, sp = 0;
2515 int stack_max = fix_sp_depth(iseq, anchor);
2517 if (stack_max < 0)
return COMPILE_NG;
2520 insn_num = code_index = 0;
2521 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2522 switch (list->type) {
2523 case ISEQ_ELEMENT_INSN:
2527 sp = calc_sp_depth(sp, iobj);
2529 events = iobj->insn_info.events |= events;
2530 if (ISEQ_COVERAGE(iseq)) {
2531 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2532 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2533 int line = iobj->insn_info.line_no - 1;
2534 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2538 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2539 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2545 code_index += insn_data_length(iobj);
2550 case ISEQ_ELEMENT_LABEL:
2553 lobj->position = code_index;
2554 if (lobj->sp != sp) {
2555 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2557 lobj->label_no, lobj->sp, sp);
2562 case ISEQ_ELEMENT_TRACE:
2565 events |= trace->event;
2566 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2569 case ISEQ_ELEMENT_ADJUST:
2572 if (adjust->line_no != -1) {
2574 sp = adjust->label ? adjust->label->sp : 0;
2575 if (orig_sp - sp > 0) {
2576 if (orig_sp - sp > 1) code_index++;
2590 positions =
ALLOC_N(
unsigned int, insn_num);
2591 if (ISEQ_IS_SIZE(body)) {
2595 body->is_entries = NULL;
2598 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2605 iseq_bits_t * mark_offset_bits;
2606 int code_size = code_index;
2608 iseq_bits_t tmp[1] = {0};
2609 bool needs_bitmap =
false;
2611 if (ISEQ_MBITS_BUFLEN(code_index) == 1) {
2612 mark_offset_bits = tmp;
2615 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(code_index));
2618 list = FIRST_ELEMENT(anchor);
2619 insns_info_index = code_index = sp = 0;
2622 switch (list->type) {
2623 case ISEQ_ELEMENT_INSN:
2631 sp = calc_sp_depth(sp, iobj);
2633 operands = iobj->operands;
2634 insn = iobj->insn_id;
2635 generated_iseq[code_index] = insn;
2636 types = insn_op_types(insn);
2637 len = insn_len(insn);
2639 for (j = 0; types[j]; j++) {
2640 char type = types[j];
2648 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2653 VALUE map = operands[j];
2656 data.pos = code_index;
2660 rb_hash_rehash(map);
2661 freeze_hide_obj(map);
2662 generated_iseq[code_index + 1 + j] = map;
2663 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2665 needs_bitmap =
true;
2670 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2675 VALUE v = operands[j];
2676 generated_iseq[code_index + 1 + j] = v;
2680 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2681 needs_bitmap =
true;
2688 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2689 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2690 if (UNLIKELY(ic_index >= body->ic_size)) {
2691 BADINSN_DUMP(anchor, &iobj->link, 0);
2692 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2693 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2694 ic_index, ISEQ_IS_SIZE(body));
2697 ic->
segments = array_to_idlist(operands[j]);
2699 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2704 unsigned int ic_index =
FIX2UINT(operands[j]);
2706 IVC cache = ((
IVC)&body->is_entries[ic_index]);
2708 if (insn == BIN(setinstancevariable)) {
2709 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2712 cache->iv_set_name = 0;
2715 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
2720 unsigned int ic_index =
FIX2UINT(operands[j]);
2721 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2722 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2723 BADINSN_DUMP(anchor, &iobj->link, 0);
2724 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2725 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2726 ic_index, ISEQ_IS_SIZE(body));
2728 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2735 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2736 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2738 cd->cc = vm_cc_empty();
2739 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2743 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2746 generated_iseq[code_index + 1 + j] = operands[j];
2749 generated_iseq[code_index + 1 + j] = operands[j];
2752 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2753 "unknown operand type: %c",
type);
2757 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2761 case ISEQ_ELEMENT_LABEL:
2764 if (lobj->sp != sp) {
2765 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2767 lobj->label_no, lobj->sp, sp);
2772 case ISEQ_ELEMENT_ADJUST:
2777 if (adjust->label) {
2778 sp = adjust->label->sp;
2784 if (adjust->line_no != -1) {
2785 const int diff = orig_sp - sp;
2787 if (insns_info_index == 0) {
2788 COMPILE_ERROR(iseq, adjust->line_no,
2789 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2791 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2794 generated_iseq[code_index++] = BIN(adjuststack);
2795 generated_iseq[code_index++] = orig_sp - sp;
2797 else if (diff == 1) {
2798 generated_iseq[code_index++] = BIN(pop);
2800 else if (diff < 0) {
2801 int label_no = adjust->label ? adjust->label->label_no : -1;
2802 xfree(generated_iseq);
2805 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
2806 xfree(mark_offset_bits);
2808 debug_list(anchor, list);
2809 COMPILE_ERROR(iseq, adjust->line_no,
2810 "iseq_set_sequence: adjust bug to %d %d < %d",
2811 label_no, orig_sp, sp);
2824 body->iseq_encoded = (
void *)generated_iseq;
2825 body->iseq_size = code_index;
2826 body->stack_max = stack_max;
2828 if (ISEQ_MBITS_BUFLEN(body->iseq_size) == 1) {
2829 body->mark_bits.single = mark_offset_bits[0];
2833 body->mark_bits.list = mark_offset_bits;
2836 body->mark_bits.list = 0;
2842 body->insns_info.body = insns_info;
2843 body->insns_info.positions = positions;
2846 body->insns_info.body = insns_info;
2847 REALLOC_N(positions,
unsigned int, insns_info_index);
2848 body->insns_info.positions = positions;
2849 body->insns_info.size = insns_info_index;
2855 label_get_position(
LABEL *lobj)
2857 return lobj->position;
2861 label_get_sp(
LABEL *lobj)
2867 iseq_set_exception_table(
rb_iseq_t *iseq)
2870 unsigned int tlen, i;
2873 ISEQ_BODY(iseq)->catch_table = NULL;
2875 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
2876 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
2884 for (i = 0; i < table->size; i++) {
2887 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
2888 entry->type = (
enum rb_catch_type)(
ptr[0] & 0xffff);
2889 pos = label_get_position((
LABEL *)(
ptr[1] & ~1));
2891 entry->start = (
unsigned int)pos;
2892 pos = label_get_position((
LABEL *)(
ptr[2] & ~1));
2894 entry->end = (
unsigned int)pos;
2901 entry->cont = label_get_position(lobj);
2902 entry->sp = label_get_sp(lobj);
2905 if (entry->type == CATCH_TYPE_RESCUE ||
2906 entry->type == CATCH_TYPE_BREAK ||
2907 entry->type == CATCH_TYPE_NEXT) {
2916 ISEQ_BODY(iseq)->catch_table = table;
2917 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
2938 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
2940 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
2941 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
2942 opt_table[i] = label_get_position((
LABEL *)opt_table[i]);
2949 get_destination_insn(
INSN *iobj)
2955 list = lobj->link.next;
2957 switch (list->type) {
2958 case ISEQ_ELEMENT_INSN:
2959 case ISEQ_ELEMENT_ADJUST:
2961 case ISEQ_ELEMENT_LABEL:
2964 case ISEQ_ELEMENT_TRACE:
2967 events |= trace->event;
2975 if (list && IS_INSN(list)) {
2977 iobj->insn_info.events |= events;
2983 get_next_insn(
INSN *iobj)
2988 if (IS_INSN(list) || IS_ADJUST(list)) {
2997 get_prev_insn(
INSN *iobj)
3002 if (IS_INSN(list) || IS_ADJUST(list)) {
3011 unref_destination(
INSN *iobj,
int pos)
3013 LABEL *lobj = (
LABEL *)OPERAND_AT(iobj, pos);
3015 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3019 replace_destination(
INSN *dobj,
INSN *nobj)
3021 VALUE n = OPERAND_AT(nobj, 0);
3024 if (dl == nl)
return false;
3027 OPERAND_AT(dobj, 0) = n;
3028 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3033 find_destination(
INSN *i)
3035 int pos,
len = insn_len(i->insn_id);
3036 for (pos = 0; pos <
len; ++pos) {
3037 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3038 return (
LABEL *)OPERAND_AT(i, pos);
3048 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
3051 unref_counts =
ALLOCA_N(
int, nlabels);
3052 MEMZERO(unref_counts,
int, nlabels);
3057 if (IS_INSN_ID(i, leave)) {
3061 else if ((lab = find_destination((
INSN *)i)) != 0) {
3062 unref_counts[lab->label_no]++;
3065 else if (IS_LABEL(i)) {
3067 if (lab->unremovable)
return 0;
3068 if (lab->refcnt > unref_counts[lab->label_no]) {
3069 if (i == first)
return 0;
3074 else if (IS_TRACE(i)) {
3077 else if (IS_ADJUST(i)) {
3081 }
while ((i = i->next) != 0);
3086 VALUE insn = INSN_OF(i);
3087 int pos,
len = insn_len(insn);
3088 for (pos = 0; pos <
len; ++pos) {
3089 switch (insn_op_types(insn)[pos]) {
3091 unref_destination((
INSN *)i, pos);
3100 }
while ((i != end) && (i = i->next) != 0);
3107 switch (OPERAND_AT(iobj, 0)) {
3109 ELEM_REMOVE(&iobj->link);
3112 ELEM_REMOVE(&iobj->link);
3115 iobj->insn_id = BIN(adjuststack);
3121 is_frozen_putstring(
INSN *insn,
VALUE *op)
3123 if (IS_INSN_ID(insn, putstring) || IS_INSN_ID(insn, putchilledstring)) {
3124 *op = OPERAND_AT(insn, 0);
3127 else if (IS_INSN_ID(insn, putobject)) {
3128 *op = OPERAND_AT(insn, 0);
3159 INSN *niobj, *ciobj, *dup = 0;
3163 switch (INSN_OF(iobj)) {
3164 case BIN(putstring):
3165 case BIN(putchilledstring):
3171 case BIN(putobject):
3174 default:
return FALSE;
3177 ciobj = (
INSN *)get_next_insn(iobj);
3178 if (IS_INSN_ID(ciobj, jump)) {
3179 ciobj = (
INSN *)get_next_insn((
INSN*)OPERAND_AT(ciobj, 0));
3181 if (IS_INSN_ID(ciobj, dup)) {
3182 ciobj = (
INSN *)get_next_insn(dup = ciobj);
3184 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3185 niobj = (
INSN *)get_next_insn(ciobj);
3190 switch (INSN_OF(niobj)) {
3192 if (OPERAND_AT(ciobj, 0) ==
type) {
3193 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3196 case BIN(branchunless):
3197 if (OPERAND_AT(ciobj, 0) !=
type) {
3198 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3204 line = ciobj->insn_info.line_no;
3205 node_id = ciobj->insn_info.node_id;
3207 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3208 dest = (
LABEL *)niobj->link.next;
3211 dest = NEW_LABEL(line);
3212 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3215 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3217 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3224 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3225 vm_ci_flag(ci) | add,
3235 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3243 #define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3251 optimize_checktype(iseq, iobj);
3253 if (IS_INSN_ID(iobj, jump)) {
3254 INSN *niobj, *diobj, *piobj;
3255 diobj = (
INSN *)get_destination_insn(iobj);
3256 niobj = (
INSN *)get_next_insn(iobj);
3258 if (diobj == niobj) {
3265 unref_destination(iobj, 0);
3266 ELEM_REMOVE(&iobj->link);
3269 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3270 IS_INSN_ID(diobj, jump) &&
3271 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3272 diobj->insn_info.events == 0) {
3283 if (replace_destination(iobj, diobj)) {
3284 remove_unreachable_chunk(iseq, iobj->link.next);
3288 else if (IS_INSN_ID(diobj, leave)) {
3301 unref_destination(iobj, 0);
3302 iobj->insn_id = BIN(leave);
3303 iobj->operand_size = 0;
3304 iobj->insn_info = diobj->insn_info;
3307 else if (IS_INSN(iobj->link.prev) &&
3308 (piobj = (
INSN *)iobj->link.prev) &&
3309 (IS_INSN_ID(piobj, branchif) ||
3310 IS_INSN_ID(piobj, branchunless))) {
3311 INSN *pdiobj = (
INSN *)get_destination_insn(piobj);
3312 if (niobj == pdiobj) {
3313 int refcnt = IS_LABEL(piobj->link.next) ?
3314 ((
LABEL *)piobj->link.next)->refcnt : 0;
3329 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3330 ? BIN(branchunless) : BIN(branchif);
3331 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3332 ELEM_REMOVE(&iobj->link);
3339 else if (diobj == pdiobj) {
3353 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3354 ELEM_REPLACE(&piobj->link, &popiobj->link);
3357 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3371 if (IS_INSN_ID(iobj, newrange)) {
3372 INSN *
const range = iobj;
3374 VALUE str_beg, str_end;
3376 if ((end = (
INSN *)get_prev_insn(range)) != 0 &&
3377 is_frozen_putstring(end, &str_end) &&
3378 (beg = (
INSN *)get_prev_insn(end)) != 0 &&
3379 is_frozen_putstring(beg, &str_beg)) {
3380 int excl =
FIX2INT(OPERAND_AT(range, 0));
3383 ELEM_REMOVE(&beg->link);
3384 ELEM_REMOVE(&end->link);
3385 range->insn_id = BIN(putobject);
3386 OPERAND_AT(range, 0) = lit_range;
3391 if (IS_INSN_ID(iobj, leave)) {
3392 remove_unreachable_chunk(iseq, iobj->link.next);
3404 if (IS_INSN_ID(iobj, duparray)) {
3406 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3407 iobj->insn_id = BIN(putobject);
3417 if (IS_INSN_ID(iobj, duparray)) {
3419 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3423 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3424 VALUE ary = iobj->operands[0];
3427 iobj->insn_id = BIN(opt_ary_freeze);
3428 iobj->operand_size = 2;
3429 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
3430 iobj->operands[0] = ary;
3431 iobj->operands[1] = (
VALUE)ci;
3443 if (IS_INSN_ID(iobj, duphash)) {
3445 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3449 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3450 VALUE hash = iobj->operands[0];
3453 iobj->insn_id = BIN(opt_hash_freeze);
3454 iobj->operand_size = 2;
3455 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
3456 iobj->operands[0] = hash;
3457 iobj->operands[1] = (
VALUE)ci;
3469 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3471 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3475 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3476 iobj->insn_id = BIN(opt_ary_freeze);
3477 iobj->operand_size = 2;
3478 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
3479 iobj->operands[0] = rb_cArray_empty_frozen;
3480 iobj->operands[1] = (
VALUE)ci;
3492 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3494 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3498 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3499 iobj->insn_id = BIN(opt_hash_freeze);
3500 iobj->operand_size = 2;
3501 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
3502 iobj->operands[0] = rb_cHash_empty_frozen;
3503 iobj->operands[1] = (
VALUE)ci;
3509 if (IS_INSN_ID(iobj, branchif) ||
3510 IS_INSN_ID(iobj, branchnil) ||
3511 IS_INSN_ID(iobj, branchunless)) {
3520 INSN *nobj = (
INSN *)get_destination_insn(iobj);
3542 int stop_optimization =
3543 ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq) &&
3544 nobj->link.type == ISEQ_ELEMENT_INSN &&
3545 nobj->insn_info.events;
3546 if (!stop_optimization) {
3547 INSN *pobj = (
INSN *)iobj->link.prev;
3550 if (!IS_INSN(&pobj->link))
3552 else if (IS_INSN_ID(pobj, dup))
3557 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3558 if (!replace_destination(iobj, nobj))
break;
3560 else if (prev_dup && IS_INSN_ID(nobj, dup) &&
3561 !!(nobj = (
INSN *)nobj->link.next) &&
3563 nobj->insn_id == iobj->insn_id) {
3579 if (!replace_destination(iobj, nobj))
break;
3607 if (prev_dup && IS_INSN(pobj->link.prev)) {
3608 pobj = (
INSN *)pobj->link.prev;
3610 if (IS_INSN_ID(pobj, putobject)) {
3611 cond = (IS_INSN_ID(iobj, branchif) ?
3612 OPERAND_AT(pobj, 0) !=
Qfalse :
3613 IS_INSN_ID(iobj, branchunless) ?
3614 OPERAND_AT(pobj, 0) ==
Qfalse :
3617 else if (IS_INSN_ID(pobj, putstring) ||
3618 IS_INSN_ID(pobj, duparray) ||
3619 IS_INSN_ID(pobj, newarray)) {
3620 cond = IS_INSN_ID(iobj, branchif);
3622 else if (IS_INSN_ID(pobj, putnil)) {
3623 cond = !IS_INSN_ID(iobj, branchif);
3626 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3627 ELEM_REMOVE(iobj->link.prev);
3629 else if (!iseq_pop_newarray(iseq, pobj)) {
3630 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3631 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3635 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3636 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3638 iobj->insn_id = BIN(jump);
3642 unref_destination(iobj, 0);
3643 ELEM_REMOVE(&iobj->link);
3648 nobj = (
INSN *)get_destination_insn(nobj);
3653 if (IS_INSN_ID(iobj, pop)) {
3661 if (IS_INSN(prev)) {
3662 enum ruby_vminsn_type previ = ((
INSN *)prev)->insn_id;
3663 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3664 previ == BIN(putself) || previ == BIN(putstring) ||
3665 previ == BIN(putchilledstring) ||
3666 previ == BIN(dup) ||
3667 previ == BIN(getlocal) ||
3668 previ == BIN(getblockparam) ||
3669 previ == BIN(getblockparamproxy) ||
3670 previ == BIN(getinstancevariable) ||
3671 previ == BIN(duparray)) {
3675 ELEM_REMOVE(&iobj->link);
3677 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (
INSN*)prev)) {
3678 ELEM_REMOVE(&iobj->link);
3680 else if (previ == BIN(concatarray)) {
3682 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3683 INSN_OF(piobj) = BIN(pop);
3685 else if (previ == BIN(concatstrings)) {
3686 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3690 ELEM_REMOVE(&iobj->link);
3691 INSN_OF(prev) = BIN(adjuststack);
3697 if (IS_INSN_ID(iobj, newarray) ||
3698 IS_INSN_ID(iobj, duparray) ||
3699 IS_INSN_ID(iobj, concatarray) ||
3700 IS_INSN_ID(iobj, splatarray) ||
3710 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3716 if (IS_INSN_ID(iobj, newarray)) {
3718 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3719 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3721 op1 = OPERAND_AT(iobj, 0);
3722 op2 = OPERAND_AT(next, 0);
3733 INSN_OF(iobj) = BIN(swap);
3734 iobj->operand_size = 0;
3743 INSN_OF(iobj) = BIN(opt_reverse);
3748 INSN_OF(iobj) = BIN(opt_reverse);
3749 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3759 for (; diff > 0; diff--) {
3760 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
3771 for (; diff < 0; diff++) {
3772 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
3779 if (IS_INSN_ID(iobj, duparray)) {
3788 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
3789 INSN_OF(iobj) = BIN(putobject);
3793 if (IS_INSN_ID(iobj, anytostring)) {
3801 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
3802 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
3807 if (IS_INSN_ID(iobj, putstring) || IS_INSN_ID(iobj, putchilledstring) ||
3815 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
3817 INSN *next = (
INSN *)iobj->link.next;
3818 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
3819 ELEM_REMOVE(&next->link);
3821 ELEM_REMOVE(&iobj->link);
3825 if (IS_INSN_ID(iobj, concatstrings)) {
3834 if (IS_INSN(next) && IS_INSN_ID(next, jump))
3835 next = get_destination_insn(jump = (
INSN *)next);
3836 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
3837 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
3838 OPERAND_AT(iobj, 0) =
INT2FIX(n);
3840 LABEL *label = ((
LABEL *)OPERAND_AT(jump, 0));
3841 if (!--label->refcnt) {
3842 ELEM_REMOVE(&label->link);
3845 label = NEW_LABEL(0);
3846 OPERAND_AT(jump, 0) = (
VALUE)label;
3849 ELEM_INSERT_NEXT(next, &label->link);
3850 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
3858 if (do_tailcallopt &&
3859 (IS_INSN_ID(iobj, send) ||
3860 IS_INSN_ID(iobj, opt_aref_with) ||
3861 IS_INSN_ID(iobj, opt_aset_with) ||
3862 IS_INSN_ID(iobj, invokesuper))) {
3871 if (iobj->link.next) {
3874 if (!IS_INSN(next)) {
3878 switch (INSN_OF(next)) {
3887 next = get_destination_insn((
INSN *)next);
3901 if (IS_INSN_ID(piobj, send) ||
3902 IS_INSN_ID(piobj, invokesuper)) {
3903 if (OPERAND_AT(piobj, 1) == 0) {
3904 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3905 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3910 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3911 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3917 if (IS_INSN_ID(iobj, dup)) {
3918 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
3929 if (IS_NEXT_INSN_ID(set1, setlocal)) {
3931 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3932 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3934 ELEM_REMOVE(&iobj->link);
3947 else if (IS_NEXT_INSN_ID(set1, dup) &&
3948 IS_NEXT_INSN_ID(set1->next, setlocal)) {
3949 set2 = set1->next->next;
3950 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3951 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3952 ELEM_REMOVE(set1->next);
3966 if (IS_INSN_ID(iobj, getlocal)) {
3968 if (IS_NEXT_INSN_ID(niobj, dup)) {
3969 niobj = niobj->next;
3971 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
3973 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
3974 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
3990 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
3991 if (IS_TRACE(iobj->link.next)) {
3992 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
3993 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
3995 if (iobj == (
INSN *)list && bf->argc == 0 && (iseq->body->builtin_attrs & BUILTIN_ATTR_LEAF)) {
3996 iseq->body->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4009 if (IS_INSN_ID(iobj, getblockparam)) {
4010 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4011 iobj->insn_id = BIN(getblockparamproxy);
4015 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4017 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4018 niobj = niobj->next;
4020 unsigned int set_flags = 0, unset_flags = 0;
4033 if (IS_NEXT_INSN_ID(niobj, send)) {
4034 siobj = niobj->next;
4035 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4036 unset_flags = VM_CALL_ARGS_BLOCKARG;
4051 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4052 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4053 siobj = niobj->next->next;
4054 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4059 unsigned int flags = vm_ci_flag(ci);
4060 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4061 ((
INSN*)niobj)->insn_id = BIN(putobject);
4062 OPERAND_AT(niobj, 0) =
rb_hash_freeze(rb_hash_resurrect(OPERAND_AT(niobj, 0)));
4064 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4065 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4067 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4077 insn_set_specialized_instruction(
rb_iseq_t *iseq,
INSN *iobj,
int insn_id)
4079 iobj->insn_id = insn_id;
4080 iobj->operand_size = insn_len(insn_id) - 1;
4083 if (insn_id == BIN(opt_neq)) {
4084 VALUE original_ci = iobj->operands[0];
4085 iobj->operand_size = 2;
4086 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
4087 iobj->operands[0] = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4088 iobj->operands[1] = original_ci;
4097 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4098 IS_INSN(iobj->link.next)) {
4102 INSN *niobj = (
INSN *)iobj->link.next;
4103 if (IS_INSN_ID(niobj, send)) {
4105 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4107 switch (vm_ci_mid(ci)) {
4109 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4112 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4115 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4120 VALUE num = iobj->operands[0];
4121 int operand_len = insn_len(BIN(opt_newarray_send)) - 1;
4122 iobj->insn_id = BIN(opt_newarray_send);
4123 iobj->operands = compile_data_calloc2(iseq, operand_len,
sizeof(
VALUE));
4124 iobj->operands[0] = num;
4125 iobj->operands[1] = method;
4126 iobj->operand_size = operand_len;
4127 ELEM_REMOVE(&niobj->link);
4132 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4134 IS_NEXT_INSN_ID(&niobj->link, send)) {
4136 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4137 VALUE num = iobj->operands[0];
4138 int operand_len = insn_len(BIN(opt_newarray_send)) - 1;
4139 iobj->insn_id = BIN(opt_newarray_send);
4140 iobj->operands = compile_data_calloc2(iseq, operand_len,
sizeof(
VALUE));
4141 iobj->operands[0] = FIXNUM_INC(num, 1);
4142 iobj->operands[1] =
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK);
4143 iobj->operand_size = operand_len;
4144 ELEM_REMOVE(&iobj->link);
4145 ELEM_REMOVE(niobj->link.next);
4146 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4152 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4154 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4155 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4158 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4159 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] ==
rb_id2sym(idBuffer))) {
4160 VALUE num = iobj->operands[0];
4161 int operand_len = insn_len(BIN(opt_newarray_send)) - 1;
4162 iobj->insn_id = BIN(opt_newarray_send);
4163 iobj->operands = compile_data_calloc2(iseq, operand_len,
sizeof(
VALUE));
4164 iobj->operands[0] = FIXNUM_INC(num, 2);
4165 iobj->operands[1] =
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER);
4166 iobj->operand_size = operand_len;
4168 ELEM_REMOVE((niobj->link.next)->next);
4170 ELEM_REMOVE(&iobj->link);
4172 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4180 if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4181 IS_INSN_ID(niobj, putobject) ||
4182 IS_INSN_ID(niobj, putself) ||
4183 IS_INSN_ID(niobj, getlocal) ||
4184 IS_INSN_ID(niobj, getinstancevariable)) &&
4185 IS_NEXT_INSN_ID(&niobj->link, send)) {
4192 sendobj = sendobj->next;
4193 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4194 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4197 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4198 VALUE num = iobj->operands[0];
4200 sendins->insn_id = BIN(opt_newarray_send);
4201 sendins->operand_size = insn_len(sendins->insn_id) - 1;
4202 sendins->operands = compile_data_calloc2(iseq, sendins->operand_size,
sizeof(
VALUE));
4203 sendins->operands[0] = FIXNUM_INC(num, 1);
4204 sendins->operands[1] =
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P);
4206 ELEM_REMOVE(&iobj->link);
4220 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4221 INSN *niobj = (
INSN *)iobj->link.next;
4222 if ((IS_INSN_ID(niobj, getlocal) ||
4223 IS_INSN_ID(niobj, getinstancevariable) ||
4224 IS_INSN_ID(niobj, putself)) &&
4225 IS_NEXT_INSN_ID(&niobj->link, send)) {
4232 sendobj = sendobj->next;
4233 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4234 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4236 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4238 VALUE ary = iobj->operands[0];
4242 sendins->insn_id = BIN(opt_duparray_send);
4243 sendins->operand_size = insn_len(sendins->insn_id) - 1;;
4244 sendins->operands = compile_data_calloc2(iseq, sendins->operand_size,
sizeof(
VALUE));
4245 sendins->operands[0] = ary;
4246 sendins->operands[1] =
rb_id2sym(idIncludeP);
4247 sendins->operands[2] =
INT2FIX(1);
4250 ELEM_REMOVE(&iobj->link);
4257 if (IS_INSN_ID(iobj, send)) {
4261 #define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4262 if (vm_ci_simple(ci)) {
4263 switch (vm_ci_argc(ci)) {
4265 switch (vm_ci_mid(ci)) {
4266 case idLength: SP_INSN(length);
return COMPILE_OK;
4267 case idSize: SP_INSN(size);
return COMPILE_OK;
4268 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4269 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4270 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4271 case idNot: SP_INSN(not);
return COMPILE_OK;
4275 switch (vm_ci_mid(ci)) {
4276 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4277 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4278 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4279 case idDIV: SP_INSN(div);
return COMPILE_OK;
4280 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4281 case idEq: SP_INSN(eq);
return COMPILE_OK;
4282 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4283 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4284 case idLT: SP_INSN(lt);
return COMPILE_OK;
4285 case idLE: SP_INSN(le);
return COMPILE_OK;
4286 case idGT: SP_INSN(gt);
return COMPILE_OK;
4287 case idGE: SP_INSN(ge);
return COMPILE_OK;
4288 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4289 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4290 case idAnd: SP_INSN(and);
return COMPILE_OK;
4291 case idOr: SP_INSN(or);
return COMPILE_OK;
4295 switch (vm_ci_mid(ci)) {
4296 case idASET: SP_INSN(aset);
return COMPILE_OK;
4302 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4303 iobj->insn_id = BIN(opt_send_without_block);
4304 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4315 switch (ISEQ_BODY(iseq)->
type) {
4317 case ISEQ_TYPE_EVAL:
4318 case ISEQ_TYPE_MAIN:
4320 case ISEQ_TYPE_RESCUE:
4321 case ISEQ_TYPE_ENSURE:
4333 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4334 const int do_tailcallopt = tailcallable_p(iseq) &&
4335 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4336 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4337 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4338 int rescue_level = 0;
4339 int tailcallopt = do_tailcallopt;
4341 list = FIRST_ELEMENT(anchor);
4343 int do_block_optimization = 0;
4345 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK && !ISEQ_COMPILE_DATA(iseq)->catch_except_p) {
4346 do_block_optimization = 1;
4350 if (IS_INSN(list)) {
4351 if (do_peepholeopt) {
4352 iseq_peephole_optimize(iseq, list, tailcallopt);
4355 iseq_specialized_instruction(iseq, (
INSN *)list);
4358 insn_operands_unification((
INSN *)list);
4361 if (do_block_optimization) {
4363 if (IS_INSN_ID(item, jump)) {
4364 do_block_optimization = 0;
4368 if (IS_LABEL(list)) {
4369 switch (((
LABEL *)list)->rescued) {
4370 case LABEL_RESCUE_BEG:
4372 tailcallopt = FALSE;
4374 case LABEL_RESCUE_END:
4375 if (!--rescue_level) tailcallopt = do_tailcallopt;
4382 if (do_block_optimization) {
4384 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop)) {
4391 #if OPT_INSTRUCTIONS_UNIFICATION
4403 for (i = 0; i < size; i++) {
4404 iobj = (
INSN *)list;
4405 argc += iobj->operand_size;
4410 ptr = operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
4415 for (i = 0; i < size; i++) {
4416 iobj = (
INSN *)list;
4418 ptr += iobj->operand_size;
4422 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4434 #if OPT_INSTRUCTIONS_UNIFICATION
4440 list = FIRST_ELEMENT(anchor);
4442 if (IS_INSN(list)) {
4443 iobj = (
INSN *)list;
4445 if (unified_insns_data[
id] != 0) {
4446 const int *
const *entry = unified_insns_data[id];
4447 for (j = 1; j < (intptr_t)entry[0]; j++) {
4448 const int *unified = entry[j];
4450 for (k = 2; k < unified[1]; k++) {
4452 ((
INSN *)li)->insn_id != unified[k]) {
4459 new_unified_insn(iseq, unified[0], unified[1] - 1,
4464 niobj->link.next = li;
4483 all_string_result_p(
const NODE *node)
4485 if (!node)
return FALSE;
4486 switch (nd_type(node)) {
4487 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4489 case NODE_IF:
case NODE_UNLESS:
4490 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4491 if (all_string_result_p(RNODE_IF(node)->nd_body))
4492 return all_string_result_p(RNODE_IF(node)->nd_else);
4494 case NODE_AND:
case NODE_OR:
4495 if (!RNODE_AND(node)->nd_2nd)
4496 return all_string_result_p(RNODE_AND(node)->nd_1st);
4497 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4499 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4508 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4509 VALUE lit = rb_node_dstr_string_val(node);
4513 debugp_param(
"nd_lit", lit);
4517 COMPILE_ERROR(ERROR_ARGS
"dstr: must be string: %s",
4518 rb_builtin_type_name(
TYPE(lit)));
4521 lit = rb_fstring(lit);
4522 ADD_INSN1(ret, node, putobject, lit);
4524 if (
RSTRING_LEN(lit) == 0) first_lit = LAST_ELEMENT(ret);
4528 const NODE *
const head = list->nd_head;
4529 if (nd_type_p(head, NODE_STR)) {
4530 lit = rb_node_str_string_val(head);
4531 ADD_INSN1(ret, head, putobject, lit);
4536 CHECK(COMPILE(ret,
"each string", head));
4541 if (
NIL_P(lit) && first_lit) {
4542 ELEM_REMOVE(first_lit);
4553 while (node && nd_type_p(node, NODE_BLOCK)) {
4554 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4555 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4556 node = RNODE_BLOCK(node)->nd_next;
4559 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4568 if (!RNODE_DSTR(node)->nd_next) {
4569 VALUE lit = rb_node_dstr_string_val(node);
4570 ADD_INSN1(ret, node, putstring, lit);
4574 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt));
4575 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4584 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
4586 if (!RNODE_DREGX(node)->nd_next) {
4588 VALUE src = rb_node_dregx_string_val(node);
4589 VALUE match = rb_reg_compile(src, cflag, NULL, 0);
4590 ADD_INSN1(ret, node, putobject, match);
4596 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt));
4600 ADD_INSN(ret, node, pop);
4610 const int line = nd_line(node);
4611 LABEL *lend = NEW_LABEL(line);
4612 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4613 + VM_SVAR_FLIPFLOP_START;
4616 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4617 ADD_INSNL(ret, node, branchif, lend);
4620 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
4621 ADD_INSNL(ret, node, branchunless, else_label);
4622 ADD_INSN1(ret, node, putobject,
Qtrue);
4623 ADD_INSN1(ret, node, setspecial, key);
4625 ADD_INSNL(ret, node, jump, then_label);
4629 ADD_LABEL(ret, lend);
4630 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
4631 ADD_INSNL(ret, node, branchunless, then_label);
4632 ADD_INSN1(ret, node, putobject,
Qfalse);
4633 ADD_INSN1(ret, node, setspecial, key);
4634 ADD_INSNL(ret, node, jump, then_label);
4643 #define COMPILE_SINGLE 2
4650 LABEL *label = NEW_LABEL(nd_line(cond));
4651 if (!then_label) then_label = label;
4652 else if (!else_label) else_label = label;
4654 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
4656 if (LIST_INSN_SIZE_ONE(seq)) {
4657 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
4658 if (insn->insn_id == BIN(jump) && (
LABEL *)(insn->operands[0]) == label)
4661 if (!label->refcnt) {
4662 return COMPILE_SINGLE;
4664 ADD_LABEL(seq, label);
4674 DECL_ANCHOR(ignore);
4677 switch (nd_type(cond)) {
4679 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
4680 cond = RNODE_AND(cond)->nd_2nd;
4681 if (ok == COMPILE_SINGLE) {
4682 INIT_ANCHOR(ignore);
4684 then_label = NEW_LABEL(nd_line(cond));
4688 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
4689 cond = RNODE_OR(cond)->nd_2nd;
4690 if (ok == COMPILE_SINGLE) {
4691 INIT_ANCHOR(ignore);
4693 else_label = NEW_LABEL(nd_line(cond));
4703 case NODE_IMAGINARY:
4710 ADD_INSNL(ret, cond, jump, then_label);
4715 ADD_INSNL(ret, cond, jump, else_label);
4721 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
4722 ADD_INSNL(ret, cond, jump, then_label);
4725 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
4728 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
4731 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
4735 DECL_ANCHOR(cond_seq);
4736 INIT_ANCHOR(cond_seq);
4738 CHECK(COMPILE(cond_seq,
"branch condition", cond));
4740 if (LIST_INSN_SIZE_ONE(cond_seq)) {
4741 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
4742 if (insn->insn_id == BIN(putobject)) {
4743 if (
RTEST(insn->operands[0])) {
4744 ADD_INSNL(ret, cond, jump, then_label);
4749 ADD_INSNL(ret, cond, jump, else_label);
4754 ADD_SEQ(ret, cond_seq);
4759 ADD_INSNL(ret, cond, branchunless, else_label);
4760 ADD_INSNL(ret, cond, jump, then_label);
4764 #define HASH_BRACE 1
4767 keyword_node_p(
const NODE *
const node)
4769 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
4775 switch (nd_type(node)) {
4777 return rb_node_sym_string_val(node);
4779 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
4786 NODE *node = node_hash->nd_head;
4790 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4791 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
4807 const NODE *
const root_node,
4815 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
4816 const NODE *node = RNODE_HASH(root_node)->nd_head;
4820 const NODE *key_node = RNODE_LIST(node)->nd_head;
4824 if (key_node && nd_type_p(key_node, NODE_SYM)) {
4829 *flag |= VM_CALL_KW_SPLAT;
4830 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4835 *flag |= VM_CALL_KW_SPLAT_MUT;
4840 node = RNODE_LIST(node)->nd_next;
4841 node = RNODE_LIST(node)->nd_next;
4845 node = RNODE_HASH(root_node)->nd_head;
4848 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
4851 VALUE *keywords = kw_arg->keywords;
4854 kw_arg->references = 0;
4855 kw_arg->keyword_len =
len;
4857 *kw_arg_ptr = kw_arg;
4859 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4860 const NODE *key_node = RNODE_LIST(node)->nd_head;
4861 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
4864 keywords[j] = get_symbol_value(iseq, key_node);
4868 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
4882 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
4884 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
4887 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
4888 *kwnode_ptr = RNODE_LIST(node)->nd_head;
4891 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
4892 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
4900 frozen_string_literal_p(
const rb_iseq_t *iseq)
4902 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
4906 static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
4908 switch (nd_type(node)) {
4916 case NODE_IMAGINARY:
4923 return hash_key || frozen_string_literal_p(iseq);
4932 switch (nd_type(node)) {
4934 return rb_node_integer_literal_val(node);
4936 return rb_node_float_literal_val(node);
4938 return rb_node_rational_literal_val(node);
4939 case NODE_IMAGINARY:
4940 return rb_node_imaginary_literal_val(node);
4948 return rb_node_sym_string_val(node);
4950 return rb_node_regx_string_val(node);
4952 return rb_node_line_lineno_val(node);
4954 return rb_node_encoding_val(node);
4957 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
4958 VALUE lit = get_string_value(node);
4959 return rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
4962 return get_string_value(node);
4965 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
4972 const NODE *line_node = node;
4974 if (nd_type_p(node, NODE_ZLIST)) {
4976 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
4981 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
4984 for (; node; node = RNODE_LIST(node)->nd_next) {
4985 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5027 const int max_stack_len = 0x100;
5028 const int min_tmp_ary_len = 0x40;
5032 #define FLUSH_CHUNK \
5034 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5035 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5036 first_chunk = FALSE; \
5044 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5046 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5047 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5050 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5055 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5056 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5062 ADD_INSN1(ret, line_node, duparray, ary);
5063 first_chunk = FALSE;
5066 ADD_INSN1(ret, line_node, putobject, ary);
5067 ADD_INSN(ret, line_node, concattoarray);
5074 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5076 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5079 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5081 if (stack_len == 0 && first_chunk) {
5082 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5087 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5088 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5092 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5097 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5107 static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5109 return RNODE_LIST(node)->nd_head && static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
true) && static_literal_node_p(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq,
false);
5115 const NODE *line_node = node;
5117 node = RNODE_HASH(node)->nd_head;
5119 if (!node || nd_type_p(node, NODE_ZLIST)) {
5121 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5126 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5129 for (; node; node = RNODE_LIST(node)->nd_next) {
5130 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5153 const int max_stack_len = 0x100;
5154 const int min_tmp_hash_len = 0x800;
5156 int first_chunk = 1;
5157 DECL_ANCHOR(anchor);
5158 INIT_ANCHOR(anchor);
5161 #define FLUSH_CHUNK() \
5163 if (first_chunk) { \
5164 APPEND_LIST(ret, anchor); \
5165 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5168 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5169 ADD_INSN(ret, line_node, swap); \
5170 APPEND_LIST(ret, anchor); \
5171 ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
5173 INIT_ANCHOR(anchor); \
5174 first_chunk = stack_len = 0; \
5181 if (static_literal_node_pair_p(node, iseq)) {
5183 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5184 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5187 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5192 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5194 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5195 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5206 ADD_INSN1(ret, line_node, duphash, hash);
5210 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5211 ADD_INSN(ret, line_node, swap);
5213 ADD_INSN1(ret, line_node, putobject, hash);
5215 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5222 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5225 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5228 if (RNODE_LIST(node)->nd_head) {
5230 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5231 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5235 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5241 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5242 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5243 int first_kw = first_chunk && stack_len == 0;
5244 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5245 int only_kw = last_kw && first_kw;
5247 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5249 if (only_kw && method_call_keywords) {
5257 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5259 else if (first_kw) {
5263 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5270 if (only_kw && method_call_keywords) {
5276 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5283 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5284 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5285 else ADD_INSN(ret, line_node, swap);
5287 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5289 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5304 rb_node_case_when_optimizable_literal(
const NODE *
const node)
5306 switch (nd_type(node)) {
5308 return rb_node_integer_literal_val(node);
5310 VALUE v = rb_node_float_literal_val(node);
5319 case NODE_IMAGINARY:
5328 return rb_node_sym_string_val(node);
5330 return rb_node_line_lineno_val(node);
5332 return rb_node_str_string_val(node);
5334 return rb_node_file_path_val(node);
5341 LABEL *l1,
int only_special_literals,
VALUE literals)
5344 const NODE *val = RNODE_LIST(vals)->nd_head;
5345 VALUE lit = rb_node_case_when_optimizable_literal(val);
5348 only_special_literals = 0;
5354 if (nd_type_p(val, NODE_STR) || nd_type_p(val, NODE_FILE)) {
5355 debugp_param(
"nd_lit", get_string_value(val));
5356 lit = get_string_value(val);
5357 ADD_INSN1(cond_seq, val, putobject, lit);
5361 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5365 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5366 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5367 ADD_INSNL(cond_seq, val, branchif, l1);
5368 vals = RNODE_LIST(vals)->nd_next;
5370 return only_special_literals;
5375 LABEL *l1,
int only_special_literals,
VALUE literals)
5377 const NODE *line_node = vals;
5379 switch (nd_type(vals)) {
5381 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5385 ADD_INSN (cond_seq, line_node, dup);
5386 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5387 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5388 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5389 ADD_INSNL(cond_seq, line_node, branchif, l1);
5392 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5393 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5396 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5397 ADD_INSN (cond_seq, line_node, dup);
5398 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5399 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5400 ADD_INSNL(cond_seq, line_node, branchif, l1);
5403 ADD_INSN (cond_seq, line_node, dup);
5404 CHECK(COMPILE(cond_seq,
"when val", vals));
5405 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5406 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5407 ADD_INSNL(cond_seq, line_node, branchif, l1);
5500 const NODE *line_node;
5515 add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc,
INSN *before_insn)
5518 rb_bug(
"no masgn_state");
5527 memo->before_insn = before_insn;
5528 memo->line_node = line_node;
5529 memo->argn = state->num_args + 1;
5530 memo->num_args = argc;
5531 state->num_args += argc;
5532 memo->lhs_pos = lhs_pos;
5534 if (!state->first_memo) {
5535 state->first_memo = memo;
5538 state->last_memo->next = memo;
5540 state->last_memo = memo;
5550 switch (nd_type(node)) {
5551 case NODE_ATTRASGN: {
5553 const NODE *line_node = node;
5555 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5557 bool safenav_call =
false;
5559 iobj = (
INSN *)get_prev_insn((
INSN *)insn_element);
5561 ELEM_REMOVE(insn_element);
5562 if (!IS_INSN_ID(iobj, send)) {
5563 safenav_call =
true;
5564 iobj = (
INSN *)get_prev_insn(iobj);
5565 ELEM_INSERT_NEXT(&iobj->link, insn_element);
5567 (pre->last = iobj->link.prev)->next = 0;
5570 int argc = vm_ci_argc(ci) + 1;
5571 ci = ci_argc_set(iseq, ci, argc);
5572 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5576 ADD_INSN(lhs, line_node, swap);
5579 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5582 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (
INSN *)LAST_ELEMENT(lhs))) {
5586 iobj->link.prev = lhs->last;
5587 lhs->last->next = &iobj->link;
5588 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
5589 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5590 int argc = vm_ci_argc(ci);
5591 bool dupsplat =
false;
5592 ci = ci_argc_set(iseq, ci, argc - 1);
5593 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
5600 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
5602 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5611 int line_no = nd_line(line_node);
5612 int node_id = nd_node_id(line_node);
5615 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5616 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
5617 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5619 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
5621 if (!safenav_call) {
5622 ADD_INSN(lhs, line_node, pop);
5624 ADD_INSN(lhs, line_node, pop);
5627 for (
int i=0; i < argc; i++) {
5628 ADD_INSN(post, line_node, pop);
5633 DECL_ANCHOR(nest_rhs);
5634 INIT_ANCHOR(nest_rhs);
5635 DECL_ANCHOR(nest_lhs);
5636 INIT_ANCHOR(nest_lhs);
5638 int prev_level = state->lhs_level;
5639 bool prev_nested = state->nested;
5641 state->lhs_level = lhs_pos - 1;
5642 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5643 state->lhs_level = prev_level;
5644 state->nested = prev_nested;
5646 ADD_SEQ(lhs, nest_rhs);
5647 ADD_SEQ(lhs, nest_lhs);
5651 if (!RNODE_CDECL(node)->nd_vid) {
5655 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5658 iobj = (
INSN *)insn_element;
5661 ELEM_REMOVE(insn_element);
5662 pre->last = iobj->link.prev;
5665 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (
INSN *)LAST_ELEMENT(lhs))) {
5669 ADD_INSN(post, node, pop);
5674 DECL_ANCHOR(anchor);
5675 INIT_ANCHOR(anchor);
5676 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
5677 ELEM_REMOVE(FIRST_ELEMENT(anchor));
5678 ADD_SEQ(lhs, anchor);
5689 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
5690 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
5697 const NODE *rhsn,
const NODE *orig_lhsn)
5700 const int memsize = numberof(mem);
5702 int llen = 0, rlen = 0;
5704 const NODE *lhsn = orig_lhsn;
5706 #define MEMORY(v) { \
5708 if (memindex == memsize) return 0; \
5709 for (i=0; i<memindex; i++) { \
5710 if (mem[i] == (v)) return 0; \
5712 mem[memindex++] = (v); \
5715 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
5720 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
5721 switch (nd_type(ln)) {
5726 MEMORY(get_nd_vid(ln));
5731 lhsn = RNODE_LIST(lhsn)->nd_next;
5737 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
5740 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
5742 rhsn = RNODE_LIST(rhsn)->nd_next;
5747 for (i=0; i<llen-rlen; i++) {
5748 ADD_INSN(ret, orig_lhsn, putnil);
5752 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
5759 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
5760 const NODE *splatn = RNODE_MASGN(node)->nd_args;
5761 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
5762 const NODE *lhsn_count = lhsn;
5763 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
5768 while (lhsn_count) {
5770 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
5773 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
5775 lhsn = RNODE_LIST(lhsn)->nd_next;
5779 if (nd_type_p(splatn, NODE_POSTARG)) {
5781 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
5782 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
5783 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
5785 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
5787 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
5789 if (NODE_NAMED_REST_P(restn)) {
5790 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
5793 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
5795 postn = RNODE_LIST(postn)->nd_next;
5800 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
5804 if (!state->nested) {
5805 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
5809 ADD_INSN(rhs, node, dup);
5811 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
5818 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
5820 state.lhs_level = popped ? 0 : 1;
5823 state.first_memo = NULL;
5824 state.last_memo = NULL;
5834 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
5838 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
5839 for (
int i = 0; i < memo->num_args; i++) {
5840 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
5842 tmp_memo = memo->next;
5851 if (!popped && state.num_args >= 1) {
5853 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
5865 switch (nd_type(node)) {
5875 node = RNODE_COLON2(node)->nd_head;
5884 compile_const_prefix(
rb_iseq_t *iseq,
const NODE *
const node,
5887 switch (nd_type(node)) {
5889 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
5890 ADD_INSN1(body, node, putobject,
Qtrue);
5891 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
5894 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
5895 ADD_INSN(body, node, pop);
5896 ADD_INSN1(body, node, putobject, rb_cObject);
5897 ADD_INSN1(body, node, putobject,
Qtrue);
5898 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
5901 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
5902 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
5903 ADD_INSN1(body, node, putobject,
Qfalse);
5904 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
5907 CHECK(COMPILE(pref,
"const colon2 prefix", node));
5916 if (nd_type_p(cpath, NODE_COLON3)) {
5918 ADD_INSN1(ret, cpath, putobject, rb_cObject);
5919 return VM_DEFINECLASS_FLAG_SCOPED;
5921 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
5923 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
5924 return VM_DEFINECLASS_FLAG_SCOPED;
5928 ADD_INSN1(ret, cpath, putspecialobject,
5929 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5935 private_recv_p(
const NODE *node)
5937 NODE *recv = get_nd_recv(node);
5938 if (recv && nd_type_p(recv, NODE_SELF)) {
5939 return RNODE_SELF(recv)->nd_state != 0;
5946 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore);
5949 compile_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);
5956 enum defined_type expr_type = DEFINED_NOT_DEFINED;
5957 enum node_type
type;
5958 const int line = nd_line(node);
5959 const NODE *line_node = node;
5961 switch (
type = nd_type(node)) {
5965 expr_type = DEFINED_NIL;
5968 expr_type = DEFINED_SELF;
5971 expr_type = DEFINED_TRUE;
5974 expr_type = DEFINED_FALSE;
5979 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
5983 if (RNODE_LIST(vals)->nd_head) {
5984 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
5987 lfinish[1] = NEW_LABEL(line);
5989 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5991 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6004 case NODE_IMAGINARY:
6009 expr_type = DEFINED_EXPR;
6013 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6015 lfinish[1] = NEW_LABEL(line);
6017 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6018 expr_type = DEFINED_EXPR;
6024 expr_type = DEFINED_LVAR;
6027 #define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6029 ADD_INSN3(ret, line_node, definedivar,
6030 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6034 ADD_INSN(ret, line_node, putnil);
6035 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6036 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6040 ADD_INSN(ret, line_node, putnil);
6041 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6042 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6046 ADD_INSN(ret, line_node, putnil);
6047 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6048 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6052 lfinish[1] = NEW_LABEL(line);
6054 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6055 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6056 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6059 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6060 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6063 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6064 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6068 ADD_INSN1(ret, line_node, putobject, rb_cObject);
6069 ADD_INSN3(ret, line_node, defined,
6070 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6078 case NODE_ATTRASGN:{
6079 const int explicit_receiver =
6080 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6081 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6083 if (get_nd_args(node) || explicit_receiver) {
6085 lfinish[1] = NEW_LABEL(line);
6088 lfinish[2] = NEW_LABEL(line);
6091 if (get_nd_args(node)) {
6092 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6093 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6095 if (explicit_receiver) {
6096 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6097 switch (nd_type(get_nd_recv(node))) {
6103 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6104 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6107 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6108 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6112 ADD_INSN(ret, line_node, dup);
6114 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6115 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6118 ADD_INSN(ret, line_node, putself);
6120 ADD_INSN(ret, line_node, dup);
6122 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6123 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6129 ADD_INSN(ret, line_node, putnil);
6130 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6131 PUSH_VAL(DEFINED_YIELD));
6132 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6137 ADD_INSN(ret, line_node, putnil);
6138 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6139 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6140 PUSH_VAL(DEFINED_GVAR));
6145 ADD_INSN(ret, line_node, putnil);
6146 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6147 PUSH_VAL(DEFINED_ZSUPER));
6153 case NODE_OP_ASGN_OR:
6154 case NODE_OP_ASGN_AND:
6163 expr_type = DEFINED_ASGN;
6170 VALUE str = rb_iseq_defined_string(expr_type);
6171 ADD_INSN1(ret, line_node, putobject, str);
6174 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6181 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6182 iseq_set_exception_local_table(iseq);
6187 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore)
6190 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6192 int line = nd_line(node);
6193 LABEL *lstart = NEW_LABEL(line);
6194 LABEL *lend = NEW_LABEL(line);
6197 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6198 rescue = new_child_iseq_with_callback(iseq, ifunc,
6200 ISEQ_BODY(iseq)->location.label),
6201 iseq, ISEQ_TYPE_RESCUE, 0);
6202 lstart->rescued = LABEL_RESCUE_BEG;
6203 lend->rescued = LABEL_RESCUE_END;
6204 APPEND_LABEL(ret, lcur, lstart);
6205 ADD_LABEL(ret, lend);
6207 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6215 const int line = nd_line(node);
6216 const NODE *line_node = node;
6217 if (!RNODE_DEFINED(node)->nd_head) {
6218 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6219 ADD_INSN1(ret, line_node, putobject, str);
6224 lfinish[0] = NEW_LABEL(line);
6227 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6229 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6230 ADD_INSN(ret, line_node, swap);
6232 ADD_LABEL(ret, lfinish[2]);
6234 ADD_INSN(ret, line_node, pop);
6235 ADD_LABEL(ret, lfinish[1]);
6237 ADD_LABEL(ret, lfinish[0]);
6243 make_name_for_block(
const rb_iseq_t *orig_iseq)
6248 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6249 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6250 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6253 iseq = ISEQ_BODY(iseq)->parent_iseq;
6258 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6261 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6270 enl->ensure_node = node;
6271 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6273 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6283 while (erange->next != 0) {
6284 erange = erange->next;
6288 ne->end = erange->end;
6289 erange->end = lstart;
6295 can_add_ensure_iseq(
const rb_iseq_t *iseq)
6298 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
6300 if (e->ensure_node)
return false;
6313 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6315 DECL_ANCHOR(ensure);
6317 INIT_ANCHOR(ensure);
6319 if (enlp->erange != NULL) {
6320 DECL_ANCHOR(ensure_part);
6321 LABEL *lstart = NEW_LABEL(0);
6322 LABEL *lend = NEW_LABEL(0);
6323 INIT_ANCHOR(ensure_part);
6325 add_ensure_range(iseq, enlp->erange, lstart, lend);
6327 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6328 ADD_LABEL(ensure_part, lstart);
6329 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6330 ADD_LABEL(ensure_part, lend);
6331 ADD_SEQ(ensure, ensure_part);
6340 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6341 ADD_SEQ(ret, ensure);
6346 check_keyword(
const NODE *node)
6350 if (nd_type_p(node, NODE_LIST)) {
6351 while (RNODE_LIST(node)->nd_next) {
6352 node = RNODE_LIST(node)->nd_next;
6354 node = RNODE_LIST(node)->nd_head;
6357 return keyword_node_p(node);
6362 keyword_node_single_splat_p(
NODE *kwnode)
6366 NODE *node = RNODE_HASH(kwnode)->nd_head;
6367 return RNODE_LIST(node)->nd_head == NULL &&
6368 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6373 NODE *kwnode,
unsigned int *flag_ptr)
6375 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6376 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6377 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6378 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6379 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6382 #define SPLATARRAY_FALSE 0
6383 #define SPLATARRAY_TRUE 1
6384 #define DUP_SINGLE_KW_SPLAT 2
6388 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6390 if (!argn)
return 0;
6392 NODE *kwnode = NULL;
6394 switch (nd_type(argn)) {
6397 int len = compile_args(iseq, args, argn, &kwnode);
6398 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6401 if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6405 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6406 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6409 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6418 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6419 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6420 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6421 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6422 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6425 case NODE_ARGSCAT: {
6426 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6427 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6428 bool args_pushed =
false;
6430 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6431 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6432 if (kwnode) rest_len--;
6433 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6437 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6438 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6441 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6442 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6443 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6446 else if (!args_pushed) {
6447 ADD_INSN(args, argn, concattoarray);
6453 *flag_ptr |= VM_CALL_KW_SPLAT;
6454 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6460 case NODE_ARGSPUSH: {
6461 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6462 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6464 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6465 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6466 if (kwnode) rest_len--;
6467 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6468 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6471 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6472 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6475 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6476 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6482 *flag_ptr |= VM_CALL_KW_SPLAT;
6483 if (!keyword_node_single_splat_p(kwnode)) {
6484 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6485 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6487 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
6488 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6491 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6499 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
6505 setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
6507 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
6508 *flag |= VM_CALL_ARGS_SPLAT_MUT;
6513 setup_args_dup_rest_p(
const NODE *argn)
6515 switch(nd_type(argn)) {
6526 case NODE_IMAGINARY:
6539 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
6550 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
6553 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
6554 RNODE_BLOCK_PASS(argn)->nd_head : argn;
6557 switch(nd_type(check_arg)) {
6560 dup_rest = SPLATARRAY_FALSE;
6564 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
6566 case(NODE_ARGSPUSH):
6568 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
6569 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
6570 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
6571 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
6572 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
6574 if (dup_rest == SPLATARRAY_FALSE) {
6576 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
6578 NODE *key_node = RNODE_LIST(node)->nd_head;
6579 if (key_node && setup_args_dup_rest_p(key_node)) {
6580 dup_rest = SPLATARRAY_TRUE;
6584 node = RNODE_LIST(node)->nd_next;
6585 NODE *value_node = RNODE_LIST(node)->nd_head;
6586 if (setup_args_dup_rest_p(value_node)) {
6587 dup_rest = SPLATARRAY_TRUE;
6591 node = RNODE_LIST(node)->nd_next;
6600 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
6602 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
6605 initial_dup_rest = dup_rest;
6607 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
6608 DECL_ANCHOR(arg_block);
6609 INIT_ANCHOR(arg_block);
6611 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
6612 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
6614 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
6615 const NODE * arg_node =
6616 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
6623 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
6624 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
6627 *flag |= VM_CALL_FORWARDING;
6629 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
6630 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6634 *flag |= VM_CALL_ARGS_BLOCKARG;
6636 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
6639 if (LIST_INSN_SIZE_ONE(arg_block)) {
6641 if (IS_INSN(elem)) {
6643 if (iobj->insn_id == BIN(getblockparam)) {
6644 iobj->insn_id = BIN(getblockparamproxy);
6648 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
6649 ADD_SEQ(args, arg_block);
6652 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
6654 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6662 int line = nd_line(body);
6664 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
6666 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6667 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
6669 iseq_set_local_table(iseq, 0, 0);
6677 int line = nd_line(node);
6678 const NODE *line_node = node;
6679 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
6681 #if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
6682 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
6686 ADD_INSN(ret, line_node, dup);
6687 ADD_INSNL(ret, line_node, branchunless, fail_label);
6689 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6691 if (RNODE_BLOCK(vars)->nd_next) {
6692 ADD_INSN(ret, line_node, dup);
6695 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6697 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
6700 #if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
6701 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
6706 ADD_INSNL(nom, line_node, jump, end_label);
6707 ADD_LABEL(nom, fail_label);
6709 ADD_INSN(nom, line_node, pop);
6710 ADD_INSN(nom, line_node, putnil);
6712 ADD_LABEL(nom, end_label);
6713 (nom->last->next = cap->link.next)->prev = nom->last;
6714 (cap->link.next = nom->anchor.next)->prev = &cap->link;
6719 ADD_INSNL(ret, line_node, jump, end_label);
6720 ADD_LABEL(ret, fail_label);
6721 ADD_INSN(ret, line_node, pop);
6722 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6724 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6726 ((
INSN*)last)->insn_id = BIN(putnil);
6727 ((
INSN*)last)->operand_size = 0;
6729 ADD_LABEL(ret, end_label);
6733 optimizable_range_item_p(
const NODE *n)
6735 if (!n)
return FALSE;
6736 switch (nd_type(n)) {
6749 optimized_range_item(
const NODE *n)
6751 switch (nd_type(n)) {
6753 return rb_node_line_lineno_val(n);
6755 return rb_node_integer_literal_val(n);
6757 return rb_node_float_literal_val(n);
6759 return rb_node_rational_literal_val(n);
6760 case NODE_IMAGINARY:
6761 return rb_node_imaginary_literal_val(n);
6765 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
6772 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
6773 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
6775 const int line = nd_line(node);
6776 const NODE *line_node = node;
6777 DECL_ANCHOR(cond_seq);
6778 LABEL *then_label, *else_label, *end_label;
6781 INIT_ANCHOR(cond_seq);
6782 then_label = NEW_LABEL(line);
6783 else_label = NEW_LABEL(line);
6786 NODE *cond = RNODE_IF(node)->nd_cond;
6787 if (nd_type(cond) == NODE_BLOCK) {
6788 cond = RNODE_BLOCK(cond)->nd_head;
6791 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
6792 ADD_SEQ(ret, cond_seq);
6794 if (then_label->refcnt && else_label->refcnt) {
6795 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
6798 if (then_label->refcnt) {
6799 ADD_LABEL(ret, then_label);
6801 DECL_ANCHOR(then_seq);
6802 INIT_ANCHOR(then_seq);
6803 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
6805 if (else_label->refcnt) {
6806 const NODE *
const coverage_node = node_body ? node_body : node;
6807 add_trace_branch_coverage(
6810 nd_code_loc(coverage_node),
6811 nd_node_id(coverage_node),
6813 type == NODE_IF ?
"then" :
"else",
6815 end_label = NEW_LABEL(line);
6816 ADD_INSNL(then_seq, line_node, jump, end_label);
6818 ADD_INSN(then_seq, line_node, pop);
6821 ADD_SEQ(ret, then_seq);
6824 if (else_label->refcnt) {
6825 ADD_LABEL(ret, else_label);
6827 DECL_ANCHOR(else_seq);
6828 INIT_ANCHOR(else_seq);
6829 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
6831 if (then_label->refcnt) {
6832 const NODE *
const coverage_node = node_else ? node_else : node;
6833 add_trace_branch_coverage(
6836 nd_code_loc(coverage_node),
6837 nd_node_id(coverage_node),
6839 type == NODE_IF ?
"else" :
"then",
6842 ADD_SEQ(ret, else_seq);
6846 ADD_LABEL(ret, end_label);
6856 const NODE *node = orig_node;
6857 LABEL *endlabel, *elselabel;
6859 DECL_ANCHOR(body_seq);
6860 DECL_ANCHOR(cond_seq);
6861 int only_special_literals = 1;
6864 enum node_type
type;
6865 const NODE *line_node;
6870 INIT_ANCHOR(body_seq);
6871 INIT_ANCHOR(cond_seq);
6873 RHASH_TBL_RAW(literals)->type = &cdhash_type;
6875 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
6877 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
6879 node = RNODE_CASE(node)->nd_body;
6880 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
6881 type = nd_type(node);
6882 line = nd_line(node);
6885 endlabel = NEW_LABEL(line);
6886 elselabel = NEW_LABEL(line);
6890 while (
type == NODE_WHEN) {
6893 l1 = NEW_LABEL(line);
6894 ADD_LABEL(body_seq, l1);
6895 ADD_INSN(body_seq, line_node, pop);
6897 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
6898 add_trace_branch_coverage(
6901 nd_code_loc(coverage_node),
6902 nd_node_id(coverage_node),
6907 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
6908 ADD_INSNL(body_seq, line_node, jump, endlabel);
6910 vals = RNODE_WHEN(node)->nd_head;
6912 switch (nd_type(vals)) {
6914 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
6915 if (only_special_literals < 0)
return COMPILE_NG;
6920 only_special_literals = 0;
6921 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
6924 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
6928 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
6931 node = RNODE_WHEN(node)->nd_next;
6935 type = nd_type(node);
6936 line = nd_line(node);
6941 ADD_LABEL(cond_seq, elselabel);
6942 ADD_INSN(cond_seq, line_node, pop);
6943 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
6944 CHECK(COMPILE_(cond_seq,
"else", node, popped));
6945 ADD_INSNL(cond_seq, line_node, jump, endlabel);
6948 debugs(
"== else (implicit)\n");
6949 ADD_LABEL(cond_seq, elselabel);
6950 ADD_INSN(cond_seq, orig_node, pop);
6951 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
6953 ADD_INSN(cond_seq, orig_node, putnil);
6955 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
6958 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
6959 ADD_INSN(ret, orig_node, dup);
6960 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
6962 LABEL_REF(elselabel);
6965 ADD_SEQ(ret, cond_seq);
6966 ADD_SEQ(ret, body_seq);
6967 ADD_LABEL(ret, endlabel);
6976 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
6978 DECL_ANCHOR(body_seq);
6982 branches = decl_branch_base(iseq, PTR2NUM(orig_node), nd_code_loc(orig_node),
"case");
6984 INIT_ANCHOR(body_seq);
6985 endlabel = NEW_LABEL(nd_line(node));
6987 while (node && nd_type_p(node, NODE_WHEN)) {
6988 const int line = nd_line(node);
6989 LABEL *l1 = NEW_LABEL(line);
6990 ADD_LABEL(body_seq, l1);
6992 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
6993 add_trace_branch_coverage(
6996 nd_code_loc(coverage_node),
6997 nd_node_id(coverage_node),
7002 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7003 ADD_INSNL(body_seq, node, jump, endlabel);
7005 vals = RNODE_WHEN(node)->nd_head;
7007 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7009 switch (nd_type(vals)) {
7013 val = RNODE_LIST(vals)->nd_head;
7014 lnext = NEW_LABEL(nd_line(val));
7015 debug_compile(
"== when2\n", (
void)0);
7016 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7017 ADD_LABEL(ret, lnext);
7018 vals = RNODE_LIST(vals)->nd_next;
7024 ADD_INSN(ret, vals, putnil);
7025 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7026 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7027 ADD_INSNL(ret, vals, branchif, l1);
7030 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7032 node = RNODE_WHEN(node)->nd_next;
7035 const NODE *
const coverage_node = node ? node : orig_node;
7036 add_trace_branch_coverage(
7039 nd_code_loc(coverage_node),
7040 nd_node_id(coverage_node),
7044 CHECK(COMPILE_(ret,
"else", node, popped));
7045 ADD_INSNL(ret, orig_node, jump, endlabel);
7047 ADD_SEQ(ret, body_seq);
7048 ADD_LABEL(ret, endlabel);
7052 static 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);
7054 static 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);
7055 static 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);
7056 static int iseq_compile_pattern_set_general_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7057 static 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);
7058 static int iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7060 #define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7061 #define CASE3_BI_OFFSET_ERROR_STRING 1
7062 #define CASE3_BI_OFFSET_KEY_ERROR_P 2
7063 #define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7064 #define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7067 iseq_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)
7069 const int line = nd_line(node);
7070 const NODE *line_node = node;
7072 switch (nd_type(node)) {
7126 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7127 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7128 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7130 const int min_argc = pre_args_num + post_args_num;
7131 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7132 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7134 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7136 match_failed = NEW_LABEL(line);
7137 type_error = NEW_LABEL(line);
7138 deconstruct = NEW_LABEL(line);
7139 deconstructed = NEW_LABEL(line);
7142 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7143 ADD_INSN(ret, line_node, swap);
7149 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7151 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7153 ADD_INSN(ret, line_node, dup);
7154 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7155 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7156 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7157 if (in_single_pattern) {
7158 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7159 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7160 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7161 INT2FIX(min_argc), base_index + 1 ));
7163 ADD_INSNL(ret, line_node, branchunless, match_failed);
7165 for (i = 0; i < pre_args_num; i++) {
7166 ADD_INSN(ret, line_node, dup);
7167 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7168 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7169 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7170 args = RNODE_LIST(args)->nd_next;
7173 if (RNODE_ARYPTN(node)->rest_arg) {
7174 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7175 ADD_INSN(ret, line_node, dup);
7176 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7177 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7178 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7179 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7180 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7181 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7182 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7184 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7187 if (post_args_num > 0) {
7188 ADD_INSN(ret, line_node, dup);
7189 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7190 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7191 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7192 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7193 ADD_INSN(ret, line_node, pop);
7198 args = RNODE_ARYPTN(node)->post_args;
7199 for (i = 0; i < post_args_num; i++) {
7200 ADD_INSN(ret, line_node, dup);
7202 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7203 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7204 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7206 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7207 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7208 args = RNODE_LIST(args)->nd_next;
7211 ADD_INSN(ret, line_node, pop);
7213 ADD_INSN(ret, line_node, pop);
7215 ADD_INSNL(ret, line_node, jump, matched);
7216 ADD_INSN(ret, line_node, putnil);
7218 ADD_INSN(ret, line_node, putnil);
7221 ADD_LABEL(ret, type_error);
7222 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7224 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7225 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7226 ADD_INSN(ret, line_node, pop);
7228 ADD_LABEL(ret, match_failed);
7229 ADD_INSN(ret, line_node, pop);
7231 ADD_INSN(ret, line_node, pop);
7233 ADD_INSNL(ret, line_node, jump, unmatched);
7286 const NODE *args = RNODE_FNDPTN(node)->args;
7287 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7289 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7290 match_failed = NEW_LABEL(line);
7291 type_error = NEW_LABEL(line);
7292 deconstruct = NEW_LABEL(line);
7293 deconstructed = NEW_LABEL(line);
7295 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7297 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7299 ADD_INSN(ret, line_node, dup);
7300 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7301 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7302 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7303 if (in_single_pattern) {
7304 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 ));
7306 ADD_INSNL(ret, line_node, branchunless, match_failed);
7309 LABEL *while_begin = NEW_LABEL(nd_line(node));
7310 LABEL *next_loop = NEW_LABEL(nd_line(node));
7311 LABEL *find_succeeded = NEW_LABEL(line);
7312 LABEL *find_failed = NEW_LABEL(nd_line(node));
7315 ADD_INSN(ret, line_node, dup);
7316 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7318 ADD_INSN(ret, line_node, dup);
7319 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7320 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7322 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7324 ADD_LABEL(ret, while_begin);
7326 ADD_INSN(ret, line_node, dup);
7327 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7328 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7329 ADD_INSNL(ret, line_node, branchunless, find_failed);
7331 for (j = 0; j < args_num; j++) {
7332 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7333 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7335 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7336 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7338 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7340 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7341 args = RNODE_LIST(args)->nd_next;
7344 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7345 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7346 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7347 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7348 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7349 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));
7351 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7352 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7353 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7354 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7355 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7356 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7357 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7358 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));
7360 ADD_INSNL(ret, line_node, jump, find_succeeded);
7362 ADD_LABEL(ret, next_loop);
7363 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7364 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7365 ADD_INSNL(ret, line_node, jump, while_begin);
7367 ADD_LABEL(ret, find_failed);
7368 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7369 if (in_single_pattern) {
7370 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7371 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7372 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7373 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7374 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7376 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7377 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7379 ADD_INSN(ret, line_node, pop);
7380 ADD_INSN(ret, line_node, pop);
7382 ADD_INSNL(ret, line_node, jump, match_failed);
7383 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7385 ADD_LABEL(ret, find_succeeded);
7386 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7389 ADD_INSN(ret, line_node, pop);
7390 ADD_INSNL(ret, line_node, jump, matched);
7391 ADD_INSN(ret, line_node, putnil);
7393 ADD_LABEL(ret, type_error);
7394 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7396 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7397 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7398 ADD_INSN(ret, line_node, pop);
7400 ADD_LABEL(ret, match_failed);
7401 ADD_INSN(ret, line_node, pop);
7402 ADD_INSNL(ret, line_node, jump, unmatched);
7466 LABEL *match_failed, *type_error;
7469 match_failed = NEW_LABEL(line);
7470 type_error = NEW_LABEL(line);
7472 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
7473 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7474 keys =
rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
7476 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
7477 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
7481 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7483 ADD_INSN(ret, line_node, dup);
7484 ADD_INSN1(ret, line_node, putobject,
ID2SYM(
rb_intern(
"deconstruct_keys")));
7485 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7486 if (in_single_pattern) {
7487 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
7489 ADD_INSNL(ret, line_node, branchunless, match_failed);
7492 ADD_INSN(ret, line_node, putnil);
7495 ADD_INSN1(ret, line_node, duparray, keys);
7500 ADD_INSN(ret, line_node, dup);
7502 ADD_INSNL(ret, line_node, branchunless, type_error);
7504 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7508 if (RNODE_HSHPTN(node)->nd_pkwargs) {
7512 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7514 DECL_ANCHOR(match_values);
7515 INIT_ANCHOR(match_values);
7516 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
7517 for (i = 0; i < keys_num; i++) {
7518 NODE *key_node = RNODE_LIST(args)->nd_head;
7519 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
7520 VALUE key = get_symbol_value(iseq, key_node);
7522 ADD_INSN(ret, line_node, dup);
7523 ADD_INSN1(ret, line_node, putobject, key);
7525 if (in_single_pattern) {
7526 LABEL *match_succeeded;
7527 match_succeeded = NEW_LABEL(line);
7529 ADD_INSN(ret, line_node, dup);
7530 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7533 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
7534 ADD_INSN1(ret, line_node, putobject,
Qtrue);
7535 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
7536 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7537 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
7538 ADD_INSN1(ret, line_node, putobject, key);
7539 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
7541 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
7543 ADD_LABEL(ret, match_succeeded);
7545 ADD_INSNL(ret, line_node, branchunless, match_failed);
7547 ADD_INSN(match_values, line_node, dup);
7548 ADD_INSN1(match_values, line_node, putobject, key);
7549 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ?
rb_intern(
"delete") : idAREF,
INT2FIX(1));
7550 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7551 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
7553 ADD_SEQ(ret, match_values);
7557 ADD_INSN(ret, line_node, dup);
7558 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7559 if (in_single_pattern) {
7560 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
7562 ADD_INSNL(ret, line_node, branchunless, match_failed);
7565 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7566 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
7567 ADD_INSN(ret, line_node, dup);
7568 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7569 if (in_single_pattern) {
7570 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
7572 ADD_INSNL(ret, line_node, branchunless, match_failed);
7575 ADD_INSN(ret, line_node, dup);
7576 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7580 ADD_INSN(ret, line_node, pop);
7581 ADD_INSNL(ret, line_node, jump, matched);
7582 ADD_INSN(ret, line_node, putnil);
7584 ADD_LABEL(ret, type_error);
7585 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7587 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
7588 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7589 ADD_INSN(ret, line_node, pop);
7591 ADD_LABEL(ret, match_failed);
7592 ADD_INSN(ret, line_node, pop);
7593 ADD_INSNL(ret, line_node, jump, unmatched);
7602 case NODE_IMAGINARY:
7630 CHECK(COMPILE(ret,
"case in literal", node));
7631 if (in_single_pattern) {
7632 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7634 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7635 if (in_single_pattern) {
7636 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
7638 ADD_INSNL(ret, line_node, branchif, matched);
7639 ADD_INSNL(ret, line_node, jump, unmatched);
7643 ID id = RNODE_LASGN(node)->nd_vid;
7644 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
7646 if (in_alt_pattern) {
7648 if (name && strlen(name) > 0 && name[0] !=
'_') {
7649 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7655 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
7656 ADD_INSNL(ret, line_node, jump, matched);
7661 ID id = RNODE_DASGN(node)->nd_vid;
7663 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
7665 if (in_alt_pattern) {
7667 if (name && strlen(name) > 0 && name[0] !=
'_') {
7668 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7675 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
7679 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
7680 ADD_INSNL(ret, line_node, jump, matched);
7685 LABEL *match_failed;
7686 match_failed = unmatched;
7687 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7688 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
7689 if (in_single_pattern) {
7690 LABEL *match_succeeded;
7691 match_succeeded = NEW_LABEL(line);
7693 ADD_INSN(ret, line_node, dup);
7694 if (nd_type_p(node, NODE_IF)) {
7695 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7698 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
7701 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
7702 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7703 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7704 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7706 ADD_INSN(ret, line_node, pop);
7707 ADD_INSN(ret, line_node, pop);
7709 ADD_LABEL(ret, match_succeeded);
7711 if (nd_type_p(node, NODE_IF)) {
7712 ADD_INSNL(ret, line_node, branchunless, match_failed);
7715 ADD_INSNL(ret, line_node, branchif, match_failed);
7717 ADD_INSNL(ret, line_node, jump, matched);
7722 LABEL *match_failed;
7723 match_failed = NEW_LABEL(line);
7725 n = RNODE_HASH(node)->nd_head;
7726 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
7727 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7731 ADD_INSN(ret, line_node, dup);
7732 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));
7733 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));
7734 ADD_INSN(ret, line_node, putnil);
7736 ADD_LABEL(ret, match_failed);
7737 ADD_INSN(ret, line_node, pop);
7738 ADD_INSNL(ret, line_node, jump, unmatched);
7742 LABEL *match_succeeded, *fin;
7743 match_succeeded = NEW_LABEL(line);
7744 fin = NEW_LABEL(line);
7746 ADD_INSN(ret, line_node, dup);
7747 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));
7748 ADD_LABEL(ret, match_succeeded);
7749 ADD_INSN(ret, line_node, pop);
7750 ADD_INSNL(ret, line_node, jump, matched);
7751 ADD_INSN(ret, line_node, putnil);
7752 ADD_LABEL(ret, fin);
7753 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
7757 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
7763 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)
7765 LABEL *fin = NEW_LABEL(nd_line(node));
7766 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7767 ADD_LABEL(ret, fin);
7772 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)
7774 const NODE *line_node = node;
7776 if (RNODE_ARYPTN(node)->nd_pconst) {
7777 ADD_INSN(ret, line_node, dup);
7778 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
7779 if (in_single_pattern) {
7780 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7782 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7783 if (in_single_pattern) {
7784 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
7786 ADD_INSNL(ret, line_node, branchunless, match_failed);
7793 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)
7795 const NODE *line_node = node;
7799 if (use_deconstructed_cache) {
7801 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7802 ADD_INSNL(ret, line_node, branchnil, deconstruct);
7805 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7806 ADD_INSNL(ret, line_node, branchunless, match_failed);
7809 ADD_INSN(ret, line_node, pop);
7810 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
7811 ADD_INSNL(ret, line_node, jump, deconstructed);
7814 ADD_INSNL(ret, line_node, jump, deconstruct);
7817 ADD_LABEL(ret, deconstruct);
7818 ADD_INSN(ret, line_node, dup);
7819 ADD_INSN1(ret, line_node, putobject,
ID2SYM(
rb_intern(
"deconstruct")));
7820 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7823 if (use_deconstructed_cache) {
7824 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
7827 if (in_single_pattern) {
7828 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
7831 ADD_INSNL(ret, line_node, branchunless, match_failed);
7836 if (use_deconstructed_cache) {
7837 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7840 ADD_INSN(ret, line_node, dup);
7842 ADD_INSNL(ret, line_node, branchunless, type_error);
7844 ADD_LABEL(ret, deconstructed);
7860 const int line = nd_line(node);
7861 const NODE *line_node = node;
7862 LABEL *match_succeeded = NEW_LABEL(line);
7864 ADD_INSN(ret, line_node, dup);
7865 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7867 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7868 ADD_INSN1(ret, line_node, putobject, errmsg);
7869 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7870 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7871 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7873 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7874 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7876 ADD_INSN(ret, line_node, pop);
7877 ADD_INSN(ret, line_node, pop);
7878 ADD_LABEL(ret, match_succeeded);
7894 const int line = nd_line(node);
7895 const NODE *line_node = node;
7896 LABEL *match_succeeded = NEW_LABEL(line);
7898 ADD_INSN(ret, line_node, dup);
7899 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7901 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7902 ADD_INSN1(ret, line_node, putobject, errmsg);
7903 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7904 ADD_INSN(ret, line_node, dup);
7905 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7906 ADD_INSN1(ret, line_node, putobject, pattern_length);
7907 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
7908 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7910 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7911 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
7913 ADD_INSN(ret, line_node, pop);
7914 ADD_INSN(ret, line_node, pop);
7915 ADD_LABEL(ret, match_succeeded);
7921 iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
7931 const int line = nd_line(node);
7932 const NODE *line_node = node;
7933 LABEL *match_succeeded = NEW_LABEL(line);
7935 ADD_INSN(ret, line_node, dup);
7936 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7938 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7939 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
7940 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7941 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
7942 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
7943 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7945 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7946 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7948 ADD_INSN(ret, line_node, pop);
7949 ADD_INSN(ret, line_node, pop);
7951 ADD_LABEL(ret, match_succeeded);
7952 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7953 ADD_INSN(ret, line_node, pop);
7954 ADD_INSN(ret, line_node, pop);
7962 const NODE *pattern;
7963 const NODE *node = orig_node;
7964 LABEL *endlabel, *elselabel;
7966 DECL_ANCHOR(body_seq);
7967 DECL_ANCHOR(cond_seq);
7969 enum node_type
type;
7970 const NODE *line_node;
7973 bool single_pattern;
7976 INIT_ANCHOR(body_seq);
7977 INIT_ANCHOR(cond_seq);
7979 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
7981 node = RNODE_CASE3(node)->nd_body;
7982 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
7983 type = nd_type(node);
7984 line = nd_line(node);
7986 single_pattern = !RNODE_IN(node)->nd_next;
7988 endlabel = NEW_LABEL(line);
7989 elselabel = NEW_LABEL(line);
7991 if (single_pattern) {
7993 ADD_INSN(head, line_node, putnil);
7994 ADD_INSN(head, line_node, putnil);
7995 ADD_INSN1(head, line_node, putobject,
Qfalse);
7996 ADD_INSN(head, line_node, putnil);
7998 ADD_INSN(head, line_node, putnil);
8000 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8004 while (
type == NODE_IN) {
8008 ADD_INSN(body_seq, line_node, putnil);
8010 l1 = NEW_LABEL(line);
8011 ADD_LABEL(body_seq, l1);
8012 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8014 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8015 add_trace_branch_coverage(
8018 nd_code_loc(coverage_node),
8019 nd_node_id(coverage_node),
8024 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8025 ADD_INSNL(body_seq, line_node, jump, endlabel);
8027 pattern = RNODE_IN(node)->nd_head;
8029 int pat_line = nd_line(pattern);
8030 LABEL *next_pat = NEW_LABEL(pat_line);
8031 ADD_INSN (cond_seq, pattern, dup);
8033 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8034 ADD_LABEL(cond_seq, next_pat);
8035 LABEL_UNREMOVABLE(next_pat);
8038 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8042 node = RNODE_IN(node)->nd_next;
8046 type = nd_type(node);
8047 line = nd_line(node);
8052 ADD_LABEL(cond_seq, elselabel);
8053 ADD_INSN(cond_seq, line_node, pop);
8054 ADD_INSN(cond_seq, line_node, pop);
8055 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8056 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8057 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8058 ADD_INSN(cond_seq, line_node, putnil);
8060 ADD_INSN(cond_seq, line_node, putnil);
8064 debugs(
"== else (implicit)\n");
8065 ADD_LABEL(cond_seq, elselabel);
8066 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8067 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8069 if (single_pattern) {
8077 LABEL *key_error, *fin;
8080 key_error = NEW_LABEL(line);
8081 fin = NEW_LABEL(line);
8084 kw_arg->references = 0;
8085 kw_arg->keyword_len = 2;
8089 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8090 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8092 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8093 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8094 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8095 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8096 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8097 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8098 ADD_INSNL(cond_seq, orig_node, jump, fin);
8100 ADD_LABEL(cond_seq, key_error);
8102 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8103 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8104 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8105 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8106 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8107 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8108 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8110 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8112 ADD_LABEL(cond_seq, fin);
8116 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8117 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8119 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8121 ADD_INSN(cond_seq, orig_node, putnil);
8123 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8124 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8126 ADD_INSN(cond_seq, line_node, putnil);
8130 ADD_SEQ(ret, cond_seq);
8131 ADD_SEQ(ret, body_seq);
8132 ADD_LABEL(ret, endlabel);
8136 #undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8137 #undef CASE3_BI_OFFSET_ERROR_STRING
8138 #undef CASE3_BI_OFFSET_KEY_ERROR_P
8139 #undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8140 #undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8145 const int line = (int)nd_line(node);
8146 const NODE *line_node = node;
8148 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8149 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8150 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8151 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8156 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8157 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8158 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8159 LABEL *end_label = NEW_LABEL(line);
8160 LABEL *adjust_label = NEW_LABEL(line);
8162 LABEL *next_catch_label = NEW_LABEL(line);
8163 LABEL *tmp_label = NULL;
8165 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8166 push_ensure_entry(iseq, &enl, NULL, NULL);
8168 if (RNODE_WHILE(node)->nd_state == 1) {
8169 ADD_INSNL(ret, line_node, jump, next_label);
8172 tmp_label = NEW_LABEL(line);
8173 ADD_INSNL(ret, line_node, jump, tmp_label);
8175 ADD_LABEL(ret, adjust_label);
8176 ADD_INSN(ret, line_node, putnil);
8177 ADD_LABEL(ret, next_catch_label);
8178 ADD_INSN(ret, line_node, pop);
8179 ADD_INSNL(ret, line_node, jump, next_label);
8180 if (tmp_label) ADD_LABEL(ret, tmp_label);
8182 ADD_LABEL(ret, redo_label);
8183 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8185 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8186 add_trace_branch_coverage(
8189 nd_code_loc(coverage_node),
8190 nd_node_id(coverage_node),
8195 CHECK(COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body));
8196 ADD_LABEL(ret, next_label);
8198 if (
type == NODE_WHILE) {
8199 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8200 redo_label, end_label));
8204 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8205 end_label, redo_label));
8208 ADD_LABEL(ret, end_label);
8209 ADD_ADJUST_RESTORE(ret, adjust_label);
8211 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8213 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8217 ADD_INSN(ret, line_node, putnil);
8220 ADD_LABEL(ret, break_label);
8223 ADD_INSN(ret, line_node, pop);
8226 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8228 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8230 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8231 ISEQ_COMPILE_DATA(iseq)->redo_label);
8233 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8234 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8235 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8236 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8237 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
8244 const int line = nd_line(node);
8245 const NODE *line_node = node;
8246 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8247 LABEL *retry_label = NEW_LABEL(line);
8248 LABEL *retry_end_l = NEW_LABEL(line);
8251 ADD_LABEL(ret, retry_label);
8252 if (nd_type_p(node, NODE_FOR)) {
8253 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8255 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8256 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8257 ISEQ_TYPE_BLOCK, line);
8258 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8261 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8262 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8263 ISEQ_TYPE_BLOCK, line);
8264 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8278 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
8279 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8280 iobj = (
INSN*) get_prev_insn(iobj);
8282 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
8286 if (&iobj->link == LAST_ELEMENT(ret)) {
8292 ADD_INSN(ret, line_node, pop);
8295 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8297 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8307 const NODE *line_node = node;
8308 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8309 LABEL *not_single = NEW_LABEL(nd_line(var));
8310 LABEL *not_ary = NEW_LABEL(nd_line(var));
8311 CHECK(COMPILE(ret,
"for var", var));
8312 ADD_INSN(ret, line_node, dup);
8313 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8314 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8315 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8316 ADD_INSNL(ret, line_node, branchunless, not_single);
8317 ADD_INSN(ret, line_node, dup);
8318 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8319 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8320 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8321 ADD_INSN(ret, line_node, swap);
8323 ADD_INSN(ret, line_node, dup);
8324 ADD_INSNL(ret, line_node, branchunless, not_ary);
8325 ADD_INSN(ret, line_node, swap);
8326 ADD_LABEL(ret, not_ary);
8327 ADD_INSN(ret, line_node, pop);
8328 ADD_LABEL(ret, not_single);
8335 const NODE *line_node = node;
8336 unsigned long throw_flag = 0;
8338 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8340 LABEL *splabel = NEW_LABEL(0);
8341 ADD_LABEL(ret, splabel);
8342 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8343 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8344 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8345 add_ensure_iseq(ret, iseq, 0);
8346 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8347 ADD_ADJUST_RESTORE(ret, splabel);
8350 ADD_INSN(ret, line_node, putnil);
8357 if (!ISEQ_COMPILE_DATA(ip)) {
8362 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8363 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8365 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8368 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8369 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8373 ip = ISEQ_BODY(ip)->parent_iseq;
8378 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8379 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8381 ADD_INSN(ret, line_node, pop);
8385 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8394 const NODE *line_node = node;
8395 unsigned long throw_flag = 0;
8397 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8398 LABEL *splabel = NEW_LABEL(0);
8399 debugs(
"next in while loop\n");
8400 ADD_LABEL(ret, splabel);
8401 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8402 add_ensure_iseq(ret, iseq, 0);
8403 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8404 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8405 ADD_ADJUST_RESTORE(ret, splabel);
8407 ADD_INSN(ret, line_node, putnil);
8410 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8411 LABEL *splabel = NEW_LABEL(0);
8412 debugs(
"next in block\n");
8413 ADD_LABEL(ret, splabel);
8414 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8415 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8416 add_ensure_iseq(ret, iseq, 0);
8417 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8418 ADD_ADJUST_RESTORE(ret, splabel);
8421 ADD_INSN(ret, line_node, putnil);
8428 if (!ISEQ_COMPILE_DATA(ip)) {
8433 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8434 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8438 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8441 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8442 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8446 ip = ISEQ_BODY(ip)->parent_iseq;
8449 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8450 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8453 ADD_INSN(ret, line_node, pop);
8457 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
8467 const NODE *line_node = node;
8469 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8470 LABEL *splabel = NEW_LABEL(0);
8471 debugs(
"redo in while");
8472 ADD_LABEL(ret, splabel);
8473 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8474 add_ensure_iseq(ret, iseq, 0);
8475 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8476 ADD_ADJUST_RESTORE(ret, splabel);
8478 ADD_INSN(ret, line_node, putnil);
8481 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8482 LABEL *splabel = NEW_LABEL(0);
8484 debugs(
"redo in block");
8485 ADD_LABEL(ret, splabel);
8486 add_ensure_iseq(ret, iseq, 0);
8487 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8488 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8489 ADD_ADJUST_RESTORE(ret, splabel);
8492 ADD_INSN(ret, line_node, putnil);
8499 if (!ISEQ_COMPILE_DATA(ip)) {
8504 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8507 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8510 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8511 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
8515 ip = ISEQ_BODY(ip)->parent_iseq;
8518 ADD_INSN(ret, line_node, putnil);
8519 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8522 ADD_INSN(ret, line_node, pop);
8526 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
8536 const NODE *line_node = node;
8538 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
8539 ADD_INSN(ret, line_node, putnil);
8540 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
8543 ADD_INSN(ret, line_node, pop);
8547 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
8556 const int line = nd_line(node);
8557 const NODE *line_node = node;
8558 LABEL *lstart = NEW_LABEL(line);
8559 LABEL *lend = NEW_LABEL(line);
8560 LABEL *lcont = NEW_LABEL(line);
8561 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
8563 ISEQ_BODY(iseq)->location.label),
8564 ISEQ_TYPE_RESCUE, line);
8566 lstart->rescued = LABEL_RESCUE_BEG;
8567 lend->rescued = LABEL_RESCUE_END;
8568 ADD_LABEL(ret, lstart);
8570 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
8571 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
8573 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
8575 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
8577 ADD_LABEL(ret, lend);
8578 if (RNODE_RESCUE(node)->nd_else) {
8579 ADD_INSN(ret, line_node, pop);
8580 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
8582 ADD_INSN(ret, line_node, nop);
8583 ADD_LABEL(ret, lcont);
8586 ADD_INSN(ret, line_node, pop);
8590 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
8591 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
8598 const int line = nd_line(node);
8599 const NODE *line_node = node;
8600 const NODE *resq = node;
8602 LABEL *label_miss, *label_hit;
8605 label_miss = NEW_LABEL(line);
8606 label_hit = NEW_LABEL(line);
8608 narg = RNODE_RESBODY(resq)->nd_args;
8610 switch (nd_type(narg)) {
8613 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8614 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
8615 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8616 ADD_INSNL(ret, line_node, branchif, label_hit);
8617 narg = RNODE_LIST(narg)->nd_next;
8623 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8624 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
8625 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
8626 ADD_INSNL(ret, line_node, branchif, label_hit);
8629 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
8633 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8635 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8636 ADD_INSNL(ret, line_node, branchif, label_hit);
8638 ADD_INSNL(ret, line_node, jump, label_miss);
8639 ADD_LABEL(ret, label_hit);
8642 if (RNODE_RESBODY(resq)->nd_exc_var) {
8643 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
8646 if (nd_type(RNODE_RESBODY(resq)->nd_body) == NODE_BEGIN && RNODE_BEGIN(RNODE_RESBODY(resq)->nd_body)->nd_body == NULL && !RNODE_RESBODY(resq)->nd_exc_var) {
8648 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
8651 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
8654 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
8655 ADD_INSN(ret, line_node, nop);
8657 ADD_INSN(ret, line_node, leave);
8658 ADD_LABEL(ret, label_miss);
8659 resq = RNODE_RESBODY(resq)->nd_next;
8667 const int line = nd_line(RNODE_ENSURE(node)->nd_ensr);
8668 const NODE *line_node = node;
8670 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
8672 ISEQ_TYPE_ENSURE, line);
8673 LABEL *lstart = NEW_LABEL(line);
8674 LABEL *lend = NEW_LABEL(line);
8675 LABEL *lcont = NEW_LABEL(line);
8683 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
8685 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
8690 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
8692 ADD_LABEL(ret, lstart);
8693 CHECK(COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
8694 ADD_LABEL(ret, lend);
8696 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
8697 ADD_LABEL(ret, lcont);
8698 if (last_leave) ADD_INSN(ret, line_node, pop);
8700 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
8701 if (lstart->link.next != &lend->link) {
8703 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
8705 erange = erange->next;
8709 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
8716 const NODE *line_node = node;
8719 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8721 enum rb_iseq_type t =
type;
8722 const NODE *retval = RNODE_RETURN(node)->nd_stts;
8725 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
8726 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
8727 t = ISEQ_BODY(is)->type;
8731 case ISEQ_TYPE_MAIN:
8733 rb_warn(
"argument of top-level return is ignored");
8737 type = ISEQ_TYPE_METHOD;
8744 if (
type == ISEQ_TYPE_METHOD) {
8745 splabel = NEW_LABEL(0);
8746 ADD_LABEL(ret, splabel);
8747 ADD_ADJUST(ret, line_node, 0);
8750 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
8752 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8753 add_ensure_iseq(ret, iseq, 1);
8755 ADD_INSN(ret, line_node, leave);
8756 ADD_ADJUST_RESTORE(ret, splabel);
8759 ADD_INSN(ret, line_node, putnil);
8763 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
8765 ADD_INSN(ret, line_node, pop);
8776 if (!i)
return false;
8777 if (IS_TRACE(i)) i = i->prev;
8778 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
8780 if (IS_ADJUST(i)) i = i->prev;
8781 if (!IS_INSN(i))
return false;
8782 switch (INSN_OF(i)) {
8789 (ret->last = last->prev)->next = NULL;
8796 CHECK(COMPILE_(ret,
"nd_body", node, popped));
8798 if (!popped && !all_string_result_p(node)) {
8799 const NODE *line_node = node;
8800 const unsigned int flag = VM_CALL_FCALL;
8804 ADD_INSN(ret, line_node, dup);
8805 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
8806 ADD_INSN(ret, line_node, anytostring);
8814 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8816 debugs(
"id: %s idx: %d\n",
rb_id2name(
id), idx);
8817 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8823 LABEL *else_label = NEW_LABEL(nd_line(line_node));
8826 br = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"&.");
8828 ADD_INSN(recv, line_node, dup);
8829 ADD_INSNL(recv, line_node, branchnil, else_label);
8830 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
8838 if (!else_label)
return;
8839 end_label = NEW_LABEL(nd_line(line_node));
8840 ADD_INSNL(ret, line_node, jump, end_label);
8841 ADD_LABEL(ret, else_label);
8842 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
8843 ADD_LABEL(ret, end_label);
8852 if (get_nd_recv(node) &&
8853 (nd_type_p(get_nd_recv(node), NODE_STR) || nd_type_p(get_nd_recv(node), NODE_FILE)) &&
8854 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
8855 get_nd_args(node) == NULL &&
8856 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8857 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8858 VALUE str = get_string_value(get_nd_recv(node));
8859 if (get_node_call_nd_mid(node) == idUMinus) {
8860 ADD_INSN2(ret, line_node, opt_str_uminus, str,
8861 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
8864 ADD_INSN2(ret, line_node, opt_str_freeze, str,
8865 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
8869 ADD_INSN(ret, line_node, pop);
8876 if (get_node_call_nd_mid(node) == idAREF && !private_recv_p(node) && get_nd_args(node) &&
8877 nd_type_p(get_nd_args(node), NODE_LIST) && RNODE_LIST(get_nd_args(node))->as.nd_alen == 1 &&
8878 (nd_type_p(RNODE_LIST(get_nd_args(node))->nd_head, NODE_STR) || nd_type_p(RNODE_LIST(get_nd_args(node))->nd_head, NODE_FILE)) &&
8879 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8880 !frozen_string_literal_p(iseq) &&
8881 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8882 VALUE str = get_string_value(RNODE_LIST(get_nd_args(node))->nd_head);
8883 CHECK(COMPILE(ret,
"recv", get_nd_recv(node)));
8884 ADD_INSN2(ret, line_node, opt_aref_with, str,
8885 new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
8888 ADD_INSN(ret, line_node, pop);
8896 iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
8898 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
8902 iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
8905 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
8906 for (i=0; table[i].index != -1; i++) {
8907 if (strcmp(table[i].name, name) == 0) {
8915 iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
8918 static const char prefix[] =
"__builtin_";
8919 const size_t prefix_len =
sizeof(prefix) - 1;
8924 switch (nd_type(recv)) {
8926 if (RNODE_VCALL(recv)->nd_mid ==
rb_intern(
"__builtin")) {
8931 if (RNODE_CONST(recv)->nd_vid ==
rb_intern(
"Primitive")) {
8941 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
8942 return &name[prefix_len];
8951 delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
8958 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
8959 unsigned int start=0;
8964 argc + start <= ISEQ_BODY(iseq)->local_table_size;
8968 for (
unsigned int i=start; i-start<argc; i++) {
8969 if (IS_INSN(elem) &&
8970 INSN_OF(elem) == BIN(getlocal)) {
8971 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
8972 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
8974 if (local_level == 0) {
8975 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
8977 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
8978 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
8979 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
8980 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9004 *pstart_index = start;
9018 if (!node)
goto no_arg;
9020 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9021 const NODE *next = RNODE_LIST(node)->nd_next;
9023 node = RNODE_LIST(node)->nd_head;
9024 if (!node)
goto no_arg;
9025 switch (nd_type(node)) {
9027 symbol = rb_node_sym_string_val(node);
9033 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9037 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9039 else if (strcmp(
RSTRING_PTR(
string),
"inline_block") == 0) {
9040 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9042 else if (strcmp(
RSTRING_PTR(
string),
"use_block") == 0) {
9043 iseq_set_use_block(iseq);
9045 else if (strcmp(
RSTRING_PTR(
string),
"c_trace") == 0) {
9047 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9056 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9059 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9062 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %s",
RSTRING_PTR(
string));
9065 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9073 if (!node)
goto no_arg;
9074 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9075 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9076 node = RNODE_LIST(node)->nd_head;
9077 if (!node)
goto no_arg;
9078 switch (nd_type(node)) {
9080 name = rb_node_sym_string_val(node);
9085 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9087 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9091 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9094 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9097 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9098 rb_builtin_class_name(name));
9101 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9105 mandatory_node(
const rb_iseq_t *iseq,
const NODE *cond_node)
9107 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9108 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9109 return RNODE_IF(node)->nd_body;
9112 rb_bug(
"mandatory_node: can't find mandatory node");
9117 compile_builtin_mandatory_only_method(
rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9121 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9124 rb_node_init(RNODE(&args_node), NODE_ARGS);
9125 args_node.nd_ainfo = args;
9128 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9129 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9133 tbl->size = table_size;
9138 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9139 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9142 for (; i<table_size; i++) {
9143 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9147 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9148 scope_node.nd_tbl = tbl;
9149 scope_node.nd_body = mandatory_node(iseq, node);
9150 scope_node.nd_args = &args_node;
9152 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9154 ISEQ_BODY(iseq)->mandatory_only_iseq =
9155 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9156 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9157 nd_line(line_node), NULL, 0,
9158 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9159 ISEQ_BODY(iseq)->variable.script_lines);
9169 NODE *args_node = get_nd_args(node);
9171 if (parent_block != NULL) {
9172 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9176 # define BUILTIN_INLINE_PREFIX "_bi"
9177 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9178 bool cconst =
false;
9183 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9184 strcmp(
"cexpr!", builtin_func) == 0) {
9187 else if (strcmp(
"cconst!", builtin_func) == 0) {
9190 else if (strcmp(
"cinit!", builtin_func) == 0) {
9194 else if (strcmp(
"attr!", builtin_func) == 0) {
9195 return compile_builtin_attr(iseq, args_node);
9197 else if (strcmp(
"arg!", builtin_func) == 0) {
9198 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9200 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9202 rb_bug(
"mandatory_only? should be in if condition");
9204 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9205 rb_bug(
"mandatory_only? should be put on top");
9208 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9209 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9212 rb_bug(
"can't find builtin function:%s", builtin_func);
9215 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9219 int inline_index = nd_line(node);
9220 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9221 builtin_func = inline_func;
9227 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9228 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9229 ADD_INSN1(ret, line_node, putobject, const_val);
9235 unsigned int flag = 0;
9237 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9239 if (
FIX2INT(argc) != bf->argc) {
9240 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9241 builtin_func, bf->argc,
FIX2INT(argc));
9245 unsigned int start_index;
9246 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9247 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9251 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9254 if (popped) ADD_INSN(ret, line_node, pop);
9260 compile_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)
9268 ID mid = get_node_call_nd_mid(node);
9270 unsigned int flag = 0;
9272 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9273 LABEL *else_label = NULL;
9276 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9280 #if OPT_SUPPORT_JOKE
9281 if (nd_type_p(node, NODE_VCALL)) {
9286 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9288 if (mid == id_bitblt) {
9289 ADD_INSN(ret, line_node, bitblt);
9292 else if (mid == id_answer) {
9293 ADD_INSN(ret, line_node, answer);
9305 if (nd_type_p(node, NODE_FCALL) &&
9306 (mid == goto_id || mid == label_id)) {
9309 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9312 if (!labels_table) {
9313 labels_table = st_init_numtable();
9314 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9317 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9321 if (mid == goto_id) {
9322 ADD_INSNL(ret, line_node, jump, label);
9325 ADD_LABEL(ret, label);
9332 const char *builtin_func;
9333 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9334 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9335 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9339 if (!assume_receiver) {
9340 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9343 if (mid == idCall &&
9344 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9345 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9346 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9348 else if (private_recv_p(node)) {
9349 ADD_INSN(recv, node, putself);
9350 flag |= VM_CALL_FCALL;
9353 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9356 if (
type == NODE_QCALL) {
9357 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9360 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9361 ADD_CALL_RECEIVER(recv, line_node);
9366 if (
type != NODE_VCALL) {
9367 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9368 CHECK(!
NIL_P(argc));
9377 debugp_param(
"call args argc", argc);
9378 debugp_param(
"call method",
ID2SYM(mid));
9380 switch ((
int)
type) {
9382 flag |= VM_CALL_VCALL;
9385 flag |= VM_CALL_FCALL;
9388 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9389 ADD_INSN(ret, line_node, splatkw);
9391 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9393 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9395 ADD_INSN(ret, line_node, pop);
9403 const int line = nd_line(node);
9405 unsigned int flag = 0;
9407 ID id = RNODE_OP_ASGN1(node)->nd_mid;
9433 ADD_INSN(ret, node, putnil);
9435 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
9436 CHECK(asgnflag != -1);
9437 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
9442 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
9443 CHECK(!
NIL_P(argc));
9445 int dup_argn =
FIX2INT(argc) + 1;
9446 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
9448 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
9450 if (
id == idOROP ||
id == idANDOP) {
9459 LABEL *label = NEW_LABEL(line);
9460 LABEL *lfin = NEW_LABEL(line);
9462 ADD_INSN(ret, node, dup);
9464 ADD_INSNL(ret, node, branchif, label);
9467 ADD_INSNL(ret, node, branchunless, label);
9469 ADD_INSN(ret, node, pop);
9471 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9473 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9475 if (flag & VM_CALL_ARGS_SPLAT) {
9476 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9477 ADD_INSN(ret, node, swap);
9478 ADD_INSN1(ret, node, splatarray,
Qtrue);
9479 ADD_INSN(ret, node, swap);
9480 flag |= VM_CALL_ARGS_SPLAT_MUT;
9482 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9483 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9486 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9488 ADD_INSN(ret, node, pop);
9489 ADD_INSNL(ret, node, jump, lfin);
9490 ADD_LABEL(ret, label);
9492 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9494 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
9495 ADD_LABEL(ret, lfin);
9498 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9499 ADD_SEND(ret, node,
id,
INT2FIX(1));
9501 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9503 if (flag & VM_CALL_ARGS_SPLAT) {
9504 if (flag & VM_CALL_KW_SPLAT) {
9505 ADD_INSN1(ret, node, topn,
INT2FIX(2));
9506 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9507 ADD_INSN1(ret, node, splatarray,
Qtrue);
9508 flag |= VM_CALL_ARGS_SPLAT_MUT;
9510 ADD_INSN(ret, node, swap);
9511 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9512 ADD_INSN1(ret, node, setn,
INT2FIX(2));
9513 ADD_INSN(ret, node, pop);
9516 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9517 ADD_INSN(ret, node, swap);
9518 ADD_INSN1(ret, node, splatarray,
Qtrue);
9519 ADD_INSN(ret, node, swap);
9520 flag |= VM_CALL_ARGS_SPLAT_MUT;
9522 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9524 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9527 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9529 ADD_INSN(ret, node, pop);
9537 const int line = nd_line(node);
9538 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
9541 LABEL *lfin = NEW_LABEL(line);
9542 LABEL *lcfin = NEW_LABEL(line);
9597 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
9598 CHECK(asgnflag != -1);
9599 if (RNODE_OP_ASGN2(node)->nd_aid) {
9600 lskip = NEW_LABEL(line);
9601 ADD_INSN(ret, node, dup);
9602 ADD_INSNL(ret, node, branchnil, lskip);
9604 ADD_INSN(ret, node, dup);
9605 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
9607 if (atype == idOROP || atype == idANDOP) {
9609 ADD_INSN(ret, node, dup);
9611 if (atype == idOROP) {
9612 ADD_INSNL(ret, node, branchif, lcfin);
9615 ADD_INSNL(ret, node, branchunless, lcfin);
9618 ADD_INSN(ret, node, pop);
9620 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9622 ADD_INSN(ret, node, swap);
9623 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9625 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9626 ADD_INSNL(ret, node, jump, lfin);
9628 ADD_LABEL(ret, lcfin);
9630 ADD_INSN(ret, node, swap);
9633 ADD_LABEL(ret, lfin);
9636 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9637 ADD_SEND(ret, node, atype,
INT2FIX(1));
9639 ADD_INSN(ret, node, swap);
9640 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9642 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9644 if (lskip && popped) {
9645 ADD_LABEL(ret, lskip);
9647 ADD_INSN(ret, node, pop);
9648 if (lskip && !popped) {
9649 ADD_LABEL(ret, lskip);
9654 static int compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
9659 const int line = nd_line(node);
9664 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
9666 ADD_INSN1(ret, node, putobject, rb_cObject);
9669 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
9672 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
9673 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
9676 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
9678 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
9679 lassign = NEW_LABEL(line);
9680 ADD_INSN(ret, node, dup);
9681 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
9683 ADD_INSNL(ret, node, branchunless, lassign);
9685 ADD_INSN(ret, node, dup);
9686 ADD_INSN1(ret, node, putobject,
Qtrue);
9687 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
9689 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
9690 lfin = NEW_LABEL(line);
9691 if (!popped) ADD_INSN(ret, node, dup);
9692 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
9693 ADD_INSNL(ret, node, branchif, lfin);
9695 ADD_INSNL(ret, node, branchunless, lfin);
9697 if (!popped) ADD_INSN(ret, node, pop);
9698 if (lassign) ADD_LABEL(ret, lassign);
9699 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9702 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9704 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
9705 ADD_INSN(ret, node, swap);
9707 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9708 ADD_LABEL(ret, lfin);
9709 if (!popped) ADD_INSN(ret, node, swap);
9710 ADD_INSN(ret, node, pop);
9713 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9715 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
9717 ADD_INSN(ret, node, swap);
9719 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9720 ADD_INSN(ret, node, swap);
9722 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9730 const int line = nd_line(node);
9731 LABEL *lfin = NEW_LABEL(line);
9734 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
9738 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
9739 lassign = lfinish[1];
9741 lassign = NEW_LABEL(line);
9743 ADD_INSNL(ret, node, branchunless, lassign);
9746 lassign = NEW_LABEL(line);
9749 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
9752 ADD_INSN(ret, node, dup);
9755 if (
type == NODE_OP_ASGN_AND) {
9756 ADD_INSNL(ret, node, branchunless, lfin);
9759 ADD_INSNL(ret, node, branchif, lfin);
9763 ADD_INSN(ret, node, pop);
9766 ADD_LABEL(ret, lassign);
9767 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
9768 ADD_LABEL(ret, lfin);
9778 unsigned int flag = 0;
9780 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9784 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9786 if (
type == NODE_SUPER) {
9787 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
9788 CHECK(!
NIL_P(vargc));
9790 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9791 ADD_INSN(args, node, splatkw);
9794 if (flag & VM_CALL_ARGS_BLOCKARG) {
9801 const rb_iseq_t *liseq = body->local_iseq;
9803 const struct rb_iseq_param_keyword *
const local_kwd = local_body->
param.keyword;
9804 int lvar_level = get_lvar_level(iseq);
9806 argc = local_body->
param.lead_num;
9809 for (i = 0; i < local_body->
param.lead_num; i++) {
9810 int idx = local_body->local_table_size - i;
9811 ADD_GETLOCAL(args, node, idx, lvar_level);
9815 if (local_body->
param.flags.forwardable) {
9816 flag |= VM_CALL_FORWARDING;
9817 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
9818 ADD_GETLOCAL(args, node, idx, lvar_level);
9821 if (local_body->
param.flags.has_opt) {
9824 for (j = 0; j < local_body->
param.opt_num; j++) {
9825 int idx = local_body->local_table_size - (i + j);
9826 ADD_GETLOCAL(args, node, idx, lvar_level);
9831 if (local_body->
param.flags.has_rest) {
9833 int idx = local_body->local_table_size - local_body->
param.rest_start;
9834 ADD_GETLOCAL(args, node, idx, lvar_level);
9835 ADD_INSN1(args, node, splatarray, RBOOL(local_body->
param.flags.has_post));
9837 argc = local_body->
param.rest_start + 1;
9838 flag |= VM_CALL_ARGS_SPLAT;
9840 if (local_body->
param.flags.has_post) {
9842 int post_len = local_body->
param.post_num;
9843 int post_start = local_body->
param.post_start;
9845 if (local_body->
param.flags.has_rest) {
9847 for (j=0; j<post_len; j++) {
9848 int idx = local_body->local_table_size - (post_start + j);
9849 ADD_GETLOCAL(args, node, idx, lvar_level);
9851 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
9852 flag |= VM_CALL_ARGS_SPLAT_MUT;
9857 for (j=0; j<post_len; j++) {
9858 int idx = local_body->local_table_size - (post_start + j);
9859 ADD_GETLOCAL(args, node, idx, lvar_level);
9861 argc = post_len + post_start;
9865 if (local_body->
param.flags.has_kw) {
9866 int local_size = local_body->local_table_size;
9869 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9871 if (local_body->
param.flags.has_kwrest) {
9872 int idx = local_body->local_table_size - local_kwd->rest_start;
9873 ADD_GETLOCAL(args, node, idx, lvar_level);
9878 ADD_INSN1(args, node, newhash,
INT2FIX(0));
9880 for (i = 0; i < local_kwd->num; ++i) {
9881 ID id = local_kwd->table[i];
9882 int idx = local_size - get_local_var_idx(liseq,
id);
9883 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
9884 ADD_GETLOCAL(args, node, idx, lvar_level);
9886 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
9887 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
9889 else if (local_body->
param.flags.has_kwrest) {
9890 int idx = local_body->local_table_size - local_kwd->rest_start;
9891 ADD_GETLOCAL(args, node, idx, lvar_level);
9893 flag |= VM_CALL_KW_SPLAT;
9897 if (use_block && parent_block == NULL) {
9898 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
9901 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
9902 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
9903 ADD_INSN(ret, node, putself);
9906 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
9908 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
9909 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
9912 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
9916 ADD_INSN(ret, node, pop);
9926 unsigned int flag = 0;
9931 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
9933 case ISEQ_TYPE_MAIN:
9934 case ISEQ_TYPE_CLASS:
9935 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
9940 if (RNODE_YIELD(node)->nd_head) {
9941 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
9942 CHECK(!
NIL_P(argc));
9949 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
9950 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
9953 ADD_INSN(ret, node, pop);
9958 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
9959 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
9961 if (level > 0) access_outer_variables(iseq, level,
rb_intern(
"yield"),
true);
9974 switch ((
int)
type) {
9976 ADD_INSN1(recv, node, putobject, rb_node_regx_string_val(node));
9977 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
9981 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
9982 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
9985 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
9986 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
9992 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
9994 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
9995 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
9999 ADD_INSN(ret, node, pop);
10010 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10011 (segments = collect_const_segments(iseq, node))) {
10012 ISEQ_BODY(iseq)->ic_size++;
10013 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10023 CHECK(compile_const_prefix(iseq, node, pref, body));
10024 if (LIST_INSN_SIZE_ZERO(pref)) {
10025 ADD_INSN(ret, node, putnil);
10026 ADD_SEQ(ret, body);
10029 ADD_SEQ(ret, pref);
10030 ADD_SEQ(ret, body);
10036 ADD_CALL_RECEIVER(ret, node);
10037 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10038 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10041 ADD_INSN(ret, node, pop);
10049 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10052 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10053 ISEQ_BODY(iseq)->ic_size++;
10055 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10059 ADD_INSN1(ret, node, putobject, rb_cObject);
10060 ADD_INSN1(ret, node, putobject,
Qtrue);
10061 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10065 ADD_INSN(ret, node, pop);
10074 const NODE *b = RNODE_DOT2(node)->nd_beg;
10075 const NODE *e = RNODE_DOT2(node)->nd_end;
10077 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10079 VALUE bv = optimized_range_item(b);
10080 VALUE ev = optimized_range_item(e);
10082 ADD_INSN1(ret, node, putobject, val);
10087 CHECK(COMPILE_(ret,
"min", b, popped));
10088 CHECK(COMPILE_(ret,
"max", e, popped));
10090 ADD_INSN1(ret, node, newrange, flag);
10100 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10101 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10107 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10110 ip = ISEQ_BODY(ip)->parent_iseq;
10114 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10117 ADD_INSN(ret, node, putnil);
10128 LABEL *end_label = NEW_LABEL(nd_line(node));
10129 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10131 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10133 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10136 else if (nd_type_p(default_value, NODE_SYM) ||
10137 nd_type_p(default_value, NODE_REGX) ||
10138 nd_type_p(default_value, NODE_LINE) ||
10139 nd_type_p(default_value, NODE_INTEGER) ||
10140 nd_type_p(default_value, NODE_FLOAT) ||
10141 nd_type_p(default_value, NODE_RATIONAL) ||
10142 nd_type_p(default_value, NODE_IMAGINARY) ||
10143 nd_type_p(default_value, NODE_NIL) ||
10144 nd_type_p(default_value, NODE_TRUE) ||
10145 nd_type_p(default_value, NODE_FALSE)) {
10146 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10154 int kw_bits_idx = body->local_table_size - body->
param.keyword->bits_start;
10155 int keyword_idx = body->
param.keyword->num;
10157 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10158 ADD_INSNL(ret, node, branchif, end_label);
10159 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10160 ADD_LABEL(ret, end_label);
10170 unsigned int flag = 0;
10171 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10173 LABEL *else_label = NULL;
10179 if (mid == idASET && !private_recv_p(node) && RNODE_ATTRASGN(node)->nd_args &&
10180 nd_type_p(RNODE_ATTRASGN(node)->nd_args, NODE_LIST) && RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->as.nd_alen == 2 &&
10181 (nd_type_p(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_head, NODE_STR) || nd_type_p(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_head, NODE_FILE)) &&
10182 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
10183 !frozen_string_literal_p(iseq) &&
10184 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction)
10186 VALUE str = get_string_value(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_head);
10187 CHECK(COMPILE(ret,
"recv", RNODE_ATTRASGN(node)->nd_recv));
10188 CHECK(COMPILE(ret,
"value", RNODE_LIST(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_next)->nd_head));
10190 ADD_INSN(ret, node, swap);
10191 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10193 ADD_INSN2(ret, node, opt_aset_with, str,
10194 new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
10196 ADD_INSN(ret, node, pop);
10202 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10203 CHECK(!
NIL_P(argc));
10205 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10206 CHECK(asgnflag != -1);
10207 flag |= (
unsigned int)asgnflag;
10209 debugp_param(
"argc", argc);
10210 debugp_param(
"nd_mid",
ID2SYM(mid));
10215 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10218 ADD_INSN(ret, node, putnil);
10219 ADD_SEQ(ret, recv);
10220 ADD_SEQ(ret, args);
10222 if (flag & VM_CALL_ARGS_SPLAT) {
10223 ADD_INSN(ret, node, dup);
10224 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10225 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10226 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10227 ADD_INSN (ret, node, pop);
10230 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10234 ADD_SEQ(ret, recv);
10235 ADD_SEQ(ret, args);
10237 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10238 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10239 ADD_INSN(ret, node, pop);
10246 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10268 node_const_decl_val(
const NODE *node)
10271 switch (nd_type(node)) {
10273 if (RNODE_CDECL(node)->nd_vid) {
10274 path =
rb_id2str(RNODE_CDECL(node)->nd_vid);
10278 node = RNODE_CDECL(node)->nd_else;
10289 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10295 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10298 if (node && nd_type_p(node, NODE_CONST)) {
10302 else if (node && nd_type_p(node, NODE_COLON3)) {
10314 path = rb_fstring(path);
10319 const_decl_path(
NODE *dest)
10322 if (!nd_type_p(dest, NODE_CALL)) {
10323 path = node_const_decl_val(dest);
10334 VALUE path = const_decl_path(dest);
10335 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10336 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10337 ADD_INSN1(ret, value, putobject, path);
10344 #ifndef SHAREABLE_BARE_EXPRESSION
10345 #define SHAREABLE_BARE_EXPRESSION 1
10349 compile_shareable_literal_constant(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
NODE *dest,
const NODE *node,
size_t level,
VALUE *value_p,
int *shareable_literal_p)
10351 # define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10352 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10354 DECL_ANCHOR(anchor);
10356 enum node_type
type = nd_type(node);
10368 *value_p = rb_node_sym_string_val(node);
10371 *value_p = rb_node_regx_string_val(node);
10374 *value_p = rb_node_line_lineno_val(node);
10377 *value_p = rb_node_integer_literal_val(node);
10380 *value_p = rb_node_float_literal_val(node);
10382 case NODE_RATIONAL:
10383 *value_p = rb_node_rational_literal_val(node);
10385 case NODE_IMAGINARY:
10386 *value_p = rb_node_imaginary_literal_val(node);
10388 case NODE_ENCODING:
10389 *value_p = rb_node_encoding_val(node);
10392 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10393 *shareable_literal_p = 1;
10397 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10398 if (shareable == rb_parser_shareable_literal) {
10404 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10407 *shareable_literal_p = 1;
10411 VALUE lit = rb_node_str_string_val(node);
10412 ADD_INSN1(ret, node, putobject, lit);
10415 *shareable_literal_p = 1;
10421 VALUE lit = rb_node_file_path_val(node);
10422 ADD_INSN1(ret, node, putobject, lit);
10425 *shareable_literal_p = 1;
10433 ADD_INSN1(ret, node, putobject, lit);
10436 *shareable_literal_p = 1;
10442 INIT_ANCHOR(anchor);
10444 for (
NODE *n = (
NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
10446 int shareable_literal_p2;
10447 NODE *elt = RNODE_LIST(n)->nd_head;
10449 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
10450 if (shareable_literal_p2) {
10453 else if (
RTEST(lit)) {
10459 if (!UNDEF_P(val)) {
10471 if (!RNODE_HASH(node)->nd_brace) {
10473 *shareable_literal_p = 0;
10477 INIT_ANCHOR(anchor);
10479 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10482 int shareable_literal_p2;
10483 NODE *key = RNODE_LIST(n)->nd_head;
10484 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
10486 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10487 if (shareable_literal_p2) {
10490 else if (
RTEST(lit)) {
10496 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10497 if (shareable_literal_p2) {
10500 else if (
RTEST(lit)) {
10506 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
10519 if (shareable == rb_parser_shareable_literal &&
10520 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
10521 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
10523 *shareable_literal_p = 1;
10526 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10528 *shareable_literal_p = 0;
10534 if (nd_type(node) == NODE_LIST) {
10535 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10537 else if (nd_type(node) == NODE_HASH) {
10538 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10539 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10542 *shareable_literal_p = 0;
10543 ADD_SEQ(ret, anchor);
10549 if (nd_type(node) == NODE_LIST) {
10550 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10552 else if (nd_type(node) == NODE_HASH) {
10553 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10554 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10556 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
10558 *shareable_literal_p = 1;
10562 ADD_INSN1(ret, node, putobject, val);
10565 *shareable_literal_p = 1;
10572 compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
10576 DECL_ANCHOR(anchor);
10577 INIT_ANCHOR(anchor);
10579 switch (shareable) {
10580 case rb_parser_shareable_none:
10581 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
10584 case rb_parser_shareable_literal:
10585 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10586 ADD_SEQ(ret, anchor);
10589 case rb_parser_shareable_copy:
10590 case rb_parser_shareable_everything:
10591 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10593 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
10596 ADD_SEQ(ret, anchor);
10600 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
10617 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
10618 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
10619 debugs(
"node: NODE_NIL(implicit)\n");
10620 ADD_SYNTHETIC_INSN(ret, lineno, -1, putnil);
10624 return iseq_compile_each0(iseq, ret, node, popped);
10630 const int line = (int)nd_line(node);
10631 const enum node_type
type = nd_type(node);
10634 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
10638 if (nd_fl_newline(node)) {
10640 ISEQ_COMPILE_DATA(iseq)->last_line = line;
10641 if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
10642 event |= RUBY_EVENT_COVERAGE_LINE;
10644 ADD_TRACE(ret, event);
10648 debug_node_start(node);
10649 #undef BEFORE_RETURN
10650 #define BEFORE_RETURN debug_node_end()
10654 CHECK(compile_block(iseq, ret, node, popped));
10658 CHECK(compile_if(iseq, ret, node, popped,
type));
10661 CHECK(compile_case(iseq, ret, node, popped));
10664 CHECK(compile_case2(iseq, ret, node, popped));
10667 CHECK(compile_case3(iseq, ret, node, popped));
10671 CHECK(compile_loop(iseq, ret, node, popped,
type));
10675 CHECK(compile_iter(iseq, ret, node, popped));
10677 case NODE_FOR_MASGN:
10678 CHECK(compile_for_masgn(iseq, ret, node, popped));
10681 CHECK(compile_break(iseq, ret, node, popped));
10684 CHECK(compile_next(iseq, ret, node, popped));
10687 CHECK(compile_redo(iseq, ret, node, popped));
10690 CHECK(compile_retry(iseq, ret, node, popped));
10693 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
10697 CHECK(compile_rescue(iseq, ret, node, popped));
10700 CHECK(compile_resbody(iseq, ret, node, popped));
10703 CHECK(compile_ensure(iseq, ret, node, popped));
10708 LABEL *end_label = NEW_LABEL(line);
10709 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(node)->nd_1st));
10711 ADD_INSN(ret, node, dup);
10713 if (
type == NODE_AND) {
10714 ADD_INSNL(ret, node, branchunless, end_label);
10717 ADD_INSNL(ret, node, branchif, end_label);
10720 ADD_INSN(ret, node, pop);
10722 CHECK(COMPILE_(ret,
"nd_2nd", RNODE_OR(node)->nd_2nd, popped));
10723 ADD_LABEL(ret, end_label);
10728 compile_massign(iseq, ret, node, popped);
10733 ID id = RNODE_LASGN(node)->nd_vid;
10734 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
10736 debugs(
"lvar: %s idx: %d\n",
rb_id2name(
id), idx);
10737 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
10740 ADD_INSN(ret, node, dup);
10742 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
10747 ID id = RNODE_DASGN(node)->nd_vid;
10748 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
10749 debugi(
"dassn id",
rb_id2str(
id) ?
id :
'*');
10752 ADD_INSN(ret, node, dup);
10755 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
10758 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
10762 ADD_SETLOCAL(ret, node, ls - idx, lv);
10766 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
10769 ADD_INSN(ret, node, dup);
10771 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
10775 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
10777 ADD_INSN(ret, node, dup);
10779 ADD_INSN2(ret, node, setinstancevariable,
10780 ID2SYM(RNODE_IASGN(node)->nd_vid),
10781 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
10785 if (RNODE_CDECL(node)->nd_vid) {
10786 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
10789 ADD_INSN(ret, node, dup);
10792 ADD_INSN1(ret, node, putspecialobject,
10793 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
10794 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
10797 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
10798 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
10799 ADD_INSN(ret, node, swap);
10802 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10803 ADD_INSN(ret, node, swap);
10806 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
10811 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
10813 ADD_INSN(ret, node, dup);
10815 ADD_INSN2(ret, node, setclassvariable,
10816 ID2SYM(RNODE_CVASGN(node)->nd_vid),
10817 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
10820 case NODE_OP_ASGN1:
10821 CHECK(compile_op_asgn1(iseq, ret, node, popped));
10823 case NODE_OP_ASGN2:
10824 CHECK(compile_op_asgn2(iseq, ret, node, popped));
10826 case NODE_OP_CDECL:
10827 CHECK(compile_op_cdecl(iseq, ret, node, popped));
10829 case NODE_OP_ASGN_AND:
10830 case NODE_OP_ASGN_OR:
10831 CHECK(compile_op_log(iseq, ret, node, popped,
type));
10835 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
10841 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
10847 CHECK(compile_super(iseq, ret, node, popped,
type));
10850 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
10855 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
10860 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
10863 CHECK(compile_return(iseq, ret, node, popped));
10866 CHECK(compile_yield(iseq, ret, node, popped));
10870 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
10876 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
10878 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
10880 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
10884 ADD_GETLOCAL(ret, node, ls - idx, lv);
10889 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
10891 ADD_INSN(ret, node, pop);
10896 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
10898 ADD_INSN2(ret, node, getinstancevariable,
10899 ID2SYM(RNODE_IVAR(node)->nd_vid),
10900 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
10905 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
10907 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10910 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10914 ADD_INSN(ret, node, putnil);
10915 ADD_INSN1(ret, node, putobject,
Qtrue);
10916 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
10920 ADD_INSN(ret, node, pop);
10926 ADD_INSN2(ret, node, getclassvariable,
10927 ID2SYM(RNODE_CVAR(node)->nd_vid),
10928 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
10932 case NODE_NTH_REF:{
10934 if (!RNODE_NTH_REF(node)->nd_nth) {
10935 ADD_INSN(ret, node, putnil);
10938 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
10939 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
10943 case NODE_BACK_REF:{
10945 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
10946 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
10953 CHECK(compile_match(iseq, ret, node, popped,
type));
10957 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
10963 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
10967 case NODE_ENCODING:{
10969 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
10973 case NODE_INTEGER:{
10974 VALUE lit = rb_node_integer_literal_val(node);
10975 debugp_param(
"integer", lit);
10977 ADD_INSN1(ret, node, putobject, lit);
10983 VALUE lit = rb_node_float_literal_val(node);
10984 debugp_param(
"float", lit);
10986 ADD_INSN1(ret, node, putobject, lit);
10991 case NODE_RATIONAL:{
10992 VALUE lit = rb_node_rational_literal_val(node);
10993 debugp_param(
"rational", lit);
10995 ADD_INSN1(ret, node, putobject, lit);
11000 case NODE_IMAGINARY:{
11001 VALUE lit = rb_node_imaginary_literal_val(node);
11002 debugp_param(
"imaginary", lit);
11004 ADD_INSN1(ret, node, putobject, lit);
11011 debugp_param(
"nd_lit", get_string_value(node));
11013 VALUE lit = get_string_value(node);
11016 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11017 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11019 switch (option->frozen_string_literal) {
11020 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11021 ADD_INSN1(ret, node, putchilledstring, lit);
11023 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11024 ADD_INSN1(ret, node, putstring, lit);
11026 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11027 ADD_INSN1(ret, node, putobject, lit);
11030 rb_bug(
"invalid frozen_string_literal");
11037 compile_dstr(iseq, ret, node);
11040 ADD_INSN(ret, node, pop);
11045 ADD_CALL_RECEIVER(ret, node);
11046 VALUE str = rb_node_str_string_val(node);
11047 ADD_INSN1(ret, node, putobject, str);
11049 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11052 ADD_INSN(ret, node, pop);
11057 ADD_CALL_RECEIVER(ret, node);
11058 compile_dstr(iseq, ret, node);
11059 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11062 ADD_INSN(ret, node, pop);
11067 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11071 VALUE lit = rb_node_regx_string_val(node);
11072 ADD_INSN1(ret, node, putobject, lit);
11078 compile_dregx(iseq, ret, node, popped);
11081 int ic_index = body->ise_size++;
11083 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11085 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
11089 ADD_INSN(ret, node, pop);
11093 case NODE_ARGSCAT:{
11095 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11096 ADD_INSN1(ret, node, splatarray,
Qfalse);
11097 ADD_INSN(ret, node, pop);
11098 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11099 ADD_INSN1(ret, node, splatarray,
Qfalse);
11100 ADD_INSN(ret, node, pop);
11103 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11104 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11105 if (nd_type_p(body_node, NODE_LIST)) {
11106 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11109 CHECK(COMPILE(ret,
"argscat body", body_node));
11110 ADD_INSN(ret, node, concattoarray);
11115 case NODE_ARGSPUSH:{
11117 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11118 ADD_INSN1(ret, node, splatarray,
Qfalse);
11119 ADD_INSN(ret, node, pop);
11120 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11123 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11124 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11125 if (keyword_node_p(body_node)) {
11126 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11127 ADD_INSN(ret, node, pushtoarraykwsplat);
11129 else if (static_literal_node_p(body_node, iseq,
false)) {
11130 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11131 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11134 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11135 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11141 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11142 ADD_INSN1(ret, node, splatarray,
Qtrue);
11145 ADD_INSN(ret, node, pop);
11150 ID mid = RNODE_DEFN(node)->nd_mid;
11151 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11153 ISEQ_TYPE_METHOD, line);
11155 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11156 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11160 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11166 ID mid = RNODE_DEFS(node)->nd_mid;
11167 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11169 ISEQ_TYPE_METHOD, line);
11171 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11172 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11173 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11177 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11182 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11183 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11184 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11185 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11186 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11189 ADD_INSN(ret, node, pop);
11194 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11195 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11196 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11197 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11200 ADD_INSN(ret, node, pop);
11207 for (
long i = 0; i < ary->len; i++) {
11208 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11209 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11210 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11211 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11213 if (i < ary->
len - 1) {
11214 ADD_INSN(ret, node, pop);
11219 ADD_INSN(ret, node, pop);
11224 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11226 ISEQ_TYPE_CLASS, line);
11227 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11228 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11229 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11231 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11232 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11236 ADD_INSN(ret, node, pop);
11241 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11243 ISEQ_TYPE_CLASS, line);
11244 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11245 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11247 ADD_INSN (ret, node, putnil);
11248 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11252 ADD_INSN(ret, node, pop);
11258 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11259 ISEQ_TYPE_CLASS, line);
11261 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11262 ADD_INSN (ret, node, putnil);
11263 CONST_ID(singletonclass,
"singletonclass");
11264 ADD_INSN3(ret, node, defineclass,
11265 ID2SYM(singletonclass), singleton_class,
11266 INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
11270 ADD_INSN(ret, node, pop);
11275 CHECK(compile_colon2(iseq, ret, node, popped));
11278 CHECK(compile_colon3(iseq, ret, node, popped));
11281 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11284 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11288 LABEL *lend = NEW_LABEL(line);
11289 LABEL *ltrue = NEW_LABEL(line);
11290 LABEL *lfalse = NEW_LABEL(line);
11291 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11293 ADD_LABEL(ret, ltrue);
11294 ADD_INSN1(ret, node, putobject,
Qtrue);
11295 ADD_INSNL(ret, node, jump, lend);
11296 ADD_LABEL(ret, lfalse);
11297 ADD_INSN1(ret, node, putobject,
Qfalse);
11298 ADD_LABEL(ret, lend);
11303 ADD_INSN(ret, node, putself);
11309 ADD_INSN(ret, node, putnil);
11315 ADD_INSN1(ret, node, putobject,
Qtrue);
11321 ADD_INSN1(ret, node, putobject,
Qfalse);
11326 CHECK(compile_errinfo(iseq, ret, node, popped));
11330 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11333 case NODE_POSTEXE:{
11337 int is_index = body->ise_size++;
11339 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11341 new_child_iseq_with_callback(iseq, ifunc,
11342 rb_fstring(make_name_for_block(iseq)), iseq, ISEQ_TYPE_BLOCK, line);
11344 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
11348 ADD_INSN(ret, node, pop);
11353 CHECK(compile_kw_arg(iseq, ret, node, popped));
11356 compile_dstr(iseq, ret, node);
11358 ADD_INSN(ret, node, intern);
11361 ADD_INSN(ret, node, pop);
11365 case NODE_ATTRASGN:
11366 CHECK(compile_attrasgn(iseq, ret, node, popped));
11370 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11373 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11374 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11378 ADD_INSN(ret, node, pop);
11383 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
11398 insn_data_length(
INSN *iobj)
11400 return insn_len(iobj->insn_id);
11404 calc_sp_depth(
int depth,
INSN *insn)
11406 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
11410 opobj_inspect(
VALUE obj)
11430 insn_data_to_s_detail(
INSN *iobj)
11434 if (iobj->operands) {
11435 const char *types = insn_op_types(iobj->insn_id);
11438 for (j = 0; types[j]; j++) {
11439 char type = types[j];
11463 VALUE v = OPERAND_AT(iobj, j);
11499 void *func = (
void *)OPERAND_AT(iobj, j);
11502 if (dladdr(func, &info) && info.dli_sname) {
11517 if (types[j + 1]) {
11528 dump_disasm_list_with_cursor(link, NULL, NULL);
11539 printf(
"-- raw disasm--------\n");
11542 if (curr) printf(curr == link ?
"*" :
" ");
11543 switch (link->type) {
11544 case ISEQ_ELEMENT_INSN:
11546 iobj = (
INSN *)link;
11547 str = insn_data_to_s_detail(iobj);
11548 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
11549 pos += insn_data_length(iobj);
11552 case ISEQ_ELEMENT_LABEL:
11554 lobj = (
LABEL *)link;
11555 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
11556 dest == lobj ?
" <---" :
"");
11559 case ISEQ_ELEMENT_TRACE:
11562 printf(
" trace: %0x\n", trace->event);
11565 case ISEQ_ELEMENT_ADJUST:
11568 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
11577 printf(
"---------------------\n");
11582 rb_insn_len(
VALUE insn)
11584 return insn_len(insn);
11588 rb_insns_name(
int i)
11590 return insn_name(i);
11594 rb_insns_name_array(
void)
11598 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
11609 obj = rb_to_symbol_type(obj);
11611 if (st_lookup(labels_table, obj, &tmp) == 0) {
11612 label = NEW_LABEL(0);
11613 st_insert(labels_table, obj, (st_data_t)label);
11616 label = (
LABEL *)tmp;
11623 get_exception_sym2type(
VALUE sym)
11625 static VALUE symRescue, symEnsure, symRetry;
11626 static VALUE symBreak, symRedo, symNext;
11628 if (symRescue == 0) {
11637 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
11638 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
11639 if (sym == symRetry)
return CATCH_TYPE_RETRY;
11640 if (sym == symBreak)
return CATCH_TYPE_BREAK;
11641 if (sym == symRedo)
return CATCH_TYPE_REDO;
11642 if (sym == symNext)
return CATCH_TYPE_NEXT;
11656 LABEL *lstart, *lend, *lcont;
11671 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
11672 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
11673 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
11677 if (
type == CATCH_TYPE_RESCUE ||
11678 type == CATCH_TYPE_BREAK ||
11679 type == CATCH_TYPE_NEXT) {
11685 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
11693 insn_make_insn_table(
void)
11697 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
11699 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
11713 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
11715 else if (
CLASS_OF(op) == rb_cISeq) {
11722 loaded_iseq = rb_iseqw_to_iseq(iseqw);
11723 return loaded_iseq;
11731 unsigned int flag = 0;
11742 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
11744 if (!
NIL_P(vkw_arg)) {
11747 size_t n = rb_callinfo_kwarg_bytes(
len);
11750 kw_arg->references = 0;
11751 kw_arg->keyword_len =
len;
11752 for (i = 0; i <
len; i++) {
11755 kw_arg->keywords[i] = kw;
11760 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
11766 event_name_to_flag(
VALUE sym)
11768 #define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
11789 int line_no = 0, node_id = -1, insn_idx = 0;
11790 int ret = COMPILE_OK;
11795 static struct st_table *insn_table;
11797 if (insn_table == 0) {
11798 insn_table = insn_make_insn_table();
11801 for (i=0; i<
len; i++) {
11807 ADD_TRACE(anchor, event);
11810 LABEL *label = register_label(iseq, labels_table, obj);
11811 ADD_LABEL(anchor, label);
11828 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
11830 COMPILE_ERROR(iseq, line_no,
11831 "unknown instruction: %+"PRIsVALUE, insn);
11836 if (argc != insn_len((
VALUE)insn_id)-1) {
11837 COMPILE_ERROR(iseq, line_no,
11838 "operand size mismatch");
11844 argv = compile_data_calloc2(iseq,
sizeof(
VALUE), argc);
11849 (
enum ruby_vminsn_type)insn_id, argc, argv));
11851 for (j=0; j<argc; j++) {
11853 switch (insn_op_type((
VALUE)insn_id, j)) {
11855 LABEL *label = register_label(iseq, labels_table, op);
11856 argv[j] = (
VALUE)label;
11871 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
11882 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
11883 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
11889 op = rb_to_array_type(op);
11893 sym = rb_to_symbol_type(sym);
11898 argv[j] = segments;
11900 ISEQ_BODY(iseq)->ic_size++;
11905 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
11906 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
11911 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
11912 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
11916 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
11919 argv[j] = rb_to_symbol_type(op);
11926 RHASH_TBL_RAW(map)->type = &cdhash_type;
11927 op = rb_to_array_type(op);
11932 register_label(iseq, labels_table, sym);
11942 #if SIZEOF_VALUE <= SIZEOF_LONG
11947 argv[j] = (
VALUE)funcptr;
11958 (
enum ruby_vminsn_type)insn_id, argc, NULL));
11967 validate_labels(iseq, labels_table);
11968 if (!ret)
return ret;
11969 return iseq_setup(iseq, anchor);
11972 #define CHECK_ARRAY(v) rb_to_array_type(v)
11973 #define CHECK_SYMBOL(v) rb_to_symbol_type(v)
11983 else if (!
NIL_P(val)) {
11990 static const struct rb_iseq_param_keyword *
11996 VALUE key, sym, default_val;
11999 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12001 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12003 keyword->num =
len;
12004 #define SYM(s) ID2SYM(rb_intern_const(#s))
12005 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12006 i = keyword->bits_start - keyword->num;
12007 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12011 for (i = 0; i <
len; i++) {
12015 goto default_values;
12018 keyword->required_num++;
12022 default_len =
len - i;
12023 if (default_len == 0) {
12024 keyword->table = ids;
12027 else if (default_len < 0) {
12033 for (j = 0; i <
len; i++, j++) {
12053 keyword->table = ids;
12054 keyword->default_values = dvs;
12060 iseq_insn_each_object_mark_and_pin(
VALUE obj,
VALUE _)
12069 size_t size =
sizeof(
INSN);
12070 unsigned int pos = 0;
12073 #ifdef STRICT_ALIGNMENT
12074 size_t padding = calc_padding((
void *)&storage->buff[pos], size);
12076 const size_t padding = 0;
12078 size_t offset = pos + size + padding;
12079 if (offset > storage->size || offset > storage->pos) {
12081 storage = storage->next;
12084 #ifdef STRICT_ALIGNMENT
12085 pos += (int)padding;
12088 iobj = (
INSN *)&storage->buff[pos];
12090 if (iobj->operands) {
12091 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_pin, (
VALUE)0);
12104 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
12111 #define SYM(s) ID2SYM(rb_intern_const(#s))
12113 unsigned int arg_size, local_size, stack_max;
12115 struct st_table *labels_table = st_init_numtable();
12120 DECL_ANCHOR(anchor);
12121 INIT_ANCHOR(anchor);
12124 ISEQ_BODY(iseq)->local_table_size =
len;
12125 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12127 for (i = 0; i <
len; i++) {
12130 if (sym_arg_rest == lv) {
12138 #define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12139 if (INT_PARAM(lead_num)) {
12140 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12142 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12143 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12144 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12145 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12148 #define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12150 INT_PARAM(arg_size);
12151 INT_PARAM(local_size);
12152 INT_PARAM(stack_max);
12157 #ifdef USE_ISEQ_NODE_ID
12166 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12168 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12171 for (i = 0; i <
len; i++) {
12173 LABEL *label = register_label(iseq, labels_table, ent);
12174 opt_table[i] = (
VALUE)label;
12177 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12178 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12181 else if (!
NIL_P(arg_opt_labels)) {
12187 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12189 else if (!
NIL_P(keywords)) {
12195 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12199 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12202 if (int_param(&i, params, SYM(kwrest))) {
12203 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12204 if (keyword == NULL) {
12205 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12207 keyword->rest_start = i;
12208 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12211 iseq_calc_param_size(iseq);
12214 iseq_build_from_ary_exception(iseq, labels_table, exception);
12217 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12219 ISEQ_BODY(iseq)->param.size = arg_size;
12220 ISEQ_BODY(iseq)->local_table_size = local_size;
12221 ISEQ_BODY(iseq)->stack_max = stack_max;
12231 while (body->type == ISEQ_TYPE_BLOCK ||
12232 body->type == ISEQ_TYPE_RESCUE ||
12233 body->type == ISEQ_TYPE_ENSURE ||
12234 body->type == ISEQ_TYPE_EVAL ||
12235 body->type == ISEQ_TYPE_MAIN
12239 for (i = 0; i < body->local_table_size; i++) {
12240 if (body->local_table[i] ==
id) {
12244 iseq = body->parent_iseq;
12245 body = ISEQ_BODY(iseq);
12258 for (i=0; i<body->local_table_size; i++) {
12259 if (body->local_table[i] ==
id) {
12269 #ifndef IBF_ISEQ_DEBUG
12270 #define IBF_ISEQ_DEBUG 0
12273 #ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12274 #define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12277 typedef uint32_t ibf_offset_t;
12278 #define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12280 #define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12282 #define IBF_DEVEL_VERSION 4
12283 #define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12285 #define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12288 static const char IBF_ENDIAN_MARK =
12289 #ifdef WORDS_BIGENDIAN
12298 uint32_t major_version;
12299 uint32_t minor_version;
12301 uint32_t extra_size;
12303 uint32_t iseq_list_size;
12304 uint32_t global_object_list_size;
12305 ibf_offset_t iseq_list_offset;
12306 ibf_offset_t global_object_list_offset;
12327 unsigned int obj_list_size;
12328 ibf_offset_t obj_list_offset;
12347 pinned_list_mark(
void *
ptr)
12351 for (i = 0; i < list->size; i++) {
12352 if (list->buffer[i]) {
12365 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
12369 pinned_list_fetch(
VALUE list,
long offset)
12375 if (offset >=
ptr->size) {
12379 return ptr->buffer[offset];
12383 pinned_list_store(
VALUE list,
long offset,
VALUE object)
12389 if (offset >=
ptr->size) {
12397 pinned_list_new(
long size)
12399 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
12406 static ibf_offset_t
12407 ibf_dump_pos(
struct ibf_dump *dump)
12409 long pos =
RSTRING_LEN(dump->current_buffer->str);
12410 #if SIZEOF_LONG > SIZEOF_INT
12411 if (pos >= UINT_MAX) {
12415 return (
unsigned int)pos;
12419 ibf_dump_align(
struct ibf_dump *dump,
size_t align)
12421 ibf_offset_t pos = ibf_dump_pos(dump);
12423 static const char padding[
sizeof(
VALUE)];
12424 size_t size = align - ((size_t)pos % align);
12425 #if SIZEOF_LONG > SIZEOF_INT
12426 if (pos + size >= UINT_MAX) {
12430 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
12431 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
12433 rb_str_cat(dump->current_buffer->str, padding, size);
12437 static ibf_offset_t
12438 ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
12440 ibf_offset_t pos = ibf_dump_pos(dump);
12441 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
12446 static ibf_offset_t
12447 ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
12449 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
12453 ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
12455 VALUE str = dump->current_buffer->str;
12457 if ((
unsigned long)(size + offset) > (
unsigned long)
RSTRING_LEN(str))
12458 rb_bug(
"ibf_dump_overwrite: overflow");
12459 memcpy(
ptr + offset, buff, size);
12462 static const void *
12463 ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
12465 ibf_offset_t beg = *offset;
12467 return load->current_buffer->buff + beg;
12471 ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
12474 size_t size = x * y;
12475 memcpy(buff, load->current_buffer->buff + offset, size);
12479 #define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
12481 #define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
12482 #define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
12483 #define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
12484 #define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
12485 #define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
12488 ibf_table_lookup(
struct st_table *table, st_data_t key)
12492 if (st_lookup(table, key, &val)) {
12501 ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
12503 int index = ibf_table_lookup(table, key);
12506 index = (int)table->num_entries;
12507 st_insert(table, key, (st_data_t)index);
12515 static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
12521 ibf_dump_object_table_new(
void)
12523 st_table *obj_table = st_init_numtable();
12524 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
12532 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
12541 return ibf_dump_object(dump,
rb_id2sym(
id));
12545 ibf_load_id(
const struct ibf_load *load,
const ID id_index)
12547 if (id_index == 0) {
12550 VALUE sym = ibf_load_object(load, id_index);
12560 static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
12565 if (iseq == NULL) {
12569 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
12573 static unsigned char
12574 ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
12577 return (
unsigned char)load->current_buffer->buff[(*offset)++];
12593 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12594 ibf_dump_write(dump, &x,
sizeof(
VALUE));
12598 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12600 unsigned char bytes[max_byte_length];
12603 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
12604 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12610 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12613 ibf_dump_write(dump, bytes + max_byte_length - n, n);
12617 ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
12619 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12620 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
12622 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
12623 *offset +=
sizeof(
VALUE);
12628 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12630 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
12631 const unsigned char c = buffer[*offset];
12635 c == 0 ? 9 : ntz_int32(c) + 1;
12638 if (*offset + n > load->current_buffer->size) {
12643 for (i = 1; i < n; i++) {
12645 x |= (
VALUE)buffer[*offset + i];
12659 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
12661 size_t len = strlen(bf->name);
12662 ibf_dump_write_small_value(dump, (
VALUE)
len);
12663 ibf_dump_write(dump, bf->name,
len);
12667 ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
12669 int i = (int)ibf_load_small_value(load, offset);
12670 int len = (int)ibf_load_small_value(load, offset);
12671 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
12674 fprintf(stderr,
"%.*s!!\n",
len, name);
12679 if (strncmp(table[i].name, name,
len) != 0) {
12680 rb_raise(
rb_eArgError,
"builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
12687 static ibf_offset_t
12691 const int iseq_size = body->iseq_size;
12693 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
12695 ibf_offset_t offset = ibf_dump_pos(dump);
12697 for (code_index=0; code_index<iseq_size;) {
12698 const VALUE insn = orig_code[code_index++];
12699 const char *types = insn_op_types(insn);
12704 ibf_dump_write_small_value(dump, insn);
12707 for (op_index=0; types[op_index]; op_index++, code_index++) {
12708 VALUE op = orig_code[code_index];
12711 switch (types[op_index]) {
12714 wv = ibf_dump_object(dump, op);
12723 wv = ibf_dump_object(dump, arr);
12731 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
12739 wv = ibf_dump_id(dump, (
ID)op);
12751 ibf_dump_write_small_value(dump, wv);
12761 ibf_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)
12764 unsigned int code_index;
12765 ibf_offset_t reading_pos = bytecode_offset;
12769 struct rb_call_data *cd_entries = load_body->call_data;
12772 iseq_bits_t * mark_offset_bits;
12774 iseq_bits_t tmp[1] = {0};
12776 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
12777 mark_offset_bits = tmp;
12780 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
12782 bool needs_bitmap =
false;
12784 for (code_index=0; code_index<iseq_size;) {
12786 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
12787 const char *types = insn_op_types(insn);
12793 for (op_index=0; types[op_index]; op_index++, code_index++) {
12794 const char operand_type = types[op_index];
12795 switch (operand_type) {
12798 VALUE op = ibf_load_small_value(load, &reading_pos);
12799 VALUE v = ibf_load_object(load, op);
12800 code[code_index] = v;
12803 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12804 needs_bitmap =
true;
12810 VALUE op = ibf_load_small_value(load, &reading_pos);
12811 VALUE v = ibf_load_object(load, op);
12813 RHASH_TBL_RAW(v)->type = &cdhash_type;
12815 freeze_hide_obj(v);
12820 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
12822 code[code_index] = v;
12823 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12825 needs_bitmap =
true;
12830 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
12832 code[code_index] = v;
12835 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12836 needs_bitmap =
true;
12842 VALUE op = ibf_load_small_value(load, &reading_pos);
12843 VALUE arr = ibf_load_object(load, op);
12845 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
12846 ic->
segments = array_to_idlist(arr);
12848 code[code_index] = (
VALUE)ic;
12855 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12857 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
12858 code[code_index] = (
VALUE)ic;
12860 if (operand_type == TS_IVC) {
12863 if (insn == BIN(setinstancevariable)) {
12864 ID iv_name = (
ID)code[code_index - 1];
12865 cache->iv_set_name = iv_name;
12868 cache->iv_set_name = 0;
12871 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
12878 code[code_index] = (
VALUE)cd_entries++;
12883 VALUE op = ibf_load_small_value(load, &reading_pos);
12884 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
12891 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
12894 code[code_index] = ibf_load_small_value(load, &reading_pos);
12898 if (insn_len(insn) != op_index+1) {
12903 load_body->iseq_encoded = code;
12904 load_body->iseq_size = code_index;
12906 if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
12907 load_body->mark_bits.single = mark_offset_bits[0];
12910 if (needs_bitmap) {
12911 load_body->mark_bits.list = mark_offset_bits;
12914 load_body->mark_bits.list = 0;
12920 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
12924 static ibf_offset_t
12927 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
12930 IBF_W_ALIGN(
VALUE);
12931 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
12934 return ibf_dump_pos(dump);
12939 ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
12943 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
12951 static ibf_offset_t
12954 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
12957 struct rb_iseq_param_keyword dump_kw = *kw;
12958 int dv_num = kw->num - kw->required_num;
12963 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
12964 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
12966 dump_kw.table = IBF_W(ids,
ID, kw->num);
12967 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
12968 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
12969 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
12976 static const struct rb_iseq_param_keyword *
12977 ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
12979 if (param_keyword_offset) {
12980 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
12981 int dv_num = kw->num - kw->required_num;
12982 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
12985 for (i=0; i<dv_num; i++) {
12986 dvs[i] = ibf_load_object(load, dvs[i]);
12992 kw->default_values = dvs;
13000 static ibf_offset_t
13003 ibf_offset_t offset = ibf_dump_pos(dump);
13007 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
13008 ibf_dump_write_small_value(dump, entries[i].line_no);
13009 #ifdef USE_ISEQ_NODE_ID
13010 ibf_dump_write_small_value(dump, entries[i].node_id);
13012 ibf_dump_write_small_value(dump, entries[i].events);
13019 ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
13021 ibf_offset_t reading_pos = body_offset;
13025 for (i = 0; i < size; i++) {
13026 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
13027 #ifdef USE_ISEQ_NODE_ID
13028 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
13030 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
13036 static ibf_offset_t
13037 ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
13039 ibf_offset_t offset = ibf_dump_pos(dump);
13041 unsigned int last = 0;
13043 for (i = 0; i < size; i++) {
13044 ibf_dump_write_small_value(dump, positions[i] - last);
13045 last = positions[i];
13051 static unsigned int *
13052 ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
13054 ibf_offset_t reading_pos = positions_offset;
13055 unsigned int *positions =
ALLOC_N(
unsigned int, size);
13057 unsigned int last = 0;
13059 for (i = 0; i < size; i++) {
13060 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
13061 last = positions[i];
13067 static ibf_offset_t
13071 const int size = body->local_table_size;
13075 for (i=0; i<size; i++) {
13076 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13079 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13085 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
13089 ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13092 ID *table = IBF_R(local_table_offset,
ID, size);
13095 for (i=0; i<size; i++) {
13096 table[i] = ibf_load_id(load, table[i]);
13105 static ibf_offset_t
13111 int *iseq_indices =
ALLOCA_N(
int, table->size);
13114 for (i=0; i<table->size; i++) {
13115 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13118 const ibf_offset_t offset = ibf_dump_pos(dump);
13120 for (i=0; i<table->size; i++) {
13121 ibf_dump_write_small_value(dump, iseq_indices[i]);
13122 ibf_dump_write_small_value(dump, table->entries[i].type);
13123 ibf_dump_write_small_value(dump, table->entries[i].start);
13124 ibf_dump_write_small_value(dump, table->entries[i].end);
13125 ibf_dump_write_small_value(dump, table->entries[i].cont);
13126 ibf_dump_write_small_value(dump, table->entries[i].sp);
13131 return ibf_dump_pos(dump);
13136 ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size)
13140 table->size = size;
13142 ibf_offset_t reading_pos = catch_table_offset;
13145 for (i=0; i<table->size; i++) {
13146 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13147 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13148 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13149 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13150 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13151 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13153 table->entries[i].iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)iseq_index);
13162 static ibf_offset_t
13166 const unsigned int ci_size = body->ci_size;
13169 ibf_offset_t offset = ibf_dump_pos(dump);
13173 for (i = 0; i < ci_size; i++) {
13176 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13177 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13178 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13182 int len = kwarg->keyword_len;
13183 ibf_dump_write_small_value(dump,
len);
13184 for (
int j=0; j<
len; j++) {
13185 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13186 ibf_dump_write_small_value(dump, keyword);
13190 ibf_dump_write_small_value(dump, 0);
13195 ibf_dump_write_small_value(dump, (
VALUE)-1);
13213 static enum rb_id_table_iterator_result
13214 store_outer_variable(
ID id,
VALUE val,
void *dump)
13221 return ID_TABLE_CONTINUE;
13225 outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13232 static ibf_offset_t
13235 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13237 ibf_offset_t offset = ibf_dump_pos(dump);
13239 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13240 ibf_dump_write_small_value(dump, (
VALUE)size);
13249 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13251 for (
size_t i = 0; i < size; ++i) {
13252 ID id = ovlist->pairs[i].id;
13253 ID val = ovlist->pairs[i].val;
13254 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13255 ibf_dump_write_small_value(dump, val);
13264 ibf_load_ci_entries(
const struct ibf_load *load,
13265 ibf_offset_t ci_entries_offset,
13266 unsigned int ci_size,
13269 ibf_offset_t reading_pos = ci_entries_offset;
13276 for (i = 0; i < ci_size; i++) {
13277 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
13278 if (mid_index != (
VALUE)-1) {
13279 ID mid = ibf_load_id(load, mid_index);
13280 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13281 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13284 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
13287 kwarg->references = 0;
13288 kwarg->keyword_len = kwlen;
13289 for (
int j=0; j<kwlen; j++) {
13290 VALUE keyword = ibf_load_small_value(load, &reading_pos);
13291 kwarg->keywords[j] = ibf_load_object(load, keyword);
13295 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
13297 cds[i].cc = vm_cc_empty();
13308 ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
13310 ibf_offset_t reading_pos = outer_variables_offset;
13314 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
13316 if (table_size > 0) {
13317 tbl = rb_id_table_create(table_size);
13320 for (
size_t i = 0; i < table_size; i++) {
13321 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
13322 VALUE value = ibf_load_small_value(load, &reading_pos);
13323 if (!key) key = rb_make_temporary_id(i);
13324 rb_id_table_insert(tbl, key, value);
13330 static ibf_offset_t
13333 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
13335 unsigned int *positions;
13339 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
13340 const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
13341 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
13343 #if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13344 ibf_offset_t iseq_start = ibf_dump_pos(dump);
13349 buffer.obj_table = ibf_dump_object_table_new();
13350 dump->current_buffer = &buffer;
13353 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
13354 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
13355 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
13356 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
13357 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
13359 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
13360 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
13363 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
13364 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
13365 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
13366 const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
13367 const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
13368 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
13369 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
13370 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
13372 #if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13373 ibf_offset_t local_obj_list_offset;
13374 unsigned int local_obj_list_size;
13376 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
13379 ibf_offset_t body_offset = ibf_dump_pos(dump);
13382 unsigned int param_flags =
13383 (body->
param.flags.has_lead << 0) |
13384 (body->
param.flags.has_opt << 1) |
13385 (body->
param.flags.has_rest << 2) |
13386 (body->
param.flags.has_post << 3) |
13387 (body->
param.flags.has_kw << 4) |
13388 (body->
param.flags.has_kwrest << 5) |
13389 (body->
param.flags.has_block << 6) |
13390 (body->
param.flags.ambiguous_param0 << 7) |
13391 (body->
param.flags.accepts_no_kwarg << 8) |
13392 (body->
param.flags.ruby2_keywords << 9) |
13393 (body->
param.flags.anon_rest << 10) |
13394 (body->
param.flags.anon_kwrest << 11) |
13395 (body->
param.flags.use_block << 12) |
13396 (body->
param.flags.forwardable << 13) ;
13398 #if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13399 # define IBF_BODY_OFFSET(x) (x)
13401 # define IBF_BODY_OFFSET(x) (body_offset - (x))
13404 ibf_dump_write_small_value(dump, body->type);
13405 ibf_dump_write_small_value(dump, body->iseq_size);
13406 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
13407 ibf_dump_write_small_value(dump, bytecode_size);
13408 ibf_dump_write_small_value(dump, param_flags);
13409 ibf_dump_write_small_value(dump, body->
param.size);
13410 ibf_dump_write_small_value(dump, body->
param.lead_num);
13411 ibf_dump_write_small_value(dump, body->
param.opt_num);
13412 ibf_dump_write_small_value(dump, body->
param.rest_start);
13413 ibf_dump_write_small_value(dump, body->
param.post_start);
13414 ibf_dump_write_small_value(dump, body->
param.post_num);
13415 ibf_dump_write_small_value(dump, body->
param.block_start);
13416 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
13417 ibf_dump_write_small_value(dump, param_keyword_offset);
13418 ibf_dump_write_small_value(dump, location_pathobj_index);
13419 ibf_dump_write_small_value(dump, location_base_label_index);
13420 ibf_dump_write_small_value(dump, location_label_index);
13421 ibf_dump_write_small_value(dump, body->location.first_lineno);
13422 ibf_dump_write_small_value(dump, body->location.node_id);
13423 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
13424 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
13425 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
13426 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
13427 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
13428 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
13429 ibf_dump_write_small_value(dump, body->insns_info.size);
13430 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
13431 ibf_dump_write_small_value(dump, catch_table_size);
13432 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
13433 ibf_dump_write_small_value(dump, parent_iseq_index);
13434 ibf_dump_write_small_value(dump, local_iseq_index);
13435 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
13436 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
13437 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
13438 ibf_dump_write_small_value(dump, body->variable.flip_count);
13439 ibf_dump_write_small_value(dump, body->local_table_size);
13440 ibf_dump_write_small_value(dump, body->ivc_size);
13441 ibf_dump_write_small_value(dump, body->icvarc_size);
13442 ibf_dump_write_small_value(dump, body->ise_size);
13443 ibf_dump_write_small_value(dump, body->ic_size);
13444 ibf_dump_write_small_value(dump, body->ci_size);
13445 ibf_dump_write_small_value(dump, body->stack_max);
13446 ibf_dump_write_small_value(dump, body->builtin_attrs);
13447 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
13449 #undef IBF_BODY_OFFSET
13451 #if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13452 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
13454 dump->current_buffer = saved_buffer;
13455 ibf_dump_write(dump,
RSTRING_PTR(buffer.str), iseq_length_bytes);
13457 ibf_offset_t offset = ibf_dump_pos(dump);
13458 ibf_dump_write_small_value(dump, iseq_start);
13459 ibf_dump_write_small_value(dump, iseq_length_bytes);
13460 ibf_dump_write_small_value(dump, body_offset);
13462 ibf_dump_write_small_value(dump, local_obj_list_offset);
13463 ibf_dump_write_small_value(dump, local_obj_list_size);
13465 st_free_table(buffer.obj_table);
13469 return body_offset;
13474 ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
13476 VALUE str = ibf_load_object(load, str_index);
13478 str = rb_fstring(str);
13484 ibf_load_iseq_each(
struct ibf_load *load,
rb_iseq_t *iseq, ibf_offset_t offset)
13488 ibf_offset_t reading_pos = offset;
13490 #if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13492 load->current_buffer = &load->global_buffer;
13494 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13495 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13496 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13499 buffer.buff = load->global_buffer.buff + iseq_start;
13500 buffer.size = iseq_length_bytes;
13501 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13502 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13503 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
13505 load->current_buffer = &buffer;
13506 reading_pos = body_offset;
13509 #if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13510 # define IBF_BODY_OFFSET(x) (x)
13512 # define IBF_BODY_OFFSET(x) (offset - (x))
13515 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13516 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13517 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13518 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13519 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13520 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13521 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
13522 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
13523 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
13524 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
13525 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
13526 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
13527 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13528 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13529 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
13530 const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
13531 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
13532 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
13533 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
13534 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13535 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13536 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13537 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13538 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13539 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13540 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13541 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13542 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13543 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13544 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13545 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13546 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13547 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13548 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13549 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
13550 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13552 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13553 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13554 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13555 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13557 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13558 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13559 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13560 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
13563 VALUE path = ibf_load_object(load, location_pathobj_index);
13568 realpath = path = rb_fstring(path);
13571 VALUE pathobj = path;
13577 if (!
NIL_P(realpath)) {
13580 "(%x), path=%+"PRIsVALUE,
13581 realpath,
TYPE(realpath), path);
13583 realpath = rb_fstring(realpath);
13589 rb_iseq_pathobj_set(iseq, path, realpath);
13594 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
13596 #undef IBF_BODY_OFFSET
13598 load_body->type =
type;
13599 load_body->stack_max = stack_max;
13600 load_body->
param.flags.has_lead = (param_flags >> 0) & 1;
13601 load_body->
param.flags.has_opt = (param_flags >> 1) & 1;
13602 load_body->
param.flags.has_rest = (param_flags >> 2) & 1;
13603 load_body->
param.flags.has_post = (param_flags >> 3) & 1;
13604 load_body->
param.flags.has_kw = FALSE;
13605 load_body->
param.flags.has_kwrest = (param_flags >> 5) & 1;
13606 load_body->
param.flags.has_block = (param_flags >> 6) & 1;
13607 load_body->
param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
13608 load_body->
param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
13609 load_body->
param.flags.ruby2_keywords = (param_flags >> 9) & 1;
13610 load_body->
param.flags.anon_rest = (param_flags >> 10) & 1;
13611 load_body->
param.flags.anon_kwrest = (param_flags >> 11) & 1;
13612 load_body->
param.flags.use_block = (param_flags >> 12) & 1;
13613 load_body->
param.flags.forwardable = (param_flags >> 13) & 1;
13614 load_body->
param.size = param_size;
13615 load_body->
param.lead_num = param_lead_num;
13616 load_body->
param.opt_num = param_opt_num;
13617 load_body->
param.rest_start = param_rest_start;
13618 load_body->
param.post_start = param_post_start;
13619 load_body->
param.post_num = param_post_num;
13620 load_body->
param.block_start = param_block_start;
13621 load_body->local_table_size = local_table_size;
13622 load_body->ci_size = ci_size;
13623 load_body->insns_info.size = insns_info_size;
13625 ISEQ_COVERAGE_SET(iseq,
Qnil);
13626 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
13627 load_body->variable.flip_count = variable_flip_count;
13628 load_body->variable.script_lines =
Qnil;
13630 load_body->location.first_lineno = location_first_lineno;
13631 load_body->location.node_id = location_node_id;
13632 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
13633 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
13634 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
13635 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
13636 load_body->builtin_attrs = builtin_attrs;
13637 load_body->prism = prism;
13639 load_body->ivc_size = ivc_size;
13640 load_body->icvarc_size = icvarc_size;
13641 load_body->ise_size = ise_size;
13642 load_body->ic_size = ic_size;
13644 if (ISEQ_IS_SIZE(load_body)) {
13648 load_body->is_entries = NULL;
13650 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
13651 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
13652 load_body->
param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
13653 load_body->
param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
13654 load_body->
param.flags.has_kw = (param_flags >> 4) & 1;
13655 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
13656 load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
13657 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
13658 load_body->catch_table = ibf_load_catch_table(load, catch_table_offset, catch_table_size);
13659 load_body->parent_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)parent_iseq_index);
13660 load_body->local_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)local_iseq_index);
13661 load_body->mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
13664 if (load_body->
param.keyword != NULL) {
13666 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->
param.keyword;
13667 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
13670 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
13671 #if VM_INSN_INFO_TABLE_IMPL == 2
13672 rb_iseq_insns_info_encode_positions(iseq);
13675 rb_iseq_translate_threaded_code(iseq);
13677 #if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13678 load->current_buffer = &load->global_buffer;
13681 RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
13682 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
13684 #if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13685 load->current_buffer = saved_buffer;
13687 verify_call_cache(iseq);
13690 rb_vm_pop_frame_no_int(ec);
13700 ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t
ptr)
13705 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
13708 return ST_CONTINUE;
13718 args.offset_list = offset_list;
13720 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
13723 st_index_t size = dump->iseq_table->num_entries;
13724 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
13726 for (i = 0; i < size; i++) {
13730 ibf_dump_align(dump,
sizeof(ibf_offset_t));
13731 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
13732 header->iseq_list_size = (
unsigned int)size;
13735 #define IBF_OBJECT_INTERNAL FL_PROMOTED0
13744 unsigned int type: 5;
13745 unsigned int special_const: 1;
13746 unsigned int frozen: 1;
13747 unsigned int internal: 1;
13750 enum ibf_object_class_index {
13751 IBF_OBJECT_CLASS_OBJECT,
13752 IBF_OBJECT_CLASS_ARRAY,
13753 IBF_OBJECT_CLASS_STANDARD_ERROR,
13754 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
13755 IBF_OBJECT_CLASS_TYPE_ERROR,
13756 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
13766 long keyval[FLEX_ARY_LEN];
13779 BDIGIT digits[FLEX_ARY_LEN];
13782 enum ibf_object_data_type {
13783 IBF_OBJECT_DATA_ENCODING,
13794 #define IBF_ALIGNED_OFFSET(align, offset) \
13795 ((((offset) - 1) / (align) + 1) * (align))
13796 #define IBF_OBJBODY(type, offset) (const type *)\
13797 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
13799 static const void *
13800 ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
13802 if (offset >= load->current_buffer->size) {
13805 return load->current_buffer->buff + offset;
13808 NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
13811 ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
13814 rb_raw_obj_info(buff,
sizeof(buff), obj);
13830 enum ibf_object_class_index cindex;
13831 if (obj == rb_cObject) {
13832 cindex = IBF_OBJECT_CLASS_OBJECT;
13835 cindex = IBF_OBJECT_CLASS_ARRAY;
13838 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
13841 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
13844 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
13847 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
13850 rb_obj_info_dump(obj);
13852 rb_bug(
"unsupported class");
13854 ibf_dump_write_small_value(dump, (
VALUE)cindex);
13860 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
13863 case IBF_OBJECT_CLASS_OBJECT:
13865 case IBF_OBJECT_CLASS_ARRAY:
13867 case IBF_OBJECT_CLASS_STANDARD_ERROR:
13869 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
13871 case IBF_OBJECT_CLASS_TYPE_ERROR:
13873 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
13885 (void)IBF_W(&dbl,
double, 1);
13891 const double *dblp = IBF_OBJBODY(
double, offset);
13902 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
13905 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
13908 ibf_dump_write_small_value(dump, encindex);
13909 ibf_dump_write_small_value(dump,
len);
13916 ibf_offset_t reading_pos = offset;
13918 int encindex = (int)ibf_load_small_value(load, &reading_pos);
13919 const long len = (long)ibf_load_small_value(load, &reading_pos);
13920 const char *
ptr = load->current_buffer->buff + reading_pos;
13922 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
13923 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
13928 if (header->frozen && !header->internal) {
13935 if (header->frozen) str = rb_fstring(str);
13946 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
13948 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
13949 ibf_dump_write_small_value(dump, regexp.srcstr);
13956 regexp.option = ibf_load_byte(load, &offset);
13957 regexp.srcstr = ibf_load_small_value(load, &offset);
13959 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
13960 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
13972 ibf_dump_write_small_value(dump,
len);
13973 for (i=0; i<
len; i++) {
13974 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
13975 ibf_dump_write_small_value(dump, index);
13982 ibf_offset_t reading_pos = offset;
13984 const long len = (long)ibf_load_small_value(load, &reading_pos);
13989 for (i=0; i<
len; i++) {
13990 const VALUE index = ibf_load_small_value(load, &reading_pos);
14000 ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t
ptr)
14004 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14005 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14007 ibf_dump_write_small_value(dump, key_index);
14008 ibf_dump_write_small_value(dump, val_index);
14009 return ST_CONTINUE;
14016 ibf_dump_write_small_value(dump, (
VALUE)
len);
14024 long len = (long)ibf_load_small_value(load, &offset);
14025 VALUE obj = rb_hash_new_with_size(
len);
14028 for (i = 0; i <
len; i++) {
14029 VALUE key_index = ibf_load_small_value(load, &offset);
14030 VALUE val_index = ibf_load_small_value(load, &offset);
14032 VALUE key = ibf_load_object(load, key_index);
14033 VALUE val = ibf_load_object(load, val_index);
14036 rb_hash_rehash(obj);
14052 range.class_index = 0;
14055 range.beg = (long)ibf_dump_object(dump, beg);
14056 range.end = (long)ibf_dump_object(dump, end);
14071 VALUE beg = ibf_load_object(load, range->beg);
14072 VALUE end = ibf_load_object(load, range->end);
14082 ssize_t
len = BIGNUM_LEN(obj);
14083 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14084 BDIGIT *d = BIGNUM_DIGITS(obj);
14086 (void)IBF_W(&slen, ssize_t, 1);
14087 IBF_WP(d, BDIGIT,
len);
14094 int sign = bignum->slen > 0;
14095 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14096 const int big_unpack_flags =
14110 if (rb_data_is_encoding(obj)) {
14113 long len = strlen(name) + 1;
14115 data[0] = IBF_OBJECT_DATA_ENCODING;
14117 (void)IBF_W(data,
long, 2);
14118 IBF_WP(name,
char,
len);
14121 ibf_dump_object_unsupported(dump, obj);
14128 const long *body = IBF_OBJBODY(
long, offset);
14129 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14131 const char *data = (
const char *)&body[2];
14134 case IBF_OBJECT_DATA_ENCODING:
14141 return ibf_load_object_unsupported(load, header, offset);
14145 ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14148 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14149 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14151 (void)IBF_W(data,
long, 2);
14155 ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14158 VALUE a = ibf_load_object(load, nums->a);
14159 VALUE b = ibf_load_object(load, nums->b);
14171 ibf_dump_object_string(dump,
rb_sym2str(obj));
14177 ibf_offset_t reading_pos = offset;
14179 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14180 const long len = (long)ibf_load_small_value(load, &reading_pos);
14181 const char *
ptr = load->current_buffer->buff + reading_pos;
14183 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14184 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14192 typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
14193 static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
14194 ibf_dump_object_unsupported,
14195 ibf_dump_object_unsupported,
14196 ibf_dump_object_class,
14197 ibf_dump_object_unsupported,
14198 ibf_dump_object_float,
14199 ibf_dump_object_string,
14200 ibf_dump_object_regexp,
14201 ibf_dump_object_array,
14202 ibf_dump_object_hash,
14203 ibf_dump_object_struct,
14204 ibf_dump_object_bignum,
14205 ibf_dump_object_unsupported,
14206 ibf_dump_object_data,
14207 ibf_dump_object_unsupported,
14208 ibf_dump_object_complex_rational,
14209 ibf_dump_object_complex_rational,
14210 ibf_dump_object_unsupported,
14211 ibf_dump_object_unsupported,
14212 ibf_dump_object_unsupported,
14213 ibf_dump_object_unsupported,
14214 ibf_dump_object_symbol,
14215 ibf_dump_object_unsupported,
14216 ibf_dump_object_unsupported,
14217 ibf_dump_object_unsupported,
14218 ibf_dump_object_unsupported,
14219 ibf_dump_object_unsupported,
14220 ibf_dump_object_unsupported,
14221 ibf_dump_object_unsupported,
14222 ibf_dump_object_unsupported,
14223 ibf_dump_object_unsupported,
14224 ibf_dump_object_unsupported,
14225 ibf_dump_object_unsupported,
14231 unsigned char byte =
14232 (header.type << 0) |
14233 (header.special_const << 5) |
14234 (header.frozen << 6) |
14235 (header.internal << 7);
14241 ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
14243 unsigned char byte = ibf_load_byte(load, offset);
14246 header.type = (
byte >> 0) & 0x1f;
14247 header.special_const = (
byte >> 5) & 0x01;
14248 header.frozen = (
byte >> 6) & 0x01;
14249 header.internal = (
byte >> 7) & 0x01;
14254 static ibf_offset_t
14258 ibf_offset_t current_offset;
14259 IBF_ZERO(obj_header);
14260 obj_header.type =
TYPE(obj);
14262 IBF_W_ALIGN(ibf_offset_t);
14263 current_offset = ibf_dump_pos(dump);
14268 obj_header.special_const = TRUE;
14269 obj_header.frozen = TRUE;
14270 obj_header.internal = TRUE;
14271 ibf_dump_object_object_header(dump, obj_header);
14272 ibf_dump_write_small_value(dump, obj);
14276 obj_header.special_const = FALSE;
14278 ibf_dump_object_object_header(dump, obj_header);
14279 (*dump_object_functions[obj_header.type])(dump, obj);
14282 return current_offset;
14286 static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
14287 ibf_load_object_unsupported,
14288 ibf_load_object_unsupported,
14289 ibf_load_object_class,
14290 ibf_load_object_unsupported,
14291 ibf_load_object_float,
14292 ibf_load_object_string,
14293 ibf_load_object_regexp,
14294 ibf_load_object_array,
14295 ibf_load_object_hash,
14296 ibf_load_object_struct,
14297 ibf_load_object_bignum,
14298 ibf_load_object_unsupported,
14299 ibf_load_object_data,
14300 ibf_load_object_unsupported,
14301 ibf_load_object_complex_rational,
14302 ibf_load_object_complex_rational,
14303 ibf_load_object_unsupported,
14304 ibf_load_object_unsupported,
14305 ibf_load_object_unsupported,
14306 ibf_load_object_unsupported,
14307 ibf_load_object_symbol,
14308 ibf_load_object_unsupported,
14309 ibf_load_object_unsupported,
14310 ibf_load_object_unsupported,
14311 ibf_load_object_unsupported,
14312 ibf_load_object_unsupported,
14313 ibf_load_object_unsupported,
14314 ibf_load_object_unsupported,
14315 ibf_load_object_unsupported,
14316 ibf_load_object_unsupported,
14317 ibf_load_object_unsupported,
14318 ibf_load_object_unsupported,
14322 ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
14324 if (object_index == 0) {
14328 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
14330 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
14331 ibf_offset_t offset = offsets[object_index];
14332 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
14335 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
14336 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
14337 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
14338 header.type, header.special_const, header.frozen, header.internal);
14340 if (offset >= load->current_buffer->size) {
14344 if (header.special_const) {
14345 ibf_offset_t reading_pos = offset;
14347 obj = ibf_load_small_value(load, &reading_pos);
14350 obj = (*load_object_functions[header.type])(load, &header, offset);
14353 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
14356 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
14357 object_index, obj);
14370 ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t
ptr)
14375 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
14378 return ST_CONTINUE;
14382 ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
14384 st_table *obj_table = dump->current_buffer->obj_table;
14389 args.offset_list = offset_list;
14391 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
14393 IBF_W_ALIGN(ibf_offset_t);
14394 *obj_list_offset = ibf_dump_pos(dump);
14396 st_index_t size = obj_table->num_entries;
14399 for (i=0; i<size; i++) {
14404 *obj_list_size = (
unsigned int)size;
14408 ibf_dump_mark(
void *
ptr)
14418 ibf_dump_free(
void *
ptr)
14421 if (dump->global_buffer.obj_table) {
14422 st_free_table(dump->global_buffer.obj_table);
14423 dump->global_buffer.obj_table = 0;
14425 if (dump->iseq_table) {
14426 st_free_table(dump->iseq_table);
14427 dump->iseq_table = 0;
14432 ibf_dump_memsize(
const void *
ptr)
14436 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
14437 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
14443 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
14444 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
14450 dump->global_buffer.obj_table = NULL;
14451 dump->iseq_table = NULL;
14454 dump->global_buffer.obj_table = ibf_dump_object_table_new();
14455 dump->iseq_table = st_init_numtable();
14457 dump->current_buffer = &dump->global_buffer;
14468 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
14469 ISEQ_BODY(iseq)->local_iseq != iseq) {
14472 if (
RTEST(ISEQ_COVERAGE(iseq))) {
14477 ibf_dump_setup(dump, dump_obj);
14479 ibf_dump_write(dump, &header,
sizeof(header));
14480 ibf_dump_iseq(dump, iseq);
14482 header.magic[0] =
'Y';
14483 header.magic[1] =
'A';
14484 header.magic[2] =
'R';
14485 header.magic[3] =
'B';
14486 header.major_version = IBF_MAJOR_VERSION;
14487 header.minor_version = IBF_MINOR_VERSION;
14488 header.endian = IBF_ENDIAN_MARK;
14490 ibf_dump_iseq_list(dump, &header);
14491 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
14492 header.size = ibf_dump_pos(dump);
14495 VALUE opt_str = opt;
14498 ibf_dump_write(dump,
ptr, header.extra_size);
14501 header.extra_size = 0;
14504 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
14506 str = dump->global_buffer.str;
14511 static const ibf_offset_t *
14512 ibf_iseq_list(
const struct ibf_load *load)
14514 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
14518 rb_ibf_load_iseq_complete(
rb_iseq_t *iseq)
14522 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
14525 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
14526 iseq->aux.loader.index, offset,
14527 load->header->size);
14529 ibf_load_iseq_each(load, iseq, offset);
14530 ISEQ_COMPILE_DATA_CLEAR(iseq);
14532 rb_iseq_init_trace(iseq);
14533 load->iseq = prev_src_iseq;
14538 rb_iseq_complete(
const rb_iseq_t *iseq)
14540 rb_ibf_load_iseq_complete((
rb_iseq_t *)iseq);
14548 int iseq_index = (int)(
VALUE)index_iseq;
14551 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
14552 (
void *)index_iseq, (
void *)load->iseq_list);
14554 if (iseq_index == -1) {
14558 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
14561 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
14569 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
14572 iseq->aux.loader.obj = load->loader_obj;
14573 iseq->aux.loader.index = iseq_index;
14575 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
14576 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
14578 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
14580 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
14582 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
14584 rb_ibf_load_iseq_complete(iseq);
14588 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
14589 (
void *)iseq, (
void *)load->iseq);
14597 ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
14600 load->loader_obj = loader_obj;
14601 load->global_buffer.buff = bytes;
14602 load->header = header;
14603 load->global_buffer.size = header->size;
14604 load->global_buffer.obj_list_offset = header->global_object_list_offset;
14605 load->global_buffer.obj_list_size = header->global_object_list_size;
14606 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
14607 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
14610 load->current_buffer = &load->global_buffer;
14612 if (size < header->size) {
14615 if (strncmp(header->magic,
"YARB", 4) != 0) {
14618 if (header->major_version != IBF_MAJOR_VERSION ||
14619 header->minor_version != IBF_MINOR_VERSION) {
14621 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
14623 if (header->endian != IBF_ENDIAN_MARK) {
14629 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14631 header->iseq_list_offset);
14633 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14635 load->global_buffer.obj_list_offset);
14648 if (USE_LAZY_LOAD) {
14657 ibf_loader_mark(
void *
ptr)
14666 ibf_loader_free(
void *
ptr)
14673 ibf_loader_memsize(
const void *
ptr)
14680 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
14681 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
14685 rb_iseq_ibf_load(
VALUE str)
14691 ibf_load_setup(load, loader_obj, str);
14692 iseq = ibf_load_iseq(load, 0);
14699 rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
14705 ibf_load_setup_bytes(load, loader_obj, bytes, size);
14706 iseq = ibf_load_iseq(load, 0);
14713 rb_iseq_ibf_load_extra_data(
VALUE str)
14719 ibf_load_setup(load, loader_obj, str);
14720 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
14725 #include "prism_compile.c"
#define RUBY_ASSERT(...)
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 NUM2ULONG
Old name of RB_NUM2ULONG.
#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 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 ULONG2NUM
Old name of RB_ULONG2NUM.
#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.
void rb_raise(VALUE exc_class, const char *fmt,...)
Exception entry point.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_eStandardError
StandardError exception.
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
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_eArgError
ArgumentError exception.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_eSyntaxError
SyntaxError exception.
@ RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK
Warning is for checking unused block strictly.
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
VALUE rb_cArray
Array class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_cHash
Hash 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.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#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.
int rb_enc_get_index(VALUE obj)
Queries the index of the encoding of the passed object, if any.
rb_encoding * rb_enc_find(const char *name)
Identical to rb_find_encoding(), except it takes a C's string instead of Ruby's.
rb_encoding * rb_to_encoding(VALUE obj)
Identical to rb_find_encoding(), except it raises an exception instead of returning NULL.
VALUE rb_enc_from_encoding(rb_encoding *enc)
Queries the Ruby-level counterpart instance of rb_cEncoding that corresponds to the passed encoding.
rb_encoding * rb_enc_from_index(int idx)
Identical to rb_find_encoding(), except it takes an encoding index instead of a Ruby object.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
int rb_enc_find_index(const char *name)
Queries the index of the encoding.
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_str_new(), except it additionally takes an encoding.
ID rb_intern3(const char *name, long len, rb_encoding *enc)
Identical to rb_intern2(), except it additionally takes an encoding.
void rb_gc_mark(VALUE obj)
Marks an object.
void rb_memerror(void)
Triggers out-of-memory error.
void rb_mark_set(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only keys of the table and leave their associated values...
VALUE rb_ary_reverse(VALUE ary)
Destructively reverses the passed array in-place.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_unshift(VALUE ary, VALUE elem)
Destructively prepends the passed item at the beginning of the passed array.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Import an integer from a buffer.
VALUE rb_big_cmp(VALUE lhs, VALUE rhs)
Compares the passed two bignums.
VALUE rb_dbl2big(double d)
Converts a C's double into a bignum.
#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.
VALUE rb_complex_new(VALUE real, VALUE imag)
Constructs a Complex, by first multiplying the imaginary part with 1i then adds it to the real part.
void rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash)
Inserts a list of key-value pairs into a hash table at once.
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_hash_freeze(VALUE obj)
Just another name of rb_obj_freeze.
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Identical to rb_hash_aref(), except it always returns RUBY_Qnil for misshits.
VALUE rb_hash_dup(VALUE hash)
Duplicates a hash.
VALUE rb_hash_clear(VALUE hash)
Swipes everything out of the passed hash table.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
ID rb_id_attrset(ID id)
Calculates an ID of attribute writer.
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.
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
int rb_reg_options(VALUE re)
Queries the options of the passed regular expression.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
VALUE rb_str_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the 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.
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to 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_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
ID rb_intern_str(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a frozen Ruby String instead of a C String.
RBIMPL_ATTR_NORETURN() void rb_eof_error(void)
Utility function to raise rb_eEOFError.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#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.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
#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.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#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 RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
void(* RUBY_DATA_FUNC)(void *)
This is the type of callbacks registered to RData.
#define RHASH_SIZE(h)
Queries the size of the hash.
static VALUE RREGEXP_SRC(VALUE rexp)
Convenient getter function.
#define StringValue(v)
Ensures that the parameter object is a String.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
static int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#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.
VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
Identical to rb_data_typed_object_wrap(), except it allocates a new data region internally instead of...
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a 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 is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
struct rb_iseq_constant_body::@154 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.
static bool rb_integer_type_p(VALUE obj)
Queries if the object is an instance of rb_cInteger.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
@ RUBY_T_MASK
Bitmask of ruby_value_type.
void * ruby_xmalloc2(size_t nelems, size_t elemsiz)
Identical to ruby_xmalloc(), except it allocates nelems * elemsiz bytes.
void * ruby_xmalloc(size_t size)
Allocates a storage instance.
void ruby_xfree(void *ptr)
Deallocates a storage instance.