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 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level),
Qtrue);
108#define PUSH_GETLOCAL(seq, location, idx, level) \
109 pm_iseq_add_getlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
111#define PUSH_SETLOCAL(seq, location, idx, level) \
112 pm_iseq_add_setlocal(iseq, (seq), (int) (location).line, (int) (location).node_id, (idx), (level))
118#define OLD_ISEQ NEW_ISEQ
121#define NEW_ISEQ(node, name, type, line_no) \
122 pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
124#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
127#define NEW_CHILD_ISEQ(node, name, type, line_no) \
128 pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
130#define PM_COMPILE(node) \
131 pm_compile_node(iseq, (node), ret, popped, scope_node)
133#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
134 pm_compile_node(iseq, (node), _ret, popped, scope_node)
136#define PM_COMPILE_POPPED(node) \
137 pm_compile_node(iseq, (node), ret, true, scope_node)
139#define PM_COMPILE_NOT_POPPED(node) \
140 pm_compile_node(iseq, (node), ret, false, scope_node)
142#define PM_NODE_START_LOCATION(parser, node) \
143 ((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 })
145#define PM_NODE_END_LOCATION(parser, node) \
146 ((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 })
148#define PM_LOCATION_START_LOCATION(parser, location, id) \
149 ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, (location)->start, (parser)->start_line), .node_id = id })
151#define PM_NODE_START_LINE_COLUMN(parser, node) \
152 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line)
154#define PM_NODE_END_LINE_COLUMN(parser, node) \
155 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line)
157#define PM_LOCATION_START_LINE_COLUMN(parser, location) \
158 pm_newline_list_line_column(&(parser)->newline_list, (location)->start, (parser)->start_line)
161pm_node_line_number(
const pm_parser_t *parser,
const pm_node_t *node)
179 if (integer->
values == NULL) {
184 unsigned char *bytes = (
unsigned char *) RSTRING_PTR(
string);
186 size_t offset = integer->
length * 8;
187 for (
size_t value_index = 0; value_index < integer->
length; value_index++) {
188 uint32_t value = integer->
values[value_index];
190 for (
int index = 0; index < 8; index++) {
191 int byte = (value >> (4 * index)) & 0xf;
192 bytes[--offset] =
byte < 10 ?
byte +
'0' :
byte - 10 +
'a';
200 result =
rb_funcall(result, rb_intern(
"-@"), 0);
210parse_integer(
const pm_integer_node_t *node)
212 return parse_integer_value(&node->
value);
219parse_float(
const pm_float_node_t *node)
231parse_rational(
const pm_rational_node_t *node)
245parse_imaginary(
const pm_imaginary_node_t *node)
247 VALUE imaginary_part;
248 switch (PM_NODE_TYPE(node->
numeric)) {
249 case PM_FLOAT_NODE: {
250 imaginary_part = parse_float((
const pm_float_node_t *) node->
numeric);
253 case PM_INTEGER_NODE: {
254 imaginary_part = parse_integer((
const pm_integer_node_t *) node->
numeric);
257 case PM_RATIONAL_NODE: {
258 imaginary_part = parse_rational((
const pm_rational_node_t *) node->
numeric);
262 rb_bug(
"Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->
numeric)));
265 return rb_complex_raw(
INT2FIX(0), imaginary_part);
269parse_string(
const pm_scope_node_t *scope_node,
const pm_string_t *
string)
271 return rb_enc_str_new((
const char *) pm_string_source(
string), pm_string_length(
string), scope_node->encoding);
282 rb_encoding *encoding;
284 if (node->
flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
287 else if (node->
flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
294 return rb_enc_str_new((
const char *) pm_string_source(
string), pm_string_length(
string), encoding);
298parse_static_literal_string(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_string_t *
string)
300 rb_encoding *encoding;
302 if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
305 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
309 encoding = scope_node->encoding;
312 VALUE value = rb_enc_literal_str((
const char *) pm_string_source(
string), pm_string_length(
string), encoding);
315 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
316 int line_number = pm_node_line_number(scope_node->parser, node);
317 value = rb_str_with_debug_created_info(value, rb_iseq_path(iseq), line_number);
324parse_string_symbol(
const pm_scope_node_t *scope_node,
const pm_symbol_node_t *symbol)
326 rb_encoding *encoding;
327 if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING) {
330 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING) {
333 else if (symbol->
base.
flags & PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING) {
337 encoding = scope_node->encoding;
340 return rb_intern3((
const char *) pm_string_source(&symbol->
unescaped), pm_string_length(&symbol->
unescaped), encoding);
344pm_optimizable_range_item_p(
const pm_node_t *node)
346 return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
351parse_regexp_error(rb_iseq_t *iseq, int32_t line_number,
const char *fmt, ...)
355 VALUE error = rb_syntax_error_append(
Qnil, rb_iseq_path(iseq), line_number, -1, NULL,
"%" PRIsVALUE, args);
361parse_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)
365 rb_encoding *encoding;
367 if (explicit_regexp_encoding != NULL) {
368 encoding = explicit_regexp_encoding;
370 else if (node->
flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
373 else if (node->
flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
377 encoding = implicit_regexp_encoding;
380 VALUE string = rb_enc_str_new((
const char *) pm_string_source(unescaped), pm_string_length(unescaped), encoding);
381 VALUE error = rb_reg_check_preprocess(
string);
383 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, node),
"%" PRIsVALUE, rb_obj_as_string(error));
388pm_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)
392 for (
size_t index = 0; index < nodes->
size; index++) {
393 const pm_node_t *part = nodes->
nodes[index];
396 switch (PM_NODE_TYPE(part)) {
398 if (implicit_regexp_encoding != NULL) {
400 string = parse_regexp_string_part(iseq, scope_node, part, &((
const pm_string_node_t *) part)->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
403 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
404 VALUE error = rb_reg_check_preprocess(
string);
405 if (error !=
Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, part),
"%" PRIsVALUE, rb_obj_as_string(error));
409 string = parse_string_encoded(part, &((
const pm_string_node_t *) part)->unescaped, scope_node->encoding);
412 case PM_INTERPOLATED_STRING_NODE:
413 string = pm_static_literal_concat(iseq, &((
const pm_interpolated_string_node_t *) part)->parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
415 case PM_EMBEDDED_STATEMENTS_NODE: {
416 const pm_embedded_statements_node_t *cast = (
const pm_embedded_statements_node_t *) part;
417 string = pm_static_literal_concat(iseq, &cast->
statements->
body, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
421 RUBY_ASSERT(
false &&
"unexpected node type in pm_static_literal_concat");
425 if (current !=
Qnil) {
433 return top ? rb_fstring(current) : current;
436#define RE_OPTION_ENCODING_SHIFT 8
437#define RE_OPTION_ENCODING(encoding) (((encoding) & 0xFF) << RE_OPTION_ENCODING_SHIFT)
438#define ARG_ENCODING_NONE 32
439#define ARG_ENCODING_FIXED 16
440#define ENC_ASCII8BIT 1
442#define ENC_Windows_31J 3
450parse_regexp_flags(
const pm_node_t *node)
457 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
458 flags |= ARG_ENCODING_NONE;
461 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
462 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_EUC_JP));
465 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
466 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_Windows_31J));
469 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
470 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_UTF8));
473 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) {
474 flags |= ONIG_OPTION_IGNORECASE;
477 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) {
478 flags |= ONIG_OPTION_MULTILINE;
481 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) {
482 flags |= ONIG_OPTION_EXTEND;
488#undef RE_OPTION_ENCODING_SHIFT
489#undef RE_OPTION_ENCODING
490#undef ARG_ENCODING_FIXED
491#undef ARG_ENCODING_NONE
494#undef ENC_Windows_31J
498parse_regexp_encoding(
const pm_scope_node_t *scope_node,
const pm_node_t *node)
500 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_FORCED_BINARY_ENCODING) || PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
503 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
506 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
507 return rb_enc_get_from_index(ENCINDEX_EUC_JP);
509 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
510 return rb_enc_get_from_index(ENCINDEX_Windows_31J);
518parse_regexp(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_node_t *node,
VALUE string)
522 int32_t line_number = pm_node_line_number(scope_node->parser, node);
523 VALUE regexp = rb_reg_compile(
string, parse_regexp_flags(node), (
const char *) pm_string_source(&scope_node->parser->
filepath), line_number);
527 rb_set_errinfo(errinfo);
529 parse_regexp_error(iseq, line_number,
"%" PRIsVALUE, message);
538parse_regexp_literal(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_string_t *unescaped)
540 rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
541 if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
543 VALUE string = rb_enc_str_new((
const char *) pm_string_source(unescaped), pm_string_length(unescaped), regexp_encoding);
544 return parse_regexp(iseq, scope_node, node,
string);
548parse_regexp_concat(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
const pm_node_t *node,
const pm_node_list_t *parts)
550 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
551 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
553 VALUE string = pm_static_literal_concat(iseq, parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding,
false);
554 return parse_regexp(iseq, scope_node, node,
string);
557static 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);
560pm_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)
563 size_t parts_size = parts->
size;
564 bool interpolated =
false;
566 if (parts_size > 0) {
570 for (
size_t index = 0; index < parts_size; index++) {
571 const pm_node_t *part = parts->
nodes[index];
573 if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
574 const pm_string_node_t *string_node = (
const pm_string_node_t *) part;
577 if (implicit_regexp_encoding == NULL) {
578 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
581 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
584 if (
RTEST(current_string)) {
585 current_string =
rb_str_concat(current_string, string_value);
588 current_string = string_value;
589 if (index != 0) current_location = PM_NODE_END_LOCATION(scope_node->parser, part);
596 PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE) &&
597 ((
const pm_embedded_statements_node_t *) part)->statements != NULL &&
598 ((
const pm_embedded_statements_node_t *) part)->statements->body.size == 1 &&
599 PM_NODE_TYPE_P(((
const pm_embedded_statements_node_t *) part)->statements->body.nodes[0], PM_STRING_NODE)
601 const pm_string_node_t *string_node = (
const pm_string_node_t *) ((
const pm_embedded_statements_node_t *) part)->statements->body.nodes[0];
604 if (implicit_regexp_encoding == NULL) {
605 string_value = parse_string_encoded(part, &string_node->
unescaped, scope_node->encoding);
608 string_value = parse_regexp_string_part(iseq, scope_node, (
const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
611 if (
RTEST(current_string)) {
612 current_string =
rb_str_concat(current_string, string_value);
615 current_string = string_value;
616 current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
620 if (!
RTEST(current_string)) {
621 rb_encoding *encoding;
623 if (implicit_regexp_encoding != NULL) {
624 if (explicit_regexp_encoding != NULL) {
625 encoding = explicit_regexp_encoding;
631 encoding = implicit_regexp_encoding;
635 encoding = scope_node->encoding;
638 if (parts_size == 1) {
639 current_string = rb_enc_str_new(NULL, 0, encoding);
643 if (
RTEST(current_string)) {
644 VALUE operand = rb_fstring(current_string);
645 PUSH_INSN1(ret, current_location, putobject, operand);
649 PM_COMPILE_NOT_POPPED(part);
651 const pm_node_location_t current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
652 PUSH_INSN(ret, current_location, dup);
655 const struct rb_callinfo *callinfo = new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE);
656 PUSH_INSN1(ret, current_location, objtostring, callinfo);
659 PUSH_INSN(ret, current_location, anytostring);
661 current_string =
Qnil;
667 if (
RTEST(current_string)) {
668 current_string = rb_fstring(current_string);
670 if (stack_size == 0 && interpolated) {
671 PUSH_INSN1(ret, current_location, putstring, current_string);
674 PUSH_INSN1(ret, current_location, putobject, current_string);
677 current_string =
Qnil;
682 PUSH_INSN(ret, *node_location, putnil);
689pm_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)
691 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
692 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
694 int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding);
695 PUSH_INSN2(ret, *node_location, toregexp,
INT2FIX(parse_regexp_flags(node) & 0xFF),
INT2FIX(length));
699pm_source_file_value(
const pm_source_file_node_t *node,
const pm_scope_node_t *scope_node)
702 size_t length = pm_string_length(filepath);
706 return rb_enc_interned_str((
const char *) pm_string_source(filepath), length, filepath_encoding);
709 return rb_fstring_lit(
"<compiled>");
718pm_static_literal_string(rb_iseq_t *iseq,
VALUE string,
int line_number)
720 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal ||
RTEST(
ruby_debug)) {
721 return rb_str_with_debug_created_info(
string, rb_iseq_path(iseq), line_number);
724 return rb_fstring(
string);
734pm_static_literal_value(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_scope_node_t *scope_node)
738 RUBY_ASSERT(PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL));
740 switch (PM_NODE_TYPE(node)) {
741 case PM_ARRAY_NODE: {
742 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
743 const pm_node_list_t *elements = &cast->
elements;
745 VALUE value = rb_ary_hidden_new(elements->
size);
746 for (
size_t index = 0; index < elements->
size; index++) {
747 rb_ary_push(value, pm_static_literal_value(iseq, elements->
nodes[index], scope_node));
756 return parse_float((
const pm_float_node_t *) node);
758 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
759 const pm_node_list_t *elements = &cast->
elements;
761 VALUE array = rb_ary_hidden_new(elements->
size * 2);
762 for (
size_t index = 0; index < elements->
size; index++) {
764 const pm_assoc_node_t *cast = (
const pm_assoc_node_t *) elements->
nodes[index];
765 VALUE pair[2] = { pm_static_literal_value(iseq, cast->key, scope_node), pm_static_literal_value(iseq, cast->value, scope_node) };
766 rb_ary_cat(array, pair, 2);
769 VALUE value = rb_hash_new_with_size(elements->
size);
776 case PM_IMAGINARY_NODE:
777 return parse_imaginary((
const pm_imaginary_node_t *) node);
778 case PM_INTEGER_NODE:
779 return parse_integer((
const pm_integer_node_t *) node);
780 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
781 const pm_interpolated_match_last_line_node_t *cast = (
const pm_interpolated_match_last_line_node_t *) node;
782 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &cast->parts);
784 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
785 const pm_interpolated_regular_expression_node_t *cast = (
const pm_interpolated_regular_expression_node_t *) node;
786 return parse_regexp_concat(iseq, scope_node, (
const pm_node_t *) cast, &cast->parts);
788 case PM_INTERPOLATED_STRING_NODE: {
789 VALUE string = pm_static_literal_concat(iseq, &((
const pm_interpolated_string_node_t *) node)->parts, scope_node, NULL, NULL,
false);
790 int line_number = pm_node_line_number(scope_node->parser, node);
791 return pm_static_literal_string(iseq,
string, line_number);
793 case PM_INTERPOLATED_SYMBOL_NODE: {
794 const pm_interpolated_symbol_node_t *cast = (
const pm_interpolated_symbol_node_t *) node;
795 VALUE string = pm_static_literal_concat(iseq, &cast->
parts, scope_node, NULL, NULL,
true);
797 return ID2SYM(rb_intern_str(
string));
799 case PM_MATCH_LAST_LINE_NODE: {
800 const pm_match_last_line_node_t *cast = (
const pm_match_last_line_node_t *) node;
801 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &cast->unescaped);
805 case PM_RATIONAL_NODE:
806 return parse_rational((
const pm_rational_node_t *) node);
807 case PM_REGULAR_EXPRESSION_NODE: {
808 const pm_regular_expression_node_t *cast = (
const pm_regular_expression_node_t *) node;
809 return parse_regexp_literal(iseq, scope_node, (
const pm_node_t *) cast, &cast->unescaped);
811 case PM_SOURCE_ENCODING_NODE:
812 return rb_enc_from_encoding(scope_node->encoding);
813 case PM_SOURCE_FILE_NODE: {
814 const pm_source_file_node_t *cast = (
const pm_source_file_node_t *) node;
815 return pm_source_file_value(cast, scope_node);
817 case PM_SOURCE_LINE_NODE:
818 return INT2FIX(pm_node_line_number(scope_node->parser, node));
819 case PM_STRING_NODE: {
820 const pm_string_node_t *cast = (
const pm_string_node_t *) node;
821 return parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
824 return ID2SYM(parse_string_symbol(scope_node, (
const pm_symbol_node_t *) node));
828 rb_bug(
"Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
836static rb_code_location_t
837pm_code_location(
const pm_scope_node_t *scope_node,
const pm_node_t *node)
839 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
840 const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, node);
842 return (rb_code_location_t) {
843 .beg_pos = { .lineno = start_location.
line, .column = start_location.
column },
844 .end_pos = { .lineno = end_location.
line, .column = end_location.
column }
853#define PM_BRANCH_COVERAGE_P(iseq) (ISEQ_COVERAGE(iseq) && ISEQ_BRANCH_COVERAGE(iseq))
856pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const pm_node_t *cond,
857 LABEL *then_label, LABEL *else_label,
bool popped, pm_scope_node_t *scope_node);
860pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, pm_node_t *cond, LABEL *then_label, LABEL *else_label,
bool popped, pm_scope_node_t *scope_node)
866 LABEL *label = NEW_LABEL(location.
line);
867 if (!then_label) then_label = label;
868 else if (!else_label) else_label = label;
870 pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, popped, scope_node);
872 if (LIST_INSN_SIZE_ONE(seq)) {
873 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
874 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
return;
877 if (!label->refcnt) {
878 if (popped) PUSH_INSN(ret, location, putnil);
881 PUSH_LABEL(seq, label);
889pm_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)
893 if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
894 PM_COMPILE_NOT_POPPED(node);
897 PUSH_INSN1(ret, location, getglobal, operand);
899 PUSH_SEND(ret, location, idEq,
INT2FIX(1));
900 if (popped) PUSH_INSN(ret, location, pop);
908pm_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)
911 LABEL *lend = NEW_LABEL(location.
line);
913 int again = !(flip_flop_node->
base.
flags & PM_RANGE_FLAGS_EXCLUDE_END);
915 rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
918 PUSH_INSN2(ret, location, getspecial, key,
INT2FIX(0));
919 PUSH_INSNL(ret, location, branchif, lend);
921 if (flip_flop_node->
left) {
922 pm_compile_flip_flop_bound(iseq, flip_flop_node->
left, ret, popped, scope_node);
925 PUSH_INSN(ret, location, putnil);
928 PUSH_INSNL(ret, location, branchunless, else_label);
929 PUSH_INSN1(ret, location, putobject,
Qtrue);
930 PUSH_INSN1(ret, location, setspecial, key);
932 PUSH_INSNL(ret, location, jump, then_label);
935 PUSH_LABEL(ret, lend);
936 if (flip_flop_node->
right) {
937 pm_compile_flip_flop_bound(iseq, flip_flop_node->
right, ret, popped, scope_node);
940 PUSH_INSN(ret, location, putnil);
943 PUSH_INSNL(ret, location, branchunless, then_label);
944 PUSH_INSN1(ret, location, putobject,
Qfalse);
945 PUSH_INSN1(ret, location, setspecial, key);
946 PUSH_INSNL(ret, location, jump, then_label);
949static 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);
952pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *
const ret,
const pm_node_t *cond, LABEL *then_label, LABEL *else_label,
bool popped, pm_scope_node_t *scope_node)
957 switch (PM_NODE_TYPE(cond)) {
959 const pm_and_node_t *cast = (
const pm_and_node_t *) cond;
960 pm_compile_logical(iseq, ret, cast->
left, NULL, else_label, popped, scope_node);
966 const pm_or_node_t *cast = (
const pm_or_node_t *) cond;
967 pm_compile_logical(iseq, ret, cast->
left, then_label, NULL, popped, scope_node);
974 PUSH_INSNL(ret, location, jump, else_label);
977 case PM_IMAGINARY_NODE:
978 case PM_INTEGER_NODE:
980 case PM_RATIONAL_NODE:
981 case PM_REGULAR_EXPRESSION_NODE:
985 PUSH_INSNL(ret, location, jump, then_label);
987 case PM_FLIP_FLOP_NODE:
988 pm_compile_flip_flop((
const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.
line, ret, popped, scope_node);
990 case PM_DEFINED_NODE: {
991 const pm_defined_node_t *cast = (
const pm_defined_node_t *) cond;
992 pm_compile_defined_expr(iseq, cast->
value, &location, ret, popped, scope_node,
true);
996 DECL_ANCHOR(cond_seq);
997 pm_compile_node(iseq, cond, cond_seq,
false, scope_node);
999 if (LIST_INSN_SIZE_ONE(cond_seq)) {
1000 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
1002 if (insn->insn_id == BIN(putobject)) {
1003 if (
RTEST(insn->operands[0])) {
1004 PUSH_INSNL(ret, location, jump, then_label);
1009 PUSH_INSNL(ret, location, jump, else_label);
1015 PUSH_SEQ(ret, cond_seq);
1020 PUSH_INSNL(ret, location, branchunless, else_label);
1021 PUSH_INSNL(ret, location, jump, then_label);
1028pm_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)
1031 LABEL *then_label = NEW_LABEL(location.
line);
1032 LABEL *else_label = NEW_LABEL(location.
line);
1033 LABEL *end_label = NULL;
1035 DECL_ANCHOR(cond_seq);
1036 pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label,
false, scope_node);
1037 PUSH_SEQ(ret, cond_seq);
1039 rb_code_location_t conditional_location = { 0 };
1042 if (then_label->refcnt && else_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1043 conditional_location = pm_code_location(scope_node, node);
1044 branches = decl_branch_base(iseq, PTR2NUM(node), &conditional_location,
type == PM_IF_NODE ?
"if" :
"unless");
1047 if (then_label->refcnt) {
1048 PUSH_LABEL(ret, then_label);
1050 DECL_ANCHOR(then_seq);
1052 if (statements != NULL) {
1053 pm_compile_node(iseq, (
const pm_node_t *) statements, then_seq, popped, scope_node);
1056 PUSH_SYNTHETIC_PUTNIL(then_seq, iseq);
1059 if (else_label->refcnt) {
1061 if (PM_BRANCH_COVERAGE_P(iseq)) {
1062 rb_code_location_t branch_location;
1064 if (statements != NULL) {
1065 branch_location = pm_code_location(scope_node, (
const pm_node_t *) statements);
1066 }
else if (
type == PM_IF_NODE) {
1067 pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(scope_node->parser, predicate);
1068 branch_location = (rb_code_location_t) {
1069 .beg_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column },
1070 .end_pos = { .lineno = predicate_end.
line, .column = predicate_end.
column }
1073 branch_location = conditional_location;
1076 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
type == PM_IF_NODE ?
"then" :
"else", branches);
1079 end_label = NEW_LABEL(location.
line);
1080 PUSH_INSNL(then_seq, location, jump, end_label);
1081 if (!popped) PUSH_INSN(then_seq, location, pop);
1084 PUSH_SEQ(ret, then_seq);
1087 if (else_label->refcnt) {
1088 PUSH_LABEL(ret, else_label);
1090 DECL_ANCHOR(else_seq);
1092 if (subsequent != NULL) {
1093 pm_compile_node(iseq, subsequent, else_seq, popped, scope_node);
1096 PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
1100 if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1101 rb_code_location_t branch_location;
1103 if (subsequent == NULL) {
1104 branch_location = conditional_location;
1105 }
else if (PM_NODE_TYPE_P(subsequent, PM_ELSE_NODE)) {
1106 const pm_else_node_t *else_node = (
const pm_else_node_t *) subsequent;
1107 branch_location = pm_code_location(scope_node, else_node->
statements != NULL ? ((
const pm_node_t *) else_node->
statements) : (
const pm_node_t *) else_node);
1109 branch_location = pm_code_location(scope_node, (
const pm_node_t *) subsequent);
1112 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1,
type == PM_IF_NODE ?
"else" :
"then", branches);
1115 PUSH_SEQ(ret, else_seq);
1119 PUSH_LABEL(ret, end_label);
1129pm_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)
1133 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
1134 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
1135 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
1137 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.
line);
1138 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.
line);
1139 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.
line);
1140 LABEL *end_label = NEW_LABEL(location.
line);
1141 LABEL *adjust_label = NEW_LABEL(location.
line);
1143 LABEL *next_catch_label = NEW_LABEL(location.
line);
1144 LABEL *tmp_label = NULL;
1150 push_ensure_entry(iseq, &enl, NULL, NULL);
1153 if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
1154 tmp_label = NEW_LABEL(location.
line);
1155 PUSH_INSNL(ret, location, jump, tmp_label);
1159 PUSH_INSNL(ret, location, jump, next_label);
1162 PUSH_LABEL(ret, adjust_label);
1163 PUSH_INSN(ret, location, putnil);
1164 PUSH_LABEL(ret, next_catch_label);
1165 PUSH_INSN(ret, location, pop);
1166 PUSH_INSNL(ret, location, jump, next_label);
1167 if (tmp_label) PUSH_LABEL(ret, tmp_label);
1169 PUSH_LABEL(ret, redo_label);
1172 if (PM_BRANCH_COVERAGE_P(iseq)) {
1173 rb_code_location_t loop_location = pm_code_location(scope_node, node);
1174 VALUE branches = decl_branch_base(iseq, PTR2NUM(node), &loop_location,
type == PM_WHILE_NODE ?
"while" :
"until");
1176 rb_code_location_t branch_location = statements != NULL ? pm_code_location(scope_node, (
const pm_node_t *) statements) : loop_location;
1177 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0,
"body", branches);
1180 if (statements != NULL) PM_COMPILE_POPPED((
const pm_node_t *) statements);
1181 PUSH_LABEL(ret, next_label);
1183 if (
type == PM_WHILE_NODE) {
1184 pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, popped, scope_node);
1186 else if (
type == PM_UNTIL_NODE) {
1187 pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, popped, scope_node);
1190 PUSH_LABEL(ret, end_label);
1191 PUSH_ADJUST_RESTORE(ret, adjust_label);
1192 PUSH_INSN(ret, location, putnil);
1194 PUSH_LABEL(ret, break_label);
1195 if (popped) PUSH_INSN(ret, location, pop);
1197 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
1198 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
1199 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL, ISEQ_COMPILE_DATA(iseq)->redo_label);
1201 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
1202 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
1203 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
1204 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->prev;
1212static pm_local_index_t
1213pm_lookup_local_index(rb_iseq_t *iseq,
const pm_scope_node_t *scope_node,
pm_constant_id_t constant_id,
int start_depth)
1215 pm_local_index_t lindex = { 0 };
1216 st_data_t local_index;
1219 for (level = 0; level < start_depth; level++) {
1220 scope_node = scope_node->previous;
1223 while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
1226 if (scope_node->previous) {
1227 scope_node = scope_node->previous;
1232 rb_bug(
"Local with constant_id %u does not exist", (
unsigned int) constant_id);
1236 lindex.level = level;
1237 lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
1248pm_constant_id_lookup(
const pm_scope_node_t *scope_node,
pm_constant_id_t constant_id)
1251 rb_bug(
"constant_id out of range: %u", (
unsigned int)constant_id);
1253 return scope_node->constants[constant_id - 1];
1257pm_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)
1259 debugs(
"[new_child_iseq]> ---------------------------------------\n");
1260 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1262 rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
1263 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1265 isolated_depth ? isolated_depth + 1 : 0,
1266 type, ISEQ_COMPILE_DATA(iseq)->option, &error_state);
1269 pm_scope_node_destroy(node);
1271 rb_jump_tag(error_state);
1273 debugs(
"[new_child_iseq]< ---------------------------------------\n");
1278pm_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)
1280 if (PM_NODE_TYPE_P(node, PM_CONSTANT_PATH_NODE)) {
1281 const pm_node_t *parent = ((
const pm_constant_path_node_t *) node)->parent;
1286 return VM_DEFINECLASS_FLAG_SCOPED;
1290 PUSH_INSN1(ret, *node_location, putobject, rb_cObject);
1291 return VM_DEFINECLASS_FLAG_SCOPED;
1296 PUSH_INSN1(ret, *node_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
1306pm_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)
1309 LABEL *lfin = NEW_LABEL(location.
line);
1310 LABEL *lcfin = NEW_LABEL(location.
line);
1311 LABEL *lskip = NULL;
1313 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1314 ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
1316 PM_COMPILE_NOT_POPPED(receiver);
1318 lskip = NEW_LABEL(location.
line);
1319 PUSH_INSN(ret, location, dup);
1320 PUSH_INSNL(ret, location, branchnil, lskip);
1323 PUSH_INSN(ret, location, dup);
1324 PUSH_SEND_WITH_FLAG(ret, location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
1325 if (!popped) PUSH_INSN(ret, location, dup);
1328 PUSH_INSNL(ret, location, branchunless, lcfin);
1331 PUSH_INSNL(ret, location, branchif, lcfin);
1334 if (!popped) PUSH_INSN(ret, location, pop);
1335 PM_COMPILE_NOT_POPPED(value);
1338 PUSH_INSN(ret, location, swap);
1339 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
1342 ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
1343 PUSH_SEND_WITH_FLAG(ret, location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
1344 PUSH_INSNL(ret, location, jump, lfin);
1346 PUSH_LABEL(ret, lcfin);
1347 if (!popped) PUSH_INSN(ret, location, swap);
1349 PUSH_LABEL(ret, lfin);
1351 if (lskip && popped) PUSH_LABEL(ret, lskip);
1352 PUSH_INSN(ret, location, pop);
1353 if (lskip && !popped) PUSH_LABEL(ret, lskip);
1356static 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);
1364pm_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)
1366 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
1372 const int max_stack_length = 0x100;
1373 const unsigned int min_tmp_hash_length = 0x800;
1375 int stack_length = 0;
1376 bool first_chunk =
true;
1382 bool static_literal =
false;
1384 DECL_ANCHOR(anchor);
1387#define FLUSH_CHUNK \
1388 if (stack_length) { \
1389 if (first_chunk) { \
1390 PUSH_SEQ(ret, anchor); \
1391 PUSH_INSN1(ret, location, newhash, INT2FIX(stack_length)); \
1392 first_chunk = false; \
1395 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE)); \
1396 PUSH_INSN(ret, location, swap); \
1397 PUSH_SEQ(ret, anchor); \
1398 PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(stack_length + 1)); \
1400 INIT_ANCHOR(anchor); \
1404 for (
size_t index = 0; index < elements->
size; index++) {
1405 const pm_node_t *element = elements->
nodes[index];
1407 switch (PM_NODE_TYPE(element)) {
1408 case PM_ASSOC_NODE: {
1411 (shareability == 0) &&
1412 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && (
1413 (!static_literal && ((index + min_tmp_hash_length) < elements->
size)) ||
1414 (first_chunk && stack_length == 0)
1419 while (index + count < elements->size && PM_NODE_FLAG_P(elements->
nodes[index + count], PM_NODE_FLAG_STATIC_LITERAL)) count++;
1421 if ((first_chunk && stack_length == 0) || count >= min_tmp_hash_length) {
1424 VALUE ary = rb_ary_hidden_new(count);
1427 for (
size_t tmp_end = index + count; index < tmp_end; index++) {
1428 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[index];
1431 pm_static_literal_value(iseq, assoc->key, scope_node),
1432 pm_static_literal_value(iseq, assoc->value, scope_node)
1435 rb_ary_cat(ary, elem, 2);
1447 PUSH_INSN1(ret, location, duphash, hash);
1448 first_chunk =
false;
1451 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1452 PUSH_INSN(ret, location, swap);
1453 PUSH_INSN1(ret, location, putobject, hash);
1454 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1460 static_literal =
true;
1464 static_literal =
false;
1469 if (shareability == 0) {
1470 pm_compile_node(iseq, element, anchor,
false, scope_node);
1473 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
1474 pm_compile_shareable_constant_value(iseq, assoc->
key, shareability, path, ret, scope_node,
false);
1475 pm_compile_shareable_constant_value(iseq, assoc->
value, shareability, path, ret, scope_node,
false);
1478 if ((stack_length += 2) >= max_stack_length) FLUSH_CHUNK;
1481 case PM_ASSOC_SPLAT_NODE: {
1484 const pm_assoc_splat_node_t *assoc_splat = (
const pm_assoc_splat_node_t *) element;
1485 bool empty_hash = assoc_splat->
value != NULL && (
1486 (PM_NODE_TYPE_P(assoc_splat->
value, PM_HASH_NODE) && ((
const pm_hash_node_t *) assoc_splat->
value)->elements.size == 0) ||
1487 PM_NODE_TYPE_P(assoc_splat->
value, PM_NIL_NODE)
1490 bool first_element = first_chunk && stack_length == 0;
1491 bool last_element = index == elements->
size - 1;
1492 bool only_element = first_element && last_element;
1495 if (only_element && argument) {
1502 PUSH_INSN(ret, location, putnil);
1504 else if (first_element) {
1507 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1514 if (only_element && argument) {
1520 if (shareability == 0) {
1521 PM_COMPILE_NOT_POPPED(element);
1524 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1532 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1534 if (first_element) {
1535 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1538 PUSH_INSN(ret, location, swap);
1541 if (shareability == 0) {
1542 PM_COMPILE_NOT_POPPED(element);
1545 pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node,
false);
1548 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1552 first_chunk =
false;
1553 static_literal =
false;
1557 RUBY_ASSERT(
"Invalid node type for hash" &&
false);
1566#define SPLATARRAY_FALSE 0
1567#define SPLATARRAY_TRUE 1
1568#define DUP_SINGLE_KW_SPLAT 2
1572pm_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)
1577 bool has_splat =
false;
1578 bool has_keyword_splat =
false;
1580 if (arguments_node == NULL) {
1581 if (*flags & VM_CALL_FCALL) {
1582 *flags |= VM_CALL_VCALL;
1586 const pm_node_list_t *arguments = &arguments_node->
arguments;
1587 has_keyword_splat = PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
1591 int post_splat_counter = 0;
1592 const pm_node_t *argument;
1595 switch (PM_NODE_TYPE(argument)) {
1597 case PM_KEYWORD_HASH_NODE: {
1598 const pm_keyword_hash_node_t *keyword_arg = (
const pm_keyword_hash_node_t *) argument;
1599 const pm_node_list_t *elements = &keyword_arg->
elements;
1601 if (has_keyword_splat || has_splat) {
1602 *flags |= VM_CALL_KW_SPLAT;
1603 has_keyword_splat =
true;
1605 if (elements->
size > 1 || !(elements->
size == 1 && PM_NODE_TYPE_P(elements->
nodes[0], PM_ASSOC_SPLAT_NODE))) {
1609 *flags |= VM_CALL_KW_SPLAT_MUT;
1610 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1612 else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
1613 *flags |= VM_CALL_KW_SPLAT_MUT;
1614 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1615 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
1616 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1617 PUSH_SEND(ret, location, id_core_hash_merge_kwd,
INT2FIX(2));
1620 pm_compile_hash_elements(iseq, argument, elements, 0,
Qundef,
true, ret, scope_node);
1626 if (PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS)) {
1632 VALUE keyword_indices = rb_ary_new_capa(elements->
size);
1635 for (
size_t element_index = 0; element_index < elements->
size; element_index++) {
1636 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[element_index];
1640 VALUE keyword = pm_static_literal_value(iseq, assoc->key, scope_node);
1641 VALUE stored_index = rb_hash_aref(stored_indices, keyword);
1646 if (!
NIL_P(stored_index)) {
1654 rb_hash_aset(stored_indices, keyword,
ULONG2NUM(element_index));
1655 rb_ary_store(keyword_indices, (
long) element_index,
Qtrue);
1660 *flags |= VM_CALL_KWARG;
1662 VALUE *keywords = (*kw_arg)->keywords;
1663 (*kw_arg)->references = 0;
1664 (*kw_arg)->keyword_len = (int) size;
1666 size_t keyword_index = 0;
1667 for (
size_t element_index = 0; element_index < elements->
size; element_index++) {
1668 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[element_index];
1671 if (rb_ary_entry(keyword_indices, (
long) element_index) ==
Qtrue) {
1672 keywords[keyword_index++] = pm_static_literal_value(iseq, assoc->key, scope_node);
1676 PM_COMPILE(assoc->value);
1685 *flags |= VM_CALL_KW_SPLAT;
1687 size_t size = elements->
size;
1692 *flags |= VM_CALL_KW_SPLAT_MUT;
1695 for (
size_t element_index = 0; element_index < size; element_index++) {
1696 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) elements->
nodes[element_index];
1697 PM_COMPILE_NOT_POPPED(assoc->key);
1698 PM_COMPILE_NOT_POPPED(assoc->value);
1701 PUSH_INSN1(ret, location, newhash,
INT2FIX(size * 2));
1706 case PM_SPLAT_NODE: {
1707 *flags |= VM_CALL_ARGS_SPLAT;
1708 const pm_splat_node_t *splat_node = (
const pm_splat_node_t *) argument;
1711 PM_COMPILE_NOT_POPPED(splat_node->
expression);
1714 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1715 PUSH_GETLOCAL(ret, location, index.index, index.level);
1718 bool first_splat = !has_splat;
1726 if (index + 1 < arguments->
size || has_regular_blockarg) {
1727 PUSH_INSN1(ret, location, splatarray, (*dup_rest & SPLATARRAY_TRUE) ?
Qtrue :
Qfalse);
1728 if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
1736 PUSH_INSN1(ret, location, splatarray,
Qfalse);
1746 PUSH_INSN(ret, location, concattoarray);
1750 post_splat_counter = 0;
1754 case PM_FORWARDING_ARGUMENTS_NODE: {
1755 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
1757 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
1758 *flags |= VM_CALL_FORWARDING;
1760 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
1761 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1768 *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_SPLAT_MUT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
1775 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1776 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1777 PUSH_INSN1(ret, location, splatarray,
Qtrue);
1780 pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
1781 PUSH_GETLOCAL(ret, location, pow_local.index, pow_local.level);
1784 pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
1785 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(and_local.level));
1786 PUSH_INSN(ret, location, splatkw);
1791 post_splat_counter++;
1792 PM_COMPILE_NOT_POPPED(argument);
1812 if (index == arguments->
size - 1) {
1814 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(post_splat_counter));
1817 pm_node_t *next_arg = arguments->
nodes[index + 1];
1819 switch (PM_NODE_TYPE(next_arg)) {
1821 case PM_KEYWORD_HASH_NODE: {
1822 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1823 PUSH_INSN(ret, location, concatarray);
1826 case PM_SPLAT_NODE: {
1827 PUSH_INSN1(ret, location, newarray,
INT2FIX(post_splat_counter));
1828 PUSH_INSN(ret, location, concatarray);
1844 if (has_splat) orig_argc++;
1845 if (has_keyword_splat) orig_argc++;
1854pm_setup_args_dup_rest_p(
const pm_node_t *node)
1856 switch (PM_NODE_TYPE(node)) {
1857 case PM_BACK_REFERENCE_READ_NODE:
1858 case PM_CLASS_VARIABLE_READ_NODE:
1859 case PM_CONSTANT_READ_NODE:
1862 case PM_GLOBAL_VARIABLE_READ_NODE:
1863 case PM_IMAGINARY_NODE:
1864 case PM_INSTANCE_VARIABLE_READ_NODE:
1865 case PM_INTEGER_NODE:
1866 case PM_LAMBDA_NODE:
1867 case PM_LOCAL_VARIABLE_READ_NODE:
1869 case PM_NUMBERED_REFERENCE_READ_NODE:
1870 case PM_RATIONAL_NODE:
1871 case PM_REGULAR_EXPRESSION_NODE:
1873 case PM_STRING_NODE:
1874 case PM_SYMBOL_NODE:
1877 case PM_CONSTANT_PATH_NODE: {
1878 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) node;
1879 if (cast->
parent != NULL) {
1880 return pm_setup_args_dup_rest_p(cast->
parent);
1884 case PM_IMPLICIT_NODE:
1885 return pm_setup_args_dup_rest_p(((
const pm_implicit_node_t *) node)->value);
1895pm_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)
1897 int dup_rest = SPLATARRAY_TRUE;
1899 const pm_node_list_t *arguments;
1900 size_t arguments_size;
1905 arguments_node != NULL &&
1906 (arguments = &arguments_node->
arguments, arguments_size = arguments->
size) >= 2 &&
1907 PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_SPLAT) &&
1908 !PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_MULTIPLE_SPLATS) &&
1909 PM_NODE_TYPE_P(arguments->
nodes[arguments_size - 1], PM_KEYWORD_HASH_NODE)
1914 dup_rest = SPLATARRAY_FALSE;
1916 const pm_keyword_hash_node_t *keyword_hash = (
const pm_keyword_hash_node_t *) arguments->
nodes[arguments_size - 1];
1917 const pm_node_list_t *elements = &keyword_hash->elements;
1919 for (
size_t index = 0; dup_rest == SPLATARRAY_FALSE && index < elements->
size; index++) {
1920 const pm_node_t *element = elements->
nodes[index];
1922 switch (PM_NODE_TYPE(element)) {
1923 case PM_ASSOC_NODE: {
1924 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
1925 if (pm_setup_args_dup_rest_p(assoc->
key) || pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
1928 case PM_ASSOC_SPLAT_NODE: {
1929 const pm_assoc_splat_node_t *assoc = (
const pm_assoc_splat_node_t *) element;
1930 if (assoc->
value != NULL && pm_setup_args_dup_rest_p(assoc->
value)) dup_rest = SPLATARRAY_TRUE;
1939 int initial_dup_rest = dup_rest;
1942 if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
1946 bool regular_block_arg =
true;
1947 const pm_node_t *block_expr = ((
const pm_block_argument_node_t *)block)->expression;
1949 if (block_expr && pm_setup_args_dup_rest_p(block_expr)) {
1950 dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
1951 initial_dup_rest = dup_rest;
1954 DECL_ANCHOR(block_arg);
1955 pm_compile_node(iseq, block, block_arg,
false, scope_node);
1957 *flags |= VM_CALL_ARGS_BLOCKARG;
1959 if (LIST_INSN_SIZE_ONE(block_arg)) {
1960 LINK_ELEMENT *elem = FIRST_ELEMENT(block_arg);
1961 if (IS_INSN(elem)) {
1962 INSN *iobj = (INSN *) elem;
1963 if (iobj->insn_id == BIN(getblockparam)) {
1964 iobj->insn_id = BIN(getblockparamproxy);
1971 regular_block_arg =
false;
1975 argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
1976 PUSH_SEQ(ret, block_arg);
1979 argc = pm_setup_args_core(arguments_node, block, flags,
false, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
1986 if (*flags & VM_CALL_ARGS_SPLAT && dup_rest != initial_dup_rest) {
1987 *flags |= VM_CALL_ARGS_SPLAT_MUT;
2004pm_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)
2007 if (!popped) PUSH_INSN(ret, location, putnil);
2009 PM_COMPILE_NOT_POPPED(node->
receiver);
2011 int boff = (node->
block == NULL ? 0 : 1);
2012 int flag = PM_NODE_TYPE_P(node->
receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2014 int argc = pm_setup_args(node->
arguments, (
const pm_node_t *) node->
block, &flag, &keywords, iseq, ret, scope_node, node_location);
2016 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2018 PUSH_INSN(ret, location, splatkw);
2021 PUSH_INSN(ret, location, dup);
2022 PUSH_INSN(ret, location, splatkw);
2023 PUSH_INSN(ret, location, pop);
2027 int dup_argn = argc + 1 + boff;
2028 int keyword_len = 0;
2031 keyword_len = keywords->keyword_len;
2032 dup_argn += keyword_len;
2035 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2036 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2037 PM_COMPILE_NOT_POPPED(node->
value);
2040 PUSH_SEND(ret, location, id_operator,
INT2FIX(1));
2043 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2045 if (flag & VM_CALL_ARGS_SPLAT) {
2046 if (flag & VM_CALL_KW_SPLAT) {
2047 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2049 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2050 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2051 flag |= VM_CALL_ARGS_SPLAT_MUT;
2054 PUSH_INSN(ret, location, swap);
2055 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2056 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2057 PUSH_INSN(ret, location, pop);
2061 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2062 PUSH_INSN(ret, location, swap);
2063 PUSH_INSN(ret, location, pop);
2065 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2066 PUSH_INSN(ret, location, swap);
2067 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2068 PUSH_INSN(ret, location, swap);
2069 flag |= VM_CALL_ARGS_SPLAT_MUT;
2071 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2073 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2074 PUSH_INSN(ret, location, pop);
2075 PUSH_INSN(ret, location, pop);
2079 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2081 else if (flag & VM_CALL_KW_SPLAT) {
2083 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2084 PUSH_INSN(ret, location, swap);
2085 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2086 PUSH_INSN(ret, location, pop);
2088 PUSH_INSN(ret, location, swap);
2089 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2091 else if (keyword_len) {
2092 PUSH_INSN(ret, location, dup);
2093 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 2));
2094 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2095 PUSH_INSN(ret, location, pop);
2096 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2100 PUSH_INSN(ret, location, swap);
2102 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2105 PUSH_INSN(ret, location, pop);
2121pm_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)
2124 if (!popped) PUSH_INSN(ret, location, putnil);
2125 PM_COMPILE_NOT_POPPED(receiver);
2127 int boff = (block == NULL ? 0 : 1);
2128 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
2130 int argc = pm_setup_args(arguments, (
const pm_node_t *) block, &flag, &keywords, iseq, ret, scope_node, node_location);
2132 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
2134 PUSH_INSN(ret, location, splatkw);
2137 PUSH_INSN(ret, location, dup);
2138 PUSH_INSN(ret, location, splatkw);
2139 PUSH_INSN(ret, location, pop);
2143 int dup_argn = argc + 1 + boff;
2144 int keyword_len = 0;
2147 keyword_len = keywords->keyword_len;
2148 dup_argn += keyword_len;
2151 PUSH_INSN1(ret, location, dupn,
INT2FIX(dup_argn));
2152 PUSH_SEND_R(ret, location, idAREF,
INT2FIX(argc), NULL,
INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
2154 LABEL *label = NEW_LABEL(location.
line);
2155 LABEL *lfin = NEW_LABEL(location.
line);
2157 PUSH_INSN(ret, location, dup);
2158 if (PM_NODE_TYPE_P(node, PM_INDEX_AND_WRITE_NODE)) {
2159 PUSH_INSNL(ret, location, branchunless, label);
2162 PUSH_INSNL(ret, location, branchif, label);
2165 PUSH_INSN(ret, location, pop);
2166 PM_COMPILE_NOT_POPPED(value);
2169 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2172 if (flag & VM_CALL_ARGS_SPLAT) {
2173 if (flag & VM_CALL_KW_SPLAT) {
2174 PUSH_INSN1(ret, location, topn,
INT2FIX(2 + boff));
2175 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2176 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2177 flag |= VM_CALL_ARGS_SPLAT_MUT;
2180 PUSH_INSN(ret, location, swap);
2181 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2182 PUSH_INSN1(ret, location, setn,
INT2FIX(2 + boff));
2183 PUSH_INSN(ret, location, pop);
2187 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2188 PUSH_INSN(ret, location, swap);
2189 PUSH_INSN(ret, location, pop);
2191 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
2192 PUSH_INSN(ret, location, swap);
2193 PUSH_INSN1(ret, location, splatarray,
Qtrue);
2194 PUSH_INSN(ret, location, swap);
2195 flag |= VM_CALL_ARGS_SPLAT_MUT;
2197 PUSH_INSN1(ret, location, pushtoarray,
INT2FIX(1));
2199 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2200 PUSH_INSN(ret, location, pop);
2201 PUSH_INSN(ret, location, pop);
2205 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc), NULL,
INT2FIX(flag), keywords);
2207 else if (flag & VM_CALL_KW_SPLAT) {
2209 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2210 PUSH_INSN(ret, location, swap);
2211 PUSH_INSN1(ret, location, setn,
INT2FIX(3));
2212 PUSH_INSN(ret, location, pop);
2215 PUSH_INSN(ret, location, swap);
2216 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2218 else if (keyword_len) {
2219 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 1));
2220 PUSH_INSN1(ret, location, opt_reverse,
INT2FIX(keyword_len + boff + 0));
2221 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2225 PUSH_INSN(ret, location, swap);
2227 PUSH_SEND_R(ret, location, idASET,
INT2FIX(argc + 1), NULL,
INT2FIX(flag), keywords);
2230 PUSH_INSN(ret, location, pop);
2231 PUSH_INSNL(ret, location, jump, lfin);
2232 PUSH_LABEL(ret, label);
2234 PUSH_INSN1(ret, location, setn,
INT2FIX(dup_argn + 1));
2236 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(dup_argn + 1));
2237 PUSH_LABEL(ret, lfin);
2246#define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
2247#define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
2248#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
2249#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
2250#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
2255static int pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node, LINK_ANCHOR *
const ret, LABEL *matched_label, LABEL *unmatched_label,
bool in_single_pattern,
bool in_alternation_pattern,
bool use_deconstructed_cache,
unsigned int base_index);
2262pm_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)
2264 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2265 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2267 PUSH_INSN(ret, location, dup);
2268 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2270 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2271 PUSH_INSN1(ret, location, putobject, message);
2272 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2273 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2274 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2276 PUSH_INSN1(ret, location, putobject,
Qfalse);
2277 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2279 PUSH_INSN(ret, location, pop);
2280 PUSH_INSN(ret, location, pop);
2281 PUSH_LABEL(ret, match_succeeded_label);
2292pm_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)
2294 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2295 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2297 PUSH_INSN(ret, location, dup);
2298 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2300 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2301 PUSH_INSN1(ret, location, putobject, message);
2302 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2303 PUSH_INSN(ret, location, dup);
2304 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2305 PUSH_INSN1(ret, location, putobject, length);
2306 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(4));
2307 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2309 PUSH_INSN1(ret, location, putobject,
Qfalse);
2310 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2312 PUSH_INSN(ret, location, pop);
2313 PUSH_INSN(ret, location, pop);
2314 PUSH_LABEL(ret, match_succeeded_label);
2325pm_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)
2327 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2328 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2330 PUSH_INSN(ret, location, dup);
2331 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2332 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2334 VALUE operand = rb_fstring_lit(
"%p === %p does not return true");
2335 PUSH_INSN1(ret, location, putobject, operand);
2337 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2338 PUSH_INSN1(ret, location, topn,
INT2FIX(5));
2339 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2340 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2341 PUSH_INSN1(ret, location, putobject,
Qfalse);
2342 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2343 PUSH_INSN(ret, location, pop);
2344 PUSH_INSN(ret, location, pop);
2346 PUSH_LABEL(ret, match_succeeded_label);
2347 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2348 PUSH_INSN(ret, location, pop);
2349 PUSH_INSN(ret, location, pop);
2361pm_compile_pattern_match(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node, LINK_ANCHOR *
const ret, LABEL *unmatched_label,
bool in_single_pattern,
bool in_alternation_pattern,
bool use_deconstructed_cache,
unsigned int base_index)
2363 LABEL *matched_label = NEW_LABEL(pm_node_line_number(scope_node->parser, node));
2364 CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
2365 PUSH_LABEL(ret, matched_label);
2375pm_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)
2377 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2379 if (use_deconstructed_cache) {
2380 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2381 PUSH_INSNL(ret, location, branchnil, deconstruct_label);
2383 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2384 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2386 PUSH_INSN(ret, location, pop);
2387 PUSH_INSN1(ret, location, topn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
2388 PUSH_INSNL(ret, location, jump, deconstructed_label);
2391 PUSH_INSNL(ret, location, jump, deconstruct_label);
2394 PUSH_LABEL(ret, deconstruct_label);
2395 PUSH_INSN(ret, location, dup);
2398 PUSH_INSN1(ret, location, putobject, operand);
2399 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2401 if (use_deconstructed_cache) {
2402 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
2405 if (in_single_pattern) {
2406 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct"), base_index + 1));
2409 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2410 PUSH_SEND(ret, location, rb_intern(
"deconstruct"),
INT2FIX(0));
2412 if (use_deconstructed_cache) {
2413 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2416 PUSH_INSN(ret, location, dup);
2418 PUSH_INSNL(ret, location, branchunless, type_error_label);
2419 PUSH_LABEL(ret, deconstructed_label);
2429pm_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)
2431 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2433 PUSH_INSN(ret, location, dup);
2434 PM_COMPILE_NOT_POPPED(node);
2436 if (in_single_pattern) {
2437 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
2439 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2440 if (in_single_pattern) {
2441 CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
2443 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2452pm_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)
2454 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2455 LABEL *key_error_label = NEW_LABEL(location.
line);
2456 LABEL *cleanup_label = NEW_LABEL(location.
line);
2459 kw_arg->references = 0;
2460 kw_arg->keyword_len = 2;
2461 kw_arg->keywords[0] =
ID2SYM(rb_intern(
"matchee"));
2462 kw_arg->keywords[1] =
ID2SYM(rb_intern(
"key"));
2464 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2465 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2466 PUSH_INSNL(ret, location, branchif, key_error_label);
2469 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2472 VALUE operand = rb_fstring_lit(
"%p: %s");
2473 PUSH_INSN1(ret, location, putobject, operand);
2476 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2477 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2478 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2479 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2480 PUSH_INSNL(ret, location, jump, cleanup_label);
2482 PUSH_LABEL(ret, key_error_label);
2484 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2487 VALUE operand = rb_fstring_lit(
"%p: %s");
2488 PUSH_INSN1(ret, location, putobject, operand);
2491 PUSH_INSN1(ret, location, topn,
INT2FIX(4));
2492 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2493 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(3));
2494 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2495 PUSH_INSN1(ret, location, topn,
INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2496 PUSH_SEND_R(ret, location, rb_intern(
"new"),
INT2FIX(1), NULL,
INT2FIX(VM_CALL_KWARG), kw_arg);
2497 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(1));
2498 PUSH_LABEL(ret, cleanup_label);
2500 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(7));
2501 if (!popped) PUSH_INSN(ret, location, putnil);
2502 PUSH_INSNL(ret, location, jump, done_label);
2503 PUSH_INSN1(ret, location, dupn,
INT2FIX(5));
2504 if (popped) PUSH_INSN(ret, location, putnil);
2511pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node,
const pm_node_t *node, LINK_ANCHOR *
const ret, LABEL *matched_label, LABEL *unmatched_label,
bool in_single_pattern,
bool in_alternation_pattern,
bool use_deconstructed_cache,
unsigned int base_index)
2513 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
2515 switch (PM_NODE_TYPE(node)) {
2516 case PM_ARRAY_PATTERN_NODE: {
2525 const pm_array_pattern_node_t *cast = (
const pm_array_pattern_node_t *) node;
2528 const size_t posts_size = cast->
posts.
size;
2529 const size_t minimum_size = requireds_size + posts_size;
2531 bool rest_named =
false;
2532 bool use_rest_size =
false;
2534 if (cast->
rest != NULL) {
2535 rest_named = (PM_NODE_TYPE_P(cast->
rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *) cast->
rest)->expression != NULL);
2536 use_rest_size = (rest_named || (!rest_named && posts_size > 0));
2539 LABEL *match_failed_label = NEW_LABEL(location.
line);
2540 LABEL *type_error_label = NEW_LABEL(location.
line);
2541 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2542 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2544 if (use_rest_size) {
2545 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2546 PUSH_INSN(ret, location, swap);
2551 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2554 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));
2556 PUSH_INSN(ret, location, dup);
2557 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2558 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2559 PUSH_SEND(ret, location, cast->
rest == NULL ? idEq : idGE,
INT2FIX(1));
2560 if (in_single_pattern) {
2561 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+)");
2562 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message,
INT2FIX(minimum_size), base_index + 1));
2564 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2566 for (
size_t index = 0; index < requireds_size; index++) {
2568 PUSH_INSN(ret, location, dup);
2569 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2570 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2571 CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2574 if (cast->
rest != NULL) {
2576 PUSH_INSN(ret, location, dup);
2577 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size));
2578 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2579 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2580 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2581 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2582 PUSH_INSN1(ret, location, setn,
INT2FIX(4));
2583 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2584 CHECK(pm_compile_pattern_match(iseq, scope_node, ((
const pm_splat_node_t *) cast->
rest)->expression, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2586 else if (posts_size > 0) {
2587 PUSH_INSN(ret, location, dup);
2588 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2589 PUSH_INSN1(ret, location, putobject,
INT2FIX(minimum_size));
2590 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2591 PUSH_INSN1(ret, location, setn,
INT2FIX(2));
2592 PUSH_INSN(ret, location, pop);
2596 for (
size_t index = 0; index < posts_size; index++) {
2597 const pm_node_t *post = cast->
posts.
nodes[index];
2598 PUSH_INSN(ret, location, dup);
2600 PUSH_INSN1(ret, location, putobject,
INT2FIX(requireds_size + index));
2601 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2602 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2603 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2604 CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2607 PUSH_INSN(ret, location, pop);
2608 if (use_rest_size) {
2609 PUSH_INSN(ret, location, pop);
2612 PUSH_INSNL(ret, location, jump, matched_label);
2613 PUSH_INSN(ret, location, putnil);
2614 if (use_rest_size) {
2615 PUSH_INSN(ret, location, putnil);
2618 PUSH_LABEL(ret, type_error_label);
2619 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2623 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2624 PUSH_INSN1(ret, location, putobject, operand);
2627 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2628 PUSH_INSN(ret, location, pop);
2630 PUSH_LABEL(ret, match_failed_label);
2631 PUSH_INSN(ret, location, pop);
2632 if (use_rest_size) {
2633 PUSH_INSN(ret, location, pop);
2636 PUSH_INSNL(ret, location, jump, unmatched_label);
2639 case PM_FIND_PATTERN_NODE: {
2648 const pm_find_pattern_node_t *cast = (
const pm_find_pattern_node_t *) node;
2651 LABEL *match_failed_label = NEW_LABEL(location.
line);
2652 LABEL *type_error_label = NEW_LABEL(location.
line);
2653 LABEL *deconstruct_label = NEW_LABEL(location.
line);
2654 LABEL *deconstructed_label = NEW_LABEL(location.
line);
2657 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2660 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));
2662 PUSH_INSN(ret, location, dup);
2663 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2664 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2665 PUSH_SEND(ret, location, idGE,
INT2FIX(1));
2666 if (in_single_pattern) {
2667 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));
2669 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2672 LABEL *while_begin_label = NEW_LABEL(location.
line);
2673 LABEL *next_loop_label = NEW_LABEL(location.
line);
2674 LABEL *find_succeeded_label = NEW_LABEL(location.
line);
2675 LABEL *find_failed_label = NEW_LABEL(location.
line);
2677 PUSH_INSN(ret, location, dup);
2678 PUSH_SEND(ret, location, idLength,
INT2FIX(0));
2680 PUSH_INSN(ret, location, dup);
2681 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2682 PUSH_SEND(ret, location, idMINUS,
INT2FIX(1));
2683 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2684 PUSH_LABEL(ret, while_begin_label);
2686 PUSH_INSN(ret, location, dup);
2687 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2688 PUSH_SEND(ret, location, idLE,
INT2FIX(1));
2689 PUSH_INSNL(ret, location, branchunless, find_failed_label);
2691 for (
size_t index = 0; index < size; index++) {
2692 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2693 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2696 PUSH_INSN1(ret, location, putobject,
INT2FIX(index));
2697 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2700 PUSH_SEND(ret, location, idAREF,
INT2FIX(1));
2701 CHECK(pm_compile_pattern_match(iseq, scope_node, cast->
requireds.
nodes[index], ret, next_loop_label, in_single_pattern, in_alternation_pattern,
false, base_index + 4));
2704 const pm_splat_node_t *left = cast->
left;
2707 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2708 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
2709 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2710 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2711 CHECK(pm_compile_pattern_match(iseq, scope_node, left->
expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 4));
2715 const pm_splat_node_t *right = (
const pm_splat_node_t *) cast->
right;
2717 if (right->expression != NULL) {
2718 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2719 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
2720 PUSH_INSN1(ret, location, putobject,
INT2FIX(size));
2721 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2722 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2723 PUSH_SEND(ret, location, idAREF,
INT2FIX(2));
2724 pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 4);
2727 PUSH_INSNL(ret, location, jump, find_succeeded_label);
2729 PUSH_LABEL(ret, next_loop_label);
2730 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
2731 PUSH_SEND(ret, location, idPLUS,
INT2FIX(1));
2732 PUSH_INSNL(ret, location, jump, while_begin_label);
2734 PUSH_LABEL(ret, find_failed_label);
2735 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2736 if (in_single_pattern) {
2737 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2740 VALUE operand = rb_fstring_lit(
"%p does not match to find pattern");
2741 PUSH_INSN1(ret, location, putobject, operand);
2744 PUSH_INSN1(ret, location, topn,
INT2FIX(2));
2745 PUSH_SEND(ret, location, id_core_sprintf,
INT2FIX(2));
2746 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2748 PUSH_INSN1(ret, location, putobject,
Qfalse);
2749 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2751 PUSH_INSN(ret, location, pop);
2752 PUSH_INSN(ret, location, pop);
2754 PUSH_INSNL(ret, location, jump, match_failed_label);
2755 PUSH_INSN1(ret, location, dupn,
INT2FIX(3));
2757 PUSH_LABEL(ret, find_succeeded_label);
2758 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(3));
2761 PUSH_INSN(ret, location, pop);
2762 PUSH_INSNL(ret, location, jump, matched_label);
2763 PUSH_INSN(ret, location, putnil);
2765 PUSH_LABEL(ret, type_error_label);
2766 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2770 VALUE operand = rb_fstring_lit(
"deconstruct must return Array");
2771 PUSH_INSN1(ret, location, putobject, operand);
2774 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2775 PUSH_INSN(ret, location, pop);
2777 PUSH_LABEL(ret, match_failed_label);
2778 PUSH_INSN(ret, location, pop);
2779 PUSH_INSNL(ret, location, jump, unmatched_label);
2783 case PM_HASH_PATTERN_NODE: {
2792 const pm_hash_pattern_node_t *cast = (
const pm_hash_pattern_node_t *) node;
2795 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);
2798 LABEL *match_failed_label = NEW_LABEL(location.
line);
2799 LABEL *type_error_label = NEW_LABEL(location.
line);
2802 if (has_keys && !has_rest) {
2805 for (
size_t index = 0; index < cast->
elements.
size; index++) {
2807 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2809 const pm_node_t *key = ((
const pm_assoc_node_t *) element)->key;
2812 VALUE symbol =
ID2SYM(parse_string_symbol(scope_node, (
const pm_symbol_node_t *) key));
2813 rb_ary_push(keys, symbol);
2818 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->
constant, ret, match_failed_label, in_single_pattern, base_index));
2821 PUSH_INSN(ret, location, dup);
2824 VALUE operand =
ID2SYM(rb_intern(
"deconstruct_keys"));
2825 PUSH_INSN1(ret, location, putobject, operand);
2828 PUSH_SEND(ret, location, idRespond_to,
INT2FIX(1));
2829 if (in_single_pattern) {
2830 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p does not respond to #deconstruct_keys"), base_index + 1));
2832 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2835 PUSH_INSN(ret, location, putnil);
2838 PUSH_INSN1(ret, location, duparray, keys);
2841 PUSH_SEND(ret, location, rb_intern(
"deconstruct_keys"),
INT2FIX(1));
2843 PUSH_INSN(ret, location, dup);
2845 PUSH_INSNL(ret, location, branchunless, type_error_label);
2848 PUSH_SEND(ret, location, rb_intern(
"dup"),
INT2FIX(0));
2852 DECL_ANCHOR(match_values);
2854 for (
size_t index = 0; index < cast->
elements.
size; index++) {
2856 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2858 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
2859 const pm_node_t *key = assoc->
key;
2862 VALUE symbol =
ID2SYM(parse_string_symbol(scope_node, (
const pm_symbol_node_t *) key));
2863 PUSH_INSN(ret, location, dup);
2864 PUSH_INSN1(ret, location, putobject, symbol);
2865 PUSH_SEND(ret, location, rb_intern(
"key?"),
INT2FIX(1));
2867 if (in_single_pattern) {
2868 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
2870 PUSH_INSN(ret, location, dup);
2871 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2875 PUSH_INSN1(ret, location, putobject, operand);
2878 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
2879 PUSH_INSN1(ret, location, putobject,
Qtrue);
2880 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
2881 PUSH_INSN1(ret, location, topn,
INT2FIX(3));
2882 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2883 PUSH_INSN1(ret, location, putobject, symbol);
2884 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2886 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(4));
2887 PUSH_LABEL(ret, match_succeeded_label);
2890 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2891 PUSH_INSN(match_values, location, dup);
2892 PUSH_INSN1(match_values, location, putobject, symbol);
2893 PUSH_SEND(match_values, location, has_rest ? rb_intern(
"delete") : idAREF,
INT2FIX(1));
2895 const pm_node_t *value = assoc->
value;
2896 if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
2897 value = ((
const pm_implicit_node_t *) value)->value;
2900 CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1));
2903 PUSH_SEQ(ret, match_values);
2906 PUSH_INSN(ret, location, dup);
2907 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
2908 if (in_single_pattern) {
2909 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"%p is not empty"), base_index + 1));
2911 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2915 switch (PM_NODE_TYPE(cast->
rest)) {
2916 case PM_NO_KEYWORDS_PARAMETER_NODE: {
2917 PUSH_INSN(ret, location, dup);
2918 PUSH_SEND(ret, location, idEmptyP,
INT2FIX(0));
2919 if (in_single_pattern) {
2920 pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit(
"rest of %p is not empty"), base_index + 1);
2922 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2925 case PM_ASSOC_SPLAT_NODE: {
2926 const pm_assoc_splat_node_t *splat = (
const pm_assoc_splat_node_t *) cast->
rest;
2927 PUSH_INSN(ret, location, dup);
2928 pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index + 1);
2932 rb_bug(
"unreachable");
2937 PUSH_INSN(ret, location, pop);
2938 PUSH_INSNL(ret, location, jump, matched_label);
2939 PUSH_INSN(ret, location, putnil);
2941 PUSH_LABEL(ret, type_error_label);
2942 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2946 VALUE operand = rb_fstring_lit(
"deconstruct_keys must return Hash");
2947 PUSH_INSN1(ret, location, putobject, operand);
2950 PUSH_SEND(ret, location, id_core_raise,
INT2FIX(2));
2951 PUSH_INSN(ret, location, pop);
2953 PUSH_LABEL(ret, match_failed_label);
2954 PUSH_INSN(ret, location, pop);
2955 PUSH_INSNL(ret, location, jump, unmatched_label);
2958 case PM_CAPTURE_PATTERN_NODE: {
2968 const pm_capture_pattern_node_t *cast = (
const pm_capture_pattern_node_t *) node;
2970 LABEL *match_failed_label = NEW_LABEL(location.
line);
2972 PUSH_INSN(ret, location, dup);
2973 CHECK(pm_compile_pattern_match(iseq, scope_node, cast->
value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index + 1));
2974 CHECK(pm_compile_pattern(iseq, scope_node, (
const pm_node_t *) cast->
target, ret, matched_label, match_failed_label, in_single_pattern, in_alternation_pattern,
false, base_index));
2975 PUSH_INSN(ret, location, putnil);
2977 PUSH_LABEL(ret, match_failed_label);
2978 PUSH_INSN(ret, location, pop);
2979 PUSH_INSNL(ret, location, jump, unmatched_label);
2983 case PM_LOCAL_VARIABLE_TARGET_NODE: {
2987 const pm_local_variable_target_node_t *cast = (
const pm_local_variable_target_node_t *) node;
2988 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
2994 if (in_alternation_pattern) {
2995 ID id = pm_constant_id_lookup(scope_node, cast->
name);
2996 const char *name = rb_id2name(
id);
2998 if (name && strlen(name) > 0 && name[0] !=
'_') {
2999 COMPILE_ERROR(iseq, location.
line,
"illegal variable in alternative pattern (%"PRIsVALUE
")", rb_id2str(
id));
3004 PUSH_SETLOCAL(ret, location, index.index, index.level);
3005 PUSH_INSNL(ret, location, jump, matched_label);
3008 case PM_ALTERNATION_PATTERN_NODE: {
3011 const pm_alternation_pattern_node_t *cast = (
const pm_alternation_pattern_node_t *) node;
3013 LABEL *matched_left_label = NEW_LABEL(location.
line);
3014 LABEL *unmatched_left_label = NEW_LABEL(location.
line);
3018 PUSH_INSN(ret, location, dup);
3019 CHECK(pm_compile_pattern(iseq, scope_node, cast->
left, ret, matched_left_label, unmatched_left_label, in_single_pattern,
true, use_deconstructed_cache, base_index + 1));
3024 PUSH_LABEL(ret, matched_left_label);
3025 PUSH_INSN(ret, location, pop);
3026 PUSH_INSNL(ret, location, jump, matched_label);
3027 PUSH_INSN(ret, location, putnil);
3031 PUSH_LABEL(ret, unmatched_left_label);
3032 CHECK(pm_compile_pattern(iseq, scope_node, cast->
right, ret, matched_label, unmatched_label, in_single_pattern,
true, use_deconstructed_cache, base_index));
3035 case PM_PARENTHESES_NODE:
3040 return pm_compile_pattern(iseq, scope_node, ((
const pm_parentheses_node_t *) node)->body, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index);
3041 case PM_PINNED_EXPRESSION_NODE:
3046 node = ((
const pm_pinned_expression_node_t *) node)->expression;
3049 case PM_CLASS_VARIABLE_READ_NODE:
3050 case PM_CONSTANT_PATH_NODE:
3051 case PM_CONSTANT_READ_NODE:
3054 case PM_GLOBAL_VARIABLE_READ_NODE:
3055 case PM_IMAGINARY_NODE:
3056 case PM_INSTANCE_VARIABLE_READ_NODE:
3057 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3058 case PM_INTEGER_NODE:
3059 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
3060 case PM_INTERPOLATED_STRING_NODE:
3061 case PM_INTERPOLATED_SYMBOL_NODE:
3062 case PM_INTERPOLATED_X_STRING_NODE:
3063 case PM_LAMBDA_NODE:
3064 case PM_LOCAL_VARIABLE_READ_NODE:
3066 case PM_SOURCE_ENCODING_NODE:
3067 case PM_SOURCE_FILE_NODE:
3068 case PM_SOURCE_LINE_NODE:
3070 case PM_RATIONAL_NODE:
3071 case PM_REGULAR_EXPRESSION_NODE:
3073 case PM_STRING_NODE:
3074 case PM_SYMBOL_NODE:
3076 case PM_X_STRING_NODE: {
3080 PM_COMPILE_NOT_POPPED(node);
3081 if (in_single_pattern) {
3082 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
3085 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE));
3087 if (in_single_pattern) {
3088 pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
3091 PUSH_INSNL(ret, location, branchif, matched_label);
3092 PUSH_INSNL(ret, location, jump, unmatched_label);
3095 case PM_PINNED_VARIABLE_NODE: {
3100 const pm_pinned_variable_node_t *cast = (
const pm_pinned_variable_node_t *) node;
3101 CHECK(pm_compile_pattern(iseq, scope_node, cast->
variable, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern,
true, base_index));
3105 case PM_UNLESS_NODE: {
3117 const pm_node_t *predicate;
3118 const pm_node_t *statement;
3120 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3121 const pm_if_node_t *cast = (
const pm_if_node_t *) node;
3128 const pm_unless_node_t *cast = (
const pm_unless_node_t *) node;
3135 CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
3136 PM_COMPILE_NOT_POPPED(predicate);
3138 if (in_single_pattern) {
3139 LABEL *match_succeeded_label = NEW_LABEL(location.
line);
3141 PUSH_INSN(ret, location, dup);
3142 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3143 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
3146 PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
3150 VALUE operand = rb_fstring_lit(
"guard clause does not return true");
3151 PUSH_INSN1(ret, location, putobject, operand);
3154 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
3155 PUSH_INSN1(ret, location, putobject,
Qfalse);
3156 PUSH_INSN1(ret, location, setn,
INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
3158 PUSH_INSN(ret, location, pop);
3159 PUSH_INSN(ret, location, pop);
3161 PUSH_LABEL(ret, match_succeeded_label);
3164 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
3165 PUSH_INSNL(ret, location, branchunless, unmatched_label);
3168 PUSH_INSNL(ret, location, branchif, unmatched_label);
3171 PUSH_INSNL(ret, location, jump, matched_label);
3178 rb_bug(
"Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
3185#undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
3186#undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
3187#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
3188#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
3189#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
3193pm_scope_node_init(
const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous)
3197 memset(scope, 0,
sizeof(pm_scope_node_t));
3199 scope->base.
type = PM_SCOPE_NODE;
3203 scope->previous = previous;
3204 scope->ast_node = (pm_node_t *) node;
3207 scope->parser = previous->parser;
3208 scope->encoding = previous->encoding;
3210 scope->constants = previous->constants;
3211 scope->coverage_enabled = previous->coverage_enabled;
3215 switch (PM_NODE_TYPE(node)) {
3216 case PM_BLOCK_NODE: {
3217 const pm_block_node_t *cast = (
const pm_block_node_t *) node;
3218 scope->body = cast->
body;
3219 scope->locals = cast->
locals;
3223 case PM_CLASS_NODE: {
3224 const pm_class_node_t *cast = (
const pm_class_node_t *) node;
3225 scope->body = cast->
body;
3226 scope->locals = cast->
locals;
3230 const pm_def_node_t *cast = (
const pm_def_node_t *) node;
3231 scope->parameters = (pm_node_t *) cast->
parameters;
3232 scope->body = cast->body;
3233 scope->locals = cast->locals;
3236 case PM_ENSURE_NODE: {
3237 const pm_ensure_node_t *cast = (
const pm_ensure_node_t *) node;
3238 scope->body = (pm_node_t *) node;
3248 const pm_for_node_t *cast = (
const pm_for_node_t *) node;
3249 scope->body = (pm_node_t *) cast->
statements;
3252 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
3254 scope->body = (pm_node_t *) node;
3257 case PM_LAMBDA_NODE: {
3258 const pm_lambda_node_t *cast = (
const pm_lambda_node_t *) node;
3260 scope->body = cast->
body;
3261 scope->locals = cast->
locals;
3271 case PM_MODULE_NODE: {
3272 const pm_module_node_t *cast = (
const pm_module_node_t *) node;
3273 scope->body = cast->
body;
3274 scope->locals = cast->
locals;
3277 case PM_POST_EXECUTION_NODE: {
3278 const pm_post_execution_node_t *cast = (
const pm_post_execution_node_t *) node;
3279 scope->body = (pm_node_t *) cast->
statements;
3282 case PM_PROGRAM_NODE: {
3283 const pm_program_node_t *cast = (
const pm_program_node_t *) node;
3284 scope->body = (pm_node_t *) cast->
statements;
3285 scope->locals = cast->locals;
3288 case PM_RESCUE_NODE: {
3289 const pm_rescue_node_t *cast = (
const pm_rescue_node_t *) node;
3290 scope->body = (pm_node_t *) cast->
statements;
3293 case PM_RESCUE_MODIFIER_NODE: {
3294 const pm_rescue_modifier_node_t *cast = (
const pm_rescue_modifier_node_t *) node;
3298 case PM_SINGLETON_CLASS_NODE: {
3299 const pm_singleton_class_node_t *cast = (
const pm_singleton_class_node_t *) node;
3300 scope->body = cast->
body;
3301 scope->locals = cast->
locals;
3304 case PM_STATEMENTS_NODE: {
3305 const pm_statements_node_t *cast = (
const pm_statements_node_t *) node;
3306 scope->body = (pm_node_t *) cast;
3310 rb_bug(
"unreachable");
3316pm_scope_node_destroy(pm_scope_node_t *scope_node)
3318 if (scope_node->index_lookup_table) {
3319 st_free_table(scope_node->index_lookup_table);
3335pm_compile_retry_end_label(rb_iseq_t *iseq, LINK_ANCHOR *
const ret, LABEL *retry_end_l)
3338 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
3339 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
3340 while (!IS_INSN_ID(iobj, send) && !IS_INSN_ID(iobj, invokesuper) && !IS_INSN_ID(iobj, sendforward) && !IS_INSN_ID(iobj, invokesuperforward)) {
3341 iobj = (INSN*) get_prev_insn(iobj);
3343 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
3348 if (&iobj->link == LAST_ELEMENT(ret)) {
3349 ret->last = (LINK_ELEMENT*) retry_end_l;
3354pm_iseq_builtin_function_name(
const pm_scope_node_t *scope_node,
const pm_node_t *receiver,
ID method_id)
3356 const char *name = rb_id2name(method_id);
3357 static const char prefix[] =
"__builtin_";
3358 const size_t prefix_len =
sizeof(prefix) - 1;
3360 if (receiver == NULL) {
3361 if (UNLIKELY(strncmp(prefix, name, prefix_len) == 0)) {
3363 return &name[prefix_len];
3366 else if (PM_NODE_TYPE_P(receiver, PM_CALL_NODE)) {
3367 if (PM_NODE_FLAG_P(receiver, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3368 const pm_call_node_t *cast = (
const pm_call_node_t *) receiver;
3375 else if (PM_NODE_TYPE_P(receiver, PM_CONSTANT_READ_NODE)) {
3376 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) receiver;
3388pm_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)
3390 if (arguments == NULL) {
3391 COMPILE_ERROR(iseq, node_location->
line,
"attr!: no argument");
3395 const pm_node_t *argument;
3397 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3398 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to attr!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3402 VALUE symbol = pm_static_literal_value(iseq, argument, scope_node);
3405 if (strcmp(RSTRING_PTR(
string),
"leaf") == 0) {
3406 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
3408 else if (strcmp(RSTRING_PTR(
string),
"inline_block") == 0) {
3409 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_INLINE_BLOCK;
3411 else if (strcmp(RSTRING_PTR(
string),
"use_block") == 0) {
3412 iseq_set_use_block(iseq);
3414 else if (strcmp(RSTRING_PTR(
string),
"c_trace") == 0) {
3416 ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
3419 COMPILE_ERROR(iseq, node_location->
line,
"unknown argument to attr!: %s", RSTRING_PTR(
string));
3428pm_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)
3430 if (arguments == NULL) {
3431 COMPILE_ERROR(iseq, node_location->
line,
"arg!: no argument");
3436 COMPILE_ERROR(iseq, node_location->
line,
"arg!: too many argument");
3441 if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
3442 COMPILE_ERROR(iseq, node_location->
line,
"non symbol argument to arg!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
3447 ID name = parse_string_symbol(scope_node, ((
const pm_symbol_node_t *) argument));
3448 int index = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->local_table_size - get_local_var_idx(iseq, name);
3450 debugs(
"id: %s idx: %d\n", rb_id2name(name), index);
3451 PUSH_GETLOCAL(ret, *node_location, index, get_lvar_level(iseq));
3458pm_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)
3460 const pm_node_t *ast_node = scope_node->ast_node;
3461 if (!PM_NODE_TYPE_P(ast_node, PM_DEF_NODE)) {
3462 rb_bug(
"mandatory_only?: not in method definition");
3466 const pm_def_node_t *def_node = (
const pm_def_node_t *) ast_node;
3467 const pm_parameters_node_t *parameters_node = def_node->
parameters;
3468 if (parameters_node == NULL) {
3469 rb_bug(
"mandatory_only?: in method definition with no parameters");
3473 const pm_node_t *body_node = def_node->
body;
3474 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)) {
3475 rb_bug(
"mandatory_only?: not in method definition with plain statements");
3479 const pm_if_node_t *if_node = (
const pm_if_node_t *) ((
const pm_statements_node_t *) body_node)->body.nodes[0];
3480 if (if_node->
predicate != ((
const pm_node_t *) call_node)) {
3481 rb_bug(
"mandatory_only?: can't find mandatory node");
3485 pm_parameters_node_t parameters = {
3486 .base = parameters_node->
base,
3490 const pm_def_node_t def = {
3491 .base = def_node->
base,
3492 .name = def_node->
name,
3494 .parameters = ¶meters,
3503 pm_scope_node_t next_scope_node;
3504 pm_scope_node_init(&def.base, &next_scope_node, scope_node);
3507 const rb_iseq_t *mandatory_only_iseq = pm_iseq_new_with_opt(
3509 rb_iseq_base_label(iseq),
3511 rb_iseq_realpath(iseq),
3512 node_location->
line,
3516 ISEQ_COMPILE_DATA(iseq)->option,
3519 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (
VALUE)mandatory_only_iseq);
3522 RUBY_ASSERT(ISEQ_BODY(iseq)->mandatory_only_iseq == NULL);
3523 rb_jump_tag(error_state);
3526 pm_scope_node_destroy(&next_scope_node);
3531pm_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)
3533 const pm_arguments_node_t *arguments = call_node->
arguments;
3535 if (parent_block != NULL) {
3536 COMPILE_ERROR(iseq, node_location->
line,
"should not call builtins here.");
3540#define BUILTIN_INLINE_PREFIX "_bi"
3541 char inline_func[
sizeof(BUILTIN_INLINE_PREFIX) +
DECIMAL_SIZE_OF(
int)];
3542 bool cconst =
false;
3547 if (strcmp(
"cstmt!", builtin_func) == 0 || strcmp(
"cexpr!", builtin_func) == 0) {
3550 else if (strcmp(
"cconst!", builtin_func) == 0) {
3553 else if (strcmp(
"cinit!", builtin_func) == 0) {
3557 else if (strcmp(
"attr!", builtin_func) == 0) {
3558 return pm_compile_builtin_attr(iseq, scope_node, arguments, node_location);
3560 else if (strcmp(
"arg!", builtin_func) == 0) {
3561 return pm_compile_builtin_arg(iseq, ret, scope_node, arguments, node_location, popped);
3563 else if (strcmp(
"mandatory_only?", builtin_func) == 0) {
3565 rb_bug(
"mandatory_only? should be in if condition");
3567 else if (!LIST_INSN_SIZE_ZERO(ret)) {
3568 rb_bug(
"mandatory_only? should be put on top");
3571 PUSH_INSN1(ret, *node_location, putobject,
Qfalse);
3572 return pm_compile_builtin_mandatory_only_method(iseq, scope_node, call_node, node_location);
3575 rb_bug(
"can't find builtin function:%s", builtin_func);
3578 COMPILE_ERROR(iseq, node_location->
line,
"can't find builtin function:%s", builtin_func);
3582 int inline_index = node_location->
line;
3583 snprintf(inline_func,
sizeof(inline_func), BUILTIN_INLINE_PREFIX
"%d", inline_index);
3584 builtin_func = inline_func;
3590 typedef VALUE(*builtin_func0)(
void *,
VALUE);
3591 VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL,
Qnil);
3592 PUSH_INSN1(ret, *node_location, putobject, const_val);
3598 DECL_ANCHOR(args_seq);
3602 int argc = pm_setup_args(arguments, call_node->
block, &flags, &keywords, iseq, args_seq, scope_node, node_location);
3604 if (argc != bf->argc) {
3605 COMPILE_ERROR(iseq, node_location->
line,
"argc is not match for builtin function:%s (expect %d but %d)", builtin_func, bf->argc, argc);
3609 unsigned int start_index;
3610 if (delegate_call_p(iseq, argc, args_seq, &start_index)) {
3611 PUSH_INSN2(ret, *node_location, opt_invokebuiltin_delegate, bf,
INT2FIX(start_index));
3614 PUSH_SEQ(ret, args_seq);
3615 PUSH_INSN1(ret, *node_location, invokebuiltin, bf);
3618 if (popped) PUSH_INSN(ret, *node_location, pop);
3626pm_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)
3632 LABEL *else_label = NEW_LABEL(location.
line);
3633 LABEL *end_label = NEW_LABEL(location.
line);
3634 LABEL *retry_end_l = NEW_LABEL(location.
line);
3637 rb_code_location_t code_location = { 0 };
3638 int node_id = location.
node_id;
3640 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3641 if (PM_BRANCH_COVERAGE_P(iseq)) {
3642 const uint8_t *cursors[3] = {
3648 const uint8_t *end_cursor = cursors[0];
3649 end_cursor = (end_cursor == NULL || cursors[1] == NULL) ? cursors[1] : (end_cursor > cursors[1] ? end_cursor : cursors[1]);
3650 end_cursor = (end_cursor == NULL || cursors[2] == NULL) ? cursors[2] : (end_cursor > cursors[2] ? end_cursor : cursors[2]);
3653 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, call_node);
3656 code_location = (rb_code_location_t) {
3657 .beg_pos = { .lineno = start_location.line, .column = start_location.column },
3658 .end_pos = { .lineno = end_location.line, .column = end_location.column }
3661 branches = decl_branch_base(iseq, PTR2NUM(call_node), &code_location,
"&.");
3664 PUSH_INSN(ret, location, dup);
3665 PUSH_INSNL(ret, location, branchnil, else_label);
3667 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0,
"then", branches);
3673 int orig_argc = pm_setup_args(call_node->
arguments, call_node->
block, &flags, &kw_arg, iseq, ret, scope_node, &location);
3674 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
3675 const rb_iseq_t *block_iseq = NULL;
3677 if (call_node->
block != NULL && PM_NODE_TYPE_P(call_node->
block, PM_BLOCK_NODE)) {
3679 pm_scope_node_t next_scope_node;
3680 pm_scope_node_init(call_node->
block, &next_scope_node, scope_node);
3682 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));
3683 pm_scope_node_destroy(&next_scope_node);
3684 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
3687 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3688 flags |= VM_CALL_VCALL;
3692 flags |= VM_CALL_ARGS_SIMPLE;
3696 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
3697 flags |= VM_CALL_FCALL;
3700 if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
3701 if (flags & VM_CALL_ARGS_BLOCKARG) {
3702 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
3703 if (flags & VM_CALL_ARGS_SPLAT) {
3704 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3705 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3707 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 3));
3708 PUSH_INSN(ret, location, pop);
3710 else if (flags & VM_CALL_ARGS_SPLAT) {
3711 PUSH_INSN(ret, location, dup);
3712 PUSH_INSN1(ret, location, putobject,
INT2FIX(-1));
3713 PUSH_SEND_WITH_FLAG(ret, location, idAREF,
INT2FIX(1),
INT2FIX(0));
3714 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 2));
3715 PUSH_INSN(ret, location, pop);
3718 PUSH_INSN1(ret, location, setn,
INT2FIX(orig_argc + 1));
3722 if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
3723 PUSH_INSN(ret, location, splatkw);
3726 PUSH_SEND_R(ret, location, method_id,
INT2FIX(orig_argc), block_iseq,
INT2FIX(flags), kw_arg);
3728 if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
3729 pm_compile_retry_end_label(iseq, ret, retry_end_l);
3730 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, start, retry_end_l, block_iseq, retry_end_l);
3733 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3734 PUSH_INSNL(ret, location, jump, end_label);
3735 PUSH_LABEL(ret, else_label);
3736 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 1,
"else", branches);
3737 PUSH_LABEL(ret, end_label);
3740 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
3741 PUSH_INSN(ret, location, pop);
3744 if (popped) PUSH_INSN(ret, location, pop);
3745 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
3753pm_compile_back_reference_ref(
const pm_back_reference_read_node_t *node)
3767pm_compile_numbered_reference_ref(
const pm_numbered_reference_read_node_t *node)
3773pm_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)
3775#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
3778 enum defined_type dtype = DEFINED_NOT_DEFINED;
3781 switch (PM_NODE_TYPE(node)) {
3786 dtype = DEFINED_NIL;
3789 case PM_INSTANCE_VARIABLE_READ_NODE: {
3792 const pm_instance_variable_read_node_t *cast = (
const pm_instance_variable_read_node_t *) node;
3793 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3795 PUSH_INSN3(ret, location, definedivar,
ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
3800 case PM_LOCAL_VARIABLE_READ_NODE:
3803 case PM_IT_LOCAL_VARIABLE_READ_NODE:
3806 dtype = DEFINED_LVAR;
3809 case PM_GLOBAL_VARIABLE_READ_NODE: {
3812 const pm_global_variable_read_node_t *cast = (
const pm_global_variable_read_node_t *) node;
3813 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3815 PUSH_INSN(ret, location, putnil);
3816 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR),
ID2SYM(name), PUSH_VAL(DEFINED_GVAR));
3821 case PM_CLASS_VARIABLE_READ_NODE: {
3824 const pm_class_variable_read_node_t *cast = (
const pm_class_variable_read_node_t *) node;
3825 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3827 PUSH_INSN(ret, location, putnil);
3828 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR),
ID2SYM(name), PUSH_VAL(DEFINED_CVAR));
3833 case PM_CONSTANT_READ_NODE: {
3836 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) node;
3837 ID name = pm_constant_id_lookup(scope_node, cast->
name);
3839 PUSH_INSN(ret, location, putnil);
3840 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
3848 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
3850 PUSH_INSN(ret, location, putnil);
3851 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
3855 case PM_SUPER_NODE: {
3858 const pm_super_node_t *cast = (
const pm_super_node_t *) node;
3860 if (cast->
block != NULL && !PM_NODE_TYPE_P(cast->
block, PM_BLOCK_ARGUMENT_NODE)) {
3861 dtype = DEFINED_EXPR;
3865 PUSH_INSN(ret, location, putnil);
3866 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3869 case PM_FORWARDING_SUPER_NODE: {
3872 const pm_forwarding_super_node_t *cast = (
const pm_forwarding_super_node_t *) node;
3874 if (cast->
block != NULL) {
3875 dtype = DEFINED_EXPR;
3879 PUSH_INSN(ret, location, putnil);
3880 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3887 dtype = DEFINED_SELF;
3893 dtype = DEFINED_TRUE;
3899 dtype = DEFINED_FALSE;
3902 case PM_CALL_AND_WRITE_NODE:
3905 case PM_CALL_OPERATOR_WRITE_NODE:
3908 case PM_CALL_OR_WRITE_NODE:
3911 case PM_CLASS_VARIABLE_AND_WRITE_NODE:
3914 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
3917 case PM_CLASS_VARIABLE_OR_WRITE_NODE:
3920 case PM_CLASS_VARIABLE_WRITE_NODE:
3923 case PM_CONSTANT_AND_WRITE_NODE:
3926 case PM_CONSTANT_OPERATOR_WRITE_NODE:
3929 case PM_CONSTANT_OR_WRITE_NODE:
3932 case PM_CONSTANT_PATH_AND_WRITE_NODE:
3935 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
3938 case PM_CONSTANT_PATH_OR_WRITE_NODE:
3941 case PM_CONSTANT_PATH_WRITE_NODE:
3944 case PM_CONSTANT_WRITE_NODE:
3947 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
3950 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
3953 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
3956 case PM_GLOBAL_VARIABLE_WRITE_NODE:
3959 case PM_INDEX_AND_WRITE_NODE:
3962 case PM_INDEX_OPERATOR_WRITE_NODE:
3965 case PM_INDEX_OR_WRITE_NODE:
3968 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
3971 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
3974 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
3977 case PM_INSTANCE_VARIABLE_WRITE_NODE:
3980 case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
3983 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
3986 case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
3989 case PM_LOCAL_VARIABLE_WRITE_NODE:
3992 case PM_MULTI_WRITE_NODE:
3995 dtype = DEFINED_ASGN;
3998 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
4001 case PM_ALIAS_METHOD_NODE:
4010 case PM_CASE_MATCH_NODE:
4022 case PM_DEFINED_NODE:
4025 case PM_FLIP_FLOP_NODE:
4037 case PM_IMAGINARY_NODE:
4040 case PM_INTEGER_NODE:
4043 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE:
4046 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
4049 case PM_INTERPOLATED_STRING_NODE:
4052 case PM_INTERPOLATED_SYMBOL_NODE:
4055 case PM_INTERPOLATED_X_STRING_NODE:
4058 case PM_LAMBDA_NODE:
4061 case PM_MATCH_LAST_LINE_NODE:
4064 case PM_MATCH_PREDICATE_NODE:
4067 case PM_MATCH_REQUIRED_NODE:
4070 case PM_MATCH_WRITE_NODE:
4073 case PM_MODULE_NODE:
4082 case PM_POST_EXECUTION_NODE:
4088 case PM_RATIONAL_NODE:
4094 case PM_REGULAR_EXPRESSION_NODE:
4097 case PM_RESCUE_MODIFIER_NODE:
4103 case PM_RETURN_NODE:
4106 case PM_SINGLETON_CLASS_NODE:
4109 case PM_SOURCE_ENCODING_NODE:
4112 case PM_SOURCE_FILE_NODE:
4115 case PM_SOURCE_LINE_NODE:
4118 case PM_STRING_NODE:
4121 case PM_SYMBOL_NODE:
4127 case PM_UNLESS_NODE:
4136 case PM_X_STRING_NODE:
4139 dtype = DEFINED_EXPR;
4142 case PM_BACK_REFERENCE_READ_NODE: {
4145 const pm_back_reference_read_node_t *cast = (
const pm_back_reference_read_node_t *) node;
4146 VALUE ref = pm_compile_back_reference_ref(cast);
4148 PUSH_INSN(ret, location, putnil);
4149 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4153 case PM_NUMBERED_REFERENCE_READ_NODE: {
4156 const pm_numbered_reference_read_node_t *cast = (
const pm_numbered_reference_read_node_t *) node;
4157 VALUE ref = pm_compile_numbered_reference_ref(cast);
4159 PUSH_INSN(ret, location, putnil);
4160 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
4165 case PM_CONSTANT_PATH_NODE: {
4168 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) node;
4169 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4171 if (cast->
parent != NULL) {
4172 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4173 pm_compile_defined_expr0(iseq, cast->
parent, node_location, ret, popped, scope_node,
true, lfinish,
false);
4175 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4176 PM_COMPILE(cast->
parent);
4179 PUSH_INSN1(ret, location, putobject, rb_cObject);
4182 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM),
ID2SYM(name), PUSH_VAL(DEFINED_CONST));
4186 case PM_BEGIN_NODE: {
4189 const pm_begin_node_t *cast = (
const pm_begin_node_t *) node;
4194 dtype = DEFINED_NIL;
4200 pm_compile_defined_expr0(iseq, cast->
statements->
body.
nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4206 dtype = DEFINED_EXPR;
4211 dtype = DEFINED_EXPR;
4216 case PM_PARENTHESES_NODE: {
4219 const pm_parentheses_node_t *cast = (
const pm_parentheses_node_t *) node;
4221 if (cast->
body == NULL) {
4223 dtype = DEFINED_NIL;
4225 else if (PM_NODE_TYPE_P(cast->
body, PM_STATEMENTS_NODE) && ((
const pm_statements_node_t *) cast->
body)->body.size == 1) {
4228 pm_compile_defined_expr0(iseq, ((
const pm_statements_node_t *) cast->
body)->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
4234 dtype = DEFINED_EXPR;
4239 case PM_ARRAY_NODE: {
4242 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
4245 lfinish[1] = NEW_LABEL(location.
line);
4248 for (
size_t index = 0; index < cast->
elements.
size; index++) {
4249 pm_compile_defined_expr0(iseq, cast->
elements.
nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4250 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4253 dtype = DEFINED_EXPR;
4259 case PM_KEYWORD_HASH_NODE: {
4262 const pm_node_list_t *elements;
4264 if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
4265 elements = &((
const pm_hash_node_t *) node)->elements;
4268 elements = &((
const pm_keyword_hash_node_t *) node)->elements;
4271 if (elements->
size > 0 && !lfinish[1]) {
4272 lfinish[1] = NEW_LABEL(location.
line);
4275 for (
size_t index = 0; index < elements->
size; index++) {
4276 pm_compile_defined_expr0(iseq, elements->
nodes[index], node_location, ret, popped, scope_node,
true, lfinish,
false);
4277 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4280 dtype = DEFINED_EXPR;
4283 case PM_ASSOC_NODE: {
4286 const pm_assoc_node_t *cast = (
const pm_assoc_node_t *) node;
4288 pm_compile_defined_expr0(iseq, cast->
key, node_location, ret, popped, scope_node,
true, lfinish,
false);
4289 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4290 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4294 case PM_ASSOC_SPLAT_NODE: {
4297 const pm_assoc_splat_node_t *cast = (
const pm_assoc_splat_node_t *) node;
4299 if (cast->
value == NULL) {
4300 dtype = DEFINED_EXPR;
4304 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node,
true, lfinish,
false);
4307 case PM_IMPLICIT_NODE: {
4310 const pm_implicit_node_t *cast = (
const pm_implicit_node_t *) node;
4311 pm_compile_defined_expr0(iseq, cast->
value, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
4314 case PM_CALL_NODE: {
4315#define BLOCK_P(cast) ((cast)->block != NULL && PM_NODE_TYPE_P((cast)->block, PM_BLOCK_NODE))
4319 const pm_call_node_t *cast = ((
const pm_call_node_t *) node);
4321 if (cast->
block != NULL && PM_NODE_TYPE_P(cast->
block, PM_BLOCK_NODE)) {
4322 dtype = DEFINED_EXPR;
4327 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4328 if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.
line);
4332 pm_compile_defined_expr0(iseq, (
const pm_node_t *) cast->
arguments, node_location, ret, popped, scope_node,
true, lfinish,
false);
4333 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4336 if (cast->
block && PM_NODE_TYPE_P(cast->
block, PM_BLOCK_ARGUMENT_NODE)) {
4337 pm_compile_defined_expr0(iseq, cast->
block, node_location, ret, popped, scope_node,
true, lfinish,
false);
4338 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4342 if (PM_NODE_TYPE_P(cast->
receiver, PM_CALL_NODE) && !BLOCK_P((
const pm_call_node_t *) cast->
receiver)) {
4345 pm_compile_defined_expr0(iseq, cast->
receiver, node_location, ret, popped, scope_node,
true, lfinish,
true);
4346 PUSH_INSNL(ret, location, branchunless, lfinish[2]);
4348 const pm_call_node_t *receiver = (
const pm_call_node_t *) cast->
receiver;
4349 ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
4351 pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
4354 pm_compile_defined_expr0(iseq, cast->
receiver, node_location, ret, popped, scope_node,
true, lfinish,
false);
4355 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4359 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
4361 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4362 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_METHOD),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4365 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
4367 PUSH_INSN(ret, location, putself);
4368 if (explicit_receiver) PUSH_INSN(ret, location, dup);
4370 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_FUNC),
rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
4375 case PM_ARGUMENTS_NODE: {
4378 const pm_arguments_node_t *cast = (
const pm_arguments_node_t *) node;
4380 for (
size_t index = 0; index < cast->
arguments.
size; index++) {
4381 pm_compile_defined_expr0(iseq, cast->
arguments.
nodes[index], node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
4384 lfinish[1] = NEW_LABEL(location.
line);
4386 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4389 dtype = DEFINED_TRUE;
4392 case PM_BLOCK_ARGUMENT_NODE:
4395 dtype = DEFINED_EXPR;
4397 case PM_FORWARDING_ARGUMENTS_NODE:
4400 dtype = DEFINED_EXPR;
4402 case PM_SPLAT_NODE: {
4405 const pm_splat_node_t *cast = (
const pm_splat_node_t *) node;
4408 dtype = DEFINED_EXPR;
4412 pm_compile_defined_expr0(iseq, cast->
expression, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4414 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.
line);
4415 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
4417 dtype = DEFINED_EXPR;
4420 case PM_SHAREABLE_CONSTANT_NODE:
4424 pm_compile_defined_expr0(iseq, ((
const pm_shareable_constant_node_t *) node)->write, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
4427 case PM_BLOCK_LOCAL_VARIABLE_NODE:
4428 case PM_BLOCK_PARAMETER_NODE:
4429 case PM_BLOCK_PARAMETERS_NODE:
4430 case PM_FORWARDING_PARAMETER_NODE:
4431 case PM_IMPLICIT_REST_NODE:
4432 case PM_IT_PARAMETERS_NODE:
4433 case PM_PARAMETERS_NODE:
4434 case PM_KEYWORD_REST_PARAMETER_NODE:
4435 case PM_NO_KEYWORDS_PARAMETER_NODE:
4436 case PM_NUMBERED_PARAMETERS_NODE:
4437 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE:
4438 case PM_OPTIONAL_PARAMETER_NODE:
4439 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
4440 case PM_REQUIRED_PARAMETER_NODE:
4441 case PM_REST_PARAMETER_NODE:
4443 case PM_ALTERNATION_PATTERN_NODE:
4444 case PM_ARRAY_PATTERN_NODE:
4445 case PM_CAPTURE_PATTERN_NODE:
4446 case PM_FIND_PATTERN_NODE:
4447 case PM_HASH_PATTERN_NODE:
4448 case PM_PINNED_EXPRESSION_NODE:
4449 case PM_PINNED_VARIABLE_NODE:
4451 case PM_CALL_TARGET_NODE:
4452 case PM_CLASS_VARIABLE_TARGET_NODE:
4453 case PM_CONSTANT_PATH_TARGET_NODE:
4454 case PM_CONSTANT_TARGET_NODE:
4455 case PM_GLOBAL_VARIABLE_TARGET_NODE:
4456 case PM_INDEX_TARGET_NODE:
4457 case PM_INSTANCE_VARIABLE_TARGET_NODE:
4458 case PM_LOCAL_VARIABLE_TARGET_NODE:
4459 case PM_MULTI_TARGET_NODE:
4462 case PM_ENSURE_NODE:
4464 case PM_RESCUE_NODE:
4468 case PM_EMBEDDED_STATEMENTS_NODE:
4469 case PM_EMBEDDED_VARIABLE_NODE:
4470 case PM_MISSING_NODE:
4471 case PM_PRE_EXECUTION_NODE:
4472 case PM_PROGRAM_NODE:
4474 case PM_STATEMENTS_NODE:
4475 rb_bug(
"Unreachable node in defined?: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4479 PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
4485pm_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,
bool explicit_receiver)
4487 LINK_ELEMENT *lcur = ret->last;
4488 pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4491 LABEL *lstart = NEW_LABEL(node_location->
line);
4492 LABEL *lend = NEW_LABEL(node_location->
line);
4495 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
4497 const rb_iseq_t *rescue = new_child_iseq_with_callback(
4506 lstart->rescued = LABEL_RESCUE_BEG;
4507 lend->rescued = LABEL_RESCUE_END;
4509 APPEND_LABEL(ret, lcur, lstart);
4510 PUSH_LABEL(ret, lend);
4511 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
4516pm_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)
4519 LINK_ELEMENT *last = ret->last;
4521 lfinish[0] = NEW_LABEL(node_location->
line);
4526 pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish,
false);
4530 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, node_location->
line, node_location->
node_id, BIN(putnil), 0)->link);
4531 PUSH_INSN(ret, *node_location, swap);
4533 if (lfinish[2]) PUSH_LABEL(ret, lfinish[2]);
4534 PUSH_INSN(ret, *node_location, pop);
4535 PUSH_LABEL(ret, lfinish[1]);
4539 PUSH_LABEL(ret, lfinish[0]);
4545pm_add_ensure_iseq(LINK_ANCHOR *
const ret, rb_iseq_t *iseq,
int is_return, pm_scope_node_t *scope_node)
4550 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
4552 DECL_ANCHOR(ensure);
4555 if (enlp->erange != NULL) {
4556 DECL_ANCHOR(ensure_part);
4557 LABEL *lstart = NEW_LABEL(0);
4558 LABEL *lend = NEW_LABEL(0);
4560 add_ensure_range(iseq, enlp->erange, lstart, lend);
4562 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
4563 PUSH_LABEL(ensure_part, lstart);
4565 PM_COMPILE_INTO_ANCHOR(ensure_part, (
const pm_node_t *) enlp->ensure_node);
4566 PUSH_LABEL(ensure_part, lend);
4567 PUSH_SEQ(ensure, ensure_part);
4576 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
4577 PUSH_SEQ(ret, ensure);
4581 pm_scope_node_t *scope_node;
4582 rb_ast_id_table_t *local_table_for_iseq;
4587pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg,
int existing)
4593 pm_scope_node_t *scope_node = ctx->scope_node;
4594 rb_ast_id_table_t *local_table_for_iseq = ctx->local_table_for_iseq;
4595 int local_index = ctx->local_index;
4597 ID local = pm_constant_id_lookup(scope_node, constant_id);
4598 local_table_for_iseq->ids[local_index] = local;
4600 *value = (st_data_t)local_index;
4614pm_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)
4616 RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
4618 ID local = pm_constant_id_lookup(scope_node, constant_id);
4619 local_table_for_iseq->ids[local_index] = local;
4620 st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
4628pm_insert_local_special(
ID local_name,
int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq)
4630 local_table_for_iseq->ids[local_index] = local_name;
4631 st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
4641pm_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)
4643 for (
size_t index = 0; index < node->
lefts.
size; index++) {
4644 const pm_node_t *left = node->
lefts.
nodes[index];
4646 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4647 if (!PM_NODE_FLAG_P(left, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4648 pm_insert_local_index(((
const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4653 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4654 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);
4658 if (node->
rest != NULL && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE)) {
4659 const pm_splat_node_t *rest = (
const pm_splat_node_t *) node->
rest;
4661 if (rest->expression != NULL) {
4662 RUBY_ASSERT(PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE));
4664 if (!PM_NODE_FLAG_P(rest->expression, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4665 pm_insert_local_index(((
const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4671 for (
size_t index = 0; index < node->
rights.
size; index++) {
4672 const pm_node_t *right = node->
rights.
nodes[index];
4674 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4675 if (!PM_NODE_FLAG_P(right, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
4676 pm_insert_local_index(((
const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
4681 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4682 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);
4694pm_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)
4696 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4697 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, node->
name, 0);
4698 PUSH_SETLOCAL(ret, location, index.index, index.level);
4710pm_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)
4712 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4713 bool has_rest = (node->
rest && PM_NODE_TYPE_P(node->
rest, PM_SPLAT_NODE) && (((
const pm_splat_node_t *) node->
rest)->expression) != NULL);
4716 int flag = (has_rest || has_rights) ? 1 : 0;
4719 for (
size_t index = 0; index < node->
lefts.
size; index++) {
4720 const pm_node_t *left = node->
lefts.
nodes[index];
4722 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
4723 pm_compile_destructured_param_write(iseq, (
const pm_required_parameter_node_t *) left, ret, scope_node);
4726 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
4727 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) left, ret, scope_node);
4736 const pm_node_t *rest = ((
const pm_splat_node_t *) node->
rest)->expression;
4737 RUBY_ASSERT(PM_NODE_TYPE_P(rest, PM_REQUIRED_PARAMETER_NODE));
4739 pm_compile_destructured_param_write(iseq, (
const pm_required_parameter_node_t *) rest, ret, scope_node);
4747 for (
size_t index = 0; index < node->
rights.
size; index++) {
4748 const pm_node_t *right = node->
rights.
nodes[index];
4750 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
4751 pm_compile_destructured_param_write(iseq, (
const pm_required_parameter_node_t *) right, ret, scope_node);
4754 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
4755 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) right, ret, scope_node);
4782} pm_multi_target_state_node_t;
4800 pm_multi_target_state_node_t *head;
4803 pm_multi_target_state_node_t *tail;
4812 pm_multi_target_state_node_t *node =
ALLOC(pm_multi_target_state_node_t);
4814 node->stack_index = state->stack_size + 1;
4815 node->stack_size = stack_size;
4816 node->position = state->position;
4819 if (state->head == NULL) {
4824 state->tail->next = node;
4828 state->stack_size += stack_size;
4841 if (state->stack_size == 0)
return;
4843 pm_multi_target_state_node_t *current = state->head;
4844 pm_multi_target_state_node_t *previous;
4846 while (current != NULL) {
4847 VALUE offset =
INT2FIX(state->stack_size - current->stack_index + current->position);
4848 current->topn->operands[0] = offset;
4854 if (current->stack_size > 1) {
4855 INSN *insn = current->topn;
4857 for (
size_t index = 1; index < current->stack_size; index += 1) {
4858 LINK_ELEMENT *element = get_next_insn(insn);
4861 insn = (INSN *) element;
4864 insn->operands[0] = offset;
4869 current = current->next;
4876pm_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);
4907pm_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)
4909 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
4911 switch (PM_NODE_TYPE(node)) {
4912 case PM_LOCAL_VARIABLE_TARGET_NODE: {
4918 const pm_local_variable_target_node_t *cast = (
const pm_local_variable_target_node_t *) node;
4919 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
4921 PUSH_SETLOCAL(writes, location, index.index, index.level);
4924 case PM_CLASS_VARIABLE_TARGET_NODE: {
4930 const pm_class_variable_target_node_t *cast = (
const pm_class_variable_target_node_t *) node;
4931 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4934 PUSH_INSN2(writes, location, setclassvariable, operand, get_cvar_ic_value(iseq, name));
4937 case PM_CONSTANT_TARGET_NODE: {
4943 const pm_constant_target_node_t *cast = (
const pm_constant_target_node_t *) node;
4944 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4947 PUSH_INSN1(writes, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
4948 PUSH_INSN1(writes, location, setconstant, operand);
4951 case PM_GLOBAL_VARIABLE_TARGET_NODE: {
4957 const pm_global_variable_target_node_t *cast = (
const pm_global_variable_target_node_t *) node;
4958 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4961 PUSH_INSN1(writes, location, setglobal, operand);
4964 case PM_INSTANCE_VARIABLE_TARGET_NODE: {
4970 const pm_instance_variable_target_node_t *cast = (
const pm_instance_variable_target_node_t *) node;
4971 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4974 PUSH_INSN2(writes, location, setinstancevariable, operand, get_ivar_ic_value(iseq, name));
4977 case PM_CONSTANT_PATH_TARGET_NODE: {
4986 const pm_constant_path_target_node_t *cast = (
const pm_constant_path_target_node_t *) node;
4987 ID name = pm_constant_id_lookup(scope_node, cast->
name);
4989 if (cast->
parent != NULL) {
4990 pm_compile_node(iseq, cast->
parent, parents,
false, scope_node);
4993 PUSH_INSN1(parents, location, putobject, rb_cObject);
4996 if (state == NULL) {
4997 PUSH_INSN(writes, location, swap);
5000 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5001 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
5005 PUSH_INSN1(writes, location, setconstant, operand);
5007 if (state != NULL) {
5008 PUSH_INSN(cleanup, location, pop);
5013 case PM_CALL_TARGET_NODE: {
5022 const pm_call_target_node_t *cast = (
const pm_call_target_node_t *) node;
5023 ID method_id = pm_constant_id_lookup(scope_node, cast->
name);
5025 pm_compile_node(iseq, cast->
receiver, parents,
false, scope_node);
5027 LABEL *safe_label = NULL;
5028 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
5029 safe_label = NEW_LABEL(location.
line);
5030 PUSH_INSN(parents, location, dup);
5031 PUSH_INSNL(parents, location, branchnil, safe_label);
5034 if (state != NULL) {
5035 PUSH_INSN1(writes, location, topn,
INT2FIX(1));
5036 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
5037 PUSH_INSN(writes, location, swap);
5040 int flags = VM_CALL_ARGS_SIMPLE;
5041 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
5043 PUSH_SEND_WITH_FLAG(writes, location, method_id,
INT2FIX(1),
INT2FIX(flags));
5044 if (safe_label != NULL && state == NULL) PUSH_LABEL(writes, safe_label);
5045 PUSH_INSN(writes, location, pop);
5046 if (safe_label != NULL && state != NULL) PUSH_LABEL(writes, safe_label);
5048 if (state != NULL) {
5049 PUSH_INSN(cleanup, location, pop);
5054 case PM_INDEX_TARGET_NODE: {
5064 const pm_index_target_node_t *cast = (
const pm_index_target_node_t *) node;
5066 pm_compile_node(iseq, cast->
receiver, parents,
false, scope_node);
5070 int argc = pm_setup_args(cast->
arguments, (
const pm_node_t *) cast->
block, &flags, &kwargs, iseq, parents, scope_node, &location);
5072 if (state != NULL) {
5073 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5074 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), argc + 1);
5077 PUSH_INSN(writes, location, swap);
5080 for (
int index = 0; index < argc; index++) {
5081 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5083 PUSH_INSN1(writes, location, topn,
INT2FIX(argc + 1));
5091 int ci_argc = argc + 1;
5092 if (flags & VM_CALL_ARGS_SPLAT) {
5094 PUSH_INSN1(writes, location, newarray,
INT2FIX(1));
5095 PUSH_INSN(writes, location, concatarray);
5098 PUSH_SEND_R(writes, location, idASET,
INT2NUM(ci_argc), NULL,
INT2FIX(flags), kwargs);
5099 PUSH_INSN(writes, location, pop);
5101 if (state != NULL) {
5103 PUSH_INSN(writes, location, pop);
5106 for (
int index = 0; index < argc + 1; index++) {
5107 PUSH_INSN(cleanup, location, pop);
5113 case PM_MULTI_TARGET_NODE: {
5120 size_t before_position;
5121 if (state != NULL) {
5122 before_position = state->position;
5126 pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
5127 if (state != NULL) state->position = before_position;
5131 case PM_SPLAT_NODE: {
5137 const pm_splat_node_t *cast = (
const pm_splat_node_t *) node;
5140 pm_compile_target_node(iseq, cast->
expression, parents, writes, cleanup, scope_node, state);
5146 rb_bug(
"Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5157pm_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)
5159 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5160 const pm_node_list_t *lefts;
5161 const pm_node_t *rest;
5162 const pm_node_list_t *rights;
5164 switch (PM_NODE_TYPE(node)) {
5165 case PM_MULTI_TARGET_NODE: {
5166 const pm_multi_target_node_t *cast = (
const pm_multi_target_node_t *) node;
5167 lefts = &cast->
lefts;
5172 case PM_MULTI_WRITE_NODE: {
5173 const pm_multi_write_node_t *cast = (
const pm_multi_write_node_t *) node;
5174 lefts = &cast->
lefts;
5180 rb_bug(
"Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5184 bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((
const pm_splat_node_t *) rest)->expression != NULL;
5185 bool has_posts = rights->
size > 0;
5190 PUSH_INSN2(writes, location, expandarray,
INT2FIX(lefts->
size),
INT2FIX((has_rest || has_posts) ? 1 : 0));
5196 if (state == NULL) state = &target_state;
5198 size_t base_position = state->position;
5199 size_t splat_position = (has_rest || has_posts) ? 1 : 0;
5202 for (
size_t index = 0; index < lefts->
size; index++) {
5203 const pm_node_t *target = lefts->
nodes[index];
5204 state->position = lefts->
size - index + splat_position + base_position;
5205 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5210 const pm_node_t *target = ((
const pm_splat_node_t *) rest)->expression;
5211 state->position = 1 + rights->
size + base_position;
5217 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5222 if (!has_rest && rest != NULL) {
5226 for (
size_t index = 0; index < rights->
size; index++) {
5227 const pm_node_t *target = rights->
nodes[index];
5228 state->position = rights->
size - index + base_position;
5229 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, state);
5240pm_compile_for_node_index(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const ret, pm_scope_node_t *scope_node)
5242 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5244 switch (PM_NODE_TYPE(node)) {
5245 case PM_LOCAL_VARIABLE_TARGET_NODE: {
5248 PUSH_GETLOCAL(ret, location, 1, 0);
5249 pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
5252 case PM_CLASS_VARIABLE_TARGET_NODE:
5253 case PM_CONSTANT_TARGET_NODE:
5254 case PM_GLOBAL_VARIABLE_TARGET_NODE:
5255 case PM_INSTANCE_VARIABLE_TARGET_NODE:
5256 case PM_CONSTANT_PATH_TARGET_NODE:
5257 case PM_CALL_TARGET_NODE:
5258 case PM_INDEX_TARGET_NODE:
5259 case PM_SPLAT_NODE: {
5263 DECL_ANCHOR(writes);
5264 DECL_ANCHOR(cleanup);
5268 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
5270 PUSH_GETLOCAL(ret, location, 1, 0);
5273 PUSH_SEQ(ret, writes);
5274 PUSH_SEQ(ret, cleanup);
5276 pm_multi_target_state_update(&state);
5279 case PM_MULTI_TARGET_NODE: {
5280 DECL_ANCHOR(writes);
5281 DECL_ANCHOR(cleanup);
5283 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
5285 LABEL *not_single = NEW_LABEL(location.
line);
5286 LABEL *not_ary = NEW_LABEL(location.
line);
5294 PUSH_GETLOCAL(ret, location, 1, 0);
5295 PUSH_INSN(ret, location, dup);
5296 PUSH_CALL(ret, location, idLength,
INT2FIX(0));
5297 PUSH_INSN1(ret, location, putobject,
INT2FIX(1));
5298 PUSH_CALL(ret, location, idEq,
INT2FIX(1));
5299 PUSH_INSNL(ret, location, branchunless, not_single);
5300 PUSH_INSN(ret, location, dup);
5301 PUSH_INSN1(ret, location, putobject,
INT2FIX(0));
5302 PUSH_CALL(ret, location, idAREF,
INT2FIX(1));
5303 PUSH_INSN1(ret, location, putobject,
rb_cArray);
5304 PUSH_INSN(ret, location, swap);
5305 PUSH_CALL(ret, location, rb_intern(
"try_convert"),
INT2FIX(1));
5306 PUSH_INSN(ret, location, dup);
5307 PUSH_INSNL(ret, location, branchunless, not_ary);
5308 PUSH_INSN(ret, location, swap);
5310 PUSH_LABEL(ret, not_ary);
5311 PUSH_INSN(ret, location, pop);
5313 PUSH_LABEL(ret, not_single);
5314 PUSH_SEQ(ret, writes);
5315 PUSH_SEQ(ret, cleanup);
5319 rb_bug(
"Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
5325pm_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)
5329 LABEL *lstart = NEW_LABEL(node_location->
line);
5330 LABEL *lend = NEW_LABEL(node_location->
line);
5331 LABEL *lcont = NEW_LABEL(node_location->
line);
5333 pm_scope_node_t rescue_scope_node;
5334 pm_scope_node_init((
const pm_node_t *) cast->
rescue_clause, &rescue_scope_node, scope_node);
5336 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
5340 pm_node_line_number(parser, (
const pm_node_t *) cast->
rescue_clause)
5343 pm_scope_node_destroy(&rescue_scope_node);
5345 lstart->rescued = LABEL_RESCUE_BEG;
5346 lend->rescued = LABEL_RESCUE_END;
5347 PUSH_LABEL(ret, lstart);
5349 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
5350 ISEQ_COMPILE_DATA(iseq)->in_rescue =
true;
5353 PM_COMPILE_NOT_POPPED((
const pm_node_t *) cast->
statements);
5357 PUSH_INSN(ret, location, putnil);
5360 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
5361 PUSH_LABEL(ret, lend);
5364 if (!popped) PUSH_INSN(ret, *node_location, pop);
5365 PM_COMPILE((
const pm_node_t *) cast->
else_clause);
5368 PUSH_INSN(ret, *node_location, nop);
5369 PUSH_LABEL(ret, lcont);
5371 if (popped) PUSH_INSN(ret, *node_location, pop);
5372 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
5373 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
5377pm_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)
5383 if (statements != NULL) {
5384 location = PM_NODE_START_LOCATION(parser, statements);
5387 location = *node_location;
5390 LABEL *lstart = NEW_LABEL(location.
line);
5391 LABEL *lend = NEW_LABEL(location.
line);
5392 LABEL *lcont = NEW_LABEL(location.
line);
5399 if (statements != NULL) {
5400 pm_compile_node(iseq, (
const pm_node_t *) statements, ensr,
true, scope_node);
5403 LINK_ELEMENT *last = ensr->last;
5404 bool last_leave = last && IS_INSN(last) && IS_INSN_ID(last, leave);
5409 push_ensure_entry(iseq, &enl, &er, (
void *) cast->
ensure_clause);
5411 PUSH_LABEL(ret, lstart);
5413 pm_compile_rescue(iseq, cast, node_location, ret, popped | last_leave, scope_node);
5416 pm_compile_node(iseq, (
const pm_node_t *) cast->
statements, ret, popped | last_leave, scope_node);
5418 else if (!(popped | last_leave)) {
5419 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
5422 PUSH_LABEL(ret, lend);
5423 PUSH_SEQ(ret, ensr);
5424 if (!popped && last_leave) PUSH_INSN(ret, *node_location, putnil);
5425 PUSH_LABEL(ret, lcont);
5426 if (last_leave) PUSH_INSN(ret, *node_location, pop);
5428 pm_scope_node_t next_scope_node;
5429 pm_scope_node_init((
const pm_node_t *) cast->
ensure_clause, &next_scope_node, scope_node);
5431 rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(
5438 pm_scope_node_destroy(&next_scope_node);
5440 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
5441 if (lstart->link.next != &lend->link) {
5443 PUSH_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, lcont);
5444 erange = erange->next;
5447 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
5455pm_opt_str_freeze_p(
const rb_iseq_t *iseq,
const pm_call_node_t *node)
5458 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5460 PM_NODE_TYPE_P(node->
receiver, PM_STRING_NODE) &&
5462 node->
block == NULL &&
5463 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5472pm_opt_aref_with_p(
const rb_iseq_t *iseq,
const pm_call_node_t *node)
5475 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5477 PM_NODE_TYPE_P((
const pm_node_t *) node->
arguments, PM_ARGUMENTS_NODE) &&
5479 PM_NODE_TYPE_P(((
const pm_arguments_node_t *) node->
arguments)->arguments.nodes[0], PM_STRING_NODE) &&
5480 node->
block == NULL &&
5481 !PM_NODE_FLAG_P(((
const pm_arguments_node_t *) node->
arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
5482 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5491pm_opt_aset_with_p(
const rb_iseq_t *iseq,
const pm_call_node_t *node)
5494 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
5496 PM_NODE_TYPE_P((
const pm_node_t *) node->
arguments, PM_ARGUMENTS_NODE) &&
5498 PM_NODE_TYPE_P(((
const pm_arguments_node_t *) node->
arguments)->arguments.nodes[0], PM_STRING_NODE) &&
5499 node->
block == NULL &&
5500 !PM_NODE_FLAG_P(((
const pm_arguments_node_t *) node->
arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
5501 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
5510pm_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)
5512 const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, name_loc, node_id);
5514 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
5515 ISEQ_BODY(iseq)->ic_size++;
5516 VALUE segments = rb_ary_new_from_args(1, name);
5517 PUSH_INSN1(ret, location, opt_getconstant_path, segments);
5520 PUSH_INSN(ret, location, putnil);
5521 PUSH_INSN1(ret, location, putobject,
Qtrue);
5522 PUSH_INSN1(ret, location, getconstant, name);
5531pm_constant_path_parts(
const pm_node_t *node,
const pm_scope_node_t *scope_node)
5536 switch (PM_NODE_TYPE(node)) {
5537 case PM_CONSTANT_READ_NODE: {
5538 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) node;
5541 rb_ary_unshift(parts, name);
5544 case PM_CONSTANT_PATH_NODE: {
5545 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) node;
5548 rb_ary_unshift(parts, name);
5549 if (cast->
parent == NULL) {
5550 rb_ary_unshift(parts,
ID2SYM(idNULL));
5569pm_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)
5571 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5573 switch (PM_NODE_TYPE(node)) {
5574 case PM_CONSTANT_READ_NODE: {
5575 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) node;
5578 PUSH_INSN1(body, location, putobject,
Qtrue);
5579 PUSH_INSN1(body, location, getconstant, name);
5582 case PM_CONSTANT_PATH_NODE: {
5583 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) node;
5586 if (cast->
parent == NULL) {
5587 PUSH_INSN(body, location, pop);
5588 PUSH_INSN1(body, location, putobject, rb_cObject);
5589 PUSH_INSN1(body, location, putobject,
Qtrue);
5590 PUSH_INSN1(body, location, getconstant, name);
5593 pm_compile_constant_path(iseq, cast->
parent, prefix, body,
false, scope_node);
5594 PUSH_INSN1(body, location, putobject,
Qfalse);
5595 PUSH_INSN1(body, location, getconstant, name);
5600 PM_COMPILE_INTO_ANCHOR(prefix, node);
5609pm_compile_shareable_constant_literal(rb_iseq_t *iseq,
const pm_node_t *node,
const pm_scope_node_t *scope_node)
5611 switch (PM_NODE_TYPE(node)) {
5615 case PM_SYMBOL_NODE:
5616 case PM_REGULAR_EXPRESSION_NODE:
5617 case PM_SOURCE_LINE_NODE:
5618 case PM_INTEGER_NODE:
5620 case PM_RATIONAL_NODE:
5621 case PM_IMAGINARY_NODE:
5622 case PM_SOURCE_ENCODING_NODE:
5623 return pm_static_literal_value(iseq, node, scope_node);
5624 case PM_STRING_NODE:
5625 return parse_static_literal_string(iseq, scope_node, node, &((
const pm_string_node_t *) node)->unescaped);
5626 case PM_SOURCE_FILE_NODE:
5627 return pm_source_file_value((
const pm_source_file_node_t *) node, scope_node);
5628 case PM_ARRAY_NODE: {
5629 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
5632 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5633 VALUE element = pm_compile_shareable_constant_literal(iseq, cast->
elements.
nodes[index], scope_node);
5636 rb_ary_push(result, element);
5641 case PM_HASH_NODE: {
5642 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
5645 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5647 if (!PM_NODE_TYPE_P(element, PM_ASSOC_NODE))
return Qundef;
5649 const pm_assoc_node_t *assoc = (
const pm_assoc_node_t *) element;
5651 VALUE key = pm_compile_shareable_constant_literal(iseq, assoc->
key, scope_node);
5654 VALUE value = pm_compile_shareable_constant_literal(iseq, assoc->
value, scope_node);
5657 rb_hash_aset(result, key, value);
5672pm_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)
5674 VALUE literal = pm_compile_shareable_constant_literal(iseq, node, scope_node);
5676 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5677 PUSH_INSN1(ret, location, putobject, literal);
5681 const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
5682 switch (PM_NODE_TYPE(node)) {
5683 case PM_ARRAY_NODE: {
5684 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
5687 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5690 for (
size_t index = 0; index < cast->
elements.
size; index++) {
5691 pm_compile_shareable_constant_value(iseq, cast->
elements.
nodes[index], shareability, path, ret, scope_node,
false);
5697 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5698 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5703 case PM_HASH_NODE: {
5704 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
5707 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5710 pm_compile_hash_elements(iseq, (
const pm_node_t *) cast, &cast->elements, shareability, path,
false, ret, scope_node);
5713 ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern(
"make_shareable_copy") : rb_intern(
"make_shareable");
5714 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5720 DECL_ANCHOR(value_seq);
5722 pm_compile_node(iseq, node, value_seq,
false, scope_node);
5723 if (PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) {
5724 PUSH_SEND_WITH_FLAG(value_seq, location, idUMinus,
INT2FIX(0),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5727 if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL) {
5728 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5729 PUSH_SEQ(ret, value_seq);
5730 PUSH_INSN1(ret, location, putobject, path);
5731 PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"ensure_shareable"),
INT2FIX(2),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5733 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) {
5734 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5735 PUSH_SEQ(ret, value_seq);
5736 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable_copy"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5738 else if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING) {
5739 if (top) PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5740 PUSH_SEQ(ret, value_seq);
5741 if (top) PUSH_SEND_WITH_FLAG(ret, location, rb_intern(
"make_shareable"),
INT2FIX(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5754pm_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)
5757 ID name_id = pm_constant_id_lookup(scope_node, node->
name);
5759 if (shareability != 0) {
5760 pm_compile_shareable_constant_value(iseq, node->
value, shareability, rb_id2str(name_id), ret, scope_node,
true);
5763 PM_COMPILE_NOT_POPPED(node->
value);
5766 if (!popped) PUSH_INSN(ret, location, dup);
5767 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5770 PUSH_INSN1(ret, location, setconstant, operand);
5778pm_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)
5783 LABEL *end_label = NEW_LABEL(location.
line);
5785 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5786 if (!popped) PUSH_INSN(ret, location, dup);
5788 PUSH_INSNL(ret, location, branchunless, end_label);
5789 if (!popped) PUSH_INSN(ret, location, pop);
5791 if (shareability != 0) {
5792 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5795 PM_COMPILE_NOT_POPPED(node->
value);
5798 if (!popped) PUSH_INSN(ret, location, dup);
5799 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5800 PUSH_INSN1(ret, location, setconstant, name);
5801 PUSH_LABEL(ret, end_label);
5809pm_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)
5814 LABEL *set_label = NEW_LABEL(location.
line);
5815 LABEL *end_label = NEW_LABEL(location.
line);
5817 PUSH_INSN(ret, location, putnil);
5818 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST), name,
Qtrue);
5819 PUSH_INSNL(ret, location, branchunless, set_label);
5821 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5822 if (!popped) PUSH_INSN(ret, location, dup);
5824 PUSH_INSNL(ret, location, branchif, end_label);
5825 if (!popped) PUSH_INSN(ret, location, pop);
5826 PUSH_LABEL(ret, set_label);
5828 if (shareability != 0) {
5829 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5832 PM_COMPILE_NOT_POPPED(node->
value);
5835 if (!popped) PUSH_INSN(ret, location, dup);
5836 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5837 PUSH_INSN1(ret, location, setconstant, name);
5838 PUSH_LABEL(ret, end_label);
5846pm_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)
5853 pm_compile_constant_read(iseq, name, &node->
name_loc, location.
node_id, ret, scope_node);
5855 if (shareability != 0) {
5856 pm_compile_shareable_constant_value(iseq, node->
value, shareability, name, ret, scope_node,
true);
5859 PM_COMPILE_NOT_POPPED(node->
value);
5862 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
5863 if (!popped) PUSH_INSN(ret, location, dup);
5865 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5866 PUSH_INSN1(ret, location, setconstant, name);
5874pm_constant_path_path(
const pm_constant_path_node_t *node,
const pm_scope_node_t *scope_node)
5877 rb_ary_push(parts, rb_id2str(pm_constant_id_lookup(scope_node, node->
name)));
5879 const pm_node_t *current = node->
parent;
5880 while (current != NULL && PM_NODE_TYPE_P(current, PM_CONSTANT_PATH_NODE)) {
5881 const pm_constant_path_node_t *cast = (
const pm_constant_path_node_t *) current;
5882 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, cast->
name)));
5886 if (current == NULL) {
5887 rb_ary_unshift(parts, rb_id2str(idNULL));
5889 else if (PM_NODE_TYPE_P(current, PM_CONSTANT_READ_NODE)) {
5890 rb_ary_unshift(parts, rb_id2str(pm_constant_id_lookup(scope_node, ((
const pm_constant_read_node_t *) current)->name)));
5904pm_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)
5907 const pm_constant_path_node_t *target = node->
target;
5911 PM_COMPILE_NOT_POPPED((
const pm_node_t *) target->
parent);
5914 PUSH_INSN1(ret, location, putobject, rb_cObject);
5917 if (shareability != 0) {
5918 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
5921 PM_COMPILE_NOT_POPPED(node->
value);
5925 PUSH_INSN(ret, location, swap);
5926 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
5929 PUSH_INSN(ret, location, swap);
5930 PUSH_INSN1(ret, location, setconstant, name);
5938pm_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)
5941 const pm_constant_path_node_t *target = node->
target;
5944 LABEL *lfin = NEW_LABEL(location.
line);
5947 PM_COMPILE_NOT_POPPED(target->
parent);
5950 PUSH_INSN1(ret, location, putobject, rb_cObject);
5953 PUSH_INSN(ret, location, dup);
5954 PUSH_INSN1(ret, location, putobject,
Qtrue);
5955 PUSH_INSN1(ret, location, getconstant, name);
5957 if (!popped) PUSH_INSN(ret, location, dup);
5958 PUSH_INSNL(ret, location, branchunless, lfin);
5960 if (!popped) PUSH_INSN(ret, location, pop);
5962 if (shareability != 0) {
5963 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
5966 PM_COMPILE_NOT_POPPED(node->
value);
5970 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
5973 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
5974 PUSH_INSN(ret, location, swap);
5977 PUSH_INSN1(ret, location, setconstant, name);
5978 PUSH_LABEL(ret, lfin);
5980 if (!popped) PUSH_INSN(ret, location, swap);
5981 PUSH_INSN(ret, location, pop);
5989pm_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)
5992 const pm_constant_path_node_t *target = node->
target;
5995 LABEL *lassign = NEW_LABEL(location.
line);
5996 LABEL *lfin = NEW_LABEL(location.
line);
5999 PM_COMPILE_NOT_POPPED(target->
parent);
6002 PUSH_INSN1(ret, location, putobject, rb_cObject);
6005 PUSH_INSN(ret, location, dup);
6006 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CONST_FROM), name,
Qtrue);
6007 PUSH_INSNL(ret, location, branchunless, lassign);
6009 PUSH_INSN(ret, location, dup);
6010 PUSH_INSN1(ret, location, putobject,
Qtrue);
6011 PUSH_INSN1(ret, location, getconstant, name);
6013 if (!popped) PUSH_INSN(ret, location, dup);
6014 PUSH_INSNL(ret, location, branchif, lfin);
6016 if (!popped) PUSH_INSN(ret, location, pop);
6017 PUSH_LABEL(ret, lassign);
6019 if (shareability != 0) {
6020 pm_compile_shareable_constant_value(iseq, node->
value, shareability, pm_constant_path_path(node->
target, scope_node), ret, scope_node,
true);
6023 PM_COMPILE_NOT_POPPED(node->
value);
6027 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6030 PUSH_INSN1(ret, location, dupn,
INT2FIX(2));
6031 PUSH_INSN(ret, location, swap);
6034 PUSH_INSN1(ret, location, setconstant, name);
6035 PUSH_LABEL(ret, lfin);
6037 if (!popped) PUSH_INSN(ret, location, swap);
6038 PUSH_INSN(ret, location, pop);
6046pm_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)
6049 const pm_constant_path_node_t *target = node->
target;
6055 PM_COMPILE_NOT_POPPED(target->
parent);
6058 PUSH_INSN1(ret, location, putobject, rb_cObject);
6061 PUSH_INSN(ret, location, dup);
6062 PUSH_INSN1(ret, location, putobject,
Qtrue);
6063 PUSH_INSN1(ret, location, getconstant, name);
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);
6072 PUSH_CALL(ret, location, method_id,
INT2FIX(1));
6073 PUSH_INSN(ret, location, swap);
6076 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
6077 PUSH_INSN(ret, location, swap);
6080 PUSH_INSN1(ret, location, setconstant, name);
6089#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))
6096pm_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)
6102 pm_parameters_node_t *parameters_node = NULL;
6103 pm_node_list_t *keywords_list = NULL;
6104 pm_node_list_t *optionals_list = NULL;
6105 pm_node_list_t *posts_list = NULL;
6106 pm_node_list_t *requireds_list = NULL;
6107 pm_node_list_t *block_locals = NULL;
6108 bool trailing_comma =
false;
6110 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
6114 if (scope_node->parameters != NULL) {
6115 switch (PM_NODE_TYPE(scope_node->parameters)) {
6116 case PM_BLOCK_PARAMETERS_NODE: {
6117 pm_block_parameters_node_t *cast = (pm_block_parameters_node_t *) scope_node->parameters;
6118 parameters_node = cast->parameters;
6119 block_locals = &cast->locals;
6121 if (parameters_node) {
6122 if (parameters_node->
rest && PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE)) {
6123 trailing_comma =
true;
6128 case PM_PARAMETERS_NODE: {
6129 parameters_node = (pm_parameters_node_t *) scope_node->parameters;
6132 case PM_NUMBERED_PARAMETERS_NODE: {
6133 uint32_t maximum = ((
const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
6134 body->
param.lead_num = maximum;
6135 body->
param.flags.ambiguous_param0 = maximum == 1;
6138 case PM_IT_PARAMETERS_NODE:
6139 body->
param.lead_num = 1;
6140 body->
param.flags.ambiguous_param0 =
true;
6143 rb_bug(
"Unexpected node type for parameters: %s", pm_node_type_to_str(PM_NODE_TYPE(scope_node->parameters)));
6147 struct rb_iseq_param_keyword *keyword = NULL;
6149 if (parameters_node) {
6150 optionals_list = ¶meters_node->
optionals;
6151 requireds_list = ¶meters_node->
requireds;
6152 keywords_list = ¶meters_node->
keywords;
6153 posts_list = ¶meters_node->
posts;
6155 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))) {
6156 body->
param.opt_num = 0;
6159 body->
param.lead_num = 0;
6160 body->
param.opt_num = 0;
6166 size_t locals_size = locals->
size;
6169 st_table *index_lookup_table = st_init_numtable();
6171 int table_size = (int) locals_size;
6175 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
6177 if (keywords_list && keywords_list->
size) {
6181 if (requireds_list) {
6182 for (
size_t i = 0; i < requireds_list->
size; i++) {
6186 pm_node_t *required = requireds_list->
nodes[i];
6187 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6190 else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
6191 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6200 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6208 if (optionals_list && optionals_list->
size) {
6209 for (
size_t i = 0; i < optionals_list->
size; i++) {
6210 pm_node_t * node = optionals_list->
nodes[i];
6211 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6221 if (parameters_node) {
6222 if (parameters_node->
rest) {
6223 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6224 if (!((
const pm_rest_parameter_node_t *) parameters_node->
rest)->name || PM_NODE_FLAG_P(parameters_node->
rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6237 if (PM_NODE_TYPE_P(parameters_node->
keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
6239 if (requireds_list->
size == 0 && optionals_list->
size == 0 && keywords_list->
size == 0) {
6240 ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
6241 ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
6249 const pm_keyword_rest_parameter_node_t *kw_rest = (
const pm_keyword_rest_parameter_node_t *) parameters_node->
keyword_rest;
6252 if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6260 for (
size_t i = 0; i < posts_list->
size; i++) {
6264 pm_node_t *required = posts_list->
nodes[i];
6265 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6271 if (keywords_list && keywords_list->
size) {
6272 for (
size_t i = 0; i < keywords_list->
size; i++) {
6273 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6274 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6280 if (parameters_node && parameters_node->
block) {
6281 const pm_block_parameter_node_t *block_node = (
const pm_block_parameter_node_t *) parameters_node->
block;
6283 if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
6290 rb_ast_id_table_t *local_table_for_iseq =
ALLOCV(idtmp,
sizeof(rb_ast_id_table_t) + table_size *
sizeof(
ID));
6291 local_table_for_iseq->size = table_size;
6311 int local_index = 0;
6318 if (requireds_list && requireds_list->
size) {
6319 for (
size_t i = 0; i < requireds_list->
size; i++, local_index++) {
6325 pm_node_t *required = requireds_list->
nodes[i];
6327 switch (PM_NODE_TYPE(required)) {
6328 case PM_MULTI_TARGET_NODE: {
6331 local = rb_make_temporary_id(local_index);
6332 local_table_for_iseq->ids[local_index] = local;
6335 case PM_REQUIRED_PARAMETER_NODE: {
6338 const pm_required_parameter_node_t *param = (
const pm_required_parameter_node_t *) required;
6340 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6341 ID local = pm_constant_id_lookup(scope_node, param->
name);
6342 local_table_for_iseq->ids[local_index] = local;
6345 pm_insert_local_index(param->
name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6351 rb_bug(
"Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(required)));
6355 body->
param.lead_num = (int) requireds_list->
size;
6356 body->
param.flags.has_lead =
true;
6359 if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
6360 ID local = rb_make_temporary_id(local_index);
6361 local_table_for_iseq->ids[local_index++] = local;
6366 if (optionals_list && optionals_list->
size) {
6367 body->
param.opt_num = (int) optionals_list->
size;
6368 body->
param.flags.has_opt =
true;
6370 for (
size_t i = 0; i < optionals_list->
size; i++, local_index++) {
6371 pm_node_t * node = optionals_list->
nodes[i];
6372 pm_constant_id_t name = ((
const pm_optional_parameter_node_t *) node)->name;
6374 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6375 ID local = pm_constant_id_lookup(scope_node, name);
6376 local_table_for_iseq->ids[local_index] = local;
6379 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6386 if (parameters_node && parameters_node->
rest) {
6387 body->
param.rest_start = local_index;
6391 if (!(PM_NODE_TYPE_P(parameters_node->
rest, PM_IMPLICIT_REST_NODE))) {
6392 body->
param.flags.has_rest =
true;
6400 if (PM_NODE_FLAG_P(parameters_node->
rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6401 ID local = pm_constant_id_lookup(scope_node, name);
6402 local_table_for_iseq->ids[local_index] = local;
6405 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6411 body->
param.flags.anon_rest =
true;
6412 pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
6421 if (posts_list && posts_list->
size) {
6422 body->
param.post_num = (int) posts_list->
size;
6423 body->
param.post_start = local_index;
6424 body->
param.flags.has_post =
true;
6426 for (
size_t i = 0; i < posts_list->
size; i++, local_index++) {
6432 const pm_node_t *post_node = posts_list->
nodes[i];
6434 switch (PM_NODE_TYPE(post_node)) {
6435 case PM_MULTI_TARGET_NODE: {
6438 local = rb_make_temporary_id(local_index);
6439 local_table_for_iseq->ids[local_index] = local;
6442 case PM_REQUIRED_PARAMETER_NODE: {
6445 const pm_required_parameter_node_t *param = (
const pm_required_parameter_node_t *) post_node;
6447 if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6448 ID local = pm_constant_id_lookup(scope_node, param->
name);
6449 local_table_for_iseq->ids[local_index] = local;
6452 pm_insert_local_index(param->
name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6457 rb_bug(
"Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(post_node)));
6465 if (keywords_list && keywords_list->
size) {
6466 keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6467 keyword->num = (int) keywords_list->
size;
6469 const VALUE default_values = rb_ary_hidden_new(1);
6472 for (
size_t i = 0; i < keywords_list->
size; i++) {
6473 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6478 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
6479 name = ((
const pm_required_keyword_parameter_node_t *) keyword_parameter_node)->name;
6480 keyword->required_num++;
6481 ID local = pm_constant_id_lookup(scope_node, name);
6483 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6484 local_table_for_iseq->ids[local_index] = local;
6487 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6493 for (
size_t i = 0; i < keywords_list->
size; i++) {
6494 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6499 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
6500 const pm_optional_keyword_parameter_node_t *cast = ((
const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6502 pm_node_t *value = cast->
value;
6505 if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !PM_CONTAINER_P(value)) {
6506 rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
6509 rb_ary_push(default_values, complex_mark);
6512 ID local = pm_constant_id_lookup(scope_node, name);
6513 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6514 local_table_for_iseq->ids[local_index] = local;
6517 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6527 for (
int i = 0; i <
RARRAY_LEN(default_values); i++) {
6529 if (dv == complex_mark) dv =
Qundef;
6533 keyword->default_values = dvs;
6537 keyword->bits_start = local_index;
6538 ID local = rb_make_temporary_id(local_index);
6539 local_table_for_iseq->ids[local_index] = local;
6542 body->
param.keyword = keyword;
6543 body->
param.flags.has_kw =
true;
6546 if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->
size == 1 && !trailing_comma) {
6547 body->
param.flags.ambiguous_param0 =
true;
6550 if (parameters_node) {
6555 case PM_NO_KEYWORDS_PARAMETER_NODE: {
6558 body->
param.flags.accepts_no_kwarg =
true;
6561 case PM_KEYWORD_REST_PARAMETER_NODE: {
6564 const pm_keyword_rest_parameter_node_t *kw_rest_node = (
const pm_keyword_rest_parameter_node_t *) parameters_node->
keyword_rest;
6565 if (!body->
param.flags.has_kw) {
6566 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6569 keyword->rest_start = local_index;
6570 body->
param.flags.has_kwrest =
true;
6574 if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6575 ID local = pm_constant_id_lookup(scope_node, constant_id);
6576 local_table_for_iseq->ids[local_index] = local;
6579 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6583 body->
param.flags.anon_kwrest =
true;
6584 pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
6590 case PM_FORWARDING_PARAMETER_NODE: {
6593 if (!ISEQ_BODY(iseq)->param.
flags.forwardable) {
6595 body->
param.rest_start = local_index;
6596 body->
param.flags.has_rest =
true;
6597 body->
param.flags.anon_rest =
true;
6598 pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
6602 body->
param.flags.has_kw =
false;
6603 body->
param.flags.has_kwrest =
true;
6604 body->
param.flags.anon_kwrest =
true;
6605 body->
param.keyword = keyword =
ZALLOC_N(
struct rb_iseq_param_keyword, 1);
6606 keyword->rest_start = local_index;
6607 pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
6610 body->
param.block_start = local_index;
6611 body->
param.flags.has_block =
true;
6612 pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
6616 pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
6620 rb_bug(
"node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->
keyword_rest)));
6626 if (parameters_node->
block) {
6627 body->
param.block_start = local_index;
6628 body->
param.flags.has_block =
true;
6629 iseq_set_use_block(iseq);
6634 if (PM_NODE_FLAG_P(parameters_node->
block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
6635 ID local = pm_constant_id_lookup(scope_node, name);
6636 local_table_for_iseq->ids[local_index] = local;
6639 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6643 pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
6662 if (requireds_list && requireds_list->
size) {
6663 for (
size_t i = 0; i < requireds_list->
size; i++) {
6667 const pm_node_t *required = requireds_list->
nodes[i];
6669 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6670 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);
6676 if (posts_list && posts_list->
size) {
6677 for (
size_t i = 0; i < posts_list->
size; i++) {
6681 const pm_node_t *post = posts_list->
nodes[i];
6683 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6684 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);
6690 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6691 if (PM_NODE_TYPE_P(((
const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
6692 body->
param.lead_num++;
6695 body->
param.rest_start = local_index;
6696 body->
param.flags.has_rest =
true;
6699 ID local = rb_make_temporary_id(local_index);
6700 local_table_for_iseq->ids[local_index] = local;
6705 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
6706 int maximum = ((
const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
6708 for (
int i = 0; i < maximum; i++, local_index++) {
6709 const uint8_t param_name[] = {
'_',
'1' + i };
6711 RUBY_ASSERT(constant_id &&
"parser should fill in any gaps in numbered parameters");
6712 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6714 body->
param.lead_num = maximum;
6715 body->
param.flags.has_lead =
true;
6726 if (block_locals && block_locals->
size) {
6727 for (
size_t i = 0; i < block_locals->
size; i++, local_index++) {
6728 pm_constant_id_t constant_id = ((
const pm_block_local_variable_node_t *) block_locals->
nodes[i])->name;
6729 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
6734 if (scope_node->locals.
size) {
6735 for (
size_t i = 0; i < scope_node->locals.
size; i++) {
6739 ctx.scope_node = scope_node;
6740 ctx.local_table_for_iseq = local_table_for_iseq;
6741 ctx.local_index = local_index;
6743 st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
6745 local_index = ctx.local_index;
6754 if (scope_node->index_lookup_table) {
6755 st_free_table(scope_node->index_lookup_table);
6757 scope_node->index_lookup_table = index_lookup_table;
6758 iseq_calc_param_size(iseq);
6760 if (ISEQ_BODY(iseq)->param.
flags.forwardable) {
6763 ISEQ_BODY(iseq)->param.size += 1;
6767 iseq_set_local_table(iseq, local_table_for_iseq, 0);
6768 scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
6770 if (keyword != NULL) {
6771 size_t keyword_start_index = keyword->bits_start - keyword->num;
6772 keyword->table = (
ID *)&ISEQ_BODY(iseq)->local_table[keyword_start_index];
6777 if (optionals_list && optionals_list->
size) {
6785 for (
size_t i = 0; i < optionals_list->
size; i++) {
6786 label = NEW_LABEL(location.
line);
6787 opt_table[i] = label;
6788 PUSH_LABEL(ret, label);
6789 pm_node_t *optional_node = optionals_list->
nodes[i];
6790 PM_COMPILE_NOT_POPPED(optional_node);
6794 label = NEW_LABEL(location.
line);
6795 opt_table[optionals_list->
size] = label;
6796 PUSH_LABEL(ret, label);
6798 body->
param.opt_table = (
const VALUE *) opt_table;
6801 if (keywords_list && keywords_list->
size) {
6802 size_t optional_index = 0;
6803 for (
size_t i = 0; i < keywords_list->
size; i++) {
6804 pm_node_t *keyword_parameter_node = keywords_list->
nodes[i];
6807 switch (PM_NODE_TYPE(keyword_parameter_node)) {
6808 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
6811 const pm_optional_keyword_parameter_node_t *cast = ((
const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
6813 pm_node_t *value = cast->
value;
6816 if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_CONTAINER_P(value)) {
6817 LABEL *end_label = NEW_LABEL(location.
line);
6819 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
6820 int kw_bits_idx = table_size - body->
param.keyword->bits_start;
6821 PUSH_INSN2(ret, location, checkkeyword,
INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1),
INT2FIX(optional_index));
6822 PUSH_INSNL(ret, location, branchif, end_label);
6824 PUSH_SETLOCAL(ret, location, index.index, index.level);
6825 PUSH_LABEL(ret, end_label);
6830 case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
6835 rb_bug(
"Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
6840 if (requireds_list && requireds_list->
size) {
6841 for (
size_t i = 0; i < requireds_list->
size; i++) {
6845 const pm_node_t *required = requireds_list->
nodes[i];
6847 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
6848 PUSH_GETLOCAL(ret, location, table_size - (
int)i, 0);
6849 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) required, ret, scope_node);
6854 if (posts_list && posts_list->
size) {
6855 for (
size_t i = 0; i < posts_list->
size; i++) {
6859 const pm_node_t *post = posts_list->
nodes[i];
6861 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
6862 PUSH_GETLOCAL(ret, location, table_size - body->
param.post_start - (
int) i, 0);
6863 pm_compile_destructured_param_writes(iseq, (
const pm_multi_target_node_t *) post, ret, scope_node);
6868 switch (body->type) {
6869 case ISEQ_TYPE_PLAIN: {
6870 RUBY_ASSERT(PM_NODE_TYPE_P(scope_node->ast_node, PM_INTERPOLATED_REGULAR_EXPRESSION_NODE));
6872 const pm_interpolated_regular_expression_node_t *cast = (
const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
6873 pm_compile_regexp_dynamic(iseq, (
const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6877 case ISEQ_TYPE_BLOCK: {
6878 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
6879 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
6880 const pm_node_location_t block_location = { .line = body->location.first_lineno, .node_id = scope_node->ast_node->
node_id };
6882 start->rescued = LABEL_RESCUE_BEG;
6883 end->rescued = LABEL_RESCUE_END;
6889 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
6890 pm_compile_for_node_index(iseq, ((
const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
6894 PUSH_INSN(ret, block_location, nop);
6895 PUSH_LABEL(ret, start);
6897 if (scope_node->body != NULL) {
6898 switch (PM_NODE_TYPE(scope_node->ast_node)) {
6899 case PM_POST_EXECUTION_NODE: {
6900 const pm_post_execution_node_t *cast = (
const pm_post_execution_node_t *) scope_node->ast_node;
6901 PUSH_INSN1(ret, block_location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6904 pm_scope_node_t next_scope_node;
6905 pm_scope_node_init((
const pm_node_t *) cast->statements, &next_scope_node, scope_node);
6907 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.
line);
6908 pm_scope_node_destroy(&next_scope_node);
6910 PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe,
INT2FIX(0), block);
6913 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
6914 const pm_interpolated_regular_expression_node_t *cast = (
const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
6915 pm_compile_regexp_dynamic(iseq, (
const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
6919 pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
6924 PUSH_INSN(ret, block_location, putnil);
6927 PUSH_LABEL(ret, end);
6929 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
6932 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
6933 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
6936 case ISEQ_TYPE_ENSURE: {
6937 const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->parser, scope_node->body) : location);
6938 iseq_set_exception_local_table(iseq);
6940 if (scope_node->body != NULL) {
6941 PM_COMPILE_POPPED((
const pm_node_t *) scope_node->body);
6944 PUSH_GETLOCAL(ret, statements_location, 1, 0);
6945 PUSH_INSN1(ret, statements_location,
throw,
INT2FIX(0));
6948 case ISEQ_TYPE_METHOD: {
6949 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
6952 if (scope_node->body) {
6953 PM_COMPILE((
const pm_node_t *) scope_node->body);
6956 PUSH_INSN(ret, location, putnil);
6959 ISEQ_COMPILE_DATA(iseq)->root_node = (
const void *) scope_node->body;
6962 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
6965 case ISEQ_TYPE_RESCUE: {
6966 iseq_set_exception_local_table(iseq);
6967 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
6968 LABEL *lab = NEW_LABEL(location.
line);
6969 LABEL *rescue_end = NEW_LABEL(location.
line);
6970 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
6972 PUSH_INSN1(ret, location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
6973 PUSH_INSNL(ret, location, branchif, lab);
6974 PUSH_INSNL(ret, location, jump, rescue_end);
6975 PUSH_LABEL(ret, lab);
6977 PM_COMPILE((
const pm_node_t *) scope_node->body);
6978 PUSH_INSN(ret, location, leave);
6979 PUSH_LABEL(ret, rescue_end);
6980 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
6983 PM_COMPILE((
const pm_node_t *) scope_node->ast_node);
6985 PUSH_INSN1(ret, location,
throw,
INT2FIX(0));
6990 if (scope_node->body) {
6991 PM_COMPILE((
const pm_node_t *) scope_node->body);
6994 PUSH_INSN(ret, location, putnil);
6999 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
7000 const pm_node_location_t end_location = PM_NODE_END_LOCATION(scope_node->parser, scope_node->ast_node);
7002 ISEQ_COMPILE_DATA(iseq)->last_line = end_location.
line;
7005 if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
7006 const pm_node_location_t location = { .line = ISEQ_COMPILE_DATA(iseq)->last_line, .node_id = scope_node->ast_node->
node_id };
7007 PUSH_INSN(ret, location, leave);
7012pm_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)
7016 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7020 VALUE operand =
ID2SYM(rb_intern3((
const char *) name_loc->
start, name_loc->
end - name_loc->
start, scope_node->encoding));
7021 PUSH_INSN1(ret, *location, putobject, operand);
7026 VALUE operand =
ID2SYM(rb_intern3((
const char *) name_loc->
start, name_loc->
end - name_loc->
start, scope_node->encoding));
7027 PUSH_INSN1(ret, *location, putobject, operand);
7030 PUSH_SEND(ret, *location, id_core_set_variable_alias,
INT2FIX(2));
7031 if (popped) PUSH_INSN(ret, *location, pop);
7035pm_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)
7037 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7038 PUSH_INSN1(ret, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
7039 PM_COMPILE_NOT_POPPED(node->
new_name);
7040 PM_COMPILE_NOT_POPPED(node->
old_name);
7042 PUSH_SEND(ret, *location, id_core_set_method_alias,
INT2FIX(3));
7043 if (popped) PUSH_INSN(ret, *location, pop);
7047pm_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)
7049 LABEL *end_label = NEW_LABEL(location->
line);
7051 PM_COMPILE_NOT_POPPED(node->
left);
7052 if (!popped) PUSH_INSN(ret, *location, dup);
7053 PUSH_INSNL(ret, *location, branchunless, end_label);
7055 if (!popped) PUSH_INSN(ret, *location, pop);
7056 PM_COMPILE(node->
right);
7057 PUSH_LABEL(ret, end_label);
7061pm_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)
7065 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
7070 if (elements->
size) {
7071 VALUE value = pm_static_literal_value(iseq, node, scope_node);
7072 PUSH_INSN1(ret, *location, duparray, value);
7075 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7093 const int max_new_array_size = 0x100;
7094 const unsigned int min_tmp_array_size = 0x40;
7096 int new_array_size = 0;
7097 bool first_chunk =
true;
7103 bool static_literal =
false;
7106#define FLUSH_CHUNK \
7107 if (new_array_size) { \
7108 if (first_chunk) PUSH_INSN1(ret, *location, newarray, INT2FIX(new_array_size)); \
7109 else PUSH_INSN1(ret, *location, pushtoarray, INT2FIX(new_array_size)); \
7110 first_chunk = false; \
7111 new_array_size = 0; \
7114 for (
size_t index = 0; index < elements->
size; index++) {
7115 const pm_node_t *element = elements->
nodes[index];
7117 if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
7120 const pm_splat_node_t *splat_element = (
const pm_splat_node_t *) element;
7122 PM_COMPILE_NOT_POPPED(splat_element->
expression);
7125 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
7126 PUSH_GETLOCAL(ret, *location, index.index, index.level);
7132 PUSH_INSN1(ret, *location, splatarray,
Qtrue);
7133 first_chunk =
false;
7136 PUSH_INSN(ret, *location, concattoarray);
7139 static_literal =
false;
7141 else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
7142 if (new_array_size == 0 && first_chunk) {
7143 PUSH_INSN1(ret, *location, newarray,
INT2FIX(0));
7144 first_chunk =
false;
7160 const pm_keyword_hash_node_t *keyword_hash = (
const pm_keyword_hash_node_t *) element;
7161 pm_compile_hash_elements(iseq, element, &keyword_hash->
elements, 0,
Qundef,
false, ret, scope_node);
7168 while (splats < keyword_hash->elements.
size && PM_NODE_TYPE_P(keyword_hash->
elements.
nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
7171 PUSH_INSN(ret, *location, pushtoarraykwsplat);
7178 PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) &&
7179 !PM_CONTAINER_P(element) &&
7181 ((index + min_tmp_array_size) < elements->
size)
7186 size_t right_index = index + 1;
7188 right_index < elements->size &&
7189 PM_NODE_FLAG_P(elements->
nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL) &&
7190 !PM_CONTAINER_P(elements->
nodes[right_index])
7193 size_t tmp_array_size = right_index - index;
7194 if (tmp_array_size >= min_tmp_array_size) {
7195 VALUE tmp_array = rb_ary_hidden_new(tmp_array_size);
7198 for (; tmp_array_size; tmp_array_size--)
7199 rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->
nodes[index++], scope_node));
7207 PUSH_INSN1(ret, *location, duparray, tmp_array);
7208 first_chunk =
false;
7211 PUSH_INSN1(ret, *location, putobject, tmp_array);
7212 PUSH_INSN(ret, *location, concattoarray);
7216 PM_COMPILE_NOT_POPPED(element);
7217 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7218 static_literal =
true;
7221 PM_COMPILE_NOT_POPPED(element);
7222 if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
7223 static_literal =
false;
7228 if (popped) PUSH_INSN(ret, *location, pop);
7234pm_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)
7236 unsigned long throw_flag = 0;
7238 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
7240 LABEL *splabel = NEW_LABEL(0);
7241 PUSH_LABEL(ret, splabel);
7242 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7245 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
7248 PUSH_INSN(ret, *location, putnil);
7251 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7252 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7253 PUSH_ADJUST_RESTORE(ret, splabel);
7254 if (!popped) PUSH_INSN(ret, *location, putnil);
7257 const rb_iseq_t *ip = iseq;
7260 if (!ISEQ_COMPILE_DATA(ip)) {
7265 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7266 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7268 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
7271 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
7272 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7276 ip = ISEQ_BODY(ip)->parent_iseq;
7282 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
7285 PUSH_INSN(ret, *location, putnil);
7288 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_BREAK));
7289 if (popped) PUSH_INSN(ret, *location, pop);
7294 COMPILE_ERROR(iseq, location->
line,
"Invalid break");
7299pm_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)
7301 ID method_id = pm_constant_id_lookup(scope_node, node->
name);
7307 const char *builtin_func;
7309 if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, node->
receiver, method_id)) != NULL) {
7310 pm_compile_builtin_function_call(iseq, ret, scope_node, node, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
7314 LABEL *start = NEW_LABEL(location.
line);
7315 if (node->
block) PUSH_LABEL(ret, start);
7317 switch (method_id) {
7319 if (pm_opt_str_freeze_p(iseq, node)) {
7320 VALUE value = parse_static_literal_string(iseq, scope_node, node->
receiver, &((
const pm_string_node_t * ) node->
receiver)->unescaped);
7321 const struct rb_callinfo *callinfo = new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE);
7322 PUSH_INSN2(ret, location, opt_str_uminus, value, callinfo);
7323 if (popped) PUSH_INSN(ret, location, pop);
7329 if (pm_opt_str_freeze_p(iseq, node)) {
7330 VALUE value = parse_static_literal_string(iseq, scope_node, node->
receiver, &((
const pm_string_node_t * ) node->
receiver)->unescaped);
7331 const struct rb_callinfo *callinfo = new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE);
7332 PUSH_INSN2(ret, location, opt_str_freeze, value, callinfo);
7333 if (popped) PUSH_INSN(ret, location, pop);
7339 if (pm_opt_aref_with_p(iseq, node)) {
7340 const pm_string_node_t *
string = (
const pm_string_node_t *) ((
const pm_arguments_node_t *) node->
arguments)->arguments.nodes[0];
7341 VALUE value = parse_static_literal_string(iseq, scope_node, (
const pm_node_t *)
string, &string->unescaped);
7343 PM_COMPILE_NOT_POPPED(node->
receiver);
7345 const struct rb_callinfo *callinfo = new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE);
7346 PUSH_INSN2(ret, location, opt_aref_with, value, callinfo);
7349 PUSH_INSN(ret, location, pop);
7357 if (pm_opt_aset_with_p(iseq, node)) {
7358 const pm_string_node_t *
string = (
const pm_string_node_t *) ((
const pm_arguments_node_t *) node->
arguments)->arguments.nodes[0];
7359 VALUE value = parse_static_literal_string(iseq, scope_node, (
const pm_node_t *)
string, &string->unescaped);
7361 PM_COMPILE_NOT_POPPED(node->
receiver);
7362 PM_COMPILE_NOT_POPPED(((
const pm_arguments_node_t *) node->
arguments)->arguments.nodes[1]);
7365 PUSH_INSN(ret, location, swap);
7366 PUSH_INSN1(ret, location, topn,
INT2FIX(1));
7369 const struct rb_callinfo *callinfo = new_callinfo(iseq, idASET, 2, 0, NULL, FALSE);
7370 PUSH_INSN2(ret, location, opt_aset_with, value, callinfo);
7371 PUSH_INSN(ret, location, pop);
7378 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
7379 PUSH_INSN(ret, location, putnil);
7383 PUSH_INSN(ret, location, putself);
7386 if (method_id == idCall && PM_NODE_TYPE_P(node->
receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
7387 const pm_local_variable_read_node_t *read_node_cast = (
const pm_local_variable_read_node_t *) node->
receiver;
7391 if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
7392 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)));
7395 PM_COMPILE_NOT_POPPED(node->
receiver);
7399 PM_COMPILE_NOT_POPPED(node->
receiver);
7403 pm_compile_call(iseq, node, ret, popped, scope_node, method_id, start);
7408pm_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)
7412 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
7413 flag = VM_CALL_FCALL;
7416 PM_COMPILE_NOT_POPPED(node->
receiver);
7418 LABEL *safe_label = NULL;
7419 if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
7420 safe_label = NEW_LABEL(location->
line);
7421 PUSH_INSN(ret, *location, dup);
7422 PUSH_INSNL(ret, *location, branchnil, safe_label);
7425 PUSH_INSN(ret, *location, dup);
7427 ID id_read_name = pm_constant_id_lookup(scope_node, node->
read_name);
7428 PUSH_SEND_WITH_FLAG(ret, *location, id_read_name,
INT2FIX(0),
INT2FIX(flag));
7430 PM_COMPILE_NOT_POPPED(node->
value);
7432 PUSH_SEND(ret, *location, id_operator,
INT2FIX(1));
7435 PUSH_INSN(ret, *location, swap);
7436 PUSH_INSN1(ret, *location, topn,
INT2FIX(1));
7439 ID id_write_name = pm_constant_id_lookup(scope_node, node->
write_name);
7440 PUSH_SEND_WITH_FLAG(ret, *location, id_write_name,
INT2FIX(1),
INT2FIX(flag));
7442 if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
7443 PUSH_INSN(ret, *location, pop);
7444 if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
7464pm_compile_case_node_dispatch(rb_iseq_t *iseq,
VALUE dispatch,
const pm_node_t *node, LABEL *label,
const pm_scope_node_t *scope_node)
7468 switch (PM_NODE_TYPE(node)) {
7469 case PM_FLOAT_NODE: {
7470 key = pm_static_literal_value(iseq, node, scope_node);
7474 key = (
FIXABLE(intptr) ?
LONG2FIX((
long) intptr) : rb_dbl2big(intptr));
7480 case PM_INTEGER_NODE:
7482 case PM_SOURCE_FILE_NODE:
7483 case PM_SOURCE_LINE_NODE:
7484 case PM_SYMBOL_NODE:
7486 key = pm_static_literal_value(iseq, node, scope_node);
7488 case PM_STRING_NODE: {
7489 const pm_string_node_t *cast = (
const pm_string_node_t *) node;
7490 key = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
7497 if (
NIL_P(rb_hash_lookup(dispatch, key))) {
7498 rb_hash_aset(dispatch, key, ((
VALUE) label) | 1);
7508pm_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)
7512 const pm_node_list_t *conditions = &cast->
conditions;
7517 DECL_ANCHOR(cond_seq);
7522 DECL_ANCHOR(body_seq);
7526 LABEL *end_label = NEW_LABEL(location.
line);
7534 rb_code_location_t case_location = { 0 };
7537 if (PM_BRANCH_COVERAGE_P(iseq)) {
7538 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7539 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location,
"case");
7545 for (
size_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7546 const pm_when_node_t *clause = (
const pm_when_node_t *) conditions->
nodes[clause_index];
7547 const pm_node_list_t *conditions = &clause->conditions;
7549 int clause_lineno = pm_node_line_number(parser, (
const pm_node_t *) clause);
7550 LABEL *label = NEW_LABEL(clause_lineno);
7551 PUSH_LABEL(body_seq, label);
7554 if (PM_BRANCH_COVERAGE_P(iseq)) {
7555 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));
7556 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7559 if (clause->statements != NULL) {
7560 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7563 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7566 PUSH_INSNL(body_seq, location, jump, end_label);
7571 for (
size_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7572 const pm_node_t *condition = conditions->
nodes[condition_index];
7574 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7576 PUSH_INSN(cond_seq, cond_location, putnil);
7577 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7578 PUSH_INSN1(cond_seq, cond_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
7579 PUSH_INSNL(cond_seq, cond_location, branchif, label);
7582 LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
7583 pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label,
false, scope_node);
7584 PUSH_LABEL(cond_seq, next_label);
7591 if (PM_BRANCH_COVERAGE_P(iseq)) {
7592 rb_code_location_t branch_location;
7595 branch_location = case_location;
7597 branch_location = pm_code_location(scope_node, (
const pm_node_t *) cast->
else_clause);
7602 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7607 pm_compile_node(iseq, (
const pm_node_t *) cast->
else_clause, cond_seq, popped, scope_node);
7610 PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
7615 PUSH_INSNL(cond_seq, location, jump, end_label);
7616 PUSH_SEQ(ret, cond_seq);
7621 rb_code_location_t case_location = { 0 };
7624 if (PM_BRANCH_COVERAGE_P(iseq)) {
7625 case_location = pm_code_location(scope_node, (
const pm_node_t *) cast);
7626 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location,
"case");
7631 LABEL *else_label = NEW_LABEL(location.
line);
7639 if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
7641 RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
7652 for (
size_t clause_index = 0; clause_index < conditions->
size; clause_index++) {
7653 const pm_when_node_t *clause = (
const pm_when_node_t *) conditions->
nodes[clause_index];
7654 pm_node_location_t clause_location = PM_NODE_START_LOCATION(parser, (
const pm_node_t *) clause);
7656 const pm_node_list_t *conditions = &clause->conditions;
7657 LABEL *label = NEW_LABEL(clause_location.
line);
7662 for (
size_t condition_index = 0; condition_index < conditions->
size; condition_index++) {
7663 const pm_node_t *condition = conditions->
nodes[condition_index];
7664 const pm_node_location_t condition_location = PM_NODE_START_LOCATION(parser, condition);
7669 if (dispatch !=
Qundef) {
7670 dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
7673 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
7674 PUSH_INSN(cond_seq, condition_location, dup);
7675 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7676 PUSH_INSN1(cond_seq, condition_location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
7679 if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
7680 const pm_string_node_t *
string = (
const pm_string_node_t *) condition;
7681 VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->
unescaped);
7682 PUSH_INSN1(cond_seq, condition_location, putobject, value);
7685 pm_compile_node(iseq, condition, cond_seq,
false, scope_node);
7688 PUSH_INSN1(cond_seq, condition_location, topn,
INT2FIX(1));
7689 PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
7692 PUSH_INSNL(cond_seq, condition_location, branchif, label);
7699 PUSH_LABEL(body_seq, label);
7700 PUSH_INSN(body_seq, clause_location, pop);
7703 if (PM_BRANCH_COVERAGE_P(iseq)) {
7704 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));
7705 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"when", branches);
7708 if (clause->statements != NULL) {
7709 pm_compile_node(iseq, (
const pm_node_t *) clause->statements, body_seq, popped, scope_node);
7712 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7715 PUSH_INSNL(body_seq, clause_location, jump, end_label);
7726 if (dispatch !=
Qundef) {
7727 PUSH_INSN(ret, location, dup);
7728 PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
7729 LABEL_REF(else_label);
7732 PUSH_SEQ(ret, cond_seq);
7736 PUSH_LABEL(ret, else_label);
7740 PUSH_INSN(ret, else_location, pop);
7743 if (PM_BRANCH_COVERAGE_P(iseq)) {
7745 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7748 PM_COMPILE((
const pm_node_t *) cast->
else_clause);
7749 PUSH_INSNL(ret, else_location, jump, end_label);
7752 PUSH_INSN(ret, location, pop);
7755 if (PM_BRANCH_COVERAGE_P(iseq)) {
7756 add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
7759 if (!popped) PUSH_INSN(ret, location, putnil);
7760 PUSH_INSNL(ret, location, jump, end_label);
7764 PUSH_SEQ(ret, body_seq);
7765 PUSH_LABEL(ret, end_label);
7769pm_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)
7774 DECL_ANCHOR(body_seq);
7779 DECL_ANCHOR(cond_seq);
7783 LABEL *end_label = NEW_LABEL(location->
line);
7788 LABEL *else_label = NEW_LABEL(location->
line);
7792 rb_code_location_t case_location = { 0 };
7796 if (PM_BRANCH_COVERAGE_P(iseq)) {
7797 case_location = pm_code_location(scope_node, (
const pm_node_t *) node);
7798 branches = decl_branch_base(iseq, PTR2NUM(node), &case_location,
"case");
7808 if (in_single_pattern) {
7809 PUSH_INSN(ret, *location, putnil);
7810 PUSH_INSN(ret, *location, putnil);
7811 PUSH_INSN1(ret, *location, putobject,
Qfalse);
7812 PUSH_INSN(ret, *location, putnil);
7816 PUSH_INSN(ret, *location, putnil);
7825 RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
7827 const pm_in_node_t *in_node = (
const pm_in_node_t *) condition;
7828 const pm_node_location_t in_location = PM_NODE_START_LOCATION(scope_node->parser, in_node);
7832 PUSH_INSN(body_seq, in_location, putnil);
7835 LABEL *body_label = NEW_LABEL(in_location.
line);
7836 PUSH_LABEL(body_seq, body_label);
7837 PUSH_INSN1(body_seq, in_location, adjuststack,
INT2FIX(in_single_pattern ? 6 : 2));
7840 if (PM_BRANCH_COVERAGE_P(iseq)) {
7841 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));
7842 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++,
"in", branches);
7846 PM_COMPILE_INTO_ANCHOR(body_seq, (
const pm_node_t *) in_node->
statements);
7849 PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
7852 PUSH_INSNL(body_seq, in_location, jump, end_label);
7853 LABEL *next_pattern_label = NEW_LABEL(pattern_location.
line);
7855 PUSH_INSN(cond_seq, pattern_location, dup);
7856 pm_compile_pattern(iseq, scope_node, in_node->
pattern, cond_seq, body_label, next_pattern_label, in_single_pattern,
false,
true, 2);
7857 PUSH_LABEL(cond_seq, next_pattern_label);
7858 LABEL_UNREMOVABLE(next_pattern_label);
7865 const pm_else_node_t *else_node = node->
else_clause;
7867 PUSH_LABEL(cond_seq, else_label);
7868 PUSH_INSN(cond_seq, *location, pop);
7869 PUSH_INSN(cond_seq, *location, pop);
7872 if (PM_BRANCH_COVERAGE_P(iseq)) {
7873 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));
7874 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id,
"else", branches);
7877 PM_COMPILE_INTO_ANCHOR(cond_seq, (
const pm_node_t *) else_node);
7878 PUSH_INSNL(cond_seq, *location, jump, end_label);
7879 PUSH_INSN(cond_seq, *location, putnil);
7880 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7885 PUSH_LABEL(cond_seq, else_label);
7888 add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id,
"else", branches);
7890 if (in_single_pattern) {
7891 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, cond_seq, end_label, popped);
7894 PUSH_INSN1(cond_seq, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
7896 PUSH_INSN1(cond_seq, *location, topn,
INT2FIX(2));
7897 PUSH_SEND(cond_seq, *location, id_core_raise,
INT2FIX(2));
7899 PUSH_INSN1(cond_seq, *location, adjuststack,
INT2FIX(3));
7900 if (!popped) PUSH_INSN(cond_seq, *location, putnil);
7901 PUSH_INSNL(cond_seq, *location, jump, end_label);
7902 PUSH_INSN1(cond_seq, *location, dupn,
INT2FIX(1));
7903 if (popped) PUSH_INSN(cond_seq, *location, putnil);
7910 PUSH_SEQ(ret, cond_seq);
7911 PUSH_SEQ(ret, body_seq);
7912 PUSH_LABEL(ret, end_label);
7916pm_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)
7918 const rb_iseq_t *block = NULL;
7919 const rb_iseq_t *previous_block = NULL;
7920 LABEL *retry_label = NULL;
7921 LABEL *retry_end_l = NULL;
7923 if (node->
block != NULL) {
7924 previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
7925 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
7927 retry_label = NEW_LABEL(location->
line);
7928 retry_end_l = NEW_LABEL(location->
line);
7930 PUSH_LABEL(ret, retry_label);
7933 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
7936 PUSH_INSN(ret, *location, putself);
7937 int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
7939 if (node->
block != NULL) {
7940 pm_scope_node_t next_scope_node;
7941 pm_scope_node_init((
const pm_node_t *) node->
block, &next_scope_node, scope_node);
7943 ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
7944 pm_scope_node_destroy(&next_scope_node);
7951 const rb_iseq_t *local_iseq = body->local_iseq;
7955 int depth = get_lvar_level(iseq);
7957 if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
param.flags.forwardable) {
7958 flag |= VM_CALL_FORWARDING;
7959 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
7960 PUSH_GETLOCAL(ret, *location, mult_local.index, mult_local.level);
7962 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, 0, flag, NULL, block != NULL);
7963 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, block);
7965 if (popped) PUSH_INSN(ret, *location, pop);
7967 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
7972 if (local_body->
param.flags.has_lead) {
7974 for (
int i = 0; i < local_body->
param.lead_num; i++) {
7975 int idx = local_body->local_table_size - i;
7976 PUSH_GETLOCAL(args, *location, idx, depth);
7978 argc += local_body->
param.lead_num;
7981 if (local_body->
param.flags.has_opt) {
7983 for (
int j = 0; j < local_body->
param.opt_num; j++) {
7984 int idx = local_body->local_table_size - (argc + j);
7985 PUSH_GETLOCAL(args, *location, idx, depth);
7987 argc += local_body->
param.opt_num;
7990 if (local_body->
param.flags.has_rest) {
7992 int idx = local_body->local_table_size - local_body->
param.rest_start;
7993 PUSH_GETLOCAL(args, *location, idx, depth);
7994 PUSH_INSN1(args, *location, splatarray,
Qfalse);
7996 argc = local_body->
param.rest_start + 1;
7997 flag |= VM_CALL_ARGS_SPLAT;
8000 if (local_body->
param.flags.has_post) {
8002 int post_len = local_body->
param.post_num;
8003 int post_start = local_body->
param.post_start;
8006 for (; j < post_len; j++) {
8007 int idx = local_body->local_table_size - (post_start + j);
8008 PUSH_GETLOCAL(args, *location, idx, depth);
8011 if (local_body->
param.flags.has_rest) {
8013 PUSH_INSN1(args, *location, newarray,
INT2FIX(j));
8014 PUSH_INSN(args, *location, concatarray);
8017 argc = post_len + post_start;
8021 const struct rb_iseq_param_keyword *
const local_keyword = local_body->
param.keyword;
8022 if (local_body->
param.flags.has_kw) {
8023 int local_size = local_body->local_table_size;
8026 PUSH_INSN1(args, *location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8028 if (local_body->
param.flags.has_kwrest) {
8029 int idx = local_body->local_table_size - local_keyword->rest_start;
8030 PUSH_GETLOCAL(args, *location, idx, depth);
8032 PUSH_SEND(args, *location, rb_intern(
"dup"),
INT2FIX(0));
8035 PUSH_INSN1(args, *location, newhash,
INT2FIX(0));
8038 for (; i < local_keyword->num; ++i) {
8039 ID id = local_keyword->table[i];
8040 int idx = local_size - get_local_var_idx(local_iseq,
id);
8044 PUSH_INSN1(args, *location, putobject, operand);
8047 PUSH_GETLOCAL(args, *location, idx, depth);
8050 PUSH_SEND(args, *location, id_core_hash_merge_ptr,
INT2FIX(i * 2 + 1));
8051 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
8053 else if (local_body->
param.flags.has_kwrest) {
8054 int idx = local_body->local_table_size - local_keyword->rest_start;
8055 PUSH_GETLOCAL(args, *location, idx, depth);
8057 flag |= VM_CALL_KW_SPLAT;
8060 PUSH_SEQ(ret, args);
8063 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flag, NULL, block != NULL);
8064 PUSH_INSN2(ret, *location, invokesuper, callinfo, block);
8067 if (node->
block != NULL) {
8068 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8069 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
8070 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8073 if (popped) PUSH_INSN(ret, *location, pop);
8077pm_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)
8079 LABEL *matched_label = NEW_LABEL(location->
line);
8080 LABEL *unmatched_label = NEW_LABEL(location->
line);
8081 LABEL *done_label = NEW_LABEL(location->
line);
8085 PUSH_INSN(ret, *location, putnil);
8086 PUSH_INSN(ret, *location, putnil);
8087 PUSH_INSN1(ret, *location, putobject,
Qfalse);
8088 PUSH_INSN(ret, *location, putnil);
8089 PUSH_INSN(ret, *location, putnil);
8093 PM_COMPILE_NOT_POPPED(node->
value);
8097 PUSH_INSN(ret, *location, dup);
8104 pm_compile_pattern(iseq, scope_node, node->
pattern, ret, matched_label, unmatched_label,
true,
false,
true, 2);
8109 PUSH_LABEL(ret, unmatched_label);
8110 pm_compile_pattern_error_handler(iseq, scope_node, (
const pm_node_t *) node, ret, done_label, popped);
8114 PUSH_LABEL(ret, matched_label);
8115 PUSH_INSN1(ret, *location, adjuststack,
INT2FIX(6));
8116 if (!popped) PUSH_INSN(ret, *location, putnil);
8117 PUSH_INSNL(ret, *location, jump, done_label);
8119 PUSH_LABEL(ret, done_label);
8123pm_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)
8125 LABEL *fail_label = NEW_LABEL(location->
line);
8126 LABEL *end_label = NEW_LABEL(location->
line);
8130 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
call);
8137 PUSH_INSN1(ret, *location, getglobal, operand);
8140 PUSH_INSN(ret, *location, dup);
8141 PUSH_INSNL(ret, *location, branchunless, fail_label);
8147 if (targets_count == 1) {
8149 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8151 const pm_local_variable_target_node_t *local_target = (
const pm_local_variable_target_node_t *) target;
8152 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->
name, local_target->
depth);
8156 PUSH_INSN1(ret, *location, putobject, operand);
8159 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8160 PUSH_LABEL(ret, fail_label);
8161 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8162 if (popped) PUSH_INSN(ret, *location, pop);
8166 DECL_ANCHOR(fail_anchor);
8170 for (
size_t targets_index = 0; targets_index < targets_count; targets_index++) {
8171 const pm_node_t *target = node->
targets.
nodes[targets_index];
8172 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
8174 const pm_local_variable_target_node_t *local_target = (
const pm_local_variable_target_node_t *) target;
8175 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->
name, local_target->
depth);
8177 if (((
size_t) targets_index) < (targets_count - 1)) {
8178 PUSH_INSN(ret, *location, dup);
8183 PUSH_INSN1(ret, *location, putobject, operand);
8186 PUSH_SEND(ret, *location, idAREF,
INT2FIX(1));
8187 PUSH_SETLOCAL(ret, *location, index.index, index.level);
8189 PUSH_INSN(fail_anchor, *location, putnil);
8190 PUSH_SETLOCAL(fail_anchor, *location, index.index, index.level);
8194 PUSH_INSNL(ret, *location, jump, end_label);
8198 PUSH_LABEL(ret, fail_label);
8199 PUSH_INSN(ret, *location, pop);
8200 PUSH_SEQ(ret, fail_anchor);
8203 PUSH_LABEL(ret, end_label);
8204 if (popped) PUSH_INSN(ret, *location, pop);
8208pm_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)
8210 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
8211 LABEL *splabel = NEW_LABEL(0);
8212 PUSH_LABEL(ret, splabel);
8215 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
8218 PUSH_INSN(ret, *location, putnil);
8220 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8222 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8223 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8225 PUSH_ADJUST_RESTORE(ret, splabel);
8226 if (!popped) PUSH_INSN(ret, *location, putnil);
8228 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
8229 LABEL *splabel = NEW_LABEL(0);
8231 PUSH_LABEL(ret, splabel);
8232 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8235 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
8238 PUSH_INSN(ret, *location, putnil);
8241 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8242 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
8243 PUSH_ADJUST_RESTORE(ret, splabel);
8244 splabel->unremovable = FALSE;
8246 if (!popped) PUSH_INSN(ret, *location, putnil);
8249 const rb_iseq_t *ip = iseq;
8250 unsigned long throw_flag = 0;
8253 if (!ISEQ_COMPILE_DATA(ip)) {
8258 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
8259 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8263 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8266 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8267 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8271 ip = ISEQ_BODY(ip)->parent_iseq;
8276 PM_COMPILE_NOT_POPPED((
const pm_node_t *) node->
arguments);
8279 PUSH_INSN(ret, *location, putnil);
8282 PUSH_INSN1(ret, *location,
throw,
INT2FIX(throw_flag | TAG_NEXT));
8283 if (popped) PUSH_INSN(ret, *location, pop);
8286 COMPILE_ERROR(iseq, location->
line,
"Invalid next");
8292pm_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)
8294 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
8295 LABEL *splabel = NEW_LABEL(0);
8297 PUSH_LABEL(ret, splabel);
8298 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
8299 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8301 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
8302 PUSH_ADJUST_RESTORE(ret, splabel);
8303 if (!popped) PUSH_INSN(ret, *location, putnil);
8305 else if (ISEQ_BODY(iseq)->
type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
8306 LABEL *splabel = NEW_LABEL(0);
8308 PUSH_LABEL(ret, splabel);
8309 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
8310 PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
8312 PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
8313 PUSH_ADJUST_RESTORE(ret, splabel);
8314 if (!popped) PUSH_INSN(ret, *location, putnil);
8317 const rb_iseq_t *ip = iseq;
8320 if (!ISEQ_COMPILE_DATA(ip)) {
8325 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
8328 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_BLOCK) {
8331 else if (ISEQ_BODY(ip)->
type == ISEQ_TYPE_EVAL) {
8332 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8336 ip = ISEQ_BODY(ip)->parent_iseq;
8340 PUSH_INSN(ret, *location, putnil);
8341 PUSH_INSN1(ret, *location,
throw,
INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
8342 if (popped) PUSH_INSN(ret, *location, pop);
8345 COMPILE_ERROR(iseq, location->
line,
"Invalid redo");
8351pm_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)
8353 iseq_set_exception_local_table(iseq);
8357 LABEL *exception_match_label = NEW_LABEL(location->
line);
8358 LABEL *rescue_end_label = NEW_LABEL(location->
line);
8366 const pm_node_list_t *exceptions = &node->
exceptions;
8368 if (exceptions->
size > 0) {
8369 for (
size_t index = 0; index < exceptions->
size; index++) {
8370 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8371 PM_COMPILE(exceptions->
nodes[index]);
8372 int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
8373 if (PM_NODE_TYPE_P(exceptions->
nodes[index], PM_SPLAT_NODE)) {
8374 checkmatch_flags |= VM_CHECKMATCH_ARRAY;
8376 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(checkmatch_flags));
8377 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8381 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8383 PUSH_INSN1(ret, *location, checkmatch,
INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8384 PUSH_INSNL(ret, *location, branchif, exception_match_label);
8389 PUSH_INSNL(ret, *location, jump, rescue_end_label);
8394 PUSH_LABEL(ret, exception_match_label);
8401 DECL_ANCHOR(writes);
8402 DECL_ANCHOR(cleanup);
8404 pm_compile_target_node(iseq, node->
reference, ret, writes, cleanup, scope_node, NULL);
8405 PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
8407 PUSH_SEQ(ret, writes);
8408 PUSH_SEQ(ret, cleanup);
8418 LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
8419 ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
8421 PM_COMPILE((
const pm_node_t *) node->
statements);
8424 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
8427 PUSH_INSN(ret, *location, putnil);
8430 PUSH_INSN(ret, *location, leave);
8436 PUSH_LABEL(ret, rescue_end_label);
8438 PM_COMPILE((
const pm_node_t *) node->
subsequent);
8441 PUSH_GETLOCAL(ret, *location, 1, 0);
8446pm_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)
8448 const pm_arguments_node_t *arguments = node->
arguments;
8449 enum rb_iseq_type
type = ISEQ_BODY(iseq)->type;
8452 const rb_iseq_t *parent_iseq = iseq;
8453 enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
8454 while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
8455 if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq))
break;
8456 parent_type = ISEQ_BODY(parent_iseq)->type;
8459 switch (parent_type) {
8461 case ISEQ_TYPE_MAIN:
8463 rb_warn(
"argument of top-level return is ignored");
8465 if (parent_iseq == iseq) {
8466 type = ISEQ_TYPE_METHOD;
8473 if (
type == ISEQ_TYPE_METHOD) {
8474 splabel = NEW_LABEL(0);
8475 PUSH_LABEL(ret, splabel);
8476 PUSH_ADJUST(ret, *location, 0);
8479 if (arguments != NULL) {
8480 PM_COMPILE_NOT_POPPED((
const pm_node_t *) arguments);
8483 PUSH_INSN(ret, *location, putnil);
8486 if (
type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
8487 pm_add_ensure_iseq(ret, iseq, 1, scope_node);
8489 PUSH_INSN(ret, *location, leave);
8490 PUSH_ADJUST_RESTORE(ret, splabel);
8491 if (!popped) PUSH_INSN(ret, *location, putnil);
8494 PUSH_INSN1(ret, *location,
throw,
INT2FIX(TAG_RETURN));
8495 if (popped) PUSH_INSN(ret, *location, pop);
8500pm_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)
8504 LABEL *retry_label = NEW_LABEL(location->
line);
8505 LABEL *retry_end_l = NEW_LABEL(location->
line);
8507 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8508 const rb_iseq_t *current_block;
8509 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
8511 PUSH_LABEL(ret, retry_label);
8512 PUSH_INSN(ret, *location, putself);
8516 int argc = pm_setup_args(node->
arguments, node->
block, &flags, &keywords, iseq, ret, scope_node, location);
8517 bool is_forwardable = (node->
arguments != NULL) && PM_NODE_FLAG_P(node->
arguments, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING);
8518 flags |= VM_CALL_SUPER | VM_CALL_FCALL;
8520 if (node->
block && PM_NODE_TYPE_P(node->
block, PM_BLOCK_NODE)) {
8521 pm_scope_node_t next_scope_node;
8522 pm_scope_node_init(node->
block, &next_scope_node, scope_node);
8524 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->
line);
8525 pm_scope_node_destroy(&next_scope_node);
8529 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8532 if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
8533 PUSH_INSN(args, *location, splatkw);
8536 PUSH_SEQ(ret, args);
8537 if (is_forwardable && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.
flags.forwardable) {
8538 flags |= VM_CALL_FORWARDING;
8541 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8542 PUSH_INSN2(ret, *location, invokesuperforward, callinfo, current_block);
8547 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
8548 PUSH_INSN2(ret, *location, invokesuper, callinfo, current_block);
8553 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8555 if (popped) PUSH_INSN(ret, *location, pop);
8556 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8557 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
8561pm_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)
8563 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->
type) {
8565 case ISEQ_TYPE_MAIN:
8566 case ISEQ_TYPE_CLASS:
8567 COMPILE_ERROR(iseq, location->
line,
"Invalid yield");
8577 argc = pm_setup_args(node->
arguments, NULL, &flags, &keywords, iseq, ret, scope_node, location);
8580 const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, FALSE);
8581 PUSH_INSN1(ret, *location, invokeblock, callinfo);
8583 iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
8584 if (popped) PUSH_INSN(ret, *location, pop);
8587 for (
const rb_iseq_t *tmp_iseq = iseq; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++) {
8588 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
8591 if (level > 0) access_outer_variables(iseq, level, rb_intern(
"yield"),
true);
8605pm_compile_node(rb_iseq_t *iseq,
const pm_node_t *node, LINK_ANCHOR *
const ret,
bool popped, pm_scope_node_t *scope_node)
8609 int lineno = (int) location.
line;
8611 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)) {
8616 lineno = (int) PM_NODE_START_LINE_COLUMN(parser, ((
const pm_begin_node_t *) node)->rescue_clause).line;
8619 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
8625 ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
8626 if (lineno > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
8627 event |= RUBY_EVENT_COVERAGE_LINE;
8629 PUSH_TRACE(ret, event);
8632 switch (PM_NODE_TYPE(node)) {
8633 case PM_ALIAS_GLOBAL_VARIABLE_NODE:
8636 pm_compile_alias_global_variable_node(iseq, (
const pm_alias_global_variable_node_t *) node, &location, ret, popped, scope_node);
8638 case PM_ALIAS_METHOD_NODE:
8641 pm_compile_alias_method_node(iseq, (
const pm_alias_method_node_t *) node, &location, ret, popped, scope_node);
8646 pm_compile_and_node(iseq, (
const pm_and_node_t *) node, &location, ret, popped, scope_node);
8648 case PM_ARGUMENTS_NODE: {
8655 const pm_arguments_node_t *cast = (
const pm_arguments_node_t *) node;
8656 const pm_node_list_t *elements = &cast->
arguments;
8658 if (elements->
size == 1) {
8661 PM_COMPILE(elements->
nodes[0]);
8664 pm_compile_array_node(iseq, (
const pm_node_t *) cast, elements, &location, ret, popped, scope_node);
8668 case PM_ARRAY_NODE: {
8671 const pm_array_node_t *cast = (
const pm_array_node_t *) node;
8672 pm_compile_array_node(iseq, (
const pm_node_t *) cast, &cast->elements, &location, ret, popped, scope_node);
8675 case PM_ASSOC_NODE: {
8681 const pm_assoc_node_t *cast = (
const pm_assoc_node_t *) node;
8683 PM_COMPILE(cast->
key);
8684 PM_COMPILE(cast->
value);
8688 case PM_ASSOC_SPLAT_NODE: {
8694 const pm_assoc_splat_node_t *cast = (
const pm_assoc_splat_node_t *) node;
8696 if (cast->
value != NULL) {
8697 PM_COMPILE(cast->
value);
8700 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
8701 PUSH_GETLOCAL(ret, location, index.index, index.level);
8706 case PM_BACK_REFERENCE_READ_NODE: {
8710 const pm_back_reference_read_node_t *cast = (
const pm_back_reference_read_node_t *) node;
8711 VALUE backref = pm_compile_back_reference_ref(cast);
8713 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), backref);
8717 case PM_BEGIN_NODE: {
8720 const pm_begin_node_t *cast = (
const pm_begin_node_t *) node;
8725 pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
8729 pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
8735 PM_COMPILE((
const pm_node_t *) cast->
statements);
8738 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
8743 case PM_BLOCK_ARGUMENT_NODE: {
8746 const pm_block_argument_node_t *cast = (
const pm_block_argument_node_t *) node;
8753 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
8754 PUSH_INSN2(ret, location, getblockparamproxy,
INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1),
INT2FIX(local_index.level));
8764 pm_compile_break_node(iseq, (
const pm_break_node_t *) node, &location, ret, popped, scope_node);
8775 pm_compile_call_node(iseq, (
const pm_call_node_t *) node, ret, popped, scope_node);
8777 case PM_CALL_AND_WRITE_NODE: {
8780 const pm_call_and_write_node_t *cast = (
const pm_call_and_write_node_t *) node;
8781 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);
8784 case PM_CALL_OR_WRITE_NODE: {
8787 const pm_call_or_write_node_t *cast = (
const pm_call_or_write_node_t *) node;
8788 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);
8791 case PM_CALL_OPERATOR_WRITE_NODE:
8801 pm_compile_call_operator_write_node(iseq, (
const pm_call_operator_write_node_t *) node, &location, ret, popped, scope_node);
8806 pm_compile_case_node(iseq, (
const pm_case_node_t *) node, &location, ret, popped, scope_node);
8808 case PM_CASE_MATCH_NODE:
8817 pm_compile_case_match_node(iseq, (
const pm_case_match_node_t *) node, &location, ret, popped, scope_node);
8819 case PM_CLASS_NODE: {
8822 const pm_class_node_t *cast = (
const pm_class_node_t *) node;
8824 ID class_id = pm_constant_id_lookup(scope_node, cast->
name);
8825 VALUE class_name =
rb_str_freeze(rb_sprintf(
"<class:%"PRIsVALUE
">", rb_id2str(class_id)));
8827 pm_scope_node_t next_scope_node;
8828 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
8830 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, location.
line);
8831 pm_scope_node_destroy(&next_scope_node);
8834 const int flags = VM_DEFINECLASS_TYPE_CLASS |
8835 (cast->
superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
8836 pm_compile_class_path(iseq, cast->
constant_path, &location, ret,
false, scope_node);
8842 PUSH_INSN(ret, location, putnil);
8847 PUSH_INSN3(ret, location, defineclass, operand, class_iseq,
INT2FIX(flags));
8851 if (popped) PUSH_INSN(ret, location, pop);
8854 case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
8857 const pm_class_variable_and_write_node_t *cast = (
const pm_class_variable_and_write_node_t *) node;
8858 LABEL *end_label = NEW_LABEL(location.
line);
8860 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8863 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8864 if (!popped) PUSH_INSN(ret, location, dup);
8866 PUSH_INSNL(ret, location, branchunless, end_label);
8867 if (!popped) PUSH_INSN(ret, location, pop);
8869 PM_COMPILE_NOT_POPPED(cast->
value);
8870 if (!popped) PUSH_INSN(ret, location, dup);
8872 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8873 PUSH_LABEL(ret, end_label);
8877 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
8880 const pm_class_variable_operator_write_node_t *cast = (
const pm_class_variable_operator_write_node_t *) node;
8882 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8885 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8886 PM_COMPILE_NOT_POPPED(cast->
value);
8889 int flags = VM_CALL_ARGS_SIMPLE;
8890 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
8892 if (!popped) PUSH_INSN(ret, location, dup);
8893 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8897 case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
8900 const pm_class_variable_or_write_node_t *cast = (
const pm_class_variable_or_write_node_t *) node;
8901 LABEL *end_label = NEW_LABEL(location.
line);
8902 LABEL *start_label = NEW_LABEL(location.
line);
8904 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
8907 PUSH_INSN(ret, location, putnil);
8908 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_CVAR), name,
Qtrue);
8909 PUSH_INSNL(ret, location, branchunless, start_label);
8911 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
8912 if (!popped) PUSH_INSN(ret, location, dup);
8914 PUSH_INSNL(ret, location, branchif, end_label);
8915 if (!popped) PUSH_INSN(ret, location, pop);
8917 PUSH_LABEL(ret, start_label);
8918 PM_COMPILE_NOT_POPPED(cast->
value);
8919 if (!popped) PUSH_INSN(ret, location, dup);
8921 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
8922 PUSH_LABEL(ret, end_label);
8926 case PM_CLASS_VARIABLE_READ_NODE: {
8930 const pm_class_variable_read_node_t *cast = (
const pm_class_variable_read_node_t *) node;
8931 ID name = pm_constant_id_lookup(scope_node, cast->
name);
8932 PUSH_INSN2(ret, location, getclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
8936 case PM_CLASS_VARIABLE_WRITE_NODE: {
8939 const pm_class_variable_write_node_t *cast = (
const pm_class_variable_write_node_t *) node;
8940 PM_COMPILE_NOT_POPPED(cast->
value);
8941 if (!popped) PUSH_INSN(ret, location, dup);
8943 ID name = pm_constant_id_lookup(scope_node, cast->
name);
8944 PUSH_INSN2(ret, location, setclassvariable,
ID2SYM(name), get_cvar_ic_value(iseq, name));
8948 case PM_CONSTANT_PATH_NODE: {
8953 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) !=
Qnil)) {
8954 ISEQ_BODY(iseq)->ic_size++;
8955 PUSH_INSN1(ret, location, opt_getconstant_path, parts);
8958 DECL_ANCHOR(prefix);
8961 pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
8962 if (LIST_INSN_SIZE_ZERO(prefix)) {
8963 PUSH_INSN(ret, location, putnil);
8966 PUSH_SEQ(ret, prefix);
8969 PUSH_SEQ(ret, body);
8972 if (popped) PUSH_INSN(ret, location, pop);
8975 case PM_CONSTANT_PATH_AND_WRITE_NODE: {
8978 const pm_constant_path_and_write_node_t *cast = (
const pm_constant_path_and_write_node_t *) node;
8979 pm_compile_constant_path_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8982 case PM_CONSTANT_PATH_OR_WRITE_NODE: {
8985 const pm_constant_path_or_write_node_t *cast = (
const pm_constant_path_or_write_node_t *) node;
8986 pm_compile_constant_path_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8989 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
8992 const pm_constant_path_operator_write_node_t *cast = (
const pm_constant_path_operator_write_node_t *) node;
8993 pm_compile_constant_path_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
8996 case PM_CONSTANT_PATH_WRITE_NODE: {
8999 const pm_constant_path_write_node_t *cast = (
const pm_constant_path_write_node_t *) node;
9000 pm_compile_constant_path_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9003 case PM_CONSTANT_READ_NODE: {
9006 const pm_constant_read_node_t *cast = (
const pm_constant_read_node_t *) node;
9009 pm_compile_constant_read(iseq, name, &cast->
base.
location, location.
node_id, ret, scope_node);
9010 if (popped) PUSH_INSN(ret, location, pop);
9014 case PM_CONSTANT_AND_WRITE_NODE: {
9017 const pm_constant_and_write_node_t *cast = (
const pm_constant_and_write_node_t *) node;
9018 pm_compile_constant_and_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9021 case PM_CONSTANT_OR_WRITE_NODE: {
9024 const pm_constant_or_write_node_t *cast = (
const pm_constant_or_write_node_t *) node;
9025 pm_compile_constant_or_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9028 case PM_CONSTANT_OPERATOR_WRITE_NODE: {
9031 const pm_constant_operator_write_node_t *cast = (
const pm_constant_operator_write_node_t *) node;
9032 pm_compile_constant_operator_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9035 case PM_CONSTANT_WRITE_NODE: {
9038 const pm_constant_write_node_t *cast = (
const pm_constant_write_node_t *) node;
9039 pm_compile_constant_write_node(iseq, cast, 0, &location, ret, popped, scope_node);
9048 const pm_def_node_t *cast = (
const pm_def_node_t *) node;
9049 ID method_name = pm_constant_id_lookup(scope_node, cast->
name);
9051 pm_scope_node_t next_scope_node;
9052 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9054 rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, location.
line);
9055 pm_scope_node_destroy(&next_scope_node);
9058 PM_COMPILE_NOT_POPPED(cast->
receiver);
9059 PUSH_INSN2(ret, location, definesmethod,
ID2SYM(method_name), method_iseq);
9062 PUSH_INSN2(ret, location, definemethod,
ID2SYM(method_name), method_iseq);
9067 PUSH_INSN1(ret, location, putobject,
ID2SYM(method_name));
9072 case PM_DEFINED_NODE: {
9075 const pm_defined_node_t *cast = (
const pm_defined_node_t *) node;
9076 pm_compile_defined_expr(iseq, cast->
value, &location, ret, popped, scope_node,
false);
9079 case PM_EMBEDDED_STATEMENTS_NODE: {
9082 const pm_embedded_statements_node_t *cast = (
const pm_embedded_statements_node_t *) node;
9085 PM_COMPILE((
const pm_node_t *) (cast->
statements));
9088 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9091 if (popped) PUSH_INSN(ret, location, pop);
9094 case PM_EMBEDDED_VARIABLE_NODE: {
9097 const pm_embedded_variable_node_t *cast = (
const pm_embedded_variable_node_t *) node;
9101 case PM_FALSE_NODE: {
9105 PUSH_INSN1(ret, location, putobject,
Qfalse);
9109 case PM_ENSURE_NODE: {
9110 const pm_ensure_node_t *cast = (
const pm_ensure_node_t *) node;
9113 PM_COMPILE((
const pm_node_t *) cast->
statements);
9118 case PM_ELSE_NODE: {
9121 const pm_else_node_t *cast = (
const pm_else_node_t *) node;
9124 PM_COMPILE((
const pm_node_t *) cast->
statements);
9127 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
9132 case PM_FLIP_FLOP_NODE: {
9135 const pm_flip_flop_node_t *cast = (
const pm_flip_flop_node_t *) node;
9137 LABEL *final_label = NEW_LABEL(location.
line);
9138 LABEL *then_label = NEW_LABEL(location.
line);
9139 LABEL *else_label = NEW_LABEL(location.
line);
9141 pm_compile_flip_flop(cast, else_label, then_label, iseq, location.
line, ret, popped, scope_node);
9143 PUSH_LABEL(ret, then_label);
9144 PUSH_INSN1(ret, location, putobject,
Qtrue);
9145 PUSH_INSNL(ret, location, jump, final_label);
9146 PUSH_LABEL(ret, else_label);
9147 PUSH_INSN1(ret, location, putobject,
Qfalse);
9148 PUSH_LABEL(ret, final_label);
9152 case PM_FLOAT_NODE: {
9156 VALUE operand = parse_float((
const pm_float_node_t *) node);
9157 PUSH_INSN1(ret, location, putobject, operand);
9164 const pm_for_node_t *cast = (
const pm_for_node_t *) node;
9166 LABEL *retry_label = NEW_LABEL(location.
line);
9167 LABEL *retry_end_l = NEW_LABEL(location.
line);
9170 PUSH_LABEL(ret, retry_label);
9175 pm_scope_node_t next_scope_node;
9176 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9178 const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.
line);
9179 pm_scope_node_destroy(&next_scope_node);
9181 const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
9182 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
9186 PUSH_SEND_WITH_BLOCK(ret, location, idEach,
INT2FIX(0), child_iseq);
9187 pm_compile_retry_end_label(iseq, ret, retry_end_l);
9189 if (popped) PUSH_INSN(ret, location, pop);
9190 ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
9191 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
9194 case PM_FORWARDING_ARGUMENTS_NODE:
9195 rb_bug(
"Cannot compile a ForwardingArgumentsNode directly\n");
9197 case PM_FORWARDING_SUPER_NODE:
9203 pm_compile_forwarding_super_node(iseq, (
const pm_forwarding_super_node_t *) node, &location, ret, popped, scope_node);
9205 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
9208 const pm_global_variable_and_write_node_t *cast = (
const pm_global_variable_and_write_node_t *) node;
9209 LABEL *end_label = NEW_LABEL(location.
line);
9212 PUSH_INSN1(ret, location, getglobal, name);
9213 if (!popped) PUSH_INSN(ret, location, dup);
9215 PUSH_INSNL(ret, location, branchunless, end_label);
9216 if (!popped) PUSH_INSN(ret, location, pop);
9218 PM_COMPILE_NOT_POPPED(cast->
value);
9219 if (!popped) PUSH_INSN(ret, location, dup);
9221 PUSH_INSN1(ret, location, setglobal, name);
9222 PUSH_LABEL(ret, end_label);
9226 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
9229 const pm_global_variable_operator_write_node_t *cast = (
const pm_global_variable_operator_write_node_t *) node;
9232 PUSH_INSN1(ret, location, getglobal, name);
9233 PM_COMPILE_NOT_POPPED(cast->
value);
9236 int flags = VM_CALL_ARGS_SIMPLE;
9237 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9239 if (!popped) PUSH_INSN(ret, location, dup);
9240 PUSH_INSN1(ret, location, setglobal, name);
9244 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
9247 const pm_global_variable_or_write_node_t *cast = (
const pm_global_variable_or_write_node_t *) node;
9248 LABEL *set_label = NEW_LABEL(location.
line);
9249 LABEL *end_label = NEW_LABEL(location.
line);
9251 PUSH_INSN(ret, location, putnil);
9254 PUSH_INSN3(ret, location, defined,
INT2FIX(DEFINED_GVAR), name,
Qtrue);
9255 PUSH_INSNL(ret, location, branchunless, set_label);
9257 PUSH_INSN1(ret, location, getglobal, name);
9258 if (!popped) PUSH_INSN(ret, location, dup);
9260 PUSH_INSNL(ret, location, branchif, end_label);
9261 if (!popped) PUSH_INSN(ret, location, pop);
9263 PUSH_LABEL(ret, set_label);
9264 PM_COMPILE_NOT_POPPED(cast->
value);
9265 if (!popped) PUSH_INSN(ret, location, dup);
9267 PUSH_INSN1(ret, location, setglobal, name);
9268 PUSH_LABEL(ret, end_label);
9272 case PM_GLOBAL_VARIABLE_READ_NODE: {
9275 const pm_global_variable_read_node_t *cast = (
const pm_global_variable_read_node_t *) node;
9278 PUSH_INSN1(ret, location, getglobal, name);
9279 if (popped) PUSH_INSN(ret, location, pop);
9283 case PM_GLOBAL_VARIABLE_WRITE_NODE: {
9286 const pm_global_variable_write_node_t *cast = (
const pm_global_variable_write_node_t *) node;
9287 PM_COMPILE_NOT_POPPED(cast->
value);
9288 if (!popped) PUSH_INSN(ret, location, dup);
9290 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9291 PUSH_INSN1(ret, location, setglobal,
ID2SYM(name));
9295 case PM_HASH_NODE: {
9301 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9306 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
9309 PUSH_INSN1(ret, location, newhash,
INT2FIX(0));
9312 VALUE value = pm_static_literal_value(iseq, node, scope_node);
9313 PUSH_INSN1(ret, location, duphash, value);
9327 const pm_hash_node_t *cast = (
const pm_hash_node_t *) node;
9328 const pm_node_list_t *elements = &cast->
elements;
9334 for (
size_t index = 0; index < elements->
size; index++) {
9335 PM_COMPILE_POPPED(elements->
nodes[index]);
9339 pm_compile_hash_elements(iseq, node, elements, 0,
Qundef,
false, ret, scope_node);
9354 const pm_if_node_t *cast = (
const pm_if_node_t *) node;
9355 pm_compile_conditional(iseq, &location, PM_IF_NODE, (
const pm_node_t *) cast, cast->statements, cast->subsequent, cast->predicate, ret, popped, scope_node);
9358 case PM_IMAGINARY_NODE: {
9362 VALUE operand = parse_imaginary((
const pm_imaginary_node_t *) node);
9363 PUSH_INSN1(ret, location, putobject, operand);
9367 case PM_IMPLICIT_NODE: {
9376 const pm_implicit_node_t *cast = (
const pm_implicit_node_t *) node;
9377 PM_COMPILE(cast->
value);
9383 rb_bug(
"Should not ever enter an in node directly");
9386 case PM_INDEX_OPERATOR_WRITE_NODE: {
9389 const pm_index_operator_write_node_t *cast = (
const pm_index_operator_write_node_t *) node;
9390 pm_compile_index_operator_write_node(iseq, cast, &location, ret, popped, scope_node);
9393 case PM_INDEX_AND_WRITE_NODE: {
9396 const pm_index_and_write_node_t *cast = (
const pm_index_and_write_node_t *) node;
9397 pm_compile_index_control_flow_write_node(iseq, node, cast->
receiver, cast->
arguments, cast->
block, cast->
value, &location, ret, popped, scope_node);
9400 case PM_INDEX_OR_WRITE_NODE: {
9403 const pm_index_or_write_node_t *cast = (
const pm_index_or_write_node_t *) node;
9404 pm_compile_index_control_flow_write_node(iseq, node, cast->
receiver, cast->
arguments, cast->
block, cast->
value, &location, ret, popped, scope_node);
9407 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
9410 const pm_instance_variable_and_write_node_t *cast = (
const pm_instance_variable_and_write_node_t *) node;
9411 LABEL *end_label = NEW_LABEL(location.
line);
9413 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9416 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9417 if (!popped) PUSH_INSN(ret, location, dup);
9419 PUSH_INSNL(ret, location, branchunless, end_label);
9420 if (!popped) PUSH_INSN(ret, location, pop);
9422 PM_COMPILE_NOT_POPPED(cast->
value);
9423 if (!popped) PUSH_INSN(ret, location, dup);
9425 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9426 PUSH_LABEL(ret, end_label);
9430 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
9433 const pm_instance_variable_operator_write_node_t *cast = (
const pm_instance_variable_operator_write_node_t *) node;
9435 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9438 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9439 PM_COMPILE_NOT_POPPED(cast->
value);
9442 int flags = VM_CALL_ARGS_SIMPLE;
9443 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(flags));
9445 if (!popped) PUSH_INSN(ret, location, dup);
9446 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9450 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
9453 const pm_instance_variable_or_write_node_t *cast = (
const pm_instance_variable_or_write_node_t *) node;
9454 LABEL *end_label = NEW_LABEL(location.
line);
9456 ID name_id = pm_constant_id_lookup(scope_node, cast->
name);
9459 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9460 if (!popped) PUSH_INSN(ret, location, dup);
9462 PUSH_INSNL(ret, location, branchif, end_label);
9463 if (!popped) PUSH_INSN(ret, location, pop);
9465 PM_COMPILE_NOT_POPPED(cast->
value);
9466 if (!popped) PUSH_INSN(ret, location, dup);
9468 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
9469 PUSH_LABEL(ret, end_label);
9473 case PM_INSTANCE_VARIABLE_READ_NODE: {
9477 const pm_instance_variable_read_node_t *cast = (
const pm_instance_variable_read_node_t *) node;
9478 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9479 PUSH_INSN2(ret, location, getinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9483 case PM_INSTANCE_VARIABLE_WRITE_NODE: {
9486 const pm_instance_variable_write_node_t *cast = (
const pm_instance_variable_write_node_t *) node;
9487 PM_COMPILE_NOT_POPPED(cast->
value);
9488 if (!popped) PUSH_INSN(ret, location, dup);
9490 ID name = pm_constant_id_lookup(scope_node, cast->
name);
9491 PUSH_INSN2(ret, location, setinstancevariable,
ID2SYM(name), get_ivar_ic_value(iseq, name));
9495 case PM_INTEGER_NODE: {
9499 VALUE operand = parse_integer((
const pm_integer_node_t *) node);
9500 PUSH_INSN1(ret, location, putobject, operand);
9504 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
9507 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9509 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9510 PUSH_INSN1(ret, location, putobject, regexp);
9514 pm_compile_regexp_dynamic(iseq, node, &((
const pm_interpolated_match_last_line_node_t *) node)->parts, &location, ret, popped, scope_node);
9517 PUSH_INSN1(ret, location, getglobal,
rb_id2sym(idLASTLINE));
9518 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
9519 if (popped) PUSH_INSN(ret, location, pop);
9523 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
9526 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ONCE)) {
9527 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
9528 const rb_iseq_t *block_iseq = NULL;
9529 int ise_index = ISEQ_BODY(iseq)->ise_size++;
9531 pm_scope_node_t next_scope_node;
9532 pm_scope_node_init(node, &next_scope_node, scope_node);
9534 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, location.
line);
9535 pm_scope_node_destroy(&next_scope_node);
9537 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
9538 PUSH_INSN2(ret, location, once, block_iseq,
INT2FIX(ise_index));
9539 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
9541 if (popped) PUSH_INSN(ret, location, pop);
9545 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9547 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9548 PUSH_INSN1(ret, location, putobject, regexp);
9552 pm_compile_regexp_dynamic(iseq, node, &((
const pm_interpolated_regular_expression_node_t *) node)->parts, &location, ret, popped, scope_node);
9553 if (popped) PUSH_INSN(ret, location, pop);
9558 case PM_INTERPOLATED_STRING_NODE: {
9561 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
9563 VALUE string = pm_static_literal_value(iseq, node, scope_node);
9565 if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN)) {
9566 PUSH_INSN1(ret, location, putobject,
string);
9568 else if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) {
9569 PUSH_INSN1(ret, location, putstring,
string);
9572 PUSH_INSN1(ret, location, putchilledstring,
string);
9577 const pm_interpolated_string_node_t *cast = (
const pm_interpolated_string_node_t *) node;
9578 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret, popped, scope_node, NULL, NULL);
9579 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9580 if (popped) PUSH_INSN(ret, location, pop);
9585 case PM_INTERPOLATED_SYMBOL_NODE: {
9588 const pm_interpolated_symbol_node_t *cast = (
const pm_interpolated_symbol_node_t *) node;
9589 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret, popped, scope_node, NULL, NULL);
9592 PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9596 PUSH_INSN(ret, location, intern);
9599 PUSH_INSN(ret, location, pop);
9604 case PM_INTERPOLATED_X_STRING_NODE: {
9607 const pm_interpolated_x_string_node_t *cast = (
const pm_interpolated_x_string_node_t *) node;
9609 PUSH_INSN(ret, location, putself);
9611 int length = pm_interpolated_node_compile(iseq, &cast->
parts, &location, ret,
false, scope_node, NULL, NULL);
9612 if (length > 1) PUSH_INSN1(ret, location, concatstrings,
INT2FIX(length));
9614 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
9615 if (popped) PUSH_INSN(ret, location, pop);
9619 case PM_IT_LOCAL_VARIABLE_READ_NODE: {
9623 pm_scope_node_t *current_scope_node = scope_node;
9626 while (current_scope_node) {
9627 if (current_scope_node->parameters && PM_NODE_TYPE_P(current_scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
9628 PUSH_GETLOCAL(ret, location, current_scope_node->local_table_for_iseq_size, level);
9632 current_scope_node = current_scope_node->previous;
9635 rb_bug(
"Local `it` does not exist");
9640 case PM_KEYWORD_HASH_NODE: {
9643 const pm_keyword_hash_node_t *cast = (
const pm_keyword_hash_node_t *) node;
9644 const pm_node_list_t *elements = &cast->
elements;
9646 const pm_node_t *element;
9648 PM_COMPILE(element);
9651 if (!popped) PUSH_INSN1(ret, location, newhash,
INT2FIX(elements->
size * 2));
9654 case PM_LAMBDA_NODE: {
9657 const pm_lambda_node_t *cast = (
const pm_lambda_node_t *) node;
9659 pm_scope_node_t next_scope_node;
9660 pm_scope_node_init(node, &next_scope_node, scope_node);
9662 int opening_lineno = pm_location_line_number(parser, &cast->
opening_loc);
9663 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
9664 pm_scope_node_destroy(&next_scope_node);
9667 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
9668 PUSH_CALL_WITH_BLOCK(ret, location, idLambda, argc, block);
9671 if (popped) PUSH_INSN(ret, location, pop);
9674 case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
9677 const pm_local_variable_and_write_node_t *cast = (
const pm_local_variable_and_write_node_t *) node;
9678 LABEL *end_label = NEW_LABEL(location.
line);
9680 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9681 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9682 if (!popped) PUSH_INSN(ret, location, dup);
9684 PUSH_INSNL(ret, location, branchunless, end_label);
9685 if (!popped) PUSH_INSN(ret, location, pop);
9687 PM_COMPILE_NOT_POPPED(cast->
value);
9688 if (!popped) PUSH_INSN(ret, location, dup);
9690 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9691 PUSH_LABEL(ret, end_label);
9695 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
9698 const pm_local_variable_operator_write_node_t *cast = (
const pm_local_variable_operator_write_node_t *) node;
9700 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9701 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9703 PM_COMPILE_NOT_POPPED(cast->
value);
9706 PUSH_SEND_WITH_FLAG(ret, location, method_id,
INT2NUM(1),
INT2FIX(VM_CALL_ARGS_SIMPLE));
9708 if (!popped) PUSH_INSN(ret, location, dup);
9709 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9713 case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
9716 const pm_local_variable_or_write_node_t *cast = (
const pm_local_variable_or_write_node_t *) node;
9718 LABEL *set_label = NEW_LABEL(location.
line);
9719 LABEL *end_label = NEW_LABEL(location.
line);
9721 PUSH_INSN1(ret, location, putobject,
Qtrue);
9722 PUSH_INSNL(ret, location, branchunless, set_label);
9724 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9725 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
9726 if (!popped) PUSH_INSN(ret, location, dup);
9728 PUSH_INSNL(ret, location, branchif, end_label);
9729 if (!popped) PUSH_INSN(ret, location, pop);
9731 PUSH_LABEL(ret, set_label);
9732 PM_COMPILE_NOT_POPPED(cast->
value);
9733 if (!popped) PUSH_INSN(ret, location, dup);
9735 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
9736 PUSH_LABEL(ret, end_label);
9740 case PM_LOCAL_VARIABLE_READ_NODE: {
9744 const pm_local_variable_read_node_t *cast = (
const pm_local_variable_read_node_t *) node;
9745 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9746 PUSH_GETLOCAL(ret, location, index.index, index.level);
9751 case PM_LOCAL_VARIABLE_WRITE_NODE: {
9754 const pm_local_variable_write_node_t *cast = (
const pm_local_variable_write_node_t *) node;
9755 PM_COMPILE_NOT_POPPED(cast->
value);
9756 if (!popped) PUSH_INSN(ret, location, dup);
9758 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, cast->
depth);
9759 PUSH_SETLOCAL(ret, location, index.index, index.level);
9762 case PM_MATCH_LAST_LINE_NODE: {
9765 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
9767 PUSH_INSN1(ret, location, putobject, regexp);
9769 PUSH_SEND(ret, location, idEqTilde,
INT2NUM(1));
9770 if (popped) PUSH_INSN(ret, location, pop);
9774 case PM_MATCH_PREDICATE_NODE: {
9777 const pm_match_predicate_node_t *cast = (
const pm_match_predicate_node_t *) node;
9781 PUSH_INSN(ret, location, putnil);
9784 PM_COMPILE_NOT_POPPED(cast->
value);
9785 PUSH_INSN(ret, location, dup);
9789 LABEL *matched_label = NEW_LABEL(location.
line);
9790 LABEL *unmatched_label = NEW_LABEL(location.
line);
9791 LABEL *done_label = NEW_LABEL(location.
line);
9792 pm_compile_pattern(iseq, scope_node, cast->
pattern, ret, matched_label, unmatched_label,
false,
false,
true, 2);
9796 PUSH_LABEL(ret, unmatched_label);
9797 PUSH_INSN(ret, location, pop);
9798 PUSH_INSN(ret, location, pop);
9800 if (!popped) PUSH_INSN1(ret, location, putobject,
Qfalse);
9801 PUSH_INSNL(ret, location, jump, done_label);
9802 PUSH_INSN(ret, location, putnil);
9806 PUSH_LABEL(ret, matched_label);
9807 PUSH_INSN1(ret, location, adjuststack,
INT2FIX(2));
9808 if (!popped) PUSH_INSN1(ret, location, putobject,
Qtrue);
9809 PUSH_INSNL(ret, location, jump, done_label);
9811 PUSH_LABEL(ret, done_label);
9814 case PM_MATCH_REQUIRED_NODE:
9826 pm_compile_match_required_node(iseq, (
const pm_match_required_node_t *) node, &location, ret, popped, scope_node);
9828 case PM_MATCH_WRITE_NODE:
9837 pm_compile_match_write_node(iseq, (
const pm_match_write_node_t *) node, &location, ret, popped, scope_node);
9839 case PM_MISSING_NODE:
9840 rb_bug(
"A pm_missing_node_t should not exist in prism's AST.");
9842 case PM_MODULE_NODE: {
9845 const pm_module_node_t *cast = (
const pm_module_node_t *) node;
9847 ID module_id = pm_constant_id_lookup(scope_node, cast->
name);
9848 VALUE module_name =
rb_str_freeze(rb_sprintf(
"<module:%"PRIsVALUE
">", rb_id2str(module_id)));
9850 pm_scope_node_t next_scope_node;
9851 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
9853 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, location.
line);
9854 pm_scope_node_destroy(&next_scope_node);
9856 const int flags = VM_DEFINECLASS_TYPE_MODULE | pm_compile_class_path(iseq, cast->
constant_path, &location, ret,
false, scope_node);
9857 PUSH_INSN(ret, location, putnil);
9858 PUSH_INSN3(ret, location, defineclass,
ID2SYM(module_id), module_iseq,
INT2FIX(flags));
9861 if (popped) PUSH_INSN(ret, location, pop);
9864 case PM_REQUIRED_PARAMETER_NODE: {
9867 const pm_required_parameter_node_t *cast = (
const pm_required_parameter_node_t *) node;
9868 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, 0);
9870 PUSH_SETLOCAL(ret, location, index.index, index.level);
9873 case PM_MULTI_WRITE_NODE: {
9882 const pm_multi_write_node_t *cast = (
const pm_multi_write_node_t *) node;
9884 DECL_ANCHOR(writes);
9885 DECL_ANCHOR(cleanup);
9888 state.position = popped ? 0 : 1;
9889 pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
9891 PM_COMPILE_NOT_POPPED(cast->
value);
9892 if (!popped) PUSH_INSN(ret, location, dup);
9894 PUSH_SEQ(ret, writes);
9895 if (!popped && state.stack_size >= 1) {
9898 PUSH_INSN1(ret, location, setn,
INT2FIX(state.stack_size));
9903 pm_multi_target_state_update(&state);
9905 PUSH_SEQ(ret, cleanup);
9914 pm_compile_next_node(iseq, (
const pm_next_node_t *) node, &location, ret, popped, scope_node);
9920 PUSH_INSN(ret, location, putnil);
9925 case PM_NO_KEYWORDS_PARAMETER_NODE: {
9928 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
9931 case PM_NUMBERED_REFERENCE_READ_NODE: {
9935 const pm_numbered_reference_read_node_t *cast = (
const pm_numbered_reference_read_node_t *) node;
9938 VALUE ref = pm_compile_numbered_reference_ref(cast);
9939 PUSH_INSN2(ret, location, getspecial,
INT2FIX(1), ref);
9942 PUSH_INSN(ret, location, putnil);
9951 const pm_or_node_t *cast = (
const pm_or_node_t *) node;
9953 LABEL *end_label = NEW_LABEL(location.
line);
9954 PM_COMPILE_NOT_POPPED(cast->
left);
9956 if (!popped) PUSH_INSN(ret, location, dup);
9957 PUSH_INSNL(ret, location, branchif, end_label);
9959 if (!popped) PUSH_INSN(ret, location, pop);
9960 PM_COMPILE(cast->
right);
9961 PUSH_LABEL(ret, end_label);
9965 case PM_OPTIONAL_PARAMETER_NODE: {
9968 const pm_optional_parameter_node_t *cast = (
const pm_optional_parameter_node_t *) node;
9969 PM_COMPILE_NOT_POPPED(cast->
value);
9971 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->
name, 0);
9972 PUSH_SETLOCAL(ret, location, index.index, index.level);
9976 case PM_PARENTHESES_NODE: {
9982 const pm_parentheses_node_t *cast = (
const pm_parentheses_node_t *) node;
9984 if (cast->
body != NULL) {
9985 PM_COMPILE(cast->
body);
9988 PUSH_INSN(ret, location, putnil);
9993 case PM_PRE_EXECUTION_NODE: {
9996 const pm_pre_execution_node_t *cast = (
const pm_pre_execution_node_t *) node;
10005 DECL_ANCHOR(inner_pre);
10008 DECL_ANCHOR(inner_body);
10013 for (
size_t index = 0; index < body->
size; index++) {
10014 pm_compile_node(iseq, body->
nodes[index], inner_body,
true, scope_node);
10019 PUSH_INSN(inner_body, location, putnil);
10025 PUSH_SEQ(outer_pre, inner_pre);
10026 PUSH_SEQ(outer_pre, inner_body);
10031 case PM_POST_EXECUTION_NODE: {
10034 const rb_iseq_t *child_iseq;
10035 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
10037 pm_scope_node_t next_scope_node;
10038 pm_scope_node_init(node, &next_scope_node, scope_node);
10039 child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
10040 pm_scope_node_destroy(&next_scope_node);
10042 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
10044 int is_index = ISEQ_BODY(iseq)->ise_size++;
10045 PUSH_INSN2(ret, location, once, child_iseq,
INT2FIX(is_index));
10047 if (popped) PUSH_INSN(ret, location, pop);
10049 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
10053 case PM_RANGE_NODE: {
10056 const pm_range_node_t *cast = (
const pm_range_node_t *) node;
10057 bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
10059 if (pm_optimizable_range_item_p(cast->
left) && pm_optimizable_range_item_p(cast->
right)) {
10061 const pm_node_t *left = cast->
left;
10062 const pm_node_t *right = cast->
right;
10065 (left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((
const pm_integer_node_t *) left) :
Qnil,
10066 (right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((
const pm_integer_node_t *) right) :
Qnil,
10070 PUSH_INSN1(ret, location, putobject, val);
10074 if (cast->
left != NULL) {
10075 PM_COMPILE(cast->
left);
10077 else if (!popped) {
10078 PUSH_INSN(ret, location, putnil);
10081 if (cast->
right != NULL) {
10082 PM_COMPILE(cast->
right);
10084 else if (!popped) {
10085 PUSH_INSN(ret, location, putnil);
10089 PUSH_INSN1(ret, location, newrange,
INT2FIX(exclude_end ? 1 : 0));
10094 case PM_RATIONAL_NODE: {
10098 PUSH_INSN1(ret, location, putobject, parse_rational((
const pm_rational_node_t *) node));
10105 pm_compile_redo_node(iseq, &location, ret, popped, scope_node);
10107 case PM_REGULAR_EXPRESSION_NODE: {
10111 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
10112 PUSH_INSN1(ret, location, putobject, regexp);
10116 case PM_RESCUE_NODE:
10119 pm_compile_rescue_node(iseq, (
const pm_rescue_node_t *) node, &location, ret, popped, scope_node);
10121 case PM_RESCUE_MODIFIER_NODE: {
10124 const pm_rescue_modifier_node_t *cast = (
const pm_rescue_modifier_node_t *) node;
10126 pm_scope_node_t rescue_scope_node;
10127 pm_scope_node_init((
const pm_node_t *) cast, &rescue_scope_node, scope_node);
10129 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
10130 &rescue_scope_node,
10136 pm_scope_node_destroy(&rescue_scope_node);
10138 LABEL *lstart = NEW_LABEL(location.
line);
10139 LABEL *lend = NEW_LABEL(location.
line);
10140 LABEL *lcont = NEW_LABEL(location.
line);
10142 lstart->rescued = LABEL_RESCUE_BEG;
10143 lend->rescued = LABEL_RESCUE_END;
10145 PUSH_LABEL(ret, lstart);
10147 PUSH_LABEL(ret, lend);
10149 PUSH_INSN(ret, location, nop);
10150 PUSH_LABEL(ret, lcont);
10151 if (popped) PUSH_INSN(ret, location, pop);
10153 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
10154 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
10157 case PM_RETURN_NODE:
10163 pm_compile_return_node(iseq, (
const pm_return_node_t *) node, &location, ret, popped, scope_node);
10165 case PM_RETRY_NODE: {
10168 if (ISEQ_BODY(iseq)->
type == ISEQ_TYPE_RESCUE) {
10169 PUSH_INSN(ret, location, putnil);
10170 PUSH_INSN1(ret, location,
throw,
INT2FIX(TAG_RETRY));
10171 if (popped) PUSH_INSN(ret, location, pop);
10174 COMPILE_ERROR(iseq, location.
line,
"Invalid retry");
10179 case PM_SCOPE_NODE:
10180 pm_compile_scope_node(iseq, (pm_scope_node_t *) node, &location, ret, popped);
10182 case PM_SELF_NODE: {
10186 PUSH_INSN(ret, location, putself);
10190 case PM_SHAREABLE_CONSTANT_NODE: {
10193 const pm_shareable_constant_node_t *cast = (
const pm_shareable_constant_node_t *) node;
10194 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));
10196 switch (PM_NODE_TYPE(cast->
write)) {
10197 case PM_CONSTANT_WRITE_NODE:
10198 pm_compile_constant_write_node(iseq, (
const pm_constant_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10200 case PM_CONSTANT_AND_WRITE_NODE:
10201 pm_compile_constant_and_write_node(iseq, (
const pm_constant_and_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10203 case PM_CONSTANT_OR_WRITE_NODE:
10204 pm_compile_constant_or_write_node(iseq, (
const pm_constant_or_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10206 case PM_CONSTANT_OPERATOR_WRITE_NODE:
10207 pm_compile_constant_operator_write_node(iseq, (
const pm_constant_operator_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10209 case PM_CONSTANT_PATH_WRITE_NODE:
10210 pm_compile_constant_path_write_node(iseq, (
const pm_constant_path_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10212 case PM_CONSTANT_PATH_AND_WRITE_NODE:
10213 pm_compile_constant_path_and_write_node(iseq, (
const pm_constant_path_and_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10215 case PM_CONSTANT_PATH_OR_WRITE_NODE:
10216 pm_compile_constant_path_or_write_node(iseq, (
const pm_constant_path_or_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10218 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
10219 pm_compile_constant_path_operator_write_node(iseq, (
const pm_constant_path_operator_write_node_t *) cast->
write, shareability, &location, ret, popped, scope_node);
10222 rb_bug(
"Unexpected node type for shareable constant write: %s", pm_node_type_to_str(PM_NODE_TYPE(cast->
write)));
10228 case PM_SINGLETON_CLASS_NODE: {
10231 const pm_singleton_class_node_t *cast = (
const pm_singleton_class_node_t *) node;
10233 pm_scope_node_t next_scope_node;
10234 pm_scope_node_init((
const pm_node_t *) cast, &next_scope_node, scope_node);
10235 const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit(
"singleton class"), ISEQ_TYPE_CLASS, location.
line);
10236 pm_scope_node_destroy(&next_scope_node);
10239 PUSH_INSN(ret, location, putnil);
10242 CONST_ID(singletonclass,
"singletonclass");
10243 PUSH_INSN3(ret, location, defineclass,
ID2SYM(singletonclass), child_iseq,
INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
10245 if (popped) PUSH_INSN(ret, location, pop);
10250 case PM_SOURCE_ENCODING_NODE: {
10254 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10255 PUSH_INSN1(ret, location, putobject, value);
10259 case PM_SOURCE_FILE_NODE: {
10263 const pm_source_file_node_t *cast = (
const pm_source_file_node_t *) node;
10264 VALUE string = pm_source_file_value(cast, scope_node);
10266 if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_FROZEN)) {
10267 PUSH_INSN1(ret, location, putobject,
string);
10269 else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
10270 PUSH_INSN1(ret, location, putstring,
string);
10273 PUSH_INSN1(ret, location, putchilledstring,
string);
10278 case PM_SOURCE_LINE_NODE: {
10282 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10283 PUSH_INSN1(ret, location, putobject, value);
10287 case PM_SPLAT_NODE: {
10290 const pm_splat_node_t *cast = (
const pm_splat_node_t *) node;
10296 PUSH_INSN1(ret, location, splatarray,
Qtrue);
10300 case PM_STATEMENTS_NODE: {
10302 const pm_statements_node_t *cast = (
const pm_statements_node_t *) node;
10303 const pm_node_list_t *body = &cast->
body;
10305 if (body->
size > 0) {
10306 for (
size_t index = 0; index < body->
size - 1; index++) {
10307 PM_COMPILE_POPPED(body->
nodes[index]);
10309 PM_COMPILE(body->
nodes[body->
size - 1]);
10312 PUSH_INSN(ret, location, putnil);
10316 case PM_STRING_NODE: {
10320 const pm_string_node_t *cast = (
const pm_string_node_t *) node;
10321 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
10323 if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FROZEN)) {
10324 PUSH_INSN1(ret, location, putobject, value);
10326 else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
10327 PUSH_INSN1(ret, location, putstring, value);
10330 PUSH_INSN1(ret, location, putchilledstring, value);
10335 case PM_SUPER_NODE:
10339 pm_compile_super_node(iseq, (
const pm_super_node_t *) node, &location, ret, popped, scope_node);
10341 case PM_SYMBOL_NODE: {
10345 VALUE value = pm_static_literal_value(iseq, node, scope_node);
10346 PUSH_INSN1(ret, location, putobject, value);
10350 case PM_TRUE_NODE: {
10354 PUSH_INSN1(ret, location, putobject,
Qtrue);
10358 case PM_UNDEF_NODE: {
10361 const pm_undef_node_t *cast = (
const pm_undef_node_t *) node;
10362 const pm_node_list_t *names = &cast->
names;
10364 for (
size_t index = 0; index < names->
size; index++) {
10365 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
10366 PUSH_INSN1(ret, location, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CBASE));
10368 PM_COMPILE_NOT_POPPED(names->
nodes[index]);
10369 PUSH_SEND(ret, location, id_core_undef_method,
INT2NUM(2));
10371 if (index < names->size - 1) {
10372 PUSH_INSN(ret, location, pop);
10376 if (popped) PUSH_INSN(ret, location, pop);
10379 case PM_UNLESS_NODE: {
10385 const pm_unless_node_t *cast = (
const pm_unless_node_t *) node;
10386 const pm_statements_node_t *statements = NULL;
10388 statements = ((
const pm_else_node_t *) cast->
else_clause)->statements;
10391 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);
10394 case PM_UNTIL_NODE: {
10400 const pm_until_node_t *cast = (
const pm_until_node_t *) node;
10401 pm_compile_loop(iseq, &location, cast->
base.
flags, PM_UNTIL_NODE, (
const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10404 case PM_WHILE_NODE: {
10410 const pm_while_node_t *cast = (
const pm_while_node_t *) node;
10411 pm_compile_loop(iseq, &location, cast->
base.
flags, PM_WHILE_NODE, (
const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
10414 case PM_X_STRING_NODE: {
10417 const pm_x_string_node_t *cast = (
const pm_x_string_node_t *) node;
10418 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->
unescaped);
10420 PUSH_INSN(ret, location, putself);
10421 PUSH_INSN1(ret, location, putobject, value);
10422 PUSH_SEND_WITH_FLAG(ret, location, idBackquote,
INT2NUM(1),
INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
10423 if (popped) PUSH_INSN(ret, location, pop);
10427 case PM_YIELD_NODE:
10433 pm_compile_yield_node(iseq, (
const pm_yield_node_t *) node, &location, ret, popped, scope_node);
10436 rb_raise(
rb_eNotImpError,
"node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
10441#undef PM_CONTAINER_P
10445pm_iseq_pre_execution_p(rb_iseq_t *iseq)
10447 switch (ISEQ_BODY(iseq)->
type) {
10448 case ISEQ_TYPE_TOP:
10449 case ISEQ_TYPE_EVAL:
10450 case ISEQ_TYPE_MAIN:
10465pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
10469 if (pm_iseq_pre_execution_p(iseq)) {
10481 pm_compile_node(iseq, (
const pm_node_t *) node, body,
false, node);
10485 PUSH_SEQ(ret, pre);
10486 PUSH_SEQ(ret, body);
10491 pm_compile_node(iseq, (
const pm_node_t *) node, ret,
false, node);
10494 CHECK(iseq_setup_insn(iseq, ret));
10495 return iseq_setup(iseq, ret);
10505 if (result->
node.ast_node != NULL) {
10506 pm_node_destroy(&result->
parser, result->
node.ast_node);
10511 pm_scope_node_destroy(&result->
node);
10514 pm_parser_free(&result->
parser);
10515 pm_string_free(&result->
input);
10516 pm_options_free(&result->
options);
10552#define PM_COLOR_BOLD "\033[1m"
10553#define PM_COLOR_GRAY "\033[2m"
10554#define PM_COLOR_RED "\033[1;31m"
10555#define PM_COLOR_RESET "\033[m"
10556#define PM_ERROR_TRUNCATE 30
10561 if (errors == NULL)
return NULL;
10565 pm_line_column_t start = pm_newline_list_line_column(newline_list, error->location.
start, start_line);
10566 pm_line_column_t end = pm_newline_list_line_column(newline_list, error->location.end, start_line);
10574 (index < error_list->size) &&
10575 (errors[index].error != NULL) &&
10577 (errors[index].line < start.
line) ||
10584 if (index + 1 < error_list->
size) {
10585 memmove(&errors[index + 1], &errors[index],
sizeof(
pm_parse_error_t) * (error_list->
size - index - 1));
10589 uint32_t column_end;
10591 column_end = end.
column;
10593 column_end = (uint32_t) (newline_list->
offsets[start.
line - start_line + 1] - newline_list->
offsets[start.
line - start_line] - 1);
10597 if (start.
column == column_end) column_end++;
10601 .line = start.
line,
10602 .column_start = start.
column,
10603 .column_end = column_end
10611#define pm_buffer_append_literal(buffer, str) pm_buffer_append_string(buffer, str, rb_strlen_lit(str))
10614pm_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) {
10615 int32_t line_delta = line - parser->
start_line;
10616 assert(line_delta >= 0);
10618 size_t index = (size_t) line_delta;
10619 assert(index < newline_list->size);
10621 const uint8_t *start = &parser->
start[newline_list->
offsets[index]];
10622 const uint8_t *end;
10624 if (index >= newline_list->
size - 1) {
10627 end = &parser->
start[newline_list->
offsets[index + 1]];
10630 pm_buffer_append_format(buffer, number_prefix, line);
10633 bool truncate_end =
false;
10634 if ((column_end != 0) && ((end - (start + column_end)) >= PM_ERROR_TRUNCATE)) {
10635 end = start + column_end + PM_ERROR_TRUNCATE;
10636 truncate_end =
true;
10640 if (column_start >= PM_ERROR_TRUNCATE) {
10641 pm_buffer_append_string(buffer,
"... ", 4);
10642 start += column_start;
10645 pm_buffer_append_string(buffer, (
const char *) start, (
size_t) (end - start));
10647 if (truncate_end) {
10648 pm_buffer_append_string(buffer,
" ...\n", 5);
10649 }
else if (end == parser->
end && end[-1] !=
'\n') {
10650 pm_buffer_append_string(buffer,
"\n", 1);
10659 assert(error_list->
size != 0);
10663 const int32_t start_line = parser->
start_line;
10666 pm_parse_error_t *errors = pm_parse_errors_format_sort(parser, error_list, newline_list);
10667 if (errors == NULL)
return;
10673 int32_t first_line_number = errors[0].
line;
10674 int32_t last_line_number = errors[error_list->
size - 1].
line;
10679 if (first_line_number < 0) first_line_number = (-first_line_number) * 10;
10680 if (last_line_number < 0) last_line_number = (-last_line_number) * 10;
10681 int32_t max_line_number = first_line_number > last_line_number ? first_line_number : last_line_number;
10683 if (max_line_number < 10) {
10684 if (highlight > 0) {
10686 .number_prefix = PM_COLOR_GRAY
"%1" PRIi32
" | " PM_COLOR_RESET,
10687 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10688 .divider = PM_COLOR_GRAY
" ~~~~~" PM_COLOR_RESET
"\n"
10692 .number_prefix =
"%1" PRIi32
" | ",
10693 .blank_prefix =
" | ",
10694 .divider =
" ~~~~~\n"
10697 }
else if (max_line_number < 100) {
10698 if (highlight > 0) {
10700 .number_prefix = PM_COLOR_GRAY
"%2" PRIi32
" | " PM_COLOR_RESET,
10701 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10702 .divider = PM_COLOR_GRAY
" ~~~~~~" PM_COLOR_RESET
"\n"
10706 .number_prefix =
"%2" PRIi32
" | ",
10707 .blank_prefix =
" | ",
10708 .divider =
" ~~~~~~\n"
10711 }
else if (max_line_number < 1000) {
10712 if (highlight > 0) {
10714 .number_prefix = PM_COLOR_GRAY
"%3" PRIi32
" | " PM_COLOR_RESET,
10715 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10716 .divider = PM_COLOR_GRAY
" ~~~~~~~" PM_COLOR_RESET
"\n"
10720 .number_prefix =
"%3" PRIi32
" | ",
10721 .blank_prefix =
" | ",
10722 .divider =
" ~~~~~~~\n"
10725 }
else if (max_line_number < 10000) {
10726 if (highlight > 0) {
10728 .number_prefix = PM_COLOR_GRAY
"%4" PRIi32
" | " PM_COLOR_RESET,
10729 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10730 .divider = PM_COLOR_GRAY
" ~~~~~~~~" PM_COLOR_RESET
"\n"
10734 .number_prefix =
"%4" PRIi32
" | ",
10735 .blank_prefix =
" | ",
10736 .divider =
" ~~~~~~~~\n"
10740 if (highlight > 0) {
10742 .number_prefix = PM_COLOR_GRAY
"%5" PRIi32
" | " PM_COLOR_RESET,
10743 .blank_prefix = PM_COLOR_GRAY
" | " PM_COLOR_RESET,
10744 .divider = PM_COLOR_GRAY
" ~~~~~~~~" PM_COLOR_RESET
"\n"
10748 .number_prefix =
"%5" PRIi32
" | ",
10749 .blank_prefix =
" | ",
10750 .divider =
" ~~~~~~~~\n"
10764 uint32_t last_column_start = 0;
10767 for (
size_t index = 0; index < error_list->
size; index++) {
10772 if (error->
line - last_line > 1) {
10773 if (error->
line - last_line > 2) {
10774 if ((index != 0) && (error->
line - last_line > 3)) {
10778 pm_buffer_append_string(buffer,
" ", 2);
10779 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, error->
line - 2, 0, 0, buffer);
10782 pm_buffer_append_string(buffer,
" ", 2);
10783 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, error->
line - 1, 0, 0, buffer);
10788 if ((index == 0) || (error->
line != last_line)) {
10789 if (highlight > 1) {
10790 pm_buffer_append_literal(buffer, PM_COLOR_RED
"> " PM_COLOR_RESET);
10791 }
else if (highlight > 0) {
10792 pm_buffer_append_literal(buffer, PM_COLOR_BOLD
"> " PM_COLOR_RESET);
10794 pm_buffer_append_literal(buffer,
"> ");
10801 for (
size_t next_index = index + 1; next_index < error_list->
size; next_index++) {
10802 if (errors[next_index].line != error->
line)
break;
10803 if (errors[next_index].column_end > column_end) column_end = errors[next_index].
column_end;
10809 const uint8_t *start = &parser->
start[newline_list->
offsets[error->
line - start_line]];
10810 if (start == parser->
end) pm_buffer_append_byte(buffer,
'\n');
10821 pm_buffer_append_string(buffer,
" ", 2);
10825 if (last_column_start >= PM_ERROR_TRUNCATE) {
10826 pm_buffer_append_string(buffer,
" ", 4);
10827 column = last_column_start;
10830 while (column < error->column_start) {
10831 pm_buffer_append_byte(buffer,
' ');
10833 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10834 column += (char_width == 0 ? 1 : char_width);
10837 if (highlight > 1) pm_buffer_append_literal(buffer, PM_COLOR_RED);
10838 else if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_BOLD);
10839 pm_buffer_append_byte(buffer,
'^');
10841 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10842 column += (char_width == 0 ? 1 : char_width);
10844 while (column < error->column_end) {
10845 pm_buffer_append_byte(buffer,
'~');
10847 size_t char_width = encoding->
char_width(start + column, parser->
end - (start + column));
10848 column += (char_width == 0 ? 1 : char_width);
10851 if (highlight > 0) pm_buffer_append_literal(buffer, PM_COLOR_RESET);
10853 if (inline_messages) {
10854 pm_buffer_append_byte(buffer,
' ');
10855 assert(error->
error != NULL);
10858 pm_buffer_append_string(buffer, message, strlen(message));
10861 pm_buffer_append_byte(buffer,
'\n');
10865 last_line = error->
line;
10868 if (index == error_list->
size - 1) {
10869 next_line = (((int32_t) newline_list->
size) + parser->
start_line);
10878 next_line = errors[index + 1].
line;
10881 if (next_line - last_line > 1) {
10882 pm_buffer_append_string(buffer,
" ", 2);
10883 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, ++last_line, 0, 0, buffer);
10886 if (next_line - last_line > 1) {
10887 pm_buffer_append_string(buffer,
" ", 2);
10888 pm_parse_errors_format_line(parser, newline_list, error_format.
number_prefix, ++last_line, 0, 0, buffer);
10896#undef PM_ERROR_TRUNCATE
10897#undef PM_COLOR_GRAY
10899#undef PM_COLOR_RESET
10910 const size_t start_line = pm_newline_list_line_column(&parser->
newline_list, location->
start, 1).line;
10911 const size_t end_line = pm_newline_list_line_column(&parser->
newline_list, location->
end, 1).line;
10917 while (start < end) {
10918 if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0)
return false;
10934 bool valid_utf8 =
true;
10939 int highlight = rb_stderr_tty_p();
10941 const char *no_color = getenv(
"NO_COLOR");
10942 highlight = (no_color == NULL || no_color[0] ==
'\0') ? 2 : 1;
10946 switch (error->level) {
10953 if (valid_utf8 && !pm_parse_process_error_utf8_p(parser, &error->location)) {
10954 valid_utf8 =
false;
10961 int32_t line_number = (int32_t) pm_location_line_number(parser, &error->location);
10963 pm_buffer_append_format(
10965 "%.*s:%" PRIi32
": %s",
10966 (
int) pm_string_length(filepath),
10967 pm_string_source(filepath),
10972 if (pm_parse_process_error_utf8_p(parser, &error->location)) {
10973 pm_buffer_append_byte(&buffer,
'\n');
10976 pm_list_t error_list = { .size = 1, .head = list_node, .tail = list_node };
10978 pm_parse_errors_format(parser, &error_list, &buffer, highlight,
false);
10981 VALUE value =
rb_exc_new(rb_eArgError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
10982 pm_buffer_free(&buffer);
10998 pm_buffer_append_format(
11000 "%.*s:%" PRIi32
": syntax error%s found\n",
11001 (
int) pm_string_length(filepath),
11002 pm_string_source(filepath),
11003 (int32_t) pm_location_line_number(parser, &head->location),
11008 pm_parse_errors_format(parser, &parser->
error_list, &buffer, highlight,
true);
11012 if (error != head) pm_buffer_append_byte(&buffer,
'\n');
11013 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);
11017 VALUE message = rb_enc_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer), result->
node.encoding);
11021 VALUE path = rb_enc_str_new((
const char *) pm_string_source(filepath), pm_string_length(filepath), filepath_encoding);
11024 pm_buffer_free(&buffer);
11041 pm_scope_node_t *scope_node = &result->
node;
11043 int coverage_enabled = scope_node->coverage_enabled;
11045 pm_scope_node_init(node, scope_node, NULL);
11048 scope_node->encoding = rb_enc_find(parser->
encoding->
name);
11049 if (!scope_node->encoding) rb_bug(
"Encoding not found %s!", parser->
encoding->
name);
11051 scope_node->coverage_enabled = coverage_enabled;
11055 if (script_lines != NULL) {
11060 size_t length = index == parser->
newline_list.
size - 1 ? ((size_t) (parser->
end - (parser->
start + offset))) : (parser->newline_list.offsets[index + 1] - offset);
11061 rb_ary_push(*script_lines, rb_enc_str_new((
const char *) parser->
start + offset, length, scope_node->encoding));
11069 const char *warning_filepath = (
const char *) pm_string_source(&parser->
filepath);
11072 int line = pm_location_line_number(parser, &warning->
location);
11075 rb_enc_compile_warning(scope_node->encoding, warning_filepath, line,
"%s", warning->
message);
11078 rb_enc_compile_warn(scope_node->encoding, warning_filepath, line,
"%s", warning->
message);
11084 VALUE error = pm_parse_process_error(result);
11093 scope_node->parser = parser;
11098 scope_node->constants[index] = rb_intern3((
const char *) constant->
start, constant->
length, scope_node->encoding);
11101 scope_node->index_lookup_table = st_init_numtable();
11103 for (
size_t index = 0; index < locals->
size; index++) {
11104 st_insert(scope_node->index_lookup_table, locals->
ids[index], index);
11118pm_options_frozen_string_literal_init(
pm_options_t *options)
11120 int frozen_string_literal = rb_iseq_opt_frozen_string_literal();
11122 switch (frozen_string_literal) {
11123 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
11125 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
11126 pm_options_frozen_string_literal_set(options,
false);
11128 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
11129 pm_options_frozen_string_literal_set(options,
true);
11132 rb_bug(
"pm_options_frozen_string_literal_init: invalid frozen_string_literal=%d", frozen_string_literal);
11142pm_parse_file_script_lines(
const pm_scope_node_t *scope_node,
const pm_parser_t *parser)
11145 const char *start = (
const char *) parser->
start;
11146 const char *end = (
const char *) parser->
end;
11151 size_t last_offset = newline_list->
offsets[newline_list->
size - 1];
11152 bool last_push = start + last_offset != end;
11155 VALUE lines = rb_ary_new_capa(newline_list->
size - (last_push ? 0 : 1));
11157 for (
size_t index = 0; index < newline_list->
size - 1; index++) {
11158 size_t offset = newline_list->
offsets[index];
11159 size_t length = newline_list->
offsets[index + 1] - offset;
11161 rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
11166 rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), scope_node->encoding));
11176pm_read_file(
pm_string_t *
string,
const char *filepath)
11180 int length = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
11183 WCHAR *wfilepath =
xmalloc(
sizeof(WCHAR) * ((
size_t) length));
11184 if ((wfilepath == NULL) || (MultiByteToWideChar(CP_UTF8, 0, filepath, -1, wfilepath, length) == 0)) {
11189 HANDLE file = CreateFileW(wfilepath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
11190 if (file == INVALID_HANDLE_VALUE) {
11193 if (GetLastError() == ERROR_ACCESS_DENIED) {
11194 DWORD attributes = GetFileAttributesW(wfilepath);
11195 if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
11205 DWORD file_size = GetFileSize(file, NULL);
11206 if (file_size == INVALID_FILE_SIZE) {
11214 if (file_size == 0) {
11217 const uint8_t source[] =
"";
11218 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11223 HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
11224 if (mapping == NULL) {
11231 uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
11232 CloseHandle(mapping);
11236 if (source == NULL) {
11240 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size };
11242#elif defined(_POSIX_MAPPED_FILES)
11244 const int open_mode = O_RDONLY | O_NONBLOCK;
11245 int fd = open(filepath, open_mode);
11252 if (fstat(fd, &sb) == -1) {
11258 if (S_ISDIR(sb.st_mode)) {
11266 if (S_ISFIFO(sb.st_mode) || S_ISCHR(sb.st_mode)) {
11275 long len = RSTRING_LEN(contents);
11280 size_t length = (size_t)
len;
11281 uint8_t *source = malloc(length);
11282 memcpy(source, RSTRING_PTR(contents), length);
11283 *
string = (
pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = length };
11289 size_t size = (size_t) sb.st_size;
11290 uint8_t *source = NULL;
11294 const uint8_t source[] =
"";
11295 *
string = (
pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
11299 source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
11300 if (source == MAP_FAILED) {
11306 *
string = (
pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
11309 return pm_string_file_init(
string, filepath);
11323 pm_options_frozen_string_literal_init(&result->
options);
11332 err = rb_w32_map_errno(GetLastError());
11364 pm_options_filepath_set(&result->
options, RSTRING_PTR(filepath));
11367 pm_options_version_for_current_ruby_set(&result->
options);
11369 pm_parser_init(&result->
parser, pm_string_source(&result->
input), pm_string_length(&result->
input), &result->
options);
11370 pm_node_t *node = pm_parse(&result->
parser);
11372 VALUE error = pm_parse_process(result, node, script_lines);
11377 ID id_script_lines = rb_intern(
"SCRIPT_LINES__");
11383 rb_hash_aset(constant_script_lines, filepath, pm_parse_file_script_lines(&result->
node, &result->
parser));
11397 VALUE error = pm_load_file(result, filepath,
false);
11398 if (
NIL_P(error)) {
11399 error = pm_parse_file(result, filepath, script_lines);
11414 rb_encoding *encoding = rb_enc_get(source);
11415 if (!rb_enc_asciicompat(encoding)) {
11419 pm_options_frozen_string_literal_init(&result->
options);
11420 pm_string_constant_init(&result->
input, RSTRING_PTR(source), RSTRING_LEN(source));
11421 pm_options_encoding_set(&result->
options, rb_enc_name(encoding));
11424 pm_options_filepath_set(&result->
options, RSTRING_PTR(filepath));
11427 pm_options_version_for_current_ruby_set(&result->
options);
11429 pm_parser_init(&result->
parser, pm_string_source(&result->
input), pm_string_length(&result->
input), &result->
options);
11430 pm_node_t *node = pm_parse(&result->
parser);
11432 return pm_parse_process(result, node, script_lines);
11441pm_parse_stdin_eof(
void *stream)
11444 return wrapped_stdin->eof_seen;
11451pm_parse_stdin_fgets(
char *
string,
int size,
void *stream)
11462 const char *cstr = RSTRING_PTR(line);
11463 long length = RSTRING_LEN(line);
11465 memcpy(
string, cstr, length);
11466 string[length] =
'\0';
11471 if (length < (size - 1) &&
string[length - 1] !=
'\n') {
11472 wrapped_stdin->eof_seen = 1;
11479void rb_reset_argf_lineno(
long n);
11489 pm_options_frozen_string_literal_init(&result->
options);
11497 pm_node_t *node = pm_parse_stream(&result->
parser, &buffer, (
void *) &wrapped_stdin, pm_parse_stdin_fgets, pm_parse_stdin_eof, &result->
options);
11502 pm_string_owned_init(&result->
input, (uint8_t *) pm_buffer_value(&buffer), pm_buffer_length(&buffer));
11506 rb_reset_argf_lineno(0);
11508 return pm_parse_process(result, node, NULL);
11511#define PM_VERSION_FOR_RELEASE(major, minor) PM_VERSION_FOR_RELEASE_IMPL(major, minor)
11512#define PM_VERSION_FOR_RELEASE_IMPL(major, minor) PM_OPTIONS_VERSION_CRUBY_##major##_##minor
11514void pm_options_version_for_current_ruby_set(
pm_options_t *options) {
11515 options->
version = PM_VERSION_FOR_RELEASE(RUBY_VERSION_MAJOR, RUBY_VERSION_MINOR);
11519#define NEW_ISEQ OLD_ISEQ
11521#undef NEW_CHILD_ISEQ
11522#define NEW_CHILD_ISEQ OLD_CHILD_ISEQ
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
@ PM_ERROR_LEVEL_ARGUMENT
For errors that should raise an argument error.
@ PM_ERROR_LEVEL_LOAD
For errors that should raise a load error.
@ PM_ERROR_LEVEL_SYNTAX
For errors that should raise a syntax error.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_CLASS
Encountered a new class.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_EVENT_RESCUE
Encountered a rescue statement.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ALLOCV
Old name of RB_ALLOCV.
#define ALLOC
Old name of RB_ALLOC.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ID2SYM
Old name of RB_ID2SYM.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define FIXABLE
Old name of RB_FIXABLE.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define rb_exc_new3
Old name of rb_exc_new_str.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
#define xcalloc
Old name of ruby_xcalloc.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
VALUE rb_eNotImpError
NotImplementedError exception.
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_new(void)
Allocates a new, empty array.
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_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
@ RUBY_IO_READABLE
IO::READABLE
VALUE rb_io_wait(VALUE io, VALUE events, VALUE timeout)
Blocks until the passed IO is ready for the passed events.
int len
Length of the buffer.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
#define 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 * 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.
struct rb_iseq_constant_body::@000024342312237062266020177166377106262102236123 param
parameter information
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.