2#include "prism_compile.h"
24#define PUSH_ADJUST(seq, location, label) \
25 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (int) (location).line))
27#define PUSH_ADJUST_RESTORE(seq, label) \
28 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
30#define PUSH_INSN(seq, location, insn) \
31 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 0))
33#define PUSH_INSN1(seq, location, insn, op1) \
34 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 1, (VALUE)(op1)))
36#define PUSH_INSN2(seq, location, insn, op1, op2) \
37 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
39#define PUSH_INSN3(seq, location, insn, op1, op2, op3) \
40 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)))
42#define PUSH_INSNL(seq, location, insn, label) \
43 (PUSH_INSN1(seq, location, insn, label), LABEL_REF(label))
45#define PUSH_LABEL(seq, label) \
46 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
48#define PUSH_SEND_R(seq, location, id, argc, block, flag, keywords) \
49 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (int) (location).line, (int) (location).node_id, (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
51#define PUSH_SEND(seq, location, id, argc) \
52 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
54#define PUSH_SEND_WITH_FLAG(seq, location, id, argc, flag) \
55 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)(flag), NULL)
57#define PUSH_SEND_WITH_BLOCK(seq, location, id, argc, block) \
58 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
60#define PUSH_CALL(seq, location, id, argc) \
61 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
63#define PUSH_CALL_WITH_BLOCK(seq, location, id, argc, block) \
64 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
66#define PUSH_TRACE(seq, event) \
67 ADD_ELEM((seq), (LINK_ELEMENT *) new_trace_body(iseq, (event), 0))
69#define PUSH_CATCH_ENTRY(type, ls, le, iseqv, lc) \
70 ADD_CATCH_ENTRY((type), (ls), (le), (iseqv), (lc))
72#define PUSH_SEQ(seq1, seq2) \
73 APPEND_LIST((seq1), (seq2))
75#define PUSH_SYNTHETIC_PUTNIL(seq, iseq) \
77 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line; \
78 if (lineno == 0) lineno = FIX2INT(rb_iseq_first_lineno(iseq)); \
79 ADD_SYNTHETIC_INSN(seq, lineno, -1, putnil); \
88pm_iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
int line,
int node_id,
int idx,
int level)
90 if (iseq_local_block_param_p(iseq, idx, level)) {
91 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getblockparam), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
94 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getlocal), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
96 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
100pm_iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
int line,
int node_id,
int idx,
int level)
102 if (iseq_local_block_param_p(iseq, idx, level)) {
103 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setblockparam), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
106 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setlocal), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
108 update_lvar_state(iseq, level, idx);
109 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
112#define PUSH_GETLOCAL(seq, location, idx, level) \
113 pm_iseq_add_getlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
115#define PUSH_SETLOCAL(seq, location, idx, level) \
116 pm_iseq_add_setlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
122#define OLD_ISEQ NEW_ISEQ
125#define NEW_ISEQ(node, name, type, line_no) \
126 pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
128#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
131#define NEW_CHILD_ISEQ(node, name, type, line_no) \
132 pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
134#define PM_COMPILE(node) \
135 pm_compile_node(iseq, (node), ret, popped, scope_node)
137#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
138 pm_compile_node(iseq, (node), _ret, popped, scope_node)
140#define PM_COMPILE_POPPED(node) \
141 pm_compile_node(iseq, (node), ret, true, scope_node)
143#define PM_COMPILE_NOT_POPPED(node) \
144 pm_compile_node(iseq, (node), ret, false, scope_node)
147#define PM_INDEX_LOOKUP_TABLE_INIT { .values = NULL, .capacity = 0, .owned = false }
152 int capacity = constants_size + PM_INDEX_LOOKUP_SPECIALS;
153 table->
values = compile_data_alloc2_type(iseq,
int, capacity);
154 memset(table->
values, -1, capacity *
sizeof(
int));
156 table->
owned =
false;
166pm_line_offset_list_line_column_cached(
const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line,
size_t *last_line)
168 size_t hint = *last_line;
169 size_t size = list->
size;
170 const uint32_t *offsets = list->
offsets;
175 if (offsets[hint] <= cursor) {
176 if (hint + 1 >= size || offsets[hint + 1] > cursor) {
179 .line = ((int32_t) hint) + start_line,
180 .column = cursor - offsets[hint]
185 size_t limit = hint + 9;
186 if (limit > size) limit = size;
187 for (
size_t idx = hint + 1; idx < limit; idx++) {
188 if (offsets[idx] > cursor) {
189 *last_line = idx - 1;
191 .line = ((int32_t) (idx - 1)) + start_line,
192 .column = cursor - offsets[idx - 1]
195 if (offsets[idx] == cursor) {
203 size_t limit = hint > 8 ? hint - 8 : 0;
204 for (
size_t idx = hint; idx > limit; idx--) {
205 if (offsets[idx - 1] <= cursor) {
206 *last_line = idx - 1;
208 .line = ((int32_t) (idx - 1)) + start_line,
209 .column = cursor - offsets[idx - 1]
216 pm_line_column_t result = pm_line_offset_list_line_column(list, cursor, start_line);
217 *last_line = (size_t) (result.
line - start_line);
226pm_line_offset_list_line_cached(
const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line,
size_t *last_line)
228 return pm_line_offset_list_line_column_cached(list, cursor, start_line, last_line).
line;
231#define PM_NODE_START_LOCATION(node) \
232 ((pm_node_location_t) { .line = pm_line_offset_list_line_cached(scope_node->line_offsets, ((const pm_node_t *) (node))->location.start, scope_node->start_line, &scope_node->last_line), .node_id = ((const pm_node_t *) (node))->node_id })
234#define PM_NODE_END_LOCATION(node) \
235 ((pm_node_location_t) { .line = pm_line_offset_list_line_cached(scope_node->line_offsets, ((const pm_node_t *) (node))->location.start + ((const pm_node_t *) (node))->location.length, scope_node->start_line, &scope_node->last_line), .node_id = ((const pm_node_t *) (node))->node_id })
237#define PM_LOCATION_START_LOCATION(location, id) \
238 ((pm_node_location_t) { .line = pm_line_offset_list_line_cached(scope_node->line_offsets, (location)->start, scope_node->start_line, &scope_node->last_line), .node_id = id })
240#define PM_NODE_START_LINE_COLUMN(node) \
241 pm_line_offset_list_line_column_cached(scope_node->line_offsets, ((const pm_node_t *) (node))->location.start, scope_node->start_line, &scope_node->last_line)
243#define PM_NODE_END_LINE_COLUMN(node) \
244 pm_line_offset_list_line_column_cached(scope_node->line_offsets, ((const pm_node_t *) (node))->location.start + ((const pm_node_t *) (node))->location.length, scope_node->start_line, &scope_node->last_line)
246#define PM_LOCATION_START_LINE_COLUMN(location) \
247 pm_line_offset_list_line_column_cached(scope_node->line_offsets, (location)->start, scope_node->start_line, &scope_node->last_line)
251 return (
int) pm_line_offset_list_line_column(pm_parser_line_offsets(parser), location->
start, pm_parser_start_line(parser)).line;
259pm_node_line_number_cached(
const pm_node_t *node, pm_scope_node_t *scope_node)
261 return (
int) pm_line_offset_list_line_cached(scope_node->line_offsets, node->
location.
start, scope_node->start_line, &scope_node->
last_line);
265pm_location_line_number_cached(
const pm_location_t *location, pm_scope_node_t *scope_node) {
266 return (
int) pm_line_offset_list_line_cached(scope_node->line_offsets, location->
start, scope_node->start_line, &scope_node->
last_line);
282 if (length <= 2 && !integer->negative) {
290 result = rb_integer_unpack(words, length,
sizeof(uint32_t), 0, flags);
304parse_integer(
const pm_integer_node_t *node)
306 return parse_integer_value(&node->
value);
313parse_float(
const pm_float_node_t *node)
329parse_rational(
const pm_rational_node_t *node)
343parse_imaginary(
const pm_imaginary_node_t *node)
345 VALUE imaginary_part;
346 switch (PM_NODE_TYPE(node->
numeric)) {
347 case PM_FLOAT_NODE: {
348 imaginary_part = parse_float((
const pm_float_node_t *) node->
numeric);
351 case PM_INTEGER_NODE: {
352 imaginary_part = parse_integer((
const pm_integer_node_t *) node->
numeric);
355 case PM_RATIONAL_NODE: {
356 imaginary_part = parse_rational((
const pm_rational_node_t *) node->
numeric);
360 rb_bug(
"Unexpected numeric type on imaginary number %s\n", pm_node_type(PM_NODE_TYPE(node->
numeric)));
367parse_string(
const pm_scope_node_t *scope_node,
const pm_string_t *
string)
369 return rb_enc_str_new((
const char *) pm_string_source(
string), pm_string_length(
string), scope_node->encoding);
380 rb_encoding *encoding;
382 if (node->
flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
385 else if (node->
flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
392 return rb_enc_str_new((
const char *) pm_string_source(
string), pm_string_length(
string), encoding);
396parse_static_literal_string(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_string_t *
string)
398 rb_encoding *encoding;
400 if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
403 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
407 encoding = scope_node->encoding;
410 VALUE value = rb_enc_literal_str((
const char *) pm_string_source(
string), pm_string_length(
string), encoding);
411 rb_enc_str_coderange(value);
413 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
414 int line_number = pm_node_line_number_cached(node, scope_node);
422parse_string_symbol(
const pm_scope_node_t *scope_node,
const pm_symbol_node_t *symbol)
424 rb_encoding *encoding;
425 if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING) {
428 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING) {
431 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING) {
435 encoding = scope_node->encoding;
438 return rb_intern3((
const char *) pm_string_source(&symbol->
unescaped), pm_string_length(&symbol->
unescaped), encoding);
442pm_optimizable_range_item_p(
const pm_node_t *node)
444 return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
449parse_regexp_error(rb_iseq_t *iseq, int32_t line_number,
const char *fmt, ...)
453 VALUE error = rb_syntax_error_append(
Qnil, rb_iseq_path(iseq), line_number, -1, NULL,
"%" PRIsVALUE, args);
459parse_regexp_string_part(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_string_t *unescaped, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding)
463 rb_encoding *encoding;
465 if (explicit_regexp_encoding != NULL) {
466 encoding = explicit_regexp_encoding;
468 else if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
471 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
475 encoding = implicit_regexp_encoding;
478 VALUE string = rb_enc_str_new((
const char *) pm_string_source(unescaped), pm_string_length(unescaped), encoding);
479 VALUE error = rb_reg_check_preprocess(
string);
481 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number_cached(node, scope_node),
"%" PRIsVALUE,
rb_obj_as_string(error));
486pm_static_literal_concat(rb_iseq_t *iseq,
const pm_node_list_t *nodes, pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding,
bool top)
490 for (uint32_t index = 0; index < nodes->
size; index++) {
491 const pm_node_t *part = nodes->
nodes[index];
494 switch (PM_NODE_TYPE(part)) {
496 if (implicit_regexp_encoding != NULL) {
498 string = parse_regexp_string_part(iseq, scope_node, part, &((
const pm_string_node_t *) part)->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
501 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
502 VALUE error = rb_reg_check_preprocess(
string);
503 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number_cached(part, scope_node),
"%" PRIsVALUE,
rb_obj_as_string(error));
507 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
510 case PM_INTERPOLATED_STRING_NODE:
511 string = pm_static_literal_concat(iseq, &((
const pm_interpolated_string_node_t *) part)->parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
513 case PM_EMBEDDED_STATEMENTS_NODE: {
514 const pm_embedded_statements_node_t *
cast = (
const pm_embedded_statements_node_t *) part;
515 string = pm_static_literal_concat(iseq, &
cast->statements->body, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
519 RUBY_ASSERT(
false &&
"unexpected node type in pm_static_literal_concat");
523 if (current !=
Qnil) {
531 return top ? rb_fstring(current) : current;
534#define RE_OPTION_ENCODING_SHIFT 8
535#define RE_OPTION_ENCODING(encoding) (((encoding) & 0xFF) << RE_OPTION_ENCODING_SHIFT)
536#define ARG_ENCODING_NONE 32
537#define ARG_ENCODING_FIXED 16
538#define ENC_ASCII8BIT 1
540#define ENC_Windows_31J 3
548parse_regexp_flags(
const pm_node_t *node)
555 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
556 flags |= ARG_ENCODING_NONE;
559 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
560 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_EUC_JP));
563 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
564 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_Windows_31J));
567 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
568 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_UTF8));
571 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) {
572 flags |= ONIG_OPTION_IGNORECASE;
575 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) {
576 flags |= ONIG_OPTION_MULTILINE;
579 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) {
580 flags |= ONIG_OPTION_EXTEND;
586#undef RE_OPTION_ENCODING_SHIFT
587#undef RE_OPTION_ENCODING
588#undef ARG_ENCODING_FIXED
589#undef ARG_ENCODING_NONE
592#undef ENC_Windows_31J
596parse_regexp_encoding(
const pm_scope_node_t *scope_node,
const pm_node_t *node)
598 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_FORCED_BINARY_ENCODING) || PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
601 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
604 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
605 return rb_enc_get_from_index(ENCINDEX_EUC_JP);
607 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
608 return rb_enc_get_from_index(ENCINDEX_Windows_31J);
616parse_regexp(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node,
VALUE string)
620 int32_t line_number = pm_node_line_number_cached(node, scope_node);
621 VALUE regexp = rb_reg_compile(
string, parse_regexp_flags(node), (
const char *) pm_string_source(pm_parser_filepath(scope_node->parser)), line_number);
625 rb_set_errinfo(errinfo);
627 parse_regexp_error(iseq, line_number,
"%" PRIsVALUE, message);
635parse_regexp_literal(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_string_t *unescaped)
637 rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
638 if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
640 VALUE string = rb_enc_str_new((
const char *) pm_string_source(unescaped), pm_string_length(unescaped), regexp_encoding);
642 return parse_regexp(iseq, scope_node, node,
string);
646parse_regexp_concat(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_node_list_t *parts)
648 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
649 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
651 VALUE string = pm_static_literal_concat(iseq, parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
652 return parse_regexp(iseq, scope_node, node,
string);
655static void pm_compile_node(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node);
658pm_interpolated_node_compile(rb_iseq_t *iseq,
const pm_node_list_t *parts,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding,
bool mutable_result,
bool frozen_result)
661 uint32_t parts_size = parts->
size;
662 bool interpolated =
false;
664 if (parts_size > 0) {
668 for (uint32_t index = 0; index < parts_size; index++) {
669 const pm_node_t *part = parts->
nodes[index];
671 if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
672 const pm_string_node_t *string_node = (
const pm_string_node_t *) part;
675 if (implicit_regexp_encoding == NULL) {
676 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
679 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
682 if (
RTEST(current_string)) {
683 current_string =
rb_str_concat(current_string, string_value);
686 current_string = string_value;
687 if (index != 0) current_location = PM_NODE_END_LOCATION(part);
694 PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE) &&
695 ((
const pm_embedded_statements_node_t *) part)->statements != NULL &&
696 ((
const pm_embedded_statements_node_t *) part)->statements->body.size == 1 &&
697 PM_NODE_TYPE_P(((
const pm_embedded_statements_node_t *) part)->statements->body.nodes[0], PM_STRING_NODE)
699 const pm_string_node_t *string_node = (
const pm_string_node_t *) ((
const pm_embedded_statements_node_t *) part)->statements->body.nodes[0];
702 if (implicit_regexp_encoding == NULL) {
703 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
706 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
709 if (
RTEST(current_string)) {
710 current_string =
rb_str_concat(current_string, string_value);
713 current_string = string_value;
714 current_location = PM_NODE_START_LOCATION(part);
718 if (!
RTEST(current_string)) {
719 rb_encoding *encoding;
721 if (implicit_regexp_encoding != NULL) {
722 if (explicit_regexp_encoding != NULL) {
723 encoding = explicit_regexp_encoding;
725 else if (pm_parser_encoding_us_ascii(scope_node->parser)) {
729 encoding = implicit_regexp_encoding;
733 encoding = scope_node->encoding;
742 if (index == 0 && (implicit_regexp_encoding == NULL || parts_size == 1)) {
743 current_string = rb_enc_str_new(NULL, 0, encoding);
748 current_location = PM_NODE_START_LOCATION(part);
752 if (
RTEST(current_string)) {
753 VALUE operand = rb_fstring(current_string);
754 PUSH_INSN1(ret, current_location, putobject, operand);
758 PM_COMPILE_NOT_POPPED(part);
761 PUSH_INSN(ret, current_location, dup);
764 const struct rb_callinfo *callinfo = new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE);
765 PUSH_INSN1(ret, current_location, objtostring, callinfo);
768 PUSH_INSN(ret, current_location, anytostring);
770 current_string =
Qnil;
776 if (
RTEST(current_string)) {
777 current_string = rb_fstring(current_string);
779 if (stack_size == 0) {
781 PUSH_INSN1(ret, current_location, putobject, current_string);
782 }
else if (mutable_result || interpolated) {
783 PUSH_INSN1(ret, current_location, dupstring, current_string);
785 PUSH_INSN1(ret, current_location, dupchilledstring, current_string);
788 PUSH_INSN1(ret, current_location, putobject, current_string);
791 current_string =
Qnil;
796 PUSH_INSN(ret, *node_location, putnil);
803pm_compile_regexp_dynamic(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_list_t *parts,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
805 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
806 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
808 int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false,
false);
809 PUSH_INSN2(ret, *node_location, toregexp,
INT2FIX(parse_regexp_flags(node) & 0xFF),
INT2FIX(length));
817pm_static_literal_string(rb_iseq_t *iseq,
VALUE string,
int line_number)
819 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
820 VALUE str = rb_str_with_debug_created_info(
string, rb_iseq_path(iseq), line_number);
825 return rb_fstring(
string);
835pm_static_literal_value(rb_iseq_t *iseq,
const pm_node_t *node, pm_scope_node_t *scope_node)
839 RUBY_ASSERT(PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL));
841 switch (PM_NODE_TYPE(node)) {
842 case PM_ARRAY_NODE: {
843 const pm_array_node_t *
cast = (
const pm_array_node_t *) node;
844 const pm_node_list_t *elements = &
cast->elements;
847 for (uint32_t index = 0; index < elements->
size; index++) {
848 rb_ary_push(value, pm_static_literal_value(iseq, elements->
nodes[index], scope_node));
857 return parse_float((
const pm_float_node_t *) node);
859 const pm_hash_node_t *
cast = (
const pm_hash_node_t *) node;
860 const pm_node_list_t *elements = &
cast->elements;
863 for (uint32_t index = 0; index < elements->
size; index++) {
865 const pm_assoc_node_t *
cast = (
const pm_assoc_node_t *) elements->
nodes[index];
866 VALUE pair[2] = { pm_static_literal_value(iseq,
cast->key, scope_node), pm_static_literal_value(iseq,
cast->value, scope_node) };
877 case PM_IMAGINARY_NODE:
878 return parse_imaginary((
const pm_imaginary_node_t *) node);
879 case PM_INTEGER_NODE:
880 return parse_integer((
const pm_integer_node_t *) node);
881 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
882 const pm_interpolated_match_last_line_node_t *
cast = (
const pm_interpolated_match_last_line_node_t *) node;
883 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &
cast->parts);
885 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
886 const pm_interpolated_regular_expression_node_t *
cast = (
const pm_interpolated_regular_expression_node_t *) node;
887 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &
cast->parts);
889 case PM_INTERPOLATED_STRING_NODE: {
890 VALUE string = pm_static_literal_concat(iseq, &((
const pm_interpolated_string_node_t *) node)->parts, scope_node, NULL, NULL,
false);
891 int line_number = pm_node_line_number_cached(node, scope_node);
892 return pm_static_literal_string(iseq,
string, line_number);
894 case PM_INTERPOLATED_SYMBOL_NODE: {
895 const pm_interpolated_symbol_node_t *
cast = (
const pm_interpolated_symbol_node_t *) node;
896 VALUE string = pm_static_literal_concat(iseq, &
cast->parts, scope_node, NULL, NULL,
true);
898 return ID2SYM(rb_intern_str(
string));
900 case PM_MATCH_LAST_LINE_NODE: {
901 const pm_match_last_line_node_t *
cast = (
const pm_match_last_line_node_t *) node;
902 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &
cast->unescaped);
906 case PM_RATIONAL_NODE:
907 return parse_rational((
const pm_rational_node_t *) node);
908 case PM_REGULAR_EXPRESSION_NODE: {
909 const pm_regular_expression_node_t *
cast = (
const pm_regular_expression_node_t *) node;
910 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &
cast->unescaped);
912 case PM_SOURCE_ENCODING_NODE:
913 return rb_enc_from_encoding(scope_node->encoding);
914 case PM_SOURCE_LINE_NODE:
915 return INT2FIX(pm_node_line_number_cached(node, scope_node));
916 case PM_STRING_NODE: {
917 const pm_string_node_t *
cast = (
const pm_string_node_t *) node;
918 return parse_static_literal_string(iseq, scope_node, node, &
cast->unescaped);
921 return ID2SYM(parse_string_symbol(scope_node, (
const pm_symbol_node_t *) node));
925 rb_bug(
"Don't have a literal value for node type %s", pm_node_type(PM_NODE_TYPE(node)));
933static rb_code_location_t
934pm_code_location(pm_scope_node_t *scope_node,
const pm_node_t *node)
939 return (rb_code_location_t) {
940 .beg_pos = { .lineno = start_location.
line, .column = start_location.
column },
941 .end_pos = { .lineno = end_location.
line, .column = end_location.
column }
950#define PM_BRANCH_COVERAGE_P(iseq) (ISEQ_COVERAGE(iseq) && ISEQ_BRANCH_COVERAGE(iseq))
953pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const pm_node_t *cond,
954 LABEL *then_label, LABEL *else_label, pm_scope_node_t *scope_node);
957pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, pm_node_t *cond, LABEL *then_label, LABEL *else_label, pm_scope_node_t *scope_node)
963 LABEL *label = NEW_LABEL(location.
line);
964 if (!then_label) then_label = label;
965 else if (!else_label) else_label = label;
967 pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, scope_node);
969 if (LIST_INSN_SIZE_ONE(seq)) {
970 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
971 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
return;
975 PUSH_LABEL(seq, label);
983pm_compile_flip_flop_bound(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
987 if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
988 PM_COMPILE_NOT_POPPED(node);
991 PUSH_INSN1(ret, location, getglobal, operand);
993 PUSH_SEND(ret, location, idEq,
INT2FIX(1));
994 if (popped) PUSH_INSN(ret, location, pop);
1002pm_compile_flip_flop(
const pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LABEL *then_label, rb_iseq_t *iseq,
const int lineno, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
1005 LABEL *lend = NEW_LABEL(location.
line);
1007 int again = !(flip_flop_node->
base.
flags & PM_RANGE_FLAGS_EXCLUDE_END);
1009 rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
1012 PUSH_INSN2(ret, location, getspecial, key,
INT2FIX(0));
1013 PUSH_INSNL(ret, location, branchif, lend);
1015 if (flip_flop_node->
left) {
1016 pm_compile_flip_flop_bound(iseq, flip_flop_node->
left, ret, popped, scope_node);
1019 PUSH_INSN(ret, location, putnil);
1022 PUSH_INSNL(ret, location, branchunless, else_label);
1023 PUSH_INSN1(ret, location, putobject,
Qtrue);
1024 PUSH_INSN1(ret, location, setspecial, key);
1026 PUSH_INSNL(ret, location, jump, then_label);
1029 PUSH_LABEL(ret, lend);
1030 if (flip_flop_node->
right) {
1031 pm_compile_flip_flop_bound(iseq, flip_flop_node->
right, ret, popped, scope_node);
1034 PUSH_INSN(ret, location, putnil);
1037 PUSH_INSNL(ret, location, branchunless, then_label);
1038 PUSH_INSN1(ret, location, putobject,
Qfalse);
1039 PUSH_INSN1(ret, location, setspecial, key);
1040 PUSH_INSNL(ret, location, jump, then_label);
1043static void pm_compile_defined_expr(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node,
bool in_condition);
1046pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const pm_node_t *cond, LABEL *then_label, LABEL *else_label, pm_scope_node_t *scope_node)
1051 switch (PM_NODE_TYPE(cond)) {
1053 const pm_and_node_t *
cast = (
const pm_and_node_t *) cond;
1054 pm_compile_logical(iseq, ret,
cast->left, NULL, else_label, scope_node);
1060 const pm_or_node_t *
cast = (
const pm_or_node_t *) cond;
1061 pm_compile_logical(iseq, ret,
cast->left, then_label, NULL, scope_node);
1068 PUSH_INSNL(ret, location, jump, else_label);
1071 case PM_IMAGINARY_NODE:
1072 case PM_INTEGER_NODE:
1073 case PM_LAMBDA_NODE:
1074 case PM_RATIONAL_NODE:
1075 case PM_REGULAR_EXPRESSION_NODE:
1076 case PM_STRING_NODE:
1077 case PM_SYMBOL_NODE:
1079 PUSH_INSNL(ret, location, jump, then_label);
1081 case PM_FLIP_FLOP_NODE:
1082 pm_compile_flip_flop((
const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.
line, ret,
false, scope_node);
1084 case PM_DEFINED_NODE: {
1085 const pm_defined_node_t *
cast = (
const pm_defined_node_t *) cond;
1086 pm_compile_defined_expr(iseq,
cast->value, &location, ret,
false, scope_node,
true);
1090 DECL_ANCHOR(cond_seq);
1091 pm_compile_node(iseq, cond, cond_seq,
false, scope_node);
1093 if (LIST_INSN_SIZE_ONE(cond_seq)) {
1094 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
1096 if (insn->insn_id == BIN(putobject)) {
1097 if (
RTEST(insn->operands[0])) {
1098 PUSH_INSNL(ret, location, jump, then_label);
1103 PUSH_INSNL(ret, location, jump, else_label);
1109 PUSH_SEQ(ret, cond_seq);
1114 PUSH_INSNL(ret, location, branchunless, else_label);
1115 PUSH_INSNL(ret, location, jump, then_label);
1122pm_compile_conditional(rb_iseq_t *iseq,
const pm_node_location_t *node_location, pm_node_type_t
type,
const pm_node_t *node,
const pm_statements_node_t *statements,
const pm_node_t *subsequent,
const pm_node_t *predicate, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
1125 LABEL *then_label = NEW_LABEL(location.
line);
1126 LABEL *else_label = NEW_LABEL(location.
line);
1127 LABEL *end_label = NULL;
1129 DECL_ANCHOR(cond_seq);
1130 pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, scope_node);
1131 PUSH_SEQ(ret, cond_seq);
1133 rb_code_location_t conditional_location = { 0 };
1136 if (then_label->refcnt && else_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1137 conditional_location = pm_code_location(scope_node, node);
1138 branches = decl_branch_base(iseq, (
int) node->
node_id, &conditional_location,
type == PM_IF_NODE ?
"if" :
"unless");
1141 if (then_label->refcnt) {
1142 PUSH_LABEL(ret, then_label);
1144 DECL_ANCHOR(then_seq);
1146 if (statements != NULL) {
1147 pm_compile_node(iseq, (
const pm_node_t *) statements, then_seq, popped, scope_node);
1150 PUSH_SYNTHETIC_PUTNIL(then_seq, iseq);
1153 if (else_label->refcnt) {
1155 if (PM_BRANCH_COVERAGE_P(iseq)) {
1156 rb_code_location_t branch_location;
1158 if (statements != NULL) {
1159 branch_location = pm_code_location(scope_node, (
const pm_node_t *) statements);
1160 }
else if (
type == PM_IF_NODE) {
1162 branch_location = (rb_code_location_t) {
1163 .beg_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column },
1164 .end_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column }
1167 branch_location = conditional_location;
1170 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
type == PM_IF_NODE ?
"then" :
"else", branches);
1173 end_label = NEW_LABEL(location.
line);
1174 PUSH_INSNL(then_seq, location, jump, end_label);
1175 if (!popped) PUSH_INSN(then_seq, location, pop);
1178 PUSH_SEQ(ret, then_seq);
1181 if (else_label->refcnt) {
1182 PUSH_LABEL(ret, else_label);
1184 DECL_ANCHOR(else_seq);
1186 if (subsequent != NULL) {
1187 pm_compile_node(iseq, subsequent, else_seq, popped, scope_node);
1190 PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
1194 if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1195 rb_code_location_t branch_location;
1197 if (subsequent == NULL) {
1198 branch_location = conditional_location;
1199 }
else if (PM_NODE_TYPE_P(subsequent, PM_ELSE_NODE)) {
1200 const pm_else_node_t *else_node = (
const pm_else_node_t *) subsequent;
1201 branch_location = pm_code_location(scope_node, else_node->
statements != NULL ? ((
const pm_node_t *) else_node->
statements) : (
const pm_node_t *) else_node);
1203 branch_location = pm_code_location(scope_node, (
const pm_node_t *) subsequent);
1206 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1,
type == PM_IF_NODE ?
"else" :
"then", branches);
1209 PUSH_SEQ(ret, else_seq);
1213 PUSH_LABEL(ret, end_label);
1223pm_compile_loop(rb_iseq_t *iseq,
const pm_node_location_t *node_location, pm_node_flags_t flags,
enum pm_node_type
type,
const pm_node_t *node,
const pm_statements_node_t *statements,
const pm_node_t *predicate, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
1227 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
1228 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
1229 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
1231 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.
line);
1232 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.
line);
1233 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.
line);
1234 LABEL *end_label = NEW_LABEL(location.
line);
1235 LABEL *adjust_label = NEW_LABEL(location.
line);
1237 LABEL *next_catch_label = NEW_LABEL(location.
line);
1238 LABEL *tmp_label = NULL;
1244 push_ensure_entry(iseq, &enl, NULL, NULL);
1247 if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
1248 tmp_label = NEW_LABEL(location.
line);
1249 PUSH_INSNL(ret, location, jump, tmp_label);
1253 PUSH_INSNL(ret, location, jump, next_label);
1256 PUSH_LABEL(ret, adjust_label);
1257 PUSH_INSN(ret, location, putnil);
1258 PUSH_LABEL(ret, next_catch_label);
1259 PUSH_INSN(ret, location, pop);
1260 PUSH_INSNL(ret, location, jump, next_label);
1261 if (tmp_label) PUSH_LABEL(ret, tmp_label);
1263 PUSH_LABEL(ret, redo_label);
1266 if (PM_BRANCH_COVERAGE_P(iseq)) {
1267 rb_code_location_t loop_location = pm_code_location(scope_node, node);
1268 VALUE branches = decl_branch_base(iseq, (
int) node->
node_id, &loop_location,
type == PM_WHILE_NODE ?
"while" :
"until");
1270 rb_code_location_t branch_location = statements != NULL ? pm_code_location(scope_node, (
const pm_node_t *) statements) : loop_location;
1271 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
"body", branches);
1274 if (statements != NULL) PM_COMPILE_POPPED((
const pm_node_t *) statements);
1275 PUSH_LABEL(ret, next_label);
1277 if (
type == PM_WHILE_NODE) {
1278 pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, scope_node);
1280 else if (
type == PM_UNTIL_NODE) {
1281 pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, scope_node);
1284 PUSH_LABEL(ret, end_label);
1285 PUSH_ADJUST_RESTORE(ret, adjust_label);
1286 PUSH_INSN(ret, location, putnil);
1288 PUSH_LABEL(ret, break_label);
1289 if (popped) PUSH_INSN(ret, location, pop);
1291 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
1292 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
1293 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL, ISEQ_COMPILE_DATA(iseq)->redo_label);
1295 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
1296 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
1297 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
1298 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
1306static pm_local_index_t
1307pm_lookup_local_index(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
pm_constant_id_t constant_id,
int start_depth)
1309 pm_local_index_t lindex = { 0 };
1313 for (level = 0; level < start_depth; level++) {
1314 scope_node = scope_node->previous;
1317 while (!pm_index_lookup_table_lookup(&scope_node->
index_lookup_table, constant_id, &local_index))
1321 if (scope_node->previous) {
1322 scope_node = scope_node->previous;
1327 rb_bug(
"Local with constant_id %u does not exist", (
unsigned int) constant_id);
1331 lindex.level = level;
1332 lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
1343pm_constant_id_lookup(
const pm_scope_node_t *scope_node,
pm_constant_id_t constant_id)
1347 RUBY_ASSERT(index < pm_parser_constants_size(scope_node->parser));
1348 return scope_node->constants[index];
1352pm_new_child_iseq(rb_iseq_t *iseq, pm_scope_node_t *node,
VALUE name,
const rb_iseq_t *parent,
enum rb_iseq_type
type,
int line_no)
1354 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1355 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1356 rb_iseq_t *ret_iseq = pm_iseq_build(node, name,
1357 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1359 isolated_depth ? isolated_depth + 1 : 0,
1360 type, ISEQ_COMPILE_DATA(iseq)->option);
1361 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1366pm_cpath_const_p(
const pm_node_t *node)
1368 switch (PM_NODE_TYPE(node)) {
1369 case PM_CONSTANT_READ_NODE:
1371 case PM_CONSTANT_PATH_NODE:
1373 const pm_node_t *parent = ((
const pm_constant_path_node_t *) node)->parent;
1374 if (!parent)
return TRUE;
1375 return pm_cpath_const_p(parent);
1383pm_compile_class_path(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
1385 if (PM_NODE_TYPE_P(node, PM_CONSTANT_PATH_NODE)) {
1386 const pm_node_t *parent = ((
const pm_constant_path_node_t *) node)->parent;
1391 int flags = VM_DEFINECLASS_FLAG_SCOPED;
1392 if (!pm_cpath_const_p(parent)) {
1393 flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
1399 PUSH_INSN1(ret, *node_location, putobject,
rb_cObject);
1400 return VM_DEFINECLASS_FLAG_SCOPED;
1405 PUSH_INSN1(ret, *node_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
1415pm_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)
1418 LABEL *lfin = NEW_LABEL(location.
line);
1419 LABEL *lcfin = NEW_LABEL(location.
line);
1420 LABEL *lskip = NULL;
1422 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1423 ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
1425 PM_COMPILE_NOT_POPPED(receiver);
1427 lskip = NEW_LABEL(location.
line);
1428 PUSH_INSN(ret, location, dup);
1429 PUSH_INSNL(ret, location, branchnil, lskip);
1432 PUSH_INSN(ret, location, dup);
1433 PUSH_SEND_WITH_FLAG(ret, location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
1434 if (!popped) PUSH_INSN(ret, location, dup);
1437 PUSH_INSNL(ret, location, branchunless, lcfin);
1440 PUSH_INSNL(ret, location, branchif, lcfin);
1443 if (!popped) PUSH_INSN(ret, location, pop);
1444 PM_COMPILE_NOT_POPPED(value);
1447 PUSH_INSN(ret, location, swap);
1448 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
1451 ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
1452 PUSH_SEND_WITH_FLAG(ret, location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
1453 PUSH_INSNL(ret, location, jump, lfin);
1455 PUSH_LABEL(ret, lcfin);
1456 if (!popped) PUSH_INSN(ret, location, swap);
1458 PUSH_LABEL(ret, lfin);
1460 if (lskip && popped) PUSH_LABEL(ret, lskip);
1461 PUSH_INSN(ret, location, pop);
1462 if (lskip && !popped) PUSH_LABEL(ret, lskip);
1465static void pm_compile_shareable_constant_value(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_flags_t shareability,
VALUE path, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node,
bool top);
1473pm_compile_hash_elements(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_list_t *elements,
const pm_node_flags_t shareability,
VALUE path,
bool argument, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node)
1481 const int max_stack_length = 0x100;
1482 const unsigned int min_tmp_hash_length = 0x800;
1484 int stack_length = 0;
1485 bool first_chunk =
true;
1486 bool owned_hash =
false;
1492 bool static_literal =
false;
1494 DECL_ANCHOR(anchor);
1497#define FLUSH_CHUNK \
1498 if (stack_length) { \
1499 if (first_chunk) { \
1500 PUSH_SEQ(ret, anchor); \
1501 PUSH_INSN1(ret, location, newhash, INT2FIX(stack_length)); \
1502 first_chunk = false; \
1503 owned_hash = true; \
1506 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
1507 PUSH_INSN(ret, location, swap); \
1508 PUSH_SEQ(ret, anchor); \
1510 PUSH_SEND(ret, location, id_core_hash_merge_bang_ptr, INT2FIX(stack_length + 1)); \
1513 PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(stack_length + 1)); \
1514 owned_hash = true; \
1517 INIT_ANCHOR(anchor); \
1521 for (uint32_t index = 0; index < elements->
size; index++) {
1522 const pm_node_t *element = elements->
nodes[index];
1524 switch (PM_NODE_TYPE(element)) {
1525 case PM_ASSOC_NODE: {
1528 (shareability == 0) &&
1529 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && (
1530 (!static_literal && ((index + min_tmp_hash_length) < elements->
size)) ||
1531 (first_chunk && stack_length == 0)
1536 while (index + count < elements->size && PM_NODE_FLAG_P(elements->
nodes[index + count], PM_NODE_FLAG_STATIC_LITERAL)) count++;
1538 bool first_element = first_chunk && stack_length == 0;
1540 if (first_element || count >= min_tmp_hash_length) {
1546 for (uint32_t tmp_end = index + count; index < tmp_end; index++) {
1547 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[index];
1550 pm_static_literal_value(iseq, assoc->key, scope_node),
1551 pm_static_literal_value(iseq, assoc->value, scope_node)
1566 if (count == elements->
size) {
1568 PUSH_INSN1(ret, location, duphash, hash);
1574 first_chunk =
false;
1577 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1578 PUSH_INSN(ret, location, swap);
1579 PUSH_INSN1(ret, location, putobject, hash);
1581 PUSH_SEND(ret, location, id_core_hash_merge_bang_kwd,
INT2FIX(2));
1584 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1592 static_literal =
true;
1596 static_literal =
false;
1601 if (shareability == 0) {
1602 pm_compile_node(iseq, element, anchor,
false, scope_node);
1605 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
1606 pm_compile_shareable_constant_value(iseq, assoc->
key, shareability, path, ret, scope_node,
false);
1607 pm_compile_shareable_constant_value(iseq, assoc->
value, shareability, path, ret, scope_node,
false);
1610 if ((stack_length += 2) >= max_stack_length) FLUSH_CHUNK;
1613 case PM_ASSOC_SPLAT_NODE: {
1616 const pm_assoc_splat_node_t *assoc_splat = (
const pm_assoc_splat_node_t *) element;
1617 bool empty_hash = assoc_splat->
value != NULL && (
1618 (PM_NODE_TYPE_P(assoc_splat->
value, PM_HASH_NODE) && ((
const pm_hash_node_t *) assoc_splat->
value)->elements.size == 0) ||
1619 PM_NODE_TYPE_P(assoc_splat->
value, PM_NIL_NODE)
1622 bool first_element = first_chunk && stack_length == 0;
1623 bool last_element = index == elements->
size - 1;
1624 bool only_element = first_element && last_element;
1626 if (only_element && argument) {
1630 PUSH_INSN(ret, location, putnil);
1636 if (shareability == 0) {
1637 PM_COMPILE_NOT_POPPED(element);
1640 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1644 else if (empty_hash && first_element) {
1645 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1648 else if (!empty_hash || !owned_hash) {
1653 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1654 if (first_element) {
1655 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1658 PUSH_INSN(ret, location, swap);
1664 PUSH_INSN(ret, location, putnil);
1666 else if (shareability == 0) {
1667 PM_COMPILE_NOT_POPPED(element);
1670 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1673 if (first_element || owned_hash) {
1674 PUSH_SEND(ret, location, id_core_hash_merge_bang_kwd,
INT2FIX(2));
1677 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1682 first_chunk =
false;
1683 static_literal =
false;
1687 RUBY_ASSERT(
"Invalid node type for hash" &&
false);
1696#define SPLATARRAY_FALSE 0
1697#define SPLATARRAY_TRUE 1
1698#define DUP_SINGLE_KW_SPLAT 2
1702pm_setup_args_core(
const pm_arguments_node_t *arguments_node,
const pm_node_t *block,
int *flags,
const bool has_regular_blockarg,
struct rb_callinfo_kwarg **kw_arg,
int *dup_rest, rb_iseq_t *iseq, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node,
const pm_node_location_t *node_location)
1707 bool has_splat =
false;
1708 bool has_keyword_splat =
false;
1710 if (arguments_node == NULL) {
1711 if (*flags & VM_CALL_FCALL) {
1712 *flags |= VM_CALL_VCALL;
1716 const pm_node_list_t *arguments = &arguments_node->
arguments;
1717 has_keyword_splat = PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
1721 int post_splat_counter = 0;
1722 const pm_node_t *argument;
1725 switch (PM_NODE_TYPE(argument)) {
1727 case PM_KEYWORD_HASH_NODE: {
1728 const pm_keyword_hash_node_t *keyword_arg = (
const pm_keyword_hash_node_t *) argument;
1729 const pm_node_list_t *elements = &keyword_arg->
elements;
1731 if (has_keyword_splat || has_splat) {
1732 *flags |= VM_CALL_KW_SPLAT;
1733 has_keyword_splat =
true;
1735 if (elements->
size > 1 || !(elements->
size == 1 && PM_NODE_TYPE_P(elements->
nodes[0], PM_ASSOC_SPLAT_NODE))) {
1739 *flags |= VM_CALL_KW_SPLAT_MUT;
1740 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1742 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
1743 *flags |= VM_CALL_KW_SPLAT_MUT;
1744 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1745 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1746 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1747 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1750 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1756 if (PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS)) {
1765 for (uint32_t element_index = 0; element_index < elements->
size; element_index++) {
1766 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[element_index];
1770 VALUE keyword = pm_static_literal_value(iseq, assoc->key, scope_node);
1771 VALUE stored_index = rb_hash_aref(stored_indices, keyword);
1776 if (!
NIL_P(stored_index)) {
1784 rb_hash_aset(stored_indices, keyword,
ULONG2NUM(element_index));
1789 if (size > VM_CALL_KW_LEN_MAX) {
1790 COMPILE_ERROR(iseq, node_location->
line,
"too many keyword arguments (%d, maximum is %d)",
1791 (
int) size, (
int) VM_CALL_KW_LEN_MAX);
1795 *flags |= VM_CALL_KWARG;
1797 VALUE *keywords = (*kw_arg)->keywords;
1798 (*kw_arg)->references = 0;
1799 (*kw_arg)->keyword_len = (int) size;
1801 uint32_t keyword_index = 0;
1802 for (uint32_t element_index = 0; element_index < elements->
size; element_index++) {
1803 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[element_index];
1807 keywords[keyword_index++] = pm_static_literal_value(iseq, assoc->key, scope_node);
1811 PM_COMPILE(assoc->value);
1820 *flags |= VM_CALL_KW_SPLAT;
1822 uint32_t size = elements->
size;
1827 *flags |= VM_CALL_KW_SPLAT_MUT;
1830 for (uint32_t element_index = 0; element_index < size; element_index++) {
1831 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[element_index];
1832 PM_COMPILE_NOT_POPPED(assoc->key);
1833 PM_COMPILE_NOT_POPPED(assoc->value);
1836 PUSH_INSN1(ret, location, newhash,
INT2FIX(size * 2));
1841 case PM_SPLAT_NODE: {
1842 *flags |= VM_CALL_ARGS_SPLAT;
1843 const pm_splat_node_t *splat_node = (
const pm_splat_node_t *) argument;
1846 PM_COMPILE_NOT_POPPED(splat_node->
expression);
1849 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1850 PUSH_GETLOCAL(ret, location, index.index, index.level);
1853 bool first_splat = !has_splat;
1861 if (index + 1 < arguments->
size || has_regular_blockarg) {
1862 PUSH_INSN1(ret, location, splatarray, (*dup_rest & SPLATARRAY_TRUE) ?
Qtrue :
Qfalse);
1863 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
1871 PUSH_INSN1(ret, location, splatarray,
Qfalse);
1881 PUSH_INSN(ret, location, concattoarray);
1885 post_splat_counter = 0;
1889 case PM_FORWARDING_ARGUMENTS_NODE: {
1890 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
1892 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
1893 *flags |= VM_CALL_FORWARDING;
1895 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
1896 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1908 *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
1915 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1916 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1920 PUSH_INSN(ret, location, concattoarray);
1922 PUSH_INSN1(ret, location, splatarray,
Qfalse);
1926 pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
1927 PUSH_GETLOCAL(ret, location, pow_local.index, pow_local.level);
1930 pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
1931 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(and_local.level));
1936 post_splat_counter++;
1937 PM_COMPILE_NOT_POPPED(argument);
1961 if (index == arguments->
size - 1) {
1963 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(post_splat_counter));
1966 pm_node_t *next_arg = arguments->
nodes[index + 1];
1968 switch (PM_NODE_TYPE(next_arg)) {
1970 case PM_KEYWORD_HASH_NODE: {
1971 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1972 PUSH_INSN(ret, location, concatarray);
1975 case PM_SPLAT_NODE: {
1976 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1977 PUSH_INSN(ret, location, concatarray);
1980 case PM_FORWARDING_ARGUMENTS_NODE: {
1981 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(post_splat_counter));
1997 if (has_splat) orig_argc++;
1998 if (has_keyword_splat) orig_argc++;
2007pm_setup_args_dup_rest_p(
const pm_node_t *node)
2009 switch (PM_NODE_TYPE(node)) {
2010 case PM_BACK_REFERENCE_READ_NODE:
2011 case PM_CLASS_VARIABLE_READ_NODE:
2012 case PM_CONSTANT_READ_NODE:
2015 case PM_GLOBAL_VARIABLE_READ_NODE:
2016 case PM_IMAGINARY_NODE:
2017 case PM_INSTANCE_VARIABLE_READ_NODE:
2018 case PM_INTEGER_NODE:
2019 case PM_LAMBDA_NODE:
2020 case PM_LOCAL_VARIABLE_READ_NODE:
2022 case PM_NUMBERED_REFERENCE_READ_NODE:
2023 case PM_RATIONAL_NODE:
2024 case PM_REGULAR_EXPRESSION_NODE:
2026 case PM_STRING_NODE:
2027 case PM_SYMBOL_NODE:
2030 case PM_CONSTANT_PATH_NODE: {
2031 const pm_constant_path_node_t *
cast = (
const pm_constant_path_node_t *) node;
2032 if (
cast->parent != NULL) {
2033 return pm_setup_args_dup_rest_p(
cast->parent);
2037 case PM_IMPLICIT_NODE:
2038 return pm_setup_args_dup_rest_p(((
const pm_implicit_node_t *) node)->value);
2039 case PM_ARRAY_NODE: {
2040 const pm_array_node_t *
cast = (
const pm_array_node_t *) node;
2041 for (uint32_t index = 0; index <
cast->elements.size; index++) {
2042 if (pm_setup_args_dup_rest_p(
cast->elements.nodes[index])) {
2057pm_setup_args(
const pm_arguments_node_t *arguments_node,
const pm_node_t *block,
int *flags,
struct rb_callinfo_kwarg **kw_arg, rb_iseq_t *iseq, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node,
const pm_node_location_t *node_location)
2059 int dup_rest = SPLATARRAY_TRUE;
2061 const pm_node_list_t *arguments;
2062 uint32_t arguments_size;
2067 arguments_node != NULL &&
2068 (arguments = &arguments_node->
arguments, arguments_size = arguments->
size) >= 2 &&
2069 PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_SPLAT) &&
2070 !PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_MULTIPLE_SPLATS) &&
2071 PM_NODE_TYPE_P(arguments->
nodes[arguments_size - 1], PM_KEYWORD_HASH_NODE)
2076 dup_rest = SPLATARRAY_FALSE;
2078 const pm_keyword_hash_node_t *keyword_hash = (
const pm_keyword_hash_node_t *) arguments->
nodes[arguments_size - 1];
2079 const pm_node_list_t *elements = &keyword_hash->elements;
2081 for (uint32_t index = 0; dup_rest == SPLATARRAY_FALSE && index < elements->
size; index++) {
2082 const pm_node_t *element = elements->
nodes[index];
2084 switch (PM_NODE_TYPE(element)) {
2085 case PM_ASSOC_NODE: {
2086 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
2087 if (pm_setup_args_dup_rest_p(assoc->
key) || pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
2090 case PM_ASSOC_SPLAT_NODE: {
2091 const pm_assoc_splat_node_t *assoc = (
const pm_assoc_splat_node_t *) element;
2092 if (assoc->
value != NULL && pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
2101 int initial_dup_rest = dup_rest;
2104 if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
2108 bool regular_block_arg =
true;
2109 const pm_node_t *block_expr = ((
const pm_block_argument_node_t *)block)->expression;
2111 if (block_expr && pm_setup_args_dup_rest_p(block_expr)) {
2112 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
2113 initial_dup_rest = dup_rest;
2116 DECL_ANCHOR(block_arg);
2117 pm_compile_node(iseq, block, block_arg,
false, scope_node);
2119 *flags |= VM_CALL_ARGS_BLOCKARG;
2121 if (LIST_INSN_SIZE_ONE(block_arg)) {
2122 LINK_ELEMENT *elem = FIRST_ELEMENT(block_arg);
2123 if (IS_INSN(elem)) {
2124 INSN *iobj = (INSN *) elem;
2125 if (iobj->insn_id == BIN(getblockparam)) {
2126 iobj->insn_id = BIN(getblockparamproxy);
2133 regular_block_arg =
false;
2137 argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
2138 PUSH_SEQ(ret, block_arg);
2141 argc = pm_setup_args_core(arguments_node, block, flags,
false, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
2148 if (*flags & VM_CALL_ARGS_SPLAT && dup_rest != initial_dup_rest) {
2149 *flags |= VM_CALL_ARGS_SPLAT_MUT;
2166pm_compile_index_operator_write_node(rb_iseq_t *iseq,
const pm_index_operator_write_node_t *node,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
2169 if (!popped) PUSH_INSN(ret, location, putnil);
2171 PM_COMPILE_NOT_POPPED(node->
receiver);
2173 int boff = (node->
block == NULL ? 0 : 1);
2174 int flag = PM_NODE_TYPE_P(node->
receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2176 int argc = pm_setup_args(node->
arguments, (
const pm_node_t *) node->
block, &flag, &keywords, iseq, ret, scope_node, node_location);
2178 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2180 PUSH_INSN(ret, location, splatkw);
2183 PUSH_INSN(ret, location, dup);
2184 PUSH_INSN(ret, location, splatkw);
2185 PUSH_INSN(ret, location, pop);
2189 int dup_argn = argc + 1 + boff;
2190 int keyword_len = 0;
2193 keyword_len = keywords->keyword_len;
2194 dup_argn += keyword_len;
2197 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2198 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2199 PM_COMPILE_NOT_POPPED(node->
value);
2202 PUSH_SEND(ret, location, id_operator,
INT2FIX(1));
2205 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2207 if (flag & VM_CALL_ARGS_SPLAT) {
2208 if (flag & VM_CALL_KW_SPLAT) {
2209 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2211 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2212 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2213 flag |= VM_CALL_ARGS_SPLAT_MUT;
2216 PUSH_INSN(ret, location, swap);
2217 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2218 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2219 PUSH_INSN(ret, location, pop);
2223 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2224 PUSH_INSN(ret, location, swap);
2225 PUSH_INSN(ret, location, pop);
2227 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2228 PUSH_INSN(ret, location, swap);
2229 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2230 PUSH_INSN(ret, location, swap);
2231 flag |= VM_CALL_ARGS_SPLAT_MUT;
2233 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2235 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2236 PUSH_INSN(ret, location, pop);
2237 PUSH_INSN(ret, location, pop);
2241 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2243 else if (flag & VM_CALL_KW_SPLAT) {
2245 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2246 PUSH_INSN(ret, location, swap);
2247 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2248 PUSH_INSN(ret, location, pop);
2250 PUSH_INSN(ret, location, swap);
2251 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2253 else if (keyword_len) {
2254 PUSH_INSN(ret, location, dup);
2255 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 2));
2256 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2257 PUSH_INSN(ret, location, pop);
2258 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2262 PUSH_INSN(ret, location, swap);
2264 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2267 PUSH_INSN(ret, location, pop);
2283pm_compile_index_control_flow_write_node(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_t *receiver,
const pm_arguments_node_t *arguments,
const pm_block_argument_node_t *block,
const pm_node_t *value,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
2286 if (!popped) PUSH_INSN(ret, location, putnil);
2287 PM_COMPILE_NOT_POPPED(receiver);
2289 int boff = (block == NULL ? 0 : 1);
2290 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2292 int argc = pm_setup_args(arguments, (
const pm_node_t *) block, &flag, &keywords, iseq, ret, scope_node, node_location);
2294 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2296 PUSH_INSN(ret, location, splatkw);
2299 PUSH_INSN(ret, location, dup);
2300 PUSH_INSN(ret, location, splatkw);
2301 PUSH_INSN(ret, location, pop);
2305 int dup_argn = argc + 1 + boff;
2306 int keyword_len = 0;
2309 keyword_len = keywords->keyword_len;
2310 dup_argn += keyword_len;
2313 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2314 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2316 LABEL *label = NEW_LABEL(location.
line);
2317 LABEL *lfin = NEW_LABEL(location.
line);
2319 PUSH_INSN(ret, location, dup);
2320 if (PM_NODE_TYPE_P(node, PM_INDEX_AND_WRITE_NODE)) {
2321 PUSH_INSNL(ret, location, branchunless, label);
2324 PUSH_INSNL(ret, location, branchif, label);
2327 PUSH_INSN(ret, location, pop);
2328 PM_COMPILE_NOT_POPPED(value);
2331 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2334 if (flag & VM_CALL_ARGS_SPLAT) {
2335 if (flag & VM_CALL_KW_SPLAT) {
2336 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2337 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2338 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2339 flag |= VM_CALL_ARGS_SPLAT_MUT;
2342 PUSH_INSN(ret, location, swap);
2343 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2344 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2345 PUSH_INSN(ret, location, pop);
2349 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2350 PUSH_INSN(ret, location, swap);
2351 PUSH_INSN(ret, location, pop);
2353 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2354 PUSH_INSN(ret, location, swap);
2355 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2356 PUSH_INSN(ret, location, swap);
2357 flag |= VM_CALL_ARGS_SPLAT_MUT;
2359 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2361 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2362 PUSH_INSN(ret, location, pop);
2363 PUSH_INSN(ret, location, pop);
2367 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2369 else if (flag & VM_CALL_KW_SPLAT) {
2371 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2372 PUSH_INSN(ret, location, swap);
2373 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2374 PUSH_INSN(ret, location, pop);
2377 PUSH_INSN(ret, location, swap);
2378 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2380 else if (keyword_len) {
2381 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2382 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 0));
2383 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2387 PUSH_INSN(ret, location, swap);
2389 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2392 PUSH_INSN(ret, location, pop);
2393 PUSH_INSNL(ret, location, jump, lfin);
2394 PUSH_LABEL(ret, label);
2396 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2398 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(dup_argn + 1));
2399 PUSH_LABEL(ret, lfin);
2408#define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
2409#define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
2410#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
2411#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
2412#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
2417static 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 use_deconstructed_cache,
unsigned int base_index);
2424pm_compile_pattern_generic_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node, LINK_ANCHOR *
const ret,
VALUE message,
unsigned int base_index)
2427 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2429 PUSH_INSN(ret, location, dup);
2430 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2432 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2433 PUSH_INSN1(ret, location, putobject, message);
2434 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2435 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2436 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2438 PUSH_INSN1(ret, location, putobject,
Qfalse);
2439 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2441 PUSH_INSN(ret, location, pop);
2442 PUSH_INSN(ret, location, pop);
2443 PUSH_LABEL(ret, match_succeeded_label);
2454pm_compile_pattern_length_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node, LINK_ANCHOR *
const ret,
VALUE message,
VALUE length,
unsigned int base_index)
2457 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2459 PUSH_INSN(ret, location, dup);
2460 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2462 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2463 PUSH_INSN1(ret, location, putobject, message);
2464 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2465 PUSH_INSN(ret, location, dup);
2466 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2467 PUSH_INSN1(ret, location, putobject, length);
2468 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(4));
2469 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2471 PUSH_INSN1(ret, location, putobject,
Qfalse);
2472 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2474 PUSH_INSN(ret, location, pop);
2475 PUSH_INSN(ret, location, pop);
2476 PUSH_LABEL(ret, match_succeeded_label);
2487pm_compile_pattern_eqq_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node, LINK_ANCHOR *
const ret,
unsigned int base_index)
2490 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2492 PUSH_INSN(ret, location, dup);
2493 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2494 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2496 VALUE operand = rb_fstring_lit(
"%p === %p does not return true");
2497 PUSH_INSN1(ret, location, putobject, operand);
2499 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2500 PUSH_INSN1(ret, location, topn,
INT2FIX(5));
2501 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2502 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2503 PUSH_INSN1(ret, location, putobject,
Qfalse);
2504 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2505 PUSH_INSN(ret, location, pop);
2506 PUSH_INSN(ret, location, pop);
2508 PUSH_LABEL(ret, match_succeeded_label);
2509 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2510 PUSH_INSN(ret, location, pop);
2511 PUSH_INSN(ret, location, pop);
2523pm_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 use_deconstructed_cache,
unsigned int base_index)
2525 LABEL *matched_label = NEW_LABEL(pm_node_line_number_cached(node, scope_node));
2526 CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index));
2527 PUSH_LABEL(ret, matched_label);
2537pm_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)
2541 if (use_deconstructed_cache) {
2542 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2543 PUSH_INSNL(ret, location, branchnil, deconstruct_label);
2545 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2546 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2548 PUSH_INSN(ret, location, pop);
2549 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
2550 PUSH_INSNL(ret, location, jump, deconstructed_label);
2553 PUSH_INSNL(ret, location, jump, deconstruct_label);
2556 PUSH_LABEL(ret, deconstruct_label);
2557 PUSH_INSN(ret, location, dup);
2560 PUSH_INSN1(ret, location, putobject, operand);
2561 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2563 if (use_deconstructed_cache) {
2564 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
2567 if (in_single_pattern) {
2568 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1));
2571 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2572 PUSH_SEND(ret, location, rb_intern(
"deconstruct"),
INT2FIX(0));
2574 if (use_deconstructed_cache) {
2575 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2578 PUSH_INSN(ret, location, dup);
2580 PUSH_INSNL(ret, location, branchunless, type_error_label);
2581 PUSH_LABEL(ret, deconstructed_label);
2591pm_compile_pattern_constant(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node, LINK_ANCHOR *
const ret, LABEL *match_failed_label,
bool in_single_pattern,
unsigned int base_index)
2595 PUSH_INSN(ret, location, dup);
2596 PM_COMPILE_NOT_POPPED(node);
2598 if (in_single_pattern) {
2599 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
2601 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2602 if (in_single_pattern) {
2603 CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
2605 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2614pm_compile_pattern_error_handler(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node, LINK_ANCHOR *
const ret, LABEL *done_label,
bool popped)
2617 LABEL *key_error_label = NEW_LABEL(location.
line);
2618 LABEL *cleanup_label = NEW_LABEL(location.
line);
2621 kw_arg->references = 0;
2622 kw_arg->keyword_len = 2;
2623 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
2624 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
2626 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2627 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2628 PUSH_INSNL(ret, location, branchif, key_error_label);
2631 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2634 VALUE operand = rb_fstring_lit(
"%p: %s");
2635 PUSH_INSN1(ret, location, putobject, operand);
2638 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2639 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2640 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2641 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2642 PUSH_INSNL(ret, location, jump, cleanup_label);
2644 PUSH_LABEL(ret, key_error_label);
2646 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2649 VALUE operand = rb_fstring_lit(
"%p: %s");
2650 PUSH_INSN1(ret, location, putobject, operand);
2653 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2654 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2655 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2656 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2657 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2658 PUSH_SEND_R(ret, location, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
2659 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(1));
2660 PUSH_LABEL(ret, cleanup_label);
2662 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(7));
2663 if (!popped) PUSH_INSN(ret, location, putnil);
2664 PUSH_INSNL(ret, location, jump, done_label);
2665 PUSH_INSN1(ret, location, dupn,
INT2FIX(5));
2666 if (popped) PUSH_INSN(ret, location, putnil);
2673pm_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 use_deconstructed_cache,
unsigned int base_index)
2677 switch (PM_NODE_TYPE(node)) {
2678 case PM_ARRAY_PATTERN_NODE: {
2687 const pm_array_pattern_node_t *
cast = (
const pm_array_pattern_node_t *) node;
2689 const uint32_t requireds_size =
cast->requireds.size;
2690 const uint32_t posts_size =
cast->posts.size;
2691 const uint32_t minimum_size = requireds_size + posts_size;
2693 bool rest_named =
false;
2694 bool use_rest_size =
false;
2696 if (
cast->rest != NULL) {
2697 rest_named = (PM_NODE_TYPE_P(
cast->rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *)
cast->rest)->expression != NULL);
2698 use_rest_size = (rest_named || (!rest_named && posts_size > 0));
2701 LABEL *match_failed_label = NEW_LABEL(location.
line);
2702 LABEL *type_error_label = NEW_LABEL(location.
line);
2703 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2704 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2706 if (use_rest_size) {
2707 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2708 PUSH_INSN(ret, location, swap);
2712 if (
cast->constant != NULL) {
2713 CHECK(pm_compile_pattern_constant(iseq, scope_node,
cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2716 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));
2718 PUSH_INSN(ret, location, dup);
2719 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2720 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2721 PUSH_SEND(ret, location,
cast->rest == NULL ? idEq : idGE,
INT2FIX(1));
2722 if (in_single_pattern) {
2723 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+)");
2724 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message,
INT2FIX(minimum_size), base_index + 1));
2726 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2728 for (uint32_t index = 0; index < requireds_size; index++) {
2729 const pm_node_t *required =
cast->requireds.nodes[index];
2730 PUSH_INSN(ret, location, dup);
2731 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2732 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2733 CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern,
false, base_index + 1));
2736 if (
cast->rest != NULL) {
2738 PUSH_INSN(ret, location, dup);
2739 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size));
2740 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2741 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2742 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2743 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2744 PUSH_INSN1(ret, location, setn,
INT2FIX(4));
2745 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2746 CHECK(pm_compile_pattern_match(iseq, scope_node, ((
const pm_splat_node_t *)
cast->rest)->expression, ret, match_failed_label, in_single_pattern,
false, base_index + 1));
2748 else if (posts_size > 0) {
2749 PUSH_INSN(ret, location, dup);
2750 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2751 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2752 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2753 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2754 PUSH_INSN(ret, location, pop);
2758 for (uint32_t index = 0; index < posts_size; index++) {
2759 const pm_node_t *post =
cast->posts.nodes[index];
2760 PUSH_INSN(ret, location, dup);
2762 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size + index));
2763 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2764 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2765 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2766 CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern,
false, base_index + 1));
2769 PUSH_INSN(ret, location, pop);
2770 if (use_rest_size) {
2771 PUSH_INSN(ret, location, pop);
2774 PUSH_INSNL(ret, location, jump, matched_label);
2775 PUSH_INSN(ret, location, putnil);
2776 if (use_rest_size) {
2777 PUSH_INSN(ret, location, putnil);
2780 PUSH_LABEL(ret, type_error_label);
2781 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2785 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2786 PUSH_INSN1(ret, location, putobject, operand);
2789 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2790 PUSH_INSN(ret, location, pop);
2792 PUSH_LABEL(ret, match_failed_label);
2793 PUSH_INSN(ret, location, pop);
2794 if (use_rest_size) {
2795 PUSH_INSN(ret, location, pop);
2798 PUSH_INSNL(ret, location, jump, unmatched_label);
2801 case PM_FIND_PATTERN_NODE: {
2810 const pm_find_pattern_node_t *
cast = (
const pm_find_pattern_node_t *) node;
2811 const uint32_t size =
cast->requireds.size;
2813 LABEL *match_failed_label = NEW_LABEL(location.
line);
2814 LABEL *type_error_label = NEW_LABEL(location.
line);
2815 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2816 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2818 if (
cast->constant) {
2819 CHECK(pm_compile_pattern_constant(iseq, scope_node,
cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2822 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));
2824 PUSH_INSN(ret, location, dup);
2825 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2826 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2827 PUSH_SEND(ret, location, idGE,
INT2FIX(1));
2828 if (in_single_pattern) {
2829 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));
2831 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2834 LABEL *while_begin_label = NEW_LABEL(location.
line);
2835 LABEL *next_loop_label = NEW_LABEL(location.
line);
2836 LABEL *find_succeeded_label = NEW_LABEL(location.
line);
2837 LABEL *find_failed_label = NEW_LABEL(location.
line);
2839 PUSH_INSN(ret, location, dup);
2840 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2842 PUSH_INSN(ret, location, dup);
2843 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2844 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2845 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2846 PUSH_LABEL(ret, while_begin_label);
2848 PUSH_INSN(ret, location, dup);
2849 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2850 PUSH_SEND(ret, location, idLE,
INT2FIX(1));
2851 PUSH_INSNL(ret, location, branchunless, find_failed_label);
2853 for (uint32_t index = 0; index < size; index++) {
2854 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2855 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2858 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2859 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2862 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2863 CHECK(pm_compile_pattern_match(iseq, scope_node,
cast->requireds.nodes[index], ret, next_loop_label, in_single_pattern,
false, base_index + 4));
2866 const pm_splat_node_t *left =
cast->left;
2869 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2870 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2871 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2872 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2873 CHECK(pm_compile_pattern_match(iseq, scope_node, left->
expression, ret, find_failed_label, in_single_pattern,
false, base_index + 4));
2876 const pm_splat_node_t *right =
cast->right;
2879 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2880 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2881 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2882 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2883 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2884 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2885 pm_compile_pattern_match(iseq, scope_node, right->
expression, ret, find_failed_label, in_single_pattern,
false, base_index + 4);
2888 PUSH_INSNL(ret, location, jump, find_succeeded_label);
2890 PUSH_LABEL(ret, next_loop_label);
2891 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
2892 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2893 PUSH_INSNL(ret, location, jump, while_begin_label);
2895 PUSH_LABEL(ret, find_failed_label);
2896 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2897 if (in_single_pattern) {
2898 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2901 VALUE operand = rb_fstring_lit(
"%p does not match to find pattern");
2902 PUSH_INSN1(ret, location, putobject, operand);
2905 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2906 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2907 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2909 PUSH_INSN1(ret, location, putobject,
Qfalse);
2910 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2912 PUSH_INSN(ret, location, pop);
2913 PUSH_INSN(ret, location, pop);
2915 PUSH_INSNL(ret, location, jump, match_failed_label);
2916 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2918 PUSH_LABEL(ret, find_succeeded_label);
2919 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2922 PUSH_INSN(ret, location, pop);
2923 PUSH_INSNL(ret, location, jump, matched_label);
2924 PUSH_INSN(ret, location, putnil);
2926 PUSH_LABEL(ret, type_error_label);
2927 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2931 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2932 PUSH_INSN1(ret, location, putobject, operand);
2935 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2936 PUSH_INSN(ret, location, pop);
2938 PUSH_LABEL(ret, match_failed_label);
2939 PUSH_INSN(ret, location, pop);
2940 PUSH_INSNL(ret, location, jump, unmatched_label);
2944 case PM_HASH_PATTERN_NODE: {
2953 const pm_hash_pattern_node_t *
cast = (
const pm_hash_pattern_node_t *) node;
2956 bool has_rest =
cast->rest != NULL && !(PM_NODE_TYPE_P(
cast->rest, PM_ASSOC_SPLAT_NODE) && ((
const pm_assoc_splat_node_t *)
cast->rest)->value == NULL);
2957 bool has_keys =
cast->elements.size > 0 ||
cast->rest != NULL;
2959 LABEL *match_failed_label = NEW_LABEL(location.
line);
2960 LABEL *type_error_label = NEW_LABEL(location.
line);
2963 if (has_keys && !has_rest) {
2966 for (uint32_t index = 0; index <
cast->elements.size; index++) {
2967 const pm_node_t *element =
cast->elements.nodes[index];
2968 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2970 const pm_node_t *key = ((
const pm_assoc_node_t *) element)->key;
2973 VALUE symbol =
ID2SYM(parse_string_symbol(scope_node, (
const pm_symbol_node_t *) key));
2978 if (
cast->constant) {
2979 CHECK(pm_compile_pattern_constant(iseq, scope_node,
cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2982 PUSH_INSN(ret, location, dup);
2985 VALUE operand =
ID2SYM(rb_intern(
"deconstruct_keys"));
2986 PUSH_INSN1(ret, location, putobject, operand);
2989 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2990 if (in_single_pattern) {
2991 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1));
2993 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2996 PUSH_INSN(ret, location, putnil);
3001 PUSH_INSN1(ret, location, duparray, keys);
3004 PUSH_SEND(ret, location, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
3006 PUSH_INSN(ret, location, dup);
3008 PUSH_INSNL(ret, location, branchunless, type_error_label);
3011 PUSH_SEND(ret, location, rb_intern(
"dup"),
INT2FIX(0));
3015 DECL_ANCHOR(match_values);
3017 for (uint32_t index = 0; index <
cast->elements.size; index++) {
3018 const pm_node_t *element =
cast->elements.nodes[index];
3019 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
3021 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
3022 const pm_node_t *key = assoc->
key;
3025 VALUE symbol =
ID2SYM(parse_string_symbol(scope_node, (
const pm_symbol_node_t *) key));
3026 PUSH_INSN(ret, location, dup);
3027 PUSH_INSN1(ret, location, putobject, symbol);
3028 PUSH_SEND(ret, location, rb_intern(
"key?"),
INT2FIX(1));
3030 if (in_single_pattern) {
3031 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
3033 PUSH_INSN(ret, location, dup);
3034 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
3039 PUSH_INSN1(ret, location, putobject, operand);
3042 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
3043 PUSH_INSN1(ret, location, putobject,
Qtrue);
3044 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
3045 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
3046 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
3047 PUSH_INSN1(ret, location, putobject, symbol);
3048 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
3050 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(4));
3051 PUSH_LABEL(ret, match_succeeded_label);
3054 PUSH_INSNL(ret, location, branchunless, match_failed_label);
3055 PUSH_INSN(match_values, location, dup);
3056 PUSH_INSN1(match_values, location, putobject, symbol);
3057 PUSH_SEND(match_values, location, has_rest ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
3059 const pm_node_t *value = assoc->
value;
3060 if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
3061 value = ((
const pm_implicit_node_t *) value)->value;
3064 CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern,
false, base_index + 1));
3067 PUSH_SEQ(ret, match_values);
3070 PUSH_INSN(ret, location, dup);
3071 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
3072 if (in_single_pattern) {
3073 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p is not empty"), base_index + 1));
3075 PUSH_INSNL(ret, location, branchunless, match_failed_label);
3079 switch (PM_NODE_TYPE(
cast->rest)) {
3080 case PM_NO_KEYWORDS_PARAMETER_NODE: {
3081 PUSH_INSN(ret, location, dup);
3082 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
3083 if (in_single_pattern) {
3084 pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1);
3086 PUSH_INSNL(ret, location, branchunless, match_failed_label);
3089 case PM_ASSOC_SPLAT_NODE: {
3090 const pm_assoc_splat_node_t *splat = (
const pm_assoc_splat_node_t *)
cast->rest;
3091 PUSH_INSN(ret, location, dup);
3092 pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern,
false, base_index + 1);
3096 rb_bug(
"unreachable");
3101 PUSH_INSN(ret, location, pop);
3102 PUSH_INSNL(ret, location, jump, matched_label);
3103 PUSH_INSN(ret, location, putnil);
3105 PUSH_LABEL(ret, type_error_label);
3106 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
3110 VALUE operand = rb_fstring_lit(
"deconstruct_keys must return Hash");
3111 PUSH_INSN1(ret, location, putobject, operand);
3114 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
3115 PUSH_INSN(ret, location, pop);
3117 PUSH_LABEL(ret, match_failed_label);
3118 PUSH_INSN(ret, location, pop);
3119 PUSH_INSNL(ret, location, jump, unmatched_label);
3122 case PM_CAPTURE_PATTERN_NODE: {
3132 const pm_capture_pattern_node_t *
cast = (
const pm_capture_pattern_node_t *) node;
3134 LABEL *match_failed_label = NEW_LABEL(location.
line);
3136 PUSH_INSN(ret, location, dup);
3137 CHECK(pm_compile_pattern_match(iseq, scope_node,
cast->value, ret, match_failed_label, in_single_pattern, use_deconstructed_cache, base_index + 1));
3138 CHECK(pm_compile_pattern(iseq, scope_node, (
const pm_node_t *)
cast->target, ret, matched_label, match_failed_label, in_single_pattern,
false, base_index));
3139 PUSH_INSN(ret, location, putnil);
3141 PUSH_LABEL(ret, match_failed_label);
3142 PUSH_INSN(ret, location, pop);
3143 PUSH_INSNL(ret, location, jump, unmatched_label);
3147 case PM_LOCAL_VARIABLE_TARGET_NODE: {
3151 const pm_local_variable_target_node_t *
cast = (
const pm_local_variable_target_node_t *) node;
3152 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node,
cast->name,
cast->depth);
3154 PUSH_SETLOCAL(ret, location, index.index, index.level);
3155 PUSH_INSNL(ret, location, jump, matched_label);
3158 case PM_ALTERNATION_PATTERN_NODE: {
3161 const pm_alternation_pattern_node_t *
cast = (
const pm_alternation_pattern_node_t *) node;
3163 LABEL *matched_left_label = NEW_LABEL(location.
line);
3164 LABEL *unmatched_left_label = NEW_LABEL(location.
line);
3168 PUSH_INSN(ret, location, dup);
3169 CHECK(pm_compile_pattern(iseq, scope_node,
cast->left, ret, matched_left_label, unmatched_left_label, in_single_pattern, use_deconstructed_cache, base_index + 1));
3174 PUSH_LABEL(ret, matched_left_label);
3175 PUSH_INSN(ret, location, pop);
3176 PUSH_INSNL(ret, location, jump, matched_label);
3177 PUSH_INSN(ret, location, putnil);
3181 PUSH_LABEL(ret, unmatched_left_label);
3182 CHECK(pm_compile_pattern(iseq, scope_node,
cast->right, ret, matched_label, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index));
3185 case PM_PARENTHESES_NODE:
3190 return pm_compile_pattern(iseq, scope_node, ((
const pm_parentheses_node_t *) node)->body, ret, matched_label, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index);
3191 case PM_PINNED_EXPRESSION_NODE:
3196 node = ((
const pm_pinned_expression_node_t *) node)->expression;
3199 case PM_CLASS_VARIABLE_READ_NODE:
3200 case PM_CONSTANT_PATH_NODE:
3201 case PM_CONSTANT_READ_NODE:
3204 case PM_GLOBAL_VARIABLE_READ_NODE:
3205 case PM_IMAGINARY_NODE:
3206 case PM_INSTANCE_VARIABLE_READ_NODE:
3207 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3208 case PM_INTEGER_NODE:
3209 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
3210 case PM_INTERPOLATED_STRING_NODE:
3211 case PM_INTERPOLATED_SYMBOL_NODE:
3212 case PM_INTERPOLATED_X_STRING_NODE:
3213 case PM_LAMBDA_NODE:
3214 case PM_LOCAL_VARIABLE_READ_NODE:
3216 case PM_SOURCE_ENCODING_NODE:
3217 case PM_SOURCE_FILE_NODE:
3218 case PM_SOURCE_LINE_NODE:
3220 case PM_RATIONAL_NODE:
3221 case PM_REGULAR_EXPRESSION_NODE:
3223 case PM_STRING_NODE:
3224 case PM_SYMBOL_NODE:
3226 case PM_X_STRING_NODE: {
3230 PM_COMPILE_NOT_POPPED(node);
3231 if (in_single_pattern) {
3232 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
3235 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
3237 if (in_single_pattern) {
3238 pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
3241 PUSH_INSNL(ret, location, branchif, matched_label);
3242 PUSH_INSNL(ret, location, jump, unmatched_label);
3245 case PM_PINNED_VARIABLE_NODE: {
3250 const pm_pinned_variable_node_t *
cast = (
const pm_pinned_variable_node_t *) node;
3251 CHECK(pm_compile_pattern(iseq, scope_node,
cast->variable, ret, matched_label, unmatched_label, in_single_pattern,
true, base_index));
3255 case PM_UNLESS_NODE: {
3267 const pm_node_t *predicate;
3268 const pm_node_t *statement;
3270 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3271 const pm_if_node_t *
cast = (
const pm_if_node_t *) node;
3272 predicate =
cast->predicate;
3275 statement =
cast->statements->body.nodes[0];
3278 const pm_unless_node_t *
cast = (
const pm_unless_node_t *) node;
3279 predicate =
cast->predicate;
3282 statement =
cast->statements->body.nodes[0];
3285 CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index));
3286 PM_COMPILE_NOT_POPPED(predicate);
3288 if (in_single_pattern) {
3289 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
3291 PUSH_INSN(ret, location, dup);
3292 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3293 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
3296 PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
3300 VALUE operand = rb_fstring_lit(
"guard clause does not return true");
3301 PUSH_INSN1(ret, location, putobject, operand);
3304 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
3305 PUSH_INSN1(ret, location, putobject,
Qfalse);
3306 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
3308 PUSH_INSN(ret, location, pop);
3309 PUSH_INSN(ret, location, pop);
3311 PUSH_LABEL(ret, match_succeeded_label);
3314 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3315 PUSH_INSNL(ret, location, branchunless, unmatched_label);
3318 PUSH_INSNL(ret, location, branchif, unmatched_label);
3321 PUSH_INSNL(ret, location, jump, matched_label);
3328 rb_bug(
"Unexpected node type in pattern matching expression: %s", pm_node_type(PM_NODE_TYPE(node)));
3335#undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
3336#undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
3337#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
3338#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
3339#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
3343pm_scope_node_init(
const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous)
3350 scope->parameters = NULL;
3352 scope->local_table_for_iseq_size = 0;
3357 memset(scope, 0,
sizeof(pm_scope_node_t));
3360 scope->base.
type = PM_SCOPE_NODE;
3363 scope->previous = previous;
3364 scope->ast_node = (pm_node_t *) node;
3366 switch (PM_NODE_TYPE(node)) {
3367 case PM_BLOCK_NODE: {
3368 const pm_block_node_t *
cast = (
const pm_block_node_t *) node;
3369 scope->body =
cast->body;
3370 scope->locals =
cast->locals;
3371 scope->parameters =
cast->parameters;
3374 case PM_CLASS_NODE: {
3375 const pm_class_node_t *
cast = (
const pm_class_node_t *) node;
3376 scope->body =
cast->body;
3377 scope->locals =
cast->locals;
3381 const pm_def_node_t *
cast = (
const pm_def_node_t *) node;
3382 scope->parameters = (pm_node_t *)
cast->parameters;
3383 scope->body =
cast->body;
3384 scope->locals =
cast->locals;
3387 case PM_ENSURE_NODE: {
3388 const pm_ensure_node_t *
cast = (
const pm_ensure_node_t *) node;
3389 scope->body = (pm_node_t *) node;
3391 if (
cast->statements != NULL) {
3399 const pm_for_node_t *
cast = (
const pm_for_node_t *) node;
3400 scope->body = (pm_node_t *)
cast->statements;
3403 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE:
3404 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
3406 scope->body = (pm_node_t *) node;
3409 case PM_LAMBDA_NODE: {
3410 const pm_lambda_node_t *
cast = (
const pm_lambda_node_t *) node;
3411 scope->parameters =
cast->parameters;
3412 scope->body =
cast->body;
3413 scope->locals =
cast->locals;
3416 case PM_MODULE_NODE: {
3417 const pm_module_node_t *
cast = (
const pm_module_node_t *) node;
3418 scope->body =
cast->body;
3419 scope->locals =
cast->locals;
3422 case PM_POST_EXECUTION_NODE: {
3423 const pm_post_execution_node_t *
cast = (
const pm_post_execution_node_t *) node;
3424 scope->body = (pm_node_t *)
cast->statements;
3427 case PM_PROGRAM_NODE: {
3428 const pm_program_node_t *
cast = (
const pm_program_node_t *) node;
3429 scope->body = (pm_node_t *)
cast->statements;
3430 scope->locals =
cast->locals;
3433 case PM_RESCUE_NODE: {
3434 const pm_rescue_node_t *
cast = (
const pm_rescue_node_t *) node;
3435 scope->body = (pm_node_t *)
cast->statements;
3438 case PM_RESCUE_MODIFIER_NODE: {
3439 const pm_rescue_modifier_node_t *
cast = (
const pm_rescue_modifier_node_t *) node;
3440 scope->body = (pm_node_t *)
cast->rescue_expression;
3443 case PM_SINGLETON_CLASS_NODE: {
3444 const pm_singleton_class_node_t *
cast = (
const pm_singleton_class_node_t *) node;
3445 scope->body =
cast->body;
3446 scope->locals =
cast->locals;
3449 case PM_STATEMENTS_NODE: {
3450 const pm_statements_node_t *
cast = (
const pm_statements_node_t *) node;
3451 scope->body = (pm_node_t *) cast;
3455 rb_bug(
"unreachable");
3461pm_scope_node_destroy(pm_scope_node_t *scope_node)
3480pm_compile_retry_end_label(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, LABEL *retry_end_l)
3483 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
3484 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
3485 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
3486 iobj = (INSN*) get_prev_insn(iobj);
3488 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
3493 if (&iobj->link == LAST_ELEMENT(ret)) {
3494 ret->last = (LINK_ELEMENT*) retry_end_l;
3499pm_iseq_builtin_function_name(
const pm_scope_node_t *scope_node,
const pm_node_t *receiver,
ID method_id)
3501 const char *name = rb_id2name(method_id);
3502 static const char prefix[] =
"__builtin_";
3503 const size_t prefix_len =
sizeof(prefix) - 1;
3505 if (receiver == NULL) {
3506 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
3508 return &name[prefix_len];
3511 else if (PM_NODE_TYPE_P(receiver, PM_CALL_NODE)) {
3512 if (PM_NODE_FLAG_P(receiver, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3513 const pm_call_node_t *
cast = (
const pm_call_node_t *) receiver;
3520 else if (PM_NODE_TYPE_P(receiver, PM_CONSTANT_READ_NODE)) {
3521 const pm_constant_read_node_t *
cast = (
const pm_constant_read_node_t *) receiver;
3533pm_compile_builtin_attr(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_arguments_node_t *arguments,
const pm_node_location_t *node_location)
3535 if (arguments == NULL) {
3536 COMPILE_ERROR(iseq, node_location->
line,
"attr!: no argument");
3540 const pm_node_t *argument;
3542 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3543 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to attr!: %s", pm_node_type(PM_NODE_TYPE(argument)));
3547 VALUE symbol = pm_static_literal_value(iseq, argument, scope_node);
3548 if (compile_builtin_attr_symbol(iseq, symbol) != COMPILE_OK) {
3549 COMPILE_ERROR(iseq, node_location->
line,
"unknown argument to attr!: %" PRIsVALUE, symbol);
3558pm_compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const pm_scope_node_t *scope_node,
const pm_arguments_node_t *arguments,
const pm_node_location_t *node_location,
int popped)
3560 if (arguments == NULL) {
3561 COMPILE_ERROR(iseq, node_location->
line,
"arg!: no argument");
3566 COMPILE_ERROR(iseq, node_location->
line,
"arg!: too many argument");
3571 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3572 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to arg!: %s", pm_node_type(PM_NODE_TYPE(argument)));
3577 ID name = parse_string_symbol(scope_node, ((
const pm_symbol_node_t *) argument));
3578 int index = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq, name);
3580 debugs(
"id: %s idx: %d\n", rb_id2name(name), index);
3581 PUSH_GETLOCAL(ret, *node_location, index, get_lvar_level(iseq));
3588pm_compile_builtin_mandatory_only_method(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_call_node_t *call_node,
const pm_node_location_t *node_location)
3590 const pm_node_t *ast_node = scope_node->ast_node;
3591 if (!PM_NODE_TYPE_P(ast_node, PM_DEF_NODE)) {
3592 rb_bug(
"mandatory_only?: not in method definition");
3596 const pm_def_node_t *def_node = (
const pm_def_node_t *) ast_node;
3597 const pm_parameters_node_t *parameters_node = def_node->
parameters;
3598 if (parameters_node == NULL) {
3599 rb_bug(
"mandatory_only?: in method definition with no parameters");
3603 const pm_node_t *body_node = def_node->
body;
3604 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)) {
3605 rb_bug(
"mandatory_only?: not in method definition with plain statements");
3609 const pm_if_node_t *if_node = (
const pm_if_node_t *) ((
const pm_statements_node_t *) body_node)->body.nodes[0];
3610 if (if_node->
predicate != ((
const pm_node_t *) call_node)) {
3611 rb_bug(
"mandatory_only?: can't find mandatory node");
3615 pm_parameters_node_t parameters = {
3616 .base = parameters_node->
base,
3620 const pm_def_node_t def = {
3621 .base = def_node->
base,
3622 .name = def_node->
name,
3624 .parameters = ¶meters,
3632 pm_scope_node_t next_scope_node;
3633 pm_scope_node_init(&def.base, &next_scope_node, scope_node);
3635 const rb_iseq_t *mandatory_only_iseq = pm_iseq_build(
3637 rb_iseq_base_label(iseq),
3639 rb_iseq_realpath(iseq),
3640 node_location->
line,
3644 ISEQ_COMPILE_DATA(iseq)->option
3646 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
3648 pm_scope_node_destroy(&next_scope_node);
3653pm_compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node,
const pm_call_node_t *call_node,
const pm_node_location_t *node_location,
int popped,
const rb_iseq_t *parent_block,
const char *builtin_func)
3655 const pm_arguments_node_t *arguments = call_node->
arguments;
3657 if (parent_block != NULL) {
3658 COMPILE_ERROR(iseq, node_location->
line,
"should not call builtins here.");
3662#define BUILTIN_INLINE_PREFIX "_bi"
3663 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
3664 bool cconst =
false;
3669 if (strcmp(
"cstmt!", builtin_func) == 0 || strcmp(
"cexpr!", builtin_func) == 0) {
3672 else if (strcmp(
"cconst!", builtin_func) == 0) {
3675 else if (strcmp(
"cinit!", builtin_func) == 0) {
3679 else if (strcmp(
"attr!", builtin_func) == 0) {
3680 return pm_compile_builtin_attr(iseq, scope_node, arguments, node_location);
3682 else if (strcmp(
"arg!", builtin_func) == 0) {
3683 return pm_compile_builtin_arg(iseq, ret, scope_node, arguments, node_location, popped);
3685 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
3687 rb_bug(
"mandatory_only? should be in if condition");
3689 else if (!LIST_INSN_SIZE_ZERO(ret)) {
3690 rb_bug(
"mandatory_only? should be put on top");
3693 PUSH_INSN1(ret, *node_location, putobject,
Qfalse);
3694 return pm_compile_builtin_mandatory_only_method(iseq, scope_node, call_node, node_location);
3696 else if (strcmp(
"local_self!", builtin_func) == 0) {
3699 pm_constant_id_t self_id = pm_parser_constant_find(scope_node->parser, (
const uint8_t *)
"self", 4);
3701 COMPILE_ERROR(iseq, node_location->
line,
"local_self! called but 'self' not found in local table");
3704 pm_local_index_t self_index = pm_lookup_local_index(iseq, scope_node, self_id, 0);
3705 PUSH_GETLOCAL(ret, *node_location, self_index.index, self_index.level);
3709 rb_bug(
"can't find builtin function:%s", builtin_func);
3712 COMPILE_ERROR(iseq, node_location->
line,
"can't find builtin function:%s", builtin_func);
3716 int inline_index = node_location->
line;
3717 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
3718 builtin_func = inline_func;
3724 typedef VALUE(*builtin_func0)(
void *,
VALUE);
3725 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
3726 PUSH_INSN1(ret, *node_location, putobject, const_val);
3732 DECL_ANCHOR(args_seq);
3736 int argc = pm_setup_args(arguments, call_node->
block, &flags, &keywords, iseq, args_seq, scope_node, node_location);
3738 if (argc != bf->argc) {
3739 COMPILE_ERROR(iseq, node_location->
line,
"argc is not match for builtin function:%s (expect %d but %d)", builtin_func, bf->argc, argc);
3743 unsigned int start_index;
3744 if (delegate_call_p(iseq, argc, args_seq, &start_index)) {
3745 PUSH_INSN2(ret, *node_location, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
3748 PUSH_SEQ(ret, args_seq);
3749 PUSH_INSN1(ret, *node_location, invokebuiltin, bf);
3752 if (popped) PUSH_INSN(ret, *node_location, pop);
3760pm_compile_call(rb_iseq_t *iseq,
const pm_call_node_t *call_node, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node,
ID method_id, LABEL *start)
3767 LABEL *else_label = NEW_LABEL(location.
line);
3768 LABEL *end_label = NEW_LABEL(location.
line);
3769 LABEL *retry_end_l = NEW_LABEL(location.
line);
3772 rb_code_location_t code_location = { 0 };
3773 int node_id = location.
node_id;
3775 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3776 if (PM_BRANCH_COVERAGE_P(iseq)) {
3777 uint32_t end_cursor = 0;
3778 bool end_found =
false;
3782 end_cursor = cursor;
3788 if (!end_found || cursor > end_cursor) {
3789 end_cursor = cursor;
3796 if (!end_found || cursor > end_cursor) {
3797 end_cursor = cursor;
3806 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(call_node);
3807 const pm_line_column_t end_location = pm_line_offset_list_line_column_cached(scope_node->line_offsets, end_cursor, scope_node->start_line, &scope_node->
last_line);
3809 code_location = (rb_code_location_t) {
3810 .beg_pos = { .lineno = start_location.
line, .column = start_location.
column },
3811 .end_pos = { .lineno = end_location.
line, .column = end_location.
column }
3814 branches = decl_branch_base(iseq, (
int) call_node->
base.
node_id, &code_location,
"&.");
3817 PUSH_INSN(ret, location, dup);
3818 PUSH_INSNL(ret, location, branchnil, else_label);
3820 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0,
"then", branches);
3823 LINK_ELEMENT *opt_new_prelude = LAST_ELEMENT(ret);
3828 int orig_argc = pm_setup_args(call_node->
arguments, call_node->
block, &flags, &kw_arg, iseq, ret, scope_node, &location);
3829 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
3830 const rb_iseq_t *block_iseq = NULL;
3832 if (call_node->
block != NULL && PM_NODE_TYPE_P(call_node->
block, PM_BLOCK_NODE)) {
3834 pm_scope_node_t next_scope_node;
3835 pm_scope_node_init(call_node->
block, &next_scope_node, scope_node);
3837 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, pm_node_line_number_cached(call_node->
block, scope_node));
3838 pm_scope_node_destroy(&next_scope_node);
3839 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
3842 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3843 flags |= VM_CALL_VCALL;
3847 flags |= VM_CALL_ARGS_SIMPLE;
3851 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
3852 flags |= VM_CALL_FCALL;
3855 if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
3856 if (flags & VM_CALL_ARGS_BLOCKARG) {
3857 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
3858 if (flags & VM_CALL_ARGS_SPLAT) {
3859 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3860 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3862 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 3));
3863 PUSH_INSN(ret, location, pop);
3865 else if (flags & VM_CALL_ARGS_SPLAT) {
3866 PUSH_INSN(ret, location, dup);
3867 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3868 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3869 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 2));
3870 PUSH_INSN(ret, location, pop);
3873 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 1));
3877 if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
3878 PUSH_INSN(ret, location, splatkw);
3881 LABEL *not_basic_new = NEW_LABEL(location.
line);
3882 LABEL *not_basic_new_finish = NEW_LABEL(location.
line);
3884 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
3885 method_id == rb_intern(
"new") &&
3886 call_node->
block == NULL &&
3887 (flags & VM_CALL_ARGS_BLOCKARG) == 0;
3890 if (LAST_ELEMENT(ret) == opt_new_prelude) {
3891 PUSH_INSN(ret, location, putnil);
3892 PUSH_INSN(ret, location, swap);
3895 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.
line, location.
node_id, BIN(swap), 0)->link);
3896 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.
line, location.
node_id, BIN(putnil), 0)->link);
3899 rb_callinfo_kwarg_retain(kw_arg);
3903 if (flags & VM_CALL_FORWARDING) {
3904 ci = (
VALUE)new_callinfo(iseq, method_id, orig_argc + 1, flags, kw_arg, 0);
3907 ci = (
VALUE)new_callinfo(iseq, method_id, orig_argc, flags, kw_arg, 0);
3910 PUSH_INSN2(ret, location, opt_new, ci, not_basic_new);
3911 LABEL_REF(not_basic_new);
3913 PUSH_SEND_R(ret, location, rb_intern(
"initialize"),
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags | VM_CALL_FCALL | VM_CALL_IGNORE_REFINEMENTS), kw_arg);
3914 PUSH_INSNL(ret, location, jump, not_basic_new_finish);
3916 PUSH_LABEL(ret, not_basic_new);
3918 PUSH_SEND_R(ret, location, method_id,
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags), kw_arg);
3919 PUSH_INSN(ret, location, swap);
3921 PUSH_LABEL(ret, not_basic_new_finish);
3922 PUSH_INSN(ret, location, pop);
3924 rb_callinfo_kwarg_release(kw_arg);
3927 PUSH_SEND_R(ret, location, method_id,
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags), kw_arg);
3930 if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
3931 pm_compile_retry_end_label(iseq, ret, retry_end_l);
3932 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, start, retry_end_l, block_iseq, retry_end_l);
3935 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3936 PUSH_INSNL(ret, location, jump, end_label);
3937 PUSH_LABEL(ret, else_label);
3938 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 1,
"else", branches);
3939 PUSH_LABEL(ret, end_label);
3942 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
3943 PUSH_INSN(ret, location, pop);
3946 if (popped) PUSH_INSN(ret, location, pop);
3947 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
3955pm_compile_back_reference_ref(
const pm_scope_node_t *scope_node,
const pm_back_reference_read_node_t *node)
3957 const char *
type = (
const char *) (pm_parser_start(scope_node->parser) + node->
base.
location.
start + 1);
3969pm_compile_numbered_reference_ref(
const pm_numbered_reference_read_node_t *node)
3975pm_compile_defined_expr0(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node,
bool in_condition, LABEL **lfinish,
bool explicit_receiver)
3977#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
3980 enum defined_type dtype = DEFINED_NOT_DEFINED;
3983 switch (PM_NODE_TYPE(node)) {
3988 dtype = DEFINED_NIL;
3991 case PM_INSTANCE_VARIABLE_READ_NODE: {
3994 const pm_instance_variable_read_node_t *
cast = (
const pm_instance_variable_read_node_t *) node;
3995 ID name = pm_constant_id_lookup(scope_node,
cast->name);
3997 PUSH_INSN3(ret, location, definedivar,
ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
4002 case PM_LOCAL_VARIABLE_READ_NODE:
4005 case PM_IT_LOCAL_VARIABLE_READ_NODE:
4008 dtype = DEFINED_LVAR;
4011 case PM_GLOBAL_VARIABLE_READ_NODE: {
4014 const pm_global_variable_read_node_t *
cast = (
const pm_global_variable_read_node_t *) node;
4015 ID name = pm_constant_id_lookup(scope_node,
cast->name);
4017 PUSH_INSN(ret, location, putnil);
4018 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR),
ID2SYM(name), PUSH_VAL(DEFINED_GVAR));
4023 case PM_CLASS_VARIABLE_READ_NODE: {
4026 const pm_class_variable_read_node_t *
cast = (
const pm_class_variable_read_node_t *) node;
4027 ID name = pm_constant_id_lookup(scope_node,
cast->name);
4029 PUSH_INSN(ret, location, putnil);
4030 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR),
ID2SYM(name), PUSH_VAL(DEFINED_CVAR));
4035 case PM_CONSTANT_READ_NODE: {
4038 const pm_constant_read_node_t *
cast = (
const pm_constant_read_node_t *) node;
4039 ID name = pm_constant_id_lookup(scope_node,
cast->name);
4041 PUSH_INSN(ret, location, putnil);
4042 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
4050 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
4052 PUSH_INSN(ret, location, putnil);
4053 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
4057 case PM_SUPER_NODE: {
4060 const pm_super_node_t *
cast = (
const pm_super_node_t *) node;
4062 if (
cast->block != NULL && !PM_NODE_TYPE_P(
cast->block, PM_BLOCK_ARGUMENT_NODE)) {
4063 dtype = DEFINED_EXPR;
4067 PUSH_INSN(ret, location, putnil);
4068 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
4071 case PM_FORWARDING_SUPER_NODE: {
4074 const pm_forwarding_super_node_t *
cast = (
const pm_forwarding_super_node_t *) node;
4076 if (
cast->block != NULL) {
4077 dtype = DEFINED_EXPR;
4081 PUSH_INSN(ret, location, putnil);
4082 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
4089 dtype = DEFINED_SELF;
4095 dtype = DEFINED_TRUE;
4101 dtype = DEFINED_FALSE;
4104 case PM_CALL_AND_WRITE_NODE:
4107 case PM_CALL_OPERATOR_WRITE_NODE:
4110 case PM_CALL_OR_WRITE_NODE:
4113 case PM_CLASS_VARIABLE_AND_WRITE_NODE:
4116 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
4119 case PM_CLASS_VARIABLE_OR_WRITE_NODE:
4122 case PM_CLASS_VARIABLE_WRITE_NODE:
4125 case PM_CONSTANT_AND_WRITE_NODE:
4128 case PM_CONSTANT_OPERATOR_WRITE_NODE:
4131 case PM_CONSTANT_OR_WRITE_NODE:
4134 case PM_CONSTANT_PATH_AND_WRITE_NODE:
4137 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
4140 case PM_CONSTANT_PATH_OR_WRITE_NODE:
4143 case PM_CONSTANT_PATH_WRITE_NODE:
4146 case PM_CONSTANT_WRITE_NODE:
4149 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
4152 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
4155 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
4158 case PM_GLOBAL_VARIABLE_WRITE_NODE:
4161 case PM_INDEX_AND_WRITE_NODE:
4164 case PM_INDEX_OPERATOR_WRITE_NODE:
4167 case PM_INDEX_OR_WRITE_NODE:
4170 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
4173 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
4176 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
4179 case PM_INSTANCE_VARIABLE_WRITE_NODE:
4182 case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
4185 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
4188 case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
4191 case PM_LOCAL_VARIABLE_WRITE_NODE:
4194 case PM_MULTI_WRITE_NODE:
4197 dtype = DEFINED_ASGN;
4200 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
4203 case PM_ALIAS_METHOD_NODE:
4212 case PM_CASE_MATCH_NODE:
4224 case PM_DEFINED_NODE:
4227 case PM_FLIP_FLOP_NODE:
4239 case PM_IMAGINARY_NODE:
4242 case PM_INTEGER_NODE:
4245 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE:
4248 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
4251 case PM_INTERPOLATED_STRING_NODE:
4254 case PM_INTERPOLATED_SYMBOL_NODE:
4257 case PM_INTERPOLATED_X_STRING_NODE:
4260 case PM_LAMBDA_NODE:
4263 case PM_MATCH_LAST_LINE_NODE:
4266 case PM_MATCH_PREDICATE_NODE:
4269 case PM_MATCH_REQUIRED_NODE:
4272 case PM_MATCH_WRITE_NODE:
4275 case PM_MODULE_NODE:
4284 case PM_POST_EXECUTION_NODE:
4290 case PM_RATIONAL_NODE:
4296 case PM_REGULAR_EXPRESSION_NODE:
4299 case PM_RESCUE_MODIFIER_NODE:
4305 case PM_RETURN_NODE:
4308 case PM_SINGLETON_CLASS_NODE:
4311 case PM_SOURCE_ENCODING_NODE:
4314 case PM_SOURCE_FILE_NODE:
4317 case PM_SOURCE_LINE_NODE:
4320 case PM_STRING_NODE:
4323 case PM_SYMBOL_NODE:
4329 case PM_UNLESS_NODE:
4338 case PM_X_STRING_NODE:
4341 dtype = DEFINED_EXPR;
4344 case PM_BACK_REFERENCE_READ_NODE: {
4347 const pm_back_reference_read_node_t *
cast = (
const pm_back_reference_read_node_t *) node;
4348 VALUE ref = pm_compile_back_reference_ref(scope_node, cast);
4350 PUSH_INSN(ret, location, putnil);
4351 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4355 case PM_NUMBERED_REFERENCE_READ_NODE: {
4358 const pm_numbered_reference_read_node_t *
cast = (
const pm_numbered_reference_read_node_t *) node;
4359 VALUE ref = pm_compile_numbered_reference_ref(cast);
4361 PUSH_INSN(ret, location, putnil);
4362 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4367 case PM_CONSTANT_PATH_NODE: {
4370 const pm_constant_path_node_t *
cast = (
const pm_constant_path_node_t *) node;
4371 ID name = pm_constant_id_lookup(scope_node,
cast->name);
4373 if (
cast->parent != NULL) {
4374 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4375 pm_compile_defined_expr0(iseq,
cast->parent, node_location, ret, popped, scope_node,
true, lfinish,
false);
4377 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4378 PM_COMPILE(
cast->parent);
4381 PUSH_INSN1(ret, location, putobject,
rb_cObject);
4384 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
4388 case PM_BEGIN_NODE: {
4391 const pm_begin_node_t *
cast = (
const pm_begin_node_t *) node;
4393 if (
cast->rescue_clause == NULL &&
cast->ensure_clause == NULL &&
cast->else_clause == NULL) {
4394 if (
cast->statements == NULL) {
4396 dtype = DEFINED_NIL;
4398 else if (
cast->statements->body.size == 1) {
4402 pm_compile_defined_expr0(iseq,
cast->statements->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4408 dtype = DEFINED_EXPR;
4413 dtype = DEFINED_EXPR;
4418 case PM_PARENTHESES_NODE: {
4421 const pm_parentheses_node_t *
cast = (
const pm_parentheses_node_t *) node;
4423 if (
cast->body == NULL) {
4425 dtype = DEFINED_NIL;
4427 else if (PM_NODE_TYPE_P(
cast->body, PM_STATEMENTS_NODE) && !PM_NODE_FLAG_P(cast, PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS)) {
4430 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);
4436 dtype = DEFINED_EXPR;
4441 case PM_ARRAY_NODE: {
4444 const pm_array_node_t *
cast = (
const pm_array_node_t *) node;
4446 if (
cast->elements.size > 0 && !lfinish[1]) {
4447 lfinish[1] = NEW_LABEL(location.
line);
4450 for (uint32_t index = 0; index <
cast->elements.size; index++) {
4451 pm_compile_defined_expr0(iseq,
cast->elements.nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4452 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4455 dtype = DEFINED_EXPR;
4461 case PM_KEYWORD_HASH_NODE: {
4464 const pm_node_list_t *elements;
4466 if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
4467 elements = &((
const pm_hash_node_t *) node)->elements;
4470 elements = &((
const pm_keyword_hash_node_t *) node)->elements;
4473 if (elements->
size > 0 && !lfinish[1]) {
4474 lfinish[1] = NEW_LABEL(location.
line);
4477 for (uint32_t index = 0; index < elements->
size; index++) {
4478 pm_compile_defined_expr0(iseq, elements->
nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4479 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4482 dtype = DEFINED_EXPR;
4485 case PM_ASSOC_NODE: {
4488 const pm_assoc_node_t *
cast = (
const pm_assoc_node_t *) node;
4490 pm_compile_defined_expr0(iseq,
cast->key, node_location, ret, popped, scope_node,
true, lfinish,
false);
4491 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4492 pm_compile_defined_expr0(iseq,
cast->value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4496 case PM_ASSOC_SPLAT_NODE: {
4499 const pm_assoc_splat_node_t *
cast = (
const pm_assoc_splat_node_t *) node;
4501 if (
cast->value == NULL) {
4502 dtype = DEFINED_EXPR;
4506 pm_compile_defined_expr0(iseq,
cast->value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4509 case PM_IMPLICIT_NODE: {
4512 const pm_implicit_node_t *
cast = (
const pm_implicit_node_t *) node;
4513 pm_compile_defined_expr0(iseq,
cast->value, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4516 case PM_CALL_NODE: {
4517#define BLOCK_P(cast) ((cast)->block != NULL && PM_NODE_TYPE_P((cast)->block, PM_BLOCK_NODE))
4521 const pm_call_node_t *
cast = ((
const pm_call_node_t *) node);
4523 if (BLOCK_P(cast)) {
4524 dtype = DEFINED_EXPR;
4528 if (
cast->receiver ||
cast->arguments || (
cast->block && PM_NODE_TYPE_P(
cast->block, PM_BLOCK_ARGUMENT_NODE))) {
4529 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4530 if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.
line);
4533 if (
cast->arguments) {
4534 pm_compile_defined_expr0(iseq, (
const pm_node_t *)
cast->arguments, node_location, ret, popped, scope_node,
true, lfinish,
false);
4535 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4538 if (
cast->block && PM_NODE_TYPE_P(
cast->block, PM_BLOCK_ARGUMENT_NODE)) {
4539 pm_compile_defined_expr0(iseq,
cast->block, node_location, ret, popped, scope_node,
true, lfinish,
false);
4540 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4543 if (
cast->receiver) {
4544 if (PM_NODE_TYPE_P(
cast->receiver, PM_CALL_NODE) && !BLOCK_P((
const pm_call_node_t *)
cast->receiver)) {
4547 pm_compile_defined_expr0(iseq,
cast->receiver, node_location, ret, popped, scope_node,
true, lfinish,
true);
4548 PUSH_INSNL(ret, location, branchunless, lfinish[2]);
4550 const pm_call_node_t *receiver = (
const pm_call_node_t *)
cast->receiver;
4551 ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
4553 pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
4556 pm_compile_defined_expr0(iseq,
cast->receiver, node_location, ret, popped, scope_node,
true, lfinish,
false);
4557 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4558 PM_COMPILE(
cast->receiver);
4561 ID method_id = pm_constant_id_lookup(scope_node,
cast->name);
4563 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4564 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_METHOD),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4567 ID method_id = pm_constant_id_lookup(scope_node,
cast->name);
4569 PUSH_INSN(ret, location, putself);
4570 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4572 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_FUNC),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4579 case PM_ARGUMENTS_NODE: {
4582 const pm_arguments_node_t *
cast = (
const pm_arguments_node_t *) node;
4584 for (uint32_t index = 0; index <
cast->arguments.size; index++) {
4585 pm_compile_defined_expr0(iseq,
cast->arguments.nodes[index], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4586 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4589 dtype = DEFINED_EXPR;
4592 case PM_BLOCK_ARGUMENT_NODE:
4595 dtype = DEFINED_EXPR;
4597 case PM_FORWARDING_ARGUMENTS_NODE:
4600 dtype = DEFINED_EXPR;
4602 case PM_SPLAT_NODE: {
4605 const pm_splat_node_t *
cast = (
const pm_splat_node_t *) node;
4607 if (
cast->expression == NULL) {
4608 dtype = DEFINED_EXPR;
4612 pm_compile_defined_expr0(iseq,
cast->expression, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4614 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4615 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4617 dtype = DEFINED_EXPR;
4620 case PM_SHAREABLE_CONSTANT_NODE:
4624 pm_compile_defined_expr0(iseq, ((
const pm_shareable_constant_node_t *) node)->write, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
4627 case PM_BLOCK_LOCAL_VARIABLE_NODE:
4628 case PM_BLOCK_PARAMETER_NODE:
4629 case PM_BLOCK_PARAMETERS_NODE:
4630 case PM_FORWARDING_PARAMETER_NODE:
4631 case PM_IMPLICIT_REST_NODE:
4632 case PM_IT_PARAMETERS_NODE:
4633 case PM_PARAMETERS_NODE:
4634 case PM_KEYWORD_REST_PARAMETER_NODE:
4635 case PM_NO_KEYWORDS_PARAMETER_NODE:
4636 case PM_NO_BLOCK_PARAMETER_NODE:
4637 case PM_NUMBERED_PARAMETERS_NODE:
4638 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE:
4639 case PM_OPTIONAL_PARAMETER_NODE:
4640 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
4641 case PM_REQUIRED_PARAMETER_NODE:
4642 case PM_REST_PARAMETER_NODE:
4644 case PM_ALTERNATION_PATTERN_NODE:
4645 case PM_ARRAY_PATTERN_NODE:
4646 case PM_CAPTURE_PATTERN_NODE:
4647 case PM_FIND_PATTERN_NODE:
4648 case PM_HASH_PATTERN_NODE:
4649 case PM_PINNED_EXPRESSION_NODE:
4650 case PM_PINNED_VARIABLE_NODE:
4652 case PM_CALL_TARGET_NODE:
4653 case PM_CLASS_VARIABLE_TARGET_NODE:
4654 case PM_CONSTANT_PATH_TARGET_NODE:
4655 case PM_CONSTANT_TARGET_NODE:
4656 case PM_GLOBAL_VARIABLE_TARGET_NODE:
4657 case PM_INDEX_TARGET_NODE:
4658 case PM_INSTANCE_VARIABLE_TARGET_NODE:
4659 case PM_LOCAL_VARIABLE_TARGET_NODE:
4660 case PM_MULTI_TARGET_NODE:
4663 case PM_ENSURE_NODE:
4665 case PM_RESCUE_NODE:
4669 case PM_EMBEDDED_STATEMENTS_NODE:
4670 case PM_EMBEDDED_VARIABLE_NODE:
4671 case PM_ERROR_RECOVERY_NODE:
4672 case PM_PRE_EXECUTION_NODE:
4673 case PM_PROGRAM_NODE:
4675 case PM_STATEMENTS_NODE:
4676 rb_bug(
"Unreachable node in defined?: %s", pm_node_type(PM_NODE_TYPE(node)));
4680 PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
4686pm_defined_expr(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node,
bool in_condition, LABEL **lfinish)
4688 LINK_ELEMENT *lcur = ret->last;
4689 pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4692 LABEL *lstart = NEW_LABEL(node_location->
line);
4693 LABEL *lend = NEW_LABEL(node_location->
line);
4696 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
4698 const rb_iseq_t *rescue = new_child_iseq_with_callback(
4707 lstart->rescued = LABEL_RESCUE_BEG;
4708 lend->rescued = LABEL_RESCUE_END;
4710 APPEND_LABEL(ret, lcur, lstart);
4711 PUSH_LABEL(ret, lend);
4712 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
4717pm_compile_defined_expr(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node,
bool in_condition)
4720 LINK_ELEMENT *last = ret->last;
4722 lfinish[0] = NEW_LABEL(node_location->
line);
4727 pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish);
4731 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, node_location->
line, node_location->
node_id, BIN(putnil), 0)->link);
4732 PUSH_INSN(ret, *node_location, swap);
4734 if (lfinish[2]) PUSH_LABEL(ret, lfinish[2]);
4735 PUSH_INSN(ret, *node_location, pop);
4736 PUSH_LABEL(ret, lfinish[1]);
4740 PUSH_LABEL(ret, lfinish[0]);
4746pm_add_ensure_iseq(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
int is_return, pm_scope_node_t *scope_node)
4751 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
4753 DECL_ANCHOR(ensure);
4756 if (enlp->erange != NULL) {
4757 DECL_ANCHOR(ensure_part);
4758 LABEL *lstart = NEW_LABEL(0);
4759 LABEL *lend = NEW_LABEL(0);
4761 add_ensure_range(iseq, enlp->erange, lstart, lend);
4763 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
4764 PUSH_LABEL(ensure_part, lstart);
4766 PM_COMPILE_INTO_ANCHOR(ensure_part, (
const pm_node_t *) enlp->ensure_node);
4767 PUSH_LABEL(ensure_part, lend);
4768 PUSH_SEQ(ensure, ensure_part);
4777 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
4778 PUSH_SEQ(ret, ensure);
4791 RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
4793 ID local = pm_constant_id_lookup(scope_node, constant_id);
4794 local_table_for_iseq->ids[local_index] = local;
4795 pm_index_lookup_table_insert(index_lookup_table, constant_id, local_index);
4804 local_table_for_iseq->ids[local_index] = local_name;
4805 pm_index_lookup_table_insert(index_lookup_table, special_id, local_index);
4815pm_compile_destructured_param_locals(
const pm_multi_target_node_t *node,
pm_index_lookup_table_t *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node,
int local_index)
4817 for (uint32_t index = 0; index < node->
lefts.
size; index++) {
4818 const pm_node_t *left = node->
lefts.
nodes[index];
4820 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4821 if (!PM_NODE_FLAG_P(left, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4822 pm_insert_local_index(((
const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4827 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4828 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);
4832 if (node->
rest != NULL && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE)) {
4833 const pm_splat_node_t *rest = (
const pm_splat_node_t *) node->
rest;
4835 if (rest->expression != NULL) {
4836 RUBY_ASSERT(PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE));
4838 if (!PM_NODE_FLAG_P(rest->expression, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4839 pm_insert_local_index(((
const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4845 for (uint32_t index = 0; index < node->
rights.
size; index++) {
4846 const pm_node_t *right = node->
rights.
nodes[index];
4848 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4849 if (!PM_NODE_FLAG_P(right, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4850 pm_insert_local_index(((
const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4855 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4856 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);
4868pm_compile_destructured_param_write(rb_iseq_t *iseq,
const pm_required_parameter_node_t *node, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node)
4871 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, node->
name, 0);
4872 PUSH_SETLOCAL(ret, location, index.index, index.level);
4884pm_compile_destructured_param_writes(rb_iseq_t *iseq,
const pm_multi_target_node_t *node, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node)
4887 bool has_rest = (node->
rest && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE) && (((
const pm_splat_node_t *) node->
rest)->expression) != NULL);
4890 int flag = (has_rest || has_rights) ? 1 : 0;
4893 for (uint32_t index = 0; index < node->
lefts.
size; index++) {
4894 const pm_node_t *left = node->
lefts.
nodes[index];
4896 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4897 pm_compile_destructured_param_write(iseq, (
const pm_required_parameter_node_t *) left, ret, scope_node);
4900 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4901 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) left, ret, scope_node);
4910 const pm_node_t *rest = ((
const pm_splat_node_t *) node->
rest)->expression;
4911 RUBY_ASSERT(PM_NODE_TYPE_P(rest, PM_REQUIRED_PARAMETER_NODE));
4913 pm_compile_destructured_param_write(iseq, (
const pm_required_parameter_node_t *) rest, ret, scope_node);
4921 for (uint32_t index = 0; index < node->
rights.
size; index++) {
4922 const pm_node_t *right = node->
rights.
nodes[index];
4924 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4925 pm_compile_destructured_param_write(iseq, (
const pm_required_parameter_node_t *) right, ret, scope_node);
4928 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4929 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) right, ret, scope_node);
4956} pm_multi_target_state_node_t;
4974 pm_multi_target_state_node_t *head;
4977 pm_multi_target_state_node_t *tail;
4986 pm_multi_target_state_node_t *node =
ALLOC(pm_multi_target_state_node_t);
4988 node->stack_index = state->stack_size + 1;
4989 node->stack_size = stack_size;
4990 node->position = state->position;
4993 if (state->head == NULL) {
4998 state->tail->next = node;
5002 state->stack_size += stack_size;
5015 if (state->stack_size == 0)
return;
5017 pm_multi_target_state_node_t *current = state->head;
5018 pm_multi_target_state_node_t *previous;
5020 while (current != NULL) {
5021 VALUE offset =
INT2FIX(state->stack_size - current->stack_index + current->position);
5022 current->topn->operands[0] = offset;
5028 if (current->stack_size > 1) {
5029 INSN *insn = current->topn;
5031 for (
size_t index = 1; index < current->stack_size; index += 1) {
5032 LINK_ELEMENT *element = get_next_insn(insn);
5035 insn = (INSN *) element;
5038 insn->operands[0] = offset;
5043 current = current->next;
5045 SIZED_FREE(previous);
5050pm_compile_multi_target_node(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const parents, LINK_ANCHOR *
const writes, LINK_ANCHOR *
const cleanup, pm_scope_node_t *scope_node,
pm_multi_target_state_t *state);
5081pm_compile_target_node(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const parents, LINK_ANCHOR *
const writes, LINK_ANCHOR *
const cleanup, pm_scope_node_t *scope_node,
pm_multi_target_state_t *state)
5085 switch (PM_NODE_TYPE(node)) {
5086 case PM_LOCAL_VARIABLE_TARGET_NODE: {
5092 const pm_local_variable_target_node_t *
cast = (
const pm_local_variable_target_node_t *) node;
5093 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node,
cast->name,
cast->depth);
5095 PUSH_SETLOCAL(writes, location, index.index, index.level);
5098 case PM_CLASS_VARIABLE_TARGET_NODE: {
5104 const pm_class_variable_target_node_t *
cast = (
const pm_class_variable_target_node_t *) node;
5105 ID name = pm_constant_id_lookup(scope_node,
cast->name);
5108 PUSH_INSN2(writes, location, setclassvariable, operand, get_cvar_ic_value(iseq, name));
5111 case PM_CONSTANT_TARGET_NODE: {
5117 const pm_constant_target_node_t *
cast = (
const pm_constant_target_node_t *) node;
5118 ID name = pm_constant_id_lookup(scope_node,
cast->name);
5121 PUSH_INSN1(writes, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5122 PUSH_INSN1(writes, location, setconstant, operand);
5125 case PM_GLOBAL_VARIABLE_TARGET_NODE: {
5131 const pm_global_variable_target_node_t *
cast = (
const pm_global_variable_target_node_t *) node;
5132 ID name = pm_constant_id_lookup(scope_node,
cast->name);
5135 PUSH_INSN1(writes, location, setglobal, operand);
5138 case PM_INSTANCE_VARIABLE_TARGET_NODE: {
5144 const pm_instance_variable_target_node_t *
cast = (
const pm_instance_variable_target_node_t *) node;
5145 ID name = pm_constant_id_lookup(scope_node,
cast->name);
5148 PUSH_INSN2(writes, location, setinstancevariable, operand, get_ivar_ic_value(iseq, name));
5151 case PM_CONSTANT_PATH_TARGET_NODE: {
5160 const pm_constant_path_target_node_t *
cast = (
const pm_constant_path_target_node_t *) node;
5161 ID name = pm_constant_id_lookup(scope_node,
cast->name);
5163 if (
cast->parent != NULL) {
5164 pm_compile_node(iseq,
cast->parent, parents,
false, scope_node);
5167 PUSH_INSN1(parents, location, putobject,
rb_cObject);
5170 if (state == NULL) {
5171 PUSH_INSN(writes, location, swap);
5174 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5175 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
5179 PUSH_INSN1(writes, location, setconstant, operand);
5181 if (state != NULL) {
5182 PUSH_INSN(cleanup, location, pop);
5187 case PM_CALL_TARGET_NODE: {
5196 const pm_call_target_node_t *
cast = (
const pm_call_target_node_t *) node;
5197 ID method_id = pm_constant_id_lookup(scope_node,
cast->name);
5199 pm_compile_node(iseq,
cast->receiver, parents,
false, scope_node);
5201 LABEL *safe_label = NULL;
5202 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
5203 safe_label = NEW_LABEL(location.
line);
5204 PUSH_INSN(parents, location, dup);
5205 PUSH_INSNL(parents, location, branchnil, safe_label);
5208 if (state != NULL) {
5209 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5210 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
5211 PUSH_INSN(writes, location, swap);
5214 int flags = VM_CALL_ARGS_SIMPLE;
5215 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
5217 PUSH_SEND_WITH_FLAG(writes, location, method_id,
INT2FIX(1),
INT2FIX(flags));
5218 if (safe_label != NULL && state == NULL) PUSH_LABEL(writes, safe_label);
5219 PUSH_INSN(writes, location, pop);
5220 if (safe_label != NULL && state != NULL) PUSH_LABEL(writes, safe_label);
5222 if (state != NULL) {
5223 PUSH_INSN(cleanup, location, pop);
5228 case PM_INDEX_TARGET_NODE: {
5238 const pm_index_target_node_t *
cast = (
const pm_index_target_node_t *) node;
5240 pm_compile_node(iseq,
cast->receiver, parents,
false, scope_node);
5244 int argc = pm_setup_args(
cast->arguments, (
const pm_node_t *)
cast->block, &flags, &kwargs, iseq, parents, scope_node, &location);
5246 if (state != NULL) {
5247 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5248 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), argc + 1);
5251 PUSH_INSN(writes, location, swap);
5254 for (
int index = 0; index < argc; index++) {
5255 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5257 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5265 int ci_argc = argc + 1;
5266 if (flags & VM_CALL_ARGS_SPLAT) {
5268 PUSH_INSN1(writes, location, newarray,
INT2FIX(1));
5269 PUSH_INSN(writes, location, concatarray);
5272 PUSH_SEND_R(writes, location, idASET,
INT2NUM(ci_argc), NULL,
INT2FIX(flags), kwargs);
5273 PUSH_INSN(writes, location, pop);
5275 if (state != NULL) {
5277 PUSH_INSN(writes, location, pop);
5280 for (
int index = 0; index < argc + 1; index++) {
5281 PUSH_INSN(cleanup, location, pop);
5287 case PM_MULTI_TARGET_NODE: {
5294 size_t before_position;
5295 if (state != NULL) {
5296 before_position = state->position;
5300 pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
5301 if (state != NULL) state->position = before_position;
5305 case PM_SPLAT_NODE: {
5311 const pm_splat_node_t *
cast = (
const pm_splat_node_t *) node;
5313 if (
cast->expression != NULL) {
5314 pm_compile_target_node(iseq,
cast->expression, parents, writes, cleanup, scope_node, state);
5320 rb_bug(
"Unexpected node type: %s", pm_node_type(PM_NODE_TYPE(node)));
5331pm_compile_multi_target_node(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const parents, LINK_ANCHOR *
const writes, LINK_ANCHOR *
const cleanup, pm_scope_node_t *scope_node,
pm_multi_target_state_t *state)
5334 const pm_node_list_t *lefts;
5335 const pm_node_t *rest;
5336 const pm_node_list_t *rights;
5338 switch (PM_NODE_TYPE(node)) {
5339 case PM_MULTI_TARGET_NODE: {
5340 const pm_multi_target_node_t *
cast = (
const pm_multi_target_node_t *) node;
5341 lefts = &
cast->lefts;
5343 rights = &
cast->rights;
5346 case PM_MULTI_WRITE_NODE: {
5347 const pm_multi_write_node_t *
cast = (
const pm_multi_write_node_t *) node;
5348 lefts = &
cast->lefts;
5350 rights = &
cast->rights;
5354 rb_bug(
"Unsupported node %s", pm_node_type(PM_NODE_TYPE(node)));
5358 bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *) rest)->expression != NULL;
5359 bool has_posts = rights->
size > 0;
5364 PUSH_INSN2(writes, location, expandarray,
INT2FIX(lefts->
size),
INT2FIX((has_rest || has_posts) ? 1 : 0));
5370 if (state == NULL) state = &target_state;
5372 size_t base_position = state->position;
5373 size_t splat_position = (has_rest || has_posts) ? 1 : 0;
5376 for (uint32_t index = 0; index < lefts->
size; index++) {
5377 const pm_node_t *target = lefts->
nodes[index];
5378 state->position = lefts->
size - index + splat_position + base_position;
5379 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5384 const pm_node_t *target = ((
const pm_splat_node_t *) rest)->expression;
5385 state->position = 1 + rights->
size + base_position;
5391 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5396 if (!has_rest && rest != NULL) {
5400 for (uint32_t index = 0; index < rights->
size; index++) {
5401 const pm_node_t *target = rights->
nodes[index];
5402 state->position = rights->
size - index + base_position;
5403 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5414pm_compile_for_node_index(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node)
5418 switch (PM_NODE_TYPE(node)) {
5419 case PM_LOCAL_VARIABLE_TARGET_NODE: {
5422 PUSH_GETLOCAL(ret, location, 1, 0);
5423 pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
5426 case PM_CLASS_VARIABLE_TARGET_NODE:
5427 case PM_CONSTANT_TARGET_NODE:
5428 case PM_GLOBAL_VARIABLE_TARGET_NODE:
5429 case PM_INSTANCE_VARIABLE_TARGET_NODE:
5430 case PM_CONSTANT_PATH_TARGET_NODE:
5431 case PM_CALL_TARGET_NODE:
5432 case PM_INDEX_TARGET_NODE: {
5436 DECL_ANCHOR(writes);
5437 DECL_ANCHOR(cleanup);
5441 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
5443 PUSH_GETLOCAL(ret, location, 1, 0);
5446 PUSH_SEQ(ret, writes);
5447 PUSH_SEQ(ret, cleanup);
5449 pm_multi_target_state_update(&state);
5453 case PM_MULTI_TARGET_NODE: {
5454 DECL_ANCHOR(writes);
5455 DECL_ANCHOR(cleanup);
5457 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
5459 LABEL *not_single = NEW_LABEL(location.
line);
5460 LABEL *not_ary = NEW_LABEL(location.
line);
5468 PUSH_GETLOCAL(ret, location, 1, 0);
5469 PUSH_INSN(ret, location, dup);
5470 PUSH_CALL(ret, location, idLength,
INT2FIX(0));
5471 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
5472 PUSH_CALL(ret, location, idEq,
INT2FIX(1));
5473 PUSH_INSNL(ret, location, branchunless, not_single);
5474 PUSH_INSN(ret, location, dup);
5475 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
5476 PUSH_CALL(ret, location, idAREF,
INT2FIX(1));
5477 PUSH_INSN1(ret, location, putobject,
rb_cArray);
5478 PUSH_INSN(ret, location, swap);
5479 PUSH_CALL(ret, location, rb_intern(
"try_convert"),
INT2FIX(1));
5480 PUSH_INSN(ret, location, dup);
5481 PUSH_INSNL(ret, location, branchunless, not_ary);
5482 PUSH_INSN(ret, location, swap);
5484 PUSH_LABEL(ret, not_ary);
5485 PUSH_INSN(ret, location, pop);
5487 PUSH_LABEL(ret, not_single);
5489 if (PM_NODE_TYPE_P(node, PM_SPLAT_NODE)) {
5490 const pm_splat_node_t *
cast = (
const pm_splat_node_t *) node;
5491 PUSH_INSN2(ret, location, expandarray,
INT2FIX(0),
INT2FIX(
cast->expression == NULL ? 0 : 1));
5494 PUSH_SEQ(ret, writes);
5495 PUSH_SEQ(ret, cleanup);
5499 rb_bug(
"Unexpected node type for index in for node: %s", pm_node_type(PM_NODE_TYPE(node)));
5505pm_compile_rescue(rb_iseq_t *iseq,
const pm_begin_node_t *cast,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
5507 LABEL *lstart = NEW_LABEL(node_location->
line);
5508 LABEL *lend = NEW_LABEL(node_location->
line);
5509 LABEL *lcont = NEW_LABEL(node_location->
line);
5511 pm_scope_node_t rescue_scope_node;
5512 pm_scope_node_init((
const pm_node_t *)
cast->rescue_clause, &rescue_scope_node, scope_node);
5514 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
5518 pm_node_line_number_cached((
const pm_node_t *)
cast->rescue_clause, scope_node)
5521 pm_scope_node_destroy(&rescue_scope_node);
5523 lstart->rescued = LABEL_RESCUE_BEG;
5524 lend->rescued = LABEL_RESCUE_END;
5525 PUSH_LABEL(ret, lstart);
5527 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
5528 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
5530 if (
cast->statements != NULL) {
5531 PM_COMPILE_NOT_POPPED((
const pm_node_t *)
cast->statements);
5535 PUSH_INSN(ret, location, putnil);
5538 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
5539 PUSH_LABEL(ret, lend);
5541 if (
cast->else_clause != NULL) {
5542 if (!popped) PUSH_INSN(ret, *node_location, pop);
5543 PM_COMPILE((
const pm_node_t *)
cast->else_clause);
5546 PUSH_INSN(ret, *node_location, nop);
5547 PUSH_LABEL(ret, lcont);
5549 if (popped) PUSH_INSN(ret, *node_location, pop);
5550 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
5551 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
5555pm_compile_ensure(rb_iseq_t *iseq,
const pm_begin_node_t *cast,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
5557 const pm_statements_node_t *statements =
cast->ensure_clause->statements;
5560 if (statements != NULL) {
5561 location = PM_NODE_START_LOCATION(statements);
5564 location = *node_location;
5567 LABEL *lstart = NEW_LABEL(location.
line);
5568 LABEL *lend = NEW_LABEL(location.
line);
5569 LABEL *lcont = NEW_LABEL(location.
line);
5576 if (statements != NULL) {
5577 pm_compile_node(iseq, (
const pm_node_t *) statements, ensr,
true, scope_node);
5580 LINK_ELEMENT *last = ensr->last;
5581 bool last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
5586 push_ensure_entry(iseq, &enl, &er, (
void *)
cast->ensure_clause);
5588 PUSH_LABEL(ret, lstart);
5589 if (
cast->rescue_clause != NULL) {
5590 pm_compile_rescue(iseq, cast, node_location, ret, popped | last_leave, scope_node);
5592 else if (
cast->statements != NULL) {
5593 pm_compile_node(iseq, (
const pm_node_t *)
cast->statements, ret, popped | last_leave, scope_node);
5595 else if (!(popped | last_leave)) {
5596 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
5599 PUSH_LABEL(ret, lend);
5600 PUSH_SEQ(ret, ensr);
5601 if (!popped && last_leave) PUSH_INSN(ret, *node_location, putnil);
5602 PUSH_LABEL(ret, lcont);
5603 if (last_leave) PUSH_INSN(ret, *node_location, pop);
5605 pm_scope_node_t next_scope_node;
5606 pm_scope_node_init((
const pm_node_t *)
cast->ensure_clause, &next_scope_node, scope_node);
5608 rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(
5615 pm_scope_node_destroy(&next_scope_node);
5617 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
5618 if (lstart->link.next != &lend->link) {
5620 PUSH_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, lcont);
5621 erange = erange->next;
5624 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
5632pm_opt_str_freeze_p(
const rb_iseq_t *iseq,
const pm_call_node_t *node)
5635 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5637 PM_NODE_TYPE_P(node->
receiver, PM_STRING_NODE) &&
5639 node->
block == NULL &&
5640 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5649pm_compile_constant_read(rb_iseq_t *iseq,
VALUE name,
const pm_location_t *name_loc, uint32_t node_id, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node)
5653 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
5654 ISEQ_BODY(iseq)->ic_size++;
5655 VALUE segments = rb_ary_new_from_args(1, name);
5657 PUSH_INSN1(ret, location, opt_getconstant_path, segments);
5660 PUSH_INSN(ret, location, putnil);
5661 PUSH_INSN1(ret, location, putobject,
Qtrue);
5662 PUSH_INSN1(ret, location, getconstant, name);
5671pm_constant_path_parts(
const pm_node_t *node,
const pm_scope_node_t *scope_node)
5676 switch (PM_NODE_TYPE(node)) {
5677 case PM_CONSTANT_READ_NODE: {
5678 const pm_constant_read_node_t *
cast = (
const pm_constant_read_node_t *) node;
5681 rb_ary_unshift(parts, name);
5684 case PM_CONSTANT_PATH_NODE: {
5685 const pm_constant_path_node_t *
cast = (
const pm_constant_path_node_t *) node;
5688 rb_ary_unshift(parts, name);
5689 if (
cast->parent == NULL) {
5690 rb_ary_unshift(parts,
ID2SYM(idNULL));
5694 node =
cast->parent;
5709pm_compile_constant_path(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const prefix, LINK_ANCHOR *
const body,
bool popped, pm_scope_node_t *scope_node)
5713 switch (PM_NODE_TYPE(node)) {
5714 case PM_CONSTANT_READ_NODE: {
5715 const pm_constant_read_node_t *
cast = (
const pm_constant_read_node_t *) node;
5718 PUSH_INSN1(body, location, putobject,
Qtrue);
5719 PUSH_INSN1(body, location, getconstant, name);
5722 case PM_CONSTANT_PATH_NODE: {
5723 const pm_constant_path_node_t *
cast = (
const pm_constant_path_node_t *) node;
5726 if (
cast->parent == NULL) {
5727 PUSH_INSN(body, location, pop);
5728 PUSH_INSN1(body, location, putobject,
rb_cObject);
5729 PUSH_INSN1(body, location, putobject,
Qtrue);
5730 PUSH_INSN1(body, location, getconstant, name);
5733 pm_compile_constant_path(iseq,
cast->parent, prefix, body,
false, scope_node);
5734 PUSH_INSN1(body, location, putobject,
Qfalse);
5735 PUSH_INSN1(body, location, getconstant, name);
5740 PM_COMPILE_INTO_ANCHOR(prefix, node);
5749pm_compile_shareable_constant_literal(rb_iseq_t *iseq,
const pm_node_t *node, pm_scope_node_t *scope_node)
5751 switch (PM_NODE_TYPE(node)) {
5755 case PM_SYMBOL_NODE:
5756 case PM_REGULAR_EXPRESSION_NODE:
5757 case PM_SOURCE_LINE_NODE:
5758 case PM_INTEGER_NODE:
5760 case PM_RATIONAL_NODE:
5761 case PM_IMAGINARY_NODE:
5762 case PM_SOURCE_ENCODING_NODE:
5763 return pm_static_literal_value(iseq, node, scope_node);
5764 case PM_STRING_NODE:
5765 return parse_static_literal_string(iseq, scope_node, node, &((
const pm_string_node_t *) node)->unescaped);
5766 case PM_ARRAY_NODE: {
5767 const pm_array_node_t *
cast = (
const pm_array_node_t *) node;
5770 for (uint32_t index = 0; index <
cast->elements.size; index++) {
5771 VALUE element = pm_compile_shareable_constant_literal(iseq,
cast->elements.nodes[index], scope_node);
5779 case PM_HASH_NODE: {
5780 const pm_hash_node_t *
cast = (
const pm_hash_node_t *) node;
5783 for (uint32_t index = 0; index <
cast->elements.size; index++) {
5784 const pm_node_t *element =
cast->elements.nodes[index];
5785 if (!PM_NODE_TYPE_P(element, PM_ASSOC_NODE))
return Qundef;
5787 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
5789 VALUE key = pm_compile_shareable_constant_literal(iseq, assoc->
key, scope_node);
5792 VALUE value = pm_compile_shareable_constant_literal(iseq, assoc->
value, scope_node);
5795 rb_hash_aset(result, key, value);
5810pm_compile_shareable_constant_value(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_flags_t shareability,
VALUE path, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node,
bool top)
5812 VALUE literal = pm_compile_shareable_constant_literal(iseq, node, scope_node);
5815 PUSH_INSN1(ret, location, putobject, literal);
5820 switch (PM_NODE_TYPE(node)) {
5821 case PM_ARRAY_NODE: {
5822 const pm_array_node_t *
cast = (
const pm_array_node_t *) node;
5825 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5828 for (uint32_t index = 0; index <
cast->elements.size; index++) {
5829 pm_compile_shareable_constant_value(iseq,
cast->elements.nodes[index], shareability, path, ret, scope_node,
false);
5832 PUSH_INSN1(ret, location, newarray,
INT2FIX(
cast->elements.size));
5835 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5836 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5841 case PM_HASH_NODE: {
5842 const pm_hash_node_t *
cast = (
const pm_hash_node_t *) node;
5845 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5848 pm_compile_hash_elements(iseq, (
const pm_node_t *) cast, &
cast->elements, shareability, path,
false, ret, scope_node);
5851 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5852 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5858 DECL_ANCHOR(value_seq);
5860 pm_compile_node(iseq, node, value_seq,
false, scope_node);
5861 if (PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) {
5862 PUSH_SEND_WITH_FLAG(value_seq, location, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5865 if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL) {
5866 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5867 PUSH_SEQ(ret, value_seq);
5871 PUSH_INSN1(ret, location, putobject, path);
5872 PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5874 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) {
5875 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5876 PUSH_SEQ(ret, value_seq);
5877 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5879 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING) {
5880 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5881 PUSH_SEQ(ret, value_seq);
5882 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5895pm_compile_constant_write_node(rb_iseq_t *iseq,
const pm_constant_write_node_t *node,
const pm_node_flags_t shareability,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
5898 ID name_id = pm_constant_id_lookup(scope_node, node->
name);
5900 if (shareability != 0) {
5901 pm_compile_shareable_constant_value(iseq, node->
value, shareability, rb_id2str(name_id), ret, scope_node,
true);
5904 PM_COMPILE_NOT_POPPED(node->
value);
5907 if (!popped) PUSH_INSN(ret, location, dup);
5908 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5911 PUSH_INSN1(ret, location, setconstant, operand);
5919pm_compile_constant_and_write_node(rb_iseq_t *iseq,
const pm_constant_and_write_node_t *node,
const pm_node_flags_t shareability,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
5924 LABEL *end_label = NEW_LABEL(location.
line);
5926 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5927 if (!popped) PUSH_INSN(ret, location, dup);
5929 PUSH_INSNL(ret, location, branchunless, end_label);
5930 if (!popped) PUSH_INSN(ret, location, pop);
5932 if (shareability != 0) {
5933 pm_compile_shareable_constant_value(iseq, node->
value, shareability,
rb_sym2str(name), ret, scope_node,
true);
5936 PM_COMPILE_NOT_POPPED(node->
value);
5939 if (!popped) PUSH_INSN(ret, location, dup);
5940 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5941 PUSH_INSN1(ret, location, setconstant, name);
5942 PUSH_LABEL(ret, end_label);
5950pm_compile_constant_or_write_node(rb_iseq_t *iseq,
const pm_constant_or_write_node_t *node,
const pm_node_flags_t shareability,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
5955 LABEL *set_label = NEW_LABEL(location.
line);
5956 LABEL *end_label = NEW_LABEL(location.
line);
5958 PUSH_INSN(ret, location, putnil);
5959 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST), name,
Qtrue);
5960 PUSH_INSNL(ret, location, branchunless, set_label);
5962 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5963 if (!popped) PUSH_INSN(ret, location, dup);
5965 PUSH_INSNL(ret, location, branchif, end_label);
5966 if (!popped) PUSH_INSN(ret, location, pop);
5967 PUSH_LABEL(ret, set_label);
5969 if (shareability != 0) {
5970 pm_compile_shareable_constant_value(iseq, node->
value, shareability,
rb_sym2str(name), ret, scope_node,
true);
5973 PM_COMPILE_NOT_POPPED(node->
value);
5976 if (!popped) PUSH_INSN(ret, location, dup);
5977 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5978 PUSH_INSN1(ret, location, setconstant, name);
5979 PUSH_LABEL(ret, end_label);
5987pm_compile_constant_operator_write_node(rb_iseq_t *iseq,
const pm_constant_operator_write_node_t *node,
const pm_node_flags_t shareability,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
5994 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5996 if (shareability != 0) {
5997 pm_compile_shareable_constant_value(iseq, node->
value, shareability,
rb_sym2str(name), ret, scope_node,
true);
6000 PM_COMPILE_NOT_POPPED(node->
value);
6003 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
6004 if (!popped) PUSH_INSN(ret, location, dup);
6006 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
6007 PUSH_INSN1(ret, location, setconstant, name);
6015pm_constant_path_path(
const pm_constant_path_node_t *node,
const pm_scope_node_t *scope_node)
6018 rb_ary_push(parts, rb_id2str(pm_constant_id_lookup(scope_node, node->
name)));
6020 const pm_node_t *current = node->
parent;
6021 while (current != NULL && PM_NODE_TYPE_P(current, PM_CONSTANT_PATH_NODE)) {
6022 const pm_constant_path_node_t *
cast = (
const pm_constant_path_node_t *) current;
6023 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node,
cast->name)));
6024 current =
cast->parent;
6027 if (current == NULL) {
6028 rb_ary_unshift(parts, rb_id2str(idNULL));
6030 else if (PM_NODE_TYPE_P(current, PM_CONSTANT_READ_NODE)) {
6031 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, ((
const pm_constant_read_node_t *) current)->name)));
6045pm_compile_constant_path_write_node(rb_iseq_t *iseq,
const pm_constant_path_write_node_t *node,
const pm_node_flags_t shareability,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
6048 const pm_constant_path_node_t *target = node->
target;
6052 PM_COMPILE_NOT_POPPED((
const pm_node_t *) target->
parent);
6055 PUSH_INSN1(ret, location, putobject,
rb_cObject);
6058 if (shareability != 0) {
6059 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6062 PM_COMPILE_NOT_POPPED(node->
value);
6066 PUSH_INSN(ret, location, swap);
6067 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6070 PUSH_INSN(ret, location, swap);
6071 PUSH_INSN1(ret, location, setconstant, name);
6079pm_compile_constant_path_and_write_node(rb_iseq_t *iseq,
const pm_constant_path_and_write_node_t *node,
const pm_node_flags_t shareability,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
6082 const pm_constant_path_node_t *target = node->
target;
6085 LABEL *lfin = NEW_LABEL(location.
line);
6088 PM_COMPILE_NOT_POPPED(target->
parent);
6091 PUSH_INSN1(ret, location, putobject,
rb_cObject);
6094 PUSH_INSN(ret, location, dup);
6095 PUSH_INSN1(ret, location, putobject,
Qtrue);
6096 PUSH_INSN1(ret, location, getconstant, name);
6098 if (!popped) PUSH_INSN(ret, location, dup);
6099 PUSH_INSNL(ret, location, branchunless, lfin);
6101 if (!popped) PUSH_INSN(ret, location, pop);
6103 if (shareability != 0) {
6104 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6107 PM_COMPILE_NOT_POPPED(node->
value);
6111 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6114 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
6115 PUSH_INSN(ret, location, swap);
6118 PUSH_INSN1(ret, location, setconstant, name);
6119 PUSH_LABEL(ret, lfin);
6121 if (!popped) PUSH_INSN(ret, location, swap);
6122 PUSH_INSN(ret, location, pop);
6130pm_compile_constant_path_or_write_node(rb_iseq_t *iseq,
const pm_constant_path_or_write_node_t *node,
const pm_node_flags_t shareability,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
6133 const pm_constant_path_node_t *target = node->
target;
6136 LABEL *lassign = NEW_LABEL(location.
line);
6137 LABEL *lfin = NEW_LABEL(location.
line);
6140 PM_COMPILE_NOT_POPPED(target->
parent);
6143 PUSH_INSN1(ret, location, putobject,
rb_cObject);
6146 PUSH_INSN(ret, location, dup);
6147 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM), name,
Qtrue);
6148 PUSH_INSNL(ret, location, branchunless, lassign);
6150 PUSH_INSN(ret, location, dup);
6151 PUSH_INSN1(ret, location, putobject,
Qtrue);
6152 PUSH_INSN1(ret, location, getconstant, name);
6154 if (!popped) PUSH_INSN(ret, location, dup);
6155 PUSH_INSNL(ret, location, branchif, lfin);
6157 if (!popped) PUSH_INSN(ret, location, pop);
6158 PUSH_LABEL(ret, lassign);
6160 if (shareability != 0) {
6161 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6164 PM_COMPILE_NOT_POPPED(node->
value);
6168 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6171 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
6172 PUSH_INSN(ret, location, swap);
6175 PUSH_INSN1(ret, location, setconstant, name);
6176 PUSH_LABEL(ret, lfin);
6178 if (!popped) PUSH_INSN(ret, location, swap);
6179 PUSH_INSN(ret, location, pop);
6187pm_compile_constant_path_operator_write_node(rb_iseq_t *iseq,
const pm_constant_path_operator_write_node_t *node,
const pm_node_flags_t shareability,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
6190 const pm_constant_path_node_t *target = node->
target;
6196 PM_COMPILE_NOT_POPPED(target->
parent);
6199 PUSH_INSN1(ret, location, putobject,
rb_cObject);
6202 PUSH_INSN(ret, location, dup);
6203 PUSH_INSN1(ret, location, putobject,
Qtrue);
6204 PUSH_INSN1(ret, location, getconstant, name);
6206 if (shareability != 0) {
6207 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6210 PM_COMPILE_NOT_POPPED(node->
value);
6213 PUSH_CALL(ret, location, method_id,
INT2FIX(1));
6214 PUSH_INSN(ret, location, swap);
6217 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6218 PUSH_INSN(ret, location, swap);
6221 PUSH_INSN1(ret, location, setconstant, name);
6230#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))
6237pm_compile_scope_node(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped)
6243 pm_parameters_node_t *parameters_node = NULL;
6244 pm_node_list_t *keywords_list = NULL;
6245 pm_node_list_t *optionals_list = NULL;
6246 pm_node_list_t *posts_list = NULL;
6247 pm_node_list_t *requireds_list = NULL;
6248 pm_node_list_t *block_locals = NULL;
6249 bool trailing_comma =
false;
6251 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
6255 if (scope_node->parameters != NULL) {
6256 switch (PM_NODE_TYPE(scope_node->parameters)) {
6257 case PM_BLOCK_PARAMETERS_NODE: {
6258 pm_block_parameters_node_t *
cast = (pm_block_parameters_node_t *) scope_node->parameters;
6259 parameters_node =
cast->parameters;
6260 block_locals = &
cast->locals;
6262 if (parameters_node) {
6263 if (parameters_node->
rest && PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE)) {
6264 trailing_comma =
true;
6269 case PM_PARAMETERS_NODE: {
6270 parameters_node = (pm_parameters_node_t *) scope_node->parameters;
6273 case PM_NUMBERED_PARAMETERS_NODE: {
6274 uint32_t maximum = ((
const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
6275 body->param.lead_num = maximum;
6276 body->param.flags.ambiguous_param0 = maximum == 1;
6279 case PM_IT_PARAMETERS_NODE:
6280 body->param.lead_num = 1;
6281 body->param.flags.ambiguous_param0 =
true;
6284 rb_bug(
"Unexpected node type for parameters: %s", pm_node_type(PM_NODE_TYPE(scope_node->parameters)));
6288 struct rb_iseq_param_keyword *keyword = NULL;
6290 if (parameters_node) {
6291 optionals_list = ¶meters_node->
optionals;
6292 requireds_list = ¶meters_node->
requireds;
6293 keywords_list = ¶meters_node->
keywords;
6294 posts_list = ¶meters_node->
posts;
6296 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))) {
6297 body->param.opt_num = 0;
6300 body->param.lead_num = 0;
6301 body->param.opt_num = 0;
6307 size_t locals_size = locals->
size;
6313 int table_size = (int) locals_size;
6317 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
6319 if (keywords_list && keywords_list->
size) {
6323 if (requireds_list) {
6324 for (uint32_t i = 0; i < requireds_list->
size; i++) {
6328 pm_node_t *required = requireds_list->
nodes[i];
6329 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6332 else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
6333 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6342 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6350 if (optionals_list && optionals_list->
size) {
6351 for (uint32_t i = 0; i < optionals_list->
size; i++) {
6352 pm_node_t * node = optionals_list->
nodes[i];
6353 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6363 if (parameters_node) {
6364 if (parameters_node->
rest) {
6365 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6366 if (!((
const pm_rest_parameter_node_t *) parameters_node->
rest)->name || PM_NODE_FLAG_P(parameters_node->
rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6379 if (PM_NODE_TYPE_P(parameters_node->
keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
6381 if (requireds_list->
size == 0 && optionals_list->
size == 0 && keywords_list->
size == 0) {
6382 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
6383 ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
6391 const pm_keyword_rest_parameter_node_t *kw_rest = (
const pm_keyword_rest_parameter_node_t *) parameters_node->
keyword_rest;
6394 if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6402 for (uint32_t i = 0; i < posts_list->
size; i++) {
6406 pm_node_t *required = posts_list->
nodes[i];
6407 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6413 if (keywords_list && keywords_list->
size) {
6414 for (uint32_t i = 0; i < keywords_list->
size; i++) {
6415 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6416 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6422 if (parameters_node && parameters_node->
block && PM_NODE_TYPE_P(parameters_node->
block, PM_BLOCK_PARAMETER_NODE)) {
6423 const pm_block_parameter_node_t *block_node = (
const pm_block_parameter_node_t *) parameters_node->
block;
6425 if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
6432 rb_ast_id_table_t *local_table_for_iseq =
ALLOCV(idtmp,
sizeof(rb_ast_id_table_t) + table_size *
sizeof(
ID));
6433 local_table_for_iseq->size = table_size;
6437 pm_index_lookup_table_init(&index_lookup_table, (
int) pm_parser_constants_size(scope_node->parser), iseq);
6457 int local_index = 0;
6464 if (requireds_list && requireds_list->
size) {
6465 for (uint32_t i = 0; i < requireds_list->
size; i++, local_index++) {
6471 pm_node_t *required = requireds_list->
nodes[i];
6473 switch (PM_NODE_TYPE(required)) {
6474 case PM_MULTI_TARGET_NODE: {
6477 local = rb_make_temporary_id(local_index);
6478 local_table_for_iseq->ids[local_index] = local;
6481 case PM_REQUIRED_PARAMETER_NODE: {
6484 const pm_required_parameter_node_t *param = (
const pm_required_parameter_node_t *) required;
6486 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6487 ID local = pm_constant_id_lookup(scope_node, param->
name);
6488 local_table_for_iseq->ids[local_index] = local;
6491 pm_insert_local_index(param->
name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6497 rb_bug(
"Unsupported node in requireds in parameters %s", pm_node_type(PM_NODE_TYPE(required)));
6501 body->param.lead_num = (int) requireds_list->
size;
6502 body->param.flags.has_lead =
true;
6505 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6506 local_table_for_iseq->ids[local_index++] = idItImplicit;
6511 if (optionals_list && optionals_list->
size) {
6512 body->param.opt_num = (int) optionals_list->
size;
6513 body->param.flags.has_opt =
true;
6515 for (uint32_t i = 0; i < optionals_list->
size; i++, local_index++) {
6516 pm_node_t * node = optionals_list->
nodes[i];
6517 pm_constant_id_t name = ((
const pm_optional_parameter_node_t *) node)->name;
6519 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6520 ID local = pm_constant_id_lookup(scope_node, name);
6521 local_table_for_iseq->ids[local_index] = local;
6524 pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6531 if (parameters_node && parameters_node->
rest) {
6532 body->param.rest_start = local_index;
6536 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6537 body->param.flags.has_rest =
true;
6545 if (PM_NODE_FLAG_P(parameters_node->
rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6546 ID local = pm_constant_id_lookup(scope_node, name);
6547 local_table_for_iseq->ids[local_index] = local;
6550 pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6556 body->param.flags.anon_rest =
true;
6557 pm_insert_local_special(PM_CONSTANT_MULT, idMULT, local_index, &index_lookup_table, local_table_for_iseq);
6566 if (posts_list && posts_list->
size) {
6567 body->param.post_num = (int) posts_list->
size;
6568 body->param.post_start = local_index;
6569 body->param.flags.has_post =
true;
6571 for (uint32_t i = 0; i < posts_list->
size; i++, local_index++) {
6577 const pm_node_t *post_node = posts_list->
nodes[i];
6579 switch (PM_NODE_TYPE(post_node)) {
6580 case PM_MULTI_TARGET_NODE: {
6583 local = rb_make_temporary_id(local_index);
6584 local_table_for_iseq->ids[local_index] = local;
6587 case PM_REQUIRED_PARAMETER_NODE: {
6590 const pm_required_parameter_node_t *param = (
const pm_required_parameter_node_t *) post_node;
6592 if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6593 ID local = pm_constant_id_lookup(scope_node, param->
name);
6594 local_table_for_iseq->ids[local_index] = local;
6597 pm_insert_local_index(param->
name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6602 rb_bug(
"Unsupported node in posts in parameters %s", pm_node_type(PM_NODE_TYPE(post_node)));
6610 if (keywords_list && keywords_list->
size) {
6611 if (keywords_list->
size > VM_CALL_KW_LEN_MAX) {
6612 COMPILE_ERROR(iseq, node_location->
line,
"too many keyword parameters (%d, maximum is %d)",
6613 (
int) keywords_list->
size, (
int) VM_CALL_KW_LEN_MAX);
6616 keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6617 keyword->num = (int) keywords_list->
size;
6622 for (uint32_t i = 0; i < keywords_list->
size; i++) {
6623 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6628 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
6629 name = ((
const pm_required_keyword_parameter_node_t *) keyword_parameter_node)->name;
6630 keyword->required_num++;
6631 ID local = pm_constant_id_lookup(scope_node, name);
6633 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6634 local_table_for_iseq->ids[local_index] = local;
6637 pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6643 for (uint32_t i = 0; i < keywords_list->
size; i++) {
6644 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6649 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
6650 const pm_optional_keyword_parameter_node_t *
cast = ((
const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6652 pm_node_t *value =
cast->value;
6655 if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !PM_CONTAINER_P(value)) {
6656 rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
6662 ID local = pm_constant_id_lookup(scope_node, name);
6663 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6664 local_table_for_iseq->ids[local_index] = local;
6667 pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6677 for (
int i = 0; i <
RARRAY_LEN(default_values); i++) {
6679 if (dv == complex_mark) dv =
Qundef;
6683 keyword->default_values = dvs;
6687 keyword->bits_start = local_index;
6688 ID local = rb_make_temporary_id(local_index);
6689 local_table_for_iseq->ids[local_index] = local;
6692 body->param.keyword = keyword;
6693 body->param.flags.has_kw =
true;
6696 if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->
size == 1 && !trailing_comma) {
6697 body->param.flags.ambiguous_param0 =
true;
6700 if (parameters_node) {
6705 case PM_NO_KEYWORDS_PARAMETER_NODE: {
6708 body->param.flags.accepts_no_kwarg =
true;
6711 case PM_KEYWORD_REST_PARAMETER_NODE: {
6714 const pm_keyword_rest_parameter_node_t *kw_rest_node = (
const pm_keyword_rest_parameter_node_t *) parameters_node->
keyword_rest;
6715 if (!body->param.flags.has_kw) {
6716 body->param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6719 keyword->rest_start = local_index;
6720 body->param.flags.has_kwrest =
true;
6724 if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6725 ID local = pm_constant_id_lookup(scope_node, constant_id);
6726 local_table_for_iseq->ids[local_index] = local;
6729 pm_insert_local_index(constant_id, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6733 body->param.flags.anon_kwrest =
true;
6734 pm_insert_local_special(PM_CONSTANT_POW, idPow, local_index, &index_lookup_table, local_table_for_iseq);
6740 case PM_FORWARDING_PARAMETER_NODE: {
6743 if (!ISEQ_BODY(iseq)->param.
flags.forwardable) {
6745 body->param.rest_start = local_index;
6746 body->param.flags.has_rest =
true;
6747 body->param.flags.anon_rest =
true;
6748 pm_insert_local_special(PM_CONSTANT_MULT, idMULT, local_index++, &index_lookup_table, local_table_for_iseq);
6752 body->param.flags.has_kw =
false;
6753 body->param.flags.has_kwrest =
true;
6754 body->param.flags.anon_kwrest =
true;
6755 body->param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6756 keyword->rest_start = local_index;
6757 pm_insert_local_special(PM_CONSTANT_POW, idPow, local_index++, &index_lookup_table, local_table_for_iseq);
6760 body->param.block_start = local_index;
6761 body->param.flags.has_block =
true;
6762 pm_insert_local_special(PM_CONSTANT_AND, idAnd, local_index++, &index_lookup_table, local_table_for_iseq);
6766 pm_insert_local_special(PM_CONSTANT_DOT3, idDot3, local_index++, &index_lookup_table, local_table_for_iseq);
6770 rb_bug(
"node type %s not expected as keyword_rest", pm_node_type(PM_NODE_TYPE(parameters_node->
keyword_rest)));
6776 if (parameters_node->
block) {
6777 switch (PM_NODE_TYPE(parameters_node->
block)) {
6778 case PM_BLOCK_PARAMETER_NODE: {
6779 body->param.block_start = local_index;
6780 body->param.flags.has_block =
true;
6782 iseq_set_use_block(iseq);
6787 if (PM_NODE_FLAG_P(parameters_node->
block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6788 ID local = pm_constant_id_lookup(scope_node, name);
6789 local_table_for_iseq->ids[local_index] = local;
6792 pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6796 pm_insert_local_special(PM_CONSTANT_AND, idAnd, local_index, &index_lookup_table, local_table_for_iseq);
6802 case PM_NO_BLOCK_PARAMETER_NODE: {
6803 body->param.flags.accepts_no_block =
true;
6807 rb_bug(
"node type %s not expected as block parameter", pm_node_type(PM_NODE_TYPE(parameters_node->
block)));
6824 if (requireds_list && requireds_list->
size) {
6825 for (uint32_t i = 0; i < requireds_list->
size; i++) {
6829 const pm_node_t *required = requireds_list->
nodes[i];
6831 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6832 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);
6838 if (posts_list && posts_list->
size) {
6839 for (uint32_t i = 0; i < posts_list->
size; i++) {
6843 const pm_node_t *post = posts_list->
nodes[i];
6845 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6846 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);
6852 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6853 if (PM_NODE_TYPE_P(((
const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
6854 body->param.lead_num++;
6857 body->param.rest_start = local_index;
6858 body->param.flags.has_rest =
true;
6861 ID local = rb_make_temporary_id(local_index);
6862 local_table_for_iseq->ids[local_index] = local;
6867 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
6868 int maximum = ((
const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
6870 for (
int i = 0; i < maximum; i++, local_index++) {
6871 const uint8_t param_name[] = {
'_',
'1' + i };
6872 pm_constant_id_t constant_id = pm_parser_constant_find(scope_node->parser, param_name, 2);
6873 RUBY_ASSERT(constant_id &&
"parser should fill in any gaps in numbered parameters");
6874 pm_insert_local_index(constant_id, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6876 body->param.lead_num = maximum;
6877 body->param.flags.has_lead =
true;
6881 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6882 body->param.lead_num = 1;
6883 body->param.flags.has_lead =
true;
6894 if (block_locals && block_locals->
size) {
6895 for (uint32_t i = 0; i < block_locals->
size; i++, local_index++) {
6896 pm_constant_id_t constant_id = ((
const pm_block_local_variable_node_t *) block_locals->
nodes[i])->name;
6897 pm_insert_local_index(constant_id, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
6902 if (scope_node->locals.
size) {
6903 for (
size_t i = 0; i < scope_node->locals.
size; i++) {
6907 if (!pm_index_lookup_table_lookup(&index_lookup_table, constant_id, &existing)) {
6908 ID local = pm_constant_id_lookup(scope_node, constant_id);
6909 local_table_for_iseq->ids[local_index] = local;
6910 pm_index_lookup_table_insert(&index_lookup_table, constant_id, local_index);
6922 iseq_calc_param_size(iseq);
6924 if (ISEQ_BODY(iseq)->param.
flags.forwardable) {
6927 ISEQ_BODY(iseq)->param.size += 1;
6931 iseq_set_local_table(iseq, local_table_for_iseq, 0);
6932 iseq_set_parameters_lvar_state(iseq);
6934 scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
6936 if (keyword != NULL) {
6937 size_t keyword_start_index = keyword->bits_start - keyword->num;
6938 keyword->table = (
ID *)&ISEQ_BODY(iseq)->local_table[keyword_start_index];
6941 local_table_for_iseq = NULL;
6946 if (optionals_list && optionals_list->
size) {
6954 for (uint32_t i = 0; i < optionals_list->
size; i++) {
6955 label = NEW_LABEL(location.
line);
6956 opt_table[i] = label;
6957 PUSH_LABEL(ret, label);
6958 pm_node_t *optional_node = optionals_list->
nodes[i];
6959 PM_COMPILE_NOT_POPPED(optional_node);
6963 label = NEW_LABEL(location.
line);
6964 opt_table[optionals_list->
size] = label;
6965 PUSH_LABEL(ret, label);
6967 body->param.opt_table = (
const VALUE *) opt_table;
6970 if (keywords_list && keywords_list->
size) {
6971 uint32_t optional_index = 0;
6972 for (uint32_t i = 0; i < keywords_list->
size; i++) {
6973 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6976 switch (PM_NODE_TYPE(keyword_parameter_node)) {
6977 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
6980 const pm_optional_keyword_parameter_node_t *
cast = ((
const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6982 pm_node_t *value =
cast->value;
6985 if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_CONTAINER_P(value)) {
6986 LABEL *end_label = NEW_LABEL(location.
line);
6988 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
6989 int kw_bits_idx = table_size - body->param.keyword->bits_start;
6990 PUSH_INSN2(ret, location, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(optional_index));
6991 PUSH_INSNL(ret, location, branchif, end_label);
6993 PUSH_SETLOCAL(ret, location, index.index, index.level);
6994 PUSH_LABEL(ret, end_label);
6999 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
7004 rb_bug(
"Unexpected keyword parameter node type %s", pm_node_type(PM_NODE_TYPE(keyword_parameter_node)));
7009 if (requireds_list && requireds_list->
size) {
7010 for (uint32_t i = 0; i < requireds_list->
size; i++) {
7014 const pm_node_t *required = requireds_list->
nodes[i];
7016 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
7017 PUSH_GETLOCAL(ret, location, table_size - (
int)i, 0);
7018 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) required, ret, scope_node);
7023 if (posts_list && posts_list->
size) {
7024 for (uint32_t i = 0; i < posts_list->
size; i++) {
7028 const pm_node_t *post = posts_list->
nodes[i];
7030 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
7031 PUSH_GETLOCAL(ret, location, table_size - body->param.post_start - (
int) i, 0);
7032 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) post, ret, scope_node);
7037 switch (body->type) {
7038 case ISEQ_TYPE_PLAIN: {
7039 const pm_node_t *node = scope_node->ast_node;
7040 const pm_node_list_t *parts;
7041 if (PM_NODE_TYPE_P(node, PM_INTERPOLATED_REGULAR_EXPRESSION_NODE)) {
7042 parts = &((
const pm_interpolated_regular_expression_node_t *) node)->parts;
7045 RUBY_ASSERT(PM_NODE_TYPE_P(node, PM_INTERPOLATED_MATCH_LAST_LINE_NODE));
7046 parts = &((
const pm_interpolated_match_last_line_node_t *) node)->parts;
7048 pm_compile_regexp_dynamic(iseq, node, parts, &location, ret, popped, scope_node);
7051 case ISEQ_TYPE_BLOCK: {
7052 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
7053 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
7054 const pm_node_location_t block_location = { .line = body->location.first_lineno, .node_id = scope_node->ast_node->
node_id };
7056 start->rescued = LABEL_RESCUE_BEG;
7057 end->rescued = LABEL_RESCUE_END;
7063 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
7064 pm_compile_for_node_index(iseq, ((
const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
7068 PUSH_INSN(ret, block_location, nop);
7069 PUSH_LABEL(ret, start);
7071 if (scope_node->body != NULL) {
7072 switch (PM_NODE_TYPE(scope_node->ast_node)) {
7073 case PM_POST_EXECUTION_NODE: {
7074 const pm_post_execution_node_t *
cast = (
const pm_post_execution_node_t *) scope_node->ast_node;
7075 PUSH_INSN1(ret, block_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7078 pm_scope_node_t next_scope_node;
7079 pm_scope_node_init((
const pm_node_t *)
cast->statements, &next_scope_node, scope_node);
7081 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.
line);
7082 pm_scope_node_destroy(&next_scope_node);
7084 PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe,
INT2FIX(0), block);
7087 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
7088 const pm_interpolated_regular_expression_node_t *
cast = (
const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
7089 pm_compile_regexp_dynamic(iseq, (
const pm_node_t *) cast, &
cast->parts, &location, ret, popped, scope_node);
7093 pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
7098 PUSH_INSN(ret, block_location, putnil);
7101 PUSH_LABEL(ret, end);
7103 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
7106 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
7107 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
7110 case ISEQ_TYPE_ENSURE: {
7111 const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->body) : location);
7112 iseq_set_exception_local_table(iseq);
7114 if (scope_node->body != NULL) {
7115 PM_COMPILE_POPPED((
const pm_node_t *) scope_node->body);
7118 PUSH_GETLOCAL(ret, statements_location, 1, 0);
7119 PUSH_INSN1(ret, statements_location,
throw,
INT2FIX(0));
7122 case ISEQ_TYPE_METHOD: {
7123 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
7126 if (scope_node->body) {
7127 PM_COMPILE((
const pm_node_t *) scope_node->body);
7130 PUSH_INSN(ret, location, putnil);
7133 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
7136 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
7139 case ISEQ_TYPE_RESCUE: {
7140 iseq_set_exception_local_table(iseq);
7141 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
7142 LABEL *lab = NEW_LABEL(location.
line);
7143 LABEL *rescue_end = NEW_LABEL(location.
line);
7144 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7146 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7147 PUSH_INSNL(ret, location, branchif, lab);
7148 PUSH_INSNL(ret, location, jump, rescue_end);
7149 PUSH_LABEL(ret, lab);
7151 PM_COMPILE((
const pm_node_t *) scope_node->body);
7152 PUSH_INSN(ret, location, leave);
7153 PUSH_LABEL(ret, rescue_end);
7154 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7157 PM_COMPILE((
const pm_node_t *) scope_node->ast_node);
7159 PUSH_INSN1(ret, location,
throw,
INT2FIX(0));
7164 if (scope_node->body) {
7165 PM_COMPILE((
const pm_node_t *) scope_node->body);
7168 PUSH_INSN(ret, location, putnil);
7173 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
7176 ISEQ_COMPILE_DATA(iseq)->last_line = end_location.
line;
7179 if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
7180 const pm_node_location_t location = { .line = ISEQ_COMPILE_DATA(iseq)->last_line, .node_id = scope_node->ast_node->
node_id };
7181 PUSH_INSN(ret, location, leave);
7186pm_compile_alias_global_variable_node(rb_iseq_t *iseq,
const pm_alias_global_variable_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7190 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7194 VALUE operand =
ID2SYM(rb_intern3((
const char *) (pm_parser_start(scope_node->parser) + name_loc->
start), name_loc->
length, scope_node->encoding));
7195 PUSH_INSN1(ret, *location, putobject, operand);
7200 VALUE operand =
ID2SYM(rb_intern3((
const char *) (pm_parser_start(scope_node->parser) + name_loc->
start), name_loc->
length, scope_node->encoding));
7201 PUSH_INSN1(ret, *location, putobject, operand);
7204 PUSH_SEND(ret, *location, id_core_set_variable_alias,
INT2FIX(2));
7205 if (popped) PUSH_INSN(ret, *location, pop);
7209pm_compile_alias_method_node(rb_iseq_t *iseq,
const pm_alias_method_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7211 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7212 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
7213 PM_COMPILE_NOT_POPPED(node->
new_name);
7214 PM_COMPILE_NOT_POPPED(node->
old_name);
7216 PUSH_SEND(ret, *location, id_core_set_method_alias,
INT2FIX(3));
7217 if (popped) PUSH_INSN(ret, *location, pop);
7235pm_compile_logical_chain(rb_iseq_t *iseq,
size_t size,
const pm_node_t **nodes,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7237 const pm_node_type_t
type = PM_NODE_TYPE(nodes[1]);
7238 LABEL *end_label = NEW_LABEL(location->
line);
7240 for (
size_t index = 0; index + 1 < size; index += 2) {
7241 PM_COMPILE_NOT_POPPED(nodes[index]);
7246 .line = location->
line,
7247 .node_id = nodes[index + 1]->
node_id
7250 if (!popped) PUSH_INSN(ret, operator_location, dup);
7251 if (
type == PM_AND_NODE)
7253 PUSH_INSNL(ret, operator_location, branchunless, end_label);
7256 PUSH_INSNL(ret, operator_location, branchif, end_label);
7258 if (!popped) PUSH_INSN(ret, operator_location, pop);
7261 PM_COMPILE(nodes[size - 1]);
7262 PUSH_LABEL(ret, end_label);
7266pm_compile_and_node(rb_iseq_t *iseq,
const pm_and_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7268 const pm_node_t *cursor = (
const pm_node_t *) node;
7271 while (PM_NODE_TYPE_P(cursor, PM_AND_NODE)) {
7272 cursor = ((
const pm_and_node_t *) cursor)->left;
7277 const pm_node_t **nodes =
ALLOCV_N(
const pm_node_t *, handle, size);
7279 cursor = (
const pm_node_t *) node;
7280 size_t index = size;
7282 while (PM_NODE_TYPE_P(cursor, PM_AND_NODE)) {
7283 const pm_and_node_t *
cast = (
const pm_and_node_t *) cursor;
7284 nodes[--index] =
cast->right;
7285 nodes[--index] = cursor;
7286 cursor =
cast->left;
7290 pm_compile_logical_chain(iseq, size, nodes, location, ret, popped, scope_node);
7295pm_compile_array_node(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_list_t *elements,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7299 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
7304 if (elements->
size) {
7305 VALUE value = pm_static_literal_value(iseq, node, scope_node);
7307 PUSH_INSN1(ret, *location, duparray, value);
7310 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7328 const int max_new_array_size = 0x100;
7329 const unsigned int min_tmp_array_size = 0x40;
7331 int new_array_size = 0;
7332 bool first_chunk =
true;
7338 bool static_literal =
false;
7341#define FLUSH_CHUNK \
7342 if (new_array_size) { \
7343 if (first_chunk) PUSH_INSN1(ret, *location, newarray, INT2FIX(new_array_size)); \
7344 else PUSH_INSN1(ret, *location, pushtoarray, INT2FIX(new_array_size)); \
7345 first_chunk = false; \
7346 new_array_size = 0; \
7349 for (uint32_t index = 0; index < elements->
size; index++) {
7350 const pm_node_t *element = elements->
nodes[index];
7352 if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
7355 const pm_splat_node_t *splat_element = (
const pm_splat_node_t *) element;
7357 PM_COMPILE_NOT_POPPED(splat_element->
expression);
7360 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
7361 PUSH_GETLOCAL(ret, *location, index.index, index.level);
7367 PUSH_INSN1(ret, *location, splatarray,
Qtrue);
7368 first_chunk =
false;
7371 PUSH_INSN(ret, *location, concattoarray);
7374 static_literal =
false;
7376 else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
7377 if (new_array_size == 0 && first_chunk) {
7378 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7379 first_chunk =
false;
7395 const pm_keyword_hash_node_t *keyword_hash = (
const pm_keyword_hash_node_t *) element;
7396 pm_compile_hash_elements(iseq, element, &keyword_hash->
elements, 0,
Qundef,
false, ret, scope_node);
7402 uint32_t splats = 0;
7403 while (splats < keyword_hash->elements.
size && PM_NODE_TYPE_P(keyword_hash->
elements.
nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
7406 PUSH_INSN(ret, *location, pushtoarraykwsplat);
7413 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) &&
7414 !PM_CONTAINER_P(element) &&
7416 ((index + min_tmp_array_size) < elements->
size)
7421 uint32_t right_index = index + 1;
7423 right_index < elements->size &&
7424 PM_NODE_FLAG_P(elements->
nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL) &&
7425 !PM_CONTAINER_P(elements->
nodes[right_index])
7428 uint32_t tmp_array_size = right_index - index;
7429 if (tmp_array_size >= min_tmp_array_size) {
7433 for (; tmp_array_size; tmp_array_size--)
7434 rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->
nodes[index++], scope_node));
7442 PUSH_INSN1(ret, *location, duparray, tmp_array);
7443 first_chunk =
false;
7446 PUSH_INSN1(ret, *location, putobject, tmp_array);
7447 PUSH_INSN(ret, *location, concattoarray);
7451 PM_COMPILE_NOT_POPPED(element);
7452 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7453 static_literal =
true;
7456 PM_COMPILE_NOT_POPPED(element);
7457 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7458 static_literal =
false;
7463 if (popped) PUSH_INSN(ret, *location, pop);
7469pm_compile_break_node(rb_iseq_t *iseq,
const pm_break_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7471 unsigned long throw_flag = 0;
7473 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7475 LABEL *splabel = NEW_LABEL(0);
7476 PUSH_LABEL(ret, splabel);
7477 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7480 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
7483 PUSH_INSN(ret, *location, putnil);
7486 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7487 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7488 PUSH_ADJUST_RESTORE(ret, splabel);
7489 if (!popped) PUSH_INSN(ret, *location, putnil);
7492 const rb_iseq_t *ip = iseq;
7495 if (!ISEQ_COMPILE_DATA(ip)) {
7500 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7501 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7503 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7506 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7507 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7511 ip = ISEQ_BODY(ip)->parent_iseq;
7517 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
7520 PUSH_INSN(ret, *location, putnil);
7523 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_BREAK));
7524 if (popped) PUSH_INSN(ret, *location, pop);
7529 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7534pm_compile_call_node(rb_iseq_t *iseq,
const pm_call_node_t *node, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7536 ID method_id = pm_constant_id_lookup(scope_node, node->
name);
7542 const char *builtin_func;
7544 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, node->
receiver, method_id)) != NULL) {
7545 pm_compile_builtin_function_call(iseq, ret, scope_node, node, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
7549 LABEL *start = NEW_LABEL(location.
line);
7550 if (node->
block) PUSH_LABEL(ret, start);
7552 switch (method_id) {
7554 if (pm_opt_str_freeze_p(iseq, node)) {
7555 VALUE value = parse_static_literal_string(iseq, scope_node, node->
receiver, &((
const pm_string_node_t * ) node->
receiver)->unescaped);
7556 const struct rb_callinfo *callinfo = new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE);
7557 PUSH_INSN2(ret, location, opt_str_uminus, value, callinfo);
7558 if (popped) PUSH_INSN(ret, location, pop);
7564 if (pm_opt_str_freeze_p(iseq, node)) {
7565 VALUE value = parse_static_literal_string(iseq, scope_node, node->
receiver, &((
const pm_string_node_t * ) node->
receiver)->unescaped);
7566 const struct rb_callinfo *callinfo = new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE);
7567 PUSH_INSN2(ret, location, opt_str_freeze, value, callinfo);
7568 if (popped) PUSH_INSN(ret, location, pop);
7575 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
7576 PUSH_INSN(ret, location, putnil);
7580 PUSH_INSN(ret, location, putself);
7583 if ((method_id == idCall || method_id == idAREF || method_id == idYield || method_id == idEqq) && PM_NODE_TYPE_P(node->
receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
7584 const pm_local_variable_read_node_t *read_node_cast = (
const pm_local_variable_read_node_t *) node->
receiver;
7588 if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
7589 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)));
7592 PM_COMPILE_NOT_POPPED(node->
receiver);
7596 PM_COMPILE_NOT_POPPED(node->
receiver);
7600 pm_compile_call(iseq, node, ret, popped, scope_node, method_id, start);
7605pm_compile_call_operator_write_node(rb_iseq_t *iseq,
const pm_call_operator_write_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7609 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
7610 flag = VM_CALL_FCALL;
7613 PM_COMPILE_NOT_POPPED(node->
receiver);
7615 LABEL *safe_label = NULL;
7616 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
7617 safe_label = NEW_LABEL(location->
line);
7618 PUSH_INSN(ret, *location, dup);
7619 PUSH_INSNL(ret, *location, branchnil, safe_label);
7622 PUSH_INSN(ret, *location, dup);
7624 ID id_read_name = pm_constant_id_lookup(scope_node, node->
read_name);
7625 PUSH_SEND_WITH_FLAG(ret, *location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
7627 PM_COMPILE_NOT_POPPED(node->
value);
7629 PUSH_SEND(ret, *location, id_operator,
INT2FIX(1));
7632 PUSH_INSN(ret, *location, swap);
7633 PUSH_INSN1(ret, *location, topn,
INT2FIX(1));
7636 ID id_write_name = pm_constant_id_lookup(scope_node, node->
write_name);
7637 PUSH_SEND_WITH_FLAG(ret, *location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
7639 if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
7640 PUSH_INSN(ret, *location, pop);
7641 if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
7661pm_compile_case_node_dispatch(rb_iseq_t *iseq,
VALUE dispatch,
const pm_node_t *node, LABEL *label, pm_scope_node_t *scope_node)
7664 switch (PM_NODE_TYPE(node)) {
7665 case PM_FLOAT_NODE: {
7666 key = pm_static_literal_value(iseq, node, scope_node);
7670 key = (
FIXABLE(intptr) ?
LONG2FIX((
long) intptr) : rb_dbl2big(intptr));
7676 case PM_INTEGER_NODE:
7678 case PM_SOURCE_LINE_NODE:
7679 case PM_SYMBOL_NODE:
7681 key = pm_static_literal_value(iseq, node, scope_node);
7683 case PM_STRING_NODE: {
7684 const pm_string_node_t *
cast = (
const pm_string_node_t *) node;
7685 key = parse_static_literal_string(iseq, scope_node, node, &
cast->unescaped);
7692 cdhash_aset_if_missing(dispatch, key, (
VALUE)label);
7700pm_compile_case_node(rb_iseq_t *iseq,
const pm_case_node_t *cast,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7703 const pm_node_list_t *conditions = &
cast->conditions;
7708 DECL_ANCHOR(cond_seq);
7713 DECL_ANCHOR(body_seq);
7717 LABEL *end_label = NEW_LABEL(location.
line);
7722 if (
cast->predicate == NULL) {
7725 rb_code_location_t case_location = { 0 };
7728 if (PM_BRANCH_COVERAGE_P(iseq)) {
7729 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7730 branches = decl_branch_base(iseq, (
int)
cast->base.node_id, &case_location,
"case");
7736 for (uint32_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7737 const pm_when_node_t *clause = (
const pm_when_node_t *) conditions->
nodes[clause_index];
7738 const pm_node_list_t *conditions = &clause->conditions;
7740 int clause_lineno = pm_node_line_number_cached((
const pm_node_t *) clause, scope_node);
7741 LABEL *label = NEW_LABEL(clause_lineno);
7742 PUSH_LABEL(body_seq, label);
7745 if (PM_BRANCH_COVERAGE_P(iseq)) {
7746 rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((
const pm_node_t *) clause->statements) : ((
const pm_node_t *) clause));
7747 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7750 if (clause->statements != NULL) {
7751 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7754 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7757 PUSH_INSNL(body_seq, location, jump, end_label);
7762 for (uint32_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7763 const pm_node_t *condition = conditions->
nodes[condition_index];
7765 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7767 PUSH_INSN(cond_seq, cond_location, putnil);
7768 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7769 PUSH_INSN1(cond_seq, cond_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7770 PUSH_INSNL(cond_seq, cond_location, branchif, label);
7773 LABEL *next_label = NEW_LABEL(pm_node_line_number_cached(condition, scope_node));
7774 pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label, scope_node);
7775 PUSH_LABEL(cond_seq, next_label);
7782 if (PM_BRANCH_COVERAGE_P(iseq)) {
7783 rb_code_location_t branch_location;
7785 if (
cast->else_clause == NULL) {
7786 branch_location = case_location;
7787 }
else if (
cast->else_clause->statements == NULL) {
7788 branch_location = pm_code_location(scope_node, (
const pm_node_t *)
cast->else_clause);
7790 branch_location = pm_code_location(scope_node, (
const pm_node_t *)
cast->else_clause->statements);
7793 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7797 if (
cast->else_clause != NULL) {
7798 pm_compile_node(iseq, (
const pm_node_t *)
cast->else_clause, cond_seq, popped, scope_node);
7801 PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
7806 PUSH_INSNL(cond_seq, location, jump, end_label);
7807 PUSH_SEQ(ret, cond_seq);
7812 rb_code_location_t case_location = { 0 };
7815 if (PM_BRANCH_COVERAGE_P(iseq)) {
7816 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7817 branches = decl_branch_base(iseq, (
int)
cast->base.node_id, &case_location,
"case");
7822 LABEL *else_label = NEW_LABEL(location.
line);
7830 if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7831 dispatch = cdhash_new(0);
7842 for (uint32_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7843 const pm_when_node_t *clause = (
const pm_when_node_t *) conditions->
nodes[clause_index];
7844 pm_node_location_t clause_location = PM_NODE_START_LOCATION((
const pm_node_t *) clause);
7846 const pm_node_list_t *conditions = &clause->conditions;
7847 LABEL *label = NEW_LABEL(clause_location.
line);
7852 for (uint32_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7853 const pm_node_t *condition = conditions->
nodes[condition_index];
7859 if (dispatch !=
Qundef) {
7860 dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
7863 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7864 PUSH_INSN(cond_seq, condition_location, dup);
7865 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7866 PUSH_INSN1(cond_seq, condition_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
7869 if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
7870 const pm_string_node_t *
string = (
const pm_string_node_t *) condition;
7871 VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->
unescaped);
7872 PUSH_INSN1(cond_seq, condition_location, putobject, value);
7875 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7878 PUSH_INSN1(cond_seq, condition_location, topn,
INT2FIX(1));
7879 PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
7882 PUSH_INSNL(cond_seq, condition_location, branchif, label);
7889 PUSH_LABEL(body_seq, label);
7890 PUSH_INSN(body_seq, clause_location, pop);
7893 if (PM_BRANCH_COVERAGE_P(iseq)) {
7894 rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((
const pm_node_t *) clause->statements) : ((
const pm_node_t *) clause));
7895 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7898 if (clause->statements != NULL) {
7899 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7902 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7905 PUSH_INSNL(body_seq, clause_location, jump, end_label);
7912 PM_COMPILE_NOT_POPPED(
cast->predicate);
7916 if (dispatch !=
Qundef) {
7917 PUSH_INSN(ret, location, dup);
7919 PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
7920 LABEL_REF(else_label);
7923 PUSH_SEQ(ret, cond_seq);
7927 PUSH_LABEL(ret, else_label);
7929 if (
cast->else_clause != NULL) {
7930 pm_node_location_t else_location = PM_NODE_START_LOCATION(
cast->else_clause->statements != NULL ? ((
const pm_node_t *)
cast->else_clause->statements) : ((
const pm_node_t *)
cast->else_clause));
7931 PUSH_INSN(ret, else_location, pop);
7934 if (PM_BRANCH_COVERAGE_P(iseq)) {
7935 rb_code_location_t branch_location = pm_code_location(scope_node,
cast->else_clause->statements != NULL ? ((
const pm_node_t *)
cast->else_clause->statements) : ((
const pm_node_t *)
cast->else_clause));
7936 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7939 PM_COMPILE((
const pm_node_t *)
cast->else_clause);
7940 PUSH_INSNL(ret, else_location, jump, end_label);
7943 PUSH_INSN(ret, location, pop);
7946 if (PM_BRANCH_COVERAGE_P(iseq)) {
7947 add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
7950 if (!popped) PUSH_INSN(ret, location, putnil);
7951 PUSH_INSNL(ret, location, jump, end_label);
7955 PUSH_SEQ(ret, body_seq);
7956 PUSH_LABEL(ret, end_label);
7960pm_compile_case_match_node(rb_iseq_t *iseq,
const pm_case_match_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
7965 DECL_ANCHOR(body_seq);
7970 DECL_ANCHOR(cond_seq);
7974 LABEL *end_label = NEW_LABEL(location->
line);
7979 LABEL *else_label = NEW_LABEL(location->
line);
7983 rb_code_location_t case_location = { 0 };
7987 if (PM_BRANCH_COVERAGE_P(iseq)) {
7988 case_location = pm_code_location(scope_node, (
const pm_node_t *) node);
7989 branches = decl_branch_base(iseq, (
int) node->
base.
node_id, &case_location,
"case");
7999 if (in_single_pattern) {
8000 PUSH_INSN(ret, *location, putnil);
8001 PUSH_INSN(ret, *location, putnil);
8002 PUSH_INSN1(ret, *location, putobject,
Qfalse);
8003 PUSH_INSN(ret, *location, putnil);
8007 PUSH_INSN(ret, *location, putnil);
8014 for (uint32_t index = 0; index < node->
conditions.
size; index++) {
8016 RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
8018 const pm_in_node_t *in_node = (
const pm_in_node_t *) condition;
8023 PUSH_INSN(body_seq, in_location, putnil);
8026 LABEL *body_label = NEW_LABEL(in_location.
line);
8027 PUSH_LABEL(body_seq, body_label);
8028 PUSH_INSN1(body_seq, in_location, adjuststack,
INT2FIX(in_single_pattern ? 6 : 2));
8031 if (PM_BRANCH_COVERAGE_P(iseq)) {
8032 rb_code_location_t branch_location = pm_code_location(scope_node, in_node->
statements != NULL ? ((
const pm_node_t *) in_node->
statements) : ((
const pm_node_t *) in_node));
8033 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"in", branches);
8037 PM_COMPILE_INTO_ANCHOR(body_seq, (
const pm_node_t *) in_node->
statements);
8040 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
8043 PUSH_INSNL(body_seq, in_location, jump, end_label);
8044 LABEL *next_pattern_label = NEW_LABEL(pattern_location.
line);
8046 PUSH_INSN(cond_seq, pattern_location, dup);
8047 pm_compile_pattern(iseq, scope_node, in_node->
pattern, cond_seq, body_label, next_pattern_label, in_single_pattern,
true, 2);
8048 PUSH_LABEL(cond_seq, next_pattern_label);
8049 LABEL_UNREMOVABLE(next_pattern_label);
8056 const pm_else_node_t *else_node = node->
else_clause;
8058 PUSH_LABEL(cond_seq, else_label);
8059 PUSH_INSN(cond_seq, *location, pop);
8060 PUSH_INSN(cond_seq, *location, pop);
8063 if (PM_BRANCH_COVERAGE_P(iseq)) {
8064 rb_code_location_t branch_location = pm_code_location(scope_node, else_node->
statements != NULL ? ((
const pm_node_t *) else_node->
statements) : ((
const pm_node_t *) else_node));
8065 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
8068 PM_COMPILE_INTO_ANCHOR(cond_seq, (
const pm_node_t *) else_node);
8069 PUSH_INSNL(cond_seq, *location, jump, end_label);
8070 PUSH_INSN(cond_seq, *location, putnil);
8071 if (popped) PUSH_INSN(cond_seq, *location, putnil);
8076 PUSH_LABEL(cond_seq, else_label);
8079 add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
8081 if (in_single_pattern) {
8082 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, cond_seq, end_label, popped);
8085 PUSH_INSN1(cond_seq, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8087 PUSH_INSN1(cond_seq, *location, topn,
INT2FIX(2));
8088 PUSH_SEND(cond_seq, *location, id_core_raise,
INT2FIX(2));
8090 PUSH_INSN1(cond_seq, *location, adjuststack,
INT2FIX(3));
8091 if (!popped) PUSH_INSN(cond_seq, *location, putnil);
8092 PUSH_INSNL(cond_seq, *location, jump, end_label);
8093 PUSH_INSN1(cond_seq, *location, dupn,
INT2FIX(1));
8094 if (popped) PUSH_INSN(cond_seq, *location, putnil);
8101 PUSH_SEQ(ret, cond_seq);
8102 PUSH_SEQ(ret, body_seq);
8103 PUSH_LABEL(ret, end_label);
8107pm_compile_forwarding_super_node(rb_iseq_t *iseq,
const pm_forwarding_super_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8109 const rb_iseq_t *block = NULL;
8110 const rb_iseq_t *previous_block = NULL;
8111 LABEL *retry_label = NULL;
8112 LABEL *retry_end_l = NULL;
8114 if (node->
block != NULL) {
8115 previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8116 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
8118 retry_label = NEW_LABEL(location->
line);
8119 retry_end_l = NEW_LABEL(location->
line);
8121 PUSH_LABEL(ret, retry_label);
8124 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8127 PUSH_INSN(ret, *location, putself);
8128 int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
8130 if (node->
block != NULL) {
8131 pm_scope_node_t next_scope_node;
8132 pm_scope_node_init((
const pm_node_t *) node->
block, &next_scope_node, scope_node);
8134 ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
8135 pm_scope_node_destroy(&next_scope_node);
8142 const rb_iseq_t *local_iseq = body->local_iseq;
8146 int depth = get_lvar_level(iseq);
8148 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.
flags.forwardable) {
8149 flag |= VM_CALL_FORWARDING;
8150 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
8151 PUSH_GETLOCAL(ret, *location, mult_local.index, mult_local.level);
8153 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, 0, flag, NULL, block != NULL);
8154 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, block);
8156 if (popped) PUSH_INSN(ret, *location, pop);
8158 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8163 if (local_body->param.flags.has_lead) {
8165 for (
int i = 0; i < local_body->param.lead_num; i++) {
8166 int idx = local_body->local_table_size - i;
8167 PUSH_GETLOCAL(args, *location, idx, depth);
8169 argc += local_body->param.lead_num;
8172 if (local_body->param.flags.has_opt) {
8174 for (
int j = 0; j < local_body->param.opt_num; j++) {
8175 int idx = local_body->local_table_size - (argc + j);
8176 PUSH_GETLOCAL(args, *location, idx, depth);
8178 argc += local_body->param.opt_num;
8181 if (local_body->param.flags.has_rest) {
8183 int idx = local_body->local_table_size - local_body->param.rest_start;
8184 PUSH_GETLOCAL(args, *location, idx, depth);
8185 PUSH_INSN1(args, *location, splatarray,
Qfalse);
8187 argc = local_body->param.rest_start + 1;
8188 flag |= VM_CALL_ARGS_SPLAT;
8191 if (local_body->param.flags.has_post) {
8193 int post_len = local_body->param.post_num;
8194 int post_start = local_body->param.post_start;
8197 for (; j < post_len; j++) {
8198 int idx = local_body->local_table_size - (post_start + j);
8199 PUSH_GETLOCAL(args, *location, idx, depth);
8202 if (local_body->param.flags.has_rest) {
8204 PUSH_INSN1(args, *location, newarray,
INT2FIX(j));
8205 PUSH_INSN(args, *location, concatarray);
8208 argc = post_len + post_start;
8212 const struct rb_iseq_param_keyword *
const local_keyword = local_body->param.keyword;
8213 if (local_body->param.flags.has_kw) {
8214 int local_size = local_body->local_table_size;
8221 if (local_body->param.flags.has_kwrest) {
8222 int idx = local_body->local_table_size - local_keyword->rest_start;
8224 PUSH_INSN1(args, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8225 PUSH_GETLOCAL(args, *location, idx, depth);
8229 for (; i < local_keyword->num; ++i) {
8230 ID id = local_keyword->table[i];
8231 int idx = local_size - get_local_var_idx(local_iseq,
id);
8233 PUSH_INSN1(args, *location, putobject,
ID2SYM(
id));
8234 PUSH_GETLOCAL(args, *location, idx, depth);
8237 if (local_body->param.flags.has_kwrest) {
8238 PUSH_SEND(args, *location, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
8241 PUSH_INSN1(args, *location, newhash,
INT2FIX(i * 2));
8243 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
8245 else if (local_body->param.flags.has_kwrest) {
8246 int idx = local_body->local_table_size - local_keyword->rest_start;
8247 PUSH_GETLOCAL(args, *location, idx, depth);
8249 flag |= VM_CALL_KW_SPLAT;
8252 PUSH_SEQ(ret, args);
8255 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flag, NULL, block != NULL);
8256 PUSH_INSN2(ret, *location, invokesuper, callinfo, block);
8259 if (node->
block != NULL) {
8260 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8261 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
8262 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8265 if (popped) PUSH_INSN(ret, *location, pop);
8269pm_compile_match_required_node(rb_iseq_t *iseq,
const pm_match_required_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8271 LABEL *matched_label = NEW_LABEL(location->
line);
8272 LABEL *unmatched_label = NEW_LABEL(location->
line);
8273 LABEL *done_label = NEW_LABEL(location->
line);
8277 PUSH_INSN(ret, *location, putnil);
8278 PUSH_INSN(ret, *location, putnil);
8279 PUSH_INSN1(ret, *location, putobject,
Qfalse);
8280 PUSH_INSN(ret, *location, putnil);
8281 PUSH_INSN(ret, *location, putnil);
8285 PM_COMPILE_NOT_POPPED(node->
value);
8289 PUSH_INSN(ret, *location, dup);
8296 pm_compile_pattern(iseq, scope_node, node->
pattern, ret, matched_label, unmatched_label,
true,
true, 2);
8301 PUSH_LABEL(ret, unmatched_label);
8302 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, ret, done_label, popped);
8306 PUSH_LABEL(ret, matched_label);
8307 PUSH_INSN1(ret, *location, adjuststack,
INT2FIX(6));
8308 if (!popped) PUSH_INSN(ret, *location, putnil);
8309 PUSH_INSNL(ret, *location, jump, done_label);
8311 PUSH_LABEL(ret, done_label);
8315pm_compile_match_write_node(rb_iseq_t *iseq,
const pm_match_write_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8317 LABEL *fail_label = NEW_LABEL(location->
line);
8318 LABEL *end_label = NEW_LABEL(location->
line);
8322 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
call);
8329 PUSH_INSN1(ret, *location, getglobal, operand);
8332 PUSH_INSN(ret, *location, dup);
8333 PUSH_INSNL(ret, *location, branchunless, fail_label);
8339 if (targets_count == 1) {
8341 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8343 const pm_local_variable_target_node_t *local_target = (
const pm_local_variable_target_node_t *) target;
8344 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->
name, local_target->
depth);
8348 PUSH_INSN1(ret, *location, putobject, operand);
8351 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8352 PUSH_LABEL(ret, fail_label);
8353 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8354 if (popped) PUSH_INSN(ret, *location, pop);
8358 DECL_ANCHOR(fail_anchor);
8362 for (uint32_t targets_index = 0; targets_index < targets_count; targets_index++) {
8363 const pm_node_t *target = node->
targets.
nodes[targets_index];
8364 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8366 const pm_local_variable_target_node_t *local_target = (
const pm_local_variable_target_node_t *) target;
8367 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->
name, local_target->
depth);
8369 if (targets_index < targets_count - 1) {
8370 PUSH_INSN(ret, *location, dup);
8375 PUSH_INSN1(ret, *location, putobject, operand);
8378 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8379 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8381 PUSH_INSN(fail_anchor, *location, putnil);
8382 PUSH_SETLOCAL(fail_anchor, *location, index.index, index.level);
8386 PUSH_INSNL(ret, *location, jump, end_label);
8390 PUSH_LABEL(ret, fail_label);
8391 PUSH_INSN(ret, *location, pop);
8392 PUSH_SEQ(ret, fail_anchor);
8395 PUSH_LABEL(ret, end_label);
8396 if (popped) PUSH_INSN(ret, *location, pop);
8400pm_compile_next_node(rb_iseq_t *iseq,
const pm_next_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8402 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8403 LABEL *splabel = NEW_LABEL(0);
8404 PUSH_LABEL(ret, splabel);
8407 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
8410 PUSH_INSN(ret, *location, putnil);
8412 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8414 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8415 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8417 PUSH_ADJUST_RESTORE(ret, splabel);
8418 if (!popped) PUSH_INSN(ret, *location, putnil);
8420 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8421 LABEL *splabel = NEW_LABEL(0);
8423 PUSH_LABEL(ret, splabel);
8424 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8427 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
8430 PUSH_INSN(ret, *location, putnil);
8433 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8434 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8435 PUSH_ADJUST_RESTORE(ret, splabel);
8436 splabel->unremovable = FALSE;
8438 if (!popped) PUSH_INSN(ret, *location, putnil);
8441 const rb_iseq_t *ip = iseq;
8442 unsigned long throw_flag = 0;
8445 if (!ISEQ_COMPILE_DATA(ip)) {
8450 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8451 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8455 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8458 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8459 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8463 ip = ISEQ_BODY(ip)->parent_iseq;
8468 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
8471 PUSH_INSN(ret, *location, putnil);
8474 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8475 if (popped) PUSH_INSN(ret, *location, pop);
8478 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8484pm_compile_or_node(rb_iseq_t *iseq,
const pm_or_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8486 const pm_node_t *cursor = (
const pm_node_t *) node;
8489 while (PM_NODE_TYPE_P(cursor, PM_OR_NODE)) {
8490 cursor = ((
const pm_or_node_t *) cursor)->left;
8495 const pm_node_t **nodes =
ALLOCV_N(
const pm_node_t *, handle, size);
8497 cursor = (
const pm_node_t *) node;
8498 size_t index = size;
8500 while (PM_NODE_TYPE_P(cursor, PM_OR_NODE)) {
8501 const pm_or_node_t *
cast = (
const pm_or_node_t *) cursor;
8502 nodes[--index] =
cast->right;
8503 nodes[--index] = cursor;
8504 cursor =
cast->left;
8508 pm_compile_logical_chain(iseq, size, nodes, location, ret, popped, scope_node);
8513pm_compile_redo_node(rb_iseq_t *iseq,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8515 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8516 LABEL *splabel = NEW_LABEL(0);
8518 PUSH_LABEL(ret, splabel);
8519 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8520 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8522 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8523 PUSH_ADJUST_RESTORE(ret, splabel);
8524 if (!popped) PUSH_INSN(ret, *location, putnil);
8526 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8527 LABEL *splabel = NEW_LABEL(0);
8529 PUSH_LABEL(ret, splabel);
8530 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8531 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8533 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8534 PUSH_ADJUST_RESTORE(ret, splabel);
8535 if (!popped) PUSH_INSN(ret, *location, putnil);
8538 const rb_iseq_t *ip = iseq;
8541 if (!ISEQ_COMPILE_DATA(ip)) {
8546 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8549 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8552 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8553 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8557 ip = ISEQ_BODY(ip)->parent_iseq;
8561 PUSH_INSN(ret, *location, putnil);
8562 PUSH_INSN1(ret, *location,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8563 if (popped) PUSH_INSN(ret, *location, pop);
8566 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8572pm_compile_rescue_node(rb_iseq_t *iseq,
const pm_rescue_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8574 iseq_set_exception_local_table(iseq);
8578 LABEL *exception_match_label = NEW_LABEL(location->
line);
8579 LABEL *rescue_end_label = NEW_LABEL(location->
line);
8587 const pm_node_list_t *exceptions = &node->
exceptions;
8589 if (exceptions->
size > 0) {
8590 for (uint32_t index = 0; index < exceptions->
size; index++) {
8591 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8592 PM_COMPILE(exceptions->
nodes[index]);
8593 int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
8594 if (PM_NODE_TYPE_P(exceptions->
nodes[index], PM_SPLAT_NODE)) {
8595 checkmatch_flags |= VM_CHECKMATCH_ARRAY;
8597 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(checkmatch_flags));
8598 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8602 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8604 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8605 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8610 PUSH_INSNL(ret, *location, jump, rescue_end_label);
8615 PUSH_LABEL(ret, exception_match_label);
8622 DECL_ANCHOR(writes);
8623 DECL_ANCHOR(cleanup);
8625 pm_compile_target_node(iseq, node->
reference, ret, writes, cleanup, scope_node, NULL);
8626 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8628 PUSH_SEQ(ret, writes);
8629 PUSH_SEQ(ret, cleanup);
8639 LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
8640 ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
8642 PM_COMPILE((
const pm_node_t *) node->
statements);
8645 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
8648 PUSH_INSN(ret, *location, putnil);
8651 PUSH_INSN(ret, *location, leave);
8657 PUSH_LABEL(ret, rescue_end_label);
8659 PM_COMPILE((
const pm_node_t *) node->
subsequent);
8662 PUSH_GETLOCAL(ret, *location, 1, 0);
8667pm_compile_return_node(rb_iseq_t *iseq,
const pm_return_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8669 const pm_arguments_node_t *arguments = node->
arguments;
8670 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8673 const rb_iseq_t *parent_iseq = iseq;
8674 enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
8675 while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
8676 if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq))
break;
8677 parent_type = ISEQ_BODY(parent_iseq)->type;
8680 switch (parent_type) {
8682 case ISEQ_TYPE_MAIN:
8684 rb_warn(
"argument of top-level return is ignored");
8686 if (parent_iseq == iseq) {
8687 type = ISEQ_TYPE_METHOD;
8694 if (
type == ISEQ_TYPE_METHOD) {
8695 splabel = NEW_LABEL(0);
8696 PUSH_LABEL(ret, splabel);
8697 PUSH_ADJUST(ret, *location, 0);
8700 if (arguments != NULL) {
8701 PM_COMPILE_NOT_POPPED((
const pm_node_t *) arguments);
8704 PUSH_INSN(ret, *location, putnil);
8707 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8708 pm_add_ensure_iseq(ret, iseq, 1, scope_node);
8710 PUSH_INSN(ret, *location, leave);
8711 PUSH_ADJUST_RESTORE(ret, splabel);
8712 if (!popped) PUSH_INSN(ret, *location, putnil);
8715 PUSH_INSN1(ret, *location,
throw,
INT2FIX(TAG_RETURN));
8716 if (popped) PUSH_INSN(ret, *location, pop);
8721pm_compile_super_node(rb_iseq_t *iseq,
const pm_super_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8725 LABEL *retry_label = NEW_LABEL(location->
line);
8726 LABEL *retry_end_l = NEW_LABEL(location->
line);
8728 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8729 const rb_iseq_t *current_block;
8730 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
8732 PUSH_LABEL(ret, retry_label);
8733 PUSH_INSN(ret, *location, putself);
8737 int argc = pm_setup_args(node->
arguments, node->
block, &flags, &keywords, iseq, ret, scope_node, location);
8738 bool is_forwardable = (node->
arguments != NULL) && PM_NODE_FLAG_P(node->
arguments, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING);
8739 flags |= VM_CALL_SUPER | VM_CALL_FCALL;
8741 if (node->
block && PM_NODE_TYPE_P(node->
block, PM_BLOCK_NODE)) {
8742 pm_scope_node_t next_scope_node;
8743 pm_scope_node_init(node->
block, &next_scope_node, scope_node);
8745 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
8746 pm_scope_node_destroy(&next_scope_node);
8750 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8753 if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
8754 PUSH_INSN(args, *location, splatkw);
8757 PUSH_SEQ(ret, args);
8758 if (is_forwardable && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.
flags.forwardable) {
8759 flags |= VM_CALL_FORWARDING;
8762 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8763 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, current_block);
8768 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8769 PUSH_INSN2(ret, *location, invokesuper, callinfo, current_block);
8774 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8776 if (popped) PUSH_INSN(ret, *location, pop);
8777 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8778 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
8782pm_compile_yield_node(rb_iseq_t *iseq,
const pm_yield_node_t *node,
const pm_node_location_t *location, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8784 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
8786 case ISEQ_TYPE_MAIN:
8787 case ISEQ_TYPE_CLASS:
8788 COMPILE_ERROR(iseq, location->
line,
"Invalid yield");
8798 argc = pm_setup_args(node->
arguments, NULL, &flags, &keywords, iseq, ret, scope_node, location);
8801 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, FALSE);
8802 PUSH_INSN1(ret, *location, invokeblock, callinfo);
8804 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8805 if (popped) PUSH_INSN(ret, *location, pop);
8808 for (
const rb_iseq_t *tmp_iseq = iseq; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++) {
8809 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
8812 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
8816pm_compile_regexp_once(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_node_location_t *node_location, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node)
8818 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
8819 const rb_iseq_t *block_iseq = NULL;
8820 int ise_index = ISEQ_BODY(iseq)->ise_size++;
8822 pm_scope_node_t next_scope_node;
8823 pm_scope_node_init(node, &next_scope_node, scope_node);
8825 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, node_location->
line);
8826 pm_scope_node_destroy(&next_scope_node);
8828 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
8829 PUSH_INSN2(ret, *node_location, once, block_iseq,
INT2FIX(ise_index));
8830 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
8844pm_compile_node(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8847 int lineno = (int) location.
line;
8849 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)) {
8854 lineno = (int) PM_NODE_START_LINE_COLUMN(((
const pm_begin_node_t *) node)->rescue_clause).line;
8857 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
8863 ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
8864 if (lineno > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
8865 event |= RUBY_EVENT_COVERAGE_LINE;
8867 PUSH_TRACE(ret, event);
8870 switch (PM_NODE_TYPE(node)) {
8871 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
8874 pm_compile_alias_global_variable_node(iseq, (
const pm_alias_global_variable_node_t *) node, &location, ret, popped, scope_node);
8876 case PM_ALIAS_METHOD_NODE:
8879 pm_compile_alias_method_node(iseq, (
const pm_alias_method_node_t *) node, &location, ret, popped, scope_node);
8884 pm_compile_and_node(iseq, (
const pm_and_node_t *) node, &location, ret, popped, scope_node);
8886 case PM_ARGUMENTS_NODE: {
8893 const pm_arguments_node_t *
cast = (
const pm_arguments_node_t *) node;
8894 const pm_node_list_t *elements = &
cast->arguments;
8896 if (elements->
size == 1) {
8899 PM_COMPILE(elements->
nodes[0]);
8902 pm_compile_array_node(iseq, (
const pm_node_t *) cast, elements, &location, ret, popped, scope_node);
8906 case PM_ARRAY_NODE: {
8909 const pm_array_node_t *
cast = (
const pm_array_node_t *) node;
8910 pm_compile_array_node(iseq, (
const pm_node_t *) cast, &
cast->elements, &location, ret, popped, scope_node);
8913 case PM_ASSOC_NODE: {
8919 const pm_assoc_node_t *
cast = (
const pm_assoc_node_t *) node;
8921 PM_COMPILE(
cast->key);
8922 PM_COMPILE(
cast->value);
8926 case PM_ASSOC_SPLAT_NODE: {
8932 const pm_assoc_splat_node_t *
cast = (
const pm_assoc_splat_node_t *) node;
8934 if (
cast->value != NULL) {
8935 PM_COMPILE(
cast->value);
8938 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
8939 PUSH_GETLOCAL(ret, location, index.index, index.level);
8944 case PM_BACK_REFERENCE_READ_NODE: {
8948 const pm_back_reference_read_node_t *
cast = (
const pm_back_reference_read_node_t *) node;
8949 VALUE backref = pm_compile_back_reference_ref(scope_node, cast);
8951 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), backref);
8955 case PM_BEGIN_NODE: {
8958 const pm_begin_node_t *
cast = (
const pm_begin_node_t *) node;
8960 if (
cast->ensure_clause) {
8963 pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
8965 else if (
cast->rescue_clause) {
8967 pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
8972 if (
cast->statements != NULL) {
8973 PM_COMPILE((
const pm_node_t *)
cast->statements);
8976 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
8981 case PM_BLOCK_ARGUMENT_NODE: {
8984 const pm_block_argument_node_t *
cast = (
const pm_block_argument_node_t *) node;
8986 if (
cast->expression != NULL) {
8987 PM_COMPILE(
cast->expression);
8991 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
8992 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(local_index.level));
9002 pm_compile_break_node(iseq, (
const pm_break_node_t *) node, &location, ret, popped, scope_node);
9013 pm_compile_call_node(iseq, (
const pm_call_node_t *) node, ret, popped, scope_node);
9015 case PM_CALL_AND_WRITE_NODE: {
9018 const pm_call_and_write_node_t *
cast = (
const pm_call_and_write_node_t *) node;
9019 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);
9022 case PM_CALL_OR_WRITE_NODE: {
9025 const pm_call_or_write_node_t *
cast = (
const pm_call_or_write_node_t *) node;
9026 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);
9029 case PM_CALL_OPERATOR_WRITE_NODE:
9039 pm_compile_call_operator_write_node(iseq, (
const pm_call_operator_write_node_t *) node, &location, ret, popped, scope_node);
9044 pm_compile_case_node(iseq, (
const pm_case_node_t *) node, &location, ret, popped, scope_node);
9046 case PM_CASE_MATCH_NODE:
9055 pm_compile_case_match_node(iseq, (
const pm_case_match_node_t *) node, &location, ret, popped, scope_node);
9057 case PM_CLASS_NODE: {
9060 const pm_class_node_t *
cast = (
const pm_class_node_t *) node;
9062 ID class_id = pm_constant_id_lookup(scope_node,
cast->name);
9063 VALUE class_name =
rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(class_id)));
9065 pm_scope_node_t next_scope_node;
9066 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9068 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, location.
line);
9069 pm_scope_node_destroy(&next_scope_node);
9072 const int flags = VM_DEFINECLASS_TYPE_CLASS |
9073 (
cast->superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
9074 pm_compile_class_path(iseq,
cast->constant_path, &location, ret,
false, scope_node);
9076 if (
cast->superclass) {
9077 PM_COMPILE_NOT_POPPED(
cast->superclass);
9080 PUSH_INSN(ret, location, putnil);
9085 PUSH_INSN3(ret, location, defineclass, operand, class_iseq,
INT2FIX(flags));
9089 if (popped) PUSH_INSN(ret, location, pop);
9092 case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
9095 const pm_class_variable_and_write_node_t *
cast = (
const pm_class_variable_and_write_node_t *) node;
9096 LABEL *end_label = NEW_LABEL(location.
line);
9098 ID name_id = pm_constant_id_lookup(scope_node,
cast->name);
9101 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
9102 if (!popped) PUSH_INSN(ret, location, dup);
9104 PUSH_INSNL(ret, location, branchunless, end_label);
9105 if (!popped) PUSH_INSN(ret, location, pop);
9107 PM_COMPILE_NOT_POPPED(
cast->value);
9108 if (!popped) PUSH_INSN(ret, location, dup);
9110 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
9111 PUSH_LABEL(ret, end_label);
9115 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
9118 const pm_class_variable_operator_write_node_t *
cast = (
const pm_class_variable_operator_write_node_t *) node;
9120 ID name_id = pm_constant_id_lookup(scope_node,
cast->name);
9123 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
9124 PM_COMPILE_NOT_POPPED(
cast->value);
9126 ID method_id = pm_constant_id_lookup(scope_node,
cast->binary_operator);
9127 int flags = VM_CALL_ARGS_SIMPLE;
9128 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9130 if (!popped) PUSH_INSN(ret, location, dup);
9131 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
9135 case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
9138 const pm_class_variable_or_write_node_t *
cast = (
const pm_class_variable_or_write_node_t *) node;
9139 LABEL *end_label = NEW_LABEL(location.
line);
9140 LABEL *start_label = NEW_LABEL(location.
line);
9142 ID name_id = pm_constant_id_lookup(scope_node,
cast->name);
9145 PUSH_INSN(ret, location, putnil);
9146 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR), name,
Qtrue);
9147 PUSH_INSNL(ret, location, branchunless, start_label);
9149 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
9150 if (!popped) PUSH_INSN(ret, location, dup);
9152 PUSH_INSNL(ret, location, branchif, end_label);
9153 if (!popped) PUSH_INSN(ret, location, pop);
9155 PUSH_LABEL(ret, start_label);
9156 PM_COMPILE_NOT_POPPED(
cast->value);
9157 if (!popped) PUSH_INSN(ret, location, dup);
9159 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
9160 PUSH_LABEL(ret, end_label);
9164 case PM_CLASS_VARIABLE_READ_NODE: {
9168 const pm_class_variable_read_node_t *
cast = (
const pm_class_variable_read_node_t *) node;
9169 ID name = pm_constant_id_lookup(scope_node,
cast->name);
9170 PUSH_INSN2(ret, location, getclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
9174 case PM_CLASS_VARIABLE_WRITE_NODE: {
9177 const pm_class_variable_write_node_t *
cast = (
const pm_class_variable_write_node_t *) node;
9178 PM_COMPILE_NOT_POPPED(
cast->value);
9179 if (!popped) PUSH_INSN(ret, location, dup);
9181 ID name = pm_constant_id_lookup(scope_node,
cast->name);
9182 PUSH_INSN2(ret, location, setclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
9186 case PM_CONSTANT_PATH_NODE: {
9191 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) !=
Qnil)) {
9192 ISEQ_BODY(iseq)->ic_size++;
9194 PUSH_INSN1(ret, location, opt_getconstant_path, parts);
9197 DECL_ANCHOR(prefix);
9200 pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
9201 if (LIST_INSN_SIZE_ZERO(prefix)) {
9202 PUSH_INSN(ret, location, putnil);
9205 PUSH_SEQ(ret, prefix);
9208 PUSH_SEQ(ret, body);
9211 if (popped) PUSH_INSN(ret, location, pop);
9214 case PM_CONSTANT_PATH_AND_WRITE_NODE: {
9217 const pm_constant_path_and_write_node_t *
cast = (
const pm_constant_path_and_write_node_t *) node;
9218 pm_compile_constant_path_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9221 case PM_CONSTANT_PATH_OR_WRITE_NODE: {
9224 const pm_constant_path_or_write_node_t *
cast = (
const pm_constant_path_or_write_node_t *) node;
9225 pm_compile_constant_path_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9228 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
9231 const pm_constant_path_operator_write_node_t *
cast = (
const pm_constant_path_operator_write_node_t *) node;
9232 pm_compile_constant_path_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9235 case PM_CONSTANT_PATH_WRITE_NODE: {
9238 const pm_constant_path_write_node_t *
cast = (
const pm_constant_path_write_node_t *) node;
9239 pm_compile_constant_path_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9242 case PM_CONSTANT_READ_NODE: {
9245 const pm_constant_read_node_t *
cast = (
const pm_constant_read_node_t *) node;
9248 pm_compile_constant_read(iseq, name, &
cast->base.location, location.
node_id, ret, scope_node);
9249 if (popped) PUSH_INSN(ret, location, pop);
9253 case PM_CONSTANT_AND_WRITE_NODE: {
9256 const pm_constant_and_write_node_t *
cast = (
const pm_constant_and_write_node_t *) node;
9257 pm_compile_constant_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9260 case PM_CONSTANT_OR_WRITE_NODE: {
9263 const pm_constant_or_write_node_t *
cast = (
const pm_constant_or_write_node_t *) node;
9264 pm_compile_constant_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9267 case PM_CONSTANT_OPERATOR_WRITE_NODE: {
9270 const pm_constant_operator_write_node_t *
cast = (
const pm_constant_operator_write_node_t *) node;
9271 pm_compile_constant_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9274 case PM_CONSTANT_WRITE_NODE: {
9277 const pm_constant_write_node_t *
cast = (
const pm_constant_write_node_t *) node;
9278 pm_compile_constant_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9287 const pm_def_node_t *
cast = (
const pm_def_node_t *) node;
9288 ID method_name = pm_constant_id_lookup(scope_node,
cast->name);
9290 pm_scope_node_t next_scope_node;
9291 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9293 rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, location.
line);
9294 pm_scope_node_destroy(&next_scope_node);
9296 if (
cast->receiver) {
9297 PM_COMPILE_NOT_POPPED(
cast->receiver);
9298 PUSH_INSN2(ret, location, definesmethod,
ID2SYM(method_name), method_iseq);
9301 PUSH_INSN2(ret, location, definemethod,
ID2SYM(method_name), method_iseq);
9306 PUSH_INSN1(ret, location, putobject,
ID2SYM(method_name));
9311 case PM_DEFINED_NODE: {
9314 const pm_defined_node_t *
cast = (
const pm_defined_node_t *) node;
9315 pm_compile_defined_expr(iseq,
cast->value, &location, ret, popped, scope_node,
false);
9318 case PM_EMBEDDED_STATEMENTS_NODE: {
9321 const pm_embedded_statements_node_t *
cast = (
const pm_embedded_statements_node_t *) node;
9323 if (
cast->statements != NULL) {
9324 PM_COMPILE((
const pm_node_t *) (
cast->statements));
9327 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9330 if (popped) PUSH_INSN(ret, location, pop);
9333 case PM_EMBEDDED_VARIABLE_NODE: {
9336 const pm_embedded_variable_node_t *
cast = (
const pm_embedded_variable_node_t *) node;
9337 PM_COMPILE(
cast->variable);
9340 case PM_FALSE_NODE: {
9344 PUSH_INSN1(ret, location, putobject,
Qfalse);
9348 case PM_ENSURE_NODE: {
9349 const pm_ensure_node_t *
cast = (
const pm_ensure_node_t *) node;
9351 if (
cast->statements != NULL) {
9352 PM_COMPILE((
const pm_node_t *)
cast->statements);
9357 case PM_ELSE_NODE: {
9360 const pm_else_node_t *
cast = (
const pm_else_node_t *) node;
9362 if (
cast->statements != NULL) {
9363 PM_COMPILE((
const pm_node_t *)
cast->statements);
9366 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9371 case PM_FLIP_FLOP_NODE: {
9374 const pm_flip_flop_node_t *
cast = (
const pm_flip_flop_node_t *) node;
9376 LABEL *final_label = NEW_LABEL(location.
line);
9377 LABEL *then_label = NEW_LABEL(location.
line);
9378 LABEL *else_label = NEW_LABEL(location.
line);
9380 pm_compile_flip_flop(cast, else_label, then_label, iseq, location.
line, ret, popped, scope_node);
9382 PUSH_LABEL(ret, then_label);
9383 PUSH_INSN1(ret, location, putobject,
Qtrue);
9384 PUSH_INSNL(ret, location, jump, final_label);
9385 PUSH_LABEL(ret, else_label);
9386 PUSH_INSN1(ret, location, putobject,
Qfalse);
9387 PUSH_LABEL(ret, final_label);
9391 case PM_FLOAT_NODE: {
9395 VALUE operand = parse_float((
const pm_float_node_t *) node);
9396 PUSH_INSN1(ret, location, putobject, operand);
9403 const pm_for_node_t *
cast = (
const pm_for_node_t *) node;
9405 LABEL *retry_label = NEW_LABEL(location.
line);
9406 LABEL *retry_end_l = NEW_LABEL(location.
line);
9409 PUSH_LABEL(ret, retry_label);
9410 PM_COMPILE_NOT_POPPED(
cast->collection);
9414 pm_scope_node_t next_scope_node;
9415 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9417 const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.
line);
9418 pm_scope_node_destroy(&next_scope_node);
9420 const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9421 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
9425 PUSH_SEND_WITH_BLOCK(ret, location, idEach,
INT2FIX(0), child_iseq);
9426 pm_compile_retry_end_label(iseq, ret, retry_end_l);
9428 if (popped) PUSH_INSN(ret, location, pop);
9429 ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
9430 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
9433 case PM_FORWARDING_ARGUMENTS_NODE:
9434 rb_bug(
"Cannot compile a ForwardingArgumentsNode directly\n");
9436 case PM_FORWARDING_SUPER_NODE:
9442 pm_compile_forwarding_super_node(iseq, (
const pm_forwarding_super_node_t *) node, &location, ret, popped, scope_node);
9444 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
9447 const pm_global_variable_and_write_node_t *
cast = (
const pm_global_variable_and_write_node_t *) node;
9448 LABEL *end_label = NEW_LABEL(location.
line);
9451 PUSH_INSN1(ret, location, getglobal, name);
9452 if (!popped) PUSH_INSN(ret, location, dup);
9454 PUSH_INSNL(ret, location, branchunless, end_label);
9455 if (!popped) PUSH_INSN(ret, location, pop);
9457 PM_COMPILE_NOT_POPPED(
cast->value);
9458 if (!popped) PUSH_INSN(ret, location, dup);
9460 PUSH_INSN1(ret, location, setglobal, name);
9461 PUSH_LABEL(ret, end_label);
9465 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
9468 const pm_global_variable_operator_write_node_t *
cast = (
const pm_global_variable_operator_write_node_t *) node;
9471 PUSH_INSN1(ret, location, getglobal, name);
9472 PM_COMPILE_NOT_POPPED(
cast->value);
9474 ID method_id = pm_constant_id_lookup(scope_node,
cast->binary_operator);
9475 int flags = VM_CALL_ARGS_SIMPLE;
9476 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9478 if (!popped) PUSH_INSN(ret, location, dup);
9479 PUSH_INSN1(ret, location, setglobal, name);
9483 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
9486 const pm_global_variable_or_write_node_t *
cast = (
const pm_global_variable_or_write_node_t *) node;
9487 LABEL *set_label = NEW_LABEL(location.
line);
9488 LABEL *end_label = NEW_LABEL(location.
line);
9490 PUSH_INSN(ret, location, putnil);
9493 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR), name,
Qtrue);
9494 PUSH_INSNL(ret, location, branchunless, set_label);
9496 PUSH_INSN1(ret, location, getglobal, name);
9497 if (!popped) PUSH_INSN(ret, location, dup);
9499 PUSH_INSNL(ret, location, branchif, end_label);
9500 if (!popped) PUSH_INSN(ret, location, pop);
9502 PUSH_LABEL(ret, set_label);
9503 PM_COMPILE_NOT_POPPED(
cast->value);
9504 if (!popped) PUSH_INSN(ret, location, dup);
9506 PUSH_INSN1(ret, location, setglobal, name);
9507 PUSH_LABEL(ret, end_label);
9511 case PM_GLOBAL_VARIABLE_READ_NODE: {
9514 const pm_global_variable_read_node_t *
cast = (
const pm_global_variable_read_node_t *) node;
9517 PUSH_INSN1(ret, location, getglobal, name);
9518 if (popped) PUSH_INSN(ret, location, pop);
9522 case PM_GLOBAL_VARIABLE_WRITE_NODE: {
9525 const pm_global_variable_write_node_t *
cast = (
const pm_global_variable_write_node_t *) node;
9526 PM_COMPILE_NOT_POPPED(
cast->value);
9527 if (!popped) PUSH_INSN(ret, location, dup);
9529 ID name = pm_constant_id_lookup(scope_node,
cast->name);
9530 PUSH_INSN1(ret, location, setglobal,
ID2SYM(name));
9534 case PM_HASH_NODE: {
9540 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9545 const pm_hash_node_t *
cast = (
const pm_hash_node_t *) node;
9547 if (
cast->elements.size == 0) {
9548 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
9551 VALUE value = pm_static_literal_value(iseq, node, scope_node);
9552 PUSH_INSN1(ret, location, duphash, value);
9566 const pm_hash_node_t *
cast = (
const pm_hash_node_t *) node;
9567 const pm_node_list_t *elements = &
cast->elements;
9573 for (uint32_t index = 0; index < elements->
size; index++) {
9574 PM_COMPILE_POPPED(elements->
nodes[index]);
9578 pm_compile_hash_elements(iseq, node, elements, 0,
Qundef,
false, ret, scope_node);
9593 const pm_if_node_t *
cast = (
const pm_if_node_t *) node;
9594 pm_compile_conditional(iseq, &location, PM_IF_NODE, (
const pm_node_t *) cast,
cast->statements,
cast->subsequent,
cast->predicate, ret, popped, scope_node);
9597 case PM_IMAGINARY_NODE: {
9601 VALUE operand = parse_imaginary((
const pm_imaginary_node_t *) node);
9602 PUSH_INSN1(ret, location, putobject, operand);
9606 case PM_IMPLICIT_NODE: {
9615 const pm_implicit_node_t *
cast = (
const pm_implicit_node_t *) node;
9616 PM_COMPILE(
cast->value);
9622 rb_bug(
"Should not ever enter an in node directly");
9625 case PM_INDEX_OPERATOR_WRITE_NODE: {
9628 const pm_index_operator_write_node_t *
cast = (
const pm_index_operator_write_node_t *) node;
9629 pm_compile_index_operator_write_node(iseq, cast, &location, ret, popped, scope_node);
9632 case PM_INDEX_AND_WRITE_NODE: {
9635 const pm_index_and_write_node_t *
cast = (
const pm_index_and_write_node_t *) node;
9636 pm_compile_index_control_flow_write_node(iseq, node,
cast->receiver,
cast->arguments,
cast->block,
cast->value, &location, ret, popped, scope_node);
9639 case PM_INDEX_OR_WRITE_NODE: {
9642 const pm_index_or_write_node_t *
cast = (
const pm_index_or_write_node_t *) node;
9643 pm_compile_index_control_flow_write_node(iseq, node,
cast->receiver,
cast->arguments,
cast->block,
cast->value, &location, ret, popped, scope_node);
9646 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
9649 const pm_instance_variable_and_write_node_t *
cast = (
const pm_instance_variable_and_write_node_t *) node;
9650 LABEL *end_label = NEW_LABEL(location.
line);
9652 ID name_id = pm_constant_id_lookup(scope_node,
cast->name);
9655 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9656 if (!popped) PUSH_INSN(ret, location, dup);
9658 PUSH_INSNL(ret, location, branchunless, end_label);
9659 if (!popped) PUSH_INSN(ret, location, pop);
9661 PM_COMPILE_NOT_POPPED(
cast->value);
9662 if (!popped) PUSH_INSN(ret, location, dup);
9664 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9665 PUSH_LABEL(ret, end_label);
9669 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
9672 const pm_instance_variable_operator_write_node_t *
cast = (
const pm_instance_variable_operator_write_node_t *) node;
9674 ID name_id = pm_constant_id_lookup(scope_node,
cast->name);
9677 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9678 PM_COMPILE_NOT_POPPED(
cast->value);
9680 ID method_id = pm_constant_id_lookup(scope_node,
cast->binary_operator);
9681 int flags = VM_CALL_ARGS_SIMPLE;
9682 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9684 if (!popped) PUSH_INSN(ret, location, dup);
9685 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9689 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
9692 const pm_instance_variable_or_write_node_t *
cast = (
const pm_instance_variable_or_write_node_t *) node;
9693 LABEL *end_label = NEW_LABEL(location.
line);
9695 ID name_id = pm_constant_id_lookup(scope_node,
cast->name);
9698 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9699 if (!popped) PUSH_INSN(ret, location, dup);
9701 PUSH_INSNL(ret, location, branchif, end_label);
9702 if (!popped) PUSH_INSN(ret, location, pop);
9704 PM_COMPILE_NOT_POPPED(
cast->value);
9705 if (!popped) PUSH_INSN(ret, location, dup);
9707 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9708 PUSH_LABEL(ret, end_label);
9712 case PM_INSTANCE_VARIABLE_READ_NODE: {
9716 const pm_instance_variable_read_node_t *
cast = (
const pm_instance_variable_read_node_t *) node;
9717 ID name = pm_constant_id_lookup(scope_node,
cast->name);
9718 PUSH_INSN2(ret, location, getinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9722 case PM_INSTANCE_VARIABLE_WRITE_NODE: {
9725 const pm_instance_variable_write_node_t *
cast = (
const pm_instance_variable_write_node_t *) node;
9726 PM_COMPILE_NOT_POPPED(
cast->value);
9727 if (!popped) PUSH_INSN(ret, location, dup);
9729 ID name = pm_constant_id_lookup(scope_node,
cast->name);
9730 PUSH_INSN2(ret, location, setinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9734 case PM_INTEGER_NODE: {
9738 VALUE operand = parse_integer((
const pm_integer_node_t *) node);
9739 PUSH_INSN1(ret, location, putobject, operand);
9743 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
9746 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ONCE)) {
9747 pm_compile_regexp_once(iseq, node, &location, ret, scope_node);
9749 else if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9750 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9751 PUSH_INSN1(ret, location, putobject, regexp);
9754 pm_compile_regexp_dynamic(iseq, node, &((
const pm_interpolated_match_last_line_node_t *) node)->parts, &location, ret,
false, scope_node);
9757 PUSH_INSN1(ret, location, getglobal,
rb_id2sym(idLASTLINE));
9758 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
9759 if (popped) PUSH_INSN(ret, location, pop);
9763 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
9766 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ONCE)) {
9767 pm_compile_regexp_once(iseq, node, &location, ret, scope_node);
9768 if (popped) PUSH_INSN(ret, location, pop);
9772 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9774 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9775 PUSH_INSN1(ret, location, putobject, regexp);
9779 pm_compile_regexp_dynamic(iseq, node, &((
const pm_interpolated_regular_expression_node_t *) node)->parts, &location, ret, popped, scope_node);
9780 if (popped) PUSH_INSN(ret, location, pop);
9785 case PM_INTERPOLATED_STRING_NODE: {
9788 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9790 VALUE string = pm_static_literal_value(iseq, node, scope_node);
9792 if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN)) {
9793 PUSH_INSN1(ret, location, putobject,
string);
9795 else if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) {
9796 PUSH_INSN1(ret, location, dupstring,
string);
9799 PUSH_INSN1(ret, location, dupchilledstring,
string);
9804 const pm_interpolated_string_node_t *
cast = (
const pm_interpolated_string_node_t *) node;
9805 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));
9806 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9807 if (popped) PUSH_INSN(ret, location, pop);
9812 case PM_INTERPOLATED_SYMBOL_NODE: {
9815 const pm_interpolated_symbol_node_t *
cast = (
const pm_interpolated_symbol_node_t *) node;
9816 int length = pm_interpolated_node_compile(iseq, &
cast->parts, &location, ret, popped, scope_node, NULL, NULL,
false,
false);
9819 PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9823 PUSH_INSN(ret, location, intern);
9826 PUSH_INSN(ret, location, pop);
9831 case PM_INTERPOLATED_X_STRING_NODE: {
9834 const pm_interpolated_x_string_node_t *
cast = (
const pm_interpolated_x_string_node_t *) node;
9836 PUSH_INSN(ret, location, putself);
9838 int length = pm_interpolated_node_compile(iseq, &
cast->parts, &location, ret,
false, scope_node, NULL, NULL,
false,
false);
9839 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9841 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
9842 if (popped) PUSH_INSN(ret, location, pop);
9846 case PM_IT_LOCAL_VARIABLE_READ_NODE: {
9850 pm_scope_node_t *current_scope_node = scope_node;
9853 while (current_scope_node) {
9854 if (current_scope_node->parameters && PM_NODE_TYPE_P(current_scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
9855 PUSH_GETLOCAL(ret, location, current_scope_node->local_table_for_iseq_size, level);
9859 current_scope_node = current_scope_node->previous;
9862 rb_bug(
"Local `it` does not exist");
9867 case PM_KEYWORD_HASH_NODE: {
9870 const pm_keyword_hash_node_t *
cast = (
const pm_keyword_hash_node_t *) node;
9871 const pm_node_list_t *elements = &
cast->elements;
9873 bool has_splat =
false;
9874 for (uint32_t index = 0; index < elements->
size; index++) {
9875 if (PM_NODE_TYPE_P(elements->
nodes[index], PM_ASSOC_SPLAT_NODE)) {
9882 pm_compile_hash_elements(iseq, node, elements, 0,
Qundef,
false, ret, scope_node);
9885 const pm_node_t *element;
9887 PM_COMPILE(element);
9890 if (!popped) PUSH_INSN1(ret, location, newhash,
INT2FIX(elements->
size * 2));
9894 case PM_LAMBDA_NODE: {
9897 const pm_lambda_node_t *
cast = (
const pm_lambda_node_t *) node;
9899 pm_scope_node_t next_scope_node;
9900 pm_scope_node_init(node, &next_scope_node, scope_node);
9902 int opening_lineno = pm_location_line_number_cached(&
cast->opening_loc, scope_node);
9903 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
9904 pm_scope_node_destroy(&next_scope_node);
9907 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9908 PUSH_CALL_WITH_BLOCK(ret, location, idLambda, argc, block);
9911 if (popped) PUSH_INSN(ret, location, pop);
9914 case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
9917 const pm_local_variable_and_write_node_t *
cast = (
const pm_local_variable_and_write_node_t *) node;
9918 LABEL *end_label = NEW_LABEL(location.
line);
9920 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node,
cast->name,
cast->depth);
9921 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9922 if (!popped) PUSH_INSN(ret, location, dup);
9924 PUSH_INSNL(ret, location, branchunless, end_label);
9925 if (!popped) PUSH_INSN(ret, location, pop);
9927 PM_COMPILE_NOT_POPPED(
cast->value);
9928 if (!popped) PUSH_INSN(ret, location, dup);
9930 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9931 PUSH_LABEL(ret, end_label);
9935 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
9938 const pm_local_variable_operator_write_node_t *
cast = (
const pm_local_variable_operator_write_node_t *) node;
9940 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node,
cast->name,
cast->depth);
9941 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9943 PM_COMPILE_NOT_POPPED(
cast->value);
9945 ID method_id = pm_constant_id_lookup(scope_node,
cast->binary_operator);
9946 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
9948 if (!popped) PUSH_INSN(ret, location, dup);
9949 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9953 case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
9956 const pm_local_variable_or_write_node_t *
cast = (
const pm_local_variable_or_write_node_t *) node;
9958 LABEL *set_label = NEW_LABEL(location.
line);
9959 LABEL *end_label = NEW_LABEL(location.
line);
9961 PUSH_INSN1(ret, location, putobject,
Qtrue);
9962 PUSH_INSNL(ret, location, branchunless, set_label);
9964 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node,
cast->name,
cast->depth);
9965 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9966 if (!popped) PUSH_INSN(ret, location, dup);
9968 PUSH_INSNL(ret, location, branchif, end_label);
9969 if (!popped) PUSH_INSN(ret, location, pop);
9971 PUSH_LABEL(ret, set_label);
9972 PM_COMPILE_NOT_POPPED(
cast->value);
9973 if (!popped) PUSH_INSN(ret, location, dup);
9975 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9976 PUSH_LABEL(ret, end_label);
9980 case PM_LOCAL_VARIABLE_READ_NODE: {
9984 const pm_local_variable_read_node_t *
cast = (
const pm_local_variable_read_node_t *) node;
9985 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node,
cast->name,
cast->depth);
9986 PUSH_GETLOCAL(ret, location, index.index, index.level);
9991 case PM_LOCAL_VARIABLE_WRITE_NODE: {
9994 const pm_local_variable_write_node_t *
cast = (
const pm_local_variable_write_node_t *) node;
9995 PM_COMPILE_NOT_POPPED(
cast->value);
9996 if (!popped) PUSH_INSN(ret, location, dup);
9998 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node,
cast->name,
cast->depth);
9999 PUSH_SETLOCAL(ret, location, index.index, index.level);
10002 case PM_MATCH_LAST_LINE_NODE: {
10005 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
10007 PUSH_INSN1(ret, location, putobject, regexp);
10009 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
10010 if (popped) PUSH_INSN(ret, location, pop);
10014 case PM_MATCH_PREDICATE_NODE: {
10017 const pm_match_predicate_node_t *
cast = (
const pm_match_predicate_node_t *) node;
10021 PUSH_INSN(ret, location, putnil);
10024 PM_COMPILE_NOT_POPPED(
cast->value);
10025 PUSH_INSN(ret, location, dup);
10029 LABEL *matched_label = NEW_LABEL(location.
line);
10030 LABEL *unmatched_label = NEW_LABEL(location.
line);
10031 LABEL *done_label = NEW_LABEL(location.
line);
10032 pm_compile_pattern(iseq, scope_node,
cast->pattern, ret, matched_label, unmatched_label,
false,
true, 2);
10036 PUSH_LABEL(ret, unmatched_label);
10037 PUSH_INSN(ret, location, pop);
10038 PUSH_INSN(ret, location, pop);
10040 if (!popped) PUSH_INSN1(ret, location, putobject,
Qfalse);
10041 PUSH_INSNL(ret, location, jump, done_label);
10042 PUSH_INSN(ret, location, putnil);
10046 PUSH_LABEL(ret, matched_label);
10047 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(2));
10048 if (!popped) PUSH_INSN1(ret, location, putobject,
Qtrue);
10049 PUSH_INSNL(ret, location, jump, done_label);
10051 PUSH_LABEL(ret, done_label);
10054 case PM_MATCH_REQUIRED_NODE:
10066 pm_compile_match_required_node(iseq, (
const pm_match_required_node_t *) node, &location, ret, popped, scope_node);
10068 case PM_MATCH_WRITE_NODE:
10077 pm_compile_match_write_node(iseq, (
const pm_match_write_node_t *) node, &location, ret, popped, scope_node);
10079 case PM_ERROR_RECOVERY_NODE:
10080 rb_bug(
"A pm_error_recovery_node_t should not exist in prism's AST.");
10082 case PM_MODULE_NODE: {
10085 const pm_module_node_t *
cast = (
const pm_module_node_t *) node;
10087 ID module_id = pm_constant_id_lookup(scope_node,
cast->name);
10088 VALUE module_name =
rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(module_id)));
10090 pm_scope_node_t next_scope_node;
10091 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
10093 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, location.
line);
10094 pm_scope_node_destroy(&next_scope_node);
10096 const int flags = VM_DEFINECLASS_TYPE_MODULE | pm_compile_class_path(iseq,
cast->constant_path, &location, ret,
false, scope_node);
10097 PUSH_INSN(ret, location, putnil);
10098 PUSH_INSN3(ret, location, defineclass,
ID2SYM(module_id), module_iseq,
INT2FIX(flags));
10101 if (popped) PUSH_INSN(ret, location, pop);
10104 case PM_REQUIRED_PARAMETER_NODE: {
10107 const pm_required_parameter_node_t *
cast = (
const pm_required_parameter_node_t *) node;
10108 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node,
cast->name, 0);
10110 PUSH_SETLOCAL(ret, location, index.index, index.level);
10113 case PM_MULTI_WRITE_NODE: {
10122 const pm_multi_write_node_t *
cast = (
const pm_multi_write_node_t *) node;
10124 DECL_ANCHOR(writes);
10125 DECL_ANCHOR(cleanup);
10128 state.position = popped ? 0 : 1;
10129 pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
10131 PM_COMPILE_NOT_POPPED(
cast->value);
10132 if (!popped) PUSH_INSN(ret, location, dup);
10134 PUSH_SEQ(ret, writes);
10135 if (!popped && state.stack_size >= 1) {
10138 PUSH_INSN1(ret, location, setn,
INT2FIX(state.stack_size));
10143 pm_multi_target_state_update(&state);
10145 PUSH_SEQ(ret, cleanup);
10154 pm_compile_next_node(iseq, (
const pm_next_node_t *) node, &location, ret, popped, scope_node);
10156 case PM_NIL_NODE: {
10160 PUSH_INSN(ret, location, putnil);
10165 case PM_NO_BLOCK_PARAMETER_NODE: {
10168 ISEQ_BODY(iseq)->param.flags.accepts_no_block = TRUE;
10171 case PM_NO_KEYWORDS_PARAMETER_NODE: {
10174 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
10177 case PM_NUMBERED_REFERENCE_READ_NODE: {
10181 const pm_numbered_reference_read_node_t *
cast = (
const pm_numbered_reference_read_node_t *) node;
10183 if (
cast->number != 0) {
10184 VALUE ref = pm_compile_numbered_reference_ref(cast);
10185 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), ref);
10188 PUSH_INSN(ret, location, putnil);
10197 pm_compile_or_node(iseq, (
const pm_or_node_t *) node, &location, ret, popped, scope_node);
10199 case PM_OPTIONAL_PARAMETER_NODE: {
10202 const pm_optional_parameter_node_t *
cast = (
const pm_optional_parameter_node_t *) node;
10203 PM_COMPILE_NOT_POPPED(
cast->value);
10205 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node,
cast->name, 0);
10206 PUSH_SETLOCAL(ret, location, index.index, index.level);
10210 case PM_PARENTHESES_NODE: {
10216 const pm_parentheses_node_t *
cast = (
const pm_parentheses_node_t *) node;
10218 if (
cast->body != NULL) {
10219 PM_COMPILE(
cast->body);
10221 else if (!popped) {
10222 PUSH_INSN(ret, location, putnil);
10227 case PM_PRE_EXECUTION_NODE: {
10230 const pm_pre_execution_node_t *
cast = (
const pm_pre_execution_node_t *) node;
10239 DECL_ANCHOR(inner_pre);
10242 DECL_ANCHOR(inner_body);
10244 if (
cast->statements != NULL) {
10245 const pm_node_list_t *body = &
cast->statements->body;
10247 for (uint32_t index = 0; index < body->
size; index++) {
10248 pm_compile_node(iseq, body->
nodes[index], inner_body,
true, scope_node);
10253 PUSH_INSN(inner_body, location, putnil);
10259 PUSH_SEQ(outer_pre, inner_pre);
10260 PUSH_SEQ(outer_pre, inner_body);
10265 case PM_POST_EXECUTION_NODE: {
10268 const rb_iseq_t *child_iseq;
10269 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
10271 pm_scope_node_t next_scope_node;
10272 pm_scope_node_init(node, &next_scope_node, scope_node);
10273 child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
10274 pm_scope_node_destroy(&next_scope_node);
10276 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
10278 int is_index = ISEQ_BODY(iseq)->ise_size++;
10279 PUSH_INSN2(ret, location, once, child_iseq,
INT2FIX(is_index));
10281 if (popped) PUSH_INSN(ret, location, pop);
10283 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
10287 case PM_RANGE_NODE: {
10290 const pm_range_node_t *
cast = (
const pm_range_node_t *) node;
10291 bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
10293 if (pm_optimizable_range_item_p(
cast->left) && pm_optimizable_range_item_p(
cast->right)) {
10295 const pm_node_t *left =
cast->left;
10296 const pm_node_t *right =
cast->right;
10299 (left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((
const pm_integer_node_t *) left) :
Qnil,
10300 (right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((
const pm_integer_node_t *) right) :
Qnil,
10305 PUSH_INSN1(ret, location, putobject, val);
10309 if (
cast->left != NULL) {
10310 PM_COMPILE(
cast->left);
10312 else if (!popped) {
10313 PUSH_INSN(ret, location, putnil);
10316 if (
cast->right != NULL) {
10317 PM_COMPILE(
cast->right);
10319 else if (!popped) {
10320 PUSH_INSN(ret, location, putnil);
10324 PUSH_INSN1(ret, location, newrange,
INT2FIX(exclude_end ? 1 : 0));
10329 case PM_RATIONAL_NODE: {
10333 PUSH_INSN1(ret, location, putobject, parse_rational((
const pm_rational_node_t *) node));
10340 pm_compile_redo_node(iseq, &location, ret, popped, scope_node);
10342 case PM_REGULAR_EXPRESSION_NODE: {
10346 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
10347 PUSH_INSN1(ret, location, putobject, regexp);
10351 case PM_RESCUE_NODE:
10354 pm_compile_rescue_node(iseq, (
const pm_rescue_node_t *) node, &location, ret, popped, scope_node);
10356 case PM_RESCUE_MODIFIER_NODE: {
10359 const pm_rescue_modifier_node_t *
cast = (
const pm_rescue_modifier_node_t *) node;
10361 pm_scope_node_t rescue_scope_node;
10362 pm_scope_node_init((
const pm_node_t *) cast, &rescue_scope_node, scope_node);
10364 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
10365 &rescue_scope_node,
10368 pm_node_line_number_cached(
cast->rescue_expression, scope_node)
10371 pm_scope_node_destroy(&rescue_scope_node);
10373 LABEL *lstart = NEW_LABEL(location.
line);
10374 LABEL *lend = NEW_LABEL(location.
line);
10375 LABEL *lcont = NEW_LABEL(location.
line);
10377 lstart->rescued = LABEL_RESCUE_BEG;
10378 lend->rescued = LABEL_RESCUE_END;
10380 PUSH_LABEL(ret, lstart);
10381 PM_COMPILE_NOT_POPPED(
cast->expression);
10382 PUSH_LABEL(ret, lend);
10384 PUSH_INSN(ret, location, nop);
10385 PUSH_LABEL(ret, lcont);
10386 if (popped) PUSH_INSN(ret, location, pop);
10388 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
10389 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
10392 case PM_RETURN_NODE:
10398 pm_compile_return_node(iseq, (
const pm_return_node_t *) node, &location, ret, popped, scope_node);
10400 case PM_RETRY_NODE: {
10403 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10404 PUSH_INSN(ret, location, putnil);
10405 PUSH_INSN1(ret, location,
throw,
INT2FIX(TAG_RETRY));
10406 if (popped) PUSH_INSN(ret, location, pop);
10409 COMPILE_ERROR(iseq, location.
line,
"Invalid retry");
10414 case PM_SCOPE_NODE:
10415 pm_compile_scope_node(iseq, (pm_scope_node_t *) node, &location, ret, popped);
10417 case PM_SELF_NODE: {
10421 PUSH_INSN(ret, location, putself);
10425 case PM_SHAREABLE_CONSTANT_NODE: {
10428 const pm_shareable_constant_node_t *
cast = (
const pm_shareable_constant_node_t *) node;
10429 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));
10431 switch (PM_NODE_TYPE(
cast->write)) {
10432 case PM_CONSTANT_WRITE_NODE:
10433 pm_compile_constant_write_node(iseq, (
const pm_constant_write_node_t *)
cast->write, shareability, &location, ret, popped, scope_node);
10435 case PM_CONSTANT_AND_WRITE_NODE:
10436 pm_compile_constant_and_write_node(iseq, (
const pm_constant_and_write_node_t *)
cast->write, shareability, &location, ret, popped, scope_node);
10438 case PM_CONSTANT_OR_WRITE_NODE:
10439 pm_compile_constant_or_write_node(iseq, (
const pm_constant_or_write_node_t *)
cast->write, shareability, &location, ret, popped, scope_node);
10441 case PM_CONSTANT_OPERATOR_WRITE_NODE:
10442 pm_compile_constant_operator_write_node(iseq, (
const pm_constant_operator_write_node_t *)
cast->write, shareability, &location, ret, popped, scope_node);
10444 case PM_CONSTANT_PATH_WRITE_NODE:
10445 pm_compile_constant_path_write_node(iseq, (
const pm_constant_path_write_node_t *)
cast->write, shareability, &location, ret, popped, scope_node);
10447 case PM_CONSTANT_PATH_AND_WRITE_NODE:
10448 pm_compile_constant_path_and_write_node(iseq, (
const pm_constant_path_and_write_node_t *)
cast->write, shareability, &location, ret, popped, scope_node);
10450 case PM_CONSTANT_PATH_OR_WRITE_NODE:
10451 pm_compile_constant_path_or_write_node(iseq, (
const pm_constant_path_or_write_node_t *)
cast->write, shareability, &location, ret, popped, scope_node);
10453 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
10454 pm_compile_constant_path_operator_write_node(iseq, (
const pm_constant_path_operator_write_node_t *)
cast->write, shareability, &location, ret, popped, scope_node);
10457 rb_bug(
"Unexpected node type for shareable constant write: %s", pm_node_type(PM_NODE_TYPE(
cast->write)));
10463 case PM_SINGLETON_CLASS_NODE: {
10466 const pm_singleton_class_node_t *
cast = (
const pm_singleton_class_node_t *) node;
10468 pm_scope_node_t next_scope_node;
10469 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
10470 const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit(
"singleton class"), ISEQ_TYPE_CLASS, location.
line);
10471 pm_scope_node_destroy(&next_scope_node);
10473 PM_COMPILE_NOT_POPPED(
cast->expression);
10474 PUSH_INSN(ret, location, putnil);
10477 CONST_ID(singletonclass,
"singletonclass");
10481 int sclass_flags = VM_DEFINECLASS_TYPE_SINGLETON_CLASS;
10482 if (!(PM_NODE_TYPE_P(
cast->expression, PM_SELF_NODE) &&
10483 ISEQ_BODY(iseq)->
type == ISEQ_TYPE_CLASS) &&
10484 !pm_cpath_const_p(
cast->expression)) {
10485 sclass_flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
10488 PUSH_INSN3(ret, location, defineclass,
ID2SYM(singletonclass), child_iseq,
INT2FIX(sclass_flags));
10490 if (popped) PUSH_INSN(ret, location, pop);
10495 case PM_SOURCE_ENCODING_NODE: {
10499 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10500 PUSH_INSN1(ret, location, putobject, value);
10504 case PM_SOURCE_FILE_NODE: {
10508 const pm_source_file_node_t *
cast = (
const pm_source_file_node_t *) node;
10511 if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_FROZEN)) {
10512 string_type = ISEQ_FROZEN_STRING_LITERAL_ENABLED;
10514 else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
10515 string_type = ISEQ_FROZEN_STRING_LITERAL_DISABLED;
10518 string_type = ISEQ_FROZEN_STRING_LITERAL_UNSET;
10521 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10522 PUSH_INSN1(ret, location, putobject,
INT2FIX(string_type));
10523 PUSH_SEND(ret, location, id_core_iseq_path,
INT2FIX(1));
10527 case PM_SOURCE_LINE_NODE: {
10531 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10532 PUSH_INSN1(ret, location, putobject, value);
10536 case PM_SPLAT_NODE: {
10539 const pm_splat_node_t *
cast = (
const pm_splat_node_t *) node;
10540 if (
cast->expression) {
10541 PM_COMPILE(
cast->expression);
10545 PUSH_INSN1(ret, location, splatarray,
Qtrue);
10549 case PM_STATEMENTS_NODE: {
10551 const pm_statements_node_t *
cast = (
const pm_statements_node_t *) node;
10552 const pm_node_list_t *body = &
cast->body;
10554 if (body->
size > 0) {
10555 for (uint32_t index = 0; index < body->
size - 1; index++) {
10556 PM_COMPILE_POPPED(body->
nodes[index]);
10558 PM_COMPILE(body->
nodes[body->
size - 1]);
10561 PUSH_INSN(ret, location, putnil);
10565 case PM_STRING_NODE: {
10569 const pm_string_node_t *
cast = (
const pm_string_node_t *) node;
10570 VALUE value = parse_static_literal_string(iseq, scope_node, node, &
cast->unescaped);
10572 if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FROZEN)) {
10573 PUSH_INSN1(ret, location, putobject, value);
10575 else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
10576 PUSH_INSN1(ret, location, dupstring, value);
10579 PUSH_INSN1(ret, location, dupchilledstring, value);
10584 case PM_SUPER_NODE:
10588 pm_compile_super_node(iseq, (
const pm_super_node_t *) node, &location, ret, popped, scope_node);
10590 case PM_SYMBOL_NODE: {
10594 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10595 PUSH_INSN1(ret, location, putobject, value);
10599 case PM_TRUE_NODE: {
10603 PUSH_INSN1(ret, location, putobject,
Qtrue);
10607 case PM_UNDEF_NODE: {
10610 const pm_undef_node_t *
cast = (
const pm_undef_node_t *) node;
10611 const pm_node_list_t *names = &
cast->names;
10613 for (uint32_t index = 0; index < names->
size; index++) {
10614 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10615 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
10617 PM_COMPILE_NOT_POPPED(names->
nodes[index]);
10618 PUSH_SEND(ret, location, id_core_undef_method,
INT2NUM(2));
10620 if (index < names->size - 1) {
10621 PUSH_INSN(ret, location, pop);
10625 if (popped) PUSH_INSN(ret, location, pop);
10628 case PM_UNLESS_NODE: {
10634 const pm_unless_node_t *
cast = (
const pm_unless_node_t *) node;
10635 const pm_statements_node_t *statements = NULL;
10636 if (
cast->else_clause != NULL) {
10637 statements = ((
const pm_else_node_t *)
cast->else_clause)->statements;
10640 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);
10643 case PM_UNTIL_NODE: {
10649 const pm_until_node_t *
cast = (
const pm_until_node_t *) node;
10650 pm_compile_loop(iseq, &location,
cast->base.flags, PM_UNTIL_NODE, (
const pm_node_t *) cast,
cast->statements,
cast->predicate, ret, popped, scope_node);
10653 case PM_WHILE_NODE: {
10659 const pm_while_node_t *
cast = (
const pm_while_node_t *) node;
10660 pm_compile_loop(iseq, &location,
cast->base.flags, PM_WHILE_NODE, (
const pm_node_t *) cast,
cast->statements,
cast->predicate, ret, popped, scope_node);
10663 case PM_X_STRING_NODE: {
10666 const pm_x_string_node_t *
cast = (
const pm_x_string_node_t *) node;
10667 VALUE value = parse_static_literal_string(iseq, scope_node, node, &
cast->unescaped);
10669 PUSH_INSN(ret, location, putself);
10670 PUSH_INSN1(ret, location, putobject, value);
10671 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
10672 if (popped) PUSH_INSN(ret, location, pop);
10676 case PM_YIELD_NODE:
10682 pm_compile_yield_node(iseq, (
const pm_yield_node_t *) node, &location, ret, popped, scope_node);
10685 rb_raise(
rb_eNotImpError,
"node type %s not implemented", pm_node_type(PM_NODE_TYPE(node)));
10690#undef PM_CONTAINER_P
10694pm_iseq_pre_execution_p(rb_iseq_t *iseq)
10696 switch (ISEQ_BODY(iseq)->
type) {
10697 case ISEQ_TYPE_TOP:
10698 case ISEQ_TYPE_EVAL:
10699 case ISEQ_TYPE_MAIN:
10714pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
10718 if (pm_iseq_pre_execution_p(iseq)) {
10730 pm_compile_node(iseq, (
const pm_node_t *) node, body,
false, node);
10734 PUSH_SEQ(ret, pre);
10735 PUSH_SEQ(ret, body);
10740 pm_compile_node(iseq, (
const pm_node_t *) node, ret,
false, node);
10743 CHECK(iseq_setup_insn(iseq, ret));
10744 return iseq_setup(iseq, ret);
10753 pm_options_line_set(result->
options, 1);
10764 SIZED_FREE_N(result->
node.constants, pm_parser_constants_size(result->
node.parser));
10765 pm_scope_node_destroy(&result->
node);
10769 pm_arena_free(result->
arena);
10771 pm_options_free(result->
options);
10783 if (buffer == NULL) {
10788 if (rb_stderr_tty_p()) {
10789 const char *no_color = getenv(
"NO_COLOR");
10790 if (no_color == NULL || no_color[0] ==
'\0') {
10799 pm_error_level_t error_level = pm_errors_format(parser, buffer, format_type);
10802 VALUE message = rb_enc_str_new(pm_buffer_value(buffer), (
rb_len_t) pm_buffer_length(buffer), message_encoding);
10803 pm_buffer_free(buffer);
10806 switch (error_level) {
10813 VALUE path = rb_enc_str_new((
const char *) pm_string_source(filepath), pm_string_length(filepath), filepath_encoding);
10845 uint64_t checksum = pm_parser_checksum(parser);
10846 return checksum == 0 ? 1 : checksum;
10861 pm_scope_node_t *scope_node = &result->
node;
10863 int coverage_enabled = scope_node->coverage_enabled;
10865 pm_scope_node_init(node, scope_node, NULL);
10868 const char *encoding_name = pm_parser_encoding_name(parser);
10869 rb_encoding *encoding = rb_enc_find(encoding_name);
10870 if (!encoding) rb_bug(
"Encoding not found %s!", encoding_name);
10871 scope_node->encoding = encoding;
10873 scope_node->coverage_enabled = coverage_enabled;
10877 if (script_lines != NULL) {
10881 for (
size_t index = 0; index < line_offsets->
size; index++) {
10882 uint32_t offset = line_offsets->
offsets[index];
10883 uint32_t length = index == line_offsets->
size - 1 ? ((uint32_t) (pm_parser_end(parser) - (pm_parser_start(parser) + offset))) : (line_offsets->
offsets[index + 1] - offset);
10884 rb_ary_push(*script_lines, rb_enc_str_new((
const char *) pm_parser_start(parser) + offset, length, encoding));
10890 const char *filepath = (
const char *) pm_string_source(pm_parser_filepath(parser));
10893 const pm_diagnostic_t *diagnostic = pm_parser_warnings_first(parser);
10894 diagnostic != NULL;
10895 diagnostic = pm_diagnostic_next(diagnostic)
10898 int line = pm_location_line_number(parser, &loc);
10901 rb_enc_compile_warning(encoding, filepath, line,
"%s", pm_diagnostic_message(diagnostic));
10904 rb_enc_compile_warn(encoding, filepath, line,
"%s", pm_diagnostic_message(diagnostic));
10909 if (pm_parser_errors_size(parser) > 0) {
10910 VALUE error = pm_parse_process_error(result);
10919 scope_node->parser = parser;
10920 scope_node->
source_hash = pm_source_hash(parser);
10921 scope_node->options = result->
options;
10922 scope_node->line_offsets = pm_parser_line_offsets(parser);
10923 scope_node->start_line = pm_parser_start_line(parser);
10925 size_t constants_size = pm_parser_constants_size(parser);
10926 ID *constants = constants_size ?
xmalloc(constants_size *
sizeof(
ID)) : NULL;
10927 scope_node->constants = constants;
10929 for (
size_t index = 0; index < constants_size; index++) {
10931 constants[index] = rb_intern3((
const char *) pm_constant_start(constant), pm_constant_length(constant), encoding);
10945pm_options_frozen_string_literal_init(
pm_options_t *options)
10947 int frozen_string_literal = rb_iseq_opt_frozen_string_literal();
10949 switch (frozen_string_literal) {
10950 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
10952 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
10953 pm_options_frozen_string_literal_set(options,
false);
10955 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
10956 pm_options_frozen_string_literal_set(options,
true);
10959 rb_bug(
"pm_options_frozen_string_literal_init: invalid frozen_string_literal=%d", frozen_string_literal);
10969pm_parse_file_script_lines(
const pm_scope_node_t *scope_node,
const pm_parser_t *parser)
10972 const char *start = (
const char *) pm_parser_start(parser);
10973 const char *end = (
const char *) pm_parser_end(parser);
10978 uint32_t last_offset = line_offsets->
offsets[line_offsets->
size - 1];
10979 bool last_push = start + last_offset != end;
10984 for (
size_t index = 0; index < line_offsets->
size - 1; index++) {
10985 uint32_t offset = line_offsets->
offsets[index];
10986 uint32_t length = line_offsets->
offsets[index + 1] - offset;
10988 rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
10993 rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), scope_node->encoding));
11007close_file(
VALUE args)
11010 if (arg->fd != -1) {
11013 else if (!
NIL_P(arg->io)) {
11020load_content(
VALUE args)
11027 return rb_funcall(io, rb_intern(
"read"), 0);
11038 result->
source = pm_source_mapped_new(RSTRING_PTR(filepath), O_RDONLY | O_NONBLOCK, &init_result);
11041 pm_options_frozen_string_literal_init(result->
options);
11052 .open_mode = O_RDONLY | O_NONBLOCK,
11056 if (args.fd == -1)
goto error_generic;
11062 if (
len < 0)
goto error_generic;
11064 size_t length = (size_t)
len;
11065 uint8_t *source_data =
xmalloc(length);
11066 memcpy(source_data, RSTRING_PTR(contents), length);
11067 result->
source = pm_source_owned_new(source_data, length);
11069 pm_options_frozen_string_literal_init(result->
options);
11078 err = rb_w32_map_errno(GetLastError());
11109 pm_options_stack_budget_set(options, rb_ec_stack_remaining(GET_EC()));
11122 pm_options_filepath_set(result->
options, RSTRING_PTR(filepath));
11125 pm_options_version_for_current_ruby_set(result->
options);
11126 pm_options_stack_budget_init(result->
options);
11131 VALUE error = pm_parse_process(result, node, script_lines);
11136 ID id_script_lines = rb_intern(
"SCRIPT_LINES__");
11142 rb_hash_aset(constant_script_lines, filepath, pm_parse_file_script_lines(&result->
node, result->
parser));
11156 VALUE error = pm_load_file(result, filepath,
false);
11157 if (
NIL_P(error)) {
11158 error = pm_parse_file(result, filepath, script_lines);
11173 rb_encoding *encoding = rb_enc_get(source);
11174 if (!rb_enc_asciicompat(encoding)) {
11178 pm_options_frozen_string_literal_init(result->
options);
11179 result->
source = pm_source_constant_new((
const uint8_t *) RSTRING_PTR(source), (
size_t) RSTRING_LEN(source));
11180 pm_options_encoding_set(result->
options, rb_enc_name(encoding));
11183 pm_options_filepath_set(result->
options, RSTRING_PTR(filepath));
11185 pm_options_version_for_current_ruby_set(result->
options);
11186 pm_options_stack_budget_init(result->
options);
11191 VALUE error = pm_parse_process(result, node, script_lines);
11200 const pm_node_t *node;
11204pm_node_find(
const pm_node_t *node,
void *data)
11208 if (node->
node_id == context->node_id) {
11209 context->node = node;
11220pm_source_hash_matches(
VALUE source,
VALUE filepath, uint64_t source_hash)
11223 pm_parse_result_init(&result);
11224 pm_parse_string(&result, source, filepath, NULL);
11226 bool matches = result.
parser != NULL && pm_source_hash(result.
parser) == source_hash;
11227 pm_parse_result_free(&result);
11241pm_node_source_location(
VALUE source,
VALUE filepath,
int start_line,
11242 int node_id, uint64_t source_hash, rb_code_location_t *location)
11245 pm_parse_result_init(&result);
11247 pm_options_line_set(result.
options, start_line);
11248 VALUE error = pm_parse_string(&result, source, filepath, NULL);
11250 if (result.
parser != NULL && pm_source_hash(result.
parser) != source_hash) {
11251 pm_parse_result_free(&result);
11255 if (!
NIL_P(error)) {
11256 pm_parse_result_free(&result);
11261 .node_id = (uint32_t) node_id,
11264 pm_visit_node(result.
node.ast_node, pm_node_find, &context);
11266 bool found = context.node != NULL;
11268 *location = pm_code_location(&result.
node, context.node);
11274 pm_parse_result_free(&result);
11288pm_parse_stdin_eof(
void *stream)
11301#define MAX_ENC_LEN 6
11309pm_parse_stdin_fgets(
char *
string,
int size,
void *stream)
11319 VALUE line = rb_io_gets_limit_internal((
VALUE) stream, size - MAX_ENC_LEN);
11324 const char *cstr = RSTRING_PTR(line);
11325 rb_len_t length = RSTRING_LEN(line);
11333 if (length > (
rb_len_t) (size - 1)) {
11337 memcpy(
string, cstr, (
size_t) length);
11338 string[length] =
'\0';
11346void rb_reset_argf_lineno(
long n);
11356 pm_options_frozen_string_literal_init(result->
options);
11357 pm_options_stack_budget_init(result->
options);
11359 result->
source = pm_source_stream_new((
void *)
rb_stdin, pm_parse_stdin_fgets, pm_parse_stdin_eof);
11364 rb_reset_argf_lineno(0);
11366 return pm_parse_process(result, node, NULL);
11369#define PM_VERSION_FOR_RELEASE(major, minor) PM_VERSION_FOR_RELEASE_IMPL(major, minor)
11370#define PM_VERSION_FOR_RELEASE_IMPL(major, minor) #major "." #minor
11372void pm_options_version_for_current_ruby_set(
pm_options_t *options) {
11374 pm_options_version_set(options, version, strlen(version));
11378#define NEW_ISEQ OLD_ISEQ
11380#undef NEW_CHILD_ISEQ
11381#define NEW_CHILD_ISEQ OLD_CHILD_ISEQ
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_arena_t * pm_arena_new(void)
Returns a newly allocated and initialized arena.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
static PRISM_INLINE size_t pm_constant_id_index(pm_constant_id_t constant_id)
Returns the index of the constant with the given id among the constants of the parser that produced i...
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
pm_error_level_t
The levels of errors generated during parsing.
@ 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_STACK
For errors that should raise a system stack error.
@ PM_ERROR_LEVEL_ENCODING_COMPATIBILITY
For errors that should raise an encoding compatibility 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 SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define LL2NUM
Old name of RB_LL2NUM.
#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 ULL2NUM
Old name of RB_ULL2NUM.
#define FLONUM_P
Old name of RB_FLONUM_P.
#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 ALLOCV_N
Old name of RB_ALLOCV_N.
#define DBL2NUM
Old name of rb_float_new.
#define NUM2LEN
Old name of RB_NUM2LEN.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#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_syserr_new_str(int n, VALUE arg)
Identical to rb_syserr_new(), except it takes the message in Ruby's String instead of C's.
VALUE rb_eNoMemError
NoMemoryError exception.
VALUE rb_eLoadError
LoadError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_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_errinfo(void)
This is the same as $! in Ruby.
VALUE rb_eSysStackError
SystemStackError exception.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
VALUE rb_cArray
Array class.
VALUE rb_cObject
Object class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_stdin
STDIN constant.
VALUE rb_cHash
Hash class.
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
rb_encoding * rb_locale_encoding(void)
Queries the encoding that represents the current locale.
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, rb_len_t len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define INTEGER_PACK_NEGATIVE
Interprets the input as a signed negative number (unpack only).
#define INTEGER_PACK_LSWORD_FIRST
Stores/interprets the least significant word as the first word.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_io_fdopen(int fd, int flags, const char *path)
Creates an IO instance whose backend is the given file descriptor.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
VALUE rb_io_eof(VALUE io)
Queries if the passed IO is at the end of file.
VALUE rb_io_close(VALUE io)
Closes the IO.
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(rb_len_t len)
Allocates a "temporary" string.
#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.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
static VALUE RB_OBJ_SET_FROZEN_SHAREABLE(VALUE obj)
Freezes and marks the object as shareable.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
#define RUBY_API_VERSION_MAJOR
Major version.
#define RUBY_API_VERSION_MINOR
Minor version.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
static PRISM_INLINE uint64_t pm_integer_uint64_value(const pm_integer_t *integer)
Return the magnitude of an integer whose length is at most 2, which always fits in a uint64_t.
static PRISM_INLINE const uint32_t * pm_integer_words(const pm_integer_t *integer, uint32_t *length)
Return the words of an integer, least significant first.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
T * cast(pm_node_t *node) noexcept
Cast the given node, which must not be null and must be of the given struct, to that struct.
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_options_t * pm_options_new(void)
Allocate a new options struct.
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_parser_t * pm_parser_new(pm_arena_t *arena, const uint8_t *source, size_t size, const pm_options_t *options) PRISM_NONNULL(1)
Allocate and initialize a parser with the given start and end pointers.
PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser) PRISM_NONNULL(1)
Free both the memory held by the given parser and the parser itself.
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser) PRISM_NONNULL(1)
Initiate the parser with the given parser.
std::string_view constant(const pm_parser_t *parser PRISM_LIFETIMEBOUND, pm_constant_id_t constant_id) noexcept
The name of the constant with the given id, which must not be zero.
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_buffer_t * pm_buffer_new(void)
Allocate and initialize a new buffer.
pm_visit_result_t
The value returned by a visitor callback, which determines where the walk goes next.
@ PM_VISIT_STOP
End the walk without visiting any more nodes.
@ PM_VISIT_DESCEND
Continue the walk by visiting this node's children.
#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.
The main header file for the prism parser.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
References an object in an array.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define errno
Ractor-aware version of errno.
pm_source_init_result_t
Represents the result of initializing a source from a file.
@ PM_SOURCE_INIT_SUCCESS
Indicates that the source was successfully initialized.
@ PM_SOURCE_INIT_ERROR_NON_REGULAR
Indicates that the file is not a regular file (e.g.
@ PM_SOURCE_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
#define RTEST
This is an old name of RB_TEST.
PM_NODE_ALIGNAS struct pm_node * new_name
AliasGlobalVariableNode::new_name.
PM_NODE_ALIGNAS struct pm_node * old_name
AliasGlobalVariableNode::old_name.
PM_NODE_ALIGNAS struct pm_node * old_name
AliasMethodNode::old_name.
PM_NODE_ALIGNAS struct pm_node * new_name
AliasMethodNode::new_name.
pm_node_t base
The embedded base node.
struct pm_node_list arguments
ArgumentsNode::arguments.
PM_NODE_ALIGNAS struct pm_node * value
AssocNode::value.
PM_NODE_ALIGNAS struct pm_node * key
AssocNode::key.
PM_NODE_ALIGNAS struct pm_node * value
AssocSplatNode::value.
pm_node_t base
The embedded base node.
PM_NODE_ALIGNAS struct pm_arguments_node * arguments
BreakNode::arguments.
pm_location_t closing_loc
CallNode::closing_loc.
pm_constant_id_t name
CallNode::name.
pm_node_t base
The embedded base node.
PM_NODE_ALIGNAS struct pm_arguments_node * arguments
CallNode::arguments.
pm_location_t message_loc
CallNode::message_loc.
PM_NODE_ALIGNAS struct pm_node * block
CallNode::block.
PM_NODE_ALIGNAS struct pm_node * receiver
CallNode::receiver.
pm_constant_id_t read_name
CallOperatorWriteNode::read_name.
pm_constant_id_t binary_operator
CallOperatorWriteNode::binary_operator.
pm_constant_id_t write_name
CallOperatorWriteNode::write_name.
PM_NODE_ALIGNAS struct pm_node * receiver
CallOperatorWriteNode::receiver.
PM_NODE_ALIGNAS struct pm_node * value
CallOperatorWriteNode::value.
struct pm_node_list conditions
CaseMatchNode::conditions.
pm_node_t base
The embedded base node.
PM_NODE_ALIGNAS struct pm_node * predicate
CaseMatchNode::predicate.
PM_NODE_ALIGNAS struct pm_else_node * else_clause
CaseMatchNode::else_clause.
PM_NODE_ALIGNAS struct pm_node * value
ConstantAndWriteNode::value.
pm_location_t name_loc
ConstantAndWriteNode::name_loc.
pm_constant_id_t name
ConstantAndWriteNode::name.
pm_constant_id_t * ids
The constant ids in the list.
uint32_t size
The number of constant ids in the list.
pm_constant_id_t name
ConstantOperatorWriteNode::name.
pm_location_t name_loc
ConstantOperatorWriteNode::name_loc.
pm_constant_id_t binary_operator
ConstantOperatorWriteNode::binary_operator.
PM_NODE_ALIGNAS struct pm_node * value
ConstantOperatorWriteNode::value.
PM_NODE_ALIGNAS struct pm_node * value
ConstantOrWriteNode::value.
pm_location_t name_loc
ConstantOrWriteNode::name_loc.
pm_constant_id_t name
ConstantOrWriteNode::name.
PM_NODE_ALIGNAS struct pm_constant_path_node * target
ConstantPathAndWriteNode::target.
PM_NODE_ALIGNAS struct pm_node * value
ConstantPathAndWriteNode::value.
PM_NODE_ALIGNAS struct pm_node * parent
ConstantPathNode::parent.
pm_constant_id_t name
ConstantPathNode::name.
PM_NODE_ALIGNAS struct pm_constant_path_node * target
ConstantPathOperatorWriteNode::target.
pm_constant_id_t binary_operator
ConstantPathOperatorWriteNode::binary_operator.
PM_NODE_ALIGNAS struct pm_node * value
ConstantPathOperatorWriteNode::value.
PM_NODE_ALIGNAS struct pm_node * value
ConstantPathOrWriteNode::value.
PM_NODE_ALIGNAS struct pm_constant_path_node * target
ConstantPathOrWriteNode::target.
PM_NODE_ALIGNAS struct pm_node * value
ConstantPathWriteNode::value.
PM_NODE_ALIGNAS struct pm_constant_path_node * target
ConstantPathWriteNode::target.
PM_NODE_ALIGNAS struct pm_node * value
ConstantWriteNode::value.
pm_constant_id_t name
ConstantWriteNode::name.
pm_constant_id_t name
DefNode::name.
PM_NODE_ALIGNAS struct pm_parameters_node * parameters
DefNode::parameters.
pm_node_t base
The embedded base node.
PM_NODE_ALIGNAS struct pm_node * receiver
DefNode::receiver.
PM_NODE_ALIGNAS struct pm_node * body
DefNode::body.
pm_constant_id_list_t locals
DefNode::locals.
PM_NODE_ALIGNAS struct pm_statements_node * statements
ElseNode::statements.
PM_NODE_ALIGNAS struct pm_node * left
FlipFlopNode::left.
pm_node_t base
The embedded base node.
PM_NODE_ALIGNAS struct pm_node * right
FlipFlopNode::right.
double value
FloatNode::value.
PM_NODE_ALIGNAS struct pm_block_node * block
ForwardingSuperNode::block.
PM_NODE_ALIGNAS struct pm_node * predicate
IfNode::predicate.
PM_NODE_ALIGNAS struct pm_statements_node * statements
IfNode::statements.
PM_NODE_ALIGNAS struct pm_node * numeric
ImaginaryNode::numeric.
PM_NODE_ALIGNAS struct pm_node * pattern
InNode::pattern.
PM_NODE_ALIGNAS struct pm_statements_node * statements
InNode::statements.
A direct-indexed lookup table mapping constant IDs to local variable indices.
int capacity
Total number of slots (constants_size + PM_INDEX_LOOKUP_SPECIALS).
bool owned
Whether the values array is heap-allocated and needs explicit free.
int * values
Array of local indices, indexed by pm_index_lookup_table_index.
PM_NODE_ALIGNAS struct pm_block_argument_node * block
IndexOperatorWriteNode::block.
PM_NODE_ALIGNAS struct pm_node * receiver
IndexOperatorWriteNode::receiver.
PM_NODE_ALIGNAS struct pm_node * value
IndexOperatorWriteNode::value.
PM_NODE_ALIGNAS struct pm_arguments_node * arguments
IndexOperatorWriteNode::arguments.
pm_constant_id_t binary_operator
IndexOperatorWriteNode::binary_operator.
pm_integer_t value
IntegerNode::value.
A structure represents an arbitrary-sized integer.
bool negative
Whether or not the integer is negative.
struct pm_node_list elements
KeywordHashNode::elements.
A line and column in a string.
uint32_t column
The column in bytes.
int32_t line
The line number.
A list of offsets of the start of lines in a string.
uint32_t * offsets
The list of offsets.
size_t size
The number of offsets in the list.
uint32_t depth
LocalVariableTargetNode::depth.
pm_constant_id_t name
LocalVariableTargetNode::name.
This struct represents a slice in the source code, defined by an offset and a length.
uint32_t start
The offset of the location from the start of the source.
uint32_t length
The length of the location.
PM_NODE_ALIGNAS struct pm_node * pattern
MatchRequiredNode::pattern.
PM_NODE_ALIGNAS struct pm_node * value
MatchRequiredNode::value.
PM_NODE_ALIGNAS struct pm_call_node * call
MatchWriteNode::call.
struct pm_node_list targets
MatchWriteNode::targets.
PM_NODE_ALIGNAS struct pm_node * rest
MultiTargetNode::rest.
struct pm_node_list lefts
MultiTargetNode::lefts.
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...
PM_NODE_ALIGNAS struct pm_arguments_node * arguments
NextNode::arguments.
uint32_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.
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.
uint32_t number
NumberedReferenceReadNode::number.
PM_NODE_ALIGNAS struct pm_node * block
ParametersNode::block.
struct pm_node_list requireds
ParametersNode::requireds.
struct pm_node_list optionals
ParametersNode::optionals.
struct pm_node_list posts
ParametersNode::posts.
pm_node_t base
The embedded base node.
PM_NODE_ALIGNAS struct pm_node * rest
ParametersNode::rest.
struct pm_node_list keywords
ParametersNode::keywords.
PM_NODE_ALIGNAS struct pm_node * keyword_rest
ParametersNode::keyword_rest.
bool parsed
Whether or not this parse result has performed its parsing yet.
pm_parser_t * parser
The parser that will do the actual parsing.
pm_source_t * source
The source backing the parse (file, string, or stream).
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_options_t * options
The options that will be passed to the parser.
pm_arena_t * arena
The arena allocator for AST-lifetime memory.
pm_integer_t denominator
RationalNode::denominator.
pm_integer_t numerator
RationalNode::numerator.
pm_constant_id_t name
RequiredParameterNode::name.
PM_NODE_ALIGNAS struct pm_statements_node * statements
RescueNode::statements.
PM_NODE_ALIGNAS struct pm_rescue_node * subsequent
RescueNode::subsequent.
PM_NODE_ALIGNAS struct pm_node * reference
RescueNode::reference.
struct pm_node_list exceptions
RescueNode::exceptions.
PM_NODE_ALIGNAS struct pm_arguments_node * arguments
ReturnNode::arguments.
uint64_t source_hash
The source hash of the parsed source, propagated to every iseq.
rb_encoding * filepath_encoding
This is the encoding of the actual filepath object that will be used when a FILE node is compiled or ...
pm_index_lookup_table_t index_lookup_table
A flat lookup table mapping constant IDs (or special IDs) to local variable indices.
size_t last_line
Cached line hint for line offset list lookups.
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...
PM_NODE_ALIGNAS struct pm_node * expression
SplatNode::expression.
pm_string_t unescaped
StringNode::unescaped.
A view of a sequence of bytes.
PM_NODE_ALIGNAS struct pm_arguments_node * arguments
SuperNode::arguments.
PM_NODE_ALIGNAS struct pm_node * block
SuperNode::block.
pm_string_t unescaped
SymbolNode::unescaped.
pm_node_t base
The embedded base node.
PM_NODE_ALIGNAS struct pm_arguments_node * arguments
YieldNode::arguments.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
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.