12#include "ruby/internal/config.h"
22#include "internal/array.h"
23#include "internal/compile.h"
24#include "internal/complex.h"
25#include "internal/encoding.h"
26#include "internal/error.h"
27#include "internal/gc.h"
28#include "internal/hash.h"
29#include "internal/io.h"
30#include "internal/numeric.h"
31#include "internal/object.h"
32#include "internal/rational.h"
33#include "internal/re.h"
34#include "internal/ruby_parser.h"
35#include "internal/symbol.h"
36#include "internal/thread.h"
37#include "internal/variable.h"
43#include "vm_callinfo.h"
49#include "insns_info.inc"
51#define FIXNUM_INC(n, i) ((n)+(INT2FIX(i)&~FIXNUM_FLAG))
85 unsigned int rescued: 2;
86 unsigned int unremovable: 1;
91 enum ruby_vminsn_type insn_id;
121 const void *ensure_node;
126const ID rb_iseq_shared_exc_local_tbl[] = {idERROR_INFO};
146#define compile_debug CPDEBUG
148#define compile_debug ISEQ_COMPILE_DATA(iseq)->option->debug_level
153#define compile_debug_print_indent(level) \
154 ruby_debug_print_indent((level), compile_debug, gl_node_level * 2)
156#define debugp(header, value) (void) \
157 (compile_debug_print_indent(1) && \
158 ruby_debug_print_value(1, compile_debug, (header), (value)))
160#define debugi(header, id) (void) \
161 (compile_debug_print_indent(1) && \
162 ruby_debug_print_id(1, compile_debug, (header), (id)))
164#define debugp_param(header, value) (void) \
165 (compile_debug_print_indent(1) && \
166 ruby_debug_print_value(1, compile_debug, (header), (value)))
168#define debugp_verbose(header, value) (void) \
169 (compile_debug_print_indent(2) && \
170 ruby_debug_print_value(2, compile_debug, (header), (value)))
172#define debugp_verbose_node(header, value) (void) \
173 (compile_debug_print_indent(10) && \
174 ruby_debug_print_value(10, compile_debug, (header), (value)))
176#define debug_node_start(node) ((void) \
177 (compile_debug_print_indent(1) && \
178 (ruby_debug_print_node(1, CPDEBUG, "", (const NODE *)(node)), gl_node_level)), \
181#define debug_node_end() gl_node_level --
185#define debugi(header, id) ((void)0)
186#define debugp(header, value) ((void)0)
187#define debugp_verbose(header, value) ((void)0)
188#define debugp_verbose_node(header, value) ((void)0)
189#define debugp_param(header, value) ((void)0)
190#define debug_node_start(node) ((void)0)
191#define debug_node_end() ((void)0)
194#if CPDEBUG > 1 || CPDEBUG < 0
196#define printf ruby_debug_printf
197#define debugs if (compile_debug_print_indent(1)) ruby_debug_printf
198#define debug_compile(msg, v) ((void)(compile_debug_print_indent(1) && fputs((msg), stderr)), (v))
200#define debugs if(0)printf
201#define debug_compile(msg, v) (v)
204#define LVAR_ERRINFO (1)
207#define NEW_LABEL(l) new_label_body(iseq, (l))
208#define LABEL_FORMAT "<L%03d>"
210#define NEW_ISEQ(node, name, type, line_no) \
211 new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
213#define NEW_CHILD_ISEQ(node, name, type, line_no) \
214 new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
216#define NEW_CHILD_ISEQ_WITH_CALLBACK(callback_func, name, type, line_no) \
217 new_child_iseq_with_callback(iseq, (callback_func), (name), iseq, (type), (line_no))
220#define ADD_SEQ(seq1, seq2) \
221 APPEND_LIST((seq1), (seq2))
224#define ADD_INSN(seq, line_node, insn) \
225 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 0))
228#define ADD_SYNTHETIC_INSN(seq, line_no, node_id, insn) \
229 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line_no), (node_id), BIN(insn), 0))
232#define INSERT_BEFORE_INSN(next, line_no, node_id, insn) \
233 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
236#define INSERT_AFTER_INSN(prev, line_no, node_id, insn) \
237 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
240#define ADD_INSN1(seq, line_node, insn, op1) \
241 ADD_ELEM((seq), (LINK_ELEMENT *) \
242 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 1, (VALUE)(op1)))
245#define INSERT_BEFORE_INSN1(next, line_no, node_id, insn, op1) \
246 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) \
247 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
250#define INSERT_AFTER_INSN1(prev, line_no, node_id, insn, op1) \
251 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) \
252 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
254#define LABEL_REF(label) ((label)->refcnt++)
257#define ADD_INSNL(seq, line_node, insn, label) (ADD_INSN1(seq, line_node, insn, label), LABEL_REF(label))
259#define ADD_INSN2(seq, line_node, insn, op1, op2) \
260 ADD_ELEM((seq), (LINK_ELEMENT *) \
261 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
263#define ADD_INSN3(seq, line_node, insn, op1, op2, op3) \
264 ADD_ELEM((seq), (LINK_ELEMENT *) \
265 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
268#define ADD_SEND(seq, line_node, id, argc) \
269 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
271#define ADD_SEND_WITH_FLAG(seq, line_node, id, argc, flag) \
272 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)(flag), NULL)
274#define ADD_SEND_WITH_BLOCK(seq, line_node, id, argc, block) \
275 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
277#define ADD_CALL_RECEIVER(seq, line_node) \
278 ADD_INSN((seq), (line_node), putself)
280#define ADD_CALL(seq, line_node, id, argc) \
281 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
283#define ADD_CALL_WITH_BLOCK(seq, line_node, id, argc, block) \
284 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
286#define ADD_SEND_R(seq, line_node, id, argc, block, flag, keywords) \
287 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
289#define ADD_TRACE(seq, event) \
290 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), 0))
291#define ADD_TRACE_WITH_DATA(seq, event, data) \
292 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), (data)))
294static void iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
295static void iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
297#define ADD_GETLOCAL(seq, line_node, idx, level) iseq_add_getlocal(iseq, (seq), (line_node), (idx), (level))
298#define ADD_SETLOCAL(seq, line_node, idx, level) iseq_add_setlocal(iseq, (seq), (line_node), (idx), (level))
301#define ADD_LABEL(seq, label) \
302 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
304#define APPEND_LABEL(seq, before, label) \
305 APPEND_ELEM((seq), (before), (LINK_ELEMENT *) (label))
307#define ADD_ADJUST(seq, line_node, label) \
308 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), nd_line(line_node)))
310#define ADD_ADJUST_RESTORE(seq, label) \
311 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
313#define LABEL_UNREMOVABLE(label) \
314 ((label) ? (LABEL_REF(label), (label)->unremovable=1) : 0)
315#define ADD_CATCH_ENTRY(type, ls, le, iseqv, lc) do { \
316 VALUE _e = rb_ary_new3(5, (type), \
317 (VALUE)(ls) | 1, (VALUE)(le) | 1, \
318 (VALUE)(iseqv), (VALUE)(lc) | 1); \
319 LABEL_UNREMOVABLE(ls); \
322 if (NIL_P(ISEQ_COMPILE_DATA(iseq)->catch_table_ary)) \
323 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, rb_ary_hidden_new(3)); \
324 rb_ary_push(ISEQ_COMPILE_DATA(iseq)->catch_table_ary, freeze_hide_obj(_e)); \
328#define COMPILE(anchor, desc, node) \
329 (debug_compile("== " desc "\n", \
330 iseq_compile_each(iseq, (anchor), (node), 0)))
333#define COMPILE_POPPED(anchor, desc, node) \
334 (debug_compile("== " desc "\n", \
335 iseq_compile_each(iseq, (anchor), (node), 1)))
338#define COMPILE_(anchor, desc, node, popped) \
339 (debug_compile("== " desc "\n", \
340 iseq_compile_each(iseq, (anchor), (node), (popped))))
342#define COMPILE_RECV(anchor, desc, node, recv) \
343 (private_recv_p(node) ? \
344 (ADD_INSN(anchor, node, putself), VM_CALL_FCALL) : \
345 COMPILE(anchor, desc, recv) ? 0 : -1)
347#define OPERAND_AT(insn, idx) \
348 (((INSN*)(insn))->operands[(idx)])
350#define INSN_OF(insn) \
351 (((INSN*)(insn))->insn_id)
353#define IS_INSN(link) ((link)->type == ISEQ_ELEMENT_INSN)
354#define IS_LABEL(link) ((link)->type == ISEQ_ELEMENT_LABEL)
355#define IS_ADJUST(link) ((link)->type == ISEQ_ELEMENT_ADJUST)
356#define IS_TRACE(link) ((link)->type == ISEQ_ELEMENT_TRACE)
357#define IS_INSN_ID(iobj, insn) (INSN_OF(iobj) == BIN(insn))
358#define IS_NEXT_INSN_ID(link, insn) \
359 ((link)->next && IS_INSN((link)->next) && IS_INSN_ID((link)->next, insn))
367append_compile_error(const rb_iseq_t *iseq,
int line, const
char *fmt, ...)
369 VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
370 VALUE file = rb_iseq_path(iseq);
375 err = rb_syntax_error_append(err, file, line, -1, NULL, fmt, args);
377 if (
NIL_P(err_info)) {
378 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err);
381 else if (!err_info) {
392compile_bug(rb_iseq_t *iseq,
int line,
const char *fmt, ...)
396 rb_report_bug_valist(rb_iseq_path(iseq), line, fmt, args);
402#define COMPILE_ERROR append_compile_error
404#define ERROR_ARGS_AT(n) iseq, nd_line(n),
405#define ERROR_ARGS ERROR_ARGS_AT(node)
407#define EXPECT_NODE(prefix, node, ndtype, errval) \
409 const NODE *error_node = (node); \
410 enum node_type error_type = nd_type(error_node); \
411 if (error_type != (ndtype)) { \
412 COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
413 prefix ": " #ndtype " is expected, but %s", \
414 ruby_node_name(error_type)); \
419#define EXPECT_NODE_NONULL(prefix, parent, ndtype, errval) \
421 COMPILE_ERROR(ERROR_ARGS_AT(parent) \
422 prefix ": must be " #ndtype ", but 0"); \
426#define UNKNOWN_NODE(prefix, node, errval) \
428 const NODE *error_node = (node); \
429 COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
430 ruby_node_name(nd_type(error_node))); \
437#define CHECK(sub) if (!(sub)) {BEFORE_RETURN;return COMPILE_NG;}
438#define NO_CHECK(sub) (void)(sub)
441#define DECL_ANCHOR(name) \
442 LINK_ANCHOR name[1] = {{{ISEQ_ELEMENT_ANCHOR,},&name[0].anchor}}
443#define INIT_ANCHOR(name) \
444 ((name->last = &name->anchor)->next = NULL)
447freeze_hide_obj(
VALUE obj)
450 RBASIC_CLEAR_CLASS(obj);
454#include "optinsn.inc"
455#if OPT_INSTRUCTIONS_UNIFICATION
456#include "optunifs.inc"
461#define ISEQ_ARG iseq,
462#define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
465#define ISEQ_ARG_DECLARE
469#define gl_node_level ISEQ_COMPILE_DATA(iseq)->node_level
472static void dump_disasm_list_with_cursor(
const LINK_ELEMENT *link,
const LINK_ELEMENT *curr,
const LABEL *dest);
473static void dump_disasm_list(
const LINK_ELEMENT *elem);
475static int insn_data_length(INSN *iobj);
476static int calc_sp_depth(
int depth, INSN *iobj);
478static INSN *new_insn_body(rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...);
479static LABEL *new_label_body(rb_iseq_t *iseq,
long line);
480static ADJUST *new_adjust_body(rb_iseq_t *iseq, LABEL *label,
int line);
481static TRACE *new_trace_body(rb_iseq_t *iseq,
rb_event_flag_t event,
long data);
484static int iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *anchor,
const NODE *n,
int);
485static int iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
486static int iseq_setup_insn(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
487static int iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
488static int iseq_insns_unification(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
490static int iseq_set_local_table(rb_iseq_t *iseq,
const rb_ast_id_table_t *tbl,
const NODE *
const node_args);
491static int iseq_set_exception_local_table(rb_iseq_t *iseq);
492static int iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor,
const NODE *
const node);
494static int iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
495static int iseq_set_exception_table(rb_iseq_t *iseq);
496static int iseq_set_optargs_table(rb_iseq_t *iseq);
497static int iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq);
499static int compile_defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE needstr,
bool ignore);
500static int compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped);
507verify_list(ISEQ_ARG_DECLARE
const char *info, LINK_ANCHOR *
const anchor)
511 LINK_ELEMENT *list, *plist;
513 if (!compile_debug)
return;
515 list = anchor->anchor.next;
516 plist = &anchor->anchor;
518 if (plist != list->prev) {
525 if (anchor->last != plist && anchor->last != 0) {
530 rb_bug(
"list verify error: %08x (%s)", flag, info);
535#define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
539verify_call_cache(rb_iseq_t *iseq)
542 VALUE *original = rb_iseq_original_iseq(iseq);
544 while (i < ISEQ_BODY(iseq)->iseq_size) {
545 VALUE insn = original[i];
546 const char *types = insn_op_types(insn);
548 for (
int j=0; types[j]; j++) {
549 if (types[j] == TS_CALLDATA) {
553 if (cc != vm_cc_empty()) {
555 rb_bug(
"call cache is not initialized by vm_cc_empty()");
562 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
563 struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
566 if (cc != NULL && cc != vm_cc_empty()) {
568 rb_bug(
"call cache is not initialized by vm_cc_empty()");
578ADD_ELEM(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anchor, LINK_ELEMENT *elem)
580 elem->prev = anchor->last;
581 anchor->last->next = elem;
583 verify_list(
"add", anchor);
590APPEND_ELEM(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anchor, LINK_ELEMENT *before, LINK_ELEMENT *elem)
593 elem->next = before->next;
594 elem->next->prev = elem;
596 if (before == anchor->last) anchor->last = elem;
597 verify_list(
"add", anchor);
600#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
601#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
605branch_coverage_valid_p(rb_iseq_t *iseq,
int first_line)
607 if (!ISEQ_COVERAGE(iseq))
return 0;
608 if (!ISEQ_BRANCH_COVERAGE(iseq))
return 0;
609 if (first_line <= 0)
return 0;
614setup_branch(
const rb_code_location_t *loc,
const char *
type,
VALUE structure,
VALUE key)
616 const int first_lineno = loc->beg_pos.lineno, first_column = loc->beg_pos.column;
617 const int last_lineno = loc->end_pos.lineno, last_column = loc->end_pos.column;
620 rb_hash_aset(structure, key, branch);
630decl_branch_base(rb_iseq_t *iseq,
VALUE key,
const rb_code_location_t *loc,
const char *
type)
632 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return Qundef;
643 VALUE branch_base = rb_hash_aref(structure, key);
646 if (
NIL_P(branch_base)) {
647 branch_base = setup_branch(loc,
type, structure, key);
660generate_dummy_line_node(
int lineno,
int node_id)
663 nd_set_line(&dummy, lineno);
664 nd_set_node_id(&dummy, node_id);
669add_trace_branch_coverage(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const rb_code_location_t *loc,
int node_id,
int branch_id,
const char *
type,
VALUE branches)
671 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return;
682 VALUE branch = rb_hash_aref(branches, key);
686 branch = setup_branch(loc,
type, branches, key);
696 ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
697 ADD_SYNTHETIC_INSN(seq, loc->end_pos.lineno, node_id, nop);
700#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
703validate_label(st_data_t name, st_data_t label, st_data_t arg)
705 rb_iseq_t *iseq = (rb_iseq_t *)arg;
706 LABEL *lobj = (LABEL *)label;
707 if (!lobj->link.next) {
709 COMPILE_ERROR(iseq, lobj->position,
710 "%"PRIsVALUE
": undefined label",
718validate_labels(rb_iseq_t *iseq, st_table *labels_table)
720 st_foreach(labels_table, validate_label, (st_data_t)iseq);
721 st_free_table(labels_table);
725get_nd_recv(
const NODE *node)
727 switch (nd_type(node)) {
729 return RNODE_CALL(node)->nd_recv;
731 return RNODE_OPCALL(node)->nd_recv;
735 return RNODE_QCALL(node)->nd_recv;
739 return RNODE_ATTRASGN(node)->nd_recv;
741 return RNODE_OP_ASGN1(node)->nd_recv;
743 return RNODE_OP_ASGN2(node)->nd_recv;
745 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
750get_node_call_nd_mid(
const NODE *node)
752 switch (nd_type(node)) {
754 return RNODE_CALL(node)->nd_mid;
756 return RNODE_OPCALL(node)->nd_mid;
758 return RNODE_FCALL(node)->nd_mid;
760 return RNODE_QCALL(node)->nd_mid;
762 return RNODE_VCALL(node)->nd_mid;
764 return RNODE_ATTRASGN(node)->nd_mid;
766 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
771get_nd_args(
const NODE *node)
773 switch (nd_type(node)) {
775 return RNODE_CALL(node)->nd_args;
777 return RNODE_OPCALL(node)->nd_args;
779 return RNODE_FCALL(node)->nd_args;
781 return RNODE_QCALL(node)->nd_args;
785 return RNODE_ATTRASGN(node)->nd_args;
787 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
792get_node_colon_nd_mid(
const NODE *node)
794 switch (nd_type(node)) {
796 return RNODE_COLON2(node)->nd_mid;
798 return RNODE_COLON3(node)->nd_mid;
800 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
805get_nd_vid(
const NODE *node)
807 switch (nd_type(node)) {
809 return RNODE_LASGN(node)->nd_vid;
811 return RNODE_DASGN(node)->nd_vid;
813 return RNODE_IASGN(node)->nd_vid;
815 return RNODE_CVASGN(node)->nd_vid;
817 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
822get_nd_value(
const NODE *node)
824 switch (nd_type(node)) {
826 return RNODE_LASGN(node)->nd_value;
828 return RNODE_DASGN(node)->nd_value;
830 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
835get_string_value(
const NODE *node)
837 switch (nd_type(node)) {
839 return RB_OBJ_SET_SHAREABLE(rb_node_str_string_val(node));
841 return RB_OBJ_SET_SHAREABLE(rb_node_file_path_val(node));
843 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
853 (*ifunc->func)(iseq, ret, ifunc->data);
855 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
857 CHECK(iseq_setup_insn(iseq, ret));
858 return iseq_setup(iseq, ret);
861static bool drop_unreachable_return(LINK_ANCHOR *ret);
864rb_iseq_compile_node(rb_iseq_t *iseq,
const NODE *node)
870 NO_CHECK(COMPILE(ret,
"nil", node));
871 iseq_set_local_table(iseq, 0, 0);
874 else if (nd_type_p(node, NODE_SCOPE)) {
876 iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl, (NODE *)RNODE_SCOPE(node)->nd_args);
877 iseq_set_arguments(iseq, ret, (NODE *)RNODE_SCOPE(node)->nd_args);
878 iseq_set_parameters_lvar_state(iseq);
880 switch (ISEQ_BODY(iseq)->
type) {
881 case ISEQ_TYPE_BLOCK:
883 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
884 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
886 start->rescued = LABEL_RESCUE_BEG;
887 end->rescued = LABEL_RESCUE_END;
890 ADD_SYNTHETIC_INSN(ret, ISEQ_BODY(iseq)->location.first_lineno, -1, nop);
891 ADD_LABEL(ret, start);
892 CHECK(COMPILE(ret,
"block body", RNODE_SCOPE(node)->nd_body));
895 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
898 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
899 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
902 case ISEQ_TYPE_CLASS:
905 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
907 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
910 case ISEQ_TYPE_METHOD:
912 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
914 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
915 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
917 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
921 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
928#define INVALID_ISEQ_TYPE(type) \
929 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
930 switch (ISEQ_BODY(iseq)->
type) {
931 case INVALID_ISEQ_TYPE(
METHOD);
932 case INVALID_ISEQ_TYPE(CLASS);
933 case INVALID_ISEQ_TYPE(BLOCK);
934 case INVALID_ISEQ_TYPE(EVAL);
935 case INVALID_ISEQ_TYPE(MAIN);
936 case INVALID_ISEQ_TYPE(TOP);
937#undef INVALID_ISEQ_TYPE
938 case ISEQ_TYPE_RESCUE:
939 iseq_set_exception_local_table(iseq);
940 CHECK(COMPILE(ret,
"rescue", node));
942 case ISEQ_TYPE_ENSURE:
943 iseq_set_exception_local_table(iseq);
944 CHECK(COMPILE_POPPED(ret,
"ensure", node));
946 case ISEQ_TYPE_PLAIN:
947 CHECK(COMPILE(ret,
"ensure", node));
950 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
953 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
958 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
959 NODE dummy_line_node = generate_dummy_line_node(0, -1);
960 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
961 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
963 else if (!drop_unreachable_return(ret)) {
964 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
968 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
969 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
970 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
971 validate_labels(iseq, labels_table);
974 CHECK(iseq_setup_insn(iseq, ret));
975 return iseq_setup(iseq, ret);
979rb_iseq_translate_threaded_code(rb_iseq_t *iseq)
981#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
982 const void *
const *table = rb_vm_get_insns_address_table();
984 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
986 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
987 int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
988 int len = insn_len(insn);
989 encoded[i] = (
VALUE)table[insn];
996 rb_yjit_live_iseq_count++;
997 rb_yjit_iseq_alloc_count++;
1004rb_iseq_original_iseq(
const rb_iseq_t *iseq)
1006 VALUE *original_code;
1008 if (ISEQ_ORIGINAL_ISEQ(iseq))
return ISEQ_ORIGINAL_ISEQ(iseq);
1009 original_code = ISEQ_ORIGINAL_ISEQ_ALLOC(iseq, ISEQ_BODY(iseq)->iseq_size);
1010 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
1012#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1016 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1017 const void *addr = (
const void *)original_code[i];
1018 const int insn = rb_vm_insn_addr2insn(addr);
1020 original_code[i] = insn;
1021 i += insn_len(insn);
1025 return original_code;
1038#if defined(__sparc) && SIZEOF_VOIDP == 4 && defined(__GNUC__)
1039 #define STRICT_ALIGNMENT
1045#if defined(__OpenBSD__)
1046 #include <sys/endian.h>
1047 #ifdef __STRICT_ALIGNMENT
1048 #define STRICT_ALIGNMENT
1052#ifdef STRICT_ALIGNMENT
1053 #if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
1054 #define ALIGNMENT_SIZE SIZEOF_LONG_LONG
1056 #define ALIGNMENT_SIZE SIZEOF_VALUE
1058 #define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
1059 #define ALIGNMENT_SIZE_MASK PADDING_SIZE_MAX
1062 #define PADDING_SIZE_MAX 0
1065#ifdef STRICT_ALIGNMENT
1068calc_padding(
void *ptr,
size_t size)
1073 mis = (size_t)ptr & ALIGNMENT_SIZE_MASK;
1075 padding = ALIGNMENT_SIZE - mis;
1081#if ALIGNMENT_SIZE > SIZEOF_VALUE
1082 if (size ==
sizeof(
VALUE) && padding ==
sizeof(
VALUE)) {
1096#ifdef STRICT_ALIGNMENT
1097 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1099 const size_t padding = 0;
1103 if (storage->pos + size + padding > storage->size) {
1104 unsigned int alloc_size = storage->size;
1106 while (alloc_size < size + PADDING_SIZE_MAX) {
1110 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
1112 storage = *arena = storage->next;
1115 storage->size = alloc_size;
1116#ifdef STRICT_ALIGNMENT
1117 padding = calc_padding((
void *)&storage->buff[storage->pos], size);
1121#ifdef STRICT_ALIGNMENT
1122 storage->pos += (int)padding;
1125 ptr = (
void *)&storage->buff[storage->pos];
1126 storage->pos += (int)size;
1131compile_data_alloc(rb_iseq_t *iseq,
size_t size)
1134 return compile_data_alloc_with_arena(arena, size);
1138compile_data_alloc2(rb_iseq_t *iseq,
size_t x,
size_t y)
1141 return compile_data_alloc(iseq, size);
1145compile_data_calloc2(rb_iseq_t *iseq,
size_t x,
size_t y)
1148 void *p = compile_data_alloc(iseq, size);
1154compile_data_alloc_insn(rb_iseq_t *iseq)
1157 return (INSN *)compile_data_alloc_with_arena(arena,
sizeof(INSN));
1161compile_data_alloc_label(rb_iseq_t *iseq)
1163 return (LABEL *)compile_data_alloc(iseq,
sizeof(LABEL));
1167compile_data_alloc_adjust(rb_iseq_t *iseq)
1169 return (ADJUST *)compile_data_alloc(iseq,
sizeof(ADJUST));
1173compile_data_alloc_trace(rb_iseq_t *iseq)
1175 return (TRACE *)compile_data_alloc(iseq,
sizeof(TRACE));
1182ELEM_INSERT_NEXT(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
1184 elem2->next = elem1->next;
1185 elem2->prev = elem1;
1186 elem1->next = elem2;
1188 elem2->next->prev = elem2;
1196ELEM_INSERT_PREV(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
1198 elem2->prev = elem1->prev;
1199 elem2->next = elem1;
1200 elem1->prev = elem2;
1202 elem2->prev->next = elem2;
1210ELEM_REPLACE(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
1212 elem2->prev = elem1->prev;
1213 elem2->next = elem1->next;
1215 elem1->prev->next = elem2;
1218 elem1->next->prev = elem2;
1223ELEM_REMOVE(LINK_ELEMENT *elem)
1225 elem->prev->next = elem->next;
1227 elem->next->prev = elem->prev;
1231static LINK_ELEMENT *
1232FIRST_ELEMENT(
const LINK_ANCHOR *
const anchor)
1234 return anchor->anchor.next;
1237static LINK_ELEMENT *
1238LAST_ELEMENT(LINK_ANCHOR *
const anchor)
1240 return anchor->last;
1243static LINK_ELEMENT *
1244ELEM_FIRST_INSN(LINK_ELEMENT *elem)
1247 switch (elem->type) {
1248 case ISEQ_ELEMENT_INSN:
1249 case ISEQ_ELEMENT_ADJUST:
1259LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1261 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1262 if (first_insn != NULL &&
1263 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1272LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1274 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1290APPEND_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anc1, LINK_ANCHOR *
const anc2)
1292 if (anc2->anchor.next) {
1295 anc1->last->next = anc2->anchor.next;
1296 anc2->anchor.next->prev = anc1->last;
1297 anc1->last = anc2->last;
1302 verify_list(
"append", anc1);
1305#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1310debug_list(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anchor, LINK_ELEMENT *cur)
1312 LINK_ELEMENT *list = FIRST_ELEMENT(anchor);
1314 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1315 (
void *)anchor->anchor.next, (
void *)anchor->last);
1317 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1318 (
void *)list->prev, (
int)list->type);
1323 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1324 verify_list(
"debug list", anchor);
1327#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1330#define debug_list(anc, cur) ((void)0)
1336 TRACE *trace = compile_data_alloc_trace(iseq);
1338 trace->link.type = ISEQ_ELEMENT_TRACE;
1339 trace->link.next = NULL;
1340 trace->event = event;
1347new_label_body(rb_iseq_t *iseq,
long line)
1349 LABEL *labelobj = compile_data_alloc_label(iseq);
1351 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1352 labelobj->link.next = 0;
1354 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1355 labelobj->sc_state = 0;
1357 labelobj->refcnt = 0;
1359 labelobj->rescued = LABEL_RESCUE_NONE;
1360 labelobj->unremovable = 0;
1361 labelobj->position = -1;
1366new_adjust_body(rb_iseq_t *iseq, LABEL *label,
int line)
1368 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1369 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1370 adjust->link.next = 0;
1371 adjust->label = label;
1372 adjust->line_no = line;
1373 LABEL_UNREMOVABLE(label);
1378iseq_insn_each_markable_object(INSN *insn,
void (*func)(
VALUE *,
VALUE),
VALUE data)
1380 const char *types = insn_op_types(insn->insn_id);
1381 for (
int j = 0; types[j]; j++) {
1382 char type = types[j];
1389 func(&OPERAND_AT(insn, j), data);
1398iseq_insn_each_object_write_barrier(
VALUE * obj,
VALUE iseq)
1407new_insn_core(rb_iseq_t *iseq,
int line_no,
int node_id,
int insn_id,
int argc,
VALUE *argv)
1409 INSN *iobj = compile_data_alloc_insn(iseq);
1413 iobj->link.type = ISEQ_ELEMENT_INSN;
1414 iobj->link.next = 0;
1415 iobj->insn_id = insn_id;
1416 iobj->insn_info.line_no = line_no;
1417 iobj->insn_info.node_id = node_id;
1418 iobj->insn_info.events = 0;
1419 iobj->operands = argv;
1420 iobj->operand_size = argc;
1423 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1429new_insn_body(rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...)
1431 VALUE *operands = 0;
1435 va_start(argv, argc);
1436 operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
1437 for (i = 0; i < argc; i++) {
1443 return new_insn_core(iseq, line_no, node_id, insn_id, argc, operands);
1447insn_replace_with_operands(rb_iseq_t *iseq, INSN *iobj,
enum ruby_vminsn_type insn_id,
int argc, ...)
1449 VALUE *operands = 0;
1453 va_start(argv, argc);
1454 operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
1455 for (i = 0; i < argc; i++) {
1462 iobj->insn_id = insn_id;
1463 iobj->operand_size = argc;
1464 iobj->operands = operands;
1465 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1471new_callinfo(rb_iseq_t *iseq,
ID mid,
int argc,
unsigned int flag,
struct rb_callinfo_kwarg *kw_arg,
int has_blockiseq)
1473 VM_ASSERT(argc >= 0);
1476 flag |= VM_CALL_KWARG;
1477 argc += kw_arg->keyword_len;
1480 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING))
1481 && !has_blockiseq) {
1482 flag |= VM_CALL_ARGS_SIMPLE;
1485 ISEQ_BODY(iseq)->ci_size++;
1486 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1492new_insn_send(rb_iseq_t *iseq,
int line_no,
int node_id,
ID id,
VALUE argc,
const rb_iseq_t *blockiseq,
VALUE flag,
struct rb_callinfo_kwarg *keywords)
1494 VALUE *operands = compile_data_calloc2(iseq,
sizeof(
VALUE), 2);
1497 operands[1] = (
VALUE)blockiseq;
1504 if (vm_ci_flag((
struct rb_callinfo *)ci) & VM_CALL_FORWARDING) {
1505 insn = new_insn_core(iseq, line_no, node_id, BIN(sendforward), 2, operands);
1508 insn = new_insn_core(iseq, line_no, node_id, BIN(send), 2, operands);
1517new_child_iseq(rb_iseq_t *iseq,
const NODE *
const node,
1518 VALUE name,
const rb_iseq_t *parent,
enum rb_iseq_type
type,
int line_no)
1520 rb_iseq_t *ret_iseq;
1521 VALUE ast_value = rb_ruby_ast_new(node);
1523 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1524 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1525 ret_iseq = rb_iseq_new_with_opt(ast_value, name,
1526 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1528 isolated_depth ? isolated_depth + 1 : 0,
1529 type, ISEQ_COMPILE_DATA(iseq)->option,
1530 ISEQ_BODY(iseq)->variable.script_lines);
1531 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1537 VALUE name,
const rb_iseq_t *parent,
enum rb_iseq_type
type,
int line_no)
1539 rb_iseq_t *ret_iseq;
1541 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1542 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1543 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1544 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1545 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1550set_catch_except_p(rb_iseq_t *iseq)
1553 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1554 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1555 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1556 set_catch_except_p((rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1579 while (pos < body->iseq_size) {
1580 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1581 if (insn == BIN(
throw)) {
1582 set_catch_except_p(iseq);
1585 pos += insn_len(insn);
1591 for (i = 0; i < ct->size; i++) {
1593 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1594 if (entry->type != CATCH_TYPE_BREAK
1595 && entry->type != CATCH_TYPE_NEXT
1596 && entry->type != CATCH_TYPE_REDO) {
1598 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1605iseq_insert_nop_between_end_and_cont(rb_iseq_t *iseq)
1607 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1608 if (
NIL_P(catch_table_ary))
return;
1609 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1611 for (i = 0; i < tlen; i++) {
1613 LINK_ELEMENT *end = (LINK_ELEMENT *)(ptr[2] & ~1);
1614 LINK_ELEMENT *cont = (LINK_ELEMENT *)(ptr[4] & ~1);
1617 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1619 if (ct != CATCH_TYPE_BREAK
1620 && ct != CATCH_TYPE_NEXT
1621 && ct != CATCH_TYPE_REDO) {
1623 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1625 INSN *nop = new_insn_core(iseq, 0, -1, BIN(nop), 0, 0);
1626 ELEM_INSERT_NEXT(end, &nop->link);
1637iseq_setup_insn(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
1639 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1644 if (compile_debug > 5)
1645 dump_disasm_list(FIRST_ELEMENT(anchor));
1647 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1648 iseq_optimize(iseq, anchor);
1650 if (compile_debug > 5)
1651 dump_disasm_list(FIRST_ELEMENT(anchor));
1653 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1654 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1655 iseq_insns_unification(iseq, anchor);
1656 if (compile_debug > 5)
1657 dump_disasm_list(FIRST_ELEMENT(anchor));
1660 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1661 iseq_insert_nop_between_end_and_cont(iseq);
1662 if (compile_debug > 5)
1663 dump_disasm_list(FIRST_ELEMENT(anchor));
1669iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
1671 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1674 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1675 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1676 if (compile_debug > 5)
1677 dump_disasm_list(FIRST_ELEMENT(anchor));
1679 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1680 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1682 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1683 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1685 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1686 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1688 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1690 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1692 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1694 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1695 xfree(ISEQ_BODY(iseq)->catch_table);
1696 ISEQ_BODY(iseq)->catch_table = NULL;
1699#if VM_INSN_INFO_TABLE_IMPL == 2
1700 if (ISEQ_BODY(iseq)->insns_info.succ_index_table == NULL) {
1701 debugs(
"[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
1702 rb_iseq_insns_info_encode_positions(iseq);
1706 if (compile_debug > 1) {
1707 VALUE str = rb_iseq_disasm(iseq);
1710 verify_call_cache(iseq);
1711 debugs(
"[compile step: finish]\n");
1717iseq_set_exception_local_table(rb_iseq_t *iseq)
1719 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1720 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1721 ISEQ_BODY(iseq)->lvar_states = NULL;
1726get_lvar_level(
const rb_iseq_t *iseq)
1729 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1731 iseq = ISEQ_BODY(iseq)->parent_iseq;
1737get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1741 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1742 if (ISEQ_BODY(iseq)->local_table[i] ==
id) {
1750get_local_var_idx(
const rb_iseq_t *iseq,
ID id)
1752 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1755 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1756 "get_local_var_idx: %d", idx);
1763get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1765 int lv = 0, idx = -1;
1766 const rb_iseq_t *
const topmost_iseq = iseq;
1769 idx = get_dyna_var_idx_at_raw(iseq,
id);
1773 iseq = ISEQ_BODY(iseq)->parent_iseq;
1778 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1779 "get_dyna_var_idx: -1");
1783 *ls = ISEQ_BODY(iseq)->local_table_size;
1788iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1792 iseq = ISEQ_BODY(iseq)->parent_iseq;
1795 body = ISEQ_BODY(iseq);
1796 if (body->local_iseq == iseq &&
1797 body->param.flags.has_block &&
1798 body->local_table_size - body->param.block_start == idx) {
1807iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1810 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1811 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1822access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1824 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1826 if (isolated_depth && level >= isolated_depth) {
1827 if (
id == rb_intern(
"yield")) {
1828 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1831 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable '%s' from isolated Proc", rb_id2name(
id));
1835 for (
int i=0; i<level; i++) {
1837 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1840 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1843 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1844 if (write && !val) {
1845 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1849 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1852 iseq = ISEQ_BODY(iseq)->parent_iseq;
1857iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1859 for (
int i=0; i<level; i++) {
1860 iseq = ISEQ_BODY(iseq)->parent_iseq;
1863 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1869update_lvar_state(
const rb_iseq_t *iseq,
int level,
int idx)
1871 for (
int i=0; i<level; i++) {
1872 iseq = ISEQ_BODY(iseq)->parent_iseq;
1875 enum lvar_state *states = ISEQ_BODY(iseq)->lvar_states;
1876 int table_idx = ISEQ_BODY(iseq)->local_table_size - idx;
1877 switch (states[table_idx]) {
1878 case lvar_uninitialized:
1879 states[table_idx] = lvar_initialized;
1881 case lvar_initialized:
1882 states[table_idx] = lvar_reassigned;
1884 case lvar_reassigned:
1888 rb_bug(
"unreachable");
1893iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq)
1895 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
1896 ISEQ_BODY(iseq)->lvar_states[i] = lvar_initialized;
1899 int lead_num = ISEQ_BODY(iseq)->param.lead_num;
1900 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
1901 for (
int i=0; i<opt_num; i++) {
1902 ISEQ_BODY(iseq)->lvar_states[lead_num + i] = lvar_uninitialized;
1909iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level)
1911 if (iseq_local_block_param_p(iseq, idx, level)) {
1912 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1915 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1917 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1921iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level)
1923 if (iseq_local_block_param_p(iseq, idx, level)) {
1924 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1927 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1929 update_lvar_state(iseq, level, idx);
1930 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
1936iseq_calc_param_size(rb_iseq_t *iseq)
1939 if (body->param.flags.has_opt ||
1940 body->param.flags.has_post ||
1941 body->param.flags.has_rest ||
1942 body->param.flags.has_block ||
1943 body->param.flags.has_kw ||
1944 body->param.flags.has_kwrest) {
1946 if (body->param.flags.has_block) {
1947 body->param.size = body->param.block_start + 1;
1949 else if (body->param.flags.has_kwrest) {
1950 body->param.size = body->param.keyword->rest_start + 1;
1952 else if (body->param.flags.has_kw) {
1953 body->param.size = body->param.keyword->bits_start + 1;
1955 else if (body->param.flags.has_post) {
1956 body->param.size = body->param.post_start + body->param.post_num;
1958 else if (body->param.flags.has_rest) {
1959 body->param.size = body->param.rest_start + 1;
1961 else if (body->param.flags.has_opt) {
1962 body->param.size = body->param.lead_num + body->param.opt_num;
1969 body->param.size = body->param.lead_num;
1974iseq_set_arguments_keywords(rb_iseq_t *iseq, LINK_ANCHOR *
const optargs,
1977 const rb_node_kw_arg_t *node = args->kw_args;
1979 struct rb_iseq_param_keyword *keyword;
1982 int kw = 0, rkw = 0, di = 0, i;
1984 body->param.flags.has_kw = TRUE;
1985 body->param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
1989 node = node->nd_next;
1992 keyword->bits_start = arg_size++;
1994 node = args->kw_args;
1996 const NODE *val_node = get_nd_value(node->nd_body);
1999 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
2003 switch (nd_type(val_node)) {
2005 dv = rb_node_sym_string_val(val_node);
2008 dv = rb_node_regx_string_val(val_node);
2011 dv = rb_node_line_lineno_val(val_node);
2014 dv = rb_node_integer_literal_val(val_node);
2017 dv = rb_node_float_literal_val(val_node);
2020 dv = rb_node_rational_literal_val(val_node);
2022 case NODE_IMAGINARY:
2023 dv = rb_node_imaginary_literal_val(val_node);
2026 dv = rb_node_encoding_val(val_node);
2038 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
2042 keyword->num = ++di;
2046 node = node->nd_next;
2051 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
2052 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2053 keyword->rest_start = arg_size++;
2054 body->param.flags.has_kwrest = TRUE;
2056 if (kw_id == idPow) body->param.flags.anon_kwrest = TRUE;
2058 keyword->required_num = rkw;
2059 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
2064 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
2066 if (dv == complex_mark) dv =
Qundef;
2071 keyword->default_values = dvs;
2077iseq_set_use_block(rb_iseq_t *iseq)
2080 if (!body->param.flags.use_block) {
2081 body->param.flags.use_block = 1;
2083 rb_vm_t *vm = GET_VM();
2086 st_data_t key = (st_data_t)rb_intern_str(body->location.label);
2087 set_insert(vm->unused_block_warning_table, key);
2093iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *
const optargs,
const NODE *
const node_args)
2095 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2099 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2105 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2107 body->param.lead_num = arg_size = (int)args->pre_args_num;
2108 if (body->param.lead_num > 0) body->param.flags.has_lead = TRUE;
2109 debugs(
" - argc: %d\n", body->param.lead_num);
2111 rest_id = args->rest_arg;
2112 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2116 block_id = args->block_arg;
2118 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2120 if (optimized_forward) {
2125 if (args->opt_args) {
2126 const rb_node_opt_arg_t *node = args->opt_args;
2133 label = NEW_LABEL(nd_line(RNODE(node)));
2135 ADD_LABEL(optargs, label);
2136 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2137 node = node->nd_next;
2142 label = NEW_LABEL(nd_line(node_args));
2144 ADD_LABEL(optargs, label);
2149 for (j = 0; j < i+1; j++) {
2154 body->param.flags.has_opt = TRUE;
2155 body->param.opt_num = i;
2156 body->param.opt_table = opt_table;
2161 body->param.rest_start = arg_size++;
2162 body->param.flags.has_rest = TRUE;
2163 if (rest_id ==
'*') body->param.flags.anon_rest = TRUE;
2167 if (args->first_post_arg) {
2168 body->param.post_start = arg_size;
2169 body->param.post_num = args->post_args_num;
2170 body->param.flags.has_post = TRUE;
2171 arg_size += args->post_args_num;
2173 if (body->param.flags.has_rest) {
2174 body->param.post_start = body->param.rest_start + 1;
2178 if (args->kw_args) {
2179 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2181 else if (args->kw_rest_arg && !optimized_forward) {
2182 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2183 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2184 keyword->rest_start = arg_size++;
2185 body->param.keyword = keyword;
2186 body->param.flags.has_kwrest = TRUE;
2188 static ID anon_kwrest = 0;
2189 if (!anon_kwrest) anon_kwrest = rb_intern(
"**");
2190 if (kw_id == anon_kwrest) body->param.flags.anon_kwrest = TRUE;
2192 else if (args->no_kwarg) {
2193 body->param.flags.accepts_no_kwarg = TRUE;
2197 body->param.block_start = arg_size++;
2198 body->param.flags.has_block = TRUE;
2199 iseq_set_use_block(iseq);
2203 if (optimized_forward) {
2204 body->param.flags.use_block = 1;
2205 body->param.flags.forwardable = TRUE;
2209 iseq_calc_param_size(iseq);
2210 body->param.size = arg_size;
2212 if (args->pre_init) {
2213 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2215 if (args->post_init) {
2216 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2219 if (body->type == ISEQ_TYPE_BLOCK) {
2220 if (body->param.flags.has_opt == FALSE &&
2221 body->param.flags.has_post == FALSE &&
2222 body->param.flags.has_rest == FALSE &&
2223 body->param.flags.has_kw == FALSE &&
2224 body->param.flags.has_kwrest == FALSE) {
2226 if (body->param.lead_num == 1 && last_comma == 0) {
2228 body->param.flags.ambiguous_param0 = TRUE;
2238iseq_set_local_table(rb_iseq_t *iseq,
const rb_ast_id_table_t *tbl,
const NODE *
const node_args)
2240 unsigned int size = tbl ? tbl->size : 0;
2241 unsigned int offset = 0;
2244 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2249 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2258 MEMCPY(ids, tbl->ids + offset,
ID, size);
2259 ISEQ_BODY(iseq)->local_table = ids;
2261 enum lvar_state *states =
ALLOC_N(
enum lvar_state, size);
2263 for (
unsigned int i=0; i<size; i++) {
2264 states[i] = lvar_uninitialized;
2267 ISEQ_BODY(iseq)->lvar_states = states;
2269 ISEQ_BODY(iseq)->local_table_size = size;
2271 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2283 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2286 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2289 else if (tlit != tval) {
2299 long x =
FIX2LONG(rb_big_cmp(lit, val));
2307 return rb_float_cmp(lit, val);
2310 const struct RRational *rat1 = RRATIONAL(val);
2311 const struct RRational *rat2 = RRATIONAL(lit);
2312 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2315 const struct RComplex *comp1 = RCOMPLEX(val);
2316 const struct RComplex *comp2 = RCOMPLEX(lit);
2317 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2320 return rb_reg_equal(val, lit) ? 0 : -1;
2328rb_iseq_cdhash_hash(
VALUE a)
2330 switch (OBJ_BUILTIN_TYPE(a)) {
2333 return (st_index_t)a;
2341 return rb_rational_hash(a);
2343 return rb_complex_hash(a);
2351static const struct st_hash_type cdhash_type = {
2353 rb_iseq_cdhash_hash,
2366 LABEL *lobj = (LABEL *)(val & ~1);
2367 rb_hash_aset(data->hash, key,
INT2FIX(lobj->position - (data->pos+data->len)));
2373get_ivar_ic_value(rb_iseq_t *iseq,
ID id)
2375 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2379get_cvar_ic_value(rb_iseq_t *iseq,
ID id)
2382 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2384 if (rb_id_table_lookup(tbl,
id,&val)) {
2389 tbl = rb_id_table_create(1);
2390 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2392 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2393 rb_id_table_insert(tbl,
id,val);
2397#define BADINSN_DUMP(anchor, list, dest) \
2398 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2400#define BADINSN_ERROR \
2401 (xfree(generated_iseq), \
2402 xfree(insns_info), \
2403 BADINSN_DUMP(anchor, list, NULL), \
2407fix_sp_depth(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
2409 int stack_max = 0, sp = 0, line = 0;
2412 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2413 if (IS_LABEL(list)) {
2414 LABEL *lobj = (LABEL *)list;
2419 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2420 switch (list->type) {
2421 case ISEQ_ELEMENT_INSN:
2426 INSN *iobj = (INSN *)list;
2429 sp = calc_sp_depth(sp, iobj);
2431 BADINSN_DUMP(anchor, list, NULL);
2432 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2433 "argument stack underflow (%d)", sp);
2436 if (sp > stack_max) {
2440 line = iobj->insn_info.line_no;
2442 operands = iobj->operands;
2443 insn = iobj->insn_id;
2444 types = insn_op_types(insn);
2445 len = insn_len(insn);
2448 if (iobj->operand_size !=
len - 1) {
2450 BADINSN_DUMP(anchor, list, NULL);
2451 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2452 "operand size miss! (%d for %d)",
2453 iobj->operand_size,
len - 1);
2457 for (j = 0; types[j]; j++) {
2458 if (types[j] == TS_OFFSET) {
2460 LABEL *lobj = (LABEL *)operands[j];
2462 BADINSN_DUMP(anchor, list, NULL);
2463 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2464 "unknown label: "LABEL_FORMAT, lobj->label_no);
2467 if (lobj->sp == -1) {
2470 else if (lobj->sp != sp) {
2471 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2472 RSTRING_PTR(rb_iseq_path(iseq)), line,
2473 lobj->label_no, lobj->sp, sp);
2479 case ISEQ_ELEMENT_LABEL:
2481 LABEL *lobj = (LABEL *)list;
2482 if (lobj->sp == -1) {
2486 if (lobj->sp != sp) {
2487 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2488 RSTRING_PTR(rb_iseq_path(iseq)), line,
2489 lobj->label_no, lobj->sp, sp);
2495 case ISEQ_ELEMENT_TRACE:
2500 case ISEQ_ELEMENT_ADJUST:
2502 ADJUST *adjust = (ADJUST *)list;
2505 sp = adjust->label ? adjust->label->sp : 0;
2506 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2507 BADINSN_DUMP(anchor, list, NULL);
2508 COMPILE_ERROR(iseq, adjust->line_no,
2509 "iseq_set_sequence: adjust bug %d < %d",
2516 BADINSN_DUMP(anchor, list, NULL);
2517 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2526 int insns_info_index,
int code_index,
const INSN *iobj)
2528 if (insns_info_index == 0 ||
2529 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2530#ifdef USE_ISEQ_NODE_ID
2531 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2533 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2534 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2535#ifdef USE_ISEQ_NODE_ID
2536 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2538 insns_info[insns_info_index].events = iobj->insn_info.events;
2539 positions[insns_info_index] = code_index;
2547 int insns_info_index,
int code_index,
const ADJUST *adjust)
2549 insns_info[insns_info_index].line_no = adjust->line_no;
2550 insns_info[insns_info_index].node_id = -1;
2551 insns_info[insns_info_index].events = 0;
2552 positions[insns_info_index] = code_index;
2557array_to_idlist(
VALUE arr)
2562 for (
long i = 0; i < size; i++) {
2571idlist_to_array(
const ID *ids)
2584iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
2588 unsigned int *positions;
2590 VALUE *generated_iseq;
2594 int insn_num, code_index, insns_info_index, sp = 0;
2595 int stack_max = fix_sp_depth(iseq, anchor);
2597 if (stack_max < 0)
return COMPILE_NG;
2600 insn_num = code_index = 0;
2601 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2602 switch (list->type) {
2603 case ISEQ_ELEMENT_INSN:
2605 INSN *iobj = (INSN *)list;
2607 sp = calc_sp_depth(sp, iobj);
2609 events = iobj->insn_info.events |= events;
2610 if (ISEQ_COVERAGE(iseq)) {
2611 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2612 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2613 int line = iobj->insn_info.line_no - 1;
2614 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2618 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2619 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2625 code_index += insn_data_length(iobj);
2630 case ISEQ_ELEMENT_LABEL:
2632 LABEL *lobj = (LABEL *)list;
2633 lobj->position = code_index;
2634 if (lobj->sp != sp) {
2635 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2636 RSTRING_PTR(rb_iseq_path(iseq)),
2637 lobj->label_no, lobj->sp, sp);
2642 case ISEQ_ELEMENT_TRACE:
2644 TRACE *trace = (TRACE *)list;
2645 events |= trace->event;
2646 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2649 case ISEQ_ELEMENT_ADJUST:
2651 ADJUST *adjust = (ADJUST *)list;
2652 if (adjust->line_no != -1) {
2654 sp = adjust->label ? adjust->label->sp : 0;
2655 if (orig_sp - sp > 0) {
2656 if (orig_sp - sp > 1) code_index++;
2670 positions =
ALLOC_N(
unsigned int, insn_num);
2671 if (ISEQ_IS_SIZE(body)) {
2675 body->is_entries = NULL;
2678 if (body->ci_size) {
2682 body->call_data = NULL;
2684 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2691 iseq_bits_t * mark_offset_bits;
2692 int code_size = code_index;
2694 bool needs_bitmap =
false;
2696 if (ISEQ_MBITS_BUFLEN(code_index) == 1) {
2697 mark_offset_bits = &ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2698 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
true;
2701 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(code_index));
2702 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = mark_offset_bits;
2703 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
false;
2706 ISEQ_COMPILE_DATA(iseq)->iseq_encoded = (
void *)generated_iseq;
2707 ISEQ_COMPILE_DATA(iseq)->iseq_size = code_index;
2709 list = FIRST_ELEMENT(anchor);
2710 insns_info_index = code_index = sp = 0;
2713 switch (list->type) {
2714 case ISEQ_ELEMENT_INSN:
2719 INSN *iobj = (INSN *)list;
2722 sp = calc_sp_depth(sp, iobj);
2724 operands = iobj->operands;
2725 insn = iobj->insn_id;
2726 generated_iseq[code_index] = insn;
2727 types = insn_op_types(insn);
2728 len = insn_len(insn);
2730 for (j = 0; types[j]; j++) {
2731 char type = types[j];
2738 LABEL *lobj = (LABEL *)operands[j];
2739 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2744 VALUE map = operands[j];
2747 data.pos = code_index;
2751 rb_hash_rehash(map);
2752 freeze_hide_obj(map);
2754 generated_iseq[code_index + 1 + j] = map;
2755 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2757 needs_bitmap =
true;
2762 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2767 VALUE v = operands[j];
2768 generated_iseq[code_index + 1 + j] = v;
2772 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2773 needs_bitmap =
true;
2780 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2781 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2782 if (UNLIKELY(ic_index >= body->ic_size)) {
2783 BADINSN_DUMP(anchor, &iobj->link, 0);
2784 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2785 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2786 ic_index, ISEQ_IS_SIZE(body));
2789 ic->
segments = array_to_idlist(operands[j]);
2791 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2796 unsigned int ic_index =
FIX2UINT(operands[j]);
2798 IVC cache = ((IVC)&body->is_entries[ic_index]);
2800 if (insn == BIN(setinstancevariable)) {
2801 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2804 cache->iv_set_name = 0;
2807 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
2812 unsigned int ic_index =
FIX2UINT(operands[j]);
2813 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2814 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2815 BADINSN_DUMP(anchor, &iobj->link, 0);
2816 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2817 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2818 ic_index, ISEQ_IS_SIZE(body));
2820 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2827 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2828 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2830 cd->cc = vm_cc_empty();
2831 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2835 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2838 generated_iseq[code_index + 1 + j] = operands[j];
2841 generated_iseq[code_index + 1 + j] = operands[j];
2844 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2845 "unknown operand type: %c",
type);
2849 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2853 case ISEQ_ELEMENT_LABEL:
2855 LABEL *lobj = (LABEL *)list;
2856 if (lobj->sp != sp) {
2857 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2858 RSTRING_PTR(rb_iseq_path(iseq)),
2859 lobj->label_no, lobj->sp, sp);
2864 case ISEQ_ELEMENT_ADJUST:
2866 ADJUST *adjust = (ADJUST *)list;
2869 if (adjust->label) {
2870 sp = adjust->label->sp;
2876 if (adjust->line_no != -1) {
2877 const int diff = orig_sp - sp;
2879 if (insns_info_index == 0) {
2880 COMPILE_ERROR(iseq, adjust->line_no,
2881 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2883 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2886 generated_iseq[code_index++] = BIN(adjuststack);
2887 generated_iseq[code_index++] = orig_sp - sp;
2889 else if (diff == 1) {
2890 generated_iseq[code_index++] = BIN(pop);
2892 else if (diff < 0) {
2893 int label_no = adjust->label ? adjust->label->label_no : -1;
2894 xfree(generated_iseq);
2897 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
2898 xfree(mark_offset_bits);
2900 debug_list(anchor, list);
2901 COMPILE_ERROR(iseq, adjust->line_no,
2902 "iseq_set_sequence: adjust bug to %d %d < %d",
2903 label_no, orig_sp, sp);
2916 body->iseq_encoded = (
void *)generated_iseq;
2917 body->iseq_size = code_index;
2918 body->stack_max = stack_max;
2920 if (ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit) {
2921 body->mark_bits.single = ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2925 body->mark_bits.list = mark_offset_bits;
2928 body->mark_bits.list = NULL;
2929 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = NULL;
2930 ruby_xfree(mark_offset_bits);
2935 body->insns_info.body = insns_info;
2936 body->insns_info.positions = positions;
2939 body->insns_info.body = insns_info;
2940 REALLOC_N(positions,
unsigned int, insns_info_index);
2941 body->insns_info.positions = positions;
2942 body->insns_info.size = insns_info_index;
2948label_get_position(LABEL *lobj)
2950 return lobj->position;
2954label_get_sp(LABEL *lobj)
2960iseq_set_exception_table(rb_iseq_t *iseq)
2962 const VALUE *tptr, *ptr;
2963 unsigned int tlen, i;
2966 ISEQ_BODY(iseq)->catch_table = NULL;
2968 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
2969 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
2977 for (i = 0; i < table->size; i++) {
2980 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
2981 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
2982 pos = label_get_position((LABEL *)(ptr[1] & ~1));
2984 entry->start = (
unsigned int)pos;
2985 pos = label_get_position((LABEL *)(ptr[2] & ~1));
2987 entry->end = (
unsigned int)pos;
2988 entry->iseq = (rb_iseq_t *)ptr[3];
2993 LABEL *lobj = (LABEL *)(ptr[4] & ~1);
2994 entry->cont = label_get_position(lobj);
2995 entry->sp = label_get_sp(lobj);
2998 if (entry->type == CATCH_TYPE_RESCUE ||
2999 entry->type == CATCH_TYPE_BREAK ||
3000 entry->type == CATCH_TYPE_NEXT) {
3009 ISEQ_BODY(iseq)->catch_table = table;
3010 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
3028iseq_set_optargs_table(rb_iseq_t *iseq)
3031 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
3033 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
3034 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
3035 opt_table[i] = label_get_position((LABEL *)opt_table[i]);
3041static LINK_ELEMENT *
3042get_destination_insn(INSN *iobj)
3044 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, 0);
3048 list = lobj->link.next;
3050 switch (list->type) {
3051 case ISEQ_ELEMENT_INSN:
3052 case ISEQ_ELEMENT_ADJUST:
3054 case ISEQ_ELEMENT_LABEL:
3057 case ISEQ_ELEMENT_TRACE:
3059 TRACE *trace = (TRACE *)list;
3060 events |= trace->event;
3068 if (list && IS_INSN(list)) {
3069 INSN *iobj = (INSN *)list;
3070 iobj->insn_info.events |= events;
3075static LINK_ELEMENT *
3076get_next_insn(INSN *iobj)
3078 LINK_ELEMENT *list = iobj->link.next;
3081 if (IS_INSN(list) || IS_ADJUST(list)) {
3089static LINK_ELEMENT *
3090get_prev_insn(INSN *iobj)
3092 LINK_ELEMENT *list = iobj->link.prev;
3095 if (IS_INSN(list) || IS_ADJUST(list)) {
3104unref_destination(INSN *iobj,
int pos)
3106 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, pos);
3108 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3112replace_destination(INSN *dobj, INSN *nobj)
3114 VALUE n = OPERAND_AT(nobj, 0);
3115 LABEL *dl = (LABEL *)OPERAND_AT(dobj, 0);
3116 LABEL *nl = (LABEL *)n;
3117 if (dl == nl)
return false;
3120 OPERAND_AT(dobj, 0) = n;
3121 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3126find_destination(INSN *i)
3128 int pos,
len = insn_len(i->insn_id);
3129 for (pos = 0; pos <
len; ++pos) {
3130 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3131 return (LABEL *)OPERAND_AT(i, pos);
3138remove_unreachable_chunk(rb_iseq_t *iseq, LINK_ELEMENT *i)
3140 LINK_ELEMENT *first = i, *end;
3141 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
3144 unref_counts =
ALLOCA_N(
int, nlabels);
3145 MEMZERO(unref_counts,
int, nlabels);
3150 if (IS_INSN_ID(i, leave)) {
3154 else if ((lab = find_destination((INSN *)i)) != 0) {
3155 unref_counts[lab->label_no]++;
3158 else if (IS_LABEL(i)) {
3160 if (lab->unremovable)
return 0;
3161 if (lab->refcnt > unref_counts[lab->label_no]) {
3162 if (i == first)
return 0;
3167 else if (IS_TRACE(i)) {
3170 else if (IS_ADJUST(i)) {
3174 }
while ((i = i->next) != 0);
3179 VALUE insn = INSN_OF(i);
3180 int pos,
len = insn_len(insn);
3181 for (pos = 0; pos <
len; ++pos) {
3182 switch (insn_op_types(insn)[pos]) {
3184 unref_destination((INSN *)i, pos);
3193 }
while ((i != end) && (i = i->next) != 0);
3198iseq_pop_newarray(rb_iseq_t *iseq, INSN *iobj)
3200 switch (OPERAND_AT(iobj, 0)) {
3202 ELEM_REMOVE(&iobj->link);
3205 ELEM_REMOVE(&iobj->link);
3208 iobj->insn_id = BIN(adjuststack);
3214is_frozen_putstring(INSN *insn,
VALUE *op)
3216 if (IS_INSN_ID(insn, putstring) || IS_INSN_ID(insn, putchilledstring)) {
3217 *op = OPERAND_AT(insn, 0);
3220 else if (IS_INSN_ID(insn, putobject)) {
3221 *op = OPERAND_AT(insn, 0);
3228insn_has_label_before(LINK_ELEMENT *elem)
3230 LINK_ELEMENT *prev = elem->prev;
3232 if (prev->type == ISEQ_ELEMENT_LABEL) {
3233 LABEL *label = (LABEL *)prev;
3234 if (label->refcnt > 0) {
3238 else if (prev->type == ISEQ_ELEMENT_INSN) {
3247optimize_checktype(rb_iseq_t *iseq, INSN *iobj)
3271 INSN *niobj, *ciobj, *dup = 0;
3275 switch (INSN_OF(iobj)) {
3276 case BIN(putstring):
3277 case BIN(putchilledstring):
3283 case BIN(putobject):
3286 default:
return FALSE;
3289 ciobj = (INSN *)get_next_insn(iobj);
3290 if (IS_INSN_ID(ciobj, jump)) {
3291 ciobj = (INSN *)get_next_insn((INSN*)OPERAND_AT(ciobj, 0));
3293 if (IS_INSN_ID(ciobj, dup)) {
3294 ciobj = (INSN *)get_next_insn(dup = ciobj);
3296 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3297 niobj = (INSN *)get_next_insn(ciobj);
3302 switch (INSN_OF(niobj)) {
3304 if (OPERAND_AT(ciobj, 0) ==
type) {
3305 dest = (LABEL *)OPERAND_AT(niobj, 0);
3308 case BIN(branchunless):
3309 if (OPERAND_AT(ciobj, 0) !=
type) {
3310 dest = (LABEL *)OPERAND_AT(niobj, 0);
3316 line = ciobj->insn_info.line_no;
3317 node_id = ciobj->insn_info.node_id;
3319 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3320 dest = (LABEL *)niobj->link.next;
3323 dest = NEW_LABEL(line);
3324 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3327 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3329 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3334ci_flag_set(
const rb_iseq_t *iseq,
const struct rb_callinfo *ci,
unsigned int add)
3336 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3337 vm_ci_flag(ci) | add,
3345ci_argc_set(
const rb_iseq_t *iseq,
const struct rb_callinfo *ci,
int argc)
3347 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3355#define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3358iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list,
const int do_tailcallopt)
3360 INSN *
const iobj = (INSN *)list;
3363 optimize_checktype(iseq, iobj);
3365 if (IS_INSN_ID(iobj, jump)) {
3366 INSN *niobj, *diobj, *piobj;
3367 diobj = (INSN *)get_destination_insn(iobj);
3368 niobj = (INSN *)get_next_insn(iobj);
3370 if (diobj == niobj) {
3377 unref_destination(iobj, 0);
3378 ELEM_REMOVE(&iobj->link);
3381 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3382 IS_INSN_ID(diobj, jump) &&
3383 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3384 diobj->insn_info.events == 0) {
3395 if (replace_destination(iobj, diobj)) {
3396 remove_unreachable_chunk(iseq, iobj->link.next);
3400 else if (IS_INSN_ID(diobj, leave)) {
3413 unref_destination(iobj, 0);
3414 iobj->insn_id = BIN(leave);
3415 iobj->operand_size = 0;
3416 iobj->insn_info = diobj->insn_info;
3419 else if (IS_INSN(iobj->link.prev) &&
3420 (piobj = (INSN *)iobj->link.prev) &&
3421 (IS_INSN_ID(piobj, branchif) ||
3422 IS_INSN_ID(piobj, branchunless))) {
3423 INSN *pdiobj = (INSN *)get_destination_insn(piobj);
3424 if (niobj == pdiobj) {
3425 int refcnt = IS_LABEL(piobj->link.next) ?
3426 ((LABEL *)piobj->link.next)->refcnt : 0;
3441 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3442 ? BIN(branchunless) : BIN(branchif);
3443 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3444 ELEM_REMOVE(&iobj->link);
3451 else if (diobj == pdiobj) {
3465 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3466 ELEM_REPLACE(&piobj->link, &popiobj->link);
3469 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3483 if (IS_INSN_ID(iobj, newrange)) {
3484 INSN *
const range = iobj;
3486 VALUE str_beg, str_end;
3488 if ((end = (INSN *)get_prev_insn(range)) != 0 &&
3489 is_frozen_putstring(end, &str_end) &&
3490 (beg = (INSN *)get_prev_insn(end)) != 0 &&
3491 is_frozen_putstring(beg, &str_beg) &&
3492 !(insn_has_label_before(&beg->link) || insn_has_label_before(&end->link))) {
3493 int excl =
FIX2INT(OPERAND_AT(range, 0));
3496 ELEM_REMOVE(&beg->link);
3497 ELEM_REMOVE(&end->link);
3498 range->insn_id = BIN(putobject);
3499 OPERAND_AT(range, 0) = lit_range;
3504 if (IS_INSN_ID(iobj, leave)) {
3505 remove_unreachable_chunk(iseq, iobj->link.next);
3517 if (IS_INSN_ID(iobj, duparray)) {
3518 LINK_ELEMENT *next = iobj->link.next;
3519 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3520 iobj->insn_id = BIN(putobject);
3530 if (IS_INSN_ID(iobj, duparray)) {
3531 LINK_ELEMENT *next = iobj->link.next;
3532 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3534 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3536 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3537 VALUE ary = iobj->operands[0];
3540 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, ary, (
VALUE)ci);
3552 if (IS_INSN_ID(iobj, duphash)) {
3553 LINK_ELEMENT *next = iobj->link.next;
3554 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3556 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3558 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3559 VALUE hash = iobj->operands[0];
3561 RB_OBJ_SET_SHAREABLE(hash);
3563 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, hash, (
VALUE)ci);
3575 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3576 LINK_ELEMENT *next = iobj->link.next;
3577 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3579 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3581 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3582 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, rb_cArray_empty_frozen, (
VALUE)ci);
3594 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3595 LINK_ELEMENT *next = iobj->link.next;
3596 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3598 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3600 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3601 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, rb_cHash_empty_frozen, (
VALUE)ci);
3607 if (IS_INSN_ID(iobj, branchif) ||
3608 IS_INSN_ID(iobj, branchnil) ||
3609 IS_INSN_ID(iobj, branchunless)) {
3618 INSN *nobj = (INSN *)get_destination_insn(iobj);
3640 int stop_optimization =
3641 ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq) &&
3642 nobj->link.type == ISEQ_ELEMENT_INSN &&
3643 nobj->insn_info.events;
3644 if (!stop_optimization) {
3645 INSN *pobj = (INSN *)iobj->link.prev;
3648 if (!IS_INSN(&pobj->link))
3650 else if (IS_INSN_ID(pobj, dup))
3655 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3656 if (!replace_destination(iobj, nobj))
break;
3658 else if (prev_dup && IS_INSN_ID(nobj, dup) &&
3659 !!(nobj = (INSN *)nobj->link.next) &&
3661 nobj->insn_id == iobj->insn_id) {
3677 if (!replace_destination(iobj, nobj))
break;
3705 if (prev_dup && IS_INSN(pobj->link.prev)) {
3706 pobj = (INSN *)pobj->link.prev;
3708 if (IS_INSN_ID(pobj, putobject)) {
3709 cond = (IS_INSN_ID(iobj, branchif) ?
3710 OPERAND_AT(pobj, 0) !=
Qfalse :
3711 IS_INSN_ID(iobj, branchunless) ?
3712 OPERAND_AT(pobj, 0) ==
Qfalse :
3715 else if (IS_INSN_ID(pobj, putstring) ||
3716 IS_INSN_ID(pobj, duparray) ||
3717 IS_INSN_ID(pobj, newarray)) {
3718 cond = IS_INSN_ID(iobj, branchif);
3720 else if (IS_INSN_ID(pobj, putnil)) {
3721 cond = !IS_INSN_ID(iobj, branchif);
3724 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3725 ELEM_REMOVE(iobj->link.prev);
3727 else if (!iseq_pop_newarray(iseq, pobj)) {
3728 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3729 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3733 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3734 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3736 iobj->insn_id = BIN(jump);
3740 unref_destination(iobj, 0);
3741 ELEM_REMOVE(&iobj->link);
3746 nobj = (INSN *)get_destination_insn(nobj);
3751 if (IS_INSN_ID(iobj, pop)) {
3758 LINK_ELEMENT *prev = iobj->link.prev;
3759 if (IS_INSN(prev)) {
3760 enum ruby_vminsn_type previ = ((INSN *)prev)->insn_id;
3761 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3762 previ == BIN(putself) || previ == BIN(putstring) ||
3763 previ == BIN(putchilledstring) ||
3764 previ == BIN(dup) ||
3765 previ == BIN(getlocal) ||
3766 previ == BIN(getblockparam) ||
3767 previ == BIN(getblockparamproxy) ||
3768 previ == BIN(getinstancevariable) ||
3769 previ == BIN(duparray)) {
3773 ELEM_REMOVE(&iobj->link);
3775 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (INSN*)prev)) {
3776 ELEM_REMOVE(&iobj->link);
3778 else if (previ == BIN(concatarray)) {
3779 INSN *piobj = (INSN *)prev;
3780 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3781 INSN_OF(piobj) = BIN(pop);
3783 else if (previ == BIN(concatstrings)) {
3784 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3788 ELEM_REMOVE(&iobj->link);
3789 INSN_OF(prev) = BIN(adjuststack);
3795 if (IS_INSN_ID(iobj, newarray) ||
3796 IS_INSN_ID(iobj, duparray) ||
3797 IS_INSN_ID(iobj, concatarray) ||
3798 IS_INSN_ID(iobj, splatarray) ||
3807 LINK_ELEMENT *next = iobj->link.next;
3808 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3814 if (IS_INSN_ID(iobj, newarray)) {
3815 LINK_ELEMENT *next = iobj->link.next;
3816 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3817 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3819 op1 = OPERAND_AT(iobj, 0);
3820 op2 = OPERAND_AT(next, 0);
3831 INSN_OF(iobj) = BIN(swap);
3832 iobj->operand_size = 0;
3841 INSN_OF(iobj) = BIN(opt_reverse);
3846 INSN_OF(iobj) = BIN(opt_reverse);
3847 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3857 for (; diff > 0; diff--) {
3858 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
3869 for (; diff < 0; diff++) {
3870 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
3877 if (IS_INSN_ID(iobj, duparray)) {
3878 LINK_ELEMENT *next = iobj->link.next;
3886 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
3887 INSN_OF(iobj) = BIN(putobject);
3891 if (IS_INSN_ID(iobj, anytostring)) {
3892 LINK_ELEMENT *next = iobj->link.next;
3899 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
3900 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
3905 if (IS_INSN_ID(iobj, putstring) || IS_INSN_ID(iobj, putchilledstring) ||
3913 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
3914 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
3915 INSN *next = (INSN *)iobj->link.next;
3916 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
3917 ELEM_REMOVE(&next->link);
3919 ELEM_REMOVE(&iobj->link);
3921 if (IS_NEXT_INSN_ID(&iobj->link, toregexp)) {
3922 INSN *next = (INSN *)iobj->link.next;
3923 if (OPERAND_AT(next, 1) ==
INT2FIX(1)) {
3924 VALUE src = OPERAND_AT(iobj, 0);
3925 int opt = (int)
FIX2LONG(OPERAND_AT(next, 0));
3926 VALUE path = rb_iseq_path(iseq);
3927 int line = iobj->insn_info.line_no;
3929 VALUE re = rb_reg_compile(src, opt, RSTRING_PTR(path), line);
3932 rb_set_errinfo(errinfo);
3933 COMPILE_ERROR(iseq, line,
"%" PRIsVALUE, message);
3936 RB_OBJ_SET_SHAREABLE(re);
3939 ELEM_REMOVE(iobj->link.next);
3944 if (IS_INSN_ID(iobj, concatstrings)) {
3951 LINK_ELEMENT *next = iobj->link.next;
3953 if (IS_INSN(next) && IS_INSN_ID(next, jump))
3954 next = get_destination_insn(jump = (INSN *)next);
3955 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
3956 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
3957 OPERAND_AT(iobj, 0) =
INT2FIX(n);
3959 LABEL *label = ((LABEL *)OPERAND_AT(jump, 0));
3960 if (!--label->refcnt) {
3961 ELEM_REMOVE(&label->link);
3964 label = NEW_LABEL(0);
3965 OPERAND_AT(jump, 0) = (
VALUE)label;
3968 ELEM_INSERT_NEXT(next, &label->link);
3969 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
3977 if (do_tailcallopt &&
3978 (IS_INSN_ID(iobj, send) ||
3979 IS_INSN_ID(iobj, invokesuper))) {
3988 if (iobj->link.next) {
3989 LINK_ELEMENT *next = iobj->link.next;
3991 if (!IS_INSN(next)) {
3995 switch (INSN_OF(next)) {
4004 next = get_destination_insn((INSN *)next);
4018 if (IS_INSN_ID(piobj, send) ||
4019 IS_INSN_ID(piobj, invokesuper)) {
4020 if (OPERAND_AT(piobj, 1) == 0) {
4021 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4022 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4027 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4028 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4034 if (IS_INSN_ID(iobj, dup)) {
4035 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
4036 LINK_ELEMENT *set1 = iobj->link.next, *set2 = NULL;
4046 if (IS_NEXT_INSN_ID(set1, setlocal)) {
4048 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4049 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4051 ELEM_REMOVE(&iobj->link);
4064 else if (IS_NEXT_INSN_ID(set1, dup) &&
4065 IS_NEXT_INSN_ID(set1->next, setlocal)) {
4066 set2 = set1->next->next;
4067 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4068 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4069 ELEM_REMOVE(set1->next);
4083 if (IS_INSN_ID(iobj, getlocal)) {
4084 LINK_ELEMENT *niobj = &iobj->link;
4085 if (IS_NEXT_INSN_ID(niobj, dup)) {
4086 niobj = niobj->next;
4088 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
4089 LINK_ELEMENT *set1 = niobj->next;
4090 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
4091 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
4107 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
4108 if (IS_TRACE(iobj->link.next)) {
4109 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
4110 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
4112 if (iobj == (INSN *)list && bf->argc == 0 && (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF)) {
4113 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4126 if (IS_INSN_ID(iobj, getblockparam)) {
4127 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4128 iobj->insn_id = BIN(getblockparamproxy);
4132 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4133 LINK_ELEMENT *niobj = &iobj->link;
4134 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4135 niobj = niobj->next;
4136 LINK_ELEMENT *siobj;
4137 unsigned int set_flags = 0, unset_flags = 0;
4150 if (IS_NEXT_INSN_ID(niobj, send)) {
4151 siobj = niobj->next;
4152 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4153 unset_flags = VM_CALL_ARGS_BLOCKARG;
4168 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4169 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4170 siobj = niobj->next->next;
4171 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4176 unsigned int flags = vm_ci_flag(ci);
4177 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4178 ((INSN*)niobj)->insn_id = BIN(putobject);
4179 RB_OBJ_WRITE(iseq, &OPERAND_AT(niobj, 0), RB_OBJ_SET_SHAREABLE(rb_hash_freeze(rb_hash_resurrect(OPERAND_AT(niobj, 0)))));
4181 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4182 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4184 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4194insn_set_specialized_instruction(rb_iseq_t *iseq, INSN *iobj,
int insn_id)
4196 if (insn_id == BIN(opt_neq)) {
4197 VALUE original_ci = iobj->operands[0];
4198 VALUE new_ci = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4199 insn_replace_with_operands(iseq, iobj, insn_id, 2, new_ci, original_ci);
4202 iobj->insn_id = insn_id;
4203 iobj->operand_size = insn_len(insn_id) - 1;
4211iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
4213 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4214 IS_INSN(iobj->link.next)) {
4218 INSN *niobj = (INSN *)iobj->link.next;
4219 if (IS_INSN_ID(niobj, send)) {
4221 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4223 switch (vm_ci_mid(ci)) {
4225 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4228 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4231 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4236 VALUE num = iobj->operands[0];
4237 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, num, method);
4238 ELEM_REMOVE(&niobj->link);
4243 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4245 IS_NEXT_INSN_ID(&niobj->link, send)) {
4247 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4248 VALUE num = iobj->operands[0];
4249 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK));
4250 ELEM_REMOVE(&iobj->link);
4251 ELEM_REMOVE(niobj->link.next);
4252 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4258 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4260 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4261 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4264 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4265 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] ==
rb_id2sym(idBuffer))) {
4266 VALUE num = iobj->operands[0];
4267 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 2),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER));
4269 ELEM_REMOVE((niobj->link.next)->next);
4271 ELEM_REMOVE(&iobj->link);
4273 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4281 if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4282 IS_INSN_ID(niobj, putobject) ||
4283 IS_INSN_ID(niobj, putself) ||
4284 IS_INSN_ID(niobj, getlocal) ||
4285 IS_INSN_ID(niobj, getinstancevariable)) &&
4286 IS_NEXT_INSN_ID(&niobj->link, send)) {
4288 LINK_ELEMENT *sendobj = &(niobj->link);
4293 sendobj = sendobj->next;
4294 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4295 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4298 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4299 VALUE num = iobj->operands[0];
4300 INSN *sendins = (INSN *)sendobj;
4301 insn_replace_with_operands(iseq, sendins, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P));
4303 ELEM_REMOVE(&iobj->link);
4317 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4318 INSN *niobj = (INSN *)iobj->link.next;
4319 if ((IS_INSN_ID(niobj, getlocal) ||
4320 IS_INSN_ID(niobj, getinstancevariable) ||
4321 IS_INSN_ID(niobj, putself)) &&
4322 IS_NEXT_INSN_ID(&niobj->link, send)) {
4324 LINK_ELEMENT *sendobj = &(niobj->link);
4329 sendobj = sendobj->next;
4330 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4331 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4333 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4335 VALUE ary = iobj->operands[0];
4338 INSN *sendins = (INSN *)sendobj;
4339 insn_replace_with_operands(iseq, sendins, BIN(opt_duparray_send), 3, ary,
rb_id2sym(idIncludeP),
INT2FIX(1));
4342 ELEM_REMOVE(&iobj->link);
4349 if (IS_INSN_ID(iobj, send)) {
4351 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(iobj, 1);
4353#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4354 if (vm_ci_simple(ci)) {
4355 switch (vm_ci_argc(ci)) {
4357 switch (vm_ci_mid(ci)) {
4358 case idLength: SP_INSN(length);
return COMPILE_OK;
4359 case idSize: SP_INSN(size);
return COMPILE_OK;
4360 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4361 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4362 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4363 case idNot: SP_INSN(not);
return COMPILE_OK;
4367 switch (vm_ci_mid(ci)) {
4368 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4369 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4370 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4371 case idDIV: SP_INSN(div);
return COMPILE_OK;
4372 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4373 case idEq: SP_INSN(eq);
return COMPILE_OK;
4374 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4375 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4376 case idLT: SP_INSN(lt);
return COMPILE_OK;
4377 case idLE: SP_INSN(le);
return COMPILE_OK;
4378 case idGT: SP_INSN(gt);
return COMPILE_OK;
4379 case idGE: SP_INSN(ge);
return COMPILE_OK;
4380 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4381 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4382 case idAnd: SP_INSN(and);
return COMPILE_OK;
4383 case idOr: SP_INSN(or);
return COMPILE_OK;
4387 switch (vm_ci_mid(ci)) {
4388 case idASET: SP_INSN(aset);
return COMPILE_OK;
4394 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4395 iobj->insn_id = BIN(opt_send_without_block);
4396 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4405tailcallable_p(rb_iseq_t *iseq)
4407 switch (ISEQ_BODY(iseq)->
type) {
4409 case ISEQ_TYPE_EVAL:
4410 case ISEQ_TYPE_MAIN:
4412 case ISEQ_TYPE_RESCUE:
4413 case ISEQ_TYPE_ENSURE:
4422iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
4425 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4426 const int do_tailcallopt = tailcallable_p(iseq) &&
4427 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4428 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4429 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4430 int rescue_level = 0;
4431 int tailcallopt = do_tailcallopt;
4433 list = FIRST_ELEMENT(anchor);
4435 int do_block_optimization = 0;
4436 LABEL * block_loop_label = NULL;
4439 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
4440 do_block_optimization = 1;
4444 LINK_ELEMENT * le = FIRST_ELEMENT(anchor)->next;
4445 if (IS_INSN(le) && IS_INSN_ID((INSN *)le, nop) && IS_LABEL(le->next)) {
4446 block_loop_label = (LABEL *)le->next;
4451 if (IS_INSN(list)) {
4452 if (do_peepholeopt) {
4453 iseq_peephole_optimize(iseq, list, tailcallopt);
4456 iseq_specialized_instruction(iseq, (INSN *)list);
4459 insn_operands_unification((INSN *)list);
4462 if (do_block_optimization) {
4463 INSN * item = (INSN *)list;
4465 if (IS_INSN_ID(item,
throw)) {
4466 do_block_optimization = 0;
4471 const char *types = insn_op_types(item->insn_id);
4472 for (
int j = 0; types[j]; j++) {
4473 if (types[j] == TS_OFFSET) {
4478 LABEL * target = (LABEL *)OPERAND_AT(item, j);
4479 if (target == block_loop_label) {
4480 do_block_optimization = 0;
4487 if (IS_LABEL(list)) {
4488 switch (((LABEL *)list)->rescued) {
4489 case LABEL_RESCUE_BEG:
4491 tailcallopt = FALSE;
4493 case LABEL_RESCUE_END:
4494 if (!--rescue_level) tailcallopt = do_tailcallopt;
4501 if (do_block_optimization) {
4502 LINK_ELEMENT * le = FIRST_ELEMENT(anchor)->next;
4503 if (IS_INSN(le) && IS_INSN_ID((INSN *)le, nop)) {
4510#if OPT_INSTRUCTIONS_UNIFICATION
4512new_unified_insn(rb_iseq_t *iseq,
4513 int insn_id,
int size, LINK_ELEMENT *seq_list)
4516 LINK_ELEMENT *list = seq_list;
4518 VALUE *operands = 0, *ptr = 0;
4522 for (i = 0; i < size; i++) {
4523 iobj = (INSN *)list;
4524 argc += iobj->operand_size;
4529 ptr = operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
4534 for (i = 0; i < size; i++) {
4535 iobj = (INSN *)list;
4536 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4537 ptr += iobj->operand_size;
4541 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4551iseq_insns_unification(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
4553#if OPT_INSTRUCTIONS_UNIFICATION
4559 list = FIRST_ELEMENT(anchor);
4561 if (IS_INSN(list)) {
4562 iobj = (INSN *)list;
4564 if (unified_insns_data[
id] != 0) {
4565 const int *
const *entry = unified_insns_data[id];
4566 for (j = 1; j < (intptr_t)entry[0]; j++) {
4567 const int *unified = entry[j];
4568 LINK_ELEMENT *li = list->next;
4569 for (k = 2; k < unified[1]; k++) {
4571 ((INSN *)li)->insn_id != unified[k]) {
4578 new_unified_insn(iseq, unified[0], unified[1] - 1,
4582 niobj->link.prev = (LINK_ELEMENT *)iobj->link.prev;
4583 niobj->link.next = li;
4585 li->prev = (LINK_ELEMENT *)niobj;
4588 list->prev->next = (LINK_ELEMENT *)niobj;
4589 list = (LINK_ELEMENT *)niobj;
4602all_string_result_p(
const NODE *node)
4604 if (!node)
return FALSE;
4605 switch (nd_type(node)) {
4606 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4608 case NODE_IF:
case NODE_UNLESS:
4609 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4610 if (all_string_result_p(RNODE_IF(node)->nd_body))
4611 return all_string_result_p(RNODE_IF(node)->nd_else);
4613 case NODE_AND:
case NODE_OR:
4614 if (!RNODE_AND(node)->nd_2nd)
4615 return all_string_result_p(RNODE_AND(node)->nd_1st);
4616 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4618 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4625 rb_iseq_t *
const iseq;
4626 LINK_ANCHOR *
const ret;
4628 const NODE *lit_node;
4634append_dstr_fragment(
struct dstr_ctxt *args,
const NODE *
const node, rb_parser_string_t *str)
4636 VALUE s = rb_str_new_mutable_parser_string(str);
4638 VALUE error = rb_reg_check_preprocess(s);
4639 if (!
NIL_P(error)) {
4640 COMPILE_ERROR(args->iseq, nd_line(node),
"%" PRIsVALUE, error);
4644 if (
NIL_P(args->lit)) {
4646 args->lit_node = node;
4655flush_dstr_fragment(
struct dstr_ctxt *args)
4657 if (!
NIL_P(args->lit)) {
4658 rb_iseq_t *iseq = args->iseq;
4659 VALUE lit = args->lit;
4661 lit = rb_fstring(lit);
4662 ADD_INSN1(args->ret, args->lit_node, putobject, lit);
4669compile_dstr_fragments_0(
struct dstr_ctxt *args,
const NODE *
const node)
4671 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4672 rb_parser_string_t *str = RNODE_DSTR(node)->string;
4675 CHECK(append_dstr_fragment(args, node, str));
4679 const NODE *
const head = list->nd_head;
4680 if (nd_type_p(head, NODE_STR)) {
4681 CHECK(append_dstr_fragment(args, node, RNODE_STR(head)->
string));
4683 else if (nd_type_p(head, NODE_DSTR)) {
4684 CHECK(compile_dstr_fragments_0(args, head));
4687 flush_dstr_fragment(args);
4688 rb_iseq_t *iseq = args->iseq;
4689 CHECK(COMPILE(args->ret,
"each string", head));
4698compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int *cntp,
int dregx)
4701 .iseq = iseq, .ret = ret,
4702 .lit =
Qnil, .lit_node = NULL,
4703 .cnt = 0, .dregx = dregx,
4705 CHECK(compile_dstr_fragments_0(&args, node));
4706 flush_dstr_fragment(&args);
4714compile_block(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int popped)
4716 while (node && nd_type_p(node, NODE_BLOCK)) {
4717 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4718 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4719 node = RNODE_BLOCK(node)->nd_next;
4722 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4728compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node)
4731 if (!RNODE_DSTR(node)->nd_next) {
4732 VALUE lit = rb_node_dstr_string_val(node);
4733 ADD_INSN1(ret, node, putstring, lit);
4734 RB_OBJ_SET_SHAREABLE(lit);
4738 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, FALSE));
4739 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4745compile_dregx(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
4748 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
4750 if (!RNODE_DREGX(node)->nd_next) {
4752 VALUE src = rb_node_dregx_string_val(node);
4753 VALUE match = rb_reg_compile(src, cflag, NULL, 0);
4754 RB_OBJ_SET_SHAREABLE(match);
4755 ADD_INSN1(ret, node, putobject, match);
4761 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, TRUE));
4765 ADD_INSN(ret, node, pop);
4772compile_flip_flop(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int again,
4773 LABEL *then_label, LABEL *else_label)
4775 const int line = nd_line(node);
4776 LABEL *lend = NEW_LABEL(line);
4777 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4778 + VM_SVAR_FLIPFLOP_START;
4781 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4782 ADD_INSNL(ret, node, branchif, lend);
4785 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
4786 ADD_INSNL(ret, node, branchunless, else_label);
4787 ADD_INSN1(ret, node, putobject,
Qtrue);
4788 ADD_INSN1(ret, node, setspecial, key);
4790 ADD_INSNL(ret, node, jump, then_label);
4794 ADD_LABEL(ret, lend);
4795 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
4796 ADD_INSNL(ret, node, branchunless, then_label);
4797 ADD_INSN1(ret, node, putobject,
Qfalse);
4798 ADD_INSN1(ret, node, setspecial, key);
4799 ADD_INSNL(ret, node, jump, then_label);
4805compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *cond,
4806 LABEL *then_label, LABEL *else_label);
4808#define COMPILE_SINGLE 2
4810compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *cond,
4811 LABEL *then_label, LABEL *else_label)
4815 LABEL *label = NEW_LABEL(nd_line(cond));
4816 if (!then_label) then_label = label;
4817 else if (!else_label) else_label = label;
4819 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
4821 if (LIST_INSN_SIZE_ONE(seq)) {
4822 INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
4823 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
4826 if (!label->refcnt) {
4827 return COMPILE_SINGLE;
4829 ADD_LABEL(seq, label);
4835compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *cond,
4836 LABEL *then_label, LABEL *else_label)
4839 DECL_ANCHOR(ignore);
4842 switch (nd_type(cond)) {
4844 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
4845 cond = RNODE_AND(cond)->nd_2nd;
4846 if (ok == COMPILE_SINGLE) {
4847 ADD_INSNL(ret, cond, jump, else_label);
4848 INIT_ANCHOR(ignore);
4850 then_label = NEW_LABEL(nd_line(cond));
4854 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
4855 cond = RNODE_OR(cond)->nd_2nd;
4856 if (ok == COMPILE_SINGLE) {
4857 ADD_INSNL(ret, cond, jump, then_label);
4858 INIT_ANCHOR(ignore);
4860 else_label = NEW_LABEL(nd_line(cond));
4870 case NODE_IMAGINARY:
4877 ADD_INSNL(ret, cond, jump, then_label);
4882 ADD_INSNL(ret, cond, jump, else_label);
4888 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
4889 ADD_INSNL(ret, cond, jump, then_label);
4892 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
4895 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
4898 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
4902 DECL_ANCHOR(cond_seq);
4903 INIT_ANCHOR(cond_seq);
4905 CHECK(COMPILE(cond_seq,
"branch condition", cond));
4907 if (LIST_INSN_SIZE_ONE(cond_seq)) {
4908 INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
4909 if (insn->insn_id == BIN(putobject)) {
4910 if (
RTEST(insn->operands[0])) {
4911 ADD_INSNL(ret, cond, jump, then_label);
4916 ADD_INSNL(ret, cond, jump, else_label);
4921 ADD_SEQ(ret, cond_seq);
4926 ADD_INSNL(ret, cond, branchunless, else_label);
4927 ADD_INSNL(ret, cond, jump, then_label);
4934keyword_node_p(
const NODE *
const node)
4936 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
4940get_symbol_value(rb_iseq_t *iseq,
const NODE *node)
4942 switch (nd_type(node)) {
4944 return rb_node_sym_string_val(node);
4946 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
4951node_hash_unique_key_index(rb_iseq_t *iseq, rb_node_hash_t *node_hash,
int *count_ptr)
4953 NODE *node = node_hash->nd_head;
4957 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4958 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
4959 VALUE idx = rb_hash_aref(hash, key);
4964 rb_hash_aset(hash, key,
INT2FIX(i));
4973compile_keyword_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
4974 const NODE *
const root_node,
4982 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
4983 const NODE *node = RNODE_HASH(root_node)->nd_head;
4987 const NODE *key_node = RNODE_LIST(node)->nd_head;
4991 if (key_node && nd_type_p(key_node, NODE_SYM)) {
4996 *flag |= VM_CALL_KW_SPLAT;
4997 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5002 *flag |= VM_CALL_KW_SPLAT_MUT;
5007 node = RNODE_LIST(node)->nd_next;
5008 node = RNODE_LIST(node)->nd_next;
5012 node = RNODE_HASH(root_node)->nd_head;
5015 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
5018 VALUE *keywords = kw_arg->keywords;
5021 kw_arg->references = 0;
5022 kw_arg->keyword_len =
len;
5024 *kw_arg_ptr = kw_arg;
5026 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5027 const NODE *key_node = RNODE_LIST(node)->nd_head;
5028 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5031 keywords[j] = get_symbol_value(iseq, key_node);
5035 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
5045compile_args(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node, NODE **kwnode_ptr)
5049 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
5051 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
5054 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5055 *kwnode_ptr = RNODE_LIST(node)->nd_head;
5058 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
5059 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
5067frozen_string_literal_p(
const rb_iseq_t *iseq)
5069 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
5073static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
5075 switch (nd_type(node)) {
5083 case NODE_IMAGINARY:
5090 return hash_key || frozen_string_literal_p(iseq);
5097static_literal_value(
const NODE *node, rb_iseq_t *iseq)
5099 switch (nd_type(node)) {
5102 VALUE lit = rb_node_integer_literal_val(node);
5108 VALUE lit = rb_node_float_literal_val(node);
5114 case NODE_IMAGINARY:
5123 return rb_node_sym_string_val(node);
5125 return RB_OBJ_SET_SHAREABLE(rb_node_regx_string_val(node));
5127 return rb_node_line_lineno_val(node);
5129 return rb_node_encoding_val(node);
5132 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
5133 VALUE lit = get_string_value(node);
5134 VALUE str = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
5135 RB_OBJ_SET_SHAREABLE(str);
5139 return get_string_value(node);
5142 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
5147compile_array(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int popped,
bool first_chunk)
5149 const NODE *line_node = node;
5151 if (nd_type_p(node, NODE_ZLIST)) {
5153 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5158 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5161 for (; node; node = RNODE_LIST(node)->nd_next) {
5162 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5204 const int max_stack_len = 0x100;
5205 const int min_tmp_ary_len = 0x40;
5209#define FLUSH_CHUNK \
5211 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5212 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5213 first_chunk = FALSE; \
5221 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5223 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5224 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5227 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5232 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5233 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5234 RB_OBJ_SET_FROZEN_SHAREABLE(ary);
5239 ADD_INSN1(ret, line_node, duparray, ary);
5240 first_chunk = FALSE;
5243 ADD_INSN1(ret, line_node, putobject, ary);
5244 ADD_INSN(ret, line_node, concattoarray);
5246 RB_OBJ_SET_SHAREABLE(ary);
5252 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5254 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5257 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5259 if (stack_len == 0 && first_chunk) {
5260 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5265 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5266 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5270 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5275 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5285static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5287 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);
5291compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped)
5293 const NODE *line_node = node;
5295 node = RNODE_HASH(node)->nd_head;
5297 if (!node || nd_type_p(node, NODE_ZLIST)) {
5299 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5304 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5307 for (; node; node = RNODE_LIST(node)->nd_next) {
5308 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5331 const int max_stack_len = 0x100;
5332 const int min_tmp_hash_len = 0x800;
5334 int first_chunk = 1;
5335 DECL_ANCHOR(anchor);
5336 INIT_ANCHOR(anchor);
5339#define FLUSH_CHUNK() \
5341 if (first_chunk) { \
5342 APPEND_LIST(ret, anchor); \
5343 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5346 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5347 ADD_INSN(ret, line_node, swap); \
5348 APPEND_LIST(ret, anchor); \
5349 ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
5351 INIT_ANCHOR(anchor); \
5352 first_chunk = stack_len = 0; \
5359 if (static_literal_node_pair_p(node, iseq)) {
5361 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5362 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5365 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5370 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5372 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5374 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5380 hash = RB_OBJ_SET_FROZEN_SHAREABLE(
rb_obj_hide(hash));
5385 ADD_INSN1(ret, line_node, duphash, hash);
5389 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5390 ADD_INSN(ret, line_node, swap);
5392 ADD_INSN1(ret, line_node, putobject, hash);
5394 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5401 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5404 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5407 if (RNODE_LIST(node)->nd_head) {
5409 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5410 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5414 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5420 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5421 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5422 int first_kw = first_chunk && stack_len == 0;
5423 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5424 int only_kw = last_kw && first_kw;
5426 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5428 if (only_kw && method_call_keywords) {
5436 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5438 else if (first_kw) {
5442 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5449 if (only_kw && method_call_keywords) {
5455 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5462 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5463 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5464 else ADD_INSN(ret, line_node, swap);
5466 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5468 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5483rb_node_case_when_optimizable_literal(
const NODE *
const node)
5485 switch (nd_type(node)) {
5487 return rb_node_integer_literal_val(node);
5489 VALUE v = rb_node_float_literal_val(node);
5498 case NODE_IMAGINARY:
5507 return rb_node_sym_string_val(node);
5509 return rb_node_line_lineno_val(node);
5511 return rb_node_str_string_val(node);
5513 return rb_node_file_path_val(node);
5519when_vals(rb_iseq_t *iseq, LINK_ANCHOR *
const cond_seq,
const NODE *vals,
5520 LABEL *l1,
int only_special_literals,
VALUE literals)
5523 const NODE *val = RNODE_LIST(vals)->nd_head;
5524 VALUE lit = rb_node_case_when_optimizable_literal(val);
5527 only_special_literals = 0;
5529 else if (
NIL_P(rb_hash_lookup(literals, lit))) {
5530 rb_hash_aset(literals, lit, (
VALUE)(l1) | 1);
5533 if (nd_type_p(val, NODE_STR) || nd_type_p(val, NODE_FILE)) {
5534 debugp_param(
"nd_lit", get_string_value(val));
5535 lit = get_string_value(val);
5536 ADD_INSN1(cond_seq, val, putobject, lit);
5540 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5544 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5545 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5546 ADD_INSNL(cond_seq, val, branchif, l1);
5547 vals = RNODE_LIST(vals)->nd_next;
5549 return only_special_literals;
5553when_splat_vals(rb_iseq_t *iseq, LINK_ANCHOR *
const cond_seq,
const NODE *vals,
5554 LABEL *l1,
int only_special_literals,
VALUE literals)
5556 const NODE *line_node = vals;
5558 switch (nd_type(vals)) {
5560 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5564 ADD_INSN (cond_seq, line_node, dup);
5565 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5566 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5567 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5568 ADD_INSNL(cond_seq, line_node, branchif, l1);
5571 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5572 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5575 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5576 ADD_INSN (cond_seq, line_node, dup);
5577 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5578 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5579 ADD_INSNL(cond_seq, line_node, branchif, l1);
5582 ADD_INSN (cond_seq, line_node, dup);
5583 CHECK(COMPILE(cond_seq,
"when val", vals));
5584 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5585 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5586 ADD_INSNL(cond_seq, line_node, branchif, l1);
5679 const NODE *line_node;
5694add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc, INSN *before_insn)
5697 rb_bug(
"no masgn_state");
5706 memo->before_insn = before_insn;
5707 memo->line_node = line_node;
5708 memo->argn = state->num_args + 1;
5709 memo->num_args = argc;
5710 state->num_args += argc;
5711 memo->lhs_pos = lhs_pos;
5713 if (!state->first_memo) {
5714 state->first_memo = memo;
5717 state->last_memo->next = memo;
5719 state->last_memo = memo;
5724static int compile_massign0(rb_iseq_t *iseq, LINK_ANCHOR *
const pre, LINK_ANCHOR *
const rhs, LINK_ANCHOR *
const lhs, LINK_ANCHOR *
const post,
const NODE *
const node,
struct masgn_state *state,
int popped);
5727compile_massign_lhs(rb_iseq_t *iseq, LINK_ANCHOR *
const pre, LINK_ANCHOR *
const rhs, LINK_ANCHOR *
const lhs, LINK_ANCHOR *
const post,
const NODE *
const node,
struct masgn_state *state,
int lhs_pos)
5729 switch (nd_type(node)) {
5730 case NODE_ATTRASGN: {
5732 const NODE *line_node = node;
5734 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5736 bool safenav_call =
false;
5737 LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
5738 iobj = (INSN *)get_prev_insn((INSN *)insn_element);
5740 ELEM_REMOVE(insn_element);
5741 if (!IS_INSN_ID(iobj, send)) {
5742 safenav_call =
true;
5743 iobj = (INSN *)get_prev_insn(iobj);
5744 ELEM_INSERT_NEXT(&iobj->link, insn_element);
5746 (pre->last = iobj->link.prev)->next = 0;
5749 int argc = vm_ci_argc(ci) + 1;
5750 ci = ci_argc_set(iseq, ci, argc);
5751 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5755 ADD_INSN(lhs, line_node, swap);
5758 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5761 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (INSN *)LAST_ELEMENT(lhs))) {
5765 iobj->link.prev = lhs->last;
5766 lhs->last->next = &iobj->link;
5767 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
5768 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5769 int argc = vm_ci_argc(ci);
5770 bool dupsplat =
false;
5771 ci = ci_argc_set(iseq, ci, argc - 1);
5772 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
5779 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
5781 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5790 int line_no = nd_line(line_node);
5791 int node_id = nd_node_id(line_node);
5794 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5795 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
5796 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5798 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
5800 if (!safenav_call) {
5801 ADD_INSN(lhs, line_node, pop);
5803 ADD_INSN(lhs, line_node, pop);
5806 for (
int i=0; i < argc; i++) {
5807 ADD_INSN(post, line_node, pop);
5812 DECL_ANCHOR(nest_rhs);
5813 INIT_ANCHOR(nest_rhs);
5814 DECL_ANCHOR(nest_lhs);
5815 INIT_ANCHOR(nest_lhs);
5817 int prev_level = state->lhs_level;
5818 bool prev_nested = state->nested;
5820 state->lhs_level = lhs_pos - 1;
5821 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5822 state->lhs_level = prev_level;
5823 state->nested = prev_nested;
5825 ADD_SEQ(lhs, nest_rhs);
5826 ADD_SEQ(lhs, nest_lhs);
5830 if (!RNODE_CDECL(node)->nd_vid) {
5834 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5836 LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
5837 iobj = (INSN *)insn_element;
5838 ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn((INSN *)get_prev_insn(iobj)));
5839 ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn(iobj));
5840 ELEM_REMOVE(insn_element);
5841 pre->last = iobj->link.prev;
5842 ADD_ELEM(lhs, (LINK_ELEMENT *)iobj);
5844 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (INSN *)LAST_ELEMENT(lhs))) {
5848 ADD_INSN(post, node, pop);
5853 DECL_ANCHOR(anchor);
5854 INIT_ANCHOR(anchor);
5855 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
5856 ELEM_REMOVE(FIRST_ELEMENT(anchor));
5857 ADD_SEQ(lhs, anchor);
5865compile_massign_opt_lhs(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *lhsn)
5868 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
5869 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
5875compile_massign_opt(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
5876 const NODE *rhsn,
const NODE *orig_lhsn)
5879 const int memsize = numberof(mem);
5881 int llen = 0, rlen = 0;
5883 const NODE *lhsn = orig_lhsn;
5885#define MEMORY(v) { \
5887 if (memindex == memsize) return 0; \
5888 for (i=0; i<memindex; i++) { \
5889 if (mem[i] == (v)) return 0; \
5891 mem[memindex++] = (v); \
5894 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
5899 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
5900 switch (nd_type(ln)) {
5905 MEMORY(get_nd_vid(ln));
5910 lhsn = RNODE_LIST(lhsn)->nd_next;
5916 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
5919 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
5921 rhsn = RNODE_LIST(rhsn)->nd_next;
5926 for (i=0; i<llen-rlen; i++) {
5927 ADD_INSN(ret, orig_lhsn, putnil);
5931 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
5936compile_massign0(rb_iseq_t *iseq, LINK_ANCHOR *
const pre, LINK_ANCHOR *
const rhs, LINK_ANCHOR *
const lhs, LINK_ANCHOR *
const post,
const NODE *
const node,
struct masgn_state *state,
int popped)
5938 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
5939 const NODE *splatn = RNODE_MASGN(node)->nd_args;
5940 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
5941 const NODE *lhsn_count = lhsn;
5942 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
5947 while (lhsn_count) {
5949 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
5952 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
5954 lhsn = RNODE_LIST(lhsn)->nd_next;
5958 if (nd_type_p(splatn, NODE_POSTARG)) {
5960 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
5961 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
5962 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
5964 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
5966 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
5968 if (NODE_NAMED_REST_P(restn)) {
5969 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
5972 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
5974 postn = RNODE_LIST(postn)->nd_next;
5979 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
5983 if (!state->nested) {
5984 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
5988 ADD_INSN(rhs, node, dup);
5990 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
5995compile_massign(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
5997 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
5999 state.lhs_level = popped ? 0 : 1;
6002 state.first_memo = NULL;
6003 state.last_memo = NULL;
6013 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
6017 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
6018 for (
int i = 0; i < memo->num_args; i++) {
6019 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
6021 tmp_memo = memo->next;
6030 if (!popped && state.num_args >= 1) {
6032 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
6040collect_const_segments(rb_iseq_t *iseq,
const NODE *node)
6044 switch (nd_type(node)) {
6046 rb_ary_unshift(arr,
ID2SYM(RNODE_CONST(node)->nd_vid));
6047 RB_OBJ_SET_SHAREABLE(arr);
6050 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6051 rb_ary_unshift(arr,
ID2SYM(idNULL));
6052 RB_OBJ_SET_SHAREABLE(arr);
6055 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6056 node = RNODE_COLON2(node)->nd_head;
6065compile_const_prefix(rb_iseq_t *iseq,
const NODE *
const node,
6066 LINK_ANCHOR *
const pref, LINK_ANCHOR *
const body)
6068 switch (nd_type(node)) {
6070 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
6071 ADD_INSN1(body, node, putobject,
Qtrue);
6072 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
6075 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
6076 ADD_INSN(body, node, pop);
6077 ADD_INSN1(body, node, putobject, rb_cObject);
6078 ADD_INSN1(body, node, putobject,
Qtrue);
6079 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6082 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
6083 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
6084 ADD_INSN1(body, node, putobject,
Qfalse);
6085 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6088 CHECK(COMPILE(pref,
"const colon2 prefix", node));
6095compile_cpath(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
const NODE *cpath)
6097 if (nd_type_p(cpath, NODE_COLON3)) {
6099 ADD_INSN1(ret, cpath, putobject, rb_cObject);
6100 return VM_DEFINECLASS_FLAG_SCOPED;
6102 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
6104 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
6105 return VM_DEFINECLASS_FLAG_SCOPED;
6109 ADD_INSN1(ret, cpath, putspecialobject,
6110 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
6116private_recv_p(
const NODE *node)
6118 NODE *recv = get_nd_recv(node);
6119 if (recv && nd_type_p(recv, NODE_SELF)) {
6120 return RNODE_SELF(recv)->nd_state != 0;
6126defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6127 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
bool ignore);
6130compile_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);
6133defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6134 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
6137 enum defined_type expr_type = DEFINED_NOT_DEFINED;
6138 enum node_type
type;
6139 const int line = nd_line(node);
6140 const NODE *line_node = node;
6142 switch (
type = nd_type(node)) {
6146 expr_type = DEFINED_NIL;
6149 expr_type = DEFINED_SELF;
6152 expr_type = DEFINED_TRUE;
6155 expr_type = DEFINED_FALSE;
6160 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
6164 if (RNODE_LIST(vals)->nd_head) {
6165 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
6168 lfinish[1] = NEW_LABEL(line);
6170 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6172 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6185 case NODE_IMAGINARY:
6190 expr_type = DEFINED_EXPR;
6194 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6196 lfinish[1] = NEW_LABEL(line);
6198 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6199 expr_type = DEFINED_EXPR;
6205 expr_type = DEFINED_LVAR;
6208#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6210 ADD_INSN3(ret, line_node, definedivar,
6211 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6215 ADD_INSN(ret, line_node, putnil);
6216 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6217 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6221 ADD_INSN(ret, line_node, putnil);
6222 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6223 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6227 ADD_INSN(ret, line_node, putnil);
6228 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6229 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6233 lfinish[1] = NEW_LABEL(line);
6235 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6236 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6237 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6240 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6241 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6244 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6245 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6249 ADD_INSN1(ret, line_node, putobject, rb_cObject);
6250 ADD_INSN3(ret, line_node, defined,
6251 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6259 case NODE_ATTRASGN:{
6260 const int explicit_receiver =
6261 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6262 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6264 if (get_nd_args(node) || explicit_receiver) {
6266 lfinish[1] = NEW_LABEL(line);
6269 lfinish[2] = NEW_LABEL(line);
6272 if (get_nd_args(node)) {
6273 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6274 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6276 if (explicit_receiver) {
6277 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6278 switch (nd_type(get_nd_recv(node))) {
6284 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6285 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6288 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6289 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6293 ADD_INSN(ret, line_node, dup);
6295 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6296 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6299 ADD_INSN(ret, line_node, putself);
6301 ADD_INSN(ret, line_node, dup);
6303 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6304 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6310 ADD_INSN(ret, line_node, putnil);
6311 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6312 PUSH_VAL(DEFINED_YIELD));
6313 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6318 ADD_INSN(ret, line_node, putnil);
6319 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6320 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6321 PUSH_VAL(DEFINED_GVAR));
6326 ADD_INSN(ret, line_node, putnil);
6327 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6328 PUSH_VAL(DEFINED_ZSUPER));
6334 case NODE_OP_ASGN_OR:
6335 case NODE_OP_ASGN_AND:
6344 expr_type = DEFINED_ASGN;
6351 VALUE str = rb_iseq_defined_string(expr_type);
6352 ADD_INSN1(ret, line_node, putobject, str);
6355 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6360build_defined_rescue_iseq(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const void *unused)
6362 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6363 iseq_set_exception_local_table(iseq);
6367defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6368 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
bool ignore)
6370 LINK_ELEMENT *lcur = ret->last;
6371 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6373 int line = nd_line(node);
6374 LABEL *lstart = NEW_LABEL(line);
6375 LABEL *lend = NEW_LABEL(line);
6376 const rb_iseq_t *rescue;
6378 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6379 rescue = NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc,
6381 ISEQ_BODY(iseq)->location.label),
6382 ISEQ_TYPE_RESCUE, 0);
6383 lstart->rescued = LABEL_RESCUE_BEG;
6384 lend->rescued = LABEL_RESCUE_END;
6385 APPEND_LABEL(ret, lcur, lstart);
6386 ADD_LABEL(ret, lend);
6388 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6394compile_defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE needstr,
bool ignore)
6396 const int line = nd_line(node);
6397 const NODE *line_node = node;
6398 if (!RNODE_DEFINED(node)->nd_head) {
6399 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6400 ADD_INSN1(ret, line_node, putobject, str);
6404 LINK_ELEMENT *last = ret->last;
6405 lfinish[0] = NEW_LABEL(line);
6408 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6410 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6411 ADD_INSN(ret, line_node, swap);
6413 ADD_LABEL(ret, lfinish[2]);
6415 ADD_INSN(ret, line_node, pop);
6416 ADD_LABEL(ret, lfinish[1]);
6418 ADD_LABEL(ret, lfinish[0]);
6424make_name_for_block(
const rb_iseq_t *orig_iseq)
6427 const rb_iseq_t *iseq = orig_iseq;
6429 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6430 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6431 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6434 iseq = ISEQ_BODY(iseq)->parent_iseq;
6439 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6442 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6447push_ensure_entry(rb_iseq_t *iseq,
6451 enl->ensure_node = node;
6452 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6454 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6458add_ensure_range(rb_iseq_t *iseq,
struct ensure_range *erange,
6459 LABEL *lstart, LABEL *lend)
6464 while (erange->next != 0) {
6465 erange = erange->next;
6469 ne->end = erange->end;
6470 erange->end = lstart;
6476can_add_ensure_iseq(
const rb_iseq_t *iseq)
6479 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
6481 if (e->ensure_node)
return false;
6489add_ensure_iseq(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
int is_return)
6494 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6496 DECL_ANCHOR(ensure);
6498 INIT_ANCHOR(ensure);
6500 if (enlp->erange != NULL) {
6501 DECL_ANCHOR(ensure_part);
6502 LABEL *lstart = NEW_LABEL(0);
6503 LABEL *lend = NEW_LABEL(0);
6504 INIT_ANCHOR(ensure_part);
6506 add_ensure_range(iseq, enlp->erange, lstart, lend);
6508 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6509 ADD_LABEL(ensure_part, lstart);
6510 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6511 ADD_LABEL(ensure_part, lend);
6512 ADD_SEQ(ensure, ensure_part);
6521 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6522 ADD_SEQ(ret, ensure);
6527check_keyword(
const NODE *node)
6531 if (nd_type_p(node, NODE_LIST)) {
6532 while (RNODE_LIST(node)->nd_next) {
6533 node = RNODE_LIST(node)->nd_next;
6535 node = RNODE_LIST(node)->nd_head;
6538 return keyword_node_p(node);
6543keyword_node_single_splat_p(NODE *kwnode)
6547 NODE *node = RNODE_HASH(kwnode)->nd_head;
6548 return RNODE_LIST(node)->nd_head == NULL &&
6549 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6553compile_single_keyword_splat_mutable(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6554 NODE *kwnode,
unsigned int *flag_ptr)
6556 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6557 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6558 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6559 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6560 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6563#define SPLATARRAY_FALSE 0
6564#define SPLATARRAY_TRUE 1
6565#define DUP_SINGLE_KW_SPLAT 2
6568setup_args_core(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6569 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6571 if (!argn)
return 0;
6573 NODE *kwnode = NULL;
6575 switch (nd_type(argn)) {
6578 int len = compile_args(iseq, args, argn, &kwnode);
6579 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6582 if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6586 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6587 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6590 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6599 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6600 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6601 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6602 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6603 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6606 case NODE_ARGSCAT: {
6607 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6608 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6609 bool args_pushed =
false;
6611 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6612 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6613 if (kwnode) rest_len--;
6614 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6618 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6619 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6622 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6623 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6624 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6627 else if (!args_pushed) {
6628 ADD_INSN(args, argn, concattoarray);
6634 *flag_ptr |= VM_CALL_KW_SPLAT;
6635 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6641 case NODE_ARGSPUSH: {
6642 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6643 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6645 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6646 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6647 if (kwnode) rest_len--;
6648 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6649 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6652 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6653 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6656 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6657 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6663 *flag_ptr |= VM_CALL_KW_SPLAT;
6664 if (!keyword_node_single_splat_p(kwnode)) {
6665 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6666 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6668 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
6669 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6672 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6680 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
6686setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
6688 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
6689 *flag |= VM_CALL_ARGS_SPLAT_MUT;
6694setup_args_dup_rest_p(
const NODE *argn)
6696 switch(nd_type(argn)) {
6707 case NODE_IMAGINARY:
6720 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
6723 if (setup_args_dup_rest_p(RNODE_LIST(argn)->nd_head)) {
6726 argn = RNODE_LIST(argn)->nd_next;
6735setup_args(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6739 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
6742 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
6743 RNODE_BLOCK_PASS(argn)->nd_head : argn;
6746 switch(nd_type(check_arg)) {
6749 dup_rest = SPLATARRAY_FALSE;
6753 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
6755 case(NODE_ARGSPUSH):
6757 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
6758 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
6759 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
6760 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
6761 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
6763 if (dup_rest == SPLATARRAY_FALSE) {
6765 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
6767 NODE *key_node = RNODE_LIST(node)->nd_head;
6768 if (key_node && setup_args_dup_rest_p(key_node)) {
6769 dup_rest = SPLATARRAY_TRUE;
6773 node = RNODE_LIST(node)->nd_next;
6774 NODE *value_node = RNODE_LIST(node)->nd_head;
6775 if (setup_args_dup_rest_p(value_node)) {
6776 dup_rest = SPLATARRAY_TRUE;
6780 node = RNODE_LIST(node)->nd_next;
6789 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
6791 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
6794 initial_dup_rest = dup_rest;
6796 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
6797 DECL_ANCHOR(arg_block);
6798 INIT_ANCHOR(arg_block);
6800 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
6801 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
6803 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
6804 const NODE * arg_node =
6805 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
6812 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
6813 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
6816 *flag |= VM_CALL_FORWARDING;
6818 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
6819 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6823 *flag |= VM_CALL_ARGS_BLOCKARG;
6825 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
6828 if (LIST_INSN_SIZE_ONE(arg_block)) {
6829 LINK_ELEMENT *elem = FIRST_ELEMENT(arg_block);
6830 if (IS_INSN(elem)) {
6831 INSN *iobj = (INSN *)elem;
6832 if (iobj->insn_id == BIN(getblockparam)) {
6833 iobj->insn_id = BIN(getblockparamproxy);
6837 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
6838 ADD_SEQ(args, arg_block);
6841 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
6843 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6848build_postexe_iseq(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const void *ptr)
6850 const NODE *body = ptr;
6851 int line = nd_line(body);
6853 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
6855 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6856 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
6858 iseq_set_local_table(iseq, 0, 0);
6862compile_named_capture_assign(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node)
6866 int line = nd_line(node);
6867 const NODE *line_node = node;
6868 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
6870#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
6871 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
6875 ADD_INSN(ret, line_node, dup);
6876 ADD_INSNL(ret, line_node, branchunless, fail_label);
6878 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6880 if (RNODE_BLOCK(vars)->nd_next) {
6881 ADD_INSN(ret, line_node, dup);
6884 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6886 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
6888 ELEM_INSERT_PREV(last->next, (LINK_ELEMENT *)cap);
6889#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
6890 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
6895 ADD_INSNL(nom, line_node, jump, end_label);
6896 ADD_LABEL(nom, fail_label);
6898 ADD_INSN(nom, line_node, pop);
6899 ADD_INSN(nom, line_node, putnil);
6901 ADD_LABEL(nom, end_label);
6902 (nom->last->next = cap->link.next)->prev = nom->last;
6903 (cap->link.next = nom->anchor.next)->prev = &cap->link;
6908 ADD_INSNL(ret, line_node, jump, end_label);
6909 ADD_LABEL(ret, fail_label);
6910 ADD_INSN(ret, line_node, pop);
6911 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6913 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6915 ((INSN*)last)->insn_id = BIN(putnil);
6916 ((INSN*)last)->operand_size = 0;
6918 ADD_LABEL(ret, end_label);
6922optimizable_range_item_p(
const NODE *n)
6924 if (!n)
return FALSE;
6925 switch (nd_type(n)) {
6938optimized_range_item(
const NODE *n)
6940 switch (nd_type(n)) {
6942 return rb_node_line_lineno_val(n);
6944 return rb_node_integer_literal_val(n);
6946 return rb_node_float_literal_val(n);
6948 return rb_node_rational_literal_val(n);
6949 case NODE_IMAGINARY:
6950 return rb_node_imaginary_literal_val(n);
6954 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
6959compile_if(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
6961 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
6962 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
6964 const int line = nd_line(node);
6965 const NODE *line_node = node;
6966 DECL_ANCHOR(cond_seq);
6967 LABEL *then_label, *else_label, *end_label;
6970 INIT_ANCHOR(cond_seq);
6971 then_label = NEW_LABEL(line);
6972 else_label = NEW_LABEL(line);
6975 NODE *cond = RNODE_IF(node)->nd_cond;
6976 if (nd_type(cond) == NODE_BLOCK) {
6977 cond = RNODE_BLOCK(cond)->nd_head;
6980 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
6981 ADD_SEQ(ret, cond_seq);
6983 if (then_label->refcnt && else_label->refcnt) {
6984 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
6987 if (then_label->refcnt) {
6988 ADD_LABEL(ret, then_label);
6990 DECL_ANCHOR(then_seq);
6991 INIT_ANCHOR(then_seq);
6992 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
6994 if (else_label->refcnt) {
6995 const NODE *
const coverage_node = node_body ? node_body : node;
6996 add_trace_branch_coverage(
6999 nd_code_loc(coverage_node),
7000 nd_node_id(coverage_node),
7002 type == NODE_IF ?
"then" :
"else",
7004 end_label = NEW_LABEL(line);
7005 ADD_INSNL(then_seq, line_node, jump, end_label);
7007 ADD_INSN(then_seq, line_node, pop);
7010 ADD_SEQ(ret, then_seq);
7013 if (else_label->refcnt) {
7014 ADD_LABEL(ret, else_label);
7016 DECL_ANCHOR(else_seq);
7017 INIT_ANCHOR(else_seq);
7018 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
7020 if (then_label->refcnt) {
7021 const NODE *
const coverage_node = node_else ? node_else : node;
7022 add_trace_branch_coverage(
7025 nd_code_loc(coverage_node),
7026 nd_node_id(coverage_node),
7028 type == NODE_IF ?
"else" :
"then",
7031 ADD_SEQ(ret, else_seq);
7035 ADD_LABEL(ret, end_label);
7042compile_case(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
7045 const NODE *node = orig_node;
7046 LABEL *endlabel, *elselabel;
7048 DECL_ANCHOR(body_seq);
7049 DECL_ANCHOR(cond_seq);
7050 int only_special_literals = 1;
7053 enum node_type
type;
7054 const NODE *line_node;
7059 INIT_ANCHOR(body_seq);
7060 INIT_ANCHOR(cond_seq);
7062 RHASH_TBL_RAW(literals)->type = &cdhash_type;
7064 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
7066 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
7068 node = RNODE_CASE(node)->nd_body;
7069 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
7070 type = nd_type(node);
7071 line = nd_line(node);
7074 endlabel = NEW_LABEL(line);
7075 elselabel = NEW_LABEL(line);
7079 while (
type == NODE_WHEN) {
7082 l1 = NEW_LABEL(line);
7083 ADD_LABEL(body_seq, l1);
7084 ADD_INSN(body_seq, line_node, pop);
7086 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7087 add_trace_branch_coverage(
7090 nd_code_loc(coverage_node),
7091 nd_node_id(coverage_node),
7096 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
7097 ADD_INSNL(body_seq, line_node, jump, endlabel);
7099 vals = RNODE_WHEN(node)->nd_head;
7101 switch (nd_type(vals)) {
7103 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
7104 if (only_special_literals < 0)
return COMPILE_NG;
7109 only_special_literals = 0;
7110 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
7113 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
7117 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
7120 node = RNODE_WHEN(node)->nd_next;
7124 type = nd_type(node);
7125 line = nd_line(node);
7130 ADD_LABEL(cond_seq, elselabel);
7131 ADD_INSN(cond_seq, line_node, pop);
7132 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
7133 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7134 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7137 debugs(
"== else (implicit)\n");
7138 ADD_LABEL(cond_seq, elselabel);
7139 ADD_INSN(cond_seq, orig_node, pop);
7140 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
7142 ADD_INSN(cond_seq, orig_node, putnil);
7144 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7147 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7148 ADD_INSN(ret, orig_node, dup);
7150 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
7152 LABEL_REF(elselabel);
7155 ADD_SEQ(ret, cond_seq);
7156 ADD_SEQ(ret, body_seq);
7157 ADD_LABEL(ret, endlabel);
7162compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
7166 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
7168 DECL_ANCHOR(body_seq);
7172 branches = decl_branch_base(iseq, PTR2NUM(orig_node), nd_code_loc(orig_node),
"case");
7174 INIT_ANCHOR(body_seq);
7175 endlabel = NEW_LABEL(nd_line(node));
7177 while (node && nd_type_p(node, NODE_WHEN)) {
7178 const int line = nd_line(node);
7179 LABEL *l1 = NEW_LABEL(line);
7180 ADD_LABEL(body_seq, l1);
7182 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7183 add_trace_branch_coverage(
7186 nd_code_loc(coverage_node),
7187 nd_node_id(coverage_node),
7192 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7193 ADD_INSNL(body_seq, node, jump, endlabel);
7195 vals = RNODE_WHEN(node)->nd_head;
7197 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7199 switch (nd_type(vals)) {
7203 val = RNODE_LIST(vals)->nd_head;
7204 lnext = NEW_LABEL(nd_line(val));
7205 debug_compile(
"== when2\n", (
void)0);
7206 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7207 ADD_LABEL(ret, lnext);
7208 vals = RNODE_LIST(vals)->nd_next;
7214 ADD_INSN(ret, vals, putnil);
7215 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7216 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7217 ADD_INSNL(ret, vals, branchif, l1);
7220 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7222 node = RNODE_WHEN(node)->nd_next;
7225 const NODE *
const coverage_node = node ? node : orig_node;
7226 add_trace_branch_coverage(
7229 nd_code_loc(coverage_node),
7230 nd_node_id(coverage_node),
7234 CHECK(COMPILE_(ret,
"else", node, popped));
7235 ADD_INSNL(ret, orig_node, jump, endlabel);
7237 ADD_SEQ(ret, body_seq);
7238 ADD_LABEL(ret, endlabel);
7242static 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);
7244static 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);
7245static 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);
7246static int iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7247static 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);
7248static int iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7250#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7251#define CASE3_BI_OFFSET_ERROR_STRING 1
7252#define CASE3_BI_OFFSET_KEY_ERROR_P 2
7253#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7254#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7257iseq_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)
7259 const int line = nd_line(node);
7260 const NODE *line_node = node;
7262 switch (nd_type(node)) {
7316 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7317 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7318 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7320 const int min_argc = pre_args_num + post_args_num;
7321 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7322 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7324 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7326 match_failed = NEW_LABEL(line);
7327 type_error = NEW_LABEL(line);
7328 deconstruct = NEW_LABEL(line);
7329 deconstructed = NEW_LABEL(line);
7332 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7333 ADD_INSN(ret, line_node, swap);
7339 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7341 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7343 ADD_INSN(ret, line_node, dup);
7344 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7345 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7346 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7347 if (in_single_pattern) {
7348 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7349 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7350 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7351 INT2FIX(min_argc), base_index + 1 ));
7353 ADD_INSNL(ret, line_node, branchunless, match_failed);
7355 for (i = 0; i < pre_args_num; i++) {
7356 ADD_INSN(ret, line_node, dup);
7357 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7358 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7359 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7360 args = RNODE_LIST(args)->nd_next;
7363 if (RNODE_ARYPTN(node)->rest_arg) {
7364 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7365 ADD_INSN(ret, line_node, dup);
7366 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7367 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7368 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7369 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7370 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7371 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7372 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7374 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7377 if (post_args_num > 0) {
7378 ADD_INSN(ret, line_node, dup);
7379 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7380 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7381 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7382 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7383 ADD_INSN(ret, line_node, pop);
7388 args = RNODE_ARYPTN(node)->post_args;
7389 for (i = 0; i < post_args_num; i++) {
7390 ADD_INSN(ret, line_node, dup);
7392 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7393 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7394 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7396 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7397 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7398 args = RNODE_LIST(args)->nd_next;
7401 ADD_INSN(ret, line_node, pop);
7403 ADD_INSN(ret, line_node, pop);
7405 ADD_INSNL(ret, line_node, jump, matched);
7406 ADD_INSN(ret, line_node, putnil);
7408 ADD_INSN(ret, line_node, putnil);
7411 ADD_LABEL(ret, type_error);
7412 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7414 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7415 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7416 ADD_INSN(ret, line_node, pop);
7418 ADD_LABEL(ret, match_failed);
7419 ADD_INSN(ret, line_node, pop);
7421 ADD_INSN(ret, line_node, pop);
7423 ADD_INSNL(ret, line_node, jump, unmatched);
7476 const NODE *args = RNODE_FNDPTN(node)->args;
7477 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7479 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7480 match_failed = NEW_LABEL(line);
7481 type_error = NEW_LABEL(line);
7482 deconstruct = NEW_LABEL(line);
7483 deconstructed = NEW_LABEL(line);
7485 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7487 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7489 ADD_INSN(ret, line_node, dup);
7490 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7491 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7492 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7493 if (in_single_pattern) {
7494 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 ));
7496 ADD_INSNL(ret, line_node, branchunless, match_failed);
7499 LABEL *while_begin = NEW_LABEL(nd_line(node));
7500 LABEL *next_loop = NEW_LABEL(nd_line(node));
7501 LABEL *find_succeeded = NEW_LABEL(line);
7502 LABEL *find_failed = NEW_LABEL(nd_line(node));
7505 ADD_INSN(ret, line_node, dup);
7506 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7508 ADD_INSN(ret, line_node, dup);
7509 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7510 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7512 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7514 ADD_LABEL(ret, while_begin);
7516 ADD_INSN(ret, line_node, dup);
7517 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7518 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7519 ADD_INSNL(ret, line_node, branchunless, find_failed);
7521 for (j = 0; j < args_num; j++) {
7522 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7523 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7525 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7526 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7528 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7530 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7531 args = RNODE_LIST(args)->nd_next;
7534 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7535 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7536 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7537 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7538 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7539 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));
7541 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7542 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7543 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7544 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7545 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7546 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7547 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7548 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));
7550 ADD_INSNL(ret, line_node, jump, find_succeeded);
7552 ADD_LABEL(ret, next_loop);
7553 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7554 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7555 ADD_INSNL(ret, line_node, jump, while_begin);
7557 ADD_LABEL(ret, find_failed);
7558 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7559 if (in_single_pattern) {
7560 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7561 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7562 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7563 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7564 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7566 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7567 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7569 ADD_INSN(ret, line_node, pop);
7570 ADD_INSN(ret, line_node, pop);
7572 ADD_INSNL(ret, line_node, jump, match_failed);
7573 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7575 ADD_LABEL(ret, find_succeeded);
7576 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7579 ADD_INSN(ret, line_node, pop);
7580 ADD_INSNL(ret, line_node, jump, matched);
7581 ADD_INSN(ret, line_node, putnil);
7583 ADD_LABEL(ret, type_error);
7584 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7586 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7587 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7588 ADD_INSN(ret, line_node, pop);
7590 ADD_LABEL(ret, match_failed);
7591 ADD_INSN(ret, line_node, pop);
7592 ADD_INSNL(ret, line_node, jump, unmatched);
7656 LABEL *match_failed, *type_error;
7659 match_failed = NEW_LABEL(line);
7660 type_error = NEW_LABEL(line);
7662 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
7663 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7664 keys =
rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
7666 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
7667 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
7671 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7673 ADD_INSN(ret, line_node, dup);
7674 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
7675 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7676 if (in_single_pattern) {
7677 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
7679 ADD_INSNL(ret, line_node, branchunless, match_failed);
7682 ADD_INSN(ret, line_node, putnil);
7685 RB_OBJ_SET_FROZEN_SHAREABLE(keys);
7686 ADD_INSN1(ret, line_node, duparray, keys);
7689 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
7691 ADD_INSN(ret, line_node, dup);
7693 ADD_INSNL(ret, line_node, branchunless, type_error);
7695 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7696 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
7699 if (RNODE_HSHPTN(node)->nd_pkwargs) {
7703 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7705 DECL_ANCHOR(match_values);
7706 INIT_ANCHOR(match_values);
7707 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
7708 for (i = 0; i < keys_num; i++) {
7709 NODE *key_node = RNODE_LIST(args)->nd_head;
7710 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
7711 VALUE key = get_symbol_value(iseq, key_node);
7713 ADD_INSN(ret, line_node, dup);
7714 ADD_INSN1(ret, line_node, putobject, key);
7715 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
7716 if (in_single_pattern) {
7717 LABEL *match_succeeded;
7718 match_succeeded = NEW_LABEL(line);
7720 ADD_INSN(ret, line_node, dup);
7721 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7724 ADD_INSN1(ret, line_node, putobject, RB_OBJ_SET_SHAREABLE(str));
7725 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
7726 ADD_INSN1(ret, line_node, putobject,
Qtrue);
7727 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
7728 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7729 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
7730 ADD_INSN1(ret, line_node, putobject, key);
7731 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
7733 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
7735 ADD_LABEL(ret, match_succeeded);
7737 ADD_INSNL(ret, line_node, branchunless, match_failed);
7739 ADD_INSN(match_values, line_node, dup);
7740 ADD_INSN1(match_values, line_node, putobject, key);
7741 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
7742 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7743 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
7745 ADD_SEQ(ret, match_values);
7749 ADD_INSN(ret, line_node, dup);
7750 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7751 if (in_single_pattern) {
7752 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
7754 ADD_INSNL(ret, line_node, branchunless, match_failed);
7757 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7758 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
7759 ADD_INSN(ret, line_node, dup);
7760 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7761 if (in_single_pattern) {
7762 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
7764 ADD_INSNL(ret, line_node, branchunless, match_failed);
7767 ADD_INSN(ret, line_node, dup);
7768 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7772 ADD_INSN(ret, line_node, pop);
7773 ADD_INSNL(ret, line_node, jump, matched);
7774 ADD_INSN(ret, line_node, putnil);
7776 ADD_LABEL(ret, type_error);
7777 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7779 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
7780 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7781 ADD_INSN(ret, line_node, pop);
7783 ADD_LABEL(ret, match_failed);
7784 ADD_INSN(ret, line_node, pop);
7785 ADD_INSNL(ret, line_node, jump, unmatched);
7794 case NODE_IMAGINARY:
7822 CHECK(COMPILE(ret,
"case in literal", node));
7823 if (in_single_pattern) {
7824 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7826 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7827 if (in_single_pattern) {
7828 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
7830 ADD_INSNL(ret, line_node, branchif, matched);
7831 ADD_INSNL(ret, line_node, jump, unmatched);
7835 ID id = RNODE_LASGN(node)->nd_vid;
7836 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
7838 if (in_alt_pattern) {
7839 const char *name = rb_id2name(
id);
7840 if (name && strlen(name) > 0 && name[0] !=
'_') {
7841 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7847 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
7848 ADD_INSNL(ret, line_node, jump, matched);
7853 ID id = RNODE_DASGN(node)->nd_vid;
7855 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
7857 if (in_alt_pattern) {
7858 const char *name = rb_id2name(
id);
7859 if (name && strlen(name) > 0 && name[0] !=
'_') {
7860 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7867 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
7871 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
7872 ADD_INSNL(ret, line_node, jump, matched);
7877 LABEL *match_failed;
7878 match_failed = unmatched;
7879 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7880 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
7881 if (in_single_pattern) {
7882 LABEL *match_succeeded;
7883 match_succeeded = NEW_LABEL(line);
7885 ADD_INSN(ret, line_node, dup);
7886 if (nd_type_p(node, NODE_IF)) {
7887 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7890 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
7893 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
7894 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7895 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7896 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7898 ADD_INSN(ret, line_node, pop);
7899 ADD_INSN(ret, line_node, pop);
7901 ADD_LABEL(ret, match_succeeded);
7903 if (nd_type_p(node, NODE_IF)) {
7904 ADD_INSNL(ret, line_node, branchunless, match_failed);
7907 ADD_INSNL(ret, line_node, branchif, match_failed);
7909 ADD_INSNL(ret, line_node, jump, matched);
7914 LABEL *match_failed;
7915 match_failed = NEW_LABEL(line);
7917 n = RNODE_HASH(node)->nd_head;
7918 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
7919 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7923 ADD_INSN(ret, line_node, dup);
7924 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));
7925 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));
7926 ADD_INSN(ret, line_node, putnil);
7928 ADD_LABEL(ret, match_failed);
7929 ADD_INSN(ret, line_node, pop);
7930 ADD_INSNL(ret, line_node, jump, unmatched);
7934 LABEL *match_succeeded, *fin;
7935 match_succeeded = NEW_LABEL(line);
7936 fin = NEW_LABEL(line);
7938 ADD_INSN(ret, line_node, dup);
7939 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));
7940 ADD_LABEL(ret, match_succeeded);
7941 ADD_INSN(ret, line_node, pop);
7942 ADD_INSNL(ret, line_node, jump, matched);
7943 ADD_INSN(ret, line_node, putnil);
7944 ADD_LABEL(ret, fin);
7945 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
7949 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
7955iseq_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)
7957 LABEL *fin = NEW_LABEL(nd_line(node));
7958 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7959 ADD_LABEL(ret, fin);
7964iseq_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)
7966 const NODE *line_node = node;
7968 if (RNODE_ARYPTN(node)->nd_pconst) {
7969 ADD_INSN(ret, line_node, dup);
7970 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
7971 if (in_single_pattern) {
7972 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7974 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7975 if (in_single_pattern) {
7976 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
7978 ADD_INSNL(ret, line_node, branchunless, match_failed);
7985iseq_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)
7987 const NODE *line_node = node;
7991 if (use_deconstructed_cache) {
7993 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7994 ADD_INSNL(ret, line_node, branchnil, deconstruct);
7997 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7998 ADD_INSNL(ret, line_node, branchunless, match_failed);
8001 ADD_INSN(ret, line_node, pop);
8002 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
8003 ADD_INSNL(ret, line_node, jump, deconstructed);
8006 ADD_INSNL(ret, line_node, jump, deconstruct);
8009 ADD_LABEL(ret, deconstruct);
8010 ADD_INSN(ret, line_node, dup);
8011 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
8012 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
8015 if (use_deconstructed_cache) {
8016 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
8019 if (in_single_pattern) {
8020 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
8023 ADD_INSNL(ret, line_node, branchunless, match_failed);
8025 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
8028 if (use_deconstructed_cache) {
8029 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8032 ADD_INSN(ret, line_node, dup);
8034 ADD_INSNL(ret, line_node, branchunless, type_error);
8036 ADD_LABEL(ret, deconstructed);
8042iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index)
8052 const int line = nd_line(node);
8053 const NODE *line_node = node;
8054 LABEL *match_succeeded = NEW_LABEL(line);
8056 ADD_INSN(ret, line_node, dup);
8057 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8059 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8060 ADD_INSN1(ret, line_node, putobject, errmsg);
8061 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8062 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
8063 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8065 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8066 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8068 ADD_INSN(ret, line_node, pop);
8069 ADD_INSN(ret, line_node, pop);
8070 ADD_LABEL(ret, match_succeeded);
8076iseq_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)
8086 const int line = nd_line(node);
8087 const NODE *line_node = node;
8088 LABEL *match_succeeded = NEW_LABEL(line);
8090 ADD_INSN(ret, line_node, dup);
8091 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8093 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8094 ADD_INSN1(ret, line_node, putobject, errmsg);
8095 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8096 ADD_INSN(ret, line_node, dup);
8097 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
8098 ADD_INSN1(ret, line_node, putobject, pattern_length);
8099 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
8100 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8102 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8103 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8105 ADD_INSN(ret, line_node, pop);
8106 ADD_INSN(ret, line_node, pop);
8107 ADD_LABEL(ret, match_succeeded);
8113iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
8123 const int line = nd_line(node);
8124 const NODE *line_node = node;
8125 LABEL *match_succeeded = NEW_LABEL(line);
8127 ADD_INSN(ret, line_node, dup);
8128 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8130 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8131 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
8132 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8133 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
8134 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
8135 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8137 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8138 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8140 ADD_INSN(ret, line_node, pop);
8141 ADD_INSN(ret, line_node, pop);
8143 ADD_LABEL(ret, match_succeeded);
8144 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
8145 ADD_INSN(ret, line_node, pop);
8146 ADD_INSN(ret, line_node, pop);
8152compile_case3(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
8154 const NODE *pattern;
8155 const NODE *node = orig_node;
8156 LABEL *endlabel, *elselabel;
8158 DECL_ANCHOR(body_seq);
8159 DECL_ANCHOR(cond_seq);
8161 enum node_type
type;
8162 const NODE *line_node;
8165 bool single_pattern;
8168 INIT_ANCHOR(body_seq);
8169 INIT_ANCHOR(cond_seq);
8171 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
8173 node = RNODE_CASE3(node)->nd_body;
8174 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
8175 type = nd_type(node);
8176 line = nd_line(node);
8178 single_pattern = !RNODE_IN(node)->nd_next;
8180 endlabel = NEW_LABEL(line);
8181 elselabel = NEW_LABEL(line);
8183 if (single_pattern) {
8185 ADD_INSN(head, line_node, putnil);
8186 ADD_INSN(head, line_node, putnil);
8187 ADD_INSN1(head, line_node, putobject,
Qfalse);
8188 ADD_INSN(head, line_node, putnil);
8190 ADD_INSN(head, line_node, putnil);
8192 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8196 while (
type == NODE_IN) {
8200 ADD_INSN(body_seq, line_node, putnil);
8202 l1 = NEW_LABEL(line);
8203 ADD_LABEL(body_seq, l1);
8204 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8206 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8207 add_trace_branch_coverage(
8210 nd_code_loc(coverage_node),
8211 nd_node_id(coverage_node),
8216 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8217 ADD_INSNL(body_seq, line_node, jump, endlabel);
8219 pattern = RNODE_IN(node)->nd_head;
8221 int pat_line = nd_line(pattern);
8222 LABEL *next_pat = NEW_LABEL(pat_line);
8223 ADD_INSN (cond_seq, pattern, dup);
8225 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8226 ADD_LABEL(cond_seq, next_pat);
8227 LABEL_UNREMOVABLE(next_pat);
8230 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8234 node = RNODE_IN(node)->nd_next;
8238 type = nd_type(node);
8239 line = nd_line(node);
8244 ADD_LABEL(cond_seq, elselabel);
8245 ADD_INSN(cond_seq, line_node, pop);
8246 ADD_INSN(cond_seq, line_node, pop);
8247 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8248 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8249 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8250 ADD_INSN(cond_seq, line_node, putnil);
8252 ADD_INSN(cond_seq, line_node, putnil);
8256 debugs(
"== else (implicit)\n");
8257 ADD_LABEL(cond_seq, elselabel);
8258 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8259 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8261 if (single_pattern) {
8269 LABEL *key_error, *fin;
8272 key_error = NEW_LABEL(line);
8273 fin = NEW_LABEL(line);
8276 kw_arg->references = 0;
8277 kw_arg->keyword_len = 2;
8278 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
8279 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
8281 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8282 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8284 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8285 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8286 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8287 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8288 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8289 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8290 ADD_INSNL(cond_seq, orig_node, jump, fin);
8292 ADD_LABEL(cond_seq, key_error);
8294 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8295 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8296 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8297 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8298 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8299 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8300 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8301 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
8302 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8304 ADD_LABEL(cond_seq, fin);
8308 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8309 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8311 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8313 ADD_INSN(cond_seq, orig_node, putnil);
8315 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8316 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8318 ADD_INSN(cond_seq, line_node, putnil);
8322 ADD_SEQ(ret, cond_seq);
8323 ADD_SEQ(ret, body_seq);
8324 ADD_LABEL(ret, endlabel);
8328#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8329#undef CASE3_BI_OFFSET_ERROR_STRING
8330#undef CASE3_BI_OFFSET_KEY_ERROR_P
8331#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8332#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8335compile_loop(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
8337 const int line = (int)nd_line(node);
8338 const NODE *line_node = node;
8340 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8341 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8342 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8343 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8348 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8349 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8350 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8351 LABEL *end_label = NEW_LABEL(line);
8352 LABEL *adjust_label = NEW_LABEL(line);
8354 LABEL *next_catch_label = NEW_LABEL(line);
8355 LABEL *tmp_label = NULL;
8357 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8358 push_ensure_entry(iseq, &enl, NULL, NULL);
8360 if (RNODE_WHILE(node)->nd_state == 1) {
8361 ADD_INSNL(ret, line_node, jump, next_label);
8364 tmp_label = NEW_LABEL(line);
8365 ADD_INSNL(ret, line_node, jump, tmp_label);
8367 ADD_LABEL(ret, adjust_label);
8368 ADD_INSN(ret, line_node, putnil);
8369 ADD_LABEL(ret, next_catch_label);
8370 ADD_INSN(ret, line_node, pop);
8371 ADD_INSNL(ret, line_node, jump, next_label);
8372 if (tmp_label) ADD_LABEL(ret, tmp_label);
8374 ADD_LABEL(ret, redo_label);
8375 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8377 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8378 add_trace_branch_coverage(
8381 nd_code_loc(coverage_node),
8382 nd_node_id(coverage_node),
8387 CHECK(COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body));
8388 ADD_LABEL(ret, next_label);
8390 if (
type == NODE_WHILE) {
8391 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8392 redo_label, end_label));
8396 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8397 end_label, redo_label));
8400 ADD_LABEL(ret, end_label);
8401 ADD_ADJUST_RESTORE(ret, adjust_label);
8403 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8405 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8409 ADD_INSN(ret, line_node, putnil);
8412 ADD_LABEL(ret, break_label);
8415 ADD_INSN(ret, line_node, pop);
8418 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8420 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8422 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8423 ISEQ_COMPILE_DATA(iseq)->redo_label);
8425 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8426 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8427 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8428 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8429 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
8434compile_iter(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8436 const int line = nd_line(node);
8437 const NODE *line_node = node;
8438 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8439 LABEL *retry_label = NEW_LABEL(line);
8440 LABEL *retry_end_l = NEW_LABEL(line);
8441 const rb_iseq_t *child_iseq;
8443 ADD_LABEL(ret, retry_label);
8444 if (nd_type_p(node, NODE_FOR)) {
8445 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8447 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8448 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8449 ISEQ_TYPE_BLOCK, line);
8450 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8453 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8454 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8455 ISEQ_TYPE_BLOCK, line);
8456 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8469 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
8470 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
8471 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8472 iobj = (INSN*) get_prev_insn(iobj);
8474 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
8478 if (&iobj->link == LAST_ELEMENT(ret)) {
8479 ret->last = (LINK_ELEMENT*) retry_end_l;
8484 ADD_INSN(ret, line_node, pop);
8487 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8489 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8494compile_for_masgn(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8499 const NODE *line_node = node;
8500 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8501 LABEL *not_single = NEW_LABEL(nd_line(var));
8502 LABEL *not_ary = NEW_LABEL(nd_line(var));
8503 CHECK(COMPILE(ret,
"for var", var));
8504 ADD_INSN(ret, line_node, dup);
8505 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8506 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8507 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8508 ADD_INSNL(ret, line_node, branchunless, not_single);
8509 ADD_INSN(ret, line_node, dup);
8510 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8511 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8512 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8513 ADD_INSN(ret, line_node, swap);
8514 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
8515 ADD_INSN(ret, line_node, dup);
8516 ADD_INSNL(ret, line_node, branchunless, not_ary);
8517 ADD_INSN(ret, line_node, swap);
8518 ADD_LABEL(ret, not_ary);
8519 ADD_INSN(ret, line_node, pop);
8520 ADD_LABEL(ret, not_single);
8525compile_break(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8527 const NODE *line_node = node;
8528 unsigned long throw_flag = 0;
8530 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8532 LABEL *splabel = NEW_LABEL(0);
8533 ADD_LABEL(ret, splabel);
8534 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8535 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8536 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8537 add_ensure_iseq(ret, iseq, 0);
8538 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8539 ADD_ADJUST_RESTORE(ret, splabel);
8542 ADD_INSN(ret, line_node, putnil);
8546 const rb_iseq_t *ip = iseq;
8549 if (!ISEQ_COMPILE_DATA(ip)) {
8554 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8555 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8557 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8560 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8561 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8565 ip = ISEQ_BODY(ip)->parent_iseq;
8570 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8571 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8573 ADD_INSN(ret, line_node, pop);
8577 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8584compile_next(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8586 const NODE *line_node = node;
8587 unsigned long throw_flag = 0;
8589 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8590 LABEL *splabel = NEW_LABEL(0);
8591 debugs(
"next in while loop\n");
8592 ADD_LABEL(ret, splabel);
8593 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8594 add_ensure_iseq(ret, iseq, 0);
8595 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8596 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8597 ADD_ADJUST_RESTORE(ret, splabel);
8599 ADD_INSN(ret, line_node, putnil);
8602 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8603 LABEL *splabel = NEW_LABEL(0);
8604 debugs(
"next in block\n");
8605 ADD_LABEL(ret, splabel);
8606 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8607 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8608 add_ensure_iseq(ret, iseq, 0);
8609 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8610 ADD_ADJUST_RESTORE(ret, splabel);
8613 ADD_INSN(ret, line_node, putnil);
8617 const rb_iseq_t *ip = iseq;
8620 if (!ISEQ_COMPILE_DATA(ip)) {
8625 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8626 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8630 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8633 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8634 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8638 ip = ISEQ_BODY(ip)->parent_iseq;
8641 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8642 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8645 ADD_INSN(ret, line_node, pop);
8649 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
8657compile_redo(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8659 const NODE *line_node = node;
8661 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8662 LABEL *splabel = NEW_LABEL(0);
8663 debugs(
"redo in while");
8664 ADD_LABEL(ret, splabel);
8665 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8666 add_ensure_iseq(ret, iseq, 0);
8667 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8668 ADD_ADJUST_RESTORE(ret, splabel);
8670 ADD_INSN(ret, line_node, putnil);
8673 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8674 LABEL *splabel = NEW_LABEL(0);
8676 debugs(
"redo in block");
8677 ADD_LABEL(ret, splabel);
8678 add_ensure_iseq(ret, iseq, 0);
8679 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8680 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8681 ADD_ADJUST_RESTORE(ret, splabel);
8684 ADD_INSN(ret, line_node, putnil);
8688 const rb_iseq_t *ip = iseq;
8691 if (!ISEQ_COMPILE_DATA(ip)) {
8696 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8699 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8702 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8703 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
8707 ip = ISEQ_BODY(ip)->parent_iseq;
8710 ADD_INSN(ret, line_node, putnil);
8711 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8714 ADD_INSN(ret, line_node, pop);
8718 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
8726compile_retry(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8728 const NODE *line_node = node;
8730 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
8731 ADD_INSN(ret, line_node, putnil);
8732 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
8735 ADD_INSN(ret, line_node, pop);
8739 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
8746compile_rescue(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8748 const int line = nd_line(node);
8749 const NODE *line_node = node;
8750 LABEL *lstart = NEW_LABEL(line);
8751 LABEL *lend = NEW_LABEL(line);
8752 LABEL *lcont = NEW_LABEL(line);
8753 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
8755 ISEQ_BODY(iseq)->location.label),
8756 ISEQ_TYPE_RESCUE, line);
8758 lstart->rescued = LABEL_RESCUE_BEG;
8759 lend->rescued = LABEL_RESCUE_END;
8760 ADD_LABEL(ret, lstart);
8762 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
8763 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
8765 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
8767 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
8769 ADD_LABEL(ret, lend);
8770 if (RNODE_RESCUE(node)->nd_else) {
8771 ADD_INSN(ret, line_node, pop);
8772 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
8774 ADD_INSN(ret, line_node, nop);
8775 ADD_LABEL(ret, lcont);
8778 ADD_INSN(ret, line_node, pop);
8782 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
8783 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
8788compile_resbody(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8790 const int line = nd_line(node);
8791 const NODE *line_node = node;
8792 const NODE *resq = node;
8794 LABEL *label_miss, *label_hit;
8797 label_miss = NEW_LABEL(line);
8798 label_hit = NEW_LABEL(line);
8800 narg = RNODE_RESBODY(resq)->nd_args;
8802 switch (nd_type(narg)) {
8805 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8806 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
8807 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8808 ADD_INSNL(ret, line_node, branchif, label_hit);
8809 narg = RNODE_LIST(narg)->nd_next;
8815 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8816 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
8817 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
8818 ADD_INSNL(ret, line_node, branchif, label_hit);
8821 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
8825 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8827 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8828 ADD_INSNL(ret, line_node, branchif, label_hit);
8830 ADD_INSNL(ret, line_node, jump, label_miss);
8831 ADD_LABEL(ret, label_hit);
8834 if (RNODE_RESBODY(resq)->nd_exc_var) {
8835 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
8838 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) {
8840 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
8843 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
8846 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
8847 ADD_INSN(ret, line_node, nop);
8849 ADD_INSN(ret, line_node, leave);
8850 ADD_LABEL(ret, label_miss);
8851 resq = RNODE_RESBODY(resq)->nd_next;
8857compile_ensure(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8859 const int line = nd_line(RNODE_ENSURE(node)->nd_ensr);
8860 const NODE *line_node = node;
8862 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
8864 ISEQ_TYPE_ENSURE, line);
8865 LABEL *lstart = NEW_LABEL(line);
8866 LABEL *lend = NEW_LABEL(line);
8867 LABEL *lcont = NEW_LABEL(line);
8875 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
8877 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
8882 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
8884 ADD_LABEL(ret, lstart);
8885 CHECK(COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
8886 ADD_LABEL(ret, lend);
8888 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
8889 ADD_LABEL(ret, lcont);
8890 if (last_leave) ADD_INSN(ret, line_node, pop);
8892 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
8893 if (lstart->link.next != &lend->link) {
8895 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
8897 erange = erange->next;
8901 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
8906compile_return(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8908 const NODE *line_node = node;
8911 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8912 const rb_iseq_t *is = iseq;
8913 enum rb_iseq_type t =
type;
8914 const NODE *retval = RNODE_RETURN(node)->nd_stts;
8917 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
8918 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
8919 t = ISEQ_BODY(is)->type;
8923 case ISEQ_TYPE_MAIN:
8925 rb_warn(
"argument of top-level return is ignored");
8929 type = ISEQ_TYPE_METHOD;
8936 if (
type == ISEQ_TYPE_METHOD) {
8937 splabel = NEW_LABEL(0);
8938 ADD_LABEL(ret, splabel);
8939 ADD_ADJUST(ret, line_node, 0);
8942 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
8944 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8945 add_ensure_iseq(ret, iseq, 1);
8947 ADD_INSN(ret, line_node, leave);
8948 ADD_ADJUST_RESTORE(ret, splabel);
8951 ADD_INSN(ret, line_node, putnil);
8955 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
8957 ADD_INSN(ret, line_node, pop);
8965drop_unreachable_return(LINK_ANCHOR *ret)
8967 LINK_ELEMENT *i = ret->last, *last;
8968 if (!i)
return false;
8969 if (IS_TRACE(i)) i = i->prev;
8970 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
8972 if (IS_ADJUST(i)) i = i->prev;
8973 if (!IS_INSN(i))
return false;
8974 switch (INSN_OF(i)) {
8981 (ret->last = last->prev)->next = NULL;
8986compile_evstr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8988 CHECK(COMPILE_(ret,
"nd_body", node, popped));
8990 if (!popped && !all_string_result_p(node)) {
8991 const NODE *line_node = node;
8992 const unsigned int flag = VM_CALL_FCALL;
8996 ADD_INSN(ret, line_node, dup);
8997 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
8998 ADD_INSN(ret, line_node, anytostring);
9004compile_lvar(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *line_node,
ID id)
9006 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
9008 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
9009 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
9013qcall_branch_start(rb_iseq_t *iseq, LINK_ANCHOR *
const recv,
VALUE *branches,
const NODE *node,
const NODE *line_node)
9015 LABEL *else_label = NEW_LABEL(nd_line(line_node));
9018 br = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"&.");
9020 ADD_INSN(recv, line_node, dup);
9021 ADD_INSNL(recv, line_node, branchnil, else_label);
9022 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
9027qcall_branch_end(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, LABEL *else_label,
VALUE branches,
const NODE *node,
const NODE *line_node)
9030 if (!else_label)
return;
9031 end_label = NEW_LABEL(nd_line(line_node));
9032 ADD_INSNL(ret, line_node, jump, end_label);
9033 ADD_LABEL(ret, else_label);
9034 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
9035 ADD_LABEL(ret, end_label);
9039compile_call_precheck_freeze(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
const NODE *line_node,
int popped)
9044 if (get_nd_recv(node) &&
9045 (nd_type_p(get_nd_recv(node), NODE_STR) || nd_type_p(get_nd_recv(node), NODE_FILE)) &&
9046 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
9047 get_nd_args(node) == NULL &&
9048 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9049 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
9050 VALUE str = get_string_value(get_nd_recv(node));
9051 if (get_node_call_nd_mid(node) == idUMinus) {
9052 ADD_INSN2(ret, line_node, opt_str_uminus, str,
9053 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
9056 ADD_INSN2(ret, line_node, opt_str_freeze, str,
9057 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
9061 ADD_INSN(ret, line_node, pop);
9069iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
9071 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
9075iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
9078 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
9079 for (i=0; table[i].index != -1; i++) {
9080 if (strcmp(table[i].name, name) == 0) {
9088iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
9090 const char *name = rb_id2name(mid);
9091 static const char prefix[] =
"__builtin_";
9092 const size_t prefix_len =
sizeof(prefix) - 1;
9097 switch (nd_type(recv)) {
9099 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
9104 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
9114 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
9115 return &name[prefix_len];
9124delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
9131 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
9132 unsigned int start=0;
9137 argc + start <= ISEQ_BODY(iseq)->local_table_size;
9139 const LINK_ELEMENT *elem = FIRST_ELEMENT(args);
9141 for (
unsigned int i=start; i-start<argc; i++) {
9142 if (IS_INSN(elem) &&
9143 INSN_OF(elem) == BIN(getlocal)) {
9144 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
9145 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
9147 if (local_level == 0) {
9148 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
9150 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
9151 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
9152 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
9153 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9177 *pstart_index = start;
9187compile_builtin_attr(rb_iseq_t *iseq,
const NODE *node)
9191 if (!node)
goto no_arg;
9193 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9194 const NODE *next = RNODE_LIST(node)->nd_next;
9196 node = RNODE_LIST(node)->nd_head;
9197 if (!node)
goto no_arg;
9198 switch (nd_type(node)) {
9200 symbol = rb_node_sym_string_val(node);
9206 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9209 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
9210 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9212 else if (strcmp(RSTRING_PTR(
string),
"inline_block") == 0) {
9213 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9215 else if (strcmp(RSTRING_PTR(
string),
"use_block") == 0) {
9216 iseq_set_use_block(iseq);
9218 else if (strcmp(RSTRING_PTR(
string),
"c_trace") == 0) {
9220 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9229 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9232 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9235 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %s", RSTRING_PTR(
string));
9238 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9242compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
const NODE *line_node,
int popped)
9246 if (!node)
goto no_arg;
9247 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9248 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9249 node = RNODE_LIST(node)->nd_head;
9250 if (!node)
goto no_arg;
9251 switch (nd_type(node)) {
9253 name = rb_node_sym_string_val(node);
9258 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9260 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9264 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9267 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9270 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9271 rb_builtin_class_name(name));
9274 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9278mandatory_node(
const rb_iseq_t *iseq,
const NODE *cond_node)
9280 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9281 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9282 return RNODE_IF(node)->nd_body;
9285 rb_bug(
"mandatory_node: can't find mandatory node");
9290compile_builtin_mandatory_only_method(rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9294 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9296 rb_node_args_t args_node;
9297 rb_node_init(RNODE(&args_node), NODE_ARGS);
9298 args_node.nd_ainfo = args;
9301 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9302 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9305 rb_ast_id_table_t *tbl =
ALLOCV(idtmp,
sizeof(rb_ast_id_table_t) + table_size *
sizeof(
ID));
9306 tbl->size = table_size;
9311 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9312 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9315 for (; i<table_size; i++) {
9316 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9319 rb_node_scope_t scope_node;
9320 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9321 scope_node.nd_tbl = tbl;
9322 scope_node.nd_body = mandatory_node(iseq, node);
9323 scope_node.nd_parent = NULL;
9324 scope_node.nd_args = &args_node;
9326 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9328 const rb_iseq_t *mandatory_only_iseq =
9329 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9330 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9331 nd_line(line_node), NULL, 0,
9332 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9333 ISEQ_BODY(iseq)->variable.script_lines);
9334 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
9341compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
const NODE *line_node,
int popped,
9342 const rb_iseq_t *parent_block, LINK_ANCHOR *args,
const char *builtin_func)
9344 NODE *args_node = get_nd_args(node);
9346 if (parent_block != NULL) {
9347 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9351# define BUILTIN_INLINE_PREFIX "_bi"
9352 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9353 bool cconst =
false;
9358 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9359 strcmp(
"cexpr!", builtin_func) == 0) {
9362 else if (strcmp(
"cconst!", builtin_func) == 0) {
9365 else if (strcmp(
"cinit!", builtin_func) == 0) {
9369 else if (strcmp(
"attr!", builtin_func) == 0) {
9370 return compile_builtin_attr(iseq, args_node);
9372 else if (strcmp(
"arg!", builtin_func) == 0) {
9373 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9375 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9377 rb_bug(
"mandatory_only? should be in if condition");
9379 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9380 rb_bug(
"mandatory_only? should be put on top");
9383 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9384 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9387 rb_bug(
"can't find builtin function:%s", builtin_func);
9390 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9394 int inline_index = nd_line(node);
9395 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9396 builtin_func = inline_func;
9402 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9403 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9404 ADD_INSN1(ret, line_node, putobject, const_val);
9410 unsigned int flag = 0;
9412 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9414 if (
FIX2INT(argc) != bf->argc) {
9415 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9416 builtin_func, bf->argc,
FIX2INT(argc));
9420 unsigned int start_index;
9421 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9422 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9426 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9429 if (popped) ADD_INSN(ret, line_node, pop);
9435compile_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)
9443 ID mid = get_node_call_nd_mid(node);
9445 unsigned int flag = 0;
9447 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9448 LABEL *else_label = NULL;
9451 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9457 if (nd_type_p(node, NODE_VCALL)) {
9462 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9464 if (mid == id_bitblt) {
9465 ADD_INSN(ret, line_node, bitblt);
9468 else if (mid == id_answer) {
9469 ADD_INSN(ret, line_node, answer);
9481 if (nd_type_p(node, NODE_FCALL) &&
9482 (mid == goto_id || mid == label_id)) {
9485 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9488 if (!labels_table) {
9489 labels_table = st_init_numtable();
9490 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9493 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9497 if (mid == goto_id) {
9498 ADD_INSNL(ret, line_node, jump, label);
9501 ADD_LABEL(ret, label);
9508 const char *builtin_func;
9509 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9510 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9511 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9515 if (!assume_receiver) {
9516 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9519 if (mid == idCall &&
9520 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9521 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9522 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9524 else if (private_recv_p(node)) {
9525 ADD_INSN(recv, node, putself);
9526 flag |= VM_CALL_FCALL;
9529 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9532 if (
type == NODE_QCALL) {
9533 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9536 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9537 ADD_CALL_RECEIVER(recv, line_node);
9542 if (
type != NODE_VCALL) {
9543 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9544 CHECK(!
NIL_P(argc));
9552 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
9553 mid == rb_intern(
"new") &&
9554 parent_block == NULL &&
9555 !(flag & VM_CALL_ARGS_BLOCKARG);
9558 ADD_INSN(ret, node, putnil);
9559 ADD_INSN(ret, node, swap);
9564 debugp_param(
"call args argc", argc);
9565 debugp_param(
"call method",
ID2SYM(mid));
9567 switch ((
int)
type) {
9569 flag |= VM_CALL_VCALL;
9572 flag |= VM_CALL_FCALL;
9575 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9576 ADD_INSN(ret, line_node, splatkw);
9579 LABEL *not_basic_new = NEW_LABEL(nd_line(node));
9580 LABEL *not_basic_new_finish = NEW_LABEL(nd_line(node));
9585 if (flag & VM_CALL_FORWARDING) {
9586 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc) + 1, flag, keywords, 0);
9589 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc), flag, keywords, 0);
9591 ADD_INSN2(ret, node, opt_new, ci, not_basic_new);
9592 LABEL_REF(not_basic_new);
9595 ADD_SEND_R(ret, line_node, rb_intern(
"initialize"), argc, parent_block,
INT2FIX(flag | VM_CALL_FCALL), keywords);
9596 ADD_INSNL(ret, line_node, jump, not_basic_new_finish);
9598 ADD_LABEL(ret, not_basic_new);
9600 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9601 ADD_INSN(ret, line_node, swap);
9603 ADD_LABEL(ret, not_basic_new_finish);
9604 ADD_INSN(ret, line_node, pop);
9607 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9610 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9612 ADD_INSN(ret, line_node, pop);
9618compile_op_asgn1(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9620 const int line = nd_line(node);
9622 unsigned int flag = 0;
9624 ID id = RNODE_OP_ASGN1(node)->nd_mid;
9650 ADD_INSN(ret, node, putnil);
9652 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
9653 CHECK(asgnflag != -1);
9654 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
9659 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
9660 CHECK(!
NIL_P(argc));
9662 int dup_argn =
FIX2INT(argc) + 1;
9663 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
9665 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
9667 if (
id == idOROP ||
id == idANDOP) {
9676 LABEL *label = NEW_LABEL(line);
9677 LABEL *lfin = NEW_LABEL(line);
9679 ADD_INSN(ret, node, dup);
9681 ADD_INSNL(ret, node, branchif, label);
9684 ADD_INSNL(ret, node, branchunless, label);
9686 ADD_INSN(ret, node, pop);
9688 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9690 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9692 if (flag & VM_CALL_ARGS_SPLAT) {
9693 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9694 ADD_INSN(ret, node, swap);
9695 ADD_INSN1(ret, node, splatarray,
Qtrue);
9696 ADD_INSN(ret, node, swap);
9697 flag |= VM_CALL_ARGS_SPLAT_MUT;
9699 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9700 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9703 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9705 ADD_INSN(ret, node, pop);
9706 ADD_INSNL(ret, node, jump, lfin);
9707 ADD_LABEL(ret, label);
9709 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9711 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
9712 ADD_LABEL(ret, lfin);
9715 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9716 ADD_SEND(ret, node,
id,
INT2FIX(1));
9718 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9720 if (flag & VM_CALL_ARGS_SPLAT) {
9721 if (flag & VM_CALL_KW_SPLAT) {
9722 ADD_INSN1(ret, node, topn,
INT2FIX(2));
9723 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9724 ADD_INSN1(ret, node, splatarray,
Qtrue);
9725 flag |= VM_CALL_ARGS_SPLAT_MUT;
9727 ADD_INSN(ret, node, swap);
9728 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9729 ADD_INSN1(ret, node, setn,
INT2FIX(2));
9730 ADD_INSN(ret, node, pop);
9733 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9734 ADD_INSN(ret, node, swap);
9735 ADD_INSN1(ret, node, splatarray,
Qtrue);
9736 ADD_INSN(ret, node, swap);
9737 flag |= VM_CALL_ARGS_SPLAT_MUT;
9739 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9741 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9744 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9746 ADD_INSN(ret, node, pop);
9752compile_op_asgn2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9754 const int line = nd_line(node);
9755 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
9756 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
9758 LABEL *lfin = NEW_LABEL(line);
9759 LABEL *lcfin = NEW_LABEL(line);
9814 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
9815 CHECK(asgnflag != -1);
9816 if (RNODE_OP_ASGN2(node)->nd_aid) {
9817 lskip = NEW_LABEL(line);
9818 ADD_INSN(ret, node, dup);
9819 ADD_INSNL(ret, node, branchnil, lskip);
9821 ADD_INSN(ret, node, dup);
9822 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
9824 if (atype == idOROP || atype == idANDOP) {
9826 ADD_INSN(ret, node, dup);
9828 if (atype == idOROP) {
9829 ADD_INSNL(ret, node, branchif, lcfin);
9832 ADD_INSNL(ret, node, branchunless, lcfin);
9835 ADD_INSN(ret, node, pop);
9837 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9839 ADD_INSN(ret, node, swap);
9840 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9842 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9843 ADD_INSNL(ret, node, jump, lfin);
9845 ADD_LABEL(ret, lcfin);
9847 ADD_INSN(ret, node, swap);
9850 ADD_LABEL(ret, lfin);
9853 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9854 ADD_SEND(ret, node, atype,
INT2FIX(1));
9856 ADD_INSN(ret, node, swap);
9857 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9859 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9861 if (lskip && popped) {
9862 ADD_LABEL(ret, lskip);
9864 ADD_INSN(ret, node, pop);
9865 if (lskip && !popped) {
9866 ADD_LABEL(ret, lskip);
9871static int compile_shareable_constant_value(rb_iseq_t *iseq, LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
9874compile_op_cdecl(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9876 const int line = nd_line(node);
9881 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
9883 ADD_INSN1(ret, node, putobject, rb_cObject);
9886 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
9889 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
9890 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
9893 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
9895 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
9896 lassign = NEW_LABEL(line);
9897 ADD_INSN(ret, node, dup);
9898 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
9900 ADD_INSNL(ret, node, branchunless, lassign);
9902 ADD_INSN(ret, node, dup);
9903 ADD_INSN1(ret, node, putobject,
Qtrue);
9904 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
9906 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
9907 lfin = NEW_LABEL(line);
9908 if (!popped) ADD_INSN(ret, node, dup);
9909 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
9910 ADD_INSNL(ret, node, branchif, lfin);
9912 ADD_INSNL(ret, node, branchunless, lfin);
9914 if (!popped) ADD_INSN(ret, node, pop);
9915 if (lassign) ADD_LABEL(ret, lassign);
9916 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9919 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9921 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
9922 ADD_INSN(ret, node, swap);
9924 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9925 ADD_LABEL(ret, lfin);
9926 if (!popped) ADD_INSN(ret, node, swap);
9927 ADD_INSN(ret, node, pop);
9930 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9932 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
9934 ADD_INSN(ret, node, swap);
9936 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9937 ADD_INSN(ret, node, swap);
9939 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9945compile_op_log(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
9947 const int line = nd_line(node);
9948 LABEL *lfin = NEW_LABEL(line);
9951 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
9955 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
9956 lassign = lfinish[1];
9958 lassign = NEW_LABEL(line);
9960 ADD_INSNL(ret, node, branchunless, lassign);
9963 lassign = NEW_LABEL(line);
9966 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
9969 ADD_INSN(ret, node, dup);
9972 if (
type == NODE_OP_ASGN_AND) {
9973 ADD_INSNL(ret, node, branchunless, lfin);
9976 ADD_INSNL(ret, node, branchif, lfin);
9980 ADD_INSN(ret, node, pop);
9983 ADD_LABEL(ret, lassign);
9984 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
9985 ADD_LABEL(ret, lfin);
9990compile_super(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
9995 unsigned int flag = 0;
9997 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
10001 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
10003 if (
type == NODE_SUPER) {
10004 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
10005 CHECK(!
NIL_P(vargc));
10007 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
10008 ADD_INSN(args, node, splatkw);
10011 if (flag & VM_CALL_ARGS_BLOCKARG) {
10018 const rb_iseq_t *liseq = body->local_iseq;
10020 const struct rb_iseq_param_keyword *
const local_kwd = local_body->param.keyword;
10021 int lvar_level = get_lvar_level(iseq);
10023 argc = local_body->param.lead_num;
10026 for (i = 0; i < local_body->param.lead_num; i++) {
10027 int idx = local_body->local_table_size - i;
10028 ADD_GETLOCAL(args, node, idx, lvar_level);
10032 if (local_body->param.flags.forwardable) {
10033 flag |= VM_CALL_FORWARDING;
10034 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
10035 ADD_GETLOCAL(args, node, idx, lvar_level);
10038 if (local_body->param.flags.has_opt) {
10041 for (j = 0; j < local_body->param.opt_num; j++) {
10042 int idx = local_body->local_table_size - (i + j);
10043 ADD_GETLOCAL(args, node, idx, lvar_level);
10048 if (local_body->param.flags.has_rest) {
10050 int idx = local_body->local_table_size - local_body->param.rest_start;
10051 ADD_GETLOCAL(args, node, idx, lvar_level);
10052 ADD_INSN1(args, node, splatarray, RBOOL(local_body->param.flags.has_post));
10054 argc = local_body->param.rest_start + 1;
10055 flag |= VM_CALL_ARGS_SPLAT;
10057 if (local_body->param.flags.has_post) {
10059 int post_len = local_body->param.post_num;
10060 int post_start = local_body->param.post_start;
10062 if (local_body->param.flags.has_rest) {
10064 for (j=0; j<post_len; j++) {
10065 int idx = local_body->local_table_size - (post_start + j);
10066 ADD_GETLOCAL(args, node, idx, lvar_level);
10068 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
10069 flag |= VM_CALL_ARGS_SPLAT_MUT;
10074 for (j=0; j<post_len; j++) {
10075 int idx = local_body->local_table_size - (post_start + j);
10076 ADD_GETLOCAL(args, node, idx, lvar_level);
10078 argc = post_len + post_start;
10082 if (local_body->param.flags.has_kw) {
10083 int local_size = local_body->local_table_size;
10086 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10088 if (local_body->param.flags.has_kwrest) {
10089 int idx = local_body->local_table_size - local_kwd->rest_start;
10090 ADD_GETLOCAL(args, node, idx, lvar_level);
10092 ADD_SEND (args, node, rb_intern(
"dup"),
INT2FIX(0));
10095 ADD_INSN1(args, node, newhash,
INT2FIX(0));
10097 for (i = 0; i < local_kwd->num; ++i) {
10098 ID id = local_kwd->table[i];
10099 int idx = local_size - get_local_var_idx(liseq,
id);
10100 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
10101 ADD_GETLOCAL(args, node, idx, lvar_level);
10103 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
10104 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
10106 else if (local_body->param.flags.has_kwrest) {
10107 int idx = local_body->local_table_size - local_kwd->rest_start;
10108 ADD_GETLOCAL(args, node, idx, lvar_level);
10110 flag |= VM_CALL_KW_SPLAT;
10114 if (use_block && parent_block == NULL) {
10115 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10118 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
10119 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
10120 ADD_INSN(ret, node, putself);
10121 ADD_SEQ(ret, args);
10123 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
10125 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
10126 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
10129 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
10133 ADD_INSN(ret, node, pop);
10139compile_yield(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10143 unsigned int flag = 0;
10148 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
10149 case ISEQ_TYPE_TOP:
10150 case ISEQ_TYPE_MAIN:
10151 case ISEQ_TYPE_CLASS:
10152 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
10157 if (RNODE_YIELD(node)->nd_head) {
10158 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
10159 CHECK(!
NIL_P(argc));
10165 ADD_SEQ(ret, args);
10166 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
10167 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10170 ADD_INSN(ret, node, pop);
10174 const rb_iseq_t *tmp_iseq = iseq;
10175 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
10176 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
10178 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
10184compile_match(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
10191 switch ((
int)
type) {
10194 VALUE re = rb_node_regx_string_val(node);
10195 RB_OBJ_SET_FROZEN_SHAREABLE(re);
10196 ADD_INSN1(recv, node, putobject, re);
10197 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
10202 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
10203 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
10206 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
10207 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
10211 ADD_SEQ(ret, recv);
10213 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
10215 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
10216 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
10220 ADD_INSN(ret, node, pop);
10226compile_colon2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10231 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10232 (segments = collect_const_segments(iseq, node))) {
10233 ISEQ_BODY(iseq)->ic_size++;
10234 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10244 CHECK(compile_const_prefix(iseq, node, pref, body));
10245 if (LIST_INSN_SIZE_ZERO(pref)) {
10246 ADD_INSN(ret, node, putnil);
10247 ADD_SEQ(ret, body);
10250 ADD_SEQ(ret, pref);
10251 ADD_SEQ(ret, body);
10257 ADD_CALL_RECEIVER(ret, node);
10258 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10259 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10262 ADD_INSN(ret, node, pop);
10268compile_colon3(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10270 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10273 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10274 ISEQ_BODY(iseq)->ic_size++;
10275 VALUE segments = rb_ary_new_from_args(2,
ID2SYM(idNULL),
ID2SYM(RNODE_COLON3(node)->nd_mid));
10276 RB_OBJ_SET_FROZEN_SHAREABLE(segments);
10277 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10281 ADD_INSN1(ret, node, putobject, rb_cObject);
10282 ADD_INSN1(ret, node, putobject,
Qtrue);
10283 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10287 ADD_INSN(ret, node, pop);
10293compile_dots(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const int excl)
10296 const NODE *b = RNODE_DOT2(node)->nd_beg;
10297 const NODE *e = RNODE_DOT2(node)->nd_end;
10299 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10301 VALUE bv = optimized_range_item(b);
10302 VALUE ev = optimized_range_item(e);
10305 ADD_INSN1(ret, node, putobject, val);
10310 CHECK(COMPILE_(ret,
"min", b, popped));
10311 CHECK(COMPILE_(ret,
"max", e, popped));
10313 ADD_INSN1(ret, node, newrange, flag);
10320compile_errinfo(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10323 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10324 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10327 const rb_iseq_t *ip = iseq;
10330 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10333 ip = ISEQ_BODY(ip)->parent_iseq;
10337 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10340 ADD_INSN(ret, node, putnil);
10348compile_kw_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10351 LABEL *end_label = NEW_LABEL(nd_line(node));
10352 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10354 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10356 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10359 else if (nd_type_p(default_value, NODE_SYM) ||
10360 nd_type_p(default_value, NODE_REGX) ||
10361 nd_type_p(default_value, NODE_LINE) ||
10362 nd_type_p(default_value, NODE_INTEGER) ||
10363 nd_type_p(default_value, NODE_FLOAT) ||
10364 nd_type_p(default_value, NODE_RATIONAL) ||
10365 nd_type_p(default_value, NODE_IMAGINARY) ||
10366 nd_type_p(default_value, NODE_NIL) ||
10367 nd_type_p(default_value, NODE_TRUE) ||
10368 nd_type_p(default_value, NODE_FALSE)) {
10369 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10377 int kw_bits_idx = body->local_table_size - body->param.keyword->bits_start;
10378 int keyword_idx = body->param.keyword->num;
10380 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10381 ADD_INSNL(ret, node, branchif, end_label);
10382 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10383 ADD_LABEL(ret, end_label);
10389compile_attrasgn(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10393 unsigned int flag = 0;
10394 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10396 LABEL *else_label = NULL;
10401 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10402 CHECK(!
NIL_P(argc));
10404 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10405 CHECK(asgnflag != -1);
10406 flag |= (
unsigned int)asgnflag;
10408 debugp_param(
"argc", argc);
10409 debugp_param(
"nd_mid",
ID2SYM(mid));
10413 mid = rb_id_attrset(mid);
10414 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10417 ADD_INSN(ret, node, putnil);
10418 ADD_SEQ(ret, recv);
10419 ADD_SEQ(ret, args);
10421 if (flag & VM_CALL_ARGS_SPLAT) {
10422 ADD_INSN(ret, node, dup);
10423 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10424 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10425 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10426 ADD_INSN (ret, node, pop);
10429 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10433 ADD_SEQ(ret, recv);
10434 ADD_SEQ(ret, args);
10436 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10437 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10438 ADD_INSN(ret, node, pop);
10443compile_make_shareable_node(rb_iseq_t *iseq, LINK_ANCHOR *ret, LINK_ANCHOR *sub,
const NODE *value,
bool copy)
10445 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10453 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10460 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10467node_const_decl_val(
const NODE *node)
10470 switch (nd_type(node)) {
10472 if (RNODE_CDECL(node)->nd_vid) {
10473 path = rb_id2str(RNODE_CDECL(node)->nd_vid);
10477 node = RNODE_CDECL(node)->nd_else;
10485 rb_str_append(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10488 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10494 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10495 rb_ary_push(path, rb_id2str(RNODE_COLON2(node)->nd_mid));
10497 if (node && nd_type_p(node, NODE_CONST)) {
10499 rb_ary_push(path, rb_id2str(RNODE_CONST(node)->nd_vid));
10501 else if (node && nd_type_p(node, NODE_COLON3)) {
10503 rb_ary_push(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10513 path = rb_fstring(path);
10518const_decl_path(NODE *dest)
10521 if (!nd_type_p(dest, NODE_CALL)) {
10522 path = node_const_decl_val(dest);
10528compile_ensure_shareable_node(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE *dest,
const NODE *value)
10533 VALUE path = const_decl_path(dest);
10534 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10535 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10536 ADD_INSN1(ret, value, putobject, path);
10538 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10543#ifndef SHAREABLE_BARE_EXPRESSION
10544#define SHAREABLE_BARE_EXPRESSION 1
10548compile_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)
10550# define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10551 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10553 DECL_ANCHOR(anchor);
10555 enum node_type
type = node ? nd_type(node) : NODE_NIL;
10567 *value_p = rb_node_sym_string_val(node);
10570 *value_p = rb_node_regx_string_val(node);
10573 *value_p = rb_node_line_lineno_val(node);
10576 *value_p = rb_node_integer_literal_val(node);
10579 *value_p = rb_node_float_literal_val(node);
10581 case NODE_RATIONAL:
10582 *value_p = rb_node_rational_literal_val(node);
10584 case NODE_IMAGINARY:
10585 *value_p = rb_node_imaginary_literal_val(node);
10587 case NODE_ENCODING:
10588 *value_p = rb_node_encoding_val(node);
10591 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10592 *shareable_literal_p = 1;
10596 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10597 if (shareable == rb_parser_shareable_literal) {
10603 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10606 *shareable_literal_p = 1;
10610 VALUE lit = rb_node_str_string_val(node);
10611 ADD_INSN1(ret, node, putobject, lit);
10614 *shareable_literal_p = 1;
10620 VALUE lit = rb_node_file_path_val(node);
10621 ADD_INSN1(ret, node, putobject, lit);
10624 *shareable_literal_p = 1;
10632 ADD_INSN1(ret, node, putobject, lit);
10635 *shareable_literal_p = 1;
10641 INIT_ANCHOR(anchor);
10643 for (NODE *n = (NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
10645 int shareable_literal_p2;
10646 NODE *elt = RNODE_LIST(n)->nd_head;
10648 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
10649 if (shareable_literal_p2) {
10652 else if (
RTEST(lit)) {
10658 if (!UNDEF_P(val)) {
10670 if (!RNODE_HASH(node)->nd_brace) {
10672 *shareable_literal_p = 0;
10675 for (NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10676 if (!RNODE_LIST(n)->nd_head) {
10678 goto compile_shareable;
10682 INIT_ANCHOR(anchor);
10684 for (NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10686 VALUE value_val = 0;
10687 int shareable_literal_p2;
10688 NODE *key = RNODE_LIST(n)->nd_head;
10689 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
10690 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10691 if (shareable_literal_p2) {
10694 else if (
RTEST(lit)) {
10695 rb_hash_clear(lit);
10698 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10699 if (shareable_literal_p2) {
10702 else if (
RTEST(lit)) {
10703 rb_hash_clear(lit);
10707 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
10708 rb_hash_aset(lit, key_val, value_val);
10711 rb_hash_clear(lit);
10722 if (shareable == rb_parser_shareable_literal &&
10723 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
10724 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
10726 *shareable_literal_p = 1;
10729 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10731 *shareable_literal_p = 0;
10737 if (nd_type(node) == NODE_LIST) {
10738 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10740 else if (nd_type(node) == NODE_HASH) {
10741 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10742 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10745 *shareable_literal_p = 0;
10746 ADD_SEQ(ret, anchor);
10752 if (nd_type(node) == NODE_LIST) {
10753 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10755 else if (nd_type(node) == NODE_HASH) {
10756 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10757 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10759 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
10761 *shareable_literal_p = 1;
10765 ADD_INSN1(ret, node, putobject, val);
10768 *shareable_literal_p = 1;
10775compile_shareable_constant_value(rb_iseq_t *iseq, LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
10779 DECL_ANCHOR(anchor);
10780 INIT_ANCHOR(anchor);
10782 switch (shareable) {
10783 case rb_parser_shareable_none:
10784 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
10787 case rb_parser_shareable_literal:
10788 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (NODE *)lhs, value, 0, &val, &literal_p));
10789 ADD_SEQ(ret, anchor);
10792 case rb_parser_shareable_copy:
10793 case rb_parser_shareable_everything:
10794 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (NODE *)lhs, value, 0, &val, &literal_p));
10796 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
10799 ADD_SEQ(ret, anchor);
10803 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
10807static int iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped);
10816iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *node,
int popped)
10820 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
10821 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
10822 debugs(
"node: NODE_NIL(implicit)\n");
10823 ADD_SYNTHETIC_INSN(ret, lineno, -1, putnil);
10827 return iseq_compile_each0(iseq, ret, node, popped);
10831iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10833 const int line = (int)nd_line(node);
10834 const enum node_type
type = nd_type(node);
10837 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
10841 if (nd_fl_newline(node)) {
10843 ISEQ_COMPILE_DATA(iseq)->last_line = line;
10844 if (line > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
10845 event |= RUBY_EVENT_COVERAGE_LINE;
10847 ADD_TRACE(ret, event);
10851 debug_node_start(node);
10852#undef BEFORE_RETURN
10853#define BEFORE_RETURN debug_node_end()
10857 CHECK(compile_block(iseq, ret, node, popped));
10861 CHECK(compile_if(iseq, ret, node, popped,
type));
10864 CHECK(compile_case(iseq, ret, node, popped));
10867 CHECK(compile_case2(iseq, ret, node, popped));
10870 CHECK(compile_case3(iseq, ret, node, popped));
10874 CHECK(compile_loop(iseq, ret, node, popped,
type));
10878 CHECK(compile_iter(iseq, ret, node, popped));
10880 case NODE_FOR_MASGN:
10881 CHECK(compile_for_masgn(iseq, ret, node, popped));
10884 CHECK(compile_break(iseq, ret, node, popped));
10887 CHECK(compile_next(iseq, ret, node, popped));
10890 CHECK(compile_redo(iseq, ret, node, popped));
10893 CHECK(compile_retry(iseq, ret, node, popped));
10896 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
10900 CHECK(compile_rescue(iseq, ret, node, popped));
10903 CHECK(compile_resbody(iseq, ret, node, popped));
10906 CHECK(compile_ensure(iseq, ret, node, popped));
10911 LABEL *end_label = NEW_LABEL(line);
10912 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(node)->nd_1st));
10914 ADD_INSN(ret, node, dup);
10916 if (
type == NODE_AND) {
10917 ADD_INSNL(ret, node, branchunless, end_label);
10920 ADD_INSNL(ret, node, branchif, end_label);
10923 ADD_INSN(ret, node, pop);
10925 CHECK(COMPILE_(ret,
"nd_2nd", RNODE_OR(node)->nd_2nd, popped));
10926 ADD_LABEL(ret, end_label);
10931 bool prev_in_masgn = ISEQ_COMPILE_DATA(iseq)->in_masgn;
10932 ISEQ_COMPILE_DATA(iseq)->in_masgn =
true;
10933 compile_massign(iseq, ret, node, popped);
10934 ISEQ_COMPILE_DATA(iseq)->in_masgn = prev_in_masgn;
10939 ID id = RNODE_LASGN(node)->nd_vid;
10940 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
10942 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
10943 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
10946 ADD_INSN(ret, node, dup);
10948 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
10953 ID id = RNODE_DASGN(node)->nd_vid;
10954 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
10955 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
10958 ADD_INSN(ret, node, dup);
10961 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
10964 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
10968 ADD_SETLOCAL(ret, node, ls - idx, lv);
10972 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
10975 ADD_INSN(ret, node, dup);
10977 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
10981 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
10983 ADD_INSN(ret, node, dup);
10985 ADD_INSN2(ret, node, setinstancevariable,
10986 ID2SYM(RNODE_IASGN(node)->nd_vid),
10987 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
10991 if (RNODE_CDECL(node)->nd_vid) {
10992 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
10995 ADD_INSN(ret, node, dup);
10998 ADD_INSN1(ret, node, putspecialobject,
10999 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
11000 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
11003 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
11004 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11005 ADD_INSN(ret, node, swap);
11008 ADD_INSN1(ret, node, topn,
INT2FIX(1));
11009 ADD_INSN(ret, node, swap);
11012 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
11017 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
11019 ADD_INSN(ret, node, dup);
11021 ADD_INSN2(ret, node, setclassvariable,
11022 ID2SYM(RNODE_CVASGN(node)->nd_vid),
11023 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
11026 case NODE_OP_ASGN1:
11027 CHECK(compile_op_asgn1(iseq, ret, node, popped));
11029 case NODE_OP_ASGN2:
11030 CHECK(compile_op_asgn2(iseq, ret, node, popped));
11032 case NODE_OP_CDECL:
11033 CHECK(compile_op_cdecl(iseq, ret, node, popped));
11035 case NODE_OP_ASGN_AND:
11036 case NODE_OP_ASGN_OR:
11037 CHECK(compile_op_log(iseq, ret, node, popped,
type));
11041 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
11047 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
11053 CHECK(compile_super(iseq, ret, node, popped,
type));
11056 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
11061 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
11066 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
11069 CHECK(compile_return(iseq, ret, node, popped));
11072 CHECK(compile_yield(iseq, ret, node, popped));
11076 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
11082 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
11084 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
11086 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
11087 rb_id2str(RNODE_DVAR(node)->nd_vid));
11090 ADD_GETLOCAL(ret, node, ls - idx, lv);
11095 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
11097 ADD_INSN(ret, node, pop);
11102 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
11104 ADD_INSN2(ret, node, getinstancevariable,
11105 ID2SYM(RNODE_IVAR(node)->nd_vid),
11106 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
11111 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
11113 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
11115 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(RNODE_CONST(node)->nd_vid));
11116 RB_OBJ_SET_FROZEN_SHAREABLE(segments);
11117 ADD_INSN1(ret, node, opt_getconstant_path, segments);
11121 ADD_INSN(ret, node, putnil);
11122 ADD_INSN1(ret, node, putobject,
Qtrue);
11123 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
11127 ADD_INSN(ret, node, pop);
11133 ADD_INSN2(ret, node, getclassvariable,
11134 ID2SYM(RNODE_CVAR(node)->nd_vid),
11135 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
11139 case NODE_NTH_REF:{
11141 if (!RNODE_NTH_REF(node)->nd_nth) {
11142 ADD_INSN(ret, node, putnil);
11145 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11146 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
11150 case NODE_BACK_REF:{
11152 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11153 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
11160 CHECK(compile_match(iseq, ret, node, popped,
type));
11164 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
11170 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
11174 case NODE_ENCODING:{
11176 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
11180 case NODE_INTEGER:{
11181 VALUE lit = rb_node_integer_literal_val(node);
11183 debugp_param(
"integer", lit);
11185 ADD_INSN1(ret, node, putobject, lit);
11191 VALUE lit = rb_node_float_literal_val(node);
11193 debugp_param(
"float", lit);
11195 ADD_INSN1(ret, node, putobject, lit);
11200 case NODE_RATIONAL:{
11201 VALUE lit = rb_node_rational_literal_val(node);
11203 debugp_param(
"rational", lit);
11205 ADD_INSN1(ret, node, putobject, lit);
11210 case NODE_IMAGINARY:{
11211 VALUE lit = rb_node_imaginary_literal_val(node);
11213 debugp_param(
"imaginary", lit);
11215 ADD_INSN1(ret, node, putobject, lit);
11222 debugp_param(
"nd_lit", get_string_value(node));
11224 VALUE lit = get_string_value(node);
11225 const rb_compile_option_t *option = ISEQ_COMPILE_DATA(iseq)->option;
11227 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11228 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11229 RB_OBJ_SET_SHAREABLE(lit);
11231 switch (option->frozen_string_literal) {
11232 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11233 ADD_INSN1(ret, node, putchilledstring, lit);
11235 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11236 ADD_INSN1(ret, node, putstring, lit);
11238 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11239 ADD_INSN1(ret, node, putobject, lit);
11242 rb_bug(
"invalid frozen_string_literal");
11249 compile_dstr(iseq, ret, node);
11252 ADD_INSN(ret, node, pop);
11257 ADD_CALL_RECEIVER(ret, node);
11258 VALUE str = rb_node_str_string_val(node);
11259 ADD_INSN1(ret, node, putobject, str);
11261 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11264 ADD_INSN(ret, node, pop);
11269 ADD_CALL_RECEIVER(ret, node);
11270 compile_dstr(iseq, ret, node);
11271 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11274 ADD_INSN(ret, node, pop);
11279 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11283 VALUE lit = rb_node_regx_string_val(node);
11284 RB_OBJ_SET_SHAREABLE(lit);
11285 ADD_INSN1(ret, node, putobject, lit);
11291 compile_dregx(iseq, ret, node, popped);
11294 int ic_index = body->ise_size++;
11295 const rb_iseq_t *block_iseq;
11296 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11298 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
11302 ADD_INSN(ret, node, pop);
11306 case NODE_ARGSCAT:{
11308 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11309 ADD_INSN1(ret, node, splatarray,
Qfalse);
11310 ADD_INSN(ret, node, pop);
11311 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11312 ADD_INSN1(ret, node, splatarray,
Qfalse);
11313 ADD_INSN(ret, node, pop);
11316 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11317 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11318 if (nd_type_p(body_node, NODE_LIST)) {
11319 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11322 CHECK(COMPILE(ret,
"argscat body", body_node));
11323 ADD_INSN(ret, node, concattoarray);
11328 case NODE_ARGSPUSH:{
11330 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11331 ADD_INSN1(ret, node, splatarray,
Qfalse);
11332 ADD_INSN(ret, node, pop);
11333 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11336 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11337 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11338 if (keyword_node_p(body_node)) {
11339 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11340 ADD_INSN(ret, node, pushtoarraykwsplat);
11342 else if (static_literal_node_p(body_node, iseq,
false)) {
11343 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11344 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11347 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11348 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11354 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11355 ADD_INSN1(ret, node, splatarray,
Qtrue);
11358 ADD_INSN(ret, node, pop);
11363 ID mid = RNODE_DEFN(node)->nd_mid;
11364 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11366 ISEQ_TYPE_METHOD, line);
11368 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11369 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11373 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11379 ID mid = RNODE_DEFS(node)->nd_mid;
11380 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11382 ISEQ_TYPE_METHOD, line);
11384 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11385 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11386 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11390 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11395 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11396 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11397 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11398 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11399 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11402 ADD_INSN(ret, node, pop);
11407 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11408 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11409 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11410 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11413 ADD_INSN(ret, node, pop);
11418 const rb_parser_ary_t *ary = RNODE_UNDEF(node)->nd_undefs;
11420 for (
long i = 0; i < ary->len; i++) {
11421 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11422 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11423 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11424 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11426 if (i < ary->
len - 1) {
11427 ADD_INSN(ret, node, pop);
11432 ADD_INSN(ret, node, pop);
11437 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11438 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
11439 ISEQ_TYPE_CLASS, line);
11440 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11441 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11442 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11444 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11445 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11449 ADD_INSN(ret, node, pop);
11454 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11455 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
11456 ISEQ_TYPE_CLASS, line);
11457 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11458 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11460 ADD_INSN (ret, node, putnil);
11461 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11465 ADD_INSN(ret, node, pop);
11471 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11472 ISEQ_TYPE_CLASS, line);
11474 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11475 ADD_INSN (ret, node, putnil);
11476 CONST_ID(singletonclass,
"singletonclass");
11477 ADD_INSN3(ret, node, defineclass,
11478 ID2SYM(singletonclass), singleton_class,
11479 INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
11483 ADD_INSN(ret, node, pop);
11488 CHECK(compile_colon2(iseq, ret, node, popped));
11491 CHECK(compile_colon3(iseq, ret, node, popped));
11494 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11497 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11501 LABEL *lend = NEW_LABEL(line);
11502 LABEL *ltrue = NEW_LABEL(line);
11503 LABEL *lfalse = NEW_LABEL(line);
11504 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11506 ADD_LABEL(ret, ltrue);
11507 ADD_INSN1(ret, node, putobject,
Qtrue);
11508 ADD_INSNL(ret, node, jump, lend);
11509 ADD_LABEL(ret, lfalse);
11510 ADD_INSN1(ret, node, putobject,
Qfalse);
11511 ADD_LABEL(ret, lend);
11516 ADD_INSN(ret, node, putself);
11522 ADD_INSN(ret, node, putnil);
11528 ADD_INSN1(ret, node, putobject,
Qtrue);
11534 ADD_INSN1(ret, node, putobject,
Qfalse);
11539 CHECK(compile_errinfo(iseq, ret, node, popped));
11543 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11546 case NODE_POSTEXE:{
11550 int is_index = body->ise_size++;
11552 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11553 const rb_iseq_t *once_iseq =
11554 NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc, rb_fstring(make_name_for_block(iseq)), ISEQ_TYPE_BLOCK, line);
11556 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
11560 ADD_INSN(ret, node, pop);
11565 CHECK(compile_kw_arg(iseq, ret, node, popped));
11568 compile_dstr(iseq, ret, node);
11570 ADD_INSN(ret, node, intern);
11573 ADD_INSN(ret, node, pop);
11577 case NODE_ATTRASGN:
11578 CHECK(compile_attrasgn(iseq, ret, node, popped));
11582 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11585 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11586 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11590 ADD_INSN(ret, node, pop);
11595 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
11610insn_data_length(INSN *iobj)
11612 return insn_len(iobj->insn_id);
11616calc_sp_depth(
int depth, INSN *insn)
11618 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
11622opobj_inspect(
VALUE obj)
11642insn_data_to_s_detail(INSN *iobj)
11644 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
11646 if (iobj->operands) {
11647 const char *types = insn_op_types(iobj->insn_id);
11650 for (j = 0; types[j]; j++) {
11651 char type = types[j];
11656 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, j);
11657 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
11663 rb_iseq_t *iseq = (rb_iseq_t *)OPERAND_AT(iobj, j);
11675 VALUE v = OPERAND_AT(iobj, j);
11690 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11693 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11696 rb_str_catf(str,
"<ise:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11702 if (vm_ci_mid(ci)) rb_str_catf(str,
"%"PRIsVALUE, rb_id2str(vm_ci_mid(ci)));
11703 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
11711 void *func = (
void *)OPERAND_AT(iobj, j);
11714 if (dladdr(func, &info) && info.dli_sname) {
11719 rb_str_catf(str,
"<%p>", func);
11729 if (types[j + 1]) {
11738dump_disasm_list(
const LINK_ELEMENT *link)
11740 dump_disasm_list_with_cursor(link, NULL, NULL);
11744dump_disasm_list_with_cursor(
const LINK_ELEMENT *link,
const LINK_ELEMENT *curr,
const LABEL *dest)
11751 printf(
"-- raw disasm--------\n");
11754 if (curr) printf(curr == link ?
"*" :
" ");
11755 switch (link->type) {
11756 case ISEQ_ELEMENT_INSN:
11758 iobj = (INSN *)link;
11759 str = insn_data_to_s_detail(iobj);
11760 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
11761 pos += insn_data_length(iobj);
11764 case ISEQ_ELEMENT_LABEL:
11766 lobj = (LABEL *)link;
11767 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
11768 dest == lobj ?
" <---" :
"");
11771 case ISEQ_ELEMENT_TRACE:
11773 TRACE *trace = (TRACE *)link;
11774 printf(
" trace: %0x\n", trace->event);
11777 case ISEQ_ELEMENT_ADJUST:
11779 ADJUST *adjust = (ADJUST *)link;
11780 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
11785 rb_raise(
rb_eSyntaxError,
"dump_disasm_list error: %d\n", (
int)link->type);
11789 printf(
"---------------------\n");
11794rb_insn_len(
VALUE insn)
11796 return insn_len(insn);
11800rb_insns_name(
int i)
11802 return insn_name(i);
11806rb_insns_name_array(
void)
11810 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
11817register_label(rb_iseq_t *iseq,
struct st_table *labels_table,
VALUE obj)
11821 obj = rb_to_symbol_type(obj);
11823 if (st_lookup(labels_table, obj, &tmp) == 0) {
11824 label = NEW_LABEL(0);
11825 st_insert(labels_table, obj, (st_data_t)label);
11828 label = (LABEL *)tmp;
11835get_exception_sym2type(
VALUE sym)
11837 static VALUE symRescue, symEnsure, symRetry;
11838 static VALUE symBreak, symRedo, symNext;
11840 if (symRescue == 0) {
11849 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
11850 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
11851 if (sym == symRetry)
return CATCH_TYPE_RETRY;
11852 if (sym == symBreak)
return CATCH_TYPE_BREAK;
11853 if (sym == symRedo)
return CATCH_TYPE_REDO;
11854 if (sym == symNext)
return CATCH_TYPE_NEXT;
11855 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
11860iseq_build_from_ary_exception(rb_iseq_t *iseq,
struct st_table *labels_table,
11866 const rb_iseq_t *eiseq;
11868 LABEL *lstart, *lend, *lcont;
11883 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
11884 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
11885 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
11889 if (
type == CATCH_TYPE_RESCUE ||
11890 type == CATCH_TYPE_BREAK ||
11891 type == CATCH_TYPE_NEXT) {
11897 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
11904static struct st_table *
11905insn_make_insn_table(
void)
11907 struct st_table *table;
11909 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
11911 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
11918static const rb_iseq_t *
11919iseq_build_load_iseq(
const rb_iseq_t *iseq,
VALUE op)
11922 const rb_iseq_t *loaded_iseq;
11925 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
11927 else if (
CLASS_OF(op) == rb_cISeq) {
11934 loaded_iseq = rb_iseqw_to_iseq(iseqw);
11935 return loaded_iseq;
11939iseq_build_callinfo_from_hash(rb_iseq_t *iseq,
VALUE op)
11943 unsigned int flag = 0;
11954 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
11956 if (!
NIL_P(vkw_arg)) {
11959 size_t n = rb_callinfo_kwarg_bytes(
len);
11962 kw_arg->references = 0;
11963 kw_arg->keyword_len =
len;
11964 for (i = 0; i <
len; i++) {
11967 kw_arg->keywords[i] = kw;
11972 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
11978event_name_to_flag(
VALUE sym)
11980#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
11994iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor,
12001 int line_no = 0, node_id = -1, insn_idx = 0;
12002 int ret = COMPILE_OK;
12007 static struct st_table *insn_table;
12009 if (insn_table == 0) {
12010 insn_table = insn_make_insn_table();
12013 for (i=0; i<
len; i++) {
12019 ADD_TRACE(anchor, event);
12022 LABEL *label = register_label(iseq, labels_table, obj);
12023 ADD_LABEL(anchor, label);
12040 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
12042 COMPILE_ERROR(iseq, line_no,
12043 "unknown instruction: %+"PRIsVALUE, insn);
12048 if (argc != insn_len((
VALUE)insn_id)-1) {
12049 COMPILE_ERROR(iseq, line_no,
12050 "operand size mismatch");
12056 argv = compile_data_calloc2(iseq,
sizeof(
VALUE), argc);
12060 (LINK_ELEMENT*)new_insn_core(iseq, line_no, node_id,
12061 (
enum ruby_vminsn_type)insn_id, argc, argv));
12063 for (j=0; j<argc; j++) {
12065 switch (insn_op_type((
VALUE)insn_id, j)) {
12067 LABEL *label = register_label(iseq, labels_table, op);
12068 argv[j] = (
VALUE)label;
12083 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
12094 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
12095 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
12101 op = rb_to_array_type(op);
12105 sym = rb_to_symbol_type(sym);
12110 argv[j] = segments;
12112 ISEQ_BODY(iseq)->ic_size++;
12117 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
12118 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
12123 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
12124 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
12128 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
12131 argv[j] = rb_to_symbol_type(op);
12138 RHASH_TBL_RAW(map)->type = &cdhash_type;
12139 op = rb_to_array_type(op);
12144 register_label(iseq, labels_table, sym);
12145 rb_hash_aset(map, key, (
VALUE)label | 1);
12155#if SIZEOF_VALUE <= SIZEOF_LONG
12160 argv[j] = (
VALUE)funcptr;
12170 (LINK_ELEMENT*)new_insn_core(iseq, line_no, node_id,
12171 (
enum ruby_vminsn_type)insn_id, argc, NULL));
12175 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
12180 validate_labels(iseq, labels_table);
12181 if (!ret)
return ret;
12182 return iseq_setup(iseq, anchor);
12185#define CHECK_ARRAY(v) rb_to_array_type(v)
12186#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
12191 VALUE val = rb_hash_aref(param, sym);
12196 else if (!
NIL_P(val)) {
12197 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
12203static const struct rb_iseq_param_keyword *
12204iseq_build_kw(rb_iseq_t *iseq,
VALUE params,
VALUE keywords)
12209 VALUE key, sym, default_val;
12212 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12214 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12216 keyword->num =
len;
12217#define SYM(s) ID2SYM(rb_intern_const(#s))
12218 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12219 i = keyword->bits_start - keyword->num;
12220 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12224 for (i = 0; i <
len; i++) {
12228 goto default_values;
12231 keyword->required_num++;
12235 default_len =
len - i;
12236 if (default_len == 0) {
12237 keyword->table = ids;
12240 else if (default_len < 0) {
12246 for (j = 0; i <
len; i++, j++) {
12260 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
12266 keyword->table = ids;
12267 keyword->default_values = dvs;
12273iseq_insn_each_object_mark_and_move(
VALUE * obj,
VALUE _)
12275 rb_gc_mark_and_move(obj);
12282 size_t size =
sizeof(INSN);
12283 unsigned int pos = 0;
12286#ifdef STRICT_ALIGNMENT
12287 size_t padding = calc_padding((
void *)&storage->buff[pos], size);
12289 const size_t padding = 0;
12291 size_t offset = pos + size + padding;
12292 if (offset > storage->size || offset > storage->pos) {
12294 storage = storage->next;
12297#ifdef STRICT_ALIGNMENT
12298 pos += (int)padding;
12301 iobj = (INSN *)&storage->buff[pos];
12303 if (iobj->operands) {
12304 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_move, (
VALUE)0);
12312 .wrap_struct_name =
"compiler/labels_wrapper",
12317 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
12321rb_iseq_build_from_ary(rb_iseq_t *iseq,
VALUE misc,
VALUE locals,
VALUE params,
12324#define SYM(s) ID2SYM(rb_intern_const(#s))
12326 unsigned int arg_size, local_size, stack_max;
12328 struct st_table *labels_table = st_init_numtable();
12330 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
12331 VALUE keywords = rb_hash_aref(params, SYM(keyword));
12333 DECL_ANCHOR(anchor);
12334 INIT_ANCHOR(anchor);
12337 ISEQ_BODY(iseq)->local_table_size =
len;
12338 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12340 for (i = 0; i <
len; i++) {
12343 if (sym_arg_rest == lv) {
12351#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12352 if (INT_PARAM(lead_num)) {
12353 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12355 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12356 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12357 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12358 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12361#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12363 INT_PARAM(arg_size);
12364 INT_PARAM(local_size);
12365 INT_PARAM(stack_max);
12370#ifdef USE_ISEQ_NODE_ID
12371 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
12379 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12381 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12384 for (i = 0; i <
len; i++) {
12386 LABEL *label = register_label(iseq, labels_table, ent);
12387 opt_table[i] = (
VALUE)label;
12390 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12391 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12394 else if (!
NIL_P(arg_opt_labels)) {
12395 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
12400 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12402 else if (!
NIL_P(keywords)) {
12403 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
12407 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
12408 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12411 if (
Qtrue == rb_hash_aref(params, SYM(use_block))) {
12412 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12415 if (int_param(&i, params, SYM(kwrest))) {
12416 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12417 if (keyword == NULL) {
12418 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12420 keyword->rest_start = i;
12421 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12424 iseq_calc_param_size(iseq);
12427 iseq_build_from_ary_exception(iseq, labels_table, exception);
12430 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12432 ISEQ_BODY(iseq)->param.size = arg_size;
12433 ISEQ_BODY(iseq)->local_table_size = local_size;
12434 ISEQ_BODY(iseq)->stack_max = stack_max;
12440rb_dvar_defined(
ID id,
const rb_iseq_t *iseq)
12444 while (body->type == ISEQ_TYPE_BLOCK ||
12445 body->type == ISEQ_TYPE_RESCUE ||
12446 body->type == ISEQ_TYPE_ENSURE ||
12447 body->type == ISEQ_TYPE_EVAL ||
12448 body->type == ISEQ_TYPE_MAIN
12452 for (i = 0; i < body->local_table_size; i++) {
12453 if (body->local_table[i] ==
id) {
12457 iseq = body->parent_iseq;
12458 body = ISEQ_BODY(iseq);
12465rb_local_defined(
ID id,
const rb_iseq_t *iseq)
12471 for (i=0; i<body->local_table_size; i++) {
12472 if (body->local_table[i] ==
id) {
12482#ifndef IBF_ISEQ_DEBUG
12483#define IBF_ISEQ_DEBUG 0
12486#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12487#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12490typedef uint32_t ibf_offset_t;
12491#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12493#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12495#define IBF_DEVEL_VERSION 5
12496#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12498#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12501static const char IBF_ENDIAN_MARK =
12502#ifdef WORDS_BIGENDIAN
12511 uint32_t major_version;
12512 uint32_t minor_version;
12514 uint32_t extra_size;
12516 uint32_t iseq_list_size;
12517 uint32_t global_object_list_size;
12518 ibf_offset_t iseq_list_offset;
12519 ibf_offset_t global_object_list_offset;
12540 unsigned int obj_list_size;
12541 ibf_offset_t obj_list_offset;
12560pinned_list_mark(
void *ptr)
12564 for (i = 0; i < list->size; i++) {
12565 if (list->buffer[i]) {
12566 rb_gc_mark(list->buffer[i]);
12578 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
12582pinned_list_fetch(
VALUE list,
long offset)
12588 if (offset >= ptr->size) {
12589 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12592 return ptr->buffer[offset];
12596pinned_list_store(
VALUE list,
long offset,
VALUE object)
12602 if (offset >= ptr->size) {
12603 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12610pinned_list_new(
long size)
12612 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
12613 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
12614 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
12620ibf_dump_pos(
struct ibf_dump *dump)
12622 long pos = RSTRING_LEN(dump->current_buffer->str);
12623#if SIZEOF_LONG > SIZEOF_INT
12624 if (pos >= UINT_MAX) {
12628 return (
unsigned int)pos;
12632ibf_dump_align(
struct ibf_dump *dump,
size_t align)
12634 ibf_offset_t pos = ibf_dump_pos(dump);
12636 static const char padding[
sizeof(
VALUE)];
12637 size_t size = align - ((size_t)pos % align);
12638#if SIZEOF_LONG > SIZEOF_INT
12639 if (pos + size >= UINT_MAX) {
12643 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
12644 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
12646 rb_str_cat(dump->current_buffer->str, padding, size);
12651ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
12653 ibf_offset_t pos = ibf_dump_pos(dump);
12654#if SIZEOF_LONG > SIZEOF_INT
12656 if (size >= UINT_MAX || pos + size >= UINT_MAX) {
12660 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
12665ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
12667 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
12671ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
12673 VALUE str = dump->current_buffer->str;
12674 char *ptr = RSTRING_PTR(str);
12675 if ((
unsigned long)(size + offset) > (
unsigned long)RSTRING_LEN(str))
12676 rb_bug(
"ibf_dump_overwrite: overflow");
12677 memcpy(ptr + offset, buff, size);
12681ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
12683 ibf_offset_t beg = *offset;
12685 return load->current_buffer->buff + beg;
12689ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
12691 void *buff = ruby_xmalloc2(x, y);
12692 size_t size = x * y;
12693 memcpy(buff, load->current_buffer->buff + offset, size);
12697#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
12699#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
12700#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
12701#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
12702#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
12703#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
12706ibf_table_lookup(
struct st_table *table, st_data_t key)
12710 if (st_lookup(table, key, &val)) {
12719ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
12721 int index = ibf_table_lookup(table, key);
12724 index = (int)table->num_entries;
12725 st_insert(table, key, (st_data_t)index);
12733static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
12736static rb_iseq_t *ibf_load_iseq(
const struct ibf_load *load,
const rb_iseq_t *index_iseq);
12739ibf_dump_object_table_new(
void)
12741 st_table *obj_table = st_init_numtable();
12742 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
12750 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
12756 if (
id == 0 || rb_id2name(
id) == NULL) {
12759 return ibf_dump_object(dump,
rb_id2sym(
id));
12763ibf_load_id(
const struct ibf_load *load,
const ID id_index)
12765 if (id_index == 0) {
12768 VALUE sym = ibf_load_object(load, id_index);
12778static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
12781ibf_dump_iseq(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
12783 if (iseq == NULL) {
12787 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
12791static unsigned char
12792ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
12794 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
12795 return (
unsigned char)load->current_buffer->buff[(*offset)++];
12811 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12812 ibf_dump_write(dump, &x,
sizeof(
VALUE));
12816 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12818 unsigned char bytes[max_byte_length];
12821 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
12822 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12828 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12831 ibf_dump_write(dump, bytes + max_byte_length - n, n);
12835ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
12837 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12838 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
12840 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
12841 *offset +=
sizeof(
VALUE);
12846 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12848 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
12849 const unsigned char c = buffer[*offset];
12853 c == 0 ? 9 : ntz_int32(c) + 1;
12856 if (*offset + n > load->current_buffer->size) {
12861 for (i = 1; i < n; i++) {
12863 x |= (
VALUE)buffer[*offset + i];
12877 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
12879 size_t len = strlen(bf->name);
12880 ibf_dump_write_small_value(dump, (
VALUE)
len);
12881 ibf_dump_write(dump, bf->name,
len);
12885ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
12887 int i = (int)ibf_load_small_value(load, offset);
12888 int len = (int)ibf_load_small_value(load, offset);
12889 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
12892 fprintf(stderr,
"%.*s!!\n",
len, name);
12896 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
12897 if (strncmp(table[i].name, name,
len) != 0) {
12898 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
12906ibf_dump_code(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
12909 const int iseq_size = body->iseq_size;
12911 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
12913 ibf_offset_t offset = ibf_dump_pos(dump);
12915 for (code_index=0; code_index<iseq_size;) {
12916 const VALUE insn = orig_code[code_index++];
12917 const char *types = insn_op_types(insn);
12922 ibf_dump_write_small_value(dump, insn);
12925 for (op_index=0; types[op_index]; op_index++, code_index++) {
12926 VALUE op = orig_code[code_index];
12929 switch (types[op_index]) {
12932 wv = ibf_dump_object(dump, op);
12935 wv = (
VALUE)ibf_dump_iseq(dump, (
const rb_iseq_t *)op);
12941 wv = ibf_dump_object(dump, arr);
12949 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
12957 wv = ibf_dump_id(dump, (
ID)op);
12969 ibf_dump_write_small_value(dump, wv);
12979ibf_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)
12982 unsigned int code_index;
12983 ibf_offset_t reading_pos = bytecode_offset;
12987 struct rb_call_data *cd_entries = load_body->call_data;
12990 load_body->iseq_encoded = code;
12991 load_body->iseq_size = 0;
12993 iseq_bits_t * mark_offset_bits;
12995 iseq_bits_t tmp[1] = {0};
12997 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
12998 mark_offset_bits = tmp;
13001 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
13003 bool needs_bitmap =
false;
13005 for (code_index=0; code_index<iseq_size;) {
13007 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
13008 const char *types = insn_op_types(insn);
13014 for (op_index=0; types[op_index]; op_index++, code_index++) {
13015 const char operand_type = types[op_index];
13016 switch (operand_type) {
13019 VALUE op = ibf_load_small_value(load, &reading_pos);
13020 VALUE v = ibf_load_object(load, op);
13021 code[code_index] = v;
13024 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13025 needs_bitmap =
true;
13031 VALUE op = ibf_load_small_value(load, &reading_pos);
13032 VALUE v = ibf_load_object(load, op);
13033 v = rb_hash_dup(v);
13034 RHASH_TBL_RAW(v)->type = &cdhash_type;
13036 RB_OBJ_SET_SHAREABLE(freeze_hide_obj(v));
13041 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
13043 code[code_index] = v;
13044 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13046 needs_bitmap =
true;
13051 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
13052 VALUE v = (
VALUE)ibf_load_iseq(load, (
const rb_iseq_t *)op);
13053 code[code_index] = v;
13056 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13057 needs_bitmap =
true;
13063 VALUE op = ibf_load_small_value(load, &reading_pos);
13064 VALUE arr = ibf_load_object(load, op);
13066 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
13067 ic->
segments = array_to_idlist(arr);
13069 code[code_index] = (
VALUE)ic;
13076 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13078 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
13079 code[code_index] = (
VALUE)ic;
13081 if (operand_type == TS_IVC) {
13082 IVC cache = (IVC)ic;
13084 if (insn == BIN(setinstancevariable)) {
13085 ID iv_name = (
ID)code[code_index - 1];
13086 cache->iv_set_name = iv_name;
13089 cache->iv_set_name = 0;
13092 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
13099 code[code_index] = (
VALUE)cd_entries++;
13104 VALUE op = ibf_load_small_value(load, &reading_pos);
13105 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
13112 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
13115 code[code_index] = ibf_load_small_value(load, &reading_pos);
13119 if (insn_len(insn) != op_index+1) {
13124 load_body->iseq_size = code_index;
13126 if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
13127 load_body->mark_bits.single = mark_offset_bits[0];
13130 if (needs_bitmap) {
13131 load_body->mark_bits.list = mark_offset_bits;
13134 load_body->mark_bits.list = 0;
13135 ruby_xfree(mark_offset_bits);
13140 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
13145ibf_dump_param_opt_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13147 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
13150 IBF_W_ALIGN(
VALUE);
13151 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
13154 return ibf_dump_pos(dump);
13159ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
13163 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
13172ibf_dump_param_keyword(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13174 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
13177 struct rb_iseq_param_keyword dump_kw = *kw;
13178 int dv_num = kw->num - kw->required_num;
13183 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
13184 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
13186 dump_kw.table = IBF_W(ids,
ID, kw->num);
13187 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
13188 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
13189 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
13196static const struct rb_iseq_param_keyword *
13197ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
13199 if (param_keyword_offset) {
13200 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
13201 int dv_num = kw->num - kw->required_num;
13202 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
13205 for (i=0; i<dv_num; i++) {
13206 dvs[i] = ibf_load_object(load, dvs[i]);
13212 kw->default_values = dvs;
13221ibf_dump_insns_info_body(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13223 ibf_offset_t offset = ibf_dump_pos(dump);
13227 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
13228 ibf_dump_write_small_value(dump, entries[i].line_no);
13229#ifdef USE_ISEQ_NODE_ID
13230 ibf_dump_write_small_value(dump, entries[i].node_id);
13232 ibf_dump_write_small_value(dump, entries[i].events);
13239ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
13241 ibf_offset_t reading_pos = body_offset;
13245 for (i = 0; i < size; i++) {
13246 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
13247#ifdef USE_ISEQ_NODE_ID
13248 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
13250 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
13257ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
13259 ibf_offset_t offset = ibf_dump_pos(dump);
13261 unsigned int last = 0;
13263 for (i = 0; i < size; i++) {
13264 ibf_dump_write_small_value(dump, positions[i] - last);
13265 last = positions[i];
13271static unsigned int *
13272ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
13274 ibf_offset_t reading_pos = positions_offset;
13275 unsigned int *positions =
ALLOC_N(
unsigned int, size);
13277 unsigned int last = 0;
13279 for (i = 0; i < size; i++) {
13280 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
13281 last = positions[i];
13288ibf_dump_local_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13291 const int size = body->local_table_size;
13295 for (i=0; i<size; i++) {
13296 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13299 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13305 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
13309ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13312 ID *table = IBF_R(local_table_offset,
ID, size);
13315 for (i=0; i<size; i++) {
13316 table[i] = ibf_load_id(load, table[i]);
13319 if (size == 1 && table[0] == idERROR_INFO) {
13321 return rb_iseq_shared_exc_local_tbl;
13333ibf_dump_lvar_states(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13336 const int size = body->local_table_size;
13337 IBF_W_ALIGN(
enum lvar_state);
13338 return ibf_dump_write(dump, body->lvar_states,
sizeof(
enum lvar_state) * (body->lvar_states ? size : 0));
13341static enum lvar_state *
13342ibf_load_lvar_states(
const struct ibf_load *load, ibf_offset_t lvar_states_offset,
int size,
const ID *local_table)
13344 if (local_table == rb_iseq_shared_exc_local_tbl ||
13349 enum lvar_state *states = IBF_R(lvar_states_offset,
enum lvar_state, size);
13355ibf_dump_catch_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13360 int *iseq_indices =
ALLOCA_N(
int, table->size);
13363 for (i=0; i<table->size; i++) {
13364 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13367 const ibf_offset_t offset = ibf_dump_pos(dump);
13369 for (i=0; i<table->size; i++) {
13370 ibf_dump_write_small_value(dump, iseq_indices[i]);
13371 ibf_dump_write_small_value(dump, table->entries[i].type);
13372 ibf_dump_write_small_value(dump, table->entries[i].start);
13373 ibf_dump_write_small_value(dump, table->entries[i].end);
13374 ibf_dump_write_small_value(dump, table->entries[i].cont);
13375 ibf_dump_write_small_value(dump, table->entries[i].sp);
13380 return ibf_dump_pos(dump);
13385ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size,
const rb_iseq_t *parent_iseq)
13388 struct iseq_catch_table *table = ruby_xcalloc(1, iseq_catch_table_bytes(size));
13389 table->size = size;
13390 ISEQ_BODY(parent_iseq)->catch_table = table;
13392 ibf_offset_t reading_pos = catch_table_offset;
13395 for (i=0; i<table->size; i++) {
13396 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13397 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13398 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13399 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13400 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13401 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13403 rb_iseq_t *catch_iseq = (rb_iseq_t *)ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)iseq_index);
13404 RB_OBJ_WRITE(parent_iseq, UNALIGNED_MEMBER_PTR(&table->entries[i], iseq), catch_iseq);
13408 ISEQ_BODY(parent_iseq)->catch_table = NULL;
13413ibf_dump_ci_entries(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13416 const unsigned int ci_size = body->ci_size;
13419 ibf_offset_t offset = ibf_dump_pos(dump);
13423 for (i = 0; i < ci_size; i++) {
13426 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13427 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13428 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13432 int len = kwarg->keyword_len;
13433 ibf_dump_write_small_value(dump,
len);
13434 for (
int j=0; j<
len; j++) {
13435 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13436 ibf_dump_write_small_value(dump, keyword);
13440 ibf_dump_write_small_value(dump, 0);
13445 ibf_dump_write_small_value(dump, (
VALUE)-1);
13463static enum rb_id_table_iterator_result
13464store_outer_variable(
ID id,
VALUE val,
void *dump)
13469 pair->name = rb_id2str(
id);
13471 return ID_TABLE_CONTINUE;
13475outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13483 else if (!bp->name) {
13491ibf_dump_outer_variables(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13493 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13495 ibf_offset_t offset = ibf_dump_pos(dump);
13497 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13498 ibf_dump_write_small_value(dump, (
VALUE)size);
13507 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13509 for (
size_t i = 0; i < size; ++i) {
13510 ID id = ovlist->pairs[i].id;
13511 ID val = ovlist->pairs[i].val;
13512 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13513 ibf_dump_write_small_value(dump, val);
13522ibf_load_ci_entries(
const struct ibf_load *load,
13523 ibf_offset_t ci_entries_offset,
13524 unsigned int ci_size,
13532 ibf_offset_t reading_pos = ci_entries_offset;
13539 for (i = 0; i < ci_size; i++) {
13540 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
13541 if (mid_index != (
VALUE)-1) {
13542 ID mid = ibf_load_id(load, mid_index);
13543 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13544 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13547 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
13550 kwarg->references = 0;
13551 kwarg->keyword_len = kwlen;
13552 for (
int j=0; j<kwlen; j++) {
13553 VALUE keyword = ibf_load_small_value(load, &reading_pos);
13554 kwarg->keywords[j] = ibf_load_object(load, keyword);
13558 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
13560 cds[i].cc = vm_cc_empty();
13571ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
13573 ibf_offset_t reading_pos = outer_variables_offset;
13577 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
13579 if (table_size > 0) {
13580 tbl = rb_id_table_create(table_size);
13583 for (
size_t i = 0; i < table_size; i++) {
13584 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
13585 VALUE value = ibf_load_small_value(load, &reading_pos);
13586 if (!key) key = rb_make_temporary_id(i);
13587 rb_id_table_insert(tbl, key, value);
13594ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13596 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
13598 unsigned int *positions;
13602 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
13603 const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
13604 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
13606#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13607 ibf_offset_t iseq_start = ibf_dump_pos(dump);
13612 buffer.obj_table = ibf_dump_object_table_new();
13613 dump->current_buffer = &buffer;
13616 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
13617 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
13618 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
13619 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
13620 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
13622 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
13623 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
13624 ruby_xfree(positions);
13626 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
13627 const ibf_offset_t lvar_states_offset = ibf_dump_lvar_states(dump, iseq);
13628 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
13629 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
13630 const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
13631 const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
13632 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
13633 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
13634 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
13636#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13637 ibf_offset_t local_obj_list_offset;
13638 unsigned int local_obj_list_size;
13640 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
13643 ibf_offset_t body_offset = ibf_dump_pos(dump);
13646 unsigned int param_flags =
13647 (body->param.flags.has_lead << 0) |
13648 (body->param.flags.has_opt << 1) |
13649 (body->param.flags.has_rest << 2) |
13650 (body->param.flags.has_post << 3) |
13651 (body->param.flags.has_kw << 4) |
13652 (body->param.flags.has_kwrest << 5) |
13653 (body->param.flags.has_block << 6) |
13654 (body->param.flags.ambiguous_param0 << 7) |
13655 (body->param.flags.accepts_no_kwarg << 8) |
13656 (body->param.flags.ruby2_keywords << 9) |
13657 (body->param.flags.anon_rest << 10) |
13658 (body->param.flags.anon_kwrest << 11) |
13659 (body->param.flags.use_block << 12) |
13660 (body->param.flags.forwardable << 13) ;
13662#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13663# define IBF_BODY_OFFSET(x) (x)
13665# define IBF_BODY_OFFSET(x) (body_offset - (x))
13668 ibf_dump_write_small_value(dump, body->type);
13669 ibf_dump_write_small_value(dump, body->iseq_size);
13670 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
13671 ibf_dump_write_small_value(dump, bytecode_size);
13672 ibf_dump_write_small_value(dump, param_flags);
13673 ibf_dump_write_small_value(dump, body->param.size);
13674 ibf_dump_write_small_value(dump, body->param.lead_num);
13675 ibf_dump_write_small_value(dump, body->param.opt_num);
13676 ibf_dump_write_small_value(dump, body->param.rest_start);
13677 ibf_dump_write_small_value(dump, body->param.post_start);
13678 ibf_dump_write_small_value(dump, body->param.post_num);
13679 ibf_dump_write_small_value(dump, body->param.block_start);
13680 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
13681 ibf_dump_write_small_value(dump, param_keyword_offset);
13682 ibf_dump_write_small_value(dump, location_pathobj_index);
13683 ibf_dump_write_small_value(dump, location_base_label_index);
13684 ibf_dump_write_small_value(dump, location_label_index);
13685 ibf_dump_write_small_value(dump, body->location.first_lineno);
13686 ibf_dump_write_small_value(dump, body->location.node_id);
13687 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
13688 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
13689 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
13690 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
13691 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
13692 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
13693 ibf_dump_write_small_value(dump, body->insns_info.size);
13694 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
13695 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(lvar_states_offset));
13696 ibf_dump_write_small_value(dump, catch_table_size);
13697 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
13698 ibf_dump_write_small_value(dump, parent_iseq_index);
13699 ibf_dump_write_small_value(dump, local_iseq_index);
13700 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
13701 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
13702 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
13703 ibf_dump_write_small_value(dump, body->variable.flip_count);
13704 ibf_dump_write_small_value(dump, body->local_table_size);
13705 ibf_dump_write_small_value(dump, body->ivc_size);
13706 ibf_dump_write_small_value(dump, body->icvarc_size);
13707 ibf_dump_write_small_value(dump, body->ise_size);
13708 ibf_dump_write_small_value(dump, body->ic_size);
13709 ibf_dump_write_small_value(dump, body->ci_size);
13710 ibf_dump_write_small_value(dump, body->stack_max);
13711 ibf_dump_write_small_value(dump, body->builtin_attrs);
13712 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
13714#undef IBF_BODY_OFFSET
13716#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13717 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
13719 dump->current_buffer = saved_buffer;
13720 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
13722 ibf_offset_t offset = ibf_dump_pos(dump);
13723 ibf_dump_write_small_value(dump, iseq_start);
13724 ibf_dump_write_small_value(dump, iseq_length_bytes);
13725 ibf_dump_write_small_value(dump, body_offset);
13727 ibf_dump_write_small_value(dump, local_obj_list_offset);
13728 ibf_dump_write_small_value(dump, local_obj_list_size);
13730 st_free_table(buffer.obj_table);
13734 return body_offset;
13739ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
13741 VALUE str = ibf_load_object(load, str_index);
13743 str = rb_fstring(str);
13749ibf_load_iseq_each(
struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t offset)
13753 ibf_offset_t reading_pos = offset;
13755#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13757 load->current_buffer = &load->global_buffer;
13759 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13760 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13761 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13764 buffer.buff = load->global_buffer.buff + iseq_start;
13765 buffer.size = iseq_length_bytes;
13766 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13767 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13768 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
13770 load->current_buffer = &buffer;
13771 reading_pos = body_offset;
13774#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13775# define IBF_BODY_OFFSET(x) (x)
13777# define IBF_BODY_OFFSET(x) (offset - (x))
13780 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13781 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13782 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13783 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13784 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13785 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13786 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
13787 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
13788 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
13789 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
13790 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
13791 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
13792 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13793 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13794 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
13795 const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
13796 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
13797 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
13798 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
13799 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13800 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13801 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13802 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13803 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13804 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13805 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13806 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13807 const ibf_offset_t lvar_states_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13808 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13809 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13810 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13811 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13812 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13813 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13814 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13815 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
13816 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13818 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13819 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13820 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13821 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13823 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13824 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13825 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13826 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
13829 VALUE path = ibf_load_object(load, location_pathobj_index);
13834 realpath = path = rb_fstring(path);
13837 VALUE pathobj = path;
13843 if (!
NIL_P(realpath)) {
13845 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
13846 "(%x), path=%+"PRIsVALUE,
13847 realpath,
TYPE(realpath), path);
13849 realpath = rb_fstring(realpath);
13855 rb_iseq_pathobj_set(iseq, path, realpath);
13859 rb_execution_context_t *ec = GET_EC();
13860 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
13862#undef IBF_BODY_OFFSET
13864 load_body->type =
type;
13865 load_body->stack_max = stack_max;
13866 load_body->param.flags.has_lead = (param_flags >> 0) & 1;
13867 load_body->param.flags.has_opt = (param_flags >> 1) & 1;
13868 load_body->param.flags.has_rest = (param_flags >> 2) & 1;
13869 load_body->param.flags.has_post = (param_flags >> 3) & 1;
13870 load_body->param.flags.has_kw = FALSE;
13871 load_body->param.flags.has_kwrest = (param_flags >> 5) & 1;
13872 load_body->param.flags.has_block = (param_flags >> 6) & 1;
13873 load_body->param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
13874 load_body->param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
13875 load_body->param.flags.ruby2_keywords = (param_flags >> 9) & 1;
13876 load_body->param.flags.anon_rest = (param_flags >> 10) & 1;
13877 load_body->param.flags.anon_kwrest = (param_flags >> 11) & 1;
13878 load_body->param.flags.use_block = (param_flags >> 12) & 1;
13879 load_body->param.flags.forwardable = (param_flags >> 13) & 1;
13880 load_body->param.size = param_size;
13881 load_body->param.lead_num = param_lead_num;
13882 load_body->param.opt_num = param_opt_num;
13883 load_body->param.rest_start = param_rest_start;
13884 load_body->param.post_start = param_post_start;
13885 load_body->param.post_num = param_post_num;
13886 load_body->param.block_start = param_block_start;
13887 load_body->local_table_size = local_table_size;
13888 load_body->ci_size = ci_size;
13889 load_body->insns_info.size = insns_info_size;
13891 ISEQ_COVERAGE_SET(iseq,
Qnil);
13892 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
13893 load_body->variable.flip_count = variable_flip_count;
13894 load_body->variable.script_lines =
Qnil;
13896 load_body->location.first_lineno = location_first_lineno;
13897 load_body->location.node_id = location_node_id;
13898 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
13899 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
13900 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
13901 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
13902 load_body->builtin_attrs = builtin_attrs;
13903 load_body->prism = prism;
13905 load_body->ivc_size = ivc_size;
13906 load_body->icvarc_size = icvarc_size;
13907 load_body->ise_size = ise_size;
13908 load_body->ic_size = ic_size;
13910 if (ISEQ_IS_SIZE(load_body)) {
13914 load_body->is_entries = NULL;
13916 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
13917 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
13918 load_body->param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
13919 load_body->param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
13920 load_body->param.flags.has_kw = (param_flags >> 4) & 1;
13921 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
13922 load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
13923 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
13924 load_body->lvar_states = ibf_load_lvar_states(load, lvar_states_offset, local_table_size, load_body->local_table);
13925 ibf_load_catch_table(load, catch_table_offset, catch_table_size, iseq);
13927 const rb_iseq_t *parent_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)parent_iseq_index);
13928 const rb_iseq_t *local_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)local_iseq_index);
13929 const rb_iseq_t *mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
13931 RB_OBJ_WRITE(iseq, &load_body->parent_iseq, parent_iseq);
13932 RB_OBJ_WRITE(iseq, &load_body->local_iseq, local_iseq);
13933 RB_OBJ_WRITE(iseq, &load_body->mandatory_only_iseq, mandatory_only_iseq);
13936 if (load_body->param.keyword != NULL) {
13938 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->param.keyword;
13939 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
13942 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
13943#if VM_INSN_INFO_TABLE_IMPL == 2
13944 rb_iseq_insns_info_encode_positions(iseq);
13947 rb_iseq_translate_threaded_code(iseq);
13949#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13950 load->current_buffer = &load->global_buffer;
13953 RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
13954 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
13956#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13957 load->current_buffer = saved_buffer;
13959 verify_call_cache(iseq);
13962 rb_vm_pop_frame_no_int(ec);
13972ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
13974 const rb_iseq_t *iseq = (
const rb_iseq_t *)key;
13977 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
13980 return ST_CONTINUE;
13990 args.offset_list = offset_list;
13992 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
13995 st_index_t size = dump->iseq_table->num_entries;
13996 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
13998 for (i = 0; i < size; i++) {
14002 ibf_dump_align(dump,
sizeof(ibf_offset_t));
14003 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
14004 header->iseq_list_size = (
unsigned int)size;
14014 unsigned int type: 5;
14015 unsigned int special_const: 1;
14016 unsigned int frozen: 1;
14017 unsigned int internal: 1;
14020enum ibf_object_class_index {
14021 IBF_OBJECT_CLASS_OBJECT,
14022 IBF_OBJECT_CLASS_ARRAY,
14023 IBF_OBJECT_CLASS_STANDARD_ERROR,
14024 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
14025 IBF_OBJECT_CLASS_TYPE_ERROR,
14026 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
14036 long keyval[FLEX_ARY_LEN];
14049 BDIGIT digits[FLEX_ARY_LEN];
14052enum ibf_object_data_type {
14053 IBF_OBJECT_DATA_ENCODING,
14064#define IBF_ALIGNED_OFFSET(align, offset) \
14065 ((((offset) - 1) / (align) + 1) * (align))
14070#define IBF_OBJBODY(type, offset) \
14071 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
14074ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
14076 if (offset >= load->current_buffer->size) {
14077 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
14079 return load->current_buffer->buff + offset;
14082NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
14085ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
14088 rb_raw_obj_info(buff,
sizeof(buff), obj);
14097 rb_raise(rb_eArgError,
"unsupported");
14104 enum ibf_object_class_index cindex;
14105 if (obj == rb_cObject) {
14106 cindex = IBF_OBJECT_CLASS_OBJECT;
14109 cindex = IBF_OBJECT_CLASS_ARRAY;
14112 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
14115 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
14118 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
14121 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
14124 rb_obj_info_dump(obj);
14126 rb_bug(
"unsupported class");
14128 ibf_dump_write_small_value(dump, (
VALUE)cindex);
14134 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
14137 case IBF_OBJECT_CLASS_OBJECT:
14139 case IBF_OBJECT_CLASS_ARRAY:
14141 case IBF_OBJECT_CLASS_STANDARD_ERROR:
14143 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
14145 case IBF_OBJECT_CLASS_TYPE_ERROR:
14147 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
14151 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
14159 (void)IBF_W(&dbl,
double, 1);
14167 memcpy(&d, IBF_OBJBODY(
double, offset),
sizeof(d));
14169 if (!
FLONUM_P(f)) RB_OBJ_SET_SHAREABLE(f);
14176 long encindex = (long)rb_enc_get_index(obj);
14177 long len = RSTRING_LEN(obj);
14178 const char *ptr = RSTRING_PTR(obj);
14180 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14181 rb_encoding *enc = rb_enc_from_index((
int)encindex);
14182 const char *enc_name = rb_enc_name(enc);
14183 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
14186 ibf_dump_write_small_value(dump, encindex);
14187 ibf_dump_write_small_value(dump,
len);
14188 IBF_WP(ptr,
char,
len);
14194 ibf_offset_t reading_pos = offset;
14196 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14197 const long len = (long)ibf_load_small_value(load, &reading_pos);
14198 const char *ptr = load->current_buffer->buff + reading_pos;
14200 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14201 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14202 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14206 if (header->frozen && !header->internal) {
14207 str = rb_enc_literal_str(ptr,
len, rb_enc_from_index(encindex));
14210 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
14213 if (header->frozen) str = rb_fstring(str);
14224 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
14226 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
14227 ibf_dump_write_small_value(dump, regexp.srcstr);
14234 regexp.option = ibf_load_byte(load, &offset);
14235 regexp.srcstr = ibf_load_small_value(load, &offset);
14237 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
14238 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
14241 if (header->frozen) RB_OBJ_SET_SHAREABLE(
rb_obj_freeze(reg));
14250 ibf_dump_write_small_value(dump,
len);
14251 for (i=0; i<
len; i++) {
14252 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
14253 ibf_dump_write_small_value(dump, index);
14260 ibf_offset_t reading_pos = offset;
14262 const long len = (long)ibf_load_small_value(load, &reading_pos);
14267 for (i=0; i<
len; i++) {
14268 const VALUE index = ibf_load_small_value(load, &reading_pos);
14272 if (header->frozen) {
14273 rb_ary_freeze(ary);
14274 rb_ractor_make_shareable(ary);
14281ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
14285 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14286 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14288 ibf_dump_write_small_value(dump, key_index);
14289 ibf_dump_write_small_value(dump, val_index);
14290 return ST_CONTINUE;
14297 ibf_dump_write_small_value(dump, (
VALUE)
len);
14305 long len = (long)ibf_load_small_value(load, &offset);
14306 VALUE obj = rb_hash_new_with_size(
len);
14309 for (i = 0; i <
len; i++) {
14310 VALUE key_index = ibf_load_small_value(load, &offset);
14311 VALUE val_index = ibf_load_small_value(load, &offset);
14313 VALUE key = ibf_load_object(load, key_index);
14314 VALUE val = ibf_load_object(load, val_index);
14315 rb_hash_aset(obj, key, val);
14317 rb_hash_rehash(obj);
14320 if (header->frozen) {
14321 RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14335 range.class_index = 0;
14338 range.beg = (long)ibf_dump_object(dump, beg);
14339 range.end = (long)ibf_dump_object(dump, end);
14345 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
14354 VALUE beg = ibf_load_object(load, range->beg);
14355 VALUE end = ibf_load_object(load, range->end);
14358 if (header->frozen) RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14365 ssize_t
len = BIGNUM_LEN(obj);
14366 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14367 BDIGIT *d = BIGNUM_DIGITS(obj);
14369 (void)IBF_W(&slen, ssize_t, 1);
14370 IBF_WP(d, BDIGIT,
len);
14377 int sign = bignum->slen > 0;
14378 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14379 const int big_unpack_flags =
14382 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
14386 if (header->frozen) RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14393 if (rb_data_is_encoding(obj)) {
14394 rb_encoding *enc = rb_to_encoding(obj);
14395 const char *name = rb_enc_name(enc);
14396 long len = strlen(name) + 1;
14398 data[0] = IBF_OBJECT_DATA_ENCODING;
14400 (void)IBF_W(data,
long, 2);
14401 IBF_WP(name,
char,
len);
14404 ibf_dump_object_unsupported(dump, obj);
14411 const long *body = IBF_OBJBODY(
long, offset);
14412 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14414 const char *data = (
const char *)&body[2];
14417 case IBF_OBJECT_DATA_ENCODING:
14419 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
14424 return ibf_load_object_unsupported(load, header, offset);
14428ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14431 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14432 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14434 (void)IBF_W(data,
long, 2);
14438ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14441 VALUE a = ibf_load_object(load, nums->a);
14442 VALUE b = ibf_load_object(load, nums->b);
14454 ibf_dump_object_string(dump,
rb_sym2str(obj));
14460 ibf_offset_t reading_pos = offset;
14462 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14463 const long len = (long)ibf_load_small_value(load, &reading_pos);
14464 const char *ptr = load->current_buffer->buff + reading_pos;
14466 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14467 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14468 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14471 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
14475typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
14476static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
14477 ibf_dump_object_unsupported,
14478 ibf_dump_object_unsupported,
14479 ibf_dump_object_class,
14480 ibf_dump_object_unsupported,
14481 ibf_dump_object_float,
14482 ibf_dump_object_string,
14483 ibf_dump_object_regexp,
14484 ibf_dump_object_array,
14485 ibf_dump_object_hash,
14486 ibf_dump_object_struct,
14487 ibf_dump_object_bignum,
14488 ibf_dump_object_unsupported,
14489 ibf_dump_object_data,
14490 ibf_dump_object_unsupported,
14491 ibf_dump_object_complex_rational,
14492 ibf_dump_object_complex_rational,
14493 ibf_dump_object_unsupported,
14494 ibf_dump_object_unsupported,
14495 ibf_dump_object_unsupported,
14496 ibf_dump_object_unsupported,
14497 ibf_dump_object_symbol,
14498 ibf_dump_object_unsupported,
14499 ibf_dump_object_unsupported,
14500 ibf_dump_object_unsupported,
14501 ibf_dump_object_unsupported,
14502 ibf_dump_object_unsupported,
14503 ibf_dump_object_unsupported,
14504 ibf_dump_object_unsupported,
14505 ibf_dump_object_unsupported,
14506 ibf_dump_object_unsupported,
14507 ibf_dump_object_unsupported,
14508 ibf_dump_object_unsupported,
14514 unsigned char byte =
14515 (header.type << 0) |
14516 (header.special_const << 5) |
14517 (header.frozen << 6) |
14518 (header.internal << 7);
14524ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
14526 unsigned char byte = ibf_load_byte(load, offset);
14529 header.type = (
byte >> 0) & 0x1f;
14530 header.special_const = (
byte >> 5) & 0x01;
14531 header.frozen = (
byte >> 6) & 0x01;
14532 header.internal = (
byte >> 7) & 0x01;
14541 ibf_offset_t current_offset;
14542 IBF_ZERO(obj_header);
14543 obj_header.type =
TYPE(obj);
14545 IBF_W_ALIGN(ibf_offset_t);
14546 current_offset = ibf_dump_pos(dump);
14551 obj_header.special_const = TRUE;
14552 obj_header.frozen = TRUE;
14553 obj_header.internal = TRUE;
14554 ibf_dump_object_object_header(dump, obj_header);
14555 ibf_dump_write_small_value(dump, obj);
14559 obj_header.special_const = FALSE;
14560 obj_header.frozen =
OBJ_FROZEN(obj) ? TRUE : FALSE;
14561 ibf_dump_object_object_header(dump, obj_header);
14562 (*dump_object_functions[obj_header.type])(dump, obj);
14565 return current_offset;
14569static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
14570 ibf_load_object_unsupported,
14571 ibf_load_object_unsupported,
14572 ibf_load_object_class,
14573 ibf_load_object_unsupported,
14574 ibf_load_object_float,
14575 ibf_load_object_string,
14576 ibf_load_object_regexp,
14577 ibf_load_object_array,
14578 ibf_load_object_hash,
14579 ibf_load_object_struct,
14580 ibf_load_object_bignum,
14581 ibf_load_object_unsupported,
14582 ibf_load_object_data,
14583 ibf_load_object_unsupported,
14584 ibf_load_object_complex_rational,
14585 ibf_load_object_complex_rational,
14586 ibf_load_object_unsupported,
14587 ibf_load_object_unsupported,
14588 ibf_load_object_unsupported,
14589 ibf_load_object_unsupported,
14590 ibf_load_object_symbol,
14591 ibf_load_object_unsupported,
14592 ibf_load_object_unsupported,
14593 ibf_load_object_unsupported,
14594 ibf_load_object_unsupported,
14595 ibf_load_object_unsupported,
14596 ibf_load_object_unsupported,
14597 ibf_load_object_unsupported,
14598 ibf_load_object_unsupported,
14599 ibf_load_object_unsupported,
14600 ibf_load_object_unsupported,
14601 ibf_load_object_unsupported,
14605ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
14607 if (object_index == 0) {
14611 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
14613 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
14614 ibf_offset_t offset = offsets[object_index];
14615 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
14618 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
14619 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
14620 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
14621 header.type, header.special_const, header.frozen, header.internal);
14623 if (offset >= load->current_buffer->size) {
14624 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
14627 if (header.special_const) {
14628 ibf_offset_t reading_pos = offset;
14630 obj = ibf_load_small_value(load, &reading_pos);
14633 obj = (*load_object_functions[header.type])(load, &header, offset);
14636 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
14639 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
14640 object_index, obj);
14653ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14658 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
14661 return ST_CONTINUE;
14665ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
14667 st_table *obj_table = dump->current_buffer->obj_table;
14672 args.offset_list = offset_list;
14674 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
14676 IBF_W_ALIGN(ibf_offset_t);
14677 *obj_list_offset = ibf_dump_pos(dump);
14679 st_index_t size = obj_table->num_entries;
14682 for (i=0; i<size; i++) {
14687 *obj_list_size = (
unsigned int)size;
14691ibf_dump_mark(
void *ptr)
14694 rb_gc_mark(dump->global_buffer.str);
14696 rb_mark_set(dump->global_buffer.obj_table);
14697 rb_mark_set(dump->iseq_table);
14701ibf_dump_free(
void *ptr)
14704 if (dump->global_buffer.obj_table) {
14705 st_free_table(dump->global_buffer.obj_table);
14706 dump->global_buffer.obj_table = 0;
14708 if (dump->iseq_table) {
14709 st_free_table(dump->iseq_table);
14710 dump->iseq_table = 0;
14715ibf_dump_memsize(
const void *ptr)
14719 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
14720 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
14726 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
14727 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
14733 dump->global_buffer.obj_table = NULL;
14734 dump->iseq_table = NULL;
14737 dump->global_buffer.obj_table = ibf_dump_object_table_new();
14738 dump->iseq_table = st_init_numtable();
14740 dump->current_buffer = &dump->global_buffer;
14744rb_iseq_ibf_dump(
const rb_iseq_t *iseq,
VALUE opt)
14751 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
14752 ISEQ_BODY(iseq)->local_iseq != iseq) {
14755 if (
RTEST(ISEQ_COVERAGE(iseq))) {
14760 ibf_dump_setup(dump, dump_obj);
14762 ibf_dump_write(dump, &header,
sizeof(header));
14763 ibf_dump_iseq(dump, iseq);
14765 header.magic[0] =
'Y';
14766 header.magic[1] =
'A';
14767 header.magic[2] =
'R';
14768 header.magic[3] =
'B';
14769 header.major_version = IBF_MAJOR_VERSION;
14770 header.minor_version = IBF_MINOR_VERSION;
14771 header.endian = IBF_ENDIAN_MARK;
14773 ibf_dump_iseq_list(dump, &header);
14774 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
14775 header.size = ibf_dump_pos(dump);
14778 VALUE opt_str = opt;
14781 ibf_dump_write(dump, ptr, header.extra_size);
14784 header.extra_size = 0;
14787 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
14789 str = dump->global_buffer.str;
14794static const ibf_offset_t *
14795ibf_iseq_list(
const struct ibf_load *load)
14797 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
14801rb_ibf_load_iseq_complete(rb_iseq_t *iseq)
14804 rb_iseq_t *prev_src_iseq = load->iseq;
14805 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
14808 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
14809 iseq->aux.loader.index, offset,
14810 load->header->size);
14812 ibf_load_iseq_each(load, iseq, offset);
14813 ISEQ_COMPILE_DATA_CLEAR(iseq);
14815 rb_iseq_init_trace(iseq);
14816 load->iseq = prev_src_iseq;
14821rb_iseq_complete(
const rb_iseq_t *iseq)
14823 rb_ibf_load_iseq_complete((rb_iseq_t *)iseq);
14829ibf_load_iseq(
const struct ibf_load *load,
const rb_iseq_t *index_iseq)
14831 int iseq_index = (int)(
VALUE)index_iseq;
14834 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
14835 (
void *)index_iseq, (
void *)load->iseq_list);
14837 if (iseq_index == -1) {
14841 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
14844 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
14847 return (rb_iseq_t *)iseqv;
14850 rb_iseq_t *iseq = iseq_imemo_alloc();
14852 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
14855 iseq->aux.loader.obj = load->loader_obj;
14856 iseq->aux.loader.index = iseq_index;
14858 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
14859 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
14861 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
14863 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
14865 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
14867 rb_ibf_load_iseq_complete(iseq);
14871 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
14872 (
void *)iseq, (
void *)load->iseq);
14880ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
14883 load->loader_obj = loader_obj;
14884 load->global_buffer.buff = bytes;
14885 load->header = header;
14886 load->global_buffer.size = header->size;
14887 load->global_buffer.obj_list_offset = header->global_object_list_offset;
14888 load->global_buffer.obj_list_size = header->global_object_list_size;
14889 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
14890 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
14893 load->current_buffer = &load->global_buffer;
14895 if (size < header->size) {
14898 if (strncmp(header->magic,
"YARB", 4) != 0) {
14901 if (header->major_version != IBF_MAJOR_VERSION ||
14902 header->minor_version != IBF_MINOR_VERSION) {
14904 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
14906 if (header->endian != IBF_ENDIAN_MARK) {
14912 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14913 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
14914 header->iseq_list_offset);
14916 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14917 rb_raise(rb_eArgError,
"unaligned object list offset: %u",
14918 load->global_buffer.obj_list_offset);
14931 if (USE_LAZY_LOAD) {
14932 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
14935 ibf_load_setup_bytes(load, loader_obj, RSTRING_PTR(str), RSTRING_LEN(str));
14940ibf_loader_mark(
void *ptr)
14943 rb_gc_mark(load->str);
14944 rb_gc_mark(load->iseq_list);
14945 rb_gc_mark(load->global_buffer.obj_list);
14949ibf_loader_free(
void *ptr)
14956ibf_loader_memsize(
const void *ptr)
14963 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
14964 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
14968rb_iseq_ibf_load(
VALUE str)
14974 ibf_load_setup(load, loader_obj, str);
14975 iseq = ibf_load_iseq(load, 0);
14982rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
14988 ibf_load_setup_bytes(load, loader_obj, bytes, size);
14989 iseq = ibf_load_iseq(load, 0);
14996rb_iseq_ibf_load_extra_data(
VALUE str)
15002 ibf_load_setup(load, loader_obj, str);
15003 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
15008#include "prism_compile.c"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ALIGNOF
Wraps (or simulates) alignof.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_CLASS
Encountered a new class.
#define RUBY_EVENT_NONE
No events.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define NUM2ULONG
Old name of RB_NUM2ULONG.
#define NUM2LL
Old name of RB_NUM2LL.
#define REALLOC_N
Old name of RB_REALLOC_N.
#define ALLOCV
Old name of RB_ALLOCV.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define FIX2UINT
Old name of RB_FIX2UINT.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define FIXABLE
Old name of RB_FIXABLE.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_SET
Old name of RB_FL_SET.
#define 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 T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
VALUE rb_eNotImpError
NotImplementedError exception.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_errinfo(void)
This is the same as $! in Ruby.
VALUE rb_eSyntaxError
SyntaxError exception.
@ RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK
Warning is for checking unused block strictly.
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
VALUE rb_cArray
Array class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_cHash
Hash class.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cRange
Range class.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
void rb_memerror(void)
Triggers out-of-memory error.
VALUE rb_ary_reverse(VALUE ary)
Destructively reverses the passed array in-place.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define INTEGER_PACK_NEGATIVE
Interprets the input as a signed negative number (unpack only).
#define INTEGER_PACK_LSWORD_FIRST
Stores/interprets the least significant word as the first word.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
int rb_is_attrset_id(ID id)
Classifies the given ID, then sees if it is an attribute writer.
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
VALUE rb_range_new(VALUE beg, VALUE end, int excl)
Creates a new Range.
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
int rb_reg_options(VALUE re)
Queries the options of the passed regular expression.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
int len
Length of the buffer.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
#define rb_long2int
Just another name of rb_long2int_inline.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define ALLOCA_N(type, n)
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RB_ALLOCV(v, n)
Identical to RB_ALLOCV_N(), except that it allocates a number of bytes and returns a void* .
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
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.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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 ::...
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.