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;
1991 if (kw > VM_CALL_KW_LEN_MAX) {
1992 COMPILE_ERROR(ERROR_ARGS_AT(RNODE(args->kw_args))
"too many keyword parameters (%d, maximum is %d)",
1993 kw, (
int)VM_CALL_KW_LEN_MAX);
1996 keyword->bits_start = arg_size++;
1998 node = args->kw_args;
2000 const NODE *val_node = get_nd_value(node->nd_body);
2003 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
2007 switch (nd_type(val_node)) {
2009 dv = rb_node_sym_string_val(val_node);
2012 dv = rb_node_regx_string_val(val_node);
2015 dv = rb_node_line_lineno_val(val_node);
2018 dv = rb_node_integer_literal_val(val_node);
2021 dv = rb_node_float_literal_val(val_node);
2024 dv = rb_node_rational_literal_val(val_node);
2026 case NODE_IMAGINARY:
2027 dv = rb_node_imaginary_literal_val(val_node);
2030 dv = rb_node_encoding_val(val_node);
2042 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
2046 keyword->num = ++di;
2050 node = node->nd_next;
2055 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
2056 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2057 keyword->rest_start = arg_size++;
2058 body->param.flags.has_kwrest = TRUE;
2060 if (kw_id == idPow) body->param.flags.anon_kwrest = TRUE;
2062 keyword->required_num = rkw;
2063 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
2068 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
2070 if (dv == complex_mark) dv =
Qundef;
2075 keyword->default_values = dvs;
2081iseq_set_use_block(rb_iseq_t *iseq)
2084 if (!body->param.flags.use_block) {
2085 body->param.flags.use_block = 1;
2087 rb_vm_t *vm = GET_VM();
2090 st_data_t key = (st_data_t)rb_intern_str(body->location.label);
2091 set_insert(vm->unused_block_warning_table, key);
2097iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *
const optargs,
const NODE *
const node_args)
2099 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2103 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2109 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2111 body->param.lead_num = arg_size = (int)args->pre_args_num;
2112 if (body->param.lead_num > 0) body->param.flags.has_lead = TRUE;
2113 debugs(
" - argc: %d\n", body->param.lead_num);
2115 rest_id = args->rest_arg;
2116 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2120 block_id = args->block_arg;
2122 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2124 if (optimized_forward) {
2129 if (args->opt_args) {
2130 const rb_node_opt_arg_t *node = args->opt_args;
2137 label = NEW_LABEL(nd_line(RNODE(node)));
2139 ADD_LABEL(optargs, label);
2140 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2141 node = node->nd_next;
2146 label = NEW_LABEL(nd_line(node_args));
2148 ADD_LABEL(optargs, label);
2153 for (j = 0; j < i+1; j++) {
2158 body->param.flags.has_opt = TRUE;
2159 body->param.opt_num = i;
2160 body->param.opt_table = opt_table;
2165 body->param.rest_start = arg_size++;
2166 body->param.flags.has_rest = TRUE;
2167 if (rest_id ==
'*') body->param.flags.anon_rest = TRUE;
2171 if (args->first_post_arg) {
2172 body->param.post_start = arg_size;
2173 body->param.post_num = args->post_args_num;
2174 body->param.flags.has_post = TRUE;
2175 arg_size += args->post_args_num;
2177 if (body->param.flags.has_rest) {
2178 body->param.post_start = body->param.rest_start + 1;
2182 if (args->kw_args) {
2183 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2185 else if (args->kw_rest_arg && !optimized_forward) {
2186 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2187 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2188 keyword->rest_start = arg_size++;
2189 body->param.keyword = keyword;
2190 body->param.flags.has_kwrest = TRUE;
2192 static ID anon_kwrest = 0;
2193 if (!anon_kwrest) anon_kwrest = rb_intern(
"**");
2194 if (kw_id == anon_kwrest) body->param.flags.anon_kwrest = TRUE;
2196 else if (args->no_kwarg) {
2197 body->param.flags.accepts_no_kwarg = TRUE;
2201 body->param.block_start = arg_size++;
2202 body->param.flags.has_block = TRUE;
2203 iseq_set_use_block(iseq);
2207 if (optimized_forward) {
2208 body->param.flags.use_block = 1;
2209 body->param.flags.forwardable = TRUE;
2213 iseq_calc_param_size(iseq);
2214 body->param.size = arg_size;
2216 if (args->pre_init) {
2217 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2219 if (args->post_init) {
2220 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2223 if (body->type == ISEQ_TYPE_BLOCK) {
2224 if (body->param.flags.has_opt == FALSE &&
2225 body->param.flags.has_post == FALSE &&
2226 body->param.flags.has_rest == FALSE &&
2227 body->param.flags.has_kw == FALSE &&
2228 body->param.flags.has_kwrest == FALSE) {
2230 if (body->param.lead_num == 1 && last_comma == 0) {
2232 body->param.flags.ambiguous_param0 = TRUE;
2242iseq_set_local_table(rb_iseq_t *iseq,
const rb_ast_id_table_t *tbl,
const NODE *
const node_args)
2244 unsigned int size = tbl ? tbl->size : 0;
2245 unsigned int offset = 0;
2248 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2253 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2262 MEMCPY(ids, tbl->ids + offset,
ID, size);
2263 ISEQ_BODY(iseq)->local_table = ids;
2265 enum lvar_state *states =
ALLOC_N(
enum lvar_state, size);
2267 for (
unsigned int i=0; i<size; i++) {
2268 states[i] = lvar_uninitialized;
2271 ISEQ_BODY(iseq)->lvar_states = states;
2273 ISEQ_BODY(iseq)->local_table_size = size;
2275 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2287 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2290 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2293 else if (tlit != tval) {
2303 long x =
FIX2LONG(rb_big_cmp(lit, val));
2311 return rb_float_cmp(lit, val);
2314 const struct RRational *rat1 = RRATIONAL(val);
2315 const struct RRational *rat2 = RRATIONAL(lit);
2316 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2319 const struct RComplex *comp1 = RCOMPLEX(val);
2320 const struct RComplex *comp2 = RCOMPLEX(lit);
2321 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2324 return rb_reg_equal(val, lit) ? 0 : -1;
2332rb_iseq_cdhash_hash(
VALUE a)
2334 switch (OBJ_BUILTIN_TYPE(a)) {
2337 return (st_index_t)a;
2345 return rb_rational_hash(a);
2347 return rb_complex_hash(a);
2355static const struct st_hash_type cdhash_type = {
2357 rb_iseq_cdhash_hash,
2370 LABEL *lobj = (LABEL *)(val & ~1);
2371 rb_hash_aset(data->hash, key,
INT2FIX(lobj->position - (data->pos+data->len)));
2377get_ivar_ic_value(rb_iseq_t *iseq,
ID id)
2379 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2383get_cvar_ic_value(rb_iseq_t *iseq,
ID id)
2386 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2388 if (rb_id_table_lookup(tbl,
id,&val)) {
2393 tbl = rb_id_table_create(1);
2394 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2396 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2397 rb_id_table_insert(tbl,
id,val);
2401#define BADINSN_DUMP(anchor, list, dest) \
2402 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2404#define BADINSN_ERROR \
2405 (xfree(generated_iseq), \
2406 xfree(insns_info), \
2407 BADINSN_DUMP(anchor, list, NULL), \
2411fix_sp_depth(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
2413 int stack_max = 0, sp = 0, line = 0;
2416 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2417 if (IS_LABEL(list)) {
2418 LABEL *lobj = (LABEL *)list;
2423 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2424 switch (list->type) {
2425 case ISEQ_ELEMENT_INSN:
2430 INSN *iobj = (INSN *)list;
2433 sp = calc_sp_depth(sp, iobj);
2435 BADINSN_DUMP(anchor, list, NULL);
2436 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2437 "argument stack underflow (%d)", sp);
2440 if (sp > stack_max) {
2444 line = iobj->insn_info.line_no;
2446 operands = iobj->operands;
2447 insn = iobj->insn_id;
2448 types = insn_op_types(insn);
2449 len = insn_len(insn);
2452 if (iobj->operand_size !=
len - 1) {
2454 BADINSN_DUMP(anchor, list, NULL);
2455 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2456 "operand size miss! (%d for %d)",
2457 iobj->operand_size,
len - 1);
2461 for (j = 0; types[j]; j++) {
2462 if (types[j] == TS_OFFSET) {
2464 LABEL *lobj = (LABEL *)operands[j];
2466 BADINSN_DUMP(anchor, list, NULL);
2467 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2468 "unknown label: "LABEL_FORMAT, lobj->label_no);
2471 if (lobj->sp == -1) {
2474 else if (lobj->sp != sp) {
2475 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2476 RSTRING_PTR(rb_iseq_path(iseq)), line,
2477 lobj->label_no, lobj->sp, sp);
2483 case ISEQ_ELEMENT_LABEL:
2485 LABEL *lobj = (LABEL *)list;
2486 if (lobj->sp == -1) {
2490 if (lobj->sp != sp) {
2491 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2492 RSTRING_PTR(rb_iseq_path(iseq)), line,
2493 lobj->label_no, lobj->sp, sp);
2499 case ISEQ_ELEMENT_TRACE:
2504 case ISEQ_ELEMENT_ADJUST:
2506 ADJUST *adjust = (ADJUST *)list;
2509 sp = adjust->label ? adjust->label->sp : 0;
2510 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2511 BADINSN_DUMP(anchor, list, NULL);
2512 COMPILE_ERROR(iseq, adjust->line_no,
2513 "iseq_set_sequence: adjust bug %d < %d",
2520 BADINSN_DUMP(anchor, list, NULL);
2521 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2530 int insns_info_index,
int code_index,
const INSN *iobj)
2532 if (insns_info_index == 0 ||
2533 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2534#ifdef USE_ISEQ_NODE_ID
2535 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2537 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2538 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2539#ifdef USE_ISEQ_NODE_ID
2540 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2542 insns_info[insns_info_index].events = iobj->insn_info.events;
2543 positions[insns_info_index] = code_index;
2551 int insns_info_index,
int code_index,
const ADJUST *adjust)
2553 insns_info[insns_info_index].line_no = adjust->line_no;
2554 insns_info[insns_info_index].node_id = -1;
2555 insns_info[insns_info_index].events = 0;
2556 positions[insns_info_index] = code_index;
2561array_to_idlist(
VALUE arr)
2566 for (
long i = 0; i < size; i++) {
2575idlist_to_array(
const ID *ids)
2588iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
2592 unsigned int *positions;
2594 VALUE *generated_iseq;
2598 int insn_num, code_index, insns_info_index, sp = 0;
2599 int stack_max = fix_sp_depth(iseq, anchor);
2601 if (stack_max < 0)
return COMPILE_NG;
2604 insn_num = code_index = 0;
2605 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2606 switch (list->type) {
2607 case ISEQ_ELEMENT_INSN:
2609 INSN *iobj = (INSN *)list;
2611 sp = calc_sp_depth(sp, iobj);
2613 events = iobj->insn_info.events |= events;
2614 if (ISEQ_COVERAGE(iseq)) {
2615 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2616 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2617 int line = iobj->insn_info.line_no - 1;
2618 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2622 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2623 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2629 code_index += insn_data_length(iobj);
2634 case ISEQ_ELEMENT_LABEL:
2636 LABEL *lobj = (LABEL *)list;
2637 lobj->position = code_index;
2638 if (lobj->sp != sp) {
2639 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2640 RSTRING_PTR(rb_iseq_path(iseq)),
2641 lobj->label_no, lobj->sp, sp);
2646 case ISEQ_ELEMENT_TRACE:
2648 TRACE *trace = (TRACE *)list;
2649 events |= trace->event;
2650 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2653 case ISEQ_ELEMENT_ADJUST:
2655 ADJUST *adjust = (ADJUST *)list;
2656 if (adjust->line_no != -1) {
2658 sp = adjust->label ? adjust->label->sp : 0;
2659 if (orig_sp - sp > 0) {
2660 if (orig_sp - sp > 1) code_index++;
2674 positions =
ALLOC_N(
unsigned int, insn_num);
2675 if (ISEQ_IS_SIZE(body)) {
2679 body->is_entries = NULL;
2682 if (body->ci_size) {
2686 body->call_data = NULL;
2688 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2695 iseq_bits_t * mark_offset_bits;
2696 int code_size = code_index;
2698 bool needs_bitmap =
false;
2700 if (ISEQ_MBITS_BUFLEN(code_index) == 1) {
2701 mark_offset_bits = &ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2702 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
true;
2705 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(code_index));
2706 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = mark_offset_bits;
2707 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
false;
2710 ISEQ_COMPILE_DATA(iseq)->iseq_encoded = (
void *)generated_iseq;
2711 ISEQ_COMPILE_DATA(iseq)->iseq_size = code_index;
2713 list = FIRST_ELEMENT(anchor);
2714 insns_info_index = code_index = sp = 0;
2717 switch (list->type) {
2718 case ISEQ_ELEMENT_INSN:
2723 INSN *iobj = (INSN *)list;
2726 sp = calc_sp_depth(sp, iobj);
2728 operands = iobj->operands;
2729 insn = iobj->insn_id;
2730 generated_iseq[code_index] = insn;
2731 types = insn_op_types(insn);
2732 len = insn_len(insn);
2734 for (j = 0; types[j]; j++) {
2735 char type = types[j];
2742 LABEL *lobj = (LABEL *)operands[j];
2743 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2748 VALUE map = operands[j];
2751 data.pos = code_index;
2755 rb_hash_rehash(map);
2756 freeze_hide_obj(map);
2758 generated_iseq[code_index + 1 + j] = map;
2759 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2761 needs_bitmap =
true;
2766 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2771 VALUE v = operands[j];
2772 generated_iseq[code_index + 1 + j] = v;
2776 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2777 needs_bitmap =
true;
2784 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2785 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2786 if (UNLIKELY(ic_index >= body->ic_size)) {
2787 BADINSN_DUMP(anchor, &iobj->link, 0);
2788 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2789 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2790 ic_index, ISEQ_IS_SIZE(body));
2793 ic->
segments = array_to_idlist(operands[j]);
2795 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2800 unsigned int ic_index =
FIX2UINT(operands[j]);
2802 IVC cache = ((IVC)&body->is_entries[ic_index]);
2804 if (insn == BIN(setinstancevariable)) {
2805 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2808 cache->iv_set_name = 0;
2811 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
2816 unsigned int ic_index =
FIX2UINT(operands[j]);
2817 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2818 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2819 BADINSN_DUMP(anchor, &iobj->link, 0);
2820 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2821 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2822 ic_index, ISEQ_IS_SIZE(body));
2824 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2831 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2832 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2834 cd->cc = vm_cc_empty();
2835 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2839 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2842 generated_iseq[code_index + 1 + j] = operands[j];
2845 generated_iseq[code_index + 1 + j] = operands[j];
2848 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2849 "unknown operand type: %c",
type);
2853 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2857 case ISEQ_ELEMENT_LABEL:
2859 LABEL *lobj = (LABEL *)list;
2860 if (lobj->sp != sp) {
2861 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2862 RSTRING_PTR(rb_iseq_path(iseq)),
2863 lobj->label_no, lobj->sp, sp);
2868 case ISEQ_ELEMENT_ADJUST:
2870 ADJUST *adjust = (ADJUST *)list;
2873 if (adjust->label) {
2874 sp = adjust->label->sp;
2880 if (adjust->line_no != -1) {
2881 const int diff = orig_sp - sp;
2883 if (insns_info_index == 0) {
2884 COMPILE_ERROR(iseq, adjust->line_no,
2885 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2887 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2890 generated_iseq[code_index++] = BIN(adjuststack);
2891 generated_iseq[code_index++] = orig_sp - sp;
2893 else if (diff == 1) {
2894 generated_iseq[code_index++] = BIN(pop);
2896 else if (diff < 0) {
2897 int label_no = adjust->label ? adjust->label->label_no : -1;
2898 xfree(generated_iseq);
2901 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
2902 xfree(mark_offset_bits);
2904 debug_list(anchor, list);
2905 COMPILE_ERROR(iseq, adjust->line_no,
2906 "iseq_set_sequence: adjust bug to %d %d < %d",
2907 label_no, orig_sp, sp);
2920 body->iseq_encoded = (
void *)generated_iseq;
2921 body->iseq_size = code_index;
2922 body->stack_max = stack_max;
2924 if (ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit) {
2925 body->mark_bits.single = ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2929 body->mark_bits.list = mark_offset_bits;
2932 body->mark_bits.list = NULL;
2933 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = NULL;
2934 ruby_xfree(mark_offset_bits);
2939 body->insns_info.body = insns_info;
2940 body->insns_info.positions = positions;
2943 body->insns_info.body = insns_info;
2944 REALLOC_N(positions,
unsigned int, insns_info_index);
2945 body->insns_info.positions = positions;
2946 body->insns_info.size = insns_info_index;
2952label_get_position(LABEL *lobj)
2954 return lobj->position;
2958label_get_sp(LABEL *lobj)
2964iseq_set_exception_table(rb_iseq_t *iseq)
2966 const VALUE *tptr, *ptr;
2967 unsigned int tlen, i;
2970 ISEQ_BODY(iseq)->catch_table = NULL;
2972 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
2973 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
2981 for (i = 0; i < table->size; i++) {
2984 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
2985 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
2986 pos = label_get_position((LABEL *)(ptr[1] & ~1));
2988 entry->start = (
unsigned int)pos;
2989 pos = label_get_position((LABEL *)(ptr[2] & ~1));
2991 entry->end = (
unsigned int)pos;
2992 entry->iseq = (rb_iseq_t *)ptr[3];
2997 LABEL *lobj = (LABEL *)(ptr[4] & ~1);
2998 entry->cont = label_get_position(lobj);
2999 entry->sp = label_get_sp(lobj);
3002 if (entry->type == CATCH_TYPE_RESCUE ||
3003 entry->type == CATCH_TYPE_BREAK ||
3004 entry->type == CATCH_TYPE_NEXT) {
3013 ISEQ_BODY(iseq)->catch_table = table;
3014 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
3032iseq_set_optargs_table(rb_iseq_t *iseq)
3035 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
3037 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
3038 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
3039 opt_table[i] = label_get_position((LABEL *)opt_table[i]);
3045static LINK_ELEMENT *
3046get_destination_insn(INSN *iobj)
3048 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, 0);
3052 list = lobj->link.next;
3054 switch (list->type) {
3055 case ISEQ_ELEMENT_INSN:
3056 case ISEQ_ELEMENT_ADJUST:
3058 case ISEQ_ELEMENT_LABEL:
3061 case ISEQ_ELEMENT_TRACE:
3063 TRACE *trace = (TRACE *)list;
3064 events |= trace->event;
3072 if (list && IS_INSN(list)) {
3073 INSN *iobj = (INSN *)list;
3074 iobj->insn_info.events |= events;
3079static LINK_ELEMENT *
3080get_next_insn(INSN *iobj)
3082 LINK_ELEMENT *list = iobj->link.next;
3085 if (IS_INSN(list) || IS_ADJUST(list)) {
3093static LINK_ELEMENT *
3094get_prev_insn(INSN *iobj)
3096 LINK_ELEMENT *list = iobj->link.prev;
3099 if (IS_INSN(list) || IS_ADJUST(list)) {
3108unref_destination(INSN *iobj,
int pos)
3110 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, pos);
3112 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3116replace_destination(INSN *dobj, INSN *nobj)
3118 VALUE n = OPERAND_AT(nobj, 0);
3119 LABEL *dl = (LABEL *)OPERAND_AT(dobj, 0);
3120 LABEL *nl = (LABEL *)n;
3121 if (dl == nl)
return false;
3124 OPERAND_AT(dobj, 0) = n;
3125 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3130find_destination(INSN *i)
3132 int pos,
len = insn_len(i->insn_id);
3133 for (pos = 0; pos <
len; ++pos) {
3134 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3135 return (LABEL *)OPERAND_AT(i, pos);
3142remove_unreachable_chunk(rb_iseq_t *iseq, LINK_ELEMENT *i)
3144 LINK_ELEMENT *first = i, *end, *scan;
3145 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
3148 unref_counts =
ALLOCA_N(
int, nlabels);
3149 MEMZERO(unref_counts,
int, nlabels);
3154 if (IS_INSN(scan)) {
3155 if (IS_INSN_ID(scan, leave)) {
3158 else if ((lab = find_destination((INSN *)scan)) != 0) {
3159 unref_counts[lab->label_no]++;
3162 else if (IS_LABEL(scan)) {
3163 lab = (LABEL *)scan;
3164 if (lab->unremovable)
return 0;
3166 else if (IS_ADJUST(scan)) {
3169 }
while ((scan = scan->next) != 0);
3175 if (IS_INSN(scan)) {
3176 if (IS_INSN_ID(scan, leave)) {
3181 else if (IS_LABEL(scan)) {
3182 lab = (LABEL *)scan;
3183 if (lab->refcnt > unref_counts[lab->label_no]) {
3184 if (scan == first)
return 0;
3189 else if (IS_ADJUST(scan)) {
3193 }
while ((scan = scan->next) != 0);
3198 VALUE insn = INSN_OF(i);
3199 int pos,
len = insn_len(insn);
3200 for (pos = 0; pos <
len; ++pos) {
3201 switch (insn_op_types(insn)[pos]) {
3203 unref_destination((INSN *)i, pos);
3212 }
while ((i != end) && (i = i->next) != 0);
3217iseq_pop_newarray(rb_iseq_t *iseq, INSN *iobj)
3219 switch (OPERAND_AT(iobj, 0)) {
3221 ELEM_REMOVE(&iobj->link);
3224 ELEM_REMOVE(&iobj->link);
3227 iobj->insn_id = BIN(adjuststack);
3233is_frozen_putstring(INSN *insn,
VALUE *op)
3235 if (IS_INSN_ID(insn, putstring) || IS_INSN_ID(insn, putchilledstring)) {
3236 *op = OPERAND_AT(insn, 0);
3239 else if (IS_INSN_ID(insn, putobject)) {
3240 *op = OPERAND_AT(insn, 0);
3247insn_has_label_before(LINK_ELEMENT *elem)
3249 LINK_ELEMENT *prev = elem->prev;
3251 if (prev->type == ISEQ_ELEMENT_LABEL) {
3252 LABEL *label = (LABEL *)prev;
3253 if (label->refcnt > 0) {
3257 else if (prev->type == ISEQ_ELEMENT_INSN) {
3266optimize_checktype(rb_iseq_t *iseq, INSN *iobj)
3290 INSN *niobj, *ciobj, *dup = 0;
3294 switch (INSN_OF(iobj)) {
3295 case BIN(putstring):
3296 case BIN(putchilledstring):
3302 case BIN(putobject):
3305 default:
return FALSE;
3308 ciobj = (INSN *)get_next_insn(iobj);
3309 if (IS_INSN_ID(ciobj, jump)) {
3310 ciobj = (INSN *)get_next_insn((INSN*)OPERAND_AT(ciobj, 0));
3312 if (IS_INSN_ID(ciobj, dup)) {
3313 ciobj = (INSN *)get_next_insn(dup = ciobj);
3315 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3316 niobj = (INSN *)get_next_insn(ciobj);
3321 switch (INSN_OF(niobj)) {
3323 if (OPERAND_AT(ciobj, 0) ==
type) {
3324 dest = (LABEL *)OPERAND_AT(niobj, 0);
3327 case BIN(branchunless):
3328 if (OPERAND_AT(ciobj, 0) !=
type) {
3329 dest = (LABEL *)OPERAND_AT(niobj, 0);
3335 line = ciobj->insn_info.line_no;
3336 node_id = ciobj->insn_info.node_id;
3338 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3339 dest = (LABEL *)niobj->link.next;
3342 dest = NEW_LABEL(line);
3343 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3346 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3348 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3353ci_flag_set(
const rb_iseq_t *iseq,
const struct rb_callinfo *ci,
unsigned int add)
3355 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3356 vm_ci_flag(ci) | add,
3364ci_argc_set(
const rb_iseq_t *iseq,
const struct rb_callinfo *ci,
int argc)
3366 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3374#define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3377iseq_reg_compile(rb_iseq_t *iseq,
VALUE str,
int options,
const char *sourcefile,
int sourceline)
3380 VALUE re = rb_reg_compile(str, options, sourcefile, sourceline);
3383 rb_set_errinfo(errinfo);
3384 COMPILE_ERROR(iseq, sourceline,
"%" PRIsVALUE, message);
3387 RB_OBJ_SET_SHAREABLE(re);
3393iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list,
const int do_tailcallopt)
3395 INSN *
const iobj = (INSN *)list;
3398 optimize_checktype(iseq, iobj);
3400 if (IS_INSN_ID(iobj, jump)) {
3401 INSN *niobj, *diobj, *piobj;
3402 diobj = (INSN *)get_destination_insn(iobj);
3403 niobj = (INSN *)get_next_insn(iobj);
3405 if (diobj == niobj) {
3412 unref_destination(iobj, 0);
3413 ELEM_REMOVE(&iobj->link);
3416 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3417 IS_INSN_ID(diobj, jump) &&
3418 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3419 diobj->insn_info.events == 0) {
3430 if (replace_destination(iobj, diobj)) {
3431 remove_unreachable_chunk(iseq, iobj->link.next);
3435 else if (IS_INSN_ID(diobj, leave)) {
3448 unref_destination(iobj, 0);
3449 iobj->insn_id = BIN(leave);
3450 iobj->operand_size = 0;
3451 iobj->insn_info = diobj->insn_info;
3454 else if (IS_INSN(iobj->link.prev) &&
3455 (piobj = (INSN *)iobj->link.prev) &&
3456 (IS_INSN_ID(piobj, branchif) ||
3457 IS_INSN_ID(piobj, branchunless))) {
3458 INSN *pdiobj = (INSN *)get_destination_insn(piobj);
3459 if (niobj == pdiobj) {
3460 int refcnt = IS_LABEL(piobj->link.next) ?
3461 ((LABEL *)piobj->link.next)->refcnt : 0;
3476 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3477 ? BIN(branchunless) : BIN(branchif);
3478 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3479 ELEM_REMOVE(&iobj->link);
3486 else if (diobj == pdiobj) {
3500 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3501 ELEM_REPLACE(&piobj->link, &popiobj->link);
3504 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3518 if (IS_INSN_ID(iobj, newrange)) {
3519 INSN *
const range = iobj;
3521 VALUE str_beg, str_end;
3523 if ((end = (INSN *)get_prev_insn(range)) != 0 &&
3524 is_frozen_putstring(end, &str_end) &&
3525 (beg = (INSN *)get_prev_insn(end)) != 0 &&
3526 is_frozen_putstring(beg, &str_beg) &&
3527 !(insn_has_label_before(&beg->link) || insn_has_label_before(&end->link))) {
3528 int excl =
FIX2INT(OPERAND_AT(range, 0));
3531 ELEM_REMOVE(&beg->link);
3532 ELEM_REMOVE(&end->link);
3533 range->insn_id = BIN(putobject);
3534 OPERAND_AT(range, 0) = lit_range;
3539 if (IS_INSN_ID(iobj, leave)) {
3540 remove_unreachable_chunk(iseq, iobj->link.next);
3552 if (IS_INSN_ID(iobj, duparray)) {
3553 LINK_ELEMENT *next = iobj->link.next;
3554 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3555 iobj->insn_id = BIN(putobject);
3565 if (IS_INSN_ID(iobj, duparray)) {
3566 LINK_ELEMENT *next = iobj->link.next;
3567 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3569 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3571 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3572 VALUE ary = iobj->operands[0];
3575 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, ary, (
VALUE)ci);
3587 if (IS_INSN_ID(iobj, duphash)) {
3588 LINK_ELEMENT *next = iobj->link.next;
3589 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3591 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3593 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3594 VALUE hash = iobj->operands[0];
3596 RB_OBJ_SET_SHAREABLE(hash);
3598 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, hash, (
VALUE)ci);
3610 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3611 LINK_ELEMENT *next = iobj->link.next;
3612 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3614 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3616 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3617 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, rb_cArray_empty_frozen, (
VALUE)ci);
3629 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3630 LINK_ELEMENT *next = iobj->link.next;
3631 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3633 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3635 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3636 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, rb_cHash_empty_frozen, (
VALUE)ci);
3642 if (IS_INSN_ID(iobj, branchif) ||
3643 IS_INSN_ID(iobj, branchnil) ||
3644 IS_INSN_ID(iobj, branchunless)) {
3653 INSN *nobj = (INSN *)get_destination_insn(iobj);
3675 int stop_optimization =
3676 ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq) &&
3677 nobj->link.type == ISEQ_ELEMENT_INSN &&
3678 nobj->insn_info.events;
3679 if (!stop_optimization) {
3680 INSN *pobj = (INSN *)iobj->link.prev;
3683 if (!IS_INSN(&pobj->link))
3685 else if (IS_INSN_ID(pobj, dup))
3690 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3691 if (!replace_destination(iobj, nobj))
break;
3693 else if (prev_dup && IS_INSN(&nobj->link) && IS_INSN_ID(nobj, dup) &&
3694 !!(nobj = (INSN *)nobj->link.next) &&
3695 IS_INSN(&nobj->link) &&
3697 nobj->insn_id == iobj->insn_id) {
3713 if (!replace_destination(iobj, nobj))
break;
3741 if (prev_dup && IS_INSN(pobj->link.prev)) {
3742 pobj = (INSN *)pobj->link.prev;
3744 if (IS_INSN_ID(pobj, putobject)) {
3745 cond = (IS_INSN_ID(iobj, branchif) ?
3746 OPERAND_AT(pobj, 0) !=
Qfalse :
3747 IS_INSN_ID(iobj, branchunless) ?
3748 OPERAND_AT(pobj, 0) ==
Qfalse :
3751 else if (IS_INSN_ID(pobj, putstring) ||
3752 IS_INSN_ID(pobj, duparray) ||
3753 IS_INSN_ID(pobj, newarray)) {
3754 cond = IS_INSN_ID(iobj, branchif);
3756 else if (IS_INSN_ID(pobj, putnil)) {
3757 cond = !IS_INSN_ID(iobj, branchif);
3760 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3761 ELEM_REMOVE(iobj->link.prev);
3763 else if (!iseq_pop_newarray(iseq, pobj)) {
3764 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3765 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3769 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3770 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3772 iobj->insn_id = BIN(jump);
3776 unref_destination(iobj, 0);
3777 ELEM_REMOVE(&iobj->link);
3783 LINK_ELEMENT *dest = get_destination_insn(nobj);
3784 if (!dest || !IS_INSN(dest))
break;
3785 nobj = (INSN *)dest;
3791 if (IS_INSN_ID(iobj, pop)) {
3798 LINK_ELEMENT *prev = iobj->link.prev;
3799 if (IS_INSN(prev)) {
3800 enum ruby_vminsn_type previ = ((INSN *)prev)->insn_id;
3801 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3802 previ == BIN(putself) || previ == BIN(putstring) ||
3803 previ == BIN(putchilledstring) ||
3804 previ == BIN(dup) ||
3805 previ == BIN(getlocal) ||
3806 previ == BIN(getblockparam) ||
3807 previ == BIN(getblockparamproxy) ||
3808 previ == BIN(getinstancevariable) ||
3809 previ == BIN(duparray)) {
3813 ELEM_REMOVE(&iobj->link);
3815 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (INSN*)prev)) {
3816 ELEM_REMOVE(&iobj->link);
3818 else if (previ == BIN(concatarray)) {
3819 INSN *piobj = (INSN *)prev;
3820 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3821 INSN_OF(piobj) = BIN(pop);
3823 else if (previ == BIN(concatstrings)) {
3824 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3828 ELEM_REMOVE(&iobj->link);
3829 INSN_OF(prev) = BIN(adjuststack);
3835 if (IS_INSN_ID(iobj, newarray) ||
3836 IS_INSN_ID(iobj, duparray) ||
3837 IS_INSN_ID(iobj, concatarray) ||
3838 IS_INSN_ID(iobj, splatarray) ||
3847 LINK_ELEMENT *next = iobj->link.next;
3848 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3854 if (IS_INSN_ID(iobj, newarray)) {
3855 LINK_ELEMENT *next = iobj->link.next;
3856 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3857 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3859 op1 = OPERAND_AT(iobj, 0);
3860 op2 = OPERAND_AT(next, 0);
3871 INSN_OF(iobj) = BIN(swap);
3872 iobj->operand_size = 0;
3881 INSN_OF(iobj) = BIN(opt_reverse);
3886 INSN_OF(iobj) = BIN(opt_reverse);
3887 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3897 for (; diff > 0; diff--) {
3898 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
3909 for (; diff < 0; diff++) {
3910 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
3917 if (IS_INSN_ID(iobj, duparray)) {
3918 LINK_ELEMENT *next = iobj->link.next;
3926 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
3927 INSN_OF(iobj) = BIN(putobject);
3931 if (IS_INSN_ID(iobj, anytostring)) {
3932 LINK_ELEMENT *next = iobj->link.next;
3939 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
3940 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
3945 if (IS_INSN_ID(iobj, putstring) || IS_INSN_ID(iobj, putchilledstring) ||
3953 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
3954 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
3955 INSN *next = (INSN *)iobj->link.next;
3956 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
3957 ELEM_REMOVE(&next->link);
3959 ELEM_REMOVE(&iobj->link);
3961 if (IS_NEXT_INSN_ID(&iobj->link, toregexp)) {
3962 INSN *next = (INSN *)iobj->link.next;
3963 if (OPERAND_AT(next, 1) ==
INT2FIX(1)) {
3964 VALUE src = OPERAND_AT(iobj, 0);
3965 int opt = (int)
FIX2LONG(OPERAND_AT(next, 0));
3966 VALUE path = rb_iseq_path(iseq);
3967 int line = iobj->insn_info.line_no;
3968 VALUE re = iseq_reg_compile(iseq, src, opt, RSTRING_PTR(path), line);
3973 iobj->insn_id = BIN(putobject);
3975 ELEM_REMOVE(iobj->link.next);
3980 if (IS_INSN_ID(iobj, concatstrings)) {
3987 LINK_ELEMENT *next = iobj->link.next;
3989 if (IS_INSN(next) && IS_INSN_ID(next, jump))
3990 next = get_destination_insn(jump = (INSN *)next);
3991 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
3992 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
3993 OPERAND_AT(iobj, 0) =
INT2FIX(n);
3995 LABEL *label = ((LABEL *)OPERAND_AT(jump, 0));
3996 if (!--label->refcnt) {
3997 ELEM_REMOVE(&label->link);
4000 label = NEW_LABEL(0);
4001 OPERAND_AT(jump, 0) = (
VALUE)label;
4004 ELEM_INSERT_NEXT(next, &label->link);
4005 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
4013 if (do_tailcallopt &&
4014 (IS_INSN_ID(iobj, send) ||
4015 IS_INSN_ID(iobj, invokesuper))) {
4024 if (iobj->link.next) {
4025 LINK_ELEMENT *next = iobj->link.next;
4027 if (!IS_INSN(next)) {
4031 switch (INSN_OF(next)) {
4040 next = get_destination_insn((INSN *)next);
4054 if (IS_INSN_ID(piobj, send) ||
4055 IS_INSN_ID(piobj, invokesuper)) {
4056 if (OPERAND_AT(piobj, 1) == 0) {
4057 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4058 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4063 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4064 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4070 if (IS_INSN_ID(iobj, dup)) {
4071 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
4072 LINK_ELEMENT *set1 = iobj->link.next, *set2 = NULL;
4082 if (IS_NEXT_INSN_ID(set1, setlocal)) {
4084 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4085 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4087 ELEM_REMOVE(&iobj->link);
4100 else if (IS_NEXT_INSN_ID(set1, dup) &&
4101 IS_NEXT_INSN_ID(set1->next, setlocal)) {
4102 set2 = set1->next->next;
4103 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4104 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4105 ELEM_REMOVE(set1->next);
4119 if (IS_INSN_ID(iobj, getlocal)) {
4120 LINK_ELEMENT *niobj = &iobj->link;
4121 if (IS_NEXT_INSN_ID(niobj, dup)) {
4122 niobj = niobj->next;
4124 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
4125 LINK_ELEMENT *set1 = niobj->next;
4126 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
4127 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
4143 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
4144 if (IS_TRACE(iobj->link.next)) {
4145 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
4146 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
4148 if (iobj == (INSN *)list && bf->argc == 0 && (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF)) {
4149 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4162 if (IS_INSN_ID(iobj, getblockparam)) {
4163 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4164 iobj->insn_id = BIN(getblockparamproxy);
4168 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4169 LINK_ELEMENT *niobj = &iobj->link;
4170 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4171 niobj = niobj->next;
4172 LINK_ELEMENT *siobj;
4173 unsigned int set_flags = 0, unset_flags = 0;
4186 if (IS_NEXT_INSN_ID(niobj, send)) {
4187 siobj = niobj->next;
4188 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4189 unset_flags = VM_CALL_ARGS_BLOCKARG;
4204 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4205 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4206 siobj = niobj->next->next;
4207 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4212 unsigned int flags = vm_ci_flag(ci);
4213 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4214 ((INSN*)niobj)->insn_id = BIN(putobject);
4215 RB_OBJ_WRITE(iseq, &OPERAND_AT(niobj, 0), RB_OBJ_SET_SHAREABLE(rb_hash_freeze(rb_hash_resurrect(OPERAND_AT(niobj, 0)))));
4217 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4218 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4220 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4230insn_set_specialized_instruction(rb_iseq_t *iseq, INSN *iobj,
int insn_id)
4232 if (insn_id == BIN(opt_neq)) {
4233 VALUE original_ci = iobj->operands[0];
4234 VALUE new_ci = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4235 insn_replace_with_operands(iseq, iobj, insn_id, 2, new_ci, original_ci);
4238 iobj->insn_id = insn_id;
4239 iobj->operand_size = insn_len(insn_id) - 1;
4247iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
4249 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4250 IS_INSN(iobj->link.next)) {
4254 INSN *niobj = (INSN *)iobj->link.next;
4255 if (IS_INSN_ID(niobj, send)) {
4257 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4259 switch (vm_ci_mid(ci)) {
4261 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4264 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4267 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4272 VALUE num = iobj->operands[0];
4273 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, num, method);
4274 ELEM_REMOVE(&niobj->link);
4279 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4281 IS_NEXT_INSN_ID(&niobj->link, send)) {
4283 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4284 VALUE num = iobj->operands[0];
4285 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK));
4286 ELEM_REMOVE(&iobj->link);
4287 ELEM_REMOVE(niobj->link.next);
4288 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4294 else if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4296 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4297 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4300 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4301 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] ==
rb_id2sym(idBuffer))) {
4302 VALUE num = iobj->operands[0];
4303 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 2),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER));
4305 ELEM_REMOVE((niobj->link.next)->next);
4307 ELEM_REMOVE(&iobj->link);
4309 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4317 if ((IS_INSN_ID(niobj, putstring) || IS_INSN_ID(niobj, putchilledstring) ||
4318 IS_INSN_ID(niobj, putobject) ||
4319 IS_INSN_ID(niobj, putself) ||
4320 IS_INSN_ID(niobj, getlocal) ||
4321 IS_INSN_ID(niobj, getinstancevariable)) &&
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));
4334 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4335 VALUE num = iobj->operands[0];
4336 INSN *sendins = (INSN *)sendobj;
4337 insn_replace_with_operands(iseq, sendins, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P));
4339 ELEM_REMOVE(&iobj->link);
4353 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4354 INSN *niobj = (INSN *)iobj->link.next;
4355 if ((IS_INSN_ID(niobj, getlocal) ||
4356 IS_INSN_ID(niobj, getinstancevariable) ||
4357 IS_INSN_ID(niobj, putself)) &&
4358 IS_NEXT_INSN_ID(&niobj->link, send)) {
4360 LINK_ELEMENT *sendobj = &(niobj->link);
4365 sendobj = sendobj->next;
4366 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4367 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4369 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4371 VALUE ary = iobj->operands[0];
4374 INSN *sendins = (INSN *)sendobj;
4375 insn_replace_with_operands(iseq, sendins, BIN(opt_duparray_send), 3, ary,
rb_id2sym(idIncludeP),
INT2FIX(1));
4378 ELEM_REMOVE(&iobj->link);
4385 if (IS_INSN_ID(iobj, send)) {
4387 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(iobj, 1);
4389#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4390 if (vm_ci_simple(ci)) {
4391 switch (vm_ci_argc(ci)) {
4393 switch (vm_ci_mid(ci)) {
4394 case idLength: SP_INSN(length);
return COMPILE_OK;
4395 case idSize: SP_INSN(size);
return COMPILE_OK;
4396 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4397 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4398 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4399 case idNot: SP_INSN(not);
return COMPILE_OK;
4403 switch (vm_ci_mid(ci)) {
4404 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4405 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4406 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4407 case idDIV: SP_INSN(div);
return COMPILE_OK;
4408 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4409 case idEq: SP_INSN(eq);
return COMPILE_OK;
4410 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4411 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4412 case idLT: SP_INSN(lt);
return COMPILE_OK;
4413 case idLE: SP_INSN(le);
return COMPILE_OK;
4414 case idGT: SP_INSN(gt);
return COMPILE_OK;
4415 case idGE: SP_INSN(ge);
return COMPILE_OK;
4416 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4417 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4418 case idAnd: SP_INSN(and);
return COMPILE_OK;
4419 case idOr: SP_INSN(or);
return COMPILE_OK;
4423 switch (vm_ci_mid(ci)) {
4424 case idASET: SP_INSN(aset);
return COMPILE_OK;
4430 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4431 iobj->insn_id = BIN(opt_send_without_block);
4432 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4441tailcallable_p(rb_iseq_t *iseq)
4443 switch (ISEQ_BODY(iseq)->
type) {
4445 case ISEQ_TYPE_EVAL:
4446 case ISEQ_TYPE_MAIN:
4448 case ISEQ_TYPE_RESCUE:
4449 case ISEQ_TYPE_ENSURE:
4458iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
4461 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4462 const int do_tailcallopt = tailcallable_p(iseq) &&
4463 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4464 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4465 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4466 int rescue_level = 0;
4467 int tailcallopt = do_tailcallopt;
4469 list = FIRST_ELEMENT(anchor);
4471 int do_block_optimization = 0;
4472 LABEL * block_loop_label = NULL;
4475 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
4476 do_block_optimization = 1;
4480 LINK_ELEMENT * le = FIRST_ELEMENT(anchor)->next;
4481 if (IS_INSN(le) && IS_INSN_ID((INSN *)le, nop) && IS_LABEL(le->next)) {
4482 block_loop_label = (LABEL *)le->next;
4487 if (IS_INSN(list)) {
4488 if (do_peepholeopt) {
4489 iseq_peephole_optimize(iseq, list, tailcallopt);
4492 iseq_specialized_instruction(iseq, (INSN *)list);
4495 insn_operands_unification((INSN *)list);
4498 if (do_block_optimization) {
4499 INSN * item = (INSN *)list;
4501 if (IS_INSN_ID(item,
throw)) {
4502 do_block_optimization = 0;
4507 const char *types = insn_op_types(item->insn_id);
4508 for (
int j = 0; types[j]; j++) {
4509 if (types[j] == TS_OFFSET) {
4514 LABEL * target = (LABEL *)OPERAND_AT(item, j);
4515 if (target == block_loop_label) {
4516 do_block_optimization = 0;
4523 if (IS_LABEL(list)) {
4524 switch (((LABEL *)list)->rescued) {
4525 case LABEL_RESCUE_BEG:
4527 tailcallopt = FALSE;
4529 case LABEL_RESCUE_END:
4530 if (!--rescue_level) tailcallopt = do_tailcallopt;
4537 if (do_block_optimization) {
4538 LINK_ELEMENT * le = FIRST_ELEMENT(anchor)->next;
4539 if (IS_INSN(le) && IS_INSN_ID((INSN *)le, nop)) {
4546#if OPT_INSTRUCTIONS_UNIFICATION
4548new_unified_insn(rb_iseq_t *iseq,
4549 int insn_id,
int size, LINK_ELEMENT *seq_list)
4552 LINK_ELEMENT *list = seq_list;
4554 VALUE *operands = 0, *ptr = 0;
4558 for (i = 0; i < size; i++) {
4559 iobj = (INSN *)list;
4560 argc += iobj->operand_size;
4565 ptr = operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
4570 for (i = 0; i < size; i++) {
4571 iobj = (INSN *)list;
4572 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4573 ptr += iobj->operand_size;
4577 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4587iseq_insns_unification(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
4589#if OPT_INSTRUCTIONS_UNIFICATION
4595 list = FIRST_ELEMENT(anchor);
4597 if (IS_INSN(list)) {
4598 iobj = (INSN *)list;
4600 if (unified_insns_data[
id] != 0) {
4601 const int *
const *entry = unified_insns_data[id];
4602 for (j = 1; j < (intptr_t)entry[0]; j++) {
4603 const int *unified = entry[j];
4604 LINK_ELEMENT *li = list->next;
4605 for (k = 2; k < unified[1]; k++) {
4607 ((INSN *)li)->insn_id != unified[k]) {
4614 new_unified_insn(iseq, unified[0], unified[1] - 1,
4618 niobj->link.prev = (LINK_ELEMENT *)iobj->link.prev;
4619 niobj->link.next = li;
4621 li->prev = (LINK_ELEMENT *)niobj;
4624 list->prev->next = (LINK_ELEMENT *)niobj;
4625 list = (LINK_ELEMENT *)niobj;
4638all_string_result_p(
const NODE *node)
4640 if (!node)
return FALSE;
4641 switch (nd_type(node)) {
4642 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4644 case NODE_IF:
case NODE_UNLESS:
4645 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4646 if (all_string_result_p(RNODE_IF(node)->nd_body))
4647 return all_string_result_p(RNODE_IF(node)->nd_else);
4649 case NODE_AND:
case NODE_OR:
4650 if (!RNODE_AND(node)->nd_2nd)
4651 return all_string_result_p(RNODE_AND(node)->nd_1st);
4652 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4654 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4661 rb_iseq_t *
const iseq;
4662 LINK_ANCHOR *
const ret;
4664 const NODE *lit_node;
4670append_dstr_fragment(
struct dstr_ctxt *args,
const NODE *
const node, rb_parser_string_t *str)
4672 VALUE s = rb_str_new_mutable_parser_string(str);
4674 VALUE error = rb_reg_check_preprocess(s);
4675 if (!
NIL_P(error)) {
4676 COMPILE_ERROR(args->iseq, nd_line(node),
"%" PRIsVALUE, error);
4680 if (
NIL_P(args->lit)) {
4682 args->lit_node = node;
4691flush_dstr_fragment(
struct dstr_ctxt *args)
4693 if (!
NIL_P(args->lit)) {
4694 rb_iseq_t *iseq = args->iseq;
4695 VALUE lit = args->lit;
4697 lit = rb_fstring(lit);
4698 ADD_INSN1(args->ret, args->lit_node, putobject, lit);
4705compile_dstr_fragments_0(
struct dstr_ctxt *args,
const NODE *
const node)
4707 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4708 rb_parser_string_t *str = RNODE_DSTR(node)->string;
4711 CHECK(append_dstr_fragment(args, node, str));
4715 const NODE *
const head = list->nd_head;
4716 if (nd_type_p(head, NODE_STR)) {
4717 CHECK(append_dstr_fragment(args, node, RNODE_STR(head)->
string));
4719 else if (nd_type_p(head, NODE_DSTR)) {
4720 CHECK(compile_dstr_fragments_0(args, head));
4723 flush_dstr_fragment(args);
4724 rb_iseq_t *iseq = args->iseq;
4725 CHECK(COMPILE(args->ret,
"each string", head));
4734compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int *cntp,
int dregx)
4737 .iseq = iseq, .ret = ret,
4738 .lit =
Qnil, .lit_node = NULL,
4739 .cnt = 0, .dregx = dregx,
4741 CHECK(compile_dstr_fragments_0(&args, node));
4742 flush_dstr_fragment(&args);
4750compile_block(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int popped)
4752 while (node && nd_type_p(node, NODE_BLOCK)) {
4753 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4754 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4755 node = RNODE_BLOCK(node)->nd_next;
4758 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4764compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node)
4767 if (!RNODE_DSTR(node)->nd_next) {
4768 VALUE lit = rb_node_dstr_string_val(node);
4769 ADD_INSN1(ret, node, putstring, lit);
4770 RB_OBJ_SET_SHAREABLE(lit);
4774 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, FALSE));
4775 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4781compile_dregx(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
4784 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
4786 if (!RNODE_DREGX(node)->nd_next) {
4788 VALUE src = rb_node_dregx_string_val(node);
4789 VALUE match = iseq_reg_compile(iseq, src, cflag, NULL, 0);
4790 ADD_INSN1(ret, node, putobject, match);
4796 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, TRUE));
4800 ADD_INSN(ret, node, pop);
4807compile_flip_flop(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int again,
4808 LABEL *then_label, LABEL *else_label)
4810 const int line = nd_line(node);
4811 LABEL *lend = NEW_LABEL(line);
4812 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4813 + VM_SVAR_FLIPFLOP_START;
4816 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4817 ADD_INSNL(ret, node, branchif, lend);
4820 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
4821 ADD_INSNL(ret, node, branchunless, else_label);
4822 ADD_INSN1(ret, node, putobject,
Qtrue);
4823 ADD_INSN1(ret, node, setspecial, key);
4825 ADD_INSNL(ret, node, jump, then_label);
4829 ADD_LABEL(ret, lend);
4830 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
4831 ADD_INSNL(ret, node, branchunless, then_label);
4832 ADD_INSN1(ret, node, putobject,
Qfalse);
4833 ADD_INSN1(ret, node, setspecial, key);
4834 ADD_INSNL(ret, node, jump, then_label);
4840compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *cond,
4841 LABEL *then_label, LABEL *else_label);
4843#define COMPILE_SINGLE 2
4845compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *cond,
4846 LABEL *then_label, LABEL *else_label)
4850 LABEL *label = NEW_LABEL(nd_line(cond));
4851 if (!then_label) then_label = label;
4852 else if (!else_label) else_label = label;
4854 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
4856 if (LIST_INSN_SIZE_ONE(seq)) {
4857 INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
4858 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
4861 if (!label->refcnt) {
4862 return COMPILE_SINGLE;
4864 ADD_LABEL(seq, label);
4870compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *cond,
4871 LABEL *then_label, LABEL *else_label)
4874 DECL_ANCHOR(ignore);
4877 switch (nd_type(cond)) {
4879 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
4880 cond = RNODE_AND(cond)->nd_2nd;
4881 if (ok == COMPILE_SINGLE) {
4882 ADD_INSNL(ret, cond, jump, else_label);
4883 INIT_ANCHOR(ignore);
4885 then_label = NEW_LABEL(nd_line(cond));
4889 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
4890 cond = RNODE_OR(cond)->nd_2nd;
4891 if (ok == COMPILE_SINGLE) {
4892 ADD_INSNL(ret, cond, jump, then_label);
4893 INIT_ANCHOR(ignore);
4895 else_label = NEW_LABEL(nd_line(cond));
4905 case NODE_IMAGINARY:
4912 ADD_INSNL(ret, cond, jump, then_label);
4917 ADD_INSNL(ret, cond, jump, else_label);
4923 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
4924 ADD_INSNL(ret, cond, jump, then_label);
4927 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
4930 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
4933 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
4937 DECL_ANCHOR(cond_seq);
4938 INIT_ANCHOR(cond_seq);
4940 CHECK(COMPILE(cond_seq,
"branch condition", cond));
4942 if (LIST_INSN_SIZE_ONE(cond_seq)) {
4943 INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
4944 if (insn->insn_id == BIN(putobject)) {
4945 if (
RTEST(insn->operands[0])) {
4946 ADD_INSNL(ret, cond, jump, then_label);
4951 ADD_INSNL(ret, cond, jump, else_label);
4956 ADD_SEQ(ret, cond_seq);
4961 ADD_INSNL(ret, cond, branchunless, else_label);
4962 ADD_INSNL(ret, cond, jump, then_label);
4969keyword_node_p(
const NODE *
const node)
4971 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
4975get_symbol_value(rb_iseq_t *iseq,
const NODE *node)
4977 switch (nd_type(node)) {
4979 return rb_node_sym_string_val(node);
4981 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
4986node_hash_unique_key_index(rb_iseq_t *iseq, rb_node_hash_t *node_hash,
int *count_ptr)
4988 NODE *node = node_hash->nd_head;
4992 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
4993 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
4994 VALUE idx = rb_hash_aref(hash, key);
4999 rb_hash_aset(hash, key,
INT2FIX(i));
5008compile_keyword_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
5009 const NODE *
const root_node,
5017 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
5018 const NODE *node = RNODE_HASH(root_node)->nd_head;
5022 const NODE *key_node = RNODE_LIST(node)->nd_head;
5026 if (key_node && nd_type_p(key_node, NODE_SYM)) {
5031 *flag |= VM_CALL_KW_SPLAT;
5032 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5037 *flag |= VM_CALL_KW_SPLAT_MUT;
5042 node = RNODE_LIST(node)->nd_next;
5043 node = RNODE_LIST(node)->nd_next;
5047 node = RNODE_HASH(root_node)->nd_head;
5050 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
5052 if (
len > VM_CALL_KW_LEN_MAX) {
5053 COMPILE_ERROR(ERROR_ARGS_AT(root_node)
"too many keyword arguments (%d, maximum is %d)",
5054 len, (
int)VM_CALL_KW_LEN_MAX);
5059 VALUE *keywords = kw_arg->keywords;
5062 kw_arg->references = 0;
5063 kw_arg->keyword_len =
len;
5065 *kw_arg_ptr = kw_arg;
5067 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5068 const NODE *key_node = RNODE_LIST(node)->nd_head;
5069 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5072 keywords[j] = get_symbol_value(iseq, key_node);
5076 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
5086compile_args(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node, NODE **kwnode_ptr)
5090 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
5092 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
5095 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5096 *kwnode_ptr = RNODE_LIST(node)->nd_head;
5099 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
5100 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
5108frozen_string_literal_p(
const rb_iseq_t *iseq)
5110 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
5114static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
5116 switch (nd_type(node)) {
5124 case NODE_IMAGINARY:
5131 return hash_key || frozen_string_literal_p(iseq);
5138static_literal_value(
const NODE *node, rb_iseq_t *iseq)
5140 switch (nd_type(node)) {
5143 VALUE lit = rb_node_integer_literal_val(node);
5149 VALUE lit = rb_node_float_literal_val(node);
5155 case NODE_IMAGINARY:
5164 return rb_node_sym_string_val(node);
5166 return RB_OBJ_SET_SHAREABLE(rb_node_regx_string_val(node));
5168 return rb_node_line_lineno_val(node);
5170 return rb_node_encoding_val(node);
5173 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
5174 VALUE lit = get_string_value(node);
5175 VALUE str = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
5176 RB_OBJ_SET_SHAREABLE(str);
5180 return get_string_value(node);
5183 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
5188compile_array(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int popped,
bool first_chunk)
5190 const NODE *line_node = node;
5192 if (nd_type_p(node, NODE_ZLIST)) {
5194 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5199 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5202 for (; node; node = RNODE_LIST(node)->nd_next) {
5203 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5245 const int max_stack_len = 0x100;
5246 const int min_tmp_ary_len = 0x40;
5250#define FLUSH_CHUNK \
5252 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5253 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5254 first_chunk = FALSE; \
5262 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5264 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5265 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5268 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5273 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5274 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5275 RB_OBJ_SET_FROZEN_SHAREABLE(ary);
5280 ADD_INSN1(ret, line_node, duparray, ary);
5281 first_chunk = FALSE;
5284 ADD_INSN1(ret, line_node, putobject, ary);
5285 ADD_INSN(ret, line_node, concattoarray);
5287 RB_OBJ_SET_SHAREABLE(ary);
5293 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5295 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5298 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5300 if (stack_len == 0 && first_chunk) {
5301 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5306 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5307 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5311 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5316 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5326static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5328 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);
5332compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped)
5334 const NODE *line_node = node;
5336 node = RNODE_HASH(node)->nd_head;
5338 if (!node || nd_type_p(node, NODE_ZLIST)) {
5340 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5345 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5348 for (; node; node = RNODE_LIST(node)->nd_next) {
5349 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5372 const int max_stack_len = 0x100;
5373 const int min_tmp_hash_len = 0x800;
5375 int first_chunk = 1;
5376 DECL_ANCHOR(anchor);
5377 INIT_ANCHOR(anchor);
5380#define FLUSH_CHUNK() \
5382 if (first_chunk) { \
5383 APPEND_LIST(ret, anchor); \
5384 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5387 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5388 ADD_INSN(ret, line_node, swap); \
5389 APPEND_LIST(ret, anchor); \
5390 ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
5392 INIT_ANCHOR(anchor); \
5393 first_chunk = stack_len = 0; \
5400 if (static_literal_node_pair_p(node, iseq)) {
5402 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5403 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5406 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5411 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5413 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5415 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5422 hash = RB_OBJ_SET_FROZEN_SHAREABLE(
rb_obj_hide(hash));
5427 ADD_INSN1(ret, line_node, duphash, hash);
5431 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5432 ADD_INSN(ret, line_node, swap);
5434 ADD_INSN1(ret, line_node, putobject, hash);
5436 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5443 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5446 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5449 if (RNODE_LIST(node)->nd_head) {
5451 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5452 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5456 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5462 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5463 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5464 int first_kw = first_chunk && stack_len == 0;
5465 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5466 int only_kw = last_kw && first_kw;
5468 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5470 if (only_kw && method_call_keywords) {
5478 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5480 else if (first_kw) {
5484 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5491 if (only_kw && method_call_keywords) {
5497 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5504 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5505 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5506 else ADD_INSN(ret, line_node, swap);
5508 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5510 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
5525rb_node_case_when_optimizable_literal(
const NODE *
const node)
5527 switch (nd_type(node)) {
5529 return rb_node_integer_literal_val(node);
5531 VALUE v = rb_node_float_literal_val(node);
5540 case NODE_IMAGINARY:
5549 return rb_node_sym_string_val(node);
5551 return rb_node_line_lineno_val(node);
5553 return rb_node_str_string_val(node);
5555 return rb_node_file_path_val(node);
5561when_vals(rb_iseq_t *iseq, LINK_ANCHOR *
const cond_seq,
const NODE *vals,
5562 LABEL *l1,
int only_special_literals,
VALUE literals)
5565 const NODE *val = RNODE_LIST(vals)->nd_head;
5566 VALUE lit = rb_node_case_when_optimizable_literal(val);
5569 only_special_literals = 0;
5571 else if (
NIL_P(rb_hash_lookup(literals, lit))) {
5572 rb_hash_aset(literals, lit, (
VALUE)(l1) | 1);
5575 if (nd_type_p(val, NODE_STR) || nd_type_p(val, NODE_FILE)) {
5576 debugp_param(
"nd_lit", get_string_value(val));
5577 lit = get_string_value(val);
5578 ADD_INSN1(cond_seq, val, putobject, lit);
5582 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5586 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5587 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5588 ADD_INSNL(cond_seq, val, branchif, l1);
5589 vals = RNODE_LIST(vals)->nd_next;
5591 return only_special_literals;
5595when_splat_vals(rb_iseq_t *iseq, LINK_ANCHOR *
const cond_seq,
const NODE *vals,
5596 LABEL *l1,
int only_special_literals,
VALUE literals)
5598 const NODE *line_node = vals;
5600 switch (nd_type(vals)) {
5602 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5606 ADD_INSN (cond_seq, line_node, dup);
5607 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5608 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5609 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5610 ADD_INSNL(cond_seq, line_node, branchif, l1);
5613 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5614 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5617 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5618 ADD_INSN (cond_seq, line_node, dup);
5619 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5620 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5621 ADD_INSNL(cond_seq, line_node, branchif, l1);
5624 ADD_INSN (cond_seq, line_node, dup);
5625 CHECK(COMPILE(cond_seq,
"when val", vals));
5626 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5627 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5628 ADD_INSNL(cond_seq, line_node, branchif, l1);
5721 const NODE *line_node;
5736add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc, INSN *before_insn)
5739 rb_bug(
"no masgn_state");
5748 memo->before_insn = before_insn;
5749 memo->line_node = line_node;
5750 memo->argn = state->num_args + 1;
5751 memo->num_args = argc;
5752 state->num_args += argc;
5753 memo->lhs_pos = lhs_pos;
5755 if (!state->first_memo) {
5756 state->first_memo = memo;
5759 state->last_memo->next = memo;
5761 state->last_memo = memo;
5766static 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);
5769compile_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)
5771 switch (nd_type(node)) {
5772 case NODE_ATTRASGN: {
5774 const NODE *line_node = node;
5776 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5778 bool safenav_call =
false;
5779 LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
5780 iobj = (INSN *)get_prev_insn((INSN *)insn_element);
5782 ELEM_REMOVE(insn_element);
5783 if (!IS_INSN_ID(iobj, send)) {
5784 safenav_call =
true;
5785 iobj = (INSN *)get_prev_insn(iobj);
5786 ELEM_INSERT_NEXT(&iobj->link, insn_element);
5788 (pre->last = iobj->link.prev)->next = 0;
5791 int argc = vm_ci_argc(ci) + 1;
5792 ci = ci_argc_set(iseq, ci, argc);
5793 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5797 ADD_INSN(lhs, line_node, swap);
5800 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5803 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (INSN *)LAST_ELEMENT(lhs))) {
5807 iobj->link.prev = lhs->last;
5808 lhs->last->next = &iobj->link;
5809 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
5810 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5811 int argc = vm_ci_argc(ci);
5812 bool dupsplat =
false;
5813 ci = ci_argc_set(iseq, ci, argc - 1);
5814 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
5821 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
5823 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5832 int line_no = nd_line(line_node);
5833 int node_id = nd_node_id(line_node);
5836 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5837 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
5838 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
5840 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
5842 if (!safenav_call) {
5843 ADD_INSN(lhs, line_node, pop);
5845 ADD_INSN(lhs, line_node, pop);
5848 for (
int i=0; i < argc; i++) {
5849 ADD_INSN(post, line_node, pop);
5854 DECL_ANCHOR(nest_rhs);
5855 INIT_ANCHOR(nest_rhs);
5856 DECL_ANCHOR(nest_lhs);
5857 INIT_ANCHOR(nest_lhs);
5859 int prev_level = state->lhs_level;
5860 bool prev_nested = state->nested;
5862 state->lhs_level = lhs_pos - 1;
5863 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5864 state->lhs_level = prev_level;
5865 state->nested = prev_nested;
5867 ADD_SEQ(lhs, nest_rhs);
5868 ADD_SEQ(lhs, nest_lhs);
5872 if (!RNODE_CDECL(node)->nd_vid) {
5876 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5878 LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
5879 iobj = (INSN *)insn_element;
5880 ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn((INSN *)get_prev_insn(iobj)));
5881 ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn(iobj));
5882 ELEM_REMOVE(insn_element);
5883 pre->last = iobj->link.prev;
5884 ADD_ELEM(lhs, (LINK_ELEMENT *)iobj);
5886 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (INSN *)LAST_ELEMENT(lhs))) {
5890 ADD_INSN(post, node, pop);
5895 DECL_ANCHOR(anchor);
5896 INIT_ANCHOR(anchor);
5897 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
5898 ELEM_REMOVE(FIRST_ELEMENT(anchor));
5899 ADD_SEQ(lhs, anchor);
5907compile_massign_opt_lhs(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *lhsn)
5910 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
5911 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
5917compile_massign_opt(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
5918 const NODE *rhsn,
const NODE *orig_lhsn)
5921 const int memsize = numberof(mem);
5923 int llen = 0, rlen = 0;
5925 const NODE *lhsn = orig_lhsn;
5927#define MEMORY(v) { \
5929 if (memindex == memsize) return 0; \
5930 for (i=0; i<memindex; i++) { \
5931 if (mem[i] == (v)) return 0; \
5933 mem[memindex++] = (v); \
5936 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
5941 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
5942 switch (nd_type(ln)) {
5947 MEMORY(get_nd_vid(ln));
5952 lhsn = RNODE_LIST(lhsn)->nd_next;
5958 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
5961 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
5963 rhsn = RNODE_LIST(rhsn)->nd_next;
5968 for (i=0; i<llen-rlen; i++) {
5969 ADD_INSN(ret, orig_lhsn, putnil);
5973 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
5978compile_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)
5980 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
5981 const NODE *splatn = RNODE_MASGN(node)->nd_args;
5982 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
5983 const NODE *lhsn_count = lhsn;
5984 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
5989 while (lhsn_count) {
5991 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
5994 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
5996 lhsn = RNODE_LIST(lhsn)->nd_next;
6000 if (nd_type_p(splatn, NODE_POSTARG)) {
6002 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
6003 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
6004 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
6006 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
6008 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
6010 if (NODE_NAMED_REST_P(restn)) {
6011 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
6014 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
6016 postn = RNODE_LIST(postn)->nd_next;
6021 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
6025 if (!state->nested) {
6026 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
6030 ADD_INSN(rhs, node, dup);
6032 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
6037compile_massign(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
6039 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
6041 state.lhs_level = popped ? 0 : 1;
6044 state.first_memo = NULL;
6045 state.last_memo = NULL;
6055 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
6059 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
6060 for (
int i = 0; i < memo->num_args; i++) {
6061 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
6063 tmp_memo = memo->next;
6072 if (!popped && state.num_args >= 1) {
6074 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
6082collect_const_segments(rb_iseq_t *iseq,
const NODE *node)
6086 switch (nd_type(node)) {
6088 rb_ary_unshift(arr,
ID2SYM(RNODE_CONST(node)->nd_vid));
6089 RB_OBJ_SET_SHAREABLE(arr);
6092 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6093 rb_ary_unshift(arr,
ID2SYM(idNULL));
6094 RB_OBJ_SET_SHAREABLE(arr);
6097 rb_ary_unshift(arr,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6098 node = RNODE_COLON2(node)->nd_head;
6107compile_const_prefix(rb_iseq_t *iseq,
const NODE *
const node,
6108 LINK_ANCHOR *
const pref, LINK_ANCHOR *
const body)
6110 switch (nd_type(node)) {
6112 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
6113 ADD_INSN1(body, node, putobject,
Qtrue);
6114 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
6117 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
6118 ADD_INSN(body, node, pop);
6119 ADD_INSN1(body, node, putobject, rb_cObject);
6120 ADD_INSN1(body, node, putobject,
Qtrue);
6121 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6124 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
6125 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
6126 ADD_INSN1(body, node, putobject,
Qfalse);
6127 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6130 CHECK(COMPILE(pref,
"const colon2 prefix", node));
6137compile_cpath(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
const NODE *cpath)
6139 if (nd_type_p(cpath, NODE_COLON3)) {
6141 ADD_INSN1(ret, cpath, putobject, rb_cObject);
6142 return VM_DEFINECLASS_FLAG_SCOPED;
6144 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
6146 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
6147 return VM_DEFINECLASS_FLAG_SCOPED;
6151 ADD_INSN1(ret, cpath, putspecialobject,
6152 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
6158private_recv_p(
const NODE *node)
6160 NODE *recv = get_nd_recv(node);
6161 if (recv && nd_type_p(recv, NODE_SELF)) {
6162 return RNODE_SELF(recv)->nd_state != 0;
6168defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6169 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
bool ignore);
6172compile_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);
6175defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6176 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
6179 enum defined_type expr_type = DEFINED_NOT_DEFINED;
6180 enum node_type
type;
6181 const int line = nd_line(node);
6182 const NODE *line_node = node;
6184 switch (
type = nd_type(node)) {
6188 expr_type = DEFINED_NIL;
6191 expr_type = DEFINED_SELF;
6194 expr_type = DEFINED_TRUE;
6197 expr_type = DEFINED_FALSE;
6202 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
6206 if (RNODE_LIST(vals)->nd_head) {
6207 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
6210 lfinish[1] = NEW_LABEL(line);
6212 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6214 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6227 case NODE_IMAGINARY:
6232 expr_type = DEFINED_EXPR;
6236 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6238 lfinish[1] = NEW_LABEL(line);
6240 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6241 expr_type = DEFINED_EXPR;
6247 expr_type = DEFINED_LVAR;
6250#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6252 ADD_INSN3(ret, line_node, definedivar,
6253 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6257 ADD_INSN(ret, line_node, putnil);
6258 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6259 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6263 ADD_INSN(ret, line_node, putnil);
6264 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6265 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6269 ADD_INSN(ret, line_node, putnil);
6270 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6271 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6275 lfinish[1] = NEW_LABEL(line);
6277 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6278 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6279 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6282 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6283 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6286 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6287 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6291 ADD_INSN1(ret, line_node, putobject, rb_cObject);
6292 ADD_INSN3(ret, line_node, defined,
6293 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6301 case NODE_ATTRASGN:{
6302 const int explicit_receiver =
6303 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6304 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6306 if (get_nd_args(node) || explicit_receiver) {
6308 lfinish[1] = NEW_LABEL(line);
6311 lfinish[2] = NEW_LABEL(line);
6314 if (get_nd_args(node)) {
6315 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6316 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6318 if (explicit_receiver) {
6319 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6320 switch (nd_type(get_nd_recv(node))) {
6326 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6327 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6330 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6331 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6335 ADD_INSN(ret, line_node, dup);
6337 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6338 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6341 ADD_INSN(ret, line_node, putself);
6343 ADD_INSN(ret, line_node, dup);
6345 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6346 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6352 ADD_INSN(ret, line_node, putnil);
6353 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6354 PUSH_VAL(DEFINED_YIELD));
6355 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6360 ADD_INSN(ret, line_node, putnil);
6361 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6362 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6363 PUSH_VAL(DEFINED_GVAR));
6368 ADD_INSN(ret, line_node, putnil);
6369 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6370 PUSH_VAL(DEFINED_ZSUPER));
6376 case NODE_OP_ASGN_OR:
6377 case NODE_OP_ASGN_AND:
6386 expr_type = DEFINED_ASGN;
6393 VALUE str = rb_iseq_defined_string(expr_type);
6394 ADD_INSN1(ret, line_node, putobject, str);
6397 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6402build_defined_rescue_iseq(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const void *unused)
6404 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6405 iseq_set_exception_local_table(iseq);
6409defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6410 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
bool ignore)
6412 LINK_ELEMENT *lcur = ret->last;
6413 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6415 int line = nd_line(node);
6416 LABEL *lstart = NEW_LABEL(line);
6417 LABEL *lend = NEW_LABEL(line);
6418 const rb_iseq_t *rescue;
6420 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6421 rescue = NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc,
6423 ISEQ_BODY(iseq)->location.label),
6424 ISEQ_TYPE_RESCUE, 0);
6425 lstart->rescued = LABEL_RESCUE_BEG;
6426 lend->rescued = LABEL_RESCUE_END;
6427 APPEND_LABEL(ret, lcur, lstart);
6428 ADD_LABEL(ret, lend);
6430 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6436compile_defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE needstr,
bool ignore)
6438 const int line = nd_line(node);
6439 const NODE *line_node = node;
6440 if (!RNODE_DEFINED(node)->nd_head) {
6441 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6442 ADD_INSN1(ret, line_node, putobject, str);
6446 LINK_ELEMENT *last = ret->last;
6447 lfinish[0] = NEW_LABEL(line);
6450 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6452 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6453 ADD_INSN(ret, line_node, swap);
6455 ADD_LABEL(ret, lfinish[2]);
6457 ADD_INSN(ret, line_node, pop);
6458 ADD_LABEL(ret, lfinish[1]);
6460 ADD_LABEL(ret, lfinish[0]);
6466make_name_for_block(
const rb_iseq_t *orig_iseq)
6469 const rb_iseq_t *iseq = orig_iseq;
6471 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6472 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6473 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6476 iseq = ISEQ_BODY(iseq)->parent_iseq;
6481 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6484 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6489push_ensure_entry(rb_iseq_t *iseq,
6493 enl->ensure_node = node;
6494 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6496 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6500add_ensure_range(rb_iseq_t *iseq,
struct ensure_range *erange,
6501 LABEL *lstart, LABEL *lend)
6506 while (erange->next != 0) {
6507 erange = erange->next;
6511 ne->end = erange->end;
6512 erange->end = lstart;
6518can_add_ensure_iseq(
const rb_iseq_t *iseq)
6521 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
6523 if (e->ensure_node)
return false;
6531add_ensure_iseq(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
int is_return)
6536 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6538 DECL_ANCHOR(ensure);
6540 INIT_ANCHOR(ensure);
6542 if (enlp->erange != NULL) {
6543 DECL_ANCHOR(ensure_part);
6544 LABEL *lstart = NEW_LABEL(0);
6545 LABEL *lend = NEW_LABEL(0);
6546 INIT_ANCHOR(ensure_part);
6548 add_ensure_range(iseq, enlp->erange, lstart, lend);
6550 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6551 ADD_LABEL(ensure_part, lstart);
6552 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6553 ADD_LABEL(ensure_part, lend);
6554 ADD_SEQ(ensure, ensure_part);
6563 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6564 ADD_SEQ(ret, ensure);
6569check_keyword(
const NODE *node)
6573 if (nd_type_p(node, NODE_LIST)) {
6574 while (RNODE_LIST(node)->nd_next) {
6575 node = RNODE_LIST(node)->nd_next;
6577 node = RNODE_LIST(node)->nd_head;
6580 return keyword_node_p(node);
6585keyword_node_single_splat_p(NODE *kwnode)
6589 NODE *node = RNODE_HASH(kwnode)->nd_head;
6590 return RNODE_LIST(node)->nd_head == NULL &&
6591 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6595compile_single_keyword_splat_mutable(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6596 NODE *kwnode,
unsigned int *flag_ptr)
6598 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6599 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6600 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6601 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6602 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6605#define SPLATARRAY_FALSE 0
6606#define SPLATARRAY_TRUE 1
6607#define DUP_SINGLE_KW_SPLAT 2
6610setup_args_core(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6611 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6613 if (!argn)
return 0;
6615 NODE *kwnode = NULL;
6617 switch (nd_type(argn)) {
6620 int len = compile_args(iseq, args, argn, &kwnode);
6621 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6624 if (compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6628 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6629 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6632 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6641 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6642 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6643 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6644 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6645 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6648 case NODE_ARGSCAT: {
6649 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6650 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6651 bool args_pushed =
false;
6653 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6654 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6655 if (kwnode) rest_len--;
6656 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6660 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6661 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6664 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6665 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6666 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6669 else if (!args_pushed) {
6670 ADD_INSN(args, argn, concattoarray);
6676 *flag_ptr |= VM_CALL_KW_SPLAT;
6677 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6683 case NODE_ARGSPUSH: {
6684 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6685 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6687 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6688 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6689 if (kwnode) rest_len--;
6690 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6691 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6694 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6695 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6698 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6699 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6705 *flag_ptr |= VM_CALL_KW_SPLAT;
6706 if (!keyword_node_single_splat_p(kwnode)) {
6707 *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6708 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6710 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
6711 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6714 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6722 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
6728setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
6730 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
6731 *flag |= VM_CALL_ARGS_SPLAT_MUT;
6736setup_args_dup_rest_p(
const NODE *argn)
6738 switch(nd_type(argn)) {
6749 case NODE_IMAGINARY:
6762 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
6765 if (setup_args_dup_rest_p(RNODE_LIST(argn)->nd_head)) {
6768 argn = RNODE_LIST(argn)->nd_next;
6777setup_args(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6781 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
6784 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
6785 RNODE_BLOCK_PASS(argn)->nd_head : argn;
6788 switch(nd_type(check_arg)) {
6791 dup_rest = SPLATARRAY_FALSE;
6795 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
6797 case(NODE_ARGSPUSH):
6799 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
6800 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
6801 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
6802 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
6803 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
6805 if (dup_rest == SPLATARRAY_FALSE) {
6807 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
6809 NODE *key_node = RNODE_LIST(node)->nd_head;
6810 if (key_node && setup_args_dup_rest_p(key_node)) {
6811 dup_rest = SPLATARRAY_TRUE;
6815 node = RNODE_LIST(node)->nd_next;
6816 NODE *value_node = RNODE_LIST(node)->nd_head;
6817 if (setup_args_dup_rest_p(value_node)) {
6818 dup_rest = SPLATARRAY_TRUE;
6822 node = RNODE_LIST(node)->nd_next;
6831 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
6833 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
6836 initial_dup_rest = dup_rest;
6838 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
6839 DECL_ANCHOR(arg_block);
6840 INIT_ANCHOR(arg_block);
6842 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
6843 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
6845 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
6846 const NODE * arg_node =
6847 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
6854 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
6855 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
6858 *flag |= VM_CALL_FORWARDING;
6860 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
6861 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6865 *flag |= VM_CALL_ARGS_BLOCKARG;
6867 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
6870 if (LIST_INSN_SIZE_ONE(arg_block)) {
6871 LINK_ELEMENT *elem = FIRST_ELEMENT(arg_block);
6872 if (IS_INSN(elem)) {
6873 INSN *iobj = (INSN *)elem;
6874 if (iobj->insn_id == BIN(getblockparam)) {
6875 iobj->insn_id = BIN(getblockparamproxy);
6879 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
6880 ADD_SEQ(args, arg_block);
6883 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
6885 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
6890build_postexe_iseq(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const void *ptr)
6892 const NODE *body = ptr;
6893 int line = nd_line(body);
6895 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
6897 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6898 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
6900 iseq_set_local_table(iseq, 0, 0);
6904compile_named_capture_assign(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node)
6908 int line = nd_line(node);
6909 const NODE *line_node = node;
6910 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
6912#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
6913 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
6917 ADD_INSN(ret, line_node, dup);
6918 ADD_INSNL(ret, line_node, branchunless, fail_label);
6920 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6922 if (RNODE_BLOCK(vars)->nd_next) {
6923 ADD_INSN(ret, line_node, dup);
6926 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6928 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
6930 ELEM_INSERT_PREV(last->next, (LINK_ELEMENT *)cap);
6931#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
6932 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
6937 ADD_INSNL(nom, line_node, jump, end_label);
6938 ADD_LABEL(nom, fail_label);
6940 ADD_INSN(nom, line_node, pop);
6941 ADD_INSN(nom, line_node, putnil);
6943 ADD_LABEL(nom, end_label);
6944 (nom->last->next = cap->link.next)->prev = nom->last;
6945 (cap->link.next = nom->anchor.next)->prev = &cap->link;
6950 ADD_INSNL(ret, line_node, jump, end_label);
6951 ADD_LABEL(ret, fail_label);
6952 ADD_INSN(ret, line_node, pop);
6953 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
6955 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
6957 ((INSN*)last)->insn_id = BIN(putnil);
6958 ((INSN*)last)->operand_size = 0;
6960 ADD_LABEL(ret, end_label);
6964optimizable_range_item_p(
const NODE *n)
6966 if (!n)
return FALSE;
6967 switch (nd_type(n)) {
6980optimized_range_item(
const NODE *n)
6982 switch (nd_type(n)) {
6984 return rb_node_line_lineno_val(n);
6986 return rb_node_integer_literal_val(n);
6988 return rb_node_float_literal_val(n);
6990 return rb_node_rational_literal_val(n);
6991 case NODE_IMAGINARY:
6992 return rb_node_imaginary_literal_val(n);
6996 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
7001compile_if(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
7003 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
7004 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
7006 const int line = nd_line(node);
7007 const NODE *line_node = node;
7008 DECL_ANCHOR(cond_seq);
7009 LABEL *then_label, *else_label, *end_label;
7012 INIT_ANCHOR(cond_seq);
7013 then_label = NEW_LABEL(line);
7014 else_label = NEW_LABEL(line);
7017 NODE *cond = RNODE_IF(node)->nd_cond;
7018 if (nd_type(cond) == NODE_BLOCK) {
7019 cond = RNODE_BLOCK(cond)->nd_head;
7022 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
7023 ADD_SEQ(ret, cond_seq);
7025 if (then_label->refcnt && else_label->refcnt) {
7026 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
7029 if (then_label->refcnt) {
7030 ADD_LABEL(ret, then_label);
7032 DECL_ANCHOR(then_seq);
7033 INIT_ANCHOR(then_seq);
7034 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
7036 if (else_label->refcnt) {
7037 const NODE *
const coverage_node = node_body ? node_body : node;
7038 add_trace_branch_coverage(
7041 nd_code_loc(coverage_node),
7042 nd_node_id(coverage_node),
7044 type == NODE_IF ?
"then" :
"else",
7046 end_label = NEW_LABEL(line);
7047 ADD_INSNL(then_seq, line_node, jump, end_label);
7049 ADD_INSN(then_seq, line_node, pop);
7052 ADD_SEQ(ret, then_seq);
7055 if (else_label->refcnt) {
7056 ADD_LABEL(ret, else_label);
7058 DECL_ANCHOR(else_seq);
7059 INIT_ANCHOR(else_seq);
7060 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
7062 if (then_label->refcnt) {
7063 const NODE *
const coverage_node = node_else ? node_else : node;
7064 add_trace_branch_coverage(
7067 nd_code_loc(coverage_node),
7068 nd_node_id(coverage_node),
7070 type == NODE_IF ?
"else" :
"then",
7073 ADD_SEQ(ret, else_seq);
7077 ADD_LABEL(ret, end_label);
7084compile_case(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
7087 const NODE *node = orig_node;
7088 LABEL *endlabel, *elselabel;
7090 DECL_ANCHOR(body_seq);
7091 DECL_ANCHOR(cond_seq);
7092 int only_special_literals = 1;
7095 enum node_type
type;
7096 const NODE *line_node;
7101 INIT_ANCHOR(body_seq);
7102 INIT_ANCHOR(cond_seq);
7104 RHASH_TBL_RAW(literals)->type = &cdhash_type;
7106 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
7108 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
7110 node = RNODE_CASE(node)->nd_body;
7111 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
7112 type = nd_type(node);
7113 line = nd_line(node);
7116 endlabel = NEW_LABEL(line);
7117 elselabel = NEW_LABEL(line);
7121 while (
type == NODE_WHEN) {
7124 l1 = NEW_LABEL(line);
7125 ADD_LABEL(body_seq, l1);
7126 ADD_INSN(body_seq, line_node, pop);
7128 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7129 add_trace_branch_coverage(
7132 nd_code_loc(coverage_node),
7133 nd_node_id(coverage_node),
7138 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
7139 ADD_INSNL(body_seq, line_node, jump, endlabel);
7141 vals = RNODE_WHEN(node)->nd_head;
7143 switch (nd_type(vals)) {
7145 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
7146 if (only_special_literals < 0)
return COMPILE_NG;
7151 only_special_literals = 0;
7152 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
7155 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
7159 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
7162 node = RNODE_WHEN(node)->nd_next;
7166 type = nd_type(node);
7167 line = nd_line(node);
7172 ADD_LABEL(cond_seq, elselabel);
7173 ADD_INSN(cond_seq, line_node, pop);
7174 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
7175 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7176 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7179 debugs(
"== else (implicit)\n");
7180 ADD_LABEL(cond_seq, elselabel);
7181 ADD_INSN(cond_seq, orig_node, pop);
7182 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
7184 ADD_INSN(cond_seq, orig_node, putnil);
7186 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7189 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7190 ADD_INSN(ret, orig_node, dup);
7192 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
7194 LABEL_REF(elselabel);
7197 ADD_SEQ(ret, cond_seq);
7198 ADD_SEQ(ret, body_seq);
7199 ADD_LABEL(ret, endlabel);
7204compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
7208 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
7210 DECL_ANCHOR(body_seq);
7214 branches = decl_branch_base(iseq, PTR2NUM(orig_node), nd_code_loc(orig_node),
"case");
7216 INIT_ANCHOR(body_seq);
7217 endlabel = NEW_LABEL(nd_line(node));
7219 while (node && nd_type_p(node, NODE_WHEN)) {
7220 const int line = nd_line(node);
7221 LABEL *l1 = NEW_LABEL(line);
7222 ADD_LABEL(body_seq, l1);
7224 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7225 add_trace_branch_coverage(
7228 nd_code_loc(coverage_node),
7229 nd_node_id(coverage_node),
7234 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7235 ADD_INSNL(body_seq, node, jump, endlabel);
7237 vals = RNODE_WHEN(node)->nd_head;
7239 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7241 switch (nd_type(vals)) {
7245 val = RNODE_LIST(vals)->nd_head;
7246 lnext = NEW_LABEL(nd_line(val));
7247 debug_compile(
"== when2\n", (
void)0);
7248 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7249 ADD_LABEL(ret, lnext);
7250 vals = RNODE_LIST(vals)->nd_next;
7256 ADD_INSN(ret, vals, putnil);
7257 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7258 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7259 ADD_INSNL(ret, vals, branchif, l1);
7262 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7264 node = RNODE_WHEN(node)->nd_next;
7267 const NODE *
const coverage_node = node ? node : orig_node;
7268 add_trace_branch_coverage(
7271 nd_code_loc(coverage_node),
7272 nd_node_id(coverage_node),
7276 CHECK(COMPILE_(ret,
"else", node, popped));
7277 ADD_INSNL(ret, orig_node, jump, endlabel);
7279 ADD_SEQ(ret, body_seq);
7280 ADD_LABEL(ret, endlabel);
7284static 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);
7286static 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);
7287static 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);
7288static int iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7289static 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);
7290static int iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7292#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7293#define CASE3_BI_OFFSET_ERROR_STRING 1
7294#define CASE3_BI_OFFSET_KEY_ERROR_P 2
7295#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7296#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7299iseq_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)
7301 const int line = nd_line(node);
7302 const NODE *line_node = node;
7304 switch (nd_type(node)) {
7358 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7359 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7360 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7362 const int min_argc = pre_args_num + post_args_num;
7363 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7364 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7366 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7368 match_failed = NEW_LABEL(line);
7369 type_error = NEW_LABEL(line);
7370 deconstruct = NEW_LABEL(line);
7371 deconstructed = NEW_LABEL(line);
7374 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7375 ADD_INSN(ret, line_node, swap);
7381 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7383 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7385 ADD_INSN(ret, line_node, dup);
7386 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7387 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7388 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7389 if (in_single_pattern) {
7390 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7391 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7392 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7393 INT2FIX(min_argc), base_index + 1 ));
7395 ADD_INSNL(ret, line_node, branchunless, match_failed);
7397 for (i = 0; i < pre_args_num; i++) {
7398 ADD_INSN(ret, line_node, dup);
7399 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7400 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7401 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7402 args = RNODE_LIST(args)->nd_next;
7405 if (RNODE_ARYPTN(node)->rest_arg) {
7406 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7407 ADD_INSN(ret, line_node, dup);
7408 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7409 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7410 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7411 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7412 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7413 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7414 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7416 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7419 if (post_args_num > 0) {
7420 ADD_INSN(ret, line_node, dup);
7421 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7422 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7423 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7424 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7425 ADD_INSN(ret, line_node, pop);
7430 args = RNODE_ARYPTN(node)->post_args;
7431 for (i = 0; i < post_args_num; i++) {
7432 ADD_INSN(ret, line_node, dup);
7434 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7435 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7436 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7438 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7439 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7440 args = RNODE_LIST(args)->nd_next;
7443 ADD_INSN(ret, line_node, pop);
7445 ADD_INSN(ret, line_node, pop);
7447 ADD_INSNL(ret, line_node, jump, matched);
7448 ADD_INSN(ret, line_node, putnil);
7450 ADD_INSN(ret, line_node, putnil);
7453 ADD_LABEL(ret, type_error);
7454 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7456 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7457 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7458 ADD_INSN(ret, line_node, pop);
7460 ADD_LABEL(ret, match_failed);
7461 ADD_INSN(ret, line_node, pop);
7463 ADD_INSN(ret, line_node, pop);
7465 ADD_INSNL(ret, line_node, jump, unmatched);
7518 const NODE *args = RNODE_FNDPTN(node)->args;
7519 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7521 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7522 match_failed = NEW_LABEL(line);
7523 type_error = NEW_LABEL(line);
7524 deconstruct = NEW_LABEL(line);
7525 deconstructed = NEW_LABEL(line);
7527 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7529 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7531 ADD_INSN(ret, line_node, dup);
7532 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7533 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7534 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7535 if (in_single_pattern) {
7536 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 ));
7538 ADD_INSNL(ret, line_node, branchunless, match_failed);
7541 LABEL *while_begin = NEW_LABEL(nd_line(node));
7542 LABEL *next_loop = NEW_LABEL(nd_line(node));
7543 LABEL *find_succeeded = NEW_LABEL(line);
7544 LABEL *find_failed = NEW_LABEL(nd_line(node));
7547 ADD_INSN(ret, line_node, dup);
7548 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7550 ADD_INSN(ret, line_node, dup);
7551 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7552 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7554 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7556 ADD_LABEL(ret, while_begin);
7558 ADD_INSN(ret, line_node, dup);
7559 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7560 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7561 ADD_INSNL(ret, line_node, branchunless, find_failed);
7563 for (j = 0; j < args_num; j++) {
7564 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7565 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7567 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7568 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7570 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7572 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7573 args = RNODE_LIST(args)->nd_next;
7576 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7577 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7578 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7579 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7580 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7581 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));
7583 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7584 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7585 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7586 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7587 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7588 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7589 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7590 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));
7592 ADD_INSNL(ret, line_node, jump, find_succeeded);
7594 ADD_LABEL(ret, next_loop);
7595 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7596 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7597 ADD_INSNL(ret, line_node, jump, while_begin);
7599 ADD_LABEL(ret, find_failed);
7600 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7601 if (in_single_pattern) {
7602 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7603 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7604 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7605 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7606 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7608 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7609 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7611 ADD_INSN(ret, line_node, pop);
7612 ADD_INSN(ret, line_node, pop);
7614 ADD_INSNL(ret, line_node, jump, match_failed);
7615 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7617 ADD_LABEL(ret, find_succeeded);
7618 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7621 ADD_INSN(ret, line_node, pop);
7622 ADD_INSNL(ret, line_node, jump, matched);
7623 ADD_INSN(ret, line_node, putnil);
7625 ADD_LABEL(ret, type_error);
7626 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7628 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7629 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7630 ADD_INSN(ret, line_node, pop);
7632 ADD_LABEL(ret, match_failed);
7633 ADD_INSN(ret, line_node, pop);
7634 ADD_INSNL(ret, line_node, jump, unmatched);
7698 LABEL *match_failed, *type_error;
7701 match_failed = NEW_LABEL(line);
7702 type_error = NEW_LABEL(line);
7704 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
7705 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7706 keys =
rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
7708 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
7709 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
7713 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7715 ADD_INSN(ret, line_node, dup);
7716 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
7717 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7718 if (in_single_pattern) {
7719 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
7721 ADD_INSNL(ret, line_node, branchunless, match_failed);
7724 ADD_INSN(ret, line_node, putnil);
7727 RB_OBJ_SET_FROZEN_SHAREABLE(keys);
7728 ADD_INSN1(ret, line_node, duparray, keys);
7731 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
7733 ADD_INSN(ret, line_node, dup);
7735 ADD_INSNL(ret, line_node, branchunless, type_error);
7737 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7738 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
7741 if (RNODE_HSHPTN(node)->nd_pkwargs) {
7745 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7747 DECL_ANCHOR(match_values);
7748 INIT_ANCHOR(match_values);
7749 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
7750 for (i = 0; i < keys_num; i++) {
7751 NODE *key_node = RNODE_LIST(args)->nd_head;
7752 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
7753 VALUE key = get_symbol_value(iseq, key_node);
7755 ADD_INSN(ret, line_node, dup);
7756 ADD_INSN1(ret, line_node, putobject, key);
7757 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
7758 if (in_single_pattern) {
7759 LABEL *match_succeeded;
7760 match_succeeded = NEW_LABEL(line);
7762 ADD_INSN(ret, line_node, dup);
7763 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7766 ADD_INSN1(ret, line_node, putobject, RB_OBJ_SET_SHAREABLE(str));
7767 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
7768 ADD_INSN1(ret, line_node, putobject,
Qtrue);
7769 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
7770 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7771 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
7772 ADD_INSN1(ret, line_node, putobject, key);
7773 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
7775 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
7777 ADD_LABEL(ret, match_succeeded);
7779 ADD_INSNL(ret, line_node, branchunless, match_failed);
7781 ADD_INSN(match_values, line_node, dup);
7782 ADD_INSN1(match_values, line_node, putobject, key);
7783 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
7784 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7785 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
7787 ADD_SEQ(ret, match_values);
7791 ADD_INSN(ret, line_node, dup);
7792 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7793 if (in_single_pattern) {
7794 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
7796 ADD_INSNL(ret, line_node, branchunless, match_failed);
7799 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
7800 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
7801 ADD_INSN(ret, line_node, dup);
7802 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
7803 if (in_single_pattern) {
7804 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
7806 ADD_INSNL(ret, line_node, branchunless, match_failed);
7809 ADD_INSN(ret, line_node, dup);
7810 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7814 ADD_INSN(ret, line_node, pop);
7815 ADD_INSNL(ret, line_node, jump, matched);
7816 ADD_INSN(ret, line_node, putnil);
7818 ADD_LABEL(ret, type_error);
7819 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7821 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
7822 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7823 ADD_INSN(ret, line_node, pop);
7825 ADD_LABEL(ret, match_failed);
7826 ADD_INSN(ret, line_node, pop);
7827 ADD_INSNL(ret, line_node, jump, unmatched);
7836 case NODE_IMAGINARY:
7864 CHECK(COMPILE(ret,
"case in literal", node));
7865 if (in_single_pattern) {
7866 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7868 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7869 if (in_single_pattern) {
7870 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
7872 ADD_INSNL(ret, line_node, branchif, matched);
7873 ADD_INSNL(ret, line_node, jump, unmatched);
7877 ID id = RNODE_LASGN(node)->nd_vid;
7878 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
7880 if (in_alt_pattern) {
7881 const char *name = rb_id2name(
id);
7882 if (name && strlen(name) > 0 && name[0] !=
'_') {
7883 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7889 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
7890 ADD_INSNL(ret, line_node, jump, matched);
7895 ID id = RNODE_DASGN(node)->nd_vid;
7897 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
7899 if (in_alt_pattern) {
7900 const char *name = rb_id2name(
id);
7901 if (name && strlen(name) > 0 && name[0] !=
'_') {
7902 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
7909 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
7913 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
7914 ADD_INSNL(ret, line_node, jump, matched);
7919 LABEL *match_failed;
7920 match_failed = unmatched;
7921 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7922 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
7923 if (in_single_pattern) {
7924 LABEL *match_succeeded;
7925 match_succeeded = NEW_LABEL(line);
7927 ADD_INSN(ret, line_node, dup);
7928 if (nd_type_p(node, NODE_IF)) {
7929 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7932 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
7935 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
7936 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7937 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7938 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7940 ADD_INSN(ret, line_node, pop);
7941 ADD_INSN(ret, line_node, pop);
7943 ADD_LABEL(ret, match_succeeded);
7945 if (nd_type_p(node, NODE_IF)) {
7946 ADD_INSNL(ret, line_node, branchunless, match_failed);
7949 ADD_INSNL(ret, line_node, branchif, match_failed);
7951 ADD_INSNL(ret, line_node, jump, matched);
7956 LABEL *match_failed;
7957 match_failed = NEW_LABEL(line);
7959 n = RNODE_HASH(node)->nd_head;
7960 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
7961 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7965 ADD_INSN(ret, line_node, dup);
7966 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));
7967 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));
7968 ADD_INSN(ret, line_node, putnil);
7970 ADD_LABEL(ret, match_failed);
7971 ADD_INSN(ret, line_node, pop);
7972 ADD_INSNL(ret, line_node, jump, unmatched);
7976 LABEL *match_succeeded, *fin;
7977 match_succeeded = NEW_LABEL(line);
7978 fin = NEW_LABEL(line);
7980 ADD_INSN(ret, line_node, dup);
7981 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));
7982 ADD_LABEL(ret, match_succeeded);
7983 ADD_INSN(ret, line_node, pop);
7984 ADD_INSNL(ret, line_node, jump, matched);
7985 ADD_INSN(ret, line_node, putnil);
7986 ADD_LABEL(ret, fin);
7987 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
7991 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
7997iseq_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)
7999 LABEL *fin = NEW_LABEL(nd_line(node));
8000 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
8001 ADD_LABEL(ret, fin);
8006iseq_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)
8008 const NODE *line_node = node;
8010 if (RNODE_ARYPTN(node)->nd_pconst) {
8011 ADD_INSN(ret, line_node, dup);
8012 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
8013 if (in_single_pattern) {
8014 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
8016 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
8017 if (in_single_pattern) {
8018 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
8020 ADD_INSNL(ret, line_node, branchunless, match_failed);
8027iseq_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)
8029 const NODE *line_node = node;
8033 if (use_deconstructed_cache) {
8035 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8036 ADD_INSNL(ret, line_node, branchnil, deconstruct);
8039 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8040 ADD_INSNL(ret, line_node, branchunless, match_failed);
8043 ADD_INSN(ret, line_node, pop);
8044 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
8045 ADD_INSNL(ret, line_node, jump, deconstructed);
8048 ADD_INSNL(ret, line_node, jump, deconstruct);
8051 ADD_LABEL(ret, deconstruct);
8052 ADD_INSN(ret, line_node, dup);
8053 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
8054 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
8057 if (use_deconstructed_cache) {
8058 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
8061 if (in_single_pattern) {
8062 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
8065 ADD_INSNL(ret, line_node, branchunless, match_failed);
8067 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
8070 if (use_deconstructed_cache) {
8071 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8074 ADD_INSN(ret, line_node, dup);
8076 ADD_INSNL(ret, line_node, branchunless, type_error);
8078 ADD_LABEL(ret, deconstructed);
8084iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index)
8094 const int line = nd_line(node);
8095 const NODE *line_node = node;
8096 LABEL *match_succeeded = NEW_LABEL(line);
8098 ADD_INSN(ret, line_node, dup);
8099 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8101 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8102 ADD_INSN1(ret, line_node, putobject, errmsg);
8103 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8104 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
8105 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8107 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8108 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8110 ADD_INSN(ret, line_node, pop);
8111 ADD_INSN(ret, line_node, pop);
8112 ADD_LABEL(ret, match_succeeded);
8118iseq_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)
8128 const int line = nd_line(node);
8129 const NODE *line_node = node;
8130 LABEL *match_succeeded = NEW_LABEL(line);
8132 ADD_INSN(ret, line_node, dup);
8133 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8135 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8136 ADD_INSN1(ret, line_node, putobject, errmsg);
8137 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8138 ADD_INSN(ret, line_node, dup);
8139 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
8140 ADD_INSN1(ret, line_node, putobject, pattern_length);
8141 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
8142 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8144 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8145 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8147 ADD_INSN(ret, line_node, pop);
8148 ADD_INSN(ret, line_node, pop);
8149 ADD_LABEL(ret, match_succeeded);
8155iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
8165 const int line = nd_line(node);
8166 const NODE *line_node = node;
8167 LABEL *match_succeeded = NEW_LABEL(line);
8169 ADD_INSN(ret, line_node, dup);
8170 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8172 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8173 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
8174 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8175 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
8176 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
8177 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8179 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8180 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8182 ADD_INSN(ret, line_node, pop);
8183 ADD_INSN(ret, line_node, pop);
8185 ADD_LABEL(ret, match_succeeded);
8186 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
8187 ADD_INSN(ret, line_node, pop);
8188 ADD_INSN(ret, line_node, pop);
8194compile_case3(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
8196 const NODE *pattern;
8197 const NODE *node = orig_node;
8198 LABEL *endlabel, *elselabel;
8200 DECL_ANCHOR(body_seq);
8201 DECL_ANCHOR(cond_seq);
8203 enum node_type
type;
8204 const NODE *line_node;
8207 bool single_pattern;
8210 INIT_ANCHOR(body_seq);
8211 INIT_ANCHOR(cond_seq);
8213 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"case");
8215 node = RNODE_CASE3(node)->nd_body;
8216 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
8217 type = nd_type(node);
8218 line = nd_line(node);
8220 single_pattern = !RNODE_IN(node)->nd_next;
8222 endlabel = NEW_LABEL(line);
8223 elselabel = NEW_LABEL(line);
8225 if (single_pattern) {
8227 ADD_INSN(head, line_node, putnil);
8228 ADD_INSN(head, line_node, putnil);
8229 ADD_INSN1(head, line_node, putobject,
Qfalse);
8230 ADD_INSN(head, line_node, putnil);
8232 ADD_INSN(head, line_node, putnil);
8234 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8238 while (
type == NODE_IN) {
8242 ADD_INSN(body_seq, line_node, putnil);
8244 l1 = NEW_LABEL(line);
8245 ADD_LABEL(body_seq, l1);
8246 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8248 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8249 add_trace_branch_coverage(
8252 nd_code_loc(coverage_node),
8253 nd_node_id(coverage_node),
8258 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8259 ADD_INSNL(body_seq, line_node, jump, endlabel);
8261 pattern = RNODE_IN(node)->nd_head;
8263 int pat_line = nd_line(pattern);
8264 LABEL *next_pat = NEW_LABEL(pat_line);
8265 ADD_INSN (cond_seq, pattern, dup);
8267 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8268 ADD_LABEL(cond_seq, next_pat);
8269 LABEL_UNREMOVABLE(next_pat);
8272 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8276 node = RNODE_IN(node)->nd_next;
8280 type = nd_type(node);
8281 line = nd_line(node);
8286 ADD_LABEL(cond_seq, elselabel);
8287 ADD_INSN(cond_seq, line_node, pop);
8288 ADD_INSN(cond_seq, line_node, pop);
8289 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8290 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8291 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8292 ADD_INSN(cond_seq, line_node, putnil);
8294 ADD_INSN(cond_seq, line_node, putnil);
8298 debugs(
"== else (implicit)\n");
8299 ADD_LABEL(cond_seq, elselabel);
8300 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8301 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8303 if (single_pattern) {
8311 LABEL *key_error, *fin;
8314 key_error = NEW_LABEL(line);
8315 fin = NEW_LABEL(line);
8318 kw_arg->references = 0;
8319 kw_arg->keyword_len = 2;
8320 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
8321 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
8323 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8324 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8326 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8327 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8328 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8329 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8330 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8331 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8332 ADD_INSNL(cond_seq, orig_node, jump, fin);
8334 ADD_LABEL(cond_seq, key_error);
8336 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8337 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8338 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8339 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8340 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8341 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8342 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8343 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
8344 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8346 ADD_LABEL(cond_seq, fin);
8350 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8351 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8353 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8355 ADD_INSN(cond_seq, orig_node, putnil);
8357 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8358 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8360 ADD_INSN(cond_seq, line_node, putnil);
8364 ADD_SEQ(ret, cond_seq);
8365 ADD_SEQ(ret, body_seq);
8366 ADD_LABEL(ret, endlabel);
8370#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8371#undef CASE3_BI_OFFSET_ERROR_STRING
8372#undef CASE3_BI_OFFSET_KEY_ERROR_P
8373#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8374#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8377compile_loop(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
8379 const int line = (int)nd_line(node);
8380 const NODE *line_node = node;
8382 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8383 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8384 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8385 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8390 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8391 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8392 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8393 LABEL *end_label = NEW_LABEL(line);
8394 LABEL *adjust_label = NEW_LABEL(line);
8396 LABEL *next_catch_label = NEW_LABEL(line);
8397 LABEL *tmp_label = NULL;
8399 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8400 push_ensure_entry(iseq, &enl, NULL, NULL);
8402 if (RNODE_WHILE(node)->nd_state == 1) {
8403 ADD_INSNL(ret, line_node, jump, next_label);
8406 tmp_label = NEW_LABEL(line);
8407 ADD_INSNL(ret, line_node, jump, tmp_label);
8409 ADD_LABEL(ret, adjust_label);
8410 ADD_INSN(ret, line_node, putnil);
8411 ADD_LABEL(ret, next_catch_label);
8412 ADD_INSN(ret, line_node, pop);
8413 ADD_INSNL(ret, line_node, jump, next_label);
8414 if (tmp_label) ADD_LABEL(ret, tmp_label);
8416 ADD_LABEL(ret, redo_label);
8417 branches = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8419 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8420 add_trace_branch_coverage(
8423 nd_code_loc(coverage_node),
8424 nd_node_id(coverage_node),
8429 CHECK(COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body));
8430 ADD_LABEL(ret, next_label);
8432 if (
type == NODE_WHILE) {
8433 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8434 redo_label, end_label));
8438 CHECK(compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8439 end_label, redo_label));
8442 ADD_LABEL(ret, end_label);
8443 ADD_ADJUST_RESTORE(ret, adjust_label);
8445 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8447 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8451 ADD_INSN(ret, line_node, putnil);
8454 ADD_LABEL(ret, break_label);
8457 ADD_INSN(ret, line_node, pop);
8460 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8462 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8464 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8465 ISEQ_COMPILE_DATA(iseq)->redo_label);
8467 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8468 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8469 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8470 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8471 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
8476compile_iter(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8478 const int line = nd_line(node);
8479 const NODE *line_node = node;
8480 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8481 LABEL *retry_label = NEW_LABEL(line);
8482 LABEL *retry_end_l = NEW_LABEL(line);
8483 const rb_iseq_t *child_iseq;
8485 ADD_LABEL(ret, retry_label);
8486 if (nd_type_p(node, NODE_FOR)) {
8487 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8489 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8490 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8491 ISEQ_TYPE_BLOCK, line);
8492 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8495 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8496 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8497 ISEQ_TYPE_BLOCK, line);
8498 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8511 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
8512 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
8513 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8514 iobj = (INSN*) get_prev_insn(iobj);
8516 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
8520 if (&iobj->link == LAST_ELEMENT(ret)) {
8521 ret->last = (LINK_ELEMENT*) retry_end_l;
8526 ADD_INSN(ret, line_node, pop);
8529 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8531 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8536compile_for_masgn(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8541 const NODE *line_node = node;
8542 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8543 LABEL *not_single = NEW_LABEL(nd_line(var));
8544 LABEL *not_ary = NEW_LABEL(nd_line(var));
8545 CHECK(COMPILE(ret,
"for var", var));
8546 ADD_INSN(ret, line_node, dup);
8547 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8548 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8549 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8550 ADD_INSNL(ret, line_node, branchunless, not_single);
8551 ADD_INSN(ret, line_node, dup);
8552 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8553 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8554 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8555 ADD_INSN(ret, line_node, swap);
8556 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
8557 ADD_INSN(ret, line_node, dup);
8558 ADD_INSNL(ret, line_node, branchunless, not_ary);
8559 ADD_INSN(ret, line_node, swap);
8560 ADD_LABEL(ret, not_ary);
8561 ADD_INSN(ret, line_node, pop);
8562 ADD_LABEL(ret, not_single);
8567compile_break(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8569 const NODE *line_node = node;
8570 unsigned long throw_flag = 0;
8572 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8574 LABEL *splabel = NEW_LABEL(0);
8575 ADD_LABEL(ret, splabel);
8576 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8577 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8578 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8579 add_ensure_iseq(ret, iseq, 0);
8580 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8581 ADD_ADJUST_RESTORE(ret, splabel);
8584 ADD_INSN(ret, line_node, putnil);
8588 const rb_iseq_t *ip = iseq;
8591 if (!ISEQ_COMPILE_DATA(ip)) {
8596 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8597 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8599 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8602 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8603 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8607 ip = ISEQ_BODY(ip)->parent_iseq;
8612 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8613 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8615 ADD_INSN(ret, line_node, pop);
8619 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8626compile_next(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8628 const NODE *line_node = node;
8629 unsigned long throw_flag = 0;
8631 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8632 LABEL *splabel = NEW_LABEL(0);
8633 debugs(
"next in while loop\n");
8634 ADD_LABEL(ret, splabel);
8635 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8636 add_ensure_iseq(ret, iseq, 0);
8637 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8638 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8639 ADD_ADJUST_RESTORE(ret, splabel);
8641 ADD_INSN(ret, line_node, putnil);
8644 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8645 LABEL *splabel = NEW_LABEL(0);
8646 debugs(
"next in block\n");
8647 ADD_LABEL(ret, splabel);
8648 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8649 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8650 add_ensure_iseq(ret, iseq, 0);
8651 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8652 ADD_ADJUST_RESTORE(ret, splabel);
8655 ADD_INSN(ret, line_node, putnil);
8659 const rb_iseq_t *ip = iseq;
8662 if (!ISEQ_COMPILE_DATA(ip)) {
8667 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8668 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8672 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8675 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8676 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8680 ip = ISEQ_BODY(ip)->parent_iseq;
8683 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8684 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8687 ADD_INSN(ret, line_node, pop);
8691 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
8699compile_redo(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8701 const NODE *line_node = node;
8703 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8704 LABEL *splabel = NEW_LABEL(0);
8705 debugs(
"redo in while");
8706 ADD_LABEL(ret, splabel);
8707 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8708 add_ensure_iseq(ret, iseq, 0);
8709 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8710 ADD_ADJUST_RESTORE(ret, splabel);
8712 ADD_INSN(ret, line_node, putnil);
8715 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8716 LABEL *splabel = NEW_LABEL(0);
8718 debugs(
"redo in block");
8719 ADD_LABEL(ret, splabel);
8720 add_ensure_iseq(ret, iseq, 0);
8721 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8722 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8723 ADD_ADJUST_RESTORE(ret, splabel);
8726 ADD_INSN(ret, line_node, putnil);
8730 const rb_iseq_t *ip = iseq;
8733 if (!ISEQ_COMPILE_DATA(ip)) {
8738 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8741 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8744 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8745 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
8749 ip = ISEQ_BODY(ip)->parent_iseq;
8752 ADD_INSN(ret, line_node, putnil);
8753 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8756 ADD_INSN(ret, line_node, pop);
8760 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
8768compile_retry(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8770 const NODE *line_node = node;
8772 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
8773 ADD_INSN(ret, line_node, putnil);
8774 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
8777 ADD_INSN(ret, line_node, pop);
8781 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
8788compile_rescue(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 LABEL *lstart = NEW_LABEL(line);
8793 LABEL *lend = NEW_LABEL(line);
8794 LABEL *lcont = NEW_LABEL(line);
8795 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
8797 ISEQ_BODY(iseq)->location.label),
8798 ISEQ_TYPE_RESCUE, line);
8800 lstart->rescued = LABEL_RESCUE_BEG;
8801 lend->rescued = LABEL_RESCUE_END;
8802 ADD_LABEL(ret, lstart);
8804 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
8805 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
8807 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
8809 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
8811 ADD_LABEL(ret, lend);
8812 if (RNODE_RESCUE(node)->nd_else) {
8813 ADD_INSN(ret, line_node, pop);
8814 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
8816 ADD_INSN(ret, line_node, nop);
8817 ADD_LABEL(ret, lcont);
8820 ADD_INSN(ret, line_node, pop);
8824 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
8825 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
8830compile_resbody(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8832 const int line = nd_line(node);
8833 const NODE *line_node = node;
8834 const NODE *resq = node;
8836 LABEL *label_miss, *label_hit;
8839 label_miss = NEW_LABEL(line);
8840 label_hit = NEW_LABEL(line);
8842 narg = RNODE_RESBODY(resq)->nd_args;
8844 switch (nd_type(narg)) {
8847 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8848 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
8849 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8850 ADD_INSNL(ret, line_node, branchif, label_hit);
8851 narg = RNODE_LIST(narg)->nd_next;
8857 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8858 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
8859 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
8860 ADD_INSNL(ret, line_node, branchif, label_hit);
8863 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
8867 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
8869 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8870 ADD_INSNL(ret, line_node, branchif, label_hit);
8872 ADD_INSNL(ret, line_node, jump, label_miss);
8873 ADD_LABEL(ret, label_hit);
8876 if (RNODE_RESBODY(resq)->nd_exc_var) {
8877 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
8880 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) {
8882 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
8885 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
8888 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
8889 ADD_INSN(ret, line_node, nop);
8891 ADD_INSN(ret, line_node, leave);
8892 ADD_LABEL(ret, label_miss);
8893 resq = RNODE_RESBODY(resq)->nd_next;
8899compile_ensure(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8901 const int line = nd_line(RNODE_ENSURE(node)->nd_ensr);
8902 const NODE *line_node = node;
8904 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
8906 ISEQ_TYPE_ENSURE, line);
8907 LABEL *lstart = NEW_LABEL(line);
8908 LABEL *lend = NEW_LABEL(line);
8909 LABEL *lcont = NEW_LABEL(line);
8917 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
8919 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
8924 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
8926 ADD_LABEL(ret, lstart);
8927 CHECK(COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave)));
8928 ADD_LABEL(ret, lend);
8930 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
8931 ADD_LABEL(ret, lcont);
8932 if (last_leave) ADD_INSN(ret, line_node, pop);
8934 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
8935 if (lstart->link.next != &lend->link) {
8937 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
8939 erange = erange->next;
8943 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
8948compile_return(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8950 const NODE *line_node = node;
8953 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8954 const rb_iseq_t *is = iseq;
8955 enum rb_iseq_type t =
type;
8956 const NODE *retval = RNODE_RETURN(node)->nd_stts;
8959 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
8960 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
8961 t = ISEQ_BODY(is)->type;
8965 case ISEQ_TYPE_MAIN:
8967 rb_warn(
"argument of top-level return is ignored");
8971 type = ISEQ_TYPE_METHOD;
8978 if (
type == ISEQ_TYPE_METHOD) {
8979 splabel = NEW_LABEL(0);
8980 ADD_LABEL(ret, splabel);
8981 ADD_ADJUST(ret, line_node, 0);
8984 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
8986 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8987 add_ensure_iseq(ret, iseq, 1);
8989 ADD_INSN(ret, line_node, leave);
8990 ADD_ADJUST_RESTORE(ret, splabel);
8993 ADD_INSN(ret, line_node, putnil);
8997 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
8999 ADD_INSN(ret, line_node, pop);
9007drop_unreachable_return(LINK_ANCHOR *ret)
9009 LINK_ELEMENT *i = ret->last, *last;
9010 if (!i)
return false;
9011 if (IS_TRACE(i)) i = i->prev;
9012 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
9014 if (IS_ADJUST(i)) i = i->prev;
9015 if (!IS_INSN(i))
return false;
9016 switch (INSN_OF(i)) {
9023 (ret->last = last->prev)->next = NULL;
9028compile_evstr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9030 CHECK(COMPILE_(ret,
"nd_body", node, popped));
9032 if (!popped && !all_string_result_p(node)) {
9033 const NODE *line_node = node;
9034 const unsigned int flag = VM_CALL_FCALL;
9038 ADD_INSN(ret, line_node, dup);
9039 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
9040 ADD_INSN(ret, line_node, anytostring);
9046compile_lvar(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *line_node,
ID id)
9048 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
9050 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
9051 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
9055qcall_branch_start(rb_iseq_t *iseq, LINK_ANCHOR *
const recv,
VALUE *branches,
const NODE *node,
const NODE *line_node)
9057 LABEL *else_label = NEW_LABEL(nd_line(line_node));
9060 br = decl_branch_base(iseq, PTR2NUM(node), nd_code_loc(node),
"&.");
9062 ADD_INSN(recv, line_node, dup);
9063 ADD_INSNL(recv, line_node, branchnil, else_label);
9064 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
9069qcall_branch_end(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, LABEL *else_label,
VALUE branches,
const NODE *node,
const NODE *line_node)
9072 if (!else_label)
return;
9073 end_label = NEW_LABEL(nd_line(line_node));
9074 ADD_INSNL(ret, line_node, jump, end_label);
9075 ADD_LABEL(ret, else_label);
9076 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
9077 ADD_LABEL(ret, end_label);
9081compile_call_precheck_freeze(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
const NODE *line_node,
int popped)
9086 if (get_nd_recv(node) &&
9087 (nd_type_p(get_nd_recv(node), NODE_STR) || nd_type_p(get_nd_recv(node), NODE_FILE)) &&
9088 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
9089 get_nd_args(node) == NULL &&
9090 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9091 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
9092 VALUE str = get_string_value(get_nd_recv(node));
9093 if (get_node_call_nd_mid(node) == idUMinus) {
9094 ADD_INSN2(ret, line_node, opt_str_uminus, str,
9095 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
9098 ADD_INSN2(ret, line_node, opt_str_freeze, str,
9099 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
9103 ADD_INSN(ret, line_node, pop);
9111iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
9113 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
9117iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
9120 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
9121 for (i=0; table[i].index != -1; i++) {
9122 if (strcmp(table[i].name, name) == 0) {
9130iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
9132 const char *name = rb_id2name(mid);
9133 static const char prefix[] =
"__builtin_";
9134 const size_t prefix_len =
sizeof(prefix) - 1;
9139 switch (nd_type(recv)) {
9141 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
9146 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
9156 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
9157 return &name[prefix_len];
9166delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
9173 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
9174 unsigned int start=0;
9179 argc + start <= ISEQ_BODY(iseq)->local_table_size;
9181 const LINK_ELEMENT *elem = FIRST_ELEMENT(args);
9183 for (
unsigned int i=start; i-start<argc; i++) {
9184 if (IS_INSN(elem) &&
9185 INSN_OF(elem) == BIN(getlocal)) {
9186 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
9187 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
9189 if (local_level == 0) {
9190 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
9192 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
9193 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
9194 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
9195 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9219 *pstart_index = start;
9229compile_builtin_attr(rb_iseq_t *iseq,
const NODE *node)
9233 if (!node)
goto no_arg;
9235 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9236 const NODE *next = RNODE_LIST(node)->nd_next;
9238 node = RNODE_LIST(node)->nd_head;
9239 if (!node)
goto no_arg;
9240 switch (nd_type(node)) {
9242 symbol = rb_node_sym_string_val(node);
9248 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9251 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
9252 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9254 else if (strcmp(RSTRING_PTR(
string),
"inline_block") == 0) {
9255 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9257 else if (strcmp(RSTRING_PTR(
string),
"use_block") == 0) {
9258 iseq_set_use_block(iseq);
9260 else if (strcmp(RSTRING_PTR(
string),
"c_trace") == 0) {
9262 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9271 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9274 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9277 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %s", RSTRING_PTR(
string));
9280 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9284compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
const NODE *line_node,
int popped)
9288 if (!node)
goto no_arg;
9289 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9290 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9291 node = RNODE_LIST(node)->nd_head;
9292 if (!node)
goto no_arg;
9293 switch (nd_type(node)) {
9295 name = rb_node_sym_string_val(node);
9300 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9302 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9306 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9309 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9312 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9313 rb_builtin_class_name(name));
9316 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9320mandatory_node(
const rb_iseq_t *iseq,
const NODE *cond_node)
9322 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9323 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9324 return RNODE_IF(node)->nd_body;
9327 rb_bug(
"mandatory_node: can't find mandatory node");
9332compile_builtin_mandatory_only_method(rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9336 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9338 rb_node_args_t args_node;
9339 rb_node_init(RNODE(&args_node), NODE_ARGS);
9340 args_node.nd_ainfo = args;
9343 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9344 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9347 rb_ast_id_table_t *tbl =
ALLOCV(idtmp,
sizeof(rb_ast_id_table_t) + table_size *
sizeof(
ID));
9348 tbl->size = table_size;
9353 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9354 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9357 for (; i<table_size; i++) {
9358 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9361 rb_node_scope_t scope_node;
9362 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9363 scope_node.nd_tbl = tbl;
9364 scope_node.nd_body = mandatory_node(iseq, node);
9365 scope_node.nd_parent = NULL;
9366 scope_node.nd_args = &args_node;
9368 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9370 const rb_iseq_t *mandatory_only_iseq =
9371 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9372 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9373 nd_line(line_node), NULL, 0,
9374 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9375 ISEQ_BODY(iseq)->variable.script_lines);
9376 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
9383compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
const NODE *line_node,
int popped,
9384 const rb_iseq_t *parent_block, LINK_ANCHOR *args,
const char *builtin_func)
9386 NODE *args_node = get_nd_args(node);
9388 if (parent_block != NULL) {
9389 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9393# define BUILTIN_INLINE_PREFIX "_bi"
9394 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9395 bool cconst =
false;
9400 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9401 strcmp(
"cexpr!", builtin_func) == 0) {
9404 else if (strcmp(
"cconst!", builtin_func) == 0) {
9407 else if (strcmp(
"cinit!", builtin_func) == 0) {
9411 else if (strcmp(
"attr!", builtin_func) == 0) {
9412 return compile_builtin_attr(iseq, args_node);
9414 else if (strcmp(
"arg!", builtin_func) == 0) {
9415 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9417 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9419 rb_bug(
"mandatory_only? should be in if condition");
9421 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9422 rb_bug(
"mandatory_only? should be put on top");
9425 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9426 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9429 rb_bug(
"can't find builtin function:%s", builtin_func);
9432 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9436 int inline_index = nd_line(node);
9437 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9438 builtin_func = inline_func;
9444 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9445 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9446 ADD_INSN1(ret, line_node, putobject, const_val);
9452 unsigned int flag = 0;
9454 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9456 if (
FIX2INT(argc) != bf->argc) {
9457 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9458 builtin_func, bf->argc,
FIX2INT(argc));
9462 unsigned int start_index;
9463 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9464 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9468 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9471 if (popped) ADD_INSN(ret, line_node, pop);
9477compile_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)
9485 ID mid = get_node_call_nd_mid(node);
9487 unsigned int flag = 0;
9489 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9490 LABEL *else_label = NULL;
9493 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9499 if (nd_type_p(node, NODE_VCALL)) {
9504 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9506 if (mid == id_bitblt) {
9507 ADD_INSN(ret, line_node, bitblt);
9510 else if (mid == id_answer) {
9511 ADD_INSN(ret, line_node, answer);
9523 if (nd_type_p(node, NODE_FCALL) &&
9524 (mid == goto_id || mid == label_id)) {
9527 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9530 if (!labels_table) {
9531 labels_table = st_init_numtable();
9532 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9535 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9539 if (mid == goto_id) {
9540 ADD_INSNL(ret, line_node, jump, label);
9543 ADD_LABEL(ret, label);
9550 const char *builtin_func;
9551 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9552 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9553 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9557 if (!assume_receiver) {
9558 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9561 if (mid == idCall &&
9562 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9563 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9564 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9566 else if (private_recv_p(node)) {
9567 ADD_INSN(recv, node, putself);
9568 flag |= VM_CALL_FCALL;
9571 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9574 if (
type == NODE_QCALL) {
9575 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9578 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9579 ADD_CALL_RECEIVER(recv, line_node);
9584 if (
type != NODE_VCALL) {
9585 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9586 CHECK(!
NIL_P(argc));
9594 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
9595 mid == rb_intern(
"new") &&
9596 parent_block == NULL &&
9597 !(flag & VM_CALL_ARGS_BLOCKARG);
9600 ADD_INSN(ret, node, putnil);
9601 ADD_INSN(ret, node, swap);
9606 debugp_param(
"call args argc", argc);
9607 debugp_param(
"call method",
ID2SYM(mid));
9609 switch ((
int)
type) {
9611 flag |= VM_CALL_VCALL;
9614 flag |= VM_CALL_FCALL;
9617 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9618 ADD_INSN(ret, line_node, splatkw);
9621 LABEL *not_basic_new = NEW_LABEL(nd_line(node));
9622 LABEL *not_basic_new_finish = NEW_LABEL(nd_line(node));
9627 if (flag & VM_CALL_FORWARDING) {
9628 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc) + 1, flag, keywords, 0);
9631 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc), flag, keywords, 0);
9633 ADD_INSN2(ret, node, opt_new, ci, not_basic_new);
9634 LABEL_REF(not_basic_new);
9637 ADD_SEND_R(ret, line_node, rb_intern(
"initialize"), argc, parent_block,
INT2FIX(flag | VM_CALL_FCALL), keywords);
9638 ADD_INSNL(ret, line_node, jump, not_basic_new_finish);
9640 ADD_LABEL(ret, not_basic_new);
9642 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9643 ADD_INSN(ret, line_node, swap);
9645 ADD_LABEL(ret, not_basic_new_finish);
9646 ADD_INSN(ret, line_node, pop);
9649 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9652 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9654 ADD_INSN(ret, line_node, pop);
9660compile_op_asgn1(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9662 const int line = nd_line(node);
9664 unsigned int flag = 0;
9666 ID id = RNODE_OP_ASGN1(node)->nd_mid;
9692 ADD_INSN(ret, node, putnil);
9694 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
9695 CHECK(asgnflag != -1);
9696 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
9701 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
9702 CHECK(!
NIL_P(argc));
9704 int dup_argn =
FIX2INT(argc) + 1;
9705 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
9707 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
9709 if (
id == idOROP ||
id == idANDOP) {
9718 LABEL *label = NEW_LABEL(line);
9719 LABEL *lfin = NEW_LABEL(line);
9721 ADD_INSN(ret, node, dup);
9723 ADD_INSNL(ret, node, branchif, label);
9726 ADD_INSNL(ret, node, branchunless, label);
9728 ADD_INSN(ret, node, pop);
9730 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9732 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9734 if (flag & VM_CALL_ARGS_SPLAT) {
9735 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9736 ADD_INSN(ret, node, swap);
9737 ADD_INSN1(ret, node, splatarray,
Qtrue);
9738 ADD_INSN(ret, node, swap);
9739 flag |= VM_CALL_ARGS_SPLAT_MUT;
9741 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9742 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9745 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9747 ADD_INSN(ret, node, pop);
9748 ADD_INSNL(ret, node, jump, lfin);
9749 ADD_LABEL(ret, label);
9751 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9753 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
9754 ADD_LABEL(ret, lfin);
9757 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
9758 ADD_SEND(ret, node,
id,
INT2FIX(1));
9760 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
9762 if (flag & VM_CALL_ARGS_SPLAT) {
9763 if (flag & VM_CALL_KW_SPLAT) {
9764 ADD_INSN1(ret, node, topn,
INT2FIX(2));
9765 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9766 ADD_INSN1(ret, node, splatarray,
Qtrue);
9767 flag |= VM_CALL_ARGS_SPLAT_MUT;
9769 ADD_INSN(ret, node, swap);
9770 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9771 ADD_INSN1(ret, node, setn,
INT2FIX(2));
9772 ADD_INSN(ret, node, pop);
9775 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
9776 ADD_INSN(ret, node, swap);
9777 ADD_INSN1(ret, node, splatarray,
Qtrue);
9778 ADD_INSN(ret, node, swap);
9779 flag |= VM_CALL_ARGS_SPLAT_MUT;
9781 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
9783 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
9786 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
9788 ADD_INSN(ret, node, pop);
9794compile_op_asgn2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9796 const int line = nd_line(node);
9797 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
9798 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
9800 LABEL *lfin = NEW_LABEL(line);
9801 LABEL *lcfin = NEW_LABEL(line);
9856 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
9857 CHECK(asgnflag != -1);
9858 if (RNODE_OP_ASGN2(node)->nd_aid) {
9859 lskip = NEW_LABEL(line);
9860 ADD_INSN(ret, node, dup);
9861 ADD_INSNL(ret, node, branchnil, lskip);
9863 ADD_INSN(ret, node, dup);
9864 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
9866 if (atype == idOROP || atype == idANDOP) {
9868 ADD_INSN(ret, node, dup);
9870 if (atype == idOROP) {
9871 ADD_INSNL(ret, node, branchif, lcfin);
9874 ADD_INSNL(ret, node, branchunless, lcfin);
9877 ADD_INSN(ret, node, pop);
9879 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9881 ADD_INSN(ret, node, swap);
9882 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9884 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9885 ADD_INSNL(ret, node, jump, lfin);
9887 ADD_LABEL(ret, lcfin);
9889 ADD_INSN(ret, node, swap);
9892 ADD_LABEL(ret, lfin);
9895 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
9896 ADD_SEND(ret, node, atype,
INT2FIX(1));
9898 ADD_INSN(ret, node, swap);
9899 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9901 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
9903 if (lskip && popped) {
9904 ADD_LABEL(ret, lskip);
9906 ADD_INSN(ret, node, pop);
9907 if (lskip && !popped) {
9908 ADD_LABEL(ret, lskip);
9913static int compile_shareable_constant_value(rb_iseq_t *iseq, LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
9916compile_op_cdecl(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9918 const int line = nd_line(node);
9923 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
9925 ADD_INSN1(ret, node, putobject, rb_cObject);
9928 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
9931 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
9932 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
9935 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
9937 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
9938 lassign = NEW_LABEL(line);
9939 ADD_INSN(ret, node, dup);
9940 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
9942 ADD_INSNL(ret, node, branchunless, lassign);
9944 ADD_INSN(ret, node, dup);
9945 ADD_INSN1(ret, node, putobject,
Qtrue);
9946 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
9948 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
9949 lfin = NEW_LABEL(line);
9950 if (!popped) ADD_INSN(ret, node, dup);
9951 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
9952 ADD_INSNL(ret, node, branchif, lfin);
9954 ADD_INSNL(ret, node, branchunless, lfin);
9956 if (!popped) ADD_INSN(ret, node, pop);
9957 if (lassign) ADD_LABEL(ret, lassign);
9958 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9961 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9963 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
9964 ADD_INSN(ret, node, swap);
9966 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9967 ADD_LABEL(ret, lfin);
9968 if (!popped) ADD_INSN(ret, node, swap);
9969 ADD_INSN(ret, node, pop);
9972 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
9974 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
9976 ADD_INSN(ret, node, swap);
9978 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9979 ADD_INSN(ret, node, swap);
9981 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
9987compile_op_log(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
9989 const int line = nd_line(node);
9990 LABEL *lfin = NEW_LABEL(line);
9993 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
9997 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
9998 lassign = lfinish[1];
10000 lassign = NEW_LABEL(line);
10002 ADD_INSNL(ret, node, branchunless, lassign);
10005 lassign = NEW_LABEL(line);
10008 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
10011 ADD_INSN(ret, node, dup);
10014 if (
type == NODE_OP_ASGN_AND) {
10015 ADD_INSNL(ret, node, branchunless, lfin);
10018 ADD_INSNL(ret, node, branchif, lfin);
10022 ADD_INSN(ret, node, pop);
10025 ADD_LABEL(ret, lassign);
10026 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
10027 ADD_LABEL(ret, lfin);
10032compile_super(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
10037 unsigned int flag = 0;
10039 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
10043 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
10045 if (
type == NODE_SUPER) {
10046 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
10047 CHECK(!
NIL_P(vargc));
10049 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
10050 ADD_INSN(args, node, splatkw);
10053 if (flag & VM_CALL_ARGS_BLOCKARG) {
10060 const rb_iseq_t *liseq = body->local_iseq;
10062 const struct rb_iseq_param_keyword *
const local_kwd = local_body->param.keyword;
10063 int lvar_level = get_lvar_level(iseq);
10065 argc = local_body->param.lead_num;
10068 for (i = 0; i < local_body->param.lead_num; i++) {
10069 int idx = local_body->local_table_size - i;
10070 ADD_GETLOCAL(args, node, idx, lvar_level);
10074 if (local_body->param.flags.forwardable) {
10075 flag |= VM_CALL_FORWARDING;
10076 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
10077 ADD_GETLOCAL(args, node, idx, lvar_level);
10080 if (local_body->param.flags.has_opt) {
10083 for (j = 0; j < local_body->param.opt_num; j++) {
10084 int idx = local_body->local_table_size - (i + j);
10085 ADD_GETLOCAL(args, node, idx, lvar_level);
10090 if (local_body->param.flags.has_rest) {
10092 int idx = local_body->local_table_size - local_body->param.rest_start;
10093 ADD_GETLOCAL(args, node, idx, lvar_level);
10094 ADD_INSN1(args, node, splatarray, RBOOL(local_body->param.flags.has_post));
10096 argc = local_body->param.rest_start + 1;
10097 flag |= VM_CALL_ARGS_SPLAT;
10099 if (local_body->param.flags.has_post) {
10101 int post_len = local_body->param.post_num;
10102 int post_start = local_body->param.post_start;
10104 if (local_body->param.flags.has_rest) {
10106 for (j=0; j<post_len; j++) {
10107 int idx = local_body->local_table_size - (post_start + j);
10108 ADD_GETLOCAL(args, node, idx, lvar_level);
10110 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
10111 flag |= VM_CALL_ARGS_SPLAT_MUT;
10116 for (j=0; j<post_len; j++) {
10117 int idx = local_body->local_table_size - (post_start + j);
10118 ADD_GETLOCAL(args, node, idx, lvar_level);
10120 argc = post_len + post_start;
10124 if (local_body->param.flags.has_kw) {
10125 int local_size = local_body->local_table_size;
10128 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10130 if (local_body->param.flags.has_kwrest) {
10131 int idx = local_body->local_table_size - local_kwd->rest_start;
10132 ADD_GETLOCAL(args, node, idx, lvar_level);
10134 ADD_SEND (args, node, rb_intern(
"dup"),
INT2FIX(0));
10137 ADD_INSN1(args, node, newhash,
INT2FIX(0));
10139 for (i = 0; i < local_kwd->num; ++i) {
10140 ID id = local_kwd->table[i];
10141 int idx = local_size - get_local_var_idx(liseq,
id);
10142 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
10143 ADD_GETLOCAL(args, node, idx, lvar_level);
10145 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
10146 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
10148 else if (local_body->param.flags.has_kwrest) {
10149 int idx = local_body->local_table_size - local_kwd->rest_start;
10150 ADD_GETLOCAL(args, node, idx, lvar_level);
10152 flag |= VM_CALL_KW_SPLAT;
10156 if (use_block && parent_block == NULL) {
10157 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10160 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
10161 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
10162 ADD_INSN(ret, node, putself);
10163 ADD_SEQ(ret, args);
10165 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
10167 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
10168 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
10171 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
10175 ADD_INSN(ret, node, pop);
10181compile_yield(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10185 unsigned int flag = 0;
10190 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
10191 case ISEQ_TYPE_TOP:
10192 case ISEQ_TYPE_MAIN:
10193 case ISEQ_TYPE_CLASS:
10194 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
10199 if (RNODE_YIELD(node)->nd_head) {
10200 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
10201 CHECK(!
NIL_P(argc));
10207 ADD_SEQ(ret, args);
10208 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
10209 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10212 ADD_INSN(ret, node, pop);
10216 const rb_iseq_t *tmp_iseq = iseq;
10217 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
10218 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
10220 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
10226compile_match(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
10233 switch ((
int)
type) {
10236 VALUE re = rb_node_regx_string_val(node);
10237 RB_OBJ_SET_FROZEN_SHAREABLE(re);
10238 ADD_INSN1(recv, node, putobject, re);
10239 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
10244 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
10245 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
10248 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
10249 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
10253 ADD_SEQ(ret, recv);
10255 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
10257 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
10258 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
10262 ADD_INSN(ret, node, pop);
10268compile_colon2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10273 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10274 (segments = collect_const_segments(iseq, node))) {
10275 ISEQ_BODY(iseq)->ic_size++;
10276 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10286 CHECK(compile_const_prefix(iseq, node, pref, body));
10287 if (LIST_INSN_SIZE_ZERO(pref)) {
10288 ADD_INSN(ret, node, putnil);
10289 ADD_SEQ(ret, body);
10292 ADD_SEQ(ret, pref);
10293 ADD_SEQ(ret, body);
10299 ADD_CALL_RECEIVER(ret, node);
10300 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10301 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10304 ADD_INSN(ret, node, pop);
10310compile_colon3(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10312 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10315 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10316 ISEQ_BODY(iseq)->ic_size++;
10317 VALUE segments = rb_ary_new_from_args(2,
ID2SYM(idNULL),
ID2SYM(RNODE_COLON3(node)->nd_mid));
10318 RB_OBJ_SET_FROZEN_SHAREABLE(segments);
10319 ADD_INSN1(ret, node, opt_getconstant_path, segments);
10323 ADD_INSN1(ret, node, putobject, rb_cObject);
10324 ADD_INSN1(ret, node, putobject,
Qtrue);
10325 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10329 ADD_INSN(ret, node, pop);
10335compile_dots(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const int excl)
10338 const NODE *b = RNODE_DOT2(node)->nd_beg;
10339 const NODE *e = RNODE_DOT2(node)->nd_end;
10341 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10343 VALUE bv = optimized_range_item(b);
10344 VALUE ev = optimized_range_item(e);
10347 ADD_INSN1(ret, node, putobject, val);
10352 CHECK(COMPILE_(ret,
"min", b, popped));
10353 CHECK(COMPILE_(ret,
"max", e, popped));
10355 ADD_INSN1(ret, node, newrange, flag);
10362compile_errinfo(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10365 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10366 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10369 const rb_iseq_t *ip = iseq;
10372 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10375 ip = ISEQ_BODY(ip)->parent_iseq;
10379 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10382 ADD_INSN(ret, node, putnil);
10390compile_kw_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10393 LABEL *end_label = NEW_LABEL(nd_line(node));
10394 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10396 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10398 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10401 else if (nd_type_p(default_value, NODE_SYM) ||
10402 nd_type_p(default_value, NODE_REGX) ||
10403 nd_type_p(default_value, NODE_LINE) ||
10404 nd_type_p(default_value, NODE_INTEGER) ||
10405 nd_type_p(default_value, NODE_FLOAT) ||
10406 nd_type_p(default_value, NODE_RATIONAL) ||
10407 nd_type_p(default_value, NODE_IMAGINARY) ||
10408 nd_type_p(default_value, NODE_NIL) ||
10409 nd_type_p(default_value, NODE_TRUE) ||
10410 nd_type_p(default_value, NODE_FALSE)) {
10411 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10419 int kw_bits_idx = body->local_table_size - body->param.keyword->bits_start;
10420 int keyword_idx = body->param.keyword->num;
10422 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10423 ADD_INSNL(ret, node, branchif, end_label);
10424 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10425 ADD_LABEL(ret, end_label);
10431compile_attrasgn(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10435 unsigned int flag = 0;
10436 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10438 LABEL *else_label = NULL;
10443 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10444 CHECK(!
NIL_P(argc));
10446 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10447 CHECK(asgnflag != -1);
10448 flag |= (
unsigned int)asgnflag;
10450 debugp_param(
"argc", argc);
10451 debugp_param(
"nd_mid",
ID2SYM(mid));
10455 mid = rb_id_attrset(mid);
10456 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10459 ADD_INSN(ret, node, putnil);
10460 ADD_SEQ(ret, recv);
10461 ADD_SEQ(ret, args);
10463 if (flag & VM_CALL_ARGS_SPLAT) {
10464 ADD_INSN(ret, node, dup);
10465 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10466 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10467 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10468 ADD_INSN (ret, node, pop);
10471 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10475 ADD_SEQ(ret, recv);
10476 ADD_SEQ(ret, args);
10478 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10479 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10480 ADD_INSN(ret, node, pop);
10485compile_make_shareable_node(rb_iseq_t *iseq, LINK_ANCHOR *ret, LINK_ANCHOR *sub,
const NODE *value,
bool copy)
10487 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10495 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10502 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10509node_const_decl_val(
const NODE *node)
10512 switch (nd_type(node)) {
10514 if (RNODE_CDECL(node)->nd_vid) {
10515 path = rb_id2str(RNODE_CDECL(node)->nd_vid);
10519 node = RNODE_CDECL(node)->nd_else;
10527 rb_str_append(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10530 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10536 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10537 rb_ary_push(path, rb_id2str(RNODE_COLON2(node)->nd_mid));
10539 if (node && nd_type_p(node, NODE_CONST)) {
10541 rb_ary_push(path, rb_id2str(RNODE_CONST(node)->nd_vid));
10543 else if (node && nd_type_p(node, NODE_COLON3)) {
10545 rb_ary_push(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10555 path = rb_fstring(path);
10560const_decl_path(NODE *dest)
10563 if (!nd_type_p(dest, NODE_CALL)) {
10564 path = node_const_decl_val(dest);
10570compile_ensure_shareable_node(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE *dest,
const NODE *value)
10575 VALUE path = const_decl_path(dest);
10576 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10577 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10578 ADD_INSN1(ret, value, putobject, path);
10580 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10585#ifndef SHAREABLE_BARE_EXPRESSION
10586#define SHAREABLE_BARE_EXPRESSION 1
10590compile_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)
10592# define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10593 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10595 DECL_ANCHOR(anchor);
10597 enum node_type
type = node ? nd_type(node) : NODE_NIL;
10609 *value_p = rb_node_sym_string_val(node);
10612 *value_p = rb_node_regx_string_val(node);
10615 *value_p = rb_node_line_lineno_val(node);
10618 *value_p = rb_node_integer_literal_val(node);
10621 *value_p = rb_node_float_literal_val(node);
10623 case NODE_RATIONAL:
10624 *value_p = rb_node_rational_literal_val(node);
10626 case NODE_IMAGINARY:
10627 *value_p = rb_node_imaginary_literal_val(node);
10629 case NODE_ENCODING:
10630 *value_p = rb_node_encoding_val(node);
10633 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10634 *shareable_literal_p = 1;
10638 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10639 if (shareable == rb_parser_shareable_literal) {
10645 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10648 *shareable_literal_p = 1;
10652 VALUE lit = rb_node_str_string_val(node);
10653 ADD_INSN1(ret, node, putobject, lit);
10656 *shareable_literal_p = 1;
10662 VALUE lit = rb_node_file_path_val(node);
10663 ADD_INSN1(ret, node, putobject, lit);
10666 *shareable_literal_p = 1;
10674 ADD_INSN1(ret, node, putobject, lit);
10677 *shareable_literal_p = 1;
10683 INIT_ANCHOR(anchor);
10685 for (NODE *n = (NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
10687 int shareable_literal_p2;
10688 NODE *elt = RNODE_LIST(n)->nd_head;
10690 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
10691 if (shareable_literal_p2) {
10694 else if (
RTEST(lit)) {
10700 if (!UNDEF_P(val)) {
10712 if (!RNODE_HASH(node)->nd_brace) {
10714 *shareable_literal_p = 0;
10717 for (NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10718 if (!RNODE_LIST(n)->nd_head) {
10720 goto compile_shareable;
10724 INIT_ANCHOR(anchor);
10726 for (NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10728 VALUE value_val = 0;
10729 int shareable_literal_p2;
10730 NODE *key = RNODE_LIST(n)->nd_head;
10731 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
10732 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10733 if (shareable_literal_p2) {
10736 else if (
RTEST(lit)) {
10737 rb_hash_clear(lit);
10740 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10741 if (shareable_literal_p2) {
10744 else if (
RTEST(lit)) {
10745 rb_hash_clear(lit);
10749 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
10750 rb_hash_aset(lit, key_val, value_val);
10753 rb_hash_clear(lit);
10764 if (shareable == rb_parser_shareable_literal &&
10765 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
10766 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
10768 *shareable_literal_p = 1;
10771 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10773 *shareable_literal_p = 0;
10779 if (nd_type(node) == NODE_LIST) {
10780 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10782 else if (nd_type(node) == NODE_HASH) {
10783 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10784 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10787 *shareable_literal_p = 0;
10788 ADD_SEQ(ret, anchor);
10794 if (nd_type(node) == NODE_LIST) {
10795 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
10797 else if (nd_type(node) == NODE_HASH) {
10798 int len = (int)RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
10799 ADD_INSN1(anchor, node, newhash,
INT2FIX(
len));
10801 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
10803 *shareable_literal_p = 1;
10807 ADD_INSN1(ret, node, putobject, val);
10810 *shareable_literal_p = 1;
10817compile_shareable_constant_value(rb_iseq_t *iseq, LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
10821 DECL_ANCHOR(anchor);
10822 INIT_ANCHOR(anchor);
10824 switch (shareable) {
10825 case rb_parser_shareable_none:
10826 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
10829 case rb_parser_shareable_literal:
10830 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (NODE *)lhs, value, 0, &val, &literal_p));
10831 ADD_SEQ(ret, anchor);
10834 case rb_parser_shareable_copy:
10835 case rb_parser_shareable_everything:
10836 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (NODE *)lhs, value, 0, &val, &literal_p));
10838 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
10841 ADD_SEQ(ret, anchor);
10845 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
10849static int iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped);
10858iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *node,
int popped)
10862 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
10863 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
10864 debugs(
"node: NODE_NIL(implicit)\n");
10865 ADD_SYNTHETIC_INSN(ret, lineno, -1, putnil);
10869 return iseq_compile_each0(iseq, ret, node, popped);
10873iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10875 const int line = (int)nd_line(node);
10876 const enum node_type
type = nd_type(node);
10879 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
10883 if (nd_fl_newline(node)) {
10885 ISEQ_COMPILE_DATA(iseq)->last_line = line;
10886 if (line > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
10887 event |= RUBY_EVENT_COVERAGE_LINE;
10889 ADD_TRACE(ret, event);
10893 debug_node_start(node);
10894#undef BEFORE_RETURN
10895#define BEFORE_RETURN debug_node_end()
10899 CHECK(compile_block(iseq, ret, node, popped));
10903 CHECK(compile_if(iseq, ret, node, popped,
type));
10906 CHECK(compile_case(iseq, ret, node, popped));
10909 CHECK(compile_case2(iseq, ret, node, popped));
10912 CHECK(compile_case3(iseq, ret, node, popped));
10916 CHECK(compile_loop(iseq, ret, node, popped,
type));
10920 CHECK(compile_iter(iseq, ret, node, popped));
10922 case NODE_FOR_MASGN:
10923 CHECK(compile_for_masgn(iseq, ret, node, popped));
10926 CHECK(compile_break(iseq, ret, node, popped));
10929 CHECK(compile_next(iseq, ret, node, popped));
10932 CHECK(compile_redo(iseq, ret, node, popped));
10935 CHECK(compile_retry(iseq, ret, node, popped));
10938 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
10942 CHECK(compile_rescue(iseq, ret, node, popped));
10945 CHECK(compile_resbody(iseq, ret, node, popped));
10948 CHECK(compile_ensure(iseq, ret, node, popped));
10953 LABEL *end_label = NEW_LABEL(line);
10954 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(node)->nd_1st));
10956 ADD_INSN(ret, node, dup);
10958 if (
type == NODE_AND) {
10959 ADD_INSNL(ret, node, branchunless, end_label);
10962 ADD_INSNL(ret, node, branchif, end_label);
10965 ADD_INSN(ret, node, pop);
10967 CHECK(COMPILE_(ret,
"nd_2nd", RNODE_OR(node)->nd_2nd, popped));
10968 ADD_LABEL(ret, end_label);
10973 bool prev_in_masgn = ISEQ_COMPILE_DATA(iseq)->in_masgn;
10974 ISEQ_COMPILE_DATA(iseq)->in_masgn =
true;
10975 compile_massign(iseq, ret, node, popped);
10976 ISEQ_COMPILE_DATA(iseq)->in_masgn = prev_in_masgn;
10981 ID id = RNODE_LASGN(node)->nd_vid;
10982 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
10984 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
10985 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
10988 ADD_INSN(ret, node, dup);
10990 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
10995 ID id = RNODE_DASGN(node)->nd_vid;
10996 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
10997 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
11000 ADD_INSN(ret, node, dup);
11003 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
11006 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
11010 ADD_SETLOCAL(ret, node, ls - idx, lv);
11014 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
11017 ADD_INSN(ret, node, dup);
11019 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
11023 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
11025 ADD_INSN(ret, node, dup);
11027 ADD_INSN2(ret, node, setinstancevariable,
11028 ID2SYM(RNODE_IASGN(node)->nd_vid),
11029 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
11033 if (RNODE_CDECL(node)->nd_vid) {
11034 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11037 ADD_INSN(ret, node, dup);
11040 ADD_INSN1(ret, node, putspecialobject,
11041 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
11042 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
11045 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
11046 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11047 ADD_INSN(ret, node, swap);
11050 ADD_INSN1(ret, node, topn,
INT2FIX(1));
11051 ADD_INSN(ret, node, swap);
11054 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
11059 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
11061 ADD_INSN(ret, node, dup);
11063 ADD_INSN2(ret, node, setclassvariable,
11064 ID2SYM(RNODE_CVASGN(node)->nd_vid),
11065 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
11068 case NODE_OP_ASGN1:
11069 CHECK(compile_op_asgn1(iseq, ret, node, popped));
11071 case NODE_OP_ASGN2:
11072 CHECK(compile_op_asgn2(iseq, ret, node, popped));
11074 case NODE_OP_CDECL:
11075 CHECK(compile_op_cdecl(iseq, ret, node, popped));
11077 case NODE_OP_ASGN_AND:
11078 case NODE_OP_ASGN_OR:
11079 CHECK(compile_op_log(iseq, ret, node, popped,
type));
11083 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
11089 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
11095 CHECK(compile_super(iseq, ret, node, popped,
type));
11098 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
11103 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
11108 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
11111 CHECK(compile_return(iseq, ret, node, popped));
11114 CHECK(compile_yield(iseq, ret, node, popped));
11118 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
11124 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
11126 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
11128 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
11129 rb_id2str(RNODE_DVAR(node)->nd_vid));
11132 ADD_GETLOCAL(ret, node, ls - idx, lv);
11137 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
11139 ADD_INSN(ret, node, pop);
11144 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
11146 ADD_INSN2(ret, node, getinstancevariable,
11147 ID2SYM(RNODE_IVAR(node)->nd_vid),
11148 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
11153 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
11155 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
11157 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(RNODE_CONST(node)->nd_vid));
11158 RB_OBJ_SET_FROZEN_SHAREABLE(segments);
11159 ADD_INSN1(ret, node, opt_getconstant_path, segments);
11163 ADD_INSN(ret, node, putnil);
11164 ADD_INSN1(ret, node, putobject,
Qtrue);
11165 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
11169 ADD_INSN(ret, node, pop);
11175 ADD_INSN2(ret, node, getclassvariable,
11176 ID2SYM(RNODE_CVAR(node)->nd_vid),
11177 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
11181 case NODE_NTH_REF:{
11183 if (!RNODE_NTH_REF(node)->nd_nth) {
11184 ADD_INSN(ret, node, putnil);
11187 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11188 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
11192 case NODE_BACK_REF:{
11194 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11195 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
11202 CHECK(compile_match(iseq, ret, node, popped,
type));
11206 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
11212 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
11216 case NODE_ENCODING:{
11218 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
11222 case NODE_INTEGER:{
11223 VALUE lit = rb_node_integer_literal_val(node);
11225 debugp_param(
"integer", lit);
11227 ADD_INSN1(ret, node, putobject, lit);
11233 VALUE lit = rb_node_float_literal_val(node);
11235 debugp_param(
"float", lit);
11237 ADD_INSN1(ret, node, putobject, lit);
11242 case NODE_RATIONAL:{
11243 VALUE lit = rb_node_rational_literal_val(node);
11245 debugp_param(
"rational", lit);
11247 ADD_INSN1(ret, node, putobject, lit);
11252 case NODE_IMAGINARY:{
11253 VALUE lit = rb_node_imaginary_literal_val(node);
11255 debugp_param(
"imaginary", lit);
11257 ADD_INSN1(ret, node, putobject, lit);
11264 debugp_param(
"nd_lit", get_string_value(node));
11266 VALUE lit = get_string_value(node);
11267 const rb_compile_option_t *option = ISEQ_COMPILE_DATA(iseq)->option;
11269 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11270 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11271 RB_OBJ_SET_SHAREABLE(lit);
11273 switch (option->frozen_string_literal) {
11274 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11275 ADD_INSN1(ret, node, putchilledstring, lit);
11277 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11278 ADD_INSN1(ret, node, putstring, lit);
11280 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11281 ADD_INSN1(ret, node, putobject, lit);
11284 rb_bug(
"invalid frozen_string_literal");
11291 compile_dstr(iseq, ret, node);
11294 ADD_INSN(ret, node, pop);
11299 ADD_CALL_RECEIVER(ret, node);
11300 VALUE str = rb_node_str_string_val(node);
11301 ADD_INSN1(ret, node, putobject, str);
11303 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11306 ADD_INSN(ret, node, pop);
11311 ADD_CALL_RECEIVER(ret, node);
11312 compile_dstr(iseq, ret, node);
11313 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11316 ADD_INSN(ret, node, pop);
11321 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11325 VALUE lit = rb_node_regx_string_val(node);
11326 RB_OBJ_SET_SHAREABLE(lit);
11327 ADD_INSN1(ret, node, putobject, lit);
11333 compile_dregx(iseq, ret, node, popped);
11336 int ic_index = body->ise_size++;
11337 const rb_iseq_t *block_iseq;
11338 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11340 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
11344 ADD_INSN(ret, node, pop);
11348 case NODE_ARGSCAT:{
11350 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11351 ADD_INSN1(ret, node, splatarray,
Qfalse);
11352 ADD_INSN(ret, node, pop);
11353 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11354 ADD_INSN1(ret, node, splatarray,
Qfalse);
11355 ADD_INSN(ret, node, pop);
11358 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11359 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11360 if (nd_type_p(body_node, NODE_LIST)) {
11361 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11364 CHECK(COMPILE(ret,
"argscat body", body_node));
11365 ADD_INSN(ret, node, concattoarray);
11370 case NODE_ARGSPUSH:{
11372 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11373 ADD_INSN1(ret, node, splatarray,
Qfalse);
11374 ADD_INSN(ret, node, pop);
11375 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11378 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11379 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11380 if (keyword_node_p(body_node)) {
11381 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11382 ADD_INSN(ret, node, pushtoarraykwsplat);
11384 else if (static_literal_node_p(body_node, iseq,
false)) {
11385 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11386 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11389 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11390 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11396 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11397 ADD_INSN1(ret, node, splatarray,
Qtrue);
11400 ADD_INSN(ret, node, pop);
11405 ID mid = RNODE_DEFN(node)->nd_mid;
11406 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11408 ISEQ_TYPE_METHOD, line);
11410 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11411 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11415 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11421 ID mid = RNODE_DEFS(node)->nd_mid;
11422 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11424 ISEQ_TYPE_METHOD, line);
11426 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11427 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11428 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11432 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11437 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11438 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11439 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11440 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11441 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11444 ADD_INSN(ret, node, pop);
11449 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11450 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11451 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11452 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11455 ADD_INSN(ret, node, pop);
11460 const rb_parser_ary_t *ary = RNODE_UNDEF(node)->nd_undefs;
11462 for (
long i = 0; i < ary->len; i++) {
11463 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11464 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11465 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11466 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11468 if (i < ary->
len - 1) {
11469 ADD_INSN(ret, node, pop);
11474 ADD_INSN(ret, node, pop);
11479 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11480 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
11481 ISEQ_TYPE_CLASS, line);
11482 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11483 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11484 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11486 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11487 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11491 ADD_INSN(ret, node, pop);
11496 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11497 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
11498 ISEQ_TYPE_CLASS, line);
11499 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11500 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11502 ADD_INSN (ret, node, putnil);
11503 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11507 ADD_INSN(ret, node, pop);
11513 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11514 ISEQ_TYPE_CLASS, line);
11516 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11517 ADD_INSN (ret, node, putnil);
11518 CONST_ID(singletonclass,
"singletonclass");
11519 ADD_INSN3(ret, node, defineclass,
11520 ID2SYM(singletonclass), singleton_class,
11521 INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
11525 ADD_INSN(ret, node, pop);
11530 CHECK(compile_colon2(iseq, ret, node, popped));
11533 CHECK(compile_colon3(iseq, ret, node, popped));
11536 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11539 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11543 LABEL *lend = NEW_LABEL(line);
11544 LABEL *ltrue = NEW_LABEL(line);
11545 LABEL *lfalse = NEW_LABEL(line);
11546 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11548 ADD_LABEL(ret, ltrue);
11549 ADD_INSN1(ret, node, putobject,
Qtrue);
11550 ADD_INSNL(ret, node, jump, lend);
11551 ADD_LABEL(ret, lfalse);
11552 ADD_INSN1(ret, node, putobject,
Qfalse);
11553 ADD_LABEL(ret, lend);
11558 ADD_INSN(ret, node, putself);
11564 ADD_INSN(ret, node, putnil);
11570 ADD_INSN1(ret, node, putobject,
Qtrue);
11576 ADD_INSN1(ret, node, putobject,
Qfalse);
11581 CHECK(compile_errinfo(iseq, ret, node, popped));
11585 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11588 case NODE_POSTEXE:{
11592 int is_index = body->ise_size++;
11594 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11595 const rb_iseq_t *once_iseq =
11596 NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc, rb_fstring(make_name_for_block(iseq)), ISEQ_TYPE_BLOCK, line);
11598 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
11602 ADD_INSN(ret, node, pop);
11607 CHECK(compile_kw_arg(iseq, ret, node, popped));
11610 compile_dstr(iseq, ret, node);
11612 ADD_INSN(ret, node, intern);
11615 ADD_INSN(ret, node, pop);
11619 case NODE_ATTRASGN:
11620 CHECK(compile_attrasgn(iseq, ret, node, popped));
11624 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11627 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11628 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11632 ADD_INSN(ret, node, pop);
11637 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
11652insn_data_length(INSN *iobj)
11654 return insn_len(iobj->insn_id);
11658calc_sp_depth(
int depth, INSN *insn)
11660 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
11664opobj_inspect(
VALUE obj)
11684insn_data_to_s_detail(INSN *iobj)
11686 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
11688 if (iobj->operands) {
11689 const char *types = insn_op_types(iobj->insn_id);
11692 for (j = 0; types[j]; j++) {
11693 char type = types[j];
11698 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, j);
11699 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
11705 rb_iseq_t *iseq = (rb_iseq_t *)OPERAND_AT(iobj, j);
11717 VALUE v = OPERAND_AT(iobj, j);
11732 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11735 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11738 rb_str_catf(str,
"<ise:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
11744 if (vm_ci_mid(ci)) rb_str_catf(str,
"%"PRIsVALUE, rb_id2str(vm_ci_mid(ci)));
11745 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
11753 void *func = (
void *)OPERAND_AT(iobj, j);
11756 if (dladdr(func, &info) && info.dli_sname) {
11761 rb_str_catf(str,
"<%p>", func);
11771 if (types[j + 1]) {
11780dump_disasm_list(
const LINK_ELEMENT *link)
11782 dump_disasm_list_with_cursor(link, NULL, NULL);
11786dump_disasm_list_with_cursor(
const LINK_ELEMENT *link,
const LINK_ELEMENT *curr,
const LABEL *dest)
11793 printf(
"-- raw disasm--------\n");
11796 if (curr) printf(curr == link ?
"*" :
" ");
11797 switch (link->type) {
11798 case ISEQ_ELEMENT_INSN:
11800 iobj = (INSN *)link;
11801 str = insn_data_to_s_detail(iobj);
11802 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
11803 pos += insn_data_length(iobj);
11806 case ISEQ_ELEMENT_LABEL:
11808 lobj = (LABEL *)link;
11809 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
11810 dest == lobj ?
" <---" :
"");
11813 case ISEQ_ELEMENT_TRACE:
11815 TRACE *trace = (TRACE *)link;
11816 printf(
" trace: %0x\n", trace->event);
11819 case ISEQ_ELEMENT_ADJUST:
11821 ADJUST *adjust = (ADJUST *)link;
11822 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
11827 rb_raise(
rb_eSyntaxError,
"dump_disasm_list error: %d\n", (
int)link->type);
11831 printf(
"---------------------\n");
11836rb_insn_len(
VALUE insn)
11838 return insn_len(insn);
11842rb_insns_name(
int i)
11844 return insn_name(i);
11848rb_insns_name_array(
void)
11852 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
11859register_label(rb_iseq_t *iseq,
struct st_table *labels_table,
VALUE obj)
11863 obj = rb_to_symbol_type(obj);
11865 if (st_lookup(labels_table, obj, &tmp) == 0) {
11866 label = NEW_LABEL(0);
11867 st_insert(labels_table, obj, (st_data_t)label);
11870 label = (LABEL *)tmp;
11877get_exception_sym2type(
VALUE sym)
11879 static VALUE symRescue, symEnsure, symRetry;
11880 static VALUE symBreak, symRedo, symNext;
11882 if (symRescue == 0) {
11891 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
11892 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
11893 if (sym == symRetry)
return CATCH_TYPE_RETRY;
11894 if (sym == symBreak)
return CATCH_TYPE_BREAK;
11895 if (sym == symRedo)
return CATCH_TYPE_REDO;
11896 if (sym == symNext)
return CATCH_TYPE_NEXT;
11897 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
11902iseq_build_from_ary_exception(rb_iseq_t *iseq,
struct st_table *labels_table,
11908 const rb_iseq_t *eiseq;
11910 LABEL *lstart, *lend, *lcont;
11925 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
11926 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
11927 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
11931 if (
type == CATCH_TYPE_RESCUE ||
11932 type == CATCH_TYPE_BREAK ||
11933 type == CATCH_TYPE_NEXT) {
11939 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
11946static struct st_table *
11947insn_make_insn_table(
void)
11949 struct st_table *table;
11951 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
11953 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
11960static const rb_iseq_t *
11961iseq_build_load_iseq(
const rb_iseq_t *iseq,
VALUE op)
11964 const rb_iseq_t *loaded_iseq;
11967 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
11969 else if (
CLASS_OF(op) == rb_cISeq) {
11976 loaded_iseq = rb_iseqw_to_iseq(iseqw);
11977 return loaded_iseq;
11981iseq_build_callinfo_from_hash(rb_iseq_t *iseq,
VALUE op)
11985 unsigned int flag = 0;
11996 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
11998 if (!
NIL_P(vkw_arg)) {
12001 size_t n = rb_callinfo_kwarg_bytes(
len);
12004 kw_arg->references = 0;
12005 kw_arg->keyword_len =
len;
12006 for (i = 0; i <
len; i++) {
12009 kw_arg->keywords[i] = kw;
12014 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
12020event_name_to_flag(
VALUE sym)
12022#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
12036iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor,
12043 int line_no = 0, node_id = -1, insn_idx = 0;
12044 int ret = COMPILE_OK;
12049 static struct st_table *insn_table;
12051 if (insn_table == 0) {
12052 insn_table = insn_make_insn_table();
12055 for (i=0; i<
len; i++) {
12061 ADD_TRACE(anchor, event);
12064 LABEL *label = register_label(iseq, labels_table, obj);
12065 ADD_LABEL(anchor, label);
12082 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
12084 COMPILE_ERROR(iseq, line_no,
12085 "unknown instruction: %+"PRIsVALUE, insn);
12090 if (argc != insn_len((
VALUE)insn_id)-1) {
12091 COMPILE_ERROR(iseq, line_no,
12092 "operand size mismatch");
12098 argv = compile_data_calloc2(iseq,
sizeof(
VALUE), argc);
12102 (LINK_ELEMENT*)new_insn_core(iseq, line_no, node_id,
12103 (
enum ruby_vminsn_type)insn_id, argc, argv));
12105 for (j=0; j<argc; j++) {
12107 switch (insn_op_type((
VALUE)insn_id, j)) {
12109 LABEL *label = register_label(iseq, labels_table, op);
12110 argv[j] = (
VALUE)label;
12125 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
12136 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
12137 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
12143 op = rb_to_array_type(op);
12147 sym = rb_to_symbol_type(sym);
12152 argv[j] = segments;
12154 ISEQ_BODY(iseq)->ic_size++;
12159 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
12160 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
12165 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
12166 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
12170 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
12173 argv[j] = rb_to_symbol_type(op);
12180 RHASH_TBL_RAW(map)->type = &cdhash_type;
12181 op = rb_to_array_type(op);
12186 register_label(iseq, labels_table, sym);
12187 rb_hash_aset(map, key, (
VALUE)label | 1);
12197#if SIZEOF_VALUE <= SIZEOF_LONG
12202 argv[j] = (
VALUE)funcptr;
12212 (LINK_ELEMENT*)new_insn_core(iseq, line_no, node_id,
12213 (
enum ruby_vminsn_type)insn_id, argc, NULL));
12217 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
12222 validate_labels(iseq, labels_table);
12223 if (!ret)
return ret;
12224 return iseq_setup(iseq, anchor);
12227#define CHECK_ARRAY(v) rb_to_array_type(v)
12228#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
12233 VALUE val = rb_hash_aref(param, sym);
12238 else if (!
NIL_P(val)) {
12239 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
12245static const struct rb_iseq_param_keyword *
12246iseq_build_kw(rb_iseq_t *iseq,
VALUE params,
VALUE keywords)
12251 VALUE key, sym, default_val;
12254 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12256 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12258 keyword->num =
len;
12259#define SYM(s) ID2SYM(rb_intern_const(#s))
12260 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12261 i = keyword->bits_start - keyword->num;
12262 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12266 for (i = 0; i <
len; i++) {
12270 goto default_values;
12273 keyword->required_num++;
12277 default_len =
len - i;
12278 if (default_len == 0) {
12279 keyword->table = ids;
12282 else if (default_len < 0) {
12288 for (j = 0; i <
len; i++, j++) {
12302 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
12308 keyword->table = ids;
12309 keyword->default_values = dvs;
12315iseq_insn_each_object_mark_and_move(
VALUE * obj,
VALUE _)
12317 rb_gc_mark_and_move(obj);
12324 size_t size =
sizeof(INSN);
12325 unsigned int pos = 0;
12328#ifdef STRICT_ALIGNMENT
12329 size_t padding = calc_padding((
void *)&storage->buff[pos], size);
12331 const size_t padding = 0;
12333 size_t offset = pos + size + padding;
12334 if (offset > storage->size || offset > storage->pos) {
12336 storage = storage->next;
12339#ifdef STRICT_ALIGNMENT
12340 pos += (int)padding;
12343 iobj = (INSN *)&storage->buff[pos];
12345 if (iobj->operands) {
12346 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_move, (
VALUE)0);
12354 .wrap_struct_name =
"compiler/labels_wrapper",
12359 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
12363rb_iseq_build_from_ary(rb_iseq_t *iseq,
VALUE misc,
VALUE locals,
VALUE params,
12366#define SYM(s) ID2SYM(rb_intern_const(#s))
12368 unsigned int arg_size, local_size, stack_max;
12370 struct st_table *labels_table = st_init_numtable();
12372 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
12373 VALUE keywords = rb_hash_aref(params, SYM(keyword));
12375 DECL_ANCHOR(anchor);
12376 INIT_ANCHOR(anchor);
12379 ISEQ_BODY(iseq)->local_table_size =
len;
12380 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12382 for (i = 0; i <
len; i++) {
12385 if (sym_arg_rest == lv) {
12393#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12394 if (INT_PARAM(lead_num)) {
12395 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12397 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12398 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12399 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12400 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12403#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12405 INT_PARAM(arg_size);
12406 INT_PARAM(local_size);
12407 INT_PARAM(stack_max);
12412#ifdef USE_ISEQ_NODE_ID
12413 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
12421 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12423 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12426 for (i = 0; i <
len; i++) {
12428 LABEL *label = register_label(iseq, labels_table, ent);
12429 opt_table[i] = (
VALUE)label;
12432 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12433 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12436 else if (!
NIL_P(arg_opt_labels)) {
12437 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
12442 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12444 else if (!
NIL_P(keywords)) {
12445 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
12449 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
12450 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12453 if (
Qtrue == rb_hash_aref(params, SYM(use_block))) {
12454 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12457 if (int_param(&i, params, SYM(kwrest))) {
12458 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12459 if (keyword == NULL) {
12460 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12462 keyword->rest_start = i;
12463 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12466 iseq_calc_param_size(iseq);
12469 iseq_build_from_ary_exception(iseq, labels_table, exception);
12472 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12474 ISEQ_BODY(iseq)->param.size = arg_size;
12475 ISEQ_BODY(iseq)->local_table_size = local_size;
12476 ISEQ_BODY(iseq)->stack_max = stack_max;
12482rb_dvar_defined(
ID id,
const rb_iseq_t *iseq)
12486 while (body->type == ISEQ_TYPE_BLOCK ||
12487 body->type == ISEQ_TYPE_RESCUE ||
12488 body->type == ISEQ_TYPE_ENSURE ||
12489 body->type == ISEQ_TYPE_EVAL ||
12490 body->type == ISEQ_TYPE_MAIN
12494 for (i = 0; i < body->local_table_size; i++) {
12495 if (body->local_table[i] ==
id) {
12499 iseq = body->parent_iseq;
12500 body = ISEQ_BODY(iseq);
12507rb_local_defined(
ID id,
const rb_iseq_t *iseq)
12513 for (i=0; i<body->local_table_size; i++) {
12514 if (body->local_table[i] ==
id) {
12524#ifndef IBF_ISEQ_DEBUG
12525#define IBF_ISEQ_DEBUG 0
12528#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12529#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12532typedef uint32_t ibf_offset_t;
12533#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12535#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12537#define IBF_DEVEL_VERSION 5
12538#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12540#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12543static const char IBF_ENDIAN_MARK =
12544#ifdef WORDS_BIGENDIAN
12553 uint32_t major_version;
12554 uint32_t minor_version;
12556 uint32_t extra_size;
12558 uint32_t iseq_list_size;
12559 uint32_t global_object_list_size;
12560 ibf_offset_t iseq_list_offset;
12561 ibf_offset_t global_object_list_offset;
12582 unsigned int obj_list_size;
12583 ibf_offset_t obj_list_offset;
12602pinned_list_mark(
void *ptr)
12606 for (i = 0; i < list->size; i++) {
12607 if (list->buffer[i]) {
12608 rb_gc_mark(list->buffer[i]);
12620 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
12624pinned_list_fetch(
VALUE list,
long offset)
12630 if (offset >= ptr->size) {
12631 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12634 return ptr->buffer[offset];
12638pinned_list_store(
VALUE list,
long offset,
VALUE object)
12644 if (offset >= ptr->size) {
12645 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12652pinned_list_new(
long size)
12654 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
12655 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
12656 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
12662ibf_dump_pos(
struct ibf_dump *dump)
12664 long pos = RSTRING_LEN(dump->current_buffer->str);
12665#if SIZEOF_LONG > SIZEOF_INT
12666 if (pos >= UINT_MAX) {
12670 return (
unsigned int)pos;
12674ibf_dump_align(
struct ibf_dump *dump,
size_t align)
12676 ibf_offset_t pos = ibf_dump_pos(dump);
12678 static const char padding[
sizeof(
VALUE)];
12679 size_t size = align - ((size_t)pos % align);
12680#if SIZEOF_LONG > SIZEOF_INT
12681 if (pos + size >= UINT_MAX) {
12685 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
12686 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
12688 rb_str_cat(dump->current_buffer->str, padding, size);
12693ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
12695 ibf_offset_t pos = ibf_dump_pos(dump);
12696#if SIZEOF_LONG > SIZEOF_INT
12698 if (size >= UINT_MAX || pos + size >= UINT_MAX) {
12702 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
12707ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
12709 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
12713ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
12715 VALUE str = dump->current_buffer->str;
12716 char *ptr = RSTRING_PTR(str);
12717 if ((
unsigned long)(size + offset) > (
unsigned long)RSTRING_LEN(str))
12718 rb_bug(
"ibf_dump_overwrite: overflow");
12719 memcpy(ptr + offset, buff, size);
12723ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
12725 ibf_offset_t beg = *offset;
12727 return load->current_buffer->buff + beg;
12731ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
12733 void *buff = ruby_xmalloc2(x, y);
12734 size_t size = x * y;
12735 memcpy(buff, load->current_buffer->buff + offset, size);
12739#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
12741#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
12742#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
12743#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
12744#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
12745#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
12748ibf_table_lookup(
struct st_table *table, st_data_t key)
12752 if (st_lookup(table, key, &val)) {
12761ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
12763 int index = ibf_table_lookup(table, key);
12766 index = (int)table->num_entries;
12767 st_insert(table, key, (st_data_t)index);
12775static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
12778static rb_iseq_t *ibf_load_iseq(
const struct ibf_load *load,
const rb_iseq_t *index_iseq);
12781ibf_dump_object_table_new(
void)
12783 st_table *obj_table = st_init_numtable();
12784 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
12792 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
12798 if (
id == 0 || rb_id2name(
id) == NULL) {
12801 return ibf_dump_object(dump,
rb_id2sym(
id));
12805ibf_load_id(
const struct ibf_load *load,
const ID id_index)
12807 if (id_index == 0) {
12810 VALUE sym = ibf_load_object(load, id_index);
12820static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
12823ibf_dump_iseq(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
12825 if (iseq == NULL) {
12829 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
12833static unsigned char
12834ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
12836 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
12837 return (
unsigned char)load->current_buffer->buff[(*offset)++];
12853 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12854 ibf_dump_write(dump, &x,
sizeof(
VALUE));
12858 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12860 unsigned char bytes[max_byte_length];
12863 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
12864 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12870 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
12873 ibf_dump_write(dump, bytes + max_byte_length - n, n);
12877ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
12879 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
12880 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
12882 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
12883 *offset +=
sizeof(
VALUE);
12888 enum { max_byte_length =
sizeof(
VALUE) + 1 };
12890 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
12891 const unsigned char c = buffer[*offset];
12895 c == 0 ? 9 : ntz_int32(c) + 1;
12898 if (*offset + n > load->current_buffer->size) {
12903 for (i = 1; i < n; i++) {
12905 x |= (
VALUE)buffer[*offset + i];
12919 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
12921 size_t len = strlen(bf->name);
12922 ibf_dump_write_small_value(dump, (
VALUE)
len);
12923 ibf_dump_write(dump, bf->name,
len);
12927ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
12929 int i = (int)ibf_load_small_value(load, offset);
12930 int len = (int)ibf_load_small_value(load, offset);
12931 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
12934 fprintf(stderr,
"%.*s!!\n",
len, name);
12938 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
12939 if (strncmp(table[i].name, name,
len) != 0) {
12940 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
12948ibf_dump_code(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
12951 const int iseq_size = body->iseq_size;
12953 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
12955 ibf_offset_t offset = ibf_dump_pos(dump);
12957 for (code_index=0; code_index<iseq_size;) {
12958 const VALUE insn = orig_code[code_index++];
12959 const char *types = insn_op_types(insn);
12964 ibf_dump_write_small_value(dump, insn);
12967 for (op_index=0; types[op_index]; op_index++, code_index++) {
12968 VALUE op = orig_code[code_index];
12971 switch (types[op_index]) {
12974 wv = ibf_dump_object(dump, op);
12977 wv = (
VALUE)ibf_dump_iseq(dump, (
const rb_iseq_t *)op);
12983 wv = ibf_dump_object(dump, arr);
12991 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
12999 wv = ibf_dump_id(dump, (
ID)op);
13011 ibf_dump_write_small_value(dump, wv);
13021ibf_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)
13024 unsigned int code_index;
13025 ibf_offset_t reading_pos = bytecode_offset;
13029 struct rb_call_data *cd_entries = load_body->call_data;
13032 load_body->iseq_encoded = code;
13033 load_body->iseq_size = 0;
13035 iseq_bits_t * mark_offset_bits;
13037 iseq_bits_t tmp[1] = {0};
13039 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
13040 mark_offset_bits = tmp;
13043 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
13045 bool needs_bitmap =
false;
13047 for (code_index=0; code_index<iseq_size;) {
13049 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
13050 const char *types = insn_op_types(insn);
13056 for (op_index=0; types[op_index]; op_index++, code_index++) {
13057 const char operand_type = types[op_index];
13058 switch (operand_type) {
13061 VALUE op = ibf_load_small_value(load, &reading_pos);
13062 VALUE v = ibf_load_object(load, op);
13063 code[code_index] = v;
13066 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13067 needs_bitmap =
true;
13073 VALUE op = ibf_load_small_value(load, &reading_pos);
13074 VALUE v = ibf_load_object(load, op);
13075 v = rb_hash_dup(v);
13076 RHASH_TBL_RAW(v)->type = &cdhash_type;
13078 RB_OBJ_SET_SHAREABLE(freeze_hide_obj(v));
13083 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
13085 code[code_index] = v;
13086 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13088 needs_bitmap =
true;
13093 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
13094 VALUE v = (
VALUE)ibf_load_iseq(load, (
const rb_iseq_t *)op);
13095 code[code_index] = v;
13098 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13099 needs_bitmap =
true;
13105 VALUE op = ibf_load_small_value(load, &reading_pos);
13106 VALUE arr = ibf_load_object(load, op);
13108 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
13109 ic->
segments = array_to_idlist(arr);
13111 code[code_index] = (
VALUE)ic;
13118 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13120 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
13121 code[code_index] = (
VALUE)ic;
13123 if (operand_type == TS_IVC) {
13124 IVC cache = (IVC)ic;
13126 if (insn == BIN(setinstancevariable)) {
13127 ID iv_name = (
ID)code[code_index - 1];
13128 cache->iv_set_name = iv_name;
13131 cache->iv_set_name = 0;
13134 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
13141 code[code_index] = (
VALUE)cd_entries++;
13146 VALUE op = ibf_load_small_value(load, &reading_pos);
13147 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
13154 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
13157 code[code_index] = ibf_load_small_value(load, &reading_pos);
13161 if (insn_len(insn) != op_index+1) {
13166 load_body->iseq_size = code_index;
13168 if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
13169 load_body->mark_bits.single = mark_offset_bits[0];
13172 if (needs_bitmap) {
13173 load_body->mark_bits.list = mark_offset_bits;
13176 load_body->mark_bits.list = 0;
13177 ruby_xfree(mark_offset_bits);
13182 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
13187ibf_dump_param_opt_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13189 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
13192 IBF_W_ALIGN(
VALUE);
13193 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
13196 return ibf_dump_pos(dump);
13201ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
13205 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
13214ibf_dump_param_keyword(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13216 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
13219 struct rb_iseq_param_keyword dump_kw = *kw;
13220 int dv_num = kw->num - kw->required_num;
13225 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
13226 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
13228 dump_kw.table = IBF_W(ids,
ID, kw->num);
13229 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
13230 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
13231 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
13238static const struct rb_iseq_param_keyword *
13239ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
13241 if (param_keyword_offset) {
13242 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
13243 int dv_num = kw->num - kw->required_num;
13244 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
13247 for (i=0; i<dv_num; i++) {
13248 dvs[i] = ibf_load_object(load, dvs[i]);
13254 kw->default_values = dvs;
13263ibf_dump_insns_info_body(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13265 ibf_offset_t offset = ibf_dump_pos(dump);
13269 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
13270 ibf_dump_write_small_value(dump, entries[i].line_no);
13271#ifdef USE_ISEQ_NODE_ID
13272 ibf_dump_write_small_value(dump, entries[i].node_id);
13274 ibf_dump_write_small_value(dump, entries[i].events);
13281ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
13283 ibf_offset_t reading_pos = body_offset;
13287 for (i = 0; i < size; i++) {
13288 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
13289#ifdef USE_ISEQ_NODE_ID
13290 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
13292 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
13299ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
13301 ibf_offset_t offset = ibf_dump_pos(dump);
13303 unsigned int last = 0;
13305 for (i = 0; i < size; i++) {
13306 ibf_dump_write_small_value(dump, positions[i] - last);
13307 last = positions[i];
13313static unsigned int *
13314ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
13316 ibf_offset_t reading_pos = positions_offset;
13317 unsigned int *positions =
ALLOC_N(
unsigned int, size);
13319 unsigned int last = 0;
13321 for (i = 0; i < size; i++) {
13322 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
13323 last = positions[i];
13330ibf_dump_local_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13333 const int size = body->local_table_size;
13337 for (i=0; i<size; i++) {
13338 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13341 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13347 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
13351ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13354 ID *table = IBF_R(local_table_offset,
ID, size);
13357 for (i=0; i<size; i++) {
13358 table[i] = ibf_load_id(load, table[i]);
13361 if (size == 1 && table[0] == idERROR_INFO) {
13363 return rb_iseq_shared_exc_local_tbl;
13375ibf_dump_lvar_states(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13378 const int size = body->local_table_size;
13379 IBF_W_ALIGN(
enum lvar_state);
13380 return ibf_dump_write(dump, body->lvar_states,
sizeof(
enum lvar_state) * (body->lvar_states ? size : 0));
13383static enum lvar_state *
13384ibf_load_lvar_states(
const struct ibf_load *load, ibf_offset_t lvar_states_offset,
int size,
const ID *local_table)
13386 if (local_table == rb_iseq_shared_exc_local_tbl ||
13391 enum lvar_state *states = IBF_R(lvar_states_offset,
enum lvar_state, size);
13397ibf_dump_catch_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13402 int *iseq_indices =
ALLOCA_N(
int, table->size);
13405 for (i=0; i<table->size; i++) {
13406 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13409 const ibf_offset_t offset = ibf_dump_pos(dump);
13411 for (i=0; i<table->size; i++) {
13412 ibf_dump_write_small_value(dump, iseq_indices[i]);
13413 ibf_dump_write_small_value(dump, table->entries[i].type);
13414 ibf_dump_write_small_value(dump, table->entries[i].start);
13415 ibf_dump_write_small_value(dump, table->entries[i].end);
13416 ibf_dump_write_small_value(dump, table->entries[i].cont);
13417 ibf_dump_write_small_value(dump, table->entries[i].sp);
13422 return ibf_dump_pos(dump);
13427ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size,
const rb_iseq_t *parent_iseq)
13430 struct iseq_catch_table *table = ruby_xcalloc(1, iseq_catch_table_bytes(size));
13431 table->size = size;
13432 ISEQ_BODY(parent_iseq)->catch_table = table;
13434 ibf_offset_t reading_pos = catch_table_offset;
13437 for (i=0; i<table->size; i++) {
13438 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13439 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13440 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13441 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13442 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13443 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13445 rb_iseq_t *catch_iseq = (rb_iseq_t *)ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)iseq_index);
13446 RB_OBJ_WRITE(parent_iseq, UNALIGNED_MEMBER_PTR(&table->entries[i], iseq), catch_iseq);
13450 ISEQ_BODY(parent_iseq)->catch_table = NULL;
13455ibf_dump_ci_entries(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13458 const unsigned int ci_size = body->ci_size;
13461 ibf_offset_t offset = ibf_dump_pos(dump);
13465 for (i = 0; i < ci_size; i++) {
13468 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13469 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13470 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13474 int len = kwarg->keyword_len;
13475 ibf_dump_write_small_value(dump,
len);
13476 for (
int j=0; j<
len; j++) {
13477 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13478 ibf_dump_write_small_value(dump, keyword);
13482 ibf_dump_write_small_value(dump, 0);
13487 ibf_dump_write_small_value(dump, (
VALUE)-1);
13505static enum rb_id_table_iterator_result
13506store_outer_variable(
ID id,
VALUE val,
void *dump)
13511 pair->name = rb_id2str(
id);
13513 return ID_TABLE_CONTINUE;
13517outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13525 else if (!bp->name) {
13533ibf_dump_outer_variables(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13535 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13537 ibf_offset_t offset = ibf_dump_pos(dump);
13539 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13540 ibf_dump_write_small_value(dump, (
VALUE)size);
13549 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13551 for (
size_t i = 0; i < size; ++i) {
13552 ID id = ovlist->pairs[i].id;
13553 ID val = ovlist->pairs[i].val;
13554 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13555 ibf_dump_write_small_value(dump, val);
13564ibf_load_ci_entries(
const struct ibf_load *load,
13565 ibf_offset_t ci_entries_offset,
13566 unsigned int ci_size,
13574 ibf_offset_t reading_pos = ci_entries_offset;
13581 for (i = 0; i < ci_size; i++) {
13582 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
13583 if (mid_index != (
VALUE)-1) {
13584 ID mid = ibf_load_id(load, mid_index);
13585 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13586 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13589 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
13592 kwarg->references = 0;
13593 kwarg->keyword_len = kwlen;
13594 for (
int j=0; j<kwlen; j++) {
13595 VALUE keyword = ibf_load_small_value(load, &reading_pos);
13596 kwarg->keywords[j] = ibf_load_object(load, keyword);
13600 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
13602 cds[i].cc = vm_cc_empty();
13613ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
13615 ibf_offset_t reading_pos = outer_variables_offset;
13619 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
13621 if (table_size > 0) {
13622 tbl = rb_id_table_create(table_size);
13625 for (
size_t i = 0; i < table_size; i++) {
13626 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
13627 VALUE value = ibf_load_small_value(load, &reading_pos);
13628 if (!key) key = rb_make_temporary_id(i);
13629 rb_id_table_insert(tbl, key, value);
13636ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13638 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
13640 unsigned int *positions;
13644 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
13645 const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
13646 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
13648#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13649 ibf_offset_t iseq_start = ibf_dump_pos(dump);
13654 buffer.obj_table = ibf_dump_object_table_new();
13655 dump->current_buffer = &buffer;
13658 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
13659 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
13660 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
13661 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
13662 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
13664 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
13665 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
13666 ruby_xfree(positions);
13668 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
13669 const ibf_offset_t lvar_states_offset = ibf_dump_lvar_states(dump, iseq);
13670 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
13671 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
13672 const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
13673 const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
13674 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
13675 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
13676 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
13678#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13679 ibf_offset_t local_obj_list_offset;
13680 unsigned int local_obj_list_size;
13682 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
13685 ibf_offset_t body_offset = ibf_dump_pos(dump);
13688 unsigned int param_flags =
13689 (body->param.flags.has_lead << 0) |
13690 (body->param.flags.has_opt << 1) |
13691 (body->param.flags.has_rest << 2) |
13692 (body->param.flags.has_post << 3) |
13693 (body->param.flags.has_kw << 4) |
13694 (body->param.flags.has_kwrest << 5) |
13695 (body->param.flags.has_block << 6) |
13696 (body->param.flags.ambiguous_param0 << 7) |
13697 (body->param.flags.accepts_no_kwarg << 8) |
13698 (body->param.flags.ruby2_keywords << 9) |
13699 (body->param.flags.anon_rest << 10) |
13700 (body->param.flags.anon_kwrest << 11) |
13701 (body->param.flags.use_block << 12) |
13702 (body->param.flags.forwardable << 13) ;
13704#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13705# define IBF_BODY_OFFSET(x) (x)
13707# define IBF_BODY_OFFSET(x) (body_offset - (x))
13710 ibf_dump_write_small_value(dump, body->type);
13711 ibf_dump_write_small_value(dump, body->iseq_size);
13712 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
13713 ibf_dump_write_small_value(dump, bytecode_size);
13714 ibf_dump_write_small_value(dump, param_flags);
13715 ibf_dump_write_small_value(dump, body->param.size);
13716 ibf_dump_write_small_value(dump, body->param.lead_num);
13717 ibf_dump_write_small_value(dump, body->param.opt_num);
13718 ibf_dump_write_small_value(dump, body->param.rest_start);
13719 ibf_dump_write_small_value(dump, body->param.post_start);
13720 ibf_dump_write_small_value(dump, body->param.post_num);
13721 ibf_dump_write_small_value(dump, body->param.block_start);
13722 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
13723 ibf_dump_write_small_value(dump, param_keyword_offset);
13724 ibf_dump_write_small_value(dump, location_pathobj_index);
13725 ibf_dump_write_small_value(dump, location_base_label_index);
13726 ibf_dump_write_small_value(dump, location_label_index);
13727 ibf_dump_write_small_value(dump, body->location.first_lineno);
13728 ibf_dump_write_small_value(dump, body->location.node_id);
13729 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
13730 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
13731 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
13732 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
13733 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
13734 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
13735 ibf_dump_write_small_value(dump, body->insns_info.size);
13736 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
13737 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(lvar_states_offset));
13738 ibf_dump_write_small_value(dump, catch_table_size);
13739 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
13740 ibf_dump_write_small_value(dump, parent_iseq_index);
13741 ibf_dump_write_small_value(dump, local_iseq_index);
13742 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
13743 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
13744 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
13745 ibf_dump_write_small_value(dump, body->variable.flip_count);
13746 ibf_dump_write_small_value(dump, body->local_table_size);
13747 ibf_dump_write_small_value(dump, body->ivc_size);
13748 ibf_dump_write_small_value(dump, body->icvarc_size);
13749 ibf_dump_write_small_value(dump, body->ise_size);
13750 ibf_dump_write_small_value(dump, body->ic_size);
13751 ibf_dump_write_small_value(dump, body->ci_size);
13752 ibf_dump_write_small_value(dump, body->stack_max);
13753 ibf_dump_write_small_value(dump, body->builtin_attrs);
13754 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
13756#undef IBF_BODY_OFFSET
13758#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13759 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
13761 dump->current_buffer = saved_buffer;
13762 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
13764 ibf_offset_t offset = ibf_dump_pos(dump);
13765 ibf_dump_write_small_value(dump, iseq_start);
13766 ibf_dump_write_small_value(dump, iseq_length_bytes);
13767 ibf_dump_write_small_value(dump, body_offset);
13769 ibf_dump_write_small_value(dump, local_obj_list_offset);
13770 ibf_dump_write_small_value(dump, local_obj_list_size);
13772 st_free_table(buffer.obj_table);
13776 return body_offset;
13781ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
13783 VALUE str = ibf_load_object(load, str_index);
13785 str = rb_fstring(str);
13791ibf_load_iseq_each(
struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t offset)
13795 ibf_offset_t reading_pos = offset;
13797#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13799 load->current_buffer = &load->global_buffer;
13801 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13802 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13803 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13806 buffer.buff = load->global_buffer.buff + iseq_start;
13807 buffer.size = iseq_length_bytes;
13808 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13809 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13810 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
13812 load->current_buffer = &buffer;
13813 reading_pos = body_offset;
13816#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13817# define IBF_BODY_OFFSET(x) (x)
13819# define IBF_BODY_OFFSET(x) (offset - (x))
13822 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13823 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13824 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13825 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13826 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13827 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13828 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
13829 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
13830 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
13831 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
13832 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
13833 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
13834 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13835 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
13836 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
13837 const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
13838 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
13839 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
13840 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
13841 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13842 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13843 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
13844 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
13845 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13846 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13847 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13848 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13849 const ibf_offset_t lvar_states_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13850 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13851 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13852 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13853 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13854 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13855 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13856 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
13857 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
13858 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13860 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13861 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13862 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13863 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13865 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13866 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13867 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13868 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
13871 VALUE path = ibf_load_object(load, location_pathobj_index);
13876 realpath = path = rb_fstring(path);
13879 VALUE pathobj = path;
13885 if (!
NIL_P(realpath)) {
13887 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
13888 "(%x), path=%+"PRIsVALUE,
13889 realpath,
TYPE(realpath), path);
13891 realpath = rb_fstring(realpath);
13897 rb_iseq_pathobj_set(iseq, path, realpath);
13901 rb_execution_context_t *ec = GET_EC();
13902 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
13904#undef IBF_BODY_OFFSET
13906 load_body->type =
type;
13907 load_body->stack_max = stack_max;
13908 load_body->param.flags.has_lead = (param_flags >> 0) & 1;
13909 load_body->param.flags.has_opt = (param_flags >> 1) & 1;
13910 load_body->param.flags.has_rest = (param_flags >> 2) & 1;
13911 load_body->param.flags.has_post = (param_flags >> 3) & 1;
13912 load_body->param.flags.has_kw = FALSE;
13913 load_body->param.flags.has_kwrest = (param_flags >> 5) & 1;
13914 load_body->param.flags.has_block = (param_flags >> 6) & 1;
13915 load_body->param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
13916 load_body->param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
13917 load_body->param.flags.ruby2_keywords = (param_flags >> 9) & 1;
13918 load_body->param.flags.anon_rest = (param_flags >> 10) & 1;
13919 load_body->param.flags.anon_kwrest = (param_flags >> 11) & 1;
13920 load_body->param.flags.use_block = (param_flags >> 12) & 1;
13921 load_body->param.flags.forwardable = (param_flags >> 13) & 1;
13922 load_body->param.size = param_size;
13923 load_body->param.lead_num = param_lead_num;
13924 load_body->param.opt_num = param_opt_num;
13925 load_body->param.rest_start = param_rest_start;
13926 load_body->param.post_start = param_post_start;
13927 load_body->param.post_num = param_post_num;
13928 load_body->param.block_start = param_block_start;
13929 load_body->local_table_size = local_table_size;
13930 load_body->ci_size = ci_size;
13931 load_body->insns_info.size = insns_info_size;
13933 ISEQ_COVERAGE_SET(iseq,
Qnil);
13934 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
13935 load_body->variable.flip_count = variable_flip_count;
13936 load_body->variable.script_lines =
Qnil;
13938 load_body->location.first_lineno = location_first_lineno;
13939 load_body->location.node_id = location_node_id;
13940 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
13941 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
13942 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
13943 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
13944 load_body->builtin_attrs = builtin_attrs;
13945 load_body->prism = prism;
13947 load_body->ivc_size = ivc_size;
13948 load_body->icvarc_size = icvarc_size;
13949 load_body->ise_size = ise_size;
13950 load_body->ic_size = ic_size;
13952 if (ISEQ_IS_SIZE(load_body)) {
13956 load_body->is_entries = NULL;
13958 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
13959 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
13960 load_body->param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
13961 load_body->param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
13962 load_body->param.flags.has_kw = (param_flags >> 4) & 1;
13963 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
13964 load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
13965 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
13966 load_body->lvar_states = ibf_load_lvar_states(load, lvar_states_offset, local_table_size, load_body->local_table);
13967 ibf_load_catch_table(load, catch_table_offset, catch_table_size, iseq);
13969 const rb_iseq_t *parent_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)parent_iseq_index);
13970 const rb_iseq_t *local_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)local_iseq_index);
13971 const rb_iseq_t *mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
13973 RB_OBJ_WRITE(iseq, &load_body->parent_iseq, parent_iseq);
13974 RB_OBJ_WRITE(iseq, &load_body->local_iseq, local_iseq);
13975 RB_OBJ_WRITE(iseq, &load_body->mandatory_only_iseq, mandatory_only_iseq);
13978 if (load_body->param.keyword != NULL) {
13980 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->param.keyword;
13981 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
13984 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
13985#if VM_INSN_INFO_TABLE_IMPL == 2
13986 rb_iseq_insns_info_encode_positions(iseq);
13989 rb_iseq_translate_threaded_code(iseq);
13991#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13992 load->current_buffer = &load->global_buffer;
13995 RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
13996 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
13998#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
13999 load->current_buffer = saved_buffer;
14001 verify_call_cache(iseq);
14004 rb_vm_pop_frame_no_int(ec);
14014ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14016 const rb_iseq_t *iseq = (
const rb_iseq_t *)key;
14019 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
14022 return ST_CONTINUE;
14032 args.offset_list = offset_list;
14034 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
14037 st_index_t size = dump->iseq_table->num_entries;
14038 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
14040 for (i = 0; i < size; i++) {
14044 ibf_dump_align(dump,
sizeof(ibf_offset_t));
14045 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
14046 header->iseq_list_size = (
unsigned int)size;
14056 unsigned int type: 5;
14057 unsigned int special_const: 1;
14058 unsigned int frozen: 1;
14059 unsigned int internal: 1;
14062enum ibf_object_class_index {
14063 IBF_OBJECT_CLASS_OBJECT,
14064 IBF_OBJECT_CLASS_ARRAY,
14065 IBF_OBJECT_CLASS_STANDARD_ERROR,
14066 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
14067 IBF_OBJECT_CLASS_TYPE_ERROR,
14068 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
14078 long keyval[FLEX_ARY_LEN];
14091 BDIGIT digits[FLEX_ARY_LEN];
14094enum ibf_object_data_type {
14095 IBF_OBJECT_DATA_ENCODING,
14106#define IBF_ALIGNED_OFFSET(align, offset) \
14107 ((((offset) - 1) / (align) + 1) * (align))
14112#define IBF_OBJBODY(type, offset) \
14113 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
14116ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
14118 if (offset >= load->current_buffer->size) {
14119 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
14121 return load->current_buffer->buff + offset;
14124NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
14127ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
14130 rb_raw_obj_info(buff,
sizeof(buff), obj);
14139 rb_raise(rb_eArgError,
"unsupported");
14146 enum ibf_object_class_index cindex;
14147 if (obj == rb_cObject) {
14148 cindex = IBF_OBJECT_CLASS_OBJECT;
14151 cindex = IBF_OBJECT_CLASS_ARRAY;
14154 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
14157 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
14160 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
14163 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
14166 rb_obj_info_dump(obj);
14168 rb_bug(
"unsupported class");
14170 ibf_dump_write_small_value(dump, (
VALUE)cindex);
14176 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
14179 case IBF_OBJECT_CLASS_OBJECT:
14181 case IBF_OBJECT_CLASS_ARRAY:
14183 case IBF_OBJECT_CLASS_STANDARD_ERROR:
14185 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
14187 case IBF_OBJECT_CLASS_TYPE_ERROR:
14189 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
14193 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
14201 (void)IBF_W(&dbl,
double, 1);
14209 memcpy(&d, IBF_OBJBODY(
double, offset),
sizeof(d));
14211 if (!
FLONUM_P(f)) RB_OBJ_SET_SHAREABLE(f);
14218 long encindex = (long)rb_enc_get_index(obj);
14219 long len = RSTRING_LEN(obj);
14220 const char *ptr = RSTRING_PTR(obj);
14222 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14223 rb_encoding *enc = rb_enc_from_index((
int)encindex);
14224 const char *enc_name = rb_enc_name(enc);
14225 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
14228 ibf_dump_write_small_value(dump, encindex);
14229 ibf_dump_write_small_value(dump,
len);
14230 IBF_WP(ptr,
char,
len);
14236 ibf_offset_t reading_pos = offset;
14238 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14239 const long len = (long)ibf_load_small_value(load, &reading_pos);
14240 const char *ptr = load->current_buffer->buff + reading_pos;
14242 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14243 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14244 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14248 if (header->frozen && !header->internal) {
14249 str = rb_enc_literal_str(ptr,
len, rb_enc_from_index(encindex));
14252 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
14255 if (header->frozen) str = rb_fstring(str);
14266 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
14268 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
14269 ibf_dump_write_small_value(dump, regexp.srcstr);
14276 regexp.option = ibf_load_byte(load, &offset);
14277 regexp.srcstr = ibf_load_small_value(load, &offset);
14279 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
14280 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
14283 if (header->frozen) RB_OBJ_SET_SHAREABLE(
rb_obj_freeze(reg));
14292 ibf_dump_write_small_value(dump,
len);
14293 for (i=0; i<
len; i++) {
14294 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
14295 ibf_dump_write_small_value(dump, index);
14302 ibf_offset_t reading_pos = offset;
14304 const long len = (long)ibf_load_small_value(load, &reading_pos);
14309 for (i=0; i<
len; i++) {
14310 const VALUE index = ibf_load_small_value(load, &reading_pos);
14314 if (header->frozen) {
14315 rb_ary_freeze(ary);
14316 rb_ractor_make_shareable(ary);
14323ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
14327 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14328 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14330 ibf_dump_write_small_value(dump, key_index);
14331 ibf_dump_write_small_value(dump, val_index);
14332 return ST_CONTINUE;
14339 ibf_dump_write_small_value(dump, (
VALUE)
len);
14347 long len = (long)ibf_load_small_value(load, &offset);
14348 VALUE obj = rb_hash_new_with_size(
len);
14351 for (i = 0; i <
len; i++) {
14352 VALUE key_index = ibf_load_small_value(load, &offset);
14353 VALUE val_index = ibf_load_small_value(load, &offset);
14355 VALUE key = ibf_load_object(load, key_index);
14356 VALUE val = ibf_load_object(load, val_index);
14357 rb_hash_aset(obj, key, val);
14359 rb_hash_rehash(obj);
14362 if (header->frozen) {
14363 RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14377 range.class_index = 0;
14380 range.beg = (long)ibf_dump_object(dump, beg);
14381 range.end = (long)ibf_dump_object(dump, end);
14387 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
14396 VALUE beg = ibf_load_object(load, range->beg);
14397 VALUE end = ibf_load_object(load, range->end);
14400 if (header->frozen) RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14407 ssize_t
len = BIGNUM_LEN(obj);
14408 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14409 BDIGIT *d = BIGNUM_DIGITS(obj);
14411 (void)IBF_W(&slen, ssize_t, 1);
14412 IBF_WP(d, BDIGIT,
len);
14419 int sign = bignum->slen > 0;
14420 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14421 const int big_unpack_flags =
14424 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
14428 if (header->frozen) RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14435 if (rb_data_is_encoding(obj)) {
14436 rb_encoding *enc = rb_to_encoding(obj);
14437 const char *name = rb_enc_name(enc);
14438 long len = strlen(name) + 1;
14440 data[0] = IBF_OBJECT_DATA_ENCODING;
14442 (void)IBF_W(data,
long, 2);
14443 IBF_WP(name,
char,
len);
14446 ibf_dump_object_unsupported(dump, obj);
14453 const long *body = IBF_OBJBODY(
long, offset);
14454 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14456 const char *data = (
const char *)&body[2];
14459 case IBF_OBJECT_DATA_ENCODING:
14461 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
14466 return ibf_load_object_unsupported(load, header, offset);
14470ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14473 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14474 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14476 (void)IBF_W(data,
long, 2);
14480ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14483 VALUE a = ibf_load_object(load, nums->a);
14484 VALUE b = ibf_load_object(load, nums->b);
14496 ibf_dump_object_string(dump,
rb_sym2str(obj));
14502 ibf_offset_t reading_pos = offset;
14504 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14505 const long len = (long)ibf_load_small_value(load, &reading_pos);
14506 const char *ptr = load->current_buffer->buff + reading_pos;
14508 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14509 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14510 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14513 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
14517typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
14518static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
14519 ibf_dump_object_unsupported,
14520 ibf_dump_object_unsupported,
14521 ibf_dump_object_class,
14522 ibf_dump_object_unsupported,
14523 ibf_dump_object_float,
14524 ibf_dump_object_string,
14525 ibf_dump_object_regexp,
14526 ibf_dump_object_array,
14527 ibf_dump_object_hash,
14528 ibf_dump_object_struct,
14529 ibf_dump_object_bignum,
14530 ibf_dump_object_unsupported,
14531 ibf_dump_object_data,
14532 ibf_dump_object_unsupported,
14533 ibf_dump_object_complex_rational,
14534 ibf_dump_object_complex_rational,
14535 ibf_dump_object_unsupported,
14536 ibf_dump_object_unsupported,
14537 ibf_dump_object_unsupported,
14538 ibf_dump_object_unsupported,
14539 ibf_dump_object_symbol,
14540 ibf_dump_object_unsupported,
14541 ibf_dump_object_unsupported,
14542 ibf_dump_object_unsupported,
14543 ibf_dump_object_unsupported,
14544 ibf_dump_object_unsupported,
14545 ibf_dump_object_unsupported,
14546 ibf_dump_object_unsupported,
14547 ibf_dump_object_unsupported,
14548 ibf_dump_object_unsupported,
14549 ibf_dump_object_unsupported,
14550 ibf_dump_object_unsupported,
14556 unsigned char byte =
14557 (header.type << 0) |
14558 (header.special_const << 5) |
14559 (header.frozen << 6) |
14560 (header.internal << 7);
14566ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
14568 unsigned char byte = ibf_load_byte(load, offset);
14571 header.type = (
byte >> 0) & 0x1f;
14572 header.special_const = (
byte >> 5) & 0x01;
14573 header.frozen = (
byte >> 6) & 0x01;
14574 header.internal = (
byte >> 7) & 0x01;
14583 ibf_offset_t current_offset;
14584 IBF_ZERO(obj_header);
14585 obj_header.type =
TYPE(obj);
14587 IBF_W_ALIGN(ibf_offset_t);
14588 current_offset = ibf_dump_pos(dump);
14593 obj_header.special_const = TRUE;
14594 obj_header.frozen = TRUE;
14595 obj_header.internal = TRUE;
14596 ibf_dump_object_object_header(dump, obj_header);
14597 ibf_dump_write_small_value(dump, obj);
14601 obj_header.special_const = FALSE;
14602 obj_header.frozen =
OBJ_FROZEN(obj) ? TRUE : FALSE;
14603 ibf_dump_object_object_header(dump, obj_header);
14604 (*dump_object_functions[obj_header.type])(dump, obj);
14607 return current_offset;
14611static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
14612 ibf_load_object_unsupported,
14613 ibf_load_object_unsupported,
14614 ibf_load_object_class,
14615 ibf_load_object_unsupported,
14616 ibf_load_object_float,
14617 ibf_load_object_string,
14618 ibf_load_object_regexp,
14619 ibf_load_object_array,
14620 ibf_load_object_hash,
14621 ibf_load_object_struct,
14622 ibf_load_object_bignum,
14623 ibf_load_object_unsupported,
14624 ibf_load_object_data,
14625 ibf_load_object_unsupported,
14626 ibf_load_object_complex_rational,
14627 ibf_load_object_complex_rational,
14628 ibf_load_object_unsupported,
14629 ibf_load_object_unsupported,
14630 ibf_load_object_unsupported,
14631 ibf_load_object_unsupported,
14632 ibf_load_object_symbol,
14633 ibf_load_object_unsupported,
14634 ibf_load_object_unsupported,
14635 ibf_load_object_unsupported,
14636 ibf_load_object_unsupported,
14637 ibf_load_object_unsupported,
14638 ibf_load_object_unsupported,
14639 ibf_load_object_unsupported,
14640 ibf_load_object_unsupported,
14641 ibf_load_object_unsupported,
14642 ibf_load_object_unsupported,
14643 ibf_load_object_unsupported,
14647ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
14649 if (object_index == 0) {
14653 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
14655 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
14656 ibf_offset_t offset = offsets[object_index];
14657 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
14660 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
14661 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
14662 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
14663 header.type, header.special_const, header.frozen, header.internal);
14665 if (offset >= load->current_buffer->size) {
14666 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
14669 if (header.special_const) {
14670 ibf_offset_t reading_pos = offset;
14672 obj = ibf_load_small_value(load, &reading_pos);
14675 obj = (*load_object_functions[header.type])(load, &header, offset);
14678 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
14681 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
14682 object_index, obj);
14695ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14700 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
14703 return ST_CONTINUE;
14707ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
14709 st_table *obj_table = dump->current_buffer->obj_table;
14714 args.offset_list = offset_list;
14716 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
14718 IBF_W_ALIGN(ibf_offset_t);
14719 *obj_list_offset = ibf_dump_pos(dump);
14721 st_index_t size = obj_table->num_entries;
14724 for (i=0; i<size; i++) {
14729 *obj_list_size = (
unsigned int)size;
14733ibf_dump_mark(
void *ptr)
14736 rb_gc_mark(dump->global_buffer.str);
14738 rb_mark_set(dump->global_buffer.obj_table);
14739 rb_mark_set(dump->iseq_table);
14743ibf_dump_free(
void *ptr)
14746 if (dump->global_buffer.obj_table) {
14747 st_free_table(dump->global_buffer.obj_table);
14748 dump->global_buffer.obj_table = 0;
14750 if (dump->iseq_table) {
14751 st_free_table(dump->iseq_table);
14752 dump->iseq_table = 0;
14757ibf_dump_memsize(
const void *ptr)
14761 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
14762 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
14768 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
14769 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
14775 dump->global_buffer.obj_table = NULL;
14776 dump->iseq_table = NULL;
14779 dump->global_buffer.obj_table = ibf_dump_object_table_new();
14780 dump->iseq_table = st_init_numtable();
14782 dump->current_buffer = &dump->global_buffer;
14786rb_iseq_ibf_dump(
const rb_iseq_t *iseq,
VALUE opt)
14793 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
14794 ISEQ_BODY(iseq)->local_iseq != iseq) {
14797 if (
RTEST(ISEQ_COVERAGE(iseq))) {
14802 ibf_dump_setup(dump, dump_obj);
14804 ibf_dump_write(dump, &header,
sizeof(header));
14805 ibf_dump_iseq(dump, iseq);
14807 header.magic[0] =
'Y';
14808 header.magic[1] =
'A';
14809 header.magic[2] =
'R';
14810 header.magic[3] =
'B';
14811 header.major_version = IBF_MAJOR_VERSION;
14812 header.minor_version = IBF_MINOR_VERSION;
14813 header.endian = IBF_ENDIAN_MARK;
14815 ibf_dump_iseq_list(dump, &header);
14816 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
14817 header.size = ibf_dump_pos(dump);
14820 VALUE opt_str = opt;
14823 ibf_dump_write(dump, ptr, header.extra_size);
14826 header.extra_size = 0;
14829 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
14831 str = dump->global_buffer.str;
14836static const ibf_offset_t *
14837ibf_iseq_list(
const struct ibf_load *load)
14839 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
14843rb_ibf_load_iseq_complete(rb_iseq_t *iseq)
14846 rb_iseq_t *prev_src_iseq = load->iseq;
14847 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
14850 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
14851 iseq->aux.loader.index, offset,
14852 load->header->size);
14854 ibf_load_iseq_each(load, iseq, offset);
14855 ISEQ_COMPILE_DATA_CLEAR(iseq);
14857 rb_iseq_init_trace(iseq);
14858 load->iseq = prev_src_iseq;
14863rb_iseq_complete(
const rb_iseq_t *iseq)
14865 rb_ibf_load_iseq_complete((rb_iseq_t *)iseq);
14871ibf_load_iseq(
const struct ibf_load *load,
const rb_iseq_t *index_iseq)
14873 int iseq_index = (int)(
VALUE)index_iseq;
14876 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
14877 (
void *)index_iseq, (
void *)load->iseq_list);
14879 if (iseq_index == -1) {
14883 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
14886 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
14889 return (rb_iseq_t *)iseqv;
14892 rb_iseq_t *iseq = iseq_imemo_alloc();
14894 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
14897 iseq->aux.loader.obj = load->loader_obj;
14898 iseq->aux.loader.index = iseq_index;
14900 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
14901 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
14903 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
14905 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
14907 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
14909 rb_ibf_load_iseq_complete(iseq);
14913 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
14914 (
void *)iseq, (
void *)load->iseq);
14922ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
14925 load->loader_obj = loader_obj;
14926 load->global_buffer.buff = bytes;
14927 load->header = header;
14928 load->global_buffer.size = header->size;
14929 load->global_buffer.obj_list_offset = header->global_object_list_offset;
14930 load->global_buffer.obj_list_size = header->global_object_list_size;
14931 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
14932 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
14935 load->current_buffer = &load->global_buffer;
14937 if (size < header->size) {
14940 if (strncmp(header->magic,
"YARB", 4) != 0) {
14943 if (header->major_version != IBF_MAJOR_VERSION ||
14944 header->minor_version != IBF_MINOR_VERSION) {
14946 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
14948 if (header->endian != IBF_ENDIAN_MARK) {
14954 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14955 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
14956 header->iseq_list_offset);
14958 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
14959 rb_raise(rb_eArgError,
"unaligned object list offset: %u",
14960 load->global_buffer.obj_list_offset);
14973 if (USE_LAZY_LOAD) {
14974 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
14977 ibf_load_setup_bytes(load, loader_obj, RSTRING_PTR(str), RSTRING_LEN(str));
14982ibf_loader_mark(
void *ptr)
14985 rb_gc_mark(load->str);
14986 rb_gc_mark(load->iseq_list);
14987 rb_gc_mark(load->global_buffer.obj_list);
14991ibf_loader_free(
void *ptr)
14998ibf_loader_memsize(
const void *ptr)
15005 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
15006 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
15010rb_iseq_ibf_load(
VALUE str)
15016 ibf_load_setup(load, loader_obj, str);
15017 iseq = ibf_load_iseq(load, 0);
15024rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
15030 ibf_load_setup_bytes(load, loader_obj, bytes, size);
15031 iseq = ibf_load_iseq(load, 0);
15038rb_iseq_ibf_load_extra_data(
VALUE str)
15044 ibf_load_setup(load, loader_obj, str);
15045 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
15050#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.