12#include "ruby/internal/config.h"
23#include "internal/array.h"
24#include "internal/compile.h"
25#include "internal/complex.h"
26#include "internal/encoding.h"
27#include "internal/error.h"
28#include "internal/hash.h"
29#include "internal/numeric.h"
30#include "internal/object.h"
31#include "internal/rational.h"
32#include "internal/re.h"
33#include "internal/symbol.h"
34#include "internal/thread.h"
35#include "internal/variable.h"
40#include "vm_callinfo.h"
45#include "insns_info.inc"
47#undef RUBY_UNTYPED_DATA_WARNING
48#define RUBY_UNTYPED_DATA_WARNING 0
50#define FIXNUM_INC(n, i) ((n)+(INT2FIX(i)&~FIXNUM_FLAG))
51#define FIXNUM_OR(n, i) ((n)|INT2FIX(i))
85 unsigned int rescued: 2;
86 unsigned int unremovable: 1;
91 enum ruby_vminsn_type insn_id;
121 const NODE *ensure_node;
126const ID rb_iseq_shared_exc_local_tbl[] = {idERROR_INFO};
146#define compile_debug CPDEBUG
148#define compile_debug ISEQ_COMPILE_DATA(iseq)->option->debug_level
153#define compile_debug_print_indent(level) \
154 ruby_debug_print_indent((level), compile_debug, gl_node_level * 2)
156#define debugp(header, value) (void) \
157 (compile_debug_print_indent(1) && \
158 ruby_debug_print_value(1, compile_debug, (header), (value)))
160#define debugi(header, id) (void) \
161 (compile_debug_print_indent(1) && \
162 ruby_debug_print_id(1, compile_debug, (header), (id)))
164#define debugp_param(header, value) (void) \
165 (compile_debug_print_indent(1) && \
166 ruby_debug_print_value(1, compile_debug, (header), (value)))
168#define debugp_verbose(header, value) (void) \
169 (compile_debug_print_indent(2) && \
170 ruby_debug_print_value(2, compile_debug, (header), (value)))
172#define debugp_verbose_node(header, value) (void) \
173 (compile_debug_print_indent(10) && \
174 ruby_debug_print_value(10, compile_debug, (header), (value)))
176#define debug_node_start(node) ((void) \
177 (compile_debug_print_indent(1) && \
178 (ruby_debug_print_node(1, CPDEBUG, "", (const NODE *)(node)), gl_node_level)), \
181#define debug_node_end() gl_node_level --
185#define debugi(header, id) ((void)0)
186#define debugp(header, value) ((void)0)
187#define debugp_verbose(header, value) ((void)0)
188#define debugp_verbose_node(header, value) ((void)0)
189#define debugp_param(header, value) ((void)0)
190#define debug_node_start(node) ((void)0)
191#define debug_node_end() ((void)0)
194#if CPDEBUG > 1 || CPDEBUG < 0
196#define printf ruby_debug_printf
197#define debugs if (compile_debug_print_indent(1)) ruby_debug_printf
198#define debug_compile(msg, v) ((void)(compile_debug_print_indent(1) && fputs((msg), stderr)), (v))
200#define debugs if(0)printf
201#define debug_compile(msg, v) (v)
204#define LVAR_ERRINFO (1)
207#define NEW_LABEL(l) new_label_body(iseq, (l))
208#define LABEL_FORMAT "<L%03d>"
210#define NEW_ISEQ(node, name, type, line_no) \
211 new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
213#define NEW_CHILD_ISEQ(node, name, type, line_no) \
214 new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
217#define ADD_SEQ(seq1, seq2) \
218 APPEND_LIST((seq1), (seq2))
221#define ADD_INSN(seq, line_node, insn) \
222 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (line_node), BIN(insn), 0))
225#define INSERT_BEFORE_INSN(next, line_node, insn) \
226 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) new_insn_body(iseq, (line_node), BIN(insn), 0))
229#define INSERT_AFTER_INSN(prev, line_node, insn) \
230 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) new_insn_body(iseq, (line_node), BIN(insn), 0))
233#define ADD_INSN1(seq, line_node, insn, op1) \
234 ADD_ELEM((seq), (LINK_ELEMENT *) \
235 new_insn_body(iseq, (line_node), BIN(insn), 1, (VALUE)(op1)))
238#define INSERT_BEFORE_INSN1(next, line_node, insn, op1) \
239 ELEM_INSERT_PREV(&(next)->link, (LINK_ELEMENT *) \
240 new_insn_body(iseq, (line_node), BIN(insn), 1, (VALUE)(op1)))
243#define INSERT_AFTER_INSN1(prev, line_node, insn, op1) \
244 ELEM_INSERT_NEXT(&(prev)->link, (LINK_ELEMENT *) \
245 new_insn_body(iseq, (line_node), BIN(insn), 1, (VALUE)(op1)))
247#define LABEL_REF(label) ((label)->refcnt++)
250#define ADD_INSNL(seq, line_node, insn, label) (ADD_INSN1(seq, line_node, insn, label), LABEL_REF(label))
252#define ADD_INSN2(seq, line_node, insn, op1, op2) \
253 ADD_ELEM((seq), (LINK_ELEMENT *) \
254 new_insn_body(iseq, (line_node), BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
256#define ADD_INSN3(seq, line_node, insn, op1, op2, op3) \
257 ADD_ELEM((seq), (LINK_ELEMENT *) \
258 new_insn_body(iseq, (line_node), BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
261#define ADD_SEND(seq, line_node, id, argc) \
262 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
264#define ADD_SEND_WITH_FLAG(seq, line_node, id, argc, flag) \
265 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)(flag), NULL)
267#define ADD_SEND_WITH_BLOCK(seq, line_node, id, argc, block) \
268 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
270#define ADD_CALL_RECEIVER(seq, line_node) \
271 ADD_INSN((seq), (line_node), putself)
273#define ADD_CALL(seq, line_node, id, argc) \
274 ADD_SEND_R((seq), (line_node), (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
276#define ADD_CALL_WITH_BLOCK(seq, line_node, id, argc, block) \
277 ADD_SEND_R((seq), (line_node), (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
279#define ADD_SEND_R(seq, line_node, id, argc, block, flag, keywords) \
280 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (line_node), (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
282#define ADD_TRACE(seq, event) \
283 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), 0))
284#define ADD_TRACE_WITH_DATA(seq, event, data) \
285 ADD_ELEM((seq), (LINK_ELEMENT *)new_trace_body(iseq, (event), (data)))
287static void iseq_add_getlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
288static void iseq_add_setlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
const NODE *
const line_node,
int idx,
int level);
290#define ADD_GETLOCAL(seq, line_node, idx, level) iseq_add_getlocal(iseq, (seq), (line_node), (idx), (level))
291#define ADD_SETLOCAL(seq, line_node, idx, level) iseq_add_setlocal(iseq, (seq), (line_node), (idx), (level))
294#define ADD_LABEL(seq, label) \
295 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
297#define APPEND_LABEL(seq, before, label) \
298 APPEND_ELEM((seq), (before), (LINK_ELEMENT *) (label))
300#define ADD_ADJUST(seq, line_node, label) \
301 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), nd_line(line_node)))
303#define ADD_ADJUST_RESTORE(seq, label) \
304 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
306#define LABEL_UNREMOVABLE(label) \
307 ((label) ? (LABEL_REF(label), (label)->unremovable=1) : 0)
308#define ADD_CATCH_ENTRY(type, ls, le, iseqv, lc) do { \
309 VALUE _e = rb_ary_new3(5, (type), \
310 (VALUE)(ls) | 1, (VALUE)(le) | 1, \
311 (VALUE)(iseqv), (VALUE)(lc) | 1); \
312 LABEL_UNREMOVABLE(ls); \
315 if (NIL_P(ISEQ_COMPILE_DATA(iseq)->catch_table_ary)) \
316 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, rb_ary_hidden_new(3)); \
317 rb_ary_push(ISEQ_COMPILE_DATA(iseq)->catch_table_ary, freeze_hide_obj(_e)); \
321#define COMPILE(anchor, desc, node) \
322 (debug_compile("== " desc "\n", \
323 iseq_compile_each(iseq, (anchor), (node), 0)))
326#define COMPILE_POPPED(anchor, desc, node) \
327 (debug_compile("== " desc "\n", \
328 iseq_compile_each(iseq, (anchor), (node), 1)))
331#define COMPILE_(anchor, desc, node, popped) \
332 (debug_compile("== " desc "\n", \
333 iseq_compile_each(iseq, (anchor), (node), (popped))))
335#define COMPILE_RECV(anchor, desc, node) \
336 (private_recv_p(node) ? \
337 (ADD_INSN(anchor, node, putself), VM_CALL_FCALL) : \
338 COMPILE(anchor, desc, node->nd_recv) ? 0 : -1)
340#define OPERAND_AT(insn, idx) \
341 (((INSN*)(insn))->operands[(idx)])
343#define INSN_OF(insn) \
344 (((INSN*)(insn))->insn_id)
346#define IS_INSN(link) ((link)->type == ISEQ_ELEMENT_INSN)
347#define IS_LABEL(link) ((link)->type == ISEQ_ELEMENT_LABEL)
348#define IS_ADJUST(link) ((link)->type == ISEQ_ELEMENT_ADJUST)
349#define IS_TRACE(link) ((link)->type == ISEQ_ELEMENT_TRACE)
350#define IS_INSN_ID(iobj, insn) (INSN_OF(iobj) == BIN(insn))
351#define IS_NEXT_INSN_ID(link, insn) \
352 ((link)->next && IS_INSN((link)->next) && IS_INSN_ID((link)->next, insn))
360append_compile_error(const
rb_iseq_t *iseq,
int line, const
char *fmt, ...)
362 VALUE err_info = ISEQ_COMPILE_DATA(iseq)->err_info;
363 VALUE file = rb_iseq_path(iseq);
368 err = rb_syntax_error_append(err, file, line, -1, NULL, fmt, args);
370 if (
NIL_P(err_info)) {
371 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err);
374 else if (!err_info) {
385compile_bug(
rb_iseq_t *iseq,
int line,
const char *fmt, ...)
389 rb_report_bug_valist(rb_iseq_path(iseq), line, fmt, args);
395#define COMPILE_ERROR append_compile_error
397#define ERROR_ARGS_AT(n) iseq, nd_line(n),
398#define ERROR_ARGS ERROR_ARGS_AT(node)
400#define EXPECT_NODE(prefix, node, ndtype, errval) \
402 const NODE *error_node = (node); \
403 enum node_type error_type = nd_type(error_node); \
404 if (error_type != (ndtype)) { \
405 COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
406 prefix ": " #ndtype " is expected, but %s", \
407 ruby_node_name(error_type)); \
412#define EXPECT_NODE_NONULL(prefix, parent, ndtype, errval) \
414 COMPILE_ERROR(ERROR_ARGS_AT(parent) \
415 prefix ": must be " #ndtype ", but 0"); \
419#define UNKNOWN_NODE(prefix, node, errval) \
421 const NODE *error_node = (node); \
422 COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
423 ruby_node_name(nd_type(error_node))); \
430#define CHECK(sub) if (!(sub)) {BEFORE_RETURN;return COMPILE_NG;}
431#define NO_CHECK(sub) (void)(sub)
436#define DECL_ANCHOR(name) \
437 LINK_ANCHOR name[1] = {{{ISEQ_ELEMENT_ANCHOR,},}}
438#define INIT_ANCHOR(name) \
439 (name->last = &name->anchor)
442freeze_hide_obj(
VALUE obj)
445 RBASIC_CLEAR_CLASS(obj);
449#include "optinsn.inc"
450#if OPT_INSTRUCTIONS_UNIFICATION
451#include "optunifs.inc"
456#define ISEQ_ARG iseq,
457#define ISEQ_ARG_DECLARE rb_iseq_t *iseq,
460#define ISEQ_ARG_DECLARE
464#define gl_node_level ISEQ_COMPILE_DATA(iseq)->node_level
470static int insn_data_length(
INSN *iobj);
471static int calc_sp_depth(
int depth,
INSN *iobj);
473static INSN *new_insn_body(
rb_iseq_t *iseq,
const NODE *
const line_node,
enum ruby_vminsn_type insn_id,
int argc, ...);
486static int iseq_set_exception_local_table(
rb_iseq_t *iseq);
491static int iseq_set_exception_table(
rb_iseq_t *iseq);
492static int iseq_set_optargs_table(
rb_iseq_t *iseq);
495static int compile_hash(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *node,
int method_call_keywords,
int popped);
502verify_list(ISEQ_ARG_DECLARE
const char *info,
LINK_ANCHOR *
const anchor)
508 if (!compile_debug)
return;
510 list = anchor->anchor.next;
511 plist = &anchor->anchor;
513 if (plist != list->prev) {
520 if (anchor->last != plist && anchor->last != 0) {
525 rb_bug(
"list verify error: %08x (%s)", flag, info);
530#define verify_list(info, anchor) verify_list(iseq, (info), (anchor))
537 VALUE *original = rb_iseq_original_iseq(iseq);
539 while (i < ISEQ_BODY(iseq)->iseq_size) {
540 VALUE insn = original[i];
541 const char *types = insn_op_types(insn);
543 for (
int j=0; types[j]; j++) {
544 if (types[j] == TS_CALLDATA) {
548 if (cc != vm_cc_empty()) {
550 rb_bug(
"call cache is not initialized by vm_cc_empty()");
557 for (
unsigned int i=0; i<ISEQ_BODY(iseq)->ci_size; i++) {
558 struct rb_call_data *cd = &ISEQ_BODY(iseq)->call_data[i];
561 if (cc != NULL && cc != vm_cc_empty()) {
563 rb_bug(
"call cache is not initialized by vm_cc_empty()");
575 elem->prev = anchor->last;
576 anchor->last->next = elem;
578 verify_list(
"add", anchor);
588 elem->next = before->next;
589 elem->next->prev = elem;
591 if (before == anchor->last) anchor->last = elem;
592 verify_list(
"add", anchor);
595#define ADD_ELEM(anchor, elem) ADD_ELEM(iseq, (anchor), (elem))
596#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
600branch_coverage_valid_p(
rb_iseq_t *iseq,
int first_line)
602 if (!ISEQ_COVERAGE(iseq))
return 0;
603 if (!ISEQ_BRANCH_COVERAGE(iseq))
return 0;
604 if (first_line <= 0)
return 0;
611 const int first_lineno = nd_first_lineno(node), first_column = nd_first_column(node);
612 const int last_lineno = nd_last_lineno(node), last_column = nd_last_column(node);
614 if (!branch_coverage_valid_p(iseq, first_lineno))
return Qundef;
626 VALUE branch_base = rb_hash_aref(structure, key);
629 if (
NIL_P(branch_base)) {
630 branch_base = rb_ary_hidden_new(6);
631 rb_hash_aset(structure, key, branch_base);
633 rb_ary_push(branch_base,
INT2FIX(first_lineno));
634 rb_ary_push(branch_base,
INT2FIX(first_column));
635 rb_ary_push(branch_base,
INT2FIX(last_lineno));
636 rb_ary_push(branch_base,
INT2FIX(last_column));
637 branches = rb_hash_new();
639 rb_ary_push(branch_base, branches);
649generate_dummy_line_node(
int lineno,
int node_id)
652 nd_set_line(&dummy, lineno);
653 nd_set_node_id(&dummy, node_id);
660 const int first_lineno = nd_first_lineno(node), first_column = nd_first_column(node);
661 const int last_lineno = nd_last_lineno(node), last_column = nd_last_column(node);
663 if (!branch_coverage_valid_p(iseq, first_lineno))
return;
674 VALUE branch = rb_hash_aref(branches, key);
678 branch = rb_ary_hidden_new(6);
679 rb_hash_aset(branches, key, branch);
681 rb_ary_push(branch,
INT2FIX(first_lineno));
682 rb_ary_push(branch,
INT2FIX(first_column));
683 rb_ary_push(branch,
INT2FIX(last_lineno));
684 rb_ary_push(branch,
INT2FIX(last_column));
687 rb_ary_push(branch,
LONG2FIX(counter_idx));
688 rb_ary_push(counters,
INT2FIX(0));
694 ADD_TRACE_WITH_DATA(seq, RUBY_EVENT_COVERAGE_BRANCH, counter_idx);
696 NODE dummy_line_node = generate_dummy_line_node(last_lineno, nd_node_id(node));
697 ADD_INSN(seq, &dummy_line_node, nop);
700#define ISEQ_LAST_LINE(iseq) (ISEQ_COMPILE_DATA(iseq)->last_line)
703validate_label(st_data_t name, st_data_t label, st_data_t arg)
707 if (!lobj->link.next) {
709 COMPILE_ERROR(iseq, lobj->position,
710 "%"PRIsVALUE
": undefined label",
720 st_foreach(labels_table, validate_label, (st_data_t)iseq);
721 st_free_table(labels_table);
730 (*ifunc->func)(iseq, ret, ifunc->data);
732 NODE dummy_line_node = generate_dummy_line_node(ISEQ_COMPILE_DATA(iseq)->last_line, -1);
733 ADD_INSN(ret, &dummy_line_node, leave);
735 CHECK(iseq_setup_insn(iseq, ret));
736 return iseq_setup(iseq, ret);
745 if (IMEMO_TYPE_P(node, imemo_ifunc)) {
750 NO_CHECK(COMPILE(ret,
"nil", node));
751 iseq_set_local_table(iseq, 0);
754 else if (nd_type_p(node, NODE_SCOPE)) {
756 iseq_set_local_table(iseq, node->nd_tbl);
757 iseq_set_arguments(iseq, ret, node->nd_args);
759 switch (ISEQ_BODY(iseq)->
type) {
760 case ISEQ_TYPE_BLOCK:
762 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
763 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
765 start->rescued = LABEL_RESCUE_BEG;
766 end->rescued = LABEL_RESCUE_END;
769 NODE dummy_line_node = generate_dummy_line_node(ISEQ_BODY(iseq)->location.first_lineno, -1);
770 ADD_INSN (ret, &dummy_line_node, nop);
771 ADD_LABEL(ret, start);
772 CHECK(COMPILE(ret,
"block body", node->nd_body));
775 ISEQ_COMPILE_DATA(iseq)->last_line = ISEQ_BODY(iseq)->location.code_location.end_pos.lineno;
778 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
779 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
782 case ISEQ_TYPE_CLASS:
785 CHECK(COMPILE(ret,
"scoped node", node->nd_body));
787 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
790 case ISEQ_TYPE_METHOD:
792 ISEQ_COMPILE_DATA(iseq)->root_node = node->nd_body;
794 CHECK(COMPILE(ret,
"scoped node", node->nd_body));
795 ISEQ_COMPILE_DATA(iseq)->root_node = node->nd_body;
797 ISEQ_COMPILE_DATA(iseq)->last_line = nd_line(node);
801 CHECK(COMPILE(ret,
"scoped node", node->nd_body));
808#define INVALID_ISEQ_TYPE(type) \
809 ISEQ_TYPE_##type: m = #type; goto invalid_iseq_type
810 switch (ISEQ_BODY(iseq)->
type) {
811 case INVALID_ISEQ_TYPE(
METHOD);
812 case INVALID_ISEQ_TYPE(CLASS);
813 case INVALID_ISEQ_TYPE(BLOCK);
814 case INVALID_ISEQ_TYPE(EVAL);
815 case INVALID_ISEQ_TYPE(MAIN);
816 case INVALID_ISEQ_TYPE(TOP);
817#undef INVALID_ISEQ_TYPE
818 case ISEQ_TYPE_RESCUE:
819 iseq_set_exception_local_table(iseq);
820 CHECK(COMPILE(ret,
"rescue", node));
822 case ISEQ_TYPE_ENSURE:
823 iseq_set_exception_local_table(iseq);
824 CHECK(COMPILE_POPPED(ret,
"ensure", node));
826 case ISEQ_TYPE_PLAIN:
827 CHECK(COMPILE(ret,
"ensure", node));
830 COMPILE_ERROR(ERROR_ARGS
"unknown scope: %d", ISEQ_BODY(iseq)->
type);
833 COMPILE_ERROR(ERROR_ARGS
"compile/ISEQ_TYPE_%s should not be reached", m);
838 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE || ISEQ_BODY(iseq)->
type == ISEQ_TYPE_ENSURE) {
839 NODE dummy_line_node = generate_dummy_line_node(0, -1);
840 ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
841 ADD_INSN1(ret, &dummy_line_node,
throw,
INT2FIX(0) );
844 NODE dummy_line_node = generate_dummy_line_node(ISEQ_COMPILE_DATA(iseq)->last_line, -1);
845 ADD_INSN(ret, &dummy_line_node, leave);
849 if (ISEQ_COMPILE_DATA(iseq)->labels_table) {
850 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
851 ISEQ_COMPILE_DATA(iseq)->labels_table = 0;
852 validate_labels(iseq, labels_table);
855 CHECK(iseq_setup_insn(iseq, ret));
856 return iseq_setup(iseq, ret);
860rb_iseq_translate_threaded_code(
rb_iseq_t *iseq)
862#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
863 const void *
const *table = rb_vm_get_insns_address_table();
865 VALUE *encoded = (
VALUE *)ISEQ_BODY(iseq)->iseq_encoded;
867 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
868 int insn = (int)ISEQ_BODY(iseq)->iseq_encoded[i];
869 int len = insn_len(insn);
870 encoded[i] = (
VALUE)table[insn];
879rb_iseq_original_iseq(
const rb_iseq_t *iseq)
881 VALUE *original_code;
883 if (ISEQ_ORIGINAL_ISEQ(iseq))
return ISEQ_ORIGINAL_ISEQ(iseq);
884 original_code = ISEQ_ORIGINAL_ISEQ_ALLOC(iseq, ISEQ_BODY(iseq)->iseq_size);
885 MEMCPY(original_code, ISEQ_BODY(iseq)->iseq_encoded,
VALUE, ISEQ_BODY(iseq)->iseq_size);
887#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
891 for (i = 0; i < ISEQ_BODY(iseq)->iseq_size; ) {
892 const void *addr = (
const void *)original_code[i];
893 const int insn = rb_vm_insn_addr2insn(addr);
895 original_code[i] = insn;
900 return original_code;
913#if defined(__sparc) && SIZEOF_VOIDP == 4 && defined(__GNUC__)
914 #define STRICT_ALIGNMENT
920#if defined(__OpenBSD__)
921 #include <sys/endian.h>
922 #ifdef __STRICT_ALIGNMENT
923 #define STRICT_ALIGNMENT
927#ifdef STRICT_ALIGNMENT
928 #if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE
929 #define ALIGNMENT_SIZE SIZEOF_LONG_LONG
931 #define ALIGNMENT_SIZE SIZEOF_VALUE
933 #define PADDING_SIZE_MAX ((size_t)((ALIGNMENT_SIZE) - 1))
934 #define ALIGNMENT_SIZE_MASK PADDING_SIZE_MAX
937 #define PADDING_SIZE_MAX 0
940#ifdef STRICT_ALIGNMENT
943calc_padding(
void *ptr,
size_t size)
948 mis = (size_t)ptr & ALIGNMENT_SIZE_MASK;
950 padding = ALIGNMENT_SIZE - mis;
956#if ALIGNMENT_SIZE > SIZEOF_VALUE
957 if (size ==
sizeof(
VALUE) && padding ==
sizeof(
VALUE)) {
971#ifdef STRICT_ALIGNMENT
972 size_t padding = calc_padding((
void *)&storage->buff[storage->pos], size);
974 const size_t padding = 0;
977 if (size >= INT_MAX - padding) rb_memerror();
978 if (storage->pos + size + padding > storage->size) {
979 unsigned int alloc_size = storage->size;
981 while (alloc_size < size + PADDING_SIZE_MAX) {
982 if (alloc_size >= INT_MAX / 2) rb_memerror();
985 storage->next = (
void *)
ALLOC_N(
char, alloc_size +
987 storage = *arena = storage->next;
990 storage->size = alloc_size;
991#ifdef STRICT_ALIGNMENT
992 padding = calc_padding((
void *)&storage->buff[storage->pos], size);
996#ifdef STRICT_ALIGNMENT
997 storage->pos += (int)padding;
1000 ptr = (
void *)&storage->buff[storage->pos];
1001 storage->pos += (int)size;
1006compile_data_alloc(
rb_iseq_t *iseq,
size_t size)
1009 return compile_data_alloc_with_arena(arena, size);
1013compile_data_alloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1016 return compile_data_alloc(iseq, size);
1020compile_data_calloc2(
rb_iseq_t *iseq,
size_t x,
size_t y)
1023 void *p = compile_data_alloc(iseq, size);
1032 return (
INSN *)compile_data_alloc_with_arena(arena,
sizeof(
INSN));
1036compile_data_alloc_label(
rb_iseq_t *iseq)
1038 return (
LABEL *)compile_data_alloc(iseq,
sizeof(
LABEL));
1042compile_data_alloc_adjust(
rb_iseq_t *iseq)
1044 return (
ADJUST *)compile_data_alloc(iseq,
sizeof(
ADJUST));
1048compile_data_alloc_trace(
rb_iseq_t *iseq)
1050 return (
TRACE *)compile_data_alloc(iseq,
sizeof(
TRACE));
1059 elem2->next = elem1->next;
1060 elem2->prev = elem1;
1061 elem1->next = elem2;
1063 elem2->next->prev = elem2;
1073 elem2->prev = elem1->prev;
1074 elem2->next = elem1;
1075 elem1->prev = elem2;
1077 elem2->prev->next = elem2;
1087 elem2->prev = elem1->prev;
1088 elem2->next = elem1->next;
1090 elem1->prev->next = elem2;
1093 elem1->next->prev = elem2;
1100 elem->prev->next = elem->next;
1102 elem->next->prev = elem->prev;
1109 return anchor->anchor.next;
1115 return anchor->last;
1122 switch (elem->type) {
1123 case ISEQ_ELEMENT_INSN:
1124 case ISEQ_ELEMENT_ADJUST:
1134LIST_INSN_SIZE_ONE(
const LINK_ANCHOR *
const anchor)
1136 LINK_ELEMENT *first_insn = ELEM_FIRST_INSN(FIRST_ELEMENT(anchor));
1137 if (first_insn != NULL &&
1138 ELEM_FIRST_INSN(first_insn->next) == NULL) {
1147LIST_INSN_SIZE_ZERO(
const LINK_ANCHOR *
const anchor)
1149 if (ELEM_FIRST_INSN(FIRST_ELEMENT(anchor)) == NULL) {
1167 if (anc2->anchor.next) {
1168 anc1->last->next = anc2->anchor.next;
1169 anc2->anchor.next->prev = anc1->last;
1170 anc1->last = anc2->last;
1172 verify_list(
"append", anc1);
1175#define APPEND_LIST(anc1, anc2) APPEND_LIST(iseq, (anc1), (anc2))
1184 printf(
"anch: %p, frst: %p, last: %p\n", (
void *)&anchor->anchor,
1185 (
void *)anchor->anchor.next, (
void *)anchor->last);
1187 printf(
"curr: %p, next: %p, prev: %p, type: %d\n", (
void *)list, (
void *)list->next,
1188 (
void *)list->prev, (
int)list->type);
1193 dump_disasm_list_with_cursor(anchor->anchor.next, cur, 0);
1194 verify_list(
"debug list", anchor);
1197#define debug_list(anc, cur) debug_list(iseq, (anc), (cur))
1200#define debug_list(anc, cur) ((void)0)
1206 TRACE *trace = compile_data_alloc_trace(iseq);
1208 trace->link.type = ISEQ_ELEMENT_TRACE;
1209 trace->link.next = NULL;
1210 trace->event = event;
1217new_label_body(
rb_iseq_t *iseq,
long line)
1219 LABEL *labelobj = compile_data_alloc_label(iseq);
1221 labelobj->link.type = ISEQ_ELEMENT_LABEL;
1222 labelobj->link.next = 0;
1224 labelobj->label_no = ISEQ_COMPILE_DATA(iseq)->label_no++;
1225 labelobj->sc_state = 0;
1227 labelobj->refcnt = 0;
1229 labelobj->rescued = LABEL_RESCUE_NONE;
1230 labelobj->unremovable = 0;
1237 ADJUST *adjust = compile_data_alloc_adjust(iseq);
1238 adjust->link.type = ISEQ_ELEMENT_ADJUST;
1239 adjust->link.next = 0;
1240 adjust->label = label;
1241 adjust->line_no = line;
1242 LABEL_UNREMOVABLE(label);
1249 const char *types = insn_op_types(insn->insn_id);
1250 for (
int j = 0; types[j]; j++) {
1251 char type = types[j];
1258 func(&OPERAND_AT(insn, j), data);
1267iseq_insn_each_object_write_barrier(
VALUE *obj_ptr,
VALUE iseq)
1274 int insn_id,
int argc,
VALUE *argv)
1276 INSN *iobj = compile_data_alloc_insn(iseq);
1280 iobj->link.type = ISEQ_ELEMENT_INSN;
1281 iobj->link.next = 0;
1282 iobj->insn_id = insn_id;
1283 iobj->insn_info.line_no = nd_line(line_node);
1284 iobj->insn_info.node_id = nd_node_id(line_node);
1285 iobj->insn_info.events = 0;
1286 iobj->operands = argv;
1287 iobj->operand_size = argc;
1290 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_write_barrier, (
VALUE)iseq);
1296new_insn_body(
rb_iseq_t *iseq,
const NODE *
const line_node,
enum ruby_vminsn_type insn_id,
int argc, ...)
1298 VALUE *operands = 0;
1302 va_start(argv, argc);
1303 operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
1304 for (i = 0; i < argc; i++) {
1310 return new_insn_core(iseq, line_node, insn_id, argc, operands);
1316 VM_ASSERT(argc >= 0);
1318 if (!(flag & (VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT)) &&
1319 kw_arg == NULL && !has_blockiseq) {
1320 flag |= VM_CALL_ARGS_SIMPLE;
1324 flag |= VM_CALL_KWARG;
1325 argc += kw_arg->keyword_len;
1328 ISEQ_BODY(iseq)->ci_size++;
1329 const struct rb_callinfo *ci = vm_ci_new(mid, flag, argc, kw_arg);
1337 VALUE *operands = compile_data_calloc2(iseq,
sizeof(
VALUE), 2);
1340 operands[1] = (
VALUE)blockiseq;
1344 INSN *insn = new_insn_core(iseq, line_node, BIN(send), 2, operands);
1358 ast.compile_option = 0;
1359 ast.script_lines = ISEQ_BODY(iseq)->variable.script_lines;
1361 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1362 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1363 ret_iseq = rb_iseq_new_with_opt(&ast, name,
1364 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1366 isolated_depth ? isolated_depth + 1 : 0,
1367 type, ISEQ_COMPILE_DATA(iseq)->option);
1368 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1378 debugs(
"[new_child_iseq_with_callback]> ---------------------------------------\n");
1379 ret_iseq = rb_iseq_new_with_callback(ifunc, name,
1380 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1381 line_no, parent,
type, ISEQ_COMPILE_DATA(iseq)->option);
1382 debugs(
"[new_child_iseq_with_callback]< ---------------------------------------\n");
1389 body->catch_except_p =
true;
1390 if (body->parent_iseq != NULL) {
1391 set_catch_except_p(ISEQ_BODY(body->parent_iseq));
1413 while (pos < body->iseq_size) {
1414 insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
1415 if (insn == BIN(
throw)) {
1416 set_catch_except_p(body);
1419 pos += insn_len(insn);
1425 for (i = 0; i < ct->size; i++) {
1427 UNALIGNED_MEMBER_PTR(ct, entries[i]);
1428 if (entry->type != CATCH_TYPE_BREAK
1429 && entry->type != CATCH_TYPE_NEXT
1430 && entry->type != CATCH_TYPE_REDO) {
1431 body->catch_except_p =
true;
1438iseq_insert_nop_between_end_and_cont(
rb_iseq_t *iseq)
1440 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
1441 if (
NIL_P(catch_table_ary))
return;
1442 unsigned int i, tlen = (
unsigned int)
RARRAY_LEN(catch_table_ary);
1444 for (i = 0; i < tlen; i++) {
1450 enum rb_catch_type ct = (
enum rb_catch_type)(ptr[0] & 0xffff);
1452 if (ct != CATCH_TYPE_BREAK
1453 && ct != CATCH_TYPE_NEXT
1454 && ct != CATCH_TYPE_REDO) {
1456 for (e = end; e && (IS_LABEL(e) || IS_TRACE(e)); e = e->next) {
1458 NODE dummy_line_node = generate_dummy_line_node(0, -1);
1459 INSN *nop = new_insn_core(iseq, &dummy_line_node, BIN(nop), 0, 0);
1460 ELEM_INSERT_NEXT(end, &nop->link);
1471 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1476 if (compile_debug > 5)
1477 dump_disasm_list(FIRST_ELEMENT(anchor));
1479 debugs(
"[compile step 3.1 (iseq_optimize)]\n");
1480 iseq_optimize(iseq, anchor);
1482 if (compile_debug > 5)
1483 dump_disasm_list(FIRST_ELEMENT(anchor));
1485 if (ISEQ_COMPILE_DATA(iseq)->option->instructions_unification) {
1486 debugs(
"[compile step 3.2 (iseq_insns_unification)]\n");
1487 iseq_insns_unification(iseq, anchor);
1488 if (compile_debug > 5)
1489 dump_disasm_list(FIRST_ELEMENT(anchor));
1492 if (ISEQ_COMPILE_DATA(iseq)->option->stack_caching) {
1493 debugs(
"[compile step 3.3 (iseq_set_sequence_stackcaching)]\n");
1494 iseq_set_sequence_stackcaching(iseq, anchor);
1495 if (compile_debug > 5)
1496 dump_disasm_list(FIRST_ELEMENT(anchor));
1499 debugs(
"[compile step 3.4 (iseq_insert_nop_between_end_and_cont)]\n");
1500 iseq_insert_nop_between_end_and_cont(iseq);
1501 if (compile_debug > 5)
1502 dump_disasm_list(FIRST_ELEMENT(anchor));
1510 if (
RTEST(ISEQ_COMPILE_DATA(iseq)->err_info))
1513 debugs(
"[compile step 4.1 (iseq_set_sequence)]\n");
1514 if (!iseq_set_sequence(iseq, anchor))
return COMPILE_NG;
1515 if (compile_debug > 5)
1516 dump_disasm_list(FIRST_ELEMENT(anchor));
1518 debugs(
"[compile step 4.2 (iseq_set_exception_table)]\n");
1519 if (!iseq_set_exception_table(iseq))
return COMPILE_NG;
1521 debugs(
"[compile step 4.3 (set_optargs_table)] \n");
1522 if (!iseq_set_optargs_table(iseq))
return COMPILE_NG;
1524 debugs(
"[compile step 5 (iseq_translate_threaded_code)] \n");
1525 if (!rb_iseq_translate_threaded_code(iseq))
return COMPILE_NG;
1527 debugs(
"[compile step 6 (update_catch_except_flags)] \n");
1528 update_catch_except_flags(ISEQ_BODY(iseq));
1530 debugs(
"[compile step 6.1 (remove unused catch tables)] \n");
1531 if (!ISEQ_BODY(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1532 xfree(ISEQ_BODY(iseq)->catch_table);
1533 ISEQ_BODY(iseq)->catch_table = NULL;
1536#if VM_INSN_INFO_TABLE_IMPL == 2
1537 if (ISEQ_BODY(iseq)->insns_info.succ_index_table == NULL) {
1538 debugs(
"[compile step 7 (rb_iseq_insns_info_encode_positions)] \n");
1539 rb_iseq_insns_info_encode_positions(iseq);
1543 if (compile_debug > 1) {
1544 VALUE str = rb_iseq_disasm(iseq);
1547 verify_call_cache(iseq);
1548 debugs(
"[compile step: finish]\n");
1554iseq_set_exception_local_table(
rb_iseq_t *iseq)
1556 ISEQ_BODY(iseq)->local_table_size = numberof(rb_iseq_shared_exc_local_tbl);
1557 ISEQ_BODY(iseq)->local_table = rb_iseq_shared_exc_local_tbl;
1565 while (iseq != ISEQ_BODY(iseq)->local_iseq) {
1567 iseq = ISEQ_BODY(iseq)->parent_iseq;
1573get_dyna_var_idx_at_raw(
const rb_iseq_t *iseq,
ID id)
1577 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1578 if (ISEQ_BODY(iseq)->local_table[i] ==
id) {
1588 int idx = get_dyna_var_idx_at_raw(ISEQ_BODY(iseq)->local_iseq,
id);
1591 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
1592 "get_local_var_idx: %d", idx);
1599get_dyna_var_idx(
const rb_iseq_t *iseq,
ID id,
int *level,
int *ls)
1601 int lv = 0, idx = -1;
1602 const rb_iseq_t *
const topmost_iseq = iseq;
1605 idx = get_dyna_var_idx_at_raw(iseq,
id);
1609 iseq = ISEQ_BODY(iseq)->parent_iseq;
1614 COMPILE_ERROR(topmost_iseq, ISEQ_LAST_LINE(topmost_iseq),
1615 "get_dyna_var_idx: -1");
1619 *ls = ISEQ_BODY(iseq)->local_table_size;
1624iseq_local_block_param_p(
const rb_iseq_t *iseq,
unsigned int idx,
unsigned int level)
1628 iseq = ISEQ_BODY(iseq)->parent_iseq;
1631 body = ISEQ_BODY(iseq);
1632 if (body->local_iseq == iseq &&
1633 body->
param.flags.has_block &&
1634 body->local_table_size - body->
param.block_start == idx) {
1643iseq_block_param_id_p(
const rb_iseq_t *iseq,
ID id,
int *pidx,
int *plevel)
1646 int idx = get_dyna_var_idx(iseq,
id, &level, &ls);
1647 if (iseq_local_block_param_p(iseq, ls - idx, level)) {
1658access_outer_variables(
const rb_iseq_t *iseq,
int level,
ID id,
bool write)
1660 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1662 if (isolated_depth && level >= isolated_depth) {
1664 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not yield from isolated Proc");
1667 COMPILE_ERROR(iseq, ISEQ_LAST_LINE(iseq),
"can not access variable `%s' from isolated Proc",
rb_id2name(
id));
1671 for (
int i=0; i<level; i++) {
1673 struct rb_id_table *ovs = ISEQ_BODY(iseq)->outer_variables;
1676 ovs = ISEQ_BODY(iseq)->outer_variables = rb_id_table_create(8);
1679 if (rb_id_table_lookup(ISEQ_BODY(iseq)->outer_variables,
id, &val)) {
1680 if (write && !val) {
1681 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id,
Qtrue);
1685 rb_id_table_insert(ISEQ_BODY(iseq)->outer_variables,
id, RBOOL(write));
1688 iseq = ISEQ_BODY(iseq)->parent_iseq;
1693iseq_lvar_id(
const rb_iseq_t *iseq,
int idx,
int level)
1695 for (
int i=0; i<level; i++) {
1696 iseq = ISEQ_BODY(iseq)->parent_iseq;
1699 ID id = ISEQ_BODY(iseq)->local_table[ISEQ_BODY(iseq)->local_table_size - idx];
1707 if (iseq_local_block_param_p(iseq, idx, level)) {
1708 ADD_INSN2(seq, line_node, getblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1711 ADD_INSN2(seq, line_node, getlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1713 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
1719 if (iseq_local_block_param_p(iseq, idx, level)) {
1720 ADD_INSN2(seq, line_node, setblockparam,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1723 ADD_INSN2(seq, line_node, setlocal,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
1725 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
1734 if (body->
param.flags.has_opt ||
1735 body->
param.flags.has_post ||
1736 body->
param.flags.has_rest ||
1737 body->
param.flags.has_block ||
1738 body->
param.flags.has_kw ||
1739 body->
param.flags.has_kwrest) {
1741 if (body->
param.flags.has_block) {
1742 body->
param.size = body->
param.block_start + 1;
1744 else if (body->
param.flags.has_kwrest) {
1745 body->
param.size = body->
param.keyword->rest_start + 1;
1747 else if (body->
param.flags.has_kw) {
1748 body->
param.size = body->
param.keyword->bits_start + 1;
1750 else if (body->
param.flags.has_post) {
1753 else if (body->
param.flags.has_rest) {
1754 body->
param.size = body->
param.rest_start + 1;
1756 else if (body->
param.flags.has_opt) {
1772 const NODE *node = args->kw_args;
1774 struct rb_iseq_param_keyword *keyword;
1775 const VALUE default_values = rb_ary_hidden_new(1);
1777 int kw = 0, rkw = 0, di = 0, i;
1779 body->
param.flags.has_kw = TRUE;
1780 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
1784 node = node->nd_next;
1787 keyword->bits_start = arg_size++;
1789 node = args->kw_args;
1791 const NODE *val_node = node->nd_body->nd_value;
1794 if (val_node == NODE_SPECIAL_REQUIRED_KEYWORD) {
1798 switch (nd_type(val_node)) {
1800 dv = val_node->nd_lit;
1812 NO_CHECK(COMPILE_POPPED(optargs,
"kwarg", node));
1816 keyword->num = ++di;
1817 rb_ary_push(default_values, dv);
1820 node = node->nd_next;
1825 if (args->kw_rest_arg->nd_vid != 0) {
1826 keyword->rest_start = arg_size++;
1827 body->
param.flags.has_kwrest = TRUE;
1829 keyword->required_num = rkw;
1830 keyword->table = &body->local_table[keyword->bits_start - keyword->num];
1835 for (i = 0; i <
RARRAY_LEN(default_values); i++) {
1837 if (dv == complex_mark) dv =
Qundef;
1844 keyword->default_values = dvs;
1852 debugs(
"iseq_set_arguments: %s\n", node_args ?
"" :
"0");
1862 EXPECT_NODE(
"iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG);
1864 body->
param.flags.ruby2_keywords = args->ruby2_keywords;
1865 body->
param.lead_num = arg_size = (int)args->pre_args_num;
1866 if (body->
param.lead_num > 0) body->
param.flags.has_lead = TRUE;
1867 debugs(
" - argc: %d\n", body->
param.lead_num);
1869 rest_id = args->rest_arg;
1870 if (rest_id == NODE_SPECIAL_EXCESSIVE_COMMA) {
1874 block_id = args->block_arg;
1876 if (args->opt_args) {
1877 const NODE *node = args->opt_args;
1879 VALUE labels = rb_ary_hidden_new(1);
1884 label = NEW_LABEL(nd_line(node));
1885 rb_ary_push(labels, (
VALUE)label | 1);
1886 ADD_LABEL(optargs, label);
1887 NO_CHECK(COMPILE_POPPED(optargs,
"optarg", node->nd_body));
1888 node = node->nd_next;
1893 label = NEW_LABEL(nd_line(node_args));
1894 rb_ary_push(labels, (
VALUE)label | 1);
1895 ADD_LABEL(optargs, label);
1900 for (j = 0; j < i+1; j++) {
1903 rb_ary_clear(labels);
1905 body->
param.flags.has_opt = TRUE;
1906 body->
param.opt_num = i;
1907 body->
param.opt_table = opt_table;
1912 body->
param.rest_start = arg_size++;
1913 body->
param.flags.has_rest = TRUE;
1914 assert(body->
param.rest_start != -1);
1917 if (args->first_post_arg) {
1918 body->
param.post_start = arg_size;
1919 body->
param.post_num = args->post_args_num;
1920 body->
param.flags.has_post = TRUE;
1921 arg_size += args->post_args_num;
1923 if (body->
param.flags.has_rest) {
1924 body->
param.post_start = body->
param.rest_start + 1;
1928 if (args->kw_args) {
1929 arg_size = iseq_set_arguments_keywords(iseq, optargs, args, arg_size);
1931 else if (args->kw_rest_arg) {
1932 struct rb_iseq_param_keyword *keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
1933 keyword->rest_start = arg_size++;
1934 body->
param.keyword = keyword;
1935 body->
param.flags.has_kwrest = TRUE;
1937 else if (args->no_kwarg) {
1938 body->
param.flags.accepts_no_kwarg = TRUE;
1942 body->
param.block_start = arg_size++;
1943 body->
param.flags.has_block = TRUE;
1946 iseq_calc_param_size(iseq);
1947 body->
param.size = arg_size;
1949 if (args->pre_init) {
1950 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (m)", args->pre_init));
1952 if (args->post_init) {
1953 NO_CHECK(COMPILE_POPPED(optargs,
"init arguments (p)", args->post_init));
1956 if (body->type == ISEQ_TYPE_BLOCK) {
1957 if (body->
param.flags.has_opt == FALSE &&
1958 body->
param.flags.has_post == FALSE &&
1959 body->
param.flags.has_rest == FALSE &&
1960 body->
param.flags.has_kw == FALSE &&
1961 body->
param.flags.has_kwrest == FALSE) {
1963 if (body->
param.lead_num == 1 && last_comma == 0) {
1965 body->
param.flags.ambiguous_param0 = TRUE;
1977 unsigned int size = tbl ? tbl->size : 0;
1982 ISEQ_BODY(iseq)->local_table = ids;
1984 ISEQ_BODY(iseq)->local_table_size = size;
1986 debugs(
"iseq_set_local_table: %u\n", ISEQ_BODY(iseq)->local_table_size);
1998 else if ((tlit = OBJ_BUILTIN_TYPE(lit)) == -1) {
2001 else if ((tval = OBJ_BUILTIN_TYPE(val)) == -1) {
2004 else if (tlit != tval) {
2014 long x =
FIX2LONG(rb_big_cmp(lit, val));
2022 return rb_float_cmp(lit, val);
2025 const struct RRational *rat1 = RRATIONAL(val);
2026 const struct RRational *rat2 = RRATIONAL(lit);
2027 return rb_iseq_cdhash_cmp(rat1->num, rat2->num) || rb_iseq_cdhash_cmp(rat1->den, rat2->den);
2030 const struct RComplex *comp1 = RCOMPLEX(val);
2031 const struct RComplex *comp2 = RCOMPLEX(lit);
2032 return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag);
2035 return rb_reg_equal(val, lit) ? 0 : -1;
2043rb_iseq_cdhash_hash(
VALUE a)
2045 switch (OBJ_BUILTIN_TYPE(a)) {
2048 return (st_index_t)a;
2056 return rb_rational_hash(a);
2058 return rb_complex_hash(a);
2068 rb_iseq_cdhash_hash,
2082 rb_hash_aset(data->hash, key,
INT2FIX(lobj->position - (data->pos+data->len)));
2090 return INT2FIX(ISEQ_BODY(iseq)->ivc_size++);
2097 struct rb_id_table *tbl = ISEQ_COMPILE_DATA(iseq)->ivar_cache_table;
2099 if (rb_id_table_lookup(tbl,
id,&val)) {
2104 tbl = rb_id_table_create(1);
2105 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = tbl;
2107 val =
INT2FIX(ISEQ_BODY(iseq)->icvarc_size++);
2108 rb_id_table_insert(tbl,
id,val);
2112#define BADINSN_DUMP(anchor, list, dest) \
2113 dump_disasm_list_with_cursor(FIRST_ELEMENT(anchor), list, dest)
2115#define BADINSN_ERROR \
2116 (xfree(generated_iseq), \
2117 xfree(insns_info), \
2118 BADINSN_DUMP(anchor, list, NULL), \
2124 int stack_max = 0, sp = 0, line = 0;
2127 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2128 if (IS_LABEL(list)) {
2134 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2135 switch (list->type) {
2136 case ISEQ_ELEMENT_INSN:
2144 sp = calc_sp_depth(sp, iobj);
2146 BADINSN_DUMP(anchor, list, NULL);
2147 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2148 "argument stack underflow (%d)", sp);
2151 if (sp > stack_max) {
2155 line = iobj->insn_info.line_no;
2157 operands = iobj->operands;
2158 insn = iobj->insn_id;
2159 types = insn_op_types(insn);
2160 len = insn_len(insn);
2163 if (iobj->operand_size != len - 1) {
2165 BADINSN_DUMP(anchor, list, NULL);
2166 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2167 "operand size miss! (%d for %d)",
2168 iobj->operand_size, len - 1);
2172 for (j = 0; types[j]; j++) {
2173 if (types[j] == TS_OFFSET) {
2177 BADINSN_DUMP(anchor, list, NULL);
2178 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2179 "unknown label: "LABEL_FORMAT, lobj->label_no);
2182 if (lobj->sp == -1) {
2185 else if (lobj->sp != sp) {
2186 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2188 lobj->label_no, lobj->sp, sp);
2194 case ISEQ_ELEMENT_LABEL:
2197 if (lobj->sp == -1) {
2201 if (lobj->sp != sp) {
2202 debugs(
"%s:%d: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2204 lobj->label_no, lobj->sp, sp);
2210 case ISEQ_ELEMENT_TRACE:
2215 case ISEQ_ELEMENT_ADJUST:
2220 sp = adjust->label ? adjust->label->sp : 0;
2221 if (adjust->line_no != -1 && orig_sp - sp < 0) {
2222 BADINSN_DUMP(anchor, list, NULL);
2223 COMPILE_ERROR(iseq, adjust->line_no,
2224 "iseq_set_sequence: adjust bug %d < %d",
2231 BADINSN_DUMP(anchor, list, NULL);
2232 COMPILE_ERROR(iseq, line,
"unknown list type: %d", list->type);
2241 int insns_info_index,
int code_index,
const INSN *iobj)
2243 if (insns_info_index == 0 ||
2244 insns_info[insns_info_index-1].line_no != iobj->insn_info.line_no ||
2245#ifdef USE_ISEQ_NODE_ID
2246 insns_info[insns_info_index-1].node_id != iobj->insn_info.node_id ||
2248 insns_info[insns_info_index-1].events != iobj->insn_info.events) {
2249 insns_info[insns_info_index].line_no = iobj->insn_info.line_no;
2250#ifdef USE_ISEQ_NODE_ID
2251 insns_info[insns_info_index].node_id = iobj->insn_info.node_id;
2253 insns_info[insns_info_index].events = iobj->insn_info.events;
2254 positions[insns_info_index] = code_index;
2262 int insns_info_index,
int code_index,
const ADJUST *adjust)
2264 insns_info[insns_info_index].line_no = adjust->line_no;
2265 insns_info[insns_info_index].events = 0;
2266 positions[insns_info_index] = code_index;
2271array_to_idlist(
VALUE arr)
2276 for (
int i = 0; i < size; i++) {
2285idlist_to_array(
const ID *ids)
2287 VALUE arr = rb_ary_new();
2289 rb_ary_push(arr,
ID2SYM(*ids++));
2302 unsigned int *positions;
2304 VALUE *generated_iseq;
2308 int insn_num, code_index, insns_info_index, sp = 0;
2309 int stack_max = fix_sp_depth(iseq, anchor);
2311 if (stack_max < 0)
return COMPILE_NG;
2314 insn_num = code_index = 0;
2315 for (list = FIRST_ELEMENT(anchor); list; list = list->next) {
2316 switch (list->type) {
2317 case ISEQ_ELEMENT_INSN:
2321 sp = calc_sp_depth(sp, iobj);
2323 events = iobj->insn_info.events |= events;
2324 if (ISEQ_COVERAGE(iseq)) {
2325 if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) &&
2326 !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) {
2327 int line = iobj->insn_info.line_no - 1;
2328 if (line >= 0 && line <
RARRAY_LEN(ISEQ_LINE_COVERAGE(iseq))) {
2332 if (ISEQ_BRANCH_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_BRANCH)) {
2333 while (
RARRAY_LEN(ISEQ_PC2BRANCHINDEX(iseq)) <= code_index) {
2334 rb_ary_push(ISEQ_PC2BRANCHINDEX(iseq),
Qnil);
2339 code_index += insn_data_length(iobj);
2344 case ISEQ_ELEMENT_LABEL:
2347 lobj->position = code_index;
2348 if (lobj->sp != sp) {
2349 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2351 lobj->label_no, lobj->sp, sp);
2356 case ISEQ_ELEMENT_TRACE:
2359 events |= trace->event;
2360 if (trace->event & RUBY_EVENT_COVERAGE_BRANCH) data = trace->data;
2363 case ISEQ_ELEMENT_ADJUST:
2366 if (adjust->line_no != -1) {
2368 sp = adjust->label ? adjust->label->sp : 0;
2369 if (orig_sp - sp > 0) {
2370 if (orig_sp - sp > 1) code_index++;
2384 positions =
ALLOC_N(
unsigned int, insn_num);
2387 ISEQ_COMPILE_DATA(iseq)->ci_index = 0;
2394 iseq_bits_t * mark_offset_bits;
2395 int code_size = code_index;
2397 iseq_bits_t tmp[1] = {0};
2398 bool needs_bitmap =
false;
2400 if (ISEQ_MBITS_BUFLEN(code_index) == 1) {
2401 mark_offset_bits = tmp;
2404 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(code_index));
2407 list = FIRST_ELEMENT(anchor);
2408 insns_info_index = code_index = sp = 0;
2411 switch (list->type) {
2412 case ISEQ_ELEMENT_INSN:
2420 sp = calc_sp_depth(sp, iobj);
2422 operands = iobj->operands;
2423 insn = iobj->insn_id;
2424 generated_iseq[code_index] = insn;
2425 types = insn_op_types(insn);
2426 len = insn_len(insn);
2428 for (j = 0; types[j]; j++) {
2429 char type = types[j];
2437 generated_iseq[code_index + 1 + j] = lobj->position - (code_index + len);
2442 VALUE map = operands[j];
2445 data.pos = code_index;
2449 rb_hash_rehash(map);
2450 freeze_hide_obj(map);
2451 generated_iseq[code_index + 1 + j] = map;
2452 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2454 needs_bitmap =
true;
2459 generated_iseq[code_index + 1 + j] =
FIX2INT(operands[j]);
2464 VALUE v = operands[j];
2465 generated_iseq[code_index + 1 + j] = v;
2469 ISEQ_MBITS_SET(mark_offset_bits, code_index + 1 + j);
2470 needs_bitmap =
true;
2477 unsigned int ic_index = ISEQ_COMPILE_DATA(iseq)->ic_index++;
2478 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2479 if (UNLIKELY(ic_index >= body->ic_size)) {
2480 BADINSN_DUMP(anchor, &iobj->link, 0);
2481 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2482 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2483 ic_index, ISEQ_IS_SIZE(body));
2486 ic->
segments = array_to_idlist(operands[j]);
2488 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2493 unsigned int ic_index =
FIX2UINT(operands[j]);
2495 IVC cache = ((
IVC)&body->is_entries[ic_index]);
2497 if (insn == BIN(setinstancevariable)) {
2498 cache->iv_set_name =
SYM2ID(operands[j - 1]);
2501 cache->iv_set_name = 0;
2504 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
2509 unsigned int ic_index =
FIX2UINT(operands[j]);
2510 IC ic = &ISEQ_IS_ENTRY_START(body,
type)[ic_index].ic_cache;
2511 if (UNLIKELY(ic_index >= ISEQ_IS_SIZE(body))) {
2512 BADINSN_DUMP(anchor, &iobj->link, 0);
2513 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
2514 "iseq_set_sequence: ic_index overflow: index: %d, size: %d",
2515 ic_index, ISEQ_IS_SIZE(body));
2517 generated_iseq[code_index + 1 + j] = (
VALUE)ic;
2524 struct rb_call_data *cd = &body->call_data[ISEQ_COMPILE_DATA(iseq)->ci_index++];
2525 assert(ISEQ_COMPILE_DATA(iseq)->ci_index <= body->ci_size);
2527 cd->cc = vm_cc_empty();
2528 generated_iseq[code_index + 1 + j] = (
VALUE)cd;
2532 generated_iseq[code_index + 1 + j] =
SYM2ID(operands[j]);
2535 generated_iseq[code_index + 1 + j] = operands[j];
2538 generated_iseq[code_index + 1 + j] = operands[j];
2541 BADINSN_ERROR(iseq, iobj->insn_info.line_no,
2542 "unknown operand type: %c",
type);
2546 if (add_insn_info(insns_info, positions, insns_info_index, code_index, iobj)) insns_info_index++;
2550 case ISEQ_ELEMENT_LABEL:
2553 if (lobj->sp != sp) {
2554 debugs(
"%s: sp inconsistency found but ignored (" LABEL_FORMAT
" sp: %d, calculated sp: %d)\n",
2556 lobj->label_no, lobj->sp, sp);
2561 case ISEQ_ELEMENT_ADJUST:
2566 if (adjust->label) {
2567 sp = adjust->label->sp;
2573 if (adjust->line_no != -1) {
2574 const int diff = orig_sp - sp;
2576 if (insns_info_index == 0) {
2577 COMPILE_ERROR(iseq, adjust->line_no,
2578 "iseq_set_sequence: adjust bug (ISEQ_ELEMENT_ADJUST must not be the first in iseq)");
2580 if (add_adjust_info(insns_info, positions, insns_info_index, code_index, adjust)) insns_info_index++;
2583 generated_iseq[code_index++] = BIN(adjuststack);
2584 generated_iseq[code_index++] = orig_sp - sp;
2586 else if (diff == 1) {
2587 generated_iseq[code_index++] = BIN(pop);
2589 else if (diff < 0) {
2590 int label_no = adjust->label ? adjust->label->label_no : -1;
2591 xfree(generated_iseq);
2594 if (ISEQ_MBITS_BUFLEN(code_size) > 1) {
2595 xfree(mark_offset_bits);
2597 debug_list(anchor, list);
2598 COMPILE_ERROR(iseq, adjust->line_no,
2599 "iseq_set_sequence: adjust bug to %d %d < %d",
2600 label_no, orig_sp, sp);
2613 body->iseq_encoded = (
void *)generated_iseq;
2614 body->iseq_size = code_index;
2615 body->stack_max = stack_max;
2617 if (ISEQ_MBITS_BUFLEN(body->iseq_size) == 1) {
2618 body->mark_bits.single = mark_offset_bits[0];
2622 body->mark_bits.list = mark_offset_bits;
2625 body->mark_bits.list = 0;
2626 ruby_xfree(mark_offset_bits);
2631 body->insns_info.body = insns_info;
2632 body->insns_info.positions = positions;
2635 body->insns_info.body = insns_info;
2636 REALLOC_N(positions,
unsigned int, insns_info_index);
2637 body->insns_info.positions = positions;
2638 body->insns_info.size = insns_info_index;
2644label_get_position(
LABEL *lobj)
2646 return lobj->position;
2650label_get_sp(
LABEL *lobj)
2656iseq_set_exception_table(
rb_iseq_t *iseq)
2658 const VALUE *tptr, *ptr;
2659 unsigned int tlen, i;
2662 ISEQ_BODY(iseq)->catch_table = NULL;
2664 VALUE catch_table_ary = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
2665 if (
NIL_P(catch_table_ary))
return COMPILE_OK;
2673 for (i = 0; i < table->size; i++) {
2675 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
2676 entry->type = (
enum rb_catch_type)(ptr[0] & 0xffff);
2677 entry->start = label_get_position((
LABEL *)(ptr[1] & ~1));
2678 entry->end = label_get_position((
LABEL *)(ptr[2] & ~1));
2685 entry->cont = label_get_position(lobj);
2686 entry->sp = label_get_sp(lobj);
2689 if (entry->type == CATCH_TYPE_RESCUE ||
2690 entry->type == CATCH_TYPE_BREAK ||
2691 entry->type == CATCH_TYPE_NEXT) {
2699 ISEQ_BODY(iseq)->catch_table = table;
2700 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, 0);
2721 VALUE *opt_table = (
VALUE *)ISEQ_BODY(iseq)->param.opt_table;
2723 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
2724 for (i = 0; i < ISEQ_BODY(iseq)->param.opt_num + 1; i++) {
2725 opt_table[i] = label_get_position((
LABEL *)opt_table[i]);
2732get_destination_insn(
INSN *iobj)
2738 list = lobj->link.next;
2740 switch (list->type) {
2741 case ISEQ_ELEMENT_INSN:
2742 case ISEQ_ELEMENT_ADJUST:
2744 case ISEQ_ELEMENT_LABEL:
2747 case ISEQ_ELEMENT_TRACE:
2750 events |= trace->event;
2758 if (list && IS_INSN(list)) {
2760 iobj->insn_info.events |= events;
2766get_next_insn(
INSN *iobj)
2771 if (IS_INSN(list) || IS_ADJUST(list)) {
2780get_prev_insn(
INSN *iobj)
2785 if (IS_INSN(list) || IS_ADJUST(list)) {
2794unref_destination(
INSN *iobj,
int pos)
2796 LABEL *lobj = (
LABEL *)OPERAND_AT(iobj, pos);
2798 if (!lobj->refcnt) ELEM_REMOVE(&lobj->link);
2802replace_destination(
INSN *dobj,
INSN *nobj)
2804 VALUE n = OPERAND_AT(nobj, 0);
2809 OPERAND_AT(dobj, 0) = n;
2810 if (!dl->refcnt) ELEM_REMOVE(&dl->link);
2814find_destination(
INSN *i)
2816 int pos, len = insn_len(i->insn_id);
2817 for (pos = 0; pos < len; ++pos) {
2818 if (insn_op_types(i->insn_id)[pos] == TS_OFFSET) {
2819 return (
LABEL *)OPERAND_AT(i, pos);
2829 int *unref_counts = 0, nlabels = ISEQ_COMPILE_DATA(iseq)->label_no;
2832 unref_counts =
ALLOCA_N(
int, nlabels);
2833 MEMZERO(unref_counts,
int, nlabels);
2838 if (IS_INSN_ID(i, leave)) {
2842 else if ((lab = find_destination((
INSN *)i)) != 0) {
2843 if (lab->unremovable)
break;
2844 unref_counts[lab->label_no]++;
2847 else if (IS_LABEL(i)) {
2849 if (lab->unremovable)
return 0;
2850 if (lab->refcnt > unref_counts[lab->label_no]) {
2851 if (i == first)
return 0;
2856 else if (IS_TRACE(i)) {
2859 else if (IS_ADJUST(i)) {
2861 if (dest && dest->unremovable)
return 0;
2864 }
while ((i = i->next) != 0);
2869 VALUE insn = INSN_OF(i);
2870 int pos, len = insn_len(insn);
2871 for (pos = 0; pos < len; ++pos) {
2872 switch (insn_op_types(insn)[pos]) {
2874 unref_destination((
INSN *)i, pos);
2883 }
while ((i != end) && (i = i->next) != 0);
2890 switch (OPERAND_AT(iobj, 0)) {
2892 ELEM_REMOVE(&iobj->link);
2895 ELEM_REMOVE(&iobj->link);
2898 iobj->insn_id = BIN(adjuststack);
2904is_frozen_putstring(
INSN *insn,
VALUE *op)
2906 if (IS_INSN_ID(insn, putstring)) {
2907 *op = OPERAND_AT(insn, 0);
2910 else if (IS_INSN_ID(insn, putobject)) {
2911 *op = OPERAND_AT(insn, 0);
2942 INSN *niobj, *ciobj, *dup = 0;
2946 switch (INSN_OF(iobj)) {
2947 case BIN(putstring):
2953 case BIN(putobject):
2956 default:
return FALSE;
2959 ciobj = (
INSN *)get_next_insn(iobj);
2960 if (IS_INSN_ID(ciobj, jump)) {
2961 ciobj = (
INSN *)get_next_insn((
INSN*)OPERAND_AT(ciobj, 0));
2963 if (IS_INSN_ID(ciobj, dup)) {
2964 ciobj = (
INSN *)get_next_insn(dup = ciobj);
2966 if (!ciobj || !IS_INSN_ID(ciobj, checktype))
return FALSE;
2967 niobj = (
INSN *)get_next_insn(ciobj);
2972 switch (INSN_OF(niobj)) {
2974 if (OPERAND_AT(ciobj, 0) ==
type) {
2975 dest = (
LABEL *)OPERAND_AT(niobj, 0);
2978 case BIN(branchunless):
2979 if (OPERAND_AT(ciobj, 0) !=
type) {
2980 dest = (
LABEL *)OPERAND_AT(niobj, 0);
2986 line = ciobj->insn_info.line_no;
2987 node_id = ciobj->insn_info.node_id;
2988 NODE dummy_line_node = generate_dummy_line_node(line, node_id);
2990 if (niobj->link.next && IS_LABEL(niobj->link.next)) {
2991 dest = (
LABEL *)niobj->link.next;
2994 dest = NEW_LABEL(line);
2995 ELEM_INSERT_NEXT(&niobj->link, &dest->link);
2998 INSERT_AFTER_INSN1(iobj, &dummy_line_node, jump, dest);
3000 if (!dup) INSERT_AFTER_INSN(iobj, &dummy_line_node, pop);
3007 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3008 vm_ci_flag(ci) | add,
3018 const struct rb_callinfo *nci = vm_ci_new(vm_ci_mid(ci),
3032 optimize_checktype(iseq, iobj);
3034 if (IS_INSN_ID(iobj, jump)) {
3035 INSN *niobj, *diobj, *piobj;
3036 diobj = (
INSN *)get_destination_insn(iobj);
3037 niobj = (
INSN *)get_next_insn(iobj);
3039 if (diobj == niobj) {
3046 unref_destination(iobj, 0);
3047 ELEM_REMOVE(&iobj->link);
3050 else if (iobj != diobj && IS_INSN(&diobj->link) &&
3051 IS_INSN_ID(diobj, jump) &&
3052 OPERAND_AT(iobj, 0) != OPERAND_AT(diobj, 0) &&
3053 diobj->insn_info.events == 0) {
3064 replace_destination(iobj, diobj);
3065 remove_unreachable_chunk(iseq, iobj->link.next);
3068 else if (IS_INSN_ID(diobj, leave)) {
3081 unref_destination(iobj, 0);
3082 iobj->insn_id = BIN(leave);
3083 iobj->operand_size = 0;
3084 iobj->insn_info = diobj->insn_info;
3087 else if (IS_INSN(iobj->link.prev) &&
3088 (piobj = (
INSN *)iobj->link.prev) &&
3089 (IS_INSN_ID(piobj, branchif) ||
3090 IS_INSN_ID(piobj, branchunless))) {
3091 INSN *pdiobj = (
INSN *)get_destination_insn(piobj);
3092 if (niobj == pdiobj) {
3093 int refcnt = IS_LABEL(piobj->link.next) ?
3094 ((
LABEL *)piobj->link.next)->refcnt : 0;
3109 piobj->insn_id = (IS_INSN_ID(piobj, branchif))
3110 ? BIN(branchunless) : BIN(branchif);
3111 replace_destination(piobj, iobj);
3113 ELEM_REMOVE(&iobj->link);
3120 else if (diobj == pdiobj) {
3134 NODE dummy_line_node = generate_dummy_line_node(iobj->insn_info.line_no, iobj->insn_info.node_id);
3135 INSN *popiobj = new_insn_core(iseq, &dummy_line_node, BIN(pop), 0, 0);
3136 ELEM_REPLACE(&piobj->link, &popiobj->link);
3139 if (remove_unreachable_chunk(iseq, iobj->link.next)) {
3153 if (IS_INSN_ID(iobj, newrange)) {
3154 INSN *
const range = iobj;
3156 VALUE str_beg, str_end;
3158 if ((end = (
INSN *)get_prev_insn(range)) != 0 &&
3159 is_frozen_putstring(end, &str_end) &&
3160 (beg = (
INSN *)get_prev_insn(end)) != 0 &&
3161 is_frozen_putstring(beg, &str_beg)) {
3162 int excl =
FIX2INT(OPERAND_AT(range, 0));
3165 ELEM_REMOVE(&beg->link);
3166 ELEM_REMOVE(&end->link);
3167 range->insn_id = BIN(putobject);
3168 OPERAND_AT(range, 0) = lit_range;
3173 if (IS_INSN_ID(iobj, leave)) {
3174 remove_unreachable_chunk(iseq, iobj->link.next);
3186 if (IS_INSN_ID(iobj, duparray)) {
3188 if (IS_INSN(next) && IS_INSN_ID(next, concatarray)) {
3189 iobj->insn_id = BIN(putobject);
3193 if (IS_INSN_ID(iobj, branchif) ||
3194 IS_INSN_ID(iobj, branchnil) ||
3195 IS_INSN_ID(iobj, branchunless)) {
3204 INSN *nobj = (
INSN *)get_destination_insn(iobj);
3226 int stop_optimization =
3227 ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq) &&
3228 nobj->link.type == ISEQ_ELEMENT_INSN &&
3229 nobj->insn_info.events;
3230 if (!stop_optimization) {
3231 INSN *pobj = (
INSN *)iobj->link.prev;
3234 if (!IS_INSN(&pobj->link))
3236 else if (IS_INSN_ID(pobj, dup))
3241 if (IS_INSN(&nobj->link) && IS_INSN_ID(nobj, jump)) {
3242 replace_destination(iobj, nobj);
3244 else if (prev_dup && IS_INSN_ID(nobj, dup) &&
3245 !!(nobj = (
INSN *)nobj->link.next) &&
3247 nobj->insn_id == iobj->insn_id) {
3263 replace_destination(iobj, nobj);
3291 if (prev_dup && IS_INSN(pobj->link.prev)) {
3292 pobj = (
INSN *)pobj->link.prev;
3294 if (IS_INSN_ID(pobj, putobject)) {
3295 cond = (IS_INSN_ID(iobj, branchif) ?
3296 OPERAND_AT(pobj, 0) !=
Qfalse :
3297 IS_INSN_ID(iobj, branchunless) ?
3298 OPERAND_AT(pobj, 0) ==
Qfalse :
3301 else if (IS_INSN_ID(pobj, putstring) ||
3302 IS_INSN_ID(pobj, duparray) ||
3303 IS_INSN_ID(pobj, newarray)) {
3304 cond = IS_INSN_ID(iobj, branchif);
3306 else if (IS_INSN_ID(pobj, putnil)) {
3307 cond = !IS_INSN_ID(iobj, branchif);
3310 if (prev_dup || !IS_INSN_ID(pobj, newarray)) {
3311 ELEM_REMOVE(iobj->link.prev);
3313 else if (!iseq_pop_newarray(iseq, pobj)) {
3314 NODE dummy_line_node = generate_dummy_line_node(pobj->insn_info.line_no, pobj->insn_info.node_id);
3315 pobj = new_insn_core(iseq, &dummy_line_node, BIN(pop), 0, NULL);
3316 ELEM_INSERT_PREV(&iobj->link, &pobj->link);
3320 NODE dummy_line_node = generate_dummy_line_node(pobj->insn_info.line_no, pobj->insn_info.node_id);
3321 pobj = new_insn_core(iseq, &dummy_line_node, BIN(putnil), 0, NULL);
3322 ELEM_INSERT_NEXT(&iobj->link, &pobj->link);
3324 iobj->insn_id = BIN(jump);
3328 unref_destination(iobj, 0);
3329 ELEM_REMOVE(&iobj->link);
3334 nobj = (
INSN *)get_destination_insn(nobj);
3339 if (IS_INSN_ID(iobj, pop)) {
3347 if (IS_INSN(prev)) {
3348 enum ruby_vminsn_type previ = ((
INSN *)prev)->insn_id;
3349 if (previ == BIN(putobject) || previ == BIN(putnil) ||
3350 previ == BIN(putself) || previ == BIN(putstring) ||
3351 previ == BIN(dup) ||
3352 previ == BIN(getlocal) ||
3353 previ == BIN(getblockparam) ||
3354 previ == BIN(getblockparamproxy) ||
3356 previ == BIN(duparray)) {
3360 ELEM_REMOVE(&iobj->link);
3362 else if (previ == BIN(newarray) && iseq_pop_newarray(iseq, (
INSN*)prev)) {
3363 ELEM_REMOVE(&iobj->link);
3365 else if (previ == BIN(concatarray)) {
3367 NODE dummy_line_node = generate_dummy_line_node(piobj->insn_info.line_no, piobj->insn_info.node_id);
3368 INSERT_BEFORE_INSN1(piobj, &dummy_line_node, splatarray,
Qfalse);
3369 INSN_OF(piobj) = BIN(pop);
3371 else if (previ == BIN(concatstrings)) {
3372 if (OPERAND_AT(prev, 0) ==
INT2FIX(1)) {
3376 ELEM_REMOVE(&iobj->link);
3377 INSN_OF(prev) = BIN(adjuststack);
3383 if (IS_INSN_ID(iobj, newarray) ||
3384 IS_INSN_ID(iobj, duparray) ||
3385 IS_INSN_ID(iobj, expandarray) ||
3386 IS_INSN_ID(iobj, concatarray) ||
3387 IS_INSN_ID(iobj, splatarray) ||
3397 if (IS_INSN(next) && IS_INSN_ID(next, splatarray)) {
3403 if (IS_INSN_ID(iobj, newarray)) {
3405 if (IS_INSN(next) && IS_INSN_ID(next, expandarray) &&
3406 OPERAND_AT(next, 1) ==
INT2FIX(0)) {
3408 op1 = OPERAND_AT(iobj, 0);
3409 op2 = OPERAND_AT(next, 0);
3420 INSN_OF(iobj) = BIN(swap);
3421 iobj->operand_size = 0;
3430 INSN_OF(iobj) = BIN(opt_reverse);
3434 NODE dummy_line_node = generate_dummy_line_node(iobj->insn_info.line_no, iobj->insn_info.node_id);
3436 INSN_OF(iobj) = BIN(opt_reverse);
3437 OPERAND_AT(iobj, 0) = OPERAND_AT(next, 0);
3447 for (; diff > 0; diff--) {
3448 INSERT_BEFORE_INSN(iobj, &dummy_line_node, pop);
3459 for (; diff < 0; diff++) {
3460 INSERT_BEFORE_INSN(iobj, &dummy_line_node, putnil);
3467 if (IS_INSN_ID(iobj, duparray)) {
3476 if (IS_INSN(next) && IS_INSN_ID(next, expandarray)) {
3477 INSN_OF(iobj) = BIN(putobject);
3481 if (IS_INSN_ID(iobj, anytostring)) {
3489 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings) &&
3490 OPERAND_AT(next, 0) ==
INT2FIX(1)) {
3495 if (IS_INSN_ID(iobj, putstring) ||
3503 if (IS_NEXT_INSN_ID(&iobj->link, concatstrings) &&
3505 INSN *next = (
INSN *)iobj->link.next;
3506 if ((OPERAND_AT(next, 0) = FIXNUM_INC(OPERAND_AT(next, 0), -1)) ==
INT2FIX(1)) {
3507 ELEM_REMOVE(&next->link);
3509 ELEM_REMOVE(&iobj->link);
3513 if (IS_INSN_ID(iobj, concatstrings)) {
3522 if (IS_INSN(next) && IS_INSN_ID(next, jump))
3523 next = get_destination_insn(jump = (
INSN *)next);
3524 if (IS_INSN(next) && IS_INSN_ID(next, concatstrings)) {
3525 int n =
FIX2INT(OPERAND_AT(iobj, 0)) +
FIX2INT(OPERAND_AT(next, 0)) - 1;
3526 OPERAND_AT(iobj, 0) =
INT2FIX(n);
3528 LABEL *label = ((
LABEL *)OPERAND_AT(jump, 0));
3529 if (!--label->refcnt) {
3530 ELEM_REMOVE(&label->link);
3533 label = NEW_LABEL(0);
3534 OPERAND_AT(jump, 0) = (
VALUE)label;
3537 ELEM_INSERT_NEXT(next, &label->link);
3538 CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
3546 if (do_tailcallopt &&
3547 (IS_INSN_ID(iobj, send) ||
3548 IS_INSN_ID(iobj, opt_aref_with) ||
3549 IS_INSN_ID(iobj, opt_aset_with) ||
3550 IS_INSN_ID(iobj, invokesuper))) {
3559 if (iobj->link.next) {
3562 if (!IS_INSN(next)) {
3566 switch (INSN_OF(next)) {
3575 next = get_destination_insn((
INSN *)next);
3589 if (IS_INSN_ID(piobj, send) ||
3590 IS_INSN_ID(piobj, invokesuper)) {
3591 if (OPERAND_AT(piobj, 1) == 0) {
3592 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3593 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3598 ci = ci_flag_set(iseq, ci, VM_CALL_TAILCALL);
3599 OPERAND_AT(piobj, 0) = (
VALUE)ci;
3605 if (IS_INSN_ID(iobj, dup)) {
3606 if (IS_NEXT_INSN_ID(&iobj->link, setlocal)) {
3617 if (IS_NEXT_INSN_ID(set1, setlocal)) {
3619 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3620 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3622 ELEM_REMOVE(&iobj->link);
3635 else if (IS_NEXT_INSN_ID(set1, dup) &&
3636 IS_NEXT_INSN_ID(set1->next, setlocal)) {
3637 set2 = set1->next->next;
3638 if (OPERAND_AT(set1, 0) == OPERAND_AT(set2, 0) &&
3639 OPERAND_AT(set1, 1) == OPERAND_AT(set2, 1)) {
3640 ELEM_REMOVE(set1->next);
3654 if (IS_INSN_ID(iobj, getlocal)) {
3656 if (IS_NEXT_INSN_ID(niobj, dup)) {
3657 niobj = niobj->next;
3659 if (IS_NEXT_INSN_ID(niobj, setlocal)) {
3661 if (OPERAND_AT(iobj, 0) == OPERAND_AT(set1, 0) &&
3662 OPERAND_AT(iobj, 1) == OPERAND_AT(set1, 1)) {
3678 if (IS_INSN_ID(iobj, opt_invokebuiltin_delegate)) {
3679 if (IS_TRACE(iobj->link.next)) {
3680 if (IS_NEXT_INSN_ID(iobj->link.next, leave)) {
3681 iobj->insn_id = BIN(opt_invokebuiltin_delegate_leave);
3693 if (IS_INSN_ID(iobj, getblockparam)) {
3694 if (IS_NEXT_INSN_ID(&iobj->link, branchif) || IS_NEXT_INSN_ID(&iobj->link, branchunless)) {
3695 iobj->insn_id = BIN(getblockparamproxy);
3703insn_set_specialized_instruction(
rb_iseq_t *iseq,
INSN *iobj,
int insn_id)
3705 iobj->insn_id = insn_id;
3706 iobj->operand_size = insn_len(insn_id) - 1;
3709 if (insn_id == BIN(opt_neq)) {
3710 VALUE original_ci = iobj->operands[0];
3711 iobj->operand_size = 2;
3712 iobj->operands = compile_data_calloc2(iseq, iobj->operand_size,
sizeof(
VALUE));
3713 iobj->operands[0] = (
VALUE)new_callinfo(iseq, idEq, 1, 0, NULL, FALSE);
3714 iobj->operands[1] = original_ci;
3723 if (IS_INSN_ID(iobj, newarray) && iobj->link.next &&
3724 IS_INSN(iobj->link.next)) {
3728 INSN *niobj = (
INSN *)iobj->link.next;
3729 if (IS_INSN_ID(niobj, send)) {
3731 if ((vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE) && vm_ci_argc(ci) == 0) {
3732 switch (vm_ci_mid(ci)) {
3734 iobj->insn_id = BIN(opt_newarray_max);
3735 ELEM_REMOVE(&niobj->link);
3738 iobj->insn_id = BIN(opt_newarray_min);
3739 ELEM_REMOVE(&niobj->link);
3746 if (IS_INSN_ID(iobj, send)) {
3750#define SP_INSN(opt) insn_set_specialized_instruction(iseq, iobj, BIN(opt_##opt))
3751 if (vm_ci_flag(ci) & VM_CALL_ARGS_SIMPLE) {
3752 switch (vm_ci_argc(ci)) {
3754 switch (vm_ci_mid(ci)) {
3755 case idLength: SP_INSN(length);
return COMPILE_OK;
3756 case idSize: SP_INSN(size);
return COMPILE_OK;
3757 case idEmptyP: SP_INSN(empty_p);
return COMPILE_OK;
3758 case idNilP: SP_INSN(nil_p);
return COMPILE_OK;
3759 case idSucc: SP_INSN(succ);
return COMPILE_OK;
3760 case idNot: SP_INSN(not);
return COMPILE_OK;
3764 switch (vm_ci_mid(ci)) {
3765 case idPLUS: SP_INSN(plus);
return COMPILE_OK;
3766 case idMINUS: SP_INSN(minus);
return COMPILE_OK;
3767 case idMULT: SP_INSN(mult);
return COMPILE_OK;
3768 case idDIV: SP_INSN(div);
return COMPILE_OK;
3769 case idMOD: SP_INSN(mod);
return COMPILE_OK;
3770 case idEq: SP_INSN(eq);
return COMPILE_OK;
3771 case idNeq: SP_INSN(neq);
return COMPILE_OK;
3772 case idEqTilde:SP_INSN(regexpmatch2);
return COMPILE_OK;
3773 case idLT: SP_INSN(lt);
return COMPILE_OK;
3774 case idLE: SP_INSN(le);
return COMPILE_OK;
3775 case idGT: SP_INSN(gt);
return COMPILE_OK;
3776 case idGE: SP_INSN(ge);
return COMPILE_OK;
3777 case idLTLT: SP_INSN(ltlt);
return COMPILE_OK;
3778 case idAREF: SP_INSN(aref);
return COMPILE_OK;
3779 case idAnd: SP_INSN(and);
return COMPILE_OK;
3780 case idOr: SP_INSN(or);
return COMPILE_OK;
3784 switch (vm_ci_mid(ci)) {
3785 case idASET: SP_INSN(aset);
return COMPILE_OK;
3791 if ((vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) == 0 && blockiseq == NULL) {
3792 iobj->insn_id = BIN(opt_send_without_block);
3793 iobj->operand_size = insn_len(iobj->insn_id) - 1;
3804 switch (ISEQ_BODY(iseq)->
type) {
3806 case ISEQ_TYPE_EVAL:
3807 case ISEQ_TYPE_MAIN:
3809 case ISEQ_TYPE_RESCUE:
3810 case ISEQ_TYPE_ENSURE:
3822 const int do_peepholeopt = ISEQ_COMPILE_DATA(iseq)->option->peephole_optimization;
3823 const int do_tailcallopt = tailcallable_p(iseq) &&
3824 ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization;
3825 const int do_si = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction;
3826 const int do_ou = ISEQ_COMPILE_DATA(iseq)->option->operands_unification;
3827 int rescue_level = 0;
3828 int tailcallopt = do_tailcallopt;
3830 list = FIRST_ELEMENT(anchor);
3832 int do_block_optimization = 0;
3834 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK && !ISEQ_BODY(iseq)->catch_except_p) {
3835 do_block_optimization = 1;
3839 if (IS_INSN(list)) {
3840 if (do_peepholeopt) {
3841 iseq_peephole_optimize(iseq, list, tailcallopt);
3844 iseq_specialized_instruction(iseq, (
INSN *)list);
3847 insn_operands_unification((
INSN *)list);
3850 if (do_block_optimization) {
3852 if (IS_INSN_ID(item, jump)) {
3853 do_block_optimization = 0;
3857 if (IS_LABEL(list)) {
3858 switch (((
LABEL *)list)->rescued) {
3859 case LABEL_RESCUE_BEG:
3861 tailcallopt = FALSE;
3863 case LABEL_RESCUE_END:
3864 if (!--rescue_level) tailcallopt = do_tailcallopt;
3871 if (do_block_optimization) {
3873 if (IS_INSN(le) && IS_INSN_ID((
INSN *)le, nop)) {
3880#if OPT_INSTRUCTIONS_UNIFICATION
3888 VALUE *operands = 0, *ptr = 0;
3892 for (i = 0; i < size; i++) {
3893 iobj = (
INSN *)list;
3894 argc += iobj->operand_size;
3899 ptr = operands = compile_data_alloc2(iseq,
sizeof(
VALUE), argc);
3904 for (i = 0; i < size; i++) {
3905 iobj = (
INSN *)list;
3906 MEMCPY(ptr, iobj->operands,
VALUE, iobj->operand_size);
3907 ptr += iobj->operand_size;
3911 NODE dummy_line_node = generate_dummy_line_node(iobj->insn_info.line_no, iobj->insn_info.node_id);
3912 return new_insn_core(iseq, &dummy_line_node, insn_id, argc, operands);
3924#if OPT_INSTRUCTIONS_UNIFICATION
3930 list = FIRST_ELEMENT(anchor);
3932 if (IS_INSN(list)) {
3933 iobj = (
INSN *)list;
3935 if (unified_insns_data[
id] != 0) {
3936 const int *
const *entry = unified_insns_data[id];
3937 for (j = 1; j < (intptr_t)entry[0]; j++) {
3938 const int *unified = entry[j];
3940 for (k = 2; k < unified[1]; k++) {
3942 ((
INSN *)li)->insn_id != unified[k]) {
3949 new_unified_insn(iseq, unified[0], unified[1] - 1,
3954 niobj->link.next = li;
3972#if OPT_STACK_CACHING
3974#define SC_INSN(insn, stat) sc_insn_info[(insn)][(stat)]
3975#define SC_NEXT(insn) sc_insn_next[(insn)]
3977#include "opt_sc.inc"
3985 insn_id = iobj->insn_id;
3986 iobj->insn_id = SC_INSN(insn_id, state);
3987 nstate = SC_NEXT(iobj->insn_id);
3989 if (insn_id == BIN(jump) ||
3990 insn_id == BIN(branchif) || insn_id == BIN(branchunless)) {
3993 if (lobj->sc_state != 0) {
3994 if (lobj->sc_state != nstate) {
3995 BADINSN_DUMP(anchor, iobj, lobj);
3996 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
3997 "insn_set_sc_state error: %d at "LABEL_FORMAT
3999 lobj->sc_state, lobj->label_no, nstate);
4004 lobj->sc_state = nstate;
4006 if (insn_id == BIN(jump)) {
4010 else if (insn_id == BIN(leave)) {
4018label_set_sc_state(
LABEL *lobj,
int state)
4020 if (lobj->sc_state != 0) {
4021 if (lobj->sc_state != state) {
4022 state = lobj->sc_state;
4026 lobj->sc_state = state;
4038#if OPT_STACK_CACHING
4044 list = FIRST_ELEMENT(anchor);
4050 switch (list->type) {
4051 case ISEQ_ELEMENT_INSN:
4054 insn_id = iobj->insn_id;
4062 if (state != SCS_AX) {
4063 NODE dummy_line_node = generate_dummy_line_node(0, -1);
4065 new_insn_body(iseq, &dummy_line_node, BIN(reput), 0);
4076 if (state == SCS_AB || state == SCS_BA) {
4077 state = (state == SCS_AB ? SCS_BA : SCS_AB);
4101 COMPILE_ERROR(iseq, iobj->insn_info.line_no,
4114 state = insn_set_sc_state(iseq, anchor, iobj, state);
4117 case ISEQ_ELEMENT_LABEL:
4120 lobj = (
LABEL *)list;
4122 state = label_set_sc_state(lobj, state);
4134all_string_result_p(
const NODE *node)
4136 if (!node)
return FALSE;
4137 switch (nd_type(node)) {
4138 case NODE_STR:
case NODE_DSTR:
4140 case NODE_IF:
case NODE_UNLESS:
4141 if (!node->nd_body || !node->nd_else)
return FALSE;
4142 if (all_string_result_p(node->nd_body))
4143 return all_string_result_p(node->nd_else);
4145 case NODE_AND:
case NODE_OR:
4147 return all_string_result_p(node->nd_1st);
4148 if (!all_string_result_p(node->nd_1st))
4150 return all_string_result_p(node->nd_2nd);
4159 const NODE *list = node->nd_next;
4160 VALUE lit = node->nd_lit;
4164 debugp_param(
"nd_lit", lit);
4168 COMPILE_ERROR(ERROR_ARGS
"dstr: must be string: %s",
4169 rb_builtin_type_name(
TYPE(lit)));
4172 lit = rb_fstring(lit);
4173 ADD_INSN1(ret, node, putobject, lit);
4175 if (
RSTRING_LEN(lit) == 0) first_lit = LAST_ELEMENT(ret);
4179 const NODE *
const head = list->nd_head;
4180 if (nd_type_p(head, NODE_STR)) {
4181 lit = rb_fstring(head->nd_lit);
4182 ADD_INSN1(ret, head, putobject, lit);
4187 CHECK(COMPILE(ret,
"each string", head));
4190 list = list->nd_next;
4192 if (
NIL_P(lit) && first_lit) {
4193 ELEM_REMOVE(first_lit);
4204 while (node && nd_type_p(node, NODE_BLOCK)) {
4205 CHECK(COMPILE_(ret,
"BLOCK body", node->nd_head,
4206 (node->nd_next ? 1 : popped)));
4207 node = node->nd_next;
4210 CHECK(COMPILE_(ret,
"BLOCK next", node->nd_next, popped));
4219 if (!node->nd_next) {
4220 VALUE lit = rb_fstring(node->nd_lit);
4221 ADD_INSN1(ret, node, putstring, lit);
4225 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt));
4226 ADD_INSN1(ret, node, concatstrings,
INT2FIX(cnt));
4235 CHECK(compile_dstr_fragments(iseq, ret, node, &cnt));
4236 ADD_INSN2(ret, node, toregexp,
INT2FIX(node->nd_cflag),
INT2FIX(cnt));
4244 const int line = nd_line(node);
4245 LABEL *lend = NEW_LABEL(line);
4246 rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
4247 + VM_SVAR_FLIPFLOP_START;
4250 ADD_INSN2(ret, node, getspecial, key,
INT2FIX(0));
4251 ADD_INSNL(ret, node, branchif, lend);
4254 CHECK(COMPILE(ret,
"flip2 beg", node->nd_beg));
4255 ADD_INSNL(ret, node, branchunless, else_label);
4256 ADD_INSN1(ret, node, putobject,
Qtrue);
4257 ADD_INSN1(ret, node, setspecial, key);
4259 ADD_INSNL(ret, node, jump, then_label);
4263 ADD_LABEL(ret, lend);
4264 CHECK(COMPILE(ret,
"flip2 end", node->nd_end));
4265 ADD_INSNL(ret, node, branchunless, then_label);
4266 ADD_INSN1(ret, node, putobject,
Qfalse);
4267 ADD_INSN1(ret, node, setspecial, key);
4268 ADD_INSNL(ret, node, jump, then_label);
4278 switch (nd_type(cond)) {
4281 LABEL *label = NEW_LABEL(nd_line(cond));
4282 CHECK(compile_branch_condition(iseq, ret, cond->nd_1st, label,
4284 if (!label->refcnt) {
4285 ADD_INSN(ret, cond, putnil);
4288 ADD_LABEL(ret, label);
4289 cond = cond->nd_2nd;
4294 LABEL *label = NEW_LABEL(nd_line(cond));
4295 CHECK(compile_branch_condition(iseq, ret, cond->nd_1st, then_label,
4297 if (!label->refcnt) {
4298 ADD_INSN(ret, cond, putnil);
4301 ADD_LABEL(ret, label);
4302 cond = cond->nd_2nd;
4311 ADD_INSNL(ret, cond, jump, then_label);
4316 ADD_INSNL(ret, cond, jump, else_label);
4322 CHECK(COMPILE_POPPED(ret,
"branch condition", cond));
4323 ADD_INSNL(ret, cond, jump, then_label);
4326 CHECK(compile_flip_flop(iseq, ret, cond, TRUE, then_label, else_label));
4329 CHECK(compile_flip_flop(iseq, ret, cond, FALSE, then_label, else_label));
4332 CHECK(compile_defined_expr(iseq, ret, cond,
Qfalse));
4335 CHECK(COMPILE(ret,
"branch condition", cond));
4339 ADD_INSNL(ret, cond, branchunless, else_label);
4340 ADD_INSNL(ret, cond, jump, then_label);
4347keyword_node_p(
const NODE *
const node)
4349 return nd_type_p(node, NODE_HASH) && (node->nd_brace & HASH_BRACE) != HASH_BRACE;
4354 const NODE *
const root_node,
4358 if (kw_arg_ptr == NULL)
return FALSE;
4360 if (root_node->nd_head && nd_type_p(root_node->nd_head, NODE_LIST)) {
4361 const NODE *node = root_node->nd_head;
4365 const NODE *key_node = node->nd_head;
4368 assert(nd_type_p(node, NODE_LIST));
4369 if (key_node && nd_type_p(key_node, NODE_LIT) &&
SYMBOL_P(key_node->nd_lit)) {
4374 *flag |= VM_CALL_KW_SPLAT;
4375 if (seen_nodes > 1 || node->nd_next->nd_next) {
4380 *flag |= VM_CALL_KW_SPLAT_MUT;
4385 node = node->nd_next;
4386 node = node->nd_next;
4390 node = root_node->nd_head;
4392 int len = (int)node->nd_alen / 2;
4395 VALUE *keywords = kw_arg->keywords;
4397 kw_arg->keyword_len = len;
4399 *kw_arg_ptr = kw_arg;
4401 for (i=0; node != NULL; i++, node = node->nd_next->nd_next) {
4402 const NODE *key_node = node->nd_head;
4403 const NODE *val_node = node->nd_next->nd_head;
4404 keywords[i] = key_node->nd_lit;
4405 NO_CHECK(COMPILE(ret,
"keyword values", val_node));
4420 for (; node; len++, node = node->nd_next) {
4422 EXPECT_NODE(
"compile_args", node, NODE_LIST, -1);
4425 if (node->nd_next == NULL && keyword_node_p(node->nd_head)) {
4426 if (compile_keyword_arg(iseq, ret, node->nd_head, keywords_ptr, flag)) {
4430 compile_hash(iseq, ret, node->nd_head, TRUE, FALSE);
4434 NO_CHECK(COMPILE_(ret,
"array element", node->nd_head, FALSE));
4442static_literal_node_p(
const NODE *node,
const rb_iseq_t *iseq)
4444 switch (nd_type(node)) {
4451 return ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal;
4460 switch (nd_type(node)) {
4468 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
4470 VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq),
INT2FIX((
int)nd_line(node)));
4476 return rb_fstring(node->nd_lit);
4479 return node->nd_lit;
4486 const NODE *line_node = node;
4488 if (nd_type_p(node, NODE_ZLIST)) {
4490 ADD_INSN1(ret, line_node, newarray,
INT2FIX(0));
4495 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
4498 for (; node; node = node->nd_next) {
4499 NO_CHECK(COMPILE_(ret,
"array element", node->nd_head, popped));
4541 const int max_stack_len = 0x100;
4542 const int min_tmp_ary_len = 0x40;
4544 int first_chunk = 1;
4547#define FLUSH_CHUNK(newarrayinsn) \
4549 ADD_INSN1(ret, line_node, newarrayinsn, INT2FIX(stack_len)); \
4550 if (!first_chunk) ADD_INSN(ret, line_node, concatarray); \
4551 first_chunk = stack_len = 0; \
4558 if (static_literal_node_p(node->nd_head, iseq)) {
4560 const NODE *node_tmp = node->nd_next;
4561 for (; node_tmp && static_literal_node_p(node_tmp->nd_head, iseq); node_tmp = node_tmp->nd_next)
4564 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_ary_len) {
4566 VALUE ary = rb_ary_hidden_new(count);
4569 for (; count; count--, node = node->nd_next)
4570 rb_ary_push(ary, static_literal_value(node->nd_head, iseq));
4574 FLUSH_CHUNK(newarray);
4576 ADD_INSN1(ret, line_node, duparray, ary);
4580 ADD_INSN1(ret, line_node, putobject, ary);
4581 ADD_INSN(ret, line_node, concatarray);
4588 for (; count; count--, node = node->nd_next) {
4590 EXPECT_NODE(
"compile_array", node, NODE_LIST, -1);
4593 NO_CHECK(COMPILE_(ret,
"array element", node->nd_head, 0));
4596 if (!node->nd_next && keyword_node_p(node->nd_head)) {
4598 FLUSH_CHUNK(newarraykwsplat);
4603 if (stack_len >= max_stack_len) FLUSH_CHUNK(newarray);
4607 FLUSH_CHUNK(newarray);
4616 if (static_literal_node_p(node, iseq)) {
4617 VALUE ary = rb_ary_hidden_new(1);
4618 rb_ary_push(ary, static_literal_value(node, iseq));
4621 ADD_INSN1(ret, node, duparray, ary);
4624 CHECK(COMPILE_(ret,
"array element", node, FALSE));
4625 if (keyword_node_p(node)) {
4626 ADD_INSN1(ret, node, newarraykwsplat,
INT2FIX(1));
4629 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
4637static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
4639 return node->nd_head && static_literal_node_p(node->nd_head, iseq) && static_literal_node_p(node->nd_next->nd_head, iseq);
4645 const NODE *line_node = node;
4647 node = node->nd_head;
4649 if (!node || nd_type_p(node, NODE_ZLIST)) {
4651 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
4656 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
4659 for (; node; node = node->nd_next) {
4660 NO_CHECK(COMPILE_(ret,
"hash element", node->nd_head, popped));
4683 const int max_stack_len = 0x100;
4684 const int min_tmp_hash_len = 0x800;
4686 int first_chunk = 1;
4687 DECL_ANCHOR(anchor);
4688 INIT_ANCHOR(anchor);
4691#define FLUSH_CHUNK() \
4693 if (first_chunk) { \
4694 APPEND_LIST(ret, anchor); \
4695 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
4698 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
4699 ADD_INSN(ret, line_node, swap); \
4700 APPEND_LIST(ret, anchor); \
4701 ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
4703 INIT_ANCHOR(anchor); \
4704 first_chunk = stack_len = 0; \
4711 if (static_literal_node_pair_p(node, iseq)) {
4713 const NODE *node_tmp = node->nd_next->nd_next;
4714 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = node_tmp->nd_next->nd_next)
4717 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
4719 VALUE ary = rb_ary_hidden_new(count);
4722 for (; count; count--, node = node->nd_next->nd_next) {
4724 elem[0] = static_literal_value(node->nd_head, iseq);
4725 elem[1] = static_literal_value(node->nd_next->nd_head, iseq);
4726 rb_ary_cat(ary, elem, 2);
4736 ADD_INSN1(ret, line_node, duphash, hash);
4740 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4741 ADD_INSN(ret, line_node, swap);
4743 ADD_INSN1(ret, line_node, putobject, hash);
4745 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
4752 for (; count; count--, node = node->nd_next->nd_next) {
4755 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
4758 if (node->nd_head) {
4760 NO_CHECK(COMPILE_(anchor,
"hash key element", node->nd_head, 0));
4761 NO_CHECK(COMPILE_(anchor,
"hash value element", node->nd_next->nd_head, 0));
4765 if (stack_len >= max_stack_len) FLUSH_CHUNK();
4771 const NODE *kw = node->nd_next->nd_head;
4772 int empty_kw = nd_type_p(kw, NODE_LIT) &&
RB_TYPE_P(kw->nd_lit,
T_HASH);
4773 int first_kw = first_chunk && stack_len == 0;
4774 int last_kw = !node->nd_next->nd_next;
4775 int only_kw = last_kw && first_kw;
4778 if (only_kw && method_call_keywords) {
4786 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
4788 else if (first_kw) {
4792 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
4799 if (only_kw && method_call_keywords) {
4805 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
4812 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4813 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
4814 else ADD_INSN(ret, line_node, swap);
4816 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
4818 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
4833rb_node_case_when_optimizable_literal(
const NODE *
const node)
4835 switch (nd_type(node)) {
4837 VALUE v = node->nd_lit;
4858 return rb_fstring(node->nd_lit);
4865 LABEL *l1,
int only_special_literals,
VALUE literals)
4868 const NODE *val = vals->nd_head;
4869 VALUE lit = rb_node_case_when_optimizable_literal(val);
4872 only_special_literals = 0;
4874 else if (
NIL_P(rb_hash_lookup(literals, lit))) {
4875 rb_hash_aset(literals, lit, (
VALUE)(l1) | 1);
4878 if (nd_type_p(val, NODE_STR)) {
4879 debugp_param(
"nd_lit", val->nd_lit);
4880 lit = rb_fstring(val->nd_lit);
4881 ADD_INSN1(cond_seq, val, putobject, lit);
4885 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
4889 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
4890 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
4891 ADD_INSNL(cond_seq, val, branchif, l1);
4892 vals = vals->nd_next;
4894 return only_special_literals;
4899 LABEL *l1,
int only_special_literals,
VALUE literals)
4901 const NODE *line_node = vals;
4903 switch (nd_type(vals)) {
4905 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
4909 ADD_INSN (cond_seq, line_node, dup);
4910 CHECK(COMPILE(cond_seq,
"when splat", vals->nd_head));
4911 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
4912 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
4913 ADD_INSNL(cond_seq, line_node, branchif, l1);
4916 CHECK(when_splat_vals(iseq, cond_seq, vals->nd_head, l1, only_special_literals, literals));
4917 CHECK(when_splat_vals(iseq, cond_seq, vals->nd_body, l1, only_special_literals, literals));
4920 CHECK(when_splat_vals(iseq, cond_seq, vals->nd_head, l1, only_special_literals, literals));
4921 ADD_INSN (cond_seq, line_node, dup);
4922 CHECK(COMPILE(cond_seq,
"when argspush body", vals->nd_body));
4923 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
4924 ADD_INSNL(cond_seq, line_node, branchif, l1);
4927 ADD_INSN (cond_seq, line_node, dup);
4928 CHECK(COMPILE(cond_seq,
"when val", vals));
4929 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
4930 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
4931 ADD_INSNL(cond_seq, line_node, branchif, l1);
5024 const NODE *line_node;
5039add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc,
INSN *before_insn)
5042 rb_bug(
"no masgn_state");
5051 memo->before_insn = before_insn;
5052 memo->line_node = line_node;
5053 memo->argn = state->num_args + 1;
5054 memo->num_args = argc;
5055 state->num_args += argc;
5056 memo->lhs_pos = lhs_pos;
5058 if (!state->first_memo) {
5059 state->first_memo = memo;
5062 state->last_memo->next = memo;
5064 state->last_memo = memo;
5074 switch (nd_type(node)) {
5075 case NODE_ATTRASGN: {
5077 const NODE *line_node = node;
5079 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5081 bool safenav_call =
false;
5083 iobj = (
INSN *)get_prev_insn((
INSN *)insn_element);
5085 ELEM_REMOVE(insn_element);
5086 if (!IS_INSN_ID(iobj, send)) {
5087 safenav_call =
true;
5088 iobj = (
INSN *)get_prev_insn(iobj);
5089 ELEM_INSERT_NEXT(&iobj->link, insn_element);
5091 (pre->last = iobj->link.prev)->next = 0;
5094 int argc = vm_ci_argc(ci) + 1;
5095 ci = ci_argc_set(iseq, ci, argc);
5096 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5100 ADD_INSN(lhs, line_node, swap);
5103 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5106 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (
INSN *)LAST_ELEMENT(lhs))) {
5110 iobj->link.prev = lhs->last;
5111 lhs->last->next = &iobj->link;
5112 for (lhs->last = &iobj->link; lhs->last->next; lhs->last = lhs->last->next);
5113 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5114 int argc = vm_ci_argc(ci);
5115 ci = ci_argc_set(iseq, ci, argc - 1);
5116 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5118 INSERT_BEFORE_INSN1(iobj, line_node, newarray,
INT2FIX(1));
5119 INSERT_BEFORE_INSN(iobj, line_node, concatarray);
5121 if (!safenav_call) {
5122 ADD_INSN(lhs, line_node, pop);
5124 ADD_INSN(lhs, line_node, pop);
5127 for (
int i=0; i < argc; i++) {
5128 ADD_INSN(post, line_node, pop);
5133 DECL_ANCHOR(nest_rhs);
5134 INIT_ANCHOR(nest_rhs);
5135 DECL_ANCHOR(nest_lhs);
5136 INIT_ANCHOR(nest_lhs);
5138 int prev_level = state->lhs_level;
5139 bool prev_nested = state->nested;
5141 state->lhs_level = lhs_pos - 1;
5142 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5143 state->lhs_level = prev_level;
5144 state->nested = prev_nested;
5146 ADD_SEQ(lhs, nest_rhs);
5147 ADD_SEQ(lhs, nest_lhs);
5151 if (!node->nd_vid) {
5155 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5158 iobj = (
INSN *)insn_element;
5161 ELEM_REMOVE(insn_element);
5162 pre->last = iobj->link.prev;
5165 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (
INSN *)LAST_ELEMENT(lhs))) {
5169 ADD_INSN(post, node, pop);
5174 DECL_ANCHOR(anchor);
5175 INIT_ANCHOR(anchor);
5176 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
5177 ELEM_REMOVE(FIRST_ELEMENT(anchor));
5178 ADD_SEQ(lhs, anchor);
5189 CHECK(compile_massign_opt_lhs(iseq, ret, lhsn->nd_next));
5190 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, lhsn->nd_head, NULL, 0));
5197 const NODE *rhsn,
const NODE *orig_lhsn)
5200 const int memsize = numberof(mem);
5202 int llen = 0, rlen = 0;
5204 const NODE *lhsn = orig_lhsn;
5206#define MEMORY(v) { \
5208 if (memindex == memsize) return 0; \
5209 for (i=0; i<memindex; i++) { \
5210 if (mem[i] == (v)) return 0; \
5212 mem[memindex++] = (v); \
5215 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
5220 const NODE *ln = lhsn->nd_head;
5221 switch (nd_type(ln)) {
5233 lhsn = lhsn->nd_next;
5239 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", rhsn->nd_head));
5242 NO_CHECK(COMPILE(ret,
"masgn val", rhsn->nd_head));
5244 rhsn = rhsn->nd_next;
5249 for (i=0; i<llen-rlen; i++) {
5250 ADD_INSN(ret, orig_lhsn, putnil);
5254 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
5261 const NODE *rhsn = node->nd_value;
5262 const NODE *splatn = node->nd_args;
5263 const NODE *lhsn = node->nd_head;
5264 const NODE *lhsn_count = lhsn;
5265 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
5271 while (lhsn_count) {
5273 lhsn_count = lhsn_count->nd_next;
5276 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, lhsn->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
5278 lhsn = lhsn->nd_next;
5282 if (nd_type_p(splatn, NODE_POSTARG)) {
5284 const NODE *postn = splatn->nd_2nd;
5285 const NODE *restn = splatn->nd_1st;
5286 int plen = (int)postn->nd_alen;
5288 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
5290 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
5292 if (NODE_NAMED_REST_P(restn)) {
5293 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
5296 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, postn->nd_head, state, (plen - ppos) + state->lhs_level));
5298 postn = postn->nd_next;
5303 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
5308 if (!state->nested) {
5309 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
5313 ADD_INSN(rhs, node, dup);
5316 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
5324 if (!popped || node->nd_args || !compile_massign_opt(iseq, ret, node->nd_value, node->nd_head)) {
5326 state.lhs_level = popped ? 0 : 1;
5329 state.first_memo = NULL;
5330 state.last_memo = NULL;
5340 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
5344 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
5345 for (
int i = 0; i < memo->num_args; i++) {
5346 INSERT_BEFORE_INSN1(memo->before_insn, memo->line_node, topn, topn_arg);
5348 tmp_memo = memo->next;
5357 if (!popped && state.num_args >= 1) {
5359 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
5369 VALUE arr = rb_ary_new();
5371 switch (nd_type(node)) {
5373 rb_ary_unshift(arr,
ID2SYM(node->nd_vid));
5376 rb_ary_unshift(arr,
ID2SYM(node->nd_mid));
5377 rb_ary_unshift(arr,
ID2SYM(idNULL));
5380 rb_ary_unshift(arr,
ID2SYM(node->nd_mid));
5381 node = node->nd_head;
5390compile_const_prefix(
rb_iseq_t *iseq,
const NODE *
const node,
5393 switch (nd_type(node)) {
5395 debugi(
"compile_const_prefix - colon", node->nd_vid);
5396 ADD_INSN1(body, node, putobject,
Qtrue);
5397 ADD_INSN1(body, node, getconstant,
ID2SYM(node->nd_vid));
5400 debugi(
"compile_const_prefix - colon3", node->nd_mid);
5401 ADD_INSN(body, node, pop);
5402 ADD_INSN1(body, node, putobject, rb_cObject);
5403 ADD_INSN1(body, node, putobject,
Qtrue);
5404 ADD_INSN1(body, node, getconstant,
ID2SYM(node->nd_mid));
5407 CHECK(compile_const_prefix(iseq, node->nd_head, pref, body));
5408 debugi(
"compile_const_prefix - colon2", node->nd_mid);
5409 ADD_INSN1(body, node, putobject,
Qfalse);
5410 ADD_INSN1(body, node, getconstant,
ID2SYM(node->nd_mid));
5413 CHECK(COMPILE(pref,
"const colon2 prefix", node));
5422 if (nd_type_p(cpath, NODE_COLON3)) {
5424 ADD_INSN1(ret, cpath, putobject, rb_cObject);
5425 return VM_DEFINECLASS_FLAG_SCOPED;
5427 else if (cpath->nd_head) {
5429 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", cpath->nd_head));
5430 return VM_DEFINECLASS_FLAG_SCOPED;
5434 ADD_INSN1(ret, cpath, putspecialobject,
5435 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5441private_recv_p(
const NODE *node)
5443 if (nd_type_p(node->nd_recv, NODE_SELF)) {
5444 NODE *self = node->nd_recv;
5445 return self->nd_state != 0;
5455compile_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);
5462 enum defined_type expr_type = DEFINED_NOT_DEFINED;
5463 enum node_type
type;
5464 const int line = nd_line(node);
5465 const NODE *line_node = node;
5467 switch (
type = nd_type(node)) {
5471 expr_type = DEFINED_NIL;
5474 expr_type = DEFINED_SELF;
5477 expr_type = DEFINED_TRUE;
5480 expr_type = DEFINED_FALSE;
5484 const NODE *vals = node;
5487 defined_expr0(iseq, ret, vals->nd_head, lfinish,
Qfalse,
false);
5490 lfinish[1] = NEW_LABEL(line);
5492 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5493 }
while ((vals = vals->nd_next) != NULL);
5502 expr_type = DEFINED_EXPR;
5508 expr_type = DEFINED_LVAR;
5511#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
5513 ADD_INSN(ret, line_node, putnil);
5514 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_IVAR),
5515 ID2SYM(node->nd_vid), PUSH_VAL(DEFINED_IVAR));
5519 ADD_INSN(ret, line_node, putnil);
5520 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
5521 ID2SYM(node->nd_entry), PUSH_VAL(DEFINED_GVAR));
5525 ADD_INSN(ret, line_node, putnil);
5526 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
5527 ID2SYM(node->nd_vid), PUSH_VAL(DEFINED_CVAR));
5531 ADD_INSN(ret, line_node, putnil);
5532 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
5533 ID2SYM(node->nd_vid), PUSH_VAL(DEFINED_CONST));
5537 lfinish[1] = NEW_LABEL(line);
5539 defined_expr0(iseq, ret, node->nd_head, lfinish,
Qfalse,
false);
5540 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5541 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", node->nd_head));
5544 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
5545 ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_CONST));
5548 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
5549 ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_METHOD));
5553 ADD_INSN1(ret, line_node, putobject, rb_cObject);
5554 ADD_INSN3(ret, line_node, defined,
5555 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_CONST));
5563 case NODE_ATTRASGN:{
5564 const int explicit_receiver =
5565 (
type == NODE_CALL ||
type == NODE_OPCALL ||
5566 (
type == NODE_ATTRASGN && !private_recv_p(node)));
5568 if (node->nd_args || explicit_receiver) {
5570 lfinish[1] = NEW_LABEL(line);
5573 lfinish[2] = NEW_LABEL(line);
5576 if (node->nd_args) {
5577 defined_expr0(iseq, ret, node->nd_args, lfinish,
Qfalse,
false);
5578 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5580 if (explicit_receiver) {
5581 defined_expr0(iseq, ret, node->nd_recv, lfinish,
Qfalse,
true);
5582 switch (nd_type(node->nd_recv)) {
5588 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
5589 compile_call(iseq, ret, node->nd_recv, nd_type(node->nd_recv), line_node, 0,
true);
5592 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5593 NO_CHECK(COMPILE(ret,
"defined/recv", node->nd_recv));
5597 ADD_INSN(ret, line_node, dup);
5599 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
5600 ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_METHOD));
5603 ADD_INSN(ret, line_node, putself);
5605 ADD_INSN(ret, line_node, dup);
5607 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
5608 ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_METHOD));
5614 ADD_INSN(ret, line_node, putnil);
5615 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
5616 PUSH_VAL(DEFINED_YIELD));
5621 ADD_INSN(ret, line_node, putnil);
5622 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
5623 INT2FIX((node->nd_nth << 1) | (
type == NODE_BACK_REF)),
5624 PUSH_VAL(DEFINED_GVAR));
5629 ADD_INSN(ret, line_node, putnil);
5630 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
5631 PUSH_VAL(DEFINED_ZSUPER));
5637 case NODE_OP_ASGN_OR:
5638 case NODE_OP_ASGN_AND:
5646 expr_type = DEFINED_ASGN;
5650 assert(expr_type != DEFINED_NOT_DEFINED);
5653 VALUE str = rb_iseq_defined_string(expr_type);
5654 ADD_INSN1(ret, line_node, putobject, str);
5657 ADD_INSN1(ret, line_node, putobject,
Qtrue);
5664 NODE dummy_line_node = generate_dummy_line_node(0, -1);
5665 ADD_INSN(ret, &dummy_line_node, putnil);
5666 iseq_set_exception_local_table(iseq);
5674 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
5676 int line = nd_line(node);
5677 LABEL *lstart = NEW_LABEL(line);
5678 LABEL *lend = NEW_LABEL(line);
5681 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
5682 rescue = new_child_iseq_with_callback(iseq, ifunc,
5684 ISEQ_BODY(iseq)->location.label),
5685 iseq, ISEQ_TYPE_RESCUE, 0);
5686 lstart->rescued = LABEL_RESCUE_BEG;
5687 lend->rescued = LABEL_RESCUE_END;
5688 APPEND_LABEL(ret, lcur, lstart);
5689 ADD_LABEL(ret, lend);
5690 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
5697 const int line = nd_line(node);
5698 const NODE *line_node = node;
5699 if (!node->nd_head) {
5700 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
5701 ADD_INSN1(ret, line_node, putobject, str);
5706 lfinish[0] = NEW_LABEL(line);
5709 defined_expr(iseq, ret, node->nd_head, lfinish, needstr);
5711 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, line_node, BIN(putnil), 0)->link);
5712 ADD_INSN(ret, line_node, swap);
5714 ADD_LABEL(ret, lfinish[2]);
5716 ADD_INSN(ret, line_node, pop);
5717 ADD_LABEL(ret, lfinish[1]);
5719 ADD_LABEL(ret, lfinish[0]);
5725make_name_for_block(
const rb_iseq_t *orig_iseq)
5730 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
5731 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
5732 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
5735 iseq = ISEQ_BODY(iseq)->parent_iseq;
5740 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
5743 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
5752 enl->ensure_node = node;
5753 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
5755 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
5765 while (erange->next != 0) {
5766 erange = erange->next;
5770 ne->end = erange->end;
5771 erange->end = lstart;
5777can_add_ensure_iseq(
const rb_iseq_t *iseq)
5780 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
5782 if (e->ensure_node)
return false;
5792 assert(can_add_ensure_iseq(iseq));
5795 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
5797 DECL_ANCHOR(ensure);
5799 INIT_ANCHOR(ensure);
5801 if (enlp->erange != NULL) {
5802 DECL_ANCHOR(ensure_part);
5803 LABEL *lstart = NEW_LABEL(0);
5804 LABEL *lend = NEW_LABEL(0);
5805 INIT_ANCHOR(ensure_part);
5807 add_ensure_range(iseq, enlp->erange, lstart, lend);
5809 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
5810 ADD_LABEL(ensure_part, lstart);
5811 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
5812 ADD_LABEL(ensure_part, lend);
5813 ADD_SEQ(ensure, ensure_part);
5822 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
5823 ADD_SEQ(ret, ensure);
5827check_keyword(
const NODE *node)
5831 if (nd_type_p(node, NODE_LIST)) {
5832 while (node->nd_next) {
5833 node = node->nd_next;
5835 node = node->nd_head;
5838 return keyword_node_p(node);
5846 switch (nd_type(argn)) {
5848 NO_CHECK(COMPILE(args,
"args (splat)", argn->nd_head));
5849 ADD_INSN1(args, argn, splatarray, RBOOL(dup_rest));
5850 if (flag) *flag |= VM_CALL_ARGS_SPLAT;
5854 case NODE_ARGSPUSH: {
5855 int next_is_list = (nd_type_p(argn->nd_head, NODE_LIST));
5856 VALUE argc = setup_args_core(iseq, args, argn->nd_head, 1, NULL, NULL);
5857 if (nd_type_p(argn->nd_body, NODE_LIST)) {
5859 int rest_len = compile_args(iseq, args, argn->nd_body, NULL, NULL);
5860 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
5863 NO_CHECK(COMPILE(args,
"args (cat: splat)", argn->nd_body));
5866 *flag |= VM_CALL_ARGS_SPLAT;
5869 if (check_keyword(argn->nd_body))
5870 *flag |= VM_CALL_KW_SPLAT;
5872 if (nd_type_p(argn, NODE_ARGSCAT)) {
5874 ADD_INSN1(args, argn, splatarray,
Qtrue);
5878 ADD_INSN1(args, argn, splatarray,
Qfalse);
5879 ADD_INSN(args, argn, concatarray);
5884 ADD_INSN1(args, argn, newarray,
INT2FIX(1));
5885 ADD_INSN(args, argn, concatarray);
5890 int len = compile_args(iseq, args, argn, keywords, flag);
5894 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
5906 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
5907 unsigned int dup_rest = 1;
5908 DECL_ANCHOR(arg_block);
5909 INIT_ANCHOR(arg_block);
5910 NO_CHECK(COMPILE(arg_block,
"block", argn->nd_body));
5912 *flag |= VM_CALL_ARGS_BLOCKARG;
5914 if (LIST_INSN_SIZE_ONE(arg_block)) {
5916 if (IS_INSN(elem)) {
5918 if (iobj->insn_id == BIN(getblockparam)) {
5919 iobj->insn_id = BIN(getblockparamproxy);
5924 ret = setup_args_core(iseq, args, argn->nd_head, dup_rest, flag, keywords);
5925 ADD_SEQ(args, arg_block);
5928 ret = setup_args_core(iseq, args, argn, 0, flag, keywords);
5936 const NODE *body = ptr;
5937 int line = nd_line(body);
5939 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
5941 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5942 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
5944 iseq_set_local_table(iseq, 0);
5952 int line = nd_line(node);
5953 const NODE *line_node = node;
5954 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
5956#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
5957 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
5961 ADD_INSN(ret, line_node, dup);
5962 ADD_INSNL(ret, line_node, branchunless, fail_label);
5964 for (vars = node; vars; vars = vars->nd_next) {
5966 if (vars->nd_next) {
5967 ADD_INSN(ret, line_node, dup);
5970 NO_CHECK(COMPILE_POPPED(ret,
"capture", vars->nd_head));
5972 cap = new_insn_send(iseq, line_node, idAREF,
INT2FIX(1),
5975#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
5976 if (!vars->nd_next && vars == node) {
5981 ADD_INSNL(nom, line_node, jump, end_label);
5982 ADD_LABEL(nom, fail_label);
5984 ADD_INSN(nom, line_node, pop);
5985 ADD_INSN(nom, line_node, putnil);
5987 ADD_LABEL(nom, end_label);
5988 (nom->last->next = cap->link.next)->prev = nom->last;
5989 (cap->link.next = nom->anchor.next)->prev = &cap->link;
5994 ADD_INSNL(ret, line_node, jump, end_label);
5995 ADD_LABEL(ret, fail_label);
5996 ADD_INSN(ret, line_node, pop);
5997 for (vars = node; vars; vars = vars->nd_next) {
5999 NO_CHECK(COMPILE_POPPED(ret,
"capture", vars->nd_head));
6001 ((
INSN*)last)->insn_id = BIN(putnil);
6002 ((
INSN*)last)->operand_size = 0;
6004 ADD_LABEL(ret, end_label);
6008optimizable_range_item_p(
const NODE *n)
6010 if (!n)
return FALSE;
6011 switch (nd_type(n)) {
6025 const NODE *
const node_body =
type == NODE_IF ? node->nd_body : node->nd_else;
6026 const NODE *
const node_else =
type == NODE_IF ? node->nd_else : node->nd_body;
6028 const int line = nd_line(node);
6029 const NODE *line_node = node;
6030 DECL_ANCHOR(cond_seq);
6031 DECL_ANCHOR(then_seq);
6032 DECL_ANCHOR(else_seq);
6033 LABEL *then_label, *else_label, *end_label;
6036 VALUE catch_table = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
6037 long catch_table_size =
NIL_P(catch_table) ? 0 :
RARRAY_LEN(catch_table);
6039 INIT_ANCHOR(cond_seq);
6040 INIT_ANCHOR(then_seq);
6041 INIT_ANCHOR(else_seq);
6042 then_label = NEW_LABEL(line);
6043 else_label = NEW_LABEL(line);
6046 compile_branch_condition(iseq, cond_seq, node->nd_cond,
6047 then_label, else_label);
6049 ci_size = body->ci_size;
6050 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
6051 catch_table = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
6052 if (!then_label->refcnt) {
6053 body->ci_size = ci_size;
6054 if (!
NIL_P(catch_table)) rb_ary_set_len(catch_table, catch_table_size);
6060 ci_size = body->ci_size;
6061 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
6062 catch_table = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
6063 if (!else_label->refcnt) {
6064 body->ci_size = ci_size;
6065 if (!
NIL_P(catch_table)) rb_ary_set_len(catch_table, catch_table_size);
6071 ADD_SEQ(ret, cond_seq);
6073 if (then_label->refcnt && else_label->refcnt) {
6074 branches = decl_branch_base(iseq, node,
type == NODE_IF ?
"if" :
"unless");
6077 if (then_label->refcnt) {
6078 ADD_LABEL(ret, then_label);
6079 if (else_label->refcnt) {
6080 add_trace_branch_coverage(
6083 node_body ? node_body : node,
6085 type == NODE_IF ?
"then" :
"else",
6087 end_label = NEW_LABEL(line);
6088 ADD_INSNL(then_seq, line_node, jump, end_label);
6090 ADD_INSN(then_seq, line_node, pop);
6093 ADD_SEQ(ret, then_seq);
6096 if (else_label->refcnt) {
6097 ADD_LABEL(ret, else_label);
6098 if (then_label->refcnt) {
6099 add_trace_branch_coverage(
6102 node_else ? node_else : node,
6104 type == NODE_IF ?
"else" :
"then",
6107 ADD_SEQ(ret, else_seq);
6111 ADD_LABEL(ret, end_label);
6121 const NODE *node = orig_node;
6122 LABEL *endlabel, *elselabel;
6124 DECL_ANCHOR(body_seq);
6125 DECL_ANCHOR(cond_seq);
6126 int only_special_literals = 1;
6127 VALUE literals = rb_hash_new();
6129 enum node_type
type;
6130 const NODE *line_node;
6135 INIT_ANCHOR(body_seq);
6136 INIT_ANCHOR(cond_seq);
6138 RHASH_TBL_RAW(literals)->type = &cdhash_type;
6140 CHECK(COMPILE(head,
"case base", node->nd_head));
6142 branches = decl_branch_base(iseq, node,
"case");
6144 node = node->nd_body;
6145 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
6146 type = nd_type(node);
6147 line = nd_line(node);
6150 endlabel = NEW_LABEL(line);
6151 elselabel = NEW_LABEL(line);
6155 while (
type == NODE_WHEN) {
6158 l1 = NEW_LABEL(line);
6159 ADD_LABEL(body_seq, l1);
6160 ADD_INSN(body_seq, line_node, pop);
6161 add_trace_branch_coverage(
6164 node->nd_body ? node->nd_body : node,
6168 CHECK(COMPILE_(body_seq,
"when body", node->nd_body, popped));
6169 ADD_INSNL(body_seq, line_node, jump, endlabel);
6171 vals = node->nd_head;
6173 switch (nd_type(vals)) {
6175 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
6176 if (only_special_literals < 0)
return COMPILE_NG;
6181 only_special_literals = 0;
6182 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
6185 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
6189 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
6192 node = node->nd_next;
6196 type = nd_type(node);
6197 line = nd_line(node);
6202 ADD_LABEL(cond_seq, elselabel);
6203 ADD_INSN(cond_seq, line_node, pop);
6204 add_trace_branch_coverage(iseq, cond_seq, node, branch_id,
"else", branches);
6205 CHECK(COMPILE_(cond_seq,
"else", node, popped));
6206 ADD_INSNL(cond_seq, line_node, jump, endlabel);
6209 debugs(
"== else (implicit)\n");
6210 ADD_LABEL(cond_seq, elselabel);
6211 ADD_INSN(cond_seq, orig_node, pop);
6212 add_trace_branch_coverage(iseq, cond_seq, orig_node, branch_id,
"else", branches);
6214 ADD_INSN(cond_seq, orig_node, putnil);
6216 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
6219 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
6220 ADD_INSN(ret, orig_node, dup);
6221 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
6223 LABEL_REF(elselabel);
6226 ADD_SEQ(ret, cond_seq);
6227 ADD_SEQ(ret, body_seq);
6228 ADD_LABEL(ret, endlabel);
6237 const NODE *node = orig_node->nd_body;
6239 DECL_ANCHOR(body_seq);
6243 branches = decl_branch_base(iseq, orig_node,
"case");
6245 INIT_ANCHOR(body_seq);
6246 endlabel = NEW_LABEL(nd_line(node));
6248 while (node && nd_type_p(node, NODE_WHEN)) {
6249 const int line = nd_line(node);
6250 LABEL *l1 = NEW_LABEL(line);
6251 ADD_LABEL(body_seq, l1);
6252 add_trace_branch_coverage(
6255 node->nd_body ? node->nd_body : node,
6259 CHECK(COMPILE_(body_seq,
"when", node->nd_body, popped));
6260 ADD_INSNL(body_seq, node, jump, endlabel);
6262 vals = node->nd_head;
6264 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
6266 switch (nd_type(vals)) {
6270 val = vals->nd_head;
6271 lnext = NEW_LABEL(nd_line(val));
6272 debug_compile(
"== when2\n", (
void)0);
6273 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
6274 ADD_LABEL(ret, lnext);
6275 vals = vals->nd_next;
6281 ADD_INSN(ret, vals, putnil);
6282 CHECK(COMPILE(ret,
"when2/cond splat", vals));
6283 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
6284 ADD_INSNL(ret, vals, branchif, l1);
6287 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
6289 node = node->nd_next;
6292 add_trace_branch_coverage(
6295 node ? node : orig_node,
6299 CHECK(COMPILE_(ret,
"else", node, popped));
6300 ADD_INSNL(ret, orig_node, jump, endlabel);
6302 ADD_SEQ(ret, body_seq);
6303 ADD_LABEL(ret, endlabel);
6307static 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);
6309static 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);
6310static 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);
6311static int iseq_compile_pattern_set_general_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
6312static 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);
6313static int iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
6315#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
6316#define CASE3_BI_OFFSET_ERROR_STRING 1
6317#define CASE3_BI_OFFSET_KEY_ERROR_P 2
6318#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
6319#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
6322iseq_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)
6324 const int line = nd_line(node);
6325 const NODE *line_node = node;
6327 switch (nd_type(node)) {
6382 const NODE *args = apinfo->pre_args;
6383 const int pre_args_num = apinfo->pre_args ?
rb_long2int(apinfo->pre_args->nd_alen) : 0;
6384 const int post_args_num = apinfo->post_args ?
rb_long2int(apinfo->post_args->nd_alen) : 0;
6386 const int min_argc = pre_args_num + post_args_num;
6387 const int use_rest_num = apinfo->rest_arg && (NODE_NAMED_REST_P(apinfo->rest_arg) ||
6388 (!NODE_NAMED_REST_P(apinfo->rest_arg) && post_args_num > 0));
6390 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
6392 match_failed = NEW_LABEL(line);
6393 type_error = NEW_LABEL(line);
6394 deconstruct = NEW_LABEL(line);
6395 deconstructed = NEW_LABEL(line);
6398 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
6399 ADD_INSN(ret, line_node, swap);
6405 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
6407 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
6409 ADD_INSN(ret, line_node, dup);
6410 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6411 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
6412 ADD_SEND(ret, line_node, apinfo->rest_arg ? idGE : idEq,
INT2FIX(1));
6413 if (in_single_pattern) {
6414 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
6415 apinfo->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
6416 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
6417 INT2FIX(min_argc), base_index + 1 ));
6419 ADD_INSNL(ret, line_node, branchunless, match_failed);
6421 for (i = 0; i < pre_args_num; i++) {
6422 ADD_INSN(ret, line_node, dup);
6423 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
6424 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
6425 CHECK(iseq_compile_pattern_match(iseq, ret, args->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6426 args = args->nd_next;
6429 if (apinfo->rest_arg) {
6430 if (NODE_NAMED_REST_P(apinfo->rest_arg)) {
6431 ADD_INSN(ret, line_node, dup);
6432 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
6433 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
6434 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6435 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
6436 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
6437 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
6438 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
6440 CHECK(iseq_compile_pattern_match(iseq, ret, apinfo->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6443 if (post_args_num > 0) {
6444 ADD_INSN(ret, line_node, dup);
6445 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6446 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
6447 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
6448 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
6449 ADD_INSN(ret, line_node, pop);
6454 args = apinfo->post_args;
6455 for (i = 0; i < post_args_num; i++) {
6456 ADD_INSN(ret, line_node, dup);
6458 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
6459 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6460 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6462 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
6463 CHECK(iseq_compile_pattern_match(iseq, ret, args->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6464 args = args->nd_next;
6467 ADD_INSN(ret, line_node, pop);
6469 ADD_INSN(ret, line_node, pop);
6471 ADD_INSNL(ret, line_node, jump, matched);
6472 ADD_INSN(ret, line_node, putnil);
6474 ADD_INSN(ret, line_node, putnil);
6477 ADD_LABEL(ret, type_error);
6478 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6480 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
6481 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
6482 ADD_INSN(ret, line_node, pop);
6484 ADD_LABEL(ret, match_failed);
6485 ADD_INSN(ret, line_node, pop);
6487 ADD_INSN(ret, line_node, pop);
6489 ADD_INSNL(ret, line_node, jump, unmatched);
6543 const NODE *args = fpinfo->args;
6544 const int args_num = fpinfo->args ?
rb_long2int(fpinfo->args->nd_alen) : 0;
6546 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
6547 match_failed = NEW_LABEL(line);
6548 type_error = NEW_LABEL(line);
6549 deconstruct = NEW_LABEL(line);
6550 deconstructed = NEW_LABEL(line);
6552 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
6554 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
6556 ADD_INSN(ret, line_node, dup);
6557 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6558 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
6559 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
6560 if (in_single_pattern) {
6561 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 ));
6563 ADD_INSNL(ret, line_node, branchunless, match_failed);
6566 LABEL *while_begin = NEW_LABEL(nd_line(node));
6567 LABEL *next_loop = NEW_LABEL(nd_line(node));
6568 LABEL *find_succeeded = NEW_LABEL(line);
6569 LABEL *find_failed = NEW_LABEL(nd_line(node));
6572 ADD_INSN(ret, line_node, dup);
6573 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6575 ADD_INSN(ret, line_node, dup);
6576 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
6577 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
6579 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
6581 ADD_LABEL(ret, while_begin);
6583 ADD_INSN(ret, line_node, dup);
6584 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
6585 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
6586 ADD_INSNL(ret, line_node, branchunless, find_failed);
6588 for (j = 0; j < args_num; j++) {
6589 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6590 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
6592 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
6593 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6595 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
6597 CHECK(iseq_compile_pattern_match(iseq, ret, args->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
6598 args = args->nd_next;
6601 if (NODE_NAMED_REST_P(fpinfo->pre_rest_arg)) {
6602 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6603 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
6604 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
6605 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
6606 CHECK(iseq_compile_pattern_match(iseq, ret, fpinfo->pre_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
6608 if (NODE_NAMED_REST_P(fpinfo->post_rest_arg)) {
6609 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6610 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
6611 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
6612 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6613 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6614 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
6615 CHECK(iseq_compile_pattern_match(iseq, ret, fpinfo->post_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
6617 ADD_INSNL(ret, line_node, jump, find_succeeded);
6619 ADD_LABEL(ret, next_loop);
6620 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
6621 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6622 ADD_INSNL(ret, line_node, jump, while_begin);
6624 ADD_LABEL(ret, find_failed);
6625 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
6626 if (in_single_pattern) {
6627 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6628 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
6629 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
6630 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
6631 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
6633 ADD_INSN1(ret, line_node, putobject,
Qfalse);
6634 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
6636 ADD_INSN(ret, line_node, pop);
6637 ADD_INSN(ret, line_node, pop);
6639 ADD_INSNL(ret, line_node, jump, match_failed);
6640 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
6642 ADD_LABEL(ret, find_succeeded);
6643 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
6646 ADD_INSN(ret, line_node, pop);
6647 ADD_INSNL(ret, line_node, jump, matched);
6648 ADD_INSN(ret, line_node, putnil);
6650 ADD_LABEL(ret, type_error);
6651 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6653 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
6654 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
6655 ADD_INSN(ret, line_node, pop);
6657 ADD_LABEL(ret, match_failed);
6658 ADD_INSN(ret, line_node, pop);
6659 ADD_INSNL(ret, line_node, jump, unmatched);
6723 LABEL *match_failed, *type_error;
6726 match_failed = NEW_LABEL(line);
6727 type_error = NEW_LABEL(line);
6729 if (node->nd_pkwargs && !node->nd_pkwrestarg) {
6730 const NODE *kw_args = node->nd_pkwargs->nd_head;
6731 keys = rb_ary_new_capa(kw_args ? kw_args->nd_alen/2 : 0);
6733 rb_ary_push(keys, kw_args->nd_head->nd_lit);
6734 kw_args = kw_args->nd_next->nd_next;
6738 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
6740 ADD_INSN(ret, line_node, dup);
6741 ADD_INSN1(ret, line_node, putobject,
ID2SYM(
rb_intern(
"deconstruct_keys")));
6742 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
6743 if (in_single_pattern) {
6744 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
6746 ADD_INSNL(ret, line_node, branchunless, match_failed);
6749 ADD_INSN(ret, line_node, putnil);
6752 ADD_INSN1(ret, line_node, duparray, keys);
6757 ADD_INSN(ret, line_node, dup);
6759 ADD_INSNL(ret, line_node, branchunless, type_error);
6761 if (node->nd_pkwrestarg) {
6765 if (node->nd_pkwargs) {
6769 args = node->nd_pkwargs->nd_head;
6771 DECL_ANCHOR(match_values);
6772 INIT_ANCHOR(match_values);
6774 for (i = 0; i < keys_num; i++) {
6775 NODE *key_node = args->nd_head;
6776 NODE *value_node = args->nd_next->nd_head;
6779 if (!nd_type_p(key_node, NODE_LIT)) {
6780 UNKNOWN_NODE(
"NODE_IN", key_node, COMPILE_NG);
6782 key = key_node->nd_lit;
6784 ADD_INSN(ret, line_node, dup);
6785 ADD_INSN1(ret, line_node, putobject, key);
6787 if (in_single_pattern) {
6788 LABEL *match_succeeded;
6789 match_succeeded = NEW_LABEL(line);
6791 ADD_INSN(ret, line_node, dup);
6792 ADD_INSNL(ret, line_node, branchif, match_succeeded);
6795 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
6796 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6797 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
6798 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6799 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
6800 ADD_INSN1(ret, line_node, putobject, key);
6801 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
6803 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
6805 ADD_LABEL(ret, match_succeeded);
6807 ADD_INSNL(ret, line_node, branchunless, match_failed);
6809 ADD_INSN(match_values, line_node, dup);
6810 ADD_INSN1(match_values, line_node, putobject, key);
6811 ADD_SEND(match_values, line_node, node->nd_pkwrestarg ?
rb_intern(
"delete") : idAREF,
INT2FIX(1));
6812 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6813 args = args->nd_next->nd_next;
6815 ADD_SEQ(ret, match_values);
6819 ADD_INSN(ret, line_node, dup);
6820 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
6821 if (in_single_pattern) {
6822 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
6824 ADD_INSNL(ret, line_node, branchunless, match_failed);
6827 if (node->nd_pkwrestarg) {
6828 if (node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
6829 ADD_INSN(ret, line_node, dup);
6830 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
6831 if (in_single_pattern) {
6832 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
6834 ADD_INSNL(ret, line_node, branchunless, match_failed);
6837 ADD_INSN(ret, line_node, dup);
6838 CHECK(iseq_compile_pattern_match(iseq, ret, node->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6842 ADD_INSN(ret, line_node, pop);
6843 ADD_INSNL(ret, line_node, jump, matched);
6844 ADD_INSN(ret, line_node, putnil);
6846 ADD_LABEL(ret, type_error);
6847 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6849 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
6850 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
6851 ADD_INSN(ret, line_node, pop);
6853 ADD_LABEL(ret, match_failed);
6854 ADD_INSN(ret, line_node, pop);
6855 ADD_INSNL(ret, line_node, jump, unmatched);
6882 CHECK(COMPILE(ret,
"case in literal", node));
6883 if (in_single_pattern) {
6884 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
6886 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
6887 if (in_single_pattern) {
6888 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
6890 ADD_INSNL(ret, line_node, branchif, matched);
6891 ADD_INSNL(ret, line_node, jump, unmatched);
6895 ID id = node->nd_vid;
6896 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
6898 if (in_alt_pattern) {
6900 if (name && strlen(name) > 0 && name[0] !=
'_') {
6901 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
6907 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
6908 ADD_INSNL(ret, line_node, jump, matched);
6913 ID id = node->nd_vid;
6915 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
6917 if (in_alt_pattern) {
6919 if (name && strlen(name) > 0 && name[0] !=
'_') {
6920 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
6927 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
6931 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
6932 ADD_INSNL(ret, line_node, jump, matched);
6937 LABEL *match_failed;
6938 match_failed = unmatched;
6939 CHECK(iseq_compile_pattern_match(iseq, ret, node->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
6940 CHECK(COMPILE(ret,
"case in if", node->nd_cond));
6941 if (in_single_pattern) {
6942 LABEL *match_succeeded;
6943 match_succeeded = NEW_LABEL(line);
6945 ADD_INSN(ret, line_node, dup);
6946 if (nd_type_p(node, NODE_IF)) {
6947 ADD_INSNL(ret, line_node, branchif, match_succeeded);
6950 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
6953 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
6954 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
6955 ADD_INSN1(ret, line_node, putobject,
Qfalse);
6956 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
6958 ADD_INSN(ret, line_node, pop);
6959 ADD_INSN(ret, line_node, pop);
6961 ADD_LABEL(ret, match_succeeded);
6963 if (nd_type_p(node, NODE_IF)) {
6964 ADD_INSNL(ret, line_node, branchunless, match_failed);
6967 ADD_INSNL(ret, line_node, branchif, match_failed);
6969 ADD_INSNL(ret, line_node, jump, matched);
6974 LABEL *match_failed;
6975 match_failed = NEW_LABEL(line);
6978 if (! (nd_type_p(n, NODE_LIST) && n->nd_alen == 2)) {
6979 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
6983 ADD_INSN(ret, line_node, dup);
6984 CHECK(iseq_compile_pattern_match(iseq, ret, n->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 , use_deconstructed_cache));
6985 CHECK(iseq_compile_pattern_each(iseq, ret, n->nd_next->nd_head, matched, match_failed, in_single_pattern, in_alt_pattern, base_index,
false));
6986 ADD_INSN(ret, line_node, putnil);
6988 ADD_LABEL(ret, match_failed);
6989 ADD_INSN(ret, line_node, pop);
6990 ADD_INSNL(ret, line_node, jump, unmatched);
6994 LABEL *match_succeeded, *fin;
6995 match_succeeded = NEW_LABEL(line);
6996 fin = NEW_LABEL(line);
6998 ADD_INSN(ret, line_node, dup);
6999 CHECK(iseq_compile_pattern_each(iseq, ret, node->nd_1st, match_succeeded, fin, in_single_pattern,
true, base_index + 1 , use_deconstructed_cache));
7000 ADD_LABEL(ret, match_succeeded);
7001 ADD_INSN(ret, line_node, pop);
7002 ADD_INSNL(ret, line_node, jump, matched);
7003 ADD_INSN(ret, line_node, putnil);
7004 ADD_LABEL(ret, fin);
7005 CHECK(iseq_compile_pattern_each(iseq, ret, node->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
7009 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
7015iseq_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)
7017 LABEL *fin = NEW_LABEL(nd_line(node));
7018 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7019 ADD_LABEL(ret, fin);
7024iseq_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)
7026 const NODE *line_node = node;
7028 if (node->nd_pconst) {
7029 ADD_INSN(ret, line_node, dup);
7030 CHECK(COMPILE(ret,
"constant", node->nd_pconst));
7031 if (in_single_pattern) {
7032 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7034 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7035 if (in_single_pattern) {
7036 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
7038 ADD_INSNL(ret, line_node, branchunless, match_failed);
7045iseq_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)
7047 const NODE *line_node = node;
7051 if (use_deconstructed_cache) {
7053 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7054 ADD_INSNL(ret, line_node, branchnil, deconstruct);
7057 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7058 ADD_INSNL(ret, line_node, branchunless, match_failed);
7061 ADD_INSN(ret, line_node, pop);
7062 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
7063 ADD_INSNL(ret, line_node, jump, deconstructed);
7066 ADD_INSNL(ret, line_node, jump, deconstruct);
7069 ADD_LABEL(ret, deconstruct);
7070 ADD_INSN(ret, line_node, dup);
7071 ADD_INSN1(ret, line_node, putobject,
ID2SYM(
rb_intern(
"deconstruct")));
7072 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7075 if (use_deconstructed_cache) {
7076 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
7079 if (in_single_pattern) {
7080 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
7083 ADD_INSNL(ret, line_node, branchunless, match_failed);
7088 if (use_deconstructed_cache) {
7089 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7092 ADD_INSN(ret, line_node, dup);
7094 ADD_INSNL(ret, line_node, branchunless, type_error);
7096 ADD_LABEL(ret, deconstructed);
7112 const int line = nd_line(node);
7113 const NODE *line_node = node;
7114 LABEL *match_succeeded = NEW_LABEL(line);
7116 ADD_INSN(ret, line_node, dup);
7117 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7119 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7120 ADD_INSN1(ret, line_node, putobject, errmsg);
7121 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7122 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7123 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7125 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7126 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7128 ADD_INSN(ret, line_node, pop);
7129 ADD_INSN(ret, line_node, pop);
7130 ADD_LABEL(ret, match_succeeded);
7146 const int line = nd_line(node);
7147 const NODE *line_node = node;
7148 LABEL *match_succeeded = NEW_LABEL(line);
7150 ADD_INSN(ret, line_node, dup);
7151 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7153 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7154 ADD_INSN1(ret, line_node, putobject, errmsg);
7155 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7156 ADD_INSN(ret, line_node, dup);
7157 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7158 ADD_INSN1(ret, line_node, putobject, pattern_length);
7159 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
7160 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7162 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7163 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
7165 ADD_INSN(ret, line_node, pop);
7166 ADD_INSN(ret, line_node, pop);
7167 ADD_LABEL(ret, match_succeeded);
7173iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
7183 const int line = nd_line(node);
7184 const NODE *line_node = node;
7185 LABEL *match_succeeded = NEW_LABEL(line);
7187 ADD_INSN(ret, line_node, dup);
7188 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7190 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7191 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
7192 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7193 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
7194 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
7195 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7197 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7198 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7200 ADD_INSN(ret, line_node, pop);
7201 ADD_INSN(ret, line_node, pop);
7203 ADD_LABEL(ret, match_succeeded);
7204 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7205 ADD_INSN(ret, line_node, pop);
7206 ADD_INSN(ret, line_node, pop);
7214 const NODE *pattern;
7215 const NODE *node = orig_node;
7216 LABEL *endlabel, *elselabel;
7218 DECL_ANCHOR(body_seq);
7219 DECL_ANCHOR(cond_seq);
7221 enum node_type
type;
7222 const NODE *line_node;
7225 bool single_pattern;
7228 INIT_ANCHOR(body_seq);
7229 INIT_ANCHOR(cond_seq);
7231 branches = decl_branch_base(iseq, node,
"case");
7233 node = node->nd_body;
7234 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
7235 type = nd_type(node);
7236 line = nd_line(node);
7238 single_pattern = !node->nd_next;
7240 endlabel = NEW_LABEL(line);
7241 elselabel = NEW_LABEL(line);
7243 if (single_pattern) {
7245 ADD_INSN(head, line_node, putnil);
7246 ADD_INSN(head, line_node, putnil);
7247 ADD_INSN1(head, line_node, putobject,
Qfalse);
7248 ADD_INSN(head, line_node, putnil);
7250 ADD_INSN(head, line_node, putnil);
7252 CHECK(COMPILE(head,
"case base", orig_node->nd_head));
7256 while (
type == NODE_IN) {
7260 ADD_INSN(body_seq, line_node, putnil);
7262 l1 = NEW_LABEL(line);
7263 ADD_LABEL(body_seq, l1);
7264 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
7265 add_trace_branch_coverage(
7268 node->nd_body ? node->nd_body : node,
7272 CHECK(COMPILE_(body_seq,
"in body", node->nd_body, popped));
7273 ADD_INSNL(body_seq, line_node, jump, endlabel);
7275 pattern = node->nd_head;
7277 int pat_line = nd_line(pattern);
7278 LABEL *next_pat = NEW_LABEL(pat_line);
7279 ADD_INSN (cond_seq, pattern, dup);
7281 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
7282 ADD_LABEL(cond_seq, next_pat);
7283 LABEL_UNREMOVABLE(next_pat);
7286 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7290 node = node->nd_next;
7294 type = nd_type(node);
7295 line = nd_line(node);
7300 ADD_LABEL(cond_seq, elselabel);
7301 ADD_INSN(cond_seq, line_node, pop);
7302 ADD_INSN(cond_seq, line_node, pop);
7303 add_trace_branch_coverage(iseq, cond_seq, node, branch_id,
"else", branches);
7304 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7305 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7306 ADD_INSN(cond_seq, line_node, putnil);
7308 ADD_INSN(cond_seq, line_node, putnil);
7312 debugs(
"== else (implicit)\n");
7313 ADD_LABEL(cond_seq, elselabel);
7314 add_trace_branch_coverage(iseq, cond_seq, orig_node, branch_id,
"else", branches);
7315 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7317 if (single_pattern) {
7325 LABEL *key_error, *fin;
7328 key_error = NEW_LABEL(line);
7329 fin = NEW_LABEL(line);
7332 kw_arg->keyword_len = 2;
7336 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
7337 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
7339 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7340 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
7341 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
7342 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
7343 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
7344 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
7345 ADD_INSNL(cond_seq, orig_node, jump, fin);
7347 ADD_LABEL(cond_seq, key_error);
7349 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7350 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
7351 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
7352 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
7353 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
7354 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
7355 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
7357 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
7359 ADD_LABEL(cond_seq, fin);
7363 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
7364 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
7366 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
7368 ADD_INSN(cond_seq, orig_node, putnil);
7370 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7371 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
7373 ADD_INSN(cond_seq, line_node, putnil);
7377 ADD_SEQ(ret, cond_seq);
7378 ADD_SEQ(ret, body_seq);
7379 ADD_LABEL(ret, endlabel);
7383#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
7384#undef CASE3_BI_OFFSET_ERROR_STRING
7385#undef CASE3_BI_OFFSET_KEY_ERROR_P
7386#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
7387#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
7392 const int line = (int)nd_line(node);
7393 const NODE *line_node = node;
7395 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
7396 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
7397 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
7398 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
7403 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
7404 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
7405 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
7406 LABEL *end_label = NEW_LABEL(line);
7407 LABEL *adjust_label = NEW_LABEL(line);
7409 LABEL *next_catch_label = NEW_LABEL(line);
7410 LABEL *tmp_label = NULL;
7412 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
7413 push_ensure_entry(iseq, &enl, NULL, NULL);
7415 if (node->nd_state == 1) {
7416 ADD_INSNL(ret, line_node, jump, next_label);
7419 tmp_label = NEW_LABEL(line);
7420 ADD_INSNL(ret, line_node, jump, tmp_label);
7422 ADD_LABEL(ret, adjust_label);
7423 ADD_INSN(ret, line_node, putnil);
7424 ADD_LABEL(ret, next_catch_label);
7425 ADD_INSN(ret, line_node, pop);
7426 ADD_INSNL(ret, line_node, jump, next_label);
7427 if (tmp_label) ADD_LABEL(ret, tmp_label);
7429 ADD_LABEL(ret, redo_label);
7430 branches = decl_branch_base(iseq, node,
type == NODE_WHILE ?
"while" :
"until");
7431 add_trace_branch_coverage(
7434 node->nd_body ? node->nd_body : node,
7438 CHECK(COMPILE_POPPED(ret,
"while body", node->nd_body));
7439 ADD_LABEL(ret, next_label);
7441 if (
type == NODE_WHILE) {
7442 compile_branch_condition(iseq, ret, node->nd_cond,
7443 redo_label, end_label);
7447 compile_branch_condition(iseq, ret, node->nd_cond,
7448 end_label, redo_label);
7451 ADD_LABEL(ret, end_label);
7452 ADD_ADJUST_RESTORE(ret, adjust_label);
7454 if (UNDEF_P(node->nd_state)) {
7456 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
7460 ADD_INSN(ret, line_node, putnil);
7463 ADD_LABEL(ret, break_label);
7466 ADD_INSN(ret, line_node, pop);
7469 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
7471 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
7473 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
7474 ISEQ_COMPILE_DATA(iseq)->redo_label);
7476 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
7477 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
7478 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
7479 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
7480 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
7487 const int line = nd_line(node);
7488 const NODE *line_node = node;
7489 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
7490 LABEL *retry_label = NEW_LABEL(line);
7491 LABEL *retry_end_l = NEW_LABEL(line);
7494 ADD_LABEL(ret, retry_label);
7495 if (nd_type_p(node, NODE_FOR)) {
7496 CHECK(COMPILE(ret,
"iter caller (for)", node->nd_iter));
7498 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
7499 NEW_CHILD_ISEQ(node->nd_body, make_name_for_block(iseq),
7500 ISEQ_TYPE_BLOCK, line);
7501 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
7504 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
7505 NEW_CHILD_ISEQ(node->nd_body, make_name_for_block(iseq),
7506 ISEQ_TYPE_BLOCK, line);
7507 CHECK(COMPILE(ret,
"iter caller", node->nd_iter));
7521 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
7522 while (INSN_OF(iobj) != BIN(send) && INSN_OF(iobj) != BIN(invokesuper)) {
7523 iobj = (
INSN*) get_prev_insn(iobj);
7525 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
7529 if (&iobj->link == LAST_ELEMENT(ret)) {
7535 ADD_INSN(ret, line_node, pop);
7538 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
7540 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
7550 const NODE *line_node = node;
7551 const NODE *var = node->nd_var;
7552 LABEL *not_single = NEW_LABEL(nd_line(var));
7553 LABEL *not_ary = NEW_LABEL(nd_line(var));
7554 CHECK(COMPILE(ret,
"for var", var));
7555 ADD_INSN(ret, line_node, dup);
7556 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
7557 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7558 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
7559 ADD_INSNL(ret, line_node, branchunless, not_single);
7560 ADD_INSN(ret, line_node, dup);
7561 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7562 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
7563 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
7564 ADD_INSN(ret, line_node, swap);
7566 ADD_INSN(ret, line_node, dup);
7567 ADD_INSNL(ret, line_node, branchunless, not_ary);
7568 ADD_INSN(ret, line_node, swap);
7569 ADD_LABEL(ret, not_ary);
7570 ADD_INSN(ret, line_node, pop);
7571 ADD_LABEL(ret, not_single);
7578 const NODE *line_node = node;
7579 unsigned long throw_flag = 0;
7581 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7583 LABEL *splabel = NEW_LABEL(0);
7584 ADD_LABEL(ret, splabel);
7585 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
7586 CHECK(COMPILE_(ret,
"break val (while/until)", node->nd_stts,
7587 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
7588 add_ensure_iseq(ret, iseq, 0);
7589 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7590 ADD_ADJUST_RESTORE(ret, splabel);
7593 ADD_INSN(ret, line_node, putnil);
7600 if (!ISEQ_COMPILE_DATA(ip)) {
7605 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7606 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7608 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7611 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7612 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
7616 ip = ISEQ_BODY(ip)->parent_iseq;
7621 CHECK(COMPILE(ret,
"break val (block)", node->nd_stts));
7622 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
7624 ADD_INSN(ret, line_node, pop);
7628 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
7637 const NODE *line_node = node;
7638 unsigned long throw_flag = 0;
7640 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7641 LABEL *splabel = NEW_LABEL(0);
7642 debugs(
"next in while loop\n");
7643 ADD_LABEL(ret, splabel);
7644 CHECK(COMPILE(ret,
"next val/valid syntax?", node->nd_stts));
7645 add_ensure_iseq(ret, iseq, 0);
7646 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
7647 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7648 ADD_ADJUST_RESTORE(ret, splabel);
7650 ADD_INSN(ret, line_node, putnil);
7653 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
7654 LABEL *splabel = NEW_LABEL(0);
7655 debugs(
"next in block\n");
7656 ADD_LABEL(ret, splabel);
7657 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
7658 CHECK(COMPILE(ret,
"next val", node->nd_stts));
7659 add_ensure_iseq(ret, iseq, 0);
7660 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7661 ADD_ADJUST_RESTORE(ret, splabel);
7662 splabel->unremovable = FALSE;
7665 ADD_INSN(ret, line_node, putnil);
7672 if (!ISEQ_COMPILE_DATA(ip)) {
7677 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7678 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7682 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7685 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7686 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
7690 ip = ISEQ_BODY(ip)->parent_iseq;
7693 CHECK(COMPILE(ret,
"next val", node->nd_stts));
7694 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
7697 ADD_INSN(ret, line_node, pop);
7701 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
7711 const NODE *line_node = node;
7713 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
7714 LABEL *splabel = NEW_LABEL(0);
7715 debugs(
"redo in while");
7716 ADD_LABEL(ret, splabel);
7717 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
7718 add_ensure_iseq(ret, iseq, 0);
7719 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
7720 ADD_ADJUST_RESTORE(ret, splabel);
7722 ADD_INSN(ret, line_node, putnil);
7725 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
7726 LABEL *splabel = NEW_LABEL(0);
7728 debugs(
"redo in block");
7729 ADD_LABEL(ret, splabel);
7730 add_ensure_iseq(ret, iseq, 0);
7731 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
7732 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7733 ADD_ADJUST_RESTORE(ret, splabel);
7736 ADD_INSN(ret, line_node, putnil);
7743 if (!ISEQ_COMPILE_DATA(ip)) {
7748 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7751 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7754 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7755 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
7759 ip = ISEQ_BODY(ip)->parent_iseq;
7762 ADD_INSN(ret, line_node, putnil);
7763 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
7766 ADD_INSN(ret, line_node, pop);
7770 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
7780 const NODE *line_node = node;
7782 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
7783 ADD_INSN(ret, line_node, putnil);
7784 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
7787 ADD_INSN(ret, line_node, pop);
7791 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
7800 const int line = nd_line(node);
7801 const NODE *line_node = node;
7802 LABEL *lstart = NEW_LABEL(line);
7803 LABEL *lend = NEW_LABEL(line);
7804 LABEL *lcont = NEW_LABEL(line);
7805 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(node->nd_resq,
7807 ISEQ_BODY(iseq)->location.label),
7808 ISEQ_TYPE_RESCUE, line);
7810 lstart->rescued = LABEL_RESCUE_BEG;
7811 lend->rescued = LABEL_RESCUE_END;
7812 ADD_LABEL(ret, lstart);
7814 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
7815 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
7817 CHECK(COMPILE(ret,
"rescue head", node->nd_head));
7819 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
7821 ADD_LABEL(ret, lend);
7822 if (node->nd_else) {
7823 ADD_INSN(ret, line_node, pop);
7824 CHECK(COMPILE(ret,
"rescue else", node->nd_else));
7826 ADD_INSN(ret, line_node, nop);
7827 ADD_LABEL(ret, lcont);
7830 ADD_INSN(ret, line_node, pop);
7834 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
7835 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
7842 const int line = nd_line(node);
7843 const NODE *line_node = node;
7844 const NODE *resq = node;
7846 LABEL *label_miss, *label_hit;
7849 label_miss = NEW_LABEL(line);
7850 label_hit = NEW_LABEL(line);
7852 narg = resq->nd_args;
7854 switch (nd_type(narg)) {
7857 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
7858 CHECK(COMPILE(ret,
"rescue arg", narg->nd_head));
7859 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7860 ADD_INSNL(ret, line_node, branchif, label_hit);
7861 narg = narg->nd_next;
7867 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
7868 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
7869 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
7870 ADD_INSNL(ret, line_node, branchif, label_hit);
7873 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
7877 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
7879 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7880 ADD_INSNL(ret, line_node, branchif, label_hit);
7882 ADD_INSNL(ret, line_node, jump, label_miss);
7883 ADD_LABEL(ret, label_hit);
7884 CHECK(COMPILE(ret,
"resbody body", resq->nd_body));
7885 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
7886 ADD_INSN(ret, line_node, nop);
7888 ADD_INSN(ret, line_node, leave);
7889 ADD_LABEL(ret, label_miss);
7890 resq = resq->nd_head;
7898 const int line = nd_line(node);
7899 const NODE *line_node = node;
7901 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(node->nd_ensr,
7903 ISEQ_TYPE_ENSURE, line);
7904 LABEL *lstart = NEW_LABEL(line);
7905 LABEL *lend = NEW_LABEL(line);
7906 LABEL *lcont = NEW_LABEL(line);
7914 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", node->nd_ensr));
7916 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
7921 push_ensure_entry(iseq, &enl, &er, node->nd_ensr);
7923 ADD_LABEL(ret, lstart);
7924 CHECK(COMPILE_(ret,
"ensure head", node->nd_head, (popped | last_leave)));
7925 ADD_LABEL(ret, lend);
7927 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
7928 ADD_LABEL(ret, lcont);
7929 if (last_leave) ADD_INSN(ret, line_node, pop);
7931 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
7932 if (lstart->link.next != &lend->link) {
7934 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
7936 erange = erange->next;
7940 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
7947 const NODE *line_node = node;
7950 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
7952 enum rb_iseq_type t =
type;
7953 const NODE *retval = node->nd_stts;
7956 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
7957 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
7958 t = ISEQ_BODY(is)->type;
7962 case ISEQ_TYPE_MAIN:
7964 rb_warn(
"argument of top-level return is ignored");
7968 type = ISEQ_TYPE_METHOD;
7975 if (
type == ISEQ_TYPE_METHOD) {
7976 splabel = NEW_LABEL(0);
7977 ADD_LABEL(ret, splabel);
7978 ADD_ADJUST(ret, line_node, 0);
7981 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
7983 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
7984 add_ensure_iseq(ret, iseq, 1);
7986 ADD_INSN(ret, line_node, leave);
7987 ADD_ADJUST_RESTORE(ret, splabel);
7990 ADD_INSN(ret, line_node, putnil);
7994 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
7996 ADD_INSN(ret, line_node, pop);
8006 CHECK(COMPILE_(ret,
"nd_body", node, popped));
8008 if (!popped && !all_string_result_p(node)) {
8009 const NODE *line_node = node;
8010 const unsigned int flag = VM_CALL_FCALL;
8014 ADD_INSN(ret, line_node, dup);
8015 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
8016 ADD_INSN(ret, line_node, anytostring);
8024 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8026 debugs(
"id: %s idx: %d\n",
rb_id2name(
id), idx);
8027 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8033 LABEL *else_label = NEW_LABEL(nd_line(line_node));
8036 br = decl_branch_base(iseq, node,
"&.");
8038 ADD_INSN(recv, line_node, dup);
8039 ADD_INSNL(recv, line_node, branchnil, else_label);
8040 add_trace_branch_coverage(iseq, recv, node, 0,
"then", br);
8048 if (!else_label)
return;
8049 end_label = NEW_LABEL(nd_line(line_node));
8050 ADD_INSNL(ret, line_node, jump, end_label);
8051 ADD_LABEL(ret, else_label);
8052 add_trace_branch_coverage(iseq, ret, node, 1,
"else", branches);
8053 ADD_LABEL(ret, end_label);
8062 if (node->nd_recv && nd_type_p(node->nd_recv, NODE_STR) &&
8063 (node->nd_mid == idFreeze || node->nd_mid == idUMinus) &&
8064 node->nd_args == NULL &&
8065 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8066 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8067 VALUE str = rb_fstring(node->nd_recv->nd_lit);
8068 if (node->nd_mid == idUMinus) {
8069 ADD_INSN2(ret, line_node, opt_str_uminus, str,
8070 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
8073 ADD_INSN2(ret, line_node, opt_str_freeze, str,
8074 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
8078 ADD_INSN(ret, line_node, pop);
8085 if (node->nd_mid == idAREF && !private_recv_p(node) && node->nd_args &&
8086 nd_type_p(node->nd_args, NODE_LIST) && node->nd_args->nd_alen == 1 &&
8087 nd_type_p(node->nd_args->nd_head, NODE_STR) &&
8088 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8089 !ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
8090 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8091 VALUE str = rb_fstring(node->nd_args->nd_head->nd_lit);
8092 CHECK(COMPILE(ret,
"recv", node->nd_recv));
8093 ADD_INSN2(ret, line_node, opt_aref_with, str,
8094 new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
8097 ADD_INSN(ret, line_node, pop);
8105iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
8107 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
8111iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
8114 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
8115 for (i=0; table[i].index != -1; i++) {
8116 if (strcmp(table[i].name, name) == 0) {
8124iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
8127 static const char prefix[] =
"__builtin_";
8128 const size_t prefix_len =
sizeof(prefix) - 1;
8133 switch (nd_type(recv)) {
8135 if (recv->nd_mid ==
rb_intern(
"__builtin")) {
8140 if (recv->nd_vid ==
rb_intern(
"Primitive")) {
8150 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
8151 return &name[prefix_len];
8160delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
8167 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
8168 unsigned int start=0;
8173 argc + start <= ISEQ_BODY(iseq)->local_table_size;
8177 for (
unsigned int i=start; i-start<argc; i++) {
8178 if (IS_INSN(elem) &&
8179 INSN_OF(elem) == BIN(getlocal)) {
8180 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
8181 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
8183 if (local_level == 0) {
8184 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
8186 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
8187 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
8188 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
8189 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
8213 *pstart_index = start;
8224 if (!node)
goto no_arg;
8225 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
8226 if (node->nd_next)
goto too_many_arg;
8227 node = node->nd_head;
8228 if (!node)
goto no_arg;
8229 if (!nd_type_p(node, NODE_LIT))
goto bad_arg;
8230 VALUE name = node->nd_lit;
8231 if (!
SYMBOL_P(name))
goto non_symbol_arg;
8233 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
8237 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
8240 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
8243 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
8244 rb_builtin_class_name(name));
8247 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
8253 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
8254 if (nd_type(node) == NODE_IF && node->nd_cond == cond_node) {
8255 return node->nd_body;
8258 rb_bug(
"mandatory_node: can't find mandatory node");
8263compile_builtin_mandatory_only_method(
rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
8267 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
8270 rb_node_init(&args_node, NODE_ARGS, 0, 0, (
VALUE)&args);
8273 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
8274 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
8278 tbl->size = table_size;
8283 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
8284 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
8287 for (; i<table_size; i++) {
8288 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
8292 rb_node_init(&scope_node, NODE_SCOPE, (
VALUE)tbl, (
VALUE)mandatory_node(iseq, node), (
VALUE)&args_node);
8295 .root = &scope_node,
8296 .compile_option = 0,
8297 .script_lines = ISEQ_BODY(iseq)->variable.script_lines,
8300 int prev_inline_index = GET_VM()->builtin_inline_index;
8302 ISEQ_BODY(iseq)->mandatory_only_iseq =
8303 rb_iseq_new_with_opt(&ast, rb_iseq_base_label(iseq),
8304 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
8305 nd_line(line_node), NULL, 0,
8306 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option);
8308 GET_VM()->builtin_inline_index = prev_inline_index;
8317 NODE *args_node = node->nd_args;
8319 if (parent_block != NULL) {
8320 COMPILE_ERROR(iseq, nd_line(line_node),
"should not call builtins here.");
8324# define BUILTIN_INLINE_PREFIX "_bi"
8325 char inline_func[
DECIMAL_SIZE_OF_BITS(
sizeof(
int) * CHAR_BIT) +
sizeof(BUILTIN_INLINE_PREFIX)];
8326 bool cconst =
false;
8331 if (strcmp(
"cstmt!", builtin_func) == 0 ||
8332 strcmp(
"cexpr!", builtin_func) == 0) {
8335 else if (strcmp(
"cconst!", builtin_func) == 0) {
8338 else if (strcmp(
"cinit!", builtin_func) == 0) {
8340 GET_VM()->builtin_inline_index++;
8343 else if (strcmp(
"attr!", builtin_func) == 0) {
8345 ISEQ_BODY(iseq)->builtin_inline_p =
true;
8348 else if (strcmp(
"arg!", builtin_func) == 0) {
8349 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
8351 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
8353 rb_bug(
"mandatory_only? should be in if condition");
8355 else if (!LIST_INSN_SIZE_ZERO(ret)) {
8356 rb_bug(
"mandatory_only? should be put on top");
8359 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8360 return compile_builtin_mandatory_only_method(iseq, node, line_node);
8363 rb_bug(
"can't find builtin function:%s", builtin_func);
8366 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
8370 if (GET_VM()->builtin_inline_index == INT_MAX) {
8371 rb_bug(
"builtin inline function index overflow:%s", builtin_func);
8373 int inline_index = GET_VM()->builtin_inline_index++;
8374 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
8375 builtin_func = inline_func;
8381 typedef VALUE(*builtin_func0)(
void *,
VALUE);
8382 VALUE const_val = (*(builtin_func0)bf->func_ptr)(NULL,
Qnil);
8383 ADD_INSN1(ret, line_node, putobject, const_val);
8389 unsigned int flag = 0;
8391 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
8393 if (
FIX2INT(argc) != bf->argc) {
8394 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
8395 builtin_func, bf->argc,
FIX2INT(argc));
8399 unsigned int start_index;
8400 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
8401 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
8405 ADD_INSN1(ret, line_node, invokebuiltin, bf);
8408 if (popped) ADD_INSN(ret, line_node, pop);
8414compile_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)
8422 ID mid = node->nd_mid;
8424 unsigned int flag = 0;
8426 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8427 LABEL *else_label = NULL;
8430 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
8435 if (nd_type_p(node, NODE_VCALL)) {
8440 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
8442 if (mid == id_bitblt) {
8443 ADD_INSN(ret, line_node, bitblt);
8446 else if (mid == id_answer) {
8447 ADD_INSN(ret, line_node, answer);
8459 if (nd_type_p(node, NODE_FCALL) &&
8460 (mid == goto_id || mid == label_id)) {
8463 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
8466 if (!labels_table) {
8467 labels_table = st_init_numtable();
8468 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
8470 if (nd_type_p(node->nd_args->nd_head, NODE_LIT) &&
8471 SYMBOL_P(node->nd_args->nd_head->nd_lit)) {
8473 label_name = node->nd_args->nd_head->nd_lit;
8474 if (!st_lookup(labels_table, (st_data_t)label_name, &data)) {
8475 label = NEW_LABEL(nd_line(line_node));
8476 label->position = nd_line(line_node);
8477 st_insert(labels_table, (st_data_t)label_name, (st_data_t)label);
8480 label = (
LABEL *)data;
8484 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
8488 if (mid == goto_id) {
8489 ADD_INSNL(ret, line_node, jump, label);
8492 ADD_LABEL(ret, label);
8499 const char *builtin_func;
8500 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
8501 (builtin_func = iseq_builtin_function_name(
type, node->nd_recv, mid)) != NULL) {
8502 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
8506 if (!assume_receiver) {
8507 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
8510 if (mid == idCall &&
8511 nd_type_p(node->nd_recv, NODE_LVAR) &&
8512 iseq_block_param_id_p(iseq, node->nd_recv->nd_vid, &idx, &level)) {
8513 ADD_INSN2(recv, node->nd_recv, getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
8515 else if (private_recv_p(node)) {
8516 ADD_INSN(recv, node, putself);
8517 flag |= VM_CALL_FCALL;
8520 CHECK(COMPILE(recv,
"recv", node->nd_recv));
8523 if (
type == NODE_QCALL) {
8524 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
8527 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
8528 ADD_CALL_RECEIVER(recv, line_node);
8533 if (
type != NODE_VCALL) {
8534 argc = setup_args(iseq, args, node->nd_args, &flag, &keywords);
8535 CHECK(!
NIL_P(argc));
8544 debugp_param(
"call args argc", argc);
8545 debugp_param(
"call method",
ID2SYM(mid));
8547 switch ((
int)
type) {
8549 flag |= VM_CALL_VCALL;
8552 flag |= VM_CALL_FCALL;
8555 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
8557 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
8559 ADD_INSN(ret, line_node, pop);
8567 const int line = nd_line(node);
8569 unsigned int flag = 0;
8571 ID id = node->nd_mid;
8598 ADD_INSN(ret, node, putnil);
8600 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node);
8601 CHECK(asgnflag != -1);
8602 switch (nd_type(node->nd_args->nd_head)) {
8606 case NODE_BLOCK_PASS:
8610 argc = setup_args(iseq, ret, node->nd_args->nd_head, &flag, NULL);
8611 CHECK(!
NIL_P(argc));
8613 ADD_INSN1(ret, node, dupn, FIXNUM_INC(argc, 1 + boff));
8615 ADD_SEND_WITH_FLAG(ret, node, idAREF, argc,
INT2FIX(flag));
8617 if (
id == idOROP ||
id == idANDOP) {
8626 LABEL *label = NEW_LABEL(line);
8627 LABEL *lfin = NEW_LABEL(line);
8629 ADD_INSN(ret, node, dup);
8631 ADD_INSNL(ret, node, branchif, label);
8634 ADD_INSNL(ret, node, branchunless, label);
8636 ADD_INSN(ret, node, pop);
8638 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 args->body: ", node->nd_args->nd_body));
8640 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2+boff));
8642 if (flag & VM_CALL_ARGS_SPLAT) {
8643 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
8645 ADD_INSN1(ret, node, dupn,
INT2FIX(3));
8646 ADD_INSN(ret, node, swap);
8647 ADD_INSN(ret, node, pop);
8649 ADD_INSN(ret, node, concatarray);
8651 ADD_INSN1(ret, node, setn,
INT2FIX(3));
8652 ADD_INSN(ret, node, pop);
8653 ADD_INSN(ret, node, pop);
8655 ADD_SEND_WITH_FLAG(ret, node, idASET, argc,
INT2FIX(flag));
8659 ADD_INSN(ret, node, swap);
8660 ADD_SEND_WITH_FLAG(ret, node, idASET, FIXNUM_INC(argc, 1),
INT2FIX(flag));
8662 ADD_INSN(ret, node, pop);
8663 ADD_INSNL(ret, node, jump, lfin);
8664 ADD_LABEL(ret, label);
8666 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2+boff));
8668 ADD_INSN1(ret, node, adjuststack, FIXNUM_INC(argc, 2+boff));
8669 ADD_LABEL(ret, lfin);
8672 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 args->body: ", node->nd_args->nd_body));
8673 ADD_SEND(ret, node,
id,
INT2FIX(1));
8675 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2+boff));
8677 if (flag & VM_CALL_ARGS_SPLAT) {
8678 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
8680 ADD_INSN1(ret, node, dupn,
INT2FIX(3));
8681 ADD_INSN(ret, node, swap);
8682 ADD_INSN(ret, node, pop);
8684 ADD_INSN(ret, node, concatarray);
8686 ADD_INSN1(ret, node, setn,
INT2FIX(3));
8687 ADD_INSN(ret, node, pop);
8688 ADD_INSN(ret, node, pop);
8690 ADD_SEND_WITH_FLAG(ret, node, idASET, argc,
INT2FIX(flag));
8694 ADD_INSN(ret, node, swap);
8695 ADD_SEND_WITH_FLAG(ret, node, idASET, FIXNUM_INC(argc, 1),
INT2FIX(flag));
8697 ADD_INSN(ret, node, pop);
8705 const int line = nd_line(node);
8706 ID atype = node->nd_next->nd_mid;
8709 LABEL *lfin = NEW_LABEL(line);
8710 LABEL *lcfin = NEW_LABEL(line);
8765 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node);
8766 CHECK(asgnflag != -1);
8767 if (node->nd_next->nd_aid) {
8768 lskip = NEW_LABEL(line);
8769 ADD_INSN(ret, node, dup);
8770 ADD_INSNL(ret, node, branchnil, lskip);
8772 ADD_INSN(ret, node, dup);
8773 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
8775 if (atype == idOROP || atype == idANDOP) {
8777 ADD_INSN(ret, node, dup);
8779 if (atype == idOROP) {
8780 ADD_INSNL(ret, node, branchif, lcfin);
8783 ADD_INSNL(ret, node, branchunless, lcfin);
8786 ADD_INSN(ret, node, pop);
8788 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", node->nd_value));
8790 ADD_INSN(ret, node, swap);
8791 ADD_INSN1(ret, node, topn,
INT2FIX(1));
8793 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
8794 ADD_INSNL(ret, node, jump, lfin);
8796 ADD_LABEL(ret, lcfin);
8798 ADD_INSN(ret, node, swap);
8801 ADD_LABEL(ret, lfin);
8804 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", node->nd_value));
8805 ADD_SEND(ret, node, atype,
INT2FIX(1));
8807 ADD_INSN(ret, node, swap);
8808 ADD_INSN1(ret, node, topn,
INT2FIX(1));
8810 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
8812 if (lskip && popped) {
8813 ADD_LABEL(ret, lskip);
8815 ADD_INSN(ret, node, pop);
8816 if (lskip && !popped) {
8817 ADD_LABEL(ret, lskip);
8825 const int line = nd_line(node);
8830 switch (nd_type(node->nd_head)) {
8832 ADD_INSN1(ret, node, putobject, rb_cObject);
8835 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", node->nd_head->nd_head));
8838 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
8839 ruby_node_name(nd_type(node->nd_head)));
8842 mid = node->nd_head->nd_mid;
8844 if (node->nd_aid == idOROP) {
8845 lassign = NEW_LABEL(line);
8846 ADD_INSN(ret, node, dup);
8847 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
8849 ADD_INSNL(ret, node, branchunless, lassign);
8851 ADD_INSN(ret, node, dup);
8852 ADD_INSN1(ret, node, putobject,
Qtrue);
8853 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
8855 if (node->nd_aid == idOROP || node->nd_aid == idANDOP) {
8856 lfin = NEW_LABEL(line);
8857 if (!popped) ADD_INSN(ret, node, dup);
8858 if (node->nd_aid == idOROP)
8859 ADD_INSNL(ret, node, branchif, lfin);
8861 ADD_INSNL(ret, node, branchunless, lfin);
8863 if (!popped) ADD_INSN(ret, node, pop);
8864 if (lassign) ADD_LABEL(ret, lassign);
8865 CHECK(COMPILE(ret,
"NODE_OP_CDECL#nd_value", node->nd_value));
8868 ADD_INSN1(ret, node, topn,
INT2FIX(1));
8870 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
8871 ADD_INSN(ret, node, swap);
8873 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
8874 ADD_LABEL(ret, lfin);
8875 if (!popped) ADD_INSN(ret, node, swap);
8876 ADD_INSN(ret, node, pop);
8879 CHECK(COMPILE(ret,
"NODE_OP_CDECL#nd_value", node->nd_value));
8881 ADD_CALL(ret, node, node->nd_aid,
INT2FIX(1));
8883 ADD_INSN(ret, node, swap);
8885 ADD_INSN1(ret, node, topn,
INT2FIX(1));
8886 ADD_INSN(ret, node, swap);
8888 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
8896 const int line = nd_line(node);
8897 LABEL *lfin = NEW_LABEL(line);
8900 if (
type == NODE_OP_ASGN_OR && !nd_type_p(node->nd_head, NODE_IVAR)) {
8904 defined_expr(iseq, ret, node->nd_head, lfinish,
Qfalse);
8905 lassign = lfinish[1];
8907 lassign = NEW_LABEL(line);
8909 ADD_INSNL(ret, node, branchunless, lassign);
8912 lassign = NEW_LABEL(line);
8915 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", node->nd_head));
8918 ADD_INSN(ret, node, dup);
8921 if (
type == NODE_OP_ASGN_AND) {
8922 ADD_INSNL(ret, node, branchunless, lfin);
8925 ADD_INSNL(ret, node, branchif, lfin);
8929 ADD_INSN(ret, node, pop);
8932 ADD_LABEL(ret, lassign);
8933 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", node->nd_value, popped));
8934 ADD_LABEL(ret, lfin);
8944 unsigned int flag = 0;
8946 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8949 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
8950 if (
type == NODE_SUPER) {
8951 VALUE vargc = setup_args(iseq, args, node->nd_args, &flag, &keywords);
8952 CHECK(!
NIL_P(vargc));
8958 const rb_iseq_t *liseq = body->local_iseq;
8960 const struct rb_iseq_param_keyword *
const local_kwd = local_body->
param.keyword;
8961 int lvar_level = get_lvar_level(iseq);
8963 argc = local_body->
param.lead_num;
8966 for (i = 0; i < local_body->
param.lead_num; i++) {
8967 int idx = local_body->local_table_size - i;
8968 ADD_GETLOCAL(args, node, idx, lvar_level);
8971 if (local_body->
param.flags.has_opt) {
8974 for (j = 0; j < local_body->
param.opt_num; j++) {
8975 int idx = local_body->local_table_size - (i + j);
8976 ADD_GETLOCAL(args, node, idx, lvar_level);
8981 if (local_body->
param.flags.has_rest) {
8983 int idx = local_body->local_table_size - local_body->
param.rest_start;
8984 ADD_GETLOCAL(args, node, idx, lvar_level);
8985 ADD_INSN1(args, node, splatarray,
Qfalse);
8987 argc = local_body->
param.rest_start + 1;
8988 flag |= VM_CALL_ARGS_SPLAT;
8990 if (local_body->
param.flags.has_post) {
8992 int post_len = local_body->
param.post_num;
8993 int post_start = local_body->
param.post_start;
8995 if (local_body->
param.flags.has_rest) {
8997 for (j=0; j<post_len; j++) {
8998 int idx = local_body->local_table_size - (post_start + j);
8999 ADD_GETLOCAL(args, node, idx, lvar_level);
9001 ADD_INSN1(args, node, newarray,
INT2FIX(j));
9002 ADD_INSN (args, node, concatarray);
9007 for (j=0; j<post_len; j++) {
9008 int idx = local_body->local_table_size - (post_start + j);
9009 ADD_GETLOCAL(args, node, idx, lvar_level);
9011 argc = post_len + post_start;
9015 if (local_body->
param.flags.has_kw) {
9016 int local_size = local_body->local_table_size;
9019 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9021 if (local_body->
param.flags.has_kwrest) {
9022 int idx = local_body->local_table_size - local_kwd->rest_start;
9023 ADD_GETLOCAL(args, node, idx, lvar_level);
9024 if (local_kwd->num > 0) {
9026 flag |= VM_CALL_KW_SPLAT_MUT;
9030 ADD_INSN1(args, node, newhash,
INT2FIX(0));
9031 flag |= VM_CALL_KW_SPLAT_MUT;
9033 for (i = 0; i < local_kwd->num; ++i) {
9034 ID id = local_kwd->table[i];
9035 int idx = local_size - get_local_var_idx(liseq,
id);
9036 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
9037 ADD_GETLOCAL(args, node, idx, lvar_level);
9039 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
9040 if (local_body->
param.flags.has_rest) {
9041 ADD_INSN1(args, node, newarray,
INT2FIX(1));
9042 ADD_INSN (args, node, concatarray);
9045 flag |= VM_CALL_KW_SPLAT;
9047 else if (local_body->
param.flags.has_kwrest) {
9048 int idx = local_body->local_table_size - local_kwd->rest_start;
9049 ADD_GETLOCAL(args, node, idx, lvar_level);
9051 if (local_body->
param.flags.has_rest) {
9052 ADD_INSN1(args, node, newarray,
INT2FIX(1));
9053 ADD_INSN (args, node, concatarray);
9058 flag |= VM_CALL_KW_SPLAT;
9062 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
9063 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
9064 ADD_INSN(ret, node, putself);
9066 ADD_INSN2(ret, node, invokesuper,
9067 new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL),
9071 ADD_INSN(ret, node, pop);
9081 unsigned int flag = 0;
9086 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
9088 case ISEQ_TYPE_MAIN:
9089 case ISEQ_TYPE_CLASS:
9090 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
9095 if (node->nd_head) {
9096 argc = setup_args(iseq, args, node->nd_head, &flag, &keywords);
9097 CHECK(!
NIL_P(argc));
9104 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
9107 ADD_INSN(ret, node, pop);
9112 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
9113 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
9115 if (level > 0) access_outer_variables(iseq, level,
rb_intern(
"yield"),
true);
9128 switch ((
int)
type) {
9130 ADD_INSN1(recv, node, putobject, node->nd_lit);
9131 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
9135 CHECK(COMPILE(recv,
"receiver", node->nd_recv));
9136 CHECK(COMPILE(val,
"value", node->nd_value));
9139 CHECK(COMPILE(recv,
"receiver", node->nd_value));
9140 CHECK(COMPILE(val,
"value", node->nd_recv));
9146 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
9148 if (node->nd_args) {
9149 compile_named_capture_assign(iseq, ret, node->nd_args);
9153 ADD_INSN(ret, node, pop);
9164 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
9165 (segments = collect_const_segments(iseq, node))) {
9166 ISEQ_BODY(iseq)->ic_size++;
9167 ADD_INSN1(ret, node, opt_getconstant_path, segments);
9177 CHECK(compile_const_prefix(iseq, node, pref, body));
9178 if (LIST_INSN_SIZE_ZERO(pref)) {
9179 ADD_INSN(ret, node, putnil);
9190 ADD_CALL_RECEIVER(ret, node);
9191 CHECK(COMPILE(ret,
"colon2#nd_head", node->nd_head));
9192 ADD_CALL(ret, node, node->nd_mid,
INT2FIX(1));
9195 ADD_INSN(ret, node, pop);
9203 debugi(
"colon3#nd_mid", node->nd_mid);
9206 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
9207 ISEQ_BODY(iseq)->ic_size++;
9209 ADD_INSN1(ret, node, opt_getconstant_path, segments);
9213 ADD_INSN1(ret, node, putobject, rb_cObject);
9214 ADD_INSN1(ret, node, putobject,
Qtrue);
9215 ADD_INSN1(ret, node, getconstant,
ID2SYM(node->nd_mid));
9219 ADD_INSN(ret, node, pop);
9228 const NODE *b = node->nd_beg;
9229 const NODE *e = node->nd_end;
9231 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
9233 VALUE bv = nd_type_p(b, NODE_LIT) ? b->nd_lit :
Qnil;
9234 VALUE ev = nd_type_p(e, NODE_LIT) ? e->nd_lit :
Qnil;
9236 ADD_INSN1(ret, node, putobject, val);
9241 CHECK(COMPILE_(ret,
"min", b, popped));
9242 CHECK(COMPILE_(ret,
"max", e, popped));
9244 ADD_INSN1(ret, node, newrange, flag);
9254 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
9255 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
9261 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
9264 ip = ISEQ_BODY(ip)->parent_iseq;
9268 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
9271 ADD_INSN(ret, node, putnil);
9282 LABEL *end_label = NEW_LABEL(nd_line(node));
9283 const NODE *default_value = node->nd_body->nd_value;
9285 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
9287 COMPILE_ERROR(ERROR_ARGS
"unreachable");
9290 else if (nd_type_p(default_value, NODE_LIT) ||
9291 nd_type_p(default_value, NODE_NIL) ||
9292 nd_type_p(default_value, NODE_TRUE) ||
9293 nd_type_p(default_value, NODE_FALSE)) {
9294 COMPILE_ERROR(ERROR_ARGS
"unreachable");
9302 int kw_bits_idx = body->local_table_size - body->
param.keyword->bits_start;
9303 int keyword_idx = body->
param.keyword->num;
9305 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
9306 ADD_INSNL(ret, node, branchif, end_label);
9307 CHECK(COMPILE_POPPED(ret,
"keyword default argument", node->nd_body));
9308 ADD_LABEL(ret, end_label);
9318 unsigned int flag = 0;
9319 ID mid = node->nd_mid;
9321 LABEL *else_label = NULL;
9327 if (mid == idASET && !private_recv_p(node) && node->nd_args &&
9328 nd_type_p(node->nd_args, NODE_LIST) && node->nd_args->nd_alen == 2 &&
9329 nd_type_p(node->nd_args->nd_head, NODE_STR) &&
9330 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9331 !ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
9332 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction)
9334 VALUE str = rb_fstring(node->nd_args->nd_head->nd_lit);
9335 CHECK(COMPILE(ret,
"recv", node->nd_recv));
9336 CHECK(COMPILE(ret,
"value", node->nd_args->nd_next->nd_head));
9338 ADD_INSN(ret, node, swap);
9339 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9341 ADD_INSN2(ret, node, opt_aset_with, str,
9342 new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
9344 ADD_INSN(ret, node, pop);
9350 argc = setup_args(iseq, args, node->nd_args, &flag, NULL);
9351 CHECK(!
NIL_P(argc));
9353 int asgnflag = COMPILE_RECV(recv,
"recv", node);
9354 CHECK(asgnflag != -1);
9355 flag |= (
unsigned int)asgnflag;
9357 debugp_param(
"argc", argc);
9358 debugp_param(
"nd_mid",
ID2SYM(mid));
9363 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
9366 ADD_INSN(ret, node, putnil);
9370 if (flag & VM_CALL_ARGS_BLOCKARG) {
9371 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9372 if (flag & VM_CALL_ARGS_SPLAT) {
9373 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
9374 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
9376 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 3));
9377 ADD_INSN (ret, node, pop);
9379 else if (flag & VM_CALL_ARGS_SPLAT) {
9380 ADD_INSN(ret, node, dup);
9381 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
9382 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
9383 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
9384 ADD_INSN (ret, node, pop);
9387 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
9394 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
9395 qcall_branch_end(iseq, ret, else_label, branches, node, node);
9396 ADD_INSN(ret, node, pop);
9413 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
9414 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
9415 debugs(
"node: NODE_NIL(implicit)\n");
9416 NODE dummy_line_node = generate_dummy_line_node(lineno, -1);
9417 ADD_INSN(ret, &dummy_line_node, putnil);
9421 return iseq_compile_each0(iseq, ret, node, popped);
9427 const int line = (int)nd_line(node);
9428 const enum node_type
type = nd_type(node);
9431 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
9435 if (node->flags & NODE_FL_NEWLINE) {
9437 ISEQ_COMPILE_DATA(iseq)->last_line = line;
9438 if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
9439 event |= RUBY_EVENT_COVERAGE_LINE;
9441 ADD_TRACE(ret, event);
9445 debug_node_start(node);
9447#define BEFORE_RETURN debug_node_end()
9451 CHECK(compile_block(iseq, ret, node, popped));
9455 CHECK(compile_if(iseq, ret, node, popped,
type));
9458 CHECK(compile_case(iseq, ret, node, popped));
9461 CHECK(compile_case2(iseq, ret, node, popped));
9464 CHECK(compile_case3(iseq, ret, node, popped));
9468 CHECK(compile_loop(iseq, ret, node, popped,
type));
9472 CHECK(compile_iter(iseq, ret, node, popped));
9474 case NODE_FOR_MASGN:
9475 CHECK(compile_for_masgn(iseq, ret, node, popped));
9478 CHECK(compile_break(iseq, ret, node, popped));
9481 CHECK(compile_next(iseq, ret, node, popped));
9484 CHECK(compile_redo(iseq, ret, node, popped));
9487 CHECK(compile_retry(iseq, ret, node, popped));
9490 CHECK(COMPILE_(ret,
"NODE_BEGIN", node->nd_body, popped));
9494 CHECK(compile_rescue(iseq, ret, node, popped));
9497 CHECK(compile_resbody(iseq, ret, node, popped));
9500 CHECK(compile_ensure(iseq, ret, node, popped));
9505 LABEL *end_label = NEW_LABEL(line);
9506 CHECK(COMPILE(ret,
"nd_1st", node->nd_1st));
9508 ADD_INSN(ret, node, dup);
9510 if (
type == NODE_AND) {
9511 ADD_INSNL(ret, node, branchunless, end_label);
9514 ADD_INSNL(ret, node, branchif, end_label);
9517 ADD_INSN(ret, node, pop);
9519 CHECK(COMPILE_(ret,
"nd_2nd", node->nd_2nd, popped));
9520 ADD_LABEL(ret, end_label);
9525 compile_massign(iseq, ret, node, popped);
9530 ID id = node->nd_vid;
9531 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
9533 debugs(
"lvar: %s idx: %d\n",
rb_id2name(
id), idx);
9534 CHECK(COMPILE(ret,
"rvalue", node->nd_value));
9537 ADD_INSN(ret, node, dup);
9539 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
9544 ID id = node->nd_vid;
9545 CHECK(COMPILE(ret,
"dvalue", node->nd_value));
9546 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
9549 ADD_INSN(ret, node, dup);
9552 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
9555 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
9559 ADD_SETLOCAL(ret, node, ls - idx, lv);
9563 CHECK(COMPILE(ret,
"lvalue", node->nd_value));
9566 ADD_INSN(ret, node, dup);
9568 ADD_INSN1(ret, node, setglobal,
ID2SYM(node->nd_entry));
9572 CHECK(COMPILE(ret,
"lvalue", node->nd_value));
9574 ADD_INSN(ret, node, dup);
9576 ADD_INSN2(ret, node, setinstancevariable,
9578 get_ivar_ic_value(iseq,node->nd_vid));
9583 CHECK(COMPILE(ret,
"lvalue", node->nd_value));
9586 ADD_INSN(ret, node, dup);
9589 ADD_INSN1(ret, node, putspecialobject,
9590 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
9591 ADD_INSN1(ret, node, setconstant,
ID2SYM(node->nd_vid));
9594 compile_cpath(ret, iseq, node->nd_else);
9595 CHECK(COMPILE(ret,
"lvalue", node->nd_value));
9596 ADD_INSN(ret, node, swap);
9599 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9600 ADD_INSN(ret, node, swap);
9603 ADD_INSN1(ret, node, setconstant,
ID2SYM(node->nd_else->nd_mid));
9608 CHECK(COMPILE(ret,
"cvasgn val", node->nd_value));
9610 ADD_INSN(ret, node, dup);
9612 ADD_INSN2(ret, node, setclassvariable,
9614 get_cvar_ic_value(iseq,node->nd_vid));
9618 CHECK(compile_op_asgn1(iseq, ret, node, popped));
9621 CHECK(compile_op_asgn2(iseq, ret, node, popped));
9624 CHECK(compile_op_cdecl(iseq, ret, node, popped));
9626 case NODE_OP_ASGN_AND:
9627 case NODE_OP_ASGN_OR:
9628 CHECK(compile_op_log(iseq, ret, node, popped,
type));
9632 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
9638 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
9644 CHECK(compile_super(iseq, ret, node, popped,
type));
9647 CHECK(compile_array(iseq, ret, node, popped) >= 0);
9652 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
9657 const NODE *n = node;
9659 COMPILE_ERROR(ERROR_ARGS
"NODE_VALUES: must not be popped");
9662 CHECK(COMPILE(ret,
"values item", n->nd_head));
9665 ADD_INSN1(ret, node, newarray,
INT2FIX(node->nd_alen));
9669 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
9672 CHECK(compile_return(iseq, ret, node, popped));
9675 CHECK(compile_yield(iseq, ret, node, popped));
9679 compile_lvar(iseq, ret, node, node->nd_vid);
9685 debugi(
"nd_vid", node->nd_vid);
9687 idx = get_dyna_var_idx(iseq, node->nd_vid, &lv, &ls);
9689 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
9690 rb_id2str(node->nd_vid));
9693 ADD_GETLOCAL(ret, node, ls - idx, lv);
9698 ADD_INSN1(ret, node, getglobal,
ID2SYM(node->nd_entry));
9700 ADD_INSN(ret, node, pop);
9705 debugi(
"nd_vid", node->nd_vid);
9707 ADD_INSN2(ret, node, getinstancevariable,
9709 get_ivar_ic_value(iseq,node->nd_vid));
9714 debugi(
"nd_vid", node->nd_vid);
9716 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
9718 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(node->nd_vid));
9719 ADD_INSN1(ret, node, opt_getconstant_path, segments);
9723 ADD_INSN(ret, node, putnil);
9724 ADD_INSN1(ret, node, putobject,
Qtrue);
9725 ADD_INSN1(ret, node, getconstant,
ID2SYM(node->nd_vid));
9729 ADD_INSN(ret, node, pop);
9735 ADD_INSN2(ret, node, getclassvariable,
9737 get_cvar_ic_value(iseq,node->nd_vid));
9743 if (!node->nd_nth) {
9744 ADD_INSN(ret, node, putnil);
9747 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
9752 case NODE_BACK_REF:{
9754 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
9755 INT2FIX(0x01 | (node->nd_nth << 1)));
9762 CHECK(compile_match(iseq, ret, node, popped,
type));
9765 debugp_param(
"lit", node->nd_lit);
9767 if (UNLIKELY(node->nd_lit == rb_mRubyVMFrozenCore)) {
9768 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9771 ADD_INSN1(ret, node, putobject, node->nd_lit);
9778 debugp_param(
"nd_lit", node->nd_lit);
9780 VALUE lit = node->nd_lit;
9781 if (!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal) {
9782 lit = rb_fstring(lit);
9783 ADD_INSN1(ret, node, putstring, lit);
9787 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
9788 VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq),
INT2FIX(line));
9794 lit = rb_fstring(lit);
9796 ADD_INSN1(ret, node, putobject, lit);
9803 compile_dstr(iseq, ret, node);
9806 ADD_INSN(ret, node, pop);
9811 ADD_CALL_RECEIVER(ret, node);
9812 VALUE str = rb_fstring(node->nd_lit);
9813 ADD_INSN1(ret, node, putobject, str);
9815 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
9818 ADD_INSN(ret, node, pop);
9823 ADD_CALL_RECEIVER(ret, node);
9824 compile_dstr(iseq, ret, node);
9825 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
9828 ADD_INSN(ret, node, pop);
9833 CHECK(compile_evstr(iseq, ret, node->nd_body, popped));
9836 compile_dregx(iseq, ret, node);
9839 ADD_INSN(ret, node, pop);
9844 int ic_index = body->ise_size++;
9846 block_iseq = NEW_CHILD_ISEQ(node->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
9848 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
9852 ADD_INSN(ret, node, pop);
9858 CHECK(COMPILE(ret,
"argscat head", node->nd_head));
9859 ADD_INSN1(ret, node, splatarray,
Qfalse);
9860 ADD_INSN(ret, node, pop);
9861 CHECK(COMPILE(ret,
"argscat body", node->nd_body));
9862 ADD_INSN1(ret, node, splatarray,
Qfalse);
9863 ADD_INSN(ret, node, pop);
9866 CHECK(COMPILE(ret,
"argscat head", node->nd_head));
9867 CHECK(COMPILE(ret,
"argscat body", node->nd_body));
9868 ADD_INSN(ret, node, concatarray);
9872 case NODE_ARGSPUSH:{
9874 CHECK(COMPILE(ret,
"argspush head", node->nd_head));
9875 ADD_INSN1(ret, node, splatarray,
Qfalse);
9876 ADD_INSN(ret, node, pop);
9877 CHECK(COMPILE_(ret,
"argspush body", node->nd_body, popped));
9880 CHECK(COMPILE(ret,
"argspush head", node->nd_head));
9881 CHECK(compile_array_1(iseq, ret, node->nd_body));
9882 ADD_INSN(ret, node, concatarray);
9887 CHECK(COMPILE(ret,
"splat", node->nd_head));
9888 ADD_INSN1(ret, node, splatarray,
Qtrue);
9891 ADD_INSN(ret, node, pop);
9896 ID mid = node->nd_mid;
9897 const rb_iseq_t *method_iseq = NEW_ISEQ(node->nd_defn,
9899 ISEQ_TYPE_METHOD, line);
9901 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
9902 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
9906 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
9912 ID mid = node->nd_mid;
9913 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(node->nd_defn,
9915 ISEQ_TYPE_METHOD, line);
9917 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
9918 CHECK(COMPILE(ret,
"defs: recv", node->nd_recv));
9919 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
9923 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
9928 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9929 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
9930 CHECK(COMPILE(ret,
"alias arg1", node->nd_1st));
9931 CHECK(COMPILE(ret,
"alias arg2", node->nd_2nd));
9932 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
9935 ADD_INSN(ret, node, pop);
9940 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9941 ADD_INSN1(ret, node, putobject,
ID2SYM(node->nd_alias));
9942 ADD_INSN1(ret, node, putobject,
ID2SYM(node->nd_orig));
9943 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
9946 ADD_INSN(ret, node, pop);
9951 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9952 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
9953 CHECK(COMPILE(ret,
"undef arg", node->nd_undef));
9954 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
9957 ADD_INSN(ret, node, pop);
9962 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(node->nd_body,
9964 ISEQ_TYPE_CLASS, line);
9965 const int flags = VM_DEFINECLASS_TYPE_CLASS |
9966 (node->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
9967 compile_cpath(ret, iseq, node->nd_cpath);
9969 CHECK(COMPILE(ret,
"super", node->nd_super));
9970 ADD_INSN3(ret, node, defineclass,
ID2SYM(node->nd_cpath->nd_mid), class_iseq,
INT2FIX(flags));
9974 ADD_INSN(ret, node, pop);
9979 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(node->nd_body,
9981 ISEQ_TYPE_CLASS, line);
9982 const int flags = VM_DEFINECLASS_TYPE_MODULE |
9983 compile_cpath(ret, iseq, node->nd_cpath);
9985 ADD_INSN (ret, node, putnil);
9986 ADD_INSN3(ret, node, defineclass,
ID2SYM(node->nd_cpath->nd_mid), module_iseq,
INT2FIX(flags));
9990 ADD_INSN(ret, node, pop);
9996 const rb_iseq_t *singleton_class = NEW_ISEQ(node->nd_body, rb_fstring_lit(
"singleton class"),
9997 ISEQ_TYPE_CLASS, line);
9999 CHECK(COMPILE(ret,
"sclass#recv", node->nd_recv));
10000 ADD_INSN (ret, node, putnil);
10001 CONST_ID(singletonclass,
"singletonclass");
10002 ADD_INSN3(ret, node, defineclass,
10003 ID2SYM(singletonclass), singleton_class,
10004 INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
10008 ADD_INSN(ret, node, pop);
10013 CHECK(compile_colon2(iseq, ret, node, popped));
10016 CHECK(compile_colon3(iseq, ret, node, popped));
10019 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
10022 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
10026 LABEL *lend = NEW_LABEL(line);
10027 LABEL *ltrue = NEW_LABEL(line);
10028 LABEL *lfalse = NEW_LABEL(line);
10029 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
10031 ADD_LABEL(ret, ltrue);
10032 ADD_INSN1(ret, node, putobject,
Qtrue);
10033 ADD_INSNL(ret, node, jump, lend);
10034 ADD_LABEL(ret, lfalse);
10035 ADD_INSN1(ret, node, putobject,
Qfalse);
10036 ADD_LABEL(ret, lend);
10041 ADD_INSN(ret, node, putself);
10047 ADD_INSN(ret, node, putnil);
10053 ADD_INSN1(ret, node, putobject,
Qtrue);
10059 ADD_INSN1(ret, node, putobject,
Qfalse);
10064 CHECK(compile_errinfo(iseq, ret, node, popped));
10068 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue));
10071 case NODE_POSTEXE:{
10075 int is_index = body->ise_size++;
10077 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, node->nd_body);
10079 new_child_iseq_with_callback(iseq, ifunc,
10080 rb_fstring(make_name_for_block(iseq)), iseq, ISEQ_TYPE_BLOCK, line);
10082 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
10086 ADD_INSN(ret, node, pop);
10091 CHECK(compile_kw_arg(iseq, ret, node, popped));
10094 compile_dstr(iseq, ret, node);
10096 ADD_INSN(ret, node, intern);
10099 ADD_INSN(ret, node, pop);
10103 case NODE_ATTRASGN:
10104 CHECK(compile_attrasgn(iseq, ret, node, popped));
10108 const rb_iseq_t *block = NEW_CHILD_ISEQ(node->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
10111 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10112 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
10116 ADD_INSN(ret, node, pop);
10121 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
10136insn_data_length(
INSN *iobj)
10138 return insn_len(iobj->insn_id);
10142calc_sp_depth(
int depth,
INSN *insn)
10144 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
10148opobj_inspect(
VALUE obj)
10156 obj = rb_ary_dup(obj);
10168insn_data_to_s_detail(
INSN *iobj)
10172 if (iobj->operands) {
10173 const char *types = insn_op_types(iobj->insn_id);
10176 for (j = 0; types[j]; j++) {
10177 char type = types[j];
10201 VALUE v = OPERAND_AT(iobj, j);
10228 if (vm_ci_mid(ci))
rb_str_catf(str,
"%"PRIsVALUE, rb_id2str(vm_ci_mid(ci)));
10237 void *func = (
void *)OPERAND_AT(iobj, j);
10240 if (dladdr(func, &info) && info.dli_sname) {
10255 if (types[j + 1]) {
10266 dump_disasm_list_with_cursor(link, NULL, NULL);
10277 printf(
"-- raw disasm--------\n");
10280 if (curr) printf(curr == link ?
"*" :
" ");
10281 switch (link->type) {
10282 case ISEQ_ELEMENT_INSN:
10284 iobj = (
INSN *)link;
10285 str = insn_data_to_s_detail(iobj);
10286 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
10287 pos += insn_data_length(iobj);
10290 case ISEQ_ELEMENT_LABEL:
10292 lobj = (
LABEL *)link;
10293 printf(LABEL_FORMAT
" [sp: %d]%s\n", lobj->label_no, lobj->sp,
10294 dest == lobj ?
" <---" :
"");
10297 case ISEQ_ELEMENT_TRACE:
10300 printf(
" trace: %0x\n", trace->event);
10303 case ISEQ_ELEMENT_ADJUST:
10306 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
10315 printf(
"---------------------\n");
10320rb_insns_name(
int i)
10322 return insn_name(i);
10326rb_insns_name_array(
void)
10328 VALUE ary = rb_ary_new_capa(VM_INSTRUCTION_SIZE);
10330 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
10331 rb_ary_push(ary, rb_fstring_cstr(insn_name(i)));
10341 obj = rb_to_symbol_type(obj);
10343 if (st_lookup(labels_table, obj, &tmp) == 0) {
10344 label = NEW_LABEL(0);
10345 st_insert(labels_table, obj, (st_data_t)label);
10348 label = (
LABEL *)tmp;
10355get_exception_sym2type(
VALUE sym)
10357 static VALUE symRescue, symEnsure, symRetry;
10358 static VALUE symBreak, symRedo, symNext;
10360 if (symRescue == 0) {
10369 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
10370 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
10371 if (sym == symRetry)
return CATCH_TYPE_RETRY;
10372 if (sym == symBreak)
return CATCH_TYPE_BREAK;
10373 if (sym == symRedo)
return CATCH_TYPE_REDO;
10374 if (sym == symNext)
return CATCH_TYPE_NEXT;
10388 LABEL *lstart, *lend, *lcont;
10403 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
10404 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
10405 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
10409 if (
type == CATCH_TYPE_RESCUE ||
10410 type == CATCH_TYPE_BREAK ||
10411 type == CATCH_TYPE_NEXT) {
10417 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
10425insn_make_insn_table(
void)
10429 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
10431 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
10445 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
10447 else if (
CLASS_OF(op) == rb_cISeq) {
10454 loaded_iseq = rb_iseqw_to_iseq(iseqw);
10455 return loaded_iseq;
10463 unsigned int flag = 0;
10474 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
10476 if (!
NIL_P(vkw_arg)) {
10479 size_t n = rb_callinfo_kwarg_bytes(len);
10482 kw_arg->keyword_len = len;
10483 for (i = 0; i < len; i++) {
10486 kw_arg->keywords[i] = kw;
10491 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
10497event_name_to_flag(
VALUE sym)
10499#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
10519 int line_no = 0, node_id = -1, insn_idx = 0;
10520 int ret = COMPILE_OK;
10525 static struct st_table *insn_table;
10527 if (insn_table == 0) {
10528 insn_table = insn_make_insn_table();
10531 for (i=0; i<len; i++) {
10537 ADD_TRACE(anchor, event);
10540 LABEL *label = register_label(iseq, labels_table, obj);
10541 ADD_LABEL(anchor, label);
10554 node_id =
NUM2INT(rb_ary_entry(node_ids, insn_idx++));
10558 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
10560 COMPILE_ERROR(iseq, line_no,
10561 "unknown instruction: %+"PRIsVALUE, insn);
10566 if (argc != insn_len((
VALUE)insn_id)-1) {
10567 COMPILE_ERROR(iseq, line_no,
10568 "operand size mismatch");
10574 argv = compile_data_calloc2(iseq,
sizeof(
VALUE), argc);
10577 NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
10580 (
enum ruby_vminsn_type)insn_id, argc, argv));
10582 for (j=0; j<argc; j++) {
10583 VALUE op = rb_ary_entry(obj, j+1);
10584 switch (insn_op_type((
VALUE)insn_id, j)) {
10586 LABEL *label = register_label(iseq, labels_table, op);
10587 argv[j] = (
VALUE)label;
10602 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
10613 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
10614 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
10619 VALUE segments = rb_ary_new();
10620 op = rb_to_array_type(op);
10624 sym = rb_to_symbol_type(sym);
10625 rb_ary_push(segments, sym);
10629 argv[j] = segments;
10631 ISEQ_BODY(iseq)->ic_size++;
10636 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
10637 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
10642 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
10643 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
10647 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
10650 argv[j] = rb_to_symbol_type(op);
10657 RHASH_TBL_RAW(map)->type = &cdhash_type;
10658 op = rb_to_array_type(op);
10663 register_label(iseq, labels_table, sym);
10664 rb_hash_aset(map, key, (
VALUE)label | 1);
10673#if SIZEOF_VALUE <= SIZEOF_LONG
10678 argv[j] = (
VALUE)funcptr;
10687 NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
10690 (
enum ruby_vminsn_type)insn_id, argc, NULL));
10698 validate_labels(iseq, labels_table);
10699 if (!ret)
return ret;
10700 return iseq_setup(iseq, anchor);
10703#define CHECK_ARRAY(v) rb_to_array_type(v)
10704#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
10709 VALUE val = rb_hash_aref(param, sym);
10714 else if (!
NIL_P(val)) {
10721static const struct rb_iseq_param_keyword *
10727 VALUE key, sym, default_val;
10730 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
10732 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
10734 keyword->num = len;
10735#define SYM(s) ID2SYM(rb_intern_const(#s))
10736 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
10737 i = keyword->bits_start - keyword->num;
10738 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
10742 for (i = 0; i < len; i++) {
10746 goto default_values;
10749 keyword->required_num++;
10753 default_len = len - i;
10754 if (default_len == 0) {
10755 keyword->table = ids;
10758 else if (default_len < 0) {
10764 for (j = 0; i < len; i++, j++) {
10781 dvs[j] = default_val;
10784 keyword->table = ids;
10785 keyword->default_values = dvs;
10791iseq_insn_each_object_mark(
VALUE *obj_ptr,
VALUE _)
10793 rb_gc_mark(*obj_ptr);
10800 size_t size =
sizeof(
INSN);
10801 unsigned int pos = 0;
10804#ifdef STRICT_ALIGNMENT
10805 size_t padding = calc_padding((
void *)&storage->buff[pos], size);
10807 const size_t padding = 0;
10809 size_t offset = pos + size + padding;
10810 if (offset > storage->size || offset > storage->pos) {
10812 storage = storage->next;
10815#ifdef STRICT_ALIGNMENT
10816 pos += (int)padding;
10819 iobj = (
INSN *)&storage->buff[pos];
10821 if (iobj->operands) {
10822 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark, (
VALUE)0);
10833#define SYM(s) ID2SYM(rb_intern_const(#s))
10835 unsigned int arg_size, local_size, stack_max;
10837 struct st_table *labels_table = st_init_numtable();
10839 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
10840 VALUE keywords = rb_hash_aref(params, SYM(keyword));
10842 DECL_ANCHOR(anchor);
10843 INIT_ANCHOR(anchor);
10846 ISEQ_BODY(iseq)->local_table_size = len;
10847 ISEQ_BODY(iseq)->local_table = tbl = len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
10849 for (i = 0; i < len; i++) {
10852 if (sym_arg_rest == lv) {
10860#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
10861 if (INT_PARAM(lead_num)) {
10862 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
10864 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
10865 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
10866 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
10867 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
10870#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
10872 INT_PARAM(arg_size);
10873 INT_PARAM(local_size);
10874 INT_PARAM(stack_max);
10879#ifdef USE_ISEQ_NODE_ID
10888 ISEQ_BODY(iseq)->param.flags.has_opt = !!(len - 1 >= 0);
10890 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
10893 for (i = 0; i < len; i++) {
10895 LABEL *label = register_label(iseq, labels_table, ent);
10896 opt_table[i] = (
VALUE)label;
10899 ISEQ_BODY(iseq)->param.opt_num = len - 1;
10900 ISEQ_BODY(iseq)->param.opt_table = opt_table;
10903 else if (!
NIL_P(arg_opt_labels)) {
10909 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
10911 else if (!
NIL_P(keywords)) {
10916 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
10917 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
10920 if (int_param(&i, params, SYM(kwrest))) {
10921 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
10922 if (keyword == NULL) {
10923 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
10925 keyword->rest_start = i;
10926 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
10929 iseq_calc_param_size(iseq);
10932 iseq_build_from_ary_exception(iseq, labels_table, exception);
10935 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
10937 ISEQ_BODY(iseq)->param.size = arg_size;
10938 ISEQ_BODY(iseq)->local_table_size = local_size;
10939 ISEQ_BODY(iseq)->stack_max = stack_max;
10949 while (body->type == ISEQ_TYPE_BLOCK ||
10950 body->type == ISEQ_TYPE_RESCUE ||
10951 body->type == ISEQ_TYPE_ENSURE ||
10952 body->type == ISEQ_TYPE_EVAL ||
10953 body->type == ISEQ_TYPE_MAIN
10957 for (i = 0; i < body->local_table_size; i++) {
10958 if (body->local_table[i] ==
id) {
10962 iseq = body->parent_iseq;
10963 body = ISEQ_BODY(iseq);
10976 for (i=0; i<body->local_table_size; i++) {
10977 if (body->local_table[i] ==
id) {
10987#ifndef IBF_ISEQ_DEBUG
10988#define IBF_ISEQ_DEBUG 0
10991#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
10992#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
10995typedef unsigned int ibf_offset_t;
10996#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
10998#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
11000#define IBF_DEVEL_VERSION 4
11001#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
11003#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
11008 unsigned int major_version;
11009 unsigned int minor_version;
11011 unsigned int extra_size;
11013 unsigned int iseq_list_size;
11014 unsigned int global_object_list_size;
11015 ibf_offset_t iseq_list_offset;
11016 ibf_offset_t global_object_list_offset;
11035 unsigned int obj_list_size;
11036 ibf_offset_t obj_list_offset;
11055pinned_list_mark(
void *ptr)
11059 for (i = 0; i < list->size; i++) {
11060 if (list->buffer[i]) {
11061 rb_gc_mark(list->buffer[i]);
11067pinned_list_free(
void *ptr)
11070 xfree(list->buffer);
11075pinned_list_memsize(
const void *ptr)
11083 {pinned_list_mark, pinned_list_free, pinned_list_memsize,},
11084 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
11088pinned_list_fetch(
VALUE list,
long offset)
11094 if (offset >= ptr->size) {
11098 return ptr->buffer[offset];
11102pinned_list_store(
VALUE list,
long offset,
VALUE object)
11108 if (offset >= ptr->size) {
11116pinned_list_new(
long size)
11129ibf_dump_pos(
struct ibf_dump *dump)
11131 long pos =
RSTRING_LEN(dump->current_buffer->str);
11132#if SIZEOF_LONG > SIZEOF_INT
11133 if (pos >= UINT_MAX) {
11137 return (
unsigned int)pos;
11141ibf_dump_align(
struct ibf_dump *dump,
size_t align)
11143 ibf_offset_t pos = ibf_dump_pos(dump);
11145 static const char padding[
sizeof(
VALUE)];
11146 size_t size = align - ((size_t)pos % align);
11147#if SIZEOF_LONG > SIZEOF_INT
11148 if (pos + size >= UINT_MAX) {
11152 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
11153 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
11155 rb_str_cat(dump->current_buffer->str, padding, size);
11160ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
11162 ibf_offset_t pos = ibf_dump_pos(dump);
11163 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
11169ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
11171 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
11175ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
11177 VALUE str = dump->current_buffer->str;
11179 if ((
unsigned long)(size + offset) > (
unsigned long)
RSTRING_LEN(str))
11180 rb_bug(
"ibf_dump_overwrite: overflow");
11181 memcpy(ptr + offset, buff, size);
11185ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
11187 ibf_offset_t beg = *offset;
11189 return load->current_buffer->buff + beg;
11193ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
11195 void *buff = ruby_xmalloc2(x, y);
11196 size_t size = x * y;
11197 memcpy(buff, load->current_buffer->buff + offset, size);
11201#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
11203#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
11204#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
11205#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
11206#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
11207#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
11210ibf_table_lookup(
struct st_table *table, st_data_t key)
11214 if (st_lookup(table, key, &val)) {
11223ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
11225 int index = ibf_table_lookup(table, key);
11228 index = (int)table->num_entries;
11229 st_insert(table, key, (st_data_t)index);
11237static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
11243ibf_dump_object_table_new(
void)
11245 st_table *obj_table = st_init_numtable();
11246 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
11254 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
11263 return ibf_dump_object(dump,
rb_id2sym(
id));
11267ibf_load_id(
const struct ibf_load *load,
const ID id_index)
11269 if (id_index == 0) {
11272 VALUE sym = ibf_load_object(load, id_index);
11278static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
11283 if (iseq == NULL) {
11287 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
11291static unsigned char
11292ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
11295 return (
unsigned char)load->current_buffer->buff[(*offset)++];
11311 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
11312 ibf_dump_write(dump, &x,
sizeof(
VALUE));
11316 enum { max_byte_length =
sizeof(
VALUE) + 1 };
11318 unsigned char bytes[max_byte_length];
11321 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
11322 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
11328 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
11331 ibf_dump_write(dump, bytes + max_byte_length - n, n);
11335ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
11337 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
11338 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
11340 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
11341 *offset +=
sizeof(
VALUE);
11346 enum { max_byte_length =
sizeof(
VALUE) + 1 };
11348 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
11349 const unsigned char c = buffer[*offset];
11353 c == 0 ? 9 : ntz_int32(c) + 1;
11356 if (*offset + n > load->current_buffer->size) {
11361 for (i = 1; i < n; i++) {
11363 x |= (
VALUE)buffer[*offset + i];
11377 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
11379 size_t len = strlen(bf->name);
11380 ibf_dump_write_small_value(dump, (
VALUE)len);
11381 ibf_dump_write(dump, bf->name, len);
11385ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
11387 int i = (int)ibf_load_small_value(load, offset);
11388 int len = (int)ibf_load_small_value(load, offset);
11389 const char *name = (
char *)ibf_load_ptr(load, offset, len);
11392 fprintf(stderr,
"%.*s!!\n", len, name);
11397 if (strncmp(table[i].name, name, len) != 0) {
11398 rb_raise(
rb_eArgError,
"builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
11409 const int iseq_size = body->iseq_size;
11411 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
11413 ibf_offset_t offset = ibf_dump_pos(dump);
11415 for (code_index=0; code_index<iseq_size;) {
11416 const VALUE insn = orig_code[code_index++];
11417 const char *types = insn_op_types(insn);
11422 ibf_dump_write_small_value(dump, insn);
11425 for (op_index=0; types[op_index]; op_index++, code_index++) {
11426 VALUE op = orig_code[code_index];
11429 switch (types[op_index]) {
11432 wv = ibf_dump_object(dump, op);
11441 wv = ibf_dump_object(dump, arr);
11449 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
11457 wv = ibf_dump_id(dump, (
ID)op);
11469 ibf_dump_write_small_value(dump, wv);
11472 assert(insn_len(insn) == op_index+1);
11479ibf_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)
11482 unsigned int code_index;
11483 ibf_offset_t reading_pos = bytecode_offset;
11487 struct rb_call_data *cd_entries = load_body->call_data;
11490 iseq_bits_t * mark_offset_bits;
11492 iseq_bits_t tmp[1] = {0};
11494 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
11495 mark_offset_bits = tmp;
11498 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
11500 bool needs_bitmap =
false;
11502 for (code_index=0; code_index<iseq_size;) {
11504 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
11505 const char *types = insn_op_types(insn);
11511 for (op_index=0; types[op_index]; op_index++, code_index++) {
11512 const char operand_type = types[op_index];
11513 switch (operand_type) {
11516 VALUE op = ibf_load_small_value(load, &reading_pos);
11517 VALUE v = ibf_load_object(load, op);
11518 code[code_index] = v;
11521 ISEQ_MBITS_SET(mark_offset_bits, code_index);
11522 needs_bitmap =
true;
11528 VALUE op = ibf_load_small_value(load, &reading_pos);
11529 VALUE v = ibf_load_object(load, op);
11530 v = rb_hash_dup(v);
11531 RHASH_TBL_RAW(v)->type = &cdhash_type;
11533 freeze_hide_obj(v);
11538 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
11540 code[code_index] = v;
11541 ISEQ_MBITS_SET(mark_offset_bits, code_index);
11543 needs_bitmap =
true;
11548 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
11550 code[code_index] = v;
11553 ISEQ_MBITS_SET(mark_offset_bits, code_index);
11554 needs_bitmap =
true;
11560 VALUE op = ibf_load_small_value(load, &reading_pos);
11561 VALUE arr = ibf_load_object(load, op);
11563 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
11564 ic->
segments = array_to_idlist(arr);
11566 code[code_index] = (
VALUE)ic;
11573 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11575 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
11576 code[code_index] = (
VALUE)ic;
11578 if (operand_type == TS_IVC) {
11581 if (insn == BIN(setinstancevariable)) {
11582 ID iv_name = (
ID)code[code_index - 1];
11583 cache->iv_set_name = iv_name;
11586 cache->iv_set_name = 0;
11589 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
11596 code[code_index] = (
VALUE)cd_entries++;
11601 VALUE op = ibf_load_small_value(load, &reading_pos);
11602 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
11609 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
11612 code[code_index] = ibf_load_small_value(load, &reading_pos);
11616 if (insn_len(insn) != op_index+1) {
11621 load_body->iseq_encoded = code;
11622 load_body->iseq_size = code_index;
11624 if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
11625 load_body->mark_bits.single = mark_offset_bits[0];
11628 if (needs_bitmap) {
11629 load_body->mark_bits.list = mark_offset_bits;
11632 load_body->mark_bits.list = 0;
11633 ruby_xfree(mark_offset_bits);
11637 assert(code_index == iseq_size);
11638 assert(reading_pos == bytecode_offset + bytecode_size);
11645 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
11648 IBF_W_ALIGN(
VALUE);
11649 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
11652 return ibf_dump_pos(dump);
11657ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
11661 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
11672 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
11675 struct rb_iseq_param_keyword dump_kw = *kw;
11676 int dv_num = kw->num - kw->required_num;
11681 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
11682 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
11684 dump_kw.table = IBF_W(ids,
ID, kw->num);
11685 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
11686 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
11687 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
11694static const struct rb_iseq_param_keyword *
11695ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
11697 if (param_keyword_offset) {
11698 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
11699 ID *ids = IBF_R(kw->table,
ID, kw->num);
11700 int dv_num = kw->num - kw->required_num;
11701 VALUE *dvs = IBF_R(kw->default_values,
VALUE, dv_num);
11704 for (i=0; i<kw->num; i++) {
11705 ids[i] = ibf_load_id(load, ids[i]);
11707 for (i=0; i<dv_num; i++) {
11708 dvs[i] = ibf_load_object(load, dvs[i]);
11712 kw->default_values = dvs;
11723 ibf_offset_t offset = ibf_dump_pos(dump);
11727 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
11728 ibf_dump_write_small_value(dump, entries[i].line_no);
11729#ifdef USE_ISEQ_NODE_ID
11730 ibf_dump_write_small_value(dump, entries[i].node_id);
11732 ibf_dump_write_small_value(dump, entries[i].events);
11739ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
11741 ibf_offset_t reading_pos = body_offset;
11745 for (i = 0; i < size; i++) {
11746 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
11747#ifdef USE_ISEQ_NODE_ID
11748 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
11750 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
11757ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
11759 ibf_offset_t offset = ibf_dump_pos(dump);
11761 unsigned int last = 0;
11763 for (i = 0; i < size; i++) {
11764 ibf_dump_write_small_value(dump, positions[i] - last);
11765 last = positions[i];
11771static unsigned int *
11772ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
11774 ibf_offset_t reading_pos = positions_offset;
11775 unsigned int *positions =
ALLOC_N(
unsigned int, size);
11777 unsigned int last = 0;
11779 for (i = 0; i < size; i++) {
11780 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
11781 last = positions[i];
11791 const int size = body->local_table_size;
11795 for (i=0; i<size; i++) {
11796 table[i] = ibf_dump_id(dump, body->local_table[i]);
11800 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
11804ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
11807 ID *table = IBF_R(local_table_offset,
ID, size);
11810 for (i=0; i<size; i++) {
11811 table[i] = ibf_load_id(load, table[i]);
11826 int *iseq_indices =
ALLOCA_N(
int, table->size);
11829 for (i=0; i<table->size; i++) {
11830 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
11833 const ibf_offset_t offset = ibf_dump_pos(dump);
11835 for (i=0; i<table->size; i++) {
11836 ibf_dump_write_small_value(dump, iseq_indices[i]);
11837 ibf_dump_write_small_value(dump, table->entries[i].type);
11838 ibf_dump_write_small_value(dump, table->entries[i].start);
11839 ibf_dump_write_small_value(dump, table->entries[i].end);
11840 ibf_dump_write_small_value(dump, table->entries[i].cont);
11841 ibf_dump_write_small_value(dump, table->entries[i].sp);
11846 return ibf_dump_pos(dump);
11851ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size)
11854 struct iseq_catch_table *table = ruby_xmalloc(iseq_catch_table_bytes(size));
11855 table->size = size;
11857 ibf_offset_t reading_pos = catch_table_offset;
11860 for (i=0; i<table->size; i++) {
11861 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
11862 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
11863 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11864 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11865 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11866 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11868 table->entries[i].iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)iseq_index);
11881 const unsigned int ci_size = body->ci_size;
11884 ibf_offset_t offset = ibf_dump_pos(dump);
11888 for (i = 0; i < ci_size; i++) {
11891 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
11892 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
11893 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
11897 int len = kwarg->keyword_len;
11898 ibf_dump_write_small_value(dump, len);
11899 for (
int j=0; j<len; j++) {
11900 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
11901 ibf_dump_write_small_value(dump, keyword);
11905 ibf_dump_write_small_value(dump, 0);
11910 ibf_dump_write_small_value(dump, (
VALUE)-1);
11917static enum rb_id_table_iterator_result
11918dump_outer_variable(
ID id,
VALUE val,
void *dump)
11920 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
11921 ibf_dump_write_small_value(dump, val);
11923 return ID_TABLE_CONTINUE;
11929 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
11931 ibf_offset_t offset = ibf_dump_pos(dump);
11934 ibf_dump_write_small_value(dump, (
VALUE)rb_id_table_size(ovs));
11935 rb_id_table_foreach(ovs, dump_outer_variable, (
void *)dump);
11938 ibf_dump_write_small_value(dump, (
VALUE)0);
11946ibf_load_ci_entries(
const struct ibf_load *load,
11947 ibf_offset_t ci_entries_offset,
11948 unsigned int ci_size,
11951 ibf_offset_t reading_pos = ci_entries_offset;
11958 for (i = 0; i < ci_size; i++) {
11959 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
11960 if (mid_index != (
VALUE)-1) {
11961 ID mid = ibf_load_id(load, mid_index);
11962 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11963 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11966 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
11969 kwarg->keyword_len = kwlen;
11970 for (
int j=0; j<kwlen; j++) {
11971 VALUE keyword = ibf_load_small_value(load, &reading_pos);
11972 kwarg->keywords[j] = ibf_load_object(load, keyword);
11976 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
11978 cds[i].cc = vm_cc_empty();
11989ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
11991 ibf_offset_t reading_pos = outer_variables_offset;
11995 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
11997 if (table_size > 0) {
11998 tbl = rb_id_table_create(table_size);
12001 for (
size_t i = 0; i < table_size; i++) {
12002 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
12003 VALUE value = ibf_load_small_value(load, &reading_pos);
12004 if (!key) key = rb_make_temporary_id(i);
12005 rb_id_table_insert(tbl, key, value);
12014 assert(dump->current_buffer == &dump->global_buffer);
12016 unsigned int *positions;
12020 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
12021 const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
12022 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
12024#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12025 ibf_offset_t iseq_start = ibf_dump_pos(dump);
12030 buffer.obj_table = ibf_dump_object_table_new();
12031 dump->current_buffer = &buffer;
12034 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
12035 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
12036 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
12037 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
12038 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
12040 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
12041 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
12042 ruby_xfree(positions);
12044 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
12045 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
12046 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
12047 const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
12048 const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
12049 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
12050 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
12051 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
12053#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12054 ibf_offset_t local_obj_list_offset;
12055 unsigned int local_obj_list_size;
12057 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
12060 ibf_offset_t body_offset = ibf_dump_pos(dump);
12063 unsigned int param_flags =
12064 (body->
param.flags.has_lead << 0) |
12065 (body->
param.flags.has_opt << 1) |
12066 (body->
param.flags.has_rest << 2) |
12067 (body->
param.flags.has_post << 3) |
12068 (body->
param.flags.has_kw << 4) |
12069 (body->
param.flags.has_kwrest << 5) |
12070 (body->
param.flags.has_block << 6) |
12071 (body->
param.flags.ambiguous_param0 << 7) |
12072 (body->
param.flags.accepts_no_kwarg << 8) |
12073 (body->
param.flags.ruby2_keywords << 9);
12075#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12076# define IBF_BODY_OFFSET(x) (x)
12078# define IBF_BODY_OFFSET(x) (body_offset - (x))
12081 ibf_dump_write_small_value(dump, body->type);
12082 ibf_dump_write_small_value(dump, body->iseq_size);
12083 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
12084 ibf_dump_write_small_value(dump, bytecode_size);
12085 ibf_dump_write_small_value(dump, param_flags);
12086 ibf_dump_write_small_value(dump, body->
param.size);
12087 ibf_dump_write_small_value(dump, body->
param.lead_num);
12088 ibf_dump_write_small_value(dump, body->
param.opt_num);
12089 ibf_dump_write_small_value(dump, body->
param.rest_start);
12090 ibf_dump_write_small_value(dump, body->
param.post_start);
12091 ibf_dump_write_small_value(dump, body->
param.post_num);
12092 ibf_dump_write_small_value(dump, body->
param.block_start);
12093 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
12094 ibf_dump_write_small_value(dump, param_keyword_offset);
12095 ibf_dump_write_small_value(dump, location_pathobj_index);
12096 ibf_dump_write_small_value(dump, location_base_label_index);
12097 ibf_dump_write_small_value(dump, location_label_index);
12098 ibf_dump_write_small_value(dump, body->location.first_lineno);
12099 ibf_dump_write_small_value(dump, body->location.node_id);
12100 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
12101 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
12102 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
12103 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
12104 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
12105 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
12106 ibf_dump_write_small_value(dump, body->insns_info.size);
12107 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
12108 ibf_dump_write_small_value(dump, catch_table_size);
12109 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
12110 ibf_dump_write_small_value(dump, parent_iseq_index);
12111 ibf_dump_write_small_value(dump, local_iseq_index);
12112 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
12113 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
12114 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
12115 ibf_dump_write_small_value(dump, body->variable.flip_count);
12116 ibf_dump_write_small_value(dump, body->local_table_size);
12117 ibf_dump_write_small_value(dump, body->ivc_size);
12118 ibf_dump_write_small_value(dump, body->icvarc_size);
12119 ibf_dump_write_small_value(dump, body->ise_size);
12120 ibf_dump_write_small_value(dump, body->ic_size);
12121 ibf_dump_write_small_value(dump, body->ci_size);
12122 ibf_dump_write_small_value(dump, body->stack_max);
12123 ibf_dump_write_small_value(dump, body->catch_except_p);
12124 ibf_dump_write_small_value(dump, body->builtin_inline_p);
12126#undef IBF_BODY_OFFSET
12128#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12129 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
12131 dump->current_buffer = saved_buffer;
12132 ibf_dump_write(dump,
RSTRING_PTR(buffer.str), iseq_length_bytes);
12134 ibf_offset_t offset = ibf_dump_pos(dump);
12135 ibf_dump_write_small_value(dump, iseq_start);
12136 ibf_dump_write_small_value(dump, iseq_length_bytes);
12137 ibf_dump_write_small_value(dump, body_offset);
12139 ibf_dump_write_small_value(dump, local_obj_list_offset);
12140 ibf_dump_write_small_value(dump, local_obj_list_size);
12142 st_free_table(buffer.obj_table);
12146 return body_offset;
12151ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
12153 VALUE str = ibf_load_object(load, str_index);
12155 str = rb_fstring(str);
12165 ibf_offset_t reading_pos = offset;
12167#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12169 load->current_buffer = &load->global_buffer;
12171 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12172 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12173 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12176 buffer.buff = load->global_buffer.buff + iseq_start;
12177 buffer.size = iseq_length_bytes;
12178 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12179 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12180 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
12182 load->current_buffer = &buffer;
12183 reading_pos = body_offset;
12186#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12187# define IBF_BODY_OFFSET(x) (x)
12189# define IBF_BODY_OFFSET(x) (offset - (x))
12192 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12193 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12194 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12195 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12196 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12197 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12198 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
12199 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
12200 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
12201 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
12202 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
12203 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
12204 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12205 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12206 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
12207 const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
12208 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
12209 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
12210 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
12211 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
12212 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
12213 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
12214 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
12215 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12216 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12217 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12218 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12219 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12220 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12221 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12222 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12223 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12224 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12225 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12226 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
12227 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12229 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12230 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12231 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12232 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12234 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12235 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12236 const char catch_except_p = (char)ibf_load_small_value(load, &reading_pos);
12237 const bool builtin_inline_p = (bool)ibf_load_small_value(load, &reading_pos);
12240 VALUE path = ibf_load_object(load, location_pathobj_index);
12245 realpath = path = rb_fstring(path);
12248 VALUE pathobj = path;
12254 if (!
NIL_P(realpath)) {
12257 "(%x), path=%+"PRIsVALUE,
12258 realpath,
TYPE(realpath), path);
12260 realpath = rb_fstring(realpath);
12266 rb_iseq_pathobj_set(iseq, path, realpath);
12271 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
12273#undef IBF_BODY_OFFSET
12275 load_body->type =
type;
12276 load_body->stack_max = stack_max;
12277 load_body->
param.flags.has_lead = (param_flags >> 0) & 1;
12278 load_body->
param.flags.has_opt = (param_flags >> 1) & 1;
12279 load_body->
param.flags.has_rest = (param_flags >> 2) & 1;
12280 load_body->
param.flags.has_post = (param_flags >> 3) & 1;
12281 load_body->
param.flags.has_kw = FALSE;
12282 load_body->
param.flags.has_kwrest = (param_flags >> 5) & 1;
12283 load_body->
param.flags.has_block = (param_flags >> 6) & 1;
12284 load_body->
param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
12285 load_body->
param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
12286 load_body->
param.flags.ruby2_keywords = (param_flags >> 9) & 1;
12287 load_body->
param.size = param_size;
12288 load_body->
param.lead_num = param_lead_num;
12289 load_body->
param.opt_num = param_opt_num;
12290 load_body->
param.rest_start = param_rest_start;
12291 load_body->
param.post_start = param_post_start;
12292 load_body->
param.post_num = param_post_num;
12293 load_body->
param.block_start = param_block_start;
12294 load_body->local_table_size = local_table_size;
12295 load_body->ci_size = ci_size;
12296 load_body->insns_info.size = insns_info_size;
12298 ISEQ_COVERAGE_SET(iseq,
Qnil);
12299 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
12300 load_body->variable.flip_count = variable_flip_count;
12301 load_body->variable.script_lines =
Qnil;
12303 load_body->location.first_lineno = location_first_lineno;
12304 load_body->location.node_id = location_node_id;
12305 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
12306 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
12307 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
12308 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
12309 load_body->catch_except_p = catch_except_p;
12310 load_body->builtin_inline_p = builtin_inline_p;
12312 load_body->ivc_size = ivc_size;
12313 load_body->icvarc_size = icvarc_size;
12314 load_body->ise_size = ise_size;
12315 load_body->ic_size = ic_size;
12317 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
12318 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
12319 load_body->
param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
12320 load_body->
param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
12321 load_body->
param.flags.has_kw = (param_flags >> 4) & 1;
12322 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
12323 load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
12324 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
12325 load_body->catch_table = ibf_load_catch_table(load, catch_table_offset, catch_table_size);
12326 load_body->parent_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)parent_iseq_index);
12327 load_body->local_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)local_iseq_index);
12328 load_body->mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
12330 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
12331#if VM_INSN_INFO_TABLE_IMPL == 2
12332 rb_iseq_insns_info_encode_positions(iseq);
12335 rb_iseq_translate_threaded_code(iseq);
12337#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12338 load->current_buffer = &load->global_buffer;
12341 RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
12342 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
12344#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12345 load->current_buffer = saved_buffer;
12347 verify_call_cache(iseq);
12350 rb_vm_pop_frame_no_int(ec);
12360ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
12365 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
12366 rb_ary_push(args->offset_list,
UINT2NUM(offset));
12368 return ST_CONTINUE;
12374 VALUE offset_list = rb_ary_hidden_new(dump->iseq_table->num_entries);
12378 args.offset_list = offset_list;
12380 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
12383 st_index_t size = dump->iseq_table->num_entries;
12384 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
12386 for (i = 0; i < size; i++) {
12390 ibf_dump_align(dump,
sizeof(ibf_offset_t));
12391 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
12392 header->iseq_list_size = (
unsigned int)size;
12395#define IBF_OBJECT_INTERNAL FL_PROMOTED0
12404 unsigned int type: 5;
12405 unsigned int special_const: 1;
12406 unsigned int frozen: 1;
12407 unsigned int internal: 1;
12410enum ibf_object_class_index {
12411 IBF_OBJECT_CLASS_OBJECT,
12412 IBF_OBJECT_CLASS_ARRAY,
12413 IBF_OBJECT_CLASS_STANDARD_ERROR,
12414 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
12415 IBF_OBJECT_CLASS_TYPE_ERROR,
12416 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
12426 long keyval[FLEX_ARY_LEN];
12439 BDIGIT digits[FLEX_ARY_LEN];
12442enum ibf_object_data_type {
12443 IBF_OBJECT_DATA_ENCODING,
12454#define IBF_ALIGNED_OFFSET(align, offset) \
12455 ((((offset) - 1) / (align) + 1) * (align))
12456#define IBF_OBJBODY(type, offset) (const type *)\
12457 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
12460ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
12462 if (offset >= load->current_buffer->size) {
12465 return load->current_buffer->buff + offset;
12468NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
12471ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
12474 rb_raw_obj_info(buff,
sizeof(buff), obj);
12490 enum ibf_object_class_index cindex;
12491 if (obj == rb_cObject) {
12492 cindex = IBF_OBJECT_CLASS_OBJECT;
12495 cindex = IBF_OBJECT_CLASS_ARRAY;
12498 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
12501 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
12504 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
12507 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
12510 rb_obj_info_dump(obj);
12512 rb_bug(
"unsupported class");
12514 ibf_dump_write_small_value(dump, (
VALUE)cindex);
12520 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
12523 case IBF_OBJECT_CLASS_OBJECT:
12525 case IBF_OBJECT_CLASS_ARRAY:
12527 case IBF_OBJECT_CLASS_STANDARD_ERROR:
12529 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
12531 case IBF_OBJECT_CLASS_TYPE_ERROR:
12533 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
12545 (void)IBF_W(&dbl,
double, 1);
12551 const double *dblp = IBF_OBJBODY(
double, offset);
12558 long encindex = (long)rb_enc_get_index(obj);
12562 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
12563 rb_encoding *enc = rb_enc_from_index((
int)encindex);
12565 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
12568 ibf_dump_write_small_value(dump, encindex);
12569 ibf_dump_write_small_value(dump, len);
12570 IBF_WP(ptr,
char, len);
12576 ibf_offset_t reading_pos = offset;
12578 int encindex = (int)ibf_load_small_value(load, &reading_pos);
12579 const long len = (long)ibf_load_small_value(load, &reading_pos);
12580 const char *ptr = load->current_buffer->buff + reading_pos;
12582 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
12583 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
12584 encindex = rb_enc_find_index(
RSTRING_PTR(enc_name_str));
12588 if (header->frozen && !header->internal) {
12595 if (header->frozen) str = rb_fstring(str);
12606 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
12608 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
12609 ibf_dump_write_small_value(dump, regexp.srcstr);
12616 regexp.option = ibf_load_byte(load, &offset);
12617 regexp.srcstr = ibf_load_small_value(load, &offset);
12619 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
12620 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
12632 ibf_dump_write_small_value(dump, len);
12633 for (i=0; i<len; i++) {
12634 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
12635 ibf_dump_write_small_value(dump, index);
12642 ibf_offset_t reading_pos = offset;
12644 const long len = (long)ibf_load_small_value(load, &reading_pos);
12646 VALUE ary = header->internal ? rb_ary_hidden_new(len) : rb_ary_new_capa(len);
12649 for (i=0; i<len; i++) {
12650 const VALUE index = ibf_load_small_value(load, &reading_pos);
12651 rb_ary_push(ary, ibf_load_object(load, index));
12660ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
12664 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
12665 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
12667 ibf_dump_write_small_value(dump, key_index);
12668 ibf_dump_write_small_value(dump, val_index);
12669 return ST_CONTINUE;
12676 ibf_dump_write_small_value(dump, (
VALUE)len);
12684 long len = (long)ibf_load_small_value(load, &offset);
12685 VALUE obj = rb_hash_new_with_size(len);
12688 for (i = 0; i < len; i++) {
12689 VALUE key_index = ibf_load_small_value(load, &offset);
12690 VALUE val_index = ibf_load_small_value(load, &offset);
12692 VALUE key = ibf_load_object(load, key_index);
12693 VALUE val = ibf_load_object(load, val_index);
12694 rb_hash_aset(obj, key, val);
12696 rb_hash_rehash(obj);
12712 range.class_index = 0;
12715 range.beg = (long)ibf_dump_object(dump, beg);
12716 range.end = (long)ibf_dump_object(dump, end);
12731 VALUE beg = ibf_load_object(load, range->beg);
12732 VALUE end = ibf_load_object(load, range->end);
12742 ssize_t len = BIGNUM_LEN(obj);
12743 ssize_t slen = BIGNUM_SIGN(obj) > 0 ? len : len * -1;
12744 BDIGIT *d = BIGNUM_DIGITS(obj);
12746 (void)IBF_W(&slen, ssize_t, 1);
12747 IBF_WP(d, BDIGIT, len);
12754 int sign = bignum->slen > 0;
12755 ssize_t len = sign > 0 ? bignum->slen : -1 * bignum->slen;
12756 VALUE obj = rb_integer_unpack(bignum->digits, len * 2, 2, 0,
12766 if (rb_data_is_encoding(obj)) {
12769 long len = strlen(name) + 1;
12771 data[0] = IBF_OBJECT_DATA_ENCODING;
12773 (void)IBF_W(data,
long, 2);
12774 IBF_WP(name,
char, len);
12777 ibf_dump_object_unsupported(dump, obj);
12784 const long *body = IBF_OBJBODY(
long, offset);
12785 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
12787 const char *data = (
const char *)&body[2];
12790 case IBF_OBJECT_DATA_ENCODING:
12792 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
12797 return ibf_load_object_unsupported(load, header, offset);
12801ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
12804 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
12805 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
12807 (void)IBF_W(data,
long, 2);
12811ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
12814 VALUE a = ibf_load_object(load, nums->a);
12815 VALUE b = ibf_load_object(load, nums->b);
12827 ibf_dump_object_string(dump,
rb_sym2str(obj));
12833 ibf_offset_t reading_pos = offset;
12835 int encindex = (int)ibf_load_small_value(load, &reading_pos);
12836 const long len = (long)ibf_load_small_value(load, &reading_pos);
12837 const char *ptr = load->current_buffer->buff + reading_pos;
12839 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
12840 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
12841 encindex = rb_enc_find_index(
RSTRING_PTR(enc_name_str));
12844 ID id =
rb_intern3(ptr, len, rb_enc_from_index(encindex));
12848typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
12849static ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
12850 ibf_dump_object_unsupported,
12851 ibf_dump_object_unsupported,
12852 ibf_dump_object_class,
12853 ibf_dump_object_unsupported,
12854 ibf_dump_object_float,
12855 ibf_dump_object_string,
12856 ibf_dump_object_regexp,
12857 ibf_dump_object_array,
12858 ibf_dump_object_hash,
12859 ibf_dump_object_struct,
12860 ibf_dump_object_bignum,
12861 ibf_dump_object_unsupported,
12862 ibf_dump_object_data,
12863 ibf_dump_object_unsupported,
12864 ibf_dump_object_complex_rational,
12865 ibf_dump_object_complex_rational,
12866 ibf_dump_object_unsupported,
12867 ibf_dump_object_unsupported,
12868 ibf_dump_object_unsupported,
12869 ibf_dump_object_unsupported,
12870 ibf_dump_object_symbol,
12871 ibf_dump_object_unsupported,
12872 ibf_dump_object_unsupported,
12873 ibf_dump_object_unsupported,
12874 ibf_dump_object_unsupported,
12875 ibf_dump_object_unsupported,
12876 ibf_dump_object_unsupported,
12877 ibf_dump_object_unsupported,
12878 ibf_dump_object_unsupported,
12879 ibf_dump_object_unsupported,
12880 ibf_dump_object_unsupported,
12881 ibf_dump_object_unsupported,
12887 unsigned char byte =
12888 (header.type << 0) |
12889 (header.special_const << 5) |
12890 (header.frozen << 6) |
12891 (header.internal << 7);
12897ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
12899 unsigned char byte = ibf_load_byte(load, offset);
12902 header.type = (
byte >> 0) & 0x1f;
12903 header.special_const = (
byte >> 5) & 0x01;
12904 header.frozen = (
byte >> 6) & 0x01;
12905 header.internal = (
byte >> 7) & 0x01;
12914 ibf_offset_t current_offset;
12915 IBF_ZERO(obj_header);
12916 obj_header.type =
TYPE(obj);
12918 IBF_W_ALIGN(ibf_offset_t);
12919 current_offset = ibf_dump_pos(dump);
12924 obj_header.special_const = TRUE;
12925 obj_header.frozen = TRUE;
12926 obj_header.internal = TRUE;
12927 ibf_dump_object_object_header(dump, obj_header);
12928 ibf_dump_write_small_value(dump, obj);
12932 obj_header.special_const = FALSE;
12934 ibf_dump_object_object_header(dump, obj_header);
12935 (*dump_object_functions[obj_header.type])(dump, obj);
12938 return current_offset;
12942static ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
12943 ibf_load_object_unsupported,
12944 ibf_load_object_unsupported,
12945 ibf_load_object_class,
12946 ibf_load_object_unsupported,
12947 ibf_load_object_float,
12948 ibf_load_object_string,
12949 ibf_load_object_regexp,
12950 ibf_load_object_array,
12951 ibf_load_object_hash,
12952 ibf_load_object_struct,
12953 ibf_load_object_bignum,
12954 ibf_load_object_unsupported,
12955 ibf_load_object_data,
12956 ibf_load_object_unsupported,
12957 ibf_load_object_complex_rational,
12958 ibf_load_object_complex_rational,
12959 ibf_load_object_unsupported,
12960 ibf_load_object_unsupported,
12961 ibf_load_object_unsupported,
12962 ibf_load_object_unsupported,
12963 ibf_load_object_symbol,
12964 ibf_load_object_unsupported,
12965 ibf_load_object_unsupported,
12966 ibf_load_object_unsupported,
12967 ibf_load_object_unsupported,
12968 ibf_load_object_unsupported,
12969 ibf_load_object_unsupported,
12970 ibf_load_object_unsupported,
12971 ibf_load_object_unsupported,
12972 ibf_load_object_unsupported,
12973 ibf_load_object_unsupported,
12974 ibf_load_object_unsupported,
12978ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
12980 if (object_index == 0) {
12984 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
12986 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
12987 ibf_offset_t offset = offsets[object_index];
12988 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
12991 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
12992 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
12993 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
12994 header.type, header.special_const, header.frozen, header.internal);
12996 if (offset >= load->current_buffer->size) {
13000 if (header.special_const) {
13001 ibf_offset_t reading_pos = offset;
13003 obj = ibf_load_small_value(load, &reading_pos);
13006 obj = (*load_object_functions[header.type])(load, &header, offset);
13009 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
13012 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
13013 object_index, obj);
13026ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
13031 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
13032 rb_ary_push(args->offset_list,
UINT2NUM(offset));
13034 return ST_CONTINUE;
13038ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
13040 st_table *obj_table = dump->current_buffer->obj_table;
13041 VALUE offset_list = rb_ary_hidden_new(obj_table->num_entries);
13045 args.offset_list = offset_list;
13047 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
13049 IBF_W_ALIGN(ibf_offset_t);
13050 *obj_list_offset = ibf_dump_pos(dump);
13052 st_index_t size = obj_table->num_entries;
13055 for (i=0; i<size; i++) {
13060 *obj_list_size = (
unsigned int)size;
13064ibf_dump_mark(
void *ptr)
13067 rb_gc_mark(dump->global_buffer.str);
13069 rb_mark_set(dump->global_buffer.obj_table);
13070 rb_mark_set(dump->iseq_table);
13074ibf_dump_free(
void *ptr)
13077 if (dump->global_buffer.obj_table) {
13078 st_free_table(dump->global_buffer.obj_table);
13079 dump->global_buffer.obj_table = 0;
13081 if (dump->iseq_table) {
13082 st_free_table(dump->iseq_table);
13083 dump->iseq_table = 0;
13089ibf_dump_memsize(
const void *ptr)
13092 size_t size =
sizeof(*dump);
13093 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
13094 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
13100 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
13101 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13107 dump->global_buffer.obj_table = NULL;
13108 dump->iseq_table = NULL;
13111 dump->global_buffer.obj_table = ibf_dump_object_table_new();
13112 dump->iseq_table = st_init_numtable();
13114 dump->current_buffer = &dump->global_buffer;
13125 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
13126 ISEQ_BODY(iseq)->local_iseq != iseq) {
13129 if (
RTEST(ISEQ_COVERAGE(iseq))) {
13134 ibf_dump_setup(dump, dump_obj);
13136 ibf_dump_write(dump, &header,
sizeof(header));
13137 ibf_dump_write(dump, RUBY_PLATFORM, strlen(RUBY_PLATFORM) + 1);
13138 ibf_dump_iseq(dump, iseq);
13140 header.magic[0] =
'Y';
13141 header.magic[1] =
'A';
13142 header.magic[2] =
'R';
13143 header.magic[3] =
'B';
13144 header.major_version = IBF_MAJOR_VERSION;
13145 header.minor_version = IBF_MINOR_VERSION;
13146 ibf_dump_iseq_list(dump, &header);
13147 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
13148 header.size = ibf_dump_pos(dump);
13151 VALUE opt_str = opt;
13154 ibf_dump_write(dump, ptr, header.extra_size);
13157 header.extra_size = 0;
13160 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
13162 str = dump->global_buffer.str;
13163 ibf_dump_free(dump);
13169static const ibf_offset_t *
13170ibf_iseq_list(
const struct ibf_load *load)
13172 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
13176rb_ibf_load_iseq_complete(
rb_iseq_t *iseq)
13180 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
13183 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
13184 iseq->aux.loader.index, offset,
13185 load->header->size);
13187 ibf_load_iseq_each(load, iseq, offset);
13188 ISEQ_COMPILE_DATA_CLEAR(iseq);
13190 rb_iseq_init_trace(iseq);
13191 load->iseq = prev_src_iseq;
13198 rb_ibf_load_iseq_complete((
rb_iseq_t *)iseq);
13206 int iseq_index = (int)(
VALUE)index_iseq;
13209 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
13210 (
void *)index_iseq, (
void *)load->iseq_list);
13212 if (iseq_index == -1) {
13216 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
13219 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
13227 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
13230 iseq->aux.loader.obj = load->loader_obj;
13231 iseq->aux.loader.index = iseq_index;
13233 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
13234 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
13236 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
13240 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
13242 rb_ibf_load_iseq_complete(iseq);
13244 if (GET_VM()->builtin_function_table) {
13245 rb_ibf_load_iseq_complete(iseq);
13250 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
13251 (
void *)iseq, (
void *)load->iseq);
13259ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
13261 load->loader_obj = loader_obj;
13262 load->global_buffer.buff = bytes;
13263 load->header = (
struct ibf_header *)load->global_buffer.buff;
13264 load->global_buffer.size = load->header->size;
13265 load->global_buffer.obj_list_offset = load->header->global_object_list_offset;
13266 load->global_buffer.obj_list_size = load->header->global_object_list_size;
13267 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(load->header->iseq_list_size));
13268 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
13271 load->current_buffer = &load->global_buffer;
13273 if (size < load->header->size) {
13276 if (strncmp(load->header->magic,
"YARB", 4) != 0) {
13279 if (load->header->major_version != IBF_MAJOR_VERSION ||
13280 load->header->minor_version != IBF_MINOR_VERSION) {
13282 load->header->major_version, load->header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
13284 if (strcmp(load->global_buffer.buff +
sizeof(
struct ibf_header), RUBY_PLATFORM) != 0) {
13287 if (load->header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
13289 load->header->iseq_list_offset);
13291 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
13293 load->global_buffer.obj_list_offset);
13313ibf_loader_mark(
void *ptr)
13316 rb_gc_mark(load->str);
13317 rb_gc_mark(load->iseq_list);
13318 rb_gc_mark(load->global_buffer.obj_list);
13322ibf_loader_free(
void *ptr)
13329ibf_loader_memsize(
const void *ptr)
13336 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
13337 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
13341rb_iseq_ibf_load(
VALUE str)
13347 ibf_load_setup(load, loader_obj, str);
13348 iseq = ibf_load_iseq(load, 0);
13355rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
13361 ibf_load_setup_bytes(load, loader_obj, bytes, size);
13362 iseq = ibf_load_iseq(load, 0);
13369rb_iseq_ibf_load_extra_data(
VALUE str)
13375 ibf_load_setup(load, loader_obj, str);
13376 extra_str =
rb_str_new(load->global_buffer.buff + load->header->size, load->header->extra_size);
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ALIGNOF
Wraps (or simulates) alignof.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_CLASS
Encountered a new class.
#define RUBY_EVENT_NONE
No events.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define NUM2LL
Old name of RB_NUM2LL.
#define REALLOC_N
Old name of RB_REALLOC_N.
#define ALLOCV
Old name of RB_ALLOCV.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define 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 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 Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
#define xcalloc
Old name of ruby_xcalloc.
#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.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_eStandardError
StandardError exception.
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
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 always regardless of runtime -W flag.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_cArray
Array class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_cNumeric
Numeric class.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cRange
Range class.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it additionally takes an encoding.
ID rb_intern3(const char *name, long len, rb_encoding *enc)
Identical to rb_intern2(), except it additionally takes an encoding.
#define INTEGER_PACK_LITTLE_ENDIAN
Little endian combination.
#define INTEGER_PACK_NEGATIVE
Interprets the input as a signed negative number (unpack only).
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
ID rb_id_attrset(ID id)
Calculates an ID of attribute writer.
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_tmp_new(long len)
Allocates a "temporary" string.
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
VALUE rb_str_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_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of 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.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
#define DECIMAL_SIZE_OF_BITS(n)
an approximation of ceil(n * log10(2)), up to 65536 at least
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
#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.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_CONST_PTR_TRANSIENT
Just another name of rb_array_const_ptr_transient.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define Data_Wrap_Struct(klass, mark, free, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define DATA_PTR(obj)
Convenient getter macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
static VALUE RREGEXP_SRC(VALUE rexp)
Convenient getter function.
#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.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#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_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.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Internal header for Complex.
Internal header for Rational.
const ID * segments
A null-terminated list of ids, used to represent a constant's path idNULL is used to represent the ::...
This is the struct that holds necessary info for a struct.
struct rb_iseq_constant_body::@131 param
parameter information
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
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_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.