12#include "ruby/internal/config.h"
22#include "internal/array.h"
23#include "internal/compile.h"
24#include "internal/coverage.h"
25#include "internal/complex.h"
26#include "internal/encoding.h"
27#include "internal/error.h"
28#include "internal/gc.h"
29#include "internal/hash.h"
30#include "internal/io.h"
31#include "internal/numeric.h"
32#include "internal/object.h"
33#include "internal/rational.h"
34#include "internal/re.h"
35#include "internal/ruby_parser.h"
36#include "internal/st.h"
37#include "internal/symbol.h"
38#include "internal/thread.h"
39#include "internal/variable.h"
45#include "vm_callinfo.h"
51#include "insns_info.inc"
53#define FIXNUM_INC(n, i) ((n)+(INT2FIX(i)&~FIXNUM_FLAG))
87 unsigned int rescued: 2;
88 unsigned int unremovable: 1;
93 enum ruby_vminsn_type insn_id;
100 rb_iseq_events_t events;
112 rb_iseq_events_t event;
123 const void *ensure_node;
128const ID rb_iseq_shared_exc_local_tbl[] = {idERROR_INFO};
148#define compile_debug CPDEBUG
150#define compile_debug ISEQ_COMPILE_DATA(iseq)->option->debug_level
155#define compile_debug_print_indent(level) \
156 ruby_debug_print_indent((level), compile_debug, gl_node_level * 2)
158#define debugp(header, value) (void) \
159 (compile_debug_print_indent(1) && \
160 ruby_debug_print_value(1, compile_debug, (header), (value)))
162#define debugi(header, id) (void) \
163 (compile_debug_print_indent(1) && \
164 ruby_debug_print_id(1, compile_debug, (header), (id)))
166#define debugp_param(header, value) (void) \
167 (compile_debug_print_indent(1) && \
168 ruby_debug_print_value(1, compile_debug, (header), (value)))
170#define debugp_verbose(header, value) (void) \
171 (compile_debug_print_indent(2) && \
172 ruby_debug_print_value(2, compile_debug, (header), (value)))
174#define debugp_verbose_node(header, value) (void) \
175 (compile_debug_print_indent(10) && \
176 ruby_debug_print_value(10, compile_debug, (header), (value)))
178#define debug_node_start(node) ((void) \
179 (compile_debug_print_indent(1) && \
180 (ruby_debug_print_node(1, CPDEBUG, "", (const NODE *)(node)), gl_node_level)), \
183#define debug_node_end() gl_node_level --
187#define debugi(header, id) ((void)0)
188#define debugp(header, value) ((void)0)
189#define debugp_verbose(header, value) ((void)0)
190#define debugp_verbose_node(header, value) ((void)0)
191#define debugp_param(header, value) ((void)0)
192#define debug_node_start(node) ((void)0)
193#define debug_node_end() ((void)0)
196#if CPDEBUG > 1 || CPDEBUG < 0
198#define printf ruby_debug_printf
199#define debugs if (compile_debug_print_indent(1)) ruby_debug_printf
200#define debug_compile(msg, v) ((void)(compile_debug_print_indent(1) && fputs((msg), stderr)), (v))
202#define debugs if(0)printf
203#define debug_compile(msg, v) (v)
206#define LVAR_ERRINFO (1)
209#define NEW_LABEL(l) new_label_body(iseq, (l))
210#define LABEL_FORMAT "<L%03d>"
212#define NEW_ISEQ(node, name, type, line_no) \
213 new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
215#define NEW_CHILD_ISEQ(node, name, type, line_no) \
216 new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
218#define NEW_CHILD_ISEQ_WITH_CALLBACK(callback_func, name, type, line_no) \
219 new_child_iseq_with_callback(iseq, (callback_func), (name), iseq, (type), (line_no))
222#define ADD_SEQ(seq1, seq2) \
223 APPEND_LIST((seq1), (seq2))
226#define ADD_INSN(seq, line_node, insn) \
227 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 0))
230#define ADD_SYNTHETIC_INSN(seq, line_no, node_id, insn) \
231 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line_no), (node_id), BIN(insn), 0))
234#define INSERT_BEFORE_INSN(next, line_no, node_id, insn) \
235 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
238#define INSERT_AFTER_INSN(prev, line_no, node_id, insn) \
239 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) new_insn_body(iseq, line_no, node_id, BIN(insn), 0))
242#define ADD_INSN1(seq, line_node, insn, op1) \
243 ADD_ELEM((seq), (LINK_ELEMENT *) \
244 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 1, (VALUE)(op1)))
247#define INSERT_BEFORE_INSN1(next, line_no, node_id, insn, op1) \
248 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) \
249 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
252#define INSERT_AFTER_INSN1(prev, line_no, node_id, insn, op1) \
253 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) \
254 new_insn_body(iseq, line_no, node_id, BIN(insn), 1, (VALUE)(op1)))
256#define LABEL_REF(label) ((label)->refcnt++)
259#define ADD_INSNL(seq, line_node, insn, label) (ADD_INSN1(seq, line_node, insn, label), LABEL_REF(label))
261#define ADD_INSN2(seq, line_node, insn, op1, op2) \
262 ADD_ELEM((seq), (LINK_ELEMENT *) \
263 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
265#define ADD_INSN3(seq, line_node, insn, op1, op2, op3) \
266 ADD_ELEM((seq), (LINK_ELEMENT *) \
267 new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
270#define ADD_SEND(seq, line_node, id, argc) \
271 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
273#define ADD_SEND_WITH_FLAG(seq, line_node, id, argc, flag) \
274 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)(flag), NULL)
276#define ADD_SEND_WITH_BLOCK(seq, line_node, id, argc, block) \
277 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
279#define ADD_CALL_RECEIVER(seq, line_node) \
280 ADD_INSN((seq), (line_node), putself)
282#define ADD_CALL(seq, line_node, id, argc) \
283 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
285#define ADD_CALL_WITH_BLOCK(seq, line_node, id, argc, block) \
286 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
288#define ADD_SEND_R(seq, line_node, id, argc, block, flag, keywords) \
289 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)))
291#define ADD_TRACE(seq, event) \
292 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), 0))
293#define ADD_TRACE_WITH_DATA(seq, event, data) \
294 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), (data)))
296static void iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
297static void iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
299#define ADD_GETLOCAL(seq, line_node, idx, level) iseq_add_getlocal(iseq, (seq), (line_node), (idx), (level))
300#define ADD_SETLOCAL(seq, line_node, idx, level) iseq_add_setlocal(iseq, (seq), (line_node), (idx), (level))
303#define ADD_LABEL(seq, label) \
304 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
306#define APPEND_LABEL(seq, before, label) \
307 APPEND_ELEM((seq), (before), (LINK_ELEMENT *) (label))
309#define ADD_ADJUST(seq, line_node, label) \
310 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), nd_line(line_node)))
312#define ADD_ADJUST_RESTORE(seq, label) \
313 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
315#define LABEL_UNREMOVABLE(label) \
316 ((label) ? (LABEL_REF(label), (label)->unremovable=1) : 0)
317#define ADD_CATCH_ENTRY(type, ls, le, iseqv, lc) do { \
318 VALUE _e = rb_ary_new3(5, (type), \
319 (VALUE)(ls) | 1, (VALUE)(le) | 1, \
320 (VALUE)(iseqv), (VALUE)(lc) | 1); \
321 LABEL_UNREMOVABLE(ls); \
324 if (NIL_P(ISEQ_COMPILE_DATA(iseq)->catch_table_ary)) \
325 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, rb_ary_hidden_new(3)); \
326 rb_ary_push(ISEQ_COMPILE_DATA(iseq)->catch_table_ary, freeze_hide_obj(_e)); \
330#define COMPILE(anchor, desc, node) \
331 (debug_compile("== " desc "\n", \
332 iseq_compile_each(iseq, (anchor), (node), 0)))
335#define COMPILE_POPPED(anchor, desc, node) \
336 (debug_compile("== " desc "\n", \
337 iseq_compile_each(iseq, (anchor), (node), 1)))
340#define COMPILE_(anchor, desc, node, popped) \
341 (debug_compile("== " desc "\n", \
342 iseq_compile_each(iseq, (anchor), (node), (popped))))
344#define COMPILE_RECV(anchor, desc, node, recv) \
345 (private_recv_p(node) ? \
346 (ADD_INSN(anchor, node, putself), VM_CALL_FCALL) : \
347 COMPILE(anchor, desc, recv) ? 0 : -1)
349#define OPERAND_AT(insn, idx) \
350 (((INSN*)(insn))->operands[(idx)])
352#define INSN_OF(insn) \
353 (((INSN*)(insn))->insn_id)
355#define IS_INSN(link) ((link)->type == ISEQ_ELEMENT_INSN)
356#define IS_LABEL(link) ((link)->type == ISEQ_ELEMENT_LABEL)
357#define IS_ADJUST(link) ((link)->type == ISEQ_ELEMENT_ADJUST)
358#define IS_TRACE(link) ((link)->type == ISEQ_ELEMENT_TRACE)
359#define IS_INSN_ID(iobj, insn) (INSN_OF(iobj) == BIN(insn))
360#define IS_NEXT_INSN_ID(link, insn) \
361 ((link)->next && IS_INSN((link)->next) && IS_INSN_ID((link)->next, insn))
364IS_INDEPENDENT_INSN(LINK_ELEMENT *link)
366 if (!IS_INSN(link)) {
370 enum ruby_vminsn_type
type = INSN_OF(link);
373 type == BIN(putobject) ||
374 type == BIN(putspecialobject) ||
375 type == BIN(putnil) ||
376 type == BIN(putself) ||
377 type == BIN(duphash) ||
378 type == BIN(getinstancevariable) ||
379 type == BIN(getlocal) ||
380 type == BIN(getlocal_WC_0) ||
381 type == BIN(getlocal_WC_1) ||
382 type == BIN(putobject_INT2FIX_0_) ||
383 type == BIN(putobject_INT2FIX_1_) ||
384 type == BIN(opt_getconstant_path)
394append_compile_error(const rb_iseq_t *iseq,
int line, const
char *fmt, ...)
396 VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
397 VALUE file = rb_iseq_path(iseq);
402 err = rb_syntax_error_append(err, file, line, -1, NULL, fmt, args);
404 if (
NIL_P(err_info)) {
405 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err);
408 else if (!err_info) {
419compile_bug(rb_iseq_t *iseq,
int line,
const char *fmt, ...)
423 rb_report_bug_valist(rb_iseq_path(iseq), line, fmt, args);
429#define COMPILE_ERROR append_compile_error
431#define ERROR_ARGS_AT(n) iseq, nd_line(n),
432#define ERROR_ARGS ERROR_ARGS_AT(node)
434#define EXPECT_NODE(prefix, node, ndtype, errval) \
436 const NODE *error_node = (node); \
437 enum node_type error_type = nd_type(error_node); \
438 if (error_type != (ndtype)) { \
439 COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
440 prefix ": " #ndtype " is expected, but %s", \
441 ruby_node_name(error_type)); \
446#define EXPECT_NODE_NONULL(prefix, parent, ndtype, errval) \
448 COMPILE_ERROR(ERROR_ARGS_AT(parent) \
449 prefix ": must be " #ndtype ", but 0"); \
453#define UNKNOWN_NODE(prefix, node, errval) \
455 const NODE *error_node = (node); \
456 COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
457 ruby_node_name(nd_type(error_node))); \
464#define CHECK(sub) if (!(sub)) {BEFORE_RETURN;return COMPILE_NG;}
465#define NO_CHECK(sub) (void)(sub)
468#define DECL_ANCHOR(name) \
469 LINK_ANCHOR name[1] = {{{ISEQ_ELEMENT_ANCHOR,},&name[0].anchor}}
470#define INIT_ANCHOR(name) \
471 ((name->last = &name->anchor)->next = NULL)
474freeze_hide_obj(
VALUE obj)
477 RBASIC_CLEAR_CLASS(obj);
481#include "optinsn.inc"
482#if OPT_INSTRUCTIONS_UNIFICATION
483#include "optunifs.inc"
488#define ISEQ_ARG iseq,
489#define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
492#define ISEQ_ARG_DECLARE
496#define gl_node_level ISEQ_COMPILE_DATA(iseq)->node_level
499static void dump_disasm_list_with_cursor(
const LINK_ELEMENT *link,
const LINK_ELEMENT *curr,
const LABEL *dest);
500static void dump_disasm_list(
const LINK_ELEMENT *elem);
502static int insn_data_length(INSN *iobj);
503static int calc_sp_depth(
int depth, INSN *iobj);
505static INSN *new_insn_body(rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...);
506static LABEL *new_label_body(rb_iseq_t *iseq,
long line);
507static ADJUST *new_adjust_body(rb_iseq_t *iseq, LABEL *label,
int line);
508static TRACE *new_trace_body(rb_iseq_t *iseq, rb_iseq_events_t event,
long data);
511static int iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *anchor,
const NODE *n,
int);
512static int iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
513static int iseq_setup_insn(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
514static int iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
515static int iseq_insns_unification(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
517static int iseq_set_local_table(rb_iseq_t *iseq,
const rb_ast_id_table_t *tbl,
const NODE *
const node_args);
518static int iseq_set_exception_local_table(rb_iseq_t *iseq);
519static int iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor,
const NODE *
const node);
521static int iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor);
522static int iseq_set_exception_table(rb_iseq_t *iseq);
523static int iseq_set_optargs_table(rb_iseq_t *iseq);
524static int iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq);
526static int compile_defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE needstr,
bool ignore);
527static int compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped);
534verify_list(ISEQ_ARG_DECLARE
const char *info, LINK_ANCHOR *
const anchor)
538 LINK_ELEMENT *list, *plist;
540 if (!compile_debug)
return;
542 list = anchor->anchor.next;
543 plist = &anchor->anchor;
545 if (plist != list->prev) {
552 if (anchor->last != plist && anchor->last != 0) {
557 rb_bug(
"list verify error: %08x (%s)", flag, info);
562#define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
566verify_call_cache(rb_iseq_t *iseq)
569 VALUE *original = rb_iseq_original_iseq(iseq);
571 while (i < ISEQ_BODY(iseq)->iseq_size) {
572 VALUE insn = original[i];
573 const char *types = insn_op_types(insn);
575 for (
int j=0; types[j]; j++) {
576 if (types[j] == TS_CALLDATA) {
580 if (cc != vm_cc_empty()) {
582 rb_bug(
"call cache is not initialized by vm_cc_empty()");
589 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
590 struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
593 if (cc != NULL && cc != vm_cc_empty()) {
595 rb_bug(
"call cache is not initialized by vm_cc_empty()");
605ADD_ELEM(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anchor, LINK_ELEMENT *elem)
607 elem->prev = anchor->last;
608 anchor->last->next = elem;
610 verify_list(
"add", anchor);
617APPEND_ELEM(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anchor, LINK_ELEMENT *before, LINK_ELEMENT *elem)
620 elem->next = before->next;
621 elem->next->prev = elem;
623 if (before == anchor->last) anchor->last = elem;
624 verify_list(
"add", anchor);
627#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
628#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
632branch_coverage_valid_p(rb_iseq_t *iseq,
int first_line)
634 if (!ISEQ_COVERAGE(iseq))
return 0;
635 if (!ISEQ_BRANCH_COVERAGE(iseq))
return 0;
636 if (first_line <= 0)
return 0;
641setup_branch(
const rb_code_location_t *loc,
const char *
type,
VALUE structure,
VALUE key)
643 const int first_lineno = loc->beg_pos.lineno, first_column = loc->beg_pos.column;
644 const int last_lineno = loc->end_pos.lineno, last_column = loc->end_pos.column;
647 rb_hash_aset(structure, key, branch);
657decl_branch_base(rb_iseq_t *iseq,
int node_id,
const rb_code_location_t *loc,
const char *
type)
659 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return Qundef;
672 uint64_t source_hash = ISEQ_BODY(iseq)->source_hash;
673 VALUE key = rb_ary_new_from_args(4,
674 ULONG2NUM((
unsigned long)(source_hash >> 32)),
675 ULONG2NUM((
unsigned long)(source_hash & 0xffffffff)),
681 VALUE branch_base = rb_hash_aref(structure, key);
684 if (
NIL_P(branch_base)) {
685 branch_base = setup_branch(loc,
type, structure, key);
698generate_dummy_line_node(
int lineno,
int node_id)
701 nd_set_line(&dummy, lineno);
702 nd_set_node_id(&dummy, node_id);
707add_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)
709 if (!branch_coverage_valid_p(iseq, loc->beg_pos.lineno))
return;
720 VALUE branch = rb_hash_aref(branches, key);
724 branch = setup_branch(loc,
type, branches, key);
734 ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
735 ADD_SYNTHETIC_INSN(seq, loc->end_pos.lineno, node_id, nop);
738#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
742iseq_synthetic_lineno(
const rb_iseq_t *iseq)
744 int lineno = ISEQ_LAST_LINE(iseq);
745 return lineno ? lineno : ISEQ_BODY(iseq)->location.first_lineno;
749validate_label(st_data_t name, st_data_t label, st_data_t arg)
751 rb_iseq_t *iseq = (rb_iseq_t *)arg;
752 LABEL *lobj = (LABEL *)label;
753 if (!lobj->link.next) {
755 COMPILE_ERROR(iseq, lobj->position,
756 "%"PRIsVALUE
": undefined label",
764validate_labels(rb_iseq_t *iseq, st_table *labels_table)
766 st_foreach(labels_table, validate_label, (st_data_t)iseq);
767 st_free_table(labels_table);
771get_nd_recv(
const NODE *node)
773 switch (nd_type(node)) {
775 return RNODE_CALL(node)->nd_recv;
777 return RNODE_OPCALL(node)->nd_recv;
781 return RNODE_QCALL(node)->nd_recv;
785 return RNODE_ATTRASGN(node)->nd_recv;
787 return RNODE_OP_ASGN1(node)->nd_recv;
789 return RNODE_OP_ASGN2(node)->nd_recv;
791 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
796get_node_call_nd_mid(
const NODE *node)
798 switch (nd_type(node)) {
800 return RNODE_CALL(node)->nd_mid;
802 return RNODE_OPCALL(node)->nd_mid;
804 return RNODE_FCALL(node)->nd_mid;
806 return RNODE_QCALL(node)->nd_mid;
808 return RNODE_VCALL(node)->nd_mid;
810 return RNODE_ATTRASGN(node)->nd_mid;
812 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
817get_nd_args(
const NODE *node)
819 switch (nd_type(node)) {
821 return RNODE_CALL(node)->nd_args;
823 return RNODE_OPCALL(node)->nd_args;
825 return RNODE_FCALL(node)->nd_args;
827 return RNODE_QCALL(node)->nd_args;
831 return RNODE_ATTRASGN(node)->nd_args;
833 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
838get_node_colon_nd_mid(
const NODE *node)
840 switch (nd_type(node)) {
842 return RNODE_COLON2(node)->nd_mid;
844 return RNODE_COLON3(node)->nd_mid;
846 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
851get_nd_vid(
const NODE *node)
853 switch (nd_type(node)) {
855 return RNODE_LASGN(node)->nd_vid;
857 return RNODE_DASGN(node)->nd_vid;
859 return RNODE_IASGN(node)->nd_vid;
861 return RNODE_CVASGN(node)->nd_vid;
863 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
868get_nd_value(
const NODE *node)
870 switch (nd_type(node)) {
872 return RNODE_LASGN(node)->nd_value;
874 return RNODE_DASGN(node)->nd_value;
876 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
881get_string_value(
const NODE *node)
883 switch (nd_type(node)) {
889 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
899 (*ifunc->func)(iseq, ret, ifunc->data);
901 ADD_SYNTHETIC_INSN(ret, iseq_synthetic_lineno(iseq), -1, leave);
903 CHECK(iseq_setup_insn(iseq, ret));
904 return iseq_setup(iseq, ret);
907static bool drop_unreachable_return(LINK_ANCHOR *ret);
910rb_iseq_compile_node(rb_iseq_t *iseq,
const NODE *node)
916 NO_CHECK(COMPILE(ret,
"nil", node));
917 iseq_set_local_table(iseq, 0, 0);
920 else if (nd_type_p(node, NODE_SCOPE)) {
922 iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl, (NODE *)RNODE_SCOPE(node)->nd_args);
923 iseq_set_arguments(iseq, ret, (NODE *)RNODE_SCOPE(node)->nd_args);
924 iseq_set_parameters_lvar_state(iseq);
926 switch (ISEQ_BODY(iseq)->
type) {
927 case ISEQ_TYPE_BLOCK:
929 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
930 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
932 start->rescued = LABEL_RESCUE_BEG;
933 end->rescued = LABEL_RESCUE_END;
936 ADD_SYNTHETIC_INSN(ret, ISEQ_BODY(iseq)->location.first_lineno, -1, nop);
937 ADD_LABEL(ret, start);
938 CHECK(COMPILE(ret,
"block body", RNODE_SCOPE(node)->nd_body));
941 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
944 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
945 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
948 case ISEQ_TYPE_CLASS:
951 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
953 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
956 case ISEQ_TYPE_METHOD:
958 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
960 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
961 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
963 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
967 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
974#define INVALID_ISEQ_TYPE(type) \
975 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
976 switch (ISEQ_BODY(iseq)->
type) {
977 case INVALID_ISEQ_TYPE(
METHOD);
978 case INVALID_ISEQ_TYPE(CLASS);
979 case INVALID_ISEQ_TYPE(BLOCK);
980 case INVALID_ISEQ_TYPE(EVAL);
981 case INVALID_ISEQ_TYPE(MAIN);
982 case INVALID_ISEQ_TYPE(TOP);
983#undef INVALID_ISEQ_TYPE
984 case ISEQ_TYPE_RESCUE:
985 iseq_set_exception_local_table(iseq);
986 CHECK(COMPILE(ret,
"rescue", node));
988 case ISEQ_TYPE_ENSURE:
989 iseq_set_exception_local_table(iseq);
990 CHECK(COMPILE_POPPED(ret,
"ensure", node));
992 case ISEQ_TYPE_PLAIN:
993 CHECK(COMPILE(ret,
"ensure", node));
996 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
999 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
1004 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
1005 NODE dummy_line_node = generate_dummy_line_node(ISEQ_BODY(iseq)->location.first_lineno, -1);
1006 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
1007 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
1009 else if (!drop_unreachable_return(ret)) {
1010 ADD_SYNTHETIC_INSN(ret, iseq_synthetic_lineno(iseq), -1, leave);
1014 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
1015 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
1016 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
1017 validate_labels(iseq, labels_table);
1020 CHECK(iseq_setup_insn(iseq, ret));
1021 return iseq_setup(iseq, ret);
1025rb_iseq_translate_threaded_code(rb_iseq_t *iseq)
1027#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1028 const void *
const *table = rb_vm_get_insns_address_table();
1030 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
1032 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1033 int insn = (int)vm_insn_unpack_insn(encoded[i]);
1034 int len = insn_len(insn);
1035 encoded[i] = vm_insn_repack_insn(encoded[i], (
VALUE)table[insn]);
1042 rb_yjit_live_iseq_count++;
1043 rb_yjit_iseq_alloc_count++;
1050rb_iseq_original_iseq(
const rb_iseq_t *iseq)
1055 if (original_code)
return original_code;
1056 original_code =
ALLOC_N(
VALUE, ISEQ_BODY(iseq)->iseq_size);
1057 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
1062 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1063 const void *addr = (
const void *)original_code[i];
1064 const int insn = rb_vm_insn_addr2insn(addr);
1066 original_code[i] = insn;
1067 i += insn_len(insn);
1073 v = rb_iseq_variable_ensure((rb_iseq_t *)iseq);
1074 VALUE *prev = ATOMIC_PTR_CAS(v->original_iseq,
1075 NULL, original_code);
1077 SIZED_FREE_N(original_code, ISEQ_BODY(iseq)->iseq_size);
1080 return original_code;
1087#if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
1088# define ALIGNMENT_SIZE SIZEOF_LONG_LONG
1090# define ALIGNMENT_SIZE SIZEOF_VALUE
1092#define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
1094#define ALIGNMENT_SIZE_OF(type) alignment_size_assert(RUBY_ALIGNOF(type), #type)
1097alignment_size_assert(
size_t align,
const char *
type)
1100 "ALIGNMENT_SIZE_OF(%s):%zd == (2 ** N) is expected",
type, align);
1106calc_padding(
void *ptr,
size_t align)
1111 mis = (size_t)ptr & (align - 1);
1113 padding = align - mis;
1124 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], align);
1127 if (storage->pos + size + padding > storage->size) {
1128 unsigned int alloc_size = storage->size;
1130 while (alloc_size < size + PADDING_SIZE_MAX) {
1134 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
1136 storage = *arena = storage->next;
1139 storage->size = alloc_size;
1140 padding = calc_padding((
void *)&storage->buff[storage->pos], align);
1143 storage->pos += (int)padding;
1145 ptr = (
void *)&storage->buff[storage->pos];
1146 storage->pos += (int)size;
1151compile_data_alloc(rb_iseq_t *iseq,
size_t size,
size_t align)
1154 return compile_data_alloc_with_arena(arena, size, align);
1157#define compile_data_alloc_type(iseq, type) \
1158 (type *)compile_data_alloc(iseq, sizeof(type), ALIGNMENT_SIZE_OF(type))
1161compile_data_alloc2(rb_iseq_t *iseq,
size_t elsize,
size_t num,
size_t align)
1164 return compile_data_alloc(iseq, size, align);
1167#define compile_data_alloc2_type(iseq, type, num) \
1168 (type *)compile_data_alloc2(iseq, sizeof(type), num, ALIGNMENT_SIZE_OF(type))
1171compile_data_calloc2(rb_iseq_t *iseq,
size_t elsize,
size_t num,
size_t align)
1174 void *p = compile_data_alloc(iseq, size, align);
1179#define compile_data_calloc2_type(iseq, type, num) \
1180 (type *)compile_data_calloc2(iseq, sizeof(type), num, ALIGNMENT_SIZE_OF(type))
1183compile_data_alloc_insn(rb_iseq_t *iseq)
1186 return (INSN *)compile_data_alloc_with_arena(arena,
sizeof(INSN), ALIGNMENT_SIZE_OF(INSN));
1190compile_data_alloc_label(rb_iseq_t *iseq)
1192 return compile_data_alloc_type(iseq, LABEL);
1196compile_data_alloc_adjust(rb_iseq_t *iseq)
1198 return compile_data_alloc_type(iseq, ADJUST);
1202compile_data_alloc_trace(rb_iseq_t *iseq)
1204 return compile_data_alloc_type(iseq, TRACE);
1211ELEM_INSERT_NEXT(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
1213 elem2->next = elem1->next;
1214 elem2->prev = elem1;
1215 elem1->next = elem2;
1217 elem2->next->prev = elem2;
1225ELEM_INSERT_PREV(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
1227 elem2->prev = elem1->prev;
1228 elem2->next = elem1;
1229 elem1->prev = elem2;
1231 elem2->prev->next = elem2;
1239ELEM_REPLACE(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
1241 elem2->prev = elem1->prev;
1242 elem2->next = elem1->next;
1244 elem1->prev->next = elem2;
1247 elem1->next->prev = elem2;
1252ELEM_REMOVE(LINK_ELEMENT *elem)
1254 elem->prev->next = elem->next;
1256 elem->next->prev = elem->prev;
1264ELEM_SWAP(LINK_ELEMENT *first, LINK_ELEMENT *second)
1269 first->prev->next = second;
1270 second->next->prev = first;
1272 first->next = second->next;
1273 second->next = first;
1275 second->prev = first->prev;
1276 first->prev = second;
1278 if (IS_INSN(first) && IS_INSN(second)) {
1279 INSN *first_insn = (INSN*)first;
1280 INSN *second_insn = (INSN*)second;
1284 if (first_insn->insn_info.line_no == second_insn->insn_info.line_no) {
1286 rb_iseq_events_t first_events = first_insn->insn_info.events & mask;
1287 rb_iseq_events_t second_events = second_insn->insn_info.events & mask;
1288 first_insn->insn_info.events = (first_insn->insn_info.events & ~mask) | second_events;
1289 second_insn->insn_info.events = (second_insn->insn_info.events & ~mask) | first_events;
1294static LINK_ELEMENT *
1295FIRST_ELEMENT(
const LINK_ANCHOR *
const anchor)
1297 return anchor->anchor.next;
1300static LINK_ELEMENT *
1301LAST_ELEMENT(LINK_ANCHOR *
const anchor)
1303 return anchor->last;
1306static LINK_ELEMENT *
1307ELEM_FIRST_INSN(LINK_ELEMENT *elem)
1310 switch (elem->type) {
1311 case ISEQ_ELEMENT_INSN:
1312 case ISEQ_ELEMENT_ADJUST:
1322LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1324 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1325 if (first_insn != NULL &&
1326 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1335LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1337 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1353APPEND_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anc1, LINK_ANCHOR *
const anc2)
1355 if (anc2->anchor.next) {
1358 anc1->last->next = anc2->anchor.next;
1359 anc2->anchor.next->prev = anc1->last;
1360 anc1->last = anc2->last;
1365 verify_list(
"append", anc1);
1368#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1373debug_list(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anchor, LINK_ELEMENT *cur)
1375 LINK_ELEMENT *list = FIRST_ELEMENT(anchor);
1377 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1378 (
void *)anchor->anchor.next, (
void *)anchor->last);
1380 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1381 (
void *)list->prev, (
int)list->type);
1386 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1387 verify_list(
"debug list", anchor);
1390#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1393#define debug_list(anc, cur) ((void)0)
1397new_trace_body(rb_iseq_t *iseq, rb_iseq_events_t event,
long data)
1399 TRACE *trace = compile_data_alloc_trace(iseq);
1401 trace->link.type = ISEQ_ELEMENT_TRACE;
1402 trace->link.next = NULL;
1403 trace->event = event;
1410new_label_body(rb_iseq_t *iseq,
long line)
1412 LABEL *labelobj = compile_data_alloc_label(iseq);
1414 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1415 labelobj->link.next = 0;
1417 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1418 labelobj->sc_state = 0;
1420 labelobj->refcnt = 0;
1422 labelobj->rescued = LABEL_RESCUE_NONE;
1423 labelobj->unremovable = 0;
1424 labelobj->position = -1;
1429new_adjust_body(rb_iseq_t *iseq, LABEL *label,
int line)
1431 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1432 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1433 adjust->link.next = 0;
1434 adjust->label = label;
1435 adjust->line_no = line;
1436 LABEL_UNREMOVABLE(label);
1441iseq_insn_each_markable_object(INSN *insn,
void (*func)(
VALUE *,
VALUE),
VALUE data)
1443 const char *types = insn_op_types(insn->insn_id);
1444 for (
int j = 0; types[j]; j++) {
1445 char type = types[j];
1451 func(&OPERAND_AT(insn, j), data);
1460iseq_insn_each_object_write_barrier(
VALUE * obj,
VALUE iseq)
1469new_insn_core(rb_iseq_t *iseq,
int line_no,
int node_id,
int insn_id,
int argc,
VALUE *argv)
1471 INSN *iobj = compile_data_alloc_insn(iseq);
1475 iobj->link.type = ISEQ_ELEMENT_INSN;
1476 iobj->link.next = 0;
1477 iobj->insn_id = insn_id;
1478 iobj->insn_info.line_no = line_no;
1479 iobj->insn_info.node_id = node_id;
1480 iobj->insn_info.events = 0;
1481 iobj->operands = argv;
1482 iobj->operand_size = argc;
1485 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1491new_insn_body(rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...)
1493 VALUE *operands = 0;
1497 va_start(argv, argc);
1498 operands = compile_data_alloc2_type(iseq,
VALUE, argc);
1499 for (i = 0; i < argc; i++) {
1505 return new_insn_core(iseq, line_no, node_id, insn_id, argc, operands);
1509insn_replace_with_operands(rb_iseq_t *iseq, INSN *iobj,
enum ruby_vminsn_type insn_id,
int argc, ...)
1511 VALUE *operands = 0;
1515 va_start(argv, argc);
1516 operands = compile_data_alloc2_type(iseq,
VALUE, argc);
1517 for (i = 0; i < argc; i++) {
1524 iobj->insn_id = insn_id;
1525 iobj->operand_size = argc;
1526 iobj->operands = operands;
1527 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1533new_callinfo(rb_iseq_t *iseq,
ID mid,
int argc,
unsigned int flag,
struct rb_callinfo_kwarg *kw_arg,
int has_blockiseq)
1535 VM_ASSERT(argc >= 0);
1538 flag |= VM_CALL_KWARG;
1539 argc += kw_arg->keyword_len;
1542 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING))
1543 && !has_blockiseq) {
1544 flag |= VM_CALL_ARGS_SIMPLE;
1547 ISEQ_BODY(iseq)->ci_size++;
1548 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1554new_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)
1556 VALUE *operands = compile_data_calloc2_type(iseq,
VALUE, 2);
1559 operands[1] = (
VALUE)blockiseq;
1566 if (vm_ci_flag((
struct rb_callinfo *)ci) & VM_CALL_FORWARDING) {
1567 insn = new_insn_core(iseq, line_no, node_id, BIN(sendforward), 2, operands);
1570 insn = new_insn_core(iseq, line_no, node_id, BIN(send), 2, operands);
1579new_child_iseq(rb_iseq_t *iseq,
const NODE *
const node,
1580 VALUE name,
const rb_iseq_t *parent,
enum rb_iseq_type
type,
int line_no)
1582 rb_iseq_t *ret_iseq;
1583 VALUE ast_value = rb_ruby_ast_new(node);
1587 if (ISEQ_BODY(iseq)->source_hash) {
1588 rb_ast_t *child_ast = rb_ruby_ast_data_get(ast_value);
1589 child_ast->body.source_hash = ISEQ_BODY(iseq)->source_hash;
1590 child_ast->body.has_source_hash = 1;
1593 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1594 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1595 ret_iseq = rb_iseq_new_with_opt(ast_value, name,
1596 ISEQ_BODY(iseq)->location.pathobj, rb_iseq_realpath(iseq),
1598 isolated_depth ? isolated_depth + 1 : 0,
1599 type, ISEQ_COMPILE_DATA(iseq)->option,
1600 ISEQ_SCRIPT_LINES(iseq));
1601 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1607 VALUE name,
const rb_iseq_t *parent,
enum rb_iseq_type
type,
int line_no)
1609 rb_iseq_t *ret_iseq;
1611 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1612 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1613 ISEQ_BODY(iseq)->location.pathobj, rb_iseq_realpath(iseq),
1614 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1615 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1620set_catch_except_p(rb_iseq_t *iseq)
1623 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1624 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1625 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1626 set_catch_except_p((rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1649 while (pos < body->iseq_size) {
1650 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1651 if (insn == BIN(
throw)) {
1652 set_catch_except_p(iseq);
1655 pos += insn_len(insn);
1661 for (i = 0; i < ct->size; i++) {
1663 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1664 if (entry->type != CATCH_TYPE_BREAK
1665 && entry->type != CATCH_TYPE_NEXT
1666 && entry->type != CATCH_TYPE_REDO) {
1668 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1675iseq_insert_nop_between_end_and_cont(rb_iseq_t *iseq)
1677 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1678 if (
NIL_P(catch_table_ary))
return;
1679 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1681 for (i = 0; i < tlen; i++) {
1683 LINK_ELEMENT *end = (LINK_ELEMENT *)(ptr[2] & ~1);
1684 LINK_ELEMENT *cont = (LINK_ELEMENT *)(ptr[4] & ~1);
1687 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1689 if (ct != CATCH_TYPE_BREAK
1690 && ct != CATCH_TYPE_NEXT
1691 && ct != CATCH_TYPE_REDO) {
1693 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1695 int lineno = ISEQ_BODY(iseq)->location.first_lineno;
1696 for (LINK_ELEMENT *prev = end; prev; prev = prev->prev) {
1697 if (IS_INSN(prev)) {
1698 lineno = ((INSN *)prev)->insn_info.line_no;
1702 INSN *nop = new_insn_core(iseq, lineno, -1, BIN(nop), 0, 0);
1703 ELEM_INSERT_NEXT(end, &nop->link);
1714iseq_setup_insn(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
1716 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1721 if (compile_debug > 5)
1722 dump_disasm_list(FIRST_ELEMENT(anchor));
1724 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1725 iseq_optimize(iseq, anchor);
1727 if (compile_debug > 5)
1728 dump_disasm_list(FIRST_ELEMENT(anchor));
1730 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1731 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1732 iseq_insns_unification(iseq, anchor);
1733 if (compile_debug > 5)
1734 dump_disasm_list(FIRST_ELEMENT(anchor));
1737 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1738 iseq_insert_nop_between_end_and_cont(iseq);
1739 if (compile_debug > 5)
1740 dump_disasm_list(FIRST_ELEMENT(anchor));
1746iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
1748 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1751 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1752 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1753 if (compile_debug > 5)
1754 dump_disasm_list(FIRST_ELEMENT(anchor));
1756 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1757 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1759 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1760 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1762 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1763 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1765 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1767 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1769 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1771 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1772 ruby_xfree_sized(ISEQ_BODY(iseq)->catch_table, iseq_catch_table_bytes(ISEQ_BODY(iseq)->catch_table->size));
1773 ISEQ_BODY(iseq)->catch_table = NULL;
1776 if (compile_debug > 1) {
1777 VALUE str = rb_iseq_disasm(iseq);
1780 verify_call_cache(iseq);
1781 debugs(
"[compile step: finish]\n");
1787iseq_set_exception_local_table(rb_iseq_t *iseq)
1789 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1790 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1791 ISEQ_BODY(iseq)->lvar_states.list = NULL;
1796get_lvar_level(
const rb_iseq_t *iseq)
1799 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1801 iseq = ISEQ_BODY(iseq)->parent_iseq;
1807get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1811 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1812 if (ISEQ_BODY(iseq)->local_table[i] ==
id) {
1820get_local_var_idx(
const rb_iseq_t *iseq,
ID id)
1822 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1825 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1826 "get_local_var_idx: %d", idx);
1833get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1835 int lv = 0, idx = -1;
1836 const rb_iseq_t *
const topmost_iseq = iseq;
1839 idx = get_dyna_var_idx_at_raw(iseq,
id);
1843 iseq = ISEQ_BODY(iseq)->parent_iseq;
1848 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1849 "get_dyna_var_idx: -1");
1853 *ls = ISEQ_BODY(iseq)->local_table_size;
1858iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1862 iseq = ISEQ_BODY(iseq)->parent_iseq;
1865 body = ISEQ_BODY(iseq);
1866 if (body->local_iseq == iseq &&
1867 body->param.flags.has_block &&
1868 body->local_table_size - body->param.block_start == idx) {
1877iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1880 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1881 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1892access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1894 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1896 if (isolated_depth && level >= isolated_depth) {
1897 if (
id == rb_intern(
"yield")) {
1898 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1901 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable '%s' from isolated Proc", rb_id2name(
id));
1905 for (
int i=0; i<level; i++) {
1907 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1910 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(0);
1913 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1914 if (write && !val) {
1915 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1919 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1922 iseq = ISEQ_BODY(iseq)->parent_iseq;
1927iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1929 for (
int i=0; i<level; i++) {
1930 iseq = ISEQ_BODY(iseq)->parent_iseq;
1933 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1939update_lvar_state(
const rb_iseq_t *iseq,
int level,
int idx)
1941 for (
int i=0; i<level; i++) {
1942 iseq = ISEQ_BODY(iseq)->parent_iseq;
1945 uint8_t *states = iseq_lvar_states(ISEQ_BODY(iseq));
1946 int table_idx = ISEQ_BODY(iseq)->local_table_size - idx;
1947 switch (iseq_lvar_state_get(states, table_idx)) {
1948 case lvar_uninitialized:
1949 iseq_lvar_state_set(states, table_idx, lvar_initialized);
1951 case lvar_initialized:
1952 iseq_lvar_state_set(states, table_idx, lvar_reassigned);
1954 case lvar_reassigned:
1958 rb_bug(
"unreachable");
1963iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq)
1965 uint8_t *states = iseq_lvar_states(ISEQ_BODY(iseq));
1967 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
1968 iseq_lvar_state_set(states, i, lvar_initialized);
1971 int lead_num = ISEQ_BODY(iseq)->param.lead_num;
1972 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
1973 for (
int i=0; i<opt_num; i++) {
1974 iseq_lvar_state_set(states, lead_num + i, lvar_uninitialized);
1981iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level)
1983 if (iseq_local_block_param_p(iseq, idx, level)) {
1984 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1987 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1989 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1993iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level)
1995 if (iseq_local_block_param_p(iseq, idx, level)) {
1996 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1999 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
2001 update_lvar_state(iseq, level, idx);
2002 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
2008iseq_calc_param_size(rb_iseq_t *iseq)
2011 if (body->param.flags.has_opt ||
2012 body->param.flags.has_post ||
2013 body->param.flags.has_rest ||
2014 body->param.flags.has_block ||
2015 body->param.flags.has_kw ||
2016 body->param.flags.has_kwrest) {
2018 if (body->param.flags.has_block) {
2019 body->param.size = body->param.block_start + 1;
2021 else if (body->param.flags.has_kwrest) {
2022 body->param.size = body->param.keyword->rest_start + 1;
2024 else if (body->param.flags.has_kw) {
2025 body->param.size = body->param.keyword->bits_start + 1;
2027 else if (body->param.flags.has_post) {
2028 body->param.size = rb_iseq_param_post_start(body) + body->param.post_num;
2030 else if (body->param.flags.has_rest) {
2031 body->param.size = rb_iseq_param_rest_start(body) + 1;
2033 else if (body->param.flags.has_opt) {
2034 body->param.size = body->param.lead_num + body->param.opt_num;
2041 body->param.size = body->param.lead_num;
2046iseq_set_arguments_keywords(rb_iseq_t *iseq, LINK_ANCHOR *
const optargs,
2049 const rb_node_kw_arg_t *node = args->kw_args;
2051 struct rb_iseq_param_keyword *keyword;
2054 int kw = 0, rkw = 0, i;
2056 body->param.flags.has_kw = TRUE;
2057 body->param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2061 if (get_nd_value(node->nd_body) == NODE_SPECIAL_REQUIRED_KEYWORD) {
2064 node = node->nd_next;
2066 if (kw > VM_CALL_KW_LEN_MAX) {
2067 COMPILE_ERROR(ERROR_ARGS_AT(RNODE(args->kw_args))
"too many keyword parameters (%d, maximum is %d)",
2068 kw, (
int)VM_CALL_KW_LEN_MAX);
2071 keyword->bits_start = arg_size++;
2073 keyword->required_num = rkw;
2074 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
2076 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
2077 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2078 keyword->rest_start = arg_size++;
2079 body->param.flags.has_kwrest = TRUE;
2081 if (kw_id == idPow) body->param.flags.anon_kwrest = TRUE;
2084 node = args->kw_args;
2086 const NODE *val_node = get_nd_value(node->nd_body);
2089 if (val_node != NODE_SPECIAL_REQUIRED_KEYWORD) {
2090 switch (nd_type(val_node)) {
2092 dv = rb_node_sym_string_val(val_node);
2095 dv = rb_node_regx_string_val(val_node);
2098 dv = rb_node_line_lineno_val(val_node);
2101 dv = rb_node_integer_literal_val(val_node);
2104 dv = rb_node_float_literal_val(val_node);
2107 dv = rb_node_rational_literal_val(val_node);
2109 case NODE_IMAGINARY:
2110 dv = rb_node_imaginary_literal_val(val_node);
2113 dv = rb_node_encoding_val(val_node);
2125 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
2132 node = node->nd_next;
2138 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
2140 if (dv == complex_mark) dv =
Qundef;
2145 keyword->default_values = dvs;
2151iseq_set_use_block(rb_iseq_t *iseq)
2154 if (!body->param.flags.use_block) {
2155 body->param.flags.use_block = 1;
2157 rb_vm_t *vm = GET_VM();
2160 st_data_t key = (st_data_t)rb_intern_str(body->location.label);
2161 set_insert(&vm->unused_block_warning_table, key);
2167iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *
const optargs,
const NODE *
const node_args)
2169 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2173 const struct rb_args_info *
const args = &RNODE_ARGS(node_args)->nd_ainfo;
2179 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2181 body->param.lead_num = arg_size = (int)args->pre_args_num;
2182 if (body->param.lead_num > 0) body->param.flags.has_lead = TRUE;
2183 debugs(
" - argc: %d\n", body->param.lead_num);
2185 rest_id = args->rest_arg;
2186 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2190 block_id = args->block_arg;
2192 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2194 if (optimized_forward) {
2199 if (args->opt_args) {
2200 const rb_node_opt_arg_t *node = args->opt_args;
2207 label = NEW_LABEL(nd_line(RNODE(node)));
2209 ADD_LABEL(optargs, label);
2210 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2211 node = node->nd_next;
2216 label = NEW_LABEL(nd_line(node_args));
2218 ADD_LABEL(optargs, label);
2223 for (j = 0; j < i+1; j++) {
2228 body->param.flags.has_opt = TRUE;
2229 body->param.opt_num = i;
2230 body->param.opt_table = opt_table;
2235 RUBY_ASSERT(rb_iseq_param_rest_start(body) == arg_size);
2237 body->param.flags.has_rest = TRUE;
2238 if (rest_id ==
'*') body->param.flags.anon_rest = TRUE;
2241 if (args->first_post_arg) {
2242 RUBY_ASSERT(rb_iseq_param_post_start(body) == arg_size);
2243 body->param.post_num = args->post_args_num;
2244 body->param.flags.has_post = TRUE;
2245 arg_size += args->post_args_num;
2248 if (args->kw_args) {
2249 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2251 else if (args->kw_rest_arg && !optimized_forward) {
2252 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2253 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2254 keyword->rest_start = arg_size++;
2255 body->param.keyword = keyword;
2256 body->param.flags.has_kwrest = TRUE;
2258 static ID anon_kwrest = 0;
2259 if (!anon_kwrest) anon_kwrest = rb_intern(
"**");
2260 if (kw_id == anon_kwrest) body->param.flags.anon_kwrest = TRUE;
2262 else if (args->no_kwarg) {
2263 body->param.flags.accepts_no_kwarg = TRUE;
2266 if (args->no_blockarg) {
2267 body->param.flags.accepts_no_block = TRUE;
2269 else if (block_id) {
2270 body->param.block_start = arg_size++;
2271 body->param.flags.has_block = TRUE;
2272 iseq_set_use_block(iseq);
2276 if (optimized_forward) {
2277 body->param.flags.use_block = 1;
2278 body->param.flags.forwardable = TRUE;
2282 iseq_calc_param_size(iseq);
2283 body->param.size = arg_size;
2285 if (args->pre_init) {
2286 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2288 if (args->post_init) {
2289 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2292 if (body->type == ISEQ_TYPE_BLOCK) {
2293 if (body->param.flags.has_opt == FALSE &&
2294 body->param.flags.has_post == FALSE &&
2295 body->param.flags.has_rest == FALSE &&
2296 body->param.flags.has_kw == FALSE &&
2297 body->param.flags.has_kwrest == FALSE) {
2299 if (body->param.lead_num == 1 && last_comma == 0) {
2301 body->param.flags.ambiguous_param0 = TRUE;
2311iseq_set_local_table(rb_iseq_t *iseq,
const rb_ast_id_table_t *tbl,
const NODE *
const node_args)
2313 unsigned int size = tbl ? tbl->size : 0;
2314 unsigned int offset = 0;
2317 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2322 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2331 MEMCPY(ids, tbl->ids + offset,
ID, size);
2332 ISEQ_BODY(iseq)->local_table = ids;
2334 if (ISEQ_LVAR_STATES_EMBED_P(size)) {
2336 memset(ISEQ_BODY(iseq)->lvar_states.single, 0,
sizeof(ISEQ_BODY(iseq)->lvar_states.single));
2339 ISEQ_BODY(iseq)->lvar_states.list =
ZALLOC_N(uint8_t, ISEQ_LVAR_STATES_BUFLEN(size));
2342 ISEQ_BODY(iseq)->local_table_size = size;
2344 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2356 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2359 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2362 else if (tlit != tval) {
2372 long x =
FIX2LONG(rb_big_cmp(lit, val));
2380 return rb_float_cmp(lit, val);
2383 const struct RRational *rat1 = RRATIONAL(val);
2384 const struct RRational *rat2 = RRATIONAL(lit);
2385 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2388 const struct RComplex *comp1 = RCOMPLEX(val);
2389 const struct RComplex *comp2 = RCOMPLEX(lit);
2390 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2393 return rb_reg_equal(val, lit) ? 0 : -1;
2401rb_iseq_cdhash_hash(
VALUE a)
2403 switch (OBJ_BUILTIN_TYPE(a)) {
2406 return (st_index_t)a;
2414 return rb_rational_hash(a);
2416 return rb_complex_hash(a);
2424static const struct st_hash_type cdhash_type = {
2426 rb_iseq_cdhash_hash,
2430cdhash_new(
size_t size)
2432 VALUE cdhash = rb_imemo_cdhash_new(size, &cdhash_type);
2440 st_table *tbl = rb_imemo_cdhash_tbl(cdhash);
2441 st_insert(tbl, key, val);
2448 st_table *tbl = rb_imemo_cdhash_tbl(cdhash);
2450 if (!st_lookup(tbl, key, &dontcare)) {
2451 st_insert(tbl, key, val);
2463cdhash_set_label_check_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
2469cdhash_set_label_replace_i(st_data_t *key, st_data_t *value, st_data_t ptr,
int existing)
2472 LABEL *lobj = (LABEL *)(*value & ~1);
2473 *value = lobj->position - (data->pos+data->len);
2478get_ivar_ic_value(rb_iseq_t *iseq,
ID id)
2480 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2484get_cvar_ic_value(rb_iseq_t *iseq,
ID id)
2487 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2489 if (rb_id_table_lookup(tbl,
id,&val)) {
2494 tbl = rb_id_table_create(1);
2495 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2497 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2498 rb_id_table_insert(tbl,
id,val);
2502#define BADINSN_DUMP(anchor, list, dest) \
2503 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2505#define BADINSN_ERROR \
2506 (SIZED_FREE_N(generated_iseq, generated_iseq_size), \
2507 BADINSN_DUMP(anchor, list, NULL), \
2511fix_sp_depth(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
2513 int stack_max = 0, sp = 0, line = 0;
2516 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2517 if (IS_LABEL(list)) {
2518 LABEL *lobj = (LABEL *)list;
2523 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2524 switch (list->type) {
2525 case ISEQ_ELEMENT_INSN:
2530 INSN *iobj = (INSN *)list;
2533 sp = calc_sp_depth(sp, iobj);
2535 BADINSN_DUMP(anchor, list, NULL);
2536 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2537 "argument stack underflow (%d)", sp);
2540 if (sp > stack_max) {
2544 line = iobj->insn_info.line_no;
2546 operands = iobj->operands;
2547 insn = iobj->insn_id;
2548 types = insn_op_types(insn);
2549 len = insn_len(insn);
2552 if (iobj->operand_size !=
len - 1) {
2554 BADINSN_DUMP(anchor, list, NULL);
2555 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2556 "operand size miss! (%d for %d)",
2557 iobj->operand_size,
len - 1);
2561 for (j = 0; types[j]; j++) {
2562 if (types[j] == TS_OFFSET) {
2564 LABEL *lobj = (LABEL *)operands[j];
2566 BADINSN_DUMP(anchor, list, NULL);
2567 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2568 "unknown label: "LABEL_FORMAT, lobj->label_no);
2571 if (lobj->sp == -1) {
2574 else if (lobj->sp != sp) {
2575 VALUE path = rb_iseq_path(iseq);
2576 debugs(
"%.*s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2578 lobj->label_no, lobj->sp, sp);
2584 case ISEQ_ELEMENT_LABEL:
2586 LABEL *lobj = (LABEL *)list;
2587 if (lobj->sp == -1) {
2591 if (lobj->sp != sp) {
2592 VALUE path = rb_iseq_path(iseq);
2593 debugs(
"%.*s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2595 lobj->label_no, lobj->sp, sp);
2601 case ISEQ_ELEMENT_TRACE:
2606 case ISEQ_ELEMENT_ADJUST:
2608 ADJUST *adjust = (ADJUST *)list;
2611 sp = adjust->label ? adjust->label->sp : 0;
2612 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2613 BADINSN_DUMP(anchor, list, NULL);
2614 COMPILE_ERROR(iseq, adjust->line_no,
2615 "iseq_set_sequence: adjust bug %d < %d",
2622 BADINSN_DUMP(anchor, list, NULL);
2623 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2631add_node_id(uint8_t *node_ids,
unsigned int *node_ids_size,
int node_id,
int *prev_node_id)
2633 int delta = node_id - *prev_node_id;
2634 uint32_t zz = ((uint32_t)delta << 1) ^ (uint32_t)(delta >> 31);
2635 *prev_node_id = node_id;
2637 uint8_t
byte = zz & 0x7f;
2639 node_ids[(*node_ids_size)++] = zz ? (
byte | 0x80) : byte;
2645 int insns_info_index,
int code_index,
const INSN *iobj)
2647 if (insns_info_index == 0 ||
2648 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2649 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2650 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2651 insns_info[insns_info_index].events = iobj->insn_info.events;
2652 positions[insns_info_index] = code_index;
2660 int insns_info_index,
int code_index,
const ADJUST *adjust)
2662 insns_info[insns_info_index].line_no = adjust->line_no;
2663 insns_info[insns_info_index].events = 0;
2664 positions[insns_info_index] = code_index;
2669 rb_const_path_t path;
2674dedup_const_path_ics(
struct ic_site *sites,
unsigned int count)
2676 unsigned int distinct = 0;
2679 for (
unsigned int i = 0; i < count; i++) {
2681 for (j = 0; j < i; j++) {
2682 if (sites[i].path == sites[j].path) {
2688 sites[i].index = distinct++;
2691 sites[i].index = sites[j].index;
2697 st_init_existing_numtable_with_size(&table, count);
2699 for (
unsigned int i = 0; i < count; i++) {
2701 if (!st_lookup(&table, (st_data_t)sites[i].path, &index)) {
2703 st_insert(&table, (st_data_t)sites[i].path, index);
2705 sites[i].index = (
unsigned int)index;
2707 st_free_embedded_table(&table);
2717iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
2722 VALUE *generated_iseq;
2723 rb_iseq_events_t events = 0;
2726 int insn_num, code_index, insns_info_index, sp = 0;
2727 VALUE ic_site_buf = 0;
2728 unsigned int ic_site_count = 0, ic_site_cursor = 0;
2730 int stack_max = fix_sp_depth(iseq, anchor);
2732 if (stack_max < 0)
return COMPILE_NG;
2735 insn_num = code_index = 0;
2736 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2737 switch (list->type) {
2738 case ISEQ_ELEMENT_INSN:
2740 INSN *iobj = (INSN *)list;
2742 sp = calc_sp_depth(sp, iobj);
2744 if (iobj->insn_id == BIN(opt_getconstant_path)) {
2745 if (UNLIKELY(ic_site_count >= body->ic_size)) {
2746 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2747 "iseq_set_sequence: ic_index overflow: size: %d", body->ic_size);
2750 ic_sites[ic_site_count++].path = (rb_const_path_t)OPERAND_AT(iobj, 0);
2752 events = iobj->insn_info.events |= events;
2753 if (ISEQ_COVERAGE(iseq)) {
2754 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2755 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2756 int line = iobj->insn_info.line_no - 1;
2757 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2761 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2762 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2768 code_index += insn_data_length(iobj);
2773 case ISEQ_ELEMENT_LABEL:
2775 LABEL *lobj = (LABEL *)list;
2776 lobj->position = code_index;
2777 if (lobj->sp != sp) {
2778 VALUE path = rb_iseq_path(iseq);
2779 debugs(
"%.*s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2781 lobj->label_no, lobj->sp, sp);
2786 case ISEQ_ELEMENT_TRACE:
2788 TRACE *trace = (TRACE *)list;
2789 events |= trace->event;
2790 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2793 case ISEQ_ELEMENT_ADJUST:
2795 ADJUST *adjust = (ADJUST *)list;
2799 sp = adjust->label ? adjust->label->sp : 0;
2800 if (adjust->line_no != -1 && orig_sp > sp) {
2801 if (orig_sp - sp > 1) code_index++;
2811 body->ic_size = dedup_const_path_ics(ic_sites, ic_site_count);
2814 const int generated_iseq_size = code_index;
2817 VALUE insns_info_buf;
2820 VALUE positions_buf;
2821 unsigned int *positions =
ALLOCV_N(
unsigned int, positions_buf, insn_num);
2823 uint8_t *node_ids =
ALLOCV_N(uint8_t, node_ids_buf, (
size_t)insn_num * 5);
2824 unsigned int node_ids_size = 0;
2825 int prev_node_id = 0;
2826 if (ISEQ_IS_SIZE(body)) {
2827 body->is_entries =
ZALLOC_N(
char, ISEQ_IS_BYTESIZE(body));
2830 body->is_entries = NULL;
2833 if (body->ci_size) {
2837 body->call_data = NULL;
2839 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2846 iseq_bits_t * mark_offset_bits;
2847 int code_size = code_index;
2849 bool needs_bitmap =
false;
2851 const size_t mark_offset_bits_size = ISEQ_MBITS_BUFLEN(code_index);
2852 if (mark_offset_bits_size == 1) {
2853 mark_offset_bits = &ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2854 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
true;
2857 mark_offset_bits =
ZALLOC_N(iseq_bits_t, mark_offset_bits_size);
2858 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = mark_offset_bits;
2859 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
false;
2862 ISEQ_COMPILE_DATA(iseq)->iseq_encoded = (
void *)generated_iseq;
2863 ISEQ_COMPILE_DATA(iseq)->iseq_size = code_index;
2865 list = FIRST_ELEMENT(anchor);
2866 insns_info_index = code_index = sp = 0;
2869 switch (list->type) {
2870 case ISEQ_ELEMENT_INSN:
2875 INSN *iobj = (INSN *)list;
2878 sp = calc_sp_depth(sp, iobj);
2880 operands = iobj->operands;
2881 insn = iobj->insn_id;
2882 generated_iseq[code_index] = insn;
2883 types = insn_op_types(insn);
2884 len = insn_len(insn);
2886 for (j = 0; types[j]; j++) {
2887 char type = types[j];
2894 LABEL *lobj = (LABEL *)operands[j];
2895 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2900 VALUE map = operands[j];
2903 data.pos = code_index;
2905 st_foreach_with_replace(rb_imemo_cdhash_tbl(map), cdhash_set_label_check_i, cdhash_set_label_replace_i, (
VALUE)&data);
2907 generated_iseq[code_index + 1 + j] = map;
2908 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2910 needs_bitmap =
true;
2915 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2920 VALUE v = operands[j];
2921 generated_iseq[code_index + 1 + j] = v;
2925 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2926 needs_bitmap =
true;
2933 const struct ic_site *site = &ic_sites[ic_site_cursor++];
2934 IC ic = ISEQ_IS_ENTRY(body,
type, site->index);
2935 ic->const_path = site->path;
2937 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2942 unsigned int ic_index =
FIX2UINT(operands[j]);
2944 IVC cache = ISEQ_IS_ENTRY(body,
type, ic_index);
2946 if (insn == BIN(setinstancevariable)) {
2947 cache->value = IVAR_CACHE_INIT;
2950 cache->value = rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET);
2955 unsigned int ic_index =
FIX2UINT(operands[j]);
2956 void *entry = ISEQ_IS_ENTRY(body,
type, ic_index);
2957 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2958 BADINSN_DUMP(anchor, &iobj->link, 0);
2959 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2960 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2961 ic_index, ISEQ_IS_SIZE(body));
2963 generated_iseq[code_index + 1 + j] = (
VALUE)entry;
2970 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2971 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2973 cd->cc = vm_cc_empty();
2974 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2978 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2981 generated_iseq[code_index + 1 + j] = operands[j];
2984 generated_iseq[code_index + 1 + j] = operands[j];
2987 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2988 "unknown operand type: %c",
type);
2992 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2993 add_node_id(node_ids, &node_ids_size, iobj->insn_info.node_id, &prev_node_id);
2997 case ISEQ_ELEMENT_LABEL:
2999 LABEL *lobj = (LABEL *)list;
3000 if (lobj->sp != sp) {
3001 VALUE path = rb_iseq_path(iseq);
3002 debugs(
"%.*s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
3004 lobj->label_no, lobj->sp, sp);
3009 case ISEQ_ELEMENT_ADJUST:
3011 ADJUST *adjust = (ADJUST *)list;
3014 if (adjust->label) {
3015 sp = adjust->label->sp;
3021 if (adjust->line_no != -1) {
3022 const int diff = orig_sp - sp;
3024 if (insns_info_index == 0) {
3025 COMPILE_ERROR(iseq, adjust->line_no,
3026 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
3028 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
3029 add_node_id(node_ids, &node_ids_size, -1, &prev_node_id);
3032 generated_iseq[code_index++] = BIN(adjuststack);
3033 generated_iseq[code_index++] = orig_sp - sp;
3035 else if (diff == 1) {
3036 generated_iseq[code_index++] = BIN(pop);
3038 else if (diff < 0) {
3039 int label_no = adjust->label ? adjust->label->label_no : -1;
3040 SIZED_FREE_N(generated_iseq, generated_iseq_size);
3041 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
3042 SIZED_FREE_N(mark_offset_bits, ISEQ_MBITS_BUFLEN(code_index));
3044 debug_list(anchor, list);
3045 COMPILE_ERROR(iseq, adjust->line_no,
3046 "iseq_set_sequence: adjust bug to %d %d < %d",
3047 label_no, orig_sp, sp);
3060 body->iseq_encoded = (
void *)generated_iseq;
3061 body->iseq_size = code_index;
3062 body->stack_max = stack_max;
3064 if (ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit) {
3065 body->mark_bits.single = ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
3069 body->mark_bits.list = mark_offset_bits;
3072 body->mark_bits.list = NULL;
3073 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = NULL;
3074 SIZED_FREE_N(mark_offset_bits, mark_offset_bits_size);
3078 rb_iseq_insn_info_setup(body, insns_info, positions, insns_info_index, node_ids, node_ids_size);
3088label_get_position(LABEL *lobj)
3090 return lobj->position;
3094label_get_sp(LABEL *lobj)
3100iseq_set_exception_table(rb_iseq_t *iseq)
3102 const VALUE *tptr, *ptr;
3103 unsigned int tlen, i, table_size = 0;
3106 ISEQ_BODY(iseq)->catch_table = NULL;
3108 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
3109 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
3117 for (i = 0; i < table->size; i++) {
3119 int start = label_get_position((LABEL *)(ptr[1] & ~1));
3120 int end = label_get_position((LABEL *)(ptr[2] & ~1));
3123 if (start < 0 || end < 0) {
3124 if ((ptr[0] & 0xffff) != CATCH_TYPE_ENSURE)
continue;
3125 if (start < 0) start = 0;
3126 if (end < 0) end = 0;
3129 LABEL *lobj = (LABEL *)(ptr[4] & ~1);
3130 cont = label_get_position(lobj);
3131 if (cont < 0)
continue;
3132 sp = label_get_sp(lobj);
3135 entry = UNALIGNED_MEMBER_PTR(table, entries[table_size++]);
3136 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
3137 entry->start = (
unsigned int)start;
3138 entry->end = (
unsigned int)end;
3139 entry->iseq = (rb_iseq_t *)ptr[3];
3144 entry->cont = (
unsigned int)cont;
3148 if (entry->type == CATCH_TYPE_RESCUE ||
3149 entry->type == CATCH_TYPE_BREAK ||
3150 entry->type == CATCH_TYPE_NEXT) {
3159 table->size = table_size;
3160 if (table_size == 0) {
3161 ruby_xfree_sized(table, iseq_catch_table_bytes(tlen));
3164 else if (table_size < tlen) {
3165 table = ruby_xrealloc(table, iseq_catch_table_bytes(table_size));
3167 ISEQ_BODY(iseq)->catch_table = table;
3168 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
3186iseq_set_optargs_table(rb_iseq_t *iseq)
3189 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
3191 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
3192 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
3193 opt_table[i] = label_get_position((LABEL *)opt_table[i]);
3199static LINK_ELEMENT *
3200get_destination_insn(INSN *iobj)
3202 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, 0);
3204 rb_iseq_events_t events = 0;
3206 list = lobj->link.next;
3208 switch (list->type) {
3209 case ISEQ_ELEMENT_INSN:
3210 case ISEQ_ELEMENT_ADJUST:
3212 case ISEQ_ELEMENT_LABEL:
3215 case ISEQ_ELEMENT_TRACE:
3217 TRACE *trace = (TRACE *)list;
3218 events |= trace->event;
3226 if (list && IS_INSN(list)) {
3227 INSN *iobj = (INSN *)list;
3228 iobj->insn_info.events |= events;
3233static LINK_ELEMENT *
3234get_next_insn(INSN *iobj)
3236 LINK_ELEMENT *list = iobj->link.next;
3239 if (IS_INSN(list) || IS_ADJUST(list)) {
3247static LINK_ELEMENT *
3248get_prev_insn(INSN *iobj)
3250 LINK_ELEMENT *list = iobj->link.prev;
3253 if (IS_INSN(list) || IS_ADJUST(list)) {
3262unref_destination(INSN *iobj,
int pos)
3264 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, pos);
3266 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3270replace_destination(INSN *dobj, INSN *nobj)
3272 VALUE n = OPERAND_AT(nobj, 0);
3273 LABEL *dl = (LABEL *)OPERAND_AT(dobj, 0);
3274 LABEL *nl = (LABEL *)n;
3275 if (dl == nl)
return false;
3278 OPERAND_AT(dobj, 0) = n;
3279 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3284find_destination(INSN *i)
3286 int pos,
len = insn_len(i->insn_id);
3287 for (pos = 0; pos <
len; ++pos) {
3288 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3289 return (LABEL *)OPERAND_AT(i, pos);
3296 const unsigned int capacity;
3299 unsigned int *stamps;
3305 unsigned int no = (
unsigned int)lab->label_no;
3306 return unref->stamps[no] == unref->gen ? unref->counts[no] : 0;
3310unreachable_unref_increment(
const struct unreachable_unref *unref,
const LABEL *lab)
3312 unsigned int no = (
unsigned int)lab->label_no;
3313 if (unref->stamps[no] != unref->gen) {
3314 unref->stamps[no] = unref->gen;
3315 unref->counts[no] = 0;
3317 unref->counts[no]++;
3321remove_unreachable_chunk(rb_iseq_t *iseq,
struct unreachable_unref *unref, LINK_ELEMENT *i)
3323 LINK_ELEMENT *first = i, *end, *scan, *pending_end = 0;
3327 if (++unref->gen == 0) {
3329 MEMZERO(unref->stamps,
unsigned int, unref->capacity);
3337 if (IS_INSN(scan)) {
3338 if (IS_INSN_ID(scan, leave)) {
3343 else if ((lab = find_destination((INSN *)scan)) != 0) {
3344 unreachable_unref_increment(unref, lab);
3345 if (lab == pending && lab->refcnt <= unreachable_unref_count(unref, lab)) {
3350 else if (IS_LABEL(scan)) {
3351 lab = (LABEL *)scan;
3352 if (lab->unremovable) {
3356 if (lab->refcnt > unreachable_unref_count(unref, lab)) {
3359 pending_end = (scan == first) ? 0 : end;
3363 else if (IS_ADJUST(scan)) {
3367 if (!pending) end = scan;
3368 }
while ((scan = scan->next) != 0);
3371 if (!pending_end)
return 0;
3378 VALUE insn = INSN_OF(i);
3379 int pos,
len = insn_len(insn);
3380 for (pos = 0; pos <
len; ++pos) {
3381 switch (insn_op_types(insn)[pos]) {
3383 unref_destination((INSN *)i, pos);
3392 }
while ((i != end) && (i = i->next) != 0);
3397iseq_pop_newarray(rb_iseq_t *iseq, INSN *iobj)
3399 switch (OPERAND_AT(iobj, 0)) {
3401 ELEM_REMOVE(&iobj->link);
3404 ELEM_REMOVE(&iobj->link);
3407 iobj->insn_id = BIN(adjuststack);
3413is_frozen_putstring(INSN *insn,
VALUE *op)
3415 if (IS_INSN_ID(insn, dupstring) || IS_INSN_ID(insn, dupchilledstring)) {
3416 *op = OPERAND_AT(insn, 0);
3419 else if (IS_INSN_ID(insn, putobject)) {
3420 *op = OPERAND_AT(insn, 0);
3427insn_has_label_before(LINK_ELEMENT *elem)
3429 LINK_ELEMENT *prev = elem->prev;
3431 if (prev->type == ISEQ_ELEMENT_LABEL) {
3432 LABEL *label = (LABEL *)prev;
3433 if (label->refcnt > 0) {
3437 else if (prev->type == ISEQ_ELEMENT_INSN) {
3446optimize_checktype(rb_iseq_t *iseq, INSN *iobj)
3470 INSN *niobj, *ciobj, *dup = 0;
3474 switch (INSN_OF(iobj)) {
3475 case BIN(dupstring):
3476 case BIN(dupchilledstring):
3482 case BIN(putobject):
3485 default:
return FALSE;
3488 ciobj = (INSN *)get_next_insn(iobj);
3489 if (IS_INSN_ID(ciobj, jump)) {
3490 ciobj = (INSN *)get_next_insn((INSN*)OPERAND_AT(ciobj, 0));
3492 if (IS_INSN_ID(ciobj, dup)) {
3493 ciobj = (INSN *)get_next_insn(dup = ciobj);
3495 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3496 niobj = (INSN *)get_next_insn(ciobj);
3501 switch (INSN_OF(niobj)) {
3503 if (OPERAND_AT(ciobj, 0) ==
type) {
3504 dest = (LABEL *)OPERAND_AT(niobj, 0);
3507 case BIN(branchunless):
3508 if (OPERAND_AT(ciobj, 0) !=
type) {
3509 dest = (LABEL *)OPERAND_AT(niobj, 0);
3515 line = ciobj->insn_info.line_no;
3516 node_id = ciobj->insn_info.node_id;
3518 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3519 dest = (LABEL *)niobj->link.next;
3522 dest = NEW_LABEL(line);
3523 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3526 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3528 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3533ci_flag_set(
const rb_iseq_t *iseq,
const struct rb_callinfo *ci,
unsigned int add)
3535 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3536 vm_ci_flag(ci) | add,
3544ci_argc_set(
const rb_iseq_t *iseq,
const struct rb_callinfo *ci,
int argc)
3546 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3554#define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3557iseq_reg_compile(rb_iseq_t *iseq,
VALUE str,
int options,
const char *sourcefile,
int sourceline)
3560 VALUE re = rb_reg_compile(str, options, sourcefile, sourceline);
3563 rb_set_errinfo(errinfo);
3564 COMPILE_ERROR(iseq, sourceline,
"%" PRIsVALUE, message);
3573iseq_peephole_optimize(rb_iseq_t *iseq,
struct unreachable_unref *unref, LINK_ELEMENT *list,
const int do_tailcallopt)
3575 INSN *
const iobj = (INSN *)list;
3578 optimize_checktype(iseq, iobj);
3580 if (IS_INSN_ID(iobj, jump)) {
3581 INSN *niobj, *diobj, *piobj;
3582 diobj = (INSN *)get_destination_insn(iobj);
3583 niobj = (INSN *)get_next_insn(iobj);
3585 if (diobj == niobj) {
3592 unref_destination(iobj, 0);
3593 ELEM_REMOVE(&iobj->link);
3596 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3597 IS_INSN_ID(diobj, jump) &&
3598 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3599 diobj->insn_info.events == 0) {
3610 if (replace_destination(iobj, diobj)) {
3611 remove_unreachable_chunk(iseq, unref, iobj->link.next);
3615 else if (IS_INSN_ID(diobj, leave)) {
3628 unref_destination(iobj, 0);
3629 iobj->insn_id = BIN(leave);
3630 iobj->operand_size = 0;
3631 iobj->insn_info = diobj->insn_info;
3634 else if (IS_INSN(iobj->link.prev) &&
3635 (piobj = (INSN *)iobj->link.prev) &&
3636 (IS_INSN_ID(piobj, branchif) ||
3637 IS_INSN_ID(piobj, branchunless))) {
3638 INSN *pdiobj = (INSN *)get_destination_insn(piobj);
3639 if (niobj == pdiobj) {
3640 int refcnt = IS_LABEL(piobj->link.next) ?
3641 ((LABEL *)piobj->link.next)->refcnt : 0;
3656 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3657 ? BIN(branchunless) : BIN(branchif);
3658 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3659 ELEM_REMOVE(&iobj->link);
3666 else if (diobj == pdiobj) {
3680 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3681 ELEM_REPLACE(&piobj->link, &popiobj->link);
3684 if (remove_unreachable_chunk(iseq, unref, iobj->link.next)) {
3698 if (IS_INSN_ID(iobj, newrange)) {
3699 INSN *
const range = iobj;
3701 VALUE str_beg, str_end;
3703 if ((end = (INSN *)get_prev_insn(range)) != 0 &&
3704 is_frozen_putstring(end, &str_end) &&
3705 (beg = (INSN *)get_prev_insn(end)) != 0 &&
3706 is_frozen_putstring(beg, &str_beg) &&
3707 !(insn_has_label_before(&beg->link) || insn_has_label_before(&end->link))) {
3708 int excl =
FIX2INT(OPERAND_AT(range, 0));
3711 ELEM_REMOVE(&beg->link);
3712 ELEM_REMOVE(&end->link);
3713 range->insn_id = BIN(putobject);
3714 OPERAND_AT(range, 0) = lit_range;
3719 if (IS_INSN_ID(iobj, leave)) {
3720 remove_unreachable_chunk(iseq, unref, iobj->link.next);
3732 if (IS_INSN_ID(iobj, duparray)) {
3733 LINK_ELEMENT *next = iobj->link.next;
3734 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3735 iobj->insn_id = BIN(putobject);
3745 if (IS_INSN_ID(iobj, duparray)) {
3746 LINK_ELEMENT *next = iobj->link.next;
3747 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3749 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3751 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3752 VALUE ary = iobj->operands[0];
3755 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, ary, (
VALUE)ci);
3767 if (IS_INSN_ID(iobj, duphash)) {
3768 LINK_ELEMENT *next = iobj->link.next;
3769 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3771 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3773 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3774 VALUE hash = iobj->operands[0];
3778 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, hash, (
VALUE)ci);
3790 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3791 LINK_ELEMENT *next = iobj->link.next;
3792 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3794 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3796 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3797 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, rb_cArray_empty_frozen, (
VALUE)ci);
3809 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3810 LINK_ELEMENT *next = iobj->link.next;
3811 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3813 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3815 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3816 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, rb_cHash_empty_frozen, (
VALUE)ci);
3822 if (IS_INSN_ID(iobj, branchif) ||
3823 IS_INSN_ID(iobj, branchnil) ||
3824 IS_INSN_ID(iobj, branchunless)) {
3833 INSN *nobj = (INSN *)get_destination_insn(iobj);
3848 int stop_optimization =
3849 nobj->link.type == ISEQ_ELEMENT_INSN &&
3850 (nobj->insn_info.events & (
RUBY_EVENT_LINE | RUBY_EVENT_COVERAGE_LINE));
3851 if (!stop_optimization) {
3852 INSN *pobj = (INSN *)iobj->link.prev;
3855 if (!IS_INSN(&pobj->link))
3857 else if (IS_INSN_ID(pobj, dup))
3862 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3863 if (!replace_destination(iobj, nobj))
break;
3865 else if (prev_dup && IS_INSN(&nobj->link) && IS_INSN_ID(nobj, dup) &&
3866 !!(nobj = (INSN *)nobj->link.next) &&
3867 IS_INSN(&nobj->link) &&
3869 nobj->insn_id == iobj->insn_id) {
3885 if (!replace_destination(iobj, nobj))
break;
3913 if (prev_dup && IS_INSN(pobj->link.prev)) {
3914 pobj = (INSN *)pobj->link.prev;
3916 if (IS_INSN_ID(pobj, putobject)) {
3917 cond = (IS_INSN_ID(iobj, branchif) ?
3918 OPERAND_AT(pobj, 0) !=
Qfalse :
3919 IS_INSN_ID(iobj, branchunless) ?
3920 OPERAND_AT(pobj, 0) ==
Qfalse :
3923 else if (IS_INSN_ID(pobj, dupstring) ||
3924 IS_INSN_ID(pobj, duparray) ||
3925 IS_INSN_ID(pobj, newarray)) {
3926 cond = IS_INSN_ID(iobj, branchif);
3928 else if (IS_INSN_ID(pobj, putnil)) {
3929 cond = !IS_INSN_ID(iobj, branchif);
3932 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3933 ELEM_REMOVE(iobj->link.prev);
3935 else if (!iseq_pop_newarray(iseq, pobj)) {
3936 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3937 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3941 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3942 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3944 iobj->insn_id = BIN(jump);
3948 unref_destination(iobj, 0);
3949 ELEM_REMOVE(&iobj->link);
3955 LINK_ELEMENT *dest = get_destination_insn(nobj);
3956 if (!dest || !IS_INSN(dest))
break;
3957 nobj = (INSN *)dest;
3963 if (IS_INSN_ID(iobj, pop)) {
3970 LINK_ELEMENT *prev = iobj->link.prev;
3971 if (IS_INSN(prev)) {
3972 enum ruby_vminsn_type previ = ((INSN *)prev)->insn_id;
3973 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3974 previ == BIN(putself) || previ == BIN(dupstring) ||
3975 previ == BIN(dupchilledstring) ||
3976 previ == BIN(dup) ||
3977 previ == BIN(getlocal) ||
3978 previ == BIN(getblockparam) ||
3979 previ == BIN(getblockparamproxy) ||
3980 previ == BIN(getinstancevariable) ||
3981 previ == BIN(duparray)) {
3985 ELEM_REMOVE(&iobj->link);
3987 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (INSN*)prev)) {
3988 ELEM_REMOVE(&iobj->link);
3990 else if (previ == BIN(concatarray)) {
3991 INSN *piobj = (INSN *)prev;
3992 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3993 INSN_OF(piobj) = BIN(pop);
3995 else if (previ == BIN(concatstrings)) {
3996 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
4000 ELEM_REMOVE(&iobj->link);
4001 INSN_OF(prev) = BIN(adjuststack);
4007 if (IS_INSN_ID(iobj, newarray) ||
4008 IS_INSN_ID(iobj, duparray) ||
4009 IS_INSN_ID(iobj, concatarray) ||
4010 IS_INSN_ID(iobj, splatarray) ||
4019 LINK_ELEMENT *next = iobj->link.next;
4020 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
4026 if (IS_INSN_ID(iobj, newarray)) {
4027 LINK_ELEMENT *next = iobj->link.next;
4028 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
4029 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
4031 op1 = OPERAND_AT(iobj, 0);
4032 op2 = OPERAND_AT(next, 0);
4043 INSN_OF(iobj) = BIN(swap);
4044 iobj->operand_size = 0;
4053 INSN_OF(iobj) = BIN(opt_reverse);
4058 INSN_OF(iobj) = BIN(opt_reverse);
4059 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
4069 for (; diff > 0; diff--) {
4070 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
4081 for (; diff < 0; diff++) {
4082 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
4089 if (IS_INSN_ID(iobj, duparray)) {
4090 LINK_ELEMENT *next = iobj->link.next;
4098 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
4099 INSN_OF(iobj) = BIN(putobject);
4103 if (IS_INSN_ID(iobj, anytostring)) {
4104 LINK_ELEMENT *next = iobj->link.next;
4111 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
4112 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
4117 if (IS_INSN_ID(iobj, dupstring) || IS_INSN_ID(iobj, dupchilledstring) ||
4125 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
4126 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
4127 INSN *next = (INSN *)iobj->link.next;
4128 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
4129 ELEM_REMOVE(&next->link);
4131 ELEM_REMOVE(&iobj->link);
4133 if (IS_NEXT_INSN_ID(&iobj->link, toregexp)) {
4134 INSN *next = (INSN *)iobj->link.next;
4135 if (OPERAND_AT(next, 1) ==
INT2FIX(1)) {
4136 VALUE src = OPERAND_AT(iobj, 0);
4137 int opt = (int)
FIX2LONG(OPERAND_AT(next, 0));
4138 VALUE path = rb_iseq_path(iseq);
4139 int line = iobj->insn_info.line_no;
4140 VALUE re = iseq_reg_compile(iseq, src, opt, RSTRING_PTR(path), line);
4145 iobj->insn_id = BIN(putobject);
4147 ELEM_REMOVE(iobj->link.next);
4152 if (IS_INSN_ID(iobj, concatstrings)) {
4159 LINK_ELEMENT *next = iobj->link.next;
4161 if (IS_INSN(next) && IS_INSN_ID(next, jump))
4162 next = get_destination_insn(jump = (INSN *)next);
4163 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
4164 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
4165 OPERAND_AT(iobj, 0) =
INT2FIX(n);
4167 LABEL *label = ((LABEL *)OPERAND_AT(jump, 0));
4168 if (!--label->refcnt) {
4169 ELEM_REMOVE(&label->link);
4172 label = NEW_LABEL(0);
4173 OPERAND_AT(jump, 0) = (
VALUE)label;
4176 ELEM_INSERT_NEXT(next, &label->link);
4177 CHECK(iseq_peephole_optimize(iseq, unref, get_next_insn(jump), do_tailcallopt));
4185 if (do_tailcallopt &&
4186 (IS_INSN_ID(iobj, send) ||
4187 IS_INSN_ID(iobj, invokesuper))) {
4196 if (iobj->link.next) {
4197 LINK_ELEMENT *next = iobj->link.next;
4199 if (!IS_INSN(next)) {
4203 switch (INSN_OF(next)) {
4212 next = get_destination_insn((INSN *)next);
4226 if (IS_INSN_ID(piobj, send) ||
4227 IS_INSN_ID(piobj, invokesuper)) {
4228 if (OPERAND_AT(piobj, 1) == 0) {
4229 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4230 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4235 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4236 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4242 if (IS_INSN_ID(iobj, dup)) {
4243 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
4244 LINK_ELEMENT *set1 = iobj->link.next, *set2 = NULL;
4254 if (IS_NEXT_INSN_ID(set1, setlocal)) {
4256 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4257 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4259 ELEM_REMOVE(&iobj->link);
4272 else if (IS_NEXT_INSN_ID(set1, dup) &&
4273 IS_NEXT_INSN_ID(set1->next, setlocal)) {
4274 set2 = set1->next->next;
4275 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4276 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4277 ELEM_REMOVE(set1->next);
4291 if (IS_INSN_ID(iobj, getlocal)) {
4292 LINK_ELEMENT *niobj = &iobj->link;
4293 if (IS_NEXT_INSN_ID(niobj, dup)) {
4294 niobj = niobj->next;
4296 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
4297 LINK_ELEMENT *set1 = niobj->next;
4298 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
4299 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
4315 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
4316 if (IS_TRACE(iobj->link.next)) {
4317 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
4318 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
4320 if (iobj == (INSN *)list && bf->argc == 0 && (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF)) {
4321 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4334 if (IS_INSN_ID(iobj, getblockparam)) {
4335 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4336 iobj->insn_id = BIN(getblockparamproxy);
4340 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4341 LINK_ELEMENT *niobj = &iobj->link;
4342 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4343 niobj = niobj->next;
4344 LINK_ELEMENT *siobj;
4345 unsigned int set_flags = 0, unset_flags = 0;
4358 if (IS_NEXT_INSN_ID(niobj, send)) {
4359 siobj = niobj->next;
4360 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4361 unset_flags = VM_CALL_ARGS_BLOCKARG;
4376 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4377 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4378 siobj = niobj->next->next;
4379 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4384 unsigned int flags = vm_ci_flag(ci);
4385 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4386 ((INSN*)niobj)->insn_id = BIN(putobject);
4389 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4390 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4392 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4406 if (IS_NEXT_INSN_ID(&iobj->link, swap)) {
4407 LINK_ELEMENT *second = &iobj->link;
4408 LINK_ELEMENT *first = second->prev;
4409 LINK_ELEMENT *swap = second->next;
4411 if (IS_INDEPENDENT_INSN(first) && IS_INDEPENDENT_INSN(second)) {
4413 ELEM_SWAP(first, second);
4421insn_set_specialized_instruction(rb_iseq_t *iseq, INSN *iobj,
int insn_id)
4423 if (insn_id == BIN(opt_neq)) {
4424 VALUE original_ci = iobj->operands[0];
4425 VALUE new_ci = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4426 insn_replace_with_operands(iseq, iobj, insn_id, 2, new_ci, original_ci);
4429 iobj->insn_id = insn_id;
4430 iobj->operand_size = insn_len(insn_id) - 1;
4438iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
4440 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4441 IS_INSN(iobj->link.next)) {
4445 INSN *niobj = (INSN *)iobj->link.next;
4446 if (IS_INSN_ID(niobj, send)) {
4448 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4450 switch (vm_ci_mid(ci)) {
4452 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4455 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4458 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4463 VALUE num = iobj->operands[0];
4464 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, num, method);
4465 ELEM_REMOVE(&niobj->link);
4470 else if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4472 IS_NEXT_INSN_ID(&niobj->link, send)) {
4474 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4475 VALUE num = iobj->operands[0];
4476 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK));
4477 ELEM_REMOVE(&iobj->link);
4478 ELEM_REMOVE(niobj->link.next);
4479 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4485 else if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4487 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4488 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4491 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4492 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] == rb_id2sym(idBuffer))) {
4493 VALUE num = iobj->operands[0];
4494 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 2),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER));
4496 ELEM_REMOVE((niobj->link.next)->next);
4498 ELEM_REMOVE(&iobj->link);
4500 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4508 if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4509 IS_INSN_ID(niobj, putobject) ||
4510 IS_INSN_ID(niobj, putself) ||
4511 IS_INSN_ID(niobj, getlocal) ||
4512 IS_INSN_ID(niobj, getinstancevariable)) &&
4513 IS_NEXT_INSN_ID(&niobj->link, send)) {
4515 LINK_ELEMENT *sendobj = &(niobj->link);
4520 sendobj = sendobj->next;
4521 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4522 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4525 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4526 VALUE num = iobj->operands[0];
4527 INSN *sendins = (INSN *)sendobj;
4528 insn_replace_with_operands(iseq, sendins, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P));
4530 ELEM_REMOVE(&iobj->link);
4544 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4545 INSN *niobj = (INSN *)iobj->link.next;
4546 if ((IS_INSN_ID(niobj, getlocal) ||
4547 IS_INSN_ID(niobj, getinstancevariable) ||
4548 IS_INSN_ID(niobj, putself) ||
4549 IS_INSN_ID(niobj, putobject) ||
4550 IS_INSN_ID(niobj, putnil)) &&
4551 IS_NEXT_INSN_ID(&niobj->link, send)) {
4553 LINK_ELEMENT *sendobj = &(niobj->link);
4558 sendobj = sendobj->next;
4559 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4560 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4562 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4564 VALUE ary = iobj->operands[0];
4567 INSN *sendins = (INSN *)sendobj;
4568 insn_replace_with_operands(iseq, sendins, BIN(opt_duparray_send), 3, ary, rb_id2sym(idIncludeP),
INT2FIX(1));
4571 ELEM_REMOVE(&iobj->link);
4578 if (IS_INSN_ID(iobj, send)) {
4580 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(iobj, 1);
4582#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4583 if (vm_ci_simple(ci)) {
4584 switch (vm_ci_argc(ci)) {
4586 switch (vm_ci_mid(ci)) {
4587 case idLength: SP_INSN(length);
return COMPILE_OK;
4588 case idSize: SP_INSN(size);
return COMPILE_OK;
4589 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4590 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4591 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4592 case idNot: SP_INSN(not);
return COMPILE_OK;
4596 switch (vm_ci_mid(ci)) {
4597 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4598 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4599 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4600 case idDIV: SP_INSN(div);
return COMPILE_OK;
4601 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4602 case idEq: SP_INSN(eq);
return COMPILE_OK;
4603 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4604 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4605 case idLT: SP_INSN(lt);
return COMPILE_OK;
4606 case idLE: SP_INSN(le);
return COMPILE_OK;
4607 case idGT: SP_INSN(gt);
return COMPILE_OK;
4608 case idGE: SP_INSN(ge);
return COMPILE_OK;
4609 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4610 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4611 case idAnd: SP_INSN(and);
return COMPILE_OK;
4612 case idOr: SP_INSN(or);
return COMPILE_OK;
4616 switch (vm_ci_mid(ci)) {
4617 case idASET: SP_INSN(aset);
return COMPILE_OK;
4623 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4624 iobj->insn_id = BIN(opt_send_without_block);
4625 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4634tailcallable_p(rb_iseq_t *iseq)
4636 switch (ISEQ_BODY(iseq)->
type) {
4638 case ISEQ_TYPE_EVAL:
4639 case ISEQ_TYPE_MAIN:
4641 case ISEQ_TYPE_RESCUE:
4642 case ISEQ_TYPE_ENSURE:
4651iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
4654 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4655 const int do_tailcallopt = tailcallable_p(iseq) &&
4656 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4657 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4658 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4659 const int do_without_ints = ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_WITHOUT_INTERRUPTS;
4660 int rescue_level = 0;
4661 int tailcallopt = do_tailcallopt;
4663 list = FIRST_ELEMENT(anchor);
4665 int do_block_optimization = 0;
4666 LABEL * block_loop_label = NULL;
4668 int nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
4669 VALUE unref_counts_buf = 0, unref_stamps_buf = 0;
4671 .capacity = nlabels,
4673 .counts =
ALLOCV_N(
int, unref_counts_buf, nlabels),
4674 .stamps =
ALLOCV_N(
unsigned int, unref_stamps_buf, nlabels),
4680 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
4681 do_block_optimization = 1;
4685 LINK_ELEMENT * le = FIRST_ELEMENT(anchor)->next;
4686 if (IS_INSN(le) && IS_INSN_ID((INSN *)le, nop) && IS_LABEL(le->next)) {
4687 block_loop_label = (LABEL *)le->next;
4692 if (IS_INSN(list)) {
4693 if (do_peepholeopt) {
4697 iseq_specialized_instruction(iseq, (INSN *)list);
4700 insn_operands_unification((INSN *)list);
4703 if (do_without_ints) {
4704 INSN *item = (INSN *)list;
4705 if (IS_INSN_ID(item, jump)) {
4706 item->insn_id = BIN(jump_without_ints);
4708 else if (IS_INSN_ID(item, branchif)) {
4709 item->insn_id = BIN(branchif_without_ints);
4711 else if (IS_INSN_ID(item, branchunless)) {
4712 item->insn_id = BIN(branchunless_without_ints);
4714 else if (IS_INSN_ID(item, branchnil)) {
4715 item->insn_id = BIN(branchnil_without_ints);
4719 if (do_block_optimization) {
4720 INSN * item = (INSN *)list;
4722 if (IS_INSN_ID(item,
throw)) {
4723 do_block_optimization = 0;
4728 const char *types = insn_op_types(item->insn_id);
4729 for (
int j = 0; types[j]; j++) {
4730 if (types[j] == TS_OFFSET) {
4735 LABEL * target = (LABEL *)OPERAND_AT(item, j);
4736 if (target == block_loop_label) {
4737 do_block_optimization = 0;
4744 if (IS_LABEL(list)) {
4745 switch (((LABEL *)list)->rescued) {
4746 case LABEL_RESCUE_BEG:
4748 tailcallopt = FALSE;
4750 case LABEL_RESCUE_END:
4751 if (!--rescue_level) tailcallopt = do_tailcallopt;
4758 if (do_block_optimization) {
4759 LINK_ELEMENT * le = FIRST_ELEMENT(anchor)->next;
4760 if (IS_INSN(le) && IS_INSN_ID((INSN *)le, nop)) {
4770#if OPT_INSTRUCTIONS_UNIFICATION
4772new_unified_insn(rb_iseq_t *iseq,
4773 int insn_id,
int size, LINK_ELEMENT *seq_list)
4776 LINK_ELEMENT *list = seq_list;
4778 VALUE *operands = 0, *ptr = 0;
4782 for (i = 0; i < size; i++) {
4783 iobj = (INSN *)list;
4784 argc += iobj->operand_size;
4789 ptr = operands = compile_data_alloc2_type(iseq,
VALUE, argc);
4794 for (i = 0; i < size; i++) {
4795 iobj = (INSN *)list;
4796 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4797 ptr += iobj->operand_size;
4801 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4811iseq_insns_unification(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
4813#if OPT_INSTRUCTIONS_UNIFICATION
4819 list = FIRST_ELEMENT(anchor);
4821 if (IS_INSN(list)) {
4822 iobj = (INSN *)list;
4824 if (unified_insns_data[
id] != 0) {
4825 const int *
const *entry = unified_insns_data[id];
4826 for (j = 1; j < (intptr_t)entry[0]; j++) {
4827 const int *unified = entry[j];
4828 LINK_ELEMENT *li = list->next;
4829 for (k = 2; k < unified[1]; k++) {
4831 ((INSN *)li)->insn_id != unified[k]) {
4838 new_unified_insn(iseq, unified[0], unified[1] - 1,
4842 niobj->link.prev = (LINK_ELEMENT *)iobj->link.prev;
4843 niobj->link.next = li;
4845 li->prev = (LINK_ELEMENT *)niobj;
4848 list->prev->next = (LINK_ELEMENT *)niobj;
4849 list = (LINK_ELEMENT *)niobj;
4862all_string_result_p(
const NODE *node)
4864 if (!node)
return FALSE;
4865 switch (nd_type(node)) {
4866 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4868 case NODE_IF:
case NODE_UNLESS:
4869 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4870 if (all_string_result_p(RNODE_IF(node)->nd_body))
4871 return all_string_result_p(RNODE_IF(node)->nd_else);
4873 case NODE_AND:
case NODE_OR:
4874 if (!RNODE_AND(node)->nd_2nd)
4875 return all_string_result_p(RNODE_AND(node)->nd_1st);
4876 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4878 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4885 rb_iseq_t *
const iseq;
4886 LINK_ANCHOR *
const ret;
4888 const NODE *lit_node;
4894append_dstr_fragment(
struct dstr_ctxt *args,
const NODE *
const node, rb_parser_string_t *str)
4896 VALUE s = rb_str_new_mutable_parser_string(str);
4898 VALUE error = rb_reg_check_preprocess(s);
4899 if (!
NIL_P(error)) {
4900 COMPILE_ERROR(args->iseq, nd_line(node),
"%" PRIsVALUE, error);
4904 if (
NIL_P(args->lit)) {
4906 args->lit_node = node;
4915flush_dstr_fragment(
struct dstr_ctxt *args)
4917 if (!
NIL_P(args->lit)) {
4918 rb_iseq_t *iseq = args->iseq;
4919 VALUE lit = args->lit;
4921 lit = rb_fstring(lit);
4922 ADD_INSN1(args->ret, args->lit_node, putobject, lit);
4929compile_dstr_fragments_0(
struct dstr_ctxt *args,
const NODE *
const node)
4931 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4932 rb_parser_string_t *str = RNODE_DSTR(node)->string;
4935 CHECK(append_dstr_fragment(args, node, str));
4939 const NODE *
const head = list->nd_head;
4940 if (nd_type_p(head, NODE_STR)) {
4941 CHECK(append_dstr_fragment(args, node, RNODE_STR(head)->
string));
4943 else if (nd_type_p(head, NODE_DSTR)) {
4944 CHECK(compile_dstr_fragments_0(args, head));
4947 flush_dstr_fragment(args);
4948 rb_iseq_t *iseq = args->iseq;
4949 CHECK(COMPILE(args->ret,
"each string", head));
4958compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int *cntp,
int dregx)
4961 .iseq = iseq, .ret = ret,
4962 .lit =
Qnil, .lit_node = NULL,
4963 .cnt = 0, .dregx = dregx,
4965 CHECK(compile_dstr_fragments_0(&args, node));
4966 flush_dstr_fragment(&args);
4974compile_block(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int popped)
4976 while (node && nd_type_p(node, NODE_BLOCK)) {
4977 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4978 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4979 node = RNODE_BLOCK(node)->nd_next;
4982 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4988compile_file_path(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int frozen_string_literal)
4990 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4991 ADD_INSN1(ret, node, putobject,
INT2FIX(frozen_string_literal));
4992 ADD_SEND(ret, node, id_core_iseq_path,
INT2FIX(1));
4996compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node)
4999 if (!RNODE_DSTR(node)->nd_next) {
5000 VALUE lit = rb_node_dstr_string_val(node);
5001 ADD_INSN1(ret, node, dupstring, lit);
5006 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, FALSE));
5007 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
5013compile_dregx(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
5016 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
5018 if (!RNODE_DREGX(node)->nd_next) {
5020 VALUE src = rb_node_dregx_string_val(node);
5021 VALUE match = iseq_reg_compile(iseq, src, cflag, NULL, 0);
5022 ADD_INSN1(ret, node, putobject, match);
5028 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, TRUE));
5032 ADD_INSN(ret, node, pop);
5039compile_flip_flop(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int again,
5040 LABEL *then_label, LABEL *else_label)
5042 const int line = nd_line(node);
5043 LABEL *lend = NEW_LABEL(line);
5044 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
5045 + VM_SVAR_FLIPFLOP_START;
5048 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
5049 ADD_INSNL(ret, node, branchif, lend);
5052 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
5053 ADD_INSNL(ret, node, branchunless, else_label);
5054 ADD_INSN1(ret, node, putobject,
Qtrue);
5055 ADD_INSN1(ret, node, setspecial, key);
5057 ADD_INSNL(ret, node, jump, then_label);
5061 ADD_LABEL(ret, lend);
5062 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
5063 ADD_INSNL(ret, node, branchunless, then_label);
5064 ADD_INSN1(ret, node, putobject,
Qfalse);
5065 ADD_INSN1(ret, node, setspecial, key);
5066 ADD_INSNL(ret, node, jump, then_label);
5072compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *cond,
5073 LABEL *then_label, LABEL *else_label);
5075#define COMPILE_SINGLE 2
5077compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *cond,
5078 LABEL *then_label, LABEL *else_label)
5082 LABEL *label = NEW_LABEL(nd_line(cond));
5083 if (!then_label) then_label = label;
5084 else if (!else_label) else_label = label;
5086 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
5088 if (LIST_INSN_SIZE_ONE(seq)) {
5089 INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
5090 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
5093 if (!label->refcnt) {
5094 return COMPILE_SINGLE;
5096 ADD_LABEL(seq, label);
5102compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *cond,
5103 LABEL *then_label, LABEL *else_label)
5106 DECL_ANCHOR(ignore);
5109 switch (nd_type(cond)) {
5111 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
5112 cond = RNODE_AND(cond)->nd_2nd;
5113 if (ok == COMPILE_SINGLE) {
5114 ADD_INSNL(ret, cond, jump, else_label);
5115 INIT_ANCHOR(ignore);
5117 then_label = NEW_LABEL(nd_line(cond));
5121 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
5122 cond = RNODE_OR(cond)->nd_2nd;
5123 if (ok == COMPILE_SINGLE) {
5124 ADD_INSNL(ret, cond, jump, then_label);
5125 INIT_ANCHOR(ignore);
5127 else_label = NEW_LABEL(nd_line(cond));
5137 case NODE_IMAGINARY:
5144 ADD_INSNL(ret, cond, jump, then_label);
5149 ADD_INSNL(ret, cond, jump, else_label);
5155 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
5156 ADD_INSNL(ret, cond, jump, then_label);
5159 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
5162 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
5165 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
5169 DECL_ANCHOR(cond_seq);
5170 INIT_ANCHOR(cond_seq);
5172 CHECK(COMPILE(cond_seq,
"branch condition", cond));
5174 if (LIST_INSN_SIZE_ONE(cond_seq)) {
5175 INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
5176 if (insn->insn_id == BIN(putobject)) {
5177 if (
RTEST(insn->operands[0])) {
5178 ADD_INSNL(ret, cond, jump, then_label);
5183 ADD_INSNL(ret, cond, jump, else_label);
5188 ADD_SEQ(ret, cond_seq);
5193 ADD_INSNL(ret, cond, branchunless, else_label);
5194 ADD_INSNL(ret, cond, jump, then_label);
5201keyword_node_p(
const NODE *
const node)
5203 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
5207get_symbol_value(rb_iseq_t *iseq,
const NODE *node)
5209 switch (nd_type(node)) {
5211 return rb_node_sym_string_val(node);
5213 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
5218node_hash_unique_key_index(rb_iseq_t *iseq, rb_node_hash_t *node_hash,
int *count_ptr)
5220 NODE *node = node_hash->nd_head;
5224 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5225 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
5226 VALUE idx = rb_hash_aref(hash, key);
5231 rb_hash_aset(hash, key,
INT2FIX(i));
5240compile_keyword_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
5241 const NODE *
const root_node,
5248 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
5249 const NODE *node = RNODE_HASH(root_node)->nd_head;
5253 const NODE *key_node = RNODE_LIST(node)->nd_head;
5257 if (key_node && nd_type_p(key_node, NODE_SYM)) {
5262 *flag |= VM_CALL_KW_SPLAT;
5263 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5268 *flag |= VM_CALL_KW_SPLAT_MUT;
5273 node = RNODE_LIST(node)->nd_next;
5274 node = RNODE_LIST(node)->nd_next;
5278 node = RNODE_HASH(root_node)->nd_head;
5281 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
5283 if (
len > VM_CALL_KW_LEN_MAX) {
5284 COMPILE_ERROR(ERROR_ARGS_AT(root_node)
"too many keyword arguments (%d, maximum is %d)",
5285 len, (
int)VM_CALL_KW_LEN_MAX);
5290 VALUE *keywords = kw_arg->keywords;
5293 kw_arg->references = 0;
5294 kw_arg->keyword_len =
len;
5296 *kw_arg_ptr = kw_arg;
5298 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5299 const NODE *key_node = RNODE_LIST(node)->nd_head;
5300 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5303 keywords[j] = get_symbol_value(iseq, key_node);
5307 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
5317compile_args(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node, NODE **kwnode_ptr)
5321 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
5323 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
5326 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5327 *kwnode_ptr = RNODE_LIST(node)->nd_head;
5331 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
5339frozen_string_literal_p(
const rb_iseq_t *iseq)
5341 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
5345static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
5347 switch (nd_type(node)) {
5355 case NODE_IMAGINARY:
5361 return hash_key || frozen_string_literal_p(iseq);
5368static_literal_value(
const NODE *node, rb_iseq_t *iseq)
5370 switch (nd_type(node)) {
5373 VALUE lit = rb_node_integer_literal_val(node);
5379 VALUE lit = rb_node_float_literal_val(node);
5385 case NODE_IMAGINARY:
5394 return rb_node_sym_string_val(node);
5398 return rb_node_line_lineno_val(node);
5400 return rb_node_encoding_val(node);
5403 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
5404 VALUE lit = get_string_value(node);
5405 VALUE str = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
5410 return get_string_value(node);
5413 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
5418compile_array(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int popped,
bool first_chunk)
5420 const NODE *line_node = node;
5422 if (nd_type_p(node, NODE_ZLIST)) {
5424 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5429 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5432 for (; node; node = RNODE_LIST(node)->nd_next) {
5433 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5475 const int max_stack_len = 0x100;
5476 const int min_tmp_ary_len = 0x40;
5480#define FLUSH_CHUNK \
5482 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5483 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5484 first_chunk = FALSE; \
5492 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5494 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5495 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5498 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5503 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5504 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5510 ADD_INSN1(ret, line_node, duparray, ary);
5511 first_chunk = FALSE;
5514 ADD_INSN1(ret, line_node, putobject, ary);
5515 ADD_INSN(ret, line_node, concattoarray);
5523 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5525 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5528 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5530 if (stack_len == 0 && first_chunk) {
5531 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5536 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5537 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5541 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5546 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5556static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5558 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);
5562compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped)
5564 const NODE *line_node = node;
5566 node = RNODE_HASH(node)->nd_head;
5568 if (!node || nd_type_p(node, NODE_ZLIST)) {
5570 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5575 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5578 for (; node; node = RNODE_LIST(node)->nd_next) {
5579 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5602 const int max_stack_len = 0x100;
5603 const int min_tmp_hash_len = 0x800;
5605 int first_chunk = 1;
5606 DECL_ANCHOR(anchor);
5607 INIT_ANCHOR(anchor);
5610#define FLUSH_CHUNK() \
5612 if (first_chunk) { \
5613 APPEND_LIST(ret, anchor); \
5614 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5617 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5618 ADD_INSN(ret, line_node, swap); \
5619 APPEND_LIST(ret, anchor); \
5620 ADD_SEND(ret, line_node, id_core_hash_merge_bang_ptr, INT2FIX(stack_len + 1)); \
5622 INIT_ANCHOR(anchor); \
5623 first_chunk = stack_len = 0; \
5630 if (static_literal_node_pair_p(node, iseq)) {
5632 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5633 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5636 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5641 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5643 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5645 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5657 ADD_INSN1(ret, line_node, duphash, hash);
5661 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5662 ADD_INSN(ret, line_node, swap);
5664 ADD_INSN1(ret, line_node, putobject, hash);
5666 ADD_SEND(ret, line_node, id_core_hash_merge_bang_kwd,
INT2FIX(2));
5673 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5676 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5679 if (RNODE_LIST(node)->nd_head) {
5681 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5682 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5686 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5692 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5693 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5694 int first_kw = first_chunk && stack_len == 0;
5695 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5696 int only_kw = last_kw && first_kw;
5698 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5700 if (only_kw && method_call_keywords) {
5708 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5710 else if (first_kw) {
5714 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5721 if (only_kw && method_call_keywords) {
5727 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5734 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5735 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5736 else ADD_INSN(ret, line_node, swap);
5738 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5740 ADD_SEND(ret, line_node, id_core_hash_merge_bang_kwd,
INT2FIX(2));
5755rb_node_case_when_optimizable_literal(
const NODE *
const node)
5757 switch (nd_type(node)) {
5759 return rb_node_integer_literal_val(node);
5761 VALUE v = rb_node_float_literal_val(node);
5770 case NODE_IMAGINARY:
5779 return rb_node_sym_string_val(node);
5781 return rb_node_line_lineno_val(node);
5783 return rb_node_str_string_val(node);
5789when_vals(rb_iseq_t *iseq, LINK_ANCHOR *
const cond_seq,
const NODE *vals,
5790 LABEL *l1,
int only_special_literals,
VALUE literals)
5793 const NODE *val = RNODE_LIST(vals)->nd_head;
5794 VALUE lit = rb_node_case_when_optimizable_literal(val);
5797 only_special_literals = 0;
5800 cdhash_aset_if_missing(literals, lit, (
VALUE)(l1));
5803 if (nd_type_p(val, NODE_STR)) {
5804 debugp_param(
"nd_lit", get_string_value(val));
5805 lit = get_string_value(val);
5806 ADD_INSN1(cond_seq, val, putobject, lit);
5810 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5814 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5815 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5816 ADD_INSNL(cond_seq, val, branchif, l1);
5817 vals = RNODE_LIST(vals)->nd_next;
5819 return only_special_literals;
5823when_splat_vals(rb_iseq_t *iseq, LINK_ANCHOR *
const cond_seq,
const NODE *vals,
5824 LABEL *l1,
int only_special_literals,
VALUE literals)
5826 const NODE *line_node = vals;
5828 switch (nd_type(vals)) {
5830 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5834 ADD_INSN (cond_seq, line_node, dup);
5835 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5836 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5837 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5838 ADD_INSNL(cond_seq, line_node, branchif, l1);
5841 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5842 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5845 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5846 ADD_INSN (cond_seq, line_node, dup);
5847 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5848 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5849 ADD_INSNL(cond_seq, line_node, branchif, l1);
5852 ADD_INSN (cond_seq, line_node, dup);
5853 CHECK(COMPILE(cond_seq,
"when val", vals));
5854 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5855 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5856 ADD_INSNL(cond_seq, line_node, branchif, l1);
5949 const NODE *line_node;
5964add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc, INSN *before_insn)
5967 rb_bug(
"no masgn_state");
5976 memo->before_insn = before_insn;
5977 memo->line_node = line_node;
5978 memo->argn = state->num_args + 1;
5979 memo->num_args = argc;
5980 state->num_args += argc;
5981 memo->lhs_pos = lhs_pos;
5983 if (!state->first_memo) {
5984 state->first_memo = memo;
5987 state->last_memo->next = memo;
5989 state->last_memo = memo;
5994static 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);
5997compile_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)
5999 switch (nd_type(node)) {
6000 case NODE_ATTRASGN: {
6002 const NODE *line_node = node;
6004 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
6006 bool safenav_call =
false;
6007 LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
6008 iobj = (INSN *)get_prev_insn((INSN *)insn_element);
6010 ELEM_REMOVE(insn_element);
6011 if (!IS_INSN_ID(iobj, send)) {
6012 safenav_call =
true;
6013 iobj = (INSN *)get_prev_insn(iobj);
6014 ELEM_INSERT_NEXT(&iobj->link, insn_element);
6016 (pre->last = iobj->link.prev)->next = 0;
6019 int argc = vm_ci_argc(ci) + 1;
6020 ci = ci_argc_set(iseq, ci, argc);
6021 OPERAND_AT(iobj, 0) = (
VALUE)ci;
6025 ADD_INSN(lhs, line_node, swap);
6028 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
6031 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (INSN *)LAST_ELEMENT(lhs))) {
6035 iobj->link.prev = lhs->last;
6036 lhs->last->next = &iobj->link;
6037 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
6038 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
6039 int argc = vm_ci_argc(ci);
6040 bool dupsplat =
false;
6041 ci = ci_argc_set(iseq, ci, argc - 1);
6042 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
6049 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
6051 OPERAND_AT(iobj, 0) = (
VALUE)ci;
6060 int line_no = nd_line(line_node);
6061 int node_id = nd_node_id(line_node);
6064 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
6065 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
6066 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
6068 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
6070 if (!safenav_call) {
6071 ADD_INSN(lhs, line_node, pop);
6073 ADD_INSN(lhs, line_node, pop);
6076 for (
int i=0; i < argc; i++) {
6077 ADD_INSN(post, line_node, pop);
6082 DECL_ANCHOR(nest_rhs);
6083 INIT_ANCHOR(nest_rhs);
6084 DECL_ANCHOR(nest_lhs);
6085 INIT_ANCHOR(nest_lhs);
6087 int prev_level = state->lhs_level;
6088 bool prev_nested = state->nested;
6090 state->lhs_level = lhs_pos - 1;
6091 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
6092 state->lhs_level = prev_level;
6093 state->nested = prev_nested;
6095 ADD_SEQ(lhs, nest_rhs);
6096 ADD_SEQ(lhs, nest_lhs);
6100 if (!RNODE_CDECL(node)->nd_vid) {
6104 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
6106 LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
6107 iobj = (INSN *)insn_element;
6108 ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn((INSN *)get_prev_insn(iobj)));
6109 ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn(iobj));
6110 ELEM_REMOVE(insn_element);
6111 pre->last = iobj->link.prev;
6112 ADD_ELEM(lhs, (LINK_ELEMENT *)iobj);
6114 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (INSN *)LAST_ELEMENT(lhs))) {
6118 ADD_INSN(post, node, pop);
6123 DECL_ANCHOR(anchor);
6124 INIT_ANCHOR(anchor);
6125 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
6126 ELEM_REMOVE(FIRST_ELEMENT(anchor));
6127 ADD_SEQ(lhs, anchor);
6135compile_massign_opt_lhs(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *lhsn)
6138 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
6139 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
6145compile_massign_opt(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6146 const NODE *rhsn,
const NODE *orig_lhsn)
6149 const int memsize = numberof(mem);
6151 int llen = 0, rlen = 0;
6153 const NODE *lhsn = orig_lhsn;
6155#define MEMORY(v) { \
6157 if (memindex == memsize) return 0; \
6158 for (i=0; i<memindex; i++) { \
6159 if (mem[i] == (v)) return 0; \
6161 mem[memindex++] = (v); \
6164 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
6169 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
6170 switch (nd_type(ln)) {
6175 MEMORY(get_nd_vid(ln));
6180 lhsn = RNODE_LIST(lhsn)->nd_next;
6186 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
6189 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
6191 rhsn = RNODE_LIST(rhsn)->nd_next;
6196 for (i=0; i<llen-rlen; i++) {
6197 ADD_INSN(ret, orig_lhsn, putnil);
6201 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
6206compile_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)
6208 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
6209 const NODE *splatn = RNODE_MASGN(node)->nd_args;
6210 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
6211 const NODE *lhsn_count = lhsn;
6212 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
6217 while (lhsn_count) {
6219 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
6222 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
6224 lhsn = RNODE_LIST(lhsn)->nd_next;
6228 if (nd_type_p(splatn, NODE_POSTARG)) {
6230 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
6231 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
6232 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
6234 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
6236 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
6238 if (NODE_NAMED_REST_P(restn)) {
6239 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
6242 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
6244 postn = RNODE_LIST(postn)->nd_next;
6249 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
6253 if (!state->nested) {
6254 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
6258 ADD_INSN(rhs, node, dup);
6260 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
6265compile_massign(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
6267 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
6269 state.lhs_level = popped ? 0 : 1;
6272 state.first_memo = NULL;
6273 state.last_memo = NULL;
6283 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
6287 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
6288 for (
int i = 0; i < memo->num_args; i++) {
6289 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
6291 tmp_memo = memo->next;
6300 if (!popped && state.num_args >= 1) {
6302 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
6310constant_path_size(
const NODE *node,
bool *absolute)
6314 switch (nd_type(node)) {
6321 node = RNODE_COLON2(node)->nd_head;
6330static rb_const_path_t
6331compile_const_path(rb_iseq_t *iseq,
const NODE *node)
6333 bool absolute =
false;
6334 size_t size = constant_path_size(node, &absolute);
6342 for (
size_t i = size; i > 0; i--) {
6343 switch (nd_type(node)) {
6345 ids[i - 1] = RNODE_CONST(node)->nd_vid;
6348 ids[i - 1] = RNODE_COLON3(node)->nd_mid;
6351 ids[i - 1] = RNODE_COLON2(node)->nd_mid;
6352 node = RNODE_COLON2(node)->nd_head;
6356 rb_const_path_t const_path = rb_const_path_from_ids(absolute, ids, size);
6362compile_const_prefix(rb_iseq_t *iseq,
const NODE *
const node,
6363 LINK_ANCHOR *
const pref, LINK_ANCHOR *
const body)
6365 switch (nd_type(node)) {
6367 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
6368 ADD_INSN1(body, node, putobject,
Qtrue);
6369 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
6372 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
6373 ADD_INSN(body, node, pop);
6374 ADD_INSN1(body, node, putobject,
rb_cObject);
6375 ADD_INSN1(body, node, putobject,
Qtrue);
6376 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6379 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
6380 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
6381 ADD_INSN1(body, node, putobject,
Qfalse);
6382 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6385 CHECK(COMPILE(pref,
"const colon2 prefix", node));
6392cpath_const_p(
const NODE *node)
6394 switch (nd_type(node)) {
6399 if (RNODE_COLON2(node)->nd_head) {
6400 return cpath_const_p(RNODE_COLON2(node)->nd_head);
6409compile_cpath(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
const NODE *cpath)
6411 if (nd_type_p(cpath, NODE_COLON3)) {
6413 ADD_INSN1(ret, cpath, putobject,
rb_cObject);
6414 return VM_DEFINECLASS_FLAG_SCOPED;
6416 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
6418 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
6419 int flags = VM_DEFINECLASS_FLAG_SCOPED;
6420 if (!cpath_const_p(RNODE_COLON2(cpath)->nd_head)) {
6421 flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
6427 ADD_INSN1(ret, cpath, putspecialobject,
6428 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
6434private_recv_p(
const NODE *node)
6436 NODE *recv = get_nd_recv(node);
6437 if (recv && nd_type_p(recv, NODE_SELF)) {
6438 return RNODE_SELF(recv)->nd_state != 0;
6444defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6445 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
bool ignore);
6448compile_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);
6451defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6452 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
6455 enum defined_type expr_type = DEFINED_NOT_DEFINED;
6456 enum node_type
type;
6457 const int line = nd_line(node);
6458 const NODE *line_node = node;
6460 switch (
type = nd_type(node)) {
6464 expr_type = DEFINED_NIL;
6467 expr_type = DEFINED_SELF;
6470 expr_type = DEFINED_TRUE;
6473 expr_type = DEFINED_FALSE;
6478 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
6482 if (RNODE_LIST(vals)->nd_head) {
6483 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
6486 lfinish[1] = NEW_LABEL(line);
6488 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6490 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6503 case NODE_IMAGINARY:
6508 expr_type = DEFINED_EXPR;
6512 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6514 lfinish[1] = NEW_LABEL(line);
6516 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6517 expr_type = DEFINED_EXPR;
6523 expr_type = DEFINED_LVAR;
6526#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6528 ADD_INSN3(ret, line_node, definedivar,
6529 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6533 ADD_INSN(ret, line_node, putnil);
6534 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6535 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6539 ADD_INSN(ret, line_node, putnil);
6540 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6541 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6545 ADD_INSN(ret, line_node, putnil);
6546 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6547 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6551 lfinish[1] = NEW_LABEL(line);
6553 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6554 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6555 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6558 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6559 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6562 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6563 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6567 ADD_INSN1(ret, line_node, putobject,
rb_cObject);
6568 ADD_INSN3(ret, line_node, defined,
6569 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6577 case NODE_ATTRASGN:{
6578 const int explicit_receiver =
6579 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6580 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6582 if (get_nd_args(node) || explicit_receiver) {
6584 lfinish[1] = NEW_LABEL(line);
6587 lfinish[2] = NEW_LABEL(line);
6590 if (get_nd_args(node)) {
6591 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6592 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6594 if (explicit_receiver) {
6595 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6596 switch (nd_type(get_nd_recv(node))) {
6602 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6603 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6606 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6607 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6611 ADD_INSN(ret, line_node, dup);
6613 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6614 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6617 ADD_INSN(ret, line_node, putself);
6619 ADD_INSN(ret, line_node, dup);
6621 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6622 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6628 ADD_INSN(ret, line_node, putnil);
6629 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6630 PUSH_VAL(DEFINED_YIELD));
6631 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6636 ADD_INSN(ret, line_node, putnil);
6637 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6638 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6639 PUSH_VAL(DEFINED_GVAR));
6644 ADD_INSN(ret, line_node, putnil);
6645 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6646 PUSH_VAL(DEFINED_ZSUPER));
6652 case NODE_OP_ASGN_OR:
6653 case NODE_OP_ASGN_AND:
6662 expr_type = DEFINED_ASGN;
6669 VALUE str = rb_iseq_defined_string(expr_type);
6670 ADD_INSN1(ret, line_node, putobject, str);
6673 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6678build_defined_rescue_iseq(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const void *unused)
6680 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6681 iseq_set_exception_local_table(iseq);
6685defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6686 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
bool ignore)
6688 LINK_ELEMENT *lcur = ret->last;
6689 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6691 int line = nd_line(node);
6692 LABEL *lstart = NEW_LABEL(line);
6693 LABEL *lend = NEW_LABEL(line);
6694 const rb_iseq_t *rescue;
6696 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6697 rescue = NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc,
6699 ISEQ_BODY(iseq)->location.label),
6700 ISEQ_TYPE_RESCUE, 0);
6701 lstart->rescued = LABEL_RESCUE_BEG;
6702 lend->rescued = LABEL_RESCUE_END;
6703 APPEND_LABEL(ret, lcur, lstart);
6704 ADD_LABEL(ret, lend);
6706 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6712compile_defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE needstr,
bool ignore)
6714 const int line = nd_line(node);
6715 const NODE *line_node = node;
6716 if (!RNODE_DEFINED(node)->nd_head) {
6717 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6718 ADD_INSN1(ret, line_node, putobject, str);
6722 LINK_ELEMENT *last = ret->last;
6723 lfinish[0] = NEW_LABEL(line);
6726 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6728 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6729 ADD_INSN(ret, line_node, swap);
6731 ADD_LABEL(ret, lfinish[2]);
6733 ADD_INSN(ret, line_node, pop);
6734 ADD_LABEL(ret, lfinish[1]);
6736 ADD_LABEL(ret, lfinish[0]);
6742make_name_for_block(
const rb_iseq_t *orig_iseq)
6745 const rb_iseq_t *iseq = orig_iseq;
6747 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6748 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6749 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6752 iseq = ISEQ_BODY(iseq)->parent_iseq;
6757 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6760 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6765push_ensure_entry(rb_iseq_t *iseq,
6769 enl->ensure_node = node;
6770 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6772 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6776add_ensure_range(rb_iseq_t *iseq,
struct ensure_range *erange,
6777 LABEL *lstart, LABEL *lend)
6782 while (erange->next != 0) {
6783 erange = erange->next;
6787 ne->end = erange->end;
6788 erange->end = lstart;
6794has_ensure_iseq(
const rb_iseq_t *iseq)
6797 for (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack; e; e = e->prev) {
6798 if (e->ensure_node)
return true;
6804can_add_ensure_iseq(
const rb_iseq_t *iseq)
6806 return !ISEQ_COMPILE_DATA(iseq)->in_rescue || !has_ensure_iseq(iseq);
6810can_inline_return(
const rb_iseq_t *iseq,
enum rb_iseq_type
type)
6812 return type == ISEQ_TYPE_METHOD &&
6813 ((!ISEQ_COMPILE_DATA(iseq)->in_rescue && !ISEQ_COMPILE_DATA(iseq)->redo_label) || !has_ensure_iseq(iseq));
6817add_ensure_iseq(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
int is_return)
6822 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6824 DECL_ANCHOR(ensure);
6826 INIT_ANCHOR(ensure);
6828 if (enlp->erange != NULL) {
6829 DECL_ANCHOR(ensure_part);
6830 LABEL *lstart = NEW_LABEL(0);
6831 LABEL *lend = NEW_LABEL(0);
6832 INIT_ANCHOR(ensure_part);
6834 add_ensure_range(iseq, enlp->erange, lstart, lend);
6836 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6837 ADD_LABEL(ensure_part, lstart);
6838 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6839 ADD_LABEL(ensure_part, lend);
6840 ADD_SEQ(ensure, ensure_part);
6849 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6850 ADD_SEQ(ret, ensure);
6855check_keyword(
const NODE *node)
6859 if (nd_type_p(node, NODE_LIST)) {
6860 while (RNODE_LIST(node)->nd_next) {
6861 node = RNODE_LIST(node)->nd_next;
6863 node = RNODE_LIST(node)->nd_head;
6866 return keyword_node_p(node);
6871keyword_node_single_splat_p(NODE *kwnode)
6875 NODE *node = RNODE_HASH(kwnode)->nd_head;
6876 return RNODE_LIST(node)->nd_head == NULL &&
6877 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6881compile_single_keyword_splat_mutable(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6882 NODE *kwnode,
unsigned int *flag_ptr)
6884 if (flag_ptr) *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6885 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6886 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6887 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6888 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6891#define SPLATARRAY_FALSE 0
6892#define SPLATARRAY_TRUE 1
6893#define DUP_SINGLE_KW_SPLAT 2
6896setup_args_core(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6897 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6899 if (!argn)
return 0;
6901 NODE *kwnode = NULL;
6903 switch (nd_type(argn)) {
6906 int len = compile_args(iseq, args, argn, &kwnode);
6907 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6910 if (kwarg_ptr && compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6914 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6915 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6918 compile_hash(iseq, args, kwnode, kwarg_ptr != NULL, FALSE);
6927 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6928 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6929 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6930 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6931 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6934 case NODE_ARGSCAT: {
6935 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6936 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6937 bool args_pushed =
false;
6939 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6940 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6941 if (kwnode) rest_len--;
6942 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6946 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6947 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6950 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6951 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6952 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6955 else if (!args_pushed) {
6956 ADD_INSN(args, argn, concattoarray);
6961 compile_hash(iseq, args, kwnode, flag_ptr != NULL, FALSE);
6964 *flag_ptr |= VM_CALL_KW_SPLAT;
6969 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6975 case NODE_ARGSPUSH: {
6976 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6977 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6979 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6980 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6981 if (kwnode) rest_len--;
6982 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6983 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6986 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6987 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6990 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6991 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6998 if (flag_ptr) *flag_ptr |= VM_CALL_KW_SPLAT;
6999 if (!keyword_node_single_splat_p(kwnode)) {
7000 if (flag_ptr) *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
7001 compile_hash(iseq, args, kwnode, flag_ptr != NULL, FALSE);
7003 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
7004 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
7007 compile_hash(iseq, args, kwnode, flag_ptr != NULL, FALSE);
7013 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
7020 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
7026setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
7028 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
7029 *flag |= VM_CALL_ARGS_SPLAT_MUT;
7034setup_args_dup_rest_p(
const NODE *argn)
7036 switch(nd_type(argn)) {
7047 case NODE_IMAGINARY:
7060 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
7063 if (setup_args_dup_rest_p(RNODE_LIST(argn)->nd_head)) {
7066 argn = RNODE_LIST(argn)->nd_next;
7075setup_args(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
7079 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
7083 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
7084 RNODE_BLOCK_PASS(argn)->nd_head : argn;
7087 switch(nd_type(check_arg)) {
7090 dup_rest = SPLATARRAY_FALSE;
7094 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
7096 case(NODE_ARGSPUSH):
7098 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
7099 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
7100 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
7101 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
7102 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
7104 if (dup_rest == SPLATARRAY_FALSE) {
7106 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
7108 NODE *key_node = RNODE_LIST(node)->nd_head;
7109 if (key_node && setup_args_dup_rest_p(key_node)) {
7110 dup_rest = SPLATARRAY_TRUE;
7114 node = RNODE_LIST(node)->nd_next;
7115 NODE *value_node = RNODE_LIST(node)->nd_head;
7116 if (setup_args_dup_rest_p(value_node)) {
7117 dup_rest = SPLATARRAY_TRUE;
7121 node = RNODE_LIST(node)->nd_next;
7130 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
7132 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
7135 initial_dup_rest = dup_rest;
7137 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
7138 DECL_ANCHOR(arg_block);
7139 INIT_ANCHOR(arg_block);
7141 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
7142 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
7144 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
7145 const NODE * arg_node =
7146 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
7153 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
7154 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
7157 *flag |= VM_CALL_FORWARDING;
7159 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
7160 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
7164 *flag |= VM_CALL_ARGS_BLOCKARG;
7166 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
7169 if (LIST_INSN_SIZE_ONE(arg_block)) {
7170 LINK_ELEMENT *elem = FIRST_ELEMENT(arg_block);
7171 if (IS_INSN(elem)) {
7172 INSN *iobj = (INSN *)elem;
7173 if (iobj->insn_id == BIN(getblockparam)) {
7174 iobj->insn_id = BIN(getblockparamproxy);
7178 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
7179 ADD_SEQ(args, arg_block);
7182 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
7184 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
7189build_postexe_iseq(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const void *ptr)
7191 const NODE *body = ptr;
7192 int line = nd_line(body);
7194 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
7196 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7197 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
7199 iseq_set_local_table(iseq, 0, 0);
7203compile_named_capture_assign(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node)
7207 int line = nd_line(node);
7208 const NODE *line_node = node;
7209 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
7211#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
7212 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
7216 ADD_INSN(ret, line_node, dup);
7217 ADD_INSNL(ret, line_node, branchunless, fail_label);
7219 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
7221 if (RNODE_BLOCK(vars)->nd_next) {
7222 ADD_INSN(ret, line_node, dup);
7225 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
7227 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
7229 ELEM_INSERT_PREV(last->next, (LINK_ELEMENT *)cap);
7230#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
7231 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
7236 ADD_INSNL(nom, line_node, jump, end_label);
7237 ADD_LABEL(nom, fail_label);
7239 ADD_INSN(nom, line_node, pop);
7240 ADD_INSN(nom, line_node, putnil);
7242 ADD_LABEL(nom, end_label);
7243 (nom->last->next = cap->link.next)->prev = nom->last;
7244 (cap->link.next = nom->anchor.next)->prev = &cap->link;
7249 ADD_INSNL(ret, line_node, jump, end_label);
7250 ADD_LABEL(ret, fail_label);
7251 ADD_INSN(ret, line_node, pop);
7252 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
7254 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
7256 ((INSN*)last)->insn_id = BIN(putnil);
7257 ((INSN*)last)->operand_size = 0;
7259 ADD_LABEL(ret, end_label);
7263optimizable_range_item_p(
const NODE *n)
7265 if (!n)
return FALSE;
7266 switch (nd_type(n)) {
7279optimized_range_item(
const NODE *n)
7281 switch (nd_type(n)) {
7283 return rb_node_line_lineno_val(n);
7285 return rb_node_integer_literal_val(n);
7287 return rb_node_float_literal_val(n);
7289 return rb_node_rational_literal_val(n);
7290 case NODE_IMAGINARY:
7291 return rb_node_imaginary_literal_val(n);
7295 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
7300compile_if(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
7302 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
7303 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
7305 const int line = nd_line(node);
7306 const NODE *line_node = node;
7307 DECL_ANCHOR(cond_seq);
7308 LABEL *then_label, *else_label, *end_label;
7311 INIT_ANCHOR(cond_seq);
7312 then_label = NEW_LABEL(line);
7313 else_label = NEW_LABEL(line);
7316 NODE *cond = RNODE_IF(node)->nd_cond;
7317 if (nd_type(cond) == NODE_BLOCK) {
7318 cond = RNODE_BLOCK(cond)->nd_head;
7321 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
7322 ADD_SEQ(ret, cond_seq);
7324 if (then_label->refcnt && else_label->refcnt) {
7325 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
7328 if (then_label->refcnt) {
7329 ADD_LABEL(ret, then_label);
7331 DECL_ANCHOR(then_seq);
7332 INIT_ANCHOR(then_seq);
7333 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
7335 if (else_label->refcnt) {
7336 const NODE *
const coverage_node = node_body ? node_body : node;
7337 add_trace_branch_coverage(
7340 nd_code_loc(coverage_node),
7341 nd_node_id(coverage_node),
7343 type == NODE_IF ?
"then" :
"else",
7345 end_label = NEW_LABEL(line);
7346 ADD_INSNL(then_seq, line_node, jump, end_label);
7348 ADD_INSN(then_seq, line_node, pop);
7351 ADD_SEQ(ret, then_seq);
7354 if (else_label->refcnt) {
7355 ADD_LABEL(ret, else_label);
7357 DECL_ANCHOR(else_seq);
7358 INIT_ANCHOR(else_seq);
7359 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
7361 if (then_label->refcnt) {
7362 const NODE *
const coverage_node = node_else ? node_else : node;
7363 add_trace_branch_coverage(
7366 nd_code_loc(coverage_node),
7367 nd_node_id(coverage_node),
7369 type == NODE_IF ?
"else" :
"then",
7372 ADD_SEQ(ret, else_seq);
7376 ADD_LABEL(ret, end_label);
7383compile_case(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
7386 const NODE *node = orig_node;
7387 LABEL *endlabel, *elselabel;
7389 DECL_ANCHOR(body_seq);
7390 DECL_ANCHOR(cond_seq);
7391 int only_special_literals = 1;
7392 VALUE literals = cdhash_new(0);
7394 enum node_type
type;
7395 const NODE *line_node;
7400 INIT_ANCHOR(body_seq);
7401 INIT_ANCHOR(cond_seq);
7403 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
7405 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"case");
7407 node = RNODE_CASE(node)->nd_body;
7408 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
7409 type = nd_type(node);
7410 line = nd_line(node);
7413 endlabel = NEW_LABEL(line);
7414 elselabel = NEW_LABEL(line);
7418 while (
type == NODE_WHEN) {
7421 l1 = NEW_LABEL(line);
7422 ADD_LABEL(body_seq, l1);
7423 ADD_INSN(body_seq, line_node, pop);
7425 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7426 add_trace_branch_coverage(
7429 nd_code_loc(coverage_node),
7430 nd_node_id(coverage_node),
7435 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
7436 ADD_INSNL(body_seq, line_node, jump, endlabel);
7438 vals = RNODE_WHEN(node)->nd_head;
7440 switch (nd_type(vals)) {
7442 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
7443 if (only_special_literals < 0)
return COMPILE_NG;
7448 only_special_literals = 0;
7449 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
7452 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
7456 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
7459 node = RNODE_WHEN(node)->nd_next;
7463 type = nd_type(node);
7464 line = nd_line(node);
7469 ADD_LABEL(cond_seq, elselabel);
7470 ADD_INSN(cond_seq, line_node, pop);
7471 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
7472 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7473 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7476 debugs(
"== else (implicit)\n");
7477 ADD_LABEL(cond_seq, elselabel);
7478 ADD_INSN(cond_seq, orig_node, pop);
7479 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
7481 ADD_INSN(cond_seq, orig_node, putnil);
7483 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7486 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7487 ADD_INSN(ret, orig_node, dup);
7488 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
7490 LABEL_REF(elselabel);
7493 ADD_SEQ(ret, cond_seq);
7494 ADD_SEQ(ret, body_seq);
7495 ADD_LABEL(ret, endlabel);
7500compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
7504 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
7506 DECL_ANCHOR(body_seq);
7510 branches = decl_branch_base(iseq, nd_node_id(orig_node), nd_code_loc(orig_node),
"case");
7512 INIT_ANCHOR(body_seq);
7513 endlabel = NEW_LABEL(nd_line(node));
7515 while (node && nd_type_p(node, NODE_WHEN)) {
7516 const int line = nd_line(node);
7517 LABEL *l1 = NEW_LABEL(line);
7518 ADD_LABEL(body_seq, l1);
7520 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7521 add_trace_branch_coverage(
7524 nd_code_loc(coverage_node),
7525 nd_node_id(coverage_node),
7530 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7531 ADD_INSNL(body_seq, node, jump, endlabel);
7533 vals = RNODE_WHEN(node)->nd_head;
7535 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7537 switch (nd_type(vals)) {
7541 val = RNODE_LIST(vals)->nd_head;
7542 lnext = NEW_LABEL(nd_line(val));
7543 debug_compile(
"== when2\n", (
void)0);
7544 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7545 ADD_LABEL(ret, lnext);
7546 vals = RNODE_LIST(vals)->nd_next;
7552 ADD_INSN(ret, vals, putnil);
7553 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7554 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7555 ADD_INSNL(ret, vals, branchif, l1);
7558 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7560 node = RNODE_WHEN(node)->nd_next;
7563 const NODE *
const coverage_node = node ? node : orig_node;
7564 add_trace_branch_coverage(
7567 nd_code_loc(coverage_node),
7568 nd_node_id(coverage_node),
7572 CHECK(COMPILE_(ret,
"else", node, popped));
7573 ADD_INSNL(ret, orig_node, jump, endlabel);
7575 ADD_SEQ(ret, body_seq);
7576 ADD_LABEL(ret, endlabel);
7580static 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);
7582static 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);
7583static 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);
7584static int iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7585static 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);
7586static int iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7588#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7589#define CASE3_BI_OFFSET_ERROR_STRING 1
7590#define CASE3_BI_OFFSET_KEY_ERROR_P 2
7591#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7592#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7595iseq_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)
7597 const int line = nd_line(node);
7598 const NODE *line_node = node;
7600 switch (nd_type(node)) {
7654 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7655 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7656 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7658 const int min_argc = pre_args_num + post_args_num;
7659 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7660 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7662 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7664 match_failed = NEW_LABEL(line);
7665 type_error = NEW_LABEL(line);
7666 deconstruct = NEW_LABEL(line);
7667 deconstructed = NEW_LABEL(line);
7670 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7671 ADD_INSN(ret, line_node, swap);
7677 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7679 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7681 ADD_INSN(ret, line_node, dup);
7682 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7683 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7684 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7685 if (in_single_pattern) {
7686 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7687 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7688 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7689 INT2FIX(min_argc), base_index + 1 ));
7691 ADD_INSNL(ret, line_node, branchunless, match_failed);
7693 for (i = 0; i < pre_args_num; i++) {
7694 ADD_INSN(ret, line_node, dup);
7695 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7696 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7697 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7698 args = RNODE_LIST(args)->nd_next;
7701 if (RNODE_ARYPTN(node)->rest_arg) {
7702 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7703 ADD_INSN(ret, line_node, dup);
7704 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7705 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7706 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7707 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7708 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7709 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7710 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7712 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7715 if (post_args_num > 0) {
7716 ADD_INSN(ret, line_node, dup);
7717 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7718 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7719 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7720 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7721 ADD_INSN(ret, line_node, pop);
7726 args = RNODE_ARYPTN(node)->post_args;
7727 for (i = 0; i < post_args_num; i++) {
7728 ADD_INSN(ret, line_node, dup);
7730 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7731 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7732 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7734 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7735 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7736 args = RNODE_LIST(args)->nd_next;
7739 ADD_INSN(ret, line_node, pop);
7741 ADD_INSN(ret, line_node, pop);
7743 ADD_INSNL(ret, line_node, jump, matched);
7744 ADD_INSN(ret, line_node, putnil);
7746 ADD_INSN(ret, line_node, putnil);
7749 ADD_LABEL(ret, type_error);
7750 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7752 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7753 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7754 ADD_INSN(ret, line_node, pop);
7756 ADD_LABEL(ret, match_failed);
7757 ADD_INSN(ret, line_node, pop);
7759 ADD_INSN(ret, line_node, pop);
7761 ADD_INSNL(ret, line_node, jump, unmatched);
7814 const NODE *args = RNODE_FNDPTN(node)->args;
7815 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7817 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7818 match_failed = NEW_LABEL(line);
7819 type_error = NEW_LABEL(line);
7820 deconstruct = NEW_LABEL(line);
7821 deconstructed = NEW_LABEL(line);
7823 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7825 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7827 ADD_INSN(ret, line_node, dup);
7828 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7829 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7830 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7831 if (in_single_pattern) {
7832 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 ));
7834 ADD_INSNL(ret, line_node, branchunless, match_failed);
7837 LABEL *while_begin = NEW_LABEL(nd_line(node));
7838 LABEL *next_loop = NEW_LABEL(nd_line(node));
7839 LABEL *find_succeeded = NEW_LABEL(line);
7840 LABEL *find_failed = NEW_LABEL(nd_line(node));
7843 ADD_INSN(ret, line_node, dup);
7844 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7846 ADD_INSN(ret, line_node, dup);
7847 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7848 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7850 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7852 ADD_LABEL(ret, while_begin);
7854 ADD_INSN(ret, line_node, dup);
7855 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7856 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7857 ADD_INSNL(ret, line_node, branchunless, find_failed);
7859 for (j = 0; j < args_num; j++) {
7860 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7861 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7863 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7864 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7866 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7868 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7869 args = RNODE_LIST(args)->nd_next;
7872 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7873 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7874 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7875 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7876 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7877 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));
7879 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7880 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7881 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7882 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7883 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7884 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7885 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7886 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));
7888 ADD_INSNL(ret, line_node, jump, find_succeeded);
7890 ADD_LABEL(ret, next_loop);
7891 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7892 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7893 ADD_INSNL(ret, line_node, jump, while_begin);
7895 ADD_LABEL(ret, find_failed);
7896 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7897 if (in_single_pattern) {
7898 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7899 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7900 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7901 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7902 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7904 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7905 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7907 ADD_INSN(ret, line_node, pop);
7908 ADD_INSN(ret, line_node, pop);
7910 ADD_INSNL(ret, line_node, jump, match_failed);
7911 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7913 ADD_LABEL(ret, find_succeeded);
7914 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7917 ADD_INSN(ret, line_node, pop);
7918 ADD_INSNL(ret, line_node, jump, matched);
7919 ADD_INSN(ret, line_node, putnil);
7921 ADD_LABEL(ret, type_error);
7922 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7924 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7925 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7926 ADD_INSN(ret, line_node, pop);
7928 ADD_LABEL(ret, match_failed);
7929 ADD_INSN(ret, line_node, pop);
7930 ADD_INSNL(ret, line_node, jump, unmatched);
7994 LABEL *match_failed, *type_error;
7997 match_failed = NEW_LABEL(line);
7998 type_error = NEW_LABEL(line);
8000 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
8001 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
8002 keys =
rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
8004 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
8005 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
8009 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
8011 ADD_INSN(ret, line_node, dup);
8012 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
8013 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
8014 if (in_single_pattern) {
8015 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
8017 ADD_INSNL(ret, line_node, branchunless, match_failed);
8020 ADD_INSN(ret, line_node, putnil);
8024 ADD_INSN1(ret, line_node, duparray, keys);
8027 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
8029 ADD_INSN(ret, line_node, dup);
8031 ADD_INSNL(ret, line_node, branchunless, type_error);
8033 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
8034 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
8037 if (RNODE_HSHPTN(node)->nd_pkwargs) {
8041 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
8043 DECL_ANCHOR(match_values);
8044 INIT_ANCHOR(match_values);
8045 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
8046 for (i = 0; i < keys_num; i++) {
8047 NODE *key_node = RNODE_LIST(args)->nd_head;
8048 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
8049 VALUE key = get_symbol_value(iseq, key_node);
8051 ADD_INSN(ret, line_node, dup);
8052 ADD_INSN1(ret, line_node, putobject, key);
8053 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
8054 if (in_single_pattern) {
8055 LABEL *match_succeeded;
8056 match_succeeded = NEW_LABEL(line);
8058 ADD_INSN(ret, line_node, dup);
8059 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8063 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
8064 ADD_INSN1(ret, line_node, putobject,
Qtrue);
8065 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
8066 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8067 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
8068 ADD_INSN1(ret, line_node, putobject, key);
8069 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
8071 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
8073 ADD_LABEL(ret, match_succeeded);
8075 ADD_INSNL(ret, line_node, branchunless, match_failed);
8077 ADD_INSN(match_values, line_node, dup);
8078 ADD_INSN1(match_values, line_node, putobject, key);
8079 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
8080 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
8081 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
8083 ADD_SEQ(ret, match_values);
8087 ADD_INSN(ret, line_node, dup);
8088 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
8089 if (in_single_pattern) {
8090 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
8092 ADD_INSNL(ret, line_node, branchunless, match_failed);
8095 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
8096 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
8097 ADD_INSN(ret, line_node, dup);
8098 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
8099 if (in_single_pattern) {
8100 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
8102 ADD_INSNL(ret, line_node, branchunless, match_failed);
8105 ADD_INSN(ret, line_node, dup);
8106 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
8110 ADD_INSN(ret, line_node, pop);
8111 ADD_INSNL(ret, line_node, jump, matched);
8112 ADD_INSN(ret, line_node, putnil);
8114 ADD_LABEL(ret, type_error);
8115 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8117 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
8118 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
8119 ADD_INSN(ret, line_node, pop);
8121 ADD_LABEL(ret, match_failed);
8122 ADD_INSN(ret, line_node, pop);
8123 ADD_INSNL(ret, line_node, jump, unmatched);
8132 case NODE_IMAGINARY:
8160 CHECK(COMPILE(ret,
"case in literal", node));
8161 if (in_single_pattern) {
8162 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
8164 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
8165 if (in_single_pattern) {
8166 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
8168 ADD_INSNL(ret, line_node, branchif, matched);
8169 ADD_INSNL(ret, line_node, jump, unmatched);
8173 ID id = RNODE_LASGN(node)->nd_vid;
8174 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8176 if (in_alt_pattern) {
8177 const char *name = rb_id2name(
id);
8178 if (name && strlen(name) > 0 && name[0] !=
'_') {
8179 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
8185 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8186 ADD_INSNL(ret, line_node, jump, matched);
8191 ID id = RNODE_DASGN(node)->nd_vid;
8193 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
8195 if (in_alt_pattern) {
8196 const char *name = rb_id2name(
id);
8197 if (name && strlen(name) > 0 && name[0] !=
'_') {
8198 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
8205 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
8209 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
8210 ADD_INSNL(ret, line_node, jump, matched);
8215 LABEL *match_failed;
8216 match_failed = unmatched;
8217 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
8218 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
8219 if (in_single_pattern) {
8220 LABEL *match_succeeded;
8221 match_succeeded = NEW_LABEL(line);
8223 ADD_INSN(ret, line_node, dup);
8224 if (nd_type_p(node, NODE_IF)) {
8225 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8228 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
8231 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
8232 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8233 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8234 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8236 ADD_INSN(ret, line_node, pop);
8237 ADD_INSN(ret, line_node, pop);
8239 ADD_LABEL(ret, match_succeeded);
8241 if (nd_type_p(node, NODE_IF)) {
8242 ADD_INSNL(ret, line_node, branchunless, match_failed);
8245 ADD_INSNL(ret, line_node, branchif, match_failed);
8247 ADD_INSNL(ret, line_node, jump, matched);
8252 LABEL *match_failed;
8253 match_failed = NEW_LABEL(line);
8255 n = RNODE_HASH(node)->nd_head;
8256 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
8257 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8261 ADD_INSN(ret, line_node, dup);
8262 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));
8263 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));
8264 ADD_INSN(ret, line_node, putnil);
8266 ADD_LABEL(ret, match_failed);
8267 ADD_INSN(ret, line_node, pop);
8268 ADD_INSNL(ret, line_node, jump, unmatched);
8272 LABEL *match_succeeded, *fin;
8273 match_succeeded = NEW_LABEL(line);
8274 fin = NEW_LABEL(line);
8276 ADD_INSN(ret, line_node, dup);
8277 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));
8278 ADD_LABEL(ret, match_succeeded);
8279 ADD_INSN(ret, line_node, pop);
8280 ADD_INSNL(ret, line_node, jump, matched);
8281 ADD_INSN(ret, line_node, putnil);
8282 ADD_LABEL(ret, fin);
8283 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
8287 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
8293iseq_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)
8295 LABEL *fin = NEW_LABEL(nd_line(node));
8296 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
8297 ADD_LABEL(ret, fin);
8302iseq_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)
8304 const NODE *line_node = node;
8306 if (RNODE_ARYPTN(node)->nd_pconst) {
8307 ADD_INSN(ret, line_node, dup);
8308 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
8309 if (in_single_pattern) {
8310 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
8312 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
8313 if (in_single_pattern) {
8314 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
8316 ADD_INSNL(ret, line_node, branchunless, match_failed);
8323iseq_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)
8325 const NODE *line_node = node;
8329 if (use_deconstructed_cache) {
8331 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8332 ADD_INSNL(ret, line_node, branchnil, deconstruct);
8335 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8336 ADD_INSNL(ret, line_node, branchunless, match_failed);
8339 ADD_INSN(ret, line_node, pop);
8340 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
8341 ADD_INSNL(ret, line_node, jump, deconstructed);
8344 ADD_INSNL(ret, line_node, jump, deconstruct);
8347 ADD_LABEL(ret, deconstruct);
8348 ADD_INSN(ret, line_node, dup);
8349 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
8350 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
8353 if (use_deconstructed_cache) {
8354 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
8357 if (in_single_pattern) {
8358 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
8361 ADD_INSNL(ret, line_node, branchunless, match_failed);
8363 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
8366 if (use_deconstructed_cache) {
8367 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8370 ADD_INSN(ret, line_node, dup);
8372 ADD_INSNL(ret, line_node, branchunless, type_error);
8374 ADD_LABEL(ret, deconstructed);
8380iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index)
8390 const int line = nd_line(node);
8391 const NODE *line_node = node;
8392 LABEL *match_succeeded = NEW_LABEL(line);
8394 ADD_INSN(ret, line_node, dup);
8395 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8397 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8398 ADD_INSN1(ret, line_node, putobject, errmsg);
8399 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8400 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
8401 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8403 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8404 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8406 ADD_INSN(ret, line_node, pop);
8407 ADD_INSN(ret, line_node, pop);
8408 ADD_LABEL(ret, match_succeeded);
8414iseq_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)
8424 const int line = nd_line(node);
8425 const NODE *line_node = node;
8426 LABEL *match_succeeded = NEW_LABEL(line);
8428 ADD_INSN(ret, line_node, dup);
8429 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8431 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8432 ADD_INSN1(ret, line_node, putobject, errmsg);
8433 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8434 ADD_INSN(ret, line_node, dup);
8435 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
8436 ADD_INSN1(ret, line_node, putobject, pattern_length);
8437 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
8438 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8440 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8441 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8443 ADD_INSN(ret, line_node, pop);
8444 ADD_INSN(ret, line_node, pop);
8445 ADD_LABEL(ret, match_succeeded);
8451iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
8461 const int line = nd_line(node);
8462 const NODE *line_node = node;
8463 LABEL *match_succeeded = NEW_LABEL(line);
8465 ADD_INSN(ret, line_node, dup);
8466 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8468 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8469 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
8470 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8471 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
8472 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
8473 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8475 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8476 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8478 ADD_INSN(ret, line_node, pop);
8479 ADD_INSN(ret, line_node, pop);
8481 ADD_LABEL(ret, match_succeeded);
8482 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
8483 ADD_INSN(ret, line_node, pop);
8484 ADD_INSN(ret, line_node, pop);
8490compile_case3(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
8492 const NODE *pattern;
8493 const NODE *node = orig_node;
8494 LABEL *endlabel, *elselabel;
8496 DECL_ANCHOR(body_seq);
8497 DECL_ANCHOR(cond_seq);
8499 enum node_type
type;
8500 const NODE *line_node;
8503 bool single_pattern;
8506 INIT_ANCHOR(body_seq);
8507 INIT_ANCHOR(cond_seq);
8509 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"case");
8511 node = RNODE_CASE3(node)->nd_body;
8512 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
8513 type = nd_type(node);
8514 line = nd_line(node);
8516 single_pattern = !RNODE_IN(node)->nd_next;
8518 endlabel = NEW_LABEL(line);
8519 elselabel = NEW_LABEL(line);
8521 if (single_pattern) {
8523 ADD_INSN(head, line_node, putnil);
8524 ADD_INSN(head, line_node, putnil);
8525 ADD_INSN1(head, line_node, putobject,
Qfalse);
8526 ADD_INSN(head, line_node, putnil);
8528 ADD_INSN(head, line_node, putnil);
8530 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8534 while (
type == NODE_IN) {
8538 ADD_INSN(body_seq, line_node, putnil);
8540 l1 = NEW_LABEL(line);
8541 ADD_LABEL(body_seq, l1);
8542 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8544 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8545 add_trace_branch_coverage(
8548 nd_code_loc(coverage_node),
8549 nd_node_id(coverage_node),
8554 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8555 ADD_INSNL(body_seq, line_node, jump, endlabel);
8557 pattern = RNODE_IN(node)->nd_head;
8559 int pat_line = nd_line(pattern);
8560 LABEL *next_pat = NEW_LABEL(pat_line);
8561 ADD_INSN (cond_seq, pattern, dup);
8563 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8564 ADD_LABEL(cond_seq, next_pat);
8565 LABEL_UNREMOVABLE(next_pat);
8568 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8572 node = RNODE_IN(node)->nd_next;
8576 type = nd_type(node);
8577 line = nd_line(node);
8582 ADD_LABEL(cond_seq, elselabel);
8583 ADD_INSN(cond_seq, line_node, pop);
8584 ADD_INSN(cond_seq, line_node, pop);
8585 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8586 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8587 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8588 ADD_INSN(cond_seq, line_node, putnil);
8590 ADD_INSN(cond_seq, line_node, putnil);
8594 debugs(
"== else (implicit)\n");
8595 ADD_LABEL(cond_seq, elselabel);
8596 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8597 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8599 if (single_pattern) {
8607 LABEL *key_error, *fin;
8610 key_error = NEW_LABEL(line);
8611 fin = NEW_LABEL(line);
8614 kw_arg->references = 0;
8615 kw_arg->keyword_len = 2;
8616 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
8617 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
8619 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8620 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8622 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8623 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8624 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8625 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8626 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8627 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8628 ADD_INSNL(cond_seq, orig_node, jump, fin);
8630 ADD_LABEL(cond_seq, key_error);
8632 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8633 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8634 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8635 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8636 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8637 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8638 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8639 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
8640 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8642 ADD_LABEL(cond_seq, fin);
8646 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8647 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8649 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8651 ADD_INSN(cond_seq, orig_node, putnil);
8653 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8654 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8656 ADD_INSN(cond_seq, line_node, putnil);
8660 ADD_SEQ(ret, cond_seq);
8661 ADD_SEQ(ret, body_seq);
8662 ADD_LABEL(ret, endlabel);
8666#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8667#undef CASE3_BI_OFFSET_ERROR_STRING
8668#undef CASE3_BI_OFFSET_KEY_ERROR_P
8669#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8670#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8673compile_loop(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
8675 const int line = (int)nd_line(node);
8676 const NODE *line_node = node;
8677 int result = COMPILE_OK;
8679 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8680 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8681 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8682 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8687 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8688 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8689 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8690 LABEL *end_label = NEW_LABEL(line);
8691 LABEL *adjust_label = NEW_LABEL(line);
8693 LABEL *next_catch_label = NEW_LABEL(line);
8694 LABEL *tmp_label = NULL;
8696 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8697 push_ensure_entry(iseq, &enl, NULL, NULL);
8699 if (RNODE_WHILE(node)->nd_state == 1) {
8700 ADD_INSNL(ret, line_node, jump, next_label);
8703 tmp_label = NEW_LABEL(line);
8704 ADD_INSNL(ret, line_node, jump, tmp_label);
8706 ADD_LABEL(ret, adjust_label);
8707 ADD_INSN(ret, line_node, putnil);
8708 ADD_LABEL(ret, next_catch_label);
8709 ADD_INSN(ret, line_node, pop);
8710 ADD_INSNL(ret, line_node, jump, next_label);
8711 if (tmp_label) ADD_LABEL(ret, tmp_label);
8713 ADD_LABEL(ret, redo_label);
8714 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8716 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8717 add_trace_branch_coverage(
8720 nd_code_loc(coverage_node),
8721 nd_node_id(coverage_node),
8726 if (!COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body)) {
8727 result = COMPILE_NG;
8730 ADD_LABEL(ret, next_label);
8732 if (
type == NODE_WHILE) {
8733 if (!compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8734 redo_label, end_label)) {
8735 result = COMPILE_NG;
8741 if (!compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8742 end_label, redo_label)) {
8743 result = COMPILE_NG;
8748 ADD_LABEL(ret, end_label);
8749 ADD_ADJUST_RESTORE(ret, adjust_label);
8751 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8753 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8754 result = COMPILE_NG;
8758 ADD_INSN(ret, line_node, putnil);
8761 ADD_LABEL(ret, break_label);
8764 ADD_INSN(ret, line_node, pop);
8767 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8769 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8771 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8772 ISEQ_COMPILE_DATA(iseq)->redo_label);
8775 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8776 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8777 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8778 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8779 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
8784compile_iter(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8786 const int line = nd_line(node);
8787 const NODE *line_node = node;
8788 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8789 LABEL *retry_label = NEW_LABEL(line);
8790 LABEL *retry_end_l = NEW_LABEL(line);
8791 const rb_iseq_t *child_iseq;
8793 ADD_LABEL(ret, retry_label);
8794 if (nd_type_p(node, NODE_FOR)) {
8795 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8797 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8798 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8799 ISEQ_TYPE_BLOCK, line);
8800 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8803 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8804 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8805 ISEQ_TYPE_BLOCK, line);
8806 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8819 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
8820 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
8821 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8822 iobj = (INSN*) get_prev_insn(iobj);
8824 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
8828 if (&iobj->link == LAST_ELEMENT(ret)) {
8829 ret->last = (LINK_ELEMENT*) retry_end_l;
8834 ADD_INSN(ret, line_node, pop);
8837 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8839 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8844compile_for_masgn(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8849 const NODE *line_node = node;
8850 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8851 LABEL *not_single = NEW_LABEL(nd_line(var));
8852 LABEL *not_ary = NEW_LABEL(nd_line(var));
8853 CHECK(COMPILE(ret,
"for var", var));
8854 ADD_INSN(ret, line_node, dup);
8855 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8856 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8857 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8858 ADD_INSNL(ret, line_node, branchunless, not_single);
8859 ADD_INSN(ret, line_node, dup);
8860 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8861 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8862 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8863 ADD_INSN(ret, line_node, swap);
8864 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
8865 ADD_INSN(ret, line_node, dup);
8866 ADD_INSNL(ret, line_node, branchunless, not_ary);
8867 ADD_INSN(ret, line_node, swap);
8868 ADD_LABEL(ret, not_ary);
8869 ADD_INSN(ret, line_node, pop);
8870 ADD_LABEL(ret, not_single);
8875compile_break(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8877 const NODE *line_node = node;
8878 unsigned long throw_flag = 0;
8880 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8882 LABEL *splabel = NEW_LABEL(0);
8883 ADD_LABEL(ret, splabel);
8884 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8885 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8886 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8887 add_ensure_iseq(ret, iseq, 0);
8888 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8889 ADD_ADJUST_RESTORE(ret, splabel);
8892 ADD_INSN(ret, line_node, putnil);
8896 const rb_iseq_t *ip = iseq;
8899 if (!ISEQ_COMPILE_DATA(ip)) {
8904 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8905 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8907 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8910 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8911 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8915 ip = ISEQ_BODY(ip)->parent_iseq;
8920 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8921 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8923 ADD_INSN(ret, line_node, pop);
8927 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8934compile_next(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8936 const NODE *line_node = node;
8937 unsigned long throw_flag = 0;
8939 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8940 LABEL *splabel = NEW_LABEL(0);
8941 debugs(
"next in while loop\n");
8942 ADD_LABEL(ret, splabel);
8943 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8944 add_ensure_iseq(ret, iseq, 0);
8945 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8946 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8947 ADD_ADJUST_RESTORE(ret, splabel);
8949 ADD_INSN(ret, line_node, putnil);
8952 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8953 LABEL *splabel = NEW_LABEL(0);
8954 debugs(
"next in block\n");
8955 ADD_LABEL(ret, splabel);
8956 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8957 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8958 add_ensure_iseq(ret, iseq, 0);
8959 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8960 ADD_ADJUST_RESTORE(ret, splabel);
8963 ADD_INSN(ret, line_node, putnil);
8967 const rb_iseq_t *ip = iseq;
8970 if (!ISEQ_COMPILE_DATA(ip)) {
8975 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8976 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8980 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8983 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8984 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8988 ip = ISEQ_BODY(ip)->parent_iseq;
8991 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8992 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8995 ADD_INSN(ret, line_node, pop);
8999 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
9007compile_redo(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9009 const NODE *line_node = node;
9011 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
9012 LABEL *splabel = NEW_LABEL(0);
9013 debugs(
"redo in while");
9014 ADD_LABEL(ret, splabel);
9015 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
9016 add_ensure_iseq(ret, iseq, 0);
9017 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
9018 ADD_ADJUST_RESTORE(ret, splabel);
9020 ADD_INSN(ret, line_node, putnil);
9023 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
9024 LABEL *splabel = NEW_LABEL(0);
9026 debugs(
"redo in block");
9027 ADD_LABEL(ret, splabel);
9028 add_ensure_iseq(ret, iseq, 0);
9029 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
9030 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
9031 ADD_ADJUST_RESTORE(ret, splabel);
9034 ADD_INSN(ret, line_node, putnil);
9038 const rb_iseq_t *ip = iseq;
9041 if (!ISEQ_COMPILE_DATA(ip)) {
9046 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
9049 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
9052 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
9053 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
9057 ip = ISEQ_BODY(ip)->parent_iseq;
9060 ADD_INSN(ret, line_node, putnil);
9061 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
9064 ADD_INSN(ret, line_node, pop);
9068 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
9076compile_retry(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9078 const NODE *line_node = node;
9080 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
9081 ADD_INSN(ret, line_node, putnil);
9082 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
9085 ADD_INSN(ret, line_node, pop);
9089 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
9096compile_rescue(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9098 const int line = nd_line(node);
9099 const NODE *line_node = node;
9100 LABEL *lstart = NEW_LABEL(line);
9101 LABEL *lend = NEW_LABEL(line);
9102 LABEL *lcont = NEW_LABEL(line);
9103 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
9105 ISEQ_BODY(iseq)->location.label),
9106 ISEQ_TYPE_RESCUE, line);
9108 lstart->rescued = LABEL_RESCUE_BEG;
9109 lend->rescued = LABEL_RESCUE_END;
9110 ADD_LABEL(ret, lstart);
9112 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
9113 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
9115 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
9117 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
9119 ADD_LABEL(ret, lend);
9120 if (RNODE_RESCUE(node)->nd_else) {
9121 ADD_INSN(ret, line_node, pop);
9122 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
9124 ADD_INSN(ret, line_node, nop);
9125 ADD_LABEL(ret, lcont);
9128 ADD_INSN(ret, line_node, pop);
9132 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
9133 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
9138compile_resbody(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9140 const int line = nd_line(node);
9141 const NODE *line_node = node;
9142 const NODE *resq = node;
9144 LABEL *label_miss, *label_hit;
9147 label_miss = NEW_LABEL(line);
9148 label_hit = NEW_LABEL(line);
9150 narg = RNODE_RESBODY(resq)->nd_args;
9152 switch (nd_type(narg)) {
9155 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9156 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
9157 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
9158 ADD_INSNL(ret, line_node, branchif, label_hit);
9159 narg = RNODE_LIST(narg)->nd_next;
9165 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9166 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
9167 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
9168 ADD_INSNL(ret, line_node, branchif, label_hit);
9171 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
9175 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9177 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
9178 ADD_INSNL(ret, line_node, branchif, label_hit);
9180 ADD_INSNL(ret, line_node, jump, label_miss);
9181 ADD_LABEL(ret, label_hit);
9184 if (RNODE_RESBODY(resq)->nd_exc_var) {
9185 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
9188 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) {
9190 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
9193 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
9196 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
9197 ADD_INSN(ret, line_node, nop);
9199 ADD_INSN(ret, line_node, leave);
9200 ADD_LABEL(ret, label_miss);
9201 resq = RNODE_RESBODY(resq)->nd_next;
9207stmts_first_lineno(
const NODE *stmts)
9211 const NODE *first = nd_type_p(stmts, NODE_BLOCK) ? RNODE_BLOCK(stmts)->nd_head : stmts;
9212 const int line = nd_first_lineno(stmts);
9213 return nd_line(first) < line ? nd_line(first) : line;
9217compile_ensure(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9219 const int line = stmts_first_lineno(RNODE_ENSURE(node)->nd_ensr);
9220 const NODE *line_node = node;
9222 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
9224 ISEQ_TYPE_ENSURE, line);
9225 LABEL *lstart = NEW_LABEL(line);
9226 LABEL *lend = NEW_LABEL(line);
9227 LABEL *lcont = NEW_LABEL(line);
9233 int result = COMPILE_OK;
9236 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
9238 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
9243 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
9245 ADD_LABEL(ret, lstart);
9246 if (!COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave))) {
9247 result = COMPILE_NG;
9250 ADD_LABEL(ret, lend);
9252 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
9253 ADD_LABEL(ret, lcont);
9254 if (last_leave) ADD_INSN(ret, line_node, pop);
9256 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
9257 if (lstart->link.next != &lend->link) {
9259 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
9261 erange = erange->next;
9266 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
9271compile_return(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9273 const NODE *line_node = node;
9276 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
9277 const rb_iseq_t *is = iseq;
9278 enum rb_iseq_type t =
type;
9279 const NODE *retval = RNODE_RETURN(node)->nd_stts;
9282 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
9283 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
9284 t = ISEQ_BODY(is)->type;
9288 case ISEQ_TYPE_MAIN:
9290 rb_warn(
"argument of top-level return is ignored");
9294 type = ISEQ_TYPE_METHOD;
9301 bool direct_return = can_inline_return(iseq,
type);
9302 if (direct_return) {
9303 splabel = NEW_LABEL(0);
9304 ADD_LABEL(ret, splabel);
9305 ADD_ADJUST(ret, line_node, 0);
9308 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
9310 if (direct_return) {
9311 add_ensure_iseq(ret, iseq, 1);
9313 ADD_INSN(ret, line_node, leave);
9314 ADD_ADJUST_RESTORE(ret, splabel);
9317 ADD_INSN(ret, line_node, putnil);
9321 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
9323 ADD_INSN(ret, line_node, pop);
9331drop_unreachable_return(LINK_ANCHOR *ret)
9333 LINK_ELEMENT *i = ret->last, *last;
9334 if (!i)
return false;
9335 if (IS_TRACE(i)) i = i->prev;
9336 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
9338 if (IS_ADJUST(i)) i = i->prev;
9339 if (!IS_INSN(i))
return false;
9340 switch (INSN_OF(i)) {
9347 (ret->last = last->prev)->next = NULL;
9352compile_evstr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9354 CHECK(COMPILE_(ret,
"nd_body", node, popped));
9356 if (!popped && !all_string_result_p(node)) {
9357 const NODE *line_node = node;
9358 const unsigned int flag = VM_CALL_FCALL;
9362 ADD_INSN(ret, line_node, dup);
9363 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
9364 ADD_INSN(ret, line_node, anytostring);
9370compile_lvar(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *line_node,
ID id)
9372 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
9374 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
9375 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
9379qcall_branch_start(rb_iseq_t *iseq, LINK_ANCHOR *
const recv,
VALUE *branches,
const NODE *node,
const NODE *line_node)
9381 LABEL *else_label = NEW_LABEL(nd_line(line_node));
9384 br = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"&.");
9386 ADD_INSN(recv, line_node, dup);
9387 ADD_INSNL(recv, line_node, branchnil, else_label);
9388 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
9393qcall_branch_end(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, LABEL *else_label,
VALUE branches,
const NODE *node,
const NODE *line_node)
9396 if (!else_label)
return;
9397 end_label = NEW_LABEL(nd_line(line_node));
9398 ADD_INSNL(ret, line_node, jump, end_label);
9399 ADD_LABEL(ret, else_label);
9400 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
9401 ADD_LABEL(ret, end_label);
9405compile_call_precheck_freeze(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
const NODE *line_node,
int popped)
9410 if (get_nd_recv(node) &&
9411 nd_type_p(get_nd_recv(node), NODE_STR) &&
9412 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
9413 get_nd_args(node) == NULL &&
9414 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9415 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
9416 VALUE str = get_string_value(get_nd_recv(node));
9417 if (get_node_call_nd_mid(node) == idUMinus) {
9418 ADD_INSN2(ret, line_node, opt_str_uminus, str,
9419 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
9422 ADD_INSN2(ret, line_node, opt_str_freeze, str,
9423 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
9427 ADD_INSN(ret, line_node, pop);
9435iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
9437 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
9441iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
9444 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
9445 for (i=0; table[i].index != -1; i++) {
9446 if (strcmp(table[i].name, name) == 0) {
9454iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
9456 const char *name = rb_id2name(mid);
9457 static const char prefix[] =
"__builtin_";
9458 const size_t prefix_len =
sizeof(prefix) - 1;
9463 switch (nd_type(recv)) {
9465 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
9470 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
9480 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
9481 return &name[prefix_len];
9490delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
9497 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
9498 unsigned int start=0;
9503 argc + start <= ISEQ_BODY(iseq)->local_table_size;
9505 const LINK_ELEMENT *elem = FIRST_ELEMENT(args);
9507 for (
unsigned int i=start; i-start<argc; i++) {
9508 if (IS_INSN(elem) &&
9509 INSN_OF(elem) == BIN(getlocal)) {
9510 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
9511 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
9513 if (local_level == 0) {
9514 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
9516 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
9517 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
9518 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
9519 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9543 *pstart_index = start;
9552compile_builtin_attr_symbol(rb_iseq_t *iseq,
VALUE symbol)
9555 if (rb_streql_lit(
string,
"leaf")) {
9556 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9558 else if (rb_streql_lit(
string,
"inline_block")) {
9559 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9561 else if (rb_streql_lit(
string,
"use_block")) {
9562 iseq_set_use_block(iseq);
9564 else if (rb_streql_lit(
string,
"c_trace")) {
9566 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9568 else if (rb_streql_lit(
string,
"without_interrupts")) {
9569 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_WITHOUT_INTERRUPTS;
9571 else if (rb_streql_lit(
string,
"caller_user_box")) {
9572 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_CALLER_USER_BOX;
9574 else if (rb_streql_lit(
string,
"always_inline")) {
9575 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_ALWAYS_INLINE;
9585compile_builtin_attr(rb_iseq_t *iseq,
const NODE *node)
9588 if (!node)
goto no_arg;
9590 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9591 const NODE *next = RNODE_LIST(node)->nd_next;
9593 node = RNODE_LIST(node)->nd_head;
9594 if (!node)
goto no_arg;
9595 switch (nd_type(node)) {
9597 symbol = rb_node_sym_string_val(node);
9603 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9604 if (compile_builtin_attr_symbol(iseq, symbol) != COMPILE_OK)
goto unknown_arg;
9609 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9612 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9615 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %" PRIsVALUE, symbol);
9618 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9622compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
const NODE *line_node,
int popped)
9626 if (!node)
goto no_arg;
9627 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9628 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9629 node = RNODE_LIST(node)->nd_head;
9630 if (!node)
goto no_arg;
9631 switch (nd_type(node)) {
9633 name = rb_node_sym_string_val(node);
9638 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9640 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9644 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9647 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9650 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9651 rb_builtin_class_name(name));
9654 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9658mandatory_node(
const rb_iseq_t *iseq,
const NODE *cond_node)
9660 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9661 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9662 return RNODE_IF(node)->nd_body;
9665 rb_bug(
"mandatory_node: can't find mandatory node");
9670compile_builtin_mandatory_only_method(rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9674 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9676 rb_node_args_t args_node;
9677 rb_node_init(RNODE(&args_node), NODE_ARGS);
9678 args_node.nd_ainfo = args;
9681 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9682 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9685 rb_ast_id_table_t *tbl =
ALLOCV(idtmp,
sizeof(rb_ast_id_table_t) + table_size *
sizeof(
ID));
9686 tbl->size = table_size;
9691 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9692 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9695 for (; i<table_size; i++) {
9696 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9699 rb_node_scope_t scope_node;
9700 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9701 scope_node.nd_tbl = tbl;
9702 scope_node.nd_body = mandatory_node(iseq, node);
9703 scope_node.nd_parent = NULL;
9704 scope_node.nd_args = &args_node;
9706 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9708 const rb_iseq_t *mandatory_only_iseq =
9709 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9710 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9711 nd_line(line_node), NULL, 0,
9712 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9713 ISEQ_SCRIPT_LINES(iseq));
9714 rb_iseq_mandatory_only_iseq_set(iseq, mandatory_only_iseq);
9721compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
const NODE *line_node,
int popped,
9722 const rb_iseq_t *parent_block, LINK_ANCHOR *args,
const char *builtin_func)
9724 NODE *args_node = get_nd_args(node);
9726 if (parent_block != NULL) {
9727 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9731# define BUILTIN_INLINE_PREFIX "_bi"
9732 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9733 bool cconst =
false;
9738 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9739 strcmp(
"cexpr!", builtin_func) == 0) {
9742 else if (strcmp(
"cconst!", builtin_func) == 0) {
9745 else if (strcmp(
"cinit!", builtin_func) == 0) {
9749 else if (strcmp(
"attr!", builtin_func) == 0) {
9750 return compile_builtin_attr(iseq, args_node);
9752 else if (strcmp(
"arg!", builtin_func) == 0) {
9753 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9755 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9757 rb_bug(
"mandatory_only? should be in if condition");
9759 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9760 rb_bug(
"mandatory_only? should be put on top");
9763 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9764 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9767 rb_bug(
"can't find builtin function:%s", builtin_func);
9770 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9774 int inline_index = nd_line(node);
9775 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9776 builtin_func = inline_func;
9782 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9783 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9784 ADD_INSN1(ret, line_node, putobject, const_val);
9790 unsigned int flag = 0;
9792 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9794 if (
FIX2INT(argc) != bf->argc) {
9795 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9796 builtin_func, bf->argc,
FIX2INT(argc));
9800 unsigned int start_index;
9801 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9802 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9806 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9809 if (popped) ADD_INSN(ret, line_node, pop);
9815compile_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)
9823 ID mid = get_node_call_nd_mid(node);
9825 unsigned int flag = 0;
9827 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9828 LABEL *else_label = NULL;
9831 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9837 if (nd_type_p(node, NODE_VCALL)) {
9842 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9844 if (mid == id_bitblt) {
9845 ADD_INSN(ret, line_node, bitblt);
9848 else if (mid == id_answer) {
9849 ADD_INSN(ret, line_node, answer);
9861 if (nd_type_p(node, NODE_FCALL) &&
9862 (mid == goto_id || mid == label_id)) {
9865 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9868 if (!labels_table) {
9869 labels_table = st_init_numtable();
9870 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9873 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9877 if (mid == goto_id) {
9878 ADD_INSNL(ret, line_node, jump, label);
9881 ADD_LABEL(ret, label);
9888 const char *builtin_func;
9889 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9890 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9891 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9895 if (!assume_receiver) {
9896 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9899 if ((mid == idCall || mid == idAREF || mid == idYield || mid == idEqq) &&
9900 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9901 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9902 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9904 else if (private_recv_p(node)) {
9905 ADD_INSN(recv, node, putself);
9906 flag |= VM_CALL_FCALL;
9909 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9912 if (
type == NODE_QCALL) {
9913 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9916 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9917 ADD_CALL_RECEIVER(recv, line_node);
9922 if (
type != NODE_VCALL) {
9923 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9924 CHECK(!
NIL_P(argc));
9932 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
9933 mid == rb_intern(
"new") &&
9934 parent_block == NULL &&
9935 !(flag & VM_CALL_ARGS_BLOCKARG);
9938 ADD_INSN(ret, node, putnil);
9939 ADD_INSN(ret, node, swap);
9944 debugp_param(
"call args argc", argc);
9945 debugp_param(
"call method",
ID2SYM(mid));
9947 switch ((
int)
type) {
9949 flag |= VM_CALL_VCALL;
9952 flag |= VM_CALL_FCALL;
9955 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9956 ADD_INSN(ret, line_node, splatkw);
9959 LABEL *not_basic_new = NEW_LABEL(nd_line(node));
9960 LABEL *not_basic_new_finish = NEW_LABEL(nd_line(node));
9965 if (flag & VM_CALL_FORWARDING) {
9966 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc) + 1, flag, keywords, 0);
9969 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc), flag, keywords, 0);
9971 ADD_INSN2(ret, node, opt_new, ci, not_basic_new);
9972 LABEL_REF(not_basic_new);
9975 ADD_SEND_R(ret, line_node, rb_intern(
"initialize"), argc, parent_block,
INT2FIX(flag | VM_CALL_FCALL | VM_CALL_IGNORE_REFINEMENTS), keywords);
9976 ADD_INSNL(ret, line_node, jump, not_basic_new_finish);
9978 ADD_LABEL(ret, not_basic_new);
9980 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9981 ADD_INSN(ret, line_node, swap);
9983 ADD_LABEL(ret, not_basic_new_finish);
9984 ADD_INSN(ret, line_node, pop);
9987 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9990 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9992 ADD_INSN(ret, line_node, pop);
9998compile_op_asgn1(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10000 const int line = nd_line(node);
10002 unsigned int flag = 0;
10004 ID id = RNODE_OP_ASGN1(node)->nd_mid;
10030 ADD_INSN(ret, node, putnil);
10032 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
10033 CHECK(asgnflag != -1);
10034 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
10039 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
10040 CHECK(!
NIL_P(argc));
10042 int dup_argn =
FIX2INT(argc) + 1;
10043 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
10045 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
10047 if (
id == idOROP ||
id == idANDOP) {
10056 LABEL *label = NEW_LABEL(line);
10057 LABEL *lfin = NEW_LABEL(line);
10059 ADD_INSN(ret, node, dup);
10060 if (
id == idOROP) {
10061 ADD_INSNL(ret, node, branchif, label);
10064 ADD_INSNL(ret, node, branchunless, label);
10066 ADD_INSN(ret, node, pop);
10068 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
10070 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
10072 if (flag & VM_CALL_ARGS_SPLAT) {
10073 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
10074 ADD_INSN(ret, node, swap);
10075 ADD_INSN1(ret, node, splatarray,
Qtrue);
10076 ADD_INSN(ret, node, swap);
10077 flag |= VM_CALL_ARGS_SPLAT_MUT;
10079 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
10080 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
10083 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
10085 ADD_INSN(ret, node, pop);
10086 ADD_INSNL(ret, node, jump, lfin);
10087 ADD_LABEL(ret, label);
10089 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
10091 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
10092 ADD_LABEL(ret, lfin);
10095 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
10096 ADD_SEND(ret, node,
id,
INT2FIX(1));
10098 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
10100 if (flag & VM_CALL_ARGS_SPLAT) {
10101 if (flag & VM_CALL_KW_SPLAT) {
10102 ADD_INSN1(ret, node, topn,
INT2FIX(2));
10103 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
10104 ADD_INSN1(ret, node, splatarray,
Qtrue);
10105 flag |= VM_CALL_ARGS_SPLAT_MUT;
10107 ADD_INSN(ret, node, swap);
10108 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
10109 ADD_INSN1(ret, node, setn,
INT2FIX(2));
10110 ADD_INSN(ret, node, pop);
10113 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
10114 ADD_INSN(ret, node, swap);
10115 ADD_INSN1(ret, node, splatarray,
Qtrue);
10116 ADD_INSN(ret, node, swap);
10117 flag |= VM_CALL_ARGS_SPLAT_MUT;
10119 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
10121 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
10124 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
10126 ADD_INSN(ret, node, pop);
10132compile_op_asgn2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10134 const int line = nd_line(node);
10135 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
10136 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
10138 LABEL *lfin = NEW_LABEL(line);
10139 LABEL *lcfin = NEW_LABEL(line);
10194 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
10195 CHECK(asgnflag != -1);
10196 if (RNODE_OP_ASGN2(node)->nd_aid) {
10197 lskip = NEW_LABEL(line);
10198 ADD_INSN(ret, node, dup);
10199 ADD_INSNL(ret, node, branchnil, lskip);
10201 ADD_INSN(ret, node, dup);
10202 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
10204 if (atype == idOROP || atype == idANDOP) {
10206 ADD_INSN(ret, node, dup);
10208 if (atype == idOROP) {
10209 ADD_INSNL(ret, node, branchif, lcfin);
10212 ADD_INSNL(ret, node, branchunless, lcfin);
10215 ADD_INSN(ret, node, pop);
10217 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
10219 ADD_INSN(ret, node, swap);
10220 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10222 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
10223 ADD_INSNL(ret, node, jump, lfin);
10225 ADD_LABEL(ret, lcfin);
10227 ADD_INSN(ret, node, swap);
10230 ADD_LABEL(ret, lfin);
10233 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
10234 ADD_SEND(ret, node, atype,
INT2FIX(1));
10236 ADD_INSN(ret, node, swap);
10237 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10239 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
10241 if (lskip && popped) {
10242 ADD_LABEL(ret, lskip);
10244 ADD_INSN(ret, node, pop);
10245 if (lskip && !popped) {
10246 ADD_LABEL(ret, lskip);
10251static int compile_shareable_constant_value(rb_iseq_t *iseq, LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
10254compile_op_cdecl(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10256 const int line = nd_line(node);
10258 LABEL *lassign = 0;
10261 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
10263 ADD_INSN1(ret, node, putobject,
rb_cObject);
10266 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
10269 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
10270 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
10273 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
10275 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
10276 lassign = NEW_LABEL(line);
10277 ADD_INSN(ret, node, dup);
10278 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
10280 ADD_INSNL(ret, node, branchunless, lassign);
10282 ADD_INSN(ret, node, dup);
10283 ADD_INSN1(ret, node, putobject,
Qtrue);
10284 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
10286 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
10287 lfin = NEW_LABEL(line);
10288 if (!popped) ADD_INSN(ret, node, dup);
10289 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
10290 ADD_INSNL(ret, node, branchif, lfin);
10292 ADD_INSNL(ret, node, branchunless, lfin);
10294 if (!popped) ADD_INSN(ret, node, pop);
10295 if (lassign) ADD_LABEL(ret, lassign);
10296 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
10299 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10301 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
10302 ADD_INSN(ret, node, swap);
10304 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
10305 ADD_LABEL(ret, lfin);
10306 if (!popped) ADD_INSN(ret, node, swap);
10307 ADD_INSN(ret, node, pop);
10310 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
10312 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
10314 ADD_INSN(ret, node, swap);
10316 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10317 ADD_INSN(ret, node, swap);
10319 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
10325compile_op_log(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
10327 const int line = nd_line(node);
10328 LABEL *lfin = NEW_LABEL(line);
10331 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
10335 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
10336 lassign = lfinish[1];
10338 lassign = NEW_LABEL(line);
10340 ADD_INSNL(ret, node, branchunless, lassign);
10343 lassign = NEW_LABEL(line);
10346 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
10349 ADD_INSN(ret, node, dup);
10352 if (
type == NODE_OP_ASGN_AND) {
10353 ADD_INSNL(ret, node, branchunless, lfin);
10356 ADD_INSNL(ret, node, branchif, lfin);
10360 ADD_INSN(ret, node, pop);
10363 ADD_LABEL(ret, lassign);
10364 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
10365 ADD_LABEL(ret, lfin);
10370compile_super(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
10375 unsigned int flag = 0;
10377 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
10381 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
10383 if (
type == NODE_SUPER) {
10384 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
10385 CHECK(!
NIL_P(vargc));
10387 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
10388 ADD_INSN(args, node, splatkw);
10391 if (flag & VM_CALL_ARGS_BLOCKARG) {
10398 const rb_iseq_t *liseq = body->local_iseq;
10400 const struct rb_iseq_param_keyword *
const local_kwd = local_body->param.keyword;
10401 int lvar_level = get_lvar_level(iseq);
10403 argc = local_body->param.lead_num;
10406 for (i = 0; i < local_body->param.lead_num; i++) {
10407 int idx = local_body->local_table_size - i;
10408 ADD_GETLOCAL(args, node, idx, lvar_level);
10412 if (local_body->param.flags.forwardable) {
10413 flag |= VM_CALL_FORWARDING;
10414 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
10415 ADD_GETLOCAL(args, node, idx, lvar_level);
10418 if (local_body->param.flags.has_opt) {
10421 for (j = 0; j < local_body->param.opt_num; j++) {
10422 int idx = local_body->local_table_size - (i + j);
10423 ADD_GETLOCAL(args, node, idx, lvar_level);
10428 if (local_body->param.flags.has_rest) {
10430 int idx = local_body->local_table_size - rb_iseq_param_rest_start(local_body);
10431 ADD_GETLOCAL(args, node, idx, lvar_level);
10432 ADD_INSN1(args, node, splatarray, RBOOL(local_body->param.flags.has_post));
10434 argc = rb_iseq_param_rest_start(local_body) + 1;
10435 flag |= VM_CALL_ARGS_SPLAT;
10437 if (local_body->param.flags.has_post) {
10439 int post_len = local_body->param.post_num;
10440 int post_start = rb_iseq_param_post_start(local_body);
10442 if (local_body->param.flags.has_rest) {
10444 for (j=0; j<post_len; j++) {
10445 int idx = local_body->local_table_size - (post_start + j);
10446 ADD_GETLOCAL(args, node, idx, lvar_level);
10448 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
10449 flag |= VM_CALL_ARGS_SPLAT_MUT;
10454 for (j=0; j<post_len; j++) {
10455 int idx = local_body->local_table_size - (post_start + j);
10456 ADD_GETLOCAL(args, node, idx, lvar_level);
10458 argc = post_len + post_start;
10462 if (local_body->param.flags.has_kw) {
10463 int local_size = local_body->local_table_size;
10470 if (local_body->param.flags.has_kwrest) {
10471 int idx = local_body->local_table_size - local_kwd->rest_start;
10473 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10474 ADD_GETLOCAL(args, node, idx, lvar_level);
10476 for (i = 0; i < local_kwd->num; ++i) {
10477 ID id = local_kwd->table[i];
10478 int idx = local_size - get_local_var_idx(liseq,
id);
10479 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
10480 ADD_GETLOCAL(args, node, idx, lvar_level);
10482 if (local_body->param.flags.has_kwrest) {
10483 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
10486 ADD_INSN1(args, node, newhash,
INT2FIX(i * 2));
10488 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
10490 else if (local_body->param.flags.has_kwrest) {
10491 int idx = local_body->local_table_size - local_kwd->rest_start;
10492 ADD_GETLOCAL(args, node, idx, lvar_level);
10494 flag |= VM_CALL_KW_SPLAT;
10498 if (use_block && parent_block == NULL) {
10499 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10502 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
10503 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
10504 ADD_INSN(ret, node, putself);
10505 ADD_SEQ(ret, args);
10507 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
10509 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
10510 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
10513 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
10517 ADD_INSN(ret, node, pop);
10523compile_yield(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10527 unsigned int flag = 0;
10532 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
10533 case ISEQ_TYPE_TOP:
10534 case ISEQ_TYPE_MAIN:
10535 case ISEQ_TYPE_CLASS:
10536 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
10541 if (RNODE_YIELD(node)->nd_head) {
10542 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
10543 CHECK(!
NIL_P(argc));
10549 ADD_SEQ(ret, args);
10550 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
10551 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10554 ADD_INSN(ret, node, pop);
10558 const rb_iseq_t *tmp_iseq = iseq;
10559 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
10560 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
10562 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
10568compile_match(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
10575 switch ((
int)
type) {
10578 VALUE re = rb_node_regx_string_val(node);
10580 ADD_INSN1(recv, node, putobject, re);
10581 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
10586 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
10587 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
10590 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
10591 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
10595 ADD_SEQ(ret, recv);
10597 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
10599 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
10600 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
10604 ADD_INSN(ret, node, pop);
10610compile_colon2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10614 rb_const_path_t const_path;
10615 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10616 (const_path = compile_const_path(iseq, node))) {
10617 ISEQ_BODY(iseq)->ic_size++;
10618 ADD_INSN1(ret, node, opt_getconstant_path, (
VALUE)const_path);
10627 CHECK(compile_const_prefix(iseq, node, pref, body));
10628 if (LIST_INSN_SIZE_ZERO(pref)) {
10629 ADD_INSN(ret, node, putnil);
10630 ADD_SEQ(ret, body);
10633 ADD_SEQ(ret, pref);
10634 ADD_SEQ(ret, body);
10640 ADD_CALL_RECEIVER(ret, node);
10641 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10642 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10645 ADD_INSN(ret, node, pop);
10651compile_colon3(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10653 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10656 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10657 ISEQ_BODY(iseq)->ic_size++;
10658 rb_const_path_t const_path = rb_const_path_from_ids(
true, &RNODE_COLON3(node)->nd_mid, 1);
10659 ADD_INSN1(ret, node, opt_getconstant_path, (
VALUE)const_path);
10662 ADD_INSN1(ret, node, putobject,
rb_cObject);
10663 ADD_INSN1(ret, node, putobject,
Qtrue);
10664 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10668 ADD_INSN(ret, node, pop);
10674compile_dots(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const int excl)
10677 const NODE *b = RNODE_DOT2(node)->nd_beg;
10678 const NODE *e = RNODE_DOT2(node)->nd_end;
10680 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10682 VALUE bv = optimized_range_item(b);
10683 VALUE ev = optimized_range_item(e);
10686 ADD_INSN1(ret, node, putobject, val);
10691 CHECK(COMPILE_(ret,
"min", b, popped));
10692 CHECK(COMPILE_(ret,
"max", e, popped));
10694 ADD_INSN1(ret, node, newrange, flag);
10701compile_errinfo(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10704 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10705 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10708 const rb_iseq_t *ip = iseq;
10711 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10714 ip = ISEQ_BODY(ip)->parent_iseq;
10718 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10721 ADD_INSN(ret, node, putnil);
10729compile_kw_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10732 LABEL *end_label = NEW_LABEL(nd_line(node));
10733 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10735 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10737 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10740 else if (nd_type_p(default_value, NODE_SYM) ||
10741 nd_type_p(default_value, NODE_REGX) ||
10742 nd_type_p(default_value, NODE_LINE) ||
10743 nd_type_p(default_value, NODE_INTEGER) ||
10744 nd_type_p(default_value, NODE_FLOAT) ||
10745 nd_type_p(default_value, NODE_RATIONAL) ||
10746 nd_type_p(default_value, NODE_IMAGINARY) ||
10747 nd_type_p(default_value, NODE_NIL) ||
10748 nd_type_p(default_value, NODE_TRUE) ||
10749 nd_type_p(default_value, NODE_FALSE)) {
10750 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10758 int kw_bits_idx = body->local_table_size - body->param.keyword->bits_start;
10759 int keyword_idx = body->param.keyword->num - body->param.keyword->required_num;
10760 for (
const rb_node_kw_arg_t *kwarg = RNODE_KW_ARG(node); kwarg; kwarg = kwarg->nd_next) {
10761 if (get_nd_value(kwarg->nd_body) != NODE_SPECIAL_REQUIRED_KEYWORD) {
10766 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10767 ADD_INSNL(ret, node, branchif, end_label);
10768 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10769 ADD_LABEL(ret, end_label);
10775compile_attrasgn(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10779 unsigned int flag = 0;
10780 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10782 LABEL *else_label = NULL;
10787 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10788 CHECK(!
NIL_P(argc));
10790 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10791 CHECK(asgnflag != -1);
10792 flag |= (
unsigned int)asgnflag;
10794 debugp_param(
"argc", argc);
10795 debugp_param(
"nd_mid",
ID2SYM(mid));
10799 mid = rb_id_attrset(mid);
10800 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10803 ADD_INSN(ret, node, putnil);
10804 ADD_SEQ(ret, recv);
10805 ADD_SEQ(ret, args);
10807 if (flag & VM_CALL_ARGS_SPLAT) {
10808 ADD_INSN(ret, node, dup);
10809 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10810 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10811 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10812 ADD_INSN (ret, node, pop);
10815 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10819 ADD_SEQ(ret, recv);
10820 ADD_SEQ(ret, args);
10822 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10823 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10824 ADD_INSN(ret, node, pop);
10829compile_make_shareable_node(rb_iseq_t *iseq, LINK_ANCHOR *ret, LINK_ANCHOR *sub,
const NODE *value,
bool copy)
10831 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10839 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10846 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10853node_const_decl_val(
const NODE *node)
10856 switch (nd_type(node)) {
10858 if (RNODE_CDECL(node)->nd_vid) {
10859 path = rb_id2str(RNODE_CDECL(node)->nd_vid);
10863 node = RNODE_CDECL(node)->nd_else;
10871 rb_str_append(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10874 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10880 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10881 rb_ary_push(path, rb_id2str(RNODE_COLON2(node)->nd_mid));
10883 if (node && nd_type_p(node, NODE_CONST)) {
10885 rb_ary_push(path, rb_id2str(RNODE_CONST(node)->nd_vid));
10887 else if (node && nd_type_p(node, NODE_COLON3)) {
10889 rb_ary_push(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10899 path = rb_fstring(path);
10904const_decl_path(NODE *dest)
10907 if (!nd_type_p(dest, NODE_CALL)) {
10908 path = node_const_decl_val(dest);
10914compile_ensure_shareable_node(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE *dest,
const NODE *value)
10919 VALUE path = const_decl_path(dest);
10920 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10921 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10922 ADD_INSN1(ret, value, putobject, path);
10924 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10929#ifndef SHAREABLE_BARE_EXPRESSION
10930#define SHAREABLE_BARE_EXPRESSION 1
10934compile_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)
10936# define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10937 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10939 DECL_ANCHOR(anchor);
10941 enum node_type
type = node ? nd_type(node) : NODE_NIL;
10953 *value_p = rb_node_sym_string_val(node);
10956 *value_p = rb_node_regx_string_val(node);
10959 *value_p = rb_node_line_lineno_val(node);
10962 *value_p = rb_node_integer_literal_val(node);
10965 *value_p = rb_node_float_literal_val(node);
10967 case NODE_RATIONAL:
10968 *value_p = rb_node_rational_literal_val(node);
10970 case NODE_IMAGINARY:
10971 *value_p = rb_node_imaginary_literal_val(node);
10973 case NODE_ENCODING:
10974 *value_p = rb_node_encoding_val(node);
10977 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10978 *shareable_literal_p = 1;
10982 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10983 if (shareable == rb_parser_shareable_literal) {
10989 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10992 *shareable_literal_p = 1;
10996 VALUE lit = rb_node_str_string_val(node);
10997 ADD_INSN1(ret, node, putobject, lit);
11000 *shareable_literal_p = 1;
11006 compile_file_path(iseq, ret, node, ISEQ_FROZEN_STRING_LITERAL_ENABLED);
11008 *shareable_literal_p = 1;
11014 ADD_INSN1(ret, node, putobject, lit);
11017 *shareable_literal_p = 1;
11023 INIT_ANCHOR(anchor);
11025 for (NODE *n = (NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
11027 int shareable_literal_p2;
11028 NODE *elt = RNODE_LIST(n)->nd_head;
11030 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
11031 if (shareable_literal_p2) {
11034 else if (
RTEST(lit)) {
11040 if (!UNDEF_P(val)) {
11052 if (!RNODE_HASH(node)->nd_brace)
goto compile_shareable;
11053 for (NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
11054 if (!RNODE_LIST(n)->nd_head) {
11056 goto compile_shareable;
11060 INIT_ANCHOR(anchor);
11062 for (NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
11064 VALUE value_val = 0;
11065 int shareable_literal_p2;
11066 NODE *key = RNODE_LIST(n)->nd_head;
11067 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
11068 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
11069 if (shareable_literal_p2) {
11072 else if (
RTEST(lit)) {
11073 rb_hash_clear(lit);
11076 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
11077 if (shareable_literal_p2) {
11080 else if (
RTEST(lit)) {
11081 rb_hash_clear(lit);
11085 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
11086 rb_hash_aset(lit, key_val, value_val);
11089 rb_hash_clear(lit);
11100 if (shareable == rb_parser_shareable_literal &&
11101 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11102 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
11104 *shareable_literal_p = 1;
11107 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
11109 *shareable_literal_p = 0;
11115 if (nd_type(node) == NODE_LIST) {
11116 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
11118 else if (nd_type(node) == NODE_HASH) {
11119 long len = RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
11125 *shareable_literal_p = 0;
11126 ADD_SEQ(ret, anchor);
11132 if (nd_type(node) == NODE_LIST) {
11133 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
11135 else if (nd_type(node) == NODE_HASH) {
11136 long len = RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
11141 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
11143 *shareable_literal_p = 1;
11147 ADD_INSN1(ret, node, putobject, val);
11150 *shareable_literal_p = 1;
11157compile_shareable_constant_value(rb_iseq_t *iseq, LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
11161 DECL_ANCHOR(anchor);
11162 INIT_ANCHOR(anchor);
11164 switch (shareable) {
11165 case rb_parser_shareable_none:
11166 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
11169 case rb_parser_shareable_literal:
11170 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (NODE *)lhs, value, 0, &val, &literal_p));
11171 ADD_SEQ(ret, anchor);
11174 case rb_parser_shareable_copy:
11175 case rb_parser_shareable_everything:
11176 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (NODE *)lhs, value, 0, &val, &literal_p));
11178 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
11181 ADD_SEQ(ret, anchor);
11185 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
11189static int iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped);
11198iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *node,
int popped)
11202 debugs(
"node: NODE_NIL(implicit)\n");
11203 ADD_SYNTHETIC_INSN(ret, iseq_synthetic_lineno(iseq), -1, putnil);
11207 return iseq_compile_each0(iseq, ret, node, popped);
11211iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
11213 const int line = (int)nd_line(node);
11214 const enum node_type
type = nd_type(node);
11217 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
11221 if (nd_fl_newline(node)) {
11223 ISEQ_COMPILE_DATA(iseq)->last_line = line;
11224 if (line > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
11225 event |= RUBY_EVENT_COVERAGE_LINE;
11227 ADD_TRACE(ret, event);
11231 debug_node_start(node);
11232#undef BEFORE_RETURN
11233#define BEFORE_RETURN debug_node_end()
11237 CHECK(compile_block(iseq, ret, node, popped));
11241 CHECK(compile_if(iseq, ret, node, popped,
type));
11244 CHECK(compile_case(iseq, ret, node, popped));
11247 CHECK(compile_case2(iseq, ret, node, popped));
11250 CHECK(compile_case3(iseq, ret, node, popped));
11254 CHECK(compile_loop(iseq, ret, node, popped,
type));
11258 CHECK(compile_iter(iseq, ret, node, popped));
11260 case NODE_FOR_MASGN:
11261 CHECK(compile_for_masgn(iseq, ret, node, popped));
11264 CHECK(compile_break(iseq, ret, node, popped));
11267 CHECK(compile_next(iseq, ret, node, popped));
11270 CHECK(compile_redo(iseq, ret, node, popped));
11273 CHECK(compile_retry(iseq, ret, node, popped));
11276 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
11280 CHECK(compile_rescue(iseq, ret, node, popped));
11283 CHECK(compile_resbody(iseq, ret, node, popped));
11286 CHECK(compile_ensure(iseq, ret, node, popped));
11291 LABEL *end_label = NEW_LABEL(line);
11292 const NODE *cond = node;
11299 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(cond)->nd_1st));
11301 ADD_INSN(ret, cond, dup);
11303 if (
type == NODE_AND) {
11304 ADD_INSNL(ret, cond, branchunless, end_label);
11307 ADD_INSNL(ret, cond, branchif, end_label);
11310 ADD_INSN(ret, cond, pop);
11312 const NODE *rest = RNODE_OR(cond)->nd_2nd;
11313 if (nd_type_p(rest,
type)) {
11317 CHECK(COMPILE_(ret,
"nd_2nd", rest, popped));
11320 ADD_LABEL(ret, end_label);
11325 compile_massign(iseq, ret, node, popped);
11330 ID id = RNODE_LASGN(node)->nd_vid;
11331 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
11333 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
11334 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
11337 ADD_INSN(ret, node, dup);
11339 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
11344 ID id = RNODE_DASGN(node)->nd_vid;
11345 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
11346 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
11349 ADD_INSN(ret, node, dup);
11352 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
11355 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
11359 ADD_SETLOCAL(ret, node, ls - idx, lv);
11363 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
11366 ADD_INSN(ret, node, dup);
11368 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
11372 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
11374 ADD_INSN(ret, node, dup);
11376 ADD_INSN2(ret, node, setinstancevariable,
11377 ID2SYM(RNODE_IASGN(node)->nd_vid),
11378 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
11382 if (RNODE_CDECL(node)->nd_vid) {
11383 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11386 ADD_INSN(ret, node, dup);
11389 ADD_INSN1(ret, node, putspecialobject,
11390 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
11391 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
11394 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
11395 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11396 ADD_INSN(ret, node, swap);
11399 ADD_INSN1(ret, node, topn,
INT2FIX(1));
11400 ADD_INSN(ret, node, swap);
11403 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
11408 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
11410 ADD_INSN(ret, node, dup);
11412 ADD_INSN2(ret, node, setclassvariable,
11413 ID2SYM(RNODE_CVASGN(node)->nd_vid),
11414 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
11417 case NODE_OP_ASGN1:
11418 CHECK(compile_op_asgn1(iseq, ret, node, popped));
11420 case NODE_OP_ASGN2:
11421 CHECK(compile_op_asgn2(iseq, ret, node, popped));
11423 case NODE_OP_CDECL:
11424 CHECK(compile_op_cdecl(iseq, ret, node, popped));
11426 case NODE_OP_ASGN_AND:
11427 case NODE_OP_ASGN_OR:
11428 CHECK(compile_op_log(iseq, ret, node, popped,
type));
11432 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
11438 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
11444 CHECK(compile_super(iseq, ret, node, popped,
type));
11447 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
11452 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
11457 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
11460 CHECK(compile_return(iseq, ret, node, popped));
11463 CHECK(compile_yield(iseq, ret, node, popped));
11467 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
11473 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
11475 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
11477 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
11478 rb_id2str(RNODE_DVAR(node)->nd_vid));
11481 ADD_GETLOCAL(ret, node, ls - idx, lv);
11486 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
11488 ADD_INSN(ret, node, pop);
11493 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
11495 ADD_INSN2(ret, node, getinstancevariable,
11496 ID2SYM(RNODE_IVAR(node)->nd_vid),
11497 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
11502 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
11504 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
11506 rb_const_path_t const_path = rb_const_path_from_ids(
false, &RNODE_CONST(node)->nd_vid, 1);
11507 ADD_INSN1(ret, node, opt_getconstant_path, (
VALUE)const_path);
11510 ADD_INSN(ret, node, putnil);
11511 ADD_INSN1(ret, node, putobject,
Qtrue);
11512 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
11516 ADD_INSN(ret, node, pop);
11522 ADD_INSN2(ret, node, getclassvariable,
11523 ID2SYM(RNODE_CVAR(node)->nd_vid),
11524 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
11528 case NODE_NTH_REF:{
11530 if (!RNODE_NTH_REF(node)->nd_nth) {
11531 ADD_INSN(ret, node, putnil);
11534 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11535 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
11539 case NODE_BACK_REF:{
11541 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11542 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
11549 CHECK(compile_match(iseq, ret, node, popped,
type));
11553 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
11559 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
11563 case NODE_ENCODING:{
11565 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
11569 case NODE_INTEGER:{
11570 VALUE lit = rb_node_integer_literal_val(node);
11572 debugp_param(
"integer", lit);
11574 ADD_INSN1(ret, node, putobject, lit);
11580 VALUE lit = rb_node_float_literal_val(node);
11582 debugp_param(
"float", lit);
11584 ADD_INSN1(ret, node, putobject, lit);
11589 case NODE_RATIONAL:{
11590 VALUE lit = rb_node_rational_literal_val(node);
11592 debugp_param(
"rational", lit);
11594 ADD_INSN1(ret, node, putobject, lit);
11599 case NODE_IMAGINARY:{
11600 VALUE lit = rb_node_imaginary_literal_val(node);
11602 debugp_param(
"imaginary", lit);
11604 ADD_INSN1(ret, node, putobject, lit);
11611 compile_file_path(iseq, ret, node, ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal);
11615 debugp_param(
"nd_lit", get_string_value(node));
11617 VALUE lit = get_string_value(node);
11618 const rb_compile_option_t *option = ISEQ_COMPILE_DATA(iseq)->option;
11620 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11621 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11624 switch (option->frozen_string_literal) {
11625 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11626 ADD_INSN1(ret, node, dupchilledstring, lit);
11628 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11629 ADD_INSN1(ret, node, dupstring, lit);
11631 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11632 ADD_INSN1(ret, node, putobject, lit);
11635 rb_bug(
"invalid frozen_string_literal");
11642 compile_dstr(iseq, ret, node);
11645 ADD_INSN(ret, node, pop);
11650 ADD_CALL_RECEIVER(ret, node);
11651 VALUE str = rb_node_str_string_val(node);
11652 ADD_INSN1(ret, node, putobject, str);
11654 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11657 ADD_INSN(ret, node, pop);
11662 ADD_CALL_RECEIVER(ret, node);
11663 compile_dstr(iseq, ret, node);
11664 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11667 ADD_INSN(ret, node, pop);
11672 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11676 VALUE lit = rb_node_regx_string_val(node);
11678 ADD_INSN1(ret, node, putobject, lit);
11684 compile_dregx(iseq, ret, node, popped);
11687 const rb_iseq_t *block_iseq;
11688 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11690 VALUE once_storage = rb_imemo_once_storage_new();
11691 ADD_INSN2(ret, node, once, block_iseq, once_storage);
11696 ADD_INSN(ret, node, pop);
11700 case NODE_ARGSCAT:{
11702 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11703 ADD_INSN1(ret, node, splatarray,
Qfalse);
11704 ADD_INSN(ret, node, pop);
11705 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11706 ADD_INSN1(ret, node, splatarray,
Qfalse);
11707 ADD_INSN(ret, node, pop);
11710 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11711 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11712 if (nd_type_p(body_node, NODE_LIST)) {
11713 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11716 CHECK(COMPILE(ret,
"argscat body", body_node));
11717 ADD_INSN(ret, node, concattoarray);
11722 case NODE_ARGSPUSH:{
11724 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11725 ADD_INSN1(ret, node, splatarray,
Qfalse);
11726 ADD_INSN(ret, node, pop);
11727 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11730 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11731 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11732 if (keyword_node_p(body_node)) {
11733 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11734 ADD_INSN(ret, node, pushtoarraykwsplat);
11736 else if (static_literal_node_p(body_node, iseq,
false)) {
11737 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11738 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11741 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11742 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11748 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11749 ADD_INSN1(ret, node, splatarray,
Qtrue);
11752 ADD_INSN(ret, node, pop);
11757 ID mid = RNODE_DEFN(node)->nd_mid;
11758 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11760 ISEQ_TYPE_METHOD, line);
11762 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11763 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11767 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11773 ID mid = RNODE_DEFS(node)->nd_mid;
11774 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11776 ISEQ_TYPE_METHOD, line);
11778 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11779 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11780 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11784 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11789 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11790 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11791 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11792 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11793 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11796 ADD_INSN(ret, node, pop);
11801 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11802 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11803 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11804 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11807 ADD_INSN(ret, node, pop);
11812 const rb_parser_ary_t *ary = RNODE_UNDEF(node)->nd_undefs;
11814 for (
long i = 0; i < ary->len; i++) {
11815 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11816 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11817 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11818 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11820 if (i < ary->
len - 1) {
11821 ADD_INSN(ret, node, pop);
11826 ADD_INSN(ret, node, pop);
11831 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11832 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
11833 ISEQ_TYPE_CLASS, line);
11834 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11835 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11836 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11838 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11839 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11843 ADD_INSN(ret, node, pop);
11848 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11849 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
11850 ISEQ_TYPE_CLASS, line);
11851 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11852 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11854 ADD_INSN (ret, node, putnil);
11855 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11859 ADD_INSN(ret, node, pop);
11865 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11866 ISEQ_TYPE_CLASS, line);
11868 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11869 ADD_INSN (ret, node, putnil);
11870 CONST_ID(singletonclass,
"singletonclass");
11874 int sclass_flags = VM_DEFINECLASS_TYPE_SINGLETON_CLASS;
11875 const NODE *recv = RNODE_SCLASS(node)->nd_recv;
11876 if (!(nd_type_p(recv, NODE_SELF) &&
11877 ISEQ_BODY(iseq)->
type == ISEQ_TYPE_CLASS) &&
11878 !cpath_const_p(recv)) {
11879 sclass_flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
11882 ADD_INSN3(ret, node, defineclass,
11883 ID2SYM(singletonclass), singleton_class,
11888 ADD_INSN(ret, node, pop);
11893 CHECK(compile_colon2(iseq, ret, node, popped));
11896 CHECK(compile_colon3(iseq, ret, node, popped));
11899 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11902 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11906 LABEL *lend = NEW_LABEL(line);
11907 LABEL *ltrue = NEW_LABEL(line);
11908 LABEL *lfalse = NEW_LABEL(line);
11909 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11911 ADD_LABEL(ret, ltrue);
11912 ADD_INSN1(ret, node, putobject,
Qtrue);
11913 ADD_INSNL(ret, node, jump, lend);
11914 ADD_LABEL(ret, lfalse);
11915 ADD_INSN1(ret, node, putobject,
Qfalse);
11916 ADD_LABEL(ret, lend);
11921 ADD_INSN(ret, node, putself);
11927 ADD_INSN(ret, node, putnil);
11933 ADD_INSN1(ret, node, putobject,
Qtrue);
11939 ADD_INSN1(ret, node, putobject,
Qfalse);
11944 CHECK(compile_errinfo(iseq, ret, node, popped));
11948 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11951 case NODE_POSTEXE:{
11956 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11957 const rb_iseq_t *once_iseq =
11958 NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc, rb_fstring(make_name_for_block(iseq)), ISEQ_TYPE_BLOCK, line);
11960 VALUE once_storage = rb_imemo_once_storage_new();
11961 ADD_INSN2(ret, node, once, once_iseq, once_storage);
11966 ADD_INSN(ret, node, pop);
11971 CHECK(compile_kw_arg(iseq, ret, node, popped));
11974 compile_dstr(iseq, ret, node);
11976 ADD_INSN(ret, node, intern);
11979 ADD_INSN(ret, node, pop);
11983 case NODE_ATTRASGN:
11984 CHECK(compile_attrasgn(iseq, ret, node, popped));
11988 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11991 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11992 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11996 ADD_INSN(ret, node, pop);
12001 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
12016insn_data_length(INSN *iobj)
12018 return insn_len(iobj->insn_id);
12022calc_sp_depth(
int depth, INSN *insn)
12024 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
12028opobj_inspect(
VALUE obj)
12048insn_data_to_s_detail(INSN *iobj)
12050 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
12052 if (iobj->operands) {
12053 const char *types = insn_op_types(iobj->insn_id);
12056 for (j = 0; types[j]; j++) {
12057 char type = types[j];
12062 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, j);
12063 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
12069 rb_iseq_t *iseq = (rb_iseq_t *)OPERAND_AT(iobj, j);
12081 VALUE v = OPERAND_AT(iobj, j);
12093 rb_str_concat(str, opobj_inspect(rb_const_path_to_array((rb_const_path_t)OPERAND_AT(iobj, j))));
12096 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
12099 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
12105 if (vm_ci_mid(ci))
rb_str_append(str, rb_id2str(vm_ci_mid(ci)));
12106 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
12114 void *func = (
void *)OPERAND_AT(iobj, j);
12117 if (dladdr(func, &info) && info.dli_sname) {
12122 rb_str_catf(str,
"<%p>", func);
12132 if (types[j + 1]) {
12141dump_disasm_list(
const LINK_ELEMENT *link)
12143 dump_disasm_list_with_cursor(link, NULL, NULL);
12147dump_disasm_list_with_cursor(
const LINK_ELEMENT *link,
const LINK_ELEMENT *curr,
const LABEL *dest)
12154 printf(
"-- raw disasm--------\n");
12157 if (curr) printf(curr == link ?
"*" :
" ");
12158 switch (link->type) {
12159 case ISEQ_ELEMENT_INSN:
12161 iobj = (INSN *)link;
12162 str = insn_data_to_s_detail(iobj);
12163 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
12164 pos += insn_data_length(iobj);
12167 case ISEQ_ELEMENT_LABEL:
12169 lobj = (LABEL *)link;
12170 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
12171 dest == lobj ?
" <---" :
"");
12174 case ISEQ_ELEMENT_TRACE:
12176 TRACE *trace = (TRACE *)link;
12177 printf(
" trace: %0x\n", trace->event);
12180 case ISEQ_ELEMENT_ADJUST:
12182 ADJUST *adjust = (ADJUST *)link;
12183 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
12188 rb_raise(
rb_eSyntaxError,
"dump_disasm_list error: %d\n", (
int)link->type);
12192 printf(
"---------------------\n");
12197rb_insn_len(
VALUE insn)
12199 return insn_len(insn);
12203rb_insns_name(
int i)
12205 return insn_name(i);
12209rb_insns_name_array(
void)
12213 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
12220register_label(rb_iseq_t *iseq,
struct st_table *labels_table,
VALUE obj)
12224 obj = rb_to_symbol_type(obj);
12226 if (st_lookup(labels_table, obj, &tmp) == 0) {
12227 label = NEW_LABEL(0);
12228 st_insert(labels_table, obj, (st_data_t)label);
12231 label = (LABEL *)tmp;
12238get_exception_sym2type(
VALUE sym)
12240 static VALUE symRescue, symEnsure, symRetry;
12241 static VALUE symBreak, symRedo, symNext;
12243 if (symRescue == 0) {
12252 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
12253 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
12254 if (sym == symRetry)
return CATCH_TYPE_RETRY;
12255 if (sym == symBreak)
return CATCH_TYPE_BREAK;
12256 if (sym == symRedo)
return CATCH_TYPE_REDO;
12257 if (sym == symNext)
return CATCH_TYPE_NEXT;
12258 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
12263iseq_build_from_ary_exception(rb_iseq_t *iseq,
struct st_table *labels_table,
12269 const rb_iseq_t *eiseq;
12271 LABEL *lstart, *lend, *lcont;
12286 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
12287 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
12288 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
12292 if (
type == CATCH_TYPE_RESCUE ||
12293 type == CATCH_TYPE_BREAK ||
12294 type == CATCH_TYPE_NEXT) {
12300 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
12307static struct st_table *
12308insn_make_insn_table(
void)
12310 struct st_table *table;
12312 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
12314 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
12321static const rb_iseq_t *
12322iseq_build_load_iseq(
const rb_iseq_t *iseq,
VALUE op)
12325 const rb_iseq_t *loaded_iseq;
12328 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
12330 else if (
CLASS_OF(op) == rb_cISeq) {
12337 loaded_iseq = rb_iseqw_to_iseq(iseqw);
12338 return loaded_iseq;
12342iseq_build_callinfo_from_hash(rb_iseq_t *iseq,
VALUE op)
12346 unsigned int flag = 0;
12357 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
12359 if (!
NIL_P(vkw_arg)) {
12362 size_t n = rb_callinfo_kwarg_bytes(
len);
12365 kw_arg->references = 0;
12366 kw_arg->keyword_len =
len;
12367 for (i = 0; i <
len; i++) {
12370 kw_arg->keywords[i] = kw;
12375 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
12380static rb_iseq_events_t
12381event_name_to_flag(
VALUE sym)
12383#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
12397iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor,
12404 int line_no = 0, node_id = -1, insn_idx = 0;
12405 int ret = COMPILE_OK;
12410 static struct st_table *insn_table;
12412 if (insn_table == 0) {
12413 insn_table = insn_make_insn_table();
12416 for (i=0; i<
len; i++) {
12420 rb_iseq_events_t event;
12422 ADD_TRACE(anchor, event);
12425 LABEL *label = register_label(iseq, labels_table, obj);
12426 ADD_LABEL(anchor, label);
12443 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
12445 COMPILE_ERROR(iseq, line_no,
12446 "unknown instruction: %+"PRIsVALUE, insn);
12451 if (argc != insn_len((
VALUE)insn_id)-1) {
12452 COMPILE_ERROR(iseq, line_no,
12453 "operand size mismatch");
12459 argv = compile_data_calloc2_type(iseq,
VALUE, argc);
12463 (LINK_ELEMENT*)new_insn_core(iseq, line_no, node_id,
12464 (
enum ruby_vminsn_type)insn_id, argc, argv));
12466 for (j=0; j<argc; j++) {
12468 switch (insn_op_type((
VALUE)insn_id, j)) {
12470 LABEL *label = register_label(iseq, labels_table, op);
12471 argv[j] = (
VALUE)label;
12480 if (insn_id == BIN(once)) op = rb_imemo_once_storage_new();
12487 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
12499 op = rb_to_array_type(op);
12503 sym = rb_to_symbol_type(sym);
12507 rb_const_path_t const_path = rb_const_path_from_array(segments);
12508 argv[j] = (
VALUE)const_path;
12511 ISEQ_BODY(iseq)->ic_size++;
12516 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
12517 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
12522 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
12523 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
12527 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
12530 argv[j] = rb_to_symbol_type(op);
12537 op = rb_to_array_type(op);
12542 register_label(iseq, labels_table, sym);
12543 cdhash_aset(map, key, (
VALUE)label);
12553#if SIZEOF_VALUE <= SIZEOF_LONG
12558 argv[j] = (
VALUE)funcptr;
12568 (LINK_ELEMENT*)new_insn_core(iseq, line_no, node_id,
12569 (
enum ruby_vminsn_type)insn_id, argc, NULL));
12573 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
12578 validate_labels(iseq, labels_table);
12579 if (!ret)
return ret;
12580 return iseq_setup(iseq, anchor);
12583#define CHECK_ARRAY(v) rb_to_array_type(v)
12584#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
12589 VALUE val = rb_hash_aref(param, sym);
12594 else if (!
NIL_P(val)) {
12595 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
12601static const struct rb_iseq_param_keyword *
12602iseq_build_kw(rb_iseq_t *iseq,
VALUE params,
VALUE keywords)
12607 VALUE key, sym, default_val;
12610 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12612 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12614 keyword->num =
len;
12615#define SYM(s) ID2SYM(rb_intern_const(#s))
12616 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12617 i = keyword->bits_start - keyword->num;
12618 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12622 for (i = 0; i <
len; i++) {
12626 goto default_values;
12629 keyword->required_num++;
12633 default_len =
len - i;
12634 if (default_len == 0) {
12635 keyword->table = ids;
12638 else if (default_len < 0) {
12644 for (j = 0; i <
len; i++, j++) {
12658 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
12664 keyword->table = ids;
12665 keyword->default_values = dvs;
12671iseq_insn_each_object_mark_and_move(
VALUE * obj,
VALUE _)
12673 rb_gc_mark_and_move(obj);
12680 size_t size =
sizeof(INSN);
12681 size_t align = ALIGNMENT_SIZE_OF(INSN);
12682 unsigned int pos = 0;
12685 size_t padding = calc_padding((
void *)&storage->buff[pos], align);
12686 size_t offset = pos + size + padding;
12687 if (offset > storage->size || offset > storage->pos) {
12689 storage = storage->next;
12692 pos += (int)padding;
12694 iobj = (INSN *)&storage->buff[pos];
12696 if (iobj->operands) {
12697 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_move, (
VALUE)0);
12705 .wrap_struct_name =
"compiler/labels_wrapper",
12710 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
12714rb_iseq_build_from_ary(rb_iseq_t *iseq,
VALUE misc,
VALUE locals,
VALUE params,
12717#define SYM(s) ID2SYM(rb_intern_const(#s))
12719 unsigned int arg_size, local_size, stack_max;
12721 struct st_table *labels_table = st_init_numtable();
12723 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
12724 VALUE keywords = rb_hash_aref(params, SYM(keyword));
12726 DECL_ANCHOR(anchor);
12727 INIT_ANCHOR(anchor);
12730 ISEQ_BODY(iseq)->local_table_size =
len;
12731 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12733 for (i = 0; i <
len; i++) {
12736 if (sym_arg_rest == lv) {
12744#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12745 if (INT_PARAM(lead_num)) {
12746 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12748 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12749 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12751 if (
Qtrue == rb_hash_aref(params, SYM(rest))) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12753#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12755 INT_PARAM(arg_size);
12756 INT_PARAM(local_size);
12757 INT_PARAM(stack_max);
12761 VALUE source_hash = rb_hash_aref(misc,
ID2SYM(rb_intern(
"source_hash")));
12762 if (!
NIL_P(source_hash)) {
12763 ISEQ_BODY(iseq)->source_hash =
NUM2ULL(source_hash);
12767 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
12774 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12776 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12779 for (i = 0; i <
len; i++) {
12781 LABEL *label = register_label(iseq, labels_table, ent);
12782 opt_table[i] = (
VALUE)label;
12785 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12786 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12789 else if (!
NIL_P(arg_opt_labels)) {
12790 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
12795 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12797 else if (!
NIL_P(keywords)) {
12798 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
12802 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
12803 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12806 if (
Qtrue == rb_hash_aref(params, SYM(use_block))) {
12807 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12810 if (int_param(&i, params, SYM(kwrest))) {
12811 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12812 if (keyword == NULL) {
12813 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12815 keyword->rest_start = i;
12816 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12819 iseq_calc_param_size(iseq);
12822 iseq_build_from_ary_exception(iseq, labels_table, exception);
12825 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12827 ISEQ_BODY(iseq)->param.size = arg_size;
12828 ISEQ_BODY(iseq)->local_table_size = local_size;
12829 ISEQ_BODY(iseq)->stack_max = stack_max;
12835rb_dvar_defined(
ID id,
const rb_iseq_t *iseq)
12839 while (body->type == ISEQ_TYPE_BLOCK ||
12840 body->type == ISEQ_TYPE_RESCUE ||
12841 body->type == ISEQ_TYPE_ENSURE ||
12842 body->type == ISEQ_TYPE_EVAL ||
12843 body->type == ISEQ_TYPE_MAIN
12847 for (i = 0; i < body->local_table_size; i++) {
12848 if (body->local_table[i] ==
id) {
12852 iseq = body->parent_iseq;
12853 body = ISEQ_BODY(iseq);
12860rb_local_defined(
ID id,
const rb_iseq_t *iseq)
12866 for (i=0; i<body->local_table_size; i++) {
12867 if (body->local_table[i] ==
id) {
12877#ifndef IBF_ISEQ_DEBUG
12878#define IBF_ISEQ_DEBUG 0
12881#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12882#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12885typedef uint32_t ibf_offset_t;
12886#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12888#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12890#define IBF_DEVEL_VERSION 22
12891#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12893#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12896static const char IBF_ENDIAN_MARK =
12897#ifdef WORDS_BIGENDIAN
12906 uint32_t major_version;
12907 uint32_t minor_version;
12909 uint32_t extra_size;
12911 uint32_t iseq_list_size;
12912 uint32_t global_object_list_size;
12913 ibf_offset_t iseq_list_offset;
12914 ibf_offset_t global_object_list_offset;
12915 uint32_t source_hash_hi;
12916 uint32_t source_hash_lo;
12937 unsigned int obj_list_size;
12938 ibf_offset_t obj_list_offset;
12939 ibf_offset_t *obj_offsets;
12949 VALUE path_override;
12950 VALUE realpath_override;
12952 VALUE pathobj_index;
12962pinned_list_mark(
void *ptr)
12966 for (i = 0; i < list->size; i++) {
12967 if (list->buffer[i]) {
12968 rb_gc_mark(list->buffer[i]);
12980 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
12984pinned_list_fetch(
VALUE list,
long offset)
12990 if (offset < 0 || offset >= ptr->size) {
12991 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12994 return ptr->buffer[offset];
12998pinned_list_store(
VALUE list,
long offset,
VALUE object)
13004 if (offset < 0 || offset >= ptr->size) {
13005 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
13012pinned_list_new(
long size)
13014 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
13015 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
13016 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
13022ibf_dump_pos(
struct ibf_dump *dump)
13024 rb_len_t pos = RSTRING_LEN(dump->current_buffer->str);
13025#if SIZEOF_RB_LEN_T > SIZEOF_INT
13026 if (pos >= UINT_MAX) {
13030 return (
unsigned int)pos;
13034ibf_dump_align(
struct ibf_dump *dump,
size_t align)
13036 ibf_offset_t pos = ibf_dump_pos(dump);
13038 static const char padding[
sizeof(
VALUE)];
13039 size_t size = align - ((size_t)pos % align);
13040#if SIZEOF_RB_LEN_T > SIZEOF_INT
13041 if (pos + size >= UINT_MAX) {
13045 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
13046 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
13048 rb_str_cat(dump->current_buffer->str, padding, size);
13053ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
size_t size)
13055 ibf_offset_t pos = ibf_dump_pos(dump);
13056#if SIZEOF_RB_LEN_T > SIZEOF_INT
13058 if (size >= UINT_MAX || pos + size >= UINT_MAX) {
13062 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
13067ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
13069 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
13073ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
rb_len_t offset)
13075 VALUE str = dump->current_buffer->str;
13076 char *ptr = RSTRING_PTR(str);
13078 rb_bug(
"ibf_dump_overwrite: overflow");
13079 memcpy(ptr + offset, buff, size);
13083ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
13085 ibf_offset_t beg = *offset;
13087 return load->current_buffer->buff + beg;
13091ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
13093 void *buff = ruby_xmalloc2(x, y);
13094 size_t size = x * y;
13095 memcpy(buff, load->current_buffer->buff + offset, size);
13099#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
13101#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
13102#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
13103#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
13104#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
13105#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
13108ibf_table_lookup(
struct st_table *table, st_data_t key)
13112 if (st_lookup(table, key, &val)) {
13121ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
13123 int index = ibf_table_lookup(table, key);
13126 index = (int)table->num_entries;
13127 st_insert(table, key, (st_data_t)index);
13135static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
13138static rb_iseq_t *ibf_load_iseq(
const struct ibf_load *load,
const rb_iseq_t *index_iseq);
13141ibf_dump_object_table_new(
void)
13143 st_table *obj_table = st_init_numtable();
13144 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
13152 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
13158 if (
id == 0 || rb_id2name(
id) == NULL) {
13161 return ibf_dump_object(dump, rb_id2sym(
id));
13165ibf_load_id(
const struct ibf_load *load,
const ID id_index)
13167 if (id_index == 0) {
13170 VALUE sym = ibf_load_object(load, id_index);
13180static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
13183ibf_dump_iseq(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13185 if (iseq == NULL) {
13189 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
13193static unsigned char
13194ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
13196 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
13197 return (
unsigned char)load->current_buffer->buff[(*offset)++];
13213 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
13214 ibf_dump_write(dump, &x,
sizeof(
VALUE));
13218 enum { max_byte_length =
sizeof(
VALUE) + 1 };
13220 unsigned char bytes[max_byte_length];
13223 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
13224 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
13230 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
13233 ibf_dump_write(dump, bytes + max_byte_length - n, n);
13237ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
13239 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
13240 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
13242 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
13243 *offset +=
sizeof(
VALUE);
13248 enum { max_byte_length =
sizeof(
VALUE) + 1 };
13250 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
13251 const unsigned char c = buffer[*offset];
13255 c == 0 ? 9 : ntz_int32(c) + 1;
13258 if (*offset + n > load->current_buffer->size) {
13263 for (i = 1; i < n; i++) {
13265 x |= (
VALUE)buffer[*offset + i];
13275ibf_dump_write_small_value_nullable(
struct ibf_dump *dump,
VALUE x)
13277 ibf_dump_write_small_value(dump, x + 1);
13281ibf_load_small_value_nullable(
const struct ibf_load *load, ibf_offset_t *offset)
13283 return ibf_load_small_value(load, offset) - 1;
13287ibf_dump_write_small_int32(
struct ibf_dump *dump, int32_t x)
13289 ibf_dump_write_small_value(dump, ((uint32_t)x << 1) ^ (x < 0 ? 0xffffffff : 0));
13293ibf_load_small_int32(
const struct ibf_load *load, ibf_offset_t *offset)
13295 VALUE x = ibf_load_small_value(load, offset);
13296 return (int32_t)((x >> 1) ^ (x & 1 ? 0xffffffff : 0));
13306 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
13308 size_t len = strlen(bf->name);
13309 ibf_dump_write_small_value(dump, (
VALUE)
len);
13310 ibf_dump_write(dump, bf->name,
len);
13314ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
13316 int i = (int)ibf_load_small_value(load, offset);
13317 int len = (int)ibf_load_small_value(load, offset);
13318 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
13321 fprintf(stderr,
"%.*s!!\n",
len, name);
13325 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
13326 if (strncmp(table[i].name, name,
len) != 0) {
13327 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %.*s but %s)",
13328 i,
len, name, table[i].name);
13336ibf_dump_insn_header(
struct ibf_dump *dump,
const rb_iseq_t *iseq,
unsigned int pos,
VALUE insn,
int prev_line)
13339 bool changed = info.events || info.line_no != prev_line;
13343 ibf_dump_write_small_value(dump, (insn << 1) | changed);
13346 if (info.line_no == prev_line) {
13347 ibf_dump_write_small_value(dump, info.events << 1);
13350 ibf_dump_write_small_value(dump, (info.events << 1) | 1);
13351 ibf_dump_write_small_int32(dump, info.line_no - prev_line);
13354 return info.line_no;
13358ibf_dump_code(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13361 const int iseq_size = body->iseq_size;
13363 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
13364 int prev_line = body->location.first_lineno;
13366 ibf_offset_t offset = ibf_dump_pos(dump);
13368 for (code_index=0; code_index<iseq_size;) {
13369 const VALUE insn = orig_code[code_index];
13370 const char *types = insn_op_types(insn);
13374 prev_line = ibf_dump_insn_header(dump, iseq, code_index, insn, prev_line);
13378 for (op_index=0; types[op_index]; op_index++, code_index++) {
13379 VALUE op = orig_code[code_index];
13382 switch (types[op_index]) {
13385 wv = ibf_dump_object(dump, op);
13388 ibf_dump_write_small_value_nullable(dump, (
VALUE)ibf_dump_iseq(dump, (
const rb_iseq_t *)op));
13393 wv = ISEQ_IS_ENTRY_INDEX(body, types[op_index], (
const void *)op);
13400 wv = ibf_dump_id(dump, (
ID)op);
13412 ibf_dump_write_small_value(dump, wv);
13421static rb_const_path_t
13422ibf_load_const_path(
const struct ibf_load *load, ibf_offset_t *reading_pos)
13424 size_t header = (size_t)ibf_load_small_value(load, reading_pos);
13425 bool absolute = header & 1;
13426 size_t size = header >> 1;
13433 for (
size_t i = 0; i < size; i++) {
13434 ids[i] = ibf_load_id(load, (
ID)ibf_load_small_value(load, reading_pos));
13435 if (!is_notop_id(ids[i]) || !is_const_id(ids[i])) {
13440 rb_const_path_t const_path = rb_const_path_from_ids(absolute, ids, size);
13448 unsigned int *positions;
13454ibf_load_insn_header(
const struct ibf_load *load, ibf_offset_t *reading_pos,
unsigned int pos,
13457 rb_iseq_events_t events = 0;
13458 VALUE opcode = ibf_load_small_value(load, reading_pos);
13459 bool has_events = opcode & 1;
13463 VALUE header = ibf_load_small_value(load, reading_pos);
13464 bool has_line = header & 1;
13465 events = (rb_iseq_events_t)(header >> 1);
13467 info->line += ibf_load_small_int32(load, reading_pos);
13472 if (!last || last->line_no != info->line || last->events != events) {
13473 info->entries[info->size].line_no = info->line;
13474 info->entries[info->size].events = events;
13475 info->positions[info->size] = pos;
13485 ibf_offset_t reading_pos = body_offset;
13486 const unsigned int node_ids_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13487 if (node_ids_size == 0 || node_ids_size > load->current_buffer->size - reading_pos) {
13490 const uint8_t *node_ids = (
const uint8_t *)load->current_buffer->buff + reading_pos;
13492 rb_iseq_insn_info_setup(body, info->entries, info->positions, info->size, node_ids, node_ids_size);
13496ibf_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,
13497 ibf_offset_t insns_info_body_offset)
13500 unsigned int code_index;
13501 ibf_offset_t reading_pos = bytecode_offset;
13505 struct rb_call_data *cd_entries = load_body->call_data;
13507 load_body->iseq_encoded = code;
13508 load_body->iseq_size = iseq_size;
13510 iseq_bits_t * mark_offset_bits;
13511 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
13512 load_body->mark_bits.single = 0;
13513 mark_offset_bits = &load_body->mark_bits.single;
13516 load_body->mark_bits.list =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
13517 mark_offset_bits = load_body->mark_bits.list;
13519 bool needs_bitmap =
false;
13521 VALUE entries_buf, positions_buf;
13524 .positions =
ALLOCV_N(
unsigned int, positions_buf, iseq_size),
13525 .line = load_body->location.first_lineno,
13528 for (code_index=0; code_index<iseq_size;) {
13529 const VALUE insn = code[code_index] = ibf_load_insn_header(load, &reading_pos, code_index, &insns_info);
13530 const char *types = insn_op_types(insn);
13536 for (op_index=0; types[op_index]; op_index++, code_index++) {
13537 const char operand_type = types[op_index];
13538 switch (operand_type) {
13541 VALUE op = ibf_load_small_value(load, &reading_pos);
13542 VALUE v = ibf_load_object(load, op);
13543 code[code_index] = v;
13546 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13547 needs_bitmap =
true;
13553 VALUE op = ibf_load_small_value(load, &reading_pos);
13554 VALUE v = ibf_load_object(load, op);
13559 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
13561 code[code_index] = v;
13562 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13564 needs_bitmap =
true;
13569 VALUE op = ibf_load_small_value_nullable(load, &reading_pos);
13570 VALUE v = (
VALUE)ibf_load_iseq(load, (
const rb_iseq_t *)op);
13571 code[code_index] = v;
13574 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13575 needs_bitmap =
true;
13583 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13585 void *entry = ISEQ_IS_ENTRY(load_body, operand_type, op);
13586 code[code_index] = (
VALUE)entry;
13588 if (operand_type == TS_IVC) {
13591 if (insn == BIN(setinstancevariable)) {
13592 cache->value = IVAR_CACHE_INIT;
13595 cache->value = rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET);
13603 code[code_index] = (
VALUE)cd_entries++;
13608 VALUE op = ibf_load_small_value(load, &reading_pos);
13609 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
13616 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
13619 code[code_index] = ibf_load_small_value(load, &reading_pos);
13623 if (insn_len(insn) != op_index+1) {
13628 if (!needs_bitmap) {
13629 SIZED_FREE_N(load_body->mark_bits.list, ISEQ_MBITS_BUFLEN(iseq_size));
13630 load_body->mark_bits.list = NULL;
13634 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
13636 ibf_load_insns_info(load, load_body, insns_info_body_offset, &insns_info);
13644ibf_dump_param_opt_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13646 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
13649 IBF_W_ALIGN(
VALUE);
13650 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
13653 return ibf_dump_pos(dump);
13658ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
13662 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
13671ibf_dump_param_keyword(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13673 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
13676 struct rb_iseq_param_keyword dump_kw = *kw;
13677 int dv_num = kw->num - kw->required_num;
13682 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
13683 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
13685 dump_kw.table = IBF_W(ids,
ID, kw->num);
13686 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
13687 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
13688 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
13695static const struct rb_iseq_param_keyword *
13696ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
13698 if (param_keyword_offset) {
13699 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
13700 int dv_num = kw->num - kw->required_num;
13701 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
13704 for (i=0; i<dv_num; i++) {
13705 dvs[i] = ibf_load_object(load, dvs[i]);
13711 kw->default_values = dvs;
13720ibf_dump_insns_info_body(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13722 ibf_offset_t offset = ibf_dump_pos(dump);
13723 const struct iseq_insn_info *info = ISEQ_BODY(iseq)->insns_info;
13725 ibf_dump_write_small_value(dump, info->node_ids_size);
13726 ibf_dump_write(dump, rb_iseq_insn_info_node_ids(info), info->node_ids_size);
13732ibf_dump_local_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13735 const int size = body->local_table_size;
13736 ibf_offset_t offset = ibf_dump_pos(dump);
13739 for (i=0; i<size; i++) {
13740 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13743 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13745 ibf_dump_write_small_value(dump, v);
13751ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13755 ibf_offset_t reading_pos = local_table_offset;
13758 for (i=0; i<size; i++) {
13759 table[i] = ibf_load_id(load, ibf_load_small_value(load, &reading_pos));
13762 if (size == 1 && table[0] == idERROR_INFO) {
13763 ruby_xfree_sized(table,
sizeof(
ID) * size);
13764 return rb_iseq_shared_exc_local_tbl;
13776ibf_dump_lvar_states(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13779 const int size = ISEQ_LVAR_STATES_BUFLEN(body->local_table_size);
13780 IBF_W_ALIGN(uint8_t);
13781 if (iseq_has_lvar_states_p(body)) {
13782 return ibf_dump_write(dump, iseq_lvar_states(body),
sizeof(uint8_t) * size);
13785 return ibf_dump_write(dump, NULL, 0);
13790ibf_load_lvar_states(
const struct ibf_load *load,
struct rb_iseq_constant_body *load_body, ibf_offset_t lvar_states_offset,
int size,
const ID *local_table)
13792 if (local_table == rb_iseq_shared_exc_local_tbl ||
13794 load_body->lvar_states.list = NULL;
13796 else if (ISEQ_LVAR_STATES_EMBED_P((
unsigned int)size)) {
13797 ibf_offset_t pos = lvar_states_offset;
13798 const int len =
sizeof(uint8_t) * ISEQ_LVAR_STATES_BUFLEN(size);
13799 memcpy(load_body->lvar_states.single, ibf_load_ptr(load, &pos,
len),
len);
13802 load_body->lvar_states.list = IBF_R(lvar_states_offset, uint8_t, ISEQ_LVAR_STATES_BUFLEN(size));
13807ibf_dump_catch_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13812 int *iseq_indices =
ALLOCA_N(
int, table->size);
13815 for (i=0; i<table->size; i++) {
13816 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13819 const ibf_offset_t offset = ibf_dump_pos(dump);
13821 for (i=0; i<table->size; i++) {
13822 ibf_dump_write_small_value_nullable(dump, (
VALUE)iseq_indices[i]);
13823 ibf_dump_write_small_value(dump, table->entries[i].type);
13824 ibf_dump_write_small_value(dump, table->entries[i].start);
13825 ibf_dump_write_small_value(dump, table->entries[i].end);
13826 ibf_dump_write_small_value(dump, table->entries[i].cont);
13827 ibf_dump_write_small_value(dump, table->entries[i].sp);
13832 return ibf_dump_pos(dump);
13837ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size,
const rb_iseq_t *parent_iseq)
13840 struct iseq_catch_table *table = ruby_xcalloc(1, iseq_catch_table_bytes(size));
13841 table->size = size;
13842 ISEQ_BODY(parent_iseq)->catch_table = table;
13844 ibf_offset_t reading_pos = catch_table_offset;
13847 for (i=0; i<table->size; i++) {
13848 int iseq_index = (int)ibf_load_small_value_nullable(load, &reading_pos);
13849 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13850 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13851 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13852 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13853 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13855 rb_iseq_t *catch_iseq = (rb_iseq_t *)ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)iseq_index);
13856 RB_OBJ_WRITE(parent_iseq, UNALIGNED_MEMBER_PTR(&table->entries[i], iseq), catch_iseq);
13860 ISEQ_BODY(parent_iseq)->catch_table = NULL;
13865ibf_dump_ci_entries(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13868 const unsigned int ci_size = body->ci_size;
13871 ibf_offset_t offset = ibf_dump_pos(dump);
13875 for (i = 0; i < ci_size; i++) {
13878 ibf_dump_write_small_value_nullable(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13879 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13880 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13884 int len = kwarg->keyword_len;
13885 ibf_dump_write_small_value(dump,
len);
13886 for (
int j=0; j<
len; j++) {
13887 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13888 ibf_dump_write_small_value(dump, keyword);
13892 ibf_dump_write_small_value(dump, 0);
13897 ibf_dump_write_small_value_nullable(dump, (
VALUE)-1);
13915static enum rb_id_table_iterator_result
13916store_outer_variable(
ID id,
VALUE val,
void *dump)
13921 pair->name = rb_id2str(
id);
13923 return ID_TABLE_CONTINUE;
13927outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13935 else if (!bp->name) {
13943ibf_dump_outer_variables(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13945 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13947 ibf_offset_t offset = ibf_dump_pos(dump);
13949 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13950 ibf_dump_write_small_value(dump, (
VALUE)size);
13959 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13961 for (
size_t i = 0; i < size; ++i) {
13962 ID id = ovlist->pairs[i].id;
13963 ID val = ovlist->pairs[i].val;
13964 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13965 ibf_dump_write_small_value(dump, val);
13974ibf_dump_ic_entries(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13977 ibf_offset_t offset = ibf_dump_pos(dump);
13979 for (
unsigned int i = 0; i < body->ic_size; i++) {
13981 const rb_const_path_t const_path = ic->const_path;
13982 size_t size = const_path_size(const_path);
13983 ibf_dump_write_small_value(dump, (size << 1) | (const_path_absolute_p(const_path) ? 1 : 0));
13984 for (
size_t j = 0; j < size; j++) {
13985 ibf_dump_write_small_value(dump, ibf_dump_id(dump, const_path_aref(const_path, j)));
13995 ibf_offset_t reading_pos = ic_entries_offset;
13997 for (
unsigned int i = 0; i < load_body->ic_size; i++) {
13998 IC ic = &ISEQ_IS_IC_ENTRIES(load_body)[i];
13999 ic->const_path = ibf_load_const_path(load, &reading_pos);
14005ibf_load_ci_entries(
const struct ibf_load *load,
14006 ibf_offset_t ci_entries_offset,
14007 unsigned int ci_size,
14015 ibf_offset_t reading_pos = ci_entries_offset;
14022 for (i = 0; i < ci_size; i++) {
14023 VALUE mid_index = ibf_load_small_value_nullable(load, &reading_pos);
14024 if (mid_index != (
VALUE)-1) {
14025 ID mid = ibf_load_id(load, mid_index);
14026 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14027 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14030 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
14033 kwarg->references = 0;
14034 kwarg->keyword_len = kwlen;
14035 for (
int j=0; j<kwlen; j++) {
14036 VALUE keyword = ibf_load_small_value(load, &reading_pos);
14037 kwarg->keywords[j] = ibf_load_object(load, keyword);
14041 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
14043 cds[i].cc = vm_cc_empty();
14054ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
14056 ibf_offset_t reading_pos = outer_variables_offset;
14060 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
14062 if (table_size > 0) {
14063 tbl = rb_id_table_create(table_size);
14066 for (
size_t i = 0; i < table_size; i++) {
14067 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
14068 VALUE value = ibf_load_small_value(load, &reading_pos);
14069 if (!key) key = rb_make_temporary_id(i);
14070 rb_id_table_insert(tbl, key, value);
14077ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
14079 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
14083 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
14084 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
14086#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14087 ibf_offset_t iseq_start = ibf_dump_pos(dump);
14092 buffer.obj_table = ibf_dump_object_table_new();
14093 dump->current_buffer = &buffer;
14096 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
14097 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
14098 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
14099 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
14100 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
14102 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
14103 const ibf_offset_t lvar_states_offset = ibf_dump_lvar_states(dump, iseq);
14104 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
14105 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
14107 const int parent_iseq_index = ibf_table_lookup(dump->iseq_table, (st_data_t)ISEQ_BODY(iseq)->parent_iseq);
14108 const int local_iseq_index = ibf_table_lookup(dump->iseq_table, (st_data_t)ISEQ_BODY(iseq)->local_iseq);
14109 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_MANDATORY_ONLY_ISEQ(iseq));
14110 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
14111 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
14112 const ibf_offset_t ic_entries_offset = ibf_dump_ic_entries(dump, iseq);
14114#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14115 ibf_offset_t local_obj_list_offset;
14116 unsigned int local_obj_list_size;
14118 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
14121 ibf_offset_t body_offset = ibf_dump_pos(dump);
14124 unsigned int param_flags =
14125 (body->param.flags.has_lead << 0) |
14126 (body->param.flags.has_opt << 1) |
14127 (body->param.flags.has_rest << 2) |
14128 (body->param.flags.has_post << 3) |
14129 (body->param.flags.has_kw << 4) |
14130 (body->param.flags.has_kwrest << 5) |
14131 (body->param.flags.has_block << 6) |
14132 (body->param.flags.ambiguous_param0 << 7) |
14133 (body->param.flags.accepts_no_kwarg << 8) |
14134 (body->param.flags.ruby2_keywords << 9) |
14135 (body->param.flags.anon_rest << 10) |
14136 (body->param.flags.anon_kwrest << 11) |
14137 (body->param.flags.use_block << 12) |
14138 (body->param.flags.forwardable << 13) |
14139 (body->param.flags.accepts_no_block << 14);
14141#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14142# define IBF_BODY_OFFSET(x) (x)
14144# define IBF_BODY_OFFSET(x) (body_offset - (x))
14147 ibf_dump_write_small_value(dump, body->type);
14148 ibf_dump_write_small_value(dump, body->iseq_size);
14149 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
14150 ibf_dump_write_small_value(dump, bytecode_size);
14151 ibf_dump_write_small_value(dump, param_flags);
14152 ibf_dump_write_small_value(dump, body->param.size);
14153 ibf_dump_write_small_value(dump, body->param.lead_num);
14154 ibf_dump_write_small_value(dump, body->param.opt_num);
14155 ibf_dump_write_small_value(dump, body->param.post_num);
14156 ibf_dump_write_small_value(dump, body->param.block_start);
14157 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
14158 ibf_dump_write_small_value(dump, param_keyword_offset);
14159 ibf_dump_write_small_value(dump, location_pathobj_index);
14160 ibf_dump_write_small_value(dump, body->no_realpath);
14161 ibf_dump_write_small_value(dump, location_label_index);
14162 ibf_dump_write_small_value(dump, body->location.first_lineno);
14163 ibf_dump_write_small_value(dump, body->location.node_id);
14164 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
14165 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
14166 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
14167 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
14168 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
14169 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
14170 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(lvar_states_offset));
14171 ibf_dump_write_small_value(dump, catch_table_size);
14172 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
14173 ibf_dump_write_small_value_nullable(dump, (
VALUE)parent_iseq_index);
14174 ibf_dump_write_small_value_nullable(dump, (
VALUE)local_iseq_index);
14175 ibf_dump_write_small_value_nullable(dump, (
VALUE)mandatory_only_iseq_index);
14176 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
14177 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
14178 ibf_dump_write_small_value(dump, ISEQ_FLIP_CNT(iseq));
14179 ibf_dump_write_small_value(dump, body->local_table_size);
14180 ibf_dump_write_small_value(dump, body->ivc_size);
14181 ibf_dump_write_small_value(dump, body->icvarc_size);
14182 ibf_dump_write_small_value(dump, body->ic_size);
14183 if (body->ic_size) {
14184 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ic_entries_offset));
14186 ibf_dump_write_small_value(dump, body->ci_size);
14187 ibf_dump_write_small_value(dump, body->stack_max);
14188 ibf_dump_write_small_value(dump, body->builtin_attrs);
14189 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
14191#undef IBF_BODY_OFFSET
14193#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14194 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
14196 dump->current_buffer = saved_buffer;
14197 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
14199 ibf_offset_t offset = ibf_dump_pos(dump);
14200 ibf_dump_write_small_value(dump, iseq_start);
14201 ibf_dump_write_small_value(dump, iseq_length_bytes);
14202 ibf_dump_write_small_value(dump, body_offset);
14204 ibf_dump_write_small_value(dump, local_obj_list_offset);
14205 ibf_dump_write_small_value(dump, local_obj_list_size);
14207 st_free_table(buffer.obj_table);
14211 return body_offset;
14216ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
14218 VALUE str = ibf_load_object(load, str_index);
14220 str = rb_fstring(str);
14226ibf_load_iseq_each(
struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t offset)
14230 ibf_offset_t reading_pos = offset;
14232#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14234 load->current_buffer = &load->global_buffer;
14236 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14237 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14238 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14241 buffer.buff = load->global_buffer.buff + iseq_start;
14242 buffer.size = iseq_length_bytes;
14243 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14244 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14245 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
14246 VALUE obj_offsets_tmp;
14248 load->current_buffer = &buffer;
14249 ibf_load_object_offsets(load,
ALLOCV_N(ibf_offset_t, obj_offsets_tmp, buffer.obj_list_size));
14250 reading_pos = body_offset;
14253#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14254# define IBF_BODY_OFFSET(x) (x)
14256# define IBF_BODY_OFFSET(x) (offset - (x))
14259 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14260 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14261 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14262 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14263 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14264 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14265 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
14266 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
14267 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
14268 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
14269 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14270 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14271 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
14272 const bool no_realpath = (bool)ibf_load_small_value(load, &reading_pos);
14273 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
14274 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
14275 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
14276 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
14277 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
14278 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
14279 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
14280 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14281 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14282 const ibf_offset_t lvar_states_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14283 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14284 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14285 const int parent_iseq_index = (int)ibf_load_small_value_nullable(load, &reading_pos);
14286 const int local_iseq_index = (int)ibf_load_small_value_nullable(load, &reading_pos);
14287 const int mandatory_only_iseq_index = (int)ibf_load_small_value_nullable(load, &reading_pos);
14288 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14289 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14290 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
14291 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14293 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14294 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14295 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14296 const ibf_offset_t ic_entries_offset = ic_size ? (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos)) : 0;
14298 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14299 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14300 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14301 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
14304 if (load->pathobj && load->pathobj_index == location_pathobj_index) {
14305 rb_iseq_pathobj_set(iseq, load->pathobj,
Qnil);
14308 VALUE path = ibf_load_object(load, location_pathobj_index);
14312 path = rb_fstring(path);
14313 realpath = no_realpath ?
Qnil : path;
14316 VALUE pathobj = path;
14322 if (!
NIL_P(realpath)) {
14324 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
14325 "(%x), path=%+"PRIsVALUE,
14326 realpath,
TYPE(realpath), path);
14328 realpath = rb_fstring(realpath);
14334 if (
RTEST(load->path_override)) {
14335 path = load->path_override;
14336 realpath = load->realpath_override;
14338 rb_iseq_pathobj_set(iseq, path, realpath);
14340 VALUE pathobj = ISEQ_BODY(iseq)->location.pathobj;
14342 RB_OBJ_WRITE(load->loader_obj, &load->pathobj, pathobj);
14343 load->pathobj_index = location_pathobj_index;
14348 rb_execution_context_t *ec = GET_EC();
14349 VALUE dummy_frame = rb_vm_push_frame_fname(ec, rb_iseq_path(iseq));
14351#undef IBF_BODY_OFFSET
14353 load_body->type =
type;
14354 load_body->stack_max = stack_max;
14355 load_body->param.flags.has_lead = (param_flags >> 0) & 1;
14356 load_body->param.flags.has_opt = (param_flags >> 1) & 1;
14357 load_body->param.flags.has_rest = (param_flags >> 2) & 1;
14358 load_body->param.flags.has_post = (param_flags >> 3) & 1;
14359 load_body->param.flags.has_kw = FALSE;
14360 load_body->param.flags.has_kwrest = (param_flags >> 5) & 1;
14361 load_body->param.flags.has_block = (param_flags >> 6) & 1;
14362 load_body->param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
14363 load_body->param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
14364 load_body->param.flags.ruby2_keywords = (param_flags >> 9) & 1;
14365 load_body->param.flags.anon_rest = (param_flags >> 10) & 1;
14366 load_body->param.flags.anon_kwrest = (param_flags >> 11) & 1;
14367 load_body->param.flags.use_block = (param_flags >> 12) & 1;
14368 load_body->param.flags.forwardable = (param_flags >> 13) & 1;
14369 load_body->param.flags.accepts_no_block = (param_flags >> 14) & 1;
14370 load_body->param.size = param_size;
14371 load_body->param.lead_num = param_lead_num;
14372 load_body->param.opt_num = param_opt_num;
14373 load_body->param.post_num = param_post_num;
14374 load_body->param.block_start = param_block_start;
14375 load_body->local_table_size = local_table_size;
14376 load_body->ci_size = ci_size;
14379 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
14380 if (variable_flip_count) {
14382 v->flip_count = variable_flip_count;
14385 load_body->location.first_lineno = location_first_lineno;
14386 load_body->location.node_id = location_node_id;
14387 load_body->source_hash = ((uint64_t)load->header->source_hash_hi << 32) | load->header->source_hash_lo;
14388 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
14389 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
14390 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
14391 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
14392 load_body->builtin_attrs = builtin_attrs;
14393 load_body->prism = prism;
14395 load_body->ivc_size = ivc_size;
14396 load_body->icvarc_size = icvarc_size;
14397 load_body->ic_size = ic_size;
14399 if (ISEQ_IS_SIZE(load_body)) {
14400 load_body->is_entries =
ZALLOC_N(
char, ISEQ_IS_BYTESIZE(load_body));
14403 load_body->is_entries = NULL;
14405 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
14406 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
14407 ibf_load_ic_entries(load, ic_entries_offset, load_body);
14408 load_body->param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
14409 load_body->param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
14410 load_body->param.flags.has_kw = (param_flags >> 4) & 1;
14411 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
14412 ibf_load_lvar_states(load, load_body, lvar_states_offset, local_table_size, load_body->local_table);
14413 ibf_load_catch_table(load, catch_table_offset, catch_table_size, iseq);
14415 const rb_iseq_t *parent_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)parent_iseq_index);
14416 const rb_iseq_t *local_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)local_iseq_index);
14417 const rb_iseq_t *mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
14419 RB_OBJ_WRITE(iseq, &load_body->parent_iseq, parent_iseq);
14420 RB_OBJ_WRITE(iseq, &load_body->local_iseq, local_iseq);
14421 rb_iseq_mandatory_only_iseq_set(iseq, mandatory_only_iseq);
14424 if (load_body->param.keyword != NULL) {
14426 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->param.keyword;
14427 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
14430 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size, insns_info_body_offset);
14432 rb_iseq_translate_threaded_code(iseq);
14434#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14435 load->current_buffer = &load->global_buffer;
14438 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
14440#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14441 load->current_buffer = saved_buffer;
14444 verify_call_cache(iseq);
14447 rb_vm_pop_frame_no_int(ec);
14457ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14459 const rb_iseq_t *iseq = (
const rb_iseq_t *)key;
14462 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
14465 return ST_CONTINUE;
14475 args.offset_list = offset_list;
14477 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
14480 st_index_t size = dump->iseq_table->num_entries;
14481 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
14483 for (i = 0; i < size; i++) {
14487 ibf_dump_align(dump,
sizeof(ibf_offset_t));
14488 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
14489 header->iseq_list_size = (
unsigned int)size;
14499 unsigned int type: 5;
14500 unsigned int special_const: 1;
14501 unsigned int frozen: 1;
14502 unsigned int internal: 1;
14505enum ibf_object_class_index {
14506 IBF_OBJECT_CLASS_OBJECT,
14507 IBF_OBJECT_CLASS_ARRAY,
14508 IBF_OBJECT_CLASS_STANDARD_ERROR,
14509 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
14510 IBF_OBJECT_CLASS_TYPE_ERROR,
14511 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
14521 long keyval[FLEX_ARY_LEN];
14534 BDIGIT digits[FLEX_ARY_LEN];
14537enum ibf_object_data_type {
14538 IBF_OBJECT_DATA_ENCODING,
14549#define IBF_ALIGNED_OFFSET(align, offset) \
14550 ((((offset) - 1) / (align) + 1) * (align))
14555#define IBF_OBJBODY(type, offset) \
14556 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
14559ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
14561 if (offset >= load->current_buffer->size) {
14562 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
14564 return load->current_buffer->buff + offset;
14567NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
14570ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
14573 rb_raw_obj_info(buff,
sizeof(buff), obj);
14582 rb_raise(rb_eArgError,
"unsupported");
14589 enum ibf_object_class_index cindex;
14591 cindex = IBF_OBJECT_CLASS_OBJECT;
14594 cindex = IBF_OBJECT_CLASS_ARRAY;
14597 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
14600 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
14603 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
14606 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
14609 rb_obj_info_dump(obj);
14611 rb_bug(
"unsupported class");
14613 ibf_dump_write_small_value(dump, (
VALUE)cindex);
14619 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
14622 case IBF_OBJECT_CLASS_OBJECT:
14624 case IBF_OBJECT_CLASS_ARRAY:
14626 case IBF_OBJECT_CLASS_STANDARD_ERROR:
14628 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
14630 case IBF_OBJECT_CLASS_TYPE_ERROR:
14632 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
14636 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
14644 (void)IBF_W(&dbl,
double, 1);
14652 memcpy(&d, IBF_OBJBODY(
double, offset),
sizeof(d));
14661 long encindex = (long)rb_enc_get_index(obj);
14663 const char *ptr = RSTRING_PTR(obj);
14665 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14666 rb_encoding *enc = rb_enc_from_index((
int)encindex);
14667 const char *enc_name = rb_enc_name(enc);
14668 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
14671 ibf_dump_write_small_value(dump, encindex);
14672 ibf_dump_write_small_value(dump,
len);
14673 IBF_WP(ptr,
char,
len);
14679 ibf_offset_t reading_pos = offset;
14681 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14683 const char *ptr = load->current_buffer->buff + reading_pos;
14685 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14686 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14687 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14691 if (header->frozen && !header->internal) {
14692 str = rb_enc_literal_str(ptr,
len, rb_enc_from_index(encindex));
14695 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
14698 if (header->frozen) str = rb_fstring(str);
14709 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
14711 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
14712 ibf_dump_write_small_value(dump, regexp.srcstr);
14719 regexp.option = ibf_load_byte(load, &offset);
14720 regexp.srcstr = ibf_load_small_value(load, &offset);
14722 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
14723 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
14735 ibf_dump_write_small_value(dump,
len);
14736 for (i=0; i<
len; i++) {
14737 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
14738 ibf_dump_write_small_value(dump, index);
14745 ibf_offset_t reading_pos = offset;
14752 for (i=0; i<
len; i++) {
14753 const VALUE index = ibf_load_small_value(load, &reading_pos);
14757 if (header->frozen) {
14758 rb_ary_freeze(ary);
14759 rb_ractor_make_shareable(ary);
14766ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
14770 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14771 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14773 ibf_dump_write_small_value(dump, key_index);
14774 ibf_dump_write_small_value(dump, val_index);
14775 return ST_CONTINUE;
14782 ibf_dump_write_small_value(dump, (
VALUE)
len);
14790 long len = (long)ibf_load_small_value(load, &offset);
14791 VALUE obj = header->frozen ? rb_hash_alloc_fixed_size(
rb_cHash,
len) : rb_hash_new_capa(
len);
14794 for (i = 0; i <
len; i++) {
14795 VALUE key_index = ibf_load_small_value(load, &offset);
14796 VALUE val_index = ibf_load_small_value(load, &offset);
14798 VALUE key = ibf_load_object(load, key_index);
14799 VALUE val = ibf_load_object(load, val_index);
14800 rb_hash_aset(obj, key, val);
14804 if (header->frozen) {
14805 RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14812ibf_dump_cdhash_i(st_data_t key, st_data_t val, st_data_t ptr)
14816 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14818 ibf_dump_write_small_value(dump, key_index);
14819 ibf_dump_write_small_value(dump, val);
14820 return ST_CONTINUE;
14826 ibf_dump_write_small_value(dump, (
VALUE)imemo_type(obj));
14827 switch (imemo_type(obj)) {
14828 case imemo_once_storage:
14830 case imemo_cdhash: {
14831 st_table *tbl = rb_imemo_cdhash_tbl(obj);
14833 long len = tbl->num_entries;
14834 ibf_dump_write_small_value(dump, (
VALUE)
len);
14839 ibf_dump_object_unsupported(dump, obj);
14847 enum imemo_type
type = (
enum imemo_type)ibf_load_small_value(load, &offset);
14849 case imemo_once_storage:
14850 return rb_imemo_once_storage_new();
14851 case imemo_cdhash: {
14852 long len = (long)ibf_load_small_value(load, &offset);
14855 for (
long i = 0; i <
len; i++) {
14856 VALUE key_index = ibf_load_small_value(load, &offset);
14857 VALUE val = ibf_load_small_value(load, &offset);
14859 VALUE key = ibf_load_object(load, key_index);
14860 cdhash_aset(obj, key, val);
14866 return ibf_load_object_unsupported(load, header, offset);
14878 range.class_index = 0;
14881 range.beg = (long)ibf_dump_object(dump, beg);
14882 range.end = (long)ibf_dump_object(dump, end);
14888 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
14897 VALUE beg = ibf_load_object(load, range->beg);
14898 VALUE end = ibf_load_object(load, range->end);
14908 ssize_t
len = BIGNUM_LEN(obj);
14909 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14910 BDIGIT *d = BIGNUM_DIGITS(obj);
14912 (void)IBF_W(&slen, ssize_t, 1);
14913 IBF_WP(d, BDIGIT,
len);
14920 int sign = bignum->slen > 0;
14921 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14922 const int big_unpack_flags =
14925 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
14936 if (rb_data_is_encoding(obj)) {
14937 rb_encoding *enc = rb_to_encoding(obj);
14938 const char *name = rb_enc_name(enc);
14939 long len = strlen(name) + 1;
14941 data[0] = IBF_OBJECT_DATA_ENCODING;
14943 (void)IBF_W(data,
long, 2);
14944 IBF_WP(name,
char,
len);
14947 ibf_dump_object_unsupported(dump, obj);
14954 const long *body = IBF_OBJBODY(
long, offset);
14955 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14957 const char *data = (
const char *)&body[2];
14960 case IBF_OBJECT_DATA_ENCODING:
14962 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
14967 return ibf_load_object_unsupported(load, header, offset);
14971ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14974 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14975 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14977 (void)IBF_W(data,
long, 2);
14981ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14984 VALUE a = ibf_load_object(load, nums->a);
14985 VALUE b = ibf_load_object(load, nums->b);
14997 ibf_dump_object_string(dump,
rb_sym2str(obj));
15003 ibf_offset_t reading_pos = offset;
15005 int encindex = (int)ibf_load_small_value(load, &reading_pos);
15007 const char *ptr = load->current_buffer->buff + reading_pos;
15009 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
15010 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
15011 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
15014 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
15018typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
15019static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
15020 ibf_dump_object_unsupported,
15021 ibf_dump_object_unsupported,
15022 ibf_dump_object_class,
15023 ibf_dump_object_unsupported,
15024 ibf_dump_object_float,
15025 ibf_dump_object_string,
15026 ibf_dump_object_regexp,
15027 ibf_dump_object_array,
15028 ibf_dump_object_hash,
15029 ibf_dump_object_struct,
15030 ibf_dump_object_bignum,
15031 ibf_dump_object_unsupported,
15032 ibf_dump_object_data,
15033 ibf_dump_object_unsupported,
15034 ibf_dump_object_complex_rational,
15035 ibf_dump_object_complex_rational,
15036 ibf_dump_object_unsupported,
15037 ibf_dump_object_unsupported,
15038 ibf_dump_object_unsupported,
15039 ibf_dump_object_unsupported,
15040 ibf_dump_object_symbol,
15041 ibf_dump_object_unsupported,
15042 ibf_dump_object_unsupported,
15043 ibf_dump_object_unsupported,
15044 ibf_dump_object_unsupported,
15045 ibf_dump_object_unsupported,
15046 ibf_dump_object_imemo,
15047 ibf_dump_object_unsupported,
15048 ibf_dump_object_unsupported,
15049 ibf_dump_object_unsupported,
15050 ibf_dump_object_unsupported,
15051 ibf_dump_object_unsupported,
15057 unsigned char byte =
15058 (header.type << 0) |
15059 (header.special_const << 5) |
15060 (header.frozen << 6) |
15061 (header.internal << 7);
15067ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
15069 unsigned char byte = ibf_load_byte(load, offset);
15072 header.type = (
byte >> 0) & 0x1f;
15073 header.special_const = (
byte >> 5) & 0x01;
15074 header.frozen = (
byte >> 6) & 0x01;
15075 header.internal = (
byte >> 7) & 0x01;
15084 ibf_offset_t current_offset;
15085 IBF_ZERO(obj_header);
15086 obj_header.type =
TYPE(obj);
15088 IBF_W_ALIGN(ibf_offset_t);
15089 current_offset = ibf_dump_pos(dump);
15094 obj_header.special_const = TRUE;
15095 obj_header.frozen = TRUE;
15096 obj_header.internal = TRUE;
15097 ibf_dump_object_object_header(dump, obj_header);
15098 ibf_dump_write_small_value(dump, obj);
15102 obj_header.special_const = FALSE;
15103 obj_header.frozen =
OBJ_FROZEN(obj) ? TRUE : FALSE;
15104 ibf_dump_object_object_header(dump, obj_header);
15105 (*dump_object_functions[obj_header.type])(dump, obj);
15108 return current_offset;
15112static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
15113 ibf_load_object_unsupported,
15114 ibf_load_object_unsupported,
15115 ibf_load_object_class,
15116 ibf_load_object_unsupported,
15117 ibf_load_object_float,
15118 ibf_load_object_string,
15119 ibf_load_object_regexp,
15120 ibf_load_object_array,
15121 ibf_load_object_hash,
15122 ibf_load_object_struct,
15123 ibf_load_object_bignum,
15124 ibf_load_object_unsupported,
15125 ibf_load_object_data,
15126 ibf_load_object_unsupported,
15127 ibf_load_object_complex_rational,
15128 ibf_load_object_complex_rational,
15129 ibf_load_object_unsupported,
15130 ibf_load_object_unsupported,
15131 ibf_load_object_unsupported,
15132 ibf_load_object_unsupported,
15133 ibf_load_object_symbol,
15134 ibf_load_object_unsupported,
15135 ibf_load_object_unsupported,
15136 ibf_load_object_unsupported,
15137 ibf_load_object_unsupported,
15138 ibf_load_object_unsupported,
15139 ibf_load_object_imemo,
15140 ibf_load_object_unsupported,
15141 ibf_load_object_unsupported,
15142 ibf_load_object_unsupported,
15143 ibf_load_object_unsupported,
15144 ibf_load_object_unsupported,
15150# define IBF_LOAD_ALLOC_OBJ_OFFSETS(load, ptr) \
15151 VALUE ptr##_tmp = 0; \
15152 ibf_offset_t *ptr = ALLOC_N(ibf_offset_t, (load)->current_buffer->obj_list_size)
15153# define IBF_LOAD_FREE_OBJ_OFFSETS(load, ptr) ((void)(ptr##_tmp))
15155# define IBF_LOAD_ALLOC_OBJ_OFFSETS(load, ptr) \
15157 ibf_offset_t *ptr = ALLOCV_N(ibf_offset_t, ptr##_tmp, (load)->current_buffer->obj_list_size)
15158# define IBF_LOAD_FREE_OBJ_OFFSETS(load, ptr) \
15159 ((load)->current_buffer->obj_offsets = NULL, ALLOCV_END(ptr##_tmp))
15163ibf_load_object_offsets(
const struct ibf_load *load, ibf_offset_t *offsets)
15166 ibf_offset_t reading_pos = buffer->obj_list_offset;
15167 buffer->obj_offsets = offsets;
15168 ibf_offset_t offset = 0;
15170 for (
unsigned int i = 0; i < buffer->obj_list_size; i++) {
15171 VALUE delta = ibf_load_small_value(load, &reading_pos);
15172 if (delta >= buffer->size - offset) {
15175 offset += (ibf_offset_t)delta;
15176 offsets[i] = offset;
15181ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
15183 if (object_index == 0) {
15187 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
15189 ibf_offset_t offset = load->current_buffer->obj_offsets[object_index];
15190 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
15193 fprintf(stderr,
"ibf_load_object: list=%#x offset=%#x\n",
15194 load->current_buffer->obj_list_offset, offset);
15195 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
15196 header.type, header.special_const, header.frozen, header.internal);
15198 if (offset >= load->current_buffer->size) {
15199 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
15202 if (header.special_const) {
15203 ibf_offset_t reading_pos = offset;
15205 obj = ibf_load_small_value(load, &reading_pos);
15208 obj = (*load_object_functions[header.type])(load, &header, offset);
15211 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
15214 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
15215 object_index, obj);
15228ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
15233 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
15236 return ST_CONTINUE;
15240ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
15242 st_table *obj_table = dump->current_buffer->obj_table;
15247 args.offset_list = offset_list;
15249 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
15251 *obj_list_offset = ibf_dump_pos(dump);
15253 st_index_t size = obj_table->num_entries;
15255 ibf_offset_t prev_offset = 0;
15262 for (i=0; i<size; i++) {
15264 ibf_dump_write_small_value(dump, offset - prev_offset);
15265 prev_offset = offset;
15268 *obj_list_size = (
unsigned int)size;
15272ibf_dump_mark(
void *ptr)
15275 rb_gc_mark(dump->global_buffer.str);
15277 rb_mark_set(dump->global_buffer.obj_table);
15278 rb_mark_set(dump->iseq_table);
15282ibf_dump_free(
void *ptr)
15285 if (dump->global_buffer.obj_table) {
15286 st_free_table(dump->global_buffer.obj_table);
15287 dump->global_buffer.obj_table = 0;
15289 if (dump->iseq_table) {
15290 st_free_table(dump->iseq_table);
15291 dump->iseq_table = 0;
15296ibf_dump_memsize(
const void *ptr)
15300 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
15301 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
15307 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
15308 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
15314 dump->global_buffer.obj_table = NULL;
15315 dump->iseq_table = NULL;
15318 dump->global_buffer.obj_table = ibf_dump_object_table_new();
15319 dump->iseq_table = st_init_numtable();
15321 dump->current_buffer = &dump->global_buffer;
15325iseq_ibf_dump_0(
struct ibf_dump *dump,
const rb_iseq_t *iseq,
VALUE opt)
15329 ibf_dump_write(dump, &header,
sizeof(header));
15330 ibf_dump_iseq(dump, iseq);
15332 header.magic[0] =
'Y';
15333 header.magic[1] =
'A';
15334 header.magic[2] =
'R';
15335 header.magic[3] =
'B';
15336 header.major_version = IBF_MAJOR_VERSION;
15337 header.minor_version = IBF_MINOR_VERSION;
15338 header.endian = IBF_ENDIAN_MARK;
15340 header.source_hash_hi = (uint32_t)(ISEQ_BODY(iseq)->source_hash >> 32);
15341 header.source_hash_lo = (uint32_t)ISEQ_BODY(iseq)->source_hash;
15342 ibf_dump_iseq_list(dump, &header);
15343 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
15344 header.size = ibf_dump_pos(dump);
15347 VALUE opt_str = opt;
15350 ibf_dump_write(dump, ptr, header.extra_size);
15353 header.extra_size = 0;
15356 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
15358 return dump->global_buffer.str;
15362rb_iseq_ibf_dump(
const rb_iseq_t *iseq,
VALUE opt)
15368 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
15369 ISEQ_BODY(iseq)->local_iseq != iseq) {
15372 if (
RTEST(ISEQ_COVERAGE(iseq))) {
15377 ibf_dump_setup(dump, dump_obj);
15379 str = iseq_ibf_dump_0(dump, iseq, opt);
15384static const ibf_offset_t *
15385ibf_iseq_list(
const struct ibf_load *load)
15387 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
15391rb_ibf_load_iseq_complete(rb_iseq_t *iseq)
15394 rb_iseq_t *prev_src_iseq = load->iseq;
15395 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
15398 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
15399 iseq->aux.loader.index, offset,
15400 load->header->size);
15402 ibf_load_iseq_each(load, iseq, offset);
15403 ISEQ_COMPILE_DATA_CLEAR(iseq);
15405 rb_iseq_init_trace(iseq);
15406 load->iseq = prev_src_iseq;
15411rb_iseq_complete(
const rb_iseq_t *iseq)
15413 rb_ibf_load_iseq_complete((rb_iseq_t *)iseq);
15419ibf_load_iseq(
const struct ibf_load *load,
const rb_iseq_t *index_iseq)
15421 int iseq_index = (int)(
VALUE)index_iseq;
15424 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
15425 (
void *)index_iseq, (
void *)load->iseq_list);
15427 if (iseq_index == -1) {
15431 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
15434 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
15437 return (rb_iseq_t *)iseqv;
15440 rb_iseq_t *iseq = iseq_imemo_alloc();
15442 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
15446 iseq->aux.loader.index = iseq_index;
15448 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
15449 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
15451 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
15453 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
15455 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
15457 rb_ibf_load_iseq_complete(iseq);
15461 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
15462 (
void *)iseq, (
void *)load->iseq);
15470ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
15473 load->loader_obj = loader_obj;
15474 load->global_buffer.buff = bytes;
15475 load->header = header;
15476 load->global_buffer.size = header->size;
15477 load->global_buffer.obj_list_offset = header->global_object_list_offset;
15478 load->global_buffer.obj_list_size = header->global_object_list_size;
15479 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
15480 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
15483 load->current_buffer = &load->global_buffer;
15485 if (size < header->size) {
15488 if (strncmp(header->magic,
"YARB", 4) != 0) {
15491 if (header->major_version != IBF_MAJOR_VERSION ||
15492 header->minor_version != IBF_MINOR_VERSION) {
15494 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
15496 if (header->endian != IBF_ENDIAN_MARK) {
15502 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
15503 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
15504 header->iseq_list_offset);
15506 if (load->global_buffer.obj_list_offset >= load->global_buffer.size) {
15507 rb_raise(rb_eArgError,
"object list offset out of range: %u",
15508 load->global_buffer.obj_list_offset);
15521 if (USE_LAZY_LOAD) {
15522 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
15525 ibf_load_setup_bytes(load, loader_obj, RSTRING_PTR(str), RSTRING_LEN(str));
15530ibf_loader_mark(
void *ptr)
15533 rb_gc_mark(load->str);
15534 rb_gc_mark(load->path_override);
15535 rb_gc_mark(load->realpath_override);
15536 rb_gc_mark(load->pathobj);
15537 rb_gc_mark(load->iseq_list);
15538 rb_gc_mark(load->global_buffer.obj_list);
15542ibf_loader_free(
void *ptr)
15546 if (load->global_buffer.obj_offsets) {
15547 SIZED_FREE_N(load->global_buffer.obj_offsets, load->global_buffer.obj_list_size);
15554ibf_loader_memsize(
const void *ptr)
15556 size_t size =
sizeof(
struct ibf_load);
15559 if (load->global_buffer.obj_offsets) {
15560 size += load->global_buffer.obj_list_size * sizeof(ibf_offset_t);
15568 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
15569 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE
15575 if (
NIL_P(path))
return;
15576 if (
NIL_P(realpath)) realpath = path;
15577 RB_OBJ_WRITE(load->loader_obj, &load->path_override, rb_fstring(path));
15578 RB_OBJ_WRITE(load->loader_obj, &load->realpath_override, rb_fstring(realpath));
15581static const rb_iseq_t *
15584 IBF_LOAD_ALLOC_OBJ_OFFSETS(load, obj_offsets);
15585 ibf_load_object_offsets(load, obj_offsets);
15586 ibf_load_set_path(load, fname, path);
15587 rb_iseq_t *iseq = ibf_load_iseq(load, 0);
15588 IBF_LOAD_FREE_OBJ_OFFSETS(load, obj_offsets);
15597 ibf_load_setup(load, loader_obj, str);
15599 const rb_iseq_t *iseq = iseq_ibf_load(load, fname, path);
15606rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size,
VALUE fname,
VALUE path)
15610 ibf_load_setup_bytes(load, loader_obj, bytes, size);
15612 const rb_iseq_t *iseq = iseq_ibf_load(load, fname, path);
15620ibf_dump_iseq_table_collect_i(st_data_t key, st_data_t val, st_data_t arg)
15622 const rb_iseq_t **srcs = (
const rb_iseq_t **)arg;
15623 srcs[(int)val] = (
const rb_iseq_t *)key;
15624 return ST_CONTINUE;
15630iseq_subtree_copy_pc2branchindex(
const struct ibf_dump *dump,
const struct ibf_load *load)
15632 unsigned int iseq_count = (
unsigned int)dump->iseq_table->num_entries;
15634 const rb_iseq_t **srcs =
ALLOCV_N(
const rb_iseq_t *, srcs_buf, iseq_count);
15635 st_foreach(dump->iseq_table, ibf_dump_iseq_table_collect_i, (st_data_t)srcs);
15637 for (
unsigned int i = 0; i < iseq_count; i++) {
15638 rb_iseq_t *copy = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)i);
15639 VALUE pc2branchindex = ISEQ_PC2BRANCHINDEX(srcs[i]);
15640 if (pc2branchindex !=
Qnil) {
15641 ISEQ_PC2BRANCHINDEX_SET(copy, pc2branchindex);
15650rb_iseq_dup_with_independent_caches(
const rb_iseq_t *src_root)
15658 ibf_dump_setup(dump, dump_obj);
15660 str = iseq_ibf_dump_0(dump, src_root,
Qnil);
15665 ibf_load_setup(load, loader_obj, str);
15666 IBF_LOAD_ALLOC_OBJ_OFFSETS(load, obj_offsets);
15667 ibf_load_object_offsets(load, obj_offsets);
15675 unsigned int iseq_count = (
unsigned int)dump->iseq_table->num_entries;
15676 const rb_iseq_t *result = NULL;
15677 for (
unsigned int i = 0; i < iseq_count; i++) {
15678 rb_iseq_t *copy = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)i);
15681 rb_ibf_load_iseq_complete(copy);
15687 if (!cb->local_iseq)
RB_OBJ_WRITE(copy, &cb->local_iseq, sb->local_iseq);
15688 RB_OBJ_WRITE(copy, &cb->location.pathobj, sb->location.pathobj);
15689 cb->no_realpath = sb->no_realpath;
15690 VALUE sl = ISEQ_SCRIPT_LINES(src_root);
15691 VALUE cov = ISEQ_COVERAGE(src_root);
15699 RB_OBJ_WRITE(copy, &cb->parent_iseq, sb->parent_iseq);
15708 if (ISEQ_PC2BRANCHINDEX(src_root) !=
Qnil) {
15709 iseq_subtree_copy_pc2branchindex(dump, load);
15711 IBF_LOAD_FREE_OBJ_OFFSETS(load, obj_offsets);
15719rb_iseq_ibf_load_extra_data(
VALUE str)
15725 ibf_load_setup(load, loader_obj, str);
15726 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
15731#include "prism_compile.c"
#define RUBY_ATOMIC_PTR_LOAD(var)
Identical to RUBY_ATOMIC_LOAD, except it expects its arguments are void*.
#define RUBY_ALIGNOF
Wraps (or simulates) alignof.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_CLASS
Encountered a new class.
#define RUBY_EVENT_NONE
No events.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
#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 RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define NUM2ULONG
Old name of RB_NUM2ULONG.
#define NUM2LL
Old name of RB_NUM2LL.
#define ALLOCV
Old name of RB_ALLOCV.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define FIX2UINT
Old name of RB_FIX2UINT.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define FIXABLE
Old name of RB_FIXABLE.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_SET
Old name of RB_FL_SET.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define NUM2ULL
Old name of RB_NUM2ULL.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
VALUE rb_eNotImpError
NotImplementedError exception.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_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_cObject
Object class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_cHash
Hash class.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cRange
Range class.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RB_POSFIXABLE(_)
Checks if the passed value is in range of fixnum, assuming it is a positive number.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]].
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_resurrect(VALUE ary)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
VALUE rb_ary_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty 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_cat(VALUE ary, const VALUE *train, rb_len_t len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
#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(rb_len_t 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, rb_len_t srclen)
Destructively appends the passed contents to the string.
VALUE rb_str_resurrect(VALUE str)
Like rb_str_dup(), but always create an instance of rb_cString regardless of the given object's class...
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
int len
Length of the buffer.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
static VALUE RB_OBJ_SET_FROZEN_SHAREABLE(VALUE obj)
Freezes and marks the object as shareable.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
#define rb_long2int
Just another name of rb_long2int_inline.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define ALLOCA_N(type, n)
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RB_ALLOCV(v, n)
Identical to RB_ALLOCV_N(), except that it allocates a number of bytes and returns a void* .
VALUE type(ANYARGS)
ANYARGS-ed function type.
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 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.
#define RARRAY_AREF(a, i)
References an object in an array.
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
#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.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
unsigned long rb_ulen_t
An unsigned integer type that has the same width with rb_len_t.
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.