21#define PUSH_ADJUST(seq, location, label) \
22 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (int) (location).line))
24#define PUSH_ADJUST_RESTORE(seq, label) \
25 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
27#define PUSH_INSN(seq, location, insn) \
28 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 0))
30#define PUSH_INSN1(seq, location, insn, op1) \
31 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 1, (VALUE)(op1)))
33#define PUSH_INSN2(seq, location, insn, op1, op2) \
34 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
36#define PUSH_INSN3(seq, location, insn, op1, op2, op3) \
37 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).node_id, BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
39#define PUSH_INSNL(seq, location, insn, label) \
40 (PUSH_INSN1(seq, location, insn, label), LABEL_REF(label))
42#define PUSH_LABEL(seq, label) \
43 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
45#define PUSH_SEND_R(seq, location, id, argc, block, flag, keywords) \
46 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (int) (location).line, (int) (location).node_id, (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
48#define PUSH_SEND(seq, location, id, argc) \
49 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
51#define PUSH_SEND_WITH_FLAG(seq, location, id, argc, flag) \
52 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)(flag), NULL)
54#define PUSH_SEND_WITH_BLOCK(seq, location, id, argc, block) \
55 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
57#define PUSH_CALL(seq, location, id, argc) \
58 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
60#define PUSH_CALL_WITH_BLOCK(seq, location, id, argc, block) \
61 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
63#define PUSH_TRACE(seq, event) \
64 ADD_ELEM((seq), (LINK_ELEMENT *) new_trace_body(iseq, (event), 0))
66#define PUSH_CATCH_ENTRY(type, ls, le, iseqv, lc) \
67 ADD_CATCH_ENTRY((type), (ls), (le), (iseqv), (lc))
69#define PUSH_SEQ(seq1, seq2) \
70 APPEND_LIST((seq1), (seq2))
72#define PUSH_SYNTHETIC_PUTNIL(seq, iseq) \
74 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line; \
75 if (lineno == 0) lineno = FIX2INT(rb_iseq_first_lineno(iseq)); \
76 ADD_SYNTHETIC_INSN(seq, lineno, -1, putnil); \
85pm_iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
int line,
int node_id,
int idx,
int level)
87 if (iseq_local_block_param_p(iseq, idx, level)) {
88 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getblockparam), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
91 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(getlocal), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
93 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qfalse);
97pm_iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *
const seq,
int line,
int node_id,
int idx,
int level)
99 if (iseq_local_block_param_p(iseq, idx, level)) {
100 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setblockparam), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
103 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setlocal), 2,
INT2FIX((idx) + VM_ENV_DATA_SIZE - 1),
INT2FIX(level)));
105 update_lvar_state(iseq, level, idx);
106 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
109#define PUSH_GETLOCAL(seq, location, idx, level) \
110 pm_iseq_add_getlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
112#define PUSH_SETLOCAL(seq, location, idx, level) \
113 pm_iseq_add_setlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
119#define OLD_ISEQ NEW_ISEQ
122#define NEW_ISEQ(node, name, type, line_no) \
123 pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
125#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
128#define NEW_CHILD_ISEQ(node, name, type, line_no) \
129 pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
131#define PM_COMPILE(node) \
132 pm_compile_node(iseq, (node), ret, popped, scope_node)
134#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
135 pm_compile_node(iseq, (node), _ret, popped, scope_node)
137#define PM_COMPILE_POPPED(node) \
138 pm_compile_node(iseq, (node), ret, true, scope_node)
140#define PM_COMPILE_NOT_POPPED(node) \
141 pm_compile_node(iseq, (node), ret, false, scope_node)
143#define PM_NODE_START_LOCATION(parser, node) \
144 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
146#define PM_NODE_END_LOCATION(parser, node) \
147 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
149#define PM_LOCATION_START_LOCATION(parser, location, id) \
150 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, (location)->start, (parser)->start_line), .node_id = id })
152#define PM_NODE_START_LINE_COLUMN(parser, node) \
153 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line)
155#define PM_NODE_END_LINE_COLUMN(parser, node) \
156 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line)
158#define PM_LOCATION_START_LINE_COLUMN(parser, location) \
159 pm_newline_list_line_column(&(parser)->newline_list, (location)->start, (parser)->start_line)
162pm_node_line_number(
const pm_parser_t *parser,
const pm_node_t *node)
180 if (integer->
values == NULL) {
185 unsigned char *bytes = (
unsigned char *) RSTRING_PTR(
string);
187 size_t offset = integer->
length * 8;
188 for (
size_t value_index = 0; value_index < integer->
length; value_index++) {
189 uint32_t value = integer->
values[value_index];
191 for (
int index = 0; index < 8; index++) {
192 int byte = (value >> (4 * index)) & 0xf;
193 bytes[--offset] =
byte < 10 ?
byte +
'0' :
byte - 10 +
'a';
201 result =
rb_funcall(result, rb_intern(
"-@"), 0);
205 RB_OBJ_SET_SHAREABLE(result);
215parse_integer(
const pm_integer_node_t *node)
217 return parse_integer_value(&node->
value);
224parse_float(
const pm_float_node_t *node)
228 RB_OBJ_SET_SHAREABLE(val);
240parse_rational(
const pm_rational_node_t *node)
255parse_imaginary(
const pm_imaginary_node_t *node)
257 VALUE imaginary_part;
258 switch (PM_NODE_TYPE(node->
numeric)) {
259 case PM_FLOAT_NODE: {
260 imaginary_part = parse_float((
const pm_float_node_t *) node->
numeric);
263 case PM_INTEGER_NODE: {
264 imaginary_part = parse_integer((
const pm_integer_node_t *) node->
numeric);
267 case PM_RATIONAL_NODE: {
268 imaginary_part = parse_rational((
const pm_rational_node_t *) node->
numeric);
272 rb_bug(
"Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->
numeric)));
275 return RB_OBJ_SET_SHAREABLE(rb_complex_raw(
INT2FIX(0), imaginary_part));
279parse_string(
const pm_scope_node_t *scope_node,
const pm_string_t *
string)
281 return rb_enc_str_new((
const char *) pm_string_source(
string), pm_string_length(
string), scope_node->encoding);
292 rb_encoding *encoding;
294 if (node->
flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
297 else if (node->
flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
304 return rb_enc_str_new((
const char *) pm_string_source(
string), pm_string_length(
string), encoding);
308parse_static_literal_string(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_string_t *
string)
310 rb_encoding *encoding;
312 if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
315 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
319 encoding = scope_node->encoding;
322 VALUE value = rb_enc_literal_str((
const char *) pm_string_source(
string), pm_string_length(
string), encoding);
325 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
326 int line_number = pm_node_line_number(scope_node->parser, node);
334parse_string_symbol(
const pm_scope_node_t *scope_node,
const pm_symbol_node_t *symbol)
336 rb_encoding *encoding;
337 if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING) {
340 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING) {
343 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING) {
347 encoding = scope_node->encoding;
350 return rb_intern3((
const char *) pm_string_source(&symbol->
unescaped), pm_string_length(&symbol->
unescaped), encoding);
354pm_optimizable_range_item_p(
const pm_node_t *node)
356 return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
361parse_regexp_error(rb_iseq_t *iseq, int32_t line_number,
const char *fmt, ...)
365 VALUE error = rb_syntax_error_append(
Qnil, rb_iseq_path(iseq), line_number, -1, NULL,
"%" PRIsVALUE, args);
371parse_regexp_string_part(rb_iseq_t *iseq,
const 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)
375 rb_encoding *encoding;
377 if (explicit_regexp_encoding != NULL) {
378 encoding = explicit_regexp_encoding;
380 else if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
383 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
387 encoding = implicit_regexp_encoding;
390 VALUE string = rb_enc_str_new((
const char *) pm_string_source(unescaped), pm_string_length(unescaped), encoding);
391 VALUE error = rb_reg_check_preprocess(
string);
393 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, node),
"%" PRIsVALUE,
rb_obj_as_string(error));
398pm_static_literal_concat(rb_iseq_t *iseq,
const pm_node_list_t *nodes,
const pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding,
bool top)
402 for (
size_t index = 0; index < nodes->
size; index++) {
403 const pm_node_t *part = nodes->
nodes[index];
406 switch (PM_NODE_TYPE(part)) {
408 if (implicit_regexp_encoding != NULL) {
410 string = parse_regexp_string_part(iseq, scope_node, part, &((
const pm_string_node_t *) part)->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
413 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
414 VALUE error = rb_reg_check_preprocess(
string);
415 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, part),
"%" PRIsVALUE,
rb_obj_as_string(error));
419 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
422 case PM_INTERPOLATED_STRING_NODE:
423 string = pm_static_literal_concat(iseq, &((
const pm_interpolated_string_node_t *) part)->parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
425 case PM_EMBEDDED_STATEMENTS_NODE: {
426 const pm_embedded_statements_node_t *cast = (
const pm_embedded_statements_node_t *) part;
427 string = pm_static_literal_concat(iseq, &cast->
statements->
body, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
431 RUBY_ASSERT(
false &&
"unexpected node type in pm_static_literal_concat");
435 if (current !=
Qnil) {
443 return top ? rb_fstring(current) : current;
446#define RE_OPTION_ENCODING_SHIFT 8
447#define RE_OPTION_ENCODING(encoding) (((encoding) & 0xFF) << RE_OPTION_ENCODING_SHIFT)
448#define ARG_ENCODING_NONE 32
449#define ARG_ENCODING_FIXED 16
450#define ENC_ASCII8BIT 1
452#define ENC_Windows_31J 3
460parse_regexp_flags(
const pm_node_t *node)
467 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
468 flags |= ARG_ENCODING_NONE;
471 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
472 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_EUC_JP));
475 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
476 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_Windows_31J));
479 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
480 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_UTF8));
483 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) {
484 flags |= ONIG_OPTION_IGNORECASE;
487 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) {
488 flags |= ONIG_OPTION_MULTILINE;
491 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) {
492 flags |= ONIG_OPTION_EXTEND;
498#undef RE_OPTION_ENCODING_SHIFT
499#undef RE_OPTION_ENCODING
500#undef ARG_ENCODING_FIXED
501#undef ARG_ENCODING_NONE
504#undef ENC_Windows_31J
508parse_regexp_encoding(
const pm_scope_node_t *scope_node,
const pm_node_t *node)
510 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_FORCED_BINARY_ENCODING) || PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
513 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
516 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
517 return rb_enc_get_from_index(ENCINDEX_EUC_JP);
519 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
520 return rb_enc_get_from_index(ENCINDEX_Windows_31J);
528parse_regexp(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_node_t *node,
VALUE string)
532 int32_t line_number = pm_node_line_number(scope_node->parser, node);
533 VALUE regexp = rb_reg_compile(
string, parse_regexp_flags(node), (
const char *) pm_string_source(&scope_node->parser->
filepath), line_number);
537 rb_set_errinfo(errinfo);
539 parse_regexp_error(iseq, line_number,
"%" PRIsVALUE, message);
547parse_regexp_literal(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_string_t *unescaped)
549 rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
550 if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
552 VALUE string = rb_enc_str_new((
const char *) pm_string_source(unescaped), pm_string_length(unescaped), regexp_encoding);
553 RB_OBJ_SET_SHAREABLE(
string);
554 return parse_regexp(iseq, scope_node, node,
string);
558parse_regexp_concat(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_node_list_t *parts)
560 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
561 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
563 VALUE string = pm_static_literal_concat(iseq, parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
564 return parse_regexp(iseq, scope_node, node,
string);
567static 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);
570pm_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)
573 size_t parts_size = parts->
size;
574 bool interpolated =
false;
576 if (parts_size > 0) {
580 for (
size_t index = 0; index < parts_size; index++) {
581 const pm_node_t *part = parts->
nodes[index];
583 if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
584 const pm_string_node_t *string_node = (
const pm_string_node_t *) part;
587 if (implicit_regexp_encoding == NULL) {
588 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
591 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
594 if (
RTEST(current_string)) {
595 current_string =
rb_str_concat(current_string, string_value);
598 current_string = string_value;
599 if (index != 0) current_location = PM_NODE_END_LOCATION(scope_node->parser, part);
606 PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE) &&
607 ((
const pm_embedded_statements_node_t *) part)->statements != NULL &&
608 ((
const pm_embedded_statements_node_t *) part)->statements->body.size == 1 &&
609 PM_NODE_TYPE_P(((
const pm_embedded_statements_node_t *) part)->statements->body.nodes[0], PM_STRING_NODE)
611 const pm_string_node_t *string_node = (
const pm_string_node_t *) ((
const pm_embedded_statements_node_t *) part)->statements->body.nodes[0];
614 if (implicit_regexp_encoding == NULL) {
615 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
618 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
621 if (
RTEST(current_string)) {
622 current_string =
rb_str_concat(current_string, string_value);
625 current_string = string_value;
626 current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
630 if (!
RTEST(current_string)) {
631 rb_encoding *encoding;
633 if (implicit_regexp_encoding != NULL) {
634 if (explicit_regexp_encoding != NULL) {
635 encoding = explicit_regexp_encoding;
641 encoding = implicit_regexp_encoding;
645 encoding = scope_node->encoding;
648 if (parts_size == 1) {
649 current_string = rb_enc_str_new(NULL, 0, encoding);
653 if (
RTEST(current_string)) {
654 VALUE operand = rb_fstring(current_string);
655 PUSH_INSN1(ret, current_location, putobject, operand);
659 PM_COMPILE_NOT_POPPED(part);
661 const pm_node_location_t current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
662 PUSH_INSN(ret, current_location, dup);
665 const struct rb_callinfo *callinfo = new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE);
666 PUSH_INSN1(ret, current_location, objtostring, callinfo);
669 PUSH_INSN(ret, current_location, anytostring);
671 current_string =
Qnil;
677 if (
RTEST(current_string)) {
678 current_string = rb_fstring(current_string);
680 if (stack_size == 0) {
682 PUSH_INSN1(ret, current_location, putobject, current_string);
683 }
else if (mutable_result || interpolated) {
684 PUSH_INSN1(ret, current_location, putstring, current_string);
686 PUSH_INSN1(ret, current_location, putchilledstring, current_string);
689 PUSH_INSN1(ret, current_location, putobject, current_string);
692 current_string =
Qnil;
697 PUSH_INSN(ret, *node_location, putnil);
704pm_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)
706 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
707 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
709 int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false,
false);
710 PUSH_INSN2(ret, *node_location, toregexp,
INT2FIX(parse_regexp_flags(node) & 0xFF),
INT2FIX(length));
714pm_source_file_value(
const pm_source_file_node_t *node,
const pm_scope_node_t *scope_node)
717 size_t length = pm_string_length(filepath);
721 return rb_enc_interned_str((
const char *) pm_string_source(filepath), length, filepath_encoding);
724 return rb_fstring_lit(
"<compiled>");
733pm_static_literal_string(rb_iseq_t *iseq,
VALUE string,
int line_number)
735 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
736 VALUE str = rb_str_with_debug_created_info(
string, rb_iseq_path(iseq), line_number);
737 RB_OBJ_SET_SHAREABLE(str);
741 return rb_fstring(
string);
751pm_static_literal_value(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_scope_node_t *scope_node)
755 RUBY_ASSERT(PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL));
757 switch (PM_NODE_TYPE(node)) {
758 case PM_ARRAY_NODE: {
759 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
760 const pm_node_list_t *elements = &cast->
elements;
763 for (
size_t index = 0; index < elements->
size; index++) {
764 rb_ary_push(value, pm_static_literal_value(iseq, elements->
nodes[index], scope_node));
767 RB_OBJ_SET_FROZEN_SHAREABLE(value);
773 return parse_float((
const pm_float_node_t *) node);
775 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
776 const pm_node_list_t *elements = &cast->
elements;
779 for (
size_t index = 0; index < elements->
size; index++) {
781 const pm_assoc_node_t *cast = (
const pm_assoc_node_t *) elements->
nodes[index];
782 VALUE pair[2] = { pm_static_literal_value(iseq, cast->key, scope_node), pm_static_literal_value(iseq, cast->value, scope_node) };
786 VALUE value = rb_hash_new_with_size(elements->
size);
790 RB_OBJ_SET_FROZEN_SHAREABLE(value);
793 case PM_IMAGINARY_NODE:
794 return parse_imaginary((
const pm_imaginary_node_t *) node);
795 case PM_INTEGER_NODE:
796 return parse_integer((
const pm_integer_node_t *) node);
797 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
798 const pm_interpolated_match_last_line_node_t *cast = (
const pm_interpolated_match_last_line_node_t *) node;
799 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &cast->parts);
801 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
802 const pm_interpolated_regular_expression_node_t *cast = (
const pm_interpolated_regular_expression_node_t *) node;
803 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &cast->parts);
805 case PM_INTERPOLATED_STRING_NODE: {
806 VALUE string = pm_static_literal_concat(iseq, &((
const pm_interpolated_string_node_t *) node)->parts, scope_node, NULL, NULL,
false);
807 int line_number = pm_node_line_number(scope_node->parser, node);
808 return pm_static_literal_string(iseq,
string, line_number);
810 case PM_INTERPOLATED_SYMBOL_NODE: {
811 const pm_interpolated_symbol_node_t *cast = (
const pm_interpolated_symbol_node_t *) node;
812 VALUE string = pm_static_literal_concat(iseq, &cast->
parts, scope_node, NULL, NULL,
true);
814 return ID2SYM(rb_intern_str(
string));
816 case PM_MATCH_LAST_LINE_NODE: {
817 const pm_match_last_line_node_t *cast = (
const pm_match_last_line_node_t *) node;
818 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &cast->unescaped);
822 case PM_RATIONAL_NODE:
823 return parse_rational((
const pm_rational_node_t *) node);
824 case PM_REGULAR_EXPRESSION_NODE: {
825 const pm_regular_expression_node_t *cast = (
const pm_regular_expression_node_t *) node;
826 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &cast->unescaped);
828 case PM_SOURCE_ENCODING_NODE:
829 return rb_enc_from_encoding(scope_node->encoding);
830 case PM_SOURCE_FILE_NODE: {
831 const pm_source_file_node_t *cast = (
const pm_source_file_node_t *) node;
832 return pm_source_file_value(cast, scope_node);
834 case PM_SOURCE_LINE_NODE:
835 return INT2FIX(pm_node_line_number(scope_node->parser, node));
836 case PM_STRING_NODE: {
837 const pm_string_node_t *cast = (
const pm_string_node_t *) node;
838 return parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
841 return ID2SYM(parse_string_symbol(scope_node, (
const pm_symbol_node_t *) node));
845 rb_bug(
"Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
853static rb_code_location_t
854pm_code_location(
const pm_scope_node_t *scope_node,
const pm_node_t *node)
856 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
857 const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, node);
859 return (rb_code_location_t) {
860 .beg_pos = { .lineno = start_location.
line, .column = start_location.
column },
861 .end_pos = { .lineno = end_location.
line, .column = end_location.
column }
870#define PM_BRANCH_COVERAGE_P(iseq) (ISEQ_COVERAGE(iseq) && ISEQ_BRANCH_COVERAGE(iseq))
873pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const pm_node_t *cond,
874 LABEL *then_label, LABEL *else_label, pm_scope_node_t *scope_node);
877pm_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)
883 LABEL *label = NEW_LABEL(location.
line);
884 if (!then_label) then_label = label;
885 else if (!else_label) else_label = label;
887 pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, scope_node);
889 if (LIST_INSN_SIZE_ONE(seq)) {
890 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
891 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
return;
895 PUSH_LABEL(seq, label);
903pm_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)
907 if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
908 PM_COMPILE_NOT_POPPED(node);
911 PUSH_INSN1(ret, location, getglobal, operand);
913 PUSH_SEND(ret, location, idEq,
INT2FIX(1));
914 if (popped) PUSH_INSN(ret, location, pop);
922pm_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)
925 LABEL *lend = NEW_LABEL(location.
line);
927 int again = !(flip_flop_node->
base.
flags & PM_RANGE_FLAGS_EXCLUDE_END);
929 rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
932 PUSH_INSN2(ret, location, getspecial, key,
INT2FIX(0));
933 PUSH_INSNL(ret, location, branchif, lend);
935 if (flip_flop_node->
left) {
936 pm_compile_flip_flop_bound(iseq, flip_flop_node->
left, ret, popped, scope_node);
939 PUSH_INSN(ret, location, putnil);
942 PUSH_INSNL(ret, location, branchunless, else_label);
943 PUSH_INSN1(ret, location, putobject,
Qtrue);
944 PUSH_INSN1(ret, location, setspecial, key);
946 PUSH_INSNL(ret, location, jump, then_label);
949 PUSH_LABEL(ret, lend);
950 if (flip_flop_node->
right) {
951 pm_compile_flip_flop_bound(iseq, flip_flop_node->
right, ret, popped, scope_node);
954 PUSH_INSN(ret, location, putnil);
957 PUSH_INSNL(ret, location, branchunless, then_label);
958 PUSH_INSN1(ret, location, putobject,
Qfalse);
959 PUSH_INSN1(ret, location, setspecial, key);
960 PUSH_INSNL(ret, location, jump, then_label);
963static 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);
966pm_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)
971 switch (PM_NODE_TYPE(cond)) {
973 const pm_and_node_t *cast = (
const pm_and_node_t *) cond;
974 pm_compile_logical(iseq, ret, cast->
left, NULL, else_label, scope_node);
980 const pm_or_node_t *cast = (
const pm_or_node_t *) cond;
981 pm_compile_logical(iseq, ret, cast->
left, then_label, NULL, scope_node);
988 PUSH_INSNL(ret, location, jump, else_label);
991 case PM_IMAGINARY_NODE:
992 case PM_INTEGER_NODE:
994 case PM_RATIONAL_NODE:
995 case PM_REGULAR_EXPRESSION_NODE:
999 PUSH_INSNL(ret, location, jump, then_label);
1001 case PM_FLIP_FLOP_NODE:
1002 pm_compile_flip_flop((
const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.
line, ret,
false, scope_node);
1004 case PM_DEFINED_NODE: {
1005 const pm_defined_node_t *cast = (
const pm_defined_node_t *) cond;
1006 pm_compile_defined_expr(iseq, cast->
value, &location, ret,
false, scope_node,
true);
1010 DECL_ANCHOR(cond_seq);
1011 pm_compile_node(iseq, cond, cond_seq,
false, scope_node);
1013 if (LIST_INSN_SIZE_ONE(cond_seq)) {
1014 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
1016 if (insn->insn_id == BIN(putobject)) {
1017 if (
RTEST(insn->operands[0])) {
1018 PUSH_INSNL(ret, location, jump, then_label);
1023 PUSH_INSNL(ret, location, jump, else_label);
1029 PUSH_SEQ(ret, cond_seq);
1034 PUSH_INSNL(ret, location, branchunless, else_label);
1035 PUSH_INSNL(ret, location, jump, then_label);
1042pm_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)
1045 LABEL *then_label = NEW_LABEL(location.
line);
1046 LABEL *else_label = NEW_LABEL(location.
line);
1047 LABEL *end_label = NULL;
1049 DECL_ANCHOR(cond_seq);
1050 pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, scope_node);
1051 PUSH_SEQ(ret, cond_seq);
1053 rb_code_location_t conditional_location = { 0 };
1056 if (then_label->refcnt && else_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1057 conditional_location = pm_code_location(scope_node, node);
1058 branches = decl_branch_base(iseq, PTR2NUM(node), &conditional_location,
type == PM_IF_NODE ?
"if" :
"unless");
1061 if (then_label->refcnt) {
1062 PUSH_LABEL(ret, then_label);
1064 DECL_ANCHOR(then_seq);
1066 if (statements != NULL) {
1067 pm_compile_node(iseq, (
const pm_node_t *) statements, then_seq, popped, scope_node);
1070 PUSH_SYNTHETIC_PUTNIL(then_seq, iseq);
1073 if (else_label->refcnt) {
1075 if (PM_BRANCH_COVERAGE_P(iseq)) {
1076 rb_code_location_t branch_location;
1078 if (statements != NULL) {
1079 branch_location = pm_code_location(scope_node, (
const pm_node_t *) statements);
1080 }
else if (
type == PM_IF_NODE) {
1081 pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(scope_node->parser, predicate);
1082 branch_location = (rb_code_location_t) {
1083 .beg_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column },
1084 .end_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column }
1087 branch_location = conditional_location;
1090 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
type == PM_IF_NODE ?
"then" :
"else", branches);
1093 end_label = NEW_LABEL(location.
line);
1094 PUSH_INSNL(then_seq, location, jump, end_label);
1095 if (!popped) PUSH_INSN(then_seq, location, pop);
1098 PUSH_SEQ(ret, then_seq);
1101 if (else_label->refcnt) {
1102 PUSH_LABEL(ret, else_label);
1104 DECL_ANCHOR(else_seq);
1106 if (subsequent != NULL) {
1107 pm_compile_node(iseq, subsequent, else_seq, popped, scope_node);
1110 PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
1114 if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1115 rb_code_location_t branch_location;
1117 if (subsequent == NULL) {
1118 branch_location = conditional_location;
1119 }
else if (PM_NODE_TYPE_P(subsequent, PM_ELSE_NODE)) {
1120 const pm_else_node_t *else_node = (
const pm_else_node_t *) subsequent;
1121 branch_location = pm_code_location(scope_node, else_node->
statements != NULL ? ((
const pm_node_t *) else_node->
statements) : (
const pm_node_t *) else_node);
1123 branch_location = pm_code_location(scope_node, (
const pm_node_t *) subsequent);
1126 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1,
type == PM_IF_NODE ?
"else" :
"then", branches);
1129 PUSH_SEQ(ret, else_seq);
1133 PUSH_LABEL(ret, end_label);
1143pm_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)
1147 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
1148 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
1149 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
1151 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.
line);
1152 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.
line);
1153 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.
line);
1154 LABEL *end_label = NEW_LABEL(location.
line);
1155 LABEL *adjust_label = NEW_LABEL(location.
line);
1157 LABEL *next_catch_label = NEW_LABEL(location.
line);
1158 LABEL *tmp_label = NULL;
1164 push_ensure_entry(iseq, &enl, NULL, NULL);
1167 if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
1168 tmp_label = NEW_LABEL(location.
line);
1169 PUSH_INSNL(ret, location, jump, tmp_label);
1173 PUSH_INSNL(ret, location, jump, next_label);
1176 PUSH_LABEL(ret, adjust_label);
1177 PUSH_INSN(ret, location, putnil);
1178 PUSH_LABEL(ret, next_catch_label);
1179 PUSH_INSN(ret, location, pop);
1180 PUSH_INSNL(ret, location, jump, next_label);
1181 if (tmp_label) PUSH_LABEL(ret, tmp_label);
1183 PUSH_LABEL(ret, redo_label);
1186 if (PM_BRANCH_COVERAGE_P(iseq)) {
1187 rb_code_location_t loop_location = pm_code_location(scope_node, node);
1188 VALUE branches = decl_branch_base(iseq, PTR2NUM(node), &loop_location,
type == PM_WHILE_NODE ?
"while" :
"until");
1190 rb_code_location_t branch_location = statements != NULL ? pm_code_location(scope_node, (
const pm_node_t *) statements) : loop_location;
1191 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
"body", branches);
1194 if (statements != NULL) PM_COMPILE_POPPED((
const pm_node_t *) statements);
1195 PUSH_LABEL(ret, next_label);
1197 if (
type == PM_WHILE_NODE) {
1198 pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, scope_node);
1200 else if (
type == PM_UNTIL_NODE) {
1201 pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, scope_node);
1204 PUSH_LABEL(ret, end_label);
1205 PUSH_ADJUST_RESTORE(ret, adjust_label);
1206 PUSH_INSN(ret, location, putnil);
1208 PUSH_LABEL(ret, break_label);
1209 if (popped) PUSH_INSN(ret, location, pop);
1211 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
1212 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
1213 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL, ISEQ_COMPILE_DATA(iseq)->redo_label);
1215 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
1216 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
1217 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
1218 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
1226static pm_local_index_t
1227pm_lookup_local_index(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
pm_constant_id_t constant_id,
int start_depth)
1229 pm_local_index_t lindex = { 0 };
1230 st_data_t local_index;
1233 for (level = 0; level < start_depth; level++) {
1234 scope_node = scope_node->previous;
1237 while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
1240 if (scope_node->previous) {
1241 scope_node = scope_node->previous;
1246 rb_bug(
"Local with constant_id %u does not exist", (
unsigned int) constant_id);
1250 lindex.level = level;
1251 lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
1262pm_constant_id_lookup(
const pm_scope_node_t *scope_node,
pm_constant_id_t constant_id)
1265 rb_bug(
"constant_id out of range: %u", (
unsigned int)constant_id);
1267 return scope_node->constants[constant_id - 1];
1271pm_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)
1273 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1274 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1276 rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
1277 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1279 isolated_depth ? isolated_depth + 1 : 0,
1280 type, ISEQ_COMPILE_DATA(iseq)->option, &error_state);
1283 pm_scope_node_destroy(node);
1285 rb_jump_tag(error_state);
1287 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1292pm_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)
1294 if (PM_NODE_TYPE_P(node, PM_CONSTANT_PATH_NODE)) {
1295 const pm_node_t *parent = ((
const pm_constant_path_node_t *) node)->parent;
1300 return VM_DEFINECLASS_FLAG_SCOPED;
1304 PUSH_INSN1(ret, *node_location, putobject, rb_cObject);
1305 return VM_DEFINECLASS_FLAG_SCOPED;
1310 PUSH_INSN1(ret, *node_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
1320pm_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)
1323 LABEL *lfin = NEW_LABEL(location.
line);
1324 LABEL *lcfin = NEW_LABEL(location.
line);
1325 LABEL *lskip = NULL;
1327 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1328 ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
1330 PM_COMPILE_NOT_POPPED(receiver);
1332 lskip = NEW_LABEL(location.
line);
1333 PUSH_INSN(ret, location, dup);
1334 PUSH_INSNL(ret, location, branchnil, lskip);
1337 PUSH_INSN(ret, location, dup);
1338 PUSH_SEND_WITH_FLAG(ret, location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
1339 if (!popped) PUSH_INSN(ret, location, dup);
1342 PUSH_INSNL(ret, location, branchunless, lcfin);
1345 PUSH_INSNL(ret, location, branchif, lcfin);
1348 if (!popped) PUSH_INSN(ret, location, pop);
1349 PM_COMPILE_NOT_POPPED(value);
1352 PUSH_INSN(ret, location, swap);
1353 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
1356 ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
1357 PUSH_SEND_WITH_FLAG(ret, location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
1358 PUSH_INSNL(ret, location, jump, lfin);
1360 PUSH_LABEL(ret, lcfin);
1361 if (!popped) PUSH_INSN(ret, location, swap);
1363 PUSH_LABEL(ret, lfin);
1365 if (lskip && popped) PUSH_LABEL(ret, lskip);
1366 PUSH_INSN(ret, location, pop);
1367 if (lskip && !popped) PUSH_LABEL(ret, lskip);
1370static 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);
1378pm_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)
1380 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
1386 const int max_stack_length = 0x100;
1387 const unsigned int min_tmp_hash_length = 0x800;
1389 int stack_length = 0;
1390 bool first_chunk =
true;
1396 bool static_literal =
false;
1398 DECL_ANCHOR(anchor);
1401#define FLUSH_CHUNK \
1402 if (stack_length) { \
1403 if (first_chunk) { \
1404 PUSH_SEQ(ret, anchor); \
1405 PUSH_INSN1(ret, location, newhash, INT2FIX(stack_length)); \
1406 first_chunk = false; \
1409 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
1410 PUSH_INSN(ret, location, swap); \
1411 PUSH_SEQ(ret, anchor); \
1412 PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(stack_length + 1)); \
1414 INIT_ANCHOR(anchor); \
1418 for (
size_t index = 0; index < elements->
size; index++) {
1419 const pm_node_t *element = elements->
nodes[index];
1421 switch (PM_NODE_TYPE(element)) {
1422 case PM_ASSOC_NODE: {
1425 (shareability == 0) &&
1426 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && (
1427 (!static_literal && ((index + min_tmp_hash_length) < elements->
size)) ||
1428 (first_chunk && stack_length == 0)
1433 while (index + count < elements->size && PM_NODE_FLAG_P(elements->
nodes[index + count], PM_NODE_FLAG_STATIC_LITERAL)) count++;
1435 if ((first_chunk && stack_length == 0) || count >= min_tmp_hash_length) {
1441 for (
size_t tmp_end = index + count; index < tmp_end; index++) {
1442 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[index];
1445 pm_static_literal_value(iseq, assoc->key, scope_node),
1446 pm_static_literal_value(iseq, assoc->value, scope_node)
1456 RB_OBJ_SET_FROZEN_SHAREABLE(hash);
1461 PUSH_INSN1(ret, location, duphash, hash);
1462 first_chunk =
false;
1465 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1466 PUSH_INSN(ret, location, swap);
1467 PUSH_INSN1(ret, location, putobject, hash);
1468 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1474 static_literal =
true;
1478 static_literal =
false;
1483 if (shareability == 0) {
1484 pm_compile_node(iseq, element, anchor,
false, scope_node);
1487 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
1488 pm_compile_shareable_constant_value(iseq, assoc->
key, shareability, path, ret, scope_node,
false);
1489 pm_compile_shareable_constant_value(iseq, assoc->
value, shareability, path, ret, scope_node,
false);
1492 if ((stack_length += 2) >= max_stack_length) FLUSH_CHUNK;
1495 case PM_ASSOC_SPLAT_NODE: {
1498 const pm_assoc_splat_node_t *assoc_splat = (
const pm_assoc_splat_node_t *) element;
1499 bool empty_hash = assoc_splat->
value != NULL && (
1500 (PM_NODE_TYPE_P(assoc_splat->
value, PM_HASH_NODE) && ((
const pm_hash_node_t *) assoc_splat->
value)->elements.size == 0) ||
1501 PM_NODE_TYPE_P(assoc_splat->
value, PM_NIL_NODE)
1504 bool first_element = first_chunk && stack_length == 0;
1505 bool last_element = index == elements->
size - 1;
1506 bool only_element = first_element && last_element;
1509 if (only_element && argument) {
1516 PUSH_INSN(ret, location, putnil);
1518 else if (first_element) {
1521 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1528 if (only_element && argument) {
1534 if (shareability == 0) {
1535 PM_COMPILE_NOT_POPPED(element);
1538 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1546 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1548 if (first_element) {
1549 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1552 PUSH_INSN(ret, location, swap);
1555 if (shareability == 0) {
1556 PM_COMPILE_NOT_POPPED(element);
1559 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1562 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1566 first_chunk =
false;
1567 static_literal =
false;
1571 RUBY_ASSERT(
"Invalid node type for hash" &&
false);
1580#define SPLATARRAY_FALSE 0
1581#define SPLATARRAY_TRUE 1
1582#define DUP_SINGLE_KW_SPLAT 2
1586pm_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)
1591 bool has_splat =
false;
1592 bool has_keyword_splat =
false;
1594 if (arguments_node == NULL) {
1595 if (*flags & VM_CALL_FCALL) {
1596 *flags |= VM_CALL_VCALL;
1600 const pm_node_list_t *arguments = &arguments_node->
arguments;
1601 has_keyword_splat = PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
1605 int post_splat_counter = 0;
1606 const pm_node_t *argument;
1609 switch (PM_NODE_TYPE(argument)) {
1611 case PM_KEYWORD_HASH_NODE: {
1612 const pm_keyword_hash_node_t *keyword_arg = (
const pm_keyword_hash_node_t *) argument;
1613 const pm_node_list_t *elements = &keyword_arg->
elements;
1615 if (has_keyword_splat || has_splat) {
1616 *flags |= VM_CALL_KW_SPLAT;
1617 has_keyword_splat =
true;
1619 if (elements->
size > 1 || !(elements->
size == 1 && PM_NODE_TYPE_P(elements->
nodes[0], PM_ASSOC_SPLAT_NODE))) {
1623 *flags |= VM_CALL_KW_SPLAT_MUT;
1624 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1626 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
1627 *flags |= VM_CALL_KW_SPLAT_MUT;
1628 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1629 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1630 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1631 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1634 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1640 if (PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS)) {
1649 for (
size_t element_index = 0; element_index < elements->
size; element_index++) {
1650 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[element_index];
1654 VALUE keyword = pm_static_literal_value(iseq, assoc->key, scope_node);
1655 VALUE stored_index = rb_hash_aref(stored_indices, keyword);
1660 if (!
NIL_P(stored_index)) {
1668 rb_hash_aset(stored_indices, keyword,
ULONG2NUM(element_index));
1674 *flags |= VM_CALL_KWARG;
1676 VALUE *keywords = (*kw_arg)->keywords;
1677 (*kw_arg)->references = 0;
1678 (*kw_arg)->keyword_len = (int) size;
1680 size_t keyword_index = 0;
1681 for (
size_t element_index = 0; element_index < elements->
size; element_index++) {
1682 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[element_index];
1686 keywords[keyword_index++] = pm_static_literal_value(iseq, assoc->key, scope_node);
1690 PM_COMPILE(assoc->value);
1699 *flags |= VM_CALL_KW_SPLAT;
1701 size_t size = elements->
size;
1706 *flags |= VM_CALL_KW_SPLAT_MUT;
1709 for (
size_t element_index = 0; element_index < size; element_index++) {
1710 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[element_index];
1711 PM_COMPILE_NOT_POPPED(assoc->key);
1712 PM_COMPILE_NOT_POPPED(assoc->value);
1715 PUSH_INSN1(ret, location, newhash,
INT2FIX(size * 2));
1720 case PM_SPLAT_NODE: {
1721 *flags |= VM_CALL_ARGS_SPLAT;
1722 const pm_splat_node_t *splat_node = (
const pm_splat_node_t *) argument;
1725 PM_COMPILE_NOT_POPPED(splat_node->
expression);
1728 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1729 PUSH_GETLOCAL(ret, location, index.index, index.level);
1732 bool first_splat = !has_splat;
1740 if (index + 1 < arguments->
size || has_regular_blockarg) {
1741 PUSH_INSN1(ret, location, splatarray, (*dup_rest & SPLATARRAY_TRUE) ?
Qtrue :
Qfalse);
1742 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
1750 PUSH_INSN1(ret, location, splatarray,
Qfalse);
1760 PUSH_INSN(ret, location, concattoarray);
1764 post_splat_counter = 0;
1768 case PM_FORWARDING_ARGUMENTS_NODE: {
1769 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
1771 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
1772 *flags |= VM_CALL_FORWARDING;
1774 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
1775 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1787 *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
1794 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1795 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1799 PUSH_INSN(ret, location, concattoarray);
1801 PUSH_INSN1(ret, location, splatarray,
Qfalse);
1805 pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
1806 PUSH_GETLOCAL(ret, location, pow_local.index, pow_local.level);
1809 pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
1810 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(and_local.level));
1815 post_splat_counter++;
1816 PM_COMPILE_NOT_POPPED(argument);
1840 if (index == arguments->
size - 1) {
1842 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(post_splat_counter));
1845 pm_node_t *next_arg = arguments->
nodes[index + 1];
1847 switch (PM_NODE_TYPE(next_arg)) {
1849 case PM_KEYWORD_HASH_NODE: {
1850 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1851 PUSH_INSN(ret, location, concatarray);
1854 case PM_SPLAT_NODE: {
1855 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1856 PUSH_INSN(ret, location, concatarray);
1859 case PM_FORWARDING_ARGUMENTS_NODE: {
1860 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(post_splat_counter));
1876 if (has_splat) orig_argc++;
1877 if (has_keyword_splat) orig_argc++;
1886pm_setup_args_dup_rest_p(
const pm_node_t *node)
1888 switch (PM_NODE_TYPE(node)) {
1889 case PM_BACK_REFERENCE_READ_NODE:
1890 case PM_CLASS_VARIABLE_READ_NODE:
1891 case PM_CONSTANT_READ_NODE:
1894 case PM_GLOBAL_VARIABLE_READ_NODE:
1895 case PM_IMAGINARY_NODE:
1896 case PM_INSTANCE_VARIABLE_READ_NODE:
1897 case PM_INTEGER_NODE:
1898 case PM_LAMBDA_NODE:
1899 case PM_LOCAL_VARIABLE_READ_NODE:
1901 case PM_NUMBERED_REFERENCE_READ_NODE:
1902 case PM_RATIONAL_NODE:
1903 case PM_REGULAR_EXPRESSION_NODE:
1905 case PM_STRING_NODE:
1906 case PM_SYMBOL_NODE:
1909 case PM_CONSTANT_PATH_NODE: {
1910 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) node;
1911 if (cast->
parent != NULL) {
1912 return pm_setup_args_dup_rest_p(cast->
parent);
1916 case PM_IMPLICIT_NODE:
1917 return pm_setup_args_dup_rest_p(((
const pm_implicit_node_t *) node)->value);
1918 case PM_ARRAY_NODE: {
1919 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
1920 for (
size_t index = 0; index < cast->
elements.
size; index++) {
1921 if (pm_setup_args_dup_rest_p(cast->
elements.
nodes[index])) {
1936pm_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)
1938 int dup_rest = SPLATARRAY_TRUE;
1940 const pm_node_list_t *arguments;
1941 size_t arguments_size;
1946 arguments_node != NULL &&
1947 (arguments = &arguments_node->
arguments, arguments_size = arguments->
size) >= 2 &&
1948 PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_SPLAT) &&
1949 !PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_MULTIPLE_SPLATS) &&
1950 PM_NODE_TYPE_P(arguments->
nodes[arguments_size - 1], PM_KEYWORD_HASH_NODE)
1955 dup_rest = SPLATARRAY_FALSE;
1957 const pm_keyword_hash_node_t *keyword_hash = (
const pm_keyword_hash_node_t *) arguments->
nodes[arguments_size - 1];
1958 const pm_node_list_t *elements = &keyword_hash->elements;
1960 for (
size_t index = 0; dup_rest == SPLATARRAY_FALSE && index < elements->
size; index++) {
1961 const pm_node_t *element = elements->
nodes[index];
1963 switch (PM_NODE_TYPE(element)) {
1964 case PM_ASSOC_NODE: {
1965 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
1966 if (pm_setup_args_dup_rest_p(assoc->
key) || pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
1969 case PM_ASSOC_SPLAT_NODE: {
1970 const pm_assoc_splat_node_t *assoc = (
const pm_assoc_splat_node_t *) element;
1971 if (assoc->
value != NULL && pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
1980 int initial_dup_rest = dup_rest;
1983 if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
1987 bool regular_block_arg =
true;
1988 const pm_node_t *block_expr = ((
const pm_block_argument_node_t *)block)->expression;
1990 if (block_expr && pm_setup_args_dup_rest_p(block_expr)) {
1991 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
1992 initial_dup_rest = dup_rest;
1995 DECL_ANCHOR(block_arg);
1996 pm_compile_node(iseq, block, block_arg,
false, scope_node);
1998 *flags |= VM_CALL_ARGS_BLOCKARG;
2000 if (LIST_INSN_SIZE_ONE(block_arg)) {
2001 LINK_ELEMENT *elem = FIRST_ELEMENT(block_arg);
2002 if (IS_INSN(elem)) {
2003 INSN *iobj = (INSN *) elem;
2004 if (iobj->insn_id == BIN(getblockparam)) {
2005 iobj->insn_id = BIN(getblockparamproxy);
2012 regular_block_arg =
false;
2016 argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
2017 PUSH_SEQ(ret, block_arg);
2020 argc = pm_setup_args_core(arguments_node, block, flags,
false, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
2027 if (*flags & VM_CALL_ARGS_SPLAT && dup_rest != initial_dup_rest) {
2028 *flags |= VM_CALL_ARGS_SPLAT_MUT;
2045pm_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)
2048 if (!popped) PUSH_INSN(ret, location, putnil);
2050 PM_COMPILE_NOT_POPPED(node->
receiver);
2052 int boff = (node->
block == NULL ? 0 : 1);
2053 int flag = PM_NODE_TYPE_P(node->
receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2055 int argc = pm_setup_args(node->
arguments, (
const pm_node_t *) node->
block, &flag, &keywords, iseq, ret, scope_node, node_location);
2057 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2059 PUSH_INSN(ret, location, splatkw);
2062 PUSH_INSN(ret, location, dup);
2063 PUSH_INSN(ret, location, splatkw);
2064 PUSH_INSN(ret, location, pop);
2068 int dup_argn = argc + 1 + boff;
2069 int keyword_len = 0;
2072 keyword_len = keywords->keyword_len;
2073 dup_argn += keyword_len;
2076 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2077 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2078 PM_COMPILE_NOT_POPPED(node->
value);
2081 PUSH_SEND(ret, location, id_operator,
INT2FIX(1));
2084 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2086 if (flag & VM_CALL_ARGS_SPLAT) {
2087 if (flag & VM_CALL_KW_SPLAT) {
2088 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2090 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2091 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2092 flag |= VM_CALL_ARGS_SPLAT_MUT;
2095 PUSH_INSN(ret, location, swap);
2096 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2097 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2098 PUSH_INSN(ret, location, pop);
2102 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2103 PUSH_INSN(ret, location, swap);
2104 PUSH_INSN(ret, location, pop);
2106 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2107 PUSH_INSN(ret, location, swap);
2108 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2109 PUSH_INSN(ret, location, swap);
2110 flag |= VM_CALL_ARGS_SPLAT_MUT;
2112 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2114 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2115 PUSH_INSN(ret, location, pop);
2116 PUSH_INSN(ret, location, pop);
2120 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2122 else if (flag & VM_CALL_KW_SPLAT) {
2124 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2125 PUSH_INSN(ret, location, swap);
2126 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2127 PUSH_INSN(ret, location, pop);
2129 PUSH_INSN(ret, location, swap);
2130 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2132 else if (keyword_len) {
2133 PUSH_INSN(ret, location, dup);
2134 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 2));
2135 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2136 PUSH_INSN(ret, location, pop);
2137 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2141 PUSH_INSN(ret, location, swap);
2143 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2146 PUSH_INSN(ret, location, pop);
2162pm_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)
2165 if (!popped) PUSH_INSN(ret, location, putnil);
2166 PM_COMPILE_NOT_POPPED(receiver);
2168 int boff = (block == NULL ? 0 : 1);
2169 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2171 int argc = pm_setup_args(arguments, (
const pm_node_t *) block, &flag, &keywords, iseq, ret, scope_node, node_location);
2173 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2175 PUSH_INSN(ret, location, splatkw);
2178 PUSH_INSN(ret, location, dup);
2179 PUSH_INSN(ret, location, splatkw);
2180 PUSH_INSN(ret, location, pop);
2184 int dup_argn = argc + 1 + boff;
2185 int keyword_len = 0;
2188 keyword_len = keywords->keyword_len;
2189 dup_argn += keyword_len;
2192 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2193 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2195 LABEL *label = NEW_LABEL(location.
line);
2196 LABEL *lfin = NEW_LABEL(location.
line);
2198 PUSH_INSN(ret, location, dup);
2199 if (PM_NODE_TYPE_P(node, PM_INDEX_AND_WRITE_NODE)) {
2200 PUSH_INSNL(ret, location, branchunless, label);
2203 PUSH_INSNL(ret, location, branchif, label);
2206 PUSH_INSN(ret, location, pop);
2207 PM_COMPILE_NOT_POPPED(value);
2210 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2213 if (flag & VM_CALL_ARGS_SPLAT) {
2214 if (flag & VM_CALL_KW_SPLAT) {
2215 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2216 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2217 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2218 flag |= VM_CALL_ARGS_SPLAT_MUT;
2221 PUSH_INSN(ret, location, swap);
2222 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2223 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2224 PUSH_INSN(ret, location, pop);
2228 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2229 PUSH_INSN(ret, location, swap);
2230 PUSH_INSN(ret, location, pop);
2232 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2233 PUSH_INSN(ret, location, swap);
2234 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2235 PUSH_INSN(ret, location, swap);
2236 flag |= VM_CALL_ARGS_SPLAT_MUT;
2238 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2240 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2241 PUSH_INSN(ret, location, pop);
2242 PUSH_INSN(ret, location, pop);
2246 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2248 else if (flag & VM_CALL_KW_SPLAT) {
2250 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2251 PUSH_INSN(ret, location, swap);
2252 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2253 PUSH_INSN(ret, location, pop);
2256 PUSH_INSN(ret, location, swap);
2257 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2259 else if (keyword_len) {
2260 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2261 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 0));
2262 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2266 PUSH_INSN(ret, location, swap);
2268 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2271 PUSH_INSN(ret, location, pop);
2272 PUSH_INSNL(ret, location, jump, lfin);
2273 PUSH_LABEL(ret, label);
2275 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2277 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(dup_argn + 1));
2278 PUSH_LABEL(ret, lfin);
2287#define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
2288#define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
2289#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
2290#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
2291#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
2296static 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);
2303pm_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)
2305 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2306 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2308 PUSH_INSN(ret, location, dup);
2309 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2311 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2312 PUSH_INSN1(ret, location, putobject, message);
2313 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2314 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2315 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2317 PUSH_INSN1(ret, location, putobject,
Qfalse);
2318 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2320 PUSH_INSN(ret, location, pop);
2321 PUSH_INSN(ret, location, pop);
2322 PUSH_LABEL(ret, match_succeeded_label);
2333pm_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)
2335 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2336 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2338 PUSH_INSN(ret, location, dup);
2339 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2341 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2342 PUSH_INSN1(ret, location, putobject, message);
2343 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2344 PUSH_INSN(ret, location, dup);
2345 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2346 PUSH_INSN1(ret, location, putobject, length);
2347 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(4));
2348 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2350 PUSH_INSN1(ret, location, putobject,
Qfalse);
2351 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2353 PUSH_INSN(ret, location, pop);
2354 PUSH_INSN(ret, location, pop);
2355 PUSH_LABEL(ret, match_succeeded_label);
2366pm_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)
2368 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2369 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2371 PUSH_INSN(ret, location, dup);
2372 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2373 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2375 VALUE operand = rb_fstring_lit(
"%p === %p does not return true");
2376 PUSH_INSN1(ret, location, putobject, operand);
2378 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2379 PUSH_INSN1(ret, location, topn,
INT2FIX(5));
2380 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2381 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2382 PUSH_INSN1(ret, location, putobject,
Qfalse);
2383 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2384 PUSH_INSN(ret, location, pop);
2385 PUSH_INSN(ret, location, pop);
2387 PUSH_LABEL(ret, match_succeeded_label);
2388 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2389 PUSH_INSN(ret, location, pop);
2390 PUSH_INSN(ret, location, pop);
2402pm_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)
2404 LABEL *matched_label = NEW_LABEL(pm_node_line_number(scope_node->parser, node));
2405 CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index));
2406 PUSH_LABEL(ret, matched_label);
2416pm_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)
2418 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2420 if (use_deconstructed_cache) {
2421 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2422 PUSH_INSNL(ret, location, branchnil, deconstruct_label);
2424 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2425 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2427 PUSH_INSN(ret, location, pop);
2428 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
2429 PUSH_INSNL(ret, location, jump, deconstructed_label);
2432 PUSH_INSNL(ret, location, jump, deconstruct_label);
2435 PUSH_LABEL(ret, deconstruct_label);
2436 PUSH_INSN(ret, location, dup);
2439 PUSH_INSN1(ret, location, putobject, operand);
2440 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2442 if (use_deconstructed_cache) {
2443 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
2446 if (in_single_pattern) {
2447 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1));
2450 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2451 PUSH_SEND(ret, location, rb_intern(
"deconstruct"),
INT2FIX(0));
2453 if (use_deconstructed_cache) {
2454 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2457 PUSH_INSN(ret, location, dup);
2459 PUSH_INSNL(ret, location, branchunless, type_error_label);
2460 PUSH_LABEL(ret, deconstructed_label);
2470pm_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)
2472 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2474 PUSH_INSN(ret, location, dup);
2475 PM_COMPILE_NOT_POPPED(node);
2477 if (in_single_pattern) {
2478 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
2480 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2481 if (in_single_pattern) {
2482 CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
2484 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2493pm_compile_pattern_error_handler(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_node_t *node, LINK_ANCHOR *
const ret, LABEL *done_label,
bool popped)
2495 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2496 LABEL *key_error_label = NEW_LABEL(location.
line);
2497 LABEL *cleanup_label = NEW_LABEL(location.
line);
2500 kw_arg->references = 0;
2501 kw_arg->keyword_len = 2;
2502 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
2503 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
2505 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2506 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2507 PUSH_INSNL(ret, location, branchif, key_error_label);
2510 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2513 VALUE operand = rb_fstring_lit(
"%p: %s");
2514 PUSH_INSN1(ret, location, putobject, operand);
2517 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2518 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2519 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2520 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2521 PUSH_INSNL(ret, location, jump, cleanup_label);
2523 PUSH_LABEL(ret, key_error_label);
2525 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2528 VALUE operand = rb_fstring_lit(
"%p: %s");
2529 PUSH_INSN1(ret, location, putobject, operand);
2532 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2533 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2534 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2535 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2536 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2537 PUSH_SEND_R(ret, location, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
2538 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(1));
2539 PUSH_LABEL(ret, cleanup_label);
2541 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(7));
2542 if (!popped) PUSH_INSN(ret, location, putnil);
2543 PUSH_INSNL(ret, location, jump, done_label);
2544 PUSH_INSN1(ret, location, dupn,
INT2FIX(5));
2545 if (popped) PUSH_INSN(ret, location, putnil);
2552pm_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)
2554 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2556 switch (PM_NODE_TYPE(node)) {
2557 case PM_ARRAY_PATTERN_NODE: {
2566 const pm_array_pattern_node_t *cast = (
const pm_array_pattern_node_t *) node;
2569 const size_t posts_size = cast->
posts.
size;
2570 const size_t minimum_size = requireds_size + posts_size;
2572 bool rest_named =
false;
2573 bool use_rest_size =
false;
2575 if (cast->
rest != NULL) {
2576 rest_named = (PM_NODE_TYPE_P(cast->
rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *) cast->
rest)->expression != NULL);
2577 use_rest_size = (rest_named || (!rest_named && posts_size > 0));
2580 LABEL *match_failed_label = NEW_LABEL(location.
line);
2581 LABEL *type_error_label = NEW_LABEL(location.
line);
2582 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2583 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2585 if (use_rest_size) {
2586 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2587 PUSH_INSN(ret, location, swap);
2592 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2595 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));
2597 PUSH_INSN(ret, location, dup);
2598 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2599 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2600 PUSH_SEND(ret, location, cast->
rest == NULL ? idEq : idGE,
INT2FIX(1));
2601 if (in_single_pattern) {
2602 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+)");
2603 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message,
INT2FIX(minimum_size), base_index + 1));
2605 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2607 for (
size_t index = 0; index < requireds_size; index++) {
2609 PUSH_INSN(ret, location, dup);
2610 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2611 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2612 CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern,
false, base_index + 1));
2615 if (cast->
rest != NULL) {
2617 PUSH_INSN(ret, location, dup);
2618 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size));
2619 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2620 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2621 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2622 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2623 PUSH_INSN1(ret, location, setn,
INT2FIX(4));
2624 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2625 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));
2627 else if (posts_size > 0) {
2628 PUSH_INSN(ret, location, dup);
2629 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2630 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2631 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2632 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2633 PUSH_INSN(ret, location, pop);
2637 for (
size_t index = 0; index < posts_size; index++) {
2638 const pm_node_t *post = cast->
posts.
nodes[index];
2639 PUSH_INSN(ret, location, dup);
2641 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size + index));
2642 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2643 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2644 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2645 CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern,
false, base_index + 1));
2648 PUSH_INSN(ret, location, pop);
2649 if (use_rest_size) {
2650 PUSH_INSN(ret, location, pop);
2653 PUSH_INSNL(ret, location, jump, matched_label);
2654 PUSH_INSN(ret, location, putnil);
2655 if (use_rest_size) {
2656 PUSH_INSN(ret, location, putnil);
2659 PUSH_LABEL(ret, type_error_label);
2660 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2664 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2665 PUSH_INSN1(ret, location, putobject, operand);
2668 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2669 PUSH_INSN(ret, location, pop);
2671 PUSH_LABEL(ret, match_failed_label);
2672 PUSH_INSN(ret, location, pop);
2673 if (use_rest_size) {
2674 PUSH_INSN(ret, location, pop);
2677 PUSH_INSNL(ret, location, jump, unmatched_label);
2680 case PM_FIND_PATTERN_NODE: {
2689 const pm_find_pattern_node_t *cast = (
const pm_find_pattern_node_t *) node;
2692 LABEL *match_failed_label = NEW_LABEL(location.
line);
2693 LABEL *type_error_label = NEW_LABEL(location.
line);
2694 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2695 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2698 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2701 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));
2703 PUSH_INSN(ret, location, dup);
2704 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2705 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2706 PUSH_SEND(ret, location, idGE,
INT2FIX(1));
2707 if (in_single_pattern) {
2708 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));
2710 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2713 LABEL *while_begin_label = NEW_LABEL(location.
line);
2714 LABEL *next_loop_label = NEW_LABEL(location.
line);
2715 LABEL *find_succeeded_label = NEW_LABEL(location.
line);
2716 LABEL *find_failed_label = NEW_LABEL(location.
line);
2718 PUSH_INSN(ret, location, dup);
2719 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2721 PUSH_INSN(ret, location, dup);
2722 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2723 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2724 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2725 PUSH_LABEL(ret, while_begin_label);
2727 PUSH_INSN(ret, location, dup);
2728 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2729 PUSH_SEND(ret, location, idLE,
INT2FIX(1));
2730 PUSH_INSNL(ret, location, branchunless, find_failed_label);
2732 for (
size_t index = 0; index < size; index++) {
2733 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2734 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2737 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2738 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2741 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2742 CHECK(pm_compile_pattern_match(iseq, scope_node, cast->
requireds.
nodes[index], ret, next_loop_label, in_single_pattern,
false, base_index + 4));
2745 const pm_splat_node_t *left = cast->
left;
2748 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2749 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2750 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2751 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2752 CHECK(pm_compile_pattern_match(iseq, scope_node, left->
expression, ret, find_failed_label, in_single_pattern,
false, base_index + 4));
2756 const pm_splat_node_t *right = (
const pm_splat_node_t *) cast->
right;
2758 if (right->expression != NULL) {
2759 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2760 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2761 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2762 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2763 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2764 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2765 pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern,
false, base_index + 4);
2768 PUSH_INSNL(ret, location, jump, find_succeeded_label);
2770 PUSH_LABEL(ret, next_loop_label);
2771 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
2772 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2773 PUSH_INSNL(ret, location, jump, while_begin_label);
2775 PUSH_LABEL(ret, find_failed_label);
2776 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2777 if (in_single_pattern) {
2778 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2781 VALUE operand = rb_fstring_lit(
"%p does not match to find pattern");
2782 PUSH_INSN1(ret, location, putobject, operand);
2785 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2786 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2787 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2789 PUSH_INSN1(ret, location, putobject,
Qfalse);
2790 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2792 PUSH_INSN(ret, location, pop);
2793 PUSH_INSN(ret, location, pop);
2795 PUSH_INSNL(ret, location, jump, match_failed_label);
2796 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2798 PUSH_LABEL(ret, find_succeeded_label);
2799 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2802 PUSH_INSN(ret, location, pop);
2803 PUSH_INSNL(ret, location, jump, matched_label);
2804 PUSH_INSN(ret, location, putnil);
2806 PUSH_LABEL(ret, type_error_label);
2807 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2811 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2812 PUSH_INSN1(ret, location, putobject, operand);
2815 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2816 PUSH_INSN(ret, location, pop);
2818 PUSH_LABEL(ret, match_failed_label);
2819 PUSH_INSN(ret, location, pop);
2820 PUSH_INSNL(ret, location, jump, unmatched_label);
2824 case PM_HASH_PATTERN_NODE: {
2833 const pm_hash_pattern_node_t *cast = (
const pm_hash_pattern_node_t *) node;
2836 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);
2839 LABEL *match_failed_label = NEW_LABEL(location.
line);
2840 LABEL *type_error_label = NEW_LABEL(location.
line);
2843 if (has_keys && !has_rest) {
2846 for (
size_t index = 0; index < cast->
elements.
size; index++) {
2848 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2850 const pm_node_t *key = ((
const pm_assoc_node_t *) element)->key;
2853 VALUE symbol =
ID2SYM(parse_string_symbol(scope_node, (
const pm_symbol_node_t *) key));
2859 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2862 PUSH_INSN(ret, location, dup);
2865 VALUE operand =
ID2SYM(rb_intern(
"deconstruct_keys"));
2866 PUSH_INSN1(ret, location, putobject, operand);
2869 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2870 if (in_single_pattern) {
2871 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1));
2873 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2876 PUSH_INSN(ret, location, putnil);
2880 RB_OBJ_SET_FROZEN_SHAREABLE(keys);
2881 PUSH_INSN1(ret, location, duparray, keys);
2884 PUSH_SEND(ret, location, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
2886 PUSH_INSN(ret, location, dup);
2888 PUSH_INSNL(ret, location, branchunless, type_error_label);
2891 PUSH_SEND(ret, location, rb_intern(
"dup"),
INT2FIX(0));
2895 DECL_ANCHOR(match_values);
2897 for (
size_t index = 0; index < cast->
elements.
size; index++) {
2899 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2901 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
2902 const pm_node_t *key = assoc->
key;
2905 VALUE symbol =
ID2SYM(parse_string_symbol(scope_node, (
const pm_symbol_node_t *) key));
2906 PUSH_INSN(ret, location, dup);
2907 PUSH_INSN1(ret, location, putobject, symbol);
2908 PUSH_SEND(ret, location, rb_intern(
"key?"),
INT2FIX(1));
2910 if (in_single_pattern) {
2911 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2913 PUSH_INSN(ret, location, dup);
2914 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2918 RB_OBJ_SET_SHAREABLE(operand);
2919 PUSH_INSN1(ret, location, putobject, operand);
2922 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
2923 PUSH_INSN1(ret, location, putobject,
Qtrue);
2924 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
2925 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2926 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2927 PUSH_INSN1(ret, location, putobject, symbol);
2928 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2930 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(4));
2931 PUSH_LABEL(ret, match_succeeded_label);
2934 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2935 PUSH_INSN(match_values, location, dup);
2936 PUSH_INSN1(match_values, location, putobject, symbol);
2937 PUSH_SEND(match_values, location, has_rest ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
2939 const pm_node_t *value = assoc->
value;
2940 if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
2941 value = ((
const pm_implicit_node_t *) value)->value;
2944 CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern,
false, base_index + 1));
2947 PUSH_SEQ(ret, match_values);
2950 PUSH_INSN(ret, location, dup);
2951 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
2952 if (in_single_pattern) {
2953 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p is not empty"), base_index + 1));
2955 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2959 switch (PM_NODE_TYPE(cast->
rest)) {
2960 case PM_NO_KEYWORDS_PARAMETER_NODE: {
2961 PUSH_INSN(ret, location, dup);
2962 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
2963 if (in_single_pattern) {
2964 pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1);
2966 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2969 case PM_ASSOC_SPLAT_NODE: {
2970 const pm_assoc_splat_node_t *splat = (
const pm_assoc_splat_node_t *) cast->
rest;
2971 PUSH_INSN(ret, location, dup);
2972 pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern,
false, base_index + 1);
2976 rb_bug(
"unreachable");
2981 PUSH_INSN(ret, location, pop);
2982 PUSH_INSNL(ret, location, jump, matched_label);
2983 PUSH_INSN(ret, location, putnil);
2985 PUSH_LABEL(ret, type_error_label);
2986 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2990 VALUE operand = rb_fstring_lit(
"deconstruct_keys must return Hash");
2991 PUSH_INSN1(ret, location, putobject, operand);
2994 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2995 PUSH_INSN(ret, location, pop);
2997 PUSH_LABEL(ret, match_failed_label);
2998 PUSH_INSN(ret, location, pop);
2999 PUSH_INSNL(ret, location, jump, unmatched_label);
3002 case PM_CAPTURE_PATTERN_NODE: {
3012 const pm_capture_pattern_node_t *cast = (
const pm_capture_pattern_node_t *) node;
3014 LABEL *match_failed_label = NEW_LABEL(location.
line);
3016 PUSH_INSN(ret, location, dup);
3017 CHECK(pm_compile_pattern_match(iseq, scope_node, cast->
value, ret, match_failed_label, in_single_pattern, use_deconstructed_cache, base_index + 1));
3018 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));
3019 PUSH_INSN(ret, location, putnil);
3021 PUSH_LABEL(ret, match_failed_label);
3022 PUSH_INSN(ret, location, pop);
3023 PUSH_INSNL(ret, location, jump, unmatched_label);
3027 case PM_LOCAL_VARIABLE_TARGET_NODE: {
3031 const pm_local_variable_target_node_t *cast = (
const pm_local_variable_target_node_t *) node;
3032 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
3034 PUSH_SETLOCAL(ret, location, index.index, index.level);
3035 PUSH_INSNL(ret, location, jump, matched_label);
3038 case PM_ALTERNATION_PATTERN_NODE: {
3041 const pm_alternation_pattern_node_t *cast = (
const pm_alternation_pattern_node_t *) node;
3043 LABEL *matched_left_label = NEW_LABEL(location.
line);
3044 LABEL *unmatched_left_label = NEW_LABEL(location.
line);
3048 PUSH_INSN(ret, location, dup);
3049 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));
3054 PUSH_LABEL(ret, matched_left_label);
3055 PUSH_INSN(ret, location, pop);
3056 PUSH_INSNL(ret, location, jump, matched_label);
3057 PUSH_INSN(ret, location, putnil);
3061 PUSH_LABEL(ret, unmatched_left_label);
3062 CHECK(pm_compile_pattern(iseq, scope_node, cast->
right, ret, matched_label, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index));
3065 case PM_PARENTHESES_NODE:
3070 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);
3071 case PM_PINNED_EXPRESSION_NODE:
3076 node = ((
const pm_pinned_expression_node_t *) node)->expression;
3079 case PM_CLASS_VARIABLE_READ_NODE:
3080 case PM_CONSTANT_PATH_NODE:
3081 case PM_CONSTANT_READ_NODE:
3084 case PM_GLOBAL_VARIABLE_READ_NODE:
3085 case PM_IMAGINARY_NODE:
3086 case PM_INSTANCE_VARIABLE_READ_NODE:
3087 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3088 case PM_INTEGER_NODE:
3089 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
3090 case PM_INTERPOLATED_STRING_NODE:
3091 case PM_INTERPOLATED_SYMBOL_NODE:
3092 case PM_INTERPOLATED_X_STRING_NODE:
3093 case PM_LAMBDA_NODE:
3094 case PM_LOCAL_VARIABLE_READ_NODE:
3096 case PM_SOURCE_ENCODING_NODE:
3097 case PM_SOURCE_FILE_NODE:
3098 case PM_SOURCE_LINE_NODE:
3100 case PM_RATIONAL_NODE:
3101 case PM_REGULAR_EXPRESSION_NODE:
3103 case PM_STRING_NODE:
3104 case PM_SYMBOL_NODE:
3106 case PM_X_STRING_NODE: {
3110 PM_COMPILE_NOT_POPPED(node);
3111 if (in_single_pattern) {
3112 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
3115 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
3117 if (in_single_pattern) {
3118 pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
3121 PUSH_INSNL(ret, location, branchif, matched_label);
3122 PUSH_INSNL(ret, location, jump, unmatched_label);
3125 case PM_PINNED_VARIABLE_NODE: {
3130 const pm_pinned_variable_node_t *cast = (
const pm_pinned_variable_node_t *) node;
3131 CHECK(pm_compile_pattern(iseq, scope_node, cast->
variable, ret, matched_label, unmatched_label, in_single_pattern,
true, base_index));
3135 case PM_UNLESS_NODE: {
3147 const pm_node_t *predicate;
3148 const pm_node_t *statement;
3150 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3151 const pm_if_node_t *cast = (
const pm_if_node_t *) node;
3158 const pm_unless_node_t *cast = (
const pm_unless_node_t *) node;
3165 CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index));
3166 PM_COMPILE_NOT_POPPED(predicate);
3168 if (in_single_pattern) {
3169 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
3171 PUSH_INSN(ret, location, dup);
3172 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3173 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
3176 PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
3180 VALUE operand = rb_fstring_lit(
"guard clause does not return true");
3181 PUSH_INSN1(ret, location, putobject, operand);
3184 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
3185 PUSH_INSN1(ret, location, putobject,
Qfalse);
3186 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
3188 PUSH_INSN(ret, location, pop);
3189 PUSH_INSN(ret, location, pop);
3191 PUSH_LABEL(ret, match_succeeded_label);
3194 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3195 PUSH_INSNL(ret, location, branchunless, unmatched_label);
3198 PUSH_INSNL(ret, location, branchif, unmatched_label);
3201 PUSH_INSNL(ret, location, jump, matched_label);
3208 rb_bug(
"Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
3215#undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
3216#undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
3217#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
3218#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
3219#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
3223pm_scope_node_init(
const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous)
3227 memset(scope, 0,
sizeof(pm_scope_node_t));
3229 scope->base.
type = PM_SCOPE_NODE;
3233 scope->previous = previous;
3234 scope->ast_node = (pm_node_t *) node;
3237 scope->parser = previous->parser;
3238 scope->encoding = previous->encoding;
3240 scope->constants = previous->constants;
3241 scope->coverage_enabled = previous->coverage_enabled;
3245 switch (PM_NODE_TYPE(node)) {
3246 case PM_BLOCK_NODE: {
3247 const pm_block_node_t *cast = (
const pm_block_node_t *) node;
3248 scope->body = cast->
body;
3249 scope->locals = cast->
locals;
3253 case PM_CLASS_NODE: {
3254 const pm_class_node_t *cast = (
const pm_class_node_t *) node;
3255 scope->body = cast->
body;
3256 scope->locals = cast->
locals;
3260 const pm_def_node_t *cast = (
const pm_def_node_t *) node;
3261 scope->parameters = (pm_node_t *) cast->
parameters;
3262 scope->body = cast->body;
3263 scope->locals = cast->locals;
3266 case PM_ENSURE_NODE: {
3267 const pm_ensure_node_t *cast = (
const pm_ensure_node_t *) node;
3268 scope->body = (pm_node_t *) node;
3278 const pm_for_node_t *cast = (
const pm_for_node_t *) node;
3279 scope->body = (pm_node_t *) cast->
statements;
3282 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
3284 scope->body = (pm_node_t *) node;
3287 case PM_LAMBDA_NODE: {
3288 const pm_lambda_node_t *cast = (
const pm_lambda_node_t *) node;
3290 scope->body = cast->
body;
3291 scope->locals = cast->
locals;
3301 case PM_MODULE_NODE: {
3302 const pm_module_node_t *cast = (
const pm_module_node_t *) node;
3303 scope->body = cast->
body;
3304 scope->locals = cast->
locals;
3307 case PM_POST_EXECUTION_NODE: {
3308 const pm_post_execution_node_t *cast = (
const pm_post_execution_node_t *) node;
3309 scope->body = (pm_node_t *) cast->
statements;
3312 case PM_PROGRAM_NODE: {
3313 const pm_program_node_t *cast = (
const pm_program_node_t *) node;
3314 scope->body = (pm_node_t *) cast->
statements;
3315 scope->locals = cast->locals;
3318 case PM_RESCUE_NODE: {
3319 const pm_rescue_node_t *cast = (
const pm_rescue_node_t *) node;
3320 scope->body = (pm_node_t *) cast->
statements;
3323 case PM_RESCUE_MODIFIER_NODE: {
3324 const pm_rescue_modifier_node_t *cast = (
const pm_rescue_modifier_node_t *) node;
3328 case PM_SINGLETON_CLASS_NODE: {
3329 const pm_singleton_class_node_t *cast = (
const pm_singleton_class_node_t *) node;
3330 scope->body = cast->
body;
3331 scope->locals = cast->
locals;
3334 case PM_STATEMENTS_NODE: {
3335 const pm_statements_node_t *cast = (
const pm_statements_node_t *) node;
3336 scope->body = (pm_node_t *) cast;
3340 rb_bug(
"unreachable");
3346pm_scope_node_destroy(pm_scope_node_t *scope_node)
3348 if (scope_node->index_lookup_table) {
3349 st_free_table(scope_node->index_lookup_table);
3365pm_compile_retry_end_label(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, LABEL *retry_end_l)
3368 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
3369 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
3370 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
3371 iobj = (INSN*) get_prev_insn(iobj);
3373 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
3378 if (&iobj->link == LAST_ELEMENT(ret)) {
3379 ret->last = (LINK_ELEMENT*) retry_end_l;
3384pm_iseq_builtin_function_name(
const pm_scope_node_t *scope_node,
const pm_node_t *receiver,
ID method_id)
3386 const char *name = rb_id2name(method_id);
3387 static const char prefix[] =
"__builtin_";
3388 const size_t prefix_len =
sizeof(prefix) - 1;
3390 if (receiver == NULL) {
3391 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
3393 return &name[prefix_len];
3396 else if (PM_NODE_TYPE_P(receiver, PM_CALL_NODE)) {
3397 if (PM_NODE_FLAG_P(receiver, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3398 const pm_call_node_t *cast = (
const pm_call_node_t *) receiver;
3405 else if (PM_NODE_TYPE_P(receiver, PM_CONSTANT_READ_NODE)) {
3406 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) receiver;
3418pm_compile_builtin_attr(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_arguments_node_t *arguments,
const pm_node_location_t *node_location)
3420 if (arguments == NULL) {
3421 COMPILE_ERROR(iseq, node_location->
line,
"attr!: no argument");
3425 const pm_node_t *argument;
3427 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3428 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to attr!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3432 VALUE symbol = pm_static_literal_value(iseq, argument, scope_node);
3435 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
3436 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
3438 else if (strcmp(RSTRING_PTR(
string),
"inline_block") == 0) {
3439 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
3441 else if (strcmp(RSTRING_PTR(
string),
"use_block") == 0) {
3442 iseq_set_use_block(iseq);
3444 else if (strcmp(RSTRING_PTR(
string),
"c_trace") == 0) {
3446 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
3449 COMPILE_ERROR(iseq, node_location->
line,
"unknown argument to attr!: %s", RSTRING_PTR(
string));
3458pm_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)
3460 if (arguments == NULL) {
3461 COMPILE_ERROR(iseq, node_location->
line,
"arg!: no argument");
3466 COMPILE_ERROR(iseq, node_location->
line,
"arg!: too many argument");
3471 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3472 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to arg!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3477 ID name = parse_string_symbol(scope_node, ((
const pm_symbol_node_t *) argument));
3478 int index = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq, name);
3480 debugs(
"id: %s idx: %d\n", rb_id2name(name), index);
3481 PUSH_GETLOCAL(ret, *node_location, index, get_lvar_level(iseq));
3488pm_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)
3490 const pm_node_t *ast_node = scope_node->ast_node;
3491 if (!PM_NODE_TYPE_P(ast_node, PM_DEF_NODE)) {
3492 rb_bug(
"mandatory_only?: not in method definition");
3496 const pm_def_node_t *def_node = (
const pm_def_node_t *) ast_node;
3497 const pm_parameters_node_t *parameters_node = def_node->
parameters;
3498 if (parameters_node == NULL) {
3499 rb_bug(
"mandatory_only?: in method definition with no parameters");
3503 const pm_node_t *body_node = def_node->
body;
3504 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)) {
3505 rb_bug(
"mandatory_only?: not in method definition with plain statements");
3509 const pm_if_node_t *if_node = (
const pm_if_node_t *) ((
const pm_statements_node_t *) body_node)->body.nodes[0];
3510 if (if_node->
predicate != ((
const pm_node_t *) call_node)) {
3511 rb_bug(
"mandatory_only?: can't find mandatory node");
3515 pm_parameters_node_t parameters = {
3516 .base = parameters_node->
base,
3520 const pm_def_node_t def = {
3521 .base = def_node->
base,
3522 .name = def_node->
name,
3524 .parameters = ¶meters,
3533 pm_scope_node_t next_scope_node;
3534 pm_scope_node_init(&def.base, &next_scope_node, scope_node);
3537 const rb_iseq_t *mandatory_only_iseq = pm_iseq_new_with_opt(
3539 rb_iseq_base_label(iseq),
3541 rb_iseq_realpath(iseq),
3542 node_location->
line,
3546 ISEQ_COMPILE_DATA(iseq)->option,
3549 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
3552 RUBY_ASSERT(ISEQ_BODY(iseq)->mandatory_only_iseq == NULL);
3553 rb_jump_tag(error_state);
3556 pm_scope_node_destroy(&next_scope_node);
3561pm_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)
3563 const pm_arguments_node_t *arguments = call_node->
arguments;
3565 if (parent_block != NULL) {
3566 COMPILE_ERROR(iseq, node_location->
line,
"should not call builtins here.");
3570#define BUILTIN_INLINE_PREFIX "_bi"
3571 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
3572 bool cconst =
false;
3577 if (strcmp(
"cstmt!", builtin_func) == 0 || strcmp(
"cexpr!", builtin_func) == 0) {
3580 else if (strcmp(
"cconst!", builtin_func) == 0) {
3583 else if (strcmp(
"cinit!", builtin_func) == 0) {
3587 else if (strcmp(
"attr!", builtin_func) == 0) {
3588 return pm_compile_builtin_attr(iseq, scope_node, arguments, node_location);
3590 else if (strcmp(
"arg!", builtin_func) == 0) {
3591 return pm_compile_builtin_arg(iseq, ret, scope_node, arguments, node_location, popped);
3593 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
3595 rb_bug(
"mandatory_only? should be in if condition");
3597 else if (!LIST_INSN_SIZE_ZERO(ret)) {
3598 rb_bug(
"mandatory_only? should be put on top");
3601 PUSH_INSN1(ret, *node_location, putobject,
Qfalse);
3602 return pm_compile_builtin_mandatory_only_method(iseq, scope_node, call_node, node_location);
3605 rb_bug(
"can't find builtin function:%s", builtin_func);
3608 COMPILE_ERROR(iseq, node_location->
line,
"can't find builtin function:%s", builtin_func);
3612 int inline_index = node_location->
line;
3613 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
3614 builtin_func = inline_func;
3620 typedef VALUE(*builtin_func0)(
void *,
VALUE);
3621 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
3622 PUSH_INSN1(ret, *node_location, putobject, const_val);
3628 DECL_ANCHOR(args_seq);
3632 int argc = pm_setup_args(arguments, call_node->
block, &flags, &keywords, iseq, args_seq, scope_node, node_location);
3634 if (argc != bf->argc) {
3635 COMPILE_ERROR(iseq, node_location->
line,
"argc is not match for builtin function:%s (expect %d but %d)", builtin_func, bf->argc, argc);
3639 unsigned int start_index;
3640 if (delegate_call_p(iseq, argc, args_seq, &start_index)) {
3641 PUSH_INSN2(ret, *node_location, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
3644 PUSH_SEQ(ret, args_seq);
3645 PUSH_INSN1(ret, *node_location, invokebuiltin, bf);
3648 if (popped) PUSH_INSN(ret, *node_location, pop);
3656pm_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)
3663 LABEL *else_label = NEW_LABEL(location.
line);
3664 LABEL *end_label = NEW_LABEL(location.
line);
3665 LABEL *retry_end_l = NEW_LABEL(location.
line);
3668 rb_code_location_t code_location = { 0 };
3669 int node_id = location.
node_id;
3671 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3672 if (PM_BRANCH_COVERAGE_P(iseq)) {
3673 const uint8_t *cursors[3] = {
3679 const uint8_t *end_cursor = cursors[0];
3680 end_cursor = (end_cursor == NULL || cursors[1] == NULL) ? cursors[1] : (end_cursor > cursors[1] ? end_cursor : cursors[1]);
3681 end_cursor = (end_cursor == NULL || cursors[2] == NULL) ? cursors[2] : (end_cursor > cursors[2] ? end_cursor : cursors[2]);
3684 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, call_node);
3687 code_location = (rb_code_location_t) {
3688 .beg_pos = { .lineno = start_location.line, .column = start_location.column },
3689 .end_pos = { .lineno = end_location.line, .column = end_location.column }
3692 branches = decl_branch_base(iseq, PTR2NUM(call_node), &code_location,
"&.");
3695 PUSH_INSN(ret, location, dup);
3696 PUSH_INSNL(ret, location, branchnil, else_label);
3698 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0,
"then", branches);
3701 LINK_ELEMENT *opt_new_prelude = LAST_ELEMENT(ret);
3706 int orig_argc = pm_setup_args(call_node->
arguments, call_node->
block, &flags, &kw_arg, iseq, ret, scope_node, &location);
3707 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
3708 const rb_iseq_t *block_iseq = NULL;
3710 if (call_node->
block != NULL && PM_NODE_TYPE_P(call_node->
block, PM_BLOCK_NODE)) {
3712 pm_scope_node_t next_scope_node;
3713 pm_scope_node_init(call_node->
block, &next_scope_node, scope_node);
3715 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, pm_node_line_number(scope_node->parser, call_node->
block));
3716 pm_scope_node_destroy(&next_scope_node);
3717 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
3720 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3721 flags |= VM_CALL_VCALL;
3725 flags |= VM_CALL_ARGS_SIMPLE;
3729 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
3730 flags |= VM_CALL_FCALL;
3733 if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
3734 if (flags & VM_CALL_ARGS_BLOCKARG) {
3735 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
3736 if (flags & VM_CALL_ARGS_SPLAT) {
3737 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3738 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3740 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 3));
3741 PUSH_INSN(ret, location, pop);
3743 else if (flags & VM_CALL_ARGS_SPLAT) {
3744 PUSH_INSN(ret, location, dup);
3745 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3746 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3747 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 2));
3748 PUSH_INSN(ret, location, pop);
3751 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 1));
3755 if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
3756 PUSH_INSN(ret, location, splatkw);
3759 LABEL *not_basic_new = NEW_LABEL(location.
line);
3760 LABEL *not_basic_new_finish = NEW_LABEL(location.
line);
3762 bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
3763 method_id == rb_intern(
"new") &&
3764 call_node->
block == NULL &&
3765 (flags & VM_CALL_ARGS_BLOCKARG) == 0;
3768 if (LAST_ELEMENT(ret) == opt_new_prelude) {
3769 PUSH_INSN(ret, location, putnil);
3770 PUSH_INSN(ret, location, swap);
3773 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.
line, location.
node_id, BIN(swap), 0)->link);
3774 ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.
line, location.
node_id, BIN(putnil), 0)->link);
3779 if (flags & VM_CALL_FORWARDING) {
3780 ci = (
VALUE)new_callinfo(iseq, method_id, orig_argc + 1, flags, kw_arg, 0);
3783 ci = (
VALUE)new_callinfo(iseq, method_id, orig_argc, flags, kw_arg, 0);
3786 PUSH_INSN2(ret, location, opt_new, ci, not_basic_new);
3787 LABEL_REF(not_basic_new);
3789 PUSH_SEND_R(ret, location, rb_intern(
"initialize"),
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags | VM_CALL_FCALL), kw_arg);
3790 PUSH_INSNL(ret, location, jump, not_basic_new_finish);
3792 PUSH_LABEL(ret, not_basic_new);
3794 PUSH_SEND_R(ret, location, method_id,
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags), kw_arg);
3795 PUSH_INSN(ret, location, swap);
3797 PUSH_LABEL(ret, not_basic_new_finish);
3798 PUSH_INSN(ret, location, pop);
3801 PUSH_SEND_R(ret, location, method_id,
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags), kw_arg);
3804 if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
3805 pm_compile_retry_end_label(iseq, ret, retry_end_l);
3806 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, start, retry_end_l, block_iseq, retry_end_l);
3809 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3810 PUSH_INSNL(ret, location, jump, end_label);
3811 PUSH_LABEL(ret, else_label);
3812 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 1,
"else", branches);
3813 PUSH_LABEL(ret, end_label);
3816 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
3817 PUSH_INSN(ret, location, pop);
3820 if (popped) PUSH_INSN(ret, location, pop);
3821 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
3829pm_compile_back_reference_ref(
const pm_back_reference_read_node_t *node)
3843pm_compile_numbered_reference_ref(
const pm_numbered_reference_read_node_t *node)
3849pm_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)
3851#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
3854 enum defined_type dtype = DEFINED_NOT_DEFINED;
3857 switch (PM_NODE_TYPE(node)) {
3862 dtype = DEFINED_NIL;
3865 case PM_INSTANCE_VARIABLE_READ_NODE: {
3868 const pm_instance_variable_read_node_t *cast = (
const pm_instance_variable_read_node_t *) node;
3869 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3871 PUSH_INSN3(ret, location, definedivar,
ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
3876 case PM_LOCAL_VARIABLE_READ_NODE:
3879 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3882 dtype = DEFINED_LVAR;
3885 case PM_GLOBAL_VARIABLE_READ_NODE: {
3888 const pm_global_variable_read_node_t *cast = (
const pm_global_variable_read_node_t *) node;
3889 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3891 PUSH_INSN(ret, location, putnil);
3892 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR),
ID2SYM(name), PUSH_VAL(DEFINED_GVAR));
3897 case PM_CLASS_VARIABLE_READ_NODE: {
3900 const pm_class_variable_read_node_t *cast = (
const pm_class_variable_read_node_t *) node;
3901 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3903 PUSH_INSN(ret, location, putnil);
3904 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR),
ID2SYM(name), PUSH_VAL(DEFINED_CVAR));
3909 case PM_CONSTANT_READ_NODE: {
3912 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) node;
3913 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3915 PUSH_INSN(ret, location, putnil);
3916 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
3924 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
3926 PUSH_INSN(ret, location, putnil);
3927 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
3931 case PM_SUPER_NODE: {
3934 const pm_super_node_t *cast = (
const pm_super_node_t *) node;
3936 if (cast->
block != NULL && !PM_NODE_TYPE_P(cast->
block, PM_BLOCK_ARGUMENT_NODE)) {
3937 dtype = DEFINED_EXPR;
3941 PUSH_INSN(ret, location, putnil);
3942 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3945 case PM_FORWARDING_SUPER_NODE: {
3948 const pm_forwarding_super_node_t *cast = (
const pm_forwarding_super_node_t *) node;
3950 if (cast->
block != NULL) {
3951 dtype = DEFINED_EXPR;
3955 PUSH_INSN(ret, location, putnil);
3956 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3963 dtype = DEFINED_SELF;
3969 dtype = DEFINED_TRUE;
3975 dtype = DEFINED_FALSE;
3978 case PM_CALL_AND_WRITE_NODE:
3981 case PM_CALL_OPERATOR_WRITE_NODE:
3984 case PM_CALL_OR_WRITE_NODE:
3987 case PM_CLASS_VARIABLE_AND_WRITE_NODE:
3990 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
3993 case PM_CLASS_VARIABLE_OR_WRITE_NODE:
3996 case PM_CLASS_VARIABLE_WRITE_NODE:
3999 case PM_CONSTANT_AND_WRITE_NODE:
4002 case PM_CONSTANT_OPERATOR_WRITE_NODE:
4005 case PM_CONSTANT_OR_WRITE_NODE:
4008 case PM_CONSTANT_PATH_AND_WRITE_NODE:
4011 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
4014 case PM_CONSTANT_PATH_OR_WRITE_NODE:
4017 case PM_CONSTANT_PATH_WRITE_NODE:
4020 case PM_CONSTANT_WRITE_NODE:
4023 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
4026 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
4029 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
4032 case PM_GLOBAL_VARIABLE_WRITE_NODE:
4035 case PM_INDEX_AND_WRITE_NODE:
4038 case PM_INDEX_OPERATOR_WRITE_NODE:
4041 case PM_INDEX_OR_WRITE_NODE:
4044 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
4047 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
4050 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
4053 case PM_INSTANCE_VARIABLE_WRITE_NODE:
4056 case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
4059 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
4062 case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
4065 case PM_LOCAL_VARIABLE_WRITE_NODE:
4068 case PM_MULTI_WRITE_NODE:
4071 dtype = DEFINED_ASGN;
4074 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
4077 case PM_ALIAS_METHOD_NODE:
4086 case PM_CASE_MATCH_NODE:
4098 case PM_DEFINED_NODE:
4101 case PM_FLIP_FLOP_NODE:
4113 case PM_IMAGINARY_NODE:
4116 case PM_INTEGER_NODE:
4119 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE:
4122 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
4125 case PM_INTERPOLATED_STRING_NODE:
4128 case PM_INTERPOLATED_SYMBOL_NODE:
4131 case PM_INTERPOLATED_X_STRING_NODE:
4134 case PM_LAMBDA_NODE:
4137 case PM_MATCH_LAST_LINE_NODE:
4140 case PM_MATCH_PREDICATE_NODE:
4143 case PM_MATCH_REQUIRED_NODE:
4146 case PM_MATCH_WRITE_NODE:
4149 case PM_MODULE_NODE:
4158 case PM_POST_EXECUTION_NODE:
4164 case PM_RATIONAL_NODE:
4170 case PM_REGULAR_EXPRESSION_NODE:
4173 case PM_RESCUE_MODIFIER_NODE:
4179 case PM_RETURN_NODE:
4182 case PM_SINGLETON_CLASS_NODE:
4185 case PM_SOURCE_ENCODING_NODE:
4188 case PM_SOURCE_FILE_NODE:
4191 case PM_SOURCE_LINE_NODE:
4194 case PM_STRING_NODE:
4197 case PM_SYMBOL_NODE:
4203 case PM_UNLESS_NODE:
4212 case PM_X_STRING_NODE:
4215 dtype = DEFINED_EXPR;
4218 case PM_BACK_REFERENCE_READ_NODE: {
4221 const pm_back_reference_read_node_t *cast = (
const pm_back_reference_read_node_t *) node;
4222 VALUE ref = pm_compile_back_reference_ref(cast);
4224 PUSH_INSN(ret, location, putnil);
4225 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4229 case PM_NUMBERED_REFERENCE_READ_NODE: {
4232 const pm_numbered_reference_read_node_t *cast = (
const pm_numbered_reference_read_node_t *) node;
4233 VALUE ref = pm_compile_numbered_reference_ref(cast);
4235 PUSH_INSN(ret, location, putnil);
4236 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4241 case PM_CONSTANT_PATH_NODE: {
4244 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) node;
4245 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4247 if (cast->
parent != NULL) {
4248 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4249 pm_compile_defined_expr0(iseq, cast->
parent, node_location, ret, popped, scope_node,
true, lfinish,
false);
4251 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4252 PM_COMPILE(cast->
parent);
4255 PUSH_INSN1(ret, location, putobject, rb_cObject);
4258 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
4262 case PM_BEGIN_NODE: {
4265 const pm_begin_node_t *cast = (
const pm_begin_node_t *) node;
4270 dtype = DEFINED_NIL;
4276 pm_compile_defined_expr0(iseq, cast->
statements->
body.
nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4282 dtype = DEFINED_EXPR;
4287 dtype = DEFINED_EXPR;
4292 case PM_PARENTHESES_NODE: {
4295 const pm_parentheses_node_t *cast = (
const pm_parentheses_node_t *) node;
4297 if (cast->
body == NULL) {
4299 dtype = DEFINED_NIL;
4301 else if (PM_NODE_TYPE_P(cast->
body, PM_STATEMENTS_NODE) && !PM_NODE_FLAG_P(cast, PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS)) {
4304 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);
4310 dtype = DEFINED_EXPR;
4315 case PM_ARRAY_NODE: {
4318 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
4321 lfinish[1] = NEW_LABEL(location.
line);
4324 for (
size_t index = 0; index < cast->
elements.
size; index++) {
4325 pm_compile_defined_expr0(iseq, cast->
elements.
nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4326 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4329 dtype = DEFINED_EXPR;
4335 case PM_KEYWORD_HASH_NODE: {
4338 const pm_node_list_t *elements;
4340 if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
4341 elements = &((
const pm_hash_node_t *) node)->elements;
4344 elements = &((
const pm_keyword_hash_node_t *) node)->elements;
4347 if (elements->
size > 0 && !lfinish[1]) {
4348 lfinish[1] = NEW_LABEL(location.
line);
4351 for (
size_t index = 0; index < elements->
size; index++) {
4352 pm_compile_defined_expr0(iseq, elements->
nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4353 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4356 dtype = DEFINED_EXPR;
4359 case PM_ASSOC_NODE: {
4362 const pm_assoc_node_t *cast = (
const pm_assoc_node_t *) node;
4364 pm_compile_defined_expr0(iseq, cast->
key, node_location, ret, popped, scope_node,
true, lfinish,
false);
4365 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4366 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4370 case PM_ASSOC_SPLAT_NODE: {
4373 const pm_assoc_splat_node_t *cast = (
const pm_assoc_splat_node_t *) node;
4375 if (cast->
value == NULL) {
4376 dtype = DEFINED_EXPR;
4380 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4383 case PM_IMPLICIT_NODE: {
4386 const pm_implicit_node_t *cast = (
const pm_implicit_node_t *) node;
4387 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4390 case PM_CALL_NODE: {
4391#define BLOCK_P(cast) ((cast)->block != NULL && PM_NODE_TYPE_P((cast)->block, PM_BLOCK_NODE))
4395 const pm_call_node_t *cast = ((
const pm_call_node_t *) node);
4397 if (BLOCK_P(cast)) {
4398 dtype = DEFINED_EXPR;
4403 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4404 if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.
line);
4408 pm_compile_defined_expr0(iseq, (
const pm_node_t *) cast->
arguments, node_location, ret, popped, scope_node,
true, lfinish,
false);
4409 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4412 if (cast->
block && PM_NODE_TYPE_P(cast->
block, PM_BLOCK_ARGUMENT_NODE)) {
4413 pm_compile_defined_expr0(iseq, cast->
block, node_location, ret, popped, scope_node,
true, lfinish,
false);
4414 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4418 if (PM_NODE_TYPE_P(cast->
receiver, PM_CALL_NODE) && !BLOCK_P((
const pm_call_node_t *) cast->
receiver)) {
4421 pm_compile_defined_expr0(iseq, cast->
receiver, node_location, ret, popped, scope_node,
true, lfinish,
true);
4422 PUSH_INSNL(ret, location, branchunless, lfinish[2]);
4424 const pm_call_node_t *receiver = (
const pm_call_node_t *) cast->
receiver;
4425 ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
4427 pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
4430 pm_compile_defined_expr0(iseq, cast->
receiver, node_location, ret, popped, scope_node,
true, lfinish,
false);
4431 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4435 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
4437 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4438 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_METHOD),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4441 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
4443 PUSH_INSN(ret, location, putself);
4444 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4446 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_FUNC),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4453 case PM_ARGUMENTS_NODE: {
4456 const pm_arguments_node_t *cast = (
const pm_arguments_node_t *) node;
4458 for (
size_t index = 0; index < cast->
arguments.
size; index++) {
4459 pm_compile_defined_expr0(iseq, cast->
arguments.
nodes[index], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4460 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4463 dtype = DEFINED_EXPR;
4466 case PM_BLOCK_ARGUMENT_NODE:
4469 dtype = DEFINED_EXPR;
4471 case PM_FORWARDING_ARGUMENTS_NODE:
4474 dtype = DEFINED_EXPR;
4476 case PM_SPLAT_NODE: {
4479 const pm_splat_node_t *cast = (
const pm_splat_node_t *) node;
4482 dtype = DEFINED_EXPR;
4486 pm_compile_defined_expr0(iseq, cast->
expression, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4488 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4489 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4491 dtype = DEFINED_EXPR;
4494 case PM_SHAREABLE_CONSTANT_NODE:
4498 pm_compile_defined_expr0(iseq, ((
const pm_shareable_constant_node_t *) node)->write, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
4501 case PM_BLOCK_LOCAL_VARIABLE_NODE:
4502 case PM_BLOCK_PARAMETER_NODE:
4503 case PM_BLOCK_PARAMETERS_NODE:
4504 case PM_FORWARDING_PARAMETER_NODE:
4505 case PM_IMPLICIT_REST_NODE:
4506 case PM_IT_PARAMETERS_NODE:
4507 case PM_PARAMETERS_NODE:
4508 case PM_KEYWORD_REST_PARAMETER_NODE:
4509 case PM_NO_KEYWORDS_PARAMETER_NODE:
4510 case PM_NUMBERED_PARAMETERS_NODE:
4511 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE:
4512 case PM_OPTIONAL_PARAMETER_NODE:
4513 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
4514 case PM_REQUIRED_PARAMETER_NODE:
4515 case PM_REST_PARAMETER_NODE:
4517 case PM_ALTERNATION_PATTERN_NODE:
4518 case PM_ARRAY_PATTERN_NODE:
4519 case PM_CAPTURE_PATTERN_NODE:
4520 case PM_FIND_PATTERN_NODE:
4521 case PM_HASH_PATTERN_NODE:
4522 case PM_PINNED_EXPRESSION_NODE:
4523 case PM_PINNED_VARIABLE_NODE:
4525 case PM_CALL_TARGET_NODE:
4526 case PM_CLASS_VARIABLE_TARGET_NODE:
4527 case PM_CONSTANT_PATH_TARGET_NODE:
4528 case PM_CONSTANT_TARGET_NODE:
4529 case PM_GLOBAL_VARIABLE_TARGET_NODE:
4530 case PM_INDEX_TARGET_NODE:
4531 case PM_INSTANCE_VARIABLE_TARGET_NODE:
4532 case PM_LOCAL_VARIABLE_TARGET_NODE:
4533 case PM_MULTI_TARGET_NODE:
4536 case PM_ENSURE_NODE:
4538 case PM_RESCUE_NODE:
4542 case PM_EMBEDDED_STATEMENTS_NODE:
4543 case PM_EMBEDDED_VARIABLE_NODE:
4544 case PM_MISSING_NODE:
4545 case PM_PRE_EXECUTION_NODE:
4546 case PM_PROGRAM_NODE:
4548 case PM_STATEMENTS_NODE:
4549 rb_bug(
"Unreachable node in defined?: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4553 PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
4559pm_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)
4561 LINK_ELEMENT *lcur = ret->last;
4562 pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4565 LABEL *lstart = NEW_LABEL(node_location->
line);
4566 LABEL *lend = NEW_LABEL(node_location->
line);
4569 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
4571 const rb_iseq_t *rescue = new_child_iseq_with_callback(
4580 lstart->rescued = LABEL_RESCUE_BEG;
4581 lend->rescued = LABEL_RESCUE_END;
4583 APPEND_LABEL(ret, lcur, lstart);
4584 PUSH_LABEL(ret, lend);
4585 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
4590pm_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)
4593 LINK_ELEMENT *last = ret->last;
4595 lfinish[0] = NEW_LABEL(node_location->
line);
4600 pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish);
4604 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, node_location->
line, node_location->
node_id, BIN(putnil), 0)->link);
4605 PUSH_INSN(ret, *node_location, swap);
4607 if (lfinish[2]) PUSH_LABEL(ret, lfinish[2]);
4608 PUSH_INSN(ret, *node_location, pop);
4609 PUSH_LABEL(ret, lfinish[1]);
4613 PUSH_LABEL(ret, lfinish[0]);
4619pm_add_ensure_iseq(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
int is_return, pm_scope_node_t *scope_node)
4624 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
4626 DECL_ANCHOR(ensure);
4629 if (enlp->erange != NULL) {
4630 DECL_ANCHOR(ensure_part);
4631 LABEL *lstart = NEW_LABEL(0);
4632 LABEL *lend = NEW_LABEL(0);
4634 add_ensure_range(iseq, enlp->erange, lstart, lend);
4636 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
4637 PUSH_LABEL(ensure_part, lstart);
4639 PM_COMPILE_INTO_ANCHOR(ensure_part, (
const pm_node_t *) enlp->ensure_node);
4640 PUSH_LABEL(ensure_part, lend);
4641 PUSH_SEQ(ensure, ensure_part);
4650 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
4651 PUSH_SEQ(ret, ensure);
4655 pm_scope_node_t *scope_node;
4656 rb_ast_id_table_t *local_table_for_iseq;
4661pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
4667 pm_scope_node_t *scope_node = ctx->scope_node;
4668 rb_ast_id_table_t *local_table_for_iseq = ctx->local_table_for_iseq;
4669 int local_index = ctx->local_index;
4671 ID local = pm_constant_id_lookup(scope_node, constant_id);
4672 local_table_for_iseq->ids[local_index] = local;
4674 *value = (st_data_t)local_index;
4688pm_insert_local_index(
pm_constant_id_t constant_id,
int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node)
4690 RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
4692 ID local = pm_constant_id_lookup(scope_node, constant_id);
4693 local_table_for_iseq->ids[local_index] = local;
4694 st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
4702pm_insert_local_special(
ID local_name,
int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq)
4704 local_table_for_iseq->ids[local_index] = local_name;
4705 st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
4715pm_compile_destructured_param_locals(
const pm_multi_target_node_t *node, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node,
int local_index)
4717 for (
size_t index = 0; index < node->
lefts.
size; index++) {
4718 const pm_node_t *left = node->
lefts.
nodes[index];
4720 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4721 if (!PM_NODE_FLAG_P(left, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4722 pm_insert_local_index(((
const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4727 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4728 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);
4732 if (node->
rest != NULL && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE)) {
4733 const pm_splat_node_t *rest = (
const pm_splat_node_t *) node->
rest;
4735 if (rest->expression != NULL) {
4736 RUBY_ASSERT(PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE));
4738 if (!PM_NODE_FLAG_P(rest->expression, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4739 pm_insert_local_index(((
const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4745 for (
size_t index = 0; index < node->
rights.
size; index++) {
4746 const pm_node_t *right = node->
rights.
nodes[index];
4748 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4749 if (!PM_NODE_FLAG_P(right, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4750 pm_insert_local_index(((
const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4755 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4756 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);
4768pm_compile_destructured_param_write(rb_iseq_t *iseq,
const pm_required_parameter_node_t *node, LINK_ANCHOR *
const ret,
const pm_scope_node_t *scope_node)
4770 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4771 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, node->
name, 0);
4772 PUSH_SETLOCAL(ret, location, index.index, index.level);
4784pm_compile_destructured_param_writes(rb_iseq_t *iseq,
const pm_multi_target_node_t *node, LINK_ANCHOR *
const ret,
const pm_scope_node_t *scope_node)
4786 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4787 bool has_rest = (node->
rest && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE) && (((
const pm_splat_node_t *) node->
rest)->expression) != NULL);
4790 int flag = (has_rest || has_rights) ? 1 : 0;
4793 for (
size_t index = 0; index < node->
lefts.
size; index++) {
4794 const pm_node_t *left = node->
lefts.
nodes[index];
4796 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4797 pm_compile_destructured_param_write(iseq, (
const pm_required_parameter_node_t *) left, ret, scope_node);
4800 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4801 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) left, ret, scope_node);
4810 const pm_node_t *rest = ((
const pm_splat_node_t *) node->
rest)->expression;
4811 RUBY_ASSERT(PM_NODE_TYPE_P(rest, PM_REQUIRED_PARAMETER_NODE));
4813 pm_compile_destructured_param_write(iseq, (
const pm_required_parameter_node_t *) rest, ret, scope_node);
4821 for (
size_t index = 0; index < node->
rights.
size; index++) {
4822 const pm_node_t *right = node->
rights.
nodes[index];
4824 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4825 pm_compile_destructured_param_write(iseq, (
const pm_required_parameter_node_t *) right, ret, scope_node);
4828 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4829 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) right, ret, scope_node);
4856} pm_multi_target_state_node_t;
4874 pm_multi_target_state_node_t *head;
4877 pm_multi_target_state_node_t *tail;
4886 pm_multi_target_state_node_t *node =
ALLOC(pm_multi_target_state_node_t);
4888 node->stack_index = state->stack_size + 1;
4889 node->stack_size = stack_size;
4890 node->position = state->position;
4893 if (state->head == NULL) {
4898 state->tail->next = node;
4902 state->stack_size += stack_size;
4915 if (state->stack_size == 0)
return;
4917 pm_multi_target_state_node_t *current = state->head;
4918 pm_multi_target_state_node_t *previous;
4920 while (current != NULL) {
4921 VALUE offset =
INT2FIX(state->stack_size - current->stack_index + current->position);
4922 current->topn->operands[0] = offset;
4928 if (current->stack_size > 1) {
4929 INSN *insn = current->topn;
4931 for (
size_t index = 1; index < current->stack_size; index += 1) {
4932 LINK_ELEMENT *element = get_next_insn(insn);
4935 insn = (INSN *) element;
4938 insn->operands[0] = offset;
4943 current = current->next;
4950pm_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);
4981pm_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)
4983 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4985 switch (PM_NODE_TYPE(node)) {
4986 case PM_LOCAL_VARIABLE_TARGET_NODE: {
4992 const pm_local_variable_target_node_t *cast = (
const pm_local_variable_target_node_t *) node;
4993 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
4995 PUSH_SETLOCAL(writes, location, index.index, index.level);
4998 case PM_CLASS_VARIABLE_TARGET_NODE: {
5004 const pm_class_variable_target_node_t *cast = (
const pm_class_variable_target_node_t *) node;
5005 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5008 PUSH_INSN2(writes, location, setclassvariable, operand, get_cvar_ic_value(iseq, name));
5011 case PM_CONSTANT_TARGET_NODE: {
5017 const pm_constant_target_node_t *cast = (
const pm_constant_target_node_t *) node;
5018 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5021 PUSH_INSN1(writes, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5022 PUSH_INSN1(writes, location, setconstant, operand);
5025 case PM_GLOBAL_VARIABLE_TARGET_NODE: {
5031 const pm_global_variable_target_node_t *cast = (
const pm_global_variable_target_node_t *) node;
5032 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5035 PUSH_INSN1(writes, location, setglobal, operand);
5038 case PM_INSTANCE_VARIABLE_TARGET_NODE: {
5044 const pm_instance_variable_target_node_t *cast = (
const pm_instance_variable_target_node_t *) node;
5045 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5048 PUSH_INSN2(writes, location, setinstancevariable, operand, get_ivar_ic_value(iseq, name));
5051 case PM_CONSTANT_PATH_TARGET_NODE: {
5060 const pm_constant_path_target_node_t *cast = (
const pm_constant_path_target_node_t *) node;
5061 ID name = pm_constant_id_lookup(scope_node, cast->
name);
5063 if (cast->
parent != NULL) {
5064 pm_compile_node(iseq, cast->
parent, parents,
false, scope_node);
5067 PUSH_INSN1(parents, location, putobject, rb_cObject);
5070 if (state == NULL) {
5071 PUSH_INSN(writes, location, swap);
5074 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5075 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
5079 PUSH_INSN1(writes, location, setconstant, operand);
5081 if (state != NULL) {
5082 PUSH_INSN(cleanup, location, pop);
5087 case PM_CALL_TARGET_NODE: {
5096 const pm_call_target_node_t *cast = (
const pm_call_target_node_t *) node;
5097 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
5099 pm_compile_node(iseq, cast->
receiver, parents,
false, scope_node);
5101 LABEL *safe_label = NULL;
5102 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
5103 safe_label = NEW_LABEL(location.
line);
5104 PUSH_INSN(parents, location, dup);
5105 PUSH_INSNL(parents, location, branchnil, safe_label);
5108 if (state != NULL) {
5109 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5110 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
5111 PUSH_INSN(writes, location, swap);
5114 int flags = VM_CALL_ARGS_SIMPLE;
5115 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
5117 PUSH_SEND_WITH_FLAG(writes, location, method_id,
INT2FIX(1),
INT2FIX(flags));
5118 if (safe_label != NULL && state == NULL) PUSH_LABEL(writes, safe_label);
5119 PUSH_INSN(writes, location, pop);
5120 if (safe_label != NULL && state != NULL) PUSH_LABEL(writes, safe_label);
5122 if (state != NULL) {
5123 PUSH_INSN(cleanup, location, pop);
5128 case PM_INDEX_TARGET_NODE: {
5138 const pm_index_target_node_t *cast = (
const pm_index_target_node_t *) node;
5140 pm_compile_node(iseq, cast->
receiver, parents,
false, scope_node);
5144 int argc = pm_setup_args(cast->
arguments, (
const pm_node_t *) cast->
block, &flags, &kwargs, iseq, parents, scope_node, &location);
5146 if (state != NULL) {
5147 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5148 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), argc + 1);
5151 PUSH_INSN(writes, location, swap);
5154 for (
int index = 0; index < argc; index++) {
5155 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5157 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5165 int ci_argc = argc + 1;
5166 if (flags & VM_CALL_ARGS_SPLAT) {
5168 PUSH_INSN1(writes, location, newarray,
INT2FIX(1));
5169 PUSH_INSN(writes, location, concatarray);
5172 PUSH_SEND_R(writes, location, idASET,
INT2NUM(ci_argc), NULL,
INT2FIX(flags), kwargs);
5173 PUSH_INSN(writes, location, pop);
5175 if (state != NULL) {
5177 PUSH_INSN(writes, location, pop);
5180 for (
int index = 0; index < argc + 1; index++) {
5181 PUSH_INSN(cleanup, location, pop);
5187 case PM_MULTI_TARGET_NODE: {
5194 size_t before_position;
5195 if (state != NULL) {
5196 before_position = state->position;
5200 pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
5201 if (state != NULL) state->position = before_position;
5205 case PM_SPLAT_NODE: {
5211 const pm_splat_node_t *cast = (
const pm_splat_node_t *) node;
5214 pm_compile_target_node(iseq, cast->
expression, parents, writes, cleanup, scope_node, state);
5220 rb_bug(
"Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5231pm_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)
5233 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5234 const pm_node_list_t *lefts;
5235 const pm_node_t *rest;
5236 const pm_node_list_t *rights;
5238 switch (PM_NODE_TYPE(node)) {
5239 case PM_MULTI_TARGET_NODE: {
5240 const pm_multi_target_node_t *cast = (
const pm_multi_target_node_t *) node;
5241 lefts = &cast->
lefts;
5246 case PM_MULTI_WRITE_NODE: {
5247 const pm_multi_write_node_t *cast = (
const pm_multi_write_node_t *) node;
5248 lefts = &cast->
lefts;
5254 rb_bug(
"Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5258 bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *) rest)->expression != NULL;
5259 bool has_posts = rights->
size > 0;
5264 PUSH_INSN2(writes, location, expandarray,
INT2FIX(lefts->
size),
INT2FIX((has_rest || has_posts) ? 1 : 0));
5270 if (state == NULL) state = &target_state;
5272 size_t base_position = state->position;
5273 size_t splat_position = (has_rest || has_posts) ? 1 : 0;
5276 for (
size_t index = 0; index < lefts->
size; index++) {
5277 const pm_node_t *target = lefts->
nodes[index];
5278 state->position = lefts->
size - index + splat_position + base_position;
5279 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5284 const pm_node_t *target = ((
const pm_splat_node_t *) rest)->expression;
5285 state->position = 1 + rights->
size + base_position;
5291 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5296 if (!has_rest && rest != NULL) {
5300 for (
size_t index = 0; index < rights->
size; index++) {
5301 const pm_node_t *target = rights->
nodes[index];
5302 state->position = rights->
size - index + base_position;
5303 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5314pm_compile_for_node_index(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node)
5316 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5318 switch (PM_NODE_TYPE(node)) {
5319 case PM_LOCAL_VARIABLE_TARGET_NODE: {
5322 PUSH_GETLOCAL(ret, location, 1, 0);
5323 pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
5326 case PM_CLASS_VARIABLE_TARGET_NODE:
5327 case PM_CONSTANT_TARGET_NODE:
5328 case PM_GLOBAL_VARIABLE_TARGET_NODE:
5329 case PM_INSTANCE_VARIABLE_TARGET_NODE:
5330 case PM_CONSTANT_PATH_TARGET_NODE:
5331 case PM_CALL_TARGET_NODE:
5332 case PM_INDEX_TARGET_NODE: {
5336 DECL_ANCHOR(writes);
5337 DECL_ANCHOR(cleanup);
5341 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
5343 PUSH_GETLOCAL(ret, location, 1, 0);
5346 PUSH_SEQ(ret, writes);
5347 PUSH_SEQ(ret, cleanup);
5349 pm_multi_target_state_update(&state);
5353 case PM_MULTI_TARGET_NODE: {
5354 DECL_ANCHOR(writes);
5355 DECL_ANCHOR(cleanup);
5357 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
5359 LABEL *not_single = NEW_LABEL(location.
line);
5360 LABEL *not_ary = NEW_LABEL(location.
line);
5368 PUSH_GETLOCAL(ret, location, 1, 0);
5369 PUSH_INSN(ret, location, dup);
5370 PUSH_CALL(ret, location, idLength,
INT2FIX(0));
5371 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
5372 PUSH_CALL(ret, location, idEq,
INT2FIX(1));
5373 PUSH_INSNL(ret, location, branchunless, not_single);
5374 PUSH_INSN(ret, location, dup);
5375 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
5376 PUSH_CALL(ret, location, idAREF,
INT2FIX(1));
5377 PUSH_INSN1(ret, location, putobject,
rb_cArray);
5378 PUSH_INSN(ret, location, swap);
5379 PUSH_CALL(ret, location, rb_intern(
"try_convert"),
INT2FIX(1));
5380 PUSH_INSN(ret, location, dup);
5381 PUSH_INSNL(ret, location, branchunless, not_ary);
5382 PUSH_INSN(ret, location, swap);
5384 PUSH_LABEL(ret, not_ary);
5385 PUSH_INSN(ret, location, pop);
5387 PUSH_LABEL(ret, not_single);
5389 if (PM_NODE_TYPE_P(node, PM_SPLAT_NODE)) {
5390 const pm_splat_node_t *cast = (
const pm_splat_node_t *) node;
5394 PUSH_SEQ(ret, writes);
5395 PUSH_SEQ(ret, cleanup);
5399 rb_bug(
"Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5405pm_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)
5409 LABEL *lstart = NEW_LABEL(node_location->
line);
5410 LABEL *lend = NEW_LABEL(node_location->
line);
5411 LABEL *lcont = NEW_LABEL(node_location->
line);
5413 pm_scope_node_t rescue_scope_node;
5414 pm_scope_node_init((
const pm_node_t *) cast->
rescue_clause, &rescue_scope_node, scope_node);
5416 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
5420 pm_node_line_number(parser, (
const pm_node_t *) cast->
rescue_clause)
5423 pm_scope_node_destroy(&rescue_scope_node);
5425 lstart->rescued = LABEL_RESCUE_BEG;
5426 lend->rescued = LABEL_RESCUE_END;
5427 PUSH_LABEL(ret, lstart);
5429 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
5430 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
5433 PM_COMPILE_NOT_POPPED((
const pm_node_t *) cast->
statements);
5437 PUSH_INSN(ret, location, putnil);
5440 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
5441 PUSH_LABEL(ret, lend);
5444 if (!popped) PUSH_INSN(ret, *node_location, pop);
5445 PM_COMPILE((
const pm_node_t *) cast->
else_clause);
5448 PUSH_INSN(ret, *node_location, nop);
5449 PUSH_LABEL(ret, lcont);
5451 if (popped) PUSH_INSN(ret, *node_location, pop);
5452 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
5453 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
5457pm_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)
5463 if (statements != NULL) {
5464 location = PM_NODE_START_LOCATION(parser, statements);
5467 location = *node_location;
5470 LABEL *lstart = NEW_LABEL(location.
line);
5471 LABEL *lend = NEW_LABEL(location.
line);
5472 LABEL *lcont = NEW_LABEL(location.
line);
5479 if (statements != NULL) {
5480 pm_compile_node(iseq, (
const pm_node_t *) statements, ensr,
true, scope_node);
5483 LINK_ELEMENT *last = ensr->last;
5484 bool last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
5489 push_ensure_entry(iseq, &enl, &er, (
void *) cast->
ensure_clause);
5491 PUSH_LABEL(ret, lstart);
5493 pm_compile_rescue(iseq, cast, node_location, ret, popped | last_leave, scope_node);
5496 pm_compile_node(iseq, (
const pm_node_t *) cast->
statements, ret, popped | last_leave, scope_node);
5498 else if (!(popped | last_leave)) {
5499 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
5502 PUSH_LABEL(ret, lend);
5503 PUSH_SEQ(ret, ensr);
5504 if (!popped && last_leave) PUSH_INSN(ret, *node_location, putnil);
5505 PUSH_LABEL(ret, lcont);
5506 if (last_leave) PUSH_INSN(ret, *node_location, pop);
5508 pm_scope_node_t next_scope_node;
5509 pm_scope_node_init((
const pm_node_t *) cast->
ensure_clause, &next_scope_node, scope_node);
5511 rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(
5518 pm_scope_node_destroy(&next_scope_node);
5520 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
5521 if (lstart->link.next != &lend->link) {
5523 PUSH_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, lcont);
5524 erange = erange->next;
5527 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
5535pm_opt_str_freeze_p(
const rb_iseq_t *iseq,
const pm_call_node_t *node)
5538 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5540 PM_NODE_TYPE_P(node->
receiver, PM_STRING_NODE) &&
5542 node->
block == NULL &&
5543 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5552pm_compile_constant_read(rb_iseq_t *iseq,
VALUE name,
const pm_location_t *name_loc, uint32_t node_id, LINK_ANCHOR *
const ret,
const pm_scope_node_t *scope_node)
5554 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, name_loc, node_id);
5556 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
5557 ISEQ_BODY(iseq)->ic_size++;
5558 VALUE segments = rb_ary_new_from_args(1, name);
5559 RB_OBJ_SET_SHAREABLE(segments);
5560 PUSH_INSN1(ret, location, opt_getconstant_path, segments);
5563 PUSH_INSN(ret, location, putnil);
5564 PUSH_INSN1(ret, location, putobject,
Qtrue);
5565 PUSH_INSN1(ret, location, getconstant, name);
5574pm_constant_path_parts(
const pm_node_t *node,
const pm_scope_node_t *scope_node)
5579 switch (PM_NODE_TYPE(node)) {
5580 case PM_CONSTANT_READ_NODE: {
5581 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) node;
5584 rb_ary_unshift(parts, name);
5587 case PM_CONSTANT_PATH_NODE: {
5588 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) node;
5591 rb_ary_unshift(parts, name);
5592 if (cast->
parent == NULL) {
5593 rb_ary_unshift(parts,
ID2SYM(idNULL));
5612pm_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)
5614 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5616 switch (PM_NODE_TYPE(node)) {
5617 case PM_CONSTANT_READ_NODE: {
5618 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) node;
5621 PUSH_INSN1(body, location, putobject,
Qtrue);
5622 PUSH_INSN1(body, location, getconstant, name);
5625 case PM_CONSTANT_PATH_NODE: {
5626 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) node;
5629 if (cast->
parent == NULL) {
5630 PUSH_INSN(body, location, pop);
5631 PUSH_INSN1(body, location, putobject, rb_cObject);
5632 PUSH_INSN1(body, location, putobject,
Qtrue);
5633 PUSH_INSN1(body, location, getconstant, name);
5636 pm_compile_constant_path(iseq, cast->
parent, prefix, body,
false, scope_node);
5637 PUSH_INSN1(body, location, putobject,
Qfalse);
5638 PUSH_INSN1(body, location, getconstant, name);
5643 PM_COMPILE_INTO_ANCHOR(prefix, node);
5652pm_compile_shareable_constant_literal(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_scope_node_t *scope_node)
5654 switch (PM_NODE_TYPE(node)) {
5658 case PM_SYMBOL_NODE:
5659 case PM_REGULAR_EXPRESSION_NODE:
5660 case PM_SOURCE_LINE_NODE:
5661 case PM_INTEGER_NODE:
5663 case PM_RATIONAL_NODE:
5664 case PM_IMAGINARY_NODE:
5665 case PM_SOURCE_ENCODING_NODE:
5666 return pm_static_literal_value(iseq, node, scope_node);
5667 case PM_STRING_NODE:
5668 return parse_static_literal_string(iseq, scope_node, node, &((
const pm_string_node_t *) node)->unescaped);
5669 case PM_SOURCE_FILE_NODE:
5670 return pm_source_file_value((
const pm_source_file_node_t *) node, scope_node);
5671 case PM_ARRAY_NODE: {
5672 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
5675 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5676 VALUE element = pm_compile_shareable_constant_literal(iseq, cast->
elements.
nodes[index], scope_node);
5684 case PM_HASH_NODE: {
5685 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
5688 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5690 if (!PM_NODE_TYPE_P(element, PM_ASSOC_NODE))
return Qundef;
5692 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
5694 VALUE key = pm_compile_shareable_constant_literal(iseq, assoc->
key, scope_node);
5697 VALUE value = pm_compile_shareable_constant_literal(iseq, assoc->
value, scope_node);
5700 rb_hash_aset(result, key, value);
5715pm_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)
5717 VALUE literal = pm_compile_shareable_constant_literal(iseq, node, scope_node);
5719 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5720 PUSH_INSN1(ret, location, putobject, literal);
5724 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5725 switch (PM_NODE_TYPE(node)) {
5726 case PM_ARRAY_NODE: {
5727 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
5730 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5733 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5734 pm_compile_shareable_constant_value(iseq, cast->
elements.
nodes[index], shareability, path, ret, scope_node,
false);
5740 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5741 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5746 case PM_HASH_NODE: {
5747 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
5750 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5753 pm_compile_hash_elements(iseq, (
const pm_node_t *) cast, &cast->elements, shareability, path,
false, ret, scope_node);
5756 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5757 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5763 DECL_ANCHOR(value_seq);
5765 pm_compile_node(iseq, node, value_seq,
false, scope_node);
5766 if (PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) {
5767 PUSH_SEND_WITH_FLAG(value_seq, location, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5770 if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL) {
5771 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5772 PUSH_SEQ(ret, value_seq);
5774 RB_OBJ_SET_SHAREABLE(path);
5776 PUSH_INSN1(ret, location, putobject, path);
5777 PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5779 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) {
5780 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5781 PUSH_SEQ(ret, value_seq);
5782 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5784 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING) {
5785 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5786 PUSH_SEQ(ret, value_seq);
5787 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5800pm_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)
5803 ID name_id = pm_constant_id_lookup(scope_node, node->
name);
5805 if (shareability != 0) {
5806 pm_compile_shareable_constant_value(iseq, node->
value, shareability, rb_id2str(name_id), ret, scope_node,
true);
5809 PM_COMPILE_NOT_POPPED(node->
value);
5812 if (!popped) PUSH_INSN(ret, location, dup);
5813 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5816 PUSH_INSN1(ret, location, setconstant, operand);
5824pm_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)
5829 LABEL *end_label = NEW_LABEL(location.
line);
5831 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5832 if (!popped) PUSH_INSN(ret, location, dup);
5834 PUSH_INSNL(ret, location, branchunless, end_label);
5835 if (!popped) PUSH_INSN(ret, location, pop);
5837 if (shareability != 0) {
5838 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5841 PM_COMPILE_NOT_POPPED(node->
value);
5844 if (!popped) PUSH_INSN(ret, location, dup);
5845 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5846 PUSH_INSN1(ret, location, setconstant, name);
5847 PUSH_LABEL(ret, end_label);
5855pm_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)
5860 LABEL *set_label = NEW_LABEL(location.
line);
5861 LABEL *end_label = NEW_LABEL(location.
line);
5863 PUSH_INSN(ret, location, putnil);
5864 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST), name,
Qtrue);
5865 PUSH_INSNL(ret, location, branchunless, set_label);
5867 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5868 if (!popped) PUSH_INSN(ret, location, dup);
5870 PUSH_INSNL(ret, location, branchif, end_label);
5871 if (!popped) PUSH_INSN(ret, location, pop);
5872 PUSH_LABEL(ret, set_label);
5874 if (shareability != 0) {
5875 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5878 PM_COMPILE_NOT_POPPED(node->
value);
5881 if (!popped) PUSH_INSN(ret, location, dup);
5882 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5883 PUSH_INSN1(ret, location, setconstant, name);
5884 PUSH_LABEL(ret, end_label);
5892pm_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)
5899 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5901 if (shareability != 0) {
5902 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5905 PM_COMPILE_NOT_POPPED(node->
value);
5908 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5909 if (!popped) PUSH_INSN(ret, location, dup);
5911 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5912 PUSH_INSN1(ret, location, setconstant, name);
5920pm_constant_path_path(
const pm_constant_path_node_t *node,
const pm_scope_node_t *scope_node)
5923 rb_ary_push(parts, rb_id2str(pm_constant_id_lookup(scope_node, node->
name)));
5925 const pm_node_t *current = node->
parent;
5926 while (current != NULL && PM_NODE_TYPE_P(current, PM_CONSTANT_PATH_NODE)) {
5927 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) current;
5928 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, cast->
name)));
5932 if (current == NULL) {
5933 rb_ary_unshift(parts, rb_id2str(idNULL));
5935 else if (PM_NODE_TYPE_P(current, PM_CONSTANT_READ_NODE)) {
5936 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, ((
const pm_constant_read_node_t *) current)->name)));
5950pm_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)
5953 const pm_constant_path_node_t *target = node->
target;
5957 PM_COMPILE_NOT_POPPED((
const pm_node_t *) target->
parent);
5960 PUSH_INSN1(ret, location, putobject, rb_cObject);
5963 if (shareability != 0) {
5964 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
5967 PM_COMPILE_NOT_POPPED(node->
value);
5971 PUSH_INSN(ret, location, swap);
5972 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
5975 PUSH_INSN(ret, location, swap);
5976 PUSH_INSN1(ret, location, setconstant, name);
5984pm_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)
5987 const pm_constant_path_node_t *target = node->
target;
5990 LABEL *lfin = NEW_LABEL(location.
line);
5993 PM_COMPILE_NOT_POPPED(target->
parent);
5996 PUSH_INSN1(ret, location, putobject, rb_cObject);
5999 PUSH_INSN(ret, location, dup);
6000 PUSH_INSN1(ret, location, putobject,
Qtrue);
6001 PUSH_INSN1(ret, location, getconstant, name);
6003 if (!popped) PUSH_INSN(ret, location, dup);
6004 PUSH_INSNL(ret, location, branchunless, lfin);
6006 if (!popped) PUSH_INSN(ret, location, pop);
6008 if (shareability != 0) {
6009 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6012 PM_COMPILE_NOT_POPPED(node->
value);
6016 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6019 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
6020 PUSH_INSN(ret, location, swap);
6023 PUSH_INSN1(ret, location, setconstant, name);
6024 PUSH_LABEL(ret, lfin);
6026 if (!popped) PUSH_INSN(ret, location, swap);
6027 PUSH_INSN(ret, location, pop);
6035pm_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)
6038 const pm_constant_path_node_t *target = node->
target;
6041 LABEL *lassign = NEW_LABEL(location.
line);
6042 LABEL *lfin = NEW_LABEL(location.
line);
6045 PM_COMPILE_NOT_POPPED(target->
parent);
6048 PUSH_INSN1(ret, location, putobject, rb_cObject);
6051 PUSH_INSN(ret, location, dup);
6052 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM), name,
Qtrue);
6053 PUSH_INSNL(ret, location, branchunless, lassign);
6055 PUSH_INSN(ret, location, dup);
6056 PUSH_INSN1(ret, location, putobject,
Qtrue);
6057 PUSH_INSN1(ret, location, getconstant, name);
6059 if (!popped) PUSH_INSN(ret, location, dup);
6060 PUSH_INSNL(ret, location, branchif, lfin);
6062 if (!popped) PUSH_INSN(ret, location, pop);
6063 PUSH_LABEL(ret, lassign);
6065 if (shareability != 0) {
6066 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6069 PM_COMPILE_NOT_POPPED(node->
value);
6073 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6076 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
6077 PUSH_INSN(ret, location, swap);
6080 PUSH_INSN1(ret, location, setconstant, name);
6081 PUSH_LABEL(ret, lfin);
6083 if (!popped) PUSH_INSN(ret, location, swap);
6084 PUSH_INSN(ret, location, pop);
6092pm_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)
6095 const pm_constant_path_node_t *target = node->
target;
6101 PM_COMPILE_NOT_POPPED(target->
parent);
6104 PUSH_INSN1(ret, location, putobject, rb_cObject);
6107 PUSH_INSN(ret, location, dup);
6108 PUSH_INSN1(ret, location, putobject,
Qtrue);
6109 PUSH_INSN1(ret, location, getconstant, name);
6111 if (shareability != 0) {
6112 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6115 PM_COMPILE_NOT_POPPED(node->
value);
6118 PUSH_CALL(ret, location, method_id,
INT2FIX(1));
6119 PUSH_INSN(ret, location, swap);
6122 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6123 PUSH_INSN(ret, location, swap);
6126 PUSH_INSN1(ret, location, setconstant, name);
6135#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))
6142pm_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)
6148 pm_parameters_node_t *parameters_node = NULL;
6149 pm_node_list_t *keywords_list = NULL;
6150 pm_node_list_t *optionals_list = NULL;
6151 pm_node_list_t *posts_list = NULL;
6152 pm_node_list_t *requireds_list = NULL;
6153 pm_node_list_t *block_locals = NULL;
6154 bool trailing_comma =
false;
6156 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
6160 if (scope_node->parameters != NULL) {
6161 switch (PM_NODE_TYPE(scope_node->parameters)) {
6162 case PM_BLOCK_PARAMETERS_NODE: {
6163 pm_block_parameters_node_t *cast = (pm_block_parameters_node_t *) scope_node->parameters;
6164 parameters_node = cast->parameters;
6165 block_locals = &cast->locals;
6167 if (parameters_node) {
6168 if (parameters_node->
rest && PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE)) {
6169 trailing_comma =
true;
6174 case PM_PARAMETERS_NODE: {
6175 parameters_node = (pm_parameters_node_t *) scope_node->parameters;
6178 case PM_NUMBERED_PARAMETERS_NODE: {
6179 uint32_t maximum = ((
const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
6180 body->param.lead_num = maximum;
6181 body->param.flags.ambiguous_param0 = maximum == 1;
6184 case PM_IT_PARAMETERS_NODE:
6185 body->param.lead_num = 1;
6186 body->param.flags.ambiguous_param0 =
true;
6189 rb_bug(
"Unexpected node type for parameters: %s", pm_node_type_to_str(PM_NODE_TYPE(scope_node->parameters)));
6193 struct rb_iseq_param_keyword *keyword = NULL;
6195 if (parameters_node) {
6196 optionals_list = ¶meters_node->
optionals;
6197 requireds_list = ¶meters_node->
requireds;
6198 keywords_list = ¶meters_node->
keywords;
6199 posts_list = ¶meters_node->
posts;
6201 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))) {
6202 body->param.opt_num = 0;
6205 body->param.lead_num = 0;
6206 body->param.opt_num = 0;
6212 size_t locals_size = locals->
size;
6215 st_table *index_lookup_table = st_init_numtable();
6217 int table_size = (int) locals_size;
6221 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
6223 if (keywords_list && keywords_list->
size) {
6227 if (requireds_list) {
6228 for (
size_t i = 0; i < requireds_list->
size; i++) {
6232 pm_node_t *required = requireds_list->
nodes[i];
6233 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6236 else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
6237 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6246 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6254 if (optionals_list && optionals_list->
size) {
6255 for (
size_t i = 0; i < optionals_list->
size; i++) {
6256 pm_node_t * node = optionals_list->
nodes[i];
6257 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6267 if (parameters_node) {
6268 if (parameters_node->
rest) {
6269 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6270 if (!((
const pm_rest_parameter_node_t *) parameters_node->
rest)->name || PM_NODE_FLAG_P(parameters_node->
rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6283 if (PM_NODE_TYPE_P(parameters_node->
keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
6285 if (requireds_list->
size == 0 && optionals_list->
size == 0 && keywords_list->
size == 0) {
6286 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
6287 ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
6295 const pm_keyword_rest_parameter_node_t *kw_rest = (
const pm_keyword_rest_parameter_node_t *) parameters_node->
keyword_rest;
6298 if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6306 for (
size_t i = 0; i < posts_list->
size; i++) {
6310 pm_node_t *required = posts_list->
nodes[i];
6311 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6317 if (keywords_list && keywords_list->
size) {
6318 for (
size_t i = 0; i < keywords_list->
size; i++) {
6319 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6320 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6326 if (parameters_node && parameters_node->
block) {
6327 const pm_block_parameter_node_t *block_node = (
const pm_block_parameter_node_t *) parameters_node->
block;
6329 if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
6336 rb_ast_id_table_t *local_table_for_iseq =
ALLOCV(idtmp,
sizeof(rb_ast_id_table_t) + table_size *
sizeof(
ID));
6337 local_table_for_iseq->size = table_size;
6357 int local_index = 0;
6364 if (requireds_list && requireds_list->
size) {
6365 for (
size_t i = 0; i < requireds_list->
size; i++, local_index++) {
6371 pm_node_t *required = requireds_list->
nodes[i];
6373 switch (PM_NODE_TYPE(required)) {
6374 case PM_MULTI_TARGET_NODE: {
6377 local = rb_make_temporary_id(local_index);
6378 local_table_for_iseq->ids[local_index] = local;
6381 case PM_REQUIRED_PARAMETER_NODE: {
6384 const pm_required_parameter_node_t *param = (
const pm_required_parameter_node_t *) required;
6386 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6387 ID local = pm_constant_id_lookup(scope_node, param->
name);
6388 local_table_for_iseq->ids[local_index] = local;
6391 pm_insert_local_index(param->
name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6397 rb_bug(
"Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(required)));
6401 body->param.lead_num = (int) requireds_list->
size;
6402 body->param.flags.has_lead =
true;
6405 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6406 local_table_for_iseq->ids[local_index++] = idItImplicit;
6411 if (optionals_list && optionals_list->
size) {
6412 body->param.opt_num = (int) optionals_list->
size;
6413 body->param.flags.has_opt =
true;
6415 for (
size_t i = 0; i < optionals_list->
size; i++, local_index++) {
6416 pm_node_t * node = optionals_list->
nodes[i];
6417 pm_constant_id_t name = ((
const pm_optional_parameter_node_t *) node)->name;
6419 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6420 ID local = pm_constant_id_lookup(scope_node, name);
6421 local_table_for_iseq->ids[local_index] = local;
6424 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6431 if (parameters_node && parameters_node->
rest) {
6432 body->param.rest_start = local_index;
6436 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6437 body->param.flags.has_rest =
true;
6445 if (PM_NODE_FLAG_P(parameters_node->
rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6446 ID local = pm_constant_id_lookup(scope_node, name);
6447 local_table_for_iseq->ids[local_index] = local;
6450 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6456 body->param.flags.anon_rest =
true;
6457 pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
6466 if (posts_list && posts_list->
size) {
6467 body->param.post_num = (int) posts_list->
size;
6468 body->param.post_start = local_index;
6469 body->param.flags.has_post =
true;
6471 for (
size_t i = 0; i < posts_list->
size; i++, local_index++) {
6477 const pm_node_t *post_node = posts_list->
nodes[i];
6479 switch (PM_NODE_TYPE(post_node)) {
6480 case PM_MULTI_TARGET_NODE: {
6483 local = rb_make_temporary_id(local_index);
6484 local_table_for_iseq->ids[local_index] = local;
6487 case PM_REQUIRED_PARAMETER_NODE: {
6490 const pm_required_parameter_node_t *param = (
const pm_required_parameter_node_t *) post_node;
6492 if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6493 ID local = pm_constant_id_lookup(scope_node, param->
name);
6494 local_table_for_iseq->ids[local_index] = local;
6497 pm_insert_local_index(param->
name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6502 rb_bug(
"Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(post_node)));
6510 if (keywords_list && keywords_list->
size) {
6511 keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6512 keyword->num = (int) keywords_list->
size;
6517 for (
size_t i = 0; i < keywords_list->
size; i++) {
6518 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6523 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
6524 name = ((
const pm_required_keyword_parameter_node_t *) keyword_parameter_node)->name;
6525 keyword->required_num++;
6526 ID local = pm_constant_id_lookup(scope_node, name);
6528 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6529 local_table_for_iseq->ids[local_index] = local;
6532 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6538 for (
size_t i = 0; i < keywords_list->
size; i++) {
6539 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6544 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
6545 const pm_optional_keyword_parameter_node_t *cast = ((
const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6547 pm_node_t *value = cast->
value;
6550 if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !PM_CONTAINER_P(value)) {
6551 rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
6557 ID local = pm_constant_id_lookup(scope_node, name);
6558 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6559 local_table_for_iseq->ids[local_index] = local;
6562 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6572 for (
int i = 0; i <
RARRAY_LEN(default_values); i++) {
6574 if (dv == complex_mark) dv =
Qundef;
6578 keyword->default_values = dvs;
6582 keyword->bits_start = local_index;
6583 ID local = rb_make_temporary_id(local_index);
6584 local_table_for_iseq->ids[local_index] = local;
6587 body->param.keyword = keyword;
6588 body->param.flags.has_kw =
true;
6591 if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->
size == 1 && !trailing_comma) {
6592 body->param.flags.ambiguous_param0 =
true;
6595 if (parameters_node) {
6600 case PM_NO_KEYWORDS_PARAMETER_NODE: {
6603 body->param.flags.accepts_no_kwarg =
true;
6606 case PM_KEYWORD_REST_PARAMETER_NODE: {
6609 const pm_keyword_rest_parameter_node_t *kw_rest_node = (
const pm_keyword_rest_parameter_node_t *) parameters_node->
keyword_rest;
6610 if (!body->param.flags.has_kw) {
6611 body->param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6614 keyword->rest_start = local_index;
6615 body->param.flags.has_kwrest =
true;
6619 if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6620 ID local = pm_constant_id_lookup(scope_node, constant_id);
6621 local_table_for_iseq->ids[local_index] = local;
6624 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6628 body->param.flags.anon_kwrest =
true;
6629 pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
6635 case PM_FORWARDING_PARAMETER_NODE: {
6638 if (!ISEQ_BODY(iseq)->param.
flags.forwardable) {
6640 body->param.rest_start = local_index;
6641 body->param.flags.has_rest =
true;
6642 body->param.flags.anon_rest =
true;
6643 pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
6647 body->param.flags.has_kw =
false;
6648 body->param.flags.has_kwrest =
true;
6649 body->param.flags.anon_kwrest =
true;
6650 body->param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6651 keyword->rest_start = local_index;
6652 pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
6655 body->param.block_start = local_index;
6656 body->param.flags.has_block =
true;
6657 pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
6661 pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
6665 rb_bug(
"node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->
keyword_rest)));
6671 if (parameters_node->
block) {
6672 body->param.block_start = local_index;
6673 body->param.flags.has_block =
true;
6674 iseq_set_use_block(iseq);
6679 if (PM_NODE_FLAG_P(parameters_node->
block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6680 ID local = pm_constant_id_lookup(scope_node, name);
6681 local_table_for_iseq->ids[local_index] = local;
6684 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6688 pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
6707 if (requireds_list && requireds_list->
size) {
6708 for (
size_t i = 0; i < requireds_list->
size; i++) {
6712 const pm_node_t *required = requireds_list->
nodes[i];
6714 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6715 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);
6721 if (posts_list && posts_list->
size) {
6722 for (
size_t i = 0; i < posts_list->
size; i++) {
6726 const pm_node_t *post = posts_list->
nodes[i];
6728 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6729 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);
6735 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6736 if (PM_NODE_TYPE_P(((
const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
6737 body->param.lead_num++;
6740 body->param.rest_start = local_index;
6741 body->param.flags.has_rest =
true;
6744 ID local = rb_make_temporary_id(local_index);
6745 local_table_for_iseq->ids[local_index] = local;
6750 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
6751 int maximum = ((
const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
6753 for (
int i = 0; i < maximum; i++, local_index++) {
6754 const uint8_t param_name[] = {
'_',
'1' + i };
6756 RUBY_ASSERT(constant_id &&
"parser should fill in any gaps in numbered parameters");
6757 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6759 body->param.lead_num = maximum;
6760 body->param.flags.has_lead =
true;
6764 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6765 body->param.lead_num = 1;
6766 body->param.flags.has_lead =
true;
6777 if (block_locals && block_locals->
size) {
6778 for (
size_t i = 0; i < block_locals->
size; i++, local_index++) {
6779 pm_constant_id_t constant_id = ((
const pm_block_local_variable_node_t *) block_locals->
nodes[i])->name;
6780 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6785 if (scope_node->locals.
size) {
6786 for (
size_t i = 0; i < scope_node->locals.
size; i++) {
6790 ctx.scope_node = scope_node;
6791 ctx.local_table_for_iseq = local_table_for_iseq;
6792 ctx.local_index = local_index;
6794 st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
6796 local_index = ctx.local_index;
6805 if (scope_node->index_lookup_table) {
6806 st_free_table(scope_node->index_lookup_table);
6808 scope_node->index_lookup_table = index_lookup_table;
6809 iseq_calc_param_size(iseq);
6811 if (ISEQ_BODY(iseq)->param.
flags.forwardable) {
6814 ISEQ_BODY(iseq)->param.size += 1;
6818 iseq_set_local_table(iseq, local_table_for_iseq, 0);
6819 iseq_set_parameters_lvar_state(iseq);
6821 scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
6823 if (keyword != NULL) {
6824 size_t keyword_start_index = keyword->bits_start - keyword->num;
6825 keyword->table = (
ID *)&ISEQ_BODY(iseq)->local_table[keyword_start_index];
6830 if (optionals_list && optionals_list->
size) {
6838 for (
size_t i = 0; i < optionals_list->
size; i++) {
6839 label = NEW_LABEL(location.
line);
6840 opt_table[i] = label;
6841 PUSH_LABEL(ret, label);
6842 pm_node_t *optional_node = optionals_list->
nodes[i];
6843 PM_COMPILE_NOT_POPPED(optional_node);
6847 label = NEW_LABEL(location.
line);
6848 opt_table[optionals_list->
size] = label;
6849 PUSH_LABEL(ret, label);
6851 body->param.opt_table = (
const VALUE *) opt_table;
6854 if (keywords_list && keywords_list->
size) {
6855 size_t optional_index = 0;
6856 for (
size_t i = 0; i < keywords_list->
size; i++) {
6857 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6860 switch (PM_NODE_TYPE(keyword_parameter_node)) {
6861 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
6864 const pm_optional_keyword_parameter_node_t *cast = ((
const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6866 pm_node_t *value = cast->
value;
6869 if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_CONTAINER_P(value)) {
6870 LABEL *end_label = NEW_LABEL(location.
line);
6872 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
6873 int kw_bits_idx = table_size - body->param.keyword->bits_start;
6874 PUSH_INSN2(ret, location, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(optional_index));
6875 PUSH_INSNL(ret, location, branchif, end_label);
6877 PUSH_SETLOCAL(ret, location, index.index, index.level);
6878 PUSH_LABEL(ret, end_label);
6883 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
6888 rb_bug(
"Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
6893 if (requireds_list && requireds_list->
size) {
6894 for (
size_t i = 0; i < requireds_list->
size; i++) {
6898 const pm_node_t *required = requireds_list->
nodes[i];
6900 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6901 PUSH_GETLOCAL(ret, location, table_size - (
int)i, 0);
6902 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) required, ret, scope_node);
6907 if (posts_list && posts_list->
size) {
6908 for (
size_t i = 0; i < posts_list->
size; i++) {
6912 const pm_node_t *post = posts_list->
nodes[i];
6914 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6915 PUSH_GETLOCAL(ret, location, table_size - body->param.post_start - (
int) i, 0);
6916 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) post, ret, scope_node);
6921 switch (body->type) {
6922 case ISEQ_TYPE_PLAIN: {
6923 RUBY_ASSERT(PM_NODE_TYPE_P(scope_node->ast_node, PM_INTERPOLATED_REGULAR_EXPRESSION_NODE));
6925 const pm_interpolated_regular_expression_node_t *cast = (
const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
6926 pm_compile_regexp_dynamic(iseq, (
const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6930 case ISEQ_TYPE_BLOCK: {
6931 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
6932 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
6933 const pm_node_location_t block_location = { .line = body->location.first_lineno, .node_id = scope_node->ast_node->
node_id };
6935 start->rescued = LABEL_RESCUE_BEG;
6936 end->rescued = LABEL_RESCUE_END;
6942 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6943 pm_compile_for_node_index(iseq, ((
const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
6947 PUSH_INSN(ret, block_location, nop);
6948 PUSH_LABEL(ret, start);
6950 if (scope_node->body != NULL) {
6951 switch (PM_NODE_TYPE(scope_node->ast_node)) {
6952 case PM_POST_EXECUTION_NODE: {
6953 const pm_post_execution_node_t *cast = (
const pm_post_execution_node_t *) scope_node->ast_node;
6954 PUSH_INSN1(ret, block_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6957 pm_scope_node_t next_scope_node;
6958 pm_scope_node_init((
const pm_node_t *) cast->statements, &next_scope_node, scope_node);
6960 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.
line);
6961 pm_scope_node_destroy(&next_scope_node);
6963 PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe,
INT2FIX(0), block);
6966 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
6967 const pm_interpolated_regular_expression_node_t *cast = (
const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
6968 pm_compile_regexp_dynamic(iseq, (
const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6972 pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
6977 PUSH_INSN(ret, block_location, putnil);
6980 PUSH_LABEL(ret, end);
6982 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
6985 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
6986 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
6989 case ISEQ_TYPE_ENSURE: {
6990 const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->parser, scope_node->body) : location);
6991 iseq_set_exception_local_table(iseq);
6993 if (scope_node->body != NULL) {
6994 PM_COMPILE_POPPED((
const pm_node_t *) scope_node->body);
6997 PUSH_GETLOCAL(ret, statements_location, 1, 0);
6998 PUSH_INSN1(ret, statements_location,
throw,
INT2FIX(0));
7001 case ISEQ_TYPE_METHOD: {
7002 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
7005 if (scope_node->body) {
7006 PM_COMPILE((
const pm_node_t *) scope_node->body);
7009 PUSH_INSN(ret, location, putnil);
7012 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
7015 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
7018 case ISEQ_TYPE_RESCUE: {
7019 iseq_set_exception_local_table(iseq);
7020 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
7021 LABEL *lab = NEW_LABEL(location.
line);
7022 LABEL *rescue_end = NEW_LABEL(location.
line);
7023 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7025 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7026 PUSH_INSNL(ret, location, branchif, lab);
7027 PUSH_INSNL(ret, location, jump, rescue_end);
7028 PUSH_LABEL(ret, lab);
7030 PM_COMPILE((
const pm_node_t *) scope_node->body);
7031 PUSH_INSN(ret, location, leave);
7032 PUSH_LABEL(ret, rescue_end);
7033 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7036 PM_COMPILE((
const pm_node_t *) scope_node->ast_node);
7038 PUSH_INSN1(ret, location,
throw,
INT2FIX(0));
7043 if (scope_node->body) {
7044 PM_COMPILE((
const pm_node_t *) scope_node->body);
7047 PUSH_INSN(ret, location, putnil);
7052 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
7053 const pm_node_location_t end_location = PM_NODE_END_LOCATION(scope_node->parser, scope_node->ast_node);
7055 ISEQ_COMPILE_DATA(iseq)->last_line = end_location.
line;
7058 if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
7059 const pm_node_location_t location = { .line = ISEQ_COMPILE_DATA(iseq)->last_line, .node_id = scope_node->ast_node->
node_id };
7060 PUSH_INSN(ret, location, leave);
7065pm_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)
7069 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7073 VALUE operand =
ID2SYM(rb_intern3((
const char *) name_loc->
start, name_loc->
end - name_loc->
start, scope_node->encoding));
7074 PUSH_INSN1(ret, *location, putobject, operand);
7079 VALUE operand =
ID2SYM(rb_intern3((
const char *) name_loc->
start, name_loc->
end - name_loc->
start, scope_node->encoding));
7080 PUSH_INSN1(ret, *location, putobject, operand);
7083 PUSH_SEND(ret, *location, id_core_set_variable_alias,
INT2FIX(2));
7084 if (popped) PUSH_INSN(ret, *location, pop);
7088pm_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)
7090 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7091 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
7092 PM_COMPILE_NOT_POPPED(node->
new_name);
7093 PM_COMPILE_NOT_POPPED(node->
old_name);
7095 PUSH_SEND(ret, *location, id_core_set_method_alias,
INT2FIX(3));
7096 if (popped) PUSH_INSN(ret, *location, pop);
7100pm_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)
7102 LABEL *end_label = NEW_LABEL(location->
line);
7104 PM_COMPILE_NOT_POPPED(node->
left);
7105 if (!popped) PUSH_INSN(ret, *location, dup);
7106 PUSH_INSNL(ret, *location, branchunless, end_label);
7108 if (!popped) PUSH_INSN(ret, *location, pop);
7109 PM_COMPILE(node->
right);
7110 PUSH_LABEL(ret, end_label);
7114pm_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)
7118 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
7123 if (elements->
size) {
7124 VALUE value = pm_static_literal_value(iseq, node, scope_node);
7125 RB_OBJ_SET_FROZEN_SHAREABLE(value);
7126 PUSH_INSN1(ret, *location, duparray, value);
7129 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7147 const int max_new_array_size = 0x100;
7148 const unsigned int min_tmp_array_size = 0x40;
7150 int new_array_size = 0;
7151 bool first_chunk =
true;
7157 bool static_literal =
false;
7160#define FLUSH_CHUNK \
7161 if (new_array_size) { \
7162 if (first_chunk) PUSH_INSN1(ret, *location, newarray, INT2FIX(new_array_size)); \
7163 else PUSH_INSN1(ret, *location, pushtoarray, INT2FIX(new_array_size)); \
7164 first_chunk = false; \
7165 new_array_size = 0; \
7168 for (
size_t index = 0; index < elements->
size; index++) {
7169 const pm_node_t *element = elements->
nodes[index];
7171 if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
7174 const pm_splat_node_t *splat_element = (
const pm_splat_node_t *) element;
7176 PM_COMPILE_NOT_POPPED(splat_element->
expression);
7179 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
7180 PUSH_GETLOCAL(ret, *location, index.index, index.level);
7186 PUSH_INSN1(ret, *location, splatarray,
Qtrue);
7187 first_chunk =
false;
7190 PUSH_INSN(ret, *location, concattoarray);
7193 static_literal =
false;
7195 else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
7196 if (new_array_size == 0 && first_chunk) {
7197 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7198 first_chunk =
false;
7214 const pm_keyword_hash_node_t *keyword_hash = (
const pm_keyword_hash_node_t *) element;
7215 pm_compile_hash_elements(iseq, element, &keyword_hash->
elements, 0,
Qundef,
false, ret, scope_node);
7222 while (splats < keyword_hash->elements.
size && PM_NODE_TYPE_P(keyword_hash->
elements.
nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
7225 PUSH_INSN(ret, *location, pushtoarraykwsplat);
7232 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) &&
7233 !PM_CONTAINER_P(element) &&
7235 ((index + min_tmp_array_size) < elements->
size)
7240 size_t right_index = index + 1;
7242 right_index < elements->size &&
7243 PM_NODE_FLAG_P(elements->
nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL) &&
7244 !PM_CONTAINER_P(elements->
nodes[right_index])
7247 size_t tmp_array_size = right_index - index;
7248 if (tmp_array_size >= min_tmp_array_size) {
7252 for (; tmp_array_size; tmp_array_size--)
7253 rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->
nodes[index++], scope_node));
7256 RB_OBJ_SET_FROZEN_SHAREABLE(tmp_array);
7261 PUSH_INSN1(ret, *location, duparray, tmp_array);
7262 first_chunk =
false;
7265 PUSH_INSN1(ret, *location, putobject, tmp_array);
7266 PUSH_INSN(ret, *location, concattoarray);
7270 PM_COMPILE_NOT_POPPED(element);
7271 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7272 static_literal =
true;
7275 PM_COMPILE_NOT_POPPED(element);
7276 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7277 static_literal =
false;
7282 if (popped) PUSH_INSN(ret, *location, pop);
7288pm_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)
7290 unsigned long throw_flag = 0;
7292 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7294 LABEL *splabel = NEW_LABEL(0);
7295 PUSH_LABEL(ret, splabel);
7296 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7299 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
7302 PUSH_INSN(ret, *location, putnil);
7305 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7306 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7307 PUSH_ADJUST_RESTORE(ret, splabel);
7308 if (!popped) PUSH_INSN(ret, *location, putnil);
7311 const rb_iseq_t *ip = iseq;
7314 if (!ISEQ_COMPILE_DATA(ip)) {
7319 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7320 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7322 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7325 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7326 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7330 ip = ISEQ_BODY(ip)->parent_iseq;
7336 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
7339 PUSH_INSN(ret, *location, putnil);
7342 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_BREAK));
7343 if (popped) PUSH_INSN(ret, *location, pop);
7348 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7353pm_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)
7355 ID method_id = pm_constant_id_lookup(scope_node, node->
name);
7361 const char *builtin_func;
7363 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, node->
receiver, method_id)) != NULL) {
7364 pm_compile_builtin_function_call(iseq, ret, scope_node, node, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
7368 LABEL *start = NEW_LABEL(location.
line);
7369 if (node->
block) PUSH_LABEL(ret, start);
7371 switch (method_id) {
7373 if (pm_opt_str_freeze_p(iseq, node)) {
7374 VALUE value = parse_static_literal_string(iseq, scope_node, node->
receiver, &((
const pm_string_node_t * ) node->
receiver)->unescaped);
7375 const struct rb_callinfo *callinfo = new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE);
7376 PUSH_INSN2(ret, location, opt_str_uminus, value, callinfo);
7377 if (popped) PUSH_INSN(ret, location, pop);
7383 if (pm_opt_str_freeze_p(iseq, node)) {
7384 VALUE value = parse_static_literal_string(iseq, scope_node, node->
receiver, &((
const pm_string_node_t * ) node->
receiver)->unescaped);
7385 const struct rb_callinfo *callinfo = new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE);
7386 PUSH_INSN2(ret, location, opt_str_freeze, value, callinfo);
7387 if (popped) PUSH_INSN(ret, location, pop);
7394 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
7395 PUSH_INSN(ret, location, putnil);
7399 PUSH_INSN(ret, location, putself);
7402 if (method_id == idCall && PM_NODE_TYPE_P(node->
receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
7403 const pm_local_variable_read_node_t *read_node_cast = (
const pm_local_variable_read_node_t *) node->
receiver;
7407 if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
7408 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)));
7411 PM_COMPILE_NOT_POPPED(node->
receiver);
7415 PM_COMPILE_NOT_POPPED(node->
receiver);
7419 pm_compile_call(iseq, node, ret, popped, scope_node, method_id, start);
7424pm_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)
7428 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
7429 flag = VM_CALL_FCALL;
7432 PM_COMPILE_NOT_POPPED(node->
receiver);
7434 LABEL *safe_label = NULL;
7435 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
7436 safe_label = NEW_LABEL(location->
line);
7437 PUSH_INSN(ret, *location, dup);
7438 PUSH_INSNL(ret, *location, branchnil, safe_label);
7441 PUSH_INSN(ret, *location, dup);
7443 ID id_read_name = pm_constant_id_lookup(scope_node, node->
read_name);
7444 PUSH_SEND_WITH_FLAG(ret, *location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
7446 PM_COMPILE_NOT_POPPED(node->
value);
7448 PUSH_SEND(ret, *location, id_operator,
INT2FIX(1));
7451 PUSH_INSN(ret, *location, swap);
7452 PUSH_INSN1(ret, *location, topn,
INT2FIX(1));
7455 ID id_write_name = pm_constant_id_lookup(scope_node, node->
write_name);
7456 PUSH_SEND_WITH_FLAG(ret, *location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
7458 if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
7459 PUSH_INSN(ret, *location, pop);
7460 if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
7480pm_compile_case_node_dispatch(rb_iseq_t *iseq,
VALUE dispatch,
const pm_node_t *node, LABEL *label,
const pm_scope_node_t *scope_node)
7483 switch (PM_NODE_TYPE(node)) {
7484 case PM_FLOAT_NODE: {
7485 key = pm_static_literal_value(iseq, node, scope_node);
7489 key = (
FIXABLE(intptr) ?
LONG2FIX((
long) intptr) : rb_dbl2big(intptr));
7495 case PM_INTEGER_NODE:
7497 case PM_SOURCE_FILE_NODE:
7498 case PM_SOURCE_LINE_NODE:
7499 case PM_SYMBOL_NODE:
7501 key = pm_static_literal_value(iseq, node, scope_node);
7503 case PM_STRING_NODE: {
7504 const pm_string_node_t *cast = (
const pm_string_node_t *) node;
7505 key = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
7512 if (
NIL_P(rb_hash_lookup(dispatch, key))) {
7513 rb_hash_aset(dispatch, key, ((
VALUE) label) | 1);
7522pm_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)
7526 const pm_node_list_t *conditions = &cast->
conditions;
7531 DECL_ANCHOR(cond_seq);
7536 DECL_ANCHOR(body_seq);
7540 LABEL *end_label = NEW_LABEL(location.
line);
7548 rb_code_location_t case_location = { 0 };
7551 if (PM_BRANCH_COVERAGE_P(iseq)) {
7552 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7553 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location,
"case");
7559 for (
size_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7560 const pm_when_node_t *clause = (
const pm_when_node_t *) conditions->
nodes[clause_index];
7561 const pm_node_list_t *conditions = &clause->conditions;
7563 int clause_lineno = pm_node_line_number(parser, (
const pm_node_t *) clause);
7564 LABEL *label = NEW_LABEL(clause_lineno);
7565 PUSH_LABEL(body_seq, label);
7568 if (PM_BRANCH_COVERAGE_P(iseq)) {
7569 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));
7570 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7573 if (clause->statements != NULL) {
7574 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7577 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7580 PUSH_INSNL(body_seq, location, jump, end_label);
7585 for (
size_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7586 const pm_node_t *condition = conditions->
nodes[condition_index];
7588 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7590 PUSH_INSN(cond_seq, cond_location, putnil);
7591 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7592 PUSH_INSN1(cond_seq, cond_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7593 PUSH_INSNL(cond_seq, cond_location, branchif, label);
7596 LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
7597 pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label, scope_node);
7598 PUSH_LABEL(cond_seq, next_label);
7605 if (PM_BRANCH_COVERAGE_P(iseq)) {
7606 rb_code_location_t branch_location;
7609 branch_location = case_location;
7611 branch_location = pm_code_location(scope_node, (
const pm_node_t *) cast->
else_clause);
7616 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7621 pm_compile_node(iseq, (
const pm_node_t *) cast->
else_clause, cond_seq, popped, scope_node);
7624 PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
7629 PUSH_INSNL(cond_seq, location, jump, end_label);
7630 PUSH_SEQ(ret, cond_seq);
7635 rb_code_location_t case_location = { 0 };
7638 if (PM_BRANCH_COVERAGE_P(iseq)) {
7639 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7640 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location,
"case");
7645 LABEL *else_label = NEW_LABEL(location.
line);
7653 if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7655 RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
7666 for (
size_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7667 const pm_when_node_t *clause = (
const pm_when_node_t *) conditions->
nodes[clause_index];
7668 pm_node_location_t clause_location = PM_NODE_START_LOCATION(parser, (
const pm_node_t *) clause);
7670 const pm_node_list_t *conditions = &clause->conditions;
7671 LABEL *label = NEW_LABEL(clause_location.
line);
7676 for (
size_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7677 const pm_node_t *condition = conditions->
nodes[condition_index];
7678 const pm_node_location_t condition_location = PM_NODE_START_LOCATION(parser, condition);
7683 if (dispatch !=
Qundef) {
7684 dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
7687 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7688 PUSH_INSN(cond_seq, condition_location, dup);
7689 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7690 PUSH_INSN1(cond_seq, condition_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
7693 if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
7694 const pm_string_node_t *
string = (
const pm_string_node_t *) condition;
7695 VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->
unescaped);
7696 PUSH_INSN1(cond_seq, condition_location, putobject, value);
7699 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7702 PUSH_INSN1(cond_seq, condition_location, topn,
INT2FIX(1));
7703 PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
7706 PUSH_INSNL(cond_seq, condition_location, branchif, label);
7713 PUSH_LABEL(body_seq, label);
7714 PUSH_INSN(body_seq, clause_location, pop);
7717 if (PM_BRANCH_COVERAGE_P(iseq)) {
7718 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));
7719 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7722 if (clause->statements != NULL) {
7723 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7726 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7729 PUSH_INSNL(body_seq, clause_location, jump, end_label);
7740 if (dispatch !=
Qundef) {
7741 PUSH_INSN(ret, location, dup);
7742 RB_OBJ_SET_SHAREABLE(dispatch);
7743 PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
7744 LABEL_REF(else_label);
7747 PUSH_SEQ(ret, cond_seq);
7751 PUSH_LABEL(ret, else_label);
7755 PUSH_INSN(ret, else_location, pop);
7758 if (PM_BRANCH_COVERAGE_P(iseq)) {
7760 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7763 PM_COMPILE((
const pm_node_t *) cast->
else_clause);
7764 PUSH_INSNL(ret, else_location, jump, end_label);
7767 PUSH_INSN(ret, location, pop);
7770 if (PM_BRANCH_COVERAGE_P(iseq)) {
7771 add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
7774 if (!popped) PUSH_INSN(ret, location, putnil);
7775 PUSH_INSNL(ret, location, jump, end_label);
7779 PUSH_SEQ(ret, body_seq);
7780 PUSH_LABEL(ret, end_label);
7784pm_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)
7789 DECL_ANCHOR(body_seq);
7794 DECL_ANCHOR(cond_seq);
7798 LABEL *end_label = NEW_LABEL(location->
line);
7803 LABEL *else_label = NEW_LABEL(location->
line);
7807 rb_code_location_t case_location = { 0 };
7811 if (PM_BRANCH_COVERAGE_P(iseq)) {
7812 case_location = pm_code_location(scope_node, (
const pm_node_t *) node);
7813 branches = decl_branch_base(iseq, PTR2NUM(node), &case_location,
"case");
7823 if (in_single_pattern) {
7824 PUSH_INSN(ret, *location, putnil);
7825 PUSH_INSN(ret, *location, putnil);
7826 PUSH_INSN1(ret, *location, putobject,
Qfalse);
7827 PUSH_INSN(ret, *location, putnil);
7831 PUSH_INSN(ret, *location, putnil);
7840 RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
7842 const pm_in_node_t *in_node = (
const pm_in_node_t *) condition;
7843 const pm_node_location_t in_location = PM_NODE_START_LOCATION(scope_node->parser, in_node);
7847 PUSH_INSN(body_seq, in_location, putnil);
7850 LABEL *body_label = NEW_LABEL(in_location.
line);
7851 PUSH_LABEL(body_seq, body_label);
7852 PUSH_INSN1(body_seq, in_location, adjuststack,
INT2FIX(in_single_pattern ? 6 : 2));
7855 if (PM_BRANCH_COVERAGE_P(iseq)) {
7856 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));
7857 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"in", branches);
7861 PM_COMPILE_INTO_ANCHOR(body_seq, (
const pm_node_t *) in_node->
statements);
7864 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7867 PUSH_INSNL(body_seq, in_location, jump, end_label);
7868 LABEL *next_pattern_label = NEW_LABEL(pattern_location.
line);
7870 PUSH_INSN(cond_seq, pattern_location, dup);
7871 pm_compile_pattern(iseq, scope_node, in_node->
pattern, cond_seq, body_label, next_pattern_label, in_single_pattern,
true, 2);
7872 PUSH_LABEL(cond_seq, next_pattern_label);
7873 LABEL_UNREMOVABLE(next_pattern_label);
7880 const pm_else_node_t *else_node = node->
else_clause;
7882 PUSH_LABEL(cond_seq, else_label);
7883 PUSH_INSN(cond_seq, *location, pop);
7884 PUSH_INSN(cond_seq, *location, pop);
7887 if (PM_BRANCH_COVERAGE_P(iseq)) {
7888 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));
7889 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7892 PM_COMPILE_INTO_ANCHOR(cond_seq, (
const pm_node_t *) else_node);
7893 PUSH_INSNL(cond_seq, *location, jump, end_label);
7894 PUSH_INSN(cond_seq, *location, putnil);
7895 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7900 PUSH_LABEL(cond_seq, else_label);
7903 add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
7905 if (in_single_pattern) {
7906 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, cond_seq, end_label, popped);
7909 PUSH_INSN1(cond_seq, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7911 PUSH_INSN1(cond_seq, *location, topn,
INT2FIX(2));
7912 PUSH_SEND(cond_seq, *location, id_core_raise,
INT2FIX(2));
7914 PUSH_INSN1(cond_seq, *location, adjuststack,
INT2FIX(3));
7915 if (!popped) PUSH_INSN(cond_seq, *location, putnil);
7916 PUSH_INSNL(cond_seq, *location, jump, end_label);
7917 PUSH_INSN1(cond_seq, *location, dupn,
INT2FIX(1));
7918 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7925 PUSH_SEQ(ret, cond_seq);
7926 PUSH_SEQ(ret, body_seq);
7927 PUSH_LABEL(ret, end_label);
7931pm_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)
7933 const rb_iseq_t *block = NULL;
7934 const rb_iseq_t *previous_block = NULL;
7935 LABEL *retry_label = NULL;
7936 LABEL *retry_end_l = NULL;
7938 if (node->
block != NULL) {
7939 previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
7940 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
7942 retry_label = NEW_LABEL(location->
line);
7943 retry_end_l = NEW_LABEL(location->
line);
7945 PUSH_LABEL(ret, retry_label);
7948 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
7951 PUSH_INSN(ret, *location, putself);
7952 int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
7954 if (node->
block != NULL) {
7955 pm_scope_node_t next_scope_node;
7956 pm_scope_node_init((
const pm_node_t *) node->
block, &next_scope_node, scope_node);
7958 ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
7959 pm_scope_node_destroy(&next_scope_node);
7966 const rb_iseq_t *local_iseq = body->local_iseq;
7970 int depth = get_lvar_level(iseq);
7972 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.
flags.forwardable) {
7973 flag |= VM_CALL_FORWARDING;
7974 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
7975 PUSH_GETLOCAL(ret, *location, mult_local.index, mult_local.level);
7977 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, 0, flag, NULL, block != NULL);
7978 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, block);
7980 if (popped) PUSH_INSN(ret, *location, pop);
7982 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
7987 if (local_body->param.flags.has_lead) {
7989 for (
int i = 0; i < local_body->param.lead_num; i++) {
7990 int idx = local_body->local_table_size - i;
7991 PUSH_GETLOCAL(args, *location, idx, depth);
7993 argc += local_body->param.lead_num;
7996 if (local_body->param.flags.has_opt) {
7998 for (
int j = 0; j < local_body->param.opt_num; j++) {
7999 int idx = local_body->local_table_size - (argc + j);
8000 PUSH_GETLOCAL(args, *location, idx, depth);
8002 argc += local_body->param.opt_num;
8005 if (local_body->param.flags.has_rest) {
8007 int idx = local_body->local_table_size - local_body->param.rest_start;
8008 PUSH_GETLOCAL(args, *location, idx, depth);
8009 PUSH_INSN1(args, *location, splatarray,
Qfalse);
8011 argc = local_body->param.rest_start + 1;
8012 flag |= VM_CALL_ARGS_SPLAT;
8015 if (local_body->param.flags.has_post) {
8017 int post_len = local_body->param.post_num;
8018 int post_start = local_body->param.post_start;
8021 for (; j < post_len; j++) {
8022 int idx = local_body->local_table_size - (post_start + j);
8023 PUSH_GETLOCAL(args, *location, idx, depth);
8026 if (local_body->param.flags.has_rest) {
8028 PUSH_INSN1(args, *location, newarray,
INT2FIX(j));
8029 PUSH_INSN(args, *location, concatarray);
8032 argc = post_len + post_start;
8036 const struct rb_iseq_param_keyword *
const local_keyword = local_body->param.keyword;
8037 if (local_body->param.flags.has_kw) {
8038 int local_size = local_body->local_table_size;
8041 PUSH_INSN1(args, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8043 if (local_body->param.flags.has_kwrest) {
8044 int idx = local_body->local_table_size - local_keyword->rest_start;
8045 PUSH_GETLOCAL(args, *location, idx, depth);
8047 PUSH_SEND(args, *location, rb_intern(
"dup"),
INT2FIX(0));
8050 PUSH_INSN1(args, *location, newhash,
INT2FIX(0));
8053 for (; i < local_keyword->num; ++i) {
8054 ID id = local_keyword->table[i];
8055 int idx = local_size - get_local_var_idx(local_iseq,
id);
8059 PUSH_INSN1(args, *location, putobject, operand);
8062 PUSH_GETLOCAL(args, *location, idx, depth);
8065 PUSH_SEND(args, *location, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
8066 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
8068 else if (local_body->param.flags.has_kwrest) {
8069 int idx = local_body->local_table_size - local_keyword->rest_start;
8070 PUSH_GETLOCAL(args, *location, idx, depth);
8072 flag |= VM_CALL_KW_SPLAT;
8075 PUSH_SEQ(ret, args);
8078 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flag, NULL, block != NULL);
8079 PUSH_INSN2(ret, *location, invokesuper, callinfo, block);
8082 if (node->
block != NULL) {
8083 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8084 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
8085 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8088 if (popped) PUSH_INSN(ret, *location, pop);
8092pm_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)
8094 LABEL *matched_label = NEW_LABEL(location->
line);
8095 LABEL *unmatched_label = NEW_LABEL(location->
line);
8096 LABEL *done_label = NEW_LABEL(location->
line);
8100 PUSH_INSN(ret, *location, putnil);
8101 PUSH_INSN(ret, *location, putnil);
8102 PUSH_INSN1(ret, *location, putobject,
Qfalse);
8103 PUSH_INSN(ret, *location, putnil);
8104 PUSH_INSN(ret, *location, putnil);
8108 PM_COMPILE_NOT_POPPED(node->
value);
8112 PUSH_INSN(ret, *location, dup);
8119 pm_compile_pattern(iseq, scope_node, node->
pattern, ret, matched_label, unmatched_label,
true,
true, 2);
8124 PUSH_LABEL(ret, unmatched_label);
8125 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, ret, done_label, popped);
8129 PUSH_LABEL(ret, matched_label);
8130 PUSH_INSN1(ret, *location, adjuststack,
INT2FIX(6));
8131 if (!popped) PUSH_INSN(ret, *location, putnil);
8132 PUSH_INSNL(ret, *location, jump, done_label);
8134 PUSH_LABEL(ret, done_label);
8138pm_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)
8140 LABEL *fail_label = NEW_LABEL(location->
line);
8141 LABEL *end_label = NEW_LABEL(location->
line);
8145 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
call);
8152 PUSH_INSN1(ret, *location, getglobal, operand);
8155 PUSH_INSN(ret, *location, dup);
8156 PUSH_INSNL(ret, *location, branchunless, fail_label);
8162 if (targets_count == 1) {
8164 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8166 const pm_local_variable_target_node_t *local_target = (
const pm_local_variable_target_node_t *) target;
8167 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->
name, local_target->
depth);
8171 PUSH_INSN1(ret, *location, putobject, operand);
8174 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8175 PUSH_LABEL(ret, fail_label);
8176 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8177 if (popped) PUSH_INSN(ret, *location, pop);
8181 DECL_ANCHOR(fail_anchor);
8185 for (
size_t targets_index = 0; targets_index < targets_count; targets_index++) {
8186 const pm_node_t *target = node->
targets.
nodes[targets_index];
8187 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8189 const pm_local_variable_target_node_t *local_target = (
const pm_local_variable_target_node_t *) target;
8190 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->
name, local_target->
depth);
8192 if (((
size_t) targets_index) < (targets_count - 1)) {
8193 PUSH_INSN(ret, *location, dup);
8198 PUSH_INSN1(ret, *location, putobject, operand);
8201 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8202 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8204 PUSH_INSN(fail_anchor, *location, putnil);
8205 PUSH_SETLOCAL(fail_anchor, *location, index.index, index.level);
8209 PUSH_INSNL(ret, *location, jump, end_label);
8213 PUSH_LABEL(ret, fail_label);
8214 PUSH_INSN(ret, *location, pop);
8215 PUSH_SEQ(ret, fail_anchor);
8218 PUSH_LABEL(ret, end_label);
8219 if (popped) PUSH_INSN(ret, *location, pop);
8223pm_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)
8225 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8226 LABEL *splabel = NEW_LABEL(0);
8227 PUSH_LABEL(ret, splabel);
8230 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
8233 PUSH_INSN(ret, *location, putnil);
8235 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8237 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8238 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8240 PUSH_ADJUST_RESTORE(ret, splabel);
8241 if (!popped) PUSH_INSN(ret, *location, putnil);
8243 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8244 LABEL *splabel = NEW_LABEL(0);
8246 PUSH_LABEL(ret, splabel);
8247 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8250 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
8253 PUSH_INSN(ret, *location, putnil);
8256 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8257 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8258 PUSH_ADJUST_RESTORE(ret, splabel);
8259 splabel->unremovable = FALSE;
8261 if (!popped) PUSH_INSN(ret, *location, putnil);
8264 const rb_iseq_t *ip = iseq;
8265 unsigned long throw_flag = 0;
8268 if (!ISEQ_COMPILE_DATA(ip)) {
8273 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8274 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8278 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8281 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8282 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8286 ip = ISEQ_BODY(ip)->parent_iseq;
8291 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
8294 PUSH_INSN(ret, *location, putnil);
8297 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8298 if (popped) PUSH_INSN(ret, *location, pop);
8301 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8307pm_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)
8309 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8310 LABEL *splabel = NEW_LABEL(0);
8312 PUSH_LABEL(ret, splabel);
8313 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8314 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8316 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8317 PUSH_ADJUST_RESTORE(ret, splabel);
8318 if (!popped) PUSH_INSN(ret, *location, putnil);
8320 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8321 LABEL *splabel = NEW_LABEL(0);
8323 PUSH_LABEL(ret, splabel);
8324 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8325 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8327 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8328 PUSH_ADJUST_RESTORE(ret, splabel);
8329 if (!popped) PUSH_INSN(ret, *location, putnil);
8332 const rb_iseq_t *ip = iseq;
8335 if (!ISEQ_COMPILE_DATA(ip)) {
8340 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8343 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8346 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8347 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8351 ip = ISEQ_BODY(ip)->parent_iseq;
8355 PUSH_INSN(ret, *location, putnil);
8356 PUSH_INSN1(ret, *location,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8357 if (popped) PUSH_INSN(ret, *location, pop);
8360 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8366pm_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)
8368 iseq_set_exception_local_table(iseq);
8372 LABEL *exception_match_label = NEW_LABEL(location->
line);
8373 LABEL *rescue_end_label = NEW_LABEL(location->
line);
8381 const pm_node_list_t *exceptions = &node->
exceptions;
8383 if (exceptions->
size > 0) {
8384 for (
size_t index = 0; index < exceptions->
size; index++) {
8385 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8386 PM_COMPILE(exceptions->
nodes[index]);
8387 int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
8388 if (PM_NODE_TYPE_P(exceptions->
nodes[index], PM_SPLAT_NODE)) {
8389 checkmatch_flags |= VM_CHECKMATCH_ARRAY;
8391 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(checkmatch_flags));
8392 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8396 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8398 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8399 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8404 PUSH_INSNL(ret, *location, jump, rescue_end_label);
8409 PUSH_LABEL(ret, exception_match_label);
8416 DECL_ANCHOR(writes);
8417 DECL_ANCHOR(cleanup);
8419 pm_compile_target_node(iseq, node->
reference, ret, writes, cleanup, scope_node, NULL);
8420 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8422 PUSH_SEQ(ret, writes);
8423 PUSH_SEQ(ret, cleanup);
8433 LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
8434 ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
8436 PM_COMPILE((
const pm_node_t *) node->
statements);
8439 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
8442 PUSH_INSN(ret, *location, putnil);
8445 PUSH_INSN(ret, *location, leave);
8451 PUSH_LABEL(ret, rescue_end_label);
8453 PM_COMPILE((
const pm_node_t *) node->
subsequent);
8456 PUSH_GETLOCAL(ret, *location, 1, 0);
8461pm_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)
8463 const pm_arguments_node_t *arguments = node->
arguments;
8464 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8467 const rb_iseq_t *parent_iseq = iseq;
8468 enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
8469 while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
8470 if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq))
break;
8471 parent_type = ISEQ_BODY(parent_iseq)->type;
8474 switch (parent_type) {
8476 case ISEQ_TYPE_MAIN:
8478 rb_warn(
"argument of top-level return is ignored");
8480 if (parent_iseq == iseq) {
8481 type = ISEQ_TYPE_METHOD;
8488 if (
type == ISEQ_TYPE_METHOD) {
8489 splabel = NEW_LABEL(0);
8490 PUSH_LABEL(ret, splabel);
8491 PUSH_ADJUST(ret, *location, 0);
8494 if (arguments != NULL) {
8495 PM_COMPILE_NOT_POPPED((
const pm_node_t *) arguments);
8498 PUSH_INSN(ret, *location, putnil);
8501 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8502 pm_add_ensure_iseq(ret, iseq, 1, scope_node);
8504 PUSH_INSN(ret, *location, leave);
8505 PUSH_ADJUST_RESTORE(ret, splabel);
8506 if (!popped) PUSH_INSN(ret, *location, putnil);
8509 PUSH_INSN1(ret, *location,
throw,
INT2FIX(TAG_RETURN));
8510 if (popped) PUSH_INSN(ret, *location, pop);
8515pm_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)
8519 LABEL *retry_label = NEW_LABEL(location->
line);
8520 LABEL *retry_end_l = NEW_LABEL(location->
line);
8522 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8523 const rb_iseq_t *current_block;
8524 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
8526 PUSH_LABEL(ret, retry_label);
8527 PUSH_INSN(ret, *location, putself);
8531 int argc = pm_setup_args(node->
arguments, node->
block, &flags, &keywords, iseq, ret, scope_node, location);
8532 bool is_forwardable = (node->
arguments != NULL) && PM_NODE_FLAG_P(node->
arguments, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING);
8533 flags |= VM_CALL_SUPER | VM_CALL_FCALL;
8535 if (node->
block && PM_NODE_TYPE_P(node->
block, PM_BLOCK_NODE)) {
8536 pm_scope_node_t next_scope_node;
8537 pm_scope_node_init(node->
block, &next_scope_node, scope_node);
8539 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
8540 pm_scope_node_destroy(&next_scope_node);
8544 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8547 if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
8548 PUSH_INSN(args, *location, splatkw);
8551 PUSH_SEQ(ret, args);
8552 if (is_forwardable && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.
flags.forwardable) {
8553 flags |= VM_CALL_FORWARDING;
8556 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8557 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, current_block);
8562 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8563 PUSH_INSN2(ret, *location, invokesuper, callinfo, current_block);
8568 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8570 if (popped) PUSH_INSN(ret, *location, pop);
8571 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8572 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
8576pm_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)
8578 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
8580 case ISEQ_TYPE_MAIN:
8581 case ISEQ_TYPE_CLASS:
8582 COMPILE_ERROR(iseq, location->
line,
"Invalid yield");
8592 argc = pm_setup_args(node->
arguments, NULL, &flags, &keywords, iseq, ret, scope_node, location);
8595 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, FALSE);
8596 PUSH_INSN1(ret, *location, invokeblock, callinfo);
8598 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8599 if (popped) PUSH_INSN(ret, *location, pop);
8602 for (
const rb_iseq_t *tmp_iseq = iseq; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++) {
8603 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
8606 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
8620pm_compile_node(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8624 int lineno = (int) location.
line;
8626 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)) {
8631 lineno = (int) PM_NODE_START_LINE_COLUMN(parser, ((
const pm_begin_node_t *) node)->rescue_clause).line;
8634 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
8640 ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
8641 if (lineno > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
8642 event |= RUBY_EVENT_COVERAGE_LINE;
8644 PUSH_TRACE(ret, event);
8647 switch (PM_NODE_TYPE(node)) {
8648 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
8651 pm_compile_alias_global_variable_node(iseq, (
const pm_alias_global_variable_node_t *) node, &location, ret, popped, scope_node);
8653 case PM_ALIAS_METHOD_NODE:
8656 pm_compile_alias_method_node(iseq, (
const pm_alias_method_node_t *) node, &location, ret, popped, scope_node);
8661 pm_compile_and_node(iseq, (
const pm_and_node_t *) node, &location, ret, popped, scope_node);
8663 case PM_ARGUMENTS_NODE: {
8670 const pm_arguments_node_t *cast = (
const pm_arguments_node_t *) node;
8671 const pm_node_list_t *elements = &cast->
arguments;
8673 if (elements->
size == 1) {
8676 PM_COMPILE(elements->
nodes[0]);
8679 pm_compile_array_node(iseq, (
const pm_node_t *) cast, elements, &location, ret, popped, scope_node);
8683 case PM_ARRAY_NODE: {
8686 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
8687 pm_compile_array_node(iseq, (
const pm_node_t *) cast, &cast->elements, &location, ret, popped, scope_node);
8690 case PM_ASSOC_NODE: {
8696 const pm_assoc_node_t *cast = (
const pm_assoc_node_t *) node;
8698 PM_COMPILE(cast->
key);
8699 PM_COMPILE(cast->
value);
8703 case PM_ASSOC_SPLAT_NODE: {
8709 const pm_assoc_splat_node_t *cast = (
const pm_assoc_splat_node_t *) node;
8711 if (cast->
value != NULL) {
8712 PM_COMPILE(cast->
value);
8715 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
8716 PUSH_GETLOCAL(ret, location, index.index, index.level);
8721 case PM_BACK_REFERENCE_READ_NODE: {
8725 const pm_back_reference_read_node_t *cast = (
const pm_back_reference_read_node_t *) node;
8726 VALUE backref = pm_compile_back_reference_ref(cast);
8728 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), backref);
8732 case PM_BEGIN_NODE: {
8735 const pm_begin_node_t *cast = (
const pm_begin_node_t *) node;
8740 pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
8744 pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
8750 PM_COMPILE((
const pm_node_t *) cast->
statements);
8753 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
8758 case PM_BLOCK_ARGUMENT_NODE: {
8761 const pm_block_argument_node_t *cast = (
const pm_block_argument_node_t *) node;
8768 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
8769 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(local_index.level));
8779 pm_compile_break_node(iseq, (
const pm_break_node_t *) node, &location, ret, popped, scope_node);
8790 pm_compile_call_node(iseq, (
const pm_call_node_t *) node, ret, popped, scope_node);
8792 case PM_CALL_AND_WRITE_NODE: {
8795 const pm_call_and_write_node_t *cast = (
const pm_call_and_write_node_t *) node;
8796 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);
8799 case PM_CALL_OR_WRITE_NODE: {
8802 const pm_call_or_write_node_t *cast = (
const pm_call_or_write_node_t *) node;
8803 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);
8806 case PM_CALL_OPERATOR_WRITE_NODE:
8816 pm_compile_call_operator_write_node(iseq, (
const pm_call_operator_write_node_t *) node, &location, ret, popped, scope_node);
8821 pm_compile_case_node(iseq, (
const pm_case_node_t *) node, &location, ret, popped, scope_node);
8823 case PM_CASE_MATCH_NODE:
8832 pm_compile_case_match_node(iseq, (
const pm_case_match_node_t *) node, &location, ret, popped, scope_node);
8834 case PM_CLASS_NODE: {
8837 const pm_class_node_t *cast = (
const pm_class_node_t *) node;
8839 ID class_id = pm_constant_id_lookup(scope_node, cast->
name);
8840 VALUE class_name =
rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(class_id)));
8842 pm_scope_node_t next_scope_node;
8843 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
8845 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, location.
line);
8846 pm_scope_node_destroy(&next_scope_node);
8849 const int flags = VM_DEFINECLASS_TYPE_CLASS |
8850 (cast->
superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
8851 pm_compile_class_path(iseq, cast->
constant_path, &location, ret,
false, scope_node);
8857 PUSH_INSN(ret, location, putnil);
8862 PUSH_INSN3(ret, location, defineclass, operand, class_iseq,
INT2FIX(flags));
8866 if (popped) PUSH_INSN(ret, location, pop);
8869 case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
8872 const pm_class_variable_and_write_node_t *cast = (
const pm_class_variable_and_write_node_t *) node;
8873 LABEL *end_label = NEW_LABEL(location.
line);
8875 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8878 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8879 if (!popped) PUSH_INSN(ret, location, dup);
8881 PUSH_INSNL(ret, location, branchunless, end_label);
8882 if (!popped) PUSH_INSN(ret, location, pop);
8884 PM_COMPILE_NOT_POPPED(cast->
value);
8885 if (!popped) PUSH_INSN(ret, location, dup);
8887 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8888 PUSH_LABEL(ret, end_label);
8892 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
8895 const pm_class_variable_operator_write_node_t *cast = (
const pm_class_variable_operator_write_node_t *) node;
8897 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8900 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8901 PM_COMPILE_NOT_POPPED(cast->
value);
8904 int flags = VM_CALL_ARGS_SIMPLE;
8905 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
8907 if (!popped) PUSH_INSN(ret, location, dup);
8908 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8912 case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
8915 const pm_class_variable_or_write_node_t *cast = (
const pm_class_variable_or_write_node_t *) node;
8916 LABEL *end_label = NEW_LABEL(location.
line);
8917 LABEL *start_label = NEW_LABEL(location.
line);
8919 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8922 PUSH_INSN(ret, location, putnil);
8923 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR), name,
Qtrue);
8924 PUSH_INSNL(ret, location, branchunless, start_label);
8926 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8927 if (!popped) PUSH_INSN(ret, location, dup);
8929 PUSH_INSNL(ret, location, branchif, end_label);
8930 if (!popped) PUSH_INSN(ret, location, pop);
8932 PUSH_LABEL(ret, start_label);
8933 PM_COMPILE_NOT_POPPED(cast->
value);
8934 if (!popped) PUSH_INSN(ret, location, dup);
8936 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8937 PUSH_LABEL(ret, end_label);
8941 case PM_CLASS_VARIABLE_READ_NODE: {
8945 const pm_class_variable_read_node_t *cast = (
const pm_class_variable_read_node_t *) node;
8946 ID name = pm_constant_id_lookup(scope_node, cast->
name);
8947 PUSH_INSN2(ret, location, getclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
8951 case PM_CLASS_VARIABLE_WRITE_NODE: {
8954 const pm_class_variable_write_node_t *cast = (
const pm_class_variable_write_node_t *) node;
8955 PM_COMPILE_NOT_POPPED(cast->
value);
8956 if (!popped) PUSH_INSN(ret, location, dup);
8958 ID name = pm_constant_id_lookup(scope_node, cast->
name);
8959 PUSH_INSN2(ret, location, setclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
8963 case PM_CONSTANT_PATH_NODE: {
8968 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) !=
Qnil)) {
8969 ISEQ_BODY(iseq)->ic_size++;
8970 RB_OBJ_SET_SHAREABLE(parts);
8971 PUSH_INSN1(ret, location, opt_getconstant_path, parts);
8974 DECL_ANCHOR(prefix);
8977 pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
8978 if (LIST_INSN_SIZE_ZERO(prefix)) {
8979 PUSH_INSN(ret, location, putnil);
8982 PUSH_SEQ(ret, prefix);
8985 PUSH_SEQ(ret, body);
8988 if (popped) PUSH_INSN(ret, location, pop);
8991 case PM_CONSTANT_PATH_AND_WRITE_NODE: {
8994 const pm_constant_path_and_write_node_t *cast = (
const pm_constant_path_and_write_node_t *) node;
8995 pm_compile_constant_path_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8998 case PM_CONSTANT_PATH_OR_WRITE_NODE: {
9001 const pm_constant_path_or_write_node_t *cast = (
const pm_constant_path_or_write_node_t *) node;
9002 pm_compile_constant_path_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9005 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
9008 const pm_constant_path_operator_write_node_t *cast = (
const pm_constant_path_operator_write_node_t *) node;
9009 pm_compile_constant_path_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9012 case PM_CONSTANT_PATH_WRITE_NODE: {
9015 const pm_constant_path_write_node_t *cast = (
const pm_constant_path_write_node_t *) node;
9016 pm_compile_constant_path_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9019 case PM_CONSTANT_READ_NODE: {
9022 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) node;
9025 pm_compile_constant_read(iseq, name, &cast->
base.
location, location.
node_id, ret, scope_node);
9026 if (popped) PUSH_INSN(ret, location, pop);
9030 case PM_CONSTANT_AND_WRITE_NODE: {
9033 const pm_constant_and_write_node_t *cast = (
const pm_constant_and_write_node_t *) node;
9034 pm_compile_constant_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9037 case PM_CONSTANT_OR_WRITE_NODE: {
9040 const pm_constant_or_write_node_t *cast = (
const pm_constant_or_write_node_t *) node;
9041 pm_compile_constant_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9044 case PM_CONSTANT_OPERATOR_WRITE_NODE: {
9047 const pm_constant_operator_write_node_t *cast = (
const pm_constant_operator_write_node_t *) node;
9048 pm_compile_constant_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9051 case PM_CONSTANT_WRITE_NODE: {
9054 const pm_constant_write_node_t *cast = (
const pm_constant_write_node_t *) node;
9055 pm_compile_constant_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9064 const pm_def_node_t *cast = (
const pm_def_node_t *) node;
9065 ID method_name = pm_constant_id_lookup(scope_node, cast->
name);
9067 pm_scope_node_t next_scope_node;
9068 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9070 rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, location.
line);
9071 pm_scope_node_destroy(&next_scope_node);
9074 PM_COMPILE_NOT_POPPED(cast->
receiver);
9075 PUSH_INSN2(ret, location, definesmethod,
ID2SYM(method_name), method_iseq);
9078 PUSH_INSN2(ret, location, definemethod,
ID2SYM(method_name), method_iseq);
9083 PUSH_INSN1(ret, location, putobject,
ID2SYM(method_name));
9088 case PM_DEFINED_NODE: {
9091 const pm_defined_node_t *cast = (
const pm_defined_node_t *) node;
9092 pm_compile_defined_expr(iseq, cast->
value, &location, ret, popped, scope_node,
false);
9095 case PM_EMBEDDED_STATEMENTS_NODE: {
9098 const pm_embedded_statements_node_t *cast = (
const pm_embedded_statements_node_t *) node;
9101 PM_COMPILE((
const pm_node_t *) (cast->
statements));
9104 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9107 if (popped) PUSH_INSN(ret, location, pop);
9110 case PM_EMBEDDED_VARIABLE_NODE: {
9113 const pm_embedded_variable_node_t *cast = (
const pm_embedded_variable_node_t *) node;
9117 case PM_FALSE_NODE: {
9121 PUSH_INSN1(ret, location, putobject,
Qfalse);
9125 case PM_ENSURE_NODE: {
9126 const pm_ensure_node_t *cast = (
const pm_ensure_node_t *) node;
9129 PM_COMPILE((
const pm_node_t *) cast->
statements);
9134 case PM_ELSE_NODE: {
9137 const pm_else_node_t *cast = (
const pm_else_node_t *) node;
9140 PM_COMPILE((
const pm_node_t *) cast->
statements);
9143 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9148 case PM_FLIP_FLOP_NODE: {
9151 const pm_flip_flop_node_t *cast = (
const pm_flip_flop_node_t *) node;
9153 LABEL *final_label = NEW_LABEL(location.
line);
9154 LABEL *then_label = NEW_LABEL(location.
line);
9155 LABEL *else_label = NEW_LABEL(location.
line);
9157 pm_compile_flip_flop(cast, else_label, then_label, iseq, location.
line, ret, popped, scope_node);
9159 PUSH_LABEL(ret, then_label);
9160 PUSH_INSN1(ret, location, putobject,
Qtrue);
9161 PUSH_INSNL(ret, location, jump, final_label);
9162 PUSH_LABEL(ret, else_label);
9163 PUSH_INSN1(ret, location, putobject,
Qfalse);
9164 PUSH_LABEL(ret, final_label);
9168 case PM_FLOAT_NODE: {
9172 VALUE operand = parse_float((
const pm_float_node_t *) node);
9173 PUSH_INSN1(ret, location, putobject, operand);
9180 const pm_for_node_t *cast = (
const pm_for_node_t *) node;
9182 LABEL *retry_label = NEW_LABEL(location.
line);
9183 LABEL *retry_end_l = NEW_LABEL(location.
line);
9186 PUSH_LABEL(ret, retry_label);
9191 pm_scope_node_t next_scope_node;
9192 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9194 const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.
line);
9195 pm_scope_node_destroy(&next_scope_node);
9197 const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9198 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
9202 PUSH_SEND_WITH_BLOCK(ret, location, idEach,
INT2FIX(0), child_iseq);
9203 pm_compile_retry_end_label(iseq, ret, retry_end_l);
9205 if (popped) PUSH_INSN(ret, location, pop);
9206 ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
9207 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
9210 case PM_FORWARDING_ARGUMENTS_NODE:
9211 rb_bug(
"Cannot compile a ForwardingArgumentsNode directly\n");
9213 case PM_FORWARDING_SUPER_NODE:
9219 pm_compile_forwarding_super_node(iseq, (
const pm_forwarding_super_node_t *) node, &location, ret, popped, scope_node);
9221 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
9224 const pm_global_variable_and_write_node_t *cast = (
const pm_global_variable_and_write_node_t *) node;
9225 LABEL *end_label = NEW_LABEL(location.
line);
9228 PUSH_INSN1(ret, location, getglobal, name);
9229 if (!popped) PUSH_INSN(ret, location, dup);
9231 PUSH_INSNL(ret, location, branchunless, end_label);
9232 if (!popped) PUSH_INSN(ret, location, pop);
9234 PM_COMPILE_NOT_POPPED(cast->
value);
9235 if (!popped) PUSH_INSN(ret, location, dup);
9237 PUSH_INSN1(ret, location, setglobal, name);
9238 PUSH_LABEL(ret, end_label);
9242 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
9245 const pm_global_variable_operator_write_node_t *cast = (
const pm_global_variable_operator_write_node_t *) node;
9248 PUSH_INSN1(ret, location, getglobal, name);
9249 PM_COMPILE_NOT_POPPED(cast->
value);
9252 int flags = VM_CALL_ARGS_SIMPLE;
9253 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9255 if (!popped) PUSH_INSN(ret, location, dup);
9256 PUSH_INSN1(ret, location, setglobal, name);
9260 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
9263 const pm_global_variable_or_write_node_t *cast = (
const pm_global_variable_or_write_node_t *) node;
9264 LABEL *set_label = NEW_LABEL(location.
line);
9265 LABEL *end_label = NEW_LABEL(location.
line);
9267 PUSH_INSN(ret, location, putnil);
9270 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR), name,
Qtrue);
9271 PUSH_INSNL(ret, location, branchunless, set_label);
9273 PUSH_INSN1(ret, location, getglobal, name);
9274 if (!popped) PUSH_INSN(ret, location, dup);
9276 PUSH_INSNL(ret, location, branchif, end_label);
9277 if (!popped) PUSH_INSN(ret, location, pop);
9279 PUSH_LABEL(ret, set_label);
9280 PM_COMPILE_NOT_POPPED(cast->
value);
9281 if (!popped) PUSH_INSN(ret, location, dup);
9283 PUSH_INSN1(ret, location, setglobal, name);
9284 PUSH_LABEL(ret, end_label);
9288 case PM_GLOBAL_VARIABLE_READ_NODE: {
9291 const pm_global_variable_read_node_t *cast = (
const pm_global_variable_read_node_t *) node;
9294 PUSH_INSN1(ret, location, getglobal, name);
9295 if (popped) PUSH_INSN(ret, location, pop);
9299 case PM_GLOBAL_VARIABLE_WRITE_NODE: {
9302 const pm_global_variable_write_node_t *cast = (
const pm_global_variable_write_node_t *) node;
9303 PM_COMPILE_NOT_POPPED(cast->
value);
9304 if (!popped) PUSH_INSN(ret, location, dup);
9306 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9307 PUSH_INSN1(ret, location, setglobal,
ID2SYM(name));
9311 case PM_HASH_NODE: {
9317 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9322 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
9325 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
9328 VALUE value = pm_static_literal_value(iseq, node, scope_node);
9329 PUSH_INSN1(ret, location, duphash, value);
9343 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
9344 const pm_node_list_t *elements = &cast->
elements;
9350 for (
size_t index = 0; index < elements->
size; index++) {
9351 PM_COMPILE_POPPED(elements->
nodes[index]);
9355 pm_compile_hash_elements(iseq, node, elements, 0,
Qundef,
false, ret, scope_node);
9370 const pm_if_node_t *cast = (
const pm_if_node_t *) node;
9371 pm_compile_conditional(iseq, &location, PM_IF_NODE, (
const pm_node_t *) cast, cast->statements, cast->subsequent, cast->predicate, ret, popped, scope_node);
9374 case PM_IMAGINARY_NODE: {
9378 VALUE operand = parse_imaginary((
const pm_imaginary_node_t *) node);
9379 PUSH_INSN1(ret, location, putobject, operand);
9383 case PM_IMPLICIT_NODE: {
9392 const pm_implicit_node_t *cast = (
const pm_implicit_node_t *) node;
9393 PM_COMPILE(cast->
value);
9399 rb_bug(
"Should not ever enter an in node directly");
9402 case PM_INDEX_OPERATOR_WRITE_NODE: {
9405 const pm_index_operator_write_node_t *cast = (
const pm_index_operator_write_node_t *) node;
9406 pm_compile_index_operator_write_node(iseq, cast, &location, ret, popped, scope_node);
9409 case PM_INDEX_AND_WRITE_NODE: {
9412 const pm_index_and_write_node_t *cast = (
const pm_index_and_write_node_t *) node;
9413 pm_compile_index_control_flow_write_node(iseq, node, cast->
receiver, cast->
arguments, cast->
block, cast->
value, &location, ret, popped, scope_node);
9416 case PM_INDEX_OR_WRITE_NODE: {
9419 const pm_index_or_write_node_t *cast = (
const pm_index_or_write_node_t *) node;
9420 pm_compile_index_control_flow_write_node(iseq, node, cast->
receiver, cast->
arguments, cast->
block, cast->
value, &location, ret, popped, scope_node);
9423 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
9426 const pm_instance_variable_and_write_node_t *cast = (
const pm_instance_variable_and_write_node_t *) node;
9427 LABEL *end_label = NEW_LABEL(location.
line);
9429 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9432 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9433 if (!popped) PUSH_INSN(ret, location, dup);
9435 PUSH_INSNL(ret, location, branchunless, end_label);
9436 if (!popped) PUSH_INSN(ret, location, pop);
9438 PM_COMPILE_NOT_POPPED(cast->
value);
9439 if (!popped) PUSH_INSN(ret, location, dup);
9441 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9442 PUSH_LABEL(ret, end_label);
9446 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
9449 const pm_instance_variable_operator_write_node_t *cast = (
const pm_instance_variable_operator_write_node_t *) node;
9451 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9454 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9455 PM_COMPILE_NOT_POPPED(cast->
value);
9458 int flags = VM_CALL_ARGS_SIMPLE;
9459 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9461 if (!popped) PUSH_INSN(ret, location, dup);
9462 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9466 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
9469 const pm_instance_variable_or_write_node_t *cast = (
const pm_instance_variable_or_write_node_t *) node;
9470 LABEL *end_label = NEW_LABEL(location.
line);
9472 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9475 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9476 if (!popped) PUSH_INSN(ret, location, dup);
9478 PUSH_INSNL(ret, location, branchif, end_label);
9479 if (!popped) PUSH_INSN(ret, location, pop);
9481 PM_COMPILE_NOT_POPPED(cast->
value);
9482 if (!popped) PUSH_INSN(ret, location, dup);
9484 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9485 PUSH_LABEL(ret, end_label);
9489 case PM_INSTANCE_VARIABLE_READ_NODE: {
9493 const pm_instance_variable_read_node_t *cast = (
const pm_instance_variable_read_node_t *) node;
9494 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9495 PUSH_INSN2(ret, location, getinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9499 case PM_INSTANCE_VARIABLE_WRITE_NODE: {
9502 const pm_instance_variable_write_node_t *cast = (
const pm_instance_variable_write_node_t *) node;
9503 PM_COMPILE_NOT_POPPED(cast->
value);
9504 if (!popped) PUSH_INSN(ret, location, dup);
9506 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9507 PUSH_INSN2(ret, location, setinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9511 case PM_INTEGER_NODE: {
9515 VALUE operand = parse_integer((
const pm_integer_node_t *) node);
9516 PUSH_INSN1(ret, location, putobject, operand);
9520 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
9523 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9525 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9526 PUSH_INSN1(ret, location, putobject, regexp);
9530 pm_compile_regexp_dynamic(iseq, node, &((
const pm_interpolated_match_last_line_node_t *) node)->parts, &location, ret, popped, scope_node);
9533 PUSH_INSN1(ret, location, getglobal,
rb_id2sym(idLASTLINE));
9534 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
9535 if (popped) PUSH_INSN(ret, location, pop);
9539 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
9542 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ONCE)) {
9543 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
9544 const rb_iseq_t *block_iseq = NULL;
9545 int ise_index = ISEQ_BODY(iseq)->ise_size++;
9547 pm_scope_node_t next_scope_node;
9548 pm_scope_node_init(node, &next_scope_node, scope_node);
9550 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, location.
line);
9551 pm_scope_node_destroy(&next_scope_node);
9553 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
9554 PUSH_INSN2(ret, location, once, block_iseq,
INT2FIX(ise_index));
9555 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
9557 if (popped) PUSH_INSN(ret, location, pop);
9561 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9563 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9564 PUSH_INSN1(ret, location, putobject, regexp);
9568 pm_compile_regexp_dynamic(iseq, node, &((
const pm_interpolated_regular_expression_node_t *) node)->parts, &location, ret, popped, scope_node);
9569 if (popped) PUSH_INSN(ret, location, pop);
9574 case PM_INTERPOLATED_STRING_NODE: {
9577 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9579 VALUE string = pm_static_literal_value(iseq, node, scope_node);
9581 if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN)) {
9582 PUSH_INSN1(ret, location, putobject,
string);
9584 else if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) {
9585 PUSH_INSN1(ret, location, putstring,
string);
9588 PUSH_INSN1(ret, location, putchilledstring,
string);
9593 const pm_interpolated_string_node_t *cast = (
const pm_interpolated_string_node_t *) node;
9594 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));
9595 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9596 if (popped) PUSH_INSN(ret, location, pop);
9601 case PM_INTERPOLATED_SYMBOL_NODE: {
9604 const pm_interpolated_symbol_node_t *cast = (
const pm_interpolated_symbol_node_t *) node;
9605 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret, popped, scope_node, NULL, NULL,
false,
false);
9608 PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9612 PUSH_INSN(ret, location, intern);
9615 PUSH_INSN(ret, location, pop);
9620 case PM_INTERPOLATED_X_STRING_NODE: {
9623 const pm_interpolated_x_string_node_t *cast = (
const pm_interpolated_x_string_node_t *) node;
9625 PUSH_INSN(ret, location, putself);
9627 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret,
false, scope_node, NULL, NULL,
false,
false);
9628 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9630 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
9631 if (popped) PUSH_INSN(ret, location, pop);
9635 case PM_IT_LOCAL_VARIABLE_READ_NODE: {
9639 pm_scope_node_t *current_scope_node = scope_node;
9642 while (current_scope_node) {
9643 if (current_scope_node->parameters && PM_NODE_TYPE_P(current_scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
9644 PUSH_GETLOCAL(ret, location, current_scope_node->local_table_for_iseq_size, level);
9648 current_scope_node = current_scope_node->previous;
9651 rb_bug(
"Local `it` does not exist");
9656 case PM_KEYWORD_HASH_NODE: {
9659 const pm_keyword_hash_node_t *cast = (
const pm_keyword_hash_node_t *) node;
9660 const pm_node_list_t *elements = &cast->
elements;
9662 const pm_node_t *element;
9664 PM_COMPILE(element);
9667 if (!popped) PUSH_INSN1(ret, location, newhash,
INT2FIX(elements->
size * 2));
9670 case PM_LAMBDA_NODE: {
9673 const pm_lambda_node_t *cast = (
const pm_lambda_node_t *) node;
9675 pm_scope_node_t next_scope_node;
9676 pm_scope_node_init(node, &next_scope_node, scope_node);
9678 int opening_lineno = pm_location_line_number(parser, &cast->
opening_loc);
9679 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
9680 pm_scope_node_destroy(&next_scope_node);
9683 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9684 PUSH_CALL_WITH_BLOCK(ret, location, idLambda, argc, block);
9687 if (popped) PUSH_INSN(ret, location, pop);
9690 case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
9693 const pm_local_variable_and_write_node_t *cast = (
const pm_local_variable_and_write_node_t *) node;
9694 LABEL *end_label = NEW_LABEL(location.
line);
9696 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9697 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9698 if (!popped) PUSH_INSN(ret, location, dup);
9700 PUSH_INSNL(ret, location, branchunless, end_label);
9701 if (!popped) PUSH_INSN(ret, location, pop);
9703 PM_COMPILE_NOT_POPPED(cast->
value);
9704 if (!popped) PUSH_INSN(ret, location, dup);
9706 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9707 PUSH_LABEL(ret, end_label);
9711 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
9714 const pm_local_variable_operator_write_node_t *cast = (
const pm_local_variable_operator_write_node_t *) node;
9716 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9717 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9719 PM_COMPILE_NOT_POPPED(cast->
value);
9722 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
9724 if (!popped) PUSH_INSN(ret, location, dup);
9725 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9729 case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
9732 const pm_local_variable_or_write_node_t *cast = (
const pm_local_variable_or_write_node_t *) node;
9734 LABEL *set_label = NEW_LABEL(location.
line);
9735 LABEL *end_label = NEW_LABEL(location.
line);
9737 PUSH_INSN1(ret, location, putobject,
Qtrue);
9738 PUSH_INSNL(ret, location, branchunless, set_label);
9740 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9741 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9742 if (!popped) PUSH_INSN(ret, location, dup);
9744 PUSH_INSNL(ret, location, branchif, end_label);
9745 if (!popped) PUSH_INSN(ret, location, pop);
9747 PUSH_LABEL(ret, set_label);
9748 PM_COMPILE_NOT_POPPED(cast->
value);
9749 if (!popped) PUSH_INSN(ret, location, dup);
9751 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9752 PUSH_LABEL(ret, end_label);
9756 case PM_LOCAL_VARIABLE_READ_NODE: {
9760 const pm_local_variable_read_node_t *cast = (
const pm_local_variable_read_node_t *) node;
9761 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9762 PUSH_GETLOCAL(ret, location, index.index, index.level);
9767 case PM_LOCAL_VARIABLE_WRITE_NODE: {
9770 const pm_local_variable_write_node_t *cast = (
const pm_local_variable_write_node_t *) node;
9771 PM_COMPILE_NOT_POPPED(cast->
value);
9772 if (!popped) PUSH_INSN(ret, location, dup);
9774 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9775 PUSH_SETLOCAL(ret, location, index.index, index.level);
9778 case PM_MATCH_LAST_LINE_NODE: {
9781 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9783 PUSH_INSN1(ret, location, putobject, regexp);
9785 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
9786 if (popped) PUSH_INSN(ret, location, pop);
9790 case PM_MATCH_PREDICATE_NODE: {
9793 const pm_match_predicate_node_t *cast = (
const pm_match_predicate_node_t *) node;
9797 PUSH_INSN(ret, location, putnil);
9800 PM_COMPILE_NOT_POPPED(cast->
value);
9801 PUSH_INSN(ret, location, dup);
9805 LABEL *matched_label = NEW_LABEL(location.
line);
9806 LABEL *unmatched_label = NEW_LABEL(location.
line);
9807 LABEL *done_label = NEW_LABEL(location.
line);
9808 pm_compile_pattern(iseq, scope_node, cast->
pattern, ret, matched_label, unmatched_label,
false,
true, 2);
9812 PUSH_LABEL(ret, unmatched_label);
9813 PUSH_INSN(ret, location, pop);
9814 PUSH_INSN(ret, location, pop);
9816 if (!popped) PUSH_INSN1(ret, location, putobject,
Qfalse);
9817 PUSH_INSNL(ret, location, jump, done_label);
9818 PUSH_INSN(ret, location, putnil);
9822 PUSH_LABEL(ret, matched_label);
9823 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(2));
9824 if (!popped) PUSH_INSN1(ret, location, putobject,
Qtrue);
9825 PUSH_INSNL(ret, location, jump, done_label);
9827 PUSH_LABEL(ret, done_label);
9830 case PM_MATCH_REQUIRED_NODE:
9842 pm_compile_match_required_node(iseq, (
const pm_match_required_node_t *) node, &location, ret, popped, scope_node);
9844 case PM_MATCH_WRITE_NODE:
9853 pm_compile_match_write_node(iseq, (
const pm_match_write_node_t *) node, &location, ret, popped, scope_node);
9855 case PM_MISSING_NODE:
9856 rb_bug(
"A pm_missing_node_t should not exist in prism's AST.");
9858 case PM_MODULE_NODE: {
9861 const pm_module_node_t *cast = (
const pm_module_node_t *) node;
9863 ID module_id = pm_constant_id_lookup(scope_node, cast->
name);
9864 VALUE module_name =
rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(module_id)));
9866 pm_scope_node_t next_scope_node;
9867 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9869 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, location.
line);
9870 pm_scope_node_destroy(&next_scope_node);
9872 const int flags = VM_DEFINECLASS_TYPE_MODULE | pm_compile_class_path(iseq, cast->
constant_path, &location, ret,
false, scope_node);
9873 PUSH_INSN(ret, location, putnil);
9874 PUSH_INSN3(ret, location, defineclass,
ID2SYM(module_id), module_iseq,
INT2FIX(flags));
9877 if (popped) PUSH_INSN(ret, location, pop);
9880 case PM_REQUIRED_PARAMETER_NODE: {
9883 const pm_required_parameter_node_t *cast = (
const pm_required_parameter_node_t *) node;
9884 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, 0);
9886 PUSH_SETLOCAL(ret, location, index.index, index.level);
9889 case PM_MULTI_WRITE_NODE: {
9898 const pm_multi_write_node_t *cast = (
const pm_multi_write_node_t *) node;
9900 DECL_ANCHOR(writes);
9901 DECL_ANCHOR(cleanup);
9904 state.position = popped ? 0 : 1;
9905 pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
9907 PM_COMPILE_NOT_POPPED(cast->
value);
9908 if (!popped) PUSH_INSN(ret, location, dup);
9910 PUSH_SEQ(ret, writes);
9911 if (!popped && state.stack_size >= 1) {
9914 PUSH_INSN1(ret, location, setn,
INT2FIX(state.stack_size));
9919 pm_multi_target_state_update(&state);
9921 PUSH_SEQ(ret, cleanup);
9930 pm_compile_next_node(iseq, (
const pm_next_node_t *) node, &location, ret, popped, scope_node);
9936 PUSH_INSN(ret, location, putnil);
9941 case PM_NO_KEYWORDS_PARAMETER_NODE: {
9944 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
9947 case PM_NUMBERED_REFERENCE_READ_NODE: {
9951 const pm_numbered_reference_read_node_t *cast = (
const pm_numbered_reference_read_node_t *) node;
9954 VALUE ref = pm_compile_numbered_reference_ref(cast);
9955 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), ref);
9958 PUSH_INSN(ret, location, putnil);
9967 const pm_or_node_t *cast = (
const pm_or_node_t *) node;
9969 LABEL *end_label = NEW_LABEL(location.
line);
9970 PM_COMPILE_NOT_POPPED(cast->
left);
9972 if (!popped) PUSH_INSN(ret, location, dup);
9973 PUSH_INSNL(ret, location, branchif, end_label);
9975 if (!popped) PUSH_INSN(ret, location, pop);
9976 PM_COMPILE(cast->
right);
9977 PUSH_LABEL(ret, end_label);
9981 case PM_OPTIONAL_PARAMETER_NODE: {
9984 const pm_optional_parameter_node_t *cast = (
const pm_optional_parameter_node_t *) node;
9985 PM_COMPILE_NOT_POPPED(cast->
value);
9987 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, 0);
9988 PUSH_SETLOCAL(ret, location, index.index, index.level);
9992 case PM_PARENTHESES_NODE: {
9998 const pm_parentheses_node_t *cast = (
const pm_parentheses_node_t *) node;
10000 if (cast->
body != NULL) {
10001 PM_COMPILE(cast->
body);
10003 else if (!popped) {
10004 PUSH_INSN(ret, location, putnil);
10009 case PM_PRE_EXECUTION_NODE: {
10012 const pm_pre_execution_node_t *cast = (
const pm_pre_execution_node_t *) node;
10021 DECL_ANCHOR(inner_pre);
10024 DECL_ANCHOR(inner_body);
10029 for (
size_t index = 0; index < body->
size; index++) {
10030 pm_compile_node(iseq, body->
nodes[index], inner_body,
true, scope_node);
10035 PUSH_INSN(inner_body, location, putnil);
10041 PUSH_SEQ(outer_pre, inner_pre);
10042 PUSH_SEQ(outer_pre, inner_body);
10047 case PM_POST_EXECUTION_NODE: {
10050 const rb_iseq_t *child_iseq;
10051 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
10053 pm_scope_node_t next_scope_node;
10054 pm_scope_node_init(node, &next_scope_node, scope_node);
10055 child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
10056 pm_scope_node_destroy(&next_scope_node);
10058 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
10060 int is_index = ISEQ_BODY(iseq)->ise_size++;
10061 PUSH_INSN2(ret, location, once, child_iseq,
INT2FIX(is_index));
10063 if (popped) PUSH_INSN(ret, location, pop);
10065 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
10069 case PM_RANGE_NODE: {
10072 const pm_range_node_t *cast = (
const pm_range_node_t *) node;
10073 bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
10075 if (pm_optimizable_range_item_p(cast->
left) && pm_optimizable_range_item_p(cast->
right)) {
10077 const pm_node_t *left = cast->
left;
10078 const pm_node_t *right = cast->
right;
10081 (left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((
const pm_integer_node_t *) left) :
Qnil,
10082 (right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((
const pm_integer_node_t *) right) :
Qnil,
10086 RB_OBJ_SET_SHAREABLE(val);
10087 PUSH_INSN1(ret, location, putobject, val);
10091 if (cast->
left != NULL) {
10092 PM_COMPILE(cast->
left);
10094 else if (!popped) {
10095 PUSH_INSN(ret, location, putnil);
10098 if (cast->
right != NULL) {
10099 PM_COMPILE(cast->
right);
10101 else if (!popped) {
10102 PUSH_INSN(ret, location, putnil);
10106 PUSH_INSN1(ret, location, newrange,
INT2FIX(exclude_end ? 1 : 0));
10111 case PM_RATIONAL_NODE: {
10115 PUSH_INSN1(ret, location, putobject, parse_rational((
const pm_rational_node_t *) node));
10122 pm_compile_redo_node(iseq, &location, ret, popped, scope_node);
10124 case PM_REGULAR_EXPRESSION_NODE: {
10128 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
10129 PUSH_INSN1(ret, location, putobject, regexp);
10133 case PM_RESCUE_NODE:
10136 pm_compile_rescue_node(iseq, (
const pm_rescue_node_t *) node, &location, ret, popped, scope_node);
10138 case PM_RESCUE_MODIFIER_NODE: {
10141 const pm_rescue_modifier_node_t *cast = (
const pm_rescue_modifier_node_t *) node;
10143 pm_scope_node_t rescue_scope_node;
10144 pm_scope_node_init((
const pm_node_t *) cast, &rescue_scope_node, scope_node);
10146 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
10147 &rescue_scope_node,
10153 pm_scope_node_destroy(&rescue_scope_node);
10155 LABEL *lstart = NEW_LABEL(location.
line);
10156 LABEL *lend = NEW_LABEL(location.
line);
10157 LABEL *lcont = NEW_LABEL(location.
line);
10159 lstart->rescued = LABEL_RESCUE_BEG;
10160 lend->rescued = LABEL_RESCUE_END;
10162 PUSH_LABEL(ret, lstart);
10164 PUSH_LABEL(ret, lend);
10166 PUSH_INSN(ret, location, nop);
10167 PUSH_LABEL(ret, lcont);
10168 if (popped) PUSH_INSN(ret, location, pop);
10170 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
10171 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
10174 case PM_RETURN_NODE:
10180 pm_compile_return_node(iseq, (
const pm_return_node_t *) node, &location, ret, popped, scope_node);
10182 case PM_RETRY_NODE: {
10185 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10186 PUSH_INSN(ret, location, putnil);
10187 PUSH_INSN1(ret, location,
throw,
INT2FIX(TAG_RETRY));
10188 if (popped) PUSH_INSN(ret, location, pop);
10191 COMPILE_ERROR(iseq, location.
line,
"Invalid retry");
10196 case PM_SCOPE_NODE:
10197 pm_compile_scope_node(iseq, (pm_scope_node_t *) node, &location, ret, popped);
10199 case PM_SELF_NODE: {
10203 PUSH_INSN(ret, location, putself);
10207 case PM_SHAREABLE_CONSTANT_NODE: {
10210 const pm_shareable_constant_node_t *cast = (
const pm_shareable_constant_node_t *) node;
10211 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));
10213 switch (PM_NODE_TYPE(cast->
write)) {
10214 case PM_CONSTANT_WRITE_NODE:
10215 pm_compile_constant_write_node(iseq, (
const pm_constant_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10217 case PM_CONSTANT_AND_WRITE_NODE:
10218 pm_compile_constant_and_write_node(iseq, (
const pm_constant_and_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10220 case PM_CONSTANT_OR_WRITE_NODE:
10221 pm_compile_constant_or_write_node(iseq, (
const pm_constant_or_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10223 case PM_CONSTANT_OPERATOR_WRITE_NODE:
10224 pm_compile_constant_operator_write_node(iseq, (
const pm_constant_operator_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10226 case PM_CONSTANT_PATH_WRITE_NODE:
10227 pm_compile_constant_path_write_node(iseq, (
const pm_constant_path_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10229 case PM_CONSTANT_PATH_AND_WRITE_NODE:
10230 pm_compile_constant_path_and_write_node(iseq, (
const pm_constant_path_and_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10232 case PM_CONSTANT_PATH_OR_WRITE_NODE:
10233 pm_compile_constant_path_or_write_node(iseq, (
const pm_constant_path_or_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10235 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
10236 pm_compile_constant_path_operator_write_node(iseq, (
const pm_constant_path_operator_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10239 rb_bug(
"Unexpected node type for shareable constant write: %s", pm_node_type_to_str(PM_NODE_TYPE(cast->
write)));
10245 case PM_SINGLETON_CLASS_NODE: {
10248 const pm_singleton_class_node_t *cast = (
const pm_singleton_class_node_t *) node;
10250 pm_scope_node_t next_scope_node;
10251 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
10252 const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit(
"singleton class"), ISEQ_TYPE_CLASS, location.
line);
10253 pm_scope_node_destroy(&next_scope_node);
10256 PUSH_INSN(ret, location, putnil);
10259 CONST_ID(singletonclass,
"singletonclass");
10260 PUSH_INSN3(ret, location, defineclass,
ID2SYM(singletonclass), child_iseq,
INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
10262 if (popped) PUSH_INSN(ret, location, pop);
10267 case PM_SOURCE_ENCODING_NODE: {
10271 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10272 PUSH_INSN1(ret, location, putobject, value);
10276 case PM_SOURCE_FILE_NODE: {
10280 const pm_source_file_node_t *cast = (
const pm_source_file_node_t *) node;
10281 VALUE string = pm_source_file_value(cast, scope_node);
10283 if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_FROZEN)) {
10284 PUSH_INSN1(ret, location, putobject,
string);
10286 else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
10287 PUSH_INSN1(ret, location, putstring,
string);
10290 PUSH_INSN1(ret, location, putchilledstring,
string);
10295 case PM_SOURCE_LINE_NODE: {
10299 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10300 PUSH_INSN1(ret, location, putobject, value);
10304 case PM_SPLAT_NODE: {
10307 const pm_splat_node_t *cast = (
const pm_splat_node_t *) node;
10313 PUSH_INSN1(ret, location, splatarray,
Qtrue);
10317 case PM_STATEMENTS_NODE: {
10319 const pm_statements_node_t *cast = (
const pm_statements_node_t *) node;
10320 const pm_node_list_t *body = &cast->
body;
10322 if (body->
size > 0) {
10323 for (
size_t index = 0; index < body->
size - 1; index++) {
10324 PM_COMPILE_POPPED(body->
nodes[index]);
10326 PM_COMPILE(body->
nodes[body->
size - 1]);
10329 PUSH_INSN(ret, location, putnil);
10333 case PM_STRING_NODE: {
10337 const pm_string_node_t *cast = (
const pm_string_node_t *) node;
10338 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
10340 if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FROZEN)) {
10341 PUSH_INSN1(ret, location, putobject, value);
10343 else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
10344 PUSH_INSN1(ret, location, putstring, value);
10347 PUSH_INSN1(ret, location, putchilledstring, value);
10352 case PM_SUPER_NODE:
10356 pm_compile_super_node(iseq, (
const pm_super_node_t *) node, &location, ret, popped, scope_node);
10358 case PM_SYMBOL_NODE: {
10362 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10363 PUSH_INSN1(ret, location, putobject, value);
10367 case PM_TRUE_NODE: {
10371 PUSH_INSN1(ret, location, putobject,
Qtrue);
10375 case PM_UNDEF_NODE: {
10378 const pm_undef_node_t *cast = (
const pm_undef_node_t *) node;
10379 const pm_node_list_t *names = &cast->
names;
10381 for (
size_t index = 0; index < names->
size; index++) {
10382 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10383 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
10385 PM_COMPILE_NOT_POPPED(names->
nodes[index]);
10386 PUSH_SEND(ret, location, id_core_undef_method,
INT2NUM(2));
10388 if (index < names->size - 1) {
10389 PUSH_INSN(ret, location, pop);
10393 if (popped) PUSH_INSN(ret, location, pop);
10396 case PM_UNLESS_NODE: {
10402 const pm_unless_node_t *cast = (
const pm_unless_node_t *) node;
10403 const pm_statements_node_t *statements = NULL;
10405 statements = ((
const pm_else_node_t *) cast->
else_clause)->statements;
10408 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);
10411 case PM_UNTIL_NODE: {
10417 const pm_until_node_t *cast = (
const pm_until_node_t *) node;
10418 pm_compile_loop(iseq, &location, cast->
base.
flags, PM_UNTIL_NODE, (
const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10421 case PM_WHILE_NODE: {
10427 const pm_while_node_t *cast = (
const pm_while_node_t *) node;
10428 pm_compile_loop(iseq, &location, cast->
base.
flags, PM_WHILE_NODE, (
const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10431 case PM_X_STRING_NODE: {
10434 const pm_x_string_node_t *cast = (
const pm_x_string_node_t *) node;
10435 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
10437 PUSH_INSN(ret, location, putself);
10438 PUSH_INSN1(ret, location, putobject, value);
10439 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
10440 if (popped) PUSH_INSN(ret, location, pop);
10444 case PM_YIELD_NODE:
10450 pm_compile_yield_node(iseq, (
const pm_yield_node_t *) node, &location, ret, popped, scope_node);
10453 rb_raise(
rb_eNotImpError,
"node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
10458#undef PM_CONTAINER_P
10462pm_iseq_pre_execution_p(rb_iseq_t *iseq)
10464 switch (ISEQ_BODY(iseq)->
type) {
10465 case ISEQ_TYPE_TOP:
10466 case ISEQ_TYPE_EVAL:
10467 case ISEQ_TYPE_MAIN:
10482pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
10486 if (pm_iseq_pre_execution_p(iseq)) {
10498 pm_compile_node(iseq, (
const pm_node_t *) node, body,
false, node);
10502 PUSH_SEQ(ret, pre);
10503 PUSH_SEQ(ret, body);
10508 pm_compile_node(iseq, (
const pm_node_t *) node, ret,
false, node);
10511 CHECK(iseq_setup_insn(iseq, ret));
10512 return iseq_setup(iseq, ret);
10522 if (result->
node.ast_node != NULL) {
10523 pm_node_destroy(&result->
parser, result->
node.ast_node);
10528 pm_scope_node_destroy(&result->
node);
10531 pm_parser_free(&result->
parser);
10532 pm_string_free(&result->
input);
10533 pm_options_free(&result->
options);
10569#define PM_COLOR_BOLD "\033[1m"
10570#define PM_COLOR_GRAY "\033[2m"
10571#define PM_COLOR_RED "\033[1;31m"
10572#define PM_COLOR_RESET "\033[m"
10573#define PM_ERROR_TRUNCATE 30
10578 if (errors == NULL)
return NULL;
10584 pm_line_column_t start = pm_newline_list_line_column(newline_list, error->location.
start, start_line);
10585 pm_line_column_t end = pm_newline_list_line_column(newline_list, error->location.end, start_line);
10593 (index < error_list->size) &&
10594 (errors[index].error != NULL) &&
10596 (errors[index].line < start.
line) ||
10603 if (index + 1 < error_list->
size) {
10604 memmove(&errors[index + 1], &errors[index],
sizeof(
pm_parse_error_t) * (error_list->
size - index - 1));
10608 uint32_t column_end;
10610 column_end = end.
column;
10612 column_end = (uint32_t) (newline_list->
offsets[start.
line - start_line + 1] - newline_list->
offsets[start.
line - start_line] - 1);
10616 if (start.
column == column_end) column_end++;
10620 .line = start.
line,
10621 .column_start = start.
column,
10622 .column_end = column_end
10630#define pm_buffer_append_literal(buffer, str) pm_buffer_append_string(buffer, str, rb_strlen_lit(str))
10633pm_parse_errors_format_line(
const pm_parser_t *parser,
const pm_newline_list_t *newline_list,
const char *number_prefix, int32_t line, uint32_t column_start, uint32_t column_end,
pm_buffer_t *buffer) {
10634 int32_t line_delta = line - parser->
start_line;
10635 assert(line_delta >= 0);
10637 size_t index = (size_t) line_delta;
10638 assert(index < newline_list->size);
10640 const uint8_t *start = &parser->
start[newline_list->
offsets[index]];
10641 const uint8_t *end;
10643 if (index >= newline_list->
size - 1) {
10646 end = &parser->
start[newline_list->
offsets[index + 1]];
10649 pm_buffer_append_format(buffer, number_prefix, line);
10652 bool truncate_end =
false;
10653 if ((column_end != 0) && ((end - (start + column_end)) >= PM_ERROR_TRUNCATE)) {
10654 const uint8_t *end_candidate = start + column_end + PM_ERROR_TRUNCATE;
10656 for (
const uint8_t *ptr = start; ptr < end_candidate;) {
10661 if (char_width == 0)
break;
10665 if (ptr + char_width > end_candidate) {
10666 end_candidate = ptr;
10673 end = end_candidate;
10674 truncate_end =
true;
10678 if (column_start >= PM_ERROR_TRUNCATE) {
10679 pm_buffer_append_string(buffer,
"... ", 4);
10680 start += column_start;
10683 pm_buffer_append_string(buffer, (
const char *) start, (
size_t) (end - start));
10685 if (truncate_end) {
10686 pm_buffer_append_string(buffer,
" ...\n", 5);
10687 }
else if (end == parser->
end && end[-1] !=
'\n') {
10688 pm_buffer_append_string(buffer,
"\n", 1);
10697 assert(error_list->
size != 0);
10701 const int32_t start_line = parser->
start_line;
10704 pm_parse_error_t *errors = pm_parse_errors_format_sort(parser, error_list, newline_list);
10705 if (errors == NULL)
return;
10711 int32_t first_line_number = errors[0].
line;
10712 int32_t last_line_number = errors[error_list->
size - 1].
line;
10717 if (first_line_number < 0) first_line_number = (-first_line_number) * 10;
10718 if (last_line_number < 0) last_line_number = (-last_line_number) * 10;
10719 int32_t max_line_number = first_line_number > last_line_number ? first_line_number : last_line_number;
10721 if (max_line_number < 10) {
10722 if (highlight > 0) {
10724 .number_prefix = PM_COLOR_GRAY
"%1" PRIi32
" | " PM_COLOR_RESET,
10725 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10726 .divider = PM_COLOR_GRAY
" ~~~~~" PM_COLOR_RESET
"\n"
10730 .number_prefix =
"%1" PRIi32
" | ",
10731 .blank_prefix =
" | ",
10732 .divider =
" ~~~~~\n"
10735 }
else if (max_line_number < 100) {
10736 if (highlight > 0) {
10738 .number_prefix = PM_COLOR_GRAY
"%2" PRIi32
" | " PM_COLOR_RESET,
10739 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10740 .divider = PM_COLOR_GRAY
" ~~~~~~" PM_COLOR_RESET
"\n"
10744 .number_prefix =
"%2" PRIi32
" | ",
10745 .blank_prefix =
" | ",
10746 .divider =
" ~~~~~~\n"
10749 }
else if (max_line_number < 1000) {
10750 if (highlight > 0) {
10752 .number_prefix = PM_COLOR_GRAY
"%3" PRIi32
" | " PM_COLOR_RESET,
10753 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10754 .divider = PM_COLOR_GRAY
" ~~~~~~~" PM_COLOR_RESET
"\n"
10758 .number_prefix =
"%3" PRIi32
" | ",
10759 .blank_prefix =
" | ",
10760 .divider =
" ~~~~~~~\n"
10763 }
else if (max_line_number < 10000) {
10764 if (highlight > 0) {
10766 .number_prefix = PM_COLOR_GRAY
"%4" PRIi32
" | " PM_COLOR_RESET,
10767 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10768 .divider = PM_COLOR_GRAY
" ~~~~~~~~" PM_COLOR_RESET
"\n"
10772 .number_prefix =
"%4" PRIi32
" | ",
10773 .blank_prefix =
" | ",
10774 .divider =
" ~~~~~~~~\n"
10778 if (highlight > 0) {
10780 .number_prefix = PM_COLOR_GRAY
"%5" PRIi32
" | " PM_COLOR_RESET,
10781 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10782 .divider = PM_COLOR_GRAY
" ~~~~~~~~" PM_COLOR_RESET
"\n"
10786 .number_prefix =
"%5" PRIi32
" | ",
10787 .blank_prefix =
" | ",
10788 .divider =
" ~~~~~~~~\n"
10802 uint32_t last_column_start = 0;
10805 for (
size_t index = 0; index < error_list->
size; index++) {
10810 if (error->
line - last_line > 1) {
10811 if (error->
line - last_line > 2) {
10812 if ((index != 0) && (error->
line - last_line > 3)) {
10816 pm_buffer_append_string(buffer,
" ", 2);
10817 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, error->
line - 2, 0, 0, buffer);
10820 pm_buffer_append_string(buffer,
" ", 2);
10821 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, error->
line - 1, 0, 0, buffer);
10826 if ((index == 0) || (error->
line != last_line)) {
10827 if (highlight > 1) {
10828 pm_buffer_append_literal(buffer, PM_COLOR_RED
"> " PM_COLOR_RESET);
10829 }
else if (highlight > 0) {
10830 pm_buffer_append_literal(buffer, PM_COLOR_BOLD
"> " PM_COLOR_RESET);
10832 pm_buffer_append_literal(buffer,
"> ");
10839 for (
size_t next_index = index + 1; next_index < error_list->
size; next_index++) {
10840 if (errors[next_index].line != error->
line)
break;
10841 if (errors[next_index].column_end > column_end) column_end = errors[next_index].
column_end;
10847 const uint8_t *start = &parser->
start[newline_list->
offsets[error->
line - start_line]];
10848 if (start == parser->
end) pm_buffer_append_byte(buffer,
'\n');
10859 pm_buffer_append_string(buffer,
" ", 2);
10863 if (last_column_start >= PM_ERROR_TRUNCATE) {
10864 pm_buffer_append_string(buffer,
" ", 4);
10865 column = last_column_start;
10868 while (column < error->column_start) {
10869 pm_buffer_append_byte(buffer,
' ');
10871 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10872 column += (char_width == 0 ? 1 : char_width);
10875 if (highlight > 1) pm_buffer_append_literal(buffer, PM_COLOR_RED);
10876 else if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_BOLD);
10877 pm_buffer_append_byte(buffer,
'^');
10879 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10880 column += (char_width == 0 ? 1 : char_width);
10882 while (column < error->column_end) {
10883 pm_buffer_append_byte(buffer,
'~');
10885 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10886 column += (char_width == 0 ? 1 : char_width);
10889 if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_RESET);
10891 if (inline_messages) {
10892 pm_buffer_append_byte(buffer,
' ');
10893 assert(error->
error != NULL);
10896 pm_buffer_append_string(buffer, message, strlen(message));
10899 pm_buffer_append_byte(buffer,
'\n');
10903 last_line = error->
line;
10906 if (index == error_list->
size - 1) {
10907 next_line = (((int32_t) newline_list->
size) + parser->
start_line);
10916 next_line = errors[index + 1].
line;
10919 if (next_line - last_line > 1) {
10920 pm_buffer_append_string(buffer,
" ", 2);
10921 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, ++last_line, 0, 0, buffer);
10924 if (next_line - last_line > 1) {
10925 pm_buffer_append_string(buffer,
" ", 2);
10926 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, ++last_line, 0, 0, buffer);
10934#undef PM_ERROR_TRUNCATE
10935#undef PM_COLOR_GRAY
10937#undef PM_COLOR_RESET
10948 const size_t start_line = pm_newline_list_line_column(&parser->
newline_list, location->
start, 1).line;
10949 const size_t end_line = pm_newline_list_line_column(&parser->
newline_list, location->
end, 1).line;
10955 while (start < end) {
10956 if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0)
return false;
10972 bool valid_utf8 =
true;
10977 int highlight = rb_stderr_tty_p();
10979 const char *no_color = getenv(
"NO_COLOR");
10980 highlight = (no_color == NULL || no_color[0] ==
'\0') ? 2 : 1;
10984 switch (error->level) {
10991 if (valid_utf8 && !pm_parse_process_error_utf8_p(parser, &error->location)) {
10992 valid_utf8 =
false;
10999 int32_t line_number = (int32_t) pm_location_line_number(parser, &error->location);
11001 pm_buffer_append_format(
11003 "%.*s:%" PRIi32
": %s",
11004 (
int) pm_string_length(filepath),
11005 pm_string_source(filepath),
11010 if (pm_parse_process_error_utf8_p(parser, &error->location)) {
11011 pm_buffer_append_byte(&buffer,
'\n');
11014 pm_list_t error_list = { .size = 1, .head = list_node, .tail = list_node };
11016 pm_parse_errors_format(parser, &error_list, &buffer, highlight,
false);
11019 VALUE value =
rb_exc_new(rb_eArgError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
11020 pm_buffer_free(&buffer);
11036 pm_buffer_append_format(
11038 "%.*s:%" PRIi32
": syntax error%s found\n",
11039 (
int) pm_string_length(filepath),
11040 pm_string_source(filepath),
11041 (int32_t) pm_location_line_number(parser, &head->location),
11046 pm_parse_errors_format(parser, &parser->
error_list, &buffer, highlight,
true);
11050 if (error != head) pm_buffer_append_byte(&buffer,
'\n');
11051 pm_buffer_append_format(&buffer,
"%.*s:%" PRIi32
": %s", (
int) pm_string_length(filepath), pm_string_source(filepath), (int32_t) pm_location_line_number(parser, &error->location), error->message);
11055 VALUE message = rb_enc_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer), result->
node.encoding);
11059 VALUE path = rb_enc_str_new((
const char *) pm_string_source(filepath), pm_string_length(filepath), filepath_encoding);
11062 pm_buffer_free(&buffer);
11079 pm_scope_node_t *scope_node = &result->
node;
11081 int coverage_enabled = scope_node->coverage_enabled;
11083 pm_scope_node_init(node, scope_node, NULL);
11086 scope_node->encoding = rb_enc_find(parser->
encoding->
name);
11087 if (!scope_node->encoding) rb_bug(
"Encoding not found %s!", parser->
encoding->
name);
11089 scope_node->coverage_enabled = coverage_enabled;
11093 if (script_lines != NULL) {
11098 size_t length = index == parser->
newline_list.
size - 1 ? ((size_t) (parser->
end - (parser->
start + offset))) : (parser->newline_list.offsets[index + 1] - offset);
11099 rb_ary_push(*script_lines, rb_enc_str_new((
const char *) parser->
start + offset, length, scope_node->encoding));
11107 const char *warning_filepath = (
const char *) pm_string_source(&parser->
filepath);
11110 int line = pm_location_line_number(parser, &warning->
location);
11113 rb_enc_compile_warning(scope_node->encoding, warning_filepath, line,
"%s", warning->
message);
11116 rb_enc_compile_warn(scope_node->encoding, warning_filepath, line,
"%s", warning->
message);
11122 VALUE error = pm_parse_process_error(result);
11131 scope_node->parser = parser;
11136 scope_node->constants[index] = rb_intern3((
const char *) constant->
start, constant->
length, scope_node->encoding);
11139 scope_node->index_lookup_table = st_init_numtable();
11141 for (
size_t index = 0; index < locals->
size; index++) {
11142 st_insert(scope_node->index_lookup_table, locals->
ids[index], index);
11156pm_options_frozen_string_literal_init(
pm_options_t *options)
11158 int frozen_string_literal = rb_iseq_opt_frozen_string_literal();
11160 switch (frozen_string_literal) {
11161 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11163 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11164 pm_options_frozen_string_literal_set(options,
false);
11166 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11167 pm_options_frozen_string_literal_set(options,
true);
11170 rb_bug(
"pm_options_frozen_string_literal_init: invalid frozen_string_literal=%d", frozen_string_literal);
11180pm_parse_file_script_lines(
const pm_scope_node_t *scope_node,
const pm_parser_t *parser)
11183 const char *start = (
const char *) parser->
start;
11184 const char *end = (
const char *) parser->
end;
11189 size_t last_offset = newline_list->
offsets[newline_list->
size - 1];
11190 bool last_push = start + last_offset != end;
11195 for (
size_t index = 0; index < newline_list->
size - 1; index++) {
11196 size_t offset = newline_list->
offsets[index];
11197 size_t length = newline_list->
offsets[index + 1] - offset;
11199 rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
11204 rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), scope_node->encoding));
11214pm_read_file(
pm_string_t *
string,
const char *filepath)
11218 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
11221 WCHAR *wfilepath =
xmalloc(
sizeof(WCHAR) * ((
size_t) length));
11222 if ((wfilepath == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, wfilepath, length) == 0)) {
11227 HANDLE file = CreateFileW(wfilepath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
11228 if (file == INVALID_HANDLE_VALUE) {
11231 if (GetLastError() == ERROR_ACCESS_DENIED) {
11232 DWORD attributes = GetFileAttributesW(wfilepath);
11233 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
11243 DWORD file_size = GetFileSize(file, NULL);
11244 if (file_size == INVALID_FILE_SIZE) {
11252 if (file_size == 0) {
11255 const uint8_t source[] =
"";
11256 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11261 HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
11262 if (mapping == NULL) {
11269 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
11270 CloseHandle(mapping);
11274 if (source == NULL) {
11278 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
11280#elif defined(_POSIX_MAPPED_FILES)
11282 const int open_mode = O_RDONLY | O_NONBLOCK;
11283 int fd = open(filepath, open_mode);
11290 if (fstat(fd, &sb) == -1) {
11296 if (S_ISDIR(sb.st_mode)) {
11304 if (S_ISFIFO(sb.st_mode) || S_ISCHR(sb.st_mode)) {
11313 long len = RSTRING_LEN(contents);
11318 size_t length = (size_t)
len;
11319 uint8_t *source = malloc(length);
11320 memcpy(source, RSTRING_PTR(contents), length);
11321 *
string = (
pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
11327 size_t size = (size_t) sb.st_size;
11328 uint8_t *source = NULL;
11332 const uint8_t source[] =
"";
11333 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11337 source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
11338 if (source == MAP_FAILED) {
11344 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
11347 return pm_string_file_init(
string, filepath);
11361 pm_options_frozen_string_literal_init(&result->
options);
11370 err = rb_w32_map_errno(GetLastError());
11402 pm_options_filepath_set(&result->
options, RSTRING_PTR(filepath));
11405 pm_options_version_for_current_ruby_set(&result->
options);
11407 pm_parser_init(&result->
parser, pm_string_source(&result->
input), pm_string_length(&result->
input), &result->
options);
11408 pm_node_t *node = pm_parse(&result->
parser);
11410 VALUE error = pm_parse_process(result, node, script_lines);
11415 ID id_script_lines = rb_intern(
"SCRIPT_LINES__");
11421 rb_hash_aset(constant_script_lines, filepath, pm_parse_file_script_lines(&result->
node, &result->
parser));
11435 VALUE error = pm_load_file(result, filepath,
false);
11436 if (
NIL_P(error)) {
11437 error = pm_parse_file(result, filepath, script_lines);
11452 rb_encoding *encoding = rb_enc_get(source);
11453 if (!rb_enc_asciicompat(encoding)) {
11457 pm_options_frozen_string_literal_init(&result->
options);
11458 pm_string_constant_init(&result->
input, RSTRING_PTR(source), RSTRING_LEN(source));
11459 pm_options_encoding_set(&result->
options, rb_enc_name(encoding));
11462 pm_options_filepath_set(&result->
options, RSTRING_PTR(filepath));
11465 pm_options_version_for_current_ruby_set(&result->
options);
11467 pm_parser_init(&result->
parser, pm_string_source(&result->
input), pm_string_length(&result->
input), &result->
options);
11468 pm_node_t *node = pm_parse(&result->
parser);
11470 return pm_parse_process(result, node, script_lines);
11479pm_parse_stdin_eof(
void *stream)
11482 return wrapped_stdin->eof_seen;
11485VALUE rb_io_gets_limit_internal(
VALUE io,
long limit);
11491pm_parse_stdin_fgets(
char *
string,
int size,
void *stream)
11497 VALUE line = rb_io_gets_limit_internal(wrapped_stdin->rb_stdin, size - 1);
11502 const char *cstr = RSTRING_PTR(line);
11503 long length = RSTRING_LEN(line);
11505 memcpy(
string, cstr, length);
11506 string[length] =
'\0';
11511 if (length < (size - 1) &&
string[length - 1] !=
'\n') {
11512 wrapped_stdin->eof_seen = 1;
11519void rb_reset_argf_lineno(
long n);
11529 pm_options_frozen_string_literal_init(&result->
options);
11537 pm_node_t *node = pm_parse_stream(&result->
parser, &buffer, (
void *) &wrapped_stdin, pm_parse_stdin_fgets, pm_parse_stdin_eof, &result->
options);
11542 pm_string_owned_init(&result->
input, (uint8_t *) pm_buffer_value(&buffer), pm_buffer_length(&buffer));
11546 rb_reset_argf_lineno(0);
11548 return pm_parse_process(result, node, NULL);
11551#define PM_VERSION_FOR_RELEASE(major, minor) PM_VERSION_FOR_RELEASE_IMPL(major, minor)
11552#define PM_VERSION_FOR_RELEASE_IMPL(major, minor) PM_OPTIONS_VERSION_CRUBY_##major##_##minor
11554void pm_options_version_for_current_ruby_set(
pm_options_t *options) {
11559#define NEW_ISEQ OLD_ISEQ
11561#undef NEW_CHILD_ISEQ
11562#define NEW_CHILD_ISEQ OLD_CHILD_ISEQ
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
@ PM_ERROR_LEVEL_ARGUMENT
For errors that should raise an argument error.
@ PM_ERROR_LEVEL_LOAD
For errors that should raise a load error.
@ PM_ERROR_LEVEL_SYNTAX
For errors that should raise a syntax error.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_CLASS
Encountered a new class.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ALLOCV
Old name of RB_ALLOCV.
#define ALLOC
Old name of RB_ALLOC.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ID2SYM
Old name of RB_ID2SYM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define FIXABLE
Old name of RB_FIXABLE.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define rb_exc_new3
Old name of rb_exc_new_str.
#define 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 DBL2NUM
Old name of rb_float_new.
#define xcalloc
Old name of ruby_xcalloc.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eLoadError
LoadError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_errinfo(void)
This is the same as $! in Ruby.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_syserr_new(int n, const char *mesg)
Creates an exception object that represents the given C errno.
VALUE rb_cArray
Array class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_stdin
STDIN constant.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
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_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it returns a "f"string.
VALUE rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_cat(VALUE ary, const VALUE *train, long len)
Destructively appends multiple elements at the end of the array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_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.
VALUE rb_range_new(VALUE beg, VALUE end, int excl)
Creates a new Range.
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
@ RUBY_IO_READABLE
IO::READABLE
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout)
Blocks until the passed IO is ready for the passed events.
int len
Length of the buffer.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
#define RUBY_API_VERSION_MAJOR
Major version.
#define RUBY_API_VERSION_MINOR
Minor version.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
struct pm_options pm_options_t
The options that can be passed to the parser.
struct pm_parser pm_parser_t
The parser used to parse Ruby source.
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
struct pm_list_node pm_list_node_t
This struct represents an abstract linked list that provides common functionality.
pm_string_init_result_t
Represents the result of calling pm_string_mapped_init or pm_string_file_init.
@ PM_STRING_INIT_SUCCESS
Indicates that the string was successfully initialized.
@ PM_STRING_INIT_ERROR_GENERIC
Indicates a generic error from a string_*_init function, where the type of error should be read from ...
@ PM_STRING_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
#define PM_ENCODING_US_ASCII_ENTRY
This is the US-ASCII encoding.
#define PM_NODE_LIST_FOREACH(list, index, node)
Loop through each node in the node list, writing each node to the given pm_node_t pointer.
The main header file for the prism parser.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define errno
Ractor-aware version of errno.
#define RTEST
This is an old name of RB_TEST.
struct pm_node * old_name
AliasGlobalVariableNode::old_name.
struct pm_node * new_name
AliasGlobalVariableNode::new_name.
struct pm_node * old_name
AliasMethodNode::old_name.
struct pm_node * new_name
AliasMethodNode::new_name.
struct pm_node * left
AlternationPatternNode::left.
struct pm_node * right
AlternationPatternNode::right.
struct pm_node * left
AndNode::left.
struct pm_node * right
AndNode::right.
pm_node_t base
The embedded base node.
struct pm_node_list arguments
ArgumentsNode::arguments.
struct pm_node_list elements
ArrayNode::elements.
struct pm_node_list requireds
ArrayPatternNode::requireds.
struct pm_node * rest
ArrayPatternNode::rest.
struct pm_node * constant
ArrayPatternNode::constant.
struct pm_node_list posts
ArrayPatternNode::posts.
struct pm_node * value
AssocNode::value.
struct pm_node * key
AssocNode::key.
struct pm_node * value
AssocSplatNode::value.
pm_node_t base
The embedded base node.
struct pm_ensure_node * ensure_clause
BeginNode::ensure_clause.
struct pm_rescue_node * rescue_clause
BeginNode::rescue_clause.
struct pm_statements_node * statements
BeginNode::statements.
struct pm_else_node * else_clause
BeginNode::else_clause.
struct pm_node * expression
BlockArgumentNode::expression.
struct pm_node * parameters
BlockNode::parameters.
struct pm_node * body
BlockNode::body.
pm_constant_id_list_t locals
BlockNode::locals.
struct pm_arguments_node * arguments
BreakNode::arguments.
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
struct pm_node * value
CallAndWriteNode::value.
pm_constant_id_t read_name
CallAndWriteNode::read_name.
pm_constant_id_t write_name
CallAndWriteNode::write_name.
struct pm_node * receiver
CallAndWriteNode::receiver.
pm_location_t closing_loc
CallNode::closing_loc.
struct pm_node * receiver
CallNode::receiver.
pm_constant_id_t name
CallNode::name.
pm_node_t base
The embedded base node.
pm_location_t message_loc
CallNode::message_loc.
struct pm_arguments_node * arguments
CallNode::arguments.
struct pm_node * block
CallNode::block.
pm_constant_id_t read_name
CallOperatorWriteNode::read_name.
pm_constant_id_t binary_operator
CallOperatorWriteNode::binary_operator.
struct pm_node * receiver
CallOperatorWriteNode::receiver.
pm_constant_id_t write_name
CallOperatorWriteNode::write_name.
struct pm_node * value
CallOperatorWriteNode::value.
struct pm_node * receiver
CallOrWriteNode::receiver.
struct pm_node * value
CallOrWriteNode::value.
pm_constant_id_t write_name
CallOrWriteNode::write_name.
pm_constant_id_t read_name
CallOrWriteNode::read_name.
pm_constant_id_t name
CallTargetNode::name.
struct pm_node * receiver
CallTargetNode::receiver.
struct pm_local_variable_target_node * target
CapturePatternNode::target.
struct pm_node * value
CapturePatternNode::value.
struct pm_node_list conditions
CaseMatchNode::conditions.
struct pm_else_node * else_clause
CaseMatchNode::else_clause.
struct pm_node * predicate
CaseMatchNode::predicate.
struct pm_node * predicate
CaseNode::predicate.
struct pm_else_node * else_clause
CaseNode::else_clause.
struct pm_node_list conditions
CaseNode::conditions.
struct pm_node * constant_path
ClassNode::constant_path.
pm_constant_id_list_t locals
ClassNode::locals.
pm_constant_id_t name
ClassNode::name.
struct pm_node * body
ClassNode::body.
struct pm_node * superclass
ClassNode::superclass.
struct pm_node * value
ClassVariableAndWriteNode::value.
pm_constant_id_t name
ClassVariableAndWriteNode::name.
pm_constant_id_t name
ClassVariableOperatorWriteNode::name.
pm_constant_id_t binary_operator
ClassVariableOperatorWriteNode::binary_operator.
struct pm_node * value
ClassVariableOperatorWriteNode::value.
pm_constant_id_t name
ClassVariableOrWriteNode::name.
struct pm_node * value
ClassVariableOrWriteNode::value.
pm_constant_id_t name
ClassVariableReadNode::name.
pm_constant_id_t name
ClassVariableTargetNode::name.
struct pm_node * value
ClassVariableWriteNode::value.
pm_constant_id_t name
ClassVariableWriteNode::name.
pm_location_t name_loc
ConstantAndWriteNode::name_loc.
pm_constant_id_t name
ConstantAndWriteNode::name.
struct pm_node * value
ConstantAndWriteNode::value.
size_t size
The number of constant ids in the list.
size_t capacity
The number of constant ids that have been allocated in the list.
pm_constant_id_t * ids
The constant ids in the list.
pm_constant_id_t name
ConstantOperatorWriteNode::name.
pm_location_t name_loc
ConstantOperatorWriteNode::name_loc.
pm_constant_id_t binary_operator
ConstantOperatorWriteNode::binary_operator.
struct pm_node * value
ConstantOperatorWriteNode::value.
pm_location_t name_loc
ConstantOrWriteNode::name_loc.
pm_constant_id_t name
ConstantOrWriteNode::name.
struct pm_node * value
ConstantOrWriteNode::value.
struct pm_constant_path_node * target
ConstantPathAndWriteNode::target.
struct pm_node * value
ConstantPathAndWriteNode::value.
pm_constant_id_t name
ConstantPathNode::name.
struct pm_node * parent
ConstantPathNode::parent.
struct pm_constant_path_node * target
ConstantPathOperatorWriteNode::target.
struct pm_node * value
ConstantPathOperatorWriteNode::value.
pm_constant_id_t binary_operator
ConstantPathOperatorWriteNode::binary_operator.
struct pm_node * value
ConstantPathOrWriteNode::value.
struct pm_constant_path_node * target
ConstantPathOrWriteNode::target.
struct pm_node * parent
ConstantPathTargetNode::parent.
pm_constant_id_t name
ConstantPathTargetNode::name.
struct pm_constant_path_node * target
ConstantPathWriteNode::target.
struct pm_node * value
ConstantPathWriteNode::value.
uint32_t size
The number of buckets in the hash map.
pm_constant_t * constants
The constants that are stored in the buckets.
pm_node_t base
The embedded base node.
pm_constant_id_t name
ConstantReadNode::name.
A constant in the pool which effectively stores a string.
size_t length
The length of the string.
const uint8_t * start
A pointer to the start of the string.
pm_constant_id_t name
ConstantTargetNode::name.
struct pm_node * value
ConstantWriteNode::value.
pm_constant_id_t name
ConstantWriteNode::name.
struct pm_parameters_node * parameters
DefNode::parameters.
pm_constant_id_t name
DefNode::name.
struct pm_node * body
DefNode::body.
struct pm_node * receiver
DefNode::receiver.
pm_node_t base
The embedded base node.
pm_constant_id_list_t locals
DefNode::locals.
struct pm_node * value
DefinedNode::value.
This struct represents a diagnostic generated during parsing.
pm_location_t location
The location of the diagnostic in the source.
const char * message
The message associated with the diagnostic.
pm_list_node_t node
The embedded base node.
uint8_t level
The level of the diagnostic, see pm_error_level_t and pm_warning_level_t for possible values.
struct pm_statements_node * statements
ElseNode::statements.
struct pm_statements_node * statements
EmbeddedStatementsNode::statements.
struct pm_node * variable
EmbeddedVariableNode::variable.
This struct defines the functions necessary to implement the encoding interface so we can determine h...
size_t(* char_width)(const uint8_t *b, ptrdiff_t n)
Return the number of bytes that the next character takes if it is valid in the encoding.
const char * name
The name of the encoding.
struct pm_statements_node * statements
EnsureNode::statements.
struct pm_node * constant
FindPatternNode::constant.
struct pm_node * right
FindPatternNode::right.
struct pm_node_list requireds
FindPatternNode::requireds.
struct pm_splat_node * left
FindPatternNode::left.
pm_node_t base
The embedded base node.
struct pm_node * left
FlipFlopNode::left.
struct pm_node * right
FlipFlopNode::right.
double value
FloatNode::value.
struct pm_statements_node * statements
ForNode::statements.
struct pm_node * collection
ForNode::collection.
struct pm_block_node * block
ForwardingSuperNode::block.
struct pm_node * value
GlobalVariableAndWriteNode::value.
pm_constant_id_t name
GlobalVariableAndWriteNode::name.
pm_constant_id_t name
GlobalVariableOperatorWriteNode::name.
pm_constant_id_t binary_operator
GlobalVariableOperatorWriteNode::binary_operator.
struct pm_node * value
GlobalVariableOperatorWriteNode::value.
pm_constant_id_t name
GlobalVariableOrWriteNode::name.
struct pm_node * value
GlobalVariableOrWriteNode::value.
pm_constant_id_t name
GlobalVariableReadNode::name.
pm_constant_id_t name
GlobalVariableTargetNode::name.
struct pm_node * value
GlobalVariableWriteNode::value.
pm_constant_id_t name
GlobalVariableWriteNode::name.
struct pm_node_list elements
HashNode::elements.
struct pm_node_list elements
HashPatternNode::elements.
struct pm_node * rest
HashPatternNode::rest.
struct pm_node * constant
HashPatternNode::constant.
struct pm_node * predicate
IfNode::predicate.
struct pm_statements_node * statements
IfNode::statements.
struct pm_node * numeric
ImaginaryNode::numeric.
struct pm_node * value
ImplicitNode::value.
struct pm_statements_node * statements
InNode::statements.
struct pm_node * pattern
InNode::pattern.
struct pm_arguments_node * arguments
IndexAndWriteNode::arguments.
struct pm_node * receiver
IndexAndWriteNode::receiver.
struct pm_block_argument_node * block
IndexAndWriteNode::block.
struct pm_node * value
IndexAndWriteNode::value.
struct pm_block_argument_node * block
IndexOperatorWriteNode::block.
struct pm_node * value
IndexOperatorWriteNode::value.
struct pm_arguments_node * arguments
IndexOperatorWriteNode::arguments.
pm_constant_id_t binary_operator
IndexOperatorWriteNode::binary_operator.
struct pm_node * receiver
IndexOperatorWriteNode::receiver.
struct pm_block_argument_node * block
IndexOrWriteNode::block.
struct pm_node * receiver
IndexOrWriteNode::receiver.
struct pm_node * value
IndexOrWriteNode::value.
struct pm_arguments_node * arguments
IndexOrWriteNode::arguments.
struct pm_node * receiver
IndexTargetNode::receiver.
struct pm_arguments_node * arguments
IndexTargetNode::arguments.
struct pm_block_argument_node * block
IndexTargetNode::block.
struct pm_node * value
InstanceVariableAndWriteNode::value.
pm_constant_id_t name
InstanceVariableAndWriteNode::name.
struct pm_node * value
InstanceVariableOperatorWriteNode::value.
pm_constant_id_t binary_operator
InstanceVariableOperatorWriteNode::binary_operator.
pm_constant_id_t name
InstanceVariableOperatorWriteNode::name.
struct pm_node * value
InstanceVariableOrWriteNode::value.
pm_constant_id_t name
InstanceVariableOrWriteNode::name.
pm_constant_id_t name
InstanceVariableReadNode::name.
pm_constant_id_t name
InstanceVariableTargetNode::name.
pm_constant_id_t name
InstanceVariableWriteNode::name.
struct pm_node * value
InstanceVariableWriteNode::value.
pm_integer_t value
IntegerNode::value.
A structure represents an arbitrary-sized integer.
size_t length
The number of allocated values.
uint32_t value
Embedded value for small integer.
uint32_t * values
List of 32-bit integers.
bool negative
Whether or not the integer is negative.
struct pm_node_list parts
InterpolatedStringNode::parts.
struct pm_node_list parts
InterpolatedSymbolNode::parts.
struct pm_node_list parts
InterpolatedXStringNode::parts.
struct pm_node_list elements
KeywordHashNode::elements.
struct pm_node * body
LambdaNode::body.
pm_location_t opening_loc
LambdaNode::opening_loc.
struct pm_node * parameters
LambdaNode::parameters.
pm_location_t operator_loc
LambdaNode::operator_loc.
pm_constant_id_list_t locals
LambdaNode::locals.
A line and column in a string.
uint32_t column
The column number.
int32_t line
The line number.
struct pm_list_node * next
A pointer to the next node in the list.
This represents the overall linked list.
pm_list_node_t * tail
A pointer to the tail of the list.
pm_list_node_t * head
A pointer to the head of the list.
size_t size
The size of the list.
pm_constant_id_t name
LocalVariableAndWriteNode::name.
uint32_t depth
LocalVariableAndWriteNode::depth.
struct pm_node * value
LocalVariableAndWriteNode::value.
uint32_t depth
LocalVariableOperatorWriteNode::depth.
pm_constant_id_t binary_operator
LocalVariableOperatorWriteNode::binary_operator.
struct pm_node * value
LocalVariableOperatorWriteNode::value.
pm_constant_id_t name
LocalVariableOperatorWriteNode::name.
uint32_t depth
LocalVariableOrWriteNode::depth.
struct pm_node * value
LocalVariableOrWriteNode::value.
pm_constant_id_t name
LocalVariableOrWriteNode::name.
uint32_t depth
LocalVariableReadNode::depth.
pm_constant_id_t name
LocalVariableReadNode::name.
uint32_t depth
LocalVariableTargetNode::depth.
pm_constant_id_t name
LocalVariableTargetNode::name.
struct pm_node * value
LocalVariableWriteNode::value.
uint32_t depth
LocalVariableWriteNode::depth.
pm_constant_id_t name
LocalVariableWriteNode::name.
This represents a range of bytes in the source string to which a node or token corresponds.
const uint8_t * start
A pointer to the start location of the range in the source.
const uint8_t * end
A pointer to the end location of the range in the source.
struct pm_node * pattern
MatchPredicateNode::pattern.
struct pm_node * value
MatchPredicateNode::value.
struct pm_node * value
MatchRequiredNode::value.
struct pm_node * pattern
MatchRequiredNode::pattern.
struct pm_node_list targets
MatchWriteNode::targets.
struct pm_call_node * call
MatchWriteNode::call.
struct pm_node * constant_path
ModuleNode::constant_path.
struct pm_node * body
ModuleNode::body.
pm_constant_id_list_t locals
ModuleNode::locals.
pm_constant_id_t name
ModuleNode::name.
struct pm_node_list lefts
MultiTargetNode::lefts.
struct pm_node * rest
MultiTargetNode::rest.
struct pm_node_list rights
MultiTargetNode::rights.
This is a node in the multi target state linked list.
As we're compiling a multi target, we need to track additional information whenever there is a parent...
struct pm_node * value
MultiWriteNode::value.
struct pm_node * rest
MultiWriteNode::rest.
struct pm_node_list rights
MultiWriteNode::rights.
struct pm_node_list lefts
MultiWriteNode::lefts.
A list of offsets of newlines in a string.
const uint8_t * start
A pointer to the start of the source string.
size_t * offsets
The list of offsets.
size_t size
The number of offsets in the list.
struct pm_arguments_node * arguments
NextNode::arguments.
size_t size
The number of nodes in the list.
struct pm_node ** nodes
The nodes in the list.
This compiler defines its own concept of the location of a node.
int32_t line
This is the line number of a node.
uint32_t node_id
This is a unique identifier for the node.
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_constant_id_t name
OptionalKeywordParameterNode::name.
struct pm_node * value
OptionalKeywordParameterNode::value.
struct pm_node * value
OptionalParameterNode::value.
pm_constant_id_t name
OptionalParameterNode::name.
pm_options_version_t version
The version of prism that we should be parsing with.
struct pm_node * left
OrNode::left.
struct pm_node * right
OrNode::right.
struct pm_node * rest
ParametersNode::rest.
struct pm_node_list requireds
ParametersNode::requireds.
struct pm_block_parameter_node * block
ParametersNode::block.
struct pm_node_list optionals
ParametersNode::optionals.
struct pm_node_list posts
ParametersNode::posts.
pm_node_t base
The embedded base node.
struct pm_node * keyword_rest
ParametersNode::keyword_rest.
struct pm_node_list keywords
ParametersNode::keywords.
struct pm_node * body
ParenthesesNode::body.
An error that is going to be formatted into the output.
pm_diagnostic_t * error
A pointer to the diagnostic that was generated during parsing.
uint32_t column_end
The column end of the diagnostic message.
int32_t line
The start line of the diagnostic message.
uint32_t column_start
The column start of the diagnostic message.
bool parsed
Whether or not this parse result has performed its parsing yet.
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_string_t input
The input that represents the source to be parsed.
pm_parser_t parser
The parser that will do the actual parsing.
pm_options_t options
The options that will be passed to the parser.
const pm_encoding_t * encoding
The encoding functions for the current file is attached to the parser as it's parsing so that it can ...
const uint8_t * end
The pointer to the end of the source.
pm_constant_pool_t constant_pool
This constant pool keeps all of the constants defined throughout the file so that we can reference th...
const uint8_t * start
The pointer to the start of the source.
pm_list_t error_list
The list of errors that have been found while parsing.
pm_list_t warning_list
The list of warnings that have been found while parsing.
int32_t start_line
The line number at the start of the parse.
pm_string_t filepath
This is the path of the file being parsed.
pm_newline_list_t newline_list
This is the list of newline offsets in the source file.
struct pm_node * variable
PinnedVariableNode::variable.
struct pm_statements_node * statements
PostExecutionNode::statements.
struct pm_statements_node * statements
PreExecutionNode::statements.
struct pm_statements_node * statements
ProgramNode::statements.
struct pm_node * right
RangeNode::right.
struct pm_node * left
RangeNode::left.
pm_integer_t denominator
RationalNode::denominator.
pm_integer_t numerator
RationalNode::numerator.
pm_constant_id_t name
RequiredParameterNode::name.
struct pm_node * rescue_expression
RescueModifierNode::rescue_expression.
struct pm_node * expression
RescueModifierNode::expression.
struct pm_rescue_node * subsequent
RescueNode::subsequent.
struct pm_node * reference
RescueNode::reference.
struct pm_node_list exceptions
RescueNode::exceptions.
struct pm_statements_node * statements
RescueNode::statements.
struct pm_arguments_node * arguments
ReturnNode::arguments.
rb_encoding * filepath_encoding
This is the encoding of the actual filepath object that will be used when a FILE node is compiled or ...
struct iseq_link_anchor * pre_execution_anchor
This will only be set on the top-level scope node.
VALUE * script_lines
This is a pointer to the list of script lines for the ISEQs that will be associated with this scope n...
struct pm_node * write
ShareableConstantNode::write.
pm_node_t base
The embedded base node.
pm_constant_id_list_t locals
SingletonClassNode::locals.
struct pm_node * expression
SingletonClassNode::expression.
struct pm_node * body
SingletonClassNode::body.
pm_string_t filepath
SourceFileNode::filepath.
struct pm_node * expression
SplatNode::expression.
struct pm_node_list body
StatementsNode::body.
pm_node_t base
The embedded base node.
pm_string_t unescaped
StringNode::unescaped.
A generic string type that can have various ownership semantics.
struct pm_arguments_node * arguments
SuperNode::arguments.
struct pm_node * block
SuperNode::block.
pm_string_t unescaped
SymbolNode::unescaped.
pm_node_t base
The embedded base node.
struct pm_node_list names
UndefNode::names.
struct pm_statements_node * statements
UnlessNode::statements.
struct pm_node * predicate
UnlessNode::predicate.
struct pm_else_node * else_clause
UnlessNode::else_clause.
pm_node_t base
The embedded base node.
pm_node_t base
The embedded base node.
pm_string_t unescaped
XStringNode::unescaped.
struct pm_arguments_node * arguments
YieldNode::arguments.
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.