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);
497static int iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq);
500static int compile_hash(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped);
507verify_list(ISEQ_ARG_DECLARE
const char *info,
LINK_ANCHOR *
const anchor)
513 if (!compile_debug)
return;
515 list = anchor->anchor.next;
516 plist = &anchor->anchor;
518 if (plist != list->prev) {
525 if (anchor->last != plist && anchor->last != 0) {
530 rb_bug(
"list verify error: %08x (%s)", flag, info);
535#define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
542 VALUE *original = rb_iseq_original_iseq(iseq);
544 while (i < ISEQ_BODY(iseq)->iseq_size) {
545 VALUE insn = original[i];
546 const char *types = insn_op_types(insn);
548 for (
int j=0; types[j]; j++) {
549 if (types[j] == TS_CALLDATA) {
553 if (cc != vm_cc_empty()) {
555 rb_bug(
"call cache is not initialized by vm_cc_empty()");
562 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
563 struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
566 if (cc != NULL && cc != vm_cc_empty()) {
568 rb_bug(
"call cache is not initialized by vm_cc_empty()");
580 elem->prev = anchor->last;
581 anchor->last->next = elem;
583 verify_list(
"add", anchor);
593 elem->next = before->next;
594 elem->next->prev = elem;
596 if (before == anchor->last) anchor->last = elem;
597 verify_list(
"add", anchor);
600#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
601#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
605branch_coverage_valid_p(
rb_iseq_t *iseq,
int first_line)
607 if (!ISEQ_COVERAGE(iseq))
return 0;
608 if (!ISEQ_BRANCH_COVERAGE(iseq))
return 0;
609 if (first_line <= 0)
return 0;
613#define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x)))
618 const int first_lineno = loc->beg_pos.lineno, first_column = loc->beg_pos.column;
619 const int last_lineno = loc->end_pos.lineno, last_column = loc->end_pos.column;
622 rb_hash_aset(structure, key, branch);
634 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return Qundef;
645 VALUE branch_base = rb_hash_aref(structure, key);
648 if (
NIL_P(branch_base)) {
649 branch_base = setup_branch(loc,
type, structure, key);
650 branches = rb_hash_new();
662generate_dummy_line_node(
int lineno,
int node_id)
665 nd_set_line(&dummy, lineno);
666 nd_set_node_id(&dummy, node_id);
673 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return;
684 VALUE branch = rb_hash_aref(branches, key);
688 branch = setup_branch(loc,
type, branches, key);
698 ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
699 ADD_SYNTHETIC_INSN(seq, loc->end_pos.lineno, node_id, nop);
702#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
705validate_label(st_data_t name, st_data_t label, st_data_t arg)
709 if (!lobj->link.next) {
711 COMPILE_ERROR(iseq, lobj->position,
712 "%"PRIsVALUE
": undefined label",
722 st_foreach(labels_table, validate_label, (st_data_t)iseq);
723 st_free_table(labels_table);
727get_nd_recv(
const NODE *node)
729 switch (nd_type(node)) {
731 return RNODE_CALL(node)->nd_recv;
733 return RNODE_OPCALL(node)->nd_recv;
737 return RNODE_QCALL(node)->nd_recv;
741 return RNODE_ATTRASGN(node)->nd_recv;
743 return RNODE_OP_ASGN1(node)->nd_recv;
745 return RNODE_OP_ASGN2(node)->nd_recv;
747 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
752get_node_call_nd_mid(
const NODE *node)
754 switch (nd_type(node)) {
756 return RNODE_CALL(node)->nd_mid;
758 return RNODE_OPCALL(node)->nd_mid;
760 return RNODE_FCALL(node)->nd_mid;
762 return RNODE_QCALL(node)->nd_mid;
764 return RNODE_VCALL(node)->nd_mid;
766 return RNODE_ATTRASGN(node)->nd_mid;
768 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
773get_nd_args(
const NODE *node)
775 switch (nd_type(node)) {
777 return RNODE_CALL(node)->nd_args;
779 return RNODE_OPCALL(node)->nd_args;
781 return RNODE_FCALL(node)->nd_args;
783 return RNODE_QCALL(node)->nd_args;
787 return RNODE_ATTRASGN(node)->nd_args;
789 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
794get_node_colon_nd_mid(
const NODE *node)
796 switch (nd_type(node)) {
798 return RNODE_COLON2(node)->nd_mid;
800 return RNODE_COLON3(node)->nd_mid;
802 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
807get_nd_vid(
const NODE *node)
809 switch (nd_type(node)) {
811 return RNODE_LASGN(node)->nd_vid;
813 return RNODE_DASGN(node)->nd_vid;
815 return RNODE_IASGN(node)->nd_vid;
817 return RNODE_CVASGN(node)->nd_vid;
819 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
824get_nd_value(
const NODE *node)
826 switch (nd_type(node)) {
828 return RNODE_LASGN(node)->nd_value;
830 return RNODE_DASGN(node)->nd_value;
832 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
837get_string_value(
const NODE *node)
839 switch (nd_type(node)) {
841 return rb_node_str_string_val(node);
843 return rb_node_file_path_val(node);
845 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
855 (*ifunc->func)(iseq, ret, ifunc->data);
857 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
859 CHECK(iseq_setup_insn(iseq, ret));
860 return iseq_setup(iseq, ret);
863static bool drop_unreachable_return(
LINK_ANCHOR *ret);
872 NO_CHECK(COMPILE(ret,
"nil", node));
873 iseq_set_local_table(iseq, 0, 0);
876 else if (nd_type_p(node, NODE_SCOPE)) {
878 iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl, (
NODE *)RNODE_SCOPE(node)->nd_args);
879 iseq_set_arguments(iseq, ret, (
NODE *)RNODE_SCOPE(node)->nd_args);
880 iseq_set_parameters_lvar_state(iseq);
882 switch (ISEQ_BODY(iseq)->
type) {
883 case ISEQ_TYPE_BLOCK:
885 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
886 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
888 start->rescued = LABEL_RESCUE_BEG;
889 end->rescued = LABEL_RESCUE_END;
892 ADD_SYNTHETIC_INSN(ret, ISEQ_BODY(iseq)->location.first_lineno, -1, nop);
893 ADD_LABEL(ret, start);
894 CHECK(COMPILE(ret,
"block body", RNODE_SCOPE(node)->nd_body));
897 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
900 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
901 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
904 case ISEQ_TYPE_CLASS:
907 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
909 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
912 case ISEQ_TYPE_METHOD:
914 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
916 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
917 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
919 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
923 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
930#define INVALID_ISEQ_TYPE(type) \
931 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
932 switch (ISEQ_BODY(iseq)->
type) {
933 case INVALID_ISEQ_TYPE(
METHOD);
934 case INVALID_ISEQ_TYPE(CLASS);
935 case INVALID_ISEQ_TYPE(BLOCK);
936 case INVALID_ISEQ_TYPE(EVAL);
937 case INVALID_ISEQ_TYPE(MAIN);
938 case INVALID_ISEQ_TYPE(TOP);
939#undef INVALID_ISEQ_TYPE
940 case ISEQ_TYPE_RESCUE:
941 iseq_set_exception_local_table(iseq);
942 CHECK(COMPILE(ret,
"rescue", node));
944 case ISEQ_TYPE_ENSURE:
945 iseq_set_exception_local_table(iseq);
946 CHECK(COMPILE_POPPED(ret,
"ensure", node));
948 case ISEQ_TYPE_PLAIN:
949 CHECK(COMPILE(ret,
"ensure", node));
952 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
955 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
960 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
961 NODE dummy_line_node = generate_dummy_line_node(0, -1);
962 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
963 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
965 else if (!drop_unreachable_return(ret)) {
966 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
970 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
971 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
972 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
973 validate_labels(iseq, labels_table);
976 CHECK(iseq_setup_insn(iseq, ret));
977 return iseq_setup(iseq, ret);
981rb_iseq_translate_threaded_code(
rb_iseq_t *iseq)
983#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
984 const void *
const *table = rb_vm_get_insns_address_table();
986 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
988 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
989 int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
990 int len = insn_len(insn);
991 encoded[i] = (
VALUE)table[insn];
998 rb_yjit_live_iseq_count++;
999 rb_yjit_iseq_alloc_count++;
1006rb_iseq_original_iseq(
const rb_iseq_t *iseq)
1008 VALUE *original_code;
1010 if (ISEQ_ORIGINAL_ISEQ(iseq))
return ISEQ_ORIGINAL_ISEQ(iseq);
1011 original_code = ISEQ_ORIGINAL_ISEQ_ALLOC(iseq, ISEQ_BODY(iseq)->iseq_size);
1012 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
1014#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1018 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1019 const void *addr = (
const void *)original_code[i];
1020 const int insn = rb_vm_insn_addr2insn(addr);
1022 original_code[i] = insn;
1023 i += insn_len(insn);
1027 return original_code;
1040#if defined(__sparc) && SIZEOF_VOIDP == 4 && defined(__GNUC__)
1041 #define STRICT_ALIGNMENT
1047#if defined(__OpenBSD__)
1048 #include <sys/endian.h>
1049 #ifdef __STRICT_ALIGNMENT
1050 #define STRICT_ALIGNMENT
1054#ifdef STRICT_ALIGNMENT
1055 #if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
1056 #define ALIGNMENT_SIZE SIZEOF_LONG_LONG
1058 #define ALIGNMENT_SIZE SIZEOF_VALUE
1060 #define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
1061 #define ALIGNMENT_SIZE_MASK PADDING_SIZE_MAX
1064 #define PADDING_SIZE_MAX 0
1067#ifdef STRICT_ALIGNMENT
1070calc_padding(
void *ptr,
size_t size)
1075 mis = (size_t)ptr & ALIGNMENT_SIZE_MASK;
1077 padding = ALIGNMENT_SIZE - mis;
1083#if ALIGNMENT_SIZE > SIZEOF_VALUE
1084 if (size ==
sizeof(
VALUE) && padding ==
sizeof(
VALUE)) {
1098#ifdef STRICT_ALIGNMENT
1099 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1101 const size_t padding = 0;
1104 if (size >= INT_MAX - padding) rb_memerror();
1105 if (storage->pos + size + padding > storage->size) {
1106 unsigned int alloc_size = storage->size;
1108 while (alloc_size < size + PADDING_SIZE_MAX) {
1109 if (alloc_size >= INT_MAX / 2) rb_memerror();
1112 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
1114 storage = *arena = storage->next;
1117 storage->size = alloc_size;
1118#ifdef STRICT_ALIGNMENT
1119 padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1123#ifdef STRICT_ALIGNMENT
1124 storage->pos += (int)padding;
1127 ptr = (
void *)&storage->buff[storage->pos];
1128 storage->pos += (int)size;
1133compile_data_alloc(
rb_iseq_t *iseq,
size_t size)
1136 return compile_data_alloc_with_arena(arena, size);
1140compile_data_alloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1143 return compile_data_alloc(iseq, size);
1147compile_data_calloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1150 void *p = compile_data_alloc(iseq, size);
1159 return (
INSN *)compile_data_alloc_with_arena(arena,
sizeof(
INSN));
1163compile_data_alloc_label(
rb_iseq_t *iseq)
1165 return (
LABEL *)compile_data_alloc(iseq,
sizeof(
LABEL));
1169compile_data_alloc_adjust(
rb_iseq_t *iseq)
1171 return (
ADJUST *)compile_data_alloc(iseq,
sizeof(
ADJUST));
1175compile_data_alloc_trace(
rb_iseq_t *iseq)
1177 return (
TRACE *)compile_data_alloc(iseq,
sizeof(
TRACE));
1186 elem2->next = elem1->next;
1187 elem2->prev = elem1;
1188 elem1->next = elem2;
1190 elem2->next->prev = elem2;
1200 elem2->prev = elem1->prev;
1201 elem2->next = elem1;
1202 elem1->prev = elem2;
1204 elem2->prev->next = elem2;
1214 elem2->prev = elem1->prev;
1215 elem2->next = elem1->next;
1217 elem1->prev->next = elem2;
1220 elem1->next->prev = elem2;
1227 elem->prev->next = elem->next;
1229 elem->next->prev = elem->prev;
1236 return anchor->anchor.next;
1242 return anchor->last;
1249 switch (elem->type) {
1250 case ISEQ_ELEMENT_INSN:
1251 case ISEQ_ELEMENT_ADJUST:
1261LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1263 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1264 if (first_insn != NULL &&
1265 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1274LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1276 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1294 if (anc2->anchor.next) {
1297 anc1->last->next = anc2->anchor.next;
1298 anc2->anchor.next->prev = anc1->last;
1299 anc1->last = anc2->last;
1304 verify_list(
"append", anc1);
1307#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1316 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1317 (
void *)anchor->anchor.next, (
void *)anchor->last);
1319 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1320 (
void *)list->prev, (
int)list->type);
1325 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1326 verify_list(
"debug list", anchor);
1329#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1332#define debug_list(anc, cur) ((void)0)
1338 TRACE *trace = compile_data_alloc_trace(iseq);
1340 trace->link.type = ISEQ_ELEMENT_TRACE;
1341 trace->link.next = NULL;
1342 trace->event = event;
1349new_label_body(
rb_iseq_t *iseq,
long line)
1351 LABEL *labelobj = compile_data_alloc_label(iseq);
1353 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1354 labelobj->link.next = 0;
1356 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1357 labelobj->sc_state = 0;
1359 labelobj->refcnt = 0;
1361 labelobj->rescued = LABEL_RESCUE_NONE;
1362 labelobj->unremovable = 0;
1363 labelobj->position = -1;
1370 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1371 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1372 adjust->link.next = 0;
1373 adjust->label = label;
1374 adjust->line_no = line;
1375 LABEL_UNREMOVABLE(label);
1382 const char *types = insn_op_types(insn->insn_id);
1383 for (
int j = 0; types[j]; j++) {
1384 char type = types[j];
1391 func(&OPERAND_AT(insn, j), data);
1400iseq_insn_each_object_write_barrier(
VALUE * obj,
VALUE iseq)
1406new_insn_core(
rb_iseq_t *iseq,
int line_no,
int node_id,
int insn_id,
int argc,
VALUE *argv)
1408 INSN *iobj = compile_data_alloc_insn(iseq);
1412 iobj->link.type = ISEQ_ELEMENT_INSN;
1413 iobj->link.next = 0;
1414 iobj->insn_id = insn_id;
1415 iobj->insn_info.line_no = line_no;
1416 iobj->insn_info.node_id = node_id;
1417 iobj->insn_info.events = 0;
1418 iobj->operands = argv;
1419 iobj->operand_size = argc;
1422 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1428new_insn_body(
rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...)
1430 VALUE *operands = 0;
1434 va_start(argv, argc);
1435 operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
1436 for (i = 0; i < argc; i++) {
1442 return new_insn_core(iseq, line_no, node_id, insn_id, argc, operands);
1446insn_replace_with_operands(
rb_iseq_t *iseq,
INSN *iobj,
enum ruby_vminsn_type insn_id,
int argc, ...)
1448 VALUE *operands = 0;
1452 va_start(argv, argc);
1453 operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
1454 for (i = 0; i < argc; i++) {
1461 iobj->insn_id = insn_id;
1462 iobj->operand_size = argc;
1463 iobj->operands = operands;
1464 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1472 VM_ASSERT(argc >= 0);
1475 flag |= VM_CALL_KWARG;
1476 argc += kw_arg->keyword_len;
1479 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING))
1480 && !has_blockiseq) {
1481 flag |= VM_CALL_ARGS_SIMPLE;
1484 ISEQ_BODY(iseq)->ci_size++;
1485 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1493 VALUE *operands = compile_data_calloc2(iseq,
sizeof(
VALUE), 2);
1496 operands[1] = (
VALUE)blockiseq;
1503 if (vm_ci_flag((
struct rb_callinfo *)ci) & VM_CALL_FORWARDING) {
1504 insn = new_insn_core(iseq, line_no, node_id, BIN(sendforward), 2, operands);
1507 insn = new_insn_core(iseq, line_no, node_id, BIN(send), 2, operands);
1520 VALUE ast_value = rb_ruby_ast_new(node);
1522 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1523 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1524 ret_iseq = rb_iseq_new_with_opt(ast_value, name,
1525 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1527 isolated_depth ? isolated_depth + 1 : 0,
1528 type, ISEQ_COMPILE_DATA(iseq)->option,
1529 ISEQ_BODY(iseq)->variable.script_lines);
1530 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1540 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1541 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1542 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1543 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1544 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1552 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1553 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1554 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1555 set_catch_except_p((
rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1578 while (pos < body->iseq_size) {
1579 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1580 if (insn == BIN(
throw)) {
1581 set_catch_except_p(iseq);
1584 pos += insn_len(insn);
1590 for (i = 0; i < ct->size; i++) {
1592 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1593 if (entry->type != CATCH_TYPE_BREAK
1594 && entry->type != CATCH_TYPE_NEXT
1595 && entry->type != CATCH_TYPE_REDO) {
1597 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1604iseq_insert_nop_between_end_and_cont(
rb_iseq_t *iseq)
1606 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1607 if (
NIL_P(catch_table_ary))
return;
1608 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1610 for (i = 0; i < tlen; i++) {
1616 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1618 if (ct != CATCH_TYPE_BREAK
1619 && ct != CATCH_TYPE_NEXT
1620 && ct != CATCH_TYPE_REDO) {
1622 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1624 INSN *nop = new_insn_core(iseq, 0, -1, BIN(nop), 0, 0);
1625 ELEM_INSERT_NEXT(end, &nop->link);
1638 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1643 if (compile_debug > 5)
1644 dump_disasm_list(FIRST_ELEMENT(anchor));
1646 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1647 iseq_optimize(iseq, anchor);
1649 if (compile_debug > 5)
1650 dump_disasm_list(FIRST_ELEMENT(anchor));
1652 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1653 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1654 iseq_insns_unification(iseq, anchor);
1655 if (compile_debug > 5)
1656 dump_disasm_list(FIRST_ELEMENT(anchor));
1659 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1660 iseq_insert_nop_between_end_and_cont(iseq);
1661 if (compile_debug > 5)
1662 dump_disasm_list(FIRST_ELEMENT(anchor));
1670 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1673 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1674 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1675 if (compile_debug > 5)
1676 dump_disasm_list(FIRST_ELEMENT(anchor));
1678 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1679 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1681 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1682 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1684 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1685 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1687 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1689 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1691 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1693 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1694 xfree(ISEQ_BODY(iseq)->catch_table);
1695 ISEQ_BODY(iseq)->catch_table = NULL;
1698#if VM_INSN_INFO_TABLE_IMPL == 2
1699 if (ISEQ_BODY(iseq)->insns_info.succ_index_table == NULL) {
1700 debugs(
"[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
1701 rb_iseq_insns_info_encode_positions(iseq);
1705 if (compile_debug > 1) {
1706 VALUE str = rb_iseq_disasm(iseq);
1709 verify_call_cache(iseq);
1710 debugs(
"[compile step: finish]\n");
1716iseq_set_exception_local_table(
rb_iseq_t *iseq)
1718 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1719 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1720 ISEQ_BODY(iseq)->lvar_states = NULL;
1728 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1730 iseq = ISEQ_BODY(iseq)->parent_iseq;
1736get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1740 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1741 if (ISEQ_BODY(iseq)->local_table[i] == id) {
1751 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1754 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1755 "get_local_var_idx: %d", idx);
1762get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1764 int lv = 0, idx = -1;
1765 const rb_iseq_t *
const topmost_iseq = iseq;
1768 idx = get_dyna_var_idx_at_raw(iseq,
id);
1772 iseq = ISEQ_BODY(iseq)->parent_iseq;
1777 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1778 "get_dyna_var_idx: -1");
1782 *ls = ISEQ_BODY(iseq)->local_table_size;
1787iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1791 iseq = ISEQ_BODY(iseq)->parent_iseq;
1794 body = ISEQ_BODY(iseq);
1795 if (body->local_iseq == iseq &&
1796 body->
param.flags.has_block &&
1797 body->local_table_size - body->
param.block_start == idx) {
1806iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1809 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1810 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1821access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1823 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1825 if (isolated_depth && level >= isolated_depth) {
1826 if (
id == rb_intern(
"yield")) {
1827 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1830 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable '%s' from isolated Proc", rb_id2name(
id));
1834 for (
int i=0; i<level; i++) {
1836 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1839 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1842 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1843 if (write && !val) {
1844 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1848 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1851 iseq = ISEQ_BODY(iseq)->parent_iseq;
1856iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1858 for (
int i=0; i<level; i++) {
1859 iseq = ISEQ_BODY(iseq)->parent_iseq;
1862 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1868update_lvar_state(
const rb_iseq_t *iseq,
int level,
int idx)
1870 for (
int i=0; i<level; i++) {
1871 iseq = ISEQ_BODY(iseq)->parent_iseq;
1874 enum lvar_state *states = ISEQ_BODY(iseq)->lvar_states;
1875 int table_idx = ISEQ_BODY(iseq)->local_table_size - idx;
1876 switch (states[table_idx]) {
1877 case lvar_uninitialized:
1878 states[table_idx] = lvar_initialized;
1880 case lvar_initialized:
1881 states[table_idx] = lvar_reassigned;
1883 case lvar_reassigned:
1887 rb_bug(
"unreachable");
1892iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq)
1894 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
1895 ISEQ_BODY(iseq)->lvar_states[i] = lvar_initialized;
1898 int lead_num = ISEQ_BODY(iseq)->param.lead_num;
1899 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
1900 for (
int i=0; i<opt_num; i++) {
1901 ISEQ_BODY(iseq)->lvar_states[lead_num + i] = lvar_uninitialized;
1910 if (iseq_local_block_param_p(iseq, idx, level)) {
1911 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1914 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1916 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1922 if (iseq_local_block_param_p(iseq, idx, level)) {
1923 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1926 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1928 update_lvar_state(iseq, level, idx);
1929 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
1938 if (body->
param.flags.has_opt ||
1939 body->
param.flags.has_post ||
1940 body->
param.flags.has_rest ||
1941 body->
param.flags.has_block ||
1942 body->
param.flags.has_kw ||
1943 body->
param.flags.has_kwrest) {
1945 if (body->
param.flags.has_block) {
1946 body->
param.size = body->
param.block_start + 1;
1948 else if (body->
param.flags.has_kwrest) {
1949 body->
param.size = body->
param.keyword->rest_start + 1;
1951 else if (body->
param.flags.has_kw) {
1952 body->
param.size = body->
param.keyword->bits_start + 1;
1954 else if (body->
param.flags.has_post) {
1957 else if (body->
param.flags.has_rest) {
1958 body->
param.size = body->
param.rest_start + 1;
1960 else if (body->
param.flags.has_opt) {
1978 struct rb_iseq_param_keyword *keyword;
1981 int kw = 0, rkw = 0, di = 0, i;
1983 body->
param.flags.has_kw = TRUE;
1984 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
1988 node = node->nd_next;
1991 keyword->bits_start = arg_size++;
1993 node = args->kw_args;
1995 const NODE *val_node = get_nd_value(node->nd_body);
1998 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
2002 switch (nd_type(val_node)) {
2004 dv = rb_node_sym_string_val(val_node);
2007 dv = rb_node_regx_string_val(val_node);
2010 dv = rb_node_line_lineno_val(val_node);
2013 dv = rb_node_integer_literal_val(val_node);
2016 dv = rb_node_float_literal_val(val_node);
2019 dv = rb_node_rational_literal_val(val_node);
2021 case NODE_IMAGINARY:
2022 dv = rb_node_imaginary_literal_val(val_node);
2025 dv = rb_node_encoding_val(val_node);
2037 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
2041 keyword->num = ++di;
2045 node = node->nd_next;
2050 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
2051 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2052 keyword->rest_start = arg_size++;
2053 body->
param.flags.has_kwrest = TRUE;
2055 if (kw_id == idPow) body->
param.flags.anon_kwrest = TRUE;
2057 keyword->required_num = rkw;
2058 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
2063 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
2065 if (dv == complex_mark) dv =
Qundef;
2069 keyword->default_values = dvs;
2078 if (!body->
param.flags.use_block) {
2079 body->
param.flags.use_block = 1;
2084 st_data_t key = (st_data_t)rb_intern_str(body->location.label);
2085 set_insert(vm->unused_block_warning_table, key);
2093 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2097 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2103 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2105 body->
param.flags.ruby2_keywords = args->ruby2_keywords;
2106 body->
param.lead_num = arg_size = (int)args->pre_args_num;
2107 if (body->
param.lead_num > 0) body->
param.flags.has_lead = TRUE;
2108 debugs(
" - argc: %d\n", body->
param.lead_num);
2110 rest_id = args->rest_arg;
2111 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2115 block_id = args->block_arg;
2117 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2119 if (optimized_forward) {
2124 if (args->opt_args) {
2132 label = NEW_LABEL(nd_line(RNODE(node)));
2134 ADD_LABEL(optargs, label);
2135 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2136 node = node->nd_next;
2141 label = NEW_LABEL(nd_line(node_args));
2143 ADD_LABEL(optargs, label);
2148 for (j = 0; j < i+1; j++) {
2153 body->
param.flags.has_opt = TRUE;
2154 body->
param.opt_num = i;
2155 body->
param.opt_table = opt_table;
2160 body->
param.rest_start = arg_size++;
2161 body->
param.flags.has_rest = TRUE;
2162 if (rest_id ==
'*') body->
param.flags.anon_rest = TRUE;
2166 if (args->first_post_arg) {
2167 body->
param.post_start = arg_size;
2168 body->
param.post_num = args->post_args_num;
2169 body->
param.flags.has_post = TRUE;
2170 arg_size += args->post_args_num;
2172 if (body->
param.flags.has_rest) {
2173 body->
param.post_start = body->
param.rest_start + 1;
2177 if (args->kw_args) {
2178 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2180 else if (args->kw_rest_arg && !optimized_forward) {
2181 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2182 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2183 keyword->rest_start = arg_size++;
2184 body->
param.keyword = keyword;
2185 body->
param.flags.has_kwrest = TRUE;
2187 static ID anon_kwrest = 0;
2188 if (!anon_kwrest) anon_kwrest = rb_intern(
"**");
2189 if (kw_id == anon_kwrest) body->
param.flags.anon_kwrest = TRUE;
2191 else if (args->no_kwarg) {
2192 body->
param.flags.accepts_no_kwarg = TRUE;
2196 body->
param.block_start = arg_size++;
2197 body->
param.flags.has_block = TRUE;
2198 iseq_set_use_block(iseq);
2202 if (optimized_forward) {
2203 body->
param.flags.use_block = 1;
2204 body->
param.flags.forwardable = TRUE;
2208 iseq_calc_param_size(iseq);
2209 body->
param.size = arg_size;
2211 if (args->pre_init) {
2212 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2214 if (args->post_init) {
2215 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2218 if (body->type == ISEQ_TYPE_BLOCK) {
2219 if (body->
param.flags.has_opt == FALSE &&
2220 body->
param.flags.has_post == FALSE &&
2221 body->
param.flags.has_rest == FALSE &&
2222 body->
param.flags.has_kw == FALSE &&
2223 body->
param.flags.has_kwrest == FALSE) {
2225 if (body->
param.lead_num == 1 && last_comma == 0) {
2227 body->
param.flags.ambiguous_param0 = TRUE;
2239 unsigned int size = tbl ? tbl->size : 0;
2240 unsigned int offset = 0;
2243 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2248 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2257 MEMCPY(ids, tbl->ids + offset,
ID, size);
2258 ISEQ_BODY(iseq)->local_table = ids;
2260 enum lvar_state *states =
ALLOC_N(
enum lvar_state, size);
2262 for (
unsigned int i=0; i<size; i++) {
2263 states[i] = lvar_uninitialized;
2266 ISEQ_BODY(iseq)->lvar_states = states;
2268 ISEQ_BODY(iseq)->local_table_size = size;
2270 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2282 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2285 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2288 else if (tlit != tval) {
2298 long x =
FIX2LONG(rb_big_cmp(lit, val));
2306 return rb_float_cmp(lit, val);
2309 const struct RRational *rat1 = RRATIONAL(val);
2310 const struct RRational *rat2 = RRATIONAL(lit);
2311 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2314 const struct RComplex *comp1 = RCOMPLEX(val);
2315 const struct RComplex *comp2 = RCOMPLEX(lit);
2316 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2319 return rb_reg_equal(val, lit) ? 0 : -1;
2327rb_iseq_cdhash_hash(
VALUE a)
2329 switch (OBJ_BUILTIN_TYPE(a)) {
2332 return (st_index_t)a;
2340 return rb_rational_hash(a);
2342 return rb_complex_hash(a);
2352 rb_iseq_cdhash_hash,
2366 rb_hash_aset(data->hash, key,
INT2FIX(lobj->position - (data->pos+data->len)));
2374 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2381 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2383 if (rb_id_table_lookup(tbl,
id,&val)) {
2388 tbl = rb_id_table_create(1);
2389 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2391 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2392 rb_id_table_insert(tbl,
id,val);
2396#define BADINSN_DUMP(anchor, list, dest) \
2397 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2399#define BADINSN_ERROR \
2400 (xfree(generated_iseq), \
2401 xfree(insns_info), \
2402 BADINSN_DUMP(anchor, list, NULL), \
2408 int stack_max = 0, sp = 0, line = 0;
2411 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2412 if (IS_LABEL(list)) {
2418 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2419 switch (list->type) {
2420 case ISEQ_ELEMENT_INSN:
2428 sp = calc_sp_depth(sp, iobj);
2430 BADINSN_DUMP(anchor, list, NULL);
2431 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2432 "argument stack underflow (%d)", sp);
2435 if (sp > stack_max) {
2439 line = iobj->insn_info.line_no;
2441 operands = iobj->operands;
2442 insn = iobj->insn_id;
2443 types = insn_op_types(insn);
2444 len = insn_len(insn);
2447 if (iobj->operand_size !=
len - 1) {
2449 BADINSN_DUMP(anchor, list, NULL);
2450 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2451 "operand size miss! (%d for %d)",
2452 iobj->operand_size,
len - 1);
2456 for (j = 0; types[j]; j++) {
2457 if (types[j] == TS_OFFSET) {
2461 BADINSN_DUMP(anchor, list, NULL);
2462 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2463 "unknown label: "LABEL_FORMAT, lobj->label_no);
2466 if (lobj->sp == -1) {
2469 else if (lobj->sp != sp) {
2470 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2471 RSTRING_PTR(rb_iseq_path(iseq)), line,
2472 lobj->label_no, lobj->sp, sp);
2478 case ISEQ_ELEMENT_LABEL:
2481 if (lobj->sp == -1) {
2485 if (lobj->sp != sp) {
2486 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2487 RSTRING_PTR(rb_iseq_path(iseq)), line,
2488 lobj->label_no, lobj->sp, sp);
2494 case ISEQ_ELEMENT_TRACE:
2499 case ISEQ_ELEMENT_ADJUST:
2504 sp = adjust->label ? adjust->label->sp : 0;
2505 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2506 BADINSN_DUMP(anchor, list, NULL);
2507 COMPILE_ERROR(iseq, adjust->line_no,
2508 "iseq_set_sequence: adjust bug %d < %d",
2515 BADINSN_DUMP(anchor, list, NULL);
2516 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2525 int insns_info_index,
int code_index,
const INSN *iobj)
2527 if (insns_info_index == 0 ||
2528 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2529#ifdef USE_ISEQ_NODE_ID
2530 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2532 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2533 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2534#ifdef USE_ISEQ_NODE_ID
2535 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2537 insns_info[insns_info_index].events = iobj->insn_info.events;
2538 positions[insns_info_index] = code_index;
2546 int insns_info_index,
int code_index,
const ADJUST *adjust)
2548 insns_info[insns_info_index].line_no = adjust->line_no;
2549 insns_info[insns_info_index].node_id = -1;
2550 insns_info[insns_info_index].events = 0;
2551 positions[insns_info_index] = code_index;
2556array_to_idlist(
VALUE arr)
2561 for (
long i = 0; i < size; i++) {
2570idlist_to_array(
const ID *ids)
2587 unsigned int *positions;
2589 VALUE *generated_iseq;
2593 int insn_num, code_index, insns_info_index, sp = 0;
2594 int stack_max = fix_sp_depth(iseq, anchor);
2596 if (stack_max < 0)
return COMPILE_NG;
2599 insn_num = code_index = 0;
2600 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2601 switch (list->type) {
2602 case ISEQ_ELEMENT_INSN:
2606 sp = calc_sp_depth(sp, iobj);
2608 events = iobj->insn_info.events |= events;
2609 if (ISEQ_COVERAGE(iseq)) {
2610 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2611 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2612 int line = iobj->insn_info.line_no - 1;
2613 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2617 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2618 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2624 code_index += insn_data_length(iobj);
2629 case ISEQ_ELEMENT_LABEL:
2632 lobj->position = code_index;
2633 if (lobj->sp != sp) {
2634 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2635 RSTRING_PTR(rb_iseq_path(iseq)),
2636 lobj->label_no, lobj->sp, sp);
2641 case ISEQ_ELEMENT_TRACE:
2644 events |= trace->event;
2645 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2648 case ISEQ_ELEMENT_ADJUST:
2651 if (adjust->line_no != -1) {
2653 sp = adjust->label ? adjust->label->sp : 0;
2654 if (orig_sp - sp > 0) {
2655 if (orig_sp - sp > 1) code_index++;
2669 positions =
ALLOC_N(
unsigned int, insn_num);
2670 if (ISEQ_IS_SIZE(body)) {
2674 body->is_entries = NULL;
2677 if (body->ci_size) {
2681 body->call_data = NULL;
2683 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2690 iseq_bits_t * mark_offset_bits;
2691 int code_size = code_index;
2693 bool needs_bitmap =
false;
2695 if (ISEQ_MBITS_BUFLEN(code_index) == 1) {
2696 mark_offset_bits = &ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2697 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
true;
2700 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(code_index));
2701 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = mark_offset_bits;
2702 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
false;
2705 ISEQ_COMPILE_DATA(iseq)->iseq_encoded = (
void *)generated_iseq;
2706 ISEQ_COMPILE_DATA(iseq)->iseq_size = code_index;
2708 list = FIRST_ELEMENT(anchor);
2709 insns_info_index = code_index = sp = 0;
2712 switch (list->type) {
2713 case ISEQ_ELEMENT_INSN:
2721 sp = calc_sp_depth(sp, iobj);
2723 operands = iobj->operands;
2724 insn = iobj->insn_id;
2725 generated_iseq[code_index] = insn;
2726 types = insn_op_types(insn);
2727 len = insn_len(insn);
2729 for (j = 0; types[j]; j++) {
2730 char type = types[j];
2738 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2743 VALUE map = operands[j];
2746 data.pos = code_index;
2750 rb_hash_rehash(map);
2751 freeze_hide_obj(map);
2752 generated_iseq[code_index + 1 + j] = map;
2753 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2755 needs_bitmap =
true;
2760 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2765 VALUE v = operands[j];
2766 generated_iseq[code_index + 1 + j] = v;
2770 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2771 needs_bitmap =
true;
2778 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2779 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2780 if (UNLIKELY(ic_index >= body->ic_size)) {
2781 BADINSN_DUMP(anchor, &iobj->link, 0);
2782 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2783 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2784 ic_index, ISEQ_IS_SIZE(body));
2787 ic->
segments = array_to_idlist(operands[j]);
2789 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2794 unsigned int ic_index =
FIX2UINT(operands[j]);
2796 IVC cache = ((
IVC)&body->is_entries[ic_index]);
2798 if (insn == BIN(setinstancevariable)) {
2799 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2802 cache->iv_set_name = 0;
2805 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
2810 unsigned int ic_index =
FIX2UINT(operands[j]);
2811 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2812 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2813 BADINSN_DUMP(anchor, &iobj->link, 0);
2814 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2815 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2816 ic_index, ISEQ_IS_SIZE(body));
2818 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2825 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2826 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2828 cd->cc = vm_cc_empty();
2829 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2833 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2836 generated_iseq[code_index + 1 + j] = operands[j];
2839 generated_iseq[code_index + 1 + j] = operands[j];
2842 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2843 "unknown operand type: %c",
type);
2847 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2851 case ISEQ_ELEMENT_LABEL:
2854 if (lobj->sp != sp) {
2855 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2856 RSTRING_PTR(rb_iseq_path(iseq)),
2857 lobj->label_no, lobj->sp, sp);
2862 case ISEQ_ELEMENT_ADJUST:
2867 if (adjust->label) {
2868 sp = adjust->label->sp;
2874 if (adjust->line_no != -1) {
2875 const int diff = orig_sp - sp;
2877 if (insns_info_index == 0) {
2878 COMPILE_ERROR(iseq, adjust->line_no,
2879 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2881 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2884 generated_iseq[code_index++] = BIN(adjuststack);
2885 generated_iseq[code_index++] = orig_sp - sp;
2887 else if (diff == 1) {
2888 generated_iseq[code_index++] = BIN(pop);
2890 else if (diff < 0) {
2891 int label_no = adjust->label ? adjust->label->label_no : -1;
2892 xfree(generated_iseq);
2895 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
2896 xfree(mark_offset_bits);
2898 debug_list(anchor, list);
2899 COMPILE_ERROR(iseq, adjust->line_no,
2900 "iseq_set_sequence: adjust bug to %d %d < %d",
2901 label_no, orig_sp, sp);
2914 body->iseq_encoded = (
void *)generated_iseq;
2915 body->iseq_size = code_index;
2916 body->stack_max = stack_max;
2918 if (ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit) {
2919 body->mark_bits.single = ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2923 body->mark_bits.list = mark_offset_bits;
2926 body->mark_bits.list = NULL;
2927 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = NULL;
2928 ruby_xfree(mark_offset_bits);
2933 body->insns_info.body = insns_info;
2934 body->insns_info.positions = positions;
2937 body->insns_info.body = insns_info;
2938 REALLOC_N(positions,
unsigned int, insns_info_index);
2939 body->insns_info.positions = positions;
2940 body->insns_info.size = insns_info_index;
2946label_get_position(
LABEL *lobj)
2948 return lobj->position;
2952label_get_sp(
LABEL *lobj)
2958iseq_set_exception_table(
rb_iseq_t *iseq)
2960 const VALUE *tptr, *ptr;
2961 unsigned int tlen, i;
2964 ISEQ_BODY(iseq)->catch_table = NULL;
2966 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
2967 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
2975 for (i = 0; i < table->size; i++) {
2978 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
2979 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
2980 pos = label_get_position((
LABEL *)(ptr[1] & ~1));
2982 entry->start = (
unsigned int)pos;
2983 pos = label_get_position((
LABEL *)(ptr[2] & ~1));
2985 entry->end = (
unsigned int)pos;
2992 entry->cont = label_get_position(lobj);
2993 entry->sp = label_get_sp(lobj);
2996 if (entry->type == CATCH_TYPE_RESCUE ||
2997 entry->type == CATCH_TYPE_BREAK ||
2998 entry->type == CATCH_TYPE_NEXT) {
3007 ISEQ_BODY(iseq)->catch_table = table;
3008 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
3029 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
3031 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
3032 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
3033 opt_table[i] = label_get_position((
LABEL *)opt_table[i]);
3040get_destination_insn(
INSN *iobj)
3046 list = lobj->link.next;
3048 switch (list->type) {
3049 case ISEQ_ELEMENT_INSN:
3050 case ISEQ_ELEMENT_ADJUST:
3052 case ISEQ_ELEMENT_LABEL:
3055 case ISEQ_ELEMENT_TRACE:
3058 events |= trace->event;
3066 if (list && IS_INSN(list)) {
3068 iobj->insn_info.events |= events;
3074get_next_insn(
INSN *iobj)
3079 if (IS_INSN(list) || IS_ADJUST(list)) {
3088get_prev_insn(
INSN *iobj)
3093 if (IS_INSN(list) || IS_ADJUST(list)) {
3102unref_destination(
INSN *iobj,
int pos)
3104 LABEL *lobj = (
LABEL *)OPERAND_AT(iobj, pos);
3106 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3110replace_destination(
INSN *dobj,
INSN *nobj)
3112 VALUE n = OPERAND_AT(nobj, 0);
3115 if (dl == nl)
return false;
3118 OPERAND_AT(dobj, 0) = n;
3119 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3124find_destination(
INSN *i)
3126 int pos,
len = insn_len(i->insn_id);
3127 for (pos = 0; pos <
len; ++pos) {
3128 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3129 return (
LABEL *)OPERAND_AT(i, pos);
3139 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
3142 unref_counts =
ALLOCA_N(
int, nlabels);
3143 MEMZERO(unref_counts,
int, nlabels);
3148 if (IS_INSN_ID(i, leave)) {
3152 else if ((lab = find_destination((
INSN *)i)) != 0) {
3153 unref_counts[lab->label_no]++;
3156 else if (IS_LABEL(i)) {
3158 if (lab->unremovable)
return 0;
3159 if (lab->refcnt > unref_counts[lab->label_no]) {
3160 if (i == first)
return 0;
3165 else if (IS_TRACE(i)) {
3168 else if (IS_ADJUST(i)) {
3172 }
while ((i = i->next) != 0);
3177 VALUE insn = INSN_OF(i);
3178 int pos,
len = insn_len(insn);
3179 for (pos = 0; pos <
len; ++pos) {
3180 switch (insn_op_types(insn)[pos]) {
3182 unref_destination((
INSN *)i, pos);
3191 }
while ((i != end) && (i = i->next) != 0);
3198 switch (OPERAND_AT(iobj, 0)) {
3200 ELEM_REMOVE(&iobj->link);
3203 ELEM_REMOVE(&iobj->link);
3206 iobj->insn_id = BIN(adjuststack);
3212is_frozen_putstring(
INSN *insn,
VALUE *op)
3214 if (IS_INSN_ID(insn, putstring) || IS_INSN_ID(insn, putchilledstring)) {
3215 *op = OPERAND_AT(insn, 0);
3218 else if (IS_INSN_ID(insn, putobject)) {
3219 *op = OPERAND_AT(insn, 0);
3250 INSN *niobj, *ciobj, *dup = 0;
3254 switch (INSN_OF(iobj)) {
3255 case BIN(putstring):
3256 case BIN(putchilledstring):
3262 case BIN(putobject):
3265 default:
return FALSE;
3268 ciobj = (
INSN *)get_next_insn(iobj);
3269 if (IS_INSN_ID(ciobj, jump)) {
3270 ciobj = (
INSN *)get_next_insn((
INSN*)OPERAND_AT(ciobj, 0));
3272 if (IS_INSN_ID(ciobj, dup)) {
3273 ciobj = (
INSN *)get_next_insn(dup = ciobj);
3275 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3276 niobj = (
INSN *)get_next_insn(ciobj);
3281 switch (INSN_OF(niobj)) {
3283 if (OPERAND_AT(ciobj, 0) ==
type) {
3284 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3287 case BIN(branchunless):
3288 if (OPERAND_AT(ciobj, 0) !=
type) {
3289 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3295 line = ciobj->insn_info.line_no;
3296 node_id = ciobj->insn_info.node_id;
3298 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3299 dest = (
LABEL *)niobj->link.next;
3302 dest = NEW_LABEL(line);
3303 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3306 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3308 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3315 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3316 vm_ci_flag(ci) | add,
3326 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3334#define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3342 optimize_checktype(iseq, iobj);
3344 if (IS_INSN_ID(iobj, jump)) {
3345 INSN *niobj, *diobj, *piobj;
3346 diobj = (
INSN *)get_destination_insn(iobj);
3347 niobj = (
INSN *)get_next_insn(iobj);
3349 if (diobj == niobj) {
3356 unref_destination(iobj, 0);
3357 ELEM_REMOVE(&iobj->link);
3360 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3361 IS_INSN_ID(diobj, jump) &&
3362 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3363 diobj->insn_info.events == 0) {
3374 if (replace_destination(iobj, diobj)) {
3375 remove_unreachable_chunk(iseq, iobj->link.next);
3379 else if (IS_INSN_ID(diobj, leave)) {
3392 unref_destination(iobj, 0);
3393 iobj->insn_id = BIN(leave);
3394 iobj->operand_size = 0;
3395 iobj->insn_info = diobj->insn_info;
3398 else if (IS_INSN(iobj->link.prev) &&
3399 (piobj = (
INSN *)iobj->link.prev) &&
3400 (IS_INSN_ID(piobj, branchif) ||
3401 IS_INSN_ID(piobj, branchunless))) {
3402 INSN *pdiobj = (
INSN *)get_destination_insn(piobj);
3403 if (niobj == pdiobj) {
3404 int refcnt = IS_LABEL(piobj->link.next) ?
3405 ((
LABEL *)piobj->link.next)->refcnt : 0;
3420 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3421 ? BIN(branchunless) : BIN(branchif);
3422 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3423 ELEM_REMOVE(&iobj->link);
3430 else if (diobj == pdiobj) {
3444 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3445 ELEM_REPLACE(&piobj->link, &popiobj->link);
3448 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3462 if (IS_INSN_ID(iobj, newrange)) {
3463 INSN *
const range = iobj;
3465 VALUE str_beg, str_end;
3467 if ((end = (
INSN *)get_prev_insn(range)) != 0 &&
3468 is_frozen_putstring(end, &str_end) &&
3469 (beg = (
INSN *)get_prev_insn(end)) != 0 &&
3470 is_frozen_putstring(beg, &str_beg)) {
3471 int excl =
FIX2INT(OPERAND_AT(range, 0));
3474 ELEM_REMOVE(&beg->link);
3475 ELEM_REMOVE(&end->link);
3476 range->insn_id = BIN(putobject);
3477 OPERAND_AT(range, 0) = lit_range;
3482 if (IS_INSN_ID(iobj, leave)) {
3483 remove_unreachable_chunk(iseq, iobj->link.next);
3495 if (IS_INSN_ID(iobj, duparray)) {
3497 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3498 iobj->insn_id = BIN(putobject);
3508 if (IS_INSN_ID(iobj, duparray)) {
3510 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3514 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3515 VALUE ary = iobj->operands[0];
3518 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, ary, (
VALUE)ci);
3530 if (IS_INSN_ID(iobj, duphash)) {
3532 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3536 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3537 VALUE hash = iobj->operands[0];
3540 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, hash, (
VALUE)ci);
3552 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3554 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3558 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3559 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, rb_cArray_empty_frozen, (
VALUE)ci);
3571 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3573 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3577 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3578 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, rb_cHash_empty_frozen, (
VALUE)ci);
3584 if (IS_INSN_ID(iobj, branchif) ||
3585 IS_INSN_ID(iobj, branchnil) ||
3586 IS_INSN_ID(iobj, branchunless)) {
3595 INSN *nobj = (
INSN *)get_destination_insn(iobj);
3617 int stop_optimization =
3618 ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq) &&
3619 nobj->link.type == ISEQ_ELEMENT_INSN &&
3620 nobj->insn_info.events;
3621 if (!stop_optimization) {
3622 INSN *pobj = (
INSN *)iobj->link.prev;
3625 if (!IS_INSN(&pobj->link))
3627 else if (IS_INSN_ID(pobj, dup))
3632 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3633 if (!replace_destination(iobj, nobj))
break;
3635 else if (prev_dup && IS_INSN_ID(nobj, dup) &&
3636 !!(nobj = (
INSN *)nobj->link.next) &&
3638 nobj->insn_id == iobj->insn_id) {
3654 if (!replace_destination(iobj, nobj))
break;
3682 if (prev_dup && IS_INSN(pobj->link.prev)) {
3683 pobj = (
INSN *)pobj->link.prev;
3685 if (IS_INSN_ID(pobj, putobject)) {
3686 cond = (IS_INSN_ID(iobj, branchif) ?
3687 OPERAND_AT(pobj, 0) !=
Qfalse :
3688 IS_INSN_ID(iobj, branchunless) ?
3689 OPERAND_AT(pobj, 0) ==
Qfalse :
3692 else if (IS_INSN_ID(pobj, putstring) ||
3693 IS_INSN_ID(pobj, duparray) ||
3694 IS_INSN_ID(pobj, newarray)) {
3695 cond = IS_INSN_ID(iobj, branchif);
3697 else if (IS_INSN_ID(pobj, putnil)) {
3698 cond = !IS_INSN_ID(iobj, branchif);
3701 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3702 ELEM_REMOVE(iobj->link.prev);
3704 else if (!iseq_pop_newarray(iseq, pobj)) {
3705 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3706 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3710 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3711 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3713 iobj->insn_id = BIN(jump);
3717 unref_destination(iobj, 0);
3718 ELEM_REMOVE(&iobj->link);
3723 nobj = (
INSN *)get_destination_insn(nobj);
3728 if (IS_INSN_ID(iobj, pop)) {
3736 if (IS_INSN(prev)) {
3737 enum ruby_vminsn_type previ = ((
INSN *)prev)->insn_id;
3738 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3739 previ == BIN(putself) || previ == BIN(putstring) ||
3740 previ == BIN(putchilledstring) ||
3741 previ == BIN(dup) ||
3742 previ == BIN(getlocal) ||
3743 previ == BIN(getblockparam) ||
3744 previ == BIN(getblockparamproxy) ||
3745 previ == BIN(getinstancevariable) ||
3746 previ == BIN(duparray)) {
3750 ELEM_REMOVE(&iobj->link);
3752 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (
INSN*)prev)) {
3753 ELEM_REMOVE(&iobj->link);
3755 else if (previ == BIN(concatarray)) {
3757 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3758 INSN_OF(piobj) = BIN(pop);
3760 else if (previ == BIN(concatstrings)) {
3761 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3765 ELEM_REMOVE(&iobj->link);
3766 INSN_OF(prev) = BIN(adjuststack);
3772 if (IS_INSN_ID(iobj, newarray) ||
3773 IS_INSN_ID(iobj, duparray) ||
3774 IS_INSN_ID(iobj, concatarray) ||
3775 IS_INSN_ID(iobj, splatarray) ||
3785 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3791 if (IS_INSN_ID(iobj, newarray)) {
3793 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3794 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3796 op1 = OPERAND_AT(iobj, 0);
3797 op2 = OPERAND_AT(next, 0);
3808 INSN_OF(iobj) = BIN(swap);
3809 iobj->operand_size = 0;
3818 INSN_OF(iobj) = BIN(opt_reverse);
3823 INSN_OF(iobj) = BIN(opt_reverse);
3824 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3834 for (; diff > 0; diff--) {
3835 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
3846 for (; diff < 0; diff++) {
3847 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
3854 if (IS_INSN_ID(iobj, duparray)) {
3863 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
3864 INSN_OF(iobj) = BIN(putobject);
3868 if (IS_INSN_ID(iobj, anytostring)) {
3876 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
3877 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
3882 if (IS_INSN_ID(iobj, putstring) || IS_INSN_ID(iobj, putchilledstring) ||
3890 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
3891 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
3892 INSN *next = (
INSN *)iobj->link.next;
3893 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
3894 ELEM_REMOVE(&next->link);
3896 ELEM_REMOVE(&iobj->link);
3898 if (IS_NEXT_INSN_ID(&iobj->link, toregexp)) {
3899 INSN *next = (
INSN *)iobj->link.next;
3900 if (OPERAND_AT(next, 1) ==
INT2FIX(1)) {
3901 VALUE src = OPERAND_AT(iobj, 0);
3902 int opt = (int)
FIX2LONG(OPERAND_AT(next, 0));
3903 VALUE path = rb_iseq_path(iseq);
3904 int line = iobj->insn_info.line_no;
3905 VALUE errinfo = rb_errinfo();
3906 VALUE re = rb_reg_compile(src, opt, RSTRING_PTR(path), line);
3908 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
3909 rb_set_errinfo(errinfo);
3910 COMPILE_ERROR(iseq, line,
"%" PRIsVALUE, message);
3913 ELEM_REMOVE(iobj->link.next);
3918 if (IS_INSN_ID(iobj, concatstrings)) {
3927 if (IS_INSN(next) && IS_INSN_ID(next, jump))
3928 next = get_destination_insn(jump = (
INSN *)next);
3929 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
3930 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
3931 OPERAND_AT(iobj, 0) =
INT2FIX(n);
3933 LABEL *label = ((
LABEL *)OPERAND_AT(jump, 0));
3934 if (!--label->refcnt) {
3935 ELEM_REMOVE(&label->link);
3938 label = NEW_LABEL(0);
3939 OPERAND_AT(jump, 0) = (
VALUE)label;
3942 ELEM_INSERT_NEXT(next, &label->link);
3943 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
3951 if (do_tailcallopt &&
3952 (IS_INSN_ID(iobj, send) ||
3953 IS_INSN_ID(iobj, invokesuper))) {
3962 if (iobj->link.next) {
3965 if (!IS_INSN(next)) {
3969 switch (INSN_OF(next)) {
3978 next = get_destination_insn((
INSN *)next);
3992 if (IS_INSN_ID(piobj, send) ||
3993 IS_INSN_ID(piobj, invokesuper)) {
3994 if (OPERAND_AT(piobj, 1) == 0) {
3995 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3996 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4001 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4002 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4008 if (IS_INSN_ID(iobj, dup)) {
4009 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
4020 if (IS_NEXT_INSN_ID(set1, setlocal)) {
4022 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4023 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4025 ELEM_REMOVE(&iobj->link);
4038 else if (IS_NEXT_INSN_ID(set1, dup) &&
4039 IS_NEXT_INSN_ID(set1->next, setlocal)) {
4040 set2 = set1->next->next;
4041 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4042 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4043 ELEM_REMOVE(set1->next);
4057 if (IS_INSN_ID(iobj, getlocal)) {
4059 if (IS_NEXT_INSN_ID(niobj, dup)) {
4060 niobj = niobj->next;
4062 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
4064 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
4065 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
4081 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
4082 if (IS_TRACE(iobj->link.next)) {
4083 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
4084 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
4086 if (iobj == (
INSN *)list && bf->argc == 0 && (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF)) {
4087 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4100 if (IS_INSN_ID(iobj, getblockparam)) {
4101 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4102 iobj->insn_id = BIN(getblockparamproxy);
4106 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4108 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4109 niobj = niobj->next;
4111 unsigned int set_flags = 0, unset_flags = 0;
4124 if (IS_NEXT_INSN_ID(niobj, send)) {
4125 siobj = niobj->next;
4126 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4127 unset_flags = VM_CALL_ARGS_BLOCKARG;
4142 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4143 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4144 siobj = niobj->next->next;
4145 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4150 unsigned int flags = vm_ci_flag(ci);
4151 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4152 ((
INSN*)niobj)->insn_id = BIN(putobject);
4153 RB_OBJ_WRITE(iseq, &OPERAND_AT(niobj, 0), rb_hash_freeze(rb_hash_resurrect(OPERAND_AT(niobj, 0))));
4155 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4156 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4158 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4168insn_set_specialized_instruction(
rb_iseq_t *iseq,
INSN *iobj,
int insn_id)
4170 if (insn_id == BIN(opt_neq)) {
4171 VALUE original_ci = iobj->operands[0];
4172 VALUE new_ci = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4173 insn_replace_with_operands(iseq, iobj, insn_id, 2, new_ci, original_ci);
4176 iobj->insn_id = insn_id;
4177 iobj->operand_size = insn_len(insn_id) - 1;
4187 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4188 IS_INSN(iobj->link.next)) {
4192 INSN *niobj = (
INSN *)iobj->link.next;
4193 if (IS_INSN_ID(niobj, send)) {
4195 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4197 switch (vm_ci_mid(ci)) {
4199 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4202 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4205 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4210 VALUE num = iobj->operands[0];
4211 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, num, method);
4212 ELEM_REMOVE(&niobj->link);
4217 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4219 IS_NEXT_INSN_ID(&niobj->link, send)) {
4221 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4222 VALUE num = iobj->operands[0];
4223 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK));
4224 ELEM_REMOVE(&iobj->link);
4225 ELEM_REMOVE(niobj->link.next);
4226 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4232 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4234 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4235 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4238 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4239 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] ==
rb_id2sym(idBuffer))) {
4240 VALUE num = iobj->operands[0];
4241 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 2),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER));
4243 ELEM_REMOVE((niobj->link.next)->next);
4245 ELEM_REMOVE(&iobj->link);
4247 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4255 if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4256 IS_INSN_ID(niobj, putobject) ||
4257 IS_INSN_ID(niobj, putself) ||
4258 IS_INSN_ID(niobj, getlocal) ||
4259 IS_INSN_ID(niobj, getinstancevariable)) &&
4260 IS_NEXT_INSN_ID(&niobj->link, send)) {
4267 sendobj = sendobj->next;
4268 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4269 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4272 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4273 VALUE num = iobj->operands[0];
4275 insn_replace_with_operands(iseq, sendins, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P));
4277 ELEM_REMOVE(&iobj->link);
4291 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4292 INSN *niobj = (
INSN *)iobj->link.next;
4293 if ((IS_INSN_ID(niobj, getlocal) ||
4294 IS_INSN_ID(niobj, getinstancevariable) ||
4295 IS_INSN_ID(niobj, putself)) &&
4296 IS_NEXT_INSN_ID(&niobj->link, send)) {
4303 sendobj = sendobj->next;
4304 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4305 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4307 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4309 VALUE ary = iobj->operands[0];
4313 insn_replace_with_operands(iseq, sendins, BIN(opt_duparray_send), 3, ary,
rb_id2sym(idIncludeP),
INT2FIX(1));
4316 ELEM_REMOVE(&iobj->link);
4323 if (IS_INSN_ID(iobj, send)) {
4327#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4328 if (vm_ci_simple(ci)) {
4329 switch (vm_ci_argc(ci)) {
4331 switch (vm_ci_mid(ci)) {
4332 case idLength: SP_INSN(length);
return COMPILE_OK;
4333 case idSize: SP_INSN(size);
return COMPILE_OK;
4334 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4335 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4336 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4337 case idNot: SP_INSN(not);
return COMPILE_OK;
4341 switch (vm_ci_mid(ci)) {
4342 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4343 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4344 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4345 case idDIV: SP_INSN(div);
return COMPILE_OK;
4346 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4347 case idEq: SP_INSN(eq);
return COMPILE_OK;
4348 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4349 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4350 case idLT: SP_INSN(lt);
return COMPILE_OK;
4351 case idLE: SP_INSN(le);
return COMPILE_OK;
4352 case idGT: SP_INSN(gt);
return COMPILE_OK;
4353 case idGE: SP_INSN(ge);
return COMPILE_OK;
4354 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4355 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4356 case idAnd: SP_INSN(and);
return COMPILE_OK;
4357 case idOr: SP_INSN(or);
return COMPILE_OK;
4361 switch (vm_ci_mid(ci)) {
4362 case idASET: SP_INSN(aset);
return COMPILE_OK;
4368 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4369 iobj->insn_id = BIN(opt_send_without_block);
4370 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4381 switch (ISEQ_BODY(iseq)->
type) {
4383 case ISEQ_TYPE_EVAL:
4384 case ISEQ_TYPE_MAIN:
4386 case ISEQ_TYPE_RESCUE:
4387 case ISEQ_TYPE_ENSURE:
4399 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4400 const int do_tailcallopt = tailcallable_p(iseq) &&
4401 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4402 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4403 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4404 int rescue_level = 0;
4405 int tailcallopt = do_tailcallopt;
4407 list = FIRST_ELEMENT(anchor);
4409 int do_block_optimization = 0;
4410 LABEL * block_loop_label = NULL;
4413 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
4414 do_block_optimization = 1;
4419 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop) && IS_LABEL(le->next)) {
4420 block_loop_label = (
LABEL *)le->next;
4425 if (IS_INSN(list)) {
4426 if (do_peepholeopt) {
4427 iseq_peephole_optimize(iseq, list, tailcallopt);
4430 iseq_specialized_instruction(iseq, (
INSN *)list);
4433 insn_operands_unification((
INSN *)list);
4436 if (do_block_optimization) {
4439 if (IS_INSN_ID(item,
throw)) {
4440 do_block_optimization = 0;
4445 const char *types = insn_op_types(item->insn_id);
4446 for (
int j = 0; types[j]; j++) {
4447 if (types[j] == TS_OFFSET) {
4452 LABEL * target = (
LABEL *)OPERAND_AT(item, j);
4453 if (target == block_loop_label) {
4454 do_block_optimization = 0;
4461 if (IS_LABEL(list)) {
4462 switch (((
LABEL *)list)->rescued) {
4463 case LABEL_RESCUE_BEG:
4465 tailcallopt = FALSE;
4467 case LABEL_RESCUE_END:
4468 if (!--rescue_level) tailcallopt = do_tailcallopt;
4475 if (do_block_optimization) {
4477 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop)) {
4484#if OPT_INSTRUCTIONS_UNIFICATION
4492 VALUE *operands = 0, *ptr = 0;
4496 for (i = 0; i < size; i++) {
4497 iobj = (
INSN *)list;
4498 argc += iobj->operand_size;
4503 ptr = operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
4508 for (i = 0; i < size; i++) {
4509 iobj = (
INSN *)list;
4510 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4511 ptr += iobj->operand_size;
4515 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4527#if OPT_INSTRUCTIONS_UNIFICATION
4533 list = FIRST_ELEMENT(anchor);
4535 if (IS_INSN(list)) {
4536 iobj = (
INSN *)list;
4538 if (unified_insns_data[
id] != 0) {
4539 const int *
const *entry = unified_insns_data[id];
4540 for (j = 1; j < (intptr_t)entry[0]; j++) {
4541 const int *unified = entry[j];
4543 for (k = 2; k < unified[1]; k++) {
4545 ((
INSN *)li)->insn_id != unified[k]) {
4552 new_unified_insn(iseq, unified[0], unified[1] - 1,
4557 niobj->link.next = li;
4576all_string_result_p(
const NODE *node)
4578 if (!node)
return FALSE;
4579 switch (nd_type(node)) {
4580 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4582 case NODE_IF:
case NODE_UNLESS:
4583 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4584 if (all_string_result_p(RNODE_IF(node)->nd_body))
4585 return all_string_result_p(RNODE_IF(node)->nd_else);
4587 case NODE_AND:
case NODE_OR:
4588 if (!RNODE_AND(node)->nd_2nd)
4589 return all_string_result_p(RNODE_AND(node)->nd_1st);
4590 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4592 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4602 const NODE *lit_node;
4610 VALUE s = rb_str_new_mutable_parser_string(str);
4612 VALUE error = rb_reg_check_preprocess(s);
4613 if (!
NIL_P(error)) {
4614 COMPILE_ERROR(args->iseq, nd_line(node),
"%" PRIsVALUE, error);
4618 if (
NIL_P(args->lit)) {
4620 args->lit_node = node;
4629flush_dstr_fragment(
struct dstr_ctxt *args)
4631 if (!
NIL_P(args->lit)) {
4633 VALUE lit = args->lit;
4635 lit = rb_fstring(lit);
4636 ADD_INSN1(args->ret, args->lit_node, putobject, lit);
4643compile_dstr_fragments_0(
struct dstr_ctxt *args,
const NODE *
const node)
4645 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4649 CHECK(append_dstr_fragment(args, node, str));
4653 const NODE *
const head = list->nd_head;
4654 if (nd_type_p(head, NODE_STR)) {
4655 CHECK(append_dstr_fragment(args, node, RNODE_STR(head)->
string));
4657 else if (nd_type_p(head, NODE_DSTR)) {
4658 CHECK(compile_dstr_fragments_0(args, head));
4661 flush_dstr_fragment(args);
4663 CHECK(COMPILE(args->ret,
"each string", head));
4675 .iseq = iseq, .ret = ret,
4676 .lit =
Qnil, .lit_node = NULL,
4677 .cnt = 0, .dregx = dregx,
4679 CHECK(compile_dstr_fragments_0(&args, node));
4680 flush_dstr_fragment(&args);
4690 while (node && nd_type_p(node, NODE_BLOCK)) {
4691 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4692 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4693 node = RNODE_BLOCK(node)->nd_next;
4696 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4705 if (!RNODE_DSTR(node)->nd_next) {
4706 VALUE lit = rb_node_dstr_string_val(node);
4707 ADD_INSN1(ret, node, putstring, lit);
4711 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, FALSE));
4712 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4721 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
4723 if (!RNODE_DREGX(node)->nd_next) {
4725 VALUE src = rb_node_dregx_string_val(node);
4726 VALUE match = rb_reg_compile(src, cflag, NULL, 0);
4727 ADD_INSN1(ret, node, putobject, match);
4733 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, TRUE));
4737 ADD_INSN(ret, node, pop);
4747 const int line = nd_line(node);
4748 LABEL *lend = NEW_LABEL(line);
4749 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4750 + VM_SVAR_FLIPFLOP_START;
4753 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4754 ADD_INSNL(ret, node, branchif, lend);
4757 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
4758 ADD_INSNL(ret, node, branchunless, else_label);
4759 ADD_INSN1(ret, node, putobject,
Qtrue);
4760 ADD_INSN1(ret, node, setspecial, key);
4762 ADD_INSNL(ret, node, jump, then_label);
4766 ADD_LABEL(ret, lend);
4767 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
4768 ADD_INSNL(ret, node, branchunless, then_label);
4769 ADD_INSN1(ret, node, putobject,
Qfalse);
4770 ADD_INSN1(ret, node, setspecial, key);
4771 ADD_INSNL(ret, node, jump, then_label);
4780#define COMPILE_SINGLE 2
4787 LABEL *label = NEW_LABEL(nd_line(cond));
4788 if (!then_label) then_label = label;
4789 else if (!else_label) else_label = label;
4791 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
4793 if (LIST_INSN_SIZE_ONE(seq)) {
4794 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
4795 if (insn->insn_id == BIN(jump) && (
LABEL *)(insn->operands[0]) == label)
4798 if (!label->refcnt) {
4799 return COMPILE_SINGLE;
4801 ADD_LABEL(seq, label);
4811 DECL_ANCHOR(ignore);
4814 switch (nd_type(cond)) {
4816 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
4817 cond = RNODE_AND(cond)->nd_2nd;
4818 if (ok == COMPILE_SINGLE) {
4819 INIT_ANCHOR(ignore);
4821 then_label = NEW_LABEL(nd_line(cond));
4825 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
4826 cond = RNODE_OR(cond)->nd_2nd;
4827 if (ok == COMPILE_SINGLE) {
4828 INIT_ANCHOR(ignore);
4830 else_label = NEW_LABEL(nd_line(cond));
4840 case NODE_IMAGINARY:
4847 ADD_INSNL(ret, cond, jump, then_label);
4852 ADD_INSNL(ret, cond, jump, else_label);
4858 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
4859 ADD_INSNL(ret, cond, jump, then_label);
4862 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
4865 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
4868 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
4872 DECL_ANCHOR(cond_seq);
4873 INIT_ANCHOR(cond_seq);
4875 CHECK(COMPILE(cond_seq,
"branch condition", cond));
4877 if (LIST_INSN_SIZE_ONE(cond_seq)) {
4878 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
4879 if (insn->insn_id == BIN(putobject)) {
4880 if (
RTEST(insn->operands[0])) {
4881 ADD_INSNL(ret, cond, jump, then_label);
4886 ADD_INSNL(ret, cond, jump, else_label);
4891 ADD_SEQ(ret, cond_seq);
4896 ADD_INSNL(ret, cond, branchunless, else_label);
4897 ADD_INSNL(ret, cond, jump, then_label);
4904keyword_node_p(
const NODE *
const node)
4906 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
4912 switch (nd_type(node)) {
4914 return rb_node_sym_string_val(node);
4916 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
4923 NODE *node = node_hash->nd_head;
4924 VALUE hash = rb_hash_new();
4927 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4928 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
4929 VALUE idx = rb_hash_aref(hash, key);
4934 rb_hash_aset(hash, key,
INT2FIX(i));
4944 const NODE *
const root_node,
4952 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
4953 const NODE *node = RNODE_HASH(root_node)->nd_head;
4957 const NODE *key_node = RNODE_LIST(node)->nd_head;
4961 if (key_node && nd_type_p(key_node, NODE_SYM)) {
4966 *flag |= VM_CALL_KW_SPLAT;
4967 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4972 *flag |= VM_CALL_KW_SPLAT_MUT;
4977 node = RNODE_LIST(node)->nd_next;
4978 node = RNODE_LIST(node)->nd_next;
4982 node = RNODE_HASH(root_node)->nd_head;
4985 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
4988 VALUE *keywords = kw_arg->keywords;
4991 kw_arg->references = 0;
4992 kw_arg->keyword_len =
len;
4994 *kw_arg_ptr = kw_arg;
4996 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4997 const NODE *key_node = RNODE_LIST(node)->nd_head;
4998 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5001 keywords[j] = get_symbol_value(iseq, key_node);
5005 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
5019 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
5021 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
5024 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5025 *kwnode_ptr = RNODE_LIST(node)->nd_head;
5028 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
5029 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
5037frozen_string_literal_p(
const rb_iseq_t *iseq)
5039 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
5043static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
5045 switch (nd_type(node)) {
5053 case NODE_IMAGINARY:
5060 return hash_key || frozen_string_literal_p(iseq);
5069 switch (nd_type(node)) {
5071 return rb_node_integer_literal_val(node);
5073 return rb_node_float_literal_val(node);
5075 return rb_node_rational_literal_val(node);
5076 case NODE_IMAGINARY:
5077 return rb_node_imaginary_literal_val(node);
5085 return rb_node_sym_string_val(node);
5087 return rb_node_regx_string_val(node);
5089 return rb_node_line_lineno_val(node);
5091 return rb_node_encoding_val(node);
5094 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
5095 VALUE lit = get_string_value(node);
5096 return rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
5099 return get_string_value(node);
5102 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
5109 const NODE *line_node = node;
5111 if (nd_type_p(node, NODE_ZLIST)) {
5113 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5118 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5121 for (; node; node = RNODE_LIST(node)->nd_next) {
5122 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5164 const int max_stack_len = 0x100;
5165 const int min_tmp_ary_len = 0x40;
5169#define FLUSH_CHUNK \
5171 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5172 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5173 first_chunk = FALSE; \
5181 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5183 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5184 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5187 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5192 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5193 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5199 ADD_INSN1(ret, line_node, duparray, ary);
5200 first_chunk = FALSE;
5203 ADD_INSN1(ret, line_node, putobject, ary);
5204 ADD_INSN(ret, line_node, concattoarray);
5211 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5213 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5216 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5218 if (stack_len == 0 && first_chunk) {
5219 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5224 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5225 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5229 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5234 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5244static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5246 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);
5252 const NODE *line_node = node;
5254 node = RNODE_HASH(node)->nd_head;
5256 if (!node || nd_type_p(node, NODE_ZLIST)) {
5258 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5263 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5266 for (; node; node = RNODE_LIST(node)->nd_next) {
5267 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5290 const int max_stack_len = 0x100;
5291 const int min_tmp_hash_len = 0x800;
5293 int first_chunk = 1;
5294 DECL_ANCHOR(anchor);
5295 INIT_ANCHOR(anchor);
5298#define FLUSH_CHUNK() \
5300 if (first_chunk) { \
5301 APPEND_LIST(ret, anchor); \
5302 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5305 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5306 ADD_INSN(ret, line_node, swap); \
5307 APPEND_LIST(ret, anchor); \
5308 ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
5310 INIT_ANCHOR(anchor); \
5311 first_chunk = stack_len = 0; \
5318 if (static_literal_node_pair_p(node, iseq)) {
5320 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5321 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5324 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5329 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5331 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5332 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5343 ADD_INSN1(ret, line_node, duphash, hash);
5347 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5348 ADD_INSN(ret, line_node, swap);
5350 ADD_INSN1(ret, line_node, putobject, hash);
5352 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5359 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5362 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5365 if (RNODE_LIST(node)->nd_head) {
5367 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5368 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5372 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5378 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5379 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5380 int first_kw = first_chunk && stack_len == 0;
5381 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5382 int only_kw = last_kw && first_kw;
5384 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5386 if (only_kw && method_call_keywords) {
5394 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5396 else if (first_kw) {
5400 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5407 if (only_kw && method_call_keywords) {
5413 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5420 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5421 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5422 else ADD_INSN(ret, line_node, swap);
5424 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5426 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5441rb_node_case_when_optimizable_literal(
const NODE *
const node)
5443 switch (nd_type(node)) {
5445 return rb_node_integer_literal_val(node);
5447 VALUE v = rb_node_float_literal_val(node);
5456 case NODE_IMAGINARY:
5465 return rb_node_sym_string_val(node);
5467 return rb_node_line_lineno_val(node);
5469 return rb_node_str_string_val(node);
5471 return rb_node_file_path_val(node);
5478 LABEL *l1,
int only_special_literals,
VALUE literals)
5481 const NODE *val = RNODE_LIST(vals)->nd_head;
5482 VALUE lit = rb_node_case_when_optimizable_literal(val);
5485 only_special_literals = 0;
5487 else if (
NIL_P(rb_hash_lookup(literals, lit))) {
5488 rb_hash_aset(literals, lit, (
VALUE)(l1) | 1);
5491 if (nd_type_p(val, NODE_STR) || nd_type_p(val, NODE_FILE)) {
5492 debugp_param(
"nd_lit", get_string_value(val));
5493 lit = get_string_value(val);
5494 ADD_INSN1(cond_seq, val, putobject, lit);
5498 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5502 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5503 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5504 ADD_INSNL(cond_seq, val, branchif, l1);
5505 vals = RNODE_LIST(vals)->nd_next;
5507 return only_special_literals;
5512 LABEL *l1,
int only_special_literals,
VALUE literals)
5514 const NODE *line_node = vals;
5516 switch (nd_type(vals)) {
5518 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5522 ADD_INSN (cond_seq, line_node, dup);
5523 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5524 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5525 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5526 ADD_INSNL(cond_seq, line_node, branchif, l1);
5529 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5530 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5533 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5534 ADD_INSN (cond_seq, line_node, dup);
5535 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5536 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5537 ADD_INSNL(cond_seq, line_node, branchif, l1);
5540 ADD_INSN (cond_seq, line_node, dup);
5541 CHECK(COMPILE(cond_seq,
"when val", vals));
5542 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5543 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5544 ADD_INSNL(cond_seq, line_node, branchif, l1);
5637 const NODE *line_node;
5652add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc,
INSN *before_insn)
5655 rb_bug(
"no masgn_state");
5664 memo->before_insn = before_insn;
5665 memo->line_node = line_node;
5666 memo->argn = state->num_args + 1;
5667 memo->num_args = argc;
5668 state->num_args += argc;
5669 memo->lhs_pos = lhs_pos;
5671 if (!state->first_memo) {
5672 state->first_memo = memo;
5675 state->last_memo->next = memo;
5677 state->last_memo = memo;
5687 switch (nd_type(node)) {
5688 case NODE_ATTRASGN: {
5690 const NODE *line_node = node;
5692 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5694 bool safenav_call =
false;
5696 iobj = (
INSN *)get_prev_insn((
INSN *)insn_element);
5698 ELEM_REMOVE(insn_element);
5699 if (!IS_INSN_ID(iobj, send)) {
5700 safenav_call =
true;
5701 iobj = (
INSN *)get_prev_insn(iobj);
5702 ELEM_INSERT_NEXT(&iobj->link, insn_element);
5704 (pre->last = iobj->link.prev)->next = 0;
5707 int argc = vm_ci_argc(ci) + 1;
5708 ci = ci_argc_set(iseq, ci, argc);
5709 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5713 ADD_INSN(lhs, line_node, swap);
5716 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5719 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (
INSN *)LAST_ELEMENT(lhs))) {
5723 iobj->link.prev = lhs->last;
5724 lhs->last->next = &iobj->link;
5725 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
5726 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5727 int argc = vm_ci_argc(ci);
5728 bool dupsplat =
false;
5729 ci = ci_argc_set(iseq, ci, argc - 1);
5730 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
5737 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
5739 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5748 int line_no = nd_line(line_node);
5749 int node_id = nd_node_id(line_node);
5752 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5753 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
5754 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5756 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
5758 if (!safenav_call) {
5759 ADD_INSN(lhs, line_node, pop);
5761 ADD_INSN(lhs, line_node, pop);
5764 for (
int i=0; i < argc; i++) {
5765 ADD_INSN(post, line_node, pop);
5770 DECL_ANCHOR(nest_rhs);
5771 INIT_ANCHOR(nest_rhs);
5772 DECL_ANCHOR(nest_lhs);
5773 INIT_ANCHOR(nest_lhs);
5775 int prev_level = state->lhs_level;
5776 bool prev_nested = state->nested;
5778 state->lhs_level = lhs_pos - 1;
5779 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5780 state->lhs_level = prev_level;
5781 state->nested = prev_nested;
5783 ADD_SEQ(lhs, nest_rhs);
5784 ADD_SEQ(lhs, nest_lhs);
5788 if (!RNODE_CDECL(node)->nd_vid) {
5792 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5795 iobj = (
INSN *)insn_element;
5798 ELEM_REMOVE(insn_element);
5799 pre->last = iobj->link.prev;
5802 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (
INSN *)LAST_ELEMENT(lhs))) {
5806 ADD_INSN(post, node, pop);
5811 DECL_ANCHOR(anchor);
5812 INIT_ANCHOR(anchor);
5813 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
5814 ELEM_REMOVE(FIRST_ELEMENT(anchor));
5815 ADD_SEQ(lhs, anchor);
5826 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
5827 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
5834 const NODE *rhsn,
const NODE *orig_lhsn)
5837 const int memsize = numberof(mem);
5839 int llen = 0, rlen = 0;
5841 const NODE *lhsn = orig_lhsn;
5843#define MEMORY(v) { \
5845 if (memindex == memsize) return 0; \
5846 for (i=0; i<memindex; i++) { \
5847 if (mem[i] == (v)) return 0; \
5849 mem[memindex++] = (v); \
5852 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
5857 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
5858 switch (nd_type(ln)) {
5863 MEMORY(get_nd_vid(ln));
5868 lhsn = RNODE_LIST(lhsn)->nd_next;
5874 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
5877 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
5879 rhsn = RNODE_LIST(rhsn)->nd_next;
5884 for (i=0; i<llen-rlen; i++) {
5885 ADD_INSN(ret, orig_lhsn, putnil);
5889 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
5896 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
5897 const NODE *splatn = RNODE_MASGN(node)->nd_args;
5898 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
5899 const NODE *lhsn_count = lhsn;
5900 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
5905 while (lhsn_count) {
5907 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
5910 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
5912 lhsn = RNODE_LIST(lhsn)->nd_next;
5916 if (nd_type_p(splatn, NODE_POSTARG)) {
5918 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
5919 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
5920 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
5922 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
5924 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
5926 if (NODE_NAMED_REST_P(restn)) {
5927 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
5930 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
5932 postn = RNODE_LIST(postn)->nd_next;
5937 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
5941 if (!state->nested) {
5942 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
5946 ADD_INSN(rhs, node, dup);
5948 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
5955 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
5957 state.lhs_level = popped ? 0 : 1;
5960 state.first_memo = NULL;
5961 state.last_memo = NULL;
5971 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
5975 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
5976 for (
int i = 0; i < memo->num_args; i++) {
5977 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
5979 tmp_memo = memo->next;
5988 if (!popped && state.num_args >= 1) {
5990 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
6002 switch (nd_type(node)) {
6004 rb_ary_unshift(arr,
ID2SYM(RNODE_CONST(node)->nd_vid));
6007 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6008 rb_ary_unshift(arr,
ID2SYM(idNULL));
6011 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6012 node = RNODE_COLON2(node)->nd_head;
6021compile_const_prefix(
rb_iseq_t *iseq,
const NODE *
const node,
6024 switch (nd_type(node)) {
6026 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
6027 ADD_INSN1(body, node, putobject,
Qtrue);
6028 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
6031 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
6032 ADD_INSN(body, node, pop);
6033 ADD_INSN1(body, node, putobject, rb_cObject);
6034 ADD_INSN1(body, node, putobject,
Qtrue);
6035 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6038 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
6039 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
6040 ADD_INSN1(body, node, putobject,
Qfalse);
6041 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6044 CHECK(COMPILE(pref,
"const colon2 prefix", node));
6053 if (nd_type_p(cpath, NODE_COLON3)) {
6055 ADD_INSN1(ret, cpath, putobject, rb_cObject);
6056 return VM_DEFINECLASS_FLAG_SCOPED;
6058 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
6060 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
6061 return VM_DEFINECLASS_FLAG_SCOPED;
6065 ADD_INSN1(ret, cpath, putspecialobject,
6066 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
6072private_recv_p(
const NODE *node)
6074 NODE *recv = get_nd_recv(node);
6075 if (recv && nd_type_p(recv, NODE_SELF)) {
6076 return RNODE_SELF(recv)->nd_state != 0;
6083 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore);
6086compile_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);
6093 enum defined_type expr_type = DEFINED_NOT_DEFINED;
6094 enum node_type
type;
6095 const int line = nd_line(node);
6096 const NODE *line_node = node;
6098 switch (
type = nd_type(node)) {
6102 expr_type = DEFINED_NIL;
6105 expr_type = DEFINED_SELF;
6108 expr_type = DEFINED_TRUE;
6111 expr_type = DEFINED_FALSE;
6116 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
6120 if (RNODE_LIST(vals)->nd_head) {
6121 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
6124 lfinish[1] = NEW_LABEL(line);
6126 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6128 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6141 case NODE_IMAGINARY:
6146 expr_type = DEFINED_EXPR;
6150 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6152 lfinish[1] = NEW_LABEL(line);
6154 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6155 expr_type = DEFINED_EXPR;
6161 expr_type = DEFINED_LVAR;
6164#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6166 ADD_INSN3(ret, line_node, definedivar,
6167 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6171 ADD_INSN(ret, line_node, putnil);
6172 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6173 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6177 ADD_INSN(ret, line_node, putnil);
6178 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6179 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6183 ADD_INSN(ret, line_node, putnil);
6184 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6185 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6189 lfinish[1] = NEW_LABEL(line);
6191 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6192 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6193 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6196 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6197 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6200 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6201 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6205 ADD_INSN1(ret, line_node, putobject, rb_cObject);
6206 ADD_INSN3(ret, line_node, defined,
6207 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6215 case NODE_ATTRASGN:{
6216 const int explicit_receiver =
6217 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6218 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6220 if (get_nd_args(node) || explicit_receiver) {
6222 lfinish[1] = NEW_LABEL(line);
6225 lfinish[2] = NEW_LABEL(line);
6228 if (get_nd_args(node)) {
6229 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6230 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6232 if (explicit_receiver) {
6233 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6234 switch (nd_type(get_nd_recv(node))) {
6240 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6241 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6244 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6245 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6249 ADD_INSN(ret, line_node, dup);
6251 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6252 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6255 ADD_INSN(ret, line_node, putself);
6257 ADD_INSN(ret, line_node, dup);
6259 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6260 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6266 ADD_INSN(ret, line_node, putnil);
6267 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6268 PUSH_VAL(DEFINED_YIELD));
6269 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6274 ADD_INSN(ret, line_node, putnil);
6275 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6276 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6277 PUSH_VAL(DEFINED_GVAR));
6282 ADD_INSN(ret, line_node, putnil);
6283 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6284 PUSH_VAL(DEFINED_ZSUPER));
6290 case NODE_OP_ASGN_OR:
6291 case NODE_OP_ASGN_AND:
6300 expr_type = DEFINED_ASGN;
6307 VALUE str = rb_iseq_defined_string(expr_type);
6308 ADD_INSN1(ret, line_node, putobject, str);
6311 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6318 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6319 iseq_set_exception_local_table(iseq);
6324 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore)
6327 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6329 int line = nd_line(node);
6330 LABEL *lstart = NEW_LABEL(line);
6331 LABEL *lend = NEW_LABEL(line);
6334 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6335 rescue = NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc,
6337 ISEQ_BODY(iseq)->location.label),
6338 ISEQ_TYPE_RESCUE, 0);
6339 lstart->rescued = LABEL_RESCUE_BEG;
6340 lend->rescued = LABEL_RESCUE_END;
6341 APPEND_LABEL(ret, lcur, lstart);
6342 ADD_LABEL(ret, lend);
6344 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6352 const int line = nd_line(node);
6353 const NODE *line_node = node;
6354 if (!RNODE_DEFINED(node)->nd_head) {
6355 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6356 ADD_INSN1(ret, line_node, putobject, str);
6361 lfinish[0] = NEW_LABEL(line);
6364 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6366 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6367 ADD_INSN(ret, line_node, swap);
6369 ADD_LABEL(ret, lfinish[2]);
6371 ADD_INSN(ret, line_node, pop);
6372 ADD_LABEL(ret, lfinish[1]);
6374 ADD_LABEL(ret, lfinish[0]);
6380make_name_for_block(
const rb_iseq_t *orig_iseq)
6385 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6386 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6387 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6390 iseq = ISEQ_BODY(iseq)->parent_iseq;
6395 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6398 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6407 enl->ensure_node = node;
6408 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6410 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6420 while (erange->next != 0) {
6421 erange = erange->next;
6425 ne->end = erange->end;
6426 erange->end = lstart;
6432can_add_ensure_iseq(
const rb_iseq_t *iseq)
6435 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
6437 if (e->ensure_node)
return false;
6450 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6452 DECL_ANCHOR(ensure);
6454 INIT_ANCHOR(ensure);
6456 if (enlp->erange != NULL) {
6457 DECL_ANCHOR(ensure_part);
6458 LABEL *lstart = NEW_LABEL(0);
6459 LABEL *lend = NEW_LABEL(0);
6460 INIT_ANCHOR(ensure_part);
6462 add_ensure_range(iseq, enlp->erange, lstart, lend);
6464 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6465 ADD_LABEL(ensure_part, lstart);
6466 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6467 ADD_LABEL(ensure_part, lend);
6468 ADD_SEQ(ensure, ensure_part);
6477 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6478 ADD_SEQ(ret, ensure);
6483check_keyword(
const NODE *node)
6487 if (nd_type_p(node, NODE_LIST)) {
6488 while (RNODE_LIST(node)->nd_next) {
6489 node = RNODE_LIST(node)->nd_next;
6491 node = RNODE_LIST(node)->nd_head;
6494 return keyword_node_p(node);
6499keyword_node_single_splat_p(
NODE *kwnode)
6503 NODE *node = RNODE_HASH(kwnode)->nd_head;
6504 return RNODE_LIST(node)->nd_head == NULL &&
6505 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6510 NODE *kwnode,
unsigned int *flag_ptr)
6512 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6513 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6514 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6515 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6516 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6519#define SPLATARRAY_FALSE 0
6520#define SPLATARRAY_TRUE 1
6521#define DUP_SINGLE_KW_SPLAT 2
6525 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6527 if (!argn)
return 0;
6529 NODE *kwnode = NULL;
6531 switch (nd_type(argn)) {
6534 int len = compile_args(iseq, args, argn, &kwnode);
6535 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6538 if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6542 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6543 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6546 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6555 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6556 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6557 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6558 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6559 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6562 case NODE_ARGSCAT: {
6563 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6564 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6565 bool args_pushed =
false;
6567 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6568 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6569 if (kwnode) rest_len--;
6570 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6574 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6575 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6578 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6579 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6580 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6583 else if (!args_pushed) {
6584 ADD_INSN(args, argn, concattoarray);
6590 *flag_ptr |= VM_CALL_KW_SPLAT;
6591 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6597 case NODE_ARGSPUSH: {
6598 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6599 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6601 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6602 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6603 if (kwnode) rest_len--;
6604 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6605 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6608 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6609 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6612 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6613 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6619 *flag_ptr |= VM_CALL_KW_SPLAT;
6620 if (!keyword_node_single_splat_p(kwnode)) {
6621 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6622 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6624 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
6625 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6628 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6636 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
6642setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
6644 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
6645 *flag |= VM_CALL_ARGS_SPLAT_MUT;
6650setup_args_dup_rest_p(
const NODE *argn)
6652 switch(nd_type(argn)) {
6663 case NODE_IMAGINARY:
6676 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
6679 if (setup_args_dup_rest_p(RNODE_LIST(argn)->nd_head)) {
6682 argn = RNODE_LIST(argn)->nd_next;
6695 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
6698 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
6699 RNODE_BLOCK_PASS(argn)->nd_head : argn;
6702 switch(nd_type(check_arg)) {
6705 dup_rest = SPLATARRAY_FALSE;
6709 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
6711 case(NODE_ARGSPUSH):
6713 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
6714 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
6715 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
6716 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
6717 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
6719 if (dup_rest == SPLATARRAY_FALSE) {
6721 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
6723 NODE *key_node = RNODE_LIST(node)->nd_head;
6724 if (key_node && setup_args_dup_rest_p(key_node)) {
6725 dup_rest = SPLATARRAY_TRUE;
6729 node = RNODE_LIST(node)->nd_next;
6730 NODE *value_node = RNODE_LIST(node)->nd_head;
6731 if (setup_args_dup_rest_p(value_node)) {
6732 dup_rest = SPLATARRAY_TRUE;
6736 node = RNODE_LIST(node)->nd_next;
6745 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
6747 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
6750 initial_dup_rest = dup_rest;
6752 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
6753 DECL_ANCHOR(arg_block);
6754 INIT_ANCHOR(arg_block);
6756 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
6757 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
6759 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
6760 const NODE * arg_node =
6761 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
6768 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
6769 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
6772 *flag |= VM_CALL_FORWARDING;
6774 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
6775 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6779 *flag |= VM_CALL_ARGS_BLOCKARG;
6781 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
6784 if (LIST_INSN_SIZE_ONE(arg_block)) {
6786 if (IS_INSN(elem)) {
6788 if (iobj->insn_id == BIN(getblockparam)) {
6789 iobj->insn_id = BIN(getblockparamproxy);
6793 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
6794 ADD_SEQ(args, arg_block);
6797 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
6799 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6806 const NODE *body = ptr;
6807 int line = nd_line(body);
6809 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
6811 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6812 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
6814 iseq_set_local_table(iseq, 0, 0);
6822 int line = nd_line(node);
6823 const NODE *line_node = node;
6824 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
6826#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
6827 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
6831 ADD_INSN(ret, line_node, dup);
6832 ADD_INSNL(ret, line_node, branchunless, fail_label);
6834 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6836 if (RNODE_BLOCK(vars)->nd_next) {
6837 ADD_INSN(ret, line_node, dup);
6840 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6842 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
6845#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
6846 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
6851 ADD_INSNL(nom, line_node, jump, end_label);
6852 ADD_LABEL(nom, fail_label);
6854 ADD_INSN(nom, line_node, pop);
6855 ADD_INSN(nom, line_node, putnil);
6857 ADD_LABEL(nom, end_label);
6858 (nom->last->next = cap->link.next)->prev = nom->last;
6859 (cap->link.next = nom->anchor.next)->prev = &cap->link;
6864 ADD_INSNL(ret, line_node, jump, end_label);
6865 ADD_LABEL(ret, fail_label);
6866 ADD_INSN(ret, line_node, pop);
6867 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6869 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6871 ((
INSN*)last)->insn_id = BIN(putnil);
6872 ((
INSN*)last)->operand_size = 0;
6874 ADD_LABEL(ret, end_label);
6878optimizable_range_item_p(
const NODE *n)
6880 if (!n)
return FALSE;
6881 switch (nd_type(n)) {
6894optimized_range_item(
const NODE *n)
6896 switch (nd_type(n)) {
6898 return rb_node_line_lineno_val(n);
6900 return rb_node_integer_literal_val(n);
6902 return rb_node_float_literal_val(n);
6904 return rb_node_rational_literal_val(n);
6905 case NODE_IMAGINARY:
6906 return rb_node_imaginary_literal_val(n);
6910 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
6917 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
6918 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
6920 const int line = nd_line(node);
6921 const NODE *line_node = node;
6922 DECL_ANCHOR(cond_seq);
6923 LABEL *then_label, *else_label, *end_label;
6926 INIT_ANCHOR(cond_seq);
6927 then_label = NEW_LABEL(line);
6928 else_label = NEW_LABEL(line);
6931 NODE *cond = RNODE_IF(node)->nd_cond;
6932 if (nd_type(cond) == NODE_BLOCK) {
6933 cond = RNODE_BLOCK(cond)->nd_head;
6936 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
6937 ADD_SEQ(ret, cond_seq);
6939 if (then_label->refcnt && else_label->refcnt) {
6940 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
6943 if (then_label->refcnt) {
6944 ADD_LABEL(ret, then_label);
6946 DECL_ANCHOR(then_seq);
6947 INIT_ANCHOR(then_seq);
6948 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
6950 if (else_label->refcnt) {
6951 const NODE *
const coverage_node = node_body ? node_body : node;
6952 add_trace_branch_coverage(
6955 nd_code_loc(coverage_node),
6956 nd_node_id(coverage_node),
6958 type == NODE_IF ?
"then" :
"else",
6960 end_label = NEW_LABEL(line);
6961 ADD_INSNL(then_seq, line_node, jump, end_label);
6963 ADD_INSN(then_seq, line_node, pop);
6966 ADD_SEQ(ret, then_seq);
6969 if (else_label->refcnt) {
6970 ADD_LABEL(ret, else_label);
6972 DECL_ANCHOR(else_seq);
6973 INIT_ANCHOR(else_seq);
6974 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
6976 if (then_label->refcnt) {
6977 const NODE *
const coverage_node = node_else ? node_else : node;
6978 add_trace_branch_coverage(
6981 nd_code_loc(coverage_node),
6982 nd_node_id(coverage_node),
6984 type == NODE_IF ?
"else" :
"then",
6987 ADD_SEQ(ret, else_seq);
6991 ADD_LABEL(ret, end_label);
7001 const NODE *node = orig_node;
7002 LABEL *endlabel, *elselabel;
7004 DECL_ANCHOR(body_seq);
7005 DECL_ANCHOR(cond_seq);
7006 int only_special_literals = 1;
7007 VALUE literals = rb_hash_new();
7009 enum node_type
type;
7010 const NODE *line_node;
7015 INIT_ANCHOR(body_seq);
7016 INIT_ANCHOR(cond_seq);
7018 RHASH_TBL_RAW(literals)->type = &cdhash_type;
7020 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
7022 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
7024 node = RNODE_CASE(node)->nd_body;
7025 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
7026 type = nd_type(node);
7027 line = nd_line(node);
7030 endlabel = NEW_LABEL(line);
7031 elselabel = NEW_LABEL(line);
7035 while (
type == NODE_WHEN) {
7038 l1 = NEW_LABEL(line);
7039 ADD_LABEL(body_seq, l1);
7040 ADD_INSN(body_seq, line_node, pop);
7042 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7043 add_trace_branch_coverage(
7046 nd_code_loc(coverage_node),
7047 nd_node_id(coverage_node),
7052 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
7053 ADD_INSNL(body_seq, line_node, jump, endlabel);
7055 vals = RNODE_WHEN(node)->nd_head;
7057 switch (nd_type(vals)) {
7059 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
7060 if (only_special_literals < 0)
return COMPILE_NG;
7065 only_special_literals = 0;
7066 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
7069 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
7073 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
7076 node = RNODE_WHEN(node)->nd_next;
7080 type = nd_type(node);
7081 line = nd_line(node);
7086 ADD_LABEL(cond_seq, elselabel);
7087 ADD_INSN(cond_seq, line_node, pop);
7088 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
7089 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7090 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7093 debugs(
"== else (implicit)\n");
7094 ADD_LABEL(cond_seq, elselabel);
7095 ADD_INSN(cond_seq, orig_node, pop);
7096 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
7098 ADD_INSN(cond_seq, orig_node, putnil);
7100 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7103 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7104 ADD_INSN(ret, orig_node, dup);
7105 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
7107 LABEL_REF(elselabel);
7110 ADD_SEQ(ret, cond_seq);
7111 ADD_SEQ(ret, body_seq);
7112 ADD_LABEL(ret, endlabel);
7121 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
7123 DECL_ANCHOR(body_seq);
7127 branches = decl_branch_base(iseq, PTR2NUM(orig_node), nd_code_loc(orig_node),
"case");
7129 INIT_ANCHOR(body_seq);
7130 endlabel = NEW_LABEL(nd_line(node));
7132 while (node && nd_type_p(node, NODE_WHEN)) {
7133 const int line = nd_line(node);
7134 LABEL *l1 = NEW_LABEL(line);
7135 ADD_LABEL(body_seq, l1);
7137 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7138 add_trace_branch_coverage(
7141 nd_code_loc(coverage_node),
7142 nd_node_id(coverage_node),
7147 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7148 ADD_INSNL(body_seq, node, jump, endlabel);
7150 vals = RNODE_WHEN(node)->nd_head;
7152 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7154 switch (nd_type(vals)) {
7158 val = RNODE_LIST(vals)->nd_head;
7159 lnext = NEW_LABEL(nd_line(val));
7160 debug_compile(
"== when2\n", (
void)0);
7161 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7162 ADD_LABEL(ret, lnext);
7163 vals = RNODE_LIST(vals)->nd_next;
7169 ADD_INSN(ret, vals, putnil);
7170 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7171 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7172 ADD_INSNL(ret, vals, branchif, l1);
7175 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7177 node = RNODE_WHEN(node)->nd_next;
7180 const NODE *
const coverage_node = node ? node : orig_node;
7181 add_trace_branch_coverage(
7184 nd_code_loc(coverage_node),
7185 nd_node_id(coverage_node),
7189 CHECK(COMPILE_(ret,
"else", node, popped));
7190 ADD_INSNL(ret, orig_node, jump, endlabel);
7192 ADD_SEQ(ret, body_seq);
7193 ADD_LABEL(ret, endlabel);
7197static 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);
7199static 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);
7200static 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);
7201static int iseq_compile_pattern_set_general_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7202static 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);
7203static int iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7205#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7206#define CASE3_BI_OFFSET_ERROR_STRING 1
7207#define CASE3_BI_OFFSET_KEY_ERROR_P 2
7208#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7209#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7212iseq_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)
7214 const int line = nd_line(node);
7215 const NODE *line_node = node;
7217 switch (nd_type(node)) {
7271 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7272 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7273 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7275 const int min_argc = pre_args_num + post_args_num;
7276 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7277 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7279 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7281 match_failed = NEW_LABEL(line);
7282 type_error = NEW_LABEL(line);
7283 deconstruct = NEW_LABEL(line);
7284 deconstructed = NEW_LABEL(line);
7287 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7288 ADD_INSN(ret, line_node, swap);
7294 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7296 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7298 ADD_INSN(ret, line_node, dup);
7299 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7300 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7301 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7302 if (in_single_pattern) {
7303 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7304 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7305 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7306 INT2FIX(min_argc), base_index + 1 ));
7308 ADD_INSNL(ret, line_node, branchunless, match_failed);
7310 for (i = 0; i < pre_args_num; i++) {
7311 ADD_INSN(ret, line_node, dup);
7312 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7313 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7314 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7315 args = RNODE_LIST(args)->nd_next;
7318 if (RNODE_ARYPTN(node)->rest_arg) {
7319 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7320 ADD_INSN(ret, line_node, dup);
7321 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7322 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7323 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7324 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7325 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7326 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7327 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7329 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7332 if (post_args_num > 0) {
7333 ADD_INSN(ret, line_node, dup);
7334 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7335 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7336 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7337 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7338 ADD_INSN(ret, line_node, pop);
7343 args = RNODE_ARYPTN(node)->post_args;
7344 for (i = 0; i < post_args_num; i++) {
7345 ADD_INSN(ret, line_node, dup);
7347 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7348 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7349 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7351 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7352 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7353 args = RNODE_LIST(args)->nd_next;
7356 ADD_INSN(ret, line_node, pop);
7358 ADD_INSN(ret, line_node, pop);
7360 ADD_INSNL(ret, line_node, jump, matched);
7361 ADD_INSN(ret, line_node, putnil);
7363 ADD_INSN(ret, line_node, putnil);
7366 ADD_LABEL(ret, type_error);
7367 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7369 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7370 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7371 ADD_INSN(ret, line_node, pop);
7373 ADD_LABEL(ret, match_failed);
7374 ADD_INSN(ret, line_node, pop);
7376 ADD_INSN(ret, line_node, pop);
7378 ADD_INSNL(ret, line_node, jump, unmatched);
7431 const NODE *args = RNODE_FNDPTN(node)->args;
7432 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7434 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7435 match_failed = NEW_LABEL(line);
7436 type_error = NEW_LABEL(line);
7437 deconstruct = NEW_LABEL(line);
7438 deconstructed = NEW_LABEL(line);
7440 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7442 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7444 ADD_INSN(ret, line_node, dup);
7445 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7446 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7447 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7448 if (in_single_pattern) {
7449 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 ));
7451 ADD_INSNL(ret, line_node, branchunless, match_failed);
7454 LABEL *while_begin = NEW_LABEL(nd_line(node));
7455 LABEL *next_loop = NEW_LABEL(nd_line(node));
7456 LABEL *find_succeeded = NEW_LABEL(line);
7457 LABEL *find_failed = NEW_LABEL(nd_line(node));
7460 ADD_INSN(ret, line_node, dup);
7461 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7463 ADD_INSN(ret, line_node, dup);
7464 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7465 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7467 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7469 ADD_LABEL(ret, while_begin);
7471 ADD_INSN(ret, line_node, dup);
7472 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7473 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7474 ADD_INSNL(ret, line_node, branchunless, find_failed);
7476 for (j = 0; j < args_num; j++) {
7477 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7478 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7480 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7481 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7483 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7485 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7486 args = RNODE_LIST(args)->nd_next;
7489 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7490 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7491 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7492 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7493 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7494 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));
7496 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7497 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7498 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7499 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7500 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7501 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7502 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7503 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));
7505 ADD_INSNL(ret, line_node, jump, find_succeeded);
7507 ADD_LABEL(ret, next_loop);
7508 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7509 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7510 ADD_INSNL(ret, line_node, jump, while_begin);
7512 ADD_LABEL(ret, find_failed);
7513 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7514 if (in_single_pattern) {
7515 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7516 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7517 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7518 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7519 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7521 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7522 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7524 ADD_INSN(ret, line_node, pop);
7525 ADD_INSN(ret, line_node, pop);
7527 ADD_INSNL(ret, line_node, jump, match_failed);
7528 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7530 ADD_LABEL(ret, find_succeeded);
7531 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7534 ADD_INSN(ret, line_node, pop);
7535 ADD_INSNL(ret, line_node, jump, matched);
7536 ADD_INSN(ret, line_node, putnil);
7538 ADD_LABEL(ret, type_error);
7539 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7541 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7542 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7543 ADD_INSN(ret, line_node, pop);
7545 ADD_LABEL(ret, match_failed);
7546 ADD_INSN(ret, line_node, pop);
7547 ADD_INSNL(ret, line_node, jump, unmatched);
7611 LABEL *match_failed, *type_error;
7614 match_failed = NEW_LABEL(line);
7615 type_error = NEW_LABEL(line);
7617 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
7618 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7619 keys =
rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
7621 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
7622 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
7626 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7628 ADD_INSN(ret, line_node, dup);
7629 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
7630 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7631 if (in_single_pattern) {
7632 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
7634 ADD_INSNL(ret, line_node, branchunless, match_failed);
7637 ADD_INSN(ret, line_node, putnil);
7640 ADD_INSN1(ret, line_node, duparray, keys);
7643 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
7645 ADD_INSN(ret, line_node, dup);
7647 ADD_INSNL(ret, line_node, branchunless, type_error);
7649 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7650 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
7653 if (RNODE_HSHPTN(node)->nd_pkwargs) {
7657 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7659 DECL_ANCHOR(match_values);
7660 INIT_ANCHOR(match_values);
7661 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
7662 for (i = 0; i < keys_num; i++) {
7663 NODE *key_node = RNODE_LIST(args)->nd_head;
7664 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
7665 VALUE key = get_symbol_value(iseq, key_node);
7667 ADD_INSN(ret, line_node, dup);
7668 ADD_INSN1(ret, line_node, putobject, key);
7669 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
7670 if (in_single_pattern) {
7671 LABEL *match_succeeded;
7672 match_succeeded = NEW_LABEL(line);
7674 ADD_INSN(ret, line_node, dup);
7675 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7677 ADD_INSN1(ret, line_node, putobject,
rb_str_freeze(rb_sprintf(
"key not found: %+"PRIsVALUE, key)));
7678 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
7679 ADD_INSN1(ret, line_node, putobject,
Qtrue);
7680 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
7681 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7682 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
7683 ADD_INSN1(ret, line_node, putobject, key);
7684 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
7686 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
7688 ADD_LABEL(ret, match_succeeded);
7690 ADD_INSNL(ret, line_node, branchunless, match_failed);
7692 ADD_INSN(match_values, line_node, dup);
7693 ADD_INSN1(match_values, line_node, putobject, key);
7694 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
7695 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7696 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
7698 ADD_SEQ(ret, match_values);
7702 ADD_INSN(ret, line_node, dup);
7703 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7704 if (in_single_pattern) {
7705 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
7707 ADD_INSNL(ret, line_node, branchunless, match_failed);
7710 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7711 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
7712 ADD_INSN(ret, line_node, dup);
7713 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7714 if (in_single_pattern) {
7715 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
7717 ADD_INSNL(ret, line_node, branchunless, match_failed);
7720 ADD_INSN(ret, line_node, dup);
7721 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7725 ADD_INSN(ret, line_node, pop);
7726 ADD_INSNL(ret, line_node, jump, matched);
7727 ADD_INSN(ret, line_node, putnil);
7729 ADD_LABEL(ret, type_error);
7730 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7732 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
7733 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7734 ADD_INSN(ret, line_node, pop);
7736 ADD_LABEL(ret, match_failed);
7737 ADD_INSN(ret, line_node, pop);
7738 ADD_INSNL(ret, line_node, jump, unmatched);
7747 case NODE_IMAGINARY:
7775 CHECK(COMPILE(ret,
"case in literal", node));
7776 if (in_single_pattern) {
7777 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7779 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7780 if (in_single_pattern) {
7781 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
7783 ADD_INSNL(ret, line_node, branchif, matched);
7784 ADD_INSNL(ret, line_node, jump, unmatched);
7788 ID id = RNODE_LASGN(node)->nd_vid;
7789 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
7791 if (in_alt_pattern) {
7792 const char *name = rb_id2name(
id);
7793 if (name && strlen(name) > 0 && name[0] !=
'_') {
7794 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7800 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
7801 ADD_INSNL(ret, line_node, jump, matched);
7806 ID id = RNODE_DASGN(node)->nd_vid;
7808 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
7810 if (in_alt_pattern) {
7811 const char *name = rb_id2name(
id);
7812 if (name && strlen(name) > 0 && name[0] !=
'_') {
7813 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7820 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
7824 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
7825 ADD_INSNL(ret, line_node, jump, matched);
7830 LABEL *match_failed;
7831 match_failed = unmatched;
7832 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7833 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
7834 if (in_single_pattern) {
7835 LABEL *match_succeeded;
7836 match_succeeded = NEW_LABEL(line);
7838 ADD_INSN(ret, line_node, dup);
7839 if (nd_type_p(node, NODE_IF)) {
7840 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7843 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
7846 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
7847 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7848 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7849 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7851 ADD_INSN(ret, line_node, pop);
7852 ADD_INSN(ret, line_node, pop);
7854 ADD_LABEL(ret, match_succeeded);
7856 if (nd_type_p(node, NODE_IF)) {
7857 ADD_INSNL(ret, line_node, branchunless, match_failed);
7860 ADD_INSNL(ret, line_node, branchif, match_failed);
7862 ADD_INSNL(ret, line_node, jump, matched);
7867 LABEL *match_failed;
7868 match_failed = NEW_LABEL(line);
7870 n = RNODE_HASH(node)->nd_head;
7871 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
7872 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7876 ADD_INSN(ret, line_node, dup);
7877 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));
7878 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));
7879 ADD_INSN(ret, line_node, putnil);
7881 ADD_LABEL(ret, match_failed);
7882 ADD_INSN(ret, line_node, pop);
7883 ADD_INSNL(ret, line_node, jump, unmatched);
7887 LABEL *match_succeeded, *fin;
7888 match_succeeded = NEW_LABEL(line);
7889 fin = NEW_LABEL(line);
7891 ADD_INSN(ret, line_node, dup);
7892 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));
7893 ADD_LABEL(ret, match_succeeded);
7894 ADD_INSN(ret, line_node, pop);
7895 ADD_INSNL(ret, line_node, jump, matched);
7896 ADD_INSN(ret, line_node, putnil);
7897 ADD_LABEL(ret, fin);
7898 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
7902 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
7908iseq_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)
7910 LABEL *fin = NEW_LABEL(nd_line(node));
7911 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7912 ADD_LABEL(ret, fin);
7917iseq_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)
7919 const NODE *line_node = node;
7921 if (RNODE_ARYPTN(node)->nd_pconst) {
7922 ADD_INSN(ret, line_node, dup);
7923 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
7924 if (in_single_pattern) {
7925 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7927 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7928 if (in_single_pattern) {
7929 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
7931 ADD_INSNL(ret, line_node, branchunless, match_failed);
7938iseq_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)
7940 const NODE *line_node = node;
7944 if (use_deconstructed_cache) {
7946 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7947 ADD_INSNL(ret, line_node, branchnil, deconstruct);
7950 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7951 ADD_INSNL(ret, line_node, branchunless, match_failed);
7954 ADD_INSN(ret, line_node, pop);
7955 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
7956 ADD_INSNL(ret, line_node, jump, deconstructed);
7959 ADD_INSNL(ret, line_node, jump, deconstruct);
7962 ADD_LABEL(ret, deconstruct);
7963 ADD_INSN(ret, line_node, dup);
7964 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
7965 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7968 if (use_deconstructed_cache) {
7969 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
7972 if (in_single_pattern) {
7973 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
7976 ADD_INSNL(ret, line_node, branchunless, match_failed);
7978 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
7981 if (use_deconstructed_cache) {
7982 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7985 ADD_INSN(ret, line_node, dup);
7987 ADD_INSNL(ret, line_node, branchunless, type_error);
7989 ADD_LABEL(ret, deconstructed);
8005 const int line = nd_line(node);
8006 const NODE *line_node = node;
8007 LABEL *match_succeeded = NEW_LABEL(line);
8009 ADD_INSN(ret, line_node, dup);
8010 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8012 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8013 ADD_INSN1(ret, line_node, putobject, errmsg);
8014 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8015 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
8016 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8018 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8019 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8021 ADD_INSN(ret, line_node, pop);
8022 ADD_INSN(ret, line_node, pop);
8023 ADD_LABEL(ret, match_succeeded);
8039 const int line = nd_line(node);
8040 const NODE *line_node = node;
8041 LABEL *match_succeeded = NEW_LABEL(line);
8043 ADD_INSN(ret, line_node, dup);
8044 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8046 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8047 ADD_INSN1(ret, line_node, putobject, errmsg);
8048 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8049 ADD_INSN(ret, line_node, dup);
8050 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
8051 ADD_INSN1(ret, line_node, putobject, pattern_length);
8052 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
8053 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8055 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8056 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8058 ADD_INSN(ret, line_node, pop);
8059 ADD_INSN(ret, line_node, pop);
8060 ADD_LABEL(ret, match_succeeded);
8066iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
8076 const int line = nd_line(node);
8077 const NODE *line_node = node;
8078 LABEL *match_succeeded = NEW_LABEL(line);
8080 ADD_INSN(ret, line_node, dup);
8081 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8083 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8084 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
8085 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8086 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
8087 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
8088 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8090 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8091 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8093 ADD_INSN(ret, line_node, pop);
8094 ADD_INSN(ret, line_node, pop);
8096 ADD_LABEL(ret, match_succeeded);
8097 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
8098 ADD_INSN(ret, line_node, pop);
8099 ADD_INSN(ret, line_node, pop);
8107 const NODE *pattern;
8108 const NODE *node = orig_node;
8109 LABEL *endlabel, *elselabel;
8111 DECL_ANCHOR(body_seq);
8112 DECL_ANCHOR(cond_seq);
8114 enum node_type
type;
8115 const NODE *line_node;
8118 bool single_pattern;
8121 INIT_ANCHOR(body_seq);
8122 INIT_ANCHOR(cond_seq);
8124 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
8126 node = RNODE_CASE3(node)->nd_body;
8127 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
8128 type = nd_type(node);
8129 line = nd_line(node);
8131 single_pattern = !RNODE_IN(node)->nd_next;
8133 endlabel = NEW_LABEL(line);
8134 elselabel = NEW_LABEL(line);
8136 if (single_pattern) {
8138 ADD_INSN(head, line_node, putnil);
8139 ADD_INSN(head, line_node, putnil);
8140 ADD_INSN1(head, line_node, putobject,
Qfalse);
8141 ADD_INSN(head, line_node, putnil);
8143 ADD_INSN(head, line_node, putnil);
8145 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8149 while (
type == NODE_IN) {
8153 ADD_INSN(body_seq, line_node, putnil);
8155 l1 = NEW_LABEL(line);
8156 ADD_LABEL(body_seq, l1);
8157 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8159 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8160 add_trace_branch_coverage(
8163 nd_code_loc(coverage_node),
8164 nd_node_id(coverage_node),
8169 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8170 ADD_INSNL(body_seq, line_node, jump, endlabel);
8172 pattern = RNODE_IN(node)->nd_head;
8174 int pat_line = nd_line(pattern);
8175 LABEL *next_pat = NEW_LABEL(pat_line);
8176 ADD_INSN (cond_seq, pattern, dup);
8178 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8179 ADD_LABEL(cond_seq, next_pat);
8180 LABEL_UNREMOVABLE(next_pat);
8183 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8187 node = RNODE_IN(node)->nd_next;
8191 type = nd_type(node);
8192 line = nd_line(node);
8197 ADD_LABEL(cond_seq, elselabel);
8198 ADD_INSN(cond_seq, line_node, pop);
8199 ADD_INSN(cond_seq, line_node, pop);
8200 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8201 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8202 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8203 ADD_INSN(cond_seq, line_node, putnil);
8205 ADD_INSN(cond_seq, line_node, putnil);
8209 debugs(
"== else (implicit)\n");
8210 ADD_LABEL(cond_seq, elselabel);
8211 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8212 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8214 if (single_pattern) {
8222 LABEL *key_error, *fin;
8225 key_error = NEW_LABEL(line);
8226 fin = NEW_LABEL(line);
8229 kw_arg->references = 0;
8230 kw_arg->keyword_len = 2;
8231 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
8232 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
8234 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8235 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8237 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8238 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8239 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8240 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8241 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8242 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8243 ADD_INSNL(cond_seq, orig_node, jump, fin);
8245 ADD_LABEL(cond_seq, key_error);
8247 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8248 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8249 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8250 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8251 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8252 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8253 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8254 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
8255 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8257 ADD_LABEL(cond_seq, fin);
8261 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8262 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8264 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8266 ADD_INSN(cond_seq, orig_node, putnil);
8268 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8269 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8271 ADD_INSN(cond_seq, line_node, putnil);
8275 ADD_SEQ(ret, cond_seq);
8276 ADD_SEQ(ret, body_seq);
8277 ADD_LABEL(ret, endlabel);
8281#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8282#undef CASE3_BI_OFFSET_ERROR_STRING
8283#undef CASE3_BI_OFFSET_KEY_ERROR_P
8284#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8285#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8290 const int line = (int)nd_line(node);
8291 const NODE *line_node = node;
8293 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8294 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8295 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8296 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8301 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8302 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8303 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8304 LABEL *end_label = NEW_LABEL(line);
8305 LABEL *adjust_label = NEW_LABEL(line);
8307 LABEL *next_catch_label = NEW_LABEL(line);
8308 LABEL *tmp_label = NULL;
8310 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8311 push_ensure_entry(iseq, &enl, NULL, NULL);
8313 if (RNODE_WHILE(node)->nd_state == 1) {
8314 ADD_INSNL(ret, line_node, jump, next_label);
8317 tmp_label = NEW_LABEL(line);
8318 ADD_INSNL(ret, line_node, jump, tmp_label);
8320 ADD_LABEL(ret, adjust_label);
8321 ADD_INSN(ret, line_node, putnil);
8322 ADD_LABEL(ret, next_catch_label);
8323 ADD_INSN(ret, line_node, pop);
8324 ADD_INSNL(ret, line_node, jump, next_label);
8325 if (tmp_label) ADD_LABEL(ret, tmp_label);
8327 ADD_LABEL(ret, redo_label);
8328 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8330 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8331 add_trace_branch_coverage(
8334 nd_code_loc(coverage_node),
8335 nd_node_id(coverage_node),
8340 CHECK(COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body));
8341 ADD_LABEL(ret, next_label);
8343 if (
type == NODE_WHILE) {
8344 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8345 redo_label, end_label));
8349 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8350 end_label, redo_label));
8353 ADD_LABEL(ret, end_label);
8354 ADD_ADJUST_RESTORE(ret, adjust_label);
8356 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8358 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8362 ADD_INSN(ret, line_node, putnil);
8365 ADD_LABEL(ret, break_label);
8368 ADD_INSN(ret, line_node, pop);
8371 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8373 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8375 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8376 ISEQ_COMPILE_DATA(iseq)->redo_label);
8378 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8379 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8380 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8381 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8382 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
8389 const int line = nd_line(node);
8390 const NODE *line_node = node;
8391 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8392 LABEL *retry_label = NEW_LABEL(line);
8393 LABEL *retry_end_l = NEW_LABEL(line);
8396 ADD_LABEL(ret, retry_label);
8397 if (nd_type_p(node, NODE_FOR)) {
8398 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8400 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8401 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8402 ISEQ_TYPE_BLOCK, line);
8403 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8406 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8407 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8408 ISEQ_TYPE_BLOCK, line);
8409 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8423 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
8424 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8425 iobj = (
INSN*) get_prev_insn(iobj);
8427 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
8431 if (&iobj->link == LAST_ELEMENT(ret)) {
8437 ADD_INSN(ret, line_node, pop);
8440 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8442 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8452 const NODE *line_node = node;
8453 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8454 LABEL *not_single = NEW_LABEL(nd_line(var));
8455 LABEL *not_ary = NEW_LABEL(nd_line(var));
8456 CHECK(COMPILE(ret,
"for var", var));
8457 ADD_INSN(ret, line_node, dup);
8458 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8459 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8460 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8461 ADD_INSNL(ret, line_node, branchunless, not_single);
8462 ADD_INSN(ret, line_node, dup);
8463 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8464 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8465 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8466 ADD_INSN(ret, line_node, swap);
8467 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
8468 ADD_INSN(ret, line_node, dup);
8469 ADD_INSNL(ret, line_node, branchunless, not_ary);
8470 ADD_INSN(ret, line_node, swap);
8471 ADD_LABEL(ret, not_ary);
8472 ADD_INSN(ret, line_node, pop);
8473 ADD_LABEL(ret, not_single);
8480 const NODE *line_node = node;
8481 unsigned long throw_flag = 0;
8483 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8485 LABEL *splabel = NEW_LABEL(0);
8486 ADD_LABEL(ret, splabel);
8487 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8488 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8489 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8490 add_ensure_iseq(ret, iseq, 0);
8491 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8492 ADD_ADJUST_RESTORE(ret, splabel);
8495 ADD_INSN(ret, line_node, putnil);
8502 if (!ISEQ_COMPILE_DATA(ip)) {
8507 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8508 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8510 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8513 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8514 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8518 ip = ISEQ_BODY(ip)->parent_iseq;
8523 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8524 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8526 ADD_INSN(ret, line_node, pop);
8530 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8539 const NODE *line_node = node;
8540 unsigned long throw_flag = 0;
8542 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8543 LABEL *splabel = NEW_LABEL(0);
8544 debugs(
"next in while loop\n");
8545 ADD_LABEL(ret, splabel);
8546 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8547 add_ensure_iseq(ret, iseq, 0);
8548 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8549 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8550 ADD_ADJUST_RESTORE(ret, splabel);
8552 ADD_INSN(ret, line_node, putnil);
8555 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8556 LABEL *splabel = NEW_LABEL(0);
8557 debugs(
"next in block\n");
8558 ADD_LABEL(ret, splabel);
8559 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8560 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8561 add_ensure_iseq(ret, iseq, 0);
8562 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8563 ADD_ADJUST_RESTORE(ret, splabel);
8566 ADD_INSN(ret, line_node, putnil);
8573 if (!ISEQ_COMPILE_DATA(ip)) {
8578 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8579 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8583 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8586 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8587 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8591 ip = ISEQ_BODY(ip)->parent_iseq;
8594 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8595 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8598 ADD_INSN(ret, line_node, pop);
8602 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
8612 const NODE *line_node = node;
8614 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8615 LABEL *splabel = NEW_LABEL(0);
8616 debugs(
"redo in while");
8617 ADD_LABEL(ret, splabel);
8618 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8619 add_ensure_iseq(ret, iseq, 0);
8620 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8621 ADD_ADJUST_RESTORE(ret, splabel);
8623 ADD_INSN(ret, line_node, putnil);
8626 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8627 LABEL *splabel = NEW_LABEL(0);
8629 debugs(
"redo in block");
8630 ADD_LABEL(ret, splabel);
8631 add_ensure_iseq(ret, iseq, 0);
8632 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8633 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8634 ADD_ADJUST_RESTORE(ret, splabel);
8637 ADD_INSN(ret, line_node, putnil);
8644 if (!ISEQ_COMPILE_DATA(ip)) {
8649 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8652 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8655 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8656 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
8660 ip = ISEQ_BODY(ip)->parent_iseq;
8663 ADD_INSN(ret, line_node, putnil);
8664 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8667 ADD_INSN(ret, line_node, pop);
8671 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
8681 const NODE *line_node = node;
8683 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
8684 ADD_INSN(ret, line_node, putnil);
8685 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
8688 ADD_INSN(ret, line_node, pop);
8692 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
8701 const int line = nd_line(node);
8702 const NODE *line_node = node;
8703 LABEL *lstart = NEW_LABEL(line);
8704 LABEL *lend = NEW_LABEL(line);
8705 LABEL *lcont = NEW_LABEL(line);
8706 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
8708 ISEQ_BODY(iseq)->location.label),
8709 ISEQ_TYPE_RESCUE, line);
8711 lstart->rescued = LABEL_RESCUE_BEG;
8712 lend->rescued = LABEL_RESCUE_END;
8713 ADD_LABEL(ret, lstart);
8715 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
8716 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
8718 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
8720 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
8722 ADD_LABEL(ret, lend);
8723 if (RNODE_RESCUE(node)->nd_else) {
8724 ADD_INSN(ret, line_node, pop);
8725 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
8727 ADD_INSN(ret, line_node, nop);
8728 ADD_LABEL(ret, lcont);
8731 ADD_INSN(ret, line_node, pop);
8735 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
8736 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
8743 const int line = nd_line(node);
8744 const NODE *line_node = node;
8745 const NODE *resq = node;
8747 LABEL *label_miss, *label_hit;
8750 label_miss = NEW_LABEL(line);
8751 label_hit = NEW_LABEL(line);
8753 narg = RNODE_RESBODY(resq)->nd_args;
8755 switch (nd_type(narg)) {
8758 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8759 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
8760 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8761 ADD_INSNL(ret, line_node, branchif, label_hit);
8762 narg = RNODE_LIST(narg)->nd_next;
8768 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8769 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
8770 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
8771 ADD_INSNL(ret, line_node, branchif, label_hit);
8774 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
8778 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8780 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8781 ADD_INSNL(ret, line_node, branchif, label_hit);
8783 ADD_INSNL(ret, line_node, jump, label_miss);
8784 ADD_LABEL(ret, label_hit);
8787 if (RNODE_RESBODY(resq)->nd_exc_var) {
8788 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
8791 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) {
8793 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
8796 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
8799 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
8800 ADD_INSN(ret, line_node, nop);
8802 ADD_INSN(ret, line_node, leave);
8803 ADD_LABEL(ret, label_miss);
8804 resq = RNODE_RESBODY(resq)->nd_next;
8812 const int line = nd_line(RNODE_ENSURE(node)->nd_ensr);
8813 const NODE *line_node = node;
8815 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
8817 ISEQ_TYPE_ENSURE, line);
8818 LABEL *lstart = NEW_LABEL(line);
8819 LABEL *lend = NEW_LABEL(line);
8820 LABEL *lcont = NEW_LABEL(line);
8828 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
8830 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
8835 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
8837 ADD_LABEL(ret, lstart);
8838 CHECK(COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
8839 ADD_LABEL(ret, lend);
8841 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
8842 ADD_LABEL(ret, lcont);
8843 if (last_leave) ADD_INSN(ret, line_node, pop);
8845 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
8846 if (lstart->link.next != &lend->link) {
8848 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
8850 erange = erange->next;
8854 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
8861 const NODE *line_node = node;
8864 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8866 enum rb_iseq_type t =
type;
8867 const NODE *retval = RNODE_RETURN(node)->nd_stts;
8870 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
8871 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
8872 t = ISEQ_BODY(is)->type;
8876 case ISEQ_TYPE_MAIN:
8878 rb_warn(
"argument of top-level return is ignored");
8882 type = ISEQ_TYPE_METHOD;
8889 if (
type == ISEQ_TYPE_METHOD) {
8890 splabel = NEW_LABEL(0);
8891 ADD_LABEL(ret, splabel);
8892 ADD_ADJUST(ret, line_node, 0);
8895 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
8897 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8898 add_ensure_iseq(ret, iseq, 1);
8900 ADD_INSN(ret, line_node, leave);
8901 ADD_ADJUST_RESTORE(ret, splabel);
8904 ADD_INSN(ret, line_node, putnil);
8908 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
8910 ADD_INSN(ret, line_node, pop);
8921 if (!i)
return false;
8922 if (IS_TRACE(i)) i = i->prev;
8923 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
8925 if (IS_ADJUST(i)) i = i->prev;
8926 if (!IS_INSN(i))
return false;
8927 switch (INSN_OF(i)) {
8934 (ret->last = last->prev)->next = NULL;
8941 CHECK(COMPILE_(ret,
"nd_body", node, popped));
8943 if (!popped && !all_string_result_p(node)) {
8944 const NODE *line_node = node;
8945 const unsigned int flag = VM_CALL_FCALL;
8949 ADD_INSN(ret, line_node, dup);
8950 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
8951 ADD_INSN(ret, line_node, anytostring);
8959 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8961 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
8962 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8968 LABEL *else_label = NEW_LABEL(nd_line(line_node));
8971 br = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"&.");
8973 ADD_INSN(recv, line_node, dup);
8974 ADD_INSNL(recv, line_node, branchnil, else_label);
8975 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
8983 if (!else_label)
return;
8984 end_label = NEW_LABEL(nd_line(line_node));
8985 ADD_INSNL(ret, line_node, jump, end_label);
8986 ADD_LABEL(ret, else_label);
8987 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
8988 ADD_LABEL(ret, end_label);
8997 if (get_nd_recv(node) &&
8998 (nd_type_p(get_nd_recv(node), NODE_STR) || nd_type_p(get_nd_recv(node), NODE_FILE)) &&
8999 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
9000 get_nd_args(node) == NULL &&
9001 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9002 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
9003 VALUE str = get_string_value(get_nd_recv(node));
9004 if (get_node_call_nd_mid(node) == idUMinus) {
9005 ADD_INSN2(ret, line_node, opt_str_uminus, str,
9006 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
9009 ADD_INSN2(ret, line_node, opt_str_freeze, str,
9010 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
9014 ADD_INSN(ret, line_node, pop);
9022iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
9024 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
9028iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
9031 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
9032 for (i=0; table[i].index != -1; i++) {
9033 if (strcmp(table[i].name, name) == 0) {
9041iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
9043 const char *name = rb_id2name(mid);
9044 static const char prefix[] =
"__builtin_";
9045 const size_t prefix_len =
sizeof(prefix) - 1;
9050 switch (nd_type(recv)) {
9052 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
9057 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
9067 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
9068 return &name[prefix_len];
9077delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
9084 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
9085 unsigned int start=0;
9090 argc + start <= ISEQ_BODY(iseq)->local_table_size;
9094 for (
unsigned int i=start; i-start<argc; i++) {
9095 if (IS_INSN(elem) &&
9096 INSN_OF(elem) == BIN(getlocal)) {
9097 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
9098 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
9100 if (local_level == 0) {
9101 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
9103 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
9104 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
9105 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
9106 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9130 *pstart_index = start;
9144 if (!node)
goto no_arg;
9146 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9147 const NODE *next = RNODE_LIST(node)->nd_next;
9149 node = RNODE_LIST(node)->nd_head;
9150 if (!node)
goto no_arg;
9151 switch (nd_type(node)) {
9153 symbol = rb_node_sym_string_val(node);
9159 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9162 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
9163 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9165 else if (strcmp(RSTRING_PTR(
string),
"inline_block") == 0) {
9166 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9168 else if (strcmp(RSTRING_PTR(
string),
"use_block") == 0) {
9169 iseq_set_use_block(iseq);
9171 else if (strcmp(RSTRING_PTR(
string),
"c_trace") == 0) {
9173 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9182 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9185 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9188 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %s", RSTRING_PTR(
string));
9191 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9199 if (!node)
goto no_arg;
9200 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9201 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9202 node = RNODE_LIST(node)->nd_head;
9203 if (!node)
goto no_arg;
9204 switch (nd_type(node)) {
9206 name = rb_node_sym_string_val(node);
9211 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9213 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9217 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9220 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9223 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9224 rb_builtin_class_name(name));
9227 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9233 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9234 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9235 return RNODE_IF(node)->nd_body;
9238 rb_bug(
"mandatory_node: can't find mandatory node");
9243compile_builtin_mandatory_only_method(
rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9247 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9250 rb_node_init(RNODE(&args_node), NODE_ARGS);
9251 args_node.nd_ainfo = args;
9254 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9255 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9259 tbl->size = table_size;
9264 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9265 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9268 for (; i<table_size; i++) {
9269 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9273 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9274 scope_node.nd_tbl = tbl;
9275 scope_node.nd_body = mandatory_node(iseq, node);
9276 scope_node.nd_parent = NULL;
9277 scope_node.nd_args = &args_node;
9279 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9282 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9283 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9284 nd_line(line_node), NULL, 0,
9285 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9286 ISEQ_BODY(iseq)->variable.script_lines);
9287 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
9297 NODE *args_node = get_nd_args(node);
9299 if (parent_block != NULL) {
9300 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9304# define BUILTIN_INLINE_PREFIX "_bi"
9305 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9306 bool cconst =
false;
9311 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9312 strcmp(
"cexpr!", builtin_func) == 0) {
9315 else if (strcmp(
"cconst!", builtin_func) == 0) {
9318 else if (strcmp(
"cinit!", builtin_func) == 0) {
9322 else if (strcmp(
"attr!", builtin_func) == 0) {
9323 return compile_builtin_attr(iseq, args_node);
9325 else if (strcmp(
"arg!", builtin_func) == 0) {
9326 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9328 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9330 rb_bug(
"mandatory_only? should be in if condition");
9332 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9333 rb_bug(
"mandatory_only? should be put on top");
9336 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9337 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9340 rb_bug(
"can't find builtin function:%s", builtin_func);
9343 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9347 int inline_index = nd_line(node);
9348 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9349 builtin_func = inline_func;
9355 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9356 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9357 ADD_INSN1(ret, line_node, putobject, const_val);
9363 unsigned int flag = 0;
9365 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9367 if (
FIX2INT(argc) != bf->argc) {
9368 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9369 builtin_func, bf->argc,
FIX2INT(argc));
9373 unsigned int start_index;
9374 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9375 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9379 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9382 if (popped) ADD_INSN(ret, line_node, pop);
9388compile_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)
9396 ID mid = get_node_call_nd_mid(node);
9398 unsigned int flag = 0;
9400 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9401 LABEL *else_label = NULL;
9404 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9410 if (nd_type_p(node, NODE_VCALL)) {
9415 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9417 if (mid == id_bitblt) {
9418 ADD_INSN(ret, line_node, bitblt);
9421 else if (mid == id_answer) {
9422 ADD_INSN(ret, line_node, answer);
9434 if (nd_type_p(node, NODE_FCALL) &&
9435 (mid == goto_id || mid == label_id)) {
9438 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9441 if (!labels_table) {
9442 labels_table = st_init_numtable();
9443 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9446 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9450 if (mid == goto_id) {
9451 ADD_INSNL(ret, line_node, jump, label);
9454 ADD_LABEL(ret, label);
9461 const char *builtin_func;
9462 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9463 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9464 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9468 if (!assume_receiver) {
9469 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9472 if (mid == idCall &&
9473 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9474 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9475 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9477 else if (private_recv_p(node)) {
9478 ADD_INSN(recv, node, putself);
9479 flag |= VM_CALL_FCALL;
9482 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9485 if (
type == NODE_QCALL) {
9486 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9489 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9490 ADD_CALL_RECEIVER(recv, line_node);
9495 if (
type != NODE_VCALL) {
9496 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9497 CHECK(!
NIL_P(argc));
9505 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
9506 mid == rb_intern(
"new") &&
9507 parent_block == NULL &&
9508 !(flag & VM_CALL_ARGS_BLOCKARG);
9511 ADD_INSN(ret, node, putnil);
9512 ADD_INSN(ret, node, swap);
9517 debugp_param(
"call args argc", argc);
9518 debugp_param(
"call method",
ID2SYM(mid));
9520 switch ((
int)
type) {
9522 flag |= VM_CALL_VCALL;
9525 flag |= VM_CALL_FCALL;
9528 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9529 ADD_INSN(ret, line_node, splatkw);
9532 LABEL *not_basic_new = NEW_LABEL(nd_line(node));
9533 LABEL *not_basic_new_finish = NEW_LABEL(nd_line(node));
9538 if (flag & VM_CALL_FORWARDING) {
9539 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc) + 1, flag, keywords, 0);
9542 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc), flag, keywords, 0);
9544 ADD_INSN2(ret, node, opt_new, ci, not_basic_new);
9545 LABEL_REF(not_basic_new);
9548 ADD_SEND_R(ret, line_node, rb_intern(
"initialize"), argc, parent_block,
INT2FIX(flag | VM_CALL_FCALL), keywords);
9549 ADD_INSNL(ret, line_node, jump, not_basic_new_finish);
9551 ADD_LABEL(ret, not_basic_new);
9553 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9554 ADD_INSN(ret, line_node, swap);
9556 ADD_LABEL(ret, not_basic_new_finish);
9557 ADD_INSN(ret, line_node, pop);
9560 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9563 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9565 ADD_INSN(ret, line_node, pop);
9573 const int line = nd_line(node);
9575 unsigned int flag = 0;
9577 ID id = RNODE_OP_ASGN1(node)->nd_mid;
9603 ADD_INSN(ret, node, putnil);
9605 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
9606 CHECK(asgnflag != -1);
9607 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
9612 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
9613 CHECK(!
NIL_P(argc));
9615 int dup_argn =
FIX2INT(argc) + 1;
9616 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
9618 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
9620 if (
id == idOROP ||
id == idANDOP) {
9629 LABEL *label = NEW_LABEL(line);
9630 LABEL *lfin = NEW_LABEL(line);
9632 ADD_INSN(ret, node, dup);
9634 ADD_INSNL(ret, node, branchif, label);
9637 ADD_INSNL(ret, node, branchunless, label);
9639 ADD_INSN(ret, node, pop);
9641 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9643 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9645 if (flag & VM_CALL_ARGS_SPLAT) {
9646 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9647 ADD_INSN(ret, node, swap);
9648 ADD_INSN1(ret, node, splatarray,
Qtrue);
9649 ADD_INSN(ret, node, swap);
9650 flag |= VM_CALL_ARGS_SPLAT_MUT;
9652 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9653 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9656 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9658 ADD_INSN(ret, node, pop);
9659 ADD_INSNL(ret, node, jump, lfin);
9660 ADD_LABEL(ret, label);
9662 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9664 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
9665 ADD_LABEL(ret, lfin);
9668 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9669 ADD_SEND(ret, node,
id,
INT2FIX(1));
9671 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9673 if (flag & VM_CALL_ARGS_SPLAT) {
9674 if (flag & VM_CALL_KW_SPLAT) {
9675 ADD_INSN1(ret, node, topn,
INT2FIX(2));
9676 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9677 ADD_INSN1(ret, node, splatarray,
Qtrue);
9678 flag |= VM_CALL_ARGS_SPLAT_MUT;
9680 ADD_INSN(ret, node, swap);
9681 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9682 ADD_INSN1(ret, node, setn,
INT2FIX(2));
9683 ADD_INSN(ret, node, pop);
9686 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9687 ADD_INSN(ret, node, swap);
9688 ADD_INSN1(ret, node, splatarray,
Qtrue);
9689 ADD_INSN(ret, node, swap);
9690 flag |= VM_CALL_ARGS_SPLAT_MUT;
9692 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9694 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9697 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9699 ADD_INSN(ret, node, pop);
9707 const int line = nd_line(node);
9708 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
9709 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
9711 LABEL *lfin = NEW_LABEL(line);
9712 LABEL *lcfin = NEW_LABEL(line);
9767 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
9768 CHECK(asgnflag != -1);
9769 if (RNODE_OP_ASGN2(node)->nd_aid) {
9770 lskip = NEW_LABEL(line);
9771 ADD_INSN(ret, node, dup);
9772 ADD_INSNL(ret, node, branchnil, lskip);
9774 ADD_INSN(ret, node, dup);
9775 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
9777 if (atype == idOROP || atype == idANDOP) {
9779 ADD_INSN(ret, node, dup);
9781 if (atype == idOROP) {
9782 ADD_INSNL(ret, node, branchif, lcfin);
9785 ADD_INSNL(ret, node, branchunless, lcfin);
9788 ADD_INSN(ret, node, pop);
9790 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9792 ADD_INSN(ret, node, swap);
9793 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9795 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9796 ADD_INSNL(ret, node, jump, lfin);
9798 ADD_LABEL(ret, lcfin);
9800 ADD_INSN(ret, node, swap);
9803 ADD_LABEL(ret, lfin);
9806 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9807 ADD_SEND(ret, node, atype,
INT2FIX(1));
9809 ADD_INSN(ret, node, swap);
9810 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9812 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9814 if (lskip && popped) {
9815 ADD_LABEL(ret, lskip);
9817 ADD_INSN(ret, node, pop);
9818 if (lskip && !popped) {
9819 ADD_LABEL(ret, lskip);
9824static int compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
9829 const int line = nd_line(node);
9834 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
9836 ADD_INSN1(ret, node, putobject, rb_cObject);
9839 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
9842 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
9843 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
9846 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
9848 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
9849 lassign = NEW_LABEL(line);
9850 ADD_INSN(ret, node, dup);
9851 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
9853 ADD_INSNL(ret, node, branchunless, lassign);
9855 ADD_INSN(ret, node, dup);
9856 ADD_INSN1(ret, node, putobject,
Qtrue);
9857 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
9859 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
9860 lfin = NEW_LABEL(line);
9861 if (!popped) ADD_INSN(ret, node, dup);
9862 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
9863 ADD_INSNL(ret, node, branchif, lfin);
9865 ADD_INSNL(ret, node, branchunless, lfin);
9867 if (!popped) ADD_INSN(ret, node, pop);
9868 if (lassign) ADD_LABEL(ret, lassign);
9869 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9872 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9874 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
9875 ADD_INSN(ret, node, swap);
9877 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9878 ADD_LABEL(ret, lfin);
9879 if (!popped) ADD_INSN(ret, node, swap);
9880 ADD_INSN(ret, node, pop);
9883 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9885 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
9887 ADD_INSN(ret, node, swap);
9889 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9890 ADD_INSN(ret, node, swap);
9892 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9900 const int line = nd_line(node);
9901 LABEL *lfin = NEW_LABEL(line);
9904 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
9908 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
9909 lassign = lfinish[1];
9911 lassign = NEW_LABEL(line);
9913 ADD_INSNL(ret, node, branchunless, lassign);
9916 lassign = NEW_LABEL(line);
9919 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
9922 ADD_INSN(ret, node, dup);
9925 if (
type == NODE_OP_ASGN_AND) {
9926 ADD_INSNL(ret, node, branchunless, lfin);
9929 ADD_INSNL(ret, node, branchif, lfin);
9933 ADD_INSN(ret, node, pop);
9936 ADD_LABEL(ret, lassign);
9937 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
9938 ADD_LABEL(ret, lfin);
9948 unsigned int flag = 0;
9950 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9954 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9956 if (
type == NODE_SUPER) {
9957 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
9958 CHECK(!
NIL_P(vargc));
9960 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9961 ADD_INSN(args, node, splatkw);
9964 if (flag & VM_CALL_ARGS_BLOCKARG) {
9971 const rb_iseq_t *liseq = body->local_iseq;
9973 const struct rb_iseq_param_keyword *
const local_kwd = local_body->
param.keyword;
9974 int lvar_level = get_lvar_level(iseq);
9976 argc = local_body->
param.lead_num;
9979 for (i = 0; i < local_body->
param.lead_num; i++) {
9980 int idx = local_body->local_table_size - i;
9981 ADD_GETLOCAL(args, node, idx, lvar_level);
9985 if (local_body->
param.flags.forwardable) {
9986 flag |= VM_CALL_FORWARDING;
9987 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
9988 ADD_GETLOCAL(args, node, idx, lvar_level);
9991 if (local_body->
param.flags.has_opt) {
9994 for (j = 0; j < local_body->
param.opt_num; j++) {
9995 int idx = local_body->local_table_size - (i + j);
9996 ADD_GETLOCAL(args, node, idx, lvar_level);
10001 if (local_body->
param.flags.has_rest) {
10003 int idx = local_body->local_table_size - local_body->
param.rest_start;
10004 ADD_GETLOCAL(args, node, idx, lvar_level);
10005 ADD_INSN1(args, node, splatarray, RBOOL(local_body->
param.flags.has_post));
10007 argc = local_body->
param.rest_start + 1;
10008 flag |= VM_CALL_ARGS_SPLAT;
10010 if (local_body->
param.flags.has_post) {
10012 int post_len = local_body->
param.post_num;
10013 int post_start = local_body->
param.post_start;
10015 if (local_body->
param.flags.has_rest) {
10017 for (j=0; j<post_len; j++) {
10018 int idx = local_body->local_table_size - (post_start + j);
10019 ADD_GETLOCAL(args, node, idx, lvar_level);
10021 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
10022 flag |= VM_CALL_ARGS_SPLAT_MUT;
10027 for (j=0; j<post_len; j++) {
10028 int idx = local_body->local_table_size - (post_start + j);
10029 ADD_GETLOCAL(args, node, idx, lvar_level);
10031 argc = post_len + post_start;
10035 if (local_body->
param.flags.has_kw) {
10036 int local_size = local_body->local_table_size;
10039 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10041 if (local_body->
param.flags.has_kwrest) {
10042 int idx = local_body->local_table_size - local_kwd->rest_start;
10043 ADD_GETLOCAL(args, node, idx, lvar_level);
10045 ADD_SEND (args, node, rb_intern(
"dup"),
INT2FIX(0));
10048 ADD_INSN1(args, node, newhash,
INT2FIX(0));
10050 for (i = 0; i < local_kwd->num; ++i) {
10051 ID id = local_kwd->table[i];
10052 int idx = local_size - get_local_var_idx(liseq,
id);
10053 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
10054 ADD_GETLOCAL(args, node, idx, lvar_level);
10056 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
10057 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
10059 else if (local_body->
param.flags.has_kwrest) {
10060 int idx = local_body->local_table_size - local_kwd->rest_start;
10061 ADD_GETLOCAL(args, node, idx, lvar_level);
10063 flag |= VM_CALL_KW_SPLAT;
10067 if (use_block && parent_block == NULL) {
10068 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10071 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
10072 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
10073 ADD_INSN(ret, node, putself);
10074 ADD_SEQ(ret, args);
10076 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
10078 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
10079 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
10082 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
10086 ADD_INSN(ret, node, pop);
10096 unsigned int flag = 0;
10101 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
10102 case ISEQ_TYPE_TOP:
10103 case ISEQ_TYPE_MAIN:
10104 case ISEQ_TYPE_CLASS:
10105 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
10110 if (RNODE_YIELD(node)->nd_head) {
10111 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
10112 CHECK(!
NIL_P(argc));
10118 ADD_SEQ(ret, args);
10119 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
10120 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10123 ADD_INSN(ret, node, pop);
10128 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
10129 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
10131 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
10144 switch ((
int)
type) {
10146 ADD_INSN1(recv, node, putobject, rb_node_regx_string_val(node));
10147 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
10151 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
10152 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
10155 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
10156 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
10160 ADD_SEQ(ret, recv);
10162 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
10164 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
10165 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
10169 ADD_INSN(ret, node, pop);
10180 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10181 (segments = collect_const_segments(iseq, node))) {
10182 ISEQ_BODY(iseq)->ic_size++;
10183 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10193 CHECK(compile_const_prefix(iseq, node, pref, body));
10194 if (LIST_INSN_SIZE_ZERO(pref)) {
10195 ADD_INSN(ret, node, putnil);
10196 ADD_SEQ(ret, body);
10199 ADD_SEQ(ret, pref);
10200 ADD_SEQ(ret, body);
10206 ADD_CALL_RECEIVER(ret, node);
10207 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10208 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10211 ADD_INSN(ret, node, pop);
10219 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10222 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10223 ISEQ_BODY(iseq)->ic_size++;
10224 VALUE segments = rb_ary_new_from_args(2,
ID2SYM(idNULL),
ID2SYM(RNODE_COLON3(node)->nd_mid));
10225 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10229 ADD_INSN1(ret, node, putobject, rb_cObject);
10230 ADD_INSN1(ret, node, putobject,
Qtrue);
10231 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10235 ADD_INSN(ret, node, pop);
10244 const NODE *b = RNODE_DOT2(node)->nd_beg;
10245 const NODE *e = RNODE_DOT2(node)->nd_end;
10247 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10249 VALUE bv = optimized_range_item(b);
10250 VALUE ev = optimized_range_item(e);
10252 ADD_INSN1(ret, node, putobject, val);
10257 CHECK(COMPILE_(ret,
"min", b, popped));
10258 CHECK(COMPILE_(ret,
"max", e, popped));
10260 ADD_INSN1(ret, node, newrange, flag);
10270 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10271 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10277 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10280 ip = ISEQ_BODY(ip)->parent_iseq;
10284 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10287 ADD_INSN(ret, node, putnil);
10298 LABEL *end_label = NEW_LABEL(nd_line(node));
10299 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10301 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10303 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10306 else if (nd_type_p(default_value, NODE_SYM) ||
10307 nd_type_p(default_value, NODE_REGX) ||
10308 nd_type_p(default_value, NODE_LINE) ||
10309 nd_type_p(default_value, NODE_INTEGER) ||
10310 nd_type_p(default_value, NODE_FLOAT) ||
10311 nd_type_p(default_value, NODE_RATIONAL) ||
10312 nd_type_p(default_value, NODE_IMAGINARY) ||
10313 nd_type_p(default_value, NODE_NIL) ||
10314 nd_type_p(default_value, NODE_TRUE) ||
10315 nd_type_p(default_value, NODE_FALSE)) {
10316 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10324 int kw_bits_idx = body->local_table_size - body->
param.keyword->bits_start;
10325 int keyword_idx = body->
param.keyword->num;
10327 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10328 ADD_INSNL(ret, node, branchif, end_label);
10329 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10330 ADD_LABEL(ret, end_label);
10340 unsigned int flag = 0;
10341 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10343 LABEL *else_label = NULL;
10348 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10349 CHECK(!
NIL_P(argc));
10351 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10352 CHECK(asgnflag != -1);
10353 flag |= (
unsigned int)asgnflag;
10355 debugp_param(
"argc", argc);
10356 debugp_param(
"nd_mid",
ID2SYM(mid));
10360 mid = rb_id_attrset(mid);
10361 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10364 ADD_INSN(ret, node, putnil);
10365 ADD_SEQ(ret, recv);
10366 ADD_SEQ(ret, args);
10368 if (flag & VM_CALL_ARGS_SPLAT) {
10369 ADD_INSN(ret, node, dup);
10370 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10371 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10372 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10373 ADD_INSN (ret, node, pop);
10376 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10380 ADD_SEQ(ret, recv);
10381 ADD_SEQ(ret, args);
10383 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10384 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10385 ADD_INSN(ret, node, pop);
10392 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10400 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10407 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10414node_const_decl_val(
const NODE *node)
10417 switch (nd_type(node)) {
10419 if (RNODE_CDECL(node)->nd_vid) {
10420 path = rb_id2str(RNODE_CDECL(node)->nd_vid);
10424 node = RNODE_CDECL(node)->nd_else;
10432 rb_str_append(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10435 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10441 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10442 rb_ary_push(path, rb_id2str(RNODE_COLON2(node)->nd_mid));
10444 if (node && nd_type_p(node, NODE_CONST)) {
10446 rb_ary_push(path, rb_id2str(RNODE_CONST(node)->nd_vid));
10448 else if (node && nd_type_p(node, NODE_COLON3)) {
10450 rb_ary_push(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10460 path = rb_fstring(path);
10465const_decl_path(
NODE *dest)
10468 if (!nd_type_p(dest, NODE_CALL)) {
10469 path = node_const_decl_val(dest);
10480 VALUE path = const_decl_path(dest);
10481 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10482 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10483 ADD_INSN1(ret, value, putobject, path);
10485 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10490#ifndef SHAREABLE_BARE_EXPRESSION
10491#define SHAREABLE_BARE_EXPRESSION 1
10495compile_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)
10497# define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10498 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10500 DECL_ANCHOR(anchor);
10502 enum node_type
type = node ? nd_type(node) : NODE_NIL;
10514 *value_p = rb_node_sym_string_val(node);
10517 *value_p = rb_node_regx_string_val(node);
10520 *value_p = rb_node_line_lineno_val(node);
10523 *value_p = rb_node_integer_literal_val(node);
10526 *value_p = rb_node_float_literal_val(node);
10528 case NODE_RATIONAL:
10529 *value_p = rb_node_rational_literal_val(node);
10531 case NODE_IMAGINARY:
10532 *value_p = rb_node_imaginary_literal_val(node);
10534 case NODE_ENCODING:
10535 *value_p = rb_node_encoding_val(node);
10538 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10539 *shareable_literal_p = 1;
10543 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10544 if (shareable == rb_parser_shareable_literal) {
10550 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10553 *shareable_literal_p = 1;
10557 VALUE lit = rb_node_str_string_val(node);
10558 ADD_INSN1(ret, node, putobject, lit);
10561 *shareable_literal_p = 1;
10567 VALUE lit = rb_node_file_path_val(node);
10568 ADD_INSN1(ret, node, putobject, lit);
10571 *shareable_literal_p = 1;
10579 ADD_INSN1(ret, node, putobject, lit);
10582 *shareable_literal_p = 1;
10588 INIT_ANCHOR(anchor);
10590 for (
NODE *n = (
NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
10592 int shareable_literal_p2;
10593 NODE *elt = RNODE_LIST(n)->nd_head;
10595 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
10596 if (shareable_literal_p2) {
10599 else if (
RTEST(lit)) {
10605 if (!UNDEF_P(val)) {
10617 if (!RNODE_HASH(node)->nd_brace) {
10619 *shareable_literal_p = 0;
10622 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10623 if (!RNODE_LIST(n)->nd_head) {
10625 goto compile_shareable;
10629 INIT_ANCHOR(anchor);
10630 lit = rb_hash_new();
10631 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10633 VALUE value_val = 0;
10634 int shareable_literal_p2;
10635 NODE *key = RNODE_LIST(n)->nd_head;
10636 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
10637 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10638 if (shareable_literal_p2) {
10641 else if (
RTEST(lit)) {
10642 rb_hash_clear(lit);
10645 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10646 if (shareable_literal_p2) {
10649 else if (
RTEST(lit)) {
10650 rb_hash_clear(lit);
10654 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
10655 rb_hash_aset(lit, key_val, value_val);
10658 rb_hash_clear(lit);
10669 if (shareable == rb_parser_shareable_literal &&
10670 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
10671 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
10673 *shareable_literal_p = 1;
10676 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10678 *shareable_literal_p = 0;
10684 if (nd_type(node) == NODE_LIST) {
10685 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10687 else if (nd_type(node) == NODE_HASH) {
10688 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10689 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10692 *shareable_literal_p = 0;
10693 ADD_SEQ(ret, anchor);
10699 if (nd_type(node) == NODE_LIST) {
10700 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10702 else if (nd_type(node) == NODE_HASH) {
10703 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10704 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10706 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
10708 *shareable_literal_p = 1;
10712 ADD_INSN1(ret, node, putobject, val);
10715 *shareable_literal_p = 1;
10722compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
10726 DECL_ANCHOR(anchor);
10727 INIT_ANCHOR(anchor);
10729 switch (shareable) {
10730 case rb_parser_shareable_none:
10731 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
10734 case rb_parser_shareable_literal:
10735 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10736 ADD_SEQ(ret, anchor);
10739 case rb_parser_shareable_copy:
10740 case rb_parser_shareable_everything:
10741 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10743 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
10746 ADD_SEQ(ret, anchor);
10750 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
10767 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
10768 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
10769 debugs(
"node: NODE_NIL(implicit)\n");
10770 ADD_SYNTHETIC_INSN(ret, lineno, -1, putnil);
10774 return iseq_compile_each0(iseq, ret, node, popped);
10780 const int line = (int)nd_line(node);
10781 const enum node_type
type = nd_type(node);
10784 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
10788 if (nd_fl_newline(node)) {
10790 ISEQ_COMPILE_DATA(iseq)->last_line = line;
10791 if (line > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
10792 event |= RUBY_EVENT_COVERAGE_LINE;
10794 ADD_TRACE(ret, event);
10798 debug_node_start(node);
10799#undef BEFORE_RETURN
10800#define BEFORE_RETURN debug_node_end()
10804 CHECK(compile_block(iseq, ret, node, popped));
10808 CHECK(compile_if(iseq, ret, node, popped,
type));
10811 CHECK(compile_case(iseq, ret, node, popped));
10814 CHECK(compile_case2(iseq, ret, node, popped));
10817 CHECK(compile_case3(iseq, ret, node, popped));
10821 CHECK(compile_loop(iseq, ret, node, popped,
type));
10825 CHECK(compile_iter(iseq, ret, node, popped));
10827 case NODE_FOR_MASGN:
10828 CHECK(compile_for_masgn(iseq, ret, node, popped));
10831 CHECK(compile_break(iseq, ret, node, popped));
10834 CHECK(compile_next(iseq, ret, node, popped));
10837 CHECK(compile_redo(iseq, ret, node, popped));
10840 CHECK(compile_retry(iseq, ret, node, popped));
10843 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
10847 CHECK(compile_rescue(iseq, ret, node, popped));
10850 CHECK(compile_resbody(iseq, ret, node, popped));
10853 CHECK(compile_ensure(iseq, ret, node, popped));
10858 LABEL *end_label = NEW_LABEL(line);
10859 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(node)->nd_1st));
10861 ADD_INSN(ret, node, dup);
10863 if (
type == NODE_AND) {
10864 ADD_INSNL(ret, node, branchunless, end_label);
10867 ADD_INSNL(ret, node, branchif, end_label);
10870 ADD_INSN(ret, node, pop);
10872 CHECK(COMPILE_(ret,
"nd_2nd", RNODE_OR(node)->nd_2nd, popped));
10873 ADD_LABEL(ret, end_label);
10878 bool prev_in_masgn = ISEQ_COMPILE_DATA(iseq)->in_masgn;
10879 ISEQ_COMPILE_DATA(iseq)->in_masgn =
true;
10880 compile_massign(iseq, ret, node, popped);
10881 ISEQ_COMPILE_DATA(iseq)->in_masgn = prev_in_masgn;
10886 ID id = RNODE_LASGN(node)->nd_vid;
10887 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
10889 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
10890 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
10893 ADD_INSN(ret, node, dup);
10895 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
10900 ID id = RNODE_DASGN(node)->nd_vid;
10901 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
10902 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
10905 ADD_INSN(ret, node, dup);
10908 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
10911 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
10915 ADD_SETLOCAL(ret, node, ls - idx, lv);
10919 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
10922 ADD_INSN(ret, node, dup);
10924 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
10928 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
10930 ADD_INSN(ret, node, dup);
10932 ADD_INSN2(ret, node, setinstancevariable,
10933 ID2SYM(RNODE_IASGN(node)->nd_vid),
10934 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
10938 if (RNODE_CDECL(node)->nd_vid) {
10939 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
10942 ADD_INSN(ret, node, dup);
10945 ADD_INSN1(ret, node, putspecialobject,
10946 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
10947 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
10950 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
10951 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
10952 ADD_INSN(ret, node, swap);
10955 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10956 ADD_INSN(ret, node, swap);
10959 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
10964 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
10966 ADD_INSN(ret, node, dup);
10968 ADD_INSN2(ret, node, setclassvariable,
10969 ID2SYM(RNODE_CVASGN(node)->nd_vid),
10970 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
10973 case NODE_OP_ASGN1:
10974 CHECK(compile_op_asgn1(iseq, ret, node, popped));
10976 case NODE_OP_ASGN2:
10977 CHECK(compile_op_asgn2(iseq, ret, node, popped));
10979 case NODE_OP_CDECL:
10980 CHECK(compile_op_cdecl(iseq, ret, node, popped));
10982 case NODE_OP_ASGN_AND:
10983 case NODE_OP_ASGN_OR:
10984 CHECK(compile_op_log(iseq, ret, node, popped,
type));
10988 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
10994 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
11000 CHECK(compile_super(iseq, ret, node, popped,
type));
11003 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
11008 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
11013 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
11016 CHECK(compile_return(iseq, ret, node, popped));
11019 CHECK(compile_yield(iseq, ret, node, popped));
11023 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
11029 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
11031 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
11033 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
11034 rb_id2str(RNODE_DVAR(node)->nd_vid));
11037 ADD_GETLOCAL(ret, node, ls - idx, lv);
11042 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
11044 ADD_INSN(ret, node, pop);
11049 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
11051 ADD_INSN2(ret, node, getinstancevariable,
11052 ID2SYM(RNODE_IVAR(node)->nd_vid),
11053 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
11058 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
11060 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
11062 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(RNODE_CONST(node)->nd_vid));
11063 ADD_INSN1(ret, node, opt_getconstant_path, segments);
11067 ADD_INSN(ret, node, putnil);
11068 ADD_INSN1(ret, node, putobject,
Qtrue);
11069 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
11073 ADD_INSN(ret, node, pop);
11079 ADD_INSN2(ret, node, getclassvariable,
11080 ID2SYM(RNODE_CVAR(node)->nd_vid),
11081 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
11085 case NODE_NTH_REF:{
11087 if (!RNODE_NTH_REF(node)->nd_nth) {
11088 ADD_INSN(ret, node, putnil);
11091 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11092 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
11096 case NODE_BACK_REF:{
11098 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11099 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
11106 CHECK(compile_match(iseq, ret, node, popped,
type));
11110 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
11116 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
11120 case NODE_ENCODING:{
11122 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
11126 case NODE_INTEGER:{
11127 VALUE lit = rb_node_integer_literal_val(node);
11128 debugp_param(
"integer", lit);
11130 ADD_INSN1(ret, node, putobject, lit);
11136 VALUE lit = rb_node_float_literal_val(node);
11137 debugp_param(
"float", lit);
11139 ADD_INSN1(ret, node, putobject, lit);
11144 case NODE_RATIONAL:{
11145 VALUE lit = rb_node_rational_literal_val(node);
11146 debugp_param(
"rational", lit);
11148 ADD_INSN1(ret, node, putobject, lit);
11153 case NODE_IMAGINARY:{
11154 VALUE lit = rb_node_imaginary_literal_val(node);
11155 debugp_param(
"imaginary", lit);
11157 ADD_INSN1(ret, node, putobject, lit);
11164 debugp_param(
"nd_lit", get_string_value(node));
11166 VALUE lit = get_string_value(node);
11169 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11170 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11172 switch (option->frozen_string_literal) {
11173 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11174 ADD_INSN1(ret, node, putchilledstring, lit);
11176 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11177 ADD_INSN1(ret, node, putstring, lit);
11179 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11180 ADD_INSN1(ret, node, putobject, lit);
11183 rb_bug(
"invalid frozen_string_literal");
11190 compile_dstr(iseq, ret, node);
11193 ADD_INSN(ret, node, pop);
11198 ADD_CALL_RECEIVER(ret, node);
11199 VALUE str = rb_node_str_string_val(node);
11200 ADD_INSN1(ret, node, putobject, str);
11202 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11205 ADD_INSN(ret, node, pop);
11210 ADD_CALL_RECEIVER(ret, node);
11211 compile_dstr(iseq, ret, node);
11212 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11215 ADD_INSN(ret, node, pop);
11220 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11224 VALUE lit = rb_node_regx_string_val(node);
11225 ADD_INSN1(ret, node, putobject, lit);
11231 compile_dregx(iseq, ret, node, popped);
11234 int ic_index = body->ise_size++;
11236 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11238 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
11242 ADD_INSN(ret, node, pop);
11246 case NODE_ARGSCAT:{
11248 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11249 ADD_INSN1(ret, node, splatarray,
Qfalse);
11250 ADD_INSN(ret, node, pop);
11251 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11252 ADD_INSN1(ret, node, splatarray,
Qfalse);
11253 ADD_INSN(ret, node, pop);
11256 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11257 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11258 if (nd_type_p(body_node, NODE_LIST)) {
11259 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11262 CHECK(COMPILE(ret,
"argscat body", body_node));
11263 ADD_INSN(ret, node, concattoarray);
11268 case NODE_ARGSPUSH:{
11270 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11271 ADD_INSN1(ret, node, splatarray,
Qfalse);
11272 ADD_INSN(ret, node, pop);
11273 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11276 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11277 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11278 if (keyword_node_p(body_node)) {
11279 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11280 ADD_INSN(ret, node, pushtoarraykwsplat);
11282 else if (static_literal_node_p(body_node, iseq,
false)) {
11283 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11284 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11287 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11288 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11294 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11295 ADD_INSN1(ret, node, splatarray,
Qtrue);
11298 ADD_INSN(ret, node, pop);
11303 ID mid = RNODE_DEFN(node)->nd_mid;
11304 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11306 ISEQ_TYPE_METHOD, line);
11308 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11309 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11313 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11319 ID mid = RNODE_DEFS(node)->nd_mid;
11320 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11322 ISEQ_TYPE_METHOD, line);
11324 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11325 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11326 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11330 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11335 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11336 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11337 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11338 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11339 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11342 ADD_INSN(ret, node, pop);
11347 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11348 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11349 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11350 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11353 ADD_INSN(ret, node, pop);
11360 for (
long i = 0; i < ary->len; i++) {
11361 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11362 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11363 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11364 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11366 if (i < ary->
len - 1) {
11367 ADD_INSN(ret, node, pop);
11372 ADD_INSN(ret, node, pop);
11377 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11378 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
11379 ISEQ_TYPE_CLASS, line);
11380 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11381 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11382 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11384 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11385 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11389 ADD_INSN(ret, node, pop);
11394 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11395 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
11396 ISEQ_TYPE_CLASS, line);
11397 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11398 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11400 ADD_INSN (ret, node, putnil);
11401 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11405 ADD_INSN(ret, node, pop);
11411 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11412 ISEQ_TYPE_CLASS, line);
11414 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11415 ADD_INSN (ret, node, putnil);
11416 CONST_ID(singletonclass,
"singletonclass");
11417 ADD_INSN3(ret, node, defineclass,
11418 ID2SYM(singletonclass), singleton_class,
11419 INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
11423 ADD_INSN(ret, node, pop);
11428 CHECK(compile_colon2(iseq, ret, node, popped));
11431 CHECK(compile_colon3(iseq, ret, node, popped));
11434 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11437 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11441 LABEL *lend = NEW_LABEL(line);
11442 LABEL *ltrue = NEW_LABEL(line);
11443 LABEL *lfalse = NEW_LABEL(line);
11444 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11446 ADD_LABEL(ret, ltrue);
11447 ADD_INSN1(ret, node, putobject,
Qtrue);
11448 ADD_INSNL(ret, node, jump, lend);
11449 ADD_LABEL(ret, lfalse);
11450 ADD_INSN1(ret, node, putobject,
Qfalse);
11451 ADD_LABEL(ret, lend);
11456 ADD_INSN(ret, node, putself);
11462 ADD_INSN(ret, node, putnil);
11468 ADD_INSN1(ret, node, putobject,
Qtrue);
11474 ADD_INSN1(ret, node, putobject,
Qfalse);
11479 CHECK(compile_errinfo(iseq, ret, node, popped));
11483 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11486 case NODE_POSTEXE:{
11490 int is_index = body->ise_size++;
11492 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11494 NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc, rb_fstring(make_name_for_block(iseq)), ISEQ_TYPE_BLOCK, line);
11496 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
11500 ADD_INSN(ret, node, pop);
11505 CHECK(compile_kw_arg(iseq, ret, node, popped));
11508 compile_dstr(iseq, ret, node);
11510 ADD_INSN(ret, node, intern);
11513 ADD_INSN(ret, node, pop);
11517 case NODE_ATTRASGN:
11518 CHECK(compile_attrasgn(iseq, ret, node, popped));
11522 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11525 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11526 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11530 ADD_INSN(ret, node, pop);
11535 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
11550insn_data_length(
INSN *iobj)
11552 return insn_len(iobj->insn_id);
11556calc_sp_depth(
int depth,
INSN *insn)
11558 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
11562opobj_inspect(
VALUE obj)
11582insn_data_to_s_detail(
INSN *iobj)
11584 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
11586 if (iobj->operands) {
11587 const char *types = insn_op_types(iobj->insn_id);
11590 for (j = 0; types[j]; j++) {
11591 char type = types[j];
11597 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
11615 VALUE v = OPERAND_AT(iobj, j);
11630 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11633 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11636 rb_str_catf(str,
"<ise:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11642 if (vm_ci_mid(ci)) rb_str_catf(str,
"%"PRIsVALUE, rb_id2str(vm_ci_mid(ci)));
11643 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
11651 void *func = (
void *)OPERAND_AT(iobj, j);
11654 if (dladdr(func, &info) && info.dli_sname) {
11659 rb_str_catf(str,
"<%p>", func);
11669 if (types[j + 1]) {
11680 dump_disasm_list_with_cursor(link, NULL, NULL);
11691 printf(
"-- raw disasm--------\n");
11694 if (curr) printf(curr == link ?
"*" :
" ");
11695 switch (link->type) {
11696 case ISEQ_ELEMENT_INSN:
11698 iobj = (
INSN *)link;
11699 str = insn_data_to_s_detail(iobj);
11700 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
11701 pos += insn_data_length(iobj);
11704 case ISEQ_ELEMENT_LABEL:
11706 lobj = (
LABEL *)link;
11707 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
11708 dest == lobj ?
" <---" :
"");
11711 case ISEQ_ELEMENT_TRACE:
11714 printf(
" trace: %0x\n", trace->event);
11717 case ISEQ_ELEMENT_ADJUST:
11720 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
11725 rb_raise(
rb_eSyntaxError,
"dump_disasm_list error: %d\n", (
int)link->type);
11729 printf(
"---------------------\n");
11734rb_insn_len(
VALUE insn)
11736 return insn_len(insn);
11740rb_insns_name(
int i)
11742 return insn_name(i);
11746rb_insns_name_array(
void)
11750 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
11761 obj = rb_to_symbol_type(obj);
11763 if (st_lookup(labels_table, obj, &tmp) == 0) {
11764 label = NEW_LABEL(0);
11765 st_insert(labels_table, obj, (st_data_t)label);
11768 label = (
LABEL *)tmp;
11775get_exception_sym2type(
VALUE sym)
11777 static VALUE symRescue, symEnsure, symRetry;
11778 static VALUE symBreak, symRedo, symNext;
11780 if (symRescue == 0) {
11789 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
11790 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
11791 if (sym == symRetry)
return CATCH_TYPE_RETRY;
11792 if (sym == symBreak)
return CATCH_TYPE_BREAK;
11793 if (sym == symRedo)
return CATCH_TYPE_REDO;
11794 if (sym == symNext)
return CATCH_TYPE_NEXT;
11795 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
11808 LABEL *lstart, *lend, *lcont;
11823 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
11824 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
11825 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
11829 if (
type == CATCH_TYPE_RESCUE ||
11830 type == CATCH_TYPE_BREAK ||
11831 type == CATCH_TYPE_NEXT) {
11837 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
11845insn_make_insn_table(
void)
11849 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
11851 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
11865 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
11867 else if (
CLASS_OF(op) == rb_cISeq) {
11874 loaded_iseq = rb_iseqw_to_iseq(iseqw);
11875 return loaded_iseq;
11883 unsigned int flag = 0;
11894 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
11896 if (!
NIL_P(vkw_arg)) {
11899 size_t n = rb_callinfo_kwarg_bytes(
len);
11902 kw_arg->references = 0;
11903 kw_arg->keyword_len =
len;
11904 for (i = 0; i <
len; i++) {
11907 kw_arg->keywords[i] = kw;
11912 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
11918event_name_to_flag(
VALUE sym)
11920#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
11941 int line_no = 0, node_id = -1, insn_idx = 0;
11942 int ret = COMPILE_OK;
11947 static struct st_table *insn_table;
11949 if (insn_table == 0) {
11950 insn_table = insn_make_insn_table();
11953 for (i=0; i<
len; i++) {
11959 ADD_TRACE(anchor, event);
11962 LABEL *label = register_label(iseq, labels_table, obj);
11963 ADD_LABEL(anchor, label);
11980 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
11982 COMPILE_ERROR(iseq, line_no,
11983 "unknown instruction: %+"PRIsVALUE, insn);
11988 if (argc != insn_len((
VALUE)insn_id)-1) {
11989 COMPILE_ERROR(iseq, line_no,
11990 "operand size mismatch");
11996 argv = compile_data_calloc2(iseq,
sizeof(
VALUE), argc);
12001 (
enum ruby_vminsn_type)insn_id, argc, argv));
12003 for (j=0; j<argc; j++) {
12005 switch (insn_op_type((
VALUE)insn_id, j)) {
12007 LABEL *label = register_label(iseq, labels_table, op);
12008 argv[j] = (
VALUE)label;
12023 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
12034 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
12035 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
12041 op = rb_to_array_type(op);
12045 sym = rb_to_symbol_type(sym);
12050 argv[j] = segments;
12052 ISEQ_BODY(iseq)->ic_size++;
12057 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
12058 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
12063 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
12064 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
12068 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
12071 argv[j] = rb_to_symbol_type(op);
12078 RHASH_TBL_RAW(map)->type = &cdhash_type;
12079 op = rb_to_array_type(op);
12084 register_label(iseq, labels_table, sym);
12085 rb_hash_aset(map, key, (
VALUE)label | 1);
12094#if SIZEOF_VALUE <= SIZEOF_LONG
12099 argv[j] = (
VALUE)funcptr;
12110 (
enum ruby_vminsn_type)insn_id, argc, NULL));
12114 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
12119 validate_labels(iseq, labels_table);
12120 if (!ret)
return ret;
12121 return iseq_setup(iseq, anchor);
12124#define CHECK_ARRAY(v) rb_to_array_type(v)
12125#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
12130 VALUE val = rb_hash_aref(param, sym);
12135 else if (!
NIL_P(val)) {
12136 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
12142static const struct rb_iseq_param_keyword *
12148 VALUE key, sym, default_val;
12151 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12153 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12155 keyword->num =
len;
12156#define SYM(s) ID2SYM(rb_intern_const(#s))
12157 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12158 i = keyword->bits_start - keyword->num;
12159 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12163 for (i = 0; i <
len; i++) {
12167 goto default_values;
12170 keyword->required_num++;
12174 default_len =
len - i;
12175 if (default_len == 0) {
12176 keyword->table = ids;
12179 else if (default_len < 0) {
12185 for (j = 0; i <
len; i++, j++) {
12199 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
12205 keyword->table = ids;
12206 keyword->default_values = dvs;
12212iseq_insn_each_object_mark_and_move(
VALUE * obj,
VALUE _)
12214 rb_gc_mark_and_move(obj);
12221 size_t size =
sizeof(
INSN);
12222 unsigned int pos = 0;
12225#ifdef STRICT_ALIGNMENT
12226 size_t padding = calc_padding((
void *)&storage->buff[pos], size);
12228 const size_t padding = 0;
12230 size_t offset = pos + size + padding;
12231 if (offset > storage->size || offset > storage->pos) {
12233 storage = storage->next;
12236#ifdef STRICT_ALIGNMENT
12237 pos += (int)padding;
12240 iobj = (
INSN *)&storage->buff[pos];
12242 if (iobj->operands) {
12243 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_move, (
VALUE)0);
12256 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
12263#define SYM(s) ID2SYM(rb_intern_const(#s))
12265 unsigned int arg_size, local_size, stack_max;
12267 struct st_table *labels_table = st_init_numtable();
12269 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
12270 VALUE keywords = rb_hash_aref(params, SYM(keyword));
12272 DECL_ANCHOR(anchor);
12273 INIT_ANCHOR(anchor);
12276 ISEQ_BODY(iseq)->local_table_size =
len;
12277 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12279 for (i = 0; i <
len; i++) {
12282 if (sym_arg_rest == lv) {
12290#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12291 if (INT_PARAM(lead_num)) {
12292 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12294 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12295 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12296 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12297 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12300#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12302 INT_PARAM(arg_size);
12303 INT_PARAM(local_size);
12304 INT_PARAM(stack_max);
12309#ifdef USE_ISEQ_NODE_ID
12310 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
12318 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12320 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12323 for (i = 0; i <
len; i++) {
12325 LABEL *label = register_label(iseq, labels_table, ent);
12326 opt_table[i] = (
VALUE)label;
12329 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12330 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12333 else if (!
NIL_P(arg_opt_labels)) {
12334 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
12339 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12341 else if (!
NIL_P(keywords)) {
12342 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
12346 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
12347 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12350 if (
Qtrue == rb_hash_aref(params, SYM(use_block))) {
12351 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12354 if (int_param(&i, params, SYM(kwrest))) {
12355 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12356 if (keyword == NULL) {
12357 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12359 keyword->rest_start = i;
12360 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12363 iseq_calc_param_size(iseq);
12366 iseq_build_from_ary_exception(iseq, labels_table, exception);
12369 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12371 ISEQ_BODY(iseq)->param.size = arg_size;
12372 ISEQ_BODY(iseq)->local_table_size = local_size;
12373 ISEQ_BODY(iseq)->stack_max = stack_max;
12383 while (body->type == ISEQ_TYPE_BLOCK ||
12384 body->type == ISEQ_TYPE_RESCUE ||
12385 body->type == ISEQ_TYPE_ENSURE ||
12386 body->type == ISEQ_TYPE_EVAL ||
12387 body->type == ISEQ_TYPE_MAIN
12391 for (i = 0; i < body->local_table_size; i++) {
12392 if (body->local_table[i] ==
id) {
12396 iseq = body->parent_iseq;
12397 body = ISEQ_BODY(iseq);
12410 for (i=0; i<body->local_table_size; i++) {
12411 if (body->local_table[i] ==
id) {
12421#ifndef IBF_ISEQ_DEBUG
12422#define IBF_ISEQ_DEBUG 0
12425#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12426#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12429typedef uint32_t ibf_offset_t;
12430#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12432#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12434#define IBF_DEVEL_VERSION 5
12435#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12437#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12440static const char IBF_ENDIAN_MARK =
12441#ifdef WORDS_BIGENDIAN
12450 uint32_t major_version;
12451 uint32_t minor_version;
12453 uint32_t extra_size;
12455 uint32_t iseq_list_size;
12456 uint32_t global_object_list_size;
12457 ibf_offset_t iseq_list_offset;
12458 ibf_offset_t global_object_list_offset;
12479 unsigned int obj_list_size;
12480 ibf_offset_t obj_list_offset;
12499pinned_list_mark(
void *ptr)
12503 for (i = 0; i < list->size; i++) {
12504 if (list->buffer[i]) {
12505 rb_gc_mark(list->buffer[i]);
12517 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
12521pinned_list_fetch(
VALUE list,
long offset)
12527 if (offset >= ptr->size) {
12528 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12531 return ptr->buffer[offset];
12535pinned_list_store(
VALUE list,
long offset,
VALUE object)
12541 if (offset >= ptr->size) {
12542 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12549pinned_list_new(
long size)
12551 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
12552 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
12553 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
12559ibf_dump_pos(
struct ibf_dump *dump)
12561 long pos = RSTRING_LEN(dump->current_buffer->str);
12562#if SIZEOF_LONG > SIZEOF_INT
12563 if (pos >= UINT_MAX) {
12567 return (
unsigned int)pos;
12571ibf_dump_align(
struct ibf_dump *dump,
size_t align)
12573 ibf_offset_t pos = ibf_dump_pos(dump);
12575 static const char padding[
sizeof(
VALUE)];
12576 size_t size = align - ((size_t)pos % align);
12577#if SIZEOF_LONG > SIZEOF_INT
12578 if (pos + size >= UINT_MAX) {
12582 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
12583 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
12585 rb_str_cat(dump->current_buffer->str, padding, size);
12590ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
12592 ibf_offset_t pos = ibf_dump_pos(dump);
12593#if SIZEOF_LONG > SIZEOF_INT
12595 if (size >= UINT_MAX || pos + size >= UINT_MAX) {
12599 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
12604ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
12606 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
12610ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
12612 VALUE str = dump->current_buffer->str;
12613 char *ptr = RSTRING_PTR(str);
12614 if ((
unsigned long)(size + offset) > (
unsigned long)RSTRING_LEN(str))
12615 rb_bug(
"ibf_dump_overwrite: overflow");
12616 memcpy(ptr + offset, buff, size);
12620ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
12622 ibf_offset_t beg = *offset;
12624 return load->current_buffer->buff + beg;
12628ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
12630 void *buff = ruby_xmalloc2(x, y);
12631 size_t size = x * y;
12632 memcpy(buff, load->current_buffer->buff + offset, size);
12636#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
12638#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
12639#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
12640#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
12641#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
12642#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
12645ibf_table_lookup(
struct st_table *table, st_data_t key)
12649 if (st_lookup(table, key, &val)) {
12658ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
12660 int index = ibf_table_lookup(table, key);
12663 index = (int)table->num_entries;
12664 st_insert(table, key, (st_data_t)index);
12672static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
12678ibf_dump_object_table_new(
void)
12680 st_table *obj_table = st_init_numtable();
12681 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
12689 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
12695 if (
id == 0 || rb_id2name(
id) == NULL) {
12698 return ibf_dump_object(dump,
rb_id2sym(
id));
12702ibf_load_id(
const struct ibf_load *load,
const ID id_index)
12704 if (id_index == 0) {
12707 VALUE sym = ibf_load_object(load, id_index);
12717static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
12722 if (iseq == NULL) {
12726 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
12730static unsigned char
12731ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
12733 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
12734 return (
unsigned char)load->current_buffer->buff[(*offset)++];
12750 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12751 ibf_dump_write(dump, &x,
sizeof(
VALUE));
12755 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12757 unsigned char bytes[max_byte_length];
12760 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
12761 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12767 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12770 ibf_dump_write(dump, bytes + max_byte_length - n, n);
12774ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
12776 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12777 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
12779 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
12780 *offset +=
sizeof(
VALUE);
12785 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12787 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
12788 const unsigned char c = buffer[*offset];
12792 c == 0 ? 9 : ntz_int32(c) + 1;
12795 if (*offset + n > load->current_buffer->size) {
12800 for (i = 1; i < n; i++) {
12802 x |= (
VALUE)buffer[*offset + i];
12816 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
12818 size_t len = strlen(bf->name);
12819 ibf_dump_write_small_value(dump, (
VALUE)
len);
12820 ibf_dump_write(dump, bf->name,
len);
12824ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
12826 int i = (int)ibf_load_small_value(load, offset);
12827 int len = (int)ibf_load_small_value(load, offset);
12828 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
12831 fprintf(stderr,
"%.*s!!\n",
len, name);
12835 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
12836 if (strncmp(table[i].name, name,
len) != 0) {
12837 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
12848 const int iseq_size = body->iseq_size;
12850 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
12852 ibf_offset_t offset = ibf_dump_pos(dump);
12854 for (code_index=0; code_index<iseq_size;) {
12855 const VALUE insn = orig_code[code_index++];
12856 const char *types = insn_op_types(insn);
12861 ibf_dump_write_small_value(dump, insn);
12864 for (op_index=0; types[op_index]; op_index++, code_index++) {
12865 VALUE op = orig_code[code_index];
12868 switch (types[op_index]) {
12871 wv = ibf_dump_object(dump, op);
12880 wv = ibf_dump_object(dump, arr);
12888 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
12896 wv = ibf_dump_id(dump, (
ID)op);
12908 ibf_dump_write_small_value(dump, wv);
12918ibf_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)
12921 unsigned int code_index;
12922 ibf_offset_t reading_pos = bytecode_offset;
12926 struct rb_call_data *cd_entries = load_body->call_data;
12929 iseq_bits_t * mark_offset_bits;
12931 iseq_bits_t tmp[1] = {0};
12933 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
12934 mark_offset_bits = tmp;
12937 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
12939 bool needs_bitmap =
false;
12941 for (code_index=0; code_index<iseq_size;) {
12943 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
12944 const char *types = insn_op_types(insn);
12950 for (op_index=0; types[op_index]; op_index++, code_index++) {
12951 const char operand_type = types[op_index];
12952 switch (operand_type) {
12955 VALUE op = ibf_load_small_value(load, &reading_pos);
12956 VALUE v = ibf_load_object(load, op);
12957 code[code_index] = v;
12960 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12961 needs_bitmap =
true;
12967 VALUE op = ibf_load_small_value(load, &reading_pos);
12968 VALUE v = ibf_load_object(load, op);
12969 v = rb_hash_dup(v);
12970 RHASH_TBL_RAW(v)->type = &cdhash_type;
12972 freeze_hide_obj(v);
12977 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
12979 code[code_index] = v;
12980 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12982 needs_bitmap =
true;
12987 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
12989 code[code_index] = v;
12992 ISEQ_MBITS_SET(mark_offset_bits, code_index);
12993 needs_bitmap =
true;
12999 VALUE op = ibf_load_small_value(load, &reading_pos);
13000 VALUE arr = ibf_load_object(load, op);
13002 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
13003 ic->
segments = array_to_idlist(arr);
13005 code[code_index] = (
VALUE)ic;
13012 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13014 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
13015 code[code_index] = (
VALUE)ic;
13017 if (operand_type == TS_IVC) {
13020 if (insn == BIN(setinstancevariable)) {
13021 ID iv_name = (
ID)code[code_index - 1];
13022 cache->iv_set_name = iv_name;
13025 cache->iv_set_name = 0;
13028 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
13035 code[code_index] = (
VALUE)cd_entries++;
13040 VALUE op = ibf_load_small_value(load, &reading_pos);
13041 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
13048 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
13051 code[code_index] = ibf_load_small_value(load, &reading_pos);
13055 if (insn_len(insn) != op_index+1) {
13060 load_body->iseq_encoded = code;
13061 load_body->iseq_size = code_index;
13063 if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
13064 load_body->mark_bits.single = mark_offset_bits[0];
13067 if (needs_bitmap) {
13068 load_body->mark_bits.list = mark_offset_bits;
13071 load_body->mark_bits.list = 0;
13072 ruby_xfree(mark_offset_bits);
13077 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
13084 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
13087 IBF_W_ALIGN(
VALUE);
13088 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
13091 return ibf_dump_pos(dump);
13096ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
13100 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
13111 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
13114 struct rb_iseq_param_keyword dump_kw = *kw;
13115 int dv_num = kw->num - kw->required_num;
13120 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
13121 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
13123 dump_kw.table = IBF_W(ids,
ID, kw->num);
13124 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
13125 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
13126 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
13133static const struct rb_iseq_param_keyword *
13134ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
13136 if (param_keyword_offset) {
13137 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
13138 int dv_num = kw->num - kw->required_num;
13139 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
13142 for (i=0; i<dv_num; i++) {
13143 dvs[i] = ibf_load_object(load, dvs[i]);
13149 kw->default_values = dvs;
13160 ibf_offset_t offset = ibf_dump_pos(dump);
13164 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
13165 ibf_dump_write_small_value(dump, entries[i].line_no);
13166#ifdef USE_ISEQ_NODE_ID
13167 ibf_dump_write_small_value(dump, entries[i].node_id);
13169 ibf_dump_write_small_value(dump, entries[i].events);
13176ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
13178 ibf_offset_t reading_pos = body_offset;
13182 for (i = 0; i < size; i++) {
13183 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
13184#ifdef USE_ISEQ_NODE_ID
13185 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
13187 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
13194ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
13196 ibf_offset_t offset = ibf_dump_pos(dump);
13198 unsigned int last = 0;
13200 for (i = 0; i < size; i++) {
13201 ibf_dump_write_small_value(dump, positions[i] - last);
13202 last = positions[i];
13208static unsigned int *
13209ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
13211 ibf_offset_t reading_pos = positions_offset;
13212 unsigned int *positions =
ALLOC_N(
unsigned int, size);
13214 unsigned int last = 0;
13216 for (i = 0; i < size; i++) {
13217 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
13218 last = positions[i];
13228 const int size = body->local_table_size;
13232 for (i=0; i<size; i++) {
13233 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13236 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13242 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
13246ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13249 ID *table = IBF_R(local_table_offset,
ID, size);
13252 for (i=0; i<size; i++) {
13253 table[i] = ibf_load_id(load, table[i]);
13256 if (size == 1 && table[0] == idERROR_INFO) {
13258 return rb_iseq_shared_exc_local_tbl;
13273 const int size = body->local_table_size;
13274 IBF_W_ALIGN(
enum lvar_state);
13275 return ibf_dump_write(dump, body->lvar_states,
sizeof(
enum lvar_state) * (body->lvar_states ? size : 0));
13278static enum lvar_state *
13279ibf_load_lvar_states(
const struct ibf_load *load, ibf_offset_t lvar_states_offset,
int size,
const ID *local_table)
13281 if (local_table == rb_iseq_shared_exc_local_tbl ||
13286 enum lvar_state *states = IBF_R(lvar_states_offset,
enum lvar_state, size);
13297 int *iseq_indices =
ALLOCA_N(
int, table->size);
13300 for (i=0; i<table->size; i++) {
13301 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13304 const ibf_offset_t offset = ibf_dump_pos(dump);
13306 for (i=0; i<table->size; i++) {
13307 ibf_dump_write_small_value(dump, iseq_indices[i]);
13308 ibf_dump_write_small_value(dump, table->entries[i].type);
13309 ibf_dump_write_small_value(dump, table->entries[i].start);
13310 ibf_dump_write_small_value(dump, table->entries[i].end);
13311 ibf_dump_write_small_value(dump, table->entries[i].cont);
13312 ibf_dump_write_small_value(dump, table->entries[i].sp);
13317 return ibf_dump_pos(dump);
13322ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size,
const rb_iseq_t *parent_iseq)
13325 struct iseq_catch_table *table = ruby_xcalloc(1, iseq_catch_table_bytes(size));
13326 table->size = size;
13327 ISEQ_BODY(parent_iseq)->catch_table = table;
13329 ibf_offset_t reading_pos = catch_table_offset;
13332 for (i=0; i<table->size; i++) {
13333 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13334 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13335 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13336 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13337 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13338 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13341 RB_OBJ_WRITE(parent_iseq, UNALIGNED_MEMBER_PTR(&table->entries[i], iseq), catch_iseq);
13345 ISEQ_BODY(parent_iseq)->catch_table = NULL;
13353 const unsigned int ci_size = body->ci_size;
13356 ibf_offset_t offset = ibf_dump_pos(dump);
13360 for (i = 0; i < ci_size; i++) {
13363 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13364 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13365 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13369 int len = kwarg->keyword_len;
13370 ibf_dump_write_small_value(dump,
len);
13371 for (
int j=0; j<
len; j++) {
13372 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13373 ibf_dump_write_small_value(dump, keyword);
13377 ibf_dump_write_small_value(dump, 0);
13382 ibf_dump_write_small_value(dump, (
VALUE)-1);
13400static enum rb_id_table_iterator_result
13401store_outer_variable(
ID id,
VALUE val,
void *dump)
13406 pair->name = rb_id2str(
id);
13408 return ID_TABLE_CONTINUE;
13412outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13420 else if (!bp->name) {
13430 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13432 ibf_offset_t offset = ibf_dump_pos(dump);
13434 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13435 ibf_dump_write_small_value(dump, (
VALUE)size);
13444 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13446 for (
size_t i = 0; i < size; ++i) {
13447 ID id = ovlist->pairs[i].id;
13448 ID val = ovlist->pairs[i].val;
13449 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13450 ibf_dump_write_small_value(dump, val);
13459ibf_load_ci_entries(
const struct ibf_load *load,
13460 ibf_offset_t ci_entries_offset,
13461 unsigned int ci_size,
13469 ibf_offset_t reading_pos = ci_entries_offset;
13476 for (i = 0; i < ci_size; i++) {
13477 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
13478 if (mid_index != (
VALUE)-1) {
13479 ID mid = ibf_load_id(load, mid_index);
13480 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13481 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13484 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
13487 kwarg->references = 0;
13488 kwarg->keyword_len = kwlen;
13489 for (
int j=0; j<kwlen; j++) {
13490 VALUE keyword = ibf_load_small_value(load, &reading_pos);
13491 kwarg->keywords[j] = ibf_load_object(load, keyword);
13495 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
13497 cds[i].cc = vm_cc_empty();
13508ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
13510 ibf_offset_t reading_pos = outer_variables_offset;
13514 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
13516 if (table_size > 0) {
13517 tbl = rb_id_table_create(table_size);
13520 for (
size_t i = 0; i < table_size; i++) {
13521 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
13522 VALUE value = ibf_load_small_value(load, &reading_pos);
13523 if (!key) key = rb_make_temporary_id(i);
13524 rb_id_table_insert(tbl, key, value);
13533 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
13535 unsigned int *positions;
13539 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
13540 const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
13541 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
13543#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13544 ibf_offset_t iseq_start = ibf_dump_pos(dump);
13549 buffer.obj_table = ibf_dump_object_table_new();
13550 dump->current_buffer = &buffer;
13553 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
13554 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
13555 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
13556 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
13557 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
13559 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
13560 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
13561 ruby_xfree(positions);
13563 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
13564 const ibf_offset_t lvar_states_offset = ibf_dump_lvar_states(dump, iseq);
13565 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
13566 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
13567 const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
13568 const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
13569 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
13570 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
13571 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
13573#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13574 ibf_offset_t local_obj_list_offset;
13575 unsigned int local_obj_list_size;
13577 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
13580 ibf_offset_t body_offset = ibf_dump_pos(dump);
13583 unsigned int param_flags =
13584 (body->
param.flags.has_lead << 0) |
13585 (body->
param.flags.has_opt << 1) |
13586 (body->
param.flags.has_rest << 2) |
13587 (body->
param.flags.has_post << 3) |
13588 (body->
param.flags.has_kw << 4) |
13589 (body->
param.flags.has_kwrest << 5) |
13590 (body->
param.flags.has_block << 6) |
13591 (body->
param.flags.ambiguous_param0 << 7) |
13592 (body->
param.flags.accepts_no_kwarg << 8) |
13593 (body->
param.flags.ruby2_keywords << 9) |
13594 (body->
param.flags.anon_rest << 10) |
13595 (body->
param.flags.anon_kwrest << 11) |
13596 (body->
param.flags.use_block << 12) |
13597 (body->
param.flags.forwardable << 13) ;
13599#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13600# define IBF_BODY_OFFSET(x) (x)
13602# define IBF_BODY_OFFSET(x) (body_offset - (x))
13605 ibf_dump_write_small_value(dump, body->type);
13606 ibf_dump_write_small_value(dump, body->iseq_size);
13607 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
13608 ibf_dump_write_small_value(dump, bytecode_size);
13609 ibf_dump_write_small_value(dump, param_flags);
13610 ibf_dump_write_small_value(dump, body->
param.size);
13611 ibf_dump_write_small_value(dump, body->
param.lead_num);
13612 ibf_dump_write_small_value(dump, body->
param.opt_num);
13613 ibf_dump_write_small_value(dump, body->
param.rest_start);
13614 ibf_dump_write_small_value(dump, body->
param.post_start);
13615 ibf_dump_write_small_value(dump, body->
param.post_num);
13616 ibf_dump_write_small_value(dump, body->
param.block_start);
13617 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
13618 ibf_dump_write_small_value(dump, param_keyword_offset);
13619 ibf_dump_write_small_value(dump, location_pathobj_index);
13620 ibf_dump_write_small_value(dump, location_base_label_index);
13621 ibf_dump_write_small_value(dump, location_label_index);
13622 ibf_dump_write_small_value(dump, body->location.first_lineno);
13623 ibf_dump_write_small_value(dump, body->location.node_id);
13624 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
13625 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
13626 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
13627 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
13628 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
13629 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
13630 ibf_dump_write_small_value(dump, body->insns_info.size);
13631 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
13632 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(lvar_states_offset));
13633 ibf_dump_write_small_value(dump, catch_table_size);
13634 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
13635 ibf_dump_write_small_value(dump, parent_iseq_index);
13636 ibf_dump_write_small_value(dump, local_iseq_index);
13637 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
13638 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
13639 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
13640 ibf_dump_write_small_value(dump, body->variable.flip_count);
13641 ibf_dump_write_small_value(dump, body->local_table_size);
13642 ibf_dump_write_small_value(dump, body->ivc_size);
13643 ibf_dump_write_small_value(dump, body->icvarc_size);
13644 ibf_dump_write_small_value(dump, body->ise_size);
13645 ibf_dump_write_small_value(dump, body->ic_size);
13646 ibf_dump_write_small_value(dump, body->ci_size);
13647 ibf_dump_write_small_value(dump, body->stack_max);
13648 ibf_dump_write_small_value(dump, body->builtin_attrs);
13649 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
13651#undef IBF_BODY_OFFSET
13653#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13654 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
13656 dump->current_buffer = saved_buffer;
13657 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
13659 ibf_offset_t offset = ibf_dump_pos(dump);
13660 ibf_dump_write_small_value(dump, iseq_start);
13661 ibf_dump_write_small_value(dump, iseq_length_bytes);
13662 ibf_dump_write_small_value(dump, body_offset);
13664 ibf_dump_write_small_value(dump, local_obj_list_offset);
13665 ibf_dump_write_small_value(dump, local_obj_list_size);
13667 st_free_table(buffer.obj_table);
13671 return body_offset;
13676ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
13678 VALUE str = ibf_load_object(load, str_index);
13680 str = rb_fstring(str);
13690 ibf_offset_t reading_pos = offset;
13692#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13694 load->current_buffer = &load->global_buffer;
13696 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13697 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13698 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13701 buffer.buff = load->global_buffer.buff + iseq_start;
13702 buffer.size = iseq_length_bytes;
13703 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13704 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13705 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
13707 load->current_buffer = &buffer;
13708 reading_pos = body_offset;
13711#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13712# define IBF_BODY_OFFSET(x) (x)
13714# define IBF_BODY_OFFSET(x) (offset - (x))
13717 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13718 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13719 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13720 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13721 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13722 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13723 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
13724 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
13725 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
13726 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
13727 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
13728 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
13729 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13730 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13731 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
13732 const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
13733 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
13734 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
13735 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
13736 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13737 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13738 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13739 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13740 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13741 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13742 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13743 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13744 const ibf_offset_t lvar_states_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13745 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13746 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13747 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13748 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13749 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13750 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13751 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13752 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
13753 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13755 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13756 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13757 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13758 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13760 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13761 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13762 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13763 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
13766 VALUE path = ibf_load_object(load, location_pathobj_index);
13771 realpath = path = rb_fstring(path);
13774 VALUE pathobj = path;
13780 if (!
NIL_P(realpath)) {
13782 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
13783 "(%x), path=%+"PRIsVALUE,
13784 realpath,
TYPE(realpath), path);
13786 realpath = rb_fstring(realpath);
13792 rb_iseq_pathobj_set(iseq, path, realpath);
13797 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
13799#undef IBF_BODY_OFFSET
13801 load_body->type =
type;
13802 load_body->stack_max = stack_max;
13803 load_body->
param.flags.has_lead = (param_flags >> 0) & 1;
13804 load_body->
param.flags.has_opt = (param_flags >> 1) & 1;
13805 load_body->
param.flags.has_rest = (param_flags >> 2) & 1;
13806 load_body->
param.flags.has_post = (param_flags >> 3) & 1;
13807 load_body->
param.flags.has_kw = FALSE;
13808 load_body->
param.flags.has_kwrest = (param_flags >> 5) & 1;
13809 load_body->
param.flags.has_block = (param_flags >> 6) & 1;
13810 load_body->
param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
13811 load_body->
param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
13812 load_body->
param.flags.ruby2_keywords = (param_flags >> 9) & 1;
13813 load_body->
param.flags.anon_rest = (param_flags >> 10) & 1;
13814 load_body->
param.flags.anon_kwrest = (param_flags >> 11) & 1;
13815 load_body->
param.flags.use_block = (param_flags >> 12) & 1;
13816 load_body->
param.flags.forwardable = (param_flags >> 13) & 1;
13817 load_body->
param.size = param_size;
13818 load_body->
param.lead_num = param_lead_num;
13819 load_body->
param.opt_num = param_opt_num;
13820 load_body->
param.rest_start = param_rest_start;
13821 load_body->
param.post_start = param_post_start;
13822 load_body->
param.post_num = param_post_num;
13823 load_body->
param.block_start = param_block_start;
13824 load_body->local_table_size = local_table_size;
13825 load_body->ci_size = ci_size;
13826 load_body->insns_info.size = insns_info_size;
13828 ISEQ_COVERAGE_SET(iseq,
Qnil);
13829 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
13830 load_body->variable.flip_count = variable_flip_count;
13831 load_body->variable.script_lines =
Qnil;
13833 load_body->location.first_lineno = location_first_lineno;
13834 load_body->location.node_id = location_node_id;
13835 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
13836 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
13837 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
13838 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
13839 load_body->builtin_attrs = builtin_attrs;
13840 load_body->prism = prism;
13842 load_body->ivc_size = ivc_size;
13843 load_body->icvarc_size = icvarc_size;
13844 load_body->ise_size = ise_size;
13845 load_body->ic_size = ic_size;
13847 if (ISEQ_IS_SIZE(load_body)) {
13851 load_body->is_entries = NULL;
13853 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
13854 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
13855 load_body->
param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
13856 load_body->
param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
13857 load_body->
param.flags.has_kw = (param_flags >> 4) & 1;
13858 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
13859 load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
13860 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
13861 load_body->lvar_states = ibf_load_lvar_states(load, lvar_states_offset, local_table_size, load_body->local_table);
13862 ibf_load_catch_table(load, catch_table_offset, catch_table_size, iseq);
13866 const rb_iseq_t *mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
13868 RB_OBJ_WRITE(iseq, &load_body->parent_iseq, parent_iseq);
13869 RB_OBJ_WRITE(iseq, &load_body->local_iseq, local_iseq);
13870 RB_OBJ_WRITE(iseq, &load_body->mandatory_only_iseq, mandatory_only_iseq);
13873 if (load_body->
param.keyword != NULL) {
13875 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->
param.keyword;
13876 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
13879 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
13880#if VM_INSN_INFO_TABLE_IMPL == 2
13881 rb_iseq_insns_info_encode_positions(iseq);
13884 rb_iseq_translate_threaded_code(iseq);
13886#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13887 load->current_buffer = &load->global_buffer;
13890 RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
13891 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
13893#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13894 load->current_buffer = saved_buffer;
13896 verify_call_cache(iseq);
13899 rb_vm_pop_frame_no_int(ec);
13909ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
13914 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
13917 return ST_CONTINUE;
13927 args.offset_list = offset_list;
13929 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
13932 st_index_t size = dump->iseq_table->num_entries;
13933 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
13935 for (i = 0; i < size; i++) {
13939 ibf_dump_align(dump,
sizeof(ibf_offset_t));
13940 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
13941 header->iseq_list_size = (
unsigned int)size;
13944#define IBF_OBJECT_INTERNAL FL_PROMOTED0
13953 unsigned int type: 5;
13954 unsigned int special_const: 1;
13955 unsigned int frozen: 1;
13956 unsigned int internal: 1;
13959enum ibf_object_class_index {
13960 IBF_OBJECT_CLASS_OBJECT,
13961 IBF_OBJECT_CLASS_ARRAY,
13962 IBF_OBJECT_CLASS_STANDARD_ERROR,
13963 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
13964 IBF_OBJECT_CLASS_TYPE_ERROR,
13965 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
13975 long keyval[FLEX_ARY_LEN];
13988 BDIGIT digits[FLEX_ARY_LEN];
13991enum ibf_object_data_type {
13992 IBF_OBJECT_DATA_ENCODING,
14003#define IBF_ALIGNED_OFFSET(align, offset) \
14004 ((((offset) - 1) / (align) + 1) * (align))
14009#define IBF_OBJBODY(type, offset) \
14010 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
14013ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
14015 if (offset >= load->current_buffer->size) {
14016 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
14018 return load->current_buffer->buff + offset;
14021NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
14024ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
14027 rb_raw_obj_info(buff,
sizeof(buff), obj);
14036 rb_raise(rb_eArgError,
"unsupported");
14043 enum ibf_object_class_index cindex;
14044 if (obj == rb_cObject) {
14045 cindex = IBF_OBJECT_CLASS_OBJECT;
14048 cindex = IBF_OBJECT_CLASS_ARRAY;
14051 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
14054 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
14057 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
14060 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
14063 rb_obj_info_dump(obj);
14065 rb_bug(
"unsupported class");
14067 ibf_dump_write_small_value(dump, (
VALUE)cindex);
14073 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
14076 case IBF_OBJECT_CLASS_OBJECT:
14078 case IBF_OBJECT_CLASS_ARRAY:
14080 case IBF_OBJECT_CLASS_STANDARD_ERROR:
14082 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
14084 case IBF_OBJECT_CLASS_TYPE_ERROR:
14086 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
14090 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
14098 (void)IBF_W(&dbl,
double, 1);
14106 memcpy(&d, IBF_OBJBODY(
double, offset),
sizeof(d));
14113 long encindex = (long)rb_enc_get_index(obj);
14114 long len = RSTRING_LEN(obj);
14115 const char *ptr = RSTRING_PTR(obj);
14117 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14118 rb_encoding *enc = rb_enc_from_index((
int)encindex);
14119 const char *enc_name = rb_enc_name(enc);
14120 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
14123 ibf_dump_write_small_value(dump, encindex);
14124 ibf_dump_write_small_value(dump,
len);
14125 IBF_WP(ptr,
char,
len);
14131 ibf_offset_t reading_pos = offset;
14133 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14134 const long len = (long)ibf_load_small_value(load, &reading_pos);
14135 const char *ptr = load->current_buffer->buff + reading_pos;
14137 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14138 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14139 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14143 if (header->frozen && !header->internal) {
14144 str = rb_enc_literal_str(ptr,
len, rb_enc_from_index(encindex));
14147 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
14150 if (header->frozen) str = rb_fstring(str);
14161 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
14163 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
14164 ibf_dump_write_small_value(dump, regexp.srcstr);
14171 regexp.option = ibf_load_byte(load, &offset);
14172 regexp.srcstr = ibf_load_small_value(load, &offset);
14174 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
14175 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
14187 ibf_dump_write_small_value(dump,
len);
14188 for (i=0; i<
len; i++) {
14189 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
14190 ibf_dump_write_small_value(dump, index);
14197 ibf_offset_t reading_pos = offset;
14199 const long len = (long)ibf_load_small_value(load, &reading_pos);
14204 for (i=0; i<
len; i++) {
14205 const VALUE index = ibf_load_small_value(load, &reading_pos);
14215ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
14219 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14220 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14222 ibf_dump_write_small_value(dump, key_index);
14223 ibf_dump_write_small_value(dump, val_index);
14224 return ST_CONTINUE;
14231 ibf_dump_write_small_value(dump, (
VALUE)
len);
14239 long len = (long)ibf_load_small_value(load, &offset);
14240 VALUE obj = rb_hash_new_with_size(
len);
14243 for (i = 0; i <
len; i++) {
14244 VALUE key_index = ibf_load_small_value(load, &offset);
14245 VALUE val_index = ibf_load_small_value(load, &offset);
14247 VALUE key = ibf_load_object(load, key_index);
14248 VALUE val = ibf_load_object(load, val_index);
14249 rb_hash_aset(obj, key, val);
14251 rb_hash_rehash(obj);
14267 range.class_index = 0;
14270 range.beg = (long)ibf_dump_object(dump, beg);
14271 range.end = (long)ibf_dump_object(dump, end);
14277 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
14286 VALUE beg = ibf_load_object(load, range->beg);
14287 VALUE end = ibf_load_object(load, range->end);
14297 ssize_t
len = BIGNUM_LEN(obj);
14298 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14299 BDIGIT *d = BIGNUM_DIGITS(obj);
14301 (void)IBF_W(&slen, ssize_t, 1);
14302 IBF_WP(d, BDIGIT,
len);
14309 int sign = bignum->slen > 0;
14310 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14311 const int big_unpack_flags =
14314 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
14325 if (rb_data_is_encoding(obj)) {
14327 const char *name = rb_enc_name(enc);
14328 long len = strlen(name) + 1;
14330 data[0] = IBF_OBJECT_DATA_ENCODING;
14332 (void)IBF_W(data,
long, 2);
14333 IBF_WP(name,
char,
len);
14336 ibf_dump_object_unsupported(dump, obj);
14343 const long *body = IBF_OBJBODY(
long, offset);
14344 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14346 const char *data = (
const char *)&body[2];
14349 case IBF_OBJECT_DATA_ENCODING:
14351 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
14356 return ibf_load_object_unsupported(load, header, offset);
14360ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14363 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14364 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14366 (void)IBF_W(data,
long, 2);
14370ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14373 VALUE a = ibf_load_object(load, nums->a);
14374 VALUE b = ibf_load_object(load, nums->b);
14386 ibf_dump_object_string(dump,
rb_sym2str(obj));
14392 ibf_offset_t reading_pos = offset;
14394 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14395 const long len = (long)ibf_load_small_value(load, &reading_pos);
14396 const char *ptr = load->current_buffer->buff + reading_pos;
14398 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14399 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14400 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14403 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
14407typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
14408static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
14409 ibf_dump_object_unsupported,
14410 ibf_dump_object_unsupported,
14411 ibf_dump_object_class,
14412 ibf_dump_object_unsupported,
14413 ibf_dump_object_float,
14414 ibf_dump_object_string,
14415 ibf_dump_object_regexp,
14416 ibf_dump_object_array,
14417 ibf_dump_object_hash,
14418 ibf_dump_object_struct,
14419 ibf_dump_object_bignum,
14420 ibf_dump_object_unsupported,
14421 ibf_dump_object_data,
14422 ibf_dump_object_unsupported,
14423 ibf_dump_object_complex_rational,
14424 ibf_dump_object_complex_rational,
14425 ibf_dump_object_unsupported,
14426 ibf_dump_object_unsupported,
14427 ibf_dump_object_unsupported,
14428 ibf_dump_object_unsupported,
14429 ibf_dump_object_symbol,
14430 ibf_dump_object_unsupported,
14431 ibf_dump_object_unsupported,
14432 ibf_dump_object_unsupported,
14433 ibf_dump_object_unsupported,
14434 ibf_dump_object_unsupported,
14435 ibf_dump_object_unsupported,
14436 ibf_dump_object_unsupported,
14437 ibf_dump_object_unsupported,
14438 ibf_dump_object_unsupported,
14439 ibf_dump_object_unsupported,
14440 ibf_dump_object_unsupported,
14446 unsigned char byte =
14447 (header.type << 0) |
14448 (header.special_const << 5) |
14449 (header.frozen << 6) |
14450 (header.internal << 7);
14456ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
14458 unsigned char byte = ibf_load_byte(load, offset);
14461 header.type = (
byte >> 0) & 0x1f;
14462 header.special_const = (
byte >> 5) & 0x01;
14463 header.frozen = (
byte >> 6) & 0x01;
14464 header.internal = (
byte >> 7) & 0x01;
14473 ibf_offset_t current_offset;
14474 IBF_ZERO(obj_header);
14475 obj_header.type =
TYPE(obj);
14477 IBF_W_ALIGN(ibf_offset_t);
14478 current_offset = ibf_dump_pos(dump);
14483 obj_header.special_const = TRUE;
14484 obj_header.frozen = TRUE;
14485 obj_header.internal = TRUE;
14486 ibf_dump_object_object_header(dump, obj_header);
14487 ibf_dump_write_small_value(dump, obj);
14491 obj_header.special_const = FALSE;
14492 obj_header.frozen =
OBJ_FROZEN(obj) ? TRUE : FALSE;
14493 ibf_dump_object_object_header(dump, obj_header);
14494 (*dump_object_functions[obj_header.type])(dump, obj);
14497 return current_offset;
14501static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
14502 ibf_load_object_unsupported,
14503 ibf_load_object_unsupported,
14504 ibf_load_object_class,
14505 ibf_load_object_unsupported,
14506 ibf_load_object_float,
14507 ibf_load_object_string,
14508 ibf_load_object_regexp,
14509 ibf_load_object_array,
14510 ibf_load_object_hash,
14511 ibf_load_object_struct,
14512 ibf_load_object_bignum,
14513 ibf_load_object_unsupported,
14514 ibf_load_object_data,
14515 ibf_load_object_unsupported,
14516 ibf_load_object_complex_rational,
14517 ibf_load_object_complex_rational,
14518 ibf_load_object_unsupported,
14519 ibf_load_object_unsupported,
14520 ibf_load_object_unsupported,
14521 ibf_load_object_unsupported,
14522 ibf_load_object_symbol,
14523 ibf_load_object_unsupported,
14524 ibf_load_object_unsupported,
14525 ibf_load_object_unsupported,
14526 ibf_load_object_unsupported,
14527 ibf_load_object_unsupported,
14528 ibf_load_object_unsupported,
14529 ibf_load_object_unsupported,
14530 ibf_load_object_unsupported,
14531 ibf_load_object_unsupported,
14532 ibf_load_object_unsupported,
14533 ibf_load_object_unsupported,
14537ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
14539 if (object_index == 0) {
14543 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
14545 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
14546 ibf_offset_t offset = offsets[object_index];
14547 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
14550 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
14551 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
14552 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
14553 header.type, header.special_const, header.frozen, header.internal);
14555 if (offset >= load->current_buffer->size) {
14556 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
14559 if (header.special_const) {
14560 ibf_offset_t reading_pos = offset;
14562 obj = ibf_load_small_value(load, &reading_pos);
14565 obj = (*load_object_functions[header.type])(load, &header, offset);
14568 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
14571 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
14572 object_index, obj);
14585ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14590 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
14593 return ST_CONTINUE;
14597ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
14599 st_table *obj_table = dump->current_buffer->obj_table;
14604 args.offset_list = offset_list;
14606 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
14608 IBF_W_ALIGN(ibf_offset_t);
14609 *obj_list_offset = ibf_dump_pos(dump);
14611 st_index_t size = obj_table->num_entries;
14614 for (i=0; i<size; i++) {
14619 *obj_list_size = (
unsigned int)size;
14623ibf_dump_mark(
void *ptr)
14626 rb_gc_mark(dump->global_buffer.str);
14628 rb_mark_set(dump->global_buffer.obj_table);
14629 rb_mark_set(dump->iseq_table);
14633ibf_dump_free(
void *ptr)
14636 if (dump->global_buffer.obj_table) {
14637 st_free_table(dump->global_buffer.obj_table);
14638 dump->global_buffer.obj_table = 0;
14640 if (dump->iseq_table) {
14641 st_free_table(dump->iseq_table);
14642 dump->iseq_table = 0;
14647ibf_dump_memsize(
const void *ptr)
14651 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
14652 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
14658 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
14659 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
14665 dump->global_buffer.obj_table = NULL;
14666 dump->iseq_table = NULL;
14669 dump->global_buffer.obj_table = ibf_dump_object_table_new();
14670 dump->iseq_table = st_init_numtable();
14672 dump->current_buffer = &dump->global_buffer;
14683 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
14684 ISEQ_BODY(iseq)->local_iseq != iseq) {
14687 if (
RTEST(ISEQ_COVERAGE(iseq))) {
14692 ibf_dump_setup(dump, dump_obj);
14694 ibf_dump_write(dump, &header,
sizeof(header));
14695 ibf_dump_iseq(dump, iseq);
14697 header.magic[0] =
'Y';
14698 header.magic[1] =
'A';
14699 header.magic[2] =
'R';
14700 header.magic[3] =
'B';
14701 header.major_version = IBF_MAJOR_VERSION;
14702 header.minor_version = IBF_MINOR_VERSION;
14703 header.endian = IBF_ENDIAN_MARK;
14705 ibf_dump_iseq_list(dump, &header);
14706 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
14707 header.size = ibf_dump_pos(dump);
14710 VALUE opt_str = opt;
14713 ibf_dump_write(dump, ptr, header.extra_size);
14716 header.extra_size = 0;
14719 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
14721 str = dump->global_buffer.str;
14726static const ibf_offset_t *
14727ibf_iseq_list(
const struct ibf_load *load)
14729 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
14733rb_ibf_load_iseq_complete(
rb_iseq_t *iseq)
14737 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
14740 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
14741 iseq->aux.loader.index, offset,
14742 load->header->size);
14744 ibf_load_iseq_each(load, iseq, offset);
14745 ISEQ_COMPILE_DATA_CLEAR(iseq);
14747 rb_iseq_init_trace(iseq);
14748 load->iseq = prev_src_iseq;
14755 rb_ibf_load_iseq_complete((
rb_iseq_t *)iseq);
14763 int iseq_index = (int)(
VALUE)index_iseq;
14766 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
14767 (
void *)index_iseq, (
void *)load->iseq_list);
14769 if (iseq_index == -1) {
14773 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
14776 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
14784 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
14787 iseq->aux.loader.obj = load->loader_obj;
14788 iseq->aux.loader.index = iseq_index;
14790 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
14791 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
14793 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
14795 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
14797 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
14799 rb_ibf_load_iseq_complete(iseq);
14803 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
14804 (
void *)iseq, (
void *)load->iseq);
14812ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
14815 load->loader_obj = loader_obj;
14816 load->global_buffer.buff = bytes;
14817 load->header = header;
14818 load->global_buffer.size = header->size;
14819 load->global_buffer.obj_list_offset = header->global_object_list_offset;
14820 load->global_buffer.obj_list_size = header->global_object_list_size;
14821 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
14822 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
14825 load->current_buffer = &load->global_buffer;
14827 if (size < header->size) {
14830 if (strncmp(header->magic,
"YARB", 4) != 0) {
14833 if (header->major_version != IBF_MAJOR_VERSION ||
14834 header->minor_version != IBF_MINOR_VERSION) {
14836 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
14838 if (header->endian != IBF_ENDIAN_MARK) {
14844 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14845 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
14846 header->iseq_list_offset);
14848 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14849 rb_raise(rb_eArgError,
"unaligned object list offset: %u",
14850 load->global_buffer.obj_list_offset);
14863 if (USE_LAZY_LOAD) {
14864 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
14867 ibf_load_setup_bytes(load, loader_obj, RSTRING_PTR(str), RSTRING_LEN(str));
14872ibf_loader_mark(
void *ptr)
14875 rb_gc_mark(load->str);
14876 rb_gc_mark(load->iseq_list);
14877 rb_gc_mark(load->global_buffer.obj_list);
14881ibf_loader_free(
void *ptr)
14888ibf_loader_memsize(
const void *ptr)
14895 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
14896 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
14900rb_iseq_ibf_load(
VALUE str)
14906 ibf_load_setup(load, loader_obj, str);
14907 iseq = ibf_load_iseq(load, 0);
14914rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
14920 ibf_load_setup_bytes(load, loader_obj, bytes, size);
14921 iseq = ibf_load_iseq(load, 0);
14928rb_iseq_ibf_load_extra_data(
VALUE str)
14934 ibf_load_setup(load, loader_obj, str);
14935 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
14940#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::@157 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.