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 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
4632static_literal_node_pair_p(
const NODE *node,
const rb_iseq_t *iseq)
4634 return node->nd_head && static_literal_node_p(node->nd_head, iseq) && static_literal_node_p(node->nd_next->nd_head, iseq);
4640 const NODE *line_node = node;
4642 node = node->nd_head;
4644 if (!node || nd_type_p(node, NODE_ZLIST)) {
4646 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
4651 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
4654 for (; node; node = node->nd_next) {
4655 NO_CHECK(COMPILE_(ret,
"hash element", node->nd_head, popped));
4678 const int max_stack_len = 0x100;
4679 const int min_tmp_hash_len = 0x800;
4681 int first_chunk = 1;
4682 DECL_ANCHOR(anchor);
4683 INIT_ANCHOR(anchor);
4686#define FLUSH_CHUNK() \
4688 if (first_chunk) { \
4689 APPEND_LIST(ret, anchor); \
4690 ADD_INSN1(ret, line_node, newhash, INT2FIX(stack_len)); \
4693 ADD_INSN1(ret, line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
4694 ADD_INSN(ret, line_node, swap); \
4695 APPEND_LIST(ret, anchor); \
4696 ADD_SEND(ret, line_node, id_core_hash_merge_ptr, INT2FIX(stack_len + 1)); \
4698 INIT_ANCHOR(anchor); \
4699 first_chunk = stack_len = 0; \
4706 if (static_literal_node_pair_p(node, iseq)) {
4708 const NODE *node_tmp = node->nd_next->nd_next;
4709 for (; node_tmp && static_literal_node_pair_p(node_tmp, iseq); node_tmp = node_tmp->nd_next->nd_next)
4712 if ((first_chunk && stack_len == 0 && !node_tmp) || count >= min_tmp_hash_len) {
4714 VALUE ary = rb_ary_hidden_new(count);
4717 for (; count; count--, node = node->nd_next->nd_next) {
4719 elem[0] = static_literal_value(node->nd_head, iseq);
4720 elem[1] = static_literal_value(node->nd_next->nd_head, iseq);
4721 rb_ary_cat(ary, elem, 2);
4731 ADD_INSN1(ret, line_node, duphash, hash);
4735 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4736 ADD_INSN(ret, line_node, swap);
4738 ADD_INSN1(ret, line_node, putobject, hash);
4740 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
4747 for (; count; count--, node = node->nd_next->nd_next) {
4750 EXPECT_NODE(
"compile_hash", node, NODE_LIST, -1);
4753 if (node->nd_head) {
4755 NO_CHECK(COMPILE_(anchor,
"hash key element", node->nd_head, 0));
4756 NO_CHECK(COMPILE_(anchor,
"hash value element", node->nd_next->nd_head, 0));
4760 if (stack_len >= max_stack_len) FLUSH_CHUNK();
4766 const NODE *kw = node->nd_next->nd_head;
4767 int empty_kw = nd_type_p(kw, NODE_LIT) &&
RB_TYPE_P(kw->nd_lit,
T_HASH);
4768 int first_kw = first_chunk && stack_len == 0;
4769 int last_kw = !node->nd_next->nd_next;
4770 int only_kw = last_kw && first_kw;
4773 if (only_kw && method_call_keywords) {
4781 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
4783 else if (first_kw) {
4787 ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
4794 if (only_kw && method_call_keywords) {
4800 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
4807 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4808 if (first_kw) ADD_INSN1(ret, line_node, newhash,
INT2FIX(0));
4809 else ADD_INSN(ret, line_node, swap);
4811 NO_CHECK(COMPILE(ret,
"keyword splat", kw));
4813 ADD_SEND(ret, line_node, id_core_hash_merge_kwd,
INT2FIX(2));
4828rb_node_case_when_optimizable_literal(
const NODE *
const node)
4830 switch (nd_type(node)) {
4832 VALUE v = node->nd_lit;
4853 return rb_fstring(node->nd_lit);
4860 LABEL *l1,
int only_special_literals,
VALUE literals)
4863 const NODE *val = vals->nd_head;
4864 VALUE lit = rb_node_case_when_optimizable_literal(val);
4867 only_special_literals = 0;
4869 else if (
NIL_P(rb_hash_lookup(literals, lit))) {
4870 rb_hash_aset(literals, lit, (
VALUE)(l1) | 1);
4873 if (nd_type_p(val, NODE_STR)) {
4874 debugp_param(
"nd_lit", val->nd_lit);
4875 lit = rb_fstring(val->nd_lit);
4876 ADD_INSN1(cond_seq, val, putobject, lit);
4880 if (!COMPILE(cond_seq,
"when cond", val))
return -1;
4884 ADD_INSN1(cond_seq, vals, topn,
INT2FIX(1));
4885 ADD_CALL(cond_seq, vals, idEqq,
INT2FIX(1));
4886 ADD_INSNL(cond_seq, val, branchif, l1);
4887 vals = vals->nd_next;
4889 return only_special_literals;
4894 LABEL *l1,
int only_special_literals,
VALUE literals)
4896 const NODE *line_node = vals;
4898 switch (nd_type(vals)) {
4900 if (when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals) < 0)
4904 ADD_INSN (cond_seq, line_node, dup);
4905 CHECK(COMPILE(cond_seq,
"when splat", vals->nd_head));
4906 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
4907 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
4908 ADD_INSNL(cond_seq, line_node, branchif, l1);
4911 CHECK(when_splat_vals(iseq, cond_seq, vals->nd_head, l1, only_special_literals, literals));
4912 CHECK(when_splat_vals(iseq, cond_seq, vals->nd_body, l1, only_special_literals, literals));
4915 CHECK(when_splat_vals(iseq, cond_seq, vals->nd_head, l1, only_special_literals, literals));
4916 ADD_INSN (cond_seq, line_node, dup);
4917 CHECK(COMPILE(cond_seq,
"when argspush body", vals->nd_body));
4918 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
4919 ADD_INSNL(cond_seq, line_node, branchif, l1);
4922 ADD_INSN (cond_seq, line_node, dup);
4923 CHECK(COMPILE(cond_seq,
"when val", vals));
4924 ADD_INSN1(cond_seq, line_node, splatarray,
Qfalse);
4925 ADD_INSN1(cond_seq, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
4926 ADD_INSNL(cond_seq, line_node, branchif, l1);
5019 const NODE *line_node;
5034add_masgn_lhs_node(
struct masgn_state *state,
int lhs_pos,
const NODE *line_node,
int argc,
INSN *before_insn)
5037 rb_bug(
"no masgn_state");
5046 memo->before_insn = before_insn;
5047 memo->line_node = line_node;
5048 memo->argn = state->num_args + 1;
5049 memo->num_args = argc;
5050 state->num_args += argc;
5051 memo->lhs_pos = lhs_pos;
5053 if (!state->first_memo) {
5054 state->first_memo = memo;
5057 state->last_memo->next = memo;
5059 state->last_memo = memo;
5069 switch (nd_type(node)) {
5070 case NODE_ATTRASGN: {
5072 const NODE *line_node = node;
5074 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_ATTRASGN)", node));
5077 iobj = (
INSN *)get_prev_insn((
INSN *)insn_element);
5079 ELEM_REMOVE(LAST_ELEMENT(pre));
5081 pre->last = iobj->link.prev;
5084 int argc = vm_ci_argc(ci) + 1;
5085 ci = ci_argc_set(iseq, ci, argc);
5086 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5090 ADD_INSN(lhs, line_node, swap);
5093 ADD_INSN1(lhs, line_node, topn,
INT2FIX(argc));
5096 if (!add_masgn_lhs_node(state, lhs_pos, line_node, argc, (
INSN *)LAST_ELEMENT(lhs))) {
5101 if (vm_ci_flag(ci) & VM_CALL_ARGS_SPLAT) {
5102 int argc = vm_ci_argc(ci);
5103 ci = ci_argc_set(iseq, ci, argc - 1);
5104 OPERAND_AT(iobj, 0) = (
VALUE)ci;
5106 INSERT_BEFORE_INSN1(iobj, line_node, newarray,
INT2FIX(1));
5107 INSERT_BEFORE_INSN(iobj, line_node, concatarray);
5109 ADD_INSN(lhs, line_node, pop);
5111 ADD_INSN(lhs, line_node, pop);
5113 for (
int i=0; i < argc; i++) {
5114 ADD_INSN(post, line_node, pop);
5119 DECL_ANCHOR(nest_rhs);
5120 INIT_ANCHOR(nest_rhs);
5121 DECL_ANCHOR(nest_lhs);
5122 INIT_ANCHOR(nest_lhs);
5124 int prev_level = state->lhs_level;
5125 bool prev_nested = state->nested;
5127 state->lhs_level = lhs_pos - 1;
5128 CHECK(compile_massign0(iseq, pre, nest_rhs, nest_lhs, post, node, state, 1));
5129 state->lhs_level = prev_level;
5130 state->nested = prev_nested;
5132 ADD_SEQ(lhs, nest_rhs);
5133 ADD_SEQ(lhs, nest_lhs);
5137 if (!node->nd_vid) {
5141 CHECK(COMPILE_POPPED(pre,
"masgn lhs (NODE_CDECL)", node));
5144 iobj = (
INSN *)insn_element;
5147 ELEM_REMOVE(insn_element);
5148 pre->last = iobj->link.prev;
5151 if (!add_masgn_lhs_node(state, lhs_pos, node, 1, (
INSN *)LAST_ELEMENT(lhs))) {
5155 ADD_INSN(post, node, pop);
5160 DECL_ANCHOR(anchor);
5161 INIT_ANCHOR(anchor);
5162 CHECK(COMPILE_POPPED(anchor,
"masgn lhs", node));
5163 ELEM_REMOVE(FIRST_ELEMENT(anchor));
5164 ADD_SEQ(lhs, anchor);
5175 CHECK(compile_massign_opt_lhs(iseq, ret, lhsn->nd_next));
5176 CHECK(compile_massign_lhs(iseq, ret, ret, ret, ret, lhsn->nd_head, NULL, 0));
5183 const NODE *rhsn,
const NODE *orig_lhsn)
5186 const int memsize = numberof(mem);
5188 int llen = 0, rlen = 0;
5190 const NODE *lhsn = orig_lhsn;
5192#define MEMORY(v) { \
5194 if (memindex == memsize) return 0; \
5195 for (i=0; i<memindex; i++) { \
5196 if (mem[i] == (v)) return 0; \
5198 mem[memindex++] = (v); \
5201 if (rhsn == 0 || !nd_type_p(rhsn, NODE_LIST)) {
5206 const NODE *ln = lhsn->nd_head;
5207 switch (nd_type(ln)) {
5219 lhsn = lhsn->nd_next;
5225 NO_CHECK(COMPILE_POPPED(ret,
"masgn val (popped)", rhsn->nd_head));
5228 NO_CHECK(COMPILE(ret,
"masgn val", rhsn->nd_head));
5230 rhsn = rhsn->nd_next;
5235 for (i=0; i<llen-rlen; i++) {
5236 ADD_INSN(ret, orig_lhsn, putnil);
5240 compile_massign_opt_lhs(iseq, ret, orig_lhsn);
5247 const NODE *rhsn = node->nd_value;
5248 const NODE *splatn = node->nd_args;
5249 const NODE *lhsn = node->nd_head;
5250 const NODE *lhsn_count = lhsn;
5251 int lhs_splat = (splatn && NODE_NAMED_REST_P(splatn)) ? 1 : 0;
5257 while (lhsn_count) {
5259 lhsn_count = lhsn_count->nd_next;
5262 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, lhsn->nd_head, state, (llen - lpos) + lhs_splat + state->lhs_level));
5264 lhsn = lhsn->nd_next;
5268 if (nd_type_p(splatn, NODE_POSTARG)) {
5270 const NODE *postn = splatn->nd_2nd;
5271 const NODE *restn = splatn->nd_1st;
5272 int plen = (int)postn->nd_alen;
5274 int flag = 0x02 | (NODE_NAMED_REST_P(restn) ? 0x01 : 0x00);
5276 ADD_INSN2(lhs, splatn, expandarray,
INT2FIX(plen),
INT2FIX(flag));
5278 if (NODE_NAMED_REST_P(restn)) {
5279 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, restn, state, 1 + plen + state->lhs_level));
5282 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, postn->nd_head, state, (plen - ppos) + state->lhs_level));
5284 postn = postn->nd_next;
5289 CHECK(compile_massign_lhs(iseq, pre, rhs, lhs, post, splatn, state, 1 + state->lhs_level));
5294 if (!state->nested) {
5295 NO_CHECK(COMPILE(rhs,
"normal masgn rhs", rhsn));
5299 ADD_INSN(rhs, node, dup);
5302 ADD_INSN2(rhs, node, expandarray,
INT2FIX(llen),
INT2FIX(lhs_splat));
5310 if (!popped || node->nd_args || !compile_massign_opt(iseq, ret, node->nd_value, node->nd_head)) {
5312 state.lhs_level = popped ? 0 : 1;
5315 state.first_memo = NULL;
5316 state.last_memo = NULL;
5326 int ok = compile_massign0(iseq, pre, rhs, lhs, post, node, &state, popped);
5330 VALUE topn_arg =
INT2FIX((state.num_args - memo->argn) + memo->lhs_pos);
5331 for (
int i = 0; i < memo->num_args; i++) {
5332 INSERT_BEFORE_INSN1(memo->before_insn, memo->line_node, topn, topn_arg);
5334 tmp_memo = memo->next;
5343 if (!popped && state.num_args >= 1) {
5345 ADD_INSN1(ret, node, setn,
INT2FIX(state.num_args));
5355 VALUE arr = rb_ary_new();
5357 switch (nd_type(node)) {
5359 rb_ary_unshift(arr,
ID2SYM(node->nd_vid));
5362 rb_ary_unshift(arr,
ID2SYM(node->nd_mid));
5363 rb_ary_unshift(arr,
ID2SYM(idNULL));
5366 rb_ary_unshift(arr,
ID2SYM(node->nd_mid));
5367 node = node->nd_head;
5376compile_const_prefix(
rb_iseq_t *iseq,
const NODE *
const node,
5379 switch (nd_type(node)) {
5381 debugi(
"compile_const_prefix - colon", node->nd_vid);
5382 ADD_INSN1(body, node, putobject,
Qtrue);
5383 ADD_INSN1(body, node, getconstant,
ID2SYM(node->nd_vid));
5386 debugi(
"compile_const_prefix - colon3", node->nd_mid);
5387 ADD_INSN(body, node, pop);
5388 ADD_INSN1(body, node, putobject, rb_cObject);
5389 ADD_INSN1(body, node, putobject,
Qtrue);
5390 ADD_INSN1(body, node, getconstant,
ID2SYM(node->nd_mid));
5393 CHECK(compile_const_prefix(iseq, node->nd_head, pref, body));
5394 debugi(
"compile_const_prefix - colon2", node->nd_mid);
5395 ADD_INSN1(body, node, putobject,
Qfalse);
5396 ADD_INSN1(body, node, getconstant,
ID2SYM(node->nd_mid));
5399 CHECK(COMPILE(pref,
"const colon2 prefix", node));
5408 if (nd_type_p(cpath, NODE_COLON3)) {
5410 ADD_INSN1(ret, cpath, putobject, rb_cObject);
5411 return VM_DEFINECLASS_FLAG_SCOPED;
5413 else if (cpath->nd_head) {
5415 NO_CHECK(COMPILE(ret,
"nd_else->nd_head", cpath->nd_head));
5416 return VM_DEFINECLASS_FLAG_SCOPED;
5420 ADD_INSN1(ret, cpath, putspecialobject,
5421 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5427private_recv_p(
const NODE *node)
5429 if (nd_type_p(node->nd_recv, NODE_SELF)) {
5430 NODE *self = node->nd_recv;
5431 return self->nd_state != 0;
5441compile_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);
5448 enum defined_type expr_type = DEFINED_NOT_DEFINED;
5449 enum node_type
type;
5450 const int line = nd_line(node);
5451 const NODE *line_node = node;
5453 switch (
type = nd_type(node)) {
5457 expr_type = DEFINED_NIL;
5460 expr_type = DEFINED_SELF;
5463 expr_type = DEFINED_TRUE;
5466 expr_type = DEFINED_FALSE;
5470 const NODE *vals = node;
5473 defined_expr0(iseq, ret, vals->nd_head, lfinish,
Qfalse,
false);
5476 lfinish[1] = NEW_LABEL(line);
5478 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5479 }
while ((vals = vals->nd_next) != NULL);
5488 expr_type = DEFINED_EXPR;
5494 expr_type = DEFINED_LVAR;
5497#define PUSH_VAL(type) (needstr == Qfalse ? Qtrue : rb_iseq_defined_string(type))
5499 ADD_INSN(ret, line_node, putnil);
5500 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_IVAR),
5501 ID2SYM(node->nd_vid), PUSH_VAL(DEFINED_IVAR));
5505 ADD_INSN(ret, line_node, putnil);
5506 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_GVAR),
5507 ID2SYM(node->nd_entry), PUSH_VAL(DEFINED_GVAR));
5511 ADD_INSN(ret, line_node, putnil);
5512 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CVAR),
5513 ID2SYM(node->nd_vid), PUSH_VAL(DEFINED_CVAR));
5517 ADD_INSN(ret, line_node, putnil);
5518 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST),
5519 ID2SYM(node->nd_vid), PUSH_VAL(DEFINED_CONST));
5523 lfinish[1] = NEW_LABEL(line);
5525 defined_expr0(iseq, ret, node->nd_head, lfinish,
Qfalse,
false);
5526 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5527 NO_CHECK(COMPILE(ret,
"defined/colon2#nd_head", node->nd_head));
5530 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_CONST_FROM),
5531 ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_CONST));
5534 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
5535 ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_METHOD));
5539 ADD_INSN1(ret, line_node, putobject, rb_cObject);
5540 ADD_INSN3(ret, line_node, defined,
5541 INT2FIX(DEFINED_CONST_FROM),
ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_CONST));
5549 case NODE_ATTRASGN:{
5550 const int explicit_receiver =
5551 (
type == NODE_CALL ||
type == NODE_OPCALL ||
5552 (
type == NODE_ATTRASGN && !private_recv_p(node)));
5554 if (node->nd_args || explicit_receiver) {
5556 lfinish[1] = NEW_LABEL(line);
5559 lfinish[2] = NEW_LABEL(line);
5562 if (node->nd_args) {
5563 defined_expr0(iseq, ret, node->nd_args, lfinish,
Qfalse,
false);
5564 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5566 if (explicit_receiver) {
5567 defined_expr0(iseq, ret, node->nd_recv, lfinish,
Qfalse,
true);
5568 switch (nd_type(node->nd_recv)) {
5574 ADD_INSNL(ret, line_node, branchunless, lfinish[2]);
5575 compile_call(iseq, ret, node->nd_recv, nd_type(node->nd_recv), line_node, 0,
true);
5578 ADD_INSNL(ret, line_node, branchunless, lfinish[1]);
5579 NO_CHECK(COMPILE(ret,
"defined/recv", node->nd_recv));
5583 ADD_INSN(ret, line_node, dup);
5585 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_METHOD),
5586 ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_METHOD));
5589 ADD_INSN(ret, line_node, putself);
5591 ADD_INSN(ret, line_node, dup);
5593 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_FUNC),
5594 ID2SYM(node->nd_mid), PUSH_VAL(DEFINED_METHOD));
5600 ADD_INSN(ret, line_node, putnil);
5601 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_YIELD), 0,
5602 PUSH_VAL(DEFINED_YIELD));
5607 ADD_INSN(ret, line_node, putnil);
5608 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_REF),
5609 INT2FIX((node->nd_nth << 1) | (
type == NODE_BACK_REF)),
5610 PUSH_VAL(DEFINED_GVAR));
5615 ADD_INSN(ret, line_node, putnil);
5616 ADD_INSN3(ret, line_node, defined,
INT2FIX(DEFINED_ZSUPER), 0,
5617 PUSH_VAL(DEFINED_ZSUPER));
5623 case NODE_OP_ASGN_OR:
5624 case NODE_OP_ASGN_AND:
5632 expr_type = DEFINED_ASGN;
5636 assert(expr_type != DEFINED_NOT_DEFINED);
5639 VALUE str = rb_iseq_defined_string(expr_type);
5640 ADD_INSN1(ret, line_node, putobject, str);
5643 ADD_INSN1(ret, line_node, putobject,
Qtrue);
5650 NODE dummy_line_node = generate_dummy_line_node(0, -1);
5651 ADD_INSN(ret, &dummy_line_node, putnil);
5652 iseq_set_exception_local_table(iseq);
5660 defined_expr0(iseq, ret, node, lfinish, needstr,
false);
5662 int line = nd_line(node);
5663 LABEL *lstart = NEW_LABEL(line);
5664 LABEL *lend = NEW_LABEL(line);
5667 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
5668 rescue = new_child_iseq_with_callback(iseq, ifunc,
5670 ISEQ_BODY(iseq)->location.label),
5671 iseq, ISEQ_TYPE_RESCUE, 0);
5672 lstart->rescued = LABEL_RESCUE_BEG;
5673 lend->rescued = LABEL_RESCUE_END;
5674 APPEND_LABEL(ret, lcur, lstart);
5675 ADD_LABEL(ret, lend);
5676 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
5683 const int line = nd_line(node);
5684 const NODE *line_node = node;
5685 if (!node->nd_head) {
5686 VALUE str = rb_iseq_defined_string(DEFINED_NIL);
5687 ADD_INSN1(ret, line_node, putobject, str);
5692 lfinish[0] = NEW_LABEL(line);
5695 defined_expr(iseq, ret, node->nd_head, lfinish, needstr);
5697 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, line_node, BIN(putnil), 0)->link);
5698 ADD_INSN(ret, line_node, swap);
5700 ADD_LABEL(ret, lfinish[2]);
5702 ADD_INSN(ret, line_node, pop);
5703 ADD_LABEL(ret, lfinish[1]);
5705 ADD_LABEL(ret, lfinish[0]);
5711make_name_for_block(
const rb_iseq_t *orig_iseq)
5716 if (ISEQ_BODY(orig_iseq)->parent_iseq != 0) {
5717 while (ISEQ_BODY(orig_iseq)->local_iseq != iseq) {
5718 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_BLOCK) {
5721 iseq = ISEQ_BODY(iseq)->parent_iseq;
5726 return rb_sprintf(
"block in %"PRIsVALUE, ISEQ_BODY(iseq)->location.label);
5729 return rb_sprintf(
"block (%d levels) in %"PRIsVALUE, level, ISEQ_BODY(iseq)->location.label);
5738 enl->ensure_node = node;
5739 enl->prev = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
5741 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl;
5751 while (erange->next != 0) {
5752 erange = erange->next;
5756 ne->end = erange->end;
5757 erange->end = lstart;
5763can_add_ensure_iseq(
const rb_iseq_t *iseq)
5766 if (ISEQ_COMPILE_DATA(iseq)->in_rescue && (e = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack) != NULL) {
5768 if (e->ensure_node)
return false;
5778 assert(can_add_ensure_iseq(iseq));
5781 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
5783 DECL_ANCHOR(ensure);
5785 INIT_ANCHOR(ensure);
5787 if (enlp->erange != NULL) {
5788 DECL_ANCHOR(ensure_part);
5789 LABEL *lstart = NEW_LABEL(0);
5790 LABEL *lend = NEW_LABEL(0);
5791 INIT_ANCHOR(ensure_part);
5793 add_ensure_range(iseq, enlp->erange, lstart, lend);
5795 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
5796 ADD_LABEL(ensure_part, lstart);
5797 NO_CHECK(COMPILE_POPPED(ensure_part,
"ensure part", enlp->ensure_node));
5798 ADD_LABEL(ensure_part, lend);
5799 ADD_SEQ(ensure, ensure_part);
5808 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
5809 ADD_SEQ(ret, ensure);
5813check_keyword(
const NODE *node)
5817 if (nd_type_p(node, NODE_LIST)) {
5818 while (node->nd_next) {
5819 node = node->nd_next;
5821 node = node->nd_head;
5824 return keyword_node_p(node);
5832 switch (nd_type(argn)) {
5834 NO_CHECK(COMPILE(args,
"args (splat)", argn->nd_head));
5835 ADD_INSN1(args, argn, splatarray, RBOOL(dup_rest));
5836 if (flag) *flag |= VM_CALL_ARGS_SPLAT;
5840 case NODE_ARGSPUSH: {
5841 int next_is_list = (nd_type_p(argn->nd_head, NODE_LIST));
5842 VALUE argc = setup_args_core(iseq, args, argn->nd_head, 1, NULL, NULL);
5843 if (nd_type_p(argn->nd_body, NODE_LIST)) {
5845 int rest_len = compile_args(iseq, args, argn->nd_body, NULL, NULL);
5846 ADD_INSN1(args, argn, newarray,
INT2FIX(rest_len));
5849 NO_CHECK(COMPILE(args,
"args (cat: splat)", argn->nd_body));
5852 *flag |= VM_CALL_ARGS_SPLAT;
5855 if (check_keyword(argn->nd_body))
5856 *flag |= VM_CALL_KW_SPLAT;
5858 if (nd_type_p(argn, NODE_ARGSCAT)) {
5860 ADD_INSN1(args, argn, splatarray,
Qtrue);
5864 ADD_INSN1(args, argn, splatarray,
Qfalse);
5865 ADD_INSN(args, argn, concatarray);
5870 ADD_INSN1(args, argn, newarray,
INT2FIX(1));
5871 ADD_INSN(args, argn, concatarray);
5876 int len = compile_args(iseq, args, argn, keywords, flag);
5880 UNKNOWN_NODE(
"setup_arg", argn,
Qnil);
5892 if (argn && nd_type_p(argn, NODE_BLOCK_PASS)) {
5893 unsigned int dup_rest = 1;
5894 DECL_ANCHOR(arg_block);
5895 INIT_ANCHOR(arg_block);
5896 NO_CHECK(COMPILE(arg_block,
"block", argn->nd_body));
5898 *flag |= VM_CALL_ARGS_BLOCKARG;
5900 if (LIST_INSN_SIZE_ONE(arg_block)) {
5902 if (IS_INSN(elem)) {
5904 if (iobj->insn_id == BIN(getblockparam)) {
5905 iobj->insn_id = BIN(getblockparamproxy);
5910 ret = setup_args_core(iseq, args, argn->nd_head, dup_rest, flag, keywords);
5911 ADD_SEQ(args, arg_block);
5914 ret = setup_args_core(iseq, args, argn, 0, flag, keywords);
5922 const NODE *body = ptr;
5923 int line = nd_line(body);
5925 const rb_iseq_t *block = NEW_CHILD_ISEQ(body, make_name_for_block(ISEQ_BODY(iseq)->parent_iseq), ISEQ_TYPE_BLOCK, line);
5927 ADD_INSN1(ret, body, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5928 ADD_CALL_WITH_BLOCK(ret, body, id_core_set_postexe, argc, block);
5930 iseq_set_local_table(iseq, 0);
5938 int line = nd_line(node);
5939 const NODE *line_node = node;
5940 LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
5942#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
5943 ADD_INSN1(ret, line_node, getglobal,
ID2SYM(idBACKREF));
5947 ADD_INSN(ret, line_node, dup);
5948 ADD_INSNL(ret, line_node, branchunless, fail_label);
5950 for (vars = node; vars; vars = vars->nd_next) {
5952 if (vars->nd_next) {
5953 ADD_INSN(ret, line_node, dup);
5956 NO_CHECK(COMPILE_POPPED(ret,
"capture", vars->nd_head));
5958 cap = new_insn_send(iseq, line_node, idAREF,
INT2FIX(1),
5961#if !defined(NAMED_CAPTURE_SINGLE_OPT) || NAMED_CAPTURE_SINGLE_OPT-0
5962 if (!vars->nd_next && vars == node) {
5967 ADD_INSNL(nom, line_node, jump, end_label);
5968 ADD_LABEL(nom, fail_label);
5970 ADD_INSN(nom, line_node, pop);
5971 ADD_INSN(nom, line_node, putnil);
5973 ADD_LABEL(nom, end_label);
5974 (nom->last->next = cap->link.next)->prev = nom->last;
5975 (cap->link.next = nom->anchor.next)->prev = &cap->link;
5980 ADD_INSNL(ret, line_node, jump, end_label);
5981 ADD_LABEL(ret, fail_label);
5982 ADD_INSN(ret, line_node, pop);
5983 for (vars = node; vars; vars = vars->nd_next) {
5985 NO_CHECK(COMPILE_POPPED(ret,
"capture", vars->nd_head));
5987 ((
INSN*)last)->insn_id = BIN(putnil);
5988 ((
INSN*)last)->operand_size = 0;
5990 ADD_LABEL(ret, end_label);
5994optimizable_range_item_p(
const NODE *n)
5996 if (!n)
return FALSE;
5997 switch (nd_type(n)) {
6011 const NODE *
const node_body =
type == NODE_IF ? node->nd_body : node->nd_else;
6012 const NODE *
const node_else =
type == NODE_IF ? node->nd_else : node->nd_body;
6014 const int line = nd_line(node);
6015 const NODE *line_node = node;
6016 DECL_ANCHOR(cond_seq);
6017 DECL_ANCHOR(then_seq);
6018 DECL_ANCHOR(else_seq);
6019 LABEL *then_label, *else_label, *end_label;
6022 VALUE catch_table = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
6023 long catch_table_size =
NIL_P(catch_table) ? 0 :
RARRAY_LEN(catch_table);
6025 INIT_ANCHOR(cond_seq);
6026 INIT_ANCHOR(then_seq);
6027 INIT_ANCHOR(else_seq);
6028 then_label = NEW_LABEL(line);
6029 else_label = NEW_LABEL(line);
6032 compile_branch_condition(iseq, cond_seq, node->nd_cond,
6033 then_label, else_label);
6035 ci_size = body->ci_size;
6036 CHECK(COMPILE_(then_seq,
"then", node_body, popped));
6037 catch_table = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
6038 if (!then_label->refcnt) {
6039 body->ci_size = ci_size;
6040 if (!
NIL_P(catch_table)) rb_ary_set_len(catch_table, catch_table_size);
6046 ci_size = body->ci_size;
6047 CHECK(COMPILE_(else_seq,
"else", node_else, popped));
6048 catch_table = ISEQ_COMPILE_DATA(iseq)->catch_table_ary;
6049 if (!else_label->refcnt) {
6050 body->ci_size = ci_size;
6051 if (!
NIL_P(catch_table)) rb_ary_set_len(catch_table, catch_table_size);
6057 ADD_SEQ(ret, cond_seq);
6059 if (then_label->refcnt && else_label->refcnt) {
6060 branches = decl_branch_base(iseq, node,
type == NODE_IF ?
"if" :
"unless");
6063 if (then_label->refcnt) {
6064 ADD_LABEL(ret, then_label);
6065 if (else_label->refcnt) {
6066 add_trace_branch_coverage(
6069 node_body ? node_body : node,
6071 type == NODE_IF ?
"then" :
"else",
6073 end_label = NEW_LABEL(line);
6074 ADD_INSNL(then_seq, line_node, jump, end_label);
6076 ADD_INSN(then_seq, line_node, pop);
6079 ADD_SEQ(ret, then_seq);
6082 if (else_label->refcnt) {
6083 ADD_LABEL(ret, else_label);
6084 if (then_label->refcnt) {
6085 add_trace_branch_coverage(
6088 node_else ? node_else : node,
6090 type == NODE_IF ?
"else" :
"then",
6093 ADD_SEQ(ret, else_seq);
6097 ADD_LABEL(ret, end_label);
6107 const NODE *node = orig_node;
6108 LABEL *endlabel, *elselabel;
6110 DECL_ANCHOR(body_seq);
6111 DECL_ANCHOR(cond_seq);
6112 int only_special_literals = 1;
6113 VALUE literals = rb_hash_new();
6115 enum node_type
type;
6116 const NODE *line_node;
6121 INIT_ANCHOR(body_seq);
6122 INIT_ANCHOR(cond_seq);
6124 RHASH_TBL_RAW(literals)->type = &cdhash_type;
6126 CHECK(COMPILE(head,
"case base", node->nd_head));
6128 branches = decl_branch_base(iseq, node,
"case");
6130 node = node->nd_body;
6131 EXPECT_NODE(
"NODE_CASE", node, NODE_WHEN, COMPILE_NG);
6132 type = nd_type(node);
6133 line = nd_line(node);
6136 endlabel = NEW_LABEL(line);
6137 elselabel = NEW_LABEL(line);
6141 while (
type == NODE_WHEN) {
6144 l1 = NEW_LABEL(line);
6145 ADD_LABEL(body_seq, l1);
6146 ADD_INSN(body_seq, line_node, pop);
6147 add_trace_branch_coverage(
6150 node->nd_body ? node->nd_body : node,
6154 CHECK(COMPILE_(body_seq,
"when body", node->nd_body, popped));
6155 ADD_INSNL(body_seq, line_node, jump, endlabel);
6157 vals = node->nd_head;
6159 switch (nd_type(vals)) {
6161 only_special_literals = when_vals(iseq, cond_seq, vals, l1, only_special_literals, literals);
6162 if (only_special_literals < 0)
return COMPILE_NG;
6167 only_special_literals = 0;
6168 CHECK(when_splat_vals(iseq, cond_seq, vals, l1, only_special_literals, literals));
6171 UNKNOWN_NODE(
"NODE_CASE", vals, COMPILE_NG);
6175 EXPECT_NODE_NONULL(
"NODE_CASE", node, NODE_LIST, COMPILE_NG);
6178 node = node->nd_next;
6182 type = nd_type(node);
6183 line = nd_line(node);
6188 ADD_LABEL(cond_seq, elselabel);
6189 ADD_INSN(cond_seq, line_node, pop);
6190 add_trace_branch_coverage(iseq, cond_seq, node, branch_id,
"else", branches);
6191 CHECK(COMPILE_(cond_seq,
"else", node, popped));
6192 ADD_INSNL(cond_seq, line_node, jump, endlabel);
6195 debugs(
"== else (implicit)\n");
6196 ADD_LABEL(cond_seq, elselabel);
6197 ADD_INSN(cond_seq, orig_node, pop);
6198 add_trace_branch_coverage(iseq, cond_seq, orig_node, branch_id,
"else", branches);
6200 ADD_INSN(cond_seq, orig_node, putnil);
6202 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
6205 if (only_special_literals && ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
6206 ADD_INSN(ret, orig_node, dup);
6207 ADD_INSN2(ret, orig_node, opt_case_dispatch, literals, elselabel);
6209 LABEL_REF(elselabel);
6212 ADD_SEQ(ret, cond_seq);
6213 ADD_SEQ(ret, body_seq);
6214 ADD_LABEL(ret, endlabel);
6223 const NODE *node = orig_node->nd_body;
6225 DECL_ANCHOR(body_seq);
6229 branches = decl_branch_base(iseq, orig_node,
"case");
6231 INIT_ANCHOR(body_seq);
6232 endlabel = NEW_LABEL(nd_line(node));
6234 while (node && nd_type_p(node, NODE_WHEN)) {
6235 const int line = nd_line(node);
6236 LABEL *l1 = NEW_LABEL(line);
6237 ADD_LABEL(body_seq, l1);
6238 add_trace_branch_coverage(
6241 node->nd_body ? node->nd_body : node,
6245 CHECK(COMPILE_(body_seq,
"when", node->nd_body, popped));
6246 ADD_INSNL(body_seq, node, jump, endlabel);
6248 vals = node->nd_head;
6250 EXPECT_NODE_NONULL(
"NODE_WHEN", node, NODE_LIST, COMPILE_NG);
6252 switch (nd_type(vals)) {
6256 val = vals->nd_head;
6257 lnext = NEW_LABEL(nd_line(val));
6258 debug_compile(
"== when2\n", (
void)0);
6259 CHECK(compile_branch_condition(iseq, ret, val, l1, lnext));
6260 ADD_LABEL(ret, lnext);
6261 vals = vals->nd_next;
6267 ADD_INSN(ret, vals, putnil);
6268 CHECK(COMPILE(ret,
"when2/cond splat", vals));
6269 ADD_INSN1(ret, vals, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
6270 ADD_INSNL(ret, vals, branchif, l1);
6273 UNKNOWN_NODE(
"NODE_WHEN", vals, COMPILE_NG);
6275 node = node->nd_next;
6278 add_trace_branch_coverage(
6281 node ? node : orig_node,
6285 CHECK(COMPILE_(ret,
"else", node, popped));
6286 ADD_INSNL(ret, orig_node, jump, endlabel);
6288 ADD_SEQ(ret, body_seq);
6289 ADD_LABEL(ret, endlabel);
6293static 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);
6295static 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);
6296static 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);
6297static int iseq_compile_pattern_set_general_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
VALUE errmsg,
int base_index);
6298static 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);
6299static int iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index);
6301#define CASE3_BI_OFFSET_DECONSTRUCTED_CACHE 0
6302#define CASE3_BI_OFFSET_ERROR_STRING 1
6303#define CASE3_BI_OFFSET_KEY_ERROR_P 2
6304#define CASE3_BI_OFFSET_KEY_ERROR_MATCHEE 3
6305#define CASE3_BI_OFFSET_KEY_ERROR_KEY 4
6308iseq_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)
6310 const int line = nd_line(node);
6311 const NODE *line_node = node;
6313 switch (nd_type(node)) {
6368 const NODE *args = apinfo->pre_args;
6369 const int pre_args_num = apinfo->pre_args ?
rb_long2int(apinfo->pre_args->nd_alen) : 0;
6370 const int post_args_num = apinfo->post_args ?
rb_long2int(apinfo->post_args->nd_alen) : 0;
6372 const int min_argc = pre_args_num + post_args_num;
6373 const int use_rest_num = apinfo->rest_arg && (NODE_NAMED_REST_P(apinfo->rest_arg) ||
6374 (!NODE_NAMED_REST_P(apinfo->rest_arg) && post_args_num > 0));
6376 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
6378 match_failed = NEW_LABEL(line);
6379 type_error = NEW_LABEL(line);
6380 deconstruct = NEW_LABEL(line);
6381 deconstructed = NEW_LABEL(line);
6384 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
6385 ADD_INSN(ret, line_node, swap);
6391 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
6393 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
6395 ADD_INSN(ret, line_node, dup);
6396 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6397 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
6398 ADD_SEND(ret, line_node, apinfo->rest_arg ? idGE : idEq,
INT2FIX(1));
6399 if (in_single_pattern) {
6400 CHECK(iseq_compile_pattern_set_length_errmsg(iseq, ret, node,
6401 apinfo->rest_arg ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)") :
6402 rb_fstring_lit(
"%p length mismatch (given %p, expected %p)"),
6403 INT2FIX(min_argc), base_index + 1 ));
6405 ADD_INSNL(ret, line_node, branchunless, match_failed);
6407 for (i = 0; i < pre_args_num; i++) {
6408 ADD_INSN(ret, line_node, dup);
6409 ADD_INSN1(ret, line_node, putobject,
INT2FIX(i));
6410 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
6411 CHECK(iseq_compile_pattern_match(iseq, ret, args->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6412 args = args->nd_next;
6415 if (apinfo->rest_arg) {
6416 if (NODE_NAMED_REST_P(apinfo->rest_arg)) {
6417 ADD_INSN(ret, line_node, dup);
6418 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num));
6419 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
6420 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6421 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
6422 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
6423 ADD_INSN1(ret, line_node, setn,
INT2FIX(4));
6424 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
6426 CHECK(iseq_compile_pattern_match(iseq, ret, apinfo->rest_arg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6429 if (post_args_num > 0) {
6430 ADD_INSN(ret, line_node, dup);
6431 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6432 ADD_INSN1(ret, line_node, putobject,
INT2FIX(min_argc));
6433 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
6434 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
6435 ADD_INSN(ret, line_node, pop);
6440 args = apinfo->post_args;
6441 for (i = 0; i < post_args_num; i++) {
6442 ADD_INSN(ret, line_node, dup);
6444 ADD_INSN1(ret, line_node, putobject,
INT2FIX(pre_args_num + i));
6445 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6446 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6448 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
6449 CHECK(iseq_compile_pattern_match(iseq, ret, args->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6450 args = args->nd_next;
6453 ADD_INSN(ret, line_node, pop);
6455 ADD_INSN(ret, line_node, pop);
6457 ADD_INSNL(ret, line_node, jump, matched);
6458 ADD_INSN(ret, line_node, putnil);
6460 ADD_INSN(ret, line_node, putnil);
6463 ADD_LABEL(ret, type_error);
6464 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6466 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
6467 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
6468 ADD_INSN(ret, line_node, pop);
6470 ADD_LABEL(ret, match_failed);
6471 ADD_INSN(ret, line_node, pop);
6473 ADD_INSN(ret, line_node, pop);
6475 ADD_INSNL(ret, line_node, jump, unmatched);
6529 const NODE *args = fpinfo->args;
6530 const int args_num = fpinfo->args ?
rb_long2int(fpinfo->args->nd_alen) : 0;
6532 LABEL *match_failed, *type_error, *deconstruct, *deconstructed;
6533 match_failed = NEW_LABEL(line);
6534 type_error = NEW_LABEL(line);
6535 deconstruct = NEW_LABEL(line);
6536 deconstructed = NEW_LABEL(line);
6538 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
6540 CHECK(iseq_compile_array_deconstruct(iseq, ret, node, deconstruct, deconstructed, match_failed, type_error, in_single_pattern, base_index, use_deconstructed_cache));
6542 ADD_INSN(ret, line_node, dup);
6543 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6544 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
6545 ADD_SEND(ret, line_node, idGE,
INT2FIX(1));
6546 if (in_single_pattern) {
6547 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 ));
6549 ADD_INSNL(ret, line_node, branchunless, match_failed);
6552 LABEL *while_begin = NEW_LABEL(nd_line(node));
6553 LABEL *next_loop = NEW_LABEL(nd_line(node));
6554 LABEL *find_succeeded = NEW_LABEL(line);
6555 LABEL *find_failed = NEW_LABEL(nd_line(node));
6558 ADD_INSN(ret, line_node, dup);
6559 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
6561 ADD_INSN(ret, line_node, dup);
6562 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
6563 ADD_SEND(ret, line_node, idMINUS,
INT2FIX(1));
6565 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
6567 ADD_LABEL(ret, while_begin);
6569 ADD_INSN(ret, line_node, dup);
6570 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
6571 ADD_SEND(ret, line_node, idLE,
INT2FIX(1));
6572 ADD_INSNL(ret, line_node, branchunless, find_failed);
6574 for (j = 0; j < args_num; j++) {
6575 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6576 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
6578 ADD_INSN1(ret, line_node, putobject,
INT2FIX(j));
6579 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6581 ADD_SEND(ret, line_node, idAREF,
INT2FIX(1));
6583 CHECK(iseq_compile_pattern_match(iseq, ret, args->nd_head, next_loop, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
6584 args = args->nd_next;
6587 if (NODE_NAMED_REST_P(fpinfo->pre_rest_arg)) {
6588 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6589 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
6590 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
6591 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
6592 CHECK(iseq_compile_pattern_match(iseq, ret, fpinfo->pre_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
6594 if (NODE_NAMED_REST_P(fpinfo->post_rest_arg)) {
6595 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6596 ADD_INSN1(ret, line_node, topn,
INT2FIX(1));
6597 ADD_INSN1(ret, line_node, putobject,
INT2FIX(args_num));
6598 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6599 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6600 ADD_SEND(ret, line_node, idAREF,
INT2FIX(2));
6601 CHECK(iseq_compile_pattern_match(iseq, ret, fpinfo->post_rest_arg, find_failed, in_single_pattern, in_alt_pattern, base_index + 4 ,
false));
6603 ADD_INSNL(ret, line_node, jump, find_succeeded);
6605 ADD_LABEL(ret, next_loop);
6606 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
6607 ADD_SEND(ret, line_node, idPLUS,
INT2FIX(1));
6608 ADD_INSNL(ret, line_node, jump, while_begin);
6610 ADD_LABEL(ret, find_failed);
6611 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
6612 if (in_single_pattern) {
6613 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6614 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p does not match to find pattern"));
6615 ADD_INSN1(ret, line_node, topn,
INT2FIX(2));
6616 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
6617 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
6619 ADD_INSN1(ret, line_node, putobject,
Qfalse);
6620 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
6622 ADD_INSN(ret, line_node, pop);
6623 ADD_INSN(ret, line_node, pop);
6625 ADD_INSNL(ret, line_node, jump, match_failed);
6626 ADD_INSN1(ret, line_node, dupn,
INT2FIX(3));
6628 ADD_LABEL(ret, find_succeeded);
6629 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(3));
6632 ADD_INSN(ret, line_node, pop);
6633 ADD_INSNL(ret, line_node, jump, matched);
6634 ADD_INSN(ret, line_node, putnil);
6636 ADD_LABEL(ret, type_error);
6637 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6639 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct must return Array"));
6640 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
6641 ADD_INSN(ret, line_node, pop);
6643 ADD_LABEL(ret, match_failed);
6644 ADD_INSN(ret, line_node, pop);
6645 ADD_INSNL(ret, line_node, jump, unmatched);
6709 LABEL *match_failed, *type_error;
6712 match_failed = NEW_LABEL(line);
6713 type_error = NEW_LABEL(line);
6715 if (node->nd_pkwargs && !node->nd_pkwrestarg) {
6716 const NODE *kw_args = node->nd_pkwargs->nd_head;
6717 keys = rb_ary_new_capa(kw_args ? kw_args->nd_alen/2 : 0);
6719 rb_ary_push(keys, kw_args->nd_head->nd_lit);
6720 kw_args = kw_args->nd_next->nd_next;
6724 CHECK(iseq_compile_pattern_constant(iseq, ret, node, match_failed, in_single_pattern, base_index));
6726 ADD_INSN(ret, line_node, dup);
6727 ADD_INSN1(ret, line_node, putobject,
ID2SYM(
rb_intern(
"deconstruct_keys")));
6728 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
6729 if (in_single_pattern) {
6730 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1 ));
6732 ADD_INSNL(ret, line_node, branchunless, match_failed);
6735 ADD_INSN(ret, line_node, putnil);
6738 ADD_INSN1(ret, line_node, duparray, keys);
6743 ADD_INSN(ret, line_node, dup);
6745 ADD_INSNL(ret, line_node, branchunless, type_error);
6747 if (node->nd_pkwrestarg) {
6751 if (node->nd_pkwargs) {
6755 args = node->nd_pkwargs->nd_head;
6757 DECL_ANCHOR(match_values);
6758 INIT_ANCHOR(match_values);
6760 for (i = 0; i < keys_num; i++) {
6761 NODE *key_node = args->nd_head;
6762 NODE *value_node = args->nd_next->nd_head;
6765 if (!nd_type_p(key_node, NODE_LIT)) {
6766 UNKNOWN_NODE(
"NODE_IN", key_node, COMPILE_NG);
6768 key = key_node->nd_lit;
6770 ADD_INSN(ret, line_node, dup);
6771 ADD_INSN1(ret, line_node, putobject, key);
6773 if (in_single_pattern) {
6774 LABEL *match_succeeded;
6775 match_succeeded = NEW_LABEL(line);
6777 ADD_INSN(ret, line_node, dup);
6778 ADD_INSNL(ret, line_node, branchif, match_succeeded);
6781 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 2 ));
6782 ADD_INSN1(ret, line_node, putobject,
Qtrue);
6783 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 3 ));
6784 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
6785 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4 ));
6786 ADD_INSN1(ret, line_node, putobject, key);
6787 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_KEY + 5 ));
6789 ADD_INSN1(ret, line_node, adjuststack,
INT2FIX(4));
6791 ADD_LABEL(ret, match_succeeded);
6793 ADD_INSNL(ret, line_node, branchunless, match_failed);
6795 ADD_INSN(match_values, line_node, dup);
6796 ADD_INSN1(match_values, line_node, putobject, key);
6797 ADD_SEND(match_values, line_node, node->nd_pkwrestarg ?
rb_intern(
"delete") : idAREF,
INT2FIX(1));
6798 CHECK(iseq_compile_pattern_match(iseq, match_values, value_node, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6799 args = args->nd_next->nd_next;
6801 ADD_SEQ(ret, match_values);
6805 ADD_INSN(ret, line_node, dup);
6806 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
6807 if (in_single_pattern) {
6808 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p is not empty"), base_index + 1 ));
6810 ADD_INSNL(ret, line_node, branchunless, match_failed);
6813 if (node->nd_pkwrestarg) {
6814 if (node->nd_pkwrestarg == NODE_SPECIAL_NO_REST_KEYWORD) {
6815 ADD_INSN(ret, line_node, dup);
6816 ADD_SEND(ret, line_node, idEmptyP,
INT2FIX(0));
6817 if (in_single_pattern) {
6818 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1 ));
6820 ADD_INSNL(ret, line_node, branchunless, match_failed);
6823 ADD_INSN(ret, line_node, dup);
6824 CHECK(iseq_compile_pattern_match(iseq, ret, node->nd_pkwrestarg, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 ,
false));
6828 ADD_INSN(ret, line_node, pop);
6829 ADD_INSNL(ret, line_node, jump, matched);
6830 ADD_INSN(ret, line_node, putnil);
6832 ADD_LABEL(ret, type_error);
6833 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6835 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"deconstruct_keys must return Hash"));
6836 ADD_SEND(ret, line_node, id_core_raise,
INT2FIX(2));
6837 ADD_INSN(ret, line_node, pop);
6839 ADD_LABEL(ret, match_failed);
6840 ADD_INSN(ret, line_node, pop);
6841 ADD_INSNL(ret, line_node, jump, unmatched);
6868 CHECK(COMPILE(ret,
"case in literal", node));
6869 if (in_single_pattern) {
6870 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
6872 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
6873 if (in_single_pattern) {
6874 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 2 ));
6876 ADD_INSNL(ret, line_node, branchif, matched);
6877 ADD_INSNL(ret, line_node, jump, unmatched);
6881 ID id = node->nd_vid;
6882 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
6884 if (in_alt_pattern) {
6886 if (name && strlen(name) > 0 && name[0] !=
'_') {
6887 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
6893 ADD_SETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
6894 ADD_INSNL(ret, line_node, jump, matched);
6899 ID id = node->nd_vid;
6901 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
6903 if (in_alt_pattern) {
6905 if (name && strlen(name) > 0 && name[0] !=
'_') {
6906 COMPILE_ERROR(ERROR_ARGS
"illegal variable in alternative pattern (%"PRIsVALUE
")",
6913 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
6917 ADD_SETLOCAL(ret, line_node, ls - idx, lv);
6918 ADD_INSNL(ret, line_node, jump, matched);
6923 LABEL *match_failed;
6924 match_failed = unmatched;
6925 CHECK(iseq_compile_pattern_match(iseq, ret, node->nd_body, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
6926 CHECK(COMPILE(ret,
"case in if", node->nd_cond));
6927 if (in_single_pattern) {
6928 LABEL *match_succeeded;
6929 match_succeeded = NEW_LABEL(line);
6931 ADD_INSN(ret, line_node, dup);
6932 if (nd_type_p(node, NODE_IF)) {
6933 ADD_INSNL(ret, line_node, branchif, match_succeeded);
6936 ADD_INSNL(ret, line_node, branchunless, match_succeeded);
6939 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"guard clause does not return true"));
6940 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
6941 ADD_INSN1(ret, line_node, putobject,
Qfalse);
6942 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
6944 ADD_INSN(ret, line_node, pop);
6945 ADD_INSN(ret, line_node, pop);
6947 ADD_LABEL(ret, match_succeeded);
6949 if (nd_type_p(node, NODE_IF)) {
6950 ADD_INSNL(ret, line_node, branchunless, match_failed);
6953 ADD_INSNL(ret, line_node, branchif, match_failed);
6955 ADD_INSNL(ret, line_node, jump, matched);
6960 LABEL *match_failed;
6961 match_failed = NEW_LABEL(line);
6964 if (! (nd_type_p(n, NODE_LIST) && n->nd_alen == 2)) {
6965 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
6969 ADD_INSN(ret, line_node, dup);
6970 CHECK(iseq_compile_pattern_match(iseq, ret, n->nd_head, match_failed, in_single_pattern, in_alt_pattern, base_index + 1 , use_deconstructed_cache));
6971 CHECK(iseq_compile_pattern_each(iseq, ret, n->nd_next->nd_head, matched, match_failed, in_single_pattern, in_alt_pattern, base_index,
false));
6972 ADD_INSN(ret, line_node, putnil);
6974 ADD_LABEL(ret, match_failed);
6975 ADD_INSN(ret, line_node, pop);
6976 ADD_INSNL(ret, line_node, jump, unmatched);
6980 LABEL *match_succeeded, *fin;
6981 match_succeeded = NEW_LABEL(line);
6982 fin = NEW_LABEL(line);
6984 ADD_INSN(ret, line_node, dup);
6985 CHECK(iseq_compile_pattern_each(iseq, ret, node->nd_1st, match_succeeded, fin, in_single_pattern,
true, base_index + 1 , use_deconstructed_cache));
6986 ADD_LABEL(ret, match_succeeded);
6987 ADD_INSN(ret, line_node, pop);
6988 ADD_INSNL(ret, line_node, jump, matched);
6989 ADD_INSN(ret, line_node, putnil);
6990 ADD_LABEL(ret, fin);
6991 CHECK(iseq_compile_pattern_each(iseq, ret, node->nd_2nd, matched, unmatched, in_single_pattern,
true, base_index, use_deconstructed_cache));
6995 UNKNOWN_NODE(
"NODE_IN", node, COMPILE_NG);
7001iseq_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)
7003 LABEL *fin = NEW_LABEL(nd_line(node));
7004 CHECK(iseq_compile_pattern_each(iseq, ret, node, fin, unmatched, in_single_pattern, in_alt_pattern, base_index, use_deconstructed_cache));
7005 ADD_LABEL(ret, fin);
7010iseq_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)
7012 const NODE *line_node = node;
7014 if (node->nd_pconst) {
7015 ADD_INSN(ret, line_node, dup);
7016 CHECK(COMPILE(ret,
"constant", node->nd_pconst));
7017 if (in_single_pattern) {
7018 ADD_INSN1(ret, line_node, dupn,
INT2FIX(2));
7020 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
7021 if (in_single_pattern) {
7022 CHECK(iseq_compile_pattern_set_eqq_errmsg(iseq, ret, node, base_index + 3 ));
7024 ADD_INSNL(ret, line_node, branchunless, match_failed);
7031iseq_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)
7033 const NODE *line_node = node;
7037 if (use_deconstructed_cache) {
7039 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7040 ADD_INSNL(ret, line_node, branchnil, deconstruct);
7043 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7044 ADD_INSNL(ret, line_node, branchunless, match_failed);
7047 ADD_INSN(ret, line_node, pop);
7048 ADD_INSN1(ret, line_node, topn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE - 1 ));
7049 ADD_INSNL(ret, line_node, jump, deconstructed);
7052 ADD_INSNL(ret, line_node, jump, deconstruct);
7055 ADD_LABEL(ret, deconstruct);
7056 ADD_INSN(ret, line_node, dup);
7057 ADD_INSN1(ret, line_node, putobject,
ID2SYM(
rb_intern(
"deconstruct")));
7058 ADD_SEND(ret, line_node, idRespond_to,
INT2FIX(1));
7061 if (use_deconstructed_cache) {
7062 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE + 1 ));
7065 if (in_single_pattern) {
7066 CHECK(iseq_compile_pattern_set_general_errmsg(iseq, ret, node, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1 ));
7069 ADD_INSNL(ret, line_node, branchunless, match_failed);
7074 if (use_deconstructed_cache) {
7075 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_DECONSTRUCTED_CACHE));
7078 ADD_INSN(ret, line_node, dup);
7080 ADD_INSNL(ret, line_node, branchunless, type_error);
7082 ADD_LABEL(ret, deconstructed);
7098 const int line = nd_line(node);
7099 const NODE *line_node = node;
7100 LABEL *match_succeeded = NEW_LABEL(line);
7102 ADD_INSN(ret, line_node, dup);
7103 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7105 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7106 ADD_INSN1(ret, line_node, putobject, errmsg);
7107 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7108 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(2));
7109 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7111 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7112 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7114 ADD_INSN(ret, line_node, pop);
7115 ADD_INSN(ret, line_node, pop);
7116 ADD_LABEL(ret, match_succeeded);
7132 const int line = nd_line(node);
7133 const NODE *line_node = node;
7134 LABEL *match_succeeded = NEW_LABEL(line);
7136 ADD_INSN(ret, line_node, dup);
7137 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7139 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7140 ADD_INSN1(ret, line_node, putobject, errmsg);
7141 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7142 ADD_INSN(ret, line_node, dup);
7143 ADD_SEND(ret, line_node, idLength,
INT2FIX(0));
7144 ADD_INSN1(ret, line_node, putobject, pattern_length);
7145 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(4));
7146 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7148 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7149 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2));
7151 ADD_INSN(ret, line_node, pop);
7152 ADD_INSN(ret, line_node, pop);
7153 ADD_LABEL(ret, match_succeeded);
7159iseq_compile_pattern_set_eqq_errmsg(
rb_iseq_t *iseq,
LINK_ANCHOR *
const ret,
const NODE *
const node,
int base_index)
7169 const int line = nd_line(node);
7170 const NODE *line_node = node;
7171 LABEL *match_succeeded = NEW_LABEL(line);
7173 ADD_INSN(ret, line_node, dup);
7174 ADD_INSNL(ret, line_node, branchif, match_succeeded);
7176 ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7177 ADD_INSN1(ret, line_node, putobject, rb_fstring_lit(
"%p === %p does not return true"));
7178 ADD_INSN1(ret, line_node, topn,
INT2FIX(3));
7179 ADD_INSN1(ret, line_node, topn,
INT2FIX(5));
7180 ADD_SEND(ret, line_node, id_core_sprintf,
INT2FIX(3));
7181 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_ERROR_STRING + 1 ));
7183 ADD_INSN1(ret, line_node, putobject,
Qfalse);
7184 ADD_INSN1(ret, line_node, setn,
INT2FIX(base_index + CASE3_BI_OFFSET_KEY_ERROR_P + 2 ));
7186 ADD_INSN(ret, line_node, pop);
7187 ADD_INSN(ret, line_node, pop);
7189 ADD_LABEL(ret, match_succeeded);
7190 ADD_INSN1(ret, line_node, setn,
INT2FIX(2));
7191 ADD_INSN(ret, line_node, pop);
7192 ADD_INSN(ret, line_node, pop);
7200 const NODE *pattern;
7201 const NODE *node = orig_node;
7202 LABEL *endlabel, *elselabel;
7204 DECL_ANCHOR(body_seq);
7205 DECL_ANCHOR(cond_seq);
7207 enum node_type
type;
7208 const NODE *line_node;
7211 bool single_pattern;
7214 INIT_ANCHOR(body_seq);
7215 INIT_ANCHOR(cond_seq);
7217 branches = decl_branch_base(iseq, node,
"case");
7219 node = node->nd_body;
7220 EXPECT_NODE(
"NODE_CASE3", node, NODE_IN, COMPILE_NG);
7221 type = nd_type(node);
7222 line = nd_line(node);
7224 single_pattern = !node->nd_next;
7226 endlabel = NEW_LABEL(line);
7227 elselabel = NEW_LABEL(line);
7229 if (single_pattern) {
7231 ADD_INSN(head, line_node, putnil);
7232 ADD_INSN(head, line_node, putnil);
7233 ADD_INSN1(head, line_node, putobject,
Qfalse);
7234 ADD_INSN(head, line_node, putnil);
7236 ADD_INSN(head, line_node, putnil);
7238 CHECK(COMPILE(head,
"case base", orig_node->nd_head));
7242 while (
type == NODE_IN) {
7246 ADD_INSN(body_seq, line_node, putnil);
7248 l1 = NEW_LABEL(line);
7249 ADD_LABEL(body_seq, l1);
7250 ADD_INSN1(body_seq, line_node, adjuststack,
INT2FIX(single_pattern ? 6 : 2));
7251 add_trace_branch_coverage(
7254 node->nd_body ? node->nd_body : node,
7258 CHECK(COMPILE_(body_seq,
"in body", node->nd_body, popped));
7259 ADD_INSNL(body_seq, line_node, jump, endlabel);
7261 pattern = node->nd_head;
7263 int pat_line = nd_line(pattern);
7264 LABEL *next_pat = NEW_LABEL(pat_line);
7265 ADD_INSN (cond_seq, pattern, dup);
7267 CHECK(iseq_compile_pattern_each(iseq, cond_seq, pattern, l1, next_pat, single_pattern,
false, 2,
true));
7268 ADD_LABEL(cond_seq, next_pat);
7269 LABEL_UNREMOVABLE(next_pat);
7272 COMPILE_ERROR(ERROR_ARGS
"unexpected node");
7276 node = node->nd_next;
7280 type = nd_type(node);
7281 line = nd_line(node);
7286 ADD_LABEL(cond_seq, elselabel);
7287 ADD_INSN(cond_seq, line_node, pop);
7288 ADD_INSN(cond_seq, line_node, pop);
7289 add_trace_branch_coverage(iseq, cond_seq, node, branch_id,
"else", branches);
7290 CHECK(COMPILE_(cond_seq,
"else", node, popped));
7291 ADD_INSNL(cond_seq, line_node, jump, endlabel);
7292 ADD_INSN(cond_seq, line_node, putnil);
7294 ADD_INSN(cond_seq, line_node, putnil);
7298 debugs(
"== else (implicit)\n");
7299 ADD_LABEL(cond_seq, elselabel);
7300 add_trace_branch_coverage(iseq, cond_seq, orig_node, branch_id,
"else", branches);
7301 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7303 if (single_pattern) {
7311 LABEL *key_error, *fin;
7314 key_error = NEW_LABEL(line);
7315 fin = NEW_LABEL(line);
7318 kw_arg->keyword_len = 2;
7322 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_P + 2));
7323 ADD_INSNL(cond_seq, orig_node, branchif, key_error);
7325 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7326 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
7327 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
7328 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
7329 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
7330 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
7331 ADD_INSNL(cond_seq, orig_node, jump, fin);
7333 ADD_LABEL(cond_seq, key_error);
7335 ADD_INSN1(cond_seq, orig_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7336 ADD_INSN1(cond_seq, orig_node, putobject, rb_fstring_lit(
"%p: %s"));
7337 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(4));
7338 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_ERROR_STRING + 6));
7339 ADD_SEND(cond_seq, orig_node, id_core_sprintf,
INT2FIX(3));
7340 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_MATCHEE + 4));
7341 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(CASE3_BI_OFFSET_KEY_ERROR_KEY + 5));
7343 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(1));
7345 ADD_LABEL(cond_seq, fin);
7349 ADD_INSN1(cond_seq, orig_node, topn,
INT2FIX(2));
7350 ADD_SEND(cond_seq, orig_node, id_core_raise,
INT2FIX(2));
7352 ADD_INSN1(cond_seq, orig_node, adjuststack,
INT2FIX(single_pattern ? 7 : 3));
7354 ADD_INSN(cond_seq, orig_node, putnil);
7356 ADD_INSNL(cond_seq, orig_node, jump, endlabel);
7357 ADD_INSN1(cond_seq, orig_node, dupn,
INT2FIX(single_pattern ? 5 : 1));
7359 ADD_INSN(cond_seq, line_node, putnil);
7363 ADD_SEQ(ret, cond_seq);
7364 ADD_SEQ(ret, body_seq);
7365 ADD_LABEL(ret, endlabel);
7369#undef CASE3_BI_OFFSET_DECONSTRUCTED_CACHE
7370#undef CASE3_BI_OFFSET_ERROR_STRING
7371#undef CASE3_BI_OFFSET_KEY_ERROR_P
7372#undef CASE3_BI_OFFSET_KEY_ERROR_MATCHEE
7373#undef CASE3_BI_OFFSET_KEY_ERROR_KEY
7378 const int line = (int)nd_line(node);
7379 const NODE *line_node = node;
7381 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
7382 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
7383 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
7384 int prev_loopval_popped = ISEQ_COMPILE_DATA(iseq)->loopval_popped;
7389 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(line);
7390 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(line);
7391 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(line);
7392 LABEL *end_label = NEW_LABEL(line);
7393 LABEL *adjust_label = NEW_LABEL(line);
7395 LABEL *next_catch_label = NEW_LABEL(line);
7396 LABEL *tmp_label = NULL;
7398 ISEQ_COMPILE_DATA(iseq)->loopval_popped = 0;
7399 push_ensure_entry(iseq, &enl, NULL, NULL);
7401 if (node->nd_state == 1) {
7402 ADD_INSNL(ret, line_node, jump, next_label);
7405 tmp_label = NEW_LABEL(line);
7406 ADD_INSNL(ret, line_node, jump, tmp_label);
7408 ADD_LABEL(ret, adjust_label);
7409 ADD_INSN(ret, line_node, putnil);
7410 ADD_LABEL(ret, next_catch_label);
7411 ADD_INSN(ret, line_node, pop);
7412 ADD_INSNL(ret, line_node, jump, next_label);
7413 if (tmp_label) ADD_LABEL(ret, tmp_label);
7415 ADD_LABEL(ret, redo_label);
7416 branches = decl_branch_base(iseq, node,
type == NODE_WHILE ?
"while" :
"until");
7417 add_trace_branch_coverage(
7420 node->nd_body ? node->nd_body : node,
7424 CHECK(COMPILE_POPPED(ret,
"while body", node->nd_body));
7425 ADD_LABEL(ret, next_label);
7427 if (
type == NODE_WHILE) {
7428 compile_branch_condition(iseq, ret, node->nd_cond,
7429 redo_label, end_label);
7433 compile_branch_condition(iseq, ret, node->nd_cond,
7434 end_label, redo_label);
7437 ADD_LABEL(ret, end_label);
7438 ADD_ADJUST_RESTORE(ret, adjust_label);
7440 if (UNDEF_P(node->nd_state)) {
7442 COMPILE_ERROR(ERROR_ARGS
"unsupported: putundef");
7446 ADD_INSN(ret, line_node, putnil);
7449 ADD_LABEL(ret, break_label);
7452 ADD_INSN(ret, line_node, pop);
7455 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
7457 ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
7459 ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
7460 ISEQ_COMPILE_DATA(iseq)->redo_label);
7462 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
7463 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
7464 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
7465 ISEQ_COMPILE_DATA(iseq)->loopval_popped = prev_loopval_popped;
7466 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
7473 const int line = nd_line(node);
7474 const NODE *line_node = node;
7475 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
7476 LABEL *retry_label = NEW_LABEL(line);
7477 LABEL *retry_end_l = NEW_LABEL(line);
7480 ADD_LABEL(ret, retry_label);
7481 if (nd_type_p(node, NODE_FOR)) {
7482 CHECK(COMPILE(ret,
"iter caller (for)", node->nd_iter));
7484 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
7485 NEW_CHILD_ISEQ(node->nd_body, make_name_for_block(iseq),
7486 ISEQ_TYPE_BLOCK, line);
7487 ADD_SEND_WITH_BLOCK(ret, line_node, idEach,
INT2FIX(0), child_iseq);
7490 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq =
7491 NEW_CHILD_ISEQ(node->nd_body, make_name_for_block(iseq),
7492 ISEQ_TYPE_BLOCK, line);
7493 CHECK(COMPILE(ret,
"iter caller", node->nd_iter));
7507 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
7508 while (INSN_OF(iobj) != BIN(send) && INSN_OF(iobj) != BIN(invokesuper)) {
7509 iobj = (
INSN*) get_prev_insn(iobj);
7511 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
7515 if (&iobj->link == LAST_ELEMENT(ret)) {
7521 ADD_INSN(ret, line_node, pop);
7524 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
7526 ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
7536 const NODE *line_node = node;
7537 const NODE *var = node->nd_var;
7538 LABEL *not_single = NEW_LABEL(nd_line(var));
7539 LABEL *not_ary = NEW_LABEL(nd_line(var));
7540 CHECK(COMPILE(ret,
"for var", var));
7541 ADD_INSN(ret, line_node, dup);
7542 ADD_CALL(ret, line_node, idLength,
INT2FIX(0));
7543 ADD_INSN1(ret, line_node, putobject,
INT2FIX(1));
7544 ADD_CALL(ret, line_node, idEq,
INT2FIX(1));
7545 ADD_INSNL(ret, line_node, branchunless, not_single);
7546 ADD_INSN(ret, line_node, dup);
7547 ADD_INSN1(ret, line_node, putobject,
INT2FIX(0));
7548 ADD_CALL(ret, line_node, idAREF,
INT2FIX(1));
7549 ADD_INSN1(ret, line_node, putobject,
rb_cArray);
7550 ADD_INSN(ret, line_node, swap);
7552 ADD_INSN(ret, line_node, dup);
7553 ADD_INSNL(ret, line_node, branchunless, not_ary);
7554 ADD_INSN(ret, line_node, swap);
7555 ADD_LABEL(ret, not_ary);
7556 ADD_INSN(ret, line_node, pop);
7557 ADD_LABEL(ret, not_single);
7564 const NODE *line_node = node;
7565 unsigned long throw_flag = 0;
7567 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7569 LABEL *splabel = NEW_LABEL(0);
7570 ADD_LABEL(ret, splabel);
7571 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
7572 CHECK(COMPILE_(ret,
"break val (while/until)", node->nd_stts,
7573 ISEQ_COMPILE_DATA(iseq)->loopval_popped));
7574 add_ensure_iseq(ret, iseq, 0);
7575 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7576 ADD_ADJUST_RESTORE(ret, splabel);
7579 ADD_INSN(ret, line_node, putnil);
7586 if (!ISEQ_COMPILE_DATA(ip)) {
7591 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7592 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7594 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7597 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7598 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with break");
7602 ip = ISEQ_BODY(ip)->parent_iseq;
7607 CHECK(COMPILE(ret,
"break val (block)", node->nd_stts));
7608 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_BREAK));
7610 ADD_INSN(ret, line_node, pop);
7614 COMPILE_ERROR(ERROR_ARGS
"Invalid break");
7623 const NODE *line_node = node;
7624 unsigned long throw_flag = 0;
7626 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7627 LABEL *splabel = NEW_LABEL(0);
7628 debugs(
"next in while loop\n");
7629 ADD_LABEL(ret, splabel);
7630 CHECK(COMPILE(ret,
"next val/valid syntax?", node->nd_stts));
7631 add_ensure_iseq(ret, iseq, 0);
7632 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
7633 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7634 ADD_ADJUST_RESTORE(ret, splabel);
7636 ADD_INSN(ret, line_node, putnil);
7639 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
7640 LABEL *splabel = NEW_LABEL(0);
7641 debugs(
"next in block\n");
7642 ADD_LABEL(ret, splabel);
7643 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
7644 CHECK(COMPILE(ret,
"next val", node->nd_stts));
7645 add_ensure_iseq(ret, iseq, 0);
7646 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7647 ADD_ADJUST_RESTORE(ret, splabel);
7648 splabel->unremovable = FALSE;
7651 ADD_INSN(ret, line_node, putnil);
7658 if (!ISEQ_COMPILE_DATA(ip)) {
7663 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7664 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7668 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7671 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7672 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with next");
7676 ip = ISEQ_BODY(ip)->parent_iseq;
7679 CHECK(COMPILE(ret,
"next val", node->nd_stts));
7680 ADD_INSN1(ret, line_node,
throw,
INT2FIX(throw_flag | TAG_NEXT));
7683 ADD_INSN(ret, line_node, pop);
7687 COMPILE_ERROR(ERROR_ARGS
"Invalid next");
7697 const NODE *line_node = node;
7699 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
7700 LABEL *splabel = NEW_LABEL(0);
7701 debugs(
"redo in while");
7702 ADD_LABEL(ret, splabel);
7703 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
7704 add_ensure_iseq(ret, iseq, 0);
7705 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
7706 ADD_ADJUST_RESTORE(ret, splabel);
7708 ADD_INSN(ret, line_node, putnil);
7711 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
7712 LABEL *splabel = NEW_LABEL(0);
7714 debugs(
"redo in block");
7715 ADD_LABEL(ret, splabel);
7716 add_ensure_iseq(ret, iseq, 0);
7717 ADD_ADJUST(ret, line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
7718 ADD_INSNL(ret, line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7719 ADD_ADJUST_RESTORE(ret, splabel);
7722 ADD_INSN(ret, line_node, putnil);
7729 if (!ISEQ_COMPILE_DATA(ip)) {
7734 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7737 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7740 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7741 COMPILE_ERROR(ERROR_ARGS
"Can't escape from eval with redo");
7745 ip = ISEQ_BODY(ip)->parent_iseq;
7748 ADD_INSN(ret, line_node, putnil);
7749 ADD_INSN1(ret, line_node,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
7752 ADD_INSN(ret, line_node, pop);
7756 COMPILE_ERROR(ERROR_ARGS
"Invalid redo");
7766 const NODE *line_node = node;
7768 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
7769 ADD_INSN(ret, line_node, putnil);
7770 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETRY));
7773 ADD_INSN(ret, line_node, pop);
7777 COMPILE_ERROR(ERROR_ARGS
"Invalid retry");
7786 const int line = nd_line(node);
7787 const NODE *line_node = node;
7788 LABEL *lstart = NEW_LABEL(line);
7789 LABEL *lend = NEW_LABEL(line);
7790 LABEL *lcont = NEW_LABEL(line);
7791 const rb_iseq_t *rescue = NEW_CHILD_ISEQ(node->nd_resq,
7793 ISEQ_BODY(iseq)->location.label),
7794 ISEQ_TYPE_RESCUE, line);
7796 lstart->rescued = LABEL_RESCUE_BEG;
7797 lend->rescued = LABEL_RESCUE_END;
7798 ADD_LABEL(ret, lstart);
7800 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
7801 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
7803 CHECK(COMPILE(ret,
"rescue head", node->nd_head));
7805 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
7807 ADD_LABEL(ret, lend);
7808 if (node->nd_else) {
7809 ADD_INSN(ret, line_node, pop);
7810 CHECK(COMPILE(ret,
"rescue else", node->nd_else));
7812 ADD_INSN(ret, line_node, nop);
7813 ADD_LABEL(ret, lcont);
7816 ADD_INSN(ret, line_node, pop);
7820 ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lcont);
7821 ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
7828 const int line = nd_line(node);
7829 const NODE *line_node = node;
7830 const NODE *resq = node;
7832 LABEL *label_miss, *label_hit;
7835 label_miss = NEW_LABEL(line);
7836 label_hit = NEW_LABEL(line);
7838 narg = resq->nd_args;
7840 switch (nd_type(narg)) {
7843 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
7844 CHECK(COMPILE(ret,
"rescue arg", narg->nd_head));
7845 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7846 ADD_INSNL(ret, line_node, branchif, label_hit);
7847 narg = narg->nd_next;
7853 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
7854 CHECK(COMPILE(ret,
"rescue/cond splat", narg));
7855 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE | VM_CHECKMATCH_ARRAY));
7856 ADD_INSNL(ret, line_node, branchif, label_hit);
7859 UNKNOWN_NODE(
"NODE_RESBODY", narg, COMPILE_NG);
7863 ADD_GETLOCAL(ret, line_node, LVAR_ERRINFO, 0);
7865 ADD_INSN1(ret, line_node, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7866 ADD_INSNL(ret, line_node, branchif, label_hit);
7868 ADD_INSNL(ret, line_node, jump, label_miss);
7869 ADD_LABEL(ret, label_hit);
7870 CHECK(COMPILE(ret,
"resbody body", resq->nd_body));
7871 if (ISEQ_COMPILE_DATA(iseq)->option->tailcall_optimization) {
7872 ADD_INSN(ret, line_node, nop);
7874 ADD_INSN(ret, line_node, leave);
7875 ADD_LABEL(ret, label_miss);
7876 resq = resq->nd_head;
7884 const int line = nd_line(node);
7885 const NODE *line_node = node;
7887 const rb_iseq_t *ensure = NEW_CHILD_ISEQ(node->nd_ensr,
7889 ISEQ_TYPE_ENSURE, line);
7890 LABEL *lstart = NEW_LABEL(line);
7891 LABEL *lend = NEW_LABEL(line);
7892 LABEL *lcont = NEW_LABEL(line);
7900 CHECK(COMPILE_POPPED(ensr,
"ensure ensr", node->nd_ensr));
7902 last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
7907 push_ensure_entry(iseq, &enl, &er, node->nd_ensr);
7909 ADD_LABEL(ret, lstart);
7910 CHECK(COMPILE_(ret,
"ensure head", node->nd_head, (popped | last_leave)));
7911 ADD_LABEL(ret, lend);
7913 if (!popped && last_leave) ADD_INSN(ret, line_node, putnil);
7914 ADD_LABEL(ret, lcont);
7915 if (last_leave) ADD_INSN(ret, line_node, pop);
7917 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
7918 if (lstart->link.next != &lend->link) {
7920 ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end,
7922 erange = erange->next;
7926 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
7933 const NODE *line_node = node;
7936 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
7938 enum rb_iseq_type t =
type;
7939 const NODE *retval = node->nd_stts;
7942 while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
7943 if (!(is = ISEQ_BODY(is)->parent_iseq))
break;
7944 t = ISEQ_BODY(is)->type;
7948 case ISEQ_TYPE_MAIN:
7950 rb_warn(
"argument of top-level return is ignored");
7954 type = ISEQ_TYPE_METHOD;
7961 if (
type == ISEQ_TYPE_METHOD) {
7962 splabel = NEW_LABEL(0);
7963 ADD_LABEL(ret, splabel);
7964 ADD_ADJUST(ret, line_node, 0);
7967 CHECK(COMPILE(ret,
"return nd_stts (return val)", retval));
7969 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
7970 add_ensure_iseq(ret, iseq, 1);
7972 ADD_INSN(ret, line_node, leave);
7973 ADD_ADJUST_RESTORE(ret, splabel);
7976 ADD_INSN(ret, line_node, putnil);
7980 ADD_INSN1(ret, line_node,
throw,
INT2FIX(TAG_RETURN));
7982 ADD_INSN(ret, line_node, pop);
7992 CHECK(COMPILE_(ret,
"nd_body", node, popped));
7994 if (!popped && !all_string_result_p(node)) {
7995 const NODE *line_node = node;
7996 const unsigned int flag = VM_CALL_FCALL;
8000 ADD_INSN(ret, line_node, dup);
8001 ADD_INSN1(ret, line_node, objtostring, new_callinfo(iseq, idTo_s, 0, flag, NULL, FALSE));
8002 ADD_INSN(ret, line_node, anytostring);
8010 int idx = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
8012 debugs(
"id: %s idx: %d\n",
rb_id2name(
id), idx);
8013 ADD_GETLOCAL(ret, line_node, idx, get_lvar_level(iseq));
8019 LABEL *else_label = NEW_LABEL(nd_line(line_node));
8022 br = decl_branch_base(iseq, node,
"&.");
8024 ADD_INSN(recv, line_node, dup);
8025 ADD_INSNL(recv, line_node, branchnil, else_label);
8026 add_trace_branch_coverage(iseq, recv, node, 0,
"then", br);
8034 if (!else_label)
return;
8035 end_label = NEW_LABEL(nd_line(line_node));
8036 ADD_INSNL(ret, line_node, jump, end_label);
8037 ADD_LABEL(ret, else_label);
8038 add_trace_branch_coverage(iseq, ret, node, 1,
"else", branches);
8039 ADD_LABEL(ret, end_label);
8048 if (node->nd_recv && nd_type_p(node->nd_recv, NODE_STR) &&
8049 (node->nd_mid == idFreeze || node->nd_mid == idUMinus) &&
8050 node->nd_args == NULL &&
8051 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8052 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8053 VALUE str = rb_fstring(node->nd_recv->nd_lit);
8054 if (node->nd_mid == idUMinus) {
8055 ADD_INSN2(ret, line_node, opt_str_uminus, str,
8056 new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
8059 ADD_INSN2(ret, line_node, opt_str_freeze, str,
8060 new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
8064 ADD_INSN(ret, line_node, pop);
8071 if (node->nd_mid == idAREF && !private_recv_p(node) && node->nd_args &&
8072 nd_type_p(node->nd_args, NODE_LIST) && node->nd_args->nd_alen == 1 &&
8073 nd_type_p(node->nd_args->nd_head, NODE_STR) &&
8074 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
8075 !ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
8076 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
8077 VALUE str = rb_fstring(node->nd_args->nd_head->nd_lit);
8078 CHECK(COMPILE(ret,
"recv", node->nd_recv));
8079 ADD_INSN2(ret, line_node, opt_aref_with, str,
8080 new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
8083 ADD_INSN(ret, line_node, pop);
8091iseq_has_builtin_function_table(
const rb_iseq_t *iseq)
8093 return ISEQ_COMPILE_DATA(iseq)->builtin_function_table != NULL;
8097iseq_builtin_function_lookup(
const rb_iseq_t *iseq,
const char *name)
8100 const struct rb_builtin_function *table = ISEQ_COMPILE_DATA(iseq)->builtin_function_table;
8101 for (i=0; table[i].index != -1; i++) {
8102 if (strcmp(table[i].name, name) == 0) {
8110iseq_builtin_function_name(
const enum node_type
type,
const NODE *recv,
ID mid)
8113 static const char prefix[] =
"__builtin_";
8114 const size_t prefix_len =
sizeof(prefix) - 1;
8119 switch (nd_type(recv)) {
8121 if (recv->nd_mid ==
rb_intern(
"__builtin")) {
8126 if (recv->nd_vid ==
rb_intern(
"Primitive")) {
8136 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
8137 return &name[prefix_len];
8146delegate_call_p(
const rb_iseq_t *iseq,
unsigned int argc,
const LINK_ANCHOR *args,
unsigned int *pstart_index)
8153 else if (argc <= ISEQ_BODY(iseq)->local_table_size) {
8154 unsigned int start=0;
8159 argc + start <= ISEQ_BODY(iseq)->local_table_size;
8163 for (
unsigned int i=start; i-start<argc; i++) {
8164 if (IS_INSN(elem) &&
8165 INSN_OF(elem) == BIN(getlocal)) {
8166 int local_index =
FIX2INT(OPERAND_AT(elem, 0));
8167 int local_level =
FIX2INT(OPERAND_AT(elem, 1));
8169 if (local_level == 0) {
8170 unsigned int index = ISEQ_BODY(iseq)->local_table_size - (local_index - VM_ENV_DATA_SIZE + 1);
8172 fprintf(stderr,
"lvar:%s (%d), id:%s (%d) local_index:%d, local_size:%d\n",
8173 rb_id2name(ISEQ_BODY(iseq)->local_table[i]), i,
8174 rb_id2name(ISEQ_BODY(iseq)->local_table[index]), index,
8175 local_index, (
int)ISEQ_BODY(iseq)->local_table_size);
8199 *pstart_index = start;
8210 if (!node)
goto no_arg;
8211 if (!nd_type_p(node, NODE_LIST))
goto bad_arg;
8212 if (node->nd_next)
goto too_many_arg;
8213 node = node->nd_head;
8214 if (!node)
goto no_arg;
8215 if (!nd_type_p(node, NODE_LIT))
goto bad_arg;
8216 VALUE name = node->nd_lit;
8217 if (!
SYMBOL_P(name))
goto non_symbol_arg;
8219 compile_lvar(iseq, ret, line_node,
SYM2ID(name));
8223 COMPILE_ERROR(ERROR_ARGS
"arg!: no argument");
8226 COMPILE_ERROR(ERROR_ARGS
"arg!: too many argument");
8229 COMPILE_ERROR(ERROR_ARGS
"non symbol argument to arg!: %s",
8230 rb_builtin_class_name(name));
8233 UNKNOWN_NODE(
"arg!", node, COMPILE_NG);
8239 const NODE *node = ISEQ_COMPILE_DATA(iseq)->root_node;
8240 if (nd_type(node) == NODE_IF && node->nd_cond == cond_node) {
8241 return node->nd_body;
8244 rb_bug(
"mandatory_node: can't find mandatory node");
8249compile_builtin_mandatory_only_method(
rb_iseq_t *iseq,
const NODE *node,
const NODE *line_node)
8253 .pre_args_num = ISEQ_BODY(iseq)->param.lead_num,
8256 rb_node_init(&args_node, NODE_ARGS, 0, 0, (
VALUE)&args);
8259 const int skip_local_size = ISEQ_BODY(iseq)->param.size - ISEQ_BODY(iseq)->param.lead_num;
8260 const int table_size = ISEQ_BODY(iseq)->local_table_size - skip_local_size;
8264 tbl->size = table_size;
8269 for (i=0; i<ISEQ_BODY(iseq)->param.lead_num; i++) {
8270 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i];
8273 for (; i<table_size; i++) {
8274 tbl->ids[i] = ISEQ_BODY(iseq)->local_table[i + skip_local_size];
8278 rb_node_init(&scope_node, NODE_SCOPE, (
VALUE)tbl, (
VALUE)mandatory_node(iseq, node), (
VALUE)&args_node);
8281 .root = &scope_node,
8282 .compile_option = 0,
8283 .script_lines = ISEQ_BODY(iseq)->variable.script_lines,
8286 int prev_inline_index = GET_VM()->builtin_inline_index;
8288 ISEQ_BODY(iseq)->mandatory_only_iseq =
8289 rb_iseq_new_with_opt(&ast, rb_iseq_base_label(iseq),
8290 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
8291 nd_line(line_node), NULL, 0,
8292 ISEQ_TYPE_METHOD, ISEQ_COMPILE_DATA(iseq)->option);
8294 GET_VM()->builtin_inline_index = prev_inline_index;
8303 NODE *args_node = node->nd_args;
8305 if (parent_block != NULL) {
8306 COMPILE_ERROR(iseq, nd_line(line_node),
"should not call builtins here.");
8310# define BUILTIN_INLINE_PREFIX "_bi"
8311 char inline_func[
DECIMAL_SIZE_OF_BITS(
sizeof(
int) * CHAR_BIT) +
sizeof(BUILTIN_INLINE_PREFIX)];
8312 bool cconst =
false;
8317 if (strcmp(
"cstmt!", builtin_func) == 0 ||
8318 strcmp(
"cexpr!", builtin_func) == 0) {
8321 else if (strcmp(
"cconst!", builtin_func) == 0) {
8324 else if (strcmp(
"cinit!", builtin_func) == 0) {
8326 GET_VM()->builtin_inline_index++;
8329 else if (strcmp(
"attr!", builtin_func) == 0) {
8331 ISEQ_BODY(iseq)->builtin_inline_p =
true;
8334 else if (strcmp(
"arg!", builtin_func) == 0) {
8335 return compile_builtin_arg(iseq, ret, args_node, line_node, popped);
8337 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
8339 rb_bug(
"mandatory_only? should be in if condition");
8341 else if (!LIST_INSN_SIZE_ZERO(ret)) {
8342 rb_bug(
"mandatory_only? should be put on top");
8345 ADD_INSN1(ret, line_node, putobject,
Qfalse);
8346 return compile_builtin_mandatory_only_method(iseq, node, line_node);
8349 rb_bug(
"can't find builtin function:%s", builtin_func);
8352 COMPILE_ERROR(ERROR_ARGS
"can't find builtin function:%s", builtin_func);
8356 if (GET_VM()->builtin_inline_index == INT_MAX) {
8357 rb_bug(
"builtin inline function index overflow:%s", builtin_func);
8359 int inline_index = GET_VM()->builtin_inline_index++;
8360 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
8361 builtin_func = inline_func;
8367 typedef VALUE(*builtin_func0)(
void *,
VALUE);
8368 VALUE const_val = (*(builtin_func0)bf->func_ptr)(NULL,
Qnil);
8369 ADD_INSN1(ret, line_node, putobject, const_val);
8375 unsigned int flag = 0;
8377 VALUE argc = setup_args(iseq, args, args_node, &flag, &keywords);
8379 if (
FIX2INT(argc) != bf->argc) {
8380 COMPILE_ERROR(ERROR_ARGS
"argc is not match for builtin function:%s (expect %d but %d)",
8381 builtin_func, bf->argc,
FIX2INT(argc));
8385 unsigned int start_index;
8386 if (delegate_call_p(iseq,
FIX2INT(argc), args, &start_index)) {
8387 ADD_INSN2(ret, line_node, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
8391 ADD_INSN1(ret, line_node, invokebuiltin, bf);
8394 if (popped) ADD_INSN(ret, line_node, pop);
8400compile_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)
8408 ID mid = node->nd_mid;
8410 unsigned int flag = 0;
8412 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8413 LABEL *else_label = NULL;
8416 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
8421 if (nd_type_p(node, NODE_VCALL)) {
8426 CONST_ID(id_answer,
"the_answer_to_life_the_universe_and_everything");
8428 if (mid == id_bitblt) {
8429 ADD_INSN(ret, line_node, bitblt);
8432 else if (mid == id_answer) {
8433 ADD_INSN(ret, line_node, answer);
8445 if (nd_type_p(node, NODE_FCALL) &&
8446 (mid == goto_id || mid == label_id)) {
8449 st_table *labels_table = ISEQ_COMPILE_DATA(iseq)->labels_table;
8452 if (!labels_table) {
8453 labels_table = st_init_numtable();
8454 ISEQ_COMPILE_DATA(iseq)->labels_table = labels_table;
8456 if (nd_type_p(node->nd_args->nd_head, NODE_LIT) &&
8457 SYMBOL_P(node->nd_args->nd_head->nd_lit)) {
8459 label_name = node->nd_args->nd_head->nd_lit;
8460 if (!st_lookup(labels_table, (st_data_t)label_name, &data)) {
8461 label = NEW_LABEL(nd_line(line_node));
8462 label->position = nd_line(line_node);
8463 st_insert(labels_table, (st_data_t)label_name, (st_data_t)label);
8466 label = (
LABEL *)data;
8470 COMPILE_ERROR(ERROR_ARGS
"invalid goto/label format");
8474 if (mid == goto_id) {
8475 ADD_INSNL(ret, line_node, jump, label);
8478 ADD_LABEL(ret, label);
8485 const char *builtin_func;
8486 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) &&
8487 (builtin_func = iseq_builtin_function_name(
type, node->nd_recv, mid)) != NULL) {
8488 return compile_builtin_function_call(iseq, ret, node, line_node, popped, parent_block, args, builtin_func);
8492 if (!assume_receiver) {
8493 if (
type == NODE_CALL ||
type == NODE_OPCALL ||
type == NODE_QCALL) {
8496 if (mid == idCall &&
8497 nd_type_p(node->nd_recv, NODE_LVAR) &&
8498 iseq_block_param_id_p(iseq, node->nd_recv->nd_vid, &idx, &level)) {
8499 ADD_INSN2(recv, node->nd_recv, getblockparamproxy,
INT2FIX(idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(level));
8501 else if (private_recv_p(node)) {
8502 ADD_INSN(recv, node, putself);
8503 flag |= VM_CALL_FCALL;
8506 CHECK(COMPILE(recv,
"recv", node->nd_recv));
8509 if (
type == NODE_QCALL) {
8510 else_label = qcall_branch_start(iseq, recv, &branches, node, line_node);
8513 else if (
type == NODE_FCALL ||
type == NODE_VCALL) {
8514 ADD_CALL_RECEIVER(recv, line_node);
8519 if (
type != NODE_VCALL) {
8520 argc = setup_args(iseq, args, node->nd_args, &flag, &keywords);
8521 CHECK(!
NIL_P(argc));
8530 debugp_param(
"call args argc", argc);
8531 debugp_param(
"call method",
ID2SYM(mid));
8533 switch ((
int)
type) {
8535 flag |= VM_CALL_VCALL;
8538 flag |= VM_CALL_FCALL;
8541 ADD_SEND_R(ret, line_node, mid, argc, parent_block,
INT2FIX(flag), keywords);
8543 qcall_branch_end(iseq, ret, else_label, branches, node, line_node);
8545 ADD_INSN(ret, line_node, pop);
8553 const int line = nd_line(node);
8555 unsigned int flag = 0;
8557 ID id = node->nd_mid;
8584 ADD_INSN(ret, node, putnil);
8586 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN1 recv", node);
8587 CHECK(asgnflag != -1);
8588 switch (nd_type(node->nd_args->nd_head)) {
8592 case NODE_BLOCK_PASS:
8596 argc = setup_args(iseq, ret, node->nd_args->nd_head, &flag, NULL);
8597 CHECK(!
NIL_P(argc));
8599 ADD_INSN1(ret, node, dupn, FIXNUM_INC(argc, 1 + boff));
8601 ADD_SEND_WITH_FLAG(ret, node, idAREF, argc,
INT2FIX(flag));
8603 if (
id == idOROP ||
id == idANDOP) {
8612 LABEL *label = NEW_LABEL(line);
8613 LABEL *lfin = NEW_LABEL(line);
8615 ADD_INSN(ret, node, dup);
8617 ADD_INSNL(ret, node, branchif, label);
8620 ADD_INSNL(ret, node, branchunless, label);
8622 ADD_INSN(ret, node, pop);
8624 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 args->body: ", node->nd_args->nd_body));
8626 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2+boff));
8628 if (flag & VM_CALL_ARGS_SPLAT) {
8629 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
8631 ADD_INSN1(ret, node, dupn,
INT2FIX(3));
8632 ADD_INSN(ret, node, swap);
8633 ADD_INSN(ret, node, pop);
8635 ADD_INSN(ret, node, concatarray);
8637 ADD_INSN1(ret, node, setn,
INT2FIX(3));
8638 ADD_INSN(ret, node, pop);
8639 ADD_INSN(ret, node, pop);
8641 ADD_SEND_WITH_FLAG(ret, node, idASET, argc,
INT2FIX(flag));
8645 ADD_INSN(ret, node, swap);
8646 ADD_SEND_WITH_FLAG(ret, node, idASET, FIXNUM_INC(argc, 1),
INT2FIX(flag));
8648 ADD_INSN(ret, node, pop);
8649 ADD_INSNL(ret, node, jump, lfin);
8650 ADD_LABEL(ret, label);
8652 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2+boff));
8654 ADD_INSN1(ret, node, adjuststack, FIXNUM_INC(argc, 2+boff));
8655 ADD_LABEL(ret, lfin);
8658 CHECK(COMPILE(ret,
"NODE_OP_ASGN1 args->body: ", node->nd_args->nd_body));
8659 ADD_SEND(ret, node,
id,
INT2FIX(1));
8661 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2+boff));
8663 if (flag & VM_CALL_ARGS_SPLAT) {
8664 ADD_INSN1(ret, node, newarray,
INT2FIX(1));
8666 ADD_INSN1(ret, node, dupn,
INT2FIX(3));
8667 ADD_INSN(ret, node, swap);
8668 ADD_INSN(ret, node, pop);
8670 ADD_INSN(ret, node, concatarray);
8672 ADD_INSN1(ret, node, setn,
INT2FIX(3));
8673 ADD_INSN(ret, node, pop);
8674 ADD_INSN(ret, node, pop);
8676 ADD_SEND_WITH_FLAG(ret, node, idASET, argc,
INT2FIX(flag));
8680 ADD_INSN(ret, node, swap);
8681 ADD_SEND_WITH_FLAG(ret, node, idASET, FIXNUM_INC(argc, 1),
INT2FIX(flag));
8683 ADD_INSN(ret, node, pop);
8691 const int line = nd_line(node);
8692 ID atype = node->nd_next->nd_mid;
8695 LABEL *lfin = NEW_LABEL(line);
8696 LABEL *lcfin = NEW_LABEL(line);
8751 asgnflag = COMPILE_RECV(ret,
"NODE_OP_ASGN2#recv", node);
8752 CHECK(asgnflag != -1);
8753 if (node->nd_next->nd_aid) {
8754 lskip = NEW_LABEL(line);
8755 ADD_INSN(ret, node, dup);
8756 ADD_INSNL(ret, node, branchnil, lskip);
8758 ADD_INSN(ret, node, dup);
8759 ADD_SEND_WITH_FLAG(ret, node, vid,
INT2FIX(0),
INT2FIX(asgnflag));
8761 if (atype == idOROP || atype == idANDOP) {
8763 ADD_INSN(ret, node, dup);
8765 if (atype == idOROP) {
8766 ADD_INSNL(ret, node, branchif, lcfin);
8769 ADD_INSNL(ret, node, branchunless, lcfin);
8772 ADD_INSN(ret, node, pop);
8774 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", node->nd_value));
8776 ADD_INSN(ret, node, swap);
8777 ADD_INSN1(ret, node, topn,
INT2FIX(1));
8779 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
8780 ADD_INSNL(ret, node, jump, lfin);
8782 ADD_LABEL(ret, lcfin);
8784 ADD_INSN(ret, node, swap);
8787 ADD_LABEL(ret, lfin);
8790 CHECK(COMPILE(ret,
"NODE_OP_ASGN2 val", node->nd_value));
8791 ADD_SEND(ret, node, atype,
INT2FIX(1));
8793 ADD_INSN(ret, node, swap);
8794 ADD_INSN1(ret, node, topn,
INT2FIX(1));
8796 ADD_SEND_WITH_FLAG(ret, node, aid,
INT2FIX(1),
INT2FIX(asgnflag));
8798 if (lskip && popped) {
8799 ADD_LABEL(ret, lskip);
8801 ADD_INSN(ret, node, pop);
8802 if (lskip && !popped) {
8803 ADD_LABEL(ret, lskip);
8811 const int line = nd_line(node);
8816 switch (nd_type(node->nd_head)) {
8818 ADD_INSN1(ret, node, putobject, rb_cObject);
8821 CHECK(COMPILE(ret,
"NODE_OP_CDECL/colon2#nd_head", node->nd_head->nd_head));
8824 COMPILE_ERROR(ERROR_ARGS
"%s: invalid node in NODE_OP_CDECL",
8825 ruby_node_name(nd_type(node->nd_head)));
8828 mid = node->nd_head->nd_mid;
8830 if (node->nd_aid == idOROP) {
8831 lassign = NEW_LABEL(line);
8832 ADD_INSN(ret, node, dup);
8833 ADD_INSN3(ret, node, defined,
INT2FIX(DEFINED_CONST_FROM),
8835 ADD_INSNL(ret, node, branchunless, lassign);
8837 ADD_INSN(ret, node, dup);
8838 ADD_INSN1(ret, node, putobject,
Qtrue);
8839 ADD_INSN1(ret, node, getconstant,
ID2SYM(mid));
8841 if (node->nd_aid == idOROP || node->nd_aid == idANDOP) {
8842 lfin = NEW_LABEL(line);
8843 if (!popped) ADD_INSN(ret, node, dup);
8844 if (node->nd_aid == idOROP)
8845 ADD_INSNL(ret, node, branchif, lfin);
8847 ADD_INSNL(ret, node, branchunless, lfin);
8849 if (!popped) ADD_INSN(ret, node, pop);
8850 if (lassign) ADD_LABEL(ret, lassign);
8851 CHECK(COMPILE(ret,
"NODE_OP_CDECL#nd_value", node->nd_value));
8854 ADD_INSN1(ret, node, topn,
INT2FIX(1));
8856 ADD_INSN1(ret, node, dupn,
INT2FIX(2));
8857 ADD_INSN(ret, node, swap);
8859 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
8860 ADD_LABEL(ret, lfin);
8861 if (!popped) ADD_INSN(ret, node, swap);
8862 ADD_INSN(ret, node, pop);
8865 CHECK(COMPILE(ret,
"NODE_OP_CDECL#nd_value", node->nd_value));
8867 ADD_CALL(ret, node, node->nd_aid,
INT2FIX(1));
8869 ADD_INSN(ret, node, swap);
8871 ADD_INSN1(ret, node, topn,
INT2FIX(1));
8872 ADD_INSN(ret, node, swap);
8874 ADD_INSN1(ret, node, setconstant,
ID2SYM(mid));
8882 const int line = nd_line(node);
8883 LABEL *lfin = NEW_LABEL(line);
8886 if (
type == NODE_OP_ASGN_OR && !nd_type_p(node->nd_head, NODE_IVAR)) {
8890 defined_expr(iseq, ret, node->nd_head, lfinish,
Qfalse);
8891 lassign = lfinish[1];
8893 lassign = NEW_LABEL(line);
8895 ADD_INSNL(ret, node, branchunless, lassign);
8898 lassign = NEW_LABEL(line);
8901 CHECK(COMPILE(ret,
"NODE_OP_ASGN_AND/OR#nd_head", node->nd_head));
8904 ADD_INSN(ret, node, dup);
8907 if (
type == NODE_OP_ASGN_AND) {
8908 ADD_INSNL(ret, node, branchunless, lfin);
8911 ADD_INSNL(ret, node, branchif, lfin);
8915 ADD_INSN(ret, node, pop);
8918 ADD_LABEL(ret, lassign);
8919 CHECK(COMPILE_(ret,
"NODE_OP_ASGN_AND/OR#nd_value", node->nd_value, popped));
8920 ADD_LABEL(ret, lfin);
8930 unsigned int flag = 0;
8932 const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8935 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
8936 if (
type == NODE_SUPER) {
8937 VALUE vargc = setup_args(iseq, args, node->nd_args, &flag, &keywords);
8938 CHECK(!
NIL_P(vargc));
8944 const rb_iseq_t *liseq = body->local_iseq;
8946 const struct rb_iseq_param_keyword *
const local_kwd = local_body->
param.keyword;
8947 int lvar_level = get_lvar_level(iseq);
8949 argc = local_body->
param.lead_num;
8952 for (i = 0; i < local_body->
param.lead_num; i++) {
8953 int idx = local_body->local_table_size - i;
8954 ADD_GETLOCAL(args, node, idx, lvar_level);
8957 if (local_body->
param.flags.has_opt) {
8960 for (j = 0; j < local_body->
param.opt_num; j++) {
8961 int idx = local_body->local_table_size - (i + j);
8962 ADD_GETLOCAL(args, node, idx, lvar_level);
8967 if (local_body->
param.flags.has_rest) {
8969 int idx = local_body->local_table_size - local_body->
param.rest_start;
8970 ADD_GETLOCAL(args, node, idx, lvar_level);
8971 ADD_INSN1(args, node, splatarray,
Qfalse);
8973 argc = local_body->
param.rest_start + 1;
8974 flag |= VM_CALL_ARGS_SPLAT;
8976 if (local_body->
param.flags.has_post) {
8978 int post_len = local_body->
param.post_num;
8979 int post_start = local_body->
param.post_start;
8981 if (local_body->
param.flags.has_rest) {
8983 for (j=0; j<post_len; j++) {
8984 int idx = local_body->local_table_size - (post_start + j);
8985 ADD_GETLOCAL(args, node, idx, lvar_level);
8987 ADD_INSN1(args, node, newarray,
INT2FIX(j));
8988 ADD_INSN (args, node, concatarray);
8993 for (j=0; j<post_len; j++) {
8994 int idx = local_body->local_table_size - (post_start + j);
8995 ADD_GETLOCAL(args, node, idx, lvar_level);
8997 argc = post_len + post_start;
9001 if (local_body->
param.flags.has_kw) {
9002 int local_size = local_body->local_table_size;
9005 ADD_INSN1(args, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9007 if (local_body->
param.flags.has_kwrest) {
9008 int idx = local_body->local_table_size - local_kwd->rest_start;
9009 ADD_GETLOCAL(args, node, idx, lvar_level);
9010 if (local_kwd->num > 0) {
9012 flag |= VM_CALL_KW_SPLAT_MUT;
9016 ADD_INSN1(args, node, newhash,
INT2FIX(0));
9017 flag |= VM_CALL_KW_SPLAT_MUT;
9019 for (i = 0; i < local_kwd->num; ++i) {
9020 ID id = local_kwd->table[i];
9021 int idx = local_size - get_local_var_idx(liseq,
id);
9022 ADD_INSN1(args, node, putobject,
ID2SYM(
id));
9023 ADD_GETLOCAL(args, node, idx, lvar_level);
9025 ADD_SEND(args, node, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
9026 if (local_body->
param.flags.has_rest) {
9027 ADD_INSN1(args, node, newarray,
INT2FIX(1));
9028 ADD_INSN (args, node, concatarray);
9031 flag |= VM_CALL_KW_SPLAT;
9033 else if (local_body->
param.flags.has_kwrest) {
9034 int idx = local_body->local_table_size - local_kwd->rest_start;
9035 ADD_GETLOCAL(args, node, idx, lvar_level);
9037 if (local_body->
param.flags.has_rest) {
9038 ADD_INSN1(args, node, newarray,
INT2FIX(1));
9039 ADD_INSN (args, node, concatarray);
9044 flag |= VM_CALL_KW_SPLAT;
9048 flag |= VM_CALL_SUPER | VM_CALL_FCALL;
9049 if (
type == NODE_ZSUPER) flag |= VM_CALL_ZSUPER;
9050 ADD_INSN(ret, node, putself);
9052 ADD_INSN2(ret, node, invokesuper,
9053 new_callinfo(iseq, 0, argc, flag, keywords, parent_block != NULL),
9057 ADD_INSN(ret, node, pop);
9067 unsigned int flag = 0;
9072 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
9074 case ISEQ_TYPE_MAIN:
9075 case ISEQ_TYPE_CLASS:
9076 COMPILE_ERROR(ERROR_ARGS
"Invalid yield");
9081 if (node->nd_head) {
9082 argc = setup_args(iseq, args, node->nd_head, &flag, &keywords);
9083 CHECK(!
NIL_P(argc));
9090 ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0,
FIX2INT(argc), flag, keywords, FALSE));
9093 ADD_INSN(ret, node, pop);
9098 for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
9099 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
9101 if (level > 0) access_outer_variables(iseq, level,
rb_intern(
"yield"),
true);
9114 switch ((
int)
type) {
9116 ADD_INSN1(recv, node, putobject, node->nd_lit);
9117 ADD_INSN2(val, node, getspecial,
INT2FIX(0),
9121 CHECK(COMPILE(recv,
"receiver", node->nd_recv));
9122 CHECK(COMPILE(val,
"value", node->nd_value));
9125 CHECK(COMPILE(recv,
"receiver", node->nd_value));
9126 CHECK(COMPILE(val,
"value", node->nd_recv));
9132 ADD_SEND(ret, node, idEqTilde,
INT2FIX(1));
9134 if (node->nd_args) {
9135 compile_named_capture_assign(iseq, ret, node->nd_args);
9139 ADD_INSN(ret, node, pop);
9150 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache &&
9151 (segments = collect_const_segments(iseq, node))) {
9152 ISEQ_BODY(iseq)->ic_size++;
9153 ADD_INSN1(ret, node, opt_getconstant_path, segments);
9163 CHECK(compile_const_prefix(iseq, node, pref, body));
9164 if (LIST_INSN_SIZE_ZERO(pref)) {
9165 ADD_INSN(ret, node, putnil);
9176 ADD_CALL_RECEIVER(ret, node);
9177 CHECK(COMPILE(ret,
"colon2#nd_head", node->nd_head));
9178 ADD_CALL(ret, node, node->nd_mid,
INT2FIX(1));
9181 ADD_INSN(ret, node, pop);
9189 debugi(
"colon3#nd_mid", node->nd_mid);
9192 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
9193 ISEQ_BODY(iseq)->ic_size++;
9195 ADD_INSN1(ret, node, opt_getconstant_path, segments);
9199 ADD_INSN1(ret, node, putobject, rb_cObject);
9200 ADD_INSN1(ret, node, putobject,
Qtrue);
9201 ADD_INSN1(ret, node, getconstant,
ID2SYM(node->nd_mid));
9205 ADD_INSN(ret, node, pop);
9214 const NODE *b = node->nd_beg;
9215 const NODE *e = node->nd_end;
9217 if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) {
9219 VALUE bv = nd_type_p(b, NODE_LIT) ? b->nd_lit :
Qnil;
9220 VALUE ev = nd_type_p(e, NODE_LIT) ? e->nd_lit :
Qnil;
9222 ADD_INSN1(ret, node, putobject, val);
9227 CHECK(COMPILE_(ret,
"min", b, popped));
9228 CHECK(COMPILE_(ret,
"max", e, popped));
9230 ADD_INSN1(ret, node, newrange, flag);
9240 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
9241 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, 0);
9247 if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_RESCUE) {
9250 ip = ISEQ_BODY(ip)->parent_iseq;
9254 ADD_GETLOCAL(ret, node, LVAR_ERRINFO, level);
9257 ADD_INSN(ret, node, putnil);
9268 LABEL *end_label = NEW_LABEL(nd_line(node));
9269 const NODE *default_value = node->nd_body->nd_value;
9271 if (default_value == NODE_SPECIAL_REQUIRED_KEYWORD) {
9273 COMPILE_ERROR(ERROR_ARGS
"unreachable");
9276 else if (nd_type_p(default_value, NODE_LIT) ||
9277 nd_type_p(default_value, NODE_NIL) ||
9278 nd_type_p(default_value, NODE_TRUE) ||
9279 nd_type_p(default_value, NODE_FALSE)) {
9280 COMPILE_ERROR(ERROR_ARGS
"unreachable");
9288 int kw_bits_idx = body->local_table_size - body->
param.keyword->bits_start;
9289 int keyword_idx = body->
param.keyword->num;
9291 ADD_INSN2(ret, node, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(keyword_idx));
9292 ADD_INSNL(ret, node, branchif, end_label);
9293 CHECK(COMPILE_POPPED(ret,
"keyword default argument", node->nd_body));
9294 ADD_LABEL(ret, end_label);
9304 unsigned int flag = 0;
9305 ID mid = node->nd_mid;
9307 LABEL *else_label = NULL;
9313 if (mid == idASET && !private_recv_p(node) && node->nd_args &&
9314 nd_type_p(node->nd_args, NODE_LIST) && node->nd_args->nd_alen == 2 &&
9315 nd_type_p(node->nd_args->nd_head, NODE_STR) &&
9316 ISEQ_COMPILE_DATA(iseq)->current_block == NULL &&
9317 !ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
9318 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction)
9320 VALUE str = rb_fstring(node->nd_args->nd_head->nd_lit);
9321 CHECK(COMPILE(ret,
"recv", node->nd_recv));
9322 CHECK(COMPILE(ret,
"value", node->nd_args->nd_next->nd_head));
9324 ADD_INSN(ret, node, swap);
9325 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9327 ADD_INSN2(ret, node, opt_aset_with, str,
9328 new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
9330 ADD_INSN(ret, node, pop);
9336 argc = setup_args(iseq, args, node->nd_args, &flag, NULL);
9337 CHECK(!
NIL_P(argc));
9339 int asgnflag = COMPILE_RECV(recv,
"recv", node);
9340 CHECK(asgnflag != -1);
9341 flag |= (
unsigned int)asgnflag;
9343 debugp_param(
"argc", argc);
9344 debugp_param(
"nd_mid",
ID2SYM(mid));
9349 else_label = qcall_branch_start(iseq, recv, &branches, node, node);
9352 ADD_INSN(ret, node, putnil);
9356 if (flag & VM_CALL_ARGS_BLOCKARG) {
9357 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9358 if (flag & VM_CALL_ARGS_SPLAT) {
9359 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
9360 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
9362 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 3));
9363 ADD_INSN (ret, node, pop);
9365 else if (flag & VM_CALL_ARGS_SPLAT) {
9366 ADD_INSN(ret, node, dup);
9367 ADD_INSN1(ret, node, putobject,
INT2FIX(-1));
9368 ADD_SEND_WITH_FLAG(ret, node, idAREF,
INT2FIX(1),
INT2FIX(asgnflag));
9369 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 2));
9370 ADD_INSN (ret, node, pop);
9373 ADD_INSN1(ret, node, setn, FIXNUM_INC(argc, 1));
9380 ADD_SEND_WITH_FLAG(ret, node, mid, argc,
INT2FIX(flag));
9381 qcall_branch_end(iseq, ret, else_label, branches, node, node);
9382 ADD_INSN(ret, node, pop);
9399 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line;
9400 if (lineno == 0) lineno =
FIX2INT(rb_iseq_first_lineno(iseq));
9401 debugs(
"node: NODE_NIL(implicit)\n");
9402 NODE dummy_line_node = generate_dummy_line_node(lineno, -1);
9403 ADD_INSN(ret, &dummy_line_node, putnil);
9407 return iseq_compile_each0(iseq, ret, node, popped);
9413 const int line = (int)nd_line(node);
9414 const enum node_type
type = nd_type(node);
9417 if (ISEQ_COMPILE_DATA(iseq)->last_line == line) {
9421 if (node->flags & NODE_FL_NEWLINE) {
9423 ISEQ_COMPILE_DATA(iseq)->last_line = line;
9424 if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
9425 event |= RUBY_EVENT_COVERAGE_LINE;
9427 ADD_TRACE(ret, event);
9431 debug_node_start(node);
9433#define BEFORE_RETURN debug_node_end()
9437 CHECK(compile_block(iseq, ret, node, popped));
9441 CHECK(compile_if(iseq, ret, node, popped,
type));
9444 CHECK(compile_case(iseq, ret, node, popped));
9447 CHECK(compile_case2(iseq, ret, node, popped));
9450 CHECK(compile_case3(iseq, ret, node, popped));
9454 CHECK(compile_loop(iseq, ret, node, popped,
type));
9458 CHECK(compile_iter(iseq, ret, node, popped));
9460 case NODE_FOR_MASGN:
9461 CHECK(compile_for_masgn(iseq, ret, node, popped));
9464 CHECK(compile_break(iseq, ret, node, popped));
9467 CHECK(compile_next(iseq, ret, node, popped));
9470 CHECK(compile_redo(iseq, ret, node, popped));
9473 CHECK(compile_retry(iseq, ret, node, popped));
9476 CHECK(COMPILE_(ret,
"NODE_BEGIN", node->nd_body, popped));
9480 CHECK(compile_rescue(iseq, ret, node, popped));
9483 CHECK(compile_resbody(iseq, ret, node, popped));
9486 CHECK(compile_ensure(iseq, ret, node, popped));
9491 LABEL *end_label = NEW_LABEL(line);
9492 CHECK(COMPILE(ret,
"nd_1st", node->nd_1st));
9494 ADD_INSN(ret, node, dup);
9496 if (
type == NODE_AND) {
9497 ADD_INSNL(ret, node, branchunless, end_label);
9500 ADD_INSNL(ret, node, branchif, end_label);
9503 ADD_INSN(ret, node, pop);
9505 CHECK(COMPILE_(ret,
"nd_2nd", node->nd_2nd, popped));
9506 ADD_LABEL(ret, end_label);
9511 compile_massign(iseq, ret, node, popped);
9516 ID id = node->nd_vid;
9517 int idx = ISEQ_BODY(body->local_iseq)->local_table_size - get_local_var_idx(iseq,
id);
9519 debugs(
"lvar: %s idx: %d\n",
rb_id2name(
id), idx);
9520 CHECK(COMPILE(ret,
"rvalue", node->nd_value));
9523 ADD_INSN(ret, node, dup);
9525 ADD_SETLOCAL(ret, node, idx, get_lvar_level(iseq));
9530 ID id = node->nd_vid;
9531 CHECK(COMPILE(ret,
"dvalue", node->nd_value));
9532 debugi(
"dassn id", rb_id2str(
id) ?
id :
'*');
9535 ADD_INSN(ret, node, dup);
9538 idx = get_dyna_var_idx(iseq,
id, &lv, &ls);
9541 COMPILE_ERROR(ERROR_ARGS
"NODE_DASGN: unknown id (%"PRIsVALUE
")",
9545 ADD_SETLOCAL(ret, node, ls - idx, lv);
9549 CHECK(COMPILE(ret,
"lvalue", node->nd_value));
9552 ADD_INSN(ret, node, dup);
9554 ADD_INSN1(ret, node, setglobal,
ID2SYM(node->nd_entry));
9558 CHECK(COMPILE(ret,
"lvalue", node->nd_value));
9560 ADD_INSN(ret, node, dup);
9562 ADD_INSN2(ret, node, setinstancevariable,
9564 get_ivar_ic_value(iseq,node->nd_vid));
9569 CHECK(COMPILE(ret,
"lvalue", node->nd_value));
9572 ADD_INSN(ret, node, dup);
9575 ADD_INSN1(ret, node, putspecialobject,
9576 INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
9577 ADD_INSN1(ret, node, setconstant,
ID2SYM(node->nd_vid));
9580 compile_cpath(ret, iseq, node->nd_else);
9581 CHECK(COMPILE(ret,
"lvalue", node->nd_value));
9582 ADD_INSN(ret, node, swap);
9585 ADD_INSN1(ret, node, topn,
INT2FIX(1));
9586 ADD_INSN(ret, node, swap);
9589 ADD_INSN1(ret, node, setconstant,
ID2SYM(node->nd_else->nd_mid));
9594 CHECK(COMPILE(ret,
"cvasgn val", node->nd_value));
9596 ADD_INSN(ret, node, dup);
9598 ADD_INSN2(ret, node, setclassvariable,
9600 get_cvar_ic_value(iseq,node->nd_vid));
9604 CHECK(compile_op_asgn1(iseq, ret, node, popped));
9607 CHECK(compile_op_asgn2(iseq, ret, node, popped));
9610 CHECK(compile_op_cdecl(iseq, ret, node, popped));
9612 case NODE_OP_ASGN_AND:
9613 case NODE_OP_ASGN_OR:
9614 CHECK(compile_op_log(iseq, ret, node, popped,
type));
9618 if (compile_call_precheck_freeze(iseq, ret, node, node, popped) == TRUE) {
9624 if (compile_call(iseq, ret, node,
type, node, popped,
false) == COMPILE_NG) {
9630 CHECK(compile_super(iseq, ret, node, popped,
type));
9633 CHECK(compile_array(iseq, ret, node, popped) >= 0);
9638 ADD_INSN1(ret, node, newarray,
INT2FIX(0));
9643 const NODE *n = node;
9645 COMPILE_ERROR(ERROR_ARGS
"NODE_VALUES: must not be popped");
9648 CHECK(COMPILE(ret,
"values item", n->nd_head));
9651 ADD_INSN1(ret, node, newarray,
INT2FIX(node->nd_alen));
9655 CHECK(compile_hash(iseq, ret, node, FALSE, popped) >= 0);
9658 CHECK(compile_return(iseq, ret, node, popped));
9661 CHECK(compile_yield(iseq, ret, node, popped));
9665 compile_lvar(iseq, ret, node, node->nd_vid);
9671 debugi(
"nd_vid", node->nd_vid);
9673 idx = get_dyna_var_idx(iseq, node->nd_vid, &lv, &ls);
9675 COMPILE_ERROR(ERROR_ARGS
"unknown dvar (%"PRIsVALUE
")",
9676 rb_id2str(node->nd_vid));
9679 ADD_GETLOCAL(ret, node, ls - idx, lv);
9684 ADD_INSN1(ret, node, getglobal,
ID2SYM(node->nd_entry));
9686 ADD_INSN(ret, node, pop);
9691 debugi(
"nd_vid", node->nd_vid);
9693 ADD_INSN2(ret, node, getinstancevariable,
9695 get_ivar_ic_value(iseq,node->nd_vid));
9700 debugi(
"nd_vid", node->nd_vid);
9702 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
9704 VALUE segments = rb_ary_new_from_args(1,
ID2SYM(node->nd_vid));
9705 ADD_INSN1(ret, node, opt_getconstant_path, segments);
9709 ADD_INSN(ret, node, putnil);
9710 ADD_INSN1(ret, node, putobject,
Qtrue);
9711 ADD_INSN1(ret, node, getconstant,
ID2SYM(node->nd_vid));
9715 ADD_INSN(ret, node, pop);
9721 ADD_INSN2(ret, node, getclassvariable,
9723 get_cvar_ic_value(iseq,node->nd_vid));
9729 if (!node->nd_nth) {
9730 ADD_INSN(ret, node, putnil);
9733 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
9738 case NODE_BACK_REF:{
9740 ADD_INSN2(ret, node, getspecial,
INT2FIX(1) ,
9741 INT2FIX(0x01 | (node->nd_nth << 1)));
9748 CHECK(compile_match(iseq, ret, node, popped,
type));
9751 debugp_param(
"lit", node->nd_lit);
9753 ADD_INSN1(ret, node, putobject, node->nd_lit);
9759 debugp_param(
"nd_lit", node->nd_lit);
9761 VALUE lit = node->nd_lit;
9762 if (!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal) {
9763 lit = rb_fstring(lit);
9764 ADD_INSN1(ret, node, putstring, lit);
9768 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
9769 VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq),
INT2FIX(line));
9775 lit = rb_fstring(lit);
9777 ADD_INSN1(ret, node, putobject, lit);
9784 compile_dstr(iseq, ret, node);
9787 ADD_INSN(ret, node, pop);
9792 ADD_CALL_RECEIVER(ret, node);
9793 VALUE str = rb_fstring(node->nd_lit);
9794 ADD_INSN1(ret, node, putobject, str);
9796 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
9799 ADD_INSN(ret, node, pop);
9804 ADD_CALL_RECEIVER(ret, node);
9805 compile_dstr(iseq, ret, node);
9806 ADD_CALL(ret, node, idBackquote,
INT2FIX(1));
9809 ADD_INSN(ret, node, pop);
9814 CHECK(compile_evstr(iseq, ret, node->nd_body, popped));
9817 compile_dregx(iseq, ret, node);
9820 ADD_INSN(ret, node, pop);
9825 int ic_index = body->ise_size++;
9827 block_iseq = NEW_CHILD_ISEQ(node->nd_body, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, line);
9829 ADD_INSN2(ret, node, once, block_iseq,
INT2FIX(ic_index));
9833 ADD_INSN(ret, node, pop);
9839 CHECK(COMPILE(ret,
"argscat head", node->nd_head));
9840 ADD_INSN1(ret, node, splatarray,
Qfalse);
9841 ADD_INSN(ret, node, pop);
9842 CHECK(COMPILE(ret,
"argscat body", node->nd_body));
9843 ADD_INSN1(ret, node, splatarray,
Qfalse);
9844 ADD_INSN(ret, node, pop);
9847 CHECK(COMPILE(ret,
"argscat head", node->nd_head));
9848 CHECK(COMPILE(ret,
"argscat body", node->nd_body));
9849 ADD_INSN(ret, node, concatarray);
9853 case NODE_ARGSPUSH:{
9855 CHECK(COMPILE(ret,
"argspush head", node->nd_head));
9856 ADD_INSN1(ret, node, splatarray,
Qfalse);
9857 ADD_INSN(ret, node, pop);
9858 CHECK(COMPILE_(ret,
"argspush body", node->nd_body, popped));
9861 CHECK(COMPILE(ret,
"argspush head", node->nd_head));
9862 CHECK(compile_array_1(iseq, ret, node->nd_body));
9863 ADD_INSN(ret, node, concatarray);
9868 CHECK(COMPILE(ret,
"splat", node->nd_head));
9869 ADD_INSN1(ret, node, splatarray,
Qtrue);
9872 ADD_INSN(ret, node, pop);
9877 ID mid = node->nd_mid;
9878 const rb_iseq_t *method_iseq = NEW_ISEQ(node->nd_defn,
9880 ISEQ_TYPE_METHOD, line);
9882 debugp_param(
"defn/iseq", rb_iseqw_new(method_iseq));
9883 ADD_INSN2(ret, node, definemethod,
ID2SYM(mid), method_iseq);
9887 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
9893 ID mid = node->nd_mid;
9894 const rb_iseq_t * singleton_method_iseq = NEW_ISEQ(node->nd_defn,
9896 ISEQ_TYPE_METHOD, line);
9898 debugp_param(
"defs/iseq", rb_iseqw_new(singleton_method_iseq));
9899 CHECK(COMPILE(ret,
"defs: recv", node->nd_recv));
9900 ADD_INSN2(ret, node, definesmethod,
ID2SYM(mid), singleton_method_iseq);
9904 ADD_INSN1(ret, node, putobject,
ID2SYM(mid));
9909 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9910 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
9911 CHECK(COMPILE(ret,
"alias arg1", node->nd_1st));
9912 CHECK(COMPILE(ret,
"alias arg2", node->nd_2nd));
9913 ADD_SEND(ret, node, id_core_set_method_alias,
INT2FIX(3));
9916 ADD_INSN(ret, node, pop);
9921 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9922 ADD_INSN1(ret, node, putobject,
ID2SYM(node->nd_alias));
9923 ADD_INSN1(ret, node, putobject,
ID2SYM(node->nd_orig));
9924 ADD_SEND(ret, node, id_core_set_variable_alias,
INT2FIX(2));
9927 ADD_INSN(ret, node, pop);
9932 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9933 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
9934 CHECK(COMPILE(ret,
"undef arg", node->nd_undef));
9935 ADD_SEND(ret, node, id_core_undef_method,
INT2FIX(2));
9938 ADD_INSN(ret, node, pop);
9943 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(node->nd_body,
9945 ISEQ_TYPE_CLASS, line);
9946 const int flags = VM_DEFINECLASS_TYPE_CLASS |
9947 (node->nd_super ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
9948 compile_cpath(ret, iseq, node->nd_cpath);
9950 CHECK(COMPILE(ret,
"super", node->nd_super));
9951 ADD_INSN3(ret, node, defineclass,
ID2SYM(node->nd_cpath->nd_mid), class_iseq,
INT2FIX(flags));
9955 ADD_INSN(ret, node, pop);
9960 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(node->nd_body,
9962 ISEQ_TYPE_CLASS, line);
9963 const int flags = VM_DEFINECLASS_TYPE_MODULE |
9964 compile_cpath(ret, iseq, node->nd_cpath);
9966 ADD_INSN (ret, node, putnil);
9967 ADD_INSN3(ret, node, defineclass,
ID2SYM(node->nd_cpath->nd_mid), module_iseq,
INT2FIX(flags));
9971 ADD_INSN(ret, node, pop);
9977 const rb_iseq_t *singleton_class = NEW_ISEQ(node->nd_body, rb_fstring_lit(
"singleton class"),
9978 ISEQ_TYPE_CLASS, line);
9980 CHECK(COMPILE(ret,
"sclass#recv", node->nd_recv));
9981 ADD_INSN (ret, node, putnil);
9982 CONST_ID(singletonclass,
"singletonclass");
9983 ADD_INSN3(ret, node, defineclass,
9984 ID2SYM(singletonclass), singleton_class,
9985 INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
9989 ADD_INSN(ret, node, pop);
9994 CHECK(compile_colon2(iseq, ret, node, popped));
9997 CHECK(compile_colon3(iseq, ret, node, popped));
10000 CHECK(compile_dots(iseq, ret, node, popped, FALSE));
10003 CHECK(compile_dots(iseq, ret, node, popped, TRUE));
10007 LABEL *lend = NEW_LABEL(line);
10008 LABEL *ltrue = NEW_LABEL(line);
10009 LABEL *lfalse = NEW_LABEL(line);
10010 CHECK(compile_flip_flop(iseq, ret, node,
type == NODE_FLIP2,
10012 ADD_LABEL(ret, ltrue);
10013 ADD_INSN1(ret, node, putobject,
Qtrue);
10014 ADD_INSNL(ret, node, jump, lend);
10015 ADD_LABEL(ret, lfalse);
10016 ADD_INSN1(ret, node, putobject,
Qfalse);
10017 ADD_LABEL(ret, lend);
10022 ADD_INSN(ret, node, putself);
10028 ADD_INSN(ret, node, putnil);
10034 ADD_INSN1(ret, node, putobject,
Qtrue);
10040 ADD_INSN1(ret, node, putobject,
Qfalse);
10045 CHECK(compile_errinfo(iseq, ret, node, popped));
10049 CHECK(compile_defined_expr(iseq, ret, node,
Qtrue));
10052 case NODE_POSTEXE:{
10056 int is_index = body->ise_size++;
10058 rb_iseq_new_with_callback_new_callback(build_postexe_iseq, node->nd_body);
10060 new_child_iseq_with_callback(iseq, ifunc,
10061 rb_fstring(make_name_for_block(iseq)), iseq, ISEQ_TYPE_BLOCK, line);
10063 ADD_INSN2(ret, node, once, once_iseq,
INT2FIX(is_index));
10067 ADD_INSN(ret, node, pop);
10072 CHECK(compile_kw_arg(iseq, ret, node, popped));
10075 compile_dstr(iseq, ret, node);
10077 ADD_INSN(ret, node, intern);
10080 ADD_INSN(ret, node, pop);
10084 case NODE_ATTRASGN:
10085 CHECK(compile_attrasgn(iseq, ret, node, popped));
10089 const rb_iseq_t *block = NEW_CHILD_ISEQ(node->nd_body, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
10092 ADD_INSN1(ret, node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10093 ADD_CALL_WITH_BLOCK(ret, node, idLambda, argc, block);
10097 ADD_INSN(ret, node, pop);
10102 UNKNOWN_NODE(
"iseq_compile_each", node, COMPILE_NG);
10117insn_data_length(
INSN *iobj)
10119 return insn_len(iobj->insn_id);
10123calc_sp_depth(
int depth,
INSN *insn)
10125 return comptime_insn_stack_increase(depth, insn->insn_id, insn->operands);
10129opobj_inspect(
VALUE obj)
10137 obj = rb_ary_dup(obj);
10149insn_data_to_s_detail(
INSN *iobj)
10153 if (iobj->operands) {
10154 const char *types = insn_op_types(iobj->insn_id);
10157 for (j = 0; types[j]; j++) {
10158 char type = types[j];
10182 VALUE v = OPERAND_AT(iobj, j);
10209 if (vm_ci_mid(ci))
rb_str_catf(str,
"%"PRIsVALUE, rb_id2str(vm_ci_mid(ci)));
10218 void *func = (
void *)OPERAND_AT(iobj, j);
10221 if (dladdr(func, &info) && info.dli_sname) {
10236 if (types[j + 1]) {
10247 dump_disasm_list_with_cursor(link, NULL, NULL);
10258 printf(
"-- raw disasm--------\n");
10261 if (curr) printf(curr == link ?
"*" :
" ");
10262 switch (link->type) {
10263 case ISEQ_ELEMENT_INSN:
10265 iobj = (
INSN *)link;
10266 str = insn_data_to_s_detail(iobj);
10267 printf(
" %04d %-65s(%4u)\n", pos,
StringValueCStr(str), iobj->insn_info.line_no);
10268 pos += insn_data_length(iobj);
10271 case ISEQ_ELEMENT_LABEL:
10273 lobj = (
LABEL *)link;
10274 printf(LABEL_FORMAT
" [sp: %d]%s\n", lobj->label_no, lobj->sp,
10275 dest == lobj ?
" <---" :
"");
10278 case ISEQ_ELEMENT_TRACE:
10281 printf(
" trace: %0x\n", trace->event);
10284 case ISEQ_ELEMENT_ADJUST:
10287 printf(
" adjust: [label: %d]\n", adjust->label ? adjust->label->label_no : -1);
10296 printf(
"---------------------\n");
10301rb_insns_name(
int i)
10303 return insn_name(i);
10307rb_insns_name_array(
void)
10309 VALUE ary = rb_ary_new_capa(VM_INSTRUCTION_SIZE);
10311 for (i = 0; i < VM_INSTRUCTION_SIZE; i++) {
10312 rb_ary_push(ary, rb_fstring_cstr(insn_name(i)));
10322 obj = rb_to_symbol_type(obj);
10324 if (st_lookup(labels_table, obj, &tmp) == 0) {
10325 label = NEW_LABEL(0);
10326 st_insert(labels_table, obj, (st_data_t)label);
10329 label = (
LABEL *)tmp;
10336get_exception_sym2type(
VALUE sym)
10338 static VALUE symRescue, symEnsure, symRetry;
10339 static VALUE symBreak, symRedo, symNext;
10341 if (symRescue == 0) {
10350 if (sym == symRescue)
return CATCH_TYPE_RESCUE;
10351 if (sym == symEnsure)
return CATCH_TYPE_ENSURE;
10352 if (sym == symRetry)
return CATCH_TYPE_RETRY;
10353 if (sym == symBreak)
return CATCH_TYPE_BREAK;
10354 if (sym == symRedo)
return CATCH_TYPE_REDO;
10355 if (sym == symNext)
return CATCH_TYPE_NEXT;
10369 LABEL *lstart, *lend, *lcont;
10384 lstart = register_label(iseq, labels_table,
RARRAY_AREF(v, 2));
10385 lend = register_label(iseq, labels_table,
RARRAY_AREF(v, 3));
10386 lcont = register_label(iseq, labels_table,
RARRAY_AREF(v, 4));
10390 if (
type == CATCH_TYPE_RESCUE ||
10391 type == CATCH_TYPE_BREAK ||
10392 type == CATCH_TYPE_NEXT) {
10398 ADD_CATCH_ENTRY(
type, lstart, lend, eiseq, lcont);
10406insn_make_insn_table(
void)
10410 table = st_init_numtable_with_size(VM_INSTRUCTION_SIZE);
10412 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
10426 iseqw = rb_iseq_load(op, (
VALUE)iseq,
Qnil);
10428 else if (
CLASS_OF(op) == rb_cISeq) {
10435 loaded_iseq = rb_iseqw_to_iseq(iseqw);
10436 return loaded_iseq;
10444 unsigned int flag = 0;
10455 if (!
NIL_P(vorig_argc)) orig_argc =
FIX2INT(vorig_argc);
10457 if (!
NIL_P(vkw_arg)) {
10460 size_t n = rb_callinfo_kwarg_bytes(len);
10463 kw_arg->keyword_len = len;
10464 for (i = 0; i < len; i++) {
10467 kw_arg->keywords[i] = kw;
10472 const struct rb_callinfo *ci = new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
10478event_name_to_flag(
VALUE sym)
10480#define CHECK_EVENT(ev) if (sym == ID2SYM(rb_intern_const(#ev))) return ev;
10500 int line_no = 0, node_id = -1, insn_idx = 0;
10501 int ret = COMPILE_OK;
10506 static struct st_table *insn_table;
10508 if (insn_table == 0) {
10509 insn_table = insn_make_insn_table();
10512 for (i=0; i<len; i++) {
10518 ADD_TRACE(anchor, event);
10521 LABEL *label = register_label(iseq, labels_table, obj);
10522 ADD_LABEL(anchor, label);
10535 node_id =
NUM2INT(rb_ary_entry(node_ids, insn_idx++));
10539 if (st_lookup(insn_table, (st_data_t)insn, &insn_id) == 0) {
10541 COMPILE_ERROR(iseq, line_no,
10542 "unknown instruction: %+"PRIsVALUE, insn);
10547 if (argc != insn_len((
VALUE)insn_id)-1) {
10548 COMPILE_ERROR(iseq, line_no,
10549 "operand size mismatch");
10555 argv = compile_data_calloc2(iseq,
sizeof(
VALUE), argc);
10558 NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
10561 (
enum ruby_vminsn_type)insn_id, argc, argv));
10563 for (j=0; j<argc; j++) {
10564 VALUE op = rb_ary_entry(obj, j+1);
10565 switch (insn_op_type((
VALUE)insn_id, j)) {
10567 LABEL *label = register_label(iseq, labels_table, op);
10568 argv[j] = (
VALUE)label;
10583 VALUE v = (
VALUE)iseq_build_load_iseq(iseq, op);
10594 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ise_size) {
10595 ISEQ_BODY(iseq)->ise_size =
NUM2INT(op) + 1;
10600 VALUE segments = rb_ary_new();
10601 op = rb_to_array_type(op);
10605 sym = rb_to_symbol_type(sym);
10606 rb_ary_push(segments, sym);
10610 argv[j] = segments;
10612 ISEQ_BODY(iseq)->ic_size++;
10617 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->ivc_size) {
10618 ISEQ_BODY(iseq)->ivc_size =
NUM2INT(op) + 1;
10623 if (
NUM2UINT(op) >= ISEQ_BODY(iseq)->icvarc_size) {
10624 ISEQ_BODY(iseq)->icvarc_size =
NUM2INT(op) + 1;
10628 argv[j] = iseq_build_callinfo_from_hash(iseq, op);
10631 argv[j] = rb_to_symbol_type(op);
10638 RHASH_TBL_RAW(map)->type = &cdhash_type;
10639 op = rb_to_array_type(op);
10644 register_label(iseq, labels_table, sym);
10645 rb_hash_aset(map, key, (
VALUE)label | 1);
10654#if SIZEOF_VALUE <= SIZEOF_LONG
10659 argv[j] = (
VALUE)funcptr;
10668 NODE dummy_line_node = generate_dummy_line_node(line_no, node_id);
10671 (
enum ruby_vminsn_type)insn_id, argc, NULL));
10679 validate_labels(iseq, labels_table);
10680 if (!ret)
return ret;
10681 return iseq_setup(iseq, anchor);
10684#define CHECK_ARRAY(v) rb_to_array_type(v)
10685#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
10690 VALUE val = rb_hash_aref(param, sym);
10695 else if (!
NIL_P(val)) {
10702static const struct rb_iseq_param_keyword *
10708 VALUE key, sym, default_val;
10711 struct rb_iseq_param_keyword *keyword =
ZALLOC(
struct rb_iseq_param_keyword);
10713 ISEQ_BODY(iseq)->param.flags.has_kw = TRUE;
10715 keyword->num = len;
10716#define SYM(s) ID2SYM(rb_intern_const(#s))
10717 (void)int_param(&keyword->bits_start, params, SYM(kwbits));
10718 i = keyword->bits_start - keyword->num;
10719 ids = (
ID *)&ISEQ_BODY(iseq)->local_table[i];
10723 for (i = 0; i < len; i++) {
10727 goto default_values;
10730 keyword->required_num++;
10734 default_len = len - i;
10735 if (default_len == 0) {
10736 keyword->table = ids;
10739 else if (default_len < 0) {
10745 for (j = 0; i < len; i++, j++) {
10762 dvs[j] = default_val;
10765 keyword->table = ids;
10766 keyword->default_values = dvs;
10772iseq_insn_each_object_mark(
VALUE *obj_ptr,
VALUE _)
10774 rb_gc_mark(*obj_ptr);
10781 size_t size =
sizeof(
INSN);
10782 unsigned int pos = 0;
10785#ifdef STRICT_ALIGNMENT
10786 size_t padding = calc_padding((
void *)&storage->buff[pos], size);
10788 const size_t padding = 0;
10790 size_t offset = pos + size + padding;
10791 if (offset > storage->size || offset > storage->pos) {
10793 storage = storage->next;
10796#ifdef STRICT_ALIGNMENT
10797 pos += (int)padding;
10800 iobj = (
INSN *)&storage->buff[pos];
10802 if (iobj->operands) {
10803 iseq_insn_each_markable_object(iobj, iseq_insn_each_object_mark, (
VALUE)0);
10814#define SYM(s) ID2SYM(rb_intern_const(#s))
10816 unsigned int arg_size, local_size, stack_max;
10818 struct st_table *labels_table = st_init_numtable();
10820 VALUE arg_opt_labels = rb_hash_aref(params, SYM(opt));
10821 VALUE keywords = rb_hash_aref(params, SYM(keyword));
10823 DECL_ANCHOR(anchor);
10824 INIT_ANCHOR(anchor);
10827 ISEQ_BODY(iseq)->local_table_size = len;
10828 ISEQ_BODY(iseq)->local_table = tbl = len > 0 ? (
ID *)
ALLOC_N(
ID, ISEQ_BODY(iseq)->local_table_size) : NULL;
10830 for (i = 0; i < len; i++) {
10833 if (sym_arg_rest == lv) {
10841#define INT_PARAM(F) int_param(&ISEQ_BODY(iseq)->param.F, params, SYM(F))
10842 if (INT_PARAM(lead_num)) {
10843 ISEQ_BODY(iseq)->param.flags.has_lead = TRUE;
10845 if (INT_PARAM(post_num)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
10846 if (INT_PARAM(post_start)) ISEQ_BODY(iseq)->param.flags.has_post = TRUE;
10847 if (INT_PARAM(rest_start)) ISEQ_BODY(iseq)->param.flags.has_rest = TRUE;
10848 if (INT_PARAM(block_start)) ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
10851#define INT_PARAM(F) F = (int_param(&x, misc, SYM(F)) ? (unsigned int)x : 0)
10853 INT_PARAM(arg_size);
10854 INT_PARAM(local_size);
10855 INT_PARAM(stack_max);
10860#ifdef USE_ISEQ_NODE_ID
10869 ISEQ_BODY(iseq)->param.flags.has_opt = !!(len - 1 >= 0);
10871 if (ISEQ_BODY(iseq)->param.flags.has_opt) {
10874 for (i = 0; i < len; i++) {
10876 LABEL *label = register_label(iseq, labels_table, ent);
10877 opt_table[i] = (
VALUE)label;
10880 ISEQ_BODY(iseq)->param.opt_num = len - 1;
10881 ISEQ_BODY(iseq)->param.opt_table = opt_table;
10884 else if (!
NIL_P(arg_opt_labels)) {
10890 ISEQ_BODY(iseq)->param.keyword = iseq_build_kw(iseq, params, keywords);
10892 else if (!
NIL_P(keywords)) {
10897 if (
Qtrue == rb_hash_aref(params, SYM(ambiguous_param0))) {
10898 ISEQ_BODY(iseq)->param.flags.ambiguous_param0 = TRUE;
10901 if (int_param(&i, params, SYM(kwrest))) {
10902 struct rb_iseq_param_keyword *keyword = (
struct rb_iseq_param_keyword *)ISEQ_BODY(iseq)->param.keyword;
10903 if (keyword == NULL) {
10904 ISEQ_BODY(iseq)->param.keyword = keyword =
ZALLOC(
struct rb_iseq_param_keyword);
10906 keyword->rest_start = i;
10907 ISEQ_BODY(iseq)->param.flags.has_kwrest = TRUE;
10910 iseq_calc_param_size(iseq);
10913 iseq_build_from_ary_exception(iseq, labels_table, exception);
10916 iseq_build_from_ary_body(iseq, anchor, body, node_ids, labels_wrapper);
10918 ISEQ_BODY(iseq)->param.size = arg_size;
10919 ISEQ_BODY(iseq)->local_table_size = local_size;
10920 ISEQ_BODY(iseq)->stack_max = stack_max;
10930 while (body->type == ISEQ_TYPE_BLOCK ||
10931 body->type == ISEQ_TYPE_RESCUE ||
10932 body->type == ISEQ_TYPE_ENSURE ||
10933 body->type == ISEQ_TYPE_EVAL ||
10934 body->type == ISEQ_TYPE_MAIN
10938 for (i = 0; i < body->local_table_size; i++) {
10939 if (body->local_table[i] ==
id) {
10943 iseq = body->parent_iseq;
10944 body = ISEQ_BODY(iseq);
10957 for (i=0; i<body->local_table_size; i++) {
10958 if (body->local_table[i] ==
id) {
10968#ifndef IBF_ISEQ_DEBUG
10969#define IBF_ISEQ_DEBUG 0
10972#ifndef IBF_ISEQ_ENABLE_LOCAL_BUFFER
10973#define IBF_ISEQ_ENABLE_LOCAL_BUFFER 0
10976typedef unsigned int ibf_offset_t;
10977#define IBF_OFFSET(ptr) ((ibf_offset_t)(VALUE)(ptr))
10979#define IBF_MAJOR_VERSION ISEQ_MAJOR_VERSION
10981#define IBF_DEVEL_VERSION 4
10982#define IBF_MINOR_VERSION (ISEQ_MINOR_VERSION * 10000 + IBF_DEVEL_VERSION)
10984#define IBF_MINOR_VERSION ISEQ_MINOR_VERSION
10989 unsigned int major_version;
10990 unsigned int minor_version;
10992 unsigned int extra_size;
10994 unsigned int iseq_list_size;
10995 unsigned int global_object_list_size;
10996 ibf_offset_t iseq_list_offset;
10997 ibf_offset_t global_object_list_offset;
11016 unsigned int obj_list_size;
11017 ibf_offset_t obj_list_offset;
11036pinned_list_mark(
void *ptr)
11040 for (i = 0; i < list->size; i++) {
11041 if (list->buffer[i]) {
11042 rb_gc_mark(list->buffer[i]);
11048pinned_list_free(
void *ptr)
11051 xfree(list->buffer);
11056pinned_list_memsize(
const void *ptr)
11064 {pinned_list_mark, pinned_list_free, pinned_list_memsize,},
11065 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
11069pinned_list_fetch(
VALUE list,
long offset)
11075 if (offset >= ptr->size) {
11079 return ptr->buffer[offset];
11083pinned_list_store(
VALUE list,
long offset,
VALUE object)
11089 if (offset >= ptr->size) {
11097pinned_list_new(
long size)
11110ibf_dump_pos(
struct ibf_dump *dump)
11112 long pos =
RSTRING_LEN(dump->current_buffer->str);
11113#if SIZEOF_LONG > SIZEOF_INT
11114 if (pos >= UINT_MAX) {
11118 return (
unsigned int)pos;
11122ibf_dump_align(
struct ibf_dump *dump,
size_t align)
11124 ibf_offset_t pos = ibf_dump_pos(dump);
11126 static const char padding[
sizeof(
VALUE)];
11127 size_t size = align - ((size_t)pos % align);
11128#if SIZEOF_LONG > SIZEOF_INT
11129 if (pos + size >= UINT_MAX) {
11133 for (; size >
sizeof(padding); size -=
sizeof(padding)) {
11134 rb_str_cat(dump->current_buffer->str, padding,
sizeof(padding));
11136 rb_str_cat(dump->current_buffer->str, padding, size);
11141ibf_dump_write(
struct ibf_dump *dump,
const void *buff,
unsigned long size)
11143 ibf_offset_t pos = ibf_dump_pos(dump);
11144 rb_str_cat(dump->current_buffer->str, (
const char *)buff, size);
11150ibf_dump_write_byte(
struct ibf_dump *dump,
unsigned char byte)
11152 return ibf_dump_write(dump, &
byte,
sizeof(
unsigned char));
11156ibf_dump_overwrite(
struct ibf_dump *dump,
void *buff,
unsigned int size,
long offset)
11158 VALUE str = dump->current_buffer->str;
11160 if ((
unsigned long)(size + offset) > (
unsigned long)
RSTRING_LEN(str))
11161 rb_bug(
"ibf_dump_overwrite: overflow");
11162 memcpy(ptr + offset, buff, size);
11166ibf_load_ptr(
const struct ibf_load *load, ibf_offset_t *offset,
int size)
11168 ibf_offset_t beg = *offset;
11170 return load->current_buffer->buff + beg;
11174ibf_load_alloc(
const struct ibf_load *load, ibf_offset_t offset,
size_t x,
size_t y)
11176 void *buff = ruby_xmalloc2(x, y);
11177 size_t size = x * y;
11178 memcpy(buff, load->current_buffer->buff + offset, size);
11182#define IBF_W_ALIGN(type) (RUBY_ALIGNOF(type) > 1 ? ibf_dump_align(dump, RUBY_ALIGNOF(type)) : (void)0)
11184#define IBF_W(b, type, n) (IBF_W_ALIGN(type), (type *)(VALUE)IBF_WP(b, type, n))
11185#define IBF_WV(variable) ibf_dump_write(dump, &(variable), sizeof(variable))
11186#define IBF_WP(b, type, n) ibf_dump_write(dump, (b), sizeof(type) * (n))
11187#define IBF_R(val, type, n) (type *)ibf_load_alloc(load, IBF_OFFSET(val), sizeof(type), (n))
11188#define IBF_ZERO(variable) memset(&(variable), 0, sizeof(variable))
11191ibf_table_lookup(
struct st_table *table, st_data_t key)
11195 if (st_lookup(table, key, &val)) {
11204ibf_table_find_or_insert(
struct st_table *table, st_data_t key)
11206 int index = ibf_table_lookup(table, key);
11209 index = (int)table->num_entries;
11210 st_insert(table, key, (st_data_t)index);
11218static void ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size);
11224ibf_dump_object_table_new(
void)
11226 st_table *obj_table = st_init_numtable();
11227 st_insert(obj_table, (st_data_t)
Qnil, (st_data_t)0);
11235 return ibf_table_find_or_insert(dump->current_buffer->obj_table, (st_data_t)obj);
11244 return ibf_dump_object(dump,
rb_id2sym(
id));
11248ibf_load_id(
const struct ibf_load *load,
const ID id_index)
11250 if (id_index == 0) {
11253 VALUE sym = ibf_load_object(load, id_index);
11259static ibf_offset_t ibf_dump_iseq_each(
struct ibf_dump *dump,
const rb_iseq_t *iseq);
11264 if (iseq == NULL) {
11268 return ibf_table_find_or_insert(dump->iseq_table, (st_data_t)iseq);
11272static unsigned char
11273ibf_load_byte(
const struct ibf_load *load, ibf_offset_t *offset)
11276 return (
unsigned char)load->current_buffer->buff[(*offset)++];
11292 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
11293 ibf_dump_write(dump, &x,
sizeof(
VALUE));
11297 enum { max_byte_length =
sizeof(
VALUE) + 1 };
11299 unsigned char bytes[max_byte_length];
11302 for (n = 0; n <
sizeof(
VALUE) && (x >> (7 - n)); n++, x >>= 8) {
11303 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
11309 bytes[max_byte_length - 1 - n] = (
unsigned char)x;
11312 ibf_dump_write(dump, bytes + max_byte_length - n, n);
11316ibf_load_small_value(
const struct ibf_load *load, ibf_offset_t *offset)
11318 if (
sizeof(
VALUE) > 8 || CHAR_BIT != 8) {
11319 union {
char s[
sizeof(
VALUE)];
VALUE v; } x;
11321 memcpy(x.s, load->current_buffer->buff + *offset,
sizeof(
VALUE));
11322 *offset +=
sizeof(
VALUE);
11327 enum { max_byte_length =
sizeof(
VALUE) + 1 };
11329 const unsigned char *buffer = (
const unsigned char *)load->current_buffer->buff;
11330 const unsigned char c = buffer[*offset];
11334 c == 0 ? 9 : ntz_int32(c) + 1;
11337 if (*offset + n > load->current_buffer->size) {
11342 for (i = 1; i < n; i++) {
11344 x |= (
VALUE)buffer[*offset + i];
11358 ibf_dump_write_small_value(dump, (
VALUE)bf->index);
11360 size_t len = strlen(bf->name);
11361 ibf_dump_write_small_value(dump, (
VALUE)len);
11362 ibf_dump_write(dump, bf->name, len);
11366ibf_load_builtin(
const struct ibf_load *load, ibf_offset_t *offset)
11368 int i = (int)ibf_load_small_value(load, offset);
11369 int len = (int)ibf_load_small_value(load, offset);
11370 const char *name = (
char *)ibf_load_ptr(load, offset, len);
11373 fprintf(stderr,
"%.*s!!\n", len, name);
11378 if (strncmp(table[i].name, name, len) != 0) {
11379 rb_raise(
rb_eArgError,
"builtin function index (%d) mismatch (expect %s but %s)", i, name, table[i].name);
11390 const int iseq_size = body->iseq_size;
11392 const VALUE *orig_code = rb_iseq_original_iseq(iseq);
11394 ibf_offset_t offset = ibf_dump_pos(dump);
11396 for (code_index=0; code_index<iseq_size;) {
11397 const VALUE insn = orig_code[code_index++];
11398 const char *types = insn_op_types(insn);
11403 ibf_dump_write_small_value(dump, insn);
11406 for (op_index=0; types[op_index]; op_index++, code_index++) {
11407 VALUE op = orig_code[code_index];
11410 switch (types[op_index]) {
11413 wv = ibf_dump_object(dump, op);
11422 wv = ibf_dump_object(dump, arr);
11430 wv = is - ISEQ_IS_ENTRY_START(body, types[op_index]);
11438 wv = ibf_dump_id(dump, (
ID)op);
11450 ibf_dump_write_small_value(dump, wv);
11453 assert(insn_len(insn) == op_index+1);
11460ibf_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)
11463 unsigned int code_index;
11464 ibf_offset_t reading_pos = bytecode_offset;
11468 struct rb_call_data *cd_entries = load_body->call_data;
11471 iseq_bits_t * mark_offset_bits;
11473 iseq_bits_t tmp[1] = {0};
11475 if (ISEQ_MBITS_BUFLEN(iseq_size) == 1) {
11476 mark_offset_bits = tmp;
11479 mark_offset_bits =
ZALLOC_N(iseq_bits_t, ISEQ_MBITS_BUFLEN(iseq_size));
11481 bool needs_bitmap =
false;
11483 for (code_index=0; code_index<iseq_size;) {
11485 const VALUE insn = code[code_index] = ibf_load_small_value(load, &reading_pos);
11486 const char *types = insn_op_types(insn);
11492 for (op_index=0; types[op_index]; op_index++, code_index++) {
11493 const char operand_type = types[op_index];
11494 switch (operand_type) {
11497 VALUE op = ibf_load_small_value(load, &reading_pos);
11498 VALUE v = ibf_load_object(load, op);
11499 code[code_index] = v;
11502 ISEQ_MBITS_SET(mark_offset_bits, code_index);
11503 needs_bitmap =
true;
11509 VALUE op = ibf_load_small_value(load, &reading_pos);
11510 VALUE v = ibf_load_object(load, op);
11511 v = rb_hash_dup(v);
11512 RHASH_TBL_RAW(v)->type = &cdhash_type;
11514 freeze_hide_obj(v);
11519 pinned_list_store(load->current_buffer->obj_list, (
long)op, v);
11521 code[code_index] = v;
11522 ISEQ_MBITS_SET(mark_offset_bits, code_index);
11524 needs_bitmap =
true;
11529 VALUE op = (
VALUE)ibf_load_small_value(load, &reading_pos);
11531 code[code_index] = v;
11534 ISEQ_MBITS_SET(mark_offset_bits, code_index);
11535 needs_bitmap =
true;
11541 VALUE op = ibf_load_small_value(load, &reading_pos);
11542 VALUE arr = ibf_load_object(load, op);
11544 IC ic = &ISEQ_IS_IC_ENTRY(load_body, ic_index++);
11545 ic->
segments = array_to_idlist(arr);
11547 code[code_index] = (
VALUE)ic;
11554 unsigned int op = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11556 ISE ic = ISEQ_IS_ENTRY_START(load_body, operand_type) + op;
11557 code[code_index] = (
VALUE)ic;
11559 if (operand_type == TS_IVC) {
11562 if (insn == BIN(setinstancevariable)) {
11563 ID iv_name = (
ID)code[code_index - 1];
11564 cache->iv_set_name = iv_name;
11567 cache->iv_set_name = 0;
11570 vm_ic_attr_index_initialize(cache, INVALID_SHAPE_ID);
11577 code[code_index] = (
VALUE)cd_entries++;
11582 VALUE op = ibf_load_small_value(load, &reading_pos);
11583 code[code_index] = ibf_load_id(load, (
ID)(
VALUE)op);
11590 code[code_index] = (
VALUE)ibf_load_builtin(load, &reading_pos);
11593 code[code_index] = ibf_load_small_value(load, &reading_pos);
11597 if (insn_len(insn) != op_index+1) {
11602 load_body->iseq_encoded = code;
11603 load_body->iseq_size = code_index;
11605 if (ISEQ_MBITS_BUFLEN(load_body->iseq_size) == 1) {
11606 load_body->mark_bits.single = mark_offset_bits[0];
11609 if (needs_bitmap) {
11610 load_body->mark_bits.list = mark_offset_bits;
11613 load_body->mark_bits.list = 0;
11614 ruby_xfree(mark_offset_bits);
11618 assert(code_index == iseq_size);
11619 assert(reading_pos == bytecode_offset + bytecode_size);
11626 int opt_num = ISEQ_BODY(iseq)->param.opt_num;
11629 IBF_W_ALIGN(
VALUE);
11630 return ibf_dump_write(dump, ISEQ_BODY(iseq)->param.opt_table,
sizeof(
VALUE) * (opt_num + 1));
11633 return ibf_dump_pos(dump);
11638ibf_load_param_opt_table(
const struct ibf_load *load, ibf_offset_t opt_table_offset,
int opt_num)
11642 MEMCPY(table, load->current_buffer->buff + opt_table_offset,
VALUE, opt_num+1);
11653 const struct rb_iseq_param_keyword *kw = ISEQ_BODY(iseq)->param.keyword;
11656 struct rb_iseq_param_keyword dump_kw = *kw;
11657 int dv_num = kw->num - kw->required_num;
11662 for (i=0; i<kw->num; i++) ids[i] = (
ID)ibf_dump_id(dump, kw->table[i]);
11663 for (i=0; i<dv_num; i++) dvs[i] = (
VALUE)ibf_dump_object(dump, kw->default_values[i]);
11665 dump_kw.table = IBF_W(ids,
ID, kw->num);
11666 dump_kw.default_values = IBF_W(dvs,
VALUE, dv_num);
11667 IBF_W_ALIGN(
struct rb_iseq_param_keyword);
11668 return ibf_dump_write(dump, &dump_kw,
sizeof(
struct rb_iseq_param_keyword) * 1);
11675static const struct rb_iseq_param_keyword *
11676ibf_load_param_keyword(
const struct ibf_load *load, ibf_offset_t param_keyword_offset)
11678 if (param_keyword_offset) {
11679 struct rb_iseq_param_keyword *kw = IBF_R(param_keyword_offset,
struct rb_iseq_param_keyword, 1);
11680 ID *ids = IBF_R(kw->table,
ID, kw->num);
11681 int dv_num = kw->num - kw->required_num;
11682 VALUE *dvs = IBF_R(kw->default_values,
VALUE, dv_num);
11685 for (i=0; i<kw->num; i++) {
11686 ids[i] = ibf_load_id(load, ids[i]);
11688 for (i=0; i<dv_num; i++) {
11689 dvs[i] = ibf_load_object(load, dvs[i]);
11693 kw->default_values = dvs;
11704 ibf_offset_t offset = ibf_dump_pos(dump);
11708 for (i = 0; i < ISEQ_BODY(iseq)->insns_info.size; i++) {
11709 ibf_dump_write_small_value(dump, entries[i].line_no);
11710#ifdef USE_ISEQ_NODE_ID
11711 ibf_dump_write_small_value(dump, entries[i].node_id);
11713 ibf_dump_write_small_value(dump, entries[i].events);
11720ibf_load_insns_info_body(
const struct ibf_load *load, ibf_offset_t body_offset,
unsigned int size)
11722 ibf_offset_t reading_pos = body_offset;
11726 for (i = 0; i < size; i++) {
11727 entries[i].line_no = (int)ibf_load_small_value(load, &reading_pos);
11728#ifdef USE_ISEQ_NODE_ID
11729 entries[i].node_id = (int)ibf_load_small_value(load, &reading_pos);
11731 entries[i].events = (
rb_event_flag_t)ibf_load_small_value(load, &reading_pos);
11738ibf_dump_insns_info_positions(
struct ibf_dump *dump,
const unsigned int *positions,
unsigned int size)
11740 ibf_offset_t offset = ibf_dump_pos(dump);
11742 unsigned int last = 0;
11744 for (i = 0; i < size; i++) {
11745 ibf_dump_write_small_value(dump, positions[i] - last);
11746 last = positions[i];
11752static unsigned int *
11753ibf_load_insns_info_positions(
const struct ibf_load *load, ibf_offset_t positions_offset,
unsigned int size)
11755 ibf_offset_t reading_pos = positions_offset;
11756 unsigned int *positions =
ALLOC_N(
unsigned int, size);
11758 unsigned int last = 0;
11760 for (i = 0; i < size; i++) {
11761 positions[i] = last + (
unsigned int)ibf_load_small_value(load, &reading_pos);
11762 last = positions[i];
11772 const int size = body->local_table_size;
11776 for (i=0; i<size; i++) {
11777 table[i] = ibf_dump_id(dump, body->local_table[i]);
11781 return ibf_dump_write(dump, table,
sizeof(
ID) * size);
11785ibf_load_local_table(
const struct ibf_load *load, ibf_offset_t local_table_offset,
int size)
11788 ID *table = IBF_R(local_table_offset,
ID, size);
11791 for (i=0; i<size; i++) {
11792 table[i] = ibf_load_id(load, table[i]);
11807 int *iseq_indices =
ALLOCA_N(
int, table->size);
11810 for (i=0; i<table->size; i++) {
11811 iseq_indices[i] = ibf_dump_iseq(dump, table->entries[i].iseq);
11814 const ibf_offset_t offset = ibf_dump_pos(dump);
11816 for (i=0; i<table->size; i++) {
11817 ibf_dump_write_small_value(dump, iseq_indices[i]);
11818 ibf_dump_write_small_value(dump, table->entries[i].type);
11819 ibf_dump_write_small_value(dump, table->entries[i].start);
11820 ibf_dump_write_small_value(dump, table->entries[i].end);
11821 ibf_dump_write_small_value(dump, table->entries[i].cont);
11822 ibf_dump_write_small_value(dump, table->entries[i].sp);
11827 return ibf_dump_pos(dump);
11832ibf_load_catch_table(
const struct ibf_load *load, ibf_offset_t catch_table_offset,
unsigned int size)
11835 struct iseq_catch_table *table = ruby_xmalloc(iseq_catch_table_bytes(size));
11836 table->size = size;
11838 ibf_offset_t reading_pos = catch_table_offset;
11841 for (i=0; i<table->size; i++) {
11842 int iseq_index = (int)ibf_load_small_value(load, &reading_pos);
11843 table->entries[i].type = (
enum rb_catch_type)ibf_load_small_value(load, &reading_pos);
11844 table->entries[i].start = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11845 table->entries[i].end = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11846 table->entries[i].cont = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11847 table->entries[i].sp = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11849 table->entries[i].iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)iseq_index);
11862 const unsigned int ci_size = body->ci_size;
11865 ibf_offset_t offset = ibf_dump_pos(dump);
11869 for (i = 0; i < ci_size; i++) {
11872 ibf_dump_write_small_value(dump, ibf_dump_id(dump, vm_ci_mid(ci)));
11873 ibf_dump_write_small_value(dump, vm_ci_flag(ci));
11874 ibf_dump_write_small_value(dump, vm_ci_argc(ci));
11878 int len = kwarg->keyword_len;
11879 ibf_dump_write_small_value(dump, len);
11880 for (
int j=0; j<len; j++) {
11881 VALUE keyword = ibf_dump_object(dump, kwarg->keywords[j]);
11882 ibf_dump_write_small_value(dump, keyword);
11886 ibf_dump_write_small_value(dump, 0);
11891 ibf_dump_write_small_value(dump, (
VALUE)-1);
11898static enum rb_id_table_iterator_result
11899dump_outer_variable(
ID id,
VALUE val,
void *dump)
11901 ibf_dump_write_small_value(dump, ibf_dump_id(dump,
id));
11902 ibf_dump_write_small_value(dump, val);
11904 return ID_TABLE_CONTINUE;
11910 struct rb_id_table * ovs = ISEQ_BODY(iseq)->outer_variables;
11912 ibf_offset_t offset = ibf_dump_pos(dump);
11915 ibf_dump_write_small_value(dump, (
VALUE)rb_id_table_size(ovs));
11916 rb_id_table_foreach(ovs, dump_outer_variable, (
void *)dump);
11919 ibf_dump_write_small_value(dump, (
VALUE)0);
11927ibf_load_ci_entries(
const struct ibf_load *load,
11928 ibf_offset_t ci_entries_offset,
11929 unsigned int ci_size,
11932 ibf_offset_t reading_pos = ci_entries_offset;
11939 for (i = 0; i < ci_size; i++) {
11940 VALUE mid_index = ibf_load_small_value(load, &reading_pos);
11941 if (mid_index != (
VALUE)-1) {
11942 ID mid = ibf_load_id(load, mid_index);
11943 unsigned int flag = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11944 unsigned int argc = (
unsigned int)ibf_load_small_value(load, &reading_pos);
11947 int kwlen = (int)ibf_load_small_value(load, &reading_pos);
11950 kwarg->keyword_len = kwlen;
11951 for (
int j=0; j<kwlen; j++) {
11952 VALUE keyword = ibf_load_small_value(load, &reading_pos);
11953 kwarg->keywords[j] = ibf_load_object(load, keyword);
11957 cds[i].ci = vm_ci_new(mid, flag, argc, kwarg);
11959 cds[i].cc = vm_cc_empty();
11970ibf_load_outer_variables(
const struct ibf_load * load, ibf_offset_t outer_variables_offset)
11972 ibf_offset_t reading_pos = outer_variables_offset;
11976 size_t table_size = (size_t)ibf_load_small_value(load, &reading_pos);
11978 if (table_size > 0) {
11979 tbl = rb_id_table_create(table_size);
11982 for (
size_t i = 0; i < table_size; i++) {
11983 ID key = ibf_load_id(load, (
ID)ibf_load_small_value(load, &reading_pos));
11984 VALUE value = ibf_load_small_value(load, &reading_pos);
11985 if (!key) key = rb_make_temporary_id(i);
11986 rb_id_table_insert(tbl, key, value);
11995 assert(dump->current_buffer == &dump->global_buffer);
11997 unsigned int *positions;
12001 const VALUE location_pathobj_index = ibf_dump_object(dump, body->location.pathobj);
12002 const VALUE location_base_label_index = ibf_dump_object(dump, body->location.base_label);
12003 const VALUE location_label_index = ibf_dump_object(dump, body->location.label);
12005#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12006 ibf_offset_t iseq_start = ibf_dump_pos(dump);
12011 buffer.obj_table = ibf_dump_object_table_new();
12012 dump->current_buffer = &buffer;
12015 const ibf_offset_t bytecode_offset = ibf_dump_code(dump, iseq);
12016 const ibf_offset_t bytecode_size = ibf_dump_pos(dump) - bytecode_offset;
12017 const ibf_offset_t param_opt_table_offset = ibf_dump_param_opt_table(dump, iseq);
12018 const ibf_offset_t param_keyword_offset = ibf_dump_param_keyword(dump, iseq);
12019 const ibf_offset_t insns_info_body_offset = ibf_dump_insns_info_body(dump, iseq);
12021 positions = rb_iseq_insns_info_decode_positions(ISEQ_BODY(iseq));
12022 const ibf_offset_t insns_info_positions_offset = ibf_dump_insns_info_positions(dump, positions, body->insns_info.size);
12023 ruby_xfree(positions);
12025 const ibf_offset_t local_table_offset = ibf_dump_local_table(dump, iseq);
12026 const unsigned int catch_table_size = body->catch_table ? body->catch_table->size : 0;
12027 const ibf_offset_t catch_table_offset = ibf_dump_catch_table(dump, iseq);
12028 const int parent_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->parent_iseq);
12029 const int local_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->local_iseq);
12030 const int mandatory_only_iseq_index = ibf_dump_iseq(dump, ISEQ_BODY(iseq)->mandatory_only_iseq);
12031 const ibf_offset_t ci_entries_offset = ibf_dump_ci_entries(dump, iseq);
12032 const ibf_offset_t outer_variables_offset = ibf_dump_outer_variables(dump, iseq);
12034#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12035 ibf_offset_t local_obj_list_offset;
12036 unsigned int local_obj_list_size;
12038 ibf_dump_object_list(dump, &local_obj_list_offset, &local_obj_list_size);
12041 ibf_offset_t body_offset = ibf_dump_pos(dump);
12044 unsigned int param_flags =
12045 (body->
param.flags.has_lead << 0) |
12046 (body->
param.flags.has_opt << 1) |
12047 (body->
param.flags.has_rest << 2) |
12048 (body->
param.flags.has_post << 3) |
12049 (body->
param.flags.has_kw << 4) |
12050 (body->
param.flags.has_kwrest << 5) |
12051 (body->
param.flags.has_block << 6) |
12052 (body->
param.flags.ambiguous_param0 << 7) |
12053 (body->
param.flags.accepts_no_kwarg << 8) |
12054 (body->
param.flags.ruby2_keywords << 9);
12056#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12057# define IBF_BODY_OFFSET(x) (x)
12059# define IBF_BODY_OFFSET(x) (body_offset - (x))
12062 ibf_dump_write_small_value(dump, body->type);
12063 ibf_dump_write_small_value(dump, body->iseq_size);
12064 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(bytecode_offset));
12065 ibf_dump_write_small_value(dump, bytecode_size);
12066 ibf_dump_write_small_value(dump, param_flags);
12067 ibf_dump_write_small_value(dump, body->
param.size);
12068 ibf_dump_write_small_value(dump, body->
param.lead_num);
12069 ibf_dump_write_small_value(dump, body->
param.opt_num);
12070 ibf_dump_write_small_value(dump, body->
param.rest_start);
12071 ibf_dump_write_small_value(dump, body->
param.post_start);
12072 ibf_dump_write_small_value(dump, body->
param.post_num);
12073 ibf_dump_write_small_value(dump, body->
param.block_start);
12074 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(param_opt_table_offset));
12075 ibf_dump_write_small_value(dump, param_keyword_offset);
12076 ibf_dump_write_small_value(dump, location_pathobj_index);
12077 ibf_dump_write_small_value(dump, location_base_label_index);
12078 ibf_dump_write_small_value(dump, location_label_index);
12079 ibf_dump_write_small_value(dump, body->location.first_lineno);
12080 ibf_dump_write_small_value(dump, body->location.node_id);
12081 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.lineno);
12082 ibf_dump_write_small_value(dump, body->location.code_location.beg_pos.column);
12083 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.lineno);
12084 ibf_dump_write_small_value(dump, body->location.code_location.end_pos.column);
12085 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_body_offset));
12086 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(insns_info_positions_offset));
12087 ibf_dump_write_small_value(dump, body->insns_info.size);
12088 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(local_table_offset));
12089 ibf_dump_write_small_value(dump, catch_table_size);
12090 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(catch_table_offset));
12091 ibf_dump_write_small_value(dump, parent_iseq_index);
12092 ibf_dump_write_small_value(dump, local_iseq_index);
12093 ibf_dump_write_small_value(dump, mandatory_only_iseq_index);
12094 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(ci_entries_offset));
12095 ibf_dump_write_small_value(dump, IBF_BODY_OFFSET(outer_variables_offset));
12096 ibf_dump_write_small_value(dump, body->variable.flip_count);
12097 ibf_dump_write_small_value(dump, body->local_table_size);
12098 ibf_dump_write_small_value(dump, body->ivc_size);
12099 ibf_dump_write_small_value(dump, body->icvarc_size);
12100 ibf_dump_write_small_value(dump, body->ise_size);
12101 ibf_dump_write_small_value(dump, body->ic_size);
12102 ibf_dump_write_small_value(dump, body->ci_size);
12103 ibf_dump_write_small_value(dump, body->stack_max);
12104 ibf_dump_write_small_value(dump, body->catch_except_p);
12105 ibf_dump_write_small_value(dump, body->builtin_inline_p);
12107#undef IBF_BODY_OFFSET
12109#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12110 ibf_offset_t iseq_length_bytes = ibf_dump_pos(dump);
12112 dump->current_buffer = saved_buffer;
12113 ibf_dump_write(dump,
RSTRING_PTR(buffer.str), iseq_length_bytes);
12115 ibf_offset_t offset = ibf_dump_pos(dump);
12116 ibf_dump_write_small_value(dump, iseq_start);
12117 ibf_dump_write_small_value(dump, iseq_length_bytes);
12118 ibf_dump_write_small_value(dump, body_offset);
12120 ibf_dump_write_small_value(dump, local_obj_list_offset);
12121 ibf_dump_write_small_value(dump, local_obj_list_size);
12123 st_free_table(buffer.obj_table);
12127 return body_offset;
12132ibf_load_location_str(
const struct ibf_load *load,
VALUE str_index)
12134 VALUE str = ibf_load_object(load, str_index);
12136 str = rb_fstring(str);
12146 ibf_offset_t reading_pos = offset;
12148#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12150 load->current_buffer = &load->global_buffer;
12152 const ibf_offset_t iseq_start = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12153 const ibf_offset_t iseq_length_bytes = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12154 const ibf_offset_t body_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12157 buffer.buff = load->global_buffer.buff + iseq_start;
12158 buffer.size = iseq_length_bytes;
12159 buffer.obj_list_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12160 buffer.obj_list_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12161 buffer.obj_list = pinned_list_new(buffer.obj_list_size);
12163 load->current_buffer = &buffer;
12164 reading_pos = body_offset;
12167#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12168# define IBF_BODY_OFFSET(x) (x)
12170# define IBF_BODY_OFFSET(x) (offset - (x))
12173 const unsigned int type = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12174 const unsigned int iseq_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12175 const ibf_offset_t bytecode_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12176 const ibf_offset_t bytecode_size = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12177 const unsigned int param_flags = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12178 const unsigned int param_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12179 const int param_lead_num = (int)ibf_load_small_value(load, &reading_pos);
12180 const int param_opt_num = (int)ibf_load_small_value(load, &reading_pos);
12181 const int param_rest_start = (int)ibf_load_small_value(load, &reading_pos);
12182 const int param_post_start = (int)ibf_load_small_value(load, &reading_pos);
12183 const int param_post_num = (int)ibf_load_small_value(load, &reading_pos);
12184 const int param_block_start = (int)ibf_load_small_value(load, &reading_pos);
12185 const ibf_offset_t param_opt_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12186 const ibf_offset_t param_keyword_offset = (ibf_offset_t)ibf_load_small_value(load, &reading_pos);
12187 const VALUE location_pathobj_index = ibf_load_small_value(load, &reading_pos);
12188 const VALUE location_base_label_index = ibf_load_small_value(load, &reading_pos);
12189 const VALUE location_label_index = ibf_load_small_value(load, &reading_pos);
12190 const int location_first_lineno = (int)ibf_load_small_value(load, &reading_pos);
12191 const int location_node_id = (int)ibf_load_small_value(load, &reading_pos);
12192 const int location_code_location_beg_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
12193 const int location_code_location_beg_pos_column = (int)ibf_load_small_value(load, &reading_pos);
12194 const int location_code_location_end_pos_lineno = (int)ibf_load_small_value(load, &reading_pos);
12195 const int location_code_location_end_pos_column = (int)ibf_load_small_value(load, &reading_pos);
12196 const ibf_offset_t insns_info_body_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12197 const ibf_offset_t insns_info_positions_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12198 const unsigned int insns_info_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12199 const ibf_offset_t local_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12200 const unsigned int catch_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12201 const ibf_offset_t catch_table_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12202 const int parent_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12203 const int local_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12204 const int mandatory_only_iseq_index = (int)ibf_load_small_value(load, &reading_pos);
12205 const ibf_offset_t ci_entries_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12206 const ibf_offset_t outer_variables_offset = (ibf_offset_t)IBF_BODY_OFFSET(ibf_load_small_value(load, &reading_pos));
12207 const rb_snum_t variable_flip_count = (rb_snum_t)ibf_load_small_value(load, &reading_pos);
12208 const unsigned int local_table_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12210 const unsigned int ivc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12211 const unsigned int icvarc_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12212 const unsigned int ise_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12213 const unsigned int ic_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12215 const unsigned int ci_size = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12216 const unsigned int stack_max = (
unsigned int)ibf_load_small_value(load, &reading_pos);
12217 const char catch_except_p = (char)ibf_load_small_value(load, &reading_pos);
12218 const bool builtin_inline_p = (bool)ibf_load_small_value(load, &reading_pos);
12221 VALUE path = ibf_load_object(load, location_pathobj_index);
12226 realpath = path = rb_fstring(path);
12229 VALUE pathobj = path;
12235 if (!
NIL_P(realpath)) {
12238 "(%x), path=%+"PRIsVALUE,
12239 realpath,
TYPE(realpath), path);
12241 realpath = rb_fstring(realpath);
12247 rb_iseq_pathobj_set(iseq, path, realpath);
12252 VALUE dummy_frame = rb_vm_push_frame_fname(ec, path);
12254#undef IBF_BODY_OFFSET
12256 load_body->type =
type;
12257 load_body->stack_max = stack_max;
12258 load_body->
param.flags.has_lead = (param_flags >> 0) & 1;
12259 load_body->
param.flags.has_opt = (param_flags >> 1) & 1;
12260 load_body->
param.flags.has_rest = (param_flags >> 2) & 1;
12261 load_body->
param.flags.has_post = (param_flags >> 3) & 1;
12262 load_body->
param.flags.has_kw = FALSE;
12263 load_body->
param.flags.has_kwrest = (param_flags >> 5) & 1;
12264 load_body->
param.flags.has_block = (param_flags >> 6) & 1;
12265 load_body->
param.flags.ambiguous_param0 = (param_flags >> 7) & 1;
12266 load_body->
param.flags.accepts_no_kwarg = (param_flags >> 8) & 1;
12267 load_body->
param.flags.ruby2_keywords = (param_flags >> 9) & 1;
12268 load_body->
param.size = param_size;
12269 load_body->
param.lead_num = param_lead_num;
12270 load_body->
param.opt_num = param_opt_num;
12271 load_body->
param.rest_start = param_rest_start;
12272 load_body->
param.post_start = param_post_start;
12273 load_body->
param.post_num = param_post_num;
12274 load_body->
param.block_start = param_block_start;
12275 load_body->local_table_size = local_table_size;
12276 load_body->ci_size = ci_size;
12277 load_body->insns_info.size = insns_info_size;
12279 ISEQ_COVERAGE_SET(iseq,
Qnil);
12280 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
12281 load_body->variable.flip_count = variable_flip_count;
12282 load_body->variable.script_lines =
Qnil;
12284 load_body->location.first_lineno = location_first_lineno;
12285 load_body->location.node_id = location_node_id;
12286 load_body->location.code_location.beg_pos.lineno = location_code_location_beg_pos_lineno;
12287 load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
12288 load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
12289 load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
12290 load_body->catch_except_p = catch_except_p;
12291 load_body->builtin_inline_p = builtin_inline_p;
12293 load_body->ivc_size = ivc_size;
12294 load_body->icvarc_size = icvarc_size;
12295 load_body->ise_size = ise_size;
12296 load_body->ic_size = ic_size;
12298 ibf_load_ci_entries(load, ci_entries_offset, ci_size, &load_body->call_data);
12299 load_body->outer_variables = ibf_load_outer_variables(load, outer_variables_offset);
12300 load_body->
param.opt_table = ibf_load_param_opt_table(load, param_opt_table_offset, param_opt_num);
12301 load_body->
param.keyword = ibf_load_param_keyword(load, param_keyword_offset);
12302 load_body->
param.flags.has_kw = (param_flags >> 4) & 1;
12303 load_body->insns_info.body = ibf_load_insns_info_body(load, insns_info_body_offset, insns_info_size);
12304 load_body->insns_info.positions = ibf_load_insns_info_positions(load, insns_info_positions_offset, insns_info_size);
12305 load_body->local_table = ibf_load_local_table(load, local_table_offset, local_table_size);
12306 load_body->catch_table = ibf_load_catch_table(load, catch_table_offset, catch_table_size);
12307 load_body->parent_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)parent_iseq_index);
12308 load_body->local_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)local_iseq_index);
12309 load_body->mandatory_only_iseq = ibf_load_iseq(load, (
const rb_iseq_t *)(
VALUE)mandatory_only_iseq_index);
12311 ibf_load_code(load, iseq, bytecode_offset, bytecode_size, iseq_size);
12312#if VM_INSN_INFO_TABLE_IMPL == 2
12313 rb_iseq_insns_info_encode_positions(iseq);
12316 rb_iseq_translate_threaded_code(iseq);
12318#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12319 load->current_buffer = &load->global_buffer;
12322 RB_OBJ_WRITE(iseq, &load_body->location.base_label, ibf_load_location_str(load, location_base_label_index));
12323 RB_OBJ_WRITE(iseq, &load_body->location.label, ibf_load_location_str(load, location_label_index));
12325#if IBF_ISEQ_ENABLE_LOCAL_BUFFER
12326 load->current_buffer = saved_buffer;
12328 verify_call_cache(iseq);
12331 rb_vm_pop_frame_no_int(ec);
12341ibf_dump_iseq_list_i(st_data_t key, st_data_t val, st_data_t ptr)
12346 ibf_offset_t offset = ibf_dump_iseq_each(args->dump, iseq);
12347 rb_ary_push(args->offset_list,
UINT2NUM(offset));
12349 return ST_CONTINUE;
12355 VALUE offset_list = rb_ary_hidden_new(dump->iseq_table->num_entries);
12359 args.offset_list = offset_list;
12361 st_foreach(dump->iseq_table, ibf_dump_iseq_list_i, (st_data_t)&args);
12364 st_index_t size = dump->iseq_table->num_entries;
12365 ibf_offset_t *offsets =
ALLOCA_N(ibf_offset_t, size);
12367 for (i = 0; i < size; i++) {
12371 ibf_dump_align(dump,
sizeof(ibf_offset_t));
12372 header->iseq_list_offset = ibf_dump_write(dump, offsets,
sizeof(ibf_offset_t) * size);
12373 header->iseq_list_size = (
unsigned int)size;
12376#define IBF_OBJECT_INTERNAL FL_PROMOTED0
12385 unsigned int type: 5;
12386 unsigned int special_const: 1;
12387 unsigned int frozen: 1;
12388 unsigned int internal: 1;
12391enum ibf_object_class_index {
12392 IBF_OBJECT_CLASS_OBJECT,
12393 IBF_OBJECT_CLASS_ARRAY,
12394 IBF_OBJECT_CLASS_STANDARD_ERROR,
12395 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR,
12396 IBF_OBJECT_CLASS_TYPE_ERROR,
12397 IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR,
12407 long keyval[FLEX_ARY_LEN];
12420 BDIGIT digits[FLEX_ARY_LEN];
12423enum ibf_object_data_type {
12424 IBF_OBJECT_DATA_ENCODING,
12435#define IBF_ALIGNED_OFFSET(align, offset) \
12436 ((((offset) - 1) / (align) + 1) * (align))
12437#define IBF_OBJBODY(type, offset) (const type *)\
12438 ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
12441ibf_load_check_offset(
const struct ibf_load *load,
size_t offset)
12443 if (offset >= load->current_buffer->size) {
12446 return load->current_buffer->buff + offset;
12449NORETURN(
static void ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj));
12452ibf_dump_object_unsupported(
struct ibf_dump *dump,
VALUE obj)
12455 rb_raw_obj_info(buff,
sizeof(buff), obj);
12471 enum ibf_object_class_index cindex;
12472 if (obj == rb_cObject) {
12473 cindex = IBF_OBJECT_CLASS_OBJECT;
12476 cindex = IBF_OBJECT_CLASS_ARRAY;
12479 cindex = IBF_OBJECT_CLASS_STANDARD_ERROR;
12482 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR;
12485 cindex = IBF_OBJECT_CLASS_TYPE_ERROR;
12488 cindex = IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR;
12491 rb_obj_info_dump(obj);
12493 rb_bug(
"unsupported class");
12495 ibf_dump_write_small_value(dump, (
VALUE)cindex);
12501 enum ibf_object_class_index cindex = (
enum ibf_object_class_index)ibf_load_small_value(load, &offset);
12504 case IBF_OBJECT_CLASS_OBJECT:
12506 case IBF_OBJECT_CLASS_ARRAY:
12508 case IBF_OBJECT_CLASS_STANDARD_ERROR:
12510 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_ERROR:
12512 case IBF_OBJECT_CLASS_TYPE_ERROR:
12514 case IBF_OBJECT_CLASS_NO_MATCHING_PATTERN_KEY_ERROR:
12526 (void)IBF_W(&dbl,
double, 1);
12532 const double *dblp = IBF_OBJBODY(
double, offset);
12539 long encindex = (long)rb_enc_get_index(obj);
12543 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
12544 rb_encoding *enc = rb_enc_from_index((
int)encindex);
12546 encindex = RUBY_ENCINDEX_BUILTIN_MAX + ibf_dump_object(dump,
rb_str_new2(enc_name));
12549 ibf_dump_write_small_value(dump, encindex);
12550 ibf_dump_write_small_value(dump, len);
12551 IBF_WP(ptr,
char, len);
12557 ibf_offset_t reading_pos = offset;
12559 int encindex = (int)ibf_load_small_value(load, &reading_pos);
12560 const long len = (long)ibf_load_small_value(load, &reading_pos);
12561 const char *ptr = load->current_buffer->buff + reading_pos;
12563 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
12564 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
12565 encindex = rb_enc_find_index(
RSTRING_PTR(enc_name_str));
12569 if (header->frozen && !header->internal) {
12576 if (header->frozen) str = rb_fstring(str);
12587 regexp.srcstr = (long)ibf_dump_object(dump, srcstr);
12589 ibf_dump_write_byte(dump, (
unsigned char)regexp.option);
12590 ibf_dump_write_small_value(dump, regexp.srcstr);
12597 regexp.option = ibf_load_byte(load, &offset);
12598 regexp.srcstr = ibf_load_small_value(load, &offset);
12600 VALUE srcstr = ibf_load_object(load, regexp.srcstr);
12601 VALUE reg = rb_reg_compile(srcstr, (
int)regexp.option, NULL, 0);
12613 ibf_dump_write_small_value(dump, len);
12614 for (i=0; i<len; i++) {
12615 long index = (long)ibf_dump_object(dump,
RARRAY_AREF(obj, i));
12616 ibf_dump_write_small_value(dump, index);
12623 ibf_offset_t reading_pos = offset;
12625 const long len = (long)ibf_load_small_value(load, &reading_pos);
12627 VALUE ary = header->internal ? rb_ary_hidden_new(len) : rb_ary_new_capa(len);
12630 for (i=0; i<len; i++) {
12631 const VALUE index = ibf_load_small_value(load, &reading_pos);
12632 rb_ary_push(ary, ibf_load_object(load, index));
12641ibf_dump_object_hash_i(st_data_t key, st_data_t val, st_data_t ptr)
12645 VALUE key_index = ibf_dump_object(dump, (
VALUE)key);
12646 VALUE val_index = ibf_dump_object(dump, (
VALUE)val);
12648 ibf_dump_write_small_value(dump, key_index);
12649 ibf_dump_write_small_value(dump, val_index);
12650 return ST_CONTINUE;
12657 ibf_dump_write_small_value(dump, (
VALUE)len);
12665 long len = (long)ibf_load_small_value(load, &offset);
12666 VALUE obj = rb_hash_new_with_size(len);
12669 for (i = 0; i < len; i++) {
12670 VALUE key_index = ibf_load_small_value(load, &offset);
12671 VALUE val_index = ibf_load_small_value(load, &offset);
12673 VALUE key = ibf_load_object(load, key_index);
12674 VALUE val = ibf_load_object(load, val_index);
12675 rb_hash_aset(obj, key, val);
12677 rb_hash_rehash(obj);
12693 range.class_index = 0;
12696 range.beg = (long)ibf_dump_object(dump, beg);
12697 range.end = (long)ibf_dump_object(dump, end);
12712 VALUE beg = ibf_load_object(load, range->beg);
12713 VALUE end = ibf_load_object(load, range->end);
12723 ssize_t len = BIGNUM_LEN(obj);
12724 ssize_t slen = BIGNUM_SIGN(obj) > 0 ? len : len * -1;
12725 BDIGIT *d = BIGNUM_DIGITS(obj);
12727 (void)IBF_W(&slen, ssize_t, 1);
12728 IBF_WP(d, BDIGIT, len);
12735 int sign = bignum->slen > 0;
12736 ssize_t len = sign > 0 ? bignum->slen : -1 * bignum->slen;
12737 VALUE obj = rb_integer_unpack(bignum->digits, len * 2, 2, 0,
12747 if (rb_data_is_encoding(obj)) {
12750 long len = strlen(name) + 1;
12752 data[0] = IBF_OBJECT_DATA_ENCODING;
12754 (void)IBF_W(data,
long, 2);
12755 IBF_WP(name,
char, len);
12758 ibf_dump_object_unsupported(dump, obj);
12765 const long *body = IBF_OBJBODY(
long, offset);
12766 const enum ibf_object_data_type
type = (
enum ibf_object_data_type)body[0];
12768 const char *data = (
const char *)&body[2];
12771 case IBF_OBJECT_DATA_ENCODING:
12773 VALUE encobj = rb_enc_from_encoding(rb_enc_find(data));
12778 return ibf_load_object_unsupported(load, header, offset);
12782ibf_dump_object_complex_rational(
struct ibf_dump *dump,
VALUE obj)
12785 data[0] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->real);
12786 data[1] = (long)ibf_dump_object(dump, RCOMPLEX(obj)->imag);
12788 (void)IBF_W(data,
long, 2);
12792ibf_load_object_complex_rational(
const struct ibf_load *load,
const struct ibf_object_header *header, ibf_offset_t offset)
12795 VALUE a = ibf_load_object(load, nums->a);
12796 VALUE b = ibf_load_object(load, nums->b);
12808 ibf_dump_object_string(dump,
rb_sym2str(obj));
12814 ibf_offset_t reading_pos = offset;
12816 int encindex = (int)ibf_load_small_value(load, &reading_pos);
12817 const long len = (long)ibf_load_small_value(load, &reading_pos);
12818 const char *ptr = load->current_buffer->buff + reading_pos;
12820 if (encindex > RUBY_ENCINDEX_BUILTIN_MAX) {
12821 VALUE enc_name_str = ibf_load_object(load, encindex - RUBY_ENCINDEX_BUILTIN_MAX);
12822 encindex = rb_enc_find_index(
RSTRING_PTR(enc_name_str));
12825 ID id =
rb_intern3(ptr, len, rb_enc_from_index(encindex));
12829typedef void (*ibf_dump_object_function)(
struct ibf_dump *dump,
VALUE obj);
12830static ibf_dump_object_function dump_object_functions[
RUBY_T_MASK+1] = {
12831 ibf_dump_object_unsupported,
12832 ibf_dump_object_unsupported,
12833 ibf_dump_object_class,
12834 ibf_dump_object_unsupported,
12835 ibf_dump_object_float,
12836 ibf_dump_object_string,
12837 ibf_dump_object_regexp,
12838 ibf_dump_object_array,
12839 ibf_dump_object_hash,
12840 ibf_dump_object_struct,
12841 ibf_dump_object_bignum,
12842 ibf_dump_object_unsupported,
12843 ibf_dump_object_data,
12844 ibf_dump_object_unsupported,
12845 ibf_dump_object_complex_rational,
12846 ibf_dump_object_complex_rational,
12847 ibf_dump_object_unsupported,
12848 ibf_dump_object_unsupported,
12849 ibf_dump_object_unsupported,
12850 ibf_dump_object_unsupported,
12851 ibf_dump_object_symbol,
12852 ibf_dump_object_unsupported,
12853 ibf_dump_object_unsupported,
12854 ibf_dump_object_unsupported,
12855 ibf_dump_object_unsupported,
12856 ibf_dump_object_unsupported,
12857 ibf_dump_object_unsupported,
12858 ibf_dump_object_unsupported,
12859 ibf_dump_object_unsupported,
12860 ibf_dump_object_unsupported,
12861 ibf_dump_object_unsupported,
12862 ibf_dump_object_unsupported,
12868 unsigned char byte =
12869 (header.type << 0) |
12870 (header.special_const << 5) |
12871 (header.frozen << 6) |
12872 (header.internal << 7);
12878ibf_load_object_object_header(const struct
ibf_load *load, ibf_offset_t *offset)
12880 unsigned char byte = ibf_load_byte(load, offset);
12883 header.type = (
byte >> 0) & 0x1f;
12884 header.special_const = (
byte >> 5) & 0x01;
12885 header.frozen = (
byte >> 6) & 0x01;
12886 header.internal = (
byte >> 7) & 0x01;
12895 ibf_offset_t current_offset;
12896 IBF_ZERO(obj_header);
12897 obj_header.type =
TYPE(obj);
12899 IBF_W_ALIGN(ibf_offset_t);
12900 current_offset = ibf_dump_pos(dump);
12905 obj_header.special_const = TRUE;
12906 obj_header.frozen = TRUE;
12907 obj_header.internal = TRUE;
12908 ibf_dump_object_object_header(dump, obj_header);
12909 ibf_dump_write_small_value(dump, obj);
12913 obj_header.special_const = FALSE;
12915 ibf_dump_object_object_header(dump, obj_header);
12916 (*dump_object_functions[obj_header.type])(dump, obj);
12919 return current_offset;
12923static ibf_load_object_function load_object_functions[
RUBY_T_MASK+1] = {
12924 ibf_load_object_unsupported,
12925 ibf_load_object_unsupported,
12926 ibf_load_object_class,
12927 ibf_load_object_unsupported,
12928 ibf_load_object_float,
12929 ibf_load_object_string,
12930 ibf_load_object_regexp,
12931 ibf_load_object_array,
12932 ibf_load_object_hash,
12933 ibf_load_object_struct,
12934 ibf_load_object_bignum,
12935 ibf_load_object_unsupported,
12936 ibf_load_object_data,
12937 ibf_load_object_unsupported,
12938 ibf_load_object_complex_rational,
12939 ibf_load_object_complex_rational,
12940 ibf_load_object_unsupported,
12941 ibf_load_object_unsupported,
12942 ibf_load_object_unsupported,
12943 ibf_load_object_unsupported,
12944 ibf_load_object_symbol,
12945 ibf_load_object_unsupported,
12946 ibf_load_object_unsupported,
12947 ibf_load_object_unsupported,
12948 ibf_load_object_unsupported,
12949 ibf_load_object_unsupported,
12950 ibf_load_object_unsupported,
12951 ibf_load_object_unsupported,
12952 ibf_load_object_unsupported,
12953 ibf_load_object_unsupported,
12954 ibf_load_object_unsupported,
12955 ibf_load_object_unsupported,
12959ibf_load_object(
const struct ibf_load *load,
VALUE object_index)
12961 if (object_index == 0) {
12965 VALUE obj = pinned_list_fetch(load->current_buffer->obj_list, (
long)object_index);
12967 ibf_offset_t *offsets = (ibf_offset_t *)(load->current_buffer->obj_list_offset + load->current_buffer->buff);
12968 ibf_offset_t offset = offsets[object_index];
12969 const struct ibf_object_header header = ibf_load_object_object_header(load, &offset);
12972 fprintf(stderr,
"ibf_load_object: list=%#x offsets=%p offset=%#x\n",
12973 load->current_buffer->obj_list_offset, (
void *)offsets, offset);
12974 fprintf(stderr,
"ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n",
12975 header.type, header.special_const, header.frozen, header.internal);
12977 if (offset >= load->current_buffer->size) {
12981 if (header.special_const) {
12982 ibf_offset_t reading_pos = offset;
12984 obj = ibf_load_small_value(load, &reading_pos);
12987 obj = (*load_object_functions[header.type])(load, &header, offset);
12990 pinned_list_store(load->current_buffer->obj_list, (
long)object_index, obj);
12993 fprintf(stderr,
"ibf_load_object: index=%#"PRIxVALUE
" obj=%#"PRIxVALUE
"\n",
12994 object_index, obj);
13007ibf_dump_object_list_i(st_data_t key, st_data_t val, st_data_t ptr)
13012 ibf_offset_t offset = ibf_dump_object_object(args->dump, obj);
13013 rb_ary_push(args->offset_list,
UINT2NUM(offset));
13015 return ST_CONTINUE;
13019ibf_dump_object_list(
struct ibf_dump *dump, ibf_offset_t *obj_list_offset,
unsigned int *obj_list_size)
13021 st_table *obj_table = dump->current_buffer->obj_table;
13022 VALUE offset_list = rb_ary_hidden_new(obj_table->num_entries);
13026 args.offset_list = offset_list;
13028 st_foreach(obj_table, ibf_dump_object_list_i, (st_data_t)&args);
13030 IBF_W_ALIGN(ibf_offset_t);
13031 *obj_list_offset = ibf_dump_pos(dump);
13033 st_index_t size = obj_table->num_entries;
13036 for (i=0; i<size; i++) {
13041 *obj_list_size = (
unsigned int)size;
13045ibf_dump_mark(
void *ptr)
13048 rb_gc_mark(dump->global_buffer.str);
13050 rb_mark_set(dump->global_buffer.obj_table);
13051 rb_mark_set(dump->iseq_table);
13055ibf_dump_free(
void *ptr)
13058 if (dump->global_buffer.obj_table) {
13059 st_free_table(dump->global_buffer.obj_table);
13060 dump->global_buffer.obj_table = 0;
13062 if (dump->iseq_table) {
13063 st_free_table(dump->iseq_table);
13064 dump->iseq_table = 0;
13070ibf_dump_memsize(
const void *ptr)
13073 size_t size =
sizeof(*dump);
13074 if (dump->iseq_table) size += st_memsize(dump->iseq_table);
13075 if (dump->global_buffer.obj_table) size += st_memsize(dump->global_buffer.obj_table);
13081 {ibf_dump_mark, ibf_dump_free, ibf_dump_memsize,},
13082 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13088 dump->global_buffer.obj_table = NULL;
13089 dump->iseq_table = NULL;
13092 dump->global_buffer.obj_table = ibf_dump_object_table_new();
13093 dump->iseq_table = st_init_numtable();
13095 dump->current_buffer = &dump->global_buffer;
13106 if (ISEQ_BODY(iseq)->parent_iseq != NULL ||
13107 ISEQ_BODY(iseq)->local_iseq != iseq) {
13110 if (
RTEST(ISEQ_COVERAGE(iseq))) {
13115 ibf_dump_setup(dump, dump_obj);
13117 ibf_dump_write(dump, &header,
sizeof(header));
13118 ibf_dump_write(dump, RUBY_PLATFORM, strlen(RUBY_PLATFORM) + 1);
13119 ibf_dump_iseq(dump, iseq);
13121 header.magic[0] =
'Y';
13122 header.magic[1] =
'A';
13123 header.magic[2] =
'R';
13124 header.magic[3] =
'B';
13125 header.major_version = IBF_MAJOR_VERSION;
13126 header.minor_version = IBF_MINOR_VERSION;
13127 ibf_dump_iseq_list(dump, &header);
13128 ibf_dump_object_list(dump, &header.global_object_list_offset, &header.global_object_list_size);
13129 header.size = ibf_dump_pos(dump);
13132 VALUE opt_str = opt;
13135 ibf_dump_write(dump, ptr, header.extra_size);
13138 header.extra_size = 0;
13141 ibf_dump_overwrite(dump, &header,
sizeof(header), 0);
13143 str = dump->global_buffer.str;
13144 ibf_dump_free(dump);
13150static const ibf_offset_t *
13151ibf_iseq_list(
const struct ibf_load *load)
13153 return (
const ibf_offset_t *)(load->global_buffer.buff + load->header->iseq_list_offset);
13157rb_ibf_load_iseq_complete(
rb_iseq_t *iseq)
13161 ibf_offset_t offset = ibf_iseq_list(load)[iseq->aux.loader.index];
13164 fprintf(stderr,
"rb_ibf_load_iseq_complete: index=%#x offset=%#x size=%#x\n",
13165 iseq->aux.loader.index, offset,
13166 load->header->size);
13168 ibf_load_iseq_each(load, iseq, offset);
13169 ISEQ_COMPILE_DATA_CLEAR(iseq);
13171 rb_iseq_init_trace(iseq);
13172 load->iseq = prev_src_iseq;
13179 rb_ibf_load_iseq_complete((
rb_iseq_t *)iseq);
13187 int iseq_index = (int)(
VALUE)index_iseq;
13190 fprintf(stderr,
"ibf_load_iseq: index_iseq=%p iseq_list=%p\n",
13191 (
void *)index_iseq, (
void *)load->iseq_list);
13193 if (iseq_index == -1) {
13197 VALUE iseqv = pinned_list_fetch(load->iseq_list, iseq_index);
13200 fprintf(stderr,
"ibf_load_iseq: iseqv=%p\n", (
void *)iseqv);
13208 fprintf(stderr,
"ibf_load_iseq: new iseq=%p\n", (
void *)iseq);
13211 iseq->aux.loader.obj = load->loader_obj;
13212 iseq->aux.loader.index = iseq_index;
13214 fprintf(stderr,
"ibf_load_iseq: iseq=%p loader_obj=%p index=%d\n",
13215 (
void *)iseq, (
void *)load->loader_obj, iseq_index);
13217 pinned_list_store(load->iseq_list, iseq_index, (
VALUE)iseq);
13221 fprintf(stderr,
"ibf_load_iseq: loading iseq=%p\n", (
void *)iseq);
13223 rb_ibf_load_iseq_complete(iseq);
13225 if (GET_VM()->builtin_function_table) {
13226 rb_ibf_load_iseq_complete(iseq);
13231 fprintf(stderr,
"ibf_load_iseq: iseq=%p loaded %p\n",
13232 (
void *)iseq, (
void *)load->iseq);
13240ibf_load_setup_bytes(
struct ibf_load *load,
VALUE loader_obj,
const char *bytes,
size_t size)
13242 load->loader_obj = loader_obj;
13243 load->global_buffer.buff = bytes;
13244 load->header = (
struct ibf_header *)load->global_buffer.buff;
13245 load->global_buffer.size = load->header->size;
13246 load->global_buffer.obj_list_offset = load->header->global_object_list_offset;
13247 load->global_buffer.obj_list_size = load->header->global_object_list_size;
13248 RB_OBJ_WRITE(loader_obj, &load->iseq_list, pinned_list_new(load->header->iseq_list_size));
13249 RB_OBJ_WRITE(loader_obj, &load->global_buffer.obj_list, pinned_list_new(load->global_buffer.obj_list_size));
13252 load->current_buffer = &load->global_buffer;
13254 if (size < load->header->size) {
13257 if (strncmp(load->header->magic,
"YARB", 4) != 0) {
13260 if (load->header->major_version != IBF_MAJOR_VERSION ||
13261 load->header->minor_version != IBF_MINOR_VERSION) {
13263 load->header->major_version, load->header->minor_version, IBF_MAJOR_VERSION, IBF_MINOR_VERSION);
13265 if (strcmp(load->global_buffer.buff +
sizeof(
struct ibf_header), RUBY_PLATFORM) != 0) {
13268 if (load->header->iseq_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
13270 load->header->iseq_list_offset);
13272 if (load->global_buffer.obj_list_offset %
RUBY_ALIGNOF(ibf_offset_t)) {
13274 load->global_buffer.obj_list_offset);
13294ibf_loader_mark(
void *ptr)
13297 rb_gc_mark(load->str);
13298 rb_gc_mark(load->iseq_list);
13299 rb_gc_mark(load->global_buffer.obj_list);
13303ibf_loader_free(
void *ptr)
13310ibf_loader_memsize(
const void *ptr)
13317 {ibf_loader_mark, ibf_loader_free, ibf_loader_memsize,},
13318 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
13322rb_iseq_ibf_load(
VALUE str)
13328 ibf_load_setup(load, loader_obj, str);
13329 iseq = ibf_load_iseq(load, 0);
13336rb_iseq_ibf_load_bytes(
const char *bytes,
size_t size)
13342 ibf_load_setup_bytes(load, loader_obj, bytes, size);
13343 iseq = ibf_load_iseq(load, 0);
13350rb_iseq_ibf_load_extra_data(
VALUE str)
13356 ibf_load_setup(load, loader_obj, str);
13357 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::@132 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.