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))
365 if (!IS_INSN(link)) {
369 enum ruby_vminsn_type
type = INSN_OF(link);
372 type == BIN(putobject) ||
373 type == BIN(putspecialobject) ||
374 type == BIN(putnil) ||
375 type == BIN(putself) ||
376 type == BIN(duphash) ||
377 type == BIN(getinstancevariable) ||
378 type == BIN(getlocal) ||
379 type == BIN(getlocal_WC_0) ||
380 type == BIN(getlocal_WC_1) ||
381 type == BIN(putobject_INT2FIX_0_) ||
382 type == BIN(putobject_INT2FIX_1_) ||
383 type == BIN(opt_getconstant_path)
393append_compile_error(const
rb_iseq_t *iseq,
int line, const
char *fmt, ...)
395 VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
396 VALUE file = rb_iseq_path(iseq);
401 err = rb_syntax_error_append(err, file, line, -1, NULL, fmt, args);
403 if (
NIL_P(err_info)) {
404 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err);
407 else if (!err_info) {
418compile_bug(
rb_iseq_t *iseq,
int line,
const char *fmt, ...)
422 rb_report_bug_valist(rb_iseq_path(iseq), line, fmt, args);
428#define COMPILE_ERROR append_compile_error
430#define ERROR_ARGS_AT(n) iseq, nd_line(n),
431#define ERROR_ARGS ERROR_ARGS_AT(node)
433#define EXPECT_NODE(prefix, node, ndtype, errval) \
435 const NODE *error_node = (node); \
436 enum node_type error_type = nd_type(error_node); \
437 if (error_type != (ndtype)) { \
438 COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
439 prefix ": " #ndtype " is expected, but %s", \
440 ruby_node_name(error_type)); \
445#define EXPECT_NODE_NONULL(prefix, parent, ndtype, errval) \
447 COMPILE_ERROR(ERROR_ARGS_AT(parent) \
448 prefix ": must be " #ndtype ", but 0"); \
452#define UNKNOWN_NODE(prefix, node, errval) \
454 const NODE *error_node = (node); \
455 COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
456 ruby_node_name(nd_type(error_node))); \
463#define CHECK(sub) if (!(sub)) {BEFORE_RETURN;return COMPILE_NG;}
464#define NO_CHECK(sub) (void)(sub)
467#define DECL_ANCHOR(name) \
468 LINK_ANCHOR name[1] = {{{ISEQ_ELEMENT_ANCHOR,},&name[0].anchor}}
469#define INIT_ANCHOR(name) \
470 ((name->last = &name->anchor)->next = NULL)
473freeze_hide_obj(
VALUE obj)
476 RBASIC_CLEAR_CLASS(obj);
480#include "optinsn.inc"
481#if OPT_INSTRUCTIONS_UNIFICATION
482#include "optunifs.inc"
487#define ISEQ_ARG iseq,
488#define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
491#define ISEQ_ARG_DECLARE
495#define gl_node_level ISEQ_COMPILE_DATA(iseq)->node_level
501static int insn_data_length(
INSN *iobj);
502static int calc_sp_depth(
int depth,
INSN *iobj);
504static INSN *new_insn_body(
rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...);
517static int iseq_set_exception_local_table(
rb_iseq_t *iseq);
521static int iseq_set_exception_table(
rb_iseq_t *iseq);
522static int iseq_set_optargs_table(
rb_iseq_t *iseq);
523static int iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq);
526static int compile_hash(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped);
533verify_list(ISEQ_ARG_DECLARE
const char *info,
LINK_ANCHOR *
const anchor)
539 if (!compile_debug)
return;
541 list = anchor->anchor.next;
542 plist = &anchor->anchor;
544 if (plist != list->prev) {
551 if (anchor->last != plist && anchor->last != 0) {
556 rb_bug(
"list verify error: %08x (%s)", flag, info);
561#define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
568 VALUE *original = rb_iseq_original_iseq(iseq);
570 while (i < ISEQ_BODY(iseq)->iseq_size) {
571 VALUE insn = original[i];
572 const char *types = insn_op_types(insn);
574 for (
int j=0; types[j]; j++) {
575 if (types[j] == TS_CALLDATA) {
579 if (cc != vm_cc_empty()) {
581 rb_bug(
"call cache is not initialized by vm_cc_empty()");
588 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
589 struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
592 if (cc != NULL && cc != vm_cc_empty()) {
594 rb_bug(
"call cache is not initialized by vm_cc_empty()");
606 elem->prev = anchor->last;
607 anchor->last->next = elem;
609 verify_list(
"add", anchor);
619 elem->next = before->next;
620 elem->next->prev = elem;
622 if (before == anchor->last) anchor->last = elem;
623 verify_list(
"add", anchor);
626#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
627#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
631branch_coverage_valid_p(
rb_iseq_t *iseq,
int first_line)
633 if (!ISEQ_COVERAGE(iseq))
return 0;
634 if (!ISEQ_BRANCH_COVERAGE(iseq))
return 0;
635 if (first_line <= 0)
return 0;
642 const int first_lineno = loc->beg_pos.lineno, first_column = loc->beg_pos.column;
643 const int last_lineno = loc->end_pos.lineno, last_column = loc->end_pos.column;
646 rb_hash_aset(structure, key, branch);
658 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return Qundef;
671 uint64_t source_hash = ISEQ_BODY(iseq)->source_hash;
672 VALUE key = rb_ary_new_from_args(4,
673 ULONG2NUM((
unsigned long)(source_hash >> 32)),
674 ULONG2NUM((
unsigned long)(source_hash & 0xffffffff)),
680 VALUE branch_base = rb_hash_aref(structure, key);
683 if (
NIL_P(branch_base)) {
684 branch_base = setup_branch(loc,
type, structure, key);
685 branches = rb_hash_new();
697generate_dummy_line_node(
int lineno,
int node_id)
700 nd_set_line(&dummy, lineno);
701 nd_set_node_id(&dummy, node_id);
708 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return;
719 VALUE branch = rb_hash_aref(branches, key);
723 branch = setup_branch(loc,
type, branches, key);
733 ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
734 ADD_SYNTHETIC_INSN(seq, loc->end_pos.lineno, node_id, nop);
737#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
740validate_label(st_data_t name, st_data_t label, st_data_t arg)
744 if (!lobj->link.next) {
746 COMPILE_ERROR(iseq, lobj->position,
747 "%"PRIsVALUE
": undefined label",
757 st_foreach(labels_table, validate_label, (st_data_t)iseq);
758 st_free_table(labels_table);
762get_nd_recv(
const NODE *node)
764 switch (nd_type(node)) {
766 return RNODE_CALL(node)->nd_recv;
768 return RNODE_OPCALL(node)->nd_recv;
772 return RNODE_QCALL(node)->nd_recv;
776 return RNODE_ATTRASGN(node)->nd_recv;
778 return RNODE_OP_ASGN1(node)->nd_recv;
780 return RNODE_OP_ASGN2(node)->nd_recv;
782 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
787get_node_call_nd_mid(
const NODE *node)
789 switch (nd_type(node)) {
791 return RNODE_CALL(node)->nd_mid;
793 return RNODE_OPCALL(node)->nd_mid;
795 return RNODE_FCALL(node)->nd_mid;
797 return RNODE_QCALL(node)->nd_mid;
799 return RNODE_VCALL(node)->nd_mid;
801 return RNODE_ATTRASGN(node)->nd_mid;
803 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
808get_nd_args(
const NODE *node)
810 switch (nd_type(node)) {
812 return RNODE_CALL(node)->nd_args;
814 return RNODE_OPCALL(node)->nd_args;
816 return RNODE_FCALL(node)->nd_args;
818 return RNODE_QCALL(node)->nd_args;
822 return RNODE_ATTRASGN(node)->nd_args;
824 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
829get_node_colon_nd_mid(
const NODE *node)
831 switch (nd_type(node)) {
833 return RNODE_COLON2(node)->nd_mid;
835 return RNODE_COLON3(node)->nd_mid;
837 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
842get_nd_vid(
const NODE *node)
844 switch (nd_type(node)) {
846 return RNODE_LASGN(node)->nd_vid;
848 return RNODE_DASGN(node)->nd_vid;
850 return RNODE_IASGN(node)->nd_vid;
852 return RNODE_CVASGN(node)->nd_vid;
854 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
859get_nd_value(
const NODE *node)
861 switch (nd_type(node)) {
863 return RNODE_LASGN(node)->nd_value;
865 return RNODE_DASGN(node)->nd_value;
867 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
872get_string_value(
const NODE *node)
874 switch (nd_type(node)) {
880 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
890 (*ifunc->func)(iseq, ret, ifunc->data);
892 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
894 CHECK(iseq_setup_insn(iseq, ret));
895 return iseq_setup(iseq, ret);
898static bool drop_unreachable_return(
LINK_ANCHOR *ret);
907 NO_CHECK(COMPILE(ret,
"nil", node));
908 iseq_set_local_table(iseq, 0, 0);
911 else if (nd_type_p(node, NODE_SCOPE)) {
913 iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl, (
NODE *)RNODE_SCOPE(node)->nd_args);
914 iseq_set_arguments(iseq, ret, (
NODE *)RNODE_SCOPE(node)->nd_args);
915 iseq_set_parameters_lvar_state(iseq);
917 switch (ISEQ_BODY(iseq)->
type) {
918 case ISEQ_TYPE_BLOCK:
920 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
921 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
923 start->rescued = LABEL_RESCUE_BEG;
924 end->rescued = LABEL_RESCUE_END;
927 ADD_SYNTHETIC_INSN(ret, ISEQ_BODY(iseq)->location.first_lineno, -1, nop);
928 ADD_LABEL(ret, start);
929 CHECK(COMPILE(ret,
"block body", RNODE_SCOPE(node)->nd_body));
932 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
935 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
936 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
939 case ISEQ_TYPE_CLASS:
942 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
944 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
947 case ISEQ_TYPE_METHOD:
949 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
951 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
952 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
954 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
958 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
965#define INVALID_ISEQ_TYPE(type) \
966 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
967 switch (ISEQ_BODY(iseq)->
type) {
968 case INVALID_ISEQ_TYPE(
METHOD);
969 case INVALID_ISEQ_TYPE(CLASS);
970 case INVALID_ISEQ_TYPE(BLOCK);
971 case INVALID_ISEQ_TYPE(EVAL);
972 case INVALID_ISEQ_TYPE(MAIN);
973 case INVALID_ISEQ_TYPE(TOP);
974#undef INVALID_ISEQ_TYPE
975 case ISEQ_TYPE_RESCUE:
976 iseq_set_exception_local_table(iseq);
977 CHECK(COMPILE(ret,
"rescue", node));
979 case ISEQ_TYPE_ENSURE:
980 iseq_set_exception_local_table(iseq);
981 CHECK(COMPILE_POPPED(ret,
"ensure", node));
983 case ISEQ_TYPE_PLAIN:
984 CHECK(COMPILE(ret,
"ensure", node));
987 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
990 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
995 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
996 NODE dummy_line_node = generate_dummy_line_node(0, -1);
997 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
998 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
1000 else if (!drop_unreachable_return(ret)) {
1001 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
1005 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
1006 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
1007 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
1008 validate_labels(iseq, labels_table);
1011 CHECK(iseq_setup_insn(iseq, ret));
1012 return iseq_setup(iseq, ret);
1016rb_iseq_translate_threaded_code(
rb_iseq_t *iseq)
1018#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1019 const void *
const *table = rb_vm_get_insns_address_table();
1021 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
1023 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1024 int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
1025 int len = insn_len(insn);
1026 encoded[i] = (
VALUE)table[insn];
1033 rb_yjit_live_iseq_count++;
1034 rb_yjit_iseq_alloc_count++;
1041rb_iseq_original_iseq(
const rb_iseq_t *iseq)
1046 if (original_code)
return original_code;
1047 original_code =
ALLOC_N(
VALUE, ISEQ_BODY(iseq)->iseq_size);
1048 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
1050#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1054 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1055 const void *addr = (
const void *)original_code[i];
1056 const int insn = rb_vm_insn_addr2insn(addr);
1058 original_code[i] = insn;
1059 i += insn_len(insn);
1066 v = rb_iseq_variable_ensure((
rb_iseq_t *)iseq);
1067 VALUE *prev = ATOMIC_PTR_CAS(v->original_iseq,
1068 NULL, original_code);
1070 SIZED_FREE_N(original_code, ISEQ_BODY(iseq)->iseq_size);
1073 return original_code;
1080#if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
1081# define ALIGNMENT_SIZE SIZEOF_LONG_LONG
1083# define ALIGNMENT_SIZE SIZEOF_VALUE
1085#define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
1087#define ALIGNMENT_SIZE_OF(type) alignment_size_assert(RUBY_ALIGNOF(type), #type)
1090alignment_size_assert(
size_t align,
const char *
type)
1093 "ALIGNMENT_SIZE_OF(%s):%zd == (2 ** N) is expected",
type, align);
1099calc_padding(
void *ptr,
size_t align)
1104 mis = (size_t)ptr & (align - 1);
1106 padding = align - mis;
1117 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], align);
1119 if (size >= INT_MAX - padding) rb_memerror();
1120 if (storage->pos + size + padding > storage->size) {
1121 unsigned int alloc_size = storage->size;
1123 while (alloc_size < size + PADDING_SIZE_MAX) {
1124 if (alloc_size >= INT_MAX / 2) rb_memerror();
1127 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
1129 storage = *arena = storage->next;
1132 storage->size = alloc_size;
1133 padding = calc_padding((
void *)&storage->buff[storage->pos], align);
1136 storage->pos += (int)padding;
1138 ptr = (
void *)&storage->buff[storage->pos];
1139 storage->pos += (int)size;
1144compile_data_alloc(
rb_iseq_t *iseq,
size_t size,
size_t align)
1147 return compile_data_alloc_with_arena(arena, size, align);
1150#define compile_data_alloc_type(iseq, type) \
1151 (type *)compile_data_alloc(iseq, sizeof(type), ALIGNMENT_SIZE_OF(type))
1154compile_data_alloc2(
rb_iseq_t *iseq,
size_t elsize,
size_t num,
size_t align)
1157 return compile_data_alloc(iseq, size, align);
1160#define compile_data_alloc2_type(iseq, type, num) \
1161 (type *)compile_data_alloc2(iseq, sizeof(type), num, ALIGNMENT_SIZE_OF(type))
1164compile_data_calloc2(
rb_iseq_t *iseq,
size_t elsize,
size_t num,
size_t align)
1167 void *p = compile_data_alloc(iseq, size, align);
1172#define compile_data_calloc2_type(iseq, type, num) \
1173 (type *)compile_data_calloc2(iseq, sizeof(type), num, ALIGNMENT_SIZE_OF(type))
1179 return (
INSN *)compile_data_alloc_with_arena(arena,
sizeof(
INSN), ALIGNMENT_SIZE_OF(
INSN));
1183compile_data_alloc_label(
rb_iseq_t *iseq)
1185 return compile_data_alloc_type(iseq,
LABEL);
1189compile_data_alloc_adjust(
rb_iseq_t *iseq)
1191 return compile_data_alloc_type(iseq,
ADJUST);
1195compile_data_alloc_trace(
rb_iseq_t *iseq)
1197 return compile_data_alloc_type(iseq,
TRACE);
1206 elem2->next = elem1->next;
1207 elem2->prev = elem1;
1208 elem1->next = elem2;
1210 elem2->next->prev = elem2;
1220 elem2->prev = elem1->prev;
1221 elem2->next = elem1;
1222 elem1->prev = elem2;
1224 elem2->prev->next = elem2;
1234 elem2->prev = elem1->prev;
1235 elem2->next = elem1->next;
1237 elem1->prev->next = elem2;
1240 elem1->next->prev = elem2;
1247 elem->prev->next = elem->next;
1249 elem->next->prev = elem->prev;
1262 first->prev->next = second;
1263 second->next->prev = first;
1265 first->next = second->next;
1266 second->next = first;
1268 second->prev = first->prev;
1269 first->prev = second;
1271 if (IS_INSN(first) && IS_INSN(second)) {
1277 if (first_insn->insn_info.line_no == second_insn->insn_info.line_no) {
1281 first_insn->insn_info.events = (first_insn->insn_info.events & ~mask) | second_events;
1282 second_insn->insn_info.events = (second_insn->insn_info.events & ~mask) | first_events;
1290 return anchor->anchor.next;
1296 return anchor->last;
1303 switch (elem->type) {
1304 case ISEQ_ELEMENT_INSN:
1305 case ISEQ_ELEMENT_ADJUST:
1315LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1317 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1318 if (first_insn != NULL &&
1319 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1328LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1330 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1348 if (anc2->anchor.next) {
1351 anc1->last->next = anc2->anchor.next;
1352 anc2->anchor.next->prev = anc1->last;
1353 anc1->last = anc2->last;
1358 verify_list(
"append", anc1);
1361#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1370 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1371 (
void *)anchor->anchor.next, (
void *)anchor->last);
1373 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1374 (
void *)list->prev, (
int)list->type);
1379 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1380 verify_list(
"debug list", anchor);
1383#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1386#define debug_list(anc, cur) ((void)0)
1392 TRACE *trace = compile_data_alloc_trace(iseq);
1394 trace->link.type = ISEQ_ELEMENT_TRACE;
1395 trace->link.next = NULL;
1396 trace->event = event;
1403new_label_body(
rb_iseq_t *iseq,
long line)
1405 LABEL *labelobj = compile_data_alloc_label(iseq);
1407 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1408 labelobj->link.next = 0;
1410 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1411 labelobj->sc_state = 0;
1413 labelobj->refcnt = 0;
1415 labelobj->rescued = LABEL_RESCUE_NONE;
1416 labelobj->unremovable = 0;
1417 labelobj->position = -1;
1424 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1425 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1426 adjust->link.next = 0;
1427 adjust->label = label;
1428 adjust->line_no = line;
1429 LABEL_UNREMOVABLE(label);
1436 const char *types = insn_op_types(insn->insn_id);
1437 for (
int j = 0; types[j]; j++) {
1438 char type = types[j];
1445 func(&OPERAND_AT(insn, j), data);
1454iseq_insn_each_object_write_barrier(
VALUE * obj,
VALUE iseq)
1463new_insn_core(
rb_iseq_t *iseq,
int line_no,
int node_id,
int insn_id,
int argc,
VALUE *argv)
1465 INSN *iobj = compile_data_alloc_insn(iseq);
1469 iobj->link.type = ISEQ_ELEMENT_INSN;
1470 iobj->link.next = 0;
1471 iobj->insn_id = insn_id;
1472 iobj->insn_info.line_no = line_no;
1473 iobj->insn_info.node_id = node_id;
1474 iobj->insn_info.events = 0;
1475 iobj->operands = argv;
1476 iobj->operand_size = argc;
1479 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1485new_insn_body(
rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...)
1487 VALUE *operands = 0;
1491 va_start(argv, argc);
1492 operands = compile_data_alloc2_type(iseq,
VALUE, argc);
1493 for (i = 0; i < argc; i++) {
1499 return new_insn_core(iseq, line_no, node_id, insn_id, argc, operands);
1503insn_replace_with_operands(
rb_iseq_t *iseq,
INSN *iobj,
enum ruby_vminsn_type insn_id,
int argc, ...)
1505 VALUE *operands = 0;
1509 va_start(argv, argc);
1510 operands = compile_data_alloc2_type(iseq,
VALUE, argc);
1511 for (i = 0; i < argc; i++) {
1518 iobj->insn_id = insn_id;
1519 iobj->operand_size = argc;
1520 iobj->operands = operands;
1521 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1529 VM_ASSERT(argc >= 0);
1532 flag |= VM_CALL_KWARG;
1533 argc += kw_arg->keyword_len;
1536 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING))
1537 && !has_blockiseq) {
1538 flag |= VM_CALL_ARGS_SIMPLE;
1541 ISEQ_BODY(iseq)->ci_size++;
1542 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1550 VALUE *operands = compile_data_calloc2_type(iseq,
VALUE, 2);
1553 operands[1] = (
VALUE)blockiseq;
1560 if (vm_ci_flag((
struct rb_callinfo *)ci) & VM_CALL_FORWARDING) {
1561 insn = new_insn_core(iseq, line_no, node_id, BIN(sendforward), 2, operands);
1564 insn = new_insn_core(iseq, line_no, node_id, BIN(send), 2, operands);
1577 VALUE ast_value = rb_ruby_ast_new(node);
1581 if (ISEQ_BODY(iseq)->source_hash) {
1582 rb_ast_t *child_ast = rb_ruby_ast_data_get(ast_value);
1583 child_ast->body.source_hash = ISEQ_BODY(iseq)->source_hash;
1584 child_ast->body.has_source_hash = 1;
1587 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1588 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1589 ret_iseq = rb_iseq_new_with_opt(ast_value, name,
1590 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1592 isolated_depth ? isolated_depth + 1 : 0,
1593 type, ISEQ_COMPILE_DATA(iseq)->option,
1594 ISEQ_SCRIPT_LINES(iseq));
1595 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1605 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1606 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1607 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1608 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1609 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1617 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1618 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1619 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1620 set_catch_except_p((
rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1643 while (pos < body->iseq_size) {
1644 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1645 if (insn == BIN(
throw)) {
1646 set_catch_except_p(iseq);
1649 pos += insn_len(insn);
1655 for (i = 0; i < ct->size; i++) {
1657 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1658 if (entry->type != CATCH_TYPE_BREAK
1659 && entry->type != CATCH_TYPE_NEXT
1660 && entry->type != CATCH_TYPE_REDO) {
1662 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1669iseq_insert_nop_between_end_and_cont(
rb_iseq_t *iseq)
1671 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1672 if (
NIL_P(catch_table_ary))
return;
1673 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1675 for (i = 0; i < tlen; i++) {
1681 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1683 if (ct != CATCH_TYPE_BREAK
1684 && ct != CATCH_TYPE_NEXT
1685 && ct != CATCH_TYPE_REDO) {
1687 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1689 INSN *nop = new_insn_core(iseq, 0, -1, BIN(nop), 0, 0);
1690 ELEM_INSERT_NEXT(end, &nop->link);
1703 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1708 if (compile_debug > 5)
1709 dump_disasm_list(FIRST_ELEMENT(anchor));
1711 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1712 iseq_optimize(iseq, anchor);
1714 if (compile_debug > 5)
1715 dump_disasm_list(FIRST_ELEMENT(anchor));
1717 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1718 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1719 iseq_insns_unification(iseq, anchor);
1720 if (compile_debug > 5)
1721 dump_disasm_list(FIRST_ELEMENT(anchor));
1724 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1725 iseq_insert_nop_between_end_and_cont(iseq);
1726 if (compile_debug > 5)
1727 dump_disasm_list(FIRST_ELEMENT(anchor));
1735 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1738 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1739 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1740 if (compile_debug > 5)
1741 dump_disasm_list(FIRST_ELEMENT(anchor));
1743 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1744 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1746 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1747 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1749 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1750 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1752 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1754 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1756 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1758 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1759 ruby_xfree_sized(ISEQ_BODY(iseq)->catch_table, iseq_catch_table_bytes(ISEQ_BODY(iseq)->catch_table->size));
1760 ISEQ_BODY(iseq)->catch_table = NULL;
1763#if VM_INSN_INFO_TABLE_IMPL == 2
1764 debugs(
"[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
1765 rb_iseq_insns_info_encode_positions(iseq);
1768 if (compile_debug > 1) {
1769 VALUE str = rb_iseq_disasm(iseq);
1772 verify_call_cache(iseq);
1773 debugs(
"[compile step: finish]\n");
1779iseq_set_exception_local_table(
rb_iseq_t *iseq)
1781 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1782 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1783 ISEQ_BODY(iseq)->lvar_states.list = NULL;
1791 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1793 iseq = ISEQ_BODY(iseq)->parent_iseq;
1799get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1803 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1804 if (ISEQ_BODY(iseq)->local_table[i] == id) {
1814 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1817 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1818 "get_local_var_idx: %d", idx);
1825get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1827 int lv = 0, idx = -1;
1828 const rb_iseq_t *
const topmost_iseq = iseq;
1831 idx = get_dyna_var_idx_at_raw(iseq,
id);
1835 iseq = ISEQ_BODY(iseq)->parent_iseq;
1840 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1841 "get_dyna_var_idx: -1");
1845 *ls = ISEQ_BODY(iseq)->local_table_size;
1850iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1854 iseq = ISEQ_BODY(iseq)->parent_iseq;
1857 body = ISEQ_BODY(iseq);
1858 if (body->local_iseq == iseq &&
1859 body->param.flags.has_block &&
1860 body->local_table_size - body->param.block_start == idx) {
1869iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1872 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1873 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1884access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1886 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1888 if (isolated_depth && level >= isolated_depth) {
1889 if (
id == rb_intern(
"yield")) {
1890 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1893 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable '%s' from isolated Proc", rb_id2name(
id));
1897 for (
int i=0; i<level; i++) {
1899 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1902 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1905 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1906 if (write && !val) {
1907 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1911 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1914 iseq = ISEQ_BODY(iseq)->parent_iseq;
1919iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1921 for (
int i=0; i<level; i++) {
1922 iseq = ISEQ_BODY(iseq)->parent_iseq;
1925 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1931update_lvar_state(
const rb_iseq_t *iseq,
int level,
int idx)
1933 for (
int i=0; i<level; i++) {
1934 iseq = ISEQ_BODY(iseq)->parent_iseq;
1937 uint8_t *states = iseq_lvar_states(ISEQ_BODY(iseq));
1938 int table_idx = ISEQ_BODY(iseq)->local_table_size - idx;
1939 switch (iseq_lvar_state_get(states, table_idx)) {
1940 case lvar_uninitialized:
1941 iseq_lvar_state_set(states, table_idx, lvar_initialized);
1943 case lvar_initialized:
1944 iseq_lvar_state_set(states, table_idx, lvar_reassigned);
1946 case lvar_reassigned:
1950 rb_bug(
"unreachable");
1955iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq)
1957 uint8_t *states = iseq_lvar_states(ISEQ_BODY(iseq));
1959 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
1960 iseq_lvar_state_set(states, i, lvar_initialized);
1963 int lead_num = ISEQ_BODY(iseq)->param.lead_num;
1964 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
1965 for (
int i=0; i<opt_num; i++) {
1966 iseq_lvar_state_set(states, lead_num + i, lvar_uninitialized);
1975 if (iseq_local_block_param_p(iseq, idx, level)) {
1976 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1979 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1981 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1987 if (iseq_local_block_param_p(iseq, idx, level)) {
1988 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1991 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1993 update_lvar_state(iseq, level, idx);
1994 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
2003 if (body->param.flags.has_opt ||
2004 body->param.flags.has_post ||
2005 body->param.flags.has_rest ||
2006 body->param.flags.has_block ||
2007 body->param.flags.has_kw ||
2008 body->param.flags.has_kwrest) {
2010 if (body->param.flags.has_block) {
2011 body->param.size = body->param.block_start + 1;
2013 else if (body->param.flags.has_kwrest) {
2014 body->param.size = body->param.keyword->rest_start + 1;
2016 else if (body->param.flags.has_kw) {
2017 body->param.size = body->param.keyword->bits_start + 1;
2019 else if (body->param.flags.has_post) {
2020 body->param.size = body->param.post_start + body->param.post_num;
2022 else if (body->param.flags.has_rest) {
2023 body->param.size = body->param.rest_start + 1;
2025 else if (body->param.flags.has_opt) {
2026 body->param.size = body->param.lead_num + body->param.opt_num;
2033 body->param.size = body->param.lead_num;
2043 struct rb_iseq_param_keyword *keyword;
2046 int kw = 0, rkw = 0, di = 0, i;
2048 body->param.flags.has_kw = TRUE;
2049 body->param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2053 node = node->nd_next;
2055 if (kw > VM_CALL_KW_LEN_MAX) {
2056 COMPILE_ERROR(ERROR_ARGS_AT(RNODE(args->kw_args))
"too many keyword parameters (%d, maximum is %d)",
2057 kw, (
int)VM_CALL_KW_LEN_MAX);
2060 keyword->bits_start = arg_size++;
2062 node = args->kw_args;
2064 const NODE *val_node = get_nd_value(node->nd_body);
2067 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
2071 switch (nd_type(val_node)) {
2073 dv = rb_node_sym_string_val(val_node);
2076 dv = rb_node_regx_string_val(val_node);
2079 dv = rb_node_line_lineno_val(val_node);
2082 dv = rb_node_integer_literal_val(val_node);
2085 dv = rb_node_float_literal_val(val_node);
2088 dv = rb_node_rational_literal_val(val_node);
2090 case NODE_IMAGINARY:
2091 dv = rb_node_imaginary_literal_val(val_node);
2094 dv = rb_node_encoding_val(val_node);
2106 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
2110 keyword->num = ++di;
2114 node = node->nd_next;
2119 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
2120 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2121 keyword->rest_start = arg_size++;
2122 body->param.flags.has_kwrest = TRUE;
2124 if (kw_id == idPow) body->param.flags.anon_kwrest = TRUE;
2126 keyword->required_num = rkw;
2127 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
2132 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
2134 if (dv == complex_mark) dv =
Qundef;
2139 keyword->default_values = dvs;
2148 if (!body->param.flags.use_block) {
2149 body->param.flags.use_block = 1;
2154 st_data_t key = (st_data_t)rb_intern_str(body->location.label);
2155 set_insert(&vm->unused_block_warning_table, key);
2163 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2167 const struct rb_args_info *
const args = &RNODE_ARGS(node_args)->nd_ainfo;
2173 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2175 body->param.lead_num = arg_size = (int)args->pre_args_num;
2176 if (body->param.lead_num > 0) body->param.flags.has_lead = TRUE;
2177 debugs(
" - argc: %d\n", body->param.lead_num);
2179 rest_id = args->rest_arg;
2180 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2184 block_id = args->block_arg;
2186 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2188 if (optimized_forward) {
2193 if (args->opt_args) {
2201 label = NEW_LABEL(nd_line(RNODE(node)));
2203 ADD_LABEL(optargs, label);
2204 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2205 node = node->nd_next;
2210 label = NEW_LABEL(nd_line(node_args));
2212 ADD_LABEL(optargs, label);
2217 for (j = 0; j < i+1; j++) {
2222 body->param.flags.has_opt = TRUE;
2223 body->param.opt_num = i;
2224 body->param.opt_table = opt_table;
2229 body->param.rest_start = arg_size++;
2230 body->param.flags.has_rest = TRUE;
2231 if (rest_id ==
'*') body->param.flags.anon_rest = TRUE;
2235 if (args->first_post_arg) {
2236 body->param.post_start = arg_size;
2237 body->param.post_num = args->post_args_num;
2238 body->param.flags.has_post = TRUE;
2239 arg_size += args->post_args_num;
2241 if (body->param.flags.has_rest) {
2242 body->param.post_start = body->param.rest_start + 1;
2246 if (args->kw_args) {
2247 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2249 else if (args->kw_rest_arg && !optimized_forward) {
2250 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2251 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2252 keyword->rest_start = arg_size++;
2253 body->param.keyword = keyword;
2254 body->param.flags.has_kwrest = TRUE;
2256 static ID anon_kwrest = 0;
2257 if (!anon_kwrest) anon_kwrest = rb_intern(
"**");
2258 if (kw_id == anon_kwrest) body->param.flags.anon_kwrest = TRUE;
2260 else if (args->no_kwarg) {
2261 body->param.flags.accepts_no_kwarg = TRUE;
2264 if (args->no_blockarg) {
2265 body->param.flags.accepts_no_block = TRUE;
2267 else if (block_id) {
2268 body->param.block_start = arg_size++;
2269 body->param.flags.has_block = TRUE;
2270 iseq_set_use_block(iseq);
2274 if (optimized_forward) {
2275 body->param.flags.use_block = 1;
2276 body->param.flags.forwardable = TRUE;
2280 iseq_calc_param_size(iseq);
2281 body->param.size = arg_size;
2283 if (args->pre_init) {
2284 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2286 if (args->post_init) {
2287 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2290 if (body->type == ISEQ_TYPE_BLOCK) {
2291 if (body->param.flags.has_opt == FALSE &&
2292 body->param.flags.has_post == FALSE &&
2293 body->param.flags.has_rest == FALSE &&
2294 body->param.flags.has_kw == FALSE &&
2295 body->param.flags.has_kwrest == FALSE) {
2297 if (body->param.lead_num == 1 && last_comma == 0) {
2299 body->param.flags.ambiguous_param0 = TRUE;
2311 unsigned int size = tbl ? tbl->size : 0;
2312 unsigned int offset = 0;
2315 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2320 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2329 MEMCPY(ids, tbl->ids + offset,
ID, size);
2330 ISEQ_BODY(iseq)->local_table = ids;
2332 if (ISEQ_LVAR_STATES_EMBED_P(size)) {
2334 memset(ISEQ_BODY(iseq)->lvar_states.single, 0,
sizeof(ISEQ_BODY(iseq)->lvar_states.single));
2337 ISEQ_BODY(iseq)->lvar_states.list =
ZALLOC_N(uint8_t, ISEQ_LVAR_STATES_BUFLEN(size));
2340 ISEQ_BODY(iseq)->local_table_size = size;
2342 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2354 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2357 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2360 else if (tlit != tval) {
2370 long x =
FIX2LONG(rb_big_cmp(lit, val));
2378 return rb_float_cmp(lit, val);
2381 const struct RRational *rat1 = RRATIONAL(val);
2382 const struct RRational *rat2 = RRATIONAL(lit);
2383 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2386 const struct RComplex *comp1 = RCOMPLEX(val);
2387 const struct RComplex *comp2 = RCOMPLEX(lit);
2388 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2391 return rb_reg_equal(val, lit) ? 0 : -1;
2399rb_iseq_cdhash_hash(
VALUE a)
2401 switch (OBJ_BUILTIN_TYPE(a)) {
2404 return (st_index_t)a;
2412 return rb_rational_hash(a);
2414 return rb_complex_hash(a);
2424 rb_iseq_cdhash_hash,
2428cdhash_new(
size_t size)
2430 VALUE cdhash = rb_imemo_cdhash_new(size, &cdhash_type);
2438 st_table *tbl = rb_imemo_cdhash_tbl(cdhash);
2439 st_insert(tbl, key, val);
2446 st_table *tbl = rb_imemo_cdhash_tbl(cdhash);
2448 if (!st_lookup(tbl, key, &dontcare)) {
2449 st_insert(tbl, key, val);
2461cdhash_set_label_check_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
2467cdhash_set_label_replace_i(st_data_t *key, st_data_t *value, st_data_t ptr,
int existing)
2471 *value = lobj->position - (data->pos+data->len);
2478 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2485 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2487 if (rb_id_table_lookup(tbl,
id,&val)) {
2492 tbl = rb_id_table_create(1);
2493 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2495 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2496 rb_id_table_insert(tbl,
id,val);
2500#define BADINSN_DUMP(anchor, list, dest) \
2501 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2503#define BADINSN_ERROR \
2504 (SIZED_FREE_N(generated_iseq, generated_iseq_size), \
2505 SIZED_FREE_N(insns_info, insns_info_size), \
2506 BADINSN_DUMP(anchor, list, NULL), \
2512 int stack_max = 0, sp = 0, line = 0;
2515 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2516 if (IS_LABEL(list)) {
2522 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2523 switch (list->type) {
2524 case ISEQ_ELEMENT_INSN:
2532 sp = calc_sp_depth(sp, iobj);
2534 BADINSN_DUMP(anchor, list, NULL);
2535 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2536 "argument stack underflow (%d)", sp);
2539 if (sp > stack_max) {
2543 line = iobj->insn_info.line_no;
2545 operands = iobj->operands;
2546 insn = iobj->insn_id;
2547 types = insn_op_types(insn);
2548 len = insn_len(insn);
2551 if (iobj->operand_size !=
len - 1) {
2553 BADINSN_DUMP(anchor, list, NULL);
2554 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2555 "operand size miss! (%d for %d)",
2556 iobj->operand_size,
len - 1);
2560 for (j = 0; types[j]; j++) {
2561 if (types[j] == TS_OFFSET) {
2565 BADINSN_DUMP(anchor, list, NULL);
2566 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2567 "unknown label: "LABEL_FORMAT, lobj->label_no);
2570 if (lobj->sp == -1) {
2573 else 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_LABEL:
2586 if (lobj->sp == -1) {
2590 if (lobj->sp != sp) {
2591 VALUE path = rb_iseq_path(iseq);
2592 debugs(
"%.*s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2594 lobj->label_no, lobj->sp, sp);
2600 case ISEQ_ELEMENT_TRACE:
2605 case ISEQ_ELEMENT_ADJUST:
2610 sp = adjust->label ? adjust->label->sp : 0;
2611 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2612 BADINSN_DUMP(anchor, list, NULL);
2613 COMPILE_ERROR(iseq, adjust->line_no,
2614 "iseq_set_sequence: adjust bug %d < %d",
2621 BADINSN_DUMP(anchor, list, NULL);
2622 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2631 int insns_info_index,
int code_index,
const INSN *iobj)
2633 if (insns_info_index == 0 ||
2634 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2635#ifdef USE_ISEQ_NODE_ID
2636 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2638 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2639 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2640#ifdef USE_ISEQ_NODE_ID
2641 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2643 insns_info[insns_info_index].events = iobj->insn_info.events;
2644 positions[insns_info_index] = code_index;
2652 int insns_info_index,
int code_index,
const ADJUST *adjust)
2654 insns_info[insns_info_index].line_no = adjust->line_no;
2655 insns_info[insns_info_index].node_id = -1;
2656 insns_info[insns_info_index].events = 0;
2657 positions[insns_info_index] = code_index;
2662array_to_idlist(
VALUE arr)
2667 for (
long i = 0; i < size; i++) {
2676idlist_to_array(
const ID *ids)
2693 unsigned int *positions;
2695 VALUE *generated_iseq;
2699 int insn_num, code_index, insns_info_index, sp = 0;
2700 int stack_max = fix_sp_depth(iseq, anchor);
2702 if (stack_max < 0)
return COMPILE_NG;
2705 insn_num = code_index = 0;
2706 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2707 switch (list->type) {
2708 case ISEQ_ELEMENT_INSN:
2712 sp = calc_sp_depth(sp, iobj);
2714 events = iobj->insn_info.events |= events;
2715 if (ISEQ_COVERAGE(iseq)) {
2716 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2717 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2718 int line = iobj->insn_info.line_no - 1;
2719 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2723 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2724 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2730 code_index += insn_data_length(iobj);
2735 case ISEQ_ELEMENT_LABEL:
2738 lobj->position = code_index;
2739 if (lobj->sp != sp) {
2740 VALUE path = rb_iseq_path(iseq);
2741 debugs(
"%.*s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2743 lobj->label_no, lobj->sp, sp);
2748 case ISEQ_ELEMENT_TRACE:
2751 events |= trace->event;
2752 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2755 case ISEQ_ELEMENT_ADJUST:
2758 if (adjust->line_no != -1) {
2760 sp = adjust->label ? adjust->label->sp : 0;
2761 if (orig_sp - sp > 0) {
2762 if (orig_sp - sp > 1) code_index++;
2774 const int generated_iseq_size = code_index;
2777 const int insns_info_size = insn_num;
2780 const int positions_size = insn_num;
2781 positions =
ALLOC_N(
unsigned int, insn_num);
2782 if (ISEQ_IS_SIZE(body)) {
2786 body->is_entries = NULL;
2789 if (body->ci_size) {
2793 body->call_data = NULL;
2795 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2802 iseq_bits_t * mark_offset_bits;
2803 int code_size = code_index;
2805 bool needs_bitmap =
false;
2807 const size_t mark_offset_bits_size = ISEQ_MBITS_BUFLEN(code_index);
2808 if (mark_offset_bits_size == 1) {
2809 mark_offset_bits = &ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2810 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
true;
2813 mark_offset_bits =
ZALLOC_N(iseq_bits_t, mark_offset_bits_size);
2814 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = mark_offset_bits;
2815 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
false;
2818 ISEQ_COMPILE_DATA(iseq)->iseq_encoded = (
void *)generated_iseq;
2819 ISEQ_COMPILE_DATA(iseq)->iseq_size = code_index;
2821 list = FIRST_ELEMENT(anchor);
2822 insns_info_index = code_index = sp = 0;
2825 switch (list->type) {
2826 case ISEQ_ELEMENT_INSN:
2834 sp = calc_sp_depth(sp, iobj);
2836 operands = iobj->operands;
2837 insn = iobj->insn_id;
2838 generated_iseq[code_index] = insn;
2839 types = insn_op_types(insn);
2840 len = insn_len(insn);
2842 for (j = 0; types[j]; j++) {
2843 char type = types[j];
2851 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2856 VALUE map = operands[j];
2859 data.pos = code_index;
2861 st_foreach_with_replace(rb_imemo_cdhash_tbl(map), cdhash_set_label_check_i, cdhash_set_label_replace_i, (
VALUE)&data);
2863 generated_iseq[code_index + 1 + j] = map;
2864 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2866 needs_bitmap =
true;
2871 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2876 VALUE v = operands[j];
2877 generated_iseq[code_index + 1 + j] = v;
2881 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2882 needs_bitmap =
true;
2889 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2890 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2891 if (UNLIKELY(ic_index >= body->ic_size)) {
2892 BADINSN_DUMP(anchor, &iobj->link, 0);
2893 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2894 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2895 ic_index, ISEQ_IS_SIZE(body));
2898 ic->
segments = array_to_idlist(operands[j]);
2900 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2905 unsigned int ic_index =
FIX2UINT(operands[j]);
2907 IVC cache = ((
IVC)&body->is_entries[ic_index]);
2909 if (insn == BIN(setinstancevariable)) {
2910 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2911 cache->value = IVAR_CACHE_INIT;
2914 cache->iv_set_name = 0;
2915 cache->value = rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET);
2921 unsigned int ic_index =
FIX2UINT(operands[j]);
2922 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2923 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2924 BADINSN_DUMP(anchor, &iobj->link, 0);
2925 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2926 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2927 ic_index, ISEQ_IS_SIZE(body));
2929 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2936 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2937 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2939 cd->cc = vm_cc_empty();
2940 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2944 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2947 generated_iseq[code_index + 1 + j] = operands[j];
2950 generated_iseq[code_index + 1 + j] = operands[j];
2953 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2954 "unknown operand type: %c",
type);
2958 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2962 case ISEQ_ELEMENT_LABEL:
2965 if (lobj->sp != sp) {
2966 VALUE path = rb_iseq_path(iseq);
2967 debugs(
"%.*s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2969 lobj->label_no, lobj->sp, sp);
2974 case ISEQ_ELEMENT_ADJUST:
2979 if (adjust->label) {
2980 sp = adjust->label->sp;
2986 if (adjust->line_no != -1) {
2987 const int diff = orig_sp - sp;
2989 if (insns_info_index == 0) {
2990 COMPILE_ERROR(iseq, adjust->line_no,
2991 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2993 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2996 generated_iseq[code_index++] = BIN(adjuststack);
2997 generated_iseq[code_index++] = orig_sp - sp;
2999 else if (diff == 1) {
3000 generated_iseq[code_index++] = BIN(pop);
3002 else if (diff < 0) {
3003 int label_no = adjust->label ? adjust->label->label_no : -1;
3004 SIZED_FREE_N(generated_iseq, generated_iseq_size);
3005 SIZED_FREE_N(insns_info, insns_info_size);
3006 SIZED_FREE_N(positions, positions_size);
3007 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
3008 SIZED_FREE_N(mark_offset_bits, ISEQ_MBITS_BUFLEN(code_index));
3010 debug_list(anchor, list);
3011 COMPILE_ERROR(iseq, adjust->line_no,
3012 "iseq_set_sequence: adjust bug to %d %d < %d",
3013 label_no, orig_sp, sp);
3026 body->iseq_encoded = (
void *)generated_iseq;
3027 body->iseq_size = code_index;
3028 body->stack_max = stack_max;
3030 if (ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit) {
3031 body->mark_bits.single = ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
3035 body->mark_bits.list = mark_offset_bits;
3038 body->mark_bits.list = NULL;
3039 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = NULL;
3040 SIZED_FREE_N(mark_offset_bits, mark_offset_bits_size);
3045 body->insns_info.body = insns_info;
3046 body->insns_info.positions_or_succ_index_table.positions = positions;
3049 body->insns_info.body = insns_info;
3050 SIZED_REALLOC_N(positions,
unsigned int, insns_info_index, positions_size);
3051 body->insns_info.positions_or_succ_index_table.positions = positions;
3052 body->insns_info.size = insns_info_index;
3058label_get_position(
LABEL *lobj)
3060 return lobj->position;
3064label_get_sp(
LABEL *lobj)
3070iseq_set_exception_table(
rb_iseq_t *iseq)
3072 const VALUE *tptr, *ptr;
3073 unsigned int tlen, i;
3076 ISEQ_BODY(iseq)->catch_table = NULL;
3078 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
3079 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
3087 for (i = 0; i < table->size; i++) {
3090 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
3091 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
3092 pos = label_get_position((
LABEL *)(ptr[1] & ~1));
3094 entry->start = (
unsigned int)pos;
3095 pos = label_get_position((
LABEL *)(ptr[2] & ~1));
3097 entry->end = (
unsigned int)pos;
3104 entry->cont = label_get_position(lobj);
3105 entry->sp = label_get_sp(lobj);
3108 if (entry->type == CATCH_TYPE_RESCUE ||
3109 entry->type == CATCH_TYPE_BREAK ||
3110 entry->type == CATCH_TYPE_NEXT) {
3119 ISEQ_BODY(iseq)->catch_table = table;
3120 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
3141 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
3143 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
3144 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
3145 opt_table[i] = label_get_position((
LABEL *)opt_table[i]);
3152get_destination_insn(
INSN *iobj)
3158 list = lobj->link.next;
3160 switch (list->type) {
3161 case ISEQ_ELEMENT_INSN:
3162 case ISEQ_ELEMENT_ADJUST:
3164 case ISEQ_ELEMENT_LABEL:
3167 case ISEQ_ELEMENT_TRACE:
3170 events |= trace->event;
3178 if (list && IS_INSN(list)) {
3180 iobj->insn_info.events |= events;
3186get_next_insn(
INSN *iobj)
3191 if (IS_INSN(list) || IS_ADJUST(list)) {
3200get_prev_insn(
INSN *iobj)
3205 if (IS_INSN(list) || IS_ADJUST(list)) {
3214unref_destination(
INSN *iobj,
int pos)
3216 LABEL *lobj = (
LABEL *)OPERAND_AT(iobj, pos);
3218 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3222replace_destination(
INSN *dobj,
INSN *nobj)
3224 VALUE n = OPERAND_AT(nobj, 0);
3227 if (dl == nl)
return false;
3230 OPERAND_AT(dobj, 0) = n;
3231 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3236find_destination(
INSN *i)
3238 int pos,
len = insn_len(i->insn_id);
3239 for (pos = 0; pos <
len; ++pos) {
3240 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3241 return (
LABEL *)OPERAND_AT(i, pos);
3250 LINK_ELEMENT *first = i, *end, *scan, *pending_end = 0;
3252 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
3255 unref_counts =
ALLOCA_N(
int, nlabels);
3256 MEMZERO(unref_counts,
int, nlabels);
3262 if (IS_INSN(scan)) {
3263 if (IS_INSN_ID(scan, leave)) {
3268 else if ((lab = find_destination((
INSN *)scan)) != 0) {
3269 unref_counts[lab->label_no]++;
3270 if (lab == pending && lab->refcnt <= unref_counts[lab->label_no]) {
3275 else if (IS_LABEL(scan)) {
3276 lab = (
LABEL *)scan;
3277 if (lab->unremovable) {
3281 if (lab->refcnt > unref_counts[lab->label_no]) {
3284 pending_end = (scan == first) ? 0 : end;
3288 else if (IS_ADJUST(scan)) {
3292 if (!pending) end = scan;
3293 }
while ((scan = scan->next) != 0);
3296 if (!pending_end)
return 0;
3303 VALUE insn = INSN_OF(i);
3304 int pos,
len = insn_len(insn);
3305 for (pos = 0; pos <
len; ++pos) {
3306 switch (insn_op_types(insn)[pos]) {
3308 unref_destination((
INSN *)i, pos);
3317 }
while ((i != end) && (i = i->next) != 0);
3324 switch (OPERAND_AT(iobj, 0)) {
3326 ELEM_REMOVE(&iobj->link);
3329 ELEM_REMOVE(&iobj->link);
3332 iobj->insn_id = BIN(adjuststack);
3338is_frozen_putstring(
INSN *insn,
VALUE *op)
3340 if (IS_INSN_ID(insn, dupstring) || IS_INSN_ID(insn, dupchilledstring)) {
3341 *op = OPERAND_AT(insn, 0);
3344 else if (IS_INSN_ID(insn, putobject)) {
3345 *op = OPERAND_AT(insn, 0);
3356 if (prev->type == ISEQ_ELEMENT_LABEL) {
3358 if (label->refcnt > 0) {
3362 else if (prev->type == ISEQ_ELEMENT_INSN) {
3395 INSN *niobj, *ciobj, *dup = 0;
3399 switch (INSN_OF(iobj)) {
3400 case BIN(dupstring):
3401 case BIN(dupchilledstring):
3407 case BIN(putobject):
3410 default:
return FALSE;
3413 ciobj = (
INSN *)get_next_insn(iobj);
3414 if (IS_INSN_ID(ciobj, jump)) {
3415 ciobj = (
INSN *)get_next_insn((
INSN*)OPERAND_AT(ciobj, 0));
3417 if (IS_INSN_ID(ciobj, dup)) {
3418 ciobj = (
INSN *)get_next_insn(dup = ciobj);
3420 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3421 niobj = (
INSN *)get_next_insn(ciobj);
3426 switch (INSN_OF(niobj)) {
3428 if (OPERAND_AT(ciobj, 0) ==
type) {
3429 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3432 case BIN(branchunless):
3433 if (OPERAND_AT(ciobj, 0) !=
type) {
3434 dest = (
LABEL *)OPERAND_AT(niobj, 0);
3440 line = ciobj->insn_info.line_no;
3441 node_id = ciobj->insn_info.node_id;
3443 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3444 dest = (
LABEL *)niobj->link.next;
3447 dest = NEW_LABEL(line);
3448 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3451 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3453 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3460 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3461 vm_ci_flag(ci) | add,
3471 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3479#define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3482iseq_reg_compile(
rb_iseq_t *iseq,
VALUE str,
int options,
const char *sourcefile,
int sourceline)
3484 VALUE errinfo = rb_errinfo();
3485 VALUE re = rb_reg_compile(str, options, sourcefile, sourceline);
3487 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
3488 rb_set_errinfo(errinfo);
3489 COMPILE_ERROR(iseq, sourceline,
"%" PRIsVALUE, message);
3503 optimize_checktype(iseq, iobj);
3505 if (IS_INSN_ID(iobj, jump)) {
3506 INSN *niobj, *diobj, *piobj;
3507 diobj = (
INSN *)get_destination_insn(iobj);
3508 niobj = (
INSN *)get_next_insn(iobj);
3510 if (diobj == niobj) {
3517 unref_destination(iobj, 0);
3518 ELEM_REMOVE(&iobj->link);
3521 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3522 IS_INSN_ID(diobj, jump) &&
3523 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3524 diobj->insn_info.events == 0) {
3535 if (replace_destination(iobj, diobj)) {
3536 remove_unreachable_chunk(iseq, iobj->link.next);
3540 else if (IS_INSN_ID(diobj, leave)) {
3553 unref_destination(iobj, 0);
3554 iobj->insn_id = BIN(leave);
3555 iobj->operand_size = 0;
3556 iobj->insn_info = diobj->insn_info;
3559 else if (IS_INSN(iobj->link.prev) &&
3560 (piobj = (
INSN *)iobj->link.prev) &&
3561 (IS_INSN_ID(piobj, branchif) ||
3562 IS_INSN_ID(piobj, branchunless))) {
3563 INSN *pdiobj = (
INSN *)get_destination_insn(piobj);
3564 if (niobj == pdiobj) {
3565 int refcnt = IS_LABEL(piobj->link.next) ?
3566 ((
LABEL *)piobj->link.next)->refcnt : 0;
3581 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3582 ? BIN(branchunless) : BIN(branchif);
3583 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3584 ELEM_REMOVE(&iobj->link);
3591 else if (diobj == pdiobj) {
3605 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3606 ELEM_REPLACE(&piobj->link, &popiobj->link);
3609 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3623 if (IS_INSN_ID(iobj, newrange)) {
3624 INSN *
const range = iobj;
3626 VALUE str_beg, str_end;
3628 if ((end = (
INSN *)get_prev_insn(range)) != 0 &&
3629 is_frozen_putstring(end, &str_end) &&
3630 (beg = (
INSN *)get_prev_insn(end)) != 0 &&
3631 is_frozen_putstring(beg, &str_beg) &&
3632 !(insn_has_label_before(&beg->link) || insn_has_label_before(&end->link))) {
3633 int excl =
FIX2INT(OPERAND_AT(range, 0));
3636 ELEM_REMOVE(&beg->link);
3637 ELEM_REMOVE(&end->link);
3638 range->insn_id = BIN(putobject);
3639 OPERAND_AT(range, 0) = lit_range;
3644 if (IS_INSN_ID(iobj, leave)) {
3645 remove_unreachable_chunk(iseq, iobj->link.next);
3657 if (IS_INSN_ID(iobj, duparray)) {
3659 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3660 iobj->insn_id = BIN(putobject);
3670 if (IS_INSN_ID(iobj, duparray)) {
3672 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3676 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3677 VALUE ary = iobj->operands[0];
3680 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, ary, (
VALUE)ci);
3692 if (IS_INSN_ID(iobj, duphash)) {
3694 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3698 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3699 VALUE hash = iobj->operands[0];
3703 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, hash, (
VALUE)ci);
3715 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3717 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3721 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3722 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, rb_cArray_empty_frozen, (
VALUE)ci);
3734 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3736 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3740 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3741 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, rb_cHash_empty_frozen, (
VALUE)ci);
3747 if (IS_INSN_ID(iobj, branchif) ||
3748 IS_INSN_ID(iobj, branchnil) ||
3749 IS_INSN_ID(iobj, branchunless)) {
3758 INSN *nobj = (
INSN *)get_destination_insn(iobj);
3773 int stop_optimization =
3774 nobj->link.type == ISEQ_ELEMENT_INSN &&
3775 (nobj->insn_info.events & (
RUBY_EVENT_LINE | RUBY_EVENT_COVERAGE_LINE));
3776 if (!stop_optimization) {
3777 INSN *pobj = (
INSN *)iobj->link.prev;
3780 if (!IS_INSN(&pobj->link))
3782 else if (IS_INSN_ID(pobj, dup))
3787 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3788 if (!replace_destination(iobj, nobj))
break;
3790 else if (prev_dup && IS_INSN(&nobj->link) && IS_INSN_ID(nobj, dup) &&
3791 !!(nobj = (
INSN *)nobj->link.next) &&
3792 IS_INSN(&nobj->link) &&
3794 nobj->insn_id == iobj->insn_id) {
3810 if (!replace_destination(iobj, nobj))
break;
3838 if (prev_dup && IS_INSN(pobj->link.prev)) {
3839 pobj = (
INSN *)pobj->link.prev;
3841 if (IS_INSN_ID(pobj, putobject)) {
3842 cond = (IS_INSN_ID(iobj, branchif) ?
3843 OPERAND_AT(pobj, 0) !=
Qfalse :
3844 IS_INSN_ID(iobj, branchunless) ?
3845 OPERAND_AT(pobj, 0) ==
Qfalse :
3848 else if (IS_INSN_ID(pobj, dupstring) ||
3849 IS_INSN_ID(pobj, duparray) ||
3850 IS_INSN_ID(pobj, newarray)) {
3851 cond = IS_INSN_ID(iobj, branchif);
3853 else if (IS_INSN_ID(pobj, putnil)) {
3854 cond = !IS_INSN_ID(iobj, branchif);
3857 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3858 ELEM_REMOVE(iobj->link.prev);
3860 else if (!iseq_pop_newarray(iseq, pobj)) {
3861 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3862 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3866 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3867 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3869 iobj->insn_id = BIN(jump);
3873 unref_destination(iobj, 0);
3874 ELEM_REMOVE(&iobj->link);
3881 if (!dest || !IS_INSN(dest))
break;
3882 nobj = (
INSN *)dest;
3888 if (IS_INSN_ID(iobj, pop)) {
3896 if (IS_INSN(prev)) {
3897 enum ruby_vminsn_type previ = ((
INSN *)prev)->insn_id;
3898 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3899 previ == BIN(putself) || previ == BIN(dupstring) ||
3900 previ == BIN(dupchilledstring) ||
3901 previ == BIN(dup) ||
3902 previ == BIN(getlocal) ||
3903 previ == BIN(getblockparam) ||
3904 previ == BIN(getblockparamproxy) ||
3905 previ == BIN(getinstancevariable) ||
3906 previ == BIN(duparray)) {
3910 ELEM_REMOVE(&iobj->link);
3912 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (
INSN*)prev)) {
3913 ELEM_REMOVE(&iobj->link);
3915 else if (previ == BIN(concatarray)) {
3917 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3918 INSN_OF(piobj) = BIN(pop);
3920 else if (previ == BIN(concatstrings)) {
3921 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3925 ELEM_REMOVE(&iobj->link);
3926 INSN_OF(prev) = BIN(adjuststack);
3932 if (IS_INSN_ID(iobj, newarray) ||
3933 IS_INSN_ID(iobj, duparray) ||
3934 IS_INSN_ID(iobj, concatarray) ||
3935 IS_INSN_ID(iobj, splatarray) ||
3945 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3951 if (IS_INSN_ID(iobj, newarray)) {
3953 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3954 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3956 op1 = OPERAND_AT(iobj, 0);
3957 op2 = OPERAND_AT(next, 0);
3968 INSN_OF(iobj) = BIN(swap);
3969 iobj->operand_size = 0;
3978 INSN_OF(iobj) = BIN(opt_reverse);
3983 INSN_OF(iobj) = BIN(opt_reverse);
3984 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3994 for (; diff > 0; diff--) {
3995 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
4006 for (; diff < 0; diff++) {
4007 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
4014 if (IS_INSN_ID(iobj, duparray)) {
4023 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
4024 INSN_OF(iobj) = BIN(putobject);
4028 if (IS_INSN_ID(iobj, anytostring)) {
4036 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
4037 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
4042 if (IS_INSN_ID(iobj, dupstring) || IS_INSN_ID(iobj, dupchilledstring) ||
4050 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
4051 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
4052 INSN *next = (
INSN *)iobj->link.next;
4053 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
4054 ELEM_REMOVE(&next->link);
4056 ELEM_REMOVE(&iobj->link);
4058 if (IS_NEXT_INSN_ID(&iobj->link, toregexp)) {
4059 INSN *next = (
INSN *)iobj->link.next;
4060 if (OPERAND_AT(next, 1) ==
INT2FIX(1)) {
4061 VALUE src = OPERAND_AT(iobj, 0);
4062 int opt = (int)
FIX2LONG(OPERAND_AT(next, 0));
4063 VALUE path = rb_iseq_path(iseq);
4064 int line = iobj->insn_info.line_no;
4065 VALUE re = iseq_reg_compile(iseq, src, opt, RSTRING_PTR(path), line);
4070 iobj->insn_id = BIN(putobject);
4072 ELEM_REMOVE(iobj->link.next);
4077 if (IS_INSN_ID(iobj, concatstrings)) {
4086 if (IS_INSN(next) && IS_INSN_ID(next, jump))
4087 next = get_destination_insn(jump = (
INSN *)next);
4088 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
4089 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
4090 OPERAND_AT(iobj, 0) =
INT2FIX(n);
4092 LABEL *label = ((
LABEL *)OPERAND_AT(jump, 0));
4093 if (!--label->refcnt) {
4094 ELEM_REMOVE(&label->link);
4097 label = NEW_LABEL(0);
4098 OPERAND_AT(jump, 0) = (
VALUE)label;
4101 ELEM_INSERT_NEXT(next, &label->link);
4102 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
4110 if (do_tailcallopt &&
4111 (IS_INSN_ID(iobj, send) ||
4112 IS_INSN_ID(iobj, invokesuper))) {
4121 if (iobj->link.next) {
4124 if (!IS_INSN(next)) {
4128 switch (INSN_OF(next)) {
4137 next = get_destination_insn((
INSN *)next);
4151 if (IS_INSN_ID(piobj, send) ||
4152 IS_INSN_ID(piobj, invokesuper)) {
4153 if (OPERAND_AT(piobj, 1) == 0) {
4154 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4155 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4160 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4161 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4167 if (IS_INSN_ID(iobj, dup)) {
4168 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
4179 if (IS_NEXT_INSN_ID(set1, setlocal)) {
4181 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4182 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4184 ELEM_REMOVE(&iobj->link);
4197 else if (IS_NEXT_INSN_ID(set1, dup) &&
4198 IS_NEXT_INSN_ID(set1->next, setlocal)) {
4199 set2 = set1->next->next;
4200 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4201 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4202 ELEM_REMOVE(set1->next);
4216 if (IS_INSN_ID(iobj, getlocal)) {
4218 if (IS_NEXT_INSN_ID(niobj, dup)) {
4219 niobj = niobj->next;
4221 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
4223 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
4224 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
4240 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
4241 if (IS_TRACE(iobj->link.next)) {
4242 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
4243 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
4245 if (iobj == (
INSN *)list && bf->argc == 0 && (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF)) {
4246 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4259 if (IS_INSN_ID(iobj, getblockparam)) {
4260 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4261 iobj->insn_id = BIN(getblockparamproxy);
4265 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4267 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4268 niobj = niobj->next;
4270 unsigned int set_flags = 0, unset_flags = 0;
4283 if (IS_NEXT_INSN_ID(niobj, send)) {
4284 siobj = niobj->next;
4285 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4286 unset_flags = VM_CALL_ARGS_BLOCKARG;
4301 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4302 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4303 siobj = niobj->next->next;
4304 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4309 unsigned int flags = vm_ci_flag(ci);
4310 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4311 ((
INSN*)niobj)->insn_id = BIN(putobject);
4314 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4315 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4317 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4331 if (IS_NEXT_INSN_ID(&iobj->link, swap)) {
4336 if (IS_INDEPENDENT_INSN(first) && IS_INDEPENDENT_INSN(second)) {
4338 ELEM_SWAP(first, second);
4346insn_set_specialized_instruction(
rb_iseq_t *iseq,
INSN *iobj,
int insn_id)
4348 if (insn_id == BIN(opt_neq)) {
4349 VALUE original_ci = iobj->operands[0];
4350 VALUE new_ci = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4351 insn_replace_with_operands(iseq, iobj, insn_id, 2, new_ci, original_ci);
4354 iobj->insn_id = insn_id;
4355 iobj->operand_size = insn_len(insn_id) - 1;
4365 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4366 IS_INSN(iobj->link.next)) {
4370 INSN *niobj = (
INSN *)iobj->link.next;
4371 if (IS_INSN_ID(niobj, send)) {
4373 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4375 switch (vm_ci_mid(ci)) {
4377 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4380 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4383 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4388 VALUE num = iobj->operands[0];
4389 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, num, method);
4390 ELEM_REMOVE(&niobj->link);
4395 else if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4397 IS_NEXT_INSN_ID(&niobj->link, send)) {
4399 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4400 VALUE num = iobj->operands[0];
4401 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK));
4402 ELEM_REMOVE(&iobj->link);
4403 ELEM_REMOVE(niobj->link.next);
4404 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4410 else if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4412 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4413 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4416 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4417 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] ==
rb_id2sym(idBuffer))) {
4418 VALUE num = iobj->operands[0];
4419 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 2),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER));
4421 ELEM_REMOVE((niobj->link.next)->next);
4423 ELEM_REMOVE(&iobj->link);
4425 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4433 if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4434 IS_INSN_ID(niobj, putobject) ||
4435 IS_INSN_ID(niobj, putself) ||
4436 IS_INSN_ID(niobj, getlocal) ||
4437 IS_INSN_ID(niobj, getinstancevariable)) &&
4438 IS_NEXT_INSN_ID(&niobj->link, send)) {
4445 sendobj = sendobj->next;
4446 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4447 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4450 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4451 VALUE num = iobj->operands[0];
4453 insn_replace_with_operands(iseq, sendins, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P));
4455 ELEM_REMOVE(&iobj->link);
4469 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4470 INSN *niobj = (
INSN *)iobj->link.next;
4471 if ((IS_INSN_ID(niobj, getlocal) ||
4472 IS_INSN_ID(niobj, getinstancevariable) ||
4473 IS_INSN_ID(niobj, putself) ||
4474 IS_INSN_ID(niobj, putobject) ||
4475 IS_INSN_ID(niobj, putnil)) &&
4476 IS_NEXT_INSN_ID(&niobj->link, send)) {
4483 sendobj = sendobj->next;
4484 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4485 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4487 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4489 VALUE ary = iobj->operands[0];
4493 insn_replace_with_operands(iseq, sendins, BIN(opt_duparray_send), 3, ary,
rb_id2sym(idIncludeP),
INT2FIX(1));
4496 ELEM_REMOVE(&iobj->link);
4503 if (IS_INSN_ID(iobj, send)) {
4507#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4508 if (vm_ci_simple(ci)) {
4509 switch (vm_ci_argc(ci)) {
4511 switch (vm_ci_mid(ci)) {
4512 case idLength: SP_INSN(length);
return COMPILE_OK;
4513 case idSize: SP_INSN(size);
return COMPILE_OK;
4514 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4515 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4516 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4517 case idNot: SP_INSN(not);
return COMPILE_OK;
4521 switch (vm_ci_mid(ci)) {
4522 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4523 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4524 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4525 case idDIV: SP_INSN(div);
return COMPILE_OK;
4526 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4527 case idEq: SP_INSN(eq);
return COMPILE_OK;
4528 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4529 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4530 case idLT: SP_INSN(lt);
return COMPILE_OK;
4531 case idLE: SP_INSN(le);
return COMPILE_OK;
4532 case idGT: SP_INSN(gt);
return COMPILE_OK;
4533 case idGE: SP_INSN(ge);
return COMPILE_OK;
4534 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4535 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4536 case idAnd: SP_INSN(and);
return COMPILE_OK;
4537 case idOr: SP_INSN(or);
return COMPILE_OK;
4541 switch (vm_ci_mid(ci)) {
4542 case idASET: SP_INSN(aset);
return COMPILE_OK;
4548 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4549 iobj->insn_id = BIN(opt_send_without_block);
4550 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4561 switch (ISEQ_BODY(iseq)->
type) {
4563 case ISEQ_TYPE_EVAL:
4564 case ISEQ_TYPE_MAIN:
4566 case ISEQ_TYPE_RESCUE:
4567 case ISEQ_TYPE_ENSURE:
4579 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4580 const int do_tailcallopt = tailcallable_p(iseq) &&
4581 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4582 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4583 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4584 const int do_without_ints = ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_WITHOUT_INTERRUPTS;
4585 int rescue_level = 0;
4586 int tailcallopt = do_tailcallopt;
4588 list = FIRST_ELEMENT(anchor);
4590 int do_block_optimization = 0;
4591 LABEL * block_loop_label = NULL;
4594 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
4595 do_block_optimization = 1;
4600 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop) && IS_LABEL(le->next)) {
4601 block_loop_label = (
LABEL *)le->next;
4606 if (IS_INSN(list)) {
4607 if (do_peepholeopt) {
4608 iseq_peephole_optimize(iseq, list, tailcallopt);
4611 iseq_specialized_instruction(iseq, (
INSN *)list);
4614 insn_operands_unification((
INSN *)list);
4617 if (do_without_ints) {
4619 if (IS_INSN_ID(item, jump)) {
4620 item->insn_id = BIN(jump_without_ints);
4622 else if (IS_INSN_ID(item, branchif)) {
4623 item->insn_id = BIN(branchif_without_ints);
4625 else if (IS_INSN_ID(item, branchunless)) {
4626 item->insn_id = BIN(branchunless_without_ints);
4628 else if (IS_INSN_ID(item, branchnil)) {
4629 item->insn_id = BIN(branchnil_without_ints);
4633 if (do_block_optimization) {
4636 if (IS_INSN_ID(item,
throw)) {
4637 do_block_optimization = 0;
4642 const char *types = insn_op_types(item->insn_id);
4643 for (
int j = 0; types[j]; j++) {
4644 if (types[j] == TS_OFFSET) {
4649 LABEL * target = (
LABEL *)OPERAND_AT(item, j);
4650 if (target == block_loop_label) {
4651 do_block_optimization = 0;
4658 if (IS_LABEL(list)) {
4659 switch (((
LABEL *)list)->rescued) {
4660 case LABEL_RESCUE_BEG:
4662 tailcallopt = FALSE;
4664 case LABEL_RESCUE_END:
4665 if (!--rescue_level) tailcallopt = do_tailcallopt;
4672 if (do_block_optimization) {
4674 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop)) {
4681#if OPT_INSTRUCTIONS_UNIFICATION
4689 VALUE *operands = 0, *ptr = 0;
4693 for (i = 0; i < size; i++) {
4694 iobj = (
INSN *)list;
4695 argc += iobj->operand_size;
4700 ptr = operands = compile_data_alloc2_type(iseq,
VALUE, argc);
4705 for (i = 0; i < size; i++) {
4706 iobj = (
INSN *)list;
4707 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4708 ptr += iobj->operand_size;
4712 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4724#if OPT_INSTRUCTIONS_UNIFICATION
4730 list = FIRST_ELEMENT(anchor);
4732 if (IS_INSN(list)) {
4733 iobj = (
INSN *)list;
4735 if (unified_insns_data[
id] != 0) {
4736 const int *
const *entry = unified_insns_data[id];
4737 for (j = 1; j < (intptr_t)entry[0]; j++) {
4738 const int *unified = entry[j];
4740 for (k = 2; k < unified[1]; k++) {
4742 ((
INSN *)li)->insn_id != unified[k]) {
4749 new_unified_insn(iseq, unified[0], unified[1] - 1,
4754 niobj->link.next = li;
4773all_string_result_p(
const NODE *node)
4775 if (!node)
return FALSE;
4776 switch (nd_type(node)) {
4777 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4779 case NODE_IF:
case NODE_UNLESS:
4780 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4781 if (all_string_result_p(RNODE_IF(node)->nd_body))
4782 return all_string_result_p(RNODE_IF(node)->nd_else);
4784 case NODE_AND:
case NODE_OR:
4785 if (!RNODE_AND(node)->nd_2nd)
4786 return all_string_result_p(RNODE_AND(node)->nd_1st);
4787 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4789 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4799 const NODE *lit_node;
4807 VALUE s = rb_str_new_mutable_parser_string(str);
4809 VALUE error = rb_reg_check_preprocess(s);
4810 if (!
NIL_P(error)) {
4811 COMPILE_ERROR(args->iseq, nd_line(node),
"%" PRIsVALUE, error);
4815 if (
NIL_P(args->lit)) {
4817 args->lit_node = node;
4826flush_dstr_fragment(
struct dstr_ctxt *args)
4828 if (!
NIL_P(args->lit)) {
4830 VALUE lit = args->lit;
4832 lit = rb_fstring(lit);
4833 ADD_INSN1(args->ret, args->lit_node, putobject, lit);
4840compile_dstr_fragments_0(
struct dstr_ctxt *args,
const NODE *
const node)
4842 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4846 CHECK(append_dstr_fragment(args, node, str));
4850 const NODE *
const head = list->nd_head;
4851 if (nd_type_p(head, NODE_STR)) {
4852 CHECK(append_dstr_fragment(args, node, RNODE_STR(head)->
string));
4854 else if (nd_type_p(head, NODE_DSTR)) {
4855 CHECK(compile_dstr_fragments_0(args, head));
4858 flush_dstr_fragment(args);
4860 CHECK(COMPILE(args->ret,
"each string", head));
4872 .iseq = iseq, .ret = ret,
4873 .lit =
Qnil, .lit_node = NULL,
4874 .cnt = 0, .dregx = dregx,
4876 CHECK(compile_dstr_fragments_0(&args, node));
4877 flush_dstr_fragment(&args);
4887 while (node && nd_type_p(node, NODE_BLOCK)) {
4888 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4889 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4890 node = RNODE_BLOCK(node)->nd_next;
4893 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4902 if (!RNODE_DSTR(node)->nd_next) {
4903 VALUE lit = rb_node_dstr_string_val(node);
4904 ADD_INSN1(ret, node, dupstring, lit);
4909 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, FALSE));
4910 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4919 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
4921 if (!RNODE_DREGX(node)->nd_next) {
4923 VALUE src = rb_node_dregx_string_val(node);
4924 VALUE match = iseq_reg_compile(iseq, src, cflag, NULL, 0);
4925 ADD_INSN1(ret, node, putobject, match);
4931 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, TRUE));
4935 ADD_INSN(ret, node, pop);
4945 const int line = nd_line(node);
4946 LABEL *lend = NEW_LABEL(line);
4947 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4948 + VM_SVAR_FLIPFLOP_START;
4951 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4952 ADD_INSNL(ret, node, branchif, lend);
4955 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
4956 ADD_INSNL(ret, node, branchunless, else_label);
4957 ADD_INSN1(ret, node, putobject,
Qtrue);
4958 ADD_INSN1(ret, node, setspecial, key);
4960 ADD_INSNL(ret, node, jump, then_label);
4964 ADD_LABEL(ret, lend);
4965 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
4966 ADD_INSNL(ret, node, branchunless, then_label);
4967 ADD_INSN1(ret, node, putobject,
Qfalse);
4968 ADD_INSN1(ret, node, setspecial, key);
4969 ADD_INSNL(ret, node, jump, then_label);
4978#define COMPILE_SINGLE 2
4985 LABEL *label = NEW_LABEL(nd_line(cond));
4986 if (!then_label) then_label = label;
4987 else if (!else_label) else_label = label;
4989 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
4991 if (LIST_INSN_SIZE_ONE(seq)) {
4992 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
4993 if (insn->insn_id == BIN(jump) && (
LABEL *)(insn->operands[0]) == label)
4996 if (!label->refcnt) {
4997 return COMPILE_SINGLE;
4999 ADD_LABEL(seq, label);
5009 DECL_ANCHOR(ignore);
5012 switch (nd_type(cond)) {
5014 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
5015 cond = RNODE_AND(cond)->nd_2nd;
5016 if (ok == COMPILE_SINGLE) {
5017 ADD_INSNL(ret, cond, jump, else_label);
5018 INIT_ANCHOR(ignore);
5020 then_label = NEW_LABEL(nd_line(cond));
5024 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
5025 cond = RNODE_OR(cond)->nd_2nd;
5026 if (ok == COMPILE_SINGLE) {
5027 ADD_INSNL(ret, cond, jump, then_label);
5028 INIT_ANCHOR(ignore);
5030 else_label = NEW_LABEL(nd_line(cond));
5040 case NODE_IMAGINARY:
5047 ADD_INSNL(ret, cond, jump, then_label);
5052 ADD_INSNL(ret, cond, jump, else_label);
5058 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
5059 ADD_INSNL(ret, cond, jump, then_label);
5062 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
5065 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
5068 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
5072 DECL_ANCHOR(cond_seq);
5073 INIT_ANCHOR(cond_seq);
5075 CHECK(COMPILE(cond_seq,
"branch condition", cond));
5077 if (LIST_INSN_SIZE_ONE(cond_seq)) {
5078 INSN *insn = (
INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
5079 if (insn->insn_id == BIN(putobject)) {
5080 if (
RTEST(insn->operands[0])) {
5081 ADD_INSNL(ret, cond, jump, then_label);
5086 ADD_INSNL(ret, cond, jump, else_label);
5091 ADD_SEQ(ret, cond_seq);
5096 ADD_INSNL(ret, cond, branchunless, else_label);
5097 ADD_INSNL(ret, cond, jump, then_label);
5104keyword_node_p(
const NODE *
const node)
5106 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
5112 switch (nd_type(node)) {
5114 return rb_node_sym_string_val(node);
5116 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
5123 NODE *node = node_hash->nd_head;
5124 VALUE hash = rb_hash_new();
5127 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5128 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
5129 VALUE idx = rb_hash_aref(hash, key);
5134 rb_hash_aset(hash, key,
INT2FIX(i));
5144 const NODE *
const root_node,
5152 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
5153 const NODE *node = RNODE_HASH(root_node)->nd_head;
5157 const NODE *key_node = RNODE_LIST(node)->nd_head;
5161 if (key_node && nd_type_p(key_node, NODE_SYM)) {
5166 *flag |= VM_CALL_KW_SPLAT;
5167 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5172 *flag |= VM_CALL_KW_SPLAT_MUT;
5177 node = RNODE_LIST(node)->nd_next;
5178 node = RNODE_LIST(node)->nd_next;
5182 node = RNODE_HASH(root_node)->nd_head;
5185 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
5187 if (
len > VM_CALL_KW_LEN_MAX) {
5188 COMPILE_ERROR(ERROR_ARGS_AT(root_node)
"too many keyword arguments (%d, maximum is %d)",
5189 len, (
int)VM_CALL_KW_LEN_MAX);
5194 VALUE *keywords = kw_arg->keywords;
5197 kw_arg->references = 0;
5198 kw_arg->keyword_len =
len;
5200 *kw_arg_ptr = kw_arg;
5202 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5203 const NODE *key_node = RNODE_LIST(node)->nd_head;
5204 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5207 keywords[j] = get_symbol_value(iseq, key_node);
5211 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
5225 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
5227 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
5230 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5231 *kwnode_ptr = RNODE_LIST(node)->nd_head;
5234 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
5235 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
5243frozen_string_literal_p(
const rb_iseq_t *iseq)
5245 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
5249static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
5251 switch (nd_type(node)) {
5259 case NODE_IMAGINARY:
5266 return hash_key || frozen_string_literal_p(iseq);
5275 switch (nd_type(node)) {
5278 VALUE lit = rb_node_integer_literal_val(node);
5284 VALUE lit = rb_node_float_literal_val(node);
5290 case NODE_IMAGINARY:
5299 return rb_node_sym_string_val(node);
5303 return rb_node_line_lineno_val(node);
5305 return rb_node_encoding_val(node);
5308 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
5309 VALUE lit = get_string_value(node);
5310 VALUE str = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
5315 return get_string_value(node);
5318 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
5325 const NODE *line_node = node;
5327 if (nd_type_p(node, NODE_ZLIST)) {
5329 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5334 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5337 for (; node; node = RNODE_LIST(node)->nd_next) {
5338 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5380 const int max_stack_len = 0x100;
5381 const int min_tmp_ary_len = 0x40;
5385#define FLUSH_CHUNK \
5387 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5388 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5389 first_chunk = FALSE; \
5397 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5399 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5400 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5403 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5408 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5409 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5415 ADD_INSN1(ret, line_node, duparray, ary);
5416 first_chunk = FALSE;
5419 ADD_INSN1(ret, line_node, putobject, ary);
5420 ADD_INSN(ret, line_node, concattoarray);
5428 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5430 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5433 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5435 if (stack_len == 0 && first_chunk) {
5436 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5441 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5442 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5446 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5451 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5461static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5463 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);
5469 const NODE *line_node = node;
5471 node = RNODE_HASH(node)->nd_head;
5473 if (!node || nd_type_p(node, NODE_ZLIST)) {
5475 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5480 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5483 for (; node; node = RNODE_LIST(node)->nd_next) {
5484 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5507 const int max_stack_len = 0x100;
5508 const int min_tmp_hash_len = 0x800;
5510 int first_chunk = 1;
5511 DECL_ANCHOR(anchor);
5512 INIT_ANCHOR(anchor);
5515#define FLUSH_CHUNK() \
5517 if (first_chunk) { \
5518 APPEND_LIST(ret, anchor); \
5519 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5522 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5523 ADD_INSN(ret, line_node, swap); \
5524 APPEND_LIST(ret, anchor); \
5525 ADD_SEND(ret, line_node, id_core_hash_merge_bang_ptr, INT2FIX(stack_len + 1)); \
5527 INIT_ANCHOR(anchor); \
5528 first_chunk = stack_len = 0; \
5535 if (static_literal_node_pair_p(node, iseq)) {
5537 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5538 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5541 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5546 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5548 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5550 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5562 ADD_INSN1(ret, line_node, duphash, hash);
5566 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5567 ADD_INSN(ret, line_node, swap);
5569 ADD_INSN1(ret, line_node, putobject, hash);
5571 ADD_SEND(ret, line_node, id_core_hash_merge_bang_kwd,
INT2FIX(2));
5578 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5581 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5584 if (RNODE_LIST(node)->nd_head) {
5586 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5587 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5591 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5597 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5598 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5599 int first_kw = first_chunk && stack_len == 0;
5600 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5601 int only_kw = last_kw && first_kw;
5603 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5605 if (only_kw && method_call_keywords) {
5613 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5615 else if (first_kw) {
5619 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5626 if (only_kw && method_call_keywords) {
5632 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5639 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5640 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5641 else ADD_INSN(ret, line_node, swap);
5643 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5645 ADD_SEND(ret, line_node, id_core_hash_merge_bang_kwd,
INT2FIX(2));
5660rb_node_case_when_optimizable_literal(
const NODE *
const node)
5662 switch (nd_type(node)) {
5664 return rb_node_integer_literal_val(node);
5666 VALUE v = rb_node_float_literal_val(node);
5675 case NODE_IMAGINARY:
5684 return rb_node_sym_string_val(node);
5686 return rb_node_line_lineno_val(node);
5688 return rb_node_str_string_val(node);
5690 return rb_node_file_path_val(node);
5697 LABEL *l1,
int only_special_literals,
VALUE literals)
5700 const NODE *val = RNODE_LIST(vals)->nd_head;
5701 VALUE lit = rb_node_case_when_optimizable_literal(val);
5704 only_special_literals = 0;
5707 cdhash_aset_if_missing(literals, lit, (
VALUE)(l1));
5710 if (nd_type_p(val, NODE_STR) || nd_type_p(val, NODE_FILE)) {
5711 debugp_param(
"nd_lit", get_string_value(val));
5712 lit = get_string_value(val);
5713 ADD_INSN1(cond_seq, val, putobject, lit);
5717 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5721 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5722 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5723 ADD_INSNL(cond_seq, val, branchif, l1);
5724 vals = RNODE_LIST(vals)->nd_next;
5726 return only_special_literals;
5731 LABEL *l1,
int only_special_literals,
VALUE literals)
5733 const NODE *line_node = vals;
5735 switch (nd_type(vals)) {
5737 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5741 ADD_INSN (cond_seq, line_node, dup);
5742 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
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);
5748 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5749 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5752 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5753 ADD_INSN (cond_seq, line_node, dup);
5754 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5755 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5756 ADD_INSNL(cond_seq, line_node, branchif, l1);
5759 ADD_INSN (cond_seq, line_node, dup);
5760 CHECK(COMPILE(cond_seq,
"when val", vals));
5761 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5762 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5763 ADD_INSNL(cond_seq, line_node, branchif, l1);
5856 const NODE *line_node;
5871add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc,
INSN *before_insn)
5874 rb_bug(
"no masgn_state");
5883 memo->before_insn = before_insn;
5884 memo->line_node = line_node;
5885 memo->argn = state->num_args + 1;
5886 memo->num_args = argc;
5887 state->num_args += argc;
5888 memo->lhs_pos = lhs_pos;
5890 if (!state->first_memo) {
5891 state->first_memo = memo;
5894 state->last_memo->next = memo;
5896 state->last_memo = memo;
5906 switch (nd_type(node)) {
5907 case NODE_ATTRASGN: {
5909 const NODE *line_node = node;
5911 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5913 bool safenav_call =
false;
5915 iobj = (
INSN *)get_prev_insn((
INSN *)insn_element);
5917 ELEM_REMOVE(insn_element);
5918 if (!IS_INSN_ID(iobj, send)) {
5919 safenav_call =
true;
5920 iobj = (
INSN *)get_prev_insn(iobj);
5921 ELEM_INSERT_NEXT(&iobj->link, insn_element);
5923 (pre->last = iobj->link.prev)->next = 0;
5926 int argc = vm_ci_argc(ci) + 1;
5927 ci = ci_argc_set(iseq, ci, argc);
5928 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5932 ADD_INSN(lhs, line_node, swap);
5935 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5938 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (
INSN *)LAST_ELEMENT(lhs))) {
5942 iobj->link.prev = lhs->last;
5943 lhs->last->next = &iobj->link;
5944 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
5945 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5946 int argc = vm_ci_argc(ci);
5947 bool dupsplat =
false;
5948 ci = ci_argc_set(iseq, ci, argc - 1);
5949 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
5956 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
5958 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5967 int line_no = nd_line(line_node);
5968 int node_id = nd_node_id(line_node);
5971 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5972 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
5973 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5975 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
5977 if (!safenav_call) {
5978 ADD_INSN(lhs, line_node, pop);
5980 ADD_INSN(lhs, line_node, pop);
5983 for (
int i=0; i < argc; i++) {
5984 ADD_INSN(post, line_node, pop);
5989 DECL_ANCHOR(nest_rhs);
5990 INIT_ANCHOR(nest_rhs);
5991 DECL_ANCHOR(nest_lhs);
5992 INIT_ANCHOR(nest_lhs);
5994 int prev_level = state->lhs_level;
5995 bool prev_nested = state->nested;
5997 state->lhs_level = lhs_pos - 1;
5998 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5999 state->lhs_level = prev_level;
6000 state->nested = prev_nested;
6002 ADD_SEQ(lhs, nest_rhs);
6003 ADD_SEQ(lhs, nest_lhs);
6007 if (!RNODE_CDECL(node)->nd_vid) {
6011 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
6014 iobj = (
INSN *)insn_element;
6017 ELEM_REMOVE(insn_element);
6018 pre->last = iobj->link.prev;
6021 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (
INSN *)LAST_ELEMENT(lhs))) {
6025 ADD_INSN(post, node, pop);
6030 DECL_ANCHOR(anchor);
6031 INIT_ANCHOR(anchor);
6032 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
6033 ELEM_REMOVE(FIRST_ELEMENT(anchor));
6034 ADD_SEQ(lhs, anchor);
6045 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
6046 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
6053 const NODE *rhsn,
const NODE *orig_lhsn)
6056 const int memsize = numberof(mem);
6058 int llen = 0, rlen = 0;
6060 const NODE *lhsn = orig_lhsn;
6062#define MEMORY(v) { \
6064 if (memindex == memsize) return 0; \
6065 for (i=0; i<memindex; i++) { \
6066 if (mem[i] == (v)) return 0; \
6068 mem[memindex++] = (v); \
6071 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
6076 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
6077 switch (nd_type(ln)) {
6082 MEMORY(get_nd_vid(ln));
6087 lhsn = RNODE_LIST(lhsn)->nd_next;
6093 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
6096 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
6098 rhsn = RNODE_LIST(rhsn)->nd_next;
6103 for (i=0; i<llen-rlen; i++) {
6104 ADD_INSN(ret, orig_lhsn, putnil);
6108 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
6115 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
6116 const NODE *splatn = RNODE_MASGN(node)->nd_args;
6117 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
6118 const NODE *lhsn_count = lhsn;
6119 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
6124 while (lhsn_count) {
6126 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
6129 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
6131 lhsn = RNODE_LIST(lhsn)->nd_next;
6135 if (nd_type_p(splatn, NODE_POSTARG)) {
6137 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
6138 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
6139 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
6141 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
6143 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
6145 if (NODE_NAMED_REST_P(restn)) {
6146 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
6149 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
6151 postn = RNODE_LIST(postn)->nd_next;
6156 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
6160 if (!state->nested) {
6161 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
6165 ADD_INSN(rhs, node, dup);
6167 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
6174 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
6176 state.lhs_level = popped ? 0 : 1;
6179 state.first_memo = NULL;
6180 state.last_memo = NULL;
6190 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
6194 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
6195 for (
int i = 0; i < memo->num_args; i++) {
6196 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
6198 tmp_memo = memo->next;
6207 if (!popped && state.num_args >= 1) {
6209 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
6221 switch (nd_type(node)) {
6223 rb_ary_unshift(arr,
ID2SYM(RNODE_CONST(node)->nd_vid));
6227 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6228 rb_ary_unshift(arr,
ID2SYM(idNULL));
6232 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6233 node = RNODE_COLON2(node)->nd_head;
6242compile_const_prefix(
rb_iseq_t *iseq,
const NODE *
const node,
6245 switch (nd_type(node)) {
6247 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
6248 ADD_INSN1(body, node, putobject,
Qtrue);
6249 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
6252 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
6253 ADD_INSN(body, node, pop);
6254 ADD_INSN1(body, node, putobject,
rb_cObject);
6255 ADD_INSN1(body, node, putobject,
Qtrue);
6256 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6259 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
6260 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
6261 ADD_INSN1(body, node, putobject,
Qfalse);
6262 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6265 CHECK(COMPILE(pref,
"const colon2 prefix", node));
6272cpath_const_p(
const NODE *node)
6274 switch (nd_type(node)) {
6279 if (RNODE_COLON2(node)->nd_head) {
6280 return cpath_const_p(RNODE_COLON2(node)->nd_head);
6291 if (nd_type_p(cpath, NODE_COLON3)) {
6293 ADD_INSN1(ret, cpath, putobject,
rb_cObject);
6294 return VM_DEFINECLASS_FLAG_SCOPED;
6296 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
6298 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
6299 int flags = VM_DEFINECLASS_FLAG_SCOPED;
6300 if (!cpath_const_p(RNODE_COLON2(cpath)->nd_head)) {
6301 flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
6307 ADD_INSN1(ret, cpath, putspecialobject,
6308 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
6314private_recv_p(
const NODE *node)
6316 NODE *recv = get_nd_recv(node);
6317 if (recv && nd_type_p(recv, NODE_SELF)) {
6318 return RNODE_SELF(recv)->nd_state != 0;
6325 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore);
6328compile_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);
6335 enum defined_type expr_type = DEFINED_NOT_DEFINED;
6336 enum node_type
type;
6337 const int line = nd_line(node);
6338 const NODE *line_node = node;
6340 switch (
type = nd_type(node)) {
6344 expr_type = DEFINED_NIL;
6347 expr_type = DEFINED_SELF;
6350 expr_type = DEFINED_TRUE;
6353 expr_type = DEFINED_FALSE;
6358 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
6362 if (RNODE_LIST(vals)->nd_head) {
6363 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
6366 lfinish[1] = NEW_LABEL(line);
6368 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6370 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6383 case NODE_IMAGINARY:
6388 expr_type = DEFINED_EXPR;
6392 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6394 lfinish[1] = NEW_LABEL(line);
6396 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6397 expr_type = DEFINED_EXPR;
6403 expr_type = DEFINED_LVAR;
6406#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6408 ADD_INSN3(ret, line_node, definedivar,
6409 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6413 ADD_INSN(ret, line_node, putnil);
6414 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6415 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6419 ADD_INSN(ret, line_node, putnil);
6420 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6421 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6425 ADD_INSN(ret, line_node, putnil);
6426 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6427 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6431 lfinish[1] = NEW_LABEL(line);
6433 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6434 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6435 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6438 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6439 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6442 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6443 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6447 ADD_INSN1(ret, line_node, putobject,
rb_cObject);
6448 ADD_INSN3(ret, line_node, defined,
6449 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6457 case NODE_ATTRASGN:{
6458 const int explicit_receiver =
6459 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6460 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6462 if (get_nd_args(node) || explicit_receiver) {
6464 lfinish[1] = NEW_LABEL(line);
6467 lfinish[2] = NEW_LABEL(line);
6470 if (get_nd_args(node)) {
6471 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6472 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6474 if (explicit_receiver) {
6475 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6476 switch (nd_type(get_nd_recv(node))) {
6482 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6483 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6486 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6487 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6491 ADD_INSN(ret, line_node, dup);
6493 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6494 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6497 ADD_INSN(ret, line_node, putself);
6499 ADD_INSN(ret, line_node, dup);
6501 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6502 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6508 ADD_INSN(ret, line_node, putnil);
6509 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6510 PUSH_VAL(DEFINED_YIELD));
6511 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6516 ADD_INSN(ret, line_node, putnil);
6517 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6518 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6519 PUSH_VAL(DEFINED_GVAR));
6524 ADD_INSN(ret, line_node, putnil);
6525 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6526 PUSH_VAL(DEFINED_ZSUPER));
6532 case NODE_OP_ASGN_OR:
6533 case NODE_OP_ASGN_AND:
6542 expr_type = DEFINED_ASGN;
6549 VALUE str = rb_iseq_defined_string(expr_type);
6550 ADD_INSN1(ret, line_node, putobject, str);
6553 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6560 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6561 iseq_set_exception_local_table(iseq);
6566 const NODE *
const node,
LABEL **lfinish,
VALUE needstr,
bool ignore)
6569 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6571 int line = nd_line(node);
6572 LABEL *lstart = NEW_LABEL(line);
6573 LABEL *lend = NEW_LABEL(line);
6576 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6577 rescue = NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc,
6579 ISEQ_BODY(iseq)->location.label),
6580 ISEQ_TYPE_RESCUE, 0);
6581 lstart->rescued = LABEL_RESCUE_BEG;
6582 lend->rescued = LABEL_RESCUE_END;
6583 APPEND_LABEL(ret, lcur, lstart);
6584 ADD_LABEL(ret, lend);
6586 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6594 const int line = nd_line(node);
6595 const NODE *line_node = node;
6596 if (!RNODE_DEFINED(node)->nd_head) {
6597 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6598 ADD_INSN1(ret, line_node, putobject, str);
6603 lfinish[0] = NEW_LABEL(line);
6606 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6608 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6609 ADD_INSN(ret, line_node, swap);
6611 ADD_LABEL(ret, lfinish[2]);
6613 ADD_INSN(ret, line_node, pop);
6614 ADD_LABEL(ret, lfinish[1]);
6616 ADD_LABEL(ret, lfinish[0]);
6622make_name_for_block(
const rb_iseq_t *orig_iseq)
6627 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6628 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6629 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6632 iseq = ISEQ_BODY(iseq)->parent_iseq;
6637 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6640 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6649 enl->ensure_node = node;
6650 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6652 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6662 while (erange->next != 0) {
6663 erange = erange->next;
6667 ne->end = erange->end;
6668 erange->end = lstart;
6674can_add_ensure_iseq(
const rb_iseq_t *iseq)
6677 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
6679 if (e->ensure_node)
return false;
6692 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6694 DECL_ANCHOR(ensure);
6696 INIT_ANCHOR(ensure);
6698 if (enlp->erange != NULL) {
6699 DECL_ANCHOR(ensure_part);
6700 LABEL *lstart = NEW_LABEL(0);
6701 LABEL *lend = NEW_LABEL(0);
6702 INIT_ANCHOR(ensure_part);
6704 add_ensure_range(iseq, enlp->erange, lstart, lend);
6706 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6707 ADD_LABEL(ensure_part, lstart);
6708 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6709 ADD_LABEL(ensure_part, lend);
6710 ADD_SEQ(ensure, ensure_part);
6719 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6720 ADD_SEQ(ret, ensure);
6725check_keyword(
const NODE *node)
6729 if (nd_type_p(node, NODE_LIST)) {
6730 while (RNODE_LIST(node)->nd_next) {
6731 node = RNODE_LIST(node)->nd_next;
6733 node = RNODE_LIST(node)->nd_head;
6736 return keyword_node_p(node);
6741keyword_node_single_splat_p(
NODE *kwnode)
6745 NODE *node = RNODE_HASH(kwnode)->nd_head;
6746 return RNODE_LIST(node)->nd_head == NULL &&
6747 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6752 NODE *kwnode,
unsigned int *flag_ptr)
6754 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6755 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6756 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6757 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6758 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6761#define SPLATARRAY_FALSE 0
6762#define SPLATARRAY_TRUE 1
6763#define DUP_SINGLE_KW_SPLAT 2
6767 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6769 if (!argn)
return 0;
6771 NODE *kwnode = NULL;
6773 switch (nd_type(argn)) {
6776 int len = compile_args(iseq, args, argn, &kwnode);
6777 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6780 if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6784 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6785 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6788 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6797 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6798 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6799 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6800 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6801 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6804 case NODE_ARGSCAT: {
6805 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6806 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6807 bool args_pushed =
false;
6809 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6810 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6811 if (kwnode) rest_len--;
6812 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6816 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6817 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6820 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6821 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6822 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6825 else if (!args_pushed) {
6826 ADD_INSN(args, argn, concattoarray);
6832 *flag_ptr |= VM_CALL_KW_SPLAT;
6833 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6839 case NODE_ARGSPUSH: {
6840 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6841 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6843 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6844 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6845 if (kwnode) rest_len--;
6846 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6847 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6850 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6851 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6854 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6855 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6861 *flag_ptr |= VM_CALL_KW_SPLAT;
6862 if (!keyword_node_single_splat_p(kwnode)) {
6863 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6864 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6866 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
6867 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6870 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6878 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
6884setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
6886 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
6887 *flag |= VM_CALL_ARGS_SPLAT_MUT;
6892setup_args_dup_rest_p(
const NODE *argn)
6894 switch(nd_type(argn)) {
6905 case NODE_IMAGINARY:
6918 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
6921 if (setup_args_dup_rest_p(RNODE_LIST(argn)->nd_head)) {
6924 argn = RNODE_LIST(argn)->nd_next;
6937 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
6940 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
6941 RNODE_BLOCK_PASS(argn)->nd_head : argn;
6944 switch(nd_type(check_arg)) {
6947 dup_rest = SPLATARRAY_FALSE;
6951 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
6953 case(NODE_ARGSPUSH):
6955 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
6956 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
6957 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
6958 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
6959 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
6961 if (dup_rest == SPLATARRAY_FALSE) {
6963 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
6965 NODE *key_node = RNODE_LIST(node)->nd_head;
6966 if (key_node && setup_args_dup_rest_p(key_node)) {
6967 dup_rest = SPLATARRAY_TRUE;
6971 node = RNODE_LIST(node)->nd_next;
6972 NODE *value_node = RNODE_LIST(node)->nd_head;
6973 if (setup_args_dup_rest_p(value_node)) {
6974 dup_rest = SPLATARRAY_TRUE;
6978 node = RNODE_LIST(node)->nd_next;
6987 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
6989 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
6992 initial_dup_rest = dup_rest;
6994 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
6995 DECL_ANCHOR(arg_block);
6996 INIT_ANCHOR(arg_block);
6998 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
6999 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
7001 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
7002 const NODE * arg_node =
7003 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
7010 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
7011 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
7014 *flag |= VM_CALL_FORWARDING;
7016 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
7017 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
7021 *flag |= VM_CALL_ARGS_BLOCKARG;
7023 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
7026 if (LIST_INSN_SIZE_ONE(arg_block)) {
7028 if (IS_INSN(elem)) {
7030 if (iobj->insn_id == BIN(getblockparam)) {
7031 iobj->insn_id = BIN(getblockparamproxy);
7035 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
7036 ADD_SEQ(args, arg_block);
7039 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
7041 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
7048 const NODE *body = ptr;
7049 int line = nd_line(body);
7051 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
7053 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7054 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
7056 iseq_set_local_table(iseq, 0, 0);
7064 int line = nd_line(node);
7065 const NODE *line_node = node;
7066 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
7068#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
7069 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
7073 ADD_INSN(ret, line_node, dup);
7074 ADD_INSNL(ret, line_node, branchunless, fail_label);
7076 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
7078 if (RNODE_BLOCK(vars)->nd_next) {
7079 ADD_INSN(ret, line_node, dup);
7082 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
7084 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
7087#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
7088 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
7093 ADD_INSNL(nom, line_node, jump, end_label);
7094 ADD_LABEL(nom, fail_label);
7096 ADD_INSN(nom, line_node, pop);
7097 ADD_INSN(nom, line_node, putnil);
7099 ADD_LABEL(nom, end_label);
7100 (nom->last->next = cap->link.next)->prev = nom->last;
7101 (cap->link.next = nom->anchor.next)->prev = &cap->link;
7106 ADD_INSNL(ret, line_node, jump, end_label);
7107 ADD_LABEL(ret, fail_label);
7108 ADD_INSN(ret, line_node, pop);
7109 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
7111 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
7113 ((
INSN*)last)->insn_id = BIN(putnil);
7114 ((
INSN*)last)->operand_size = 0;
7116 ADD_LABEL(ret, end_label);
7120optimizable_range_item_p(
const NODE *n)
7122 if (!n)
return FALSE;
7123 switch (nd_type(n)) {
7136optimized_range_item(
const NODE *n)
7138 switch (nd_type(n)) {
7140 return rb_node_line_lineno_val(n);
7142 return rb_node_integer_literal_val(n);
7144 return rb_node_float_literal_val(n);
7146 return rb_node_rational_literal_val(n);
7147 case NODE_IMAGINARY:
7148 return rb_node_imaginary_literal_val(n);
7152 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
7159 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
7160 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
7162 const int line = nd_line(node);
7163 const NODE *line_node = node;
7164 DECL_ANCHOR(cond_seq);
7165 LABEL *then_label, *else_label, *end_label;
7168 INIT_ANCHOR(cond_seq);
7169 then_label = NEW_LABEL(line);
7170 else_label = NEW_LABEL(line);
7173 NODE *cond = RNODE_IF(node)->nd_cond;
7174 if (nd_type(cond) == NODE_BLOCK) {
7175 cond = RNODE_BLOCK(cond)->nd_head;
7178 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
7179 ADD_SEQ(ret, cond_seq);
7181 if (then_label->refcnt && else_label->refcnt) {
7182 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
7185 if (then_label->refcnt) {
7186 ADD_LABEL(ret, then_label);
7188 DECL_ANCHOR(then_seq);
7189 INIT_ANCHOR(then_seq);
7190 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
7192 if (else_label->refcnt) {
7193 const NODE *
const coverage_node = node_body ? node_body : node;
7194 add_trace_branch_coverage(
7197 nd_code_loc(coverage_node),
7198 nd_node_id(coverage_node),
7200 type == NODE_IF ?
"then" :
"else",
7202 end_label = NEW_LABEL(line);
7203 ADD_INSNL(then_seq, line_node, jump, end_label);
7205 ADD_INSN(then_seq, line_node, pop);
7208 ADD_SEQ(ret, then_seq);
7211 if (else_label->refcnt) {
7212 ADD_LABEL(ret, else_label);
7214 DECL_ANCHOR(else_seq);
7215 INIT_ANCHOR(else_seq);
7216 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
7218 if (then_label->refcnt) {
7219 const NODE *
const coverage_node = node_else ? node_else : node;
7220 add_trace_branch_coverage(
7223 nd_code_loc(coverage_node),
7224 nd_node_id(coverage_node),
7226 type == NODE_IF ?
"else" :
"then",
7229 ADD_SEQ(ret, else_seq);
7233 ADD_LABEL(ret, end_label);
7243 const NODE *node = orig_node;
7244 LABEL *endlabel, *elselabel;
7246 DECL_ANCHOR(body_seq);
7247 DECL_ANCHOR(cond_seq);
7248 int only_special_literals = 1;
7249 VALUE literals = cdhash_new(0);
7251 enum node_type
type;
7252 const NODE *line_node;
7257 INIT_ANCHOR(body_seq);
7258 INIT_ANCHOR(cond_seq);
7260 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
7262 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"case");
7264 node = RNODE_CASE(node)->nd_body;
7265 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
7266 type = nd_type(node);
7267 line = nd_line(node);
7270 endlabel = NEW_LABEL(line);
7271 elselabel = NEW_LABEL(line);
7275 while (
type == NODE_WHEN) {
7278 l1 = NEW_LABEL(line);
7279 ADD_LABEL(body_seq, l1);
7280 ADD_INSN(body_seq, line_node, pop);
7282 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7283 add_trace_branch_coverage(
7286 nd_code_loc(coverage_node),
7287 nd_node_id(coverage_node),
7292 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
7293 ADD_INSNL(body_seq, line_node, jump, endlabel);
7295 vals = RNODE_WHEN(node)->nd_head;
7297 switch (nd_type(vals)) {
7299 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
7300 if (only_special_literals < 0)
return COMPILE_NG;
7305 only_special_literals = 0;
7306 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
7309 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
7313 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
7316 node = RNODE_WHEN(node)->nd_next;
7320 type = nd_type(node);
7321 line = nd_line(node);
7326 ADD_LABEL(cond_seq, elselabel);
7327 ADD_INSN(cond_seq, line_node, pop);
7328 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
7329 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7330 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7333 debugs(
"== else (implicit)\n");
7334 ADD_LABEL(cond_seq, elselabel);
7335 ADD_INSN(cond_seq, orig_node, pop);
7336 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
7338 ADD_INSN(cond_seq, orig_node, putnil);
7340 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7343 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7344 ADD_INSN(ret, orig_node, dup);
7345 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
7347 LABEL_REF(elselabel);
7350 ADD_SEQ(ret, cond_seq);
7351 ADD_SEQ(ret, body_seq);
7352 ADD_LABEL(ret, endlabel);
7361 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
7363 DECL_ANCHOR(body_seq);
7367 branches = decl_branch_base(iseq, nd_node_id(orig_node), nd_code_loc(orig_node),
"case");
7369 INIT_ANCHOR(body_seq);
7370 endlabel = NEW_LABEL(nd_line(node));
7372 while (node && nd_type_p(node, NODE_WHEN)) {
7373 const int line = nd_line(node);
7374 LABEL *l1 = NEW_LABEL(line);
7375 ADD_LABEL(body_seq, l1);
7377 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7378 add_trace_branch_coverage(
7381 nd_code_loc(coverage_node),
7382 nd_node_id(coverage_node),
7387 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7388 ADD_INSNL(body_seq, node, jump, endlabel);
7390 vals = RNODE_WHEN(node)->nd_head;
7392 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7394 switch (nd_type(vals)) {
7398 val = RNODE_LIST(vals)->nd_head;
7399 lnext = NEW_LABEL(nd_line(val));
7400 debug_compile(
"== when2\n", (
void)0);
7401 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7402 ADD_LABEL(ret, lnext);
7403 vals = RNODE_LIST(vals)->nd_next;
7409 ADD_INSN(ret, vals, putnil);
7410 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7411 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7412 ADD_INSNL(ret, vals, branchif, l1);
7415 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7417 node = RNODE_WHEN(node)->nd_next;
7420 const NODE *
const coverage_node = node ? node : orig_node;
7421 add_trace_branch_coverage(
7424 nd_code_loc(coverage_node),
7425 nd_node_id(coverage_node),
7429 CHECK(COMPILE_(ret,
"else", node, popped));
7430 ADD_INSNL(ret, orig_node, jump, endlabel);
7432 ADD_SEQ(ret, body_seq);
7433 ADD_LABEL(ret, endlabel);
7437static 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);
7439static 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);
7440static 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);
7441static int iseq_compile_pattern_set_general_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7442static 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);
7443static int iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7445#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7446#define CASE3_BI_OFFSET_ERROR_STRING 1
7447#define CASE3_BI_OFFSET_KEY_ERROR_P 2
7448#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7449#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7452iseq_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)
7454 const int line = nd_line(node);
7455 const NODE *line_node = node;
7457 switch (nd_type(node)) {
7511 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7512 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7513 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7515 const int min_argc = pre_args_num + post_args_num;
7516 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7517 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7519 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7521 match_failed = NEW_LABEL(line);
7522 type_error = NEW_LABEL(line);
7523 deconstruct = NEW_LABEL(line);
7524 deconstructed = NEW_LABEL(line);
7527 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7528 ADD_INSN(ret, line_node, swap);
7534 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7536 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7538 ADD_INSN(ret, line_node, dup);
7539 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7540 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7541 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7542 if (in_single_pattern) {
7543 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7544 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7545 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7546 INT2FIX(min_argc), base_index + 1 ));
7548 ADD_INSNL(ret, line_node, branchunless, match_failed);
7550 for (i = 0; i < pre_args_num; i++) {
7551 ADD_INSN(ret, line_node, dup);
7552 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7553 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7554 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7555 args = RNODE_LIST(args)->nd_next;
7558 if (RNODE_ARYPTN(node)->rest_arg) {
7559 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7560 ADD_INSN(ret, line_node, dup);
7561 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7562 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7563 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7564 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7565 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7566 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7567 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7569 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7572 if (post_args_num > 0) {
7573 ADD_INSN(ret, line_node, dup);
7574 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7575 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7576 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7577 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7578 ADD_INSN(ret, line_node, pop);
7583 args = RNODE_ARYPTN(node)->post_args;
7584 for (i = 0; i < post_args_num; i++) {
7585 ADD_INSN(ret, line_node, dup);
7587 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7588 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7589 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7591 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7592 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7593 args = RNODE_LIST(args)->nd_next;
7596 ADD_INSN(ret, line_node, pop);
7598 ADD_INSN(ret, line_node, pop);
7600 ADD_INSNL(ret, line_node, jump, matched);
7601 ADD_INSN(ret, line_node, putnil);
7603 ADD_INSN(ret, line_node, putnil);
7606 ADD_LABEL(ret, type_error);
7607 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7609 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7610 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7611 ADD_INSN(ret, line_node, pop);
7613 ADD_LABEL(ret, match_failed);
7614 ADD_INSN(ret, line_node, pop);
7616 ADD_INSN(ret, line_node, pop);
7618 ADD_INSNL(ret, line_node, jump, unmatched);
7671 const NODE *args = RNODE_FNDPTN(node)->args;
7672 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7674 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7675 match_failed = NEW_LABEL(line);
7676 type_error = NEW_LABEL(line);
7677 deconstruct = NEW_LABEL(line);
7678 deconstructed = NEW_LABEL(line);
7680 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7682 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7684 ADD_INSN(ret, line_node, dup);
7685 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7686 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7687 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7688 if (in_single_pattern) {
7689 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 ));
7691 ADD_INSNL(ret, line_node, branchunless, match_failed);
7694 LABEL *while_begin = NEW_LABEL(nd_line(node));
7695 LABEL *next_loop = NEW_LABEL(nd_line(node));
7696 LABEL *find_succeeded = NEW_LABEL(line);
7697 LABEL *find_failed = NEW_LABEL(nd_line(node));
7700 ADD_INSN(ret, line_node, dup);
7701 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7703 ADD_INSN(ret, line_node, dup);
7704 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7705 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7707 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7709 ADD_LABEL(ret, while_begin);
7711 ADD_INSN(ret, line_node, dup);
7712 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7713 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7714 ADD_INSNL(ret, line_node, branchunless, find_failed);
7716 for (j = 0; j < args_num; j++) {
7717 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7718 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7720 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7721 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7723 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7725 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7726 args = RNODE_LIST(args)->nd_next;
7729 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7730 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7731 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7732 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7733 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7734 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));
7736 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7737 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7738 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7739 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7740 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7741 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7742 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7743 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));
7745 ADD_INSNL(ret, line_node, jump, find_succeeded);
7747 ADD_LABEL(ret, next_loop);
7748 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7749 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7750 ADD_INSNL(ret, line_node, jump, while_begin);
7752 ADD_LABEL(ret, find_failed);
7753 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7754 if (in_single_pattern) {
7755 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7756 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7757 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7758 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7759 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7761 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7762 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7764 ADD_INSN(ret, line_node, pop);
7765 ADD_INSN(ret, line_node, pop);
7767 ADD_INSNL(ret, line_node, jump, match_failed);
7768 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7770 ADD_LABEL(ret, find_succeeded);
7771 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7774 ADD_INSN(ret, line_node, pop);
7775 ADD_INSNL(ret, line_node, jump, matched);
7776 ADD_INSN(ret, line_node, putnil);
7778 ADD_LABEL(ret, type_error);
7779 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7781 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7782 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7783 ADD_INSN(ret, line_node, pop);
7785 ADD_LABEL(ret, match_failed);
7786 ADD_INSN(ret, line_node, pop);
7787 ADD_INSNL(ret, line_node, jump, unmatched);
7851 LABEL *match_failed, *type_error;
7854 match_failed = NEW_LABEL(line);
7855 type_error = NEW_LABEL(line);
7857 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
7858 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7859 keys =
rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
7861 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
7862 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
7866 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7868 ADD_INSN(ret, line_node, dup);
7869 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
7870 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7871 if (in_single_pattern) {
7872 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
7874 ADD_INSNL(ret, line_node, branchunless, match_failed);
7877 ADD_INSN(ret, line_node, putnil);
7881 ADD_INSN1(ret, line_node, duparray, keys);
7884 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
7886 ADD_INSN(ret, line_node, dup);
7888 ADD_INSNL(ret, line_node, branchunless, type_error);
7890 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7891 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
7894 if (RNODE_HSHPTN(node)->nd_pkwargs) {
7898 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7900 DECL_ANCHOR(match_values);
7901 INIT_ANCHOR(match_values);
7902 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
7903 for (i = 0; i < keys_num; i++) {
7904 NODE *key_node = RNODE_LIST(args)->nd_head;
7905 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
7906 VALUE key = get_symbol_value(iseq, key_node);
7908 ADD_INSN(ret, line_node, dup);
7909 ADD_INSN1(ret, line_node, putobject, key);
7910 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
7911 if (in_single_pattern) {
7912 LABEL *match_succeeded;
7913 match_succeeded = NEW_LABEL(line);
7915 ADD_INSN(ret, line_node, dup);
7916 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7920 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
7921 ADD_INSN1(ret, line_node, putobject,
Qtrue);
7922 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
7923 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7924 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
7925 ADD_INSN1(ret, line_node, putobject, key);
7926 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
7928 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
7930 ADD_LABEL(ret, match_succeeded);
7932 ADD_INSNL(ret, line_node, branchunless, match_failed);
7934 ADD_INSN(match_values, line_node, dup);
7935 ADD_INSN1(match_values, line_node, putobject, key);
7936 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
7937 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7938 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
7940 ADD_SEQ(ret, match_values);
7944 ADD_INSN(ret, line_node, dup);
7945 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7946 if (in_single_pattern) {
7947 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
7949 ADD_INSNL(ret, line_node, branchunless, match_failed);
7952 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7953 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
7954 ADD_INSN(ret, line_node, dup);
7955 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7956 if (in_single_pattern) {
7957 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
7959 ADD_INSNL(ret, line_node, branchunless, match_failed);
7962 ADD_INSN(ret, line_node, dup);
7963 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7967 ADD_INSN(ret, line_node, pop);
7968 ADD_INSNL(ret, line_node, jump, matched);
7969 ADD_INSN(ret, line_node, putnil);
7971 ADD_LABEL(ret, type_error);
7972 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7974 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
7975 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7976 ADD_INSN(ret, line_node, pop);
7978 ADD_LABEL(ret, match_failed);
7979 ADD_INSN(ret, line_node, pop);
7980 ADD_INSNL(ret, line_node, jump, unmatched);
7989 case NODE_IMAGINARY:
8017 CHECK(COMPILE(ret,
"case in literal", node));
8018 if (in_single_pattern) {
8019 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
8021 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
8022 if (in_single_pattern) {
8023 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
8025 ADD_INSNL(ret, line_node, branchif, matched);
8026 ADD_INSNL(ret, line_node, jump, unmatched);
8030 ID id = RNODE_LASGN(node)->nd_vid;
8031 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8033 if (in_alt_pattern) {
8034 const char *name = rb_id2name(
id);
8035 if (name && strlen(name) > 0 && name[0] !=
'_') {
8036 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
8042 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8043 ADD_INSNL(ret, line_node, jump, matched);
8048 ID id = RNODE_DASGN(node)->nd_vid;
8050 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
8052 if (in_alt_pattern) {
8053 const char *name = rb_id2name(
id);
8054 if (name && strlen(name) > 0 && name[0] !=
'_') {
8055 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
8062 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
8066 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
8067 ADD_INSNL(ret, line_node, jump, matched);
8072 LABEL *match_failed;
8073 match_failed = unmatched;
8074 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
8075 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
8076 if (in_single_pattern) {
8077 LABEL *match_succeeded;
8078 match_succeeded = NEW_LABEL(line);
8080 ADD_INSN(ret, line_node, dup);
8081 if (nd_type_p(node, NODE_IF)) {
8082 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8085 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
8088 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
8089 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8090 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8091 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8093 ADD_INSN(ret, line_node, pop);
8094 ADD_INSN(ret, line_node, pop);
8096 ADD_LABEL(ret, match_succeeded);
8098 if (nd_type_p(node, NODE_IF)) {
8099 ADD_INSNL(ret, line_node, branchunless, match_failed);
8102 ADD_INSNL(ret, line_node, branchif, match_failed);
8104 ADD_INSNL(ret, line_node, jump, matched);
8109 LABEL *match_failed;
8110 match_failed = NEW_LABEL(line);
8112 n = RNODE_HASH(node)->nd_head;
8113 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
8114 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8118 ADD_INSN(ret, line_node, dup);
8119 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));
8120 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));
8121 ADD_INSN(ret, line_node, putnil);
8123 ADD_LABEL(ret, match_failed);
8124 ADD_INSN(ret, line_node, pop);
8125 ADD_INSNL(ret, line_node, jump, unmatched);
8129 LABEL *match_succeeded, *fin;
8130 match_succeeded = NEW_LABEL(line);
8131 fin = NEW_LABEL(line);
8133 ADD_INSN(ret, line_node, dup);
8134 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));
8135 ADD_LABEL(ret, match_succeeded);
8136 ADD_INSN(ret, line_node, pop);
8137 ADD_INSNL(ret, line_node, jump, matched);
8138 ADD_INSN(ret, line_node, putnil);
8139 ADD_LABEL(ret, fin);
8140 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
8144 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
8150iseq_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)
8152 LABEL *fin = NEW_LABEL(nd_line(node));
8153 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
8154 ADD_LABEL(ret, fin);
8159iseq_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)
8161 const NODE *line_node = node;
8163 if (RNODE_ARYPTN(node)->nd_pconst) {
8164 ADD_INSN(ret, line_node, dup);
8165 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
8166 if (in_single_pattern) {
8167 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
8169 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
8170 if (in_single_pattern) {
8171 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
8173 ADD_INSNL(ret, line_node, branchunless, match_failed);
8180iseq_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)
8182 const NODE *line_node = node;
8186 if (use_deconstructed_cache) {
8188 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8189 ADD_INSNL(ret, line_node, branchnil, deconstruct);
8192 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8193 ADD_INSNL(ret, line_node, branchunless, match_failed);
8196 ADD_INSN(ret, line_node, pop);
8197 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
8198 ADD_INSNL(ret, line_node, jump, deconstructed);
8201 ADD_INSNL(ret, line_node, jump, deconstruct);
8204 ADD_LABEL(ret, deconstruct);
8205 ADD_INSN(ret, line_node, dup);
8206 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
8207 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
8210 if (use_deconstructed_cache) {
8211 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
8214 if (in_single_pattern) {
8215 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
8218 ADD_INSNL(ret, line_node, branchunless, match_failed);
8220 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
8223 if (use_deconstructed_cache) {
8224 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8227 ADD_INSN(ret, line_node, dup);
8229 ADD_INSNL(ret, line_node, branchunless, type_error);
8231 ADD_LABEL(ret, deconstructed);
8247 const int line = nd_line(node);
8248 const NODE *line_node = node;
8249 LABEL *match_succeeded = NEW_LABEL(line);
8251 ADD_INSN(ret, line_node, dup);
8252 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8254 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8255 ADD_INSN1(ret, line_node, putobject, errmsg);
8256 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8257 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
8258 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8260 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8261 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8263 ADD_INSN(ret, line_node, pop);
8264 ADD_INSN(ret, line_node, pop);
8265 ADD_LABEL(ret, match_succeeded);
8281 const int line = nd_line(node);
8282 const NODE *line_node = node;
8283 LABEL *match_succeeded = NEW_LABEL(line);
8285 ADD_INSN(ret, line_node, dup);
8286 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8288 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8289 ADD_INSN1(ret, line_node, putobject, errmsg);
8290 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8291 ADD_INSN(ret, line_node, dup);
8292 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
8293 ADD_INSN1(ret, line_node, putobject, pattern_length);
8294 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
8295 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8297 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8298 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8300 ADD_INSN(ret, line_node, pop);
8301 ADD_INSN(ret, line_node, pop);
8302 ADD_LABEL(ret, match_succeeded);
8308iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
8318 const int line = nd_line(node);
8319 const NODE *line_node = node;
8320 LABEL *match_succeeded = NEW_LABEL(line);
8322 ADD_INSN(ret, line_node, dup);
8323 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8325 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8326 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
8327 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8328 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
8329 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
8330 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8332 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8333 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8335 ADD_INSN(ret, line_node, pop);
8336 ADD_INSN(ret, line_node, pop);
8338 ADD_LABEL(ret, match_succeeded);
8339 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
8340 ADD_INSN(ret, line_node, pop);
8341 ADD_INSN(ret, line_node, pop);
8349 const NODE *pattern;
8350 const NODE *node = orig_node;
8351 LABEL *endlabel, *elselabel;
8353 DECL_ANCHOR(body_seq);
8354 DECL_ANCHOR(cond_seq);
8356 enum node_type
type;
8357 const NODE *line_node;
8360 bool single_pattern;
8363 INIT_ANCHOR(body_seq);
8364 INIT_ANCHOR(cond_seq);
8366 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"case");
8368 node = RNODE_CASE3(node)->nd_body;
8369 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
8370 type = nd_type(node);
8371 line = nd_line(node);
8373 single_pattern = !RNODE_IN(node)->nd_next;
8375 endlabel = NEW_LABEL(line);
8376 elselabel = NEW_LABEL(line);
8378 if (single_pattern) {
8380 ADD_INSN(head, line_node, putnil);
8381 ADD_INSN(head, line_node, putnil);
8382 ADD_INSN1(head, line_node, putobject,
Qfalse);
8383 ADD_INSN(head, line_node, putnil);
8385 ADD_INSN(head, line_node, putnil);
8387 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8391 while (
type == NODE_IN) {
8395 ADD_INSN(body_seq, line_node, putnil);
8397 l1 = NEW_LABEL(line);
8398 ADD_LABEL(body_seq, l1);
8399 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8401 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8402 add_trace_branch_coverage(
8405 nd_code_loc(coverage_node),
8406 nd_node_id(coverage_node),
8411 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8412 ADD_INSNL(body_seq, line_node, jump, endlabel);
8414 pattern = RNODE_IN(node)->nd_head;
8416 int pat_line = nd_line(pattern);
8417 LABEL *next_pat = NEW_LABEL(pat_line);
8418 ADD_INSN (cond_seq, pattern, dup);
8420 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8421 ADD_LABEL(cond_seq, next_pat);
8422 LABEL_UNREMOVABLE(next_pat);
8425 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8429 node = RNODE_IN(node)->nd_next;
8433 type = nd_type(node);
8434 line = nd_line(node);
8439 ADD_LABEL(cond_seq, elselabel);
8440 ADD_INSN(cond_seq, line_node, pop);
8441 ADD_INSN(cond_seq, line_node, pop);
8442 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8443 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8444 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8445 ADD_INSN(cond_seq, line_node, putnil);
8447 ADD_INSN(cond_seq, line_node, putnil);
8451 debugs(
"== else (implicit)\n");
8452 ADD_LABEL(cond_seq, elselabel);
8453 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8454 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8456 if (single_pattern) {
8464 LABEL *key_error, *fin;
8467 key_error = NEW_LABEL(line);
8468 fin = NEW_LABEL(line);
8471 kw_arg->references = 0;
8472 kw_arg->keyword_len = 2;
8473 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
8474 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
8476 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8477 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8479 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8480 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8481 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8482 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8483 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8484 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8485 ADD_INSNL(cond_seq, orig_node, jump, fin);
8487 ADD_LABEL(cond_seq, key_error);
8489 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8490 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8491 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8492 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8493 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8494 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8495 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8496 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
8497 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8499 ADD_LABEL(cond_seq, fin);
8503 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8504 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8506 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8508 ADD_INSN(cond_seq, orig_node, putnil);
8510 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8511 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8513 ADD_INSN(cond_seq, line_node, putnil);
8517 ADD_SEQ(ret, cond_seq);
8518 ADD_SEQ(ret, body_seq);
8519 ADD_LABEL(ret, endlabel);
8523#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8524#undef CASE3_BI_OFFSET_ERROR_STRING
8525#undef CASE3_BI_OFFSET_KEY_ERROR_P
8526#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8527#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8532 const int line = (int)nd_line(node);
8533 const NODE *line_node = node;
8535 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8536 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8537 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8538 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8543 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8544 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8545 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8546 LABEL *end_label = NEW_LABEL(line);
8547 LABEL *adjust_label = NEW_LABEL(line);
8549 LABEL *next_catch_label = NEW_LABEL(line);
8550 LABEL *tmp_label = NULL;
8552 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8553 push_ensure_entry(iseq, &enl, NULL, NULL);
8555 if (RNODE_WHILE(node)->nd_state == 1) {
8556 ADD_INSNL(ret, line_node, jump, next_label);
8559 tmp_label = NEW_LABEL(line);
8560 ADD_INSNL(ret, line_node, jump, tmp_label);
8562 ADD_LABEL(ret, adjust_label);
8563 ADD_INSN(ret, line_node, putnil);
8564 ADD_LABEL(ret, next_catch_label);
8565 ADD_INSN(ret, line_node, pop);
8566 ADD_INSNL(ret, line_node, jump, next_label);
8567 if (tmp_label) ADD_LABEL(ret, tmp_label);
8569 ADD_LABEL(ret, redo_label);
8570 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8572 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8573 add_trace_branch_coverage(
8576 nd_code_loc(coverage_node),
8577 nd_node_id(coverage_node),
8582 CHECK(COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body));
8583 ADD_LABEL(ret, next_label);
8585 if (
type == NODE_WHILE) {
8586 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8587 redo_label, end_label));
8591 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8592 end_label, redo_label));
8595 ADD_LABEL(ret, end_label);
8596 ADD_ADJUST_RESTORE(ret, adjust_label);
8598 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8600 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8604 ADD_INSN(ret, line_node, putnil);
8607 ADD_LABEL(ret, break_label);
8610 ADD_INSN(ret, line_node, pop);
8613 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8615 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8617 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8618 ISEQ_COMPILE_DATA(iseq)->redo_label);
8620 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8621 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8622 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8623 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8624 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
8631 const int line = nd_line(node);
8632 const NODE *line_node = node;
8633 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8634 LABEL *retry_label = NEW_LABEL(line);
8635 LABEL *retry_end_l = NEW_LABEL(line);
8638 ADD_LABEL(ret, retry_label);
8639 if (nd_type_p(node, NODE_FOR)) {
8640 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8642 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8643 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8644 ISEQ_TYPE_BLOCK, line);
8645 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8648 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8649 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8650 ISEQ_TYPE_BLOCK, line);
8651 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8665 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
8666 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8667 iobj = (
INSN*) get_prev_insn(iobj);
8669 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
8673 if (&iobj->link == LAST_ELEMENT(ret)) {
8679 ADD_INSN(ret, line_node, pop);
8682 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8684 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8694 const NODE *line_node = node;
8695 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8696 LABEL *not_single = NEW_LABEL(nd_line(var));
8697 LABEL *not_ary = NEW_LABEL(nd_line(var));
8698 CHECK(COMPILE(ret,
"for var", var));
8699 ADD_INSN(ret, line_node, dup);
8700 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8701 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8702 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8703 ADD_INSNL(ret, line_node, branchunless, not_single);
8704 ADD_INSN(ret, line_node, dup);
8705 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8706 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8707 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8708 ADD_INSN(ret, line_node, swap);
8709 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
8710 ADD_INSN(ret, line_node, dup);
8711 ADD_INSNL(ret, line_node, branchunless, not_ary);
8712 ADD_INSN(ret, line_node, swap);
8713 ADD_LABEL(ret, not_ary);
8714 ADD_INSN(ret, line_node, pop);
8715 ADD_LABEL(ret, not_single);
8722 const NODE *line_node = node;
8723 unsigned long throw_flag = 0;
8725 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8727 LABEL *splabel = NEW_LABEL(0);
8728 ADD_LABEL(ret, splabel);
8729 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8730 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8731 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8732 add_ensure_iseq(ret, iseq, 0);
8733 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8734 ADD_ADJUST_RESTORE(ret, splabel);
8737 ADD_INSN(ret, line_node, putnil);
8744 if (!ISEQ_COMPILE_DATA(ip)) {
8749 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8750 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8752 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8755 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8756 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8760 ip = ISEQ_BODY(ip)->parent_iseq;
8765 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8766 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8768 ADD_INSN(ret, line_node, pop);
8772 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8781 const NODE *line_node = node;
8782 unsigned long throw_flag = 0;
8784 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8785 LABEL *splabel = NEW_LABEL(0);
8786 debugs(
"next in while loop\n");
8787 ADD_LABEL(ret, splabel);
8788 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8789 add_ensure_iseq(ret, iseq, 0);
8790 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8791 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8792 ADD_ADJUST_RESTORE(ret, splabel);
8794 ADD_INSN(ret, line_node, putnil);
8797 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8798 LABEL *splabel = NEW_LABEL(0);
8799 debugs(
"next in block\n");
8800 ADD_LABEL(ret, splabel);
8801 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8802 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8803 add_ensure_iseq(ret, iseq, 0);
8804 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8805 ADD_ADJUST_RESTORE(ret, splabel);
8808 ADD_INSN(ret, line_node, putnil);
8815 if (!ISEQ_COMPILE_DATA(ip)) {
8820 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8821 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8825 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8828 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8829 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8833 ip = ISEQ_BODY(ip)->parent_iseq;
8836 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8837 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8840 ADD_INSN(ret, line_node, pop);
8844 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
8854 const NODE *line_node = node;
8856 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8857 LABEL *splabel = NEW_LABEL(0);
8858 debugs(
"redo in while");
8859 ADD_LABEL(ret, splabel);
8860 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8861 add_ensure_iseq(ret, iseq, 0);
8862 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8863 ADD_ADJUST_RESTORE(ret, splabel);
8865 ADD_INSN(ret, line_node, putnil);
8868 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8869 LABEL *splabel = NEW_LABEL(0);
8871 debugs(
"redo in block");
8872 ADD_LABEL(ret, splabel);
8873 add_ensure_iseq(ret, iseq, 0);
8874 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8875 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8876 ADD_ADJUST_RESTORE(ret, splabel);
8879 ADD_INSN(ret, line_node, putnil);
8886 if (!ISEQ_COMPILE_DATA(ip)) {
8891 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8894 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8897 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8898 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
8902 ip = ISEQ_BODY(ip)->parent_iseq;
8905 ADD_INSN(ret, line_node, putnil);
8906 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8909 ADD_INSN(ret, line_node, pop);
8913 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
8923 const NODE *line_node = node;
8925 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
8926 ADD_INSN(ret, line_node, putnil);
8927 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
8930 ADD_INSN(ret, line_node, pop);
8934 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
8943 const int line = nd_line(node);
8944 const NODE *line_node = node;
8945 LABEL *lstart = NEW_LABEL(line);
8946 LABEL *lend = NEW_LABEL(line);
8947 LABEL *lcont = NEW_LABEL(line);
8948 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
8950 ISEQ_BODY(iseq)->location.label),
8951 ISEQ_TYPE_RESCUE, line);
8953 lstart->rescued = LABEL_RESCUE_BEG;
8954 lend->rescued = LABEL_RESCUE_END;
8955 ADD_LABEL(ret, lstart);
8957 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
8958 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
8960 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
8962 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
8964 ADD_LABEL(ret, lend);
8965 if (RNODE_RESCUE(node)->nd_else) {
8966 ADD_INSN(ret, line_node, pop);
8967 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
8969 ADD_INSN(ret, line_node, nop);
8970 ADD_LABEL(ret, lcont);
8973 ADD_INSN(ret, line_node, pop);
8977 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
8978 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
8985 const int line = nd_line(node);
8986 const NODE *line_node = node;
8987 const NODE *resq = node;
8989 LABEL *label_miss, *label_hit;
8992 label_miss = NEW_LABEL(line);
8993 label_hit = NEW_LABEL(line);
8995 narg = RNODE_RESBODY(resq)->nd_args;
8997 switch (nd_type(narg)) {
9000 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9001 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
9002 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
9003 ADD_INSNL(ret, line_node, branchif, label_hit);
9004 narg = RNODE_LIST(narg)->nd_next;
9010 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9011 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
9012 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
9013 ADD_INSNL(ret, line_node, branchif, label_hit);
9016 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
9020 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9022 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
9023 ADD_INSNL(ret, line_node, branchif, label_hit);
9025 ADD_INSNL(ret, line_node, jump, label_miss);
9026 ADD_LABEL(ret, label_hit);
9029 if (RNODE_RESBODY(resq)->nd_exc_var) {
9030 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
9033 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) {
9035 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
9038 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
9041 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
9042 ADD_INSN(ret, line_node, nop);
9044 ADD_INSN(ret, line_node, leave);
9045 ADD_LABEL(ret, label_miss);
9046 resq = RNODE_RESBODY(resq)->nd_next;
9054 const int line = nd_line(RNODE_ENSURE(node)->nd_ensr);
9055 const NODE *line_node = node;
9057 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
9059 ISEQ_TYPE_ENSURE, line);
9060 LABEL *lstart = NEW_LABEL(line);
9061 LABEL *lend = NEW_LABEL(line);
9062 LABEL *lcont = NEW_LABEL(line);
9070 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
9072 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
9077 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
9079 ADD_LABEL(ret, lstart);
9080 CHECK(COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
9081 ADD_LABEL(ret, lend);
9083 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
9084 ADD_LABEL(ret, lcont);
9085 if (last_leave) ADD_INSN(ret, line_node, pop);
9087 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
9088 if (lstart->link.next != &lend->link) {
9090 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
9092 erange = erange->next;
9096 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
9103 const NODE *line_node = node;
9106 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
9108 enum rb_iseq_type t =
type;
9109 const NODE *retval = RNODE_RETURN(node)->nd_stts;
9112 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
9113 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
9114 t = ISEQ_BODY(is)->type;
9118 case ISEQ_TYPE_MAIN:
9120 rb_warn(
"argument of top-level return is ignored");
9124 type = ISEQ_TYPE_METHOD;
9131 if (
type == ISEQ_TYPE_METHOD) {
9132 splabel = NEW_LABEL(0);
9133 ADD_LABEL(ret, splabel);
9134 ADD_ADJUST(ret, line_node, 0);
9137 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
9139 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
9140 add_ensure_iseq(ret, iseq, 1);
9142 ADD_INSN(ret, line_node, leave);
9143 ADD_ADJUST_RESTORE(ret, splabel);
9146 ADD_INSN(ret, line_node, putnil);
9150 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
9152 ADD_INSN(ret, line_node, pop);
9163 if (!i)
return false;
9164 if (IS_TRACE(i)) i = i->prev;
9165 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
9167 if (IS_ADJUST(i)) i = i->prev;
9168 if (!IS_INSN(i))
return false;
9169 switch (INSN_OF(i)) {
9176 (ret->last = last->prev)->next = NULL;
9183 CHECK(COMPILE_(ret,
"nd_body", node, popped));
9185 if (!popped && !all_string_result_p(node)) {
9186 const NODE *line_node = node;
9187 const unsigned int flag = VM_CALL_FCALL;
9191 ADD_INSN(ret, line_node, dup);
9192 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
9193 ADD_INSN(ret, line_node, anytostring);
9201 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
9203 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
9204 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
9210 LABEL *else_label = NEW_LABEL(nd_line(line_node));
9213 br = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"&.");
9215 ADD_INSN(recv, line_node, dup);
9216 ADD_INSNL(recv, line_node, branchnil, else_label);
9217 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
9225 if (!else_label)
return;
9226 end_label = NEW_LABEL(nd_line(line_node));
9227 ADD_INSNL(ret, line_node, jump, end_label);
9228 ADD_LABEL(ret, else_label);
9229 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
9230 ADD_LABEL(ret, end_label);
9239 if (get_nd_recv(node) &&
9240 (nd_type_p(get_nd_recv(node), NODE_STR) || nd_type_p(get_nd_recv(node), NODE_FILE)) &&
9241 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
9242 get_nd_args(node) == NULL &&
9243 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9244 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
9245 VALUE str = get_string_value(get_nd_recv(node));
9246 if (get_node_call_nd_mid(node) == idUMinus) {
9247 ADD_INSN2(ret, line_node, opt_str_uminus, str,
9248 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
9251 ADD_INSN2(ret, line_node, opt_str_freeze, str,
9252 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
9256 ADD_INSN(ret, line_node, pop);
9264iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
9266 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
9270iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
9273 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
9274 for (i=0; table[i].index != -1; i++) {
9275 if (strcmp(table[i].name, name) == 0) {
9283iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
9285 const char *name = rb_id2name(mid);
9286 static const char prefix[] =
"__builtin_";
9287 const size_t prefix_len =
sizeof(prefix) - 1;
9292 switch (nd_type(recv)) {
9294 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
9299 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
9309 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
9310 return &name[prefix_len];
9319delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
9326 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
9327 unsigned int start=0;
9332 argc + start <= ISEQ_BODY(iseq)->local_table_size;
9336 for (
unsigned int i=start; i-start<argc; i++) {
9337 if (IS_INSN(elem) &&
9338 INSN_OF(elem) == BIN(getlocal)) {
9339 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
9340 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
9342 if (local_level == 0) {
9343 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
9345 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
9346 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
9347 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
9348 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9372 *pstart_index = start;
9384 if (rb_streql_lit(
string,
"leaf")) {
9385 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9387 else if (rb_streql_lit(
string,
"inline_block")) {
9388 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9390 else if (rb_streql_lit(
string,
"use_block")) {
9391 iseq_set_use_block(iseq);
9393 else if (rb_streql_lit(
string,
"c_trace")) {
9395 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9397 else if (rb_streql_lit(
string,
"without_interrupts")) {
9398 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_WITHOUT_INTERRUPTS;
9400 else if (rb_streql_lit(
string,
"caller_user_box")) {
9401 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_CALLER_USER_BOX;
9414 if (!node)
goto no_arg;
9416 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9417 const NODE *next = RNODE_LIST(node)->nd_next;
9419 node = RNODE_LIST(node)->nd_head;
9420 if (!node)
goto no_arg;
9421 switch (nd_type(node)) {
9423 symbol = rb_node_sym_string_val(node);
9429 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9430 if (compile_builtin_attr_symbol(iseq, symbol) != COMPILE_OK)
goto unknown_arg;
9435 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9438 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9441 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %" PRIsVALUE, symbol);
9444 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9452 if (!node)
goto no_arg;
9453 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9454 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9455 node = RNODE_LIST(node)->nd_head;
9456 if (!node)
goto no_arg;
9457 switch (nd_type(node)) {
9459 name = rb_node_sym_string_val(node);
9464 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9466 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9470 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9473 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9476 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9477 rb_builtin_class_name(name));
9480 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9486 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9487 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9488 return RNODE_IF(node)->nd_body;
9491 rb_bug(
"mandatory_node: can't find mandatory node");
9496compile_builtin_mandatory_only_method(
rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9500 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9503 rb_node_init(RNODE(&args_node), NODE_ARGS);
9504 args_node.nd_ainfo = args;
9507 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9508 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9512 tbl->size = table_size;
9517 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9518 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9521 for (; i<table_size; i++) {
9522 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9526 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9527 scope_node.nd_tbl = tbl;
9528 scope_node.nd_body = mandatory_node(iseq, node);
9529 scope_node.nd_parent = NULL;
9530 scope_node.nd_args = &args_node;
9532 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9535 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9536 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9537 nd_line(line_node), NULL, 0,
9538 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9539 ISEQ_SCRIPT_LINES(iseq));
9540 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
9550 NODE *args_node = get_nd_args(node);
9552 if (parent_block != NULL) {
9553 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9557# define BUILTIN_INLINE_PREFIX "_bi"
9558 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9559 bool cconst =
false;
9564 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9565 strcmp(
"cexpr!", builtin_func) == 0) {
9568 else if (strcmp(
"cconst!", builtin_func) == 0) {
9571 else if (strcmp(
"cinit!", builtin_func) == 0) {
9575 else if (strcmp(
"attr!", builtin_func) == 0) {
9576 return compile_builtin_attr(iseq, args_node);
9578 else if (strcmp(
"arg!", builtin_func) == 0) {
9579 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9581 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9583 rb_bug(
"mandatory_only? should be in if condition");
9585 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9586 rb_bug(
"mandatory_only? should be put on top");
9589 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9590 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9592 else if (strcmp(
"local_self!", builtin_func) == 0) {
9597 compile_lvar(iseq, ret, line_node, id_self);
9601 rb_bug(
"can't find builtin function:%s", builtin_func);
9604 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9608 int inline_index = nd_line(node);
9609 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9610 builtin_func = inline_func;
9616 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9617 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9618 ADD_INSN1(ret, line_node, putobject, const_val);
9624 unsigned int flag = 0;
9626 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9628 if (
FIX2INT(argc) != bf->argc) {
9629 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9630 builtin_func, bf->argc,
FIX2INT(argc));
9634 unsigned int start_index;
9635 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9636 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9640 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9643 if (popped) ADD_INSN(ret, line_node, pop);
9649compile_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)
9657 ID mid = get_node_call_nd_mid(node);
9659 unsigned int flag = 0;
9661 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9662 LABEL *else_label = NULL;
9665 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9671 if (nd_type_p(node, NODE_VCALL)) {
9676 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9678 if (mid == id_bitblt) {
9679 ADD_INSN(ret, line_node, bitblt);
9682 else if (mid == id_answer) {
9683 ADD_INSN(ret, line_node, answer);
9695 if (nd_type_p(node, NODE_FCALL) &&
9696 (mid == goto_id || mid == label_id)) {
9699 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9702 if (!labels_table) {
9703 labels_table = st_init_numtable();
9704 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9707 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9711 if (mid == goto_id) {
9712 ADD_INSNL(ret, line_node, jump, label);
9715 ADD_LABEL(ret, label);
9722 const char *builtin_func;
9723 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9724 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9725 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9729 if (!assume_receiver) {
9730 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9733 if ((mid == idCall || mid == idAREF || mid == idYield || mid == idEqq) &&
9734 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9735 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9736 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9738 else if (private_recv_p(node)) {
9739 ADD_INSN(recv, node, putself);
9740 flag |= VM_CALL_FCALL;
9743 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9746 if (
type == NODE_QCALL) {
9747 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9750 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9751 ADD_CALL_RECEIVER(recv, line_node);
9756 if (
type != NODE_VCALL) {
9757 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9758 CHECK(!
NIL_P(argc));
9766 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
9767 mid == rb_intern(
"new") &&
9768 parent_block == NULL &&
9769 !(flag & VM_CALL_ARGS_BLOCKARG);
9772 ADD_INSN(ret, node, putnil);
9773 ADD_INSN(ret, node, swap);
9778 debugp_param(
"call args argc", argc);
9779 debugp_param(
"call method",
ID2SYM(mid));
9781 switch ((
int)
type) {
9783 flag |= VM_CALL_VCALL;
9786 flag |= VM_CALL_FCALL;
9789 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9790 ADD_INSN(ret, line_node, splatkw);
9793 LABEL *not_basic_new = NEW_LABEL(nd_line(node));
9794 LABEL *not_basic_new_finish = NEW_LABEL(nd_line(node));
9799 if (flag & VM_CALL_FORWARDING) {
9800 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc) + 1, flag, keywords, 0);
9803 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc), flag, keywords, 0);
9805 ADD_INSN2(ret, node, opt_new, ci, not_basic_new);
9806 LABEL_REF(not_basic_new);
9809 ADD_SEND_R(ret, line_node, rb_intern(
"initialize"), argc, parent_block,
INT2FIX(flag | VM_CALL_FCALL), keywords);
9810 ADD_INSNL(ret, line_node, jump, not_basic_new_finish);
9812 ADD_LABEL(ret, not_basic_new);
9814 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9815 ADD_INSN(ret, line_node, swap);
9817 ADD_LABEL(ret, not_basic_new_finish);
9818 ADD_INSN(ret, line_node, pop);
9821 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9824 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9826 ADD_INSN(ret, line_node, pop);
9834 const int line = nd_line(node);
9836 unsigned int flag = 0;
9838 ID id = RNODE_OP_ASGN1(node)->nd_mid;
9864 ADD_INSN(ret, node, putnil);
9866 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
9867 CHECK(asgnflag != -1);
9868 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
9873 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
9874 CHECK(!
NIL_P(argc));
9876 int dup_argn =
FIX2INT(argc) + 1;
9877 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
9879 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
9881 if (
id == idOROP ||
id == idANDOP) {
9890 LABEL *label = NEW_LABEL(line);
9891 LABEL *lfin = NEW_LABEL(line);
9893 ADD_INSN(ret, node, dup);
9895 ADD_INSNL(ret, node, branchif, label);
9898 ADD_INSNL(ret, node, branchunless, label);
9900 ADD_INSN(ret, node, pop);
9902 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9904 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9906 if (flag & VM_CALL_ARGS_SPLAT) {
9907 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9908 ADD_INSN(ret, node, swap);
9909 ADD_INSN1(ret, node, splatarray,
Qtrue);
9910 ADD_INSN(ret, node, swap);
9911 flag |= VM_CALL_ARGS_SPLAT_MUT;
9913 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9914 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9917 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9919 ADD_INSN(ret, node, pop);
9920 ADD_INSNL(ret, node, jump, lfin);
9921 ADD_LABEL(ret, label);
9923 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9925 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
9926 ADD_LABEL(ret, lfin);
9929 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9930 ADD_SEND(ret, node,
id,
INT2FIX(1));
9932 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9934 if (flag & VM_CALL_ARGS_SPLAT) {
9935 if (flag & VM_CALL_KW_SPLAT) {
9936 ADD_INSN1(ret, node, topn,
INT2FIX(2));
9937 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9938 ADD_INSN1(ret, node, splatarray,
Qtrue);
9939 flag |= VM_CALL_ARGS_SPLAT_MUT;
9941 ADD_INSN(ret, node, swap);
9942 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9943 ADD_INSN1(ret, node, setn,
INT2FIX(2));
9944 ADD_INSN(ret, node, pop);
9947 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9948 ADD_INSN(ret, node, swap);
9949 ADD_INSN1(ret, node, splatarray,
Qtrue);
9950 ADD_INSN(ret, node, swap);
9951 flag |= VM_CALL_ARGS_SPLAT_MUT;
9953 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9955 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9958 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9960 ADD_INSN(ret, node, pop);
9968 const int line = nd_line(node);
9969 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
9970 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
9972 LABEL *lfin = NEW_LABEL(line);
9973 LABEL *lcfin = NEW_LABEL(line);
10028 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
10029 CHECK(asgnflag != -1);
10030 if (RNODE_OP_ASGN2(node)->nd_aid) {
10031 lskip = NEW_LABEL(line);
10032 ADD_INSN(ret, node, dup);
10033 ADD_INSNL(ret, node, branchnil, lskip);
10035 ADD_INSN(ret, node, dup);
10036 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
10038 if (atype == idOROP || atype == idANDOP) {
10040 ADD_INSN(ret, node, dup);
10042 if (atype == idOROP) {
10043 ADD_INSNL(ret, node, branchif, lcfin);
10046 ADD_INSNL(ret, node, branchunless, lcfin);
10049 ADD_INSN(ret, node, pop);
10051 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
10053 ADD_INSN(ret, node, swap);
10054 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10056 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
10057 ADD_INSNL(ret, node, jump, lfin);
10059 ADD_LABEL(ret, lcfin);
10061 ADD_INSN(ret, node, swap);
10064 ADD_LABEL(ret, lfin);
10067 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
10068 ADD_SEND(ret, node, atype,
INT2FIX(1));
10070 ADD_INSN(ret, node, swap);
10071 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10073 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
10075 if (lskip && popped) {
10076 ADD_LABEL(ret, lskip);
10078 ADD_INSN(ret, node, pop);
10079 if (lskip && !popped) {
10080 ADD_LABEL(ret, lskip);
10085static int compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
10090 const int line = nd_line(node);
10092 LABEL *lassign = 0;
10095 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
10097 ADD_INSN1(ret, node, putobject,
rb_cObject);
10100 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
10103 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
10104 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
10107 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
10109 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
10110 lassign = NEW_LABEL(line);
10111 ADD_INSN(ret, node, dup);
10112 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
10114 ADD_INSNL(ret, node, branchunless, lassign);
10116 ADD_INSN(ret, node, dup);
10117 ADD_INSN1(ret, node, putobject,
Qtrue);
10118 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
10120 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
10121 lfin = NEW_LABEL(line);
10122 if (!popped) ADD_INSN(ret, node, dup);
10123 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
10124 ADD_INSNL(ret, node, branchif, lfin);
10126 ADD_INSNL(ret, node, branchunless, lfin);
10128 if (!popped) ADD_INSN(ret, node, pop);
10129 if (lassign) ADD_LABEL(ret, lassign);
10130 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
10133 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10135 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
10136 ADD_INSN(ret, node, swap);
10138 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
10139 ADD_LABEL(ret, lfin);
10140 if (!popped) ADD_INSN(ret, node, swap);
10141 ADD_INSN(ret, node, pop);
10144 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
10146 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
10148 ADD_INSN(ret, node, swap);
10150 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10151 ADD_INSN(ret, node, swap);
10153 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
10161 const int line = nd_line(node);
10162 LABEL *lfin = NEW_LABEL(line);
10165 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
10169 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
10170 lassign = lfinish[1];
10172 lassign = NEW_LABEL(line);
10174 ADD_INSNL(ret, node, branchunless, lassign);
10177 lassign = NEW_LABEL(line);
10180 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
10183 ADD_INSN(ret, node, dup);
10186 if (
type == NODE_OP_ASGN_AND) {
10187 ADD_INSNL(ret, node, branchunless, lfin);
10190 ADD_INSNL(ret, node, branchif, lfin);
10194 ADD_INSN(ret, node, pop);
10197 ADD_LABEL(ret, lassign);
10198 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
10199 ADD_LABEL(ret, lfin);
10209 unsigned int flag = 0;
10211 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
10215 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
10217 if (
type == NODE_SUPER) {
10218 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
10219 CHECK(!
NIL_P(vargc));
10221 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
10222 ADD_INSN(args, node, splatkw);
10225 if (flag & VM_CALL_ARGS_BLOCKARG) {
10232 const rb_iseq_t *liseq = body->local_iseq;
10234 const struct rb_iseq_param_keyword *
const local_kwd = local_body->param.keyword;
10235 int lvar_level = get_lvar_level(iseq);
10237 argc = local_body->param.lead_num;
10240 for (i = 0; i < local_body->param.lead_num; i++) {
10241 int idx = local_body->local_table_size - i;
10242 ADD_GETLOCAL(args, node, idx, lvar_level);
10246 if (local_body->param.flags.forwardable) {
10247 flag |= VM_CALL_FORWARDING;
10248 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
10249 ADD_GETLOCAL(args, node, idx, lvar_level);
10252 if (local_body->param.flags.has_opt) {
10255 for (j = 0; j < local_body->param.opt_num; j++) {
10256 int idx = local_body->local_table_size - (i + j);
10257 ADD_GETLOCAL(args, node, idx, lvar_level);
10262 if (local_body->param.flags.has_rest) {
10264 int idx = local_body->local_table_size - local_body->param.rest_start;
10265 ADD_GETLOCAL(args, node, idx, lvar_level);
10266 ADD_INSN1(args, node, splatarray, RBOOL(local_body->param.flags.has_post));
10268 argc = local_body->param.rest_start + 1;
10269 flag |= VM_CALL_ARGS_SPLAT;
10271 if (local_body->param.flags.has_post) {
10273 int post_len = local_body->param.post_num;
10274 int post_start = local_body->param.post_start;
10276 if (local_body->param.flags.has_rest) {
10278 for (j=0; j<post_len; j++) {
10279 int idx = local_body->local_table_size - (post_start + j);
10280 ADD_GETLOCAL(args, node, idx, lvar_level);
10282 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
10283 flag |= VM_CALL_ARGS_SPLAT_MUT;
10288 for (j=0; j<post_len; j++) {
10289 int idx = local_body->local_table_size - (post_start + j);
10290 ADD_GETLOCAL(args, node, idx, lvar_level);
10292 argc = post_len + post_start;
10296 if (local_body->param.flags.has_kw) {
10297 int local_size = local_body->local_table_size;
10300 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10302 if (local_body->param.flags.has_kwrest) {
10303 int idx = local_body->local_table_size - local_kwd->rest_start;
10304 ADD_GETLOCAL(args, node, idx, lvar_level);
10306 ADD_SEND (args, node, rb_intern(
"dup"),
INT2FIX(0));
10309 ADD_INSN1(args, node, newhash,
INT2FIX(0));
10311 for (i = 0; i < local_kwd->num; ++i) {
10312 ID id = local_kwd->table[i];
10313 int idx = local_size - get_local_var_idx(liseq,
id);
10314 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
10315 ADD_GETLOCAL(args, node, idx, lvar_level);
10317 ADD_SEND(args, node, id_core_hash_merge_bang_ptr,
INT2FIX(i * 2 + 1));
10318 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
10320 else if (local_body->param.flags.has_kwrest) {
10321 int idx = local_body->local_table_size - local_kwd->rest_start;
10322 ADD_GETLOCAL(args, node, idx, lvar_level);
10324 flag |= VM_CALL_KW_SPLAT;
10328 if (use_block && parent_block == NULL) {
10329 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10332 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
10333 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
10334 ADD_INSN(ret, node, putself);
10335 ADD_SEQ(ret, args);
10337 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
10339 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
10340 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
10343 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
10347 ADD_INSN(ret, node, pop);
10357 unsigned int flag = 0;
10362 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
10363 case ISEQ_TYPE_TOP:
10364 case ISEQ_TYPE_MAIN:
10365 case ISEQ_TYPE_CLASS:
10366 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
10371 if (RNODE_YIELD(node)->nd_head) {
10372 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
10373 CHECK(!
NIL_P(argc));
10379 ADD_SEQ(ret, args);
10380 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
10381 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10384 ADD_INSN(ret, node, pop);
10389 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
10390 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
10392 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
10405 switch ((
int)
type) {
10408 VALUE re = rb_node_regx_string_val(node);
10410 ADD_INSN1(recv, node, putobject, re);
10411 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
10416 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
10417 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
10420 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
10421 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
10425 ADD_SEQ(ret, recv);
10427 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
10429 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
10430 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
10434 ADD_INSN(ret, node, pop);
10445 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10446 (segments = collect_const_segments(iseq, node))) {
10447 ISEQ_BODY(iseq)->ic_size++;
10448 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10458 CHECK(compile_const_prefix(iseq, node, pref, body));
10459 if (LIST_INSN_SIZE_ZERO(pref)) {
10460 ADD_INSN(ret, node, putnil);
10461 ADD_SEQ(ret, body);
10464 ADD_SEQ(ret, pref);
10465 ADD_SEQ(ret, body);
10471 ADD_CALL_RECEIVER(ret, node);
10472 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10473 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10476 ADD_INSN(ret, node, pop);
10484 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10487 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10488 ISEQ_BODY(iseq)->ic_size++;
10489 VALUE segments = rb_ary_new_from_args(2,
ID2SYM(idNULL),
ID2SYM(RNODE_COLON3(node)->nd_mid));
10491 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10495 ADD_INSN1(ret, node, putobject,
rb_cObject);
10496 ADD_INSN1(ret, node, putobject,
Qtrue);
10497 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10501 ADD_INSN(ret, node, pop);
10510 const NODE *b = RNODE_DOT2(node)->nd_beg;
10511 const NODE *e = RNODE_DOT2(node)->nd_end;
10513 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10515 VALUE bv = optimized_range_item(b);
10516 VALUE ev = optimized_range_item(e);
10519 ADD_INSN1(ret, node, putobject, val);
10524 CHECK(COMPILE_(ret,
"min", b, popped));
10525 CHECK(COMPILE_(ret,
"max", e, popped));
10527 ADD_INSN1(ret, node, newrange, flag);
10537 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10538 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10544 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10547 ip = ISEQ_BODY(ip)->parent_iseq;
10551 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10554 ADD_INSN(ret, node, putnil);
10565 LABEL *end_label = NEW_LABEL(nd_line(node));
10566 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10568 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10570 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10573 else if (nd_type_p(default_value, NODE_SYM) ||
10574 nd_type_p(default_value, NODE_REGX) ||
10575 nd_type_p(default_value, NODE_LINE) ||
10576 nd_type_p(default_value, NODE_INTEGER) ||
10577 nd_type_p(default_value, NODE_FLOAT) ||
10578 nd_type_p(default_value, NODE_RATIONAL) ||
10579 nd_type_p(default_value, NODE_IMAGINARY) ||
10580 nd_type_p(default_value, NODE_NIL) ||
10581 nd_type_p(default_value, NODE_TRUE) ||
10582 nd_type_p(default_value, NODE_FALSE)) {
10583 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10591 int kw_bits_idx = body->local_table_size - body->param.keyword->bits_start;
10592 int keyword_idx = body->param.keyword->num;
10594 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10595 ADD_INSNL(ret, node, branchif, end_label);
10596 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10597 ADD_LABEL(ret, end_label);
10607 unsigned int flag = 0;
10608 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10610 LABEL *else_label = NULL;
10615 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10616 CHECK(!
NIL_P(argc));
10618 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10619 CHECK(asgnflag != -1);
10620 flag |= (
unsigned int)asgnflag;
10622 debugp_param(
"argc", argc);
10623 debugp_param(
"nd_mid",
ID2SYM(mid));
10627 mid = rb_id_attrset(mid);
10628 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10631 ADD_INSN(ret, node, putnil);
10632 ADD_SEQ(ret, recv);
10633 ADD_SEQ(ret, args);
10635 if (flag & VM_CALL_ARGS_SPLAT) {
10636 ADD_INSN(ret, node, dup);
10637 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10638 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10639 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10640 ADD_INSN (ret, node, pop);
10643 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10647 ADD_SEQ(ret, recv);
10648 ADD_SEQ(ret, args);
10650 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10651 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10652 ADD_INSN(ret, node, pop);
10659 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10667 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10674 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10681node_const_decl_val(
const NODE *node)
10684 switch (nd_type(node)) {
10686 if (RNODE_CDECL(node)->nd_vid) {
10687 path = rb_id2str(RNODE_CDECL(node)->nd_vid);
10691 node = RNODE_CDECL(node)->nd_else;
10699 rb_str_append(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10702 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10708 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10709 rb_ary_push(path, rb_id2str(RNODE_COLON2(node)->nd_mid));
10711 if (node && nd_type_p(node, NODE_CONST)) {
10713 rb_ary_push(path, rb_id2str(RNODE_CONST(node)->nd_vid));
10715 else if (node && nd_type_p(node, NODE_COLON3)) {
10717 rb_ary_push(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10727 path = rb_fstring(path);
10732const_decl_path(
NODE *dest)
10735 if (!nd_type_p(dest, NODE_CALL)) {
10736 path = node_const_decl_val(dest);
10747 VALUE path = const_decl_path(dest);
10748 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10749 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10750 ADD_INSN1(ret, value, putobject, path);
10752 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10757#ifndef SHAREABLE_BARE_EXPRESSION
10758#define SHAREABLE_BARE_EXPRESSION 1
10762compile_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)
10764# define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10765 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10767 DECL_ANCHOR(anchor);
10769 enum node_type
type = node ? nd_type(node) : NODE_NIL;
10781 *value_p = rb_node_sym_string_val(node);
10784 *value_p = rb_node_regx_string_val(node);
10787 *value_p = rb_node_line_lineno_val(node);
10790 *value_p = rb_node_integer_literal_val(node);
10793 *value_p = rb_node_float_literal_val(node);
10795 case NODE_RATIONAL:
10796 *value_p = rb_node_rational_literal_val(node);
10798 case NODE_IMAGINARY:
10799 *value_p = rb_node_imaginary_literal_val(node);
10801 case NODE_ENCODING:
10802 *value_p = rb_node_encoding_val(node);
10805 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10806 *shareable_literal_p = 1;
10810 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10811 if (shareable == rb_parser_shareable_literal) {
10817 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10820 *shareable_literal_p = 1;
10824 VALUE lit = rb_node_str_string_val(node);
10825 ADD_INSN1(ret, node, putobject, lit);
10828 *shareable_literal_p = 1;
10834 VALUE lit = rb_node_file_path_val(node);
10835 ADD_INSN1(ret, node, putobject, lit);
10838 *shareable_literal_p = 1;
10846 ADD_INSN1(ret, node, putobject, lit);
10849 *shareable_literal_p = 1;
10855 INIT_ANCHOR(anchor);
10857 for (
NODE *n = (
NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
10859 int shareable_literal_p2;
10860 NODE *elt = RNODE_LIST(n)->nd_head;
10862 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
10863 if (shareable_literal_p2) {
10866 else if (
RTEST(lit)) {
10872 if (!UNDEF_P(val)) {
10884 if (!RNODE_HASH(node)->nd_brace) {
10886 *shareable_literal_p = 0;
10889 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10890 if (!RNODE_LIST(n)->nd_head) {
10892 goto compile_shareable;
10896 INIT_ANCHOR(anchor);
10897 lit = rb_hash_new();
10898 for (
NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10900 VALUE value_val = 0;
10901 int shareable_literal_p2;
10902 NODE *key = RNODE_LIST(n)->nd_head;
10903 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
10904 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10905 if (shareable_literal_p2) {
10908 else if (
RTEST(lit)) {
10909 rb_hash_clear(lit);
10912 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10913 if (shareable_literal_p2) {
10916 else if (
RTEST(lit)) {
10917 rb_hash_clear(lit);
10921 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
10922 rb_hash_aset(lit, key_val, value_val);
10925 rb_hash_clear(lit);
10936 if (shareable == rb_parser_shareable_literal &&
10937 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
10938 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
10940 *shareable_literal_p = 1;
10943 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10945 *shareable_literal_p = 0;
10951 if (nd_type(node) == NODE_LIST) {
10952 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10954 else if (nd_type(node) == NODE_HASH) {
10955 long len = RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10961 *shareable_literal_p = 0;
10962 ADD_SEQ(ret, anchor);
10968 if (nd_type(node) == NODE_LIST) {
10969 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10971 else if (nd_type(node) == NODE_HASH) {
10972 long len = RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10977 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
10979 *shareable_literal_p = 1;
10983 ADD_INSN1(ret, node, putobject, val);
10986 *shareable_literal_p = 1;
10993compile_shareable_constant_value(
rb_iseq_t *iseq,
LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
10997 DECL_ANCHOR(anchor);
10998 INIT_ANCHOR(anchor);
11000 switch (shareable) {
11001 case rb_parser_shareable_none:
11002 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
11005 case rb_parser_shareable_literal:
11006 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
11007 ADD_SEQ(ret, anchor);
11010 case rb_parser_shareable_copy:
11011 case rb_parser_shareable_everything:
11012 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (
NODE *)lhs, value, 0, &val, &literal_p));
11014 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
11017 ADD_SEQ(ret, anchor);
11021 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
11038 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
11039 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
11040 debugs(
"node: NODE_NIL(implicit)\n");
11041 ADD_SYNTHETIC_INSN(ret, lineno, -1, putnil);
11045 return iseq_compile_each0(iseq, ret, node, popped);
11051 const int line = (int)nd_line(node);
11052 const enum node_type
type = nd_type(node);
11055 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
11059 if (nd_fl_newline(node)) {
11061 ISEQ_COMPILE_DATA(iseq)->last_line = line;
11062 if (line > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
11063 event |= RUBY_EVENT_COVERAGE_LINE;
11065 ADD_TRACE(ret, event);
11069 debug_node_start(node);
11070#undef BEFORE_RETURN
11071#define BEFORE_RETURN debug_node_end()
11075 CHECK(compile_block(iseq, ret, node, popped));
11079 CHECK(compile_if(iseq, ret, node, popped,
type));
11082 CHECK(compile_case(iseq, ret, node, popped));
11085 CHECK(compile_case2(iseq, ret, node, popped));
11088 CHECK(compile_case3(iseq, ret, node, popped));
11092 CHECK(compile_loop(iseq, ret, node, popped,
type));
11096 CHECK(compile_iter(iseq, ret, node, popped));
11098 case NODE_FOR_MASGN:
11099 CHECK(compile_for_masgn(iseq, ret, node, popped));
11102 CHECK(compile_break(iseq, ret, node, popped));
11105 CHECK(compile_next(iseq, ret, node, popped));
11108 CHECK(compile_redo(iseq, ret, node, popped));
11111 CHECK(compile_retry(iseq, ret, node, popped));
11114 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
11118 CHECK(compile_rescue(iseq, ret, node, popped));
11121 CHECK(compile_resbody(iseq, ret, node, popped));
11124 CHECK(compile_ensure(iseq, ret, node, popped));
11129 LABEL *end_label = NEW_LABEL(line);
11130 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(node)->nd_1st));
11132 ADD_INSN(ret, node, dup);
11134 if (
type == NODE_AND) {
11135 ADD_INSNL(ret, node, branchunless, end_label);
11138 ADD_INSNL(ret, node, branchif, end_label);
11141 ADD_INSN(ret, node, pop);
11143 CHECK(COMPILE_(ret,
"nd_2nd", RNODE_OR(node)->nd_2nd, popped));
11144 ADD_LABEL(ret, end_label);
11149 compile_massign(iseq, ret, node, popped);
11154 ID id = RNODE_LASGN(node)->nd_vid;
11155 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
11157 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
11158 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
11161 ADD_INSN(ret, node, dup);
11163 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
11168 ID id = RNODE_DASGN(node)->nd_vid;
11169 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
11170 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
11173 ADD_INSN(ret, node, dup);
11176 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
11179 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
11183 ADD_SETLOCAL(ret, node, ls - idx, lv);
11187 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
11190 ADD_INSN(ret, node, dup);
11192 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
11196 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
11198 ADD_INSN(ret, node, dup);
11200 ADD_INSN2(ret, node, setinstancevariable,
11201 ID2SYM(RNODE_IASGN(node)->nd_vid),
11202 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
11206 if (RNODE_CDECL(node)->nd_vid) {
11207 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11210 ADD_INSN(ret, node, dup);
11213 ADD_INSN1(ret, node, putspecialobject,
11214 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
11215 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
11218 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
11219 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11220 ADD_INSN(ret, node, swap);
11223 ADD_INSN1(ret, node, topn,
INT2FIX(1));
11224 ADD_INSN(ret, node, swap);
11227 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
11232 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
11234 ADD_INSN(ret, node, dup);
11236 ADD_INSN2(ret, node, setclassvariable,
11237 ID2SYM(RNODE_CVASGN(node)->nd_vid),
11238 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
11241 case NODE_OP_ASGN1:
11242 CHECK(compile_op_asgn1(iseq, ret, node, popped));
11244 case NODE_OP_ASGN2:
11245 CHECK(compile_op_asgn2(iseq, ret, node, popped));
11247 case NODE_OP_CDECL:
11248 CHECK(compile_op_cdecl(iseq, ret, node, popped));
11250 case NODE_OP_ASGN_AND:
11251 case NODE_OP_ASGN_OR:
11252 CHECK(compile_op_log(iseq, ret, node, popped,
type));
11256 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
11262 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
11268 CHECK(compile_super(iseq, ret, node, popped,
type));
11271 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
11276 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
11281 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
11284 CHECK(compile_return(iseq, ret, node, popped));
11287 CHECK(compile_yield(iseq, ret, node, popped));
11291 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
11297 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
11299 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
11301 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
11302 rb_id2str(RNODE_DVAR(node)->nd_vid));
11305 ADD_GETLOCAL(ret, node, ls - idx, lv);
11310 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
11312 ADD_INSN(ret, node, pop);
11317 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
11319 ADD_INSN2(ret, node, getinstancevariable,
11320 ID2SYM(RNODE_IVAR(node)->nd_vid),
11321 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
11326 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
11328 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
11330 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(RNODE_CONST(node)->nd_vid));
11332 ADD_INSN1(ret, node, opt_getconstant_path, segments);
11336 ADD_INSN(ret, node, putnil);
11337 ADD_INSN1(ret, node, putobject,
Qtrue);
11338 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
11342 ADD_INSN(ret, node, pop);
11348 ADD_INSN2(ret, node, getclassvariable,
11349 ID2SYM(RNODE_CVAR(node)->nd_vid),
11350 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
11354 case NODE_NTH_REF:{
11356 if (!RNODE_NTH_REF(node)->nd_nth) {
11357 ADD_INSN(ret, node, putnil);
11360 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11361 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
11365 case NODE_BACK_REF:{
11367 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11368 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
11375 CHECK(compile_match(iseq, ret, node, popped,
type));
11379 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
11385 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
11389 case NODE_ENCODING:{
11391 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
11395 case NODE_INTEGER:{
11396 VALUE lit = rb_node_integer_literal_val(node);
11398 debugp_param(
"integer", lit);
11400 ADD_INSN1(ret, node, putobject, lit);
11406 VALUE lit = rb_node_float_literal_val(node);
11408 debugp_param(
"float", lit);
11410 ADD_INSN1(ret, node, putobject, lit);
11415 case NODE_RATIONAL:{
11416 VALUE lit = rb_node_rational_literal_val(node);
11418 debugp_param(
"rational", lit);
11420 ADD_INSN1(ret, node, putobject, lit);
11425 case NODE_IMAGINARY:{
11426 VALUE lit = rb_node_imaginary_literal_val(node);
11428 debugp_param(
"imaginary", lit);
11430 ADD_INSN1(ret, node, putobject, lit);
11437 debugp_param(
"nd_lit", get_string_value(node));
11439 VALUE lit = get_string_value(node);
11442 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11443 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11446 switch (option->frozen_string_literal) {
11447 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11448 ADD_INSN1(ret, node, dupchilledstring, lit);
11450 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11451 ADD_INSN1(ret, node, dupstring, lit);
11453 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11454 ADD_INSN1(ret, node, putobject, lit);
11457 rb_bug(
"invalid frozen_string_literal");
11464 compile_dstr(iseq, ret, node);
11467 ADD_INSN(ret, node, pop);
11472 ADD_CALL_RECEIVER(ret, node);
11473 VALUE str = rb_node_str_string_val(node);
11474 ADD_INSN1(ret, node, putobject, str);
11476 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11479 ADD_INSN(ret, node, pop);
11484 ADD_CALL_RECEIVER(ret, node);
11485 compile_dstr(iseq, ret, node);
11486 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11489 ADD_INSN(ret, node, pop);
11494 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11498 VALUE lit = rb_node_regx_string_val(node);
11500 ADD_INSN1(ret, node, putobject, lit);
11506 compile_dregx(iseq, ret, node, popped);
11509 int ic_index = body->ise_size++;
11511 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11513 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
11517 ADD_INSN(ret, node, pop);
11521 case NODE_ARGSCAT:{
11523 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11524 ADD_INSN1(ret, node, splatarray,
Qfalse);
11525 ADD_INSN(ret, node, pop);
11526 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11527 ADD_INSN1(ret, node, splatarray,
Qfalse);
11528 ADD_INSN(ret, node, pop);
11531 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11532 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11533 if (nd_type_p(body_node, NODE_LIST)) {
11534 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11537 CHECK(COMPILE(ret,
"argscat body", body_node));
11538 ADD_INSN(ret, node, concattoarray);
11543 case NODE_ARGSPUSH:{
11545 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11546 ADD_INSN1(ret, node, splatarray,
Qfalse);
11547 ADD_INSN(ret, node, pop);
11548 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11551 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11552 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11553 if (keyword_node_p(body_node)) {
11554 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11555 ADD_INSN(ret, node, pushtoarraykwsplat);
11557 else if (static_literal_node_p(body_node, iseq,
false)) {
11558 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11559 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11562 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11563 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11569 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11570 ADD_INSN1(ret, node, splatarray,
Qtrue);
11573 ADD_INSN(ret, node, pop);
11578 ID mid = RNODE_DEFN(node)->nd_mid;
11579 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11581 ISEQ_TYPE_METHOD, line);
11583 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11584 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11588 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11594 ID mid = RNODE_DEFS(node)->nd_mid;
11595 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11597 ISEQ_TYPE_METHOD, line);
11599 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11600 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11601 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11605 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11610 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11611 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11612 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11613 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11614 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11617 ADD_INSN(ret, node, pop);
11622 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11623 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11624 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11625 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11628 ADD_INSN(ret, node, pop);
11635 for (
long i = 0; i < ary->len; i++) {
11636 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11637 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11638 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11639 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11641 if (i < ary->
len - 1) {
11642 ADD_INSN(ret, node, pop);
11647 ADD_INSN(ret, node, pop);
11652 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11653 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
11654 ISEQ_TYPE_CLASS, line);
11655 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11656 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11657 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11659 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11660 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11664 ADD_INSN(ret, node, pop);
11669 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11670 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
11671 ISEQ_TYPE_CLASS, line);
11672 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11673 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11675 ADD_INSN (ret, node, putnil);
11676 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11680 ADD_INSN(ret, node, pop);
11686 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11687 ISEQ_TYPE_CLASS, line);
11689 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11690 ADD_INSN (ret, node, putnil);
11691 CONST_ID(singletonclass,
"singletonclass");
11695 int sclass_flags = VM_DEFINECLASS_TYPE_SINGLETON_CLASS;
11696 const NODE *recv = RNODE_SCLASS(node)->nd_recv;
11697 if (!(nd_type_p(recv, NODE_SELF) &&
11698 ISEQ_BODY(iseq)->
type == ISEQ_TYPE_CLASS) &&
11699 !cpath_const_p(recv)) {
11700 sclass_flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
11703 ADD_INSN3(ret, node, defineclass,
11704 ID2SYM(singletonclass), singleton_class,
11709 ADD_INSN(ret, node, pop);
11714 CHECK(compile_colon2(iseq, ret, node, popped));
11717 CHECK(compile_colon3(iseq, ret, node, popped));
11720 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11723 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11727 LABEL *lend = NEW_LABEL(line);
11728 LABEL *ltrue = NEW_LABEL(line);
11729 LABEL *lfalse = NEW_LABEL(line);
11730 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11732 ADD_LABEL(ret, ltrue);
11733 ADD_INSN1(ret, node, putobject,
Qtrue);
11734 ADD_INSNL(ret, node, jump, lend);
11735 ADD_LABEL(ret, lfalse);
11736 ADD_INSN1(ret, node, putobject,
Qfalse);
11737 ADD_LABEL(ret, lend);
11742 ADD_INSN(ret, node, putself);
11748 ADD_INSN(ret, node, putnil);
11754 ADD_INSN1(ret, node, putobject,
Qtrue);
11760 ADD_INSN1(ret, node, putobject,
Qfalse);
11765 CHECK(compile_errinfo(iseq, ret, node, popped));
11769 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11772 case NODE_POSTEXE:{
11776 int is_index = body->ise_size++;
11778 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11780 NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc, rb_fstring(make_name_for_block(iseq)), ISEQ_TYPE_BLOCK, line);
11782 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
11786 ADD_INSN(ret, node, pop);
11791 CHECK(compile_kw_arg(iseq, ret, node, popped));
11794 compile_dstr(iseq, ret, node);
11796 ADD_INSN(ret, node, intern);
11799 ADD_INSN(ret, node, pop);
11803 case NODE_ATTRASGN:
11804 CHECK(compile_attrasgn(iseq, ret, node, popped));
11808 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11811 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11812 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11816 ADD_INSN(ret, node, pop);
11821 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
11836insn_data_length(
INSN *iobj)
11838 return insn_len(iobj->insn_id);
11842calc_sp_depth(
int depth,
INSN *insn)
11844 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
11848opobj_inspect(
VALUE obj)
11868insn_data_to_s_detail(
INSN *iobj)
11870 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
11872 if (iobj->operands) {
11873 const char *types = insn_op_types(iobj->insn_id);
11876 for (j = 0; types[j]; j++) {
11877 char type = types[j];
11883 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
11901 VALUE v = OPERAND_AT(iobj, j);
11916 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11919 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11922 rb_str_catf(str,
"<ise:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11928 if (vm_ci_mid(ci))
rb_str_append(str, rb_id2str(vm_ci_mid(ci)));
11929 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
11937 void *func = (
void *)OPERAND_AT(iobj, j);
11940 if (dladdr(func, &info) && info.dli_sname) {
11945 rb_str_catf(str,
"<%p>", func);
11955 if (types[j + 1]) {
11966 dump_disasm_list_with_cursor(link, NULL, NULL);
11977 printf(
"-- raw disasm--------\n");
11980 if (curr) printf(curr == link ?
"*" :
" ");
11981 switch (link->type) {
11982 case ISEQ_ELEMENT_INSN:
11984 iobj = (
INSN *)link;
11985 str = insn_data_to_s_detail(iobj);
11986 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
11987 pos += insn_data_length(iobj);
11990 case ISEQ_ELEMENT_LABEL:
11992 lobj = (
LABEL *)link;
11993 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
11994 dest == lobj ?
" <---" :
"");
11997 case ISEQ_ELEMENT_TRACE:
12000 printf(
" trace: %0x\n", trace->event);
12003 case ISEQ_ELEMENT_ADJUST:
12006 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
12011 rb_raise(
rb_eSyntaxError,
"dump_disasm_list error: %d\n", (
int)link->type);
12015 printf(
"---------------------\n");
12020rb_insn_len(
VALUE insn)
12022 return insn_len(insn);
12026rb_insns_name(
int i)
12028 return insn_name(i);
12032rb_insns_name_array(
void)
12036 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
12047 obj = rb_to_symbol_type(obj);
12049 if (st_lookup(labels_table, obj, &tmp) == 0) {
12050 label = NEW_LABEL(0);
12051 st_insert(labels_table, obj, (st_data_t)label);
12054 label = (
LABEL *)tmp;
12061get_exception_sym2type(
VALUE sym)
12063 static VALUE symRescue, symEnsure, symRetry;
12064 static VALUE symBreak, symRedo, symNext;
12066 if (symRescue == 0) {
12075 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
12076 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
12077 if (sym == symRetry)
return CATCH_TYPE_RETRY;
12078 if (sym == symBreak)
return CATCH_TYPE_BREAK;
12079 if (sym == symRedo)
return CATCH_TYPE_REDO;
12080 if (sym == symNext)
return CATCH_TYPE_NEXT;
12081 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
12094 LABEL *lstart, *lend, *lcont;
12109 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
12110 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
12111 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
12115 if (
type == CATCH_TYPE_RESCUE ||
12116 type == CATCH_TYPE_BREAK ||
12117 type == CATCH_TYPE_NEXT) {
12123 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
12131insn_make_insn_table(
void)
12135 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
12137 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
12151 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
12153 else if (
CLASS_OF(op) == rb_cISeq) {
12160 loaded_iseq = rb_iseqw_to_iseq(iseqw);
12161 return loaded_iseq;
12169 unsigned int flag = 0;
12180 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
12182 if (!
NIL_P(vkw_arg)) {
12185 size_t n = rb_callinfo_kwarg_bytes(
len);
12188 kw_arg->references = 0;
12189 kw_arg->keyword_len =
len;
12190 for (i = 0; i <
len; i++) {
12193 kw_arg->keywords[i] = kw;
12198 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
12204event_name_to_flag(
VALUE sym)
12206#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
12227 int line_no = 0, node_id = -1, insn_idx = 0;
12228 int ret = COMPILE_OK;
12233 static struct st_table *insn_table;
12235 if (insn_table == 0) {
12236 insn_table = insn_make_insn_table();
12239 for (i=0; i<
len; i++) {
12245 ADD_TRACE(anchor, event);
12248 LABEL *label = register_label(iseq, labels_table, obj);
12249 ADD_LABEL(anchor, label);
12266 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
12268 COMPILE_ERROR(iseq, line_no,
12269 "unknown instruction: %+"PRIsVALUE, insn);
12274 if (argc != insn_len((
VALUE)insn_id)-1) {
12275 COMPILE_ERROR(iseq, line_no,
12276 "operand size mismatch");
12282 argv = compile_data_calloc2_type(iseq,
VALUE, argc);
12287 (
enum ruby_vminsn_type)insn_id, argc, argv));
12289 for (j=0; j<argc; j++) {
12291 switch (insn_op_type((
VALUE)insn_id, j)) {
12293 LABEL *label = register_label(iseq, labels_table, op);
12294 argv[j] = (
VALUE)label;
12309 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
12320 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
12321 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
12327 op = rb_to_array_type(op);
12331 sym = rb_to_symbol_type(sym);
12336 argv[j] = segments;
12338 ISEQ_BODY(iseq)->ic_size++;
12343 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
12344 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
12349 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
12350 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
12354 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
12357 argv[j] = rb_to_symbol_type(op);
12364 op = rb_to_array_type(op);
12369 register_label(iseq, labels_table, sym);
12370 cdhash_aset(map, key, (
VALUE)label);
12380#if SIZEOF_VALUE <= SIZEOF_LONG
12385 argv[j] = (
VALUE)funcptr;
12396 (
enum ruby_vminsn_type)insn_id, argc, NULL));
12400 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
12405 validate_labels(iseq, labels_table);
12406 if (!ret)
return ret;
12407 return iseq_setup(iseq, anchor);
12410#define CHECK_ARRAY(v) rb_to_array_type(v)
12411#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
12416 VALUE val = rb_hash_aref(param, sym);
12421 else if (!
NIL_P(val)) {
12422 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
12428static const struct rb_iseq_param_keyword *
12434 VALUE key, sym, default_val;
12437 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12439 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12441 keyword->num =
len;
12442#define SYM(s) ID2SYM(rb_intern_const(#s))
12443 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12444 i = keyword->bits_start - keyword->num;
12445 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12449 for (i = 0; i <
len; i++) {
12453 goto default_values;
12456 keyword->required_num++;
12460 default_len =
len - i;
12461 if (default_len == 0) {
12462 keyword->table = ids;
12465 else if (default_len < 0) {
12471 for (j = 0; i <
len; i++, j++) {
12485 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
12491 keyword->table = ids;
12492 keyword->default_values = dvs;
12498iseq_insn_each_object_mark_and_move(
VALUE * obj,
VALUE _)
12500 rb_gc_mark_and_move(obj);
12507 size_t size =
sizeof(
INSN);
12508 size_t align = ALIGNMENT_SIZE_OF(
INSN);
12509 unsigned int pos = 0;
12512 size_t padding = calc_padding((
void *)&storage->buff[pos], align);
12513 size_t offset = pos + size + padding;
12514 if (offset > storage->size || offset > storage->pos) {
12516 storage = storage->next;
12519 pos += (int)padding;
12521 iobj = (
INSN *)&storage->buff[pos];
12523 if (iobj->operands) {
12524 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_move, (
VALUE)0);
12537 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
12544#define SYM(s) ID2SYM(rb_intern_const(#s))
12546 unsigned int arg_size, local_size, stack_max;
12548 struct st_table *labels_table = st_init_numtable();
12550 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
12551 VALUE keywords = rb_hash_aref(params, SYM(keyword));
12553 DECL_ANCHOR(anchor);
12554 INIT_ANCHOR(anchor);
12557 ISEQ_BODY(iseq)->local_table_size =
len;
12558 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12560 for (i = 0; i <
len; i++) {
12563 if (sym_arg_rest == lv) {
12571#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12572 if (INT_PARAM(lead_num)) {
12573 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12575 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12576 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12577 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12578 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12581#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12583 INT_PARAM(arg_size);
12584 INT_PARAM(local_size);
12585 INT_PARAM(stack_max);
12589 VALUE source_hash = rb_hash_aref(misc,
ID2SYM(rb_intern(
"source_hash")));
12590 if (!
NIL_P(source_hash)) {
12591 ISEQ_BODY(iseq)->source_hash =
NUM2ULL(source_hash);
12595#ifdef USE_ISEQ_NODE_ID
12596 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
12604 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12606 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12609 for (i = 0; i <
len; i++) {
12611 LABEL *label = register_label(iseq, labels_table, ent);
12612 opt_table[i] = (
VALUE)label;
12615 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12616 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12619 else if (!
NIL_P(arg_opt_labels)) {
12620 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
12625 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12627 else if (!
NIL_P(keywords)) {
12628 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
12632 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
12633 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12636 if (
Qtrue == rb_hash_aref(params, SYM(use_block))) {
12637 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12640 if (int_param(&i, params, SYM(kwrest))) {
12641 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12642 if (keyword == NULL) {
12643 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12645 keyword->rest_start = i;
12646 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12649 iseq_calc_param_size(iseq);
12652 iseq_build_from_ary_exception(iseq, labels_table, exception);
12655 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12657 ISEQ_BODY(iseq)->param.size = arg_size;
12658 ISEQ_BODY(iseq)->local_table_size = local_size;
12659 ISEQ_BODY(iseq)->stack_max = stack_max;
12669 while (body->type == ISEQ_TYPE_BLOCK ||
12670 body->type == ISEQ_TYPE_RESCUE ||
12671 body->type == ISEQ_TYPE_ENSURE ||
12672 body->type == ISEQ_TYPE_EVAL ||
12673 body->type == ISEQ_TYPE_MAIN
12677 for (i = 0; i < body->local_table_size; i++) {
12678 if (body->local_table[i] ==
id) {
12682 iseq = body->parent_iseq;
12683 body = ISEQ_BODY(iseq);
12696 for (i=0; i<body->local_table_size; i++) {
12697 if (body->local_table[i] ==
id) {
12707#ifndef IBF_ISEQ_DEBUG
12708#define IBF_ISEQ_DEBUG 0
12711#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12712#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12715typedef uint32_t ibf_offset_t;
12716#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12718#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12720#define IBF_DEVEL_VERSION 8
12721#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12723#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12726static const char IBF_ENDIAN_MARK =
12727#ifdef WORDS_BIGENDIAN
12736 uint32_t major_version;
12737 uint32_t minor_version;
12739 uint32_t extra_size;
12741 uint32_t iseq_list_size;
12742 uint32_t global_object_list_size;
12743 ibf_offset_t iseq_list_offset;
12744 ibf_offset_t global_object_list_offset;
12765 unsigned int obj_list_size;
12766 ibf_offset_t obj_list_offset;
12785pinned_list_mark(
void *ptr)
12789 for (i = 0; i < list->size; i++) {
12790 if (list->buffer[i]) {
12791 rb_gc_mark(list->buffer[i]);
12803 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
12807pinned_list_fetch(
VALUE list,
long offset)
12813 if (offset < 0 || offset >= ptr->size) {
12814 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12817 return ptr->buffer[offset];
12821pinned_list_store(
VALUE list,
long offset,
VALUE object)
12827 if (offset < 0 || offset >= ptr->size) {
12828 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12835pinned_list_new(
long size)
12837 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
12838 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
12839 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
12845ibf_dump_pos(
struct ibf_dump *dump)
12847 long pos = RSTRING_LEN(dump->current_buffer->str);
12848#if SIZEOF_LONG > SIZEOF_INT
12849 if (pos >= UINT_MAX) {
12853 return (
unsigned int)pos;
12857ibf_dump_align(
struct ibf_dump *dump,
size_t align)
12859 ibf_offset_t pos = ibf_dump_pos(dump);
12861 static const char padding[
sizeof(
VALUE)];
12862 size_t size = align - ((size_t)pos % align);
12863#if SIZEOF_LONG > SIZEOF_INT
12864 if (pos + size >= UINT_MAX) {
12868 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
12869 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
12871 rb_str_cat(dump->current_buffer->str, padding, size);
12876ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
12878 ibf_offset_t pos = ibf_dump_pos(dump);
12879#if SIZEOF_LONG > SIZEOF_INT
12881 if (size >= UINT_MAX || pos + size >= UINT_MAX) {
12885 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
12890ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
12892 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
12896ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
12898 VALUE str = dump->current_buffer->str;
12899 char *ptr = RSTRING_PTR(str);
12900 if ((
unsigned long)(size + offset) > (
unsigned long)RSTRING_LEN(str))
12901 rb_bug(
"ibf_dump_overwrite: overflow");
12902 memcpy(ptr + offset, buff, size);
12906ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
12908 ibf_offset_t beg = *offset;
12910 return load->current_buffer->buff + beg;
12914ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
12916 void *buff = ruby_xmalloc2(x, y);
12917 size_t size = x * y;
12918 memcpy(buff, load->current_buffer->buff + offset, size);
12922#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
12924#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
12925#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
12926#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
12927#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
12928#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
12931ibf_table_lookup(
struct st_table *table, st_data_t key)
12935 if (st_lookup(table, key, &val)) {
12944ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
12946 int index = ibf_table_lookup(table, key);
12949 index = (int)table->num_entries;
12950 st_insert(table, key, (st_data_t)index);
12958static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
12964ibf_dump_object_table_new(
void)
12966 st_table *obj_table = st_init_numtable();
12967 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
12975 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
12981 if (
id == 0 || rb_id2name(
id) == NULL) {
12984 return ibf_dump_object(dump,
rb_id2sym(
id));
12988ibf_load_id(
const struct ibf_load *load,
const ID id_index)
12990 if (id_index == 0) {
12993 VALUE sym = ibf_load_object(load, id_index);
13003static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
13008 if (iseq == NULL) {
13012 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
13016static unsigned char
13017ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
13019 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
13020 return (
unsigned char)load->current_buffer->buff[(*offset)++];
13036 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
13037 ibf_dump_write(dump, &x,
sizeof(
VALUE));
13041 enum { max_byte_length =
sizeof(
VALUE) + 1 };
13043 unsigned char bytes[max_byte_length];
13046 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
13047 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
13053 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
13056 ibf_dump_write(dump, bytes + max_byte_length - n, n);
13060ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
13062 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
13063 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
13065 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
13066 *offset +=
sizeof(
VALUE);
13071 enum { max_byte_length =
sizeof(
VALUE) + 1 };
13073 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
13074 const unsigned char c = buffer[*offset];
13078 c == 0 ? 9 : ntz_int32(c) + 1;
13081 if (*offset + n > load->current_buffer->size) {
13086 for (i = 1; i < n; i++) {
13088 x |= (
VALUE)buffer[*offset + i];
13102 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
13104 size_t len = strlen(bf->name);
13105 ibf_dump_write_small_value(dump, (
VALUE)
len);
13106 ibf_dump_write(dump, bf->name,
len);
13110ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
13112 int i = (int)ibf_load_small_value(load, offset);
13113 int len = (int)ibf_load_small_value(load, offset);
13114 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
13117 fprintf(stderr,
"%.*s!!\n",
len, name);
13121 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
13122 if (strncmp(table[i].name, name,
len) != 0) {
13123 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %.*s but %s)",
13124 i,
len, name, table[i].name);
13135 const int iseq_size = body->iseq_size;
13137 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
13139 ibf_offset_t offset = ibf_dump_pos(dump);
13141 for (code_index=0; code_index<iseq_size;) {
13142 const VALUE insn = orig_code[code_index++];
13143 const char *types = insn_op_types(insn);
13148 ibf_dump_write_small_value(dump, insn);
13151 for (op_index=0; types[op_index]; op_index++, code_index++) {
13152 VALUE op = orig_code[code_index];
13155 switch (types[op_index]) {
13158 wv = ibf_dump_object(dump, op);
13167 wv = ibf_dump_object(dump, arr);
13175 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
13183 wv = ibf_dump_id(dump, (
ID)op);
13195 ibf_dump_write_small_value(dump, wv);
13205ibf_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)
13208 unsigned int code_index;
13209 ibf_offset_t reading_pos = bytecode_offset;
13213 struct rb_call_data *cd_entries = load_body->call_data;
13216 load_body->iseq_encoded = code;
13217 load_body->iseq_size = iseq_size;
13219 iseq_bits_t * mark_offset_bits;
13220 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
13221 load_body->mark_bits.single = 0;
13222 mark_offset_bits = &load_body->mark_bits.single;
13225 load_body->mark_bits.list =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
13226 mark_offset_bits = load_body->mark_bits.list;
13228 bool needs_bitmap =
false;
13230 for (code_index=0; code_index<iseq_size;) {
13232 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
13233 const char *types = insn_op_types(insn);
13239 for (op_index=0; types[op_index]; op_index++, code_index++) {
13240 const char operand_type = types[op_index];
13241 switch (operand_type) {
13244 VALUE op = ibf_load_small_value(load, &reading_pos);
13245 VALUE v = ibf_load_object(load, op);
13246 code[code_index] = v;
13249 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13250 needs_bitmap =
true;
13256 VALUE op = ibf_load_small_value(load, &reading_pos);
13257 VALUE v = ibf_load_object(load, op);
13262 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
13264 code[code_index] = v;
13265 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13267 needs_bitmap =
true;
13272 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
13274 code[code_index] = v;
13277 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13278 needs_bitmap =
true;
13284 VALUE op = ibf_load_small_value(load, &reading_pos);
13285 VALUE arr = ibf_load_object(load, op);
13287 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
13288 ic->
segments = array_to_idlist(arr);
13290 code[code_index] = (
VALUE)ic;
13297 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13299 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
13300 code[code_index] = (
VALUE)ic;
13302 if (operand_type == TS_IVC) {
13305 if (insn == BIN(setinstancevariable)) {
13306 ID iv_name = (
ID)code[code_index - 1];
13307 cache->iv_set_name = iv_name;
13308 cache->value = IVAR_CACHE_INIT;
13311 cache->iv_set_name = 0;
13312 cache->value = rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET);
13320 code[code_index] = (
VALUE)cd_entries++;
13325 VALUE op = ibf_load_small_value(load, &reading_pos);
13326 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
13333 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
13336 code[code_index] = ibf_load_small_value(load, &reading_pos);
13340 if (insn_len(insn) != op_index+1) {
13345 if (!needs_bitmap) {
13346 SIZED_FREE_N(load_body->mark_bits.list, ISEQ_MBITS_BUFLEN(iseq_size));
13347 load_body->mark_bits.list = NULL;
13351 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
13358 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
13361 IBF_W_ALIGN(
VALUE);
13362 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
13365 return ibf_dump_pos(dump);
13370ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
13374 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
13385 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
13388 struct rb_iseq_param_keyword dump_kw = *kw;
13389 int dv_num = kw->num - kw->required_num;
13394 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
13395 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
13397 dump_kw.table = IBF_W(ids,
ID, kw->num);
13398 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
13399 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
13400 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
13407static const struct rb_iseq_param_keyword *
13408ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
13410 if (param_keyword_offset) {
13411 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
13412 int dv_num = kw->num - kw->required_num;
13413 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
13416 for (i=0; i<dv_num; i++) {
13417 dvs[i] = ibf_load_object(load, dvs[i]);
13423 kw->default_values = dvs;
13434 ibf_offset_t offset = ibf_dump_pos(dump);
13438 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
13439 ibf_dump_write_small_value(dump, entries[i].line_no);
13440#ifdef USE_ISEQ_NODE_ID
13441 ibf_dump_write_small_value(dump, entries[i].node_id);
13443 ibf_dump_write_small_value(dump, entries[i].events);
13450ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
13452 ibf_offset_t reading_pos = body_offset;
13456 for (i = 0; i < size; i++) {
13457 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
13458#ifdef USE_ISEQ_NODE_ID
13459 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
13461 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
13468ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
13470 ibf_offset_t offset = ibf_dump_pos(dump);
13472 unsigned int last = 0;
13474 for (i = 0; i < size; i++) {
13475 ibf_dump_write_small_value(dump, positions[i] - last);
13476 last = positions[i];
13482static unsigned int *
13483ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
13485 ibf_offset_t reading_pos = positions_offset;
13486 unsigned int *positions =
ALLOC_N(
unsigned int, size);
13488 unsigned int last = 0;
13490 for (i = 0; i < size; i++) {
13491 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
13492 last = positions[i];
13502 const int size = body->local_table_size;
13506 for (i=0; i<size; i++) {
13507 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13510 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13516 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
13520ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13523 ID *table = IBF_R(local_table_offset,
ID, size);
13526 for (i=0; i<size; i++) {
13527 table[i] = ibf_load_id(load, table[i]);
13530 if (size == 1 && table[0] == idERROR_INFO) {
13531 ruby_xfree_sized(table,
sizeof(
ID) * size);
13532 return rb_iseq_shared_exc_local_tbl;
13547 const int size = ISEQ_LVAR_STATES_BUFLEN(body->local_table_size);
13548 IBF_W_ALIGN(uint8_t);
13549 if (iseq_has_lvar_states_p(body)) {
13550 return ibf_dump_write(dump, iseq_lvar_states(body),
sizeof(uint8_t) * size);
13553 return ibf_dump_write(dump, NULL, 0);
13558ibf_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)
13560 if (local_table == rb_iseq_shared_exc_local_tbl ||
13562 load_body->lvar_states.list = NULL;
13564 else if (ISEQ_LVAR_STATES_EMBED_P((
unsigned int)size)) {
13565 ibf_offset_t pos = lvar_states_offset;
13566 const int len =
sizeof(uint8_t) * ISEQ_LVAR_STATES_BUFLEN(size);
13567 memcpy(load_body->lvar_states.single, ibf_load_ptr(load, &pos,
len),
len);
13570 load_body->lvar_states.list = IBF_R(lvar_states_offset, uint8_t, ISEQ_LVAR_STATES_BUFLEN(size));
13580 int *iseq_indices =
ALLOCA_N(
int, table->size);
13583 for (i=0; i<table->size; i++) {
13584 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13587 const ibf_offset_t offset = ibf_dump_pos(dump);
13589 for (i=0; i<table->size; i++) {
13590 ibf_dump_write_small_value(dump, iseq_indices[i]);
13591 ibf_dump_write_small_value(dump, table->entries[i].type);
13592 ibf_dump_write_small_value(dump, table->entries[i].start);
13593 ibf_dump_write_small_value(dump, table->entries[i].end);
13594 ibf_dump_write_small_value(dump, table->entries[i].cont);
13595 ibf_dump_write_small_value(dump, table->entries[i].sp);
13600 return ibf_dump_pos(dump);
13605ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size,
const rb_iseq_t *parent_iseq)
13608 struct iseq_catch_table *table = ruby_xcalloc(1, iseq_catch_table_bytes(size));
13609 table->size = size;
13610 ISEQ_BODY(parent_iseq)->catch_table = table;
13612 ibf_offset_t reading_pos = catch_table_offset;
13615 for (i=0; i<table->size; i++) {
13616 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13617 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13618 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13619 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13620 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13621 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13624 RB_OBJ_WRITE(parent_iseq, UNALIGNED_MEMBER_PTR(&table->entries[i], iseq), catch_iseq);
13628 ISEQ_BODY(parent_iseq)->catch_table = NULL;
13636 const unsigned int ci_size = body->ci_size;
13639 ibf_offset_t offset = ibf_dump_pos(dump);
13643 for (i = 0; i < ci_size; i++) {
13646 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13647 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13648 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13652 int len = kwarg->keyword_len;
13653 ibf_dump_write_small_value(dump,
len);
13654 for (
int j=0; j<
len; j++) {
13655 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13656 ibf_dump_write_small_value(dump, keyword);
13660 ibf_dump_write_small_value(dump, 0);
13665 ibf_dump_write_small_value(dump, (
VALUE)-1);
13683static enum rb_id_table_iterator_result
13684store_outer_variable(
ID id,
VALUE val,
void *dump)
13689 pair->name = rb_id2str(
id);
13691 return ID_TABLE_CONTINUE;
13695outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13703 else if (!bp->name) {
13713 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13715 ibf_offset_t offset = ibf_dump_pos(dump);
13717 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13718 ibf_dump_write_small_value(dump, (
VALUE)size);
13727 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13729 for (
size_t i = 0; i < size; ++i) {
13730 ID id = ovlist->pairs[i].id;
13731 ID val = ovlist->pairs[i].val;
13732 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13733 ibf_dump_write_small_value(dump, val);
13742ibf_load_ci_entries(
const struct ibf_load *load,
13743 ibf_offset_t ci_entries_offset,
13744 unsigned int ci_size,
13752 ibf_offset_t reading_pos = ci_entries_offset;
13759 for (i = 0; i < ci_size; i++) {
13760 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
13761 if (mid_index != (
VALUE)-1) {
13762 ID mid = ibf_load_id(load, mid_index);
13763 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13764 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13767 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
13770 kwarg->references = 0;
13771 kwarg->keyword_len = kwlen;
13772 for (
int j=0; j<kwlen; j++) {
13773 VALUE keyword = ibf_load_small_value(load, &reading_pos);
13774 kwarg->keywords[j] = ibf_load_object(load, keyword);
13778 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
13780 cds[i].cc = vm_cc_empty();
13791ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
13793 ibf_offset_t reading_pos = outer_variables_offset;
13797 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
13799 if (table_size > 0) {
13800 tbl = rb_id_table_create(table_size);
13803 for (
size_t i = 0; i < table_size; i++) {
13804 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
13805 VALUE value = ibf_load_small_value(load, &reading_pos);
13806 if (!key) key = rb_make_temporary_id(i);
13807 rb_id_table_insert(tbl, key, value);
13816 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
13818 unsigned int *positions;
13822 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
13823 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
13825#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13826 ibf_offset_t iseq_start = ibf_dump_pos(dump);
13831 buffer.obj_table = ibf_dump_object_table_new();
13832 dump->current_buffer = &buffer;
13835 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
13836 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
13837 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
13838 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
13839 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
13841 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
13842 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
13843 SIZED_FREE_N(positions, ISEQ_BODY(iseq)->insns_info.size);
13845 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
13846 const ibf_offset_t lvar_states_offset = ibf_dump_lvar_states(dump, iseq);
13847 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
13848 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
13850 const int parent_iseq_index = ibf_table_lookup(dump->iseq_table, (st_data_t)ISEQ_BODY(iseq)->parent_iseq);
13851 const int local_iseq_index = ibf_table_lookup(dump->iseq_table, (st_data_t)ISEQ_BODY(iseq)->local_iseq);
13852 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
13853 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
13854 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
13856#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13857 ibf_offset_t local_obj_list_offset;
13858 unsigned int local_obj_list_size;
13860 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
13863 ibf_offset_t body_offset = ibf_dump_pos(dump);
13866 unsigned int param_flags =
13867 (body->param.flags.has_lead << 0) |
13868 (body->param.flags.has_opt << 1) |
13869 (body->param.flags.has_rest << 2) |
13870 (body->param.flags.has_post << 3) |
13871 (body->param.flags.has_kw << 4) |
13872 (body->param.flags.has_kwrest << 5) |
13873 (body->param.flags.has_block << 6) |
13874 (body->param.flags.ambiguous_param0 << 7) |
13875 (body->param.flags.accepts_no_kwarg << 8) |
13876 (body->param.flags.ruby2_keywords << 9) |
13877 (body->param.flags.anon_rest << 10) |
13878 (body->param.flags.anon_kwrest << 11) |
13879 (body->param.flags.use_block << 12) |
13880 (body->param.flags.forwardable << 13) |
13881 (body->param.flags.accepts_no_block << 14);
13883#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13884# define IBF_BODY_OFFSET(x) (x)
13886# define IBF_BODY_OFFSET(x) (body_offset - (x))
13889 ibf_dump_write_small_value(dump, body->type);
13890 ibf_dump_write_small_value(dump, body->iseq_size);
13891 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
13892 ibf_dump_write_small_value(dump, bytecode_size);
13893 ibf_dump_write_small_value(dump, param_flags);
13894 ibf_dump_write_small_value(dump, body->param.size);
13895 ibf_dump_write_small_value(dump, body->param.lead_num);
13896 ibf_dump_write_small_value(dump, body->param.opt_num);
13897 ibf_dump_write_small_value(dump, body->param.rest_start);
13898 ibf_dump_write_small_value(dump, body->param.post_start);
13899 ibf_dump_write_small_value(dump, body->param.post_num);
13900 ibf_dump_write_small_value(dump, body->param.block_start);
13901 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
13902 ibf_dump_write_small_value(dump, param_keyword_offset);
13903 ibf_dump_write_small_value(dump, location_pathobj_index);
13904 ibf_dump_write_small_value(dump, location_label_index);
13905 ibf_dump_write_small_value(dump, body->location.first_lineno);
13906 ibf_dump_write_small_value(dump, body->location.node_id);
13909 ibf_dump_write_small_value(dump, (
VALUE)(uint32_t)(body->source_hash >> 32));
13910 ibf_dump_write_small_value(dump, (
VALUE)(uint32_t)body->source_hash);
13911 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
13912 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
13913 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
13914 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
13915 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
13916 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
13917 ibf_dump_write_small_value(dump, body->insns_info.size);
13918 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
13919 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(lvar_states_offset));
13920 ibf_dump_write_small_value(dump, catch_table_size);
13921 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
13922 ibf_dump_write_small_value(dump, parent_iseq_index);
13923 ibf_dump_write_small_value(dump, local_iseq_index);
13924 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
13925 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
13926 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
13927 ibf_dump_write_small_value(dump, ISEQ_FLIP_CNT(iseq));
13928 ibf_dump_write_small_value(dump, body->local_table_size);
13929 ibf_dump_write_small_value(dump, body->ivc_size);
13930 ibf_dump_write_small_value(dump, body->icvarc_size);
13931 ibf_dump_write_small_value(dump, body->ise_size);
13932 ibf_dump_write_small_value(dump, body->ic_size);
13933 ibf_dump_write_small_value(dump, body->ci_size);
13934 ibf_dump_write_small_value(dump, body->stack_max);
13935 ibf_dump_write_small_value(dump, body->builtin_attrs);
13936 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
13938#undef IBF_BODY_OFFSET
13940#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13941 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
13943 dump->current_buffer = saved_buffer;
13944 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
13946 ibf_offset_t offset = ibf_dump_pos(dump);
13947 ibf_dump_write_small_value(dump, iseq_start);
13948 ibf_dump_write_small_value(dump, iseq_length_bytes);
13949 ibf_dump_write_small_value(dump, body_offset);
13951 ibf_dump_write_small_value(dump, local_obj_list_offset);
13952 ibf_dump_write_small_value(dump, local_obj_list_size);
13954 st_free_table(buffer.obj_table);
13958 return body_offset;
13963ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
13965 VALUE str = ibf_load_object(load, str_index);
13967 str = rb_fstring(str);
13977 ibf_offset_t reading_pos = offset;
13979#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13981 load->current_buffer = &load->global_buffer;
13983 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13984 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13985 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13988 buffer.buff = load->global_buffer.buff + iseq_start;
13989 buffer.size = iseq_length_bytes;
13990 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13991 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13992 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
13994 load->current_buffer = &buffer;
13995 reading_pos = body_offset;
13998#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13999# define IBF_BODY_OFFSET(x) (x)
14001# define IBF_BODY_OFFSET(x) (offset - (x))
14004 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14005 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14006 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14007 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14008 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14009 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14010 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
14011 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
14012 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
14013 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
14014 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
14015 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
14016 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14017 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14018 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
14019 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
14020 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
14021 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
14022 const uint64_t source_hash_hi = (uint64_t)ibf_load_small_value(load, &reading_pos);
14023 const uint64_t source_hash_lo = (uint64_t)ibf_load_small_value(load, &reading_pos);
14024 const uint64_t source_hash = (source_hash_hi << 32) | (uint32_t)source_hash_lo;
14025 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
14026 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
14027 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
14028 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
14029 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14030 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14031 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14032 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14033 const ibf_offset_t lvar_states_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14034 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14035 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14036 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
14037 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
14038 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
14039 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14040 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14041 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
14042 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14044 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14045 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14046 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14047 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14049 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14050 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14051 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14052 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
14055 VALUE path = ibf_load_object(load, location_pathobj_index);
14060 realpath = path = rb_fstring(path);
14063 VALUE pathobj = path;
14069 if (!
NIL_P(realpath)) {
14071 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
14072 "(%x), path=%+"PRIsVALUE,
14073 realpath,
TYPE(realpath), path);
14075 realpath = rb_fstring(realpath);
14081 rb_iseq_pathobj_set(iseq, path, realpath);
14086 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
14088#undef IBF_BODY_OFFSET
14090 load_body->type =
type;
14091 load_body->stack_max = stack_max;
14092 load_body->param.flags.has_lead = (param_flags >> 0) & 1;
14093 load_body->param.flags.has_opt = (param_flags >> 1) & 1;
14094 load_body->param.flags.has_rest = (param_flags >> 2) & 1;
14095 load_body->param.flags.has_post = (param_flags >> 3) & 1;
14096 load_body->param.flags.has_kw = FALSE;
14097 load_body->param.flags.has_kwrest = (param_flags >> 5) & 1;
14098 load_body->param.flags.has_block = (param_flags >> 6) & 1;
14099 load_body->param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
14100 load_body->param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
14101 load_body->param.flags.ruby2_keywords = (param_flags >> 9) & 1;
14102 load_body->param.flags.anon_rest = (param_flags >> 10) & 1;
14103 load_body->param.flags.anon_kwrest = (param_flags >> 11) & 1;
14104 load_body->param.flags.use_block = (param_flags >> 12) & 1;
14105 load_body->param.flags.forwardable = (param_flags >> 13) & 1;
14106 load_body->param.flags.accepts_no_block = (param_flags >> 14) & 1;
14107 load_body->param.size = param_size;
14108 load_body->param.lead_num = param_lead_num;
14109 load_body->param.opt_num = param_opt_num;
14110 load_body->param.rest_start = param_rest_start;
14111 load_body->param.post_start = param_post_start;
14112 load_body->param.post_num = param_post_num;
14113 load_body->param.block_start = param_block_start;
14114 load_body->local_table_size = local_table_size;
14115 load_body->ci_size = ci_size;
14116 load_body->insns_info.size = insns_info_size;
14119 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
14120 if (variable_flip_count) {
14122 v->flip_count = variable_flip_count;
14125 load_body->location.first_lineno = location_first_lineno;
14126 load_body->location.node_id = location_node_id;
14127 load_body->source_hash = source_hash;
14128 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
14129 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
14130 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
14131 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
14132 load_body->builtin_attrs = builtin_attrs;
14133 load_body->prism = prism;
14135 load_body->ivc_size = ivc_size;
14136 load_body->icvarc_size = icvarc_size;
14137 load_body->ise_size = ise_size;
14138 load_body->ic_size = ic_size;
14140 if (ISEQ_IS_SIZE(load_body)) {
14144 load_body->is_entries = NULL;
14146 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
14147 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
14148 load_body->param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
14149 load_body->param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
14150 load_body->param.flags.has_kw = (param_flags >> 4) & 1;
14151 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
14152 load_body->insns_info.positions_or_succ_index_table.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
14153 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
14154 ibf_load_lvar_states(load, load_body, lvar_states_offset, local_table_size, load_body->local_table);
14155 ibf_load_catch_table(load, catch_table_offset, catch_table_size, iseq);
14159 const rb_iseq_t *mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
14161 RB_OBJ_WRITE(iseq, &load_body->parent_iseq, parent_iseq);
14162 RB_OBJ_WRITE(iseq, &load_body->local_iseq, local_iseq);
14163 RB_OBJ_WRITE(iseq, &load_body->mandatory_only_iseq, mandatory_only_iseq);
14166 if (load_body->param.keyword != NULL) {
14168 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->param.keyword;
14169 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
14172 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
14173#if VM_INSN_INFO_TABLE_IMPL == 2
14174 rb_iseq_insns_info_encode_positions(iseq);
14177 rb_iseq_translate_threaded_code(iseq);
14179#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14180 load->current_buffer = &load->global_buffer;
14183 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
14185#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14186 load->current_buffer = saved_buffer;
14188 verify_call_cache(iseq);
14191 rb_vm_pop_frame_no_int(ec);
14201ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14206 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
14209 return ST_CONTINUE;
14219 args.offset_list = offset_list;
14221 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
14224 st_index_t size = dump->iseq_table->num_entries;
14225 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
14227 for (i = 0; i < size; i++) {
14231 ibf_dump_align(dump,
sizeof(ibf_offset_t));
14232 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
14233 header->iseq_list_size = (
unsigned int)size;
14243 unsigned int type: 5;
14244 unsigned int special_const: 1;
14245 unsigned int frozen: 1;
14246 unsigned int internal: 1;
14249enum ibf_object_class_index {
14250 IBF_OBJECT_CLASS_OBJECT,
14251 IBF_OBJECT_CLASS_ARRAY,
14252 IBF_OBJECT_CLASS_STANDARD_ERROR,
14253 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
14254 IBF_OBJECT_CLASS_TYPE_ERROR,
14255 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
14265 long keyval[FLEX_ARY_LEN];
14278 BDIGIT digits[FLEX_ARY_LEN];
14281enum ibf_object_data_type {
14282 IBF_OBJECT_DATA_ENCODING,
14293#define IBF_ALIGNED_OFFSET(align, offset) \
14294 ((((offset) - 1) / (align) + 1) * (align))
14299#define IBF_OBJBODY(type, offset) \
14300 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
14303ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
14305 if (offset >= load->current_buffer->size) {
14306 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
14308 return load->current_buffer->buff + offset;
14311NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
14314ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
14317 rb_raw_obj_info(buff,
sizeof(buff), obj);
14326 rb_raise(rb_eArgError,
"unsupported");
14333 enum ibf_object_class_index cindex;
14335 cindex = IBF_OBJECT_CLASS_OBJECT;
14338 cindex = IBF_OBJECT_CLASS_ARRAY;
14341 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
14344 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
14347 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
14350 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
14353 rb_obj_info_dump(obj);
14355 rb_bug(
"unsupported class");
14357 ibf_dump_write_small_value(dump, (
VALUE)cindex);
14363 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
14366 case IBF_OBJECT_CLASS_OBJECT:
14368 case IBF_OBJECT_CLASS_ARRAY:
14370 case IBF_OBJECT_CLASS_STANDARD_ERROR:
14372 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
14374 case IBF_OBJECT_CLASS_TYPE_ERROR:
14376 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
14380 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
14388 (void)IBF_W(&dbl,
double, 1);
14396 memcpy(&d, IBF_OBJBODY(
double, offset),
sizeof(d));
14405 long encindex = (long)rb_enc_get_index(obj);
14406 long len = RSTRING_LEN(obj);
14407 const char *ptr = RSTRING_PTR(obj);
14409 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14410 rb_encoding *enc = rb_enc_from_index((
int)encindex);
14411 const char *enc_name = rb_enc_name(enc);
14412 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
14415 ibf_dump_write_small_value(dump, encindex);
14416 ibf_dump_write_small_value(dump,
len);
14417 IBF_WP(ptr,
char,
len);
14423 ibf_offset_t reading_pos = offset;
14425 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14426 const long len = (long)ibf_load_small_value(load, &reading_pos);
14427 const char *ptr = load->current_buffer->buff + reading_pos;
14429 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14430 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14431 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14435 if (header->frozen && !header->internal) {
14436 str = rb_enc_literal_str(ptr,
len, rb_enc_from_index(encindex));
14439 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
14442 if (header->frozen) str = rb_fstring(str);
14453 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
14455 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
14456 ibf_dump_write_small_value(dump, regexp.srcstr);
14463 regexp.option = ibf_load_byte(load, &offset);
14464 regexp.srcstr = ibf_load_small_value(load, &offset);
14466 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
14467 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
14479 ibf_dump_write_small_value(dump,
len);
14480 for (i=0; i<
len; i++) {
14481 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
14482 ibf_dump_write_small_value(dump, index);
14489 ibf_offset_t reading_pos = offset;
14491 const long len = (long)ibf_load_small_value(load, &reading_pos);
14496 for (i=0; i<
len; i++) {
14497 const VALUE index = ibf_load_small_value(load, &reading_pos);
14501 if (header->frozen) {
14510ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
14514 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14515 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14517 ibf_dump_write_small_value(dump, key_index);
14518 ibf_dump_write_small_value(dump, val_index);
14519 return ST_CONTINUE;
14526 ibf_dump_write_small_value(dump, (
VALUE)
len);
14534 long len = (long)ibf_load_small_value(load, &offset);
14535 VALUE obj = header->frozen ? rb_hash_alloc_fixed_size(
rb_cHash,
len) : rb_hash_new_capa(
len);
14538 for (i = 0; i <
len; i++) {
14539 VALUE key_index = ibf_load_small_value(load, &offset);
14540 VALUE val_index = ibf_load_small_value(load, &offset);
14542 VALUE key = ibf_load_object(load, key_index);
14543 VALUE val = ibf_load_object(load, val_index);
14544 rb_hash_aset(obj, key, val);
14548 if (header->frozen) {
14556ibf_dump_cdhash_i(st_data_t key, st_data_t val, st_data_t ptr)
14560 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14562 ibf_dump_write_small_value(dump, key_index);
14563 ibf_dump_write_small_value(dump, val);
14564 return ST_CONTINUE;
14570 switch (imemo_type(obj)) {
14571 case imemo_cdhash: {
14572 st_table *tbl = rb_imemo_cdhash_tbl(obj);
14574 long len = tbl->num_entries;
14575 ibf_dump_write_small_value(dump, (
VALUE)
len);
14576 if (
len > 0) st_foreach(tbl, ibf_dump_cdhash_i, (
VALUE)dump);
14580 ibf_dump_object_unsupported(dump, obj);
14588 long len = (long)ibf_load_small_value(load, &offset);
14592 for (i = 0; i <
len; i++) {
14593 VALUE key_index = ibf_load_small_value(load, &offset);
14594 VALUE val = ibf_load_small_value(load, &offset);
14596 VALUE key = ibf_load_object(load, key_index);
14597 cdhash_aset(obj, key, val);
14611 range.class_index = 0;
14614 range.beg = (long)ibf_dump_object(dump, beg);
14615 range.end = (long)ibf_dump_object(dump, end);
14621 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
14630 VALUE beg = ibf_load_object(load, range->beg);
14631 VALUE end = ibf_load_object(load, range->end);
14641 ssize_t
len = BIGNUM_LEN(obj);
14642 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14643 BDIGIT *d = BIGNUM_DIGITS(obj);
14645 (void)IBF_W(&slen, ssize_t, 1);
14646 IBF_WP(d, BDIGIT,
len);
14653 int sign = bignum->slen > 0;
14654 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14655 const int big_unpack_flags =
14658 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
14669 if (rb_data_is_encoding(obj)) {
14671 const char *name = rb_enc_name(enc);
14672 long len = strlen(name) + 1;
14674 data[0] = IBF_OBJECT_DATA_ENCODING;
14676 (void)IBF_W(data,
long, 2);
14677 IBF_WP(name,
char,
len);
14680 ibf_dump_object_unsupported(dump, obj);
14687 const long *body = IBF_OBJBODY(
long, offset);
14688 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14690 const char *data = (
const char *)&body[2];
14693 case IBF_OBJECT_DATA_ENCODING:
14695 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
14700 return ibf_load_object_unsupported(load, header, offset);
14704ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14707 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14708 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14710 (void)IBF_W(data,
long, 2);
14714ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14717 VALUE a = ibf_load_object(load, nums->a);
14718 VALUE b = ibf_load_object(load, nums->b);
14730 ibf_dump_object_string(dump,
rb_sym2str(obj));
14736 ibf_offset_t reading_pos = offset;
14738 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14739 const long len = (long)ibf_load_small_value(load, &reading_pos);
14740 const char *ptr = load->current_buffer->buff + reading_pos;
14742 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14743 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14744 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14747 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
14751typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
14752static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
14753 ibf_dump_object_unsupported,
14754 ibf_dump_object_unsupported,
14755 ibf_dump_object_class,
14756 ibf_dump_object_unsupported,
14757 ibf_dump_object_float,
14758 ibf_dump_object_string,
14759 ibf_dump_object_regexp,
14760 ibf_dump_object_array,
14761 ibf_dump_object_hash,
14762 ibf_dump_object_struct,
14763 ibf_dump_object_bignum,
14764 ibf_dump_object_unsupported,
14765 ibf_dump_object_data,
14766 ibf_dump_object_unsupported,
14767 ibf_dump_object_complex_rational,
14768 ibf_dump_object_complex_rational,
14769 ibf_dump_object_unsupported,
14770 ibf_dump_object_unsupported,
14771 ibf_dump_object_unsupported,
14772 ibf_dump_object_unsupported,
14773 ibf_dump_object_symbol,
14774 ibf_dump_object_unsupported,
14775 ibf_dump_object_unsupported,
14776 ibf_dump_object_unsupported,
14777 ibf_dump_object_unsupported,
14778 ibf_dump_object_unsupported,
14779 ibf_dump_object_imemo,
14780 ibf_dump_object_unsupported,
14781 ibf_dump_object_unsupported,
14782 ibf_dump_object_unsupported,
14783 ibf_dump_object_unsupported,
14784 ibf_dump_object_unsupported,
14790 unsigned char byte =
14791 (header.type << 0) |
14792 (header.special_const << 5) |
14793 (header.frozen << 6) |
14794 (header.internal << 7);
14800ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
14802 unsigned char byte = ibf_load_byte(load, offset);
14805 header.type = (
byte >> 0) & 0x1f;
14806 header.special_const = (
byte >> 5) & 0x01;
14807 header.frozen = (
byte >> 6) & 0x01;
14808 header.internal = (
byte >> 7) & 0x01;
14817 ibf_offset_t current_offset;
14818 IBF_ZERO(obj_header);
14819 obj_header.type =
TYPE(obj);
14821 IBF_W_ALIGN(ibf_offset_t);
14822 current_offset = ibf_dump_pos(dump);
14827 obj_header.special_const = TRUE;
14828 obj_header.frozen = TRUE;
14829 obj_header.internal = TRUE;
14830 ibf_dump_object_object_header(dump, obj_header);
14831 ibf_dump_write_small_value(dump, obj);
14835 obj_header.special_const = FALSE;
14836 obj_header.frozen =
OBJ_FROZEN(obj) ? TRUE : FALSE;
14837 ibf_dump_object_object_header(dump, obj_header);
14838 (*dump_object_functions[obj_header.type])(dump, obj);
14841 return current_offset;
14845static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
14846 ibf_load_object_unsupported,
14847 ibf_load_object_unsupported,
14848 ibf_load_object_class,
14849 ibf_load_object_unsupported,
14850 ibf_load_object_float,
14851 ibf_load_object_string,
14852 ibf_load_object_regexp,
14853 ibf_load_object_array,
14854 ibf_load_object_hash,
14855 ibf_load_object_struct,
14856 ibf_load_object_bignum,
14857 ibf_load_object_unsupported,
14858 ibf_load_object_data,
14859 ibf_load_object_unsupported,
14860 ibf_load_object_complex_rational,
14861 ibf_load_object_complex_rational,
14862 ibf_load_object_unsupported,
14863 ibf_load_object_unsupported,
14864 ibf_load_object_unsupported,
14865 ibf_load_object_unsupported,
14866 ibf_load_object_symbol,
14867 ibf_load_object_unsupported,
14868 ibf_load_object_unsupported,
14869 ibf_load_object_unsupported,
14870 ibf_load_object_unsupported,
14871 ibf_load_object_unsupported,
14872 ibf_load_object_imemo,
14873 ibf_load_object_unsupported,
14874 ibf_load_object_unsupported,
14875 ibf_load_object_unsupported,
14876 ibf_load_object_unsupported,
14877 ibf_load_object_unsupported,
14881ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
14883 if (object_index == 0) {
14887 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
14889 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
14890 ibf_offset_t offset = offsets[object_index];
14891 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
14894 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
14895 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
14896 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
14897 header.type, header.special_const, header.frozen, header.internal);
14899 if (offset >= load->current_buffer->size) {
14900 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
14903 if (header.special_const) {
14904 ibf_offset_t reading_pos = offset;
14906 obj = ibf_load_small_value(load, &reading_pos);
14909 obj = (*load_object_functions[header.type])(load, &header, offset);
14912 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
14915 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
14916 object_index, obj);
14929ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14934 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
14937 return ST_CONTINUE;
14941ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
14943 st_table *obj_table = dump->current_buffer->obj_table;
14948 args.offset_list = offset_list;
14950 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
14952 IBF_W_ALIGN(ibf_offset_t);
14953 *obj_list_offset = ibf_dump_pos(dump);
14955 st_index_t size = obj_table->num_entries;
14958 for (i=0; i<size; i++) {
14963 *obj_list_size = (
unsigned int)size;
14967ibf_dump_mark(
void *ptr)
14970 rb_gc_mark(dump->global_buffer.str);
14972 rb_mark_set(dump->global_buffer.obj_table);
14973 rb_mark_set(dump->iseq_table);
14977ibf_dump_free(
void *ptr)
14980 if (dump->global_buffer.obj_table) {
14981 st_free_table(dump->global_buffer.obj_table);
14982 dump->global_buffer.obj_table = 0;
14984 if (dump->iseq_table) {
14985 st_free_table(dump->iseq_table);
14986 dump->iseq_table = 0;
14991ibf_dump_memsize(
const void *ptr)
14995 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
14996 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
15002 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
15003 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
15009 dump->global_buffer.obj_table = NULL;
15010 dump->iseq_table = NULL;
15013 dump->global_buffer.obj_table = ibf_dump_object_table_new();
15014 dump->iseq_table = st_init_numtable();
15016 dump->current_buffer = &dump->global_buffer;
15024 ibf_dump_write(dump, &header,
sizeof(header));
15025 ibf_dump_iseq(dump, iseq);
15027 header.magic[0] =
'Y';
15028 header.magic[1] =
'A';
15029 header.magic[2] =
'R';
15030 header.magic[3] =
'B';
15031 header.major_version = IBF_MAJOR_VERSION;
15032 header.minor_version = IBF_MINOR_VERSION;
15033 header.endian = IBF_ENDIAN_MARK;
15035 ibf_dump_iseq_list(dump, &header);
15036 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
15037 header.size = ibf_dump_pos(dump);
15040 VALUE opt_str = opt;
15043 ibf_dump_write(dump, ptr, header.extra_size);
15046 header.extra_size = 0;
15049 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
15051 return dump->global_buffer.str;
15061 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
15062 ISEQ_BODY(iseq)->local_iseq != iseq) {
15065 if (
RTEST(ISEQ_COVERAGE(iseq))) {
15070 ibf_dump_setup(dump, dump_obj);
15072 str = iseq_ibf_dump_0(dump, iseq, opt);
15077static const ibf_offset_t *
15078ibf_iseq_list(
const struct ibf_load *load)
15080 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
15084rb_ibf_load_iseq_complete(
rb_iseq_t *iseq)
15088 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
15091 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
15092 iseq->aux.loader.index, offset,
15093 load->header->size);
15095 ibf_load_iseq_each(load, iseq, offset);
15096 ISEQ_COMPILE_DATA_CLEAR(iseq);
15098 rb_iseq_init_trace(iseq);
15099 load->iseq = prev_src_iseq;
15106 rb_ibf_load_iseq_complete((
rb_iseq_t *)iseq);
15114 int iseq_index = (int)(
VALUE)index_iseq;
15117 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
15118 (
void *)index_iseq, (
void *)load->iseq_list);
15120 if (iseq_index == -1) {
15124 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
15127 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
15135 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
15139 iseq->aux.loader.index = iseq_index;
15141 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
15142 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
15144 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
15146 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
15148 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
15150 rb_ibf_load_iseq_complete(iseq);
15154 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
15155 (
void *)iseq, (
void *)load->iseq);
15163ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
15166 load->loader_obj = loader_obj;
15167 load->global_buffer.buff = bytes;
15168 load->header = header;
15169 load->global_buffer.size = header->size;
15170 load->global_buffer.obj_list_offset = header->global_object_list_offset;
15171 load->global_buffer.obj_list_size = header->global_object_list_size;
15172 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
15173 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
15176 load->current_buffer = &load->global_buffer;
15178 if (size < header->size) {
15181 if (strncmp(header->magic,
"YARB", 4) != 0) {
15184 if (header->major_version != IBF_MAJOR_VERSION ||
15185 header->minor_version != IBF_MINOR_VERSION) {
15187 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
15189 if (header->endian != IBF_ENDIAN_MARK) {
15195 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
15196 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
15197 header->iseq_list_offset);
15199 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
15200 rb_raise(rb_eArgError,
"unaligned object list offset: %u",
15201 load->global_buffer.obj_list_offset);
15214 if (USE_LAZY_LOAD) {
15215 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
15218 ibf_load_setup_bytes(load, loader_obj, RSTRING_PTR(str), RSTRING_LEN(str));
15223ibf_loader_mark(
void *ptr)
15226 rb_gc_mark(load->str);
15227 rb_gc_mark(load->iseq_list);
15228 rb_gc_mark(load->global_buffer.obj_list);
15232ibf_loader_free(
void *ptr)
15239ibf_loader_memsize(
const void *ptr)
15246 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
15247 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE
15251rb_iseq_ibf_load(
VALUE str)
15257 ibf_load_setup(load, loader_obj, str);
15258 iseq = ibf_load_iseq(load, 0);
15265rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
15271 ibf_load_setup_bytes(load, loader_obj, bytes, size);
15272 iseq = ibf_load_iseq(load, 0);
15280ibf_dump_iseq_table_collect_i(st_data_t key, st_data_t val, st_data_t arg)
15283 srcs[(int)val] = (
const rb_iseq_t *)key;
15284 return ST_CONTINUE;
15290iseq_subtree_copy_pc2branchindex(
const struct ibf_dump *dump,
const struct ibf_load *load)
15292 unsigned int iseq_count = (
unsigned int)dump->iseq_table->num_entries;
15295 st_foreach(dump->iseq_table, ibf_dump_iseq_table_collect_i, (st_data_t)srcs);
15297 for (
unsigned int i = 0; i < iseq_count; i++) {
15299 VALUE pc2branchindex = ISEQ_PC2BRANCHINDEX(srcs[i]);
15300 if (pc2branchindex !=
Qnil) {
15301 ISEQ_PC2BRANCHINDEX_SET(copy, pc2branchindex);
15310rb_iseq_dup_with_independent_caches(
const rb_iseq_t *src_root)
15318 ibf_dump_setup(dump, dump_obj);
15320 str = iseq_ibf_dump_0(dump, src_root,
Qnil);
15325 ibf_load_setup(load, loader_obj, str);
15333 unsigned int iseq_count = (
unsigned int)dump->iseq_table->num_entries;
15335 for (
unsigned int i = 0; i < iseq_count; i++) {
15339 rb_ibf_load_iseq_complete(copy);
15345 if (!cb->local_iseq)
RB_OBJ_WRITE(copy, &cb->local_iseq, sb->local_iseq);
15346 RB_OBJ_WRITE(copy, &cb->location.pathobj, sb->location.pathobj);
15347 VALUE sl = ISEQ_SCRIPT_LINES(src_root);
15348 VALUE cov = ISEQ_COVERAGE(src_root);
15356 RB_OBJ_WRITE(copy, &cb->parent_iseq, sb->parent_iseq);
15365 if (ISEQ_PC2BRANCHINDEX(src_root) !=
Qnil) {
15366 iseq_subtree_copy_pc2branchindex(dump, load);
15375rb_iseq_ibf_load_extra_data(
VALUE str)
15381 ibf_load_setup(load, loader_obj, str);
15382 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
15387#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)
Same as RB_OBJ_FREEZE(), but returns the given object.
#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_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
static VALUE RB_OBJ_SET_FROZEN_SHAREABLE(VALUE obj)
Freezes and marks the object as shareable.
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.