21#define PUSH_ADJUST(seq, location, label) \
22 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (int) (location).line))
24#define PUSH_ADJUST_RESTORE(seq, label) \
25 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
27#define PUSH_INSN(seq, location, insn) \
28 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 0))
30#define PUSH_INSN1(seq, location, insn, op1) \
31 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 1, (VALUE)(op1)))
33#define PUSH_INSN2(seq, location, insn, op1, op2) \
34 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
36#define PUSH_INSN3(seq, location, insn, op1, op2, op3) \
37 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
39#define PUSH_INSNL(seq, location, insn, label) \
40 (PUSH_INSN1(seq, location, insn, label), LABEL_REF(label))
42#define PUSH_LABEL(seq, label) \
43 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
45#define PUSH_SEND_R(seq, location, id, argc, block, flag, keywords) \
46 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (int) (location).line, (int) (location).node_id, (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
48#define PUSH_SEND(seq, location, id, argc) \
49 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
51#define PUSH_SEND_WITH_FLAG(seq, location, id, argc, flag) \
52 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)(flag), NULL)
54#define PUSH_SEND_WITH_BLOCK(seq, location, id, argc, block) \
55 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
57#define PUSH_CALL(seq, location, id, argc) \
58 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
60#define PUSH_CALL_WITH_BLOCK(seq, location, id, argc, block) \
61 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
63#define PUSH_TRACE(seq, event) \
64 ADD_ELEM((seq), (LINK_ELEMENT *) new_trace_body(iseq, (event), 0))
66#define PUSH_CATCH_ENTRY(type, ls, le, iseqv, lc) \
67 ADD_CATCH_ENTRY((type), (ls), (le), (iseqv), (lc))
69#define PUSH_SEQ(seq1, seq2) \
70 APPEND_LIST((seq1), (seq2))
72#define PUSH_SYNTHETIC_PUTNIL(seq, iseq) \
74 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line; \
75 if (lineno == 0) lineno = FIX2INT(rb_iseq_first_lineno(iseq)); \
76 ADD_SYNTHETIC_INSN(seq, lineno, -1, putnil); \
85pm_iseq_add_getlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
int line,
int node_id,
int idx,
int level)
87 if (iseq_local_block_param_p(iseq, idx, level)) {
88 ADD_ELEM(seq, (
LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getblockparam), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
91 ADD_ELEM(seq, (
LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getlocal), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
93 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
97pm_iseq_add_setlocal(
rb_iseq_t *iseq,
LINK_ANCHOR *
const seq,
int line,
int node_id,
int idx,
int level)
99 if (iseq_local_block_param_p(iseq, idx, level)) {
100 ADD_ELEM(seq, (
LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setblockparam), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
103 ADD_ELEM(seq, (
LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setlocal), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
105 update_lvar_state(iseq, level, idx);
106 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
109#define PUSH_GETLOCAL(seq, location, idx, level) \
110 pm_iseq_add_getlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
112#define PUSH_SETLOCAL(seq, location, idx, level) \
113 pm_iseq_add_setlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
119#define OLD_ISEQ NEW_ISEQ
122#define NEW_ISEQ(node, name, type, line_no) \
123 pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
125#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
128#define NEW_CHILD_ISEQ(node, name, type, line_no) \
129 pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
131#define PM_COMPILE(node) \
132 pm_compile_node(iseq, (node), ret, popped, scope_node)
134#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
135 pm_compile_node(iseq, (node), _ret, popped, scope_node)
137#define PM_COMPILE_POPPED(node) \
138 pm_compile_node(iseq, (node), ret, true, scope_node)
140#define PM_COMPILE_NOT_POPPED(node) \
141 pm_compile_node(iseq, (node), ret, false, scope_node)
143#define PM_NODE_START_LOCATION(parser, node) \
144 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
146#define PM_NODE_END_LOCATION(parser, node) \
147 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
149#define PM_LOCATION_START_LOCATION(parser, location, id) \
150 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, (location)->start, (parser)->start_line), .node_id = id })
152#define PM_NODE_START_LINE_COLUMN(parser, node) \
153 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line)
155#define PM_NODE_END_LINE_COLUMN(parser, node) \
156 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line)
158#define PM_LOCATION_START_LINE_COLUMN(parser, location) \
159 pm_newline_list_line_column(&(parser)->newline_list, (location)->start, (parser)->start_line)
180 if (integer->
values == NULL) {
185 unsigned char *bytes = (
unsigned char *) RSTRING_PTR(
string);
187 size_t offset = integer->
length * 8;
188 for (
size_t value_index = 0; value_index < integer->
length; value_index++) {
189 uint32_t value = integer->
values[value_index];
191 for (
int index = 0; index < 8; index++) {
192 int byte = (value >> (4 * index)) & 0xf;
193 bytes[--offset] =
byte < 10 ?
byte +
'0' :
byte - 10 +
'a';
201 result =
rb_funcall(result, rb_intern(
"-@"), 0);
213 return parse_integer_value(&node->
value);
248 VALUE imaginary_part;
249 switch (PM_NODE_TYPE(node->
numeric)) {
250 case PM_FLOAT_NODE: {
254 case PM_INTEGER_NODE: {
258 case PM_RATIONAL_NODE: {
263 rb_bug(
"Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->
numeric)));
266 return rb_complex_raw(
INT2FIX(0), imaginary_part);
285 if (node->
flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
286 encoding = rb_ascii8bit_encoding();
288 else if (node->
flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
289 encoding = rb_utf8_encoding();
303 if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
304 encoding = rb_ascii8bit_encoding();
306 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
307 encoding = rb_utf8_encoding();
310 encoding = scope_node->encoding;
316 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
317 int line_number = pm_node_line_number(scope_node->parser, node);
318 value = rb_str_with_debug_created_info(value, rb_iseq_path(iseq), line_number);
328 if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING) {
329 encoding = rb_utf8_encoding();
331 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING) {
332 encoding = rb_ascii8bit_encoding();
334 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING) {
335 encoding = rb_usascii_encoding();
338 encoding = scope_node->encoding;
345pm_optimizable_range_item_p(
const pm_node_t *node)
347 return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
352parse_regexp_error(
rb_iseq_t *iseq, int32_t line_number,
const char *fmt, ...)
356 VALUE error = rb_syntax_error_append(
Qnil, rb_iseq_path(iseq), line_number, -1, NULL,
"%" PRIsVALUE, args);
368 if (explicit_regexp_encoding != NULL) {
369 encoding = explicit_regexp_encoding;
371 else if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
372 encoding = rb_ascii8bit_encoding();
374 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
375 encoding = rb_utf8_encoding();
378 encoding = implicit_regexp_encoding;
382 VALUE error = rb_reg_check_preprocess(
string);
384 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, node),
"%" PRIsVALUE,
rb_obj_as_string(error));
393 for (
size_t index = 0; index < nodes->
size; index++) {
397 switch (PM_NODE_TYPE(part)) {
399 if (implicit_regexp_encoding != NULL) {
401 string = parse_regexp_string_part(iseq, scope_node, part, &((
const pm_string_node_t *) part)->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
404 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
405 VALUE error = rb_reg_check_preprocess(
string);
406 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, part),
"%" PRIsVALUE,
rb_obj_as_string(error));
410 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
413 case PM_INTERPOLATED_STRING_NODE:
414 string = pm_static_literal_concat(iseq, &((
const pm_interpolated_string_node_t *) part)->parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
416 case PM_EMBEDDED_STATEMENTS_NODE: {
418 string = pm_static_literal_concat(iseq, &cast->
statements->
body, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
422 RUBY_ASSERT(
false &&
"unexpected node type in pm_static_literal_concat");
426 if (current !=
Qnil) {
434 return top ? rb_fstring(current) : current;
437#define RE_OPTION_ENCODING_SHIFT 8
438#define RE_OPTION_ENCODING(encoding) (((encoding) & 0xFF) << RE_OPTION_ENCODING_SHIFT)
439#define ARG_ENCODING_NONE 32
440#define ARG_ENCODING_FIXED 16
441#define ENC_ASCII8BIT 1
443#define ENC_Windows_31J 3
458 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
459 flags |= ARG_ENCODING_NONE;
462 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
463 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_EUC_JP));
466 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
467 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_Windows_31J));
470 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
471 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_UTF8));
474 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) {
475 flags |= ONIG_OPTION_IGNORECASE;
478 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) {
479 flags |= ONIG_OPTION_MULTILINE;
482 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) {
483 flags |= ONIG_OPTION_EXTEND;
489#undef RE_OPTION_ENCODING_SHIFT
490#undef RE_OPTION_ENCODING
491#undef ARG_ENCODING_FIXED
492#undef ARG_ENCODING_NONE
495#undef ENC_Windows_31J
501 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_FORCED_BINARY_ENCODING) || PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
502 return rb_ascii8bit_encoding();
504 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
505 return rb_utf8_encoding();
507 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
508 return rb_enc_get_from_index(ENCINDEX_EUC_JP);
510 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
511 return rb_enc_get_from_index(ENCINDEX_Windows_31J);
521 VALUE errinfo = rb_errinfo();
523 int32_t line_number = pm_node_line_number(scope_node->parser, node);
524 VALUE regexp = rb_reg_compile(
string, parse_regexp_flags(node), (
const char *)
pm_string_source(&scope_node->parser->
filepath), line_number);
527 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
528 rb_set_errinfo(errinfo);
530 parse_regexp_error(iseq, line_number,
"%" PRIsVALUE, message);
541 rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
542 if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
545 return parse_regexp(iseq, scope_node, node,
string);
551 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
552 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
554 VALUE string = pm_static_literal_concat(iseq, parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
555 return parse_regexp(iseq, scope_node, node,
string);
564 size_t parts_size = parts->
size;
565 bool interpolated =
false;
567 if (parts_size > 0) {
571 for (
size_t index = 0; index < parts_size; index++) {
574 if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
578 if (implicit_regexp_encoding == NULL) {
579 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
582 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
585 if (
RTEST(current_string)) {
586 current_string =
rb_str_concat(current_string, string_value);
589 current_string = string_value;
590 if (index != 0) current_location = PM_NODE_END_LOCATION(scope_node->parser, part);
597 PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE) &&
605 if (implicit_regexp_encoding == NULL) {
606 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
609 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
612 if (
RTEST(current_string)) {
613 current_string =
rb_str_concat(current_string, string_value);
616 current_string = string_value;
617 current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
621 if (!
RTEST(current_string)) {
624 if (implicit_regexp_encoding != NULL) {
625 if (explicit_regexp_encoding != NULL) {
626 encoding = explicit_regexp_encoding;
629 encoding = rb_ascii8bit_encoding();
632 encoding = implicit_regexp_encoding;
636 encoding = scope_node->encoding;
639 if (parts_size == 1) {
640 current_string = rb_enc_str_new(NULL, 0, encoding);
644 if (
RTEST(current_string)) {
645 VALUE operand = rb_fstring(current_string);
646 PUSH_INSN1(ret, current_location, putobject, operand);
650 PM_COMPILE_NOT_POPPED(part);
652 const pm_node_location_t current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
653 PUSH_INSN(ret, current_location, dup);
656 const struct rb_callinfo *callinfo = new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE);
657 PUSH_INSN1(ret, current_location, objtostring, callinfo);
660 PUSH_INSN(ret, current_location, anytostring);
662 current_string =
Qnil;
668 if (
RTEST(current_string)) {
669 current_string = rb_fstring(current_string);
671 if (stack_size == 0) {
673 PUSH_INSN1(ret, current_location, putobject, current_string);
674 }
else if (mutable_result || interpolated) {
675 PUSH_INSN1(ret, current_location, putstring, current_string);
677 PUSH_INSN1(ret, current_location, putchilledstring, current_string);
680 PUSH_INSN1(ret, current_location, putobject, current_string);
683 current_string =
Qnil;
688 PUSH_INSN(ret, *node_location, putnil);
697 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
698 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
700 int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false,
false);
701 PUSH_INSN2(ret, *node_location, toregexp,
INT2FIX(parse_regexp_flags(node) & 0xFF),
INT2FIX(length));
715 return rb_fstring_lit(
"<compiled>");
724pm_static_literal_string(
rb_iseq_t *iseq,
VALUE string,
int line_number)
726 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
727 return rb_str_with_debug_created_info(
string, rb_iseq_path(iseq), line_number);
730 return rb_fstring(
string);
744 RUBY_ASSERT(PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL));
746 switch (PM_NODE_TYPE(node)) {
747 case PM_ARRAY_NODE: {
752 for (
size_t index = 0; index < elements->
size; index++) {
753 rb_ary_push(value, pm_static_literal_value(iseq, elements->
nodes[index], scope_node));
768 for (
size_t index = 0; index < elements->
size; index++) {
771 VALUE pair[2] = { pm_static_literal_value(iseq, cast->key, scope_node), pm_static_literal_value(iseq, cast->value, scope_node) };
775 VALUE value = rb_hash_new_with_size(elements->
size);
782 case PM_IMAGINARY_NODE:
784 case PM_INTEGER_NODE:
786 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
788 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &cast->parts);
790 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
792 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &cast->parts);
794 case PM_INTERPOLATED_STRING_NODE: {
796 int line_number = pm_node_line_number(scope_node->parser, node);
797 return pm_static_literal_string(iseq,
string, line_number);
799 case PM_INTERPOLATED_SYMBOL_NODE: {
801 VALUE string = pm_static_literal_concat(iseq, &cast->
parts, scope_node, NULL, NULL,
true);
803 return ID2SYM(rb_intern_str(
string));
805 case PM_MATCH_LAST_LINE_NODE: {
807 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &cast->unescaped);
811 case PM_RATIONAL_NODE:
813 case PM_REGULAR_EXPRESSION_NODE: {
815 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &cast->unescaped);
817 case PM_SOURCE_ENCODING_NODE:
818 return rb_enc_from_encoding(scope_node->encoding);
819 case PM_SOURCE_FILE_NODE: {
821 return pm_source_file_value(cast, scope_node);
823 case PM_SOURCE_LINE_NODE:
824 return INT2FIX(pm_node_line_number(scope_node->parser, node));
825 case PM_STRING_NODE: {
827 return parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
834 rb_bug(
"Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
845 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
846 const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, node);
849 .beg_pos = { .lineno = start_location.
line, .column = start_location.
column },
850 .end_pos = { .lineno = end_location.
line, .column = end_location.
column }
859#define PM_BRANCH_COVERAGE_P(iseq) (ISEQ_COVERAGE(iseq) && ISEQ_BRANCH_COVERAGE(iseq))
873 if (!then_label) then_label = label;
874 else if (!else_label) else_label = label;
876 pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, popped, scope_node);
878 if (LIST_INSN_SIZE_ONE(seq)) {
879 INSN *insn = (
INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
880 if (insn->insn_id == BIN(jump) && (
LABEL *)(insn->operands[0]) == label)
return;
883 if (!label->refcnt) {
884 if (popped) PUSH_INSN(ret, location, putnil);
887 PUSH_LABEL(seq, label);
899 if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
900 PM_COMPILE_NOT_POPPED(node);
903 PUSH_INSN1(ret, location, getglobal, operand);
905 PUSH_SEND(ret, location, idEq,
INT2FIX(1));
906 if (popped) PUSH_INSN(ret, location, pop);
919 int again = !(flip_flop_node->
base.
flags & PM_RANGE_FLAGS_EXCLUDE_END);
921 rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
924 PUSH_INSN2(ret, location, getspecial, key,
INT2FIX(0));
925 PUSH_INSNL(ret, location, branchif, lend);
927 if (flip_flop_node->
left) {
928 pm_compile_flip_flop_bound(iseq, flip_flop_node->
left, ret, popped, scope_node);
931 PUSH_INSN(ret, location, putnil);
934 PUSH_INSNL(ret, location, branchunless, else_label);
935 PUSH_INSN1(ret, location, putobject,
Qtrue);
936 PUSH_INSN1(ret, location, setspecial, key);
938 PUSH_INSNL(ret, location, jump, then_label);
941 PUSH_LABEL(ret, lend);
942 if (flip_flop_node->
right) {
943 pm_compile_flip_flop_bound(iseq, flip_flop_node->
right, ret, popped, scope_node);
946 PUSH_INSN(ret, location, putnil);
949 PUSH_INSNL(ret, location, branchunless, then_label);
950 PUSH_INSN1(ret, location, putobject,
Qfalse);
951 PUSH_INSN1(ret, location, setspecial, key);
952 PUSH_INSNL(ret, location, jump, then_label);
963 switch (PM_NODE_TYPE(cond)) {
966 pm_compile_logical(iseq, ret, cast->
left, NULL, else_label, popped, scope_node);
973 pm_compile_logical(iseq, ret, cast->
left, then_label, NULL, popped, scope_node);
980 PUSH_INSNL(ret, location, jump, else_label);
983 case PM_IMAGINARY_NODE:
984 case PM_INTEGER_NODE:
986 case PM_RATIONAL_NODE:
987 case PM_REGULAR_EXPRESSION_NODE:
991 PUSH_INSNL(ret, location, jump, then_label);
993 case PM_FLIP_FLOP_NODE:
994 pm_compile_flip_flop((
const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.
line, ret, popped, scope_node);
996 case PM_DEFINED_NODE: {
998 pm_compile_defined_expr(iseq, cast->
value, &location, ret, popped, scope_node,
true);
1002 DECL_ANCHOR(cond_seq);
1003 pm_compile_node(iseq, cond, cond_seq,
false, scope_node);
1005 if (LIST_INSN_SIZE_ONE(cond_seq)) {
1006 INSN *insn = (
INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
1008 if (insn->insn_id == BIN(putobject)) {
1009 if (
RTEST(insn->operands[0])) {
1010 PUSH_INSNL(ret, location, jump, then_label);
1015 PUSH_INSNL(ret, location, jump, else_label);
1021 PUSH_SEQ(ret, cond_seq);
1026 PUSH_INSNL(ret, location, branchunless, else_label);
1027 PUSH_INSNL(ret, location, jump, then_label);
1037 LABEL *then_label = NEW_LABEL(location.
line);
1038 LABEL *else_label = NEW_LABEL(location.
line);
1039 LABEL *end_label = NULL;
1041 DECL_ANCHOR(cond_seq);
1042 pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label,
false, scope_node);
1043 PUSH_SEQ(ret, cond_seq);
1048 if (then_label->refcnt && else_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1049 conditional_location = pm_code_location(scope_node, node);
1050 branches = decl_branch_base(iseq, PTR2NUM(node), &conditional_location,
type == PM_IF_NODE ?
"if" :
"unless");
1053 if (then_label->refcnt) {
1054 PUSH_LABEL(ret, then_label);
1056 DECL_ANCHOR(then_seq);
1058 if (statements != NULL) {
1059 pm_compile_node(iseq, (
const pm_node_t *) statements, then_seq, popped, scope_node);
1062 PUSH_SYNTHETIC_PUTNIL(then_seq, iseq);
1065 if (else_label->refcnt) {
1067 if (PM_BRANCH_COVERAGE_P(iseq)) {
1070 if (statements != NULL) {
1071 branch_location = pm_code_location(scope_node, (
const pm_node_t *) statements);
1072 }
else if (
type == PM_IF_NODE) {
1073 pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(scope_node->parser, predicate);
1075 .beg_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column },
1076 .end_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column }
1079 branch_location = conditional_location;
1082 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
type == PM_IF_NODE ?
"then" :
"else", branches);
1085 end_label = NEW_LABEL(location.
line);
1086 PUSH_INSNL(then_seq, location, jump, end_label);
1087 if (!popped) PUSH_INSN(then_seq, location, pop);
1090 PUSH_SEQ(ret, then_seq);
1093 if (else_label->refcnt) {
1094 PUSH_LABEL(ret, else_label);
1096 DECL_ANCHOR(else_seq);
1098 if (subsequent != NULL) {
1099 pm_compile_node(iseq, subsequent, else_seq, popped, scope_node);
1102 PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
1106 if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1109 if (subsequent == NULL) {
1110 branch_location = conditional_location;
1111 }
else if (PM_NODE_TYPE_P(subsequent, PM_ELSE_NODE)) {
1115 branch_location = pm_code_location(scope_node, (
const pm_node_t *) subsequent);
1118 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1,
type == PM_IF_NODE ?
"else" :
"then", branches);
1121 PUSH_SEQ(ret, else_seq);
1125 PUSH_LABEL(ret, end_label);
1139 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
1140 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
1141 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
1143 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.
line);
1144 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.
line);
1145 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.
line);
1146 LABEL *end_label = NEW_LABEL(location.
line);
1147 LABEL *adjust_label = NEW_LABEL(location.
line);
1149 LABEL *next_catch_label = NEW_LABEL(location.
line);
1150 LABEL *tmp_label = NULL;
1156 push_ensure_entry(iseq, &enl, NULL, NULL);
1159 if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
1160 tmp_label = NEW_LABEL(location.
line);
1161 PUSH_INSNL(ret, location, jump, tmp_label);
1165 PUSH_INSNL(ret, location, jump, next_label);
1168 PUSH_LABEL(ret, adjust_label);
1169 PUSH_INSN(ret, location, putnil);
1170 PUSH_LABEL(ret, next_catch_label);
1171 PUSH_INSN(ret, location, pop);
1172 PUSH_INSNL(ret, location, jump, next_label);
1173 if (tmp_label) PUSH_LABEL(ret, tmp_label);
1175 PUSH_LABEL(ret, redo_label);
1178 if (PM_BRANCH_COVERAGE_P(iseq)) {
1180 VALUE branches = decl_branch_base(iseq, PTR2NUM(node), &loop_location,
type == PM_WHILE_NODE ?
"while" :
"until");
1182 rb_code_location_t branch_location = statements != NULL ? pm_code_location(scope_node, (
const pm_node_t *) statements) : loop_location;
1183 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
"body", branches);
1186 if (statements != NULL) PM_COMPILE_POPPED((
const pm_node_t *) statements);
1187 PUSH_LABEL(ret, next_label);
1189 if (
type == PM_WHILE_NODE) {
1190 pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, popped, scope_node);
1192 else if (
type == PM_UNTIL_NODE) {
1193 pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, popped, scope_node);
1196 PUSH_LABEL(ret, end_label);
1197 PUSH_ADJUST_RESTORE(ret, adjust_label);
1198 PUSH_INSN(ret, location, putnil);
1200 PUSH_LABEL(ret, break_label);
1201 if (popped) PUSH_INSN(ret, location, pop);
1203 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
1204 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
1205 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL, ISEQ_COMPILE_DATA(iseq)->redo_label);
1207 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
1208 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
1209 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
1210 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
1222 st_data_t local_index;
1225 for (level = 0; level < start_depth; level++) {
1226 scope_node = scope_node->previous;
1229 while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
1232 if (scope_node->previous) {
1233 scope_node = scope_node->previous;
1238 rb_bug(
"Local with constant_id %u does not exist", (
unsigned int) constant_id);
1242 lindex.level = level;
1243 lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
1257 rb_bug(
"constant_id out of range: %u", (
unsigned int)constant_id);
1259 return scope_node->constants[constant_id - 1];
1265 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1266 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1268 rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
1269 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1271 isolated_depth ? isolated_depth + 1 : 0,
1272 type, ISEQ_COMPILE_DATA(iseq)->option, &error_state);
1275 pm_scope_node_destroy(node);
1277 rb_jump_tag(error_state);
1279 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1286 if (PM_NODE_TYPE_P(node, PM_CONSTANT_PATH_NODE)) {
1292 return VM_DEFINECLASS_FLAG_SCOPED;
1296 PUSH_INSN1(ret, *node_location, putobject, rb_cObject);
1297 return VM_DEFINECLASS_FLAG_SCOPED;
1302 PUSH_INSN1(ret, *node_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
1312pm_compile_call_and_or_write_node(
rb_iseq_t *iseq,
bool and_node,
const pm_node_t *receiver,
const pm_node_t *value,
pm_constant_id_t write_name,
pm_constant_id_t read_name,
bool safe_nav,
const pm_node_location_t *node_location,
LINK_ANCHOR *
const ret,
bool popped,
pm_scope_node_t *scope_node)
1316 LABEL *lcfin = NEW_LABEL(location.
line);
1317 LABEL *lskip = NULL;
1319 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1320 ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
1322 PM_COMPILE_NOT_POPPED(receiver);
1324 lskip = NEW_LABEL(location.
line);
1325 PUSH_INSN(ret, location, dup);
1326 PUSH_INSNL(ret, location, branchnil, lskip);
1329 PUSH_INSN(ret, location, dup);
1330 PUSH_SEND_WITH_FLAG(ret, location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
1331 if (!popped) PUSH_INSN(ret, location, dup);
1334 PUSH_INSNL(ret, location, branchunless, lcfin);
1337 PUSH_INSNL(ret, location, branchif, lcfin);
1340 if (!popped) PUSH_INSN(ret, location, pop);
1341 PM_COMPILE_NOT_POPPED(value);
1344 PUSH_INSN(ret, location, swap);
1345 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
1348 ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
1349 PUSH_SEND_WITH_FLAG(ret, location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
1350 PUSH_INSNL(ret, location, jump, lfin);
1352 PUSH_LABEL(ret, lcfin);
1353 if (!popped) PUSH_INSN(ret, location, swap);
1355 PUSH_LABEL(ret, lfin);
1357 if (lskip && popped) PUSH_LABEL(ret, lskip);
1358 PUSH_INSN(ret, location, pop);
1359 if (lskip && !popped) PUSH_LABEL(ret, lskip);
1372 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
1378 const int max_stack_length = 0x100;
1379 const unsigned int min_tmp_hash_length = 0x800;
1381 int stack_length = 0;
1382 bool first_chunk =
true;
1388 bool static_literal =
false;
1390 DECL_ANCHOR(anchor);
1393#define FLUSH_CHUNK \
1394 if (stack_length) { \
1395 if (first_chunk) { \
1396 PUSH_SEQ(ret, anchor); \
1397 PUSH_INSN1(ret, location, newhash, INT2FIX(stack_length)); \
1398 first_chunk = false; \
1401 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
1402 PUSH_INSN(ret, location, swap); \
1403 PUSH_SEQ(ret, anchor); \
1404 PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(stack_length + 1)); \
1406 INIT_ANCHOR(anchor); \
1410 for (
size_t index = 0; index < elements->
size; index++) {
1413 switch (PM_NODE_TYPE(element)) {
1414 case PM_ASSOC_NODE: {
1417 (shareability == 0) &&
1418 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && (
1419 (!static_literal && ((index + min_tmp_hash_length) < elements->
size)) ||
1420 (first_chunk && stack_length == 0)
1425 while (index + count < elements->size && PM_NODE_FLAG_P(elements->
nodes[index + count], PM_NODE_FLAG_STATIC_LITERAL)) count++;
1427 if ((first_chunk && stack_length == 0) || count >= min_tmp_hash_length) {
1433 for (
size_t tmp_end = index + count; index < tmp_end; index++) {
1437 pm_static_literal_value(iseq, assoc->key, scope_node),
1438 pm_static_literal_value(iseq, assoc->value, scope_node)
1453 PUSH_INSN1(ret, location, duphash, hash);
1454 first_chunk =
false;
1457 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1458 PUSH_INSN(ret, location, swap);
1459 PUSH_INSN1(ret, location, putobject, hash);
1460 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1466 static_literal =
true;
1470 static_literal =
false;
1475 if (shareability == 0) {
1476 pm_compile_node(iseq, element, anchor,
false, scope_node);
1480 pm_compile_shareable_constant_value(iseq, assoc->
key, shareability, path, ret, scope_node,
false);
1481 pm_compile_shareable_constant_value(iseq, assoc->
value, shareability, path, ret, scope_node,
false);
1484 if ((stack_length += 2) >= max_stack_length) FLUSH_CHUNK;
1487 case PM_ASSOC_SPLAT_NODE: {
1491 bool empty_hash = assoc_splat->
value != NULL && (
1492 (PM_NODE_TYPE_P(assoc_splat->
value, PM_HASH_NODE) && ((
const pm_hash_node_t *) assoc_splat->
value)->elements.size == 0) ||
1493 PM_NODE_TYPE_P(assoc_splat->
value, PM_NIL_NODE)
1496 bool first_element = first_chunk && stack_length == 0;
1497 bool last_element = index == elements->
size - 1;
1498 bool only_element = first_element && last_element;
1501 if (only_element && argument) {
1508 PUSH_INSN(ret, location, putnil);
1510 else if (first_element) {
1513 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1520 if (only_element && argument) {
1526 if (shareability == 0) {
1527 PM_COMPILE_NOT_POPPED(element);
1530 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1538 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1540 if (first_element) {
1541 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1544 PUSH_INSN(ret, location, swap);
1547 if (shareability == 0) {
1548 PM_COMPILE_NOT_POPPED(element);
1551 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1554 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1558 first_chunk =
false;
1559 static_literal =
false;
1563 RUBY_ASSERT(
"Invalid node type for hash" &&
false);
1572#define SPLATARRAY_FALSE 0
1573#define SPLATARRAY_TRUE 1
1574#define DUP_SINGLE_KW_SPLAT 2
1583 bool has_splat =
false;
1584 bool has_keyword_splat =
false;
1586 if (arguments_node == NULL) {
1587 if (*flags & VM_CALL_FCALL) {
1588 *flags |= VM_CALL_VCALL;
1593 has_keyword_splat = PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
1597 int post_splat_counter = 0;
1601 switch (PM_NODE_TYPE(argument)) {
1603 case PM_KEYWORD_HASH_NODE: {
1607 if (has_keyword_splat || has_splat) {
1608 *flags |= VM_CALL_KW_SPLAT;
1609 has_keyword_splat =
true;
1611 if (elements->
size > 1 || !(elements->
size == 1 && PM_NODE_TYPE_P(elements->
nodes[0], PM_ASSOC_SPLAT_NODE))) {
1615 *flags |= VM_CALL_KW_SPLAT_MUT;
1616 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1618 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
1619 *flags |= VM_CALL_KW_SPLAT_MUT;
1620 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1621 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1622 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1623 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1626 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1632 if (PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS)) {
1637 VALUE stored_indices = rb_hash_new();
1641 for (
size_t element_index = 0; element_index < elements->
size; element_index++) {
1646 VALUE keyword = pm_static_literal_value(iseq, assoc->key, scope_node);
1647 VALUE stored_index = rb_hash_aref(stored_indices, keyword);
1652 if (!
NIL_P(stored_index)) {
1660 rb_hash_aset(stored_indices, keyword,
ULONG2NUM(element_index));
1666 *flags |= VM_CALL_KWARG;
1668 VALUE *keywords = (*kw_arg)->keywords;
1669 (*kw_arg)->references = 0;
1670 (*kw_arg)->keyword_len = (int) size;
1672 size_t keyword_index = 0;
1673 for (
size_t element_index = 0; element_index < elements->
size; element_index++) {
1678 keywords[keyword_index++] = pm_static_literal_value(iseq, assoc->key, scope_node);
1682 PM_COMPILE(assoc->value);
1691 *flags |= VM_CALL_KW_SPLAT;
1693 size_t size = elements->
size;
1698 *flags |= VM_CALL_KW_SPLAT_MUT;
1701 for (
size_t element_index = 0; element_index < size; element_index++) {
1703 PM_COMPILE_NOT_POPPED(assoc->key);
1704 PM_COMPILE_NOT_POPPED(assoc->value);
1707 PUSH_INSN1(ret, location, newhash,
INT2FIX(size * 2));
1712 case PM_SPLAT_NODE: {
1713 *flags |= VM_CALL_ARGS_SPLAT;
1717 PM_COMPILE_NOT_POPPED(splat_node->
expression);
1720 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1721 PUSH_GETLOCAL(ret, location, index.index, index.level);
1724 bool first_splat = !has_splat;
1732 if (index + 1 < arguments->
size || has_regular_blockarg) {
1733 PUSH_INSN1(ret, location, splatarray, (*dup_rest & SPLATARRAY_TRUE) ?
Qtrue :
Qfalse);
1734 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
1742 PUSH_INSN1(ret, location, splatarray,
Qfalse);
1752 PUSH_INSN(ret, location, concattoarray);
1756 post_splat_counter = 0;
1760 case PM_FORWARDING_ARGUMENTS_NODE: {
1761 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
1763 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
1764 *flags |= VM_CALL_FORWARDING;
1766 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
1767 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1779 *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
1786 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1787 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1791 PUSH_INSN(ret, location, concattoarray);
1793 PUSH_INSN1(ret, location, splatarray,
Qfalse);
1797 pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
1798 PUSH_GETLOCAL(ret, location, pow_local.index, pow_local.level);
1801 pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
1802 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(and_local.level));
1807 post_splat_counter++;
1808 PM_COMPILE_NOT_POPPED(argument);
1828 if (index == arguments->
size - 1) {
1830 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(post_splat_counter));
1835 switch (PM_NODE_TYPE(next_arg)) {
1837 case PM_KEYWORD_HASH_NODE: {
1838 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1839 PUSH_INSN(ret, location, concatarray);
1842 case PM_SPLAT_NODE: {
1843 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1844 PUSH_INSN(ret, location, concatarray);
1860 if (has_splat) orig_argc++;
1861 if (has_keyword_splat) orig_argc++;
1870pm_setup_args_dup_rest_p(
const pm_node_t *node)
1872 switch (PM_NODE_TYPE(node)) {
1873 case PM_BACK_REFERENCE_READ_NODE:
1874 case PM_CLASS_VARIABLE_READ_NODE:
1875 case PM_CONSTANT_READ_NODE:
1878 case PM_GLOBAL_VARIABLE_READ_NODE:
1879 case PM_IMAGINARY_NODE:
1880 case PM_INSTANCE_VARIABLE_READ_NODE:
1881 case PM_INTEGER_NODE:
1882 case PM_LAMBDA_NODE:
1883 case PM_LOCAL_VARIABLE_READ_NODE:
1885 case PM_NUMBERED_REFERENCE_READ_NODE:
1886 case PM_RATIONAL_NODE:
1887 case PM_REGULAR_EXPRESSION_NODE:
1889 case PM_STRING_NODE:
1890 case PM_SYMBOL_NODE:
1893 case PM_CONSTANT_PATH_NODE: {
1895 if (cast->
parent != NULL) {
1896 return pm_setup_args_dup_rest_p(cast->
parent);
1900 case PM_IMPLICIT_NODE:
1902 case PM_ARRAY_NODE: {
1904 for (
size_t index = 0; index < cast->
elements.
size; index++) {
1905 if (pm_setup_args_dup_rest_p(cast->
elements.
nodes[index])) {
1922 int dup_rest = SPLATARRAY_TRUE;
1925 size_t arguments_size;
1930 arguments_node != NULL &&
1931 (arguments = &arguments_node->
arguments, arguments_size = arguments->
size) >= 2 &&
1932 PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_SPLAT) &&
1933 !PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_MULTIPLE_SPLATS) &&
1934 PM_NODE_TYPE_P(arguments->
nodes[arguments_size - 1], PM_KEYWORD_HASH_NODE)
1939 dup_rest = SPLATARRAY_FALSE;
1944 for (
size_t index = 0; dup_rest == SPLATARRAY_FALSE && index < elements->
size; index++) {
1947 switch (PM_NODE_TYPE(element)) {
1948 case PM_ASSOC_NODE: {
1950 if (pm_setup_args_dup_rest_p(assoc->
key) || pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
1953 case PM_ASSOC_SPLAT_NODE: {
1955 if (assoc->
value != NULL && pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
1964 int initial_dup_rest = dup_rest;
1967 if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
1971 bool regular_block_arg =
true;
1974 if (block_expr && pm_setup_args_dup_rest_p(block_expr)) {
1975 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
1976 initial_dup_rest = dup_rest;
1979 DECL_ANCHOR(block_arg);
1980 pm_compile_node(iseq, block, block_arg,
false, scope_node);
1982 *flags |= VM_CALL_ARGS_BLOCKARG;
1984 if (LIST_INSN_SIZE_ONE(block_arg)) {
1986 if (IS_INSN(elem)) {
1988 if (iobj->insn_id == BIN(getblockparam)) {
1989 iobj->insn_id = BIN(getblockparamproxy);
1996 regular_block_arg =
false;
2000 argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
2001 PUSH_SEQ(ret, block_arg);
2004 argc = pm_setup_args_core(arguments_node, block, flags,
false, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
2011 if (*flags & VM_CALL_ARGS_SPLAT && dup_rest != initial_dup_rest) {
2012 *flags |= VM_CALL_ARGS_SPLAT_MUT;
2032 if (!popped) PUSH_INSN(ret, location, putnil);
2034 PM_COMPILE_NOT_POPPED(node->
receiver);
2036 int boff = (node->
block == NULL ? 0 : 1);
2037 int flag = PM_NODE_TYPE_P(node->
receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2039 int argc = pm_setup_args(node->
arguments, (
const pm_node_t *) node->
block, &flag, &keywords, iseq, ret, scope_node, node_location);
2041 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2043 PUSH_INSN(ret, location, splatkw);
2046 PUSH_INSN(ret, location, dup);
2047 PUSH_INSN(ret, location, splatkw);
2048 PUSH_INSN(ret, location, pop);
2052 int dup_argn = argc + 1 + boff;
2053 int keyword_len = 0;
2056 keyword_len = keywords->keyword_len;
2057 dup_argn += keyword_len;
2060 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2061 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2062 PM_COMPILE_NOT_POPPED(node->
value);
2065 PUSH_SEND(ret, location, id_operator,
INT2FIX(1));
2068 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2070 if (flag & VM_CALL_ARGS_SPLAT) {
2071 if (flag & VM_CALL_KW_SPLAT) {
2072 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2074 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2075 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2076 flag |= VM_CALL_ARGS_SPLAT_MUT;
2079 PUSH_INSN(ret, location, swap);
2080 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2081 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2082 PUSH_INSN(ret, location, pop);
2086 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2087 PUSH_INSN(ret, location, swap);
2088 PUSH_INSN(ret, location, pop);
2090 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2091 PUSH_INSN(ret, location, swap);
2092 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2093 PUSH_INSN(ret, location, swap);
2094 flag |= VM_CALL_ARGS_SPLAT_MUT;
2096 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2098 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2099 PUSH_INSN(ret, location, pop);
2100 PUSH_INSN(ret, location, pop);
2104 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2106 else if (flag & VM_CALL_KW_SPLAT) {
2108 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2109 PUSH_INSN(ret, location, swap);
2110 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2111 PUSH_INSN(ret, location, pop);
2113 PUSH_INSN(ret, location, swap);
2114 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2116 else if (keyword_len) {
2117 PUSH_INSN(ret, location, dup);
2118 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 2));
2119 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2120 PUSH_INSN(ret, location, pop);
2121 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2125 PUSH_INSN(ret, location, swap);
2127 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2130 PUSH_INSN(ret, location, pop);
2149 if (!popped) PUSH_INSN(ret, location, putnil);
2150 PM_COMPILE_NOT_POPPED(receiver);
2152 int boff = (block == NULL ? 0 : 1);
2153 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2155 int argc = pm_setup_args(arguments, (
const pm_node_t *) block, &flag, &keywords, iseq, ret, scope_node, node_location);
2157 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2159 PUSH_INSN(ret, location, splatkw);
2162 PUSH_INSN(ret, location, dup);
2163 PUSH_INSN(ret, location, splatkw);
2164 PUSH_INSN(ret, location, pop);
2168 int dup_argn = argc + 1 + boff;
2169 int keyword_len = 0;
2172 keyword_len = keywords->keyword_len;
2173 dup_argn += keyword_len;
2176 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2177 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2179 LABEL *label = NEW_LABEL(location.
line);
2182 PUSH_INSN(ret, location, dup);
2183 if (PM_NODE_TYPE_P(node, PM_INDEX_AND_WRITE_NODE)) {
2184 PUSH_INSNL(ret, location, branchunless, label);
2187 PUSH_INSNL(ret, location, branchif, label);
2190 PUSH_INSN(ret, location, pop);
2191 PM_COMPILE_NOT_POPPED(value);
2194 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2197 if (flag & VM_CALL_ARGS_SPLAT) {
2198 if (flag & VM_CALL_KW_SPLAT) {
2199 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2200 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2201 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2202 flag |= VM_CALL_ARGS_SPLAT_MUT;
2205 PUSH_INSN(ret, location, swap);
2206 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2207 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2208 PUSH_INSN(ret, location, pop);
2212 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2213 PUSH_INSN(ret, location, swap);
2214 PUSH_INSN(ret, location, pop);
2216 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2217 PUSH_INSN(ret, location, swap);
2218 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2219 PUSH_INSN(ret, location, swap);
2220 flag |= VM_CALL_ARGS_SPLAT_MUT;
2222 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2224 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2225 PUSH_INSN(ret, location, pop);
2226 PUSH_INSN(ret, location, pop);
2230 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2232 else if (flag & VM_CALL_KW_SPLAT) {
2234 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2235 PUSH_INSN(ret, location, swap);
2236 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2237 PUSH_INSN(ret, location, pop);
2240 PUSH_INSN(ret, location, swap);
2241 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2243 else if (keyword_len) {
2244 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2245 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 0));
2246 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2250 PUSH_INSN(ret, location, swap);
2252 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2255 PUSH_INSN(ret, location, pop);
2256 PUSH_INSNL(ret, location, jump, lfin);
2257 PUSH_LABEL(ret, label);
2259 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2261 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(dup_argn + 1));
2262 PUSH_LABEL(ret, lfin);
2271#define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
2272#define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
2273#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
2274#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
2275#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
2280static int pm_compile_pattern(
rb_iseq_t *iseq,
pm_scope_node_t *scope_node,
const pm_node_t *node,
LINK_ANCHOR *
const ret,
LABEL *matched_label,
LABEL *unmatched_label,
bool in_single_pattern,
bool in_alternation_pattern,
bool use_deconstructed_cache,
unsigned int base_index);
2289 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2290 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2292 PUSH_INSN(ret, location, dup);
2293 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2295 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2296 PUSH_INSN1(ret, location, putobject, message);
2297 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2298 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2299 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2301 PUSH_INSN1(ret, location, putobject,
Qfalse);
2302 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2304 PUSH_INSN(ret, location, pop);
2305 PUSH_INSN(ret, location, pop);
2306 PUSH_LABEL(ret, match_succeeded_label);
2319 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2320 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2322 PUSH_INSN(ret, location, dup);
2323 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2325 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2326 PUSH_INSN1(ret, location, putobject, message);
2327 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2328 PUSH_INSN(ret, location, dup);
2329 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2330 PUSH_INSN1(ret, location, putobject, length);
2331 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(4));
2332 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2334 PUSH_INSN1(ret, location, putobject,
Qfalse);
2335 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2337 PUSH_INSN(ret, location, pop);
2338 PUSH_INSN(ret, location, pop);
2339 PUSH_LABEL(ret, match_succeeded_label);
2352 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2353 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2355 PUSH_INSN(ret, location, dup);
2356 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2357 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2359 VALUE operand = rb_fstring_lit(
"%p === %p does not return true");
2360 PUSH_INSN1(ret, location, putobject, operand);
2362 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2363 PUSH_INSN1(ret, location, topn,
INT2FIX(5));
2364 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2365 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2366 PUSH_INSN1(ret, location, putobject,
Qfalse);
2367 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2368 PUSH_INSN(ret, location, pop);
2369 PUSH_INSN(ret, location, pop);
2371 PUSH_LABEL(ret, match_succeeded_label);
2372 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2373 PUSH_INSN(ret, location, pop);
2374 PUSH_INSN(ret, location, pop);
2386pm_compile_pattern_match(
rb_iseq_t *iseq,
pm_scope_node_t *scope_node,
const pm_node_t *node,
LINK_ANCHOR *
const ret,
LABEL *unmatched_label,
bool in_single_pattern,
bool in_alternation_pattern,
bool use_deconstructed_cache,
unsigned int base_index)
2388 LABEL *matched_label = NEW_LABEL(pm_node_line_number(scope_node->parser, node));
2389 CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
2390 PUSH_LABEL(ret, matched_label);
2400pm_compile_pattern_deconstruct(
rb_iseq_t *iseq,
pm_scope_node_t *scope_node,
const pm_node_t *node,
LINK_ANCHOR *
const ret,
LABEL *deconstruct_label,
LABEL *match_failed_label,
LABEL *deconstructed_label,
LABEL *type_error_label,
bool in_single_pattern,
bool use_deconstructed_cache,
unsigned int base_index)
2402 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2404 if (use_deconstructed_cache) {
2405 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2406 PUSH_INSNL(ret, location, branchnil, deconstruct_label);
2408 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2409 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2411 PUSH_INSN(ret, location, pop);
2412 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
2413 PUSH_INSNL(ret, location, jump, deconstructed_label);
2416 PUSH_INSNL(ret, location, jump, deconstruct_label);
2419 PUSH_LABEL(ret, deconstruct_label);
2420 PUSH_INSN(ret, location, dup);
2423 PUSH_INSN1(ret, location, putobject, operand);
2424 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2426 if (use_deconstructed_cache) {
2427 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
2430 if (in_single_pattern) {
2431 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1));
2434 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2435 PUSH_SEND(ret, location, rb_intern(
"deconstruct"),
INT2FIX(0));
2437 if (use_deconstructed_cache) {
2438 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2441 PUSH_INSN(ret, location, dup);
2443 PUSH_INSNL(ret, location, branchunless, type_error_label);
2444 PUSH_LABEL(ret, deconstructed_label);
2456 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2458 PUSH_INSN(ret, location, dup);
2459 PM_COMPILE_NOT_POPPED(node);
2461 if (in_single_pattern) {
2462 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
2464 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2465 if (in_single_pattern) {
2466 CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
2468 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2479 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2480 LABEL *key_error_label = NEW_LABEL(location.
line);
2481 LABEL *cleanup_label = NEW_LABEL(location.
line);
2484 kw_arg->references = 0;
2485 kw_arg->keyword_len = 2;
2486 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
2487 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
2489 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2490 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2491 PUSH_INSNL(ret, location, branchif, key_error_label);
2494 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2497 VALUE operand = rb_fstring_lit(
"%p: %s");
2498 PUSH_INSN1(ret, location, putobject, operand);
2501 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2502 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2503 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2504 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2505 PUSH_INSNL(ret, location, jump, cleanup_label);
2507 PUSH_LABEL(ret, key_error_label);
2509 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2512 VALUE operand = rb_fstring_lit(
"%p: %s");
2513 PUSH_INSN1(ret, location, putobject, operand);
2516 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2517 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2518 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2519 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2520 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2521 PUSH_SEND_R(ret, location, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
2522 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(1));
2523 PUSH_LABEL(ret, cleanup_label);
2525 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(7));
2526 if (!popped) PUSH_INSN(ret, location, putnil);
2527 PUSH_INSNL(ret, location, jump, done_label);
2528 PUSH_INSN1(ret, location, dupn,
INT2FIX(5));
2529 if (popped) PUSH_INSN(ret, location, putnil);
2538 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2540 switch (PM_NODE_TYPE(node)) {
2541 case PM_ARRAY_PATTERN_NODE: {
2553 const size_t posts_size = cast->
posts.
size;
2554 const size_t minimum_size = requireds_size + posts_size;
2556 bool rest_named =
false;
2557 bool use_rest_size =
false;
2559 if (cast->
rest != NULL) {
2560 rest_named = (PM_NODE_TYPE_P(cast->
rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *) cast->
rest)->expression != NULL);
2561 use_rest_size = (rest_named || (!rest_named && posts_size > 0));
2564 LABEL *match_failed_label = NEW_LABEL(location.
line);
2565 LABEL *type_error_label = NEW_LABEL(location.
line);
2566 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2567 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2569 if (use_rest_size) {
2570 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2571 PUSH_INSN(ret, location, swap);
2576 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2579 CHECK(pm_compile_pattern_deconstruct(iseq, scope_node, node, ret, deconstruct_label, match_failed_label, deconstructed_label, type_error_label, in_single_pattern, use_deconstructed_cache, base_index));
2581 PUSH_INSN(ret, location, dup);
2582 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2583 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2584 PUSH_SEND(ret, location, cast->
rest == NULL ? idEq : idGE,
INT2FIX(1));
2585 if (in_single_pattern) {
2586 VALUE message = cast->
rest == NULL ? rb_fstring_lit(
"%p length mismatch (given %p, expected %p)") : rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)");
2587 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message,
INT2FIX(minimum_size), base_index + 1));
2589 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2591 for (
size_t index = 0; index < requireds_size; index++) {
2593 PUSH_INSN(ret, location, dup);
2594 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2595 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2596 CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2599 if (cast->
rest != NULL) {
2601 PUSH_INSN(ret, location, dup);
2602 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size));
2603 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2604 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2605 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2606 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2607 PUSH_INSN1(ret, location, setn,
INT2FIX(4));
2608 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2609 CHECK(pm_compile_pattern_match(iseq, scope_node, ((
const pm_splat_node_t *) cast->
rest)->expression, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2611 else if (posts_size > 0) {
2612 PUSH_INSN(ret, location, dup);
2613 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2614 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2615 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2616 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2617 PUSH_INSN(ret, location, pop);
2621 for (
size_t index = 0; index < posts_size; index++) {
2623 PUSH_INSN(ret, location, dup);
2625 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size + index));
2626 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2627 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2628 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2629 CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2632 PUSH_INSN(ret, location, pop);
2633 if (use_rest_size) {
2634 PUSH_INSN(ret, location, pop);
2637 PUSH_INSNL(ret, location, jump, matched_label);
2638 PUSH_INSN(ret, location, putnil);
2639 if (use_rest_size) {
2640 PUSH_INSN(ret, location, putnil);
2643 PUSH_LABEL(ret, type_error_label);
2644 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2648 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2649 PUSH_INSN1(ret, location, putobject, operand);
2652 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2653 PUSH_INSN(ret, location, pop);
2655 PUSH_LABEL(ret, match_failed_label);
2656 PUSH_INSN(ret, location, pop);
2657 if (use_rest_size) {
2658 PUSH_INSN(ret, location, pop);
2661 PUSH_INSNL(ret, location, jump, unmatched_label);
2664 case PM_FIND_PATTERN_NODE: {
2676 LABEL *match_failed_label = NEW_LABEL(location.
line);
2677 LABEL *type_error_label = NEW_LABEL(location.
line);
2678 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2679 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2682 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2685 CHECK(pm_compile_pattern_deconstruct(iseq, scope_node, node, ret, deconstruct_label, match_failed_label, deconstructed_label, type_error_label, in_single_pattern, use_deconstructed_cache, base_index));
2687 PUSH_INSN(ret, location, dup);
2688 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2689 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2690 PUSH_SEND(ret, location, idGE,
INT2FIX(1));
2691 if (in_single_pattern) {
2692 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p length mismatch (given %p, expected %p+)"),
INT2FIX(size), base_index + 1));
2694 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2697 LABEL *while_begin_label = NEW_LABEL(location.
line);
2698 LABEL *next_loop_label = NEW_LABEL(location.
line);
2699 LABEL *find_succeeded_label = NEW_LABEL(location.
line);
2700 LABEL *find_failed_label = NEW_LABEL(location.
line);
2702 PUSH_INSN(ret, location, dup);
2703 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2705 PUSH_INSN(ret, location, dup);
2706 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2707 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2708 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2709 PUSH_LABEL(ret, while_begin_label);
2711 PUSH_INSN(ret, location, dup);
2712 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2713 PUSH_SEND(ret, location, idLE,
INT2FIX(1));
2714 PUSH_INSNL(ret, location, branchunless, find_failed_label);
2716 for (
size_t index = 0; index < size; index++) {
2717 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2718 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2721 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2722 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2725 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2726 CHECK(pm_compile_pattern_match(iseq, scope_node, cast->
requireds.
nodes[index], ret, next_loop_label, in_single_pattern, in_alternation_pattern,
false, base_index + 4));
2732 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2733 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2734 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2735 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2736 CHECK(pm_compile_pattern_match(iseq, scope_node, left->
expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 4));
2742 if (right->expression != NULL) {
2743 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2744 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2745 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2746 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2747 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2748 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2749 pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 4);
2752 PUSH_INSNL(ret, location, jump, find_succeeded_label);
2754 PUSH_LABEL(ret, next_loop_label);
2755 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
2756 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2757 PUSH_INSNL(ret, location, jump, while_begin_label);
2759 PUSH_LABEL(ret, find_failed_label);
2760 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2761 if (in_single_pattern) {
2762 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2765 VALUE operand = rb_fstring_lit(
"%p does not match to find pattern");
2766 PUSH_INSN1(ret, location, putobject, operand);
2769 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2770 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2771 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2773 PUSH_INSN1(ret, location, putobject,
Qfalse);
2774 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2776 PUSH_INSN(ret, location, pop);
2777 PUSH_INSN(ret, location, pop);
2779 PUSH_INSNL(ret, location, jump, match_failed_label);
2780 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2782 PUSH_LABEL(ret, find_succeeded_label);
2783 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2786 PUSH_INSN(ret, location, pop);
2787 PUSH_INSNL(ret, location, jump, matched_label);
2788 PUSH_INSN(ret, location, putnil);
2790 PUSH_LABEL(ret, type_error_label);
2791 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2795 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2796 PUSH_INSN1(ret, location, putobject, operand);
2799 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2800 PUSH_INSN(ret, location, pop);
2802 PUSH_LABEL(ret, match_failed_label);
2803 PUSH_INSN(ret, location, pop);
2804 PUSH_INSNL(ret, location, jump, unmatched_label);
2808 case PM_HASH_PATTERN_NODE: {
2823 LABEL *match_failed_label = NEW_LABEL(location.
line);
2824 LABEL *type_error_label = NEW_LABEL(location.
line);
2827 if (has_keys && !has_rest) {
2830 for (
size_t index = 0; index < cast->
elements.
size; index++) {
2832 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2843 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2846 PUSH_INSN(ret, location, dup);
2849 VALUE operand =
ID2SYM(rb_intern(
"deconstruct_keys"));
2850 PUSH_INSN1(ret, location, putobject, operand);
2853 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2854 if (in_single_pattern) {
2855 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1));
2857 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2860 PUSH_INSN(ret, location, putnil);
2863 PUSH_INSN1(ret, location, duparray, keys);
2866 PUSH_SEND(ret, location, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
2868 PUSH_INSN(ret, location, dup);
2870 PUSH_INSNL(ret, location, branchunless, type_error_label);
2873 PUSH_SEND(ret, location, rb_intern(
"dup"),
INT2FIX(0));
2877 DECL_ANCHOR(match_values);
2879 for (
size_t index = 0; index < cast->
elements.
size; index++) {
2881 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2888 PUSH_INSN(ret, location, dup);
2889 PUSH_INSN1(ret, location, putobject, symbol);
2890 PUSH_SEND(ret, location, rb_intern(
"key?"),
INT2FIX(1));
2892 if (in_single_pattern) {
2893 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2895 PUSH_INSN(ret, location, dup);
2896 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2900 PUSH_INSN1(ret, location, putobject, operand);
2903 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
2904 PUSH_INSN1(ret, location, putobject,
Qtrue);
2905 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
2906 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2907 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2908 PUSH_INSN1(ret, location, putobject, symbol);
2909 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2911 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(4));
2912 PUSH_LABEL(ret, match_succeeded_label);
2915 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2916 PUSH_INSN(match_values, location, dup);
2917 PUSH_INSN1(match_values, location, putobject, symbol);
2918 PUSH_SEND(match_values, location, has_rest ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
2921 if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
2925 CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2928 PUSH_SEQ(ret, match_values);
2931 PUSH_INSN(ret, location, dup);
2932 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
2933 if (in_single_pattern) {
2934 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p is not empty"), base_index + 1));
2936 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2940 switch (PM_NODE_TYPE(cast->
rest)) {
2941 case PM_NO_KEYWORDS_PARAMETER_NODE: {
2942 PUSH_INSN(ret, location, dup);
2943 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
2944 if (in_single_pattern) {
2945 pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1);
2947 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2950 case PM_ASSOC_SPLAT_NODE: {
2952 PUSH_INSN(ret, location, dup);
2953 pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1);
2957 rb_bug(
"unreachable");
2962 PUSH_INSN(ret, location, pop);
2963 PUSH_INSNL(ret, location, jump, matched_label);
2964 PUSH_INSN(ret, location, putnil);
2966 PUSH_LABEL(ret, type_error_label);
2967 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2971 VALUE operand = rb_fstring_lit(
"deconstruct_keys must return Hash");
2972 PUSH_INSN1(ret, location, putobject, operand);
2975 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2976 PUSH_INSN(ret, location, pop);
2978 PUSH_LABEL(ret, match_failed_label);
2979 PUSH_INSN(ret, location, pop);
2980 PUSH_INSNL(ret, location, jump, unmatched_label);
2983 case PM_CAPTURE_PATTERN_NODE: {
2995 LABEL *match_failed_label = NEW_LABEL(location.
line);
2997 PUSH_INSN(ret, location, dup);
2998 CHECK(pm_compile_pattern_match(iseq, scope_node, cast->
value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index + 1));
2999 CHECK(pm_compile_pattern(iseq, scope_node, (
const pm_node_t *) cast->
target, ret, matched_label, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index));
3000 PUSH_INSN(ret, location, putnil);
3002 PUSH_LABEL(ret, match_failed_label);
3003 PUSH_INSN(ret, location, pop);
3004 PUSH_INSNL(ret, location, jump, unmatched_label);
3008 case PM_LOCAL_VARIABLE_TARGET_NODE: {
3019 if (in_alternation_pattern) {
3020 ID id = pm_constant_id_lookup(scope_node, cast->
name);
3021 const char *name = rb_id2name(
id);
3023 if (name && strlen(name) > 0 && name[0] !=
'_') {
3024 COMPILE_ERROR(iseq, location.
line,
"illegal variable in alternative pattern (%"PRIsVALUE
")", rb_id2str(
id));
3029 PUSH_SETLOCAL(ret, location, index.index, index.level);
3030 PUSH_INSNL(ret, location, jump, matched_label);
3033 case PM_ALTERNATION_PATTERN_NODE: {
3038 LABEL *matched_left_label = NEW_LABEL(location.
line);
3039 LABEL *unmatched_left_label = NEW_LABEL(location.
line);
3043 PUSH_INSN(ret, location, dup);
3044 CHECK(pm_compile_pattern(iseq, scope_node, cast->
left, ret, matched_left_label, unmatched_left_label, in_single_pattern,
true, use_deconstructed_cache, base_index + 1));
3049 PUSH_LABEL(ret, matched_left_label);
3050 PUSH_INSN(ret, location, pop);
3051 PUSH_INSNL(ret, location, jump, matched_label);
3052 PUSH_INSN(ret, location, putnil);
3056 PUSH_LABEL(ret, unmatched_left_label);
3057 CHECK(pm_compile_pattern(iseq, scope_node, cast->
right, ret, matched_label, unmatched_label, in_single_pattern,
true, use_deconstructed_cache, base_index));
3060 case PM_PARENTHESES_NODE:
3065 return pm_compile_pattern(iseq, scope_node, ((
const pm_parentheses_node_t *) node)->body, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index);
3066 case PM_PINNED_EXPRESSION_NODE:
3074 case PM_CLASS_VARIABLE_READ_NODE:
3075 case PM_CONSTANT_PATH_NODE:
3076 case PM_CONSTANT_READ_NODE:
3079 case PM_GLOBAL_VARIABLE_READ_NODE:
3080 case PM_IMAGINARY_NODE:
3081 case PM_INSTANCE_VARIABLE_READ_NODE:
3082 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3083 case PM_INTEGER_NODE:
3084 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
3085 case PM_INTERPOLATED_STRING_NODE:
3086 case PM_INTERPOLATED_SYMBOL_NODE:
3087 case PM_INTERPOLATED_X_STRING_NODE:
3088 case PM_LAMBDA_NODE:
3089 case PM_LOCAL_VARIABLE_READ_NODE:
3091 case PM_SOURCE_ENCODING_NODE:
3092 case PM_SOURCE_FILE_NODE:
3093 case PM_SOURCE_LINE_NODE:
3095 case PM_RATIONAL_NODE:
3096 case PM_REGULAR_EXPRESSION_NODE:
3098 case PM_STRING_NODE:
3099 case PM_SYMBOL_NODE:
3101 case PM_X_STRING_NODE: {
3105 PM_COMPILE_NOT_POPPED(node);
3106 if (in_single_pattern) {
3107 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
3110 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
3112 if (in_single_pattern) {
3113 pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
3116 PUSH_INSNL(ret, location, branchif, matched_label);
3117 PUSH_INSNL(ret, location, jump, unmatched_label);
3120 case PM_PINNED_VARIABLE_NODE: {
3126 CHECK(pm_compile_pattern(iseq, scope_node, cast->
variable, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern,
true, base_index));
3130 case PM_UNLESS_NODE: {
3145 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3160 CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
3161 PM_COMPILE_NOT_POPPED(predicate);
3163 if (in_single_pattern) {
3164 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
3166 PUSH_INSN(ret, location, dup);
3167 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3168 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
3171 PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
3175 VALUE operand = rb_fstring_lit(
"guard clause does not return true");
3176 PUSH_INSN1(ret, location, putobject, operand);
3179 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
3180 PUSH_INSN1(ret, location, putobject,
Qfalse);
3181 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
3183 PUSH_INSN(ret, location, pop);
3184 PUSH_INSN(ret, location, pop);
3186 PUSH_LABEL(ret, match_succeeded_label);
3189 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3190 PUSH_INSNL(ret, location, branchunless, unmatched_label);
3193 PUSH_INSNL(ret, location, branchif, unmatched_label);
3196 PUSH_INSNL(ret, location, jump, matched_label);
3203 rb_bug(
"Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
3210#undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
3211#undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
3212#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
3213#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
3214#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
3224 scope->base.
type = PM_SCOPE_NODE;
3228 scope->previous = previous;
3232 scope->parser = previous->parser;
3233 scope->encoding = previous->encoding;
3235 scope->constants = previous->constants;
3236 scope->coverage_enabled = previous->coverage_enabled;
3240 switch (PM_NODE_TYPE(node)) {
3241 case PM_BLOCK_NODE: {
3243 scope->body = cast->
body;
3244 scope->locals = cast->
locals;
3248 case PM_CLASS_NODE: {
3250 scope->body = cast->
body;
3251 scope->locals = cast->
locals;
3257 scope->body = cast->body;
3258 scope->locals = cast->locals;
3261 case PM_ENSURE_NODE: {
3277 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
3282 case PM_LAMBDA_NODE: {
3285 scope->body = cast->
body;
3286 scope->locals = cast->
locals;
3296 case PM_MODULE_NODE: {
3298 scope->body = cast->
body;
3299 scope->locals = cast->
locals;
3302 case PM_POST_EXECUTION_NODE: {
3307 case PM_PROGRAM_NODE: {
3310 scope->locals = cast->locals;
3313 case PM_RESCUE_NODE: {
3318 case PM_RESCUE_MODIFIER_NODE: {
3323 case PM_SINGLETON_CLASS_NODE: {
3325 scope->body = cast->
body;
3326 scope->locals = cast->
locals;
3329 case PM_STATEMENTS_NODE: {
3335 rb_bug(
"unreachable");
3343 if (scope_node->index_lookup_table) {
3344 st_free_table(scope_node->index_lookup_table);
3364 iobj = IS_INSN(last_elem) ? (
INSN*) last_elem : (
INSN*) get_prev_insn((
INSN*) last_elem);
3365 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
3366 iobj = (
INSN*) get_prev_insn(iobj);
3368 ELEM_INSERT_NEXT(&iobj->link, (
LINK_ELEMENT*) retry_end_l);
3373 if (&iobj->link == LAST_ELEMENT(ret)) {
3381 const char *name = rb_id2name(method_id);
3382 static const char prefix[] =
"__builtin_";
3383 const size_t prefix_len =
sizeof(prefix) - 1;
3385 if (receiver == NULL) {
3386 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
3388 return &name[prefix_len];
3391 else if (PM_NODE_TYPE_P(receiver, PM_CALL_NODE)) {
3392 if (PM_NODE_FLAG_P(receiver, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3400 else if (PM_NODE_TYPE_P(receiver, PM_CONSTANT_READ_NODE)) {
3415 if (arguments == NULL) {
3416 COMPILE_ERROR(iseq, node_location->
line,
"attr!: no argument");
3422 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3423 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to attr!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3427 VALUE symbol = pm_static_literal_value(iseq, argument, scope_node);
3430 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
3431 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
3433 else if (strcmp(RSTRING_PTR(
string),
"inline_block") == 0) {
3434 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
3436 else if (strcmp(RSTRING_PTR(
string),
"use_block") == 0) {
3437 iseq_set_use_block(iseq);
3439 else if (strcmp(RSTRING_PTR(
string),
"c_trace") == 0) {
3441 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
3444 COMPILE_ERROR(iseq, node_location->
line,
"unknown argument to attr!: %s", RSTRING_PTR(
string));
3455 if (arguments == NULL) {
3456 COMPILE_ERROR(iseq, node_location->
line,
"arg!: no argument");
3461 COMPILE_ERROR(iseq, node_location->
line,
"arg!: too many argument");
3466 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3467 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to arg!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3473 int index = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq, name);
3475 debugs(
"id: %s idx: %d\n", rb_id2name(name), index);
3476 PUSH_GETLOCAL(ret, *node_location, index, get_lvar_level(iseq));
3485 const pm_node_t *ast_node = scope_node->ast_node;
3486 if (!PM_NODE_TYPE_P(ast_node, PM_DEF_NODE)) {
3487 rb_bug(
"mandatory_only?: not in method definition");
3493 if (parameters_node == NULL) {
3494 rb_bug(
"mandatory_only?: in method definition with no parameters");
3499 if (body_node == NULL || !PM_NODE_TYPE_P(body_node, PM_STATEMENTS_NODE) || (((
const pm_statements_node_t *) body_node)->body.size != 1) || !PM_NODE_TYPE_P(((
const pm_statements_node_t *) body_node)->body.nodes[0], PM_IF_NODE)) {
3500 rb_bug(
"mandatory_only?: not in method definition with plain statements");
3506 rb_bug(
"mandatory_only?: can't find mandatory node");
3517 .name = def_node->
name,
3519 .parameters = ¶meters,
3529 pm_scope_node_init(&def.base, &next_scope_node, scope_node);
3532 const rb_iseq_t *mandatory_only_iseq = pm_iseq_new_with_opt(
3534 rb_iseq_base_label(iseq),
3536 rb_iseq_realpath(iseq),
3537 node_location->
line,
3541 ISEQ_COMPILE_DATA(iseq)->option,
3544 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
3547 RUBY_ASSERT(ISEQ_BODY(iseq)->mandatory_only_iseq == NULL);
3548 rb_jump_tag(error_state);
3551 pm_scope_node_destroy(&next_scope_node);
3560 if (parent_block != NULL) {
3561 COMPILE_ERROR(iseq, node_location->
line,
"should not call builtins here.");
3565#define BUILTIN_INLINE_PREFIX "_bi"
3566 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
3567 bool cconst =
false;
3572 if (strcmp(
"cstmt!", builtin_func) == 0 || strcmp(
"cexpr!", builtin_func) == 0) {
3575 else if (strcmp(
"cconst!", builtin_func) == 0) {
3578 else if (strcmp(
"cinit!", builtin_func) == 0) {
3582 else if (strcmp(
"attr!", builtin_func) == 0) {
3583 return pm_compile_builtin_attr(iseq, scope_node, arguments, node_location);
3585 else if (strcmp(
"arg!", builtin_func) == 0) {
3586 return pm_compile_builtin_arg(iseq, ret, scope_node, arguments, node_location, popped);
3588 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
3590 rb_bug(
"mandatory_only? should be in if condition");
3592 else if (!LIST_INSN_SIZE_ZERO(ret)) {
3593 rb_bug(
"mandatory_only? should be put on top");
3596 PUSH_INSN1(ret, *node_location, putobject,
Qfalse);
3597 return pm_compile_builtin_mandatory_only_method(iseq, scope_node, call_node, node_location);
3600 rb_bug(
"can't find builtin function:%s", builtin_func);
3603 COMPILE_ERROR(iseq, node_location->
line,
"can't find builtin function:%s", builtin_func);
3607 int inline_index = node_location->
line;
3608 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
3609 builtin_func = inline_func;
3615 typedef VALUE(*builtin_func0)(
void *,
VALUE);
3616 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
3617 PUSH_INSN1(ret, *node_location, putobject, const_val);
3623 DECL_ANCHOR(args_seq);
3627 int argc = pm_setup_args(arguments, call_node->
block, &flags, &keywords, iseq, args_seq, scope_node, node_location);
3629 if (argc != bf->argc) {
3630 COMPILE_ERROR(iseq, node_location->
line,
"argc is not match for builtin function:%s (expect %d but %d)", builtin_func, bf->argc, argc);
3634 unsigned int start_index;
3635 if (delegate_call_p(iseq, argc, args_seq, &start_index)) {
3636 PUSH_INSN2(ret, *node_location, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
3639 PUSH_SEQ(ret, args_seq);
3640 PUSH_INSN1(ret, *node_location, invokebuiltin, bf);
3643 if (popped) PUSH_INSN(ret, *node_location, pop);
3658 LABEL *else_label = NEW_LABEL(location.
line);
3659 LABEL *end_label = NEW_LABEL(location.
line);
3660 LABEL *retry_end_l = NEW_LABEL(location.
line);
3664 int node_id = location.
node_id;
3666 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3667 if (PM_BRANCH_COVERAGE_P(iseq)) {
3668 const uint8_t *cursors[3] = {
3674 const uint8_t *end_cursor = cursors[0];
3675 end_cursor = (end_cursor == NULL || cursors[1] == NULL) ? cursors[1] : (end_cursor > cursors[1] ? end_cursor : cursors[1]);
3676 end_cursor = (end_cursor == NULL || cursors[2] == NULL) ? cursors[2] : (end_cursor > cursors[2] ? end_cursor : cursors[2]);
3679 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, call_node);
3683 .beg_pos = { .lineno = start_location.
line, .column = start_location.
column },
3684 .end_pos = { .lineno = end_location.
line, .column = end_location.
column }
3687 branches = decl_branch_base(iseq, PTR2NUM(call_node), &code_location,
"&.");
3690 PUSH_INSN(ret, location, dup);
3691 PUSH_INSNL(ret, location, branchnil, else_label);
3693 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0,
"then", branches);
3701 int orig_argc = pm_setup_args(call_node->
arguments, call_node->
block, &flags, &kw_arg, iseq, ret, scope_node, &location);
3702 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
3705 if (call_node->
block != NULL && PM_NODE_TYPE_P(call_node->
block, PM_BLOCK_NODE)) {
3708 pm_scope_node_init(call_node->
block, &next_scope_node, scope_node);
3710 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, pm_node_line_number(scope_node->parser, call_node->
block));
3711 pm_scope_node_destroy(&next_scope_node);
3712 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
3715 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3716 flags |= VM_CALL_VCALL;
3720 flags |= VM_CALL_ARGS_SIMPLE;
3724 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
3725 flags |= VM_CALL_FCALL;
3728 if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
3729 if (flags & VM_CALL_ARGS_BLOCKARG) {
3730 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
3731 if (flags & VM_CALL_ARGS_SPLAT) {
3732 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3733 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3735 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 3));
3736 PUSH_INSN(ret, location, pop);
3738 else if (flags & VM_CALL_ARGS_SPLAT) {
3739 PUSH_INSN(ret, location, dup);
3740 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3741 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3742 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 2));
3743 PUSH_INSN(ret, location, pop);
3746 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 1));
3750 if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
3751 PUSH_INSN(ret, location, splatkw);
3754 LABEL *not_basic_new = NEW_LABEL(location.
line);
3755 LABEL *not_basic_new_finish = NEW_LABEL(location.
line);
3757 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
3758 method_id == rb_intern(
"new") &&
3759 call_node->
block == NULL &&
3760 (flags & VM_CALL_ARGS_BLOCKARG) == 0;
3763 if (LAST_ELEMENT(ret) == opt_new_prelude) {
3764 PUSH_INSN(ret, location, putnil);
3765 PUSH_INSN(ret, location, swap);
3768 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.
line, location.
node_id, BIN(swap), 0)->link);
3769 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.
line, location.
node_id, BIN(putnil), 0)->link);
3774 if (flags & VM_CALL_FORWARDING) {
3775 ci = (
VALUE)new_callinfo(iseq, method_id, orig_argc + 1, flags, kw_arg, 0);
3778 ci = (
VALUE)new_callinfo(iseq, method_id, orig_argc, flags, kw_arg, 0);
3781 PUSH_INSN2(ret, location, opt_new, ci, not_basic_new);
3782 LABEL_REF(not_basic_new);
3784 PUSH_SEND_R(ret, location, rb_intern(
"initialize"),
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags | VM_CALL_FCALL), kw_arg);
3785 PUSH_INSNL(ret, location, jump, not_basic_new_finish);
3787 PUSH_LABEL(ret, not_basic_new);
3789 PUSH_SEND_R(ret, location, method_id,
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags), kw_arg);
3790 PUSH_INSN(ret, location, swap);
3792 PUSH_LABEL(ret, not_basic_new_finish);
3793 PUSH_INSN(ret, location, pop);
3796 PUSH_SEND_R(ret, location, method_id,
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags), kw_arg);
3799 if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
3800 pm_compile_retry_end_label(iseq, ret, retry_end_l);
3801 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, start, retry_end_l, block_iseq, retry_end_l);
3804 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3805 PUSH_INSNL(ret, location, jump, end_label);
3806 PUSH_LABEL(ret, else_label);
3807 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 1,
"else", branches);
3808 PUSH_LABEL(ret, end_label);
3811 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
3812 PUSH_INSN(ret, location, pop);
3815 if (popped) PUSH_INSN(ret, location, pop);
3816 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
3846#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
3849 enum defined_type dtype = DEFINED_NOT_DEFINED;
3852 switch (PM_NODE_TYPE(node)) {
3857 dtype = DEFINED_NIL;
3860 case PM_INSTANCE_VARIABLE_READ_NODE: {
3864 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3866 PUSH_INSN3(ret, location, definedivar,
ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
3871 case PM_LOCAL_VARIABLE_READ_NODE:
3874 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3877 dtype = DEFINED_LVAR;
3880 case PM_GLOBAL_VARIABLE_READ_NODE: {
3884 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3886 PUSH_INSN(ret, location, putnil);
3887 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR),
ID2SYM(name), PUSH_VAL(DEFINED_GVAR));
3892 case PM_CLASS_VARIABLE_READ_NODE: {
3896 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3898 PUSH_INSN(ret, location, putnil);
3899 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR),
ID2SYM(name), PUSH_VAL(DEFINED_CVAR));
3904 case PM_CONSTANT_READ_NODE: {
3908 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3910 PUSH_INSN(ret, location, putnil);
3911 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
3919 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
3921 PUSH_INSN(ret, location, putnil);
3922 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
3926 case PM_SUPER_NODE: {
3931 if (cast->
block != NULL && !PM_NODE_TYPE_P(cast->
block, PM_BLOCK_ARGUMENT_NODE)) {
3932 dtype = DEFINED_EXPR;
3936 PUSH_INSN(ret, location, putnil);
3937 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3940 case PM_FORWARDING_SUPER_NODE: {
3945 if (cast->
block != NULL) {
3946 dtype = DEFINED_EXPR;
3950 PUSH_INSN(ret, location, putnil);
3951 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3958 dtype = DEFINED_SELF;
3964 dtype = DEFINED_TRUE;
3970 dtype = DEFINED_FALSE;
3973 case PM_CALL_AND_WRITE_NODE:
3976 case PM_CALL_OPERATOR_WRITE_NODE:
3979 case PM_CALL_OR_WRITE_NODE:
3982 case PM_CLASS_VARIABLE_AND_WRITE_NODE:
3985 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
3988 case PM_CLASS_VARIABLE_OR_WRITE_NODE:
3991 case PM_CLASS_VARIABLE_WRITE_NODE:
3994 case PM_CONSTANT_AND_WRITE_NODE:
3997 case PM_CONSTANT_OPERATOR_WRITE_NODE:
4000 case PM_CONSTANT_OR_WRITE_NODE:
4003 case PM_CONSTANT_PATH_AND_WRITE_NODE:
4006 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
4009 case PM_CONSTANT_PATH_OR_WRITE_NODE:
4012 case PM_CONSTANT_PATH_WRITE_NODE:
4015 case PM_CONSTANT_WRITE_NODE:
4018 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
4021 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
4024 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
4027 case PM_GLOBAL_VARIABLE_WRITE_NODE:
4030 case PM_INDEX_AND_WRITE_NODE:
4033 case PM_INDEX_OPERATOR_WRITE_NODE:
4036 case PM_INDEX_OR_WRITE_NODE:
4039 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
4042 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
4045 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
4048 case PM_INSTANCE_VARIABLE_WRITE_NODE:
4051 case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
4054 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
4057 case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
4060 case PM_LOCAL_VARIABLE_WRITE_NODE:
4063 case PM_MULTI_WRITE_NODE:
4066 dtype = DEFINED_ASGN;
4069 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
4072 case PM_ALIAS_METHOD_NODE:
4081 case PM_CASE_MATCH_NODE:
4093 case PM_DEFINED_NODE:
4096 case PM_FLIP_FLOP_NODE:
4108 case PM_IMAGINARY_NODE:
4111 case PM_INTEGER_NODE:
4114 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE:
4117 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
4120 case PM_INTERPOLATED_STRING_NODE:
4123 case PM_INTERPOLATED_SYMBOL_NODE:
4126 case PM_INTERPOLATED_X_STRING_NODE:
4129 case PM_LAMBDA_NODE:
4132 case PM_MATCH_LAST_LINE_NODE:
4135 case PM_MATCH_PREDICATE_NODE:
4138 case PM_MATCH_REQUIRED_NODE:
4141 case PM_MATCH_WRITE_NODE:
4144 case PM_MODULE_NODE:
4153 case PM_POST_EXECUTION_NODE:
4159 case PM_RATIONAL_NODE:
4165 case PM_REGULAR_EXPRESSION_NODE:
4168 case PM_RESCUE_MODIFIER_NODE:
4174 case PM_RETURN_NODE:
4177 case PM_SINGLETON_CLASS_NODE:
4180 case PM_SOURCE_ENCODING_NODE:
4183 case PM_SOURCE_FILE_NODE:
4186 case PM_SOURCE_LINE_NODE:
4189 case PM_STRING_NODE:
4192 case PM_SYMBOL_NODE:
4198 case PM_UNLESS_NODE:
4207 case PM_X_STRING_NODE:
4210 dtype = DEFINED_EXPR;
4213 case PM_BACK_REFERENCE_READ_NODE: {
4217 VALUE ref = pm_compile_back_reference_ref(cast);
4219 PUSH_INSN(ret, location, putnil);
4220 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4224 case PM_NUMBERED_REFERENCE_READ_NODE: {
4228 VALUE ref = pm_compile_numbered_reference_ref(cast);
4230 PUSH_INSN(ret, location, putnil);
4231 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4236 case PM_CONSTANT_PATH_NODE: {
4240 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4242 if (cast->
parent != NULL) {
4243 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4244 pm_compile_defined_expr0(iseq, cast->
parent, node_location, ret, popped, scope_node,
true, lfinish,
false);
4246 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4247 PM_COMPILE(cast->
parent);
4250 PUSH_INSN1(ret, location, putobject, rb_cObject);
4253 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
4257 case PM_BEGIN_NODE: {
4265 dtype = DEFINED_NIL;
4271 pm_compile_defined_expr0(iseq, cast->
statements->
body.
nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4277 dtype = DEFINED_EXPR;
4282 dtype = DEFINED_EXPR;
4287 case PM_PARENTHESES_NODE: {
4292 if (cast->
body == NULL) {
4294 dtype = DEFINED_NIL;
4296 else if (PM_NODE_TYPE_P(cast->
body, PM_STATEMENTS_NODE) && !PM_NODE_FLAG_P(cast, PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS)) {
4299 pm_compile_defined_expr0(iseq, ((
const pm_statements_node_t *) cast->
body)->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4305 dtype = DEFINED_EXPR;
4310 case PM_ARRAY_NODE: {
4316 lfinish[1] = NEW_LABEL(location.
line);
4319 for (
size_t index = 0; index < cast->
elements.
size; index++) {
4320 pm_compile_defined_expr0(iseq, cast->
elements.
nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4321 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4324 dtype = DEFINED_EXPR;
4330 case PM_KEYWORD_HASH_NODE: {
4335 if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
4342 if (elements->
size > 0 && !lfinish[1]) {
4343 lfinish[1] = NEW_LABEL(location.
line);
4346 for (
size_t index = 0; index < elements->
size; index++) {
4347 pm_compile_defined_expr0(iseq, elements->
nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4348 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4351 dtype = DEFINED_EXPR;
4354 case PM_ASSOC_NODE: {
4359 pm_compile_defined_expr0(iseq, cast->
key, node_location, ret, popped, scope_node,
true, lfinish,
false);
4360 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4361 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4365 case PM_ASSOC_SPLAT_NODE: {
4370 if (cast->
value == NULL) {
4371 dtype = DEFINED_EXPR;
4375 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4378 case PM_IMPLICIT_NODE: {
4382 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4385 case PM_CALL_NODE: {
4386#define BLOCK_P(cast) ((cast)->block != NULL && PM_NODE_TYPE_P((cast)->block, PM_BLOCK_NODE))
4392 if (BLOCK_P(cast)) {
4393 dtype = DEFINED_EXPR;
4398 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4399 if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.
line);
4403 pm_compile_defined_expr0(iseq, (
const pm_node_t *) cast->
arguments, node_location, ret, popped, scope_node,
true, lfinish,
false);
4404 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4407 if (cast->
block && PM_NODE_TYPE_P(cast->
block, PM_BLOCK_ARGUMENT_NODE)) {
4408 pm_compile_defined_expr0(iseq, cast->
block, node_location, ret, popped, scope_node,
true, lfinish,
false);
4409 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4416 pm_compile_defined_expr0(iseq, cast->
receiver, node_location, ret, popped, scope_node,
true, lfinish,
true);
4417 PUSH_INSNL(ret, location, branchunless, lfinish[2]);
4420 ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
4422 pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
4425 pm_compile_defined_expr0(iseq, cast->
receiver, node_location, ret, popped, scope_node,
true, lfinish,
false);
4426 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4430 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
4432 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4433 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_METHOD),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4436 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
4438 PUSH_INSN(ret, location, putself);
4439 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4441 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_FUNC),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4448 case PM_ARGUMENTS_NODE: {
4453 for (
size_t index = 0; index < cast->
arguments.
size; index++) {
4454 pm_compile_defined_expr0(iseq, cast->
arguments.
nodes[index], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4455 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4458 dtype = DEFINED_EXPR;
4461 case PM_BLOCK_ARGUMENT_NODE:
4464 dtype = DEFINED_EXPR;
4466 case PM_FORWARDING_ARGUMENTS_NODE:
4469 dtype = DEFINED_EXPR;
4471 case PM_SPLAT_NODE: {
4477 dtype = DEFINED_EXPR;
4481 pm_compile_defined_expr0(iseq, cast->
expression, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4483 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4484 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4486 dtype = DEFINED_EXPR;
4489 case PM_SHAREABLE_CONSTANT_NODE:
4493 pm_compile_defined_expr0(iseq, ((
const pm_shareable_constant_node_t *) node)->write, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
4496 case PM_BLOCK_LOCAL_VARIABLE_NODE:
4497 case PM_BLOCK_PARAMETER_NODE:
4498 case PM_BLOCK_PARAMETERS_NODE:
4499 case PM_FORWARDING_PARAMETER_NODE:
4500 case PM_IMPLICIT_REST_NODE:
4501 case PM_IT_PARAMETERS_NODE:
4502 case PM_PARAMETERS_NODE:
4503 case PM_KEYWORD_REST_PARAMETER_NODE:
4504 case PM_NO_KEYWORDS_PARAMETER_NODE:
4505 case PM_NUMBERED_PARAMETERS_NODE:
4506 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE:
4507 case PM_OPTIONAL_PARAMETER_NODE:
4508 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
4509 case PM_REQUIRED_PARAMETER_NODE:
4510 case PM_REST_PARAMETER_NODE:
4512 case PM_ALTERNATION_PATTERN_NODE:
4513 case PM_ARRAY_PATTERN_NODE:
4514 case PM_CAPTURE_PATTERN_NODE:
4515 case PM_FIND_PATTERN_NODE:
4516 case PM_HASH_PATTERN_NODE:
4517 case PM_PINNED_EXPRESSION_NODE:
4518 case PM_PINNED_VARIABLE_NODE:
4520 case PM_CALL_TARGET_NODE:
4521 case PM_CLASS_VARIABLE_TARGET_NODE:
4522 case PM_CONSTANT_PATH_TARGET_NODE:
4523 case PM_CONSTANT_TARGET_NODE:
4524 case PM_GLOBAL_VARIABLE_TARGET_NODE:
4525 case PM_INDEX_TARGET_NODE:
4526 case PM_INSTANCE_VARIABLE_TARGET_NODE:
4527 case PM_LOCAL_VARIABLE_TARGET_NODE:
4528 case PM_MULTI_TARGET_NODE:
4531 case PM_ENSURE_NODE:
4533 case PM_RESCUE_NODE:
4537 case PM_EMBEDDED_STATEMENTS_NODE:
4538 case PM_EMBEDDED_VARIABLE_NODE:
4539 case PM_MISSING_NODE:
4540 case PM_PRE_EXECUTION_NODE:
4541 case PM_PROGRAM_NODE:
4543 case PM_STATEMENTS_NODE:
4544 rb_bug(
"Unreachable node in defined?: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4548 PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
4557 pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4560 LABEL *lstart = NEW_LABEL(node_location->
line);
4561 LABEL *lend = NEW_LABEL(node_location->
line);
4564 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
4566 const rb_iseq_t *rescue = new_child_iseq_with_callback(
4575 lstart->rescued = LABEL_RESCUE_BEG;
4576 lend->rescued = LABEL_RESCUE_END;
4578 APPEND_LABEL(ret, lcur, lstart);
4579 PUSH_LABEL(ret, lend);
4580 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
4590 lfinish[0] = NEW_LABEL(node_location->
line);
4595 pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish);
4599 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, node_location->
line, node_location->
node_id, BIN(putnil), 0)->link);
4600 PUSH_INSN(ret, *node_location, swap);
4602 if (lfinish[2]) PUSH_LABEL(ret, lfinish[2]);
4603 PUSH_INSN(ret, *node_location, pop);
4604 PUSH_LABEL(ret, lfinish[1]);
4608 PUSH_LABEL(ret, lfinish[0]);
4619 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
4621 DECL_ANCHOR(ensure);
4624 if (enlp->erange != NULL) {
4625 DECL_ANCHOR(ensure_part);
4626 LABEL *lstart = NEW_LABEL(0);
4627 LABEL *lend = NEW_LABEL(0);
4629 add_ensure_range(iseq, enlp->erange, lstart, lend);
4631 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
4632 PUSH_LABEL(ensure_part, lstart);
4634 PM_COMPILE_INTO_ANCHOR(ensure_part, (
const pm_node_t *) enlp->ensure_node);
4635 PUSH_LABEL(ensure_part, lend);
4636 PUSH_SEQ(ensure, ensure_part);
4645 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
4646 PUSH_SEQ(ret, ensure);
4656pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
4664 int local_index = ctx->local_index;
4666 ID local = pm_constant_id_lookup(scope_node, constant_id);
4667 local_table_for_iseq->ids[local_index] = local;
4669 *value = (st_data_t)local_index;
4685 RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
4687 ID local = pm_constant_id_lookup(scope_node, constant_id);
4688 local_table_for_iseq->ids[local_index] = local;
4689 st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
4699 local_table_for_iseq->ids[local_index] = local_name;
4700 st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
4712 for (
size_t index = 0; index < node->
lefts.
size; index++) {
4715 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4716 if (!PM_NODE_FLAG_P(left, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4717 pm_insert_local_index(((
const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4722 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4723 local_index = pm_compile_destructured_param_locals((
const pm_multi_target_node_t *) left, index_lookup_table, local_table_for_iseq, scope_node, local_index);
4727 if (node->
rest != NULL && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE)) {
4730 if (rest->expression != NULL) {
4731 RUBY_ASSERT(PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE));
4733 if (!PM_NODE_FLAG_P(rest->expression, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4734 pm_insert_local_index(((
const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4740 for (
size_t index = 0; index < node->
rights.
size; index++) {
4743 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4744 if (!PM_NODE_FLAG_P(right, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4745 pm_insert_local_index(((
const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4750 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4751 local_index = pm_compile_destructured_param_locals((
const pm_multi_target_node_t *) right, index_lookup_table, local_table_for_iseq, scope_node, local_index);
4765 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4767 PUSH_SETLOCAL(ret, location, index.index, index.level);
4781 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4782 bool has_rest = (node->
rest && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE) && (((
const pm_splat_node_t *) node->
rest)->expression) != NULL);
4785 int flag = (has_rest || has_rights) ? 1 : 0;
4788 for (
size_t index = 0; index < node->
lefts.
size; index++) {
4791 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4795 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4806 RUBY_ASSERT(PM_NODE_TYPE_P(rest, PM_REQUIRED_PARAMETER_NODE));
4816 for (
size_t index = 0; index < node->
rights.
size; index++) {
4819 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4823 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4883 node->stack_index = state->stack_size + 1;
4884 node->stack_size = stack_size;
4885 node->position = state->position;
4888 if (state->head == NULL) {
4893 state->tail->next = node;
4897 state->stack_size += stack_size;
4910 if (state->stack_size == 0)
return;
4915 while (current != NULL) {
4916 VALUE offset =
INT2FIX(state->stack_size - current->stack_index + current->position);
4917 current->topn->operands[0] = offset;
4923 if (current->stack_size > 1) {
4924 INSN *insn = current->topn;
4926 for (
size_t index = 1; index < current->stack_size; index += 1) {
4930 insn = (
INSN *) element;
4933 insn->operands[0] = offset;
4938 current = current->next;
4978 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4980 switch (PM_NODE_TYPE(node)) {
4981 case PM_LOCAL_VARIABLE_TARGET_NODE: {
4990 PUSH_SETLOCAL(writes, location, index.index, index.level);
4993 case PM_CLASS_VARIABLE_TARGET_NODE: {
5000 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5003 PUSH_INSN2(writes, location, setclassvariable, operand, get_cvar_ic_value(iseq, name));
5006 case PM_CONSTANT_TARGET_NODE: {
5013 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5016 PUSH_INSN1(writes, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5017 PUSH_INSN1(writes, location, setconstant, operand);
5020 case PM_GLOBAL_VARIABLE_TARGET_NODE: {
5027 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5030 PUSH_INSN1(writes, location, setglobal, operand);
5033 case PM_INSTANCE_VARIABLE_TARGET_NODE: {
5040 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5043 PUSH_INSN2(writes, location, setinstancevariable, operand, get_ivar_ic_value(iseq, name));
5046 case PM_CONSTANT_PATH_TARGET_NODE: {
5056 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5058 if (cast->
parent != NULL) {
5059 pm_compile_node(iseq, cast->
parent, parents,
false, scope_node);
5062 PUSH_INSN1(parents, location, putobject, rb_cObject);
5065 if (state == NULL) {
5066 PUSH_INSN(writes, location, swap);
5069 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5070 pm_multi_target_state_push(state, (
INSN *) LAST_ELEMENT(writes), 1);
5074 PUSH_INSN1(writes, location, setconstant, operand);
5076 if (state != NULL) {
5077 PUSH_INSN(cleanup, location, pop);
5082 case PM_CALL_TARGET_NODE: {
5092 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
5094 pm_compile_node(iseq, cast->
receiver, parents,
false, scope_node);
5096 LABEL *safe_label = NULL;
5097 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
5098 safe_label = NEW_LABEL(location.
line);
5099 PUSH_INSN(parents, location, dup);
5100 PUSH_INSNL(parents, location, branchnil, safe_label);
5103 if (state != NULL) {
5104 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5105 pm_multi_target_state_push(state, (
INSN *) LAST_ELEMENT(writes), 1);
5106 PUSH_INSN(writes, location, swap);
5109 int flags = VM_CALL_ARGS_SIMPLE;
5110 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
5112 PUSH_SEND_WITH_FLAG(writes, location, method_id,
INT2FIX(1),
INT2FIX(flags));
5113 if (safe_label != NULL && state == NULL) PUSH_LABEL(writes, safe_label);
5114 PUSH_INSN(writes, location, pop);
5115 if (safe_label != NULL && state != NULL) PUSH_LABEL(writes, safe_label);
5117 if (state != NULL) {
5118 PUSH_INSN(cleanup, location, pop);
5123 case PM_INDEX_TARGET_NODE: {
5135 pm_compile_node(iseq, cast->
receiver, parents,
false, scope_node);
5139 int argc = pm_setup_args(cast->
arguments, (
const pm_node_t *) cast->
block, &flags, &kwargs, iseq, parents, scope_node, &location);
5141 if (state != NULL) {
5142 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5143 pm_multi_target_state_push(state, (
INSN *) LAST_ELEMENT(writes), argc + 1);
5146 PUSH_INSN(writes, location, swap);
5149 for (
int index = 0; index < argc; index++) {
5150 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5152 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5160 int ci_argc = argc + 1;
5161 if (flags & VM_CALL_ARGS_SPLAT) {
5163 PUSH_INSN1(writes, location, newarray,
INT2FIX(1));
5164 PUSH_INSN(writes, location, concatarray);
5167 PUSH_SEND_R(writes, location, idASET,
INT2NUM(ci_argc), NULL,
INT2FIX(flags), kwargs);
5168 PUSH_INSN(writes, location, pop);
5170 if (state != NULL) {
5172 PUSH_INSN(writes, location, pop);
5175 for (
int index = 0; index < argc + 1; index++) {
5176 PUSH_INSN(cleanup, location, pop);
5182 case PM_MULTI_TARGET_NODE: {
5189 size_t before_position;
5190 if (state != NULL) {
5191 before_position = state->position;
5195 pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
5196 if (state != NULL) state->position = before_position;
5200 case PM_SPLAT_NODE: {
5209 pm_compile_target_node(iseq, cast->
expression, parents, writes, cleanup, scope_node, state);
5215 rb_bug(
"Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5228 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5233 switch (PM_NODE_TYPE(node)) {
5234 case PM_MULTI_TARGET_NODE: {
5236 lefts = &cast->
lefts;
5241 case PM_MULTI_WRITE_NODE: {
5243 lefts = &cast->
lefts;
5249 rb_bug(
"Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5253 bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *) rest)->expression != NULL;
5254 bool has_posts = rights->
size > 0;
5259 PUSH_INSN2(writes, location, expandarray,
INT2FIX(lefts->
size),
INT2FIX((has_rest || has_posts) ? 1 : 0));
5265 if (state == NULL) state = &target_state;
5267 size_t base_position = state->position;
5268 size_t splat_position = (has_rest || has_posts) ? 1 : 0;
5271 for (
size_t index = 0; index < lefts->
size; index++) {
5273 state->position = lefts->
size - index + splat_position + base_position;
5274 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5280 state->position = 1 + rights->
size + base_position;
5286 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5291 if (!has_rest && rest != NULL) {
5295 for (
size_t index = 0; index < rights->
size; index++) {
5297 state->position = rights->
size - index + base_position;
5298 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5311 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5313 switch (PM_NODE_TYPE(node)) {
5314 case PM_LOCAL_VARIABLE_TARGET_NODE: {
5317 PUSH_GETLOCAL(ret, location, 1, 0);
5318 pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
5321 case PM_CLASS_VARIABLE_TARGET_NODE:
5322 case PM_CONSTANT_TARGET_NODE:
5323 case PM_GLOBAL_VARIABLE_TARGET_NODE:
5324 case PM_INSTANCE_VARIABLE_TARGET_NODE:
5325 case PM_CONSTANT_PATH_TARGET_NODE:
5326 case PM_CALL_TARGET_NODE:
5327 case PM_INDEX_TARGET_NODE:
5328 case PM_SPLAT_NODE: {
5332 DECL_ANCHOR(writes);
5333 DECL_ANCHOR(cleanup);
5337 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
5339 PUSH_GETLOCAL(ret, location, 1, 0);
5342 PUSH_SEQ(ret, writes);
5343 PUSH_SEQ(ret, cleanup);
5345 pm_multi_target_state_update(&state);
5348 case PM_MULTI_TARGET_NODE: {
5349 DECL_ANCHOR(writes);
5350 DECL_ANCHOR(cleanup);
5352 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
5354 LABEL *not_single = NEW_LABEL(location.
line);
5355 LABEL *not_ary = NEW_LABEL(location.
line);
5363 PUSH_GETLOCAL(ret, location, 1, 0);
5364 PUSH_INSN(ret, location, dup);
5365 PUSH_CALL(ret, location, idLength,
INT2FIX(0));
5366 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
5367 PUSH_CALL(ret, location, idEq,
INT2FIX(1));
5368 PUSH_INSNL(ret, location, branchunless, not_single);
5369 PUSH_INSN(ret, location, dup);
5370 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
5371 PUSH_CALL(ret, location, idAREF,
INT2FIX(1));
5372 PUSH_INSN1(ret, location, putobject,
rb_cArray);
5373 PUSH_INSN(ret, location, swap);
5374 PUSH_CALL(ret, location, rb_intern(
"try_convert"),
INT2FIX(1));
5375 PUSH_INSN(ret, location, dup);
5376 PUSH_INSNL(ret, location, branchunless, not_ary);
5377 PUSH_INSN(ret, location, swap);
5379 PUSH_LABEL(ret, not_ary);
5380 PUSH_INSN(ret, location, pop);
5382 PUSH_LABEL(ret, not_single);
5383 PUSH_SEQ(ret, writes);
5384 PUSH_SEQ(ret, cleanup);
5388 rb_bug(
"Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5398 LABEL *lstart = NEW_LABEL(node_location->
line);
5399 LABEL *lend = NEW_LABEL(node_location->
line);
5400 LABEL *lcont = NEW_LABEL(node_location->
line);
5405 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
5412 pm_scope_node_destroy(&rescue_scope_node);
5414 lstart->rescued = LABEL_RESCUE_BEG;
5415 lend->rescued = LABEL_RESCUE_END;
5416 PUSH_LABEL(ret, lstart);
5418 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
5419 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
5426 PUSH_INSN(ret, location, putnil);
5429 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
5430 PUSH_LABEL(ret, lend);
5433 if (!popped) PUSH_INSN(ret, *node_location, pop);
5437 PUSH_INSN(ret, *node_location, nop);
5438 PUSH_LABEL(ret, lcont);
5440 if (popped) PUSH_INSN(ret, *node_location, pop);
5441 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
5442 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
5452 if (statements != NULL) {
5453 location = PM_NODE_START_LOCATION(parser, statements);
5456 location = *node_location;
5459 LABEL *lstart = NEW_LABEL(location.
line);
5461 LABEL *lcont = NEW_LABEL(location.
line);
5468 if (statements != NULL) {
5469 pm_compile_node(iseq, (
const pm_node_t *) statements, ensr,
true, scope_node);
5473 bool last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
5478 push_ensure_entry(iseq, &enl, &er, (
void *) cast->
ensure_clause);
5480 PUSH_LABEL(ret, lstart);
5482 pm_compile_rescue(iseq, cast, node_location, ret, popped | last_leave, scope_node);
5485 pm_compile_node(iseq, (
const pm_node_t *) cast->
statements, ret, popped | last_leave, scope_node);
5487 else if (!(popped | last_leave)) {
5488 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
5491 PUSH_LABEL(ret, lend);
5492 PUSH_SEQ(ret, ensr);
5493 if (!popped && last_leave) PUSH_INSN(ret, *node_location, putnil);
5494 PUSH_LABEL(ret, lcont);
5495 if (last_leave) PUSH_INSN(ret, *node_location, pop);
5507 pm_scope_node_destroy(&next_scope_node);
5509 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
5510 if (lstart->link.next != &lend->link) {
5512 PUSH_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, lcont);
5513 erange = erange->next;
5516 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
5527 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5529 PM_NODE_TYPE_P(node->
receiver, PM_STRING_NODE) &&
5531 node->
block == NULL &&
5532 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5543 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, name_loc, node_id);
5545 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
5546 ISEQ_BODY(iseq)->ic_size++;
5547 VALUE segments = rb_ary_new_from_args(1, name);
5548 PUSH_INSN1(ret, location, opt_getconstant_path, segments);
5551 PUSH_INSN(ret, location, putnil);
5552 PUSH_INSN1(ret, location, putobject,
Qtrue);
5553 PUSH_INSN1(ret, location, getconstant, name);
5567 switch (PM_NODE_TYPE(node)) {
5568 case PM_CONSTANT_READ_NODE: {
5572 rb_ary_unshift(parts, name);
5575 case PM_CONSTANT_PATH_NODE: {
5579 rb_ary_unshift(parts, name);
5580 if (cast->
parent == NULL) {
5581 rb_ary_unshift(parts,
ID2SYM(idNULL));
5602 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5604 switch (PM_NODE_TYPE(node)) {
5605 case PM_CONSTANT_READ_NODE: {
5609 PUSH_INSN1(body, location, putobject,
Qtrue);
5610 PUSH_INSN1(body, location, getconstant, name);
5613 case PM_CONSTANT_PATH_NODE: {
5617 if (cast->
parent == NULL) {
5618 PUSH_INSN(body, location, pop);
5619 PUSH_INSN1(body, location, putobject, rb_cObject);
5620 PUSH_INSN1(body, location, putobject,
Qtrue);
5621 PUSH_INSN1(body, location, getconstant, name);
5624 pm_compile_constant_path(iseq, cast->
parent, prefix, body,
false, scope_node);
5625 PUSH_INSN1(body, location, putobject,
Qfalse);
5626 PUSH_INSN1(body, location, getconstant, name);
5631 PM_COMPILE_INTO_ANCHOR(prefix, node);
5642 switch (PM_NODE_TYPE(node)) {
5646 case PM_SYMBOL_NODE:
5647 case PM_REGULAR_EXPRESSION_NODE:
5648 case PM_SOURCE_LINE_NODE:
5649 case PM_INTEGER_NODE:
5651 case PM_RATIONAL_NODE:
5652 case PM_IMAGINARY_NODE:
5653 case PM_SOURCE_ENCODING_NODE:
5654 return pm_static_literal_value(iseq, node, scope_node);
5655 case PM_STRING_NODE:
5656 return parse_static_literal_string(iseq, scope_node, node, &((
const pm_string_node_t *) node)->unescaped);
5657 case PM_SOURCE_FILE_NODE:
5659 case PM_ARRAY_NODE: {
5663 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5664 VALUE element = pm_compile_shareable_constant_literal(iseq, cast->
elements.
nodes[index], scope_node);
5672 case PM_HASH_NODE: {
5676 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5678 if (!PM_NODE_TYPE_P(element, PM_ASSOC_NODE))
return Qundef;
5682 VALUE key = pm_compile_shareable_constant_literal(iseq, assoc->
key, scope_node);
5685 VALUE value = pm_compile_shareable_constant_literal(iseq, assoc->
value, scope_node);
5688 rb_hash_aset(result, key, value);
5705 VALUE literal = pm_compile_shareable_constant_literal(iseq, node, scope_node);
5707 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5708 PUSH_INSN1(ret, location, putobject, literal);
5712 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5713 switch (PM_NODE_TYPE(node)) {
5714 case PM_ARRAY_NODE: {
5718 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5721 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5722 pm_compile_shareable_constant_value(iseq, cast->
elements.
nodes[index], shareability, path, ret, scope_node,
false);
5728 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5729 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5734 case PM_HASH_NODE: {
5738 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5741 pm_compile_hash_elements(iseq, (
const pm_node_t *) cast, &cast->elements, shareability, path,
false, ret, scope_node);
5744 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5745 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5751 DECL_ANCHOR(value_seq);
5753 pm_compile_node(iseq, node, value_seq,
false, scope_node);
5754 if (PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) {
5755 PUSH_SEND_WITH_FLAG(value_seq, location, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5758 if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL) {
5759 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5760 PUSH_SEQ(ret, value_seq);
5761 PUSH_INSN1(ret, location, putobject, path);
5762 PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5764 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) {
5765 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5766 PUSH_SEQ(ret, value_seq);
5767 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5769 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING) {
5770 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5771 PUSH_SEQ(ret, value_seq);
5772 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5788 ID name_id = pm_constant_id_lookup(scope_node, node->
name);
5790 if (shareability != 0) {
5791 pm_compile_shareable_constant_value(iseq, node->
value, shareability, rb_id2str(name_id), ret, scope_node,
true);
5794 PM_COMPILE_NOT_POPPED(node->
value);
5797 if (!popped) PUSH_INSN(ret, location, dup);
5798 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5801 PUSH_INSN1(ret, location, setconstant, operand);
5814 LABEL *end_label = NEW_LABEL(location.
line);
5816 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5817 if (!popped) PUSH_INSN(ret, location, dup);
5819 PUSH_INSNL(ret, location, branchunless, end_label);
5820 if (!popped) PUSH_INSN(ret, location, pop);
5822 if (shareability != 0) {
5823 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5826 PM_COMPILE_NOT_POPPED(node->
value);
5829 if (!popped) PUSH_INSN(ret, location, dup);
5830 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5831 PUSH_INSN1(ret, location, setconstant, name);
5832 PUSH_LABEL(ret, end_label);
5845 LABEL *set_label = NEW_LABEL(location.
line);
5846 LABEL *end_label = NEW_LABEL(location.
line);
5848 PUSH_INSN(ret, location, putnil);
5849 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST), name,
Qtrue);
5850 PUSH_INSNL(ret, location, branchunless, set_label);
5852 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5853 if (!popped) PUSH_INSN(ret, location, dup);
5855 PUSH_INSNL(ret, location, branchif, end_label);
5856 if (!popped) PUSH_INSN(ret, location, pop);
5857 PUSH_LABEL(ret, set_label);
5859 if (shareability != 0) {
5860 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5863 PM_COMPILE_NOT_POPPED(node->
value);
5866 if (!popped) PUSH_INSN(ret, location, dup);
5867 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5868 PUSH_INSN1(ret, location, setconstant, name);
5869 PUSH_LABEL(ret, end_label);
5884 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5886 if (shareability != 0) {
5887 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5890 PM_COMPILE_NOT_POPPED(node->
value);
5893 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5894 if (!popped) PUSH_INSN(ret, location, dup);
5896 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5897 PUSH_INSN1(ret, location, setconstant, name);
5908 rb_ary_push(parts, rb_id2str(pm_constant_id_lookup(scope_node, node->
name)));
5911 while (current != NULL && PM_NODE_TYPE_P(current, PM_CONSTANT_PATH_NODE)) {
5913 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, cast->
name)));
5917 if (current == NULL) {
5918 rb_ary_unshift(parts, rb_id2str(idNULL));
5920 else if (PM_NODE_TYPE_P(current, PM_CONSTANT_READ_NODE)) {
5921 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, ((
const pm_constant_read_node_t *) current)->name)));
5945 PUSH_INSN1(ret, location, putobject, rb_cObject);
5948 if (shareability != 0) {
5949 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
5952 PM_COMPILE_NOT_POPPED(node->
value);
5956 PUSH_INSN(ret, location, swap);
5957 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
5960 PUSH_INSN(ret, location, swap);
5961 PUSH_INSN1(ret, location, setconstant, name);
5978 PM_COMPILE_NOT_POPPED(target->
parent);
5981 PUSH_INSN1(ret, location, putobject, rb_cObject);
5984 PUSH_INSN(ret, location, dup);
5985 PUSH_INSN1(ret, location, putobject,
Qtrue);
5986 PUSH_INSN1(ret, location, getconstant, name);
5988 if (!popped) PUSH_INSN(ret, location, dup);
5989 PUSH_INSNL(ret, location, branchunless, lfin);
5991 if (!popped) PUSH_INSN(ret, location, pop);
5993 if (shareability != 0) {
5994 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
5997 PM_COMPILE_NOT_POPPED(node->
value);
6001 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6004 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
6005 PUSH_INSN(ret, location, swap);
6008 PUSH_INSN1(ret, location, setconstant, name);
6009 PUSH_LABEL(ret, lfin);
6011 if (!popped) PUSH_INSN(ret, location, swap);
6012 PUSH_INSN(ret, location, pop);
6026 LABEL *lassign = NEW_LABEL(location.
line);
6030 PM_COMPILE_NOT_POPPED(target->
parent);
6033 PUSH_INSN1(ret, location, putobject, rb_cObject);
6036 PUSH_INSN(ret, location, dup);
6037 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM), name,
Qtrue);
6038 PUSH_INSNL(ret, location, branchunless, lassign);
6040 PUSH_INSN(ret, location, dup);
6041 PUSH_INSN1(ret, location, putobject,
Qtrue);
6042 PUSH_INSN1(ret, location, getconstant, name);
6044 if (!popped) PUSH_INSN(ret, location, dup);
6045 PUSH_INSNL(ret, location, branchif, lfin);
6047 if (!popped) PUSH_INSN(ret, location, pop);
6048 PUSH_LABEL(ret, lassign);
6050 if (shareability != 0) {
6051 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6054 PM_COMPILE_NOT_POPPED(node->
value);
6058 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6061 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
6062 PUSH_INSN(ret, location, swap);
6065 PUSH_INSN1(ret, location, setconstant, name);
6066 PUSH_LABEL(ret, lfin);
6068 if (!popped) PUSH_INSN(ret, location, swap);
6069 PUSH_INSN(ret, location, pop);
6086 PM_COMPILE_NOT_POPPED(target->
parent);
6089 PUSH_INSN1(ret, location, putobject, rb_cObject);
6092 PUSH_INSN(ret, location, dup);
6093 PUSH_INSN1(ret, location, putobject,
Qtrue);
6094 PUSH_INSN1(ret, location, getconstant, name);
6096 if (shareability != 0) {
6097 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6100 PM_COMPILE_NOT_POPPED(node->
value);
6103 PUSH_CALL(ret, location, method_id,
INT2FIX(1));
6104 PUSH_INSN(ret, location, swap);
6107 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6108 PUSH_INSN(ret, location, swap);
6111 PUSH_INSN1(ret, location, setconstant, name);
6120#define PM_CONTAINER_P(node) (PM_NODE_TYPE_P(node, PM_ARRAY_NODE) || PM_NODE_TYPE_P(node, PM_HASH_NODE) || PM_NODE_TYPE_P(node, PM_RANGE_NODE))
6139 bool trailing_comma =
false;
6141 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
6145 if (scope_node->parameters != NULL) {
6146 switch (PM_NODE_TYPE(scope_node->parameters)) {
6147 case PM_BLOCK_PARAMETERS_NODE: {
6149 parameters_node = cast->parameters;
6150 block_locals = &cast->locals;
6152 if (parameters_node) {
6153 if (parameters_node->
rest && PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE)) {
6154 trailing_comma =
true;
6159 case PM_PARAMETERS_NODE: {
6163 case PM_NUMBERED_PARAMETERS_NODE: {
6165 body->
param.lead_num = maximum;
6166 body->
param.flags.ambiguous_param0 = maximum == 1;
6169 case PM_IT_PARAMETERS_NODE:
6170 body->
param.lead_num = 1;
6171 body->
param.flags.ambiguous_param0 =
true;
6174 rb_bug(
"Unexpected node type for parameters: %s", pm_node_type_to_str(PM_NODE_TYPE(scope_node->parameters)));
6178 struct rb_iseq_param_keyword *keyword = NULL;
6180 if (parameters_node) {
6181 optionals_list = ¶meters_node->
optionals;
6182 requireds_list = ¶meters_node->
requireds;
6183 keywords_list = ¶meters_node->
keywords;
6184 posts_list = ¶meters_node->
posts;
6186 else if (scope_node->parameters && (PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE) || PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE))) {
6187 body->
param.opt_num = 0;
6190 body->
param.lead_num = 0;
6191 body->
param.opt_num = 0;
6197 size_t locals_size = locals->
size;
6200 st_table *index_lookup_table = st_init_numtable();
6202 int table_size = (int) locals_size;
6206 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
6208 if (keywords_list && keywords_list->
size) {
6212 if (requireds_list) {
6213 for (
size_t i = 0; i < requireds_list->
size; i++) {
6218 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6221 else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
6222 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6231 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6239 if (optionals_list && optionals_list->
size) {
6240 for (
size_t i = 0; i < optionals_list->
size; i++) {
6242 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6252 if (parameters_node) {
6253 if (parameters_node->
rest) {
6254 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6268 if (PM_NODE_TYPE_P(parameters_node->
keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
6270 if (requireds_list->
size == 0 && optionals_list->
size == 0 && keywords_list->
size == 0) {
6271 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
6272 ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
6283 if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6291 for (
size_t i = 0; i < posts_list->
size; i++) {
6296 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6302 if (keywords_list && keywords_list->
size) {
6303 for (
size_t i = 0; i < keywords_list->
size; i++) {
6305 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6311 if (parameters_node && parameters_node->
block) {
6314 if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
6322 local_table_for_iseq->size = table_size;
6342 int local_index = 0;
6349 if (requireds_list && requireds_list->
size) {
6350 for (
size_t i = 0; i < requireds_list->
size; i++, local_index++) {
6358 switch (PM_NODE_TYPE(required)) {
6359 case PM_MULTI_TARGET_NODE: {
6362 local = rb_make_temporary_id(local_index);
6363 local_table_for_iseq->ids[local_index] = local;
6366 case PM_REQUIRED_PARAMETER_NODE: {
6371 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6372 ID local = pm_constant_id_lookup(scope_node, param->
name);
6373 local_table_for_iseq->ids[local_index] = local;
6376 pm_insert_local_index(param->
name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6382 rb_bug(
"Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(required)));
6386 body->
param.lead_num = (int) requireds_list->
size;
6387 body->
param.flags.has_lead =
true;
6390 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6391 ID local = rb_make_temporary_id(local_index);
6392 local_table_for_iseq->ids[local_index++] = local;
6397 if (optionals_list && optionals_list->
size) {
6398 body->
param.opt_num = (int) optionals_list->
size;
6399 body->
param.flags.has_opt =
true;
6401 for (
size_t i = 0; i < optionals_list->
size; i++, local_index++) {
6405 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6406 ID local = pm_constant_id_lookup(scope_node, name);
6407 local_table_for_iseq->ids[local_index] = local;
6410 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6417 if (parameters_node && parameters_node->
rest) {
6418 body->
param.rest_start = local_index;
6422 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6423 body->
param.flags.has_rest =
true;
6431 if (PM_NODE_FLAG_P(parameters_node->
rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6432 ID local = pm_constant_id_lookup(scope_node, name);
6433 local_table_for_iseq->ids[local_index] = local;
6436 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6442 body->
param.flags.anon_rest =
true;
6443 pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
6452 if (posts_list && posts_list->
size) {
6453 body->
param.post_num = (int) posts_list->
size;
6454 body->
param.post_start = local_index;
6455 body->
param.flags.has_post =
true;
6457 for (
size_t i = 0; i < posts_list->
size; i++, local_index++) {
6465 switch (PM_NODE_TYPE(post_node)) {
6466 case PM_MULTI_TARGET_NODE: {
6469 local = rb_make_temporary_id(local_index);
6470 local_table_for_iseq->ids[local_index] = local;
6473 case PM_REQUIRED_PARAMETER_NODE: {
6478 if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6479 ID local = pm_constant_id_lookup(scope_node, param->
name);
6480 local_table_for_iseq->ids[local_index] = local;
6483 pm_insert_local_index(param->
name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6488 rb_bug(
"Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(post_node)));
6496 if (keywords_list && keywords_list->
size) {
6497 keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6498 keyword->num = (int) keywords_list->
size;
6503 for (
size_t i = 0; i < keywords_list->
size; i++) {
6509 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
6511 keyword->required_num++;
6512 ID local = pm_constant_id_lookup(scope_node, name);
6514 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6515 local_table_for_iseq->ids[local_index] = local;
6518 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6524 for (
size_t i = 0; i < keywords_list->
size; i++) {
6530 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
6536 if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !PM_CONTAINER_P(value)) {
6537 rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
6543 ID local = pm_constant_id_lookup(scope_node, name);
6544 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6545 local_table_for_iseq->ids[local_index] = local;
6548 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6558 for (
int i = 0; i <
RARRAY_LEN(default_values); i++) {
6560 if (dv == complex_mark) dv =
Qundef;
6564 keyword->default_values = dvs;
6568 keyword->bits_start = local_index;
6569 ID local = rb_make_temporary_id(local_index);
6570 local_table_for_iseq->ids[local_index] = local;
6573 body->
param.keyword = keyword;
6574 body->
param.flags.has_kw =
true;
6577 if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->
size == 1 && !trailing_comma) {
6578 body->
param.flags.ambiguous_param0 =
true;
6581 if (parameters_node) {
6586 case PM_NO_KEYWORDS_PARAMETER_NODE: {
6589 body->
param.flags.accepts_no_kwarg =
true;
6592 case PM_KEYWORD_REST_PARAMETER_NODE: {
6596 if (!body->
param.flags.has_kw) {
6597 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6600 keyword->rest_start = local_index;
6601 body->
param.flags.has_kwrest =
true;
6605 if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6606 ID local = pm_constant_id_lookup(scope_node, constant_id);
6607 local_table_for_iseq->ids[local_index] = local;
6610 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6614 body->
param.flags.anon_kwrest =
true;
6615 pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
6621 case PM_FORWARDING_PARAMETER_NODE: {
6624 if (!ISEQ_BODY(iseq)->param.
flags.forwardable) {
6626 body->
param.rest_start = local_index;
6627 body->
param.flags.has_rest =
true;
6628 body->
param.flags.anon_rest =
true;
6629 pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
6633 body->
param.flags.has_kw =
false;
6634 body->
param.flags.has_kwrest =
true;
6635 body->
param.flags.anon_kwrest =
true;
6636 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6637 keyword->rest_start = local_index;
6638 pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
6641 body->
param.block_start = local_index;
6642 body->
param.flags.has_block =
true;
6643 pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
6647 pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
6651 rb_bug(
"node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->
keyword_rest)));
6657 if (parameters_node->
block) {
6658 body->
param.block_start = local_index;
6659 body->
param.flags.has_block =
true;
6660 iseq_set_use_block(iseq);
6665 if (PM_NODE_FLAG_P(parameters_node->
block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6666 ID local = pm_constant_id_lookup(scope_node, name);
6667 local_table_for_iseq->ids[local_index] = local;
6670 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6674 pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
6693 if (requireds_list && requireds_list->
size) {
6694 for (
size_t i = 0; i < requireds_list->
size; i++) {
6700 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6701 local_index = pm_compile_destructured_param_locals((
const pm_multi_target_node_t *) required, index_lookup_table, local_table_for_iseq, scope_node, local_index);
6707 if (posts_list && posts_list->
size) {
6708 for (
size_t i = 0; i < posts_list->
size; i++) {
6714 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6715 local_index = pm_compile_destructured_param_locals((
const pm_multi_target_node_t *) post, index_lookup_table, local_table_for_iseq, scope_node, local_index);
6721 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6722 if (PM_NODE_TYPE_P(((
const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
6723 body->
param.lead_num++;
6726 body->
param.rest_start = local_index;
6727 body->
param.flags.has_rest =
true;
6730 ID local = rb_make_temporary_id(local_index);
6731 local_table_for_iseq->ids[local_index] = local;
6736 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
6739 for (
int i = 0; i < maximum; i++, local_index++) {
6740 const uint8_t param_name[] = {
'_',
'1' + i };
6742 RUBY_ASSERT(constant_id &&
"parser should fill in any gaps in numbered parameters");
6743 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6745 body->
param.lead_num = maximum;
6746 body->
param.flags.has_lead =
true;
6750 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6751 body->
param.lead_num = 1;
6752 body->
param.flags.has_lead =
true;
6763 if (block_locals && block_locals->
size) {
6764 for (
size_t i = 0; i < block_locals->
size; i++, local_index++) {
6766 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6771 if (scope_node->locals.
size) {
6772 for (
size_t i = 0; i < scope_node->locals.
size; i++) {
6776 ctx.scope_node = scope_node;
6777 ctx.local_table_for_iseq = local_table_for_iseq;
6778 ctx.local_index = local_index;
6780 st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
6782 local_index = ctx.local_index;
6791 if (scope_node->index_lookup_table) {
6792 st_free_table(scope_node->index_lookup_table);
6794 scope_node->index_lookup_table = index_lookup_table;
6795 iseq_calc_param_size(iseq);
6797 if (ISEQ_BODY(iseq)->param.
flags.forwardable) {
6800 ISEQ_BODY(iseq)->param.size += 1;
6804 iseq_set_local_table(iseq, local_table_for_iseq, 0);
6805 iseq_set_parameters_lvar_state(iseq);
6807 scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
6809 if (keyword != NULL) {
6810 size_t keyword_start_index = keyword->bits_start - keyword->num;
6811 keyword->table = (
ID *)&ISEQ_BODY(iseq)->local_table[keyword_start_index];
6816 if (optionals_list && optionals_list->
size) {
6824 for (
size_t i = 0; i < optionals_list->
size; i++) {
6825 label = NEW_LABEL(location.
line);
6826 opt_table[i] = label;
6827 PUSH_LABEL(ret, label);
6829 PM_COMPILE_NOT_POPPED(optional_node);
6833 label = NEW_LABEL(location.
line);
6834 opt_table[optionals_list->
size] = label;
6835 PUSH_LABEL(ret, label);
6837 body->
param.opt_table = (
const VALUE *) opt_table;
6840 if (keywords_list && keywords_list->
size) {
6841 size_t optional_index = 0;
6842 for (
size_t i = 0; i < keywords_list->
size; i++) {
6846 switch (PM_NODE_TYPE(keyword_parameter_node)) {
6847 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
6855 if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_CONTAINER_P(value)) {
6856 LABEL *end_label = NEW_LABEL(location.
line);
6859 int kw_bits_idx = table_size - body->
param.keyword->bits_start;
6860 PUSH_INSN2(ret, location, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(optional_index));
6861 PUSH_INSNL(ret, location, branchif, end_label);
6863 PUSH_SETLOCAL(ret, location, index.index, index.level);
6864 PUSH_LABEL(ret, end_label);
6869 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
6874 rb_bug(
"Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
6879 if (requireds_list && requireds_list->
size) {
6880 for (
size_t i = 0; i < requireds_list->
size; i++) {
6886 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6887 PUSH_GETLOCAL(ret, location, table_size - (
int)i, 0);
6893 if (posts_list && posts_list->
size) {
6894 for (
size_t i = 0; i < posts_list->
size; i++) {
6900 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6901 PUSH_GETLOCAL(ret, location, table_size - body->
param.post_start - (
int) i, 0);
6907 switch (body->type) {
6908 case ISEQ_TYPE_PLAIN: {
6909 RUBY_ASSERT(PM_NODE_TYPE_P(scope_node->ast_node, PM_INTERPOLATED_REGULAR_EXPRESSION_NODE));
6912 pm_compile_regexp_dynamic(iseq, (
const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6916 case ISEQ_TYPE_BLOCK: {
6917 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
6918 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
6921 start->rescued = LABEL_RESCUE_BEG;
6922 end->rescued = LABEL_RESCUE_END;
6928 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6929 pm_compile_for_node_index(iseq, ((
const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
6933 PUSH_INSN(ret, block_location, nop);
6934 PUSH_LABEL(ret, start);
6936 if (scope_node->body != NULL) {
6937 switch (PM_NODE_TYPE(scope_node->ast_node)) {
6938 case PM_POST_EXECUTION_NODE: {
6940 PUSH_INSN1(ret, block_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6944 pm_scope_node_init((
const pm_node_t *) cast->statements, &next_scope_node, scope_node);
6946 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.
line);
6947 pm_scope_node_destroy(&next_scope_node);
6949 PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe,
INT2FIX(0), block);
6952 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
6954 pm_compile_regexp_dynamic(iseq, (
const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6958 pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
6963 PUSH_INSN(ret, block_location, putnil);
6966 PUSH_LABEL(ret, end);
6968 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
6971 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
6972 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
6975 case ISEQ_TYPE_ENSURE: {
6976 const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->parser, scope_node->body) : location);
6977 iseq_set_exception_local_table(iseq);
6979 if (scope_node->body != NULL) {
6980 PM_COMPILE_POPPED((
const pm_node_t *) scope_node->body);
6983 PUSH_GETLOCAL(ret, statements_location, 1, 0);
6984 PUSH_INSN1(ret, statements_location,
throw,
INT2FIX(0));
6987 case ISEQ_TYPE_METHOD: {
6988 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
6991 if (scope_node->body) {
6992 PM_COMPILE((
const pm_node_t *) scope_node->body);
6995 PUSH_INSN(ret, location, putnil);
6998 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
7001 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
7004 case ISEQ_TYPE_RESCUE: {
7005 iseq_set_exception_local_table(iseq);
7006 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
7008 LABEL *rescue_end = NEW_LABEL(location.
line);
7009 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7011 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7012 PUSH_INSNL(ret, location, branchif, lab);
7013 PUSH_INSNL(ret, location, jump, rescue_end);
7014 PUSH_LABEL(ret, lab);
7016 PM_COMPILE((
const pm_node_t *) scope_node->body);
7017 PUSH_INSN(ret, location, leave);
7018 PUSH_LABEL(ret, rescue_end);
7019 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7022 PM_COMPILE((
const pm_node_t *) scope_node->ast_node);
7024 PUSH_INSN1(ret, location,
throw,
INT2FIX(0));
7029 if (scope_node->body) {
7030 PM_COMPILE((
const pm_node_t *) scope_node->body);
7033 PUSH_INSN(ret, location, putnil);
7038 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
7039 const pm_node_location_t end_location = PM_NODE_END_LOCATION(scope_node->parser, scope_node->ast_node);
7041 ISEQ_COMPILE_DATA(iseq)->last_line = end_location.
line;
7044 if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
7046 PUSH_INSN(ret, location, leave);
7055 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7059 VALUE operand =
ID2SYM(rb_intern3((
const char *) name_loc->
start, name_loc->
end - name_loc->
start, scope_node->encoding));
7060 PUSH_INSN1(ret, *location, putobject, operand);
7065 VALUE operand =
ID2SYM(rb_intern3((
const char *) name_loc->
start, name_loc->
end - name_loc->
start, scope_node->encoding));
7066 PUSH_INSN1(ret, *location, putobject, operand);
7069 PUSH_SEND(ret, *location, id_core_set_variable_alias,
INT2FIX(2));
7070 if (popped) PUSH_INSN(ret, *location, pop);
7076 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7077 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
7078 PM_COMPILE_NOT_POPPED(node->
new_name);
7079 PM_COMPILE_NOT_POPPED(node->
old_name);
7081 PUSH_SEND(ret, *location, id_core_set_method_alias,
INT2FIX(3));
7082 if (popped) PUSH_INSN(ret, *location, pop);
7088 LABEL *end_label = NEW_LABEL(location->
line);
7090 PM_COMPILE_NOT_POPPED(node->
left);
7091 if (!popped) PUSH_INSN(ret, *location, dup);
7092 PUSH_INSNL(ret, *location, branchunless, end_label);
7094 if (!popped) PUSH_INSN(ret, *location, pop);
7095 PM_COMPILE(node->
right);
7096 PUSH_LABEL(ret, end_label);
7104 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
7109 if (elements->
size) {
7110 VALUE value = pm_static_literal_value(iseq, node, scope_node);
7111 PUSH_INSN1(ret, *location, duparray, value);
7114 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7132 const int max_new_array_size = 0x100;
7133 const unsigned int min_tmp_array_size = 0x40;
7135 int new_array_size = 0;
7136 bool first_chunk =
true;
7142 bool static_literal =
false;
7145#define FLUSH_CHUNK \
7146 if (new_array_size) { \
7147 if (first_chunk) PUSH_INSN1(ret, *location, newarray, INT2FIX(new_array_size)); \
7148 else PUSH_INSN1(ret, *location, pushtoarray, INT2FIX(new_array_size)); \
7149 first_chunk = false; \
7150 new_array_size = 0; \
7153 for (
size_t index = 0; index < elements->
size; index++) {
7156 if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
7161 PM_COMPILE_NOT_POPPED(splat_element->
expression);
7164 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
7165 PUSH_GETLOCAL(ret, *location, index.index, index.level);
7171 PUSH_INSN1(ret, *location, splatarray,
Qtrue);
7172 first_chunk =
false;
7175 PUSH_INSN(ret, *location, concattoarray);
7178 static_literal =
false;
7180 else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
7181 if (new_array_size == 0 && first_chunk) {
7182 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7183 first_chunk =
false;
7200 pm_compile_hash_elements(iseq, element, &keyword_hash->
elements, 0,
Qundef,
false, ret, scope_node);
7207 while (splats < keyword_hash->elements.
size && PM_NODE_TYPE_P(keyword_hash->
elements.
nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
7210 PUSH_INSN(ret, *location, pushtoarraykwsplat);
7217 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) &&
7218 !PM_CONTAINER_P(element) &&
7220 ((index + min_tmp_array_size) < elements->
size)
7225 size_t right_index = index + 1;
7227 right_index < elements->size &&
7228 PM_NODE_FLAG_P(elements->
nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL) &&
7229 !PM_CONTAINER_P(elements->
nodes[right_index])
7232 size_t tmp_array_size = right_index - index;
7233 if (tmp_array_size >= min_tmp_array_size) {
7237 for (; tmp_array_size; tmp_array_size--)
7238 rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->
nodes[index++], scope_node));
7246 PUSH_INSN1(ret, *location, duparray, tmp_array);
7247 first_chunk =
false;
7250 PUSH_INSN1(ret, *location, putobject, tmp_array);
7251 PUSH_INSN(ret, *location, concattoarray);
7255 PM_COMPILE_NOT_POPPED(element);
7256 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7257 static_literal =
true;
7260 PM_COMPILE_NOT_POPPED(element);
7261 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7262 static_literal =
false;
7267 if (popped) PUSH_INSN(ret, *location, pop);
7275 unsigned long throw_flag = 0;
7277 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7279 LABEL *splabel = NEW_LABEL(0);
7280 PUSH_LABEL(ret, splabel);
7281 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7287 PUSH_INSN(ret, *location, putnil);
7290 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7291 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7292 PUSH_ADJUST_RESTORE(ret, splabel);
7293 if (!popped) PUSH_INSN(ret, *location, putnil);
7299 if (!ISEQ_COMPILE_DATA(ip)) {
7304 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7305 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7307 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7310 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7311 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7315 ip = ISEQ_BODY(ip)->parent_iseq;
7324 PUSH_INSN(ret, *location, putnil);
7327 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_BREAK));
7328 if (popped) PUSH_INSN(ret, *location, pop);
7333 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7340 ID method_id = pm_constant_id_lookup(scope_node, node->
name);
7346 const char *builtin_func;
7348 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, node->
receiver, method_id)) != NULL) {
7349 pm_compile_builtin_function_call(iseq, ret, scope_node, node, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
7353 LABEL *start = NEW_LABEL(location.
line);
7354 if (node->
block) PUSH_LABEL(ret, start);
7356 switch (method_id) {
7358 if (pm_opt_str_freeze_p(iseq, node)) {
7360 const struct rb_callinfo *callinfo = new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE);
7361 PUSH_INSN2(ret, location, opt_str_uminus, value, callinfo);
7362 if (popped) PUSH_INSN(ret, location, pop);
7368 if (pm_opt_str_freeze_p(iseq, node)) {
7370 const struct rb_callinfo *callinfo = new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE);
7371 PUSH_INSN2(ret, location, opt_str_freeze, value, callinfo);
7372 if (popped) PUSH_INSN(ret, location, pop);
7379 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
7380 PUSH_INSN(ret, location, putnil);
7384 PUSH_INSN(ret, location, putself);
7387 if (method_id == idCall && PM_NODE_TYPE_P(node->
receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
7392 if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
7393 ADD_ELEM(ret, (
LINK_ELEMENT *) new_insn_body(iseq, location.
line, node_id, BIN(getblockparamproxy), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
7396 PM_COMPILE_NOT_POPPED(node->
receiver);
7400 PM_COMPILE_NOT_POPPED(node->
receiver);
7404 pm_compile_call(iseq, node, ret, popped, scope_node, method_id, start);
7413 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
7414 flag = VM_CALL_FCALL;
7417 PM_COMPILE_NOT_POPPED(node->
receiver);
7419 LABEL *safe_label = NULL;
7420 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
7421 safe_label = NEW_LABEL(location->
line);
7422 PUSH_INSN(ret, *location, dup);
7423 PUSH_INSNL(ret, *location, branchnil, safe_label);
7426 PUSH_INSN(ret, *location, dup);
7428 ID id_read_name = pm_constant_id_lookup(scope_node, node->
read_name);
7429 PUSH_SEND_WITH_FLAG(ret, *location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
7431 PM_COMPILE_NOT_POPPED(node->
value);
7433 PUSH_SEND(ret, *location, id_operator,
INT2FIX(1));
7436 PUSH_INSN(ret, *location, swap);
7437 PUSH_INSN1(ret, *location, topn,
INT2FIX(1));
7440 ID id_write_name = pm_constant_id_lookup(scope_node, node->
write_name);
7441 PUSH_SEND_WITH_FLAG(ret, *location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
7443 if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
7444 PUSH_INSN(ret, *location, pop);
7445 if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
7469 switch (PM_NODE_TYPE(node)) {
7470 case PM_FLOAT_NODE: {
7471 key = pm_static_literal_value(iseq, node, scope_node);
7475 key = (
FIXABLE(intptr) ?
LONG2FIX((
long) intptr) : rb_dbl2big(intptr));
7481 case PM_INTEGER_NODE:
7483 case PM_SOURCE_FILE_NODE:
7484 case PM_SOURCE_LINE_NODE:
7485 case PM_SYMBOL_NODE:
7487 key = pm_static_literal_value(iseq, node, scope_node);
7489 case PM_STRING_NODE: {
7491 key = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
7498 if (
NIL_P(rb_hash_lookup(dispatch, key))) {
7499 rb_hash_aset(dispatch, key, ((
VALUE) label) | 1);
7518 DECL_ANCHOR(cond_seq);
7523 DECL_ANCHOR(body_seq);
7527 LABEL *end_label = NEW_LABEL(location.
line);
7538 if (PM_BRANCH_COVERAGE_P(iseq)) {
7539 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7540 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location,
"case");
7546 for (
size_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7550 int clause_lineno = pm_node_line_number(parser, (
const pm_node_t *) clause);
7551 LABEL *label = NEW_LABEL(clause_lineno);
7552 PUSH_LABEL(body_seq, label);
7555 if (PM_BRANCH_COVERAGE_P(iseq)) {
7557 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7560 if (clause->statements != NULL) {
7561 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7564 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7567 PUSH_INSNL(body_seq, location, jump, end_label);
7572 for (
size_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7575 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7577 PUSH_INSN(cond_seq, cond_location, putnil);
7578 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7579 PUSH_INSN1(cond_seq, cond_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7580 PUSH_INSNL(cond_seq, cond_location, branchif, label);
7583 LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
7584 pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label,
false, scope_node);
7585 PUSH_LABEL(cond_seq, next_label);
7592 if (PM_BRANCH_COVERAGE_P(iseq)) {
7596 branch_location = case_location;
7603 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7611 PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
7616 PUSH_INSNL(cond_seq, location, jump, end_label);
7617 PUSH_SEQ(ret, cond_seq);
7625 if (PM_BRANCH_COVERAGE_P(iseq)) {
7626 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7627 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location,
"case");
7632 LABEL *else_label = NEW_LABEL(location.
line);
7640 if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7641 dispatch = rb_hash_new();
7642 RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
7653 for (
size_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7658 LABEL *label = NEW_LABEL(clause_location.
line);
7663 for (
size_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7665 const pm_node_location_t condition_location = PM_NODE_START_LOCATION(parser, condition);
7670 if (dispatch !=
Qundef) {
7671 dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
7674 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7675 PUSH_INSN(cond_seq, condition_location, dup);
7676 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7677 PUSH_INSN1(cond_seq, condition_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
7680 if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
7682 VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->
unescaped);
7683 PUSH_INSN1(cond_seq, condition_location, putobject, value);
7686 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7689 PUSH_INSN1(cond_seq, condition_location, topn,
INT2FIX(1));
7690 PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
7693 PUSH_INSNL(cond_seq, condition_location, branchif, label);
7700 PUSH_LABEL(body_seq, label);
7701 PUSH_INSN(body_seq, clause_location, pop);
7704 if (PM_BRANCH_COVERAGE_P(iseq)) {
7706 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7709 if (clause->statements != NULL) {
7710 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7713 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7716 PUSH_INSNL(body_seq, clause_location, jump, end_label);
7727 if (dispatch !=
Qundef) {
7728 PUSH_INSN(ret, location, dup);
7729 PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
7730 LABEL_REF(else_label);
7733 PUSH_SEQ(ret, cond_seq);
7737 PUSH_LABEL(ret, else_label);
7741 PUSH_INSN(ret, else_location, pop);
7744 if (PM_BRANCH_COVERAGE_P(iseq)) {
7746 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7750 PUSH_INSNL(ret, else_location, jump, end_label);
7753 PUSH_INSN(ret, location, pop);
7756 if (PM_BRANCH_COVERAGE_P(iseq)) {
7757 add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
7760 if (!popped) PUSH_INSN(ret, location, putnil);
7761 PUSH_INSNL(ret, location, jump, end_label);
7765 PUSH_SEQ(ret, body_seq);
7766 PUSH_LABEL(ret, end_label);
7775 DECL_ANCHOR(body_seq);
7780 DECL_ANCHOR(cond_seq);
7784 LABEL *end_label = NEW_LABEL(location->
line);
7789 LABEL *else_label = NEW_LABEL(location->
line);
7797 if (PM_BRANCH_COVERAGE_P(iseq)) {
7798 case_location = pm_code_location(scope_node, (
const pm_node_t *) node);
7799 branches = decl_branch_base(iseq, PTR2NUM(node), &case_location,
"case");
7809 if (in_single_pattern) {
7810 PUSH_INSN(ret, *location, putnil);
7811 PUSH_INSN(ret, *location, putnil);
7812 PUSH_INSN1(ret, *location, putobject,
Qfalse);
7813 PUSH_INSN(ret, *location, putnil);
7817 PUSH_INSN(ret, *location, putnil);
7826 RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
7829 const pm_node_location_t in_location = PM_NODE_START_LOCATION(scope_node->parser, in_node);
7833 PUSH_INSN(body_seq, in_location, putnil);
7836 LABEL *body_label = NEW_LABEL(in_location.
line);
7837 PUSH_LABEL(body_seq, body_label);
7838 PUSH_INSN1(body_seq, in_location, adjuststack,
INT2FIX(in_single_pattern ? 6 : 2));
7841 if (PM_BRANCH_COVERAGE_P(iseq)) {
7843 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"in", branches);
7850 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7853 PUSH_INSNL(body_seq, in_location, jump, end_label);
7854 LABEL *next_pattern_label = NEW_LABEL(pattern_location.
line);
7856 PUSH_INSN(cond_seq, pattern_location, dup);
7857 pm_compile_pattern(iseq, scope_node, in_node->
pattern, cond_seq, body_label, next_pattern_label, in_single_pattern,
false,
true, 2);
7858 PUSH_LABEL(cond_seq, next_pattern_label);
7859 LABEL_UNREMOVABLE(next_pattern_label);
7868 PUSH_LABEL(cond_seq, else_label);
7869 PUSH_INSN(cond_seq, *location, pop);
7870 PUSH_INSN(cond_seq, *location, pop);
7873 if (PM_BRANCH_COVERAGE_P(iseq)) {
7875 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7878 PM_COMPILE_INTO_ANCHOR(cond_seq, (
const pm_node_t *) else_node);
7879 PUSH_INSNL(cond_seq, *location, jump, end_label);
7880 PUSH_INSN(cond_seq, *location, putnil);
7881 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7886 PUSH_LABEL(cond_seq, else_label);
7889 add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
7891 if (in_single_pattern) {
7892 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, cond_seq, end_label, popped);
7895 PUSH_INSN1(cond_seq, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7897 PUSH_INSN1(cond_seq, *location, topn,
INT2FIX(2));
7898 PUSH_SEND(cond_seq, *location, id_core_raise,
INT2FIX(2));
7900 PUSH_INSN1(cond_seq, *location, adjuststack,
INT2FIX(3));
7901 if (!popped) PUSH_INSN(cond_seq, *location, putnil);
7902 PUSH_INSNL(cond_seq, *location, jump, end_label);
7903 PUSH_INSN1(cond_seq, *location, dupn,
INT2FIX(1));
7904 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7911 PUSH_SEQ(ret, cond_seq);
7912 PUSH_SEQ(ret, body_seq);
7913 PUSH_LABEL(ret, end_label);
7921 LABEL *retry_label = NULL;
7922 LABEL *retry_end_l = NULL;
7924 if (node->
block != NULL) {
7925 previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
7926 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
7928 retry_label = NEW_LABEL(location->
line);
7929 retry_end_l = NEW_LABEL(location->
line);
7931 PUSH_LABEL(ret, retry_label);
7934 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
7937 PUSH_INSN(ret, *location, putself);
7938 int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
7940 if (node->
block != NULL) {
7942 pm_scope_node_init((
const pm_node_t *) node->
block, &next_scope_node, scope_node);
7944 ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
7945 pm_scope_node_destroy(&next_scope_node);
7952 const rb_iseq_t *local_iseq = body->local_iseq;
7956 int depth = get_lvar_level(iseq);
7958 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
param.flags.forwardable) {
7959 flag |= VM_CALL_FORWARDING;
7960 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
7961 PUSH_GETLOCAL(ret, *location, mult_local.index, mult_local.level);
7963 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, 0, flag, NULL, block != NULL);
7964 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, block);
7966 if (popped) PUSH_INSN(ret, *location, pop);
7968 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
7973 if (local_body->
param.flags.has_lead) {
7975 for (
int i = 0; i < local_body->
param.lead_num; i++) {
7976 int idx = local_body->local_table_size - i;
7977 PUSH_GETLOCAL(args, *location, idx, depth);
7979 argc += local_body->
param.lead_num;
7982 if (local_body->
param.flags.has_opt) {
7984 for (
int j = 0; j < local_body->
param.opt_num; j++) {
7985 int idx = local_body->local_table_size - (argc + j);
7986 PUSH_GETLOCAL(args, *location, idx, depth);
7988 argc += local_body->
param.opt_num;
7991 if (local_body->
param.flags.has_rest) {
7993 int idx = local_body->local_table_size - local_body->
param.rest_start;
7994 PUSH_GETLOCAL(args, *location, idx, depth);
7995 PUSH_INSN1(args, *location, splatarray,
Qfalse);
7997 argc = local_body->
param.rest_start + 1;
7998 flag |= VM_CALL_ARGS_SPLAT;
8001 if (local_body->
param.flags.has_post) {
8003 int post_len = local_body->
param.post_num;
8004 int post_start = local_body->
param.post_start;
8007 for (; j < post_len; j++) {
8008 int idx = local_body->local_table_size - (post_start + j);
8009 PUSH_GETLOCAL(args, *location, idx, depth);
8012 if (local_body->
param.flags.has_rest) {
8014 PUSH_INSN1(args, *location, newarray,
INT2FIX(j));
8015 PUSH_INSN(args, *location, concatarray);
8018 argc = post_len + post_start;
8022 const struct rb_iseq_param_keyword *
const local_keyword = local_body->
param.keyword;
8023 if (local_body->
param.flags.has_kw) {
8024 int local_size = local_body->local_table_size;
8027 PUSH_INSN1(args, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8029 if (local_body->
param.flags.has_kwrest) {
8030 int idx = local_body->local_table_size - local_keyword->rest_start;
8031 PUSH_GETLOCAL(args, *location, idx, depth);
8033 PUSH_SEND(args, *location, rb_intern(
"dup"),
INT2FIX(0));
8036 PUSH_INSN1(args, *location, newhash,
INT2FIX(0));
8039 for (; i < local_keyword->num; ++i) {
8040 ID id = local_keyword->table[i];
8041 int idx = local_size - get_local_var_idx(local_iseq,
id);
8045 PUSH_INSN1(args, *location, putobject, operand);
8048 PUSH_GETLOCAL(args, *location, idx, depth);
8051 PUSH_SEND(args, *location, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
8052 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
8054 else if (local_body->
param.flags.has_kwrest) {
8055 int idx = local_body->local_table_size - local_keyword->rest_start;
8056 PUSH_GETLOCAL(args, *location, idx, depth);
8058 flag |= VM_CALL_KW_SPLAT;
8061 PUSH_SEQ(ret, args);
8064 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flag, NULL, block != NULL);
8065 PUSH_INSN2(ret, *location, invokesuper, callinfo, block);
8068 if (node->
block != NULL) {
8069 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8070 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
8071 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8074 if (popped) PUSH_INSN(ret, *location, pop);
8080 LABEL *matched_label = NEW_LABEL(location->
line);
8081 LABEL *unmatched_label = NEW_LABEL(location->
line);
8082 LABEL *done_label = NEW_LABEL(location->
line);
8086 PUSH_INSN(ret, *location, putnil);
8087 PUSH_INSN(ret, *location, putnil);
8088 PUSH_INSN1(ret, *location, putobject,
Qfalse);
8089 PUSH_INSN(ret, *location, putnil);
8090 PUSH_INSN(ret, *location, putnil);
8094 PM_COMPILE_NOT_POPPED(node->
value);
8098 PUSH_INSN(ret, *location, dup);
8105 pm_compile_pattern(iseq, scope_node, node->
pattern, ret, matched_label, unmatched_label,
true,
false,
true, 2);
8110 PUSH_LABEL(ret, unmatched_label);
8111 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, ret, done_label, popped);
8115 PUSH_LABEL(ret, matched_label);
8116 PUSH_INSN1(ret, *location, adjuststack,
INT2FIX(6));
8117 if (!popped) PUSH_INSN(ret, *location, putnil);
8118 PUSH_INSNL(ret, *location, jump, done_label);
8120 PUSH_LABEL(ret, done_label);
8126 LABEL *fail_label = NEW_LABEL(location->
line);
8127 LABEL *end_label = NEW_LABEL(location->
line);
8138 PUSH_INSN1(ret, *location, getglobal, operand);
8141 PUSH_INSN(ret, *location, dup);
8142 PUSH_INSNL(ret, *location, branchunless, fail_label);
8148 if (targets_count == 1) {
8150 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8157 PUSH_INSN1(ret, *location, putobject, operand);
8160 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8161 PUSH_LABEL(ret, fail_label);
8162 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8163 if (popped) PUSH_INSN(ret, *location, pop);
8167 DECL_ANCHOR(fail_anchor);
8171 for (
size_t targets_index = 0; targets_index < targets_count; targets_index++) {
8173 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8178 if (((
size_t) targets_index) < (targets_count - 1)) {
8179 PUSH_INSN(ret, *location, dup);
8184 PUSH_INSN1(ret, *location, putobject, operand);
8187 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8188 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8190 PUSH_INSN(fail_anchor, *location, putnil);
8191 PUSH_SETLOCAL(fail_anchor, *location, index.index, index.level);
8195 PUSH_INSNL(ret, *location, jump, end_label);
8199 PUSH_LABEL(ret, fail_label);
8200 PUSH_INSN(ret, *location, pop);
8201 PUSH_SEQ(ret, fail_anchor);
8204 PUSH_LABEL(ret, end_label);
8205 if (popped) PUSH_INSN(ret, *location, pop);
8211 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8212 LABEL *splabel = NEW_LABEL(0);
8213 PUSH_LABEL(ret, splabel);
8219 PUSH_INSN(ret, *location, putnil);
8221 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8223 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8224 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8226 PUSH_ADJUST_RESTORE(ret, splabel);
8227 if (!popped) PUSH_INSN(ret, *location, putnil);
8229 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8230 LABEL *splabel = NEW_LABEL(0);
8232 PUSH_LABEL(ret, splabel);
8233 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8239 PUSH_INSN(ret, *location, putnil);
8242 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8243 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8244 PUSH_ADJUST_RESTORE(ret, splabel);
8245 splabel->unremovable = FALSE;
8247 if (!popped) PUSH_INSN(ret, *location, putnil);
8251 unsigned long throw_flag = 0;
8254 if (!ISEQ_COMPILE_DATA(ip)) {
8259 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8260 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8264 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8267 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8268 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8272 ip = ISEQ_BODY(ip)->parent_iseq;
8280 PUSH_INSN(ret, *location, putnil);
8283 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8284 if (popped) PUSH_INSN(ret, *location, pop);
8287 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8295 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8296 LABEL *splabel = NEW_LABEL(0);
8298 PUSH_LABEL(ret, splabel);
8299 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8300 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8302 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8303 PUSH_ADJUST_RESTORE(ret, splabel);
8304 if (!popped) PUSH_INSN(ret, *location, putnil);
8306 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8307 LABEL *splabel = NEW_LABEL(0);
8309 PUSH_LABEL(ret, splabel);
8310 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8311 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8313 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8314 PUSH_ADJUST_RESTORE(ret, splabel);
8315 if (!popped) PUSH_INSN(ret, *location, putnil);
8321 if (!ISEQ_COMPILE_DATA(ip)) {
8326 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8329 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8332 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8333 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8337 ip = ISEQ_BODY(ip)->parent_iseq;
8341 PUSH_INSN(ret, *location, putnil);
8342 PUSH_INSN1(ret, *location,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8343 if (popped) PUSH_INSN(ret, *location, pop);
8346 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8354 iseq_set_exception_local_table(iseq);
8358 LABEL *exception_match_label = NEW_LABEL(location->
line);
8359 LABEL *rescue_end_label = NEW_LABEL(location->
line);
8369 if (exceptions->
size > 0) {
8370 for (
size_t index = 0; index < exceptions->
size; index++) {
8371 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8372 PM_COMPILE(exceptions->
nodes[index]);
8373 int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
8374 if (PM_NODE_TYPE_P(exceptions->
nodes[index], PM_SPLAT_NODE)) {
8375 checkmatch_flags |= VM_CHECKMATCH_ARRAY;
8377 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(checkmatch_flags));
8378 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8382 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8384 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8385 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8390 PUSH_INSNL(ret, *location, jump, rescue_end_label);
8395 PUSH_LABEL(ret, exception_match_label);
8402 DECL_ANCHOR(writes);
8403 DECL_ANCHOR(cleanup);
8405 pm_compile_target_node(iseq, node->
reference, ret, writes, cleanup, scope_node, NULL);
8406 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8408 PUSH_SEQ(ret, writes);
8409 PUSH_SEQ(ret, cleanup);
8419 LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
8420 ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
8425 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
8428 PUSH_INSN(ret, *location, putnil);
8431 PUSH_INSN(ret, *location, leave);
8437 PUSH_LABEL(ret, rescue_end_label);
8442 PUSH_GETLOCAL(ret, *location, 1, 0);
8450 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8454 enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
8455 while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
8456 if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq))
break;
8457 parent_type = ISEQ_BODY(parent_iseq)->type;
8460 switch (parent_type) {
8462 case ISEQ_TYPE_MAIN:
8464 rb_warn(
"argument of top-level return is ignored");
8466 if (parent_iseq == iseq) {
8467 type = ISEQ_TYPE_METHOD;
8474 if (
type == ISEQ_TYPE_METHOD) {
8475 splabel = NEW_LABEL(0);
8476 PUSH_LABEL(ret, splabel);
8477 PUSH_ADJUST(ret, *location, 0);
8480 if (arguments != NULL) {
8481 PM_COMPILE_NOT_POPPED((
const pm_node_t *) arguments);
8484 PUSH_INSN(ret, *location, putnil);
8487 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8488 pm_add_ensure_iseq(ret, iseq, 1, scope_node);
8490 PUSH_INSN(ret, *location, leave);
8491 PUSH_ADJUST_RESTORE(ret, splabel);
8492 if (!popped) PUSH_INSN(ret, *location, putnil);
8495 PUSH_INSN1(ret, *location,
throw,
INT2FIX(TAG_RETURN));
8496 if (popped) PUSH_INSN(ret, *location, pop);
8505 LABEL *retry_label = NEW_LABEL(location->
line);
8506 LABEL *retry_end_l = NEW_LABEL(location->
line);
8508 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8510 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
8512 PUSH_LABEL(ret, retry_label);
8513 PUSH_INSN(ret, *location, putself);
8517 int argc = pm_setup_args(node->
arguments, node->
block, &flags, &keywords, iseq, ret, scope_node, location);
8518 bool is_forwardable = (node->
arguments != NULL) && PM_NODE_FLAG_P(node->
arguments, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING);
8519 flags |= VM_CALL_SUPER | VM_CALL_FCALL;
8521 if (node->
block && PM_NODE_TYPE_P(node->
block, PM_BLOCK_NODE)) {
8523 pm_scope_node_init(node->
block, &next_scope_node, scope_node);
8525 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
8526 pm_scope_node_destroy(&next_scope_node);
8530 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8533 if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
8534 PUSH_INSN(args, *location, splatkw);
8537 PUSH_SEQ(ret, args);
8538 if (is_forwardable && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.
flags.forwardable) {
8539 flags |= VM_CALL_FORWARDING;
8542 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8543 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, current_block);
8548 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8549 PUSH_INSN2(ret, *location, invokesuper, callinfo, current_block);
8554 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8556 if (popped) PUSH_INSN(ret, *location, pop);
8557 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8558 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
8564 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
8566 case ISEQ_TYPE_MAIN:
8567 case ISEQ_TYPE_CLASS:
8568 COMPILE_ERROR(iseq, location->
line,
"Invalid yield");
8578 argc = pm_setup_args(node->
arguments, NULL, &flags, &keywords, iseq, ret, scope_node, location);
8581 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, FALSE);
8582 PUSH_INSN1(ret, *location, invokeblock, callinfo);
8584 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8585 if (popped) PUSH_INSN(ret, *location, pop);
8588 for (
const rb_iseq_t *tmp_iseq = iseq; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++) {
8589 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
8592 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
8610 int lineno = (int) location.
line;
8612 if (PM_NODE_TYPE_P(node, PM_BEGIN_NODE) && (((
const pm_begin_node_t *) node)->statements == NULL) && (((
const pm_begin_node_t *) node)->rescue_clause != NULL)) {
8617 lineno = (int) PM_NODE_START_LINE_COLUMN(parser, ((
const pm_begin_node_t *) node)->rescue_clause).line;
8620 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
8626 ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
8627 if (lineno > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
8628 event |= RUBY_EVENT_COVERAGE_LINE;
8630 PUSH_TRACE(ret, event);
8633 switch (PM_NODE_TYPE(node)) {
8634 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
8639 case PM_ALIAS_METHOD_NODE:
8642 pm_compile_alias_method_node(iseq, (
const pm_alias_method_node_t *) node, &location, ret, popped, scope_node);
8647 pm_compile_and_node(iseq, (
const pm_and_node_t *) node, &location, ret, popped, scope_node);
8649 case PM_ARGUMENTS_NODE: {
8659 if (elements->
size == 1) {
8662 PM_COMPILE(elements->
nodes[0]);
8665 pm_compile_array_node(iseq, (
const pm_node_t *) cast, elements, &location, ret, popped, scope_node);
8669 case PM_ARRAY_NODE: {
8673 pm_compile_array_node(iseq, (
const pm_node_t *) cast, &cast->elements, &location, ret, popped, scope_node);
8676 case PM_ASSOC_NODE: {
8684 PM_COMPILE(cast->
key);
8685 PM_COMPILE(cast->
value);
8689 case PM_ASSOC_SPLAT_NODE: {
8697 if (cast->
value != NULL) {
8698 PM_COMPILE(cast->
value);
8701 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
8702 PUSH_GETLOCAL(ret, location, index.index, index.level);
8707 case PM_BACK_REFERENCE_READ_NODE: {
8712 VALUE backref = pm_compile_back_reference_ref(cast);
8714 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), backref);
8718 case PM_BEGIN_NODE: {
8726 pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
8730 pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
8739 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
8744 case PM_BLOCK_ARGUMENT_NODE: {
8754 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
8755 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(local_index.level));
8765 pm_compile_break_node(iseq, (
const pm_break_node_t *) node, &location, ret, popped, scope_node);
8776 pm_compile_call_node(iseq, (
const pm_call_node_t *) node, ret, popped, scope_node);
8778 case PM_CALL_AND_WRITE_NODE: {
8782 pm_compile_call_and_or_write_node(iseq,
true, cast->
receiver, cast->
value, cast->
write_name, cast->
read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
8785 case PM_CALL_OR_WRITE_NODE: {
8789 pm_compile_call_and_or_write_node(iseq,
false, cast->
receiver, cast->
value, cast->
write_name, cast->
read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
8792 case PM_CALL_OPERATOR_WRITE_NODE:
8807 pm_compile_case_node(iseq, (
const pm_case_node_t *) node, &location, ret, popped, scope_node);
8809 case PM_CASE_MATCH_NODE:
8818 pm_compile_case_match_node(iseq, (
const pm_case_match_node_t *) node, &location, ret, popped, scope_node);
8820 case PM_CLASS_NODE: {
8825 ID class_id = pm_constant_id_lookup(scope_node, cast->
name);
8826 VALUE class_name =
rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(class_id)));
8829 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
8831 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, location.
line);
8832 pm_scope_node_destroy(&next_scope_node);
8835 const int flags = VM_DEFINECLASS_TYPE_CLASS |
8836 (cast->
superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
8837 pm_compile_class_path(iseq, cast->
constant_path, &location, ret,
false, scope_node);
8843 PUSH_INSN(ret, location, putnil);
8848 PUSH_INSN3(ret, location, defineclass, operand, class_iseq,
INT2FIX(flags));
8852 if (popped) PUSH_INSN(ret, location, pop);
8855 case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
8859 LABEL *end_label = NEW_LABEL(location.
line);
8861 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8864 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8865 if (!popped) PUSH_INSN(ret, location, dup);
8867 PUSH_INSNL(ret, location, branchunless, end_label);
8868 if (!popped) PUSH_INSN(ret, location, pop);
8870 PM_COMPILE_NOT_POPPED(cast->
value);
8871 if (!popped) PUSH_INSN(ret, location, dup);
8873 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8874 PUSH_LABEL(ret, end_label);
8878 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
8883 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8886 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8887 PM_COMPILE_NOT_POPPED(cast->
value);
8890 int flags = VM_CALL_ARGS_SIMPLE;
8891 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
8893 if (!popped) PUSH_INSN(ret, location, dup);
8894 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8898 case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
8902 LABEL *end_label = NEW_LABEL(location.
line);
8903 LABEL *start_label = NEW_LABEL(location.
line);
8905 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8908 PUSH_INSN(ret, location, putnil);
8909 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR), name,
Qtrue);
8910 PUSH_INSNL(ret, location, branchunless, start_label);
8912 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8913 if (!popped) PUSH_INSN(ret, location, dup);
8915 PUSH_INSNL(ret, location, branchif, end_label);
8916 if (!popped) PUSH_INSN(ret, location, pop);
8918 PUSH_LABEL(ret, start_label);
8919 PM_COMPILE_NOT_POPPED(cast->
value);
8920 if (!popped) PUSH_INSN(ret, location, dup);
8922 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8923 PUSH_LABEL(ret, end_label);
8927 case PM_CLASS_VARIABLE_READ_NODE: {
8932 ID name = pm_constant_id_lookup(scope_node, cast->
name);
8933 PUSH_INSN2(ret, location, getclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
8937 case PM_CLASS_VARIABLE_WRITE_NODE: {
8941 PM_COMPILE_NOT_POPPED(cast->
value);
8942 if (!popped) PUSH_INSN(ret, location, dup);
8944 ID name = pm_constant_id_lookup(scope_node, cast->
name);
8945 PUSH_INSN2(ret, location, setclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
8949 case PM_CONSTANT_PATH_NODE: {
8954 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) !=
Qnil)) {
8955 ISEQ_BODY(iseq)->ic_size++;
8956 PUSH_INSN1(ret, location, opt_getconstant_path, parts);
8959 DECL_ANCHOR(prefix);
8962 pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
8963 if (LIST_INSN_SIZE_ZERO(prefix)) {
8964 PUSH_INSN(ret, location, putnil);
8967 PUSH_SEQ(ret, prefix);
8970 PUSH_SEQ(ret, body);
8973 if (popped) PUSH_INSN(ret, location, pop);
8976 case PM_CONSTANT_PATH_AND_WRITE_NODE: {
8980 pm_compile_constant_path_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8983 case PM_CONSTANT_PATH_OR_WRITE_NODE: {
8987 pm_compile_constant_path_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8990 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
8994 pm_compile_constant_path_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8997 case PM_CONSTANT_PATH_WRITE_NODE: {
9001 pm_compile_constant_path_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9004 case PM_CONSTANT_READ_NODE: {
9010 pm_compile_constant_read(iseq, name, &cast->
base.
location, location.
node_id, ret, scope_node);
9011 if (popped) PUSH_INSN(ret, location, pop);
9015 case PM_CONSTANT_AND_WRITE_NODE: {
9019 pm_compile_constant_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9022 case PM_CONSTANT_OR_WRITE_NODE: {
9026 pm_compile_constant_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9029 case PM_CONSTANT_OPERATOR_WRITE_NODE: {
9033 pm_compile_constant_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9036 case PM_CONSTANT_WRITE_NODE: {
9040 pm_compile_constant_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9050 ID method_name = pm_constant_id_lookup(scope_node, cast->
name);
9053 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9055 rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, location.
line);
9056 pm_scope_node_destroy(&next_scope_node);
9059 PM_COMPILE_NOT_POPPED(cast->
receiver);
9060 PUSH_INSN2(ret, location, definesmethod,
ID2SYM(method_name), method_iseq);
9063 PUSH_INSN2(ret, location, definemethod,
ID2SYM(method_name), method_iseq);
9068 PUSH_INSN1(ret, location, putobject,
ID2SYM(method_name));
9073 case PM_DEFINED_NODE: {
9077 pm_compile_defined_expr(iseq, cast->
value, &location, ret, popped, scope_node,
false);
9080 case PM_EMBEDDED_STATEMENTS_NODE: {
9089 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9092 if (popped) PUSH_INSN(ret, location, pop);
9095 case PM_EMBEDDED_VARIABLE_NODE: {
9102 case PM_FALSE_NODE: {
9106 PUSH_INSN1(ret, location, putobject,
Qfalse);
9110 case PM_ENSURE_NODE: {
9119 case PM_ELSE_NODE: {
9128 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9133 case PM_FLIP_FLOP_NODE: {
9138 LABEL *final_label = NEW_LABEL(location.
line);
9139 LABEL *then_label = NEW_LABEL(location.
line);
9140 LABEL *else_label = NEW_LABEL(location.
line);
9142 pm_compile_flip_flop(cast, else_label, then_label, iseq, location.
line, ret, popped, scope_node);
9144 PUSH_LABEL(ret, then_label);
9145 PUSH_INSN1(ret, location, putobject,
Qtrue);
9146 PUSH_INSNL(ret, location, jump, final_label);
9147 PUSH_LABEL(ret, else_label);
9148 PUSH_INSN1(ret, location, putobject,
Qfalse);
9149 PUSH_LABEL(ret, final_label);
9153 case PM_FLOAT_NODE: {
9158 PUSH_INSN1(ret, location, putobject, operand);
9167 LABEL *retry_label = NEW_LABEL(location.
line);
9168 LABEL *retry_end_l = NEW_LABEL(location.
line);
9171 PUSH_LABEL(ret, retry_label);
9177 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9179 const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.
line);
9180 pm_scope_node_destroy(&next_scope_node);
9182 const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9183 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
9187 PUSH_SEND_WITH_BLOCK(ret, location, idEach,
INT2FIX(0), child_iseq);
9188 pm_compile_retry_end_label(iseq, ret, retry_end_l);
9190 if (popped) PUSH_INSN(ret, location, pop);
9191 ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
9192 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
9195 case PM_FORWARDING_ARGUMENTS_NODE:
9196 rb_bug(
"Cannot compile a ForwardingArgumentsNode directly\n");
9198 case PM_FORWARDING_SUPER_NODE:
9206 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
9210 LABEL *end_label = NEW_LABEL(location.
line);
9213 PUSH_INSN1(ret, location, getglobal, name);
9214 if (!popped) PUSH_INSN(ret, location, dup);
9216 PUSH_INSNL(ret, location, branchunless, end_label);
9217 if (!popped) PUSH_INSN(ret, location, pop);
9219 PM_COMPILE_NOT_POPPED(cast->
value);
9220 if (!popped) PUSH_INSN(ret, location, dup);
9222 PUSH_INSN1(ret, location, setglobal, name);
9223 PUSH_LABEL(ret, end_label);
9227 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
9233 PUSH_INSN1(ret, location, getglobal, name);
9234 PM_COMPILE_NOT_POPPED(cast->
value);
9237 int flags = VM_CALL_ARGS_SIMPLE;
9238 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9240 if (!popped) PUSH_INSN(ret, location, dup);
9241 PUSH_INSN1(ret, location, setglobal, name);
9245 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
9249 LABEL *set_label = NEW_LABEL(location.
line);
9250 LABEL *end_label = NEW_LABEL(location.
line);
9252 PUSH_INSN(ret, location, putnil);
9255 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR), name,
Qtrue);
9256 PUSH_INSNL(ret, location, branchunless, set_label);
9258 PUSH_INSN1(ret, location, getglobal, name);
9259 if (!popped) PUSH_INSN(ret, location, dup);
9261 PUSH_INSNL(ret, location, branchif, end_label);
9262 if (!popped) PUSH_INSN(ret, location, pop);
9264 PUSH_LABEL(ret, set_label);
9265 PM_COMPILE_NOT_POPPED(cast->
value);
9266 if (!popped) PUSH_INSN(ret, location, dup);
9268 PUSH_INSN1(ret, location, setglobal, name);
9269 PUSH_LABEL(ret, end_label);
9273 case PM_GLOBAL_VARIABLE_READ_NODE: {
9279 PUSH_INSN1(ret, location, getglobal, name);
9280 if (popped) PUSH_INSN(ret, location, pop);
9284 case PM_GLOBAL_VARIABLE_WRITE_NODE: {
9288 PM_COMPILE_NOT_POPPED(cast->
value);
9289 if (!popped) PUSH_INSN(ret, location, dup);
9291 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9292 PUSH_INSN1(ret, location, setglobal,
ID2SYM(name));
9296 case PM_HASH_NODE: {
9302 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9310 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
9313 VALUE value = pm_static_literal_value(iseq, node, scope_node);
9314 PUSH_INSN1(ret, location, duphash, value);
9335 for (
size_t index = 0; index < elements->
size; index++) {
9336 PM_COMPILE_POPPED(elements->
nodes[index]);
9340 pm_compile_hash_elements(iseq, node, elements, 0,
Qundef,
false, ret, scope_node);
9356 pm_compile_conditional(iseq, &location, PM_IF_NODE, (
const pm_node_t *) cast, cast->statements, cast->subsequent, cast->predicate, ret, popped, scope_node);
9359 case PM_IMAGINARY_NODE: {
9364 PUSH_INSN1(ret, location, putobject, operand);
9368 case PM_IMPLICIT_NODE: {
9378 PM_COMPILE(cast->
value);
9384 rb_bug(
"Should not ever enter an in node directly");
9387 case PM_INDEX_OPERATOR_WRITE_NODE: {
9391 pm_compile_index_operator_write_node(iseq, cast, &location, ret, popped, scope_node);
9394 case PM_INDEX_AND_WRITE_NODE: {
9398 pm_compile_index_control_flow_write_node(iseq, node, cast->
receiver, cast->
arguments, cast->
block, cast->
value, &location, ret, popped, scope_node);
9401 case PM_INDEX_OR_WRITE_NODE: {
9405 pm_compile_index_control_flow_write_node(iseq, node, cast->
receiver, cast->
arguments, cast->
block, cast->
value, &location, ret, popped, scope_node);
9408 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
9412 LABEL *end_label = NEW_LABEL(location.
line);
9414 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9417 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9418 if (!popped) PUSH_INSN(ret, location, dup);
9420 PUSH_INSNL(ret, location, branchunless, end_label);
9421 if (!popped) PUSH_INSN(ret, location, pop);
9423 PM_COMPILE_NOT_POPPED(cast->
value);
9424 if (!popped) PUSH_INSN(ret, location, dup);
9426 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9427 PUSH_LABEL(ret, end_label);
9431 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
9436 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9439 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9440 PM_COMPILE_NOT_POPPED(cast->
value);
9443 int flags = VM_CALL_ARGS_SIMPLE;
9444 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9446 if (!popped) PUSH_INSN(ret, location, dup);
9447 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9451 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
9455 LABEL *end_label = NEW_LABEL(location.
line);
9457 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9460 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9461 if (!popped) PUSH_INSN(ret, location, dup);
9463 PUSH_INSNL(ret, location, branchif, end_label);
9464 if (!popped) PUSH_INSN(ret, location, pop);
9466 PM_COMPILE_NOT_POPPED(cast->
value);
9467 if (!popped) PUSH_INSN(ret, location, dup);
9469 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9470 PUSH_LABEL(ret, end_label);
9474 case PM_INSTANCE_VARIABLE_READ_NODE: {
9479 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9480 PUSH_INSN2(ret, location, getinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9484 case PM_INSTANCE_VARIABLE_WRITE_NODE: {
9488 PM_COMPILE_NOT_POPPED(cast->
value);
9489 if (!popped) PUSH_INSN(ret, location, dup);
9491 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9492 PUSH_INSN2(ret, location, setinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9496 case PM_INTEGER_NODE: {
9501 PUSH_INSN1(ret, location, putobject, operand);
9505 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
9508 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9510 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9511 PUSH_INSN1(ret, location, putobject, regexp);
9518 PUSH_INSN1(ret, location, getglobal,
rb_id2sym(idLASTLINE));
9519 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
9520 if (popped) PUSH_INSN(ret, location, pop);
9524 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
9527 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ONCE)) {
9528 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
9530 int ise_index = ISEQ_BODY(iseq)->ise_size++;
9533 pm_scope_node_init(node, &next_scope_node, scope_node);
9535 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, location.
line);
9536 pm_scope_node_destroy(&next_scope_node);
9538 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
9539 PUSH_INSN2(ret, location, once, block_iseq,
INT2FIX(ise_index));
9540 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
9542 if (popped) PUSH_INSN(ret, location, pop);
9546 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9548 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9549 PUSH_INSN1(ret, location, putobject, regexp);
9554 if (popped) PUSH_INSN(ret, location, pop);
9559 case PM_INTERPOLATED_STRING_NODE: {
9562 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9564 VALUE string = pm_static_literal_value(iseq, node, scope_node);
9566 if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN)) {
9567 PUSH_INSN1(ret, location, putobject,
string);
9569 else if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) {
9570 PUSH_INSN1(ret, location, putstring,
string);
9573 PUSH_INSN1(ret, location, putchilledstring,
string);
9579 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret, popped, scope_node, NULL, NULL, PM_NODE_FLAG_P(cast, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE), PM_NODE_FLAG_P(cast, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN));
9580 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9581 if (popped) PUSH_INSN(ret, location, pop);
9586 case PM_INTERPOLATED_SYMBOL_NODE: {
9590 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret, popped, scope_node, NULL, NULL,
false,
false);
9593 PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9597 PUSH_INSN(ret, location, intern);
9600 PUSH_INSN(ret, location, pop);
9605 case PM_INTERPOLATED_X_STRING_NODE: {
9610 PUSH_INSN(ret, location, putself);
9612 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret,
false, scope_node, NULL, NULL,
false,
false);
9613 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9615 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
9616 if (popped) PUSH_INSN(ret, location, pop);
9620 case PM_IT_LOCAL_VARIABLE_READ_NODE: {
9627 while (current_scope_node) {
9628 if (current_scope_node->parameters && PM_NODE_TYPE_P(current_scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
9629 PUSH_GETLOCAL(ret, location, current_scope_node->local_table_for_iseq_size, level);
9633 current_scope_node = current_scope_node->previous;
9636 rb_bug(
"Local `it` does not exist");
9641 case PM_KEYWORD_HASH_NODE: {
9649 PM_COMPILE(element);
9652 if (!popped) PUSH_INSN1(ret, location, newhash,
INT2FIX(elements->
size * 2));
9655 case PM_LAMBDA_NODE: {
9661 pm_scope_node_init(node, &next_scope_node, scope_node);
9663 int opening_lineno = pm_location_line_number(parser, &cast->
opening_loc);
9664 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
9665 pm_scope_node_destroy(&next_scope_node);
9668 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9669 PUSH_CALL_WITH_BLOCK(ret, location, idLambda, argc, block);
9672 if (popped) PUSH_INSN(ret, location, pop);
9675 case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
9679 LABEL *end_label = NEW_LABEL(location.
line);
9682 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9683 if (!popped) PUSH_INSN(ret, location, dup);
9685 PUSH_INSNL(ret, location, branchunless, end_label);
9686 if (!popped) PUSH_INSN(ret, location, pop);
9688 PM_COMPILE_NOT_POPPED(cast->
value);
9689 if (!popped) PUSH_INSN(ret, location, dup);
9691 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9692 PUSH_LABEL(ret, end_label);
9696 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
9702 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9704 PM_COMPILE_NOT_POPPED(cast->
value);
9707 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
9709 if (!popped) PUSH_INSN(ret, location, dup);
9710 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9714 case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
9719 LABEL *set_label = NEW_LABEL(location.
line);
9720 LABEL *end_label = NEW_LABEL(location.
line);
9722 PUSH_INSN1(ret, location, putobject,
Qtrue);
9723 PUSH_INSNL(ret, location, branchunless, set_label);
9726 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9727 if (!popped) PUSH_INSN(ret, location, dup);
9729 PUSH_INSNL(ret, location, branchif, end_label);
9730 if (!popped) PUSH_INSN(ret, location, pop);
9732 PUSH_LABEL(ret, set_label);
9733 PM_COMPILE_NOT_POPPED(cast->
value);
9734 if (!popped) PUSH_INSN(ret, location, dup);
9736 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9737 PUSH_LABEL(ret, end_label);
9741 case PM_LOCAL_VARIABLE_READ_NODE: {
9747 PUSH_GETLOCAL(ret, location, index.index, index.level);
9752 case PM_LOCAL_VARIABLE_WRITE_NODE: {
9756 PM_COMPILE_NOT_POPPED(cast->
value);
9757 if (!popped) PUSH_INSN(ret, location, dup);
9760 PUSH_SETLOCAL(ret, location, index.index, index.level);
9763 case PM_MATCH_LAST_LINE_NODE: {
9766 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9768 PUSH_INSN1(ret, location, putobject, regexp);
9770 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
9771 if (popped) PUSH_INSN(ret, location, pop);
9775 case PM_MATCH_PREDICATE_NODE: {
9782 PUSH_INSN(ret, location, putnil);
9785 PM_COMPILE_NOT_POPPED(cast->
value);
9786 PUSH_INSN(ret, location, dup);
9790 LABEL *matched_label = NEW_LABEL(location.
line);
9791 LABEL *unmatched_label = NEW_LABEL(location.
line);
9792 LABEL *done_label = NEW_LABEL(location.
line);
9793 pm_compile_pattern(iseq, scope_node, cast->
pattern, ret, matched_label, unmatched_label,
false,
false,
true, 2);
9797 PUSH_LABEL(ret, unmatched_label);
9798 PUSH_INSN(ret, location, pop);
9799 PUSH_INSN(ret, location, pop);
9801 if (!popped) PUSH_INSN1(ret, location, putobject,
Qfalse);
9802 PUSH_INSNL(ret, location, jump, done_label);
9803 PUSH_INSN(ret, location, putnil);
9807 PUSH_LABEL(ret, matched_label);
9808 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(2));
9809 if (!popped) PUSH_INSN1(ret, location, putobject,
Qtrue);
9810 PUSH_INSNL(ret, location, jump, done_label);
9812 PUSH_LABEL(ret, done_label);
9815 case PM_MATCH_REQUIRED_NODE:
9829 case PM_MATCH_WRITE_NODE:
9838 pm_compile_match_write_node(iseq, (
const pm_match_write_node_t *) node, &location, ret, popped, scope_node);
9840 case PM_MISSING_NODE:
9841 rb_bug(
"A pm_missing_node_t should not exist in prism's AST.");
9843 case PM_MODULE_NODE: {
9848 ID module_id = pm_constant_id_lookup(scope_node, cast->
name);
9849 VALUE module_name =
rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(module_id)));
9852 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9854 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, location.
line);
9855 pm_scope_node_destroy(&next_scope_node);
9857 const int flags = VM_DEFINECLASS_TYPE_MODULE | pm_compile_class_path(iseq, cast->
constant_path, &location, ret,
false, scope_node);
9858 PUSH_INSN(ret, location, putnil);
9859 PUSH_INSN3(ret, location, defineclass,
ID2SYM(module_id), module_iseq,
INT2FIX(flags));
9862 if (popped) PUSH_INSN(ret, location, pop);
9865 case PM_REQUIRED_PARAMETER_NODE: {
9871 PUSH_SETLOCAL(ret, location, index.index, index.level);
9874 case PM_MULTI_WRITE_NODE: {
9885 DECL_ANCHOR(writes);
9886 DECL_ANCHOR(cleanup);
9889 state.position = popped ? 0 : 1;
9890 pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
9892 PM_COMPILE_NOT_POPPED(cast->
value);
9893 if (!popped) PUSH_INSN(ret, location, dup);
9895 PUSH_SEQ(ret, writes);
9896 if (!popped && state.stack_size >= 1) {
9899 PUSH_INSN1(ret, location, setn,
INT2FIX(state.stack_size));
9904 pm_multi_target_state_update(&state);
9906 PUSH_SEQ(ret, cleanup);
9915 pm_compile_next_node(iseq, (
const pm_next_node_t *) node, &location, ret, popped, scope_node);
9921 PUSH_INSN(ret, location, putnil);
9926 case PM_NO_KEYWORDS_PARAMETER_NODE: {
9929 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
9932 case PM_NUMBERED_REFERENCE_READ_NODE: {
9939 VALUE ref = pm_compile_numbered_reference_ref(cast);
9940 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), ref);
9943 PUSH_INSN(ret, location, putnil);
9954 LABEL *end_label = NEW_LABEL(location.
line);
9955 PM_COMPILE_NOT_POPPED(cast->
left);
9957 if (!popped) PUSH_INSN(ret, location, dup);
9958 PUSH_INSNL(ret, location, branchif, end_label);
9960 if (!popped) PUSH_INSN(ret, location, pop);
9961 PM_COMPILE(cast->
right);
9962 PUSH_LABEL(ret, end_label);
9966 case PM_OPTIONAL_PARAMETER_NODE: {
9970 PM_COMPILE_NOT_POPPED(cast->
value);
9973 PUSH_SETLOCAL(ret, location, index.index, index.level);
9977 case PM_PARENTHESES_NODE: {
9985 if (cast->
body != NULL) {
9986 PM_COMPILE(cast->
body);
9989 PUSH_INSN(ret, location, putnil);
9994 case PM_PRE_EXECUTION_NODE: {
10006 DECL_ANCHOR(inner_pre);
10009 DECL_ANCHOR(inner_body);
10014 for (
size_t index = 0; index < body->
size; index++) {
10015 pm_compile_node(iseq, body->
nodes[index], inner_body,
true, scope_node);
10020 PUSH_INSN(inner_body, location, putnil);
10026 PUSH_SEQ(outer_pre, inner_pre);
10027 PUSH_SEQ(outer_pre, inner_body);
10032 case PM_POST_EXECUTION_NODE: {
10036 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
10039 pm_scope_node_init(node, &next_scope_node, scope_node);
10040 child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
10041 pm_scope_node_destroy(&next_scope_node);
10043 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
10045 int is_index = ISEQ_BODY(iseq)->ise_size++;
10046 PUSH_INSN2(ret, location, once, child_iseq,
INT2FIX(is_index));
10048 if (popped) PUSH_INSN(ret, location, pop);
10050 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
10054 case PM_RANGE_NODE: {
10058 bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
10060 if (pm_optimizable_range_item_p(cast->
left) && pm_optimizable_range_item_p(cast->
right)) {
10066 (left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((
const pm_integer_node_t *) left) :
Qnil,
10067 (right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((const
pm_integer_node_t *) right) :
Qnil,
10071 PUSH_INSN1(ret, location, putobject, val);
10075 if (cast->
left != NULL) {
10076 PM_COMPILE(cast->
left);
10078 else if (!popped) {
10079 PUSH_INSN(ret, location, putnil);
10082 if (cast->
right != NULL) {
10083 PM_COMPILE(cast->
right);
10085 else if (!popped) {
10086 PUSH_INSN(ret, location, putnil);
10090 PUSH_INSN1(ret, location, newrange,
INT2FIX(exclude_end ? 1 : 0));
10095 case PM_RATIONAL_NODE: {
10099 PUSH_INSN1(ret, location, putobject, parse_rational((
const pm_rational_node_t *) node));
10106 pm_compile_redo_node(iseq, &location, ret, popped, scope_node);
10108 case PM_REGULAR_EXPRESSION_NODE: {
10112 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
10113 PUSH_INSN1(ret, location, putobject, regexp);
10117 case PM_RESCUE_NODE:
10120 pm_compile_rescue_node(iseq, (
const pm_rescue_node_t *) node, &location, ret, popped, scope_node);
10122 case PM_RESCUE_MODIFIER_NODE: {
10128 pm_scope_node_init((
const pm_node_t *) cast, &rescue_scope_node, scope_node);
10130 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
10131 &rescue_scope_node,
10137 pm_scope_node_destroy(&rescue_scope_node);
10139 LABEL *lstart = NEW_LABEL(location.
line);
10140 LABEL *lend = NEW_LABEL(location.
line);
10141 LABEL *lcont = NEW_LABEL(location.
line);
10143 lstart->rescued = LABEL_RESCUE_BEG;
10144 lend->rescued = LABEL_RESCUE_END;
10146 PUSH_LABEL(ret, lstart);
10148 PUSH_LABEL(ret, lend);
10150 PUSH_INSN(ret, location, nop);
10151 PUSH_LABEL(ret, lcont);
10152 if (popped) PUSH_INSN(ret, location, pop);
10154 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
10155 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
10158 case PM_RETURN_NODE:
10164 pm_compile_return_node(iseq, (
const pm_return_node_t *) node, &location, ret, popped, scope_node);
10166 case PM_RETRY_NODE: {
10169 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10170 PUSH_INSN(ret, location, putnil);
10171 PUSH_INSN1(ret, location,
throw,
INT2FIX(TAG_RETRY));
10172 if (popped) PUSH_INSN(ret, location, pop);
10175 COMPILE_ERROR(iseq, location.
line,
"Invalid retry");
10180 case PM_SCOPE_NODE:
10181 pm_compile_scope_node(iseq, (
pm_scope_node_t *) node, &location, ret, popped);
10183 case PM_SELF_NODE: {
10187 PUSH_INSN(ret, location, putself);
10191 case PM_SHAREABLE_CONSTANT_NODE: {
10195 pm_node_flags_t shareability = (cast->
base.
flags & (PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL | PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING | PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY));
10197 switch (PM_NODE_TYPE(cast->
write)) {
10198 case PM_CONSTANT_WRITE_NODE:
10201 case PM_CONSTANT_AND_WRITE_NODE:
10204 case PM_CONSTANT_OR_WRITE_NODE:
10207 case PM_CONSTANT_OPERATOR_WRITE_NODE:
10210 case PM_CONSTANT_PATH_WRITE_NODE:
10213 case PM_CONSTANT_PATH_AND_WRITE_NODE:
10216 case PM_CONSTANT_PATH_OR_WRITE_NODE:
10219 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
10223 rb_bug(
"Unexpected node type for shareable constant write: %s", pm_node_type_to_str(PM_NODE_TYPE(cast->
write)));
10229 case PM_SINGLETON_CLASS_NODE: {
10235 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
10236 const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit(
"singleton class"), ISEQ_TYPE_CLASS, location.
line);
10237 pm_scope_node_destroy(&next_scope_node);
10240 PUSH_INSN(ret, location, putnil);
10243 CONST_ID(singletonclass,
"singletonclass");
10244 PUSH_INSN3(ret, location, defineclass,
ID2SYM(singletonclass), child_iseq,
INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
10246 if (popped) PUSH_INSN(ret, location, pop);
10251 case PM_SOURCE_ENCODING_NODE: {
10255 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10256 PUSH_INSN1(ret, location, putobject, value);
10260 case PM_SOURCE_FILE_NODE: {
10265 VALUE string = pm_source_file_value(cast, scope_node);
10267 if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_FROZEN)) {
10268 PUSH_INSN1(ret, location, putobject,
string);
10270 else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
10271 PUSH_INSN1(ret, location, putstring,
string);
10274 PUSH_INSN1(ret, location, putchilledstring,
string);
10279 case PM_SOURCE_LINE_NODE: {
10283 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10284 PUSH_INSN1(ret, location, putobject, value);
10288 case PM_SPLAT_NODE: {
10297 PUSH_INSN1(ret, location, splatarray,
Qtrue);
10301 case PM_STATEMENTS_NODE: {
10306 if (body->
size > 0) {
10307 for (
size_t index = 0; index < body->
size - 1; index++) {
10308 PM_COMPILE_POPPED(body->
nodes[index]);
10310 PM_COMPILE(body->
nodes[body->
size - 1]);
10313 PUSH_INSN(ret, location, putnil);
10317 case PM_STRING_NODE: {
10322 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
10324 if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FROZEN)) {
10325 PUSH_INSN1(ret, location, putobject, value);
10327 else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
10328 PUSH_INSN1(ret, location, putstring, value);
10331 PUSH_INSN1(ret, location, putchilledstring, value);
10336 case PM_SUPER_NODE:
10340 pm_compile_super_node(iseq, (
const pm_super_node_t *) node, &location, ret, popped, scope_node);
10342 case PM_SYMBOL_NODE: {
10346 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10347 PUSH_INSN1(ret, location, putobject, value);
10351 case PM_TRUE_NODE: {
10355 PUSH_INSN1(ret, location, putobject,
Qtrue);
10359 case PM_UNDEF_NODE: {
10365 for (
size_t index = 0; index < names->
size; index++) {
10366 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10367 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
10369 PM_COMPILE_NOT_POPPED(names->
nodes[index]);
10370 PUSH_SEND(ret, location, id_core_undef_method,
INT2NUM(2));
10372 if (index < names->size - 1) {
10373 PUSH_INSN(ret, location, pop);
10377 if (popped) PUSH_INSN(ret, location, pop);
10380 case PM_UNLESS_NODE: {
10392 pm_compile_conditional(iseq, &location, PM_UNLESS_NODE, (
const pm_node_t *) cast, statements, (
const pm_node_t *) cast->statements, cast->predicate, ret, popped, scope_node);
10395 case PM_UNTIL_NODE: {
10402 pm_compile_loop(iseq, &location, cast->
base.
flags, PM_UNTIL_NODE, (
const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10405 case PM_WHILE_NODE: {
10412 pm_compile_loop(iseq, &location, cast->
base.
flags, PM_WHILE_NODE, (
const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10415 case PM_X_STRING_NODE: {
10419 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
10421 PUSH_INSN(ret, location, putself);
10422 PUSH_INSN1(ret, location, putobject, value);
10423 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
10424 if (popped) PUSH_INSN(ret, location, pop);
10428 case PM_YIELD_NODE:
10434 pm_compile_yield_node(iseq, (
const pm_yield_node_t *) node, &location, ret, popped, scope_node);
10437 rb_raise(
rb_eNotImpError,
"node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
10442#undef PM_CONTAINER_P
10446pm_iseq_pre_execution_p(
rb_iseq_t *iseq)
10448 switch (ISEQ_BODY(iseq)->
type) {
10449 case ISEQ_TYPE_TOP:
10450 case ISEQ_TYPE_EVAL:
10451 case ISEQ_TYPE_MAIN:
10470 if (pm_iseq_pre_execution_p(iseq)) {
10482 pm_compile_node(iseq, (
const pm_node_t *) node, body,
false, node);
10486 PUSH_SEQ(ret, pre);
10487 PUSH_SEQ(ret, body);
10492 pm_compile_node(iseq, (
const pm_node_t *) node, ret,
false, node);
10495 CHECK(iseq_setup_insn(iseq, ret));
10496 return iseq_setup(iseq, ret);
10506 if (result->
node.ast_node != NULL) {
10507 pm_node_destroy(&result->
parser, result->
node.ast_node);
10512 pm_scope_node_destroy(&result->
node);
10553#define PM_COLOR_BOLD "\033[1m"
10554#define PM_COLOR_GRAY "\033[2m"
10555#define PM_COLOR_RED "\033[1;31m"
10556#define PM_COLOR_RESET "\033[m"
10557#define PM_ERROR_TRUNCATE 30
10562 if (errors == NULL)
return NULL;
10568 pm_line_column_t start = pm_newline_list_line_column(newline_list, error->location.
start, start_line);
10569 pm_line_column_t end = pm_newline_list_line_column(newline_list, error->location.end, start_line);
10577 (index < error_list->size) &&
10578 (errors[index].error != NULL) &&
10580 (errors[index].line < start.
line) ||
10587 if (index + 1 < error_list->
size) {
10588 memmove(&errors[index + 1], &errors[index],
sizeof(
pm_parse_error_t) * (error_list->
size - index - 1));
10592 uint32_t column_end;
10594 column_end = end.
column;
10596 column_end = (uint32_t) (newline_list->
offsets[start.
line - start_line + 1] - newline_list->
offsets[start.
line - start_line] - 1);
10600 if (start.
column == column_end) column_end++;
10604 .line = start.
line,
10605 .column_start = start.
column,
10606 .column_end = column_end
10614#define pm_buffer_append_literal(buffer, str) pm_buffer_append_string(buffer, str, rb_strlen_lit(str))
10617pm_parse_errors_format_line(
const pm_parser_t *parser,
const pm_newline_list_t *newline_list,
const char *number_prefix, int32_t line, uint32_t column_start, uint32_t column_end,
pm_buffer_t *buffer) {
10618 int32_t line_delta = line - parser->
start_line;
10619 assert(line_delta >= 0);
10621 size_t index = (size_t) line_delta;
10622 assert(index < newline_list->size);
10624 const uint8_t *start = &parser->
start[newline_list->
offsets[index]];
10625 const uint8_t *end;
10627 if (index >= newline_list->
size - 1) {
10630 end = &parser->
start[newline_list->
offsets[index + 1]];
10633 pm_buffer_append_format(buffer, number_prefix, line);
10636 bool truncate_end =
false;
10637 if ((column_end != 0) && ((end - (start + column_end)) >= PM_ERROR_TRUNCATE)) {
10638 const uint8_t *end_candidate = start + column_end + PM_ERROR_TRUNCATE;
10640 for (
const uint8_t *ptr = start; ptr < end_candidate;) {
10645 if (char_width == 0)
break;
10649 if (ptr + char_width > end_candidate) {
10650 end_candidate = ptr;
10657 end = end_candidate;
10658 truncate_end =
true;
10662 if (column_start >= PM_ERROR_TRUNCATE) {
10663 pm_buffer_append_string(buffer,
"... ", 4);
10664 start += column_start;
10667 pm_buffer_append_string(buffer, (
const char *) start, (
size_t) (end - start));
10669 if (truncate_end) {
10670 pm_buffer_append_string(buffer,
" ...\n", 5);
10671 }
else if (end == parser->
end && end[-1] !=
'\n') {
10672 pm_buffer_append_string(buffer,
"\n", 1);
10681 assert(error_list->
size != 0);
10685 const int32_t start_line = parser->
start_line;
10688 pm_parse_error_t *errors = pm_parse_errors_format_sort(parser, error_list, newline_list);
10689 if (errors == NULL)
return;
10695 int32_t first_line_number = errors[0].
line;
10696 int32_t last_line_number = errors[error_list->
size - 1].
line;
10701 if (first_line_number < 0) first_line_number = (-first_line_number) * 10;
10702 if (last_line_number < 0) last_line_number = (-last_line_number) * 10;
10703 int32_t max_line_number = first_line_number > last_line_number ? first_line_number : last_line_number;
10705 if (max_line_number < 10) {
10706 if (highlight > 0) {
10708 .
number_prefix = PM_COLOR_GRAY
"%1" PRIi32
" | " PM_COLOR_RESET,
10709 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10710 .divider = PM_COLOR_GRAY
" ~~~~~" PM_COLOR_RESET
"\n"
10715 .blank_prefix =
" | ",
10716 .divider =
" ~~~~~\n"
10719 }
else if (max_line_number < 100) {
10720 if (highlight > 0) {
10722 .
number_prefix = PM_COLOR_GRAY
"%2" PRIi32
" | " PM_COLOR_RESET,
10723 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10724 .divider = PM_COLOR_GRAY
" ~~~~~~" PM_COLOR_RESET
"\n"
10729 .blank_prefix =
" | ",
10730 .divider =
" ~~~~~~\n"
10733 }
else if (max_line_number < 1000) {
10734 if (highlight > 0) {
10736 .
number_prefix = PM_COLOR_GRAY
"%3" PRIi32
" | " PM_COLOR_RESET,
10737 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10738 .divider = PM_COLOR_GRAY
" ~~~~~~~" PM_COLOR_RESET
"\n"
10743 .blank_prefix =
" | ",
10744 .divider =
" ~~~~~~~\n"
10747 }
else if (max_line_number < 10000) {
10748 if (highlight > 0) {
10750 .
number_prefix = PM_COLOR_GRAY
"%4" PRIi32
" | " PM_COLOR_RESET,
10751 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10752 .divider = PM_COLOR_GRAY
" ~~~~~~~~" PM_COLOR_RESET
"\n"
10757 .blank_prefix =
" | ",
10758 .divider =
" ~~~~~~~~\n"
10762 if (highlight > 0) {
10764 .
number_prefix = PM_COLOR_GRAY
"%5" PRIi32
" | " PM_COLOR_RESET,
10765 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10766 .divider = PM_COLOR_GRAY
" ~~~~~~~~" PM_COLOR_RESET
"\n"
10771 .blank_prefix =
" | ",
10772 .divider =
" ~~~~~~~~\n"
10786 uint32_t last_column_start = 0;
10789 for (
size_t index = 0; index < error_list->
size; index++) {
10794 if (error->
line - last_line > 1) {
10795 if (error->
line - last_line > 2) {
10796 if ((index != 0) && (error->
line - last_line > 3)) {
10800 pm_buffer_append_string(buffer,
" ", 2);
10801 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, error->
line - 2, 0, 0, buffer);
10804 pm_buffer_append_string(buffer,
" ", 2);
10805 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, error->
line - 1, 0, 0, buffer);
10810 if ((index == 0) || (error->
line != last_line)) {
10811 if (highlight > 1) {
10812 pm_buffer_append_literal(buffer, PM_COLOR_RED
"> " PM_COLOR_RESET);
10813 }
else if (highlight > 0) {
10814 pm_buffer_append_literal(buffer, PM_COLOR_BOLD
"> " PM_COLOR_RESET);
10816 pm_buffer_append_literal(buffer,
"> ");
10823 for (
size_t next_index = index + 1; next_index < error_list->
size; next_index++) {
10824 if (errors[next_index].line != error->
line)
break;
10825 if (errors[next_index].column_end > column_end) column_end = errors[next_index].
column_end;
10831 const uint8_t *start = &parser->
start[newline_list->
offsets[error->
line - start_line]];
10832 if (start == parser->
end) pm_buffer_append_byte(buffer,
'\n');
10843 pm_buffer_append_string(buffer,
" ", 2);
10847 if (last_column_start >= PM_ERROR_TRUNCATE) {
10848 pm_buffer_append_string(buffer,
" ", 4);
10849 column = last_column_start;
10852 while (column < error->column_start) {
10853 pm_buffer_append_byte(buffer,
' ');
10855 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10856 column += (char_width == 0 ? 1 : char_width);
10859 if (highlight > 1) pm_buffer_append_literal(buffer, PM_COLOR_RED);
10860 else if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_BOLD);
10861 pm_buffer_append_byte(buffer,
'^');
10863 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10864 column += (char_width == 0 ? 1 : char_width);
10866 while (column < error->column_end) {
10867 pm_buffer_append_byte(buffer,
'~');
10869 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10870 column += (char_width == 0 ? 1 : char_width);
10873 if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_RESET);
10875 if (inline_messages) {
10876 pm_buffer_append_byte(buffer,
' ');
10877 assert(error->
error != NULL);
10880 pm_buffer_append_string(buffer, message, strlen(message));
10883 pm_buffer_append_byte(buffer,
'\n');
10887 last_line = error->
line;
10890 if (index == error_list->
size - 1) {
10891 next_line = (((int32_t) newline_list->
size) + parser->
start_line);
10900 next_line = errors[index + 1].
line;
10903 if (next_line - last_line > 1) {
10904 pm_buffer_append_string(buffer,
" ", 2);
10905 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, ++last_line, 0, 0, buffer);
10908 if (next_line - last_line > 1) {
10909 pm_buffer_append_string(buffer,
" ", 2);
10910 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, ++last_line, 0, 0, buffer);
10918#undef PM_ERROR_TRUNCATE
10919#undef PM_COLOR_GRAY
10921#undef PM_COLOR_RESET
10932 const size_t start_line = pm_newline_list_line_column(&parser->
newline_list, location->
start, 1).
line;
10933 const size_t end_line = pm_newline_list_line_column(&parser->
newline_list, location->
end, 1).
line;
10936 const uint8_t *end = ((end_line == parser->
newline_list.
size) ? parser->
end : (parser->start + parser->newline_list.offsets[end_line]));
10939 while (start < end) {
10940 if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0)
return false;
10956 bool valid_utf8 =
true;
10961 int highlight = rb_stderr_tty_p();
10963 const char *no_color = getenv(
"NO_COLOR");
10964 highlight = (no_color == NULL || no_color[0] ==
'\0') ? 2 : 1;
10968 switch (error->level) {
10975 if (valid_utf8 && !pm_parse_process_error_utf8_p(parser, &error->location)) {
10976 valid_utf8 =
false;
10983 int32_t line_number = (int32_t) pm_location_line_number(parser, &error->location);
10985 pm_buffer_append_format(
10987 "%.*s:%" PRIi32
": %s",
10994 if (pm_parse_process_error_utf8_p(parser, &error->location)) {
10995 pm_buffer_append_byte(&buffer,
'\n');
10998 pm_list_t error_list = { .
size = 1, .head = list_node, .tail = list_node };
11000 pm_parse_errors_format(parser, &error_list, &buffer, highlight,
false);
11020 pm_buffer_append_format(
11022 "%.*s:%" PRIi32
": syntax error%s found\n",
11025 (int32_t) pm_location_line_number(parser, &head->location),
11030 pm_parse_errors_format(parser, &parser->
error_list, &buffer, highlight,
true);
11034 if (error != head) pm_buffer_append_byte(&buffer,
'\n');
11035 pm_buffer_append_format(&buffer,
"%.*s:%" PRIi32
": %s", (
int)
pm_string_length(filepath),
pm_string_source(filepath), (int32_t) pm_location_line_number(parser, &error->location), error->message);
11065 int coverage_enabled = scope_node->coverage_enabled;
11067 pm_scope_node_init(node, scope_node, NULL);
11070 scope_node->encoding = rb_enc_find(parser->
encoding->
name);
11071 if (!scope_node->encoding) rb_bug(
"Encoding not found %s!", parser->
encoding->
name);
11073 scope_node->coverage_enabled = coverage_enabled;
11077 if (script_lines != NULL) {
11083 rb_ary_push(*script_lines, rb_enc_str_new((
const char *) parser->
start + offset, length, scope_node->encoding));
11094 int line = pm_location_line_number(parser, &warning->
location);
11097 rb_enc_compile_warning(scope_node->encoding, warning_filepath, line,
"%s", warning->
message);
11100 rb_enc_compile_warn(scope_node->encoding, warning_filepath, line,
"%s", warning->
message);
11106 VALUE error = pm_parse_process_error(result);
11115 scope_node->parser = parser;
11120 scope_node->constants[index] = rb_intern3((
const char *) constant->
start, constant->
length, scope_node->encoding);
11123 scope_node->index_lookup_table = st_init_numtable();
11125 for (
size_t index = 0; index < locals->
size; index++) {
11126 st_insert(scope_node->index_lookup_table, locals->
ids[index], index);
11140pm_options_frozen_string_literal_init(
pm_options_t *options)
11142 int frozen_string_literal = rb_iseq_opt_frozen_string_literal();
11144 switch (frozen_string_literal) {
11145 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11147 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11150 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11154 rb_bug(
"pm_options_frozen_string_literal_init: invalid frozen_string_literal=%d", frozen_string_literal);
11167 const char *start = (
const char *) parser->
start;
11168 const char *end = (
const char *) parser->
end;
11173 size_t last_offset = newline_list->
offsets[newline_list->
size - 1];
11174 bool last_push = start + last_offset != end;
11179 for (
size_t index = 0; index < newline_list->
size - 1; index++) {
11180 size_t offset = newline_list->
offsets[index];
11181 size_t length = newline_list->
offsets[index + 1] - offset;
11183 rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
11188 rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), scope_node->encoding));
11198pm_read_file(
pm_string_t *
string,
const char *filepath)
11202 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
11205 WCHAR *wfilepath =
xmalloc(
sizeof(WCHAR) * ((
size_t) length));
11206 if ((wfilepath == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, wfilepath, length) == 0)) {
11211 HANDLE file = CreateFileW(wfilepath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
11212 if (file == INVALID_HANDLE_VALUE) {
11215 if (GetLastError() == ERROR_ACCESS_DENIED) {
11216 DWORD attributes = GetFileAttributesW(wfilepath);
11217 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
11227 DWORD file_size = GetFileSize(file, NULL);
11228 if (file_size == INVALID_FILE_SIZE) {
11236 if (file_size == 0) {
11239 const uint8_t source[] =
"";
11240 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11245 HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
11246 if (mapping == NULL) {
11253 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
11254 CloseHandle(mapping);
11258 if (source == NULL) {
11262 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
11264#elif defined(_POSIX_MAPPED_FILES)
11266 const int open_mode = O_RDONLY | O_NONBLOCK;
11267 int fd = open(filepath, open_mode);
11274 if (fstat(fd, &sb) == -1) {
11280 if (S_ISDIR(sb.st_mode)) {
11288 if (S_ISFIFO(sb.st_mode) || S_ISCHR(sb.st_mode)) {
11297 long len = RSTRING_LEN(contents);
11302 size_t length = (size_t)
len;
11303 uint8_t *source = malloc(length);
11304 memcpy(source, RSTRING_PTR(contents), length);
11305 *
string = (
pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
11311 size_t size = (size_t) sb.st_size;
11312 uint8_t *source = NULL;
11316 const uint8_t source[] =
"";
11317 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11321 source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
11322 if (source == MAP_FAILED) {
11328 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
11345 pm_options_frozen_string_literal_init(&result->
options);
11354 err = rb_w32_map_errno(GetLastError());
11389 pm_options_version_for_current_ruby_set(&result->
options);
11394 VALUE error = pm_parse_process(result, node, script_lines);
11399 ID id_script_lines = rb_intern(
"SCRIPT_LINES__");
11405 rb_hash_aset(constant_script_lines, filepath, pm_parse_file_script_lines(&result->
node, &result->
parser));
11419 VALUE error = pm_load_file(result, filepath,
false);
11420 if (
NIL_P(error)) {
11421 error = pm_parse_file(result, filepath, script_lines);
11437 if (!rb_enc_asciicompat(encoding)) {
11441 pm_options_frozen_string_literal_init(&result->
options);
11442 pm_string_constant_init(&result->
input, RSTRING_PTR(source), RSTRING_LEN(source));
11449 pm_options_version_for_current_ruby_set(&result->
options);
11454 return pm_parse_process(result, node, script_lines);
11463pm_parse_stdin_eof(
void *stream)
11466 return wrapped_stdin->eof_seen;
11469VALUE rb_io_gets_limit_internal(
VALUE io,
long limit);
11475pm_parse_stdin_fgets(
char *
string,
int size,
void *stream)
11481 VALUE line = rb_io_gets_limit_internal(wrapped_stdin->rb_stdin, size - 1);
11486 const char *cstr = RSTRING_PTR(line);
11487 long length = RSTRING_LEN(line);
11489 memcpy(
string, cstr, length);
11490 string[length] =
'\0';
11495 if (length < (size - 1) &&
string[length - 1] !=
'\n') {
11496 wrapped_stdin->eof_seen = 1;
11503void rb_reset_argf_lineno(
long n);
11513 pm_options_frozen_string_literal_init(&result->
options);
11530 rb_reset_argf_lineno(0);
11532 return pm_parse_process(result, node, NULL);
11535#define PM_VERSION_FOR_RELEASE(major, minor) PM_VERSION_FOR_RELEASE_IMPL(major, minor)
11536#define PM_VERSION_FOR_RELEASE_IMPL(major, minor) PM_OPTIONS_VERSION_CRUBY_##major##_##minor
11538void pm_options_version_for_current_ruby_set(
pm_options_t *options) {
11543#define NEW_ISEQ OLD_ISEQ
11545#undef NEW_CHILD_ISEQ
11546#define NEW_CHILD_ISEQ OLD_CHILD_ISEQ
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
@ PM_ERROR_LEVEL_ARGUMENT
For errors that should raise an argument error.
@ PM_ERROR_LEVEL_LOAD
For errors that should raise a load error.
@ PM_ERROR_LEVEL_SYNTAX
For errors that should raise a syntax error.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_CLASS
Encountered a new class.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ALLOCV
Old name of RB_ALLOCV.
#define ALLOC
Old name of RB_ALLOC.
#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 ID2SYM
Old name of RB_ID2SYM.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define FIXABLE
Old name of RB_FIXABLE.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define rb_exc_new3
Old name of rb_exc_new_str.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
#define xcalloc
Old name of ruby_xcalloc.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eLoadError
LoadError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_syserr_new(int n, const char *mesg)
Creates an exception object that represents the given C errno.
VALUE rb_cArray
Array class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_stdin
STDIN constant.
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.
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
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_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_io_fdopen(int fd, int flags, const char *path)
Creates an IO instance whose backend is the given file descriptor.
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.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
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_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
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_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
@ RUBY_IO_READABLE
IO::READABLE
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout)
Blocks until the passed IO is ready for the passed events.
int len
Length of the buffer.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
#define RUBY_API_VERSION_MAJOR
Major version.
#define RUBY_API_VERSION_MINOR
Minor version.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, const char *encoding)
Set the encoding option on the given options struct.
PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options)
Free the internal memory associated with the options.
PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal)
Set the frozen string literal option on the given options struct.
PRISM_EXPORTED_FUNCTION void pm_options_filepath_set(pm_options_t *options, const char *filepath)
Set the filepath option on the given options struct.
void pm_buffer_free(pm_buffer_t *buffer)
Free the memory associated with the buffer.
size_t pm_buffer_length(const pm_buffer_t *buffer)
Return the length of the buffer.
char * pm_buffer_value(const pm_buffer_t *buffer)
Return the value of the buffer.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string)
Returns the length associated with the string.
PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string)
Returns the start pointer associated with the string.
PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string)
Free the associated memory of the given string.
PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init(pm_string_t *string, const char *filepath)
Read the file indicated by the filepath parameter into source and load its contents and size into the...
pm_string_init_result_t
Represents the result of calling pm_string_mapped_init or pm_string_file_init.
@ PM_STRING_INIT_SUCCESS
Indicates that the string was successfully initialized.
@ PM_STRING_INIT_ERROR_GENERIC
Indicates a generic error from a string_*_init function, where the type of error should be read from ...
@ PM_STRING_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
#define PM_ENCODING_US_ASCII_ENTRY
This is the US-ASCII encoding.
#define PM_NODE_LIST_FOREACH(list, index, node)
Loop through each node in the node list, writing each node to the given pm_node_t pointer.
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser)
Parse the Ruby source associated with the given parser and return the tree.
PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser)
Free any memory associated with the given parser.
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse_stream(pm_parser_t *parser, pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *stream_fgets, pm_parse_stream_feof_t *stream_feof, const pm_options_t *options)
Parse a stream of Ruby source and return the tree.
PRISM_EXPORTED_FUNCTION void pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm_options_t *options)
Initialize a parser with the given start and end pointers.
The main header file for the prism parser.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define errno
Ractor-aware version of errno.
#define RTEST
This is an old name of RB_TEST.
struct pm_node * old_name
AliasGlobalVariableNode::old_name.
struct pm_node * new_name
AliasGlobalVariableNode::new_name.
struct pm_node * old_name
AliasMethodNode::old_name.
struct pm_node * new_name
AliasMethodNode::new_name.
struct pm_node * left
AlternationPatternNode::left.
struct pm_node * right
AlternationPatternNode::right.
struct pm_node * left
AndNode::left.
struct pm_node * right
AndNode::right.
pm_node_t base
The embedded base node.
struct pm_node_list arguments
ArgumentsNode::arguments.
struct pm_node_list elements
ArrayNode::elements.
struct pm_node_list requireds
ArrayPatternNode::requireds.
struct pm_node * rest
ArrayPatternNode::rest.
struct pm_node * constant
ArrayPatternNode::constant.
struct pm_node_list posts
ArrayPatternNode::posts.
struct pm_node * value
AssocNode::value.
struct pm_node * key
AssocNode::key.
struct pm_node * value
AssocSplatNode::value.
pm_node_t base
The embedded base node.
struct pm_ensure_node * ensure_clause
BeginNode::ensure_clause.
struct pm_rescue_node * rescue_clause
BeginNode::rescue_clause.
struct pm_statements_node * statements
BeginNode::statements.
struct pm_else_node * else_clause
BeginNode::else_clause.
struct pm_node * expression
BlockArgumentNode::expression.
struct pm_node * parameters
BlockNode::parameters.
struct pm_node * body
BlockNode::body.
pm_constant_id_list_t locals
BlockNode::locals.
struct pm_arguments_node * arguments
BreakNode::arguments.
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
struct pm_node * value
CallAndWriteNode::value.
pm_constant_id_t read_name
CallAndWriteNode::read_name.
pm_constant_id_t write_name
CallAndWriteNode::write_name.
struct pm_node * receiver
CallAndWriteNode::receiver.
pm_location_t closing_loc
CallNode::closing_loc.
struct pm_node * receiver
CallNode::receiver.
pm_constant_id_t name
CallNode::name.
pm_node_t base
The embedded base node.
pm_location_t message_loc
CallNode::message_loc.
struct pm_arguments_node * arguments
CallNode::arguments.
struct pm_node * block
CallNode::block.
pm_constant_id_t read_name
CallOperatorWriteNode::read_name.
pm_constant_id_t binary_operator
CallOperatorWriteNode::binary_operator.
struct pm_node * receiver
CallOperatorWriteNode::receiver.
pm_constant_id_t write_name
CallOperatorWriteNode::write_name.
struct pm_node * value
CallOperatorWriteNode::value.
struct pm_node * receiver
CallOrWriteNode::receiver.
struct pm_node * value
CallOrWriteNode::value.
pm_constant_id_t write_name
CallOrWriteNode::write_name.
pm_constant_id_t read_name
CallOrWriteNode::read_name.
pm_constant_id_t name
CallTargetNode::name.
struct pm_node * receiver
CallTargetNode::receiver.
struct pm_local_variable_target_node * target
CapturePatternNode::target.
struct pm_node * value
CapturePatternNode::value.
struct pm_node_list conditions
CaseMatchNode::conditions.
struct pm_else_node * else_clause
CaseMatchNode::else_clause.
struct pm_node * predicate
CaseMatchNode::predicate.
struct pm_node * predicate
CaseNode::predicate.
struct pm_else_node * else_clause
CaseNode::else_clause.
struct pm_node_list conditions
CaseNode::conditions.
struct pm_node * constant_path
ClassNode::constant_path.
pm_constant_id_list_t locals
ClassNode::locals.
pm_constant_id_t name
ClassNode::name.
struct pm_node * body
ClassNode::body.
struct pm_node * superclass
ClassNode::superclass.
ClassVariableAndWriteNode.
struct pm_node * value
ClassVariableAndWriteNode::value.
pm_constant_id_t name
ClassVariableAndWriteNode::name.
ClassVariableOperatorWriteNode.
pm_constant_id_t name
ClassVariableOperatorWriteNode::name.
pm_constant_id_t binary_operator
ClassVariableOperatorWriteNode::binary_operator.
struct pm_node * value
ClassVariableOperatorWriteNode::value.
ClassVariableOrWriteNode.
pm_constant_id_t name
ClassVariableOrWriteNode::name.
struct pm_node * value
ClassVariableOrWriteNode::value.
pm_constant_id_t name
ClassVariableReadNode::name.
pm_constant_id_t name
ClassVariableTargetNode::name.
struct pm_node * value
ClassVariableWriteNode::value.
pm_constant_id_t name
ClassVariableWriteNode::name.
pm_location_t name_loc
ConstantAndWriteNode::name_loc.
pm_constant_id_t name
ConstantAndWriteNode::name.
struct pm_node * value
ConstantAndWriteNode::value.
size_t size
The number of constant ids in the list.
size_t capacity
The number of constant ids that have been allocated in the list.
pm_constant_id_t * ids
The constant ids in the list.
ConstantOperatorWriteNode.
pm_constant_id_t name
ConstantOperatorWriteNode::name.
pm_location_t name_loc
ConstantOperatorWriteNode::name_loc.
pm_constant_id_t binary_operator
ConstantOperatorWriteNode::binary_operator.
struct pm_node * value
ConstantOperatorWriteNode::value.
pm_location_t name_loc
ConstantOrWriteNode::name_loc.
pm_constant_id_t name
ConstantOrWriteNode::name.
struct pm_node * value
ConstantOrWriteNode::value.
ConstantPathAndWriteNode.
struct pm_constant_path_node * target
ConstantPathAndWriteNode::target.
struct pm_node * value
ConstantPathAndWriteNode::value.
pm_constant_id_t name
ConstantPathNode::name.
struct pm_node * parent
ConstantPathNode::parent.
ConstantPathOperatorWriteNode.
struct pm_constant_path_node * target
ConstantPathOperatorWriteNode::target.
struct pm_node * value
ConstantPathOperatorWriteNode::value.
pm_constant_id_t binary_operator
ConstantPathOperatorWriteNode::binary_operator.
struct pm_node * value
ConstantPathOrWriteNode::value.
struct pm_constant_path_node * target
ConstantPathOrWriteNode::target.
struct pm_node * parent
ConstantPathTargetNode::parent.
pm_constant_id_t name
ConstantPathTargetNode::name.
struct pm_constant_path_node * target
ConstantPathWriteNode::target.
struct pm_node * value
ConstantPathWriteNode::value.
uint32_t size
The number of buckets in the hash map.
pm_constant_t * constants
The constants that are stored in the buckets.
pm_node_t base
The embedded base node.
pm_constant_id_t name
ConstantReadNode::name.
A constant in the pool which effectively stores a string.
size_t length
The length of the string.
const uint8_t * start
A pointer to the start of the string.
pm_constant_id_t name
ConstantTargetNode::name.
struct pm_node * value
ConstantWriteNode::value.
pm_constant_id_t name
ConstantWriteNode::name.
struct pm_parameters_node * parameters
DefNode::parameters.
pm_constant_id_t name
DefNode::name.
struct pm_node * body
DefNode::body.
struct pm_node * receiver
DefNode::receiver.
pm_node_t base
The embedded base node.
pm_constant_id_list_t locals
DefNode::locals.
struct pm_node * value
DefinedNode::value.
This struct represents a diagnostic generated during parsing.
pm_location_t location
The location of the diagnostic in the source.
const char * message
The message associated with the diagnostic.
pm_list_node_t node
The embedded base node.
uint8_t level
The level of the diagnostic, see pm_error_level_t and pm_warning_level_t for possible values.
struct pm_statements_node * statements
ElseNode::statements.
struct pm_statements_node * statements
EmbeddedStatementsNode::statements.
struct pm_node * variable
EmbeddedVariableNode::variable.
This struct defines the functions necessary to implement the encoding interface so we can determine h...
size_t(* char_width)(const uint8_t *b, ptrdiff_t n)
Return the number of bytes that the next character takes if it is valid in the encoding.
const char * name
The name of the encoding.
struct pm_statements_node * statements
EnsureNode::statements.
struct pm_node * constant
FindPatternNode::constant.
struct pm_node * right
FindPatternNode::right.
struct pm_node_list requireds
FindPatternNode::requireds.
struct pm_splat_node * left
FindPatternNode::left.
pm_node_t base
The embedded base node.
struct pm_node * left
FlipFlopNode::left.
struct pm_node * right
FlipFlopNode::right.
double value
FloatNode::value.
struct pm_statements_node * statements
ForNode::statements.
struct pm_node * collection
ForNode::collection.
struct pm_block_node * block
ForwardingSuperNode::block.
GlobalVariableAndWriteNode.
struct pm_node * value
GlobalVariableAndWriteNode::value.
pm_constant_id_t name
GlobalVariableAndWriteNode::name.
GlobalVariableOperatorWriteNode.
pm_constant_id_t name
GlobalVariableOperatorWriteNode::name.
pm_constant_id_t binary_operator
GlobalVariableOperatorWriteNode::binary_operator.
struct pm_node * value
GlobalVariableOperatorWriteNode::value.
GlobalVariableOrWriteNode.
pm_constant_id_t name
GlobalVariableOrWriteNode::name.
struct pm_node * value
GlobalVariableOrWriteNode::value.
pm_constant_id_t name
GlobalVariableReadNode::name.
GlobalVariableTargetNode.
pm_constant_id_t name
GlobalVariableTargetNode::name.
struct pm_node * value
GlobalVariableWriteNode::value.
pm_constant_id_t name
GlobalVariableWriteNode::name.
struct pm_node_list elements
HashNode::elements.
struct pm_node_list elements
HashPatternNode::elements.
struct pm_node * rest
HashPatternNode::rest.
struct pm_node * constant
HashPatternNode::constant.
struct pm_node * predicate
IfNode::predicate.
struct pm_statements_node * statements
IfNode::statements.
struct pm_node * numeric
ImaginaryNode::numeric.
struct pm_node * value
ImplicitNode::value.
struct pm_statements_node * statements
InNode::statements.
struct pm_node * pattern
InNode::pattern.
struct pm_arguments_node * arguments
IndexAndWriteNode::arguments.
struct pm_node * receiver
IndexAndWriteNode::receiver.
struct pm_block_argument_node * block
IndexAndWriteNode::block.
struct pm_node * value
IndexAndWriteNode::value.
struct pm_block_argument_node * block
IndexOperatorWriteNode::block.
struct pm_node * value
IndexOperatorWriteNode::value.
struct pm_arguments_node * arguments
IndexOperatorWriteNode::arguments.
pm_constant_id_t binary_operator
IndexOperatorWriteNode::binary_operator.
struct pm_node * receiver
IndexOperatorWriteNode::receiver.
struct pm_block_argument_node * block
IndexOrWriteNode::block.
struct pm_node * receiver
IndexOrWriteNode::receiver.
struct pm_node * value
IndexOrWriteNode::value.
struct pm_arguments_node * arguments
IndexOrWriteNode::arguments.
struct pm_node * receiver
IndexTargetNode::receiver.
struct pm_arguments_node * arguments
IndexTargetNode::arguments.
struct pm_block_argument_node * block
IndexTargetNode::block.
InstanceVariableAndWriteNode.
struct pm_node * value
InstanceVariableAndWriteNode::value.
pm_constant_id_t name
InstanceVariableAndWriteNode::name.
InstanceVariableOperatorWriteNode.
struct pm_node * value
InstanceVariableOperatorWriteNode::value.
pm_constant_id_t binary_operator
InstanceVariableOperatorWriteNode::binary_operator.
pm_constant_id_t name
InstanceVariableOperatorWriteNode::name.
InstanceVariableOrWriteNode.
struct pm_node * value
InstanceVariableOrWriteNode::value.
pm_constant_id_t name
InstanceVariableOrWriteNode::name.
InstanceVariableReadNode.
pm_constant_id_t name
InstanceVariableReadNode::name.
InstanceVariableTargetNode.
pm_constant_id_t name
InstanceVariableTargetNode::name.
InstanceVariableWriteNode.
pm_constant_id_t name
InstanceVariableWriteNode::name.
struct pm_node * value
InstanceVariableWriteNode::value.
pm_integer_t value
IntegerNode::value.
A structure represents an arbitrary-sized integer.
size_t length
The number of allocated values.
uint32_t value
Embedded value for small integer.
uint32_t * values
List of 32-bit integers.
bool negative
Whether or not the integer is negative.
InterpolatedMatchLastLineNode.
InterpolatedRegularExpressionNode.
struct pm_node_list parts
InterpolatedStringNode::parts.
struct pm_node_list parts
InterpolatedSymbolNode::parts.
struct pm_node_list parts
InterpolatedXStringNode::parts.
struct pm_node_list elements
KeywordHashNode::elements.
KeywordRestParameterNode.
struct pm_node * body
LambdaNode::body.
pm_location_t opening_loc
LambdaNode::opening_loc.
struct pm_node * parameters
LambdaNode::parameters.
pm_location_t operator_loc
LambdaNode::operator_loc.
pm_constant_id_list_t locals
LambdaNode::locals.
A line and column in a string.
uint32_t column
The column number.
int32_t line
The line number.
This struct represents an abstract linked list that provides common functionality.
struct pm_list_node * next
A pointer to the next node in the list.
This represents the overall linked list.
pm_list_node_t * tail
A pointer to the tail of the list.
pm_list_node_t * head
A pointer to the head of the list.
size_t size
The size of the list.
the getlocal and setlocal instructions require two parameters.
LocalVariableAndWriteNode.
pm_constant_id_t name
LocalVariableAndWriteNode::name.
uint32_t depth
LocalVariableAndWriteNode::depth.
struct pm_node * value
LocalVariableAndWriteNode::value.
LocalVariableOperatorWriteNode.
uint32_t depth
LocalVariableOperatorWriteNode::depth.
pm_constant_id_t binary_operator
LocalVariableOperatorWriteNode::binary_operator.
struct pm_node * value
LocalVariableOperatorWriteNode::value.
pm_constant_id_t name
LocalVariableOperatorWriteNode::name.
LocalVariableOrWriteNode.
uint32_t depth
LocalVariableOrWriteNode::depth.
struct pm_node * value
LocalVariableOrWriteNode::value.
pm_constant_id_t name
LocalVariableOrWriteNode::name.
uint32_t depth
LocalVariableReadNode::depth.
pm_constant_id_t name
LocalVariableReadNode::name.
uint32_t depth
LocalVariableTargetNode::depth.
pm_constant_id_t name
LocalVariableTargetNode::name.
struct pm_node * value
LocalVariableWriteNode::value.
uint32_t depth
LocalVariableWriteNode::depth.
pm_constant_id_t name
LocalVariableWriteNode::name.
This represents a range of bytes in the source string to which a node or token corresponds.
const uint8_t * start
A pointer to the start location of the range in the source.
const uint8_t * end
A pointer to the end location of the range in the source.
struct pm_node * pattern
MatchPredicateNode::pattern.
struct pm_node * value
MatchPredicateNode::value.
struct pm_node * value
MatchRequiredNode::value.
struct pm_node * pattern
MatchRequiredNode::pattern.
struct pm_node_list targets
MatchWriteNode::targets.
struct pm_call_node * call
MatchWriteNode::call.
struct pm_node * constant_path
ModuleNode::constant_path.
struct pm_node * body
ModuleNode::body.
pm_constant_id_list_t locals
ModuleNode::locals.
pm_constant_id_t name
ModuleNode::name.
struct pm_node_list lefts
MultiTargetNode::lefts.
struct pm_node * rest
MultiTargetNode::rest.
struct pm_node_list rights
MultiTargetNode::rights.
This is a node in the multi target state linked list.
As we're compiling a multi target, we need to track additional information whenever there is a parent...
struct pm_node * value
MultiWriteNode::value.
struct pm_node * rest
MultiWriteNode::rest.
struct pm_node_list rights
MultiWriteNode::rights.
struct pm_node_list lefts
MultiWriteNode::lefts.
A list of offsets of newlines in a string.
const uint8_t * start
A pointer to the start of the source string.
size_t * offsets
The list of offsets.
size_t size
The number of offsets in the list.
struct pm_arguments_node * arguments
NextNode::arguments.
A list of nodes in the source, most often used for lists of children.
size_t size
The number of nodes in the list.
struct pm_node ** nodes
The nodes in the list.
This compiler defines its own concept of the location of a node.
int32_t line
This is the line number of a node.
uint32_t node_id
This is a unique identifier for the node.
This is the base structure that represents a node in the syntax tree.
pm_node_type_t type
This represents the type of the node.
uint32_t node_id
The unique identifier for this node, which is deterministic based on the source.
pm_node_flags_t flags
This represents any flags on the node.
pm_location_t location
This is the location of the node in the source.
NumberedReferenceReadNode.
uint32_t number
NumberedReferenceReadNode::number.
OptionalKeywordParameterNode.
pm_constant_id_t name
OptionalKeywordParameterNode::name.
struct pm_node * value
OptionalKeywordParameterNode::value.
struct pm_node * value
OptionalParameterNode::value.
pm_constant_id_t name
OptionalParameterNode::name.
The options that can be passed to the parser.
pm_options_version_t version
The version of prism that we should be parsing with.
struct pm_node * left
OrNode::left.
struct pm_node * right
OrNode::right.
struct pm_node * rest
ParametersNode::rest.
struct pm_node_list requireds
ParametersNode::requireds.
struct pm_block_parameter_node * block
ParametersNode::block.
struct pm_node_list optionals
ParametersNode::optionals.
struct pm_node_list posts
ParametersNode::posts.
pm_node_t base
The embedded base node.
struct pm_node * keyword_rest
ParametersNode::keyword_rest.
struct pm_node_list keywords
ParametersNode::keywords.
struct pm_node * body
ParenthesesNode::body.
An error that is going to be formatted into the output.
pm_diagnostic_t * error
A pointer to the diagnostic that was generated during parsing.
uint32_t column_end
The column end of the diagnostic message.
int32_t line
The start line of the diagnostic message.
uint32_t column_start
The column start of the diagnostic message.
bool parsed
Whether or not this parse result has performed its parsing yet.
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_string_t input
The input that represents the source to be parsed.
pm_parser_t parser
The parser that will do the actual parsing.
pm_options_t options
The options that will be passed to the parser.
This struct represents the overall parser.
const pm_encoding_t * encoding
The encoding functions for the current file is attached to the parser as it's parsing so that it can ...
const uint8_t * end
The pointer to the end of the source.
pm_constant_pool_t constant_pool
This constant pool keeps all of the constants defined throughout the file so that we can reference th...
const uint8_t * start
The pointer to the start of the source.
pm_list_t error_list
The list of errors that have been found while parsing.
pm_list_t warning_list
The list of warnings that have been found while parsing.
int32_t start_line
The line number at the start of the parse.
pm_string_t filepath
This is the path of the file being parsed.
pm_newline_list_t newline_list
This is the list of newline offsets in the source file.
struct pm_node * variable
PinnedVariableNode::variable.
struct pm_statements_node * statements
PostExecutionNode::statements.
struct pm_statements_node * statements
PreExecutionNode::statements.
struct pm_statements_node * statements
ProgramNode::statements.
struct pm_node * right
RangeNode::right.
struct pm_node * left
RangeNode::left.
pm_integer_t denominator
RationalNode::denominator.
pm_integer_t numerator
RationalNode::numerator.
RequiredKeywordParameterNode.
pm_constant_id_t name
RequiredParameterNode::name.
struct pm_node * rescue_expression
RescueModifierNode::rescue_expression.
struct pm_node * expression
RescueModifierNode::expression.
struct pm_rescue_node * subsequent
RescueNode::subsequent.
struct pm_node * reference
RescueNode::reference.
struct pm_node_list exceptions
RescueNode::exceptions.
struct pm_statements_node * statements
RescueNode::statements.
struct pm_arguments_node * arguments
ReturnNode::arguments.
rb_encoding * filepath_encoding
This is the encoding of the actual filepath object that will be used when a FILE node is compiled or ...
struct iseq_link_anchor * pre_execution_anchor
This will only be set on the top-level scope node.
VALUE * script_lines
This is a pointer to the list of script lines for the ISEQs that will be associated with this scope n...
struct pm_node * write
ShareableConstantNode::write.
pm_node_t base
The embedded base node.
pm_constant_id_list_t locals
SingletonClassNode::locals.
struct pm_node * expression
SingletonClassNode::expression.
struct pm_node * body
SingletonClassNode::body.
pm_string_t filepath
SourceFileNode::filepath.
struct pm_node * expression
SplatNode::expression.
struct pm_node_list body
StatementsNode::body.
pm_node_t base
The embedded base node.
pm_string_t unescaped
StringNode::unescaped.
A generic string type that can have various ownership semantics.
struct pm_arguments_node * arguments
SuperNode::arguments.
struct pm_node * block
SuperNode::block.
pm_string_t unescaped
SymbolNode::unescaped.
pm_node_t base
The embedded base node.
struct pm_node_list names
UndefNode::names.
struct pm_statements_node * statements
UnlessNode::statements.
struct pm_node * predicate
UnlessNode::predicate.
struct pm_else_node * else_clause
UnlessNode::else_clause.
pm_node_t base
The embedded base node.
pm_node_t base
The embedded base node.
pm_string_t unescaped
XStringNode::unescaped.
struct pm_arguments_node * arguments
YieldNode::arguments.
struct rb_iseq_constant_body::@157 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_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.