12#include "ruby/internal/config.h"
22#include "internal/array.h"
23#include "internal/compile.h"
24#include "internal/coverage.h"
25#include "internal/complex.h"
26#include "internal/encoding.h"
27#include "internal/error.h"
28#include "internal/gc.h"
29#include "internal/hash.h"
30#include "internal/io.h"
31#include "internal/numeric.h"
32#include "internal/object.h"
33#include "internal/rational.h"
34#include "internal/re.h"
35#include "internal/ruby_parser.h"
36#include "internal/symbol.h"
37#include "internal/thread.h"
38#include "internal/variable.h"
44#include "vm_callinfo.h"
50#include "insns_info.inc"
52#define FIXNUM_INC(n, i) ((n)+(INT2FIX(i)&~FIXNUM_FLAG))
86 unsigned int rescued: 2;
87 unsigned int unremovable: 1;
92 enum ruby_vminsn_type insn_id;
122 const void *ensure_node;
127const ID rb_iseq_shared_exc_local_tbl[] = {idERROR_INFO};
147#define compile_debug CPDEBUG
149#define compile_debug ISEQ_COMPILE_DATA(iseq)->option->debug_level
154#define compile_debug_print_indent(level) \
155 ruby_debug_print_indent((level), compile_debug, gl_node_level * 2)
157#define debugp(header, value) (void) \
158 (compile_debug_print_indent(1) && \
159 ruby_debug_print_value(1, compile_debug, (header), (value)))
161#define debugi(header, id) (void) \
162 (compile_debug_print_indent(1) && \
163 ruby_debug_print_id(1, compile_debug, (header), (id)))
165#define debugp_param(header, value) (void) \
166 (compile_debug_print_indent(1) && \
167 ruby_debug_print_value(1, compile_debug, (header), (value)))
169#define debugp_verbose(header, value) (void) \
170 (compile_debug_print_indent(2) && \
171 ruby_debug_print_value(2, compile_debug, (header), (value)))
173#define debugp_verbose_node(header, value) (void) \
174 (compile_debug_print_indent(10) && \
175 ruby_debug_print_value(10, compile_debug, (header), (value)))
177#define debug_node_start(node) ((void) \
178 (compile_debug_print_indent(1) && \
179 (ruby_debug_print_node(1, CPDEBUG, "", (const NODE *)(node)), gl_node_level)), \
182#define debug_node_end() gl_node_level --
186#define debugi(header, id) ((void)0)
187#define debugp(header, value) ((void)0)
188#define debugp_verbose(header, value) ((void)0)
189#define debugp_verbose_node(header, value) ((void)0)
190#define debugp_param(header, value) ((void)0)
191#define debug_node_start(node) ((void)0)
192#define debug_node_end() ((void)0)
195#if CPDEBUG > 1 || CPDEBUG < 0
197#define printf ruby_debug_printf
198#define debugs if (compile_debug_print_indent(1)) ruby_debug_printf
199#define debug_compile(msg, v) ((void)(compile_debug_print_indent(1) && fputs((msg), stderr)), (v))
201#define debugs if(0)printf
202#define debug_compile(msg, v) (v)
205#define LVAR_ERRINFO (1)
208#define NEW_LABEL(l) new_label_body(iseq, (l))
209#define LABEL_FORMAT "<L%03d>"
211#define NEW_ISEQ(node, name, type, line_no) \
212 new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
214#define NEW_CHILD_ISEQ(node, name, type, line_no) \
215 new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
217#define NEW_CHILD_ISEQ_WITH_CALLBACK(callback_func, name, type, line_no) \
218 new_child_iseq_with_callback(iseq, (callback_func), (name), iseq, (type), (line_no))
221#define ADD_SEQ(seq1, seq2) \
222 APPEND_LIST((seq1), (seq2))
225#define ADD_INSN(seq, line_node, insn) \
226 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 0))
229#define ADD_SYNTHETIC_INSN(seq, line_no, node_id, insn) \
230 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line_no), (node_id), BIN(insn), 0))
233#define INSERT_BEFORE_INSN(next, line_no, node_id, insn) \
234 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
237#define INSERT_AFTER_INSN(prev, line_no, node_id, insn) \
238 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
241#define ADD_INSN1(seq, line_node, insn, op1) \
242 ADD_ELEM((seq), (LINK_ELEMENT *) \
243 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 1, (VALUE)(op1)))
246#define INSERT_BEFORE_INSN1(next, line_no, node_id, insn, op1) \
247 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) \
248 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
251#define INSERT_AFTER_INSN1(prev, line_no, node_id, insn, op1) \
252 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) \
253 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
255#define LABEL_REF(label) ((label)->refcnt++)
258#define ADD_INSNL(seq, line_node, insn, label) (ADD_INSN1(seq, line_node, insn, label), LABEL_REF(label))
260#define ADD_INSN2(seq, line_node, insn, op1, op2) \
261 ADD_ELEM((seq), (LINK_ELEMENT *) \
262 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
264#define ADD_INSN3(seq, line_node, insn, op1, op2, op3) \
265 ADD_ELEM((seq), (LINK_ELEMENT *) \
266 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
269#define ADD_SEND(seq, line_node, id, argc) \
270 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
272#define ADD_SEND_WITH_FLAG(seq, line_node, id, argc, flag) \
273 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)(flag), NULL)
275#define ADD_SEND_WITH_BLOCK(seq, line_node, id, argc, block) \
276 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
278#define ADD_CALL_RECEIVER(seq, line_node) \
279 ADD_INSN((seq), (line_node), putself)
281#define ADD_CALL(seq, line_node, id, argc) \
282 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
284#define ADD_CALL_WITH_BLOCK(seq, line_node, id, argc, block) \
285 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
287#define ADD_SEND_R(seq, line_node, id, argc, block, flag, keywords) \
288 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)))
290#define ADD_TRACE(seq, event) \
291 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), 0))
292#define ADD_TRACE_WITH_DATA(seq, event, data) \
293 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), (data)))
295static void iseq_add_getlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
296static void iseq_add_setlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
298#define ADD_GETLOCAL(seq, line_node, idx, level) iseq_add_getlocal(iseq, (seq), (line_node), (idx), (level))
299#define ADD_SETLOCAL(seq, line_node, idx, level) iseq_add_setlocal(iseq, (seq), (line_node), (idx), (level))
302#define ADD_LABEL(seq, label) \
303 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
305#define APPEND_LABEL(seq, before, label) \
306 APPEND_ELEM((seq), (before), (LINK_ELEMENT *) (label))
308#define ADD_ADJUST(seq, line_node, label) \
309 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), nd_line(line_node)))
311#define ADD_ADJUST_RESTORE(seq, label) \
312 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
314#define LABEL_UNREMOVABLE(label) \
315 ((label) ? (LABEL_REF(label), (label)->unremovable=1) : 0)
316#define ADD_CATCH_ENTRY(type, ls, le, iseqv, lc) do { \
317 VALUE _e = rb_ary_new3(5, (type), \
318 (VALUE)(ls) | 1, (VALUE)(le) | 1, \
319 (VALUE)(iseqv), (VALUE)(lc) | 1); \
320 LABEL_UNREMOVABLE(ls); \
323 if (NIL_P(ISEQ_COMPILE_DATA(iseq)->catch_table_ary)) \
324 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, rb_ary_hidden_new(3)); \
325 rb_ary_push(ISEQ_COMPILE_DATA(iseq)->catch_table_ary, freeze_hide_obj(_e)); \
329#define COMPILE(anchor, desc, node) \
330 (debug_compile("== " desc "\n", \
331 iseq_compile_each(iseq, (anchor), (node), 0)))
334#define COMPILE_POPPED(anchor, desc, node) \
335 (debug_compile("== " desc "\n", \
336 iseq_compile_each(iseq, (anchor), (node), 1)))
339#define COMPILE_(anchor, desc, node, popped) \
340 (debug_compile("== " desc "\n", \
341 iseq_compile_each(iseq, (anchor), (node), (popped))))
343#define COMPILE_RECV(anchor, desc, node, recv) \
344 (private_recv_p(node) ? \
345 (ADD_INSN(anchor, node, putself), VM_CALL_FCALL) : \
346 COMPILE(anchor, desc, recv) ? 0 : -1)
348#define OPERAND_AT(insn, idx) \
349 (((INSN*)(insn))->operands[(idx)])
351#define INSN_OF(insn) \
352 (((INSN*)(insn))->insn_id)
354#define IS_INSN(link) ((link)->type == ISEQ_ELEMENT_INSN)
355#define IS_LABEL(link) ((link)->type == ISEQ_ELEMENT_LABEL)
356#define IS_ADJUST(link) ((link)->type == ISEQ_ELEMENT_ADJUST)
357#define IS_TRACE(link) ((link)->type == ISEQ_ELEMENT_TRACE)
358#define IS_INSN_ID(iobj, insn) (INSN_OF(iobj) == BIN(insn))
359#define IS_NEXT_INSN_ID(link, insn) \
360 ((link)->next && IS_INSN((link)->next) && IS_INSN_ID((link)->next, insn))
361#define IS_NEXT_NEXT_INSN_ID(link, insn) \
362 ((link)->next && IS_NEXT_INSN_ID((link)->next, insn))
367 if (!IS_INSN(link)) {
371 enum ruby_vminsn_type
type = INSN_OF(link);
374 type == BIN(putobject) ||
375 type == BIN(putspecialobject) ||
376 type == BIN(putnil) ||
377 type == BIN(putself) ||
378 type == BIN(duphash) ||
379 type == BIN(getinstancevariable) ||
380 type == BIN(getlocal) ||
381 type == BIN(opt_getconstant_path)
391append_compile_error(const
rb_iseq_t *iseq,
int line, const
char *fmt, ...)
393 VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
394 VALUE file = rb_iseq_path(iseq);
399 err = rb_syntax_error_append(err, file, line, -1, NULL, fmt, args);
401 if (
NIL_P(err_info)) {
402 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err);
405 else if (!err_info) {
416compile_bug(
rb_iseq_t *iseq,
int line,
const char *fmt, ...)
420 rb_report_bug_valist(rb_iseq_path(iseq), line, fmt, args);
426#define COMPILE_ERROR append_compile_error
428#define ERROR_ARGS_AT(n) iseq, nd_line(n),
429#define ERROR_ARGS ERROR_ARGS_AT(node)
431#define EXPECT_NODE(prefix, node, ndtype, errval) \
433 const NODE *error_node = (node); \
434 enum node_type error_type = nd_type(error_node); \
435 if (error_type != (ndtype)) { \
436 COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
437 prefix ": " #ndtype " is expected, but %s", \
438 ruby_node_name(error_type)); \
443#define EXPECT_NODE_NONULL(prefix, parent, ndtype, errval) \
445 COMPILE_ERROR(ERROR_ARGS_AT(parent) \
446 prefix ": must be " #ndtype ", but 0"); \
450#define UNKNOWN_NODE(prefix, node, errval) \
452 const NODE *error_node = (node); \
453 COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
454 ruby_node_name(nd_type(error_node))); \
461#define CHECK(sub) if (!(sub)) {BEFORE_RETURN;return COMPILE_NG;}
462#define NO_CHECK(sub) (void)(sub)
465#define DECL_ANCHOR(name) \
466 LINK_ANCHOR name[1] = {{{ISEQ_ELEMENT_ANCHOR,},&name[0].anchor}}
467#define INIT_ANCHOR(name) \
468 ((name->last = &name->anchor)->next = NULL)
471freeze_hide_obj(
VALUE obj)
474 RBASIC_CLEAR_CLASS(obj);
478#include "optinsn.inc"
479#if OPT_INSTRUCTIONS_UNIFICATION
480#include "optunifs.inc"
485#define ISEQ_ARG iseq,
486#define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
489#define ISEQ_ARG_DECLARE
493#define gl_node_level ISEQ_COMPILE_DATA(iseq)->node_level
499static int insn_data_length(
INSN *iobj);
500static int calc_sp_depth(
int depth,
INSN *iobj);
502static INSN *new_insn_body(
rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...);
515static int iseq_set_exception_local_table(
rb_iseq_t *iseq);
519static int iseq_set_exception_table(
rb_iseq_t *iseq);
520static int iseq_set_optargs_table(
rb_iseq_t *iseq);
521static int iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq);
524static int compile_hash(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped);
531verify_list(ISEQ_ARG_DECLARE
const char *info,
LINK_ANCHOR *
const anchor)
537 if (!compile_debug)
return;
539 list = anchor->anchor.next;
540 plist = &anchor->anchor;
542 if (plist != list->prev) {
549 if (anchor->last != plist && anchor->last != 0) {
554 rb_bug(
"list verify error: %08x (%s)", flag, info);
559#define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
566 VALUE *original = rb_iseq_original_iseq(iseq);
568 while (i < ISEQ_BODY(iseq)->iseq_size) {
569 VALUE insn = original[i];
570 const char *types = insn_op_types(insn);
572 for (
int j=0; types[j]; j++) {
573 if (types[j] == TS_CALLDATA) {
577 if (cc != vm_cc_empty()) {
579 rb_bug(
"call cache is not initialized by vm_cc_empty()");
586 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
587 struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
590 if (cc != NULL && cc != vm_cc_empty()) {
592 rb_bug(
"call cache is not initialized by vm_cc_empty()");
604 elem->prev = anchor->last;
605 anchor->last->next = elem;
607 verify_list(
"add", anchor);
617 elem->next = before->next;
618 elem->next->prev = elem;
620 if (before == anchor->last) anchor->last = elem;
621 verify_list(
"add", anchor);
624#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
625#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
629branch_coverage_valid_p(
rb_iseq_t *iseq,
int first_line)
631 if (!ISEQ_COVERAGE(iseq))
return 0;
632 if (!ISEQ_BRANCH_COVERAGE(iseq))
return 0;
633 if (first_line <= 0)
return 0;
640 const int first_lineno = loc->beg_pos.lineno, first_column = loc->beg_pos.column;
641 const int last_lineno = loc->end_pos.lineno, last_column = loc->end_pos.column;
644 rb_hash_aset(structure, key, branch);
656 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return Qundef;
669 uint64_t source_hash = ISEQ_BODY(iseq)->source_hash;
670 VALUE key = rb_ary_new_from_args(4,
671 ULONG2NUM((
unsigned long)(source_hash >> 32)),
672 ULONG2NUM((
unsigned long)(source_hash & 0xffffffff)),
678 VALUE branch_base = rb_hash_aref(structure, key);
681 if (
NIL_P(branch_base)) {
682 branch_base = setup_branch(loc,
type, structure, key);
683 branches = rb_hash_new();
695generate_dummy_line_node(
int lineno,
int node_id)
698 nd_set_line(&dummy, lineno);
699 nd_set_node_id(&dummy, node_id);
706 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return;
717 VALUE branch = rb_hash_aref(branches, key);
721 branch = setup_branch(loc,
type, branches, key);
731 ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
732 ADD_SYNTHETIC_INSN(seq, loc->end_pos.lineno, node_id, nop);
735#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
738validate_label(st_data_t name, st_data_t label, st_data_t arg)
742 if (!lobj->link.next) {
744 COMPILE_ERROR(iseq, lobj->position,
745 "%"PRIsVALUE
": undefined label",
755 st_foreach(labels_table, validate_label, (st_data_t)iseq);
756 st_free_table(labels_table);
760get_nd_recv(
const NODE *node)
762 switch (nd_type(node)) {
764 return RNODE_CALL(node)->nd_recv;
766 return RNODE_OPCALL(node)->nd_recv;
770 return RNODE_QCALL(node)->nd_recv;
774 return RNODE_ATTRASGN(node)->nd_recv;
776 return RNODE_OP_ASGN1(node)->nd_recv;
778 return RNODE_OP_ASGN2(node)->nd_recv;
780 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
785get_node_call_nd_mid(
const NODE *node)
787 switch (nd_type(node)) {
789 return RNODE_CALL(node)->nd_mid;
791 return RNODE_OPCALL(node)->nd_mid;
793 return RNODE_FCALL(node)->nd_mid;
795 return RNODE_QCALL(node)->nd_mid;
797 return RNODE_VCALL(node)->nd_mid;
799 return RNODE_ATTRASGN(node)->nd_mid;
801 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
806get_nd_args(
const NODE *node)
808 switch (nd_type(node)) {
810 return RNODE_CALL(node)->nd_args;
812 return RNODE_OPCALL(node)->nd_args;
814 return RNODE_FCALL(node)->nd_args;
816 return RNODE_QCALL(node)->nd_args;
820 return RNODE_ATTRASGN(node)->nd_args;
822 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
827get_node_colon_nd_mid(
const NODE *node)
829 switch (nd_type(node)) {
831 return RNODE_COLON2(node)->nd_mid;
833 return RNODE_COLON3(node)->nd_mid;
835 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
840get_nd_vid(
const NODE *node)
842 switch (nd_type(node)) {
844 return RNODE_LASGN(node)->nd_vid;
846 return RNODE_DASGN(node)->nd_vid;
848 return RNODE_IASGN(node)->nd_vid;
850 return RNODE_CVASGN(node)->nd_vid;
852 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
857get_nd_value(
const NODE *node)
859 switch (nd_type(node)) {
861 return RNODE_LASGN(node)->nd_value;
863 return RNODE_DASGN(node)->nd_value;
865 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
870get_string_value(
const NODE *node)
872 switch (nd_type(node)) {
874 return RB_OBJ_SET_SHAREABLE(rb_node_str_string_val(node));
876 return RB_OBJ_SET_SHAREABLE(rb_node_file_path_val(node));
878 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
888 (*ifunc->func)(iseq, ret, ifunc->data);
890 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
892 CHECK(iseq_setup_insn(iseq, ret));
893 return iseq_setup(iseq, ret);
896static bool drop_unreachable_return(
LINK_ANCHOR *ret);
905 NO_CHECK(COMPILE(ret,
"nil", node));
906 iseq_set_local_table(iseq, 0, 0);
909 else if (nd_type_p(node, NODE_SCOPE)) {
911 iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl, (
NODE *)RNODE_SCOPE(node)->nd_args);
912 iseq_set_arguments(iseq, ret, (
NODE *)RNODE_SCOPE(node)->nd_args);
913 iseq_set_parameters_lvar_state(iseq);
915 switch (ISEQ_BODY(iseq)->
type) {
916 case ISEQ_TYPE_BLOCK:
918 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
919 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
921 start->rescued = LABEL_RESCUE_BEG;
922 end->rescued = LABEL_RESCUE_END;
925 ADD_SYNTHETIC_INSN(ret, ISEQ_BODY(iseq)->location.first_lineno, -1, nop);
926 ADD_LABEL(ret, start);
927 CHECK(COMPILE(ret,
"block body", RNODE_SCOPE(node)->nd_body));
930 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
933 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
934 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
937 case ISEQ_TYPE_CLASS:
940 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
942 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
945 case ISEQ_TYPE_METHOD:
947 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
949 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
950 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
952 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
956 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
963#define INVALID_ISEQ_TYPE(type) \
964 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
965 switch (ISEQ_BODY(iseq)->
type) {
966 case INVALID_ISEQ_TYPE(
METHOD);
967 case INVALID_ISEQ_TYPE(CLASS);
968 case INVALID_ISEQ_TYPE(BLOCK);
969 case INVALID_ISEQ_TYPE(EVAL);
970 case INVALID_ISEQ_TYPE(MAIN);
971 case INVALID_ISEQ_TYPE(TOP);
972#undef INVALID_ISEQ_TYPE
973 case ISEQ_TYPE_RESCUE:
974 iseq_set_exception_local_table(iseq);
975 CHECK(COMPILE(ret,
"rescue", node));
977 case ISEQ_TYPE_ENSURE:
978 iseq_set_exception_local_table(iseq);
979 CHECK(COMPILE_POPPED(ret,
"ensure", node));
981 case ISEQ_TYPE_PLAIN:
982 CHECK(COMPILE(ret,
"ensure", node));
985 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
988 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
993 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
994 NODE dummy_line_node = generate_dummy_line_node(0, -1);
995 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
996 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
998 else if (!drop_unreachable_return(ret)) {
999 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
1003 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
1004 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
1005 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
1006 validate_labels(iseq, labels_table);
1009 CHECK(iseq_setup_insn(iseq, ret));
1010 return iseq_setup(iseq, ret);
1014rb_iseq_translate_threaded_code(
rb_iseq_t *iseq)
1016#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1017 const void *
const *table = rb_vm_get_insns_address_table();
1019 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
1021 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1022 int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
1023 int len = insn_len(insn);
1024 encoded[i] = (
VALUE)table[insn];
1031 rb_yjit_live_iseq_count++;
1032 rb_yjit_iseq_alloc_count++;
1039rb_iseq_original_iseq(
const rb_iseq_t *iseq)
1044 if (original_code)
return original_code;
1045 original_code =
ALLOC_N(
VALUE, ISEQ_BODY(iseq)->iseq_size);
1046 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
1048#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1052 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1053 const void *addr = (
const void *)original_code[i];
1054 const int insn = rb_vm_insn_addr2insn(addr);
1056 original_code[i] = insn;
1057 i += insn_len(insn);
1064 v = rb_iseq_variable_ensure((
rb_iseq_t *)iseq);
1065 VALUE *prev = ATOMIC_PTR_CAS(v->original_iseq,
1066 NULL, original_code);
1068 SIZED_FREE_N(original_code, ISEQ_BODY(iseq)->iseq_size);
1071 return original_code;
1078#if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
1079# define ALIGNMENT_SIZE SIZEOF_LONG_LONG
1081# define ALIGNMENT_SIZE SIZEOF_VALUE
1083#define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
1085#define ALIGNMENT_SIZE_OF(type) alignment_size_assert(RUBY_ALIGNOF(type), #type)
1088alignment_size_assert(
size_t align,
const char *
type)
1091 "ALIGNMENT_SIZE_OF(%s):%zd == (2 ** N) is expected",
type, align);
1097calc_padding(
void *ptr,
size_t align)
1102 mis = (size_t)ptr & (align - 1);
1104 padding = align - mis;
1115 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], align);
1117 if (size >= INT_MAX - padding) rb_memerror();
1118 if (storage->pos + size + padding > storage->size) {
1119 unsigned int alloc_size = storage->size;
1121 while (alloc_size < size + PADDING_SIZE_MAX) {
1122 if (alloc_size >= INT_MAX / 2) rb_memerror();
1125 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
1127 storage = *arena = storage->next;
1130 storage->size = alloc_size;
1131 padding = calc_padding((
void *)&storage->buff[storage->pos], align);
1134 storage->pos += (int)padding;
1136 ptr = (
void *)&storage->buff[storage->pos];
1137 storage->pos += (int)size;
1142compile_data_alloc(
rb_iseq_t *iseq,
size_t size,
size_t align)
1145 return compile_data_alloc_with_arena(arena, size, align);
1148#define compile_data_alloc_type(iseq, type) \
1149 (type *)compile_data_alloc(iseq, sizeof(type), ALIGNMENT_SIZE_OF(type))
1152compile_data_alloc2(
rb_iseq_t *iseq,
size_t elsize,
size_t num,
size_t align)
1155 return compile_data_alloc(iseq, size, align);
1158#define compile_data_alloc2_type(iseq, type, num) \
1159 (type *)compile_data_alloc2(iseq, sizeof(type), num, ALIGNMENT_SIZE_OF(type))
1162compile_data_calloc2(
rb_iseq_t *iseq,
size_t elsize,
size_t num,
size_t align)
1165 void *p = compile_data_alloc(iseq, size, align);
1170#define compile_data_calloc2_type(iseq, type, num) \
1171 (type *)compile_data_calloc2(iseq, sizeof(type), num, ALIGNMENT_SIZE_OF(type))
1177 return (
INSN *)compile_data_alloc_with_arena(arena,
sizeof(
INSN), ALIGNMENT_SIZE_OF(
INSN));
1181compile_data_alloc_label(
rb_iseq_t *iseq)
1183 return compile_data_alloc_type(iseq,
LABEL);
1187compile_data_alloc_adjust(
rb_iseq_t *iseq)
1189 return compile_data_alloc_type(iseq,
ADJUST);
1193compile_data_alloc_trace(
rb_iseq_t *iseq)
1195 return compile_data_alloc_type(iseq,
TRACE);
1204 elem2->next = elem1->next;
1205 elem2->prev = elem1;
1206 elem1->next = elem2;
1208 elem2->next->prev = elem2;
1218 elem2->prev = elem1->prev;
1219 elem2->next = elem1;
1220 elem1->prev = elem2;
1222 elem2->prev->next = elem2;
1232 elem2->prev = elem1->prev;
1233 elem2->next = elem1->next;
1235 elem1->prev->next = elem2;
1238 elem1->next->prev = elem2;
1245 elem->prev->next = elem->next;
1247 elem->next->prev = elem->prev;
1260 first->prev->next = second;
1261 second->next->prev = first;
1263 first->next = second->next;
1264 second->next = first;
1266 second->prev = first->prev;
1267 first->prev = second;
1273 return anchor->anchor.next;
1279 return anchor->last;
1286 switch (elem->type) {
1287 case ISEQ_ELEMENT_INSN:
1288 case ISEQ_ELEMENT_ADJUST:
1298LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1300 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1301 if (first_insn != NULL &&
1302 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1311LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1313 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1331 if (anc2->anchor.next) {
1334 anc1->last->next = anc2->anchor.next;
1335 anc2->anchor.next->prev = anc1->last;
1336 anc1->last = anc2->last;
1341 verify_list(
"append", anc1);
1344#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1353 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1354 (
void *)anchor->anchor.next, (
void *)anchor->last);
1356 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1357 (
void *)list->prev, (
int)list->type);
1362 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1363 verify_list(
"debug list", anchor);
1366#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1369#define debug_list(anc, cur) ((void)0)
1375 TRACE *trace = compile_data_alloc_trace(iseq);
1377 trace->link.type = ISEQ_ELEMENT_TRACE;
1378 trace->link.next = NULL;
1379 trace->event = event;
1386new_label_body(
rb_iseq_t *iseq,
long line)
1388 LABEL *labelobj = compile_data_alloc_label(iseq);
1390 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1391 labelobj->link.next = 0;
1393 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1394 labelobj->sc_state = 0;
1396 labelobj->refcnt = 0;
1398 labelobj->rescued = LABEL_RESCUE_NONE;
1399 labelobj->unremovable = 0;
1400 labelobj->position = -1;
1407 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1408 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1409 adjust->link.next = 0;
1410 adjust->label = label;
1411 adjust->line_no = line;
1412 LABEL_UNREMOVABLE(label);
1419 const char *types = insn_op_types(insn->insn_id);
1420 for (
int j = 0; types[j]; j++) {
1421 char type = types[j];
1428 func(&OPERAND_AT(insn, j), data);
1437iseq_insn_each_object_write_barrier(
VALUE * obj,
VALUE iseq)
1446new_insn_core(
rb_iseq_t *iseq,
int line_no,
int node_id,
int insn_id,
int argc,
VALUE *argv)
1448 INSN *iobj = compile_data_alloc_insn(iseq);
1452 iobj->link.type = ISEQ_ELEMENT_INSN;
1453 iobj->link.next = 0;
1454 iobj->insn_id = insn_id;
1455 iobj->insn_info.line_no = line_no;
1456 iobj->insn_info.node_id = node_id;
1457 iobj->insn_info.events = 0;
1458 iobj->operands = argv;
1459 iobj->operand_size = argc;
1462 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1468new_insn_body(
rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...)
1470 VALUE *operands = 0;
1474 va_start(argv, argc);
1475 operands = compile_data_alloc2_type(iseq,
VALUE, argc);
1476 for (i = 0; i < argc; i++) {
1482 return new_insn_core(iseq, line_no, node_id, insn_id, argc, operands);
1486insn_replace_with_operands(
rb_iseq_t *iseq,
INSN *iobj,
enum ruby_vminsn_type insn_id,
int argc, ...)
1488 VALUE *operands = 0;
1492 va_start(argv, argc);
1493 operands = compile_data_alloc2_type(iseq,
VALUE, argc);
1494 for (i = 0; i < argc; i++) {
1501 iobj->insn_id = insn_id;
1502 iobj->operand_size = argc;
1503 iobj->operands = operands;
1504 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1512 VM_ASSERT(argc >= 0);
1515 flag |= VM_CALL_KWARG;
1516 argc += kw_arg->keyword_len;
1519 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING))
1520 && !has_blockiseq) {
1521 flag |= VM_CALL_ARGS_SIMPLE;
1524 ISEQ_BODY(iseq)->ci_size++;
1525 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1533 VALUE *operands = compile_data_calloc2_type(iseq,
VALUE, 2);
1536 operands[1] = (
VALUE)blockiseq;
1543 if (vm_ci_flag((
struct rb_callinfo *)ci) & VM_CALL_FORWARDING) {
1544 insn = new_insn_core(iseq, line_no, node_id, BIN(sendforward), 2, operands);
1547 insn = new_insn_core(iseq, line_no, node_id, BIN(send), 2, operands);
1560 VALUE ast_value = rb_ruby_ast_new(node);
1564 if (ISEQ_BODY(iseq)->source_hash) {
1565 rb_ast_t *child_ast = rb_ruby_ast_data_get(ast_value);
1566 child_ast->body.source_hash = ISEQ_BODY(iseq)->source_hash;
1567 child_ast->body.has_source_hash = 1;
1570 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1571 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1572 ret_iseq = rb_iseq_new_with_opt(ast_value, name,
1573 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1575 isolated_depth ? isolated_depth + 1 : 0,
1576 type, ISEQ_COMPILE_DATA(iseq)->option,
1577 ISEQ_SCRIPT_LINES(iseq));
1578 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1588 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1589 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1590 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1591 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1592 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1600 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1601 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1602 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1603 set_catch_except_p((
rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1626 while (pos < body->iseq_size) {
1627 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1628 if (insn == BIN(
throw)) {
1629 set_catch_except_p(iseq);
1632 pos += insn_len(insn);
1638 for (i = 0; i < ct->size; i++) {
1640 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1641 if (entry->type != CATCH_TYPE_BREAK
1642 && entry->type != CATCH_TYPE_NEXT
1643 && entry->type != CATCH_TYPE_REDO) {
1645 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1652iseq_insert_nop_between_end_and_cont(
rb_iseq_t *iseq)
1654 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1655 if (
NIL_P(catch_table_ary))
return;
1656 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1658 for (i = 0; i < tlen; i++) {
1664 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1666 if (ct != CATCH_TYPE_BREAK
1667 && ct != CATCH_TYPE_NEXT
1668 && ct != CATCH_TYPE_REDO) {
1670 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1672 INSN *nop = new_insn_core(iseq, 0, -1, BIN(nop), 0, 0);
1673 ELEM_INSERT_NEXT(end, &nop->link);
1686 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1691 if (compile_debug > 5)
1692 dump_disasm_list(FIRST_ELEMENT(anchor));
1694 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1695 iseq_optimize(iseq, anchor);
1697 if (compile_debug > 5)
1698 dump_disasm_list(FIRST_ELEMENT(anchor));
1700 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1701 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1702 iseq_insns_unification(iseq, anchor);
1703 if (compile_debug > 5)
1704 dump_disasm_list(FIRST_ELEMENT(anchor));
1707 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1708 iseq_insert_nop_between_end_and_cont(iseq);
1709 if (compile_debug > 5)
1710 dump_disasm_list(FIRST_ELEMENT(anchor));
1718 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1721 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1722 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1723 if (compile_debug > 5)
1724 dump_disasm_list(FIRST_ELEMENT(anchor));
1726 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1727 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1729 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1730 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1732 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1733 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1735 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1737 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1739 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1741 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1742 ruby_xfree_sized(ISEQ_BODY(iseq)->catch_table, iseq_catch_table_bytes(ISEQ_BODY(iseq)->catch_table->size));
1743 ISEQ_BODY(iseq)->catch_table = NULL;
1746#if VM_INSN_INFO_TABLE_IMPL == 2
1747 debugs(
"[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
1748 rb_iseq_insns_info_encode_positions(iseq);
1751 if (compile_debug > 1) {
1752 VALUE str = rb_iseq_disasm(iseq);
1755 verify_call_cache(iseq);
1756 debugs(
"[compile step: finish]\n");
1762iseq_set_exception_local_table(
rb_iseq_t *iseq)
1764 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1765 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1766 ISEQ_BODY(iseq)->lvar_states.list = NULL;
1774 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1776 iseq = ISEQ_BODY(iseq)->parent_iseq;
1782get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1786 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1787 if (ISEQ_BODY(iseq)->local_table[i] == id) {
1797 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1800 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1801 "get_local_var_idx: %d", idx);
1808get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1810 int lv = 0, idx = -1;
1811 const rb_iseq_t *
const topmost_iseq = iseq;
1814 idx = get_dyna_var_idx_at_raw(iseq,
id);
1818 iseq = ISEQ_BODY(iseq)->parent_iseq;
1823 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1824 "get_dyna_var_idx: -1");
1828 *ls = ISEQ_BODY(iseq)->local_table_size;
1833iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1837 iseq = ISEQ_BODY(iseq)->parent_iseq;
1840 body = ISEQ_BODY(iseq);
1841 if (body->local_iseq == iseq &&
1842 body->param.flags.has_block &&
1843 body->local_table_size - body->param.block_start == idx) {
1852iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1855 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1856 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1867access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1869 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1871 if (isolated_depth && level >= isolated_depth) {
1872 if (
id == rb_intern(
"yield")) {
1873 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1876 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable '%s' from isolated Proc", rb_id2name(
id));
1880 for (
int i=0; i<level; i++) {
1882 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1885 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1888 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1889 if (write && !val) {
1890 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1894 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1897 iseq = ISEQ_BODY(iseq)->parent_iseq;
1902iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1904 for (
int i=0; i<level; i++) {
1905 iseq = ISEQ_BODY(iseq)->parent_iseq;
1908 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1914update_lvar_state(
const rb_iseq_t *iseq,
int level,
int idx)
1916 for (
int i=0; i<level; i++) {
1917 iseq = ISEQ_BODY(iseq)->parent_iseq;
1920 uint8_t *states = iseq_lvar_states(ISEQ_BODY(iseq));
1921 int table_idx = ISEQ_BODY(iseq)->local_table_size - idx;
1922 switch (iseq_lvar_state_get(states, table_idx)) {
1923 case lvar_uninitialized:
1924 iseq_lvar_state_set(states, table_idx, lvar_initialized);
1926 case lvar_initialized:
1927 iseq_lvar_state_set(states, table_idx, lvar_reassigned);
1929 case lvar_reassigned:
1933 rb_bug(
"unreachable");
1938iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq)
1940 uint8_t *states = iseq_lvar_states(ISEQ_BODY(iseq));
1942 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
1943 iseq_lvar_state_set(states, i, lvar_initialized);
1946 int lead_num = ISEQ_BODY(iseq)->param.lead_num;
1947 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
1948 for (
int i=0; i<opt_num; i++) {
1949 iseq_lvar_state_set(states, lead_num + i, lvar_uninitialized);
1958 if (iseq_local_block_param_p(iseq, idx, level)) {
1959 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1962 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1964 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1970 if (iseq_local_block_param_p(iseq, idx, level)) {
1971 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1974 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1976 update_lvar_state(iseq, level, idx);
1977 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
1986 if (body->param.flags.has_opt ||
1987 body->param.flags.has_post ||
1988 body->param.flags.has_rest ||
1989 body->param.flags.has_block ||
1990 body->param.flags.has_kw ||
1991 body->param.flags.has_kwrest) {
1993 if (body->param.flags.has_block) {
1994 body->param.size = body->param.block_start + 1;
1996 else if (body->param.flags.has_kwrest) {
1997 body->param.size = body->param.keyword->rest_start + 1;
1999 else if (body->param.flags.has_kw) {
2000 body->param.size = body->param.keyword->bits_start + 1;
2002 else if (body->param.flags.has_post) {
2003 body->param.size = body->param.post_start + body->param.post_num;
2005 else if (body->param.flags.has_rest) {
2006 body->param.size = body->param.rest_start + 1;
2008 else if (body->param.flags.has_opt) {
2009 body->param.size = body->param.lead_num + body->param.opt_num;
2016 body->param.size = body->param.lead_num;
2026 struct rb_iseq_param_keyword *keyword;
2029 int kw = 0, rkw = 0, di = 0, i;
2031 body->param.flags.has_kw = TRUE;
2032 body->param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2036 node = node->nd_next;
2038 if (kw > VM_CALL_KW_LEN_MAX) {
2039 COMPILE_ERROR(ERROR_ARGS_AT(RNODE(args->kw_args))
"too many keyword parameters (%d, maximum is %d)",
2040 kw, (
int)VM_CALL_KW_LEN_MAX);
2043 keyword->bits_start = arg_size++;
2045 node = args->kw_args;
2047 const NODE *val_node = get_nd_value(node->nd_body);
2050 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
2054 switch (nd_type(val_node)) {
2056 dv = rb_node_sym_string_val(val_node);
2059 dv = rb_node_regx_string_val(val_node);
2062 dv = rb_node_line_lineno_val(val_node);
2065 dv = rb_node_integer_literal_val(val_node);
2068 dv = rb_node_float_literal_val(val_node);
2071 dv = rb_node_rational_literal_val(val_node);
2073 case NODE_IMAGINARY:
2074 dv = rb_node_imaginary_literal_val(val_node);
2077 dv = rb_node_encoding_val(val_node);
2089 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
2093 keyword->num = ++di;
2097 node = node->nd_next;
2102 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
2103 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2104 keyword->rest_start = arg_size++;
2105 body->param.flags.has_kwrest = TRUE;
2107 if (kw_id == idPow) body->param.flags.anon_kwrest = TRUE;
2109 keyword->required_num = rkw;
2110 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
2115 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
2117 if (dv == complex_mark) dv =
Qundef;
2122 keyword->default_values = dvs;
2131 if (!body->param.flags.use_block) {
2132 body->param.flags.use_block = 1;
2137 st_data_t key = (st_data_t)rb_intern_str(body->location.label);
2138 set_insert(&vm->unused_block_warning_table, key);
2146 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2150 const struct rb_args_info *
const args = &RNODE_ARGS(node_args)->nd_ainfo;
2156 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2158 body->param.lead_num = arg_size = (int)args->pre_args_num;
2159 if (body->param.lead_num > 0) body->param.flags.has_lead = TRUE;
2160 debugs(
" - argc: %d\n", body->param.lead_num);
2162 rest_id = args->rest_arg;
2163 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2167 block_id = args->block_arg;
2169 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2171 if (optimized_forward) {
2176 if (args->opt_args) {
2184 label = NEW_LABEL(nd_line(RNODE(node)));
2186 ADD_LABEL(optargs, label);
2187 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2188 node = node->nd_next;
2193 label = NEW_LABEL(nd_line(node_args));
2195 ADD_LABEL(optargs, label);
2200 for (j = 0; j < i+1; j++) {
2205 body->param.flags.has_opt = TRUE;
2206 body->param.opt_num = i;
2207 body->param.opt_table = opt_table;
2212 body->param.rest_start = arg_size++;
2213 body->param.flags.has_rest = TRUE;
2214 if (rest_id ==
'*') body->param.flags.anon_rest = TRUE;
2218 if (args->first_post_arg) {
2219 body->param.post_start = arg_size;
2220 body->param.post_num = args->post_args_num;
2221 body->param.flags.has_post = TRUE;
2222 arg_size += args->post_args_num;
2224 if (body->param.flags.has_rest) {
2225 body->param.post_start = body->param.rest_start + 1;
2229 if (args->kw_args) {
2230 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2232 else if (args->kw_rest_arg && !optimized_forward) {
2233 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2234 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2235 keyword->rest_start = arg_size++;
2236 body->param.keyword = keyword;
2237 body->param.flags.has_kwrest = TRUE;
2239 static ID anon_kwrest = 0;
2240 if (!anon_kwrest) anon_kwrest = rb_intern(
"**");
2241 if (kw_id == anon_kwrest) body->param.flags.anon_kwrest = TRUE;
2243 else if (args->no_kwarg) {
2244 body->param.flags.accepts_no_kwarg = TRUE;
2247 if (args->no_blockarg) {
2248 body->param.flags.accepts_no_block = TRUE;
2250 else if (block_id) {
2251 body->param.block_start = arg_size++;
2252 body->param.flags.has_block = TRUE;
2253 iseq_set_use_block(iseq);
2257 if (optimized_forward) {
2258 body->param.flags.use_block = 1;
2259 body->param.flags.forwardable = TRUE;
2263 iseq_calc_param_size(iseq);
2264 body->param.size = arg_size;
2266 if (args->pre_init) {
2267 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2269 if (args->post_init) {
2270 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2273 if (body->type == ISEQ_TYPE_BLOCK) {
2274 if (body->param.flags.has_opt == FALSE &&
2275 body->param.flags.has_post == FALSE &&
2276 body->param.flags.has_rest == FALSE &&
2277 body->param.flags.has_kw == FALSE &&
2278 body->param.flags.has_kwrest == FALSE) {
2280 if (body->param.lead_num == 1 && last_comma == 0) {
2282 body->param.flags.ambiguous_param0 = TRUE;
2294 unsigned int size = tbl ? tbl->size : 0;
2295 unsigned int offset = 0;
2298 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2303 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2312 MEMCPY(ids, tbl->ids + offset,
ID, size);
2313 ISEQ_BODY(iseq)->local_table = ids;
2315 if (ISEQ_LVAR_STATES_EMBED_P(size)) {
2317 memset(ISEQ_BODY(iseq)->lvar_states.single, 0,
sizeof(ISEQ_BODY(iseq)->lvar_states.single));
2320 ISEQ_BODY(iseq)->lvar_states.list =
ZALLOC_N(uint8_t, ISEQ_LVAR_STATES_BUFLEN(size));
2323 ISEQ_BODY(iseq)->local_table_size = size;
2325 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2337 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2340 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2343 else if (tlit != tval) {
2353 long x =
FIX2LONG(rb_big_cmp(lit, val));
2361 return rb_float_cmp(lit, val);
2364 const struct RRational *rat1 = RRATIONAL(val);
2365 const struct RRational *rat2 = RRATIONAL(lit);
2366 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2369 const struct RComplex *comp1 = RCOMPLEX(val);
2370 const struct RComplex *comp2 = RCOMPLEX(lit);
2371 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2374 return rb_reg_equal(val, lit) ? 0 : -1;
2382rb_iseq_cdhash_hash(
VALUE a)
2384 switch (OBJ_BUILTIN_TYPE(a)) {
2387 return (st_index_t)a;
2395 return rb_rational_hash(a);
2397 return rb_complex_hash(a);
2407 rb_iseq_cdhash_hash,
2411cdhash_new(
size_t size)
2413 VALUE cdhash = rb_imemo_cdhash_new(size, &cdhash_type);
2414 RB_OBJ_SET_SHAREABLE(cdhash);
2421 st_table *tbl = rb_imemo_cdhash_tbl(cdhash);
2422 st_insert(tbl, key, val);
2429 st_table *tbl = rb_imemo_cdhash_tbl(cdhash);
2431 if (!st_lookup(tbl, key, &dontcare)) {
2432 st_insert(tbl, key, val);
2444cdhash_set_label_check_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
2450cdhash_set_label_replace_i(st_data_t *key, st_data_t *value, st_data_t ptr,
int existing)
2454 *value = lobj->position - (data->pos+data->len);
2461 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2468 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2470 if (rb_id_table_lookup(tbl,
id,&val)) {
2475 tbl = rb_id_table_create(1);
2476 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2478 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2479 rb_id_table_insert(tbl,
id,val);
2483#define BADINSN_DUMP(anchor, list, dest) \
2484 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2486#define BADINSN_ERROR \
2487 (SIZED_FREE_N(generated_iseq, generated_iseq_size), \
2488 SIZED_FREE_N(insns_info, insns_info_size), \
2489 BADINSN_DUMP(anchor, list, NULL), \
2495 int stack_max = 0, sp = 0, line = 0;
2498 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2499 if (IS_LABEL(list)) {
2505 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2506 switch (list->type) {
2507 case ISEQ_ELEMENT_INSN:
2515 sp = calc_sp_depth(sp, iobj);
2517 BADINSN_DUMP(anchor, list, NULL);
2518 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2519 "argument stack underflow (%d)", sp);
2522 if (sp > stack_max) {
2526 line = iobj->insn_info.line_no;
2528 operands = iobj->operands;
2529 insn = iobj->insn_id;
2530 types = insn_op_types(insn);
2531 len = insn_len(insn);
2534 if (iobj->operand_size !=
len - 1) {
2536 BADINSN_DUMP(anchor, list, NULL);
2537 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2538 "operand size miss! (%d for %d)",
2539 iobj->operand_size,
len - 1);
2543 for (j = 0; types[j]; j++) {
2544 if (types[j] == TS_OFFSET) {
2548 BADINSN_DUMP(anchor, list, NULL);
2549 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2550 "unknown label: "LABEL_FORMAT, lobj->label_no);
2553 if (lobj->sp == -1) {
2556 else if (lobj->sp != sp) {
2557 VALUE path = rb_iseq_path(iseq);
2558 debugs(
"%.*s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2560 lobj->label_no, lobj->sp, sp);
2566 case ISEQ_ELEMENT_LABEL:
2569 if (lobj->sp == -1) {
2573 if (lobj->sp != sp) {
2574 VALUE path = rb_iseq_path(iseq);
2575 debugs(
"%.*s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2577 lobj->label_no, lobj->sp, sp);
2583 case ISEQ_ELEMENT_TRACE:
2588 case ISEQ_ELEMENT_ADJUST:
2593 sp = adjust->label ? adjust->label->sp : 0;
2594 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2595 BADINSN_DUMP(anchor, list, NULL);
2596 COMPILE_ERROR(iseq, adjust->line_no,
2597 "iseq_set_sequence: adjust bug %d < %d",
2604 BADINSN_DUMP(anchor, list, NULL);
2605 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2614 int insns_info_index,
int code_index,
const INSN *iobj)
2616 if (insns_info_index == 0 ||
2617 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2618#ifdef USE_ISEQ_NODE_ID
2619 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2621 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2622 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2623#ifdef USE_ISEQ_NODE_ID
2624 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2626 insns_info[insns_info_index].events = iobj->insn_info.events;
2627 positions[insns_info_index] = code_index;
2635 int insns_info_index,
int code_index,
const ADJUST *adjust)
2637 insns_info[insns_info_index].line_no = adjust->line_no;
2638 insns_info[insns_info_index].node_id = -1;
2639 insns_info[insns_info_index].events = 0;
2640 positions[insns_info_index] = code_index;
2645array_to_idlist(
VALUE arr)
2650 for (
long i = 0; i < size; i++) {
2659idlist_to_array(
const ID *ids)
2676 unsigned int *positions;
2678 VALUE *generated_iseq;
2682 int insn_num, code_index, insns_info_index, sp = 0;
2683 int stack_max = fix_sp_depth(iseq, anchor);
2685 if (stack_max < 0)
return COMPILE_NG;
2688 insn_num = code_index = 0;
2689 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2690 switch (list->type) {
2691 case ISEQ_ELEMENT_INSN:
2695 sp = calc_sp_depth(sp, iobj);
2697 events = iobj->insn_info.events |= events;
2698 if (ISEQ_COVERAGE(iseq)) {
2699 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2700 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2701 int line = iobj->insn_info.line_no - 1;
2702 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2706 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2707 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2713 code_index += insn_data_length(iobj);
2718 case ISEQ_ELEMENT_LABEL:
2721 lobj->position = code_index;
2722 if (lobj->sp != sp) {
2723 VALUE path = rb_iseq_path(iseq);
2724 debugs(
"%.*s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2726 lobj->label_no, lobj->sp, sp);
2731 case ISEQ_ELEMENT_TRACE:
2734 events |= trace->event;
2735 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2738 case ISEQ_ELEMENT_ADJUST:
2741 if (adjust->line_no != -1) {
2743 sp = adjust->label ? adjust->label->sp : 0;
2744 if (orig_sp - sp > 0) {
2745 if (orig_sp - sp > 1) code_index++;
2757 const int generated_iseq_size = code_index;
2760 const int insns_info_size = insn_num;
2763 const int positions_size = insn_num;
2764 positions =
ALLOC_N(
unsigned int, insn_num);
2765 if (ISEQ_IS_SIZE(body)) {
2769 body->is_entries = NULL;
2772 if (body->ci_size) {
2776 body->call_data = NULL;
2778 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2785 iseq_bits_t * mark_offset_bits;
2786 int code_size = code_index;
2788 bool needs_bitmap =
false;
2790 const size_t mark_offset_bits_size = ISEQ_MBITS_BUFLEN(code_index);
2791 if (mark_offset_bits_size == 1) {
2792 mark_offset_bits = &ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2793 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
true;
2796 mark_offset_bits =
ZALLOC_N(iseq_bits_t, mark_offset_bits_size);
2797 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = mark_offset_bits;
2798 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
false;
2801 ISEQ_COMPILE_DATA(iseq)->iseq_encoded = (
void *)generated_iseq;
2802 ISEQ_COMPILE_DATA(iseq)->iseq_size = code_index;
2804 list = FIRST_ELEMENT(anchor);
2805 insns_info_index = code_index = sp = 0;
2808 switch (list->type) {
2809 case ISEQ_ELEMENT_INSN:
2817 sp = calc_sp_depth(sp, iobj);
2819 operands = iobj->operands;
2820 insn = iobj->insn_id;
2821 generated_iseq[code_index] = insn;
2822 types = insn_op_types(insn);
2823 len = insn_len(insn);
2825 for (j = 0; types[j]; j++) {
2826 char type = types[j];
2834 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2839 VALUE map = operands[j];
2842 data.pos = code_index;
2844 st_foreach_with_replace(rb_imemo_cdhash_tbl(map), cdhash_set_label_check_i, cdhash_set_label_replace_i, (
VALUE)&data);
2846 generated_iseq[code_index + 1 + j] = map;
2847 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2849 needs_bitmap =
true;
2854 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2859 VALUE v = operands[j];
2860 generated_iseq[code_index + 1 + j] = v;
2864 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2865 needs_bitmap =
true;
2872 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2873 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2874 if (UNLIKELY(ic_index >= body->ic_size)) {
2875 BADINSN_DUMP(anchor, &iobj->link, 0);
2876 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2877 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2878 ic_index, ISEQ_IS_SIZE(body));
2881 ic->
segments = array_to_idlist(operands[j]);
2883 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2888 unsigned int ic_index =
FIX2UINT(operands[j]);
2890 IVC cache = ((
IVC)&body->is_entries[ic_index]);
2892 if (insn == BIN(setinstancevariable)) {
2893 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2894 cache->value = IVAR_CACHE_INIT;
2897 cache->iv_set_name = 0;
2898 cache->value = rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET);
2904 unsigned int ic_index =
FIX2UINT(operands[j]);
2905 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2906 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2907 BADINSN_DUMP(anchor, &iobj->link, 0);
2908 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2909 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2910 ic_index, ISEQ_IS_SIZE(body));
2912 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2919 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2920 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2922 cd->cc = vm_cc_empty();
2923 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2927 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2930 generated_iseq[code_index + 1 + j] = operands[j];
2933 generated_iseq[code_index + 1 + j] = operands[j];
2936 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2937 "unknown operand type: %c",
type);
2941 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2945 case ISEQ_ELEMENT_LABEL:
2948 if (lobj->sp != sp) {
2949 VALUE path = rb_iseq_path(iseq);
2950 debugs(
"%.*s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2952 lobj->label_no, lobj->sp, sp);
2957 case ISEQ_ELEMENT_ADJUST:
2962 if (adjust->label) {
2963 sp = adjust->label->sp;
2969 if (adjust->line_no != -1) {
2970 const int diff = orig_sp - sp;
2972 if (insns_info_index == 0) {
2973 COMPILE_ERROR(iseq, adjust->line_no,
2974 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2976 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2979 generated_iseq[code_index++] = BIN(adjuststack);
2980 generated_iseq[code_index++] = orig_sp - sp;
2982 else if (diff == 1) {
2983 generated_iseq[code_index++] = BIN(pop);
2985 else if (diff < 0) {
2986 int label_no = adjust->label ? adjust->label->label_no : -1;
2987 SIZED_FREE_N(generated_iseq, generated_iseq_size);
2988 SIZED_FREE_N(insns_info, insns_info_size);
2989 SIZED_FREE_N(positions, positions_size);
2990 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
2991 SIZED_FREE_N(mark_offset_bits, ISEQ_MBITS_BUFLEN(code_index));
2993 debug_list(anchor, list);
2994 COMPILE_ERROR(iseq, adjust->line_no,
2995 "iseq_set_sequence: adjust bug to %d %d < %d",
2996 label_no, orig_sp, sp);
3009 body->iseq_encoded = (
void *)generated_iseq;
3010 body->iseq_size = code_index;
3011 body->stack_max = stack_max;
3013 if (ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit) {
3014 body->mark_bits.single = ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
3018 body->mark_bits.list = mark_offset_bits;
3021 body->mark_bits.list = NULL;
3022 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = NULL;
3023 SIZED_FREE_N(mark_offset_bits, mark_offset_bits_size);
3028 body->insns_info.body = insns_info;
3029 body->insns_info.positions_or_succ_index_table.positions = positions;
3032 body->insns_info.body = insns_info;
3033 SIZED_REALLOC_N(positions,
unsigned int, insns_info_index, positions_size);
3034 body->insns_info.positions_or_succ_index_table.positions = positions;
3035 body->insns_info.size = insns_info_index;
3041label_get_position(
LABEL *lobj)
3043 return lobj->position;
3047label_get_sp(
LABEL *lobj)
3053iseq_set_exception_table(
rb_iseq_t *iseq)
3055 const VALUE *tptr, *ptr;
3056 unsigned int tlen, i;
3059 ISEQ_BODY(iseq)->catch_table = NULL;
3061 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
3062 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
3070 for (i = 0; i < table->size; i++) {
3073 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
3074 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
3075 pos = label_get_position((
LABEL *)(ptr[1] & ~1));
3077 entry->start = (
unsigned int)pos;
3078 pos = label_get_position((
LABEL *)(ptr[2] & ~1));
3080 entry->end = (
unsigned int)pos;
3087 entry->cont = label_get_position(lobj);
3088 entry->sp = label_get_sp(lobj);
3091 if (entry->type == CATCH_TYPE_RESCUE ||
3092 entry->type == CATCH_TYPE_BREAK ||
3093 entry->type == CATCH_TYPE_NEXT) {
3102 ISEQ_BODY(iseq)->catch_table = table;
3103 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
3124 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
3126 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
3127 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
3128 opt_table[i] = label_get_position((
LABEL *)opt_table[i]);
3135get_destination_insn(
INSN *iobj)
3141 list = lobj->link.next;
3143 switch (list->type) {
3144 case ISEQ_ELEMENT_INSN:
3145 case ISEQ_ELEMENT_ADJUST:
3147 case ISEQ_ELEMENT_LABEL:
3150 case ISEQ_ELEMENT_TRACE:
3153 events |= trace->event;
3161 if (list && IS_INSN(list)) {
3163 iobj->insn_info.events |= events;
3169get_next_insn(
INSN *iobj)
3174 if (IS_INSN(list) || IS_ADJUST(list)) {
3183get_prev_insn(
INSN *iobj)
3188 if (IS_INSN(list) || IS_ADJUST(list)) {
3197unref_destination(
INSN *iobj,
int pos)
3199 LABEL *lobj = (
LABEL *)OPERAND_AT(iobj, pos);
3201 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3205replace_destination(
INSN *dobj,
INSN *nobj)
3207 VALUE n = OPERAND_AT(nobj, 0);
3210 if (dl == nl)
return false;
3213 OPERAND_AT(dobj, 0) = n;
3214 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3219find_destination(
INSN *i)
3221 int pos,
len = insn_len(i->insn_id);
3222 for (pos = 0; pos <
len; ++pos) {
3223 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3224 return (
LABEL *)OPERAND_AT(i, pos);
3233 LINK_ELEMENT *first = i, *end, *scan, *pending_end = 0;
3235 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
3238 unref_counts =
ALLOCA_N(
int, nlabels);
3239 MEMZERO(unref_counts,
int, nlabels);
3245 if (IS_INSN(scan)) {
3246 if (IS_INSN_ID(scan, leave)) {
3251 else if ((lab = find_destination((
INSN *)scan)) != 0) {
3252 unref_counts[lab->label_no]++;
3253 if (lab == pending && lab->refcnt <= unref_counts[lab->label_no]) {
3258 else if (IS_LABEL(scan)) {
3259 lab = (
LABEL *)scan;
3260 if (lab->unremovable) {
3264 if (lab->refcnt > unref_counts[lab->label_no]) {
3267 pending_end = (scan == first) ? 0 : end;
3271 else if (IS_ADJUST(scan)) {
3275 if (!pending) end = scan;
3276 }
while ((scan = scan->next) != 0);
3279 if (!pending_end)
return 0;
3286 VALUE insn = INSN_OF(i);
3287 int pos,
len = insn_len(insn);
3288 for (pos = 0; pos <
len; ++pos) {
3289 switch (insn_op_types(insn)[pos]) {
3291 unref_destination((
INSN *)i, pos);
3300 }
while ((i != end) && (i = i->next) != 0);
3307 switch (OPERAND_AT(iobj, 0)) {
3309 ELEM_REMOVE(&iobj->link);
3312 ELEM_REMOVE(&iobj->link);
3315 iobj->insn_id = BIN(adjuststack);
3321is_frozen_putstring(
INSN *insn,
VALUE *op)
3323 if (IS_INSN_ID(insn, dupstring) || IS_INSN_ID(insn, dupchilledstring)) {
3324 *op = OPERAND_AT(insn, 0);
3327 else if (IS_INSN_ID(insn, putobject)) {
3328 *op = OPERAND_AT(insn, 0);
3339 if (prev->type == ISEQ_ELEMENT_LABEL) {
3341 if (label->refcnt > 0) {
3345 else if (prev->type == ISEQ_ELEMENT_INSN) {
3378 INSN *niobj, *ciobj, *dup = 0;
3382 switch (INSN_OF(iobj)) {
3383 case BIN(dupstring):
3384 case BIN(dupchilledstring):
3390 case BIN(putobject):
3393 default:
return FALSE;
3396 ciobj = (
INSN *)get_next_insn(iobj);
3397 if (IS_INSN_ID(ciobj, jump)) {
3398 ciobj = (
INSN *)get_next_insn((
INSN*)OPERAND_AT(ciobj, 0));
3400 if (IS_INSN_ID(ciobj, dup)) {
3401 ciobj = (
INSN *)get_next_insn(dup = ciobj);
3403 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3404 niobj = (
INSN *)get_next_insn(ciobj);
3409 switch (INSN_OF(niobj)) {
3411 if (OPERAND_AT(ciobj, 0) ==
type) {
3412 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3415 case BIN(branchunless):
3416 if (OPERAND_AT(ciobj, 0) !=
type) {
3417 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3423 line = ciobj->insn_info.line_no;
3424 node_id = ciobj->insn_info.node_id;
3426 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3427 dest = (
LABEL *)niobj->link.next;
3430 dest = NEW_LABEL(line);
3431 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3434 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3436 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3443 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3444 vm_ci_flag(ci) | add,
3454 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3462#define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3465iseq_reg_compile(
rb_iseq_t *iseq,
VALUE str,
int options,
const char *sourcefile,
int sourceline)
3467 VALUE errinfo = rb_errinfo();
3468 VALUE re = rb_reg_compile(str, options, sourcefile, sourceline);
3470 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
3471 rb_set_errinfo(errinfo);
3472 COMPILE_ERROR(iseq, sourceline,
"%" PRIsVALUE, message);
3475 RB_OBJ_SET_SHAREABLE(re);
3486 optimize_checktype(iseq, iobj);
3488 if (IS_INSN_ID(iobj, jump)) {
3489 INSN *niobj, *diobj, *piobj;
3490 diobj = (
INSN *)get_destination_insn(iobj);
3491 niobj = (
INSN *)get_next_insn(iobj);
3493 if (diobj == niobj) {
3500 unref_destination(iobj, 0);
3501 ELEM_REMOVE(&iobj->link);
3504 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3505 IS_INSN_ID(diobj, jump) &&
3506 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3507 diobj->insn_info.events == 0) {
3518 if (replace_destination(iobj, diobj)) {
3519 remove_unreachable_chunk(iseq, iobj->link.next);
3523 else if (IS_INSN_ID(diobj, leave)) {
3536 unref_destination(iobj, 0);
3537 iobj->insn_id = BIN(leave);
3538 iobj->operand_size = 0;
3539 iobj->insn_info = diobj->insn_info;
3542 else if (IS_INSN(iobj->link.prev) &&
3543 (piobj = (
INSN *)iobj->link.prev) &&
3544 (IS_INSN_ID(piobj, branchif) ||
3545 IS_INSN_ID(piobj, branchunless))) {
3546 INSN *pdiobj = (
INSN *)get_destination_insn(piobj);
3547 if (niobj == pdiobj) {
3548 int refcnt = IS_LABEL(piobj->link.next) ?
3549 ((
LABEL *)piobj->link.next)->refcnt : 0;
3564 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3565 ? BIN(branchunless) : BIN(branchif);
3566 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3567 ELEM_REMOVE(&iobj->link);
3574 else if (diobj == pdiobj) {
3588 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3589 ELEM_REPLACE(&piobj->link, &popiobj->link);
3592 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3606 if (IS_INSN_ID(iobj, newrange)) {
3607 INSN *
const range = iobj;
3609 VALUE str_beg, str_end;
3611 if ((end = (
INSN *)get_prev_insn(range)) != 0 &&
3612 is_frozen_putstring(end, &str_end) &&
3613 (beg = (
INSN *)get_prev_insn(end)) != 0 &&
3614 is_frozen_putstring(beg, &str_beg) &&
3615 !(insn_has_label_before(&beg->link) || insn_has_label_before(&end->link))) {
3616 int excl =
FIX2INT(OPERAND_AT(range, 0));
3619 ELEM_REMOVE(&beg->link);
3620 ELEM_REMOVE(&end->link);
3621 range->insn_id = BIN(putobject);
3622 OPERAND_AT(range, 0) = lit_range;
3627 if (IS_INSN_ID(iobj, leave)) {
3628 remove_unreachable_chunk(iseq, iobj->link.next);
3640 if (IS_INSN_ID(iobj, duparray)) {
3642 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3643 iobj->insn_id = BIN(putobject);
3653 if (IS_INSN_ID(iobj, duparray)) {
3655 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3659 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3660 VALUE ary = iobj->operands[0];
3663 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, ary, (
VALUE)ci);
3675 if (IS_INSN_ID(iobj, duphash)) {
3677 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3681 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3682 VALUE hash = iobj->operands[0];
3684 RB_OBJ_SET_SHAREABLE(hash);
3686 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, hash, (
VALUE)ci);
3698 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3700 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3704 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3705 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, rb_cArray_empty_frozen, (
VALUE)ci);
3717 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3719 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3723 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3724 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, rb_cHash_empty_frozen, (
VALUE)ci);
3730 if (IS_INSN_ID(iobj, branchif) ||
3731 IS_INSN_ID(iobj, branchnil) ||
3732 IS_INSN_ID(iobj, branchunless)) {
3741 INSN *nobj = (
INSN *)get_destination_insn(iobj);
3756 int stop_optimization =
3757 nobj->link.type == ISEQ_ELEMENT_INSN &&
3758 (nobj->insn_info.events & (
RUBY_EVENT_LINE | RUBY_EVENT_COVERAGE_LINE));
3759 if (!stop_optimization) {
3760 INSN *pobj = (
INSN *)iobj->link.prev;
3763 if (!IS_INSN(&pobj->link))
3765 else if (IS_INSN_ID(pobj, dup))
3770 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3771 if (!replace_destination(iobj, nobj))
break;
3773 else if (prev_dup && IS_INSN(&nobj->link) && IS_INSN_ID(nobj, dup) &&
3774 !!(nobj = (
INSN *)nobj->link.next) &&
3775 IS_INSN(&nobj->link) &&
3777 nobj->insn_id == iobj->insn_id) {
3793 if (!replace_destination(iobj, nobj))
break;
3821 if (prev_dup && IS_INSN(pobj->link.prev)) {
3822 pobj = (
INSN *)pobj->link.prev;
3824 if (IS_INSN_ID(pobj, putobject)) {
3825 cond = (IS_INSN_ID(iobj, branchif) ?
3826 OPERAND_AT(pobj, 0) !=
Qfalse :
3827 IS_INSN_ID(iobj, branchunless) ?
3828 OPERAND_AT(pobj, 0) ==
Qfalse :
3831 else if (IS_INSN_ID(pobj, dupstring) ||
3832 IS_INSN_ID(pobj, duparray) ||
3833 IS_INSN_ID(pobj, newarray)) {
3834 cond = IS_INSN_ID(iobj, branchif);
3836 else if (IS_INSN_ID(pobj, putnil)) {
3837 cond = !IS_INSN_ID(iobj, branchif);
3840 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3841 ELEM_REMOVE(iobj->link.prev);
3843 else if (!iseq_pop_newarray(iseq, pobj)) {
3844 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3845 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3849 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3850 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3852 iobj->insn_id = BIN(jump);
3856 unref_destination(iobj, 0);
3857 ELEM_REMOVE(&iobj->link);
3864 if (!dest || !IS_INSN(dest))
break;
3865 nobj = (
INSN *)dest;
3871 if (IS_INSN_ID(iobj, pop)) {
3879 if (IS_INSN(prev)) {
3880 enum ruby_vminsn_type previ = ((
INSN *)prev)->insn_id;
3881 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3882 previ == BIN(putself) || previ == BIN(dupstring) ||
3883 previ == BIN(dupchilledstring) ||
3884 previ == BIN(dup) ||
3885 previ == BIN(getlocal) ||
3886 previ == BIN(getblockparam) ||
3887 previ == BIN(getblockparamproxy) ||
3888 previ == BIN(getinstancevariable) ||
3889 previ == BIN(duparray)) {
3893 ELEM_REMOVE(&iobj->link);
3895 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (
INSN*)prev)) {
3896 ELEM_REMOVE(&iobj->link);
3898 else if (previ == BIN(concatarray)) {
3900 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3901 INSN_OF(piobj) = BIN(pop);
3903 else if (previ == BIN(concatstrings)) {
3904 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3908 ELEM_REMOVE(&iobj->link);
3909 INSN_OF(prev) = BIN(adjuststack);
3915 if (IS_INSN_ID(iobj, newarray) ||
3916 IS_INSN_ID(iobj, duparray) ||
3917 IS_INSN_ID(iobj, concatarray) ||
3918 IS_INSN_ID(iobj, splatarray) ||
3928 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3934 if (IS_INSN_ID(iobj, newarray)) {
3936 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3937 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3939 op1 = OPERAND_AT(iobj, 0);
3940 op2 = OPERAND_AT(next, 0);
3951 INSN_OF(iobj) = BIN(swap);
3952 iobj->operand_size = 0;
3961 INSN_OF(iobj) = BIN(opt_reverse);
3966 INSN_OF(iobj) = BIN(opt_reverse);
3967 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3977 for (; diff > 0; diff--) {
3978 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
3989 for (; diff < 0; diff++) {
3990 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
3997 if (IS_INSN_ID(iobj, duparray)) {
4006 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
4007 INSN_OF(iobj) = BIN(putobject);
4011 if (IS_INSN_ID(iobj, anytostring)) {
4019 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
4020 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
4025 if (IS_INSN_ID(iobj, dupstring) || IS_INSN_ID(iobj, dupchilledstring) ||
4033 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
4034 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
4035 INSN *next = (
INSN *)iobj->link.next;
4036 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
4037 ELEM_REMOVE(&next->link);
4039 ELEM_REMOVE(&iobj->link);
4041 if (IS_NEXT_INSN_ID(&iobj->link, toregexp)) {
4042 INSN *next = (
INSN *)iobj->link.next;
4043 if (OPERAND_AT(next, 1) ==
INT2FIX(1)) {
4044 VALUE src = OPERAND_AT(iobj, 0);
4045 int opt = (int)
FIX2LONG(OPERAND_AT(next, 0));
4046 VALUE path = rb_iseq_path(iseq);
4047 int line = iobj->insn_info.line_no;
4048 VALUE re = iseq_reg_compile(iseq, src, opt, RSTRING_PTR(path), line);
4053 iobj->insn_id = BIN(putobject);
4055 ELEM_REMOVE(iobj->link.next);
4060 if (IS_INSN_ID(iobj, concatstrings)) {
4069 if (IS_INSN(next) && IS_INSN_ID(next, jump))
4070 next = get_destination_insn(jump = (
INSN *)next);
4071 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
4072 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
4073 OPERAND_AT(iobj, 0) =
INT2FIX(n);
4075 LABEL *label = ((
LABEL *)OPERAND_AT(jump, 0));
4076 if (!--label->refcnt) {
4077 ELEM_REMOVE(&label->link);
4080 label = NEW_LABEL(0);
4081 OPERAND_AT(jump, 0) = (
VALUE)label;
4084 ELEM_INSERT_NEXT(next, &label->link);
4085 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
4093 if (do_tailcallopt &&
4094 (IS_INSN_ID(iobj, send) ||
4095 IS_INSN_ID(iobj, invokesuper))) {
4104 if (iobj->link.next) {
4107 if (!IS_INSN(next)) {
4111 switch (INSN_OF(next)) {
4120 next = get_destination_insn((
INSN *)next);
4134 if (IS_INSN_ID(piobj, send) ||
4135 IS_INSN_ID(piobj, invokesuper)) {
4136 if (OPERAND_AT(piobj, 1) == 0) {
4137 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4138 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4143 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4144 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4150 if (IS_INSN_ID(iobj, dup)) {
4151 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
4162 if (IS_NEXT_INSN_ID(set1, setlocal)) {
4164 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4165 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4167 ELEM_REMOVE(&iobj->link);
4180 else if (IS_NEXT_INSN_ID(set1, dup) &&
4181 IS_NEXT_INSN_ID(set1->next, setlocal)) {
4182 set2 = set1->next->next;
4183 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4184 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4185 ELEM_REMOVE(set1->next);
4199 if (IS_INSN_ID(iobj, getlocal)) {
4201 if (IS_NEXT_INSN_ID(niobj, dup)) {
4202 niobj = niobj->next;
4204 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
4206 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
4207 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
4223 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
4224 if (IS_TRACE(iobj->link.next)) {
4225 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
4226 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
4228 if (iobj == (
INSN *)list && bf->argc == 0 && (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF)) {
4229 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4242 if (IS_INSN_ID(iobj, getblockparam)) {
4243 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4244 iobj->insn_id = BIN(getblockparamproxy);
4248 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4250 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4251 niobj = niobj->next;
4253 unsigned int set_flags = 0, unset_flags = 0;
4266 if (IS_NEXT_INSN_ID(niobj, send)) {
4267 siobj = niobj->next;
4268 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4269 unset_flags = VM_CALL_ARGS_BLOCKARG;
4284 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4285 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4286 siobj = niobj->next->next;
4287 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4292 unsigned int flags = vm_ci_flag(ci);
4293 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4294 ((
INSN*)niobj)->insn_id = BIN(putobject);
4295 RB_OBJ_WRITE(iseq, &OPERAND_AT(niobj, 0), RB_OBJ_SET_SHAREABLE(rb_hash_freeze(rb_hash_resurrect(OPERAND_AT(niobj, 0)))));
4297 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4298 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4300 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4314 if (IS_NEXT_NEXT_INSN_ID(&iobj->link, swap)) {
4318 if (IS_INDEPENDENT_INSN(first) && IS_INDEPENDENT_INSN(second)) {
4320 ELEM_SWAP(first, second);
4328insn_set_specialized_instruction(
rb_iseq_t *iseq,
INSN *iobj,
int insn_id)
4330 if (insn_id == BIN(opt_neq)) {
4331 VALUE original_ci = iobj->operands[0];
4332 VALUE new_ci = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4333 insn_replace_with_operands(iseq, iobj, insn_id, 2, new_ci, original_ci);
4336 iobj->insn_id = insn_id;
4337 iobj->operand_size = insn_len(insn_id) - 1;
4347 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4348 IS_INSN(iobj->link.next)) {
4352 INSN *niobj = (
INSN *)iobj->link.next;
4353 if (IS_INSN_ID(niobj, send)) {
4355 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4357 switch (vm_ci_mid(ci)) {
4359 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4362 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4365 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4370 VALUE num = iobj->operands[0];
4371 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, num, method);
4372 ELEM_REMOVE(&niobj->link);
4377 else if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4379 IS_NEXT_INSN_ID(&niobj->link, send)) {
4381 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4382 VALUE num = iobj->operands[0];
4383 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK));
4384 ELEM_REMOVE(&iobj->link);
4385 ELEM_REMOVE(niobj->link.next);
4386 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4392 else if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4394 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4395 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4398 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4399 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] ==
rb_id2sym(idBuffer))) {
4400 VALUE num = iobj->operands[0];
4401 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 2),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER));
4403 ELEM_REMOVE((niobj->link.next)->next);
4405 ELEM_REMOVE(&iobj->link);
4407 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4415 if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4416 IS_INSN_ID(niobj, putobject) ||
4417 IS_INSN_ID(niobj, putself) ||
4418 IS_INSN_ID(niobj, getlocal) ||
4419 IS_INSN_ID(niobj, getinstancevariable)) &&
4420 IS_NEXT_INSN_ID(&niobj->link, send)) {
4427 sendobj = sendobj->next;
4428 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4429 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4432 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4433 VALUE num = iobj->operands[0];
4435 insn_replace_with_operands(iseq, sendins, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P));
4437 ELEM_REMOVE(&iobj->link);
4451 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4452 INSN *niobj = (
INSN *)iobj->link.next;
4453 if ((IS_INSN_ID(niobj, getlocal) ||
4454 IS_INSN_ID(niobj, getinstancevariable) ||
4455 IS_INSN_ID(niobj, putself) ||
4456 IS_INSN_ID(niobj, putobject) ||
4457 IS_INSN_ID(niobj, putnil)) &&
4458 IS_NEXT_INSN_ID(&niobj->link, send)) {
4465 sendobj = sendobj->next;
4466 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4467 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4469 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4471 VALUE ary = iobj->operands[0];
4475 insn_replace_with_operands(iseq, sendins, BIN(opt_duparray_send), 3, ary,
rb_id2sym(idIncludeP),
INT2FIX(1));
4478 ELEM_REMOVE(&iobj->link);
4485 if (IS_INSN_ID(iobj, send)) {
4489#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4490 if (vm_ci_simple(ci)) {
4491 switch (vm_ci_argc(ci)) {
4493 switch (vm_ci_mid(ci)) {
4494 case idLength: SP_INSN(length);
return COMPILE_OK;
4495 case idSize: SP_INSN(size);
return COMPILE_OK;
4496 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4497 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4498 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4499 case idNot: SP_INSN(not);
return COMPILE_OK;
4503 switch (vm_ci_mid(ci)) {
4504 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4505 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4506 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4507 case idDIV: SP_INSN(div);
return COMPILE_OK;
4508 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4509 case idEq: SP_INSN(eq);
return COMPILE_OK;
4510 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4511 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4512 case idLT: SP_INSN(lt);
return COMPILE_OK;
4513 case idLE: SP_INSN(le);
return COMPILE_OK;
4514 case idGT: SP_INSN(gt);
return COMPILE_OK;
4515 case idGE: SP_INSN(ge);
return COMPILE_OK;
4516 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4517 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4518 case idAnd: SP_INSN(and);
return COMPILE_OK;
4519 case idOr: SP_INSN(or);
return COMPILE_OK;
4523 switch (vm_ci_mid(ci)) {
4524 case idASET: SP_INSN(aset);
return COMPILE_OK;
4530 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4531 iobj->insn_id = BIN(opt_send_without_block);
4532 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4543 switch (ISEQ_BODY(iseq)->
type) {
4545 case ISEQ_TYPE_EVAL:
4546 case ISEQ_TYPE_MAIN:
4548 case ISEQ_TYPE_RESCUE:
4549 case ISEQ_TYPE_ENSURE:
4561 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4562 const int do_tailcallopt = tailcallable_p(iseq) &&
4563 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4564 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4565 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4566 const int do_without_ints = ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_WITHOUT_INTERRUPTS;
4567 int rescue_level = 0;
4568 int tailcallopt = do_tailcallopt;
4570 list = FIRST_ELEMENT(anchor);
4572 int do_block_optimization = 0;
4573 LABEL * block_loop_label = NULL;
4576 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
4577 do_block_optimization = 1;
4582 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop) && IS_LABEL(le->next)) {
4583 block_loop_label = (
LABEL *)le->next;
4588 if (IS_INSN(list)) {
4589 if (do_peepholeopt) {
4590 iseq_peephole_optimize(iseq, list, tailcallopt);
4593 iseq_specialized_instruction(iseq, (
INSN *)list);
4596 insn_operands_unification((
INSN *)list);
4599 if (do_without_ints) {
4601 if (IS_INSN_ID(item, jump)) {
4602 item->insn_id = BIN(jump_without_ints);
4604 else if (IS_INSN_ID(item, branchif)) {
4605 item->insn_id = BIN(branchif_without_ints);
4607 else if (IS_INSN_ID(item, branchunless)) {
4608 item->insn_id = BIN(branchunless_without_ints);
4610 else if (IS_INSN_ID(item, branchnil)) {
4611 item->insn_id = BIN(branchnil_without_ints);
4615 if (do_block_optimization) {
4618 if (IS_INSN_ID(item,
throw)) {
4619 do_block_optimization = 0;
4624 const char *types = insn_op_types(item->insn_id);
4625 for (
int j = 0; types[j]; j++) {
4626 if (types[j] == TS_OFFSET) {
4631 LABEL * target = (
LABEL *)OPERAND_AT(item, j);
4632 if (target == block_loop_label) {
4633 do_block_optimization = 0;
4640 if (IS_LABEL(list)) {
4641 switch (((
LABEL *)list)->rescued) {
4642 case LABEL_RESCUE_BEG:
4644 tailcallopt = FALSE;
4646 case LABEL_RESCUE_END:
4647 if (!--rescue_level) tailcallopt = do_tailcallopt;
4654 if (do_block_optimization) {
4656 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop)) {
4663#if OPT_INSTRUCTIONS_UNIFICATION
4671 VALUE *operands = 0, *ptr = 0;
4675 for (i = 0; i < size; i++) {
4676 iobj = (
INSN *)list;
4677 argc += iobj->operand_size;
4682 ptr = operands = compile_data_alloc2_type(iseq,
VALUE, argc);
4687 for (i = 0; i < size; i++) {
4688 iobj = (
INSN *)list;
4689 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4690 ptr += iobj->operand_size;
4694 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4706#if OPT_INSTRUCTIONS_UNIFICATION
4712 list = FIRST_ELEMENT(anchor);
4714 if (IS_INSN(list)) {
4715 iobj = (
INSN *)list;
4717 if (unified_insns_data[
id] != 0) {
4718 const int *
const *entry = unified_insns_data[id];
4719 for (j = 1; j < (intptr_t)entry[0]; j++) {
4720 const int *unified = entry[j];
4722 for (k = 2; k < unified[1]; k++) {
4724 ((
INSN *)li)->insn_id != unified[k]) {
4731 new_unified_insn(iseq, unified[0], unified[1] - 1,
4736 niobj->link.next = li;
4755all_string_result_p(
const NODE *node)
4757 if (!node)
return FALSE;
4758 switch (nd_type(node)) {
4759 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4761 case NODE_IF:
case NODE_UNLESS:
4762 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4763 if (all_string_result_p(RNODE_IF(node)->nd_body))
4764 return all_string_result_p(RNODE_IF(node)->nd_else);
4766 case NODE_AND:
case NODE_OR:
4767 if (!RNODE_AND(node)->nd_2nd)
4768 return all_string_result_p(RNODE_AND(node)->nd_1st);
4769 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4771 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4781 const NODE *lit_node;
4789 VALUE s = rb_str_new_mutable_parser_string(str);
4791 VALUE error = rb_reg_check_preprocess(s);
4792 if (!
NIL_P(error)) {
4793 COMPILE_ERROR(args->iseq, nd_line(node),
"%" PRIsVALUE, error);
4797 if (
NIL_P(args->lit)) {
4799 args->lit_node = node;
4808flush_dstr_fragment(
struct dstr_ctxt *args)
4810 if (!
NIL_P(args->lit)) {
4812 VALUE lit = args->lit;
4814 lit = rb_fstring(lit);
4815 ADD_INSN1(args->ret, args->lit_node, putobject, lit);
4822compile_dstr_fragments_0(
struct dstr_ctxt *args,
const NODE *
const node)
4824 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4828 CHECK(append_dstr_fragment(args, node, str));
4832 const NODE *
const head = list->nd_head;
4833 if (nd_type_p(head, NODE_STR)) {
4834 CHECK(append_dstr_fragment(args, node, RNODE_STR(head)->
string));
4836 else if (nd_type_p(head, NODE_DSTR)) {
4837 CHECK(compile_dstr_fragments_0(args, head));
4840 flush_dstr_fragment(args);
4842 CHECK(COMPILE(args->ret,
"each string", head));
4854 .iseq = iseq, .ret = ret,
4855 .lit =
Qnil, .lit_node = NULL,
4856 .cnt = 0, .dregx = dregx,
4858 CHECK(compile_dstr_fragments_0(&args, node));
4859 flush_dstr_fragment(&args);
4869 while (node && nd_type_p(node, NODE_BLOCK)) {
4870 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4871 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4872 node = RNODE_BLOCK(node)->nd_next;
4875 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4884 if (!RNODE_DSTR(node)->nd_next) {
4885 VALUE lit = rb_node_dstr_string_val(node);
4886 ADD_INSN1(ret, node, dupstring, lit);
4887 RB_OBJ_SET_SHAREABLE(lit);
4891 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, FALSE));
4892 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4901 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
4903 if (!RNODE_DREGX(node)->nd_next) {
4905 VALUE src = rb_node_dregx_string_val(node);
4906 VALUE match = iseq_reg_compile(iseq, src, cflag, NULL, 0);
4907 ADD_INSN1(ret, node, putobject, match);
4913 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, TRUE));
4917 ADD_INSN(ret, node, pop);
4927 const int line = nd_line(node);
4928 LABEL *lend = NEW_LABEL(line);
4929 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4930 + VM_SVAR_FLIPFLOP_START;
4933 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4934 ADD_INSNL(ret, node, branchif, lend);
4937 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
4938 ADD_INSNL(ret, node, branchunless, else_label);
4939 ADD_INSN1(ret, node, putobject,
Qtrue);
4940 ADD_INSN1(ret, node, setspecial, key);
4942 ADD_INSNL(ret, node, jump, then_label);
4946 ADD_LABEL(ret, lend);
4947 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
4948 ADD_INSNL(ret, node, branchunless, then_label);
4949 ADD_INSN1(ret, node, putobject,
Qfalse);
4950 ADD_INSN1(ret, node, setspecial, key);
4951 ADD_INSNL(ret, node, jump, then_label);
4960#define COMPILE_SINGLE 2
4967 LABEL *label = NEW_LABEL(nd_line(cond));
4968 if (!then_label) then_label = label;
4969 else if (!else_label) else_label = label;
4971 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
4973 if (LIST_INSN_SIZE_ONE(seq)) {
4974 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
4975 if (insn->insn_id == BIN(jump) && (
LABEL *)(insn->operands[0]) == label)
4978 if (!label->refcnt) {
4979 return COMPILE_SINGLE;
4981 ADD_LABEL(seq, label);
4991 DECL_ANCHOR(ignore);
4994 switch (nd_type(cond)) {
4996 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
4997 cond = RNODE_AND(cond)->nd_2nd;
4998 if (ok == COMPILE_SINGLE) {
4999 ADD_INSNL(ret, cond, jump, else_label);
5000 INIT_ANCHOR(ignore);
5002 then_label = NEW_LABEL(nd_line(cond));
5006 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
5007 cond = RNODE_OR(cond)->nd_2nd;
5008 if (ok == COMPILE_SINGLE) {
5009 ADD_INSNL(ret, cond, jump, then_label);
5010 INIT_ANCHOR(ignore);
5012 else_label = NEW_LABEL(nd_line(cond));
5022 case NODE_IMAGINARY:
5029 ADD_INSNL(ret, cond, jump, then_label);
5034 ADD_INSNL(ret, cond, jump, else_label);
5040 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
5041 ADD_INSNL(ret, cond, jump, then_label);
5044 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
5047 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
5050 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
5054 DECL_ANCHOR(cond_seq);
5055 INIT_ANCHOR(cond_seq);
5057 CHECK(COMPILE(cond_seq,
"branch condition", cond));
5059 if (LIST_INSN_SIZE_ONE(cond_seq)) {
5060 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
5061 if (insn->insn_id == BIN(putobject)) {
5062 if (
RTEST(insn->operands[0])) {
5063 ADD_INSNL(ret, cond, jump, then_label);
5068 ADD_INSNL(ret, cond, jump, else_label);
5073 ADD_SEQ(ret, cond_seq);
5078 ADD_INSNL(ret, cond, branchunless, else_label);
5079 ADD_INSNL(ret, cond, jump, then_label);
5086keyword_node_p(
const NODE *
const node)
5088 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
5094 switch (nd_type(node)) {
5096 return rb_node_sym_string_val(node);
5098 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
5105 NODE *node = node_hash->nd_head;
5106 VALUE hash = rb_hash_new();
5109 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5110 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
5111 VALUE idx = rb_hash_aref(hash, key);
5116 rb_hash_aset(hash, key,
INT2FIX(i));
5126 const NODE *
const root_node,
5134 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
5135 const NODE *node = RNODE_HASH(root_node)->nd_head;
5139 const NODE *key_node = RNODE_LIST(node)->nd_head;
5143 if (key_node && nd_type_p(key_node, NODE_SYM)) {
5148 *flag |= VM_CALL_KW_SPLAT;
5149 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5154 *flag |= VM_CALL_KW_SPLAT_MUT;
5159 node = RNODE_LIST(node)->nd_next;
5160 node = RNODE_LIST(node)->nd_next;
5164 node = RNODE_HASH(root_node)->nd_head;
5167 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
5169 if (
len > VM_CALL_KW_LEN_MAX) {
5170 COMPILE_ERROR(ERROR_ARGS_AT(root_node)
"too many keyword arguments (%d, maximum is %d)",
5171 len, (
int)VM_CALL_KW_LEN_MAX);
5176 VALUE *keywords = kw_arg->keywords;
5179 kw_arg->references = 0;
5180 kw_arg->keyword_len =
len;
5182 *kw_arg_ptr = kw_arg;
5184 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5185 const NODE *key_node = RNODE_LIST(node)->nd_head;
5186 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5189 keywords[j] = get_symbol_value(iseq, key_node);
5193 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
5207 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
5209 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
5212 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5213 *kwnode_ptr = RNODE_LIST(node)->nd_head;
5216 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
5217 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
5225frozen_string_literal_p(
const rb_iseq_t *iseq)
5227 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
5231static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
5233 switch (nd_type(node)) {
5241 case NODE_IMAGINARY:
5248 return hash_key || frozen_string_literal_p(iseq);
5257 switch (nd_type(node)) {
5260 VALUE lit = rb_node_integer_literal_val(node);
5266 VALUE lit = rb_node_float_literal_val(node);
5272 case NODE_IMAGINARY:
5281 return rb_node_sym_string_val(node);
5283 return RB_OBJ_SET_SHAREABLE(rb_node_regx_string_val(node));
5285 return rb_node_line_lineno_val(node);
5287 return rb_node_encoding_val(node);
5290 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
5291 VALUE lit = get_string_value(node);
5292 VALUE str = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
5293 RB_OBJ_SET_SHAREABLE(str);
5297 return get_string_value(node);
5300 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
5307 const NODE *line_node = node;
5309 if (nd_type_p(node, NODE_ZLIST)) {
5311 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5316 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5319 for (; node; node = RNODE_LIST(node)->nd_next) {
5320 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5362 const int max_stack_len = 0x100;
5363 const int min_tmp_ary_len = 0x40;
5367#define FLUSH_CHUNK \
5369 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5370 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5371 first_chunk = FALSE; \
5379 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5381 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5382 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5385 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5390 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5391 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5392 RB_OBJ_SET_FROZEN_SHAREABLE(ary);
5397 ADD_INSN1(ret, line_node, duparray, ary);
5398 first_chunk = FALSE;
5401 ADD_INSN1(ret, line_node, putobject, ary);
5402 ADD_INSN(ret, line_node, concattoarray);
5404 RB_OBJ_SET_SHAREABLE(ary);
5410 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5412 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5415 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5417 if (stack_len == 0 && first_chunk) {
5418 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5423 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5424 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5428 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5433 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5443static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5445 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);
5451 const NODE *line_node = node;
5453 node = RNODE_HASH(node)->nd_head;
5455 if (!node || nd_type_p(node, NODE_ZLIST)) {
5457 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5462 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5465 for (; node; node = RNODE_LIST(node)->nd_next) {
5466 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5489 const int max_stack_len = 0x100;
5490 const int min_tmp_hash_len = 0x800;
5492 int first_chunk = 1;
5493 DECL_ANCHOR(anchor);
5494 INIT_ANCHOR(anchor);
5497#define FLUSH_CHUNK() \
5499 if (first_chunk) { \
5500 APPEND_LIST(ret, anchor); \
5501 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5504 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5505 ADD_INSN(ret, line_node, swap); \
5506 APPEND_LIST(ret, anchor); \
5507 ADD_SEND(ret, line_node, id_core_hash_merge_bang_ptr, INT2FIX(stack_len + 1)); \
5509 INIT_ANCHOR(anchor); \
5510 first_chunk = stack_len = 0; \
5517 if (static_literal_node_pair_p(node, iseq)) {
5519 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5520 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5523 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5528 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5530 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5532 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5539 hash = RB_OBJ_SET_FROZEN_SHAREABLE(hash);
5544 ADD_INSN1(ret, line_node, duphash, hash);
5548 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5549 ADD_INSN(ret, line_node, swap);
5551 ADD_INSN1(ret, line_node, putobject, hash);
5553 ADD_SEND(ret, line_node, id_core_hash_merge_bang_kwd,
INT2FIX(2));
5560 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5563 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5566 if (RNODE_LIST(node)->nd_head) {
5568 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5569 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5573 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5579 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5580 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5581 int first_kw = first_chunk && stack_len == 0;
5582 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5583 int only_kw = last_kw && first_kw;
5585 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5587 if (only_kw && method_call_keywords) {
5595 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5597 else if (first_kw) {
5601 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5608 if (only_kw && method_call_keywords) {
5614 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5621 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5622 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5623 else ADD_INSN(ret, line_node, swap);
5625 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5627 ADD_SEND(ret, line_node, id_core_hash_merge_bang_kwd,
INT2FIX(2));
5642rb_node_case_when_optimizable_literal(
const NODE *
const node)
5644 switch (nd_type(node)) {
5646 return rb_node_integer_literal_val(node);
5648 VALUE v = rb_node_float_literal_val(node);
5657 case NODE_IMAGINARY:
5666 return rb_node_sym_string_val(node);
5668 return rb_node_line_lineno_val(node);
5670 return rb_node_str_string_val(node);
5672 return rb_node_file_path_val(node);
5679 LABEL *l1,
int only_special_literals,
VALUE literals)
5682 const NODE *val = RNODE_LIST(vals)->nd_head;
5683 VALUE lit = rb_node_case_when_optimizable_literal(val);
5686 only_special_literals = 0;
5689 cdhash_aset_if_missing(literals, lit, (
VALUE)(l1));
5692 if (nd_type_p(val, NODE_STR) || nd_type_p(val, NODE_FILE)) {
5693 debugp_param(
"nd_lit", get_string_value(val));
5694 lit = get_string_value(val);
5695 ADD_INSN1(cond_seq, val, putobject, lit);
5699 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5703 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5704 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5705 ADD_INSNL(cond_seq, val, branchif, l1);
5706 vals = RNODE_LIST(vals)->nd_next;
5708 return only_special_literals;
5713 LABEL *l1,
int only_special_literals,
VALUE literals)
5715 const NODE *line_node = vals;
5717 switch (nd_type(vals)) {
5719 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5723 ADD_INSN (cond_seq, line_node, dup);
5724 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5725 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5726 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5727 ADD_INSNL(cond_seq, line_node, branchif, l1);
5730 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5731 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5734 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5735 ADD_INSN (cond_seq, line_node, dup);
5736 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5737 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5738 ADD_INSNL(cond_seq, line_node, branchif, l1);
5741 ADD_INSN (cond_seq, line_node, dup);
5742 CHECK(COMPILE(cond_seq,
"when val", vals));
5743 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5744 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5745 ADD_INSNL(cond_seq, line_node, branchif, l1);
5838 const NODE *line_node;
5853add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc,
INSN *before_insn)
5856 rb_bug(
"no masgn_state");
5865 memo->before_insn = before_insn;
5866 memo->line_node = line_node;
5867 memo->argn = state->num_args + 1;
5868 memo->num_args = argc;
5869 state->num_args += argc;
5870 memo->lhs_pos = lhs_pos;
5872 if (!state->first_memo) {
5873 state->first_memo = memo;
5876 state->last_memo->next = memo;
5878 state->last_memo = memo;
5888 switch (nd_type(node)) {
5889 case NODE_ATTRASGN: {
5891 const NODE *line_node = node;
5893 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5895 bool safenav_call =
false;
5897 iobj = (
INSN *)get_prev_insn((
INSN *)insn_element);
5899 ELEM_REMOVE(insn_element);
5900 if (!IS_INSN_ID(iobj, send)) {
5901 safenav_call =
true;
5902 iobj = (
INSN *)get_prev_insn(iobj);
5903 ELEM_INSERT_NEXT(&iobj->link, insn_element);
5905 (pre->last = iobj->link.prev)->next = 0;
5908 int argc = vm_ci_argc(ci) + 1;
5909 ci = ci_argc_set(iseq, ci, argc);
5910 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5914 ADD_INSN(lhs, line_node, swap);
5917 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5920 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (
INSN *)LAST_ELEMENT(lhs))) {
5924 iobj->link.prev = lhs->last;
5925 lhs->last->next = &iobj->link;
5926 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
5927 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5928 int argc = vm_ci_argc(ci);
5929 bool dupsplat =
false;
5930 ci = ci_argc_set(iseq, ci, argc - 1);
5931 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
5938 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
5940 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5949 int line_no = nd_line(line_node);
5950 int node_id = nd_node_id(line_node);
5953 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5954 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
5955 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5957 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
5959 if (!safenav_call) {
5960 ADD_INSN(lhs, line_node, pop);
5962 ADD_INSN(lhs, line_node, pop);
5965 for (
int i=0; i < argc; i++) {
5966 ADD_INSN(post, line_node, pop);
5971 DECL_ANCHOR(nest_rhs);
5972 INIT_ANCHOR(nest_rhs);
5973 DECL_ANCHOR(nest_lhs);
5974 INIT_ANCHOR(nest_lhs);
5976 int prev_level = state->lhs_level;
5977 bool prev_nested = state->nested;
5979 state->lhs_level = lhs_pos - 1;
5980 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5981 state->lhs_level = prev_level;
5982 state->nested = prev_nested;
5984 ADD_SEQ(lhs, nest_rhs);
5985 ADD_SEQ(lhs, nest_lhs);
5989 if (!RNODE_CDECL(node)->nd_vid) {
5993 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5996 iobj = (
INSN *)insn_element;
5999 ELEM_REMOVE(insn_element);
6000 pre->last = iobj->link.prev;
6003 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (
INSN *)LAST_ELEMENT(lhs))) {
6007 ADD_INSN(post, node, pop);
6012 DECL_ANCHOR(anchor);
6013 INIT_ANCHOR(anchor);
6014 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
6015 ELEM_REMOVE(FIRST_ELEMENT(anchor));
6016 ADD_SEQ(lhs, anchor);
6027 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
6028 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
6035 const NODE *rhsn,
const NODE *orig_lhsn)
6038 const int memsize = numberof(mem);
6040 int llen = 0, rlen = 0;
6042 const NODE *lhsn = orig_lhsn;
6044#define MEMORY(v) { \
6046 if (memindex == memsize) return 0; \
6047 for (i=0; i<memindex; i++) { \
6048 if (mem[i] == (v)) return 0; \
6050 mem[memindex++] = (v); \
6053 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
6058 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
6059 switch (nd_type(ln)) {
6064 MEMORY(get_nd_vid(ln));
6069 lhsn = RNODE_LIST(lhsn)->nd_next;
6075 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
6078 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
6080 rhsn = RNODE_LIST(rhsn)->nd_next;
6085 for (i=0; i<llen-rlen; i++) {
6086 ADD_INSN(ret, orig_lhsn, putnil);
6090 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
6097 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
6098 const NODE *splatn = RNODE_MASGN(node)->nd_args;
6099 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
6100 const NODE *lhsn_count = lhsn;
6101 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
6106 while (lhsn_count) {
6108 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
6111 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
6113 lhsn = RNODE_LIST(lhsn)->nd_next;
6117 if (nd_type_p(splatn, NODE_POSTARG)) {
6119 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
6120 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
6121 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
6123 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
6125 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
6127 if (NODE_NAMED_REST_P(restn)) {
6128 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
6131 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
6133 postn = RNODE_LIST(postn)->nd_next;
6138 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
6142 if (!state->nested) {
6143 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
6147 ADD_INSN(rhs, node, dup);
6149 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
6156 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
6158 state.lhs_level = popped ? 0 : 1;
6161 state.first_memo = NULL;
6162 state.last_memo = NULL;
6172 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
6176 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
6177 for (
int i = 0; i < memo->num_args; i++) {
6178 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
6180 tmp_memo = memo->next;
6189 if (!popped && state.num_args >= 1) {
6191 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
6203 switch (nd_type(node)) {
6205 rb_ary_unshift(arr,
ID2SYM(RNODE_CONST(node)->nd_vid));
6206 RB_OBJ_SET_SHAREABLE(arr);
6209 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6210 rb_ary_unshift(arr,
ID2SYM(idNULL));
6211 RB_OBJ_SET_SHAREABLE(arr);
6214 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6215 node = RNODE_COLON2(node)->nd_head;
6224compile_const_prefix(
rb_iseq_t *iseq,
const NODE *
const node,
6227 switch (nd_type(node)) {
6229 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
6230 ADD_INSN1(body, node, putobject,
Qtrue);
6231 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
6234 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
6235 ADD_INSN(body, node, pop);
6236 ADD_INSN1(body, node, putobject,
rb_cObject);
6237 ADD_INSN1(body, node, putobject,
Qtrue);
6238 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6241 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
6242 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
6243 ADD_INSN1(body, node, putobject,
Qfalse);
6244 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6247 CHECK(COMPILE(pref,
"const colon2 prefix", node));
6254cpath_const_p(
const NODE *node)
6256 switch (nd_type(node)) {
6261 if (RNODE_COLON2(node)->nd_head) {
6262 return cpath_const_p(RNODE_COLON2(node)->nd_head);
6273 if (nd_type_p(cpath, NODE_COLON3)) {
6275 ADD_INSN1(ret, cpath, putobject,
rb_cObject);
6276 return VM_DEFINECLASS_FLAG_SCOPED;
6278 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
6280 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
6281 int flags = VM_DEFINECLASS_FLAG_SCOPED;
6282 if (!cpath_const_p(RNODE_COLON2(cpath)->nd_head)) {
6283 flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
6289 ADD_INSN1(ret, cpath, putspecialobject,
6290 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
6296private_recv_p(
const NODE *node)
6298 NODE *recv = get_nd_recv(node);
6299 if (recv && nd_type_p(recv, NODE_SELF)) {
6300 return RNODE_SELF(recv)->nd_state != 0;
6307 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore);
6310compile_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);
6317 enum defined_type expr_type = DEFINED_NOT_DEFINED;
6318 enum node_type
type;
6319 const int line = nd_line(node);
6320 const NODE *line_node = node;
6322 switch (
type = nd_type(node)) {
6326 expr_type = DEFINED_NIL;
6329 expr_type = DEFINED_SELF;
6332 expr_type = DEFINED_TRUE;
6335 expr_type = DEFINED_FALSE;
6340 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
6344 if (RNODE_LIST(vals)->nd_head) {
6345 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
6348 lfinish[1] = NEW_LABEL(line);
6350 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6352 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6365 case NODE_IMAGINARY:
6370 expr_type = DEFINED_EXPR;
6374 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6376 lfinish[1] = NEW_LABEL(line);
6378 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6379 expr_type = DEFINED_EXPR;
6385 expr_type = DEFINED_LVAR;
6388#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6390 ADD_INSN3(ret, line_node, definedivar,
6391 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6395 ADD_INSN(ret, line_node, putnil);
6396 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6397 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6401 ADD_INSN(ret, line_node, putnil);
6402 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6403 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6407 ADD_INSN(ret, line_node, putnil);
6408 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6409 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6413 lfinish[1] = NEW_LABEL(line);
6415 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6416 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6417 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6420 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6421 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6424 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6425 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6429 ADD_INSN1(ret, line_node, putobject,
rb_cObject);
6430 ADD_INSN3(ret, line_node, defined,
6431 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6439 case NODE_ATTRASGN:{
6440 const int explicit_receiver =
6441 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6442 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6444 if (get_nd_args(node) || explicit_receiver) {
6446 lfinish[1] = NEW_LABEL(line);
6449 lfinish[2] = NEW_LABEL(line);
6452 if (get_nd_args(node)) {
6453 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6454 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6456 if (explicit_receiver) {
6457 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6458 switch (nd_type(get_nd_recv(node))) {
6464 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6465 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6468 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6469 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6473 ADD_INSN(ret, line_node, dup);
6475 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6476 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6479 ADD_INSN(ret, line_node, putself);
6481 ADD_INSN(ret, line_node, dup);
6483 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6484 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6490 ADD_INSN(ret, line_node, putnil);
6491 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6492 PUSH_VAL(DEFINED_YIELD));
6493 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6498 ADD_INSN(ret, line_node, putnil);
6499 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6500 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6501 PUSH_VAL(DEFINED_GVAR));
6506 ADD_INSN(ret, line_node, putnil);
6507 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6508 PUSH_VAL(DEFINED_ZSUPER));
6514 case NODE_OP_ASGN_OR:
6515 case NODE_OP_ASGN_AND:
6524 expr_type = DEFINED_ASGN;
6531 VALUE str = rb_iseq_defined_string(expr_type);
6532 ADD_INSN1(ret, line_node, putobject, str);
6535 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6542 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6543 iseq_set_exception_local_table(iseq);
6548 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore)
6551 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6553 int line = nd_line(node);
6554 LABEL *lstart = NEW_LABEL(line);
6555 LABEL *lend = NEW_LABEL(line);
6558 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6559 rescue = NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc,
6561 ISEQ_BODY(iseq)->location.label),
6562 ISEQ_TYPE_RESCUE, 0);
6563 lstart->rescued = LABEL_RESCUE_BEG;
6564 lend->rescued = LABEL_RESCUE_END;
6565 APPEND_LABEL(ret, lcur, lstart);
6566 ADD_LABEL(ret, lend);
6568 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6576 const int line = nd_line(node);
6577 const NODE *line_node = node;
6578 if (!RNODE_DEFINED(node)->nd_head) {
6579 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6580 ADD_INSN1(ret, line_node, putobject, str);
6585 lfinish[0] = NEW_LABEL(line);
6588 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6590 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6591 ADD_INSN(ret, line_node, swap);
6593 ADD_LABEL(ret, lfinish[2]);
6595 ADD_INSN(ret, line_node, pop);
6596 ADD_LABEL(ret, lfinish[1]);
6598 ADD_LABEL(ret, lfinish[0]);
6604make_name_for_block(
const rb_iseq_t *orig_iseq)
6609 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6610 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6611 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6614 iseq = ISEQ_BODY(iseq)->parent_iseq;
6619 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6622 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6631 enl->ensure_node = node;
6632 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6634 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6644 while (erange->next != 0) {
6645 erange = erange->next;
6649 ne->end = erange->end;
6650 erange->end = lstart;
6656can_add_ensure_iseq(
const rb_iseq_t *iseq)
6659 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
6661 if (e->ensure_node)
return false;
6674 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6676 DECL_ANCHOR(ensure);
6678 INIT_ANCHOR(ensure);
6680 if (enlp->erange != NULL) {
6681 DECL_ANCHOR(ensure_part);
6682 LABEL *lstart = NEW_LABEL(0);
6683 LABEL *lend = NEW_LABEL(0);
6684 INIT_ANCHOR(ensure_part);
6686 add_ensure_range(iseq, enlp->erange, lstart, lend);
6688 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6689 ADD_LABEL(ensure_part, lstart);
6690 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6691 ADD_LABEL(ensure_part, lend);
6692 ADD_SEQ(ensure, ensure_part);
6701 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6702 ADD_SEQ(ret, ensure);
6707check_keyword(
const NODE *node)
6711 if (nd_type_p(node, NODE_LIST)) {
6712 while (RNODE_LIST(node)->nd_next) {
6713 node = RNODE_LIST(node)->nd_next;
6715 node = RNODE_LIST(node)->nd_head;
6718 return keyword_node_p(node);
6723keyword_node_single_splat_p(
NODE *kwnode)
6727 NODE *node = RNODE_HASH(kwnode)->nd_head;
6728 return RNODE_LIST(node)->nd_head == NULL &&
6729 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6734 NODE *kwnode,
unsigned int *flag_ptr)
6736 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6737 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6738 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6739 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6740 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6743#define SPLATARRAY_FALSE 0
6744#define SPLATARRAY_TRUE 1
6745#define DUP_SINGLE_KW_SPLAT 2
6749 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6751 if (!argn)
return 0;
6753 NODE *kwnode = NULL;
6755 switch (nd_type(argn)) {
6758 int len = compile_args(iseq, args, argn, &kwnode);
6759 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6762 if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6766 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6767 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6770 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6779 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6780 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6781 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6782 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6783 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6786 case NODE_ARGSCAT: {
6787 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6788 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6789 bool args_pushed =
false;
6791 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6792 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6793 if (kwnode) rest_len--;
6794 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6798 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6799 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6802 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6803 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6804 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6807 else if (!args_pushed) {
6808 ADD_INSN(args, argn, concattoarray);
6814 *flag_ptr |= VM_CALL_KW_SPLAT;
6815 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6821 case NODE_ARGSPUSH: {
6822 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6823 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6825 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6826 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6827 if (kwnode) rest_len--;
6828 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6829 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6832 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6833 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6836 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6837 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6843 *flag_ptr |= VM_CALL_KW_SPLAT;
6844 if (!keyword_node_single_splat_p(kwnode)) {
6845 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6846 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6848 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
6849 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6852 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6860 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
6866setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
6868 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
6869 *flag |= VM_CALL_ARGS_SPLAT_MUT;
6874setup_args_dup_rest_p(
const NODE *argn)
6876 switch(nd_type(argn)) {
6887 case NODE_IMAGINARY:
6900 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
6903 if (setup_args_dup_rest_p(RNODE_LIST(argn)->nd_head)) {
6906 argn = RNODE_LIST(argn)->nd_next;
6919 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
6922 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
6923 RNODE_BLOCK_PASS(argn)->nd_head : argn;
6926 switch(nd_type(check_arg)) {
6929 dup_rest = SPLATARRAY_FALSE;
6933 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
6935 case(NODE_ARGSPUSH):
6937 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
6938 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
6939 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
6940 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
6941 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
6943 if (dup_rest == SPLATARRAY_FALSE) {
6945 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
6947 NODE *key_node = RNODE_LIST(node)->nd_head;
6948 if (key_node && setup_args_dup_rest_p(key_node)) {
6949 dup_rest = SPLATARRAY_TRUE;
6953 node = RNODE_LIST(node)->nd_next;
6954 NODE *value_node = RNODE_LIST(node)->nd_head;
6955 if (setup_args_dup_rest_p(value_node)) {
6956 dup_rest = SPLATARRAY_TRUE;
6960 node = RNODE_LIST(node)->nd_next;
6969 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
6971 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
6974 initial_dup_rest = dup_rest;
6976 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
6977 DECL_ANCHOR(arg_block);
6978 INIT_ANCHOR(arg_block);
6980 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
6981 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
6983 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
6984 const NODE * arg_node =
6985 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
6992 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
6993 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
6996 *flag |= VM_CALL_FORWARDING;
6998 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
6999 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
7003 *flag |= VM_CALL_ARGS_BLOCKARG;
7005 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
7008 if (LIST_INSN_SIZE_ONE(arg_block)) {
7010 if (IS_INSN(elem)) {
7012 if (iobj->insn_id == BIN(getblockparam)) {
7013 iobj->insn_id = BIN(getblockparamproxy);
7017 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
7018 ADD_SEQ(args, arg_block);
7021 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
7023 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
7030 const NODE *body = ptr;
7031 int line = nd_line(body);
7033 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
7035 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7036 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
7038 iseq_set_local_table(iseq, 0, 0);
7046 int line = nd_line(node);
7047 const NODE *line_node = node;
7048 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
7050#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
7051 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
7055 ADD_INSN(ret, line_node, dup);
7056 ADD_INSNL(ret, line_node, branchunless, fail_label);
7058 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
7060 if (RNODE_BLOCK(vars)->nd_next) {
7061 ADD_INSN(ret, line_node, dup);
7064 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
7066 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
7069#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
7070 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
7075 ADD_INSNL(nom, line_node, jump, end_label);
7076 ADD_LABEL(nom, fail_label);
7078 ADD_INSN(nom, line_node, pop);
7079 ADD_INSN(nom, line_node, putnil);
7081 ADD_LABEL(nom, end_label);
7082 (nom->last->next = cap->link.next)->prev = nom->last;
7083 (cap->link.next = nom->anchor.next)->prev = &cap->link;
7088 ADD_INSNL(ret, line_node, jump, end_label);
7089 ADD_LABEL(ret, fail_label);
7090 ADD_INSN(ret, line_node, pop);
7091 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
7093 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
7095 ((
INSN*)last)->insn_id = BIN(putnil);
7096 ((
INSN*)last)->operand_size = 0;
7098 ADD_LABEL(ret, end_label);
7102optimizable_range_item_p(
const NODE *n)
7104 if (!n)
return FALSE;
7105 switch (nd_type(n)) {
7118optimized_range_item(
const NODE *n)
7120 switch (nd_type(n)) {
7122 return rb_node_line_lineno_val(n);
7124 return rb_node_integer_literal_val(n);
7126 return rb_node_float_literal_val(n);
7128 return rb_node_rational_literal_val(n);
7129 case NODE_IMAGINARY:
7130 return rb_node_imaginary_literal_val(n);
7134 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
7141 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
7142 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
7144 const int line = nd_line(node);
7145 const NODE *line_node = node;
7146 DECL_ANCHOR(cond_seq);
7147 LABEL *then_label, *else_label, *end_label;
7150 INIT_ANCHOR(cond_seq);
7151 then_label = NEW_LABEL(line);
7152 else_label = NEW_LABEL(line);
7155 NODE *cond = RNODE_IF(node)->nd_cond;
7156 if (nd_type(cond) == NODE_BLOCK) {
7157 cond = RNODE_BLOCK(cond)->nd_head;
7160 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
7161 ADD_SEQ(ret, cond_seq);
7163 if (then_label->refcnt && else_label->refcnt) {
7164 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
7167 if (then_label->refcnt) {
7168 ADD_LABEL(ret, then_label);
7170 DECL_ANCHOR(then_seq);
7171 INIT_ANCHOR(then_seq);
7172 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
7174 if (else_label->refcnt) {
7175 const NODE *
const coverage_node = node_body ? node_body : node;
7176 add_trace_branch_coverage(
7179 nd_code_loc(coverage_node),
7180 nd_node_id(coverage_node),
7182 type == NODE_IF ?
"then" :
"else",
7184 end_label = NEW_LABEL(line);
7185 ADD_INSNL(then_seq, line_node, jump, end_label);
7187 ADD_INSN(then_seq, line_node, pop);
7190 ADD_SEQ(ret, then_seq);
7193 if (else_label->refcnt) {
7194 ADD_LABEL(ret, else_label);
7196 DECL_ANCHOR(else_seq);
7197 INIT_ANCHOR(else_seq);
7198 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
7200 if (then_label->refcnt) {
7201 const NODE *
const coverage_node = node_else ? node_else : node;
7202 add_trace_branch_coverage(
7205 nd_code_loc(coverage_node),
7206 nd_node_id(coverage_node),
7208 type == NODE_IF ?
"else" :
"then",
7211 ADD_SEQ(ret, else_seq);
7215 ADD_LABEL(ret, end_label);
7225 const NODE *node = orig_node;
7226 LABEL *endlabel, *elselabel;
7228 DECL_ANCHOR(body_seq);
7229 DECL_ANCHOR(cond_seq);
7230 int only_special_literals = 1;
7231 VALUE literals = cdhash_new(0);
7233 enum node_type
type;
7234 const NODE *line_node;
7239 INIT_ANCHOR(body_seq);
7240 INIT_ANCHOR(cond_seq);
7242 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
7244 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"case");
7246 node = RNODE_CASE(node)->nd_body;
7247 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
7248 type = nd_type(node);
7249 line = nd_line(node);
7252 endlabel = NEW_LABEL(line);
7253 elselabel = NEW_LABEL(line);
7257 while (
type == NODE_WHEN) {
7260 l1 = NEW_LABEL(line);
7261 ADD_LABEL(body_seq, l1);
7262 ADD_INSN(body_seq, line_node, pop);
7264 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7265 add_trace_branch_coverage(
7268 nd_code_loc(coverage_node),
7269 nd_node_id(coverage_node),
7274 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
7275 ADD_INSNL(body_seq, line_node, jump, endlabel);
7277 vals = RNODE_WHEN(node)->nd_head;
7279 switch (nd_type(vals)) {
7281 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
7282 if (only_special_literals < 0)
return COMPILE_NG;
7287 only_special_literals = 0;
7288 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
7291 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
7295 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
7298 node = RNODE_WHEN(node)->nd_next;
7302 type = nd_type(node);
7303 line = nd_line(node);
7308 ADD_LABEL(cond_seq, elselabel);
7309 ADD_INSN(cond_seq, line_node, pop);
7310 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
7311 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7312 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7315 debugs(
"== else (implicit)\n");
7316 ADD_LABEL(cond_seq, elselabel);
7317 ADD_INSN(cond_seq, orig_node, pop);
7318 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
7320 ADD_INSN(cond_seq, orig_node, putnil);
7322 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7325 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7326 ADD_INSN(ret, orig_node, dup);
7327 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
7329 LABEL_REF(elselabel);
7332 ADD_SEQ(ret, cond_seq);
7333 ADD_SEQ(ret, body_seq);
7334 ADD_LABEL(ret, endlabel);
7343 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
7345 DECL_ANCHOR(body_seq);
7349 branches = decl_branch_base(iseq, nd_node_id(orig_node), nd_code_loc(orig_node),
"case");
7351 INIT_ANCHOR(body_seq);
7352 endlabel = NEW_LABEL(nd_line(node));
7354 while (node && nd_type_p(node, NODE_WHEN)) {
7355 const int line = nd_line(node);
7356 LABEL *l1 = NEW_LABEL(line);
7357 ADD_LABEL(body_seq, l1);
7359 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7360 add_trace_branch_coverage(
7363 nd_code_loc(coverage_node),
7364 nd_node_id(coverage_node),
7369 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7370 ADD_INSNL(body_seq, node, jump, endlabel);
7372 vals = RNODE_WHEN(node)->nd_head;
7374 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7376 switch (nd_type(vals)) {
7380 val = RNODE_LIST(vals)->nd_head;
7381 lnext = NEW_LABEL(nd_line(val));
7382 debug_compile(
"== when2\n", (
void)0);
7383 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7384 ADD_LABEL(ret, lnext);
7385 vals = RNODE_LIST(vals)->nd_next;
7391 ADD_INSN(ret, vals, putnil);
7392 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7393 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7394 ADD_INSNL(ret, vals, branchif, l1);
7397 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7399 node = RNODE_WHEN(node)->nd_next;
7402 const NODE *
const coverage_node = node ? node : orig_node;
7403 add_trace_branch_coverage(
7406 nd_code_loc(coverage_node),
7407 nd_node_id(coverage_node),
7411 CHECK(COMPILE_(ret,
"else", node, popped));
7412 ADD_INSNL(ret, orig_node, jump, endlabel);
7414 ADD_SEQ(ret, body_seq);
7415 ADD_LABEL(ret, endlabel);
7419static 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);
7421static 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);
7422static 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);
7423static int iseq_compile_pattern_set_general_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7424static 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);
7425static int iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7427#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7428#define CASE3_BI_OFFSET_ERROR_STRING 1
7429#define CASE3_BI_OFFSET_KEY_ERROR_P 2
7430#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7431#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7434iseq_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)
7436 const int line = nd_line(node);
7437 const NODE *line_node = node;
7439 switch (nd_type(node)) {
7493 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7494 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7495 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7497 const int min_argc = pre_args_num + post_args_num;
7498 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7499 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7501 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7503 match_failed = NEW_LABEL(line);
7504 type_error = NEW_LABEL(line);
7505 deconstruct = NEW_LABEL(line);
7506 deconstructed = NEW_LABEL(line);
7509 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7510 ADD_INSN(ret, line_node, swap);
7516 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7518 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7520 ADD_INSN(ret, line_node, dup);
7521 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7522 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7523 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7524 if (in_single_pattern) {
7525 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7526 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7527 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7528 INT2FIX(min_argc), base_index + 1 ));
7530 ADD_INSNL(ret, line_node, branchunless, match_failed);
7532 for (i = 0; i < pre_args_num; i++) {
7533 ADD_INSN(ret, line_node, dup);
7534 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7535 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7536 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7537 args = RNODE_LIST(args)->nd_next;
7540 if (RNODE_ARYPTN(node)->rest_arg) {
7541 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7542 ADD_INSN(ret, line_node, dup);
7543 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7544 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7545 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7546 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7547 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7548 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7549 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7551 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7554 if (post_args_num > 0) {
7555 ADD_INSN(ret, line_node, dup);
7556 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7557 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7558 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7559 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7560 ADD_INSN(ret, line_node, pop);
7565 args = RNODE_ARYPTN(node)->post_args;
7566 for (i = 0; i < post_args_num; i++) {
7567 ADD_INSN(ret, line_node, dup);
7569 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7570 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7571 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7573 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7574 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7575 args = RNODE_LIST(args)->nd_next;
7578 ADD_INSN(ret, line_node, pop);
7580 ADD_INSN(ret, line_node, pop);
7582 ADD_INSNL(ret, line_node, jump, matched);
7583 ADD_INSN(ret, line_node, putnil);
7585 ADD_INSN(ret, line_node, putnil);
7588 ADD_LABEL(ret, type_error);
7589 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7591 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7592 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7593 ADD_INSN(ret, line_node, pop);
7595 ADD_LABEL(ret, match_failed);
7596 ADD_INSN(ret, line_node, pop);
7598 ADD_INSN(ret, line_node, pop);
7600 ADD_INSNL(ret, line_node, jump, unmatched);
7653 const NODE *args = RNODE_FNDPTN(node)->args;
7654 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7656 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7657 match_failed = NEW_LABEL(line);
7658 type_error = NEW_LABEL(line);
7659 deconstruct = NEW_LABEL(line);
7660 deconstructed = NEW_LABEL(line);
7662 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7664 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7666 ADD_INSN(ret, line_node, dup);
7667 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7668 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7669 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7670 if (in_single_pattern) {
7671 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 ));
7673 ADD_INSNL(ret, line_node, branchunless, match_failed);
7676 LABEL *while_begin = NEW_LABEL(nd_line(node));
7677 LABEL *next_loop = NEW_LABEL(nd_line(node));
7678 LABEL *find_succeeded = NEW_LABEL(line);
7679 LABEL *find_failed = NEW_LABEL(nd_line(node));
7682 ADD_INSN(ret, line_node, dup);
7683 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7685 ADD_INSN(ret, line_node, dup);
7686 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7687 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7689 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7691 ADD_LABEL(ret, while_begin);
7693 ADD_INSN(ret, line_node, dup);
7694 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7695 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7696 ADD_INSNL(ret, line_node, branchunless, find_failed);
7698 for (j = 0; j < args_num; j++) {
7699 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7700 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7702 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7703 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7705 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7707 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7708 args = RNODE_LIST(args)->nd_next;
7711 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7712 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7713 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7714 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7715 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7716 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));
7718 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7719 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7720 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7721 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7722 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7723 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7724 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7725 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));
7727 ADD_INSNL(ret, line_node, jump, find_succeeded);
7729 ADD_LABEL(ret, next_loop);
7730 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7731 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7732 ADD_INSNL(ret, line_node, jump, while_begin);
7734 ADD_LABEL(ret, find_failed);
7735 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7736 if (in_single_pattern) {
7737 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7738 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7739 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7740 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7741 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7743 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7744 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7746 ADD_INSN(ret, line_node, pop);
7747 ADD_INSN(ret, line_node, pop);
7749 ADD_INSNL(ret, line_node, jump, match_failed);
7750 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7752 ADD_LABEL(ret, find_succeeded);
7753 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7756 ADD_INSN(ret, line_node, pop);
7757 ADD_INSNL(ret, line_node, jump, matched);
7758 ADD_INSN(ret, line_node, putnil);
7760 ADD_LABEL(ret, type_error);
7761 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7763 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7764 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7765 ADD_INSN(ret, line_node, pop);
7767 ADD_LABEL(ret, match_failed);
7768 ADD_INSN(ret, line_node, pop);
7769 ADD_INSNL(ret, line_node, jump, unmatched);
7833 LABEL *match_failed, *type_error;
7836 match_failed = NEW_LABEL(line);
7837 type_error = NEW_LABEL(line);
7839 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
7840 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7841 keys =
rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
7843 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
7844 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
7848 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7850 ADD_INSN(ret, line_node, dup);
7851 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
7852 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7853 if (in_single_pattern) {
7854 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
7856 ADD_INSNL(ret, line_node, branchunless, match_failed);
7859 ADD_INSN(ret, line_node, putnil);
7862 RB_OBJ_SET_FROZEN_SHAREABLE(keys);
7863 ADD_INSN1(ret, line_node, duparray, keys);
7866 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
7868 ADD_INSN(ret, line_node, dup);
7870 ADD_INSNL(ret, line_node, branchunless, type_error);
7872 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7873 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
7876 if (RNODE_HSHPTN(node)->nd_pkwargs) {
7880 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7882 DECL_ANCHOR(match_values);
7883 INIT_ANCHOR(match_values);
7884 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
7885 for (i = 0; i < keys_num; i++) {
7886 NODE *key_node = RNODE_LIST(args)->nd_head;
7887 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
7888 VALUE key = get_symbol_value(iseq, key_node);
7890 ADD_INSN(ret, line_node, dup);
7891 ADD_INSN1(ret, line_node, putobject, key);
7892 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
7893 if (in_single_pattern) {
7894 LABEL *match_succeeded;
7895 match_succeeded = NEW_LABEL(line);
7897 ADD_INSN(ret, line_node, dup);
7898 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7901 ADD_INSN1(ret, line_node, putobject, RB_OBJ_SET_SHAREABLE(str));
7902 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
7903 ADD_INSN1(ret, line_node, putobject,
Qtrue);
7904 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
7905 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7906 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
7907 ADD_INSN1(ret, line_node, putobject, key);
7908 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
7910 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
7912 ADD_LABEL(ret, match_succeeded);
7914 ADD_INSNL(ret, line_node, branchunless, match_failed);
7916 ADD_INSN(match_values, line_node, dup);
7917 ADD_INSN1(match_values, line_node, putobject, key);
7918 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
7919 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7920 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
7922 ADD_SEQ(ret, match_values);
7926 ADD_INSN(ret, line_node, dup);
7927 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7928 if (in_single_pattern) {
7929 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
7931 ADD_INSNL(ret, line_node, branchunless, match_failed);
7934 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7935 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
7936 ADD_INSN(ret, line_node, dup);
7937 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7938 if (in_single_pattern) {
7939 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
7941 ADD_INSNL(ret, line_node, branchunless, match_failed);
7944 ADD_INSN(ret, line_node, dup);
7945 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7949 ADD_INSN(ret, line_node, pop);
7950 ADD_INSNL(ret, line_node, jump, matched);
7951 ADD_INSN(ret, line_node, putnil);
7953 ADD_LABEL(ret, type_error);
7954 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7956 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
7957 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7958 ADD_INSN(ret, line_node, pop);
7960 ADD_LABEL(ret, match_failed);
7961 ADD_INSN(ret, line_node, pop);
7962 ADD_INSNL(ret, line_node, jump, unmatched);
7971 case NODE_IMAGINARY:
7999 CHECK(COMPILE(ret,
"case in literal", node));
8000 if (in_single_pattern) {
8001 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
8003 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
8004 if (in_single_pattern) {
8005 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
8007 ADD_INSNL(ret, line_node, branchif, matched);
8008 ADD_INSNL(ret, line_node, jump, unmatched);
8012 ID id = RNODE_LASGN(node)->nd_vid;
8013 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8015 if (in_alt_pattern) {
8016 const char *name = rb_id2name(
id);
8017 if (name && strlen(name) > 0 && name[0] !=
'_') {
8018 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
8024 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8025 ADD_INSNL(ret, line_node, jump, matched);
8030 ID id = RNODE_DASGN(node)->nd_vid;
8032 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
8034 if (in_alt_pattern) {
8035 const char *name = rb_id2name(
id);
8036 if (name && strlen(name) > 0 && name[0] !=
'_') {
8037 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
8044 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
8048 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
8049 ADD_INSNL(ret, line_node, jump, matched);
8054 LABEL *match_failed;
8055 match_failed = unmatched;
8056 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
8057 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
8058 if (in_single_pattern) {
8059 LABEL *match_succeeded;
8060 match_succeeded = NEW_LABEL(line);
8062 ADD_INSN(ret, line_node, dup);
8063 if (nd_type_p(node, NODE_IF)) {
8064 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8067 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
8070 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
8071 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8072 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8073 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8075 ADD_INSN(ret, line_node, pop);
8076 ADD_INSN(ret, line_node, pop);
8078 ADD_LABEL(ret, match_succeeded);
8080 if (nd_type_p(node, NODE_IF)) {
8081 ADD_INSNL(ret, line_node, branchunless, match_failed);
8084 ADD_INSNL(ret, line_node, branchif, match_failed);
8086 ADD_INSNL(ret, line_node, jump, matched);
8091 LABEL *match_failed;
8092 match_failed = NEW_LABEL(line);
8094 n = RNODE_HASH(node)->nd_head;
8095 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
8096 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8100 ADD_INSN(ret, line_node, dup);
8101 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));
8102 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));
8103 ADD_INSN(ret, line_node, putnil);
8105 ADD_LABEL(ret, match_failed);
8106 ADD_INSN(ret, line_node, pop);
8107 ADD_INSNL(ret, line_node, jump, unmatched);
8111 LABEL *match_succeeded, *fin;
8112 match_succeeded = NEW_LABEL(line);
8113 fin = NEW_LABEL(line);
8115 ADD_INSN(ret, line_node, dup);
8116 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));
8117 ADD_LABEL(ret, match_succeeded);
8118 ADD_INSN(ret, line_node, pop);
8119 ADD_INSNL(ret, line_node, jump, matched);
8120 ADD_INSN(ret, line_node, putnil);
8121 ADD_LABEL(ret, fin);
8122 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
8126 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
8132iseq_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)
8134 LABEL *fin = NEW_LABEL(nd_line(node));
8135 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
8136 ADD_LABEL(ret, fin);
8141iseq_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)
8143 const NODE *line_node = node;
8145 if (RNODE_ARYPTN(node)->nd_pconst) {
8146 ADD_INSN(ret, line_node, dup);
8147 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
8148 if (in_single_pattern) {
8149 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
8151 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
8152 if (in_single_pattern) {
8153 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
8155 ADD_INSNL(ret, line_node, branchunless, match_failed);
8162iseq_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)
8164 const NODE *line_node = node;
8168 if (use_deconstructed_cache) {
8170 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8171 ADD_INSNL(ret, line_node, branchnil, deconstruct);
8174 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8175 ADD_INSNL(ret, line_node, branchunless, match_failed);
8178 ADD_INSN(ret, line_node, pop);
8179 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
8180 ADD_INSNL(ret, line_node, jump, deconstructed);
8183 ADD_INSNL(ret, line_node, jump, deconstruct);
8186 ADD_LABEL(ret, deconstruct);
8187 ADD_INSN(ret, line_node, dup);
8188 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
8189 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
8192 if (use_deconstructed_cache) {
8193 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
8196 if (in_single_pattern) {
8197 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
8200 ADD_INSNL(ret, line_node, branchunless, match_failed);
8202 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
8205 if (use_deconstructed_cache) {
8206 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8209 ADD_INSN(ret, line_node, dup);
8211 ADD_INSNL(ret, line_node, branchunless, type_error);
8213 ADD_LABEL(ret, deconstructed);
8229 const int line = nd_line(node);
8230 const NODE *line_node = node;
8231 LABEL *match_succeeded = NEW_LABEL(line);
8233 ADD_INSN(ret, line_node, dup);
8234 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8236 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8237 ADD_INSN1(ret, line_node, putobject, errmsg);
8238 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8239 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
8240 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8242 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8243 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8245 ADD_INSN(ret, line_node, pop);
8246 ADD_INSN(ret, line_node, pop);
8247 ADD_LABEL(ret, match_succeeded);
8263 const int line = nd_line(node);
8264 const NODE *line_node = node;
8265 LABEL *match_succeeded = NEW_LABEL(line);
8267 ADD_INSN(ret, line_node, dup);
8268 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8270 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8271 ADD_INSN1(ret, line_node, putobject, errmsg);
8272 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8273 ADD_INSN(ret, line_node, dup);
8274 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
8275 ADD_INSN1(ret, line_node, putobject, pattern_length);
8276 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
8277 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8279 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8280 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8282 ADD_INSN(ret, line_node, pop);
8283 ADD_INSN(ret, line_node, pop);
8284 ADD_LABEL(ret, match_succeeded);
8290iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
8300 const int line = nd_line(node);
8301 const NODE *line_node = node;
8302 LABEL *match_succeeded = NEW_LABEL(line);
8304 ADD_INSN(ret, line_node, dup);
8305 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8307 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8308 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
8309 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8310 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
8311 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
8312 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8314 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8315 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8317 ADD_INSN(ret, line_node, pop);
8318 ADD_INSN(ret, line_node, pop);
8320 ADD_LABEL(ret, match_succeeded);
8321 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
8322 ADD_INSN(ret, line_node, pop);
8323 ADD_INSN(ret, line_node, pop);
8331 const NODE *pattern;
8332 const NODE *node = orig_node;
8333 LABEL *endlabel, *elselabel;
8335 DECL_ANCHOR(body_seq);
8336 DECL_ANCHOR(cond_seq);
8338 enum node_type
type;
8339 const NODE *line_node;
8342 bool single_pattern;
8345 INIT_ANCHOR(body_seq);
8346 INIT_ANCHOR(cond_seq);
8348 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"case");
8350 node = RNODE_CASE3(node)->nd_body;
8351 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
8352 type = nd_type(node);
8353 line = nd_line(node);
8355 single_pattern = !RNODE_IN(node)->nd_next;
8357 endlabel = NEW_LABEL(line);
8358 elselabel = NEW_LABEL(line);
8360 if (single_pattern) {
8362 ADD_INSN(head, line_node, putnil);
8363 ADD_INSN(head, line_node, putnil);
8364 ADD_INSN1(head, line_node, putobject,
Qfalse);
8365 ADD_INSN(head, line_node, putnil);
8367 ADD_INSN(head, line_node, putnil);
8369 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8373 while (
type == NODE_IN) {
8377 ADD_INSN(body_seq, line_node, putnil);
8379 l1 = NEW_LABEL(line);
8380 ADD_LABEL(body_seq, l1);
8381 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8383 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8384 add_trace_branch_coverage(
8387 nd_code_loc(coverage_node),
8388 nd_node_id(coverage_node),
8393 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8394 ADD_INSNL(body_seq, line_node, jump, endlabel);
8396 pattern = RNODE_IN(node)->nd_head;
8398 int pat_line = nd_line(pattern);
8399 LABEL *next_pat = NEW_LABEL(pat_line);
8400 ADD_INSN (cond_seq, pattern, dup);
8402 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8403 ADD_LABEL(cond_seq, next_pat);
8404 LABEL_UNREMOVABLE(next_pat);
8407 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8411 node = RNODE_IN(node)->nd_next;
8415 type = nd_type(node);
8416 line = nd_line(node);
8421 ADD_LABEL(cond_seq, elselabel);
8422 ADD_INSN(cond_seq, line_node, pop);
8423 ADD_INSN(cond_seq, line_node, pop);
8424 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8425 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8426 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8427 ADD_INSN(cond_seq, line_node, putnil);
8429 ADD_INSN(cond_seq, line_node, putnil);
8433 debugs(
"== else (implicit)\n");
8434 ADD_LABEL(cond_seq, elselabel);
8435 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8436 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8438 if (single_pattern) {
8446 LABEL *key_error, *fin;
8449 key_error = NEW_LABEL(line);
8450 fin = NEW_LABEL(line);
8453 kw_arg->references = 0;
8454 kw_arg->keyword_len = 2;
8455 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
8456 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
8458 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8459 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8461 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8462 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8463 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8464 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8465 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8466 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8467 ADD_INSNL(cond_seq, orig_node, jump, fin);
8469 ADD_LABEL(cond_seq, key_error);
8471 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8472 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8473 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8474 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8475 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8476 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8477 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8478 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
8479 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8481 ADD_LABEL(cond_seq, fin);
8485 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8486 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8488 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8490 ADD_INSN(cond_seq, orig_node, putnil);
8492 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8493 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8495 ADD_INSN(cond_seq, line_node, putnil);
8499 ADD_SEQ(ret, cond_seq);
8500 ADD_SEQ(ret, body_seq);
8501 ADD_LABEL(ret, endlabel);
8505#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8506#undef CASE3_BI_OFFSET_ERROR_STRING
8507#undef CASE3_BI_OFFSET_KEY_ERROR_P
8508#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8509#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8514 const int line = (int)nd_line(node);
8515 const NODE *line_node = node;
8517 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8518 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8519 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8520 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8525 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8526 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8527 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8528 LABEL *end_label = NEW_LABEL(line);
8529 LABEL *adjust_label = NEW_LABEL(line);
8531 LABEL *next_catch_label = NEW_LABEL(line);
8532 LABEL *tmp_label = NULL;
8534 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8535 push_ensure_entry(iseq, &enl, NULL, NULL);
8537 if (RNODE_WHILE(node)->nd_state == 1) {
8538 ADD_INSNL(ret, line_node, jump, next_label);
8541 tmp_label = NEW_LABEL(line);
8542 ADD_INSNL(ret, line_node, jump, tmp_label);
8544 ADD_LABEL(ret, adjust_label);
8545 ADD_INSN(ret, line_node, putnil);
8546 ADD_LABEL(ret, next_catch_label);
8547 ADD_INSN(ret, line_node, pop);
8548 ADD_INSNL(ret, line_node, jump, next_label);
8549 if (tmp_label) ADD_LABEL(ret, tmp_label);
8551 ADD_LABEL(ret, redo_label);
8552 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8554 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8555 add_trace_branch_coverage(
8558 nd_code_loc(coverage_node),
8559 nd_node_id(coverage_node),
8564 CHECK(COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body));
8565 ADD_LABEL(ret, next_label);
8567 if (
type == NODE_WHILE) {
8568 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8569 redo_label, end_label));
8573 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8574 end_label, redo_label));
8577 ADD_LABEL(ret, end_label);
8578 ADD_ADJUST_RESTORE(ret, adjust_label);
8580 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8582 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8586 ADD_INSN(ret, line_node, putnil);
8589 ADD_LABEL(ret, break_label);
8592 ADD_INSN(ret, line_node, pop);
8595 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8597 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8599 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8600 ISEQ_COMPILE_DATA(iseq)->redo_label);
8602 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8603 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8604 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8605 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8606 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
8613 const int line = nd_line(node);
8614 const NODE *line_node = node;
8615 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8616 LABEL *retry_label = NEW_LABEL(line);
8617 LABEL *retry_end_l = NEW_LABEL(line);
8620 ADD_LABEL(ret, retry_label);
8621 if (nd_type_p(node, NODE_FOR)) {
8622 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8624 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8625 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8626 ISEQ_TYPE_BLOCK, line);
8627 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8630 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8631 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8632 ISEQ_TYPE_BLOCK, line);
8633 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8647 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
8648 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8649 iobj = (
INSN*) get_prev_insn(iobj);
8651 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
8655 if (&iobj->link == LAST_ELEMENT(ret)) {
8661 ADD_INSN(ret, line_node, pop);
8664 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8666 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8676 const NODE *line_node = node;
8677 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8678 LABEL *not_single = NEW_LABEL(nd_line(var));
8679 LABEL *not_ary = NEW_LABEL(nd_line(var));
8680 CHECK(COMPILE(ret,
"for var", var));
8681 ADD_INSN(ret, line_node, dup);
8682 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8683 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8684 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8685 ADD_INSNL(ret, line_node, branchunless, not_single);
8686 ADD_INSN(ret, line_node, dup);
8687 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8688 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8689 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8690 ADD_INSN(ret, line_node, swap);
8691 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
8692 ADD_INSN(ret, line_node, dup);
8693 ADD_INSNL(ret, line_node, branchunless, not_ary);
8694 ADD_INSN(ret, line_node, swap);
8695 ADD_LABEL(ret, not_ary);
8696 ADD_INSN(ret, line_node, pop);
8697 ADD_LABEL(ret, not_single);
8704 const NODE *line_node = node;
8705 unsigned long throw_flag = 0;
8707 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8709 LABEL *splabel = NEW_LABEL(0);
8710 ADD_LABEL(ret, splabel);
8711 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8712 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8713 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8714 add_ensure_iseq(ret, iseq, 0);
8715 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8716 ADD_ADJUST_RESTORE(ret, splabel);
8719 ADD_INSN(ret, line_node, putnil);
8726 if (!ISEQ_COMPILE_DATA(ip)) {
8731 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8732 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8734 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8737 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8738 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8742 ip = ISEQ_BODY(ip)->parent_iseq;
8747 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8748 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8750 ADD_INSN(ret, line_node, pop);
8754 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8763 const NODE *line_node = node;
8764 unsigned long throw_flag = 0;
8766 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8767 LABEL *splabel = NEW_LABEL(0);
8768 debugs(
"next in while loop\n");
8769 ADD_LABEL(ret, splabel);
8770 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8771 add_ensure_iseq(ret, iseq, 0);
8772 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8773 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8774 ADD_ADJUST_RESTORE(ret, splabel);
8776 ADD_INSN(ret, line_node, putnil);
8779 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8780 LABEL *splabel = NEW_LABEL(0);
8781 debugs(
"next in block\n");
8782 ADD_LABEL(ret, splabel);
8783 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8784 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8785 add_ensure_iseq(ret, iseq, 0);
8786 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8787 ADD_ADJUST_RESTORE(ret, splabel);
8790 ADD_INSN(ret, line_node, putnil);
8797 if (!ISEQ_COMPILE_DATA(ip)) {
8802 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8803 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8807 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8810 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8811 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8815 ip = ISEQ_BODY(ip)->parent_iseq;
8818 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8819 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8822 ADD_INSN(ret, line_node, pop);
8826 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
8836 const NODE *line_node = node;
8838 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8839 LABEL *splabel = NEW_LABEL(0);
8840 debugs(
"redo in while");
8841 ADD_LABEL(ret, splabel);
8842 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8843 add_ensure_iseq(ret, iseq, 0);
8844 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8845 ADD_ADJUST_RESTORE(ret, splabel);
8847 ADD_INSN(ret, line_node, putnil);
8850 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8851 LABEL *splabel = NEW_LABEL(0);
8853 debugs(
"redo in block");
8854 ADD_LABEL(ret, splabel);
8855 add_ensure_iseq(ret, iseq, 0);
8856 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8857 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8858 ADD_ADJUST_RESTORE(ret, splabel);
8861 ADD_INSN(ret, line_node, putnil);
8868 if (!ISEQ_COMPILE_DATA(ip)) {
8873 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8876 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8879 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8880 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
8884 ip = ISEQ_BODY(ip)->parent_iseq;
8887 ADD_INSN(ret, line_node, putnil);
8888 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8891 ADD_INSN(ret, line_node, pop);
8895 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
8905 const NODE *line_node = node;
8907 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
8908 ADD_INSN(ret, line_node, putnil);
8909 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
8912 ADD_INSN(ret, line_node, pop);
8916 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
8925 const int line = nd_line(node);
8926 const NODE *line_node = node;
8927 LABEL *lstart = NEW_LABEL(line);
8928 LABEL *lend = NEW_LABEL(line);
8929 LABEL *lcont = NEW_LABEL(line);
8930 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
8932 ISEQ_BODY(iseq)->location.label),
8933 ISEQ_TYPE_RESCUE, line);
8935 lstart->rescued = LABEL_RESCUE_BEG;
8936 lend->rescued = LABEL_RESCUE_END;
8937 ADD_LABEL(ret, lstart);
8939 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
8940 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
8942 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
8944 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
8946 ADD_LABEL(ret, lend);
8947 if (RNODE_RESCUE(node)->nd_else) {
8948 ADD_INSN(ret, line_node, pop);
8949 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
8951 ADD_INSN(ret, line_node, nop);
8952 ADD_LABEL(ret, lcont);
8955 ADD_INSN(ret, line_node, pop);
8959 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
8960 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
8967 const int line = nd_line(node);
8968 const NODE *line_node = node;
8969 const NODE *resq = node;
8971 LABEL *label_miss, *label_hit;
8974 label_miss = NEW_LABEL(line);
8975 label_hit = NEW_LABEL(line);
8977 narg = RNODE_RESBODY(resq)->nd_args;
8979 switch (nd_type(narg)) {
8982 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8983 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
8984 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8985 ADD_INSNL(ret, line_node, branchif, label_hit);
8986 narg = RNODE_LIST(narg)->nd_next;
8992 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8993 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
8994 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
8995 ADD_INSNL(ret, line_node, branchif, label_hit);
8998 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
9002 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9004 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
9005 ADD_INSNL(ret, line_node, branchif, label_hit);
9007 ADD_INSNL(ret, line_node, jump, label_miss);
9008 ADD_LABEL(ret, label_hit);
9011 if (RNODE_RESBODY(resq)->nd_exc_var) {
9012 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
9015 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) {
9017 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
9020 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
9023 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
9024 ADD_INSN(ret, line_node, nop);
9026 ADD_INSN(ret, line_node, leave);
9027 ADD_LABEL(ret, label_miss);
9028 resq = RNODE_RESBODY(resq)->nd_next;
9036 const int line = nd_line(RNODE_ENSURE(node)->nd_ensr);
9037 const NODE *line_node = node;
9039 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
9041 ISEQ_TYPE_ENSURE, line);
9042 LABEL *lstart = NEW_LABEL(line);
9043 LABEL *lend = NEW_LABEL(line);
9044 LABEL *lcont = NEW_LABEL(line);
9052 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
9054 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
9059 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
9061 ADD_LABEL(ret, lstart);
9062 CHECK(COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
9063 ADD_LABEL(ret, lend);
9065 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
9066 ADD_LABEL(ret, lcont);
9067 if (last_leave) ADD_INSN(ret, line_node, pop);
9069 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
9070 if (lstart->link.next != &lend->link) {
9072 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
9074 erange = erange->next;
9078 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
9085 const NODE *line_node = node;
9088 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
9090 enum rb_iseq_type t =
type;
9091 const NODE *retval = RNODE_RETURN(node)->nd_stts;
9094 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
9095 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
9096 t = ISEQ_BODY(is)->type;
9100 case ISEQ_TYPE_MAIN:
9102 rb_warn(
"argument of top-level return is ignored");
9106 type = ISEQ_TYPE_METHOD;
9113 if (
type == ISEQ_TYPE_METHOD) {
9114 splabel = NEW_LABEL(0);
9115 ADD_LABEL(ret, splabel);
9116 ADD_ADJUST(ret, line_node, 0);
9119 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
9121 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
9122 add_ensure_iseq(ret, iseq, 1);
9124 ADD_INSN(ret, line_node, leave);
9125 ADD_ADJUST_RESTORE(ret, splabel);
9128 ADD_INSN(ret, line_node, putnil);
9132 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
9134 ADD_INSN(ret, line_node, pop);
9145 if (!i)
return false;
9146 if (IS_TRACE(i)) i = i->prev;
9147 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
9149 if (IS_ADJUST(i)) i = i->prev;
9150 if (!IS_INSN(i))
return false;
9151 switch (INSN_OF(i)) {
9158 (ret->last = last->prev)->next = NULL;
9165 CHECK(COMPILE_(ret,
"nd_body", node, popped));
9167 if (!popped && !all_string_result_p(node)) {
9168 const NODE *line_node = node;
9169 const unsigned int flag = VM_CALL_FCALL;
9173 ADD_INSN(ret, line_node, dup);
9174 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
9175 ADD_INSN(ret, line_node, anytostring);
9183 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
9185 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
9186 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
9192 LABEL *else_label = NEW_LABEL(nd_line(line_node));
9195 br = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"&.");
9197 ADD_INSN(recv, line_node, dup);
9198 ADD_INSNL(recv, line_node, branchnil, else_label);
9199 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
9207 if (!else_label)
return;
9208 end_label = NEW_LABEL(nd_line(line_node));
9209 ADD_INSNL(ret, line_node, jump, end_label);
9210 ADD_LABEL(ret, else_label);
9211 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
9212 ADD_LABEL(ret, end_label);
9221 if (get_nd_recv(node) &&
9222 (nd_type_p(get_nd_recv(node), NODE_STR) || nd_type_p(get_nd_recv(node), NODE_FILE)) &&
9223 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
9224 get_nd_args(node) == NULL &&
9225 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9226 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
9227 VALUE str = get_string_value(get_nd_recv(node));
9228 if (get_node_call_nd_mid(node) == idUMinus) {
9229 ADD_INSN2(ret, line_node, opt_str_uminus, str,
9230 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
9233 ADD_INSN2(ret, line_node, opt_str_freeze, str,
9234 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
9238 ADD_INSN(ret, line_node, pop);
9246iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
9248 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
9252iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
9255 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
9256 for (i=0; table[i].index != -1; i++) {
9257 if (strcmp(table[i].name, name) == 0) {
9265iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
9267 const char *name = rb_id2name(mid);
9268 static const char prefix[] =
"__builtin_";
9269 const size_t prefix_len =
sizeof(prefix) - 1;
9274 switch (nd_type(recv)) {
9276 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
9281 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
9291 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
9292 return &name[prefix_len];
9301delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
9308 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
9309 unsigned int start=0;
9314 argc + start <= ISEQ_BODY(iseq)->local_table_size;
9318 for (
unsigned int i=start; i-start<argc; i++) {
9319 if (IS_INSN(elem) &&
9320 INSN_OF(elem) == BIN(getlocal)) {
9321 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
9322 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
9324 if (local_level == 0) {
9325 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
9327 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
9328 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
9329 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
9330 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9354 *pstart_index = start;
9366 if (rb_streql_lit(
string,
"leaf")) {
9367 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9369 else if (rb_streql_lit(
string,
"inline_block")) {
9370 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9372 else if (rb_streql_lit(
string,
"use_block")) {
9373 iseq_set_use_block(iseq);
9375 else if (rb_streql_lit(
string,
"c_trace")) {
9377 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9379 else if (rb_streql_lit(
string,
"without_interrupts")) {
9380 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_WITHOUT_INTERRUPTS;
9393 if (!node)
goto no_arg;
9395 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9396 const NODE *next = RNODE_LIST(node)->nd_next;
9398 node = RNODE_LIST(node)->nd_head;
9399 if (!node)
goto no_arg;
9400 switch (nd_type(node)) {
9402 symbol = rb_node_sym_string_val(node);
9408 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9409 if (compile_builtin_attr_symbol(iseq, symbol) != COMPILE_OK)
goto unknown_arg;
9414 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9417 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9420 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %" PRIsVALUE, symbol);
9423 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9431 if (!node)
goto no_arg;
9432 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9433 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9434 node = RNODE_LIST(node)->nd_head;
9435 if (!node)
goto no_arg;
9436 switch (nd_type(node)) {
9438 name = rb_node_sym_string_val(node);
9443 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9445 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9449 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9452 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9455 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9456 rb_builtin_class_name(name));
9459 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9465 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9466 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9467 return RNODE_IF(node)->nd_body;
9470 rb_bug(
"mandatory_node: can't find mandatory node");
9475compile_builtin_mandatory_only_method(
rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9479 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9482 rb_node_init(RNODE(&args_node), NODE_ARGS);
9483 args_node.nd_ainfo = args;
9486 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9487 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9491 tbl->size = table_size;
9496 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9497 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9500 for (; i<table_size; i++) {
9501 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9505 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9506 scope_node.nd_tbl = tbl;
9507 scope_node.nd_body = mandatory_node(iseq, node);
9508 scope_node.nd_parent = NULL;
9509 scope_node.nd_args = &args_node;
9511 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9514 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9515 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9516 nd_line(line_node), NULL, 0,
9517 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9518 ISEQ_SCRIPT_LINES(iseq));
9519 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
9529 NODE *args_node = get_nd_args(node);
9531 if (parent_block != NULL) {
9532 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9536# define BUILTIN_INLINE_PREFIX "_bi"
9537 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9538 bool cconst =
false;
9543 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9544 strcmp(
"cexpr!", builtin_func) == 0) {
9547 else if (strcmp(
"cconst!", builtin_func) == 0) {
9550 else if (strcmp(
"cinit!", builtin_func) == 0) {
9554 else if (strcmp(
"attr!", builtin_func) == 0) {
9555 return compile_builtin_attr(iseq, args_node);
9557 else if (strcmp(
"arg!", builtin_func) == 0) {
9558 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9560 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9562 rb_bug(
"mandatory_only? should be in if condition");
9564 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9565 rb_bug(
"mandatory_only? should be put on top");
9568 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9569 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9572 rb_bug(
"can't find builtin function:%s", builtin_func);
9575 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9579 int inline_index = nd_line(node);
9580 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9581 builtin_func = inline_func;
9587 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9588 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9589 ADD_INSN1(ret, line_node, putobject, const_val);
9595 unsigned int flag = 0;
9597 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9599 if (
FIX2INT(argc) != bf->argc) {
9600 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9601 builtin_func, bf->argc,
FIX2INT(argc));
9605 unsigned int start_index;
9606 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9607 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9611 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9614 if (popped) ADD_INSN(ret, line_node, pop);
9620compile_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)
9628 ID mid = get_node_call_nd_mid(node);
9630 unsigned int flag = 0;
9632 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9633 LABEL *else_label = NULL;
9636 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9642 if (nd_type_p(node, NODE_VCALL)) {
9647 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9649 if (mid == id_bitblt) {
9650 ADD_INSN(ret, line_node, bitblt);
9653 else if (mid == id_answer) {
9654 ADD_INSN(ret, line_node, answer);
9666 if (nd_type_p(node, NODE_FCALL) &&
9667 (mid == goto_id || mid == label_id)) {
9670 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9673 if (!labels_table) {
9674 labels_table = st_init_numtable();
9675 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9678 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9682 if (mid == goto_id) {
9683 ADD_INSNL(ret, line_node, jump, label);
9686 ADD_LABEL(ret, label);
9693 const char *builtin_func;
9694 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9695 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9696 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9700 if (!assume_receiver) {
9701 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9704 if ((mid == idCall || mid == idAREF || mid == idYield || mid == idEqq) &&
9705 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9706 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9707 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9709 else if (private_recv_p(node)) {
9710 ADD_INSN(recv, node, putself);
9711 flag |= VM_CALL_FCALL;
9714 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9717 if (
type == NODE_QCALL) {
9718 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9721 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9722 ADD_CALL_RECEIVER(recv, line_node);
9727 if (
type != NODE_VCALL) {
9728 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9729 CHECK(!
NIL_P(argc));
9737 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
9738 mid == rb_intern(
"new") &&
9739 parent_block == NULL &&
9740 !(flag & VM_CALL_ARGS_BLOCKARG);
9743 ADD_INSN(ret, node, putnil);
9744 ADD_INSN(ret, node, swap);
9749 debugp_param(
"call args argc", argc);
9750 debugp_param(
"call method",
ID2SYM(mid));
9752 switch ((
int)
type) {
9754 flag |= VM_CALL_VCALL;
9757 flag |= VM_CALL_FCALL;
9760 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9761 ADD_INSN(ret, line_node, splatkw);
9764 LABEL *not_basic_new = NEW_LABEL(nd_line(node));
9765 LABEL *not_basic_new_finish = NEW_LABEL(nd_line(node));
9770 if (flag & VM_CALL_FORWARDING) {
9771 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc) + 1, flag, keywords, 0);
9774 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc), flag, keywords, 0);
9776 ADD_INSN2(ret, node, opt_new, ci, not_basic_new);
9777 LABEL_REF(not_basic_new);
9780 ADD_SEND_R(ret, line_node, rb_intern(
"initialize"), argc, parent_block,
INT2FIX(flag | VM_CALL_FCALL), keywords);
9781 ADD_INSNL(ret, line_node, jump, not_basic_new_finish);
9783 ADD_LABEL(ret, not_basic_new);
9785 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9786 ADD_INSN(ret, line_node, swap);
9788 ADD_LABEL(ret, not_basic_new_finish);
9789 ADD_INSN(ret, line_node, pop);
9792 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9795 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9797 ADD_INSN(ret, line_node, pop);
9805 const int line = nd_line(node);
9807 unsigned int flag = 0;
9809 ID id = RNODE_OP_ASGN1(node)->nd_mid;
9835 ADD_INSN(ret, node, putnil);
9837 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
9838 CHECK(asgnflag != -1);
9839 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
9844 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
9845 CHECK(!
NIL_P(argc));
9847 int dup_argn =
FIX2INT(argc) + 1;
9848 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
9850 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
9852 if (
id == idOROP ||
id == idANDOP) {
9861 LABEL *label = NEW_LABEL(line);
9862 LABEL *lfin = NEW_LABEL(line);
9864 ADD_INSN(ret, node, dup);
9866 ADD_INSNL(ret, node, branchif, label);
9869 ADD_INSNL(ret, node, branchunless, label);
9871 ADD_INSN(ret, node, pop);
9873 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9875 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9877 if (flag & VM_CALL_ARGS_SPLAT) {
9878 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9879 ADD_INSN(ret, node, swap);
9880 ADD_INSN1(ret, node, splatarray,
Qtrue);
9881 ADD_INSN(ret, node, swap);
9882 flag |= VM_CALL_ARGS_SPLAT_MUT;
9884 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9885 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9888 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9890 ADD_INSN(ret, node, pop);
9891 ADD_INSNL(ret, node, jump, lfin);
9892 ADD_LABEL(ret, label);
9894 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9896 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
9897 ADD_LABEL(ret, lfin);
9900 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9901 ADD_SEND(ret, node,
id,
INT2FIX(1));
9903 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9905 if (flag & VM_CALL_ARGS_SPLAT) {
9906 if (flag & VM_CALL_KW_SPLAT) {
9907 ADD_INSN1(ret, node, topn,
INT2FIX(2));
9908 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9909 ADD_INSN1(ret, node, splatarray,
Qtrue);
9910 flag |= VM_CALL_ARGS_SPLAT_MUT;
9912 ADD_INSN(ret, node, swap);
9913 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9914 ADD_INSN1(ret, node, setn,
INT2FIX(2));
9915 ADD_INSN(ret, node, pop);
9918 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9919 ADD_INSN(ret, node, swap);
9920 ADD_INSN1(ret, node, splatarray,
Qtrue);
9921 ADD_INSN(ret, node, swap);
9922 flag |= VM_CALL_ARGS_SPLAT_MUT;
9924 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9926 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9929 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9931 ADD_INSN(ret, node, pop);
9939 const int line = nd_line(node);
9940 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
9941 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
9943 LABEL *lfin = NEW_LABEL(line);
9944 LABEL *lcfin = NEW_LABEL(line);
9999 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
10000 CHECK(asgnflag != -1);
10001 if (RNODE_OP_ASGN2(node)->nd_aid) {
10002 lskip = NEW_LABEL(line);
10003 ADD_INSN(ret, node, dup);
10004 ADD_INSNL(ret, node, branchnil, lskip);
10006 ADD_INSN(ret, node, dup);
10007 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
10009 if (atype == idOROP || atype == idANDOP) {
10011 ADD_INSN(ret, node, dup);
10013 if (atype == idOROP) {
10014 ADD_INSNL(ret, node, branchif, lcfin);
10017 ADD_INSNL(ret, node, branchunless, lcfin);
10020 ADD_INSN(ret, node, pop);
10022 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
10024 ADD_INSN(ret, node, swap);
10025 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10027 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
10028 ADD_INSNL(ret, node, jump, lfin);
10030 ADD_LABEL(ret, lcfin);
10032 ADD_INSN(ret, node, swap);
10035 ADD_LABEL(ret, lfin);
10038 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
10039 ADD_SEND(ret, node, atype,
INT2FIX(1));
10041 ADD_INSN(ret, node, swap);
10042 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10044 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
10046 if (lskip && popped) {
10047 ADD_LABEL(ret, lskip);
10049 ADD_INSN(ret, node, pop);
10050 if (lskip && !popped) {
10051 ADD_LABEL(ret, lskip);
10056static int compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
10061 const int line = nd_line(node);
10063 LABEL *lassign = 0;
10066 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
10068 ADD_INSN1(ret, node, putobject,
rb_cObject);
10071 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
10074 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
10075 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
10078 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
10080 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
10081 lassign = NEW_LABEL(line);
10082 ADD_INSN(ret, node, dup);
10083 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
10085 ADD_INSNL(ret, node, branchunless, lassign);
10087 ADD_INSN(ret, node, dup);
10088 ADD_INSN1(ret, node, putobject,
Qtrue);
10089 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
10091 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
10092 lfin = NEW_LABEL(line);
10093 if (!popped) ADD_INSN(ret, node, dup);
10094 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
10095 ADD_INSNL(ret, node, branchif, lfin);
10097 ADD_INSNL(ret, node, branchunless, lfin);
10099 if (!popped) ADD_INSN(ret, node, pop);
10100 if (lassign) ADD_LABEL(ret, lassign);
10101 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
10104 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10106 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
10107 ADD_INSN(ret, node, swap);
10109 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
10110 ADD_LABEL(ret, lfin);
10111 if (!popped) ADD_INSN(ret, node, swap);
10112 ADD_INSN(ret, node, pop);
10115 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
10117 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
10119 ADD_INSN(ret, node, swap);
10121 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10122 ADD_INSN(ret, node, swap);
10124 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
10132 const int line = nd_line(node);
10133 LABEL *lfin = NEW_LABEL(line);
10136 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
10140 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
10141 lassign = lfinish[1];
10143 lassign = NEW_LABEL(line);
10145 ADD_INSNL(ret, node, branchunless, lassign);
10148 lassign = NEW_LABEL(line);
10151 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
10154 ADD_INSN(ret, node, dup);
10157 if (
type == NODE_OP_ASGN_AND) {
10158 ADD_INSNL(ret, node, branchunless, lfin);
10161 ADD_INSNL(ret, node, branchif, lfin);
10165 ADD_INSN(ret, node, pop);
10168 ADD_LABEL(ret, lassign);
10169 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
10170 ADD_LABEL(ret, lfin);
10180 unsigned int flag = 0;
10182 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
10186 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
10188 if (
type == NODE_SUPER) {
10189 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
10190 CHECK(!
NIL_P(vargc));
10192 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
10193 ADD_INSN(args, node, splatkw);
10196 if (flag & VM_CALL_ARGS_BLOCKARG) {
10203 const rb_iseq_t *liseq = body->local_iseq;
10205 const struct rb_iseq_param_keyword *
const local_kwd = local_body->param.keyword;
10206 int lvar_level = get_lvar_level(iseq);
10208 argc = local_body->param.lead_num;
10211 for (i = 0; i < local_body->param.lead_num; i++) {
10212 int idx = local_body->local_table_size - i;
10213 ADD_GETLOCAL(args, node, idx, lvar_level);
10217 if (local_body->param.flags.forwardable) {
10218 flag |= VM_CALL_FORWARDING;
10219 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
10220 ADD_GETLOCAL(args, node, idx, lvar_level);
10223 if (local_body->param.flags.has_opt) {
10226 for (j = 0; j < local_body->param.opt_num; j++) {
10227 int idx = local_body->local_table_size - (i + j);
10228 ADD_GETLOCAL(args, node, idx, lvar_level);
10233 if (local_body->param.flags.has_rest) {
10235 int idx = local_body->local_table_size - local_body->param.rest_start;
10236 ADD_GETLOCAL(args, node, idx, lvar_level);
10237 ADD_INSN1(args, node, splatarray, RBOOL(local_body->param.flags.has_post));
10239 argc = local_body->param.rest_start + 1;
10240 flag |= VM_CALL_ARGS_SPLAT;
10242 if (local_body->param.flags.has_post) {
10244 int post_len = local_body->param.post_num;
10245 int post_start = local_body->param.post_start;
10247 if (local_body->param.flags.has_rest) {
10249 for (j=0; j<post_len; j++) {
10250 int idx = local_body->local_table_size - (post_start + j);
10251 ADD_GETLOCAL(args, node, idx, lvar_level);
10253 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
10254 flag |= VM_CALL_ARGS_SPLAT_MUT;
10259 for (j=0; j<post_len; j++) {
10260 int idx = local_body->local_table_size - (post_start + j);
10261 ADD_GETLOCAL(args, node, idx, lvar_level);
10263 argc = post_len + post_start;
10267 if (local_body->param.flags.has_kw) {
10268 int local_size = local_body->local_table_size;
10271 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10273 if (local_body->param.flags.has_kwrest) {
10274 int idx = local_body->local_table_size - local_kwd->rest_start;
10275 ADD_GETLOCAL(args, node, idx, lvar_level);
10277 ADD_SEND (args, node, rb_intern(
"dup"),
INT2FIX(0));
10280 ADD_INSN1(args, node, newhash,
INT2FIX(0));
10282 for (i = 0; i < local_kwd->num; ++i) {
10283 ID id = local_kwd->table[i];
10284 int idx = local_size - get_local_var_idx(liseq,
id);
10285 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
10286 ADD_GETLOCAL(args, node, idx, lvar_level);
10288 ADD_SEND(args, node, id_core_hash_merge_bang_ptr,
INT2FIX(i * 2 + 1));
10289 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
10291 else if (local_body->param.flags.has_kwrest) {
10292 int idx = local_body->local_table_size - local_kwd->rest_start;
10293 ADD_GETLOCAL(args, node, idx, lvar_level);
10295 flag |= VM_CALL_KW_SPLAT;
10299 if (use_block && parent_block == NULL) {
10300 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10303 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
10304 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
10305 ADD_INSN(ret, node, putself);
10306 ADD_SEQ(ret, args);
10308 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
10310 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
10311 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
10314 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
10318 ADD_INSN(ret, node, pop);
10328 unsigned int flag = 0;
10333 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
10334 case ISEQ_TYPE_TOP:
10335 case ISEQ_TYPE_MAIN:
10336 case ISEQ_TYPE_CLASS:
10337 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
10342 if (RNODE_YIELD(node)->nd_head) {
10343 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
10344 CHECK(!
NIL_P(argc));
10350 ADD_SEQ(ret, args);
10351 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
10352 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10355 ADD_INSN(ret, node, pop);
10360 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
10361 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
10363 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
10376 switch ((
int)
type) {
10379 VALUE re = rb_node_regx_string_val(node);
10380 RB_OBJ_SET_FROZEN_SHAREABLE(re);
10381 ADD_INSN1(recv, node, putobject, re);
10382 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
10387 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
10388 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
10391 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
10392 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
10396 ADD_SEQ(ret, recv);
10398 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
10400 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
10401 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
10405 ADD_INSN(ret, node, pop);
10416 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10417 (segments = collect_const_segments(iseq, node))) {
10418 ISEQ_BODY(iseq)->ic_size++;
10419 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10429 CHECK(compile_const_prefix(iseq, node, pref, body));
10430 if (LIST_INSN_SIZE_ZERO(pref)) {
10431 ADD_INSN(ret, node, putnil);
10432 ADD_SEQ(ret, body);
10435 ADD_SEQ(ret, pref);
10436 ADD_SEQ(ret, body);
10442 ADD_CALL_RECEIVER(ret, node);
10443 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10444 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10447 ADD_INSN(ret, node, pop);
10455 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10458 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10459 ISEQ_BODY(iseq)->ic_size++;
10460 VALUE segments = rb_ary_new_from_args(2,
ID2SYM(idNULL),
ID2SYM(RNODE_COLON3(node)->nd_mid));
10461 RB_OBJ_SET_FROZEN_SHAREABLE(segments);
10462 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10466 ADD_INSN1(ret, node, putobject,
rb_cObject);
10467 ADD_INSN1(ret, node, putobject,
Qtrue);
10468 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10472 ADD_INSN(ret, node, pop);
10481 const NODE *b = RNODE_DOT2(node)->nd_beg;
10482 const NODE *e = RNODE_DOT2(node)->nd_end;
10484 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10486 VALUE bv = optimized_range_item(b);
10487 VALUE ev = optimized_range_item(e);
10490 ADD_INSN1(ret, node, putobject, val);
10495 CHECK(COMPILE_(ret,
"min", b, popped));
10496 CHECK(COMPILE_(ret,
"max", e, popped));
10498 ADD_INSN1(ret, node, newrange, flag);
10508 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10509 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10515 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10518 ip = ISEQ_BODY(ip)->parent_iseq;
10522 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10525 ADD_INSN(ret, node, putnil);
10536 LABEL *end_label = NEW_LABEL(nd_line(node));
10537 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10539 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10541 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10544 else if (nd_type_p(default_value, NODE_SYM) ||
10545 nd_type_p(default_value, NODE_REGX) ||
10546 nd_type_p(default_value, NODE_LINE) ||
10547 nd_type_p(default_value, NODE_INTEGER) ||
10548 nd_type_p(default_value, NODE_FLOAT) ||
10549 nd_type_p(default_value, NODE_RATIONAL) ||
10550 nd_type_p(default_value, NODE_IMAGINARY) ||
10551 nd_type_p(default_value, NODE_NIL) ||
10552 nd_type_p(default_value, NODE_TRUE) ||
10553 nd_type_p(default_value, NODE_FALSE)) {
10554 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10562 int kw_bits_idx = body->local_table_size - body->param.keyword->bits_start;
10563 int keyword_idx = body->param.keyword->num;
10565 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10566 ADD_INSNL(ret, node, branchif, end_label);
10567 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10568 ADD_LABEL(ret, end_label);
10578 unsigned int flag = 0;
10579 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10581 LABEL *else_label = NULL;
10586 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10587 CHECK(!
NIL_P(argc));
10589 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10590 CHECK(asgnflag != -1);
10591 flag |= (
unsigned int)asgnflag;
10593 debugp_param(
"argc", argc);
10594 debugp_param(
"nd_mid",
ID2SYM(mid));
10598 mid = rb_id_attrset(mid);
10599 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10602 ADD_INSN(ret, node, putnil);
10603 ADD_SEQ(ret, recv);
10604 ADD_SEQ(ret, args);
10606 if (flag & VM_CALL_ARGS_SPLAT) {
10607 ADD_INSN(ret, node, dup);
10608 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10609 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10610 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10611 ADD_INSN (ret, node, pop);
10614 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10618 ADD_SEQ(ret, recv);
10619 ADD_SEQ(ret, args);
10621 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10622 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10623 ADD_INSN(ret, node, pop);
10630 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10638 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10645 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10652node_const_decl_val(
const NODE *node)
10655 switch (nd_type(node)) {
10657 if (RNODE_CDECL(node)->nd_vid) {
10658 path = rb_id2str(RNODE_CDECL(node)->nd_vid);
10662 node = RNODE_CDECL(node)->nd_else;
10670 rb_str_append(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10673 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10679 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10680 rb_ary_push(path, rb_id2str(RNODE_COLON2(node)->nd_mid));
10682 if (node && nd_type_p(node, NODE_CONST)) {
10684 rb_ary_push(path, rb_id2str(RNODE_CONST(node)->nd_vid));
10686 else if (node && nd_type_p(node, NODE_COLON3)) {
10688 rb_ary_push(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10698 path = rb_fstring(path);
10703const_decl_path(
NODE *dest)
10706 if (!nd_type_p(dest, NODE_CALL)) {
10707 path = node_const_decl_val(dest);
10718 VALUE path = const_decl_path(dest);
10719 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10720 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10721 ADD_INSN1(ret, value, putobject, path);
10723 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10728#ifndef SHAREABLE_BARE_EXPRESSION
10729#define SHAREABLE_BARE_EXPRESSION 1
10733compile_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)
10735# define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10736 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10738 DECL_ANCHOR(anchor);
10740 enum node_type
type = node ? nd_type(node) : NODE_NIL;
10752 *value_p = rb_node_sym_string_val(node);
10755 *value_p = rb_node_regx_string_val(node);
10758 *value_p = rb_node_line_lineno_val(node);
10761 *value_p = rb_node_integer_literal_val(node);
10764 *value_p = rb_node_float_literal_val(node);
10766 case NODE_RATIONAL:
10767 *value_p = rb_node_rational_literal_val(node);
10769 case NODE_IMAGINARY:
10770 *value_p = rb_node_imaginary_literal_val(node);
10772 case NODE_ENCODING:
10773 *value_p = rb_node_encoding_val(node);
10776 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10777 *shareable_literal_p = 1;
10781 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10782 if (shareable == rb_parser_shareable_literal) {
10788 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10791 *shareable_literal_p = 1;
10795 VALUE lit = rb_node_str_string_val(node);
10796 ADD_INSN1(ret, node, putobject, lit);
10799 *shareable_literal_p = 1;
10805 VALUE lit = rb_node_file_path_val(node);
10806 ADD_INSN1(ret, node, putobject, lit);
10809 *shareable_literal_p = 1;
10817 ADD_INSN1(ret, node, putobject, lit);
10820 *shareable_literal_p = 1;
10826 INIT_ANCHOR(anchor);
10828 for (
NODE *n = (
NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
10830 int shareable_literal_p2;
10831 NODE *elt = RNODE_LIST(n)->nd_head;
10833 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
10834 if (shareable_literal_p2) {
10837 else if (
RTEST(lit)) {
10843 if (!UNDEF_P(val)) {
10855 if (!RNODE_HASH(node)->nd_brace) {
10857 *shareable_literal_p = 0;
10860 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10861 if (!RNODE_LIST(n)->nd_head) {
10863 goto compile_shareable;
10867 INIT_ANCHOR(anchor);
10868 lit = rb_hash_new();
10869 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10871 VALUE value_val = 0;
10872 int shareable_literal_p2;
10873 NODE *key = RNODE_LIST(n)->nd_head;
10874 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
10875 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10876 if (shareable_literal_p2) {
10879 else if (
RTEST(lit)) {
10880 rb_hash_clear(lit);
10883 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10884 if (shareable_literal_p2) {
10887 else if (
RTEST(lit)) {
10888 rb_hash_clear(lit);
10892 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
10893 rb_hash_aset(lit, key_val, value_val);
10896 rb_hash_clear(lit);
10907 if (shareable == rb_parser_shareable_literal &&
10908 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
10909 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
10911 *shareable_literal_p = 1;
10914 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10916 *shareable_literal_p = 0;
10922 if (nd_type(node) == NODE_LIST) {
10923 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10925 else if (nd_type(node) == NODE_HASH) {
10926 long len = RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10932 *shareable_literal_p = 0;
10933 ADD_SEQ(ret, anchor);
10939 if (nd_type(node) == NODE_LIST) {
10940 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10942 else if (nd_type(node) == NODE_HASH) {
10943 long len = RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10948 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
10950 *shareable_literal_p = 1;
10954 ADD_INSN1(ret, node, putobject, val);
10957 *shareable_literal_p = 1;
10964compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
10968 DECL_ANCHOR(anchor);
10969 INIT_ANCHOR(anchor);
10971 switch (shareable) {
10972 case rb_parser_shareable_none:
10973 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
10976 case rb_parser_shareable_literal:
10977 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10978 ADD_SEQ(ret, anchor);
10981 case rb_parser_shareable_copy:
10982 case rb_parser_shareable_everything:
10983 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
10985 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
10988 ADD_SEQ(ret, anchor);
10992 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
11009 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
11010 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
11011 debugs(
"node: NODE_NIL(implicit)\n");
11012 ADD_SYNTHETIC_INSN(ret, lineno, -1, putnil);
11016 return iseq_compile_each0(iseq, ret, node, popped);
11022 const int line = (int)nd_line(node);
11023 const enum node_type
type = nd_type(node);
11026 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
11030 if (nd_fl_newline(node)) {
11032 ISEQ_COMPILE_DATA(iseq)->last_line = line;
11033 if (line > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
11034 event |= RUBY_EVENT_COVERAGE_LINE;
11036 ADD_TRACE(ret, event);
11040 debug_node_start(node);
11041#undef BEFORE_RETURN
11042#define BEFORE_RETURN debug_node_end()
11046 CHECK(compile_block(iseq, ret, node, popped));
11050 CHECK(compile_if(iseq, ret, node, popped,
type));
11053 CHECK(compile_case(iseq, ret, node, popped));
11056 CHECK(compile_case2(iseq, ret, node, popped));
11059 CHECK(compile_case3(iseq, ret, node, popped));
11063 CHECK(compile_loop(iseq, ret, node, popped,
type));
11067 CHECK(compile_iter(iseq, ret, node, popped));
11069 case NODE_FOR_MASGN:
11070 CHECK(compile_for_masgn(iseq, ret, node, popped));
11073 CHECK(compile_break(iseq, ret, node, popped));
11076 CHECK(compile_next(iseq, ret, node, popped));
11079 CHECK(compile_redo(iseq, ret, node, popped));
11082 CHECK(compile_retry(iseq, ret, node, popped));
11085 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
11089 CHECK(compile_rescue(iseq, ret, node, popped));
11092 CHECK(compile_resbody(iseq, ret, node, popped));
11095 CHECK(compile_ensure(iseq, ret, node, popped));
11100 LABEL *end_label = NEW_LABEL(line);
11101 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(node)->nd_1st));
11103 ADD_INSN(ret, node, dup);
11105 if (
type == NODE_AND) {
11106 ADD_INSNL(ret, node, branchunless, end_label);
11109 ADD_INSNL(ret, node, branchif, end_label);
11112 ADD_INSN(ret, node, pop);
11114 CHECK(COMPILE_(ret,
"nd_2nd", RNODE_OR(node)->nd_2nd, popped));
11115 ADD_LABEL(ret, end_label);
11120 compile_massign(iseq, ret, node, popped);
11125 ID id = RNODE_LASGN(node)->nd_vid;
11126 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
11128 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
11129 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
11132 ADD_INSN(ret, node, dup);
11134 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
11139 ID id = RNODE_DASGN(node)->nd_vid;
11140 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
11141 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
11144 ADD_INSN(ret, node, dup);
11147 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
11150 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
11154 ADD_SETLOCAL(ret, node, ls - idx, lv);
11158 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
11161 ADD_INSN(ret, node, dup);
11163 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
11167 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
11169 ADD_INSN(ret, node, dup);
11171 ADD_INSN2(ret, node, setinstancevariable,
11172 ID2SYM(RNODE_IASGN(node)->nd_vid),
11173 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
11177 if (RNODE_CDECL(node)->nd_vid) {
11178 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11181 ADD_INSN(ret, node, dup);
11184 ADD_INSN1(ret, node, putspecialobject,
11185 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
11186 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
11189 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
11190 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11191 ADD_INSN(ret, node, swap);
11194 ADD_INSN1(ret, node, topn,
INT2FIX(1));
11195 ADD_INSN(ret, node, swap);
11198 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
11203 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
11205 ADD_INSN(ret, node, dup);
11207 ADD_INSN2(ret, node, setclassvariable,
11208 ID2SYM(RNODE_CVASGN(node)->nd_vid),
11209 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
11212 case NODE_OP_ASGN1:
11213 CHECK(compile_op_asgn1(iseq, ret, node, popped));
11215 case NODE_OP_ASGN2:
11216 CHECK(compile_op_asgn2(iseq, ret, node, popped));
11218 case NODE_OP_CDECL:
11219 CHECK(compile_op_cdecl(iseq, ret, node, popped));
11221 case NODE_OP_ASGN_AND:
11222 case NODE_OP_ASGN_OR:
11223 CHECK(compile_op_log(iseq, ret, node, popped,
type));
11227 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
11233 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
11239 CHECK(compile_super(iseq, ret, node, popped,
type));
11242 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
11247 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
11252 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
11255 CHECK(compile_return(iseq, ret, node, popped));
11258 CHECK(compile_yield(iseq, ret, node, popped));
11262 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
11268 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
11270 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
11272 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
11273 rb_id2str(RNODE_DVAR(node)->nd_vid));
11276 ADD_GETLOCAL(ret, node, ls - idx, lv);
11281 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
11283 ADD_INSN(ret, node, pop);
11288 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
11290 ADD_INSN2(ret, node, getinstancevariable,
11291 ID2SYM(RNODE_IVAR(node)->nd_vid),
11292 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
11297 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
11299 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
11301 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(RNODE_CONST(node)->nd_vid));
11302 RB_OBJ_SET_FROZEN_SHAREABLE(segments);
11303 ADD_INSN1(ret, node, opt_getconstant_path, segments);
11307 ADD_INSN(ret, node, putnil);
11308 ADD_INSN1(ret, node, putobject,
Qtrue);
11309 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
11313 ADD_INSN(ret, node, pop);
11319 ADD_INSN2(ret, node, getclassvariable,
11320 ID2SYM(RNODE_CVAR(node)->nd_vid),
11321 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
11325 case NODE_NTH_REF:{
11327 if (!RNODE_NTH_REF(node)->nd_nth) {
11328 ADD_INSN(ret, node, putnil);
11331 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11332 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
11336 case NODE_BACK_REF:{
11338 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11339 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
11346 CHECK(compile_match(iseq, ret, node, popped,
type));
11350 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
11356 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
11360 case NODE_ENCODING:{
11362 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
11366 case NODE_INTEGER:{
11367 VALUE lit = rb_node_integer_literal_val(node);
11369 debugp_param(
"integer", lit);
11371 ADD_INSN1(ret, node, putobject, lit);
11377 VALUE lit = rb_node_float_literal_val(node);
11379 debugp_param(
"float", lit);
11381 ADD_INSN1(ret, node, putobject, lit);
11386 case NODE_RATIONAL:{
11387 VALUE lit = rb_node_rational_literal_val(node);
11389 debugp_param(
"rational", lit);
11391 ADD_INSN1(ret, node, putobject, lit);
11396 case NODE_IMAGINARY:{
11397 VALUE lit = rb_node_imaginary_literal_val(node);
11399 debugp_param(
"imaginary", lit);
11401 ADD_INSN1(ret, node, putobject, lit);
11408 debugp_param(
"nd_lit", get_string_value(node));
11410 VALUE lit = get_string_value(node);
11413 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11414 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11415 RB_OBJ_SET_SHAREABLE(lit);
11417 switch (option->frozen_string_literal) {
11418 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11419 ADD_INSN1(ret, node, dupchilledstring, lit);
11421 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11422 ADD_INSN1(ret, node, dupstring, lit);
11424 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11425 ADD_INSN1(ret, node, putobject, lit);
11428 rb_bug(
"invalid frozen_string_literal");
11435 compile_dstr(iseq, ret, node);
11438 ADD_INSN(ret, node, pop);
11443 ADD_CALL_RECEIVER(ret, node);
11444 VALUE str = rb_node_str_string_val(node);
11445 ADD_INSN1(ret, node, putobject, str);
11447 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11450 ADD_INSN(ret, node, pop);
11455 ADD_CALL_RECEIVER(ret, node);
11456 compile_dstr(iseq, ret, node);
11457 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11460 ADD_INSN(ret, node, pop);
11465 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11469 VALUE lit = rb_node_regx_string_val(node);
11470 RB_OBJ_SET_SHAREABLE(lit);
11471 ADD_INSN1(ret, node, putobject, lit);
11477 compile_dregx(iseq, ret, node, popped);
11480 int ic_index = body->ise_size++;
11482 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11484 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
11488 ADD_INSN(ret, node, pop);
11492 case NODE_ARGSCAT:{
11494 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11495 ADD_INSN1(ret, node, splatarray,
Qfalse);
11496 ADD_INSN(ret, node, pop);
11497 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11498 ADD_INSN1(ret, node, splatarray,
Qfalse);
11499 ADD_INSN(ret, node, pop);
11502 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11503 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11504 if (nd_type_p(body_node, NODE_LIST)) {
11505 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11508 CHECK(COMPILE(ret,
"argscat body", body_node));
11509 ADD_INSN(ret, node, concattoarray);
11514 case NODE_ARGSPUSH:{
11516 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11517 ADD_INSN1(ret, node, splatarray,
Qfalse);
11518 ADD_INSN(ret, node, pop);
11519 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11522 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11523 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11524 if (keyword_node_p(body_node)) {
11525 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11526 ADD_INSN(ret, node, pushtoarraykwsplat);
11528 else if (static_literal_node_p(body_node, iseq,
false)) {
11529 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11530 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11533 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11534 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11540 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11541 ADD_INSN1(ret, node, splatarray,
Qtrue);
11544 ADD_INSN(ret, node, pop);
11549 ID mid = RNODE_DEFN(node)->nd_mid;
11550 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11552 ISEQ_TYPE_METHOD, line);
11554 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11555 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11559 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11565 ID mid = RNODE_DEFS(node)->nd_mid;
11566 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11568 ISEQ_TYPE_METHOD, line);
11570 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11571 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11572 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11576 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11581 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11582 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11583 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11584 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11585 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11588 ADD_INSN(ret, node, pop);
11593 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11594 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11595 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11596 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11599 ADD_INSN(ret, node, pop);
11606 for (
long i = 0; i < ary->len; i++) {
11607 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11608 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11609 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11610 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11612 if (i < ary->
len - 1) {
11613 ADD_INSN(ret, node, pop);
11618 ADD_INSN(ret, node, pop);
11623 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11624 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
11625 ISEQ_TYPE_CLASS, line);
11626 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11627 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11628 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11630 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11631 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11635 ADD_INSN(ret, node, pop);
11640 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11641 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
11642 ISEQ_TYPE_CLASS, line);
11643 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11644 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11646 ADD_INSN (ret, node, putnil);
11647 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11651 ADD_INSN(ret, node, pop);
11657 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11658 ISEQ_TYPE_CLASS, line);
11660 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11661 ADD_INSN (ret, node, putnil);
11662 CONST_ID(singletonclass,
"singletonclass");
11666 int sclass_flags = VM_DEFINECLASS_TYPE_SINGLETON_CLASS;
11667 const NODE *recv = RNODE_SCLASS(node)->nd_recv;
11668 if (!(nd_type_p(recv, NODE_SELF) &&
11669 ISEQ_BODY(iseq)->
type == ISEQ_TYPE_CLASS) &&
11670 !cpath_const_p(recv)) {
11671 sclass_flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
11674 ADD_INSN3(ret, node, defineclass,
11675 ID2SYM(singletonclass), singleton_class,
11680 ADD_INSN(ret, node, pop);
11685 CHECK(compile_colon2(iseq, ret, node, popped));
11688 CHECK(compile_colon3(iseq, ret, node, popped));
11691 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11694 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11698 LABEL *lend = NEW_LABEL(line);
11699 LABEL *ltrue = NEW_LABEL(line);
11700 LABEL *lfalse = NEW_LABEL(line);
11701 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11703 ADD_LABEL(ret, ltrue);
11704 ADD_INSN1(ret, node, putobject,
Qtrue);
11705 ADD_INSNL(ret, node, jump, lend);
11706 ADD_LABEL(ret, lfalse);
11707 ADD_INSN1(ret, node, putobject,
Qfalse);
11708 ADD_LABEL(ret, lend);
11713 ADD_INSN(ret, node, putself);
11719 ADD_INSN(ret, node, putnil);
11725 ADD_INSN1(ret, node, putobject,
Qtrue);
11731 ADD_INSN1(ret, node, putobject,
Qfalse);
11736 CHECK(compile_errinfo(iseq, ret, node, popped));
11740 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11743 case NODE_POSTEXE:{
11747 int is_index = body->ise_size++;
11749 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11751 NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc, rb_fstring(make_name_for_block(iseq)), ISEQ_TYPE_BLOCK, line);
11753 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
11757 ADD_INSN(ret, node, pop);
11762 CHECK(compile_kw_arg(iseq, ret, node, popped));
11765 compile_dstr(iseq, ret, node);
11767 ADD_INSN(ret, node, intern);
11770 ADD_INSN(ret, node, pop);
11774 case NODE_ATTRASGN:
11775 CHECK(compile_attrasgn(iseq, ret, node, popped));
11779 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11782 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11783 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11787 ADD_INSN(ret, node, pop);
11792 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
11807insn_data_length(
INSN *iobj)
11809 return insn_len(iobj->insn_id);
11813calc_sp_depth(
int depth,
INSN *insn)
11815 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
11819opobj_inspect(
VALUE obj)
11839insn_data_to_s_detail(
INSN *iobj)
11841 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
11843 if (iobj->operands) {
11844 const char *types = insn_op_types(iobj->insn_id);
11847 for (j = 0; types[j]; j++) {
11848 char type = types[j];
11854 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
11872 VALUE v = OPERAND_AT(iobj, j);
11887 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11890 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11893 rb_str_catf(str,
"<ise:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11899 if (vm_ci_mid(ci))
rb_str_append(str, rb_id2str(vm_ci_mid(ci)));
11900 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
11908 void *func = (
void *)OPERAND_AT(iobj, j);
11911 if (dladdr(func, &info) && info.dli_sname) {
11916 rb_str_catf(str,
"<%p>", func);
11926 if (types[j + 1]) {
11937 dump_disasm_list_with_cursor(link, NULL, NULL);
11948 printf(
"-- raw disasm--------\n");
11951 if (curr) printf(curr == link ?
"*" :
" ");
11952 switch (link->type) {
11953 case ISEQ_ELEMENT_INSN:
11955 iobj = (
INSN *)link;
11956 str = insn_data_to_s_detail(iobj);
11957 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
11958 pos += insn_data_length(iobj);
11961 case ISEQ_ELEMENT_LABEL:
11963 lobj = (
LABEL *)link;
11964 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
11965 dest == lobj ?
" <---" :
"");
11968 case ISEQ_ELEMENT_TRACE:
11971 printf(
" trace: %0x\n", trace->event);
11974 case ISEQ_ELEMENT_ADJUST:
11977 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
11982 rb_raise(
rb_eSyntaxError,
"dump_disasm_list error: %d\n", (
int)link->type);
11986 printf(
"---------------------\n");
11991rb_insn_len(
VALUE insn)
11993 return insn_len(insn);
11997rb_insns_name(
int i)
11999 return insn_name(i);
12003rb_insns_name_array(
void)
12007 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
12018 obj = rb_to_symbol_type(obj);
12020 if (st_lookup(labels_table, obj, &tmp) == 0) {
12021 label = NEW_LABEL(0);
12022 st_insert(labels_table, obj, (st_data_t)label);
12025 label = (
LABEL *)tmp;
12032get_exception_sym2type(
VALUE sym)
12034 static VALUE symRescue, symEnsure, symRetry;
12035 static VALUE symBreak, symRedo, symNext;
12037 if (symRescue == 0) {
12046 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
12047 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
12048 if (sym == symRetry)
return CATCH_TYPE_RETRY;
12049 if (sym == symBreak)
return CATCH_TYPE_BREAK;
12050 if (sym == symRedo)
return CATCH_TYPE_REDO;
12051 if (sym == symNext)
return CATCH_TYPE_NEXT;
12052 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
12065 LABEL *lstart, *lend, *lcont;
12080 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
12081 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
12082 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
12086 if (
type == CATCH_TYPE_RESCUE ||
12087 type == CATCH_TYPE_BREAK ||
12088 type == CATCH_TYPE_NEXT) {
12094 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
12102insn_make_insn_table(
void)
12106 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
12108 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
12122 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
12124 else if (
CLASS_OF(op) == rb_cISeq) {
12131 loaded_iseq = rb_iseqw_to_iseq(iseqw);
12132 return loaded_iseq;
12140 unsigned int flag = 0;
12151 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
12153 if (!
NIL_P(vkw_arg)) {
12156 size_t n = rb_callinfo_kwarg_bytes(
len);
12159 kw_arg->references = 0;
12160 kw_arg->keyword_len =
len;
12161 for (i = 0; i <
len; i++) {
12164 kw_arg->keywords[i] = kw;
12169 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
12175event_name_to_flag(
VALUE sym)
12177#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
12198 int line_no = 0, node_id = -1, insn_idx = 0;
12199 int ret = COMPILE_OK;
12204 static struct st_table *insn_table;
12206 if (insn_table == 0) {
12207 insn_table = insn_make_insn_table();
12210 for (i=0; i<
len; i++) {
12216 ADD_TRACE(anchor, event);
12219 LABEL *label = register_label(iseq, labels_table, obj);
12220 ADD_LABEL(anchor, label);
12237 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
12239 COMPILE_ERROR(iseq, line_no,
12240 "unknown instruction: %+"PRIsVALUE, insn);
12245 if (argc != insn_len((
VALUE)insn_id)-1) {
12246 COMPILE_ERROR(iseq, line_no,
12247 "operand size mismatch");
12253 argv = compile_data_calloc2_type(iseq,
VALUE, argc);
12258 (
enum ruby_vminsn_type)insn_id, argc, argv));
12260 for (j=0; j<argc; j++) {
12262 switch (insn_op_type((
VALUE)insn_id, j)) {
12264 LABEL *label = register_label(iseq, labels_table, op);
12265 argv[j] = (
VALUE)label;
12280 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
12291 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
12292 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
12298 op = rb_to_array_type(op);
12302 sym = rb_to_symbol_type(sym);
12307 argv[j] = segments;
12309 ISEQ_BODY(iseq)->ic_size++;
12314 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
12315 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
12320 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
12321 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
12325 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
12328 argv[j] = rb_to_symbol_type(op);
12335 op = rb_to_array_type(op);
12340 register_label(iseq, labels_table, sym);
12341 cdhash_aset(map, key, (
VALUE)label);
12344 RB_OBJ_SET_SHAREABLE(map);
12351#if SIZEOF_VALUE <= SIZEOF_LONG
12356 argv[j] = (
VALUE)funcptr;
12367 (
enum ruby_vminsn_type)insn_id, argc, NULL));
12371 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
12376 validate_labels(iseq, labels_table);
12377 if (!ret)
return ret;
12378 return iseq_setup(iseq, anchor);
12381#define CHECK_ARRAY(v) rb_to_array_type(v)
12382#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
12387 VALUE val = rb_hash_aref(param, sym);
12392 else if (!
NIL_P(val)) {
12393 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
12399static const struct rb_iseq_param_keyword *
12405 VALUE key, sym, default_val;
12408 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12410 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12412 keyword->num =
len;
12413#define SYM(s) ID2SYM(rb_intern_const(#s))
12414 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12415 i = keyword->bits_start - keyword->num;
12416 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12420 for (i = 0; i <
len; i++) {
12424 goto default_values;
12427 keyword->required_num++;
12431 default_len =
len - i;
12432 if (default_len == 0) {
12433 keyword->table = ids;
12436 else if (default_len < 0) {
12442 for (j = 0; i <
len; i++, j++) {
12456 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
12462 keyword->table = ids;
12463 keyword->default_values = dvs;
12469iseq_insn_each_object_mark_and_move(
VALUE * obj,
VALUE _)
12471 rb_gc_mark_and_move(obj);
12478 size_t size =
sizeof(
INSN);
12479 size_t align = ALIGNMENT_SIZE_OF(
INSN);
12480 unsigned int pos = 0;
12483 size_t padding = calc_padding((
void *)&storage->buff[pos], align);
12484 size_t offset = pos + size + padding;
12485 if (offset > storage->size || offset > storage->pos) {
12487 storage = storage->next;
12490 pos += (int)padding;
12492 iobj = (
INSN *)&storage->buff[pos];
12494 if (iobj->operands) {
12495 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_move, (
VALUE)0);
12508 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
12515#define SYM(s) ID2SYM(rb_intern_const(#s))
12517 unsigned int arg_size, local_size, stack_max;
12519 struct st_table *labels_table = st_init_numtable();
12521 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
12522 VALUE keywords = rb_hash_aref(params, SYM(keyword));
12524 DECL_ANCHOR(anchor);
12525 INIT_ANCHOR(anchor);
12528 ISEQ_BODY(iseq)->local_table_size =
len;
12529 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12531 for (i = 0; i <
len; i++) {
12534 if (sym_arg_rest == lv) {
12542#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12543 if (INT_PARAM(lead_num)) {
12544 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12546 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12547 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12548 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12549 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12552#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12554 INT_PARAM(arg_size);
12555 INT_PARAM(local_size);
12556 INT_PARAM(stack_max);
12560 VALUE source_hash = rb_hash_aref(misc,
ID2SYM(rb_intern(
"source_hash")));
12561 if (!
NIL_P(source_hash)) {
12562 ISEQ_BODY(iseq)->source_hash =
NUM2ULL(source_hash);
12566#ifdef USE_ISEQ_NODE_ID
12567 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
12575 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12577 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12580 for (i = 0; i <
len; i++) {
12582 LABEL *label = register_label(iseq, labels_table, ent);
12583 opt_table[i] = (
VALUE)label;
12586 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12587 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12590 else if (!
NIL_P(arg_opt_labels)) {
12591 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
12596 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12598 else if (!
NIL_P(keywords)) {
12599 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
12603 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
12604 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12607 if (
Qtrue == rb_hash_aref(params, SYM(use_block))) {
12608 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12611 if (int_param(&i, params, SYM(kwrest))) {
12612 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12613 if (keyword == NULL) {
12614 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12616 keyword->rest_start = i;
12617 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12620 iseq_calc_param_size(iseq);
12623 iseq_build_from_ary_exception(iseq, labels_table, exception);
12626 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12628 ISEQ_BODY(iseq)->param.size = arg_size;
12629 ISEQ_BODY(iseq)->local_table_size = local_size;
12630 ISEQ_BODY(iseq)->stack_max = stack_max;
12640 while (body->type == ISEQ_TYPE_BLOCK ||
12641 body->type == ISEQ_TYPE_RESCUE ||
12642 body->type == ISEQ_TYPE_ENSURE ||
12643 body->type == ISEQ_TYPE_EVAL ||
12644 body->type == ISEQ_TYPE_MAIN
12648 for (i = 0; i < body->local_table_size; i++) {
12649 if (body->local_table[i] ==
id) {
12653 iseq = body->parent_iseq;
12654 body = ISEQ_BODY(iseq);
12667 for (i=0; i<body->local_table_size; i++) {
12668 if (body->local_table[i] ==
id) {
12678#ifndef IBF_ISEQ_DEBUG
12679#define IBF_ISEQ_DEBUG 0
12682#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12683#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12686typedef uint32_t ibf_offset_t;
12687#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12689#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12691#define IBF_DEVEL_VERSION 8
12692#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12694#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12697static const char IBF_ENDIAN_MARK =
12698#ifdef WORDS_BIGENDIAN
12707 uint32_t major_version;
12708 uint32_t minor_version;
12710 uint32_t extra_size;
12712 uint32_t iseq_list_size;
12713 uint32_t global_object_list_size;
12714 ibf_offset_t iseq_list_offset;
12715 ibf_offset_t global_object_list_offset;
12736 unsigned int obj_list_size;
12737 ibf_offset_t obj_list_offset;
12756pinned_list_mark(
void *ptr)
12760 for (i = 0; i < list->size; i++) {
12761 if (list->buffer[i]) {
12762 rb_gc_mark(list->buffer[i]);
12774 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
12778pinned_list_fetch(
VALUE list,
long offset)
12784 if (offset < 0 || offset >= ptr->size) {
12785 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12788 return ptr->buffer[offset];
12792pinned_list_store(
VALUE list,
long offset,
VALUE object)
12798 if (offset < 0 || offset >= ptr->size) {
12799 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12806pinned_list_new(
long size)
12808 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
12809 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
12810 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
12816ibf_dump_pos(
struct ibf_dump *dump)
12818 long pos = RSTRING_LEN(dump->current_buffer->str);
12819#if SIZEOF_LONG > SIZEOF_INT
12820 if (pos >= UINT_MAX) {
12824 return (
unsigned int)pos;
12828ibf_dump_align(
struct ibf_dump *dump,
size_t align)
12830 ibf_offset_t pos = ibf_dump_pos(dump);
12832 static const char padding[
sizeof(
VALUE)];
12833 size_t size = align - ((size_t)pos % align);
12834#if SIZEOF_LONG > SIZEOF_INT
12835 if (pos + size >= UINT_MAX) {
12839 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
12840 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
12842 rb_str_cat(dump->current_buffer->str, padding, size);
12847ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
12849 ibf_offset_t pos = ibf_dump_pos(dump);
12850#if SIZEOF_LONG > SIZEOF_INT
12852 if (size >= UINT_MAX || pos + size >= UINT_MAX) {
12856 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
12861ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
12863 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
12867ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
12869 VALUE str = dump->current_buffer->str;
12870 char *ptr = RSTRING_PTR(str);
12871 if ((
unsigned long)(size + offset) > (
unsigned long)RSTRING_LEN(str))
12872 rb_bug(
"ibf_dump_overwrite: overflow");
12873 memcpy(ptr + offset, buff, size);
12877ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
12879 ibf_offset_t beg = *offset;
12881 return load->current_buffer->buff + beg;
12885ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
12887 void *buff = ruby_xmalloc2(x, y);
12888 size_t size = x * y;
12889 memcpy(buff, load->current_buffer->buff + offset, size);
12893#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
12895#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
12896#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
12897#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
12898#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
12899#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
12902ibf_table_lookup(
struct st_table *table, st_data_t key)
12906 if (st_lookup(table, key, &val)) {
12915ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
12917 int index = ibf_table_lookup(table, key);
12920 index = (int)table->num_entries;
12921 st_insert(table, key, (st_data_t)index);
12929static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
12935ibf_dump_object_table_new(
void)
12937 st_table *obj_table = st_init_numtable();
12938 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
12946 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
12952 if (
id == 0 || rb_id2name(
id) == NULL) {
12955 return ibf_dump_object(dump,
rb_id2sym(
id));
12959ibf_load_id(
const struct ibf_load *load,
const ID id_index)
12961 if (id_index == 0) {
12964 VALUE sym = ibf_load_object(load, id_index);
12974static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
12979 if (iseq == NULL) {
12983 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
12987static unsigned char
12988ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
12990 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
12991 return (
unsigned char)load->current_buffer->buff[(*offset)++];
13007 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
13008 ibf_dump_write(dump, &x,
sizeof(
VALUE));
13012 enum { max_byte_length =
sizeof(
VALUE) + 1 };
13014 unsigned char bytes[max_byte_length];
13017 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
13018 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
13024 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
13027 ibf_dump_write(dump, bytes + max_byte_length - n, n);
13031ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
13033 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
13034 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
13036 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
13037 *offset +=
sizeof(
VALUE);
13042 enum { max_byte_length =
sizeof(
VALUE) + 1 };
13044 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
13045 const unsigned char c = buffer[*offset];
13049 c == 0 ? 9 : ntz_int32(c) + 1;
13052 if (*offset + n > load->current_buffer->size) {
13057 for (i = 1; i < n; i++) {
13059 x |= (
VALUE)buffer[*offset + i];
13073 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
13075 size_t len = strlen(bf->name);
13076 ibf_dump_write_small_value(dump, (
VALUE)
len);
13077 ibf_dump_write(dump, bf->name,
len);
13081ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
13083 int i = (int)ibf_load_small_value(load, offset);
13084 int len = (int)ibf_load_small_value(load, offset);
13085 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
13088 fprintf(stderr,
"%.*s!!\n",
len, name);
13092 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
13093 if (strncmp(table[i].name, name,
len) != 0) {
13094 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %.*s but %s)",
13095 i,
len, name, table[i].name);
13106 const int iseq_size = body->iseq_size;
13108 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
13110 ibf_offset_t offset = ibf_dump_pos(dump);
13112 for (code_index=0; code_index<iseq_size;) {
13113 const VALUE insn = orig_code[code_index++];
13114 const char *types = insn_op_types(insn);
13119 ibf_dump_write_small_value(dump, insn);
13122 for (op_index=0; types[op_index]; op_index++, code_index++) {
13123 VALUE op = orig_code[code_index];
13126 switch (types[op_index]) {
13129 wv = ibf_dump_object(dump, op);
13138 wv = ibf_dump_object(dump, arr);
13146 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
13154 wv = ibf_dump_id(dump, (
ID)op);
13166 ibf_dump_write_small_value(dump, wv);
13176ibf_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)
13179 unsigned int code_index;
13180 ibf_offset_t reading_pos = bytecode_offset;
13184 struct rb_call_data *cd_entries = load_body->call_data;
13187 load_body->iseq_encoded = code;
13188 load_body->iseq_size = iseq_size;
13190 iseq_bits_t * mark_offset_bits;
13191 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
13192 load_body->mark_bits.single = 0;
13193 mark_offset_bits = &load_body->mark_bits.single;
13196 load_body->mark_bits.list =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
13197 mark_offset_bits = load_body->mark_bits.list;
13199 bool needs_bitmap =
false;
13201 for (code_index=0; code_index<iseq_size;) {
13203 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
13204 const char *types = insn_op_types(insn);
13210 for (op_index=0; types[op_index]; op_index++, code_index++) {
13211 const char operand_type = types[op_index];
13212 switch (operand_type) {
13215 VALUE op = ibf_load_small_value(load, &reading_pos);
13216 VALUE v = ibf_load_object(load, op);
13217 code[code_index] = v;
13220 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13221 needs_bitmap =
true;
13227 VALUE op = ibf_load_small_value(load, &reading_pos);
13228 VALUE v = ibf_load_object(load, op);
13233 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
13235 code[code_index] = v;
13236 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13238 needs_bitmap =
true;
13243 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
13245 code[code_index] = v;
13248 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13249 needs_bitmap =
true;
13255 VALUE op = ibf_load_small_value(load, &reading_pos);
13256 VALUE arr = ibf_load_object(load, op);
13258 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
13259 ic->
segments = array_to_idlist(arr);
13261 code[code_index] = (
VALUE)ic;
13268 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13270 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
13271 code[code_index] = (
VALUE)ic;
13273 if (operand_type == TS_IVC) {
13276 if (insn == BIN(setinstancevariable)) {
13277 ID iv_name = (
ID)code[code_index - 1];
13278 cache->iv_set_name = iv_name;
13279 cache->value = IVAR_CACHE_INIT;
13282 cache->iv_set_name = 0;
13283 cache->value = rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET);
13291 code[code_index] = (
VALUE)cd_entries++;
13296 VALUE op = ibf_load_small_value(load, &reading_pos);
13297 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
13304 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
13307 code[code_index] = ibf_load_small_value(load, &reading_pos);
13311 if (insn_len(insn) != op_index+1) {
13316 if (!needs_bitmap) {
13317 SIZED_FREE_N(load_body->mark_bits.list, ISEQ_MBITS_BUFLEN(iseq_size));
13318 load_body->mark_bits.list = NULL;
13322 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
13329 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
13332 IBF_W_ALIGN(
VALUE);
13333 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
13336 return ibf_dump_pos(dump);
13341ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
13345 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
13356 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
13359 struct rb_iseq_param_keyword dump_kw = *kw;
13360 int dv_num = kw->num - kw->required_num;
13365 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
13366 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
13368 dump_kw.table = IBF_W(ids,
ID, kw->num);
13369 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
13370 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
13371 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
13378static const struct rb_iseq_param_keyword *
13379ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
13381 if (param_keyword_offset) {
13382 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
13383 int dv_num = kw->num - kw->required_num;
13384 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
13387 for (i=0; i<dv_num; i++) {
13388 dvs[i] = ibf_load_object(load, dvs[i]);
13394 kw->default_values = dvs;
13405 ibf_offset_t offset = ibf_dump_pos(dump);
13409 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
13410 ibf_dump_write_small_value(dump, entries[i].line_no);
13411#ifdef USE_ISEQ_NODE_ID
13412 ibf_dump_write_small_value(dump, entries[i].node_id);
13414 ibf_dump_write_small_value(dump, entries[i].events);
13421ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
13423 ibf_offset_t reading_pos = body_offset;
13427 for (i = 0; i < size; i++) {
13428 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
13429#ifdef USE_ISEQ_NODE_ID
13430 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
13432 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
13439ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
13441 ibf_offset_t offset = ibf_dump_pos(dump);
13443 unsigned int last = 0;
13445 for (i = 0; i < size; i++) {
13446 ibf_dump_write_small_value(dump, positions[i] - last);
13447 last = positions[i];
13453static unsigned int *
13454ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
13456 ibf_offset_t reading_pos = positions_offset;
13457 unsigned int *positions =
ALLOC_N(
unsigned int, size);
13459 unsigned int last = 0;
13461 for (i = 0; i < size; i++) {
13462 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
13463 last = positions[i];
13473 const int size = body->local_table_size;
13477 for (i=0; i<size; i++) {
13478 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13481 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13487 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
13491ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13494 ID *table = IBF_R(local_table_offset,
ID, size);
13497 for (i=0; i<size; i++) {
13498 table[i] = ibf_load_id(load, table[i]);
13501 if (size == 1 && table[0] == idERROR_INFO) {
13502 ruby_xfree_sized(table,
sizeof(
ID) * size);
13503 return rb_iseq_shared_exc_local_tbl;
13518 const int size = ISEQ_LVAR_STATES_BUFLEN(body->local_table_size);
13519 IBF_W_ALIGN(uint8_t);
13520 if (iseq_has_lvar_states_p(body)) {
13521 return ibf_dump_write(dump, iseq_lvar_states(body),
sizeof(uint8_t) * size);
13524 return ibf_dump_write(dump, NULL, 0);
13529ibf_load_lvar_states(
const struct ibf_load *load,
struct rb_iseq_constant_body *load_body, ibf_offset_t lvar_states_offset,
int size,
const ID *local_table)
13531 if (local_table == rb_iseq_shared_exc_local_tbl ||
13533 load_body->lvar_states.list = NULL;
13535 else if (ISEQ_LVAR_STATES_EMBED_P((
unsigned int)size)) {
13536 ibf_offset_t pos = lvar_states_offset;
13537 const int len =
sizeof(uint8_t) * ISEQ_LVAR_STATES_BUFLEN(size);
13538 memcpy(load_body->lvar_states.single, ibf_load_ptr(load, &pos,
len),
len);
13541 load_body->lvar_states.list = IBF_R(lvar_states_offset, uint8_t, ISEQ_LVAR_STATES_BUFLEN(size));
13551 int *iseq_indices =
ALLOCA_N(
int, table->size);
13554 for (i=0; i<table->size; i++) {
13555 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13558 const ibf_offset_t offset = ibf_dump_pos(dump);
13560 for (i=0; i<table->size; i++) {
13561 ibf_dump_write_small_value(dump, iseq_indices[i]);
13562 ibf_dump_write_small_value(dump, table->entries[i].type);
13563 ibf_dump_write_small_value(dump, table->entries[i].start);
13564 ibf_dump_write_small_value(dump, table->entries[i].end);
13565 ibf_dump_write_small_value(dump, table->entries[i].cont);
13566 ibf_dump_write_small_value(dump, table->entries[i].sp);
13571 return ibf_dump_pos(dump);
13576ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size,
const rb_iseq_t *parent_iseq)
13579 struct iseq_catch_table *table = ruby_xcalloc(1, iseq_catch_table_bytes(size));
13580 table->size = size;
13581 ISEQ_BODY(parent_iseq)->catch_table = table;
13583 ibf_offset_t reading_pos = catch_table_offset;
13586 for (i=0; i<table->size; i++) {
13587 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13588 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13589 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13590 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13591 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13592 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13595 RB_OBJ_WRITE(parent_iseq, UNALIGNED_MEMBER_PTR(&table->entries[i], iseq), catch_iseq);
13599 ISEQ_BODY(parent_iseq)->catch_table = NULL;
13607 const unsigned int ci_size = body->ci_size;
13610 ibf_offset_t offset = ibf_dump_pos(dump);
13614 for (i = 0; i < ci_size; i++) {
13617 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13618 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13619 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13623 int len = kwarg->keyword_len;
13624 ibf_dump_write_small_value(dump,
len);
13625 for (
int j=0; j<
len; j++) {
13626 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13627 ibf_dump_write_small_value(dump, keyword);
13631 ibf_dump_write_small_value(dump, 0);
13636 ibf_dump_write_small_value(dump, (
VALUE)-1);
13654static enum rb_id_table_iterator_result
13655store_outer_variable(
ID id,
VALUE val,
void *dump)
13660 pair->name = rb_id2str(
id);
13662 return ID_TABLE_CONTINUE;
13666outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13674 else if (!bp->name) {
13684 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13686 ibf_offset_t offset = ibf_dump_pos(dump);
13688 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13689 ibf_dump_write_small_value(dump, (
VALUE)size);
13698 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13700 for (
size_t i = 0; i < size; ++i) {
13701 ID id = ovlist->pairs[i].id;
13702 ID val = ovlist->pairs[i].val;
13703 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13704 ibf_dump_write_small_value(dump, val);
13713ibf_load_ci_entries(
const struct ibf_load *load,
13714 ibf_offset_t ci_entries_offset,
13715 unsigned int ci_size,
13723 ibf_offset_t reading_pos = ci_entries_offset;
13730 for (i = 0; i < ci_size; i++) {
13731 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
13732 if (mid_index != (
VALUE)-1) {
13733 ID mid = ibf_load_id(load, mid_index);
13734 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13735 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13738 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
13741 kwarg->references = 0;
13742 kwarg->keyword_len = kwlen;
13743 for (
int j=0; j<kwlen; j++) {
13744 VALUE keyword = ibf_load_small_value(load, &reading_pos);
13745 kwarg->keywords[j] = ibf_load_object(load, keyword);
13749 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
13751 cds[i].cc = vm_cc_empty();
13762ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
13764 ibf_offset_t reading_pos = outer_variables_offset;
13768 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
13770 if (table_size > 0) {
13771 tbl = rb_id_table_create(table_size);
13774 for (
size_t i = 0; i < table_size; i++) {
13775 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
13776 VALUE value = ibf_load_small_value(load, &reading_pos);
13777 if (!key) key = rb_make_temporary_id(i);
13778 rb_id_table_insert(tbl, key, value);
13787 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
13789 unsigned int *positions;
13793 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
13794 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
13796#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13797 ibf_offset_t iseq_start = ibf_dump_pos(dump);
13802 buffer.obj_table = ibf_dump_object_table_new();
13803 dump->current_buffer = &buffer;
13806 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
13807 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
13808 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
13809 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
13810 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
13812 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
13813 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
13814 SIZED_FREE_N(positions, ISEQ_BODY(iseq)->insns_info.size);
13816 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
13817 const ibf_offset_t lvar_states_offset = ibf_dump_lvar_states(dump, iseq);
13818 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
13819 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
13821 const int parent_iseq_index = ibf_table_lookup(dump->iseq_table, (st_data_t)ISEQ_BODY(iseq)->parent_iseq);
13822 const int local_iseq_index = ibf_table_lookup(dump->iseq_table, (st_data_t)ISEQ_BODY(iseq)->local_iseq);
13823 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
13824 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
13825 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
13827#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13828 ibf_offset_t local_obj_list_offset;
13829 unsigned int local_obj_list_size;
13831 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
13834 ibf_offset_t body_offset = ibf_dump_pos(dump);
13837 unsigned int param_flags =
13838 (body->param.flags.has_lead << 0) |
13839 (body->param.flags.has_opt << 1) |
13840 (body->param.flags.has_rest << 2) |
13841 (body->param.flags.has_post << 3) |
13842 (body->param.flags.has_kw << 4) |
13843 (body->param.flags.has_kwrest << 5) |
13844 (body->param.flags.has_block << 6) |
13845 (body->param.flags.ambiguous_param0 << 7) |
13846 (body->param.flags.accepts_no_kwarg << 8) |
13847 (body->param.flags.ruby2_keywords << 9) |
13848 (body->param.flags.anon_rest << 10) |
13849 (body->param.flags.anon_kwrest << 11) |
13850 (body->param.flags.use_block << 12) |
13851 (body->param.flags.forwardable << 13) |
13852 (body->param.flags.accepts_no_block << 14);
13854#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13855# define IBF_BODY_OFFSET(x) (x)
13857# define IBF_BODY_OFFSET(x) (body_offset - (x))
13860 ibf_dump_write_small_value(dump, body->type);
13861 ibf_dump_write_small_value(dump, body->iseq_size);
13862 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
13863 ibf_dump_write_small_value(dump, bytecode_size);
13864 ibf_dump_write_small_value(dump, param_flags);
13865 ibf_dump_write_small_value(dump, body->param.size);
13866 ibf_dump_write_small_value(dump, body->param.lead_num);
13867 ibf_dump_write_small_value(dump, body->param.opt_num);
13868 ibf_dump_write_small_value(dump, body->param.rest_start);
13869 ibf_dump_write_small_value(dump, body->param.post_start);
13870 ibf_dump_write_small_value(dump, body->param.post_num);
13871 ibf_dump_write_small_value(dump, body->param.block_start);
13872 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
13873 ibf_dump_write_small_value(dump, param_keyword_offset);
13874 ibf_dump_write_small_value(dump, location_pathobj_index);
13875 ibf_dump_write_small_value(dump, location_label_index);
13876 ibf_dump_write_small_value(dump, body->location.first_lineno);
13877 ibf_dump_write_small_value(dump, body->location.node_id);
13880 ibf_dump_write_small_value(dump, (
VALUE)(uint32_t)(body->source_hash >> 32));
13881 ibf_dump_write_small_value(dump, (
VALUE)(uint32_t)body->source_hash);
13882 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
13883 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
13884 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
13885 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
13886 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
13887 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
13888 ibf_dump_write_small_value(dump, body->insns_info.size);
13889 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
13890 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(lvar_states_offset));
13891 ibf_dump_write_small_value(dump, catch_table_size);
13892 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
13893 ibf_dump_write_small_value(dump, parent_iseq_index);
13894 ibf_dump_write_small_value(dump, local_iseq_index);
13895 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
13896 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
13897 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
13898 ibf_dump_write_small_value(dump, ISEQ_FLIP_CNT(iseq));
13899 ibf_dump_write_small_value(dump, body->local_table_size);
13900 ibf_dump_write_small_value(dump, body->ivc_size);
13901 ibf_dump_write_small_value(dump, body->icvarc_size);
13902 ibf_dump_write_small_value(dump, body->ise_size);
13903 ibf_dump_write_small_value(dump, body->ic_size);
13904 ibf_dump_write_small_value(dump, body->ci_size);
13905 ibf_dump_write_small_value(dump, body->stack_max);
13906 ibf_dump_write_small_value(dump, body->builtin_attrs);
13907 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
13909#undef IBF_BODY_OFFSET
13911#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13912 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
13914 dump->current_buffer = saved_buffer;
13915 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
13917 ibf_offset_t offset = ibf_dump_pos(dump);
13918 ibf_dump_write_small_value(dump, iseq_start);
13919 ibf_dump_write_small_value(dump, iseq_length_bytes);
13920 ibf_dump_write_small_value(dump, body_offset);
13922 ibf_dump_write_small_value(dump, local_obj_list_offset);
13923 ibf_dump_write_small_value(dump, local_obj_list_size);
13925 st_free_table(buffer.obj_table);
13929 return body_offset;
13934ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
13936 VALUE str = ibf_load_object(load, str_index);
13938 str = rb_fstring(str);
13948 ibf_offset_t reading_pos = offset;
13950#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13952 load->current_buffer = &load->global_buffer;
13954 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13955 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13956 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13959 buffer.buff = load->global_buffer.buff + iseq_start;
13960 buffer.size = iseq_length_bytes;
13961 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13962 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13963 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
13965 load->current_buffer = &buffer;
13966 reading_pos = body_offset;
13969#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13970# define IBF_BODY_OFFSET(x) (x)
13972# define IBF_BODY_OFFSET(x) (offset - (x))
13975 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13976 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13977 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13978 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13979 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13980 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13981 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
13982 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
13983 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
13984 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
13985 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
13986 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
13987 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13988 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13989 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
13990 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
13991 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
13992 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
13993 const uint64_t source_hash_hi = (uint64_t)ibf_load_small_value(load, &reading_pos);
13994 const uint64_t source_hash_lo = (uint64_t)ibf_load_small_value(load, &reading_pos);
13995 const uint64_t source_hash = (source_hash_hi << 32) | (uint32_t)source_hash_lo;
13996 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13997 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13998 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13999 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
14000 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14001 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14002 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14003 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14004 const ibf_offset_t lvar_states_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14005 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14006 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14007 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
14008 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
14009 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
14010 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14011 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14012 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
14013 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14015 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14016 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14017 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14018 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14020 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14021 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14022 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14023 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
14026 VALUE path = ibf_load_object(load, location_pathobj_index);
14031 realpath = path = rb_fstring(path);
14034 VALUE pathobj = path;
14040 if (!
NIL_P(realpath)) {
14042 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
14043 "(%x), path=%+"PRIsVALUE,
14044 realpath,
TYPE(realpath), path);
14046 realpath = rb_fstring(realpath);
14052 rb_iseq_pathobj_set(iseq, path, realpath);
14057 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
14059#undef IBF_BODY_OFFSET
14061 load_body->type =
type;
14062 load_body->stack_max = stack_max;
14063 load_body->param.flags.has_lead = (param_flags >> 0) & 1;
14064 load_body->param.flags.has_opt = (param_flags >> 1) & 1;
14065 load_body->param.flags.has_rest = (param_flags >> 2) & 1;
14066 load_body->param.flags.has_post = (param_flags >> 3) & 1;
14067 load_body->param.flags.has_kw = FALSE;
14068 load_body->param.flags.has_kwrest = (param_flags >> 5) & 1;
14069 load_body->param.flags.has_block = (param_flags >> 6) & 1;
14070 load_body->param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
14071 load_body->param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
14072 load_body->param.flags.ruby2_keywords = (param_flags >> 9) & 1;
14073 load_body->param.flags.anon_rest = (param_flags >> 10) & 1;
14074 load_body->param.flags.anon_kwrest = (param_flags >> 11) & 1;
14075 load_body->param.flags.use_block = (param_flags >> 12) & 1;
14076 load_body->param.flags.forwardable = (param_flags >> 13) & 1;
14077 load_body->param.flags.accepts_no_block = (param_flags >> 14) & 1;
14078 load_body->param.size = param_size;
14079 load_body->param.lead_num = param_lead_num;
14080 load_body->param.opt_num = param_opt_num;
14081 load_body->param.rest_start = param_rest_start;
14082 load_body->param.post_start = param_post_start;
14083 load_body->param.post_num = param_post_num;
14084 load_body->param.block_start = param_block_start;
14085 load_body->local_table_size = local_table_size;
14086 load_body->ci_size = ci_size;
14087 load_body->insns_info.size = insns_info_size;
14090 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
14091 if (variable_flip_count) {
14093 v->flip_count = variable_flip_count;
14096 load_body->location.first_lineno = location_first_lineno;
14097 load_body->location.node_id = location_node_id;
14098 load_body->source_hash = source_hash;
14099 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
14100 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
14101 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
14102 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
14103 load_body->builtin_attrs = builtin_attrs;
14104 load_body->prism = prism;
14106 load_body->ivc_size = ivc_size;
14107 load_body->icvarc_size = icvarc_size;
14108 load_body->ise_size = ise_size;
14109 load_body->ic_size = ic_size;
14111 if (ISEQ_IS_SIZE(load_body)) {
14115 load_body->is_entries = NULL;
14117 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
14118 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
14119 load_body->param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
14120 load_body->param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
14121 load_body->param.flags.has_kw = (param_flags >> 4) & 1;
14122 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
14123 load_body->insns_info.positions_or_succ_index_table.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
14124 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
14125 ibf_load_lvar_states(load, load_body, lvar_states_offset, local_table_size, load_body->local_table);
14126 ibf_load_catch_table(load, catch_table_offset, catch_table_size, iseq);
14130 const rb_iseq_t *mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
14132 RB_OBJ_WRITE(iseq, &load_body->parent_iseq, parent_iseq);
14133 RB_OBJ_WRITE(iseq, &load_body->local_iseq, local_iseq);
14134 RB_OBJ_WRITE(iseq, &load_body->mandatory_only_iseq, mandatory_only_iseq);
14137 if (load_body->param.keyword != NULL) {
14139 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->param.keyword;
14140 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
14143 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
14144#if VM_INSN_INFO_TABLE_IMPL == 2
14145 rb_iseq_insns_info_encode_positions(iseq);
14148 rb_iseq_translate_threaded_code(iseq);
14150#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14151 load->current_buffer = &load->global_buffer;
14154 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
14156#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14157 load->current_buffer = saved_buffer;
14159 verify_call_cache(iseq);
14162 rb_vm_pop_frame_no_int(ec);
14172ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14177 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
14180 return ST_CONTINUE;
14190 args.offset_list = offset_list;
14192 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
14195 st_index_t size = dump->iseq_table->num_entries;
14196 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
14198 for (i = 0; i < size; i++) {
14202 ibf_dump_align(dump,
sizeof(ibf_offset_t));
14203 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
14204 header->iseq_list_size = (
unsigned int)size;
14214 unsigned int type: 5;
14215 unsigned int special_const: 1;
14216 unsigned int frozen: 1;
14217 unsigned int internal: 1;
14220enum ibf_object_class_index {
14221 IBF_OBJECT_CLASS_OBJECT,
14222 IBF_OBJECT_CLASS_ARRAY,
14223 IBF_OBJECT_CLASS_STANDARD_ERROR,
14224 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
14225 IBF_OBJECT_CLASS_TYPE_ERROR,
14226 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
14236 long keyval[FLEX_ARY_LEN];
14249 BDIGIT digits[FLEX_ARY_LEN];
14252enum ibf_object_data_type {
14253 IBF_OBJECT_DATA_ENCODING,
14264#define IBF_ALIGNED_OFFSET(align, offset) \
14265 ((((offset) - 1) / (align) + 1) * (align))
14270#define IBF_OBJBODY(type, offset) \
14271 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
14274ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
14276 if (offset >= load->current_buffer->size) {
14277 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
14279 return load->current_buffer->buff + offset;
14282NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
14285ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
14288 rb_raw_obj_info(buff,
sizeof(buff), obj);
14297 rb_raise(rb_eArgError,
"unsupported");
14304 enum ibf_object_class_index cindex;
14306 cindex = IBF_OBJECT_CLASS_OBJECT;
14309 cindex = IBF_OBJECT_CLASS_ARRAY;
14312 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
14315 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
14318 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
14321 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
14324 rb_obj_info_dump(obj);
14326 rb_bug(
"unsupported class");
14328 ibf_dump_write_small_value(dump, (
VALUE)cindex);
14334 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
14337 case IBF_OBJECT_CLASS_OBJECT:
14339 case IBF_OBJECT_CLASS_ARRAY:
14341 case IBF_OBJECT_CLASS_STANDARD_ERROR:
14343 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
14345 case IBF_OBJECT_CLASS_TYPE_ERROR:
14347 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
14351 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
14359 (void)IBF_W(&dbl,
double, 1);
14367 memcpy(&d, IBF_OBJBODY(
double, offset),
sizeof(d));
14369 if (!
FLONUM_P(f)) RB_OBJ_SET_SHAREABLE(f);
14376 long encindex = (long)rb_enc_get_index(obj);
14377 long len = RSTRING_LEN(obj);
14378 const char *ptr = RSTRING_PTR(obj);
14380 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14381 rb_encoding *enc = rb_enc_from_index((
int)encindex);
14382 const char *enc_name = rb_enc_name(enc);
14383 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
14386 ibf_dump_write_small_value(dump, encindex);
14387 ibf_dump_write_small_value(dump,
len);
14388 IBF_WP(ptr,
char,
len);
14394 ibf_offset_t reading_pos = offset;
14396 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14397 const long len = (long)ibf_load_small_value(load, &reading_pos);
14398 const char *ptr = load->current_buffer->buff + reading_pos;
14400 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14401 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14402 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14406 if (header->frozen && !header->internal) {
14407 str = rb_enc_literal_str(ptr,
len, rb_enc_from_index(encindex));
14410 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
14413 if (header->frozen) str = rb_fstring(str);
14424 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
14426 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
14427 ibf_dump_write_small_value(dump, regexp.srcstr);
14434 regexp.option = ibf_load_byte(load, &offset);
14435 regexp.srcstr = ibf_load_small_value(load, &offset);
14437 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
14438 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
14441 if (header->frozen) RB_OBJ_SET_SHAREABLE(
rb_obj_freeze(reg));
14450 ibf_dump_write_small_value(dump,
len);
14451 for (i=0; i<
len; i++) {
14452 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
14453 ibf_dump_write_small_value(dump, index);
14460 ibf_offset_t reading_pos = offset;
14462 const long len = (long)ibf_load_small_value(load, &reading_pos);
14467 for (i=0; i<
len; i++) {
14468 const VALUE index = ibf_load_small_value(load, &reading_pos);
14472 if (header->frozen) {
14481ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
14485 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14486 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14488 ibf_dump_write_small_value(dump, key_index);
14489 ibf_dump_write_small_value(dump, val_index);
14490 return ST_CONTINUE;
14497 ibf_dump_write_small_value(dump, (
VALUE)
len);
14505 long len = (long)ibf_load_small_value(load, &offset);
14506 VALUE obj = header->frozen ? rb_hash_alloc_fixed_size(
rb_cHash,
len) : rb_hash_new_capa(
len);
14509 for (i = 0; i <
len; i++) {
14510 VALUE key_index = ibf_load_small_value(load, &offset);
14511 VALUE val_index = ibf_load_small_value(load, &offset);
14513 VALUE key = ibf_load_object(load, key_index);
14514 VALUE val = ibf_load_object(load, val_index);
14515 rb_hash_aset(obj, key, val);
14519 if (header->frozen) {
14520 RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14527ibf_dump_cdhash_i(st_data_t key, st_data_t val, st_data_t ptr)
14531 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14533 ibf_dump_write_small_value(dump, key_index);
14534 ibf_dump_write_small_value(dump, val);
14535 return ST_CONTINUE;
14541 switch (imemo_type(obj)) {
14542 case imemo_cdhash: {
14543 st_table *tbl = rb_imemo_cdhash_tbl(obj);
14545 long len = tbl->num_entries;
14546 ibf_dump_write_small_value(dump, (
VALUE)
len);
14547 if (
len > 0) st_foreach(tbl, ibf_dump_cdhash_i, (
VALUE)dump);
14551 ibf_dump_object_unsupported(dump, obj);
14559 long len = (long)ibf_load_small_value(load, &offset);
14563 for (i = 0; i <
len; i++) {
14564 VALUE key_index = ibf_load_small_value(load, &offset);
14565 VALUE val = ibf_load_small_value(load, &offset);
14567 VALUE key = ibf_load_object(load, key_index);
14568 cdhash_aset(obj, key, val);
14582 range.class_index = 0;
14585 range.beg = (long)ibf_dump_object(dump, beg);
14586 range.end = (long)ibf_dump_object(dump, end);
14592 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
14601 VALUE beg = ibf_load_object(load, range->beg);
14602 VALUE end = ibf_load_object(load, range->end);
14605 if (header->frozen) RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14612 ssize_t
len = BIGNUM_LEN(obj);
14613 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14614 BDIGIT *d = BIGNUM_DIGITS(obj);
14616 (void)IBF_W(&slen, ssize_t, 1);
14617 IBF_WP(d, BDIGIT,
len);
14624 int sign = bignum->slen > 0;
14625 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14626 const int big_unpack_flags =
14629 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
14633 if (header->frozen) RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14640 if (rb_data_is_encoding(obj)) {
14642 const char *name = rb_enc_name(enc);
14643 long len = strlen(name) + 1;
14645 data[0] = IBF_OBJECT_DATA_ENCODING;
14647 (void)IBF_W(data,
long, 2);
14648 IBF_WP(name,
char,
len);
14651 ibf_dump_object_unsupported(dump, obj);
14658 const long *body = IBF_OBJBODY(
long, offset);
14659 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14661 const char *data = (
const char *)&body[2];
14664 case IBF_OBJECT_DATA_ENCODING:
14666 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
14671 return ibf_load_object_unsupported(load, header, offset);
14675ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14678 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14679 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14681 (void)IBF_W(data,
long, 2);
14685ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14688 VALUE a = ibf_load_object(load, nums->a);
14689 VALUE b = ibf_load_object(load, nums->b);
14701 ibf_dump_object_string(dump,
rb_sym2str(obj));
14707 ibf_offset_t reading_pos = offset;
14709 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14710 const long len = (long)ibf_load_small_value(load, &reading_pos);
14711 const char *ptr = load->current_buffer->buff + reading_pos;
14713 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14714 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14715 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14718 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
14722typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
14723static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
14724 ibf_dump_object_unsupported,
14725 ibf_dump_object_unsupported,
14726 ibf_dump_object_class,
14727 ibf_dump_object_unsupported,
14728 ibf_dump_object_float,
14729 ibf_dump_object_string,
14730 ibf_dump_object_regexp,
14731 ibf_dump_object_array,
14732 ibf_dump_object_hash,
14733 ibf_dump_object_struct,
14734 ibf_dump_object_bignum,
14735 ibf_dump_object_unsupported,
14736 ibf_dump_object_data,
14737 ibf_dump_object_unsupported,
14738 ibf_dump_object_complex_rational,
14739 ibf_dump_object_complex_rational,
14740 ibf_dump_object_unsupported,
14741 ibf_dump_object_unsupported,
14742 ibf_dump_object_unsupported,
14743 ibf_dump_object_unsupported,
14744 ibf_dump_object_symbol,
14745 ibf_dump_object_unsupported,
14746 ibf_dump_object_unsupported,
14747 ibf_dump_object_unsupported,
14748 ibf_dump_object_unsupported,
14749 ibf_dump_object_unsupported,
14750 ibf_dump_object_imemo,
14751 ibf_dump_object_unsupported,
14752 ibf_dump_object_unsupported,
14753 ibf_dump_object_unsupported,
14754 ibf_dump_object_unsupported,
14755 ibf_dump_object_unsupported,
14761 unsigned char byte =
14762 (header.type << 0) |
14763 (header.special_const << 5) |
14764 (header.frozen << 6) |
14765 (header.internal << 7);
14771ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
14773 unsigned char byte = ibf_load_byte(load, offset);
14776 header.type = (
byte >> 0) & 0x1f;
14777 header.special_const = (
byte >> 5) & 0x01;
14778 header.frozen = (
byte >> 6) & 0x01;
14779 header.internal = (
byte >> 7) & 0x01;
14788 ibf_offset_t current_offset;
14789 IBF_ZERO(obj_header);
14790 obj_header.type =
TYPE(obj);
14792 IBF_W_ALIGN(ibf_offset_t);
14793 current_offset = ibf_dump_pos(dump);
14798 obj_header.special_const = TRUE;
14799 obj_header.frozen = TRUE;
14800 obj_header.internal = TRUE;
14801 ibf_dump_object_object_header(dump, obj_header);
14802 ibf_dump_write_small_value(dump, obj);
14806 obj_header.special_const = FALSE;
14807 obj_header.frozen =
OBJ_FROZEN(obj) ? TRUE : FALSE;
14808 ibf_dump_object_object_header(dump, obj_header);
14809 (*dump_object_functions[obj_header.type])(dump, obj);
14812 return current_offset;
14816static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
14817 ibf_load_object_unsupported,
14818 ibf_load_object_unsupported,
14819 ibf_load_object_class,
14820 ibf_load_object_unsupported,
14821 ibf_load_object_float,
14822 ibf_load_object_string,
14823 ibf_load_object_regexp,
14824 ibf_load_object_array,
14825 ibf_load_object_hash,
14826 ibf_load_object_struct,
14827 ibf_load_object_bignum,
14828 ibf_load_object_unsupported,
14829 ibf_load_object_data,
14830 ibf_load_object_unsupported,
14831 ibf_load_object_complex_rational,
14832 ibf_load_object_complex_rational,
14833 ibf_load_object_unsupported,
14834 ibf_load_object_unsupported,
14835 ibf_load_object_unsupported,
14836 ibf_load_object_unsupported,
14837 ibf_load_object_symbol,
14838 ibf_load_object_unsupported,
14839 ibf_load_object_unsupported,
14840 ibf_load_object_unsupported,
14841 ibf_load_object_unsupported,
14842 ibf_load_object_unsupported,
14843 ibf_load_object_imemo,
14844 ibf_load_object_unsupported,
14845 ibf_load_object_unsupported,
14846 ibf_load_object_unsupported,
14847 ibf_load_object_unsupported,
14848 ibf_load_object_unsupported,
14852ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
14854 if (object_index == 0) {
14858 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
14860 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
14861 ibf_offset_t offset = offsets[object_index];
14862 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
14865 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
14866 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
14867 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
14868 header.type, header.special_const, header.frozen, header.internal);
14870 if (offset >= load->current_buffer->size) {
14871 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
14874 if (header.special_const) {
14875 ibf_offset_t reading_pos = offset;
14877 obj = ibf_load_small_value(load, &reading_pos);
14880 obj = (*load_object_functions[header.type])(load, &header, offset);
14883 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
14886 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
14887 object_index, obj);
14900ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14905 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
14908 return ST_CONTINUE;
14912ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
14914 st_table *obj_table = dump->current_buffer->obj_table;
14919 args.offset_list = offset_list;
14921 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
14923 IBF_W_ALIGN(ibf_offset_t);
14924 *obj_list_offset = ibf_dump_pos(dump);
14926 st_index_t size = obj_table->num_entries;
14929 for (i=0; i<size; i++) {
14934 *obj_list_size = (
unsigned int)size;
14938ibf_dump_mark(
void *ptr)
14941 rb_gc_mark(dump->global_buffer.str);
14943 rb_mark_set(dump->global_buffer.obj_table);
14944 rb_mark_set(dump->iseq_table);
14948ibf_dump_free(
void *ptr)
14951 if (dump->global_buffer.obj_table) {
14952 st_free_table(dump->global_buffer.obj_table);
14953 dump->global_buffer.obj_table = 0;
14955 if (dump->iseq_table) {
14956 st_free_table(dump->iseq_table);
14957 dump->iseq_table = 0;
14962ibf_dump_memsize(
const void *ptr)
14966 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
14967 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
14973 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
14974 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
14980 dump->global_buffer.obj_table = NULL;
14981 dump->iseq_table = NULL;
14984 dump->global_buffer.obj_table = ibf_dump_object_table_new();
14985 dump->iseq_table = st_init_numtable();
14987 dump->current_buffer = &dump->global_buffer;
14995 ibf_dump_write(dump, &header,
sizeof(header));
14996 ibf_dump_iseq(dump, iseq);
14998 header.magic[0] =
'Y';
14999 header.magic[1] =
'A';
15000 header.magic[2] =
'R';
15001 header.magic[3] =
'B';
15002 header.major_version = IBF_MAJOR_VERSION;
15003 header.minor_version = IBF_MINOR_VERSION;
15004 header.endian = IBF_ENDIAN_MARK;
15006 ibf_dump_iseq_list(dump, &header);
15007 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
15008 header.size = ibf_dump_pos(dump);
15011 VALUE opt_str = opt;
15014 ibf_dump_write(dump, ptr, header.extra_size);
15017 header.extra_size = 0;
15020 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
15022 return dump->global_buffer.str;
15032 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
15033 ISEQ_BODY(iseq)->local_iseq != iseq) {
15036 if (
RTEST(ISEQ_COVERAGE(iseq))) {
15041 ibf_dump_setup(dump, dump_obj);
15043 str = iseq_ibf_dump_0(dump, iseq, opt);
15048static const ibf_offset_t *
15049ibf_iseq_list(
const struct ibf_load *load)
15051 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
15055rb_ibf_load_iseq_complete(
rb_iseq_t *iseq)
15059 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
15062 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
15063 iseq->aux.loader.index, offset,
15064 load->header->size);
15066 ibf_load_iseq_each(load, iseq, offset);
15067 ISEQ_COMPILE_DATA_CLEAR(iseq);
15069 rb_iseq_init_trace(iseq);
15070 load->iseq = prev_src_iseq;
15077 rb_ibf_load_iseq_complete((
rb_iseq_t *)iseq);
15085 int iseq_index = (int)(
VALUE)index_iseq;
15088 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
15089 (
void *)index_iseq, (
void *)load->iseq_list);
15091 if (iseq_index == -1) {
15095 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
15098 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
15106 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
15110 iseq->aux.loader.index = iseq_index;
15112 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
15113 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
15115 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
15117 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
15119 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
15121 rb_ibf_load_iseq_complete(iseq);
15125 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
15126 (
void *)iseq, (
void *)load->iseq);
15134ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
15137 load->loader_obj = loader_obj;
15138 load->global_buffer.buff = bytes;
15139 load->header = header;
15140 load->global_buffer.size = header->size;
15141 load->global_buffer.obj_list_offset = header->global_object_list_offset;
15142 load->global_buffer.obj_list_size = header->global_object_list_size;
15143 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
15144 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
15147 load->current_buffer = &load->global_buffer;
15149 if (size < header->size) {
15152 if (strncmp(header->magic,
"YARB", 4) != 0) {
15155 if (header->major_version != IBF_MAJOR_VERSION ||
15156 header->minor_version != IBF_MINOR_VERSION) {
15158 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
15160 if (header->endian != IBF_ENDIAN_MARK) {
15166 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
15167 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
15168 header->iseq_list_offset);
15170 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
15171 rb_raise(rb_eArgError,
"unaligned object list offset: %u",
15172 load->global_buffer.obj_list_offset);
15185 if (USE_LAZY_LOAD) {
15186 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
15189 ibf_load_setup_bytes(load, loader_obj, RSTRING_PTR(str), RSTRING_LEN(str));
15194ibf_loader_mark(
void *ptr)
15197 rb_gc_mark(load->str);
15198 rb_gc_mark(load->iseq_list);
15199 rb_gc_mark(load->global_buffer.obj_list);
15203ibf_loader_free(
void *ptr)
15210ibf_loader_memsize(
const void *ptr)
15217 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
15218 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE
15222rb_iseq_ibf_load(
VALUE str)
15228 ibf_load_setup(load, loader_obj, str);
15229 iseq = ibf_load_iseq(load, 0);
15236rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
15242 ibf_load_setup_bytes(load, loader_obj, bytes, size);
15243 iseq = ibf_load_iseq(load, 0);
15251ibf_dump_iseq_table_collect_i(st_data_t key, st_data_t val, st_data_t arg)
15254 srcs[(int)val] = (
const rb_iseq_t *)key;
15255 return ST_CONTINUE;
15261iseq_subtree_copy_pc2branchindex(
const struct ibf_dump *dump,
const struct ibf_load *load)
15263 unsigned int iseq_count = (
unsigned int)dump->iseq_table->num_entries;
15266 st_foreach(dump->iseq_table, ibf_dump_iseq_table_collect_i, (st_data_t)srcs);
15268 for (
unsigned int i = 0; i < iseq_count; i++) {
15270 VALUE pc2branchindex = ISEQ_PC2BRANCHINDEX(srcs[i]);
15271 if (pc2branchindex !=
Qnil) {
15272 ISEQ_PC2BRANCHINDEX_SET(copy, pc2branchindex);
15281rb_iseq_dup_with_independent_caches(
const rb_iseq_t *src_root)
15289 ibf_dump_setup(dump, dump_obj);
15291 str = iseq_ibf_dump_0(dump, src_root,
Qnil);
15296 ibf_load_setup(load, loader_obj, str);
15304 unsigned int iseq_count = (
unsigned int)dump->iseq_table->num_entries;
15306 for (
unsigned int i = 0; i < iseq_count; i++) {
15310 rb_ibf_load_iseq_complete(copy);
15316 if (!cb->local_iseq)
RB_OBJ_WRITE(copy, &cb->local_iseq, sb->local_iseq);
15317 RB_OBJ_WRITE(copy, &cb->location.pathobj, sb->location.pathobj);
15318 VALUE sl = ISEQ_SCRIPT_LINES(src_root);
15319 VALUE cov = ISEQ_COVERAGE(src_root);
15327 RB_OBJ_WRITE(copy, &cb->parent_iseq, sb->parent_iseq);
15333 RB_OBJ_SET_SHAREABLE((
VALUE)copy);
15336 if (ISEQ_PC2BRANCHINDEX(src_root) !=
Qnil) {
15337 iseq_subtree_copy_pc2branchindex(dump, load);
15346rb_iseq_ibf_load_extra_data(
VALUE str)
15352 ibf_load_setup(load, loader_obj, str);
15353 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
15358#include "prism_compile.c"
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ATOMIC_PTR_LOAD(var)
Identical to RUBY_ATOMIC_LOAD, except it expects its arguments are void*.
#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 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 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 FLONUM_P
Old name of RB_FLONUM_P.
#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 ALLOCV_N
Old name of RB_ALLOCV_N.
#define NUM2ULL
Old name of RB_NUM2ULL.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define 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_cObject
Object class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_cHash
Hash class.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cRange
Range class.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RB_POSFIXABLE(_)
Checks if the passed value is in range of fixnum, assuming it is a positive number.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_reverse(VALUE ary)
Destructively reverses the passed array in-place.
VALUE rb_ary_resurrect(VALUE ary)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
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_resurrect(VALUE str)
Like rb_str_dup(), but always create an instance of rb_cString regardless of the given object's class...
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.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
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.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#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.
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.