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;
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_event_flag_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)
741validate_label(st_data_t name, st_data_t label, st_data_t arg)
743 rb_iseq_t *iseq = (rb_iseq_t *)arg;
744 LABEL *lobj = (LABEL *)label;
745 if (!lobj->link.next) {
747 COMPILE_ERROR(iseq, lobj->position,
748 "%"PRIsVALUE
": undefined label",
756validate_labels(rb_iseq_t *iseq, st_table *labels_table)
758 st_foreach(labels_table, validate_label, (st_data_t)iseq);
759 st_free_table(labels_table);
763get_nd_recv(
const NODE *node)
765 switch (nd_type(node)) {
767 return RNODE_CALL(node)->nd_recv;
769 return RNODE_OPCALL(node)->nd_recv;
773 return RNODE_QCALL(node)->nd_recv;
777 return RNODE_ATTRASGN(node)->nd_recv;
779 return RNODE_OP_ASGN1(node)->nd_recv;
781 return RNODE_OP_ASGN2(node)->nd_recv;
783 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
788get_node_call_nd_mid(
const NODE *node)
790 switch (nd_type(node)) {
792 return RNODE_CALL(node)->nd_mid;
794 return RNODE_OPCALL(node)->nd_mid;
796 return RNODE_FCALL(node)->nd_mid;
798 return RNODE_QCALL(node)->nd_mid;
800 return RNODE_VCALL(node)->nd_mid;
802 return RNODE_ATTRASGN(node)->nd_mid;
804 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
809get_nd_args(
const NODE *node)
811 switch (nd_type(node)) {
813 return RNODE_CALL(node)->nd_args;
815 return RNODE_OPCALL(node)->nd_args;
817 return RNODE_FCALL(node)->nd_args;
819 return RNODE_QCALL(node)->nd_args;
823 return RNODE_ATTRASGN(node)->nd_args;
825 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
830get_node_colon_nd_mid(
const NODE *node)
832 switch (nd_type(node)) {
834 return RNODE_COLON2(node)->nd_mid;
836 return RNODE_COLON3(node)->nd_mid;
838 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
843get_nd_vid(
const NODE *node)
845 switch (nd_type(node)) {
847 return RNODE_LASGN(node)->nd_vid;
849 return RNODE_DASGN(node)->nd_vid;
851 return RNODE_IASGN(node)->nd_vid;
853 return RNODE_CVASGN(node)->nd_vid;
855 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
860get_nd_value(
const NODE *node)
862 switch (nd_type(node)) {
864 return RNODE_LASGN(node)->nd_value;
866 return RNODE_DASGN(node)->nd_value;
868 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
873get_string_value(
const NODE *node)
875 switch (nd_type(node)) {
881 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
891 (*ifunc->func)(iseq, ret, ifunc->data);
893 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
895 CHECK(iseq_setup_insn(iseq, ret));
896 return iseq_setup(iseq, ret);
899static bool drop_unreachable_return(LINK_ANCHOR *ret);
902rb_iseq_compile_node(rb_iseq_t *iseq,
const NODE *node)
908 NO_CHECK(COMPILE(ret,
"nil", node));
909 iseq_set_local_table(iseq, 0, 0);
912 else if (nd_type_p(node, NODE_SCOPE)) {
914 iseq_set_local_table(iseq, RNODE_SCOPE(node)->nd_tbl, (NODE *)RNODE_SCOPE(node)->nd_args);
915 iseq_set_arguments(iseq, ret, (NODE *)RNODE_SCOPE(node)->nd_args);
916 iseq_set_parameters_lvar_state(iseq);
918 switch (ISEQ_BODY(iseq)->
type) {
919 case ISEQ_TYPE_BLOCK:
921 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
922 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
924 start->rescued = LABEL_RESCUE_BEG;
925 end->rescued = LABEL_RESCUE_END;
928 ADD_SYNTHETIC_INSN(ret, ISEQ_BODY(iseq)->location.first_lineno, -1, nop);
929 ADD_LABEL(ret, start);
930 CHECK(COMPILE(ret,
"block body", RNODE_SCOPE(node)->nd_body));
933 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
936 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
937 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
940 case ISEQ_TYPE_CLASS:
943 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
945 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
948 case ISEQ_TYPE_METHOD:
950 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
952 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
953 ISEQ_COMPILE_DATA(iseq)->root_node = RNODE_SCOPE(node)->nd_body;
955 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
959 CHECK(COMPILE(ret,
"scoped node", RNODE_SCOPE(node)->nd_body));
966#define INVALID_ISEQ_TYPE(type) \
967 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
968 switch (ISEQ_BODY(iseq)->
type) {
969 case INVALID_ISEQ_TYPE(
METHOD);
970 case INVALID_ISEQ_TYPE(CLASS);
971 case INVALID_ISEQ_TYPE(BLOCK);
972 case INVALID_ISEQ_TYPE(EVAL);
973 case INVALID_ISEQ_TYPE(MAIN);
974 case INVALID_ISEQ_TYPE(TOP);
975#undef INVALID_ISEQ_TYPE
976 case ISEQ_TYPE_RESCUE:
977 iseq_set_exception_local_table(iseq);
978 CHECK(COMPILE(ret,
"rescue", node));
980 case ISEQ_TYPE_ENSURE:
981 iseq_set_exception_local_table(iseq);
982 CHECK(COMPILE_POPPED(ret,
"ensure", node));
984 case ISEQ_TYPE_PLAIN:
985 CHECK(COMPILE(ret,
"ensure", node));
988 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
991 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
996 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
997 NODE dummy_line_node = generate_dummy_line_node(0, -1);
998 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
999 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
1001 else if (!drop_unreachable_return(ret)) {
1002 ADD_SYNTHETIC_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, -1, leave);
1006 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
1007 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
1008 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
1009 validate_labels(iseq, labels_table);
1012 CHECK(iseq_setup_insn(iseq, ret));
1013 return iseq_setup(iseq, ret);
1017rb_iseq_translate_threaded_code(rb_iseq_t *iseq)
1019#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
1020 const void *
const *table = rb_vm_get_insns_address_table();
1022 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
1024 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1025 int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
1026 int len = insn_len(insn);
1027 encoded[i] = (
VALUE)table[insn];
1034 rb_yjit_live_iseq_count++;
1035 rb_yjit_iseq_alloc_count++;
1042rb_iseq_original_iseq(
const rb_iseq_t *iseq)
1047 if (original_code)
return original_code;
1048 original_code =
ALLOC_N(
VALUE, ISEQ_BODY(iseq)->iseq_size);
1049 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
1054 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
1055 const void *addr = (
const void *)original_code[i];
1056 const int insn = rb_vm_insn_addr2insn(addr);
1058 original_code[i] = insn;
1059 i += insn_len(insn);
1065 v = rb_iseq_variable_ensure((rb_iseq_t *)iseq);
1066 VALUE *prev = ATOMIC_PTR_CAS(v->original_iseq,
1067 NULL, original_code);
1069 SIZED_FREE_N(original_code, ISEQ_BODY(iseq)->iseq_size);
1072 return original_code;
1079#if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
1080# define ALIGNMENT_SIZE SIZEOF_LONG_LONG
1082# define ALIGNMENT_SIZE SIZEOF_VALUE
1084#define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
1086#define ALIGNMENT_SIZE_OF(type) alignment_size_assert(RUBY_ALIGNOF(type), #type)
1089alignment_size_assert(
size_t align,
const char *
type)
1092 "ALIGNMENT_SIZE_OF(%s):%zd == (2 ** N) is expected",
type, align);
1098calc_padding(
void *ptr,
size_t align)
1103 mis = (size_t)ptr & (align - 1);
1105 padding = align - mis;
1116 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], align);
1119 if (storage->pos + size + padding > storage->size) {
1120 unsigned int alloc_size = storage->size;
1122 while (alloc_size < size + PADDING_SIZE_MAX) {
1126 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
1128 storage = *arena = storage->next;
1131 storage->size = alloc_size;
1132 padding = calc_padding((
void *)&storage->buff[storage->pos], align);
1135 storage->pos += (int)padding;
1137 ptr = (
void *)&storage->buff[storage->pos];
1138 storage->pos += (int)size;
1143compile_data_alloc(rb_iseq_t *iseq,
size_t size,
size_t align)
1146 return compile_data_alloc_with_arena(arena, size, align);
1149#define compile_data_alloc_type(iseq, type) \
1150 (type *)compile_data_alloc(iseq, sizeof(type), ALIGNMENT_SIZE_OF(type))
1153compile_data_alloc2(rb_iseq_t *iseq,
size_t elsize,
size_t num,
size_t align)
1156 return compile_data_alloc(iseq, size, align);
1159#define compile_data_alloc2_type(iseq, type, num) \
1160 (type *)compile_data_alloc2(iseq, sizeof(type), num, ALIGNMENT_SIZE_OF(type))
1163compile_data_calloc2(rb_iseq_t *iseq,
size_t elsize,
size_t num,
size_t align)
1166 void *p = compile_data_alloc(iseq, size, align);
1171#define compile_data_calloc2_type(iseq, type, num) \
1172 (type *)compile_data_calloc2(iseq, sizeof(type), num, ALIGNMENT_SIZE_OF(type))
1175compile_data_alloc_insn(rb_iseq_t *iseq)
1178 return (INSN *)compile_data_alloc_with_arena(arena,
sizeof(INSN), ALIGNMENT_SIZE_OF(INSN));
1182compile_data_alloc_label(rb_iseq_t *iseq)
1184 return compile_data_alloc_type(iseq, LABEL);
1188compile_data_alloc_adjust(rb_iseq_t *iseq)
1190 return compile_data_alloc_type(iseq, ADJUST);
1194compile_data_alloc_trace(rb_iseq_t *iseq)
1196 return compile_data_alloc_type(iseq, TRACE);
1203ELEM_INSERT_NEXT(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
1205 elem2->next = elem1->next;
1206 elem2->prev = elem1;
1207 elem1->next = elem2;
1209 elem2->next->prev = elem2;
1217ELEM_INSERT_PREV(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
1219 elem2->prev = elem1->prev;
1220 elem2->next = elem1;
1221 elem1->prev = elem2;
1223 elem2->prev->next = elem2;
1231ELEM_REPLACE(LINK_ELEMENT *elem1, LINK_ELEMENT *elem2)
1233 elem2->prev = elem1->prev;
1234 elem2->next = elem1->next;
1236 elem1->prev->next = elem2;
1239 elem1->next->prev = elem2;
1244ELEM_REMOVE(LINK_ELEMENT *elem)
1246 elem->prev->next = elem->next;
1248 elem->next->prev = elem->prev;
1256ELEM_SWAP(LINK_ELEMENT *first, LINK_ELEMENT *second)
1261 first->prev->next = second;
1262 second->next->prev = first;
1264 first->next = second->next;
1265 second->next = first;
1267 second->prev = first->prev;
1268 first->prev = second;
1270 if (IS_INSN(first) && IS_INSN(second)) {
1271 INSN *first_insn = (INSN*)first;
1272 INSN *second_insn = (INSN*)second;
1276 if (first_insn->insn_info.line_no == second_insn->insn_info.line_no) {
1280 first_insn->insn_info.events = (first_insn->insn_info.events & ~mask) | second_events;
1281 second_insn->insn_info.events = (second_insn->insn_info.events & ~mask) | first_events;
1286static LINK_ELEMENT *
1287FIRST_ELEMENT(
const LINK_ANCHOR *
const anchor)
1289 return anchor->anchor.next;
1292static LINK_ELEMENT *
1293LAST_ELEMENT(LINK_ANCHOR *
const anchor)
1295 return anchor->last;
1298static LINK_ELEMENT *
1299ELEM_FIRST_INSN(LINK_ELEMENT *elem)
1302 switch (elem->type) {
1303 case ISEQ_ELEMENT_INSN:
1304 case ISEQ_ELEMENT_ADJUST:
1314LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1316 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1317 if (first_insn != NULL &&
1318 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1327LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1329 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1345APPEND_LIST(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anc1, LINK_ANCHOR *
const anc2)
1347 if (anc2->anchor.next) {
1350 anc1->last->next = anc2->anchor.next;
1351 anc2->anchor.next->prev = anc1->last;
1352 anc1->last = anc2->last;
1357 verify_list(
"append", anc1);
1360#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1365debug_list(ISEQ_ARG_DECLARE LINK_ANCHOR *
const anchor, LINK_ELEMENT *cur)
1367 LINK_ELEMENT *list = FIRST_ELEMENT(anchor);
1369 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1370 (
void *)anchor->anchor.next, (
void *)anchor->last);
1372 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1373 (
void *)list->prev, (
int)list->type);
1378 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1379 verify_list(
"debug list", anchor);
1382#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1385#define debug_list(anc, cur) ((void)0)
1391 TRACE *trace = compile_data_alloc_trace(iseq);
1393 trace->link.type = ISEQ_ELEMENT_TRACE;
1394 trace->link.next = NULL;
1395 trace->event = event;
1402new_label_body(rb_iseq_t *iseq,
long line)
1404 LABEL *labelobj = compile_data_alloc_label(iseq);
1406 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1407 labelobj->link.next = 0;
1409 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1410 labelobj->sc_state = 0;
1412 labelobj->refcnt = 0;
1414 labelobj->rescued = LABEL_RESCUE_NONE;
1415 labelobj->unremovable = 0;
1416 labelobj->position = -1;
1421new_adjust_body(rb_iseq_t *iseq, LABEL *label,
int line)
1423 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1424 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1425 adjust->link.next = 0;
1426 adjust->label = label;
1427 adjust->line_no = line;
1428 LABEL_UNREMOVABLE(label);
1433iseq_insn_each_markable_object(INSN *insn,
void (*func)(
VALUE *,
VALUE),
VALUE data)
1435 const char *types = insn_op_types(insn->insn_id);
1436 for (
int j = 0; types[j]; j++) {
1437 char type = types[j];
1443 func(&OPERAND_AT(insn, j), data);
1452iseq_insn_each_object_write_barrier(
VALUE * obj,
VALUE iseq)
1461new_insn_core(rb_iseq_t *iseq,
int line_no,
int node_id,
int insn_id,
int argc,
VALUE *argv)
1463 INSN *iobj = compile_data_alloc_insn(iseq);
1467 iobj->link.type = ISEQ_ELEMENT_INSN;
1468 iobj->link.next = 0;
1469 iobj->insn_id = insn_id;
1470 iobj->insn_info.line_no = line_no;
1471 iobj->insn_info.node_id = node_id;
1472 iobj->insn_info.events = 0;
1473 iobj->operands = argv;
1474 iobj->operand_size = argc;
1477 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1483new_insn_body(rb_iseq_t *iseq,
int line_no,
int node_id,
enum ruby_vminsn_type insn_id,
int argc, ...)
1485 VALUE *operands = 0;
1489 va_start(argv, argc);
1490 operands = compile_data_alloc2_type(iseq,
VALUE, argc);
1491 for (i = 0; i < argc; i++) {
1497 return new_insn_core(iseq, line_no, node_id, insn_id, argc, operands);
1501insn_replace_with_operands(rb_iseq_t *iseq, INSN *iobj,
enum ruby_vminsn_type insn_id,
int argc, ...)
1503 VALUE *operands = 0;
1507 va_start(argv, argc);
1508 operands = compile_data_alloc2_type(iseq,
VALUE, argc);
1509 for (i = 0; i < argc; i++) {
1516 iobj->insn_id = insn_id;
1517 iobj->operand_size = argc;
1518 iobj->operands = operands;
1519 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1525new_callinfo(rb_iseq_t *iseq,
ID mid,
int argc,
unsigned int flag,
struct rb_callinfo_kwarg *kw_arg,
int has_blockiseq)
1527 VM_ASSERT(argc >= 0);
1530 flag |= VM_CALL_KWARG;
1531 argc += kw_arg->keyword_len;
1534 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KWARG | VM_CALL_KW_SPLAT | VM_CALL_FORWARDING))
1535 && !has_blockiseq) {
1536 flag |= VM_CALL_ARGS_SIMPLE;
1539 ISEQ_BODY(iseq)->ci_size++;
1540 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1546new_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)
1548 VALUE *operands = compile_data_calloc2_type(iseq,
VALUE, 2);
1551 operands[1] = (
VALUE)blockiseq;
1558 if (vm_ci_flag((
struct rb_callinfo *)ci) & VM_CALL_FORWARDING) {
1559 insn = new_insn_core(iseq, line_no, node_id, BIN(sendforward), 2, operands);
1562 insn = new_insn_core(iseq, line_no, node_id, BIN(send), 2, operands);
1571new_child_iseq(rb_iseq_t *iseq,
const NODE *
const node,
1572 VALUE name,
const rb_iseq_t *parent,
enum rb_iseq_type
type,
int line_no)
1574 rb_iseq_t *ret_iseq;
1575 VALUE ast_value = rb_ruby_ast_new(node);
1579 if (ISEQ_BODY(iseq)->source_hash) {
1580 rb_ast_t *child_ast = rb_ruby_ast_data_get(ast_value);
1581 child_ast->body.source_hash = ISEQ_BODY(iseq)->source_hash;
1582 child_ast->body.has_source_hash = 1;
1585 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1586 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1587 ret_iseq = rb_iseq_new_with_opt(ast_value, name,
1588 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1590 isolated_depth ? isolated_depth + 1 : 0,
1591 type, ISEQ_COMPILE_DATA(iseq)->option,
1592 ISEQ_SCRIPT_LINES(iseq));
1593 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1599 VALUE name,
const rb_iseq_t *parent,
enum rb_iseq_type
type,
int line_no)
1601 rb_iseq_t *ret_iseq;
1603 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1604 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1605 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1606 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1607 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1612set_catch_except_p(rb_iseq_t *iseq)
1615 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1616 if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1617 if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1618 set_catch_except_p((rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1641 while (pos < body->iseq_size) {
1642 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1643 if (insn == BIN(
throw)) {
1644 set_catch_except_p(iseq);
1647 pos += insn_len(insn);
1653 for (i = 0; i < ct->size; i++) {
1655 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1656 if (entry->type != CATCH_TYPE_BREAK
1657 && entry->type != CATCH_TYPE_NEXT
1658 && entry->type != CATCH_TYPE_REDO) {
1660 ISEQ_COMPILE_DATA(iseq)->catch_except_p =
true;
1667iseq_insert_nop_between_end_and_cont(rb_iseq_t *iseq)
1669 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1670 if (
NIL_P(catch_table_ary))
return;
1671 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1673 for (i = 0; i < tlen; i++) {
1675 LINK_ELEMENT *end = (LINK_ELEMENT *)(ptr[2] & ~1);
1676 LINK_ELEMENT *cont = (LINK_ELEMENT *)(ptr[4] & ~1);
1679 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1681 if (ct != CATCH_TYPE_BREAK
1682 && ct != CATCH_TYPE_NEXT
1683 && ct != CATCH_TYPE_REDO) {
1685 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1687 INSN *nop = new_insn_core(iseq, 0, -1, BIN(nop), 0, 0);
1688 ELEM_INSERT_NEXT(end, &nop->link);
1699iseq_setup_insn(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
1701 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1706 if (compile_debug > 5)
1707 dump_disasm_list(FIRST_ELEMENT(anchor));
1709 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1710 iseq_optimize(iseq, anchor);
1712 if (compile_debug > 5)
1713 dump_disasm_list(FIRST_ELEMENT(anchor));
1715 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1716 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1717 iseq_insns_unification(iseq, anchor);
1718 if (compile_debug > 5)
1719 dump_disasm_list(FIRST_ELEMENT(anchor));
1722 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1723 iseq_insert_nop_between_end_and_cont(iseq);
1724 if (compile_debug > 5)
1725 dump_disasm_list(FIRST_ELEMENT(anchor));
1731iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
1733 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1736 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1737 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1738 if (compile_debug > 5)
1739 dump_disasm_list(FIRST_ELEMENT(anchor));
1741 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1742 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1744 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1745 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1747 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1748 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1750 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1752 update_catch_except_flags(iseq, ISEQ_BODY(iseq));
1754 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1756 if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1757 ruby_xfree_sized(ISEQ_BODY(iseq)->catch_table, iseq_catch_table_bytes(ISEQ_BODY(iseq)->catch_table->size));
1758 ISEQ_BODY(iseq)->catch_table = NULL;
1761 if (compile_debug > 1) {
1762 VALUE str = rb_iseq_disasm(iseq);
1765 verify_call_cache(iseq);
1766 debugs(
"[compile step: finish]\n");
1772iseq_set_exception_local_table(rb_iseq_t *iseq)
1774 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1775 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1776 ISEQ_BODY(iseq)->lvar_states.list = NULL;
1781get_lvar_level(
const rb_iseq_t *iseq)
1784 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1786 iseq = ISEQ_BODY(iseq)->parent_iseq;
1792get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1796 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1797 if (ISEQ_BODY(iseq)->local_table[i] ==
id) {
1805get_local_var_idx(
const rb_iseq_t *iseq,
ID id)
1807 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1810 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1811 "get_local_var_idx: %d", idx);
1818get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1820 int lv = 0, idx = -1;
1821 const rb_iseq_t *
const topmost_iseq = iseq;
1824 idx = get_dyna_var_idx_at_raw(iseq,
id);
1828 iseq = ISEQ_BODY(iseq)->parent_iseq;
1833 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1834 "get_dyna_var_idx: -1");
1838 *ls = ISEQ_BODY(iseq)->local_table_size;
1843iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1847 iseq = ISEQ_BODY(iseq)->parent_iseq;
1850 body = ISEQ_BODY(iseq);
1851 if (body->local_iseq == iseq &&
1852 body->param.flags.has_block &&
1853 body->local_table_size - body->param.block_start == idx) {
1862iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1865 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1866 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1877access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1879 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1881 if (isolated_depth && level >= isolated_depth) {
1882 if (
id == rb_intern(
"yield")) {
1883 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1886 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable '%s' from isolated Proc", rb_id2name(
id));
1890 for (
int i=0; i<level; i++) {
1892 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1895 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1898 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1899 if (write && !val) {
1900 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1904 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1907 iseq = ISEQ_BODY(iseq)->parent_iseq;
1912iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1914 for (
int i=0; i<level; i++) {
1915 iseq = ISEQ_BODY(iseq)->parent_iseq;
1918 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1924update_lvar_state(
const rb_iseq_t *iseq,
int level,
int idx)
1926 for (
int i=0; i<level; i++) {
1927 iseq = ISEQ_BODY(iseq)->parent_iseq;
1930 uint8_t *states = iseq_lvar_states(ISEQ_BODY(iseq));
1931 int table_idx = ISEQ_BODY(iseq)->local_table_size - idx;
1932 switch (iseq_lvar_state_get(states, table_idx)) {
1933 case lvar_uninitialized:
1934 iseq_lvar_state_set(states, table_idx, lvar_initialized);
1936 case lvar_initialized:
1937 iseq_lvar_state_set(states, table_idx, lvar_reassigned);
1939 case lvar_reassigned:
1943 rb_bug(
"unreachable");
1948iseq_set_parameters_lvar_state(
const rb_iseq_t *iseq)
1950 uint8_t *states = iseq_lvar_states(ISEQ_BODY(iseq));
1952 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->param.size; i++) {
1953 iseq_lvar_state_set(states, i, lvar_initialized);
1956 int lead_num = ISEQ_BODY(iseq)->param.lead_num;
1957 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
1958 for (
int i=0; i<opt_num; i++) {
1959 iseq_lvar_state_set(states, lead_num + i, lvar_uninitialized);
1966iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level)
1968 if (iseq_local_block_param_p(iseq, idx, level)) {
1969 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1972 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1974 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1978iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level)
1980 if (iseq_local_block_param_p(iseq, idx, level)) {
1981 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1984 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1986 update_lvar_state(iseq, level, idx);
1987 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
1993iseq_calc_param_size(rb_iseq_t *iseq)
1996 if (body->param.flags.has_opt ||
1997 body->param.flags.has_post ||
1998 body->param.flags.has_rest ||
1999 body->param.flags.has_block ||
2000 body->param.flags.has_kw ||
2001 body->param.flags.has_kwrest) {
2003 if (body->param.flags.has_block) {
2004 body->param.size = body->param.block_start + 1;
2006 else if (body->param.flags.has_kwrest) {
2007 body->param.size = body->param.keyword->rest_start + 1;
2009 else if (body->param.flags.has_kw) {
2010 body->param.size = body->param.keyword->bits_start + 1;
2012 else if (body->param.flags.has_post) {
2013 body->param.size = rb_iseq_param_post_start(body) + body->param.post_num;
2015 else if (body->param.flags.has_rest) {
2016 body->param.size = rb_iseq_param_rest_start(body) + 1;
2018 else if (body->param.flags.has_opt) {
2019 body->param.size = body->param.lead_num + body->param.opt_num;
2026 body->param.size = body->param.lead_num;
2031iseq_set_arguments_keywords(rb_iseq_t *iseq, LINK_ANCHOR *
const optargs,
2034 const rb_node_kw_arg_t *node = args->kw_args;
2036 struct rb_iseq_param_keyword *keyword;
2039 int kw = 0, rkw = 0, i;
2041 body->param.flags.has_kw = TRUE;
2042 body->param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2046 if (get_nd_value(node->nd_body) == NODE_SPECIAL_REQUIRED_KEYWORD) {
2049 node = node->nd_next;
2051 if (kw > VM_CALL_KW_LEN_MAX) {
2052 COMPILE_ERROR(ERROR_ARGS_AT(RNODE(args->kw_args))
"too many keyword parameters (%d, maximum is %d)",
2053 kw, (
int)VM_CALL_KW_LEN_MAX);
2056 keyword->bits_start = arg_size++;
2058 keyword->required_num = rkw;
2059 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
2061 if (RNODE_DVAR(args->kw_rest_arg)->nd_vid != 0) {
2062 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2063 keyword->rest_start = arg_size++;
2064 body->param.flags.has_kwrest = TRUE;
2066 if (kw_id == idPow) body->param.flags.anon_kwrest = TRUE;
2069 node = args->kw_args;
2071 const NODE *val_node = get_nd_value(node->nd_body);
2074 if (val_node != NODE_SPECIAL_REQUIRED_KEYWORD) {
2075 switch (nd_type(val_node)) {
2077 dv = rb_node_sym_string_val(val_node);
2080 dv = rb_node_regx_string_val(val_node);
2083 dv = rb_node_line_lineno_val(val_node);
2086 dv = rb_node_integer_literal_val(val_node);
2089 dv = rb_node_float_literal_val(val_node);
2092 dv = rb_node_rational_literal_val(val_node);
2094 case NODE_IMAGINARY:
2095 dv = rb_node_imaginary_literal_val(val_node);
2098 dv = rb_node_encoding_val(val_node);
2110 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", RNODE(node)));
2117 node = node->nd_next;
2123 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
2125 if (dv == complex_mark) dv =
Qundef;
2130 keyword->default_values = dvs;
2136iseq_set_use_block(rb_iseq_t *iseq)
2139 if (!body->param.flags.use_block) {
2140 body->param.flags.use_block = 1;
2142 rb_vm_t *vm = GET_VM();
2145 st_data_t key = (st_data_t)rb_intern_str(body->location.label);
2146 set_insert(&vm->unused_block_warning_table, key);
2152iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *
const optargs,
const NODE *
const node_args)
2154 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
2158 const struct rb_args_info *
const args = &RNODE_ARGS(node_args)->nd_ainfo;
2164 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
2166 body->param.lead_num = arg_size = (int)args->pre_args_num;
2167 if (body->param.lead_num > 0) body->param.flags.has_lead = TRUE;
2168 debugs(
" - argc: %d\n", body->param.lead_num);
2170 rest_id = args->rest_arg;
2171 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
2175 block_id = args->block_arg;
2177 bool optimized_forward = (args->forwarding && args->pre_args_num == 0 && !args->opt_args);
2179 if (optimized_forward) {
2184 if (args->opt_args) {
2185 const rb_node_opt_arg_t *node = args->opt_args;
2192 label = NEW_LABEL(nd_line(RNODE(node)));
2194 ADD_LABEL(optargs, label);
2195 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
2196 node = node->nd_next;
2201 label = NEW_LABEL(nd_line(node_args));
2203 ADD_LABEL(optargs, label);
2208 for (j = 0; j < i+1; j++) {
2213 body->param.flags.has_opt = TRUE;
2214 body->param.opt_num = i;
2215 body->param.opt_table = opt_table;
2220 RUBY_ASSERT(rb_iseq_param_rest_start(body) == arg_size);
2222 body->param.flags.has_rest = TRUE;
2223 if (rest_id ==
'*') body->param.flags.anon_rest = TRUE;
2226 if (args->first_post_arg) {
2227 RUBY_ASSERT(rb_iseq_param_post_start(body) == arg_size);
2228 body->param.post_num = args->post_args_num;
2229 body->param.flags.has_post = TRUE;
2230 arg_size += args->post_args_num;
2233 if (args->kw_args) {
2234 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
2236 else if (args->kw_rest_arg && !optimized_forward) {
2237 ID kw_id = ISEQ_BODY(iseq)->local_table[arg_size];
2238 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
2239 keyword->rest_start = arg_size++;
2240 body->param.keyword = keyword;
2241 body->param.flags.has_kwrest = TRUE;
2243 static ID anon_kwrest = 0;
2244 if (!anon_kwrest) anon_kwrest = rb_intern(
"**");
2245 if (kw_id == anon_kwrest) body->param.flags.anon_kwrest = TRUE;
2247 else if (args->no_kwarg) {
2248 body->param.flags.accepts_no_kwarg = TRUE;
2251 if (args->no_blockarg) {
2252 body->param.flags.accepts_no_block = TRUE;
2254 else if (block_id) {
2255 body->param.block_start = arg_size++;
2256 body->param.flags.has_block = TRUE;
2257 iseq_set_use_block(iseq);
2261 if (optimized_forward) {
2262 body->param.flags.use_block = 1;
2263 body->param.flags.forwardable = TRUE;
2267 iseq_calc_param_size(iseq);
2268 body->param.size = arg_size;
2270 if (args->pre_init) {
2271 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
2273 if (args->post_init) {
2274 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
2277 if (body->type == ISEQ_TYPE_BLOCK) {
2278 if (body->param.flags.has_opt == FALSE &&
2279 body->param.flags.has_post == FALSE &&
2280 body->param.flags.has_rest == FALSE &&
2281 body->param.flags.has_kw == FALSE &&
2282 body->param.flags.has_kwrest == FALSE) {
2284 if (body->param.lead_num == 1 && last_comma == 0) {
2286 body->param.flags.ambiguous_param0 = TRUE;
2296iseq_set_local_table(rb_iseq_t *iseq,
const rb_ast_id_table_t *tbl,
const NODE *
const node_args)
2298 unsigned int size = tbl ? tbl->size : 0;
2299 unsigned int offset = 0;
2302 struct rb_args_info *args = &RNODE_ARGS(node_args)->nd_ainfo;
2307 if (args->forwarding && args->pre_args_num == 0 && !args->opt_args) {
2316 MEMCPY(ids, tbl->ids + offset,
ID, size);
2317 ISEQ_BODY(iseq)->local_table = ids;
2319 if (ISEQ_LVAR_STATES_EMBED_P(size)) {
2321 memset(ISEQ_BODY(iseq)->lvar_states.single, 0,
sizeof(ISEQ_BODY(iseq)->lvar_states.single));
2324 ISEQ_BODY(iseq)->lvar_states.list =
ZALLOC_N(uint8_t, ISEQ_LVAR_STATES_BUFLEN(size));
2327 ISEQ_BODY(iseq)->local_table_size = size;
2329 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
2341 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2344 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2347 else if (tlit != tval) {
2357 long x =
FIX2LONG(rb_big_cmp(lit, val));
2365 return rb_float_cmp(lit, val);
2368 const struct RRational *rat1 = RRATIONAL(val);
2369 const struct RRational *rat2 = RRATIONAL(lit);
2370 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2373 const struct RComplex *comp1 = RCOMPLEX(val);
2374 const struct RComplex *comp2 = RCOMPLEX(lit);
2375 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2378 return rb_reg_equal(val, lit) ? 0 : -1;
2386rb_iseq_cdhash_hash(
VALUE a)
2388 switch (OBJ_BUILTIN_TYPE(a)) {
2391 return (st_index_t)a;
2399 return rb_rational_hash(a);
2401 return rb_complex_hash(a);
2409static const struct st_hash_type cdhash_type = {
2411 rb_iseq_cdhash_hash,
2415cdhash_new(
size_t size)
2417 VALUE cdhash = rb_imemo_cdhash_new(size, &cdhash_type);
2425 st_table *tbl = rb_imemo_cdhash_tbl(cdhash);
2426 st_insert(tbl, key, val);
2433 st_table *tbl = rb_imemo_cdhash_tbl(cdhash);
2435 if (!st_lookup(tbl, key, &dontcare)) {
2436 st_insert(tbl, key, val);
2448cdhash_set_label_check_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
2454cdhash_set_label_replace_i(st_data_t *key, st_data_t *value, st_data_t ptr,
int existing)
2457 LABEL *lobj = (LABEL *)(*value & ~1);
2458 *value = lobj->position - (data->pos+data->len);
2463get_ivar_ic_value(rb_iseq_t *iseq,
ID id)
2465 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2469get_cvar_ic_value(rb_iseq_t *iseq,
ID id)
2472 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2474 if (rb_id_table_lookup(tbl,
id,&val)) {
2479 tbl = rb_id_table_create(1);
2480 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2482 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2483 rb_id_table_insert(tbl,
id,val);
2487#define BADINSN_DUMP(anchor, list, dest) \
2488 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2490#define BADINSN_ERROR \
2491 (SIZED_FREE_N(generated_iseq, generated_iseq_size), \
2492 BADINSN_DUMP(anchor, list, NULL), \
2496fix_sp_depth(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
2498 int stack_max = 0, sp = 0, line = 0;
2501 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2502 if (IS_LABEL(list)) {
2503 LABEL *lobj = (LABEL *)list;
2508 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2509 switch (list->type) {
2510 case ISEQ_ELEMENT_INSN:
2515 INSN *iobj = (INSN *)list;
2518 sp = calc_sp_depth(sp, iobj);
2520 BADINSN_DUMP(anchor, list, NULL);
2521 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2522 "argument stack underflow (%d)", sp);
2525 if (sp > stack_max) {
2529 line = iobj->insn_info.line_no;
2531 operands = iobj->operands;
2532 insn = iobj->insn_id;
2533 types = insn_op_types(insn);
2534 len = insn_len(insn);
2537 if (iobj->operand_size !=
len - 1) {
2539 BADINSN_DUMP(anchor, list, NULL);
2540 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2541 "operand size miss! (%d for %d)",
2542 iobj->operand_size,
len - 1);
2546 for (j = 0; types[j]; j++) {
2547 if (types[j] == TS_OFFSET) {
2549 LABEL *lobj = (LABEL *)operands[j];
2551 BADINSN_DUMP(anchor, list, NULL);
2552 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2553 "unknown label: "LABEL_FORMAT, lobj->label_no);
2556 if (lobj->sp == -1) {
2559 else if (lobj->sp != sp) {
2560 VALUE path = rb_iseq_path(iseq);
2561 debugs(
"%.*s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2563 lobj->label_no, lobj->sp, sp);
2569 case ISEQ_ELEMENT_LABEL:
2571 LABEL *lobj = (LABEL *)list;
2572 if (lobj->sp == -1) {
2576 if (lobj->sp != sp) {
2577 VALUE path = rb_iseq_path(iseq);
2578 debugs(
"%.*s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2580 lobj->label_no, lobj->sp, sp);
2586 case ISEQ_ELEMENT_TRACE:
2591 case ISEQ_ELEMENT_ADJUST:
2593 ADJUST *adjust = (ADJUST *)list;
2596 sp = adjust->label ? adjust->label->sp : 0;
2597 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2598 BADINSN_DUMP(anchor, list, NULL);
2599 COMPILE_ERROR(iseq, adjust->line_no,
2600 "iseq_set_sequence: adjust bug %d < %d",
2607 BADINSN_DUMP(anchor, list, NULL);
2608 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2616add_node_id(uint8_t *node_ids,
unsigned int *node_ids_size,
int node_id,
int *prev_node_id)
2618 int delta = node_id - *prev_node_id;
2619 uint32_t zz = ((uint32_t)delta << 1) ^ (uint32_t)(delta >> 31);
2620 *prev_node_id = node_id;
2622 uint8_t
byte = zz & 0x7f;
2624 node_ids[(*node_ids_size)++] = zz ? (
byte | 0x80) : byte;
2630 int insns_info_index,
int code_index,
const INSN *iobj)
2632 if (insns_info_index == 0 ||
2633 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2634 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2635 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2636 insns_info[insns_info_index].events = iobj->insn_info.events;
2637 positions[insns_info_index] = code_index;
2645 int insns_info_index,
int code_index,
const ADJUST *adjust)
2647 insns_info[insns_info_index].line_no = adjust->line_no;
2648 insns_info[insns_info_index].events = 0;
2649 positions[insns_info_index] = code_index;
2654 rb_const_path_t path;
2659dedup_const_path_ics(
struct ic_site *sites,
unsigned int count)
2661 unsigned int distinct = 0;
2664 for (
unsigned int i = 0; i < count; i++) {
2666 for (j = 0; j < i; j++) {
2667 if (sites[i].path == sites[j].path) {
2673 sites[i].index = distinct++;
2676 sites[i].index = sites[j].index;
2682 st_init_existing_numtable_with_size(&table, count);
2684 for (
unsigned int i = 0; i < count; i++) {
2686 if (!st_lookup(&table, (st_data_t)sites[i].path, &index)) {
2688 st_insert(&table, (st_data_t)sites[i].path, index);
2690 sites[i].index = (
unsigned int)index;
2692 st_free_embedded_table(&table);
2702iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
2707 VALUE *generated_iseq;
2711 int insn_num, code_index, insns_info_index, sp = 0;
2712 VALUE ic_site_buf = 0;
2713 unsigned int ic_site_count = 0, ic_site_cursor = 0;
2715 int stack_max = fix_sp_depth(iseq, anchor);
2717 if (stack_max < 0)
return COMPILE_NG;
2720 insn_num = code_index = 0;
2721 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2722 switch (list->type) {
2723 case ISEQ_ELEMENT_INSN:
2725 INSN *iobj = (INSN *)list;
2727 sp = calc_sp_depth(sp, iobj);
2729 if (iobj->insn_id == BIN(opt_getconstant_path)) {
2730 if (UNLIKELY(ic_site_count >= body->ic_size)) {
2731 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2732 "iseq_set_sequence: ic_index overflow: size: %d", body->ic_size);
2735 ic_sites[ic_site_count++].path = (rb_const_path_t)OPERAND_AT(iobj, 0);
2737 events = iobj->insn_info.events |= events;
2738 if (ISEQ_COVERAGE(iseq)) {
2739 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2740 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2741 int line = iobj->insn_info.line_no - 1;
2742 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2746 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2747 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2753 code_index += insn_data_length(iobj);
2758 case ISEQ_ELEMENT_LABEL:
2760 LABEL *lobj = (LABEL *)list;
2761 lobj->position = code_index;
2762 if (lobj->sp != sp) {
2763 VALUE path = rb_iseq_path(iseq);
2764 debugs(
"%.*s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2766 lobj->label_no, lobj->sp, sp);
2771 case ISEQ_ELEMENT_TRACE:
2773 TRACE *trace = (TRACE *)list;
2774 events |= trace->event;
2775 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2778 case ISEQ_ELEMENT_ADJUST:
2780 ADJUST *adjust = (ADJUST *)list;
2784 sp = adjust->label ? adjust->label->sp : 0;
2785 if (adjust->line_no != -1 && orig_sp > sp) {
2786 if (orig_sp - sp > 1) code_index++;
2796 body->ic_size = dedup_const_path_ics(ic_sites, ic_site_count);
2799 const int generated_iseq_size = code_index;
2802 VALUE insns_info_buf;
2805 VALUE positions_buf;
2806 unsigned int *positions =
ALLOCV_N(
unsigned int, positions_buf, insn_num);
2808 uint8_t *node_ids =
ALLOCV_N(uint8_t, node_ids_buf, (
size_t)insn_num * 5);
2809 unsigned int node_ids_size = 0;
2810 int prev_node_id = 0;
2811 if (ISEQ_IS_SIZE(body)) {
2815 body->is_entries = NULL;
2818 if (body->ci_size) {
2822 body->call_data = NULL;
2824 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2831 iseq_bits_t * mark_offset_bits;
2832 int code_size = code_index;
2834 bool needs_bitmap =
false;
2836 const size_t mark_offset_bits_size = ISEQ_MBITS_BUFLEN(code_index);
2837 if (mark_offset_bits_size == 1) {
2838 mark_offset_bits = &ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
2839 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
true;
2842 mark_offset_bits =
ZALLOC_N(iseq_bits_t, mark_offset_bits_size);
2843 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = mark_offset_bits;
2844 ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit =
false;
2847 ISEQ_COMPILE_DATA(iseq)->iseq_encoded = (
void *)generated_iseq;
2848 ISEQ_COMPILE_DATA(iseq)->iseq_size = code_index;
2850 list = FIRST_ELEMENT(anchor);
2851 insns_info_index = code_index = sp = 0;
2854 switch (list->type) {
2855 case ISEQ_ELEMENT_INSN:
2860 INSN *iobj = (INSN *)list;
2863 sp = calc_sp_depth(sp, iobj);
2865 operands = iobj->operands;
2866 insn = iobj->insn_id;
2867 generated_iseq[code_index] = insn;
2868 types = insn_op_types(insn);
2869 len = insn_len(insn);
2871 for (j = 0; types[j]; j++) {
2872 char type = types[j];
2879 LABEL *lobj = (LABEL *)operands[j];
2880 generated_iseq[code_index + 1 + j] = lobj->position - (code_index +
len);
2885 VALUE map = operands[j];
2888 data.pos = code_index;
2890 st_foreach_with_replace(rb_imemo_cdhash_tbl(map), cdhash_set_label_check_i, cdhash_set_label_replace_i, (
VALUE)&data);
2892 generated_iseq[code_index + 1 + j] = map;
2893 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2895 needs_bitmap =
true;
2900 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2905 VALUE v = operands[j];
2906 generated_iseq[code_index + 1 + j] = v;
2910 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2911 needs_bitmap =
true;
2918 const struct ic_site *site = &ic_sites[ic_site_cursor++];
2919 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[site->index].ic_cache;
2920 ic->const_path = site->path;
2922 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2927 unsigned int ic_index =
FIX2UINT(operands[j]);
2929 IVC cache = ((IVC)&body->is_entries[ic_index]);
2931 if (insn == BIN(setinstancevariable)) {
2932 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2933 cache->value = IVAR_CACHE_INIT;
2936 cache->iv_set_name = 0;
2937 cache->value = rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET);
2942 unsigned int ic_index =
FIX2UINT(operands[j]);
2943 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2944 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2945 BADINSN_DUMP(anchor, &iobj->link, 0);
2946 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2947 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2948 ic_index, ISEQ_IS_SIZE(body));
2950 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2957 RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2958 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2960 cd->cc = vm_cc_empty();
2961 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2965 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2968 generated_iseq[code_index + 1 + j] = operands[j];
2971 generated_iseq[code_index + 1 + j] = operands[j];
2974 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2975 "unknown operand type: %c",
type);
2979 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2980 add_node_id(node_ids, &node_ids_size, iobj->insn_info.node_id, &prev_node_id);
2984 case ISEQ_ELEMENT_LABEL:
2986 LABEL *lobj = (LABEL *)list;
2987 if (lobj->sp != sp) {
2988 VALUE path = rb_iseq_path(iseq);
2989 debugs(
"%.*s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2991 lobj->label_no, lobj->sp, sp);
2996 case ISEQ_ELEMENT_ADJUST:
2998 ADJUST *adjust = (ADJUST *)list;
3001 if (adjust->label) {
3002 sp = adjust->label->sp;
3008 if (adjust->line_no != -1) {
3009 const int diff = orig_sp - sp;
3011 if (insns_info_index == 0) {
3012 COMPILE_ERROR(iseq, adjust->line_no,
3013 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
3015 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
3016 add_node_id(node_ids, &node_ids_size, -1, &prev_node_id);
3019 generated_iseq[code_index++] = BIN(adjuststack);
3020 generated_iseq[code_index++] = orig_sp - sp;
3022 else if (diff == 1) {
3023 generated_iseq[code_index++] = BIN(pop);
3025 else if (diff < 0) {
3026 int label_no = adjust->label ? adjust->label->label_no : -1;
3027 SIZED_FREE_N(generated_iseq, generated_iseq_size);
3028 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
3029 SIZED_FREE_N(mark_offset_bits, ISEQ_MBITS_BUFLEN(code_index));
3031 debug_list(anchor, list);
3032 COMPILE_ERROR(iseq, adjust->line_no,
3033 "iseq_set_sequence: adjust bug to %d %d < %d",
3034 label_no, orig_sp, sp);
3047 body->iseq_encoded = (
void *)generated_iseq;
3048 body->iseq_size = code_index;
3049 body->stack_max = stack_max;
3051 if (ISEQ_COMPILE_DATA(iseq)->is_single_mark_bit) {
3052 body->mark_bits.single = ISEQ_COMPILE_DATA(iseq)->mark_bits.single;
3056 body->mark_bits.list = mark_offset_bits;
3059 body->mark_bits.list = NULL;
3060 ISEQ_COMPILE_DATA(iseq)->mark_bits.list = NULL;
3061 SIZED_FREE_N(mark_offset_bits, mark_offset_bits_size);
3065 body->insns_info = rb_iseq_insn_info_new(body->iseq_size, insns_info, positions, insns_info_index, node_ids, node_ids_size);
3075label_get_position(LABEL *lobj)
3077 return lobj->position;
3081label_get_sp(LABEL *lobj)
3087iseq_set_exception_table(rb_iseq_t *iseq)
3089 const VALUE *tptr, *ptr;
3090 unsigned int tlen, i, table_size = 0;
3093 ISEQ_BODY(iseq)->catch_table = NULL;
3095 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
3096 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
3104 for (i = 0; i < table->size; i++) {
3106 int start = label_get_position((LABEL *)(ptr[1] & ~1));
3107 int end = label_get_position((LABEL *)(ptr[2] & ~1));
3110 if (start < 0 || end < 0) {
3111 if ((ptr[0] & 0xffff) != CATCH_TYPE_ENSURE)
continue;
3112 if (start < 0) start = 0;
3113 if (end < 0) end = 0;
3116 LABEL *lobj = (LABEL *)(ptr[4] & ~1);
3117 cont = label_get_position(lobj);
3118 if (cont < 0)
continue;
3119 sp = label_get_sp(lobj);
3122 entry = UNALIGNED_MEMBER_PTR(table, entries[table_size++]);
3123 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
3124 entry->start = (
unsigned int)start;
3125 entry->end = (
unsigned int)end;
3126 entry->iseq = (rb_iseq_t *)ptr[3];
3131 entry->cont = (
unsigned int)cont;
3135 if (entry->type == CATCH_TYPE_RESCUE ||
3136 entry->type == CATCH_TYPE_BREAK ||
3137 entry->type == CATCH_TYPE_NEXT) {
3146 table->size = table_size;
3147 if (table_size == 0) {
3148 ruby_xfree_sized(table, iseq_catch_table_bytes(tlen));
3151 else if (table_size < tlen) {
3152 table = ruby_xrealloc(table, iseq_catch_table_bytes(table_size));
3154 ISEQ_BODY(iseq)->catch_table = table;
3155 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
3173iseq_set_optargs_table(rb_iseq_t *iseq)
3176 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
3178 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
3179 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
3180 opt_table[i] = label_get_position((LABEL *)opt_table[i]);
3186static LINK_ELEMENT *
3187get_destination_insn(INSN *iobj)
3189 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, 0);
3193 list = lobj->link.next;
3195 switch (list->type) {
3196 case ISEQ_ELEMENT_INSN:
3197 case ISEQ_ELEMENT_ADJUST:
3199 case ISEQ_ELEMENT_LABEL:
3202 case ISEQ_ELEMENT_TRACE:
3204 TRACE *trace = (TRACE *)list;
3205 events |= trace->event;
3213 if (list && IS_INSN(list)) {
3214 INSN *iobj = (INSN *)list;
3215 iobj->insn_info.events |= events;
3220static LINK_ELEMENT *
3221get_next_insn(INSN *iobj)
3223 LINK_ELEMENT *list = iobj->link.next;
3226 if (IS_INSN(list) || IS_ADJUST(list)) {
3234static LINK_ELEMENT *
3235get_prev_insn(INSN *iobj)
3237 LINK_ELEMENT *list = iobj->link.prev;
3240 if (IS_INSN(list) || IS_ADJUST(list)) {
3249unref_destination(INSN *iobj,
int pos)
3251 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, pos);
3253 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
3257replace_destination(INSN *dobj, INSN *nobj)
3259 VALUE n = OPERAND_AT(nobj, 0);
3260 LABEL *dl = (LABEL *)OPERAND_AT(dobj, 0);
3261 LABEL *nl = (LABEL *)n;
3262 if (dl == nl)
return false;
3265 OPERAND_AT(dobj, 0) = n;
3266 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
3271find_destination(INSN *i)
3273 int pos,
len = insn_len(i->insn_id);
3274 for (pos = 0; pos <
len; ++pos) {
3275 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
3276 return (LABEL *)OPERAND_AT(i, pos);
3283 const unsigned int capacity;
3286 unsigned int *stamps;
3292 unsigned int no = (
unsigned int)lab->label_no;
3293 return unref->stamps[no] == unref->gen ? unref->counts[no] : 0;
3297unreachable_unref_increment(
const struct unreachable_unref *unref,
const LABEL *lab)
3299 unsigned int no = (
unsigned int)lab->label_no;
3300 if (unref->stamps[no] != unref->gen) {
3301 unref->stamps[no] = unref->gen;
3302 unref->counts[no] = 0;
3304 unref->counts[no]++;
3308remove_unreachable_chunk(rb_iseq_t *iseq,
struct unreachable_unref *unref, LINK_ELEMENT *i)
3310 LINK_ELEMENT *first = i, *end, *scan, *pending_end = 0;
3314 if (++unref->gen == 0) {
3316 MEMZERO(unref->stamps,
unsigned int, unref->capacity);
3324 if (IS_INSN(scan)) {
3325 if (IS_INSN_ID(scan, leave)) {
3330 else if ((lab = find_destination((INSN *)scan)) != 0) {
3331 unreachable_unref_increment(unref, lab);
3332 if (lab == pending && lab->refcnt <= unreachable_unref_count(unref, lab)) {
3337 else if (IS_LABEL(scan)) {
3338 lab = (LABEL *)scan;
3339 if (lab->unremovable) {
3343 if (lab->refcnt > unreachable_unref_count(unref, lab)) {
3346 pending_end = (scan == first) ? 0 : end;
3350 else if (IS_ADJUST(scan)) {
3354 if (!pending) end = scan;
3355 }
while ((scan = scan->next) != 0);
3358 if (!pending_end)
return 0;
3365 VALUE insn = INSN_OF(i);
3366 int pos,
len = insn_len(insn);
3367 for (pos = 0; pos <
len; ++pos) {
3368 switch (insn_op_types(insn)[pos]) {
3370 unref_destination((INSN *)i, pos);
3379 }
while ((i != end) && (i = i->next) != 0);
3384iseq_pop_newarray(rb_iseq_t *iseq, INSN *iobj)
3386 switch (OPERAND_AT(iobj, 0)) {
3388 ELEM_REMOVE(&iobj->link);
3391 ELEM_REMOVE(&iobj->link);
3394 iobj->insn_id = BIN(adjuststack);
3400is_frozen_putstring(INSN *insn,
VALUE *op)
3402 if (IS_INSN_ID(insn, dupstring) || IS_INSN_ID(insn, dupchilledstring)) {
3403 *op = OPERAND_AT(insn, 0);
3406 else if (IS_INSN_ID(insn, putobject)) {
3407 *op = OPERAND_AT(insn, 0);
3414insn_has_label_before(LINK_ELEMENT *elem)
3416 LINK_ELEMENT *prev = elem->prev;
3418 if (prev->type == ISEQ_ELEMENT_LABEL) {
3419 LABEL *label = (LABEL *)prev;
3420 if (label->refcnt > 0) {
3424 else if (prev->type == ISEQ_ELEMENT_INSN) {
3433optimize_checktype(rb_iseq_t *iseq, INSN *iobj)
3457 INSN *niobj, *ciobj, *dup = 0;
3461 switch (INSN_OF(iobj)) {
3462 case BIN(dupstring):
3463 case BIN(dupchilledstring):
3469 case BIN(putobject):
3472 default:
return FALSE;
3475 ciobj = (INSN *)get_next_insn(iobj);
3476 if (IS_INSN_ID(ciobj, jump)) {
3477 ciobj = (INSN *)get_next_insn((INSN*)OPERAND_AT(ciobj, 0));
3479 if (IS_INSN_ID(ciobj, dup)) {
3480 ciobj = (INSN *)get_next_insn(dup = ciobj);
3482 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
3483 niobj = (INSN *)get_next_insn(ciobj);
3488 switch (INSN_OF(niobj)) {
3490 if (OPERAND_AT(ciobj, 0) ==
type) {
3491 dest = (LABEL *)OPERAND_AT(niobj, 0);
3494 case BIN(branchunless):
3495 if (OPERAND_AT(ciobj, 0) !=
type) {
3496 dest = (LABEL *)OPERAND_AT(niobj, 0);
3502 line = ciobj->insn_info.line_no;
3503 node_id = ciobj->insn_info.node_id;
3505 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
3506 dest = (LABEL *)niobj->link.next;
3509 dest = NEW_LABEL(line);
3510 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
3513 INSERT_AFTER_INSN1(iobj, line, node_id, jump, dest);
3515 if (!dup) INSERT_AFTER_INSN(iobj, line, node_id, pop);
3520ci_flag_set(
const rb_iseq_t *iseq,
const struct rb_callinfo *ci,
unsigned int add)
3522 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3523 vm_ci_flag(ci) | add,
3531ci_argc_set(
const rb_iseq_t *iseq,
const struct rb_callinfo *ci,
int argc)
3533 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3541#define vm_ci_simple(ci) (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE)
3544iseq_reg_compile(rb_iseq_t *iseq,
VALUE str,
int options,
const char *sourcefile,
int sourceline)
3547 VALUE re = rb_reg_compile(str, options, sourcefile, sourceline);
3550 rb_set_errinfo(errinfo);
3551 COMPILE_ERROR(iseq, sourceline,
"%" PRIsVALUE, message);
3560iseq_peephole_optimize(rb_iseq_t *iseq,
struct unreachable_unref *unref, LINK_ELEMENT *list,
const int do_tailcallopt)
3562 INSN *
const iobj = (INSN *)list;
3565 optimize_checktype(iseq, iobj);
3567 if (IS_INSN_ID(iobj, jump)) {
3568 INSN *niobj, *diobj, *piobj;
3569 diobj = (INSN *)get_destination_insn(iobj);
3570 niobj = (INSN *)get_next_insn(iobj);
3572 if (diobj == niobj) {
3579 unref_destination(iobj, 0);
3580 ELEM_REMOVE(&iobj->link);
3583 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3584 IS_INSN_ID(diobj, jump) &&
3585 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3586 diobj->insn_info.events == 0) {
3597 if (replace_destination(iobj, diobj)) {
3598 remove_unreachable_chunk(iseq, unref, iobj->link.next);
3602 else if (IS_INSN_ID(diobj, leave)) {
3615 unref_destination(iobj, 0);
3616 iobj->insn_id = BIN(leave);
3617 iobj->operand_size = 0;
3618 iobj->insn_info = diobj->insn_info;
3621 else if (IS_INSN(iobj->link.prev) &&
3622 (piobj = (INSN *)iobj->link.prev) &&
3623 (IS_INSN_ID(piobj, branchif) ||
3624 IS_INSN_ID(piobj, branchunless))) {
3625 INSN *pdiobj = (INSN *)get_destination_insn(piobj);
3626 if (niobj == pdiobj) {
3627 int refcnt = IS_LABEL(piobj->link.next) ?
3628 ((LABEL *)piobj->link.next)->refcnt : 0;
3643 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3644 ? BIN(branchunless) : BIN(branchif);
3645 if (replace_destination(piobj, iobj) && refcnt <= 1) {
3646 ELEM_REMOVE(&iobj->link);
3653 else if (diobj == pdiobj) {
3667 INSN *popiobj = new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, BIN(pop), 0, 0);
3668 ELEM_REPLACE(&piobj->link, &popiobj->link);
3671 if (remove_unreachable_chunk(iseq, unref, iobj->link.next)) {
3685 if (IS_INSN_ID(iobj, newrange)) {
3686 INSN *
const range = iobj;
3688 VALUE str_beg, str_end;
3690 if ((end = (INSN *)get_prev_insn(range)) != 0 &&
3691 is_frozen_putstring(end, &str_end) &&
3692 (beg = (INSN *)get_prev_insn(end)) != 0 &&
3693 is_frozen_putstring(beg, &str_beg) &&
3694 !(insn_has_label_before(&beg->link) || insn_has_label_before(&end->link))) {
3695 int excl =
FIX2INT(OPERAND_AT(range, 0));
3698 ELEM_REMOVE(&beg->link);
3699 ELEM_REMOVE(&end->link);
3700 range->insn_id = BIN(putobject);
3701 OPERAND_AT(range, 0) = lit_range;
3706 if (IS_INSN_ID(iobj, leave)) {
3707 remove_unreachable_chunk(iseq, unref, iobj->link.next);
3719 if (IS_INSN_ID(iobj, duparray)) {
3720 LINK_ELEMENT *next = iobj->link.next;
3721 if (IS_INSN(next) && (IS_INSN_ID(next, concatarray) || IS_INSN_ID(next, concattoarray))) {
3722 iobj->insn_id = BIN(putobject);
3732 if (IS_INSN_ID(iobj, duparray)) {
3733 LINK_ELEMENT *next = iobj->link.next;
3734 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3736 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3738 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3739 VALUE ary = iobj->operands[0];
3742 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, ary, (
VALUE)ci);
3754 if (IS_INSN_ID(iobj, duphash)) {
3755 LINK_ELEMENT *next = iobj->link.next;
3756 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3758 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3760 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3761 VALUE hash = iobj->operands[0];
3765 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, hash, (
VALUE)ci);
3777 if (IS_INSN_ID(iobj, newarray) && iobj->operands[0] ==
INT2FIX(0)) {
3778 LINK_ELEMENT *next = iobj->link.next;
3779 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3781 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3783 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3784 insn_replace_with_operands(iseq, iobj, BIN(opt_ary_freeze), 2, rb_cArray_empty_frozen, (
VALUE)ci);
3796 if (IS_INSN_ID(iobj, newhash) && iobj->operands[0] ==
INT2FIX(0)) {
3797 LINK_ELEMENT *next = iobj->link.next;
3798 if (IS_INSN(next) && (IS_INSN_ID(next, send))) {
3800 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(next, 1);
3802 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && blockiseq == NULL && vm_ci_mid(ci) == idFreeze) {
3803 insn_replace_with_operands(iseq, iobj, BIN(opt_hash_freeze), 2, rb_cHash_empty_frozen, (
VALUE)ci);
3809 if (IS_INSN_ID(iobj, branchif) ||
3810 IS_INSN_ID(iobj, branchnil) ||
3811 IS_INSN_ID(iobj, branchunless)) {
3820 INSN *nobj = (INSN *)get_destination_insn(iobj);
3835 int stop_optimization =
3836 nobj->link.type == ISEQ_ELEMENT_INSN &&
3837 (nobj->insn_info.events & (
RUBY_EVENT_LINE | RUBY_EVENT_COVERAGE_LINE));
3838 if (!stop_optimization) {
3839 INSN *pobj = (INSN *)iobj->link.prev;
3842 if (!IS_INSN(&pobj->link))
3844 else if (IS_INSN_ID(pobj, dup))
3849 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3850 if (!replace_destination(iobj, nobj))
break;
3852 else if (prev_dup && IS_INSN(&nobj->link) && IS_INSN_ID(nobj, dup) &&
3853 !!(nobj = (INSN *)nobj->link.next) &&
3854 IS_INSN(&nobj->link) &&
3856 nobj->insn_id == iobj->insn_id) {
3872 if (!replace_destination(iobj, nobj))
break;
3900 if (prev_dup && IS_INSN(pobj->link.prev)) {
3901 pobj = (INSN *)pobj->link.prev;
3903 if (IS_INSN_ID(pobj, putobject)) {
3904 cond = (IS_INSN_ID(iobj, branchif) ?
3905 OPERAND_AT(pobj, 0) !=
Qfalse :
3906 IS_INSN_ID(iobj, branchunless) ?
3907 OPERAND_AT(pobj, 0) ==
Qfalse :
3910 else if (IS_INSN_ID(pobj, dupstring) ||
3911 IS_INSN_ID(pobj, duparray) ||
3912 IS_INSN_ID(pobj, newarray)) {
3913 cond = IS_INSN_ID(iobj, branchif);
3915 else if (IS_INSN_ID(pobj, putnil)) {
3916 cond = !IS_INSN_ID(iobj, branchif);
3919 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3920 ELEM_REMOVE(iobj->link.prev);
3922 else if (!iseq_pop_newarray(iseq, pobj)) {
3923 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(pop), 0, NULL);
3924 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3928 pobj = new_insn_core(iseq, pobj->insn_info.line_no, pobj->insn_info.node_id, BIN(putnil), 0, NULL);
3929 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3931 iobj->insn_id = BIN(jump);
3935 unref_destination(iobj, 0);
3936 ELEM_REMOVE(&iobj->link);
3942 LINK_ELEMENT *dest = get_destination_insn(nobj);
3943 if (!dest || !IS_INSN(dest))
break;
3944 nobj = (INSN *)dest;
3950 if (IS_INSN_ID(iobj, pop)) {
3957 LINK_ELEMENT *prev = iobj->link.prev;
3958 if (IS_INSN(prev)) {
3959 enum ruby_vminsn_type previ = ((INSN *)prev)->insn_id;
3960 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3961 previ == BIN(putself) || previ == BIN(dupstring) ||
3962 previ == BIN(dupchilledstring) ||
3963 previ == BIN(dup) ||
3964 previ == BIN(getlocal) ||
3965 previ == BIN(getblockparam) ||
3966 previ == BIN(getblockparamproxy) ||
3967 previ == BIN(getinstancevariable) ||
3968 previ == BIN(duparray)) {
3972 ELEM_REMOVE(&iobj->link);
3974 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (INSN*)prev)) {
3975 ELEM_REMOVE(&iobj->link);
3977 else if (previ == BIN(concatarray)) {
3978 INSN *piobj = (INSN *)prev;
3979 INSERT_BEFORE_INSN1(piobj, piobj->insn_info.line_no, piobj->insn_info.node_id, splatarray,
Qfalse);
3980 INSN_OF(piobj) = BIN(pop);
3982 else if (previ == BIN(concatstrings)) {
3983 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3987 ELEM_REMOVE(&iobj->link);
3988 INSN_OF(prev) = BIN(adjuststack);
3994 if (IS_INSN_ID(iobj, newarray) ||
3995 IS_INSN_ID(iobj, duparray) ||
3996 IS_INSN_ID(iobj, concatarray) ||
3997 IS_INSN_ID(iobj, splatarray) ||
4006 LINK_ELEMENT *next = iobj->link.next;
4007 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
4013 if (IS_INSN_ID(iobj, newarray)) {
4014 LINK_ELEMENT *next = iobj->link.next;
4015 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
4016 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
4018 op1 = OPERAND_AT(iobj, 0);
4019 op2 = OPERAND_AT(next, 0);
4030 INSN_OF(iobj) = BIN(swap);
4031 iobj->operand_size = 0;
4040 INSN_OF(iobj) = BIN(opt_reverse);
4045 INSN_OF(iobj) = BIN(opt_reverse);
4046 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
4056 for (; diff > 0; diff--) {
4057 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, pop);
4068 for (; diff < 0; diff++) {
4069 INSERT_BEFORE_INSN(iobj, iobj->insn_info.line_no, iobj->insn_info.node_id, putnil);
4076 if (IS_INSN_ID(iobj, duparray)) {
4077 LINK_ELEMENT *next = iobj->link.next;
4085 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
4086 INSN_OF(iobj) = BIN(putobject);
4090 if (IS_INSN_ID(iobj, anytostring)) {
4091 LINK_ELEMENT *next = iobj->link.next;
4098 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
4099 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
4104 if (IS_INSN_ID(iobj, dupstring) || IS_INSN_ID(iobj, dupchilledstring) ||
4112 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
4113 RSTRING_LEN(OPERAND_AT(iobj, 0)) == 0) {
4114 INSN *next = (INSN *)iobj->link.next;
4115 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
4116 ELEM_REMOVE(&next->link);
4118 ELEM_REMOVE(&iobj->link);
4120 if (IS_NEXT_INSN_ID(&iobj->link, toregexp)) {
4121 INSN *next = (INSN *)iobj->link.next;
4122 if (OPERAND_AT(next, 1) ==
INT2FIX(1)) {
4123 VALUE src = OPERAND_AT(iobj, 0);
4124 int opt = (int)
FIX2LONG(OPERAND_AT(next, 0));
4125 VALUE path = rb_iseq_path(iseq);
4126 int line = iobj->insn_info.line_no;
4127 VALUE re = iseq_reg_compile(iseq, src, opt, RSTRING_PTR(path), line);
4132 iobj->insn_id = BIN(putobject);
4134 ELEM_REMOVE(iobj->link.next);
4139 if (IS_INSN_ID(iobj, concatstrings)) {
4146 LINK_ELEMENT *next = iobj->link.next;
4148 if (IS_INSN(next) && IS_INSN_ID(next, jump))
4149 next = get_destination_insn(jump = (INSN *)next);
4150 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
4151 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
4152 OPERAND_AT(iobj, 0) =
INT2FIX(n);
4154 LABEL *label = ((LABEL *)OPERAND_AT(jump, 0));
4155 if (!--label->refcnt) {
4156 ELEM_REMOVE(&label->link);
4159 label = NEW_LABEL(0);
4160 OPERAND_AT(jump, 0) = (
VALUE)label;
4163 ELEM_INSERT_NEXT(next, &label->link);
4164 CHECK(iseq_peephole_optimize(iseq, unref, get_next_insn(jump), do_tailcallopt));
4172 if (do_tailcallopt &&
4173 (IS_INSN_ID(iobj, send) ||
4174 IS_INSN_ID(iobj, invokesuper))) {
4183 if (iobj->link.next) {
4184 LINK_ELEMENT *next = iobj->link.next;
4186 if (!IS_INSN(next)) {
4190 switch (INSN_OF(next)) {
4199 next = get_destination_insn((INSN *)next);
4213 if (IS_INSN_ID(piobj, send) ||
4214 IS_INSN_ID(piobj, invokesuper)) {
4215 if (OPERAND_AT(piobj, 1) == 0) {
4216 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4217 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4222 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
4223 OPERAND_AT(piobj, 0) = (
VALUE)ci;
4229 if (IS_INSN_ID(iobj, dup)) {
4230 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
4231 LINK_ELEMENT *set1 = iobj->link.next, *set2 = NULL;
4241 if (IS_NEXT_INSN_ID(set1, setlocal)) {
4243 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4244 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4246 ELEM_REMOVE(&iobj->link);
4259 else if (IS_NEXT_INSN_ID(set1, dup) &&
4260 IS_NEXT_INSN_ID(set1->next, setlocal)) {
4261 set2 = set1->next->next;
4262 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
4263 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
4264 ELEM_REMOVE(set1->next);
4278 if (IS_INSN_ID(iobj, getlocal)) {
4279 LINK_ELEMENT *niobj = &iobj->link;
4280 if (IS_NEXT_INSN_ID(niobj, dup)) {
4281 niobj = niobj->next;
4283 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
4284 LINK_ELEMENT *set1 = niobj->next;
4285 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
4286 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
4302 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
4303 if (IS_TRACE(iobj->link.next)) {
4304 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
4305 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
4307 if (iobj == (INSN *)list && bf->argc == 0 && (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_LEAF)) {
4308 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_SINGLE_NOARG_LEAF;
4321 if (IS_INSN_ID(iobj, getblockparam)) {
4322 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
4323 iobj->insn_id = BIN(getblockparamproxy);
4327 if (IS_INSN_ID(iobj, splatarray) && OPERAND_AT(iobj, 0) ==
false) {
4328 LINK_ELEMENT *niobj = &iobj->link;
4329 if (IS_NEXT_INSN_ID(niobj, duphash)) {
4330 niobj = niobj->next;
4331 LINK_ELEMENT *siobj;
4332 unsigned int set_flags = 0, unset_flags = 0;
4345 if (IS_NEXT_INSN_ID(niobj, send)) {
4346 siobj = niobj->next;
4347 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT;
4348 unset_flags = VM_CALL_ARGS_BLOCKARG;
4363 else if ((IS_NEXT_INSN_ID(niobj, getlocal) || IS_NEXT_INSN_ID(niobj, getinstancevariable) ||
4364 IS_NEXT_INSN_ID(niobj, getblockparamproxy)) && (IS_NEXT_INSN_ID(niobj->next, send))) {
4365 siobj = niobj->next->next;
4366 set_flags = VM_CALL_ARGS_SPLAT|VM_CALL_KW_SPLAT|VM_CALL_KW_SPLAT_MUT|VM_CALL_ARGS_BLOCKARG;
4371 unsigned int flags = vm_ci_flag(ci);
4372 if ((flags & set_flags) == set_flags && !(flags & unset_flags)) {
4373 ((INSN*)niobj)->insn_id = BIN(putobject);
4376 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
4377 flags & ~VM_CALL_KW_SPLAT_MUT, vm_ci_argc(ci), vm_ci_kwarg(ci));
4379 OPERAND_AT(siobj, 0) = (
VALUE)nci;
4393 if (IS_NEXT_INSN_ID(&iobj->link, swap)) {
4394 LINK_ELEMENT *second = &iobj->link;
4395 LINK_ELEMENT *first = second->prev;
4396 LINK_ELEMENT *swap = second->next;
4398 if (IS_INDEPENDENT_INSN(first) && IS_INDEPENDENT_INSN(second)) {
4400 ELEM_SWAP(first, second);
4408insn_set_specialized_instruction(rb_iseq_t *iseq, INSN *iobj,
int insn_id)
4410 if (insn_id == BIN(opt_neq)) {
4411 VALUE original_ci = iobj->operands[0];
4412 VALUE new_ci = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
4413 insn_replace_with_operands(iseq, iobj, insn_id, 2, new_ci, original_ci);
4416 iobj->insn_id = insn_id;
4417 iobj->operand_size = insn_len(insn_id) - 1;
4425iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
4427 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
4428 IS_INSN(iobj->link.next)) {
4432 INSN *niobj = (INSN *)iobj->link.next;
4433 if (IS_INSN_ID(niobj, send)) {
4435 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 0) {
4437 switch (vm_ci_mid(ci)) {
4439 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MAX);
4442 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_MIN);
4445 method =
INT2FIX(VM_OPT_NEWARRAY_SEND_HASH);
4450 VALUE num = iobj->operands[0];
4451 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, num, method);
4452 ELEM_REMOVE(&niobj->link);
4457 else if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4459 IS_NEXT_INSN_ID(&niobj->link, send)) {
4461 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idPack) {
4462 VALUE num = iobj->operands[0];
4463 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK));
4464 ELEM_REMOVE(&iobj->link);
4465 ELEM_REMOVE(niobj->link.next);
4466 ELEM_INSERT_NEXT(&niobj->link, &iobj->link);
4472 else if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4474 IS_NEXT_INSN_ID(&niobj->link, getlocal) &&
4475 (niobj->link.next && IS_NEXT_INSN_ID(niobj->link.next, send))) {
4478 if (vm_ci_mid(ci) == idPack && vm_ci_argc(ci) == 2 &&
4479 (kwarg && kwarg->keyword_len == 1 && kwarg->keywords[0] ==
rb_id2sym(idBuffer))) {
4480 VALUE num = iobj->operands[0];
4481 insn_replace_with_operands(iseq, iobj, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 2),
INT2FIX(VM_OPT_NEWARRAY_SEND_PACK_BUFFER));
4483 ELEM_REMOVE((niobj->link.next)->next);
4485 ELEM_REMOVE(&iobj->link);
4487 ELEM_INSERT_NEXT(niobj->link.next, &iobj->link);
4495 if ((IS_INSN_ID(niobj, dupstring) || IS_INSN_ID(niobj, dupchilledstring) ||
4496 IS_INSN_ID(niobj, putobject) ||
4497 IS_INSN_ID(niobj, putself) ||
4498 IS_INSN_ID(niobj, getlocal) ||
4499 IS_INSN_ID(niobj, getinstancevariable)) &&
4500 IS_NEXT_INSN_ID(&niobj->link, send)) {
4502 LINK_ELEMENT *sendobj = &(niobj->link);
4507 sendobj = sendobj->next;
4508 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4509 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4512 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4513 VALUE num = iobj->operands[0];
4514 INSN *sendins = (INSN *)sendobj;
4515 insn_replace_with_operands(iseq, sendins, BIN(opt_newarray_send), 2, FIXNUM_INC(num, 1),
INT2FIX(VM_OPT_NEWARRAY_SEND_INCLUDE_P));
4517 ELEM_REMOVE(&iobj->link);
4531 if (IS_INSN_ID(iobj, duparray) && iobj->link.next && IS_INSN(iobj->link.next)) {
4532 INSN *niobj = (INSN *)iobj->link.next;
4533 if ((IS_INSN_ID(niobj, getlocal) ||
4534 IS_INSN_ID(niobj, getinstancevariable) ||
4535 IS_INSN_ID(niobj, putself) ||
4536 IS_INSN_ID(niobj, putobject) ||
4537 IS_INSN_ID(niobj, putnil)) &&
4538 IS_NEXT_INSN_ID(&niobj->link, send)) {
4540 LINK_ELEMENT *sendobj = &(niobj->link);
4545 sendobj = sendobj->next;
4546 ci = (
struct rb_callinfo *)OPERAND_AT(sendobj, 0);
4547 }
while (vm_ci_simple(ci) && vm_ci_argc(ci) == 0 && IS_NEXT_INSN_ID(sendobj, send));
4549 if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && vm_ci_mid(ci) == idIncludeP) {
4551 VALUE ary = iobj->operands[0];
4554 INSN *sendins = (INSN *)sendobj;
4555 insn_replace_with_operands(iseq, sendins, BIN(opt_duparray_send), 3, ary,
rb_id2sym(idIncludeP),
INT2FIX(1));
4558 ELEM_REMOVE(&iobj->link);
4565 if (IS_INSN_ID(iobj, send)) {
4567 const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(iobj, 1);
4569#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
4570 if (vm_ci_simple(ci)) {
4571 switch (vm_ci_argc(ci)) {
4573 switch (vm_ci_mid(ci)) {
4574 case idLength: SP_INSN(length);
return COMPILE_OK;
4575 case idSize: SP_INSN(size);
return COMPILE_OK;
4576 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
4577 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
4578 case idSucc: SP_INSN(succ);
return COMPILE_OK;
4579 case idNot: SP_INSN(not);
return COMPILE_OK;
4583 switch (vm_ci_mid(ci)) {
4584 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
4585 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
4586 case idMULT: SP_INSN(mult);
return COMPILE_OK;
4587 case idDIV: SP_INSN(div);
return COMPILE_OK;
4588 case idMOD: SP_INSN(mod);
return COMPILE_OK;
4589 case idEq: SP_INSN(eq);
return COMPILE_OK;
4590 case idNeq: SP_INSN(neq);
return COMPILE_OK;
4591 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
4592 case idLT: SP_INSN(lt);
return COMPILE_OK;
4593 case idLE: SP_INSN(le);
return COMPILE_OK;
4594 case idGT: SP_INSN(gt);
return COMPILE_OK;
4595 case idGE: SP_INSN(ge);
return COMPILE_OK;
4596 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
4597 case idAREF: SP_INSN(aref);
return COMPILE_OK;
4598 case idAnd: SP_INSN(and);
return COMPILE_OK;
4599 case idOr: SP_INSN(or);
return COMPILE_OK;
4603 switch (vm_ci_mid(ci)) {
4604 case idASET: SP_INSN(aset);
return COMPILE_OK;
4610 if ((vm_ci_flag(ci) & (VM_CALL_ARGS_BLOCKARG | VM_CALL_FORWARDING)) == 0 && blockiseq == NULL) {
4611 iobj->insn_id = BIN(opt_send_without_block);
4612 iobj->operand_size = insn_len(iobj->insn_id) - 1;
4621tailcallable_p(rb_iseq_t *iseq)
4623 switch (ISEQ_BODY(iseq)->
type) {
4625 case ISEQ_TYPE_EVAL:
4626 case ISEQ_TYPE_MAIN:
4628 case ISEQ_TYPE_RESCUE:
4629 case ISEQ_TYPE_ENSURE:
4638iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
4641 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
4642 const int do_tailcallopt = tailcallable_p(iseq) &&
4643 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
4644 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
4645 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
4646 const int do_without_ints = ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_WITHOUT_INTERRUPTS;
4647 int rescue_level = 0;
4648 int tailcallopt = do_tailcallopt;
4650 list = FIRST_ELEMENT(anchor);
4652 int do_block_optimization = 0;
4653 LABEL * block_loop_label = NULL;
4655 int nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
4656 VALUE unref_counts_buf = 0, unref_stamps_buf = 0;
4658 .capacity = nlabels,
4660 .counts =
ALLOCV_N(
int, unref_counts_buf, nlabels),
4661 .stamps =
ALLOCV_N(
unsigned int, unref_stamps_buf, nlabels),
4667 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
4668 do_block_optimization = 1;
4672 LINK_ELEMENT * le = FIRST_ELEMENT(anchor)->next;
4673 if (IS_INSN(le) && IS_INSN_ID((INSN *)le, nop) && IS_LABEL(le->next)) {
4674 block_loop_label = (LABEL *)le->next;
4679 if (IS_INSN(list)) {
4680 if (do_peepholeopt) {
4684 iseq_specialized_instruction(iseq, (INSN *)list);
4687 insn_operands_unification((INSN *)list);
4690 if (do_without_ints) {
4691 INSN *item = (INSN *)list;
4692 if (IS_INSN_ID(item, jump)) {
4693 item->insn_id = BIN(jump_without_ints);
4695 else if (IS_INSN_ID(item, branchif)) {
4696 item->insn_id = BIN(branchif_without_ints);
4698 else if (IS_INSN_ID(item, branchunless)) {
4699 item->insn_id = BIN(branchunless_without_ints);
4701 else if (IS_INSN_ID(item, branchnil)) {
4702 item->insn_id = BIN(branchnil_without_ints);
4706 if (do_block_optimization) {
4707 INSN * item = (INSN *)list;
4709 if (IS_INSN_ID(item,
throw)) {
4710 do_block_optimization = 0;
4715 const char *types = insn_op_types(item->insn_id);
4716 for (
int j = 0; types[j]; j++) {
4717 if (types[j] == TS_OFFSET) {
4722 LABEL * target = (LABEL *)OPERAND_AT(item, j);
4723 if (target == block_loop_label) {
4724 do_block_optimization = 0;
4731 if (IS_LABEL(list)) {
4732 switch (((LABEL *)list)->rescued) {
4733 case LABEL_RESCUE_BEG:
4735 tailcallopt = FALSE;
4737 case LABEL_RESCUE_END:
4738 if (!--rescue_level) tailcallopt = do_tailcallopt;
4745 if (do_block_optimization) {
4746 LINK_ELEMENT * le = FIRST_ELEMENT(anchor)->next;
4747 if (IS_INSN(le) && IS_INSN_ID((INSN *)le, nop)) {
4757#if OPT_INSTRUCTIONS_UNIFICATION
4759new_unified_insn(rb_iseq_t *iseq,
4760 int insn_id,
int size, LINK_ELEMENT *seq_list)
4763 LINK_ELEMENT *list = seq_list;
4765 VALUE *operands = 0, *ptr = 0;
4769 for (i = 0; i < size; i++) {
4770 iobj = (INSN *)list;
4771 argc += iobj->operand_size;
4776 ptr = operands = compile_data_alloc2_type(iseq,
VALUE, argc);
4781 for (i = 0; i < size; i++) {
4782 iobj = (INSN *)list;
4783 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
4784 ptr += iobj->operand_size;
4788 return new_insn_core(iseq, iobj->insn_info.line_no, iobj->insn_info.node_id, insn_id, argc, operands);
4798iseq_insns_unification(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor)
4800#if OPT_INSTRUCTIONS_UNIFICATION
4806 list = FIRST_ELEMENT(anchor);
4808 if (IS_INSN(list)) {
4809 iobj = (INSN *)list;
4811 if (unified_insns_data[
id] != 0) {
4812 const int *
const *entry = unified_insns_data[id];
4813 for (j = 1; j < (intptr_t)entry[0]; j++) {
4814 const int *unified = entry[j];
4815 LINK_ELEMENT *li = list->next;
4816 for (k = 2; k < unified[1]; k++) {
4818 ((INSN *)li)->insn_id != unified[k]) {
4825 new_unified_insn(iseq, unified[0], unified[1] - 1,
4829 niobj->link.prev = (LINK_ELEMENT *)iobj->link.prev;
4830 niobj->link.next = li;
4832 li->prev = (LINK_ELEMENT *)niobj;
4835 list->prev->next = (LINK_ELEMENT *)niobj;
4836 list = (LINK_ELEMENT *)niobj;
4849all_string_result_p(
const NODE *node)
4851 if (!node)
return FALSE;
4852 switch (nd_type(node)) {
4853 case NODE_STR:
case NODE_DSTR:
case NODE_FILE:
4855 case NODE_IF:
case NODE_UNLESS:
4856 if (!RNODE_IF(node)->nd_body || !RNODE_IF(node)->nd_else)
return FALSE;
4857 if (all_string_result_p(RNODE_IF(node)->nd_body))
4858 return all_string_result_p(RNODE_IF(node)->nd_else);
4860 case NODE_AND:
case NODE_OR:
4861 if (!RNODE_AND(node)->nd_2nd)
4862 return all_string_result_p(RNODE_AND(node)->nd_1st);
4863 if (!all_string_result_p(RNODE_AND(node)->nd_1st))
4865 return all_string_result_p(RNODE_AND(node)->nd_2nd);
4872 rb_iseq_t *
const iseq;
4873 LINK_ANCHOR *
const ret;
4875 const NODE *lit_node;
4881append_dstr_fragment(
struct dstr_ctxt *args,
const NODE *
const node, rb_parser_string_t *str)
4883 VALUE s = rb_str_new_mutable_parser_string(str);
4885 VALUE error = rb_reg_check_preprocess(s);
4886 if (!
NIL_P(error)) {
4887 COMPILE_ERROR(args->iseq, nd_line(node),
"%" PRIsVALUE, error);
4891 if (
NIL_P(args->lit)) {
4893 args->lit_node = node;
4902flush_dstr_fragment(
struct dstr_ctxt *args)
4904 if (!
NIL_P(args->lit)) {
4905 rb_iseq_t *iseq = args->iseq;
4906 VALUE lit = args->lit;
4908 lit = rb_fstring(lit);
4909 ADD_INSN1(args->ret, args->lit_node, putobject, lit);
4916compile_dstr_fragments_0(
struct dstr_ctxt *args,
const NODE *
const node)
4918 const struct RNode_LIST *list = RNODE_DSTR(node)->nd_next;
4919 rb_parser_string_t *str = RNODE_DSTR(node)->string;
4922 CHECK(append_dstr_fragment(args, node, str));
4926 const NODE *
const head = list->nd_head;
4927 if (nd_type_p(head, NODE_STR)) {
4928 CHECK(append_dstr_fragment(args, node, RNODE_STR(head)->
string));
4930 else if (nd_type_p(head, NODE_DSTR)) {
4931 CHECK(compile_dstr_fragments_0(args, head));
4934 flush_dstr_fragment(args);
4935 rb_iseq_t *iseq = args->iseq;
4936 CHECK(COMPILE(args->ret,
"each string", head));
4945compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int *cntp,
int dregx)
4948 .iseq = iseq, .ret = ret,
4949 .lit =
Qnil, .lit_node = NULL,
4950 .cnt = 0, .dregx = dregx,
4952 CHECK(compile_dstr_fragments_0(&args, node));
4953 flush_dstr_fragment(&args);
4961compile_block(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int popped)
4963 while (node && nd_type_p(node, NODE_BLOCK)) {
4964 CHECK(COMPILE_(ret,
"BLOCK body", RNODE_BLOCK(node)->nd_head,
4965 (RNODE_BLOCK(node)->nd_next ? 1 : popped)));
4966 node = RNODE_BLOCK(node)->nd_next;
4969 CHECK(COMPILE_(ret,
"BLOCK next", RNODE_BLOCK(node)->nd_next, popped));
4975compile_file_path(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int frozen_string_literal)
4977 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4978 ADD_INSN1(ret, node, putobject,
INT2FIX(frozen_string_literal));
4979 ADD_SEND(ret, node, id_core_iseq_path,
INT2FIX(1));
4983compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node)
4986 if (!RNODE_DSTR(node)->nd_next) {
4987 VALUE lit = rb_node_dstr_string_val(node);
4988 ADD_INSN1(ret, node, dupstring, lit);
4993 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, FALSE));
4994 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
5000compile_dregx(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
5003 int cflag = (int)RNODE_DREGX(node)->as.nd_cflag;
5005 if (!RNODE_DREGX(node)->nd_next) {
5007 VALUE src = rb_node_dregx_string_val(node);
5008 VALUE match = iseq_reg_compile(iseq, src, cflag, NULL, 0);
5009 ADD_INSN1(ret, node, putobject, match);
5015 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt, TRUE));
5019 ADD_INSN(ret, node, pop);
5026compile_flip_flop(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int again,
5027 LABEL *then_label, LABEL *else_label)
5029 const int line = nd_line(node);
5030 LABEL *lend = NEW_LABEL(line);
5031 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
5032 + VM_SVAR_FLIPFLOP_START;
5035 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
5036 ADD_INSNL(ret, node, branchif, lend);
5039 CHECK(COMPILE(ret,
"flip2 beg", RNODE_FLIP2(node)->nd_beg));
5040 ADD_INSNL(ret, node, branchunless, else_label);
5041 ADD_INSN1(ret, node, putobject,
Qtrue);
5042 ADD_INSN1(ret, node, setspecial, key);
5044 ADD_INSNL(ret, node, jump, then_label);
5048 ADD_LABEL(ret, lend);
5049 CHECK(COMPILE(ret,
"flip2 end", RNODE_FLIP2(node)->nd_end));
5050 ADD_INSNL(ret, node, branchunless, then_label);
5051 ADD_INSN1(ret, node, putobject,
Qfalse);
5052 ADD_INSN1(ret, node, setspecial, key);
5053 ADD_INSNL(ret, node, jump, then_label);
5059compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *cond,
5060 LABEL *then_label, LABEL *else_label);
5062#define COMPILE_SINGLE 2
5064compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *cond,
5065 LABEL *then_label, LABEL *else_label)
5069 LABEL *label = NEW_LABEL(nd_line(cond));
5070 if (!then_label) then_label = label;
5071 else if (!else_label) else_label = label;
5073 CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
5075 if (LIST_INSN_SIZE_ONE(seq)) {
5076 INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
5077 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
5080 if (!label->refcnt) {
5081 return COMPILE_SINGLE;
5083 ADD_LABEL(seq, label);
5089compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *cond,
5090 LABEL *then_label, LABEL *else_label)
5093 DECL_ANCHOR(ignore);
5096 switch (nd_type(cond)) {
5098 CHECK(ok = compile_logical(iseq, ret, RNODE_AND(cond)->nd_1st, NULL, else_label));
5099 cond = RNODE_AND(cond)->nd_2nd;
5100 if (ok == COMPILE_SINGLE) {
5101 ADD_INSNL(ret, cond, jump, else_label);
5102 INIT_ANCHOR(ignore);
5104 then_label = NEW_LABEL(nd_line(cond));
5108 CHECK(ok = compile_logical(iseq, ret, RNODE_OR(cond)->nd_1st, then_label, NULL));
5109 cond = RNODE_OR(cond)->nd_2nd;
5110 if (ok == COMPILE_SINGLE) {
5111 ADD_INSNL(ret, cond, jump, then_label);
5112 INIT_ANCHOR(ignore);
5114 else_label = NEW_LABEL(nd_line(cond));
5124 case NODE_IMAGINARY:
5131 ADD_INSNL(ret, cond, jump, then_label);
5136 ADD_INSNL(ret, cond, jump, else_label);
5142 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
5143 ADD_INSNL(ret, cond, jump, then_label);
5146 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
5149 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
5152 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse, ret == ignore));
5156 DECL_ANCHOR(cond_seq);
5157 INIT_ANCHOR(cond_seq);
5159 CHECK(COMPILE(cond_seq,
"branch condition", cond));
5161 if (LIST_INSN_SIZE_ONE(cond_seq)) {
5162 INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
5163 if (insn->insn_id == BIN(putobject)) {
5164 if (
RTEST(insn->operands[0])) {
5165 ADD_INSNL(ret, cond, jump, then_label);
5170 ADD_INSNL(ret, cond, jump, else_label);
5175 ADD_SEQ(ret, cond_seq);
5180 ADD_INSNL(ret, cond, branchunless, else_label);
5181 ADD_INSNL(ret, cond, jump, then_label);
5188keyword_node_p(
const NODE *
const node)
5190 return nd_type_p(node, NODE_HASH) && (RNODE_HASH(node)->nd_brace & HASH_BRACE) != HASH_BRACE;
5194get_symbol_value(rb_iseq_t *iseq,
const NODE *node)
5196 switch (nd_type(node)) {
5198 return rb_node_sym_string_val(node);
5200 UNKNOWN_NODE(
"get_symbol_value", node,
Qnil);
5205node_hash_unique_key_index(rb_iseq_t *iseq, rb_node_hash_t *node_hash,
int *count_ptr)
5207 NODE *node = node_hash->nd_head;
5211 for (
int i = 0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5212 VALUE key = get_symbol_value(iseq, RNODE_LIST(node)->nd_head);
5213 VALUE idx = rb_hash_aref(hash, key);
5218 rb_hash_aset(hash, key,
INT2FIX(i));
5227compile_keyword_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
5228 const NODE *
const root_node,
5235 if (RNODE_HASH(root_node)->nd_head && nd_type_p(RNODE_HASH(root_node)->nd_head, NODE_LIST)) {
5236 const NODE *node = RNODE_HASH(root_node)->nd_head;
5240 const NODE *key_node = RNODE_LIST(node)->nd_head;
5244 if (key_node && nd_type_p(key_node, NODE_SYM)) {
5249 *flag |= VM_CALL_KW_SPLAT;
5250 if (seen_nodes > 1 || RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5255 *flag |= VM_CALL_KW_SPLAT_MUT;
5260 node = RNODE_LIST(node)->nd_next;
5261 node = RNODE_LIST(node)->nd_next;
5265 node = RNODE_HASH(root_node)->nd_head;
5268 VALUE key_index = node_hash_unique_key_index(iseq, RNODE_HASH(root_node), &
len);
5270 if (
len > VM_CALL_KW_LEN_MAX) {
5271 COMPILE_ERROR(ERROR_ARGS_AT(root_node)
"too many keyword arguments (%d, maximum is %d)",
5272 len, (
int)VM_CALL_KW_LEN_MAX);
5277 VALUE *keywords = kw_arg->keywords;
5280 kw_arg->references = 0;
5281 kw_arg->keyword_len =
len;
5283 *kw_arg_ptr = kw_arg;
5285 for (i=0; node != NULL; i++, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5286 const NODE *key_node = RNODE_LIST(node)->nd_head;
5287 const NODE *val_node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5290 keywords[j] = get_symbol_value(iseq, key_node);
5294 NO_CHECK(COMPILE_(ret,
"keyword values", val_node, popped));
5304compile_args(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node, NODE **kwnode_ptr)
5308 for (; node;
len++, node = RNODE_LIST(node)->nd_next) {
5310 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
5313 if (RNODE_LIST(node)->nd_next == NULL && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5314 *kwnode_ptr = RNODE_LIST(node)->nd_head;
5317 RUBY_ASSERT(!keyword_node_p(RNODE_LIST(node)->nd_head));
5318 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, FALSE));
5326frozen_string_literal_p(
const rb_iseq_t *iseq)
5328 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal > 0;
5332static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq,
bool hash_key)
5334 switch (nd_type(node)) {
5342 case NODE_IMAGINARY:
5348 return hash_key || frozen_string_literal_p(iseq);
5355static_literal_value(
const NODE *node, rb_iseq_t *iseq)
5357 switch (nd_type(node)) {
5360 VALUE lit = rb_node_integer_literal_val(node);
5366 VALUE lit = rb_node_float_literal_val(node);
5372 case NODE_IMAGINARY:
5381 return rb_node_sym_string_val(node);
5385 return rb_node_line_lineno_val(node);
5387 return rb_node_encoding_val(node);
5390 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
5391 VALUE lit = get_string_value(node);
5392 VALUE str = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), (
int)nd_line(node));
5397 return get_string_value(node);
5400 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
5405compile_array(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int popped,
bool first_chunk)
5407 const NODE *line_node = node;
5409 if (nd_type_p(node, NODE_ZLIST)) {
5411 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5416 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5419 for (; node; node = RNODE_LIST(node)->nd_next) {
5420 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, popped));
5462 const int max_stack_len = 0x100;
5463 const int min_tmp_ary_len = 0x40;
5467#define FLUSH_CHUNK \
5469 if (first_chunk) ADD_INSN1(ret, line_node, newarray, INT2FIX(stack_len)); \
5470 else ADD_INSN1(ret, line_node, pushtoarray, INT2FIX(stack_len)); \
5471 first_chunk = FALSE; \
5479 if (static_literal_node_p(RNODE_LIST(node)->nd_head, iseq,
false)) {
5481 const NODE *node_tmp = RNODE_LIST(node)->nd_next;
5482 for (; node_tmp && static_literal_node_p(RNODE_LIST(node_tmp)->nd_head, iseq,
false); node_tmp = RNODE_LIST(node_tmp)->nd_next)
5485 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
5490 for (; count; count--, node = RNODE_LIST(node)->nd_next)
5491 rb_ary_push(ary, static_literal_value(RNODE_LIST(node)->nd_head, iseq));
5497 ADD_INSN1(ret, line_node, duparray, ary);
5498 first_chunk = FALSE;
5501 ADD_INSN1(ret, line_node, putobject, ary);
5502 ADD_INSN(ret, line_node, concattoarray);
5510 for (; count; count--, node = RNODE_LIST(node)->nd_next) {
5512 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
5515 if (!RNODE_LIST(node)->nd_next && keyword_node_p(RNODE_LIST(node)->nd_head)) {
5517 if (stack_len == 0 && first_chunk) {
5518 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
5523 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5524 ADD_INSN(ret, line_node, pushtoarraykwsplat);
5528 NO_CHECK(COMPILE_(ret,
"array element", RNODE_LIST(node)->nd_head, 0));
5533 if (stack_len >= max_stack_len) FLUSH_CHUNK;
5543static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
5545 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);
5549compile_hash(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped)
5551 const NODE *line_node = node;
5553 node = RNODE_HASH(node)->nd_head;
5555 if (!node || nd_type_p(node, NODE_ZLIST)) {
5557 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5562 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5565 for (; node; node = RNODE_LIST(node)->nd_next) {
5566 NO_CHECK(COMPILE_(ret,
"hash element", RNODE_LIST(node)->nd_head, popped));
5589 const int max_stack_len = 0x100;
5590 const int min_tmp_hash_len = 0x800;
5592 int first_chunk = 1;
5593 DECL_ANCHOR(anchor);
5594 INIT_ANCHOR(anchor);
5597#define FLUSH_CHUNK() \
5599 if (first_chunk) { \
5600 APPEND_LIST(ret, anchor); \
5601 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
5604 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
5605 ADD_INSN(ret, line_node, swap); \
5606 APPEND_LIST(ret, anchor); \
5607 ADD_SEND(ret, line_node, id_core_hash_merge_bang_ptr, INT2FIX(stack_len + 1)); \
5609 INIT_ANCHOR(anchor); \
5610 first_chunk = stack_len = 0; \
5617 if (static_literal_node_pair_p(node, iseq)) {
5619 const NODE *node_tmp = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5620 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = RNODE_LIST(RNODE_LIST(node_tmp)->nd_next)->nd_next)
5623 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
5628 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5630 elem[0] = static_literal_value(RNODE_LIST(node)->nd_head, iseq);
5632 elem[1] = static_literal_value(RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, iseq);
5644 ADD_INSN1(ret, line_node, duphash, hash);
5648 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5649 ADD_INSN(ret, line_node, swap);
5651 ADD_INSN1(ret, line_node, putobject, hash);
5653 ADD_SEND(ret, line_node, id_core_hash_merge_bang_kwd,
INT2FIX(2));
5660 for (; count; count--, node = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next) {
5663 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
5666 if (RNODE_LIST(node)->nd_head) {
5668 NO_CHECK(COMPILE_(anchor,
"hash key element", RNODE_LIST(node)->nd_head, 0));
5669 NO_CHECK(COMPILE_(anchor,
"hash value element", RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head, 0));
5673 if (stack_len >= max_stack_len) FLUSH_CHUNK();
5679 const NODE *kw = RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_head;
5680 int empty_kw = nd_type_p(kw, NODE_HASH) && (!RNODE_HASH(kw)->nd_head);
5681 int first_kw = first_chunk && stack_len == 0;
5682 int last_kw = !RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next;
5683 int only_kw = last_kw && first_kw;
5685 empty_kw = empty_kw || nd_type_p(kw, NODE_NIL);
5687 if (only_kw && method_call_keywords) {
5695 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5697 else if (first_kw) {
5701 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5708 if (only_kw && method_call_keywords) {
5714 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5721 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5722 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
5723 else ADD_INSN(ret, line_node, swap);
5725 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
5727 ADD_SEND(ret, line_node, id_core_hash_merge_bang_kwd,
INT2FIX(2));
5742rb_node_case_when_optimizable_literal(
const NODE *
const node)
5744 switch (nd_type(node)) {
5746 return rb_node_integer_literal_val(node);
5748 VALUE v = rb_node_float_literal_val(node);
5757 case NODE_IMAGINARY:
5766 return rb_node_sym_string_val(node);
5768 return rb_node_line_lineno_val(node);
5770 return rb_node_str_string_val(node);
5776when_vals(rb_iseq_t *iseq, LINK_ANCHOR *
const cond_seq,
const NODE *vals,
5777 LABEL *l1,
int only_special_literals,
VALUE literals)
5780 const NODE *val = RNODE_LIST(vals)->nd_head;
5781 VALUE lit = rb_node_case_when_optimizable_literal(val);
5784 only_special_literals = 0;
5787 cdhash_aset_if_missing(literals, lit, (
VALUE)(l1));
5790 if (nd_type_p(val, NODE_STR)) {
5791 debugp_param(
"nd_lit", get_string_value(val));
5792 lit = get_string_value(val);
5793 ADD_INSN1(cond_seq, val, putobject, lit);
5797 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
5801 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
5802 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
5803 ADD_INSNL(cond_seq, val, branchif, l1);
5804 vals = RNODE_LIST(vals)->nd_next;
5806 return only_special_literals;
5810when_splat_vals(rb_iseq_t *iseq, LINK_ANCHOR *
const cond_seq,
const NODE *vals,
5811 LABEL *l1,
int only_special_literals,
VALUE literals)
5813 const NODE *line_node = vals;
5815 switch (nd_type(vals)) {
5817 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
5821 ADD_INSN (cond_seq, line_node, dup);
5822 CHECK(COMPILE(cond_seq,
"when splat", RNODE_SPLAT(vals)->nd_head));
5823 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5824 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5825 ADD_INSNL(cond_seq, line_node, branchif, l1);
5828 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_head, l1, only_special_literals, literals));
5829 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSCAT(vals)->nd_body, l1, only_special_literals, literals));
5832 CHECK(when_splat_vals(iseq, cond_seq, RNODE_ARGSPUSH(vals)->nd_head, l1, only_special_literals, literals));
5833 ADD_INSN (cond_seq, line_node, dup);
5834 CHECK(COMPILE(cond_seq,
"when argspush body", RNODE_ARGSPUSH(vals)->nd_body));
5835 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
5836 ADD_INSNL(cond_seq, line_node, branchif, l1);
5839 ADD_INSN (cond_seq, line_node, dup);
5840 CHECK(COMPILE(cond_seq,
"when val", vals));
5841 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
5842 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5843 ADD_INSNL(cond_seq, line_node, branchif, l1);
5936 const NODE *line_node;
5951add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc, INSN *before_insn)
5954 rb_bug(
"no masgn_state");
5963 memo->before_insn = before_insn;
5964 memo->line_node = line_node;
5965 memo->argn = state->num_args + 1;
5966 memo->num_args = argc;
5967 state->num_args += argc;
5968 memo->lhs_pos = lhs_pos;
5970 if (!state->first_memo) {
5971 state->first_memo = memo;
5974 state->last_memo->next = memo;
5976 state->last_memo = memo;
5981static 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);
5984compile_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)
5986 switch (nd_type(node)) {
5987 case NODE_ATTRASGN: {
5989 const NODE *line_node = node;
5991 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5993 bool safenav_call =
false;
5994 LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
5995 iobj = (INSN *)get_prev_insn((INSN *)insn_element);
5997 ELEM_REMOVE(insn_element);
5998 if (!IS_INSN_ID(iobj, send)) {
5999 safenav_call =
true;
6000 iobj = (INSN *)get_prev_insn(iobj);
6001 ELEM_INSERT_NEXT(&iobj->link, insn_element);
6003 (pre->last = iobj->link.prev)->next = 0;
6006 int argc = vm_ci_argc(ci) + 1;
6007 ci = ci_argc_set(iseq, ci, argc);
6008 OPERAND_AT(iobj, 0) = (
VALUE)ci;
6012 ADD_INSN(lhs, line_node, swap);
6015 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
6018 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (INSN *)LAST_ELEMENT(lhs))) {
6022 iobj->link.prev = lhs->last;
6023 lhs->last->next = &iobj->link;
6024 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
6025 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
6026 int argc = vm_ci_argc(ci);
6027 bool dupsplat =
false;
6028 ci = ci_argc_set(iseq, ci, argc - 1);
6029 if (!(vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT_MUT)) {
6036 ci = ci_flag_set(iseq, ci, VM_CALL_ARGS_SPLAT_MUT);
6038 OPERAND_AT(iobj, 0) = (
VALUE)ci;
6047 int line_no = nd_line(line_node);
6048 int node_id = nd_node_id(line_node);
6051 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
6052 INSERT_BEFORE_INSN1(iobj, line_no, node_id, splatarray,
Qtrue);
6053 INSERT_BEFORE_INSN(iobj, line_no, node_id, swap);
6055 INSERT_BEFORE_INSN1(iobj, line_no, node_id, pushtoarray,
INT2FIX(1));
6057 if (!safenav_call) {
6058 ADD_INSN(lhs, line_node, pop);
6060 ADD_INSN(lhs, line_node, pop);
6063 for (
int i=0; i < argc; i++) {
6064 ADD_INSN(post, line_node, pop);
6069 DECL_ANCHOR(nest_rhs);
6070 INIT_ANCHOR(nest_rhs);
6071 DECL_ANCHOR(nest_lhs);
6072 INIT_ANCHOR(nest_lhs);
6074 int prev_level = state->lhs_level;
6075 bool prev_nested = state->nested;
6077 state->lhs_level = lhs_pos - 1;
6078 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
6079 state->lhs_level = prev_level;
6080 state->nested = prev_nested;
6082 ADD_SEQ(lhs, nest_rhs);
6083 ADD_SEQ(lhs, nest_lhs);
6087 if (!RNODE_CDECL(node)->nd_vid) {
6091 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
6093 LINK_ELEMENT *insn_element = LAST_ELEMENT(pre);
6094 iobj = (INSN *)insn_element;
6095 ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn((INSN *)get_prev_insn(iobj)));
6096 ELEM_REMOVE((LINK_ELEMENT *)get_prev_insn(iobj));
6097 ELEM_REMOVE(insn_element);
6098 pre->last = iobj->link.prev;
6099 ADD_ELEM(lhs, (LINK_ELEMENT *)iobj);
6101 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (INSN *)LAST_ELEMENT(lhs))) {
6105 ADD_INSN(post, node, pop);
6110 DECL_ANCHOR(anchor);
6111 INIT_ANCHOR(anchor);
6112 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
6113 ELEM_REMOVE(FIRST_ELEMENT(anchor));
6114 ADD_SEQ(lhs, anchor);
6122compile_massign_opt_lhs(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *lhsn)
6125 CHECK(compile_massign_opt_lhs(iseq, ret, RNODE_LIST(lhsn)->nd_next));
6126 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, RNODE_LIST(lhsn)->nd_head, NULL, 0));
6132compile_massign_opt(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6133 const NODE *rhsn,
const NODE *orig_lhsn)
6136 const int memsize = numberof(mem);
6138 int llen = 0, rlen = 0;
6140 const NODE *lhsn = orig_lhsn;
6142#define MEMORY(v) { \
6144 if (memindex == memsize) return 0; \
6145 for (i=0; i<memindex; i++) { \
6146 if (mem[i] == (v)) return 0; \
6148 mem[memindex++] = (v); \
6151 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
6156 const NODE *ln = RNODE_LIST(lhsn)->nd_head;
6157 switch (nd_type(ln)) {
6162 MEMORY(get_nd_vid(ln));
6167 lhsn = RNODE_LIST(lhsn)->nd_next;
6173 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", RNODE_LIST(rhsn)->nd_head));
6176 NO_CHECK(COMPILE(ret,
"masgn val", RNODE_LIST(rhsn)->nd_head));
6178 rhsn = RNODE_LIST(rhsn)->nd_next;
6183 for (i=0; i<llen-rlen; i++) {
6184 ADD_INSN(ret, orig_lhsn, putnil);
6188 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
6193compile_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)
6195 const NODE *rhsn = RNODE_MASGN(node)->nd_value;
6196 const NODE *splatn = RNODE_MASGN(node)->nd_args;
6197 const NODE *lhsn = RNODE_MASGN(node)->nd_head;
6198 const NODE *lhsn_count = lhsn;
6199 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
6204 while (lhsn_count) {
6206 lhsn_count = RNODE_LIST(lhsn_count)->nd_next;
6209 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(lhsn)->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
6211 lhsn = RNODE_LIST(lhsn)->nd_next;
6215 if (nd_type_p(splatn, NODE_POSTARG)) {
6217 const NODE *postn = RNODE_POSTARG(splatn)->nd_2nd;
6218 const NODE *restn = RNODE_POSTARG(splatn)->nd_1st;
6219 int plen = (int)RNODE_LIST(postn)->as.nd_alen;
6221 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
6223 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
6225 if (NODE_NAMED_REST_P(restn)) {
6226 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
6229 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, RNODE_LIST(postn)->nd_head, state, (plen - ppos) + state->lhs_level));
6231 postn = RNODE_LIST(postn)->nd_next;
6236 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
6240 if (!state->nested) {
6241 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
6245 ADD_INSN(rhs, node, dup);
6247 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
6252compile_massign(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
6254 if (!popped || RNODE_MASGN(node)->nd_args || !compile_massign_opt(iseq, ret, RNODE_MASGN(node)->nd_value, RNODE_MASGN(node)->nd_head)) {
6256 state.lhs_level = popped ? 0 : 1;
6259 state.first_memo = NULL;
6260 state.last_memo = NULL;
6270 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
6274 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
6275 for (
int i = 0; i < memo->num_args; i++) {
6276 INSERT_BEFORE_INSN1(memo->before_insn, nd_line(memo->line_node), nd_node_id(memo->line_node), topn, topn_arg);
6278 tmp_memo = memo->next;
6287 if (!popped && state.num_args >= 1) {
6289 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
6297constant_path_size(
const NODE *node,
bool *absolute)
6301 switch (nd_type(node)) {
6308 node = RNODE_COLON2(node)->nd_head;
6317static rb_const_path_t
6318compile_const_path(rb_iseq_t *iseq,
const NODE *node)
6320 bool absolute =
false;
6321 size_t size = constant_path_size(node, &absolute);
6329 for (
size_t i = size; i > 0; i--) {
6330 switch (nd_type(node)) {
6332 ids[i - 1] = RNODE_CONST(node)->nd_vid;
6335 ids[i - 1] = RNODE_COLON3(node)->nd_mid;
6338 ids[i - 1] = RNODE_COLON2(node)->nd_mid;
6339 node = RNODE_COLON2(node)->nd_head;
6343 rb_const_path_t const_path = rb_const_path_from_ids(absolute, ids, size);
6349compile_const_prefix(rb_iseq_t *iseq,
const NODE *
const node,
6350 LINK_ANCHOR *
const pref, LINK_ANCHOR *
const body)
6352 switch (nd_type(node)) {
6354 debugi(
"compile_const_prefix - colon", RNODE_CONST(node)->nd_vid);
6355 ADD_INSN1(body, node, putobject,
Qtrue);
6356 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
6359 debugi(
"compile_const_prefix - colon3", RNODE_COLON3(node)->nd_mid);
6360 ADD_INSN(body, node, pop);
6361 ADD_INSN1(body, node, putobject,
rb_cObject);
6362 ADD_INSN1(body, node, putobject,
Qtrue);
6363 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
6366 CHECK(compile_const_prefix(iseq, RNODE_COLON2(node)->nd_head, pref, body));
6367 debugi(
"compile_const_prefix - colon2", RNODE_COLON2(node)->nd_mid);
6368 ADD_INSN1(body, node, putobject,
Qfalse);
6369 ADD_INSN1(body, node, getconstant,
ID2SYM(RNODE_COLON2(node)->nd_mid));
6372 CHECK(COMPILE(pref,
"const colon2 prefix", node));
6379cpath_const_p(
const NODE *node)
6381 switch (nd_type(node)) {
6386 if (RNODE_COLON2(node)->nd_head) {
6387 return cpath_const_p(RNODE_COLON2(node)->nd_head);
6396compile_cpath(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
const NODE *cpath)
6398 if (nd_type_p(cpath, NODE_COLON3)) {
6400 ADD_INSN1(ret, cpath, putobject,
rb_cObject);
6401 return VM_DEFINECLASS_FLAG_SCOPED;
6403 else if (nd_type_p(cpath, NODE_COLON2) && RNODE_COLON2(cpath)->nd_head) {
6405 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", RNODE_COLON2(cpath)->nd_head));
6406 int flags = VM_DEFINECLASS_FLAG_SCOPED;
6407 if (!cpath_const_p(RNODE_COLON2(cpath)->nd_head)) {
6408 flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
6414 ADD_INSN1(ret, cpath, putspecialobject,
6415 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
6421private_recv_p(
const NODE *node)
6423 NODE *recv = get_nd_recv(node);
6424 if (recv && nd_type_p(recv, NODE_SELF)) {
6425 return RNODE_SELF(recv)->nd_state != 0;
6431defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6432 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
bool ignore);
6435compile_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);
6438defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6439 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
6442 enum defined_type expr_type = DEFINED_NOT_DEFINED;
6443 enum node_type
type;
6444 const int line = nd_line(node);
6445 const NODE *line_node = node;
6447 switch (
type = nd_type(node)) {
6451 expr_type = DEFINED_NIL;
6454 expr_type = DEFINED_SELF;
6457 expr_type = DEFINED_TRUE;
6460 expr_type = DEFINED_FALSE;
6465 const NODE *vals = (nd_type(node) == NODE_HASH) ? RNODE_HASH(node)->nd_head : node;
6469 if (RNODE_LIST(vals)->nd_head) {
6470 defined_expr0(iseq, ret, RNODE_LIST(vals)->nd_head, lfinish,
Qfalse,
false);
6473 lfinish[1] = NEW_LABEL(line);
6475 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6477 }
while ((vals = RNODE_LIST(vals)->nd_next) != NULL);
6490 case NODE_IMAGINARY:
6495 expr_type = DEFINED_EXPR;
6499 defined_expr0(iseq, ret, RNODE_LIST(node)->nd_head, lfinish,
Qfalse,
false);
6501 lfinish[1] = NEW_LABEL(line);
6503 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6504 expr_type = DEFINED_EXPR;
6510 expr_type = DEFINED_LVAR;
6513#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
6515 ADD_INSN3(ret, line_node, definedivar,
6516 ID2SYM(RNODE_IVAR(node)->nd_vid), get_ivar_ic_value(iseq,RNODE_IVAR(node)->nd_vid), PUSH_VAL(DEFINED_IVAR));
6520 ADD_INSN(ret, line_node, putnil);
6521 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
6522 ID2SYM(RNODE_GVAR(node)->nd_vid), PUSH_VAL(DEFINED_GVAR));
6526 ADD_INSN(ret, line_node, putnil);
6527 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
6528 ID2SYM(RNODE_CVAR(node)->nd_vid), PUSH_VAL(DEFINED_CVAR));
6532 ADD_INSN(ret, line_node, putnil);
6533 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
6534 ID2SYM(RNODE_CONST(node)->nd_vid), PUSH_VAL(DEFINED_CONST));
6538 lfinish[1] = NEW_LABEL(line);
6540 defined_expr0(iseq, ret, RNODE_COLON2(node)->nd_head, lfinish,
Qfalse,
false);
6541 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6542 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", RNODE_COLON2(node)->nd_head));
6545 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
6546 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6549 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6550 ID2SYM(RNODE_COLON2(node)->nd_mid), PUSH_VAL(DEFINED_METHOD));
6554 ADD_INSN1(ret, line_node, putobject,
rb_cObject);
6555 ADD_INSN3(ret, line_node, defined,
6556 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(RNODE_COLON3(node)->nd_mid), PUSH_VAL(DEFINED_CONST));
6564 case NODE_ATTRASGN:{
6565 const int explicit_receiver =
6566 (
type == NODE_CALL ||
type == NODE_OPCALL ||
6567 (
type == NODE_ATTRASGN && !private_recv_p(node)));
6569 if (get_nd_args(node) || explicit_receiver) {
6571 lfinish[1] = NEW_LABEL(line);
6574 lfinish[2] = NEW_LABEL(line);
6577 if (get_nd_args(node)) {
6578 defined_expr0(iseq, ret, get_nd_args(node), lfinish,
Qfalse,
false);
6579 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6581 if (explicit_receiver) {
6582 defined_expr0(iseq, ret, get_nd_recv(node), lfinish,
Qfalse,
true);
6583 switch (nd_type(get_nd_recv(node))) {
6589 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
6590 compile_call(iseq, ret, get_nd_recv(node), nd_type(get_nd_recv(node)), line_node, 0,
true);
6593 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
6594 NO_CHECK(COMPILE(ret,
"defined/recv", get_nd_recv(node)));
6598 ADD_INSN(ret, line_node, dup);
6600 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
6601 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6604 ADD_INSN(ret, line_node, putself);
6606 ADD_INSN(ret, line_node, dup);
6608 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
6609 ID2SYM(get_node_call_nd_mid(node)), PUSH_VAL(DEFINED_METHOD));
6615 ADD_INSN(ret, line_node, putnil);
6616 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
6617 PUSH_VAL(DEFINED_YIELD));
6618 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
6623 ADD_INSN(ret, line_node, putnil);
6624 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
6625 INT2FIX((RNODE_BACK_REF(node)->nd_nth << 1) | (
type == NODE_BACK_REF)),
6626 PUSH_VAL(DEFINED_GVAR));
6631 ADD_INSN(ret, line_node, putnil);
6632 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
6633 PUSH_VAL(DEFINED_ZSUPER));
6639 case NODE_OP_ASGN_OR:
6640 case NODE_OP_ASGN_AND:
6649 expr_type = DEFINED_ASGN;
6656 VALUE str = rb_iseq_defined_string(expr_type);
6657 ADD_INSN1(ret, line_node, putobject, str);
6660 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6665build_defined_rescue_iseq(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const void *unused)
6667 ADD_SYNTHETIC_INSN(ret, 0, -1, putnil);
6668 iseq_set_exception_local_table(iseq);
6672defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
6673 const NODE *
const node, LABEL **lfinish,
VALUE needstr,
bool ignore)
6675 LINK_ELEMENT *lcur = ret->last;
6676 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
6678 int line = nd_line(node);
6679 LABEL *lstart = NEW_LABEL(line);
6680 LABEL *lend = NEW_LABEL(line);
6681 const rb_iseq_t *rescue;
6683 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
6684 rescue = NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc,
6686 ISEQ_BODY(iseq)->location.label),
6687 ISEQ_TYPE_RESCUE, 0);
6688 lstart->rescued = LABEL_RESCUE_BEG;
6689 lend->rescued = LABEL_RESCUE_END;
6690 APPEND_LABEL(ret, lcur, lstart);
6691 ADD_LABEL(ret, lend);
6693 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
6699compile_defined_expr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE needstr,
bool ignore)
6701 const int line = nd_line(node);
6702 const NODE *line_node = node;
6703 if (!RNODE_DEFINED(node)->nd_head) {
6704 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
6705 ADD_INSN1(ret, line_node, putobject, str);
6709 LINK_ELEMENT *last = ret->last;
6710 lfinish[0] = NEW_LABEL(line);
6713 defined_expr(iseq, ret, RNODE_DEFINED(node)->nd_head, lfinish, needstr, ignore);
6715 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(line_node), nd_node_id(line_node), BIN(putnil), 0)->link);
6716 ADD_INSN(ret, line_node, swap);
6718 ADD_LABEL(ret, lfinish[2]);
6720 ADD_INSN(ret, line_node, pop);
6721 ADD_LABEL(ret, lfinish[1]);
6723 ADD_LABEL(ret, lfinish[0]);
6729make_name_for_block(
const rb_iseq_t *orig_iseq)
6732 const rb_iseq_t *iseq = orig_iseq;
6734 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
6735 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
6736 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
6739 iseq = ISEQ_BODY(iseq)->parent_iseq;
6744 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
6747 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
6752push_ensure_entry(rb_iseq_t *iseq,
6756 enl->ensure_node = node;
6757 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6759 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
6763add_ensure_range(rb_iseq_t *iseq,
struct ensure_range *erange,
6764 LABEL *lstart, LABEL *lend)
6769 while (erange->next != 0) {
6770 erange = erange->next;
6774 ne->end = erange->end;
6775 erange->end = lstart;
6781has_ensure_iseq(
const rb_iseq_t *iseq)
6784 for (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack; e; e = e->prev) {
6785 if (e->ensure_node)
return true;
6791can_add_ensure_iseq(
const rb_iseq_t *iseq)
6793 return !ISEQ_COMPILE_DATA(iseq)->in_rescue || !has_ensure_iseq(iseq);
6797can_inline_return(
const rb_iseq_t *iseq,
enum rb_iseq_type
type)
6799 return type == ISEQ_TYPE_METHOD &&
6800 ((!ISEQ_COMPILE_DATA(iseq)->in_rescue && !ISEQ_COMPILE_DATA(iseq)->redo_label) || !has_ensure_iseq(iseq));
6804add_ensure_iseq(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
int is_return)
6809 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
6811 DECL_ANCHOR(ensure);
6813 INIT_ANCHOR(ensure);
6815 if (enlp->erange != NULL) {
6816 DECL_ANCHOR(ensure_part);
6817 LABEL *lstart = NEW_LABEL(0);
6818 LABEL *lend = NEW_LABEL(0);
6819 INIT_ANCHOR(ensure_part);
6821 add_ensure_range(iseq, enlp->erange, lstart, lend);
6823 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
6824 ADD_LABEL(ensure_part, lstart);
6825 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
6826 ADD_LABEL(ensure_part, lend);
6827 ADD_SEQ(ensure, ensure_part);
6836 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
6837 ADD_SEQ(ret, ensure);
6842check_keyword(
const NODE *node)
6846 if (nd_type_p(node, NODE_LIST)) {
6847 while (RNODE_LIST(node)->nd_next) {
6848 node = RNODE_LIST(node)->nd_next;
6850 node = RNODE_LIST(node)->nd_head;
6853 return keyword_node_p(node);
6858keyword_node_single_splat_p(NODE *kwnode)
6862 NODE *node = RNODE_HASH(kwnode)->nd_head;
6863 return RNODE_LIST(node)->nd_head == NULL &&
6864 RNODE_LIST(RNODE_LIST(node)->nd_next)->nd_next == NULL;
6868compile_single_keyword_splat_mutable(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6869 NODE *kwnode,
unsigned int *flag_ptr)
6871 if (flag_ptr) *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6872 ADD_INSN1(args, argn, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6873 ADD_INSN1(args, argn, newhash,
INT2FIX(0));
6874 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6875 ADD_SEND(args, argn, id_core_hash_merge_kwd,
INT2FIX(2));
6878#define SPLATARRAY_FALSE 0
6879#define SPLATARRAY_TRUE 1
6880#define DUP_SINGLE_KW_SPLAT 2
6883setup_args_core(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
6884 unsigned int *dup_rest,
unsigned int *flag_ptr,
struct rb_callinfo_kwarg **kwarg_ptr)
6886 if (!argn)
return 0;
6888 NODE *kwnode = NULL;
6890 switch (nd_type(argn)) {
6893 int len = compile_args(iseq, args, argn, &kwnode);
6894 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_ARGS_SPLAT) == 0);
6897 if (kwarg_ptr && compile_keyword_arg(iseq, args, kwnode, kwarg_ptr, flag_ptr)) {
6901 if (keyword_node_single_splat_p(kwnode) && (*dup_rest & DUP_SINGLE_KW_SPLAT)) {
6902 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6905 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6914 NO_CHECK(COMPILE(args,
"args (splat)", RNODE_SPLAT(argn)->nd_head));
6915 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6916 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6917 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6918 RUBY_ASSERT(flag_ptr == NULL || (*flag_ptr & VM_CALL_KW_SPLAT) == 0);
6921 case NODE_ARGSCAT: {
6922 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6923 int argc = setup_args_core(iseq, args, RNODE_ARGSCAT(argn)->nd_head, dup_rest, NULL, NULL);
6924 bool args_pushed =
false;
6926 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_body, NODE_LIST)) {
6927 int rest_len = compile_args(iseq, args, RNODE_ARGSCAT(argn)->nd_body, &kwnode);
6928 if (kwnode) rest_len--;
6929 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(rest_len));
6933 RUBY_ASSERT(!check_keyword(RNODE_ARGSCAT(argn)->nd_body));
6934 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSCAT(argn)->nd_body));
6937 if (nd_type_p(RNODE_ARGSCAT(argn)->nd_head, NODE_LIST)) {
6938 ADD_INSN1(args, argn, splatarray, RBOOL(*dup_rest & SPLATARRAY_TRUE));
6939 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
6942 else if (!args_pushed) {
6943 ADD_INSN(args, argn, concattoarray);
6949 if (flag_ptr) *flag_ptr |= VM_CALL_KW_SPLAT;
6950 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6956 case NODE_ARGSPUSH: {
6957 if (flag_ptr) *flag_ptr |= VM_CALL_ARGS_SPLAT;
6958 int argc = setup_args_core(iseq, args, RNODE_ARGSPUSH(argn)->nd_head, dup_rest, NULL, NULL);
6960 if (nd_type_p(RNODE_ARGSPUSH(argn)->nd_body, NODE_LIST)) {
6961 int rest_len = compile_args(iseq, args, RNODE_ARGSPUSH(argn)->nd_body, &kwnode);
6962 if (kwnode) rest_len--;
6963 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
6964 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6967 if (keyword_node_p(RNODE_ARGSPUSH(argn)->nd_body)) {
6968 kwnode = RNODE_ARGSPUSH(argn)->nd_body;
6971 NO_CHECK(COMPILE(args,
"args (cat: splat)", RNODE_ARGSPUSH(argn)->nd_body));
6972 ADD_INSN1(args, argn, pushtoarray,
INT2FIX(1));
6978 if (flag_ptr) *flag_ptr |= VM_CALL_KW_SPLAT;
6979 if (!keyword_node_single_splat_p(kwnode)) {
6980 if (flag_ptr) *flag_ptr |= VM_CALL_KW_SPLAT_MUT;
6981 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6983 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
6984 compile_single_keyword_splat_mutable(iseq, args, argn, kwnode, flag_ptr);
6987 compile_hash(iseq, args, kwnode, TRUE, FALSE);
6995 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
7001setup_args_splat_mut(
unsigned int *flag,
int dup_rest,
int initial_dup_rest)
7003 if ((*flag & VM_CALL_ARGS_SPLAT) && dup_rest != initial_dup_rest) {
7004 *flag |= VM_CALL_ARGS_SPLAT_MUT;
7009setup_args_dup_rest_p(
const NODE *argn)
7011 switch(nd_type(argn)) {
7022 case NODE_IMAGINARY:
7035 return setup_args_dup_rest_p(RNODE_COLON2(argn)->nd_head);
7038 if (setup_args_dup_rest_p(RNODE_LIST(argn)->nd_head)) {
7041 argn = RNODE_LIST(argn)->nd_next;
7050setup_args(rb_iseq_t *iseq, LINK_ANCHOR *
const args,
const NODE *argn,
7054 unsigned int dup_rest = SPLATARRAY_TRUE, initial_dup_rest;
7058 const NODE *check_arg = nd_type_p(argn, NODE_BLOCK_PASS) ?
7059 RNODE_BLOCK_PASS(argn)->nd_head : argn;
7062 switch(nd_type(check_arg)) {
7065 dup_rest = SPLATARRAY_FALSE;
7069 dup_rest = !nd_type_p(RNODE_ARGSCAT(check_arg)->nd_head, NODE_LIST);
7071 case(NODE_ARGSPUSH):
7073 dup_rest = !((nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_SPLAT) ||
7074 (nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_head, NODE_ARGSCAT) &&
7075 nd_type_p(RNODE_ARGSCAT(RNODE_ARGSPUSH(check_arg)->nd_head)->nd_head, NODE_LIST))) &&
7076 nd_type_p(RNODE_ARGSPUSH(check_arg)->nd_body, NODE_HASH) &&
7077 !RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_brace);
7079 if (dup_rest == SPLATARRAY_FALSE) {
7081 NODE *node = RNODE_HASH(RNODE_ARGSPUSH(check_arg)->nd_body)->nd_head;
7083 NODE *key_node = RNODE_LIST(node)->nd_head;
7084 if (key_node && setup_args_dup_rest_p(key_node)) {
7085 dup_rest = SPLATARRAY_TRUE;
7089 node = RNODE_LIST(node)->nd_next;
7090 NODE *value_node = RNODE_LIST(node)->nd_head;
7091 if (setup_args_dup_rest_p(value_node)) {
7092 dup_rest = SPLATARRAY_TRUE;
7096 node = RNODE_LIST(node)->nd_next;
7105 if (check_arg != argn && setup_args_dup_rest_p(RNODE_BLOCK_PASS(argn)->nd_body)) {
7107 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
7110 initial_dup_rest = dup_rest;
7112 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
7113 DECL_ANCHOR(arg_block);
7114 INIT_ANCHOR(arg_block);
7116 if (RNODE_BLOCK_PASS(argn)->forwarding && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
7117 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size;
7119 RUBY_ASSERT(nd_type_p(RNODE_BLOCK_PASS(argn)->nd_head, NODE_ARGSPUSH));
7120 const NODE * arg_node =
7121 RNODE_ARGSPUSH(RNODE_BLOCK_PASS(argn)->nd_head)->nd_head;
7128 if (nd_type_p(arg_node, NODE_ARGSCAT)) {
7129 argc += setup_args_core(iseq, args, RNODE_ARGSCAT(arg_node)->nd_head, &dup_rest, flag, keywords);
7132 *flag |= VM_CALL_FORWARDING;
7134 ADD_GETLOCAL(args, argn, idx, get_lvar_level(iseq));
7135 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
7139 *flag |= VM_CALL_ARGS_BLOCKARG;
7141 NO_CHECK(COMPILE(arg_block,
"block", RNODE_BLOCK_PASS(argn)->nd_body));
7144 if (LIST_INSN_SIZE_ONE(arg_block)) {
7145 LINK_ELEMENT *elem = FIRST_ELEMENT(arg_block);
7146 if (IS_INSN(elem)) {
7147 INSN *iobj = (INSN *)elem;
7148 if (iobj->insn_id == BIN(getblockparam)) {
7149 iobj->insn_id = BIN(getblockparamproxy);
7153 ret =
INT2FIX(setup_args_core(iseq, args, RNODE_BLOCK_PASS(argn)->nd_head, &dup_rest, flag, keywords));
7154 ADD_SEQ(args, arg_block);
7157 ret =
INT2FIX(setup_args_core(iseq, args, argn, &dup_rest, flag, keywords));
7159 setup_args_splat_mut(flag, dup_rest, initial_dup_rest);
7164build_postexe_iseq(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const void *ptr)
7166 const NODE *body = ptr;
7167 int line = nd_line(body);
7169 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
7171 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7172 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
7174 iseq_set_local_table(iseq, 0, 0);
7178compile_named_capture_assign(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node)
7182 int line = nd_line(node);
7183 const NODE *line_node = node;
7184 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
7186#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
7187 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
7191 ADD_INSN(ret, line_node, dup);
7192 ADD_INSNL(ret, line_node, branchunless, fail_label);
7194 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
7196 if (RNODE_BLOCK(vars)->nd_next) {
7197 ADD_INSN(ret, line_node, dup);
7200 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
7202 cap = new_insn_send(iseq, nd_line(line_node), nd_node_id(line_node), idAREF,
INT2FIX(1),
7204 ELEM_INSERT_PREV(last->next, (LINK_ELEMENT *)cap);
7205#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
7206 if (!RNODE_BLOCK(vars)->nd_next && vars == node) {
7211 ADD_INSNL(nom, line_node, jump, end_label);
7212 ADD_LABEL(nom, fail_label);
7214 ADD_INSN(nom, line_node, pop);
7215 ADD_INSN(nom, line_node, putnil);
7217 ADD_LABEL(nom, end_label);
7218 (nom->last->next = cap->link.next)->prev = nom->last;
7219 (cap->link.next = nom->anchor.next)->prev = &cap->link;
7224 ADD_INSNL(ret, line_node, jump, end_label);
7225 ADD_LABEL(ret, fail_label);
7226 ADD_INSN(ret, line_node, pop);
7227 for (vars = node; vars; vars = RNODE_BLOCK(vars)->nd_next) {
7229 NO_CHECK(COMPILE_POPPED(ret,
"capture", RNODE_BLOCK(vars)->nd_head));
7231 ((INSN*)last)->insn_id = BIN(putnil);
7232 ((INSN*)last)->operand_size = 0;
7234 ADD_LABEL(ret, end_label);
7238optimizable_range_item_p(
const NODE *n)
7240 if (!n)
return FALSE;
7241 switch (nd_type(n)) {
7254optimized_range_item(
const NODE *n)
7256 switch (nd_type(n)) {
7258 return rb_node_line_lineno_val(n);
7260 return rb_node_integer_literal_val(n);
7262 return rb_node_float_literal_val(n);
7264 return rb_node_rational_literal_val(n);
7265 case NODE_IMAGINARY:
7266 return rb_node_imaginary_literal_val(n);
7270 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(n)));
7275compile_if(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
7277 const NODE *
const node_body =
type == NODE_IF ? RNODE_IF(node)->nd_body : RNODE_UNLESS(node)->nd_else;
7278 const NODE *
const node_else =
type == NODE_IF ? RNODE_IF(node)->nd_else : RNODE_UNLESS(node)->nd_body;
7280 const int line = nd_line(node);
7281 const NODE *line_node = node;
7282 DECL_ANCHOR(cond_seq);
7283 LABEL *then_label, *else_label, *end_label;
7286 INIT_ANCHOR(cond_seq);
7287 then_label = NEW_LABEL(line);
7288 else_label = NEW_LABEL(line);
7291 NODE *cond = RNODE_IF(node)->nd_cond;
7292 if (nd_type(cond) == NODE_BLOCK) {
7293 cond = RNODE_BLOCK(cond)->nd_head;
7296 CHECK(compile_branch_condition(iseq, cond_seq, cond, then_label, else_label));
7297 ADD_SEQ(ret, cond_seq);
7299 if (then_label->refcnt && else_label->refcnt) {
7300 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
type == NODE_IF ?
"if" :
"unless");
7303 if (then_label->refcnt) {
7304 ADD_LABEL(ret, then_label);
7306 DECL_ANCHOR(then_seq);
7307 INIT_ANCHOR(then_seq);
7308 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
7310 if (else_label->refcnt) {
7311 const NODE *
const coverage_node = node_body ? node_body : node;
7312 add_trace_branch_coverage(
7315 nd_code_loc(coverage_node),
7316 nd_node_id(coverage_node),
7318 type == NODE_IF ?
"then" :
"else",
7320 end_label = NEW_LABEL(line);
7321 ADD_INSNL(then_seq, line_node, jump, end_label);
7323 ADD_INSN(then_seq, line_node, pop);
7326 ADD_SEQ(ret, then_seq);
7329 if (else_label->refcnt) {
7330 ADD_LABEL(ret, else_label);
7332 DECL_ANCHOR(else_seq);
7333 INIT_ANCHOR(else_seq);
7334 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
7336 if (then_label->refcnt) {
7337 const NODE *
const coverage_node = node_else ? node_else : node;
7338 add_trace_branch_coverage(
7341 nd_code_loc(coverage_node),
7342 nd_node_id(coverage_node),
7344 type == NODE_IF ?
"else" :
"then",
7347 ADD_SEQ(ret, else_seq);
7351 ADD_LABEL(ret, end_label);
7358compile_case(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
7361 const NODE *node = orig_node;
7362 LABEL *endlabel, *elselabel;
7364 DECL_ANCHOR(body_seq);
7365 DECL_ANCHOR(cond_seq);
7366 int only_special_literals = 1;
7367 VALUE literals = cdhash_new(0);
7369 enum node_type
type;
7370 const NODE *line_node;
7375 INIT_ANCHOR(body_seq);
7376 INIT_ANCHOR(cond_seq);
7378 CHECK(COMPILE(head,
"case base", RNODE_CASE(node)->nd_head));
7380 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"case");
7382 node = RNODE_CASE(node)->nd_body;
7383 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
7384 type = nd_type(node);
7385 line = nd_line(node);
7388 endlabel = NEW_LABEL(line);
7389 elselabel = NEW_LABEL(line);
7393 while (
type == NODE_WHEN) {
7396 l1 = NEW_LABEL(line);
7397 ADD_LABEL(body_seq, l1);
7398 ADD_INSN(body_seq, line_node, pop);
7400 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7401 add_trace_branch_coverage(
7404 nd_code_loc(coverage_node),
7405 nd_node_id(coverage_node),
7410 CHECK(COMPILE_(body_seq,
"when body", RNODE_WHEN(node)->nd_body, popped));
7411 ADD_INSNL(body_seq, line_node, jump, endlabel);
7413 vals = RNODE_WHEN(node)->nd_head;
7415 switch (nd_type(vals)) {
7417 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
7418 if (only_special_literals < 0)
return COMPILE_NG;
7423 only_special_literals = 0;
7424 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
7427 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
7431 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
7434 node = RNODE_WHEN(node)->nd_next;
7438 type = nd_type(node);
7439 line = nd_line(node);
7444 ADD_LABEL(cond_seq, elselabel);
7445 ADD_INSN(cond_seq, line_node, pop);
7446 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
7447 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7448 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7451 debugs(
"== else (implicit)\n");
7452 ADD_LABEL(cond_seq, elselabel);
7453 ADD_INSN(cond_seq, orig_node, pop);
7454 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
7456 ADD_INSN(cond_seq, orig_node, putnil);
7458 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7461 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7462 ADD_INSN(ret, orig_node, dup);
7463 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
7465 LABEL_REF(elselabel);
7468 ADD_SEQ(ret, cond_seq);
7469 ADD_SEQ(ret, body_seq);
7470 ADD_LABEL(ret, endlabel);
7475compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
7479 const NODE *node = RNODE_CASE2(orig_node)->nd_body;
7481 DECL_ANCHOR(body_seq);
7485 branches = decl_branch_base(iseq, nd_node_id(orig_node), nd_code_loc(orig_node),
"case");
7487 INIT_ANCHOR(body_seq);
7488 endlabel = NEW_LABEL(nd_line(node));
7490 while (node && nd_type_p(node, NODE_WHEN)) {
7491 const int line = nd_line(node);
7492 LABEL *l1 = NEW_LABEL(line);
7493 ADD_LABEL(body_seq, l1);
7495 const NODE *
const coverage_node = RNODE_WHEN(node)->nd_body ? RNODE_WHEN(node)->nd_body : node;
7496 add_trace_branch_coverage(
7499 nd_code_loc(coverage_node),
7500 nd_node_id(coverage_node),
7505 CHECK(COMPILE_(body_seq,
"when", RNODE_WHEN(node)->nd_body, popped));
7506 ADD_INSNL(body_seq, node, jump, endlabel);
7508 vals = RNODE_WHEN(node)->nd_head;
7510 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
7512 switch (nd_type(vals)) {
7516 val = RNODE_LIST(vals)->nd_head;
7517 lnext = NEW_LABEL(nd_line(val));
7518 debug_compile(
"== when2\n", (
void)0);
7519 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
7520 ADD_LABEL(ret, lnext);
7521 vals = RNODE_LIST(vals)->nd_next;
7527 ADD_INSN(ret, vals, putnil);
7528 CHECK(COMPILE(ret,
"when2/cond splat", vals));
7529 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7530 ADD_INSNL(ret, vals, branchif, l1);
7533 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
7535 node = RNODE_WHEN(node)->nd_next;
7538 const NODE *
const coverage_node = node ? node : orig_node;
7539 add_trace_branch_coverage(
7542 nd_code_loc(coverage_node),
7543 nd_node_id(coverage_node),
7547 CHECK(COMPILE_(ret,
"else", node, popped));
7548 ADD_INSNL(ret, orig_node, jump, endlabel);
7550 ADD_SEQ(ret, body_seq);
7551 ADD_LABEL(ret, endlabel);
7555static 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);
7557static 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);
7558static 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);
7559static int iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
7560static 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);
7561static int iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
7563#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
7564#define CASE3_BI_OFFSET_ERROR_STRING 1
7565#define CASE3_BI_OFFSET_KEY_ERROR_P 2
7566#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
7567#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
7570iseq_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)
7572 const int line = nd_line(node);
7573 const NODE *line_node = node;
7575 switch (nd_type(node)) {
7629 const NODE *args = RNODE_ARYPTN(node)->pre_args;
7630 const int pre_args_num = RNODE_ARYPTN(node)->pre_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->pre_args)->as.nd_alen) : 0;
7631 const int post_args_num = RNODE_ARYPTN(node)->post_args ?
rb_long2int(RNODE_LIST(RNODE_ARYPTN(node)->post_args)->as.nd_alen) : 0;
7633 const int min_argc = pre_args_num + post_args_num;
7634 const int use_rest_num = RNODE_ARYPTN(node)->rest_arg && (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) ||
7635 (!NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg) && post_args_num > 0));
7637 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7639 match_failed = NEW_LABEL(line);
7640 type_error = NEW_LABEL(line);
7641 deconstruct = NEW_LABEL(line);
7642 deconstructed = NEW_LABEL(line);
7645 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7646 ADD_INSN(ret, line_node, swap);
7652 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7654 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7656 ADD_INSN(ret, line_node, dup);
7657 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7658 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7659 ADD_SEND(ret, line_node, RNODE_ARYPTN(node)->rest_arg ? idGE : idEq,
INT2FIX(1));
7660 if (in_single_pattern) {
7661 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
7662 RNODE_ARYPTN(node)->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
7663 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
7664 INT2FIX(min_argc), base_index + 1 ));
7666 ADD_INSNL(ret, line_node, branchunless, match_failed);
7668 for (i = 0; i < pre_args_num; i++) {
7669 ADD_INSN(ret, line_node, dup);
7670 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
7671 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7672 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7673 args = RNODE_LIST(args)->nd_next;
7676 if (RNODE_ARYPTN(node)->rest_arg) {
7677 if (NODE_NAMED_REST_P(RNODE_ARYPTN(node)->rest_arg)) {
7678 ADD_INSN(ret, line_node, dup);
7679 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
7680 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7681 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7682 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7683 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7684 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
7685 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7687 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_ARYPTN(node)->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7690 if (post_args_num > 0) {
7691 ADD_INSN(ret, line_node, dup);
7692 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7693 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
7694 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7695 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7696 ADD_INSN(ret, line_node, pop);
7701 args = RNODE_ARYPTN(node)->post_args;
7702 for (i = 0; i < post_args_num; i++) {
7703 ADD_INSN(ret, line_node, dup);
7705 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
7706 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7707 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7709 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7710 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
7711 args = RNODE_LIST(args)->nd_next;
7714 ADD_INSN(ret, line_node, pop);
7716 ADD_INSN(ret, line_node, pop);
7718 ADD_INSNL(ret, line_node, jump, matched);
7719 ADD_INSN(ret, line_node, putnil);
7721 ADD_INSN(ret, line_node, putnil);
7724 ADD_LABEL(ret, type_error);
7725 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7727 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7728 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7729 ADD_INSN(ret, line_node, pop);
7731 ADD_LABEL(ret, match_failed);
7732 ADD_INSN(ret, line_node, pop);
7734 ADD_INSN(ret, line_node, pop);
7736 ADD_INSNL(ret, line_node, jump, unmatched);
7789 const NODE *args = RNODE_FNDPTN(node)->args;
7790 const int args_num = RNODE_FNDPTN(node)->args ?
rb_long2int(RNODE_LIST(RNODE_FNDPTN(node)->args)->as.nd_alen) : 0;
7792 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
7793 match_failed = NEW_LABEL(line);
7794 type_error = NEW_LABEL(line);
7795 deconstruct = NEW_LABEL(line);
7796 deconstructed = NEW_LABEL(line);
7798 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7800 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
7802 ADD_INSN(ret, line_node, dup);
7803 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7804 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7805 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
7806 if (in_single_pattern) {
7807 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 ));
7809 ADD_INSNL(ret, line_node, branchunless, match_failed);
7812 LABEL *while_begin = NEW_LABEL(nd_line(node));
7813 LABEL *next_loop = NEW_LABEL(nd_line(node));
7814 LABEL *find_succeeded = NEW_LABEL(line);
7815 LABEL *find_failed = NEW_LABEL(nd_line(node));
7818 ADD_INSN(ret, line_node, dup);
7819 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7821 ADD_INSN(ret, line_node, dup);
7822 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7823 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
7825 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7827 ADD_LABEL(ret, while_begin);
7829 ADD_INSN(ret, line_node, dup);
7830 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7831 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
7832 ADD_INSNL(ret, line_node, branchunless, find_failed);
7834 for (j = 0; j < args_num; j++) {
7835 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7836 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7838 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
7839 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7841 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
7843 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_LIST(args)->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
7844 args = RNODE_LIST(args)->nd_next;
7847 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->pre_rest_arg)) {
7848 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7849 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7850 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7851 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7852 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));
7854 if (NODE_NAMED_REST_P(RNODE_FNDPTN(node)->post_rest_arg)) {
7855 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7856 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
7857 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
7858 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7859 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7860 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
7861 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));
7863 ADD_INSNL(ret, line_node, jump, find_succeeded);
7865 ADD_LABEL(ret, next_loop);
7866 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7867 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
7868 ADD_INSNL(ret, line_node, jump, while_begin);
7870 ADD_LABEL(ret, find_failed);
7871 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7872 if (in_single_pattern) {
7873 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7874 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
7875 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
7876 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7877 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7879 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7880 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7882 ADD_INSN(ret, line_node, pop);
7883 ADD_INSN(ret, line_node, pop);
7885 ADD_INSNL(ret, line_node, jump, match_failed);
7886 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
7888 ADD_LABEL(ret, find_succeeded);
7889 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
7892 ADD_INSN(ret, line_node, pop);
7893 ADD_INSNL(ret, line_node, jump, matched);
7894 ADD_INSN(ret, line_node, putnil);
7896 ADD_LABEL(ret, type_error);
7897 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7899 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
7900 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
7901 ADD_INSN(ret, line_node, pop);
7903 ADD_LABEL(ret, match_failed);
7904 ADD_INSN(ret, line_node, pop);
7905 ADD_INSNL(ret, line_node, jump, unmatched);
7969 LABEL *match_failed, *type_error;
7972 match_failed = NEW_LABEL(line);
7973 type_error = NEW_LABEL(line);
7975 if (RNODE_HSHPTN(node)->nd_pkwargs && !RNODE_HSHPTN(node)->nd_pkwrestarg) {
7976 const NODE *kw_args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
7977 keys =
rb_ary_new_capa(kw_args ? RNODE_LIST(kw_args)->as.nd_alen/2 : 0);
7979 rb_ary_push(keys, get_symbol_value(iseq, RNODE_LIST(kw_args)->nd_head));
7980 kw_args = RNODE_LIST(RNODE_LIST(kw_args)->nd_next)->nd_next;
7984 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
7986 ADD_INSN(ret, line_node, dup);
7987 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct_keys")));
7988 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7989 if (in_single_pattern) {
7990 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
7992 ADD_INSNL(ret, line_node, branchunless, match_failed);
7995 ADD_INSN(ret, line_node, putnil);
7999 ADD_INSN1(ret, line_node, duparray, keys);
8002 ADD_SEND(ret, line_node, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
8004 ADD_INSN(ret, line_node, dup);
8006 ADD_INSNL(ret, line_node, branchunless, type_error);
8008 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
8009 ADD_SEND(ret, line_node, rb_intern(
"dup"),
INT2FIX(0));
8012 if (RNODE_HSHPTN(node)->nd_pkwargs) {
8016 args = RNODE_HASH(RNODE_HSHPTN(node)->nd_pkwargs)->nd_head;
8018 DECL_ANCHOR(match_values);
8019 INIT_ANCHOR(match_values);
8020 keys_num =
rb_long2int(RNODE_LIST(args)->as.nd_alen) / 2;
8021 for (i = 0; i < keys_num; i++) {
8022 NODE *key_node = RNODE_LIST(args)->nd_head;
8023 NODE *value_node = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_head;
8024 VALUE key = get_symbol_value(iseq, key_node);
8026 ADD_INSN(ret, line_node, dup);
8027 ADD_INSN1(ret, line_node, putobject, key);
8028 ADD_SEND(ret, line_node, rb_intern(
"key?"),
INT2FIX(1));
8029 if (in_single_pattern) {
8030 LABEL *match_succeeded;
8031 match_succeeded = NEW_LABEL(line);
8033 ADD_INSN(ret, line_node, dup);
8034 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8038 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
8039 ADD_INSN1(ret, line_node, putobject,
Qtrue);
8040 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
8041 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8042 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
8043 ADD_INSN1(ret, line_node, putobject, key);
8044 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
8046 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
8048 ADD_LABEL(ret, match_succeeded);
8050 ADD_INSNL(ret, line_node, branchunless, match_failed);
8052 ADD_INSN(match_values, line_node, dup);
8053 ADD_INSN1(match_values, line_node, putobject, key);
8054 ADD_SEND(match_values, line_node, RNODE_HSHPTN(node)->nd_pkwrestarg ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
8055 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
8056 args = RNODE_LIST(RNODE_LIST(args)->nd_next)->nd_next;
8058 ADD_SEQ(ret, match_values);
8062 ADD_INSN(ret, line_node, dup);
8063 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
8064 if (in_single_pattern) {
8065 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
8067 ADD_INSNL(ret, line_node, branchunless, match_failed);
8070 if (RNODE_HSHPTN(node)->nd_pkwrestarg) {
8071 if (RNODE_HSHPTN(node)->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
8072 ADD_INSN(ret, line_node, dup);
8073 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
8074 if (in_single_pattern) {
8075 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
8077 ADD_INSNL(ret, line_node, branchunless, match_failed);
8080 ADD_INSN(ret, line_node, dup);
8081 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_HSHPTN(node)->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
8085 ADD_INSN(ret, line_node, pop);
8086 ADD_INSNL(ret, line_node, jump, matched);
8087 ADD_INSN(ret, line_node, putnil);
8089 ADD_LABEL(ret, type_error);
8090 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8092 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
8093 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
8094 ADD_INSN(ret, line_node, pop);
8096 ADD_LABEL(ret, match_failed);
8097 ADD_INSN(ret, line_node, pop);
8098 ADD_INSNL(ret, line_node, jump, unmatched);
8107 case NODE_IMAGINARY:
8135 CHECK(COMPILE(ret,
"case in literal", node));
8136 if (in_single_pattern) {
8137 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
8139 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
8140 if (in_single_pattern) {
8141 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
8143 ADD_INSNL(ret, line_node, branchif, matched);
8144 ADD_INSNL(ret, line_node, jump, unmatched);
8148 ID id = RNODE_LASGN(node)->nd_vid;
8149 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8151 if (in_alt_pattern) {
8152 const char *name = rb_id2name(
id);
8153 if (name && strlen(name) > 0 && name[0] !=
'_') {
8154 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
8160 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8161 ADD_INSNL(ret, line_node, jump, matched);
8166 ID id = RNODE_DASGN(node)->nd_vid;
8168 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
8170 if (in_alt_pattern) {
8171 const char *name = rb_id2name(
id);
8172 if (name && strlen(name) > 0 && name[0] !=
'_') {
8173 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
8180 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
8184 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
8185 ADD_INSNL(ret, line_node, jump, matched);
8190 LABEL *match_failed;
8191 match_failed = unmatched;
8192 CHECK(iseq_compile_pattern_match(iseq, ret, RNODE_IF(node)->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
8193 CHECK(COMPILE(ret,
"case in if", RNODE_IF(node)->nd_cond));
8194 if (in_single_pattern) {
8195 LABEL *match_succeeded;
8196 match_succeeded = NEW_LABEL(line);
8198 ADD_INSN(ret, line_node, dup);
8199 if (nd_type_p(node, NODE_IF)) {
8200 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8203 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
8206 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
8207 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8208 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8209 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8211 ADD_INSN(ret, line_node, pop);
8212 ADD_INSN(ret, line_node, pop);
8214 ADD_LABEL(ret, match_succeeded);
8216 if (nd_type_p(node, NODE_IF)) {
8217 ADD_INSNL(ret, line_node, branchunless, match_failed);
8220 ADD_INSNL(ret, line_node, branchif, match_failed);
8222 ADD_INSNL(ret, line_node, jump, matched);
8227 LABEL *match_failed;
8228 match_failed = NEW_LABEL(line);
8230 n = RNODE_HASH(node)->nd_head;
8231 if (! (nd_type_p(n, NODE_LIST) && RNODE_LIST(n)->as.nd_alen == 2)) {
8232 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8236 ADD_INSN(ret, line_node, dup);
8237 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));
8238 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));
8239 ADD_INSN(ret, line_node, putnil);
8241 ADD_LABEL(ret, match_failed);
8242 ADD_INSN(ret, line_node, pop);
8243 ADD_INSNL(ret, line_node, jump, unmatched);
8247 LABEL *match_succeeded, *fin;
8248 match_succeeded = NEW_LABEL(line);
8249 fin = NEW_LABEL(line);
8251 ADD_INSN(ret, line_node, dup);
8252 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));
8253 ADD_LABEL(ret, match_succeeded);
8254 ADD_INSN(ret, line_node, pop);
8255 ADD_INSNL(ret, line_node, jump, matched);
8256 ADD_INSN(ret, line_node, putnil);
8257 ADD_LABEL(ret, fin);
8258 CHECK(iseq_compile_pattern_each(iseq, ret, RNODE_OR(node)->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
8262 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
8268iseq_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)
8270 LABEL *fin = NEW_LABEL(nd_line(node));
8271 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
8272 ADD_LABEL(ret, fin);
8277iseq_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)
8279 const NODE *line_node = node;
8281 if (RNODE_ARYPTN(node)->nd_pconst) {
8282 ADD_INSN(ret, line_node, dup);
8283 CHECK(COMPILE(ret,
"constant", RNODE_ARYPTN(node)->nd_pconst));
8284 if (in_single_pattern) {
8285 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
8287 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
8288 if (in_single_pattern) {
8289 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
8291 ADD_INSNL(ret, line_node, branchunless, match_failed);
8298iseq_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)
8300 const NODE *line_node = node;
8304 if (use_deconstructed_cache) {
8306 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8307 ADD_INSNL(ret, line_node, branchnil, deconstruct);
8310 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8311 ADD_INSNL(ret, line_node, branchunless, match_failed);
8314 ADD_INSN(ret, line_node, pop);
8315 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
8316 ADD_INSNL(ret, line_node, jump, deconstructed);
8319 ADD_INSNL(ret, line_node, jump, deconstruct);
8322 ADD_LABEL(ret, deconstruct);
8323 ADD_INSN(ret, line_node, dup);
8324 ADD_INSN1(ret, line_node, putobject,
ID2SYM(rb_intern(
"deconstruct")));
8325 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
8328 if (use_deconstructed_cache) {
8329 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
8332 if (in_single_pattern) {
8333 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
8336 ADD_INSNL(ret, line_node, branchunless, match_failed);
8338 ADD_SEND(ret, line_node, rb_intern(
"deconstruct"),
INT2FIX(0));
8341 if (use_deconstructed_cache) {
8342 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
8345 ADD_INSN(ret, line_node, dup);
8347 ADD_INSNL(ret, line_node, branchunless, type_error);
8349 ADD_LABEL(ret, deconstructed);
8355iseq_compile_pattern_set_general_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index)
8365 const int line = nd_line(node);
8366 const NODE *line_node = node;
8367 LABEL *match_succeeded = NEW_LABEL(line);
8369 ADD_INSN(ret, line_node, dup);
8370 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8372 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8373 ADD_INSN1(ret, line_node, putobject, errmsg);
8374 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8375 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
8376 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8378 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8379 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8381 ADD_INSN(ret, line_node, pop);
8382 ADD_INSN(ret, line_node, pop);
8383 ADD_LABEL(ret, match_succeeded);
8389iseq_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)
8399 const int line = nd_line(node);
8400 const NODE *line_node = node;
8401 LABEL *match_succeeded = NEW_LABEL(line);
8403 ADD_INSN(ret, line_node, dup);
8404 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8406 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8407 ADD_INSN1(ret, line_node, putobject, errmsg);
8408 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8409 ADD_INSN(ret, line_node, dup);
8410 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
8411 ADD_INSN1(ret, line_node, putobject, pattern_length);
8412 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
8413 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8415 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8416 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8418 ADD_INSN(ret, line_node, pop);
8419 ADD_INSN(ret, line_node, pop);
8420 ADD_LABEL(ret, match_succeeded);
8426iseq_compile_pattern_set_eqq_errmsg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
8436 const int line = nd_line(node);
8437 const NODE *line_node = node;
8438 LABEL *match_succeeded = NEW_LABEL(line);
8440 ADD_INSN(ret, line_node, dup);
8441 ADD_INSNL(ret, line_node, branchif, match_succeeded);
8443 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8444 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
8445 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
8446 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
8447 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
8448 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
8450 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8451 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
8453 ADD_INSN(ret, line_node, pop);
8454 ADD_INSN(ret, line_node, pop);
8456 ADD_LABEL(ret, match_succeeded);
8457 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
8458 ADD_INSN(ret, line_node, pop);
8459 ADD_INSN(ret, line_node, pop);
8465compile_case3(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const orig_node,
int popped)
8467 const NODE *pattern;
8468 const NODE *node = orig_node;
8469 LABEL *endlabel, *elselabel;
8471 DECL_ANCHOR(body_seq);
8472 DECL_ANCHOR(cond_seq);
8474 enum node_type
type;
8475 const NODE *line_node;
8478 bool single_pattern;
8481 INIT_ANCHOR(body_seq);
8482 INIT_ANCHOR(cond_seq);
8484 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"case");
8486 node = RNODE_CASE3(node)->nd_body;
8487 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
8488 type = nd_type(node);
8489 line = nd_line(node);
8491 single_pattern = !RNODE_IN(node)->nd_next;
8493 endlabel = NEW_LABEL(line);
8494 elselabel = NEW_LABEL(line);
8496 if (single_pattern) {
8498 ADD_INSN(head, line_node, putnil);
8499 ADD_INSN(head, line_node, putnil);
8500 ADD_INSN1(head, line_node, putobject,
Qfalse);
8501 ADD_INSN(head, line_node, putnil);
8503 ADD_INSN(head, line_node, putnil);
8505 CHECK(COMPILE(head,
"case base", RNODE_CASE3(orig_node)->nd_head));
8509 while (
type == NODE_IN) {
8513 ADD_INSN(body_seq, line_node, putnil);
8515 l1 = NEW_LABEL(line);
8516 ADD_LABEL(body_seq, l1);
8517 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
8519 const NODE *
const coverage_node = RNODE_IN(node)->nd_body ? RNODE_IN(node)->nd_body : node;
8520 add_trace_branch_coverage(
8523 nd_code_loc(coverage_node),
8524 nd_node_id(coverage_node),
8529 CHECK(COMPILE_(body_seq,
"in body", RNODE_IN(node)->nd_body, popped));
8530 ADD_INSNL(body_seq, line_node, jump, endlabel);
8532 pattern = RNODE_IN(node)->nd_head;
8534 int pat_line = nd_line(pattern);
8535 LABEL *next_pat = NEW_LABEL(pat_line);
8536 ADD_INSN (cond_seq, pattern, dup);
8538 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
8539 ADD_LABEL(cond_seq, next_pat);
8540 LABEL_UNREMOVABLE(next_pat);
8543 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
8547 node = RNODE_IN(node)->nd_next;
8551 type = nd_type(node);
8552 line = nd_line(node);
8557 ADD_LABEL(cond_seq, elselabel);
8558 ADD_INSN(cond_seq, line_node, pop);
8559 ADD_INSN(cond_seq, line_node, pop);
8560 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(node), nd_node_id(node), branch_id,
"else", branches);
8561 CHECK(COMPILE_(cond_seq,
"else", node, popped));
8562 ADD_INSNL(cond_seq, line_node, jump, endlabel);
8563 ADD_INSN(cond_seq, line_node, putnil);
8565 ADD_INSN(cond_seq, line_node, putnil);
8569 debugs(
"== else (implicit)\n");
8570 ADD_LABEL(cond_seq, elselabel);
8571 add_trace_branch_coverage(iseq, cond_seq, nd_code_loc(orig_node), nd_node_id(orig_node), branch_id,
"else", branches);
8572 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8574 if (single_pattern) {
8582 LABEL *key_error, *fin;
8585 key_error = NEW_LABEL(line);
8586 fin = NEW_LABEL(line);
8589 kw_arg->references = 0;
8590 kw_arg->keyword_len = 2;
8591 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
8592 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
8594 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
8595 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
8597 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8598 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8599 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8600 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8601 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8602 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8603 ADD_INSNL(cond_seq, orig_node, jump, fin);
8605 ADD_LABEL(cond_seq, key_error);
8607 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8608 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
8609 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
8610 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
8611 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
8612 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
8613 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
8614 ADD_SEND_R(cond_seq, orig_node, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
8615 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
8617 ADD_LABEL(cond_seq, fin);
8621 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
8622 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
8624 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
8626 ADD_INSN(cond_seq, orig_node, putnil);
8628 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
8629 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
8631 ADD_INSN(cond_seq, line_node, putnil);
8635 ADD_SEQ(ret, cond_seq);
8636 ADD_SEQ(ret, body_seq);
8637 ADD_LABEL(ret, endlabel);
8641#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
8642#undef CASE3_BI_OFFSET_ERROR_STRING
8643#undef CASE3_BI_OFFSET_KEY_ERROR_P
8644#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
8645#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
8648compile_loop(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
8650 const int line = (int)nd_line(node);
8651 const NODE *line_node = node;
8652 int result = COMPILE_OK;
8654 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
8655 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
8656 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
8657 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
8662 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
8663 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
8664 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
8665 LABEL *end_label = NEW_LABEL(line);
8666 LABEL *adjust_label = NEW_LABEL(line);
8668 LABEL *next_catch_label = NEW_LABEL(line);
8669 LABEL *tmp_label = NULL;
8671 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
8672 push_ensure_entry(iseq, &enl, NULL, NULL);
8674 if (RNODE_WHILE(node)->nd_state == 1) {
8675 ADD_INSNL(ret, line_node, jump, next_label);
8678 tmp_label = NEW_LABEL(line);
8679 ADD_INSNL(ret, line_node, jump, tmp_label);
8681 ADD_LABEL(ret, adjust_label);
8682 ADD_INSN(ret, line_node, putnil);
8683 ADD_LABEL(ret, next_catch_label);
8684 ADD_INSN(ret, line_node, pop);
8685 ADD_INSNL(ret, line_node, jump, next_label);
8686 if (tmp_label) ADD_LABEL(ret, tmp_label);
8688 ADD_LABEL(ret, redo_label);
8689 branches = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
type == NODE_WHILE ?
"while" :
"until");
8691 const NODE *
const coverage_node = RNODE_WHILE(node)->nd_body ? RNODE_WHILE(node)->nd_body : node;
8692 add_trace_branch_coverage(
8695 nd_code_loc(coverage_node),
8696 nd_node_id(coverage_node),
8701 if (!COMPILE_POPPED(ret,
"while body", RNODE_WHILE(node)->nd_body)) {
8702 result = COMPILE_NG;
8705 ADD_LABEL(ret, next_label);
8707 if (
type == NODE_WHILE) {
8708 if (!compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8709 redo_label, end_label)) {
8710 result = COMPILE_NG;
8716 if (!compile_branch_condition(iseq, ret, RNODE_WHILE(node)->nd_cond,
8717 end_label, redo_label)) {
8718 result = COMPILE_NG;
8723 ADD_LABEL(ret, end_label);
8724 ADD_ADJUST_RESTORE(ret, adjust_label);
8726 if (UNDEF_P(RNODE_WHILE(node)->nd_state)) {
8728 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
8729 result = COMPILE_NG;
8733 ADD_INSN(ret, line_node, putnil);
8736 ADD_LABEL(ret, break_label);
8739 ADD_INSN(ret, line_node, pop);
8742 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
8744 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
8746 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
8747 ISEQ_COMPILE_DATA(iseq)->redo_label);
8750 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
8751 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
8752 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
8753 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
8754 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
8759compile_iter(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8761 const int line = nd_line(node);
8762 const NODE *line_node = node;
8763 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8764 LABEL *retry_label = NEW_LABEL(line);
8765 LABEL *retry_end_l = NEW_LABEL(line);
8766 const rb_iseq_t *child_iseq;
8768 ADD_LABEL(ret, retry_label);
8769 if (nd_type_p(node, NODE_FOR)) {
8770 CHECK(COMPILE(ret,
"iter caller (for)", RNODE_FOR(node)->nd_iter));
8772 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8773 NEW_CHILD_ISEQ(RNODE_FOR(node)->nd_body, make_name_for_block(iseq),
8774 ISEQ_TYPE_BLOCK, line);
8775 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
8778 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
8779 NEW_CHILD_ISEQ(RNODE_ITER(node)->nd_body, make_name_for_block(iseq),
8780 ISEQ_TYPE_BLOCK, line);
8781 CHECK(COMPILE(ret,
"iter caller", RNODE_ITER(node)->nd_iter));
8794 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
8795 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
8796 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
8797 iobj = (INSN*) get_prev_insn(iobj);
8799 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
8803 if (&iobj->link == LAST_ELEMENT(ret)) {
8804 ret->last = (LINK_ELEMENT*) retry_end_l;
8809 ADD_INSN(ret, line_node, pop);
8812 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8814 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
8819compile_for_masgn(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8824 const NODE *line_node = node;
8825 const NODE *var = RNODE_FOR_MASGN(node)->nd_var;
8826 LABEL *not_single = NEW_LABEL(nd_line(var));
8827 LABEL *not_ary = NEW_LABEL(nd_line(var));
8828 CHECK(COMPILE(ret,
"for var", var));
8829 ADD_INSN(ret, line_node, dup);
8830 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
8831 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
8832 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
8833 ADD_INSNL(ret, line_node, branchunless, not_single);
8834 ADD_INSN(ret, line_node, dup);
8835 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
8836 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
8837 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
8838 ADD_INSN(ret, line_node, swap);
8839 ADD_CALL(ret, line_node, rb_intern(
"try_convert"),
INT2FIX(1));
8840 ADD_INSN(ret, line_node, dup);
8841 ADD_INSNL(ret, line_node, branchunless, not_ary);
8842 ADD_INSN(ret, line_node, swap);
8843 ADD_LABEL(ret, not_ary);
8844 ADD_INSN(ret, line_node, pop);
8845 ADD_LABEL(ret, not_single);
8850compile_break(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8852 const NODE *line_node = node;
8853 unsigned long throw_flag = 0;
8855 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8857 LABEL *splabel = NEW_LABEL(0);
8858 ADD_LABEL(ret, splabel);
8859 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8860 CHECK(COMPILE_(ret,
"break val (while/until)", RNODE_BREAK(node)->nd_stts,
8861 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
8862 add_ensure_iseq(ret, iseq, 0);
8863 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8864 ADD_ADJUST_RESTORE(ret, splabel);
8867 ADD_INSN(ret, line_node, putnil);
8871 const rb_iseq_t *ip = iseq;
8874 if (!ISEQ_COMPILE_DATA(ip)) {
8879 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8880 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8882 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8885 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8886 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
8890 ip = ISEQ_BODY(ip)->parent_iseq;
8895 CHECK(COMPILE(ret,
"break val (block)", RNODE_BREAK(node)->nd_stts));
8896 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
8898 ADD_INSN(ret, line_node, pop);
8902 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
8909compile_next(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8911 const NODE *line_node = node;
8912 unsigned long throw_flag = 0;
8914 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8915 LABEL *splabel = NEW_LABEL(0);
8916 debugs(
"next in while loop\n");
8917 ADD_LABEL(ret, splabel);
8918 CHECK(COMPILE(ret,
"next val/valid syntax?", RNODE_NEXT(node)->nd_stts));
8919 add_ensure_iseq(ret, iseq, 0);
8920 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8921 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8922 ADD_ADJUST_RESTORE(ret, splabel);
8924 ADD_INSN(ret, line_node, putnil);
8927 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8928 LABEL *splabel = NEW_LABEL(0);
8929 debugs(
"next in block\n");
8930 ADD_LABEL(ret, splabel);
8931 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
8932 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8933 add_ensure_iseq(ret, iseq, 0);
8934 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8935 ADD_ADJUST_RESTORE(ret, splabel);
8938 ADD_INSN(ret, line_node, putnil);
8942 const rb_iseq_t *ip = iseq;
8945 if (!ISEQ_COMPILE_DATA(ip)) {
8950 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8951 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8955 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8958 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8959 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
8963 ip = ISEQ_BODY(ip)->parent_iseq;
8966 CHECK(COMPILE(ret,
"next val", RNODE_NEXT(node)->nd_stts));
8967 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8970 ADD_INSN(ret, line_node, pop);
8974 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
8982compile_redo(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
8984 const NODE *line_node = node;
8986 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8987 LABEL *splabel = NEW_LABEL(0);
8988 debugs(
"redo in while");
8989 ADD_LABEL(ret, splabel);
8990 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
8991 add_ensure_iseq(ret, iseq, 0);
8992 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8993 ADD_ADJUST_RESTORE(ret, splabel);
8995 ADD_INSN(ret, line_node, putnil);
8998 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8999 LABEL *splabel = NEW_LABEL(0);
9001 debugs(
"redo in block");
9002 ADD_LABEL(ret, splabel);
9003 add_ensure_iseq(ret, iseq, 0);
9004 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
9005 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
9006 ADD_ADJUST_RESTORE(ret, splabel);
9009 ADD_INSN(ret, line_node, putnil);
9013 const rb_iseq_t *ip = iseq;
9016 if (!ISEQ_COMPILE_DATA(ip)) {
9021 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
9024 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
9027 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
9028 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
9032 ip = ISEQ_BODY(ip)->parent_iseq;
9035 ADD_INSN(ret, line_node, putnil);
9036 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
9039 ADD_INSN(ret, line_node, pop);
9043 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
9051compile_retry(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9053 const NODE *line_node = node;
9055 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
9056 ADD_INSN(ret, line_node, putnil);
9057 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
9060 ADD_INSN(ret, line_node, pop);
9064 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
9071compile_rescue(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9073 const int line = nd_line(node);
9074 const NODE *line_node = node;
9075 LABEL *lstart = NEW_LABEL(line);
9076 LABEL *lend = NEW_LABEL(line);
9077 LABEL *lcont = NEW_LABEL(line);
9078 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(RNODE_RESCUE(node)->nd_resq,
9080 ISEQ_BODY(iseq)->location.label),
9081 ISEQ_TYPE_RESCUE, line);
9083 lstart->rescued = LABEL_RESCUE_BEG;
9084 lend->rescued = LABEL_RESCUE_END;
9085 ADD_LABEL(ret, lstart);
9087 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
9088 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
9090 CHECK(COMPILE(ret,
"rescue head", RNODE_RESCUE(node)->nd_head));
9092 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
9094 ADD_LABEL(ret, lend);
9095 if (RNODE_RESCUE(node)->nd_else) {
9096 ADD_INSN(ret, line_node, pop);
9097 CHECK(COMPILE(ret,
"rescue else", RNODE_RESCUE(node)->nd_else));
9099 ADD_INSN(ret, line_node, nop);
9100 ADD_LABEL(ret, lcont);
9103 ADD_INSN(ret, line_node, pop);
9107 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
9108 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
9113compile_resbody(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9115 const int line = nd_line(node);
9116 const NODE *line_node = node;
9117 const NODE *resq = node;
9119 LABEL *label_miss, *label_hit;
9122 label_miss = NEW_LABEL(line);
9123 label_hit = NEW_LABEL(line);
9125 narg = RNODE_RESBODY(resq)->nd_args;
9127 switch (nd_type(narg)) {
9130 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9131 CHECK(COMPILE(ret,
"rescue arg", RNODE_LIST(narg)->nd_head));
9132 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
9133 ADD_INSNL(ret, line_node, branchif, label_hit);
9134 narg = RNODE_LIST(narg)->nd_next;
9140 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9141 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
9142 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
9143 ADD_INSNL(ret, line_node, branchif, label_hit);
9146 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
9150 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
9152 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
9153 ADD_INSNL(ret, line_node, branchif, label_hit);
9155 ADD_INSNL(ret, line_node, jump, label_miss);
9156 ADD_LABEL(ret, label_hit);
9159 if (RNODE_RESBODY(resq)->nd_exc_var) {
9160 CHECK(COMPILE_POPPED(ret,
"resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
9163 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) {
9165 ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
9168 CHECK(COMPILE(ret,
"resbody body", RNODE_RESBODY(resq)->nd_body));
9171 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
9172 ADD_INSN(ret, line_node, nop);
9174 ADD_INSN(ret, line_node, leave);
9175 ADD_LABEL(ret, label_miss);
9176 resq = RNODE_RESBODY(resq)->nd_next;
9182compile_ensure(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9184 const int line = nd_line(RNODE_ENSURE(node)->nd_ensr);
9185 const NODE *line_node = node;
9187 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(RNODE_ENSURE(node)->nd_ensr,
9189 ISEQ_TYPE_ENSURE, line);
9190 LABEL *lstart = NEW_LABEL(line);
9191 LABEL *lend = NEW_LABEL(line);
9192 LABEL *lcont = NEW_LABEL(line);
9198 int result = COMPILE_OK;
9201 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", RNODE_ENSURE(node)->nd_ensr));
9203 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
9208 push_ensure_entry(iseq, &enl, &er, RNODE_ENSURE(node)->nd_ensr);
9210 ADD_LABEL(ret, lstart);
9211 if (!COMPILE_(ret,
"ensure head", RNODE_ENSURE(node)->nd_head, (popped | last_leave))) {
9212 result = COMPILE_NG;
9215 ADD_LABEL(ret, lend);
9217 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
9218 ADD_LABEL(ret, lcont);
9219 if (last_leave) ADD_INSN(ret, line_node, pop);
9221 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
9222 if (lstart->link.next != &lend->link) {
9224 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
9226 erange = erange->next;
9231 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
9236compile_return(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9238 const NODE *line_node = node;
9241 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
9242 const rb_iseq_t *is = iseq;
9243 enum rb_iseq_type t =
type;
9244 const NODE *retval = RNODE_RETURN(node)->nd_stts;
9247 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
9248 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
9249 t = ISEQ_BODY(is)->type;
9253 case ISEQ_TYPE_MAIN:
9255 rb_warn(
"argument of top-level return is ignored");
9259 type = ISEQ_TYPE_METHOD;
9266 bool direct_return = can_inline_return(iseq,
type);
9267 if (direct_return) {
9268 splabel = NEW_LABEL(0);
9269 ADD_LABEL(ret, splabel);
9270 ADD_ADJUST(ret, line_node, 0);
9273 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
9275 if (direct_return) {
9276 add_ensure_iseq(ret, iseq, 1);
9278 ADD_INSN(ret, line_node, leave);
9279 ADD_ADJUST_RESTORE(ret, splabel);
9282 ADD_INSN(ret, line_node, putnil);
9286 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
9288 ADD_INSN(ret, line_node, pop);
9296drop_unreachable_return(LINK_ANCHOR *ret)
9298 LINK_ELEMENT *i = ret->last, *last;
9299 if (!i)
return false;
9300 if (IS_TRACE(i)) i = i->prev;
9301 if (!IS_INSN(i) || !IS_INSN_ID(i, putnil))
return false;
9303 if (IS_ADJUST(i)) i = i->prev;
9304 if (!IS_INSN(i))
return false;
9305 switch (INSN_OF(i)) {
9312 (ret->last = last->prev)->next = NULL;
9317compile_evstr(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9319 CHECK(COMPILE_(ret,
"nd_body", node, popped));
9321 if (!popped && !all_string_result_p(node)) {
9322 const NODE *line_node = node;
9323 const unsigned int flag = VM_CALL_FCALL;
9327 ADD_INSN(ret, line_node, dup);
9328 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
9329 ADD_INSN(ret, line_node, anytostring);
9335compile_lvar(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *line_node,
ID id)
9337 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
9339 debugs(
"id: %s idx: %d\n", rb_id2name(
id), idx);
9340 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
9344qcall_branch_start(rb_iseq_t *iseq, LINK_ANCHOR *
const recv,
VALUE *branches,
const NODE *node,
const NODE *line_node)
9346 LABEL *else_label = NEW_LABEL(nd_line(line_node));
9349 br = decl_branch_base(iseq, nd_node_id(node), nd_code_loc(node),
"&.");
9351 ADD_INSN(recv, line_node, dup);
9352 ADD_INSNL(recv, line_node, branchnil, else_label);
9353 add_trace_branch_coverage(iseq, recv, nd_code_loc(node), nd_node_id(node), 0,
"then", br);
9358qcall_branch_end(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, LABEL *else_label,
VALUE branches,
const NODE *node,
const NODE *line_node)
9361 if (!else_label)
return;
9362 end_label = NEW_LABEL(nd_line(line_node));
9363 ADD_INSNL(ret, line_node, jump, end_label);
9364 ADD_LABEL(ret, else_label);
9365 add_trace_branch_coverage(iseq, ret, nd_code_loc(node), nd_node_id(node), 1,
"else", branches);
9366 ADD_LABEL(ret, end_label);
9370compile_call_precheck_freeze(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
const NODE *line_node,
int popped)
9375 if (get_nd_recv(node) &&
9376 nd_type_p(get_nd_recv(node), NODE_STR) &&
9377 (get_node_call_nd_mid(node) == idFreeze || get_node_call_nd_mid(node) == idUMinus) &&
9378 get_nd_args(node) == NULL &&
9379 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9380 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
9381 VALUE str = get_string_value(get_nd_recv(node));
9382 if (get_node_call_nd_mid(node) == idUMinus) {
9383 ADD_INSN2(ret, line_node, opt_str_uminus, str,
9384 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
9387 ADD_INSN2(ret, line_node, opt_str_freeze, str,
9388 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
9392 ADD_INSN(ret, line_node, pop);
9400iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
9402 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
9406iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
9409 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
9410 for (i=0; table[i].index != -1; i++) {
9411 if (strcmp(table[i].name, name) == 0) {
9419iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
9421 const char *name = rb_id2name(mid);
9422 static const char prefix[] =
"__builtin_";
9423 const size_t prefix_len =
sizeof(prefix) - 1;
9428 switch (nd_type(recv)) {
9430 if (RNODE_VCALL(recv)->nd_mid == rb_intern(
"__builtin")) {
9435 if (RNODE_CONST(recv)->nd_vid == rb_intern(
"Primitive")) {
9445 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
9446 return &name[prefix_len];
9455delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
9462 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
9463 unsigned int start=0;
9468 argc + start <= ISEQ_BODY(iseq)->local_table_size;
9470 const LINK_ELEMENT *elem = FIRST_ELEMENT(args);
9472 for (
unsigned int i=start; i-start<argc; i++) {
9473 if (IS_INSN(elem) &&
9474 INSN_OF(elem) == BIN(getlocal)) {
9475 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
9476 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
9478 if (local_level == 0) {
9479 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
9481 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
9482 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
9483 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
9484 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
9508 *pstart_index = start;
9517compile_builtin_attr_symbol(rb_iseq_t *iseq,
VALUE symbol)
9520 if (rb_streql_lit(
string,
"leaf")) {
9521 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
9523 else if (rb_streql_lit(
string,
"inline_block")) {
9524 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
9526 else if (rb_streql_lit(
string,
"use_block")) {
9527 iseq_set_use_block(iseq);
9529 else if (rb_streql_lit(
string,
"c_trace")) {
9531 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
9533 else if (rb_streql_lit(
string,
"without_interrupts")) {
9534 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_WITHOUT_INTERRUPTS;
9536 else if (rb_streql_lit(
string,
"caller_user_box")) {
9537 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_CALLER_USER_BOX;
9539 else if (rb_streql_lit(
string,
"always_inline")) {
9540 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_ALWAYS_INLINE;
9550compile_builtin_attr(rb_iseq_t *iseq,
const NODE *node)
9553 if (!node)
goto no_arg;
9555 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9556 const NODE *next = RNODE_LIST(node)->nd_next;
9558 node = RNODE_LIST(node)->nd_head;
9559 if (!node)
goto no_arg;
9560 switch (nd_type(node)) {
9562 symbol = rb_node_sym_string_val(node);
9568 if (!
SYMBOL_P(symbol))
goto non_symbol_arg;
9569 if (compile_builtin_attr_symbol(iseq, symbol) != COMPILE_OK)
goto unknown_arg;
9574 COMPILE_ERROR(ERROR_ARGS
"attr!: no argument");
9577 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to attr!: %s", rb_builtin_class_name(symbol));
9580 COMPILE_ERROR(ERROR_ARGS
"unknown argument to attr!: %" PRIsVALUE, symbol);
9583 UNKNOWN_NODE(
"attr!", node, COMPILE_NG);
9587compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *node,
const NODE *line_node,
int popped)
9591 if (!node)
goto no_arg;
9592 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
9593 if (RNODE_LIST(node)->nd_next)
goto too_many_arg;
9594 node = RNODE_LIST(node)->nd_head;
9595 if (!node)
goto no_arg;
9596 switch (nd_type(node)) {
9598 name = rb_node_sym_string_val(node);
9603 if (!
SYMBOL_P(name))
goto non_symbol_arg;
9605 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
9609 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
9612 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
9615 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
9616 rb_builtin_class_name(name));
9619 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
9623mandatory_node(
const rb_iseq_t *iseq,
const NODE *cond_node)
9625 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
9626 if (nd_type(node) == NODE_IF && RNODE_IF(node)->nd_cond == cond_node) {
9627 return RNODE_IF(node)->nd_body;
9630 rb_bug(
"mandatory_node: can't find mandatory node");
9635compile_builtin_mandatory_only_method(rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
9639 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
9641 rb_node_args_t args_node;
9642 rb_node_init(RNODE(&args_node), NODE_ARGS);
9643 args_node.nd_ainfo = args;
9646 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
9647 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
9650 rb_ast_id_table_t *tbl =
ALLOCV(idtmp,
sizeof(rb_ast_id_table_t) + table_size *
sizeof(
ID));
9651 tbl->size = table_size;
9656 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
9657 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
9660 for (; i<table_size; i++) {
9661 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
9664 rb_node_scope_t scope_node;
9665 rb_node_init(RNODE(&scope_node), NODE_SCOPE);
9666 scope_node.nd_tbl = tbl;
9667 scope_node.nd_body = mandatory_node(iseq, node);
9668 scope_node.nd_parent = NULL;
9669 scope_node.nd_args = &args_node;
9671 VALUE ast_value = rb_ruby_ast_new(RNODE(&scope_node));
9673 const rb_iseq_t *mandatory_only_iseq =
9674 rb_iseq_new_with_opt(ast_value, rb_iseq_base_label(iseq),
9675 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
9676 nd_line(line_node), NULL, 0,
9677 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option,
9678 ISEQ_SCRIPT_LINES(iseq));
9679 rb_iseq_mandatory_only_iseq_set(iseq, mandatory_only_iseq);
9686compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
const NODE *line_node,
int popped,
9687 const rb_iseq_t *parent_block, LINK_ANCHOR *args,
const char *builtin_func)
9689 NODE *args_node = get_nd_args(node);
9691 if (parent_block != NULL) {
9692 COMPILE_ERROR(ERROR_ARGS_AT(line_node)
"should not call builtins here.");
9696# define BUILTIN_INLINE_PREFIX "_bi"
9697 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
9698 bool cconst =
false;
9703 if (strcmp(
"cstmt!", builtin_func) == 0 ||
9704 strcmp(
"cexpr!", builtin_func) == 0) {
9707 else if (strcmp(
"cconst!", builtin_func) == 0) {
9710 else if (strcmp(
"cinit!", builtin_func) == 0) {
9714 else if (strcmp(
"attr!", builtin_func) == 0) {
9715 return compile_builtin_attr(iseq, args_node);
9717 else if (strcmp(
"arg!", builtin_func) == 0) {
9718 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
9720 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
9722 rb_bug(
"mandatory_only? should be in if condition");
9724 else if (!LIST_INSN_SIZE_ZERO(ret)) {
9725 rb_bug(
"mandatory_only? should be put on top");
9728 ADD_INSN1(ret, line_node, putobject,
Qfalse);
9729 return compile_builtin_mandatory_only_method(iseq, node, line_node);
9731 else if (strcmp(
"local_self!", builtin_func) == 0) {
9736 compile_lvar(iseq, ret, line_node, id_self);
9740 rb_bug(
"can't find builtin function:%s", builtin_func);
9743 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
9747 int inline_index = nd_line(node);
9748 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
9749 builtin_func = inline_func;
9755 typedef VALUE(*builtin_func0)(
void *,
VALUE);
9756 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
9757 ADD_INSN1(ret, line_node, putobject, const_val);
9763 unsigned int flag = 0;
9765 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
9767 if (
FIX2INT(argc) != bf->argc) {
9768 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
9769 builtin_func, bf->argc,
FIX2INT(argc));
9773 unsigned int start_index;
9774 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
9775 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
9779 ADD_INSN1(ret, line_node, invokebuiltin, bf);
9782 if (popped) ADD_INSN(ret, line_node, pop);
9788compile_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)
9796 ID mid = get_node_call_nd_mid(node);
9798 unsigned int flag = 0;
9800 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9801 LABEL *else_label = NULL;
9804 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
9810 if (nd_type_p(node, NODE_VCALL)) {
9815 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
9817 if (mid == id_bitblt) {
9818 ADD_INSN(ret, line_node, bitblt);
9821 else if (mid == id_answer) {
9822 ADD_INSN(ret, line_node, answer);
9834 if (nd_type_p(node, NODE_FCALL) &&
9835 (mid == goto_id || mid == label_id)) {
9838 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
9841 if (!labels_table) {
9842 labels_table = st_init_numtable();
9843 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
9846 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
9850 if (mid == goto_id) {
9851 ADD_INSNL(ret, line_node, jump, label);
9854 ADD_LABEL(ret, label);
9861 const char *builtin_func;
9862 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
9863 (builtin_func = iseq_builtin_function_name(
type, get_nd_recv(node), mid)) != NULL) {
9864 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
9868 if (!assume_receiver) {
9869 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
9872 if ((mid == idCall || mid == idAREF || mid == idYield || mid == idEqq) &&
9873 nd_type_p(get_nd_recv(node), NODE_LVAR) &&
9874 iseq_block_param_id_p(iseq, RNODE_LVAR(get_nd_recv(node))->nd_vid, &idx, &level)) {
9875 ADD_INSN2(recv, get_nd_recv(node), getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
9877 else if (private_recv_p(node)) {
9878 ADD_INSN(recv, node, putself);
9879 flag |= VM_CALL_FCALL;
9882 CHECK(COMPILE(recv,
"recv", get_nd_recv(node)));
9885 if (
type == NODE_QCALL) {
9886 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
9889 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
9890 ADD_CALL_RECEIVER(recv, line_node);
9895 if (
type != NODE_VCALL) {
9896 argc = setup_args(iseq, args, get_nd_args(node), &flag, &keywords);
9897 CHECK(!
NIL_P(argc));
9905 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
9906 mid == rb_intern(
"new") &&
9907 parent_block == NULL &&
9908 !(flag & VM_CALL_ARGS_BLOCKARG);
9911 ADD_INSN(ret, node, putnil);
9912 ADD_INSN(ret, node, swap);
9917 debugp_param(
"call args argc", argc);
9918 debugp_param(
"call method",
ID2SYM(mid));
9920 switch ((
int)
type) {
9922 flag |= VM_CALL_VCALL;
9925 flag |= VM_CALL_FCALL;
9928 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
9929 ADD_INSN(ret, line_node, splatkw);
9932 LABEL *not_basic_new = NEW_LABEL(nd_line(node));
9933 LABEL *not_basic_new_finish = NEW_LABEL(nd_line(node));
9938 if (flag & VM_CALL_FORWARDING) {
9939 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc) + 1, flag, keywords, 0);
9942 ci = (
VALUE)new_callinfo(iseq, mid,
NUM2INT(argc), flag, keywords, 0);
9944 ADD_INSN2(ret, node, opt_new, ci, not_basic_new);
9945 LABEL_REF(not_basic_new);
9948 ADD_SEND_R(ret, line_node, rb_intern(
"initialize"), argc, parent_block,
INT2FIX(flag | VM_CALL_FCALL | VM_CALL_IGNORE_REFINEMENTS), keywords);
9949 ADD_INSNL(ret, line_node, jump, not_basic_new_finish);
9951 ADD_LABEL(ret, not_basic_new);
9953 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9954 ADD_INSN(ret, line_node, swap);
9956 ADD_LABEL(ret, not_basic_new_finish);
9957 ADD_INSN(ret, line_node, pop);
9960 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
9963 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
9965 ADD_INSN(ret, line_node, pop);
9971compile_op_asgn1(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
9973 const int line = nd_line(node);
9975 unsigned int flag = 0;
9977 ID id = RNODE_OP_ASGN1(node)->nd_mid;
10003 ADD_INSN(ret, node, putnil);
10005 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node, RNODE_OP_ASGN1(node)->nd_recv);
10006 CHECK(asgnflag != -1);
10007 switch (nd_type(RNODE_OP_ASGN1(node)->nd_index)) {
10012 argc = setup_args(iseq, ret, RNODE_OP_ASGN1(node)->nd_index, &flag, NULL);
10013 CHECK(!
NIL_P(argc));
10015 int dup_argn =
FIX2INT(argc) + 1;
10016 ADD_INSN1(ret, node, dupn,
INT2FIX(dup_argn));
10018 ADD_SEND_R(ret, node, idAREF, argc, NULL,
INT2FIX(flag & ~VM_CALL_ARGS_SPLAT_MUT), NULL);
10020 if (
id == idOROP ||
id == idANDOP) {
10029 LABEL *label = NEW_LABEL(line);
10030 LABEL *lfin = NEW_LABEL(line);
10032 ADD_INSN(ret, node, dup);
10033 if (
id == idOROP) {
10034 ADD_INSNL(ret, node, branchif, label);
10037 ADD_INSNL(ret, node, branchunless, label);
10039 ADD_INSN(ret, node, pop);
10041 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
10043 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
10045 if (flag & VM_CALL_ARGS_SPLAT) {
10046 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
10047 ADD_INSN(ret, node, swap);
10048 ADD_INSN1(ret, node, splatarray,
Qtrue);
10049 ADD_INSN(ret, node, swap);
10050 flag |= VM_CALL_ARGS_SPLAT_MUT;
10052 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
10053 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
10056 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
10058 ADD_INSN(ret, node, pop);
10059 ADD_INSNL(ret, node, jump, lfin);
10060 ADD_LABEL(ret, label);
10062 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
10064 ADD_INSN1(ret, node, adjuststack,
INT2FIX(dup_argn+1));
10065 ADD_LABEL(ret, lfin);
10068 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 nd_rvalue: ", RNODE_OP_ASGN1(node)->nd_rvalue));
10069 ADD_SEND(ret, node,
id,
INT2FIX(1));
10071 ADD_INSN1(ret, node, setn,
INT2FIX(dup_argn+1));
10073 if (flag & VM_CALL_ARGS_SPLAT) {
10074 if (flag & VM_CALL_KW_SPLAT) {
10075 ADD_INSN1(ret, node, topn,
INT2FIX(2));
10076 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
10077 ADD_INSN1(ret, node, splatarray,
Qtrue);
10078 flag |= VM_CALL_ARGS_SPLAT_MUT;
10080 ADD_INSN(ret, node, swap);
10081 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
10082 ADD_INSN1(ret, node, setn,
INT2FIX(2));
10083 ADD_INSN(ret, node, pop);
10086 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
10087 ADD_INSN(ret, node, swap);
10088 ADD_INSN1(ret, node, splatarray,
Qtrue);
10089 ADD_INSN(ret, node, swap);
10090 flag |= VM_CALL_ARGS_SPLAT_MUT;
10092 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
10094 ADD_SEND_R(ret, node, idASET, argc, NULL,
INT2FIX(flag), NULL);
10097 ADD_SEND_R(ret, node, idASET, FIXNUM_INC(argc, 1), NULL,
INT2FIX(flag), NULL);
10099 ADD_INSN(ret, node, pop);
10105compile_op_asgn2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10107 const int line = nd_line(node);
10108 ID atype = RNODE_OP_ASGN2(node)->nd_mid;
10109 ID vid = RNODE_OP_ASGN2(node)->nd_vid, aid = rb_id_attrset(vid);
10111 LABEL *lfin = NEW_LABEL(line);
10112 LABEL *lcfin = NEW_LABEL(line);
10167 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node, RNODE_OP_ASGN2(node)->nd_recv);
10168 CHECK(asgnflag != -1);
10169 if (RNODE_OP_ASGN2(node)->nd_aid) {
10170 lskip = NEW_LABEL(line);
10171 ADD_INSN(ret, node, dup);
10172 ADD_INSNL(ret, node, branchnil, lskip);
10174 ADD_INSN(ret, node, dup);
10175 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
10177 if (atype == idOROP || atype == idANDOP) {
10179 ADD_INSN(ret, node, dup);
10181 if (atype == idOROP) {
10182 ADD_INSNL(ret, node, branchif, lcfin);
10185 ADD_INSNL(ret, node, branchunless, lcfin);
10188 ADD_INSN(ret, node, pop);
10190 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
10192 ADD_INSN(ret, node, swap);
10193 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10195 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
10196 ADD_INSNL(ret, node, jump, lfin);
10198 ADD_LABEL(ret, lcfin);
10200 ADD_INSN(ret, node, swap);
10203 ADD_LABEL(ret, lfin);
10206 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", RNODE_OP_ASGN2(node)->nd_value));
10207 ADD_SEND(ret, node, atype,
INT2FIX(1));
10209 ADD_INSN(ret, node, swap);
10210 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10212 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
10214 if (lskip && popped) {
10215 ADD_LABEL(ret, lskip);
10217 ADD_INSN(ret, node, pop);
10218 if (lskip && !popped) {
10219 ADD_LABEL(ret, lskip);
10224static int compile_shareable_constant_value(rb_iseq_t *iseq, LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value);
10227compile_op_cdecl(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10229 const int line = nd_line(node);
10231 LABEL *lassign = 0;
10234 switch (nd_type(RNODE_OP_CDECL(node)->nd_head)) {
10236 ADD_INSN1(ret, node, putobject,
rb_cObject);
10239 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", RNODE_COLON2(RNODE_OP_CDECL(node)->nd_head)->nd_head));
10242 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
10243 ruby_node_name(nd_type(RNODE_OP_CDECL(node)->nd_head)));
10246 mid = get_node_colon_nd_mid(RNODE_OP_CDECL(node)->nd_head);
10248 if (RNODE_OP_CDECL(node)->nd_aid == idOROP) {
10249 lassign = NEW_LABEL(line);
10250 ADD_INSN(ret, node, dup);
10251 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
10253 ADD_INSNL(ret, node, branchunless, lassign);
10255 ADD_INSN(ret, node, dup);
10256 ADD_INSN1(ret, node, putobject,
Qtrue);
10257 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
10259 if (RNODE_OP_CDECL(node)->nd_aid == idOROP || RNODE_OP_CDECL(node)->nd_aid == idANDOP) {
10260 lfin = NEW_LABEL(line);
10261 if (!popped) ADD_INSN(ret, node, dup);
10262 if (RNODE_OP_CDECL(node)->nd_aid == idOROP)
10263 ADD_INSNL(ret, node, branchif, lfin);
10265 ADD_INSNL(ret, node, branchunless, lfin);
10267 if (!popped) ADD_INSN(ret, node, pop);
10268 if (lassign) ADD_LABEL(ret, lassign);
10269 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
10272 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10274 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
10275 ADD_INSN(ret, node, swap);
10277 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
10278 ADD_LABEL(ret, lfin);
10279 if (!popped) ADD_INSN(ret, node, swap);
10280 ADD_INSN(ret, node, pop);
10283 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_OP_CDECL(node)->shareability, RNODE_OP_CDECL(node)->nd_head, RNODE_OP_CDECL(node)->nd_value));
10285 ADD_CALL(ret, node, RNODE_OP_CDECL(node)->nd_aid,
INT2FIX(1));
10287 ADD_INSN(ret, node, swap);
10289 ADD_INSN1(ret, node, topn,
INT2FIX(1));
10290 ADD_INSN(ret, node, swap);
10292 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
10298compile_op_log(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
10300 const int line = nd_line(node);
10301 LABEL *lfin = NEW_LABEL(line);
10304 if (
type == NODE_OP_ASGN_OR && !nd_type_p(RNODE_OP_ASGN_OR(node)->nd_head, NODE_IVAR)) {
10308 defined_expr(iseq, ret, RNODE_OP_ASGN_OR(node)->nd_head, lfinish,
Qfalse,
false);
10309 lassign = lfinish[1];
10311 lassign = NEW_LABEL(line);
10313 ADD_INSNL(ret, node, branchunless, lassign);
10316 lassign = NEW_LABEL(line);
10319 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", RNODE_OP_ASGN_OR(node)->nd_head));
10322 ADD_INSN(ret, node, dup);
10325 if (
type == NODE_OP_ASGN_AND) {
10326 ADD_INSNL(ret, node, branchunless, lfin);
10329 ADD_INSNL(ret, node, branchif, lfin);
10333 ADD_INSN(ret, node, pop);
10336 ADD_LABEL(ret, lassign);
10337 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", RNODE_OP_ASGN_OR(node)->nd_value, popped));
10338 ADD_LABEL(ret, lfin);
10343compile_super(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
10348 unsigned int flag = 0;
10350 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
10354 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
10356 if (
type == NODE_SUPER) {
10357 VALUE vargc = setup_args(iseq, args, RNODE_SUPER(node)->nd_args, &flag, &keywords);
10358 CHECK(!
NIL_P(vargc));
10360 if ((flag & VM_CALL_ARGS_BLOCKARG) && (flag & VM_CALL_KW_SPLAT) && !(flag & VM_CALL_KW_SPLAT_MUT)) {
10361 ADD_INSN(args, node, splatkw);
10364 if (flag & VM_CALL_ARGS_BLOCKARG) {
10371 const rb_iseq_t *liseq = body->local_iseq;
10373 const struct rb_iseq_param_keyword *
const local_kwd = local_body->param.keyword;
10374 int lvar_level = get_lvar_level(iseq);
10376 argc = local_body->param.lead_num;
10379 for (i = 0; i < local_body->param.lead_num; i++) {
10380 int idx = local_body->local_table_size - i;
10381 ADD_GETLOCAL(args, node, idx, lvar_level);
10385 if (local_body->param.flags.forwardable) {
10386 flag |= VM_CALL_FORWARDING;
10387 int idx = local_body->local_table_size - get_local_var_idx(liseq, idDot3);
10388 ADD_GETLOCAL(args, node, idx, lvar_level);
10391 if (local_body->param.flags.has_opt) {
10394 for (j = 0; j < local_body->param.opt_num; j++) {
10395 int idx = local_body->local_table_size - (i + j);
10396 ADD_GETLOCAL(args, node, idx, lvar_level);
10401 if (local_body->param.flags.has_rest) {
10403 int idx = local_body->local_table_size - rb_iseq_param_rest_start(local_body);
10404 ADD_GETLOCAL(args, node, idx, lvar_level);
10405 ADD_INSN1(args, node, splatarray, RBOOL(local_body->param.flags.has_post));
10407 argc = rb_iseq_param_rest_start(local_body) + 1;
10408 flag |= VM_CALL_ARGS_SPLAT;
10410 if (local_body->param.flags.has_post) {
10412 int post_len = local_body->param.post_num;
10413 int post_start = rb_iseq_param_post_start(local_body);
10415 if (local_body->param.flags.has_rest) {
10417 for (j=0; j<post_len; j++) {
10418 int idx = local_body->local_table_size - (post_start + j);
10419 ADD_GETLOCAL(args, node, idx, lvar_level);
10421 ADD_INSN1(args, node, pushtoarray,
INT2FIX(j));
10422 flag |= VM_CALL_ARGS_SPLAT_MUT;
10427 for (j=0; j<post_len; j++) {
10428 int idx = local_body->local_table_size - (post_start + j);
10429 ADD_GETLOCAL(args, node, idx, lvar_level);
10431 argc = post_len + post_start;
10435 if (local_body->param.flags.has_kw) {
10436 int local_size = local_body->local_table_size;
10443 if (local_body->param.flags.has_kwrest) {
10444 int idx = local_body->local_table_size - local_kwd->rest_start;
10446 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10447 ADD_GETLOCAL(args, node, idx, lvar_level);
10449 for (i = 0; i < local_kwd->num; ++i) {
10450 ID id = local_kwd->table[i];
10451 int idx = local_size - get_local_var_idx(liseq,
id);
10452 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
10453 ADD_GETLOCAL(args, node, idx, lvar_level);
10455 if (local_body->param.flags.has_kwrest) {
10456 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
10459 ADD_INSN1(args, node, newhash,
INT2FIX(i * 2));
10461 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
10463 else if (local_body->param.flags.has_kwrest) {
10464 int idx = local_body->local_table_size - local_kwd->rest_start;
10465 ADD_GETLOCAL(args, node, idx, lvar_level);
10467 flag |= VM_CALL_KW_SPLAT;
10471 if (use_block && parent_block == NULL) {
10472 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10475 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
10476 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
10477 ADD_INSN(ret, node, putself);
10478 ADD_SEQ(ret, args);
10480 const struct rb_callinfo * ci = new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL);
10482 if (vm_ci_flag(ci) & VM_CALL_FORWARDING) {
10483 ADD_INSN2(ret, node, invokesuperforward, ci, parent_block);
10486 ADD_INSN2(ret, node, invokesuper, ci, parent_block);
10490 ADD_INSN(ret, node, pop);
10496compile_yield(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10500 unsigned int flag = 0;
10505 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
10506 case ISEQ_TYPE_TOP:
10507 case ISEQ_TYPE_MAIN:
10508 case ISEQ_TYPE_CLASS:
10509 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
10514 if (RNODE_YIELD(node)->nd_head) {
10515 argc = setup_args(iseq, args, RNODE_YIELD(node)->nd_head, &flag, &keywords);
10516 CHECK(!
NIL_P(argc));
10522 ADD_SEQ(ret, args);
10523 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
10524 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
10527 ADD_INSN(ret, node, pop);
10531 const rb_iseq_t *tmp_iseq = iseq;
10532 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
10533 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
10535 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
10541compile_match(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const enum node_type
type)
10548 switch ((
int)
type) {
10551 VALUE re = rb_node_regx_string_val(node);
10553 ADD_INSN1(recv, node, putobject, re);
10554 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
10559 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH2(node)->nd_recv));
10560 CHECK(COMPILE(val,
"value", RNODE_MATCH2(node)->nd_value));
10563 CHECK(COMPILE(recv,
"receiver", RNODE_MATCH3(node)->nd_value));
10564 CHECK(COMPILE(val,
"value", RNODE_MATCH3(node)->nd_recv));
10568 ADD_SEQ(ret, recv);
10570 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
10572 if (nd_type_p(node, NODE_MATCH2) && RNODE_MATCH2(node)->nd_args) {
10573 compile_named_capture_assign(iseq, ret, RNODE_MATCH2(node)->nd_args);
10577 ADD_INSN(ret, node, pop);
10583compile_colon2(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10587 rb_const_path_t const_path;
10588 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
10589 (const_path = compile_const_path(iseq, node))) {
10590 ISEQ_BODY(iseq)->ic_size++;
10591 ADD_INSN1(ret, node, opt_getconstant_path, (
VALUE)const_path);
10600 CHECK(compile_const_prefix(iseq, node, pref, body));
10601 if (LIST_INSN_SIZE_ZERO(pref)) {
10602 ADD_INSN(ret, node, putnil);
10603 ADD_SEQ(ret, body);
10606 ADD_SEQ(ret, pref);
10607 ADD_SEQ(ret, body);
10613 ADD_CALL_RECEIVER(ret, node);
10614 CHECK(COMPILE(ret,
"colon2#nd_head", RNODE_COLON2(node)->nd_head));
10615 ADD_CALL(ret, node, RNODE_COLON2(node)->nd_mid,
INT2FIX(1));
10618 ADD_INSN(ret, node, pop);
10624compile_colon3(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10626 debugi(
"colon3#nd_mid", RNODE_COLON3(node)->nd_mid);
10629 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
10630 ISEQ_BODY(iseq)->ic_size++;
10631 rb_const_path_t const_path = rb_const_path_from_ids(
true, &RNODE_COLON3(node)->nd_mid, 1);
10632 ADD_INSN1(ret, node, opt_getconstant_path, (
VALUE)const_path);
10635 ADD_INSN1(ret, node, putobject,
rb_cObject);
10636 ADD_INSN1(ret, node, putobject,
Qtrue);
10637 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_COLON3(node)->nd_mid));
10641 ADD_INSN(ret, node, pop);
10647compile_dots(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped,
const int excl)
10650 const NODE *b = RNODE_DOT2(node)->nd_beg;
10651 const NODE *e = RNODE_DOT2(node)->nd_end;
10653 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
10655 VALUE bv = optimized_range_item(b);
10656 VALUE ev = optimized_range_item(e);
10659 ADD_INSN1(ret, node, putobject, val);
10664 CHECK(COMPILE_(ret,
"min", b, popped));
10665 CHECK(COMPILE_(ret,
"max", e, popped));
10667 ADD_INSN1(ret, node, newrange, flag);
10674compile_errinfo(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10677 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10678 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
10681 const rb_iseq_t *ip = iseq;
10684 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
10687 ip = ISEQ_BODY(ip)->parent_iseq;
10691 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
10694 ADD_INSN(ret, node, putnil);
10702compile_kw_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10705 LABEL *end_label = NEW_LABEL(nd_line(node));
10706 const NODE *default_value = get_nd_value(RNODE_KW_ARG(node)->nd_body);
10708 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
10710 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10713 else if (nd_type_p(default_value, NODE_SYM) ||
10714 nd_type_p(default_value, NODE_REGX) ||
10715 nd_type_p(default_value, NODE_LINE) ||
10716 nd_type_p(default_value, NODE_INTEGER) ||
10717 nd_type_p(default_value, NODE_FLOAT) ||
10718 nd_type_p(default_value, NODE_RATIONAL) ||
10719 nd_type_p(default_value, NODE_IMAGINARY) ||
10720 nd_type_p(default_value, NODE_NIL) ||
10721 nd_type_p(default_value, NODE_TRUE) ||
10722 nd_type_p(default_value, NODE_FALSE)) {
10723 COMPILE_ERROR(ERROR_ARGS
"unreachable");
10731 int kw_bits_idx = body->local_table_size - body->param.keyword->bits_start;
10732 int keyword_idx = body->param.keyword->num - body->param.keyword->required_num;
10733 for (
const rb_node_kw_arg_t *kwarg = RNODE_KW_ARG(node); kwarg; kwarg = kwarg->nd_next) {
10734 if (get_nd_value(kwarg->nd_body) != NODE_SPECIAL_REQUIRED_KEYWORD) {
10739 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
10740 ADD_INSNL(ret, node, branchif, end_label);
10741 CHECK(COMPILE_POPPED(ret,
"keyword default argument", RNODE_KW_ARG(node)->nd_body));
10742 ADD_LABEL(ret, end_label);
10748compile_attrasgn(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
10752 unsigned int flag = 0;
10753 ID mid = RNODE_ATTRASGN(node)->nd_mid;
10755 LABEL *else_label = NULL;
10760 argc = setup_args(iseq, args, RNODE_ATTRASGN(node)->nd_args, &flag, NULL);
10761 CHECK(!
NIL_P(argc));
10763 int asgnflag = COMPILE_RECV(recv,
"recv", node, RNODE_ATTRASGN(node)->nd_recv);
10764 CHECK(asgnflag != -1);
10765 flag |= (
unsigned int)asgnflag;
10767 debugp_param(
"argc", argc);
10768 debugp_param(
"nd_mid",
ID2SYM(mid));
10772 mid = rb_id_attrset(mid);
10773 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
10776 ADD_INSN(ret, node, putnil);
10777 ADD_SEQ(ret, recv);
10778 ADD_SEQ(ret, args);
10780 if (flag & VM_CALL_ARGS_SPLAT) {
10781 ADD_INSN(ret, node, dup);
10782 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
10783 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
10784 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
10785 ADD_INSN (ret, node, pop);
10788 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
10792 ADD_SEQ(ret, recv);
10793 ADD_SEQ(ret, args);
10795 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
10796 qcall_branch_end(iseq, ret, else_label, branches, node, node);
10797 ADD_INSN(ret, node, pop);
10802compile_make_shareable_node(rb_iseq_t *iseq, LINK_ANCHOR *ret, LINK_ANCHOR *sub,
const NODE *value,
bool copy)
10804 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10812 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10819 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10826node_const_decl_val(
const NODE *node)
10829 switch (nd_type(node)) {
10831 if (RNODE_CDECL(node)->nd_vid) {
10832 path = rb_id2str(RNODE_CDECL(node)->nd_vid);
10836 node = RNODE_CDECL(node)->nd_else;
10844 rb_str_append(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10847 rb_bug(
"unexpected node: %s", ruby_node_name(nd_type(node)));
10853 for (; node && nd_type_p(node, NODE_COLON2); node = RNODE_COLON2(node)->nd_head) {
10854 rb_ary_push(path, rb_id2str(RNODE_COLON2(node)->nd_mid));
10856 if (node && nd_type_p(node, NODE_CONST)) {
10858 rb_ary_push(path, rb_id2str(RNODE_CONST(node)->nd_vid));
10860 else if (node && nd_type_p(node, NODE_COLON3)) {
10862 rb_ary_push(path, rb_id2str(RNODE_COLON3(node)->nd_mid));
10872 path = rb_fstring(path);
10877const_decl_path(NODE *dest)
10880 if (!nd_type_p(dest, NODE_CALL)) {
10881 path = node_const_decl_val(dest);
10887compile_ensure_shareable_node(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE *dest,
const NODE *value)
10892 VALUE path = const_decl_path(dest);
10893 ADD_INSN1(ret, value, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10894 CHECK(COMPILE(ret,
"compile_ensure_shareable_node", value));
10895 ADD_INSN1(ret, value, putobject, path);
10897 ADD_SEND_WITH_FLAG(ret, value, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10902#ifndef SHAREABLE_BARE_EXPRESSION
10903#define SHAREABLE_BARE_EXPRESSION 1
10907compile_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)
10909# define compile_shareable_literal_constant_next(node, anchor, value_p, shareable_literal_p) \
10910 compile_shareable_literal_constant(iseq, anchor, shareable, dest, node, level+1, value_p, shareable_literal_p)
10912 DECL_ANCHOR(anchor);
10914 enum node_type
type = node ? nd_type(node) : NODE_NIL;
10926 *value_p = rb_node_sym_string_val(node);
10929 *value_p = rb_node_regx_string_val(node);
10932 *value_p = rb_node_line_lineno_val(node);
10935 *value_p = rb_node_integer_literal_val(node);
10938 *value_p = rb_node_float_literal_val(node);
10940 case NODE_RATIONAL:
10941 *value_p = rb_node_rational_literal_val(node);
10943 case NODE_IMAGINARY:
10944 *value_p = rb_node_imaginary_literal_val(node);
10946 case NODE_ENCODING:
10947 *value_p = rb_node_encoding_val(node);
10950 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10951 *shareable_literal_p = 1;
10955 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
10956 if (shareable == rb_parser_shareable_literal) {
10962 ADD_SEND_WITH_FLAG(ret, node, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
10965 *shareable_literal_p = 1;
10969 VALUE lit = rb_node_str_string_val(node);
10970 ADD_INSN1(ret, node, putobject, lit);
10973 *shareable_literal_p = 1;
10979 compile_file_path(iseq, ret, node, ISEQ_FROZEN_STRING_LITERAL_ENABLED);
10981 *shareable_literal_p = 1;
10987 ADD_INSN1(ret, node, putobject, lit);
10990 *shareable_literal_p = 1;
10996 INIT_ANCHOR(anchor);
10998 for (NODE *n = (NODE *)node; n; n = RNODE_LIST(n)->nd_next) {
11000 int shareable_literal_p2;
11001 NODE *elt = RNODE_LIST(n)->nd_head;
11003 CHECK(compile_shareable_literal_constant_next(elt, anchor, &val, &shareable_literal_p2));
11004 if (shareable_literal_p2) {
11007 else if (
RTEST(lit)) {
11013 if (!UNDEF_P(val)) {
11025 if (!RNODE_HASH(node)->nd_brace)
goto compile_shareable;
11026 for (NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
11027 if (!RNODE_LIST(n)->nd_head) {
11029 goto compile_shareable;
11033 INIT_ANCHOR(anchor);
11035 for (NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
11037 VALUE value_val = 0;
11038 int shareable_literal_p2;
11039 NODE *key = RNODE_LIST(n)->nd_head;
11040 NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
11041 CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
11042 if (shareable_literal_p2) {
11045 else if (
RTEST(lit)) {
11046 rb_hash_clear(lit);
11049 CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
11050 if (shareable_literal_p2) {
11053 else if (
RTEST(lit)) {
11054 rb_hash_clear(lit);
11058 if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
11059 rb_hash_aset(lit, key_val, value_val);
11062 rb_hash_clear(lit);
11073 if (shareable == rb_parser_shareable_literal &&
11074 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11075 CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
11077 *shareable_literal_p = 1;
11080 CHECK(COMPILE(ret,
"shareable_literal_constant", node));
11082 *shareable_literal_p = 0;
11088 if (nd_type(node) == NODE_LIST) {
11089 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
11091 else if (nd_type(node) == NODE_HASH) {
11092 long len = RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
11098 *shareable_literal_p = 0;
11099 ADD_SEQ(ret, anchor);
11105 if (nd_type(node) == NODE_LIST) {
11106 ADD_INSN1(anchor, node, newarray,
INT2FIX(RNODE_LIST(node)->as.nd_alen));
11108 else if (nd_type(node) == NODE_HASH) {
11109 long len = RNODE_LIST(RNODE_HASH(node)->nd_head)->as.nd_alen;
11114 CHECK(compile_make_shareable_node(iseq, ret, anchor, node,
false));
11116 *shareable_literal_p = 1;
11120 ADD_INSN1(ret, node, putobject, val);
11123 *shareable_literal_p = 1;
11130compile_shareable_constant_value(rb_iseq_t *iseq, LINK_ANCHOR *ret,
enum rb_parser_shareability shareable,
const NODE *lhs,
const NODE *value)
11134 DECL_ANCHOR(anchor);
11135 INIT_ANCHOR(anchor);
11137 switch (shareable) {
11138 case rb_parser_shareable_none:
11139 CHECK(COMPILE(ret,
"compile_shareable_constant_value", value));
11142 case rb_parser_shareable_literal:
11143 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (NODE *)lhs, value, 0, &val, &literal_p));
11144 ADD_SEQ(ret, anchor);
11147 case rb_parser_shareable_copy:
11148 case rb_parser_shareable_everything:
11149 CHECK(compile_shareable_literal_constant(iseq, anchor, shareable, (NODE *)lhs, value, 0, &val, &literal_p));
11151 CHECK(compile_make_shareable_node(iseq, ret, anchor, value, shareable == rb_parser_shareable_copy));
11154 ADD_SEQ(ret, anchor);
11158 rb_bug(
"unexpected rb_parser_shareability: %d", shareable);
11162static int iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped);
11171iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret,
const NODE *node,
int popped)
11175 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
11176 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
11177 debugs(
"node: NODE_NIL(implicit)\n");
11178 ADD_SYNTHETIC_INSN(ret, lineno, -1, putnil);
11182 return iseq_compile_each0(iseq, ret, node, popped);
11186iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const NODE *
const node,
int popped)
11188 const int line = (int)nd_line(node);
11189 const enum node_type
type = nd_type(node);
11192 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
11196 if (nd_fl_newline(node)) {
11198 ISEQ_COMPILE_DATA(iseq)->last_line = line;
11199 if (line > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
11200 event |= RUBY_EVENT_COVERAGE_LINE;
11202 ADD_TRACE(ret, event);
11206 debug_node_start(node);
11207#undef BEFORE_RETURN
11208#define BEFORE_RETURN debug_node_end()
11212 CHECK(compile_block(iseq, ret, node, popped));
11216 CHECK(compile_if(iseq, ret, node, popped,
type));
11219 CHECK(compile_case(iseq, ret, node, popped));
11222 CHECK(compile_case2(iseq, ret, node, popped));
11225 CHECK(compile_case3(iseq, ret, node, popped));
11229 CHECK(compile_loop(iseq, ret, node, popped,
type));
11233 CHECK(compile_iter(iseq, ret, node, popped));
11235 case NODE_FOR_MASGN:
11236 CHECK(compile_for_masgn(iseq, ret, node, popped));
11239 CHECK(compile_break(iseq, ret, node, popped));
11242 CHECK(compile_next(iseq, ret, node, popped));
11245 CHECK(compile_redo(iseq, ret, node, popped));
11248 CHECK(compile_retry(iseq, ret, node, popped));
11251 CHECK(COMPILE_(ret,
"NODE_BEGIN", RNODE_BEGIN(node)->nd_body, popped));
11255 CHECK(compile_rescue(iseq, ret, node, popped));
11258 CHECK(compile_resbody(iseq, ret, node, popped));
11261 CHECK(compile_ensure(iseq, ret, node, popped));
11266 LABEL *end_label = NEW_LABEL(line);
11267 const NODE *cond = node;
11274 CHECK(COMPILE(ret,
"nd_1st", RNODE_OR(cond)->nd_1st));
11276 ADD_INSN(ret, cond, dup);
11278 if (
type == NODE_AND) {
11279 ADD_INSNL(ret, cond, branchunless, end_label);
11282 ADD_INSNL(ret, cond, branchif, end_label);
11285 ADD_INSN(ret, cond, pop);
11287 const NODE *rest = RNODE_OR(cond)->nd_2nd;
11288 if (nd_type_p(rest,
type)) {
11292 CHECK(COMPILE_(ret,
"nd_2nd", rest, popped));
11295 ADD_LABEL(ret, end_label);
11300 compile_massign(iseq, ret, node, popped);
11305 ID id = RNODE_LASGN(node)->nd_vid;
11306 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
11308 debugs(
"lvar: %s idx: %d\n", rb_id2name(
id), idx);
11309 CHECK(COMPILE(ret,
"rvalue", RNODE_LASGN(node)->nd_value));
11312 ADD_INSN(ret, node, dup);
11314 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
11319 ID id = RNODE_DASGN(node)->nd_vid;
11320 CHECK(COMPILE(ret,
"dvalue", RNODE_DASGN(node)->nd_value));
11321 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
11324 ADD_INSN(ret, node, dup);
11327 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
11330 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
11334 ADD_SETLOCAL(ret, node, ls - idx, lv);
11338 CHECK(COMPILE(ret,
"lvalue", RNODE_GASGN(node)->nd_value));
11341 ADD_INSN(ret, node, dup);
11343 ADD_INSN1(ret, node, setglobal,
ID2SYM(RNODE_GASGN(node)->nd_vid));
11347 CHECK(COMPILE(ret,
"lvalue", RNODE_IASGN(node)->nd_value));
11349 ADD_INSN(ret, node, dup);
11351 ADD_INSN2(ret, node, setinstancevariable,
11352 ID2SYM(RNODE_IASGN(node)->nd_vid),
11353 get_ivar_ic_value(iseq,RNODE_IASGN(node)->nd_vid));
11357 if (RNODE_CDECL(node)->nd_vid) {
11358 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11361 ADD_INSN(ret, node, dup);
11364 ADD_INSN1(ret, node, putspecialobject,
11365 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
11366 ADD_INSN1(ret, node, setconstant,
ID2SYM(RNODE_CDECL(node)->nd_vid));
11369 compile_cpath(ret, iseq, RNODE_CDECL(node)->nd_else);
11370 CHECK(compile_shareable_constant_value(iseq, ret, RNODE_CDECL(node)->shareability, node, RNODE_CDECL(node)->nd_value));
11371 ADD_INSN(ret, node, swap);
11374 ADD_INSN1(ret, node, topn,
INT2FIX(1));
11375 ADD_INSN(ret, node, swap);
11378 ADD_INSN1(ret, node, setconstant,
ID2SYM(get_node_colon_nd_mid(RNODE_CDECL(node)->nd_else)));
11383 CHECK(COMPILE(ret,
"cvasgn val", RNODE_CVASGN(node)->nd_value));
11385 ADD_INSN(ret, node, dup);
11387 ADD_INSN2(ret, node, setclassvariable,
11388 ID2SYM(RNODE_CVASGN(node)->nd_vid),
11389 get_cvar_ic_value(iseq, RNODE_CVASGN(node)->nd_vid));
11392 case NODE_OP_ASGN1:
11393 CHECK(compile_op_asgn1(iseq, ret, node, popped));
11395 case NODE_OP_ASGN2:
11396 CHECK(compile_op_asgn2(iseq, ret, node, popped));
11398 case NODE_OP_CDECL:
11399 CHECK(compile_op_cdecl(iseq, ret, node, popped));
11401 case NODE_OP_ASGN_AND:
11402 case NODE_OP_ASGN_OR:
11403 CHECK(compile_op_log(iseq, ret, node, popped,
type));
11407 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
11413 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
11419 CHECK(compile_super(iseq, ret, node, popped,
type));
11422 CHECK(compile_array(iseq, ret, node, popped, TRUE) >= 0);
11427 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
11432 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
11435 CHECK(compile_return(iseq, ret, node, popped));
11438 CHECK(compile_yield(iseq, ret, node, popped));
11442 compile_lvar(iseq, ret, node, RNODE_LVAR(node)->nd_vid);
11448 debugi(
"nd_vid", RNODE_DVAR(node)->nd_vid);
11450 idx = get_dyna_var_idx(iseq, RNODE_DVAR(node)->nd_vid, &lv, &ls);
11452 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
11453 rb_id2str(RNODE_DVAR(node)->nd_vid));
11456 ADD_GETLOCAL(ret, node, ls - idx, lv);
11461 ADD_INSN1(ret, node, getglobal,
ID2SYM(RNODE_GVAR(node)->nd_vid));
11463 ADD_INSN(ret, node, pop);
11468 debugi(
"nd_vid", RNODE_IVAR(node)->nd_vid);
11470 ADD_INSN2(ret, node, getinstancevariable,
11471 ID2SYM(RNODE_IVAR(node)->nd_vid),
11472 get_ivar_ic_value(iseq, RNODE_IVAR(node)->nd_vid));
11477 debugi(
"nd_vid", RNODE_CONST(node)->nd_vid);
11479 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
11481 rb_const_path_t const_path = rb_const_path_from_ids(
false, &RNODE_CONST(node)->nd_vid, 1);
11482 ADD_INSN1(ret, node, opt_getconstant_path, (
VALUE)const_path);
11485 ADD_INSN(ret, node, putnil);
11486 ADD_INSN1(ret, node, putobject,
Qtrue);
11487 ADD_INSN1(ret, node, getconstant,
ID2SYM(RNODE_CONST(node)->nd_vid));
11491 ADD_INSN(ret, node, pop);
11497 ADD_INSN2(ret, node, getclassvariable,
11498 ID2SYM(RNODE_CVAR(node)->nd_vid),
11499 get_cvar_ic_value(iseq, RNODE_CVAR(node)->nd_vid));
11503 case NODE_NTH_REF:{
11505 if (!RNODE_NTH_REF(node)->nd_nth) {
11506 ADD_INSN(ret, node, putnil);
11509 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11510 INT2FIX(RNODE_NTH_REF(node)->nd_nth << 1));
11514 case NODE_BACK_REF:{
11516 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
11517 INT2FIX(0x01 | (RNODE_BACK_REF(node)->nd_nth << 1)));
11524 CHECK(compile_match(iseq, ret, node, popped,
type));
11528 ADD_INSN1(ret, node, putobject, rb_node_sym_string_val(node));
11534 ADD_INSN1(ret, node, putobject, rb_node_line_lineno_val(node));
11538 case NODE_ENCODING:{
11540 ADD_INSN1(ret, node, putobject, rb_node_encoding_val(node));
11544 case NODE_INTEGER:{
11545 VALUE lit = rb_node_integer_literal_val(node);
11547 debugp_param(
"integer", lit);
11549 ADD_INSN1(ret, node, putobject, lit);
11555 VALUE lit = rb_node_float_literal_val(node);
11557 debugp_param(
"float", lit);
11559 ADD_INSN1(ret, node, putobject, lit);
11564 case NODE_RATIONAL:{
11565 VALUE lit = rb_node_rational_literal_val(node);
11567 debugp_param(
"rational", lit);
11569 ADD_INSN1(ret, node, putobject, lit);
11574 case NODE_IMAGINARY:{
11575 VALUE lit = rb_node_imaginary_literal_val(node);
11577 debugp_param(
"imaginary", lit);
11579 ADD_INSN1(ret, node, putobject, lit);
11586 compile_file_path(iseq, ret, node, ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal);
11590 debugp_param(
"nd_lit", get_string_value(node));
11592 VALUE lit = get_string_value(node);
11593 const rb_compile_option_t *option = ISEQ_COMPILE_DATA(iseq)->option;
11595 option->frozen_string_literal != ISEQ_FROZEN_STRING_LITERAL_DISABLED) {
11596 lit = rb_str_with_debug_created_info(lit, rb_iseq_path(iseq), line);
11599 switch (option->frozen_string_literal) {
11600 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11601 ADD_INSN1(ret, node, dupchilledstring, lit);
11603 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11604 ADD_INSN1(ret, node, dupstring, lit);
11606 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11607 ADD_INSN1(ret, node, putobject, lit);
11610 rb_bug(
"invalid frozen_string_literal");
11617 compile_dstr(iseq, ret, node);
11620 ADD_INSN(ret, node, pop);
11625 ADD_CALL_RECEIVER(ret, node);
11626 VALUE str = rb_node_str_string_val(node);
11627 ADD_INSN1(ret, node, putobject, str);
11629 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11632 ADD_INSN(ret, node, pop);
11637 ADD_CALL_RECEIVER(ret, node);
11638 compile_dstr(iseq, ret, node);
11639 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
11642 ADD_INSN(ret, node, pop);
11647 CHECK(compile_evstr(iseq, ret, RNODE_EVSTR(node)->nd_body, popped));
11651 VALUE lit = rb_node_regx_string_val(node);
11653 ADD_INSN1(ret, node, putobject, lit);
11659 compile_dregx(iseq, ret, node, popped);
11662 const rb_iseq_t *block_iseq;
11663 block_iseq = NEW_CHILD_ISEQ(RNODE_ONCE(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
11665 VALUE once_storage = rb_imemo_once_storage_new();
11666 ADD_INSN2(ret, node, once, block_iseq, once_storage);
11671 ADD_INSN(ret, node, pop);
11675 case NODE_ARGSCAT:{
11677 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11678 ADD_INSN1(ret, node, splatarray,
Qfalse);
11679 ADD_INSN(ret, node, pop);
11680 CHECK(COMPILE(ret,
"argscat body", RNODE_ARGSCAT(node)->nd_body));
11681 ADD_INSN1(ret, node, splatarray,
Qfalse);
11682 ADD_INSN(ret, node, pop);
11685 CHECK(COMPILE(ret,
"argscat head", RNODE_ARGSCAT(node)->nd_head));
11686 const NODE *body_node = RNODE_ARGSCAT(node)->nd_body;
11687 if (nd_type_p(body_node, NODE_LIST)) {
11688 CHECK(compile_array(iseq, ret, body_node, popped, FALSE) >= 0);
11691 CHECK(COMPILE(ret,
"argscat body", body_node));
11692 ADD_INSN(ret, node, concattoarray);
11697 case NODE_ARGSPUSH:{
11699 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11700 ADD_INSN1(ret, node, splatarray,
Qfalse);
11701 ADD_INSN(ret, node, pop);
11702 CHECK(COMPILE_(ret,
"argspush body", RNODE_ARGSPUSH(node)->nd_body, popped));
11705 CHECK(COMPILE(ret,
"argspush head", RNODE_ARGSPUSH(node)->nd_head));
11706 const NODE *body_node = RNODE_ARGSPUSH(node)->nd_body;
11707 if (keyword_node_p(body_node)) {
11708 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11709 ADD_INSN(ret, node, pushtoarraykwsplat);
11711 else if (static_literal_node_p(body_node, iseq,
false)) {
11712 ADD_INSN1(ret, body_node, putobject, static_literal_value(body_node, iseq));
11713 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11716 CHECK(COMPILE_(ret,
"array element", body_node, FALSE));
11717 ADD_INSN1(ret, node, pushtoarray,
INT2FIX(1));
11723 CHECK(COMPILE(ret,
"splat", RNODE_SPLAT(node)->nd_head));
11724 ADD_INSN1(ret, node, splatarray,
Qtrue);
11727 ADD_INSN(ret, node, pop);
11732 ID mid = RNODE_DEFN(node)->nd_mid;
11733 const rb_iseq_t *method_iseq = NEW_ISEQ(RNODE_DEFN(node)->nd_defn,
11735 ISEQ_TYPE_METHOD, line);
11737 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
11738 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
11742 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11748 ID mid = RNODE_DEFS(node)->nd_mid;
11749 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(RNODE_DEFS(node)->nd_defn,
11751 ISEQ_TYPE_METHOD, line);
11753 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
11754 CHECK(COMPILE(ret,
"defs: recv", RNODE_DEFS(node)->nd_recv));
11755 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
11759 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
11764 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11765 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11766 CHECK(COMPILE(ret,
"alias arg1", RNODE_ALIAS(node)->nd_1st));
11767 CHECK(COMPILE(ret,
"alias arg2", RNODE_ALIAS(node)->nd_2nd));
11768 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
11771 ADD_INSN(ret, node, pop);
11776 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11777 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_alias));
11778 ADD_INSN1(ret, node, putobject,
ID2SYM(RNODE_VALIAS(node)->nd_orig));
11779 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
11782 ADD_INSN(ret, node, pop);
11787 const rb_parser_ary_t *ary = RNODE_UNDEF(node)->nd_undefs;
11789 for (
long i = 0; i < ary->len; i++) {
11790 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11791 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
11792 CHECK(COMPILE(ret,
"undef arg", ary->data[i]));
11793 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
11795 if (i < ary->
len - 1) {
11796 ADD_INSN(ret, node, pop);
11801 ADD_INSN(ret, node, pop);
11806 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(RNODE_CLASS(node)->nd_body,
11807 rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)))),
11808 ISEQ_TYPE_CLASS, line);
11809 const int flags = VM_DEFINECLASS_TYPE_CLASS |
11810 (RNODE_CLASS(node)->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
11811 compile_cpath(ret, iseq, RNODE_CLASS(node)->nd_cpath);
11813 CHECK(COMPILE(ret,
"super", RNODE_CLASS(node)->nd_super));
11814 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_CLASS(node)->nd_cpath)), class_iseq,
INT2FIX(flags));
11818 ADD_INSN(ret, node, pop);
11823 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(RNODE_MODULE(node)->nd_body,
11824 rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)))),
11825 ISEQ_TYPE_CLASS, line);
11826 const int flags = VM_DEFINECLASS_TYPE_MODULE |
11827 compile_cpath(ret, iseq, RNODE_MODULE(node)->nd_cpath);
11829 ADD_INSN (ret, node, putnil);
11830 ADD_INSN3(ret, node, defineclass,
ID2SYM(get_node_colon_nd_mid(RNODE_MODULE(node)->nd_cpath)), module_iseq,
INT2FIX(flags));
11834 ADD_INSN(ret, node, pop);
11840 const rb_iseq_t *singleton_class = NEW_ISEQ(RNODE_SCLASS(node)->nd_body, rb_fstring_lit(
"singleton class"),
11841 ISEQ_TYPE_CLASS, line);
11843 CHECK(COMPILE(ret,
"sclass#recv", RNODE_SCLASS(node)->nd_recv));
11844 ADD_INSN (ret, node, putnil);
11845 CONST_ID(singletonclass,
"singletonclass");
11849 int sclass_flags = VM_DEFINECLASS_TYPE_SINGLETON_CLASS;
11850 const NODE *recv = RNODE_SCLASS(node)->nd_recv;
11851 if (!(nd_type_p(recv, NODE_SELF) &&
11852 ISEQ_BODY(iseq)->
type == ISEQ_TYPE_CLASS) &&
11853 !cpath_const_p(recv)) {
11854 sclass_flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
11857 ADD_INSN3(ret, node, defineclass,
11858 ID2SYM(singletonclass), singleton_class,
11863 ADD_INSN(ret, node, pop);
11868 CHECK(compile_colon2(iseq, ret, node, popped));
11871 CHECK(compile_colon3(iseq, ret, node, popped));
11874 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
11877 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
11881 LABEL *lend = NEW_LABEL(line);
11882 LABEL *ltrue = NEW_LABEL(line);
11883 LABEL *lfalse = NEW_LABEL(line);
11884 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
11886 ADD_LABEL(ret, ltrue);
11887 ADD_INSN1(ret, node, putobject,
Qtrue);
11888 ADD_INSNL(ret, node, jump, lend);
11889 ADD_LABEL(ret, lfalse);
11890 ADD_INSN1(ret, node, putobject,
Qfalse);
11891 ADD_LABEL(ret, lend);
11896 ADD_INSN(ret, node, putself);
11902 ADD_INSN(ret, node, putnil);
11908 ADD_INSN1(ret, node, putobject,
Qtrue);
11914 ADD_INSN1(ret, node, putobject,
Qfalse);
11919 CHECK(compile_errinfo(iseq, ret, node, popped));
11923 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue,
false));
11926 case NODE_POSTEXE:{
11931 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, RNODE_POSTEXE(node)->nd_body);
11932 const rb_iseq_t *once_iseq =
11933 NEW_CHILD_ISEQ_WITH_CALLBACK(ifunc, rb_fstring(make_name_for_block(iseq)), ISEQ_TYPE_BLOCK, line);
11935 VALUE once_storage = rb_imemo_once_storage_new();
11936 ADD_INSN2(ret, node, once, once_iseq, once_storage);
11941 ADD_INSN(ret, node, pop);
11946 CHECK(compile_kw_arg(iseq, ret, node, popped));
11949 compile_dstr(iseq, ret, node);
11951 ADD_INSN(ret, node, intern);
11954 ADD_INSN(ret, node, pop);
11958 case NODE_ATTRASGN:
11959 CHECK(compile_attrasgn(iseq, ret, node, popped));
11963 const rb_iseq_t *block = NEW_CHILD_ISEQ(RNODE_LAMBDA(node)->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
11966 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
11967 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
11971 ADD_INSN(ret, node, pop);
11976 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
11991insn_data_length(INSN *iobj)
11993 return insn_len(iobj->insn_id);
11997calc_sp_depth(
int depth, INSN *insn)
11999 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
12003opobj_inspect(
VALUE obj)
12023insn_data_to_s_detail(INSN *iobj)
12025 VALUE str = rb_sprintf(
"%-20s ", insn_name(iobj->insn_id));
12027 if (iobj->operands) {
12028 const char *types = insn_op_types(iobj->insn_id);
12031 for (j = 0; types[j]; j++) {
12032 char type = types[j];
12037 LABEL *lobj = (LABEL *)OPERAND_AT(iobj, j);
12038 rb_str_catf(str, LABEL_FORMAT, lobj->label_no);
12044 rb_iseq_t *iseq = (rb_iseq_t *)OPERAND_AT(iobj, j);
12056 VALUE v = OPERAND_AT(iobj, j);
12068 rb_str_concat(str, opobj_inspect(rb_const_path_to_array((rb_const_path_t)OPERAND_AT(iobj, j))));
12071 rb_str_catf(str,
"<ivc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
12074 rb_str_catf(str,
"<icvarc:%d>",
FIX2INT(OPERAND_AT(iobj, j)));
12080 if (vm_ci_mid(ci))
rb_str_append(str, rb_id2str(vm_ci_mid(ci)));
12081 rb_str_catf(str,
", %d>", vm_ci_argc(ci));
12089 void *func = (
void *)OPERAND_AT(iobj, j);
12092 if (dladdr(func, &info) && info.dli_sname) {
12097 rb_str_catf(str,
"<%p>", func);
12107 if (types[j + 1]) {
12116dump_disasm_list(
const LINK_ELEMENT *link)
12118 dump_disasm_list_with_cursor(link, NULL, NULL);
12122dump_disasm_list_with_cursor(
const LINK_ELEMENT *link,
const LINK_ELEMENT *curr,
const LABEL *dest)
12129 printf(
"-- raw disasm--------\n");
12132 if (curr) printf(curr == link ?
"*" :
" ");
12133 switch (link->type) {
12134 case ISEQ_ELEMENT_INSN:
12136 iobj = (INSN *)link;
12137 str = insn_data_to_s_detail(iobj);
12138 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
12139 pos += insn_data_length(iobj);
12142 case ISEQ_ELEMENT_LABEL:
12144 lobj = (LABEL *)link;
12145 printf(LABEL_FORMAT
" [sp: %d, unremovable: %d, refcnt: %d]%s\n", lobj->label_no, lobj->sp, lobj->unremovable, lobj->refcnt,
12146 dest == lobj ?
" <---" :
"");
12149 case ISEQ_ELEMENT_TRACE:
12151 TRACE *trace = (TRACE *)link;
12152 printf(
" trace: %0x\n", trace->event);
12155 case ISEQ_ELEMENT_ADJUST:
12157 ADJUST *adjust = (ADJUST *)link;
12158 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
12163 rb_raise(
rb_eSyntaxError,
"dump_disasm_list error: %d\n", (
int)link->type);
12167 printf(
"---------------------\n");
12172rb_insn_len(
VALUE insn)
12174 return insn_len(insn);
12178rb_insns_name(
int i)
12180 return insn_name(i);
12184rb_insns_name_array(
void)
12188 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
12195register_label(rb_iseq_t *iseq,
struct st_table *labels_table,
VALUE obj)
12199 obj = rb_to_symbol_type(obj);
12201 if (st_lookup(labels_table, obj, &tmp) == 0) {
12202 label = NEW_LABEL(0);
12203 st_insert(labels_table, obj, (st_data_t)label);
12206 label = (LABEL *)tmp;
12213get_exception_sym2type(
VALUE sym)
12215 static VALUE symRescue, symEnsure, symRetry;
12216 static VALUE symBreak, symRedo, symNext;
12218 if (symRescue == 0) {
12227 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
12228 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
12229 if (sym == symRetry)
return CATCH_TYPE_RETRY;
12230 if (sym == symBreak)
return CATCH_TYPE_BREAK;
12231 if (sym == symRedo)
return CATCH_TYPE_REDO;
12232 if (sym == symNext)
return CATCH_TYPE_NEXT;
12233 rb_raise(
rb_eSyntaxError,
"invalid exception symbol: %+"PRIsVALUE, sym);
12238iseq_build_from_ary_exception(rb_iseq_t *iseq,
struct st_table *labels_table,
12244 const rb_iseq_t *eiseq;
12246 LABEL *lstart, *lend, *lcont;
12261 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
12262 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
12263 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
12267 if (
type == CATCH_TYPE_RESCUE ||
12268 type == CATCH_TYPE_BREAK ||
12269 type == CATCH_TYPE_NEXT) {
12275 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
12282static struct st_table *
12283insn_make_insn_table(
void)
12285 struct st_table *table;
12287 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
12289 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
12296static const rb_iseq_t *
12297iseq_build_load_iseq(
const rb_iseq_t *iseq,
VALUE op)
12300 const rb_iseq_t *loaded_iseq;
12303 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
12305 else if (
CLASS_OF(op) == rb_cISeq) {
12312 loaded_iseq = rb_iseqw_to_iseq(iseqw);
12313 return loaded_iseq;
12317iseq_build_callinfo_from_hash(rb_iseq_t *iseq,
VALUE op)
12321 unsigned int flag = 0;
12332 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
12334 if (!
NIL_P(vkw_arg)) {
12337 size_t n = rb_callinfo_kwarg_bytes(
len);
12340 kw_arg->references = 0;
12341 kw_arg->keyword_len =
len;
12342 for (i = 0; i <
len; i++) {
12345 kw_arg->keywords[i] = kw;
12350 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
12356event_name_to_flag(
VALUE sym)
12358#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
12372iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *
const anchor,
12379 int line_no = 0, node_id = -1, insn_idx = 0;
12380 int ret = COMPILE_OK;
12385 static struct st_table *insn_table;
12387 if (insn_table == 0) {
12388 insn_table = insn_make_insn_table();
12391 for (i=0; i<
len; i++) {
12397 ADD_TRACE(anchor, event);
12400 LABEL *label = register_label(iseq, labels_table, obj);
12401 ADD_LABEL(anchor, label);
12418 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
12420 COMPILE_ERROR(iseq, line_no,
12421 "unknown instruction: %+"PRIsVALUE, insn);
12426 if (argc != insn_len((
VALUE)insn_id)-1) {
12427 COMPILE_ERROR(iseq, line_no,
12428 "operand size mismatch");
12434 argv = compile_data_calloc2_type(iseq,
VALUE, argc);
12438 (LINK_ELEMENT*)new_insn_core(iseq, line_no, node_id,
12439 (
enum ruby_vminsn_type)insn_id, argc, argv));
12441 for (j=0; j<argc; j++) {
12443 switch (insn_op_type((
VALUE)insn_id, j)) {
12445 LABEL *label = register_label(iseq, labels_table, op);
12446 argv[j] = (
VALUE)label;
12455 if (insn_id == BIN(once)) op = rb_imemo_once_storage_new();
12462 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
12474 op = rb_to_array_type(op);
12478 sym = rb_to_symbol_type(sym);
12482 rb_const_path_t const_path = rb_const_path_from_array(segments);
12483 argv[j] = (
VALUE)const_path;
12486 ISEQ_BODY(iseq)->ic_size++;
12491 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
12492 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
12497 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
12498 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
12502 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
12505 argv[j] = rb_to_symbol_type(op);
12512 op = rb_to_array_type(op);
12517 register_label(iseq, labels_table, sym);
12518 cdhash_aset(map, key, (
VALUE)label);
12528#if SIZEOF_VALUE <= SIZEOF_LONG
12533 argv[j] = (
VALUE)funcptr;
12543 (LINK_ELEMENT*)new_insn_core(iseq, line_no, node_id,
12544 (
enum ruby_vminsn_type)insn_id, argc, NULL));
12548 rb_raise(
rb_eTypeError,
"unexpected object for instruction");
12553 validate_labels(iseq, labels_table);
12554 if (!ret)
return ret;
12555 return iseq_setup(iseq, anchor);
12558#define CHECK_ARRAY(v) rb_to_array_type(v)
12559#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
12564 VALUE val = rb_hash_aref(param, sym);
12569 else if (!
NIL_P(val)) {
12570 rb_raise(
rb_eTypeError,
"invalid %+"PRIsVALUE
" Fixnum: %+"PRIsVALUE,
12576static const struct rb_iseq_param_keyword *
12577iseq_build_kw(rb_iseq_t *iseq,
VALUE params,
VALUE keywords)
12582 VALUE key, sym, default_val;
12585 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12587 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
12589 keyword->num =
len;
12590#define SYM(s) ID2SYM(rb_intern_const(#s))
12591 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
12592 i = keyword->bits_start - keyword->num;
12593 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
12597 for (i = 0; i <
len; i++) {
12601 goto default_values;
12604 keyword->required_num++;
12608 default_len =
len - i;
12609 if (default_len == 0) {
12610 keyword->table = ids;
12613 else if (default_len < 0) {
12619 for (j = 0; i <
len; i++, j++) {
12633 rb_raise(
rb_eTypeError,
"keyword default has unsupported len %+"PRIsVALUE, key);
12639 keyword->table = ids;
12640 keyword->default_values = dvs;
12646iseq_insn_each_object_mark_and_move(
VALUE * obj,
VALUE _)
12648 rb_gc_mark_and_move(obj);
12655 size_t size =
sizeof(INSN);
12656 size_t align = ALIGNMENT_SIZE_OF(INSN);
12657 unsigned int pos = 0;
12660 size_t padding = calc_padding((
void *)&storage->buff[pos], align);
12661 size_t offset = pos + size + padding;
12662 if (offset > storage->size || offset > storage->pos) {
12664 storage = storage->next;
12667 pos += (int)padding;
12669 iobj = (INSN *)&storage->buff[pos];
12671 if (iobj->operands) {
12672 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark_and_move, (
VALUE)0);
12680 .wrap_struct_name =
"compiler/labels_wrapper",
12685 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
12689rb_iseq_build_from_ary(rb_iseq_t *iseq,
VALUE misc,
VALUE locals,
VALUE params,
12692#define SYM(s) ID2SYM(rb_intern_const(#s))
12694 unsigned int arg_size, local_size, stack_max;
12696 struct st_table *labels_table = st_init_numtable();
12698 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
12699 VALUE keywords = rb_hash_aref(params, SYM(keyword));
12701 DECL_ANCHOR(anchor);
12702 INIT_ANCHOR(anchor);
12705 ISEQ_BODY(iseq)->local_table_size =
len;
12706 ISEQ_BODY(iseq)->local_table = tbl =
len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
12708 for (i = 0; i <
len; i++) {
12711 if (sym_arg_rest == lv) {
12719#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
12720 if (INT_PARAM(lead_num)) {
12721 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
12723 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
12724 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
12726 if (
Qtrue == rb_hash_aref(params, SYM(rest))) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
12728#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
12730 INT_PARAM(arg_size);
12731 INT_PARAM(local_size);
12732 INT_PARAM(stack_max);
12736 VALUE source_hash = rb_hash_aref(misc,
ID2SYM(rb_intern(
"source_hash")));
12737 if (!
NIL_P(source_hash)) {
12738 ISEQ_BODY(iseq)->source_hash =
NUM2ULL(source_hash);
12742 node_ids = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_ids")));
12749 ISEQ_BODY(iseq)->param.flags.has_opt = !!(
len - 1 >= 0);
12751 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
12754 for (i = 0; i <
len; i++) {
12756 LABEL *label = register_label(iseq, labels_table, ent);
12757 opt_table[i] = (
VALUE)label;
12760 ISEQ_BODY(iseq)->param.opt_num =
len - 1;
12761 ISEQ_BODY(iseq)->param.opt_table = opt_table;
12764 else if (!
NIL_P(arg_opt_labels)) {
12765 rb_raise(
rb_eTypeError,
":opt param is not an array: %+"PRIsVALUE,
12770 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
12772 else if (!
NIL_P(keywords)) {
12773 rb_raise(
rb_eTypeError,
":keywords param is not an array: %+"PRIsVALUE,
12777 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
12778 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
12781 if (
Qtrue == rb_hash_aref(params, SYM(use_block))) {
12782 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
12785 if (int_param(&i, params, SYM(kwrest))) {
12786 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
12787 if (keyword == NULL) {
12788 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
12790 keyword->rest_start = i;
12791 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
12794 iseq_calc_param_size(iseq);
12797 iseq_build_from_ary_exception(iseq, labels_table, exception);
12800 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
12802 ISEQ_BODY(iseq)->param.size = arg_size;
12803 ISEQ_BODY(iseq)->local_table_size = local_size;
12804 ISEQ_BODY(iseq)->stack_max = stack_max;
12810rb_dvar_defined(
ID id,
const rb_iseq_t *iseq)
12814 while (body->type == ISEQ_TYPE_BLOCK ||
12815 body->type == ISEQ_TYPE_RESCUE ||
12816 body->type == ISEQ_TYPE_ENSURE ||
12817 body->type == ISEQ_TYPE_EVAL ||
12818 body->type == ISEQ_TYPE_MAIN
12822 for (i = 0; i < body->local_table_size; i++) {
12823 if (body->local_table[i] ==
id) {
12827 iseq = body->parent_iseq;
12828 body = ISEQ_BODY(iseq);
12835rb_local_defined(
ID id,
const rb_iseq_t *iseq)
12841 for (i=0; i<body->local_table_size; i++) {
12842 if (body->local_table[i] ==
id) {
12852#ifndef IBF_ISEQ_DEBUG
12853#define IBF_ISEQ_DEBUG 0
12856#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
12857#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
12860typedef uint32_t ibf_offset_t;
12861#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
12863#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
12865#define IBF_DEVEL_VERSION 15
12866#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
12868#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
12871static const char IBF_ENDIAN_MARK =
12872#ifdef WORDS_BIGENDIAN
12881 uint32_t major_version;
12882 uint32_t minor_version;
12884 uint32_t extra_size;
12886 uint32_t iseq_list_size;
12887 uint32_t global_object_list_size;
12888 ibf_offset_t iseq_list_offset;
12889 ibf_offset_t global_object_list_offset;
12910 unsigned int obj_list_size;
12911 ibf_offset_t obj_list_offset;
12921 VALUE path_override;
12922 VALUE realpath_override;
12932pinned_list_mark(
void *ptr)
12936 for (i = 0; i < list->size; i++) {
12937 if (list->buffer[i]) {
12938 rb_gc_mark(list->buffer[i]);
12950 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
12954pinned_list_fetch(
VALUE list,
long offset)
12960 if (offset < 0 || offset >= ptr->size) {
12961 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12964 return ptr->buffer[offset];
12968pinned_list_store(
VALUE list,
long offset,
VALUE object)
12974 if (offset < 0 || offset >= ptr->size) {
12975 rb_raise(
rb_eIndexError,
"object index out of range: %ld", offset);
12982pinned_list_new(
long size)
12984 size_t memsize = offsetof(
struct pinned_list, buffer) + size *
sizeof(
VALUE);
12985 VALUE obj_list = rb_data_typed_object_zalloc(0, memsize, &pinned_list_type);
12986 struct pinned_list * ptr = RTYPEDDATA_GET_DATA(obj_list);
12992ibf_dump_pos(
struct ibf_dump *dump)
12994 rb_len_t pos = RSTRING_LEN(dump->current_buffer->str);
12995#if SIZEOF_RB_LEN_T > SIZEOF_INT
12996 if (pos >= UINT_MAX) {
13000 return (
unsigned int)pos;
13004ibf_dump_align(
struct ibf_dump *dump,
size_t align)
13006 ibf_offset_t pos = ibf_dump_pos(dump);
13008 static const char padding[
sizeof(
VALUE)];
13009 size_t size = align - ((size_t)pos % align);
13010#if SIZEOF_RB_LEN_T > SIZEOF_INT
13011 if (pos + size >= UINT_MAX) {
13015 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
13016 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
13018 rb_str_cat(dump->current_buffer->str, padding, size);
13023ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
size_t size)
13025 ibf_offset_t pos = ibf_dump_pos(dump);
13026#if SIZEOF_RB_LEN_T > SIZEOF_INT
13028 if (size >= UINT_MAX || pos + size >= UINT_MAX) {
13032 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
13037ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
13039 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
13043ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
rb_len_t offset)
13045 VALUE str = dump->current_buffer->str;
13046 char *ptr = RSTRING_PTR(str);
13048 rb_bug(
"ibf_dump_overwrite: overflow");
13049 memcpy(ptr + offset, buff, size);
13053ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
13055 ibf_offset_t beg = *offset;
13057 return load->current_buffer->buff + beg;
13061ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
13063 void *buff = ruby_xmalloc2(x, y);
13064 size_t size = x * y;
13065 memcpy(buff, load->current_buffer->buff + offset, size);
13069#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
13071#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
13072#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
13073#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
13074#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
13075#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
13078ibf_table_lookup(
struct st_table *table, st_data_t key)
13082 if (st_lookup(table, key, &val)) {
13091ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
13093 int index = ibf_table_lookup(table, key);
13096 index = (int)table->num_entries;
13097 st_insert(table, key, (st_data_t)index);
13105static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
13108static rb_iseq_t *ibf_load_iseq(
const struct ibf_load *load,
const rb_iseq_t *index_iseq);
13111ibf_dump_object_table_new(
void)
13113 st_table *obj_table = st_init_numtable();
13114 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
13122 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
13128 if (
id == 0 || rb_id2name(
id) == NULL) {
13131 return ibf_dump_object(dump,
rb_id2sym(
id));
13135ibf_load_id(
const struct ibf_load *load,
const ID id_index)
13137 if (id_index == 0) {
13140 VALUE sym = ibf_load_object(load, id_index);
13150static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
13153ibf_dump_iseq(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13155 if (iseq == NULL) {
13159 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
13163static unsigned char
13164ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
13166 if (*offset >= load->current_buffer->size) { rb_raise(
rb_eRuntimeError,
"invalid bytecode"); }
13167 return (
unsigned char)load->current_buffer->buff[(*offset)++];
13183 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
13184 ibf_dump_write(dump, &x,
sizeof(
VALUE));
13188 enum { max_byte_length =
sizeof(
VALUE) + 1 };
13190 unsigned char bytes[max_byte_length];
13193 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
13194 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
13200 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
13203 ibf_dump_write(dump, bytes + max_byte_length - n, n);
13207ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
13209 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
13210 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
13212 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
13213 *offset +=
sizeof(
VALUE);
13218 enum { max_byte_length =
sizeof(
VALUE) + 1 };
13220 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
13221 const unsigned char c = buffer[*offset];
13225 c == 0 ? 9 : ntz_int32(c) + 1;
13228 if (*offset + n > load->current_buffer->size) {
13233 for (i = 1; i < n; i++) {
13235 x |= (
VALUE)buffer[*offset + i];
13249 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
13251 size_t len = strlen(bf->name);
13252 ibf_dump_write_small_value(dump, (
VALUE)
len);
13253 ibf_dump_write(dump, bf->name,
len);
13257ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
13259 int i = (int)ibf_load_small_value(load, offset);
13260 int len = (int)ibf_load_small_value(load, offset);
13261 const char *name = (
char *)ibf_load_ptr(load, offset,
len);
13264 fprintf(stderr,
"%.*s!!\n",
len, name);
13268 if (table == NULL) rb_raise(rb_eArgError,
"builtin function table is not provided");
13269 if (strncmp(table[i].name, name,
len) != 0) {
13270 rb_raise(rb_eArgError,
"builtin function index (%d) mismatch (expect %.*s but %s)",
13271 i,
len, name, table[i].name);
13279ibf_dump_code(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13282 const int iseq_size = body->iseq_size;
13284 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
13286 ibf_offset_t offset = ibf_dump_pos(dump);
13288 for (code_index=0; code_index<iseq_size;) {
13289 const VALUE insn = orig_code[code_index++];
13290 const char *types = insn_op_types(insn);
13295 ibf_dump_write_small_value(dump, insn);
13298 for (op_index=0; types[op_index]; op_index++, code_index++) {
13299 VALUE op = orig_code[code_index];
13302 switch (types[op_index]) {
13305 wv = ibf_dump_object(dump, op);
13308 wv = (
VALUE)ibf_dump_iseq(dump, (
const rb_iseq_t *)op);
13315 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
13323 wv = ibf_dump_id(dump, (
ID)op);
13335 ibf_dump_write_small_value(dump, wv);
13344static rb_const_path_t
13345ibf_load_const_path(
const struct ibf_load *load, ibf_offset_t *reading_pos)
13347 size_t header = (size_t)ibf_load_small_value(load, reading_pos);
13348 bool absolute = header & 1;
13349 size_t size = header >> 1;
13356 for (
size_t i = 0; i < size; i++) {
13357 ids[i] = ibf_load_id(load, (
ID)ibf_load_small_value(load, reading_pos));
13358 if (!is_notop_id(ids[i]) || !is_const_id(ids[i])) {
13363 rb_const_path_t const_path = rb_const_path_from_ids(absolute, ids, size);
13369ibf_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)
13372 unsigned int code_index;
13373 ibf_offset_t reading_pos = bytecode_offset;
13377 struct rb_call_data *cd_entries = load_body->call_data;
13379 load_body->iseq_encoded = code;
13380 load_body->iseq_size = iseq_size;
13382 iseq_bits_t * mark_offset_bits;
13383 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
13384 load_body->mark_bits.single = 0;
13385 mark_offset_bits = &load_body->mark_bits.single;
13388 load_body->mark_bits.list =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
13389 mark_offset_bits = load_body->mark_bits.list;
13391 bool needs_bitmap =
false;
13393 for (code_index=0; code_index<iseq_size;) {
13395 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
13396 const char *types = insn_op_types(insn);
13402 for (op_index=0; types[op_index]; op_index++, code_index++) {
13403 const char operand_type = types[op_index];
13404 switch (operand_type) {
13407 VALUE op = ibf_load_small_value(load, &reading_pos);
13408 VALUE v = ibf_load_object(load, op);
13409 code[code_index] = v;
13412 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13413 needs_bitmap =
true;
13419 VALUE op = ibf_load_small_value(load, &reading_pos);
13420 VALUE v = ibf_load_object(load, op);
13425 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
13427 code[code_index] = v;
13428 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13430 needs_bitmap =
true;
13435 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
13436 VALUE v = (
VALUE)ibf_load_iseq(load, (
const rb_iseq_t *)op);
13437 code[code_index] = v;
13440 ISEQ_MBITS_SET(mark_offset_bits, code_index);
13441 needs_bitmap =
true;
13449 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13452 code[code_index] = (
VALUE)ic;
13454 if (operand_type == TS_IVC) {
13455 IVC cache = (IVC)ic;
13457 if (insn == BIN(setinstancevariable)) {
13458 ID iv_name = (
ID)code[code_index - 1];
13459 cache->iv_set_name = iv_name;
13460 cache->value = IVAR_CACHE_INIT;
13463 cache->iv_set_name = 0;
13464 cache->value = rb_getivar_cache_pack(ROOT_SHAPE_ID, ATTR_INDEX_NOT_SET);
13472 code[code_index] = (
VALUE)cd_entries++;
13477 VALUE op = ibf_load_small_value(load, &reading_pos);
13478 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
13485 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
13488 code[code_index] = ibf_load_small_value(load, &reading_pos);
13492 if (insn_len(insn) != op_index+1) {
13497 if (!needs_bitmap) {
13498 SIZED_FREE_N(load_body->mark_bits.list, ISEQ_MBITS_BUFLEN(iseq_size));
13499 load_body->mark_bits.list = NULL;
13503 RUBY_ASSERT(reading_pos == bytecode_offset + bytecode_size);
13508ibf_dump_param_opt_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13510 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
13513 IBF_W_ALIGN(
VALUE);
13514 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
13517 return ibf_dump_pos(dump);
13522ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
13526 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
13535ibf_dump_param_keyword(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13537 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
13540 struct rb_iseq_param_keyword dump_kw = *kw;
13541 int dv_num = kw->num - kw->required_num;
13546 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
13547 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
13549 dump_kw.table = IBF_W(ids,
ID, kw->num);
13550 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
13551 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
13552 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
13559static const struct rb_iseq_param_keyword *
13560ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
13562 if (param_keyword_offset) {
13563 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
13564 int dv_num = kw->num - kw->required_num;
13565 VALUE *dvs = dv_num ? IBF_R(kw->default_values,
VALUE, dv_num) : NULL;
13568 for (i=0; i<dv_num; i++) {
13569 dvs[i] = ibf_load_object(load, dvs[i]);
13575 kw->default_values = dvs;
13584ibf_dump_insns_info_body(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13586 ibf_offset_t offset = ibf_dump_pos(dump);
13591 ibf_dump_write_small_value(dump, info->node_ids_size);
13593 for (i = 0; i < info->size; i++) {
13594 ibf_dump_write_small_value(dump, entries[i].line_no);
13595 ibf_dump_write_small_value(dump, entries[i].events);
13597 ibf_dump_write(dump, rb_iseq_insn_info_node_ids(info), info->node_ids_size);
13603ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
13605 ibf_offset_t offset = ibf_dump_pos(dump);
13607 unsigned int last = 0;
13609 for (i = 0; i < size; i++) {
13610 ibf_dump_write_small_value(dump, positions[i] - last);
13611 last = positions[i];
13618ibf_load_insns_info(
const struct ibf_load *load, ibf_offset_t body_offset, ibf_offset_t positions_offset,
13619 unsigned int size,
unsigned int iseq_size)
13621 VALUE entries_buf, positions_buf;
13623 unsigned int *positions =
ALLOCV_N(
unsigned int, positions_buf, size);
13625 ibf_offset_t reading_pos = body_offset;
13626 const unsigned int node_ids_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13627 for (
unsigned int i = 0; i < size; i++) {
13628 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
13629 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
13631 if (node_ids_size == 0 || node_ids_size > load->current_buffer->size - reading_pos) {
13634 const uint8_t *node_ids = (
const uint8_t *)load->current_buffer->buff + reading_pos;
13636 reading_pos = positions_offset;
13637 unsigned int last = 0;
13638 for (
unsigned int i = 0; i < size; i++) {
13639 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
13640 last = positions[i];
13643 const struct iseq_insn_info *info = rb_iseq_insn_info_new(iseq_size, entries, positions, size, node_ids, node_ids_size);
13651ibf_dump_local_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13654 const int size = body->local_table_size;
13658 for (i=0; i<size; i++) {
13659 VALUE v = ibf_dump_id(dump, body->local_table[i]);
13662 v = ibf_dump_object(dump,
ULONG2NUM(body->local_table[i]));
13668 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
13672ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
13675 ID *table = IBF_R(local_table_offset,
ID, size);
13678 for (i=0; i<size; i++) {
13679 table[i] = ibf_load_id(load, table[i]);
13682 if (size == 1 && table[0] == idERROR_INFO) {
13683 ruby_xfree_sized(table,
sizeof(
ID) * size);
13684 return rb_iseq_shared_exc_local_tbl;
13696ibf_dump_lvar_states(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13699 const int size = ISEQ_LVAR_STATES_BUFLEN(body->local_table_size);
13700 IBF_W_ALIGN(uint8_t);
13701 if (iseq_has_lvar_states_p(body)) {
13702 return ibf_dump_write(dump, iseq_lvar_states(body),
sizeof(uint8_t) * size);
13705 return ibf_dump_write(dump, NULL, 0);
13710ibf_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)
13712 if (local_table == rb_iseq_shared_exc_local_tbl ||
13714 load_body->lvar_states.list = NULL;
13716 else if (ISEQ_LVAR_STATES_EMBED_P((
unsigned int)size)) {
13717 ibf_offset_t pos = lvar_states_offset;
13718 const int len =
sizeof(uint8_t) * ISEQ_LVAR_STATES_BUFLEN(size);
13719 memcpy(load_body->lvar_states.single, ibf_load_ptr(load, &pos,
len),
len);
13722 load_body->lvar_states.list = IBF_R(lvar_states_offset, uint8_t, ISEQ_LVAR_STATES_BUFLEN(size));
13727ibf_dump_catch_table(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13732 int *iseq_indices =
ALLOCA_N(
int, table->size);
13735 for (i=0; i<table->size; i++) {
13736 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
13739 const ibf_offset_t offset = ibf_dump_pos(dump);
13741 for (i=0; i<table->size; i++) {
13742 ibf_dump_write_small_value(dump, iseq_indices[i]);
13743 ibf_dump_write_small_value(dump, table->entries[i].type);
13744 ibf_dump_write_small_value(dump, table->entries[i].start);
13745 ibf_dump_write_small_value(dump, table->entries[i].end);
13746 ibf_dump_write_small_value(dump, table->entries[i].cont);
13747 ibf_dump_write_small_value(dump, table->entries[i].sp);
13752 return ibf_dump_pos(dump);
13757ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size,
const rb_iseq_t *parent_iseq)
13760 struct iseq_catch_table *table = ruby_xcalloc(1, iseq_catch_table_bytes(size));
13761 table->size = size;
13762 ISEQ_BODY(parent_iseq)->catch_table = table;
13764 ibf_offset_t reading_pos = catch_table_offset;
13767 for (i=0; i<table->size; i++) {
13768 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
13769 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
13770 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13771 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13772 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13773 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13775 rb_iseq_t *catch_iseq = (rb_iseq_t *)ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)iseq_index);
13776 RB_OBJ_WRITE(parent_iseq, UNALIGNED_MEMBER_PTR(&table->entries[i], iseq), catch_iseq);
13780 ISEQ_BODY(parent_iseq)->catch_table = NULL;
13785ibf_dump_ci_entries(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13788 const unsigned int ci_size = body->ci_size;
13791 ibf_offset_t offset = ibf_dump_pos(dump);
13795 for (i = 0; i < ci_size; i++) {
13798 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
13799 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
13800 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
13804 int len = kwarg->keyword_len;
13805 ibf_dump_write_small_value(dump,
len);
13806 for (
int j=0; j<
len; j++) {
13807 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
13808 ibf_dump_write_small_value(dump, keyword);
13812 ibf_dump_write_small_value(dump, 0);
13817 ibf_dump_write_small_value(dump, (
VALUE)-1);
13835static enum rb_id_table_iterator_result
13836store_outer_variable(
ID id,
VALUE val,
void *dump)
13841 pair->name = rb_id2str(
id);
13843 return ID_TABLE_CONTINUE;
13847outer_variable_cmp(
const void *a,
const void *b,
void *arg)
13855 else if (!bp->name) {
13863ibf_dump_outer_variables(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13865 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
13867 ibf_offset_t offset = ibf_dump_pos(dump);
13869 size_t size = ovs ? rb_id_table_size(ovs) : 0;
13870 ibf_dump_write_small_value(dump, (
VALUE)size);
13879 rb_id_table_foreach(ovs, store_outer_variable, ovlist);
13881 for (
size_t i = 0; i < size; ++i) {
13882 ID id = ovlist->pairs[i].id;
13883 ID val = ovlist->pairs[i].val;
13884 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
13885 ibf_dump_write_small_value(dump, val);
13894ibf_dump_ic_entries(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13897 ibf_offset_t offset = ibf_dump_pos(dump);
13899 for (
unsigned int i = 0; i < body->ic_size; i++) {
13901 const rb_const_path_t const_path = ic->const_path;
13902 size_t size = const_path_size(const_path);
13903 ibf_dump_write_small_value(dump, (size << 1) | (const_path_absolute_p(const_path) ? 1 : 0));
13904 for (
size_t j = 0; j < size; j++) {
13905 ibf_dump_write_small_value(dump, ibf_dump_id(dump, const_path_aref(const_path, j)));
13915 ibf_offset_t reading_pos = ic_entries_offset;
13917 for (
unsigned int i = 0; i < load_body->ic_size; i++) {
13918 IC ic = &ISEQ_IS_IC_ENTRY(load_body, i);
13919 ic->const_path = ibf_load_const_path(load, &reading_pos);
13925ibf_load_ci_entries(
const struct ibf_load *load,
13926 ibf_offset_t ci_entries_offset,
13927 unsigned int ci_size,
13935 ibf_offset_t reading_pos = ci_entries_offset;
13942 for (i = 0; i < ci_size; i++) {
13943 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
13944 if (mid_index != (
VALUE)-1) {
13945 ID mid = ibf_load_id(load, mid_index);
13946 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13947 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
13950 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
13953 kwarg->references = 0;
13954 kwarg->keyword_len = kwlen;
13955 for (
int j=0; j<kwlen; j++) {
13956 VALUE keyword = ibf_load_small_value(load, &reading_pos);
13957 kwarg->keywords[j] = ibf_load_object(load, keyword);
13961 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
13963 cds[i].cc = vm_cc_empty();
13974ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
13976 ibf_offset_t reading_pos = outer_variables_offset;
13980 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
13982 if (table_size > 0) {
13983 tbl = rb_id_table_create(table_size);
13986 for (
size_t i = 0; i < table_size; i++) {
13987 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
13988 VALUE value = ibf_load_small_value(load, &reading_pos);
13989 if (!key) key = rb_make_temporary_id(i);
13990 rb_id_table_insert(tbl, key, value);
13997ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq)
13999 RUBY_ASSERT(dump->current_buffer == &dump->global_buffer);
14001 unsigned int *positions;
14005 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
14006 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
14008#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14009 ibf_offset_t iseq_start = ibf_dump_pos(dump);
14014 buffer.obj_table = ibf_dump_object_table_new();
14015 dump->current_buffer = &buffer;
14018 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
14019 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
14020 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
14021 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
14022 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
14024 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
14025 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info->size);
14026 SIZED_FREE_N(positions, body->insns_info->size);
14028 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
14029 const ibf_offset_t lvar_states_offset = ibf_dump_lvar_states(dump, iseq);
14030 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
14031 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
14033 const int parent_iseq_index = ibf_table_lookup(dump->iseq_table, (st_data_t)ISEQ_BODY(iseq)->parent_iseq);
14034 const int local_iseq_index = ibf_table_lookup(dump->iseq_table, (st_data_t)ISEQ_BODY(iseq)->local_iseq);
14035 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_MANDATORY_ONLY_ISEQ(iseq));
14036 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
14037 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
14038 const ibf_offset_t ic_entries_offset = ibf_dump_ic_entries(dump, iseq);
14040#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14041 ibf_offset_t local_obj_list_offset;
14042 unsigned int local_obj_list_size;
14044 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
14047 ibf_offset_t body_offset = ibf_dump_pos(dump);
14050 unsigned int param_flags =
14051 (body->param.flags.has_lead << 0) |
14052 (body->param.flags.has_opt << 1) |
14053 (body->param.flags.has_rest << 2) |
14054 (body->param.flags.has_post << 3) |
14055 (body->param.flags.has_kw << 4) |
14056 (body->param.flags.has_kwrest << 5) |
14057 (body->param.flags.has_block << 6) |
14058 (body->param.flags.ambiguous_param0 << 7) |
14059 (body->param.flags.accepts_no_kwarg << 8) |
14060 (body->param.flags.ruby2_keywords << 9) |
14061 (body->param.flags.anon_rest << 10) |
14062 (body->param.flags.anon_kwrest << 11) |
14063 (body->param.flags.use_block << 12) |
14064 (body->param.flags.forwardable << 13) |
14065 (body->param.flags.accepts_no_block << 14);
14067#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14068# define IBF_BODY_OFFSET(x) (x)
14070# define IBF_BODY_OFFSET(x) (body_offset - (x))
14073 ibf_dump_write_small_value(dump, body->type);
14074 ibf_dump_write_small_value(dump, body->iseq_size);
14075 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
14076 ibf_dump_write_small_value(dump, bytecode_size);
14077 ibf_dump_write_small_value(dump, param_flags);
14078 ibf_dump_write_small_value(dump, body->param.size);
14079 ibf_dump_write_small_value(dump, body->param.lead_num);
14080 ibf_dump_write_small_value(dump, body->param.opt_num);
14081 ibf_dump_write_small_value(dump, body->param.post_num);
14082 ibf_dump_write_small_value(dump, body->param.block_start);
14083 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
14084 ibf_dump_write_small_value(dump, param_keyword_offset);
14085 ibf_dump_write_small_value(dump, location_pathobj_index);
14086 ibf_dump_write_small_value(dump, body->no_realpath);
14087 ibf_dump_write_small_value(dump, location_label_index);
14088 ibf_dump_write_small_value(dump, body->location.first_lineno);
14089 ibf_dump_write_small_value(dump, body->location.node_id);
14092 ibf_dump_write_small_value(dump, (
VALUE)(uint32_t)(body->source_hash >> 32));
14093 ibf_dump_write_small_value(dump, (
VALUE)(uint32_t)body->source_hash);
14094 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
14095 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
14096 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
14097 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
14098 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
14099 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
14100 ibf_dump_write_small_value(dump, body->insns_info->size);
14101 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
14102 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(lvar_states_offset));
14103 ibf_dump_write_small_value(dump, catch_table_size);
14104 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
14105 ibf_dump_write_small_value(dump, parent_iseq_index);
14106 ibf_dump_write_small_value(dump, local_iseq_index);
14107 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
14108 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
14109 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
14110 ibf_dump_write_small_value(dump, ISEQ_FLIP_CNT(iseq));
14111 ibf_dump_write_small_value(dump, body->local_table_size);
14112 ibf_dump_write_small_value(dump, body->ivc_size);
14113 ibf_dump_write_small_value(dump, body->icvarc_size);
14114 ibf_dump_write_small_value(dump, body->ic_size);
14115 if (body->ic_size) {
14116 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ic_entries_offset));
14118 ibf_dump_write_small_value(dump, body->ci_size);
14119 ibf_dump_write_small_value(dump, body->stack_max);
14120 ibf_dump_write_small_value(dump, body->builtin_attrs);
14121 ibf_dump_write_small_value(dump, body->prism ? 1 : 0);
14123#undef IBF_BODY_OFFSET
14125#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14126 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
14128 dump->current_buffer = saved_buffer;
14129 ibf_dump_write(dump, RSTRING_PTR(buffer.str), iseq_length_bytes);
14131 ibf_offset_t offset = ibf_dump_pos(dump);
14132 ibf_dump_write_small_value(dump, iseq_start);
14133 ibf_dump_write_small_value(dump, iseq_length_bytes);
14134 ibf_dump_write_small_value(dump, body_offset);
14136 ibf_dump_write_small_value(dump, local_obj_list_offset);
14137 ibf_dump_write_small_value(dump, local_obj_list_size);
14139 st_free_table(buffer.obj_table);
14143 return body_offset;
14148ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
14150 VALUE str = ibf_load_object(load, str_index);
14152 str = rb_fstring(str);
14158ibf_load_iseq_each(
struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t offset)
14162 ibf_offset_t reading_pos = offset;
14164#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14166 load->current_buffer = &load->global_buffer;
14168 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14169 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14170 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14173 buffer.buff = load->global_buffer.buff + iseq_start;
14174 buffer.size = iseq_length_bytes;
14175 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14176 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14177 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
14179 load->current_buffer = &buffer;
14180 reading_pos = body_offset;
14183#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14184# define IBF_BODY_OFFSET(x) (x)
14186# define IBF_BODY_OFFSET(x) (offset - (x))
14189 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14190 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14191 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14192 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14193 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14194 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14195 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
14196 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
14197 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
14198 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
14199 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14200 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
14201 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
14202 const bool no_realpath = (bool)ibf_load_small_value(load, &reading_pos);
14203 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
14204 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
14205 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
14206 const uint64_t source_hash_hi = (uint64_t)ibf_load_small_value(load, &reading_pos);
14207 const uint64_t source_hash_lo = (uint64_t)ibf_load_small_value(load, &reading_pos);
14208 const uint64_t source_hash = (source_hash_hi << 32) | (uint32_t)source_hash_lo;
14209 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
14210 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
14211 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
14212 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
14213 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14214 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14215 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14216 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14217 const ibf_offset_t lvar_states_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14218 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14219 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14220 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
14221 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
14222 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
14223 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14224 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
14225 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
14226 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14228 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14229 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14230 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14231 const ibf_offset_t ic_entries_offset = ic_size ? (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos)) : 0;
14233 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14234 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14235 const unsigned int builtin_attrs = (
unsigned int)ibf_load_small_value(load, &reading_pos);
14236 const bool prism = (bool)ibf_load_small_value(load, &reading_pos);
14239 VALUE path = ibf_load_object(load, location_pathobj_index);
14244 path = rb_fstring(path);
14245 realpath = no_realpath ?
Qnil : path;
14248 VALUE pathobj = path;
14254 if (!
NIL_P(realpath)) {
14256 rb_raise(rb_eArgError,
"unexpected realpath %"PRIxVALUE
14257 "(%x), path=%+"PRIsVALUE,
14258 realpath,
TYPE(realpath), path);
14260 realpath = rb_fstring(realpath);
14266 if (
RTEST(load->path_override)) {
14267 path = load->path_override;
14268 realpath = load->realpath_override;
14270 rb_iseq_pathobj_set(iseq, path, realpath);
14274 rb_execution_context_t *ec = GET_EC();
14275 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
14277#undef IBF_BODY_OFFSET
14279 load_body->type =
type;
14280 load_body->stack_max = stack_max;
14281 load_body->param.flags.has_lead = (param_flags >> 0) & 1;
14282 load_body->param.flags.has_opt = (param_flags >> 1) & 1;
14283 load_body->param.flags.has_rest = (param_flags >> 2) & 1;
14284 load_body->param.flags.has_post = (param_flags >> 3) & 1;
14285 load_body->param.flags.has_kw = FALSE;
14286 load_body->param.flags.has_kwrest = (param_flags >> 5) & 1;
14287 load_body->param.flags.has_block = (param_flags >> 6) & 1;
14288 load_body->param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
14289 load_body->param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
14290 load_body->param.flags.ruby2_keywords = (param_flags >> 9) & 1;
14291 load_body->param.flags.anon_rest = (param_flags >> 10) & 1;
14292 load_body->param.flags.anon_kwrest = (param_flags >> 11) & 1;
14293 load_body->param.flags.use_block = (param_flags >> 12) & 1;
14294 load_body->param.flags.forwardable = (param_flags >> 13) & 1;
14295 load_body->param.flags.accepts_no_block = (param_flags >> 14) & 1;
14296 load_body->param.size = param_size;
14297 load_body->param.lead_num = param_lead_num;
14298 load_body->param.opt_num = param_opt_num;
14299 load_body->param.post_num = param_post_num;
14300 load_body->param.block_start = param_block_start;
14301 load_body->local_table_size = local_table_size;
14302 load_body->ci_size = ci_size;
14305 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
14306 if (variable_flip_count) {
14308 v->flip_count = variable_flip_count;
14311 load_body->location.first_lineno = location_first_lineno;
14312 load_body->location.node_id = location_node_id;
14313 load_body->source_hash = source_hash;
14314 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
14315 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
14316 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
14317 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
14318 load_body->builtin_attrs = builtin_attrs;
14319 load_body->prism = prism;
14321 load_body->ivc_size = ivc_size;
14322 load_body->icvarc_size = icvarc_size;
14323 load_body->ic_size = ic_size;
14325 if (ISEQ_IS_SIZE(load_body)) {
14329 load_body->is_entries = NULL;
14331 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
14332 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
14333 ibf_load_ic_entries(load, ic_entries_offset, load_body);
14334 load_body->param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
14335 load_body->param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
14336 load_body->param.flags.has_kw = (param_flags >> 4) & 1;
14337 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
14338 ibf_load_lvar_states(load, load_body, lvar_states_offset, local_table_size, load_body->local_table);
14339 ibf_load_catch_table(load, catch_table_offset, catch_table_size, iseq);
14341 const rb_iseq_t *parent_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)parent_iseq_index);
14342 const rb_iseq_t *local_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)local_iseq_index);
14343 const rb_iseq_t *mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
14345 RB_OBJ_WRITE(iseq, &load_body->parent_iseq, parent_iseq);
14346 RB_OBJ_WRITE(iseq, &load_body->local_iseq, local_iseq);
14347 rb_iseq_mandatory_only_iseq_set(iseq, mandatory_only_iseq);
14350 if (load_body->param.keyword != NULL) {
14352 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *) load_body->param.keyword;
14353 keyword->table = &load_body->local_table[keyword->bits_start - keyword->num];
14356 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
14357 load_body->insns_info = ibf_load_insns_info(load, insns_info_body_offset, insns_info_positions_offset,
14358 insns_info_size, load_body->iseq_size);
14360 rb_iseq_translate_threaded_code(iseq);
14362#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14363 load->current_buffer = &load->global_buffer;
14366 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
14368#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
14369 load->current_buffer = saved_buffer;
14371 verify_call_cache(iseq);
14374 rb_vm_pop_frame_no_int(ec);
14384ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
14386 const rb_iseq_t *iseq = (
const rb_iseq_t *)key;
14389 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
14392 return ST_CONTINUE;
14402 args.offset_list = offset_list;
14404 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
14407 st_index_t size = dump->iseq_table->num_entries;
14408 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
14410 for (i = 0; i < size; i++) {
14414 ibf_dump_align(dump,
sizeof(ibf_offset_t));
14415 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
14416 header->iseq_list_size = (
unsigned int)size;
14426 unsigned int type: 5;
14427 unsigned int special_const: 1;
14428 unsigned int frozen: 1;
14429 unsigned int internal: 1;
14432enum ibf_object_class_index {
14433 IBF_OBJECT_CLASS_OBJECT,
14434 IBF_OBJECT_CLASS_ARRAY,
14435 IBF_OBJECT_CLASS_STANDARD_ERROR,
14436 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
14437 IBF_OBJECT_CLASS_TYPE_ERROR,
14438 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
14448 long keyval[FLEX_ARY_LEN];
14461 BDIGIT digits[FLEX_ARY_LEN];
14464enum ibf_object_data_type {
14465 IBF_OBJECT_DATA_ENCODING,
14476#define IBF_ALIGNED_OFFSET(align, offset) \
14477 ((((offset) - 1) / (align) + 1) * (align))
14482#define IBF_OBJBODY(type, offset) \
14483 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
14486ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
14488 if (offset >= load->current_buffer->size) {
14489 rb_raise(
rb_eIndexError,
"object offset out of range: %"PRIdSIZE, offset);
14491 return load->current_buffer->buff + offset;
14494NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
14497ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
14500 rb_raw_obj_info(buff,
sizeof(buff), obj);
14509 rb_raise(rb_eArgError,
"unsupported");
14516 enum ibf_object_class_index cindex;
14518 cindex = IBF_OBJECT_CLASS_OBJECT;
14521 cindex = IBF_OBJECT_CLASS_ARRAY;
14524 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
14527 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
14530 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
14533 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
14536 rb_obj_info_dump(obj);
14538 rb_bug(
"unsupported class");
14540 ibf_dump_write_small_value(dump, (
VALUE)cindex);
14546 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
14549 case IBF_OBJECT_CLASS_OBJECT:
14551 case IBF_OBJECT_CLASS_ARRAY:
14553 case IBF_OBJECT_CLASS_STANDARD_ERROR:
14555 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
14557 case IBF_OBJECT_CLASS_TYPE_ERROR:
14559 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
14563 rb_raise(rb_eArgError,
"ibf_load_object_class: unknown class (%d)", (
int)cindex);
14571 (void)IBF_W(&dbl,
double, 1);
14579 memcpy(&d, IBF_OBJBODY(
double, offset),
sizeof(d));
14588 long encindex = (long)rb_enc_get_index(obj);
14590 const char *ptr = RSTRING_PTR(obj);
14592 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14593 rb_encoding *enc = rb_enc_from_index((
int)encindex);
14594 const char *enc_name = rb_enc_name(enc);
14595 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
14598 ibf_dump_write_small_value(dump, encindex);
14599 ibf_dump_write_small_value(dump,
len);
14600 IBF_WP(ptr,
char,
len);
14606 ibf_offset_t reading_pos = offset;
14608 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14610 const char *ptr = load->current_buffer->buff + reading_pos;
14612 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14613 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14614 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14618 if (header->frozen && !header->internal) {
14619 str = rb_enc_literal_str(ptr,
len, rb_enc_from_index(encindex));
14622 str = rb_enc_str_new(ptr,
len, rb_enc_from_index(encindex));
14625 if (header->frozen) str = rb_fstring(str);
14636 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
14638 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
14639 ibf_dump_write_small_value(dump, regexp.srcstr);
14646 regexp.option = ibf_load_byte(load, &offset);
14647 regexp.srcstr = ibf_load_small_value(load, &offset);
14649 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
14650 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
14662 ibf_dump_write_small_value(dump,
len);
14663 for (i=0; i<
len; i++) {
14664 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
14665 ibf_dump_write_small_value(dump, index);
14672 ibf_offset_t reading_pos = offset;
14679 for (i=0; i<
len; i++) {
14680 const VALUE index = ibf_load_small_value(load, &reading_pos);
14684 if (header->frozen) {
14685 rb_ary_freeze(ary);
14686 rb_ractor_make_shareable(ary);
14693ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
14697 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14698 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
14700 ibf_dump_write_small_value(dump, key_index);
14701 ibf_dump_write_small_value(dump, val_index);
14702 return ST_CONTINUE;
14709 ibf_dump_write_small_value(dump, (
VALUE)
len);
14717 long len = (long)ibf_load_small_value(load, &offset);
14718 VALUE obj = header->frozen ? rb_hash_alloc_fixed_size(
rb_cHash,
len) : rb_hash_new_capa(
len);
14721 for (i = 0; i <
len; i++) {
14722 VALUE key_index = ibf_load_small_value(load, &offset);
14723 VALUE val_index = ibf_load_small_value(load, &offset);
14725 VALUE key = ibf_load_object(load, key_index);
14726 VALUE val = ibf_load_object(load, val_index);
14727 rb_hash_aset(obj, key, val);
14731 if (header->frozen) {
14732 RB_OBJ_SET_FROZEN_SHAREABLE(obj);
14739ibf_dump_cdhash_i(st_data_t key, st_data_t val, st_data_t ptr)
14743 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
14745 ibf_dump_write_small_value(dump, key_index);
14746 ibf_dump_write_small_value(dump, val);
14747 return ST_CONTINUE;
14753 ibf_dump_write_small_value(dump, (
VALUE)imemo_type(obj));
14754 switch (imemo_type(obj)) {
14755 case imemo_once_storage:
14757 case imemo_cdhash: {
14758 st_table *tbl = rb_imemo_cdhash_tbl(obj);
14760 long len = tbl->num_entries;
14761 ibf_dump_write_small_value(dump, (
VALUE)
len);
14766 ibf_dump_object_unsupported(dump, obj);
14774 enum imemo_type
type = (
enum imemo_type)ibf_load_small_value(load, &offset);
14776 case imemo_once_storage:
14777 return rb_imemo_once_storage_new();
14778 case imemo_cdhash: {
14779 long len = (long)ibf_load_small_value(load, &offset);
14782 for (
long i = 0; i <
len; i++) {
14783 VALUE key_index = ibf_load_small_value(load, &offset);
14784 VALUE val = ibf_load_small_value(load, &offset);
14786 VALUE key = ibf_load_object(load, key_index);
14787 cdhash_aset(obj, key, val);
14793 return ibf_load_object_unsupported(load, header, offset);
14805 range.class_index = 0;
14808 range.beg = (long)ibf_dump_object(dump, beg);
14809 range.end = (long)ibf_dump_object(dump, end);
14815 rb_raise(
rb_eNotImpError,
"ibf_dump_object_struct: unsupported class %"PRIsVALUE,
14824 VALUE beg = ibf_load_object(load, range->beg);
14825 VALUE end = ibf_load_object(load, range->end);
14835 ssize_t
len = BIGNUM_LEN(obj);
14836 ssize_t slen = BIGNUM_SIGN(obj) > 0 ?
len :
len * -1;
14837 BDIGIT *d = BIGNUM_DIGITS(obj);
14839 (void)IBF_W(&slen, ssize_t, 1);
14840 IBF_WP(d, BDIGIT,
len);
14847 int sign = bignum->slen > 0;
14848 ssize_t
len = sign > 0 ? bignum->slen : -1 * bignum->slen;
14849 const int big_unpack_flags =
14852 VALUE obj = rb_integer_unpack(bignum->digits,
len,
sizeof(BDIGIT), 0,
14863 if (rb_data_is_encoding(obj)) {
14864 rb_encoding *enc = rb_to_encoding(obj);
14865 const char *name = rb_enc_name(enc);
14866 long len = strlen(name) + 1;
14868 data[0] = IBF_OBJECT_DATA_ENCODING;
14870 (void)IBF_W(data,
long, 2);
14871 IBF_WP(name,
char,
len);
14874 ibf_dump_object_unsupported(dump, obj);
14881 const long *body = IBF_OBJBODY(
long, offset);
14882 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
14884 const char *data = (
const char *)&body[2];
14887 case IBF_OBJECT_DATA_ENCODING:
14889 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
14894 return ibf_load_object_unsupported(load, header, offset);
14898ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
14901 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
14902 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
14904 (void)IBF_W(data,
long, 2);
14908ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
14911 VALUE a = ibf_load_object(load, nums->a);
14912 VALUE b = ibf_load_object(load, nums->b);
14924 ibf_dump_object_string(dump,
rb_sym2str(obj));
14930 ibf_offset_t reading_pos = offset;
14932 int encindex = (int)ibf_load_small_value(load, &reading_pos);
14934 const char *ptr = load->current_buffer->buff + reading_pos;
14936 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
14937 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
14938 encindex = rb_enc_find_index(RSTRING_PTR(enc_name_str));
14941 ID id = rb_intern3(ptr,
len, rb_enc_from_index(encindex));
14945typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
14946static const ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
14947 ibf_dump_object_unsupported,
14948 ibf_dump_object_unsupported,
14949 ibf_dump_object_class,
14950 ibf_dump_object_unsupported,
14951 ibf_dump_object_float,
14952 ibf_dump_object_string,
14953 ibf_dump_object_regexp,
14954 ibf_dump_object_array,
14955 ibf_dump_object_hash,
14956 ibf_dump_object_struct,
14957 ibf_dump_object_bignum,
14958 ibf_dump_object_unsupported,
14959 ibf_dump_object_data,
14960 ibf_dump_object_unsupported,
14961 ibf_dump_object_complex_rational,
14962 ibf_dump_object_complex_rational,
14963 ibf_dump_object_unsupported,
14964 ibf_dump_object_unsupported,
14965 ibf_dump_object_unsupported,
14966 ibf_dump_object_unsupported,
14967 ibf_dump_object_symbol,
14968 ibf_dump_object_unsupported,
14969 ibf_dump_object_unsupported,
14970 ibf_dump_object_unsupported,
14971 ibf_dump_object_unsupported,
14972 ibf_dump_object_unsupported,
14973 ibf_dump_object_imemo,
14974 ibf_dump_object_unsupported,
14975 ibf_dump_object_unsupported,
14976 ibf_dump_object_unsupported,
14977 ibf_dump_object_unsupported,
14978 ibf_dump_object_unsupported,
14984 unsigned char byte =
14985 (header.type << 0) |
14986 (header.special_const << 5) |
14987 (header.frozen << 6) |
14988 (header.internal << 7);
14994ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
14996 unsigned char byte = ibf_load_byte(load, offset);
14999 header.type = (
byte >> 0) & 0x1f;
15000 header.special_const = (
byte >> 5) & 0x01;
15001 header.frozen = (
byte >> 6) & 0x01;
15002 header.internal = (
byte >> 7) & 0x01;
15011 ibf_offset_t current_offset;
15012 IBF_ZERO(obj_header);
15013 obj_header.type =
TYPE(obj);
15015 IBF_W_ALIGN(ibf_offset_t);
15016 current_offset = ibf_dump_pos(dump);
15021 obj_header.special_const = TRUE;
15022 obj_header.frozen = TRUE;
15023 obj_header.internal = TRUE;
15024 ibf_dump_object_object_header(dump, obj_header);
15025 ibf_dump_write_small_value(dump, obj);
15029 obj_header.special_const = FALSE;
15030 obj_header.frozen =
OBJ_FROZEN(obj) ? TRUE : FALSE;
15031 ibf_dump_object_object_header(dump, obj_header);
15032 (*dump_object_functions[obj_header.type])(dump, obj);
15035 return current_offset;
15039static const ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
15040 ibf_load_object_unsupported,
15041 ibf_load_object_unsupported,
15042 ibf_load_object_class,
15043 ibf_load_object_unsupported,
15044 ibf_load_object_float,
15045 ibf_load_object_string,
15046 ibf_load_object_regexp,
15047 ibf_load_object_array,
15048 ibf_load_object_hash,
15049 ibf_load_object_struct,
15050 ibf_load_object_bignum,
15051 ibf_load_object_unsupported,
15052 ibf_load_object_data,
15053 ibf_load_object_unsupported,
15054 ibf_load_object_complex_rational,
15055 ibf_load_object_complex_rational,
15056 ibf_load_object_unsupported,
15057 ibf_load_object_unsupported,
15058 ibf_load_object_unsupported,
15059 ibf_load_object_unsupported,
15060 ibf_load_object_symbol,
15061 ibf_load_object_unsupported,
15062 ibf_load_object_unsupported,
15063 ibf_load_object_unsupported,
15064 ibf_load_object_unsupported,
15065 ibf_load_object_unsupported,
15066 ibf_load_object_imemo,
15067 ibf_load_object_unsupported,
15068 ibf_load_object_unsupported,
15069 ibf_load_object_unsupported,
15070 ibf_load_object_unsupported,
15071 ibf_load_object_unsupported,
15075ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
15077 if (object_index == 0) {
15081 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
15083 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
15084 ibf_offset_t offset = offsets[object_index];
15085 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
15088 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
15089 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
15090 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
15091 header.type, header.special_const, header.frozen, header.internal);
15093 if (offset >= load->current_buffer->size) {
15094 rb_raise(
rb_eIndexError,
"object offset out of range: %u", offset);
15097 if (header.special_const) {
15098 ibf_offset_t reading_pos = offset;
15100 obj = ibf_load_small_value(load, &reading_pos);
15103 obj = (*load_object_functions[header.type])(load, &header, offset);
15106 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
15109 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
15110 object_index, obj);
15123ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
15128 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
15131 return ST_CONTINUE;
15135ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
15137 st_table *obj_table = dump->current_buffer->obj_table;
15142 args.offset_list = offset_list;
15144 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
15146 IBF_W_ALIGN(ibf_offset_t);
15147 *obj_list_offset = ibf_dump_pos(dump);
15149 st_index_t size = obj_table->num_entries;
15152 for (i=0; i<size; i++) {
15157 *obj_list_size = (
unsigned int)size;
15161ibf_dump_mark(
void *ptr)
15164 rb_gc_mark(dump->global_buffer.str);
15166 rb_mark_set(dump->global_buffer.obj_table);
15167 rb_mark_set(dump->iseq_table);
15171ibf_dump_free(
void *ptr)
15174 if (dump->global_buffer.obj_table) {
15175 st_free_table(dump->global_buffer.obj_table);
15176 dump->global_buffer.obj_table = 0;
15178 if (dump->iseq_table) {
15179 st_free_table(dump->iseq_table);
15180 dump->iseq_table = 0;
15185ibf_dump_memsize(
const void *ptr)
15189 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
15190 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
15196 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
15197 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
15203 dump->global_buffer.obj_table = NULL;
15204 dump->iseq_table = NULL;
15207 dump->global_buffer.obj_table = ibf_dump_object_table_new();
15208 dump->iseq_table = st_init_numtable();
15210 dump->current_buffer = &dump->global_buffer;
15214iseq_ibf_dump_0(
struct ibf_dump *dump,
const rb_iseq_t *iseq,
VALUE opt)
15218 ibf_dump_write(dump, &header,
sizeof(header));
15219 ibf_dump_iseq(dump, iseq);
15221 header.magic[0] =
'Y';
15222 header.magic[1] =
'A';
15223 header.magic[2] =
'R';
15224 header.magic[3] =
'B';
15225 header.major_version = IBF_MAJOR_VERSION;
15226 header.minor_version = IBF_MINOR_VERSION;
15227 header.endian = IBF_ENDIAN_MARK;
15229 ibf_dump_iseq_list(dump, &header);
15230 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
15231 header.size = ibf_dump_pos(dump);
15234 VALUE opt_str = opt;
15237 ibf_dump_write(dump, ptr, header.extra_size);
15240 header.extra_size = 0;
15243 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
15245 return dump->global_buffer.str;
15249rb_iseq_ibf_dump(
const rb_iseq_t *iseq,
VALUE opt)
15255 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
15256 ISEQ_BODY(iseq)->local_iseq != iseq) {
15259 if (
RTEST(ISEQ_COVERAGE(iseq))) {
15264 ibf_dump_setup(dump, dump_obj);
15266 str = iseq_ibf_dump_0(dump, iseq, opt);
15271static const ibf_offset_t *
15272ibf_iseq_list(
const struct ibf_load *load)
15274 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
15278rb_ibf_load_iseq_complete(rb_iseq_t *iseq)
15281 rb_iseq_t *prev_src_iseq = load->iseq;
15282 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
15285 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
15286 iseq->aux.loader.index, offset,
15287 load->header->size);
15289 ibf_load_iseq_each(load, iseq, offset);
15290 ISEQ_COMPILE_DATA_CLEAR(iseq);
15292 rb_iseq_init_trace(iseq);
15293 load->iseq = prev_src_iseq;
15298rb_iseq_complete(
const rb_iseq_t *iseq)
15300 rb_ibf_load_iseq_complete((rb_iseq_t *)iseq);
15306ibf_load_iseq(
const struct ibf_load *load,
const rb_iseq_t *index_iseq)
15308 int iseq_index = (int)(
VALUE)index_iseq;
15311 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
15312 (
void *)index_iseq, (
void *)load->iseq_list);
15314 if (iseq_index == -1) {
15318 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
15321 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
15324 return (rb_iseq_t *)iseqv;
15327 rb_iseq_t *iseq = iseq_imemo_alloc();
15329 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
15333 iseq->aux.loader.index = iseq_index;
15335 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
15336 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
15338 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
15340 if (!USE_LAZY_LOAD || GET_VM()->builtin_function_table) {
15342 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
15344 rb_ibf_load_iseq_complete(iseq);
15348 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
15349 (
void *)iseq, (
void *)load->iseq);
15357ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
15360 load->loader_obj = loader_obj;
15361 load->global_buffer.buff = bytes;
15362 load->header = header;
15363 load->global_buffer.size = header->size;
15364 load->global_buffer.obj_list_offset = header->global_object_list_offset;
15365 load->global_buffer.obj_list_size = header->global_object_list_size;
15366 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(header->iseq_list_size));
15367 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
15370 load->current_buffer = &load->global_buffer;
15372 if (size < header->size) {
15375 if (strncmp(header->magic,
"YARB", 4) != 0) {
15378 if (header->major_version != IBF_MAJOR_VERSION ||
15379 header->minor_version != IBF_MINOR_VERSION) {
15381 header->major_version, header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
15383 if (header->endian != IBF_ENDIAN_MARK) {
15389 if (header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
15390 rb_raise(rb_eArgError,
"unaligned iseq list offset: %u",
15391 header->iseq_list_offset);
15393 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
15394 rb_raise(rb_eArgError,
"unaligned object list offset: %u",
15395 load->global_buffer.obj_list_offset);
15408 if (USE_LAZY_LOAD) {
15409 str =
rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
15412 ibf_load_setup_bytes(load, loader_obj, RSTRING_PTR(str), RSTRING_LEN(str));
15417ibf_loader_mark(
void *ptr)
15420 rb_gc_mark(load->str);
15421 rb_gc_mark(load->path_override);
15422 rb_gc_mark(load->realpath_override);
15423 rb_gc_mark(load->iseq_list);
15424 rb_gc_mark(load->global_buffer.obj_list);
15428ibf_loader_free(
void *ptr)
15435ibf_loader_memsize(
const void *ptr)
15442 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
15443 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE
15449 if (
NIL_P(path))
return;
15450 if (
NIL_P(realpath)) realpath = path;
15451 RB_OBJ_WRITE(load->loader_obj, &load->path_override, rb_fstring(path));
15452 RB_OBJ_WRITE(load->loader_obj, &load->realpath_override, rb_fstring(realpath));
15462 ibf_load_setup(load, loader_obj, str);
15463 ibf_load_set_path(load, fname, path);
15464 iseq = ibf_load_iseq(load, 0);
15471rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size,
VALUE fname,
VALUE path)
15477 ibf_load_setup_bytes(load, loader_obj, bytes, size);
15478 ibf_load_set_path(load, fname, path);
15479 iseq = ibf_load_iseq(load, 0);
15487ibf_dump_iseq_table_collect_i(st_data_t key, st_data_t val, st_data_t arg)
15489 const rb_iseq_t **srcs = (
const rb_iseq_t **)arg;
15490 srcs[(int)val] = (
const rb_iseq_t *)key;
15491 return ST_CONTINUE;
15497iseq_subtree_copy_pc2branchindex(
const struct ibf_dump *dump,
const struct ibf_load *load)
15499 unsigned int iseq_count = (
unsigned int)dump->iseq_table->num_entries;
15501 const rb_iseq_t **srcs =
ALLOCV_N(
const rb_iseq_t *, srcs_buf, iseq_count);
15502 st_foreach(dump->iseq_table, ibf_dump_iseq_table_collect_i, (st_data_t)srcs);
15504 for (
unsigned int i = 0; i < iseq_count; i++) {
15505 rb_iseq_t *copy = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)i);
15506 VALUE pc2branchindex = ISEQ_PC2BRANCHINDEX(srcs[i]);
15507 if (pc2branchindex !=
Qnil) {
15508 ISEQ_PC2BRANCHINDEX_SET(copy, pc2branchindex);
15517rb_iseq_dup_with_independent_caches(
const rb_iseq_t *src_root)
15525 ibf_dump_setup(dump, dump_obj);
15527 str = iseq_ibf_dump_0(dump, src_root,
Qnil);
15532 ibf_load_setup(load, loader_obj, str);
15540 unsigned int iseq_count = (
unsigned int)dump->iseq_table->num_entries;
15541 const rb_iseq_t *result = NULL;
15542 for (
unsigned int i = 0; i < iseq_count; i++) {
15543 rb_iseq_t *copy = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)i);
15546 rb_ibf_load_iseq_complete(copy);
15552 if (!cb->local_iseq)
RB_OBJ_WRITE(copy, &cb->local_iseq, sb->local_iseq);
15553 RB_OBJ_WRITE(copy, &cb->location.pathobj, sb->location.pathobj);
15554 cb->no_realpath = sb->no_realpath;
15555 VALUE sl = ISEQ_SCRIPT_LINES(src_root);
15556 VALUE cov = ISEQ_COVERAGE(src_root);
15564 RB_OBJ_WRITE(copy, &cb->parent_iseq, sb->parent_iseq);
15573 if (ISEQ_PC2BRANCHINDEX(src_root) !=
Qnil) {
15574 iseq_subtree_copy_pc2branchindex(dump, load);
15583rb_iseq_ibf_load_extra_data(
VALUE str)
15589 ibf_load_setup(load, loader_obj, str);
15590 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
15595#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.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define NUM2ULONG
Old name of RB_NUM2ULONG.
#define NUM2LL
Old name of RB_NUM2LL.
#define ALLOCV
Old name of RB_ALLOCV.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define T_NIL
Old name of RUBY_T_NIL.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define FIX2UINT
Old name of RB_FIX2UINT.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define FIXABLE
Old name of RB_FIXABLE.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_SET
Old name of RB_FL_SET.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define NUM2ULL
Old name of RB_NUM2ULL.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
VALUE rb_eNotImpError
NotImplementedError exception.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_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_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
int len
Length of the buffer.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
static VALUE RB_OBJ_SET_FROZEN_SHAREABLE(VALUE obj)
Freezes and marks the object as shareable.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
#define rb_long2int
Just another name of rb_long2int_inline.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define ALLOCA_N(type, n)
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RB_ALLOCV(v, n)
Identical to RB_ALLOCV_N(), except that it allocates a number of bytes and returns a void* .
VALUE type(ANYARGS)
ANYARGS-ed function type.
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.