[ruby/prism] Use `version: 3.3.1` against `Translation::Parser`
[ruby.git] / prism_compile.c
blobf784e9d0eed1386baa21c038768f60abcb29e455
1 #include "prism.h"
3 /******************************************************************************/
4 /* These macros operate on pm_line_column_t structs as opposed to NODE*s. */
5 /******************************************************************************/
7 #define PUSH_ADJUST(seq, location, label) \
8 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (int) (location).line))
10 #define PUSH_ADJUST_RESTORE(seq, label) \
11 ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
13 #define PUSH_INSN(seq, location, insn) \
14 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).column, BIN(insn), 0))
16 #define PUSH_INSN1(seq, location, insn, op1) \
17 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).column, BIN(insn), 1, (VALUE)(op1)))
19 #define PUSH_INSN2(seq, location, insn, op1, op2) \
20 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).column, BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
22 #define PUSH_INSN3(seq, location, insn, op1, op2, op3) \
23 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).column, BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
25 #define PUSH_INSNL(seq, location, insn, label) \
26 (PUSH_INSN1(seq, location, insn, label), LABEL_REF(label))
28 #define PUSH_LABEL(seq, label) \
29 ADD_ELEM((seq), (LINK_ELEMENT *) (label))
31 #define PUSH_SEND_R(seq, location, id, argc, block, flag, keywords) \
32 ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (int) (location).line, (int) (location).column, (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
34 #define PUSH_SEND(seq, location, id, argc) \
35 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
37 #define PUSH_SEND_WITH_FLAG(seq, location, id, argc, flag) \
38 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)(flag), NULL)
40 #define PUSH_SEND_WITH_BLOCK(seq, location, id, argc, block) \
41 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(0), NULL)
43 #define PUSH_CALL(seq, location, id, argc) \
44 PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
46 #define PUSH_CALL_WITH_BLOCK(seq, location, id, argc, block) \
47 PUSH_SEND_R((seq), location, (id), (argc), (block), (VALUE)INT2FIX(VM_CALL_FCALL), NULL)
49 #define PUSH_TRACE(seq, event) \
50 ADD_ELEM((seq), (LINK_ELEMENT *) new_trace_body(iseq, (event), 0))
52 #define PUSH_CATCH_ENTRY(type, ls, le, iseqv, lc) \
53 ADD_CATCH_ENTRY((type), (ls), (le), (iseqv), (lc))
55 #define PUSH_SEQ(seq1, seq2) \
56 APPEND_LIST((seq1), (seq2))
58 #define PUSH_SYNTHETIC_PUTNIL(seq, iseq) \
59 do { \
60 int lineno = ISEQ_COMPILE_DATA(iseq)->last_line; \
61 if (lineno == 0) lineno = FIX2INT(rb_iseq_first_lineno(iseq)); \
62 ADD_SYNTHETIC_INSN(seq, lineno, -1, putnil); \
63 } while (0)
65 /******************************************************************************/
66 /* These functions compile getlocal/setlocal instructions but operate on */
67 /* prism locations instead of NODEs. */
68 /******************************************************************************/
70 static void
71 pm_iseq_add_getlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line_no, int column, int idx, int level)
73 if (iseq_local_block_param_p(iseq, idx, level)) {
74 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line_no, column, BIN(getblockparam), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
76 else {
77 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line_no, column, BIN(getlocal), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
79 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qfalse);
82 static void
83 pm_iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line_no, int column, int idx, int level)
85 if (iseq_local_block_param_p(iseq, idx, level)) {
86 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line_no, column, BIN(setblockparam), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
88 else {
89 ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line_no, column, BIN(setlocal), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
91 if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qtrue);
94 #define PUSH_GETLOCAL(seq, location, idx, level) \
95 pm_iseq_add_getlocal(iseq, (seq), (int) (location).line, (int) (location).column, (idx), (level))
97 #define PUSH_SETLOCAL(seq, location, idx, level) \
98 pm_iseq_add_setlocal(iseq, (seq), (int) (location).line, (int) (location).column, (idx), (level))
100 /******************************************************************************/
101 /* These are helper macros for the compiler. */
102 /******************************************************************************/
104 #define OLD_ISEQ NEW_ISEQ
105 #undef NEW_ISEQ
107 #define NEW_ISEQ(node, name, type, line_no) \
108 pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
110 #define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
111 #undef NEW_CHILD_ISEQ
113 #define NEW_CHILD_ISEQ(node, name, type, line_no) \
114 pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
116 #define PM_COMPILE(node) \
117 pm_compile_node(iseq, (node), ret, popped, scope_node)
119 #define PM_COMPILE_INTO_ANCHOR(_ret, node) \
120 pm_compile_node(iseq, (node), _ret, popped, scope_node)
122 #define PM_COMPILE_POPPED(node) \
123 pm_compile_node(iseq, (node), ret, true, scope_node)
125 #define PM_COMPILE_NOT_POPPED(node) \
126 pm_compile_node(iseq, (node), ret, false, scope_node)
128 #define PM_SPECIAL_CONSTANT_FLAG ((pm_constant_id_t)(1 << 31))
129 #define PM_CONSTANT_AND ((pm_constant_id_t)(idAnd | PM_SPECIAL_CONSTANT_FLAG))
130 #define PM_CONSTANT_DOT3 ((pm_constant_id_t)(idDot3 | PM_SPECIAL_CONSTANT_FLAG))
131 #define PM_CONSTANT_MULT ((pm_constant_id_t)(idMULT | PM_SPECIAL_CONSTANT_FLAG))
132 #define PM_CONSTANT_POW ((pm_constant_id_t)(idPow | PM_SPECIAL_CONSTANT_FLAG))
134 #define PM_NODE_START_LINE_COLUMN(parser, node) \
135 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line)
137 #define PM_NODE_END_LINE_COLUMN(parser, node) \
138 pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line)
140 #define PM_LOCATION_START_LINE_COLUMN(parser, location) \
141 pm_newline_list_line_column(&(parser)->newline_list, (location)->start, (parser)->start_line)
143 static int
144 pm_node_line_number(const pm_parser_t *parser, const pm_node_t *node)
146 return (int) PM_NODE_START_LINE_COLUMN(parser, node).line;
149 static int
150 pm_location_line_number(const pm_parser_t *parser, const pm_location_t *location) {
151 return (int) PM_LOCATION_START_LINE_COLUMN(parser, location).line;
155 * Convert the value of an integer node into a Ruby Integer.
157 static VALUE
158 parse_integer(const pm_integer_node_t *node)
160 const pm_integer_t *integer = &node->value;
161 VALUE result;
163 if (integer->values == NULL) {
164 result = UINT2NUM(integer->value);
166 else {
167 VALUE string = rb_str_new(NULL, integer->length * 8);
168 unsigned char *bytes = (unsigned char *) RSTRING_PTR(string);
170 size_t offset = integer->length * 8;
171 for (size_t value_index = 0; value_index < integer->length; value_index++) {
172 uint32_t value = integer->values[value_index];
174 for (int index = 0; index < 8; index++) {
175 int byte = (value >> (4 * index)) & 0xf;
176 bytes[--offset] = byte < 10 ? byte + '0' : byte - 10 + 'a';
180 result = rb_funcall(string, rb_intern("to_i"), 1, UINT2NUM(16));
183 if (integer->negative) {
184 result = rb_funcall(result, rb_intern("-@"), 0);
187 return result;
191 * Convert the value of a float node into a Ruby Float.
193 static VALUE
194 parse_float(const pm_float_node_t *node)
196 return DBL2NUM(node->value);
200 * Convert the value of a rational node into a Ruby Rational. Rational nodes can
201 * either be wrapping an integer node or a float node. If it's an integer node,
202 * we can reuse our parsing. If it's not, then we'll parse the numerator and
203 * then parse the denominator and create the rational from those two values.
205 static VALUE
206 parse_rational(const pm_rational_node_t *node)
208 VALUE result;
210 if (PM_NODE_TYPE_P(node->numeric, PM_FLOAT_NODE)) {
211 const uint8_t *start = node->base.location.start;
212 const uint8_t *end = node->base.location.end - 1;
213 size_t length = end - start;
215 char *buffer = malloc(length + 1);
216 memcpy(buffer, start, length);
218 buffer[length] = '\0';
220 char *decimal = memchr(buffer, '.', length);
221 RUBY_ASSERT(decimal);
222 size_t seen_decimal = decimal - buffer;
223 size_t fraclen = length - seen_decimal - 1;
224 memmove(decimal, decimal + 1, fraclen + 1);
226 VALUE numerator = rb_cstr_to_inum(buffer, 10, false);
227 result = rb_rational_new(numerator, rb_int_positive_pow(10, fraclen));
229 free(buffer);
231 else {
232 RUBY_ASSERT(PM_NODE_TYPE_P(node->numeric, PM_INTEGER_NODE));
233 VALUE numerator = parse_integer((const pm_integer_node_t *) node->numeric);
234 result = rb_rational_raw(numerator, INT2FIX(1));
237 return result;
241 * Convert the value of an imaginary node into a Ruby Complex. Imaginary nodes
242 * can be wrapping an integer node, a float node, or a rational node. In all
243 * cases we will reuse parsing functions seen above to get the inner value, and
244 * then convert into an imaginary with rb_complex_raw.
246 static VALUE
247 parse_imaginary(const pm_imaginary_node_t *node)
249 VALUE imaginary_part;
250 switch (PM_NODE_TYPE(node->numeric)) {
251 case PM_FLOAT_NODE: {
252 imaginary_part = parse_float((const pm_float_node_t *) node->numeric);
253 break;
255 case PM_INTEGER_NODE: {
256 imaginary_part = parse_integer((const pm_integer_node_t *) node->numeric);
257 break;
259 case PM_RATIONAL_NODE: {
260 imaginary_part = parse_rational((const pm_rational_node_t *) node->numeric);
261 break;
263 default:
264 rb_bug("Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->numeric)));
267 return rb_complex_raw(INT2FIX(0), imaginary_part);
270 static inline VALUE
271 parse_string(const pm_scope_node_t *scope_node, const pm_string_t *string)
273 return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), scope_node->encoding);
277 * Certain strings can have their encoding differ from the parser's encoding due
278 * to bytes or escape sequences that have the top bit set. This function handles
279 * creating those strings based on the flags set on the owning node.
281 static inline VALUE
282 parse_string_encoded(const pm_node_t *node, const pm_string_t *string, rb_encoding *default_encoding)
284 rb_encoding *encoding;
286 if (node->flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
287 encoding = rb_ascii8bit_encoding();
289 else if (node->flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
290 encoding = rb_utf8_encoding();
292 else {
293 encoding = default_encoding;
296 return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), encoding);
299 static inline VALUE
300 parse_static_literal_string(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *string)
302 rb_encoding *encoding;
304 if (node->flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
305 encoding = rb_ascii8bit_encoding();
307 else if (node->flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
308 encoding = rb_utf8_encoding();
310 else {
311 encoding = scope_node->encoding;
314 VALUE value = rb_enc_interned_str((const char *) pm_string_source(string), pm_string_length(string), encoding);
315 rb_enc_str_coderange(value);
317 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
318 int line_number = pm_node_line_number(scope_node->parser, node);
319 VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq), INT2FIX(line_number));
320 value = rb_str_dup(value);
321 rb_ivar_set(value, id_debug_created_info, rb_obj_freeze(debug_info));
322 rb_str_freeze(value);
325 return value;
328 static inline ID
329 parse_string_symbol(const pm_scope_node_t *scope_node, const pm_symbol_node_t *symbol)
331 rb_encoding *encoding;
332 if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING) {
333 encoding = rb_utf8_encoding();
335 else if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING) {
336 encoding = rb_ascii8bit_encoding();
338 else if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING) {
339 encoding = rb_usascii_encoding();
341 else {
342 encoding = scope_node->encoding;
345 return rb_intern3((const char *) pm_string_source(&symbol->unescaped), pm_string_length(&symbol->unescaped), encoding);
348 static int
349 pm_optimizable_range_item_p(const pm_node_t *node)
351 return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
354 /** Raise an error corresponding to the invalid regular expression. */
355 static VALUE
356 parse_regexp_error(rb_iseq_t *iseq, int32_t line_number, const char *fmt, ...)
358 va_list args;
359 va_start(args, fmt);
360 VALUE error = rb_syntax_error_append(Qnil, rb_iseq_path(iseq), line_number, -1, NULL, "%" PRIsVALUE, args);
361 va_end(args);
362 rb_exc_raise(error);
365 static VALUE
366 parse_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)
368 // If we were passed an explicit regexp encoding, then we need to double
369 // check that it's okay here for this fragment of the string.
370 rb_encoding *encoding;
372 if (explicit_regexp_encoding != NULL) {
373 encoding = explicit_regexp_encoding;
375 else if (node->flags & PM_STRING_FLAGS_FORCED_BINARY_ENCODING) {
376 encoding = rb_ascii8bit_encoding();
378 else if (node->flags & PM_STRING_FLAGS_FORCED_UTF8_ENCODING) {
379 encoding = rb_utf8_encoding();
381 else {
382 encoding = implicit_regexp_encoding;
385 VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), encoding);
386 VALUE error = rb_reg_check_preprocess(string);
388 if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, node), "%" PRIsVALUE, rb_obj_as_string(error));
389 return string;
392 static VALUE
393 pm_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)
395 VALUE current = Qnil;
397 for (size_t index = 0; index < nodes->size; index++) {
398 const pm_node_t *part = nodes->nodes[index];
399 VALUE string;
401 switch (PM_NODE_TYPE(part)) {
402 case PM_STRING_NODE:
403 if (implicit_regexp_encoding != NULL) {
404 if (top) {
405 string = parse_regexp_string_part(iseq, scope_node, part, &((const pm_string_node_t *) part)->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
407 else {
408 string = parse_string_encoded(part, &((const pm_string_node_t *) part)->unescaped, scope_node->encoding);
409 VALUE error = rb_reg_check_preprocess(string);
410 if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, part), "%" PRIsVALUE, rb_obj_as_string(error));
413 else {
414 string = parse_string_encoded(part, &((const pm_string_node_t *) part)->unescaped, scope_node->encoding);
416 break;
417 case PM_INTERPOLATED_STRING_NODE:
418 string = pm_static_literal_concat(iseq, &((const pm_interpolated_string_node_t *) part)->parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
419 break;
420 case PM_EMBEDDED_STATEMENTS_NODE: {
421 const pm_embedded_statements_node_t *cast = (const pm_embedded_statements_node_t *) part;
422 string = pm_static_literal_concat(iseq, &cast->statements->body, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
423 break;
425 default:
426 RUBY_ASSERT(false && "unexpected node type in pm_static_literal_concat");
427 return Qnil;
430 if (current != Qnil) {
431 current = rb_str_concat(current, string);
433 else {
434 current = string;
438 return top ? rb_fstring(current) : current;
441 #define RE_OPTION_ENCODING_SHIFT 8
442 #define RE_OPTION_ENCODING(encoding) (((encoding) & 0xFF) << RE_OPTION_ENCODING_SHIFT)
443 #define ARG_ENCODING_NONE 32
444 #define ARG_ENCODING_FIXED 16
445 #define ENC_ASCII8BIT 1
446 #define ENC_EUC_JP 2
447 #define ENC_Windows_31J 3
448 #define ENC_UTF8 4
451 * Check the prism flags of a regular expression-like node and return the flags
452 * that are expected by the CRuby VM.
454 static int
455 parse_regexp_flags(const pm_node_t *node)
457 int flags = 0;
459 // Check "no encoding" first so that flags don't get clobbered
460 // We're calling `rb_char_to_option_kcode` in this case so that
461 // we don't need to have access to `ARG_ENCODING_NONE`
462 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
463 flags |= ARG_ENCODING_NONE;
466 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
467 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_EUC_JP));
470 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
471 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_Windows_31J));
474 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
475 flags |= (ARG_ENCODING_FIXED | RE_OPTION_ENCODING(ENC_UTF8));
478 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) {
479 flags |= ONIG_OPTION_IGNORECASE;
482 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) {
483 flags |= ONIG_OPTION_MULTILINE;
486 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) {
487 flags |= ONIG_OPTION_EXTEND;
490 return flags;
493 #undef RE_OPTION_ENCODING_SHIFT
494 #undef RE_OPTION_ENCODING
495 #undef ARG_ENCODING_FIXED
496 #undef ARG_ENCODING_NONE
497 #undef ENC_ASCII8BIT
498 #undef ENC_EUC_JP
499 #undef ENC_Windows_31J
500 #undef ENC_UTF8
502 static rb_encoding *
503 parse_regexp_encoding(const pm_scope_node_t *scope_node, const pm_node_t *node)
505 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
506 return rb_ascii8bit_encoding();
508 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
509 return rb_utf8_encoding();
511 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EUC_JP)) {
512 return rb_enc_get_from_index(ENCINDEX_EUC_JP);
514 else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J)) {
515 return rb_enc_get_from_index(ENCINDEX_Windows_31J);
517 else {
518 return NULL;
522 static VALUE
523 parse_regexp(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, VALUE string)
525 VALUE errinfo = rb_errinfo();
527 int32_t line_number = pm_node_line_number(scope_node->parser, node);
528 VALUE regexp = rb_reg_compile(string, parse_regexp_flags(node), (const char *) pm_string_source(&scope_node->parser->filepath), line_number);
530 if (NIL_P(regexp)) {
531 VALUE message = rb_attr_get(rb_errinfo(), idMesg);
532 rb_set_errinfo(errinfo);
534 parse_regexp_error(iseq, line_number, "%" PRIsVALUE, message);
535 return Qnil;
538 rb_obj_freeze(regexp);
539 return regexp;
542 static inline VALUE
543 parse_regexp_literal(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped)
545 rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
546 if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
548 VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), regexp_encoding);
549 return parse_regexp(iseq, scope_node, node, string);
552 static inline VALUE
553 parse_regexp_concat(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_node_list_t *parts)
555 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
556 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
558 VALUE string = pm_static_literal_concat(iseq, parts, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false);
559 return parse_regexp(iseq, scope_node, node, string);
562 static 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);
564 static int
565 pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const pm_line_column_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)
567 int stack_size = 0;
568 size_t parts_size = parts->size;
569 bool interpolated = false;
571 if (parts_size > 0) {
572 VALUE current_string = Qnil;
574 for (size_t index = 0; index < parts_size; index++) {
575 const pm_node_t *part = parts->nodes[index];
577 if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
578 const pm_string_node_t *string_node = (const pm_string_node_t *) part;
579 VALUE string_value;
581 if (implicit_regexp_encoding == NULL) {
582 string_value = parse_string_encoded(part, &string_node->unescaped, scope_node->encoding);
584 else {
585 string_value = parse_regexp_string_part(iseq, scope_node, (const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
588 if (RTEST(current_string)) {
589 current_string = rb_str_concat(current_string, string_value);
591 else {
592 current_string = string_value;
595 else {
596 interpolated = true;
598 if (
599 PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE) &&
600 ((const pm_embedded_statements_node_t *) part)->statements != NULL &&
601 ((const pm_embedded_statements_node_t *) part)->statements->body.size == 1 &&
602 PM_NODE_TYPE_P(((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0], PM_STRING_NODE)
604 const pm_string_node_t *string_node = (const pm_string_node_t *) ((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0];
605 VALUE string_value;
607 if (implicit_regexp_encoding == NULL) {
608 string_value = parse_string_encoded(part, &string_node->unescaped, scope_node->encoding);
610 else {
611 string_value = parse_regexp_string_part(iseq, scope_node, (const pm_node_t *) string_node, &string_node->unescaped, implicit_regexp_encoding, explicit_regexp_encoding);
614 if (RTEST(current_string)) {
615 current_string = rb_str_concat(current_string, string_value);
617 else {
618 current_string = string_value;
621 else {
622 if (!RTEST(current_string)) {
623 rb_encoding *encoding;
625 if (implicit_regexp_encoding != NULL) {
626 if (explicit_regexp_encoding != NULL) {
627 encoding = explicit_regexp_encoding;
629 else if (scope_node->parser->encoding == PM_ENCODING_US_ASCII_ENTRY) {
630 encoding = rb_ascii8bit_encoding();
632 else {
633 encoding = implicit_regexp_encoding;
636 else {
637 encoding = scope_node->encoding;
640 current_string = rb_enc_str_new(NULL, 0, encoding);
643 PUSH_INSN1(ret, *node_location, putobject, rb_fstring(current_string));
644 PM_COMPILE_NOT_POPPED(part);
645 PUSH_INSN(ret, *node_location, dup);
646 PUSH_INSN1(ret, *node_location, objtostring, new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE , NULL, FALSE));
647 PUSH_INSN(ret, *node_location, anytostring);
649 current_string = Qnil;
650 stack_size += 2;
655 if (RTEST(current_string)) {
656 current_string = rb_fstring(current_string);
658 if (stack_size == 0 && interpolated) {
659 PUSH_INSN1(ret, *node_location, putstring, current_string);
661 else {
662 PUSH_INSN1(ret, *node_location, putobject, current_string);
665 current_string = Qnil;
666 stack_size++;
669 else {
670 PUSH_INSN(ret, *node_location, putnil);
673 return stack_size;
676 static void
677 pm_compile_regexp_dynamic(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *parts, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
679 rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
680 rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
682 int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding);
683 PUSH_INSN2(ret, *node_location, toregexp, INT2FIX(parse_regexp_flags(node) & 0xFF), INT2FIX(length));
686 static VALUE
687 pm_source_file_value(const pm_source_file_node_t *node, const pm_scope_node_t *scope_node)
689 const pm_string_t *filepath = &node->filepath;
690 size_t length = pm_string_length(filepath);
692 if (length > 0) {
693 rb_encoding *filepath_encoding = scope_node->filepath_encoding != NULL ? scope_node->filepath_encoding : rb_utf8_encoding();
694 return rb_enc_interned_str((const char *) pm_string_source(filepath), length, filepath_encoding);
696 else {
697 return rb_fstring_lit("<compiled>");
702 * Return a static literal string, optionally with attached debugging
703 * information.
705 static VALUE
706 pm_static_literal_string(rb_iseq_t *iseq, VALUE string, int line_number)
708 if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
709 VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq), INT2FIX(line_number));
710 rb_ivar_set(string, id_debug_created_info, rb_obj_freeze(debug_info));
711 return rb_str_freeze(string);
713 else {
714 return rb_fstring(string);
719 * Certain nodes can be compiled literally. This function returns the literal
720 * value described by the given node. For example, an array node with all static
721 * literal values can be compiled into a literal array.
723 static VALUE
724 pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_node_t *scope_node)
726 // Every node that comes into this function should already be marked as
727 // static literal. If it's not, then we have a bug somewhere.
728 RUBY_ASSERT(PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL));
730 switch (PM_NODE_TYPE(node)) {
731 case PM_ARRAY_NODE: {
732 const pm_array_node_t *cast = (const pm_array_node_t *) node;
733 const pm_node_list_t *elements = &cast->elements;
735 VALUE value = rb_ary_hidden_new(elements->size);
736 for (size_t index = 0; index < elements->size; index++) {
737 rb_ary_push(value, pm_static_literal_value(iseq, elements->nodes[index], scope_node));
740 OBJ_FREEZE(value);
741 return value;
743 case PM_FALSE_NODE:
744 return Qfalse;
745 case PM_FLOAT_NODE:
746 return parse_float((const pm_float_node_t *) node);
747 case PM_HASH_NODE: {
748 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
749 const pm_node_list_t *elements = &cast->elements;
751 VALUE array = rb_ary_hidden_new(elements->size * 2);
752 for (size_t index = 0; index < elements->size; index++) {
753 RUBY_ASSERT(PM_NODE_TYPE_P(elements->nodes[index], PM_ASSOC_NODE));
754 const pm_assoc_node_t *cast = (const pm_assoc_node_t *) elements->nodes[index];
755 VALUE pair[2] = { pm_static_literal_value(iseq, cast->key, scope_node), pm_static_literal_value(iseq, cast->value, scope_node) };
756 rb_ary_cat(array, pair, 2);
759 VALUE value = rb_hash_new_with_size(elements->size);
760 rb_hash_bulk_insert(RARRAY_LEN(array), RARRAY_CONST_PTR(array), value);
762 value = rb_obj_hide(value);
763 OBJ_FREEZE(value);
764 return value;
766 case PM_IMAGINARY_NODE:
767 return parse_imaginary((const pm_imaginary_node_t *) node);
768 case PM_INTEGER_NODE:
769 return parse_integer((const pm_integer_node_t *) node);
770 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
771 const pm_interpolated_match_last_line_node_t *cast = (const pm_interpolated_match_last_line_node_t *) node;
772 return parse_regexp_concat(iseq, scope_node, (const pm_node_t *) cast, &cast->parts);
774 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
775 const pm_interpolated_regular_expression_node_t *cast = (const pm_interpolated_regular_expression_node_t *) node;
776 return parse_regexp_concat(iseq, scope_node, (const pm_node_t *) cast, &cast->parts);
778 case PM_INTERPOLATED_STRING_NODE: {
779 VALUE string = pm_static_literal_concat(iseq, &((const pm_interpolated_string_node_t *) node)->parts, scope_node, NULL, NULL, false);
780 int line_number = pm_node_line_number(scope_node->parser, node);
781 return pm_static_literal_string(iseq, string, line_number);
783 case PM_INTERPOLATED_SYMBOL_NODE: {
784 const pm_interpolated_symbol_node_t *cast = (const pm_interpolated_symbol_node_t *) node;
785 VALUE string = pm_static_literal_concat(iseq, &cast->parts, scope_node, NULL, NULL, true);
787 return ID2SYM(rb_intern_str(string));
789 case PM_MATCH_LAST_LINE_NODE: {
790 const pm_match_last_line_node_t *cast = (const pm_match_last_line_node_t *) node;
791 return parse_regexp_literal(iseq, scope_node, (const pm_node_t *) cast, &cast->unescaped);
793 case PM_NIL_NODE:
794 return Qnil;
795 case PM_RATIONAL_NODE:
796 return parse_rational((const pm_rational_node_t *) node);
797 case PM_REGULAR_EXPRESSION_NODE: {
798 const pm_regular_expression_node_t *cast = (const pm_regular_expression_node_t *) node;
799 return parse_regexp_literal(iseq, scope_node, (const pm_node_t *) cast, &cast->unescaped);
801 case PM_SOURCE_ENCODING_NODE:
802 return rb_enc_from_encoding(scope_node->encoding);
803 case PM_SOURCE_FILE_NODE: {
804 const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
805 return pm_source_file_value(cast, scope_node);
807 case PM_SOURCE_LINE_NODE:
808 return INT2FIX(pm_node_line_number(scope_node->parser, node));
809 case PM_STRING_NODE: {
810 const pm_string_node_t *cast = (const pm_string_node_t *) node;
811 return parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
813 case PM_SYMBOL_NODE:
814 return ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) node));
815 case PM_TRUE_NODE:
816 return Qtrue;
817 default:
818 rb_bug("Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
819 return Qfalse;
824 * A helper for converting a pm_location_t into a rb_code_location_t.
826 static rb_code_location_t
827 pm_code_location(const pm_scope_node_t *scope_node, const pm_node_t *node)
829 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
830 const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, node);
832 return (rb_code_location_t) {
833 .beg_pos = { .lineno = start_location.line, .column = start_location.column },
834 .end_pos = { .lineno = end_location.line, .column = end_location.column }
839 * A macro for determining if we should go through the work of adding branch
840 * coverage to the current iseq. We check this manually each time because we
841 * want to avoid the overhead of creating rb_code_location_t objects.
843 #define PM_BRANCH_COVERAGE_P(iseq) (ISEQ_COVERAGE(iseq) && ISEQ_BRANCH_COVERAGE(iseq))
845 static void
846 pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
847 LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node);
849 static void
850 pm_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)
852 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, cond);
854 DECL_ANCHOR(seq);
855 INIT_ANCHOR(seq);
857 LABEL *label = NEW_LABEL(location.line);
858 if (!then_label) then_label = label;
859 else if (!else_label) else_label = label;
861 pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, popped, scope_node);
863 if (LIST_INSN_SIZE_ONE(seq)) {
864 INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
865 if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label) return;
868 if (!label->refcnt) {
869 if (popped) PUSH_INSN(ret, location, putnil);
871 else {
872 PUSH_LABEL(seq, label);
875 PUSH_SEQ(ret, seq);
876 return;
879 static void
880 pm_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)
882 const pm_line_column_t location = { .line = ISEQ_BODY(iseq)->location.first_lineno, .column = -1 };
884 if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
885 PM_COMPILE_NOT_POPPED(node);
886 PUSH_INSN1(ret, location, getglobal, ID2SYM(rb_intern("$.")));
887 PUSH_SEND(ret, location, idEq, INT2FIX(1));
888 if (popped) PUSH_INSN(ret, location, pop);
890 else {
891 PM_COMPILE(node);
895 static void
896 pm_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)
898 const pm_line_column_t location = { .line = ISEQ_BODY(iseq)->location.first_lineno, .column = -1 };
899 LABEL *lend = NEW_LABEL(location.line);
901 int again = !(flip_flop_node->base.flags & PM_RANGE_FLAGS_EXCLUDE_END);
903 rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
904 VALUE key = INT2FIX(count);
906 PUSH_INSN2(ret, location, getspecial, key, INT2FIX(0));
907 PUSH_INSNL(ret, location, branchif, lend);
909 if (flip_flop_node->left) {
910 pm_compile_flip_flop_bound(iseq, flip_flop_node->left, ret, popped, scope_node);
912 else {
913 PUSH_INSN(ret, location, putnil);
916 PUSH_INSNL(ret, location, branchunless, else_label);
917 PUSH_INSN1(ret, location, putobject, Qtrue);
918 PUSH_INSN1(ret, location, setspecial, key);
919 if (!again) {
920 PUSH_INSNL(ret, location, jump, then_label);
923 PUSH_LABEL(ret, lend);
924 if (flip_flop_node->right) {
925 pm_compile_flip_flop_bound(iseq, flip_flop_node->right, ret, popped, scope_node);
927 else {
928 PUSH_INSN(ret, location, putnil);
931 PUSH_INSNL(ret, location, branchunless, then_label);
932 PUSH_INSN1(ret, location, putobject, Qfalse);
933 PUSH_INSN1(ret, location, setspecial, key);
934 PUSH_INSNL(ret, location, jump, then_label);
937 static void pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition);
939 static void
940 pm_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)
942 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, cond);
944 again:
945 switch (PM_NODE_TYPE(cond)) {
946 case PM_AND_NODE: {
947 const pm_and_node_t *cast = (const pm_and_node_t *) cond;
948 pm_compile_logical(iseq, ret, cast->left, NULL, else_label, popped, scope_node);
950 cond = cast->right;
951 goto again;
953 case PM_OR_NODE: {
954 const pm_or_node_t *cast = (const pm_or_node_t *) cond;
955 pm_compile_logical(iseq, ret, cast->left, then_label, NULL, popped, scope_node);
957 cond = cast->right;
958 goto again;
960 case PM_FALSE_NODE:
961 case PM_NIL_NODE:
962 PUSH_INSNL(ret, location, jump, else_label);
963 return;
964 case PM_FLOAT_NODE:
965 case PM_IMAGINARY_NODE:
966 case PM_INTEGER_NODE:
967 case PM_LAMBDA_NODE:
968 case PM_RATIONAL_NODE:
969 case PM_REGULAR_EXPRESSION_NODE:
970 case PM_STRING_NODE:
971 case PM_SYMBOL_NODE:
972 case PM_TRUE_NODE:
973 PUSH_INSNL(ret, location, jump, then_label);
974 return;
975 case PM_FLIP_FLOP_NODE:
976 pm_compile_flip_flop((const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.line, ret, popped, scope_node);
977 return;
978 case PM_DEFINED_NODE: {
979 const pm_defined_node_t *cast = (const pm_defined_node_t *) cond;
980 pm_compile_defined_expr(iseq, cast->value, &location, ret, popped, scope_node, true);
981 break;
983 default: {
984 pm_compile_node(iseq, cond, ret, false, scope_node);
985 break;
989 PUSH_INSNL(ret, location, branchunless, else_label);
990 PUSH_INSNL(ret, location, jump, then_label);
994 * Compile an if or unless node.
996 static void
997 pm_compile_conditional(rb_iseq_t *iseq, const pm_line_column_t *line_column, pm_node_type_t type, const pm_node_t *node, const pm_statements_node_t *statements, const pm_node_t *consequent, const pm_node_t *predicate, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
999 const pm_line_column_t location = *line_column;
1000 LABEL *then_label = NEW_LABEL(location.line);
1001 LABEL *else_label = NEW_LABEL(location.line);
1002 LABEL *end_label = NULL;
1004 pm_compile_branch_condition(iseq, ret, predicate, then_label, else_label, false, scope_node);
1006 rb_code_location_t conditional_location;
1007 VALUE branches = Qfalse;
1009 if (then_label->refcnt && else_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1010 conditional_location = pm_code_location(scope_node, node);
1011 branches = decl_branch_base(iseq, PTR2NUM(node), &conditional_location, type == PM_IF_NODE ? "if" : "unless");
1014 if (then_label->refcnt) {
1015 PUSH_LABEL(ret, then_label);
1017 DECL_ANCHOR(then_seq);
1018 INIT_ANCHOR(then_seq);
1020 if (statements != NULL) {
1021 pm_compile_node(iseq, (const pm_node_t *) statements, then_seq, popped, scope_node);
1023 else if (!popped) {
1024 PUSH_SYNTHETIC_PUTNIL(then_seq, iseq);
1027 if (else_label->refcnt) {
1028 // Establish branch coverage for the then block.
1029 if (PM_BRANCH_COVERAGE_P(iseq)) {
1030 rb_code_location_t branch_location;
1032 if (statements != NULL) {
1033 branch_location = pm_code_location(scope_node, (const pm_node_t *) statements);
1034 } else if (type == PM_IF_NODE) {
1035 pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(scope_node->parser, predicate);
1036 branch_location = (rb_code_location_t) {
1037 .beg_pos = { .lineno = predicate_end.line, .column = predicate_end.column },
1038 .end_pos = { .lineno = predicate_end.line, .column = predicate_end.column }
1040 } else {
1041 branch_location = conditional_location;
1044 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0, type == PM_IF_NODE ? "then" : "else", branches);
1047 end_label = NEW_LABEL(location.line);
1048 PUSH_INSNL(then_seq, location, jump, end_label);
1049 if (!popped) PUSH_INSN(then_seq, location, pop);
1052 PUSH_SEQ(ret, then_seq);
1055 if (else_label->refcnt) {
1056 PUSH_LABEL(ret, else_label);
1058 DECL_ANCHOR(else_seq);
1059 INIT_ANCHOR(else_seq);
1061 if (consequent != NULL) {
1062 pm_compile_node(iseq, consequent, else_seq, popped, scope_node);
1064 else if (!popped) {
1065 PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
1068 // Establish branch coverage for the else block.
1069 if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
1070 rb_code_location_t branch_location;
1072 if (consequent == NULL) {
1073 branch_location = conditional_location;
1074 } else if (PM_NODE_TYPE_P(consequent, PM_ELSE_NODE)) {
1075 const pm_else_node_t *else_node = (const pm_else_node_t *) consequent;
1076 branch_location = pm_code_location(scope_node, else_node->statements != NULL ? ((const pm_node_t *) else_node->statements) : (const pm_node_t *) else_node);
1077 } else {
1078 branch_location = pm_code_location(scope_node, (const pm_node_t *) consequent);
1081 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1, type == PM_IF_NODE ? "else" : "then", branches);
1084 PUSH_SEQ(ret, else_seq);
1087 if (end_label) {
1088 PUSH_LABEL(ret, end_label);
1091 return;
1095 * Compile a while or until loop.
1097 static void
1098 pm_compile_loop(rb_iseq_t *iseq, const pm_line_column_t *line_column, 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)
1100 const pm_line_column_t location = *line_column;
1102 LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
1103 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
1104 LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
1106 // TODO: Deal with ensures in here
1107 LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.line); /* next */
1108 LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.line); /* redo */
1109 LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.line); /* break */
1110 LABEL *end_label = NEW_LABEL(location.line);
1111 LABEL *adjust_label = NEW_LABEL(location.line);
1113 LABEL *next_catch_label = NEW_LABEL(location.line);
1114 LABEL *tmp_label = NULL;
1116 // begin; end while true
1117 if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
1118 tmp_label = NEW_LABEL(location.line);
1119 PUSH_INSNL(ret, location, jump, tmp_label);
1121 else {
1122 // while true; end
1123 PUSH_INSNL(ret, location, jump, next_label);
1126 PUSH_LABEL(ret, adjust_label);
1127 PUSH_INSN(ret, location, putnil);
1128 PUSH_LABEL(ret, next_catch_label);
1129 PUSH_INSN(ret, location, pop);
1130 PUSH_INSNL(ret, location, jump, next_label);
1131 if (tmp_label) PUSH_LABEL(ret, tmp_label);
1133 PUSH_LABEL(ret, redo_label);
1135 // Establish branch coverage for the loop.
1136 if (PM_BRANCH_COVERAGE_P(iseq)) {
1137 rb_code_location_t loop_location = pm_code_location(scope_node, node);
1138 VALUE branches = decl_branch_base(iseq, PTR2NUM(node), &loop_location, type == PM_WHILE_NODE ? "while" : "until");
1140 rb_code_location_t branch_location = statements != NULL ? pm_code_location(scope_node, (const pm_node_t *) statements) : loop_location;
1141 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 0, "body", branches);
1144 if (statements != NULL) PM_COMPILE_POPPED((const pm_node_t *) statements);
1145 PUSH_LABEL(ret, next_label);
1147 if (type == PM_WHILE_NODE) {
1148 pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, popped, scope_node);
1150 else if (type == PM_UNTIL_NODE) {
1151 pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, popped, scope_node);
1154 PUSH_LABEL(ret, end_label);
1155 PUSH_ADJUST_RESTORE(ret, adjust_label);
1156 PUSH_INSN(ret, location, putnil);
1158 PUSH_LABEL(ret, break_label);
1159 if (popped) PUSH_INSN(ret, location, pop);
1161 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
1162 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
1163 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL, ISEQ_COMPILE_DATA(iseq)->redo_label);
1165 ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
1166 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
1167 ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
1168 return;
1171 // This recurses through scopes and finds the local index at any scope level
1172 // It also takes a pointer to depth, and increments depth appropriately
1173 // according to the depth of the local.
1174 static pm_local_index_t
1175 pm_lookup_local_index(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, pm_constant_id_t constant_id, int start_depth)
1177 pm_local_index_t lindex = { 0 };
1178 st_data_t local_index;
1180 int level;
1181 for (level = 0; level < start_depth; level++) {
1182 scope_node = scope_node->previous;
1185 while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
1186 level++;
1188 if (scope_node->previous) {
1189 scope_node = scope_node->previous;
1191 else {
1192 // We have recursed up all scope nodes
1193 // and have not found the local yet
1194 rb_bug("Local with constant_id %u does not exist", (unsigned int) constant_id);
1198 lindex.level = level;
1199 lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
1200 return lindex;
1203 // This returns the CRuby ID which maps to the pm_constant_id_t
1205 // Constant_ids in prism are indexes of the constants in prism's constant pool.
1206 // We add a constants mapping on the scope_node which is a mapping from
1207 // these constant_id indexes to the CRuby IDs that they represent.
1208 // This helper method allows easy access to those IDs
1209 static ID
1210 pm_constant_id_lookup(const pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
1212 if (constant_id < 1 || constant_id > scope_node->parser->constant_pool.size) {
1213 rb_bug("constant_id out of range: %u", (unsigned int)constant_id);
1215 return scope_node->constants[constant_id - 1];
1218 static rb_iseq_t *
1219 pm_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)
1221 debugs("[new_child_iseq]> ---------------------------------------\n");
1222 int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
1223 rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
1224 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1225 line_no, parent,
1226 isolated_depth ? isolated_depth + 1 : 0,
1227 type, ISEQ_COMPILE_DATA(iseq)->option);
1228 debugs("[new_child_iseq]< ---------------------------------------\n");
1229 return ret_iseq;
1232 static int
1233 pm_compile_class_path(rb_iseq_t *iseq, const pm_node_t *node, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
1235 if (PM_NODE_TYPE_P(node, PM_CONSTANT_PATH_NODE)) {
1236 const pm_node_t *parent = ((const pm_constant_path_node_t *) node)->parent;
1238 if (parent) {
1239 /* Bar::Foo */
1240 PM_COMPILE(parent);
1241 return VM_DEFINECLASS_FLAG_SCOPED;
1243 else {
1244 /* toplevel class ::Foo */
1245 PUSH_INSN1(ret, *node_location, putobject, rb_cObject);
1246 return VM_DEFINECLASS_FLAG_SCOPED;
1249 else {
1250 /* class at cbase Foo */
1251 PUSH_INSN1(ret, *node_location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
1252 return 0;
1257 * Compile either a call and write node or a call or write node. These look like
1258 * method calls that are followed by a ||= or &&= operator.
1260 static void
1261 pm_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_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
1263 const pm_line_column_t location = *node_location;
1264 LABEL *lfin = NEW_LABEL(location.line);
1265 LABEL *lcfin = NEW_LABEL(location.line);
1266 LABEL *lskip = NULL;
1268 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1269 ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
1271 PM_COMPILE_NOT_POPPED(receiver);
1272 if (safe_nav) {
1273 lskip = NEW_LABEL(location.line);
1274 PUSH_INSN(ret, location, dup);
1275 PUSH_INSNL(ret, location, branchnil, lskip);
1278 PUSH_INSN(ret, location, dup);
1279 PUSH_SEND_WITH_FLAG(ret, location, id_read_name, INT2FIX(0), INT2FIX(flag));
1280 if (!popped) PUSH_INSN(ret, location, dup);
1282 if (and_node) {
1283 PUSH_INSNL(ret, location, branchunless, lcfin);
1285 else {
1286 PUSH_INSNL(ret, location, branchif, lcfin);
1289 if (!popped) PUSH_INSN(ret, location, pop);
1290 PM_COMPILE_NOT_POPPED(value);
1292 if (!popped) {
1293 PUSH_INSN(ret, location, swap);
1294 PUSH_INSN1(ret, location, topn, INT2FIX(1));
1297 ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
1298 PUSH_SEND_WITH_FLAG(ret, location, id_write_name, INT2FIX(1), INT2FIX(flag));
1299 PUSH_INSNL(ret, location, jump, lfin);
1301 PUSH_LABEL(ret, lcfin);
1302 if (!popped) PUSH_INSN(ret, location, swap);
1304 PUSH_LABEL(ret, lfin);
1306 if (lskip && popped) PUSH_LABEL(ret, lskip);
1307 PUSH_INSN(ret, location, pop);
1308 if (lskip && !popped) PUSH_LABEL(ret, lskip);
1312 * This function compiles a hash onto the stack. It is used to compile hash
1313 * literals and keyword arguments. It is assumed that if we get here that the
1314 * contents of the hash are not popped.
1316 static void
1317 pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *elements, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
1319 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
1321 // If this element is not popped, then we need to create the hash on the
1322 // stack. Neighboring plain assoc nodes should be grouped together (either
1323 // by newhash or hash merge). Double splat nodes should be merged using the
1324 // merge_kwd method call.
1325 int assoc_length = 0;
1326 bool made_hash = false;
1328 for (size_t index = 0; index < elements->size; index++) {
1329 const pm_node_t *element = elements->nodes[index];
1331 switch (PM_NODE_TYPE(element)) {
1332 case PM_ASSOC_NODE: {
1333 // If this is a plain assoc node, then we can compile it directly
1334 // and then add to the number of assoc nodes we've seen so far.
1335 PM_COMPILE_NOT_POPPED(element);
1336 assoc_length++;
1337 break;
1339 case PM_ASSOC_SPLAT_NODE: {
1340 // If we are at a splat and we have already compiled some elements
1341 // of the hash, then we need to either create the first hash or
1342 // merge the current elements into the existing hash.
1343 if (assoc_length > 0) {
1344 if (!made_hash) {
1345 PUSH_INSN1(ret, location, newhash, INT2FIX(assoc_length * 2));
1346 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1347 PUSH_INSN(ret, location, swap);
1348 made_hash = true;
1350 else {
1351 // Here we are merging plain assoc nodes into the hash on
1352 // the stack.
1353 PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(assoc_length * 2 + 1));
1355 // Since we already have a hash on the stack, we need to set
1356 // up the method call for the next merge that will occur.
1357 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1358 PUSH_INSN(ret, location, swap);
1361 assoc_length = 0;
1364 // If this is the first time we've seen a splat, then we need to
1365 // create a hash that we can merge into.
1366 if (!made_hash) {
1367 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1368 PUSH_INSN1(ret, location, newhash, INT2FIX(0));
1369 made_hash = true;
1372 // Now compile the splat node itself and merge it into the hash.
1373 PM_COMPILE_NOT_POPPED(element);
1374 PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
1376 // We know that any subsequent elements will need to be merged in
1377 // using one of the special core methods. So here we will put the
1378 // receiver of the merge and then swap it with the hash that is
1379 // going to be the first argument.
1380 if (index != elements->size - 1) {
1381 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1382 PUSH_INSN(ret, location, swap);
1385 break;
1387 default:
1388 RUBY_ASSERT("Invalid node type for hash" && false);
1389 break;
1393 if (!made_hash) {
1394 // If we haven't already made the hash, then this means we only saw
1395 // plain assoc nodes. In this case, we can just create the hash
1396 // directly.
1397 PUSH_INSN1(ret, location, newhash, INT2FIX(assoc_length * 2));
1399 else if (assoc_length > 0) {
1400 // If we have already made the hash, then we need to merge the remaining
1401 // assoc nodes into the hash on the stack.
1402 PUSH_SEND(ret, location, id_core_hash_merge_ptr, INT2FIX(assoc_length * 2 + 1));
1406 // This is details. Users should call pm_setup_args() instead.
1407 static int
1408 pm_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, rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_line_column_t *node_location)
1410 const pm_line_column_t location = *node_location;
1412 int orig_argc = 0;
1413 bool has_splat = false;
1414 bool has_keyword_splat = false;
1416 if (arguments_node == NULL) {
1417 if (*flags & VM_CALL_FCALL) {
1418 *flags |= VM_CALL_VCALL;
1421 else {
1422 const pm_node_list_t *arguments = &arguments_node->arguments;
1423 has_keyword_splat = PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
1425 // We count the number of elements post the splat node that are not keyword elements to
1426 // eventually pass as an argument to newarray
1427 int post_splat_counter = 0;
1428 const pm_node_t *argument;
1430 PM_NODE_LIST_FOREACH(arguments, index, argument) {
1431 switch (PM_NODE_TYPE(argument)) {
1432 // A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
1433 case PM_KEYWORD_HASH_NODE: {
1434 const pm_keyword_hash_node_t *keyword_arg = (const pm_keyword_hash_node_t *) argument;
1435 const pm_node_list_t *elements = &keyword_arg->elements;
1437 if (has_keyword_splat || has_splat) {
1438 *flags |= VM_CALL_KW_SPLAT;
1439 has_keyword_splat = true;
1440 pm_compile_hash_elements(iseq, argument, elements, ret, scope_node);
1442 else {
1443 // We need to first figure out if all elements of the
1444 // KeywordHashNode are AssocNodes with symbol keys.
1445 if (PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS)) {
1446 // If they are all symbol keys then we can pass them as
1447 // keyword arguments. The first thing we need to do is
1448 // deduplicate. We'll do this using the combination of a
1449 // Ruby hash and a Ruby array.
1450 VALUE stored_indices = rb_hash_new();
1451 VALUE keyword_indices = rb_ary_new_capa(elements->size);
1453 size_t size = 0;
1454 for (size_t element_index = 0; element_index < elements->size; element_index++) {
1455 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
1457 // Retrieve the stored index from the hash for this
1458 // keyword.
1459 VALUE keyword = pm_static_literal_value(iseq, assoc->key, scope_node);
1460 VALUE stored_index = rb_hash_aref(stored_indices, keyword);
1462 // If this keyword was already seen in the hash,
1463 // then mark the array at that index as false and
1464 // decrement the keyword size.
1465 if (!NIL_P(stored_index)) {
1466 rb_ary_store(keyword_indices, NUM2LONG(stored_index), Qfalse);
1467 size--;
1470 // Store (and possibly overwrite) the index for this
1471 // keyword in the hash, mark the array at that index
1472 // as true, and increment the keyword size.
1473 rb_hash_aset(stored_indices, keyword, ULONG2NUM(element_index));
1474 rb_ary_store(keyword_indices, (long) element_index, Qtrue);
1475 size++;
1478 *kw_arg = rb_xmalloc_mul_add(size, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
1479 *flags |= VM_CALL_KWARG;
1481 VALUE *keywords = (*kw_arg)->keywords;
1482 (*kw_arg)->references = 0;
1483 (*kw_arg)->keyword_len = (int) size;
1485 size_t keyword_index = 0;
1486 for (size_t element_index = 0; element_index < elements->size; element_index++) {
1487 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
1488 bool popped = true;
1490 if (rb_ary_entry(keyword_indices, (long) element_index) == Qtrue) {
1491 keywords[keyword_index++] = pm_static_literal_value(iseq, assoc->key, scope_node);
1492 popped = false;
1495 PM_COMPILE(assoc->value);
1498 RUBY_ASSERT(keyword_index == size);
1500 else {
1501 // If they aren't all symbol keys then we need to
1502 // construct a new hash and pass that as an argument.
1503 orig_argc++;
1504 *flags |= VM_CALL_KW_SPLAT;
1506 size_t size = elements->size;
1507 if (size > 1) {
1508 // A new hash will be created for the keyword
1509 // arguments in this case, so mark the method as
1510 // passing mutable keyword splat.
1511 *flags |= VM_CALL_KW_SPLAT_MUT;
1514 for (size_t element_index = 0; element_index < size; element_index++) {
1515 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) elements->nodes[element_index];
1516 PM_COMPILE_NOT_POPPED(assoc->key);
1517 PM_COMPILE_NOT_POPPED(assoc->value);
1520 PUSH_INSN1(ret, location, newhash, INT2FIX(size * 2));
1523 break;
1525 case PM_SPLAT_NODE: {
1526 *flags |= VM_CALL_ARGS_SPLAT;
1527 const pm_splat_node_t *splat_node = (const pm_splat_node_t *) argument;
1529 if (splat_node->expression) {
1530 PM_COMPILE_NOT_POPPED(splat_node->expression);
1532 else {
1533 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1534 PUSH_GETLOCAL(ret, location, index.index, index.level);
1537 bool first_splat = !has_splat;
1539 if (first_splat) {
1540 // If this is the first splat array seen and it's not the
1541 // last parameter, we want splatarray to dup it.
1543 // foo(a, *b, c)
1544 // ^^
1545 if (index + 1 < arguments->size || has_regular_blockarg) {
1546 PUSH_INSN1(ret, location, splatarray, Qtrue);
1547 *flags |= VM_CALL_ARGS_SPLAT_MUT;
1549 // If this is the first spalt array seen and it's the last
1550 // parameter, we don't want splatarray to dup it.
1552 // foo(a, *b)
1553 // ^^
1554 else {
1555 PUSH_INSN1(ret, location, splatarray, Qfalse);
1558 else {
1559 // If this is not the first splat array seen and it is also
1560 // the last parameter, we don't want splatarray to dup it
1561 // and we need to concat the array.
1563 // foo(a, *b, *c)
1564 // ^^
1565 PUSH_INSN1(ret, location, splatarray, Qfalse);
1566 PUSH_INSN(ret, location, concatarray);
1569 has_splat = true;
1570 post_splat_counter = 0;
1572 break;
1574 case PM_FORWARDING_ARGUMENTS_NODE: {
1575 orig_argc += 2;
1576 *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_SPLAT_MUT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
1578 // Forwarding arguments nodes are treated as foo(*, **, &)
1579 // So foo(...) equals foo(*, **, &) and as such the local
1580 // table for this method is known in advance
1582 // Push the *
1583 pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
1584 PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
1585 PUSH_INSN1(ret, location, splatarray, Qtrue);
1587 // Push the **
1588 pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
1589 PUSH_GETLOCAL(ret, location, pow_local.index, pow_local.level);
1591 // Push the &
1592 pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
1593 PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1), INT2FIX(and_local.level));
1594 PUSH_INSN(ret, location, splatkw);
1596 break;
1598 default: {
1599 post_splat_counter++;
1600 PM_COMPILE_NOT_POPPED(argument);
1602 // If we have a splat and we've seen a splat, we need to process
1603 // everything after the splat.
1604 if (has_splat) {
1605 // Stack items are turned into an array and concatenated in
1606 // the following cases:
1608 // If the next node is a splat:
1610 // foo(*a, b, *c)
1612 // If the next node is a kwarg or kwarg splat:
1614 // foo(*a, b, c: :d)
1615 // foo(*a, b, **c)
1617 // If the next node is NULL (we have hit the end):
1619 // foo(*a, b)
1620 if (index == arguments->size - 1) {
1621 RUBY_ASSERT(post_splat_counter > 0);
1622 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(post_splat_counter));
1624 else {
1625 pm_node_t *next_arg = arguments->nodes[index + 1];
1627 switch (PM_NODE_TYPE(next_arg)) {
1628 // A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
1629 case PM_KEYWORD_HASH_NODE: {
1630 PUSH_INSN1(ret, location, newarray, INT2FIX(post_splat_counter));
1631 PUSH_INSN(ret, location, concatarray);
1632 break;
1634 case PM_SPLAT_NODE: {
1635 PUSH_INSN1(ret, location, newarray, INT2FIX(post_splat_counter));
1636 PUSH_INSN(ret, location, concatarray);
1637 break;
1639 default:
1640 break;
1644 else {
1645 orig_argc++;
1652 if (has_splat) orig_argc++;
1653 if (has_keyword_splat) orig_argc++;
1654 return orig_argc;
1657 // Compile the argument parts of a call
1658 static int
1659 pm_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_line_column_t *node_location)
1661 if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
1662 // We compile the `&block_arg` expression first and stitch it later
1663 // since the nature of the expression influences whether splat should
1664 // duplicate the array.
1665 bool regular_block_arg = true;
1666 DECL_ANCHOR(block_arg);
1667 INIT_ANCHOR(block_arg);
1668 pm_compile_node(iseq, block, block_arg, false, scope_node);
1670 *flags |= VM_CALL_ARGS_BLOCKARG;
1672 if (LIST_INSN_SIZE_ONE(block_arg)) {
1673 LINK_ELEMENT *elem = FIRST_ELEMENT(block_arg);
1674 if (IS_INSN(elem)) {
1675 INSN *iobj = (INSN *) elem;
1676 if (iobj->insn_id == BIN(getblockparam)) {
1677 iobj->insn_id = BIN(getblockparamproxy);
1679 // Allow splat without duplication for simple one-instruction
1680 // block arguments like `&arg`. It is known that this optimization
1681 // can be too aggressive in some cases. See [Bug #16504].
1682 regular_block_arg = false;
1686 int argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, iseq, ret, scope_node, node_location);
1687 PUSH_SEQ(ret, block_arg);
1688 return argc;
1691 return pm_setup_args_core(arguments_node, block, flags, false, kw_arg, iseq, ret, scope_node, node_location);
1695 * Compile an index operator write node, which is a node that is writing a value
1696 * using the [] and []= methods. It looks like:
1698 * foo[bar] += baz
1700 * This breaks down to caching the receiver and arguments on the stack, calling
1701 * the [] method, calling the operator method with the result of the [] method,
1702 * and then calling the []= method with the result of the operator method.
1704 static void
1705 pm_compile_index_operator_write_node(rb_iseq_t *iseq, const pm_index_operator_write_node_t *node, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
1707 const pm_line_column_t location = *node_location;
1708 if (!popped) PUSH_INSN(ret, location, putnil);
1710 PM_COMPILE_NOT_POPPED(node->receiver);
1712 int boff = (node->block == NULL ? 0 : 1);
1713 int flag = PM_NODE_TYPE_P(node->receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1714 struct rb_callinfo_kwarg *keywords = NULL;
1715 int argc = pm_setup_args(node->arguments, node->block, &flag, &keywords, iseq, ret, scope_node, node_location);
1717 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
1718 if (boff) {
1719 PUSH_INSN(ret, location, splatkw);
1721 else {
1722 PUSH_INSN(ret, location, dup);
1723 PUSH_INSN(ret, location, splatkw);
1724 PUSH_INSN(ret, location, pop);
1728 int dup_argn = argc + 1 + boff;
1729 int keyword_len = 0;
1731 if (keywords) {
1732 keyword_len = keywords->keyword_len;
1733 dup_argn += keyword_len;
1736 PUSH_INSN1(ret, location, dupn, INT2FIX(dup_argn));
1737 PUSH_SEND_R(ret, location, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
1738 PM_COMPILE_NOT_POPPED(node->value);
1740 ID id_operator = pm_constant_id_lookup(scope_node, node->operator);
1741 PUSH_SEND(ret, location, id_operator, INT2FIX(1));
1743 if (!popped) {
1744 PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
1746 if (flag & VM_CALL_ARGS_SPLAT) {
1747 if (flag & VM_CALL_KW_SPLAT) {
1748 PUSH_INSN1(ret, location, topn, INT2FIX(2 + boff));
1750 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
1751 PUSH_INSN1(ret, location, splatarray, Qtrue);
1752 flag |= VM_CALL_ARGS_SPLAT_MUT;
1755 PUSH_INSN(ret, location, swap);
1756 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
1757 PUSH_INSN1(ret, location, setn, INT2FIX(2 + boff));
1758 PUSH_INSN(ret, location, pop);
1760 else {
1761 if (boff > 0) {
1762 PUSH_INSN1(ret, location, dupn, INT2FIX(3));
1763 PUSH_INSN(ret, location, swap);
1764 PUSH_INSN(ret, location, pop);
1766 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
1767 PUSH_INSN(ret, location, swap);
1768 PUSH_INSN1(ret, location, splatarray, Qtrue);
1769 PUSH_INSN(ret, location, swap);
1770 flag |= VM_CALL_ARGS_SPLAT_MUT;
1772 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
1773 if (boff > 0) {
1774 PUSH_INSN1(ret, location, setn, INT2FIX(3));
1775 PUSH_INSN(ret, location, pop);
1776 PUSH_INSN(ret, location, pop);
1780 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
1782 else if (flag & VM_CALL_KW_SPLAT) {
1783 if (boff > 0) {
1784 PUSH_INSN1(ret, location, topn, INT2FIX(2));
1785 PUSH_INSN(ret, location, swap);
1786 PUSH_INSN1(ret, location, setn, INT2FIX(3));
1787 PUSH_INSN(ret, location, pop);
1789 PUSH_INSN(ret, location, swap);
1790 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
1792 else if (keyword_len) {
1793 PUSH_INSN(ret, location, dup);
1794 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 2));
1795 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 1));
1796 PUSH_INSN(ret, location, pop);
1797 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
1799 else {
1800 if (boff > 0) {
1801 PUSH_INSN(ret, location, swap);
1803 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
1806 PUSH_INSN(ret, location, pop);
1810 * Compile an index control flow write node, which is a node that is writing a
1811 * value using the [] and []= methods and the &&= and ||= operators. It looks
1812 * like:
1814 * foo[bar] ||= baz
1816 * This breaks down to caching the receiver and arguments on the stack, calling
1817 * the [] method, checking the result and then changing control flow based on
1818 * it. If the value would result in a write, then the value is written using the
1819 * []= method.
1821 static void
1822 pm_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_node_t *block, const pm_node_t *value, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
1824 const pm_line_column_t location = *node_location;
1825 if (!popped) PUSH_INSN(ret, location, putnil);
1826 PM_COMPILE_NOT_POPPED(receiver);
1828 int boff = (block == NULL ? 0 : 1);
1829 int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
1830 struct rb_callinfo_kwarg *keywords = NULL;
1831 int argc = pm_setup_args(arguments, block, &flag, &keywords, iseq, ret, scope_node, node_location);
1833 if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
1834 if (boff) {
1835 PUSH_INSN(ret, location, splatkw);
1837 else {
1838 PUSH_INSN(ret, location, dup);
1839 PUSH_INSN(ret, location, splatkw);
1840 PUSH_INSN(ret, location, pop);
1844 int dup_argn = argc + 1 + boff;
1845 int keyword_len = 0;
1847 if (keywords) {
1848 keyword_len = keywords->keyword_len;
1849 dup_argn += keyword_len;
1852 PUSH_INSN1(ret, location, dupn, INT2FIX(dup_argn));
1853 PUSH_SEND_R(ret, location, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
1855 LABEL *label = NEW_LABEL(location.line);
1856 LABEL *lfin = NEW_LABEL(location.line);
1858 PUSH_INSN(ret, location, dup);
1859 if (PM_NODE_TYPE_P(node, PM_INDEX_AND_WRITE_NODE)) {
1860 PUSH_INSNL(ret, location, branchunless, label);
1862 else {
1863 PUSH_INSNL(ret, location, branchif, label);
1866 PUSH_INSN(ret, location, pop);
1867 PM_COMPILE_NOT_POPPED(value);
1869 if (!popped) {
1870 PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
1873 if (flag & VM_CALL_ARGS_SPLAT) {
1874 if (flag & VM_CALL_KW_SPLAT) {
1875 PUSH_INSN1(ret, location, topn, INT2FIX(2 + boff));
1876 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
1877 PUSH_INSN1(ret, location, splatarray, Qtrue);
1878 flag |= VM_CALL_ARGS_SPLAT_MUT;
1881 PUSH_INSN(ret, location, swap);
1882 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
1883 PUSH_INSN1(ret, location, setn, INT2FIX(2 + boff));
1884 PUSH_INSN(ret, location, pop);
1886 else {
1887 if (boff > 0) {
1888 PUSH_INSN1(ret, location, dupn, INT2FIX(3));
1889 PUSH_INSN(ret, location, swap);
1890 PUSH_INSN(ret, location, pop);
1892 if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
1893 PUSH_INSN(ret, location, swap);
1894 PUSH_INSN1(ret, location, splatarray, Qtrue);
1895 PUSH_INSN(ret, location, swap);
1896 flag |= VM_CALL_ARGS_SPLAT_MUT;
1898 PUSH_INSN1(ret, location, pushtoarray, INT2FIX(1));
1899 if (boff > 0) {
1900 PUSH_INSN1(ret, location, setn, INT2FIX(3));
1901 PUSH_INSN(ret, location, pop);
1902 PUSH_INSN(ret, location, pop);
1906 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
1908 else if (flag & VM_CALL_KW_SPLAT) {
1909 if (boff > 0) {
1910 PUSH_INSN1(ret, location, topn, INT2FIX(2));
1911 PUSH_INSN(ret, location, swap);
1912 PUSH_INSN1(ret, location, setn, INT2FIX(3));
1913 PUSH_INSN(ret, location, pop);
1916 PUSH_INSN(ret, location, swap);
1917 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
1919 else if (keyword_len) {
1920 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 1));
1921 PUSH_INSN1(ret, location, opt_reverse, INT2FIX(keyword_len + boff + 0));
1922 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
1924 else {
1925 if (boff > 0) {
1926 PUSH_INSN(ret, location, swap);
1928 PUSH_SEND_R(ret, location, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
1931 PUSH_INSN(ret, location, pop);
1932 PUSH_INSNL(ret, location, jump, lfin);
1933 PUSH_LABEL(ret, label);
1934 if (!popped) {
1935 PUSH_INSN1(ret, location, setn, INT2FIX(dup_argn + 1));
1937 PUSH_INSN1(ret, location, adjuststack, INT2FIX(dup_argn + 1));
1938 PUSH_LABEL(ret, lfin);
1941 // When we compile a pattern matching expression, we use the stack as a scratch
1942 // space to store lots of different values (consider it like we have a pattern
1943 // matching function and we need space for a bunch of different local
1944 // variables). The "base index" refers to the index on the stack where we
1945 // started compiling the pattern matching expression. These offsets from that
1946 // base index indicate the location of the various locals we need.
1947 #define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
1948 #define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
1949 #define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
1950 #define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
1951 #define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
1953 // A forward declaration because this is the recursive function that handles
1954 // compiling a pattern. It can be reentered by nesting patterns, as in the case
1955 // of arrays or hashes.
1956 static 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);
1959 * This function generates the code to set up the error string and error_p
1960 * locals depending on whether or not the pattern matched.
1962 static int
1963 pm_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)
1965 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
1966 LABEL *match_succeeded_label = NEW_LABEL(location.line);
1968 PUSH_INSN(ret, location, dup);
1969 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
1971 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
1972 PUSH_INSN1(ret, location, putobject, message);
1973 PUSH_INSN1(ret, location, topn, INT2FIX(3));
1974 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(2));
1975 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
1977 PUSH_INSN1(ret, location, putobject, Qfalse);
1978 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
1980 PUSH_INSN(ret, location, pop);
1981 PUSH_INSN(ret, location, pop);
1982 PUSH_LABEL(ret, match_succeeded_label);
1984 return COMPILE_OK;
1988 * This function generates the code to set up the error string and error_p
1989 * locals depending on whether or not the pattern matched when the value needs
1990 * to match a specific deconstructed length.
1992 static int
1993 pm_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)
1995 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
1996 LABEL *match_succeeded_label = NEW_LABEL(location.line);
1998 PUSH_INSN(ret, location, dup);
1999 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2001 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2002 PUSH_INSN1(ret, location, putobject, message);
2003 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2004 PUSH_INSN(ret, location, dup);
2005 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2006 PUSH_INSN1(ret, location, putobject, length);
2007 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(4));
2008 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2010 PUSH_INSN1(ret, location, putobject, Qfalse);
2011 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2013 PUSH_INSN(ret, location, pop);
2014 PUSH_INSN(ret, location, pop);
2015 PUSH_LABEL(ret, match_succeeded_label);
2017 return COMPILE_OK;
2021 * This function generates the code to set up the error string and error_p
2022 * locals depending on whether or not the pattern matched when the value needs
2023 * to pass a specific #=== method call.
2025 static int
2026 pm_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)
2028 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
2029 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2031 PUSH_INSN(ret, location, dup);
2032 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2034 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2035 PUSH_INSN1(ret, location, putobject, rb_fstring_lit("%p === %p does not return true"));
2036 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2037 PUSH_INSN1(ret, location, topn, INT2FIX(5));
2038 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
2039 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2040 PUSH_INSN1(ret, location, putobject, Qfalse);
2041 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2042 PUSH_INSN(ret, location, pop);
2043 PUSH_INSN(ret, location, pop);
2045 PUSH_LABEL(ret, match_succeeded_label);
2046 PUSH_INSN1(ret, location, setn, INT2FIX(2));
2047 PUSH_INSN(ret, location, pop);
2048 PUSH_INSN(ret, location, pop);
2050 return COMPILE_OK;
2054 * This is a variation on compiling a pattern matching expression that is used
2055 * to have the pattern matching instructions fall through to immediately after
2056 * the pattern if it passes. Otherwise it jumps to the given unmatched_label
2057 * label.
2059 static int
2060 pm_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)
2062 LABEL *matched_label = NEW_LABEL(pm_node_line_number(scope_node->parser, node));
2063 CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
2064 PUSH_LABEL(ret, matched_label);
2065 return COMPILE_OK;
2069 * This function compiles in the code necessary to call #deconstruct on the
2070 * value to match against. It raises appropriate errors if the method does not
2071 * exist or if it returns the wrong type.
2073 static int
2074 pm_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)
2076 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
2078 if (use_deconstructed_cache) {
2079 PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2080 PUSH_INSNL(ret, location, branchnil, deconstruct_label);
2082 PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2083 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2085 PUSH_INSN(ret, location, pop);
2086 PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
2087 PUSH_INSNL(ret, location, jump, deconstructed_label);
2089 else {
2090 PUSH_INSNL(ret, location, jump, deconstruct_label);
2093 PUSH_LABEL(ret, deconstruct_label);
2094 PUSH_INSN(ret, location, dup);
2095 PUSH_INSN1(ret, location, putobject, ID2SYM(rb_intern("deconstruct")));
2096 PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
2098 if (use_deconstructed_cache) {
2099 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
2102 if (in_single_pattern) {
2103 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct"), base_index + 1));
2106 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2107 PUSH_SEND(ret, location, rb_intern("deconstruct"), INT2FIX(0));
2109 if (use_deconstructed_cache) {
2110 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
2113 PUSH_INSN(ret, location, dup);
2114 PUSH_INSN1(ret, location, checktype, INT2FIX(T_ARRAY));
2115 PUSH_INSNL(ret, location, branchunless, type_error_label);
2116 PUSH_LABEL(ret, deconstructed_label);
2118 return COMPILE_OK;
2122 * This function compiles in the code necessary to match against the optional
2123 * constant path that is attached to an array, find, or hash pattern.
2125 static int
2126 pm_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)
2128 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
2130 PUSH_INSN(ret, location, dup);
2131 PM_COMPILE_NOT_POPPED(node);
2133 if (in_single_pattern) {
2134 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
2136 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2137 if (in_single_pattern) {
2138 CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
2140 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2141 return COMPILE_OK;
2145 * When matching fails, an appropriate error must be raised. This function is
2146 * responsible for compiling in those error raising instructions.
2148 static void
2149 pm_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)
2151 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
2152 LABEL *key_error_label = NEW_LABEL(location.line);
2153 LABEL *cleanup_label = NEW_LABEL(location.line);
2155 struct rb_callinfo_kwarg *kw_arg = rb_xmalloc_mul_add(2, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
2156 kw_arg->references = 0;
2157 kw_arg->keyword_len = 2;
2158 kw_arg->keywords[0] = ID2SYM(rb_intern("matchee"));
2159 kw_arg->keywords[1] = ID2SYM(rb_intern("key"));
2161 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2162 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2163 PUSH_INSNL(ret, location, branchif, key_error_label);
2165 PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternError);
2166 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2167 PUSH_INSN1(ret, location, putobject, rb_fstring_lit("%p: %s"));
2168 PUSH_INSN1(ret, location, topn, INT2FIX(4));
2169 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2170 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
2171 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2172 PUSH_INSNL(ret, location, jump, cleanup_label);
2174 PUSH_LABEL(ret, key_error_label);
2175 PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternKeyError);
2176 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2177 PUSH_INSN1(ret, location, putobject, rb_fstring_lit("%p: %s"));
2178 PUSH_INSN1(ret, location, topn, INT2FIX(4));
2179 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
2180 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
2181 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2182 PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2183 PUSH_SEND_R(ret, location, rb_intern("new"), INT2FIX(1), NULL, INT2FIX(VM_CALL_KWARG), kw_arg);
2184 PUSH_SEND(ret, location, id_core_raise, INT2FIX(1));
2185 PUSH_LABEL(ret, cleanup_label);
2187 PUSH_INSN1(ret, location, adjuststack, INT2FIX(7));
2188 if (!popped) PUSH_INSN(ret, location, putnil);
2189 PUSH_INSNL(ret, location, jump, done_label);
2190 PUSH_INSN1(ret, location, dupn, INT2FIX(5));
2191 if (popped) PUSH_INSN(ret, location, putnil);
2195 * Compile a pattern matching expression.
2197 static int
2198 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)
2200 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
2202 switch (PM_NODE_TYPE(node)) {
2203 case PM_ARRAY_PATTERN_NODE: {
2204 // Array patterns in pattern matching are triggered by using commas in
2205 // a pattern or wrapping it in braces. They are represented by a
2206 // ArrayPatternNode. This looks like:
2208 // foo => [1, 2, 3]
2210 // It can optionally have a splat in the middle of it, which can
2211 // optionally have a name attached.
2212 const pm_array_pattern_node_t *cast = (const pm_array_pattern_node_t *) node;
2214 const size_t requireds_size = cast->requireds.size;
2215 const size_t posts_size = cast->posts.size;
2216 const size_t minimum_size = requireds_size + posts_size;
2218 bool rest_named = false;
2219 bool use_rest_size = false;
2221 if (cast->rest != NULL) {
2222 rest_named = (PM_NODE_TYPE_P(cast->rest, PM_SPLAT_NODE) && ((const pm_splat_node_t *) cast->rest)->expression != NULL);
2223 use_rest_size = (rest_named || (!rest_named && posts_size > 0));
2226 LABEL *match_failed_label = NEW_LABEL(location.line);
2227 LABEL *type_error_label = NEW_LABEL(location.line);
2228 LABEL *deconstruct_label = NEW_LABEL(location.line);
2229 LABEL *deconstructed_label = NEW_LABEL(location.line);
2231 if (use_rest_size) {
2232 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
2233 PUSH_INSN(ret, location, swap);
2234 base_index++;
2237 if (cast->constant != NULL) {
2238 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2241 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));
2243 PUSH_INSN(ret, location, dup);
2244 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2245 PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
2246 PUSH_SEND(ret, location, cast->rest == NULL ? idEq : idGE, INT2FIX(1));
2247 if (in_single_pattern) {
2248 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+)");
2249 CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message, INT2FIX(minimum_size), base_index + 1));
2251 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2253 for (size_t index = 0; index < requireds_size; index++) {
2254 const pm_node_t *required = cast->requireds.nodes[index];
2255 PUSH_INSN(ret, location, dup);
2256 PUSH_INSN1(ret, location, putobject, INT2FIX(index));
2257 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
2258 CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
2261 if (cast->rest != NULL) {
2262 if (rest_named) {
2263 PUSH_INSN(ret, location, dup);
2264 PUSH_INSN1(ret, location, putobject, INT2FIX(requireds_size));
2265 PUSH_INSN1(ret, location, topn, INT2FIX(1));
2266 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2267 PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
2268 PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
2269 PUSH_INSN1(ret, location, setn, INT2FIX(4));
2270 PUSH_SEND(ret, location, idAREF, INT2FIX(2));
2271 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));
2273 else if (posts_size > 0) {
2274 PUSH_INSN(ret, location, dup);
2275 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2276 PUSH_INSN1(ret, location, putobject, INT2FIX(minimum_size));
2277 PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
2278 PUSH_INSN1(ret, location, setn, INT2FIX(2));
2279 PUSH_INSN(ret, location, pop);
2283 for (size_t index = 0; index < posts_size; index++) {
2284 const pm_node_t *post = cast->posts.nodes[index];
2285 PUSH_INSN(ret, location, dup);
2287 PUSH_INSN1(ret, location, putobject, INT2FIX(requireds_size + index));
2288 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2289 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2290 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
2291 CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
2294 PUSH_INSN(ret, location, pop);
2295 if (use_rest_size) {
2296 PUSH_INSN(ret, location, pop);
2299 PUSH_INSNL(ret, location, jump, matched_label);
2300 PUSH_INSN(ret, location, putnil);
2301 if (use_rest_size) {
2302 PUSH_INSN(ret, location, putnil);
2305 PUSH_LABEL(ret, type_error_label);
2306 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2307 PUSH_INSN1(ret, location, putobject, rb_eTypeError);
2308 PUSH_INSN1(ret, location, putobject, rb_fstring_lit("deconstruct must return Array"));
2309 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2310 PUSH_INSN(ret, location, pop);
2312 PUSH_LABEL(ret, match_failed_label);
2313 PUSH_INSN(ret, location, pop);
2314 if (use_rest_size) {
2315 PUSH_INSN(ret, location, pop);
2318 PUSH_INSNL(ret, location, jump, unmatched_label);
2319 break;
2321 case PM_FIND_PATTERN_NODE: {
2322 // Find patterns in pattern matching are triggered by using commas in
2323 // a pattern or wrapping it in braces and using a splat on both the left
2324 // and right side of the pattern. This looks like:
2326 // foo => [*, 1, 2, 3, *]
2328 // There can be any number of requireds in the middle. The splats on
2329 // both sides can optionally have names attached.
2330 const pm_find_pattern_node_t *cast = (const pm_find_pattern_node_t *) node;
2331 const size_t size = cast->requireds.size;
2333 LABEL *match_failed_label = NEW_LABEL(location.line);
2334 LABEL *type_error_label = NEW_LABEL(location.line);
2335 LABEL *deconstruct_label = NEW_LABEL(location.line);
2336 LABEL *deconstructed_label = NEW_LABEL(location.line);
2338 if (cast->constant) {
2339 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2342 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));
2344 PUSH_INSN(ret, location, dup);
2345 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2346 PUSH_INSN1(ret, location, putobject, INT2FIX(size));
2347 PUSH_SEND(ret, location, idGE, INT2FIX(1));
2348 if (in_single_pattern) {
2349 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));
2351 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2354 LABEL *while_begin_label = NEW_LABEL(location.line);
2355 LABEL *next_loop_label = NEW_LABEL(location.line);
2356 LABEL *find_succeeded_label = NEW_LABEL(location.line);
2357 LABEL *find_failed_label = NEW_LABEL(location.line);
2359 PUSH_INSN(ret, location, dup);
2360 PUSH_SEND(ret, location, idLength, INT2FIX(0));
2362 PUSH_INSN(ret, location, dup);
2363 PUSH_INSN1(ret, location, putobject, INT2FIX(size));
2364 PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
2365 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
2366 PUSH_LABEL(ret, while_begin_label);
2368 PUSH_INSN(ret, location, dup);
2369 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2370 PUSH_SEND(ret, location, idLE, INT2FIX(1));
2371 PUSH_INSNL(ret, location, branchunless, find_failed_label);
2373 for (size_t index = 0; index < size; index++) {
2374 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2375 PUSH_INSN1(ret, location, topn, INT2FIX(1));
2377 if (index != 0) {
2378 PUSH_INSN1(ret, location, putobject, INT2FIX(index));
2379 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2382 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
2383 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));
2386 RUBY_ASSERT(PM_NODE_TYPE_P(cast->left, PM_SPLAT_NODE));
2387 const pm_splat_node_t *left = (const pm_splat_node_t *) cast->left;
2389 if (left->expression != NULL) {
2390 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2391 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
2392 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2393 PUSH_SEND(ret, location, idAREF, INT2FIX(2));
2394 CHECK(pm_compile_pattern_match(iseq, scope_node, left->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4));
2397 RUBY_ASSERT(PM_NODE_TYPE_P(cast->right, PM_SPLAT_NODE));
2398 const pm_splat_node_t *right = (const pm_splat_node_t *) cast->right;
2400 if (right->expression != NULL) {
2401 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2402 PUSH_INSN1(ret, location, topn, INT2FIX(1));
2403 PUSH_INSN1(ret, location, putobject, INT2FIX(size));
2404 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2405 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2406 PUSH_SEND(ret, location, idAREF, INT2FIX(2));
2407 pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4);
2410 PUSH_INSNL(ret, location, jump, find_succeeded_label);
2412 PUSH_LABEL(ret, next_loop_label);
2413 PUSH_INSN1(ret, location, putobject, INT2FIX(1));
2414 PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
2415 PUSH_INSNL(ret, location, jump, while_begin_label);
2417 PUSH_LABEL(ret, find_failed_label);
2418 PUSH_INSN1(ret, location, adjuststack, INT2FIX(3));
2419 if (in_single_pattern) {
2420 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2421 PUSH_INSN1(ret, location, putobject, rb_fstring_lit("%p does not match to find pattern"));
2422 PUSH_INSN1(ret, location, topn, INT2FIX(2));
2423 PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(2));
2424 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2426 PUSH_INSN1(ret, location, putobject, Qfalse);
2427 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2429 PUSH_INSN(ret, location, pop);
2430 PUSH_INSN(ret, location, pop);
2432 PUSH_INSNL(ret, location, jump, match_failed_label);
2433 PUSH_INSN1(ret, location, dupn, INT2FIX(3));
2435 PUSH_LABEL(ret, find_succeeded_label);
2436 PUSH_INSN1(ret, location, adjuststack, INT2FIX(3));
2439 PUSH_INSN(ret, location, pop);
2440 PUSH_INSNL(ret, location, jump, matched_label);
2441 PUSH_INSN(ret, location, putnil);
2443 PUSH_LABEL(ret, type_error_label);
2444 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2445 PUSH_INSN1(ret, location, putobject, rb_eTypeError);
2446 PUSH_INSN1(ret, location, putobject, rb_fstring_lit("deconstruct must return Array"));
2447 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2448 PUSH_INSN(ret, location, pop);
2450 PUSH_LABEL(ret, match_failed_label);
2451 PUSH_INSN(ret, location, pop);
2452 PUSH_INSNL(ret, location, jump, unmatched_label);
2454 break;
2456 case PM_HASH_PATTERN_NODE: {
2457 // Hash patterns in pattern matching are triggered by using labels and
2458 // values in a pattern or by using the ** operator. They are represented
2459 // by the HashPatternNode. This looks like:
2461 // foo => { a: 1, b: 2, **bar }
2463 // It can optionally have an assoc splat in the middle of it, which can
2464 // optionally have a name.
2465 const pm_hash_pattern_node_t *cast = (const pm_hash_pattern_node_t *) node;
2467 // We don't consider it a "rest" parameter if it's a ** that is unnamed.
2468 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);
2469 bool has_keys = cast->elements.size > 0 || cast->rest != NULL;
2471 LABEL *match_failed_label = NEW_LABEL(location.line);
2472 LABEL *type_error_label = NEW_LABEL(location.line);
2473 VALUE keys = Qnil;
2475 if (has_keys && !has_rest) {
2476 keys = rb_ary_new_capa(cast->elements.size);
2478 for (size_t index = 0; index < cast->elements.size; index++) {
2479 const pm_node_t *element = cast->elements.nodes[index];
2480 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2482 const pm_node_t *key = ((const pm_assoc_node_t *) element)->key;
2483 RUBY_ASSERT(PM_NODE_TYPE_P(key, PM_SYMBOL_NODE));
2485 VALUE symbol = ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) key));
2486 rb_ary_push(keys, symbol);
2490 if (cast->constant) {
2491 CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
2494 PUSH_INSN(ret, location, dup);
2495 PUSH_INSN1(ret, location, putobject, ID2SYM(rb_intern("deconstruct_keys")));
2496 PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
2497 if (in_single_pattern) {
2498 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct_keys"), base_index + 1));
2500 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2502 if (NIL_P(keys)) {
2503 PUSH_INSN(ret, location, putnil);
2505 else {
2506 PUSH_INSN1(ret, location, duparray, keys);
2507 RB_OBJ_WRITTEN(iseq, Qundef, rb_obj_hide(keys));
2509 PUSH_SEND(ret, location, rb_intern("deconstruct_keys"), INT2FIX(1));
2511 PUSH_INSN(ret, location, dup);
2512 PUSH_INSN1(ret, location, checktype, INT2FIX(T_HASH));
2513 PUSH_INSNL(ret, location, branchunless, type_error_label);
2515 if (has_rest) {
2516 PUSH_SEND(ret, location, rb_intern("dup"), INT2FIX(0));
2519 if (has_keys) {
2520 DECL_ANCHOR(match_values);
2521 INIT_ANCHOR(match_values);
2523 for (size_t index = 0; index < cast->elements.size; index++) {
2524 const pm_node_t *element = cast->elements.nodes[index];
2525 RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
2527 const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
2528 const pm_node_t *key = assoc->key;
2529 RUBY_ASSERT(PM_NODE_TYPE_P(key, PM_SYMBOL_NODE));
2531 VALUE symbol = ID2SYM(parse_string_symbol(scope_node, (const pm_symbol_node_t *) key));
2532 PUSH_INSN(ret, location, dup);
2533 PUSH_INSN1(ret, location, putobject, symbol);
2534 PUSH_SEND(ret, location, rb_intern("key?"), INT2FIX(1));
2536 if (in_single_pattern) {
2537 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2539 PUSH_INSN(ret, location, dup);
2540 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2542 PUSH_INSN1(ret, location, putobject, rb_str_freeze(rb_sprintf("key not found: %+"PRIsVALUE, symbol)));
2543 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
2544 PUSH_INSN1(ret, location, putobject, Qtrue);
2545 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
2546 PUSH_INSN1(ret, location, topn, INT2FIX(3));
2547 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
2548 PUSH_INSN1(ret, location, putobject, symbol);
2549 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
2551 PUSH_INSN1(ret, location, adjuststack, INT2FIX(4));
2552 PUSH_LABEL(ret, match_succeeded_label);
2555 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2556 PUSH_INSN(match_values, location, dup);
2557 PUSH_INSN1(match_values, location, putobject, symbol);
2558 PUSH_SEND(match_values, location, has_rest ? rb_intern("delete") : idAREF, INT2FIX(1));
2560 const pm_node_t *value = assoc->value;
2561 if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
2562 value = ((const pm_implicit_node_t *) value)->value;
2565 CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
2568 PUSH_SEQ(ret, match_values);
2570 else {
2571 PUSH_INSN(ret, location, dup);
2572 PUSH_SEND(ret, location, idEmptyP, INT2FIX(0));
2573 if (in_single_pattern) {
2574 CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p is not empty"), base_index + 1));
2576 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2579 if (has_rest) {
2580 switch (PM_NODE_TYPE(cast->rest)) {
2581 case PM_NO_KEYWORDS_PARAMETER_NODE: {
2582 PUSH_INSN(ret, location, dup);
2583 PUSH_SEND(ret, location, idEmptyP, INT2FIX(0));
2584 if (in_single_pattern) {
2585 pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("rest of %p is not empty"), base_index + 1);
2587 PUSH_INSNL(ret, location, branchunless, match_failed_label);
2588 break;
2590 case PM_ASSOC_SPLAT_NODE: {
2591 const pm_assoc_splat_node_t *splat = (const pm_assoc_splat_node_t *) cast->rest;
2592 PUSH_INSN(ret, location, dup);
2593 pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1);
2594 break;
2596 default:
2597 rb_bug("unreachable");
2598 break;
2602 PUSH_INSN(ret, location, pop);
2603 PUSH_INSNL(ret, location, jump, matched_label);
2604 PUSH_INSN(ret, location, putnil);
2606 PUSH_LABEL(ret, type_error_label);
2607 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
2608 PUSH_INSN1(ret, location, putobject, rb_eTypeError);
2609 PUSH_INSN1(ret, location, putobject, rb_fstring_lit("deconstruct_keys must return Hash"));
2610 PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
2611 PUSH_INSN(ret, location, pop);
2613 PUSH_LABEL(ret, match_failed_label);
2614 PUSH_INSN(ret, location, pop);
2615 PUSH_INSNL(ret, location, jump, unmatched_label);
2616 break;
2618 case PM_CAPTURE_PATTERN_NODE: {
2619 // Capture patterns allow you to pattern match against an element in a
2620 // pattern and also capture the value into a local variable. This looks
2621 // like:
2623 // [1] => [Integer => foo]
2625 // In this case the `Integer => foo` will be represented by a
2626 // CapturePatternNode, which has both a value (the pattern to match
2627 // against) and a target (the place to write the variable into).
2628 const pm_capture_pattern_node_t *cast = (const pm_capture_pattern_node_t *) node;
2630 LABEL *match_failed_label = NEW_LABEL(location.line);
2632 PUSH_INSN(ret, location, dup);
2633 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));
2634 CHECK(pm_compile_pattern(iseq, scope_node, cast->target, ret, matched_label, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index));
2635 PUSH_INSN(ret, location, putnil);
2637 PUSH_LABEL(ret, match_failed_label);
2638 PUSH_INSN(ret, location, pop);
2639 PUSH_INSNL(ret, location, jump, unmatched_label);
2641 break;
2643 case PM_LOCAL_VARIABLE_TARGET_NODE: {
2644 // Local variables can be targeted by placing identifiers in the place
2645 // of a pattern. For example, foo in bar. This results in the value
2646 // being matched being written to that local variable.
2647 const pm_local_variable_target_node_t *cast = (const pm_local_variable_target_node_t *) node;
2648 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
2650 // If this local variable is being written from within an alternation
2651 // pattern, then it cannot actually be added to the local table since
2652 // it's ambiguous which value should be used. So instead we indicate
2653 // this with a compile error.
2654 if (in_alternation_pattern) {
2655 ID id = pm_constant_id_lookup(scope_node, cast->name);
2656 const char *name = rb_id2name(id);
2658 if (name && strlen(name) > 0 && name[0] != '_') {
2659 COMPILE_ERROR(ERROR_ARGS "illegal variable in alternative pattern (%"PRIsVALUE")", rb_id2str(id));
2660 return COMPILE_NG;
2664 PUSH_SETLOCAL(ret, location, index.index, index.level);
2665 PUSH_INSNL(ret, location, jump, matched_label);
2666 break;
2668 case PM_ALTERNATION_PATTERN_NODE: {
2669 // Alternation patterns allow you to specify multiple patterns in a
2670 // single expression using the | operator.
2671 const pm_alternation_pattern_node_t *cast = (const pm_alternation_pattern_node_t *) node;
2673 LABEL *matched_left_label = NEW_LABEL(location.line);
2674 LABEL *unmatched_left_label = NEW_LABEL(location.line);
2676 // First, we're going to attempt to match against the left pattern. If
2677 // that pattern matches, then we'll skip matching the right pattern.
2678 PUSH_INSN(ret, location, dup);
2679 CHECK(pm_compile_pattern(iseq, scope_node, cast->left, ret, matched_left_label, unmatched_left_label, in_single_pattern, true, true, base_index + 1));
2681 // If we get here, then we matched on the left pattern. In this case we
2682 // should pop out the duplicate value that we preemptively added to
2683 // match against the right pattern and then jump to the match label.
2684 PUSH_LABEL(ret, matched_left_label);
2685 PUSH_INSN(ret, location, pop);
2686 PUSH_INSNL(ret, location, jump, matched_label);
2687 PUSH_INSN(ret, location, putnil);
2689 // If we get here, then we didn't match on the left pattern. In this
2690 // case we attempt to match against the right pattern.
2691 PUSH_LABEL(ret, unmatched_left_label);
2692 CHECK(pm_compile_pattern(iseq, scope_node, cast->right, ret, matched_label, unmatched_label, in_single_pattern, true, true, base_index));
2693 break;
2695 case PM_PARENTHESES_NODE:
2696 // Parentheses are allowed to wrap expressions in pattern matching and
2697 // they do nothing since they can only wrap individual expressions and
2698 // not groups. In this case we'll recurse back into this same function
2699 // with the body of the parentheses.
2700 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);
2701 case PM_PINNED_EXPRESSION_NODE:
2702 // Pinned expressions are a way to match against the value of an
2703 // expression that should be evaluated at runtime. This looks like:
2704 // foo in ^(bar). To compile these, we compile the expression as if it
2705 // were a literal value by falling through to the literal case.
2706 node = ((const pm_pinned_expression_node_t *) node)->expression;
2707 /* fallthrough */
2708 case PM_ARRAY_NODE:
2709 case PM_CLASS_VARIABLE_READ_NODE:
2710 case PM_CONSTANT_PATH_NODE:
2711 case PM_CONSTANT_READ_NODE:
2712 case PM_FALSE_NODE:
2713 case PM_FLOAT_NODE:
2714 case PM_GLOBAL_VARIABLE_READ_NODE:
2715 case PM_IMAGINARY_NODE:
2716 case PM_INSTANCE_VARIABLE_READ_NODE:
2717 case PM_INTEGER_NODE:
2718 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
2719 case PM_INTERPOLATED_STRING_NODE:
2720 case PM_INTERPOLATED_SYMBOL_NODE:
2721 case PM_INTERPOLATED_X_STRING_NODE:
2722 case PM_LAMBDA_NODE:
2723 case PM_LOCAL_VARIABLE_READ_NODE:
2724 case PM_NIL_NODE:
2725 case PM_SOURCE_ENCODING_NODE:
2726 case PM_SOURCE_FILE_NODE:
2727 case PM_SOURCE_LINE_NODE:
2728 case PM_RANGE_NODE:
2729 case PM_RATIONAL_NODE:
2730 case PM_REGULAR_EXPRESSION_NODE:
2731 case PM_SELF_NODE:
2732 case PM_STRING_NODE:
2733 case PM_SYMBOL_NODE:
2734 case PM_TRUE_NODE:
2735 case PM_X_STRING_NODE: {
2736 // These nodes are all simple patterns, which means we'll use the
2737 // checkmatch instruction to match against them, which is effectively a
2738 // VM-level === operator.
2739 PM_COMPILE_NOT_POPPED(node);
2740 if (in_single_pattern) {
2741 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
2744 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
2746 if (in_single_pattern) {
2747 pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
2750 PUSH_INSNL(ret, location, branchif, matched_label);
2751 PUSH_INSNL(ret, location, jump, unmatched_label);
2752 break;
2754 case PM_PINNED_VARIABLE_NODE: {
2755 // Pinned variables are a way to match against the value of a variable
2756 // without it looking like you're trying to write to the variable. This
2757 // looks like: foo in ^@bar. To compile these, we compile the variable
2758 // that they hold.
2759 const pm_pinned_variable_node_t *cast = (const pm_pinned_variable_node_t *) node;
2760 CHECK(pm_compile_pattern(iseq, scope_node, cast->variable, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, true, base_index));
2761 break;
2763 case PM_IF_NODE:
2764 case PM_UNLESS_NODE: {
2765 // If and unless nodes can show up here as guards on `in` clauses. This
2766 // looks like:
2768 // case foo
2769 // in bar if baz?
2770 // qux
2771 // end
2773 // Because we know they're in the modifier form and they can't have any
2774 // variation on this pattern, we compile them differently (more simply)
2775 // here than we would in the normal compilation path.
2776 const pm_node_t *predicate;
2777 const pm_node_t *statement;
2779 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
2780 const pm_if_node_t *cast = (const pm_if_node_t *) node;
2781 predicate = cast->predicate;
2783 RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
2784 statement = cast->statements->body.nodes[0];
2786 else {
2787 const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
2788 predicate = cast->predicate;
2790 RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
2791 statement = cast->statements->body.nodes[0];
2794 CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
2795 PM_COMPILE_NOT_POPPED(predicate);
2797 if (in_single_pattern) {
2798 LABEL *match_succeeded_label = NEW_LABEL(location.line);
2800 PUSH_INSN(ret, location, dup);
2801 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
2802 PUSH_INSNL(ret, location, branchif, match_succeeded_label);
2804 else {
2805 PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
2808 PUSH_INSN1(ret, location, putobject, rb_fstring_lit("guard clause does not return true"));
2809 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
2810 PUSH_INSN1(ret, location, putobject, Qfalse);
2811 PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
2813 PUSH_INSN(ret, location, pop);
2814 PUSH_INSN(ret, location, pop);
2816 PUSH_LABEL(ret, match_succeeded_label);
2819 if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
2820 PUSH_INSNL(ret, location, branchunless, unmatched_label);
2822 else {
2823 PUSH_INSNL(ret, location, branchif, unmatched_label);
2826 PUSH_INSNL(ret, location, jump, matched_label);
2827 break;
2829 default:
2830 // If we get here, then we have a node type that should not be in this
2831 // position. This would be a bug in the parser, because a different node
2832 // type should never have been created in this position in the tree.
2833 rb_bug("Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
2834 break;
2837 return COMPILE_OK;
2840 #undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
2841 #undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
2842 #undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
2843 #undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
2844 #undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
2846 // Generate a scope node from the given node.
2847 void
2848 pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous)
2850 // This is very important, otherwise the scope node could be seen as having
2851 // certain flags set that _should not_ be set.
2852 memset(scope, 0, sizeof(pm_scope_node_t));
2854 scope->base.type = PM_SCOPE_NODE;
2855 scope->base.location.start = node->location.start;
2856 scope->base.location.end = node->location.end;
2858 scope->previous = previous;
2859 scope->ast_node = (pm_node_t *) node;
2861 if (previous) {
2862 scope->parser = previous->parser;
2863 scope->encoding = previous->encoding;
2864 scope->filepath_encoding = previous->filepath_encoding;
2865 scope->constants = previous->constants;
2868 switch (PM_NODE_TYPE(node)) {
2869 case PM_BLOCK_NODE: {
2870 const pm_block_node_t *cast = (const pm_block_node_t *) node;
2871 scope->body = cast->body;
2872 scope->locals = cast->locals;
2873 scope->parameters = cast->parameters;
2874 break;
2876 case PM_CLASS_NODE: {
2877 const pm_class_node_t *cast = (const pm_class_node_t *) node;
2878 scope->body = cast->body;
2879 scope->locals = cast->locals;
2880 break;
2882 case PM_DEF_NODE: {
2883 const pm_def_node_t *cast = (const pm_def_node_t *) node;
2884 scope->parameters = (pm_node_t *) cast->parameters;
2885 scope->body = cast->body;
2886 scope->locals = cast->locals;
2887 break;
2889 case PM_ENSURE_NODE: {
2890 const pm_ensure_node_t *cast = (const pm_ensure_node_t *) node;
2891 scope->body = (pm_node_t *) node;
2893 if (cast->statements != NULL) {
2894 scope->base.location.start = cast->statements->base.location.start;
2895 scope->base.location.end = cast->statements->base.location.end;
2898 break;
2900 case PM_FOR_NODE: {
2901 const pm_for_node_t *cast = (const pm_for_node_t *) node;
2902 scope->body = (pm_node_t *) cast->statements;
2903 break;
2905 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
2906 RUBY_ASSERT(node->flags & PM_REGULAR_EXPRESSION_FLAGS_ONCE);
2907 scope->body = (pm_node_t *) node;
2908 break;
2910 case PM_LAMBDA_NODE: {
2911 const pm_lambda_node_t *cast = (const pm_lambda_node_t *) node;
2912 scope->parameters = cast->parameters;
2913 scope->body = cast->body;
2914 scope->locals = cast->locals;
2916 if (cast->parameters != NULL) {
2917 scope->base.location.start = cast->parameters->location.start;
2919 else {
2920 scope->base.location.start = cast->operator_loc.end;
2922 break;
2924 case PM_MODULE_NODE: {
2925 const pm_module_node_t *cast = (const pm_module_node_t *) node;
2926 scope->body = cast->body;
2927 scope->locals = cast->locals;
2928 break;
2930 case PM_POST_EXECUTION_NODE: {
2931 const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) node;
2932 scope->body = (pm_node_t *) cast->statements;
2933 break;
2935 case PM_PROGRAM_NODE: {
2936 const pm_program_node_t *cast = (const pm_program_node_t *) node;
2937 scope->body = (pm_node_t *) cast->statements;
2938 scope->locals = cast->locals;
2939 break;
2941 case PM_RESCUE_NODE: {
2942 const pm_rescue_node_t *cast = (const pm_rescue_node_t *) node;
2943 scope->body = (pm_node_t *) cast->statements;
2944 break;
2946 case PM_RESCUE_MODIFIER_NODE: {
2947 const pm_rescue_modifier_node_t *cast = (const pm_rescue_modifier_node_t *) node;
2948 scope->body = (pm_node_t *) cast->rescue_expression;
2949 break;
2951 case PM_SINGLETON_CLASS_NODE: {
2952 const pm_singleton_class_node_t *cast = (const pm_singleton_class_node_t *) node;
2953 scope->body = cast->body;
2954 scope->locals = cast->locals;
2955 break;
2957 case PM_STATEMENTS_NODE: {
2958 const pm_statements_node_t *cast = (const pm_statements_node_t *) node;
2959 scope->body = (pm_node_t *) cast;
2960 break;
2962 default:
2963 rb_bug("unreachable");
2964 break;
2968 void
2969 pm_scope_node_destroy(pm_scope_node_t *scope_node)
2971 if (scope_node->index_lookup_table) {
2972 st_free_table(scope_node->index_lookup_table);
2977 * We need to put the label "retry_end_l" immediately after the last "send"
2978 * instruction. This because vm_throw checks if the break cont is equal to the
2979 * index of next insn of the "send". (Otherwise, it is considered
2980 * "break from proc-closure". See "TAG_BREAK" handling in "vm_throw_start".)
2982 * Normally, "send" instruction is at the last. However, qcall under branch
2983 * coverage measurement adds some instructions after the "send".
2985 * Note that "invokesuper" appears instead of "send".
2987 static void
2988 pm_compile_retry_end_label(rb_iseq_t *iseq, LINK_ANCHOR *const ret, LABEL *retry_end_l)
2990 INSN *iobj;
2991 LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
2992 iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
2993 while (INSN_OF(iobj) != BIN(send) && INSN_OF(iobj) != BIN(invokesuper)) {
2994 iobj = (INSN*) get_prev_insn(iobj);
2996 ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
2998 // LINK_ANCHOR has a pointer to the last element, but
2999 // ELEM_INSERT_NEXT does not update it even if we add an insn to the
3000 // last of LINK_ANCHOR. So this updates it manually.
3001 if (&iobj->link == LAST_ELEMENT(ret)) {
3002 ret->last = (LINK_ELEMENT*) retry_end_l;
3007 * Compile a call node into the given iseq.
3009 static void
3010 pm_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)
3012 const pm_location_t *message_loc = &call_node->message_loc;
3013 if (message_loc->start == NULL) message_loc = &call_node->base.location;
3015 const pm_line_column_t location = PM_LOCATION_START_LINE_COLUMN(scope_node->parser, message_loc);
3016 LABEL *else_label = NEW_LABEL(location.line);
3017 LABEL *end_label = NEW_LABEL(location.line);
3018 LABEL *retry_end_l = NEW_LABEL(location.line);
3020 VALUE branches = Qfalse;
3021 rb_code_location_t code_location = { 0 };
3022 int node_id = location.column;
3024 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3025 if (PM_BRANCH_COVERAGE_P(iseq)) {
3026 const uint8_t *cursors[3] = {
3027 call_node->closing_loc.end,
3028 call_node->arguments == NULL ? NULL : call_node->arguments->base.location.end,
3029 call_node->message_loc.end
3032 const uint8_t *end_cursor = cursors[0];
3033 end_cursor = (end_cursor == NULL || cursors[1] == NULL) ? cursors[1] : (end_cursor > cursors[1] ? end_cursor : cursors[1]);
3034 end_cursor = (end_cursor == NULL || cursors[2] == NULL) ? cursors[2] : (end_cursor > cursors[2] ? end_cursor : cursors[2]);
3036 const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, call_node);
3037 const pm_line_column_t end_location = pm_newline_list_line_column(&scope_node->parser->newline_list, end_cursor, scope_node->parser->start_line);
3039 code_location = (rb_code_location_t) {
3040 .beg_pos = { .lineno = start_location.line, .column = start_location.column },
3041 .end_pos = { .lineno = end_location.line, .column = end_location.column }
3044 branches = decl_branch_base(iseq, PTR2NUM(call_node), &code_location, "&.");
3047 PUSH_INSN(ret, location, dup);
3048 PUSH_INSNL(ret, location, branchnil, else_label);
3050 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0, "then", branches);
3053 int flags = 0;
3054 struct rb_callinfo_kwarg *kw_arg = NULL;
3056 int orig_argc = pm_setup_args(call_node->arguments, call_node->block, &flags, &kw_arg, iseq, ret, scope_node, &location);
3057 const rb_iseq_t *block_iseq = NULL;
3059 if (call_node->block != NULL && PM_NODE_TYPE_P(call_node->block, PM_BLOCK_NODE)) {
3060 // Scope associated with the block
3061 pm_scope_node_t next_scope_node;
3062 pm_scope_node_init(call_node->block, &next_scope_node, scope_node);
3064 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));
3065 pm_scope_node_destroy(&next_scope_node);
3066 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
3068 else {
3069 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
3070 flags |= VM_CALL_VCALL;
3073 if (!flags) {
3074 flags |= VM_CALL_ARGS_SIMPLE;
3078 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
3079 flags |= VM_CALL_FCALL;
3082 if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
3083 if (flags & VM_CALL_ARGS_BLOCKARG) {
3084 PUSH_INSN1(ret, location, topn, INT2FIX(1));
3085 if (flags & VM_CALL_ARGS_SPLAT) {
3086 PUSH_INSN1(ret, location, putobject, INT2FIX(-1));
3087 PUSH_SEND_WITH_FLAG(ret, location, idAREF, INT2FIX(1), INT2FIX(0));
3089 PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 3));
3090 PUSH_INSN(ret, location, pop);
3092 else if (flags & VM_CALL_ARGS_SPLAT) {
3093 PUSH_INSN(ret, location, dup);
3094 PUSH_INSN1(ret, location, putobject, INT2FIX(-1));
3095 PUSH_SEND_WITH_FLAG(ret, location, idAREF, INT2FIX(1), INT2FIX(0));
3096 PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 2));
3097 PUSH_INSN(ret, location, pop);
3099 else {
3100 PUSH_INSN1(ret, location, setn, INT2FIX(orig_argc + 1));
3104 if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
3105 PUSH_INSN(ret, location, splatkw);
3108 PUSH_SEND_R(ret, location, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
3110 if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
3111 pm_compile_retry_end_label(iseq, ret, retry_end_l);
3112 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, start, retry_end_l, block_iseq, retry_end_l);
3115 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
3116 PUSH_INSNL(ret, location, jump, end_label);
3117 PUSH_LABEL(ret, else_label);
3118 add_trace_branch_coverage(iseq, ret, &code_location, node_id, 1, "else", branches);
3119 PUSH_LABEL(ret, end_label);
3122 if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
3123 PUSH_INSN(ret, location, pop);
3126 if (popped) PUSH_INSN(ret, location, pop);
3129 static void
3130 pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition, LABEL **lfinish, bool explicit_receiver)
3132 // in_condition is the same as compile.c's needstr
3133 enum defined_type dtype = DEFINED_NOT_DEFINED;
3134 const pm_line_column_t location = *node_location;
3136 switch (PM_NODE_TYPE(node)) {
3137 case PM_ARGUMENTS_NODE: {
3138 const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
3139 const pm_node_list_t *arguments = &cast->arguments;
3140 for (size_t idx = 0; idx < arguments->size; idx++) {
3141 const pm_node_t *argument = arguments->nodes[idx];
3142 pm_compile_defined_expr0(iseq, argument, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
3144 if (!lfinish[1]) {
3145 lfinish[1] = NEW_LABEL(location.line);
3147 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3149 dtype = DEFINED_TRUE;
3150 break;
3152 case PM_NIL_NODE:
3153 dtype = DEFINED_NIL;
3154 break;
3155 case PM_PARENTHESES_NODE: {
3156 const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
3158 if (cast->body == NULL) {
3159 // If we have empty parentheses, then we want to return "nil".
3160 dtype = DEFINED_NIL;
3162 else if (PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE) && ((const pm_statements_node_t *) cast->body)->body.size == 1) {
3163 // If we have a parentheses node that is wrapping a single statement
3164 // then we want to recurse down to that statement and compile it.
3165 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);
3166 return;
3168 else {
3169 // Otherwise, we have parentheses wrapping multiple statements, in
3170 // which case this is defined as "expression".
3171 dtype = DEFINED_EXPR;
3174 break;
3176 case PM_SELF_NODE:
3177 dtype = DEFINED_SELF;
3178 break;
3179 case PM_TRUE_NODE:
3180 dtype = DEFINED_TRUE;
3181 break;
3182 case PM_FALSE_NODE:
3183 dtype = DEFINED_FALSE;
3184 break;
3185 case PM_ARRAY_NODE: {
3186 const pm_array_node_t *cast = (const pm_array_node_t *) node;
3188 if (!PM_NODE_FLAG_P(cast, PM_ARRAY_NODE_FLAGS_CONTAINS_SPLAT)) {
3189 for (size_t index = 0; index < cast->elements.size; index++) {
3190 pm_compile_defined_expr0(iseq, cast->elements.nodes[index], node_location, ret, popped, scope_node, true, lfinish, false);
3192 if (!lfinish[1]) {
3193 lfinish[1] = NEW_LABEL(location.line);
3196 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3200 case PM_AND_NODE:
3201 case PM_BEGIN_NODE:
3202 case PM_BREAK_NODE:
3203 case PM_CASE_NODE:
3204 case PM_CASE_MATCH_NODE:
3205 case PM_CLASS_NODE:
3206 case PM_DEF_NODE:
3207 case PM_DEFINED_NODE:
3208 case PM_FLOAT_NODE:
3209 case PM_FOR_NODE:
3210 case PM_HASH_NODE:
3211 case PM_IF_NODE:
3212 case PM_IMAGINARY_NODE:
3213 case PM_INTEGER_NODE:
3214 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
3215 case PM_INTERPOLATED_STRING_NODE:
3216 case PM_INTERPOLATED_SYMBOL_NODE:
3217 case PM_INTERPOLATED_X_STRING_NODE:
3218 case PM_KEYWORD_HASH_NODE:
3219 case PM_LAMBDA_NODE:
3220 case PM_MATCH_PREDICATE_NODE:
3221 case PM_MATCH_REQUIRED_NODE:
3222 case PM_MATCH_WRITE_NODE:
3223 case PM_MODULE_NODE:
3224 case PM_NEXT_NODE:
3225 case PM_OR_NODE:
3226 case PM_RANGE_NODE:
3227 case PM_RATIONAL_NODE:
3228 case PM_REDO_NODE:
3229 case PM_REGULAR_EXPRESSION_NODE:
3230 case PM_RETRY_NODE:
3231 case PM_RETURN_NODE:
3232 case PM_SINGLETON_CLASS_NODE:
3233 case PM_SOURCE_ENCODING_NODE:
3234 case PM_SOURCE_FILE_NODE:
3235 case PM_SOURCE_LINE_NODE:
3236 case PM_STRING_NODE:
3237 case PM_SYMBOL_NODE:
3238 case PM_UNLESS_NODE:
3239 case PM_UNTIL_NODE:
3240 case PM_WHILE_NODE:
3241 case PM_X_STRING_NODE:
3242 dtype = DEFINED_EXPR;
3243 break;
3244 case PM_LOCAL_VARIABLE_READ_NODE:
3245 dtype = DEFINED_LVAR;
3246 break;
3248 #define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
3250 case PM_INSTANCE_VARIABLE_READ_NODE: {
3251 const pm_instance_variable_read_node_t *cast = (const pm_instance_variable_read_node_t *) node;
3253 ID name = pm_constant_id_lookup(scope_node, cast->name);
3254 PUSH_INSN3(ret, location, definedivar, ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
3256 return;
3258 case PM_BACK_REFERENCE_READ_NODE: {
3259 const char *char_ptr = (const char *) (node->location.start + 1);
3260 ID backref_val = INT2FIX(rb_intern2(char_ptr, 1)) << 1 | 1;
3262 PUSH_INSN(ret, location, putnil);
3263 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), backref_val, PUSH_VAL(DEFINED_GVAR));
3265 return;
3267 case PM_NUMBERED_REFERENCE_READ_NODE: {
3268 uint32_t reference_number = ((const pm_numbered_reference_read_node_t *) node)->number;
3270 PUSH_INSN(ret, location, putnil);
3271 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), INT2FIX(reference_number << 1), PUSH_VAL(DEFINED_GVAR));
3273 return;
3275 case PM_GLOBAL_VARIABLE_READ_NODE: {
3276 const pm_global_variable_read_node_t *cast = (const pm_global_variable_read_node_t *) node;
3277 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
3279 PUSH_INSN(ret, location, putnil);
3280 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), name, PUSH_VAL(DEFINED_GVAR));
3282 return;
3284 case PM_CLASS_VARIABLE_READ_NODE: {
3285 const pm_class_variable_read_node_t *cast = (const pm_class_variable_read_node_t *) node;
3286 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
3288 PUSH_INSN(ret, location, putnil);
3289 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), name, PUSH_VAL(DEFINED_CVAR));
3291 return;
3293 case PM_CONSTANT_READ_NODE: {
3294 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
3295 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
3297 PUSH_INSN(ret, location, putnil);
3298 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), name, PUSH_VAL(DEFINED_CONST));
3300 return;
3302 case PM_CONSTANT_PATH_NODE: {
3303 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
3304 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
3306 if (cast->parent != NULL) {
3307 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
3308 pm_compile_defined_expr0(iseq, cast->parent, node_location, ret, popped, scope_node, true, lfinish, false);
3310 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3311 PM_COMPILE(cast->parent);
3313 else {
3314 PUSH_INSN1(ret, location, putobject, rb_cObject);
3317 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), name, PUSH_VAL(DEFINED_CONST));
3318 return;
3320 case PM_CALL_NODE: {
3321 const pm_call_node_t *cast = ((const pm_call_node_t *) node);
3322 ID method_id = pm_constant_id_lookup(scope_node, cast->name);
3324 if (cast->receiver || cast->arguments) {
3325 if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
3326 if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.line);
3329 if (cast->arguments) {
3330 pm_compile_defined_expr0(iseq, (const pm_node_t *) cast->arguments, node_location, ret, popped, scope_node, true, lfinish, false);
3331 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3334 if (cast->receiver) {
3335 pm_compile_defined_expr0(iseq, cast->receiver, node_location, ret, popped, scope_node, true, lfinish, true);
3337 if (PM_NODE_TYPE_P(cast->receiver, PM_CALL_NODE)) {
3338 PUSH_INSNL(ret, location, branchunless, lfinish[2]);
3340 const pm_call_node_t *receiver = (const pm_call_node_t *) cast->receiver;
3341 ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
3342 pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
3344 else {
3345 PUSH_INSNL(ret, location, branchunless, lfinish[1]);
3346 PM_COMPILE(cast->receiver);
3349 if (explicit_receiver) PUSH_INSN(ret, location, dup);
3350 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_METHOD), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
3352 else {
3353 PUSH_INSN(ret, location, putself);
3354 if (explicit_receiver) PUSH_INSN(ret, location, dup);
3355 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_FUNC), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
3358 return;
3360 case PM_YIELD_NODE:
3361 PUSH_INSN(ret, location, putnil);
3362 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
3363 return;
3364 case PM_SUPER_NODE:
3365 case PM_FORWARDING_SUPER_NODE:
3366 PUSH_INSN(ret, location, putnil);
3367 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
3368 return;
3369 case PM_CALL_AND_WRITE_NODE:
3370 case PM_CALL_OPERATOR_WRITE_NODE:
3371 case PM_CALL_OR_WRITE_NODE:
3373 case PM_CONSTANT_WRITE_NODE:
3374 case PM_CONSTANT_OPERATOR_WRITE_NODE:
3375 case PM_CONSTANT_AND_WRITE_NODE:
3376 case PM_CONSTANT_OR_WRITE_NODE:
3378 case PM_CONSTANT_PATH_AND_WRITE_NODE:
3379 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
3380 case PM_CONSTANT_PATH_OR_WRITE_NODE:
3381 case PM_CONSTANT_PATH_WRITE_NODE:
3383 case PM_GLOBAL_VARIABLE_WRITE_NODE:
3384 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
3385 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
3386 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
3388 case PM_CLASS_VARIABLE_WRITE_NODE:
3389 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
3390 case PM_CLASS_VARIABLE_AND_WRITE_NODE:
3391 case PM_CLASS_VARIABLE_OR_WRITE_NODE:
3393 case PM_INDEX_AND_WRITE_NODE:
3394 case PM_INDEX_OPERATOR_WRITE_NODE:
3395 case PM_INDEX_OR_WRITE_NODE:
3397 case PM_INSTANCE_VARIABLE_WRITE_NODE:
3398 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
3399 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
3400 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
3402 case PM_LOCAL_VARIABLE_WRITE_NODE:
3403 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
3404 case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
3405 case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
3407 case PM_MULTI_WRITE_NODE:
3408 dtype = DEFINED_ASGN;
3409 break;
3410 default:
3411 rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
3414 RUBY_ASSERT(dtype != DEFINED_NOT_DEFINED);
3415 PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
3416 #undef PUSH_VAL
3419 static void
3420 pm_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition, LABEL **lfinish, bool explicit_receiver)
3422 LINK_ELEMENT *lcur = ret->last;
3423 pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish, false);
3425 if (lfinish[1]) {
3426 LABEL *lstart = NEW_LABEL(node_location->line);
3427 LABEL *lend = NEW_LABEL(node_location->line);
3429 struct rb_iseq_new_with_callback_callback_func *ifunc =
3430 rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
3432 const rb_iseq_t *rescue = new_child_iseq_with_callback(
3433 iseq,
3434 ifunc,
3435 rb_str_concat(rb_str_new2("defined guard in "), ISEQ_BODY(iseq)->location.label),
3436 iseq,
3437 ISEQ_TYPE_RESCUE,
3441 lstart->rescued = LABEL_RESCUE_BEG;
3442 lend->rescued = LABEL_RESCUE_END;
3444 APPEND_LABEL(ret, lcur, lstart);
3445 PUSH_LABEL(ret, lend);
3446 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
3450 static void
3451 pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition)
3453 LABEL *lfinish[3];
3454 LINK_ELEMENT *last = ret->last;
3456 lfinish[0] = NEW_LABEL(node_location->line);
3457 lfinish[1] = 0;
3458 lfinish[2] = 0;
3460 if (!popped) {
3461 pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish, false);
3464 if (lfinish[1]) {
3465 ELEM_INSERT_NEXT(last, &new_insn_body(iseq, node_location->line, node_location->column, BIN(putnil), 0)->link);
3466 PUSH_INSN(ret, *node_location, swap);
3468 if (lfinish[2]) PUSH_LABEL(ret, lfinish[2]);
3469 PUSH_INSN(ret, *node_location, pop);
3470 PUSH_LABEL(ret, lfinish[1]);
3474 PUSH_LABEL(ret, lfinish[0]);
3477 // This is exactly the same as add_ensure_iseq, except it compiled
3478 // the node as a Prism node, and not a CRuby node
3479 static void
3480 pm_add_ensure_iseq(LINK_ANCHOR *const ret, rb_iseq_t *iseq, int is_return, pm_scope_node_t *scope_node)
3482 RUBY_ASSERT(can_add_ensure_iseq(iseq));
3484 struct iseq_compile_data_ensure_node_stack *enlp =
3485 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
3486 struct iseq_compile_data_ensure_node_stack *prev_enlp = enlp;
3487 DECL_ANCHOR(ensure);
3489 INIT_ANCHOR(ensure);
3490 while (enlp) {
3491 if (enlp->erange != NULL) {
3492 DECL_ANCHOR(ensure_part);
3493 LABEL *lstart = NEW_LABEL(0);
3494 LABEL *lend = NEW_LABEL(0);
3495 INIT_ANCHOR(ensure_part);
3497 add_ensure_range(iseq, enlp->erange, lstart, lend);
3499 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
3500 PUSH_LABEL(ensure_part, lstart);
3501 bool popped = true;
3502 PM_COMPILE_INTO_ANCHOR(ensure_part, (const pm_node_t *) enlp->ensure_node);
3503 PUSH_LABEL(ensure_part, lend);
3504 PUSH_SEQ(ensure, ensure_part);
3506 else {
3507 if (!is_return) {
3508 break;
3511 enlp = enlp->prev;
3513 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
3514 PUSH_SEQ(ret, ensure);
3517 struct pm_local_table_insert_ctx {
3518 pm_scope_node_t *scope_node;
3519 rb_ast_id_table_t *local_table_for_iseq;
3520 int local_index;
3523 static int
3524 pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
3526 if (!existing) {
3527 pm_constant_id_t constant_id = (pm_constant_id_t) *key;
3528 struct pm_local_table_insert_ctx * ctx = (struct pm_local_table_insert_ctx *) arg;
3530 pm_scope_node_t *scope_node = ctx->scope_node;
3531 rb_ast_id_table_t *local_table_for_iseq = ctx->local_table_for_iseq;
3532 int local_index = ctx->local_index;
3534 ID local = pm_constant_id_lookup(scope_node, constant_id);
3535 local_table_for_iseq->ids[local_index] = local;
3537 *value = (st_data_t)local_index;
3539 ctx->local_index++;
3542 return ST_CONTINUE;
3546 * Insert a local into the local table for the iseq. This is used to create the
3547 * local table in the correct order while compiling the scope. The locals being
3548 * inserted are regular named locals, as opposed to special forwarding locals.
3550 static void
3551 pm_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)
3553 RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
3555 ID local = pm_constant_id_lookup(scope_node, constant_id);
3556 local_table_for_iseq->ids[local_index] = local;
3557 st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
3561 * Insert a local into the local table for the iseq that is a special forwarding
3562 * local variable.
3564 static void
3565 pm_insert_local_special(ID local_name, int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq)
3567 local_table_for_iseq->ids[local_index] = local_name;
3568 st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
3572 * Compile the locals of a multi target node that is used as a positional
3573 * parameter in a method, block, or lambda definition. Note that this doesn't
3574 * actually add any instructions to the iseq. Instead, it adds locals to the
3575 * local and index lookup tables and increments the local index as necessary.
3577 static int
3578 pm_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)
3580 for (size_t index = 0; index < node->lefts.size; index++) {
3581 const pm_node_t *left = node->lefts.nodes[index];
3583 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
3584 if (!PM_NODE_FLAG_P(left, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
3585 pm_insert_local_index(((const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
3586 local_index++;
3589 else {
3590 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
3591 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);
3595 if (node->rest != NULL && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE)) {
3596 const pm_splat_node_t *rest = (const pm_splat_node_t *) node->rest;
3598 if (rest->expression != NULL) {
3599 RUBY_ASSERT(PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE));
3601 if (!PM_NODE_FLAG_P(rest->expression, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
3602 pm_insert_local_index(((const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
3603 local_index++;
3608 for (size_t index = 0; index < node->rights.size; index++) {
3609 const pm_node_t *right = node->rights.nodes[index];
3611 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
3612 if (!PM_NODE_FLAG_P(right, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
3613 pm_insert_local_index(((const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
3614 local_index++;
3617 else {
3618 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
3619 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);
3623 return local_index;
3627 * Compile a required parameter node that is part of a destructure that is used
3628 * as a positional parameter in a method, block, or lambda definition.
3630 static inline void
3631 pm_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)
3633 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
3634 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, node->name, 0);
3635 PUSH_SETLOCAL(ret, location, index.index, index.level);
3639 * Compile a multi target node that is used as a positional parameter in a
3640 * method, block, or lambda definition. Note that this is effectively the same
3641 * as a multi write, but with the added context that all of the targets
3642 * contained in the write are required parameter nodes. With this context, we
3643 * know they won't have any parent expressions so we build a separate code path
3644 * for this simplified case.
3646 static void
3647 pm_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)
3649 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
3650 bool has_rest = (node->rest && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE) && (((const pm_splat_node_t *) node->rest)->expression) != NULL);
3651 bool has_rights = node->rights.size > 0;
3653 int flag = (has_rest || has_rights) ? 1 : 0;
3654 PUSH_INSN2(ret, location, expandarray, INT2FIX(node->lefts.size), INT2FIX(flag));
3656 for (size_t index = 0; index < node->lefts.size; index++) {
3657 const pm_node_t *left = node->lefts.nodes[index];
3659 if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
3660 pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) left, ret, scope_node);
3662 else {
3663 RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
3664 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) left, ret, scope_node);
3668 if (has_rest) {
3669 if (has_rights) {
3670 PUSH_INSN2(ret, location, expandarray, INT2FIX(node->rights.size), INT2FIX(3));
3673 const pm_node_t *rest = ((const pm_splat_node_t *) node->rest)->expression;
3674 RUBY_ASSERT(PM_NODE_TYPE_P(rest, PM_REQUIRED_PARAMETER_NODE));
3676 pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) rest, ret, scope_node);
3679 if (has_rights) {
3680 if (!has_rest) {
3681 PUSH_INSN2(ret, location, expandarray, INT2FIX(node->rights.size), INT2FIX(2));
3684 for (size_t index = 0; index < node->rights.size; index++) {
3685 const pm_node_t *right = node->rights.nodes[index];
3687 if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
3688 pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) right, ret, scope_node);
3690 else {
3691 RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
3692 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) right, ret, scope_node);
3699 * This is a node in the multi target state linked list. It tracks the
3700 * information for a particular target that necessarily has a parent expression.
3702 typedef struct pm_multi_target_state_node {
3703 // The pointer to the topn instruction that will need to be modified after
3704 // we know the total stack size of all of the targets.
3705 INSN *topn;
3707 // The index of the stack from the base of the entire multi target at which
3708 // the parent expression is located.
3709 size_t stack_index;
3711 // The number of slots in the stack that this node occupies.
3712 size_t stack_size;
3714 // The position of the node in the list of targets.
3715 size_t position;
3717 // A pointer to the next node in this linked list.
3718 struct pm_multi_target_state_node *next;
3719 } pm_multi_target_state_node_t;
3722 * As we're compiling a multi target, we need to track additional information
3723 * whenever there is a parent expression on the left hand side of the target.
3724 * This is because we need to go back and tell the expression where to fetch its
3725 * parent expression from the stack. We use a linked list of nodes to track this
3726 * information.
3728 typedef struct {
3729 // The total number of slots in the stack that this multi target occupies.
3730 size_t stack_size;
3732 // The position of the current node being compiled. This is forwarded to
3733 // nodes when they are allocated.
3734 size_t position;
3736 // A pointer to the head of this linked list.
3737 pm_multi_target_state_node_t *head;
3739 // A pointer to the tail of this linked list.
3740 pm_multi_target_state_node_t *tail;
3741 } pm_multi_target_state_t;
3744 * Push a new state node onto the multi target state.
3746 static void
3747 pm_multi_target_state_push(pm_multi_target_state_t *state, INSN *topn, size_t stack_size)
3749 pm_multi_target_state_node_t *node = ALLOC(pm_multi_target_state_node_t);
3750 node->topn = topn;
3751 node->stack_index = state->stack_size + 1;
3752 node->stack_size = stack_size;
3753 node->position = state->position;
3754 node->next = NULL;
3756 if (state->head == NULL) {
3757 state->head = node;
3758 state->tail = node;
3760 else {
3761 state->tail->next = node;
3762 state->tail = node;
3765 state->stack_size += stack_size;
3769 * Walk through a multi target state's linked list and update the topn
3770 * instructions that were inserted into the write sequence to make sure they can
3771 * correctly retrieve their parent expressions.
3773 static void
3774 pm_multi_target_state_update(pm_multi_target_state_t *state)
3776 // If nothing was ever pushed onto the stack, then we don't need to do any
3777 // kind of updates.
3778 if (state->stack_size == 0) return;
3780 pm_multi_target_state_node_t *current = state->head;
3781 pm_multi_target_state_node_t *previous;
3783 while (current != NULL) {
3784 VALUE offset = INT2FIX(state->stack_size - current->stack_index + current->position);
3785 current->topn->operands[0] = offset;
3787 // stack_size will be > 1 in the case that we compiled an index target
3788 // and it had arguments. In this case, we use multiple topn instructions
3789 // to grab up all of the arguments as well, so those offsets need to be
3790 // updated as well.
3791 if (current->stack_size > 1) {
3792 INSN *insn = current->topn;
3794 for (size_t index = 1; index < current->stack_size; index += 1) {
3795 LINK_ELEMENT *element = get_next_insn(insn);
3796 RUBY_ASSERT(IS_INSN(element));
3798 insn = (INSN *) element;
3799 RUBY_ASSERT(insn->insn_id == BIN(topn));
3801 insn->operands[0] = offset;
3805 previous = current;
3806 current = current->next;
3808 xfree(previous);
3812 static size_t
3813 pm_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);
3816 * A target node represents an indirect write to a variable or a method call to
3817 * a method ending in =. Compiling one of these nodes requires three sequences:
3819 * * The first is to compile retrieving the parent expression if there is one.
3820 * This could be the object that owns a constant or the receiver of a method
3821 * call.
3822 * * The second is to compile the writes to the targets. This could be writing
3823 * to variables, or it could be performing method calls.
3824 * * The third is to compile any cleanup that needs to happen, i.e., popping the
3825 * appropriate number of values off the stack.
3827 * When there is a parent expression and this target is part of a multi write, a
3828 * topn instruction will be inserted into the write sequence. This is to move
3829 * the parent expression to the top of the stack so that it can be used as the
3830 * receiver of the method call or the owner of the constant. To facilitate this,
3831 * we return a pointer to the topn instruction that was used to be later
3832 * modified with the correct offset.
3834 * These nodes can appear in a couple of places, but most commonly:
3836 * * For loops - the index variable is a target node
3837 * * Rescue clauses - the exception reference variable is a target node
3838 * * Multi writes - the left hand side contains a list of target nodes
3840 * For the comments with examples within this function, we'll use for loops as
3841 * the containing node.
3843 static void
3844 pm_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)
3846 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
3848 switch (PM_NODE_TYPE(node)) {
3849 case PM_LOCAL_VARIABLE_TARGET_NODE: {
3850 // Local variable targets have no parent expression, so they only need
3851 // to compile the write.
3853 // for i in []; end
3855 const pm_local_variable_target_node_t *cast = (const pm_local_variable_target_node_t *) node;
3856 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
3858 PUSH_SETLOCAL(writes, location, index.index, index.level);
3859 break;
3861 case PM_CLASS_VARIABLE_TARGET_NODE: {
3862 // Class variable targets have no parent expression, so they only need
3863 // to compile the write.
3865 // for @@i in []; end
3867 const pm_class_variable_target_node_t *cast = (const pm_class_variable_target_node_t *) node;
3868 ID name = pm_constant_id_lookup(scope_node, cast->name);
3870 PUSH_INSN2(writes, location, setclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
3871 break;
3873 case PM_CONSTANT_TARGET_NODE: {
3874 // Constant targets have no parent expression, so they only need to
3875 // compile the write.
3877 // for I in []; end
3879 const pm_constant_target_node_t *cast = (const pm_constant_target_node_t *) node;
3880 ID name = pm_constant_id_lookup(scope_node, cast->name);
3882 PUSH_INSN1(writes, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
3883 PUSH_INSN1(writes, location, setconstant, ID2SYM(name));
3884 break;
3886 case PM_GLOBAL_VARIABLE_TARGET_NODE: {
3887 // Global variable targets have no parent expression, so they only need
3888 // to compile the write.
3890 // for $i in []; end
3892 const pm_global_variable_target_node_t *cast = (const pm_global_variable_target_node_t *) node;
3893 ID name = pm_constant_id_lookup(scope_node, cast->name);
3895 PUSH_INSN1(writes, location, setglobal, ID2SYM(name));
3896 break;
3898 case PM_INSTANCE_VARIABLE_TARGET_NODE: {
3899 // Instance variable targets have no parent expression, so they only
3900 // need to compile the write.
3902 // for @i in []; end
3904 const pm_instance_variable_target_node_t *cast = (const pm_instance_variable_target_node_t *) node;
3905 ID name = pm_constant_id_lookup(scope_node, cast->name);
3907 PUSH_INSN2(writes, location, setinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
3908 break;
3910 case PM_CONSTANT_PATH_TARGET_NODE: {
3911 // Constant path targets have a parent expression that is the object
3912 // that owns the constant. This needs to be compiled first into the
3913 // parents sequence. If no parent is found, then it represents using the
3914 // unary :: operator to indicate a top-level constant. In that case we
3915 // need to push Object onto the stack.
3917 // for I::J in []; end
3919 const pm_constant_path_target_node_t *cast = (const pm_constant_path_target_node_t *) node;
3920 ID name = pm_constant_id_lookup(scope_node, cast->name);
3922 if (cast->parent != NULL) {
3923 pm_compile_node(iseq, cast->parent, parents, false, scope_node);
3925 else {
3926 PUSH_INSN1(parents, location, putobject, rb_cObject);
3929 if (state == NULL) {
3930 PUSH_INSN(writes, location, swap);
3932 else {
3933 PUSH_INSN1(writes, location, topn, INT2FIX(1));
3934 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
3937 PUSH_INSN1(writes, location, setconstant, ID2SYM(name));
3939 if (state != NULL) {
3940 PUSH_INSN(cleanup, location, pop);
3943 break;
3945 case PM_CALL_TARGET_NODE: {
3946 // Call targets have a parent expression that is the receiver of the
3947 // method being called. This needs to be compiled first into the parents
3948 // sequence. These nodes cannot have arguments, so the method call is
3949 // compiled with a single argument which represents the value being
3950 // written.
3952 // for i.j in []; end
3954 const pm_call_target_node_t *cast = (const pm_call_target_node_t *) node;
3955 ID method_id = pm_constant_id_lookup(scope_node, cast->name);
3957 pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
3959 if (state != NULL) {
3960 PUSH_INSN1(writes, location, topn, INT2FIX(1));
3961 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
3962 PUSH_INSN(writes, location, swap);
3965 int flags = VM_CALL_ARGS_SIMPLE;
3966 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
3968 PUSH_SEND_WITH_FLAG(writes, location, method_id, INT2FIX(1), INT2FIX(flags));
3969 PUSH_INSN(writes, location, pop);
3971 if (state != NULL) {
3972 PUSH_INSN(cleanup, location, pop);
3975 break;
3977 case PM_INDEX_TARGET_NODE: {
3978 // Index targets have a parent expression that is the receiver of the
3979 // method being called and any additional arguments that are being
3980 // passed along with the value being written. The receiver and arguments
3981 // both need to be on the stack. Note that this is even more complicated
3982 // by the fact that these nodes can hold a block using the unary &
3983 // operator.
3985 // for i[:j] in []; end
3987 const pm_index_target_node_t *cast = (const pm_index_target_node_t *) node;
3989 pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
3991 int flags = 0;
3992 struct rb_callinfo_kwarg *kwargs = NULL;
3993 int argc = pm_setup_args(cast->arguments, cast->block, &flags, &kwargs, iseq, parents, scope_node, &location);
3995 if (state != NULL) {
3996 PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
3997 pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), argc + 1);
3999 if (argc == 0) {
4000 PUSH_INSN(writes, location, swap);
4002 else {
4003 for (int index = 0; index < argc; index++) {
4004 PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
4006 PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
4010 // The argc that we're going to pass to the send instruction is the
4011 // number of arguments + 1 for the value being written. If there's a
4012 // splat, then we need to insert newarray and concatarray instructions
4013 // after the arguments have been written.
4014 int ci_argc = argc + 1;
4015 if (flags & VM_CALL_ARGS_SPLAT) {
4016 ci_argc--;
4017 PUSH_INSN1(writes, location, newarray, INT2FIX(1));
4018 PUSH_INSN(writes, location, concatarray);
4021 PUSH_SEND_R(writes, location, idASET, INT2NUM(ci_argc), NULL, INT2FIX(flags), kwargs);
4022 PUSH_INSN(writes, location, pop);
4024 if (state != NULL) {
4025 if (argc != 0) {
4026 PUSH_INSN(writes, location, pop);
4029 for (int index = 0; index < argc + 1; index++) {
4030 PUSH_INSN(cleanup, location, pop);
4034 break;
4036 case PM_MULTI_TARGET_NODE: {
4037 // Multi target nodes represent a set of writes to multiple variables.
4038 // The parent expressions are the combined set of the parent expressions
4039 // of its inner target nodes.
4041 // for i, j in []; end
4043 if (state != NULL) state->position--;
4044 pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
4045 if (state != NULL) state->position++;
4046 break;
4048 default:
4049 rb_bug("Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4050 break;
4055 * Compile a multi target or multi write node. It returns the number of values
4056 * on the stack that correspond to the parent expressions of the various
4057 * targets.
4059 static size_t
4060 pm_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)
4062 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
4063 const pm_node_list_t *lefts;
4064 const pm_node_t *rest;
4065 const pm_node_list_t *rights;
4067 switch (PM_NODE_TYPE(node)) {
4068 case PM_MULTI_TARGET_NODE: {
4069 const pm_multi_target_node_t *cast = (const pm_multi_target_node_t *) node;
4070 lefts = &cast->lefts;
4071 rest = cast->rest;
4072 rights = &cast->rights;
4073 break;
4075 case PM_MULTI_WRITE_NODE: {
4076 const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
4077 lefts = &cast->lefts;
4078 rest = cast->rest;
4079 rights = &cast->rights;
4080 break;
4082 default:
4083 rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4084 break;
4087 bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((const pm_splat_node_t *) rest)->expression != NULL;
4088 bool has_posts = rights->size > 0;
4090 // The first instruction in the writes sequence is going to spread the
4091 // top value of the stack onto the number of values that we're going to
4092 // write.
4093 PUSH_INSN2(writes, location, expandarray, INT2FIX(lefts->size), INT2FIX((has_rest || has_posts) ? 1 : 0));
4095 // We need to keep track of some additional state information as we're
4096 // going through the targets because we will need to revisit them once
4097 // we know how many values are being pushed onto the stack.
4098 pm_multi_target_state_t target_state = { 0 };
4099 size_t base_position = state == NULL ? 0 : state->position;
4100 size_t splat_position = has_rest ? 1 : 0;
4102 // Next, we'll iterate through all of the leading targets.
4103 for (size_t index = 0; index < lefts->size; index++) {
4104 const pm_node_t *target = lefts->nodes[index];
4105 target_state.position = lefts->size - index + splat_position + base_position;
4106 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, &target_state);
4109 // Next, we'll compile the rest target if there is one.
4110 if (has_rest) {
4111 const pm_node_t *target = ((const pm_splat_node_t *) rest)->expression;
4112 target_state.position = 1 + rights->size + base_position;
4114 if (has_posts) {
4115 PUSH_INSN2(writes, location, expandarray, INT2FIX(rights->size), INT2FIX(3));
4118 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, &target_state);
4121 // Finally, we'll compile the trailing targets.
4122 if (has_posts) {
4123 if (!has_rest && rest != NULL) {
4124 PUSH_INSN2(writes, location, expandarray, INT2FIX(rights->size), INT2FIX(2));
4127 for (size_t index = 0; index < rights->size; index++) {
4128 const pm_node_t *target = rights->nodes[index];
4129 target_state.position = rights->size - index + base_position;
4130 pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, &target_state);
4134 // Now, we need to go back and modify the topn instructions in order to
4135 // ensure they can correctly retrieve the parent expressions.
4136 pm_multi_target_state_update(&target_state);
4138 if (state != NULL) state->stack_size += target_state.stack_size;
4140 return target_state.stack_size;
4144 * When compiling a for loop, we need to write the iteration variable to
4145 * whatever expression exists in the index slot. This function performs that
4146 * compilation.
4148 static void
4149 pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
4151 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
4153 switch (PM_NODE_TYPE(node)) {
4154 case PM_LOCAL_VARIABLE_TARGET_NODE: {
4155 // For local variables, all we have to do is retrieve the value and then
4156 // compile the index node.
4157 PUSH_GETLOCAL(ret, location, 1, 0);
4158 pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
4159 break;
4161 case PM_CLASS_VARIABLE_TARGET_NODE:
4162 case PM_CONSTANT_TARGET_NODE:
4163 case PM_GLOBAL_VARIABLE_TARGET_NODE:
4164 case PM_INSTANCE_VARIABLE_TARGET_NODE:
4165 case PM_CONSTANT_PATH_TARGET_NODE:
4166 case PM_CALL_TARGET_NODE:
4167 case PM_INDEX_TARGET_NODE: {
4168 // For other targets, we need to potentially compile the parent or
4169 // owning expression of this target, then retrieve the value, expand it,
4170 // and then compile the necessary writes.
4171 DECL_ANCHOR(writes);
4172 INIT_ANCHOR(writes);
4174 DECL_ANCHOR(cleanup);
4175 INIT_ANCHOR(cleanup);
4177 pm_multi_target_state_t state = { 0 };
4178 state.position = 1;
4179 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
4181 PUSH_GETLOCAL(ret, location, 1, 0);
4182 PUSH_INSN2(ret, location, expandarray, INT2FIX(1), INT2FIX(0));
4184 PUSH_SEQ(ret, writes);
4185 PUSH_SEQ(ret, cleanup);
4187 pm_multi_target_state_update(&state);
4188 break;
4190 case PM_MULTI_TARGET_NODE: {
4191 DECL_ANCHOR(writes);
4192 INIT_ANCHOR(writes);
4194 DECL_ANCHOR(cleanup);
4195 INIT_ANCHOR(cleanup);
4197 pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
4199 LABEL *not_single = NEW_LABEL(location.line);
4200 LABEL *not_ary = NEW_LABEL(location.line);
4202 // When there are multiple targets, we'll do a bunch of work to convert
4203 // the value into an array before we expand it. Effectively we're trying
4204 // to accomplish:
4206 // (args.length == 1 && Array.try_convert(args[0])) || args
4208 PUSH_GETLOCAL(ret, location, 1, 0);
4209 PUSH_INSN(ret, location, dup);
4210 PUSH_CALL(ret, location, idLength, INT2FIX(0));
4211 PUSH_INSN1(ret, location, putobject, INT2FIX(1));
4212 PUSH_CALL(ret, location, idEq, INT2FIX(1));
4213 PUSH_INSNL(ret, location, branchunless, not_single);
4214 PUSH_INSN(ret, location, dup);
4215 PUSH_INSN1(ret, location, putobject, INT2FIX(0));
4216 PUSH_CALL(ret, location, idAREF, INT2FIX(1));
4217 PUSH_INSN1(ret, location, putobject, rb_cArray);
4218 PUSH_INSN(ret, location, swap);
4219 PUSH_CALL(ret, location, rb_intern("try_convert"), INT2FIX(1));
4220 PUSH_INSN(ret, location, dup);
4221 PUSH_INSNL(ret, location, branchunless, not_ary);
4222 PUSH_INSN(ret, location, swap);
4224 PUSH_LABEL(ret, not_ary);
4225 PUSH_INSN(ret, location, pop);
4227 PUSH_LABEL(ret, not_single);
4228 PUSH_SEQ(ret, writes);
4229 PUSH_SEQ(ret, cleanup);
4230 break;
4232 default:
4233 rb_bug("Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
4234 break;
4238 static void
4239 pm_compile_rescue(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
4241 const pm_parser_t *parser = scope_node->parser;
4243 LABEL *lstart = NEW_LABEL(node_location->line);
4244 LABEL *lend = NEW_LABEL(node_location->line);
4245 LABEL *lcont = NEW_LABEL(node_location->line);
4247 pm_scope_node_t rescue_scope_node;
4248 pm_scope_node_init((const pm_node_t *) cast->rescue_clause, &rescue_scope_node, scope_node);
4250 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
4251 &rescue_scope_node,
4252 rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
4253 ISEQ_TYPE_RESCUE,
4254 pm_node_line_number(parser, (const pm_node_t *) cast->rescue_clause)
4257 pm_scope_node_destroy(&rescue_scope_node);
4259 lstart->rescued = LABEL_RESCUE_BEG;
4260 lend->rescued = LABEL_RESCUE_END;
4261 PUSH_LABEL(ret, lstart);
4263 bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
4264 ISEQ_COMPILE_DATA(iseq)->in_rescue = true;
4266 if (cast->statements != NULL) {
4267 PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->statements);
4269 else {
4270 PUSH_INSN(ret, *node_location, putnil);
4273 ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
4274 PUSH_LABEL(ret, lend);
4276 if (cast->else_clause != NULL) {
4277 if (!popped) PUSH_INSN(ret, *node_location, pop);
4278 PM_COMPILE((const pm_node_t *) cast->else_clause);
4281 PUSH_INSN(ret, *node_location, nop);
4282 PUSH_LABEL(ret, lcont);
4284 if (popped) PUSH_INSN(ret, *node_location, pop);
4285 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
4286 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
4289 static void
4290 pm_compile_ensure(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_line_column_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
4292 const pm_parser_t *parser = scope_node->parser;
4293 const pm_statements_node_t *statements = cast->ensure_clause->statements;
4294 const pm_line_column_t location = statements != NULL ? PM_NODE_START_LINE_COLUMN(parser, statements) : *node_location;
4296 LABEL *estart = NEW_LABEL(location.line);
4297 LABEL *eend = NEW_LABEL(location.line);
4298 LABEL *econt = NEW_LABEL(location.line);
4300 struct ensure_range er;
4301 struct iseq_compile_data_ensure_node_stack enl;
4302 struct ensure_range *erange;
4304 er.begin = estart;
4305 er.end = eend;
4306 er.next = 0;
4307 push_ensure_entry(iseq, &enl, &er, (void *) cast->ensure_clause);
4309 PUSH_LABEL(ret, estart);
4310 if (cast->rescue_clause) {
4311 pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
4313 else {
4314 if (cast->statements) {
4315 PM_COMPILE((const pm_node_t *) cast->statements);
4317 else if (!popped) {
4318 PUSH_INSN(ret, *node_location, putnil);
4322 PUSH_LABEL(ret, eend);
4323 PUSH_LABEL(ret, econt);
4325 pm_scope_node_t next_scope_node;
4326 pm_scope_node_init((const pm_node_t *) cast->ensure_clause, &next_scope_node, scope_node);
4328 rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(
4329 &next_scope_node,
4330 rb_str_concat(rb_str_new2("ensure in "), ISEQ_BODY(iseq)->location.label),
4331 ISEQ_TYPE_ENSURE,
4332 location.line
4335 pm_scope_node_destroy(&next_scope_node);
4336 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
4338 erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
4339 if (estart->link.next != &eend->link) {
4340 while (erange) {
4341 PUSH_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, econt);
4342 erange = erange->next;
4345 ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
4347 // Compile the ensure entry
4348 if (statements != NULL) {
4349 PM_COMPILE((const pm_node_t *) statements);
4350 if (!popped) PUSH_INSN(ret, *node_location, pop);
4355 * Returns true if the given call node can use the opt_str_uminus or
4356 * opt_str_freeze instructions as an optimization with the current iseq options.
4358 static inline bool
4359 pm_opt_str_freeze_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
4361 return (
4362 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
4363 node->receiver != NULL &&
4364 PM_NODE_TYPE_P(node->receiver, PM_STRING_NODE) &&
4365 node->arguments == NULL &&
4366 node->block == NULL &&
4367 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
4372 * Returns true if the given call node can use the opt_aref_with optimization
4373 * with the current iseq options.
4375 static inline bool
4376 pm_opt_aref_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
4378 return (
4379 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
4380 node->arguments != NULL &&
4381 PM_NODE_TYPE_P((const pm_node_t *) node->arguments, PM_ARGUMENTS_NODE) &&
4382 ((const pm_arguments_node_t *) node->arguments)->arguments.size == 1 &&
4383 PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
4384 node->block == NULL &&
4385 !PM_NODE_FLAG_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
4386 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
4391 * Returns true if the given call node can use the opt_aset_with optimization
4392 * with the current iseq options.
4394 static inline bool
4395 pm_opt_aset_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
4397 return (
4398 !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
4399 node->arguments != NULL &&
4400 PM_NODE_TYPE_P((const pm_node_t *) node->arguments, PM_ARGUMENTS_NODE) &&
4401 ((const pm_arguments_node_t *) node->arguments)->arguments.size == 2 &&
4402 PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
4403 node->block == NULL &&
4404 !PM_NODE_FLAG_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
4405 ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
4410 * Compile the instructions necessary to read a constant, based on the options
4411 * of the current iseq.
4413 static void
4414 pm_compile_constant_read(rb_iseq_t *iseq, VALUE name, const pm_location_t *name_loc, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
4416 const pm_line_column_t location = PM_LOCATION_START_LINE_COLUMN(scope_node->parser, name_loc);
4418 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
4419 ISEQ_BODY(iseq)->ic_size++;
4420 VALUE segments = rb_ary_new_from_args(1, name);
4421 PUSH_INSN1(ret, location, opt_getconstant_path, segments);
4423 else {
4424 PUSH_INSN(ret, location, putnil);
4425 PUSH_INSN1(ret, location, putobject, Qtrue);
4426 PUSH_INSN1(ret, location, getconstant, name);
4431 * Returns a Ruby array of the parts of the constant path node if it is constant
4432 * reads all of the way down. If it isn't, then Qnil is returned.
4434 static VALUE
4435 pm_constant_path_parts(const pm_node_t *node, const pm_scope_node_t *scope_node)
4437 VALUE parts = rb_ary_new();
4439 while (true) {
4440 switch (PM_NODE_TYPE(node)) {
4441 case PM_CONSTANT_READ_NODE: {
4442 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
4443 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
4445 rb_ary_unshift(parts, name);
4446 return parts;
4448 case PM_CONSTANT_PATH_NODE: {
4449 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
4450 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
4452 rb_ary_unshift(parts, name);
4453 if (cast->parent == NULL) {
4454 rb_ary_unshift(parts, ID2SYM(idNULL));
4455 return parts;
4458 node = cast->parent;
4459 break;
4461 default:
4462 return Qnil;
4468 * Compile a constant path into two sequences of instructions, one for the
4469 * owning expression if there is one (prefix) and one for the constant reads
4470 * (body).
4472 static void
4473 pm_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)
4475 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
4477 switch (PM_NODE_TYPE(node)) {
4478 case PM_CONSTANT_READ_NODE: {
4479 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
4480 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
4482 PUSH_INSN1(body, location, putobject, Qtrue);
4483 PUSH_INSN1(body, location, getconstant, name);
4484 break;
4486 case PM_CONSTANT_PATH_NODE: {
4487 const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
4488 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
4490 if (cast->parent == NULL) {
4491 PUSH_INSN(body, location, pop);
4492 PUSH_INSN1(body, location, putobject, rb_cObject);
4493 PUSH_INSN1(body, location, putobject, Qtrue);
4494 PUSH_INSN1(body, location, getconstant, name);
4496 else {
4497 pm_compile_constant_path(iseq, cast->parent, prefix, body, false, scope_node);
4498 PUSH_INSN1(body, location, putobject, Qfalse);
4499 PUSH_INSN1(body, location, getconstant, name);
4501 break;
4503 default:
4504 PM_COMPILE_INTO_ANCHOR(prefix, node);
4505 break;
4510 * When we're compiling a case node, it's possible that we can speed it up using
4511 * a dispatch hash, which will allow us to jump directly to the correct when
4512 * clause body based on a hash lookup of the value. This can only happen when
4513 * the conditions are literals that can be compiled into a hash key.
4515 * This function accepts a dispatch hash and the condition of a when clause. It
4516 * is responsible for compiling the condition into a hash key and then adding it
4517 * to the dispatch hash.
4519 * If the value can be successfully compiled into the hash, then this function
4520 * returns the dispatch hash with the new key added. If the value cannot be
4521 * compiled into the hash, then this function returns Qundef. In the case of
4522 * Qundef, this function is signaling that the caller should abandon the
4523 * optimization entirely.
4525 static VALUE
4526 pm_compile_case_node_dispatch(rb_iseq_t *iseq, VALUE dispatch, const pm_node_t *node, LABEL *label, const pm_scope_node_t *scope_node)
4528 VALUE key = Qundef;
4530 switch (PM_NODE_TYPE(node)) {
4531 case PM_FLOAT_NODE: {
4532 key = pm_static_literal_value(iseq, node, scope_node);
4533 double intptr;
4535 if (modf(RFLOAT_VALUE(key), &intptr) == 0.0) {
4536 key = (FIXABLE(intptr) ? LONG2FIX((long) intptr) : rb_dbl2big(intptr));
4539 break;
4541 case PM_FALSE_NODE:
4542 case PM_INTEGER_NODE:
4543 case PM_NIL_NODE:
4544 case PM_SOURCE_FILE_NODE:
4545 case PM_SOURCE_LINE_NODE:
4546 case PM_SYMBOL_NODE:
4547 case PM_TRUE_NODE:
4548 key = pm_static_literal_value(iseq, node, scope_node);
4549 break;
4550 case PM_STRING_NODE: {
4551 const pm_string_node_t *cast = (const pm_string_node_t *) node;
4552 key = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
4553 break;
4555 default:
4556 return Qundef;
4559 if (NIL_P(rb_hash_lookup(dispatch, key))) {
4560 rb_hash_aset(dispatch, key, ((VALUE) label) | 1);
4563 return dispatch;
4567 * Compiles a prism node into instruction sequences.
4569 * iseq - The current instruction sequence object (used for locals)
4570 * node - The prism node to compile
4571 * ret - The linked list of instructions to append instructions onto
4572 * popped - True if compiling something with no side effects, so instructions don't
4573 * need to be added
4574 * scope_node - Stores parser and local information
4576 static void
4577 pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
4579 const pm_parser_t *parser = scope_node->parser;
4580 const pm_line_column_t location = PM_NODE_START_LINE_COLUMN(parser, node);
4581 int lineno = (int) location.line;
4583 if (!PM_NODE_TYPE_P(node, PM_RETURN_NODE) || !PM_NODE_FLAG_P(node, PM_RETURN_NODE_FLAGS_REDUNDANT) || ((const pm_return_node_t *) node)->arguments != NULL) {
4584 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
4585 int event = RUBY_EVENT_LINE;
4587 ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
4588 if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
4589 event |= RUBY_EVENT_COVERAGE_LINE;
4591 PUSH_TRACE(ret, event);
4595 switch (PM_NODE_TYPE(node)) {
4596 case PM_ALIAS_GLOBAL_VARIABLE_NODE: {
4597 // alias $foo $bar
4598 // ^^^^^^^^^^^^^^^
4599 const pm_alias_global_variable_node_t *cast = (const pm_alias_global_variable_node_t *) node;
4600 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4602 const pm_location_t *new_name_loc = &cast->new_name->location;
4603 PUSH_INSN1(ret, location, putobject, ID2SYM(rb_intern3((const char *) new_name_loc->start, new_name_loc->end - new_name_loc->start, scope_node->encoding)));
4605 const pm_location_t *old_name_loc = &cast->old_name->location;
4606 PUSH_INSN1(ret, location, putobject, ID2SYM(rb_intern3((const char *) old_name_loc->start, old_name_loc->end - old_name_loc->start, scope_node->encoding)));
4608 PUSH_SEND(ret, location, id_core_set_variable_alias, INT2FIX(2));
4609 if (popped) PUSH_INSN(ret, location, pop);
4611 return;
4613 case PM_ALIAS_METHOD_NODE: {
4614 // alias foo bar
4615 // ^^^^^^^^^^^^^
4616 const pm_alias_method_node_t *cast = (const pm_alias_method_node_t *) node;
4618 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
4619 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
4620 PM_COMPILE_NOT_POPPED(cast->new_name);
4621 PM_COMPILE_NOT_POPPED(cast->old_name);
4623 PUSH_SEND(ret, location, id_core_set_method_alias, INT2FIX(3));
4624 if (popped) PUSH_INSN(ret, location, pop);
4626 return;
4628 case PM_AND_NODE: {
4629 // a and b
4630 // ^^^^^^^
4631 const pm_and_node_t *cast = (const pm_and_node_t *) node;
4632 LABEL *end_label = NEW_LABEL(lineno);
4634 PM_COMPILE_NOT_POPPED(cast->left);
4635 if (!popped) PUSH_INSN(ret, location, dup);
4636 PUSH_INSNL(ret, location, branchunless, end_label);
4638 if (!popped) PUSH_INSN(ret, location, pop);
4639 PM_COMPILE(cast->right);
4640 PUSH_LABEL(ret, end_label);
4642 return;
4644 case PM_ARGUMENTS_NODE:
4645 // These are ArgumentsNodes that are not compiled directly by their
4646 // parent call nodes, used in the cases of NextNodes, ReturnNodes, and
4647 // BreakNodes. They can create an array like ArrayNode.
4648 case PM_ARRAY_NODE: {
4649 const pm_node_list_t *elements;
4651 if (PM_NODE_TYPE(node) == PM_ARGUMENTS_NODE) {
4652 // break foo
4653 // ^^^
4654 const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
4655 elements = &cast->arguments;
4657 // If we are only returning a single element through one of the jump
4658 // nodes, then we will only compile that node directly.
4659 if (elements->size == 1) {
4660 PM_COMPILE(elements->nodes[0]);
4661 return;
4664 else {
4665 // [foo, bar, baz]
4666 // ^^^^^^^^^^^^^^^
4667 const pm_array_node_t *cast = (const pm_array_node_t *) node;
4668 elements = &cast->elements;
4671 // If every node in the array is static, then we can compile the entire
4672 // array now instead of later.
4673 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
4674 // We're only going to compile this node if it's not popped. If it
4675 // is popped, then we know we don't need to do anything since it's
4676 // statically known.
4677 if (!popped) {
4678 if (elements->size) {
4679 VALUE value = pm_static_literal_value(iseq, node, scope_node);
4680 PUSH_INSN1(ret, location, duparray, value);
4682 else {
4683 PUSH_INSN1(ret, location, newarray, INT2FIX(0));
4687 else {
4688 // Here since we know there are possible side-effects inside the
4689 // array contents, we're going to build it entirely at runtime.
4690 // We'll do this by pushing all of the elements onto the stack and
4691 // then combining them with newarray.
4693 // If this array is popped, then this serves only to ensure we enact
4694 // all side-effects (like method calls) that are contained within
4695 // the array contents.
4697 // We treat all sequences of non-splat elements as their
4698 // own arrays, followed by a newarray, and then continually
4699 // concat the arrays with the SplatNode nodes.
4700 int new_array_size = 0;
4702 bool need_to_concat_array = false;
4703 bool has_kw_splat = false;
4705 for (size_t index = 0; index < elements->size; index++) {
4706 const pm_node_t *element = elements->nodes[index];
4708 if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
4709 const pm_splat_node_t *splat_element = (const pm_splat_node_t *) element;
4711 // If we already have non-splat elements, we need to emit a
4712 // newarray instruction.
4713 if (new_array_size > 0) {
4714 PUSH_INSN1(ret, location, newarray, INT2FIX(new_array_size));
4715 new_array_size = 0;
4717 // We don't want to emit a concat array in the case
4718 // where we're seeing our first splat, and already have
4719 // elements.
4720 if (need_to_concat_array) PUSH_INSN(ret, location, concatarray);
4723 if (splat_element->expression) {
4724 PM_COMPILE_NOT_POPPED(splat_element->expression);
4726 else {
4727 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
4728 PUSH_GETLOCAL(ret, location, index.index, index.level);
4731 if (index > 0) {
4732 PUSH_INSN(ret, location, concatarray);
4734 else {
4735 // If this is the first element of the array then we
4736 // need to splatarray the elements into the list.
4737 PUSH_INSN1(ret, location, splatarray, Qtrue);
4740 // Since we have now seen a splat and are concat-ing arrays,
4741 // all subsequent splats will need to concat as well.
4742 need_to_concat_array = true;
4744 else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
4745 new_array_size++;
4746 has_kw_splat = true;
4747 pm_compile_hash_elements(iseq, element, &((const pm_keyword_hash_node_t *) element)->elements, ret, scope_node);
4749 else {
4750 new_array_size++;
4751 PM_COMPILE_NOT_POPPED(element);
4755 if (new_array_size) {
4756 if (has_kw_splat) {
4757 PUSH_INSN1(ret, location, newarraykwsplat, INT2FIX(new_array_size));
4759 else {
4760 PUSH_INSN1(ret, location, newarray, INT2FIX(new_array_size));
4763 if (need_to_concat_array) PUSH_INSN(ret, location, concatarray);
4766 if (popped) PUSH_INSN(ret, location, pop);
4768 return;
4770 case PM_ASSOC_NODE: {
4771 // { foo: 1 }
4772 // ^^^^^^
4774 // foo(bar: 1)
4775 // ^^^^^^
4776 const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
4778 PM_COMPILE(cast->key);
4779 PM_COMPILE(cast->value);
4781 return;
4783 case PM_ASSOC_SPLAT_NODE: {
4784 // { **foo }
4785 // ^^^^^
4787 // def foo(**); bar(**); end
4788 // ^^
4789 const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
4791 if (cast->value != NULL) {
4792 PM_COMPILE(cast->value);
4794 else if (!popped) {
4795 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
4796 PUSH_GETLOCAL(ret, location, index.index, index.level);
4799 return;
4801 case PM_BACK_REFERENCE_READ_NODE: {
4802 // $+
4803 // ^^
4804 if (!popped) {
4805 // Since a back reference is `$<char>`, ruby represents the ID as the
4806 // an rb_intern on the value after the `$`.
4807 char *char_ptr = (char *)(node->location.start) + 1;
4808 ID backref_val = INT2FIX(rb_intern2(char_ptr, 1)) << 1 | 1;
4809 PUSH_INSN2(ret, location, getspecial, INT2FIX(1), backref_val);
4811 return;
4813 case PM_BEGIN_NODE: {
4814 // begin end
4815 // ^^^^^^^^^
4816 const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
4818 if (cast->ensure_clause) {
4819 // Compiling the ensure clause will compile the rescue clause (if
4820 // there is one), which will compile the begin statements.
4821 pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
4823 else if (cast->rescue_clause) {
4824 // Compiling rescue will compile begin statements (if applicable).
4825 pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
4827 else {
4828 // If there is neither ensure or rescue, the just compile the
4829 // statements.
4830 if (cast->statements != NULL) {
4831 PM_COMPILE((const pm_node_t *) cast->statements);
4833 else if (!popped) {
4834 PUSH_INSN(ret, location, putnil);
4837 return;
4839 case PM_BLOCK_ARGUMENT_NODE: {
4840 // foo(&bar)
4841 // ^^^^
4842 const pm_block_argument_node_t *cast = (const pm_block_argument_node_t *) node;
4844 if (cast->expression != NULL) {
4845 PM_COMPILE(cast->expression);
4847 else {
4848 // If there's no expression, this must be block forwarding.
4849 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
4850 PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1), INT2FIX(local_index.level));
4852 return;
4854 case PM_BREAK_NODE: {
4855 // break
4856 // ^^^^^
4858 // break foo
4859 // ^^^^^^^^^
4860 const pm_break_node_t *cast = (const pm_break_node_t *) node;
4861 unsigned long throw_flag = 0;
4863 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
4864 /* while/until */
4865 LABEL *splabel = NEW_LABEL(0);
4866 PUSH_LABEL(ret, splabel);
4867 PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->redo_label);
4869 if (cast->arguments != NULL) {
4870 PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
4872 else {
4873 PUSH_INSN(ret, location, putnil);
4876 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
4877 PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
4878 PUSH_ADJUST_RESTORE(ret, splabel);
4879 if (!popped) PUSH_INSN(ret, location, putnil);
4881 else {
4882 const rb_iseq_t *ip = iseq;
4884 while (ip) {
4885 if (!ISEQ_COMPILE_DATA(ip)) {
4886 ip = 0;
4887 break;
4890 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
4891 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
4893 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
4894 throw_flag = 0;
4896 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
4897 COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with break");
4898 return;
4900 else {
4901 ip = ISEQ_BODY(ip)->parent_iseq;
4902 continue;
4905 /* escape from block */
4906 if (cast->arguments != NULL) {
4907 PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
4909 else {
4910 PUSH_INSN(ret, location, putnil);
4913 PUSH_INSN1(ret, location, throw, INT2FIX(throw_flag | TAG_BREAK));
4914 if (popped) PUSH_INSN(ret, location, pop);
4916 return;
4919 COMPILE_ERROR(ERROR_ARGS "Invalid break");
4920 rb_bug("Invalid break");
4922 return;
4924 case PM_CALL_NODE: {
4925 // foo
4926 // ^^^
4928 // foo.bar
4929 // ^^^^^^^
4931 // foo.bar() {}
4932 // ^^^^^^^^^^^^
4933 const pm_call_node_t *cast = (const pm_call_node_t *) node;
4934 LABEL *start = NEW_LABEL(location.line);
4936 if (cast->block) {
4937 PUSH_LABEL(ret, start);
4940 ID method_id = pm_constant_id_lookup(scope_node, cast->name);
4942 switch (method_id) {
4943 case idUMinus: {
4944 if (pm_opt_str_freeze_p(iseq, cast)) {
4945 VALUE value = parse_static_literal_string(iseq, scope_node, cast->receiver, &((const pm_string_node_t * ) cast->receiver)->unescaped);
4946 PUSH_INSN2(ret, location, opt_str_uminus, value, new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
4947 return;
4949 break;
4951 case idFreeze: {
4952 if (pm_opt_str_freeze_p(iseq, cast)) {
4953 VALUE value = parse_static_literal_string(iseq, scope_node, cast->receiver, &((const pm_string_node_t * ) cast->receiver)->unescaped);
4954 PUSH_INSN2(ret, location, opt_str_freeze, value, new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
4955 return;
4957 break;
4959 case idAREF: {
4960 if (pm_opt_aref_with_p(iseq, cast)) {
4961 const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) cast->arguments)->arguments.nodes[0];
4962 VALUE value = parse_static_literal_string(iseq, scope_node, (const pm_node_t *) string, &string->unescaped);
4964 PM_COMPILE_NOT_POPPED(cast->receiver);
4965 PUSH_INSN2(ret, location, opt_aref_with, value, new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
4967 if (popped) {
4968 PUSH_INSN(ret, location, pop);
4971 return;
4973 break;
4975 case idASET: {
4976 if (pm_opt_aset_with_p(iseq, cast)) {
4977 const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) cast->arguments)->arguments.nodes[0];
4978 VALUE value = parse_static_literal_string(iseq, scope_node, (const pm_node_t *) string, &string->unescaped);
4980 PM_COMPILE_NOT_POPPED(cast->receiver);
4981 PM_COMPILE_NOT_POPPED(((const pm_arguments_node_t *) cast->arguments)->arguments.nodes[1]);
4983 if (!popped) {
4984 PUSH_INSN(ret, location, swap);
4985 PUSH_INSN1(ret, location, topn, INT2FIX(1));
4988 PUSH_INSN2(ret, location, opt_aset_with, value, new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
4989 PUSH_INSN(ret, location, pop);
4990 return;
4992 break;
4996 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
4997 PUSH_INSN(ret, location, putnil);
5000 if (cast->receiver == NULL) {
5001 PUSH_INSN(ret, location, putself);
5003 else {
5004 PM_COMPILE_NOT_POPPED(cast->receiver);
5007 pm_compile_call(iseq, cast, ret, popped, scope_node, method_id, start);
5008 return;
5010 case PM_CALL_AND_WRITE_NODE: {
5011 // foo.bar &&= baz
5012 // ^^^^^^^^^^^^^^^
5013 const pm_call_and_write_node_t *cast = (const pm_call_and_write_node_t *) node;
5014 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);
5015 return;
5017 case PM_CALL_OR_WRITE_NODE: {
5018 // foo.bar ||= baz
5019 // ^^^^^^^^^^^^^^^
5020 const pm_call_or_write_node_t *cast = (const pm_call_or_write_node_t *) node;
5021 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);
5022 return;
5024 case PM_CALL_OPERATOR_WRITE_NODE: {
5025 // foo.bar += baz
5026 // ^^^^^^^^^^^^^^^
5028 // Call operator writes occur when you have a call node on the left-hand
5029 // side of a write operator that is not `=`. As an example,
5030 // `foo.bar *= 1`. This breaks down to caching the receiver on the
5031 // stack and then performing three method calls, one to read the value,
5032 // one to compute the result, and one to write the result back to the
5033 // receiver.
5034 const pm_call_operator_write_node_t *cast = (const pm_call_operator_write_node_t *) node;
5035 int flag = 0;
5037 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
5038 flag = VM_CALL_FCALL;
5041 PM_COMPILE_NOT_POPPED(cast->receiver);
5043 LABEL *safe_label = NULL;
5044 if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
5045 safe_label = NEW_LABEL(location.line);
5046 PUSH_INSN(ret, location, dup);
5047 PUSH_INSNL(ret, location, branchnil, safe_label);
5050 PUSH_INSN(ret, location, dup);
5052 ID id_read_name = pm_constant_id_lookup(scope_node, cast->read_name);
5053 PUSH_SEND_WITH_FLAG(ret, location, id_read_name, INT2FIX(0), INT2FIX(flag));
5055 PM_COMPILE_NOT_POPPED(cast->value);
5056 ID id_operator = pm_constant_id_lookup(scope_node, cast->operator);
5057 PUSH_SEND(ret, location, id_operator, INT2FIX(1));
5059 if (!popped) {
5060 PUSH_INSN(ret, location, swap);
5061 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5064 ID id_write_name = pm_constant_id_lookup(scope_node, cast->write_name);
5065 PUSH_SEND_WITH_FLAG(ret, location, id_write_name, INT2FIX(1), INT2FIX(flag));
5067 if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
5068 PUSH_INSN(ret, location, pop);
5069 if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
5071 return;
5073 case PM_CASE_NODE: {
5074 // case foo; when bar; end
5075 // ^^^^^^^^^^^^^^^^^^^^^^^
5076 const pm_case_node_t *cast = (const pm_case_node_t *) node;
5077 const pm_node_list_t *conditions = &cast->conditions;
5079 // This is the anchor that we will compile the conditions of the various
5080 // `when` nodes into. If a match is found, they will need to jump into
5081 // the body_seq anchor to the correct spot.
5082 DECL_ANCHOR(cond_seq);
5083 INIT_ANCHOR(cond_seq);
5085 // This is the anchor that we will compile the bodies of the various
5086 // `when` nodes into. We'll make sure that the clauses that are compiled
5087 // jump into the correct spots within this anchor.
5088 DECL_ANCHOR(body_seq);
5089 INIT_ANCHOR(body_seq);
5091 // This is the label where all of the when clauses will jump to if they
5092 // have matched and are done executing their bodies.
5093 LABEL *end_label = NEW_LABEL(location.line);
5095 // If we have a predicate on this case statement, then it's going to
5096 // compare all of the various when clauses to the predicate. If we
5097 // don't, then it's basically an if-elsif-else chain.
5098 if (cast->predicate == NULL) {
5099 // Establish branch coverage for the case node.
5100 VALUE branches = Qfalse;
5101 rb_code_location_t case_location = { 0 };
5102 int branch_id = 0;
5104 if (PM_BRANCH_COVERAGE_P(iseq)) {
5105 case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
5106 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
5109 // Loop through each clauses in the case node and compile each of
5110 // the conditions within them into cond_seq. If they match, they
5111 // should jump into their respective bodies in body_seq.
5112 for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
5113 const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
5114 const pm_node_list_t *conditions = &clause->conditions;
5116 int clause_lineno = pm_node_line_number(parser, (const pm_node_t *) clause);
5117 LABEL *label = NEW_LABEL(clause_lineno);
5118 PUSH_LABEL(body_seq, label);
5120 // Establish branch coverage for the when clause.
5121 if (PM_BRANCH_COVERAGE_P(iseq)) {
5122 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));
5123 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
5126 if (clause->statements != NULL) {
5127 pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
5129 else if (!popped) {
5130 PUSH_INSN(body_seq, location, putnil);
5133 PUSH_INSNL(body_seq, location, jump, end_label);
5135 // Compile each of the conditions for the when clause into the
5136 // cond_seq. Each one should have a unique condition and should
5137 // jump to the subsequent one if it doesn't match.
5138 for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
5139 const pm_node_t *condition = conditions->nodes[condition_index];
5141 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
5142 pm_line_column_t cond_location = PM_NODE_START_LINE_COLUMN(parser, condition);
5143 PUSH_INSN(cond_seq, cond_location, putnil);
5144 pm_compile_node(iseq, condition, cond_seq, false, scope_node);
5145 PUSH_INSN1(cond_seq, cond_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
5146 PUSH_INSNL(cond_seq, cond_location, branchif, label);
5148 else {
5149 LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
5150 pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label, false, scope_node);
5151 PUSH_LABEL(cond_seq, next_label);
5156 // Establish branch coverage for the else clause (implicit or
5157 // explicit).
5158 if (PM_BRANCH_COVERAGE_P(iseq)) {
5159 rb_code_location_t branch_location;
5161 if (cast->consequent == NULL) {
5162 branch_location = case_location;
5163 } else if (cast->consequent->statements == NULL) {
5164 branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->consequent);
5165 } else {
5166 branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->consequent->statements);
5169 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
5172 // Compile the consequent else clause if there is one.
5173 if (cast->consequent != NULL) {
5174 pm_compile_node(iseq, (const pm_node_t *) cast->consequent, cond_seq, popped, scope_node);
5176 else if (!popped) {
5177 PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
5180 // Finally, jump to the end label if none of the other conditions
5181 // have matched.
5182 PUSH_INSNL(cond_seq, location, jump, end_label);
5183 PUSH_SEQ(ret, cond_seq);
5185 else {
5186 // Establish branch coverage for the case node.
5187 VALUE branches = Qfalse;
5188 rb_code_location_t case_location = { 0 };
5189 int branch_id = 0;
5191 if (PM_BRANCH_COVERAGE_P(iseq)) {
5192 case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
5193 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
5196 // This is the label where everything will fall into if none of the
5197 // conditions matched.
5198 LABEL *else_label = NEW_LABEL(location.line);
5200 // It's possible for us to speed up the case node by using a
5201 // dispatch hash. This is a hash that maps the conditions of the
5202 // various when clauses to the labels of their bodies. If we can
5203 // compile the conditions into a hash key, then we can use a hash
5204 // lookup to jump directly to the correct when clause body.
5205 VALUE dispatch = Qundef;
5206 if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
5207 dispatch = rb_hash_new();
5208 RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
5211 // We're going to loop through each of the conditions in the case
5212 // node and compile each of their contents into both the cond_seq
5213 // and the body_seq. Each condition will use its own label to jump
5214 // from its conditions into its body.
5216 // Note that none of the code in the loop below should be adding
5217 // anything to ret, as we're going to be laying out the entire case
5218 // node instructions later.
5219 for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
5220 const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
5221 pm_line_column_t clause_location = PM_NODE_START_LINE_COLUMN(parser, (const pm_node_t *) clause);
5223 const pm_node_list_t *conditions = &clause->conditions;
5224 LABEL *label = NEW_LABEL(clause_location.line);
5226 // Compile each of the conditions for the when clause into the
5227 // cond_seq. Each one should have a unique comparison that then
5228 // jumps into the body if it matches.
5229 for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
5230 const pm_node_t *condition = conditions->nodes[condition_index];
5231 const pm_line_column_t condition_location = PM_NODE_START_LINE_COLUMN(parser, condition);
5233 // If we haven't already abandoned the optimization, then
5234 // we're going to try to compile the condition into the
5235 // dispatch hash.
5236 if (dispatch != Qundef) {
5237 dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
5240 if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
5241 PUSH_INSN(cond_seq, condition_location, dup);
5242 pm_compile_node(iseq, condition, cond_seq, false, scope_node);
5243 PUSH_INSN1(cond_seq, condition_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
5245 else {
5246 if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
5247 const pm_string_node_t *string = (const pm_string_node_t *) condition;
5248 VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->unescaped);
5249 PUSH_INSN1(cond_seq, condition_location, putobject, value);
5251 else {
5252 pm_compile_node(iseq, condition, cond_seq, false, scope_node);
5255 PUSH_INSN1(cond_seq, condition_location, topn, INT2FIX(1));
5256 PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
5259 PUSH_INSNL(cond_seq, condition_location, branchif, label);
5262 // Now, add the label to the body and compile the body of the
5263 // when clause. This involves popping the predicate, compiling
5264 // the statements to be executed, and then compiling a jump to
5265 // the end of the case node.
5266 PUSH_LABEL(body_seq, label);
5267 PUSH_INSN(body_seq, clause_location, pop);
5269 // Establish branch coverage for the when clause.
5270 if (PM_BRANCH_COVERAGE_P(iseq)) {
5271 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));
5272 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
5275 if (clause->statements != NULL) {
5276 pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
5278 else if (!popped) {
5279 PUSH_INSN(body_seq, clause_location, putnil);
5282 PUSH_INSNL(body_seq, clause_location, jump, end_label);
5285 // Now that we have compiled the conditions and the bodies of the
5286 // various when clauses, we can compile the predicate, lay out the
5287 // conditions, compile the fallback consequent if there is one, and
5288 // finally put in the bodies of the when clauses.
5289 PM_COMPILE_NOT_POPPED(cast->predicate);
5291 // If we have a dispatch hash, then we'll use it here to create the
5292 // optimization.
5293 if (dispatch != Qundef) {
5294 PUSH_INSN(ret, location, dup);
5295 PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
5296 LABEL_REF(else_label);
5299 PUSH_SEQ(ret, cond_seq);
5301 // Compile either the explicit else clause or an implicit else
5302 // clause.
5303 PUSH_LABEL(ret, else_label);
5305 if (cast->consequent != NULL) {
5306 pm_line_column_t else_location = PM_NODE_START_LINE_COLUMN(parser, cast->consequent->statements != NULL ? ((const pm_node_t *) cast->consequent->statements) : ((const pm_node_t *) cast->consequent));
5307 PUSH_INSN(ret, else_location, pop);
5309 // Establish branch coverage for the else clause.
5310 if (PM_BRANCH_COVERAGE_P(iseq)) {
5311 rb_code_location_t branch_location = pm_code_location(scope_node, cast->consequent->statements != NULL ? ((const pm_node_t *) cast->consequent->statements) : ((const pm_node_t *) cast->consequent));
5312 add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
5315 PM_COMPILE((const pm_node_t *) cast->consequent);
5316 PUSH_INSNL(ret, else_location, jump, end_label);
5318 else {
5319 PUSH_INSN(ret, location, pop);
5321 // Establish branch coverage for the implicit else clause.
5322 if (PM_BRANCH_COVERAGE_P(iseq)) {
5323 add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
5326 if (!popped) PUSH_INSN(ret, location, putnil);
5327 PUSH_INSNL(ret, location, jump, end_label);
5331 PUSH_SEQ(ret, body_seq);
5332 PUSH_LABEL(ret, end_label);
5334 return;
5336 case PM_CASE_MATCH_NODE: {
5337 // case foo; in bar; end
5338 // ^^^^^^^^^^^^^^^^^^^^^
5340 // If you use the `case` keyword to create a case match node, it will
5341 // match against all of the `in` clauses until it finds one that
5342 // matches. If it doesn't find one, it can optionally fall back to an
5343 // `else` clause. If none is present and a match wasn't found, it will
5344 // raise an appropriate error.
5345 const pm_case_match_node_t *cast = (const pm_case_match_node_t *) node;
5347 // This is the anchor that we will compile the bodies of the various
5348 // `in` nodes into. We'll make sure that the patterns that are compiled
5349 // jump into the correct spots within this anchor.
5350 DECL_ANCHOR(body_seq);
5351 INIT_ANCHOR(body_seq);
5353 // This is the anchor that we will compile the patterns of the various
5354 // `in` nodes into. If a match is found, they will need to jump into the
5355 // body_seq anchor to the correct spot.
5356 DECL_ANCHOR(cond_seq);
5357 INIT_ANCHOR(cond_seq);
5359 // This label is used to indicate the end of the entire node. It is
5360 // jumped to after the entire stack is cleaned up.
5361 LABEL *end_label = NEW_LABEL(location.line);
5363 // This label is used as the fallback for the case match. If no match is
5364 // found, then we jump to this label. This is either an `else` clause or
5365 // an error handler.
5366 LABEL *else_label = NEW_LABEL(location.line);
5368 // We're going to use this to uniquely identify each branch so that we
5369 // can track coverage information.
5370 rb_code_location_t case_location;
5371 VALUE branches = Qfalse;
5372 int branch_id = 0;
5374 if (PM_BRANCH_COVERAGE_P(iseq)) {
5375 case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
5376 branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
5379 // If there is only one pattern, then the behavior changes a bit. It
5380 // effectively gets treated as a match required node (this is how it is
5381 // represented in the other parser).
5382 bool in_single_pattern = cast->consequent == NULL && cast->conditions.size == 1;
5384 // First, we're going to push a bunch of stuff onto the stack that is
5385 // going to serve as our scratch space.
5386 if (in_single_pattern) {
5387 PUSH_INSN(ret, location, putnil); // key error key
5388 PUSH_INSN(ret, location, putnil); // key error matchee
5389 PUSH_INSN1(ret, location, putobject, Qfalse); // key error?
5390 PUSH_INSN(ret, location, putnil); // error string
5393 // Now we're going to compile the value to match against.
5394 PUSH_INSN(ret, location, putnil); // deconstruct cache
5395 PM_COMPILE_NOT_POPPED(cast->predicate);
5397 // Next, we'll loop through every in clause and compile its body into
5398 // the body_seq anchor and its pattern into the cond_seq anchor. We'll
5399 // make sure the pattern knows how to jump correctly into the body if it
5400 // finds a match.
5401 for (size_t index = 0; index < cast->conditions.size; index++) {
5402 const pm_node_t *condition = cast->conditions.nodes[index];
5403 RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
5405 const pm_in_node_t *in_node = (const pm_in_node_t *) condition;
5406 const pm_line_column_t in_location = PM_NODE_START_LINE_COLUMN(parser, in_node);
5407 const pm_line_column_t pattern_location = PM_NODE_START_LINE_COLUMN(parser, in_node->pattern);
5409 if (branch_id) {
5410 PUSH_INSN(body_seq, in_location, putnil);
5413 LABEL *body_label = NEW_LABEL(in_location.line);
5414 PUSH_LABEL(body_seq, body_label);
5415 PUSH_INSN1(body_seq, in_location, adjuststack, INT2FIX(in_single_pattern ? 6 : 2));
5417 // Establish branch coverage for the in clause.
5418 if (PM_BRANCH_COVERAGE_P(iseq)) {
5419 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));
5420 add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "in", branches);
5423 if (in_node->statements != NULL) {
5424 PM_COMPILE_INTO_ANCHOR(body_seq, (const pm_node_t *) in_node->statements);
5426 else if (!popped) {
5427 PUSH_INSN(body_seq, in_location, putnil);
5430 PUSH_INSNL(body_seq, in_location, jump, end_label);
5431 LABEL *next_pattern_label = NEW_LABEL(pattern_location.line);
5433 PUSH_INSN(cond_seq, pattern_location, dup);
5434 pm_compile_pattern(iseq, scope_node, in_node->pattern, cond_seq, body_label, next_pattern_label, in_single_pattern, false, true, 2);
5435 PUSH_LABEL(cond_seq, next_pattern_label);
5436 LABEL_UNREMOVABLE(next_pattern_label);
5439 if (cast->consequent != NULL) {
5440 // If we have an `else` clause, then this becomes our fallback (and
5441 // there is no need to compile in code to potentially raise an
5442 // error).
5443 const pm_else_node_t *else_node = (const pm_else_node_t *) cast->consequent;
5445 PUSH_LABEL(cond_seq, else_label);
5446 PUSH_INSN(cond_seq, location, pop);
5447 PUSH_INSN(cond_seq, location, pop);
5449 // Establish branch coverage for the else clause.
5450 if (PM_BRANCH_COVERAGE_P(iseq)) {
5451 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));
5452 add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
5455 PM_COMPILE_INTO_ANCHOR(cond_seq, (const pm_node_t *) else_node);
5456 PUSH_INSNL(cond_seq, location, jump, end_label);
5457 PUSH_INSN(cond_seq, location, putnil);
5458 if (popped) PUSH_INSN(cond_seq, location, putnil);
5460 else {
5461 // Otherwise, if we do not have an `else` clause, we will compile in
5462 // the code to handle raising an appropriate error.
5463 PUSH_LABEL(cond_seq, else_label);
5465 // Establish branch coverage for the implicit else clause.
5466 add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
5468 if (in_single_pattern) {
5469 pm_compile_pattern_error_handler(iseq, scope_node, node, cond_seq, end_label, popped);
5471 else {
5472 PUSH_INSN1(cond_seq, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
5473 PUSH_INSN1(cond_seq, location, putobject, rb_eNoMatchingPatternError);
5474 PUSH_INSN1(cond_seq, location, topn, INT2FIX(2));
5475 PUSH_SEND(cond_seq, location, id_core_raise, INT2FIX(2));
5477 PUSH_INSN1(cond_seq, location, adjuststack, INT2FIX(3));
5478 if (!popped) PUSH_INSN(cond_seq, location, putnil);
5479 PUSH_INSNL(cond_seq, location, jump, end_label);
5480 PUSH_INSN1(cond_seq, location, dupn, INT2FIX(1));
5481 if (popped) PUSH_INSN(cond_seq, location, putnil);
5485 // At the end of all of this compilation, we will add the code for the
5486 // conditions first, then the various bodies, then mark the end of the
5487 // entire sequence with the end label.
5488 PUSH_SEQ(ret, cond_seq);
5489 PUSH_SEQ(ret, body_seq);
5490 PUSH_LABEL(ret, end_label);
5492 return;
5494 case PM_CLASS_NODE: {
5495 // class Foo; end
5496 // ^^^^^^^^^^^^^^
5497 const pm_class_node_t *cast = (const pm_class_node_t *) node;
5499 ID class_id = pm_constant_id_lookup(scope_node, cast->name);
5500 VALUE class_name = rb_str_freeze(rb_sprintf("<class:%"PRIsVALUE">", rb_id2str(class_id)));
5502 pm_scope_node_t next_scope_node;
5503 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
5505 const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, location.line);
5506 pm_scope_node_destroy(&next_scope_node);
5508 // TODO: Once we merge constant path nodes correctly, fix this flag
5509 const int flags = VM_DEFINECLASS_TYPE_CLASS |
5510 (cast->superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
5511 pm_compile_class_path(iseq, cast->constant_path, &location, ret, false, scope_node);
5513 if (cast->superclass) {
5514 PM_COMPILE_NOT_POPPED(cast->superclass);
5516 else {
5517 PUSH_INSN(ret, location, putnil);
5520 PUSH_INSN3(ret, location, defineclass, ID2SYM(class_id), class_iseq, INT2FIX(flags));
5521 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)class_iseq);
5523 if (popped) PUSH_INSN(ret, location, pop);
5524 return;
5526 case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
5527 // @@foo &&= bar
5528 // ^^^^^^^^^^^^^
5529 const pm_class_variable_and_write_node_t *cast = (const pm_class_variable_and_write_node_t *) node;
5530 LABEL *end_label = NEW_LABEL(location.line);
5532 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
5533 VALUE name = ID2SYM(name_id);
5535 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
5536 if (!popped) PUSH_INSN(ret, location, dup);
5538 PUSH_INSNL(ret, location, branchunless, end_label);
5539 if (!popped) PUSH_INSN(ret, location, pop);
5541 PM_COMPILE_NOT_POPPED(cast->value);
5542 if (!popped) PUSH_INSN(ret, location, dup);
5544 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
5545 PUSH_LABEL(ret, end_label);
5547 return;
5549 case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
5550 // @@foo += bar
5551 // ^^^^^^^^^^^^
5552 const pm_class_variable_operator_write_node_t *cast = (const pm_class_variable_operator_write_node_t *) node;
5554 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
5555 VALUE name = ID2SYM(name_id);
5557 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
5558 PM_COMPILE_NOT_POPPED(cast->value);
5560 ID method_id = pm_constant_id_lookup(scope_node, cast->operator);
5561 int flags = VM_CALL_ARGS_SIMPLE;
5562 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
5564 if (!popped) PUSH_INSN(ret, location, dup);
5565 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
5567 return;
5569 case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
5570 // @@foo ||= bar
5571 // ^^^^^^^^^^^^^
5572 const pm_class_variable_or_write_node_t *cast = (const pm_class_variable_or_write_node_t *) node;
5573 LABEL *end_label = NEW_LABEL(location.line);
5574 LABEL *start_label = NEW_LABEL(location.line);
5576 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
5577 VALUE name = ID2SYM(name_id);
5579 PUSH_INSN(ret, location, putnil);
5580 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), name, Qtrue);
5581 PUSH_INSNL(ret, location, branchunless, start_label);
5583 PUSH_INSN2(ret, location, getclassvariable, name, get_cvar_ic_value(iseq, name_id));
5584 if (!popped) PUSH_INSN(ret, location, dup);
5586 PUSH_INSNL(ret, location, branchif, end_label);
5587 if (!popped) PUSH_INSN(ret, location, pop);
5589 PUSH_LABEL(ret, start_label);
5590 PM_COMPILE_NOT_POPPED(cast->value);
5591 if (!popped) PUSH_INSN(ret, location, dup);
5593 PUSH_INSN2(ret, location, setclassvariable, name, get_cvar_ic_value(iseq, name_id));
5594 PUSH_LABEL(ret, end_label);
5596 return;
5598 case PM_CLASS_VARIABLE_READ_NODE: {
5599 // @@foo
5600 // ^^^^^
5601 if (!popped) {
5602 const pm_class_variable_read_node_t *cast = (const pm_class_variable_read_node_t *) node;
5603 ID name = pm_constant_id_lookup(scope_node, cast->name);
5604 PUSH_INSN2(ret, location, getclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
5606 return;
5608 case PM_CLASS_VARIABLE_WRITE_NODE: {
5609 // @@foo = 1
5610 // ^^^^^^^^^
5611 const pm_class_variable_write_node_t *cast = (const pm_class_variable_write_node_t *) node;
5612 PM_COMPILE_NOT_POPPED(cast->value);
5613 if (!popped) PUSH_INSN(ret, location, dup);
5615 ID name = pm_constant_id_lookup(scope_node, cast->name);
5616 PUSH_INSN2(ret, location, setclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
5618 return;
5620 case PM_CONSTANT_PATH_NODE: {
5621 // Foo::Bar
5622 // ^^^^^^^^
5623 VALUE parts;
5625 if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) != Qnil)) {
5626 ISEQ_BODY(iseq)->ic_size++;
5627 PUSH_INSN1(ret, location, opt_getconstant_path, parts);
5629 else {
5630 DECL_ANCHOR(prefix);
5631 INIT_ANCHOR(prefix);
5633 DECL_ANCHOR(body);
5634 INIT_ANCHOR(body);
5636 pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
5637 if (LIST_INSN_SIZE_ZERO(prefix)) {
5638 PUSH_INSN(ret, location, putnil);
5640 else {
5641 PUSH_SEQ(ret, prefix);
5644 PUSH_SEQ(ret, body);
5647 if (popped) PUSH_INSN(ret, location, pop);
5648 return;
5650 case PM_CONSTANT_PATH_AND_WRITE_NODE: {
5651 // Foo::Bar &&= baz
5652 // ^^^^^^^^^^^^^^^^
5653 const pm_constant_path_and_write_node_t *cast = (const pm_constant_path_and_write_node_t *) node;
5654 const pm_constant_path_node_t *target = cast->target;
5656 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5657 LABEL *lfin = NEW_LABEL(location.line);
5659 if (target->parent) {
5660 PM_COMPILE_NOT_POPPED(target->parent);
5662 else {
5663 PUSH_INSN1(ret, location, putobject, rb_cObject);
5666 PUSH_INSN(ret, location, dup);
5667 PUSH_INSN1(ret, location, putobject, Qtrue);
5668 PUSH_INSN1(ret, location, getconstant, name);
5670 if (!popped) PUSH_INSN(ret, location, dup);
5671 PUSH_INSNL(ret, location, branchunless, lfin);
5673 if (!popped) PUSH_INSN(ret, location, pop);
5674 PM_COMPILE_NOT_POPPED(cast->value);
5676 if (popped) {
5677 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5679 else {
5680 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
5681 PUSH_INSN(ret, location, swap);
5684 PUSH_INSN1(ret, location, setconstant, name);
5685 PUSH_LABEL(ret, lfin);
5687 if (!popped) PUSH_INSN(ret, location, swap);
5688 PUSH_INSN(ret, location, pop);
5690 return;
5692 case PM_CONSTANT_PATH_OR_WRITE_NODE: {
5693 // Foo::Bar ||= baz
5694 // ^^^^^^^^^^^^^^^^
5695 const pm_constant_path_or_write_node_t *cast = (const pm_constant_path_or_write_node_t *) node;
5696 const pm_constant_path_node_t *target = cast->target;
5698 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5699 LABEL *lassign = NEW_LABEL(location.line);
5700 LABEL *lfin = NEW_LABEL(location.line);
5702 if (target->parent) {
5703 PM_COMPILE_NOT_POPPED(target->parent);
5705 else {
5706 PUSH_INSN1(ret, location, putobject, rb_cObject);
5709 PUSH_INSN(ret, location, dup);
5710 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), name, Qtrue);
5711 PUSH_INSNL(ret, location, branchunless, lassign);
5713 PUSH_INSN(ret, location, dup);
5714 PUSH_INSN1(ret, location, putobject, Qtrue);
5715 PUSH_INSN1(ret, location, getconstant, name);
5717 if (!popped) PUSH_INSN(ret, location, dup);
5718 PUSH_INSNL(ret, location, branchif, lfin);
5720 if (!popped) PUSH_INSN(ret, location, pop);
5721 PUSH_LABEL(ret, lassign);
5722 PM_COMPILE_NOT_POPPED(cast->value);
5724 if (popped) {
5725 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5727 else {
5728 PUSH_INSN1(ret, location, dupn, INT2FIX(2));
5729 PUSH_INSN(ret, location, swap);
5732 PUSH_INSN1(ret, location, setconstant, name);
5733 PUSH_LABEL(ret, lfin);
5735 if (!popped) PUSH_INSN(ret, location, swap);
5736 PUSH_INSN(ret, location, pop);
5738 return;
5740 case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
5741 // Foo::Bar += baz
5742 // ^^^^^^^^^^^^^^^
5743 const pm_constant_path_operator_write_node_t *cast = (const pm_constant_path_operator_write_node_t *) node;
5744 const pm_constant_path_node_t *target = cast->target;
5745 ID method_id = pm_constant_id_lookup(scope_node, cast->operator);
5746 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5748 if (target->parent) {
5749 PM_COMPILE_NOT_POPPED(target->parent);
5751 else {
5752 PUSH_INSN1(ret, location, putobject, rb_cObject);
5755 PUSH_INSN(ret, location, dup);
5756 PUSH_INSN1(ret, location, putobject, Qtrue);
5757 PUSH_INSN1(ret, location, getconstant, name);
5759 PM_COMPILE_NOT_POPPED(cast->value);
5760 PUSH_CALL(ret, location, method_id, INT2FIX(1));
5761 PUSH_INSN(ret, location, swap);
5763 if (!popped) {
5764 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5765 PUSH_INSN(ret, location, swap);
5768 PUSH_INSN1(ret, location, setconstant, name);
5769 return;
5771 case PM_CONSTANT_PATH_WRITE_NODE: {
5772 // Foo::Bar = 1
5773 // ^^^^^^^^^^^^
5774 const pm_constant_path_write_node_t *cast = (const pm_constant_path_write_node_t *) node;
5775 const pm_constant_path_node_t *target = cast->target;
5776 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, target->name));
5778 if (target->parent) {
5779 PM_COMPILE_NOT_POPPED((const pm_node_t *) target->parent);
5781 else {
5782 PUSH_INSN1(ret, location, putobject, rb_cObject);
5785 PM_COMPILE_NOT_POPPED(cast->value);
5787 if (!popped) {
5788 PUSH_INSN(ret, location, swap);
5789 PUSH_INSN1(ret, location, topn, INT2FIX(1));
5792 PUSH_INSN(ret, location, swap);
5793 PUSH_INSN1(ret, location, setconstant, name);
5795 return;
5797 case PM_CONSTANT_READ_NODE: {
5798 // Foo
5799 // ^^^
5800 const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
5801 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5803 pm_compile_constant_read(iseq, name, &cast->base.location, ret, scope_node);
5804 if (popped) PUSH_INSN(ret, location, pop);
5806 return;
5808 case PM_CONSTANT_AND_WRITE_NODE: {
5809 // Foo &&= bar
5810 // ^^^^^^^^^^^
5811 const pm_constant_and_write_node_t *cast = (const pm_constant_and_write_node_t *) node;
5812 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5813 LABEL *end_label = NEW_LABEL(location.line);
5815 pm_compile_constant_read(iseq, name, &cast->name_loc, ret, scope_node);
5816 if (!popped) PUSH_INSN(ret, location, dup);
5818 PUSH_INSNL(ret, location, branchunless, end_label);
5819 if (!popped) PUSH_INSN(ret, location, pop);
5821 PM_COMPILE_NOT_POPPED(cast->value);
5822 if (!popped) PUSH_INSN(ret, location, dup);
5824 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5825 PUSH_INSN1(ret, location, setconstant, name);
5826 PUSH_LABEL(ret, end_label);
5828 return;
5830 case PM_CONSTANT_OR_WRITE_NODE: {
5831 // Foo ||= bar
5832 // ^^^^^^^^^^^
5833 const pm_constant_or_write_node_t *cast = (const pm_constant_or_write_node_t *) node;
5834 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5835 LABEL *set_label = NEW_LABEL(location.line);
5836 LABEL *end_label = NEW_LABEL(location.line);
5838 PUSH_INSN(ret, location, putnil);
5839 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), name, Qtrue);
5840 PUSH_INSNL(ret, location, branchunless, set_label);
5842 pm_compile_constant_read(iseq, name, &cast->name_loc, ret, scope_node);
5843 if (!popped) PUSH_INSN(ret, location, dup);
5845 PUSH_INSNL(ret, location, branchif, end_label);
5846 if (!popped) PUSH_INSN(ret, location, pop);
5848 PUSH_LABEL(ret, set_label);
5849 PM_COMPILE_NOT_POPPED(cast->value);
5850 if (!popped) PUSH_INSN(ret, location, dup);
5852 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5853 PUSH_INSN1(ret, location, setconstant, name);
5854 PUSH_LABEL(ret, end_label);
5856 return;
5858 case PM_CONSTANT_OPERATOR_WRITE_NODE: {
5859 // Foo += bar
5860 // ^^^^^^^^^^
5861 const pm_constant_operator_write_node_t *cast = (const pm_constant_operator_write_node_t *) node;
5862 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5863 ID method_id = pm_constant_id_lookup(scope_node, cast->operator);
5865 pm_compile_constant_read(iseq, name, &cast->name_loc, ret, scope_node);
5866 PM_COMPILE_NOT_POPPED(cast->value);
5868 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
5869 if (!popped) PUSH_INSN(ret, location, dup);
5871 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5872 PUSH_INSN1(ret, location, setconstant, name);
5874 return;
5876 case PM_CONSTANT_WRITE_NODE: {
5877 // Foo = 1
5878 // ^^^^^^^
5879 const pm_constant_write_node_t *cast = (const pm_constant_write_node_t *) node;
5880 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
5882 PM_COMPILE_NOT_POPPED(cast->value);
5883 if (!popped) PUSH_INSN(ret, location, dup);
5885 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
5886 PUSH_INSN1(ret, location, setconstant, name);
5888 return;
5890 case PM_DEF_NODE: {
5891 // def foo; end
5892 // ^^^^^^^^^^^^
5894 // def self.foo; end
5895 // ^^^^^^^^^^^^^^^^^
5896 const pm_def_node_t *cast = (const pm_def_node_t *) node;
5897 ID method_name = pm_constant_id_lookup(scope_node, cast->name);
5899 pm_scope_node_t next_scope_node;
5900 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
5902 rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, location.line);
5903 pm_scope_node_destroy(&next_scope_node);
5905 if (cast->receiver) {
5906 PM_COMPILE_NOT_POPPED(cast->receiver);
5907 PUSH_INSN2(ret, location, definesmethod, ID2SYM(method_name), method_iseq);
5909 else {
5910 PUSH_INSN2(ret, location, definemethod, ID2SYM(method_name), method_iseq);
5912 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) method_iseq);
5914 if (!popped) {
5915 PUSH_INSN1(ret, location, putobject, ID2SYM(method_name));
5918 return;
5920 case PM_DEFINED_NODE: {
5921 // defined?(a)
5922 // ^^^^^^^^^^^
5923 const pm_defined_node_t *cast = (const pm_defined_node_t *) node;
5924 pm_compile_defined_expr(iseq, cast->value, &location, ret, popped, scope_node, false);
5925 return;
5927 case PM_EMBEDDED_STATEMENTS_NODE: {
5928 // "foo #{bar}"
5929 // ^^^^^^
5930 const pm_embedded_statements_node_t *cast = (const pm_embedded_statements_node_t *) node;
5932 if (cast->statements != NULL) {
5933 PM_COMPILE((const pm_node_t *) (cast->statements));
5935 else {
5936 PUSH_INSN(ret, location, putnil);
5939 if (popped) PUSH_INSN(ret, location, pop);
5940 return;
5942 case PM_EMBEDDED_VARIABLE_NODE: {
5943 // "foo #@bar"
5944 // ^^^^^
5945 const pm_embedded_variable_node_t *cast = (const pm_embedded_variable_node_t *) node;
5946 PM_COMPILE(cast->variable);
5947 return;
5949 case PM_FALSE_NODE: {
5950 // false
5951 // ^^^^^
5952 if (!popped) {
5953 PUSH_INSN1(ret, location, putobject, Qfalse);
5955 return;
5957 case PM_ENSURE_NODE: {
5958 const pm_ensure_node_t *cast = (const pm_ensure_node_t *) node;
5960 if (cast->statements != NULL) {
5961 LABEL *start = NEW_LABEL(location.line);
5962 LABEL *end = NEW_LABEL(location.line);
5963 PUSH_LABEL(ret, start);
5965 LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
5966 ISEQ_COMPILE_DATA(iseq)->end_label = end;
5968 PM_COMPILE((const pm_node_t *) cast->statements);
5969 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
5970 PUSH_LABEL(ret, end);
5973 return;
5975 case PM_ELSE_NODE: {
5976 // if foo then bar else baz end
5977 // ^^^^^^^^^^^^
5978 const pm_else_node_t *cast = (const pm_else_node_t *) node;
5980 if (cast->statements != NULL) {
5981 PM_COMPILE((const pm_node_t *) cast->statements);
5983 else if (!popped) {
5984 PUSH_SYNTHETIC_PUTNIL(ret, iseq);
5987 return;
5989 case PM_FLIP_FLOP_NODE: {
5990 // if foo .. bar; end
5991 // ^^^^^^^^^^
5992 const pm_flip_flop_node_t *cast = (const pm_flip_flop_node_t *) node;
5994 LABEL *final_label = NEW_LABEL(location.line);
5995 LABEL *then_label = NEW_LABEL(location.line);
5996 LABEL *else_label = NEW_LABEL(location.line);
5998 pm_compile_flip_flop(cast, else_label, then_label, iseq, location.line, ret, popped, scope_node);
6000 PUSH_LABEL(ret, then_label);
6001 PUSH_INSN1(ret, location, putobject, Qtrue);
6002 PUSH_INSNL(ret, location, jump, final_label);
6003 PUSH_LABEL(ret, else_label);
6004 PUSH_INSN1(ret, location, putobject, Qfalse);
6005 PUSH_LABEL(ret, final_label);
6007 return;
6009 case PM_FLOAT_NODE: {
6010 // 1.0
6011 // ^^^
6012 if (!popped) {
6013 PUSH_INSN1(ret, location, putobject, parse_float((const pm_float_node_t *) node));
6015 return;
6017 case PM_FOR_NODE: {
6018 // for foo in bar do end
6019 // ^^^^^^^^^^^^^^^^^^^^^
6020 const pm_for_node_t *cast = (const pm_for_node_t *) node;
6022 LABEL *retry_label = NEW_LABEL(location.line);
6023 LABEL *retry_end_l = NEW_LABEL(location.line);
6025 // First, compile the collection that we're going to be iterating over.
6026 PUSH_LABEL(ret, retry_label);
6027 PM_COMPILE_NOT_POPPED(cast->collection);
6029 // Next, create the new scope that is going to contain the block that
6030 // will be passed to the each method.
6031 pm_scope_node_t next_scope_node;
6032 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
6034 const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.line);
6035 pm_scope_node_destroy(&next_scope_node);
6037 const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
6038 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
6040 // Now, create the method call to each that will be used to iterate over
6041 // the collection, and pass the newly created iseq as the block.
6042 PUSH_SEND_WITH_BLOCK(ret, location, idEach, INT2FIX(0), child_iseq);
6043 pm_compile_retry_end_label(iseq, ret, retry_end_l);
6045 if (popped) PUSH_INSN(ret, location, pop);
6046 ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
6047 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
6048 return;
6050 case PM_FORWARDING_ARGUMENTS_NODE: {
6051 rb_bug("Cannot compile a ForwardingArgumentsNode directly\n");
6052 return;
6054 case PM_FORWARDING_SUPER_NODE: {
6055 // super
6056 // ^^^^^
6058 // super {}
6059 // ^^^^^^^^
6060 const pm_forwarding_super_node_t *cast = (const pm_forwarding_super_node_t *) node;
6061 const rb_iseq_t *block = NULL;
6063 const rb_iseq_t *previous_block = NULL;
6064 LABEL *retry_label = NULL;
6065 LABEL *retry_end_l = NULL;
6067 if (cast->block != NULL) {
6068 previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
6069 ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
6071 retry_label = NEW_LABEL(location.line);
6072 retry_end_l = NEW_LABEL(location.line);
6074 PUSH_LABEL(ret, retry_label);
6077 PUSH_INSN(ret, location, putself);
6078 int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
6080 if (cast->block != NULL) {
6081 pm_scope_node_t next_scope_node;
6082 pm_scope_node_init((const pm_node_t *) cast->block, &next_scope_node, scope_node);
6084 ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.line);
6085 pm_scope_node_destroy(&next_scope_node);
6086 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
6089 DECL_ANCHOR(args);
6090 INIT_ANCHOR(args);
6092 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
6093 const rb_iseq_t *local_iseq = body->local_iseq;
6094 const struct rb_iseq_constant_body *const local_body = ISEQ_BODY(local_iseq);
6096 int argc = 0;
6097 int depth = get_lvar_level(iseq);
6099 if (local_body->param.flags.has_lead) {
6100 /* required arguments */
6101 for (int i = 0; i < local_body->param.lead_num; i++) {
6102 int idx = local_body->local_table_size - i;
6103 PUSH_GETLOCAL(args, location, idx, depth);
6105 argc += local_body->param.lead_num;
6108 if (local_body->param.flags.has_opt) {
6109 /* optional arguments */
6110 for (int j = 0; j < local_body->param.opt_num; j++) {
6111 int idx = local_body->local_table_size - (argc + j);
6112 PUSH_GETLOCAL(args, location, idx, depth);
6114 argc += local_body->param.opt_num;
6117 if (local_body->param.flags.has_rest) {
6118 /* rest argument */
6119 int idx = local_body->local_table_size - local_body->param.rest_start;
6120 PUSH_GETLOCAL(args, location, idx, depth);
6121 PUSH_INSN1(args, location, splatarray, Qfalse);
6123 argc = local_body->param.rest_start + 1;
6124 flag |= VM_CALL_ARGS_SPLAT;
6127 if (local_body->param.flags.has_post) {
6128 /* post arguments */
6129 int post_len = local_body->param.post_num;
6130 int post_start = local_body->param.post_start;
6132 int j = 0;
6133 for (; j < post_len; j++) {
6134 int idx = local_body->local_table_size - (post_start + j);
6135 PUSH_GETLOCAL(args, location, idx, depth);
6138 if (local_body->param.flags.has_rest) {
6139 // argc remains unchanged from rest branch
6140 PUSH_INSN1(args, location, newarray, INT2FIX(j));
6141 PUSH_INSN(args, location, concatarray);
6143 else {
6144 argc = post_len + post_start;
6148 const struct rb_iseq_param_keyword *const local_keyword = local_body->param.keyword;
6149 if (local_body->param.flags.has_kw) {
6150 int local_size = local_body->local_table_size;
6151 argc++;
6153 PUSH_INSN1(args, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6155 if (local_body->param.flags.has_kwrest) {
6156 int idx = local_body->local_table_size - local_keyword->rest_start;
6157 PUSH_GETLOCAL(args, location, idx, depth);
6158 RUBY_ASSERT(local_keyword->num > 0);
6159 PUSH_SEND(args, location, rb_intern("dup"), INT2FIX(0));
6161 else {
6162 PUSH_INSN1(args, location, newhash, INT2FIX(0));
6164 int i = 0;
6165 for (; i < local_keyword->num; ++i) {
6166 ID id = local_keyword->table[i];
6167 int idx = local_size - get_local_var_idx(local_iseq, id);
6168 PUSH_INSN1(args, location, putobject, ID2SYM(id));
6169 PUSH_GETLOCAL(args, location, idx, depth);
6172 PUSH_SEND(args, location, id_core_hash_merge_ptr, INT2FIX(i * 2 + 1));
6173 flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
6175 else if (local_body->param.flags.has_kwrest) {
6176 int idx = local_body->local_table_size - local_keyword->rest_start;
6177 PUSH_GETLOCAL(args, location, idx, depth);
6178 argc++;
6179 flag |= VM_CALL_KW_SPLAT;
6182 PUSH_SEQ(ret, args);
6183 PUSH_INSN2(ret, location, invokesuper, new_callinfo(iseq, 0, argc, flag, NULL, block != NULL), block);
6185 if (cast->block != NULL) {
6186 pm_compile_retry_end_label(iseq, ret, retry_end_l);
6187 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
6188 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
6191 if (popped) PUSH_INSN(ret, location, pop);
6192 return;
6194 case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
6195 // $foo &&= bar
6196 // ^^^^^^^^^^^^
6197 const pm_global_variable_and_write_node_t *cast = (const pm_global_variable_and_write_node_t *) node;
6198 LABEL *end_label = NEW_LABEL(location.line);
6200 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
6201 PUSH_INSN1(ret, location, getglobal, name);
6202 if (!popped) PUSH_INSN(ret, location, dup);
6204 PUSH_INSNL(ret, location, branchunless, end_label);
6205 if (!popped) PUSH_INSN(ret, location, pop);
6207 PM_COMPILE_NOT_POPPED(cast->value);
6208 if (!popped) PUSH_INSN(ret, location, dup);
6210 PUSH_INSN1(ret, location, setglobal, name);
6211 PUSH_LABEL(ret, end_label);
6213 return;
6215 case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
6216 // $foo += bar
6217 // ^^^^^^^^^^^
6218 const pm_global_variable_operator_write_node_t *cast = (const pm_global_variable_operator_write_node_t *) node;
6220 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
6221 PUSH_INSN1(ret, location, getglobal, name);
6222 PM_COMPILE_NOT_POPPED(cast->value);
6224 ID method_id = pm_constant_id_lookup(scope_node, cast->operator);
6225 int flags = VM_CALL_ARGS_SIMPLE;
6226 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
6228 if (!popped) PUSH_INSN(ret, location, dup);
6229 PUSH_INSN1(ret, location, setglobal, name);
6231 return;
6233 case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
6234 // $foo ||= bar
6235 // ^^^^^^^^^^^^
6236 const pm_global_variable_or_write_node_t *cast = (const pm_global_variable_or_write_node_t *) node;
6237 LABEL *set_label = NEW_LABEL(location.line);
6238 LABEL *end_label = NEW_LABEL(location.line);
6240 PUSH_INSN(ret, location, putnil);
6241 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
6243 PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), name, Qtrue);
6244 PUSH_INSNL(ret, location, branchunless, set_label);
6246 PUSH_INSN1(ret, location, getglobal, name);
6247 if (!popped) PUSH_INSN(ret, location, dup);
6249 PUSH_INSNL(ret, location, branchif, end_label);
6250 if (!popped) PUSH_INSN(ret, location, pop);
6252 PUSH_LABEL(ret, set_label);
6253 PM_COMPILE_NOT_POPPED(cast->value);
6254 if (!popped) PUSH_INSN(ret, location, dup);
6256 PUSH_INSN1(ret, location, setglobal, name);
6257 PUSH_LABEL(ret, end_label);
6259 return;
6261 case PM_GLOBAL_VARIABLE_READ_NODE: {
6262 // $foo
6263 // ^^^^
6264 const pm_global_variable_read_node_t *cast = (const pm_global_variable_read_node_t *) node;
6265 VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
6267 PUSH_INSN1(ret, location, getglobal, name);
6268 if (popped) PUSH_INSN(ret, location, pop);
6270 return;
6272 case PM_GLOBAL_VARIABLE_WRITE_NODE: {
6273 // $foo = 1
6274 // ^^^^^^^^
6275 const pm_global_variable_write_node_t *cast = (const pm_global_variable_write_node_t *) node;
6276 PM_COMPILE_NOT_POPPED(cast->value);
6277 if (!popped) PUSH_INSN(ret, location, dup);
6279 ID name = pm_constant_id_lookup(scope_node, cast->name);
6280 PUSH_INSN1(ret, location, setglobal, ID2SYM(name));
6282 return;
6284 case PM_HASH_NODE: {
6285 // {}
6286 // ^^
6288 // If every node in the hash is static, then we can compile the entire
6289 // hash now instead of later.
6290 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
6291 // We're only going to compile this node if it's not popped. If it
6292 // is popped, then we know we don't need to do anything since it's
6293 // statically known.
6294 if (!popped) {
6295 VALUE value = pm_static_literal_value(iseq, node, scope_node);
6296 PUSH_INSN1(ret, location, duphash, value);
6297 RB_OBJ_WRITTEN(iseq, Qundef, value);
6300 else {
6301 // Here since we know there are possible side-effects inside the
6302 // hash contents, we're going to build it entirely at runtime. We'll
6303 // do this by pushing all of the key-value pairs onto the stack and
6304 // then combining them with newhash.
6306 // If this hash is popped, then this serves only to ensure we enact
6307 // all side-effects (like method calls) that are contained within
6308 // the hash contents.
6309 const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
6310 const pm_node_list_t *elements = &cast->elements;
6312 if (popped) {
6313 // If this hash is popped, then we can iterate through each
6314 // element and compile it. The result of each compilation will
6315 // only include the side effects of the element itself.
6316 for (size_t index = 0; index < elements->size; index++) {
6317 PM_COMPILE_POPPED(elements->nodes[index]);
6320 else {
6321 pm_compile_hash_elements(iseq, node, elements, ret, scope_node);
6325 return;
6327 case PM_IF_NODE: {
6328 // if foo then bar end
6329 // ^^^^^^^^^^^^^^^^^^^
6331 // bar if foo
6332 // ^^^^^^^^^^
6334 // foo ? bar : baz
6335 // ^^^^^^^^^^^^^^^
6336 const pm_if_node_t *cast = (const pm_if_node_t *) node;
6337 pm_compile_conditional(iseq, &location, PM_IF_NODE, (const pm_node_t *) cast, cast->statements, cast->consequent, cast->predicate, ret, popped, scope_node);
6338 return;
6340 case PM_IMAGINARY_NODE: {
6341 // 1i
6342 // ^^
6343 if (!popped) {
6344 PUSH_INSN1(ret, location, putobject, parse_imaginary((const pm_imaginary_node_t *) node));
6346 return;
6348 case PM_IMPLICIT_NODE: {
6349 // Implicit nodes mark places in the syntax tree where explicit syntax
6350 // was omitted, but implied. For example,
6352 // { foo: }
6354 // In this case a method call/local variable read is implied by virtue
6355 // of the missing value. To compile these nodes, we simply compile the
6356 // value that is implied, which is helpfully supplied by the parser.
6357 const pm_implicit_node_t *cast = (const pm_implicit_node_t *) node;
6358 PM_COMPILE(cast->value);
6359 return;
6361 case PM_IN_NODE: {
6362 // In nodes are handled by the case match node directly, so we should
6363 // never end up hitting them through this path.
6364 rb_bug("Should not ever enter an in node directly");
6365 return;
6367 case PM_INDEX_OPERATOR_WRITE_NODE: {
6368 // foo[bar] += baz
6369 // ^^^^^^^^^^^^^^^
6370 const pm_index_operator_write_node_t *cast = (const pm_index_operator_write_node_t *) node;
6371 pm_compile_index_operator_write_node(iseq, cast, &location, ret, popped, scope_node);
6372 return;
6374 case PM_INDEX_AND_WRITE_NODE: {
6375 // foo[bar] &&= baz
6376 // ^^^^^^^^^^^^^^^^
6377 const pm_index_and_write_node_t *cast = (const pm_index_and_write_node_t *) node;
6378 pm_compile_index_control_flow_write_node(iseq, node, cast->receiver, cast->arguments, cast->block, cast->value, &location, ret, popped, scope_node);
6379 return;
6381 case PM_INDEX_OR_WRITE_NODE: {
6382 // foo[bar] ||= baz
6383 // ^^^^^^^^^^^^^^^^
6384 const pm_index_or_write_node_t *cast = (const pm_index_or_write_node_t *) node;
6385 pm_compile_index_control_flow_write_node(iseq, node, cast->receiver, cast->arguments, cast->block, cast->value, &location, ret, popped, scope_node);
6386 return;
6388 case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
6389 // @foo &&= bar
6390 // ^^^^^^^^^^^^
6391 const pm_instance_variable_and_write_node_t *cast = (const pm_instance_variable_and_write_node_t *) node;
6392 LABEL *end_label = NEW_LABEL(location.line);
6394 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
6395 VALUE name = ID2SYM(name_id);
6397 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
6398 if (!popped) PUSH_INSN(ret, location, dup);
6400 PUSH_INSNL(ret, location, branchunless, end_label);
6401 if (!popped) PUSH_INSN(ret, location, pop);
6403 PM_COMPILE_NOT_POPPED(cast->value);
6404 if (!popped) PUSH_INSN(ret, location, dup);
6406 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
6407 PUSH_LABEL(ret, end_label);
6409 return;
6411 case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
6412 // @foo += bar
6413 // ^^^^^^^^^^^
6414 const pm_instance_variable_operator_write_node_t *cast = (const pm_instance_variable_operator_write_node_t *) node;
6416 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
6417 VALUE name = ID2SYM(name_id);
6419 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
6420 PM_COMPILE_NOT_POPPED(cast->value);
6422 ID method_id = pm_constant_id_lookup(scope_node, cast->operator);
6423 int flags = VM_CALL_ARGS_SIMPLE;
6424 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(flags));
6426 if (!popped) PUSH_INSN(ret, location, dup);
6427 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
6429 return;
6431 case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
6432 // @foo ||= bar
6433 // ^^^^^^^^^^^^
6434 const pm_instance_variable_or_write_node_t *cast = (const pm_instance_variable_or_write_node_t *) node;
6435 LABEL *end_label = NEW_LABEL(location.line);
6437 ID name_id = pm_constant_id_lookup(scope_node, cast->name);
6438 VALUE name = ID2SYM(name_id);
6440 PUSH_INSN2(ret, location, getinstancevariable, name, get_ivar_ic_value(iseq, name_id));
6441 if (!popped) PUSH_INSN(ret, location, dup);
6443 PUSH_INSNL(ret, location, branchif, end_label);
6444 if (!popped) PUSH_INSN(ret, location, pop);
6446 PM_COMPILE_NOT_POPPED(cast->value);
6447 if (!popped) PUSH_INSN(ret, location, dup);
6449 PUSH_INSN2(ret, location, setinstancevariable, name, get_ivar_ic_value(iseq, name_id));
6450 PUSH_LABEL(ret, end_label);
6452 return;
6454 case PM_INSTANCE_VARIABLE_READ_NODE: {
6455 // @foo
6456 // ^^^^
6457 if (!popped) {
6458 const pm_instance_variable_read_node_t *cast = (const pm_instance_variable_read_node_t *) node;
6459 ID name = pm_constant_id_lookup(scope_node, cast->name);
6460 PUSH_INSN2(ret, location, getinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
6462 return;
6464 case PM_INSTANCE_VARIABLE_WRITE_NODE: {
6465 // @foo = 1
6466 // ^^^^^^^^
6467 const pm_instance_variable_write_node_t *cast = (const pm_instance_variable_write_node_t *) node;
6468 PM_COMPILE_NOT_POPPED(cast->value);
6469 if (!popped) PUSH_INSN(ret, location, dup);
6471 ID name = pm_constant_id_lookup(scope_node, cast->name);
6472 PUSH_INSN2(ret, location, setinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
6474 return;
6476 case PM_INTEGER_NODE: {
6477 // 1
6478 // ^
6479 if (!popped) {
6480 PUSH_INSN1(ret, location, putobject, parse_integer((const pm_integer_node_t *) node));
6482 return;
6484 case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
6485 // if /foo #{bar}/ then end
6486 // ^^^^^^^^^^^^
6487 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
6488 if (!popped) {
6489 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
6490 PUSH_INSN1(ret, location, putobject, regexp);
6493 else {
6494 pm_compile_regexp_dynamic(iseq, node, &((const pm_interpolated_match_last_line_node_t *) node)->parts, &location, ret, popped, scope_node);
6497 PUSH_INSN1(ret, location, getglobal, rb_id2sym(idLASTLINE));
6498 PUSH_SEND(ret, location, idEqTilde, INT2NUM(1));
6499 if (popped) PUSH_INSN(ret, location, pop);
6501 return;
6503 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
6504 // /foo #{bar}/
6505 // ^^^^^^^^^^^^
6506 if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ONCE)) {
6507 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
6508 const rb_iseq_t *block_iseq = NULL;
6509 int ise_index = ISEQ_BODY(iseq)->ise_size++;
6511 pm_scope_node_t next_scope_node;
6512 pm_scope_node_init(node, &next_scope_node, scope_node);
6514 block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.line);
6515 pm_scope_node_destroy(&next_scope_node);
6517 ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
6518 PUSH_INSN2(ret, location, once, block_iseq, INT2FIX(ise_index));
6519 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
6521 if (popped) PUSH_INSN(ret, location, pop);
6522 return;
6525 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
6526 if (!popped) {
6527 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
6528 PUSH_INSN1(ret, location, putobject, regexp);
6531 else {
6532 pm_compile_regexp_dynamic(iseq, node, &((const pm_interpolated_regular_expression_node_t *) node)->parts, &location, ret, popped, scope_node);
6533 if (popped) PUSH_INSN(ret, location, pop);
6536 return;
6538 case PM_INTERPOLATED_STRING_NODE: {
6539 // "foo #{bar}"
6540 // ^^^^^^^^^^^^
6541 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
6542 if (!popped) {
6543 VALUE string = pm_static_literal_value(iseq, node, scope_node);
6545 if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN)) {
6546 PUSH_INSN1(ret, location, putobject, string);
6548 else if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) {
6549 PUSH_INSN1(ret, location, putstring, string);
6551 else {
6552 PUSH_INSN1(ret, location, putchilledstring, string);
6556 else {
6557 const pm_interpolated_string_node_t *cast = (const pm_interpolated_string_node_t *) node;
6558 int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL);
6559 if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
6560 if (popped) PUSH_INSN(ret, location, pop);
6563 return;
6565 case PM_INTERPOLATED_SYMBOL_NODE: {
6566 // :"foo #{bar}"
6567 // ^^^^^^^^^^^^^
6568 const pm_interpolated_symbol_node_t *cast = (const pm_interpolated_symbol_node_t *) node;
6570 if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
6571 if (!popped) {
6572 VALUE symbol = pm_static_literal_value(iseq, node, scope_node);
6573 PUSH_INSN1(ret, location, putobject, symbol);
6576 else {
6577 int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL);
6578 if (length > 1) {
6579 PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
6582 if (!popped) {
6583 PUSH_INSN(ret, location, intern);
6585 else {
6586 PUSH_INSN(ret, location, pop);
6590 return;
6592 case PM_INTERPOLATED_X_STRING_NODE: {
6593 // `foo #{bar}`
6594 // ^^^^^^^^^^^^
6595 const pm_interpolated_x_string_node_t *cast = (const pm_interpolated_x_string_node_t *) node;
6597 PUSH_INSN(ret, location, putself);
6599 int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, false, scope_node, NULL, NULL);
6600 if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
6602 PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
6603 if (popped) PUSH_INSN(ret, location, pop);
6605 return;
6607 case PM_KEYWORD_HASH_NODE: {
6608 // foo(bar: baz)
6609 // ^^^^^^^^
6610 const pm_keyword_hash_node_t *cast = (const pm_keyword_hash_node_t *) node;
6611 const pm_node_list_t *elements = &cast->elements;
6613 const pm_node_t *element;
6614 PM_NODE_LIST_FOREACH(elements, index, element) {
6615 PM_COMPILE(element);
6618 if (!popped) PUSH_INSN1(ret, location, newhash, INT2FIX(elements->size * 2));
6619 return;
6621 case PM_LAMBDA_NODE: {
6622 // -> {}
6623 // ^^^^^
6624 const pm_lambda_node_t *cast = (const pm_lambda_node_t *) node;
6626 pm_scope_node_t next_scope_node;
6627 pm_scope_node_init(node, &next_scope_node, scope_node);
6629 int opening_lineno = pm_location_line_number(parser, &cast->opening_loc);
6630 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
6631 pm_scope_node_destroy(&next_scope_node);
6633 VALUE argc = INT2FIX(0);
6634 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
6635 PUSH_CALL_WITH_BLOCK(ret, location, idLambda, argc, block);
6636 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
6638 if (popped) PUSH_INSN(ret, location, pop);
6639 return;
6641 case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
6642 // foo &&= bar
6643 // ^^^^^^^^^^^
6644 const pm_local_variable_and_write_node_t *cast = (const pm_local_variable_and_write_node_t *) node;
6645 LABEL *end_label = NEW_LABEL(location.line);
6647 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
6648 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
6649 if (!popped) PUSH_INSN(ret, location, dup);
6651 PUSH_INSNL(ret, location, branchunless, end_label);
6652 if (!popped) PUSH_INSN(ret, location, pop);
6654 PM_COMPILE_NOT_POPPED(cast->value);
6655 if (!popped) PUSH_INSN(ret, location, dup);
6657 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
6658 PUSH_LABEL(ret, end_label);
6660 return;
6662 case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
6663 // foo += bar
6664 // ^^^^^^^^^^
6665 const pm_local_variable_operator_write_node_t *cast = (const pm_local_variable_operator_write_node_t *) node;
6667 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
6668 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
6670 PM_COMPILE_NOT_POPPED(cast->value);
6672 ID method_id = pm_constant_id_lookup(scope_node, cast->operator);
6673 PUSH_SEND_WITH_FLAG(ret, location, method_id, INT2NUM(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
6675 if (!popped) PUSH_INSN(ret, location, dup);
6676 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
6678 return;
6680 case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
6681 // foo ||= bar
6682 // ^^^^^^^^^^^
6683 const pm_local_variable_or_write_node_t *cast = (const pm_local_variable_or_write_node_t *) node;
6685 LABEL *set_label = NEW_LABEL(location.line);
6686 LABEL *end_label = NEW_LABEL(location.line);
6688 PUSH_INSN1(ret, location, putobject, Qtrue);
6689 PUSH_INSNL(ret, location, branchunless, set_label);
6691 pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
6692 PUSH_GETLOCAL(ret, location, local_index.index, local_index.level);
6693 if (!popped) PUSH_INSN(ret, location, dup);
6695 PUSH_INSNL(ret, location, branchif, end_label);
6696 if (!popped) PUSH_INSN(ret, location, pop);
6698 PUSH_LABEL(ret, set_label);
6699 PM_COMPILE_NOT_POPPED(cast->value);
6700 if (!popped) PUSH_INSN(ret, location, dup);
6702 PUSH_SETLOCAL(ret, location, local_index.index, local_index.level);
6703 PUSH_LABEL(ret, end_label);
6705 return;
6707 case PM_LOCAL_VARIABLE_READ_NODE: {
6708 // foo
6709 // ^^^
6710 const pm_local_variable_read_node_t *cast = (const pm_local_variable_read_node_t *) node;
6712 if (!popped) {
6713 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
6714 PUSH_GETLOCAL(ret, location, index.index, index.level);
6717 return;
6719 case PM_LOCAL_VARIABLE_WRITE_NODE: {
6720 // foo = 1
6721 // ^^^^^^^
6722 const pm_local_variable_write_node_t *cast = (const pm_local_variable_write_node_t *) node;
6723 PM_COMPILE_NOT_POPPED(cast->value);
6724 if (!popped) PUSH_INSN(ret, location, dup);
6726 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
6727 PUSH_SETLOCAL(ret, location, index.index, index.level);
6728 return;
6730 case PM_MATCH_LAST_LINE_NODE: {
6731 // if /foo/ then end
6732 // ^^^^^
6733 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
6735 PUSH_INSN1(ret, location, putobject, regexp);
6736 PUSH_INSN2(ret, location, getspecial, INT2FIX(0), INT2FIX(0));
6737 PUSH_SEND(ret, location, idEqTilde, INT2NUM(1));
6738 if (popped) PUSH_INSN(ret, location, pop);
6740 return;
6742 case PM_MATCH_PREDICATE_NODE: {
6743 // foo in bar
6744 // ^^^^^^^^^^
6745 const pm_match_predicate_node_t *cast = (const pm_match_predicate_node_t *) node;
6747 // First, allocate some stack space for the cached return value of any
6748 // calls to #deconstruct.
6749 PUSH_INSN(ret, location, putnil);
6751 // Next, compile the expression that we're going to match against.
6752 PM_COMPILE_NOT_POPPED(cast->value);
6753 PUSH_INSN(ret, location, dup);
6755 // Now compile the pattern that is going to be used to match against the
6756 // expression.
6757 LABEL *matched_label = NEW_LABEL(location.line);
6758 LABEL *unmatched_label = NEW_LABEL(location.line);
6759 LABEL *done_label = NEW_LABEL(location.line);
6760 pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, false, false, true, 2);
6762 // If the pattern did not match, then compile the necessary instructions
6763 // to handle pushing false onto the stack, then jump to the end.
6764 PUSH_LABEL(ret, unmatched_label);
6765 PUSH_INSN(ret, location, pop);
6766 PUSH_INSN(ret, location, pop);
6768 if (!popped) PUSH_INSN1(ret, location, putobject, Qfalse);
6769 PUSH_INSNL(ret, location, jump, done_label);
6770 PUSH_INSN(ret, location, putnil);
6772 // If the pattern did match, then compile the necessary instructions to
6773 // handle pushing true onto the stack, then jump to the end.
6774 PUSH_LABEL(ret, matched_label);
6775 PUSH_INSN1(ret, location, adjuststack, INT2FIX(2));
6776 if (!popped) PUSH_INSN1(ret, location, putobject, Qtrue);
6777 PUSH_INSNL(ret, location, jump, done_label);
6779 PUSH_LABEL(ret, done_label);
6780 return;
6782 case PM_MATCH_REQUIRED_NODE: {
6783 // foo => bar
6784 // ^^^^^^^^^^
6786 // A match required node represents pattern matching against a single
6787 // pattern using the => operator. For example,
6789 // foo => bar
6791 // This is somewhat analogous to compiling a case match statement with a
6792 // single pattern. In both cases, if the pattern fails it should
6793 // immediately raise an error.
6794 const pm_match_required_node_t *cast = (const pm_match_required_node_t *) node;
6796 LABEL *matched_label = NEW_LABEL(location.line);
6797 LABEL *unmatched_label = NEW_LABEL(location.line);
6798 LABEL *done_label = NEW_LABEL(location.line);
6800 // First, we're going to push a bunch of stuff onto the stack that is
6801 // going to serve as our scratch space.
6802 PUSH_INSN(ret, location, putnil); // key error key
6803 PUSH_INSN(ret, location, putnil); // key error matchee
6804 PUSH_INSN1(ret, location, putobject, Qfalse); // key error?
6805 PUSH_INSN(ret, location, putnil); // error string
6806 PUSH_INSN(ret, location, putnil); // deconstruct cache
6808 // Next we're going to compile the value expression such that it's on
6809 // the stack.
6810 PM_COMPILE_NOT_POPPED(cast->value);
6812 // Here we'll dup it so that it can be used for comparison, but also be
6813 // used for error handling.
6814 PUSH_INSN(ret, location, dup);
6816 // Next we'll compile the pattern. We indicate to the pm_compile_pattern
6817 // function that this is the only pattern that will be matched against
6818 // through the in_single_pattern parameter. We also indicate that the
6819 // value to compare against is 2 slots from the top of the stack (the
6820 // base_index parameter).
6821 pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, true, false, true, 2);
6823 // If the pattern did not match the value, then we're going to compile
6824 // in our error handler code. This will determine which error to raise
6825 // and raise it.
6826 PUSH_LABEL(ret, unmatched_label);
6827 pm_compile_pattern_error_handler(iseq, scope_node, node, ret, done_label, popped);
6829 // If the pattern did match, we'll clean up the values we've pushed onto
6830 // the stack and then push nil onto the stack if it's not popped.
6831 PUSH_LABEL(ret, matched_label);
6832 PUSH_INSN1(ret, location, adjuststack, INT2FIX(6));
6833 if (!popped) PUSH_INSN(ret, location, putnil);
6834 PUSH_INSNL(ret, location, jump, done_label);
6836 PUSH_LABEL(ret, done_label);
6837 return;
6839 case PM_MATCH_WRITE_NODE: {
6840 // /(?<foo>foo)/ =~ bar
6841 // ^^^^^^^^^^^^^^^^^^^^
6843 // Match write nodes are specialized call nodes that have a regular
6844 // expression with valid named capture groups on the left, the =~
6845 // operator, and some value on the right. The nodes themselves simply
6846 // wrap the call with the local variable targets that will be written
6847 // when the call is executed.
6848 const pm_match_write_node_t *cast = (const pm_match_write_node_t *) node;
6849 LABEL *fail_label = NEW_LABEL(location.line);
6850 LABEL *end_label = NEW_LABEL(location.line);
6852 // First, we'll compile the call so that all of its instructions are
6853 // present. Then we'll compile all of the local variable targets.
6854 PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->call);
6856 // Now, check if the match was successful. If it was, then we'll
6857 // continue on and assign local variables. Otherwise we'll skip over the
6858 // assignment code.
6859 PUSH_INSN1(ret, location, getglobal, rb_id2sym(idBACKREF));
6860 PUSH_INSN(ret, location, dup);
6861 PUSH_INSNL(ret, location, branchunless, fail_label);
6863 // If there's only a single local variable target, we can skip some of
6864 // the bookkeeping, so we'll put a special branch here.
6865 size_t targets_count = cast->targets.size;
6867 if (targets_count == 1) {
6868 const pm_node_t *target = cast->targets.nodes[0];
6869 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
6871 const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
6872 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
6874 PUSH_INSN1(ret, location, putobject, rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name)));
6875 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
6876 PUSH_LABEL(ret, fail_label);
6877 PUSH_SETLOCAL(ret, location, index.index, index.level);
6878 if (popped) PUSH_INSN(ret, location, pop);
6879 return;
6882 DECL_ANCHOR(fail_anchor);
6883 INIT_ANCHOR(fail_anchor);
6885 // Otherwise there is more than one local variable target, so we'll need
6886 // to do some bookkeeping.
6887 for (size_t targets_index = 0; targets_index < targets_count; targets_index++) {
6888 const pm_node_t *target = cast->targets.nodes[targets_index];
6889 RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
6891 const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
6892 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
6894 if (((size_t) targets_index) < (targets_count - 1)) {
6895 PUSH_INSN(ret, location, dup);
6897 PUSH_INSN1(ret, location, putobject, rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name)));
6898 PUSH_SEND(ret, location, idAREF, INT2FIX(1));
6899 PUSH_SETLOCAL(ret, location, index.index, index.level);
6901 PUSH_INSN(fail_anchor, location, putnil);
6902 PUSH_SETLOCAL(fail_anchor, location, index.index, index.level);
6905 // Since we matched successfully, now we'll jump to the end.
6906 PUSH_INSNL(ret, location, jump, end_label);
6908 // In the case that the match failed, we'll loop through each local
6909 // variable target and set all of them to `nil`.
6910 PUSH_LABEL(ret, fail_label);
6911 PUSH_INSN(ret, location, pop);
6912 PUSH_SEQ(ret, fail_anchor);
6914 // Finally, we can push the end label for either case.
6915 PUSH_LABEL(ret, end_label);
6916 if (popped) PUSH_INSN(ret, location, pop);
6917 return;
6919 case PM_MISSING_NODE: {
6920 rb_bug("A pm_missing_node_t should not exist in prism's AST.");
6921 return;
6923 case PM_MODULE_NODE: {
6924 // module Foo; end
6925 // ^^^^^^^^^^^^^^^
6926 const pm_module_node_t *cast = (const pm_module_node_t *) node;
6928 ID module_id = pm_constant_id_lookup(scope_node, cast->name);
6929 VALUE module_name = rb_str_freeze(rb_sprintf("<module:%"PRIsVALUE">", rb_id2str(module_id)));
6931 pm_scope_node_t next_scope_node;
6932 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
6934 const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, location.line);
6935 pm_scope_node_destroy(&next_scope_node);
6937 const int flags = VM_DEFINECLASS_TYPE_MODULE | pm_compile_class_path(iseq, cast->constant_path, &location, ret, false, scope_node);
6938 PUSH_INSN(ret, location, putnil);
6939 PUSH_INSN3(ret, location, defineclass, ID2SYM(module_id), module_iseq, INT2FIX(flags));
6940 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) module_iseq);
6942 if (popped) PUSH_INSN(ret, location, pop);
6943 return;
6945 case PM_REQUIRED_PARAMETER_NODE: {
6946 // def foo(bar); end
6947 // ^^^
6948 const pm_required_parameter_node_t *cast = (const pm_required_parameter_node_t *) node;
6949 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
6951 PUSH_SETLOCAL(ret, location, index.index, index.level);
6952 return;
6954 case PM_MULTI_WRITE_NODE: {
6955 // foo, bar = baz
6956 // ^^^^^^^^^^^^^^
6958 // A multi write node represents writing to multiple values using an =
6959 // operator. Importantly these nodes are only parsed when the left-hand
6960 // side of the operator has multiple targets. The right-hand side of the
6961 // operator having multiple targets represents an implicit array
6962 // instead.
6963 const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
6965 DECL_ANCHOR(writes);
6966 INIT_ANCHOR(writes);
6968 DECL_ANCHOR(cleanup);
6969 INIT_ANCHOR(cleanup);
6971 pm_multi_target_state_t state = { 0 };
6972 state.position = popped ? 0 : 1;
6973 size_t stack_size = pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
6975 PM_COMPILE_NOT_POPPED(cast->value);
6976 if (!popped) PUSH_INSN(ret, location, dup);
6978 PUSH_SEQ(ret, writes);
6979 if (!popped && stack_size >= 1) {
6980 // Make sure the value on the right-hand side of the = operator is
6981 // being returned before we pop the parent expressions.
6982 PUSH_INSN1(ret, location, setn, INT2FIX(stack_size));
6985 PUSH_SEQ(ret, cleanup);
6986 return;
6988 case PM_NEXT_NODE: {
6989 // next
6990 // ^^^^
6992 // next foo
6993 // ^^^^^^^^
6994 const pm_next_node_t *cast = (const pm_next_node_t *) node;
6996 if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
6997 LABEL *splabel = NEW_LABEL(0);
6998 PUSH_LABEL(ret, splabel);
7000 if (cast->arguments) {
7001 PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
7003 else {
7004 PUSH_INSN(ret, location, putnil);
7006 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7008 PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7009 PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7011 PUSH_ADJUST_RESTORE(ret, splabel);
7012 if (!popped) PUSH_INSN(ret, location, putnil);
7014 else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
7015 LABEL *splabel = NEW_LABEL(0);
7017 PUSH_LABEL(ret, splabel);
7018 PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->start_label);
7020 if (cast->arguments != NULL) {
7021 PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
7023 else {
7024 PUSH_INSN(ret, location, putnil);
7027 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7028 PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
7029 PUSH_ADJUST_RESTORE(ret, splabel);
7030 splabel->unremovable = FALSE;
7032 if (!popped) PUSH_INSN(ret, location, putnil);
7034 else {
7035 const rb_iseq_t *ip = iseq;
7036 unsigned long throw_flag = 0;
7038 while (ip) {
7039 if (!ISEQ_COMPILE_DATA(ip)) {
7040 ip = 0;
7041 break;
7044 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
7045 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7046 /* while loop */
7047 break;
7049 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
7050 break;
7052 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
7053 COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with next");
7054 return;
7057 ip = ISEQ_BODY(ip)->parent_iseq;
7059 if (ip != 0) {
7060 if (cast->arguments) {
7061 PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
7063 else {
7064 PUSH_INSN(ret, location, putnil);
7067 PUSH_INSN1(ret, location, throw, INT2FIX(throw_flag | TAG_NEXT));
7068 if (popped) PUSH_INSN(ret, location, pop);
7070 else {
7071 COMPILE_ERROR(ERROR_ARGS "Invalid next");
7072 return;
7076 return;
7078 case PM_NIL_NODE: {
7079 // nil
7080 // ^^^
7081 if (!popped) {
7082 PUSH_INSN(ret, location, putnil);
7085 return;
7087 case PM_NO_KEYWORDS_PARAMETER_NODE: {
7088 // def foo(**nil); end
7089 // ^^^^^
7090 ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
7091 return;
7093 case PM_NUMBERED_REFERENCE_READ_NODE: {
7094 // $1
7095 // ^^
7096 if (!popped) {
7097 uint32_t reference_number = ((const pm_numbered_reference_read_node_t *) node)->number;
7099 if (reference_number > 0) {
7100 PUSH_INSN2(ret, location, getspecial, INT2FIX(1), INT2FIX(reference_number << 1));
7102 else {
7103 PUSH_INSN(ret, location, putnil);
7107 return;
7109 case PM_OR_NODE: {
7110 // a or b
7111 // ^^^^^^
7112 const pm_or_node_t *cast = (const pm_or_node_t *) node;
7114 LABEL *end_label = NEW_LABEL(location.line);
7115 PM_COMPILE_NOT_POPPED(cast->left);
7117 if (!popped) PUSH_INSN(ret, location, dup);
7118 PUSH_INSNL(ret, location, branchif, end_label);
7120 if (!popped) PUSH_INSN(ret, location, pop);
7121 PM_COMPILE(cast->right);
7122 PUSH_LABEL(ret, end_label);
7124 return;
7126 case PM_OPTIONAL_PARAMETER_NODE: {
7127 // def foo(bar = 1); end
7128 // ^^^^^^^
7129 const pm_optional_parameter_node_t *cast = (const pm_optional_parameter_node_t *) node;
7130 PM_COMPILE_NOT_POPPED(cast->value);
7132 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
7133 PUSH_SETLOCAL(ret, location, index.index, index.level);
7135 return;
7137 case PM_PARENTHESES_NODE: {
7138 // ()
7139 // ^^
7141 // (1)
7142 // ^^^
7143 const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
7145 if (cast->body != NULL) {
7146 PM_COMPILE(cast->body);
7148 else if (!popped) {
7149 PUSH_INSN(ret, location, putnil);
7152 return;
7154 case PM_PRE_EXECUTION_NODE: {
7155 // BEGIN {}
7156 // ^^^^^^^^
7157 const pm_pre_execution_node_t *cast = (const pm_pre_execution_node_t *) node;
7159 LINK_ANCHOR *outer_pre = scope_node->pre_execution_anchor;
7160 RUBY_ASSERT(outer_pre != NULL);
7162 // BEGIN{} nodes can be nested, so here we're going to do the same thing
7163 // that we did for the top-level compilation where we create two
7164 // anchors and then join them in the correct order into the resulting
7165 // anchor.
7166 DECL_ANCHOR(inner_pre);
7167 INIT_ANCHOR(inner_pre);
7168 scope_node->pre_execution_anchor = inner_pre;
7170 DECL_ANCHOR(inner_body);
7171 INIT_ANCHOR(inner_body);
7173 if (cast->statements != NULL) {
7174 const pm_node_list_t *body = &cast->statements->body;
7176 for (size_t index = 0; index < body->size; index++) {
7177 pm_compile_node(iseq, body->nodes[index], inner_body, true, scope_node);
7181 if (!popped) {
7182 PUSH_INSN(inner_body, location, putnil);
7185 // Now that everything has been compiled, join both anchors together
7186 // into the correct outer pre execution anchor, and reset the value so
7187 // that subsequent BEGIN{} nodes can be compiled correctly.
7188 PUSH_SEQ(outer_pre, inner_pre);
7189 PUSH_SEQ(outer_pre, inner_body);
7190 scope_node->pre_execution_anchor = outer_pre;
7192 return;
7194 case PM_POST_EXECUTION_NODE: {
7195 // END {}
7196 // ^^^^^^
7197 const rb_iseq_t *child_iseq;
7198 const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
7200 pm_scope_node_t next_scope_node;
7201 pm_scope_node_init(node, &next_scope_node, scope_node);
7202 child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
7203 pm_scope_node_destroy(&next_scope_node);
7205 ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
7207 int is_index = ISEQ_BODY(iseq)->ise_size++;
7208 PUSH_INSN2(ret, location, once, child_iseq, INT2FIX(is_index));
7209 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
7210 if (popped) PUSH_INSN(ret, location, pop);
7212 ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
7214 return;
7216 case PM_RANGE_NODE: {
7217 // 0..5
7218 // ^^^^
7219 const pm_range_node_t *cast = (const pm_range_node_t *) node;
7220 bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
7222 if (pm_optimizable_range_item_p(cast->left) && pm_optimizable_range_item_p(cast->right)) {
7223 if (!popped) {
7224 const pm_node_t *left = cast->left;
7225 const pm_node_t *right = cast->right;
7227 VALUE val = rb_range_new(
7228 (left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) left) : Qnil,
7229 (right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) right) : Qnil,
7230 exclude_end
7233 PUSH_INSN1(ret, location, putobject, val);
7236 else {
7237 if (cast->left == NULL) {
7238 PUSH_INSN(ret, location, putnil);
7240 else {
7241 PM_COMPILE(cast->left);
7244 if (cast->right == NULL) {
7245 PUSH_INSN(ret, location, putnil);
7247 else {
7248 PM_COMPILE(cast->right);
7251 if (!popped) {
7252 PUSH_INSN1(ret, location, newrange, INT2FIX(exclude_end ? 1 : 0));
7255 return;
7257 case PM_RATIONAL_NODE: {
7258 // 1r
7259 // ^^
7260 if (!popped) {
7261 PUSH_INSN1(ret, location, putobject, parse_rational((const pm_rational_node_t *) node));
7263 return;
7265 case PM_REDO_NODE: {
7266 // redo
7267 // ^^^^
7268 if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
7269 LABEL *splabel = NEW_LABEL(0);
7271 PUSH_LABEL(ret, splabel);
7272 PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->redo_label);
7273 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7275 PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
7276 PUSH_ADJUST_RESTORE(ret, splabel);
7277 if (!popped) PUSH_INSN(ret, location, putnil);
7279 else if (ISEQ_BODY(iseq)->type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
7280 LABEL *splabel = NEW_LABEL(0);
7282 PUSH_LABEL(ret, splabel);
7283 pm_add_ensure_iseq(ret, iseq, 0, scope_node);
7284 PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->start_label);
7286 PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
7287 PUSH_ADJUST_RESTORE(ret, splabel);
7288 if (!popped) PUSH_INSN(ret, location, putnil);
7290 else {
7291 const rb_iseq_t *ip = iseq;
7293 while (ip) {
7294 if (!ISEQ_COMPILE_DATA(ip)) {
7295 ip = 0;
7296 break;
7299 if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
7300 break;
7302 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
7303 break;
7305 else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
7306 COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with redo");
7307 return;
7310 ip = ISEQ_BODY(ip)->parent_iseq;
7313 if (ip != 0) {
7314 PUSH_INSN(ret, location, putnil);
7315 PUSH_INSN1(ret, location, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
7316 if (popped) PUSH_INSN(ret, location, pop);
7318 else {
7319 COMPILE_ERROR(ERROR_ARGS "Invalid redo");
7320 return;
7323 return;
7325 case PM_REGULAR_EXPRESSION_NODE: {
7326 // /foo/
7327 // ^^^^^
7328 if (!popped) {
7329 VALUE regexp = pm_static_literal_value(iseq, node, scope_node);
7330 PUSH_INSN1(ret, location, putobject, regexp);
7332 return;
7334 case PM_RESCUE_NODE: {
7335 // begin; rescue; end
7336 // ^^^^^^^
7337 const pm_rescue_node_t *cast = (const pm_rescue_node_t *) node;
7338 iseq_set_exception_local_table(iseq);
7340 // First, establish the labels that we need to be able to jump to within
7341 // this compilation block.
7342 LABEL *exception_match_label = NEW_LABEL(location.line);
7343 LABEL *rescue_end_label = NEW_LABEL(location.line);
7345 // Next, compile each of the exceptions that we're going to be
7346 // handling. For each one, we'll add instructions to check if the
7347 // exception matches the raised one, and if it does then jump to the
7348 // exception_match_label label. Otherwise it will fall through to the
7349 // subsequent check. If there are no exceptions, we'll only check
7350 // StandardError.
7351 const pm_node_list_t *exceptions = &cast->exceptions;
7353 if (exceptions->size > 0) {
7354 for (size_t index = 0; index < exceptions->size; index++) {
7355 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7356 PM_COMPILE(exceptions->nodes[index]);
7357 int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
7358 if (PM_NODE_TYPE_P(exceptions->nodes[index], PM_SPLAT_NODE)) {
7359 checkmatch_flags |= VM_CHECKMATCH_ARRAY;
7361 PUSH_INSN1(ret, location, checkmatch, INT2FIX(checkmatch_flags));
7362 PUSH_INSNL(ret, location, branchif, exception_match_label);
7365 else {
7366 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7367 PUSH_INSN1(ret, location, putobject, rb_eStandardError);
7368 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
7369 PUSH_INSNL(ret, location, branchif, exception_match_label);
7372 // If none of the exceptions that we are matching against matched, then
7373 // we'll jump straight to the rescue_end_label label.
7374 PUSH_INSNL(ret, location, jump, rescue_end_label);
7376 // Here we have the exception_match_label, which is where the
7377 // control-flow goes in the case that one of the exceptions matched.
7378 // Here we will compile the instructions to handle the exception.
7379 PUSH_LABEL(ret, exception_match_label);
7380 PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
7382 // If we have a reference to the exception, then we'll compile the write
7383 // into the instruction sequence. This can look quite different
7384 // depending on the kind of write being performed.
7385 if (cast->reference) {
7386 DECL_ANCHOR(writes);
7387 INIT_ANCHOR(writes);
7389 DECL_ANCHOR(cleanup);
7390 INIT_ANCHOR(cleanup);
7392 pm_compile_target_node(iseq, cast->reference, ret, writes, cleanup, scope_node, NULL);
7393 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
7395 PUSH_SEQ(ret, writes);
7396 PUSH_SEQ(ret, cleanup);
7399 // If we have statements to execute, we'll compile them here. Otherwise
7400 // we'll push nil onto the stack.
7401 if (cast->statements) {
7402 // We'll temporarily remove the end_label location from the iseq
7403 // when compiling the statements so that next/redo statements
7404 // inside the body will throw to the correct place instead of
7405 // jumping straight to the end of this iseq
7406 LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
7407 ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
7409 PM_COMPILE((const pm_node_t *) cast->statements);
7411 // Now restore the end_label
7412 ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
7414 else {
7415 PUSH_INSN(ret, location, putnil);
7418 PUSH_INSN(ret, location, leave);
7420 // Here we'll insert the rescue_end_label label, which is jumped to if
7421 // none of the exceptions matched. It will cause the control-flow to
7422 // either jump to the next rescue clause or it will fall through to the
7423 // subsequent instruction returning the raised error.
7424 PUSH_LABEL(ret, rescue_end_label);
7425 if (cast->consequent) {
7426 PM_COMPILE((const pm_node_t *) cast->consequent);
7428 else {
7429 PUSH_GETLOCAL(ret, location, 1, 0);
7432 return;
7434 case PM_RESCUE_MODIFIER_NODE: {
7435 // foo rescue bar
7436 // ^^^^^^^^^^^^^^
7437 const pm_rescue_modifier_node_t *cast = (const pm_rescue_modifier_node_t *) node;
7439 pm_scope_node_t rescue_scope_node;
7440 pm_scope_node_init((const pm_node_t *) cast, &rescue_scope_node, scope_node);
7442 rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
7443 &rescue_scope_node,
7444 rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
7445 ISEQ_TYPE_RESCUE,
7446 pm_node_line_number(parser, cast->rescue_expression)
7449 pm_scope_node_destroy(&rescue_scope_node);
7451 LABEL *lstart = NEW_LABEL(location.line);
7452 LABEL *lend = NEW_LABEL(location.line);
7453 LABEL *lcont = NEW_LABEL(location.line);
7455 lstart->rescued = LABEL_RESCUE_BEG;
7456 lend->rescued = LABEL_RESCUE_END;
7457 PUSH_LABEL(ret, lstart);
7458 PM_COMPILE_NOT_POPPED(cast->expression);
7459 PUSH_LABEL(ret, lend);
7460 PUSH_INSN(ret, location, nop);
7461 PUSH_LABEL(ret, lcont);
7462 if (popped) PUSH_INSN(ret, location, pop);
7464 PUSH_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
7465 PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
7466 return;
7468 case PM_RETURN_NODE: {
7469 // return
7470 // ^^^^^^
7472 // return 1
7473 // ^^^^^^^^
7474 const pm_return_node_t *cast = (const pm_return_node_t *) node;
7475 const pm_arguments_node_t *arguments = cast->arguments;
7477 if (PM_NODE_FLAG_P(cast, PM_RETURN_NODE_FLAGS_REDUNDANT)) {
7478 if (arguments) {
7479 PM_COMPILE_NOT_POPPED((const pm_node_t *) arguments);
7481 else {
7482 PUSH_INSN(ret, location, putnil);
7485 else {
7486 enum rb_iseq_type type = ISEQ_BODY(iseq)->type;
7487 LABEL *splabel = 0;
7489 const rb_iseq_t *parent_iseq = iseq;
7490 enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
7491 while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
7492 if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq)) break;
7493 parent_type = ISEQ_BODY(parent_iseq)->type;
7496 switch (parent_type) {
7497 case ISEQ_TYPE_TOP:
7498 case ISEQ_TYPE_MAIN:
7499 if (arguments) {
7500 rb_warn("argument of top-level return is ignored");
7502 if (parent_iseq == iseq) {
7503 type = ISEQ_TYPE_METHOD;
7505 break;
7506 default:
7507 break;
7510 if (type == ISEQ_TYPE_METHOD) {
7511 splabel = NEW_LABEL(0);
7512 PUSH_LABEL(ret, splabel);
7513 PUSH_ADJUST(ret, location, 0);
7516 if (arguments) {
7517 PM_COMPILE_NOT_POPPED((const pm_node_t *) arguments);
7519 else {
7520 PUSH_INSN(ret, location, putnil);
7523 if (type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
7524 pm_add_ensure_iseq(ret, iseq, 1, scope_node);
7525 PUSH_TRACE(ret, RUBY_EVENT_RETURN);
7526 PUSH_INSN(ret, location, leave);
7527 PUSH_ADJUST_RESTORE(ret, splabel);
7528 if (!popped) PUSH_INSN(ret, location, putnil);
7530 else {
7531 PUSH_INSN1(ret, location, throw, INT2FIX(TAG_RETURN));
7532 if (popped) PUSH_INSN(ret, location, pop);
7536 return;
7538 case PM_RETRY_NODE: {
7539 // retry
7540 // ^^^^^
7541 if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_RESCUE) {
7542 PUSH_INSN(ret, location, putnil);
7543 PUSH_INSN1(ret, location, throw, INT2FIX(TAG_RETRY));
7544 if (popped) PUSH_INSN(ret, location, pop);
7546 else {
7547 COMPILE_ERROR(ERROR_ARGS "Invalid retry");
7548 return;
7550 return;
7552 case PM_SCOPE_NODE: {
7553 pm_scope_node_t *scope_node = (pm_scope_node_t *) node;
7554 pm_constant_id_list_t *locals = &scope_node->locals;
7556 pm_parameters_node_t *parameters_node = NULL;
7557 pm_node_list_t *keywords_list = NULL;
7558 pm_node_list_t *optionals_list = NULL;
7559 pm_node_list_t *posts_list = NULL;
7560 pm_node_list_t *requireds_list = NULL;
7561 pm_node_list_t *block_locals = NULL;
7562 bool trailing_comma = false;
7564 struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
7566 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE)) {
7567 ADD_TRACE(ret, RUBY_EVENT_CLASS);
7570 if (scope_node->parameters) {
7571 switch (PM_NODE_TYPE(scope_node->parameters)) {
7572 case PM_BLOCK_PARAMETERS_NODE: {
7573 pm_block_parameters_node_t *cast = (pm_block_parameters_node_t *) scope_node->parameters;
7574 parameters_node = cast->parameters;
7575 block_locals = &cast->locals;
7577 if (parameters_node) {
7578 if (parameters_node->rest && PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE)) {
7579 trailing_comma = true;
7582 break;
7584 case PM_PARAMETERS_NODE: {
7585 parameters_node = (pm_parameters_node_t *) scope_node->parameters;
7586 break;
7588 case PM_NUMBERED_PARAMETERS_NODE: {
7589 uint32_t maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
7590 body->param.lead_num = maximum;
7591 body->param.flags.ambiguous_param0 = maximum == 1;
7592 break;
7594 case PM_IT_PARAMETERS_NODE:
7595 body->param.lead_num = 1;
7596 body->param.flags.ambiguous_param0 = true;
7597 break;
7598 default:
7599 rb_bug("Unexpected node type for parameters: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
7603 struct rb_iseq_param_keyword *keyword = NULL;
7605 if (parameters_node) {
7606 optionals_list = &parameters_node->optionals;
7607 requireds_list = &parameters_node->requireds;
7608 keywords_list = &parameters_node->keywords;
7609 posts_list = &parameters_node->posts;
7611 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))) {
7612 body->param.opt_num = 0;
7614 else {
7615 body->param.lead_num = 0;
7616 body->param.opt_num = 0;
7619 //********STEP 1**********
7620 // Goal: calculate the table size for the locals, accounting for
7621 // hidden variables and multi target nodes
7622 size_t locals_size = locals->size;
7624 // Index lookup table buffer size is only the number of the locals
7625 st_table *index_lookup_table = st_init_numtable();
7627 int table_size = (int) locals_size;
7629 // For nodes have a hidden iteration variable. We add that to the local
7630 // table size here.
7631 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
7633 if (keywords_list && keywords_list->size) {
7634 table_size++;
7637 if (requireds_list) {
7638 for (size_t i = 0; i < requireds_list->size; i++) {
7639 // For each MultiTargetNode, we're going to have one
7640 // additional anonymous local not represented in the locals table
7641 // We want to account for this in our table size
7642 pm_node_t *required = requireds_list->nodes[i];
7643 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
7644 table_size++;
7646 else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
7647 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7648 table_size++;
7654 // Ensure there is enough room in the local table for any
7655 // parameters that have been repeated
7656 // ex: def underscore_parameters(_, _ = 1, _ = 2); _; end
7657 // ^^^^^^^^^^^^
7658 if (optionals_list && optionals_list->size) {
7659 for (size_t i = 0; i < optionals_list->size; i++) {
7660 pm_node_t * node = optionals_list->nodes[i];
7661 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7662 table_size++;
7667 // If we have an anonymous "rest" node, we'll need to increase the local
7668 // table size to take it in to account.
7669 // def m(foo, *, bar)
7670 // ^
7671 if (parameters_node) {
7672 if (parameters_node->rest) {
7673 if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
7674 if (!((const pm_rest_parameter_node_t *) parameters_node->rest)->name || PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7675 table_size++;
7680 // def foo(_, **_); _; end
7681 // ^^^
7682 if (parameters_node->keyword_rest) {
7683 // def foo(...); end
7684 // ^^^
7685 // When we have a `...` as the keyword_rest, it's a forwarding_parameter_node and
7686 // we need to leave space for 4 locals: *, **, &, ...
7687 if (PM_NODE_TYPE_P(parameters_node->keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
7688 table_size += 4;
7690 else {
7691 const pm_keyword_rest_parameter_node_t *kw_rest = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
7693 // If it's anonymous or repeated, then we need to allocate stack space
7694 if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7695 table_size++;
7701 if (posts_list) {
7702 for (size_t i = 0; i < posts_list->size; i++) {
7703 // For each MultiTargetNode, we're going to have one
7704 // additional anonymous local not represented in the locals table
7705 // We want to account for this in our table size
7706 pm_node_t *required = posts_list->nodes[i];
7707 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7708 table_size++;
7713 if (keywords_list && keywords_list->size) {
7714 for (size_t i = 0; i < keywords_list->size; i++) {
7715 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
7716 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7717 table_size++;
7722 if (parameters_node && parameters_node->block) {
7723 const pm_block_parameter_node_t *block_node = (const pm_block_parameter_node_t *) parameters_node->block;
7725 if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
7726 table_size++;
7730 // We can create local_table_for_iseq with the correct size
7731 VALUE idtmp = 0;
7732 rb_ast_id_table_t *local_table_for_iseq = ALLOCV(idtmp, sizeof(rb_ast_id_table_t) + table_size * sizeof(ID));
7733 local_table_for_iseq->size = table_size;
7735 //********END OF STEP 1**********
7737 //********STEP 2**********
7738 // Goal: populate iv index table as well as local table, keeping the
7739 // layout of the local table consistent with the layout of the
7740 // stack when calling the method
7742 // Do a first pass on all of the parameters, setting their values in
7743 // the local_table_for_iseq, _except_ for Multis who get a hidden
7744 // variable in this step, and will get their names inserted in step 3
7746 // local_index is a cursor that keeps track of the current
7747 // index into local_table_for_iseq. The local table is actually a list,
7748 // and the order of that list must match the order of the items pushed
7749 // on the stack. We need to take in to account things pushed on the
7750 // stack that _might not have a name_ (for example array destructuring).
7751 // This index helps us know which item we're dealing with and also give
7752 // those anonymous items temporary names (as below)
7753 int local_index = 0;
7755 // Here we figure out local table indices and insert them in to the
7756 // index lookup table and local tables.
7758 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7759 // ^^^^^^^^^^^^^
7760 if (requireds_list && requireds_list->size) {
7761 for (size_t i = 0; i < requireds_list->size; i++, local_index++) {
7762 ID local;
7764 // For each MultiTargetNode, we're going to have one additional
7765 // anonymous local not represented in the locals table. We want
7766 // to account for this in our table size.
7767 pm_node_t *required = requireds_list->nodes[i];
7769 switch (PM_NODE_TYPE(required)) {
7770 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7771 // ^^^^^^^^^^
7772 case PM_MULTI_TARGET_NODE: {
7773 local = rb_make_temporary_id(local_index);
7774 local_table_for_iseq->ids[local_index] = local;
7775 break;
7777 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7778 // ^
7779 case PM_REQUIRED_PARAMETER_NODE: {
7780 const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) required;
7782 if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7783 ID local = pm_constant_id_lookup(scope_node, param->name);
7784 local_table_for_iseq->ids[local_index] = local;
7786 else {
7787 pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
7790 break;
7792 default: {
7793 rb_bug("Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
7798 body->param.lead_num = (int) requireds_list->size;
7799 body->param.flags.has_lead = true;
7802 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7803 // ^^^^^
7804 if (optionals_list && optionals_list->size) {
7805 body->param.opt_num = (int) optionals_list->size;
7806 body->param.flags.has_opt = true;
7808 for (size_t i = 0; i < optionals_list->size; i++, local_index++) {
7809 pm_node_t * node = optionals_list->nodes[i];
7810 pm_constant_id_t name = ((const pm_optional_parameter_node_t *) node)->name;
7812 if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7813 ID local = pm_constant_id_lookup(scope_node, name);
7814 local_table_for_iseq->ids[local_index] = local;
7816 else {
7817 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
7822 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7823 // ^^
7824 if (parameters_node && parameters_node->rest) {
7825 body->param.rest_start = local_index;
7827 // If there's a trailing comma, we'll have an implicit rest node,
7828 // and we don't want it to impact the rest variables on param
7829 if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
7830 body->param.flags.has_rest = true;
7831 RUBY_ASSERT(body->param.rest_start != -1);
7833 pm_constant_id_t name = ((const pm_rest_parameter_node_t *) parameters_node->rest)->name;
7835 if (name) {
7836 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7837 // ^^
7838 if (PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7839 ID local = pm_constant_id_lookup(scope_node, name);
7840 local_table_for_iseq->ids[local_index] = local;
7842 else {
7843 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
7846 else {
7847 // def foo(a, (b, *c, d), e = 1, *, g, (h, *i, j), k:, l: 1, **m, &n)
7848 // ^
7849 pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
7852 local_index++;
7856 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7857 // ^^^^^^^^^^^^^
7858 if (posts_list && posts_list->size) {
7859 body->param.post_num = (int) posts_list->size;
7860 body->param.post_start = local_index;
7861 body->param.flags.has_post = true;
7863 for (size_t i = 0; i < posts_list->size; i++, local_index++) {
7864 ID local;
7866 // For each MultiTargetNode, we're going to have one additional
7867 // anonymous local not represented in the locals table. We want
7868 // to account for this in our table size.
7869 const pm_node_t *post_node = posts_list->nodes[i];
7871 switch (PM_NODE_TYPE(post_node)) {
7872 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7873 // ^^^^^^^^^^
7874 case PM_MULTI_TARGET_NODE: {
7875 local = rb_make_temporary_id(local_index);
7876 local_table_for_iseq->ids[local_index] = local;
7877 break;
7879 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7880 // ^
7881 case PM_REQUIRED_PARAMETER_NODE: {
7882 const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) post_node;
7884 if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7885 ID local = pm_constant_id_lookup(scope_node, param->name);
7886 local_table_for_iseq->ids[local_index] = local;
7888 else {
7889 pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
7891 break;
7893 default: {
7894 rb_bug("Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
7900 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7901 // ^^^^^^^^
7902 // Keywords create an internal variable on the parse tree
7903 if (keywords_list && keywords_list->size) {
7904 body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
7905 keyword->num = (int) keywords_list->size;
7907 body->param.flags.has_kw = true;
7908 const VALUE default_values = rb_ary_hidden_new(1);
7909 const VALUE complex_mark = rb_str_tmp_new(0);
7911 ID *ids = xcalloc(keywords_list->size, sizeof(ID));
7913 size_t kw_index = 0;
7915 for (size_t i = 0; i < keywords_list->size; i++) {
7916 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
7917 pm_constant_id_t name;
7919 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7920 // ^^
7921 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
7922 name = ((const pm_required_keyword_parameter_node_t *) keyword_parameter_node)->name;
7923 keyword->required_num++;
7924 ID local = pm_constant_id_lookup(scope_node, name);
7926 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7927 local_table_for_iseq->ids[local_index] = local;
7929 else {
7930 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
7932 local_index++;
7933 ids[kw_index++] = local;
7937 for (size_t i = 0; i < keywords_list->size; i++) {
7938 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
7939 pm_constant_id_t name;
7941 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7942 // ^^^^
7943 if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
7944 const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
7946 pm_node_t *value = cast->value;
7947 name = cast->name;
7949 if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !(PM_NODE_TYPE_P(value, PM_ARRAY_NODE) || PM_NODE_TYPE_P(value, PM_HASH_NODE) || PM_NODE_TYPE_P(value, PM_RANGE_NODE))) {
7950 rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
7952 else {
7953 rb_ary_push(default_values, complex_mark);
7956 ID local = pm_constant_id_lookup(scope_node, name);
7957 if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
7958 local_table_for_iseq->ids[local_index] = local;
7960 else {
7961 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
7963 ids[kw_index++] = local;
7964 local_index++;
7969 keyword->bits_start = local_index;
7970 keyword->table = ids;
7972 VALUE *dvs = ALLOC_N(VALUE, RARRAY_LEN(default_values));
7974 for (int i = 0; i < RARRAY_LEN(default_values); i++) {
7975 VALUE dv = RARRAY_AREF(default_values, i);
7976 if (dv == complex_mark) dv = Qundef;
7977 if (!SPECIAL_CONST_P(dv)) {
7978 RB_OBJ_WRITTEN(iseq, Qundef, dv);
7980 dvs[i] = dv;
7983 keyword->default_values = dvs;
7985 // Hidden local for keyword arguments
7986 ID local = rb_make_temporary_id(local_index);
7987 local_table_for_iseq->ids[local_index] = local;
7988 local_index++;
7991 if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->size == 1 && !trailing_comma) {
7992 body->param.flags.ambiguous_param0 = true;
7995 if (parameters_node) {
7996 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
7997 // ^^^
7998 if (parameters_node->keyword_rest) {
7999 switch (PM_NODE_TYPE(parameters_node->keyword_rest)) {
8000 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **nil, &n)
8001 // ^^^^^
8002 case PM_NO_KEYWORDS_PARAMETER_NODE: {
8003 body->param.flags.accepts_no_kwarg = true;
8004 break;
8006 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
8007 // ^^^
8008 case PM_KEYWORD_REST_PARAMETER_NODE: {
8009 const pm_keyword_rest_parameter_node_t *kw_rest_node = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
8010 if (!body->param.flags.has_kw) {
8011 body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
8014 keyword->rest_start = local_index;
8015 body->param.flags.has_kwrest = true;
8017 pm_constant_id_t constant_id = kw_rest_node->name;
8018 if (constant_id) {
8019 if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
8020 ID local = pm_constant_id_lookup(scope_node, constant_id);
8021 local_table_for_iseq->ids[local_index] = local;
8023 else {
8024 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
8027 else {
8028 pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
8031 local_index++;
8032 break;
8034 // def foo(...)
8035 // ^^^
8036 case PM_FORWARDING_PARAMETER_NODE: {
8037 body->param.rest_start = local_index;
8038 body->param.flags.has_rest = true;
8040 // Add the leading *
8041 pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
8043 // Add the kwrest **
8044 RUBY_ASSERT(!body->param.flags.has_kw);
8046 // There are no keywords declared (in the text of the program)
8047 // but the forwarding node implies we support kwrest (**)
8048 body->param.flags.has_kw = false;
8049 body->param.flags.has_kwrest = true;
8050 body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
8052 keyword->rest_start = local_index;
8054 pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
8056 body->param.block_start = local_index;
8057 body->param.flags.has_block = true;
8059 pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
8060 pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
8061 break;
8063 default: {
8064 rb_bug("node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->keyword_rest)));
8069 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
8070 // ^^
8071 if (parameters_node->block) {
8072 body->param.block_start = local_index;
8073 body->param.flags.has_block = true;
8075 pm_constant_id_t name = ((const pm_block_parameter_node_t *) parameters_node->block)->name;
8077 if (name) {
8078 if (PM_NODE_FLAG_P(parameters_node->block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
8079 ID local = pm_constant_id_lookup(scope_node, name);
8080 local_table_for_iseq->ids[local_index] = local;
8082 else {
8083 pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
8086 else {
8087 pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
8090 local_index++;
8094 //********END OF STEP 2**********
8095 // The local table is now consistent with expected
8096 // stack layout
8098 // If there's only one required element in the parameters
8099 // CRuby needs to recognize it as an ambiguous parameter
8101 //********STEP 3**********
8102 // Goal: fill in the names of the parameters in MultiTargetNodes
8104 // Go through requireds again to set the multis
8106 if (requireds_list && requireds_list->size) {
8107 for (size_t i = 0; i < requireds_list->size; i++) {
8108 // For each MultiTargetNode, we're going to have one
8109 // additional anonymous local not represented in the locals table
8110 // We want to account for this in our table size
8111 const pm_node_t *required = requireds_list->nodes[i];
8113 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
8114 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);
8119 // Go through posts again to set the multis
8120 if (posts_list && posts_list->size) {
8121 for (size_t i = 0; i < posts_list->size; i++) {
8122 // For each MultiTargetNode, we're going to have one
8123 // additional anonymous local not represented in the locals table
8124 // We want to account for this in our table size
8125 const pm_node_t *post = posts_list->nodes[i];
8127 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
8128 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);
8133 // Set any anonymous locals for the for node
8134 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
8135 if (PM_NODE_TYPE_P(((const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
8136 body->param.lead_num++;
8138 else {
8139 body->param.rest_start = local_index;
8140 body->param.flags.has_rest = true;
8143 ID local = rb_make_temporary_id(local_index);
8144 local_table_for_iseq->ids[local_index] = local;
8145 local_index++;
8148 // Fill in any NumberedParameters, if they exist
8149 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
8150 int maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
8151 RUBY_ASSERT(0 < maximum && maximum <= 9);
8152 for (int i = 0; i < maximum; i++, local_index++) {
8153 const uint8_t param_name[] = { '_', '1' + i };
8154 pm_constant_id_t constant_id = pm_constant_pool_find(&parser->constant_pool, param_name, 2);
8155 RUBY_ASSERT(constant_id && "parser should fill in any gaps in numbered parameters");
8156 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
8158 body->param.lead_num = maximum;
8159 body->param.flags.has_lead = true;
8162 // Fill in the it variable, if it exists
8163 if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
8164 const uint8_t param_name[] = { '0', 'i', 't' };
8165 pm_constant_id_t constant_id = pm_constant_pool_find(&parser->constant_pool, param_name, 3);
8166 RUBY_ASSERT(constant_id && "parser should have inserted 0it for 'it' local");
8168 ID local = rb_make_temporary_id(local_index);
8169 local_table_for_iseq->ids[local_index] = local;
8170 st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
8171 local_index++;
8174 //********END OF STEP 3**********
8176 //********STEP 4**********
8177 // Goal: fill in the method body locals
8178 // To be explicit, these are the non-parameter locals
8179 // We fill in the block_locals, if they exist
8180 // lambda { |x; y| y }
8181 // ^
8182 if (block_locals && block_locals->size) {
8183 for (size_t i = 0; i < block_locals->size; i++, local_index++) {
8184 pm_constant_id_t constant_id = ((const pm_block_local_variable_node_t *) block_locals->nodes[i])->name;
8185 pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
8189 // Fill in any locals we missed
8190 if (scope_node->locals.size) {
8191 for (size_t i = 0; i < scope_node->locals.size; i++) {
8192 pm_constant_id_t constant_id = locals->ids[i];
8193 if (constant_id) {
8194 struct pm_local_table_insert_ctx ctx;
8195 ctx.scope_node = scope_node;
8196 ctx.local_table_for_iseq = local_table_for_iseq;
8197 ctx.local_index = local_index;
8199 st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
8201 local_index = ctx.local_index;
8206 //********END OF STEP 4**********
8208 // We set the index_lookup_table on the scope node so we can
8209 // refer to the parameters correctly
8210 if (scope_node->index_lookup_table) {
8211 st_free_table(scope_node->index_lookup_table);
8213 scope_node->index_lookup_table = index_lookup_table;
8214 iseq_calc_param_size(iseq);
8215 iseq_set_local_table(iseq, local_table_for_iseq);
8216 scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
8218 //********STEP 5************
8219 // Goal: compile anything that needed to be compiled
8220 if (optionals_list && optionals_list->size) {
8221 LABEL **opt_table = (LABEL **) ALLOC_N(VALUE, optionals_list->size + 1);
8222 LABEL *label;
8224 // TODO: Should we make an api for NEW_LABEL where you can pass
8225 // a pointer to the label it should fill out? We already
8226 // have a list of labels allocated above so it seems wasteful
8227 // to do the copies.
8228 for (size_t i = 0; i < optionals_list->size; i++) {
8229 label = NEW_LABEL(lineno);
8230 opt_table[i] = label;
8231 PUSH_LABEL(ret, label);
8232 pm_node_t *optional_node = optionals_list->nodes[i];
8233 PM_COMPILE_NOT_POPPED(optional_node);
8236 // Set the last label
8237 label = NEW_LABEL(lineno);
8238 opt_table[optionals_list->size] = label;
8239 PUSH_LABEL(ret, label);
8241 body->param.opt_table = (const VALUE *) opt_table;
8244 if (keywords_list && keywords_list->size) {
8245 size_t optional_index = 0;
8246 for (size_t i = 0; i < keywords_list->size; i++) {
8247 pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
8248 pm_constant_id_t name;
8250 switch (PM_NODE_TYPE(keyword_parameter_node)) {
8251 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
8252 // ^^^^
8253 case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
8254 const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
8256 pm_node_t *value = cast->value;
8257 name = cast->name;
8259 if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_NODE_TYPE_P(value, PM_ARRAY_NODE) || PM_NODE_TYPE_P(value, PM_HASH_NODE) || PM_NODE_TYPE_P(value, PM_RANGE_NODE)) {
8260 LABEL *end_label = NEW_LABEL(location.line);
8262 pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
8263 int kw_bits_idx = table_size - body->param.keyword->bits_start;
8264 PUSH_INSN2(ret, location, checkkeyword, INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1), INT2FIX(optional_index));
8265 PUSH_INSNL(ret, location, branchif, end_label);
8266 PM_COMPILE(value);
8267 PUSH_SETLOCAL(ret, location, index.index, index.level);
8268 PUSH_LABEL(ret, end_label);
8270 optional_index++;
8271 break;
8273 // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
8274 // ^^
8275 case PM_REQUIRED_KEYWORD_PARAMETER_NODE: {
8276 break;
8278 default: {
8279 rb_bug("Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
8285 if (requireds_list && requireds_list->size) {
8286 for (size_t i = 0; i < requireds_list->size; i++) {
8287 // For each MultiTargetNode, we're going to have one additional
8288 // anonymous local not represented in the locals table. We want
8289 // to account for this in our table size.
8290 const pm_node_t *required = requireds_list->nodes[i];
8292 if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
8293 PUSH_GETLOCAL(ret, location, table_size - (int)i, 0);
8294 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) required, ret, scope_node);
8299 if (posts_list && posts_list->size) {
8300 for (size_t i = 0; i < posts_list->size; i++) {
8301 // For each MultiTargetNode, we're going to have one additional
8302 // anonymous local not represented in the locals table. We want
8303 // to account for this in our table size.
8304 const pm_node_t *post = posts_list->nodes[i];
8306 if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
8307 PUSH_GETLOCAL(ret, location, table_size - body->param.post_start - (int) i, 0);
8308 pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) post, ret, scope_node);
8313 switch (body->type) {
8314 case ISEQ_TYPE_BLOCK: {
8315 LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
8316 LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
8317 const pm_line_column_t block_location = { .line = body->location.first_lineno, .column = -1 };
8319 start->rescued = LABEL_RESCUE_BEG;
8320 end->rescued = LABEL_RESCUE_END;
8322 // For nodes automatically assign the iteration variable to whatever
8323 // index variable. We need to handle that write here because it has
8324 // to happen in the context of the block. Note that this happens
8325 // before the B_CALL tracepoint event.
8326 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
8327 pm_compile_for_node_index(iseq, ((const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
8330 PUSH_TRACE(ret, RUBY_EVENT_B_CALL);
8331 PUSH_INSN(ret, block_location, nop);
8332 PUSH_LABEL(ret, start);
8334 if (scope_node->body != NULL) {
8335 switch (PM_NODE_TYPE(scope_node->ast_node)) {
8336 case PM_POST_EXECUTION_NODE: {
8337 const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) scope_node->ast_node;
8338 PUSH_INSN1(ret, block_location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8340 // We create another ScopeNode from the statements within the PostExecutionNode
8341 pm_scope_node_t next_scope_node;
8342 pm_scope_node_init((const pm_node_t *) cast->statements, &next_scope_node, scope_node);
8344 const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.line);
8345 pm_scope_node_destroy(&next_scope_node);
8347 PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe, INT2FIX(0), block);
8348 break;
8350 case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
8351 const pm_interpolated_regular_expression_node_t *cast = (const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
8352 pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
8353 break;
8355 default:
8356 pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
8357 break;
8360 else {
8361 PUSH_INSN(ret, block_location, putnil);
8364 PUSH_LABEL(ret, end);
8365 PUSH_TRACE(ret, RUBY_EVENT_B_RETURN);
8366 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
8368 /* wide range catch handler must put at last */
8369 PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
8370 PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
8371 break;
8373 case ISEQ_TYPE_ENSURE: {
8374 const pm_line_column_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LINE_COLUMN(scope_node->parser, scope_node->body) : location);
8375 iseq_set_exception_local_table(iseq);
8377 if (scope_node->body != NULL) {
8378 PM_COMPILE_POPPED((const pm_node_t *) scope_node->body);
8381 PUSH_GETLOCAL(ret, statements_location, 1, 0);
8382 PUSH_INSN1(ret, statements_location, throw, INT2FIX(0));
8383 return;
8385 case ISEQ_TYPE_METHOD: {
8386 PUSH_TRACE(ret, RUBY_EVENT_CALL);
8387 if (scope_node->body) {
8388 PM_COMPILE((const pm_node_t *) scope_node->body);
8390 else {
8391 PUSH_INSN(ret, location, putnil);
8394 PUSH_TRACE(ret, RUBY_EVENT_RETURN);
8395 ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
8397 break;
8399 case ISEQ_TYPE_RESCUE: {
8400 iseq_set_exception_local_table(iseq);
8401 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
8402 LABEL *lab = NEW_LABEL(lineno);
8403 LABEL *rescue_end = NEW_LABEL(lineno);
8404 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
8405 PUSH_INSN1(ret, location, putobject, rb_eStandardError);
8406 PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
8407 PUSH_INSNL(ret, location, branchif, lab);
8408 PUSH_INSNL(ret, location, jump, rescue_end);
8409 PUSH_LABEL(ret, lab);
8410 PM_COMPILE((const pm_node_t *) scope_node->body);
8411 PUSH_INSN(ret, location, leave);
8412 PUSH_LABEL(ret, rescue_end);
8413 PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
8415 else {
8416 PM_COMPILE((const pm_node_t *) scope_node->ast_node);
8418 PUSH_INSN1(ret, location, throw, INT2FIX(0));
8420 return;
8422 default:
8423 if (scope_node->body) {
8424 PM_COMPILE((const pm_node_t *) scope_node->body);
8426 else {
8427 PUSH_INSN(ret, location, putnil);
8429 break;
8432 if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE)) {
8433 const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, scope_node->ast_node);
8434 ADD_TRACE(ret, RUBY_EVENT_END);
8435 ISEQ_COMPILE_DATA(iseq)->last_line = end_location.line;
8438 if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
8439 const pm_line_column_t location = { .line = ISEQ_COMPILE_DATA(iseq)->last_line, .column = -1 };
8440 PUSH_INSN(ret, location, leave);
8443 return;
8445 case PM_SELF_NODE: {
8446 // self
8447 // ^^^^
8448 if (!popped) {
8449 PUSH_INSN(ret, location, putself);
8451 return;
8453 case PM_SHAREABLE_CONSTANT_NODE: {
8454 // A value that is being written to a constant that is being marked as
8455 // shared depending on the current lexical context.
8456 PM_COMPILE(((const pm_shareable_constant_node_t *) node)->write);
8457 return;
8459 case PM_SINGLETON_CLASS_NODE: {
8460 // class << self; end
8461 // ^^^^^^^^^^^^^^^^^^
8462 const pm_singleton_class_node_t *cast = (const pm_singleton_class_node_t *) node;
8464 pm_scope_node_t next_scope_node;
8465 pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node);
8466 const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit("singleton class"), ISEQ_TYPE_CLASS, location.line);
8467 pm_scope_node_destroy(&next_scope_node);
8469 PM_COMPILE_NOT_POPPED(cast->expression);
8470 PUSH_INSN(ret, location, putnil);
8472 ID singletonclass;
8473 CONST_ID(singletonclass, "singletonclass");
8474 PUSH_INSN3(ret, location, defineclass, ID2SYM(singletonclass), child_iseq, INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
8476 if (popped) PUSH_INSN(ret, location, pop);
8477 RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
8479 return;
8481 case PM_SOURCE_ENCODING_NODE: {
8482 // __ENCODING__
8483 // ^^^^^^^^^^^^
8484 if (!popped) {
8485 VALUE value = pm_static_literal_value(iseq, node, scope_node);
8486 PUSH_INSN1(ret, location, putobject, value);
8488 return;
8490 case PM_SOURCE_FILE_NODE: {
8491 // __FILE__
8492 // ^^^^^^^^
8493 if (!popped) {
8494 const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
8495 VALUE string = pm_source_file_value(cast, scope_node);
8497 if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_FROZEN)) {
8498 PUSH_INSN1(ret, location, putobject, string);
8500 else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
8501 PUSH_INSN1(ret, location, putstring, string);
8503 else {
8504 PUSH_INSN1(ret, location, putchilledstring, string);
8507 return;
8509 case PM_SOURCE_LINE_NODE: {
8510 // __LINE__
8511 // ^^^^^^^^
8512 if (!popped) {
8513 VALUE value = pm_static_literal_value(iseq, node, scope_node);
8514 PUSH_INSN1(ret, location, putobject, value);
8516 return;
8518 case PM_SPLAT_NODE: {
8519 // foo(*bar)
8520 // ^^^^
8521 const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
8522 if (cast->expression) {
8523 PM_COMPILE(cast->expression);
8526 if (!popped) {
8527 PUSH_INSN1(ret, location, splatarray, Qtrue);
8529 return;
8531 case PM_STATEMENTS_NODE: {
8532 // A list of statements.
8533 const pm_statements_node_t *cast = (const pm_statements_node_t *) node;
8534 const pm_node_list_t *body = &cast->body;
8536 if (body->size > 0) {
8537 for (size_t index = 0; index < body->size - 1; index++) {
8538 PM_COMPILE_POPPED(body->nodes[index]);
8540 PM_COMPILE(body->nodes[body->size - 1]);
8542 else {
8543 PUSH_INSN(ret, location, putnil);
8545 return;
8547 case PM_STRING_NODE: {
8548 // "foo"
8549 // ^^^^^
8550 if (!popped) {
8551 const pm_string_node_t *cast = (const pm_string_node_t *) node;
8552 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
8554 if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FROZEN)) {
8555 PUSH_INSN1(ret, location, putobject, value);
8557 else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
8558 PUSH_INSN1(ret, location, putstring, value);
8560 else {
8561 PUSH_INSN1(ret, location, putchilledstring, value);
8564 return;
8566 case PM_SUPER_NODE: {
8567 // super(foo)
8568 // ^^^^^^^^^^
8569 const pm_super_node_t *cast = (const pm_super_node_t *) node;
8571 DECL_ANCHOR(args);
8572 INIT_ANCHOR(args);
8574 LABEL *retry_label = NEW_LABEL(location.line);
8575 LABEL *retry_end_l = NEW_LABEL(location.line);
8577 const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
8578 const rb_iseq_t *current_block;
8579 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
8581 PUSH_LABEL(ret, retry_label);
8582 PUSH_INSN(ret, location, putself);
8584 int flags = 0;
8585 struct rb_callinfo_kwarg *keywords = NULL;
8586 int argc = pm_setup_args(cast->arguments, cast->block, &flags, &keywords, iseq, ret, scope_node, &location);
8587 flags |= VM_CALL_SUPER | VM_CALL_FCALL;
8589 if (cast->block && PM_NODE_TYPE_P(cast->block, PM_BLOCK_NODE)) {
8590 pm_scope_node_t next_scope_node;
8591 pm_scope_node_init(cast->block, &next_scope_node, scope_node);
8593 ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
8594 pm_scope_node_destroy(&next_scope_node);
8597 if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
8598 PUSH_INSN(args, location, splatkw);
8601 PUSH_SEQ(ret, args);
8602 PUSH_INSN2(ret, location, invokesuper, new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL), current_block);
8603 pm_compile_retry_end_label(iseq, ret, retry_end_l);
8605 if (popped) PUSH_INSN(ret, location, pop);
8606 ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
8607 PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
8609 return;
8611 case PM_SYMBOL_NODE: {
8612 // :foo
8613 // ^^^^
8614 if (!popped) {
8615 VALUE value = pm_static_literal_value(iseq, node, scope_node);
8616 PUSH_INSN1(ret, location, putobject, value);
8618 return;
8620 case PM_TRUE_NODE: {
8621 // true
8622 // ^^^^
8623 if (!popped) {
8624 PUSH_INSN1(ret, location, putobject, Qtrue);
8626 return;
8628 case PM_UNDEF_NODE: {
8629 // undef foo
8630 // ^^^^^^^^^
8631 const pm_undef_node_t *cast = (const pm_undef_node_t *) node;
8632 const pm_node_list_t *names = &cast->names;
8634 for (size_t index = 0; index < names->size; index++) {
8635 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
8636 PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
8638 PM_COMPILE_NOT_POPPED(names->nodes[index]);
8639 PUSH_SEND(ret, location, id_core_undef_method, INT2NUM(2));
8641 if (index < names->size - 1) {
8642 PUSH_INSN(ret, location, pop);
8646 if (popped) PUSH_INSN(ret, location, pop);
8647 return;
8649 case PM_UNLESS_NODE: {
8650 // unless foo; bar end
8651 // ^^^^^^^^^^^^^^^^^^^
8653 // bar unless foo
8654 // ^^^^^^^^^^^^^^
8655 const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
8656 const pm_statements_node_t *consequent = NULL;
8657 if (cast->consequent != NULL) {
8658 consequent = ((const pm_else_node_t *) cast->consequent)->statements;
8661 pm_compile_conditional(iseq, &location, PM_UNLESS_NODE, (const pm_node_t *) cast, consequent, (const pm_node_t *) cast->statements, cast->predicate, ret, popped, scope_node);
8662 return;
8664 case PM_UNTIL_NODE: {
8665 // until foo; bar end
8666 // ^^^^^^^^^^^^^^^^^
8668 // bar until foo
8669 // ^^^^^^^^^^^^^
8670 const pm_until_node_t *cast = (const pm_until_node_t *) node;
8671 pm_compile_loop(iseq, &location, cast->base.flags, PM_UNTIL_NODE, (const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
8672 return;
8674 case PM_WHILE_NODE: {
8675 // while foo; bar end
8676 // ^^^^^^^^^^^^^^^^^^
8678 // bar while foo
8679 // ^^^^^^^^^^^^^
8680 const pm_while_node_t *cast = (const pm_while_node_t *) node;
8681 pm_compile_loop(iseq, &location, cast->base.flags, PM_WHILE_NODE, (const pm_node_t *) cast, cast->statements, cast->predicate, ret, popped, scope_node);
8682 return;
8684 case PM_X_STRING_NODE: {
8685 // `foo`
8686 // ^^^^^
8687 const pm_x_string_node_t *cast = (const pm_x_string_node_t *) node;
8688 VALUE value = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
8690 PUSH_INSN(ret, location, putself);
8691 PUSH_INSN1(ret, location, putobject, value);
8692 PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
8693 if (popped) PUSH_INSN(ret, location, pop);
8695 return;
8697 case PM_YIELD_NODE: {
8698 // yield
8699 // ^^^^^
8701 // yield 1
8702 // ^^^^^^^
8703 const pm_yield_node_t *cast = (const pm_yield_node_t *) node;
8705 switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->type) {
8706 case ISEQ_TYPE_TOP:
8707 case ISEQ_TYPE_MAIN:
8708 case ISEQ_TYPE_CLASS:
8709 COMPILE_ERROR(ERROR_ARGS "Invalid yield");
8710 return;
8711 default: /* valid */;
8714 int argc = 0;
8715 int flags = 0;
8716 struct rb_callinfo_kwarg *keywords = NULL;
8718 if (cast->arguments) {
8719 argc = pm_setup_args(cast->arguments, NULL, &flags, &keywords, iseq, ret, scope_node, &location);
8722 PUSH_INSN1(ret, location, invokeblock, new_callinfo(iseq, 0, argc, flags, keywords, FALSE));
8723 if (popped) PUSH_INSN(ret, location, pop);
8725 int level = 0;
8726 for (const rb_iseq_t *tmp_iseq = iseq; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++) {
8727 tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
8730 if (level > 0) access_outer_variables(iseq, level, rb_intern("yield"), true);
8731 return;
8733 default: {
8734 rb_raise(rb_eNotImpError, "node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
8735 return;
8740 /** True if the given iseq can have pre execution blocks. */
8741 static inline bool
8742 pm_iseq_pre_execution_p(rb_iseq_t *iseq)
8744 switch (ISEQ_BODY(iseq)->type) {
8745 case ISEQ_TYPE_TOP:
8746 case ISEQ_TYPE_EVAL:
8747 case ISEQ_TYPE_MAIN:
8748 return true;
8749 default:
8750 return false;
8755 * This is the main entry-point into the prism compiler. It accepts the iseq
8756 * that it should be compiling instruction into and a pointer to the scope node
8757 * that it should be compiling. It returns the established instruction sequence.
8758 * Note that this function could raise Ruby errors if it encounters compilation
8759 * errors or if there is a bug in the compiler.
8761 VALUE
8762 pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
8764 DECL_ANCHOR(ret);
8765 INIT_ANCHOR(ret);
8767 if (pm_iseq_pre_execution_p(iseq)) {
8768 // Because these ISEQs can have BEGIN{}, we're going to create two
8769 // anchors to compile them, a "pre" and a "body". We'll mark the "pre"
8770 // on the scope node so that when BEGIN{} is found, its contents will be
8771 // added to the "pre" anchor.
8772 DECL_ANCHOR(pre);
8773 INIT_ANCHOR(pre);
8774 node->pre_execution_anchor = pre;
8776 // Now we'll compile the body as normal. We won't compile directly into
8777 // the "ret" anchor yet because we want to add the "pre" anchor to the
8778 // beginning of the "ret" anchor first.
8779 DECL_ANCHOR(body);
8780 INIT_ANCHOR(body);
8781 pm_compile_node(iseq, (const pm_node_t *) node, body, false, node);
8783 // Now we'll join both anchors together so that the content is in the
8784 // correct order.
8785 PUSH_SEQ(ret, pre);
8786 PUSH_SEQ(ret, body);
8788 else {
8789 // In other circumstances, we can just compile the node directly into
8790 // the "ret" anchor.
8791 pm_compile_node(iseq, (const pm_node_t *) node, ret, false, node);
8794 CHECK(iseq_setup_insn(iseq, ret));
8795 return iseq_setup(iseq, ret);
8799 * Free the internal memory associated with a pm_parse_result_t struct.
8800 * Importantly this does not free the struct itself.
8802 void
8803 pm_parse_result_free(pm_parse_result_t *result)
8805 if (result->parsed) {
8806 pm_node_destroy(&result->parser, result->node.ast_node);
8807 pm_scope_node_destroy(&result->node);
8810 pm_parser_free(&result->parser);
8811 pm_string_free(&result->input);
8812 pm_options_free(&result->options);
8816 * Check if the given source slice is valid UTF-8. The location represents the
8817 * location of the error, but the slice of the source will include the content
8818 * of all of the lines that the error touches, so we need to check those parts
8819 * as well.
8821 static bool
8822 pm_parse_process_error_utf8_p(const pm_parser_t *parser, const pm_location_t *location)
8824 const size_t start_line = pm_newline_list_line_column(&parser->newline_list, location->start, 1).line;
8825 const size_t end_line = pm_newline_list_line_column(&parser->newline_list, location->end, 1).line;
8827 const uint8_t *start = parser->start + parser->newline_list.offsets[start_line - 1];
8828 const uint8_t *end = ((end_line == parser->newline_list.size) ? parser->end : (parser->start + parser->newline_list.offsets[end_line]));
8829 size_t width;
8831 while (start < end) {
8832 if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0) return false;
8833 start += width;
8836 return true;
8840 * Generate an error object from the given parser that contains as much
8841 * information as possible about the errors that were encountered.
8843 static VALUE
8844 pm_parse_process_error(const pm_parse_result_t *result)
8846 const pm_parser_t *parser = &result->parser;
8847 const pm_diagnostic_t *head = (const pm_diagnostic_t *) parser->error_list.head;
8848 bool valid_utf8 = true;
8850 pm_buffer_t buffer = { 0 };
8851 const pm_string_t *filepath = &parser->filepath;
8853 for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
8854 switch (error->level) {
8855 case PM_ERROR_LEVEL_SYNTAX:
8856 // It is implicitly assumed that the error messages will be
8857 // encodeable as UTF-8. Because of this, we can't include source
8858 // examples that contain invalid byte sequences. So if any source
8859 // examples include invalid UTF-8 byte sequences, we will skip
8860 // showing source examples entirely.
8861 if (valid_utf8 && !pm_parse_process_error_utf8_p(parser, &error->location)) {
8862 valid_utf8 = false;
8864 break;
8865 case PM_ERROR_LEVEL_ARGUMENT: {
8866 // Any errors with the level PM_ERROR_LEVEL_ARGUMENT take over as
8867 // the only argument that gets raised. This is to allow priority
8868 // messages that should be handled before anything else.
8869 int32_t line_number = (int32_t) pm_location_line_number(parser, &error->location);
8871 pm_buffer_append_format(
8872 &buffer,
8873 "%.*s:%" PRIi32 ": %s",
8874 (int) pm_string_length(filepath),
8875 pm_string_source(filepath),
8876 line_number,
8877 error->message
8880 if (pm_parse_process_error_utf8_p(parser, &error->location)) {
8881 pm_buffer_append_byte(&buffer, '\n');
8883 pm_list_node_t *list_node = (pm_list_node_t *) error;
8884 pm_list_t error_list = { .size = 1, .head = list_node, .tail = list_node };
8886 pm_parser_errors_format(parser, &error_list, &buffer, rb_stderr_tty_p(), false);
8889 VALUE value = rb_exc_new(rb_eArgError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
8890 pm_buffer_free(&buffer);
8892 return value;
8894 case PM_ERROR_LEVEL_LOAD: {
8895 // Load errors are much simpler, because they don't include any of
8896 // the source in them. We create the error directly from the
8897 // message.
8898 VALUE message = rb_enc_str_new_cstr(error->message, rb_locale_encoding());
8899 VALUE value = rb_exc_new3(rb_eLoadError, message);
8900 rb_ivar_set(value, rb_intern_const("@path"), Qnil);
8901 return value;
8906 pm_buffer_append_format(
8907 &buffer,
8908 "%.*s:%" PRIi32 ": syntax error%s found\n",
8909 (int) pm_string_length(filepath),
8910 pm_string_source(filepath),
8911 (int32_t) pm_location_line_number(parser, &head->location),
8912 (parser->error_list.size > 1) ? "s" : ""
8915 if (valid_utf8) {
8916 pm_parser_errors_format(parser, &parser->error_list, &buffer, rb_stderr_tty_p(), true);
8918 else {
8919 for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
8920 if (error != head) pm_buffer_append_byte(&buffer, '\n');
8921 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);
8925 VALUE error = rb_exc_new(rb_eSyntaxError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
8927 rb_encoding *filepath_encoding = result->node.filepath_encoding != NULL ? result->node.filepath_encoding : rb_utf8_encoding();
8928 VALUE path = rb_enc_str_new((const char *) pm_string_source(filepath), pm_string_length(filepath), filepath_encoding);
8930 rb_ivar_set(error, rb_intern_const("@path"), path);
8931 pm_buffer_free(&buffer);
8933 return error;
8937 * Parse the parse result and raise a Ruby error if there are any syntax errors.
8938 * It returns an error if one should be raised. It is assumed that the parse
8939 * result object is zeroed out.
8941 static VALUE
8942 pm_parse_process(pm_parse_result_t *result, pm_node_t *node)
8944 pm_parser_t *parser = &result->parser;
8946 // First, set up the scope node so that the AST node is attached and can be
8947 // freed regardless of whether or we return an error.
8948 pm_scope_node_t *scope_node = &result->node;
8949 rb_encoding *filepath_encoding = scope_node->filepath_encoding;
8951 pm_scope_node_init(node, scope_node, NULL);
8952 scope_node->filepath_encoding = filepath_encoding;
8954 // Emit all of the various warnings from the parse.
8955 const pm_diagnostic_t *warning;
8956 const char *warning_filepath = (const char *) pm_string_source(&parser->filepath);
8958 for (warning = (const pm_diagnostic_t *) parser->warning_list.head; warning != NULL; warning = (const pm_diagnostic_t *) warning->node.next) {
8959 int line = pm_location_line_number(parser, &warning->location);
8961 if (warning->level == PM_WARNING_LEVEL_VERBOSE) {
8962 rb_compile_warning(warning_filepath, line, "%s", warning->message);
8964 else {
8965 rb_compile_warn(warning_filepath, line, "%s", warning->message);
8969 // If there are errors, raise an appropriate error and free the result.
8970 if (parser->error_list.size > 0) {
8971 VALUE error = pm_parse_process_error(result);
8973 // TODO: We need to set the backtrace.
8974 // rb_funcallv(error, rb_intern("set_backtrace"), 1, &path);
8975 return error;
8978 // Now set up the constant pool and intern all of the various constants into
8979 // their corresponding IDs.
8980 scope_node->encoding = rb_enc_find(parser->encoding->name);
8981 if (!scope_node->encoding) rb_bug("Encoding not found %s!", parser->encoding->name);
8983 scope_node->parser = parser;
8984 scope_node->constants = calloc(parser->constant_pool.size, sizeof(ID));
8986 for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
8987 pm_constant_t *constant = &parser->constant_pool.constants[index];
8988 scope_node->constants[index] = rb_intern3((const char *) constant->start, constant->length, scope_node->encoding);
8991 scope_node->index_lookup_table = st_init_numtable();
8992 pm_constant_id_list_t *locals = &scope_node->locals;
8993 for (size_t index = 0; index < locals->size; index++) {
8994 st_insert(scope_node->index_lookup_table, locals->ids[index], index);
8997 // If we got here, this is a success and we can return Qnil to indicate that
8998 // no error should be raised.
8999 result->parsed = true;
9000 return Qnil;
9004 * Set the frozen_string_literal option based on the default value used by the
9005 * CRuby compiler.
9007 static void
9008 pm_options_frozen_string_literal_init(pm_options_t *options)
9010 int frozen_string_literal = rb_iseq_opt_frozen_string_literal();
9012 switch (frozen_string_literal) {
9013 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
9014 break;
9015 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
9016 pm_options_frozen_string_literal_set(options, false);
9017 break;
9018 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
9019 pm_options_frozen_string_literal_set(options, true);
9020 break;
9021 default:
9022 rb_bug("pm_options_frozen_string_literal_init: invalid frozen_string_literal=%d", frozen_string_literal);
9023 break;
9028 * Returns an array of ruby String objects that represent the lines of the
9029 * source file that the given parser parsed.
9031 static inline VALUE
9032 pm_parse_file_script_lines(const pm_scope_node_t *scope_node, const pm_parser_t *parser)
9034 const pm_newline_list_t *newline_list = &parser->newline_list;
9035 const char *start = (const char *) parser->start;
9036 const char *end = (const char *) parser->end;
9038 // If we end exactly on a newline, then there's no need to push on a final
9039 // segment. If we don't, then we need to push on the last offset up to the
9040 // end of the string.
9041 size_t last_offset = newline_list->offsets[newline_list->size - 1];
9042 bool last_push = start + last_offset != end;
9044 // Create the ruby strings that represent the lines of the source.
9045 VALUE lines = rb_ary_new_capa(newline_list->size - (last_push ? 0 : 1));
9047 for (size_t index = 0; index < newline_list->size - 1; index++) {
9048 size_t offset = newline_list->offsets[index];
9049 size_t length = newline_list->offsets[index + 1] - offset;
9051 rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
9054 // Push on the last line if we need to.
9055 if (last_push) {
9056 rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), scope_node->encoding));
9059 return lines;
9063 * Attempt to load the file into memory. Return a Ruby error if the file cannot
9064 * be read.
9066 VALUE
9067 pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error)
9069 if (!pm_string_mapped_init(&result->input, RSTRING_PTR(filepath))) {
9070 #ifdef _WIN32
9071 int e = rb_w32_map_errno(GetLastError());
9072 #else
9073 int e = errno;
9074 #endif
9076 VALUE error;
9078 if (load_error) {
9079 VALUE message = rb_str_buf_new_cstr(strerror(e));
9080 rb_str_cat2(message, " -- ");
9081 rb_str_append(message, filepath);
9083 error = rb_exc_new3(rb_eLoadError, message);
9084 rb_ivar_set(error, rb_intern_const("@path"), filepath);
9085 } else {
9086 error = rb_syserr_new(e, RSTRING_PTR(filepath));
9087 RB_GC_GUARD(filepath);
9090 return error;
9093 pm_options_frozen_string_literal_init(&result->options);
9094 return Qnil;
9098 * Parse the given filepath and store the resulting scope node in the given
9099 * parse result struct. It returns a Ruby error if the file cannot be read or
9100 * if it cannot be parsed properly. It is assumed that the parse result object
9101 * is zeroed out.
9103 VALUE
9104 pm_parse_file(pm_parse_result_t *result, VALUE filepath)
9106 pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
9107 RB_GC_GUARD(filepath);
9109 pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
9110 pm_node_t *node = pm_parse(&result->parser);
9112 VALUE error = pm_parse_process(result, node);
9114 // If we're parsing a filepath, then we need to potentially support the
9115 // SCRIPT_LINES__ constant, which can be a hash that has an array of lines
9116 // of every read file.
9117 ID id_script_lines = rb_intern("SCRIPT_LINES__");
9119 if (rb_const_defined_at(rb_cObject, id_script_lines)) {
9120 VALUE script_lines = rb_const_get_at(rb_cObject, id_script_lines);
9122 if (RB_TYPE_P(script_lines, T_HASH)) {
9123 rb_hash_aset(script_lines, filepath, pm_parse_file_script_lines(&result->node, &result->parser));
9127 return error;
9131 * Load and then parse the given filepath. It returns a Ruby error if the file
9132 * cannot be read or if it cannot be parsed properly.
9134 VALUE
9135 pm_load_parse_file(pm_parse_result_t *result, VALUE filepath)
9137 VALUE error = pm_load_file(result, filepath, false);
9138 if (NIL_P(error)) {
9139 error = pm_parse_file(result, filepath);
9142 return error;
9146 * Parse the given source that corresponds to the given filepath and store the
9147 * resulting scope node in the given parse result struct. It is assumed that the
9148 * parse result object is zeroed out. If the string fails to parse, then a Ruby
9149 * error is returned.
9151 VALUE
9152 pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath)
9154 rb_encoding *encoding = rb_enc_get(source);
9155 if (!rb_enc_asciicompat(encoding)) {
9156 return rb_exc_new_cstr(rb_eArgError, "invalid source encoding");
9159 pm_options_frozen_string_literal_init(&result->options);
9160 pm_string_constant_init(&result->input, RSTRING_PTR(source), RSTRING_LEN(source));
9161 pm_options_encoding_set(&result->options, rb_enc_name(encoding));
9163 result->node.filepath_encoding = rb_enc_get(filepath);
9164 pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
9165 RB_GC_GUARD(filepath);
9167 pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
9168 pm_node_t *node = pm_parse(&result->parser);
9170 return pm_parse_process(result, node);
9174 * An implementation of fgets that is suitable for use with Ruby IO objects.
9176 static char *
9177 pm_parse_stdin_fgets(char *string, int size, void *stream)
9179 RUBY_ASSERT(size > 0);
9181 VALUE line = rb_funcall((VALUE) stream, rb_intern("gets"), 1, INT2FIX(size - 1));
9182 if (NIL_P(line)) {
9183 return NULL;
9186 const char *cstr = StringValueCStr(line);
9187 size_t length = strlen(cstr);
9189 memcpy(string, cstr, length);
9190 string[length] = '\0';
9192 return string;
9196 * Parse the source off STDIN and store the resulting scope node in the given
9197 * parse result struct. It is assumed that the parse result object is zeroed
9198 * out. If the stream fails to parse, then a Ruby error is returned.
9200 VALUE
9201 pm_parse_stdin(pm_parse_result_t *result)
9203 pm_options_frozen_string_literal_init(&result->options);
9205 pm_buffer_t buffer;
9206 pm_node_t *node = pm_parse_stream(&result->parser, &buffer, (void *) rb_stdin, pm_parse_stdin_fgets, &result->options);
9208 // Copy the allocated buffer contents into the input string so that it gets
9209 // freed. At this point we've handed over ownership, so we don't need to
9210 // free the buffer itself.
9211 pm_string_owned_init(&result->input, (uint8_t *) pm_buffer_value(&buffer), pm_buffer_length(&buffer));
9213 return pm_parse_process(result, node);
9216 #undef NEW_ISEQ
9217 #define NEW_ISEQ OLD_ISEQ
9219 #undef NEW_CHILD_ISEQ
9220 #define NEW_CHILD_ISEQ OLD_CHILD_ISEQ