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;
621 rb_hash_aset(structure, key, branch);
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();
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);
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);
1444insn_replace_with_operands(
rb_iseq_t *iseq,
INSN *iobj,
enum ruby_vminsn_type insn_id,
int argc, ...)
1446 VALUE *operands = 0;
1450 va_start(argv, argc);
1451 operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
1452 for (i = 0; i < argc; i++) {
1459 iobj->insn_id = insn_id;
1460 iobj->operand_size = argc;
1461 iobj->operands = operands;
1462 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1470 VM_ASSERT(argc >= 0);
1473 flag |= VM_CALL_KWARG;
1474 argc += kw_arg->keyword_len;
1477 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING))
1478 && !has_blockiseq) {
1479 flag |= VM_CALL_ARGS_SIMPLE;
1482 ISEQ_BODY(iseq)->ci_size++;
1483 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1491 VALUE *operands = compile_data_calloc2(iseq,
sizeof(
VALUE), 2);
1494 operands[1] = (
VALUE)blockiseq;
1501 if (vm_ci_flag((
struct rb_callinfo *)ci) & VM_CALL_FORWARDING) {
1502 insn = new_insn_core(iseq, line_no, node_id, BIN(sendforward), 2, operands);
1505 insn = new_insn_core(iseq, line_no, node_id, BIN(send), 2, operands);
1518 VALUE ast_value = rb_ruby_ast_new(node);
1520 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1521 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1522 ret_iseq = rb_iseq_new_with_opt(ast_value, name,
1523 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1525 isolated_depth ? isolated_depth + 1 : 0,
1526 type, ISEQ_COMPILE_DATA(iseq)->option,
1527 ISEQ_BODY(iseq)->variable.script_lines);
1528 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1538 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1539 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1540 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1541 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1542 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1550 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1551 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1552 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1553 set_catch_except_p((
rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1576 while (pos < body->iseq_size) {
1577 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1578 if (insn == BIN(
throw)) {
1579 set_catch_except_p(iseq);
1582 pos += insn_len(insn);
1588 for (i = 0; i < ct->size; i++) {
1590 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1591 if (entry->type != CATCH_TYPE_BREAK
1592 && entry->type != CATCH_TYPE_NEXT
1593 && entry->type != CATCH_TYPE_REDO) {
1595 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1602iseq_insert_nop_between_end_and_cont(
rb_iseq_t *iseq)
1604 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1605 if (
NIL_P(catch_table_ary))
return;
1606 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1608 for (i = 0; i < tlen; i++) {
1614 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1616 if (ct != CATCH_TYPE_BREAK
1617 && ct != CATCH_TYPE_NEXT
1618 && ct != CATCH_TYPE_REDO) {
1620 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1622 INSN *nop = new_insn_core(iseq, 0, -1, BIN(nop), 0, 0);
1623 ELEM_INSERT_NEXT(end, &nop->link);
1636 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1641 if (compile_debug > 5)
1642 dump_disasm_list(FIRST_ELEMENT(anchor));
1644 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1645 iseq_optimize(iseq, anchor);
1647 if (compile_debug > 5)
1648 dump_disasm_list(FIRST_ELEMENT(anchor));
1650 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1651 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1652 iseq_insns_unification(iseq, anchor);
1653 if (compile_debug > 5)
1654 dump_disasm_list(FIRST_ELEMENT(anchor));
1657 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1658 iseq_insert_nop_between_end_and_cont(iseq);
1659 if (compile_debug > 5)
1660 dump_disasm_list(FIRST_ELEMENT(anchor));
1668 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1671 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1672 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1673 if (compile_debug > 5)
1674 dump_disasm_list(FIRST_ELEMENT(anchor));
1676 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1677 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1679 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1680 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1682 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1683 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1685 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1687 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1689 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1691 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1692 xfree(ISEQ_BODY(iseq)->catch_table);
1693 ISEQ_BODY(iseq)->catch_table = NULL;
1696#if VM_INSN_INFO_TABLE_IMPL == 2
1697 if (ISEQ_BODY(iseq)->insns_info.succ_index_table == NULL) {
1698 debugs(
"[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
1699 rb_iseq_insns_info_encode_positions(iseq);
1703 if (compile_debug > 1) {
1704 VALUE str = rb_iseq_disasm(iseq);
1707 verify_call_cache(iseq);
1708 debugs(
"[compile step: finish]\n");
1714iseq_set_exception_local_table(
rb_iseq_t *iseq)
1716 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1717 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1725 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1727 iseq = ISEQ_BODY(iseq)->parent_iseq;
1733get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1737 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1738 if (ISEQ_BODY(iseq)->local_table[i] == id) {
1748 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1751 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1752 "get_local_var_idx: %d", idx);
1759get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1761 int lv = 0, idx = -1;
1762 const rb_iseq_t *
const topmost_iseq = iseq;
1765 idx = get_dyna_var_idx_at_raw(iseq,
id);
1769 iseq = ISEQ_BODY(iseq)->parent_iseq;
1774 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1775 "get_dyna_var_idx: -1");
1779 *ls = ISEQ_BODY(iseq)->local_table_size;
1784iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1788 iseq = ISEQ_BODY(iseq)->parent_iseq;
1791 body = ISEQ_BODY(iseq);
1792 if (body->local_iseq == iseq &&
1793 body->
param.flags.has_block &&
1794 body->local_table_size - body->
param.block_start == idx) {
1803iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1806 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1807 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1818access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1820 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1822 if (isolated_depth && level >= isolated_depth) {
1823 if (
id == rb_intern(
"yield")) {
1824 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1827 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable '%s' from isolated Proc", rb_id2name(
id));
1831 for (
int i=0; i<level; i++) {
1833 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1836 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1839 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1840 if (write && !val) {
1841 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1845 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1848 iseq = ISEQ_BODY(iseq)->parent_iseq;
1853iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1855 for (
int i=0; i<level; i++) {
1856 iseq = ISEQ_BODY(iseq)->parent_iseq;
1859 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1867 if (iseq_local_block_param_p(iseq, idx, level)) {
1868 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1871 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1873 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1879 if (iseq_local_block_param_p(iseq, idx, level)) {
1880 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1883 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1885 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
1894 if (body->
param.flags.has_opt ||
1895 body->
param.flags.has_post ||
1896 body->
param.flags.has_rest ||
1897 body->
param.flags.has_block ||
1898 body->
param.flags.has_kw ||
1899 body->
param.flags.has_kwrest) {
1901 if (body->
param.flags.has_block) {
1902 body->
param.size = body->
param.block_start + 1;
1904 else if (body->
param.flags.has_kwrest) {
1905 body->
param.size = body->
param.keyword->rest_start + 1;
1907 else if (body->
param.flags.has_kw) {
1908 body->
param.size = body->
param.keyword->bits_start + 1;
1910 else if (body->
param.flags.has_post) {
1913 else if (body->
param.flags.has_rest) {
1914 body->
param.size = body->
param.rest_start + 1;
1916 else if (body->
param.flags.has_opt) {
1934 struct rb_iseq_param_keyword *keyword;
1937 int kw = 0, rkw = 0, di = 0, i;
1939 body->
param.flags.has_kw = TRUE;
1940 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
1944 node = node->nd_next;
1947 keyword->bits_start = arg_size++;
1949 node = args->kw_args;
1951 const NODE *val_node = get_nd_value(node->nd_body);
1954 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
1958 switch (nd_type(val_node)) {
1960 dv = rb_node_sym_string_val(val_node);
1963 dv = rb_node_regx_string_val(val_node);
1966 dv = rb_node_line_lineno_val(val_node);
1969 dv = rb_node_integer_literal_val(val_node);
1972 dv = rb_node_float_literal_val(val_node);
1975 dv = rb_node_rational_literal_val(val_node);
1977 case NODE_IMAGINARY:
1978 dv = rb_node_imaginary_literal_val(val_node);
1981 dv = rb_node_encoding_val(val_node);
1993 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
1997 keyword->num = ++di;
2001 node = node->nd_next;
2006 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
2007 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2008 keyword->rest_start = arg_size++;
2009 body->
param.flags.has_kwrest = TRUE;
2011 if (kw_id == idPow) body->
param.flags.anon_kwrest = TRUE;
2013 keyword->required_num = rkw;
2014 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
2019 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
2021 if (dv == complex_mark) dv =
Qundef;
2025 keyword->default_values = dvs;
2034 if (!body->
param.flags.use_block) {
2035 body->
param.flags.use_block = 1;
2040 st_data_t key = (st_data_t)rb_intern_str(body->location.label);
2041 set_insert(vm->unused_block_warning_table, key);
2049 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2053 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2059 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2061 body->
param.flags.ruby2_keywords = args->ruby2_keywords;
2062 body->
param.lead_num = arg_size = (int)args->pre_args_num;
2063 if (body->
param.lead_num > 0) body->
param.flags.has_lead = TRUE;
2064 debugs(
" - argc: %d\n", body->
param.lead_num);
2066 rest_id = args->rest_arg;
2067 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2071 block_id = args->block_arg;
2073 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2075 if (optimized_forward) {
2080 if (args->opt_args) {
2088 label = NEW_LABEL(nd_line(RNODE(node)));
2090 ADD_LABEL(optargs, label);
2091 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2092 node = node->nd_next;
2097 label = NEW_LABEL(nd_line(node_args));
2099 ADD_LABEL(optargs, label);
2104 for (j = 0; j < i+1; j++) {
2109 body->
param.flags.has_opt = TRUE;
2110 body->
param.opt_num = i;
2111 body->
param.opt_table = opt_table;
2116 body->
param.rest_start = arg_size++;
2117 body->
param.flags.has_rest = TRUE;
2118 if (rest_id ==
'*') body->
param.flags.anon_rest = TRUE;
2122 if (args->first_post_arg) {
2123 body->
param.post_start = arg_size;
2124 body->
param.post_num = args->post_args_num;
2125 body->
param.flags.has_post = TRUE;
2126 arg_size += args->post_args_num;
2128 if (body->
param.flags.has_rest) {
2129 body->
param.post_start = body->
param.rest_start + 1;
2133 if (args->kw_args) {
2134 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2136 else if (args->kw_rest_arg && !optimized_forward) {
2137 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2138 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2139 keyword->rest_start = arg_size++;
2140 body->
param.keyword = keyword;
2141 body->
param.flags.has_kwrest = TRUE;
2143 static ID anon_kwrest = 0;
2144 if (!anon_kwrest) anon_kwrest = rb_intern(
"**");
2145 if (kw_id == anon_kwrest) body->
param.flags.anon_kwrest = TRUE;
2147 else if (args->no_kwarg) {
2148 body->
param.flags.accepts_no_kwarg = TRUE;
2152 body->
param.block_start = arg_size++;
2153 body->
param.flags.has_block = TRUE;
2154 iseq_set_use_block(iseq);
2158 if (optimized_forward) {
2159 body->
param.flags.use_block = 1;
2160 body->
param.flags.forwardable = TRUE;
2164 iseq_calc_param_size(iseq);
2165 body->
param.size = arg_size;
2167 if (args->pre_init) {
2168 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2170 if (args->post_init) {
2171 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2174 if (body->type == ISEQ_TYPE_BLOCK) {
2175 if (body->
param.flags.has_opt == FALSE &&
2176 body->
param.flags.has_post == FALSE &&
2177 body->
param.flags.has_rest == FALSE &&
2178 body->
param.flags.has_kw == FALSE &&
2179 body->
param.flags.has_kwrest == FALSE) {
2181 if (body->
param.lead_num == 1 && last_comma == 0) {
2183 body->
param.flags.ambiguous_param0 = TRUE;
2195 unsigned int size = tbl ? tbl->size : 0;
2196 unsigned int offset = 0;
2199 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2204 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2213 MEMCPY(ids, tbl->ids + offset,
ID, size);
2214 ISEQ_BODY(iseq)->local_table = ids;
2216 ISEQ_BODY(iseq)->local_table_size = size;
2218 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2230 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2233 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2236 else if (tlit != tval) {
2246 long x =
FIX2LONG(rb_big_cmp(lit, val));
2254 return rb_float_cmp(lit, val);
2257 const struct RRational *rat1 = RRATIONAL(val);
2258 const struct RRational *rat2 = RRATIONAL(lit);
2259 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2262 const struct RComplex *comp1 = RCOMPLEX(val);
2263 const struct RComplex *comp2 = RCOMPLEX(lit);
2264 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2267 return rb_reg_equal(val, lit) ? 0 : -1;
2275rb_iseq_cdhash_hash(
VALUE a)
2277 switch (OBJ_BUILTIN_TYPE(a)) {
2280 return (st_index_t)a;
2288 return rb_rational_hash(a);
2290 return rb_complex_hash(a);
2300 rb_iseq_cdhash_hash,
2314 rb_hash_aset(data->hash, key,
INT2FIX(lobj->position - (data->pos+data->len)));
2322 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2329 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2331 if (rb_id_table_lookup(tbl,
id,&val)) {
2336 tbl = rb_id_table_create(1);
2337 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2339 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2340 rb_id_table_insert(tbl,
id,val);
2344#define BADINSN_DUMP(anchor, list, dest) \
2345 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2347#define BADINSN_ERROR \
2348 (xfree(generated_iseq), \
2349 xfree(insns_info), \
2350 BADINSN_DUMP(anchor, list, NULL), \
2356 int stack_max = 0, sp = 0, line = 0;
2359 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2360 if (IS_LABEL(list)) {
2366 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2367 switch (list->type) {
2368 case ISEQ_ELEMENT_INSN:
2376 sp = calc_sp_depth(sp, iobj);
2378 BADINSN_DUMP(anchor, list, NULL);
2379 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2380 "argument stack underflow (%d)", sp);
2383 if (sp > stack_max) {
2387 line = iobj->insn_info.line_no;
2389 operands = iobj->operands;
2390 insn = iobj->insn_id;
2391 types = insn_op_types(insn);
2392 len = insn_len(insn);
2395 if (iobj->operand_size !=
len - 1) {
2397 BADINSN_DUMP(anchor, list, NULL);
2398 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2399 "operand size miss! (%d for %d)",
2400 iobj->operand_size,
len - 1);
2404 for (j = 0; types[j]; j++) {
2405 if (types[j] == TS_OFFSET) {
2409 BADINSN_DUMP(anchor, list, NULL);
2410 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2411 "unknown label: "LABEL_FORMAT, lobj->label_no);
2414 if (lobj->sp == -1) {
2417 else if (lobj->sp != sp) {
2418 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2419 RSTRING_PTR(rb_iseq_path(iseq)), line,
2420 lobj->label_no, lobj->sp, sp);
2426 case ISEQ_ELEMENT_LABEL:
2429 if (lobj->sp == -1) {
2433 if (lobj->sp != sp) {
2434 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2435 RSTRING_PTR(rb_iseq_path(iseq)), line,
2436 lobj->label_no, lobj->sp, sp);
2442 case ISEQ_ELEMENT_TRACE:
2447 case ISEQ_ELEMENT_ADJUST:
2452 sp = adjust->label ? adjust->label->sp : 0;
2453 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2454 BADINSN_DUMP(anchor, list, NULL);
2455 COMPILE_ERROR(iseq, adjust->line_no,
2456 "iseq_set_sequence: adjust bug %d < %d",
2463 BADINSN_DUMP(anchor, list, NULL);
2464 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2473 int insns_info_index,
int code_index,
const INSN *iobj)
2475 if (insns_info_index == 0 ||
2476 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2477#ifdef USE_ISEQ_NODE_ID
2478 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2480 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2481 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2482#ifdef USE_ISEQ_NODE_ID
2483 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2485 insns_info[insns_info_index].events = iobj->insn_info.events;
2486 positions[insns_info_index] = code_index;
2494 int insns_info_index,
int code_index,
const ADJUST *adjust)
2496 insns_info[insns_info_index].line_no = adjust->line_no;
2497 insns_info[insns_info_index].node_id = -1;
2498 insns_info[insns_info_index].events = 0;
2499 positions[insns_info_index] = code_index;
2504array_to_idlist(
VALUE arr)
2509 for (
long i = 0; i < size; i++) {
2518idlist_to_array(
const ID *ids)
2535 unsigned int *positions;
2537 VALUE *generated_iseq;
2541 int insn_num, code_index, insns_info_index, sp = 0;
2542 int stack_max = fix_sp_depth(iseq, anchor);
2544 if (stack_max < 0)
return COMPILE_NG;
2547 insn_num = code_index = 0;
2548 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2549 switch (list->type) {
2550 case ISEQ_ELEMENT_INSN:
2554 sp = calc_sp_depth(sp, iobj);
2556 events = iobj->insn_info.events |= events;
2557 if (ISEQ_COVERAGE(iseq)) {
2558 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2559 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2560 int line = iobj->insn_info.line_no - 1;
2561 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2565 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2566 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2572 code_index += insn_data_length(iobj);
2577 case ISEQ_ELEMENT_LABEL:
2580 lobj->position = code_index;
2581 if (lobj->sp != sp) {
2582 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2583 RSTRING_PTR(rb_iseq_path(iseq)),
2584 lobj->label_no, lobj->sp, sp);
2589 case ISEQ_ELEMENT_TRACE:
2592 events |= trace->event;
2593 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2596 case ISEQ_ELEMENT_ADJUST:
2599 if (adjust->line_no != -1) {
2601 sp = adjust->label ? adjust->label->sp : 0;
2602 if (orig_sp - sp > 0) {
2603 if (orig_sp - sp > 1) code_index++;
2617 positions =
ALLOC_N(
unsigned int, insn_num);
2618 if (ISEQ_IS_SIZE(body)) {
2622 body->is_entries = NULL;
2625 if (body->ci_size) {
2629 body->call_data = NULL;
2631 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2638 iseq_bits_t * mark_offset_bits;
2639 int code_size = code_index;
2641 bool needs_bitmap =
false;
2643 if (ISEQ_MBITS_BUFLEN(code_index) == 1) {
2644 mark_offset_bits = &ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2645 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
true;
2648 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(code_index));
2649 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = mark_offset_bits;
2650 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
false;
2653 ISEQ_COMPILE_DATA(iseq)->iseq_encoded = (
void *)generated_iseq;
2654 ISEQ_COMPILE_DATA(iseq)->iseq_size = code_index;
2656 list = FIRST_ELEMENT(anchor);
2657 insns_info_index = code_index = sp = 0;
2660 switch (list->type) {
2661 case ISEQ_ELEMENT_INSN:
2669 sp = calc_sp_depth(sp, iobj);
2671 operands = iobj->operands;
2672 insn = iobj->insn_id;
2673 generated_iseq[code_index] = insn;
2674 types = insn_op_types(insn);
2675 len = insn_len(insn);
2677 for (j = 0; types[j]; j++) {
2678 char type = types[j];
2686 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2691 VALUE map = operands[j];
2694 data.pos = code_index;
2698 rb_hash_rehash(map);
2699 freeze_hide_obj(map);
2700 generated_iseq[code_index + 1 + j] = map;
2701 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2703 needs_bitmap =
true;
2708 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2713 VALUE v = operands[j];
2714 generated_iseq[code_index + 1 + j] = v;
2718 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2719 needs_bitmap =
true;
2726 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2727 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2728 if (UNLIKELY(ic_index >= body->ic_size)) {
2729 BADINSN_DUMP(anchor, &iobj->link, 0);
2730 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2731 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2732 ic_index, ISEQ_IS_SIZE(body));
2735 ic->
segments = array_to_idlist(operands[j]);
2737 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2742 unsigned int ic_index =
FIX2UINT(operands[j]);
2744 IVC cache = ((
IVC)&body->is_entries[ic_index]);
2746 if (insn == BIN(setinstancevariable)) {
2747 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2750 cache->iv_set_name = 0;
2753 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
2758 unsigned int ic_index =
FIX2UINT(operands[j]);
2759 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2760 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2761 BADINSN_DUMP(anchor, &iobj->link, 0);
2762 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2763 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2764 ic_index, ISEQ_IS_SIZE(body));
2766 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2773 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2774 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2776 cd->cc = vm_cc_empty();
2777 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2781 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2784 generated_iseq[code_index + 1 + j] = operands[j];
2787 generated_iseq[code_index + 1 + j] = operands[j];
2790 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2791 "unknown operand type: %c",
type);
2795 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2799 case ISEQ_ELEMENT_LABEL:
2802 if (lobj->sp != sp) {
2803 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2804 RSTRING_PTR(rb_iseq_path(iseq)),
2805 lobj->label_no, lobj->sp, sp);
2810 case ISEQ_ELEMENT_ADJUST:
2815 if (adjust->label) {
2816 sp = adjust->label->sp;
2822 if (adjust->line_no != -1) {
2823 const int diff = orig_sp - sp;
2825 if (insns_info_index == 0) {
2826 COMPILE_ERROR(iseq, adjust->line_no,
2827 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2829 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2832 generated_iseq[code_index++] = BIN(adjuststack);
2833 generated_iseq[code_index++] = orig_sp - sp;
2835 else if (diff == 1) {
2836 generated_iseq[code_index++] = BIN(pop);
2838 else if (diff < 0) {
2839 int label_no = adjust->label ? adjust->label->label_no : -1;
2840 xfree(generated_iseq);
2843 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
2844 xfree(mark_offset_bits);
2846 debug_list(anchor, list);
2847 COMPILE_ERROR(iseq, adjust->line_no,
2848 "iseq_set_sequence: adjust bug to %d %d < %d",
2849 label_no, orig_sp, sp);
2862 body->iseq_encoded = (
void *)generated_iseq;
2863 body->iseq_size = code_index;
2864 body->stack_max = stack_max;
2866 if (ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit) {
2867 body->mark_bits.single = ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2871 body->mark_bits.list = mark_offset_bits;
2874 body->mark_bits.list = NULL;
2875 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = NULL;
2876 ruby_xfree(mark_offset_bits);
2881 body->insns_info.body = insns_info;
2882 body->insns_info.positions = positions;
2885 body->insns_info.body = insns_info;
2886 REALLOC_N(positions,
unsigned int, insns_info_index);
2887 body->insns_info.positions = positions;
2888 body->insns_info.size = insns_info_index;
2894label_get_position(
LABEL *lobj)
2896 return lobj->position;
2900label_get_sp(
LABEL *lobj)
2906iseq_set_exception_table(
rb_iseq_t *iseq)
2908 const VALUE *tptr, *ptr;
2909 unsigned int tlen, i;
2912 ISEQ_BODY(iseq)->catch_table = NULL;
2914 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
2915 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
2923 for (i = 0; i < table->size; i++) {
2926 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
2927 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
2928 pos = label_get_position((
LABEL *)(ptr[1] & ~1));
2930 entry->start = (
unsigned int)pos;
2931 pos = label_get_position((
LABEL *)(ptr[2] & ~1));
2933 entry->end = (
unsigned int)pos;
2940 entry->cont = label_get_position(lobj);
2941 entry->sp = label_get_sp(lobj);
2944 if (entry->type == CATCH_TYPE_RESCUE ||
2945 entry->type == CATCH_TYPE_BREAK ||
2946 entry->type == CATCH_TYPE_NEXT) {
2955 ISEQ_BODY(iseq)->catch_table = table;
2956 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
2977 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
2979 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
2980 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
2981 opt_table[i] = label_get_position((
LABEL *)opt_table[i]);
2988get_destination_insn(
INSN *iobj)
2994 list = lobj->link.next;
2996 switch (list->type) {
2997 case ISEQ_ELEMENT_INSN:
2998 case ISEQ_ELEMENT_ADJUST:
3000 case ISEQ_ELEMENT_LABEL:
3003 case ISEQ_ELEMENT_TRACE:
3006 events |= trace->event;
3014 if (list && IS_INSN(list)) {
3016 iobj->insn_info.events |= events;
3022get_next_insn(
INSN *iobj)
3027 if (IS_INSN(list) || IS_ADJUST(list)) {
3036get_prev_insn(
INSN *iobj)
3041 if (IS_INSN(list) || IS_ADJUST(list)) {
3050unref_destination(
INSN *iobj,
int pos)
3052 LABEL *lobj = (
LABEL *)OPERAND_AT(iobj, pos);
3054 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3058replace_destination(
INSN *dobj,
INSN *nobj)
3060 VALUE n = OPERAND_AT(nobj, 0);
3063 if (dl == nl)
return false;
3066 OPERAND_AT(dobj, 0) = n;
3067 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3072find_destination(
INSN *i)
3074 int pos,
len = insn_len(i->insn_id);
3075 for (pos = 0; pos <
len; ++pos) {
3076 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3077 return (
LABEL *)OPERAND_AT(i, pos);
3087 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
3090 unref_counts =
ALLOCA_N(
int, nlabels);
3091 MEMZERO(unref_counts,
int, nlabels);
3096 if (IS_INSN_ID(i, leave)) {
3100 else if ((lab = find_destination((
INSN *)i)) != 0) {
3101 unref_counts[lab->label_no]++;
3104 else if (IS_LABEL(i)) {
3106 if (lab->unremovable)
return 0;
3107 if (lab->refcnt > unref_counts[lab->label_no]) {
3108 if (i == first)
return 0;
3113 else if (IS_TRACE(i)) {
3116 else if (IS_ADJUST(i)) {
3120 }
while ((i = i->next) != 0);
3125 VALUE insn = INSN_OF(i);
3126 int pos,
len = insn_len(insn);
3127 for (pos = 0; pos <
len; ++pos) {
3128 switch (insn_op_types(insn)[pos]) {
3130 unref_destination((
INSN *)i, pos);
3139 }
while ((i != end) && (i = i->next) != 0);
3146 switch (OPERAND_AT(iobj, 0)) {
3148 ELEM_REMOVE(&iobj->link);
3151 ELEM_REMOVE(&iobj->link);
3154 iobj->insn_id = BIN(adjuststack);
3160is_frozen_putstring(
INSN *insn,
VALUE *op)
3162 if (IS_INSN_ID(insn, putstring) || IS_INSN_ID(insn, putchilledstring)) {
3163 *op = OPERAND_AT(insn, 0);
3166 else if (IS_INSN_ID(insn, putobject)) {
3167 *op = OPERAND_AT(insn, 0);
3198 INSN *niobj, *ciobj, *dup = 0;
3202 switch (INSN_OF(iobj)) {
3203 case BIN(putstring):
3204 case BIN(putchilledstring):
3210 case BIN(putobject):
3213 default:
return FALSE;
3216 ciobj = (
INSN *)get_next_insn(iobj);
3217 if (IS_INSN_ID(ciobj, jump)) {
3218 ciobj = (
INSN *)get_next_insn((
INSN*)OPERAND_AT(ciobj, 0));
3220 if (IS_INSN_ID(ciobj, dup)) {
3221 ciobj = (
INSN *)get_next_insn(dup = ciobj);
3223 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3224 niobj = (
INSN *)get_next_insn(ciobj);
3229 switch (INSN_OF(niobj)) {
3231 if (OPERAND_AT(ciobj, 0) ==
type) {
3232 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3235 case BIN(branchunless):
3236 if (OPERAND_AT(ciobj, 0) !=
type) {
3237 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3243 line = ciobj->insn_info.line_no;
3244 node_id = ciobj->insn_info.node_id;
3246 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3247 dest = (
LABEL *)niobj->link.next;
3250 dest = NEW_LABEL(line);
3251 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3254 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3256 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3263 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3264 vm_ci_flag(ci) | add,
3274 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3282#define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3290 optimize_checktype(iseq, iobj);
3292 if (IS_INSN_ID(iobj, jump)) {
3293 INSN *niobj, *diobj, *piobj;
3294 diobj = (
INSN *)get_destination_insn(iobj);
3295 niobj = (
INSN *)get_next_insn(iobj);
3297 if (diobj == niobj) {
3304 unref_destination(iobj, 0);
3305 ELEM_REMOVE(&iobj->link);
3308 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3309 IS_INSN_ID(diobj, jump) &&
3310 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3311 diobj->insn_info.events == 0) {
3322 if (replace_destination(iobj, diobj)) {
3323 remove_unreachable_chunk(iseq, iobj->link.next);
3327 else if (IS_INSN_ID(diobj, leave)) {
3340 unref_destination(iobj, 0);
3341 iobj->insn_id = BIN(leave);
3342 iobj->operand_size = 0;
3343 iobj->insn_info = diobj->insn_info;
3346 else if (IS_INSN(iobj->link.prev) &&
3347 (piobj = (
INSN *)iobj->link.prev) &&
3348 (IS_INSN_ID(piobj, branchif) ||
3349 IS_INSN_ID(piobj, branchunless))) {
3350 INSN *pdiobj = (
INSN *)get_destination_insn(piobj);
3351 if (niobj == pdiobj) {
3352 int refcnt = IS_LABEL(piobj->link.next) ?
3353 ((
LABEL *)piobj->link.next)->refcnt : 0;
3368 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3369 ? BIN(branchunless) : BIN(branchif);
3370 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3371 ELEM_REMOVE(&iobj->link);
3378 else if (diobj == pdiobj) {
3392 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3393 ELEM_REPLACE(&piobj->link, &popiobj->link);
3396 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3410 if (IS_INSN_ID(iobj, newrange)) {
3411 INSN *
const range = iobj;
3413 VALUE str_beg, str_end;
3415 if ((end = (
INSN *)get_prev_insn(range)) != 0 &&
3416 is_frozen_putstring(end, &str_end) &&
3417 (beg = (
INSN *)get_prev_insn(end)) != 0 &&
3418 is_frozen_putstring(beg, &str_beg)) {
3419 int excl =
FIX2INT(OPERAND_AT(range, 0));
3422 ELEM_REMOVE(&beg->link);
3423 ELEM_REMOVE(&end->link);
3424 range->insn_id = BIN(putobject);
3425 OPERAND_AT(range, 0) = lit_range;
3430 if (IS_INSN_ID(iobj, leave)) {
3431 remove_unreachable_chunk(iseq, iobj->link.next);
3443 if (IS_INSN_ID(iobj, duparray)) {
3445 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3446 iobj->insn_id = BIN(putobject);
3456 if (IS_INSN_ID(iobj, duparray)) {
3458 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3462 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3463 VALUE ary = iobj->operands[0];
3466 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, ary, (
VALUE)ci);
3478 if (IS_INSN_ID(iobj, duphash)) {
3480 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3484 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3485 VALUE hash = iobj->operands[0];
3488 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, hash, (
VALUE)ci);
3500 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3502 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3506 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3507 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, rb_cArray_empty_frozen, (
VALUE)ci);
3519 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3521 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3525 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3526 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, rb_cHash_empty_frozen, (
VALUE)ci);
3532 if (IS_INSN_ID(iobj, branchif) ||
3533 IS_INSN_ID(iobj, branchnil) ||
3534 IS_INSN_ID(iobj, branchunless)) {
3543 INSN *nobj = (
INSN *)get_destination_insn(iobj);
3565 int stop_optimization =
3566 ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq) &&
3567 nobj->link.type == ISEQ_ELEMENT_INSN &&
3568 nobj->insn_info.events;
3569 if (!stop_optimization) {
3570 INSN *pobj = (
INSN *)iobj->link.prev;
3573 if (!IS_INSN(&pobj->link))
3575 else if (IS_INSN_ID(pobj, dup))
3580 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3581 if (!replace_destination(iobj, nobj))
break;
3583 else if (prev_dup && IS_INSN_ID(nobj, dup) &&
3584 !!(nobj = (
INSN *)nobj->link.next) &&
3586 nobj->insn_id == iobj->insn_id) {
3602 if (!replace_destination(iobj, nobj))
break;
3630 if (prev_dup && IS_INSN(pobj->link.prev)) {
3631 pobj = (
INSN *)pobj->link.prev;
3633 if (IS_INSN_ID(pobj, putobject)) {
3634 cond = (IS_INSN_ID(iobj, branchif) ?
3635 OPERAND_AT(pobj, 0) !=
Qfalse :
3636 IS_INSN_ID(iobj, branchunless) ?
3637 OPERAND_AT(pobj, 0) ==
Qfalse :
3640 else if (IS_INSN_ID(pobj, putstring) ||
3641 IS_INSN_ID(pobj, duparray) ||
3642 IS_INSN_ID(pobj, newarray)) {
3643 cond = IS_INSN_ID(iobj, branchif);
3645 else if (IS_INSN_ID(pobj, putnil)) {
3646 cond = !IS_INSN_ID(iobj, branchif);
3649 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3650 ELEM_REMOVE(iobj->link.prev);
3652 else if (!iseq_pop_newarray(iseq, pobj)) {
3653 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3654 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3658 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3659 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3661 iobj->insn_id = BIN(jump);
3665 unref_destination(iobj, 0);
3666 ELEM_REMOVE(&iobj->link);
3671 nobj = (
INSN *)get_destination_insn(nobj);
3676 if (IS_INSN_ID(iobj, pop)) {
3684 if (IS_INSN(prev)) {
3685 enum ruby_vminsn_type previ = ((
INSN *)prev)->insn_id;
3686 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3687 previ == BIN(putself) || previ == BIN(putstring) ||
3688 previ == BIN(putchilledstring) ||
3689 previ == BIN(dup) ||
3690 previ == BIN(getlocal) ||
3691 previ == BIN(getblockparam) ||
3692 previ == BIN(getblockparamproxy) ||
3693 previ == BIN(getinstancevariable) ||
3694 previ == BIN(duparray)) {
3698 ELEM_REMOVE(&iobj->link);
3700 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (
INSN*)prev)) {
3701 ELEM_REMOVE(&iobj->link);
3703 else if (previ == BIN(concatarray)) {
3705 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3706 INSN_OF(piobj) = BIN(pop);
3708 else if (previ == BIN(concatstrings)) {
3709 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3713 ELEM_REMOVE(&iobj->link);
3714 INSN_OF(prev) = BIN(adjuststack);
3720 if (IS_INSN_ID(iobj, newarray) ||
3721 IS_INSN_ID(iobj, duparray) ||
3722 IS_INSN_ID(iobj, concatarray) ||
3723 IS_INSN_ID(iobj, splatarray) ||
3733 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3739 if (IS_INSN_ID(iobj, newarray)) {
3741 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3742 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3744 op1 = OPERAND_AT(iobj, 0);
3745 op2 = OPERAND_AT(next, 0);
3756 INSN_OF(iobj) = BIN(swap);
3757 iobj->operand_size = 0;
3766 INSN_OF(iobj) = BIN(opt_reverse);
3771 INSN_OF(iobj) = BIN(opt_reverse);
3772 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3782 for (; diff > 0; diff--) {
3783 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
3794 for (; diff < 0; diff++) {
3795 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
3802 if (IS_INSN_ID(iobj, duparray)) {
3811 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
3812 INSN_OF(iobj) = BIN(putobject);
3816 if (IS_INSN_ID(iobj, anytostring)) {
3824 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
3825 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
3830 if (IS_INSN_ID(iobj, putstring) || IS_INSN_ID(iobj, putchilledstring) ||
3838 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
3839 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
3840 INSN *next = (
INSN *)iobj->link.next;
3841 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
3842 ELEM_REMOVE(&next->link);
3844 ELEM_REMOVE(&iobj->link);
3846 if (IS_NEXT_INSN_ID(&iobj->link, toregexp)) {
3847 INSN *next = (
INSN *)iobj->link.next;
3848 if (OPERAND_AT(next, 1) ==
INT2FIX(1)) {
3849 VALUE src = OPERAND_AT(iobj, 0);
3850 int opt = (int)
FIX2LONG(OPERAND_AT(next, 0));
3851 VALUE path = rb_iseq_path(iseq);
3852 int line = iobj->insn_info.line_no;
3853 VALUE errinfo = rb_errinfo();
3854 VALUE re = rb_reg_compile(src, opt, RSTRING_PTR(path), line);
3856 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
3857 rb_set_errinfo(errinfo);
3858 COMPILE_ERROR(iseq, line,
"%" PRIsVALUE, message);
3861 ELEM_REMOVE(iobj->link.next);
3866 if (IS_INSN_ID(iobj, concatstrings)) {
3875 if (IS_INSN(next) && IS_INSN_ID(next, jump))
3876 next = get_destination_insn(jump = (
INSN *)next);
3877 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
3878 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
3879 OPERAND_AT(iobj, 0) =
INT2FIX(n);
3881 LABEL *label = ((
LABEL *)OPERAND_AT(jump, 0));
3882 if (!--label->refcnt) {
3883 ELEM_REMOVE(&label->link);
3886 label = NEW_LABEL(0);
3887 OPERAND_AT(jump, 0) = (
VALUE)label;
3890 ELEM_INSERT_NEXT(next, &label->link);
3891 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
3899 if (do_tailcallopt &&
3900 (IS_INSN_ID(iobj, send) ||
3901 IS_INSN_ID(iobj, opt_aref_with) ||
3902 IS_INSN_ID(iobj, opt_aset_with) ||
3903 IS_INSN_ID(iobj, invokesuper))) {
3912 if (iobj->link.next) {
3915 if (!IS_INSN(next)) {
3919 switch (INSN_OF(next)) {
3928 next = get_destination_insn((
INSN *)next);
3942 if (IS_INSN_ID(piobj, send) ||
3943 IS_INSN_ID(piobj, invokesuper)) {
3944 if (OPERAND_AT(piobj, 1) == 0) {
3945 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3946 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3951 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3952 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3958 if (IS_INSN_ID(iobj, dup)) {
3959 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
3970 if (IS_NEXT_INSN_ID(set1, setlocal)) {
3972 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3973 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3975 ELEM_REMOVE(&iobj->link);
3988 else if (IS_NEXT_INSN_ID(set1, dup) &&
3989 IS_NEXT_INSN_ID(set1->next, setlocal)) {
3990 set2 = set1->next->next;
3991 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3992 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3993 ELEM_REMOVE(set1->next);
4007 if (IS_INSN_ID(iobj, getlocal)) {
4009 if (IS_NEXT_INSN_ID(niobj, dup)) {
4010 niobj = niobj->next;
4012 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
4014 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
4015 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
4031 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
4032 if (IS_TRACE(iobj->link.next)) {
4033 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
4034 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
4036 if (iobj == (
INSN *)list && bf->argc == 0 && (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF)) {
4037 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4050 if (IS_INSN_ID(iobj, getblockparam)) {
4051 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4052 iobj->insn_id = BIN(getblockparamproxy);
4056 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4058 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4059 niobj = niobj->next;
4061 unsigned int set_flags = 0, unset_flags = 0;
4074 if (IS_NEXT_INSN_ID(niobj, send)) {
4075 siobj = niobj->next;
4076 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4077 unset_flags = VM_CALL_ARGS_BLOCKARG;
4092 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4093 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4094 siobj = niobj->next->next;
4095 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4100 unsigned int flags = vm_ci_flag(ci);
4101 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4102 ((
INSN*)niobj)->insn_id = BIN(putobject);
4103 RB_OBJ_WRITE(iseq, &OPERAND_AT(niobj, 0), rb_hash_freeze(rb_hash_resurrect(OPERAND_AT(niobj, 0))));
4105 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4106 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4108 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4118insn_set_specialized_instruction(
rb_iseq_t *iseq,
INSN *iobj,
int insn_id)
4120 if (insn_id == BIN(opt_neq)) {
4121 VALUE original_ci = iobj->operands[0];
4122 VALUE new_ci = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4123 insn_replace_with_operands(iseq, iobj, insn_id, 2, new_ci, original_ci);
4126 iobj->insn_id = insn_id;
4127 iobj->operand_size = insn_len(insn_id) - 1;
4137 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4138 IS_INSN(iobj->link.next)) {
4142 INSN *niobj = (
INSN *)iobj->link.next;
4143 if (IS_INSN_ID(niobj, send)) {
4145 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4147 switch (vm_ci_mid(ci)) {
4149 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4152 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4155 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4160 VALUE num = iobj->operands[0];
4161 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, num, method);
4162 ELEM_REMOVE(&niobj->link);
4167 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4169 IS_NEXT_INSN_ID(&niobj->link, send)) {
4171 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4172 VALUE num = iobj->operands[0];
4173 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK));
4174 ELEM_REMOVE(&iobj->link);
4175 ELEM_REMOVE(niobj->link.next);
4176 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4182 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4184 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4185 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4188 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4189 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] ==
rb_id2sym(idBuffer))) {
4190 VALUE num = iobj->operands[0];
4191 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 2),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER));
4193 ELEM_REMOVE((niobj->link.next)->next);
4195 ELEM_REMOVE(&iobj->link);
4197 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4205 if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4206 IS_INSN_ID(niobj, putobject) ||
4207 IS_INSN_ID(niobj, putself) ||
4208 IS_INSN_ID(niobj, getlocal) ||
4209 IS_INSN_ID(niobj, getinstancevariable)) &&
4210 IS_NEXT_INSN_ID(&niobj->link, send)) {
4217 sendobj = sendobj->next;
4218 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4219 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4222 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4223 VALUE num = iobj->operands[0];
4225 insn_replace_with_operands(iseq, sendins, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P));
4227 ELEM_REMOVE(&iobj->link);
4241 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4242 INSN *niobj = (
INSN *)iobj->link.next;
4243 if ((IS_INSN_ID(niobj, getlocal) ||
4244 IS_INSN_ID(niobj, getinstancevariable) ||
4245 IS_INSN_ID(niobj, putself)) &&
4246 IS_NEXT_INSN_ID(&niobj->link, send)) {
4253 sendobj = sendobj->next;
4254 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4255 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4257 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4259 VALUE ary = iobj->operands[0];
4263 insn_replace_with_operands(iseq, sendins, BIN(opt_duparray_send), 3, ary,
rb_id2sym(idIncludeP),
INT2FIX(1));
4266 ELEM_REMOVE(&iobj->link);
4273 if (IS_INSN_ID(iobj, send)) {
4277#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4278 if (vm_ci_simple(ci)) {
4279 switch (vm_ci_argc(ci)) {
4281 switch (vm_ci_mid(ci)) {
4282 case idLength: SP_INSN(length);
return COMPILE_OK;
4283 case idSize: SP_INSN(size);
return COMPILE_OK;
4284 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4285 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4286 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4287 case idNot: SP_INSN(not);
return COMPILE_OK;
4291 switch (vm_ci_mid(ci)) {
4292 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4293 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4294 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4295 case idDIV: SP_INSN(div);
return COMPILE_OK;
4296 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4297 case idEq: SP_INSN(eq);
return COMPILE_OK;
4298 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4299 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4300 case idLT: SP_INSN(lt);
return COMPILE_OK;
4301 case idLE: SP_INSN(le);
return COMPILE_OK;
4302 case idGT: SP_INSN(gt);
return COMPILE_OK;
4303 case idGE: SP_INSN(ge);
return COMPILE_OK;
4304 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4305 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4306 case idAnd: SP_INSN(and);
return COMPILE_OK;
4307 case idOr: SP_INSN(or);
return COMPILE_OK;
4311 switch (vm_ci_mid(ci)) {
4312 case idASET: SP_INSN(aset);
return COMPILE_OK;
4318 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4319 iobj->insn_id = BIN(opt_send_without_block);
4320 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4331 switch (ISEQ_BODY(iseq)->
type) {
4333 case ISEQ_TYPE_EVAL:
4334 case ISEQ_TYPE_MAIN:
4336 case ISEQ_TYPE_RESCUE:
4337 case ISEQ_TYPE_ENSURE:
4349 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4350 const int do_tailcallopt = tailcallable_p(iseq) &&
4351 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4352 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4353 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4354 int rescue_level = 0;
4355 int tailcallopt = do_tailcallopt;
4357 list = FIRST_ELEMENT(anchor);
4359 int do_block_optimization = 0;
4360 LABEL * block_loop_label = NULL;
4363 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
4364 do_block_optimization = 1;
4369 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop) && IS_LABEL(le->next)) {
4370 block_loop_label = (
LABEL *)le->next;
4375 if (IS_INSN(list)) {
4376 if (do_peepholeopt) {
4377 iseq_peephole_optimize(iseq, list, tailcallopt);
4380 iseq_specialized_instruction(iseq, (
INSN *)list);
4383 insn_operands_unification((
INSN *)list);
4386 if (do_block_optimization) {
4389 if (IS_INSN_ID(item,
throw)) {
4390 do_block_optimization = 0;
4395 const char *types = insn_op_types(item->insn_id);
4396 for (
int j = 0; types[j]; j++) {
4397 if (types[j] == TS_OFFSET) {
4402 LABEL * target = (
LABEL *)OPERAND_AT(item, j);
4403 if (target == block_loop_label) {
4404 do_block_optimization = 0;
4411 if (IS_LABEL(list)) {
4412 switch (((
LABEL *)list)->rescued) {
4413 case LABEL_RESCUE_BEG:
4415 tailcallopt = FALSE;
4417 case LABEL_RESCUE_END:
4418 if (!--rescue_level) tailcallopt = do_tailcallopt;
4425 if (do_block_optimization) {
4427 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop)) {
4434#if OPT_INSTRUCTIONS_UNIFICATION
4442 VALUE *operands = 0, *ptr = 0;
4446 for (i = 0; i < size; i++) {
4447 iobj = (
INSN *)list;
4448 argc += iobj->operand_size;
4453 ptr = operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
4458 for (i = 0; i < size; i++) {
4459 iobj = (
INSN *)list;
4460 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4461 ptr += iobj->operand_size;
4465 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4477#if OPT_INSTRUCTIONS_UNIFICATION
4483 list = FIRST_ELEMENT(anchor);
4485 if (IS_INSN(list)) {
4486 iobj = (
INSN *)list;
4488 if (unified_insns_data[
id] != 0) {
4489 const int *
const *entry = unified_insns_data[id];
4490 for (j = 1; j < (intptr_t)entry[0]; j++) {
4491 const int *unified = entry[j];
4493 for (k = 2; k < unified[1]; k++) {
4495 ((
INSN *)li)->insn_id != unified[k]) {
4502 new_unified_insn(iseq, unified[0], unified[1] - 1,
4507 niobj->link.next = li;
4526all_string_result_p(
const NODE *node)
4528 if (!node)
return FALSE;
4529 switch (nd_type(node)) {
4530 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4532 case NODE_IF:
case NODE_UNLESS:
4533 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4534 if (all_string_result_p(RNODE_IF(node)->nd_body))
4535 return all_string_result_p(RNODE_IF(node)->nd_else);
4537 case NODE_AND:
case NODE_OR:
4538 if (!RNODE_AND(node)->nd_2nd)
4539 return all_string_result_p(RNODE_AND(node)->nd_1st);
4540 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4542 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4552 const NODE *lit_node;
4560 VALUE s = rb_str_new_mutable_parser_string(str);
4562 VALUE error = rb_reg_check_preprocess(s);
4563 if (!
NIL_P(error)) {
4564 COMPILE_ERROR(args->iseq, nd_line(node),
"%" PRIsVALUE, error);
4568 if (
NIL_P(args->lit)) {
4570 args->lit_node = node;
4579flush_dstr_fragment(
struct dstr_ctxt *args)
4581 if (!
NIL_P(args->lit)) {
4583 VALUE lit = args->lit;
4585 lit = rb_fstring(lit);
4586 ADD_INSN1(args->ret, args->lit_node, putobject, lit);
4593compile_dstr_fragments_0(
struct dstr_ctxt *args,
const NODE *
const node)
4595 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4599 CHECK(append_dstr_fragment(args, node, str));
4603 const NODE *
const head = list->nd_head;
4604 if (nd_type_p(head, NODE_STR)) {
4605 CHECK(append_dstr_fragment(args, node, RNODE_STR(head)->
string));
4607 else if (nd_type_p(head, NODE_DSTR)) {
4608 CHECK(compile_dstr_fragments_0(args, head));
4611 flush_dstr_fragment(args);
4613 CHECK(COMPILE(args->ret,
"each string", head));
4625 .iseq = iseq, .ret = ret,
4626 .lit =
Qnil, .lit_node = NULL,
4627 .cnt = 0, .dregx = dregx,
4629 CHECK(compile_dstr_fragments_0(&args, node));
4630 flush_dstr_fragment(&args);
4640 while (node && nd_type_p(node, NODE_BLOCK)) {
4641 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4642 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4643 node = RNODE_BLOCK(node)->nd_next;
4646 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4655 if (!RNODE_DSTR(node)->nd_next) {
4656 VALUE lit = rb_node_dstr_string_val(node);
4657 ADD_INSN1(ret, node, putstring, lit);
4661 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, FALSE));
4662 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4671 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
4673 if (!RNODE_DREGX(node)->nd_next) {
4675 VALUE src = rb_node_dregx_string_val(node);
4676 VALUE match = rb_reg_compile(src, cflag, NULL, 0);
4677 ADD_INSN1(ret, node, putobject, match);
4683 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, TRUE));
4687 ADD_INSN(ret, node, pop);
4697 const int line = nd_line(node);
4698 LABEL *lend = NEW_LABEL(line);
4699 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4700 + VM_SVAR_FLIPFLOP_START;
4703 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4704 ADD_INSNL(ret, node, branchif, lend);
4707 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
4708 ADD_INSNL(ret, node, branchunless, else_label);
4709 ADD_INSN1(ret, node, putobject,
Qtrue);
4710 ADD_INSN1(ret, node, setspecial, key);
4712 ADD_INSNL(ret, node, jump, then_label);
4716 ADD_LABEL(ret, lend);
4717 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
4718 ADD_INSNL(ret, node, branchunless, then_label);
4719 ADD_INSN1(ret, node, putobject,
Qfalse);
4720 ADD_INSN1(ret, node, setspecial, key);
4721 ADD_INSNL(ret, node, jump, then_label);
4730#define COMPILE_SINGLE 2
4737 LABEL *label = NEW_LABEL(nd_line(cond));
4738 if (!then_label) then_label = label;
4739 else if (!else_label) else_label = label;
4741 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
4743 if (LIST_INSN_SIZE_ONE(seq)) {
4744 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
4745 if (insn->insn_id == BIN(jump) && (
LABEL *)(insn->operands[0]) == label)
4748 if (!label->refcnt) {
4749 return COMPILE_SINGLE;
4751 ADD_LABEL(seq, label);
4761 DECL_ANCHOR(ignore);
4764 switch (nd_type(cond)) {
4766 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
4767 cond = RNODE_AND(cond)->nd_2nd;
4768 if (ok == COMPILE_SINGLE) {
4769 INIT_ANCHOR(ignore);
4771 then_label = NEW_LABEL(nd_line(cond));
4775 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
4776 cond = RNODE_OR(cond)->nd_2nd;
4777 if (ok == COMPILE_SINGLE) {
4778 INIT_ANCHOR(ignore);
4780 else_label = NEW_LABEL(nd_line(cond));
4790 case NODE_IMAGINARY:
4797 ADD_INSNL(ret, cond, jump, then_label);
4802 ADD_INSNL(ret, cond, jump, else_label);
4808 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
4809 ADD_INSNL(ret, cond, jump, then_label);
4812 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
4815 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
4818 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
4822 DECL_ANCHOR(cond_seq);
4823 INIT_ANCHOR(cond_seq);
4825 CHECK(COMPILE(cond_seq,
"branch condition", cond));
4827 if (LIST_INSN_SIZE_ONE(cond_seq)) {
4828 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
4829 if (insn->insn_id == BIN(putobject)) {
4830 if (
RTEST(insn->operands[0])) {
4831 ADD_INSNL(ret, cond, jump, then_label);
4836 ADD_INSNL(ret, cond, jump, else_label);
4841 ADD_SEQ(ret, cond_seq);
4846 ADD_INSNL(ret, cond, branchunless, else_label);
4847 ADD_INSNL(ret, cond, jump, then_label);
4854keyword_node_p(
const NODE *
const node)
4856 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
4862 switch (nd_type(node)) {
4864 return rb_node_sym_string_val(node);
4866 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
4873 NODE *node = node_hash->nd_head;
4874 VALUE hash = rb_hash_new();
4877 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4878 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
4879 VALUE idx = rb_hash_aref(hash, key);
4884 rb_hash_aset(hash, key,
INT2FIX(i));
4894 const NODE *
const root_node,
4902 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
4903 const NODE *node = RNODE_HASH(root_node)->nd_head;
4907 const NODE *key_node = RNODE_LIST(node)->nd_head;
4911 if (key_node && nd_type_p(key_node, NODE_SYM)) {
4916 *flag |= VM_CALL_KW_SPLAT;
4917 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4922 *flag |= VM_CALL_KW_SPLAT_MUT;
4927 node = RNODE_LIST(node)->nd_next;
4928 node = RNODE_LIST(node)->nd_next;
4932 node = RNODE_HASH(root_node)->nd_head;
4935 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
4938 VALUE *keywords = kw_arg->keywords;
4941 kw_arg->references = 0;
4942 kw_arg->keyword_len =
len;
4944 *kw_arg_ptr = kw_arg;
4946 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4947 const NODE *key_node = RNODE_LIST(node)->nd_head;
4948 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
4951 keywords[j] = get_symbol_value(iseq, key_node);
4955 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
4969 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
4971 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
4974 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
4975 *kwnode_ptr = RNODE_LIST(node)->nd_head;
4978 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
4979 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
4987frozen_string_literal_p(
const rb_iseq_t *iseq)
4989 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
4993static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
4995 switch (nd_type(node)) {
5003 case NODE_IMAGINARY:
5010 return hash_key || frozen_string_literal_p(iseq);
5019 switch (nd_type(node)) {
5021 return rb_node_integer_literal_val(node);
5023 return rb_node_float_literal_val(node);
5025 return rb_node_rational_literal_val(node);
5026 case NODE_IMAGINARY:
5027 return rb_node_imaginary_literal_val(node);
5035 return rb_node_sym_string_val(node);
5037 return rb_node_regx_string_val(node);
5039 return rb_node_line_lineno_val(node);
5041 return rb_node_encoding_val(node);
5044 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
5045 VALUE lit = get_string_value(node);
5046 return rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
5049 return get_string_value(node);
5052 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
5059 const NODE *line_node = node;
5061 if (nd_type_p(node, NODE_ZLIST)) {
5063 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5068 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5071 for (; node; node = RNODE_LIST(node)->nd_next) {
5072 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5114 const int max_stack_len = 0x100;
5115 const int min_tmp_ary_len = 0x40;
5119#define FLUSH_CHUNK \
5121 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5122 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5123 first_chunk = FALSE; \
5131 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5133 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5134 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5137 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5142 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5143 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5149 ADD_INSN1(ret, line_node, duparray, ary);
5150 first_chunk = FALSE;
5153 ADD_INSN1(ret, line_node, putobject, ary);
5154 ADD_INSN(ret, line_node, concattoarray);
5161 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5163 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5166 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5168 if (stack_len == 0 && first_chunk) {
5169 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5174 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5175 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5179 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5184 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5194static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5196 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);
5202 const NODE *line_node = node;
5204 node = RNODE_HASH(node)->nd_head;
5206 if (!node || nd_type_p(node, NODE_ZLIST)) {
5208 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5213 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5216 for (; node; node = RNODE_LIST(node)->nd_next) {
5217 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5240 const int max_stack_len = 0x100;
5241 const int min_tmp_hash_len = 0x800;
5243 int first_chunk = 1;
5244 DECL_ANCHOR(anchor);
5245 INIT_ANCHOR(anchor);
5248#define FLUSH_CHUNK() \
5250 if (first_chunk) { \
5251 APPEND_LIST(ret, anchor); \
5252 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5255 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5256 ADD_INSN(ret, line_node, swap); \
5257 APPEND_LIST(ret, anchor); \
5258 ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
5260 INIT_ANCHOR(anchor); \
5261 first_chunk = stack_len = 0; \
5268 if (static_literal_node_pair_p(node, iseq)) {
5270 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5271 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5274 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5279 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5281 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5282 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5293 ADD_INSN1(ret, line_node, duphash, hash);
5297 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5298 ADD_INSN(ret, line_node, swap);
5300 ADD_INSN1(ret, line_node, putobject, hash);
5302 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5309 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5312 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5315 if (RNODE_LIST(node)->nd_head) {
5317 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5318 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5322 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5328 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5329 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5330 int first_kw = first_chunk && stack_len == 0;
5331 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5332 int only_kw = last_kw && first_kw;
5334 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5336 if (only_kw && method_call_keywords) {
5344 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5346 else if (first_kw) {
5350 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5357 if (only_kw && method_call_keywords) {
5363 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5370 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5371 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5372 else ADD_INSN(ret, line_node, swap);
5374 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5376 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5391rb_node_case_when_optimizable_literal(
const NODE *
const node)
5393 switch (nd_type(node)) {
5395 return rb_node_integer_literal_val(node);
5397 VALUE v = rb_node_float_literal_val(node);
5406 case NODE_IMAGINARY:
5415 return rb_node_sym_string_val(node);
5417 return rb_node_line_lineno_val(node);
5419 return rb_node_str_string_val(node);
5421 return rb_node_file_path_val(node);
5428 LABEL *l1,
int only_special_literals,
VALUE literals)
5431 const NODE *val = RNODE_LIST(vals)->nd_head;
5432 VALUE lit = rb_node_case_when_optimizable_literal(val);
5435 only_special_literals = 0;
5437 else if (
NIL_P(rb_hash_lookup(literals, lit))) {
5438 rb_hash_aset(literals, lit, (
VALUE)(l1) | 1);
5441 if (nd_type_p(val, NODE_STR) || nd_type_p(val, NODE_FILE)) {
5442 debugp_param(
"nd_lit", get_string_value(val));
5443 lit = get_string_value(val);
5444 ADD_INSN1(cond_seq, val, putobject, lit);
5448 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5452 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5453 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5454 ADD_INSNL(cond_seq, val, branchif, l1);
5455 vals = RNODE_LIST(vals)->nd_next;
5457 return only_special_literals;
5462 LABEL *l1,
int only_special_literals,
VALUE literals)
5464 const NODE *line_node = vals;
5466 switch (nd_type(vals)) {
5468 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5472 ADD_INSN (cond_seq, line_node, dup);
5473 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5474 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5475 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5476 ADD_INSNL(cond_seq, line_node, branchif, l1);
5479 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5480 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5483 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5484 ADD_INSN (cond_seq, line_node, dup);
5485 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5486 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5487 ADD_INSNL(cond_seq, line_node, branchif, l1);
5490 ADD_INSN (cond_seq, line_node, dup);
5491 CHECK(COMPILE(cond_seq,
"when val", vals));
5492 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5493 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5494 ADD_INSNL(cond_seq, line_node, branchif, l1);
5587 const NODE *line_node;
5602add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc,
INSN *before_insn)
5605 rb_bug(
"no masgn_state");
5614 memo->before_insn = before_insn;
5615 memo->line_node = line_node;
5616 memo->argn = state->num_args + 1;
5617 memo->num_args = argc;
5618 state->num_args += argc;
5619 memo->lhs_pos = lhs_pos;
5621 if (!state->first_memo) {
5622 state->first_memo = memo;
5625 state->last_memo->next = memo;
5627 state->last_memo = memo;
5637 switch (nd_type(node)) {
5638 case NODE_ATTRASGN: {
5640 const NODE *line_node = node;
5642 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5644 bool safenav_call =
false;
5646 iobj = (
INSN *)get_prev_insn((
INSN *)insn_element);
5648 ELEM_REMOVE(insn_element);
5649 if (!IS_INSN_ID(iobj, send)) {
5650 safenav_call =
true;
5651 iobj = (
INSN *)get_prev_insn(iobj);
5652 ELEM_INSERT_NEXT(&iobj->link, insn_element);
5654 (pre->last = iobj->link.prev)->next = 0;
5657 int argc = vm_ci_argc(ci) + 1;
5658 ci = ci_argc_set(iseq, ci, argc);
5659 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5663 ADD_INSN(lhs, line_node, swap);
5666 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5669 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (
INSN *)LAST_ELEMENT(lhs))) {
5673 iobj->link.prev = lhs->last;
5674 lhs->last->next = &iobj->link;
5675 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
5676 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5677 int argc = vm_ci_argc(ci);
5678 bool dupsplat =
false;
5679 ci = ci_argc_set(iseq, ci, argc - 1);
5680 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
5687 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
5689 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5698 int line_no = nd_line(line_node);
5699 int node_id = nd_node_id(line_node);
5702 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5703 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
5704 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5706 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
5708 if (!safenav_call) {
5709 ADD_INSN(lhs, line_node, pop);
5711 ADD_INSN(lhs, line_node, pop);
5714 for (
int i=0; i < argc; i++) {
5715 ADD_INSN(post, line_node, pop);
5720 DECL_ANCHOR(nest_rhs);
5721 INIT_ANCHOR(nest_rhs);
5722 DECL_ANCHOR(nest_lhs);
5723 INIT_ANCHOR(nest_lhs);
5725 int prev_level = state->lhs_level;
5726 bool prev_nested = state->nested;
5728 state->lhs_level = lhs_pos - 1;
5729 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5730 state->lhs_level = prev_level;
5731 state->nested = prev_nested;
5733 ADD_SEQ(lhs, nest_rhs);
5734 ADD_SEQ(lhs, nest_lhs);
5738 if (!RNODE_CDECL(node)->nd_vid) {
5742 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5745 iobj = (
INSN *)insn_element;
5748 ELEM_REMOVE(insn_element);
5749 pre->last = iobj->link.prev;
5752 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (
INSN *)LAST_ELEMENT(lhs))) {
5756 ADD_INSN(post, node, pop);
5761 DECL_ANCHOR(anchor);
5762 INIT_ANCHOR(anchor);
5763 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
5764 ELEM_REMOVE(FIRST_ELEMENT(anchor));
5765 ADD_SEQ(lhs, anchor);
5776 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
5777 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
5784 const NODE *rhsn,
const NODE *orig_lhsn)
5787 const int memsize = numberof(mem);
5789 int llen = 0, rlen = 0;
5791 const NODE *lhsn = orig_lhsn;
5793#define MEMORY(v) { \
5795 if (memindex == memsize) return 0; \
5796 for (i=0; i<memindex; i++) { \
5797 if (mem[i] == (v)) return 0; \
5799 mem[memindex++] = (v); \
5802 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
5807 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
5808 switch (nd_type(ln)) {
5813 MEMORY(get_nd_vid(ln));
5818 lhsn = RNODE_LIST(lhsn)->nd_next;
5824 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
5827 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
5829 rhsn = RNODE_LIST(rhsn)->nd_next;
5834 for (i=0; i<llen-rlen; i++) {
5835 ADD_INSN(ret, orig_lhsn, putnil);
5839 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
5846 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
5847 const NODE *splatn = RNODE_MASGN(node)->nd_args;
5848 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
5849 const NODE *lhsn_count = lhsn;
5850 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
5855 while (lhsn_count) {
5857 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
5860 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
5862 lhsn = RNODE_LIST(lhsn)->nd_next;
5866 if (nd_type_p(splatn, NODE_POSTARG)) {
5868 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
5869 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
5870 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
5872 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
5874 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
5876 if (NODE_NAMED_REST_P(restn)) {
5877 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
5880 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
5882 postn = RNODE_LIST(postn)->nd_next;
5887 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
5891 if (!state->nested) {
5892 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
5896 ADD_INSN(rhs, node, dup);
5898 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
5905 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
5907 state.lhs_level = popped ? 0 : 1;
5910 state.first_memo = NULL;
5911 state.last_memo = NULL;
5921 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
5925 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
5926 for (
int i = 0; i < memo->num_args; i++) {
5927 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
5929 tmp_memo = memo->next;
5938 if (!popped && state.num_args >= 1) {
5940 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
5952 switch (nd_type(node)) {
5954 rb_ary_unshift(arr,
ID2SYM(RNODE_CONST(node)->nd_vid));
5957 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON3(node)->nd_mid));
5958 rb_ary_unshift(arr,
ID2SYM(idNULL));
5961 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON2(node)->nd_mid));
5962 node = RNODE_COLON2(node)->nd_head;
5971compile_const_prefix(
rb_iseq_t *iseq,
const NODE *
const node,
5974 switch (nd_type(node)) {
5976 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
5977 ADD_INSN1(body, node, putobject,
Qtrue);
5978 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
5981 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
5982 ADD_INSN(body, node, pop);
5983 ADD_INSN1(body, node, putobject, rb_cObject);
5984 ADD_INSN1(body, node, putobject,
Qtrue);
5985 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
5988 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
5989 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
5990 ADD_INSN1(body, node, putobject,
Qfalse);
5991 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
5994 CHECK(COMPILE(pref,
"const colon2 prefix", node));
6003 if (nd_type_p(cpath, NODE_COLON3)) {
6005 ADD_INSN1(ret, cpath, putobject, rb_cObject);
6006 return VM_DEFINECLASS_FLAG_SCOPED;
6008 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
6010 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
6011 return VM_DEFINECLASS_FLAG_SCOPED;
6015 ADD_INSN1(ret, cpath, putspecialobject,
6016 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
6022private_recv_p(
const NODE *node)
6024 NODE *recv = get_nd_recv(node);
6025 if (recv && nd_type_p(recv, NODE_SELF)) {
6026 return RNODE_SELF(recv)->nd_state != 0;
6033 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore);
6036compile_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);
6043 enum defined_type expr_type = DEFINED_NOT_DEFINED;
6044 enum node_type
type;
6045 const int line = nd_line(node);
6046 const NODE *line_node = node;
6048 switch (
type = nd_type(node)) {
6052 expr_type = DEFINED_NIL;
6055 expr_type = DEFINED_SELF;
6058 expr_type = DEFINED_TRUE;
6061 expr_type = DEFINED_FALSE;
6066 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
6070 if (RNODE_LIST(vals)->nd_head) {
6071 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
6074 lfinish[1] = NEW_LABEL(line);
6076 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6078 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6091 case NODE_IMAGINARY:
6096 expr_type = DEFINED_EXPR;
6100 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6102 lfinish[1] = NEW_LABEL(line);
6104 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6105 expr_type = DEFINED_EXPR;
6111 expr_type = DEFINED_LVAR;
6114#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6116 ADD_INSN3(ret, line_node, definedivar,
6117 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6121 ADD_INSN(ret, line_node, putnil);
6122 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6123 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6127 ADD_INSN(ret, line_node, putnil);
6128 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6129 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6133 ADD_INSN(ret, line_node, putnil);
6134 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6135 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6139 lfinish[1] = NEW_LABEL(line);
6141 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6142 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6143 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6146 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6147 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6150 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6151 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6155 ADD_INSN1(ret, line_node, putobject, rb_cObject);
6156 ADD_INSN3(ret, line_node, defined,
6157 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6165 case NODE_ATTRASGN:{
6166 const int explicit_receiver =
6167 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6168 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6170 if (get_nd_args(node) || explicit_receiver) {
6172 lfinish[1] = NEW_LABEL(line);
6175 lfinish[2] = NEW_LABEL(line);
6178 if (get_nd_args(node)) {
6179 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6180 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6182 if (explicit_receiver) {
6183 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6184 switch (nd_type(get_nd_recv(node))) {
6190 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6191 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6194 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6195 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6199 ADD_INSN(ret, line_node, dup);
6201 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6202 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6205 ADD_INSN(ret, line_node, putself);
6207 ADD_INSN(ret, line_node, dup);
6209 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6210 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6216 ADD_INSN(ret, line_node, putnil);
6217 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6218 PUSH_VAL(DEFINED_YIELD));
6219 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6224 ADD_INSN(ret, line_node, putnil);
6225 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6226 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6227 PUSH_VAL(DEFINED_GVAR));
6232 ADD_INSN(ret, line_node, putnil);
6233 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6234 PUSH_VAL(DEFINED_ZSUPER));
6240 case NODE_OP_ASGN_OR:
6241 case NODE_OP_ASGN_AND:
6250 expr_type = DEFINED_ASGN;
6257 VALUE str = rb_iseq_defined_string(expr_type);
6258 ADD_INSN1(ret, line_node, putobject, str);
6261 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6268 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6269 iseq_set_exception_local_table(iseq);
6274 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore)
6277 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6279 int line = nd_line(node);
6280 LABEL *lstart = NEW_LABEL(line);
6281 LABEL *lend = NEW_LABEL(line);
6284 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6285 rescue = NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc,
6287 ISEQ_BODY(iseq)->location.label),
6288 ISEQ_TYPE_RESCUE, 0);
6289 lstart->rescued = LABEL_RESCUE_BEG;
6290 lend->rescued = LABEL_RESCUE_END;
6291 APPEND_LABEL(ret, lcur, lstart);
6292 ADD_LABEL(ret, lend);
6294 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6302 const int line = nd_line(node);
6303 const NODE *line_node = node;
6304 if (!RNODE_DEFINED(node)->nd_head) {
6305 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6306 ADD_INSN1(ret, line_node, putobject, str);
6311 lfinish[0] = NEW_LABEL(line);
6314 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6316 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6317 ADD_INSN(ret, line_node, swap);
6319 ADD_LABEL(ret, lfinish[2]);
6321 ADD_INSN(ret, line_node, pop);
6322 ADD_LABEL(ret, lfinish[1]);
6324 ADD_LABEL(ret, lfinish[0]);
6330make_name_for_block(
const rb_iseq_t *orig_iseq)
6335 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6336 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6337 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6340 iseq = ISEQ_BODY(iseq)->parent_iseq;
6345 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6348 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6357 enl->ensure_node = node;
6358 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6360 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6370 while (erange->next != 0) {
6371 erange = erange->next;
6375 ne->end = erange->end;
6376 erange->end = lstart;
6382can_add_ensure_iseq(
const rb_iseq_t *iseq)
6385 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
6387 if (e->ensure_node)
return false;
6400 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6402 DECL_ANCHOR(ensure);
6404 INIT_ANCHOR(ensure);
6406 if (enlp->erange != NULL) {
6407 DECL_ANCHOR(ensure_part);
6408 LABEL *lstart = NEW_LABEL(0);
6409 LABEL *lend = NEW_LABEL(0);
6410 INIT_ANCHOR(ensure_part);
6412 add_ensure_range(iseq, enlp->erange, lstart, lend);
6414 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6415 ADD_LABEL(ensure_part, lstart);
6416 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6417 ADD_LABEL(ensure_part, lend);
6418 ADD_SEQ(ensure, ensure_part);
6427 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6428 ADD_SEQ(ret, ensure);
6433check_keyword(
const NODE *node)
6437 if (nd_type_p(node, NODE_LIST)) {
6438 while (RNODE_LIST(node)->nd_next) {
6439 node = RNODE_LIST(node)->nd_next;
6441 node = RNODE_LIST(node)->nd_head;
6444 return keyword_node_p(node);
6449keyword_node_single_splat_p(
NODE *kwnode)
6453 NODE *node = RNODE_HASH(kwnode)->nd_head;
6454 return RNODE_LIST(node)->nd_head == NULL &&
6455 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6460 NODE *kwnode,
unsigned int *flag_ptr)
6462 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6463 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6464 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6465 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6466 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6469#define SPLATARRAY_FALSE 0
6470#define SPLATARRAY_TRUE 1
6471#define DUP_SINGLE_KW_SPLAT 2
6475 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6477 if (!argn)
return 0;
6479 NODE *kwnode = NULL;
6481 switch (nd_type(argn)) {
6484 int len = compile_args(iseq, args, argn, &kwnode);
6485 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6488 if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6492 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6493 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6496 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6505 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6506 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6507 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6508 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6509 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6512 case NODE_ARGSCAT: {
6513 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6514 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6515 bool args_pushed =
false;
6517 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6518 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6519 if (kwnode) rest_len--;
6520 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6524 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6525 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6528 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6529 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6530 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6533 else if (!args_pushed) {
6534 ADD_INSN(args, argn, concattoarray);
6540 *flag_ptr |= VM_CALL_KW_SPLAT;
6541 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6547 case NODE_ARGSPUSH: {
6548 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6549 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6551 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6552 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6553 if (kwnode) rest_len--;
6554 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6555 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6558 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6559 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6562 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6563 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6569 *flag_ptr |= VM_CALL_KW_SPLAT;
6570 if (!keyword_node_single_splat_p(kwnode)) {
6571 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6572 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6574 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
6575 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6578 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6586 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
6592setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
6594 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
6595 *flag |= VM_CALL_ARGS_SPLAT_MUT;
6600setup_args_dup_rest_p(
const NODE *argn)
6602 switch(nd_type(argn)) {
6613 case NODE_IMAGINARY:
6626 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
6629 if (setup_args_dup_rest_p(RNODE_LIST(argn)->nd_head)) {
6632 argn = RNODE_LIST(argn)->nd_next;
6645 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
6648 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
6649 RNODE_BLOCK_PASS(argn)->nd_head : argn;
6652 switch(nd_type(check_arg)) {
6655 dup_rest = SPLATARRAY_FALSE;
6659 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
6661 case(NODE_ARGSPUSH):
6663 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
6664 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
6665 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
6666 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
6667 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
6669 if (dup_rest == SPLATARRAY_FALSE) {
6671 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
6673 NODE *key_node = RNODE_LIST(node)->nd_head;
6674 if (key_node && setup_args_dup_rest_p(key_node)) {
6675 dup_rest = SPLATARRAY_TRUE;
6679 node = RNODE_LIST(node)->nd_next;
6680 NODE *value_node = RNODE_LIST(node)->nd_head;
6681 if (setup_args_dup_rest_p(value_node)) {
6682 dup_rest = SPLATARRAY_TRUE;
6686 node = RNODE_LIST(node)->nd_next;
6695 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
6697 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
6700 initial_dup_rest = dup_rest;
6702 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
6703 DECL_ANCHOR(arg_block);
6704 INIT_ANCHOR(arg_block);
6706 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
6707 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
6709 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
6710 const NODE * arg_node =
6711 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
6718 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
6719 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
6722 *flag |= VM_CALL_FORWARDING;
6724 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
6725 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6729 *flag |= VM_CALL_ARGS_BLOCKARG;
6731 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
6734 if (LIST_INSN_SIZE_ONE(arg_block)) {
6736 if (IS_INSN(elem)) {
6738 if (iobj->insn_id == BIN(getblockparam)) {
6739 iobj->insn_id = BIN(getblockparamproxy);
6743 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
6744 ADD_SEQ(args, arg_block);
6747 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
6749 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6756 const NODE *body = ptr;
6757 int line = nd_line(body);
6759 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
6761 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6762 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
6764 iseq_set_local_table(iseq, 0, 0);
6772 int line = nd_line(node);
6773 const NODE *line_node = node;
6774 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
6776#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
6777 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
6781 ADD_INSN(ret, line_node, dup);
6782 ADD_INSNL(ret, line_node, branchunless, fail_label);
6784 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6786 if (RNODE_BLOCK(vars)->nd_next) {
6787 ADD_INSN(ret, line_node, dup);
6790 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6792 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
6795#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
6796 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
6801 ADD_INSNL(nom, line_node, jump, end_label);
6802 ADD_LABEL(nom, fail_label);
6804 ADD_INSN(nom, line_node, pop);
6805 ADD_INSN(nom, line_node, putnil);
6807 ADD_LABEL(nom, end_label);
6808 (nom->last->next = cap->link.next)->prev = nom->last;
6809 (cap->link.next = nom->anchor.next)->prev = &cap->link;
6814 ADD_INSNL(ret, line_node, jump, end_label);
6815 ADD_LABEL(ret, fail_label);
6816 ADD_INSN(ret, line_node, pop);
6817 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6819 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6821 ((
INSN*)last)->insn_id = BIN(putnil);
6822 ((
INSN*)last)->operand_size = 0;
6824 ADD_LABEL(ret, end_label);
6828optimizable_range_item_p(
const NODE *n)
6830 if (!n)
return FALSE;
6831 switch (nd_type(n)) {
6844optimized_range_item(
const NODE *n)
6846 switch (nd_type(n)) {
6848 return rb_node_line_lineno_val(n);
6850 return rb_node_integer_literal_val(n);
6852 return rb_node_float_literal_val(n);
6854 return rb_node_rational_literal_val(n);
6855 case NODE_IMAGINARY:
6856 return rb_node_imaginary_literal_val(n);
6860 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
6867 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
6868 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
6870 const int line = nd_line(node);
6871 const NODE *line_node = node;
6872 DECL_ANCHOR(cond_seq);
6873 LABEL *then_label, *else_label, *end_label;
6876 INIT_ANCHOR(cond_seq);
6877 then_label = NEW_LABEL(line);
6878 else_label = NEW_LABEL(line);
6881 NODE *cond = RNODE_IF(node)->nd_cond;
6882 if (nd_type(cond) == NODE_BLOCK) {
6883 cond = RNODE_BLOCK(cond)->nd_head;
6886 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
6887 ADD_SEQ(ret, cond_seq);
6889 if (then_label->refcnt && else_label->refcnt) {
6890 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
6893 if (then_label->refcnt) {
6894 ADD_LABEL(ret, then_label);
6896 DECL_ANCHOR(then_seq);
6897 INIT_ANCHOR(then_seq);
6898 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
6900 if (else_label->refcnt) {
6901 const NODE *
const coverage_node = node_body ? node_body : node;
6902 add_trace_branch_coverage(
6905 nd_code_loc(coverage_node),
6906 nd_node_id(coverage_node),
6908 type == NODE_IF ?
"then" :
"else",
6910 end_label = NEW_LABEL(line);
6911 ADD_INSNL(then_seq, line_node, jump, end_label);
6913 ADD_INSN(then_seq, line_node, pop);
6916 ADD_SEQ(ret, then_seq);
6919 if (else_label->refcnt) {
6920 ADD_LABEL(ret, else_label);
6922 DECL_ANCHOR(else_seq);
6923 INIT_ANCHOR(else_seq);
6924 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
6926 if (then_label->refcnt) {
6927 const NODE *
const coverage_node = node_else ? node_else : node;
6928 add_trace_branch_coverage(
6931 nd_code_loc(coverage_node),
6932 nd_node_id(coverage_node),
6934 type == NODE_IF ?
"else" :
"then",
6937 ADD_SEQ(ret, else_seq);
6941 ADD_LABEL(ret, end_label);
6951 const NODE *node = orig_node;
6952 LABEL *endlabel, *elselabel;
6954 DECL_ANCHOR(body_seq);
6955 DECL_ANCHOR(cond_seq);
6956 int only_special_literals = 1;
6957 VALUE literals = rb_hash_new();
6959 enum node_type
type;
6960 const NODE *line_node;
6965 INIT_ANCHOR(body_seq);
6966 INIT_ANCHOR(cond_seq);
6968 RHASH_TBL_RAW(literals)->type = &cdhash_type;
6970 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
6972 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
6974 node = RNODE_CASE(node)->nd_body;
6975 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
6976 type = nd_type(node);
6977 line = nd_line(node);
6980 endlabel = NEW_LABEL(line);
6981 elselabel = NEW_LABEL(line);
6985 while (
type == NODE_WHEN) {
6988 l1 = NEW_LABEL(line);
6989 ADD_LABEL(body_seq, l1);
6990 ADD_INSN(body_seq, line_node, pop);
6992 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
6993 add_trace_branch_coverage(
6996 nd_code_loc(coverage_node),
6997 nd_node_id(coverage_node),
7002 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
7003 ADD_INSNL(body_seq, line_node, jump, endlabel);
7005 vals = RNODE_WHEN(node)->nd_head;
7007 switch (nd_type(vals)) {
7009 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
7010 if (only_special_literals < 0)
return COMPILE_NG;
7015 only_special_literals = 0;
7016 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
7019 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
7023 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
7026 node = RNODE_WHEN(node)->nd_next;
7030 type = nd_type(node);
7031 line = nd_line(node);
7036 ADD_LABEL(cond_seq, elselabel);
7037 ADD_INSN(cond_seq, line_node, pop);
7038 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
7039 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7040 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7043 debugs(
"== else (implicit)\n");
7044 ADD_LABEL(cond_seq, elselabel);
7045 ADD_INSN(cond_seq, orig_node, pop);
7046 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
7048 ADD_INSN(cond_seq, orig_node, putnil);
7050 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7053 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7054 ADD_INSN(ret, orig_node, dup);
7055 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
7057 LABEL_REF(elselabel);
7060 ADD_SEQ(ret, cond_seq);
7061 ADD_SEQ(ret, body_seq);
7062 ADD_LABEL(ret, endlabel);
7071 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
7073 DECL_ANCHOR(body_seq);
7077 branches = decl_branch_base(iseq, PTR2NUM(orig_node), nd_code_loc(orig_node),
"case");
7079 INIT_ANCHOR(body_seq);
7080 endlabel = NEW_LABEL(nd_line(node));
7082 while (node && nd_type_p(node, NODE_WHEN)) {
7083 const int line = nd_line(node);
7084 LABEL *l1 = NEW_LABEL(line);
7085 ADD_LABEL(body_seq, l1);
7087 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7088 add_trace_branch_coverage(
7091 nd_code_loc(coverage_node),
7092 nd_node_id(coverage_node),
7097 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7098 ADD_INSNL(body_seq, node, jump, endlabel);
7100 vals = RNODE_WHEN(node)->nd_head;
7102 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7104 switch (nd_type(vals)) {
7108 val = RNODE_LIST(vals)->nd_head;
7109 lnext = NEW_LABEL(nd_line(val));
7110 debug_compile(
"== when2\n", (
void)0);
7111 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7112 ADD_LABEL(ret, lnext);
7113 vals = RNODE_LIST(vals)->nd_next;
7119 ADD_INSN(ret, vals, putnil);
7120 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7121 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7122 ADD_INSNL(ret, vals, branchif, l1);
7125 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7127 node = RNODE_WHEN(node)->nd_next;
7130 const NODE *
const coverage_node = node ? node : orig_node;
7131 add_trace_branch_coverage(
7134 nd_code_loc(coverage_node),
7135 nd_node_id(coverage_node),
7139 CHECK(COMPILE_(ret,
"else", node, popped));
7140 ADD_INSNL(ret, orig_node, jump, endlabel);
7142 ADD_SEQ(ret, body_seq);
7143 ADD_LABEL(ret, endlabel);
7147static 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);
7149static 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);
7150static 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);
7151static int iseq_compile_pattern_set_general_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7152static 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);
7153static int iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7155#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7156#define CASE3_BI_OFFSET_ERROR_STRING 1
7157#define CASE3_BI_OFFSET_KEY_ERROR_P 2
7158#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7159#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7162iseq_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)
7164 const int line = nd_line(node);
7165 const NODE *line_node = node;
7167 switch (nd_type(node)) {
7221 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7222 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7223 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7225 const int min_argc = pre_args_num + post_args_num;
7226 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7227 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7229 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7231 match_failed = NEW_LABEL(line);
7232 type_error = NEW_LABEL(line);
7233 deconstruct = NEW_LABEL(line);
7234 deconstructed = NEW_LABEL(line);
7237 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7238 ADD_INSN(ret, line_node, swap);
7244 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7246 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7248 ADD_INSN(ret, line_node, dup);
7249 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7250 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7251 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7252 if (in_single_pattern) {
7253 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7254 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7255 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7256 INT2FIX(min_argc), base_index + 1 ));
7258 ADD_INSNL(ret, line_node, branchunless, match_failed);
7260 for (i = 0; i < pre_args_num; i++) {
7261 ADD_INSN(ret, line_node, dup);
7262 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7263 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7264 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7265 args = RNODE_LIST(args)->nd_next;
7268 if (RNODE_ARYPTN(node)->rest_arg) {
7269 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7270 ADD_INSN(ret, line_node, dup);
7271 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7272 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7273 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7274 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7275 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7276 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7277 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7279 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7282 if (post_args_num > 0) {
7283 ADD_INSN(ret, line_node, dup);
7284 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7285 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7286 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7287 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7288 ADD_INSN(ret, line_node, pop);
7293 args = RNODE_ARYPTN(node)->post_args;
7294 for (i = 0; i < post_args_num; i++) {
7295 ADD_INSN(ret, line_node, dup);
7297 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7298 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7299 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7301 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7302 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7303 args = RNODE_LIST(args)->nd_next;
7306 ADD_INSN(ret, line_node, pop);
7308 ADD_INSN(ret, line_node, pop);
7310 ADD_INSNL(ret, line_node, jump, matched);
7311 ADD_INSN(ret, line_node, putnil);
7313 ADD_INSN(ret, line_node, putnil);
7316 ADD_LABEL(ret, type_error);
7317 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7319 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7320 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7321 ADD_INSN(ret, line_node, pop);
7323 ADD_LABEL(ret, match_failed);
7324 ADD_INSN(ret, line_node, pop);
7326 ADD_INSN(ret, line_node, pop);
7328 ADD_INSNL(ret, line_node, jump, unmatched);
7381 const NODE *args = RNODE_FNDPTN(node)->args;
7382 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7384 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7385 match_failed = NEW_LABEL(line);
7386 type_error = NEW_LABEL(line);
7387 deconstruct = NEW_LABEL(line);
7388 deconstructed = NEW_LABEL(line);
7390 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7392 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7394 ADD_INSN(ret, line_node, dup);
7395 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7396 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7397 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7398 if (in_single_pattern) {
7399 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 ));
7401 ADD_INSNL(ret, line_node, branchunless, match_failed);
7404 LABEL *while_begin = NEW_LABEL(nd_line(node));
7405 LABEL *next_loop = NEW_LABEL(nd_line(node));
7406 LABEL *find_succeeded = NEW_LABEL(line);
7407 LABEL *find_failed = NEW_LABEL(nd_line(node));
7410 ADD_INSN(ret, line_node, dup);
7411 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7413 ADD_INSN(ret, line_node, dup);
7414 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7415 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7417 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7419 ADD_LABEL(ret, while_begin);
7421 ADD_INSN(ret, line_node, dup);
7422 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7423 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7424 ADD_INSNL(ret, line_node, branchunless, find_failed);
7426 for (j = 0; j < args_num; j++) {
7427 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7428 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7430 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7431 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7433 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7435 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7436 args = RNODE_LIST(args)->nd_next;
7439 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7440 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7441 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7442 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7443 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7444 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));
7446 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7447 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7448 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7449 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7450 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7451 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7452 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7453 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));
7455 ADD_INSNL(ret, line_node, jump, find_succeeded);
7457 ADD_LABEL(ret, next_loop);
7458 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7459 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7460 ADD_INSNL(ret, line_node, jump, while_begin);
7462 ADD_LABEL(ret, find_failed);
7463 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7464 if (in_single_pattern) {
7465 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7466 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7467 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7468 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7469 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7471 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7472 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7474 ADD_INSN(ret, line_node, pop);
7475 ADD_INSN(ret, line_node, pop);
7477 ADD_INSNL(ret, line_node, jump, match_failed);
7478 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7480 ADD_LABEL(ret, find_succeeded);
7481 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7484 ADD_INSN(ret, line_node, pop);
7485 ADD_INSNL(ret, line_node, jump, matched);
7486 ADD_INSN(ret, line_node, putnil);
7488 ADD_LABEL(ret, type_error);
7489 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7491 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7492 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7493 ADD_INSN(ret, line_node, pop);
7495 ADD_LABEL(ret, match_failed);
7496 ADD_INSN(ret, line_node, pop);
7497 ADD_INSNL(ret, line_node, jump, unmatched);
7561 LABEL *match_failed, *type_error;
7564 match_failed = NEW_LABEL(line);
7565 type_error = NEW_LABEL(line);
7567 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
7568 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7569 keys =
rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
7571 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
7572 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
7576 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7578 ADD_INSN(ret, line_node, dup);
7579 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
7580 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7581 if (in_single_pattern) {
7582 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
7584 ADD_INSNL(ret, line_node, branchunless, match_failed);
7587 ADD_INSN(ret, line_node, putnil);
7590 ADD_INSN1(ret, line_node, duparray, keys);
7593 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
7595 ADD_INSN(ret, line_node, dup);
7597 ADD_INSNL(ret, line_node, branchunless, type_error);
7599 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7600 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
7603 if (RNODE_HSHPTN(node)->nd_pkwargs) {
7607 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7609 DECL_ANCHOR(match_values);
7610 INIT_ANCHOR(match_values);
7611 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
7612 for (i = 0; i < keys_num; i++) {
7613 NODE *key_node = RNODE_LIST(args)->nd_head;
7614 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
7615 VALUE key = get_symbol_value(iseq, key_node);
7617 ADD_INSN(ret, line_node, dup);
7618 ADD_INSN1(ret, line_node, putobject, key);
7619 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
7620 if (in_single_pattern) {
7621 LABEL *match_succeeded;
7622 match_succeeded = NEW_LABEL(line);
7624 ADD_INSN(ret, line_node, dup);
7625 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7627 ADD_INSN1(ret, line_node, putobject,
rb_str_freeze(rb_sprintf(
"key not found: %+"PRIsVALUE, key)));
7628 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
7629 ADD_INSN1(ret, line_node, putobject,
Qtrue);
7630 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
7631 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7632 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
7633 ADD_INSN1(ret, line_node, putobject, key);
7634 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
7636 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
7638 ADD_LABEL(ret, match_succeeded);
7640 ADD_INSNL(ret, line_node, branchunless, match_failed);
7642 ADD_INSN(match_values, line_node, dup);
7643 ADD_INSN1(match_values, line_node, putobject, key);
7644 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
7645 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7646 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
7648 ADD_SEQ(ret, match_values);
7652 ADD_INSN(ret, line_node, dup);
7653 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7654 if (in_single_pattern) {
7655 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
7657 ADD_INSNL(ret, line_node, branchunless, match_failed);
7660 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7661 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
7662 ADD_INSN(ret, line_node, dup);
7663 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7664 if (in_single_pattern) {
7665 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
7667 ADD_INSNL(ret, line_node, branchunless, match_failed);
7670 ADD_INSN(ret, line_node, dup);
7671 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7675 ADD_INSN(ret, line_node, pop);
7676 ADD_INSNL(ret, line_node, jump, matched);
7677 ADD_INSN(ret, line_node, putnil);
7679 ADD_LABEL(ret, type_error);
7680 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7682 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
7683 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7684 ADD_INSN(ret, line_node, pop);
7686 ADD_LABEL(ret, match_failed);
7687 ADD_INSN(ret, line_node, pop);
7688 ADD_INSNL(ret, line_node, jump, unmatched);
7697 case NODE_IMAGINARY:
7725 CHECK(COMPILE(ret,
"case in literal", node));
7726 if (in_single_pattern) {
7727 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7729 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7730 if (in_single_pattern) {
7731 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
7733 ADD_INSNL(ret, line_node, branchif, matched);
7734 ADD_INSNL(ret, line_node, jump, unmatched);
7738 ID id = RNODE_LASGN(node)->nd_vid;
7739 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
7741 if (in_alt_pattern) {
7742 const char *name = rb_id2name(
id);
7743 if (name && strlen(name) > 0 && name[0] !=
'_') {
7744 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7750 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
7751 ADD_INSNL(ret, line_node, jump, matched);
7756 ID id = RNODE_DASGN(node)->nd_vid;
7758 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
7760 if (in_alt_pattern) {
7761 const char *name = rb_id2name(
id);
7762 if (name && strlen(name) > 0 && name[0] !=
'_') {
7763 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7770 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
7774 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
7775 ADD_INSNL(ret, line_node, jump, matched);
7780 LABEL *match_failed;
7781 match_failed = unmatched;
7782 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7783 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
7784 if (in_single_pattern) {
7785 LABEL *match_succeeded;
7786 match_succeeded = NEW_LABEL(line);
7788 ADD_INSN(ret, line_node, dup);
7789 if (nd_type_p(node, NODE_IF)) {
7790 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7793 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
7796 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
7797 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7798 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7799 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7801 ADD_INSN(ret, line_node, pop);
7802 ADD_INSN(ret, line_node, pop);
7804 ADD_LABEL(ret, match_succeeded);
7806 if (nd_type_p(node, NODE_IF)) {
7807 ADD_INSNL(ret, line_node, branchunless, match_failed);
7810 ADD_INSNL(ret, line_node, branchif, match_failed);
7812 ADD_INSNL(ret, line_node, jump, matched);
7817 LABEL *match_failed;
7818 match_failed = NEW_LABEL(line);
7820 n = RNODE_HASH(node)->nd_head;
7821 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
7822 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7826 ADD_INSN(ret, line_node, dup);
7827 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));
7828 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));
7829 ADD_INSN(ret, line_node, putnil);
7831 ADD_LABEL(ret, match_failed);
7832 ADD_INSN(ret, line_node, pop);
7833 ADD_INSNL(ret, line_node, jump, unmatched);
7837 LABEL *match_succeeded, *fin;
7838 match_succeeded = NEW_LABEL(line);
7839 fin = NEW_LABEL(line);
7841 ADD_INSN(ret, line_node, dup);
7842 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));
7843 ADD_LABEL(ret, match_succeeded);
7844 ADD_INSN(ret, line_node, pop);
7845 ADD_INSNL(ret, line_node, jump, matched);
7846 ADD_INSN(ret, line_node, putnil);
7847 ADD_LABEL(ret, fin);
7848 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
7852 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
7858iseq_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)
7860 LABEL *fin = NEW_LABEL(nd_line(node));
7861 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7862 ADD_LABEL(ret, fin);
7867iseq_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)
7869 const NODE *line_node = node;
7871 if (RNODE_ARYPTN(node)->nd_pconst) {
7872 ADD_INSN(ret, line_node, dup);
7873 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
7874 if (in_single_pattern) {
7875 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7877 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7878 if (in_single_pattern) {
7879 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
7881 ADD_INSNL(ret, line_node, branchunless, match_failed);
7888iseq_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)
7890 const NODE *line_node = node;
7894 if (use_deconstructed_cache) {
7896 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7897 ADD_INSNL(ret, line_node, branchnil, deconstruct);
7900 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7901 ADD_INSNL(ret, line_node, branchunless, match_failed);
7904 ADD_INSN(ret, line_node, pop);
7905 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
7906 ADD_INSNL(ret, line_node, jump, deconstructed);
7909 ADD_INSNL(ret, line_node, jump, deconstruct);
7912 ADD_LABEL(ret, deconstruct);
7913 ADD_INSN(ret, line_node, dup);
7914 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
7915 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7918 if (use_deconstructed_cache) {
7919 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
7922 if (in_single_pattern) {
7923 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
7926 ADD_INSNL(ret, line_node, branchunless, match_failed);
7928 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
7931 if (use_deconstructed_cache) {
7932 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7935 ADD_INSN(ret, line_node, dup);
7937 ADD_INSNL(ret, line_node, branchunless, type_error);
7939 ADD_LABEL(ret, deconstructed);
7955 const int line = nd_line(node);
7956 const NODE *line_node = node;
7957 LABEL *match_succeeded = NEW_LABEL(line);
7959 ADD_INSN(ret, line_node, dup);
7960 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7962 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7963 ADD_INSN1(ret, line_node, putobject, errmsg);
7964 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7965 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7966 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7968 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7969 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7971 ADD_INSN(ret, line_node, pop);
7972 ADD_INSN(ret, line_node, pop);
7973 ADD_LABEL(ret, match_succeeded);
7989 const int line = nd_line(node);
7990 const NODE *line_node = node;
7991 LABEL *match_succeeded = NEW_LABEL(line);
7993 ADD_INSN(ret, line_node, dup);
7994 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7996 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7997 ADD_INSN1(ret, line_node, putobject, errmsg);
7998 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7999 ADD_INSN(ret, line_node, dup);
8000 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
8001 ADD_INSN1(ret, line_node, putobject, pattern_length);
8002 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
8003 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8005 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8006 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8008 ADD_INSN(ret, line_node, pop);
8009 ADD_INSN(ret, line_node, pop);
8010 ADD_LABEL(ret, match_succeeded);
8016iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
8026 const int line = nd_line(node);
8027 const NODE *line_node = node;
8028 LABEL *match_succeeded = NEW_LABEL(line);
8030 ADD_INSN(ret, line_node, dup);
8031 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8033 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8034 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
8035 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8036 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
8037 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
8038 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8040 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8041 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8043 ADD_INSN(ret, line_node, pop);
8044 ADD_INSN(ret, line_node, pop);
8046 ADD_LABEL(ret, match_succeeded);
8047 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
8048 ADD_INSN(ret, line_node, pop);
8049 ADD_INSN(ret, line_node, pop);
8057 const NODE *pattern;
8058 const NODE *node = orig_node;
8059 LABEL *endlabel, *elselabel;
8061 DECL_ANCHOR(body_seq);
8062 DECL_ANCHOR(cond_seq);
8064 enum node_type
type;
8065 const NODE *line_node;
8068 bool single_pattern;
8071 INIT_ANCHOR(body_seq);
8072 INIT_ANCHOR(cond_seq);
8074 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
8076 node = RNODE_CASE3(node)->nd_body;
8077 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
8078 type = nd_type(node);
8079 line = nd_line(node);
8081 single_pattern = !RNODE_IN(node)->nd_next;
8083 endlabel = NEW_LABEL(line);
8084 elselabel = NEW_LABEL(line);
8086 if (single_pattern) {
8088 ADD_INSN(head, line_node, putnil);
8089 ADD_INSN(head, line_node, putnil);
8090 ADD_INSN1(head, line_node, putobject,
Qfalse);
8091 ADD_INSN(head, line_node, putnil);
8093 ADD_INSN(head, line_node, putnil);
8095 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8099 while (
type == NODE_IN) {
8103 ADD_INSN(body_seq, line_node, putnil);
8105 l1 = NEW_LABEL(line);
8106 ADD_LABEL(body_seq, l1);
8107 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8109 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8110 add_trace_branch_coverage(
8113 nd_code_loc(coverage_node),
8114 nd_node_id(coverage_node),
8119 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8120 ADD_INSNL(body_seq, line_node, jump, endlabel);
8122 pattern = RNODE_IN(node)->nd_head;
8124 int pat_line = nd_line(pattern);
8125 LABEL *next_pat = NEW_LABEL(pat_line);
8126 ADD_INSN (cond_seq, pattern, dup);
8128 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8129 ADD_LABEL(cond_seq, next_pat);
8130 LABEL_UNREMOVABLE(next_pat);
8133 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8137 node = RNODE_IN(node)->nd_next;
8141 type = nd_type(node);
8142 line = nd_line(node);
8147 ADD_LABEL(cond_seq, elselabel);
8148 ADD_INSN(cond_seq, line_node, pop);
8149 ADD_INSN(cond_seq, line_node, pop);
8150 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8151 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8152 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8153 ADD_INSN(cond_seq, line_node, putnil);
8155 ADD_INSN(cond_seq, line_node, putnil);
8159 debugs(
"== else (implicit)\n");
8160 ADD_LABEL(cond_seq, elselabel);
8161 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8162 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8164 if (single_pattern) {
8172 LABEL *key_error, *fin;
8175 key_error = NEW_LABEL(line);
8176 fin = NEW_LABEL(line);
8179 kw_arg->references = 0;
8180 kw_arg->keyword_len = 2;
8181 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
8182 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
8184 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8185 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8187 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8188 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8189 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8190 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8191 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8192 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8193 ADD_INSNL(cond_seq, orig_node, jump, fin);
8195 ADD_LABEL(cond_seq, key_error);
8197 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8198 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8199 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8200 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8201 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8202 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8203 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8204 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
8205 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8207 ADD_LABEL(cond_seq, fin);
8211 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8212 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8214 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8216 ADD_INSN(cond_seq, orig_node, putnil);
8218 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8219 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8221 ADD_INSN(cond_seq, line_node, putnil);
8225 ADD_SEQ(ret, cond_seq);
8226 ADD_SEQ(ret, body_seq);
8227 ADD_LABEL(ret, endlabel);
8231#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8232#undef CASE3_BI_OFFSET_ERROR_STRING
8233#undef CASE3_BI_OFFSET_KEY_ERROR_P
8234#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8235#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8240 const int line = (int)nd_line(node);
8241 const NODE *line_node = node;
8243 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8244 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8245 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8246 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8251 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8252 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8253 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8254 LABEL *end_label = NEW_LABEL(line);
8255 LABEL *adjust_label = NEW_LABEL(line);
8257 LABEL *next_catch_label = NEW_LABEL(line);
8258 LABEL *tmp_label = NULL;
8260 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8261 push_ensure_entry(iseq, &enl, NULL, NULL);
8263 if (RNODE_WHILE(node)->nd_state == 1) {
8264 ADD_INSNL(ret, line_node, jump, next_label);
8267 tmp_label = NEW_LABEL(line);
8268 ADD_INSNL(ret, line_node, jump, tmp_label);
8270 ADD_LABEL(ret, adjust_label);
8271 ADD_INSN(ret, line_node, putnil);
8272 ADD_LABEL(ret, next_catch_label);
8273 ADD_INSN(ret, line_node, pop);
8274 ADD_INSNL(ret, line_node, jump, next_label);
8275 if (tmp_label) ADD_LABEL(ret, tmp_label);
8277 ADD_LABEL(ret, redo_label);
8278 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8280 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8281 add_trace_branch_coverage(
8284 nd_code_loc(coverage_node),
8285 nd_node_id(coverage_node),
8290 CHECK(COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body));
8291 ADD_LABEL(ret, next_label);
8293 if (
type == NODE_WHILE) {
8294 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8295 redo_label, end_label));
8299 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8300 end_label, redo_label));
8303 ADD_LABEL(ret, end_label);
8304 ADD_ADJUST_RESTORE(ret, adjust_label);
8306 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8308 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8312 ADD_INSN(ret, line_node, putnil);
8315 ADD_LABEL(ret, break_label);
8318 ADD_INSN(ret, line_node, pop);
8321 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8323 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8325 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8326 ISEQ_COMPILE_DATA(iseq)->redo_label);
8328 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8329 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8330 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8331 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8332 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
8339 const int line = nd_line(node);
8340 const NODE *line_node = node;
8341 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8342 LABEL *retry_label = NEW_LABEL(line);
8343 LABEL *retry_end_l = NEW_LABEL(line);
8346 ADD_LABEL(ret, retry_label);
8347 if (nd_type_p(node, NODE_FOR)) {
8348 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8350 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8351 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8352 ISEQ_TYPE_BLOCK, line);
8353 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8356 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8357 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8358 ISEQ_TYPE_BLOCK, line);
8359 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8373 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
8374 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8375 iobj = (
INSN*) get_prev_insn(iobj);
8377 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
8381 if (&iobj->link == LAST_ELEMENT(ret)) {
8387 ADD_INSN(ret, line_node, pop);
8390 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8392 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8402 const NODE *line_node = node;
8403 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8404 LABEL *not_single = NEW_LABEL(nd_line(var));
8405 LABEL *not_ary = NEW_LABEL(nd_line(var));
8406 CHECK(COMPILE(ret,
"for var", var));
8407 ADD_INSN(ret, line_node, dup);
8408 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8409 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8410 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8411 ADD_INSNL(ret, line_node, branchunless, not_single);
8412 ADD_INSN(ret, line_node, dup);
8413 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8414 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8415 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8416 ADD_INSN(ret, line_node, swap);
8417 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
8418 ADD_INSN(ret, line_node, dup);
8419 ADD_INSNL(ret, line_node, branchunless, not_ary);
8420 ADD_INSN(ret, line_node, swap);
8421 ADD_LABEL(ret, not_ary);
8422 ADD_INSN(ret, line_node, pop);
8423 ADD_LABEL(ret, not_single);
8430 const NODE *line_node = node;
8431 unsigned long throw_flag = 0;
8433 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8435 LABEL *splabel = NEW_LABEL(0);
8436 ADD_LABEL(ret, splabel);
8437 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8438 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8439 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8440 add_ensure_iseq(ret, iseq, 0);
8441 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8442 ADD_ADJUST_RESTORE(ret, splabel);
8445 ADD_INSN(ret, line_node, putnil);
8452 if (!ISEQ_COMPILE_DATA(ip)) {
8457 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8458 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8460 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8463 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8464 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8468 ip = ISEQ_BODY(ip)->parent_iseq;
8473 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8474 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8476 ADD_INSN(ret, line_node, pop);
8480 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8489 const NODE *line_node = node;
8490 unsigned long throw_flag = 0;
8492 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8493 LABEL *splabel = NEW_LABEL(0);
8494 debugs(
"next in while loop\n");
8495 ADD_LABEL(ret, splabel);
8496 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8497 add_ensure_iseq(ret, iseq, 0);
8498 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8499 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8500 ADD_ADJUST_RESTORE(ret, splabel);
8502 ADD_INSN(ret, line_node, putnil);
8505 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8506 LABEL *splabel = NEW_LABEL(0);
8507 debugs(
"next in block\n");
8508 ADD_LABEL(ret, splabel);
8509 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8510 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8511 add_ensure_iseq(ret, iseq, 0);
8512 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8513 ADD_ADJUST_RESTORE(ret, splabel);
8516 ADD_INSN(ret, line_node, putnil);
8523 if (!ISEQ_COMPILE_DATA(ip)) {
8528 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8529 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8533 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8536 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8537 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8541 ip = ISEQ_BODY(ip)->parent_iseq;
8544 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8545 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8548 ADD_INSN(ret, line_node, pop);
8552 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
8562 const NODE *line_node = node;
8564 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8565 LABEL *splabel = NEW_LABEL(0);
8566 debugs(
"redo in while");
8567 ADD_LABEL(ret, splabel);
8568 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8569 add_ensure_iseq(ret, iseq, 0);
8570 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8571 ADD_ADJUST_RESTORE(ret, splabel);
8573 ADD_INSN(ret, line_node, putnil);
8576 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8577 LABEL *splabel = NEW_LABEL(0);
8579 debugs(
"redo in block");
8580 ADD_LABEL(ret, splabel);
8581 add_ensure_iseq(ret, iseq, 0);
8582 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8583 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8584 ADD_ADJUST_RESTORE(ret, splabel);
8587 ADD_INSN(ret, line_node, putnil);
8594 if (!ISEQ_COMPILE_DATA(ip)) {
8599 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8602 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8605 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8606 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
8610 ip = ISEQ_BODY(ip)->parent_iseq;
8613 ADD_INSN(ret, line_node, putnil);
8614 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8617 ADD_INSN(ret, line_node, pop);
8621 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
8631 const NODE *line_node = node;
8633 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
8634 ADD_INSN(ret, line_node, putnil);
8635 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
8638 ADD_INSN(ret, line_node, pop);
8642 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
8651 const int line = nd_line(node);
8652 const NODE *line_node = node;
8653 LABEL *lstart = NEW_LABEL(line);
8654 LABEL *lend = NEW_LABEL(line);
8655 LABEL *lcont = NEW_LABEL(line);
8656 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
8658 ISEQ_BODY(iseq)->location.label),
8659 ISEQ_TYPE_RESCUE, line);
8661 lstart->rescued = LABEL_RESCUE_BEG;
8662 lend->rescued = LABEL_RESCUE_END;
8663 ADD_LABEL(ret, lstart);
8665 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
8666 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
8668 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
8670 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
8672 ADD_LABEL(ret, lend);
8673 if (RNODE_RESCUE(node)->nd_else) {
8674 ADD_INSN(ret, line_node, pop);
8675 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
8677 ADD_INSN(ret, line_node, nop);
8678 ADD_LABEL(ret, lcont);
8681 ADD_INSN(ret, line_node, pop);
8685 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
8686 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
8693 const int line = nd_line(node);
8694 const NODE *line_node = node;
8695 const NODE *resq = node;
8697 LABEL *label_miss, *label_hit;
8700 label_miss = NEW_LABEL(line);
8701 label_hit = NEW_LABEL(line);
8703 narg = RNODE_RESBODY(resq)->nd_args;
8705 switch (nd_type(narg)) {
8708 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8709 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
8710 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8711 ADD_INSNL(ret, line_node, branchif, label_hit);
8712 narg = RNODE_LIST(narg)->nd_next;
8718 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8719 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
8720 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
8721 ADD_INSNL(ret, line_node, branchif, label_hit);
8724 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
8728 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8730 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8731 ADD_INSNL(ret, line_node, branchif, label_hit);
8733 ADD_INSNL(ret, line_node, jump, label_miss);
8734 ADD_LABEL(ret, label_hit);
8737 if (RNODE_RESBODY(resq)->nd_exc_var) {
8738 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
8741 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) {
8743 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
8746 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
8749 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
8750 ADD_INSN(ret, line_node, nop);
8752 ADD_INSN(ret, line_node, leave);
8753 ADD_LABEL(ret, label_miss);
8754 resq = RNODE_RESBODY(resq)->nd_next;
8762 const int line = nd_line(RNODE_ENSURE(node)->nd_ensr);
8763 const NODE *line_node = node;
8765 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
8767 ISEQ_TYPE_ENSURE, line);
8768 LABEL *lstart = NEW_LABEL(line);
8769 LABEL *lend = NEW_LABEL(line);
8770 LABEL *lcont = NEW_LABEL(line);
8778 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
8780 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
8785 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
8787 ADD_LABEL(ret, lstart);
8788 CHECK(COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
8789 ADD_LABEL(ret, lend);
8791 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
8792 ADD_LABEL(ret, lcont);
8793 if (last_leave) ADD_INSN(ret, line_node, pop);
8795 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
8796 if (lstart->link.next != &lend->link) {
8798 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
8800 erange = erange->next;
8804 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
8811 const NODE *line_node = node;
8814 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8816 enum rb_iseq_type t =
type;
8817 const NODE *retval = RNODE_RETURN(node)->nd_stts;
8820 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
8821 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
8822 t = ISEQ_BODY(is)->type;
8826 case ISEQ_TYPE_MAIN:
8828 rb_warn(
"argument of top-level return is ignored");
8832 type = ISEQ_TYPE_METHOD;
8839 if (
type == ISEQ_TYPE_METHOD) {
8840 splabel = NEW_LABEL(0);
8841 ADD_LABEL(ret, splabel);
8842 ADD_ADJUST(ret, line_node, 0);
8845 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
8847 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8848 add_ensure_iseq(ret, iseq, 1);
8850 ADD_INSN(ret, line_node, leave);
8851 ADD_ADJUST_RESTORE(ret, splabel);
8854 ADD_INSN(ret, line_node, putnil);
8858 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
8860 ADD_INSN(ret, line_node, pop);
8871 if (!i)
return false;
8872 if (IS_TRACE(i)) i = i->prev;
8873 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
8875 if (IS_ADJUST(i)) i = i->prev;
8876 if (!IS_INSN(i))
return false;
8877 switch (INSN_OF(i)) {
8884 (ret->last = last->prev)->next = NULL;
8891 CHECK(COMPILE_(ret,
"nd_body", node, popped));
8893 if (!popped && !all_string_result_p(node)) {
8894 const NODE *line_node = node;
8895 const unsigned int flag = VM_CALL_FCALL;
8899 ADD_INSN(ret, line_node, dup);
8900 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
8901 ADD_INSN(ret, line_node, anytostring);
8909 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8911 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
8912 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8918 LABEL *else_label = NEW_LABEL(nd_line(line_node));
8921 br = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"&.");
8923 ADD_INSN(recv, line_node, dup);
8924 ADD_INSNL(recv, line_node, branchnil, else_label);
8925 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
8933 if (!else_label)
return;
8934 end_label = NEW_LABEL(nd_line(line_node));
8935 ADD_INSNL(ret, line_node, jump, end_label);
8936 ADD_LABEL(ret, else_label);
8937 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
8938 ADD_LABEL(ret, end_label);
8947 if (get_nd_recv(node) &&
8948 (nd_type_p(get_nd_recv(node), NODE_STR) || nd_type_p(get_nd_recv(node), NODE_FILE)) &&
8949 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
8950 get_nd_args(node) == NULL &&
8951 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8952 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8953 VALUE str = get_string_value(get_nd_recv(node));
8954 if (get_node_call_nd_mid(node) == idUMinus) {
8955 ADD_INSN2(ret, line_node, opt_str_uminus, str,
8956 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
8959 ADD_INSN2(ret, line_node, opt_str_freeze, str,
8960 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
8964 ADD_INSN(ret, line_node, pop);
8971 if (get_node_call_nd_mid(node) == idAREF && !private_recv_p(node) && get_nd_args(node) &&
8972 nd_type_p(get_nd_args(node), NODE_LIST) && RNODE_LIST(get_nd_args(node))->as.nd_alen == 1 &&
8973 (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)) &&
8974 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8975 !frozen_string_literal_p(iseq) &&
8976 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8977 VALUE str = get_string_value(RNODE_LIST(get_nd_args(node))->nd_head);
8978 CHECK(COMPILE(ret,
"recv", get_nd_recv(node)));
8979 ADD_INSN2(ret, line_node, opt_aref_with, str,
8980 new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
8983 ADD_INSN(ret, line_node, pop);
8991iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
8993 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
8997iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
9000 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
9001 for (i=0; table[i].index != -1; i++) {
9002 if (strcmp(table[i].name, name) == 0) {
9010iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
9012 const char *name = rb_id2name(mid);
9013 static const char prefix[] =
"__builtin_";
9014 const size_t prefix_len =
sizeof(prefix) - 1;
9019 switch (nd_type(recv)) {
9021 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
9026 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
9036 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
9037 return &name[prefix_len];
9046delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
9053 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
9054 unsigned int start=0;
9059 argc + start <= ISEQ_BODY(iseq)->local_table_size;
9063 for (
unsigned int i=start; i-start<argc; i++) {
9064 if (IS_INSN(elem) &&
9065 INSN_OF(elem) == BIN(getlocal)) {
9066 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
9067 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
9069 if (local_level == 0) {
9070 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
9072 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
9073 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
9074 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
9075 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9099 *pstart_index = start;
9113 if (!node)
goto no_arg;
9115 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9116 const NODE *next = RNODE_LIST(node)->nd_next;
9118 node = RNODE_LIST(node)->nd_head;
9119 if (!node)
goto no_arg;
9120 switch (nd_type(node)) {
9122 symbol = rb_node_sym_string_val(node);
9128 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9131 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
9132 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9134 else if (strcmp(RSTRING_PTR(
string),
"inline_block") == 0) {
9135 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9137 else if (strcmp(RSTRING_PTR(
string),
"use_block") == 0) {
9138 iseq_set_use_block(iseq);
9140 else if (strcmp(RSTRING_PTR(
string),
"c_trace") == 0) {
9142 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9151 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9154 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9157 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %s", RSTRING_PTR(
string));
9160 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9168 if (!node)
goto no_arg;
9169 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9170 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9171 node = RNODE_LIST(node)->nd_head;
9172 if (!node)
goto no_arg;
9173 switch (nd_type(node)) {
9175 name = rb_node_sym_string_val(node);
9180 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9182 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9186 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9189 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9192 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9193 rb_builtin_class_name(name));
9196 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9202 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9203 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9204 return RNODE_IF(node)->nd_body;
9207 rb_bug(
"mandatory_node: can't find mandatory node");
9212compile_builtin_mandatory_only_method(
rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9216 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9219 rb_node_init(RNODE(&args_node), NODE_ARGS);
9220 args_node.nd_ainfo = args;
9223 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9224 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9228 tbl->size = table_size;
9233 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9234 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9237 for (; i<table_size; i++) {
9238 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9242 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9243 scope_node.nd_tbl = tbl;
9244 scope_node.nd_body = mandatory_node(iseq, node);
9245 scope_node.nd_args = &args_node;
9247 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9250 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9251 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9252 nd_line(line_node), NULL, 0,
9253 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9254 ISEQ_BODY(iseq)->variable.script_lines);
9255 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
9265 NODE *args_node = get_nd_args(node);
9267 if (parent_block != NULL) {
9268 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9272# define BUILTIN_INLINE_PREFIX "_bi"
9273 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9274 bool cconst =
false;
9279 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9280 strcmp(
"cexpr!", builtin_func) == 0) {
9283 else if (strcmp(
"cconst!", builtin_func) == 0) {
9286 else if (strcmp(
"cinit!", builtin_func) == 0) {
9290 else if (strcmp(
"attr!", builtin_func) == 0) {
9291 return compile_builtin_attr(iseq, args_node);
9293 else if (strcmp(
"arg!", builtin_func) == 0) {
9294 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9296 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9298 rb_bug(
"mandatory_only? should be in if condition");
9300 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9301 rb_bug(
"mandatory_only? should be put on top");
9304 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9305 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9308 rb_bug(
"can't find builtin function:%s", builtin_func);
9311 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9315 int inline_index = nd_line(node);
9316 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9317 builtin_func = inline_func;
9323 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9324 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9325 ADD_INSN1(ret, line_node, putobject, const_val);
9331 unsigned int flag = 0;
9333 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9335 if (
FIX2INT(argc) != bf->argc) {
9336 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9337 builtin_func, bf->argc,
FIX2INT(argc));
9341 unsigned int start_index;
9342 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9343 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9347 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9350 if (popped) ADD_INSN(ret, line_node, pop);
9356compile_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)
9364 ID mid = get_node_call_nd_mid(node);
9366 unsigned int flag = 0;
9368 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9369 LABEL *else_label = NULL;
9372 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9378 if (nd_type_p(node, NODE_VCALL)) {
9383 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9385 if (mid == id_bitblt) {
9386 ADD_INSN(ret, line_node, bitblt);
9389 else if (mid == id_answer) {
9390 ADD_INSN(ret, line_node, answer);
9402 if (nd_type_p(node, NODE_FCALL) &&
9403 (mid == goto_id || mid == label_id)) {
9406 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9409 if (!labels_table) {
9410 labels_table = st_init_numtable();
9411 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9414 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9418 if (mid == goto_id) {
9419 ADD_INSNL(ret, line_node, jump, label);
9422 ADD_LABEL(ret, label);
9429 const char *builtin_func;
9430 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9431 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9432 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9436 if (!assume_receiver) {
9437 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9440 if (mid == idCall &&
9441 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9442 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9443 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9445 else if (private_recv_p(node)) {
9446 ADD_INSN(recv, node, putself);
9447 flag |= VM_CALL_FCALL;
9450 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9453 if (
type == NODE_QCALL) {
9454 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9457 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9458 ADD_CALL_RECEIVER(recv, line_node);
9463 if (
type != NODE_VCALL) {
9464 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9465 CHECK(!
NIL_P(argc));
9473 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
9474 mid == rb_intern(
"new") &&
9475 parent_block == NULL &&
9476 !(flag & VM_CALL_ARGS_BLOCKARG);
9479 ADD_INSN(ret, node, putnil);
9480 ADD_INSN(ret, node, swap);
9485 debugp_param(
"call args argc", argc);
9486 debugp_param(
"call method",
ID2SYM(mid));
9488 switch ((
int)
type) {
9490 flag |= VM_CALL_VCALL;
9493 flag |= VM_CALL_FCALL;
9496 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9497 ADD_INSN(ret, line_node, splatkw);
9500 LABEL *not_basic_new = NEW_LABEL(nd_line(node));
9501 LABEL *not_basic_new_finish = NEW_LABEL(nd_line(node));
9506 if (flag & VM_CALL_FORWARDING) {
9507 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc) + 1, flag, keywords, 0);
9510 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc), flag, keywords, 0);
9512 ADD_INSN2(ret, node, opt_new, ci, not_basic_new);
9513 LABEL_REF(not_basic_new);
9516 ADD_SEND_R(ret, line_node, rb_intern(
"initialize"), argc, parent_block,
INT2FIX(flag | VM_CALL_FCALL), keywords);
9517 ADD_INSNL(ret, line_node, jump, not_basic_new_finish);
9519 ADD_LABEL(ret, not_basic_new);
9521 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9522 ADD_INSN(ret, line_node, swap);
9524 ADD_LABEL(ret, not_basic_new_finish);
9525 ADD_INSN(ret, line_node, pop);
9528 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9531 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9533 ADD_INSN(ret, line_node, pop);
9541 const int line = nd_line(node);
9543 unsigned int flag = 0;
9545 ID id = RNODE_OP_ASGN1(node)->nd_mid;
9571 ADD_INSN(ret, node, putnil);
9573 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
9574 CHECK(asgnflag != -1);
9575 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
9580 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
9581 CHECK(!
NIL_P(argc));
9583 int dup_argn =
FIX2INT(argc) + 1;
9584 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
9586 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
9588 if (
id == idOROP ||
id == idANDOP) {
9597 LABEL *label = NEW_LABEL(line);
9598 LABEL *lfin = NEW_LABEL(line);
9600 ADD_INSN(ret, node, dup);
9602 ADD_INSNL(ret, node, branchif, label);
9605 ADD_INSNL(ret, node, branchunless, label);
9607 ADD_INSN(ret, node, pop);
9609 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9611 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9613 if (flag & VM_CALL_ARGS_SPLAT) {
9614 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9615 ADD_INSN(ret, node, swap);
9616 ADD_INSN1(ret, node, splatarray,
Qtrue);
9617 ADD_INSN(ret, node, swap);
9618 flag |= VM_CALL_ARGS_SPLAT_MUT;
9620 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9621 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9624 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9626 ADD_INSN(ret, node, pop);
9627 ADD_INSNL(ret, node, jump, lfin);
9628 ADD_LABEL(ret, label);
9630 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9632 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
9633 ADD_LABEL(ret, lfin);
9636 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9637 ADD_SEND(ret, node,
id,
INT2FIX(1));
9639 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9641 if (flag & VM_CALL_ARGS_SPLAT) {
9642 if (flag & VM_CALL_KW_SPLAT) {
9643 ADD_INSN1(ret, node, topn,
INT2FIX(2));
9644 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9645 ADD_INSN1(ret, node, splatarray,
Qtrue);
9646 flag |= VM_CALL_ARGS_SPLAT_MUT;
9648 ADD_INSN(ret, node, swap);
9649 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9650 ADD_INSN1(ret, node, setn,
INT2FIX(2));
9651 ADD_INSN(ret, node, pop);
9654 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9655 ADD_INSN(ret, node, swap);
9656 ADD_INSN1(ret, node, splatarray,
Qtrue);
9657 ADD_INSN(ret, node, swap);
9658 flag |= VM_CALL_ARGS_SPLAT_MUT;
9660 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9662 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9665 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9667 ADD_INSN(ret, node, pop);
9675 const int line = nd_line(node);
9676 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
9677 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
9679 LABEL *lfin = NEW_LABEL(line);
9680 LABEL *lcfin = NEW_LABEL(line);
9735 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
9736 CHECK(asgnflag != -1);
9737 if (RNODE_OP_ASGN2(node)->nd_aid) {
9738 lskip = NEW_LABEL(line);
9739 ADD_INSN(ret, node, dup);
9740 ADD_INSNL(ret, node, branchnil, lskip);
9742 ADD_INSN(ret, node, dup);
9743 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
9745 if (atype == idOROP || atype == idANDOP) {
9747 ADD_INSN(ret, node, dup);
9749 if (atype == idOROP) {
9750 ADD_INSNL(ret, node, branchif, lcfin);
9753 ADD_INSNL(ret, node, branchunless, lcfin);
9756 ADD_INSN(ret, node, pop);
9758 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9760 ADD_INSN(ret, node, swap);
9761 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9763 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9764 ADD_INSNL(ret, node, jump, lfin);
9766 ADD_LABEL(ret, lcfin);
9768 ADD_INSN(ret, node, swap);
9771 ADD_LABEL(ret, lfin);
9774 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9775 ADD_SEND(ret, node, atype,
INT2FIX(1));
9777 ADD_INSN(ret, node, swap);
9778 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9780 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9782 if (lskip && popped) {
9783 ADD_LABEL(ret, lskip);
9785 ADD_INSN(ret, node, pop);
9786 if (lskip && !popped) {
9787 ADD_LABEL(ret, lskip);
9792static int compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
9797 const int line = nd_line(node);
9802 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
9804 ADD_INSN1(ret, node, putobject, rb_cObject);
9807 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
9810 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
9811 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
9814 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
9816 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
9817 lassign = NEW_LABEL(line);
9818 ADD_INSN(ret, node, dup);
9819 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
9821 ADD_INSNL(ret, node, branchunless, lassign);
9823 ADD_INSN(ret, node, dup);
9824 ADD_INSN1(ret, node, putobject,
Qtrue);
9825 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
9827 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
9828 lfin = NEW_LABEL(line);
9829 if (!popped) ADD_INSN(ret, node, dup);
9830 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
9831 ADD_INSNL(ret, node, branchif, lfin);
9833 ADD_INSNL(ret, node, branchunless, lfin);
9835 if (!popped) ADD_INSN(ret, node, pop);
9836 if (lassign) ADD_LABEL(ret, lassign);
9837 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9840 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9842 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
9843 ADD_INSN(ret, node, swap);
9845 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9846 ADD_LABEL(ret, lfin);
9847 if (!popped) ADD_INSN(ret, node, swap);
9848 ADD_INSN(ret, node, pop);
9851 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9853 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
9855 ADD_INSN(ret, node, swap);
9857 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9858 ADD_INSN(ret, node, swap);
9860 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9868 const int line = nd_line(node);
9869 LABEL *lfin = NEW_LABEL(line);
9872 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
9876 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
9877 lassign = lfinish[1];
9879 lassign = NEW_LABEL(line);
9881 ADD_INSNL(ret, node, branchunless, lassign);
9884 lassign = NEW_LABEL(line);
9887 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
9890 ADD_INSN(ret, node, dup);
9893 if (
type == NODE_OP_ASGN_AND) {
9894 ADD_INSNL(ret, node, branchunless, lfin);
9897 ADD_INSNL(ret, node, branchif, lfin);
9901 ADD_INSN(ret, node, pop);
9904 ADD_LABEL(ret, lassign);
9905 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
9906 ADD_LABEL(ret, lfin);
9916 unsigned int flag = 0;
9918 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9922 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9924 if (
type == NODE_SUPER) {
9925 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
9926 CHECK(!
NIL_P(vargc));
9928 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9929 ADD_INSN(args, node, splatkw);
9932 if (flag & VM_CALL_ARGS_BLOCKARG) {
9939 const rb_iseq_t *liseq = body->local_iseq;
9941 const struct rb_iseq_param_keyword *
const local_kwd = local_body->
param.keyword;
9942 int lvar_level = get_lvar_level(iseq);
9944 argc = local_body->
param.lead_num;
9947 for (i = 0; i < local_body->
param.lead_num; i++) {
9948 int idx = local_body->local_table_size - i;
9949 ADD_GETLOCAL(args, node, idx, lvar_level);
9953 if (local_body->
param.flags.forwardable) {
9954 flag |= VM_CALL_FORWARDING;
9955 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
9956 ADD_GETLOCAL(args, node, idx, lvar_level);
9959 if (local_body->
param.flags.has_opt) {
9962 for (j = 0; j < local_body->
param.opt_num; j++) {
9963 int idx = local_body->local_table_size - (i + j);
9964 ADD_GETLOCAL(args, node, idx, lvar_level);
9969 if (local_body->
param.flags.has_rest) {
9971 int idx = local_body->local_table_size - local_body->
param.rest_start;
9972 ADD_GETLOCAL(args, node, idx, lvar_level);
9973 ADD_INSN1(args, node, splatarray, RBOOL(local_body->
param.flags.has_post));
9975 argc = local_body->
param.rest_start + 1;
9976 flag |= VM_CALL_ARGS_SPLAT;
9978 if (local_body->
param.flags.has_post) {
9980 int post_len = local_body->
param.post_num;
9981 int post_start = local_body->
param.post_start;
9983 if (local_body->
param.flags.has_rest) {
9985 for (j=0; j<post_len; j++) {
9986 int idx = local_body->local_table_size - (post_start + j);
9987 ADD_GETLOCAL(args, node, idx, lvar_level);
9989 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
9990 flag |= VM_CALL_ARGS_SPLAT_MUT;
9995 for (j=0; j<post_len; j++) {
9996 int idx = local_body->local_table_size - (post_start + j);
9997 ADD_GETLOCAL(args, node, idx, lvar_level);
9999 argc = post_len + post_start;
10003 if (local_body->
param.flags.has_kw) {
10004 int local_size = local_body->local_table_size;
10007 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10009 if (local_body->
param.flags.has_kwrest) {
10010 int idx = local_body->local_table_size - local_kwd->rest_start;
10011 ADD_GETLOCAL(args, node, idx, lvar_level);
10013 ADD_SEND (args, node, rb_intern(
"dup"),
INT2FIX(0));
10016 ADD_INSN1(args, node, newhash,
INT2FIX(0));
10018 for (i = 0; i < local_kwd->num; ++i) {
10019 ID id = local_kwd->table[i];
10020 int idx = local_size - get_local_var_idx(liseq,
id);
10021 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
10022 ADD_GETLOCAL(args, node, idx, lvar_level);
10024 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
10025 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
10027 else if (local_body->
param.flags.has_kwrest) {
10028 int idx = local_body->local_table_size - local_kwd->rest_start;
10029 ADD_GETLOCAL(args, node, idx, lvar_level);
10031 flag |= VM_CALL_KW_SPLAT;
10035 if (use_block && parent_block == NULL) {
10036 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10039 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
10040 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
10041 ADD_INSN(ret, node, putself);
10042 ADD_SEQ(ret, args);
10044 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
10046 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
10047 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
10050 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
10054 ADD_INSN(ret, node, pop);
10064 unsigned int flag = 0;
10069 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
10070 case ISEQ_TYPE_TOP:
10071 case ISEQ_TYPE_MAIN:
10072 case ISEQ_TYPE_CLASS:
10073 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
10078 if (RNODE_YIELD(node)->nd_head) {
10079 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
10080 CHECK(!
NIL_P(argc));
10086 ADD_SEQ(ret, args);
10087 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
10088 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10091 ADD_INSN(ret, node, pop);
10096 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
10097 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
10099 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
10112 switch ((
int)
type) {
10114 ADD_INSN1(recv, node, putobject, rb_node_regx_string_val(node));
10115 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
10119 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
10120 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
10123 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
10124 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
10128 ADD_SEQ(ret, recv);
10130 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
10132 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
10133 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
10137 ADD_INSN(ret, node, pop);
10148 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10149 (segments = collect_const_segments(iseq, node))) {
10150 ISEQ_BODY(iseq)->ic_size++;
10151 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10161 CHECK(compile_const_prefix(iseq, node, pref, body));
10162 if (LIST_INSN_SIZE_ZERO(pref)) {
10163 ADD_INSN(ret, node, putnil);
10164 ADD_SEQ(ret, body);
10167 ADD_SEQ(ret, pref);
10168 ADD_SEQ(ret, body);
10174 ADD_CALL_RECEIVER(ret, node);
10175 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10176 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10179 ADD_INSN(ret, node, pop);
10187 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10190 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10191 ISEQ_BODY(iseq)->ic_size++;
10192 VALUE segments = rb_ary_new_from_args(2,
ID2SYM(idNULL),
ID2SYM(RNODE_COLON3(node)->nd_mid));
10193 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10197 ADD_INSN1(ret, node, putobject, rb_cObject);
10198 ADD_INSN1(ret, node, putobject,
Qtrue);
10199 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10203 ADD_INSN(ret, node, pop);
10212 const NODE *b = RNODE_DOT2(node)->nd_beg;
10213 const NODE *e = RNODE_DOT2(node)->nd_end;
10215 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10217 VALUE bv = optimized_range_item(b);
10218 VALUE ev = optimized_range_item(e);
10220 ADD_INSN1(ret, node, putobject, val);
10225 CHECK(COMPILE_(ret,
"min", b, popped));
10226 CHECK(COMPILE_(ret,
"max", e, popped));
10228 ADD_INSN1(ret, node, newrange, flag);
10238 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10239 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10245 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10248 ip = ISEQ_BODY(ip)->parent_iseq;
10252 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10255 ADD_INSN(ret, node, putnil);
10266 LABEL *end_label = NEW_LABEL(nd_line(node));
10267 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10269 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10271 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10274 else if (nd_type_p(default_value, NODE_SYM) ||
10275 nd_type_p(default_value, NODE_REGX) ||
10276 nd_type_p(default_value, NODE_LINE) ||
10277 nd_type_p(default_value, NODE_INTEGER) ||
10278 nd_type_p(default_value, NODE_FLOAT) ||
10279 nd_type_p(default_value, NODE_RATIONAL) ||
10280 nd_type_p(default_value, NODE_IMAGINARY) ||
10281 nd_type_p(default_value, NODE_NIL) ||
10282 nd_type_p(default_value, NODE_TRUE) ||
10283 nd_type_p(default_value, NODE_FALSE)) {
10284 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10292 int kw_bits_idx = body->local_table_size - body->
param.keyword->bits_start;
10293 int keyword_idx = body->
param.keyword->num;
10295 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10296 ADD_INSNL(ret, node, branchif, end_label);
10297 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10298 ADD_LABEL(ret, end_label);
10308 unsigned int flag = 0;
10309 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10311 LABEL *else_label = NULL;
10317 if (!ISEQ_COMPILE_DATA(iseq)->in_masgn &&
10318 mid == idASET && !private_recv_p(node) && RNODE_ATTRASGN(node)->nd_args &&
10319 nd_type_p(RNODE_ATTRASGN(node)->nd_args, NODE_LIST) && RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->as.nd_alen == 2 &&
10320 (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)) &&
10321 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
10322 !frozen_string_literal_p(iseq) &&
10323 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction)
10325 VALUE str = get_string_value(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_head);
10326 CHECK(COMPILE(ret,
"recv", RNODE_ATTRASGN(node)->nd_recv));
10327 CHECK(COMPILE(ret,
"value", RNODE_LIST(RNODE_LIST(RNODE_ATTRASGN(node)->nd_args)->nd_next)->nd_head));
10329 ADD_INSN(ret, node, swap);
10330 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10332 ADD_INSN2(ret, node, opt_aset_with, str,
10333 new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
10335 ADD_INSN(ret, node, pop);
10341 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10342 CHECK(!
NIL_P(argc));
10344 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10345 CHECK(asgnflag != -1);
10346 flag |= (
unsigned int)asgnflag;
10348 debugp_param(
"argc", argc);
10349 debugp_param(
"nd_mid",
ID2SYM(mid));
10353 mid = rb_id_attrset(mid);
10354 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10357 ADD_INSN(ret, node, putnil);
10358 ADD_SEQ(ret, recv);
10359 ADD_SEQ(ret, args);
10361 if (flag & VM_CALL_ARGS_SPLAT) {
10362 ADD_INSN(ret, node, dup);
10363 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10364 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10365 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10366 ADD_INSN (ret, node, pop);
10369 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10373 ADD_SEQ(ret, recv);
10374 ADD_SEQ(ret, args);
10376 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10377 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10378 ADD_INSN(ret, node, pop);
10385 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10393 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10400 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10407node_const_decl_val(
const NODE *node)
10410 switch (nd_type(node)) {
10412 if (RNODE_CDECL(node)->nd_vid) {
10413 path = rb_id2str(RNODE_CDECL(node)->nd_vid);
10417 node = RNODE_CDECL(node)->nd_else;
10425 rb_str_append(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10428 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10434 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10435 rb_ary_push(path, rb_id2str(RNODE_COLON2(node)->nd_mid));
10437 if (node && nd_type_p(node, NODE_CONST)) {
10439 rb_ary_push(path, rb_id2str(RNODE_CONST(node)->nd_vid));
10441 else if (node && nd_type_p(node, NODE_COLON3)) {
10443 rb_ary_push(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10453 path = rb_fstring(path);
10458const_decl_path(
NODE *dest)
10461 if (!nd_type_p(dest, NODE_CALL)) {
10462 path = node_const_decl_val(dest);
10473 VALUE path = const_decl_path(dest);
10474 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10475 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10476 ADD_INSN1(ret, value, putobject, path);
10478 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10483#ifndef SHAREABLE_BARE_EXPRESSION
10484#define SHAREABLE_BARE_EXPRESSION 1
10488compile_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)
10490# define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10491 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10493 DECL_ANCHOR(anchor);
10495 enum node_type
type = node ? nd_type(node) : NODE_NIL;
10507 *value_p = rb_node_sym_string_val(node);
10510 *value_p = rb_node_regx_string_val(node);
10513 *value_p = rb_node_line_lineno_val(node);
10516 *value_p = rb_node_integer_literal_val(node);
10519 *value_p = rb_node_float_literal_val(node);
10521 case NODE_RATIONAL:
10522 *value_p = rb_node_rational_literal_val(node);
10524 case NODE_IMAGINARY:
10525 *value_p = rb_node_imaginary_literal_val(node);
10527 case NODE_ENCODING:
10528 *value_p = rb_node_encoding_val(node);
10531 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10532 *shareable_literal_p = 1;
10536 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10537 if (shareable == rb_parser_shareable_literal) {
10543 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10546 *shareable_literal_p = 1;
10550 VALUE lit = rb_node_str_string_val(node);
10551 ADD_INSN1(ret, node, putobject, lit);
10554 *shareable_literal_p = 1;
10560 VALUE lit = rb_node_file_path_val(node);
10561 ADD_INSN1(ret, node, putobject, lit);
10564 *shareable_literal_p = 1;
10572 ADD_INSN1(ret, node, putobject, lit);
10575 *shareable_literal_p = 1;
10581 INIT_ANCHOR(anchor);
10583 for (
NODE *n = (
NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
10585 int shareable_literal_p2;
10586 NODE *elt = RNODE_LIST(n)->nd_head;
10588 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
10589 if (shareable_literal_p2) {
10592 else if (
RTEST(lit)) {
10598 if (!UNDEF_P(val)) {
10610 if (!RNODE_HASH(node)->nd_brace) {
10612 *shareable_literal_p = 0;
10615 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10616 if (!RNODE_LIST(n)->nd_head) {
10618 goto compile_shareable;
10622 INIT_ANCHOR(anchor);
10623 lit = rb_hash_new();
10624 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10626 VALUE value_val = 0;
10627 int shareable_literal_p2;
10628 NODE *key = RNODE_LIST(n)->nd_head;
10629 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
10630 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10631 if (shareable_literal_p2) {
10634 else if (
RTEST(lit)) {
10635 rb_hash_clear(lit);
10638 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10639 if (shareable_literal_p2) {
10642 else if (
RTEST(lit)) {
10643 rb_hash_clear(lit);
10647 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
10648 rb_hash_aset(lit, key_val, value_val);
10651 rb_hash_clear(lit);
10662 if (shareable == rb_parser_shareable_literal &&
10663 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
10664 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
10666 *shareable_literal_p = 1;
10669 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10671 *shareable_literal_p = 0;
10677 if (nd_type(node) == NODE_LIST) {
10678 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10680 else if (nd_type(node) == NODE_HASH) {
10681 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10682 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10685 *shareable_literal_p = 0;
10686 ADD_SEQ(ret, anchor);
10692 if (nd_type(node) == NODE_LIST) {
10693 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10695 else if (nd_type(node) == NODE_HASH) {
10696 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10697 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10699 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
10701 *shareable_literal_p = 1;
10705 ADD_INSN1(ret, node, putobject, val);
10708 *shareable_literal_p = 1;
10715compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
10719 DECL_ANCHOR(anchor);
10720 INIT_ANCHOR(anchor);
10722 switch (shareable) {
10723 case rb_parser_shareable_none:
10724 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
10727 case rb_parser_shareable_literal:
10728 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10729 ADD_SEQ(ret, anchor);
10732 case rb_parser_shareable_copy:
10733 case rb_parser_shareable_everything:
10734 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10736 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
10739 ADD_SEQ(ret, anchor);
10743 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
10760 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
10761 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
10762 debugs(
"node: NODE_NIL(implicit)\n");
10763 ADD_SYNTHETIC_INSN(ret, lineno, -1, putnil);
10767 return iseq_compile_each0(iseq, ret, node, popped);
10773 const int line = (int)nd_line(node);
10774 const enum node_type
type = nd_type(node);
10777 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
10781 if (nd_fl_newline(node)) {
10783 ISEQ_COMPILE_DATA(iseq)->last_line = line;
10784 if (line > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
10785 event |= RUBY_EVENT_COVERAGE_LINE;
10787 ADD_TRACE(ret, event);
10791 debug_node_start(node);
10792#undef BEFORE_RETURN
10793#define BEFORE_RETURN debug_node_end()
10797 CHECK(compile_block(iseq, ret, node, popped));
10801 CHECK(compile_if(iseq, ret, node, popped,
type));
10804 CHECK(compile_case(iseq, ret, node, popped));
10807 CHECK(compile_case2(iseq, ret, node, popped));
10810 CHECK(compile_case3(iseq, ret, node, popped));
10814 CHECK(compile_loop(iseq, ret, node, popped,
type));
10818 CHECK(compile_iter(iseq, ret, node, popped));
10820 case NODE_FOR_MASGN:
10821 CHECK(compile_for_masgn(iseq, ret, node, popped));
10824 CHECK(compile_break(iseq, ret, node, popped));
10827 CHECK(compile_next(iseq, ret, node, popped));
10830 CHECK(compile_redo(iseq, ret, node, popped));
10833 CHECK(compile_retry(iseq, ret, node, popped));
10836 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
10840 CHECK(compile_rescue(iseq, ret, node, popped));
10843 CHECK(compile_resbody(iseq, ret, node, popped));
10846 CHECK(compile_ensure(iseq, ret, node, popped));
10851 LABEL *end_label = NEW_LABEL(line);
10852 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(node)->nd_1st));
10854 ADD_INSN(ret, node, dup);
10856 if (
type == NODE_AND) {
10857 ADD_INSNL(ret, node, branchunless, end_label);
10860 ADD_INSNL(ret, node, branchif, end_label);
10863 ADD_INSN(ret, node, pop);
10865 CHECK(COMPILE_(ret,
"nd_2nd", RNODE_OR(node)->nd_2nd, popped));
10866 ADD_LABEL(ret, end_label);
10871 bool prev_in_masgn = ISEQ_COMPILE_DATA(iseq)->in_masgn;
10872 ISEQ_COMPILE_DATA(iseq)->in_masgn =
true;
10873 compile_massign(iseq, ret, node, popped);
10874 ISEQ_COMPILE_DATA(iseq)->in_masgn = prev_in_masgn;
10879 ID id = RNODE_LASGN(node)->nd_vid;
10880 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
10882 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
10883 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
10886 ADD_INSN(ret, node, dup);
10888 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
10893 ID id = RNODE_DASGN(node)->nd_vid;
10894 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
10895 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
10898 ADD_INSN(ret, node, dup);
10901 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
10904 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
10908 ADD_SETLOCAL(ret, node, ls - idx, lv);
10912 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
10915 ADD_INSN(ret, node, dup);
10917 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
10921 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
10923 ADD_INSN(ret, node, dup);
10925 ADD_INSN2(ret, node, setinstancevariable,
10926 ID2SYM(RNODE_IASGN(node)->nd_vid),
10927 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
10931 if (RNODE_CDECL(node)->nd_vid) {
10932 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
10935 ADD_INSN(ret, node, dup);
10938 ADD_INSN1(ret, node, putspecialobject,
10939 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
10940 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
10943 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
10944 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
10945 ADD_INSN(ret, node, swap);
10948 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10949 ADD_INSN(ret, node, swap);
10952 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
10957 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
10959 ADD_INSN(ret, node, dup);
10961 ADD_INSN2(ret, node, setclassvariable,
10962 ID2SYM(RNODE_CVASGN(node)->nd_vid),
10963 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
10966 case NODE_OP_ASGN1:
10967 CHECK(compile_op_asgn1(iseq, ret, node, popped));
10969 case NODE_OP_ASGN2:
10970 CHECK(compile_op_asgn2(iseq, ret, node, popped));
10972 case NODE_OP_CDECL:
10973 CHECK(compile_op_cdecl(iseq, ret, node, popped));
10975 case NODE_OP_ASGN_AND:
10976 case NODE_OP_ASGN_OR:
10977 CHECK(compile_op_log(iseq, ret, node, popped,
type));
10981 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
10987 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
10993 CHECK(compile_super(iseq, ret, node, popped,
type));
10996 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
11001 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
11006 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
11009 CHECK(compile_return(iseq, ret, node, popped));
11012 CHECK(compile_yield(iseq, ret, node, popped));
11016 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
11022 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
11024 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
11026 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
11027 rb_id2str(RNODE_DVAR(node)->nd_vid));
11030 ADD_GETLOCAL(ret, node, ls - idx, lv);
11035 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
11037 ADD_INSN(ret, node, pop);
11042 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
11044 ADD_INSN2(ret, node, getinstancevariable,
11045 ID2SYM(RNODE_IVAR(node)->nd_vid),
11046 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
11051 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
11053 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
11055 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(RNODE_CONST(node)->nd_vid));
11056 ADD_INSN1(ret, node, opt_getconstant_path, segments);
11060 ADD_INSN(ret, node, putnil);
11061 ADD_INSN1(ret, node, putobject,
Qtrue);
11062 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
11066 ADD_INSN(ret, node, pop);
11072 ADD_INSN2(ret, node, getclassvariable,
11073 ID2SYM(RNODE_CVAR(node)->nd_vid),
11074 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
11078 case NODE_NTH_REF:{
11080 if (!RNODE_NTH_REF(node)->nd_nth) {
11081 ADD_INSN(ret, node, putnil);
11084 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11085 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
11089 case NODE_BACK_REF:{
11091 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11092 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
11099 CHECK(compile_match(iseq, ret, node, popped,
type));
11103 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
11109 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
11113 case NODE_ENCODING:{
11115 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
11119 case NODE_INTEGER:{
11120 VALUE lit = rb_node_integer_literal_val(node);
11121 debugp_param(
"integer", lit);
11123 ADD_INSN1(ret, node, putobject, lit);
11129 VALUE lit = rb_node_float_literal_val(node);
11130 debugp_param(
"float", lit);
11132 ADD_INSN1(ret, node, putobject, lit);
11137 case NODE_RATIONAL:{
11138 VALUE lit = rb_node_rational_literal_val(node);
11139 debugp_param(
"rational", lit);
11141 ADD_INSN1(ret, node, putobject, lit);
11146 case NODE_IMAGINARY:{
11147 VALUE lit = rb_node_imaginary_literal_val(node);
11148 debugp_param(
"imaginary", lit);
11150 ADD_INSN1(ret, node, putobject, lit);
11157 debugp_param(
"nd_lit", get_string_value(node));
11159 VALUE lit = get_string_value(node);
11162 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11163 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11165 switch (option->frozen_string_literal) {
11166 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11167 ADD_INSN1(ret, node, putchilledstring, lit);
11169 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11170 ADD_INSN1(ret, node, putstring, lit);
11172 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11173 ADD_INSN1(ret, node, putobject, lit);
11176 rb_bug(
"invalid frozen_string_literal");
11183 compile_dstr(iseq, ret, node);
11186 ADD_INSN(ret, node, pop);
11191 ADD_CALL_RECEIVER(ret, node);
11192 VALUE str = rb_node_str_string_val(node);
11193 ADD_INSN1(ret, node, putobject, str);
11195 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11198 ADD_INSN(ret, node, pop);
11203 ADD_CALL_RECEIVER(ret, node);
11204 compile_dstr(iseq, ret, node);
11205 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11208 ADD_INSN(ret, node, pop);
11213 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11217 VALUE lit = rb_node_regx_string_val(node);
11218 ADD_INSN1(ret, node, putobject, lit);
11224 compile_dregx(iseq, ret, node, popped);
11227 int ic_index = body->ise_size++;
11229 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11231 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
11235 ADD_INSN(ret, node, pop);
11239 case NODE_ARGSCAT:{
11241 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11242 ADD_INSN1(ret, node, splatarray,
Qfalse);
11243 ADD_INSN(ret, node, pop);
11244 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11245 ADD_INSN1(ret, node, splatarray,
Qfalse);
11246 ADD_INSN(ret, node, pop);
11249 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11250 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11251 if (nd_type_p(body_node, NODE_LIST)) {
11252 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11255 CHECK(COMPILE(ret,
"argscat body", body_node));
11256 ADD_INSN(ret, node, concattoarray);
11261 case NODE_ARGSPUSH:{
11263 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11264 ADD_INSN1(ret, node, splatarray,
Qfalse);
11265 ADD_INSN(ret, node, pop);
11266 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11269 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11270 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11271 if (keyword_node_p(body_node)) {
11272 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11273 ADD_INSN(ret, node, pushtoarraykwsplat);
11275 else if (static_literal_node_p(body_node, iseq,
false)) {
11276 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11277 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11280 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11281 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11287 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11288 ADD_INSN1(ret, node, splatarray,
Qtrue);
11291 ADD_INSN(ret, node, pop);
11296 ID mid = RNODE_DEFN(node)->nd_mid;
11297 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11299 ISEQ_TYPE_METHOD, line);
11301 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11302 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11306 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11312 ID mid = RNODE_DEFS(node)->nd_mid;
11313 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11315 ISEQ_TYPE_METHOD, line);
11317 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11318 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11319 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11323 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11328 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11329 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11330 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11331 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11332 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11335 ADD_INSN(ret, node, pop);
11340 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11341 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11342 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11343 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11346 ADD_INSN(ret, node, pop);
11353 for (
long i = 0; i < ary->len; i++) {
11354 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11355 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11356 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11357 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11359 if (i < ary->
len - 1) {
11360 ADD_INSN(ret, node, pop);
11365 ADD_INSN(ret, node, pop);
11370 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11371 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
11372 ISEQ_TYPE_CLASS, line);
11373 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11374 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11375 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11377 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11378 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11382 ADD_INSN(ret, node, pop);
11387 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11388 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
11389 ISEQ_TYPE_CLASS, line);
11390 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11391 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11393 ADD_INSN (ret, node, putnil);
11394 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11398 ADD_INSN(ret, node, pop);
11404 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11405 ISEQ_TYPE_CLASS, line);
11407 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11408 ADD_INSN (ret, node, putnil);
11409 CONST_ID(singletonclass,
"singletonclass");
11410 ADD_INSN3(ret, node, defineclass,
11411 ID2SYM(singletonclass), singleton_class,
11412 INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
11416 ADD_INSN(ret, node, pop);
11421 CHECK(compile_colon2(iseq, ret, node, popped));
11424 CHECK(compile_colon3(iseq, ret, node, popped));
11427 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11430 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11434 LABEL *lend = NEW_LABEL(line);
11435 LABEL *ltrue = NEW_LABEL(line);
11436 LABEL *lfalse = NEW_LABEL(line);
11437 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11439 ADD_LABEL(ret, ltrue);
11440 ADD_INSN1(ret, node, putobject,
Qtrue);
11441 ADD_INSNL(ret, node, jump, lend);
11442 ADD_LABEL(ret, lfalse);
11443 ADD_INSN1(ret, node, putobject,
Qfalse);
11444 ADD_LABEL(ret, lend);
11449 ADD_INSN(ret, node, putself);
11455 ADD_INSN(ret, node, putnil);
11461 ADD_INSN1(ret, node, putobject,
Qtrue);
11467 ADD_INSN1(ret, node, putobject,
Qfalse);
11472 CHECK(compile_errinfo(iseq, ret, node, popped));
11476 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11479 case NODE_POSTEXE:{
11483 int is_index = body->ise_size++;
11485 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11487 NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc, rb_fstring(make_name_for_block(iseq)), ISEQ_TYPE_BLOCK, line);
11489 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
11493 ADD_INSN(ret, node, pop);
11498 CHECK(compile_kw_arg(iseq, ret, node, popped));
11501 compile_dstr(iseq, ret, node);
11503 ADD_INSN(ret, node, intern);
11506 ADD_INSN(ret, node, pop);
11510 case NODE_ATTRASGN:
11511 CHECK(compile_attrasgn(iseq, ret, node, popped));
11515 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11518 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11519 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11523 ADD_INSN(ret, node, pop);
11528 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
11543insn_data_length(
INSN *iobj)
11545 return insn_len(iobj->insn_id);
11549calc_sp_depth(
int depth,
INSN *insn)
11551 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
11555opobj_inspect(
VALUE obj)
11575insn_data_to_s_detail(
INSN *iobj)
11577 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
11579 if (iobj->operands) {
11580 const char *types = insn_op_types(iobj->insn_id);
11583 for (j = 0; types[j]; j++) {
11584 char type = types[j];
11590 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
11608 VALUE v = OPERAND_AT(iobj, j);
11623 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11626 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11629 rb_str_catf(str,
"<ise:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11635 if (vm_ci_mid(ci)) rb_str_catf(str,
"%"PRIsVALUE, rb_id2str(vm_ci_mid(ci)));
11636 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
11644 void *func = (
void *)OPERAND_AT(iobj, j);
11647 if (dladdr(func, &info) && info.dli_sname) {
11652 rb_str_catf(str,
"<%p>", func);
11662 if (types[j + 1]) {
11673 dump_disasm_list_with_cursor(link, NULL, NULL);
11684 printf(
"-- raw disasm--------\n");
11687 if (curr) printf(curr == link ?
"*" :
" ");
11688 switch (link->type) {
11689 case ISEQ_ELEMENT_INSN:
11691 iobj = (
INSN *)link;
11692 str = insn_data_to_s_detail(iobj);
11693 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
11694 pos += insn_data_length(iobj);
11697 case ISEQ_ELEMENT_LABEL:
11699 lobj = (
LABEL *)link;
11700 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
11701 dest == lobj ?
" <---" :
"");
11704 case ISEQ_ELEMENT_TRACE:
11707 printf(
" trace: %0x\n", trace->event);
11710 case ISEQ_ELEMENT_ADJUST:
11713 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
11718 rb_raise(
rb_eSyntaxError,
"dump_disasm_list error: %d\n", (
int)link->type);
11722 printf(
"---------------------\n");
11727rb_insn_len(
VALUE insn)
11729 return insn_len(insn);
11733rb_insns_name(
int i)
11735 return insn_name(i);
11739rb_insns_name_array(
void)
11743 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
11754 obj = rb_to_symbol_type(obj);
11756 if (st_lookup(labels_table, obj, &tmp) == 0) {
11757 label = NEW_LABEL(0);
11758 st_insert(labels_table, obj, (st_data_t)label);
11761 label = (
LABEL *)tmp;
11768get_exception_sym2type(
VALUE sym)
11770 static VALUE symRescue, symEnsure, symRetry;
11771 static VALUE symBreak, symRedo, symNext;
11773 if (symRescue == 0) {
11782 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
11783 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
11784 if (sym == symRetry)
return CATCH_TYPE_RETRY;
11785 if (sym == symBreak)
return CATCH_TYPE_BREAK;
11786 if (sym == symRedo)
return CATCH_TYPE_REDO;
11787 if (sym == symNext)
return CATCH_TYPE_NEXT;
11788 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
11801 LABEL *lstart, *lend, *lcont;
11816 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
11817 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
11818 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
11822 if (
type == CATCH_TYPE_RESCUE ||
11823 type == CATCH_TYPE_BREAK ||
11824 type == CATCH_TYPE_NEXT) {
11830 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
11838insn_make_insn_table(
void)
11842 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
11844 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
11858 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
11860 else if (
CLASS_OF(op) == rb_cISeq) {
11867 loaded_iseq = rb_iseqw_to_iseq(iseqw);
11868 return loaded_iseq;
11876 unsigned int flag = 0;
11887 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
11889 if (!
NIL_P(vkw_arg)) {
11892 size_t n = rb_callinfo_kwarg_bytes(
len);
11895 kw_arg->references = 0;
11896 kw_arg->keyword_len =
len;
11897 for (i = 0; i <
len; i++) {
11900 kw_arg->keywords[i] = kw;
11905 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
11911event_name_to_flag(
VALUE sym)
11913#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
11934 int line_no = 0, node_id = -1, insn_idx = 0;
11935 int ret = COMPILE_OK;
11940 static struct st_table *insn_table;
11942 if (insn_table == 0) {
11943 insn_table = insn_make_insn_table();
11946 for (i=0; i<
len; i++) {
11952 ADD_TRACE(anchor, event);
11955 LABEL *label = register_label(iseq, labels_table, obj);
11956 ADD_LABEL(anchor, label);
11973 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
11975 COMPILE_ERROR(iseq, line_no,
11976 "unknown instruction: %+"PRIsVALUE, insn);
11981 if (argc != insn_len((
VALUE)insn_id)-1) {
11982 COMPILE_ERROR(iseq, line_no,
11983 "operand size mismatch");
11989 argv = compile_data_calloc2(iseq,
sizeof(
VALUE), argc);
11994 (
enum ruby_vminsn_type)insn_id, argc, argv));
11996 for (j=0; j<argc; j++) {
11998 switch (insn_op_type((
VALUE)insn_id, j)) {
12000 LABEL *label = register_label(iseq, labels_table, op);
12001 argv[j] = (
VALUE)label;
12016 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
12027 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
12028 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
12034 op = rb_to_array_type(op);
12038 sym = rb_to_symbol_type(sym);
12043 argv[j] = segments;
12045 ISEQ_BODY(iseq)->ic_size++;
12050 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
12051 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
12056 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
12057 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
12061 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
12064 argv[j] = rb_to_symbol_type(op);
12071 RHASH_TBL_RAW(map)->type = &cdhash_type;
12072 op = rb_to_array_type(op);
12077 register_label(iseq, labels_table, sym);
12078 rb_hash_aset(map, key, (
VALUE)label | 1);
12087#if SIZEOF_VALUE <= SIZEOF_LONG
12092 argv[j] = (
VALUE)funcptr;
12103 (
enum ruby_vminsn_type)insn_id, argc, NULL));
12107 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
12112 validate_labels(iseq, labels_table);
12113 if (!ret)
return ret;
12114 return iseq_setup(iseq, anchor);
12117#define CHECK_ARRAY(v) rb_to_array_type(v)
12118#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
12123 VALUE val = rb_hash_aref(param, sym);
12128 else if (!
NIL_P(val)) {
12129 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
12135static const struct rb_iseq_param_keyword *
12141 VALUE key, sym, default_val;
12144 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12146 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12148 keyword->num =
len;
12149#define SYM(s) ID2SYM(rb_intern_const(#s))
12150 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12151 i = keyword->bits_start - keyword->num;
12152 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12156 for (i = 0; i <
len; i++) {
12160 goto default_values;
12163 keyword->required_num++;
12167 default_len =
len - i;
12168 if (default_len == 0) {
12169 keyword->table = ids;
12172 else if (default_len < 0) {
12178 for (j = 0; i <
len; i++, j++) {
12192 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
12198 keyword->table = ids;
12199 keyword->default_values = dvs;
12205iseq_insn_each_object_mark_and_move(
VALUE * obj,
VALUE _)
12207 rb_gc_mark_and_move(obj);
12214 size_t size =
sizeof(
INSN);
12215 unsigned int pos = 0;
12218#ifdef STRICT_ALIGNMENT
12219 size_t padding = calc_padding((
void *)&storage->buff[pos], size);
12221 const size_t padding = 0;
12223 size_t offset = pos + size + padding;
12224 if (offset > storage->size || offset > storage->pos) {
12226 storage = storage->next;
12229#ifdef STRICT_ALIGNMENT
12230 pos += (int)padding;
12233 iobj = (
INSN *)&storage->buff[pos];
12235 if (iobj->operands) {
12236 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_move, (
VALUE)0);
12249 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
12256#define SYM(s) ID2SYM(rb_intern_const(#s))
12258 unsigned int arg_size, local_size, stack_max;
12260 struct st_table *labels_table = st_init_numtable();
12262 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
12263 VALUE keywords = rb_hash_aref(params, SYM(keyword));
12265 DECL_ANCHOR(anchor);
12266 INIT_ANCHOR(anchor);
12269 ISEQ_BODY(iseq)->local_table_size =
len;
12270 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12272 for (i = 0; i <
len; i++) {
12275 if (sym_arg_rest == lv) {
12283#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12284 if (INT_PARAM(lead_num)) {
12285 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12287 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12288 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12289 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12290 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12293#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12295 INT_PARAM(arg_size);
12296 INT_PARAM(local_size);
12297 INT_PARAM(stack_max);
12302#ifdef USE_ISEQ_NODE_ID
12303 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
12311 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12313 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12316 for (i = 0; i <
len; i++) {
12318 LABEL *label = register_label(iseq, labels_table, ent);
12319 opt_table[i] = (
VALUE)label;
12322 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12323 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12326 else if (!
NIL_P(arg_opt_labels)) {
12327 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
12332 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12334 else if (!
NIL_P(keywords)) {
12335 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
12339 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
12340 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12343 if (
Qtrue == rb_hash_aref(params, SYM(use_block))) {
12344 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12347 if (int_param(&i, params, SYM(kwrest))) {
12348 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12349 if (keyword == NULL) {
12350 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12352 keyword->rest_start = i;
12353 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12356 iseq_calc_param_size(iseq);
12359 iseq_build_from_ary_exception(iseq, labels_table, exception);
12362 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12364 ISEQ_BODY(iseq)->param.size = arg_size;
12365 ISEQ_BODY(iseq)->local_table_size = local_size;
12366 ISEQ_BODY(iseq)->stack_max = stack_max;
12376 while (body->type == ISEQ_TYPE_BLOCK ||
12377 body->type == ISEQ_TYPE_RESCUE ||
12378 body->type == ISEQ_TYPE_ENSURE ||
12379 body->type == ISEQ_TYPE_EVAL ||
12380 body->type == ISEQ_TYPE_MAIN
12384 for (i = 0; i < body->local_table_size; i++) {
12385 if (body->local_table[i] ==
id) {
12389 iseq = body->parent_iseq;
12390 body = ISEQ_BODY(iseq);
12403 for (i=0; i<body->local_table_size; i++) {
12404 if (body->local_table[i] ==
id) {
12414#ifndef IBF_ISEQ_DEBUG
12415#define IBF_ISEQ_DEBUG 0
12418#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12419#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12422typedef uint32_t ibf_offset_t;
12423#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12425#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12427#define IBF_DEVEL_VERSION 4
12428#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12430#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12433static const char IBF_ENDIAN_MARK =
12434#ifdef WORDS_BIGENDIAN
12443 uint32_t major_version;
12444 uint32_t minor_version;
12446 uint32_t extra_size;
12448 uint32_t iseq_list_size;
12449 uint32_t global_object_list_size;
12450 ibf_offset_t iseq_list_offset;
12451 ibf_offset_t global_object_list_offset;
12472 unsigned int obj_list_size;
12473 ibf_offset_t obj_list_offset;
12492pinned_list_mark(
void *ptr)
12496 for (i = 0; i < list->size; i++) {
12497 if (list->buffer[i]) {
12498 rb_gc_mark(list->buffer[i]);
12510 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
12514pinned_list_fetch(
VALUE list,
long offset)
12520 if (offset >= ptr->size) {
12521 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12524 return ptr->buffer[offset];
12528pinned_list_store(
VALUE list,
long offset,
VALUE object)
12534 if (offset >= ptr->size) {
12535 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12542pinned_list_new(
long size)
12544 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
12545 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
12546 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
12552ibf_dump_pos(
struct ibf_dump *dump)
12554 long pos = RSTRING_LEN(dump->current_buffer->str);
12555#if SIZEOF_LONG > SIZEOF_INT
12556 if (pos >= UINT_MAX) {
12560 return (
unsigned int)pos;
12564ibf_dump_align(
struct ibf_dump *dump,
size_t align)
12566 ibf_offset_t pos = ibf_dump_pos(dump);
12568 static const char padding[
sizeof(
VALUE)];
12569 size_t size = align - ((size_t)pos % align);
12570#if SIZEOF_LONG > SIZEOF_INT
12571 if (pos + size >= UINT_MAX) {
12575 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
12576 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
12578 rb_str_cat(dump->current_buffer->str, padding, size);
12583ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
12585 ibf_offset_t pos = ibf_dump_pos(dump);
12586#if SIZEOF_LONG > SIZEOF_INT
12588 if (size >= UINT_MAX || pos + size >= UINT_MAX) {
12592 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
12597ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
12599 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
12603ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
12605 VALUE str = dump->current_buffer->str;
12606 char *ptr = RSTRING_PTR(str);
12607 if ((
unsigned long)(size + offset) > (
unsigned long)RSTRING_LEN(str))
12608 rb_bug(
"ibf_dump_overwrite: overflow");
12609 memcpy(ptr + offset, buff, size);
12613ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
12615 ibf_offset_t beg = *offset;
12617 return load->current_buffer->buff + beg;
12621ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
12623 void *buff = ruby_xmalloc2(x, y);
12624 size_t size = x * y;
12625 memcpy(buff, load->current_buffer->buff + offset, size);
12629#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
12631#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
12632#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
12633#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
12634#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
12635#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
12638ibf_table_lookup(
struct st_table *table, st_data_t key)
12642 if (st_lookup(table, key, &val)) {
12651ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
12653 int index = ibf_table_lookup(table, key);
12656 index = (int)table->num_entries;
12657 st_insert(table, key, (st_data_t)index);
12665static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
12671ibf_dump_object_table_new(
void)
12673 st_table *obj_table = st_init_numtable();
12674 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
12682 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
12688 if (
id == 0 || rb_id2name(
id) == NULL) {
12691 return ibf_dump_object(dump,
rb_id2sym(
id));
12695ibf_load_id(
const struct ibf_load *load,
const ID id_index)
12697 if (id_index == 0) {
12700 VALUE sym = ibf_load_object(load, id_index);
12710static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
12715 if (iseq == NULL) {
12719 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
12723static unsigned char
12724ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
12726 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
12727 return (
unsigned char)load->current_buffer->buff[(*offset)++];
12743 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12744 ibf_dump_write(dump, &x,
sizeof(
VALUE));
12748 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12750 unsigned char bytes[max_byte_length];
12753 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
12754 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12760 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12763 ibf_dump_write(dump, bytes + max_byte_length - n, n);
12767ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
12769 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12770 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
12772 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
12773 *offset +=
sizeof(
VALUE);
12778 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12780 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
12781 const unsigned char c = buffer[*offset];
12785 c == 0 ? 9 : ntz_int32(c) + 1;
12788 if (*offset + n > load->current_buffer->size) {
12793 for (i = 1; i < n; i++) {
12795 x |= (
VALUE)buffer[*offset + i];
12809 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
12811 size_t len = strlen(bf->name);
12812 ibf_dump_write_small_value(dump, (
VALUE)
len);
12813 ibf_dump_write(dump, bf->name,
len);
12817ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
12819 int i = (int)ibf_load_small_value(load, offset);
12820 int len = (int)ibf_load_small_value(load, offset);
12821 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
12824 fprintf(stderr,
"%.*s!!\n",
len, name);
12828 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
12829 if (strncmp(table[i].name, name,
len) != 0) {
12830 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
12841 const int iseq_size = body->iseq_size;
12843 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
12845 ibf_offset_t offset = ibf_dump_pos(dump);
12847 for (code_index=0; code_index<iseq_size;) {
12848 const VALUE insn = orig_code[code_index++];
12849 const char *types = insn_op_types(insn);
12854 ibf_dump_write_small_value(dump, insn);
12857 for (op_index=0; types[op_index]; op_index++, code_index++) {
12858 VALUE op = orig_code[code_index];
12861 switch (types[op_index]) {
12864 wv = ibf_dump_object(dump, op);
12873 wv = ibf_dump_object(dump, arr);
12881 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
12889 wv = ibf_dump_id(dump, (
ID)op);
12901 ibf_dump_write_small_value(dump, wv);
12911ibf_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)
12914 unsigned int code_index;
12915 ibf_offset_t reading_pos = bytecode_offset;
12919 struct rb_call_data *cd_entries = load_body->call_data;
12922 iseq_bits_t * mark_offset_bits;
12924 iseq_bits_t tmp[1] = {0};
12926 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
12927 mark_offset_bits = tmp;
12930 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
12932 bool needs_bitmap =
false;
12934 for (code_index=0; code_index<iseq_size;) {
12936 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
12937 const char *types = insn_op_types(insn);
12943 for (op_index=0; types[op_index]; op_index++, code_index++) {
12944 const char operand_type = types[op_index];
12945 switch (operand_type) {
12948 VALUE op = ibf_load_small_value(load, &reading_pos);
12949 VALUE v = ibf_load_object(load, op);
12950 code[code_index] = v;
12953 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12954 needs_bitmap =
true;
12960 VALUE op = ibf_load_small_value(load, &reading_pos);
12961 VALUE v = ibf_load_object(load, op);
12962 v = rb_hash_dup(v);
12963 RHASH_TBL_RAW(v)->type = &cdhash_type;
12965 freeze_hide_obj(v);
12970 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
12972 code[code_index] = v;
12973 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12975 needs_bitmap =
true;
12980 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
12982 code[code_index] = v;
12985 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12986 needs_bitmap =
true;
12992 VALUE op = ibf_load_small_value(load, &reading_pos);
12993 VALUE arr = ibf_load_object(load, op);
12995 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
12996 ic->
segments = array_to_idlist(arr);
12998 code[code_index] = (
VALUE)ic;
13005 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13007 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
13008 code[code_index] = (
VALUE)ic;
13010 if (operand_type == TS_IVC) {
13013 if (insn == BIN(setinstancevariable)) {
13014 ID iv_name = (
ID)code[code_index - 1];
13015 cache->iv_set_name = iv_name;
13018 cache->iv_set_name = 0;
13021 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
13028 code[code_index] = (
VALUE)cd_entries++;
13033 VALUE op = ibf_load_small_value(load, &reading_pos);
13034 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
13041 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
13044 code[code_index] = ibf_load_small_value(load, &reading_pos);
13048 if (insn_len(insn) != op_index+1) {
13053 load_body->iseq_encoded = code;
13054 load_body->iseq_size = code_index;
13056 if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
13057 load_body->mark_bits.single = mark_offset_bits[0];
13060 if (needs_bitmap) {
13061 load_body->mark_bits.list = mark_offset_bits;
13064 load_body->mark_bits.list = 0;
13065 ruby_xfree(mark_offset_bits);
13070 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
13077 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
13080 IBF_W_ALIGN(
VALUE);
13081 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
13084 return ibf_dump_pos(dump);
13089ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
13093 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
13104 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
13107 struct rb_iseq_param_keyword dump_kw = *kw;
13108 int dv_num = kw->num - kw->required_num;
13113 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
13114 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
13116 dump_kw.table = IBF_W(ids,
ID, kw->num);
13117 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
13118 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
13119 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
13126static const struct rb_iseq_param_keyword *
13127ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
13129 if (param_keyword_offset) {
13130 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
13131 int dv_num = kw->num - kw->required_num;
13132 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
13135 for (i=0; i<dv_num; i++) {
13136 dvs[i] = ibf_load_object(load, dvs[i]);
13142 kw->default_values = dvs;
13153 ibf_offset_t offset = ibf_dump_pos(dump);
13157 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
13158 ibf_dump_write_small_value(dump, entries[i].line_no);
13159#ifdef USE_ISEQ_NODE_ID
13160 ibf_dump_write_small_value(dump, entries[i].node_id);
13162 ibf_dump_write_small_value(dump, entries[i].events);
13169ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
13171 ibf_offset_t reading_pos = body_offset;
13175 for (i = 0; i < size; i++) {
13176 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
13177#ifdef USE_ISEQ_NODE_ID
13178 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
13180 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
13187ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
13189 ibf_offset_t offset = ibf_dump_pos(dump);
13191 unsigned int last = 0;
13193 for (i = 0; i < size; i++) {
13194 ibf_dump_write_small_value(dump, positions[i] - last);
13195 last = positions[i];
13201static unsigned int *
13202ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
13204 ibf_offset_t reading_pos = positions_offset;
13205 unsigned int *positions =
ALLOC_N(
unsigned int, size);
13207 unsigned int last = 0;
13209 for (i = 0; i < size; i++) {
13210 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
13211 last = positions[i];
13221 const int size = body->local_table_size;
13225 for (i=0; i<size; i++) {
13226 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13229 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13235 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
13239ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13242 ID *table = IBF_R(local_table_offset,
ID, size);
13245 for (i=0; i<size; i++) {
13246 table[i] = ibf_load_id(load, table[i]);
13261 int *iseq_indices =
ALLOCA_N(
int, table->size);
13264 for (i=0; i<table->size; i++) {
13265 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13268 const ibf_offset_t offset = ibf_dump_pos(dump);
13270 for (i=0; i<table->size; i++) {
13271 ibf_dump_write_small_value(dump, iseq_indices[i]);
13272 ibf_dump_write_small_value(dump, table->entries[i].type);
13273 ibf_dump_write_small_value(dump, table->entries[i].start);
13274 ibf_dump_write_small_value(dump, table->entries[i].end);
13275 ibf_dump_write_small_value(dump, table->entries[i].cont);
13276 ibf_dump_write_small_value(dump, table->entries[i].sp);
13281 return ibf_dump_pos(dump);
13286ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size,
const rb_iseq_t *parent_iseq)
13289 struct iseq_catch_table *table = ruby_xcalloc(1, iseq_catch_table_bytes(size));
13290 table->size = size;
13291 ISEQ_BODY(parent_iseq)->catch_table = table;
13293 ibf_offset_t reading_pos = catch_table_offset;
13296 for (i=0; i<table->size; i++) {
13297 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13298 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13299 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13300 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13301 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13302 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13305 RB_OBJ_WRITE(parent_iseq, UNALIGNED_MEMBER_PTR(&table->entries[i], iseq), catch_iseq);
13309 ISEQ_BODY(parent_iseq)->catch_table = NULL;
13317 const unsigned int ci_size = body->ci_size;
13320 ibf_offset_t offset = ibf_dump_pos(dump);
13324 for (i = 0; i < ci_size; i++) {
13327 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13328 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13329 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13333 int len = kwarg->keyword_len;
13334 ibf_dump_write_small_value(dump,
len);
13335 for (
int j=0; j<
len; j++) {
13336 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13337 ibf_dump_write_small_value(dump, keyword);
13341 ibf_dump_write_small_value(dump, 0);
13346 ibf_dump_write_small_value(dump, (
VALUE)-1);
13364static enum rb_id_table_iterator_result
13365store_outer_variable(
ID id,
VALUE val,
void *dump)
13370 pair->name = rb_id2str(
id);
13372 return ID_TABLE_CONTINUE;
13376outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13384 else if (!bp->name) {
13394 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13396 ibf_offset_t offset = ibf_dump_pos(dump);
13398 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13399 ibf_dump_write_small_value(dump, (
VALUE)size);
13408 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13410 for (
size_t i = 0; i < size; ++i) {
13411 ID id = ovlist->pairs[i].id;
13412 ID val = ovlist->pairs[i].val;
13413 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13414 ibf_dump_write_small_value(dump, val);
13423ibf_load_ci_entries(
const struct ibf_load *load,
13424 ibf_offset_t ci_entries_offset,
13425 unsigned int ci_size,
13433 ibf_offset_t reading_pos = ci_entries_offset;
13440 for (i = 0; i < ci_size; i++) {
13441 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
13442 if (mid_index != (
VALUE)-1) {
13443 ID mid = ibf_load_id(load, mid_index);
13444 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13445 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13448 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
13451 kwarg->references = 0;
13452 kwarg->keyword_len = kwlen;
13453 for (
int j=0; j<kwlen; j++) {
13454 VALUE keyword = ibf_load_small_value(load, &reading_pos);
13455 kwarg->keywords[j] = ibf_load_object(load, keyword);
13459 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
13461 cds[i].cc = vm_cc_empty();
13472ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
13474 ibf_offset_t reading_pos = outer_variables_offset;
13478 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
13480 if (table_size > 0) {
13481 tbl = rb_id_table_create(table_size);
13484 for (
size_t i = 0; i < table_size; i++) {
13485 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
13486 VALUE value = ibf_load_small_value(load, &reading_pos);
13487 if (!key) key = rb_make_temporary_id(i);
13488 rb_id_table_insert(tbl, key, value);
13497 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
13499 unsigned int *positions;
13503 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
13504 const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
13505 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
13507#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13508 ibf_offset_t iseq_start = ibf_dump_pos(dump);
13513 buffer.obj_table = ibf_dump_object_table_new();
13514 dump->current_buffer = &buffer;
13517 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
13518 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
13519 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
13520 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
13521 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
13523 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
13524 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
13525 ruby_xfree(positions);
13527 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
13528 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
13529 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
13530 const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
13531 const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
13532 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
13533 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
13534 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
13536#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13537 ibf_offset_t local_obj_list_offset;
13538 unsigned int local_obj_list_size;
13540 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
13543 ibf_offset_t body_offset = ibf_dump_pos(dump);
13546 unsigned int param_flags =
13547 (body->
param.flags.has_lead << 0) |
13548 (body->
param.flags.has_opt << 1) |
13549 (body->
param.flags.has_rest << 2) |
13550 (body->
param.flags.has_post << 3) |
13551 (body->
param.flags.has_kw << 4) |
13552 (body->
param.flags.has_kwrest << 5) |
13553 (body->
param.flags.has_block << 6) |
13554 (body->
param.flags.ambiguous_param0 << 7) |
13555 (body->
param.flags.accepts_no_kwarg << 8) |
13556 (body->
param.flags.ruby2_keywords << 9) |
13557 (body->
param.flags.anon_rest << 10) |
13558 (body->
param.flags.anon_kwrest << 11) |
13559 (body->
param.flags.use_block << 12) |
13560 (body->
param.flags.forwardable << 13) ;
13562#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13563# define IBF_BODY_OFFSET(x) (x)
13565# define IBF_BODY_OFFSET(x) (body_offset - (x))
13568 ibf_dump_write_small_value(dump, body->type);
13569 ibf_dump_write_small_value(dump, body->iseq_size);
13570 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
13571 ibf_dump_write_small_value(dump, bytecode_size);
13572 ibf_dump_write_small_value(dump, param_flags);
13573 ibf_dump_write_small_value(dump, body->
param.size);
13574 ibf_dump_write_small_value(dump, body->
param.lead_num);
13575 ibf_dump_write_small_value(dump, body->
param.opt_num);
13576 ibf_dump_write_small_value(dump, body->
param.rest_start);
13577 ibf_dump_write_small_value(dump, body->
param.post_start);
13578 ibf_dump_write_small_value(dump, body->
param.post_num);
13579 ibf_dump_write_small_value(dump, body->
param.block_start);
13580 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
13581 ibf_dump_write_small_value(dump, param_keyword_offset);
13582 ibf_dump_write_small_value(dump, location_pathobj_index);
13583 ibf_dump_write_small_value(dump, location_base_label_index);
13584 ibf_dump_write_small_value(dump, location_label_index);
13585 ibf_dump_write_small_value(dump, body->location.first_lineno);
13586 ibf_dump_write_small_value(dump, body->location.node_id);
13587 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
13588 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
13589 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
13590 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
13591 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
13592 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
13593 ibf_dump_write_small_value(dump, body->insns_info.size);
13594 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
13595 ibf_dump_write_small_value(dump, catch_table_size);
13596 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
13597 ibf_dump_write_small_value(dump, parent_iseq_index);
13598 ibf_dump_write_small_value(dump, local_iseq_index);
13599 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
13600 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
13601 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
13602 ibf_dump_write_small_value(dump, body->variable.flip_count);
13603 ibf_dump_write_small_value(dump, body->local_table_size);
13604 ibf_dump_write_small_value(dump, body->ivc_size);
13605 ibf_dump_write_small_value(dump, body->icvarc_size);
13606 ibf_dump_write_small_value(dump, body->ise_size);
13607 ibf_dump_write_small_value(dump, body->ic_size);
13608 ibf_dump_write_small_value(dump, body->ci_size);
13609 ibf_dump_write_small_value(dump, body->stack_max);
13610 ibf_dump_write_small_value(dump, body->builtin_attrs);
13611 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
13613#undef IBF_BODY_OFFSET
13615#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13616 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
13618 dump->current_buffer = saved_buffer;
13619 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
13621 ibf_offset_t offset = ibf_dump_pos(dump);
13622 ibf_dump_write_small_value(dump, iseq_start);
13623 ibf_dump_write_small_value(dump, iseq_length_bytes);
13624 ibf_dump_write_small_value(dump, body_offset);
13626 ibf_dump_write_small_value(dump, local_obj_list_offset);
13627 ibf_dump_write_small_value(dump, local_obj_list_size);
13629 st_free_table(buffer.obj_table);
13633 return body_offset;
13638ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
13640 VALUE str = ibf_load_object(load, str_index);
13642 str = rb_fstring(str);
13652 ibf_offset_t reading_pos = offset;
13654#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13656 load->current_buffer = &load->global_buffer;
13658 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13659 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13660 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13663 buffer.buff = load->global_buffer.buff + iseq_start;
13664 buffer.size = iseq_length_bytes;
13665 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13666 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13667 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
13669 load->current_buffer = &buffer;
13670 reading_pos = body_offset;
13673#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13674# define IBF_BODY_OFFSET(x) (x)
13676# define IBF_BODY_OFFSET(x) (offset - (x))
13679 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13680 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13681 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13682 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13683 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13684 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13685 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
13686 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
13687 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
13688 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
13689 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
13690 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
13691 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13692 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13693 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
13694 const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
13695 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
13696 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
13697 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
13698 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13699 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13700 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13701 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13702 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13703 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13704 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13705 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13706 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13707 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13708 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13709 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13710 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13711 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13712 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13713 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
13714 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13716 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13717 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13718 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13719 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13721 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13722 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13723 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13724 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
13727 VALUE path = ibf_load_object(load, location_pathobj_index);
13732 realpath = path = rb_fstring(path);
13735 VALUE pathobj = path;
13741 if (!
NIL_P(realpath)) {
13743 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
13744 "(%x), path=%+"PRIsVALUE,
13745 realpath,
TYPE(realpath), path);
13747 realpath = rb_fstring(realpath);
13753 rb_iseq_pathobj_set(iseq, path, realpath);
13758 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
13760#undef IBF_BODY_OFFSET
13762 load_body->type =
type;
13763 load_body->stack_max = stack_max;
13764 load_body->
param.flags.has_lead = (param_flags >> 0) & 1;
13765 load_body->
param.flags.has_opt = (param_flags >> 1) & 1;
13766 load_body->
param.flags.has_rest = (param_flags >> 2) & 1;
13767 load_body->
param.flags.has_post = (param_flags >> 3) & 1;
13768 load_body->
param.flags.has_kw = FALSE;
13769 load_body->
param.flags.has_kwrest = (param_flags >> 5) & 1;
13770 load_body->
param.flags.has_block = (param_flags >> 6) & 1;
13771 load_body->
param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
13772 load_body->
param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
13773 load_body->
param.flags.ruby2_keywords = (param_flags >> 9) & 1;
13774 load_body->
param.flags.anon_rest = (param_flags >> 10) & 1;
13775 load_body->
param.flags.anon_kwrest = (param_flags >> 11) & 1;
13776 load_body->
param.flags.use_block = (param_flags >> 12) & 1;
13777 load_body->
param.flags.forwardable = (param_flags >> 13) & 1;
13778 load_body->
param.size = param_size;
13779 load_body->
param.lead_num = param_lead_num;
13780 load_body->
param.opt_num = param_opt_num;
13781 load_body->
param.rest_start = param_rest_start;
13782 load_body->
param.post_start = param_post_start;
13783 load_body->
param.post_num = param_post_num;
13784 load_body->
param.block_start = param_block_start;
13785 load_body->local_table_size = local_table_size;
13786 load_body->ci_size = ci_size;
13787 load_body->insns_info.size = insns_info_size;
13789 ISEQ_COVERAGE_SET(iseq,
Qnil);
13790 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
13791 load_body->variable.flip_count = variable_flip_count;
13792 load_body->variable.script_lines =
Qnil;
13794 load_body->location.first_lineno = location_first_lineno;
13795 load_body->location.node_id = location_node_id;
13796 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
13797 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
13798 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
13799 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
13800 load_body->builtin_attrs = builtin_attrs;
13801 load_body->prism = prism;
13803 load_body->ivc_size = ivc_size;
13804 load_body->icvarc_size = icvarc_size;
13805 load_body->ise_size = ise_size;
13806 load_body->ic_size = ic_size;
13808 if (ISEQ_IS_SIZE(load_body)) {
13812 load_body->is_entries = NULL;
13814 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
13815 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
13816 load_body->
param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
13817 load_body->
param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
13818 load_body->
param.flags.has_kw = (param_flags >> 4) & 1;
13819 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
13820 load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
13821 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
13822 ibf_load_catch_table(load, catch_table_offset, catch_table_size, iseq);
13826 const rb_iseq_t *mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
13828 RB_OBJ_WRITE(iseq, &load_body->parent_iseq, parent_iseq);
13829 RB_OBJ_WRITE(iseq, &load_body->local_iseq, local_iseq);
13830 RB_OBJ_WRITE(iseq, &load_body->mandatory_only_iseq, mandatory_only_iseq);
13833 if (load_body->
param.keyword != NULL) {
13835 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->
param.keyword;
13836 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
13839 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
13840#if VM_INSN_INFO_TABLE_IMPL == 2
13841 rb_iseq_insns_info_encode_positions(iseq);
13844 rb_iseq_translate_threaded_code(iseq);
13846#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13847 load->current_buffer = &load->global_buffer;
13850 RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
13851 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
13853#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13854 load->current_buffer = saved_buffer;
13856 verify_call_cache(iseq);
13859 rb_vm_pop_frame_no_int(ec);
13869ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
13874 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
13877 return ST_CONTINUE;
13887 args.offset_list = offset_list;
13889 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
13892 st_index_t size = dump->iseq_table->num_entries;
13893 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
13895 for (i = 0; i < size; i++) {
13899 ibf_dump_align(dump,
sizeof(ibf_offset_t));
13900 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
13901 header->iseq_list_size = (
unsigned int)size;
13904#define IBF_OBJECT_INTERNAL FL_PROMOTED0
13913 unsigned int type: 5;
13914 unsigned int special_const: 1;
13915 unsigned int frozen: 1;
13916 unsigned int internal: 1;
13919enum ibf_object_class_index {
13920 IBF_OBJECT_CLASS_OBJECT,
13921 IBF_OBJECT_CLASS_ARRAY,
13922 IBF_OBJECT_CLASS_STANDARD_ERROR,
13923 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
13924 IBF_OBJECT_CLASS_TYPE_ERROR,
13925 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
13935 long keyval[FLEX_ARY_LEN];
13948 BDIGIT digits[FLEX_ARY_LEN];
13951enum ibf_object_data_type {
13952 IBF_OBJECT_DATA_ENCODING,
13963#define IBF_ALIGNED_OFFSET(align, offset) \
13964 ((((offset) - 1) / (align) + 1) * (align))
13965#define IBF_OBJBODY(type, offset) (const type *)\
13966 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
13969ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
13971 if (offset >= load->current_buffer->size) {
13972 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
13974 return load->current_buffer->buff + offset;
13977NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
13980ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
13983 rb_raw_obj_info(buff,
sizeof(buff), obj);
13992 rb_raise(rb_eArgError,
"unsupported");
13999 enum ibf_object_class_index cindex;
14000 if (obj == rb_cObject) {
14001 cindex = IBF_OBJECT_CLASS_OBJECT;
14004 cindex = IBF_OBJECT_CLASS_ARRAY;
14007 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
14010 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
14013 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
14016 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
14019 rb_obj_info_dump(obj);
14021 rb_bug(
"unsupported class");
14023 ibf_dump_write_small_value(dump, (
VALUE)cindex);
14029 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
14032 case IBF_OBJECT_CLASS_OBJECT:
14034 case IBF_OBJECT_CLASS_ARRAY:
14036 case IBF_OBJECT_CLASS_STANDARD_ERROR:
14038 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
14040 case IBF_OBJECT_CLASS_TYPE_ERROR:
14042 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
14046 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
14054 (void)IBF_W(&dbl,
double, 1);
14060 const double *dblp = IBF_OBJBODY(
double, offset);
14067 long encindex = (long)rb_enc_get_index(obj);
14068 long len = RSTRING_LEN(obj);
14069 const char *ptr = RSTRING_PTR(obj);
14071 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14072 rb_encoding *enc = rb_enc_from_index((
int)encindex);
14073 const char *enc_name = rb_enc_name(enc);
14074 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
14077 ibf_dump_write_small_value(dump, encindex);
14078 ibf_dump_write_small_value(dump,
len);
14079 IBF_WP(ptr,
char,
len);
14085 ibf_offset_t reading_pos = offset;
14087 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14088 const long len = (long)ibf_load_small_value(load, &reading_pos);
14089 const char *ptr = load->current_buffer->buff + reading_pos;
14091 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14092 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14093 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14097 if (header->frozen && !header->internal) {
14098 str = rb_enc_literal_str(ptr,
len, rb_enc_from_index(encindex));
14101 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
14104 if (header->frozen) str = rb_fstring(str);
14115 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
14117 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
14118 ibf_dump_write_small_value(dump, regexp.srcstr);
14125 regexp.option = ibf_load_byte(load, &offset);
14126 regexp.srcstr = ibf_load_small_value(load, &offset);
14128 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
14129 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
14141 ibf_dump_write_small_value(dump,
len);
14142 for (i=0; i<
len; i++) {
14143 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
14144 ibf_dump_write_small_value(dump, index);
14151 ibf_offset_t reading_pos = offset;
14153 const long len = (long)ibf_load_small_value(load, &reading_pos);
14158 for (i=0; i<
len; i++) {
14159 const VALUE index = ibf_load_small_value(load, &reading_pos);
14169ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
14173 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14174 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14176 ibf_dump_write_small_value(dump, key_index);
14177 ibf_dump_write_small_value(dump, val_index);
14178 return ST_CONTINUE;
14185 ibf_dump_write_small_value(dump, (
VALUE)
len);
14193 long len = (long)ibf_load_small_value(load, &offset);
14194 VALUE obj = rb_hash_new_with_size(
len);
14197 for (i = 0; i <
len; i++) {
14198 VALUE key_index = ibf_load_small_value(load, &offset);
14199 VALUE val_index = ibf_load_small_value(load, &offset);
14201 VALUE key = ibf_load_object(load, key_index);
14202 VALUE val = ibf_load_object(load, val_index);
14203 rb_hash_aset(obj, key, val);
14205 rb_hash_rehash(obj);
14221 range.class_index = 0;
14224 range.beg = (long)ibf_dump_object(dump, beg);
14225 range.end = (long)ibf_dump_object(dump, end);
14231 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
14240 VALUE beg = ibf_load_object(load, range->beg);
14241 VALUE end = ibf_load_object(load, range->end);
14251 ssize_t
len = BIGNUM_LEN(obj);
14252 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14253 BDIGIT *d = BIGNUM_DIGITS(obj);
14255 (void)IBF_W(&slen, ssize_t, 1);
14256 IBF_WP(d, BDIGIT,
len);
14263 int sign = bignum->slen > 0;
14264 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14265 const int big_unpack_flags =
14268 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
14279 if (rb_data_is_encoding(obj)) {
14281 const char *name = rb_enc_name(enc);
14282 long len = strlen(name) + 1;
14284 data[0] = IBF_OBJECT_DATA_ENCODING;
14286 (void)IBF_W(data,
long, 2);
14287 IBF_WP(name,
char,
len);
14290 ibf_dump_object_unsupported(dump, obj);
14297 const long *body = IBF_OBJBODY(
long, offset);
14298 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14300 const char *data = (
const char *)&body[2];
14303 case IBF_OBJECT_DATA_ENCODING:
14305 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
14310 return ibf_load_object_unsupported(load, header, offset);
14314ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14317 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14318 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14320 (void)IBF_W(data,
long, 2);
14324ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14327 VALUE a = ibf_load_object(load, nums->a);
14328 VALUE b = ibf_load_object(load, nums->b);
14340 ibf_dump_object_string(dump,
rb_sym2str(obj));
14346 ibf_offset_t reading_pos = offset;
14348 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14349 const long len = (long)ibf_load_small_value(load, &reading_pos);
14350 const char *ptr = load->current_buffer->buff + reading_pos;
14352 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14353 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14354 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14357 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
14361typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
14362static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
14363 ibf_dump_object_unsupported,
14364 ibf_dump_object_unsupported,
14365 ibf_dump_object_class,
14366 ibf_dump_object_unsupported,
14367 ibf_dump_object_float,
14368 ibf_dump_object_string,
14369 ibf_dump_object_regexp,
14370 ibf_dump_object_array,
14371 ibf_dump_object_hash,
14372 ibf_dump_object_struct,
14373 ibf_dump_object_bignum,
14374 ibf_dump_object_unsupported,
14375 ibf_dump_object_data,
14376 ibf_dump_object_unsupported,
14377 ibf_dump_object_complex_rational,
14378 ibf_dump_object_complex_rational,
14379 ibf_dump_object_unsupported,
14380 ibf_dump_object_unsupported,
14381 ibf_dump_object_unsupported,
14382 ibf_dump_object_unsupported,
14383 ibf_dump_object_symbol,
14384 ibf_dump_object_unsupported,
14385 ibf_dump_object_unsupported,
14386 ibf_dump_object_unsupported,
14387 ibf_dump_object_unsupported,
14388 ibf_dump_object_unsupported,
14389 ibf_dump_object_unsupported,
14390 ibf_dump_object_unsupported,
14391 ibf_dump_object_unsupported,
14392 ibf_dump_object_unsupported,
14393 ibf_dump_object_unsupported,
14394 ibf_dump_object_unsupported,
14400 unsigned char byte =
14401 (header.type << 0) |
14402 (header.special_const << 5) |
14403 (header.frozen << 6) |
14404 (header.internal << 7);
14410ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
14412 unsigned char byte = ibf_load_byte(load, offset);
14415 header.type = (
byte >> 0) & 0x1f;
14416 header.special_const = (
byte >> 5) & 0x01;
14417 header.frozen = (
byte >> 6) & 0x01;
14418 header.internal = (
byte >> 7) & 0x01;
14427 ibf_offset_t current_offset;
14428 IBF_ZERO(obj_header);
14429 obj_header.type =
TYPE(obj);
14431 IBF_W_ALIGN(ibf_offset_t);
14432 current_offset = ibf_dump_pos(dump);
14437 obj_header.special_const = TRUE;
14438 obj_header.frozen = TRUE;
14439 obj_header.internal = TRUE;
14440 ibf_dump_object_object_header(dump, obj_header);
14441 ibf_dump_write_small_value(dump, obj);
14445 obj_header.special_const = FALSE;
14446 obj_header.frozen =
OBJ_FROZEN(obj) ? TRUE : FALSE;
14447 ibf_dump_object_object_header(dump, obj_header);
14448 (*dump_object_functions[obj_header.type])(dump, obj);
14451 return current_offset;
14455static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
14456 ibf_load_object_unsupported,
14457 ibf_load_object_unsupported,
14458 ibf_load_object_class,
14459 ibf_load_object_unsupported,
14460 ibf_load_object_float,
14461 ibf_load_object_string,
14462 ibf_load_object_regexp,
14463 ibf_load_object_array,
14464 ibf_load_object_hash,
14465 ibf_load_object_struct,
14466 ibf_load_object_bignum,
14467 ibf_load_object_unsupported,
14468 ibf_load_object_data,
14469 ibf_load_object_unsupported,
14470 ibf_load_object_complex_rational,
14471 ibf_load_object_complex_rational,
14472 ibf_load_object_unsupported,
14473 ibf_load_object_unsupported,
14474 ibf_load_object_unsupported,
14475 ibf_load_object_unsupported,
14476 ibf_load_object_symbol,
14477 ibf_load_object_unsupported,
14478 ibf_load_object_unsupported,
14479 ibf_load_object_unsupported,
14480 ibf_load_object_unsupported,
14481 ibf_load_object_unsupported,
14482 ibf_load_object_unsupported,
14483 ibf_load_object_unsupported,
14484 ibf_load_object_unsupported,
14485 ibf_load_object_unsupported,
14486 ibf_load_object_unsupported,
14487 ibf_load_object_unsupported,
14491ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
14493 if (object_index == 0) {
14497 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
14499 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
14500 ibf_offset_t offset = offsets[object_index];
14501 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
14504 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
14505 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
14506 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
14507 header.type, header.special_const, header.frozen, header.internal);
14509 if (offset >= load->current_buffer->size) {
14510 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
14513 if (header.special_const) {
14514 ibf_offset_t reading_pos = offset;
14516 obj = ibf_load_small_value(load, &reading_pos);
14519 obj = (*load_object_functions[header.type])(load, &header, offset);
14522 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
14525 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
14526 object_index, obj);
14539ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14544 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
14547 return ST_CONTINUE;
14551ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
14553 st_table *obj_table = dump->current_buffer->obj_table;
14558 args.offset_list = offset_list;
14560 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
14562 IBF_W_ALIGN(ibf_offset_t);
14563 *obj_list_offset = ibf_dump_pos(dump);
14565 st_index_t size = obj_table->num_entries;
14568 for (i=0; i<size; i++) {
14573 *obj_list_size = (
unsigned int)size;
14577ibf_dump_mark(
void *ptr)
14580 rb_gc_mark(dump->global_buffer.str);
14582 rb_mark_set(dump->global_buffer.obj_table);
14583 rb_mark_set(dump->iseq_table);
14587ibf_dump_free(
void *ptr)
14590 if (dump->global_buffer.obj_table) {
14591 st_free_table(dump->global_buffer.obj_table);
14592 dump->global_buffer.obj_table = 0;
14594 if (dump->iseq_table) {
14595 st_free_table(dump->iseq_table);
14596 dump->iseq_table = 0;
14601ibf_dump_memsize(
const void *ptr)
14605 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
14606 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
14612 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
14613 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
14619 dump->global_buffer.obj_table = NULL;
14620 dump->iseq_table = NULL;
14623 dump->global_buffer.obj_table = ibf_dump_object_table_new();
14624 dump->iseq_table = st_init_numtable();
14626 dump->current_buffer = &dump->global_buffer;
14637 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
14638 ISEQ_BODY(iseq)->local_iseq != iseq) {
14641 if (
RTEST(ISEQ_COVERAGE(iseq))) {
14646 ibf_dump_setup(dump, dump_obj);
14648 ibf_dump_write(dump, &header,
sizeof(header));
14649 ibf_dump_iseq(dump, iseq);
14651 header.magic[0] =
'Y';
14652 header.magic[1] =
'A';
14653 header.magic[2] =
'R';
14654 header.magic[3] =
'B';
14655 header.major_version = IBF_MAJOR_VERSION;
14656 header.minor_version = IBF_MINOR_VERSION;
14657 header.endian = IBF_ENDIAN_MARK;
14659 ibf_dump_iseq_list(dump, &header);
14660 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
14661 header.size = ibf_dump_pos(dump);
14664 VALUE opt_str = opt;
14667 ibf_dump_write(dump, ptr, header.extra_size);
14670 header.extra_size = 0;
14673 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
14675 str = dump->global_buffer.str;
14680static const ibf_offset_t *
14681ibf_iseq_list(
const struct ibf_load *load)
14683 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
14687rb_ibf_load_iseq_complete(
rb_iseq_t *iseq)
14691 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
14694 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
14695 iseq->aux.loader.index, offset,
14696 load->header->size);
14698 ibf_load_iseq_each(load, iseq, offset);
14699 ISEQ_COMPILE_DATA_CLEAR(iseq);
14701 rb_iseq_init_trace(iseq);
14702 load->iseq = prev_src_iseq;
14709 rb_ibf_load_iseq_complete((
rb_iseq_t *)iseq);
14717 int iseq_index = (int)(
VALUE)index_iseq;
14720 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
14721 (
void *)index_iseq, (
void *)load->iseq_list);
14723 if (iseq_index == -1) {
14727 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
14730 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
14738 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
14741 iseq->aux.loader.obj = load->loader_obj;
14742 iseq->aux.loader.index = iseq_index;
14744 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
14745 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
14747 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
14749 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
14751 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
14753 rb_ibf_load_iseq_complete(iseq);
14757 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
14758 (
void *)iseq, (
void *)load->iseq);
14766ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
14769 load->loader_obj = loader_obj;
14770 load->global_buffer.buff = bytes;
14771 load->header = header;
14772 load->global_buffer.size = header->size;
14773 load->global_buffer.obj_list_offset = header->global_object_list_offset;
14774 load->global_buffer.obj_list_size = header->global_object_list_size;
14775 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
14776 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
14779 load->current_buffer = &load->global_buffer;
14781 if (size < header->size) {
14784 if (strncmp(header->magic,
"YARB", 4) != 0) {
14787 if (header->major_version != IBF_MAJOR_VERSION ||
14788 header->minor_version != IBF_MINOR_VERSION) {
14790 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
14792 if (header->endian != IBF_ENDIAN_MARK) {
14798 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14799 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
14800 header->iseq_list_offset);
14802 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14803 rb_raise(rb_eArgError,
"unaligned object list offset: %u",
14804 load->global_buffer.obj_list_offset);
14817 if (USE_LAZY_LOAD) {
14818 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
14821 ibf_load_setup_bytes(load, loader_obj, RSTRING_PTR(str), RSTRING_LEN(str));
14826ibf_loader_mark(
void *ptr)
14829 rb_gc_mark(load->str);
14830 rb_gc_mark(load->iseq_list);
14831 rb_gc_mark(load->global_buffer.obj_list);
14835ibf_loader_free(
void *ptr)
14842ibf_loader_memsize(
const void *ptr)
14849 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
14850 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
14854rb_iseq_ibf_load(
VALUE str)
14860 ibf_load_setup(load, loader_obj, str);
14861 iseq = ibf_load_iseq(load, 0);
14868rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
14874 ibf_load_setup_bytes(load, loader_obj, bytes, size);
14875 iseq = ibf_load_iseq(load, 0);
14882rb_iseq_ibf_load_extra_data(
VALUE str)
14888 ibf_load_setup(load, loader_obj, str);
14889 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
14894#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 OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#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 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.
VALUE rb_ary_reverse(VALUE ary)
Destructively reverses the passed array in-place.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#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.