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))
85 unsigned int rescued: 2;
86 unsigned int unremovable: 1;
91 enum ruby_vminsn_type insn_id;
121 const void *ensure_node;
126const ID rb_iseq_shared_exc_local_tbl[] = {idERROR_INFO};
146#define compile_debug CPDEBUG
148#define compile_debug ISEQ_COMPILE_DATA(iseq)->option->debug_level
153#define compile_debug_print_indent(level) \
154 ruby_debug_print_indent((level), compile_debug, gl_node_level * 2)
156#define debugp(header, value) (void) \
157 (compile_debug_print_indent(1) && \
158 ruby_debug_print_value(1, compile_debug, (header), (value)))
160#define debugi(header, id) (void) \
161 (compile_debug_print_indent(1) && \
162 ruby_debug_print_id(1, compile_debug, (header), (id)))
164#define debugp_param(header, value) (void) \
165 (compile_debug_print_indent(1) && \
166 ruby_debug_print_value(1, compile_debug, (header), (value)))
168#define debugp_verbose(header, value) (void) \
169 (compile_debug_print_indent(2) && \
170 ruby_debug_print_value(2, compile_debug, (header), (value)))
172#define debugp_verbose_node(header, value) (void) \
173 (compile_debug_print_indent(10) && \
174 ruby_debug_print_value(10, compile_debug, (header), (value)))
176#define debug_node_start(node) ((void) \
177 (compile_debug_print_indent(1) && \
178 (ruby_debug_print_node(1, CPDEBUG, "", (const NODE *)(node)), gl_node_level)), \
181#define debug_node_end() gl_node_level --
185#define debugi(header, id) ((void)0)
186#define debugp(header, value) ((void)0)
187#define debugp_verbose(header, value) ((void)0)
188#define debugp_verbose_node(header, value) ((void)0)
189#define debugp_param(header, value) ((void)0)
190#define debug_node_start(node) ((void)0)
191#define debug_node_end() ((void)0)
194#if CPDEBUG > 1 || CPDEBUG < 0
196#define printf ruby_debug_printf
197#define debugs if (compile_debug_print_indent(1)) ruby_debug_printf
198#define debug_compile(msg, v) ((void)(compile_debug_print_indent(1) && fputs((msg), stderr)), (v))
200#define debugs if(0)printf
201#define debug_compile(msg, v) (v)
204#define LVAR_ERRINFO (1)
207#define NEW_LABEL(l) new_label_body(iseq, (l))
208#define LABEL_FORMAT "<L%03d>"
210#define NEW_ISEQ(node, name, type, line_no) \
211 new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
213#define NEW_CHILD_ISEQ(node, name, type, line_no) \
214 new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
216#define NEW_CHILD_ISEQ_WITH_CALLBACK(callback_func, name, type, line_no) \
217 new_child_iseq_with_callback(iseq, (callback_func), (name), iseq, (type), (line_no))
220#define ADD_SEQ(seq1, seq2) \
221 APPEND_LIST((seq1), (seq2))
224#define ADD_INSN(seq, line_node, insn) \
225 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 0))
228#define ADD_SYNTHETIC_INSN(seq, line_no, node_id, insn) \
229 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line_no), (node_id), BIN(insn), 0))
232#define INSERT_BEFORE_INSN(next, line_no, node_id, insn) \
233 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
236#define INSERT_AFTER_INSN(prev, line_no, node_id, insn) \
237 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
240#define ADD_INSN1(seq, line_node, insn, op1) \
241 ADD_ELEM((seq), (LINK_ELEMENT *) \
242 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 1, (VALUE)(op1)))
245#define INSERT_BEFORE_INSN1(next, line_no, node_id, insn, op1) \
246 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) \
247 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
250#define INSERT_AFTER_INSN1(prev, line_no, node_id, insn, op1) \
251 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) \
252 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
254#define LABEL_REF(label) ((label)->refcnt++)
257#define ADD_INSNL(seq, line_node, insn, label) (ADD_INSN1(seq, line_node, insn, label), LABEL_REF(label))
259#define ADD_INSN2(seq, line_node, insn, op1, op2) \
260 ADD_ELEM((seq), (LINK_ELEMENT *) \
261 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
263#define ADD_INSN3(seq, line_node, insn, op1, op2, op3) \
264 ADD_ELEM((seq), (LINK_ELEMENT *) \
265 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
268#define ADD_SEND(seq, line_node, id, argc) \
269 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
271#define ADD_SEND_WITH_FLAG(seq, line_node, id, argc, flag) \
272 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)(flag), NULL)
274#define ADD_SEND_WITH_BLOCK(seq, line_node, id, argc, block) \
275 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
277#define ADD_CALL_RECEIVER(seq, line_node) \
278 ADD_INSN((seq), (line_node), putself)
280#define ADD_CALL(seq, line_node, id, argc) \
281 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
283#define ADD_CALL_WITH_BLOCK(seq, line_node, id, argc, block) \
284 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
286#define ADD_SEND_R(seq, line_node, id, argc, block, flag, keywords) \
287 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)))
289#define ADD_TRACE(seq, event) \
290 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), 0))
291#define ADD_TRACE_WITH_DATA(seq, event, data) \
292 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), (data)))
294static void iseq_add_getlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
295static void iseq_add_setlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
297#define ADD_GETLOCAL(seq, line_node, idx, level) iseq_add_getlocal(iseq, (seq), (line_node), (idx), (level))
298#define ADD_SETLOCAL(seq, line_node, idx, level) iseq_add_setlocal(iseq, (seq), (line_node), (idx), (level))
301#define ADD_LABEL(seq, label) \
302 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
304#define APPEND_LABEL(seq, before, label) \
305 APPEND_ELEM((seq), (before), (LINK_ELEMENT *) (label))
307#define ADD_ADJUST(seq, line_node, label) \
308 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), nd_line(line_node)))
310#define ADD_ADJUST_RESTORE(seq, label) \
311 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
313#define LABEL_UNREMOVABLE(label) \
314 ((label) ? (LABEL_REF(label), (label)->unremovable=1) : 0)
315#define ADD_CATCH_ENTRY(type, ls, le, iseqv, lc) do { \
316 VALUE _e = rb_ary_new3(5, (type), \
317 (VALUE)(ls) | 1, (VALUE)(le) | 1, \
318 (VALUE)(iseqv), (VALUE)(lc) | 1); \
319 LABEL_UNREMOVABLE(ls); \
322 if (NIL_P(ISEQ_COMPILE_DATA(iseq)->catch_table_ary)) \
323 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, rb_ary_hidden_new(3)); \
324 rb_ary_push(ISEQ_COMPILE_DATA(iseq)->catch_table_ary, freeze_hide_obj(_e)); \
328#define COMPILE(anchor, desc, node) \
329 (debug_compile("== " desc "\n", \
330 iseq_compile_each(iseq, (anchor), (node), 0)))
333#define COMPILE_POPPED(anchor, desc, node) \
334 (debug_compile("== " desc "\n", \
335 iseq_compile_each(iseq, (anchor), (node), 1)))
338#define COMPILE_(anchor, desc, node, popped) \
339 (debug_compile("== " desc "\n", \
340 iseq_compile_each(iseq, (anchor), (node), (popped))))
342#define COMPILE_RECV(anchor, desc, node, recv) \
343 (private_recv_p(node) ? \
344 (ADD_INSN(anchor, node, putself), VM_CALL_FCALL) : \
345 COMPILE(anchor, desc, recv) ? 0 : -1)
347#define OPERAND_AT(insn, idx) \
348 (((INSN*)(insn))->operands[(idx)])
350#define INSN_OF(insn) \
351 (((INSN*)(insn))->insn_id)
353#define IS_INSN(link) ((link)->type == ISEQ_ELEMENT_INSN)
354#define IS_LABEL(link) ((link)->type == ISEQ_ELEMENT_LABEL)
355#define IS_ADJUST(link) ((link)->type == ISEQ_ELEMENT_ADJUST)
356#define IS_TRACE(link) ((link)->type == ISEQ_ELEMENT_TRACE)
357#define IS_INSN_ID(iobj, insn) (INSN_OF(iobj) == BIN(insn))
358#define IS_NEXT_INSN_ID(link, insn) \
359 ((link)->next && IS_INSN((link)->next) && IS_INSN_ID((link)->next, insn))
367append_compile_error(const
rb_iseq_t *iseq,
int line, const
char *fmt, ...)
369 VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
370 VALUE file = rb_iseq_path(iseq);
375 err = rb_syntax_error_append(err, file, line, -1, NULL, fmt, args);
377 if (
NIL_P(err_info)) {
378 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err);
381 else if (!err_info) {
392compile_bug(
rb_iseq_t *iseq,
int line,
const char *fmt, ...)
396 rb_report_bug_valist(rb_iseq_path(iseq), line, fmt, args);
402#define COMPILE_ERROR append_compile_error
404#define ERROR_ARGS_AT(n) iseq, nd_line(n),
405#define ERROR_ARGS ERROR_ARGS_AT(node)
407#define EXPECT_NODE(prefix, node, ndtype, errval) \
409 const NODE *error_node = (node); \
410 enum node_type error_type = nd_type(error_node); \
411 if (error_type != (ndtype)) { \
412 COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
413 prefix ": " #ndtype " is expected, but %s", \
414 ruby_node_name(error_type)); \
419#define EXPECT_NODE_NONULL(prefix, parent, ndtype, errval) \
421 COMPILE_ERROR(ERROR_ARGS_AT(parent) \
422 prefix ": must be " #ndtype ", but 0"); \
426#define UNKNOWN_NODE(prefix, node, errval) \
428 const NODE *error_node = (node); \
429 COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
430 ruby_node_name(nd_type(error_node))); \
437#define CHECK(sub) if (!(sub)) {BEFORE_RETURN;return COMPILE_NG;}
438#define NO_CHECK(sub) (void)(sub)
441#define DECL_ANCHOR(name) \
442 LINK_ANCHOR name[1] = {{{ISEQ_ELEMENT_ANCHOR,},&name[0].anchor}}
443#define INIT_ANCHOR(name) \
444 ((name->last = &name->anchor)->next = NULL)
447freeze_hide_obj(
VALUE obj)
450 RBASIC_CLEAR_CLASS(obj);
454#include "optinsn.inc"
455#if OPT_INSTRUCTIONS_UNIFICATION
456#include "optunifs.inc"
461#define ISEQ_ARG iseq,
462#define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
465#define ISEQ_ARG_DECLARE
469#define gl_node_level ISEQ_COMPILE_DATA(iseq)->node_level
475static int insn_data_length(
INSN *iobj);
476static int calc_sp_depth(
int depth,
INSN *iobj);
478static INSN *new_insn_body(
rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...);
491static int iseq_set_exception_local_table(
rb_iseq_t *iseq);
495static int iseq_set_exception_table(
rb_iseq_t *iseq);
496static int iseq_set_optargs_table(
rb_iseq_t *iseq);
499static int compile_hash(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped);
506verify_list(ISEQ_ARG_DECLARE
const char *info,
LINK_ANCHOR *
const anchor)
512 if (!compile_debug)
return;
514 list = anchor->anchor.next;
515 plist = &anchor->anchor;
517 if (plist != list->prev) {
524 if (anchor->last != plist && anchor->last != 0) {
529 rb_bug(
"list verify error: %08x (%s)", flag, info);
534#define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
541 VALUE *original = rb_iseq_original_iseq(iseq);
543 while (i < ISEQ_BODY(iseq)->iseq_size) {
544 VALUE insn = original[i];
545 const char *types = insn_op_types(insn);
547 for (
int j=0; types[j]; j++) {
548 if (types[j] == TS_CALLDATA) {
552 if (cc != vm_cc_empty()) {
554 rb_bug(
"call cache is not initialized by vm_cc_empty()");
561 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
562 struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
565 if (cc != NULL && cc != vm_cc_empty()) {
567 rb_bug(
"call cache is not initialized by vm_cc_empty()");
579 elem->prev = anchor->last;
580 anchor->last->next = elem;
582 verify_list(
"add", anchor);
592 elem->next = before->next;
593 elem->next->prev = elem;
595 if (before == anchor->last) anchor->last = elem;
596 verify_list(
"add", anchor);
599#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
600#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
604branch_coverage_valid_p(
rb_iseq_t *iseq,
int first_line)
606 if (!ISEQ_COVERAGE(iseq))
return 0;
607 if (!ISEQ_BRANCH_COVERAGE(iseq))
return 0;
608 if (first_line <= 0)
return 0;
612#define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x)))
617 const int first_lineno = loc->beg_pos.lineno, first_column = loc->beg_pos.column;
618 const int last_lineno = loc->end_pos.lineno, last_column = loc->end_pos.column;
619 VALUE branch = rb_ary_hidden_new(6);
621 rb_hash_aset(structure, key, branch);
623 rb_ary_push(branch,
INT2FIX(first_lineno));
624 rb_ary_push(branch,
INT2FIX(first_column));
625 rb_ary_push(branch,
INT2FIX(last_lineno));
626 rb_ary_push(branch,
INT2FIX(last_column));
633 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return Qundef;
644 VALUE branch_base = rb_hash_aref(structure, key);
647 if (
NIL_P(branch_base)) {
648 branch_base = setup_branch(loc,
type, structure, key);
649 branches = rb_hash_new();
651 rb_ary_push(branch_base, branches);
661generate_dummy_line_node(
int lineno,
int node_id)
664 nd_set_line(&dummy, lineno);
665 nd_set_node_id(&dummy, node_id);
672 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return;
683 VALUE branch = rb_hash_aref(branches, key);
687 branch = setup_branch(loc,
type, branches, key);
690 rb_ary_push(branch,
LONG2FIX(counter_idx));
691 rb_ary_push(counters,
INT2FIX(0));
697 ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
698 ADD_SYNTHETIC_INSN(seq, loc->end_pos.lineno, node_id, nop);
701#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
704validate_label(st_data_t name, st_data_t label, st_data_t arg)
708 if (!lobj->link.next) {
710 COMPILE_ERROR(iseq, lobj->position,
711 "%"PRIsVALUE
": undefined label",
721 st_foreach(labels_table, validate_label, (st_data_t)iseq);
722 st_free_table(labels_table);
726get_nd_recv(
const NODE *node)
728 switch (nd_type(node)) {
730 return RNODE_CALL(node)->nd_recv;
732 return RNODE_OPCALL(node)->nd_recv;
736 return RNODE_QCALL(node)->nd_recv;
740 return RNODE_ATTRASGN(node)->nd_recv;
742 return RNODE_OP_ASGN1(node)->nd_recv;
744 return RNODE_OP_ASGN2(node)->nd_recv;
746 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
751get_node_call_nd_mid(
const NODE *node)
753 switch (nd_type(node)) {
755 return RNODE_CALL(node)->nd_mid;
757 return RNODE_OPCALL(node)->nd_mid;
759 return RNODE_FCALL(node)->nd_mid;
761 return RNODE_QCALL(node)->nd_mid;
763 return RNODE_VCALL(node)->nd_mid;
765 return RNODE_ATTRASGN(node)->nd_mid;
767 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
772get_nd_args(
const NODE *node)
774 switch (nd_type(node)) {
776 return RNODE_CALL(node)->nd_args;
778 return RNODE_OPCALL(node)->nd_args;
780 return RNODE_FCALL(node)->nd_args;
782 return RNODE_QCALL(node)->nd_args;
786 return RNODE_ATTRASGN(node)->nd_args;
788 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
793get_node_colon_nd_mid(
const NODE *node)
795 switch (nd_type(node)) {
797 return RNODE_COLON2(node)->nd_mid;
799 return RNODE_COLON3(node)->nd_mid;
801 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
806get_nd_vid(
const NODE *node)
808 switch (nd_type(node)) {
810 return RNODE_LASGN(node)->nd_vid;
812 return RNODE_DASGN(node)->nd_vid;
814 return RNODE_IASGN(node)->nd_vid;
816 return RNODE_CVASGN(node)->nd_vid;
818 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
823get_nd_value(
const NODE *node)
825 switch (nd_type(node)) {
827 return RNODE_LASGN(node)->nd_value;
829 return RNODE_DASGN(node)->nd_value;
831 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
836get_string_value(
const NODE *node)
838 switch (nd_type(node)) {
840 return rb_node_str_string_val(node);
842 return rb_node_file_path_val(node);
844 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
854 (*ifunc->func)(iseq, ret, ifunc->data);
856 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
858 CHECK(iseq_setup_insn(iseq, ret));
859 return iseq_setup(iseq, ret);
862static bool drop_unreachable_return(
LINK_ANCHOR *ret);
871 NO_CHECK(COMPILE(ret,
"nil", node));
872 iseq_set_local_table(iseq, 0, 0);
875 else if (nd_type_p(node, NODE_SCOPE)) {
877 iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl, (
NODE *)RNODE_SCOPE(node)->nd_args);
878 iseq_set_arguments(iseq, ret, (
NODE *)RNODE_SCOPE(node)->nd_args);
880 switch (ISEQ_BODY(iseq)->
type) {
881 case ISEQ_TYPE_BLOCK:
883 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
884 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
886 start->rescued = LABEL_RESCUE_BEG;
887 end->rescued = LABEL_RESCUE_END;
890 ADD_SYNTHETIC_INSN(ret, ISEQ_BODY(iseq)->location.first_lineno, -1, nop);
891 ADD_LABEL(ret, start);
892 CHECK(COMPILE(ret,
"block body", RNODE_SCOPE(node)->nd_body));
895 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
898 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
899 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
902 case ISEQ_TYPE_CLASS:
905 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
907 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
910 case ISEQ_TYPE_METHOD:
912 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
914 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
915 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
917 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
921 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
928#define INVALID_ISEQ_TYPE(type) \
929 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
930 switch (ISEQ_BODY(iseq)->
type) {
931 case INVALID_ISEQ_TYPE(
METHOD);
932 case INVALID_ISEQ_TYPE(CLASS);
933 case INVALID_ISEQ_TYPE(BLOCK);
934 case INVALID_ISEQ_TYPE(EVAL);
935 case INVALID_ISEQ_TYPE(MAIN);
936 case INVALID_ISEQ_TYPE(TOP);
937#undef INVALID_ISEQ_TYPE
938 case ISEQ_TYPE_RESCUE:
939 iseq_set_exception_local_table(iseq);
940 CHECK(COMPILE(ret,
"rescue", node));
942 case ISEQ_TYPE_ENSURE:
943 iseq_set_exception_local_table(iseq);
944 CHECK(COMPILE_POPPED(ret,
"ensure", node));
946 case ISEQ_TYPE_PLAIN:
947 CHECK(COMPILE(ret,
"ensure", node));
950 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
953 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
958 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
959 NODE dummy_line_node = generate_dummy_line_node(0, -1);
960 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
961 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
963 else if (!drop_unreachable_return(ret)) {
964 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
968 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
969 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
970 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
971 validate_labels(iseq, labels_table);
974 CHECK(iseq_setup_insn(iseq, ret));
975 return iseq_setup(iseq, ret);
979rb_iseq_translate_threaded_code(
rb_iseq_t *iseq)
981#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
982 const void *
const *table = rb_vm_get_insns_address_table();
984 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
986 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
987 int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
988 int len = insn_len(insn);
989 encoded[i] = (
VALUE)table[insn];
996 rb_yjit_live_iseq_count++;
997 rb_yjit_iseq_alloc_count++;
1004rb_iseq_original_iseq(
const rb_iseq_t *iseq)
1006 VALUE *original_code;
1008 if (ISEQ_ORIGINAL_ISEQ(iseq))
return ISEQ_ORIGINAL_ISEQ(iseq);
1009 original_code = ISEQ_ORIGINAL_ISEQ_ALLOC(iseq, ISEQ_BODY(iseq)->iseq_size);
1010 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
1012#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1016 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1017 const void *addr = (
const void *)original_code[i];
1018 const int insn = rb_vm_insn_addr2insn(addr);
1020 original_code[i] = insn;
1021 i += insn_len(insn);
1025 return original_code;
1038#if defined(__sparc) && SIZEOF_VOIDP == 4 && defined(__GNUC__)
1039 #define STRICT_ALIGNMENT
1045#if defined(__OpenBSD__)
1046 #include <sys/endian.h>
1047 #ifdef __STRICT_ALIGNMENT
1048 #define STRICT_ALIGNMENT
1052#ifdef STRICT_ALIGNMENT
1053 #if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
1054 #define ALIGNMENT_SIZE SIZEOF_LONG_LONG
1056 #define ALIGNMENT_SIZE SIZEOF_VALUE
1058 #define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
1059 #define ALIGNMENT_SIZE_MASK PADDING_SIZE_MAX
1062 #define PADDING_SIZE_MAX 0
1065#ifdef STRICT_ALIGNMENT
1068calc_padding(
void *ptr,
size_t size)
1073 mis = (size_t)ptr & ALIGNMENT_SIZE_MASK;
1075 padding = ALIGNMENT_SIZE - mis;
1081#if ALIGNMENT_SIZE > SIZEOF_VALUE
1082 if (size ==
sizeof(
VALUE) && padding ==
sizeof(
VALUE)) {
1096#ifdef STRICT_ALIGNMENT
1097 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1099 const size_t padding = 0;
1102 if (size >= INT_MAX - padding) rb_memerror();
1103 if (storage->pos + size + padding > storage->size) {
1104 unsigned int alloc_size = storage->size;
1106 while (alloc_size < size + PADDING_SIZE_MAX) {
1107 if (alloc_size >= INT_MAX / 2) rb_memerror();
1110 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
1112 storage = *arena = storage->next;
1115 storage->size = alloc_size;
1116#ifdef STRICT_ALIGNMENT
1117 padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1121#ifdef STRICT_ALIGNMENT
1122 storage->pos += (int)padding;
1125 ptr = (
void *)&storage->buff[storage->pos];
1126 storage->pos += (int)size;
1131compile_data_alloc(
rb_iseq_t *iseq,
size_t size)
1134 return compile_data_alloc_with_arena(arena, size);
1138compile_data_alloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1141 return compile_data_alloc(iseq, size);
1145compile_data_calloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1148 void *p = compile_data_alloc(iseq, size);
1157 return (
INSN *)compile_data_alloc_with_arena(arena,
sizeof(
INSN));
1161compile_data_alloc_label(
rb_iseq_t *iseq)
1163 return (
LABEL *)compile_data_alloc(iseq,
sizeof(
LABEL));
1167compile_data_alloc_adjust(
rb_iseq_t *iseq)
1169 return (
ADJUST *)compile_data_alloc(iseq,
sizeof(
ADJUST));
1173compile_data_alloc_trace(
rb_iseq_t *iseq)
1175 return (
TRACE *)compile_data_alloc(iseq,
sizeof(
TRACE));
1184 elem2->next = elem1->next;
1185 elem2->prev = elem1;
1186 elem1->next = elem2;
1188 elem2->next->prev = elem2;
1198 elem2->prev = elem1->prev;
1199 elem2->next = elem1;
1200 elem1->prev = elem2;
1202 elem2->prev->next = elem2;
1212 elem2->prev = elem1->prev;
1213 elem2->next = elem1->next;
1215 elem1->prev->next = elem2;
1218 elem1->next->prev = elem2;
1225 elem->prev->next = elem->next;
1227 elem->next->prev = elem->prev;
1234 return anchor->anchor.next;
1240 return anchor->last;
1247 switch (elem->type) {
1248 case ISEQ_ELEMENT_INSN:
1249 case ISEQ_ELEMENT_ADJUST:
1259LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1261 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1262 if (first_insn != NULL &&
1263 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1272LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1274 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1292 if (anc2->anchor.next) {
1295 anc1->last->next = anc2->anchor.next;
1296 anc2->anchor.next->prev = anc1->last;
1297 anc1->last = anc2->last;
1302 verify_list(
"append", anc1);
1305#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1314 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1315 (
void *)anchor->anchor.next, (
void *)anchor->last);
1317 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1318 (
void *)list->prev, (
int)list->type);
1323 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1324 verify_list(
"debug list", anchor);
1327#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1330#define debug_list(anc, cur) ((void)0)
1336 TRACE *trace = compile_data_alloc_trace(iseq);
1338 trace->link.type = ISEQ_ELEMENT_TRACE;
1339 trace->link.next = NULL;
1340 trace->event = event;
1347new_label_body(
rb_iseq_t *iseq,
long line)
1349 LABEL *labelobj = compile_data_alloc_label(iseq);
1351 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1352 labelobj->link.next = 0;
1354 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1355 labelobj->sc_state = 0;
1357 labelobj->refcnt = 0;
1359 labelobj->rescued = LABEL_RESCUE_NONE;
1360 labelobj->unremovable = 0;
1361 labelobj->position = -1;
1368 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1369 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1370 adjust->link.next = 0;
1371 adjust->label = label;
1372 adjust->line_no = line;
1373 LABEL_UNREMOVABLE(label);
1380 const char *types = insn_op_types(insn->insn_id);
1381 for (
int j = 0; types[j]; j++) {
1382 char type = types[j];
1389 func(OPERAND_AT(insn, j), data);
1398iseq_insn_each_object_write_barrier(
VALUE obj,
VALUE iseq)
1404new_insn_core(
rb_iseq_t *iseq,
int line_no,
int node_id,
int insn_id,
int argc,
VALUE *argv)
1406 INSN *iobj = compile_data_alloc_insn(iseq);
1410 iobj->link.type = ISEQ_ELEMENT_INSN;
1411 iobj->link.next = 0;
1412 iobj->insn_id = insn_id;
1413 iobj->insn_info.line_no = line_no;
1414 iobj->insn_info.node_id = node_id;
1415 iobj->insn_info.events = 0;
1416 iobj->operands = argv;
1417 iobj->operand_size = argc;
1420 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1426new_insn_body(
rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...)
1428 VALUE *operands = 0;
1432 va_start(argv, argc);
1433 operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
1434 for (i = 0; i < argc; i++) {
1440 return new_insn_core(iseq, line_no, node_id, insn_id, argc, operands);
1446 VM_ASSERT(argc >= 0);
1449 flag |= VM_CALL_KWARG;
1450 argc += kw_arg->keyword_len;
1453 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING))
1454 && !has_blockiseq) {
1455 flag |= VM_CALL_ARGS_SIMPLE;
1458 ISEQ_BODY(iseq)->ci_size++;
1459 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1467 VALUE *operands = compile_data_calloc2(iseq,
sizeof(
VALUE), 2);
1470 operands[1] = (
VALUE)blockiseq;
1477 if (vm_ci_flag((
struct rb_callinfo *)ci) & VM_CALL_FORWARDING) {
1478 insn = new_insn_core(iseq, line_no, node_id, BIN(sendforward), 2, operands);
1481 insn = new_insn_core(iseq, line_no, node_id, BIN(send), 2, operands);
1494 VALUE ast_value = rb_ruby_ast_new(node);
1496 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1497 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1498 ret_iseq = rb_iseq_new_with_opt(ast_value, name,
1499 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1501 isolated_depth ? isolated_depth + 1 : 0,
1502 type, ISEQ_COMPILE_DATA(iseq)->option,
1503 ISEQ_BODY(iseq)->variable.script_lines);
1504 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1514 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1515 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1516 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1517 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1518 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1526 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1527 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1528 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1529 set_catch_except_p((
rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1552 while (pos < body->iseq_size) {
1553 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1554 if (insn == BIN(
throw)) {
1555 set_catch_except_p(iseq);
1558 pos += insn_len(insn);
1564 for (i = 0; i < ct->size; i++) {
1566 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1567 if (entry->type != CATCH_TYPE_BREAK
1568 && entry->type != CATCH_TYPE_NEXT
1569 && entry->type != CATCH_TYPE_REDO) {
1571 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1578iseq_insert_nop_between_end_and_cont(
rb_iseq_t *iseq)
1580 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1581 if (
NIL_P(catch_table_ary))
return;
1582 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1584 for (i = 0; i < tlen; i++) {
1590 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1592 if (ct != CATCH_TYPE_BREAK
1593 && ct != CATCH_TYPE_NEXT
1594 && ct != CATCH_TYPE_REDO) {
1596 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1598 INSN *nop = new_insn_core(iseq, 0, -1, BIN(nop), 0, 0);
1599 ELEM_INSERT_NEXT(end, &nop->link);
1612 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1617 if (compile_debug > 5)
1618 dump_disasm_list(FIRST_ELEMENT(anchor));
1620 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1621 iseq_optimize(iseq, anchor);
1623 if (compile_debug > 5)
1624 dump_disasm_list(FIRST_ELEMENT(anchor));
1626 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1627 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1628 iseq_insns_unification(iseq, anchor);
1629 if (compile_debug > 5)
1630 dump_disasm_list(FIRST_ELEMENT(anchor));
1633 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1634 iseq_insert_nop_between_end_and_cont(iseq);
1635 if (compile_debug > 5)
1636 dump_disasm_list(FIRST_ELEMENT(anchor));
1644 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1647 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1648 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1649 if (compile_debug > 5)
1650 dump_disasm_list(FIRST_ELEMENT(anchor));
1652 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1653 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1655 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1656 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1658 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1659 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1661 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1663 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1665 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1667 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1668 xfree(ISEQ_BODY(iseq)->catch_table);
1669 ISEQ_BODY(iseq)->catch_table = NULL;
1672#if VM_INSN_INFO_TABLE_IMPL == 2
1673 if (ISEQ_BODY(iseq)->insns_info.succ_index_table == NULL) {
1674 debugs(
"[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
1675 rb_iseq_insns_info_encode_positions(iseq);
1679 if (compile_debug > 1) {
1680 VALUE str = rb_iseq_disasm(iseq);
1683 verify_call_cache(iseq);
1684 debugs(
"[compile step: finish]\n");
1690iseq_set_exception_local_table(
rb_iseq_t *iseq)
1692 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1693 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1701 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1703 iseq = ISEQ_BODY(iseq)->parent_iseq;
1709get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1713 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1714 if (ISEQ_BODY(iseq)->local_table[i] == id) {
1724 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1727 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1728 "get_local_var_idx: %d", idx);
1735get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1737 int lv = 0, idx = -1;
1738 const rb_iseq_t *
const topmost_iseq = iseq;
1741 idx = get_dyna_var_idx_at_raw(iseq,
id);
1745 iseq = ISEQ_BODY(iseq)->parent_iseq;
1750 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1751 "get_dyna_var_idx: -1");
1755 *ls = ISEQ_BODY(iseq)->local_table_size;
1760iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1764 iseq = ISEQ_BODY(iseq)->parent_iseq;
1767 body = ISEQ_BODY(iseq);
1768 if (body->local_iseq == iseq &&
1769 body->
param.flags.has_block &&
1770 body->local_table_size - body->
param.block_start == idx) {
1779iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1782 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1783 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1794access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1796 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1798 if (isolated_depth && level >= isolated_depth) {
1799 if (
id == rb_intern(
"yield")) {
1800 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1803 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable '%s' from isolated Proc", rb_id2name(
id));
1807 for (
int i=0; i<level; i++) {
1809 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1812 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1815 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1816 if (write && !val) {
1817 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1821 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1824 iseq = ISEQ_BODY(iseq)->parent_iseq;
1829iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1831 for (
int i=0; i<level; i++) {
1832 iseq = ISEQ_BODY(iseq)->parent_iseq;
1835 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1843 if (iseq_local_block_param_p(iseq, idx, level)) {
1844 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1847 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1849 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1855 if (iseq_local_block_param_p(iseq, idx, level)) {
1856 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1859 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1861 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
1870 if (body->
param.flags.has_opt ||
1871 body->
param.flags.has_post ||
1872 body->
param.flags.has_rest ||
1873 body->
param.flags.has_block ||
1874 body->
param.flags.has_kw ||
1875 body->
param.flags.has_kwrest) {
1877 if (body->
param.flags.has_block) {
1878 body->
param.size = body->
param.block_start + 1;
1880 else if (body->
param.flags.has_kwrest) {
1881 body->
param.size = body->
param.keyword->rest_start + 1;
1883 else if (body->
param.flags.has_kw) {
1884 body->
param.size = body->
param.keyword->bits_start + 1;
1886 else if (body->
param.flags.has_post) {
1889 else if (body->
param.flags.has_rest) {
1890 body->
param.size = body->
param.rest_start + 1;
1892 else if (body->
param.flags.has_opt) {
1910 struct rb_iseq_param_keyword *keyword;
1911 const VALUE default_values = rb_ary_hidden_new(1);
1913 int kw = 0, rkw = 0, di = 0, i;
1915 body->
param.flags.has_kw = TRUE;
1916 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
1920 node = node->nd_next;
1923 keyword->bits_start = arg_size++;
1925 node = args->kw_args;
1927 const NODE *val_node = get_nd_value(node->nd_body);
1930 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
1934 switch (nd_type(val_node)) {
1936 dv = rb_node_sym_string_val(val_node);
1939 dv = rb_node_regx_string_val(val_node);
1942 dv = rb_node_line_lineno_val(val_node);
1945 dv = rb_node_integer_literal_val(val_node);
1948 dv = rb_node_float_literal_val(val_node);
1951 dv = rb_node_rational_literal_val(val_node);
1953 case NODE_IMAGINARY:
1954 dv = rb_node_imaginary_literal_val(val_node);
1957 dv = rb_node_encoding_val(val_node);
1969 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
1973 keyword->num = ++di;
1974 rb_ary_push(default_values, dv);
1977 node = node->nd_next;
1982 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
1983 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
1984 keyword->rest_start = arg_size++;
1985 body->
param.flags.has_kwrest = TRUE;
1987 if (kw_id == idPow) body->
param.flags.anon_kwrest = TRUE;
1989 keyword->required_num = rkw;
1990 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
1995 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
1997 if (dv == complex_mark) dv =
Qundef;
2001 keyword->default_values = dvs;
2010 if (!body->
param.flags.use_block) {
2011 body->
param.flags.use_block = 1;
2016 st_data_t key = (st_data_t)rb_intern_str(body->location.label);
2017 st_insert(vm->unused_block_warning_table, key, 1);
2025 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2029 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2035 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2037 body->
param.flags.ruby2_keywords = args->ruby2_keywords;
2038 body->
param.lead_num = arg_size = (int)args->pre_args_num;
2039 if (body->
param.lead_num > 0) body->
param.flags.has_lead = TRUE;
2040 debugs(
" - argc: %d\n", body->
param.lead_num);
2042 rest_id = args->rest_arg;
2043 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2047 block_id = args->block_arg;
2049 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2051 if (optimized_forward) {
2056 if (args->opt_args) {
2059 VALUE labels = rb_ary_hidden_new(1);
2064 label = NEW_LABEL(nd_line(RNODE(node)));
2065 rb_ary_push(labels, (
VALUE)label | 1);
2066 ADD_LABEL(optargs, label);
2067 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2068 node = node->nd_next;
2073 label = NEW_LABEL(nd_line(node_args));
2074 rb_ary_push(labels, (
VALUE)label | 1);
2075 ADD_LABEL(optargs, label);
2080 for (j = 0; j < i+1; j++) {
2083 rb_ary_clear(labels);
2085 body->
param.flags.has_opt = TRUE;
2086 body->
param.opt_num = i;
2087 body->
param.opt_table = opt_table;
2092 body->
param.rest_start = arg_size++;
2093 body->
param.flags.has_rest = TRUE;
2094 if (rest_id ==
'*') body->
param.flags.anon_rest = TRUE;
2098 if (args->first_post_arg) {
2099 body->
param.post_start = arg_size;
2100 body->
param.post_num = args->post_args_num;
2101 body->
param.flags.has_post = TRUE;
2102 arg_size += args->post_args_num;
2104 if (body->
param.flags.has_rest) {
2105 body->
param.post_start = body->
param.rest_start + 1;
2109 if (args->kw_args) {
2110 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2112 else if (args->kw_rest_arg && !optimized_forward) {
2113 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2114 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2115 keyword->rest_start = arg_size++;
2116 body->
param.keyword = keyword;
2117 body->
param.flags.has_kwrest = TRUE;
2119 static ID anon_kwrest = 0;
2120 if (!anon_kwrest) anon_kwrest = rb_intern(
"**");
2121 if (kw_id == anon_kwrest) body->
param.flags.anon_kwrest = TRUE;
2123 else if (args->no_kwarg) {
2124 body->
param.flags.accepts_no_kwarg = TRUE;
2128 body->
param.block_start = arg_size++;
2129 body->
param.flags.has_block = TRUE;
2130 iseq_set_use_block(iseq);
2134 if (optimized_forward) {
2135 body->
param.flags.use_block = 1;
2136 body->
param.flags.forwardable = TRUE;
2140 iseq_calc_param_size(iseq);
2141 body->
param.size = arg_size;
2143 if (args->pre_init) {
2144 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2146 if (args->post_init) {
2147 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2150 if (body->type == ISEQ_TYPE_BLOCK) {
2151 if (body->
param.flags.has_opt == FALSE &&
2152 body->
param.flags.has_post == FALSE &&
2153 body->
param.flags.has_rest == FALSE &&
2154 body->
param.flags.has_kw == FALSE &&
2155 body->
param.flags.has_kwrest == FALSE) {
2157 if (body->
param.lead_num == 1 && last_comma == 0) {
2159 body->
param.flags.ambiguous_param0 = TRUE;
2171 unsigned int size = tbl ? tbl->size : 0;
2172 unsigned int offset = 0;
2175 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2180 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2188 MEMCPY(ids, tbl->ids + offset,
ID, size);
2189 ISEQ_BODY(iseq)->local_table = ids;
2191 ISEQ_BODY(iseq)->local_table_size = size;
2193 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2205 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2208 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2211 else if (tlit != tval) {
2221 long x =
FIX2LONG(rb_big_cmp(lit, val));
2229 return rb_float_cmp(lit, val);
2232 const struct RRational *rat1 = RRATIONAL(val);
2233 const struct RRational *rat2 = RRATIONAL(lit);
2234 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2237 const struct RComplex *comp1 = RCOMPLEX(val);
2238 const struct RComplex *comp2 = RCOMPLEX(lit);
2239 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2242 return rb_reg_equal(val, lit) ? 0 : -1;
2250rb_iseq_cdhash_hash(
VALUE a)
2252 switch (OBJ_BUILTIN_TYPE(a)) {
2255 return (st_index_t)a;
2263 return rb_rational_hash(a);
2265 return rb_complex_hash(a);
2275 rb_iseq_cdhash_hash,
2289 rb_hash_aset(data->hash, key,
INT2FIX(lobj->position - (data->pos+data->len)));
2297 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2304 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2306 if (rb_id_table_lookup(tbl,
id,&val)) {
2311 tbl = rb_id_table_create(1);
2312 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2314 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2315 rb_id_table_insert(tbl,
id,val);
2319#define BADINSN_DUMP(anchor, list, dest) \
2320 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2322#define BADINSN_ERROR \
2323 (xfree(generated_iseq), \
2324 xfree(insns_info), \
2325 BADINSN_DUMP(anchor, list, NULL), \
2331 int stack_max = 0, sp = 0, line = 0;
2334 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2335 if (IS_LABEL(list)) {
2341 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2342 switch (list->type) {
2343 case ISEQ_ELEMENT_INSN:
2351 sp = calc_sp_depth(sp, iobj);
2353 BADINSN_DUMP(anchor, list, NULL);
2354 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2355 "argument stack underflow (%d)", sp);
2358 if (sp > stack_max) {
2362 line = iobj->insn_info.line_no;
2364 operands = iobj->operands;
2365 insn = iobj->insn_id;
2366 types = insn_op_types(insn);
2367 len = insn_len(insn);
2370 if (iobj->operand_size !=
len - 1) {
2372 BADINSN_DUMP(anchor, list, NULL);
2373 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2374 "operand size miss! (%d for %d)",
2375 iobj->operand_size,
len - 1);
2379 for (j = 0; types[j]; j++) {
2380 if (types[j] == TS_OFFSET) {
2384 BADINSN_DUMP(anchor, list, NULL);
2385 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2386 "unknown label: "LABEL_FORMAT, lobj->label_no);
2389 if (lobj->sp == -1) {
2392 else if (lobj->sp != sp) {
2393 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2394 RSTRING_PTR(rb_iseq_path(iseq)), line,
2395 lobj->label_no, lobj->sp, sp);
2401 case ISEQ_ELEMENT_LABEL:
2404 if (lobj->sp == -1) {
2408 if (lobj->sp != sp) {
2409 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2410 RSTRING_PTR(rb_iseq_path(iseq)), line,
2411 lobj->label_no, lobj->sp, sp);
2417 case ISEQ_ELEMENT_TRACE:
2422 case ISEQ_ELEMENT_ADJUST:
2427 sp = adjust->label ? adjust->label->sp : 0;
2428 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2429 BADINSN_DUMP(anchor, list, NULL);
2430 COMPILE_ERROR(iseq, adjust->line_no,
2431 "iseq_set_sequence: adjust bug %d < %d",
2438 BADINSN_DUMP(anchor, list, NULL);
2439 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2448 int insns_info_index,
int code_index,
const INSN *iobj)
2450 if (insns_info_index == 0 ||
2451 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2452#ifdef USE_ISEQ_NODE_ID
2453 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2455 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2456 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2457#ifdef USE_ISEQ_NODE_ID
2458 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2460 insns_info[insns_info_index].events = iobj->insn_info.events;
2461 positions[insns_info_index] = code_index;
2469 int insns_info_index,
int code_index,
const ADJUST *adjust)
2471 insns_info[insns_info_index].line_no = adjust->line_no;
2472 insns_info[insns_info_index].node_id = -1;
2473 insns_info[insns_info_index].events = 0;
2474 positions[insns_info_index] = code_index;
2479array_to_idlist(
VALUE arr)
2484 for (
int i = 0; i < size; i++) {
2493idlist_to_array(
const ID *ids)
2495 VALUE arr = rb_ary_new();
2497 rb_ary_push(arr,
ID2SYM(*ids++));
2510 unsigned int *positions;
2512 VALUE *generated_iseq;
2516 int insn_num, code_index, insns_info_index, sp = 0;
2517 int stack_max = fix_sp_depth(iseq, anchor);
2519 if (stack_max < 0)
return COMPILE_NG;
2522 insn_num = code_index = 0;
2523 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2524 switch (list->type) {
2525 case ISEQ_ELEMENT_INSN:
2529 sp = calc_sp_depth(sp, iobj);
2531 events = iobj->insn_info.events |= events;
2532 if (ISEQ_COVERAGE(iseq)) {
2533 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2534 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2535 int line = iobj->insn_info.line_no - 1;
2536 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2540 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2541 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2542 rb_ary_push(ISEQ_PC2BRANCHINDEX(iseq),
Qnil);
2547 code_index += insn_data_length(iobj);
2552 case ISEQ_ELEMENT_LABEL:
2555 lobj->position = code_index;
2556 if (lobj->sp != sp) {
2557 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2558 RSTRING_PTR(rb_iseq_path(iseq)),
2559 lobj->label_no, lobj->sp, sp);
2564 case ISEQ_ELEMENT_TRACE:
2567 events |= trace->event;
2568 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2571 case ISEQ_ELEMENT_ADJUST:
2574 if (adjust->line_no != -1) {
2576 sp = adjust->label ? adjust->label->sp : 0;
2577 if (orig_sp - sp > 0) {
2578 if (orig_sp - sp > 1) code_index++;
2592 positions =
ALLOC_N(
unsigned int, insn_num);
2593 if (ISEQ_IS_SIZE(body)) {
2597 body->is_entries = NULL;
2600 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2607 iseq_bits_t * mark_offset_bits;
2608 int code_size = code_index;
2610 iseq_bits_t tmp[1] = {0};
2611 bool needs_bitmap =
false;
2613 if (ISEQ_MBITS_BUFLEN(code_index) == 1) {
2614 mark_offset_bits = tmp;
2617 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(code_index));
2620 list = FIRST_ELEMENT(anchor);
2621 insns_info_index = code_index = sp = 0;
2624 switch (list->type) {
2625 case ISEQ_ELEMENT_INSN:
2633 sp = calc_sp_depth(sp, iobj);
2635 operands = iobj->operands;
2636 insn = iobj->insn_id;
2637 generated_iseq[code_index] = insn;
2638 types = insn_op_types(insn);
2639 len = insn_len(insn);
2641 for (j = 0; types[j]; j++) {
2642 char type = types[j];
2650 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2655 VALUE map = operands[j];
2658 data.pos = code_index;
2662 rb_hash_rehash(map);
2663 freeze_hide_obj(map);
2664 generated_iseq[code_index + 1 + j] = map;
2665 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2667 needs_bitmap =
true;
2672 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2677 VALUE v = operands[j];
2678 generated_iseq[code_index + 1 + j] = v;
2682 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2683 needs_bitmap =
true;
2690 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2691 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2692 if (UNLIKELY(ic_index >= body->ic_size)) {
2693 BADINSN_DUMP(anchor, &iobj->link, 0);
2694 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2695 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2696 ic_index, ISEQ_IS_SIZE(body));
2699 ic->
segments = array_to_idlist(operands[j]);
2701 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2706 unsigned int ic_index =
FIX2UINT(operands[j]);
2708 IVC cache = ((
IVC)&body->is_entries[ic_index]);
2710 if (insn == BIN(setinstancevariable)) {
2711 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2714 cache->iv_set_name = 0;
2717 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
2722 unsigned int ic_index =
FIX2UINT(operands[j]);
2723 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2724 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2725 BADINSN_DUMP(anchor, &iobj->link, 0);
2726 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2727 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2728 ic_index, ISEQ_IS_SIZE(body));
2730 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2737 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2738 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2740 cd->cc = vm_cc_empty();
2741 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2745 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2748 generated_iseq[code_index + 1 + j] = operands[j];
2751 generated_iseq[code_index + 1 + j] = operands[j];
2754 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2755 "unknown operand type: %c",
type);
2759 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2763 case ISEQ_ELEMENT_LABEL:
2766 if (lobj->sp != sp) {
2767 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2768 RSTRING_PTR(rb_iseq_path(iseq)),
2769 lobj->label_no, lobj->sp, sp);
2774 case ISEQ_ELEMENT_ADJUST:
2779 if (adjust->label) {
2780 sp = adjust->label->sp;
2786 if (adjust->line_no != -1) {
2787 const int diff = orig_sp - sp;
2789 if (insns_info_index == 0) {
2790 COMPILE_ERROR(iseq, adjust->line_no,
2791 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2793 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2796 generated_iseq[code_index++] = BIN(adjuststack);
2797 generated_iseq[code_index++] = orig_sp - sp;
2799 else if (diff == 1) {
2800 generated_iseq[code_index++] = BIN(pop);
2802 else if (diff < 0) {
2803 int label_no = adjust->label ? adjust->label->label_no : -1;
2804 xfree(generated_iseq);
2807 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
2808 xfree(mark_offset_bits);
2810 debug_list(anchor, list);
2811 COMPILE_ERROR(iseq, adjust->line_no,
2812 "iseq_set_sequence: adjust bug to %d %d < %d",
2813 label_no, orig_sp, sp);
2826 body->iseq_encoded = (
void *)generated_iseq;
2827 body->iseq_size = code_index;
2828 body->stack_max = stack_max;
2830 if (ISEQ_MBITS_BUFLEN(body->iseq_size) == 1) {
2831 body->mark_bits.single = mark_offset_bits[0];
2835 body->mark_bits.list = mark_offset_bits;
2838 body->mark_bits.list = 0;
2839 ruby_xfree(mark_offset_bits);
2844 body->insns_info.body = insns_info;
2845 body->insns_info.positions = positions;
2848 body->insns_info.body = insns_info;
2849 REALLOC_N(positions,
unsigned int, insns_info_index);
2850 body->insns_info.positions = positions;
2851 body->insns_info.size = insns_info_index;
2857label_get_position(
LABEL *lobj)
2859 return lobj->position;
2863label_get_sp(
LABEL *lobj)
2869iseq_set_exception_table(
rb_iseq_t *iseq)
2871 const VALUE *tptr, *ptr;
2872 unsigned int tlen, i;
2875 ISEQ_BODY(iseq)->catch_table = NULL;
2877 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
2878 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
2886 for (i = 0; i < table->size; i++) {
2889 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
2890 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
2891 pos = label_get_position((
LABEL *)(ptr[1] & ~1));
2893 entry->start = (
unsigned int)pos;
2894 pos = label_get_position((
LABEL *)(ptr[2] & ~1));
2896 entry->end = (
unsigned int)pos;
2903 entry->cont = label_get_position(lobj);
2904 entry->sp = label_get_sp(lobj);
2907 if (entry->type == CATCH_TYPE_RESCUE ||
2908 entry->type == CATCH_TYPE_BREAK ||
2909 entry->type == CATCH_TYPE_NEXT) {
2918 ISEQ_BODY(iseq)->catch_table = table;
2919 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
2940 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
2942 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
2943 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
2944 opt_table[i] = label_get_position((
LABEL *)opt_table[i]);
2951get_destination_insn(
INSN *iobj)
2957 list = lobj->link.next;
2959 switch (list->type) {
2960 case ISEQ_ELEMENT_INSN:
2961 case ISEQ_ELEMENT_ADJUST:
2963 case ISEQ_ELEMENT_LABEL:
2966 case ISEQ_ELEMENT_TRACE:
2969 events |= trace->event;
2977 if (list && IS_INSN(list)) {
2979 iobj->insn_info.events |= events;
2985get_next_insn(
INSN *iobj)
2990 if (IS_INSN(list) || IS_ADJUST(list)) {
2999get_prev_insn(
INSN *iobj)
3004 if (IS_INSN(list) || IS_ADJUST(list)) {
3013unref_destination(
INSN *iobj,
int pos)
3015 LABEL *lobj = (
LABEL *)OPERAND_AT(iobj, pos);
3017 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3021replace_destination(
INSN *dobj,
INSN *nobj)
3023 VALUE n = OPERAND_AT(nobj, 0);
3026 if (dl == nl)
return false;
3029 OPERAND_AT(dobj, 0) = n;
3030 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3035find_destination(
INSN *i)
3037 int pos,
len = insn_len(i->insn_id);
3038 for (pos = 0; pos <
len; ++pos) {
3039 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3040 return (
LABEL *)OPERAND_AT(i, pos);
3050 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
3053 unref_counts =
ALLOCA_N(
int, nlabels);
3054 MEMZERO(unref_counts,
int, nlabels);
3059 if (IS_INSN_ID(i, leave)) {
3063 else if ((lab = find_destination((
INSN *)i)) != 0) {
3064 unref_counts[lab->label_no]++;
3067 else if (IS_LABEL(i)) {
3069 if (lab->unremovable)
return 0;
3070 if (lab->refcnt > unref_counts[lab->label_no]) {
3071 if (i == first)
return 0;
3076 else if (IS_TRACE(i)) {
3079 else if (IS_ADJUST(i)) {
3083 }
while ((i = i->next) != 0);
3088 VALUE insn = INSN_OF(i);
3089 int pos,
len = insn_len(insn);
3090 for (pos = 0; pos <
len; ++pos) {
3091 switch (insn_op_types(insn)[pos]) {
3093 unref_destination((
INSN *)i, pos);
3102 }
while ((i != end) && (i = i->next) != 0);
3109 switch (OPERAND_AT(iobj, 0)) {
3111 ELEM_REMOVE(&iobj->link);
3114 ELEM_REMOVE(&iobj->link);
3117 iobj->insn_id = BIN(adjuststack);
3123is_frozen_putstring(
INSN *insn,
VALUE *op)
3125 if (IS_INSN_ID(insn, putstring) || IS_INSN_ID(insn, putchilledstring)) {
3126 *op = OPERAND_AT(insn, 0);
3129 else if (IS_INSN_ID(insn, putobject)) {
3130 *op = OPERAND_AT(insn, 0);
3161 INSN *niobj, *ciobj, *dup = 0;
3165 switch (INSN_OF(iobj)) {
3166 case BIN(putstring):
3167 case BIN(putchilledstring):
3173 case BIN(putobject):
3176 default:
return FALSE;
3179 ciobj = (
INSN *)get_next_insn(iobj);
3180 if (IS_INSN_ID(ciobj, jump)) {
3181 ciobj = (
INSN *)get_next_insn((
INSN*)OPERAND_AT(ciobj, 0));
3183 if (IS_INSN_ID(ciobj, dup)) {
3184 ciobj = (
INSN *)get_next_insn(dup = ciobj);
3186 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3187 niobj = (
INSN *)get_next_insn(ciobj);
3192 switch (INSN_OF(niobj)) {
3194 if (OPERAND_AT(ciobj, 0) ==
type) {
3195 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3198 case BIN(branchunless):
3199 if (OPERAND_AT(ciobj, 0) !=
type) {
3200 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3206 line = ciobj->insn_info.line_no;
3207 node_id = ciobj->insn_info.node_id;
3209 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3210 dest = (
LABEL *)niobj->link.next;
3213 dest = NEW_LABEL(line);
3214 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3217 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3219 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3226 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3227 vm_ci_flag(ci) | add,
3237 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3245#define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3253 optimize_checktype(iseq, iobj);
3255 if (IS_INSN_ID(iobj, jump)) {
3256 INSN *niobj, *diobj, *piobj;
3257 diobj = (
INSN *)get_destination_insn(iobj);
3258 niobj = (
INSN *)get_next_insn(iobj);
3260 if (diobj == niobj) {
3267 unref_destination(iobj, 0);
3268 ELEM_REMOVE(&iobj->link);
3271 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3272 IS_INSN_ID(diobj, jump) &&
3273 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3274 diobj->insn_info.events == 0) {
3285 if (replace_destination(iobj, diobj)) {
3286 remove_unreachable_chunk(iseq, iobj->link.next);
3290 else if (IS_INSN_ID(diobj, leave)) {
3303 unref_destination(iobj, 0);
3304 iobj->insn_id = BIN(leave);
3305 iobj->operand_size = 0;
3306 iobj->insn_info = diobj->insn_info;
3309 else if (IS_INSN(iobj->link.prev) &&
3310 (piobj = (
INSN *)iobj->link.prev) &&
3311 (IS_INSN_ID(piobj, branchif) ||
3312 IS_INSN_ID(piobj, branchunless))) {
3313 INSN *pdiobj = (
INSN *)get_destination_insn(piobj);
3314 if (niobj == pdiobj) {
3315 int refcnt = IS_LABEL(piobj->link.next) ?
3316 ((
LABEL *)piobj->link.next)->refcnt : 0;
3331 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3332 ? BIN(branchunless) : BIN(branchif);
3333 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3334 ELEM_REMOVE(&iobj->link);
3341 else if (diobj == pdiobj) {
3355 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3356 ELEM_REPLACE(&piobj->link, &popiobj->link);
3359 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3373 if (IS_INSN_ID(iobj, newrange)) {
3374 INSN *
const range = iobj;
3376 VALUE str_beg, str_end;
3378 if ((end = (
INSN *)get_prev_insn(range)) != 0 &&
3379 is_frozen_putstring(end, &str_end) &&
3380 (beg = (
INSN *)get_prev_insn(end)) != 0 &&
3381 is_frozen_putstring(beg, &str_beg)) {
3382 int excl =
FIX2INT(OPERAND_AT(range, 0));
3385 ELEM_REMOVE(&beg->link);
3386 ELEM_REMOVE(&end->link);
3387 range->insn_id = BIN(putobject);
3388 OPERAND_AT(range, 0) = lit_range;
3393 if (IS_INSN_ID(iobj, leave)) {
3394 remove_unreachable_chunk(iseq, iobj->link.next);
3406 if (IS_INSN_ID(iobj, duparray)) {
3408 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3409 iobj->insn_id = BIN(putobject);
3419 if (IS_INSN_ID(iobj, duparray)) {
3421 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3425 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3426 VALUE ary = iobj->operands[0];
3429 iobj->insn_id = BIN(opt_ary_freeze);
3430 iobj->operand_size = 2;
3431 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
3432 iobj->operands[0] = ary;
3433 iobj->operands[1] = (
VALUE)ci;
3445 if (IS_INSN_ID(iobj, duphash)) {
3447 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3451 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3452 VALUE hash = iobj->operands[0];
3455 iobj->insn_id = BIN(opt_hash_freeze);
3456 iobj->operand_size = 2;
3457 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
3458 iobj->operands[0] = hash;
3459 iobj->operands[1] = (
VALUE)ci;
3471 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3473 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3477 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3478 iobj->insn_id = BIN(opt_ary_freeze);
3479 iobj->operand_size = 2;
3480 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
3481 iobj->operands[0] = rb_cArray_empty_frozen;
3482 iobj->operands[1] = (
VALUE)ci;
3494 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3496 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3500 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3501 iobj->insn_id = BIN(opt_hash_freeze);
3502 iobj->operand_size = 2;
3503 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
3504 iobj->operands[0] = rb_cHash_empty_frozen;
3505 iobj->operands[1] = (
VALUE)ci;
3511 if (IS_INSN_ID(iobj, branchif) ||
3512 IS_INSN_ID(iobj, branchnil) ||
3513 IS_INSN_ID(iobj, branchunless)) {
3522 INSN *nobj = (
INSN *)get_destination_insn(iobj);
3544 int stop_optimization =
3545 ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq) &&
3546 nobj->link.type == ISEQ_ELEMENT_INSN &&
3547 nobj->insn_info.events;
3548 if (!stop_optimization) {
3549 INSN *pobj = (
INSN *)iobj->link.prev;
3552 if (!IS_INSN(&pobj->link))
3554 else if (IS_INSN_ID(pobj, dup))
3559 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3560 if (!replace_destination(iobj, nobj))
break;
3562 else if (prev_dup && IS_INSN_ID(nobj, dup) &&
3563 !!(nobj = (
INSN *)nobj->link.next) &&
3565 nobj->insn_id == iobj->insn_id) {
3581 if (!replace_destination(iobj, nobj))
break;
3609 if (prev_dup && IS_INSN(pobj->link.prev)) {
3610 pobj = (
INSN *)pobj->link.prev;
3612 if (IS_INSN_ID(pobj, putobject)) {
3613 cond = (IS_INSN_ID(iobj, branchif) ?
3614 OPERAND_AT(pobj, 0) !=
Qfalse :
3615 IS_INSN_ID(iobj, branchunless) ?
3616 OPERAND_AT(pobj, 0) ==
Qfalse :
3619 else if (IS_INSN_ID(pobj, putstring) ||
3620 IS_INSN_ID(pobj, duparray) ||
3621 IS_INSN_ID(pobj, newarray)) {
3622 cond = IS_INSN_ID(iobj, branchif);
3624 else if (IS_INSN_ID(pobj, putnil)) {
3625 cond = !IS_INSN_ID(iobj, branchif);
3628 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3629 ELEM_REMOVE(iobj->link.prev);
3631 else if (!iseq_pop_newarray(iseq, pobj)) {
3632 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3633 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3637 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3638 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3640 iobj->insn_id = BIN(jump);
3644 unref_destination(iobj, 0);
3645 ELEM_REMOVE(&iobj->link);
3650 nobj = (
INSN *)get_destination_insn(nobj);
3655 if (IS_INSN_ID(iobj, pop)) {
3663 if (IS_INSN(prev)) {
3664 enum ruby_vminsn_type previ = ((
INSN *)prev)->insn_id;
3665 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3666 previ == BIN(putself) || previ == BIN(putstring) ||
3667 previ == BIN(putchilledstring) ||
3668 previ == BIN(dup) ||
3669 previ == BIN(getlocal) ||
3670 previ == BIN(getblockparam) ||
3671 previ == BIN(getblockparamproxy) ||
3672 previ == BIN(getinstancevariable) ||
3673 previ == BIN(duparray)) {
3677 ELEM_REMOVE(&iobj->link);
3679 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (
INSN*)prev)) {
3680 ELEM_REMOVE(&iobj->link);
3682 else if (previ == BIN(concatarray)) {
3684 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3685 INSN_OF(piobj) = BIN(pop);
3687 else if (previ == BIN(concatstrings)) {
3688 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3692 ELEM_REMOVE(&iobj->link);
3693 INSN_OF(prev) = BIN(adjuststack);
3699 if (IS_INSN_ID(iobj, newarray) ||
3700 IS_INSN_ID(iobj, duparray) ||
3701 IS_INSN_ID(iobj, concatarray) ||
3702 IS_INSN_ID(iobj, splatarray) ||
3712 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3718 if (IS_INSN_ID(iobj, newarray)) {
3720 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3721 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3723 op1 = OPERAND_AT(iobj, 0);
3724 op2 = OPERAND_AT(next, 0);
3735 INSN_OF(iobj) = BIN(swap);
3736 iobj->operand_size = 0;
3745 INSN_OF(iobj) = BIN(opt_reverse);
3750 INSN_OF(iobj) = BIN(opt_reverse);
3751 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3761 for (; diff > 0; diff--) {
3762 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
3773 for (; diff < 0; diff++) {
3774 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
3781 if (IS_INSN_ID(iobj, duparray)) {
3790 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
3791 INSN_OF(iobj) = BIN(putobject);
3795 if (IS_INSN_ID(iobj, anytostring)) {
3803 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
3804 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
3809 if (IS_INSN_ID(iobj, putstring) || IS_INSN_ID(iobj, putchilledstring) ||
3817 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
3818 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
3819 INSN *next = (
INSN *)iobj->link.next;
3820 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
3821 ELEM_REMOVE(&next->link);
3823 ELEM_REMOVE(&iobj->link);
3825 if (IS_NEXT_INSN_ID(&iobj->link, toregexp)) {
3826 INSN *next = (
INSN *)iobj->link.next;
3827 if (OPERAND_AT(next, 1) ==
INT2FIX(1)) {
3828 VALUE src = OPERAND_AT(iobj, 0);
3829 int opt = (int)
FIX2LONG(OPERAND_AT(next, 0));
3830 VALUE path = rb_iseq_path(iseq);
3831 int line = iobj->insn_info.line_no;
3832 VALUE errinfo = rb_errinfo();
3833 VALUE re = rb_reg_compile(src, opt, RSTRING_PTR(path), line);
3835 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
3836 rb_set_errinfo(errinfo);
3837 COMPILE_ERROR(iseq, line,
"%" PRIsVALUE, message);
3840 ELEM_REMOVE(iobj->link.next);
3845 if (IS_INSN_ID(iobj, concatstrings)) {
3854 if (IS_INSN(next) && IS_INSN_ID(next, jump))
3855 next = get_destination_insn(jump = (
INSN *)next);
3856 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
3857 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
3858 OPERAND_AT(iobj, 0) =
INT2FIX(n);
3860 LABEL *label = ((
LABEL *)OPERAND_AT(jump, 0));
3861 if (!--label->refcnt) {
3862 ELEM_REMOVE(&label->link);
3865 label = NEW_LABEL(0);
3866 OPERAND_AT(jump, 0) = (
VALUE)label;
3869 ELEM_INSERT_NEXT(next, &label->link);
3870 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
3878 if (do_tailcallopt &&
3879 (IS_INSN_ID(iobj, send) ||
3880 IS_INSN_ID(iobj, opt_aref_with) ||
3881 IS_INSN_ID(iobj, opt_aset_with) ||
3882 IS_INSN_ID(iobj, invokesuper))) {
3891 if (iobj->link.next) {
3894 if (!IS_INSN(next)) {
3898 switch (INSN_OF(next)) {
3907 next = get_destination_insn((
INSN *)next);
3921 if (IS_INSN_ID(piobj, send) ||
3922 IS_INSN_ID(piobj, invokesuper)) {
3923 if (OPERAND_AT(piobj, 1) == 0) {
3924 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3925 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3930 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3931 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3937 if (IS_INSN_ID(iobj, dup)) {
3938 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
3949 if (IS_NEXT_INSN_ID(set1, setlocal)) {
3951 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3952 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3954 ELEM_REMOVE(&iobj->link);
3967 else if (IS_NEXT_INSN_ID(set1, dup) &&
3968 IS_NEXT_INSN_ID(set1->next, setlocal)) {
3969 set2 = set1->next->next;
3970 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3971 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3972 ELEM_REMOVE(set1->next);
3986 if (IS_INSN_ID(iobj, getlocal)) {
3988 if (IS_NEXT_INSN_ID(niobj, dup)) {
3989 niobj = niobj->next;
3991 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
3993 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
3994 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
4010 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
4011 if (IS_TRACE(iobj->link.next)) {
4012 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
4013 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
4015 if (iobj == (
INSN *)list && bf->argc == 0 && (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF)) {
4016 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4029 if (IS_INSN_ID(iobj, getblockparam)) {
4030 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4031 iobj->insn_id = BIN(getblockparamproxy);
4035 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4037 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4038 niobj = niobj->next;
4040 unsigned int set_flags = 0, unset_flags = 0;
4053 if (IS_NEXT_INSN_ID(niobj, send)) {
4054 siobj = niobj->next;
4055 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4056 unset_flags = VM_CALL_ARGS_BLOCKARG;
4071 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4072 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4073 siobj = niobj->next->next;
4074 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4079 unsigned int flags = vm_ci_flag(ci);
4080 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4081 ((
INSN*)niobj)->insn_id = BIN(putobject);
4082 OPERAND_AT(niobj, 0) = rb_hash_freeze(rb_hash_resurrect(OPERAND_AT(niobj, 0)));
4084 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4085 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4087 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4097insn_set_specialized_instruction(
rb_iseq_t *iseq,
INSN *iobj,
int insn_id)
4099 iobj->insn_id = insn_id;
4100 iobj->operand_size = insn_len(insn_id) - 1;
4103 if (insn_id == BIN(opt_neq)) {
4104 VALUE original_ci = iobj->operands[0];
4105 iobj->operand_size = 2;
4106 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
4107 iobj->operands[0] = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4108 iobj->operands[1] = original_ci;
4117 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4118 IS_INSN(iobj->link.next)) {
4122 INSN *niobj = (
INSN *)iobj->link.next;
4123 if (IS_INSN_ID(niobj, send)) {
4125 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4127 switch (vm_ci_mid(ci)) {
4129 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4132 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4135 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4140 VALUE num = iobj->operands[0];
4141 int operand_len = insn_len(BIN(opt_newarray_send)) - 1;
4142 iobj->insn_id = BIN(opt_newarray_send);
4143 iobj->operands = compile_data_calloc2(iseq, operand_len,
sizeof(
VALUE));
4144 iobj->operands[0] = num;
4145 iobj->operands[1] = method;
4146 iobj->operand_size = operand_len;
4147 ELEM_REMOVE(&niobj->link);
4152 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4154 IS_NEXT_INSN_ID(&niobj->link, send)) {
4156 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4157 VALUE num = iobj->operands[0];
4158 int operand_len = insn_len(BIN(opt_newarray_send)) - 1;
4159 iobj->insn_id = BIN(opt_newarray_send);
4160 iobj->operands = compile_data_calloc2(iseq, operand_len,
sizeof(
VALUE));
4161 iobj->operands[0] = FIXNUM_INC(num, 1);
4162 iobj->operands[1] =
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK);
4163 iobj->operand_size = operand_len;
4164 ELEM_REMOVE(&iobj->link);
4165 ELEM_REMOVE(niobj->link.next);
4166 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4172 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4174 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4175 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4178 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4179 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] ==
rb_id2sym(idBuffer))) {
4180 VALUE num = iobj->operands[0];
4181 int operand_len = insn_len(BIN(opt_newarray_send)) - 1;
4182 iobj->insn_id = BIN(opt_newarray_send);
4183 iobj->operands = compile_data_calloc2(iseq, operand_len,
sizeof(
VALUE));
4184 iobj->operands[0] = FIXNUM_INC(num, 2);
4185 iobj->operands[1] =
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER);
4186 iobj->operand_size = operand_len;
4188 ELEM_REMOVE((niobj->link.next)->next);
4190 ELEM_REMOVE(&iobj->link);
4192 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4200 if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4201 IS_INSN_ID(niobj, putobject) ||
4202 IS_INSN_ID(niobj, putself) ||
4203 IS_INSN_ID(niobj, getlocal) ||
4204 IS_INSN_ID(niobj, getinstancevariable)) &&
4205 IS_NEXT_INSN_ID(&niobj->link, send)) {
4212 sendobj = sendobj->next;
4213 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4214 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4217 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4218 VALUE num = iobj->operands[0];
4220 sendins->insn_id = BIN(opt_newarray_send);
4221 sendins->operand_size = insn_len(sendins->insn_id) - 1;
4222 sendins->operands = compile_data_calloc2(iseq, sendins->operand_size,
sizeof(
VALUE));
4223 sendins->operands[0] = FIXNUM_INC(num, 1);
4224 sendins->operands[1] =
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P);
4226 ELEM_REMOVE(&iobj->link);
4240 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4241 INSN *niobj = (
INSN *)iobj->link.next;
4242 if ((IS_INSN_ID(niobj, getlocal) ||
4243 IS_INSN_ID(niobj, getinstancevariable) ||
4244 IS_INSN_ID(niobj, putself)) &&
4245 IS_NEXT_INSN_ID(&niobj->link, send)) {
4252 sendobj = sendobj->next;
4253 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4254 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4256 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4258 VALUE ary = iobj->operands[0];
4262 sendins->insn_id = BIN(opt_duparray_send);
4263 sendins->operand_size = insn_len(sendins->insn_id) - 1;;
4264 sendins->operands = compile_data_calloc2(iseq, sendins->operand_size,
sizeof(
VALUE));
4265 sendins->operands[0] = ary;
4266 sendins->operands[1] =
rb_id2sym(idIncludeP);
4267 sendins->operands[2] =
INT2FIX(1);
4270 ELEM_REMOVE(&iobj->link);
4277 if (IS_INSN_ID(iobj, send)) {
4281#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4282 if (vm_ci_simple(ci)) {
4283 switch (vm_ci_argc(ci)) {
4285 switch (vm_ci_mid(ci)) {
4286 case idLength: SP_INSN(length);
return COMPILE_OK;
4287 case idSize: SP_INSN(size);
return COMPILE_OK;
4288 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4289 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4290 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4291 case idNot: SP_INSN(not);
return COMPILE_OK;
4295 switch (vm_ci_mid(ci)) {
4296 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4297 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4298 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4299 case idDIV: SP_INSN(div);
return COMPILE_OK;
4300 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4301 case idEq: SP_INSN(eq);
return COMPILE_OK;
4302 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4303 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4304 case idLT: SP_INSN(lt);
return COMPILE_OK;
4305 case idLE: SP_INSN(le);
return COMPILE_OK;
4306 case idGT: SP_INSN(gt);
return COMPILE_OK;
4307 case idGE: SP_INSN(ge);
return COMPILE_OK;
4308 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4309 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4310 case idAnd: SP_INSN(and);
return COMPILE_OK;
4311 case idOr: SP_INSN(or);
return COMPILE_OK;
4315 switch (vm_ci_mid(ci)) {
4316 case idASET: SP_INSN(aset);
return COMPILE_OK;
4322 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4323 iobj->insn_id = BIN(opt_send_without_block);
4324 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4335 switch (ISEQ_BODY(iseq)->
type) {
4337 case ISEQ_TYPE_EVAL:
4338 case ISEQ_TYPE_MAIN:
4340 case ISEQ_TYPE_RESCUE:
4341 case ISEQ_TYPE_ENSURE:
4353 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4354 const int do_tailcallopt = tailcallable_p(iseq) &&
4355 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4356 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4357 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4358 int rescue_level = 0;
4359 int tailcallopt = do_tailcallopt;
4361 list = FIRST_ELEMENT(anchor);
4363 int do_block_optimization = 0;
4365 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK && !ISEQ_COMPILE_DATA(iseq)->catch_except_p) {
4366 do_block_optimization = 1;
4370 if (IS_INSN(list)) {
4371 if (do_peepholeopt) {
4372 iseq_peephole_optimize(iseq, list, tailcallopt);
4375 iseq_specialized_instruction(iseq, (
INSN *)list);
4378 insn_operands_unification((
INSN *)list);
4381 if (do_block_optimization) {
4383 if (IS_INSN_ID(item, jump)) {
4384 do_block_optimization = 0;
4388 if (IS_LABEL(list)) {
4389 switch (((
LABEL *)list)->rescued) {
4390 case LABEL_RESCUE_BEG:
4392 tailcallopt = FALSE;
4394 case LABEL_RESCUE_END:
4395 if (!--rescue_level) tailcallopt = do_tailcallopt;
4402 if (do_block_optimization) {
4404 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop)) {
4411#if OPT_INSTRUCTIONS_UNIFICATION
4419 VALUE *operands = 0, *ptr = 0;
4423 for (i = 0; i < size; i++) {
4424 iobj = (
INSN *)list;
4425 argc += iobj->operand_size;
4430 ptr = operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
4435 for (i = 0; i < size; i++) {
4436 iobj = (
INSN *)list;
4437 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4438 ptr += iobj->operand_size;
4442 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4454#if OPT_INSTRUCTIONS_UNIFICATION
4460 list = FIRST_ELEMENT(anchor);
4462 if (IS_INSN(list)) {
4463 iobj = (
INSN *)list;
4465 if (unified_insns_data[
id] != 0) {
4466 const int *
const *entry = unified_insns_data[id];
4467 for (j = 1; j < (intptr_t)entry[0]; j++) {
4468 const int *unified = entry[j];
4470 for (k = 2; k < unified[1]; k++) {
4472 ((
INSN *)li)->insn_id != unified[k]) {
4479 new_unified_insn(iseq, unified[0], unified[1] - 1,
4484 niobj->link.next = li;
4503all_string_result_p(
const NODE *node)
4505 if (!node)
return FALSE;
4506 switch (nd_type(node)) {
4507 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4509 case NODE_IF:
case NODE_UNLESS:
4510 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4511 if (all_string_result_p(RNODE_IF(node)->nd_body))
4512 return all_string_result_p(RNODE_IF(node)->nd_else);
4514 case NODE_AND:
case NODE_OR:
4515 if (!RNODE_AND(node)->nd_2nd)
4516 return all_string_result_p(RNODE_AND(node)->nd_1st);
4517 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4519 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4529 const NODE *lit_node;
4537 VALUE s = rb_str_new_mutable_parser_string(str);
4539 VALUE error = rb_reg_check_preprocess(s);
4540 if (!
NIL_P(error)) {
4541 COMPILE_ERROR(args->iseq, nd_line(node),
"%" PRIsVALUE, error);
4545 if (
NIL_P(args->lit)) {
4547 args->lit_node = node;
4556flush_dstr_fragment(
struct dstr_ctxt *args)
4558 if (!
NIL_P(args->lit)) {
4560 VALUE lit = args->lit;
4562 lit = rb_fstring(lit);
4563 ADD_INSN1(args->ret, args->lit_node, putobject, lit);
4570compile_dstr_fragments_0(
struct dstr_ctxt *args,
const NODE *
const node)
4572 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4576 CHECK(append_dstr_fragment(args, node, str));
4580 const NODE *
const head = list->nd_head;
4581 if (nd_type_p(head, NODE_STR)) {
4582 CHECK(append_dstr_fragment(args, node, RNODE_STR(head)->
string));
4584 else if (nd_type_p(head, NODE_DSTR)) {
4585 CHECK(compile_dstr_fragments_0(args, head));
4588 flush_dstr_fragment(args);
4590 CHECK(COMPILE(args->ret,
"each string", head));
4602 .iseq = iseq, .ret = ret,
4603 .lit =
Qnil, .lit_node = NULL,
4604 .cnt = 0, .dregx = dregx,
4606 CHECK(compile_dstr_fragments_0(&args, node));
4607 flush_dstr_fragment(&args);
4617 while (node && nd_type_p(node, NODE_BLOCK)) {
4618 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4619 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4620 node = RNODE_BLOCK(node)->nd_next;
4623 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4632 if (!RNODE_DSTR(node)->nd_next) {
4633 VALUE lit = rb_node_dstr_string_val(node);
4634 ADD_INSN1(ret, node, putstring, lit);
4638 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, FALSE));
4639 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4648 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
4650 if (!RNODE_DREGX(node)->nd_next) {
4652 VALUE src = rb_node_dregx_string_val(node);
4653 VALUE match = rb_reg_compile(src, cflag, NULL, 0);
4654 ADD_INSN1(ret, node, putobject, match);
4660 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, TRUE));
4664 ADD_INSN(ret, node, pop);
4674 const int line = nd_line(node);
4675 LABEL *lend = NEW_LABEL(line);
4676 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4677 + VM_SVAR_FLIPFLOP_START;
4680 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4681 ADD_INSNL(ret, node, branchif, lend);
4684 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
4685 ADD_INSNL(ret, node, branchunless, else_label);
4686 ADD_INSN1(ret, node, putobject,
Qtrue);
4687 ADD_INSN1(ret, node, setspecial, key);
4689 ADD_INSNL(ret, node, jump, then_label);
4693 ADD_LABEL(ret, lend);
4694 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
4695 ADD_INSNL(ret, node, branchunless, then_label);
4696 ADD_INSN1(ret, node, putobject,
Qfalse);
4697 ADD_INSN1(ret, node, setspecial, key);
4698 ADD_INSNL(ret, node, jump, then_label);
4707#define COMPILE_SINGLE 2
4714 LABEL *label = NEW_LABEL(nd_line(cond));
4715 if (!then_label) then_label = label;
4716 else if (!else_label) else_label = label;
4718 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
4720 if (LIST_INSN_SIZE_ONE(seq)) {
4721 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
4722 if (insn->insn_id == BIN(jump) && (
LABEL *)(insn->operands[0]) == label)
4725 if (!label->refcnt) {
4726 return COMPILE_SINGLE;
4728 ADD_LABEL(seq, label);
4738 DECL_ANCHOR(ignore);
4741 switch (nd_type(cond)) {
4743 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
4744 cond = RNODE_AND(cond)->nd_2nd;
4745 if (ok == COMPILE_SINGLE) {
4746 INIT_ANCHOR(ignore);
4748 then_label = NEW_LABEL(nd_line(cond));
4752 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
4753 cond = RNODE_OR(cond)->nd_2nd;
4754 if (ok == COMPILE_SINGLE) {
4755 INIT_ANCHOR(ignore);
4757 else_label = NEW_LABEL(nd_line(cond));
4767 case NODE_IMAGINARY:
4774 ADD_INSNL(ret, cond, jump, then_label);
4779 ADD_INSNL(ret, cond, jump, else_label);
4785 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
4786 ADD_INSNL(ret, cond, jump, then_label);
4789 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
4792 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
4795 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
4799 DECL_ANCHOR(cond_seq);
4800 INIT_ANCHOR(cond_seq);
4802 CHECK(COMPILE(cond_seq,
"branch condition", cond));
4804 if (LIST_INSN_SIZE_ONE(cond_seq)) {
4805 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
4806 if (insn->insn_id == BIN(putobject)) {
4807 if (
RTEST(insn->operands[0])) {
4808 ADD_INSNL(ret, cond, jump, then_label);
4813 ADD_INSNL(ret, cond, jump, else_label);
4818 ADD_SEQ(ret, cond_seq);
4823 ADD_INSNL(ret, cond, branchunless, else_label);
4824 ADD_INSNL(ret, cond, jump, then_label);
4831keyword_node_p(
const NODE *
const node)
4833 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
4839 switch (nd_type(node)) {
4841 return rb_node_sym_string_val(node);
4843 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
4850 NODE *node = node_hash->nd_head;
4851 VALUE hash = rb_hash_new();
4852 VALUE ary = rb_ary_new();
4854 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4855 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
4856 VALUE idx = rb_hash_aref(hash, key);
4861 rb_hash_aset(hash, key,
INT2FIX(i));
4862 rb_ary_store(ary, i,
Qtrue);
4871 const NODE *
const root_node,
4879 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
4880 const NODE *node = RNODE_HASH(root_node)->nd_head;
4884 const NODE *key_node = RNODE_LIST(node)->nd_head;
4888 if (key_node && nd_type_p(key_node, NODE_SYM)) {
4893 *flag |= VM_CALL_KW_SPLAT;
4894 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4899 *flag |= VM_CALL_KW_SPLAT_MUT;
4904 node = RNODE_LIST(node)->nd_next;
4905 node = RNODE_LIST(node)->nd_next;
4909 node = RNODE_HASH(root_node)->nd_head;
4912 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
4915 VALUE *keywords = kw_arg->keywords;
4918 kw_arg->references = 0;
4919 kw_arg->keyword_len =
len;
4921 *kw_arg_ptr = kw_arg;
4923 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4924 const NODE *key_node = RNODE_LIST(node)->nd_head;
4925 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
4927 if (rb_ary_entry(key_index, i)) {
4928 keywords[j] = get_symbol_value(iseq, key_node);
4932 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
4946 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
4948 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
4951 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
4952 *kwnode_ptr = RNODE_LIST(node)->nd_head;
4955 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
4956 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
4964frozen_string_literal_p(
const rb_iseq_t *iseq)
4966 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
4970static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
4972 switch (nd_type(node)) {
4980 case NODE_IMAGINARY:
4987 return hash_key || frozen_string_literal_p(iseq);
4996 switch (nd_type(node)) {
4998 return rb_node_integer_literal_val(node);
5000 return rb_node_float_literal_val(node);
5002 return rb_node_rational_literal_val(node);
5003 case NODE_IMAGINARY:
5004 return rb_node_imaginary_literal_val(node);
5012 return rb_node_sym_string_val(node);
5014 return rb_node_regx_string_val(node);
5016 return rb_node_line_lineno_val(node);
5018 return rb_node_encoding_val(node);
5021 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
5022 VALUE lit = get_string_value(node);
5023 return rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
5026 return get_string_value(node);
5029 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
5036 const NODE *line_node = node;
5038 if (nd_type_p(node, NODE_ZLIST)) {
5040 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5045 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5048 for (; node; node = RNODE_LIST(node)->nd_next) {
5049 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5091 const int max_stack_len = 0x100;
5092 const int min_tmp_ary_len = 0x40;
5096#define FLUSH_CHUNK \
5098 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5099 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5100 first_chunk = FALSE; \
5108 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5110 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5111 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5114 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5116 VALUE ary = rb_ary_hidden_new(count);
5119 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5120 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5126 ADD_INSN1(ret, line_node, duparray, ary);
5127 first_chunk = FALSE;
5130 ADD_INSN1(ret, line_node, putobject, ary);
5131 ADD_INSN(ret, line_node, concattoarray);
5138 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5140 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5143 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5145 if (stack_len == 0 && first_chunk) {
5146 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5151 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5152 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5156 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5161 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5171static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5173 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);
5179 const NODE *line_node = node;
5181 node = RNODE_HASH(node)->nd_head;
5183 if (!node || nd_type_p(node, NODE_ZLIST)) {
5185 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5190 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5193 for (; node; node = RNODE_LIST(node)->nd_next) {
5194 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5217 const int max_stack_len = 0x100;
5218 const int min_tmp_hash_len = 0x800;
5220 int first_chunk = 1;
5221 DECL_ANCHOR(anchor);
5222 INIT_ANCHOR(anchor);
5225#define FLUSH_CHUNK() \
5227 if (first_chunk) { \
5228 APPEND_LIST(ret, anchor); \
5229 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5232 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5233 ADD_INSN(ret, line_node, swap); \
5234 APPEND_LIST(ret, anchor); \
5235 ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
5237 INIT_ANCHOR(anchor); \
5238 first_chunk = stack_len = 0; \
5245 if (static_literal_node_pair_p(node, iseq)) {
5247 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5248 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5251 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5253 VALUE ary = rb_ary_hidden_new(count);
5256 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5258 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5259 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5260 rb_ary_cat(ary, elem, 2);
5270 ADD_INSN1(ret, line_node, duphash, hash);
5274 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5275 ADD_INSN(ret, line_node, swap);
5277 ADD_INSN1(ret, line_node, putobject, hash);
5279 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5286 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5289 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5292 if (RNODE_LIST(node)->nd_head) {
5294 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5295 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5299 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5305 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5306 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5307 int first_kw = first_chunk && stack_len == 0;
5308 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5309 int only_kw = last_kw && first_kw;
5311 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5313 if (only_kw && method_call_keywords) {
5321 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5323 else if (first_kw) {
5327 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5334 if (only_kw && method_call_keywords) {
5340 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5347 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5348 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5349 else ADD_INSN(ret, line_node, swap);
5351 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5353 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5368rb_node_case_when_optimizable_literal(
const NODE *
const node)
5370 switch (nd_type(node)) {
5372 return rb_node_integer_literal_val(node);
5374 VALUE v = rb_node_float_literal_val(node);
5383 case NODE_IMAGINARY:
5392 return rb_node_sym_string_val(node);
5394 return rb_node_line_lineno_val(node);
5396 return rb_node_str_string_val(node);
5398 return rb_node_file_path_val(node);
5405 LABEL *l1,
int only_special_literals,
VALUE literals)
5408 const NODE *val = RNODE_LIST(vals)->nd_head;
5409 VALUE lit = rb_node_case_when_optimizable_literal(val);
5412 only_special_literals = 0;
5414 else if (
NIL_P(rb_hash_lookup(literals, lit))) {
5415 rb_hash_aset(literals, lit, (
VALUE)(l1) | 1);
5418 if (nd_type_p(val, NODE_STR) || nd_type_p(val, NODE_FILE)) {
5419 debugp_param(
"nd_lit", get_string_value(val));
5420 lit = get_string_value(val);
5421 ADD_INSN1(cond_seq, val, putobject, lit);
5425 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5429 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5430 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5431 ADD_INSNL(cond_seq, val, branchif, l1);
5432 vals = RNODE_LIST(vals)->nd_next;
5434 return only_special_literals;
5439 LABEL *l1,
int only_special_literals,
VALUE literals)
5441 const NODE *line_node = vals;
5443 switch (nd_type(vals)) {
5445 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5449 ADD_INSN (cond_seq, line_node, dup);
5450 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5451 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5452 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5453 ADD_INSNL(cond_seq, line_node, branchif, l1);
5456 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5457 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5460 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5461 ADD_INSN (cond_seq, line_node, dup);
5462 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5463 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5464 ADD_INSNL(cond_seq, line_node, branchif, l1);
5467 ADD_INSN (cond_seq, line_node, dup);
5468 CHECK(COMPILE(cond_seq,
"when val", vals));
5469 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5470 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5471 ADD_INSNL(cond_seq, line_node, branchif, l1);
5564 const NODE *line_node;
5579add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc,
INSN *before_insn)
5582 rb_bug(
"no masgn_state");
5591 memo->before_insn = before_insn;
5592 memo->line_node = line_node;
5593 memo->argn = state->num_args + 1;
5594 memo->num_args = argc;
5595 state->num_args += argc;
5596 memo->lhs_pos = lhs_pos;
5598 if (!state->first_memo) {
5599 state->first_memo = memo;
5602 state->last_memo->next = memo;
5604 state->last_memo = memo;
5614 switch (nd_type(node)) {
5615 case NODE_ATTRASGN: {
5617 const NODE *line_node = node;
5619 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5621 bool safenav_call =
false;
5623 iobj = (
INSN *)get_prev_insn((
INSN *)insn_element);
5625 ELEM_REMOVE(insn_element);
5626 if (!IS_INSN_ID(iobj, send)) {
5627 safenav_call =
true;
5628 iobj = (
INSN *)get_prev_insn(iobj);
5629 ELEM_INSERT_NEXT(&iobj->link, insn_element);
5631 (pre->last = iobj->link.prev)->next = 0;
5634 int argc = vm_ci_argc(ci) + 1;
5635 ci = ci_argc_set(iseq, ci, argc);
5636 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5640 ADD_INSN(lhs, line_node, swap);
5643 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5646 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (
INSN *)LAST_ELEMENT(lhs))) {
5650 iobj->link.prev = lhs->last;
5651 lhs->last->next = &iobj->link;
5652 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
5653 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5654 int argc = vm_ci_argc(ci);
5655 bool dupsplat =
false;
5656 ci = ci_argc_set(iseq, ci, argc - 1);
5657 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
5664 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
5666 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5675 int line_no = nd_line(line_node);
5676 int node_id = nd_node_id(line_node);
5679 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5680 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
5681 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5683 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
5685 if (!safenav_call) {
5686 ADD_INSN(lhs, line_node, pop);
5688 ADD_INSN(lhs, line_node, pop);
5691 for (
int i=0; i < argc; i++) {
5692 ADD_INSN(post, line_node, pop);
5697 DECL_ANCHOR(nest_rhs);
5698 INIT_ANCHOR(nest_rhs);
5699 DECL_ANCHOR(nest_lhs);
5700 INIT_ANCHOR(nest_lhs);
5702 int prev_level = state->lhs_level;
5703 bool prev_nested = state->nested;
5705 state->lhs_level = lhs_pos - 1;
5706 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5707 state->lhs_level = prev_level;
5708 state->nested = prev_nested;
5710 ADD_SEQ(lhs, nest_rhs);
5711 ADD_SEQ(lhs, nest_lhs);
5715 if (!RNODE_CDECL(node)->nd_vid) {
5719 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5722 iobj = (
INSN *)insn_element;
5725 ELEM_REMOVE(insn_element);
5726 pre->last = iobj->link.prev;
5729 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (
INSN *)LAST_ELEMENT(lhs))) {
5733 ADD_INSN(post, node, pop);
5738 DECL_ANCHOR(anchor);
5739 INIT_ANCHOR(anchor);
5740 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
5741 ELEM_REMOVE(FIRST_ELEMENT(anchor));
5742 ADD_SEQ(lhs, anchor);
5753 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
5754 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
5761 const NODE *rhsn,
const NODE *orig_lhsn)
5764 const int memsize = numberof(mem);
5766 int llen = 0, rlen = 0;
5768 const NODE *lhsn = orig_lhsn;
5770#define MEMORY(v) { \
5772 if (memindex == memsize) return 0; \
5773 for (i=0; i<memindex; i++) { \
5774 if (mem[i] == (v)) return 0; \
5776 mem[memindex++] = (v); \
5779 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
5784 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
5785 switch (nd_type(ln)) {
5790 MEMORY(get_nd_vid(ln));
5795 lhsn = RNODE_LIST(lhsn)->nd_next;
5801 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
5804 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
5806 rhsn = RNODE_LIST(rhsn)->nd_next;
5811 for (i=0; i<llen-rlen; i++) {
5812 ADD_INSN(ret, orig_lhsn, putnil);
5816 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
5823 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
5824 const NODE *splatn = RNODE_MASGN(node)->nd_args;
5825 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
5826 const NODE *lhsn_count = lhsn;
5827 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
5832 while (lhsn_count) {
5834 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
5837 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
5839 lhsn = RNODE_LIST(lhsn)->nd_next;
5843 if (nd_type_p(splatn, NODE_POSTARG)) {
5845 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
5846 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
5847 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
5849 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
5851 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
5853 if (NODE_NAMED_REST_P(restn)) {
5854 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
5857 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
5859 postn = RNODE_LIST(postn)->nd_next;
5864 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
5868 if (!state->nested) {
5869 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
5873 ADD_INSN(rhs, node, dup);
5875 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
5882 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
5884 state.lhs_level = popped ? 0 : 1;
5887 state.first_memo = NULL;
5888 state.last_memo = NULL;
5898 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
5902 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
5903 for (
int i = 0; i < memo->num_args; i++) {
5904 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
5906 tmp_memo = memo->next;
5915 if (!popped && state.num_args >= 1) {
5917 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
5927 VALUE arr = rb_ary_new();
5929 switch (nd_type(node)) {
5931 rb_ary_unshift(arr,
ID2SYM(RNODE_CONST(node)->nd_vid));
5934 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON3(node)->nd_mid));
5935 rb_ary_unshift(arr,
ID2SYM(idNULL));
5938 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON2(node)->nd_mid));
5939 node = RNODE_COLON2(node)->nd_head;
5948compile_const_prefix(
rb_iseq_t *iseq,
const NODE *
const node,
5951 switch (nd_type(node)) {
5953 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
5954 ADD_INSN1(body, node, putobject,
Qtrue);
5955 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
5958 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
5959 ADD_INSN(body, node, pop);
5960 ADD_INSN1(body, node, putobject, rb_cObject);
5961 ADD_INSN1(body, node, putobject,
Qtrue);
5962 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
5965 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
5966 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
5967 ADD_INSN1(body, node, putobject,
Qfalse);
5968 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
5971 CHECK(COMPILE(pref,
"const colon2 prefix", node));
5980 if (nd_type_p(cpath, NODE_COLON3)) {
5982 ADD_INSN1(ret, cpath, putobject, rb_cObject);
5983 return VM_DEFINECLASS_FLAG_SCOPED;
5985 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
5987 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
5988 return VM_DEFINECLASS_FLAG_SCOPED;
5992 ADD_INSN1(ret, cpath, putspecialobject,
5993 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5999private_recv_p(
const NODE *node)
6001 NODE *recv = get_nd_recv(node);
6002 if (recv && nd_type_p(recv, NODE_SELF)) {
6003 return RNODE_SELF(recv)->nd_state != 0;
6010 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore);
6013compile_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);
6020 enum defined_type expr_type = DEFINED_NOT_DEFINED;
6021 enum node_type
type;
6022 const int line = nd_line(node);
6023 const NODE *line_node = node;
6025 switch (
type = nd_type(node)) {
6029 expr_type = DEFINED_NIL;
6032 expr_type = DEFINED_SELF;
6035 expr_type = DEFINED_TRUE;
6038 expr_type = DEFINED_FALSE;
6043 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
6047 if (RNODE_LIST(vals)->nd_head) {
6048 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
6051 lfinish[1] = NEW_LABEL(line);
6053 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6055 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6068 case NODE_IMAGINARY:
6073 expr_type = DEFINED_EXPR;
6077 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6079 lfinish[1] = NEW_LABEL(line);
6081 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6082 expr_type = DEFINED_EXPR;
6088 expr_type = DEFINED_LVAR;
6091#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6093 ADD_INSN3(ret, line_node, definedivar,
6094 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6098 ADD_INSN(ret, line_node, putnil);
6099 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6100 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6104 ADD_INSN(ret, line_node, putnil);
6105 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6106 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6110 ADD_INSN(ret, line_node, putnil);
6111 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6112 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6116 lfinish[1] = NEW_LABEL(line);
6118 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6119 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6120 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6123 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6124 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6127 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6128 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6132 ADD_INSN1(ret, line_node, putobject, rb_cObject);
6133 ADD_INSN3(ret, line_node, defined,
6134 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6142 case NODE_ATTRASGN:{
6143 const int explicit_receiver =
6144 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6145 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6147 if (get_nd_args(node) || explicit_receiver) {
6149 lfinish[1] = NEW_LABEL(line);
6152 lfinish[2] = NEW_LABEL(line);
6155 if (get_nd_args(node)) {
6156 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6157 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6159 if (explicit_receiver) {
6160 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6161 switch (nd_type(get_nd_recv(node))) {
6167 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6168 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6171 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6172 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6176 ADD_INSN(ret, line_node, dup);
6178 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6179 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6182 ADD_INSN(ret, line_node, putself);
6184 ADD_INSN(ret, line_node, dup);
6186 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6187 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6193 ADD_INSN(ret, line_node, putnil);
6194 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6195 PUSH_VAL(DEFINED_YIELD));
6196 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6201 ADD_INSN(ret, line_node, putnil);
6202 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6203 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6204 PUSH_VAL(DEFINED_GVAR));
6209 ADD_INSN(ret, line_node, putnil);
6210 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6211 PUSH_VAL(DEFINED_ZSUPER));
6217 case NODE_OP_ASGN_OR:
6218 case NODE_OP_ASGN_AND:
6227 expr_type = DEFINED_ASGN;
6234 VALUE str = rb_iseq_defined_string(expr_type);
6235 ADD_INSN1(ret, line_node, putobject, str);
6238 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6245 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6246 iseq_set_exception_local_table(iseq);
6251 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore)
6254 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6256 int line = nd_line(node);
6257 LABEL *lstart = NEW_LABEL(line);
6258 LABEL *lend = NEW_LABEL(line);
6261 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6262 rescue = NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc,
6264 ISEQ_BODY(iseq)->location.label),
6265 ISEQ_TYPE_RESCUE, 0);
6266 lstart->rescued = LABEL_RESCUE_BEG;
6267 lend->rescued = LABEL_RESCUE_END;
6268 APPEND_LABEL(ret, lcur, lstart);
6269 ADD_LABEL(ret, lend);
6271 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6279 const int line = nd_line(node);
6280 const NODE *line_node = node;
6281 if (!RNODE_DEFINED(node)->nd_head) {
6282 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6283 ADD_INSN1(ret, line_node, putobject, str);
6288 lfinish[0] = NEW_LABEL(line);
6291 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6293 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6294 ADD_INSN(ret, line_node, swap);
6296 ADD_LABEL(ret, lfinish[2]);
6298 ADD_INSN(ret, line_node, pop);
6299 ADD_LABEL(ret, lfinish[1]);
6301 ADD_LABEL(ret, lfinish[0]);
6307make_name_for_block(
const rb_iseq_t *orig_iseq)
6312 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6313 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6314 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6317 iseq = ISEQ_BODY(iseq)->parent_iseq;
6322 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6325 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6334 enl->ensure_node = node;
6335 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6337 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6347 while (erange->next != 0) {
6348 erange = erange->next;
6352 ne->end = erange->end;
6353 erange->end = lstart;
6359can_add_ensure_iseq(
const rb_iseq_t *iseq)
6362 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
6364 if (e->ensure_node)
return false;
6377 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6379 DECL_ANCHOR(ensure);
6381 INIT_ANCHOR(ensure);
6383 if (enlp->erange != NULL) {
6384 DECL_ANCHOR(ensure_part);
6385 LABEL *lstart = NEW_LABEL(0);
6386 LABEL *lend = NEW_LABEL(0);
6387 INIT_ANCHOR(ensure_part);
6389 add_ensure_range(iseq, enlp->erange, lstart, lend);
6391 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6392 ADD_LABEL(ensure_part, lstart);
6393 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6394 ADD_LABEL(ensure_part, lend);
6395 ADD_SEQ(ensure, ensure_part);
6404 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6405 ADD_SEQ(ret, ensure);
6410check_keyword(
const NODE *node)
6414 if (nd_type_p(node, NODE_LIST)) {
6415 while (RNODE_LIST(node)->nd_next) {
6416 node = RNODE_LIST(node)->nd_next;
6418 node = RNODE_LIST(node)->nd_head;
6421 return keyword_node_p(node);
6426keyword_node_single_splat_p(
NODE *kwnode)
6430 NODE *node = RNODE_HASH(kwnode)->nd_head;
6431 return RNODE_LIST(node)->nd_head == NULL &&
6432 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6437 NODE *kwnode,
unsigned int *flag_ptr)
6439 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6440 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6441 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6442 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6443 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6446#define SPLATARRAY_FALSE 0
6447#define SPLATARRAY_TRUE 1
6448#define DUP_SINGLE_KW_SPLAT 2
6452 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6454 if (!argn)
return 0;
6456 NODE *kwnode = NULL;
6458 switch (nd_type(argn)) {
6461 int len = compile_args(iseq, args, argn, &kwnode);
6462 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6465 if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6469 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6470 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6473 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6482 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6483 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6484 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6485 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6486 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6489 case NODE_ARGSCAT: {
6490 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6491 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6492 bool args_pushed =
false;
6494 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6495 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6496 if (kwnode) rest_len--;
6497 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6501 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6502 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6505 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6506 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6507 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6510 else if (!args_pushed) {
6511 ADD_INSN(args, argn, concattoarray);
6517 *flag_ptr |= VM_CALL_KW_SPLAT;
6518 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6524 case NODE_ARGSPUSH: {
6525 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6526 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6528 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6529 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6530 if (kwnode) rest_len--;
6531 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6532 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6535 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6536 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6539 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6540 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6546 *flag_ptr |= VM_CALL_KW_SPLAT;
6547 if (!keyword_node_single_splat_p(kwnode)) {
6548 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6549 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6551 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
6552 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6555 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6563 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
6569setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
6571 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
6572 *flag |= VM_CALL_ARGS_SPLAT_MUT;
6577setup_args_dup_rest_p(
const NODE *argn)
6579 switch(nd_type(argn)) {
6590 case NODE_IMAGINARY:
6603 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
6614 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
6617 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
6618 RNODE_BLOCK_PASS(argn)->nd_head : argn;
6621 switch(nd_type(check_arg)) {
6624 dup_rest = SPLATARRAY_FALSE;
6628 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
6630 case(NODE_ARGSPUSH):
6632 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
6633 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
6634 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
6635 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
6636 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
6638 if (dup_rest == SPLATARRAY_FALSE) {
6640 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
6642 NODE *key_node = RNODE_LIST(node)->nd_head;
6643 if (key_node && setup_args_dup_rest_p(key_node)) {
6644 dup_rest = SPLATARRAY_TRUE;
6648 node = RNODE_LIST(node)->nd_next;
6649 NODE *value_node = RNODE_LIST(node)->nd_head;
6650 if (setup_args_dup_rest_p(value_node)) {
6651 dup_rest = SPLATARRAY_TRUE;
6655 node = RNODE_LIST(node)->nd_next;
6664 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
6666 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
6669 initial_dup_rest = dup_rest;
6671 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
6672 DECL_ANCHOR(arg_block);
6673 INIT_ANCHOR(arg_block);
6675 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
6676 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
6678 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
6679 const NODE * arg_node =
6680 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
6687 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
6688 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
6691 *flag |= VM_CALL_FORWARDING;
6693 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
6694 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6698 *flag |= VM_CALL_ARGS_BLOCKARG;
6700 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
6703 if (LIST_INSN_SIZE_ONE(arg_block)) {
6705 if (IS_INSN(elem)) {
6707 if (iobj->insn_id == BIN(getblockparam)) {
6708 iobj->insn_id = BIN(getblockparamproxy);
6712 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
6713 ADD_SEQ(args, arg_block);
6716 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
6718 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6725 const NODE *body = ptr;
6726 int line = nd_line(body);
6728 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
6730 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6731 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
6733 iseq_set_local_table(iseq, 0, 0);
6741 int line = nd_line(node);
6742 const NODE *line_node = node;
6743 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
6745#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
6746 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
6750 ADD_INSN(ret, line_node, dup);
6751 ADD_INSNL(ret, line_node, branchunless, fail_label);
6753 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6755 if (RNODE_BLOCK(vars)->nd_next) {
6756 ADD_INSN(ret, line_node, dup);
6759 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6761 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
6764#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
6765 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
6770 ADD_INSNL(nom, line_node, jump, end_label);
6771 ADD_LABEL(nom, fail_label);
6773 ADD_INSN(nom, line_node, pop);
6774 ADD_INSN(nom, line_node, putnil);
6776 ADD_LABEL(nom, end_label);
6777 (nom->last->next = cap->link.next)->prev = nom->last;
6778 (cap->link.next = nom->anchor.next)->prev = &cap->link;
6783 ADD_INSNL(ret, line_node, jump, end_label);
6784 ADD_LABEL(ret, fail_label);
6785 ADD_INSN(ret, line_node, pop);
6786 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6788 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6790 ((
INSN*)last)->insn_id = BIN(putnil);
6791 ((
INSN*)last)->operand_size = 0;
6793 ADD_LABEL(ret, end_label);
6797optimizable_range_item_p(
const NODE *n)
6799 if (!n)
return FALSE;
6800 switch (nd_type(n)) {
6813optimized_range_item(
const NODE *n)
6815 switch (nd_type(n)) {
6817 return rb_node_line_lineno_val(n);
6819 return rb_node_integer_literal_val(n);
6821 return rb_node_float_literal_val(n);
6823 return rb_node_rational_literal_val(n);
6824 case NODE_IMAGINARY:
6825 return rb_node_imaginary_literal_val(n);
6829 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
6836 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
6837 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
6839 const int line = nd_line(node);
6840 const NODE *line_node = node;
6841 DECL_ANCHOR(cond_seq);
6842 LABEL *then_label, *else_label, *end_label;
6845 INIT_ANCHOR(cond_seq);
6846 then_label = NEW_LABEL(line);
6847 else_label = NEW_LABEL(line);
6850 NODE *cond = RNODE_IF(node)->nd_cond;
6851 if (nd_type(cond) == NODE_BLOCK) {
6852 cond = RNODE_BLOCK(cond)->nd_head;
6855 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
6856 ADD_SEQ(ret, cond_seq);
6858 if (then_label->refcnt && else_label->refcnt) {
6859 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
6862 if (then_label->refcnt) {
6863 ADD_LABEL(ret, then_label);
6865 DECL_ANCHOR(then_seq);
6866 INIT_ANCHOR(then_seq);
6867 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
6869 if (else_label->refcnt) {
6870 const NODE *
const coverage_node = node_body ? node_body : node;
6871 add_trace_branch_coverage(
6874 nd_code_loc(coverage_node),
6875 nd_node_id(coverage_node),
6877 type == NODE_IF ?
"then" :
"else",
6879 end_label = NEW_LABEL(line);
6880 ADD_INSNL(then_seq, line_node, jump, end_label);
6882 ADD_INSN(then_seq, line_node, pop);
6885 ADD_SEQ(ret, then_seq);
6888 if (else_label->refcnt) {
6889 ADD_LABEL(ret, else_label);
6891 DECL_ANCHOR(else_seq);
6892 INIT_ANCHOR(else_seq);
6893 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
6895 if (then_label->refcnt) {
6896 const NODE *
const coverage_node = node_else ? node_else : node;
6897 add_trace_branch_coverage(
6900 nd_code_loc(coverage_node),
6901 nd_node_id(coverage_node),
6903 type == NODE_IF ?
"else" :
"then",
6906 ADD_SEQ(ret, else_seq);
6910 ADD_LABEL(ret, end_label);
6920 const NODE *node = orig_node;
6921 LABEL *endlabel, *elselabel;
6923 DECL_ANCHOR(body_seq);
6924 DECL_ANCHOR(cond_seq);
6925 int only_special_literals = 1;
6926 VALUE literals = rb_hash_new();
6928 enum node_type
type;
6929 const NODE *line_node;
6934 INIT_ANCHOR(body_seq);
6935 INIT_ANCHOR(cond_seq);
6937 RHASH_TBL_RAW(literals)->type = &cdhash_type;
6939 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
6941 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
6943 node = RNODE_CASE(node)->nd_body;
6944 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
6945 type = nd_type(node);
6946 line = nd_line(node);
6949 endlabel = NEW_LABEL(line);
6950 elselabel = NEW_LABEL(line);
6954 while (
type == NODE_WHEN) {
6957 l1 = NEW_LABEL(line);
6958 ADD_LABEL(body_seq, l1);
6959 ADD_INSN(body_seq, line_node, pop);
6961 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
6962 add_trace_branch_coverage(
6965 nd_code_loc(coverage_node),
6966 nd_node_id(coverage_node),
6971 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
6972 ADD_INSNL(body_seq, line_node, jump, endlabel);
6974 vals = RNODE_WHEN(node)->nd_head;
6976 switch (nd_type(vals)) {
6978 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
6979 if (only_special_literals < 0)
return COMPILE_NG;
6984 only_special_literals = 0;
6985 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
6988 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
6992 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
6995 node = RNODE_WHEN(node)->nd_next;
6999 type = nd_type(node);
7000 line = nd_line(node);
7005 ADD_LABEL(cond_seq, elselabel);
7006 ADD_INSN(cond_seq, line_node, pop);
7007 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
7008 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7009 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7012 debugs(
"== else (implicit)\n");
7013 ADD_LABEL(cond_seq, elselabel);
7014 ADD_INSN(cond_seq, orig_node, pop);
7015 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
7017 ADD_INSN(cond_seq, orig_node, putnil);
7019 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7022 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7023 ADD_INSN(ret, orig_node, dup);
7024 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
7026 LABEL_REF(elselabel);
7029 ADD_SEQ(ret, cond_seq);
7030 ADD_SEQ(ret, body_seq);
7031 ADD_LABEL(ret, endlabel);
7040 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
7042 DECL_ANCHOR(body_seq);
7046 branches = decl_branch_base(iseq, PTR2NUM(orig_node), nd_code_loc(orig_node),
"case");
7048 INIT_ANCHOR(body_seq);
7049 endlabel = NEW_LABEL(nd_line(node));
7051 while (node && nd_type_p(node, NODE_WHEN)) {
7052 const int line = nd_line(node);
7053 LABEL *l1 = NEW_LABEL(line);
7054 ADD_LABEL(body_seq, l1);
7056 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7057 add_trace_branch_coverage(
7060 nd_code_loc(coverage_node),
7061 nd_node_id(coverage_node),
7066 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7067 ADD_INSNL(body_seq, node, jump, endlabel);
7069 vals = RNODE_WHEN(node)->nd_head;
7071 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7073 switch (nd_type(vals)) {
7077 val = RNODE_LIST(vals)->nd_head;
7078 lnext = NEW_LABEL(nd_line(val));
7079 debug_compile(
"== when2\n", (
void)0);
7080 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7081 ADD_LABEL(ret, lnext);
7082 vals = RNODE_LIST(vals)->nd_next;
7088 ADD_INSN(ret, vals, putnil);
7089 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7090 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7091 ADD_INSNL(ret, vals, branchif, l1);
7094 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7096 node = RNODE_WHEN(node)->nd_next;
7099 const NODE *
const coverage_node = node ? node : orig_node;
7100 add_trace_branch_coverage(
7103 nd_code_loc(coverage_node),
7104 nd_node_id(coverage_node),
7108 CHECK(COMPILE_(ret,
"else", node, popped));
7109 ADD_INSNL(ret, orig_node, jump, endlabel);
7111 ADD_SEQ(ret, body_seq);
7112 ADD_LABEL(ret, endlabel);
7116static 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);
7118static 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);
7119static 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);
7120static int iseq_compile_pattern_set_general_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7121static 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);
7122static int iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7124#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7125#define CASE3_BI_OFFSET_ERROR_STRING 1
7126#define CASE3_BI_OFFSET_KEY_ERROR_P 2
7127#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7128#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7131iseq_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)
7133 const int line = nd_line(node);
7134 const NODE *line_node = node;
7136 switch (nd_type(node)) {
7190 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7191 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7192 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7194 const int min_argc = pre_args_num + post_args_num;
7195 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7196 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7198 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7200 match_failed = NEW_LABEL(line);
7201 type_error = NEW_LABEL(line);
7202 deconstruct = NEW_LABEL(line);
7203 deconstructed = NEW_LABEL(line);
7206 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7207 ADD_INSN(ret, line_node, swap);
7213 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7215 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7217 ADD_INSN(ret, line_node, dup);
7218 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7219 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7220 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7221 if (in_single_pattern) {
7222 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7223 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7224 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7225 INT2FIX(min_argc), base_index + 1 ));
7227 ADD_INSNL(ret, line_node, branchunless, match_failed);
7229 for (i = 0; i < pre_args_num; i++) {
7230 ADD_INSN(ret, line_node, dup);
7231 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7232 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7233 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7234 args = RNODE_LIST(args)->nd_next;
7237 if (RNODE_ARYPTN(node)->rest_arg) {
7238 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7239 ADD_INSN(ret, line_node, dup);
7240 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7241 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7242 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7243 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7244 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7245 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7246 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7248 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7251 if (post_args_num > 0) {
7252 ADD_INSN(ret, line_node, dup);
7253 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7254 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7255 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7256 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7257 ADD_INSN(ret, line_node, pop);
7262 args = RNODE_ARYPTN(node)->post_args;
7263 for (i = 0; i < post_args_num; i++) {
7264 ADD_INSN(ret, line_node, dup);
7266 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7267 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7268 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7270 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7271 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7272 args = RNODE_LIST(args)->nd_next;
7275 ADD_INSN(ret, line_node, pop);
7277 ADD_INSN(ret, line_node, pop);
7279 ADD_INSNL(ret, line_node, jump, matched);
7280 ADD_INSN(ret, line_node, putnil);
7282 ADD_INSN(ret, line_node, putnil);
7285 ADD_LABEL(ret, type_error);
7286 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7288 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7289 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7290 ADD_INSN(ret, line_node, pop);
7292 ADD_LABEL(ret, match_failed);
7293 ADD_INSN(ret, line_node, pop);
7295 ADD_INSN(ret, line_node, pop);
7297 ADD_INSNL(ret, line_node, jump, unmatched);
7350 const NODE *args = RNODE_FNDPTN(node)->args;
7351 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7353 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7354 match_failed = NEW_LABEL(line);
7355 type_error = NEW_LABEL(line);
7356 deconstruct = NEW_LABEL(line);
7357 deconstructed = NEW_LABEL(line);
7359 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7361 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7363 ADD_INSN(ret, line_node, dup);
7364 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7365 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7366 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7367 if (in_single_pattern) {
7368 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 ));
7370 ADD_INSNL(ret, line_node, branchunless, match_failed);
7373 LABEL *while_begin = NEW_LABEL(nd_line(node));
7374 LABEL *next_loop = NEW_LABEL(nd_line(node));
7375 LABEL *find_succeeded = NEW_LABEL(line);
7376 LABEL *find_failed = NEW_LABEL(nd_line(node));
7379 ADD_INSN(ret, line_node, dup);
7380 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7382 ADD_INSN(ret, line_node, dup);
7383 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7384 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7386 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7388 ADD_LABEL(ret, while_begin);
7390 ADD_INSN(ret, line_node, dup);
7391 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7392 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7393 ADD_INSNL(ret, line_node, branchunless, find_failed);
7395 for (j = 0; j < args_num; j++) {
7396 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7397 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7399 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7400 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7402 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7404 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7405 args = RNODE_LIST(args)->nd_next;
7408 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7409 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7410 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7411 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7412 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7413 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));
7415 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7416 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7417 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7418 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7419 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7420 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7421 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7422 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));
7424 ADD_INSNL(ret, line_node, jump, find_succeeded);
7426 ADD_LABEL(ret, next_loop);
7427 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7428 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7429 ADD_INSNL(ret, line_node, jump, while_begin);
7431 ADD_LABEL(ret, find_failed);
7432 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7433 if (in_single_pattern) {
7434 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7435 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7436 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7437 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7438 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7440 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7441 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7443 ADD_INSN(ret, line_node, pop);
7444 ADD_INSN(ret, line_node, pop);
7446 ADD_INSNL(ret, line_node, jump, match_failed);
7447 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7449 ADD_LABEL(ret, find_succeeded);
7450 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7453 ADD_INSN(ret, line_node, pop);
7454 ADD_INSNL(ret, line_node, jump, matched);
7455 ADD_INSN(ret, line_node, putnil);
7457 ADD_LABEL(ret, type_error);
7458 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7460 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7461 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7462 ADD_INSN(ret, line_node, pop);
7464 ADD_LABEL(ret, match_failed);
7465 ADD_INSN(ret, line_node, pop);
7466 ADD_INSNL(ret, line_node, jump, unmatched);
7530 LABEL *match_failed, *type_error;
7533 match_failed = NEW_LABEL(line);
7534 type_error = NEW_LABEL(line);
7536 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
7537 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7538 keys = rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
7540 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
7541 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
7545 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7547 ADD_INSN(ret, line_node, dup);
7548 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
7549 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7550 if (in_single_pattern) {
7551 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
7553 ADD_INSNL(ret, line_node, branchunless, match_failed);
7556 ADD_INSN(ret, line_node, putnil);
7559 ADD_INSN1(ret, line_node, duparray, keys);
7562 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
7564 ADD_INSN(ret, line_node, dup);
7566 ADD_INSNL(ret, line_node, branchunless, type_error);
7568 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7569 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
7572 if (RNODE_HSHPTN(node)->nd_pkwargs) {
7576 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7578 DECL_ANCHOR(match_values);
7579 INIT_ANCHOR(match_values);
7580 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
7581 for (i = 0; i < keys_num; i++) {
7582 NODE *key_node = RNODE_LIST(args)->nd_head;
7583 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
7584 VALUE key = get_symbol_value(iseq, key_node);
7586 ADD_INSN(ret, line_node, dup);
7587 ADD_INSN1(ret, line_node, putobject, key);
7588 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
7589 if (in_single_pattern) {
7590 LABEL *match_succeeded;
7591 match_succeeded = NEW_LABEL(line);
7593 ADD_INSN(ret, line_node, dup);
7594 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7596 ADD_INSN1(ret, line_node, putobject,
rb_str_freeze(rb_sprintf(
"key not found: %+"PRIsVALUE, key)));
7597 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
7598 ADD_INSN1(ret, line_node, putobject,
Qtrue);
7599 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
7600 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7601 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
7602 ADD_INSN1(ret, line_node, putobject, key);
7603 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
7605 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
7607 ADD_LABEL(ret, match_succeeded);
7609 ADD_INSNL(ret, line_node, branchunless, match_failed);
7611 ADD_INSN(match_values, line_node, dup);
7612 ADD_INSN1(match_values, line_node, putobject, key);
7613 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
7614 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7615 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
7617 ADD_SEQ(ret, match_values);
7621 ADD_INSN(ret, line_node, dup);
7622 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7623 if (in_single_pattern) {
7624 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
7626 ADD_INSNL(ret, line_node, branchunless, match_failed);
7629 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7630 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
7631 ADD_INSN(ret, line_node, dup);
7632 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7633 if (in_single_pattern) {
7634 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
7636 ADD_INSNL(ret, line_node, branchunless, match_failed);
7639 ADD_INSN(ret, line_node, dup);
7640 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7644 ADD_INSN(ret, line_node, pop);
7645 ADD_INSNL(ret, line_node, jump, matched);
7646 ADD_INSN(ret, line_node, putnil);
7648 ADD_LABEL(ret, type_error);
7649 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7651 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
7652 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7653 ADD_INSN(ret, line_node, pop);
7655 ADD_LABEL(ret, match_failed);
7656 ADD_INSN(ret, line_node, pop);
7657 ADD_INSNL(ret, line_node, jump, unmatched);
7666 case NODE_IMAGINARY:
7694 CHECK(COMPILE(ret,
"case in literal", node));
7695 if (in_single_pattern) {
7696 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7698 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7699 if (in_single_pattern) {
7700 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
7702 ADD_INSNL(ret, line_node, branchif, matched);
7703 ADD_INSNL(ret, line_node, jump, unmatched);
7707 ID id = RNODE_LASGN(node)->nd_vid;
7708 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
7710 if (in_alt_pattern) {
7711 const char *name = rb_id2name(
id);
7712 if (name && strlen(name) > 0 && name[0] !=
'_') {
7713 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7719 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
7720 ADD_INSNL(ret, line_node, jump, matched);
7725 ID id = RNODE_DASGN(node)->nd_vid;
7727 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
7729 if (in_alt_pattern) {
7730 const char *name = rb_id2name(
id);
7731 if (name && strlen(name) > 0 && name[0] !=
'_') {
7732 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7739 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
7743 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
7744 ADD_INSNL(ret, line_node, jump, matched);
7749 LABEL *match_failed;
7750 match_failed = unmatched;
7751 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7752 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
7753 if (in_single_pattern) {
7754 LABEL *match_succeeded;
7755 match_succeeded = NEW_LABEL(line);
7757 ADD_INSN(ret, line_node, dup);
7758 if (nd_type_p(node, NODE_IF)) {
7759 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7762 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
7765 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
7766 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7767 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7768 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7770 ADD_INSN(ret, line_node, pop);
7771 ADD_INSN(ret, line_node, pop);
7773 ADD_LABEL(ret, match_succeeded);
7775 if (nd_type_p(node, NODE_IF)) {
7776 ADD_INSNL(ret, line_node, branchunless, match_failed);
7779 ADD_INSNL(ret, line_node, branchif, match_failed);
7781 ADD_INSNL(ret, line_node, jump, matched);
7786 LABEL *match_failed;
7787 match_failed = NEW_LABEL(line);
7789 n = RNODE_HASH(node)->nd_head;
7790 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
7791 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7795 ADD_INSN(ret, line_node, dup);
7796 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));
7797 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));
7798 ADD_INSN(ret, line_node, putnil);
7800 ADD_LABEL(ret, match_failed);
7801 ADD_INSN(ret, line_node, pop);
7802 ADD_INSNL(ret, line_node, jump, unmatched);
7806 LABEL *match_succeeded, *fin;
7807 match_succeeded = NEW_LABEL(line);
7808 fin = NEW_LABEL(line);
7810 ADD_INSN(ret, line_node, dup);
7811 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));
7812 ADD_LABEL(ret, match_succeeded);
7813 ADD_INSN(ret, line_node, pop);
7814 ADD_INSNL(ret, line_node, jump, matched);
7815 ADD_INSN(ret, line_node, putnil);
7816 ADD_LABEL(ret, fin);
7817 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
7821 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
7827iseq_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)
7829 LABEL *fin = NEW_LABEL(nd_line(node));
7830 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7831 ADD_LABEL(ret, fin);
7836iseq_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)
7838 const NODE *line_node = node;
7840 if (RNODE_ARYPTN(node)->nd_pconst) {
7841 ADD_INSN(ret, line_node, dup);
7842 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
7843 if (in_single_pattern) {
7844 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7846 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7847 if (in_single_pattern) {
7848 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
7850 ADD_INSNL(ret, line_node, branchunless, match_failed);
7857iseq_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)
7859 const NODE *line_node = node;
7863 if (use_deconstructed_cache) {
7865 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7866 ADD_INSNL(ret, line_node, branchnil, deconstruct);
7869 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7870 ADD_INSNL(ret, line_node, branchunless, match_failed);
7873 ADD_INSN(ret, line_node, pop);
7874 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
7875 ADD_INSNL(ret, line_node, jump, deconstructed);
7878 ADD_INSNL(ret, line_node, jump, deconstruct);
7881 ADD_LABEL(ret, deconstruct);
7882 ADD_INSN(ret, line_node, dup);
7883 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
7884 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7887 if (use_deconstructed_cache) {
7888 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
7891 if (in_single_pattern) {
7892 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
7895 ADD_INSNL(ret, line_node, branchunless, match_failed);
7897 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
7900 if (use_deconstructed_cache) {
7901 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7904 ADD_INSN(ret, line_node, dup);
7906 ADD_INSNL(ret, line_node, branchunless, type_error);
7908 ADD_LABEL(ret, deconstructed);
7924 const int line = nd_line(node);
7925 const NODE *line_node = node;
7926 LABEL *match_succeeded = NEW_LABEL(line);
7928 ADD_INSN(ret, line_node, dup);
7929 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7931 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7932 ADD_INSN1(ret, line_node, putobject, errmsg);
7933 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7934 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7935 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7937 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7938 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7940 ADD_INSN(ret, line_node, pop);
7941 ADD_INSN(ret, line_node, pop);
7942 ADD_LABEL(ret, match_succeeded);
7958 const int line = nd_line(node);
7959 const NODE *line_node = node;
7960 LABEL *match_succeeded = NEW_LABEL(line);
7962 ADD_INSN(ret, line_node, dup);
7963 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7965 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7966 ADD_INSN1(ret, line_node, putobject, errmsg);
7967 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7968 ADD_INSN(ret, line_node, dup);
7969 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7970 ADD_INSN1(ret, line_node, putobject, pattern_length);
7971 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
7972 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7974 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7975 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
7977 ADD_INSN(ret, line_node, pop);
7978 ADD_INSN(ret, line_node, pop);
7979 ADD_LABEL(ret, match_succeeded);
7985iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
7995 const int line = nd_line(node);
7996 const NODE *line_node = node;
7997 LABEL *match_succeeded = NEW_LABEL(line);
7999 ADD_INSN(ret, line_node, dup);
8000 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8002 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8003 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
8004 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8005 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
8006 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
8007 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8009 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8010 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8012 ADD_INSN(ret, line_node, pop);
8013 ADD_INSN(ret, line_node, pop);
8015 ADD_LABEL(ret, match_succeeded);
8016 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
8017 ADD_INSN(ret, line_node, pop);
8018 ADD_INSN(ret, line_node, pop);
8026 const NODE *pattern;
8027 const NODE *node = orig_node;
8028 LABEL *endlabel, *elselabel;
8030 DECL_ANCHOR(body_seq);
8031 DECL_ANCHOR(cond_seq);
8033 enum node_type
type;
8034 const NODE *line_node;
8037 bool single_pattern;
8040 INIT_ANCHOR(body_seq);
8041 INIT_ANCHOR(cond_seq);
8043 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
8045 node = RNODE_CASE3(node)->nd_body;
8046 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
8047 type = nd_type(node);
8048 line = nd_line(node);
8050 single_pattern = !RNODE_IN(node)->nd_next;
8052 endlabel = NEW_LABEL(line);
8053 elselabel = NEW_LABEL(line);
8055 if (single_pattern) {
8057 ADD_INSN(head, line_node, putnil);
8058 ADD_INSN(head, line_node, putnil);
8059 ADD_INSN1(head, line_node, putobject,
Qfalse);
8060 ADD_INSN(head, line_node, putnil);
8062 ADD_INSN(head, line_node, putnil);
8064 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8068 while (
type == NODE_IN) {
8072 ADD_INSN(body_seq, line_node, putnil);
8074 l1 = NEW_LABEL(line);
8075 ADD_LABEL(body_seq, l1);
8076 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8078 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8079 add_trace_branch_coverage(
8082 nd_code_loc(coverage_node),
8083 nd_node_id(coverage_node),
8088 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8089 ADD_INSNL(body_seq, line_node, jump, endlabel);
8091 pattern = RNODE_IN(node)->nd_head;
8093 int pat_line = nd_line(pattern);
8094 LABEL *next_pat = NEW_LABEL(pat_line);
8095 ADD_INSN (cond_seq, pattern, dup);
8097 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8098 ADD_LABEL(cond_seq, next_pat);
8099 LABEL_UNREMOVABLE(next_pat);
8102 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8106 node = RNODE_IN(node)->nd_next;
8110 type = nd_type(node);
8111 line = nd_line(node);
8116 ADD_LABEL(cond_seq, elselabel);
8117 ADD_INSN(cond_seq, line_node, pop);
8118 ADD_INSN(cond_seq, line_node, pop);
8119 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8120 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8121 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8122 ADD_INSN(cond_seq, line_node, putnil);
8124 ADD_INSN(cond_seq, line_node, putnil);
8128 debugs(
"== else (implicit)\n");
8129 ADD_LABEL(cond_seq, elselabel);
8130 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8131 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8133 if (single_pattern) {
8141 LABEL *key_error, *fin;
8144 key_error = NEW_LABEL(line);
8145 fin = NEW_LABEL(line);
8148 kw_arg->references = 0;
8149 kw_arg->keyword_len = 2;
8150 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
8151 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
8153 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8154 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8156 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8157 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8158 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8159 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8160 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8161 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8162 ADD_INSNL(cond_seq, orig_node, jump, fin);
8164 ADD_LABEL(cond_seq, key_error);
8166 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8167 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8168 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8169 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8170 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8171 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8172 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8173 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
8174 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8176 ADD_LABEL(cond_seq, fin);
8180 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8181 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8183 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8185 ADD_INSN(cond_seq, orig_node, putnil);
8187 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8188 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8190 ADD_INSN(cond_seq, line_node, putnil);
8194 ADD_SEQ(ret, cond_seq);
8195 ADD_SEQ(ret, body_seq);
8196 ADD_LABEL(ret, endlabel);
8200#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8201#undef CASE3_BI_OFFSET_ERROR_STRING
8202#undef CASE3_BI_OFFSET_KEY_ERROR_P
8203#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8204#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8209 const int line = (int)nd_line(node);
8210 const NODE *line_node = node;
8212 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8213 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8214 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8215 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8220 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8221 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8222 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8223 LABEL *end_label = NEW_LABEL(line);
8224 LABEL *adjust_label = NEW_LABEL(line);
8226 LABEL *next_catch_label = NEW_LABEL(line);
8227 LABEL *tmp_label = NULL;
8229 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8230 push_ensure_entry(iseq, &enl, NULL, NULL);
8232 if (RNODE_WHILE(node)->nd_state == 1) {
8233 ADD_INSNL(ret, line_node, jump, next_label);
8236 tmp_label = NEW_LABEL(line);
8237 ADD_INSNL(ret, line_node, jump, tmp_label);
8239 ADD_LABEL(ret, adjust_label);
8240 ADD_INSN(ret, line_node, putnil);
8241 ADD_LABEL(ret, next_catch_label);
8242 ADD_INSN(ret, line_node, pop);
8243 ADD_INSNL(ret, line_node, jump, next_label);
8244 if (tmp_label) ADD_LABEL(ret, tmp_label);
8246 ADD_LABEL(ret, redo_label);
8247 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8249 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8250 add_trace_branch_coverage(
8253 nd_code_loc(coverage_node),
8254 nd_node_id(coverage_node),
8259 CHECK(COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body));
8260 ADD_LABEL(ret, next_label);
8262 if (
type == NODE_WHILE) {
8263 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8264 redo_label, end_label));
8268 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8269 end_label, redo_label));
8272 ADD_LABEL(ret, end_label);
8273 ADD_ADJUST_RESTORE(ret, adjust_label);
8275 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8277 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8281 ADD_INSN(ret, line_node, putnil);
8284 ADD_LABEL(ret, break_label);
8287 ADD_INSN(ret, line_node, pop);
8290 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8292 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8294 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8295 ISEQ_COMPILE_DATA(iseq)->redo_label);
8297 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8298 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8299 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8300 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8301 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
8308 const int line = nd_line(node);
8309 const NODE *line_node = node;
8310 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8311 LABEL *retry_label = NEW_LABEL(line);
8312 LABEL *retry_end_l = NEW_LABEL(line);
8315 ADD_LABEL(ret, retry_label);
8316 if (nd_type_p(node, NODE_FOR)) {
8317 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8319 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8320 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8321 ISEQ_TYPE_BLOCK, line);
8322 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8325 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8326 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8327 ISEQ_TYPE_BLOCK, line);
8328 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8342 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
8343 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8344 iobj = (
INSN*) get_prev_insn(iobj);
8346 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
8350 if (&iobj->link == LAST_ELEMENT(ret)) {
8356 ADD_INSN(ret, line_node, pop);
8359 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8361 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8371 const NODE *line_node = node;
8372 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8373 LABEL *not_single = NEW_LABEL(nd_line(var));
8374 LABEL *not_ary = NEW_LABEL(nd_line(var));
8375 CHECK(COMPILE(ret,
"for var", var));
8376 ADD_INSN(ret, line_node, dup);
8377 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8378 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8379 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8380 ADD_INSNL(ret, line_node, branchunless, not_single);
8381 ADD_INSN(ret, line_node, dup);
8382 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8383 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8384 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8385 ADD_INSN(ret, line_node, swap);
8386 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
8387 ADD_INSN(ret, line_node, dup);
8388 ADD_INSNL(ret, line_node, branchunless, not_ary);
8389 ADD_INSN(ret, line_node, swap);
8390 ADD_LABEL(ret, not_ary);
8391 ADD_INSN(ret, line_node, pop);
8392 ADD_LABEL(ret, not_single);
8399 const NODE *line_node = node;
8400 unsigned long throw_flag = 0;
8402 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8404 LABEL *splabel = NEW_LABEL(0);
8405 ADD_LABEL(ret, splabel);
8406 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8407 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8408 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8409 add_ensure_iseq(ret, iseq, 0);
8410 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8411 ADD_ADJUST_RESTORE(ret, splabel);
8414 ADD_INSN(ret, line_node, putnil);
8421 if (!ISEQ_COMPILE_DATA(ip)) {
8426 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8427 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8429 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8432 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8433 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8437 ip = ISEQ_BODY(ip)->parent_iseq;
8442 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8443 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8445 ADD_INSN(ret, line_node, pop);
8449 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8458 const NODE *line_node = node;
8459 unsigned long throw_flag = 0;
8461 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8462 LABEL *splabel = NEW_LABEL(0);
8463 debugs(
"next in while loop\n");
8464 ADD_LABEL(ret, splabel);
8465 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8466 add_ensure_iseq(ret, iseq, 0);
8467 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8468 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8469 ADD_ADJUST_RESTORE(ret, splabel);
8471 ADD_INSN(ret, line_node, putnil);
8474 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8475 LABEL *splabel = NEW_LABEL(0);
8476 debugs(
"next in block\n");
8477 ADD_LABEL(ret, splabel);
8478 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8479 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8480 add_ensure_iseq(ret, iseq, 0);
8481 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8482 ADD_ADJUST_RESTORE(ret, splabel);
8485 ADD_INSN(ret, line_node, putnil);
8492 if (!ISEQ_COMPILE_DATA(ip)) {
8497 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8498 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8502 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8505 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8506 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8510 ip = ISEQ_BODY(ip)->parent_iseq;
8513 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8514 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8517 ADD_INSN(ret, line_node, pop);
8521 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
8531 const NODE *line_node = node;
8533 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8534 LABEL *splabel = NEW_LABEL(0);
8535 debugs(
"redo in while");
8536 ADD_LABEL(ret, splabel);
8537 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8538 add_ensure_iseq(ret, iseq, 0);
8539 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8540 ADD_ADJUST_RESTORE(ret, splabel);
8542 ADD_INSN(ret, line_node, putnil);
8545 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8546 LABEL *splabel = NEW_LABEL(0);
8548 debugs(
"redo in block");
8549 ADD_LABEL(ret, splabel);
8550 add_ensure_iseq(ret, iseq, 0);
8551 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8552 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8553 ADD_ADJUST_RESTORE(ret, splabel);
8556 ADD_INSN(ret, line_node, putnil);
8563 if (!ISEQ_COMPILE_DATA(ip)) {
8568 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8571 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8574 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8575 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
8579 ip = ISEQ_BODY(ip)->parent_iseq;
8582 ADD_INSN(ret, line_node, putnil);
8583 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8586 ADD_INSN(ret, line_node, pop);
8590 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
8600 const NODE *line_node = node;
8602 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
8603 ADD_INSN(ret, line_node, putnil);
8604 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
8607 ADD_INSN(ret, line_node, pop);
8611 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
8620 const int line = nd_line(node);
8621 const NODE *line_node = node;
8622 LABEL *lstart = NEW_LABEL(line);
8623 LABEL *lend = NEW_LABEL(line);
8624 LABEL *lcont = NEW_LABEL(line);
8625 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
8627 ISEQ_BODY(iseq)->location.label),
8628 ISEQ_TYPE_RESCUE, line);
8630 lstart->rescued = LABEL_RESCUE_BEG;
8631 lend->rescued = LABEL_RESCUE_END;
8632 ADD_LABEL(ret, lstart);
8634 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
8635 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
8637 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
8639 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
8641 ADD_LABEL(ret, lend);
8642 if (RNODE_RESCUE(node)->nd_else) {
8643 ADD_INSN(ret, line_node, pop);
8644 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
8646 ADD_INSN(ret, line_node, nop);
8647 ADD_LABEL(ret, lcont);
8650 ADD_INSN(ret, line_node, pop);
8654 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
8655 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
8662 const int line = nd_line(node);
8663 const NODE *line_node = node;
8664 const NODE *resq = node;
8666 LABEL *label_miss, *label_hit;
8669 label_miss = NEW_LABEL(line);
8670 label_hit = NEW_LABEL(line);
8672 narg = RNODE_RESBODY(resq)->nd_args;
8674 switch (nd_type(narg)) {
8677 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8678 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
8679 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8680 ADD_INSNL(ret, line_node, branchif, label_hit);
8681 narg = RNODE_LIST(narg)->nd_next;
8687 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8688 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
8689 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
8690 ADD_INSNL(ret, line_node, branchif, label_hit);
8693 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
8697 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8699 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8700 ADD_INSNL(ret, line_node, branchif, label_hit);
8702 ADD_INSNL(ret, line_node, jump, label_miss);
8703 ADD_LABEL(ret, label_hit);
8706 if (RNODE_RESBODY(resq)->nd_exc_var) {
8707 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
8710 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) {
8712 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
8715 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
8718 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
8719 ADD_INSN(ret, line_node, nop);
8721 ADD_INSN(ret, line_node, leave);
8722 ADD_LABEL(ret, label_miss);
8723 resq = RNODE_RESBODY(resq)->nd_next;
8731 const int line = nd_line(RNODE_ENSURE(node)->nd_ensr);
8732 const NODE *line_node = node;
8734 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
8736 ISEQ_TYPE_ENSURE, line);
8737 LABEL *lstart = NEW_LABEL(line);
8738 LABEL *lend = NEW_LABEL(line);
8739 LABEL *lcont = NEW_LABEL(line);
8747 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
8749 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
8754 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
8756 ADD_LABEL(ret, lstart);
8757 CHECK(COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
8758 ADD_LABEL(ret, lend);
8760 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
8761 ADD_LABEL(ret, lcont);
8762 if (last_leave) ADD_INSN(ret, line_node, pop);
8764 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
8765 if (lstart->link.next != &lend->link) {
8767 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
8769 erange = erange->next;
8773 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
8780 const NODE *line_node = node;
8783 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8785 enum rb_iseq_type t =
type;
8786 const NODE *retval = RNODE_RETURN(node)->nd_stts;
8789 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
8790 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
8791 t = ISEQ_BODY(is)->type;
8795 case ISEQ_TYPE_MAIN:
8797 rb_warn(
"argument of top-level return is ignored");
8801 type = ISEQ_TYPE_METHOD;
8808 if (
type == ISEQ_TYPE_METHOD) {
8809 splabel = NEW_LABEL(0);
8810 ADD_LABEL(ret, splabel);
8811 ADD_ADJUST(ret, line_node, 0);
8814 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
8816 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8817 add_ensure_iseq(ret, iseq, 1);
8819 ADD_INSN(ret, line_node, leave);
8820 ADD_ADJUST_RESTORE(ret, splabel);
8823 ADD_INSN(ret, line_node, putnil);
8827 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
8829 ADD_INSN(ret, line_node, pop);
8840 if (!i)
return false;
8841 if (IS_TRACE(i)) i = i->prev;
8842 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
8844 if (IS_ADJUST(i)) i = i->prev;
8845 if (!IS_INSN(i))
return false;
8846 switch (INSN_OF(i)) {
8853 (ret->last = last->prev)->next = NULL;
8860 CHECK(COMPILE_(ret,
"nd_body", node, popped));
8862 if (!popped && !all_string_result_p(node)) {
8863 const NODE *line_node = node;
8864 const unsigned int flag = VM_CALL_FCALL;
8868 ADD_INSN(ret, line_node, dup);
8869 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
8870 ADD_INSN(ret, line_node, anytostring);
8878 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8880 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
8881 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8887 LABEL *else_label = NEW_LABEL(nd_line(line_node));
8890 br = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"&.");
8892 ADD_INSN(recv, line_node, dup);
8893 ADD_INSNL(recv, line_node, branchnil, else_label);
8894 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
8902 if (!else_label)
return;
8903 end_label = NEW_LABEL(nd_line(line_node));
8904 ADD_INSNL(ret, line_node, jump, end_label);
8905 ADD_LABEL(ret, else_label);
8906 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
8907 ADD_LABEL(ret, end_label);
8916 if (get_nd_recv(node) &&
8917 (nd_type_p(get_nd_recv(node), NODE_STR) || nd_type_p(get_nd_recv(node), NODE_FILE)) &&
8918 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
8919 get_nd_args(node) == NULL &&
8920 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8921 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8922 VALUE str = get_string_value(get_nd_recv(node));
8923 if (get_node_call_nd_mid(node) == idUMinus) {
8924 ADD_INSN2(ret, line_node, opt_str_uminus, str,
8925 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
8928 ADD_INSN2(ret, line_node, opt_str_freeze, str,
8929 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
8933 ADD_INSN(ret, line_node, pop);
8940 if (get_node_call_nd_mid(node) == idAREF && !private_recv_p(node) && get_nd_args(node) &&
8941 nd_type_p(get_nd_args(node), NODE_LIST) && RNODE_LIST(get_nd_args(node))->as.nd_alen == 1 &&
8942 (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)) &&
8943 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8944 !frozen_string_literal_p(iseq) &&
8945 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8946 VALUE str = get_string_value(RNODE_LIST(get_nd_args(node))->nd_head);
8947 CHECK(COMPILE(ret,
"recv", get_nd_recv(node)));
8948 ADD_INSN2(ret, line_node, opt_aref_with, str,
8949 new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
8952 ADD_INSN(ret, line_node, pop);
8960iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
8962 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
8966iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
8969 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
8970 for (i=0; table[i].index != -1; i++) {
8971 if (strcmp(table[i].name, name) == 0) {
8979iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
8981 const char *name = rb_id2name(mid);
8982 static const char prefix[] =
"__builtin_";
8983 const size_t prefix_len =
sizeof(prefix) - 1;
8988 switch (nd_type(recv)) {
8990 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
8995 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
9005 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
9006 return &name[prefix_len];
9015delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
9022 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
9023 unsigned int start=0;
9028 argc + start <= ISEQ_BODY(iseq)->local_table_size;
9032 for (
unsigned int i=start; i-start<argc; i++) {
9033 if (IS_INSN(elem) &&
9034 INSN_OF(elem) == BIN(getlocal)) {
9035 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
9036 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
9038 if (local_level == 0) {
9039 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
9041 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
9042 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
9043 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
9044 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9068 *pstart_index = start;
9082 if (!node)
goto no_arg;
9084 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9085 const NODE *next = RNODE_LIST(node)->nd_next;
9087 node = RNODE_LIST(node)->nd_head;
9088 if (!node)
goto no_arg;
9089 switch (nd_type(node)) {
9091 symbol = rb_node_sym_string_val(node);
9097 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9100 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
9101 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9103 else if (strcmp(RSTRING_PTR(
string),
"inline_block") == 0) {
9104 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9106 else if (strcmp(RSTRING_PTR(
string),
"use_block") == 0) {
9107 iseq_set_use_block(iseq);
9109 else if (strcmp(RSTRING_PTR(
string),
"c_trace") == 0) {
9111 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9120 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9123 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9126 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %s", RSTRING_PTR(
string));
9129 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9137 if (!node)
goto no_arg;
9138 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9139 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9140 node = RNODE_LIST(node)->nd_head;
9141 if (!node)
goto no_arg;
9142 switch (nd_type(node)) {
9144 name = rb_node_sym_string_val(node);
9149 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9151 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9155 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9158 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9161 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9162 rb_builtin_class_name(name));
9165 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9171 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9172 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9173 return RNODE_IF(node)->nd_body;
9176 rb_bug(
"mandatory_node: can't find mandatory node");
9181compile_builtin_mandatory_only_method(
rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9185 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9188 rb_node_init(RNODE(&args_node), NODE_ARGS);
9189 args_node.nd_ainfo = args;
9192 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9193 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9197 tbl->size = table_size;
9202 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9203 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9206 for (; i<table_size; i++) {
9207 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9211 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9212 scope_node.nd_tbl = tbl;
9213 scope_node.nd_body = mandatory_node(iseq, node);
9214 scope_node.nd_args = &args_node;
9216 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9218 ISEQ_BODY(iseq)->mandatory_only_iseq =
9219 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9220 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9221 nd_line(line_node), NULL, 0,
9222 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9223 ISEQ_BODY(iseq)->variable.script_lines);
9233 NODE *args_node = get_nd_args(node);
9235 if (parent_block != NULL) {
9236 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9240# define BUILTIN_INLINE_PREFIX "_bi"
9241 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9242 bool cconst =
false;
9247 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9248 strcmp(
"cexpr!", builtin_func) == 0) {
9251 else if (strcmp(
"cconst!", builtin_func) == 0) {
9254 else if (strcmp(
"cinit!", builtin_func) == 0) {
9258 else if (strcmp(
"attr!", builtin_func) == 0) {
9259 return compile_builtin_attr(iseq, args_node);
9261 else if (strcmp(
"arg!", builtin_func) == 0) {
9262 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9264 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9266 rb_bug(
"mandatory_only? should be in if condition");
9268 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9269 rb_bug(
"mandatory_only? should be put on top");
9272 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9273 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9276 rb_bug(
"can't find builtin function:%s", builtin_func);
9279 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9283 int inline_index = nd_line(node);
9284 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9285 builtin_func = inline_func;
9291 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9292 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9293 ADD_INSN1(ret, line_node, putobject, const_val);
9299 unsigned int flag = 0;
9301 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9303 if (
FIX2INT(argc) != bf->argc) {
9304 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9305 builtin_func, bf->argc,
FIX2INT(argc));
9309 unsigned int start_index;
9310 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9311 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9315 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9318 if (popped) ADD_INSN(ret, line_node, pop);
9324compile_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)
9332 ID mid = get_node_call_nd_mid(node);
9334 unsigned int flag = 0;
9336 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9337 LABEL *else_label = NULL;
9340 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9345 if (nd_type_p(node, NODE_VCALL)) {
9350 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9352 if (mid == id_bitblt) {
9353 ADD_INSN(ret, line_node, bitblt);
9356 else if (mid == id_answer) {
9357 ADD_INSN(ret, line_node, answer);
9369 if (nd_type_p(node, NODE_FCALL) &&
9370 (mid == goto_id || mid == label_id)) {
9373 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9376 if (!labels_table) {
9377 labels_table = st_init_numtable();
9378 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9381 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9385 if (mid == goto_id) {
9386 ADD_INSNL(ret, line_node, jump, label);
9389 ADD_LABEL(ret, label);
9396 const char *builtin_func;
9397 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9398 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9399 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9403 if (!assume_receiver) {
9404 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9407 if (mid == idCall &&
9408 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9409 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9410 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9412 else if (private_recv_p(node)) {
9413 ADD_INSN(recv, node, putself);
9414 flag |= VM_CALL_FCALL;
9417 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9420 if (
type == NODE_QCALL) {
9421 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9424 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9425 ADD_CALL_RECEIVER(recv, line_node);
9430 if (
type != NODE_VCALL) {
9431 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9432 CHECK(!
NIL_P(argc));
9441 debugp_param(
"call args argc", argc);
9442 debugp_param(
"call method",
ID2SYM(mid));
9444 switch ((
int)
type) {
9446 flag |= VM_CALL_VCALL;
9449 flag |= VM_CALL_FCALL;
9452 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9453 ADD_INSN(ret, line_node, splatkw);
9455 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9457 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9459 ADD_INSN(ret, line_node, pop);
9467 const int line = nd_line(node);
9469 unsigned int flag = 0;
9471 ID id = RNODE_OP_ASGN1(node)->nd_mid;
9497 ADD_INSN(ret, node, putnil);
9499 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
9500 CHECK(asgnflag != -1);
9501 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
9506 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
9507 CHECK(!
NIL_P(argc));
9509 int dup_argn =
FIX2INT(argc) + 1;
9510 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
9512 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
9514 if (
id == idOROP ||
id == idANDOP) {
9523 LABEL *label = NEW_LABEL(line);
9524 LABEL *lfin = NEW_LABEL(line);
9526 ADD_INSN(ret, node, dup);
9528 ADD_INSNL(ret, node, branchif, label);
9531 ADD_INSNL(ret, node, branchunless, label);
9533 ADD_INSN(ret, node, pop);
9535 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9537 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9539 if (flag & VM_CALL_ARGS_SPLAT) {
9540 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9541 ADD_INSN(ret, node, swap);
9542 ADD_INSN1(ret, node, splatarray,
Qtrue);
9543 ADD_INSN(ret, node, swap);
9544 flag |= VM_CALL_ARGS_SPLAT_MUT;
9546 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9547 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9550 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9552 ADD_INSN(ret, node, pop);
9553 ADD_INSNL(ret, node, jump, lfin);
9554 ADD_LABEL(ret, label);
9556 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9558 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
9559 ADD_LABEL(ret, lfin);
9562 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9563 ADD_SEND(ret, node,
id,
INT2FIX(1));
9565 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9567 if (flag & VM_CALL_ARGS_SPLAT) {
9568 if (flag & VM_CALL_KW_SPLAT) {
9569 ADD_INSN1(ret, node, topn,
INT2FIX(2));
9570 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9571 ADD_INSN1(ret, node, splatarray,
Qtrue);
9572 flag |= VM_CALL_ARGS_SPLAT_MUT;
9574 ADD_INSN(ret, node, swap);
9575 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9576 ADD_INSN1(ret, node, setn,
INT2FIX(2));
9577 ADD_INSN(ret, node, pop);
9580 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9581 ADD_INSN(ret, node, swap);
9582 ADD_INSN1(ret, node, splatarray,
Qtrue);
9583 ADD_INSN(ret, node, swap);
9584 flag |= VM_CALL_ARGS_SPLAT_MUT;
9586 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9588 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9591 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9593 ADD_INSN(ret, node, pop);
9601 const int line = nd_line(node);
9602 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
9603 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
9605 LABEL *lfin = NEW_LABEL(line);
9606 LABEL *lcfin = NEW_LABEL(line);
9661 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
9662 CHECK(asgnflag != -1);
9663 if (RNODE_OP_ASGN2(node)->nd_aid) {
9664 lskip = NEW_LABEL(line);
9665 ADD_INSN(ret, node, dup);
9666 ADD_INSNL(ret, node, branchnil, lskip);
9668 ADD_INSN(ret, node, dup);
9669 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
9671 if (atype == idOROP || atype == idANDOP) {
9673 ADD_INSN(ret, node, dup);
9675 if (atype == idOROP) {
9676 ADD_INSNL(ret, node, branchif, lcfin);
9679 ADD_INSNL(ret, node, branchunless, lcfin);
9682 ADD_INSN(ret, node, pop);
9684 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9686 ADD_INSN(ret, node, swap);
9687 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9689 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9690 ADD_INSNL(ret, node, jump, lfin);
9692 ADD_LABEL(ret, lcfin);
9694 ADD_INSN(ret, node, swap);
9697 ADD_LABEL(ret, lfin);
9700 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9701 ADD_SEND(ret, node, atype,
INT2FIX(1));
9703 ADD_INSN(ret, node, swap);
9704 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9706 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9708 if (lskip && popped) {
9709 ADD_LABEL(ret, lskip);
9711 ADD_INSN(ret, node, pop);
9712 if (lskip && !popped) {
9713 ADD_LABEL(ret, lskip);
9718static int compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
9723 const int line = nd_line(node);
9728 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
9730 ADD_INSN1(ret, node, putobject, rb_cObject);
9733 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
9736 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
9737 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
9740 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
9742 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
9743 lassign = NEW_LABEL(line);
9744 ADD_INSN(ret, node, dup);
9745 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
9747 ADD_INSNL(ret, node, branchunless, lassign);
9749 ADD_INSN(ret, node, dup);
9750 ADD_INSN1(ret, node, putobject,
Qtrue);
9751 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
9753 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
9754 lfin = NEW_LABEL(line);
9755 if (!popped) ADD_INSN(ret, node, dup);
9756 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
9757 ADD_INSNL(ret, node, branchif, lfin);
9759 ADD_INSNL(ret, node, branchunless, lfin);
9761 if (!popped) ADD_INSN(ret, node, pop);
9762 if (lassign) ADD_LABEL(ret, lassign);
9763 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9766 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9768 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
9769 ADD_INSN(ret, node, swap);
9771 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9772 ADD_LABEL(ret, lfin);
9773 if (!popped) ADD_INSN(ret, node, swap);
9774 ADD_INSN(ret, node, pop);
9777 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9779 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
9781 ADD_INSN(ret, node, swap);
9783 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9784 ADD_INSN(ret, node, swap);
9786 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9794 const int line = nd_line(node);
9795 LABEL *lfin = NEW_LABEL(line);
9798 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
9802 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
9803 lassign = lfinish[1];
9805 lassign = NEW_LABEL(line);
9807 ADD_INSNL(ret, node, branchunless, lassign);
9810 lassign = NEW_LABEL(line);
9813 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
9816 ADD_INSN(ret, node, dup);
9819 if (
type == NODE_OP_ASGN_AND) {
9820 ADD_INSNL(ret, node, branchunless, lfin);
9823 ADD_INSNL(ret, node, branchif, lfin);
9827 ADD_INSN(ret, node, pop);
9830 ADD_LABEL(ret, lassign);
9831 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
9832 ADD_LABEL(ret, lfin);
9842 unsigned int flag = 0;
9844 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9848 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9850 if (
type == NODE_SUPER) {
9851 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
9852 CHECK(!
NIL_P(vargc));
9854 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9855 ADD_INSN(args, node, splatkw);
9858 if (flag & VM_CALL_ARGS_BLOCKARG) {
9865 const rb_iseq_t *liseq = body->local_iseq;
9867 const struct rb_iseq_param_keyword *
const local_kwd = local_body->
param.keyword;
9868 int lvar_level = get_lvar_level(iseq);
9870 argc = local_body->
param.lead_num;
9873 for (i = 0; i < local_body->
param.lead_num; i++) {
9874 int idx = local_body->local_table_size - i;
9875 ADD_GETLOCAL(args, node, idx, lvar_level);
9879 if (local_body->
param.flags.forwardable) {
9880 flag |= VM_CALL_FORWARDING;
9881 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
9882 ADD_GETLOCAL(args, node, idx, lvar_level);
9885 if (local_body->
param.flags.has_opt) {
9888 for (j = 0; j < local_body->
param.opt_num; j++) {
9889 int idx = local_body->local_table_size - (i + j);
9890 ADD_GETLOCAL(args, node, idx, lvar_level);
9895 if (local_body->
param.flags.has_rest) {
9897 int idx = local_body->local_table_size - local_body->
param.rest_start;
9898 ADD_GETLOCAL(args, node, idx, lvar_level);
9899 ADD_INSN1(args, node, splatarray, RBOOL(local_body->
param.flags.has_post));
9901 argc = local_body->
param.rest_start + 1;
9902 flag |= VM_CALL_ARGS_SPLAT;
9904 if (local_body->
param.flags.has_post) {
9906 int post_len = local_body->
param.post_num;
9907 int post_start = local_body->
param.post_start;
9909 if (local_body->
param.flags.has_rest) {
9911 for (j=0; j<post_len; j++) {
9912 int idx = local_body->local_table_size - (post_start + j);
9913 ADD_GETLOCAL(args, node, idx, lvar_level);
9915 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
9916 flag |= VM_CALL_ARGS_SPLAT_MUT;
9921 for (j=0; j<post_len; j++) {
9922 int idx = local_body->local_table_size - (post_start + j);
9923 ADD_GETLOCAL(args, node, idx, lvar_level);
9925 argc = post_len + post_start;
9929 if (local_body->
param.flags.has_kw) {
9930 int local_size = local_body->local_table_size;
9933 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9935 if (local_body->
param.flags.has_kwrest) {
9936 int idx = local_body->local_table_size - local_kwd->rest_start;
9937 ADD_GETLOCAL(args, node, idx, lvar_level);
9939 ADD_SEND (args, node, rb_intern(
"dup"),
INT2FIX(0));
9942 ADD_INSN1(args, node, newhash,
INT2FIX(0));
9944 for (i = 0; i < local_kwd->num; ++i) {
9945 ID id = local_kwd->table[i];
9946 int idx = local_size - get_local_var_idx(liseq,
id);
9947 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
9948 ADD_GETLOCAL(args, node, idx, lvar_level);
9950 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
9951 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
9953 else if (local_body->
param.flags.has_kwrest) {
9954 int idx = local_body->local_table_size - local_kwd->rest_start;
9955 ADD_GETLOCAL(args, node, idx, lvar_level);
9957 flag |= VM_CALL_KW_SPLAT;
9961 if (use_block && parent_block == NULL) {
9962 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
9965 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
9966 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
9967 ADD_INSN(ret, node, putself);
9970 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
9972 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
9973 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
9976 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
9980 ADD_INSN(ret, node, pop);
9990 unsigned int flag = 0;
9995 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
9997 case ISEQ_TYPE_MAIN:
9998 case ISEQ_TYPE_CLASS:
9999 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
10004 if (RNODE_YIELD(node)->nd_head) {
10005 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
10006 CHECK(!
NIL_P(argc));
10012 ADD_SEQ(ret, args);
10013 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
10014 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10017 ADD_INSN(ret, node, pop);
10022 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
10023 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
10025 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
10038 switch ((
int)
type) {
10040 ADD_INSN1(recv, node, putobject, rb_node_regx_string_val(node));
10041 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
10045 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
10046 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
10049 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
10050 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
10054 ADD_SEQ(ret, recv);
10056 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
10058 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
10059 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
10063 ADD_INSN(ret, node, pop);
10074 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10075 (segments = collect_const_segments(iseq, node))) {
10076 ISEQ_BODY(iseq)->ic_size++;
10077 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10087 CHECK(compile_const_prefix(iseq, node, pref, body));
10088 if (LIST_INSN_SIZE_ZERO(pref)) {
10089 ADD_INSN(ret, node, putnil);
10090 ADD_SEQ(ret, body);
10093 ADD_SEQ(ret, pref);
10094 ADD_SEQ(ret, body);
10100 ADD_CALL_RECEIVER(ret, node);
10101 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10102 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10105 ADD_INSN(ret, node, pop);
10113 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10116 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10117 ISEQ_BODY(iseq)->ic_size++;
10118 VALUE segments = rb_ary_new_from_args(2,
ID2SYM(idNULL),
ID2SYM(RNODE_COLON3(node)->nd_mid));
10119 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10123 ADD_INSN1(ret, node, putobject, rb_cObject);
10124 ADD_INSN1(ret, node, putobject,
Qtrue);
10125 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10129 ADD_INSN(ret, node, pop);
10138 const NODE *b = RNODE_DOT2(node)->nd_beg;
10139 const NODE *e = RNODE_DOT2(node)->nd_end;
10141 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10143 VALUE bv = optimized_range_item(b);
10144 VALUE ev = optimized_range_item(e);
10146 ADD_INSN1(ret, node, putobject, val);
10151 CHECK(COMPILE_(ret,
"min", b, popped));
10152 CHECK(COMPILE_(ret,
"max", e, popped));
10154 ADD_INSN1(ret, node, newrange, flag);
10164 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10165 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10171 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10174 ip = ISEQ_BODY(ip)->parent_iseq;
10178 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10181 ADD_INSN(ret, node, putnil);
10192 LABEL *end_label = NEW_LABEL(nd_line(node));
10193 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10195 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10197 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10200 else if (nd_type_p(default_value, NODE_SYM) ||
10201 nd_type_p(default_value, NODE_REGX) ||
10202 nd_type_p(default_value, NODE_LINE) ||
10203 nd_type_p(default_value, NODE_INTEGER) ||
10204 nd_type_p(default_value, NODE_FLOAT) ||
10205 nd_type_p(default_value, NODE_RATIONAL) ||
10206 nd_type_p(default_value, NODE_IMAGINARY) ||
10207 nd_type_p(default_value, NODE_NIL) ||
10208 nd_type_p(default_value, NODE_TRUE) ||
10209 nd_type_p(default_value, NODE_FALSE)) {
10210 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10218 int kw_bits_idx = body->local_table_size - body->
param.keyword->bits_start;
10219 int keyword_idx = body->
param.keyword->num;
10221 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10222 ADD_INSNL(ret, node, branchif, end_label);
10223 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10224 ADD_LABEL(ret, end_label);
10234 unsigned int flag = 0;
10235 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10237 LABEL *else_label = NULL;
10243 if (!ISEQ_COMPILE_DATA(iseq)->in_masgn &&
10244 mid == idASET && !private_recv_p(node) && RNODE_ATTRASGN(node)->nd_args &&
10245 nd_type_p(RNODE_ATTRASGN(node)->nd_args, NODE_LIST) && RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->as.nd_alen == 2 &&
10246 (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)) &&
10247 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
10248 !frozen_string_literal_p(iseq) &&
10249 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction)
10251 VALUE str = get_string_value(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_head);
10252 CHECK(COMPILE(ret,
"recv", RNODE_ATTRASGN(node)->nd_recv));
10253 CHECK(COMPILE(ret,
"value", RNODE_LIST(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_next)->nd_head));
10255 ADD_INSN(ret, node, swap);
10256 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10258 ADD_INSN2(ret, node, opt_aset_with, str,
10259 new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
10261 ADD_INSN(ret, node, pop);
10267 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10268 CHECK(!
NIL_P(argc));
10270 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10271 CHECK(asgnflag != -1);
10272 flag |= (
unsigned int)asgnflag;
10274 debugp_param(
"argc", argc);
10275 debugp_param(
"nd_mid",
ID2SYM(mid));
10279 mid = rb_id_attrset(mid);
10280 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10283 ADD_INSN(ret, node, putnil);
10284 ADD_SEQ(ret, recv);
10285 ADD_SEQ(ret, args);
10287 if (flag & VM_CALL_ARGS_SPLAT) {
10288 ADD_INSN(ret, node, dup);
10289 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10290 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10291 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10292 ADD_INSN (ret, node, pop);
10295 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10299 ADD_SEQ(ret, recv);
10300 ADD_SEQ(ret, args);
10302 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10303 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10304 ADD_INSN(ret, node, pop);
10311 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10319 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10326 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10333node_const_decl_val(
const NODE *node)
10336 switch (nd_type(node)) {
10338 if (RNODE_CDECL(node)->nd_vid) {
10339 path = rb_id2str(RNODE_CDECL(node)->nd_vid);
10343 node = RNODE_CDECL(node)->nd_else;
10351 rb_str_append(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10354 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10358 path = rb_ary_new();
10360 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10361 rb_ary_push(path, rb_id2str(RNODE_COLON2(node)->nd_mid));
10363 if (node && nd_type_p(node, NODE_CONST)) {
10365 rb_ary_push(path, rb_id2str(RNODE_CONST(node)->nd_vid));
10367 else if (node && nd_type_p(node, NODE_COLON3)) {
10369 rb_ary_push(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10379 path = rb_fstring(path);
10384const_decl_path(
NODE *dest)
10387 if (!nd_type_p(dest, NODE_CALL)) {
10388 path = node_const_decl_val(dest);
10399 VALUE path = const_decl_path(dest);
10400 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10401 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10402 ADD_INSN1(ret, value, putobject, path);
10404 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10409#ifndef SHAREABLE_BARE_EXPRESSION
10410#define SHAREABLE_BARE_EXPRESSION 1
10414compile_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)
10416# define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10417 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10419 DECL_ANCHOR(anchor);
10421 enum node_type
type = node ? nd_type(node) : NODE_NIL;
10433 *value_p = rb_node_sym_string_val(node);
10436 *value_p = rb_node_regx_string_val(node);
10439 *value_p = rb_node_line_lineno_val(node);
10442 *value_p = rb_node_integer_literal_val(node);
10445 *value_p = rb_node_float_literal_val(node);
10447 case NODE_RATIONAL:
10448 *value_p = rb_node_rational_literal_val(node);
10450 case NODE_IMAGINARY:
10451 *value_p = rb_node_imaginary_literal_val(node);
10453 case NODE_ENCODING:
10454 *value_p = rb_node_encoding_val(node);
10457 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10458 *shareable_literal_p = 1;
10462 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10463 if (shareable == rb_parser_shareable_literal) {
10469 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10472 *shareable_literal_p = 1;
10476 VALUE lit = rb_node_str_string_val(node);
10477 ADD_INSN1(ret, node, putobject, lit);
10480 *shareable_literal_p = 1;
10486 VALUE lit = rb_node_file_path_val(node);
10487 ADD_INSN1(ret, node, putobject, lit);
10490 *shareable_literal_p = 1;
10496 VALUE lit = rb_ary_new();
10498 ADD_INSN1(ret, node, putobject, lit);
10501 *shareable_literal_p = 1;
10507 INIT_ANCHOR(anchor);
10508 lit = rb_ary_new();
10509 for (
NODE *n = (
NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
10511 int shareable_literal_p2;
10512 NODE *elt = RNODE_LIST(n)->nd_head;
10514 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
10515 if (shareable_literal_p2) {
10518 else if (
RTEST(lit)) {
10524 if (!UNDEF_P(val)) {
10525 rb_ary_push(lit, val);
10536 if (!RNODE_HASH(node)->nd_brace) {
10538 *shareable_literal_p = 0;
10541 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10542 if (!RNODE_LIST(n)->nd_head) {
10544 goto compile_shareable;
10548 INIT_ANCHOR(anchor);
10549 lit = rb_hash_new();
10550 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10552 VALUE value_val = 0;
10553 int shareable_literal_p2;
10554 NODE *key = RNODE_LIST(n)->nd_head;
10555 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
10556 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10557 if (shareable_literal_p2) {
10560 else if (
RTEST(lit)) {
10561 rb_hash_clear(lit);
10564 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10565 if (shareable_literal_p2) {
10568 else if (
RTEST(lit)) {
10569 rb_hash_clear(lit);
10573 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
10574 rb_hash_aset(lit, key_val, value_val);
10577 rb_hash_clear(lit);
10588 if (shareable == rb_parser_shareable_literal &&
10589 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
10590 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
10592 *shareable_literal_p = 1;
10595 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10597 *shareable_literal_p = 0;
10603 if (nd_type(node) == NODE_LIST) {
10604 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10606 else if (nd_type(node) == NODE_HASH) {
10607 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10608 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10611 *shareable_literal_p = 0;
10612 ADD_SEQ(ret, anchor);
10618 if (nd_type(node) == NODE_LIST) {
10619 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10621 else if (nd_type(node) == NODE_HASH) {
10622 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10623 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10625 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
10627 *shareable_literal_p = 1;
10631 ADD_INSN1(ret, node, putobject, val);
10634 *shareable_literal_p = 1;
10641compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
10645 DECL_ANCHOR(anchor);
10646 INIT_ANCHOR(anchor);
10648 switch (shareable) {
10649 case rb_parser_shareable_none:
10650 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
10653 case rb_parser_shareable_literal:
10654 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10655 ADD_SEQ(ret, anchor);
10658 case rb_parser_shareable_copy:
10659 case rb_parser_shareable_everything:
10660 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10662 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
10665 ADD_SEQ(ret, anchor);
10669 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
10686 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
10687 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
10688 debugs(
"node: NODE_NIL(implicit)\n");
10689 ADD_SYNTHETIC_INSN(ret, lineno, -1, putnil);
10693 return iseq_compile_each0(iseq, ret, node, popped);
10699 const int line = (int)nd_line(node);
10700 const enum node_type
type = nd_type(node);
10703 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
10707 if (nd_fl_newline(node)) {
10709 ISEQ_COMPILE_DATA(iseq)->last_line = line;
10710 if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
10711 event |= RUBY_EVENT_COVERAGE_LINE;
10713 ADD_TRACE(ret, event);
10717 debug_node_start(node);
10718#undef BEFORE_RETURN
10719#define BEFORE_RETURN debug_node_end()
10723 CHECK(compile_block(iseq, ret, node, popped));
10727 CHECK(compile_if(iseq, ret, node, popped,
type));
10730 CHECK(compile_case(iseq, ret, node, popped));
10733 CHECK(compile_case2(iseq, ret, node, popped));
10736 CHECK(compile_case3(iseq, ret, node, popped));
10740 CHECK(compile_loop(iseq, ret, node, popped,
type));
10744 CHECK(compile_iter(iseq, ret, node, popped));
10746 case NODE_FOR_MASGN:
10747 CHECK(compile_for_masgn(iseq, ret, node, popped));
10750 CHECK(compile_break(iseq, ret, node, popped));
10753 CHECK(compile_next(iseq, ret, node, popped));
10756 CHECK(compile_redo(iseq, ret, node, popped));
10759 CHECK(compile_retry(iseq, ret, node, popped));
10762 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
10766 CHECK(compile_rescue(iseq, ret, node, popped));
10769 CHECK(compile_resbody(iseq, ret, node, popped));
10772 CHECK(compile_ensure(iseq, ret, node, popped));
10777 LABEL *end_label = NEW_LABEL(line);
10778 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(node)->nd_1st));
10780 ADD_INSN(ret, node, dup);
10782 if (
type == NODE_AND) {
10783 ADD_INSNL(ret, node, branchunless, end_label);
10786 ADD_INSNL(ret, node, branchif, end_label);
10789 ADD_INSN(ret, node, pop);
10791 CHECK(COMPILE_(ret,
"nd_2nd", RNODE_OR(node)->nd_2nd, popped));
10792 ADD_LABEL(ret, end_label);
10797 bool prev_in_masgn = ISEQ_COMPILE_DATA(iseq)->in_masgn;
10798 ISEQ_COMPILE_DATA(iseq)->in_masgn =
true;
10799 compile_massign(iseq, ret, node, popped);
10800 ISEQ_COMPILE_DATA(iseq)->in_masgn = prev_in_masgn;
10805 ID id = RNODE_LASGN(node)->nd_vid;
10806 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
10808 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
10809 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
10812 ADD_INSN(ret, node, dup);
10814 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
10819 ID id = RNODE_DASGN(node)->nd_vid;
10820 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
10821 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
10824 ADD_INSN(ret, node, dup);
10827 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
10830 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
10834 ADD_SETLOCAL(ret, node, ls - idx, lv);
10838 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
10841 ADD_INSN(ret, node, dup);
10843 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
10847 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
10849 ADD_INSN(ret, node, dup);
10851 ADD_INSN2(ret, node, setinstancevariable,
10852 ID2SYM(RNODE_IASGN(node)->nd_vid),
10853 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
10857 if (RNODE_CDECL(node)->nd_vid) {
10858 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
10861 ADD_INSN(ret, node, dup);
10864 ADD_INSN1(ret, node, putspecialobject,
10865 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
10866 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
10869 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
10870 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
10871 ADD_INSN(ret, node, swap);
10874 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10875 ADD_INSN(ret, node, swap);
10878 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
10883 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
10885 ADD_INSN(ret, node, dup);
10887 ADD_INSN2(ret, node, setclassvariable,
10888 ID2SYM(RNODE_CVASGN(node)->nd_vid),
10889 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
10892 case NODE_OP_ASGN1:
10893 CHECK(compile_op_asgn1(iseq, ret, node, popped));
10895 case NODE_OP_ASGN2:
10896 CHECK(compile_op_asgn2(iseq, ret, node, popped));
10898 case NODE_OP_CDECL:
10899 CHECK(compile_op_cdecl(iseq, ret, node, popped));
10901 case NODE_OP_ASGN_AND:
10902 case NODE_OP_ASGN_OR:
10903 CHECK(compile_op_log(iseq, ret, node, popped,
type));
10907 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
10913 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
10919 CHECK(compile_super(iseq, ret, node, popped,
type));
10922 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
10927 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
10932 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
10935 CHECK(compile_return(iseq, ret, node, popped));
10938 CHECK(compile_yield(iseq, ret, node, popped));
10942 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
10948 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
10950 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
10952 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
10953 rb_id2str(RNODE_DVAR(node)->nd_vid));
10956 ADD_GETLOCAL(ret, node, ls - idx, lv);
10961 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
10963 ADD_INSN(ret, node, pop);
10968 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
10970 ADD_INSN2(ret, node, getinstancevariable,
10971 ID2SYM(RNODE_IVAR(node)->nd_vid),
10972 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
10977 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
10979 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10981 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(RNODE_CONST(node)->nd_vid));
10982 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10986 ADD_INSN(ret, node, putnil);
10987 ADD_INSN1(ret, node, putobject,
Qtrue);
10988 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
10992 ADD_INSN(ret, node, pop);
10998 ADD_INSN2(ret, node, getclassvariable,
10999 ID2SYM(RNODE_CVAR(node)->nd_vid),
11000 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
11004 case NODE_NTH_REF:{
11006 if (!RNODE_NTH_REF(node)->nd_nth) {
11007 ADD_INSN(ret, node, putnil);
11010 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11011 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
11015 case NODE_BACK_REF:{
11017 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11018 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
11025 CHECK(compile_match(iseq, ret, node, popped,
type));
11029 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
11035 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
11039 case NODE_ENCODING:{
11041 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
11045 case NODE_INTEGER:{
11046 VALUE lit = rb_node_integer_literal_val(node);
11047 debugp_param(
"integer", lit);
11049 ADD_INSN1(ret, node, putobject, lit);
11055 VALUE lit = rb_node_float_literal_val(node);
11056 debugp_param(
"float", lit);
11058 ADD_INSN1(ret, node, putobject, lit);
11063 case NODE_RATIONAL:{
11064 VALUE lit = rb_node_rational_literal_val(node);
11065 debugp_param(
"rational", lit);
11067 ADD_INSN1(ret, node, putobject, lit);
11072 case NODE_IMAGINARY:{
11073 VALUE lit = rb_node_imaginary_literal_val(node);
11074 debugp_param(
"imaginary", lit);
11076 ADD_INSN1(ret, node, putobject, lit);
11083 debugp_param(
"nd_lit", get_string_value(node));
11085 VALUE lit = get_string_value(node);
11088 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11089 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11091 switch (option->frozen_string_literal) {
11092 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11093 ADD_INSN1(ret, node, putchilledstring, lit);
11095 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11096 ADD_INSN1(ret, node, putstring, lit);
11098 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11099 ADD_INSN1(ret, node, putobject, lit);
11102 rb_bug(
"invalid frozen_string_literal");
11109 compile_dstr(iseq, ret, node);
11112 ADD_INSN(ret, node, pop);
11117 ADD_CALL_RECEIVER(ret, node);
11118 VALUE str = rb_node_str_string_val(node);
11119 ADD_INSN1(ret, node, putobject, str);
11121 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11124 ADD_INSN(ret, node, pop);
11129 ADD_CALL_RECEIVER(ret, node);
11130 compile_dstr(iseq, ret, node);
11131 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11134 ADD_INSN(ret, node, pop);
11139 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11143 VALUE lit = rb_node_regx_string_val(node);
11144 ADD_INSN1(ret, node, putobject, lit);
11150 compile_dregx(iseq, ret, node, popped);
11153 int ic_index = body->ise_size++;
11155 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11157 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
11161 ADD_INSN(ret, node, pop);
11165 case NODE_ARGSCAT:{
11167 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11168 ADD_INSN1(ret, node, splatarray,
Qfalse);
11169 ADD_INSN(ret, node, pop);
11170 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11171 ADD_INSN1(ret, node, splatarray,
Qfalse);
11172 ADD_INSN(ret, node, pop);
11175 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11176 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11177 if (nd_type_p(body_node, NODE_LIST)) {
11178 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11181 CHECK(COMPILE(ret,
"argscat body", body_node));
11182 ADD_INSN(ret, node, concattoarray);
11187 case NODE_ARGSPUSH:{
11189 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11190 ADD_INSN1(ret, node, splatarray,
Qfalse);
11191 ADD_INSN(ret, node, pop);
11192 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11195 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11196 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11197 if (keyword_node_p(body_node)) {
11198 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11199 ADD_INSN(ret, node, pushtoarraykwsplat);
11201 else if (static_literal_node_p(body_node, iseq,
false)) {
11202 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11203 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11206 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11207 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11213 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11214 ADD_INSN1(ret, node, splatarray,
Qtrue);
11217 ADD_INSN(ret, node, pop);
11222 ID mid = RNODE_DEFN(node)->nd_mid;
11223 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11225 ISEQ_TYPE_METHOD, line);
11227 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11228 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11232 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11238 ID mid = RNODE_DEFS(node)->nd_mid;
11239 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11241 ISEQ_TYPE_METHOD, line);
11243 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11244 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11245 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11249 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11254 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11255 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11256 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11257 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11258 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11261 ADD_INSN(ret, node, pop);
11266 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11267 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11268 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11269 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11272 ADD_INSN(ret, node, pop);
11279 for (
long i = 0; i < ary->len; i++) {
11280 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11281 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11282 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11283 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11285 if (i < ary->
len - 1) {
11286 ADD_INSN(ret, node, pop);
11291 ADD_INSN(ret, node, pop);
11296 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11297 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
11298 ISEQ_TYPE_CLASS, line);
11299 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11300 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11301 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11303 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11304 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11308 ADD_INSN(ret, node, pop);
11313 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11314 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
11315 ISEQ_TYPE_CLASS, line);
11316 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11317 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11319 ADD_INSN (ret, node, putnil);
11320 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11324 ADD_INSN(ret, node, pop);
11330 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11331 ISEQ_TYPE_CLASS, line);
11333 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11334 ADD_INSN (ret, node, putnil);
11335 CONST_ID(singletonclass,
"singletonclass");
11336 ADD_INSN3(ret, node, defineclass,
11337 ID2SYM(singletonclass), singleton_class,
11338 INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
11342 ADD_INSN(ret, node, pop);
11347 CHECK(compile_colon2(iseq, ret, node, popped));
11350 CHECK(compile_colon3(iseq, ret, node, popped));
11353 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11356 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11360 LABEL *lend = NEW_LABEL(line);
11361 LABEL *ltrue = NEW_LABEL(line);
11362 LABEL *lfalse = NEW_LABEL(line);
11363 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11365 ADD_LABEL(ret, ltrue);
11366 ADD_INSN1(ret, node, putobject,
Qtrue);
11367 ADD_INSNL(ret, node, jump, lend);
11368 ADD_LABEL(ret, lfalse);
11369 ADD_INSN1(ret, node, putobject,
Qfalse);
11370 ADD_LABEL(ret, lend);
11375 ADD_INSN(ret, node, putself);
11381 ADD_INSN(ret, node, putnil);
11387 ADD_INSN1(ret, node, putobject,
Qtrue);
11393 ADD_INSN1(ret, node, putobject,
Qfalse);
11398 CHECK(compile_errinfo(iseq, ret, node, popped));
11402 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11405 case NODE_POSTEXE:{
11409 int is_index = body->ise_size++;
11411 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11413 NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc, rb_fstring(make_name_for_block(iseq)), ISEQ_TYPE_BLOCK, line);
11415 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
11419 ADD_INSN(ret, node, pop);
11424 CHECK(compile_kw_arg(iseq, ret, node, popped));
11427 compile_dstr(iseq, ret, node);
11429 ADD_INSN(ret, node, intern);
11432 ADD_INSN(ret, node, pop);
11436 case NODE_ATTRASGN:
11437 CHECK(compile_attrasgn(iseq, ret, node, popped));
11441 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11444 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11445 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11449 ADD_INSN(ret, node, pop);
11454 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
11469insn_data_length(
INSN *iobj)
11471 return insn_len(iobj->insn_id);
11475calc_sp_depth(
int depth,
INSN *insn)
11477 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
11481opobj_inspect(
VALUE obj)
11489 obj = rb_ary_dup(obj);
11501insn_data_to_s_detail(
INSN *iobj)
11503 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
11505 if (iobj->operands) {
11506 const char *types = insn_op_types(iobj->insn_id);
11509 for (j = 0; types[j]; j++) {
11510 char type = types[j];
11516 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
11534 VALUE v = OPERAND_AT(iobj, j);
11549 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11552 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11555 rb_str_catf(str,
"<ise:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11561 if (vm_ci_mid(ci)) rb_str_catf(str,
"%"PRIsVALUE, rb_id2str(vm_ci_mid(ci)));
11562 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
11570 void *func = (
void *)OPERAND_AT(iobj, j);
11573 if (dladdr(func, &info) && info.dli_sname) {
11578 rb_str_catf(str,
"<%p>", func);
11588 if (types[j + 1]) {
11599 dump_disasm_list_with_cursor(link, NULL, NULL);
11610 printf(
"-- raw disasm--------\n");
11613 if (curr) printf(curr == link ?
"*" :
" ");
11614 switch (link->type) {
11615 case ISEQ_ELEMENT_INSN:
11617 iobj = (
INSN *)link;
11618 str = insn_data_to_s_detail(iobj);
11619 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
11620 pos += insn_data_length(iobj);
11623 case ISEQ_ELEMENT_LABEL:
11625 lobj = (
LABEL *)link;
11626 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
11627 dest == lobj ?
" <---" :
"");
11630 case ISEQ_ELEMENT_TRACE:
11633 printf(
" trace: %0x\n", trace->event);
11636 case ISEQ_ELEMENT_ADJUST:
11639 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
11644 rb_raise(
rb_eSyntaxError,
"dump_disasm_list error: %d\n", (
int)link->type);
11648 printf(
"---------------------\n");
11653rb_insn_len(
VALUE insn)
11655 return insn_len(insn);
11659rb_insns_name(
int i)
11661 return insn_name(i);
11665rb_insns_name_array(
void)
11667 VALUE ary = rb_ary_new_capa(VM_INSTRUCTION_SIZE);
11669 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
11670 rb_ary_push(ary, rb_fstring_cstr(insn_name(i)));
11672 return rb_ary_freeze(ary);
11680 obj = rb_to_symbol_type(obj);
11682 if (st_lookup(labels_table, obj, &tmp) == 0) {
11683 label = NEW_LABEL(0);
11684 st_insert(labels_table, obj, (st_data_t)label);
11687 label = (
LABEL *)tmp;
11694get_exception_sym2type(
VALUE sym)
11696 static VALUE symRescue, symEnsure, symRetry;
11697 static VALUE symBreak, symRedo, symNext;
11699 if (symRescue == 0) {
11708 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
11709 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
11710 if (sym == symRetry)
return CATCH_TYPE_RETRY;
11711 if (sym == symBreak)
return CATCH_TYPE_BREAK;
11712 if (sym == symRedo)
return CATCH_TYPE_REDO;
11713 if (sym == symNext)
return CATCH_TYPE_NEXT;
11714 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
11727 LABEL *lstart, *lend, *lcont;
11742 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
11743 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
11744 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
11748 if (
type == CATCH_TYPE_RESCUE ||
11749 type == CATCH_TYPE_BREAK ||
11750 type == CATCH_TYPE_NEXT) {
11756 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
11764insn_make_insn_table(
void)
11768 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
11770 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
11784 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
11786 else if (
CLASS_OF(op) == rb_cISeq) {
11793 loaded_iseq = rb_iseqw_to_iseq(iseqw);
11794 return loaded_iseq;
11802 unsigned int flag = 0;
11813 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
11815 if (!
NIL_P(vkw_arg)) {
11818 size_t n = rb_callinfo_kwarg_bytes(
len);
11821 kw_arg->references = 0;
11822 kw_arg->keyword_len =
len;
11823 for (i = 0; i <
len; i++) {
11826 kw_arg->keywords[i] = kw;
11831 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
11837event_name_to_flag(
VALUE sym)
11839#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
11860 int line_no = 0, node_id = -1, insn_idx = 0;
11861 int ret = COMPILE_OK;
11866 static struct st_table *insn_table;
11868 if (insn_table == 0) {
11869 insn_table = insn_make_insn_table();
11872 for (i=0; i<
len; i++) {
11878 ADD_TRACE(anchor, event);
11881 LABEL *label = register_label(iseq, labels_table, obj);
11882 ADD_LABEL(anchor, label);
11895 node_id =
NUM2INT(rb_ary_entry(node_ids, insn_idx++));
11899 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
11901 COMPILE_ERROR(iseq, line_no,
11902 "unknown instruction: %+"PRIsVALUE, insn);
11907 if (argc != insn_len((
VALUE)insn_id)-1) {
11908 COMPILE_ERROR(iseq, line_no,
11909 "operand size mismatch");
11915 argv = compile_data_calloc2(iseq,
sizeof(
VALUE), argc);
11920 (
enum ruby_vminsn_type)insn_id, argc, argv));
11922 for (j=0; j<argc; j++) {
11923 VALUE op = rb_ary_entry(obj, j+1);
11924 switch (insn_op_type((
VALUE)insn_id, j)) {
11926 LABEL *label = register_label(iseq, labels_table, op);
11927 argv[j] = (
VALUE)label;
11942 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
11953 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
11954 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
11959 VALUE segments = rb_ary_new();
11960 op = rb_to_array_type(op);
11964 sym = rb_to_symbol_type(sym);
11965 rb_ary_push(segments, sym);
11969 argv[j] = segments;
11971 ISEQ_BODY(iseq)->ic_size++;
11976 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
11977 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
11982 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
11983 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
11987 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
11990 argv[j] = rb_to_symbol_type(op);
11997 RHASH_TBL_RAW(map)->type = &cdhash_type;
11998 op = rb_to_array_type(op);
12003 register_label(iseq, labels_table, sym);
12004 rb_hash_aset(map, key, (
VALUE)label | 1);
12013#if SIZEOF_VALUE <= SIZEOF_LONG
12018 argv[j] = (
VALUE)funcptr;
12029 (
enum ruby_vminsn_type)insn_id, argc, NULL));
12033 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
12038 validate_labels(iseq, labels_table);
12039 if (!ret)
return ret;
12040 return iseq_setup(iseq, anchor);
12043#define CHECK_ARRAY(v) rb_to_array_type(v)
12044#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
12049 VALUE val = rb_hash_aref(param, sym);
12054 else if (!
NIL_P(val)) {
12055 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
12061static const struct rb_iseq_param_keyword *
12067 VALUE key, sym, default_val;
12070 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12072 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12074 keyword->num =
len;
12075#define SYM(s) ID2SYM(rb_intern_const(#s))
12076 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12077 i = keyword->bits_start - keyword->num;
12078 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12082 for (i = 0; i <
len; i++) {
12086 goto default_values;
12089 keyword->required_num++;
12093 default_len =
len - i;
12094 if (default_len == 0) {
12095 keyword->table = ids;
12098 else if (default_len < 0) {
12104 for (j = 0; i <
len; i++, j++) {
12118 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
12124 keyword->table = ids;
12125 keyword->default_values = dvs;
12131iseq_insn_each_object_mark_and_pin(
VALUE obj,
VALUE _)
12140 size_t size =
sizeof(
INSN);
12141 unsigned int pos = 0;
12144#ifdef STRICT_ALIGNMENT
12145 size_t padding = calc_padding((
void *)&storage->buff[pos], size);
12147 const size_t padding = 0;
12149 size_t offset = pos + size + padding;
12150 if (offset > storage->size || offset > storage->pos) {
12152 storage = storage->next;
12155#ifdef STRICT_ALIGNMENT
12156 pos += (int)padding;
12159 iobj = (
INSN *)&storage->buff[pos];
12161 if (iobj->operands) {
12162 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_pin, (
VALUE)0);
12175 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
12182#define SYM(s) ID2SYM(rb_intern_const(#s))
12184 unsigned int arg_size, local_size, stack_max;
12186 struct st_table *labels_table = st_init_numtable();
12188 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
12189 VALUE keywords = rb_hash_aref(params, SYM(keyword));
12191 DECL_ANCHOR(anchor);
12192 INIT_ANCHOR(anchor);
12195 ISEQ_BODY(iseq)->local_table_size =
len;
12196 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12198 for (i = 0; i <
len; i++) {
12201 if (sym_arg_rest == lv) {
12209#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12210 if (INT_PARAM(lead_num)) {
12211 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12213 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12214 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12215 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12216 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12219#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12221 INT_PARAM(arg_size);
12222 INT_PARAM(local_size);
12223 INT_PARAM(stack_max);
12228#ifdef USE_ISEQ_NODE_ID
12229 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
12237 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12239 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12242 for (i = 0; i <
len; i++) {
12244 LABEL *label = register_label(iseq, labels_table, ent);
12245 opt_table[i] = (
VALUE)label;
12248 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12249 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12252 else if (!
NIL_P(arg_opt_labels)) {
12253 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
12258 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12260 else if (!
NIL_P(keywords)) {
12261 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
12265 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
12266 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12269 if (
Qtrue == rb_hash_aref(params, SYM(use_block))) {
12270 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12273 if (int_param(&i, params, SYM(kwrest))) {
12274 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12275 if (keyword == NULL) {
12276 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12278 keyword->rest_start = i;
12279 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12282 iseq_calc_param_size(iseq);
12285 iseq_build_from_ary_exception(iseq, labels_table, exception);
12288 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12290 ISEQ_BODY(iseq)->param.size = arg_size;
12291 ISEQ_BODY(iseq)->local_table_size = local_size;
12292 ISEQ_BODY(iseq)->stack_max = stack_max;
12302 while (body->type == ISEQ_TYPE_BLOCK ||
12303 body->type == ISEQ_TYPE_RESCUE ||
12304 body->type == ISEQ_TYPE_ENSURE ||
12305 body->type == ISEQ_TYPE_EVAL ||
12306 body->type == ISEQ_TYPE_MAIN
12310 for (i = 0; i < body->local_table_size; i++) {
12311 if (body->local_table[i] ==
id) {
12315 iseq = body->parent_iseq;
12316 body = ISEQ_BODY(iseq);
12329 for (i=0; i<body->local_table_size; i++) {
12330 if (body->local_table[i] ==
id) {
12340#ifndef IBF_ISEQ_DEBUG
12341#define IBF_ISEQ_DEBUG 0
12344#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12345#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12348typedef uint32_t ibf_offset_t;
12349#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12351#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12353#define IBF_DEVEL_VERSION 4
12354#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12356#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12359static const char IBF_ENDIAN_MARK =
12360#ifdef WORDS_BIGENDIAN
12369 uint32_t major_version;
12370 uint32_t minor_version;
12372 uint32_t extra_size;
12374 uint32_t iseq_list_size;
12375 uint32_t global_object_list_size;
12376 ibf_offset_t iseq_list_offset;
12377 ibf_offset_t global_object_list_offset;
12398 unsigned int obj_list_size;
12399 ibf_offset_t obj_list_offset;
12418pinned_list_mark(
void *ptr)
12422 for (i = 0; i < list->size; i++) {
12423 if (list->buffer[i]) {
12424 rb_gc_mark(list->buffer[i]);
12436 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
12440pinned_list_fetch(
VALUE list,
long offset)
12446 if (offset >= ptr->size) {
12447 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12450 return ptr->buffer[offset];
12454pinned_list_store(
VALUE list,
long offset,
VALUE object)
12460 if (offset >= ptr->size) {
12461 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12468pinned_list_new(
long size)
12470 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
12471 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
12472 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
12478ibf_dump_pos(
struct ibf_dump *dump)
12480 long pos = RSTRING_LEN(dump->current_buffer->str);
12481#if SIZEOF_LONG > SIZEOF_INT
12482 if (pos >= UINT_MAX) {
12486 return (
unsigned int)pos;
12490ibf_dump_align(
struct ibf_dump *dump,
size_t align)
12492 ibf_offset_t pos = ibf_dump_pos(dump);
12494 static const char padding[
sizeof(
VALUE)];
12495 size_t size = align - ((size_t)pos % align);
12496#if SIZEOF_LONG > SIZEOF_INT
12497 if (pos + size >= UINT_MAX) {
12501 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
12502 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
12504 rb_str_cat(dump->current_buffer->str, padding, size);
12509ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
12511 ibf_offset_t pos = ibf_dump_pos(dump);
12512 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
12518ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
12520 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
12524ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
12526 VALUE str = dump->current_buffer->str;
12527 char *ptr = RSTRING_PTR(str);
12528 if ((
unsigned long)(size + offset) > (
unsigned long)RSTRING_LEN(str))
12529 rb_bug(
"ibf_dump_overwrite: overflow");
12530 memcpy(ptr + offset, buff, size);
12534ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
12536 ibf_offset_t beg = *offset;
12538 return load->current_buffer->buff + beg;
12542ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
12544 void *buff = ruby_xmalloc2(x, y);
12545 size_t size = x * y;
12546 memcpy(buff, load->current_buffer->buff + offset, size);
12550#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
12552#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
12553#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
12554#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
12555#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
12556#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
12559ibf_table_lookup(
struct st_table *table, st_data_t key)
12563 if (st_lookup(table, key, &val)) {
12572ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
12574 int index = ibf_table_lookup(table, key);
12577 index = (int)table->num_entries;
12578 st_insert(table, key, (st_data_t)index);
12586static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
12592ibf_dump_object_table_new(
void)
12594 st_table *obj_table = st_init_numtable();
12595 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
12603 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
12609 if (
id == 0 || rb_id2name(
id) == NULL) {
12612 return ibf_dump_object(dump,
rb_id2sym(
id));
12616ibf_load_id(
const struct ibf_load *load,
const ID id_index)
12618 if (id_index == 0) {
12621 VALUE sym = ibf_load_object(load, id_index);
12631static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
12636 if (iseq == NULL) {
12640 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
12644static unsigned char
12645ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
12647 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
12648 return (
unsigned char)load->current_buffer->buff[(*offset)++];
12664 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12665 ibf_dump_write(dump, &x,
sizeof(
VALUE));
12669 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12671 unsigned char bytes[max_byte_length];
12674 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
12675 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12681 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12684 ibf_dump_write(dump, bytes + max_byte_length - n, n);
12688ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
12690 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12691 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
12693 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
12694 *offset +=
sizeof(
VALUE);
12699 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12701 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
12702 const unsigned char c = buffer[*offset];
12706 c == 0 ? 9 : ntz_int32(c) + 1;
12709 if (*offset + n > load->current_buffer->size) {
12714 for (i = 1; i < n; i++) {
12716 x |= (
VALUE)buffer[*offset + i];
12730 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
12732 size_t len = strlen(bf->name);
12733 ibf_dump_write_small_value(dump, (
VALUE)
len);
12734 ibf_dump_write(dump, bf->name,
len);
12738ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
12740 int i = (int)ibf_load_small_value(load, offset);
12741 int len = (int)ibf_load_small_value(load, offset);
12742 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
12745 fprintf(stderr,
"%.*s!!\n",
len, name);
12749 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
12750 if (strncmp(table[i].name, name,
len) != 0) {
12751 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
12762 const int iseq_size = body->iseq_size;
12764 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
12766 ibf_offset_t offset = ibf_dump_pos(dump);
12768 for (code_index=0; code_index<iseq_size;) {
12769 const VALUE insn = orig_code[code_index++];
12770 const char *types = insn_op_types(insn);
12775 ibf_dump_write_small_value(dump, insn);
12778 for (op_index=0; types[op_index]; op_index++, code_index++) {
12779 VALUE op = orig_code[code_index];
12782 switch (types[op_index]) {
12785 wv = ibf_dump_object(dump, op);
12794 wv = ibf_dump_object(dump, arr);
12802 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
12810 wv = ibf_dump_id(dump, (
ID)op);
12822 ibf_dump_write_small_value(dump, wv);
12832ibf_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)
12835 unsigned int code_index;
12836 ibf_offset_t reading_pos = bytecode_offset;
12840 struct rb_call_data *cd_entries = load_body->call_data;
12843 iseq_bits_t * mark_offset_bits;
12845 iseq_bits_t tmp[1] = {0};
12847 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
12848 mark_offset_bits = tmp;
12851 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
12853 bool needs_bitmap =
false;
12855 for (code_index=0; code_index<iseq_size;) {
12857 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
12858 const char *types = insn_op_types(insn);
12864 for (op_index=0; types[op_index]; op_index++, code_index++) {
12865 const char operand_type = types[op_index];
12866 switch (operand_type) {
12869 VALUE op = ibf_load_small_value(load, &reading_pos);
12870 VALUE v = ibf_load_object(load, op);
12871 code[code_index] = v;
12874 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12875 needs_bitmap =
true;
12881 VALUE op = ibf_load_small_value(load, &reading_pos);
12882 VALUE v = ibf_load_object(load, op);
12883 v = rb_hash_dup(v);
12884 RHASH_TBL_RAW(v)->type = &cdhash_type;
12886 freeze_hide_obj(v);
12891 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
12893 code[code_index] = v;
12894 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12896 needs_bitmap =
true;
12901 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
12903 code[code_index] = v;
12906 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12907 needs_bitmap =
true;
12913 VALUE op = ibf_load_small_value(load, &reading_pos);
12914 VALUE arr = ibf_load_object(load, op);
12916 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
12917 ic->
segments = array_to_idlist(arr);
12919 code[code_index] = (
VALUE)ic;
12926 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12928 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
12929 code[code_index] = (
VALUE)ic;
12931 if (operand_type == TS_IVC) {
12934 if (insn == BIN(setinstancevariable)) {
12935 ID iv_name = (
ID)code[code_index - 1];
12936 cache->iv_set_name = iv_name;
12939 cache->iv_set_name = 0;
12942 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
12949 code[code_index] = (
VALUE)cd_entries++;
12954 VALUE op = ibf_load_small_value(load, &reading_pos);
12955 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
12962 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
12965 code[code_index] = ibf_load_small_value(load, &reading_pos);
12969 if (insn_len(insn) != op_index+1) {
12974 load_body->iseq_encoded = code;
12975 load_body->iseq_size = code_index;
12977 if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
12978 load_body->mark_bits.single = mark_offset_bits[0];
12981 if (needs_bitmap) {
12982 load_body->mark_bits.list = mark_offset_bits;
12985 load_body->mark_bits.list = 0;
12986 ruby_xfree(mark_offset_bits);
12991 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
12998 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
13001 IBF_W_ALIGN(
VALUE);
13002 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
13005 return ibf_dump_pos(dump);
13010ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
13014 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
13025 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
13028 struct rb_iseq_param_keyword dump_kw = *kw;
13029 int dv_num = kw->num - kw->required_num;
13034 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
13035 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
13037 dump_kw.table = IBF_W(ids,
ID, kw->num);
13038 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
13039 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
13040 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
13047static const struct rb_iseq_param_keyword *
13048ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
13050 if (param_keyword_offset) {
13051 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
13052 int dv_num = kw->num - kw->required_num;
13053 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
13056 for (i=0; i<dv_num; i++) {
13057 dvs[i] = ibf_load_object(load, dvs[i]);
13063 kw->default_values = dvs;
13074 ibf_offset_t offset = ibf_dump_pos(dump);
13078 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
13079 ibf_dump_write_small_value(dump, entries[i].line_no);
13080#ifdef USE_ISEQ_NODE_ID
13081 ibf_dump_write_small_value(dump, entries[i].node_id);
13083 ibf_dump_write_small_value(dump, entries[i].events);
13090ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
13092 ibf_offset_t reading_pos = body_offset;
13096 for (i = 0; i < size; i++) {
13097 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
13098#ifdef USE_ISEQ_NODE_ID
13099 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
13101 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
13108ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
13110 ibf_offset_t offset = ibf_dump_pos(dump);
13112 unsigned int last = 0;
13114 for (i = 0; i < size; i++) {
13115 ibf_dump_write_small_value(dump, positions[i] - last);
13116 last = positions[i];
13122static unsigned int *
13123ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
13125 ibf_offset_t reading_pos = positions_offset;
13126 unsigned int *positions =
ALLOC_N(
unsigned int, size);
13128 unsigned int last = 0;
13130 for (i = 0; i < size; i++) {
13131 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
13132 last = positions[i];
13142 const int size = body->local_table_size;
13146 for (i=0; i<size; i++) {
13147 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13150 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13156 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
13160ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13163 ID *table = IBF_R(local_table_offset,
ID, size);
13166 for (i=0; i<size; i++) {
13167 table[i] = ibf_load_id(load, table[i]);
13182 int *iseq_indices =
ALLOCA_N(
int, table->size);
13185 for (i=0; i<table->size; i++) {
13186 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13189 const ibf_offset_t offset = ibf_dump_pos(dump);
13191 for (i=0; i<table->size; i++) {
13192 ibf_dump_write_small_value(dump, iseq_indices[i]);
13193 ibf_dump_write_small_value(dump, table->entries[i].type);
13194 ibf_dump_write_small_value(dump, table->entries[i].start);
13195 ibf_dump_write_small_value(dump, table->entries[i].end);
13196 ibf_dump_write_small_value(dump, table->entries[i].cont);
13197 ibf_dump_write_small_value(dump, table->entries[i].sp);
13202 return ibf_dump_pos(dump);
13207ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size)
13210 struct iseq_catch_table *table = ruby_xmalloc(iseq_catch_table_bytes(size));
13211 table->size = size;
13213 ibf_offset_t reading_pos = catch_table_offset;
13216 for (i=0; i<table->size; i++) {
13217 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13218 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13219 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13220 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13221 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13222 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13224 table->entries[i].iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)iseq_index);
13237 const unsigned int ci_size = body->ci_size;
13240 ibf_offset_t offset = ibf_dump_pos(dump);
13244 for (i = 0; i < ci_size; i++) {
13247 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13248 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13249 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13253 int len = kwarg->keyword_len;
13254 ibf_dump_write_small_value(dump,
len);
13255 for (
int j=0; j<
len; j++) {
13256 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13257 ibf_dump_write_small_value(dump, keyword);
13261 ibf_dump_write_small_value(dump, 0);
13266 ibf_dump_write_small_value(dump, (
VALUE)-1);
13284static enum rb_id_table_iterator_result
13285store_outer_variable(
ID id,
VALUE val,
void *dump)
13290 pair->name = rb_id2str(
id);
13292 return ID_TABLE_CONTINUE;
13296outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13306 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13308 ibf_offset_t offset = ibf_dump_pos(dump);
13310 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13311 ibf_dump_write_small_value(dump, (
VALUE)size);
13320 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13322 for (
size_t i = 0; i < size; ++i) {
13323 ID id = ovlist->pairs[i].id;
13324 ID val = ovlist->pairs[i].val;
13325 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13326 ibf_dump_write_small_value(dump, val);
13335ibf_load_ci_entries(
const struct ibf_load *load,
13336 ibf_offset_t ci_entries_offset,
13337 unsigned int ci_size,
13340 ibf_offset_t reading_pos = ci_entries_offset;
13347 for (i = 0; i < ci_size; i++) {
13348 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
13349 if (mid_index != (
VALUE)-1) {
13350 ID mid = ibf_load_id(load, mid_index);
13351 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13352 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13355 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
13358 kwarg->references = 0;
13359 kwarg->keyword_len = kwlen;
13360 for (
int j=0; j<kwlen; j++) {
13361 VALUE keyword = ibf_load_small_value(load, &reading_pos);
13362 kwarg->keywords[j] = ibf_load_object(load, keyword);
13366 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
13368 cds[i].cc = vm_cc_empty();
13379ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
13381 ibf_offset_t reading_pos = outer_variables_offset;
13385 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
13387 if (table_size > 0) {
13388 tbl = rb_id_table_create(table_size);
13391 for (
size_t i = 0; i < table_size; i++) {
13392 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
13393 VALUE value = ibf_load_small_value(load, &reading_pos);
13394 if (!key) key = rb_make_temporary_id(i);
13395 rb_id_table_insert(tbl, key, value);
13404 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
13406 unsigned int *positions;
13410 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
13411 const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
13412 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
13414#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13415 ibf_offset_t iseq_start = ibf_dump_pos(dump);
13420 buffer.obj_table = ibf_dump_object_table_new();
13421 dump->current_buffer = &buffer;
13424 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
13425 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
13426 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
13427 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
13428 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
13430 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
13431 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
13432 ruby_xfree(positions);
13434 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
13435 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
13436 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
13437 const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
13438 const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
13439 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
13440 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
13441 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
13443#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13444 ibf_offset_t local_obj_list_offset;
13445 unsigned int local_obj_list_size;
13447 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
13450 ibf_offset_t body_offset = ibf_dump_pos(dump);
13453 unsigned int param_flags =
13454 (body->
param.flags.has_lead << 0) |
13455 (body->
param.flags.has_opt << 1) |
13456 (body->
param.flags.has_rest << 2) |
13457 (body->
param.flags.has_post << 3) |
13458 (body->
param.flags.has_kw << 4) |
13459 (body->
param.flags.has_kwrest << 5) |
13460 (body->
param.flags.has_block << 6) |
13461 (body->
param.flags.ambiguous_param0 << 7) |
13462 (body->
param.flags.accepts_no_kwarg << 8) |
13463 (body->
param.flags.ruby2_keywords << 9) |
13464 (body->
param.flags.anon_rest << 10) |
13465 (body->
param.flags.anon_kwrest << 11) |
13466 (body->
param.flags.use_block << 12) |
13467 (body->
param.flags.forwardable << 13) ;
13469#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13470# define IBF_BODY_OFFSET(x) (x)
13472# define IBF_BODY_OFFSET(x) (body_offset - (x))
13475 ibf_dump_write_small_value(dump, body->type);
13476 ibf_dump_write_small_value(dump, body->iseq_size);
13477 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
13478 ibf_dump_write_small_value(dump, bytecode_size);
13479 ibf_dump_write_small_value(dump, param_flags);
13480 ibf_dump_write_small_value(dump, body->
param.size);
13481 ibf_dump_write_small_value(dump, body->
param.lead_num);
13482 ibf_dump_write_small_value(dump, body->
param.opt_num);
13483 ibf_dump_write_small_value(dump, body->
param.rest_start);
13484 ibf_dump_write_small_value(dump, body->
param.post_start);
13485 ibf_dump_write_small_value(dump, body->
param.post_num);
13486 ibf_dump_write_small_value(dump, body->
param.block_start);
13487 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
13488 ibf_dump_write_small_value(dump, param_keyword_offset);
13489 ibf_dump_write_small_value(dump, location_pathobj_index);
13490 ibf_dump_write_small_value(dump, location_base_label_index);
13491 ibf_dump_write_small_value(dump, location_label_index);
13492 ibf_dump_write_small_value(dump, body->location.first_lineno);
13493 ibf_dump_write_small_value(dump, body->location.node_id);
13494 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
13495 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
13496 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
13497 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
13498 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
13499 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
13500 ibf_dump_write_small_value(dump, body->insns_info.size);
13501 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
13502 ibf_dump_write_small_value(dump, catch_table_size);
13503 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
13504 ibf_dump_write_small_value(dump, parent_iseq_index);
13505 ibf_dump_write_small_value(dump, local_iseq_index);
13506 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
13507 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
13508 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
13509 ibf_dump_write_small_value(dump, body->variable.flip_count);
13510 ibf_dump_write_small_value(dump, body->local_table_size);
13511 ibf_dump_write_small_value(dump, body->ivc_size);
13512 ibf_dump_write_small_value(dump, body->icvarc_size);
13513 ibf_dump_write_small_value(dump, body->ise_size);
13514 ibf_dump_write_small_value(dump, body->ic_size);
13515 ibf_dump_write_small_value(dump, body->ci_size);
13516 ibf_dump_write_small_value(dump, body->stack_max);
13517 ibf_dump_write_small_value(dump, body->builtin_attrs);
13518 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
13520#undef IBF_BODY_OFFSET
13522#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13523 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
13525 dump->current_buffer = saved_buffer;
13526 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
13528 ibf_offset_t offset = ibf_dump_pos(dump);
13529 ibf_dump_write_small_value(dump, iseq_start);
13530 ibf_dump_write_small_value(dump, iseq_length_bytes);
13531 ibf_dump_write_small_value(dump, body_offset);
13533 ibf_dump_write_small_value(dump, local_obj_list_offset);
13534 ibf_dump_write_small_value(dump, local_obj_list_size);
13536 st_free_table(buffer.obj_table);
13540 return body_offset;
13545ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
13547 VALUE str = ibf_load_object(load, str_index);
13549 str = rb_fstring(str);
13559 ibf_offset_t reading_pos = offset;
13561#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13563 load->current_buffer = &load->global_buffer;
13565 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13566 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13567 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13570 buffer.buff = load->global_buffer.buff + iseq_start;
13571 buffer.size = iseq_length_bytes;
13572 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13573 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13574 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
13576 load->current_buffer = &buffer;
13577 reading_pos = body_offset;
13580#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13581# define IBF_BODY_OFFSET(x) (x)
13583# define IBF_BODY_OFFSET(x) (offset - (x))
13586 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13587 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13588 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13589 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13590 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13591 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13592 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
13593 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
13594 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
13595 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
13596 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
13597 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
13598 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13599 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13600 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
13601 const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
13602 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
13603 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
13604 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
13605 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13606 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13607 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13608 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13609 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13610 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13611 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13612 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13613 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13614 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13615 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13616 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13617 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13618 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13619 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13620 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
13621 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13623 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13624 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13625 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13626 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13628 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13629 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13630 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13631 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
13634 VALUE path = ibf_load_object(load, location_pathobj_index);
13639 realpath = path = rb_fstring(path);
13642 VALUE pathobj = path;
13648 if (!
NIL_P(realpath)) {
13650 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
13651 "(%x), path=%+"PRIsVALUE,
13652 realpath,
TYPE(realpath), path);
13654 realpath = rb_fstring(realpath);
13660 rb_iseq_pathobj_set(iseq, path, realpath);
13665 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
13667#undef IBF_BODY_OFFSET
13669 load_body->type =
type;
13670 load_body->stack_max = stack_max;
13671 load_body->
param.flags.has_lead = (param_flags >> 0) & 1;
13672 load_body->
param.flags.has_opt = (param_flags >> 1) & 1;
13673 load_body->
param.flags.has_rest = (param_flags >> 2) & 1;
13674 load_body->
param.flags.has_post = (param_flags >> 3) & 1;
13675 load_body->
param.flags.has_kw = FALSE;
13676 load_body->
param.flags.has_kwrest = (param_flags >> 5) & 1;
13677 load_body->
param.flags.has_block = (param_flags >> 6) & 1;
13678 load_body->
param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
13679 load_body->
param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
13680 load_body->
param.flags.ruby2_keywords = (param_flags >> 9) & 1;
13681 load_body->
param.flags.anon_rest = (param_flags >> 10) & 1;
13682 load_body->
param.flags.anon_kwrest = (param_flags >> 11) & 1;
13683 load_body->
param.flags.use_block = (param_flags >> 12) & 1;
13684 load_body->
param.flags.forwardable = (param_flags >> 13) & 1;
13685 load_body->
param.size = param_size;
13686 load_body->
param.lead_num = param_lead_num;
13687 load_body->
param.opt_num = param_opt_num;
13688 load_body->
param.rest_start = param_rest_start;
13689 load_body->
param.post_start = param_post_start;
13690 load_body->
param.post_num = param_post_num;
13691 load_body->
param.block_start = param_block_start;
13692 load_body->local_table_size = local_table_size;
13693 load_body->ci_size = ci_size;
13694 load_body->insns_info.size = insns_info_size;
13696 ISEQ_COVERAGE_SET(iseq,
Qnil);
13697 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
13698 load_body->variable.flip_count = variable_flip_count;
13699 load_body->variable.script_lines =
Qnil;
13701 load_body->location.first_lineno = location_first_lineno;
13702 load_body->location.node_id = location_node_id;
13703 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
13704 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
13705 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
13706 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
13707 load_body->builtin_attrs = builtin_attrs;
13708 load_body->prism = prism;
13710 load_body->ivc_size = ivc_size;
13711 load_body->icvarc_size = icvarc_size;
13712 load_body->ise_size = ise_size;
13713 load_body->ic_size = ic_size;
13715 if (ISEQ_IS_SIZE(load_body)) {
13719 load_body->is_entries = NULL;
13721 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
13722 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
13723 load_body->
param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
13724 load_body->
param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
13725 load_body->
param.flags.has_kw = (param_flags >> 4) & 1;
13726 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
13727 load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
13728 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
13729 load_body->catch_table = ibf_load_catch_table(load, catch_table_offset, catch_table_size);
13730 load_body->parent_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)parent_iseq_index);
13731 load_body->local_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)local_iseq_index);
13732 load_body->mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
13735 if (load_body->
param.keyword != NULL) {
13737 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->
param.keyword;
13738 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
13741 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
13742#if VM_INSN_INFO_TABLE_IMPL == 2
13743 rb_iseq_insns_info_encode_positions(iseq);
13746 rb_iseq_translate_threaded_code(iseq);
13748#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13749 load->current_buffer = &load->global_buffer;
13752 RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
13753 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
13755#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13756 load->current_buffer = saved_buffer;
13758 verify_call_cache(iseq);
13761 rb_vm_pop_frame_no_int(ec);
13771ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
13776 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
13777 rb_ary_push(args->offset_list,
UINT2NUM(offset));
13779 return ST_CONTINUE;
13785 VALUE offset_list = rb_ary_hidden_new(dump->iseq_table->num_entries);
13789 args.offset_list = offset_list;
13791 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
13794 st_index_t size = dump->iseq_table->num_entries;
13795 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
13797 for (i = 0; i < size; i++) {
13801 ibf_dump_align(dump,
sizeof(ibf_offset_t));
13802 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
13803 header->iseq_list_size = (
unsigned int)size;
13806#define IBF_OBJECT_INTERNAL FL_PROMOTED0
13815 unsigned int type: 5;
13816 unsigned int special_const: 1;
13817 unsigned int frozen: 1;
13818 unsigned int internal: 1;
13821enum ibf_object_class_index {
13822 IBF_OBJECT_CLASS_OBJECT,
13823 IBF_OBJECT_CLASS_ARRAY,
13824 IBF_OBJECT_CLASS_STANDARD_ERROR,
13825 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
13826 IBF_OBJECT_CLASS_TYPE_ERROR,
13827 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
13837 long keyval[FLEX_ARY_LEN];
13850 BDIGIT digits[FLEX_ARY_LEN];
13853enum ibf_object_data_type {
13854 IBF_OBJECT_DATA_ENCODING,
13865#define IBF_ALIGNED_OFFSET(align, offset) \
13866 ((((offset) - 1) / (align) + 1) * (align))
13867#define IBF_OBJBODY(type, offset) (const type *)\
13868 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
13871ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
13873 if (offset >= load->current_buffer->size) {
13874 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
13876 return load->current_buffer->buff + offset;
13879NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
13882ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
13885 rb_raw_obj_info(buff,
sizeof(buff), obj);
13894 rb_raise(rb_eArgError,
"unsupported");
13901 enum ibf_object_class_index cindex;
13902 if (obj == rb_cObject) {
13903 cindex = IBF_OBJECT_CLASS_OBJECT;
13906 cindex = IBF_OBJECT_CLASS_ARRAY;
13909 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
13912 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
13915 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
13918 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
13921 rb_obj_info_dump(obj);
13923 rb_bug(
"unsupported class");
13925 ibf_dump_write_small_value(dump, (
VALUE)cindex);
13931 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
13934 case IBF_OBJECT_CLASS_OBJECT:
13936 case IBF_OBJECT_CLASS_ARRAY:
13938 case IBF_OBJECT_CLASS_STANDARD_ERROR:
13940 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
13942 case IBF_OBJECT_CLASS_TYPE_ERROR:
13944 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
13948 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
13956 (void)IBF_W(&dbl,
double, 1);
13962 const double *dblp = IBF_OBJBODY(
double, offset);
13969 long encindex = (long)rb_enc_get_index(obj);
13970 long len = RSTRING_LEN(obj);
13971 const char *ptr = RSTRING_PTR(obj);
13973 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
13974 rb_encoding *enc = rb_enc_from_index((
int)encindex);
13975 const char *enc_name = rb_enc_name(enc);
13976 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
13979 ibf_dump_write_small_value(dump, encindex);
13980 ibf_dump_write_small_value(dump,
len);
13981 IBF_WP(ptr,
char,
len);
13987 ibf_offset_t reading_pos = offset;
13989 int encindex = (int)ibf_load_small_value(load, &reading_pos);
13990 const long len = (long)ibf_load_small_value(load, &reading_pos);
13991 const char *ptr = load->current_buffer->buff + reading_pos;
13993 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
13994 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
13995 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
13999 if (header->frozen && !header->internal) {
14000 str = rb_enc_literal_str(ptr,
len, rb_enc_from_index(encindex));
14003 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
14006 if (header->frozen) str = rb_fstring(str);
14017 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
14019 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
14020 ibf_dump_write_small_value(dump, regexp.srcstr);
14027 regexp.option = ibf_load_byte(load, &offset);
14028 regexp.srcstr = ibf_load_small_value(load, &offset);
14030 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
14031 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
14043 ibf_dump_write_small_value(dump,
len);
14044 for (i=0; i<
len; i++) {
14045 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
14046 ibf_dump_write_small_value(dump, index);
14053 ibf_offset_t reading_pos = offset;
14055 const long len = (long)ibf_load_small_value(load, &reading_pos);
14057 VALUE ary = header->internal ? rb_ary_hidden_new(
len) : rb_ary_new_capa(
len);
14060 for (i=0; i<
len; i++) {
14061 const VALUE index = ibf_load_small_value(load, &reading_pos);
14062 rb_ary_push(ary, ibf_load_object(load, index));
14065 if (header->frozen) rb_ary_freeze(ary);
14071ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
14075 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14076 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14078 ibf_dump_write_small_value(dump, key_index);
14079 ibf_dump_write_small_value(dump, val_index);
14080 return ST_CONTINUE;
14087 ibf_dump_write_small_value(dump, (
VALUE)
len);
14095 long len = (long)ibf_load_small_value(load, &offset);
14096 VALUE obj = rb_hash_new_with_size(
len);
14099 for (i = 0; i <
len; i++) {
14100 VALUE key_index = ibf_load_small_value(load, &offset);
14101 VALUE val_index = ibf_load_small_value(load, &offset);
14103 VALUE key = ibf_load_object(load, key_index);
14104 VALUE val = ibf_load_object(load, val_index);
14105 rb_hash_aset(obj, key, val);
14107 rb_hash_rehash(obj);
14123 range.class_index = 0;
14126 range.beg = (long)ibf_dump_object(dump, beg);
14127 range.end = (long)ibf_dump_object(dump, end);
14133 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
14142 VALUE beg = ibf_load_object(load, range->beg);
14143 VALUE end = ibf_load_object(load, range->end);
14153 ssize_t
len = BIGNUM_LEN(obj);
14154 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14155 BDIGIT *d = BIGNUM_DIGITS(obj);
14157 (void)IBF_W(&slen, ssize_t, 1);
14158 IBF_WP(d, BDIGIT,
len);
14165 int sign = bignum->slen > 0;
14166 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14167 const int big_unpack_flags =
14170 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
14181 if (rb_data_is_encoding(obj)) {
14183 const char *name = rb_enc_name(enc);
14184 long len = strlen(name) + 1;
14186 data[0] = IBF_OBJECT_DATA_ENCODING;
14188 (void)IBF_W(data,
long, 2);
14189 IBF_WP(name,
char,
len);
14192 ibf_dump_object_unsupported(dump, obj);
14199 const long *body = IBF_OBJBODY(
long, offset);
14200 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14202 const char *data = (
const char *)&body[2];
14205 case IBF_OBJECT_DATA_ENCODING:
14207 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
14212 return ibf_load_object_unsupported(load, header, offset);
14216ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14219 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14220 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14222 (void)IBF_W(data,
long, 2);
14226ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14229 VALUE a = ibf_load_object(load, nums->a);
14230 VALUE b = ibf_load_object(load, nums->b);
14242 ibf_dump_object_string(dump,
rb_sym2str(obj));
14248 ibf_offset_t reading_pos = offset;
14250 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14251 const long len = (long)ibf_load_small_value(load, &reading_pos);
14252 const char *ptr = load->current_buffer->buff + reading_pos;
14254 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14255 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14256 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14259 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
14263typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
14264static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
14265 ibf_dump_object_unsupported,
14266 ibf_dump_object_unsupported,
14267 ibf_dump_object_class,
14268 ibf_dump_object_unsupported,
14269 ibf_dump_object_float,
14270 ibf_dump_object_string,
14271 ibf_dump_object_regexp,
14272 ibf_dump_object_array,
14273 ibf_dump_object_hash,
14274 ibf_dump_object_struct,
14275 ibf_dump_object_bignum,
14276 ibf_dump_object_unsupported,
14277 ibf_dump_object_data,
14278 ibf_dump_object_unsupported,
14279 ibf_dump_object_complex_rational,
14280 ibf_dump_object_complex_rational,
14281 ibf_dump_object_unsupported,
14282 ibf_dump_object_unsupported,
14283 ibf_dump_object_unsupported,
14284 ibf_dump_object_unsupported,
14285 ibf_dump_object_symbol,
14286 ibf_dump_object_unsupported,
14287 ibf_dump_object_unsupported,
14288 ibf_dump_object_unsupported,
14289 ibf_dump_object_unsupported,
14290 ibf_dump_object_unsupported,
14291 ibf_dump_object_unsupported,
14292 ibf_dump_object_unsupported,
14293 ibf_dump_object_unsupported,
14294 ibf_dump_object_unsupported,
14295 ibf_dump_object_unsupported,
14296 ibf_dump_object_unsupported,
14302 unsigned char byte =
14303 (header.type << 0) |
14304 (header.special_const << 5) |
14305 (header.frozen << 6) |
14306 (header.internal << 7);
14312ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
14314 unsigned char byte = ibf_load_byte(load, offset);
14317 header.type = (
byte >> 0) & 0x1f;
14318 header.special_const = (
byte >> 5) & 0x01;
14319 header.frozen = (
byte >> 6) & 0x01;
14320 header.internal = (
byte >> 7) & 0x01;
14329 ibf_offset_t current_offset;
14330 IBF_ZERO(obj_header);
14331 obj_header.type =
TYPE(obj);
14333 IBF_W_ALIGN(ibf_offset_t);
14334 current_offset = ibf_dump_pos(dump);
14339 obj_header.special_const = TRUE;
14340 obj_header.frozen = TRUE;
14341 obj_header.internal = TRUE;
14342 ibf_dump_object_object_header(dump, obj_header);
14343 ibf_dump_write_small_value(dump, obj);
14347 obj_header.special_const = FALSE;
14349 ibf_dump_object_object_header(dump, obj_header);
14350 (*dump_object_functions[obj_header.type])(dump, obj);
14353 return current_offset;
14357static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
14358 ibf_load_object_unsupported,
14359 ibf_load_object_unsupported,
14360 ibf_load_object_class,
14361 ibf_load_object_unsupported,
14362 ibf_load_object_float,
14363 ibf_load_object_string,
14364 ibf_load_object_regexp,
14365 ibf_load_object_array,
14366 ibf_load_object_hash,
14367 ibf_load_object_struct,
14368 ibf_load_object_bignum,
14369 ibf_load_object_unsupported,
14370 ibf_load_object_data,
14371 ibf_load_object_unsupported,
14372 ibf_load_object_complex_rational,
14373 ibf_load_object_complex_rational,
14374 ibf_load_object_unsupported,
14375 ibf_load_object_unsupported,
14376 ibf_load_object_unsupported,
14377 ibf_load_object_unsupported,
14378 ibf_load_object_symbol,
14379 ibf_load_object_unsupported,
14380 ibf_load_object_unsupported,
14381 ibf_load_object_unsupported,
14382 ibf_load_object_unsupported,
14383 ibf_load_object_unsupported,
14384 ibf_load_object_unsupported,
14385 ibf_load_object_unsupported,
14386 ibf_load_object_unsupported,
14387 ibf_load_object_unsupported,
14388 ibf_load_object_unsupported,
14389 ibf_load_object_unsupported,
14393ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
14395 if (object_index == 0) {
14399 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
14401 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
14402 ibf_offset_t offset = offsets[object_index];
14403 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
14406 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
14407 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
14408 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
14409 header.type, header.special_const, header.frozen, header.internal);
14411 if (offset >= load->current_buffer->size) {
14412 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
14415 if (header.special_const) {
14416 ibf_offset_t reading_pos = offset;
14418 obj = ibf_load_small_value(load, &reading_pos);
14421 obj = (*load_object_functions[header.type])(load, &header, offset);
14424 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
14427 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
14428 object_index, obj);
14441ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14446 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
14447 rb_ary_push(args->offset_list,
UINT2NUM(offset));
14449 return ST_CONTINUE;
14453ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
14455 st_table *obj_table = dump->current_buffer->obj_table;
14456 VALUE offset_list = rb_ary_hidden_new(obj_table->num_entries);
14460 args.offset_list = offset_list;
14462 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
14464 IBF_W_ALIGN(ibf_offset_t);
14465 *obj_list_offset = ibf_dump_pos(dump);
14467 st_index_t size = obj_table->num_entries;
14470 for (i=0; i<size; i++) {
14475 *obj_list_size = (
unsigned int)size;
14479ibf_dump_mark(
void *ptr)
14482 rb_gc_mark(dump->global_buffer.str);
14484 rb_mark_set(dump->global_buffer.obj_table);
14485 rb_mark_set(dump->iseq_table);
14489ibf_dump_free(
void *ptr)
14492 if (dump->global_buffer.obj_table) {
14493 st_free_table(dump->global_buffer.obj_table);
14494 dump->global_buffer.obj_table = 0;
14496 if (dump->iseq_table) {
14497 st_free_table(dump->iseq_table);
14498 dump->iseq_table = 0;
14503ibf_dump_memsize(
const void *ptr)
14507 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
14508 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
14514 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
14515 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
14521 dump->global_buffer.obj_table = NULL;
14522 dump->iseq_table = NULL;
14525 dump->global_buffer.obj_table = ibf_dump_object_table_new();
14526 dump->iseq_table = st_init_numtable();
14528 dump->current_buffer = &dump->global_buffer;
14539 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
14540 ISEQ_BODY(iseq)->local_iseq != iseq) {
14543 if (
RTEST(ISEQ_COVERAGE(iseq))) {
14548 ibf_dump_setup(dump, dump_obj);
14550 ibf_dump_write(dump, &header,
sizeof(header));
14551 ibf_dump_iseq(dump, iseq);
14553 header.magic[0] =
'Y';
14554 header.magic[1] =
'A';
14555 header.magic[2] =
'R';
14556 header.magic[3] =
'B';
14557 header.major_version = IBF_MAJOR_VERSION;
14558 header.minor_version = IBF_MINOR_VERSION;
14559 header.endian = IBF_ENDIAN_MARK;
14561 ibf_dump_iseq_list(dump, &header);
14562 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
14563 header.size = ibf_dump_pos(dump);
14566 VALUE opt_str = opt;
14569 ibf_dump_write(dump, ptr, header.extra_size);
14572 header.extra_size = 0;
14575 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
14577 str = dump->global_buffer.str;
14582static const ibf_offset_t *
14583ibf_iseq_list(
const struct ibf_load *load)
14585 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
14589rb_ibf_load_iseq_complete(
rb_iseq_t *iseq)
14593 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
14596 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
14597 iseq->aux.loader.index, offset,
14598 load->header->size);
14600 ibf_load_iseq_each(load, iseq, offset);
14601 ISEQ_COMPILE_DATA_CLEAR(iseq);
14603 rb_iseq_init_trace(iseq);
14604 load->iseq = prev_src_iseq;
14611 rb_ibf_load_iseq_complete((
rb_iseq_t *)iseq);
14619 int iseq_index = (int)(
VALUE)index_iseq;
14622 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
14623 (
void *)index_iseq, (
void *)load->iseq_list);
14625 if (iseq_index == -1) {
14629 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
14632 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
14640 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
14643 iseq->aux.loader.obj = load->loader_obj;
14644 iseq->aux.loader.index = iseq_index;
14646 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
14647 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
14649 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
14651 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
14653 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
14655 rb_ibf_load_iseq_complete(iseq);
14659 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
14660 (
void *)iseq, (
void *)load->iseq);
14668ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
14671 load->loader_obj = loader_obj;
14672 load->global_buffer.buff = bytes;
14673 load->header = header;
14674 load->global_buffer.size = header->size;
14675 load->global_buffer.obj_list_offset = header->global_object_list_offset;
14676 load->global_buffer.obj_list_size = header->global_object_list_size;
14677 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
14678 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
14681 load->current_buffer = &load->global_buffer;
14683 if (size < header->size) {
14686 if (strncmp(header->magic,
"YARB", 4) != 0) {
14689 if (header->major_version != IBF_MAJOR_VERSION ||
14690 header->minor_version != IBF_MINOR_VERSION) {
14692 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
14694 if (header->endian != IBF_ENDIAN_MARK) {
14700 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14701 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
14702 header->iseq_list_offset);
14704 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14705 rb_raise(rb_eArgError,
"unaligned object list offset: %u",
14706 load->global_buffer.obj_list_offset);
14719 if (USE_LAZY_LOAD) {
14720 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
14723 ibf_load_setup_bytes(load, loader_obj, RSTRING_PTR(str), RSTRING_LEN(str));
14728ibf_loader_mark(
void *ptr)
14731 rb_gc_mark(load->str);
14732 rb_gc_mark(load->iseq_list);
14733 rb_gc_mark(load->global_buffer.obj_list);
14737ibf_loader_free(
void *ptr)
14744ibf_loader_memsize(
const void *ptr)
14751 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
14752 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
14756rb_iseq_ibf_load(
VALUE str)
14762 ibf_load_setup(load, loader_obj, str);
14763 iseq = ibf_load_iseq(load, 0);
14770rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
14776 ibf_load_setup_bytes(load, loader_obj, bytes, size);
14777 iseq = ibf_load_iseq(load, 0);
14784rb_iseq_ibf_load_extra_data(
VALUE str)
14790 ibf_load_setup(load, loader_obj, str);
14791 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
14796#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 rb_str_cat2
Old name of rb_str_cat_cstr.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define 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.
VALUE rb_eNotImpError
NotImplementedError exception.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_eSyntaxError
SyntaxError exception.
@ 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.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define INTEGER_PACK_NEGATIVE
Interprets the input as a signed negative number (unpack only).
#define INTEGER_PACK_LSWORD_FIRST
Stores/interprets the least significant word as the first word.
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
int rb_is_attrset_id(ID id)
Classifies the given ID, then sees if it is an attribute writer.
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
VALUE rb_range_new(VALUE beg, VALUE end, int excl)
Creates a new Range.
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.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_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.
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.
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.
#define rb_long2int
Just another name of rb_long2int_inline.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define ALLOCA_N(type, n)
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RB_ALLOCV(v, n)
Identical to RB_ALLOCV_N(), except that it allocates a number of bytes and returns a void* .
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define 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 int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_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.