2023-09-27 19:39:53 +03:00
|
|
|
#include "prism.h"
|
2023-08-28 23:55:58 +03:00
|
|
|
|
2024-02-12 23:37:48 +03:00
|
|
|
/******************************************************************************/
|
|
|
|
/* These macros operate on pm_line_column_t structs as opposed to NODE*s. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2024-02-20 19:23:06 +03:00
|
|
|
#define PUSH_ADJUST(seq, location, label) \
|
|
|
|
ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), (int) (location).line))
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
#define PUSH_ADJUST_RESTORE(seq, label) \
|
|
|
|
ADD_ELEM((seq), (LINK_ELEMENT *) new_adjust_body(iseq, (label), -1))
|
|
|
|
|
2024-02-12 23:37:48 +03:00
|
|
|
#define PUSH_INSN(seq, location, insn) \
|
2024-02-13 05:47:23 +03:00
|
|
|
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).column, BIN(insn), 0))
|
2024-02-12 23:37:48 +03:00
|
|
|
|
|
|
|
#define PUSH_INSN1(seq, location, insn, op1) \
|
2024-02-13 05:47:23 +03:00
|
|
|
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).column, BIN(insn), 1, (VALUE)(op1)))
|
2024-02-12 23:37:48 +03:00
|
|
|
|
2024-02-13 05:47:23 +03:00
|
|
|
#define PUSH_INSNL(seq, location, insn, label) \
|
|
|
|
(PUSH_INSN1(seq, location, insn, label), LABEL_REF(label))
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
#define PUSH_INSN2(seq, location, insn, op1, op2) \
|
|
|
|
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).column, BIN(insn), 2, (VALUE)(op1), (VALUE)(op2)))
|
|
|
|
|
2024-02-20 19:23:06 +03:00
|
|
|
#define PUSH_INSN3(seq, location, insn, op1, op2, op3) \
|
|
|
|
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_body(iseq, (int) (location).line, (int) (location).column, BIN(insn), 3, (VALUE)(op1), (VALUE)(op2), (VALUE)(op3)))
|
|
|
|
|
2024-02-13 05:47:23 +03:00
|
|
|
#define PUSH_LABEL(seq, label) \
|
|
|
|
ADD_ELEM((seq), (LINK_ELEMENT *) (label))
|
2024-02-12 23:37:48 +03:00
|
|
|
|
|
|
|
#define PUSH_SEND_R(seq, location, id, argc, block, flag, keywords) \
|
2024-02-13 05:47:23 +03:00
|
|
|
ADD_ELEM((seq), (LINK_ELEMENT *) new_insn_send(iseq, (int) (location).line, (int) (location).column, (id), (VALUE)(argc), (block), (VALUE)(flag), (keywords)))
|
2024-02-12 23:37:48 +03:00
|
|
|
|
|
|
|
#define PUSH_SEND(seq, location, id, argc) \
|
2024-02-13 05:47:23 +03:00
|
|
|
PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)INT2FIX(0), NULL)
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
#define PUSH_SEND_WITH_FLAG(seq, location, id, argc, flag) \
|
|
|
|
PUSH_SEND_R((seq), location, (id), (argc), NULL, (VALUE)(flag), NULL)
|
|
|
|
|
2024-02-13 05:47:23 +03:00
|
|
|
#define PUSH_TRACE(seq, event) \
|
|
|
|
ADD_ELEM((seq), (LINK_ELEMENT *) new_trace_body(iseq, (event), 0))
|
2024-02-12 23:37:48 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* These are helper macros for the compiler. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
#define OLD_ISEQ NEW_ISEQ
|
|
|
|
#undef NEW_ISEQ
|
|
|
|
|
|
|
|
#define NEW_ISEQ(node, name, type, line_no) \
|
2024-02-12 23:48:23 +03:00
|
|
|
pm_new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
|
|
|
|
#undef NEW_CHILD_ISEQ
|
|
|
|
|
|
|
|
#define NEW_CHILD_ISEQ(node, name, type, line_no) \
|
2024-02-12 23:48:23 +03:00
|
|
|
pm_new_child_iseq(iseq, (node), rb_fstring(name), iseq, (type), (line_no))
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-11-03 02:29:20 +03:00
|
|
|
#define PM_COMPILE(node) \
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(iseq, (node), ret, popped, scope_node)
|
2023-11-03 02:29:20 +03:00
|
|
|
|
|
|
|
#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(iseq, (node), _ret, popped, scope_node)
|
2023-09-08 23:33:05 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_COMPILE_POPPED(node) \
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(iseq, (node), ret, true, scope_node)
|
2023-09-08 23:33:05 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_COMPILE_NOT_POPPED(node) \
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(iseq, (node), ret, false, scope_node)
|
2023-09-08 23:33:05 +03:00
|
|
|
|
2023-10-24 23:08:50 +03:00
|
|
|
#define PM_POP \
|
2024-01-22 23:22:12 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, pop)
|
2023-10-24 23:08:50 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_POP_IF_POPPED \
|
2024-01-22 23:22:12 +03:00
|
|
|
if (popped) PM_POP
|
2023-09-21 22:28:08 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_POP_UNLESS_POPPED \
|
2024-01-22 23:22:12 +03:00
|
|
|
if (!popped) PM_POP
|
2023-09-21 22:28:08 +03:00
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
#define PM_DUP \
|
2024-01-22 23:22:12 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, dup)
|
2023-10-26 18:03:18 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_DUP_UNLESS_POPPED \
|
2024-01-22 23:22:12 +03:00
|
|
|
if (!popped) PM_DUP
|
2023-09-21 22:28:08 +03:00
|
|
|
|
2023-10-30 19:56:30 +03:00
|
|
|
#define PM_PUTSELF \
|
2024-01-22 23:22:12 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, putself)
|
2023-10-30 19:56:30 +03:00
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
#define PM_PUTNIL \
|
2024-01-22 23:22:12 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil)
|
2023-10-23 16:46:37 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_PUTNIL_UNLESS_POPPED \
|
2024-01-22 23:22:12 +03:00
|
|
|
if (!popped) PM_PUTNIL
|
2023-09-21 22:28:08 +03:00
|
|
|
|
2023-10-25 18:34:03 +03:00
|
|
|
#define PM_SWAP \
|
2024-01-22 23:22:12 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, swap)
|
2023-10-25 18:34:03 +03:00
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
#define PM_SWAP_UNLESS_POPPED \
|
2024-01-22 23:22:12 +03:00
|
|
|
if (!popped) PM_SWAP
|
2023-10-26 18:03:18 +03:00
|
|
|
|
2023-12-06 17:45:04 +03:00
|
|
|
#define PM_NOP \
|
2024-01-22 23:22:12 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, nop)
|
2023-12-06 17:45:04 +03:00
|
|
|
|
2024-01-23 22:43:53 +03:00
|
|
|
#define PM_SPECIAL_CONSTANT_FLAG ((pm_constant_id_t)(1 << 31))
|
|
|
|
#define PM_CONSTANT_AND ((pm_constant_id_t)(idAnd | PM_SPECIAL_CONSTANT_FLAG))
|
2024-01-24 21:43:26 +03:00
|
|
|
#define PM_CONSTANT_DOT3 ((pm_constant_id_t)(idDot3 | PM_SPECIAL_CONSTANT_FLAG))
|
2024-01-24 00:13:18 +03:00
|
|
|
#define PM_CONSTANT_MULT ((pm_constant_id_t)(idMULT | PM_SPECIAL_CONSTANT_FLAG))
|
2024-01-24 18:22:46 +03:00
|
|
|
#define PM_CONSTANT_POW ((pm_constant_id_t)(idPow | PM_SPECIAL_CONSTANT_FLAG))
|
2023-10-20 12:59:02 +03:00
|
|
|
|
2024-01-30 00:03:03 +03:00
|
|
|
static int
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_location_line_number(const pm_parser_t *parser, const pm_location_t *location) {
|
|
|
|
return (int) pm_newline_list_line_column(&parser->newline_list, location->start, parser->start_line).line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pm_node_line_number(const pm_parser_t *parser, const pm_node_t *node)
|
2024-01-30 00:03:03 +03:00
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
return (int) pm_newline_list_line_column(&parser->newline_list, node->location.start, parser->start_line).line;
|
2024-01-30 00:03:03 +03:00
|
|
|
}
|
|
|
|
|
2024-02-23 04:22:47 +03:00
|
|
|
/**
|
|
|
|
* Convert the value of an integer node into a Ruby Integer.
|
|
|
|
*/
|
2023-08-29 19:27:00 +03:00
|
|
|
static VALUE
|
2023-09-27 19:39:53 +03:00
|
|
|
parse_integer(const pm_integer_node_t *node)
|
2023-09-08 22:33:51 +03:00
|
|
|
{
|
2024-02-23 04:22:47 +03:00
|
|
|
const pm_integer_t *integer = &node->value;
|
2023-09-22 21:53:55 +03:00
|
|
|
|
2024-02-23 04:22:47 +03:00
|
|
|
VALUE result = UINT2NUM(integer->head.value);
|
|
|
|
size_t shift = 0;
|
2023-09-08 22:33:51 +03:00
|
|
|
|
2024-02-23 04:22:47 +03:00
|
|
|
for (pm_integer_word_t *node = integer->head.next; node != NULL; node = node->next) {
|
|
|
|
VALUE receiver = rb_funcall(UINT2NUM(node->value), rb_intern("<<"), 1, ULONG2NUM(++shift * 32));
|
|
|
|
result = rb_funcall(receiver, rb_intern("|"), 1, result);
|
2023-09-22 21:53:55 +03:00
|
|
|
}
|
|
|
|
|
2024-02-23 04:22:47 +03:00
|
|
|
if (integer->negative) result = rb_funcall(result, rb_intern("-@"), 0);
|
|
|
|
return result;
|
2023-09-08 22:33:51 +03:00
|
|
|
}
|
|
|
|
|
2024-02-23 04:22:47 +03:00
|
|
|
/**
|
|
|
|
* Convert the value of a float node into a Ruby Float.
|
|
|
|
*/
|
2023-09-08 22:33:51 +03:00
|
|
|
static VALUE
|
2024-02-23 04:22:47 +03:00
|
|
|
parse_float(const pm_float_node_t *node)
|
2023-09-08 22:33:51 +03:00
|
|
|
{
|
2024-02-23 04:22:47 +03:00
|
|
|
return DBL2NUM(node->value);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
2024-02-23 04:22:47 +03:00
|
|
|
/**
|
|
|
|
* Convert the value of a rational node into a Ruby Rational. Rational nodes can
|
|
|
|
* either be wrapping an integer node or a float node. If it's an integer node,
|
|
|
|
* we can reuse our parsing. If it's not, then we'll parse the numerator and
|
|
|
|
* then parse the denominator and create the rational from those two values.
|
|
|
|
*/
|
2023-09-08 22:33:51 +03:00
|
|
|
static VALUE
|
2024-02-23 04:22:47 +03:00
|
|
|
parse_rational(const pm_rational_node_t *node)
|
2023-09-08 22:33:51 +03:00
|
|
|
{
|
2024-02-23 04:22:47 +03:00
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
if (PM_NODE_TYPE_P(node->numeric, PM_FLOAT_NODE)) {
|
|
|
|
const uint8_t *start = node->base.location.start;
|
|
|
|
const uint8_t *end = node->base.location.end - 1;
|
|
|
|
size_t length = end - start;
|
2023-09-08 22:33:51 +03:00
|
|
|
|
|
|
|
char *buffer = malloc(length + 1);
|
|
|
|
memcpy(buffer, start, length);
|
|
|
|
|
|
|
|
buffer[length] = '\0';
|
|
|
|
|
|
|
|
char *decimal = memchr(buffer, '.', length);
|
|
|
|
RUBY_ASSERT(decimal);
|
|
|
|
size_t seen_decimal = decimal - buffer;
|
|
|
|
size_t fraclen = length - seen_decimal - 1;
|
|
|
|
memmove(decimal, decimal + 1, fraclen + 1);
|
|
|
|
|
2024-02-23 04:22:47 +03:00
|
|
|
VALUE numerator = rb_cstr_to_inum(buffer, 10, false);
|
|
|
|
result = rb_rational_new(numerator, rb_int_positive_pow(10, fraclen));
|
2023-09-08 22:33:51 +03:00
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
else {
|
2024-02-23 04:22:47 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(node->numeric, PM_INTEGER_NODE));
|
|
|
|
VALUE numerator = parse_integer((const pm_integer_node_t *) node->numeric);
|
|
|
|
result = rb_rational_raw(numerator, INT2FIX(1));
|
2023-09-08 22:33:51 +03:00
|
|
|
}
|
|
|
|
|
2024-02-23 04:22:47 +03:00
|
|
|
return result;
|
2023-09-08 22:33:51 +03:00
|
|
|
}
|
|
|
|
|
2024-02-23 04:22:47 +03:00
|
|
|
/**
|
|
|
|
* Convert the value of an imaginary node into a Ruby Complex. Imaginary nodes
|
|
|
|
* can be wrapping an integer node, a float node, or a rational node. In all
|
|
|
|
* cases we will reuse parsing functions seen above to get the inner value, and
|
|
|
|
* then convert into an imaginary with rb_complex_raw.
|
|
|
|
*/
|
2023-09-08 22:33:51 +03:00
|
|
|
static VALUE
|
2023-09-27 19:39:53 +03:00
|
|
|
parse_imaginary(pm_imaginary_node_t *node)
|
2023-09-08 22:33:51 +03:00
|
|
|
{
|
|
|
|
VALUE imaginary_part;
|
2023-09-27 19:39:53 +03:00
|
|
|
switch (PM_NODE_TYPE(node->numeric)) {
|
|
|
|
case PM_FLOAT_NODE: {
|
2024-02-23 04:22:47 +03:00
|
|
|
imaginary_part = parse_float((const pm_float_node_t *) node->numeric);
|
2023-09-08 22:33:51 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTEGER_NODE: {
|
2024-02-23 04:22:47 +03:00
|
|
|
imaginary_part = parse_integer((const pm_integer_node_t *) node->numeric);
|
2023-09-08 22:33:51 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_RATIONAL_NODE: {
|
2024-02-23 04:22:47 +03:00
|
|
|
imaginary_part = parse_rational((const pm_rational_node_t *) node->numeric);
|
2023-09-08 22:33:51 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2024-01-02 19:34:04 +03:00
|
|
|
rb_bug("Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->numeric)));
|
2023-09-08 22:33:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rb_complex_raw(INT2FIX(0), imaginary_part);
|
|
|
|
}
|
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
static inline VALUE
|
2023-10-24 20:17:18 +03:00
|
|
|
parse_string(pm_string_t *string, const pm_parser_t *parser)
|
2023-09-08 22:33:51 +03:00
|
|
|
{
|
2023-12-06 20:54:16 +03:00
|
|
|
rb_encoding *enc = rb_enc_from_index(rb_enc_find_index(parser->encoding->name));
|
2023-10-19 23:50:21 +03:00
|
|
|
return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), enc);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 20:54:16 +03:00
|
|
|
/**
|
|
|
|
* Certain strings can have their encoding differ from the parser's encoding due
|
|
|
|
* to bytes or escape sequences that have the top bit set. This function handles
|
|
|
|
* creating those strings based on the flags set on the owning node.
|
|
|
|
*/
|
|
|
|
static inline VALUE
|
2024-02-12 23:16:44 +03:00
|
|
|
parse_string_encoded(const pm_node_t *node, const pm_string_t *string, const pm_parser_t *parser)
|
|
|
|
{
|
2023-12-06 20:54:16 +03:00
|
|
|
rb_encoding *encoding;
|
|
|
|
|
|
|
|
if (node->flags & PM_ENCODING_FLAGS_FORCED_BINARY_ENCODING) {
|
|
|
|
encoding = rb_ascii8bit_encoding();
|
|
|
|
} else if (node->flags & PM_ENCODING_FLAGS_FORCED_UTF8_ENCODING) {
|
|
|
|
encoding = rb_utf8_encoding();
|
|
|
|
} else {
|
|
|
|
encoding = rb_enc_from_index(rb_enc_find_index(parser->encoding->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), encoding);
|
|
|
|
}
|
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
static inline ID
|
2024-01-26 23:18:21 +03:00
|
|
|
parse_symbol(const uint8_t *start, const uint8_t *end, const char *encoding)
|
2023-09-08 22:33:51 +03:00
|
|
|
{
|
2024-01-26 23:18:21 +03:00
|
|
|
rb_encoding *enc = rb_enc_from_index(rb_enc_find_index(encoding));
|
2023-10-19 23:50:21 +03:00
|
|
|
return rb_intern3((const char *) start, end - start, enc);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline ID
|
2024-01-26 23:18:21 +03:00
|
|
|
parse_string_symbol(const pm_symbol_node_t *symbol, const pm_parser_t *parser)
|
2023-09-27 19:39:53 +03:00
|
|
|
{
|
2024-01-26 23:56:30 +03:00
|
|
|
const char *encoding = parser->encoding->name;
|
|
|
|
if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING) {
|
|
|
|
encoding = "UTF-8";
|
|
|
|
}
|
|
|
|
else if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING) {
|
|
|
|
encoding = "ASCII-8BIT";
|
|
|
|
}
|
2024-01-27 00:13:04 +03:00
|
|
|
else if (symbol->base.flags & PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING) {
|
|
|
|
encoding = "US-ASCII";
|
|
|
|
}
|
2024-01-26 23:18:21 +03:00
|
|
|
const uint8_t *start = pm_string_source(&symbol->unescaped);
|
|
|
|
return parse_symbol(start, start + pm_string_length(&symbol->unescaped), encoding);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 22:00:01 +03:00
|
|
|
static inline ID
|
2024-01-12 01:05:42 +03:00
|
|
|
parse_location_symbol(const pm_location_t *location, const pm_parser_t *parser)
|
2023-10-16 22:00:01 +03:00
|
|
|
{
|
2024-01-26 23:18:21 +03:00
|
|
|
return parse_symbol(location->start, location->end, parser->encoding->name);
|
2023-10-16 22:00:01 +03:00
|
|
|
}
|
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
static int
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_optimizable_range_item_p(pm_node_t *node)
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
{
|
2023-09-27 19:39:53 +03:00
|
|
|
return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
|
2023-10-24 20:17:18 +03:00
|
|
|
#define RE_OPTION_ENCODING_SHIFT 8
|
|
|
|
|
2023-09-28 22:19:56 +03:00
|
|
|
/**
|
|
|
|
* Check the prism flags of a regular expression-like node and return the flags
|
|
|
|
* that are expected by the CRuby VM.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
pm_reg_flags(const pm_node_t *node) {
|
|
|
|
int flags = 0;
|
2023-10-24 20:17:18 +03:00
|
|
|
int dummy = 0;
|
|
|
|
|
|
|
|
// Check "no encoding" first so that flags don't get clobbered
|
|
|
|
// We're calling `rb_char_to_option_kcode` in this case so that
|
|
|
|
// we don't need to have access to `ARG_ENCODING_NONE`
|
|
|
|
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT) {
|
|
|
|
rb_char_to_option_kcode('n', &flags, &dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_EUC_JP) {
|
|
|
|
rb_char_to_option_kcode('e', &flags, &dummy);
|
|
|
|
flags |= ('e' << RE_OPTION_ENCODING_SHIFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J) {
|
|
|
|
rb_char_to_option_kcode('s', &flags, &dummy);
|
|
|
|
flags |= ('s' << RE_OPTION_ENCODING_SHIFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_UTF_8) {
|
|
|
|
rb_char_to_option_kcode('u', &flags, &dummy);
|
|
|
|
flags |= ('u' << RE_OPTION_ENCODING_SHIFT);
|
|
|
|
}
|
2023-09-28 22:19:56 +03:00
|
|
|
|
|
|
|
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE) {
|
|
|
|
flags |= ONIG_OPTION_IGNORECASE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE) {
|
|
|
|
flags |= ONIG_OPTION_MULTILINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_EXTENDED) {
|
|
|
|
flags |= ONIG_OPTION_EXTEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2023-10-24 20:17:18 +03:00
|
|
|
static rb_encoding *
|
2024-02-12 23:16:44 +03:00
|
|
|
pm_reg_enc(const pm_regular_expression_node_t *node, const pm_parser_t *parser)
|
|
|
|
{
|
2023-10-24 20:17:18 +03:00
|
|
|
if (node->base.flags & PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT) {
|
|
|
|
return rb_ascii8bit_encoding();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->base.flags & PM_REGULAR_EXPRESSION_FLAGS_EUC_JP) {
|
|
|
|
return rb_enc_get_from_index(ENCINDEX_EUC_JP);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->base.flags & PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J) {
|
|
|
|
return rb_enc_get_from_index(ENCINDEX_Windows_31J);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->base.flags & PM_REGULAR_EXPRESSION_FLAGS_UTF_8) {
|
|
|
|
return rb_utf8_encoding();
|
|
|
|
}
|
|
|
|
|
2023-12-06 20:54:16 +03:00
|
|
|
return rb_enc_from_index(rb_enc_find_index(parser->encoding->name));
|
2023-10-24 20:17:18 +03:00
|
|
|
}
|
|
|
|
|
2023-09-19 18:35:22 +03:00
|
|
|
/**
|
|
|
|
* Certain nodes can be compiled literally, which can lead to further
|
2023-09-27 19:39:53 +03:00
|
|
|
* optimizations. These nodes will all have the PM_NODE_FLAG_STATIC_LITERAL flag
|
2023-09-19 18:35:22 +03:00
|
|
|
* set.
|
|
|
|
*/
|
|
|
|
static inline bool
|
2023-09-28 22:19:56 +03:00
|
|
|
pm_static_literal_p(const pm_node_t *node)
|
2023-09-27 19:39:53 +03:00
|
|
|
{
|
|
|
|
return node->flags & PM_NODE_FLAG_STATIC_LITERAL;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
|
2023-10-24 20:17:18 +03:00
|
|
|
static VALUE
|
2024-02-12 23:16:44 +03:00
|
|
|
pm_new_regex(pm_regular_expression_node_t * cast, const pm_parser_t * parser)
|
|
|
|
{
|
2023-10-24 20:17:18 +03:00
|
|
|
VALUE regex_str = parse_string(&cast->unescaped, parser);
|
|
|
|
rb_encoding * enc = pm_reg_enc(cast, parser);
|
|
|
|
|
2024-01-22 18:29:09 +03:00
|
|
|
VALUE regex = rb_enc_reg_new(RSTRING_PTR(regex_str), RSTRING_LEN(regex_str), enc, pm_reg_flags((const pm_node_t *)cast));
|
2024-02-16 22:23:00 +03:00
|
|
|
RB_GC_GUARD(regex_str);
|
|
|
|
|
2024-01-22 18:29:09 +03:00
|
|
|
rb_obj_freeze(regex);
|
|
|
|
|
|
|
|
return regex;
|
2023-10-24 20:17:18 +03:00
|
|
|
}
|
|
|
|
|
2023-09-28 22:19:56 +03:00
|
|
|
/**
|
|
|
|
* Certain nodes can be compiled literally. This function returns the literal
|
|
|
|
* value described by the given node. For example, an array node with all static
|
|
|
|
* literal values can be compiled into a literal array.
|
|
|
|
*/
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
static inline VALUE
|
2024-02-12 23:48:23 +03:00
|
|
|
pm_static_literal_value(const pm_node_t *node, const pm_scope_node_t *scope_node)
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
{
|
2023-09-28 22:19:56 +03:00
|
|
|
// Every node that comes into this function should already be marked as
|
|
|
|
// static literal. If it's not, then we have a bug somewhere.
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(pm_static_literal_p(node));
|
2023-09-28 22:19:56 +03:00
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
|
|
|
case PM_ARRAY_NODE: {
|
|
|
|
pm_array_node_t *cast = (pm_array_node_t *) node;
|
|
|
|
pm_node_list_t *elements = &cast->elements;
|
|
|
|
|
|
|
|
VALUE value = rb_ary_hidden_new(elements->size);
|
|
|
|
for (size_t index = 0; index < elements->size; index++) {
|
2024-02-12 23:48:23 +03:00
|
|
|
rb_ary_push(value, pm_static_literal_value(elements->nodes[index], scope_node));
|
2023-09-28 22:19:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
OBJ_FREEZE(value);
|
|
|
|
return value;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_FALSE_NODE:
|
2023-09-18 16:29:52 +03:00
|
|
|
return Qfalse;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_FLOAT_NODE:
|
2024-02-23 04:22:47 +03:00
|
|
|
return parse_float((const pm_float_node_t *) node);
|
2023-09-28 22:19:56 +03:00
|
|
|
case PM_HASH_NODE: {
|
|
|
|
pm_hash_node_t *cast = (pm_hash_node_t *) node;
|
|
|
|
pm_node_list_t *elements = &cast->elements;
|
|
|
|
|
|
|
|
VALUE array = rb_ary_hidden_new(elements->size * 2);
|
|
|
|
for (size_t index = 0; index < elements->size; index++) {
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(elements->nodes[index], PM_ASSOC_NODE));
|
2023-09-28 22:19:56 +03:00
|
|
|
pm_assoc_node_t *cast = (pm_assoc_node_t *) elements->nodes[index];
|
2024-02-12 23:48:23 +03:00
|
|
|
VALUE pair[2] = { pm_static_literal_value(cast->key, scope_node), pm_static_literal_value(cast->value, scope_node) };
|
2023-09-28 22:19:56 +03:00
|
|
|
rb_ary_cat(array, pair, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE value = rb_hash_new_with_size(elements->size);
|
|
|
|
rb_hash_bulk_insert(RARRAY_LEN(array), RARRAY_CONST_PTR(array), value);
|
|
|
|
|
|
|
|
value = rb_obj_hide(value);
|
|
|
|
OBJ_FREEZE(value);
|
|
|
|
return value;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_IMAGINARY_NODE:
|
2023-09-28 22:19:56 +03:00
|
|
|
return parse_imaginary((pm_imaginary_node_t *) node);
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTEGER_NODE:
|
2024-02-23 04:22:47 +03:00
|
|
|
return parse_integer((const pm_integer_node_t *) node);
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_NIL_NODE:
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return Qnil;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_RATIONAL_NODE:
|
2024-02-23 04:22:47 +03:00
|
|
|
return parse_rational((const pm_rational_node_t *) node);
|
2023-09-28 22:19:56 +03:00
|
|
|
case PM_REGULAR_EXPRESSION_NODE: {
|
|
|
|
pm_regular_expression_node_t *cast = (pm_regular_expression_node_t *) node;
|
|
|
|
|
2024-02-12 23:48:23 +03:00
|
|
|
return pm_new_regex(cast, scope_node->parser);
|
2023-09-28 22:19:56 +03:00
|
|
|
}
|
|
|
|
case PM_SOURCE_ENCODING_NODE: {
|
2024-01-02 19:34:04 +03:00
|
|
|
const char *name = scope_node->parser->encoding->name;
|
|
|
|
rb_encoding *encoding = rb_find_encoding(rb_str_new_cstr(name));
|
|
|
|
if (!encoding) rb_bug("Encoding not found %s!", name);
|
2023-09-28 22:19:56 +03:00
|
|
|
return rb_enc_from_encoding(encoding);
|
|
|
|
}
|
|
|
|
case PM_SOURCE_FILE_NODE: {
|
|
|
|
pm_source_file_node_t *cast = (pm_source_file_node_t *)node;
|
2024-02-12 23:48:23 +03:00
|
|
|
return cast->filepath.length ? parse_string(&cast->filepath, scope_node->parser) : rb_fstring_lit("<compiled>");
|
2023-09-28 22:19:56 +03:00
|
|
|
}
|
2024-02-14 22:17:32 +03:00
|
|
|
case PM_SOURCE_LINE_NODE:
|
|
|
|
return INT2FIX(pm_node_line_number(scope_node->parser, node));
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_STRING_NODE:
|
2024-02-12 23:48:23 +03:00
|
|
|
return parse_string_encoded(node, &((pm_string_node_t *)node)->unescaped, scope_node->parser);
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SYMBOL_NODE:
|
2024-02-12 23:48:23 +03:00
|
|
|
return ID2SYM(parse_string_symbol((pm_symbol_node_t *)node, scope_node->parser));
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_TRUE_NODE:
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return Qtrue;
|
|
|
|
default:
|
2024-01-02 19:34:04 +03:00
|
|
|
rb_bug("Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
2023-09-07 17:59:51 +03:00
|
|
|
return Qfalse;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 19:34:05 +03:00
|
|
|
/**
|
|
|
|
* Currently, the ADD_INSN family of macros expects a NODE as the second
|
|
|
|
* parameter. It uses this node to determine the line number and the node ID for
|
|
|
|
* the instruction.
|
|
|
|
*
|
|
|
|
* Because prism does not use the NODE struct (or have node IDs for that matter)
|
|
|
|
* we need to generate a dummy node to pass to these macros. We also need to use
|
|
|
|
* the line number from the node to generate labels.
|
|
|
|
*
|
|
|
|
* We use this struct to store the dummy node and the line number together so
|
|
|
|
* that we can use it while we're compiling code.
|
|
|
|
*
|
|
|
|
* In the future, we'll need to eventually remove this dependency and figure out
|
|
|
|
* a more permanent solution. For the line numbers, this shouldn't be too much
|
|
|
|
* of a problem, we can redefine the ADD_INSN family of macros. For the node ID,
|
|
|
|
* we can probably replace it directly with the column information since we have
|
|
|
|
* that at the time that we're generating instructions. In theory this could
|
|
|
|
* make node ID unnecessary.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
NODE node;
|
|
|
|
int lineno;
|
|
|
|
} pm_line_node_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The function generates a dummy node and stores the line number after it looks
|
|
|
|
* it up for the given scope and node. (The scope in this case is just used
|
|
|
|
* because it holds a reference to the parser, which holds a reference to the
|
|
|
|
* newline list that we need to look up the line numbers.)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pm_line_node(pm_line_node_t *line_node, const pm_scope_node_t *scope_node, const pm_node_t *node)
|
|
|
|
{
|
|
|
|
// First, clear out the pointer.
|
|
|
|
memset(line_node, 0, sizeof(pm_line_node_t));
|
|
|
|
|
|
|
|
// Next, use the line number for the dummy node.
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, node);
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
nd_set_line(&line_node->node, lineno);
|
|
|
|
nd_set_node_id(&line_node->node, lineno);
|
|
|
|
line_node->lineno = lineno;
|
|
|
|
}
|
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
static void
|
2023-10-23 20:49:39 +03:00
|
|
|
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
|
2024-01-31 20:17:31 +03:00
|
|
|
LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
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)
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, cond);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
DECL_ANCHOR(seq);
|
|
|
|
INIT_ANCHOR(seq);
|
|
|
|
LABEL *label = NEW_LABEL(lineno);
|
|
|
|
if (!then_label) then_label = label;
|
|
|
|
else if (!else_label) else_label = label;
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, popped, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
if (LIST_INSN_SIZE_ONE(seq)) {
|
|
|
|
INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
|
|
|
|
if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!label->refcnt) {
|
2024-01-30 23:51:30 +03:00
|
|
|
if (popped) {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_LABEL(seq, label);
|
|
|
|
}
|
|
|
|
ADD_SEQ(ret, seq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
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);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-10-20 18:03:02 +03:00
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_flip_flop(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)
|
2023-10-20 18:03:02 +03:00
|
|
|
{
|
|
|
|
NODE dummy_line_node = generate_dummy_line_node(ISEQ_BODY(iseq)->location.first_lineno, -1);
|
|
|
|
LABEL *lend = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
int again = !(flip_flop_node->base.flags & PM_RANGE_FLAGS_EXCLUDE_END);
|
|
|
|
|
|
|
|
rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
|
|
|
|
VALUE key = INT2FIX(count);
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getspecial, key, INT2FIX(0));
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, lend);
|
|
|
|
|
|
|
|
if (flip_flop_node->left) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(flip_flop_node->left);
|
2023-10-20 18:03:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-10-24 23:08:50 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-20 18:03:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, else_label);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setspecial, key);
|
|
|
|
if (!again) {
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, then_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_LABEL(ret, lend);
|
|
|
|
if (flip_flop_node->right) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(flip_flop_node->right);
|
2023-10-20 18:03:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-10-24 23:08:50 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-20 18:03:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, then_label);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setspecial, key);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, then_label);
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
void pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *defined_node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, int lineno, bool in_condition);
|
2024-01-30 22:45:16 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
static void
|
2023-10-23 20:49:39 +03:00
|
|
|
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
|
2024-01-31 20:17:31 +03:00
|
|
|
LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node)
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, cond);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
again:
|
2023-09-27 19:39:53 +03:00
|
|
|
switch (PM_NODE_TYPE(cond)) {
|
|
|
|
case PM_AND_NODE: {
|
|
|
|
pm_and_node_t *and_node = (pm_and_node_t *)cond;
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_logical(iseq, ret, and_node->left, NULL, else_label, popped, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
cond = and_node->right;
|
|
|
|
goto again;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_OR_NODE: {
|
|
|
|
pm_or_node_t *or_node = (pm_or_node_t *)cond;
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_logical(iseq, ret, or_node->left, then_label, NULL, popped, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
cond = or_node->right;
|
|
|
|
goto again;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_FALSE_NODE:
|
|
|
|
case PM_NIL_NODE:
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, else_label);
|
|
|
|
return;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_FLOAT_NODE:
|
|
|
|
case PM_IMAGINARY_NODE:
|
|
|
|
case PM_INTEGER_NODE:
|
|
|
|
case PM_LAMBDA_NODE:
|
|
|
|
case PM_RATIONAL_NODE:
|
|
|
|
case PM_REGULAR_EXPRESSION_NODE:
|
|
|
|
case PM_STRING_NODE:
|
|
|
|
case PM_SYMBOL_NODE:
|
|
|
|
case PM_TRUE_NODE:
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, then_label);
|
|
|
|
return;
|
2023-10-20 18:03:02 +03:00
|
|
|
case PM_FLIP_FLOP_NODE:
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_flip_flop((pm_flip_flop_node_t *)cond, else_label, then_label, iseq, lineno, ret, popped, scope_node);
|
2023-10-20 18:03:02 +03:00
|
|
|
return;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
// TODO: Several more nodes in this case statement
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_DEFINED_NODE: {
|
|
|
|
pm_defined_node_t *defined_node = (pm_defined_node_t *)cond;
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_defined_expr(iseq, defined_node->value, ret, popped, scope_node, dummy_line_node, lineno, true);
|
2023-10-25 11:07:26 +03:00
|
|
|
break;
|
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
default: {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(iseq, cond, ret, false, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
break;
|
2023-09-28 16:47:46 +03:00
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, else_label);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, then_label);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-20 19:37:54 +03:00
|
|
|
/**
|
|
|
|
* Compile an if or unless node.
|
|
|
|
*/
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
static void
|
2024-02-20 19:37:54 +03:00
|
|
|
pm_compile_conditional(rb_iseq_t *iseq, const pm_line_column_t *line_column, 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)
|
2023-09-27 19:39:53 +03:00
|
|
|
{
|
2024-02-20 19:37:54 +03:00
|
|
|
const pm_line_column_t location = *line_column;
|
|
|
|
LABEL *then_label = NEW_LABEL(location.line);
|
|
|
|
LABEL *else_label = NEW_LABEL(location.line);
|
|
|
|
LABEL *end_label = NULL;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_branch_condition(iseq, ret, predicate, then_label, else_label, false, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
if (then_label->refcnt) {
|
2024-02-20 19:37:54 +03:00
|
|
|
PUSH_LABEL(ret, then_label);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
DECL_ANCHOR(then_seq);
|
|
|
|
INIT_ANCHOR(then_seq);
|
2024-02-20 19:37:54 +03:00
|
|
|
|
|
|
|
if (statements) {
|
|
|
|
pm_compile_node(iseq, (const pm_node_t *) statements, then_seq, popped, scope_node);
|
|
|
|
}
|
|
|
|
else if (!popped) {
|
|
|
|
PUSH_INSN(then_seq, location, putnil);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (else_label->refcnt) {
|
2024-02-20 19:37:54 +03:00
|
|
|
end_label = NEW_LABEL(location.line);
|
|
|
|
PUSH_INSNL(then_seq, location, jump, end_label);
|
|
|
|
if (!popped) PUSH_INSN(then_seq, location, pop);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2024-02-20 19:37:54 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
ADD_SEQ(ret, then_seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (else_label->refcnt) {
|
2024-02-20 19:37:54 +03:00
|
|
|
PUSH_LABEL(ret, else_label);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
DECL_ANCHOR(else_seq);
|
|
|
|
INIT_ANCHOR(else_seq);
|
2024-02-20 19:37:54 +03:00
|
|
|
|
|
|
|
if (consequent) {
|
|
|
|
pm_compile_node(iseq, (const pm_node_t *) consequent, else_seq, popped, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2024-02-20 19:37:54 +03:00
|
|
|
else if (!popped) {
|
|
|
|
PUSH_INSN(else_seq, location, putnil);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEQ(ret, else_seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end_label) {
|
2024-02-20 19:37:54 +03:00
|
|
|
PUSH_LABEL(ret, end_label);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
/**
|
|
|
|
* Compile a while or until loop.
|
|
|
|
*/
|
2023-08-30 00:17:08 +03:00
|
|
|
static void
|
2024-02-20 19:08:30 +03:00
|
|
|
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_statements_node_t *statements, const pm_node_t *predicate, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
|
2023-08-30 00:17:08 +03:00
|
|
|
{
|
2024-02-20 19:08:30 +03:00
|
|
|
const pm_line_column_t location = *line_column;
|
2023-08-30 00:17:08 +03:00
|
|
|
|
|
|
|
LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
|
|
|
|
LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
|
|
|
|
LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
|
|
|
|
|
|
|
|
// TODO: Deal with ensures in here
|
2024-02-20 19:08:30 +03:00
|
|
|
LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(location.line); /* next */
|
|
|
|
LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(location.line); /* redo */
|
|
|
|
LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(location.line); /* break */
|
|
|
|
LABEL *end_label = NEW_LABEL(location.line);
|
|
|
|
LABEL *adjust_label = NEW_LABEL(location.line);
|
2023-08-30 00:17:08 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
LABEL *next_catch_label = NEW_LABEL(location.line);
|
2023-08-30 00:17:08 +03:00
|
|
|
LABEL *tmp_label = NULL;
|
|
|
|
|
|
|
|
// begin; end while true
|
2023-09-27 19:39:53 +03:00
|
|
|
if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
|
2024-02-20 19:08:30 +03:00
|
|
|
tmp_label = NEW_LABEL(location.line);
|
|
|
|
PUSH_INSNL(ret, location, jump, tmp_label);
|
2023-08-30 00:17:08 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// while true; end
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSNL(ret, location, jump, next_label);
|
2023-08-30 00:17:08 +03:00
|
|
|
}
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_LABEL(ret, adjust_label);
|
|
|
|
PUSH_INSN(ret, location, putnil);
|
|
|
|
PUSH_LABEL(ret, next_catch_label);
|
|
|
|
PUSH_INSN(ret, location, pop);
|
|
|
|
PUSH_INSNL(ret, location, jump, next_label);
|
|
|
|
if (tmp_label) PUSH_LABEL(ret, tmp_label);
|
2023-08-30 00:17:08 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_LABEL(ret, redo_label);
|
|
|
|
if (statements != NULL) PM_COMPILE_POPPED((const pm_node_t *) statements);
|
2023-08-30 00:17:08 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_LABEL(ret, next_label);
|
2023-09-27 19:39:53 +03:00
|
|
|
if (type == PM_WHILE_NODE) {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, popped, scope_node);
|
2023-09-27 19:39:53 +03:00
|
|
|
} else if (type == PM_UNTIL_NODE) {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, popped, scope_node);
|
2023-08-30 00:17:08 +03:00
|
|
|
}
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_LABEL(ret, end_label);
|
|
|
|
PUSH_ADJUST_RESTORE(ret, adjust_label);
|
|
|
|
PUSH_INSN(ret, location, putnil);
|
2023-08-30 00:17:08 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_LABEL(ret, break_label);
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-08-30 00:17:08 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL, break_label);
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL, next_catch_label);
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL, ISEQ_COMPILE_DATA(iseq)->redo_label);
|
2023-08-30 00:17:08 +03:00
|
|
|
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-16 00:28:29 +03:00
|
|
|
static int
|
2024-02-12 23:48:23 +03:00
|
|
|
pm_interpolated_node_compile(pm_node_list_t *parts, rb_iseq_t *iseq, NODE dummy_line_node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
|
2023-09-08 17:40:07 +03:00
|
|
|
{
|
2023-12-16 00:28:29 +03:00
|
|
|
int number_of_items_pushed = 0;
|
2023-11-14 03:03:24 +03:00
|
|
|
size_t parts_size = parts->size;
|
2023-09-08 17:40:07 +03:00
|
|
|
|
|
|
|
if (parts_size > 0) {
|
2023-12-16 00:28:29 +03:00
|
|
|
VALUE current_string = Qnil;
|
2024-02-06 17:44:46 +03:00
|
|
|
|
2023-09-08 17:40:07 +03:00
|
|
|
for (size_t index = 0; index < parts_size; index++) {
|
2024-02-06 17:44:46 +03:00
|
|
|
const pm_node_t *part = parts->nodes[index];
|
2023-09-08 17:40:07 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
|
2024-02-06 17:44:46 +03:00
|
|
|
const pm_string_node_t *string_node = (const pm_string_node_t *)part;
|
2024-02-12 23:48:23 +03:00
|
|
|
VALUE string_value = parse_string_encoded((pm_node_t *)string_node, &string_node->unescaped, scope_node->parser);
|
2024-02-06 17:44:46 +03:00
|
|
|
|
2023-12-16 00:28:29 +03:00
|
|
|
if (RTEST(current_string)) {
|
|
|
|
current_string = rb_str_concat(current_string, string_value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
current_string = string_value;
|
|
|
|
}
|
2023-09-08 17:40:07 +03:00
|
|
|
}
|
2024-01-31 00:42:34 +03:00
|
|
|
else if (PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE) &&
|
2024-02-06 17:44:46 +03:00
|
|
|
((const pm_embedded_statements_node_t *) part)->statements != NULL &&
|
|
|
|
((const pm_embedded_statements_node_t *) part)->statements->body.size == 1 &&
|
|
|
|
PM_NODE_TYPE_P(((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0], PM_STRING_NODE)) {
|
|
|
|
const pm_string_node_t *string_node = (const pm_string_node_t *) ((const pm_embedded_statements_node_t *) part)->statements->body.nodes[0];
|
2024-02-12 23:48:23 +03:00
|
|
|
VALUE string_value = parse_string_encoded((pm_node_t *)string_node, &string_node->unescaped, scope_node->parser);
|
2024-02-06 17:44:46 +03:00
|
|
|
|
2023-12-16 00:28:29 +03:00
|
|
|
if (RTEST(current_string)) {
|
2024-01-31 00:42:34 +03:00
|
|
|
current_string = rb_str_concat(current_string, string_value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
current_string = string_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!RTEST(current_string)) {
|
2024-02-12 23:48:23 +03:00
|
|
|
rb_encoding *enc = rb_enc_from_index(rb_enc_find_index(scope_node->parser->encoding->name));
|
2024-01-31 00:42:34 +03:00
|
|
|
current_string = rb_enc_str_new(NULL, 0, enc);
|
|
|
|
}
|
|
|
|
|
2024-02-06 19:59:49 +03:00
|
|
|
if (ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal) {
|
2024-01-31 00:42:34 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_str_freeze(current_string));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putstring, rb_str_freeze(current_string));
|
2023-12-16 00:28:29 +03:00
|
|
|
}
|
2024-02-06 19:59:49 +03:00
|
|
|
|
2024-01-31 00:42:34 +03:00
|
|
|
current_string = Qnil;
|
|
|
|
number_of_items_pushed++;
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(part);
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-09-08 17:40:07 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, objtostring, new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE , NULL, FALSE));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, anytostring);
|
2023-12-16 00:28:29 +03:00
|
|
|
|
|
|
|
number_of_items_pushed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RTEST(current_string)) {
|
2024-02-01 23:15:07 +03:00
|
|
|
current_string = rb_fstring(current_string);
|
2024-02-06 19:59:49 +03:00
|
|
|
|
|
|
|
if (ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal) {
|
2024-02-01 23:15:07 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, current_string);
|
2023-12-16 00:28:29 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-02-01 23:15:07 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putstring, current_string);
|
2023-09-08 17:40:07 +03:00
|
|
|
}
|
2024-02-06 19:59:49 +03:00
|
|
|
|
2023-12-16 00:28:29 +03:00
|
|
|
current_string = Qnil;
|
|
|
|
number_of_items_pushed++;
|
2023-09-08 17:40:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-09-08 17:40:07 +03:00
|
|
|
}
|
2023-12-16 00:28:29 +03:00
|
|
|
return number_of_items_pushed;
|
2023-09-08 17:40:07 +03:00
|
|
|
}
|
2023-11-08 20:49:58 +03:00
|
|
|
|
2023-11-29 22:12:31 +03:00
|
|
|
// This recurses through scopes and finds the local index at any scope level
|
|
|
|
// It also takes a pointer to depth, and increments depth appropriately
|
2024-01-26 02:08:44 +03:00
|
|
|
// according to the depth of the local.
|
2024-01-16 00:32:17 +03:00
|
|
|
static pm_local_index_t
|
2024-01-24 18:39:40 +03:00
|
|
|
pm_lookup_local_index(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, pm_constant_id_t constant_id, int start_depth)
|
2023-11-20 23:40:35 +03:00
|
|
|
{
|
2024-01-23 23:33:45 +03:00
|
|
|
pm_local_index_t lindex = { 0 };
|
2023-11-20 23:40:35 +03:00
|
|
|
st_data_t local_index;
|
|
|
|
|
2024-01-23 23:33:45 +03:00
|
|
|
int level;
|
|
|
|
for (level = 0; level < start_depth; level++) {
|
|
|
|
scope_node = scope_node->previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
|
2024-01-16 00:42:16 +03:00
|
|
|
level++;
|
2024-01-26 02:08:44 +03:00
|
|
|
|
2024-01-16 19:06:18 +03:00
|
|
|
if (scope_node->previous) {
|
|
|
|
scope_node = scope_node->previous;
|
|
|
|
} else {
|
|
|
|
// We have recursed up all scope nodes
|
|
|
|
// and have not found the local yet
|
2024-01-23 23:33:45 +03:00
|
|
|
rb_bug("Local with constant_id %u does not exist", (unsigned int) constant_id);
|
2024-01-16 19:06:18 +03:00
|
|
|
}
|
2023-11-20 23:40:35 +03:00
|
|
|
}
|
|
|
|
|
2024-01-16 00:32:17 +03:00
|
|
|
lindex.level = level;
|
2024-01-23 23:33:45 +03:00
|
|
|
lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
|
2024-01-16 00:32:17 +03:00
|
|
|
return lindex;
|
2023-11-20 23:40:35 +03:00
|
|
|
}
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
// This returns the CRuby ID which maps to the pm_constant_id_t
|
2023-09-06 17:28:29 +03:00
|
|
|
//
|
2023-09-27 19:39:53 +03:00
|
|
|
// Constant_ids in prism are indexes of the constants in prism's constant pool.
|
2023-10-17 01:36:25 +03:00
|
|
|
// We add a constants mapping on the scope_node which is a mapping from
|
2023-09-06 17:28:29 +03:00
|
|
|
// these constant_id indexes to the CRuby IDs that they represent.
|
|
|
|
// This helper method allows easy access to those IDs
|
|
|
|
static ID
|
2024-02-08 18:59:01 +03:00
|
|
|
pm_constant_id_lookup(const pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
|
2023-09-06 17:28:29 +03:00
|
|
|
{
|
2023-11-09 10:39:49 +03:00
|
|
|
if (constant_id < 1 || constant_id > scope_node->parser->constant_pool.size) {
|
2024-01-02 19:34:04 +03:00
|
|
|
rb_bug("constant_id out of range: %u", (unsigned int)constant_id);
|
2023-11-09 10:39:49 +03:00
|
|
|
}
|
2023-11-09 10:29:49 +03:00
|
|
|
return scope_node->constants[constant_id - 1];
|
2023-09-06 17:28:29 +03:00
|
|
|
}
|
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
static rb_iseq_t *
|
2024-02-12 23:48:23 +03:00
|
|
|
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)
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
{
|
|
|
|
debugs("[new_child_iseq]> ---------------------------------------\n");
|
|
|
|
int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
|
2024-01-31 20:17:31 +03:00
|
|
|
rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
rb_iseq_path(iseq), rb_iseq_realpath(iseq),
|
|
|
|
line_no, parent,
|
|
|
|
isolated_depth ? isolated_depth + 1 : 0,
|
|
|
|
type, ISEQ_COMPILE_DATA(iseq)->option);
|
|
|
|
debugs("[new_child_iseq]< ---------------------------------------\n");
|
|
|
|
return ret_iseq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_class_path(LINK_ANCHOR *const ret, rb_iseq_t *iseq, const pm_node_t *constant_path_node, const NODE *line_node, bool popped, pm_scope_node_t *scope_node)
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
{
|
2023-09-27 19:39:53 +03:00
|
|
|
if (PM_NODE_TYPE_P(constant_path_node, PM_CONSTANT_PATH_NODE)) {
|
|
|
|
pm_node_t *parent = ((pm_constant_path_node_t *)constant_path_node)->parent;
|
2023-08-31 00:26:22 +03:00
|
|
|
if (parent) {
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
/* Bar::Foo */
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(parent);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return VM_DEFINECLASS_FLAG_SCOPED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* toplevel class ::Foo */
|
|
|
|
ADD_INSN1(ret, line_node, putobject, rb_cObject);
|
|
|
|
return VM_DEFINECLASS_FLAG_SCOPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* class at cbase Foo */
|
|
|
|
ADD_INSN1(ret, line_node, putspecialobject,
|
|
|
|
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
/**
|
|
|
|
* Compile either a call and write node or a call or write node. These look like
|
|
|
|
* method calls that are followed by a ||= or &&= operator.
|
|
|
|
*/
|
2023-10-25 18:34:03 +03:00
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_call_and_or_write_node(bool and_node, pm_node_t *receiver, pm_node_t *value, pm_constant_id_t write_name, pm_constant_id_t read_name, bool safe_nav, LINK_ANCHOR *const ret, rb_iseq_t *iseq, int lineno, bool popped, pm_scope_node_t *scope_node)
|
2023-10-25 18:34:03 +03:00
|
|
|
{
|
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
LABEL *lfin = NEW_LABEL(lineno);
|
|
|
|
LABEL *lcfin = NEW_LABEL(lineno);
|
|
|
|
LABEL *lskip = NULL;
|
2023-10-25 18:34:03 +03:00
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
|
|
|
|
ID id_read_name = pm_constant_id_lookup(scope_node, read_name);
|
2023-10-25 18:34:03 +03:00
|
|
|
|
2023-11-02 21:07:00 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(receiver);
|
2023-12-15 00:44:38 +03:00
|
|
|
if (safe_nav) {
|
2024-02-01 00:59:03 +03:00
|
|
|
lskip = NEW_LABEL(lineno);
|
2023-12-15 00:44:38 +03:00
|
|
|
PM_DUP;
|
2024-02-01 00:59:03 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchnil, lskip);
|
2023-12-15 00:44:38 +03:00
|
|
|
}
|
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2024-02-01 00:59:03 +03:00
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, id_read_name, INT2FIX(0), INT2FIX(flag));
|
2023-10-25 18:34:03 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
|
|
|
|
|
|
|
if (and_node) {
|
2024-02-01 00:59:03 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lcfin);
|
2023-10-25 18:34:03 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-02-01 00:59:03 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, lcfin);
|
2023-10-25 18:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-11-02 21:07:00 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(value);
|
2024-02-01 00:59:03 +03:00
|
|
|
|
2023-10-25 18:34:03 +03:00
|
|
|
if (!popped) {
|
|
|
|
PM_SWAP;
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
ID id_write_name = pm_constant_id_lookup(scope_node, write_name);
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, id_write_name, INT2FIX(1), INT2FIX(flag));
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, lfin);
|
2023-10-25 18:34:03 +03:00
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
ADD_LABEL(ret, lcfin);
|
|
|
|
if (!popped) PM_SWAP;
|
2023-12-15 00:44:38 +03:00
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
ADD_LABEL(ret, lfin);
|
|
|
|
|
|
|
|
if (lskip && popped) ADD_LABEL(ret, lskip);
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_POP;
|
2024-02-01 00:59:03 +03:00
|
|
|
if (lskip && !popped) ADD_LABEL(ret, lskip);
|
2023-10-25 18:34:03 +03:00
|
|
|
}
|
|
|
|
|
2024-02-13 07:13:35 +03:00
|
|
|
/**
|
|
|
|
* This function compiles a hash onto the stack. It is used to compile hash
|
|
|
|
* literals and keyword arguments. It is assumed that if we get here that the
|
|
|
|
* contents of the hash are not popped.
|
|
|
|
*/
|
2024-01-17 23:29:37 +03:00
|
|
|
static void
|
2024-02-13 07:13:35 +03:00
|
|
|
pm_compile_hash_elements(const pm_node_list_t *elements, int lineno, rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
|
2024-01-17 23:29:37 +03:00
|
|
|
{
|
2024-02-13 07:13:35 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
2024-01-17 23:29:37 +03:00
|
|
|
|
2024-02-13 07:13:35 +03:00
|
|
|
// If this element is not popped, then we need to create the
|
|
|
|
// hash on the stack. Neighboring plain assoc nodes should be
|
|
|
|
// grouped together (either by newhash or hash merge). Double
|
|
|
|
// splat nodes should be merged using the mege_kwd method call.
|
|
|
|
int assoc_length = 0;
|
|
|
|
bool made_hash = false;
|
2024-01-17 23:29:37 +03:00
|
|
|
|
2024-02-13 07:13:35 +03:00
|
|
|
for (size_t index = 0; index < elements->size; index++) {
|
|
|
|
const pm_node_t *element = elements->nodes[index];
|
2024-01-17 23:29:37 +03:00
|
|
|
|
2024-02-13 07:13:35 +03:00
|
|
|
switch (PM_NODE_TYPE(element)) {
|
|
|
|
case PM_ASSOC_NODE: {
|
|
|
|
// If this is a plain assoc node, then we can compile it directly
|
|
|
|
// and then add to the number of assoc nodes we've seen so far.
|
|
|
|
PM_COMPILE_NOT_POPPED(element);
|
|
|
|
assoc_length++;
|
2024-01-17 23:29:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_ASSOC_SPLAT_NODE: {
|
2024-02-13 07:13:35 +03:00
|
|
|
// If we are at a splat and we have already compiled some elements
|
|
|
|
// of the hash, then we need to either create the first hash or
|
|
|
|
// merge the current elements into the existing hash.
|
|
|
|
if (assoc_length > 0) {
|
|
|
|
if (!made_hash) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(assoc_length * 2));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
PM_SWAP;
|
|
|
|
made_hash = true;
|
2024-01-17 23:29:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-02-13 07:13:35 +03:00
|
|
|
// Here we are merging plain assoc nodes into the hash on
|
|
|
|
// the stack.
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_ptr, INT2FIX(assoc_length * 2 + 1));
|
|
|
|
|
|
|
|
// Since we already have a hash on the stack, we need to set
|
|
|
|
// up the method call for the next merge that will occur.
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
2024-01-17 23:29:37 +03:00
|
|
|
PM_SWAP;
|
|
|
|
}
|
|
|
|
|
2024-02-13 07:13:35 +03:00
|
|
|
assoc_length = 0;
|
2024-01-24 18:22:46 +03:00
|
|
|
}
|
2024-01-17 23:29:37 +03:00
|
|
|
|
2024-02-13 07:13:35 +03:00
|
|
|
// If this is the first time we've seen a splat, then we need to
|
|
|
|
// create a hash that we can merge into.
|
|
|
|
if (!made_hash) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(0));
|
|
|
|
made_hash = true;
|
2024-01-17 23:29:37 +03:00
|
|
|
}
|
|
|
|
|
2024-02-13 07:13:35 +03:00
|
|
|
// Now compile the splat node itself and merge it into the hash.
|
|
|
|
PM_COMPILE_NOT_POPPED(element);
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_kwd, INT2FIX(2));
|
|
|
|
|
|
|
|
// We know that any subsequent elements will need to be merged in
|
|
|
|
// using one of the special core methods. So here we will put the
|
|
|
|
// receiver of the merge and then swap it with the hash that is
|
|
|
|
// going to be the first argument.
|
|
|
|
if (index != elements->size - 1) {
|
2024-01-17 23:29:37 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
PM_SWAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2024-02-13 07:13:35 +03:00
|
|
|
default:
|
|
|
|
RUBY_ASSERT("Invalid node type for hash" && false);
|
|
|
|
break;
|
2024-01-17 23:29:37 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-13 07:13:35 +03:00
|
|
|
|
|
|
|
if (!made_hash) {
|
|
|
|
// If we haven't already made the hash, then this means we only saw
|
|
|
|
// plain assoc nodes. In this case, we can just create the hash
|
|
|
|
// directly.
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(assoc_length * 2));
|
|
|
|
}
|
|
|
|
else if (assoc_length > 0) {
|
|
|
|
// If we have already made the hash, then we need to merge the remaining
|
|
|
|
// assoc nodes into the hash on the stack.
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_ptr, INT2FIX(assoc_length * 2 + 1));
|
|
|
|
}
|
2024-01-17 23:29:37 +03:00
|
|
|
}
|
|
|
|
|
2024-02-03 03:29:50 +03:00
|
|
|
// This is details. Users should call pm_setup_args() instead.
|
2023-11-27 19:12:24 +03:00
|
|
|
static int
|
2024-02-12 23:48:23 +03:00
|
|
|
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, NODE dummy_line_node)
|
2023-11-27 19:12:24 +03:00
|
|
|
{
|
|
|
|
int orig_argc = 0;
|
[Prism] Fix more method call argumnents
In #2087 it was noted that there was a bug in the number of arguments in
`SplatNode` and `KeywordHashNode`. I looked into this with Aaron before
the linked PR was merged and we found a bunch of cases that weren't
working quite right. This PR aims to fix some of those cases, but there
may be more.
A splat argument followed by a positional argument will concat the array
until the end or unless the argument is a kwarg or splat kwarg. For
example
```
foo(a, *b, c, *d, e)
```
Will have an `argc` of 2, because `b`, `c`, `d`, and `e` will be
concatenated together.
```
foo(a, *b, c, *d, **e)
```
Will have an `argc` of 3, because `b`, `c`, and `d` will be concatenated
together and `e` is a separate argument.
2024-01-03 22:23:44 +03:00
|
|
|
bool has_splat = false;
|
|
|
|
bool has_keyword_splat = false;
|
|
|
|
|
2023-11-27 19:12:24 +03:00
|
|
|
if (arguments_node == NULL) {
|
|
|
|
if (*flags & VM_CALL_FCALL) {
|
|
|
|
*flags |= VM_CALL_VCALL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_node_list_t arguments_node_list = arguments_node->arguments;
|
|
|
|
|
[Prism] Fix more method call argumnents
In #2087 it was noted that there was a bug in the number of arguments in
`SplatNode` and `KeywordHashNode`. I looked into this with Aaron before
the linked PR was merged and we found a bunch of cases that weren't
working quite right. This PR aims to fix some of those cases, but there
may be more.
A splat argument followed by a positional argument will concat the array
until the end or unless the argument is a kwarg or splat kwarg. For
example
```
foo(a, *b, c, *d, e)
```
Will have an `argc` of 2, because `b`, `c`, `d`, and `e` will be
concatenated together.
```
foo(a, *b, c, *d, **e)
```
Will have an `argc` of 3, because `b`, `c`, and `d` will be concatenated
together and `e` is a separate argument.
2024-01-03 22:23:44 +03:00
|
|
|
has_keyword_splat = (arguments_node->base.flags & PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
|
2023-11-27 19:12:24 +03:00
|
|
|
|
|
|
|
// We count the number of elements post the splat node that are not keyword elements to
|
|
|
|
// eventually pass as an argument to newarray
|
|
|
|
int post_splat_counter = 0;
|
|
|
|
|
|
|
|
for (size_t index = 0; index < arguments_node_list.size; index++) {
|
|
|
|
pm_node_t *argument = arguments_node_list.nodes[index];
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(argument)) {
|
2024-01-17 22:45:05 +03:00
|
|
|
// A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
|
2023-11-27 19:12:24 +03:00
|
|
|
case PM_KEYWORD_HASH_NODE: {
|
2024-01-17 22:45:05 +03:00
|
|
|
pm_keyword_hash_node_t *keyword_arg = (pm_keyword_hash_node_t *)argument;
|
2023-11-27 19:12:24 +03:00
|
|
|
|
2024-01-17 22:45:05 +03:00
|
|
|
if (has_keyword_splat || has_splat) {
|
|
|
|
*flags |= VM_CALL_KW_SPLAT;
|
|
|
|
has_keyword_splat = true;
|
2024-02-13 07:13:35 +03:00
|
|
|
pm_compile_hash_elements(&keyword_arg->elements, nd_line(&dummy_line_node), iseq, ret, scope_node);
|
2024-01-17 22:45:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-01-17 23:29:37 +03:00
|
|
|
size_t len = keyword_arg->elements.size;
|
|
|
|
|
2023-12-07 02:36:00 +03:00
|
|
|
// We need to first figure out if all elements of the KeywordHashNode are AssocNodes
|
2023-12-14 21:15:30 +03:00
|
|
|
// with symbol keys.
|
|
|
|
if (PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_SYMBOL_KEYS)) {
|
|
|
|
// If they are all symbol keys then we can pass them as keyword arguments.
|
2023-12-07 02:36:00 +03:00
|
|
|
*kw_arg = rb_xmalloc_mul_add(len, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
|
2023-12-14 21:15:30 +03:00
|
|
|
*flags |= VM_CALL_KWARG;
|
2023-12-07 02:36:00 +03:00
|
|
|
VALUE *keywords = (*kw_arg)->keywords;
|
|
|
|
(*kw_arg)->references = 0;
|
|
|
|
(*kw_arg)->keyword_len = (int)len;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
pm_assoc_node_t *assoc = (pm_assoc_node_t *)keyword_arg->elements.nodes[i];
|
|
|
|
pm_node_t *key = assoc->key;
|
2024-02-12 23:48:23 +03:00
|
|
|
keywords[i] = pm_static_literal_value(key, scope_node);
|
2023-12-07 02:36:00 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(assoc->value);
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-14 21:15:30 +03:00
|
|
|
// If they aren't all symbol keys then we need to construct a new hash
|
2023-12-07 02:36:00 +03:00
|
|
|
// and pass that as an argument.
|
|
|
|
orig_argc++;
|
2023-12-14 21:15:30 +03:00
|
|
|
*flags |= VM_CALL_KW_SPLAT;
|
|
|
|
if (len > 1) {
|
|
|
|
// A new hash will be created for the keyword arguments in this case,
|
|
|
|
// so mark the method as passing mutable keyword splat.
|
|
|
|
*flags |= VM_CALL_KW_SPLAT_MUT;
|
|
|
|
}
|
2023-12-07 02:36:00 +03:00
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
pm_assoc_node_t *assoc = (pm_assoc_node_t *)keyword_arg->elements.nodes[i];
|
|
|
|
PM_COMPILE_NOT_POPPED(assoc->key);
|
|
|
|
PM_COMPILE_NOT_POPPED(assoc->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(len * 2));
|
|
|
|
}
|
2024-01-17 22:45:05 +03:00
|
|
|
}
|
|
|
|
break;
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
|
|
|
case PM_SPLAT_NODE: {
|
2024-01-17 22:45:05 +03:00
|
|
|
*flags |= VM_CALL_ARGS_SPLAT;
|
|
|
|
pm_splat_node_t *splat_node = (pm_splat_node_t *)argument;
|
2024-01-26 02:08:44 +03:00
|
|
|
|
2024-01-17 22:45:05 +03:00
|
|
|
if (splat_node->expression) {
|
|
|
|
PM_COMPILE_NOT_POPPED(splat_node->expression);
|
|
|
|
}
|
2024-01-24 00:13:18 +03:00
|
|
|
else {
|
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
|
|
|
}
|
2024-01-17 22:45:05 +03:00
|
|
|
|
|
|
|
bool first_splat = !has_splat;
|
|
|
|
|
|
|
|
if (first_splat) {
|
|
|
|
// If this is the first splat array seen and it's not the
|
|
|
|
// last parameter, we want splatarray to dup it.
|
|
|
|
//
|
|
|
|
// foo(a, *b, c)
|
|
|
|
// ^^
|
2024-02-03 03:29:50 +03:00
|
|
|
if (index + 1 < arguments_node_list.size || has_regular_blockarg) {
|
2024-01-17 22:45:05 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qtrue);
|
2024-01-25 18:33:48 +03:00
|
|
|
*flags |= VM_CALL_ARGS_SPLAT_MUT;
|
2024-01-17 22:45:05 +03:00
|
|
|
}
|
|
|
|
// If this is the first spalt array seen and it's the last
|
|
|
|
// parameter, we don't want splatarray to dup it.
|
|
|
|
//
|
|
|
|
// foo(a, *b)
|
|
|
|
// ^^
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qfalse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If this is not the first splat array seen and it is also
|
2024-01-22 19:36:55 +03:00
|
|
|
// the last parameter, we don't want splatarray to dup it
|
2024-01-17 22:45:05 +03:00
|
|
|
// and we need to concat the array.
|
|
|
|
//
|
|
|
|
// foo(a, *b, *c)
|
|
|
|
// ^^
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qfalse);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
}
|
|
|
|
|
|
|
|
has_splat = true;
|
|
|
|
post_splat_counter = 0;
|
|
|
|
|
|
|
|
break;
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
|
|
|
case PM_FORWARDING_ARGUMENTS_NODE: {
|
2024-01-26 02:08:44 +03:00
|
|
|
orig_argc += 2;
|
|
|
|
*flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_SPLAT_MUT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
|
|
|
|
|
|
|
|
// Forwarding arguments nodes are treated as foo(*, **, &)
|
|
|
|
// So foo(...) equals foo(*, **, &) and as such the local
|
|
|
|
// table for this method is known in advance
|
|
|
|
//
|
|
|
|
// Push the *
|
|
|
|
pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, mult_local.index, mult_local.level);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qtrue);
|
|
|
|
|
|
|
|
// Push the **
|
|
|
|
pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, pow_local.index, pow_local.level);
|
|
|
|
|
|
|
|
// Push the &
|
|
|
|
pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getblockparamproxy, INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1), INT2FIX(and_local.level));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, splatkw);
|
2024-01-24 21:43:26 +03:00
|
|
|
|
2024-01-17 22:45:05 +03:00
|
|
|
break;
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
|
|
|
default: {
|
2024-01-17 22:45:05 +03:00
|
|
|
post_splat_counter++;
|
|
|
|
PM_COMPILE_NOT_POPPED(argument);
|
|
|
|
|
|
|
|
// If we have a splat and we've seen a splat, we need to process
|
|
|
|
// everything after the splat.
|
|
|
|
if (has_splat) {
|
|
|
|
// Stack items are turned into an array and concatenated in
|
|
|
|
// the following cases:
|
|
|
|
//
|
|
|
|
// If the next node is a splat:
|
|
|
|
//
|
|
|
|
// foo(*a, b, *c)
|
|
|
|
//
|
|
|
|
// If the next node is a kwarg or kwarg splat:
|
|
|
|
//
|
|
|
|
// foo(*a, b, c: :d)
|
|
|
|
// foo(*a, b, **c)
|
|
|
|
//
|
|
|
|
// If the next node is NULL (we have hit the end):
|
|
|
|
//
|
|
|
|
// foo(*a, b)
|
|
|
|
if (index == arguments_node_list.size - 1) {
|
|
|
|
RUBY_ASSERT(post_splat_counter > 0);
|
2024-02-06 18:02:49 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, pushtoarray, INT2FIX(post_splat_counter));
|
2024-01-17 22:45:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_node_t *next_arg = arguments_node_list.nodes[index + 1];
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(next_arg)) {
|
|
|
|
// A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
|
|
|
|
case PM_KEYWORD_HASH_NODE: {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(post_splat_counter));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_SPLAT_NODE: {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(post_splat_counter));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
break;
|
2023-11-29 22:58:26 +03:00
|
|
|
}
|
2024-01-17 22:45:05 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
orig_argc++;
|
|
|
|
}
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
[Prism] Fix more method call argumnents
In #2087 it was noted that there was a bug in the number of arguments in
`SplatNode` and `KeywordHashNode`. I looked into this with Aaron before
the linked PR was merged and we found a bunch of cases that weren't
working quite right. This PR aims to fix some of those cases, but there
may be more.
A splat argument followed by a positional argument will concat the array
until the end or unless the argument is a kwarg or splat kwarg. For
example
```
foo(a, *b, c, *d, e)
```
Will have an `argc` of 2, because `b`, `c`, `d`, and `e` will be
concatenated together.
```
foo(a, *b, c, *d, **e)
```
Will have an `argc` of 3, because `b`, `c`, and `d` will be concatenated
together and `e` is a separate argument.
2024-01-03 22:23:44 +03:00
|
|
|
|
|
|
|
if (has_splat) {
|
|
|
|
orig_argc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_keyword_splat) {
|
|
|
|
orig_argc++;
|
|
|
|
}
|
|
|
|
|
2023-11-27 19:12:24 +03:00
|
|
|
return orig_argc;
|
|
|
|
}
|
|
|
|
|
2024-02-03 03:29:50 +03:00
|
|
|
// Compile the argument parts of a call
|
|
|
|
static int
|
2024-02-12 23:48:23 +03:00
|
|
|
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, NODE dummy_line_node)
|
2024-02-03 03:29:50 +03:00
|
|
|
{
|
|
|
|
if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
|
|
|
|
// We compile the `&block_arg` expression first and stitch it later
|
|
|
|
// since the nature of the expression influences whether splat should
|
|
|
|
// duplicate the array.
|
|
|
|
bool regular_block_arg = true;
|
|
|
|
DECL_ANCHOR(block_arg);
|
|
|
|
INIT_ANCHOR(block_arg);
|
|
|
|
pm_compile_node(iseq, block, block_arg, false, scope_node);
|
|
|
|
|
|
|
|
*flags |= VM_CALL_ARGS_BLOCKARG;
|
|
|
|
|
|
|
|
if (LIST_INSN_SIZE_ONE(block_arg)) {
|
|
|
|
LINK_ELEMENT *elem = FIRST_ELEMENT(block_arg);
|
|
|
|
if (IS_INSN(elem)) {
|
|
|
|
INSN *iobj = (INSN *)elem;
|
|
|
|
if (iobj->insn_id == BIN(getblockparam)) {
|
|
|
|
iobj->insn_id = BIN(getblockparamproxy);
|
|
|
|
}
|
|
|
|
// Allow splat without duplication for simple one-instruction
|
|
|
|
// block arguments like `&arg`. It is known that this optimization
|
|
|
|
// can be too aggressive in some cases. See [Bug #16504].
|
|
|
|
regular_block_arg = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 23:48:23 +03:00
|
|
|
int argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, iseq, ret, scope_node, dummy_line_node);
|
2024-02-03 03:29:50 +03:00
|
|
|
ADD_SEQ(ret, block_arg);
|
|
|
|
return argc;
|
|
|
|
}
|
2024-02-12 23:48:23 +03:00
|
|
|
return pm_setup_args_core(arguments_node, block, flags, false, kw_arg, iseq, ret, scope_node, dummy_line_node);
|
2024-02-03 03:29:50 +03:00
|
|
|
}
|
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
/**
|
|
|
|
* Compile an index operator write node, which is a node that is writing a value
|
|
|
|
* using the [] and []= methods. It looks like:
|
|
|
|
*
|
|
|
|
* foo[bar] += baz
|
|
|
|
*
|
|
|
|
* This breaks down to caching the receiver and arguments on the stack, calling
|
|
|
|
* the [] method, calling the operator method with the result of the [] method,
|
|
|
|
* and then calling the []= method with the result of the operator method.
|
|
|
|
*/
|
2023-11-27 19:12:24 +03:00
|
|
|
static void
|
2024-02-01 05:25:21 +03:00
|
|
|
pm_compile_index_operator_write_node(pm_scope_node_t *scope_node, const pm_index_operator_write_node_t *node, rb_iseq_t *iseq, LINK_ANCHOR *const ret, bool popped)
|
2023-11-27 19:12:24 +03:00
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, (const pm_node_t *) node);
|
2024-02-01 05:25:21 +03:00
|
|
|
const NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
2023-11-27 19:12:24 +03:00
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
if (!popped) {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
2023-11-27 19:12:24 +03:00
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(node->receiver);
|
2023-11-27 19:12:24 +03:00
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
int boff = (node->block == NULL ? 0 : 1);
|
|
|
|
int flag = PM_NODE_TYPE_P(node->receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
|
2024-01-22 18:46:12 +03:00
|
|
|
struct rb_callinfo_kwarg *keywords = NULL;
|
2024-02-12 23:48:23 +03:00
|
|
|
int argc = pm_setup_args(node->arguments, node->block, &flag, &keywords, iseq, ret, scope_node, dummy_line_node);
|
2023-11-27 19:12:24 +03:00
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
|
|
|
|
if (boff) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, splatkw);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, splatkw);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 19:12:24 +03:00
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
int dup_argn = argc + 1 + boff;
|
|
|
|
int keyword_len = 0;
|
|
|
|
|
|
|
|
if (keywords) {
|
|
|
|
keyword_len = keywords->keyword_len;
|
|
|
|
dup_argn += keyword_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, dupn, INT2FIX(dup_argn));
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(node->value);
|
|
|
|
|
|
|
|
ID id_operator = pm_constant_id_lookup(scope_node, node->operator);
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_operator, INT2FIX(1));
|
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(dup_argn + 1));
|
|
|
|
}
|
|
|
|
if (flag & VM_CALL_ARGS_SPLAT) {
|
|
|
|
if (flag & VM_CALL_KW_SPLAT) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(2 + boff));
|
|
|
|
|
|
|
|
if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qtrue);
|
|
|
|
flag |= VM_CALL_ARGS_SPLAT_MUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, pushtoarray, INT2FIX(1));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(2 + boff));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (boff > 0) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, dupn, INT2FIX(3));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qtrue);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
flag |= VM_CALL_ARGS_SPLAT_MUT;
|
|
|
|
}
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, pushtoarray, INT2FIX(1));
|
|
|
|
if (boff > 0) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(3));
|
|
|
|
PM_POP;
|
|
|
|
PM_POP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
|
|
|
|
}
|
|
|
|
else if (flag & VM_CALL_KW_SPLAT) {
|
|
|
|
if (boff > 0) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(2));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(3));
|
|
|
|
PM_POP;
|
|
|
|
}
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
|
|
|
|
}
|
|
|
|
else if (keyword_len) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, opt_reverse, INT2FIX(keyword_len + boff + 2));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, opt_reverse, INT2FIX(keyword_len + boff + 1));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (boff > 0) {
|
|
|
|
PM_SWAP;
|
|
|
|
}
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
|
|
|
|
}
|
|
|
|
PM_POP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile an index control flow write node, which is a node that is writing a
|
|
|
|
* value using the [] and []= methods and the &&= and ||= operators. It looks
|
|
|
|
* like:
|
|
|
|
*
|
|
|
|
* foo[bar] ||= baz
|
|
|
|
*
|
|
|
|
* This breaks down to caching the receiver and arguments on the stack, calling
|
|
|
|
* the [] method, checking the result and then changing control flow based on
|
|
|
|
* it. If the value would result in a write, then the value is written using the
|
|
|
|
* []= method.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pm_compile_index_control_flow_write_node(pm_scope_node_t *scope_node, 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, rb_iseq_t *iseq, LINK_ANCHOR *const ret, bool popped)
|
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, node);
|
2024-02-01 05:25:21 +03:00
|
|
|
const NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(receiver);
|
|
|
|
|
|
|
|
int boff = (block == NULL ? 0 : 1);
|
|
|
|
int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
|
|
|
|
struct rb_callinfo_kwarg *keywords = NULL;
|
2024-02-12 23:48:23 +03:00
|
|
|
int argc = pm_setup_args(arguments, block, &flag, &keywords, iseq, ret, scope_node, dummy_line_node);
|
2023-11-27 19:12:24 +03:00
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
|
|
|
|
if (boff) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, splatkw);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, splatkw);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int dup_argn = argc + 1 + boff;
|
|
|
|
int keyword_len = 0;
|
|
|
|
|
|
|
|
if (keywords) {
|
|
|
|
keyword_len = keywords->keyword_len;
|
|
|
|
dup_argn += keyword_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, dupn, INT2FIX(dup_argn));
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idAREF, INT2FIX(argc), NULL, INT2FIX(flag & ~(VM_CALL_ARGS_SPLAT_MUT | VM_CALL_KW_SPLAT_MUT)), keywords);
|
2023-11-27 19:12:24 +03:00
|
|
|
|
|
|
|
LABEL *label = NEW_LABEL(lineno);
|
|
|
|
LABEL *lfin = NEW_LABEL(lineno);
|
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
if (PM_NODE_TYPE_P(node, PM_INDEX_AND_WRITE_NODE)) {
|
2023-11-27 19:12:24 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, label);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, label);
|
|
|
|
}
|
|
|
|
|
|
|
|
PM_POP;
|
|
|
|
PM_COMPILE_NOT_POPPED(value);
|
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(dup_argn + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag & VM_CALL_ARGS_SPLAT) {
|
|
|
|
if (flag & VM_CALL_KW_SPLAT) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(2 + boff));
|
|
|
|
if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qtrue);
|
|
|
|
flag |= VM_CALL_ARGS_SPLAT_MUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, pushtoarray, INT2FIX(1));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(2 + boff));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (boff > 0) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, dupn, INT2FIX(3));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
if (!(flag & VM_CALL_ARGS_SPLAT_MUT)) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qtrue);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
flag |= VM_CALL_ARGS_SPLAT_MUT;
|
|
|
|
}
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, pushtoarray, INT2FIX(1));
|
|
|
|
if (boff > 0) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(3));
|
|
|
|
PM_POP;
|
|
|
|
PM_POP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idASET, INT2FIX(argc), NULL, INT2FIX(flag), keywords);
|
|
|
|
}
|
|
|
|
else if (flag & VM_CALL_KW_SPLAT) {
|
|
|
|
if (boff > 0) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(2));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(3));
|
|
|
|
PM_POP;
|
|
|
|
}
|
2023-11-27 20:48:51 +03:00
|
|
|
|
2024-02-01 05:25:21 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
|
|
|
|
}
|
|
|
|
else if (keyword_len) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, opt_reverse, INT2FIX(keyword_len + boff + 1));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, opt_reverse, INT2FIX(keyword_len + boff + 0));
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (boff > 0) {
|
|
|
|
PM_SWAP;
|
|
|
|
}
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, idASET, INT2FIX(argc + 1), NULL, INT2FIX(flag), keywords);
|
|
|
|
}
|
|
|
|
PM_POP;
|
2023-11-27 19:12:24 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, lfin);
|
|
|
|
ADD_LABEL(ret, label);
|
|
|
|
if (!popped) {
|
2024-02-01 05:25:21 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(dup_argn + 1));
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
2024-02-01 05:25:21 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, adjuststack, INT2FIX(dup_argn + 1));
|
2023-11-27 19:12:24 +03:00
|
|
|
ADD_LABEL(ret, lfin);
|
|
|
|
}
|
|
|
|
|
2023-12-13 19:34:05 +03:00
|
|
|
// When we compile a pattern matching expression, we use the stack as a scratch
|
|
|
|
// space to store lots of different values (consider it like we have a pattern
|
|
|
|
// matching function and we need space for a bunch of different local
|
|
|
|
// variables). The "base index" refers to the index on the stack where we
|
|
|
|
// started compiling the pattern matching expression. These offsets from that
|
|
|
|
// base index indicate the location of the various locals we need.
|
|
|
|
#define PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE 0
|
|
|
|
#define PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING 1
|
|
|
|
#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P 2
|
|
|
|
#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE 3
|
|
|
|
#define PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY 4
|
|
|
|
|
|
|
|
// A forward declaration because this is the recursive function that handles
|
|
|
|
// compiling a pattern. It can be reentered by nesting patterns, as in the case
|
|
|
|
// of arrays or hashes.
|
2024-01-31 20:17:31 +03:00
|
|
|
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);
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function generates the code to set up the error string and error_p
|
|
|
|
* locals depending on whether or not the pattern matched.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
pm_line_node_t line;
|
|
|
|
pm_line_node(&line, scope_node, node);
|
|
|
|
|
|
|
|
LABEL *match_succeeded_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSNL(ret, &line.node, branchif, match_succeeded_label);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, message);
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(3));
|
|
|
|
ADD_SEND(ret, &line.node, id_core_sprintf, INT2FIX(2));
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, Qfalse);
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_LABEL(ret, match_succeeded_label);
|
|
|
|
|
|
|
|
return COMPILE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function generates the code to set up the error string and error_p
|
|
|
|
* locals depending on whether or not the pattern matched when the value needs
|
|
|
|
* to match a specific deconstructed length.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
pm_line_node_t line;
|
|
|
|
pm_line_node(&line, scope_node, node);
|
|
|
|
|
|
|
|
LABEL *match_succeeded_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSNL(ret, &line.node, branchif, match_succeeded_label);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, message);
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(3));
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_SEND(ret, &line.node, idLength, INT2FIX(0));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, length);
|
|
|
|
ADD_SEND(ret, &line.node, id_core_sprintf, INT2FIX(4));
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, Qfalse);
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_LABEL(ret, match_succeeded_label);
|
|
|
|
|
|
|
|
return COMPILE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function generates the code to set up the error string and error_p
|
|
|
|
* locals depending on whether or not the pattern matched when the value needs
|
|
|
|
* to pass a specific #=== method call.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
pm_line_node_t line;
|
|
|
|
pm_line_node(&line, scope_node, node);
|
|
|
|
|
|
|
|
LABEL *match_succeeded_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSNL(ret, &line.node, branchif, match_succeeded_label);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_fstring_lit("%p === %p does not return true"));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(3));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(5));
|
|
|
|
ADD_SEND(ret, &line.node, id_core_sprintf, INT2FIX(3));
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, Qfalse);
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, match_succeeded_label);
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(2));
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
|
|
|
|
return COMPILE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a variation on compiling a pattern matching expression that is used
|
|
|
|
* to have the pattern matching instructions fall through to immediately after
|
|
|
|
* the pattern if it passes. Otherwise it jumps to the given unmatched_label
|
|
|
|
* label.
|
|
|
|
*/
|
|
|
|
static int
|
2024-01-31 20:17:31 +03:00
|
|
|
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)
|
2023-12-13 19:34:05 +03:00
|
|
|
{
|
|
|
|
LABEL *matched_label = NEW_LABEL(nd_line(node));
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
|
2023-12-13 19:34:05 +03:00
|
|
|
ADD_LABEL(ret, matched_label);
|
|
|
|
return COMPILE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function compiles in the code necessary to call #deconstruct on the
|
|
|
|
* value to match against. It raises appropriate errors if the method does not
|
|
|
|
* exist or if it returns the wrong type.
|
|
|
|
*/
|
|
|
|
static int
|
2024-01-31 20:17:31 +03:00
|
|
|
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)
|
2023-12-13 19:34:05 +03:00
|
|
|
{
|
|
|
|
pm_line_node_t line;
|
|
|
|
pm_line_node(&line, scope_node, node);
|
|
|
|
|
|
|
|
if (use_deconstructed_cache) {
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
|
|
|
|
ADD_INSNL(ret, &line.node, branchnil, deconstruct_label);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_failed_label);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE - 1));
|
|
|
|
ADD_INSNL(ret, &line.node, jump, deconstructed_label);
|
|
|
|
} else {
|
|
|
|
ADD_INSNL(ret, &line.node, jump, deconstruct_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_LABEL(ret, deconstruct_label);
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, ID2SYM(rb_intern("deconstruct")));
|
|
|
|
ADD_SEND(ret, &line.node, idRespond_to, INT2FIX(1));
|
|
|
|
|
|
|
|
if (use_deconstructed_cache) {
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_single_pattern) {
|
|
|
|
CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct"), base_index + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_failed_label);
|
|
|
|
ADD_SEND(ret, &line.node, rb_intern("deconstruct"), INT2FIX(0));
|
|
|
|
|
|
|
|
if (use_deconstructed_cache) {
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSN1(ret, &line.node, checktype, INT2FIX(T_ARRAY));
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, type_error_label);
|
|
|
|
ADD_LABEL(ret, deconstructed_label);
|
|
|
|
|
|
|
|
return COMPILE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function compiles in the code necessary to match against the optional
|
|
|
|
* constant path that is attached to an array, find, or hash pattern.
|
|
|
|
*/
|
|
|
|
static int
|
2024-01-31 20:17:31 +03:00
|
|
|
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)
|
2023-12-13 19:34:05 +03:00
|
|
|
{
|
|
|
|
pm_line_node_t line;
|
|
|
|
pm_line_node(&line, scope_node, node);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
PM_COMPILE_NOT_POPPED(node);
|
|
|
|
|
|
|
|
if (in_single_pattern) {
|
|
|
|
ADD_INSN1(ret, &line.node, dupn, INT2FIX(2));
|
|
|
|
}
|
|
|
|
ADD_INSN1(ret, &line.node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
|
|
|
|
if (in_single_pattern) {
|
|
|
|
CHECK(pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 3));
|
|
|
|
}
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_failed_label);
|
|
|
|
return COMPILE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When matching fails, an appropriate error must be raised. This function is
|
|
|
|
* responsible for compiling in those error raising instructions.
|
|
|
|
*/
|
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
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)
|
2023-12-13 19:34:05 +03:00
|
|
|
{
|
|
|
|
pm_line_node_t line;
|
|
|
|
pm_line_node(&line, scope_node, node);
|
|
|
|
|
|
|
|
LABEL *key_error_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *cleanup_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
struct rb_callinfo_kwarg *kw_arg = rb_xmalloc_mul_add(2, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
|
|
|
|
kw_arg->references = 0;
|
|
|
|
kw_arg->keyword_len = 2;
|
|
|
|
kw_arg->keywords[0] = ID2SYM(rb_intern("matchee"));
|
|
|
|
kw_arg->keywords[1] = ID2SYM(rb_intern("key"));
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
|
|
|
|
ADD_INSNL(ret, &line.node, branchif, key_error_label);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_eNoMatchingPatternError);
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_fstring_lit("%p: %s"));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(4));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
|
|
|
|
ADD_SEND(ret, &line.node, id_core_sprintf, INT2FIX(3));
|
|
|
|
ADD_SEND(ret, &line.node, id_core_raise, INT2FIX(2));
|
|
|
|
ADD_INSNL(ret, &line.node, jump, cleanup_label);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, key_error_label);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_eNoMatchingPatternKeyError);
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_fstring_lit("%p: %s"));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(4));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
|
|
|
|
ADD_SEND(ret, &line.node, id_core_sprintf, INT2FIX(3));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
|
|
|
|
ADD_SEND_R(ret, &line.node, rb_intern("new"), INT2FIX(1), NULL, INT2FIX(VM_CALL_KWARG), kw_arg);
|
|
|
|
ADD_SEND(ret, &line.node, id_core_raise, INT2FIX(1));
|
|
|
|
ADD_LABEL(ret, cleanup_label);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, adjuststack, INT2FIX(7));
|
|
|
|
if (!popped) ADD_INSN(ret, &line.node, putnil);
|
|
|
|
ADD_INSNL(ret, &line.node, jump, done_label);
|
|
|
|
ADD_INSN1(ret, &line.node, dupn, INT2FIX(5));
|
|
|
|
if (popped) ADD_INSN(ret, &line.node, putnil);
|
|
|
|
}
|
|
|
|
|
2023-09-28 19:14:38 +03:00
|
|
|
/**
|
|
|
|
* Compile a pattern matching expression.
|
|
|
|
*/
|
2023-09-28 19:36:17 +03:00
|
|
|
static int
|
2024-01-31 20:17:31 +03:00
|
|
|
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)
|
2023-09-28 19:14:38 +03:00
|
|
|
{
|
2023-12-13 19:34:05 +03:00
|
|
|
pm_line_node_t line;
|
|
|
|
pm_line_node(&line, scope_node, node);
|
2023-09-28 19:14:38 +03:00
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
2023-12-13 19:34:05 +03:00
|
|
|
case PM_ARRAY_PATTERN_NODE: {
|
|
|
|
// Array patterns in pattern matching are triggered by using commas in
|
|
|
|
// a pattern or wrapping it in braces. They are represented by a
|
|
|
|
// ArrayPatternNode. This looks like:
|
|
|
|
//
|
|
|
|
// foo => [1, 2, 3]
|
|
|
|
//
|
|
|
|
// It can optionally have a splat in the middle of it, which can
|
|
|
|
// optionally have a name attached.
|
|
|
|
const pm_array_pattern_node_t *cast = (const pm_array_pattern_node_t *) node;
|
|
|
|
|
|
|
|
const size_t requireds_size = cast->requireds.size;
|
|
|
|
const size_t posts_size = cast->posts.size;
|
|
|
|
const size_t minimum_size = requireds_size + posts_size;
|
|
|
|
|
2024-02-05 20:25:44 +03:00
|
|
|
bool rest_named = false;
|
|
|
|
bool use_rest_size = false;
|
|
|
|
|
|
|
|
if (cast->rest != NULL) {
|
|
|
|
rest_named = (PM_NODE_TYPE_P(cast->rest, PM_SPLAT_NODE) && ((const pm_splat_node_t *) cast->rest)->expression != NULL);
|
|
|
|
use_rest_size = (rest_named || (!rest_named && posts_size > 0));
|
|
|
|
}
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
LABEL *match_failed_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *type_error_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *deconstruct_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *deconstructed_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
if (use_rest_size) {
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(0));
|
|
|
|
ADD_INSN(ret, &line.node, swap);
|
|
|
|
base_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cast->constant != NULL) {
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
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));
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_SEND(ret, &line.node, idLength, INT2FIX(0));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(minimum_size));
|
|
|
|
ADD_SEND(ret, &line.node, cast->rest == NULL ? idEq : idGE, INT2FIX(1));
|
|
|
|
if (in_single_pattern) {
|
|
|
|
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+)");
|
|
|
|
CHECK(pm_compile_pattern_length_error(iseq, scope_node, node, ret, message, INT2FIX(minimum_size), base_index + 1));
|
|
|
|
}
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_failed_label);
|
|
|
|
|
|
|
|
for (size_t index = 0; index < requireds_size; index++) {
|
|
|
|
const pm_node_t *required = cast->requireds.nodes[index];
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(index));
|
|
|
|
ADD_SEND(ret, &line.node, idAREF, INT2FIX(1));
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cast->rest != NULL) {
|
2024-02-05 20:25:44 +03:00
|
|
|
if (rest_named) {
|
2023-12-13 19:34:05 +03:00
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(requireds_size));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(1));
|
|
|
|
ADD_SEND(ret, &line.node, idLength, INT2FIX(0));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(minimum_size));
|
|
|
|
ADD_SEND(ret, &line.node, idMINUS, INT2FIX(1));
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(4));
|
|
|
|
ADD_SEND(ret, &line.node, idAREF, INT2FIX(2));
|
2024-01-31 20:17:31 +03:00
|
|
|
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));
|
2023-12-13 19:34:05 +03:00
|
|
|
} else if (posts_size > 0) {
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_SEND(ret, &line.node, idLength, INT2FIX(0));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(minimum_size));
|
|
|
|
ADD_SEND(ret, &line.node, idMINUS, INT2FIX(1));
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(2));
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t index = 0; index < posts_size; index++) {
|
|
|
|
const pm_node_t *post = cast->posts.nodes[index];
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(requireds_size + index));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(3));
|
|
|
|
ADD_SEND(ret, &line.node, idPLUS, INT2FIX(1));
|
|
|
|
ADD_SEND(ret, &line.node, idAREF, INT2FIX(1));
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
if (use_rest_size) {
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &line.node, jump, matched_label);
|
|
|
|
ADD_INSN(ret, &line.node, putnil);
|
|
|
|
if (use_rest_size) {
|
|
|
|
ADD_INSN(ret, &line.node, putnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_LABEL(ret, type_error_label);
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_eTypeError);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_fstring_lit("deconstruct must return Array"));
|
|
|
|
ADD_SEND(ret, &line.node, id_core_raise, INT2FIX(2));
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, match_failed_label);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
if (use_rest_size) {
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &line.node, jump, unmatched_label);
|
2023-09-28 19:19:50 +03:00
|
|
|
break;
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
case PM_FIND_PATTERN_NODE: {
|
|
|
|
// Find patterns in pattern matching are triggered by using commas in
|
|
|
|
// a pattern or wrapping it in braces and using a splat on both the left
|
|
|
|
// and right side of the pattern. This looks like:
|
|
|
|
//
|
|
|
|
// foo => [*, 1, 2, 3, *]
|
|
|
|
//
|
|
|
|
// There can be any number of requireds in the middle. The splats on
|
|
|
|
// both sides can optionally have names attached.
|
|
|
|
const pm_find_pattern_node_t *cast = (const pm_find_pattern_node_t *) node;
|
|
|
|
const size_t size = cast->requireds.size;
|
|
|
|
|
|
|
|
LABEL *match_failed_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *type_error_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *deconstruct_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *deconstructed_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
if (cast->constant) {
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
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));
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_SEND(ret, &line.node, idLength, INT2FIX(0));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(size));
|
|
|
|
ADD_SEND(ret, &line.node, idGE, INT2FIX(1));
|
|
|
|
if (in_single_pattern) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_failed_label);
|
|
|
|
|
|
|
|
{
|
|
|
|
LABEL *while_begin_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *next_loop_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *find_succeeded_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *find_failed_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_SEND(ret, &line.node, idLength, INT2FIX(0));
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(size));
|
|
|
|
ADD_SEND(ret, &line.node, idMINUS, INT2FIX(1));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(0));
|
|
|
|
ADD_LABEL(ret, while_begin_label);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(2));
|
|
|
|
ADD_SEND(ret, &line.node, idLE, INT2FIX(1));
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, find_failed_label);
|
|
|
|
|
|
|
|
for (size_t index = 0; index < size; index++) {
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(3));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(1));
|
|
|
|
|
|
|
|
if (index != 0) {
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(index));
|
|
|
|
ADD_SEND(ret, &line.node, idPLUS, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEND(ret, &line.node, idAREF, INT2FIX(1));
|
2024-01-31 20:17:31 +03:00
|
|
|
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));
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(cast->left, PM_SPLAT_NODE));
|
2023-12-13 19:34:05 +03:00
|
|
|
const pm_splat_node_t *left = (const pm_splat_node_t *) cast->left;
|
|
|
|
|
|
|
|
if (left->expression != NULL) {
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(3));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(0));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(2));
|
|
|
|
ADD_SEND(ret, &line.node, idAREF, INT2FIX(2));
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern_match(iseq, scope_node, left->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4));
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(cast->right, PM_SPLAT_NODE));
|
2023-12-13 19:34:05 +03:00
|
|
|
const pm_splat_node_t *right = (const pm_splat_node_t *) cast->right;
|
|
|
|
|
|
|
|
if (right->expression != NULL) {
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(3));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(1));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(size));
|
|
|
|
ADD_SEND(ret, &line.node, idPLUS, INT2FIX(1));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(3));
|
|
|
|
ADD_SEND(ret, &line.node, idAREF, INT2FIX(2));
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4);
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &line.node, jump, find_succeeded_label);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, next_loop_label);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, INT2FIX(1));
|
|
|
|
ADD_SEND(ret, &line.node, idPLUS, INT2FIX(1));
|
|
|
|
ADD_INSNL(ret, &line.node, jump, while_begin_label);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, find_failed_label);
|
|
|
|
ADD_INSN1(ret, &line.node, adjuststack, INT2FIX(3));
|
|
|
|
if (in_single_pattern) {
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_fstring_lit("%p does not match to find pattern"));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(2));
|
|
|
|
ADD_SEND(ret, &line.node, id_core_sprintf, INT2FIX(2));
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, Qfalse);
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
}
|
|
|
|
ADD_INSNL(ret, &line.node, jump, match_failed_label);
|
|
|
|
ADD_INSN1(ret, &line.node, dupn, INT2FIX(3));
|
|
|
|
|
|
|
|
ADD_LABEL(ret, find_succeeded_label);
|
|
|
|
ADD_INSN1(ret, &line.node, adjuststack, INT2FIX(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSNL(ret, &line.node, jump, matched_label);
|
|
|
|
ADD_INSN(ret, &line.node, putnil);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, type_error_label);
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_eTypeError);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_fstring_lit("deconstruct must return Array"));
|
|
|
|
ADD_SEND(ret, &line.node, id_core_raise, INT2FIX(2));
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, match_failed_label);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSNL(ret, &line.node, jump, unmatched_label);
|
|
|
|
|
2023-09-28 19:36:17 +03:00
|
|
|
break;
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
case PM_HASH_PATTERN_NODE: {
|
|
|
|
// Hash patterns in pattern matching are triggered by using labels and
|
|
|
|
// values in a pattern or by using the ** operator. They are represented
|
|
|
|
// by the HashPatternNode. This looks like:
|
2023-09-28 19:51:33 +03:00
|
|
|
//
|
2023-12-13 19:34:05 +03:00
|
|
|
// foo => { a: 1, b: 2, **bar }
|
2023-09-28 19:51:33 +03:00
|
|
|
//
|
2023-12-13 19:34:05 +03:00
|
|
|
// It can optionally have an assoc splat in the middle of it, which can
|
|
|
|
// optionally have a name.
|
|
|
|
const pm_hash_pattern_node_t *cast = (const pm_hash_pattern_node_t *) node;
|
2023-09-28 19:51:33 +03:00
|
|
|
|
2023-12-13 19:34:05 +03:00
|
|
|
// We don't consider it a "rest" parameter if it's a ** that is unnamed.
|
|
|
|
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);
|
|
|
|
bool has_keys = cast->elements.size > 0 || cast->rest != NULL;
|
|
|
|
|
|
|
|
LABEL *match_failed_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *type_error_label = NEW_LABEL(line.lineno);
|
|
|
|
VALUE keys = Qnil;
|
|
|
|
|
|
|
|
if (has_keys && !has_rest) {
|
|
|
|
keys = rb_ary_new_capa(cast->elements.size);
|
|
|
|
|
|
|
|
for (size_t index = 0; index < cast->elements.size; index++) {
|
|
|
|
const pm_node_t *element = cast->elements.nodes[index];
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
const pm_node_t *key = ((const pm_assoc_node_t *) element)->key;
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(key, PM_SYMBOL_NODE));
|
2023-12-13 19:34:05 +03:00
|
|
|
|
2024-01-26 23:18:21 +03:00
|
|
|
VALUE symbol = ID2SYM(parse_string_symbol((const pm_symbol_node_t *)key, scope_node->parser));
|
2023-12-13 19:34:05 +03:00
|
|
|
rb_ary_push(keys, symbol);
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 19:51:33 +03:00
|
|
|
|
2023-12-13 19:34:05 +03:00
|
|
|
if (cast->constant) {
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern_constant(iseq, scope_node, cast->constant, ret, match_failed_label, in_single_pattern, base_index));
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, ID2SYM(rb_intern("deconstruct_keys")));
|
|
|
|
ADD_SEND(ret, &line.node, idRespond_to, INT2FIX(1));
|
|
|
|
if (in_single_pattern) {
|
|
|
|
CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct_keys"), base_index + 1));
|
|
|
|
}
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_failed_label);
|
|
|
|
|
|
|
|
if (NIL_P(keys)) {
|
|
|
|
ADD_INSN(ret, &line.node, putnil);
|
|
|
|
} else {
|
|
|
|
ADD_INSN1(ret, &line.node, duparray, keys);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, rb_obj_hide(keys));
|
|
|
|
}
|
|
|
|
ADD_SEND(ret, &line.node, rb_intern("deconstruct_keys"), INT2FIX(1));
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSN1(ret, &line.node, checktype, INT2FIX(T_HASH));
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, type_error_label);
|
|
|
|
|
|
|
|
if (has_rest) {
|
|
|
|
ADD_SEND(ret, &line.node, rb_intern("dup"), INT2FIX(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_keys) {
|
|
|
|
DECL_ANCHOR(match_values);
|
|
|
|
INIT_ANCHOR(match_values);
|
|
|
|
|
|
|
|
for (size_t index = 0; index < cast->elements.size; index++) {
|
|
|
|
const pm_node_t *element = cast->elements.nodes[index];
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(element, PM_ASSOC_NODE));
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
|
|
|
|
const pm_node_t *key = assoc->key;
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(key, PM_SYMBOL_NODE));
|
2023-12-13 19:34:05 +03:00
|
|
|
|
2024-01-26 23:18:21 +03:00
|
|
|
VALUE symbol = ID2SYM(parse_string_symbol((const pm_symbol_node_t *)key, scope_node->parser));
|
2023-12-13 19:34:05 +03:00
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, symbol);
|
|
|
|
ADD_SEND(ret, &line.node, rb_intern("key?"), INT2FIX(1));
|
|
|
|
|
|
|
|
if (in_single_pattern) {
|
|
|
|
LABEL *match_succeeded_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_INSNL(ret, &line.node, branchif, match_succeeded_label);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_str_freeze(rb_sprintf("key not found: %+"PRIsVALUE, symbol)));
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, Qtrue);
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
|
|
|
|
ADD_INSN1(ret, &line.node, topn, INT2FIX(3));
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE + 4));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, symbol);
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY + 5));
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, adjuststack, INT2FIX(4));
|
|
|
|
ADD_LABEL(ret, match_succeeded_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_failed_label);
|
|
|
|
ADD_INSN(match_values, &line.node, dup);
|
|
|
|
ADD_INSN1(match_values, &line.node, putobject, symbol);
|
|
|
|
ADD_SEND(match_values, &line.node, has_rest ? rb_intern("delete") : idAREF, INT2FIX(1));
|
|
|
|
|
2024-01-24 21:43:04 +03:00
|
|
|
const pm_node_t *value = assoc->value;
|
|
|
|
if (PM_NODE_TYPE_P(value, PM_IMPLICIT_NODE)) {
|
|
|
|
value = ((const pm_implicit_node_t *) value)->value;
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEQ(ret, match_values);
|
|
|
|
} else {
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_SEND(ret, &line.node, idEmptyP, INT2FIX(0));
|
|
|
|
if (in_single_pattern) {
|
|
|
|
CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p is not empty"), base_index + 1));
|
|
|
|
}
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_failed_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_rest) {
|
|
|
|
switch (PM_NODE_TYPE(cast->rest)) {
|
|
|
|
case PM_NO_KEYWORDS_PARAMETER_NODE: {
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
ADD_SEND(ret, &line.node, idEmptyP, INT2FIX(0));
|
|
|
|
if (in_single_pattern) {
|
|
|
|
pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("rest of %p is not empty"), base_index + 1);
|
|
|
|
}
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_failed_label);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_ASSOC_SPLAT_NODE: {
|
|
|
|
const pm_assoc_splat_node_t *splat = (const pm_assoc_splat_node_t *) cast->rest;
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1);
|
2023-12-13 19:34:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
rb_bug("unreachable");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSNL(ret, &line.node, jump, matched_label);
|
|
|
|
ADD_INSN(ret, &line.node, putnil);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, type_error_label);
|
|
|
|
ADD_INSN1(ret, &line.node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_eTypeError);
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_fstring_lit("deconstruct_keys must return Hash"));
|
|
|
|
ADD_SEND(ret, &line.node, id_core_raise, INT2FIX(2));
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, match_failed_label);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSNL(ret, &line.node, jump, unmatched_label);
|
2023-09-28 19:51:33 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-12-13 19:34:05 +03:00
|
|
|
case PM_CAPTURE_PATTERN_NODE: {
|
|
|
|
// Capture patterns allow you to pattern match against an element in a
|
|
|
|
// pattern and also capture the value into a local variable. This looks
|
|
|
|
// like:
|
2023-09-28 19:51:33 +03:00
|
|
|
//
|
2023-12-13 19:34:05 +03:00
|
|
|
// [1] => [Integer => foo]
|
2023-09-28 19:51:33 +03:00
|
|
|
//
|
2023-12-13 19:34:05 +03:00
|
|
|
// In this case the `Integer => foo` will be represented by a
|
|
|
|
// CapturePatternNode, which has both a value (the pattern to match
|
|
|
|
// against) and a target (the place to write the variable into).
|
|
|
|
const pm_capture_pattern_node_t *cast = (const pm_capture_pattern_node_t *) node;
|
2023-09-28 19:51:33 +03:00
|
|
|
|
2023-12-13 19:34:05 +03:00
|
|
|
LABEL *match_failed_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
2024-01-31 20:17:31 +03:00
|
|
|
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));
|
|
|
|
CHECK(pm_compile_pattern(iseq, scope_node, cast->target, ret, matched_label, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index));
|
2023-12-13 19:34:05 +03:00
|
|
|
ADD_INSN(ret, &line.node, putnil);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, match_failed_label);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSNL(ret, &line.node, jump, unmatched_label);
|
2023-09-28 19:51:33 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-09-28 19:36:17 +03:00
|
|
|
case PM_LOCAL_VARIABLE_TARGET_NODE: {
|
|
|
|
// Local variables can be targetted by placing identifiers in the place
|
|
|
|
// of a pattern. For example, foo in bar. This results in the value
|
|
|
|
// being matched being written to that local variable.
|
|
|
|
pm_local_variable_target_node_t *cast = (pm_local_variable_target_node_t *) node;
|
2024-01-12 23:23:47 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
|
2023-09-28 19:36:17 +03:00
|
|
|
|
|
|
|
// If this local variable is being written from within an alternation
|
|
|
|
// pattern, then it cannot actually be added to the local table since
|
|
|
|
// it's ambiguous which value should be used. So instead we indicate
|
|
|
|
// this with a compile error.
|
|
|
|
if (in_alternation_pattern) {
|
2023-10-17 01:36:25 +03:00
|
|
|
ID id = pm_constant_id_lookup(scope_node, cast->name);
|
2023-09-28 19:51:33 +03:00
|
|
|
const char *name = rb_id2name(id);
|
|
|
|
|
2023-09-28 19:36:17 +03:00
|
|
|
if (name && strlen(name) > 0 && name[0] != '_') {
|
2023-09-28 19:51:33 +03:00
|
|
|
COMPILE_ERROR(ERROR_ARGS "illegal variable in alternative pattern (%"PRIsVALUE")", rb_id2str(id));
|
2023-09-28 19:36:17 +03:00
|
|
|
return COMPILE_NG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &line.node, index.index, index.level);
|
2023-12-13 19:34:05 +03:00
|
|
|
ADD_INSNL(ret, &line.node, jump, matched_label);
|
2023-09-28 19:36:17 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-09-28 19:27:04 +03:00
|
|
|
case PM_ALTERNATION_PATTERN_NODE: {
|
|
|
|
// Alternation patterns allow you to specify multiple patterns in a
|
|
|
|
// single expression using the | operator.
|
|
|
|
pm_alternation_pattern_node_t *cast = (pm_alternation_pattern_node_t *) node;
|
|
|
|
|
2023-12-13 19:34:05 +03:00
|
|
|
LABEL *matched_left_label = NEW_LABEL(line.lineno);
|
|
|
|
LABEL *unmatched_left_label = NEW_LABEL(line.lineno);
|
2023-09-28 19:27:04 +03:00
|
|
|
|
|
|
|
// First, we're going to attempt to match against the left pattern. If
|
|
|
|
// that pattern matches, then we'll skip matching the right pattern.
|
2023-12-13 19:34:05 +03:00
|
|
|
ADD_INSN(ret, &line.node, dup);
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern(iseq, scope_node, cast->left, ret, matched_left_label, unmatched_left_label, in_single_pattern, true, true, base_index + 1));
|
2023-09-28 19:27:04 +03:00
|
|
|
|
|
|
|
// If we get here, then we matched on the left pattern. In this case we
|
|
|
|
// should pop out the duplicate value that we preemptively added to
|
|
|
|
// match against the right pattern and then jump to the match label.
|
|
|
|
ADD_LABEL(ret, matched_left_label);
|
2023-12-13 19:34:05 +03:00
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSNL(ret, &line.node, jump, matched_label);
|
|
|
|
ADD_INSN(ret, &line.node, putnil);
|
2023-09-28 19:27:04 +03:00
|
|
|
|
|
|
|
// If we get here, then we didn't match on the left pattern. In this
|
|
|
|
// case we attempt to match against the right pattern.
|
|
|
|
ADD_LABEL(ret, unmatched_left_label);
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern(iseq, scope_node, cast->right, ret, matched_label, unmatched_label, in_single_pattern, true, true, base_index));
|
2023-09-28 19:19:50 +03:00
|
|
|
break;
|
2023-09-28 19:27:04 +03:00
|
|
|
}
|
2024-01-24 22:24:13 +03:00
|
|
|
case PM_PINNED_EXPRESSION_NODE:
|
|
|
|
// Pinned expressions are a way to match against the value of an
|
|
|
|
// expression that should be evaluated at runtime. This looks like:
|
|
|
|
// foo in ^(bar). To compile these, we compile the expression as if it
|
|
|
|
// were a literal value by falling through to the literal case.
|
|
|
|
node = ((pm_pinned_expression_node_t *) node)->expression;
|
|
|
|
/* fallthrough */
|
2023-09-28 19:14:38 +03:00
|
|
|
case PM_ARRAY_NODE:
|
|
|
|
case PM_CLASS_VARIABLE_READ_NODE:
|
|
|
|
case PM_CONSTANT_PATH_NODE:
|
|
|
|
case PM_CONSTANT_READ_NODE:
|
|
|
|
case PM_FALSE_NODE:
|
|
|
|
case PM_FLOAT_NODE:
|
|
|
|
case PM_GLOBAL_VARIABLE_READ_NODE:
|
|
|
|
case PM_IMAGINARY_NODE:
|
|
|
|
case PM_INSTANCE_VARIABLE_READ_NODE:
|
|
|
|
case PM_INTEGER_NODE:
|
|
|
|
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
|
|
|
|
case PM_INTERPOLATED_STRING_NODE:
|
|
|
|
case PM_INTERPOLATED_SYMBOL_NODE:
|
|
|
|
case PM_INTERPOLATED_X_STRING_NODE:
|
|
|
|
case PM_LAMBDA_NODE:
|
|
|
|
case PM_LOCAL_VARIABLE_READ_NODE:
|
|
|
|
case PM_NIL_NODE:
|
2024-01-24 22:24:13 +03:00
|
|
|
case PM_SOURCE_ENCODING_NODE:
|
|
|
|
case PM_SOURCE_FILE_NODE:
|
|
|
|
case PM_SOURCE_LINE_NODE:
|
2023-09-28 19:14:38 +03:00
|
|
|
case PM_RANGE_NODE:
|
|
|
|
case PM_RATIONAL_NODE:
|
|
|
|
case PM_REGULAR_EXPRESSION_NODE:
|
|
|
|
case PM_SELF_NODE:
|
|
|
|
case PM_STRING_NODE:
|
|
|
|
case PM_SYMBOL_NODE:
|
|
|
|
case PM_TRUE_NODE:
|
2023-12-13 19:34:05 +03:00
|
|
|
case PM_X_STRING_NODE: {
|
2023-09-28 19:58:19 +03:00
|
|
|
// These nodes are all simple patterns, which means we'll use the
|
|
|
|
// checkmatch instruction to match against them, which is effectively a
|
|
|
|
// VM-level === operator.
|
2023-09-28 19:14:38 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(node);
|
2023-12-13 19:34:05 +03:00
|
|
|
if (in_single_pattern) {
|
|
|
|
ADD_INSN1(ret, &line.node, dupn, INT2FIX(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
|
|
|
|
|
|
|
|
if (in_single_pattern) {
|
|
|
|
pm_compile_pattern_eqq_error(iseq, scope_node, node, ret, base_index + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &line.node, branchif, matched_label);
|
|
|
|
ADD_INSNL(ret, &line.node, jump, unmatched_label);
|
2023-09-28 19:14:38 +03:00
|
|
|
break;
|
2023-12-13 19:34:05 +03:00
|
|
|
}
|
2023-09-28 19:14:38 +03:00
|
|
|
case PM_PINNED_VARIABLE_NODE: {
|
2023-09-28 19:58:19 +03:00
|
|
|
// Pinned variables are a way to match against the value of a variable
|
|
|
|
// without it looking like you're trying to write to the variable. This
|
|
|
|
// looks like: foo in ^@bar. To compile these, we compile the variable
|
|
|
|
// that they hold.
|
2023-09-28 19:14:38 +03:00
|
|
|
pm_pinned_variable_node_t *cast = (pm_pinned_variable_node_t *) node;
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern(iseq, scope_node, cast->variable, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, true, base_index));
|
2023-09-28 19:14:38 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-12-13 19:34:05 +03:00
|
|
|
case PM_IF_NODE:
|
|
|
|
case PM_UNLESS_NODE: {
|
|
|
|
// If and unless nodes can show up here as guards on `in` clauses. This
|
|
|
|
// looks like:
|
|
|
|
//
|
|
|
|
// case foo
|
|
|
|
// in bar if baz?
|
|
|
|
// qux
|
|
|
|
// end
|
|
|
|
//
|
|
|
|
// Because we know they're in the modifier form and they can't have any
|
|
|
|
// variation on this pattern, we compile them differently (more simply)
|
|
|
|
// here than we would in the normal compilation path.
|
|
|
|
const pm_node_t *predicate;
|
|
|
|
const pm_node_t *statement;
|
|
|
|
|
|
|
|
if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
|
|
|
|
const pm_if_node_t *cast = (const pm_if_node_t *) node;
|
|
|
|
predicate = cast->predicate;
|
|
|
|
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
|
2023-12-13 19:34:05 +03:00
|
|
|
statement = cast->statements->body.nodes[0];
|
|
|
|
} else {
|
|
|
|
const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
|
|
|
|
predicate = cast->predicate;
|
|
|
|
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(cast->statements != NULL && cast->statements->body.size == 1);
|
2023-12-13 19:34:05 +03:00
|
|
|
statement = cast->statements->body.nodes[0];
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
|
2023-12-13 19:34:05 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(predicate);
|
|
|
|
|
|
|
|
if (in_single_pattern) {
|
|
|
|
LABEL *match_succeeded_label = NEW_LABEL(line.lineno);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, dup);
|
|
|
|
if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
|
|
|
|
ADD_INSNL(ret, &line.node, branchif, match_succeeded_label);
|
|
|
|
} else {
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, match_succeeded_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, rb_fstring_lit("guard clause does not return true"));
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
|
|
|
|
ADD_INSN1(ret, &line.node, putobject, Qfalse);
|
|
|
|
ADD_INSN1(ret, &line.node, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
|
|
|
|
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
ADD_INSN(ret, &line.node, pop);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, match_succeeded_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PM_NODE_TYPE_P(node, PM_IF_NODE)) {
|
|
|
|
ADD_INSNL(ret, &line.node, branchunless, unmatched_label);
|
|
|
|
} else {
|
|
|
|
ADD_INSNL(ret, &line.node, branchif, unmatched_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &line.node, jump, matched_label);
|
2023-09-28 19:14:38 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2023-09-28 19:58:19 +03:00
|
|
|
// If we get here, then we have a node type that should not be in this
|
|
|
|
// position. This would be a bug in the parser, because a different node
|
|
|
|
// type should never have been created in this position in the tree.
|
2023-09-28 19:14:38 +03:00
|
|
|
rb_bug("Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
|
|
|
break;
|
|
|
|
}
|
2023-09-28 19:51:33 +03:00
|
|
|
|
|
|
|
return COMPILE_OK;
|
2023-09-28 19:14:38 +03:00
|
|
|
}
|
|
|
|
|
2023-12-13 19:34:05 +03:00
|
|
|
#undef PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE
|
|
|
|
#undef PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING
|
|
|
|
#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P
|
|
|
|
#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_MATCHEE
|
|
|
|
#undef PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_KEY
|
|
|
|
|
2023-10-24 22:54:44 +03:00
|
|
|
// Generate a scope node from the given node.
|
|
|
|
void
|
2024-02-08 18:14:27 +03:00
|
|
|
pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous, const pm_parser_t *parser)
|
2023-10-24 22:54:44 +03:00
|
|
|
{
|
2024-02-09 23:54:00 +03:00
|
|
|
// This is very important, otherwise the scope node could be seen as having
|
|
|
|
// certain flags set that _should not_ be set.
|
|
|
|
memset(scope, 0, sizeof(pm_scope_node_t));
|
|
|
|
|
2023-10-24 22:54:44 +03:00
|
|
|
scope->base.type = PM_SCOPE_NODE;
|
|
|
|
scope->base.location.start = node->location.start;
|
|
|
|
scope->base.location.end = node->location.end;
|
|
|
|
|
|
|
|
scope->previous = previous;
|
|
|
|
scope->parser = parser;
|
|
|
|
scope->ast_node = (pm_node_t *)node;
|
2023-12-06 18:05:14 +03:00
|
|
|
|
2023-10-24 22:54:44 +03:00
|
|
|
if (previous) {
|
|
|
|
scope->constants = previous->constants;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
2024-02-02 22:36:08 +03:00
|
|
|
case PM_BLOCK_NODE: {
|
|
|
|
pm_block_node_t *cast = (pm_block_node_t *) node;
|
|
|
|
scope->body = cast->body;
|
|
|
|
scope->locals = cast->locals;
|
|
|
|
scope->parameters = cast->parameters;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_CLASS_NODE: {
|
|
|
|
pm_class_node_t *cast = (pm_class_node_t *) node;
|
|
|
|
scope->body = cast->body;
|
|
|
|
scope->locals = cast->locals;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_DEF_NODE: {
|
|
|
|
pm_def_node_t *cast = (pm_def_node_t *) node;
|
|
|
|
scope->parameters = (pm_node_t *)cast->parameters;
|
|
|
|
scope->body = cast->body;
|
|
|
|
scope->locals = cast->locals;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_ENSURE_NODE: {
|
|
|
|
scope->body = (pm_node_t *)node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_FOR_NODE: {
|
|
|
|
pm_for_node_t *cast = (pm_for_node_t *)node;
|
|
|
|
scope->body = (pm_node_t *)cast->statements;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
|
|
|
|
RUBY_ASSERT(node->flags & PM_REGULAR_EXPRESSION_FLAGS_ONCE);
|
|
|
|
scope->body = (pm_node_t *)node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_LAMBDA_NODE: {
|
|
|
|
pm_lambda_node_t *cast = (pm_lambda_node_t *) node;
|
|
|
|
scope->parameters = cast->parameters;
|
|
|
|
scope->body = cast->body;
|
|
|
|
scope->locals = cast->locals;
|
2024-02-07 19:51:22 +03:00
|
|
|
|
|
|
|
if (cast->parameters != NULL) {
|
|
|
|
scope->base.location.start = cast->parameters->location.start;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scope->base.location.start = cast->operator_loc.end;
|
|
|
|
}
|
2024-02-02 22:36:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_MODULE_NODE: {
|
|
|
|
pm_module_node_t *cast = (pm_module_node_t *) node;
|
|
|
|
scope->body = cast->body;
|
|
|
|
scope->locals = cast->locals;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_POST_EXECUTION_NODE: {
|
|
|
|
pm_post_execution_node_t *cast = (pm_post_execution_node_t *) node;
|
|
|
|
scope->body = (pm_node_t *) cast->statements;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_PROGRAM_NODE: {
|
|
|
|
pm_program_node_t *cast = (pm_program_node_t *) node;
|
|
|
|
scope->body = (pm_node_t *) cast->statements;
|
|
|
|
scope->locals = cast->locals;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_RESCUE_NODE: {
|
|
|
|
pm_rescue_node_t *cast = (pm_rescue_node_t *)node;
|
|
|
|
scope->body = (pm_node_t *)cast->statements;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_RESCUE_MODIFIER_NODE: {
|
|
|
|
pm_rescue_modifier_node_t *cast = (pm_rescue_modifier_node_t *)node;
|
|
|
|
scope->body = (pm_node_t *)cast->rescue_expression;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_SINGLETON_CLASS_NODE: {
|
|
|
|
pm_singleton_class_node_t *cast = (pm_singleton_class_node_t *) node;
|
|
|
|
scope->body = cast->body;
|
|
|
|
scope->locals = cast->locals;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_STATEMENTS_NODE: {
|
|
|
|
pm_statements_node_t *cast = (pm_statements_node_t *) node;
|
|
|
|
scope->body = (pm_node_t *)cast;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2024-02-13 23:14:55 +03:00
|
|
|
rb_bug("unreachable");
|
2024-02-02 22:36:08 +03:00
|
|
|
break;
|
2024-02-09 23:54:00 +03:00
|
|
|
}
|
2023-10-24 22:54:44 +03:00
|
|
|
}
|
|
|
|
|
2024-01-18 19:55:31 +03:00
|
|
|
void
|
|
|
|
pm_scope_node_destroy(pm_scope_node_t *scope_node)
|
|
|
|
{
|
|
|
|
if (scope_node->index_lookup_table) {
|
|
|
|
st_free_table(scope_node->index_lookup_table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
static void 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);
|
2023-12-06 01:07:50 +03:00
|
|
|
|
2023-10-25 11:07:26 +03:00
|
|
|
void
|
2024-02-08 23:46:53 +03:00
|
|
|
pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, int lineno, bool in_condition, LABEL **lfinish, bool explicit_receiver)
|
2023-10-25 11:07:26 +03:00
|
|
|
{
|
|
|
|
// in_condition is the same as compile.c's needstr
|
|
|
|
enum defined_type dtype = DEFINED_NOT_DEFINED;
|
2024-02-06 17:22:30 +03:00
|
|
|
|
2023-10-27 00:54:38 +03:00
|
|
|
switch (PM_NODE_TYPE(node)) {
|
2023-12-06 01:07:50 +03:00
|
|
|
case PM_ARGUMENTS_NODE: {
|
|
|
|
const pm_arguments_node_t *cast = (pm_arguments_node_t *) node;
|
|
|
|
const pm_node_list_t *arguments = &cast->arguments;
|
|
|
|
for (size_t idx = 0; idx < arguments->size; idx++) {
|
|
|
|
const pm_node_t *argument = arguments->nodes[idx];
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_defined_expr0(iseq, argument, ret, popped, scope_node, dummy_line_node, lineno, in_condition, lfinish, explicit_receiver);
|
2023-12-06 01:07:50 +03:00
|
|
|
|
|
|
|
if (!lfinish[1]) {
|
|
|
|
lfinish[1] = NEW_LABEL(lineno);
|
|
|
|
}
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfinish[1]);
|
|
|
|
}
|
|
|
|
dtype = DEFINED_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2023-12-01 22:52:38 +03:00
|
|
|
case PM_NIL_NODE:
|
2023-10-25 11:07:26 +03:00
|
|
|
dtype = DEFINED_NIL;
|
|
|
|
break;
|
2023-12-04 23:01:51 +03:00
|
|
|
case PM_PARENTHESES_NODE: {
|
2024-02-06 17:22:30 +03:00
|
|
|
const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
|
2023-12-04 23:01:51 +03:00
|
|
|
|
2024-02-06 17:22:30 +03:00
|
|
|
if (cast->body == NULL) {
|
|
|
|
// If we have empty parentheses, then we want to return "nil".
|
|
|
|
dtype = DEFINED_NIL;
|
|
|
|
}
|
|
|
|
else if (PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE) && ((const pm_statements_node_t *) cast->body)->body.size == 1) {
|
|
|
|
// If we have a parentheses node that is wrapping a single statement
|
|
|
|
// then we want to recurse down to that statement and compile it.
|
|
|
|
pm_compile_defined_expr0(iseq, ((const pm_statements_node_t *) cast->body)->body.nodes[0], ret, popped, scope_node, dummy_line_node, lineno, in_condition, lfinish, explicit_receiver);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Otherwise, we have parentheses wrapping multiple statements, in
|
|
|
|
// which case this is defined as "expression".
|
|
|
|
dtype = DEFINED_EXPR;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2023-12-04 23:01:51 +03:00
|
|
|
}
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_SELF_NODE:
|
|
|
|
dtype = DEFINED_SELF;
|
|
|
|
break;
|
|
|
|
case PM_TRUE_NODE:
|
|
|
|
dtype = DEFINED_TRUE;
|
|
|
|
break;
|
|
|
|
case PM_FALSE_NODE:
|
|
|
|
dtype = DEFINED_FALSE;
|
|
|
|
break;
|
2023-10-27 20:44:41 +03:00
|
|
|
case PM_ARRAY_NODE: {
|
2024-02-06 17:22:30 +03:00
|
|
|
pm_array_node_t *cast = (pm_array_node_t *) node;
|
|
|
|
|
|
|
|
if (!PM_NODE_FLAG_P(cast, PM_ARRAY_NODE_FLAGS_CONTAINS_SPLAT)) {
|
|
|
|
for (size_t index = 0; index < cast->elements.size; index++) {
|
|
|
|
pm_compile_defined_expr0(iseq, cast->elements.nodes[index], ret, popped, scope_node, dummy_line_node, lineno, true, lfinish, false);
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
if (!lfinish[1]) {
|
|
|
|
lfinish[1] = NEW_LABEL(lineno);
|
|
|
|
}
|
2024-02-06 17:22:30 +03:00
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfinish[1]);
|
2023-12-05 21:47:46 +03:00
|
|
|
}
|
2023-12-06 01:07:50 +03:00
|
|
|
}
|
2023-10-27 20:44:41 +03:00
|
|
|
}
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_AND_NODE:
|
2024-01-16 23:50:45 +03:00
|
|
|
case PM_BEGIN_NODE:
|
2024-01-16 23:16:31 +03:00
|
|
|
case PM_BREAK_NODE:
|
2024-01-17 18:37:14 +03:00
|
|
|
case PM_CASE_NODE:
|
2024-01-17 18:38:33 +03:00
|
|
|
case PM_CASE_MATCH_NODE:
|
2024-01-17 18:40:57 +03:00
|
|
|
case PM_CLASS_NODE:
|
2024-01-17 18:42:22 +03:00
|
|
|
case PM_DEF_NODE:
|
2024-01-16 23:24:55 +03:00
|
|
|
case PM_DEFINED_NODE:
|
2023-12-01 22:28:54 +03:00
|
|
|
case PM_FLOAT_NODE:
|
2024-01-17 18:44:01 +03:00
|
|
|
case PM_FOR_NODE:
|
2023-12-01 22:28:54 +03:00
|
|
|
case PM_HASH_NODE:
|
2024-01-17 18:45:48 +03:00
|
|
|
case PM_IF_NODE:
|
2023-12-05 20:04:56 +03:00
|
|
|
case PM_IMAGINARY_NODE:
|
2023-12-01 22:28:54 +03:00
|
|
|
case PM_INTEGER_NODE:
|
2023-12-04 23:22:10 +03:00
|
|
|
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
|
2023-12-04 23:10:12 +03:00
|
|
|
case PM_INTERPOLATED_STRING_NODE:
|
2024-01-16 23:31:24 +03:00
|
|
|
case PM_INTERPOLATED_SYMBOL_NODE:
|
2024-01-16 23:40:54 +03:00
|
|
|
case PM_INTERPOLATED_X_STRING_NODE:
|
[PRISM] Implement `PM_KEYWORD_HASH_NODE` for `defined?`
Ruby code:
```ruby
defined? [a: [:b, :c]]
```
Instructions (without optimizations):
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:0 (0,0)-(0,23)>
== catch table
| catch type: rescue st: 0001 ed: 0007 sp: 0000 cont: 0009
| == disasm: #<ISeq:defined guard in <compiled>@<compiled>:0 (0,0)-(-1,-1)>
| local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
| [ 1] "$!"@0
| 0000 putnil
| 0001 leave
|------------------------------------------------------------------------
0000 putnil
0001 putobject true
0003 branchunless 9
0005 putobject "expression"
0007 swap
0008 pop
0009 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:0 (0,0)-(0,23)>
== catch table
| catch type: rescue st: 0000 ed: 0009 sp: 0000 cont: 0009
| == disasm: #<ISeq:defined guard in <compiled>@<compiled>:0 (0,0)-(-1,-1)>
| local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
| [ 1] "$!"@0
| 0000 putnil
| 0001 leave
|------------------------------------------------------------------------
0000 putnil
0001 putobject true
0003 branchunless 9
0005 putobject "expression"
0007 swap
0008 pop
0009 leave
```
Instructions (with optimizations):
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:0 (0,0)-(0,23)>
== catch table
| catch type: rescue st: 0001 ed: 0003 sp: 0000 cont: 0005
| == disasm: #<ISeq:defined guard in <compiled>@<compiled>:0 (0,0)-(-1,-1)>
| local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
| [ 1] "$!"@0
| 0000 putnil
| 0001 leave
|------------------------------------------------------------------------
0000 putnil
0001 putobject "expression"
0003 swap
0004 pop
0005 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:0 (0,0)-(0,23)>
== catch table
| catch type: rescue st: 0000 ed: 0005 sp: 0000 cont: 0005
| == disasm: #<ISeq:defined guard in <compiled>@<compiled>:0 (0,0)-(-1,-1)>
| local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
| [ 1] "$!"@0
| 0000 putnil
| 0001 leave
|------------------------------------------------------------------------
0000 putnil
0001 putobject "expression"
0003 swap
0004 pop
0005 leave
```
2023-12-05 22:06:01 +03:00
|
|
|
case PM_KEYWORD_HASH_NODE:
|
2023-12-01 22:28:54 +03:00
|
|
|
case PM_LAMBDA_NODE:
|
2023-12-05 22:08:21 +03:00
|
|
|
case PM_MATCH_PREDICATE_NODE:
|
2024-01-17 18:49:29 +03:00
|
|
|
case PM_MATCH_REQUIRED_NODE:
|
[Prism] Implement defined? for PM_MATCH_WRITE_NODE
Ruby code:
```ruby
defined?(/(?<foo>bar)/ =~ 'barbar')
```
Instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,35)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] foo@0
0000 putobject "expression" ( 59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,35)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] foo@0
0000 putobject "expression" ( 58)[Li]
0002 leave
```
Related: ruby/prism#2188
2024-01-17 18:47:50 +03:00
|
|
|
case PM_MATCH_WRITE_NODE:
|
2024-01-17 18:50:58 +03:00
|
|
|
case PM_MODULE_NODE:
|
2024-01-16 23:10:39 +03:00
|
|
|
case PM_NEXT_NODE:
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_OR_NODE:
|
2023-12-01 22:28:54 +03:00
|
|
|
case PM_RANGE_NODE:
|
2024-01-17 19:00:08 +03:00
|
|
|
case PM_RATIONAL_NODE:
|
2024-01-16 23:43:24 +03:00
|
|
|
case PM_REDO_NODE:
|
2023-12-01 22:28:54 +03:00
|
|
|
case PM_REGULAR_EXPRESSION_NODE:
|
2024-01-16 23:47:10 +03:00
|
|
|
case PM_RETRY_NODE:
|
2024-01-16 23:44:58 +03:00
|
|
|
case PM_RETURN_NODE:
|
2024-01-17 19:03:28 +03:00
|
|
|
case PM_SINGLETON_CLASS_NODE:
|
2023-12-05 20:07:26 +03:00
|
|
|
case PM_SOURCE_ENCODING_NODE:
|
2023-12-05 20:12:44 +03:00
|
|
|
case PM_SOURCE_FILE_NODE:
|
2023-12-05 20:14:41 +03:00
|
|
|
case PM_SOURCE_LINE_NODE:
|
2023-12-01 22:28:54 +03:00
|
|
|
case PM_STRING_NODE:
|
|
|
|
case PM_SYMBOL_NODE:
|
2024-01-17 19:40:01 +03:00
|
|
|
case PM_UNLESS_NODE:
|
2024-01-17 19:38:02 +03:00
|
|
|
case PM_UNTIL_NODE:
|
2024-01-17 19:36:45 +03:00
|
|
|
case PM_WHILE_NODE:
|
2023-12-04 23:40:43 +03:00
|
|
|
case PM_X_STRING_NODE:
|
2023-10-25 11:07:26 +03:00
|
|
|
dtype = DEFINED_EXPR;
|
|
|
|
break;
|
|
|
|
case PM_LOCAL_VARIABLE_READ_NODE:
|
|
|
|
dtype = DEFINED_LVAR;
|
|
|
|
break;
|
|
|
|
#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
|
|
|
|
case PM_INSTANCE_VARIABLE_READ_NODE: {
|
2023-10-27 00:54:38 +03:00
|
|
|
pm_instance_variable_read_node_t *instance_variable_read_node = (pm_instance_variable_read_node_t *)node;
|
2023-10-25 11:07:26 +03:00
|
|
|
ID id = pm_constant_id_lookup(scope_node, instance_variable_read_node->name);
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, definedivar,
|
|
|
|
ID2SYM(id), get_ivar_ic_value(iseq, id), PUSH_VAL(DEFINED_IVAR));
|
|
|
|
return;
|
|
|
|
}
|
[Prism] Implement backref and numbered reference for `defined?`
This PR implements `PM_BACK_REFERENCE_READ_NODE` and
`PM_NUMBERED_REFERENCE_READ_NODE` for `defined?`. The following now
works:
* `PM_NUMBERED_REFERENCE_READ_NODE`
```
defined? $1
defined? $2
```
Instructions:
```
"********* RUBY *************"
== disasm: #<ISeq:<compiled>@<compiled>:0 (0,0)-(0,12)>
0000 putnil
0001 defined ref, :$1, "global-variable"
0005 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:0 (0,0)-(0,12)>
0000 putnil
0001 defined ref, :$1, "global-variable"
0005 leave
```
* `PM_BACK_REFERENCE_READ_NODE`
```
defined? $'
defined? $`
defined? $&
```
Instructions:
```
"********* RUBY *************"
== disasm: #<ISeq:<compiled>@<compiled>:0 (0,0)-(0,12)>
0000 putnil
0001 defined ref, :$`, "global-variable"
0005 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:0 (0,0)-(0,12)>
0000 putnil
0001 defined ref, :$`, "global-variable"
0005 leave
```
2023-12-04 19:45:42 +03:00
|
|
|
case PM_BACK_REFERENCE_READ_NODE: {
|
|
|
|
char *char_ptr = (char *)(node->location.start) + 1;
|
|
|
|
ID backref_val = INT2FIX(rb_intern2(char_ptr, 1)) << 1 | 1;
|
|
|
|
|
|
|
|
PM_PUTNIL;
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_REF),
|
|
|
|
backref_val,
|
|
|
|
PUSH_VAL(DEFINED_GVAR));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case PM_NUMBERED_REFERENCE_READ_NODE: {
|
|
|
|
uint32_t reference_number = ((pm_numbered_reference_read_node_t *)node)->number;
|
|
|
|
|
|
|
|
PM_PUTNIL;
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_REF),
|
|
|
|
INT2FIX(reference_number << 1),
|
|
|
|
PUSH_VAL(DEFINED_GVAR));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_GLOBAL_VARIABLE_READ_NODE: {
|
2023-10-27 00:54:38 +03:00
|
|
|
pm_global_variable_read_node_t *glabal_variable_read_node = (pm_global_variable_read_node_t *)node;
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-25 11:07:26 +03:00
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_GVAR),
|
|
|
|
ID2SYM(pm_constant_id_lookup(scope_node, glabal_variable_read_node->name)), PUSH_VAL(DEFINED_GVAR));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case PM_CLASS_VARIABLE_READ_NODE: {
|
2023-10-27 00:54:38 +03:00
|
|
|
pm_class_variable_read_node_t *class_variable_read_node = (pm_class_variable_read_node_t *)node;
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-25 11:07:26 +03:00
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_CVAR),
|
|
|
|
ID2SYM(pm_constant_id_lookup(scope_node, class_variable_read_node->name)), PUSH_VAL(DEFINED_CVAR));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case PM_CONSTANT_READ_NODE: {
|
2023-10-27 00:54:38 +03:00
|
|
|
pm_constant_read_node_t *constant_node = (pm_constant_read_node_t *)node;
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-25 11:07:26 +03:00
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_CONST),
|
|
|
|
ID2SYM(pm_constant_id_lookup(scope_node, constant_node->name)), PUSH_VAL(DEFINED_CONST));
|
|
|
|
return;
|
|
|
|
}
|
2023-10-27 00:54:38 +03:00
|
|
|
case PM_CONSTANT_PATH_NODE: {
|
|
|
|
pm_constant_path_node_t *constant_path_node = ((pm_constant_path_node_t *)node);
|
|
|
|
if (constant_path_node->parent) {
|
|
|
|
if (!lfinish[1]) {
|
|
|
|
lfinish[1] = NEW_LABEL(lineno);
|
|
|
|
}
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_defined_expr0(iseq, constant_path_node->parent, ret, popped, scope_node, dummy_line_node, lineno, true, lfinish, false);
|
2023-10-27 00:54:38 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfinish[1]);
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(constant_path_node->parent);
|
2023-12-06 01:07:50 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-10-27 00:54:38 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
|
|
|
|
}
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_CONST_FROM),
|
|
|
|
ID2SYM(pm_constant_id_lookup(scope_node, ((pm_constant_read_node_t *)constant_path_node->child)->name)), PUSH_VAL(DEFINED_CONST));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
case PM_CALL_NODE: {
|
|
|
|
pm_call_node_t *call_node = ((pm_call_node_t *)node);
|
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, call_node->name);
|
|
|
|
|
|
|
|
if (call_node->receiver || call_node->arguments) {
|
|
|
|
if (!lfinish[1]) {
|
|
|
|
lfinish[1] = NEW_LABEL(lineno);
|
|
|
|
}
|
|
|
|
if (!lfinish[2]) {
|
|
|
|
lfinish[2] = NEW_LABEL(lineno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call_node->arguments) {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_defined_expr0(iseq, (const pm_node_t *)call_node->arguments, ret, popped, scope_node, dummy_line_node, lineno, true, lfinish, false);
|
2023-12-06 01:07:50 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfinish[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call_node->receiver) {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_defined_expr0(iseq, call_node->receiver, ret, popped, scope_node, dummy_line_node, lineno, true, lfinish, true);
|
2023-12-06 01:07:50 +03:00
|
|
|
if (PM_NODE_TYPE_P(call_node->receiver, PM_CALL_NODE)) {
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfinish[2]);
|
2024-01-15 23:08:39 +03:00
|
|
|
|
|
|
|
const pm_call_node_t *receiver = (const pm_call_node_t *)call_node->receiver;
|
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
|
2023-12-06 01:07:50 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfinish[1]);
|
|
|
|
PM_COMPILE(call_node->receiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (explicit_receiver) {
|
|
|
|
PM_DUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_METHOD), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
|
|
|
|
}
|
|
|
|
else {
|
2023-12-12 19:08:40 +03:00
|
|
|
PM_PUTSELF;
|
2023-12-06 01:07:50 +03:00
|
|
|
if (explicit_receiver) {
|
|
|
|
PM_DUP;
|
|
|
|
}
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_FUNC), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_YIELD_NODE:
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-25 11:07:26 +03:00
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_YIELD), 0,
|
|
|
|
PUSH_VAL(DEFINED_YIELD));
|
|
|
|
return;
|
|
|
|
case PM_SUPER_NODE:
|
|
|
|
case PM_FORWARDING_SUPER_NODE:
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-25 11:07:26 +03:00
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_ZSUPER), 0,
|
|
|
|
PUSH_VAL(DEFINED_ZSUPER));
|
|
|
|
return;
|
2024-01-17 00:11:24 +03:00
|
|
|
case PM_CALL_AND_WRITE_NODE:
|
2024-01-17 18:01:38 +03:00
|
|
|
case PM_CALL_OPERATOR_WRITE_NODE:
|
2024-01-17 18:03:36 +03:00
|
|
|
case PM_CALL_OR_WRITE_NODE:
|
2024-01-17 18:01:38 +03:00
|
|
|
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_CONSTANT_WRITE_NODE:
|
|
|
|
case PM_CONSTANT_OPERATOR_WRITE_NODE:
|
|
|
|
case PM_CONSTANT_AND_WRITE_NODE:
|
|
|
|
case PM_CONSTANT_OR_WRITE_NODE:
|
|
|
|
|
2024-01-17 18:05:58 +03:00
|
|
|
case PM_CONSTANT_PATH_AND_WRITE_NODE:
|
2024-01-17 18:09:18 +03:00
|
|
|
case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
|
2024-01-17 18:12:49 +03:00
|
|
|
case PM_CONSTANT_PATH_OR_WRITE_NODE:
|
2024-01-17 18:18:02 +03:00
|
|
|
case PM_CONSTANT_PATH_WRITE_NODE:
|
2024-01-17 18:05:58 +03:00
|
|
|
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_GLOBAL_VARIABLE_WRITE_NODE:
|
|
|
|
case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
|
|
|
|
case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
|
|
|
|
case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
|
|
|
|
|
|
|
|
case PM_CLASS_VARIABLE_WRITE_NODE:
|
|
|
|
case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
|
|
|
|
case PM_CLASS_VARIABLE_AND_WRITE_NODE:
|
|
|
|
case PM_CLASS_VARIABLE_OR_WRITE_NODE:
|
|
|
|
|
2024-01-17 18:22:01 +03:00
|
|
|
case PM_INDEX_AND_WRITE_NODE:
|
2024-01-17 18:23:23 +03:00
|
|
|
case PM_INDEX_OPERATOR_WRITE_NODE:
|
2024-01-17 18:24:34 +03:00
|
|
|
case PM_INDEX_OR_WRITE_NODE:
|
2024-01-17 18:22:01 +03:00
|
|
|
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_INSTANCE_VARIABLE_WRITE_NODE:
|
|
|
|
case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
|
|
|
|
case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
|
|
|
|
case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
|
|
|
|
|
|
|
|
case PM_LOCAL_VARIABLE_WRITE_NODE:
|
|
|
|
case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
|
|
|
|
case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
|
|
|
|
case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
|
|
|
|
|
|
|
|
case PM_MULTI_WRITE_NODE:
|
|
|
|
dtype = DEFINED_ASGN;
|
|
|
|
break;
|
|
|
|
default:
|
2023-11-30 01:39:26 +03:00
|
|
|
rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
2023-10-25 11:07:26 +03:00
|
|
|
}
|
|
|
|
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(dtype != DEFINED_NOT_DEFINED);
|
2023-10-25 11:07:26 +03:00
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, PUSH_VAL(dtype));
|
|
|
|
#undef PUSH_VAL
|
|
|
|
}
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, int lineno, bool in_condition, LABEL **lfinish, bool explicit_receiver)
|
2023-10-27 00:54:38 +03:00
|
|
|
{
|
2023-12-06 01:07:50 +03:00
|
|
|
LINK_ELEMENT *lcur = ret->last;
|
2023-10-27 00:54:38 +03:00
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_defined_expr0(iseq, node, ret, popped, scope_node, dummy_line_node, lineno, in_condition, lfinish, false);
|
2023-10-27 00:54:38 +03:00
|
|
|
|
|
|
|
if (lfinish[1]) {
|
2023-11-30 01:39:26 +03:00
|
|
|
LABEL *lstart = NEW_LABEL(lineno);
|
|
|
|
LABEL *lend = NEW_LABEL(lineno);
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
struct rb_iseq_new_with_callback_callback_func *ifunc =
|
|
|
|
rb_iseq_new_with_callback_new_callback(build_defined_rescue_iseq, NULL);
|
|
|
|
|
2023-11-30 01:39:26 +03:00
|
|
|
const rb_iseq_t *rescue = new_child_iseq_with_callback(iseq, ifunc,
|
|
|
|
rb_str_concat(rb_str_new2("defined guard in "),
|
|
|
|
ISEQ_BODY(iseq)->location.label),
|
|
|
|
iseq, ISEQ_TYPE_RESCUE, 0);
|
|
|
|
|
|
|
|
lstart->rescued = LABEL_RESCUE_BEG;
|
|
|
|
lend->rescued = LABEL_RESCUE_END;
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
APPEND_LABEL(ret, lcur, lstart);
|
|
|
|
ADD_LABEL(ret, lend);
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue, lfinish[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, int lineno, bool in_condition)
|
2023-12-06 01:07:50 +03:00
|
|
|
{
|
|
|
|
LABEL *lfinish[3];
|
|
|
|
LINK_ELEMENT *last = ret->last;
|
|
|
|
|
|
|
|
lfinish[0] = NEW_LABEL(lineno);
|
|
|
|
lfinish[1] = 0;
|
|
|
|
lfinish[2] = 0;
|
|
|
|
|
|
|
|
if (!popped) {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_defined_expr(iseq, node, ret, popped, scope_node, dummy_line_node, lineno, in_condition, lfinish, false);
|
2023-12-06 01:07:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lfinish[1]) {
|
2024-02-08 23:46:53 +03:00
|
|
|
ELEM_INSERT_NEXT(last, &new_insn_body(iseq, nd_line(&dummy_line_node), nd_node_id(&dummy_line_node), BIN(putnil), 0)->link);
|
2023-12-12 19:08:40 +03:00
|
|
|
PM_SWAP;
|
2023-12-06 01:07:50 +03:00
|
|
|
if (lfinish[2]) {
|
|
|
|
ADD_LABEL(ret, lfinish[2]);
|
|
|
|
}
|
2023-12-12 19:08:40 +03:00
|
|
|
PM_POP;
|
2023-10-27 00:54:38 +03:00
|
|
|
ADD_LABEL(ret, lfinish[1]);
|
2023-12-06 01:07:50 +03:00
|
|
|
|
2023-10-27 00:54:38 +03:00
|
|
|
}
|
|
|
|
ADD_LABEL(ret, lfinish[0]);
|
|
|
|
}
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
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)
|
2023-12-06 01:07:50 +03:00
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
const pm_location_t *message_loc = &call_node->message_loc;
|
|
|
|
if (message_loc->start == NULL) message_loc = &call_node->base.location;
|
2024-01-30 00:39:46 +03:00
|
|
|
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_location_line_number(scope_node->parser, message_loc);
|
2023-12-06 01:07:50 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
2024-01-30 00:39:46 +03:00
|
|
|
|
2023-12-15 00:44:38 +03:00
|
|
|
LABEL *else_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
2023-12-06 01:07:50 +03:00
|
|
|
|
2024-01-31 23:53:00 +03:00
|
|
|
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
|
2023-12-15 00:44:38 +03:00
|
|
|
PM_DUP;
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchnil, else_label);
|
|
|
|
}
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
int flags = 0;
|
|
|
|
struct rb_callinfo_kwarg *kw_arg = NULL;
|
|
|
|
|
2024-02-12 23:48:23 +03:00
|
|
|
int orig_argc = pm_setup_args(call_node->arguments, call_node->block, &flags, &kw_arg, iseq, ret, scope_node, dummy_line_node);
|
2023-12-06 01:07:50 +03:00
|
|
|
const rb_iseq_t *block_iseq = NULL;
|
2024-01-31 23:53:00 +03:00
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
if (call_node->block != NULL && PM_NODE_TYPE_P(call_node->block, PM_BLOCK_NODE)) {
|
|
|
|
// Scope associated with the block
|
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init(call_node->block, &next_scope_node, scope_node, scope_node->parser);
|
2024-02-06 21:14:27 +03:00
|
|
|
|
2024-02-14 22:17:32 +03:00
|
|
|
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));
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
|
|
|
|
2023-12-12 01:35:13 +03:00
|
|
|
if (ISEQ_BODY(block_iseq)->catch_table) {
|
2023-12-15 00:44:38 +03:00
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, start, end_label, block_iseq, end_label);
|
2023-12-12 01:35:13 +03:00
|
|
|
}
|
2023-12-06 01:07:50 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
|
|
|
|
}
|
|
|
|
else {
|
2024-01-31 23:53:00 +03:00
|
|
|
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_VARIABLE_CALL)) {
|
2023-12-06 01:07:50 +03:00
|
|
|
flags |= VM_CALL_VCALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!flags) {
|
|
|
|
flags |= VM_CALL_ARGS_SIMPLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 23:53:00 +03:00
|
|
|
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
|
2023-12-06 01:07:50 +03:00
|
|
|
flags |= VM_CALL_FCALL;
|
|
|
|
}
|
|
|
|
|
2024-02-06 18:02:49 +03:00
|
|
|
if (!popped && PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
|
2024-01-22 23:00:36 +03:00
|
|
|
if (flags & VM_CALL_ARGS_BLOCKARG) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
2024-01-22 23:13:33 +03:00
|
|
|
if (flags & VM_CALL_ARGS_SPLAT) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, INT2FIX(-1));
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idAREF, INT2FIX(1), INT2FIX(0));
|
|
|
|
}
|
2024-01-22 23:00:36 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(orig_argc + 3));
|
|
|
|
PM_POP;
|
|
|
|
}
|
|
|
|
else if (flags & VM_CALL_ARGS_SPLAT) {
|
2024-01-11 20:27:50 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, INT2FIX(-1));
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idAREF, INT2FIX(1), INT2FIX(0));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(orig_argc + 2));
|
2024-01-22 23:16:14 +03:00
|
|
|
PM_POP;
|
2024-01-11 20:27:50 +03:00
|
|
|
}
|
2024-02-06 18:02:49 +03:00
|
|
|
else {
|
[PRISM] Fix incorrect instructions for `default_proc=`
This is kind of specific and was found via debugging ruby/prism#2061 but
does not actually fix that issue.
The change here checks for `!popped` for the additional instructions
that are normally added for nodes with the `attribute_write` flag.
I also removed the extra catch table by deleting the
`ISEQ_COMPILE_DATA(iseq)->catch_except_p = true;` line. I'm...not
entirely sure why it was added but it doesn't match the upstream
compiler
[code](https://github.com/ruby/ruby/blob/92b10f5be7453aceb5863e46ac775a4ddbf83b97/compile.c#L887-L892).
The changes here unfortunately don't improve the test failures mentioned in
the linked issue so we still have other issues with instructions for
hashes.
Instructions before:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(34,8)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] h@0
0000 putnil ( 32)[Li]
0001 putobject true
0003 getconstant :Hash
0005 send <calldata!mid:new, argc:0, ARGS_SIMPLE>, nil
0008 setlocal h@0, 0
0011 getlocal h@0, 0 ( 33)[Li]
0014 putspecialobject 1
0016 send <calldata!mid:lambda, argc:0, FCALL>, block in <compiled>
0019 send <calldata!mid:default_proc=, argc:1, ARGS_SIMPLE>, nil
0022 pop
0023 getlocal h@0, 0 ( 34)[Li]
0026 putobject :nope
0028 send <calldata!mid:[], argc:1, ARGS_SIMPLE>, nil
0031 leave
== disasm: #<ISeq:block in <compiled>@<compiled>:33 (33,19)-(33,32)>
local table (size: 2, argc: 2 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] _@0<Arg> [ 1] _@1<Arg>
0000 putobject true ( 33)[LiBc]
0002 leave [Br]
true
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:31 (31,0)-(33,8)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] h@0
0000 putnil ( 31)[Li]
0001 putobject true
0003 getconstant :Hash
0005 send <calldata!mid:new, argc:0, ARGS_SIMPLE>, nil
0008 setlocal h@0, 0
0011 putnil ( 32)[Li]
0012 getlocal h@0, 0
0015 putspecialobject 1
0017 send <calldata!mid:lambda, argc:0, FCALL>, block in <compiled>
0020 setn 2
0022 send <calldata!mid:default_proc=, argc:1, ARGS_SIMPLE>, nil
0025 pop
0026 pop
0027 getlocal h@0, 0 ( 33)[Li]
0030 putobject :nope
0032 send <calldata!mid:[], argc:1, ARGS_SIMPLE>, nil
0035 leave ( 31)
== disasm: #<ISeq:block in <compiled>@<compiled>:32 (32,17)-(32,32)>
== catch table
| catch type: redo st: 0000 ed: 0002 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0002 sp: 0000 cont: 0002
|------------------------------------------------------------------------
local table (size: 2, argc: 2 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] _@0<Arg> [ 1] _@1<Arg>
0000 putobject true ( 32)[LiBc]
0002 leave [Br]
```
Instructions after:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(34,8)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] h@0
0000 putnil ( 32)[Li]
0001 putobject true
0003 getconstant :Hash
0005 send <calldata!mid:new, argc:0, ARGS_SIMPLE>, nil
0008 setlocal h@0, 0
0011 getlocal h@0, 0 ( 33)[Li]
0014 putspecialobject 1
0016 send <calldata!mid:lambda, argc:0, FCALL>, block in <compiled>
0019 send <calldata!mid:default_proc=, argc:1, ARGS_SIMPLE>, nil
0022 pop
0023 getlocal h@0, 0 ( 34)[Li]
0026 putobject :nope
0028 send <calldata!mid:[], argc:1, ARGS_SIMPLE>, nil
0031 leave
== disasm: #<ISeq:block in <compiled>@<compiled>:33 (33,19)-(33,32)>
local table (size: 2, argc: 2 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] _@0<Arg> [ 1] _@1<Arg>
0000 putobject true ( 33)[LiBc]
0002 leave [Br]
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:31 (31,0)-(33,8)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] h@0
0000 putnil ( 31)[Li]
0001 putobject true
0003 getconstant :Hash
0005 send <calldata!mid:new, argc:0, ARGS_SIMPLE>, nil
0008 setlocal h@0, 0
0011 getlocal h@0, 0 ( 32)[Li]
0014 putspecialobject 1
0016 send <calldata!mid:lambda, argc:0, FCALL>, block in <compiled>
0019 send <calldata!mid:default_proc=, argc:1, ARGS_SIMPLE>, nil
0022 pop
0023 getlocal h@0, 0 ( 33)[Li]
0026 putobject :nope
0028 send <calldata!mid:[], argc:1, ARGS_SIMPLE>, nil
0031 leave ( 31)
== disasm: #<ISeq:block in <compiled>@<compiled>:32 (32,17)-(32,32)>
local table (size: 2, argc: 2 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] _@0<Arg> [ 1] _@1<Arg>
0000 putobject true ( 32)[LiBc]
0002 leave [Br]
```
2023-12-15 19:32:10 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(orig_argc + 1));
|
|
|
|
}
|
2024-01-29 17:47:52 +03:00
|
|
|
}
|
2024-01-11 20:27:50 +03:00
|
|
|
|
2024-01-29 17:47:52 +03:00
|
|
|
if ((flags & VM_CALL_KW_SPLAT) && (flags & VM_CALL_ARGS_BLOCKARG) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, splatkw);
|
[PRISM] Fix `PM_CALL_NODE` assignment
This PR fixes ruby/prism#1963. Array and variable assignment was broken
for call nodes. The change checks if the `method_id` is related to
assignment and if is adds a `putnil`, `setn` and a `pop`.
The incorrect instructions meant that in some cases (demonstrated in
tests) the wrong value would be returned.
I verified that this fixes the test mentioned in the issue
(run: `RUBY_ISEQ_DUMP_DEBUG=prism make test/-ext-/st/test_numhash.rb`)
Incorrect instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(4,10)>
0000 putnil ( 4)[Li]
0001 putself
0002 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0005 putself
0006 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0009 putself
0010 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0013 setn 3
0015 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0018 pop
0019 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:3 (3,0)-(3,10)>
0000 putself ( 3)[Li]
0001 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0004 putself
0005 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0008 putself
0009 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0012 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0015 leave
```
Fixed instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(4,10)>
0000 putnil ( 4)[Li]
0001 putself
0002 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0005 putself
0006 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0009 putself
0010 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0013 setn 3
0015 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0018 pop
0019 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:3 (3,0)-(3,10)>
0000 putnil ( 3)[Li]
0001 putself
0002 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0005 putself
0006 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0009 putself
0010 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0013 setn 3
0015 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0018 pop
0019 leave
```
Fixes ruby/prism#1963
2023-12-08 23:00:48 +03:00
|
|
|
}
|
2024-01-29 17:47:52 +03:00
|
|
|
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
|
|
|
|
|
2024-01-31 23:53:00 +03:00
|
|
|
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
|
2023-12-15 00:44:38 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, end_label);
|
|
|
|
ADD_LABEL(ret, else_label);
|
|
|
|
}
|
2024-01-26 20:17:04 +03:00
|
|
|
|
2024-01-31 23:53:00 +03:00
|
|
|
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) || (block_iseq && ISEQ_BODY(block_iseq)->catch_table)) {
|
2024-01-26 20:17:04 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
}
|
[PRISM] Fix `PM_CALL_NODE` assignment
This PR fixes ruby/prism#1963. Array and variable assignment was broken
for call nodes. The change checks if the `method_id` is related to
assignment and if is adds a `putnil`, `setn` and a `pop`.
The incorrect instructions meant that in some cases (demonstrated in
tests) the wrong value would be returned.
I verified that this fixes the test mentioned in the issue
(run: `RUBY_ISEQ_DUMP_DEBUG=prism make test/-ext-/st/test_numhash.rb`)
Incorrect instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(4,10)>
0000 putnil ( 4)[Li]
0001 putself
0002 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0005 putself
0006 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0009 putself
0010 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0013 setn 3
0015 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0018 pop
0019 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:3 (3,0)-(3,10)>
0000 putself ( 3)[Li]
0001 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0004 putself
0005 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0008 putself
0009 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0012 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0015 leave
```
Fixed instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(4,10)>
0000 putnil ( 4)[Li]
0001 putself
0002 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0005 putself
0006 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0009 putself
0010 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0013 setn 3
0015 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0018 pop
0019 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:3 (3,0)-(3,10)>
0000 putnil ( 3)[Li]
0001 putself
0002 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0005 putself
0006 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0009 putself
0010 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0013 setn 3
0015 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0018 pop
0019 leave
```
Fixes ruby/prism#1963
2023-12-08 23:00:48 +03:00
|
|
|
|
2024-01-31 23:53:00 +03:00
|
|
|
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE)) {
|
|
|
|
PM_POP_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
}
|
|
|
|
|
2023-12-11 20:14:21 +03:00
|
|
|
// This is exactly the same as add_ensure_iseq, except it compiled
|
|
|
|
// the node as a Prism node, and not a CRuby node
|
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_add_ensure_iseq(LINK_ANCHOR *const ret, rb_iseq_t *iseq, int is_return, pm_scope_node_t *scope_node)
|
2023-12-11 20:14:21 +03:00
|
|
|
{
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(can_add_ensure_iseq(iseq));
|
2023-12-11 20:14:21 +03:00
|
|
|
|
|
|
|
struct iseq_compile_data_ensure_node_stack *enlp =
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack;
|
|
|
|
struct iseq_compile_data_ensure_node_stack *prev_enlp = enlp;
|
|
|
|
DECL_ANCHOR(ensure);
|
|
|
|
|
|
|
|
INIT_ANCHOR(ensure);
|
|
|
|
while (enlp) {
|
|
|
|
if (enlp->erange != NULL) {
|
|
|
|
DECL_ANCHOR(ensure_part);
|
|
|
|
LABEL *lstart = NEW_LABEL(0);
|
|
|
|
LABEL *lend = NEW_LABEL(0);
|
|
|
|
INIT_ANCHOR(ensure_part);
|
|
|
|
|
|
|
|
add_ensure_range(iseq, enlp->erange, lstart, lend);
|
|
|
|
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enlp->prev;
|
|
|
|
ADD_LABEL(ensure_part, lstart);
|
|
|
|
bool popped = true;
|
|
|
|
PM_COMPILE_INTO_ANCHOR(ensure_part, (pm_node_t *)enlp->ensure_node);
|
|
|
|
ADD_LABEL(ensure_part, lend);
|
|
|
|
ADD_SEQ(ensure, ensure_part);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!is_return) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enlp = enlp->prev;
|
|
|
|
}
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = prev_enlp;
|
|
|
|
ADD_SEQ(ret, ensure);
|
|
|
|
}
|
|
|
|
|
2024-01-23 23:11:21 +03:00
|
|
|
struct pm_local_table_insert_ctx {
|
|
|
|
pm_scope_node_t *scope_node;
|
|
|
|
rb_ast_id_table_t *local_table_for_iseq;
|
|
|
|
int local_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
|
|
|
|
{
|
|
|
|
if (!existing) {
|
2024-01-26 22:51:25 +03:00
|
|
|
pm_constant_id_t constant_id = (pm_constant_id_t)*key;
|
2024-01-23 23:11:21 +03:00
|
|
|
struct pm_local_table_insert_ctx * ctx = (struct pm_local_table_insert_ctx *)arg;
|
|
|
|
|
|
|
|
pm_scope_node_t *scope_node = ctx->scope_node;
|
|
|
|
rb_ast_id_table_t *local_table_for_iseq = ctx->local_table_for_iseq;
|
|
|
|
int local_index = ctx->local_index;
|
|
|
|
|
|
|
|
ID local = pm_constant_id_lookup(scope_node, constant_id);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
|
2024-01-26 22:51:25 +03:00
|
|
|
*value = (st_data_t)local_index;
|
2024-01-23 23:11:21 +03:00
|
|
|
|
|
|
|
ctx->local_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2024-01-26 02:08:44 +03:00
|
|
|
/**
|
|
|
|
* Insert a local into the local table for the iseq. This is used to create the
|
|
|
|
* local table in the correct order while compiling the scope. The locals being
|
|
|
|
* inserted are regular named locals, as opposed to special forwarding locals.
|
|
|
|
*/
|
2023-12-06 18:05:14 +03:00
|
|
|
static void
|
|
|
|
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)
|
|
|
|
{
|
2024-01-23 22:43:53 +03:00
|
|
|
RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
ID local = pm_constant_id_lookup(scope_node, constant_id);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
2024-01-26 02:08:44 +03:00
|
|
|
st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a local into the local table for the iseq that is a special forwarding
|
|
|
|
* local variable.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pm_insert_local_special(ID local_name, int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq)
|
|
|
|
{
|
|
|
|
local_table_for_iseq->ids[local_index] = local_name;
|
|
|
|
st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
|
2024-01-24 18:39:40 +03:00
|
|
|
/**
|
|
|
|
* Compile the locals of a multi target node that is used as a positional
|
|
|
|
* parameter in a method, block, or lambda definition. Note that this doesn't
|
|
|
|
* actually add any instructions to the iseq. Instead, it adds locals to the
|
|
|
|
* local and index lookup tables and increments the local index as necessary.
|
|
|
|
*/
|
2023-12-06 18:05:14 +03:00
|
|
|
static int
|
2024-01-24 18:39:40 +03:00
|
|
|
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)
|
2023-12-06 18:05:14 +03:00
|
|
|
{
|
2024-01-24 18:39:40 +03:00
|
|
|
for (size_t index = 0; index < node->lefts.size; index++) {
|
|
|
|
const pm_node_t *left = node->lefts.nodes[index];
|
2023-12-06 18:05:14 +03:00
|
|
|
|
2024-01-24 18:39:40 +03:00
|
|
|
if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
|
2024-01-30 00:35:54 +03:00
|
|
|
if (!PM_NODE_FLAG_P(left, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
pm_insert_local_index(((const pm_required_parameter_node_t *) left)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
local_index++;
|
|
|
|
}
|
2024-01-24 18:39:40 +03:00
|
|
|
} else {
|
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
|
|
|
|
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);
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 18:39:40 +03:00
|
|
|
if (node->rest != NULL && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE)) {
|
|
|
|
const pm_splat_node_t *rest = (const pm_splat_node_t *) node->rest;
|
|
|
|
|
|
|
|
if (rest->expression != NULL) {
|
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE));
|
|
|
|
pm_insert_local_index(((const pm_required_parameter_node_t *) rest->expression)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
2023-12-06 18:05:14 +03:00
|
|
|
local_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 18:39:40 +03:00
|
|
|
for (size_t index = 0; index < node->rights.size; index++) {
|
|
|
|
const pm_node_t *right = node->rights.nodes[index];
|
2023-12-06 18:05:14 +03:00
|
|
|
|
2024-01-24 18:39:40 +03:00
|
|
|
if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
|
|
|
|
pm_insert_local_index(((const pm_required_parameter_node_t *) right)->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
2023-12-06 18:05:14 +03:00
|
|
|
local_index++;
|
2024-01-24 18:39:40 +03:00
|
|
|
} else {
|
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
|
|
|
|
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);
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return local_index;
|
|
|
|
}
|
|
|
|
|
2024-01-24 18:39:40 +03:00
|
|
|
/**
|
|
|
|
* Compile a required parameter node that is part of a destructure that is used
|
|
|
|
* as a positional parameter in a method, block, or lambda definition.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
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)
|
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, (const pm_node_t *) node);
|
2024-01-24 18:39:40 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, node->name, 0);
|
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile a multi target node that is used as a positional parameter in a
|
|
|
|
* method, block, or lambda definition. Note that this is effectively the same
|
|
|
|
* as a multi write, but with the added context that all of the targets
|
|
|
|
* contained in the write are required parameter nodes. With this context, we
|
|
|
|
* know they won't have any parent expressions so we build a separate code path
|
|
|
|
* for this simplified case.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
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)
|
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, (const pm_node_t *) node);
|
2024-01-24 18:39:40 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
bool has_rest = (node->rest && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE) && (((pm_splat_node_t *) node->rest)->expression) != NULL);
|
|
|
|
bool has_rights = node->rights.size > 0;
|
|
|
|
|
|
|
|
int flag = (has_rest || has_rights) ? 1 : 0;
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(node->lefts.size), INT2FIX(flag));
|
|
|
|
|
|
|
|
for (size_t index = 0; index < node->lefts.size; index++) {
|
|
|
|
const pm_node_t *left = node->lefts.nodes[index];
|
|
|
|
|
|
|
|
if (PM_NODE_TYPE_P(left, PM_REQUIRED_PARAMETER_NODE)) {
|
|
|
|
pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) left, ret, scope_node);
|
|
|
|
} else {
|
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(left, PM_MULTI_TARGET_NODE));
|
|
|
|
pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) left, ret, scope_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_rest) {
|
|
|
|
if (has_rights) {
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(node->rights.size), INT2FIX(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
const pm_node_t *rest = ((pm_splat_node_t *) node->rest)->expression;
|
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(rest, PM_REQUIRED_PARAMETER_NODE));
|
|
|
|
|
|
|
|
pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) rest, ret, scope_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_rights) {
|
|
|
|
if (!has_rest) {
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(node->rights.size), INT2FIX(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t index = 0; index < node->rights.size; index++) {
|
|
|
|
const pm_node_t *right = node->rights.nodes[index];
|
|
|
|
|
|
|
|
if (PM_NODE_TYPE_P(right, PM_REQUIRED_PARAMETER_NODE)) {
|
|
|
|
pm_compile_destructured_param_write(iseq, (const pm_required_parameter_node_t *) right, ret, scope_node);
|
|
|
|
} else {
|
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(right, PM_MULTI_TARGET_NODE));
|
|
|
|
pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) right, ret, scope_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a node in the multi target state linked list. It tracks the
|
|
|
|
* information for a particular target that necessarily has a parent expression.
|
|
|
|
*/
|
2024-01-12 23:23:47 +03:00
|
|
|
typedef struct pm_multi_target_state_node {
|
|
|
|
// The pointer to the topn instruction that will need to be modified after
|
|
|
|
// we know the total stack size of all of the targets.
|
|
|
|
INSN *topn;
|
|
|
|
|
|
|
|
// The index of the stack from the base of the entire multi target at which
|
|
|
|
// the parent expression is located.
|
|
|
|
size_t stack_index;
|
|
|
|
|
|
|
|
// The number of slots in the stack that this node occupies.
|
|
|
|
size_t stack_size;
|
|
|
|
|
|
|
|
// The position of the node in the list of targets.
|
|
|
|
size_t position;
|
|
|
|
|
|
|
|
// A pointer to the next node in this linked list.
|
|
|
|
struct pm_multi_target_state_node *next;
|
|
|
|
} pm_multi_target_state_node_t;
|
|
|
|
|
2024-01-24 18:39:40 +03:00
|
|
|
/**
|
|
|
|
* As we're compiling a multi target, we need to track additional information
|
|
|
|
* whenever there is a parent expression on the left hand side of the target.
|
|
|
|
* This is because we need to go back and tell the expression where to fetch its
|
|
|
|
* parent expression from the stack. We use a linked list of nodes to track this
|
|
|
|
* information.
|
|
|
|
*/
|
2024-01-12 23:23:47 +03:00
|
|
|
typedef struct {
|
|
|
|
// The total number of slots in the stack that this multi target occupies.
|
|
|
|
size_t stack_size;
|
|
|
|
|
|
|
|
// The position of the current node being compiled. This is forwarded to
|
|
|
|
// nodes when they are allocated.
|
|
|
|
size_t position;
|
|
|
|
|
|
|
|
// A pointer to the head of this linked list.
|
|
|
|
pm_multi_target_state_node_t *head;
|
|
|
|
|
|
|
|
// A pointer to the tail of this linked list.
|
|
|
|
pm_multi_target_state_node_t *tail;
|
|
|
|
} pm_multi_target_state_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push a new state node onto the multi target state.
|
|
|
|
*/
|
|
|
|
static void
|
2024-02-12 23:16:44 +03:00
|
|
|
pm_multi_target_state_push(pm_multi_target_state_t *state, INSN *topn, size_t stack_size)
|
|
|
|
{
|
2024-01-12 23:23:47 +03:00
|
|
|
pm_multi_target_state_node_t *node = ALLOC(pm_multi_target_state_node_t);
|
|
|
|
node->topn = topn;
|
|
|
|
node->stack_index = state->stack_size + 1;
|
|
|
|
node->stack_size = stack_size;
|
|
|
|
node->position = state->position;
|
|
|
|
node->next = NULL;
|
|
|
|
|
|
|
|
if (state->head == NULL) {
|
|
|
|
state->head = node;
|
|
|
|
state->tail = node;
|
|
|
|
} else {
|
|
|
|
state->tail->next = node;
|
|
|
|
state->tail = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->stack_size += stack_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Walk through a multi target state's linked list and update the topn
|
|
|
|
* instructions that were inserted into the write sequence to make sure they can
|
|
|
|
* correctly retrieve their parent expressions.
|
|
|
|
*/
|
|
|
|
static void
|
2024-02-12 23:16:44 +03:00
|
|
|
pm_multi_target_state_update(pm_multi_target_state_t *state)
|
|
|
|
{
|
2024-01-12 23:23:47 +03:00
|
|
|
// If nothing was ever pushed onto the stack, then we don't need to do any
|
|
|
|
// kind of updates.
|
|
|
|
if (state->stack_size == 0) return;
|
|
|
|
|
|
|
|
pm_multi_target_state_node_t *current = state->head;
|
|
|
|
pm_multi_target_state_node_t *previous;
|
|
|
|
|
|
|
|
while (current != NULL) {
|
|
|
|
VALUE offset = INT2FIX(state->stack_size - current->stack_index + current->position);
|
|
|
|
current->topn->operands[0] = offset;
|
|
|
|
|
|
|
|
// stack_size will be > 1 in the case that we compiled an index target
|
|
|
|
// and it had arguments. In this case, we use multiple topn instructions
|
|
|
|
// to grab up all of the arguments as well, so those offsets need to be
|
|
|
|
// updated as well.
|
|
|
|
if (current->stack_size > 1) {
|
|
|
|
INSN *insn = current->topn;
|
|
|
|
|
|
|
|
for (size_t index = 1; index < current->stack_size; index += 1) {
|
|
|
|
LINK_ELEMENT *element = get_next_insn(insn);
|
|
|
|
RUBY_ASSERT(IS_INSN(element));
|
|
|
|
|
|
|
|
insn = (INSN *) element;
|
|
|
|
RUBY_ASSERT(insn->insn_id == BIN(topn));
|
|
|
|
|
|
|
|
insn->operands[0] = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previous = current;
|
|
|
|
current = current->next;
|
|
|
|
|
|
|
|
free(previous);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2024-01-31 20:17:31 +03:00
|
|
|
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);
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A target node represents an indirect write to a variable or a method call to
|
|
|
|
* a method ending in =. Compiling one of these nodes requires three sequences:
|
|
|
|
*
|
|
|
|
* * The first is to compile retrieving the parent expression if there is one.
|
|
|
|
* This could be the object that owns a constant or the receiver of a method
|
|
|
|
* call.
|
|
|
|
* * The second is to compile the writes to the targets. This could be writing
|
|
|
|
* to variables, or it could be performing method calls.
|
|
|
|
* * The third is to compile any cleanup that needs to happen, i.e., popping the
|
|
|
|
* appropriate number of values off the stack.
|
|
|
|
*
|
|
|
|
* When there is a parent expression and this target is part of a multi write, a
|
|
|
|
* topn instruction will be inserted into the write sequence. This is to move
|
|
|
|
* the parent expression to the top of the stack so that it can be used as the
|
|
|
|
* receiver of the method call or the owner of the constant. To facilitate this,
|
|
|
|
* we return a pointer to the topn instruction that was used to be later
|
|
|
|
* modified with the correct offset.
|
|
|
|
*
|
|
|
|
* These nodes can appear in a couple of places, but most commonly:
|
|
|
|
*
|
|
|
|
* * For loops - the index variable is a target node
|
|
|
|
* * Rescue clauses - the exception reference variable is a target node
|
|
|
|
* * Multi writes - the left hand side contains a list of target nodes
|
|
|
|
*
|
|
|
|
* For the comments with examples within this function, we'll use for loops as
|
|
|
|
* the containing node.
|
|
|
|
*/
|
|
|
|
static void
|
2024-02-12 23:16:44 +03:00
|
|
|
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)
|
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, node);
|
2024-01-12 23:23:47 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
|
|
|
case PM_LOCAL_VARIABLE_TARGET_NODE: {
|
|
|
|
// Local variable targets have no parent expression, so they only need
|
|
|
|
// to compile the write.
|
|
|
|
//
|
|
|
|
// for i in []; end
|
|
|
|
//
|
|
|
|
pm_local_variable_target_node_t *cast = (pm_local_variable_target_node_t *) node;
|
2024-01-23 23:33:45 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
ADD_SETLOCAL(writes, &dummy_line_node, index.index, index.level);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_CLASS_VARIABLE_TARGET_NODE: {
|
|
|
|
// Class variable targets have no parent expression, so they only need
|
|
|
|
// to compile the write.
|
|
|
|
//
|
|
|
|
// for @@i in []; end
|
|
|
|
//
|
|
|
|
pm_class_variable_target_node_t *cast = (pm_class_variable_target_node_t *) node;
|
|
|
|
ID name = pm_constant_id_lookup(scope_node, cast->name);
|
|
|
|
|
|
|
|
ADD_INSN2(writes, &dummy_line_node, setclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_CONSTANT_TARGET_NODE: {
|
|
|
|
// Constant targets have no parent expression, so they only need to
|
|
|
|
// compile the write.
|
|
|
|
//
|
|
|
|
// for I in []; end
|
|
|
|
//
|
|
|
|
pm_constant_target_node_t *cast = (pm_constant_target_node_t *) node;
|
|
|
|
ID name = pm_constant_id_lookup(scope_node, cast->name);
|
|
|
|
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, setconstant, ID2SYM(name));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_GLOBAL_VARIABLE_TARGET_NODE: {
|
|
|
|
// Global variable targets have no parent expression, so they only need
|
|
|
|
// to compile the write.
|
|
|
|
//
|
|
|
|
// for $i in []; end
|
|
|
|
//
|
|
|
|
pm_global_variable_target_node_t *cast = (pm_global_variable_target_node_t *) node;
|
|
|
|
ID name = pm_constant_id_lookup(scope_node, cast->name);
|
|
|
|
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, setglobal, ID2SYM(name));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_INSTANCE_VARIABLE_TARGET_NODE: {
|
|
|
|
// Instance variable targets have no parent expression, so they only
|
|
|
|
// need to compile the write.
|
|
|
|
//
|
|
|
|
// for @i in []; end
|
|
|
|
//
|
|
|
|
pm_instance_variable_target_node_t *cast = (pm_instance_variable_target_node_t *) node;
|
|
|
|
ID name = pm_constant_id_lookup(scope_node, cast->name);
|
|
|
|
|
|
|
|
ADD_INSN2(writes, &dummy_line_node, setinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_CONSTANT_PATH_TARGET_NODE: {
|
|
|
|
// Constant path targets have a parent expression that is the object
|
|
|
|
// that owns the constant. This needs to be compiled first into the
|
|
|
|
// parents sequence. If no parent is found, then it represents using the
|
|
|
|
// unary :: operator to indicate a top-level constant. In that case we
|
|
|
|
// need to push Object onto the stack.
|
|
|
|
//
|
|
|
|
// for I::J in []; end
|
|
|
|
//
|
|
|
|
const pm_constant_path_target_node_t *cast = (const pm_constant_path_target_node_t *) node;
|
|
|
|
ID name = pm_constant_id_lookup(scope_node, ((const pm_constant_read_node_t *) cast->child)->name);
|
|
|
|
|
|
|
|
if (cast->parent != NULL) {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(iseq, cast->parent, parents, false, scope_node);
|
2024-01-12 23:23:47 +03:00
|
|
|
} else {
|
|
|
|
ADD_INSN1(parents, &dummy_line_node, putobject, rb_cObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == NULL) {
|
|
|
|
ADD_INSN(writes, &dummy_line_node, swap);
|
|
|
|
} else {
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, setconstant, ID2SYM(name));
|
|
|
|
|
|
|
|
if (state != NULL) {
|
|
|
|
ADD_INSN(cleanup, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_CALL_TARGET_NODE: {
|
|
|
|
// Call targets have a parent expression that is the receiver of the
|
|
|
|
// method being called. This needs to be compiled first into the parents
|
|
|
|
// sequence. These nodes cannot have arguments, so the method call is
|
|
|
|
// compiled with a single argument which represents the value being
|
|
|
|
// written.
|
|
|
|
//
|
|
|
|
// for i.j in []; end
|
|
|
|
//
|
|
|
|
const pm_call_target_node_t *cast = (const pm_call_target_node_t *) node;
|
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, cast->name);
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
if (state != NULL) {
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
|
|
|
|
ADD_INSN(writes, &dummy_line_node, swap);
|
|
|
|
}
|
|
|
|
|
2024-02-06 18:50:55 +03:00
|
|
|
int flags = VM_CALL_ARGS_SIMPLE;
|
|
|
|
if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) flags |= VM_CALL_FCALL;
|
|
|
|
|
|
|
|
ADD_SEND_WITH_FLAG(writes, &dummy_line_node, method_id, INT2FIX(1), INT2FIX(flags));
|
2024-01-12 23:23:47 +03:00
|
|
|
ADD_INSN(writes, &dummy_line_node, pop);
|
|
|
|
|
|
|
|
if (state != NULL) {
|
|
|
|
ADD_INSN(cleanup, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_INDEX_TARGET_NODE: {
|
|
|
|
// Index targets have a parent expression that is the receiver of the
|
|
|
|
// method being called and any additional arguments that are being
|
|
|
|
// passed along with the value being written. The receiver and arguments
|
|
|
|
// both need to be on the stack. Note that this is even more complicated
|
|
|
|
// by the fact that these nodes can hold a block using the unary &
|
|
|
|
// operator.
|
|
|
|
//
|
|
|
|
// for i[:j] in []; end
|
|
|
|
//
|
|
|
|
const pm_index_target_node_t *cast = (const pm_index_target_node_t *) node;
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(iseq, cast->receiver, parents, false, scope_node);
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
int flags = 0;
|
|
|
|
struct rb_callinfo_kwarg *kwargs = NULL;
|
2024-02-12 23:48:23 +03:00
|
|
|
int argc = pm_setup_args(cast->arguments, cast->block, &flags, &kwargs, iseq, parents, scope_node, dummy_line_node);
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
if (state != NULL) {
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, topn, INT2FIX(argc + 1));
|
|
|
|
pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), argc + 1);
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
ADD_INSN(writes, &dummy_line_node, swap);
|
|
|
|
} else {
|
|
|
|
for (int index = 0; index < argc; index++) {
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, topn, INT2FIX(argc + 1));
|
|
|
|
}
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, topn, INT2FIX(argc + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 19:36:55 +03:00
|
|
|
// The argc that we're going to pass to the send instruction is the
|
|
|
|
// number of arguments + 1 for the value being written. If there's a
|
|
|
|
// splat, then we need to insert newarray and concatarray instructions
|
|
|
|
// after the arguments have been written.
|
|
|
|
int ci_argc = argc + 1;
|
|
|
|
if (flags & VM_CALL_ARGS_SPLAT) {
|
|
|
|
ci_argc--;
|
|
|
|
ADD_INSN1(writes, &dummy_line_node, newarray, INT2FIX(1));
|
|
|
|
ADD_INSN(writes, &dummy_line_node, concatarray);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEND_R(writes, &dummy_line_node, idASET, INT2NUM(ci_argc), NULL, INT2FIX(flags), kwargs);
|
2024-01-12 23:23:47 +03:00
|
|
|
ADD_INSN(writes, &dummy_line_node, pop);
|
|
|
|
|
|
|
|
if (state != NULL) {
|
|
|
|
if (argc != 0) {
|
|
|
|
ADD_INSN(writes, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int index = 0; index < argc + 1; index++) {
|
|
|
|
ADD_INSN(cleanup, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
|
|
|
// Multi target nodes represent a set of writes to multiple variables.
|
|
|
|
// The parent expressions are the combined set of the parent expressions
|
|
|
|
// of its inner target nodes.
|
|
|
|
//
|
|
|
|
// for i, j in []; end
|
|
|
|
//
|
2024-01-23 19:32:35 +03:00
|
|
|
if (state != NULL) state->position--;
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_multi_target_node(iseq, node, parents, writes, cleanup, scope_node, state);
|
2024-01-23 19:32:35 +03:00
|
|
|
if (state != NULL) state->position++;
|
2024-01-12 23:23:47 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
rb_bug("Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile a multi target or multi write node. It returns the number of values
|
|
|
|
* on the stack that correspond to the parent expressions of the various
|
|
|
|
* targets.
|
|
|
|
*/
|
|
|
|
static size_t
|
2024-02-12 23:16:44 +03:00
|
|
|
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)
|
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, node);
|
2024-01-12 23:23:47 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
const pm_node_list_t *lefts;
|
|
|
|
const pm_node_t *rest;
|
|
|
|
const pm_node_list_t *rights;
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
|
|
|
pm_multi_target_node_t *cast = (pm_multi_target_node_t *) node;
|
|
|
|
lefts = &cast->lefts;
|
|
|
|
rest = cast->rest;
|
|
|
|
rights = &cast->rights;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_MULTI_WRITE_NODE: {
|
|
|
|
pm_multi_write_node_t *cast = (pm_multi_write_node_t *) node;
|
|
|
|
lefts = &cast->lefts;
|
|
|
|
rest = cast->rest;
|
|
|
|
rights = &cast->rights;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_rest = (rest != NULL) && PM_NODE_TYPE_P(rest, PM_SPLAT_NODE) && ((pm_splat_node_t *) rest)->expression != NULL;
|
|
|
|
bool has_posts = rights->size > 0;
|
|
|
|
|
|
|
|
// The first instruction in the writes sequence is going to spread the
|
|
|
|
// top value of the stack onto the number of values that we're going to
|
|
|
|
// write.
|
|
|
|
ADD_INSN2(writes, &dummy_line_node, expandarray, INT2FIX(lefts->size), INT2FIX((has_rest || has_posts) ? 1 : 0));
|
|
|
|
|
|
|
|
// We need to keep track of some additional state information as we're
|
|
|
|
// going through the targets because we will need to revisit them once
|
|
|
|
// we know how many values are being pushed onto the stack.
|
|
|
|
pm_multi_target_state_t target_state = { 0 };
|
|
|
|
size_t base_position = state == NULL ? 0 : state->position;
|
|
|
|
size_t splat_position = has_rest ? 1 : 0;
|
|
|
|
|
|
|
|
// Next, we'll iterate through all of the leading targets.
|
|
|
|
for (size_t index = 0; index < lefts->size; index++) {
|
|
|
|
const pm_node_t *target = lefts->nodes[index];
|
|
|
|
target_state.position = lefts->size - index + splat_position + base_position;
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, &target_state);
|
2024-01-12 23:23:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll compile the rest target if there is one.
|
|
|
|
if (has_rest) {
|
|
|
|
const pm_node_t *target = ((pm_splat_node_t *) rest)->expression;
|
|
|
|
target_state.position = 1 + rights->size + base_position;
|
|
|
|
|
|
|
|
if (has_posts) {
|
|
|
|
ADD_INSN2(writes, &dummy_line_node, expandarray, INT2FIX(rights->size), INT2FIX(3));
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, &target_state);
|
2024-01-12 23:23:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we'll compile the trailing targets.
|
|
|
|
if (has_posts) {
|
|
|
|
if (!has_rest && rest != NULL) {
|
|
|
|
ADD_INSN2(writes, &dummy_line_node, expandarray, INT2FIX(rights->size), INT2FIX(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t index = 0; index < rights->size; index++) {
|
|
|
|
const pm_node_t *target = rights->nodes[index];
|
|
|
|
target_state.position = rights->size - index + base_position;
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_target_node(iseq, target, parents, writes, cleanup, scope_node, &target_state);
|
2024-01-12 23:23:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, we need to go back and modify the topn instructions in order to
|
|
|
|
// ensure they can correctly retrieve the parent expressions.
|
|
|
|
pm_multi_target_state_update(&target_state);
|
2024-01-23 19:32:35 +03:00
|
|
|
|
|
|
|
if (state != NULL) state->stack_size += target_state.stack_size;
|
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
return target_state.stack_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When compiling a for loop, we need to write the iteration variable to
|
|
|
|
* whatever expression exists in the index slot. This function performs that
|
|
|
|
* compilation.
|
|
|
|
*/
|
|
|
|
static void
|
2024-02-12 23:16:44 +03:00
|
|
|
pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
|
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, node);
|
2024-01-12 23:23:47 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
|
|
|
case PM_LOCAL_VARIABLE_TARGET_NODE: {
|
|
|
|
// For local variables, all we have to do is retrieve the value and then
|
|
|
|
// compile the index node.
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, 1, 0);
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_target_node(iseq, node, ret, ret, ret, scope_node, NULL);
|
2024-01-12 23:23:47 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_CLASS_VARIABLE_TARGET_NODE:
|
|
|
|
case PM_CONSTANT_TARGET_NODE:
|
|
|
|
case PM_GLOBAL_VARIABLE_TARGET_NODE:
|
|
|
|
case PM_INSTANCE_VARIABLE_TARGET_NODE:
|
|
|
|
case PM_CONSTANT_PATH_TARGET_NODE:
|
|
|
|
case PM_CALL_TARGET_NODE:
|
|
|
|
case PM_INDEX_TARGET_NODE: {
|
|
|
|
// For other targets, we need to potentially compile the parent or
|
|
|
|
// owning expression of this target, then retrieve the value, expand it,
|
|
|
|
// and then compile the necessary writes.
|
|
|
|
DECL_ANCHOR(writes);
|
|
|
|
INIT_ANCHOR(writes);
|
|
|
|
|
|
|
|
DECL_ANCHOR(cleanup);
|
|
|
|
INIT_ANCHOR(cleanup);
|
|
|
|
|
|
|
|
pm_multi_target_state_t state = { 0 };
|
|
|
|
state.position = 1;
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, 1, 0);
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(1), INT2FIX(0));
|
|
|
|
|
|
|
|
ADD_SEQ(ret, writes);
|
|
|
|
ADD_SEQ(ret, cleanup);
|
|
|
|
|
|
|
|
pm_multi_target_state_update(&state);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
|
|
|
DECL_ANCHOR(writes);
|
|
|
|
INIT_ANCHOR(writes);
|
|
|
|
|
|
|
|
DECL_ANCHOR(cleanup);
|
|
|
|
INIT_ANCHOR(cleanup);
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
LABEL *not_single = NEW_LABEL(lineno);
|
|
|
|
LABEL *not_ary = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
// When there are multiple targets, we'll do a bunch of work to convert
|
|
|
|
// the value into an array before we expand it. Effectively we're trying
|
|
|
|
// to accomplish:
|
|
|
|
//
|
|
|
|
// (args.length == 1 && Array.try_convert(args[0])) || args
|
|
|
|
//
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, 1, 0);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
ADD_CALL(ret, &dummy_line_node, idLength, INT2FIX(0));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, INT2FIX(1));
|
|
|
|
ADD_CALL(ret, &dummy_line_node, idEq, INT2FIX(1));
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, not_single);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, INT2FIX(0));
|
|
|
|
ADD_CALL(ret, &dummy_line_node, idAREF, INT2FIX(1));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cArray);
|
2024-01-22 23:21:09 +03:00
|
|
|
PM_SWAP;
|
2024-01-12 23:23:47 +03:00
|
|
|
ADD_CALL(ret, &dummy_line_node, rb_intern("try_convert"), INT2FIX(1));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, not_ary);
|
2024-01-22 23:21:09 +03:00
|
|
|
PM_SWAP;
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
ADD_LABEL(ret, not_ary);
|
2024-01-22 23:16:14 +03:00
|
|
|
PM_POP;
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
ADD_LABEL(ret, not_single);
|
|
|
|
ADD_SEQ(ret, writes);
|
|
|
|
ADD_SEQ(ret, cleanup);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
rb_bug("Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 22:24:34 +03:00
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_rescue(rb_iseq_t *iseq, pm_begin_node_t *begin_node, LINK_ANCHOR *const ret, int lineno, bool popped, pm_scope_node_t *scope_node)
|
2024-01-22 22:24:34 +03:00
|
|
|
{
|
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
2024-02-08 18:14:27 +03:00
|
|
|
const pm_parser_t *parser = scope_node->parser;
|
2024-01-22 22:24:34 +03:00
|
|
|
LABEL *lstart = NEW_LABEL(lineno);
|
|
|
|
LABEL *lend = NEW_LABEL(lineno);
|
|
|
|
LABEL *lcont = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
pm_scope_node_t rescue_scope_node;
|
2024-01-30 00:03:03 +03:00
|
|
|
pm_scope_node_init((pm_node_t *) begin_node->rescue_clause, &rescue_scope_node, scope_node, parser);
|
|
|
|
|
|
|
|
rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
|
|
|
|
&rescue_scope_node,
|
|
|
|
rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
|
|
|
|
ISEQ_TYPE_RESCUE,
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_node_line_number(parser, (const pm_node_t *) begin_node->rescue_clause)
|
2024-01-30 00:03:03 +03:00
|
|
|
);
|
|
|
|
|
2024-01-22 22:24:34 +03:00
|
|
|
pm_scope_node_destroy(&rescue_scope_node);
|
|
|
|
|
|
|
|
lstart->rescued = LABEL_RESCUE_BEG;
|
|
|
|
lend->rescued = LABEL_RESCUE_END;
|
|
|
|
ADD_LABEL(ret, lstart);
|
|
|
|
bool prev_in_rescue = ISEQ_COMPILE_DATA(iseq)->in_rescue;
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->in_rescue = true;
|
|
|
|
if (begin_node->statements) {
|
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *)begin_node->statements);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->in_rescue = prev_in_rescue;
|
|
|
|
|
2024-01-31 21:42:11 +03:00
|
|
|
ADD_LABEL(ret, lend);
|
|
|
|
|
2024-01-22 22:24:34 +03:00
|
|
|
if (begin_node->else_clause) {
|
|
|
|
PM_POP_UNLESS_POPPED;
|
|
|
|
PM_COMPILE((pm_node_t *)begin_node->else_clause);
|
|
|
|
}
|
|
|
|
|
|
|
|
PM_NOP;
|
|
|
|
ADD_LABEL(ret, lcont);
|
|
|
|
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_ensure(rb_iseq_t *iseq, pm_begin_node_t *begin_node, LINK_ANCHOR *const ret, int lineno, bool popped, pm_scope_node_t *scope_node)
|
2024-01-22 22:24:34 +03:00
|
|
|
{
|
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
2024-02-08 18:14:27 +03:00
|
|
|
const pm_parser_t *parser = scope_node->parser;
|
2024-01-22 22:24:34 +03:00
|
|
|
|
|
|
|
LABEL *estart = NEW_LABEL(lineno);
|
|
|
|
LABEL *eend = NEW_LABEL(lineno);
|
|
|
|
LABEL *econt = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
struct ensure_range er;
|
|
|
|
struct iseq_compile_data_ensure_node_stack enl;
|
|
|
|
struct ensure_range *erange;
|
|
|
|
|
|
|
|
er.begin = estart;
|
|
|
|
er.end = eend;
|
|
|
|
er.next = 0;
|
|
|
|
push_ensure_entry(iseq, &enl, &er, (void *)begin_node->ensure_clause);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, estart);
|
|
|
|
if (begin_node->rescue_clause) {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_rescue(iseq, begin_node, ret, lineno, popped, scope_node);
|
2024-01-22 22:24:34 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (begin_node->statements) {
|
|
|
|
PM_COMPILE((pm_node_t *)begin_node->statements);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_LABEL(ret, eend);
|
|
|
|
ADD_LABEL(ret, econt);
|
|
|
|
|
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init((pm_node_t *)begin_node->ensure_clause, &next_scope_node, scope_node, parser);
|
2024-01-30 00:03:03 +03:00
|
|
|
|
|
|
|
rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(
|
|
|
|
&next_scope_node,
|
|
|
|
rb_str_concat(rb_str_new2("ensure in "), ISEQ_BODY(iseq)->location.label),
|
|
|
|
ISEQ_TYPE_ENSURE,
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_node_line_number(parser, (const pm_node_t *) begin_node->ensure_clause)
|
2024-01-30 00:03:03 +03:00
|
|
|
);
|
|
|
|
|
2024-01-22 22:24:34 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
|
|
|
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
|
|
|
|
|
|
|
|
erange = ISEQ_COMPILE_DATA(iseq)->ensure_node_stack->erange;
|
|
|
|
if (estart->link.next != &eend->link) {
|
|
|
|
while (erange) {
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_ENSURE, erange->begin, erange->end, child_iseq, econt);
|
|
|
|
erange = erange->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->ensure_node_stack = enl.prev;
|
|
|
|
|
|
|
|
// Compile the ensure entry
|
|
|
|
pm_statements_node_t *statements = begin_node->ensure_clause->statements;
|
|
|
|
if (statements) {
|
|
|
|
PM_COMPILE((pm_node_t *)statements);
|
|
|
|
PM_POP_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:36:29 +03:00
|
|
|
/**
|
|
|
|
* Returns true if the given call node can use the opt_str_uminus or
|
|
|
|
* opt_str_freeze instructions as an optimization with the current iseq options.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
pm_opt_str_freeze_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
!PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
|
|
|
|
node->receiver != NULL &&
|
|
|
|
PM_NODE_TYPE_P(node->receiver, PM_STRING_NODE) &&
|
|
|
|
node->arguments == NULL &&
|
|
|
|
node->block == NULL &&
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given call node can use the opt_aref_with optimization
|
|
|
|
* with the current iseq options.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
pm_opt_aref_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
!PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
|
|
|
|
node->arguments != NULL &&
|
|
|
|
PM_NODE_TYPE_P((const pm_node_t *) node->arguments, PM_ARGUMENTS_NODE) &&
|
|
|
|
((const pm_arguments_node_t *) node->arguments)->arguments.size == 1 &&
|
|
|
|
PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
|
|
|
|
node->block == NULL &&
|
|
|
|
!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given call node can use the opt_aset_with optimization
|
|
|
|
* with the current iseq options.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
pm_opt_aset_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
!PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
|
|
|
|
node->arguments != NULL &&
|
|
|
|
PM_NODE_TYPE_P((const pm_node_t *) node->arguments, PM_ARGUMENTS_NODE) &&
|
|
|
|
((const pm_arguments_node_t *) node->arguments)->arguments.size == 2 &&
|
|
|
|
PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
|
|
|
|
node->block == NULL &&
|
|
|
|
!ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal &&
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-08 18:14:27 +03:00
|
|
|
/**
|
|
|
|
* Compile the instructions necessary to read a constant, based on the options
|
|
|
|
* of the current iseq.
|
|
|
|
*/
|
2024-02-08 18:59:01 +03:00
|
|
|
static void
|
2024-02-08 18:14:27 +03:00
|
|
|
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)
|
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_location_line_number(scope_node->parser, name_loc);
|
2024-02-08 18:14:27 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
|
|
|
|
ISEQ_BODY(iseq)->ic_size++;
|
|
|
|
VALUE segments = rb_ary_new_from_args(1, name);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, opt_getconstant_path, segments);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 18:59:01 +03:00
|
|
|
/**
|
|
|
|
* Returns a Ruby array of the parts of the constant path node if it is constant
|
|
|
|
* reads all of the way down. If it isn't, then Qnil is returned.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
pm_constant_path_parts(const pm_node_t *node, const pm_scope_node_t *scope_node)
|
|
|
|
{
|
|
|
|
VALUE parts = rb_ary_new();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
|
|
|
case PM_CONSTANT_READ_NODE: {
|
|
|
|
const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
|
|
|
|
|
|
|
|
rb_ary_unshift(parts, name);
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
case PM_CONSTANT_PATH_NODE: {
|
|
|
|
const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, ((const pm_constant_read_node_t *) cast->child)->name));
|
|
|
|
|
|
|
|
rb_ary_unshift(parts, name);
|
|
|
|
if (cast->parent == NULL) {
|
|
|
|
rb_ary_unshift(parts, ID2SYM(idNULL));
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = cast->parent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile a constant path into two sequences of instructions, one for the
|
|
|
|
* owning expression if there is one (prefix) and one for the constant reads
|
|
|
|
* (body).
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
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)
|
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(scope_node->parser, node);
|
2024-02-08 18:59:01 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
|
|
|
case PM_CONSTANT_READ_NODE: {
|
|
|
|
const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
|
|
|
|
|
|
|
|
ADD_INSN1(body, &dummy_line_node, putobject, Qtrue);
|
|
|
|
ADD_INSN1(body, &dummy_line_node, getconstant, name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_CONSTANT_PATH_NODE: {
|
|
|
|
const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, ((const pm_constant_read_node_t *) cast->child)->name));
|
|
|
|
|
|
|
|
if (cast->parent == NULL) {
|
|
|
|
ADD_INSN(body, &dummy_line_node, pop);
|
|
|
|
ADD_INSN1(body, &dummy_line_node, putobject, rb_cObject);
|
|
|
|
ADD_INSN1(body, &dummy_line_node, putobject, Qtrue);
|
|
|
|
ADD_INSN1(body, &dummy_line_node, getconstant, name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_compile_constant_path(iseq, cast->parent, prefix, body, popped, scope_node);
|
|
|
|
ADD_INSN1(body, &dummy_line_node, putobject, Qfalse);
|
|
|
|
ADD_INSN1(body, &dummy_line_node, getconstant, name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
PM_COMPILE_INTO_ANCHOR(prefix, node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 20:00:19 +03:00
|
|
|
/**
|
|
|
|
* When we're compiling a case node, it's possible that we can speed it up using
|
|
|
|
* a dispatch hash, which will allow us to jump directly to the correct when
|
|
|
|
* clause body based on a hash lookup of the value. This can only happen when
|
|
|
|
* the conditions are literals that can be compiled into a hash key.
|
|
|
|
*
|
|
|
|
* This function accepts a dispatch hash and the condition of a when clause. It
|
|
|
|
* is responsible for compiling the condition into a hash key and then adding it
|
|
|
|
* to the dispatch hash.
|
|
|
|
*
|
|
|
|
* If the value can be successfully compiled into the hash, then this function
|
|
|
|
* returns the dispatch hash with the new key added. If the value cannot be
|
|
|
|
* compiled into the hash, then this function returns Qundef. In the case of
|
|
|
|
* Qundef, this function is signaling that the caller should abandon the
|
|
|
|
* optimization entirely.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
pm_compile_case_node_dispatch(VALUE dispatch, const pm_node_t *node, LABEL *label, const pm_scope_node_t *scope_node)
|
|
|
|
{
|
|
|
|
VALUE key = Qundef;
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
|
|
|
case PM_FALSE_NODE:
|
|
|
|
case PM_FLOAT_NODE:
|
|
|
|
case PM_INTEGER_NODE:
|
|
|
|
case PM_NIL_NODE:
|
|
|
|
case PM_SOURCE_FILE_NODE:
|
|
|
|
case PM_SOURCE_LINE_NODE:
|
|
|
|
case PM_SYMBOL_NODE:
|
|
|
|
case PM_TRUE_NODE:
|
2024-02-12 23:48:23 +03:00
|
|
|
key = pm_static_literal_value(node, scope_node);
|
2024-02-08 20:00:19 +03:00
|
|
|
break;
|
|
|
|
case PM_STRING_NODE: {
|
|
|
|
const pm_string_node_t *cast = (const pm_string_node_t *) node;
|
|
|
|
key = rb_fstring(parse_string_encoded(node, &cast->unescaped, scope_node->parser));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return Qundef;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NIL_P(rb_hash_lookup(dispatch, key))) {
|
|
|
|
rb_hash_aset(dispatch, key, ((VALUE) label) | 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dispatch;
|
|
|
|
}
|
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
/*
|
2024-02-12 23:16:44 +03:00
|
|
|
* Compiles a prism node into instruction sequences.
|
2023-08-29 19:27:00 +03:00
|
|
|
*
|
|
|
|
* iseq - The current instruction sequence object (used for locals)
|
2023-09-27 19:39:53 +03:00
|
|
|
* node - The prism node to compile
|
2023-10-20 12:58:23 +03:00
|
|
|
* ret - The linked list of instructions to append instructions onto
|
2023-08-29 19:27:00 +03:00
|
|
|
* popped - True if compiling something with no side effects, so instructions don't
|
|
|
|
* need to be added
|
2023-10-17 01:36:25 +03:00
|
|
|
* scope_node - Stores parser and local information
|
2023-08-29 19:27:00 +03:00
|
|
|
*/
|
2023-08-28 23:55:58 +03:00
|
|
|
static void
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
|
2023-08-30 00:17:08 +03:00
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
const pm_parser_t *parser = scope_node->parser;
|
|
|
|
const pm_line_column_t location = pm_newline_list_line_column(&parser->newline_list, node->location.start, parser->start_line);
|
2024-02-13 05:47:23 +03:00
|
|
|
int lineno = (int) location.line;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-12 23:37:48 +03:00
|
|
|
if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
|
2023-11-24 14:36:55 +03:00
|
|
|
int event = RUBY_EVENT_LINE;
|
|
|
|
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
|
|
|
|
if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
|
|
|
|
event |= RUBY_EVENT_COVERAGE_LINE;
|
|
|
|
}
|
2024-02-13 05:47:23 +03:00
|
|
|
PUSH_TRACE(ret, event);
|
2023-11-24 14:36:55 +03:00
|
|
|
}
|
|
|
|
|
2024-02-13 05:47:23 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
switch (PM_NODE_TYPE(node)) {
|
2023-10-16 22:00:01 +03:00
|
|
|
case PM_ALIAS_GLOBAL_VARIABLE_NODE: {
|
2024-02-12 23:37:48 +03:00
|
|
|
// alias $foo $bar
|
|
|
|
// ^^^^^^^^^^^^^^^
|
|
|
|
const pm_alias_global_variable_node_t *cast = (const pm_alias_global_variable_node_t *) node;
|
2023-10-16 22:00:01 +03:00
|
|
|
|
2024-02-12 23:37:48 +03:00
|
|
|
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
2024-02-14 22:17:32 +03:00
|
|
|
PUSH_INSN1(ret, location, putobject, ID2SYM(parse_location_symbol(&cast->new_name->location, parser)));
|
|
|
|
PUSH_INSN1(ret, location, putobject, ID2SYM(parse_location_symbol(&cast->old_name->location, parser)));
|
2023-10-16 22:00:01 +03:00
|
|
|
|
2024-02-12 23:37:48 +03:00
|
|
|
PUSH_SEND(ret, location, id_core_set_variable_alias, INT2FIX(2));
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-10-16 22:00:01 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_ALIAS_METHOD_NODE: {
|
2024-02-12 23:37:48 +03:00
|
|
|
// alias foo bar
|
|
|
|
// ^^^^^^^^^^^^^
|
|
|
|
const pm_alias_method_node_t *cast = (const pm_alias_method_node_t *) node;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-12 23:37:48 +03:00
|
|
|
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->new_name);
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->old_name);
|
2023-09-20 18:05:45 +03:00
|
|
|
|
2024-02-12 23:37:48 +03:00
|
|
|
PUSH_SEND(ret, location, id_core_set_method_alias, INT2FIX(3));
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-09-20 18:05:45 +03:00
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_AND_NODE: {
|
2024-02-12 23:37:48 +03:00
|
|
|
// a and b
|
|
|
|
// ^^^^^^^
|
|
|
|
const pm_and_node_t *cast = (const pm_and_node_t *) node;
|
2024-02-13 05:47:23 +03:00
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-12 23:37:48 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->left);
|
|
|
|
if (!popped) PUSH_INSN(ret, location, dup);
|
|
|
|
PUSH_INSNL(ret, location, branchunless, end_label);
|
|
|
|
|
|
|
|
if (!popped) PUSH_INSN(ret, location, pop);
|
|
|
|
PM_COMPILE(cast->right);
|
2024-02-13 05:47:23 +03:00
|
|
|
PUSH_LABEL(ret, end_label);
|
2024-02-12 23:37:48 +03:00
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2024-02-01 00:22:27 +03:00
|
|
|
case PM_ARGUMENTS_NODE:
|
2023-12-07 23:03:06 +03:00
|
|
|
// These are ArgumentsNodes that are not compiled directly by their
|
2024-02-13 05:47:23 +03:00
|
|
|
// parent call nodes, used in the cases of NextNodes, ReturnNodes, and
|
|
|
|
// BreakNodes. They can create an array like ArrayNode.
|
2024-02-01 00:22:27 +03:00
|
|
|
case PM_ARRAY_NODE: {
|
|
|
|
const pm_node_list_t *elements;
|
2024-02-13 05:47:23 +03:00
|
|
|
|
2024-02-01 00:22:27 +03:00
|
|
|
if (PM_NODE_TYPE(node) == PM_ARGUMENTS_NODE) {
|
2024-02-13 05:47:23 +03:00
|
|
|
// break foo
|
|
|
|
// ^^^
|
|
|
|
const pm_arguments_node_t *cast = (const pm_arguments_node_t *)node;
|
2024-02-01 00:22:27 +03:00
|
|
|
elements = &cast->arguments;
|
2024-02-13 05:47:23 +03:00
|
|
|
|
|
|
|
// If we are only returning a single element through one of the jump
|
|
|
|
// nodes, then we will only compile that node directly.
|
2024-02-01 00:22:27 +03:00
|
|
|
if (elements->size == 1) {
|
|
|
|
PM_COMPILE(elements->nodes[0]);
|
|
|
|
return;
|
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
2024-02-01 00:22:27 +03:00
|
|
|
else {
|
2024-02-13 05:47:23 +03:00
|
|
|
// [foo, bar, baz]
|
|
|
|
// ^^^^^^^^^^^^^^^
|
|
|
|
const pm_array_node_t *cast = (const pm_array_node_t *) node;
|
2024-02-01 00:22:27 +03:00
|
|
|
elements = &cast->elements;
|
2023-12-07 22:13:33 +03:00
|
|
|
}
|
2024-02-06 18:02:49 +03:00
|
|
|
|
2023-09-28 21:25:47 +03:00
|
|
|
// If every node in the array is static, then we can compile the entire
|
|
|
|
// array now instead of later.
|
2023-09-28 22:19:56 +03:00
|
|
|
if (pm_static_literal_p(node)) {
|
2023-09-28 21:25:47 +03:00
|
|
|
// We're only going to compile this node if it's not popped. If it
|
|
|
|
// is popped, then we know we don't need to do anything since it's
|
|
|
|
// statically known.
|
|
|
|
if (!popped) {
|
2024-02-01 00:22:27 +03:00
|
|
|
if (elements->size) {
|
2024-02-12 23:48:23 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node);
|
2024-02-13 05:47:23 +03:00
|
|
|
PUSH_INSN1(ret, location, duparray, value);
|
2023-11-29 23:02:32 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-02-13 05:47:23 +03:00
|
|
|
PUSH_INSN1(ret, location, newarray, INT2FIX(0));
|
2023-11-29 23:02:32 +03:00
|
|
|
}
|
2023-09-28 21:25:47 +03:00
|
|
|
}
|
2024-02-01 00:22:27 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-09-28 21:25:47 +03:00
|
|
|
// Here since we know there are possible side-effects inside the
|
|
|
|
// array contents, we're going to build it entirely at runtime.
|
|
|
|
// We'll do this by pushing all of the elements onto the stack and
|
|
|
|
// then combining them with newarray.
|
|
|
|
//
|
2024-02-01 00:22:27 +03:00
|
|
|
// If this array is popped, then this serves only to ensure we enact
|
2023-09-28 21:25:47 +03:00
|
|
|
// all side-effects (like method calls) that are contained within
|
2024-02-01 00:22:27 +03:00
|
|
|
// the array contents.
|
2024-02-06 18:02:49 +03:00
|
|
|
//
|
2024-02-01 00:22:27 +03:00
|
|
|
// We treat all sequences of non-splat elements as their
|
|
|
|
// own arrays, followed by a newarray, and then continually
|
2024-02-06 18:02:49 +03:00
|
|
|
// concat the arrays with the SplatNode nodes.
|
2024-02-01 00:22:27 +03:00
|
|
|
int new_array_size = 0;
|
2024-02-13 05:47:23 +03:00
|
|
|
|
2024-02-01 00:22:27 +03:00
|
|
|
bool need_to_concat_array = false;
|
|
|
|
bool has_kw_splat = false;
|
2024-02-06 18:02:49 +03:00
|
|
|
|
2024-02-01 00:22:27 +03:00
|
|
|
for (size_t index = 0; index < elements->size; index++) {
|
2024-02-13 05:47:23 +03:00
|
|
|
const pm_node_t *element = elements->nodes[index];
|
2023-11-22 17:49:26 +03:00
|
|
|
|
2024-02-13 05:47:23 +03:00
|
|
|
if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
|
|
|
|
const pm_splat_node_t *splat_element = (const pm_splat_node_t *) element;
|
|
|
|
|
|
|
|
// If we already have non-splat elements, we need to emit a
|
|
|
|
// newarray instruction.
|
|
|
|
if (new_array_size > 0) {
|
|
|
|
PUSH_INSN1(ret, location, newarray, INT2FIX(new_array_size));
|
2024-02-01 00:22:27 +03:00
|
|
|
new_array_size = 0;
|
2024-02-13 05:47:23 +03:00
|
|
|
|
|
|
|
// We don't want to emit a concat array in the case
|
|
|
|
// where we're seeing our first splat, and already have
|
|
|
|
// elements.
|
|
|
|
if (need_to_concat_array) PUSH_INSN(ret, location, concatarray);
|
2024-02-01 00:22:27 +03:00
|
|
|
}
|
2024-01-15 18:28:39 +03:00
|
|
|
|
2024-02-13 07:13:35 +03:00
|
|
|
if (splat_element->expression) {
|
|
|
|
PM_COMPILE_NOT_POPPED(splat_element->expression);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
|
|
|
}
|
2024-01-15 18:28:39 +03:00
|
|
|
|
2024-02-01 00:22:27 +03:00
|
|
|
if (index > 0) {
|
2024-02-13 05:47:23 +03:00
|
|
|
PUSH_INSN(ret, location, concatarray);
|
2024-02-01 00:22:27 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-02-13 05:47:23 +03:00
|
|
|
// If this is the first element of the array then we
|
|
|
|
// need to splatarray the elements into the list.
|
|
|
|
PUSH_INSN1(ret, location, splatarray, Qtrue);
|
2024-02-01 00:22:27 +03:00
|
|
|
}
|
2024-01-15 18:28:39 +03:00
|
|
|
|
2024-02-13 05:47:23 +03:00
|
|
|
// Since we have now seen a splat and are concat-ing arrays,
|
|
|
|
// all subsequent splats will need to concat as well.
|
2024-02-01 00:22:27 +03:00
|
|
|
need_to_concat_array = true;
|
|
|
|
}
|
2024-02-13 05:47:23 +03:00
|
|
|
else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
|
2024-02-01 00:22:27 +03:00
|
|
|
new_array_size++;
|
|
|
|
has_kw_splat = true;
|
2024-02-13 05:47:23 +03:00
|
|
|
pm_compile_hash_elements(&((const pm_keyword_hash_node_t *) element)->elements, lineno, iseq, ret, scope_node);
|
2024-02-01 00:22:27 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_array_size++;
|
2024-02-13 05:47:23 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(element);
|
2023-11-22 17:49:26 +03:00
|
|
|
}
|
2024-02-01 00:22:27 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2024-02-01 00:22:27 +03:00
|
|
|
if (new_array_size) {
|
|
|
|
if (has_kw_splat) {
|
2024-02-13 05:47:23 +03:00
|
|
|
PUSH_INSN1(ret, location, newarraykwsplat, INT2FIX(new_array_size));
|
2024-02-01 00:22:27 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-02-13 05:47:23 +03:00
|
|
|
PUSH_INSN1(ret, location, newarray, INT2FIX(new_array_size));
|
2023-11-22 17:49:26 +03:00
|
|
|
}
|
2024-02-13 05:47:23 +03:00
|
|
|
|
|
|
|
if (need_to_concat_array) PUSH_INSN(ret, location, concatarray);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2024-02-01 00:22:27 +03:00
|
|
|
|
2024-02-13 05:47:23 +03:00
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_ASSOC_NODE: {
|
2024-02-13 07:13:35 +03:00
|
|
|
// { foo: 1 }
|
|
|
|
// ^^^^^^
|
|
|
|
//
|
|
|
|
// foo(bar: 1)
|
|
|
|
// ^^^^^^
|
|
|
|
const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
|
|
|
|
|
|
|
|
PM_COMPILE(cast->key);
|
|
|
|
PM_COMPILE(cast->value);
|
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_ASSOC_SPLAT_NODE: {
|
2024-02-13 07:13:35 +03:00
|
|
|
// { **foo }
|
|
|
|
// ^^^^^
|
|
|
|
//
|
|
|
|
// def foo(**); bar(**); end
|
|
|
|
// ^^
|
|
|
|
const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
|
|
|
|
|
|
|
|
if (cast->value != NULL) {
|
|
|
|
PM_COMPILE(cast->value);
|
|
|
|
}
|
|
|
|
else if (!popped) {
|
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_BACK_REFERENCE_READ_NODE: {
|
2023-09-01 21:30:22 +03:00
|
|
|
if (!popped) {
|
|
|
|
// Since a back reference is `$<char>`, ruby represents the ID as the
|
|
|
|
// an rb_intern on the value after the `$`.
|
|
|
|
char *char_ptr = (char *)(node->location.start) + 1;
|
|
|
|
ID backref_val = INT2FIX(rb_intern2(char_ptr, 1)) << 1 | 1;
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getspecial, INT2FIX(1), backref_val);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_BEGIN_NODE: {
|
|
|
|
pm_begin_node_t *begin_node = (pm_begin_node_t *) node;
|
2023-11-10 17:51:14 +03:00
|
|
|
|
2023-12-04 15:27:34 +03:00
|
|
|
if (begin_node->ensure_clause) {
|
2024-01-22 22:24:34 +03:00
|
|
|
// Compiling the ensure clause will compile the rescue clause (if
|
|
|
|
// there is one), which will compile the begin statements
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_ensure(iseq, begin_node, ret, lineno, popped, scope_node);
|
2023-12-04 15:27:34 +03:00
|
|
|
}
|
2024-01-22 22:24:34 +03:00
|
|
|
else if (begin_node->rescue_clause) {
|
|
|
|
// Compiling rescue will compile begin statements (if applicable)
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_rescue(iseq, begin_node, ret, lineno, popped, scope_node);
|
2024-01-22 22:24:34 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If there is neither ensure or rescue, the just compile statements
|
2023-11-10 17:51:14 +03:00
|
|
|
if (begin_node->statements) {
|
|
|
|
PM_COMPILE((pm_node_t *)begin_node->statements);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_BLOCK_ARGUMENT_NODE: {
|
2024-01-26 02:08:44 +03:00
|
|
|
pm_block_argument_node_t *cast = (pm_block_argument_node_t *) node;
|
|
|
|
|
|
|
|
if (cast->expression) {
|
|
|
|
PM_COMPILE(cast->expression);
|
2023-10-13 20:59:37 +03:00
|
|
|
}
|
2024-01-23 22:43:53 +03:00
|
|
|
else {
|
2024-01-26 02:08:44 +03:00
|
|
|
// If there's no expression, this must be block forwarding.
|
|
|
|
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getblockparamproxy, INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1), INT2FIX(local_index.level));
|
2024-01-23 22:43:53 +03:00
|
|
|
}
|
2023-09-27 18:08:54 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_BREAK_NODE: {
|
|
|
|
pm_break_node_t *break_node = (pm_break_node_t *) node;
|
2023-12-11 18:27:46 +03:00
|
|
|
unsigned long throw_flag = 0;
|
|
|
|
if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
|
|
|
|
/* while/until */
|
|
|
|
LABEL *splabel = NEW_LABEL(0);
|
|
|
|
ADD_LABEL(ret, splabel);
|
|
|
|
ADD_ADJUST(ret, &dummy_line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
|
|
|
|
if (break_node->arguments) {
|
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *)break_node->arguments);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
2024-01-19 02:11:17 +03:00
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
|
2023-12-11 18:27:46 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
|
|
|
|
ADD_ADJUST_RESTORE(ret, splabel);
|
|
|
|
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
} else {
|
|
|
|
const rb_iseq_t *ip = iseq;
|
|
|
|
|
|
|
|
while (ip) {
|
|
|
|
if (!ISEQ_COMPILE_DATA(ip)) {
|
|
|
|
ip = 0;
|
|
|
|
break;
|
|
|
|
}
|
2023-08-31 00:27:08 +03:00
|
|
|
|
2023-12-11 18:27:46 +03:00
|
|
|
if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
|
|
|
|
throw_flag = VM_THROW_NO_ESCAPE_FLAG;
|
|
|
|
}
|
|
|
|
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
|
|
|
|
throw_flag = 0;
|
|
|
|
}
|
|
|
|
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
|
|
|
|
COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with break");
|
2024-02-08 17:57:40 +03:00
|
|
|
return;
|
2023-12-11 18:27:46 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ip = ISEQ_BODY(ip)->parent_iseq;
|
|
|
|
continue;
|
|
|
|
}
|
2023-08-31 00:27:08 +03:00
|
|
|
|
2023-12-11 18:27:46 +03:00
|
|
|
/* escape from block */
|
|
|
|
if (break_node->arguments) {
|
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *)break_node->arguments);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, throw, INT2FIX(throw_flag | TAG_BREAK));
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
COMPILE_ERROR(ERROR_ARGS "Invalid break");
|
2023-12-13 21:28:19 +03:00
|
|
|
rb_bug("Invalid break");
|
2023-12-11 18:27:46 +03:00
|
|
|
}
|
2023-08-31 00:27:08 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CALL_NODE: {
|
2024-02-08 17:36:29 +03:00
|
|
|
const pm_call_node_t *call_node = (const pm_call_node_t *) node;
|
2023-12-12 01:35:13 +03:00
|
|
|
LABEL *start = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
if (call_node->block) {
|
|
|
|
ADD_LABEL(ret, start);
|
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
[PRISM] Fix `PM_CALL_NODE` assignment
This PR fixes ruby/prism#1963. Array and variable assignment was broken
for call nodes. The change checks if the `method_id` is related to
assignment and if is adds a `putnil`, `setn` and a `pop`.
The incorrect instructions meant that in some cases (demonstrated in
tests) the wrong value would be returned.
I verified that this fixes the test mentioned in the issue
(run: `RUBY_ISEQ_DUMP_DEBUG=prism make test/-ext-/st/test_numhash.rb`)
Incorrect instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(4,10)>
0000 putnil ( 4)[Li]
0001 putself
0002 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0005 putself
0006 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0009 putself
0010 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0013 setn 3
0015 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0018 pop
0019 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:3 (3,0)-(3,10)>
0000 putself ( 3)[Li]
0001 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0004 putself
0005 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0008 putself
0009 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0012 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0015 leave
```
Fixed instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(4,10)>
0000 putnil ( 4)[Li]
0001 putself
0002 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0005 putself
0006 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0009 putself
0010 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0013 setn 3
0015 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0018 pop
0019 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:3 (3,0)-(3,10)>
0000 putnil ( 3)[Li]
0001 putself
0002 send <calldata!mid:tbl, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0005 putself
0006 send <calldata!mid:i, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0009 putself
0010 send <calldata!mid:j, argc:0, FCALL|VCALL|ARGS_SIMPLE>, nil
0013 setn 3
0015 send <calldata!mid:[]=, argc:2, ARGS_SIMPLE>, nil
0018 pop
0019 leave
```
Fixes ruby/prism#1963
2023-12-08 23:00:48 +03:00
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, call_node->name);
|
|
|
|
|
2024-02-08 17:36:29 +03:00
|
|
|
switch (method_id) {
|
|
|
|
case idUMinus: {
|
|
|
|
if (pm_opt_str_freeze_p(iseq, call_node)) {
|
2024-02-14 22:17:32 +03:00
|
|
|
VALUE value = rb_fstring(parse_string_encoded(call_node->receiver, &((const pm_string_node_t * )call_node->receiver)->unescaped, parser));
|
2024-02-08 17:36:29 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, opt_str_uminus, value, new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
|
|
|
|
return;
|
2024-01-29 22:58:49 +03:00
|
|
|
}
|
2024-02-08 17:36:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case idFreeze: {
|
|
|
|
if (pm_opt_str_freeze_p(iseq, call_node)) {
|
2024-02-14 22:17:32 +03:00
|
|
|
VALUE value = rb_fstring(parse_string_encoded(call_node->receiver, &((const pm_string_node_t * )call_node->receiver)->unescaped, parser));
|
2024-02-08 17:36:29 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, opt_str_freeze, value, new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
|
|
|
|
return;
|
2024-01-29 22:58:49 +03:00
|
|
|
}
|
2024-02-08 17:36:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case idAREF: {
|
|
|
|
if (pm_opt_aref_with_p(iseq, call_node)) {
|
|
|
|
const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) call_node->arguments)->arguments.nodes[0];
|
2024-02-14 22:17:32 +03:00
|
|
|
VALUE value = rb_fstring(parse_string_encoded((const pm_node_t *) string, &string->unescaped, parser));
|
2024-02-08 17:36:29 +03:00
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(call_node->receiver);
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, opt_aref_with, value, new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
|
|
|
|
|
|
|
|
if (popped) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case idASET: {
|
|
|
|
if (pm_opt_aset_with_p(iseq, call_node)) {
|
|
|
|
const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) call_node->arguments)->arguments.nodes[0];
|
2024-02-14 22:17:32 +03:00
|
|
|
VALUE value = rb_fstring(parse_string_encoded((const pm_node_t *) string, &string->unescaped, parser));
|
2024-02-08 17:36:29 +03:00
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(call_node->receiver);
|
|
|
|
PM_COMPILE_NOT_POPPED(((const pm_arguments_node_t *) call_node->arguments)->arguments.nodes[1]);
|
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, opt_aset_with, value, new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
|
2024-02-01 22:56:26 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
2024-02-08 17:36:29 +03:00
|
|
|
return;
|
2024-02-01 22:56:26 +03:00
|
|
|
}
|
2024-02-08 17:36:29 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-02-01 22:56:26 +03:00
|
|
|
}
|
2024-02-08 17:36:29 +03:00
|
|
|
|
|
|
|
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
|
|
|
|
PM_PUTNIL;
|
2024-02-03 00:57:51 +03:00
|
|
|
}
|
2024-02-03 00:53:55 +03:00
|
|
|
|
2024-02-08 17:36:29 +03:00
|
|
|
if (call_node->receiver == NULL) {
|
|
|
|
PM_PUTSELF;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_COMPILE_NOT_POPPED(call_node->receiver);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2024-02-08 17:36:29 +03:00
|
|
|
pm_compile_call(iseq, call_node, ret, popped, scope_node, method_id, start);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-25 18:20:07 +03:00
|
|
|
case PM_CALL_AND_WRITE_NODE: {
|
2024-02-01 00:59:03 +03:00
|
|
|
pm_call_and_write_node_t *cast = (pm_call_and_write_node_t*) node;
|
|
|
|
pm_compile_call_and_or_write_node(true, cast->receiver, cast->value, cast->write_name, cast->read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), ret, iseq, lineno, popped, scope_node);
|
2023-10-25 18:40:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case PM_CALL_OR_WRITE_NODE: {
|
2024-02-01 00:59:03 +03:00
|
|
|
pm_call_or_write_node_t *cast = (pm_call_or_write_node_t*) node;
|
|
|
|
pm_compile_call_and_or_write_node(false, cast->receiver, cast->value, cast->write_name, cast->read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), ret, iseq, lineno, popped, scope_node);
|
2023-10-25 18:20:07 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-25 18:58:45 +03:00
|
|
|
case PM_CALL_OPERATOR_WRITE_NODE: {
|
2024-02-01 00:59:03 +03:00
|
|
|
// Call operator writes occur when you have a call node on the left-hand
|
|
|
|
// side of a write operator that is not `=`. As an example,
|
|
|
|
// `foo.bar *= 1`. This breaks down to caching the receiver on the
|
|
|
|
// stack and then performing three method calls, one to read the value,
|
|
|
|
// one to compute the result, and one to write the result back to the
|
|
|
|
// receiver.
|
|
|
|
const pm_call_operator_write_node_t *cast = (const pm_call_operator_write_node_t *) node;
|
2023-10-25 18:58:45 +03:00
|
|
|
int flag = 0;
|
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
|
2023-10-25 18:58:45 +03:00
|
|
|
flag = VM_CALL_FCALL;
|
|
|
|
}
|
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->receiver);
|
|
|
|
|
|
|
|
LABEL *safe_label = NULL;
|
|
|
|
if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
|
|
|
|
safe_label = NEW_LABEL(lineno);
|
|
|
|
PM_DUP;
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchnil, safe_label);
|
|
|
|
}
|
2023-10-25 18:58:45 +03:00
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-10-25 18:58:45 +03:00
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
ID id_read_name = pm_constant_id_lookup(scope_node, cast->read_name);
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, id_read_name, INT2FIX(0), INT2FIX(flag));
|
2023-10-25 18:58:45 +03:00
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
|
|
|
ID id_operator = pm_constant_id_lookup(scope_node, cast->operator);
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_operator, INT2FIX(1));
|
2023-10-25 18:58:45 +03:00
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
PM_SWAP;
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
2024-02-01 00:59:03 +03:00
|
|
|
ID id_write_name = pm_constant_id_lookup(scope_node, cast->write_name);
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, id_write_name, INT2FIX(1), INT2FIX(flag));
|
|
|
|
|
|
|
|
if (safe_label != NULL && popped) ADD_LABEL(ret, safe_label);
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_POP;
|
2024-02-01 00:59:03 +03:00
|
|
|
if (safe_label != NULL && !popped) ADD_LABEL(ret, safe_label);
|
2023-10-25 18:58:45 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-10-24 21:24:36 +03:00
|
|
|
case PM_CASE_NODE: {
|
2024-02-08 19:29:36 +03:00
|
|
|
// case foo; when bar; end
|
|
|
|
// ^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
const pm_case_node_t *cast = (const pm_case_node_t *) node;
|
|
|
|
const pm_node_list_t *conditions = &cast->conditions;
|
|
|
|
|
|
|
|
// This is the anchor that we will compile the conditions of the various
|
|
|
|
// `when` nodes into. If a match is found, they will need to jump into
|
|
|
|
// the body_seq anchor to the correct spot.
|
|
|
|
DECL_ANCHOR(cond_seq);
|
|
|
|
INIT_ANCHOR(cond_seq);
|
|
|
|
|
|
|
|
// This is the anchor that we will compile the bodies of the various
|
|
|
|
// `when` nodes into. We'll make sure that the clauses that are compiled
|
|
|
|
// jump into the correct spots within this anchor.
|
|
|
|
DECL_ANCHOR(body_seq);
|
|
|
|
INIT_ANCHOR(body_seq);
|
|
|
|
|
|
|
|
// This is the label where all of the when clauses will jump to if they
|
|
|
|
// have matched and are done executing their bodies.
|
2023-10-24 21:24:36 +03:00
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
2024-02-08 20:00:19 +03:00
|
|
|
// If we have a predicate on this case statement, then it's going to
|
|
|
|
// compare all of the various when clauses to the predicate. If we
|
|
|
|
// don't, then it's basically an if-elsif-else chain.
|
|
|
|
if (cast->predicate == NULL) {
|
|
|
|
// Loop through each clauses in the case node and compile each of
|
|
|
|
// the conditions within them into cond_seq. If they match, they
|
|
|
|
// should jump into their respective bodies in body_seq.
|
|
|
|
for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
|
|
|
|
const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
|
|
|
|
const pm_node_list_t *conditions = &clause->conditions;
|
|
|
|
|
2024-02-14 22:17:32 +03:00
|
|
|
int clause_lineno = pm_node_line_number(parser, (const pm_node_t *) clause);
|
2024-02-08 20:00:19 +03:00
|
|
|
LABEL *label = NEW_LABEL(clause_lineno);
|
|
|
|
|
|
|
|
ADD_LABEL(body_seq, label);
|
|
|
|
if (clause->statements != NULL) {
|
|
|
|
pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
|
2024-01-12 21:04:59 +03:00
|
|
|
}
|
2024-02-08 20:00:19 +03:00
|
|
|
else if (!popped) {
|
|
|
|
ADD_INSN(body_seq, &dummy_line_node, putnil);
|
2023-10-24 21:24:36 +03:00
|
|
|
}
|
2024-01-12 21:04:59 +03:00
|
|
|
|
2024-02-08 20:00:19 +03:00
|
|
|
ADD_INSNL(body_seq, &dummy_line_node, jump, end_label);
|
2024-02-08 19:29:36 +03:00
|
|
|
|
2024-02-08 20:00:19 +03:00
|
|
|
// Compile each of the conditions for the when clause into the
|
|
|
|
// cond_seq. Each one should have a unique condition and should
|
|
|
|
// jump to the subsequent one if it doesn't match.
|
|
|
|
for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
|
|
|
|
const pm_node_t *condition = conditions->nodes[condition_index];
|
|
|
|
|
|
|
|
if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
|
|
|
|
ADD_INSN(cond_seq, &dummy_line_node, putnil);
|
|
|
|
pm_compile_node(iseq, condition, cond_seq, false, scope_node);
|
|
|
|
ADD_INSN1(cond_seq, &dummy_line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
|
|
|
|
ADD_INSNL(cond_seq, &dummy_line_node, branchif, label);
|
|
|
|
}
|
|
|
|
else {
|
2024-02-14 22:17:32 +03:00
|
|
|
LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
|
2024-02-08 20:00:19 +03:00
|
|
|
pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label, false, scope_node);
|
|
|
|
ADD_LABEL(cond_seq, next_label);
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 19:29:36 +03:00
|
|
|
}
|
|
|
|
|
2024-02-08 20:00:19 +03:00
|
|
|
// Compile the consequent else clause if there is one.
|
|
|
|
if (cast->consequent) {
|
|
|
|
pm_compile_node(iseq, (const pm_node_t *) cast->consequent, cond_seq, popped, scope_node);
|
2024-02-08 19:29:36 +03:00
|
|
|
}
|
|
|
|
else if (!popped) {
|
2024-02-08 20:00:19 +03:00
|
|
|
ADD_INSN(cond_seq, &dummy_line_node, putnil);
|
2024-02-08 19:29:36 +03:00
|
|
|
}
|
|
|
|
|
2024-02-08 20:00:19 +03:00
|
|
|
// Finally, jump to the end label if none of the other conditions
|
|
|
|
// have matched.
|
|
|
|
ADD_INSNL(cond_seq, &dummy_line_node, jump, end_label);
|
|
|
|
ADD_SEQ(ret, cond_seq);
|
2023-10-24 21:24:36 +03:00
|
|
|
}
|
2024-02-08 20:00:19 +03:00
|
|
|
else {
|
|
|
|
// This is the label where everything will fall into if none of the
|
|
|
|
// conditions matched.
|
|
|
|
LABEL *else_label = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
// It's possible for us to speed up the case node by using a
|
|
|
|
// dispatch hash. This is a hash that maps the conditions of the
|
|
|
|
// various when clauses to the labels of their bodies. If we can
|
|
|
|
// compile the conditions into a hash key, then we can use a hash
|
|
|
|
// lookup to jump directly to the correct when clause body.
|
|
|
|
VALUE dispatch = Qundef;
|
|
|
|
if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
|
|
|
|
dispatch = rb_hash_new();
|
|
|
|
RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're going to loop through each of the conditions in the case
|
|
|
|
// node and compile each of their contents into both the cond_seq
|
|
|
|
// and the body_seq. Each condition will use its own label to jump
|
|
|
|
// from its conditions into its body.
|
|
|
|
//
|
|
|
|
// Note that none of the code in the loop below should be adding
|
|
|
|
// anything to ret, as we're going to be laying out the entire case
|
|
|
|
// node instructions later.
|
|
|
|
for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
|
|
|
|
const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
|
|
|
|
const pm_node_list_t *conditions = &clause->conditions;
|
|
|
|
|
|
|
|
LABEL *label = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
// Compile each of the conditions for the when clause into the
|
|
|
|
// cond_seq. Each one should have a unique comparison that then
|
|
|
|
// jumps into the body if it matches.
|
|
|
|
for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
|
|
|
|
const pm_node_t *condition = conditions->nodes[condition_index];
|
|
|
|
|
|
|
|
// If we haven't already abandoned the optimization, then
|
|
|
|
// we're going to try to compile the condition into the
|
|
|
|
// dispatch hash.
|
|
|
|
if (dispatch != Qundef) {
|
|
|
|
dispatch = pm_compile_case_node_dispatch(dispatch, condition, label, scope_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
|
|
|
|
ADD_INSN(cond_seq, &dummy_line_node, dup);
|
|
|
|
pm_compile_node(iseq, condition, cond_seq, false, scope_node);
|
|
|
|
ADD_INSN1(cond_seq, &dummy_line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
|
|
|
|
const pm_string_node_t *string = (const pm_string_node_t *) condition;
|
2024-02-14 22:17:32 +03:00
|
|
|
VALUE value = rb_fstring(parse_string_encoded((const pm_node_t *) string, &string->unescaped, parser));
|
2024-02-08 20:00:19 +03:00
|
|
|
ADD_INSN1(cond_seq, &dummy_line_node, putobject, value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_compile_node(iseq, condition, cond_seq, false, scope_node);
|
|
|
|
}
|
2023-10-24 21:24:36 +03:00
|
|
|
|
2024-02-08 20:00:19 +03:00
|
|
|
ADD_INSN1(cond_seq, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
ADD_SEND_WITH_FLAG(cond_seq, &dummy_line_node, idEqq, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(cond_seq, &dummy_line_node, branchif, label);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, add the label to the body and compile the body of the
|
|
|
|
// when clause. This involves popping the predicate, compiling
|
|
|
|
// the statements to be executed, and then compiling a jump to
|
|
|
|
// the end of the case node.
|
|
|
|
ADD_LABEL(body_seq, label);
|
|
|
|
ADD_INSN(body_seq, &dummy_line_node, pop);
|
|
|
|
|
|
|
|
if (clause->statements != NULL) {
|
|
|
|
pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
|
|
|
|
}
|
|
|
|
else if (!popped) {
|
|
|
|
ADD_INSN(body_seq, &dummy_line_node, putnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(body_seq, &dummy_line_node, jump, end_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have compiled the conditions and the bodies of the
|
|
|
|
// various when clauses, we can compile the predicate, lay out the
|
|
|
|
// conditions, compile the fallback consequent if there is one, and
|
|
|
|
// finally put in the bodies of the when clauses.
|
2024-02-08 19:29:36 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->predicate);
|
|
|
|
|
2024-02-08 20:00:19 +03:00
|
|
|
// If we have a dispatch hash, then we'll use it here to create the
|
|
|
|
// optimization.
|
|
|
|
if (dispatch != Qundef) {
|
|
|
|
PM_DUP;
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, opt_case_dispatch, dispatch, else_label);
|
|
|
|
LABEL_REF(else_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEQ(ret, cond_seq);
|
|
|
|
|
|
|
|
// Compile either the explicit else clause or an implicit else
|
|
|
|
// clause.
|
|
|
|
ADD_LABEL(ret, else_label);
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_POP;
|
2023-10-24 21:24:36 +03:00
|
|
|
|
2024-02-08 20:00:19 +03:00
|
|
|
if (cast->consequent != NULL) {
|
|
|
|
PM_COMPILE((const pm_node_t *) cast->consequent);
|
|
|
|
}
|
|
|
|
else if (!popped) {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, end_label);
|
2023-10-24 21:24:36 +03:00
|
|
|
}
|
|
|
|
|
2024-02-08 19:29:36 +03:00
|
|
|
ADD_SEQ(ret, body_seq);
|
2023-10-24 21:24:36 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
2024-02-08 20:00:19 +03:00
|
|
|
|
2023-10-24 21:24:36 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-12-13 19:34:05 +03:00
|
|
|
case PM_CASE_MATCH_NODE: {
|
|
|
|
// If you use the `case` keyword to create a case match node, it will
|
|
|
|
// match against all of the `in` clauses until it finds one that
|
|
|
|
// matches. If it doesn't find one, it can optionally fall back to an
|
|
|
|
// `else` clause. If none is present and a match wasn't found, it will
|
|
|
|
// raise an appropriate error.
|
|
|
|
const pm_case_match_node_t *cast = (const pm_case_match_node_t *) node;
|
|
|
|
|
|
|
|
// This is the anchor that we will compile the bodies of the various
|
|
|
|
// `in` nodes into. We'll make sure that the patterns that are compiled
|
|
|
|
// jump into the correct spots within this anchor.
|
|
|
|
DECL_ANCHOR(body_seq);
|
|
|
|
INIT_ANCHOR(body_seq);
|
|
|
|
|
|
|
|
// This is the anchor that we will compile the patterns of the various
|
|
|
|
// `in` nodes into. If a match is found, they will need to jump into the
|
|
|
|
// body_seq anchor to the correct spot.
|
|
|
|
DECL_ANCHOR(cond_seq);
|
|
|
|
INIT_ANCHOR(cond_seq);
|
|
|
|
|
|
|
|
// This label is used to indicate the end of the entire node. It is
|
|
|
|
// jumped to after the entire stack is cleaned up.
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
// This label is used as the fallback for the case match. If no match is
|
|
|
|
// found, then we jump to this label. This is either an `else` clause or
|
|
|
|
// an error handler.
|
|
|
|
LABEL *else_label = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
// We're going to use this to uniquely identify each branch so that we
|
|
|
|
// can track coverage information.
|
|
|
|
int branch_id = 0;
|
|
|
|
// VALUE branches = 0;
|
|
|
|
|
|
|
|
// If there is only one pattern, then the behavior changes a bit. It
|
|
|
|
// effectively gets treated as a match required node (this is how it is
|
|
|
|
// represented in the other parser).
|
|
|
|
bool in_single_pattern = cast->consequent == NULL && cast->conditions.size == 1;
|
|
|
|
|
|
|
|
// First, we're going to push a bunch of stuff onto the stack that is
|
|
|
|
// going to serve as our scratch space.
|
|
|
|
if (in_single_pattern) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil); // key error key
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil); // key error matchee
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse); // key error?
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil); // error string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we're going to compile the value to match against.
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil); // deconstruct cache
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->predicate);
|
|
|
|
|
|
|
|
// Next, we'll loop through every in clause and compile its body into
|
|
|
|
// the body_seq anchor and its pattern into the cond_seq anchor. We'll
|
|
|
|
// make sure the pattern knows how to jump correctly into the body if it
|
|
|
|
// finds a match.
|
|
|
|
for (size_t index = 0; index < cast->conditions.size; index++) {
|
|
|
|
const pm_node_t *condition = cast->conditions.nodes[index];
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
|
2023-12-13 19:34:05 +03:00
|
|
|
|
2023-12-14 23:25:54 +03:00
|
|
|
const pm_in_node_t *in_node = (const pm_in_node_t *) condition;
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
pm_line_node_t in_line;
|
|
|
|
pm_line_node(&in_line, scope_node, (const pm_node_t *) in_node);
|
|
|
|
|
|
|
|
pm_line_node_t pattern_line;
|
|
|
|
pm_line_node(&pattern_line, scope_node, (const pm_node_t *) in_node->pattern);
|
|
|
|
|
|
|
|
if (branch_id) {
|
|
|
|
ADD_INSN(body_seq, &in_line.node, putnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
LABEL *body_label = NEW_LABEL(in_line.lineno);
|
|
|
|
ADD_LABEL(body_seq, body_label);
|
|
|
|
ADD_INSN1(body_seq, &in_line.node, adjuststack, INT2FIX(in_single_pattern ? 6 : 2));
|
|
|
|
|
|
|
|
// TODO: We need to come back to this and enable trace branch
|
|
|
|
// coverage. At the moment we can't call this function because it
|
|
|
|
// accepts a NODE* and not a pm_node_t*.
|
|
|
|
// add_trace_branch_coverage(iseq, body_seq, in_node->statements || in, branch_id++, "in", branches);
|
|
|
|
|
|
|
|
branch_id++;
|
|
|
|
if (in_node->statements != NULL) {
|
|
|
|
PM_COMPILE_INTO_ANCHOR(body_seq, (const pm_node_t *) in_node->statements);
|
|
|
|
} else if (!popped) {
|
|
|
|
ADD_INSN(body_seq, &in_line.node, putnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(body_seq, &in_line.node, jump, end_label);
|
|
|
|
LABEL *next_pattern_label = NEW_LABEL(pattern_line.lineno);
|
|
|
|
|
|
|
|
ADD_INSN(cond_seq, &pattern_line.node, dup);
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_pattern(iseq, scope_node, in_node->pattern, cond_seq, body_label, next_pattern_label, in_single_pattern, false, true, 2);
|
2023-12-13 19:34:05 +03:00
|
|
|
ADD_LABEL(cond_seq, next_pattern_label);
|
|
|
|
LABEL_UNREMOVABLE(next_pattern_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cast->consequent != NULL) {
|
|
|
|
// If we have an `else` clause, then this becomes our fallback (and
|
|
|
|
// there is no need to compile in code to potentially raise an
|
|
|
|
// error).
|
|
|
|
const pm_else_node_t *else_node = (const pm_else_node_t *) cast->consequent;
|
|
|
|
|
|
|
|
ADD_LABEL(cond_seq, else_label);
|
|
|
|
ADD_INSN(cond_seq, &dummy_line_node, pop);
|
|
|
|
ADD_INSN(cond_seq, &dummy_line_node, pop);
|
|
|
|
|
|
|
|
// TODO: trace branch coverage
|
|
|
|
// add_trace_branch_coverage(iseq, cond_seq, cast->consequent, branch_id, "else", branches);
|
|
|
|
|
|
|
|
if (else_node->statements != NULL) {
|
|
|
|
PM_COMPILE_INTO_ANCHOR(cond_seq, (const pm_node_t *) else_node->statements);
|
|
|
|
} else if (!popped) {
|
|
|
|
ADD_INSN(cond_seq, &dummy_line_node, putnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(cond_seq, &dummy_line_node, jump, end_label);
|
|
|
|
ADD_INSN(cond_seq, &dummy_line_node, putnil);
|
|
|
|
if (popped) {
|
|
|
|
ADD_INSN(cond_seq, &dummy_line_node, putnil);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, if we do not have an `else` clause, we will compile in
|
|
|
|
// the code to handle raising an appropriate error.
|
|
|
|
ADD_LABEL(cond_seq, else_label);
|
|
|
|
|
|
|
|
// TODO: trace branch coverage
|
|
|
|
// add_trace_branch_coverage(iseq, cond_seq, orig_node, branch_id, "else", branches);
|
|
|
|
|
|
|
|
if (in_single_pattern) {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_pattern_error_handler(iseq, scope_node, node, cond_seq, end_label, popped);
|
2023-12-13 19:34:05 +03:00
|
|
|
} else {
|
|
|
|
ADD_INSN1(cond_seq, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(cond_seq, &dummy_line_node, putobject, rb_eNoMatchingPatternError);
|
|
|
|
ADD_INSN1(cond_seq, &dummy_line_node, topn, INT2FIX(2));
|
|
|
|
ADD_SEND(cond_seq, &dummy_line_node, id_core_raise, INT2FIX(2));
|
|
|
|
|
|
|
|
ADD_INSN1(cond_seq, &dummy_line_node, adjuststack, INT2FIX(3));
|
|
|
|
if (!popped) ADD_INSN(cond_seq, &dummy_line_node, putnil);
|
|
|
|
ADD_INSNL(cond_seq, &dummy_line_node, jump, end_label);
|
|
|
|
ADD_INSN1(cond_seq, &dummy_line_node, dupn, INT2FIX(1));
|
|
|
|
if (popped) ADD_INSN(cond_seq, &dummy_line_node, putnil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At the end of all of this compilation, we will add the code for the
|
|
|
|
// conditions first, then the various bodies, then mark the end of the
|
|
|
|
// entire sequence with the end label.
|
|
|
|
ADD_SEQ(ret, cond_seq);
|
|
|
|
ADD_SEQ(ret, body_seq);
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CLASS_NODE: {
|
|
|
|
pm_class_node_t *class_node = (pm_class_node_t *)node;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID class_id = pm_constant_id_lookup(scope_node, class_node->name);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
VALUE class_name = rb_str_freeze(rb_sprintf("<class:%"PRIsVALUE">", rb_id2str(class_id)));
|
|
|
|
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init((pm_node_t *)class_node, &next_scope_node, scope_node, parser);
|
2024-01-18 19:38:53 +03:00
|
|
|
const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(&next_scope_node, class_name, ISEQ_TYPE_CLASS, lineno);
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
// TODO: Once we merge constant path nodes correctly, fix this flag
|
|
|
|
const int flags = VM_DEFINECLASS_TYPE_CLASS |
|
|
|
|
(class_node->superclass ? VM_DEFINECLASS_FLAG_HAS_SUPERCLASS : 0) |
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_class_path(ret, iseq, class_node->constant_path, &dummy_line_node, false, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
if (class_node->superclass) {
|
2023-10-13 20:59:37 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(class_node->superclass);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defineclass, ID2SYM(class_id), class_iseq, INT2FIX(flags));
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)class_iseq);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
|
|
|
|
pm_class_variable_and_write_node_t *class_variable_and_write_node = (pm_class_variable_and_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID class_variable_name_id = pm_constant_id_lookup(scope_node, class_variable_and_write_node->name);
|
2023-09-02 00:20:03 +03:00
|
|
|
VALUE class_variable_name_val = ID2SYM(class_variable_name_id);
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getclassvariable,
|
|
|
|
class_variable_name_val,
|
|
|
|
get_cvar_ic_value(iseq, class_variable_name_id));
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, end_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(class_variable_and_write_node->value);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, setclassvariable,
|
|
|
|
class_variable_name_val,
|
|
|
|
get_cvar_ic_value(iseq, class_variable_name_id));
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
|
|
|
|
pm_class_variable_operator_write_node_t *class_variable_operator_write_node = (pm_class_variable_operator_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID class_variable_name_id = pm_constant_id_lookup(scope_node, class_variable_operator_write_node->name);
|
2023-09-02 00:20:03 +03:00
|
|
|
VALUE class_variable_name_val = ID2SYM(class_variable_name_id);
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getclassvariable,
|
|
|
|
class_variable_name_val,
|
|
|
|
get_cvar_ic_value(iseq, class_variable_name_id));
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(class_variable_operator_write_node->value);
|
2023-10-17 01:36:25 +03:00
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, class_variable_operator_write_node->operator);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
int flags = VM_CALL_ARGS_SIMPLE;
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, method_id, INT2NUM(1), INT2FIX(flags));
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, setclassvariable,
|
|
|
|
class_variable_name_val,
|
|
|
|
get_cvar_ic_value(iseq, class_variable_name_id));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
|
|
|
|
pm_class_variable_or_write_node_t *class_variable_or_write_node = (pm_class_variable_or_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
[Prism] Fix cvar or assignment instructions
The instructions for `PM_CLASS_VARIABLE_OR_WRITE_NODE` were incorrect as
they were missing a `putnil`, a `defined`, and a `branchunless`.
I verified this is fixed via the instructions and running the following:
`RUBY_ISEQ_DUMP_DEBUG=prism make test/csv/interface/test_read_write.rb`.
These new instructions can't go in the defined function because
`defined?(@@fop ||= 1)` should return "assignment" not "class variable".
Instructions before:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(30,11)>
0000 putnil ( 30)[Li]
0001 defined class variable, :@@foo, true
0005 branchunless 14
0007 getclassvariable :@@foo, <is:0>
0010 dup
0011 branchif 20
0013 pop
0014 putobject 1
0016 dup
0017 setclassvariable :@@foo, <is:0>
0020 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:29 (29,0)-(29,11)>
0000 getclassvariable :@@foo, <is:0> ( 29)[Li]
0003 dup
0004 branchif 13
0006 pop
0007 putobject 1
0009 dup
0010 setclassvariable :@@foo, <is:0>
0013 leave
```
Instructions after:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(30,11)>
0000 putnil ( 30)[Li]
0001 defined class variable, :@@foo, true
0005 branchunless 14
0007 getclassvariable :@@foo, <is:0>
0010 dup
0011 branchif 20
0013 pop
0014 putobject 1
0016 dup
0017 setclassvariable :@@foo, <is:0>
0020 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:29 (29,0)-(29,11)>
0000 putnil ( 29)[Li]
0001 defined class variable, :@@foo, true
0005 branchunless 14
0007 getclassvariable :@@foo, <is:0>
0010 dup
0011 branchif 20
0013 pop
0014 putobject 1
0016 dup
0017 setclassvariable :@@foo, <is:0>
0020 leave
```
Fixes ruby/prism#2064
2023-12-14 20:20:24 +03:00
|
|
|
LABEL *start_label = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil);
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_CVAR),
|
|
|
|
ID2SYM(pm_constant_id_lookup(scope_node, class_variable_or_write_node->name)), Qtrue);
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, start_label);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID class_variable_name_id = pm_constant_id_lookup(scope_node, class_variable_or_write_node->name);
|
2023-09-02 00:20:03 +03:00
|
|
|
VALUE class_variable_name_val = ID2SYM(class_variable_name_id);
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getclassvariable,
|
|
|
|
class_variable_name_val,
|
|
|
|
get_cvar_ic_value(iseq, class_variable_name_id));
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, end_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
[Prism] Fix cvar or assignment instructions
The instructions for `PM_CLASS_VARIABLE_OR_WRITE_NODE` were incorrect as
they were missing a `putnil`, a `defined`, and a `branchunless`.
I verified this is fixed via the instructions and running the following:
`RUBY_ISEQ_DUMP_DEBUG=prism make test/csv/interface/test_read_write.rb`.
These new instructions can't go in the defined function because
`defined?(@@fop ||= 1)` should return "assignment" not "class variable".
Instructions before:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(30,11)>
0000 putnil ( 30)[Li]
0001 defined class variable, :@@foo, true
0005 branchunless 14
0007 getclassvariable :@@foo, <is:0>
0010 dup
0011 branchif 20
0013 pop
0014 putobject 1
0016 dup
0017 setclassvariable :@@foo, <is:0>
0020 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:29 (29,0)-(29,11)>
0000 getclassvariable :@@foo, <is:0> ( 29)[Li]
0003 dup
0004 branchif 13
0006 pop
0007 putobject 1
0009 dup
0010 setclassvariable :@@foo, <is:0>
0013 leave
```
Instructions after:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(30,11)>
0000 putnil ( 30)[Li]
0001 defined class variable, :@@foo, true
0005 branchunless 14
0007 getclassvariable :@@foo, <is:0>
0010 dup
0011 branchif 20
0013 pop
0014 putobject 1
0016 dup
0017 setclassvariable :@@foo, <is:0>
0020 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:29 (29,0)-(29,11)>
0000 putnil ( 29)[Li]
0001 defined class variable, :@@foo, true
0005 branchunless 14
0007 getclassvariable :@@foo, <is:0>
0010 dup
0011 branchif 20
0013 pop
0014 putobject 1
0016 dup
0017 setclassvariable :@@foo, <is:0>
0020 leave
```
Fixes ruby/prism#2064
2023-12-14 20:20:24 +03:00
|
|
|
ADD_LABEL(ret, start_label);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(class_variable_or_write_node->value);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, setclassvariable,
|
|
|
|
class_variable_name_val,
|
|
|
|
get_cvar_ic_value(iseq, class_variable_name_id));
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CLASS_VARIABLE_READ_NODE: {
|
2023-09-02 00:20:03 +03:00
|
|
|
if (!popped) {
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_class_variable_read_node_t *class_variable_read_node = (pm_class_variable_read_node_t *) node;
|
2023-10-17 01:36:25 +03:00
|
|
|
ID cvar_name = pm_constant_id_lookup(scope_node, class_variable_read_node->name);
|
2023-09-02 00:20:03 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, getclassvariable, ID2SYM(cvar_name), get_cvar_ic_value(iseq, cvar_name));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CLASS_VARIABLE_WRITE_NODE: {
|
|
|
|
pm_class_variable_write_node_t *write_node = (pm_class_variable_write_node_t *) node;
|
|
|
|
PM_COMPILE_NOT_POPPED(write_node->value);
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID cvar_name = pm_constant_id_lookup(scope_node, write_node->name);
|
2023-08-29 19:27:00 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, setclassvariable, ID2SYM(cvar_name), get_cvar_ic_value(iseq, cvar_name));
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_PATH_NODE: {
|
2024-02-08 18:59:01 +03:00
|
|
|
// Foo::Bar
|
|
|
|
// ^^^^^^^^
|
|
|
|
VALUE parts;
|
|
|
|
|
|
|
|
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) != Qnil)) {
|
|
|
|
ISEQ_BODY(iseq)->ic_size++;
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, opt_getconstant_path, parts);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
2024-02-08 18:59:01 +03:00
|
|
|
else {
|
2024-02-14 22:17:32 +03:00
|
|
|
int lineno = pm_node_line_number(parser, node);
|
2024-02-08 18:59:01 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
DECL_ANCHOR(prefix);
|
|
|
|
INIT_ANCHOR(prefix);
|
2023-09-11 21:36:09 +03:00
|
|
|
|
2024-02-08 18:59:01 +03:00
|
|
|
DECL_ANCHOR(body);
|
|
|
|
INIT_ANCHOR(body);
|
|
|
|
|
|
|
|
pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
|
|
|
|
if (LIST_INSN_SIZE_ZERO(prefix)) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_SEQ(ret, prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEQ(ret, body);
|
|
|
|
}
|
2023-09-11 21:36:09 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-26 13:02:55 +03:00
|
|
|
case PM_CONSTANT_PATH_AND_WRITE_NODE: {
|
2024-02-08 18:59:01 +03:00
|
|
|
// Foo::Bar &&= baz
|
|
|
|
// ^^^^^^^^^^^^^^^^
|
|
|
|
const pm_constant_path_and_write_node_t *cast = (const pm_constant_path_and_write_node_t*) node;
|
|
|
|
const pm_constant_path_node_t *target = cast->target;
|
|
|
|
|
|
|
|
const pm_constant_read_node_t *child = (const pm_constant_read_node_t *) target->child;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, child->name));
|
2023-10-26 13:02:55 +03:00
|
|
|
|
|
|
|
LABEL *lfin = NEW_LABEL(lineno);
|
|
|
|
|
2023-12-06 19:05:43 +03:00
|
|
|
if (target->parent) {
|
|
|
|
PM_COMPILE_NOT_POPPED(target->parent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
|
|
|
|
}
|
2023-10-26 13:02:55 +03:00
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-10-26 13:02:55 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
2024-02-08 18:59:01 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, name);
|
2023-10-26 13:02:55 +03:00
|
|
|
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfin);
|
|
|
|
|
|
|
|
PM_POP_UNLESS_POPPED;
|
2024-02-08 18:59:01 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
2023-10-26 13:02:55 +03:00
|
|
|
|
|
|
|
if (popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, dupn, INT2FIX(2));
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_SWAP;
|
2023-10-26 13:02:55 +03:00
|
|
|
}
|
|
|
|
|
2024-02-08 18:59:01 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, name);
|
2023-10-26 13:02:55 +03:00
|
|
|
ADD_LABEL(ret, lfin);
|
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_SWAP_UNLESS_POPPED;
|
2023-10-26 13:02:55 +03:00
|
|
|
PM_POP;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-10-26 12:57:05 +03:00
|
|
|
case PM_CONSTANT_PATH_OR_WRITE_NODE: {
|
2024-02-08 18:59:01 +03:00
|
|
|
// Foo::Bar ||= baz
|
|
|
|
// ^^^^^^^^^^^^^^^^
|
|
|
|
const pm_constant_path_or_write_node_t *cast = (const pm_constant_path_or_write_node_t *) node;
|
|
|
|
const pm_constant_path_node_t *target = cast->target;
|
|
|
|
|
|
|
|
const pm_constant_read_node_t *child = (const pm_constant_read_node_t *) target->child;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, child->name));
|
2023-10-26 12:57:05 +03:00
|
|
|
|
|
|
|
LABEL *lassign = NEW_LABEL(lineno);
|
|
|
|
LABEL *lfin = NEW_LABEL(lineno);
|
|
|
|
|
2023-12-06 19:05:43 +03:00
|
|
|
if (target->parent) {
|
|
|
|
PM_COMPILE_NOT_POPPED(target->parent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
|
|
|
|
}
|
2023-10-26 12:57:05 +03:00
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2024-02-08 18:59:01 +03:00
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_CONST_FROM), name, Qtrue);
|
2023-10-26 12:57:05 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lassign);
|
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-10-26 12:57:05 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
2024-02-08 18:59:01 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, name);
|
2023-10-26 12:57:05 +03:00
|
|
|
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, lfin);
|
|
|
|
|
|
|
|
PM_POP_UNLESS_POPPED;
|
|
|
|
ADD_LABEL(ret, lassign);
|
2024-02-08 18:59:01 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
2023-10-26 12:57:05 +03:00
|
|
|
|
|
|
|
if (popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, dupn, INT2FIX(2));
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_SWAP;
|
2023-10-26 12:57:05 +03:00
|
|
|
}
|
|
|
|
|
2024-02-08 18:59:01 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, name);
|
2023-10-26 12:57:05 +03:00
|
|
|
ADD_LABEL(ret, lfin);
|
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_SWAP_UNLESS_POPPED;
|
2023-10-26 12:57:05 +03:00
|
|
|
PM_POP;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-10-25 21:00:24 +03:00
|
|
|
case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
|
2024-02-08 18:59:01 +03:00
|
|
|
// Foo::Bar += baz
|
|
|
|
// ^^^^^^^^^^^^^^^
|
|
|
|
const pm_constant_path_operator_write_node_t *cast = (const pm_constant_path_operator_write_node_t *) node;
|
|
|
|
const pm_constant_path_node_t *target = cast->target;
|
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, cast->operator);
|
|
|
|
|
|
|
|
const pm_constant_read_node_t *child = (const pm_constant_read_node_t *) target->child;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, child->name));
|
2023-10-25 21:00:24 +03:00
|
|
|
|
2023-12-06 19:05:43 +03:00
|
|
|
if (target->parent) {
|
|
|
|
PM_COMPILE_NOT_POPPED(target->parent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
|
|
|
|
}
|
2023-10-25 21:00:24 +03:00
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-10-25 21:00:24 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
2024-02-08 18:59:01 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, name);
|
2023-10-25 21:00:24 +03:00
|
|
|
|
2024-02-08 18:59:01 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
2023-10-25 21:00:24 +03:00
|
|
|
ADD_CALL(ret, &dummy_line_node, method_id, INT2FIX(1));
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_SWAP;
|
2023-10-25 21:00:24 +03:00
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_SWAP;
|
2023-10-25 21:00:24 +03:00
|
|
|
}
|
|
|
|
|
2024-02-08 18:59:01 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, name);
|
2024-01-12 01:05:42 +03:00
|
|
|
return;
|
2023-10-25 21:00:24 +03:00
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_PATH_WRITE_NODE: {
|
2024-02-08 18:59:01 +03:00
|
|
|
// Foo::Bar = 1
|
|
|
|
// ^^^^^^^^^^^^
|
|
|
|
const pm_constant_path_write_node_t *cast = (const pm_constant_path_write_node_t *) node;
|
|
|
|
const pm_constant_path_node_t *target = cast->target;
|
|
|
|
|
|
|
|
const pm_constant_read_node_t *child = (const pm_constant_read_node_t *) target->child;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, child->name));
|
|
|
|
|
|
|
|
if (target->parent) {
|
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *) target->parent);
|
2023-10-16 16:31:32 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
|
|
|
|
}
|
2024-02-08 18:59:01 +03:00
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
|
|
|
|
2023-10-16 16:31:32 +03:00
|
|
|
if (!popped) {
|
2023-10-25 18:34:03 +03:00
|
|
|
PM_SWAP;
|
2023-10-16 16:31:32 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
}
|
2024-02-08 18:59:01 +03:00
|
|
|
|
2023-10-25 18:34:03 +03:00
|
|
|
PM_SWAP;
|
2024-02-08 18:59:01 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, name);
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_READ_NODE: {
|
2024-02-08 18:14:27 +03:00
|
|
|
// Foo
|
|
|
|
// ^^^
|
|
|
|
const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
|
|
|
|
|
|
|
|
pm_compile_constant_read(iseq, name, &cast->base.location, ret, scope_node);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_AND_WRITE_NODE: {
|
2024-02-08 18:14:27 +03:00
|
|
|
// Foo &&= bar
|
|
|
|
// ^^^^^^^^^^^
|
|
|
|
const pm_constant_and_write_node_t *cast = (const pm_constant_and_write_node_t *) node;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
|
2023-09-05 17:54:21 +03:00
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
2024-02-08 18:14:27 +03:00
|
|
|
pm_compile_constant_read(iseq, name, &cast->name_loc, ret, scope_node);
|
2023-09-05 17:54:21 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-05 17:54:21 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, end_label);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-09-05 17:54:21 +03:00
|
|
|
|
2024-02-08 18:14:27 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-05 17:54:21 +03:00
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
|
2024-02-08 18:14:27 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, name);
|
2023-09-05 17:54:21 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_OR_WRITE_NODE: {
|
2024-02-08 18:14:27 +03:00
|
|
|
// Foo ||= bar
|
|
|
|
// ^^^^^^^^^^^
|
|
|
|
const pm_constant_or_write_node_t *cast = (const pm_constant_or_write_node_t *) node;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
|
|
|
|
LABEL *set_label = NEW_LABEL(lineno);
|
2023-09-05 17:54:21 +03:00
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2024-02-08 18:14:27 +03:00
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_CONST), name, Qtrue);
|
2023-09-05 17:54:21 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, set_label);
|
|
|
|
|
2024-02-08 18:14:27 +03:00
|
|
|
pm_compile_constant_read(iseq, name, &cast->name_loc, ret, scope_node);
|
2023-09-05 17:54:21 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-05 17:54:21 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, end_label);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-09-05 17:54:21 +03:00
|
|
|
|
|
|
|
ADD_LABEL(ret, set_label);
|
2024-02-08 18:14:27 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
2023-09-05 17:54:21 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-05 17:54:21 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
|
2024-02-08 18:14:27 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, name);
|
2023-09-05 17:54:21 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2024-02-08 18:59:01 +03:00
|
|
|
case PM_CONSTANT_OPERATOR_WRITE_NODE: {
|
|
|
|
// Foo += bar
|
|
|
|
// ^^^^^^^^^^
|
|
|
|
const pm_constant_operator_write_node_t *cast = (const pm_constant_operator_write_node_t *) node;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
|
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, cast->operator);
|
|
|
|
|
|
|
|
pm_compile_constant_read(iseq, name, &cast->name_loc, ret, scope_node);
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, method_id, INT2NUM(1), INT2FIX(VM_CALL_ARGS_SIMPLE));
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, name);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_WRITE_NODE: {
|
2024-02-08 18:59:01 +03:00
|
|
|
// Foo = 1
|
|
|
|
// ^^^^^^^
|
|
|
|
const pm_constant_write_node_t *cast = (const pm_constant_write_node_t *) node;
|
|
|
|
VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-08 18:59:01 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
|
2024-02-08 18:59:01 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, name);
|
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_DEF_NODE: {
|
|
|
|
pm_def_node_t *def_node = (pm_def_node_t *) node;
|
2023-10-17 01:36:25 +03:00
|
|
|
ID method_name = pm_constant_id_lookup(scope_node, def_node->name);
|
2024-01-18 19:55:31 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init((pm_node_t *)def_node, &next_scope_node, scope_node, parser);
|
2024-01-18 19:38:53 +03:00
|
|
|
rb_iseq_t *method_iseq = NEW_ISEQ(&next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, lineno);
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-10-20 04:01:09 +03:00
|
|
|
if (def_node->receiver) {
|
2023-10-24 23:08:50 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(def_node->receiver);
|
2023-10-20 04:01:09 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, definesmethod, ID2SYM(method_name), method_iseq);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, definemethod, ID2SYM(method_name), method_iseq);
|
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)method_iseq);
|
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, ID2SYM(method_name));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_DEFINED_NODE: {
|
|
|
|
pm_defined_node_t *defined_node = (pm_defined_node_t *)node;
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_defined_expr(iseq, defined_node->value, ret, popped, scope_node, dummy_line_node, lineno, false);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_EMBEDDED_STATEMENTS_NODE: {
|
|
|
|
pm_embedded_statements_node_t *embedded_statements_node = (pm_embedded_statements_node_t *)node;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-09-07 17:59:51 +03:00
|
|
|
if (embedded_statements_node->statements) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE((pm_node_t *) (embedded_statements_node->statements));
|
2023-09-07 17:59:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-09-07 17:59:51 +03:00
|
|
|
}
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
// TODO: Concatenate the strings that exist here
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_EMBEDDED_VARIABLE_NODE: {
|
|
|
|
pm_embedded_variable_node_t *embedded_node = (pm_embedded_variable_node_t *)node;
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(embedded_node->variable);
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_FALSE_NODE:
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse);
|
|
|
|
}
|
|
|
|
return;
|
2023-12-07 13:24:33 +03:00
|
|
|
case PM_ENSURE_NODE: {
|
|
|
|
pm_ensure_node_t *ensure_node = (pm_ensure_node_t *)node;
|
|
|
|
|
|
|
|
LABEL *start = NEW_LABEL(lineno);
|
|
|
|
LABEL *end = NEW_LABEL(lineno);
|
|
|
|
ADD_LABEL(ret, start);
|
|
|
|
if (ensure_node->statements) {
|
2024-01-19 02:11:17 +03:00
|
|
|
LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
|
2023-12-07 13:24:33 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->end_label = end;
|
|
|
|
PM_COMPILE((pm_node_t *)ensure_node->statements);
|
2024-01-19 02:11:17 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
|
2023-12-07 13:24:33 +03:00
|
|
|
}
|
|
|
|
ADD_LABEL(ret, end);
|
2024-01-17 19:19:54 +03:00
|
|
|
return;
|
2023-12-07 13:24:33 +03:00
|
|
|
}
|
[Prism] Fix IfNode and ElseNode
ElseNode looks to have been implemented at the same time as IfNode, but
was resulting in a stack underflow error.
The following is from the test code
```
if foo
bar
end
```
```
❯ make run
compiling compile.c
linking miniruby
./miniruby -I./lib -I. -I.ext/common -r./arm64-darwin22-fake ./test.rb
CRUBY: **************************************************
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(2,19)>
0000 putself ( 2)[Li]
0001 opt_send_without_block <calldata!mid:foo, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0003 branchunless 9
0005 putself
0006 opt_send_without_block <calldata!mid:bar, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0008 pop
0009 putobject_INT2FIX_1_
0010 leave
PRISM: **************************************************
-- raw disasm--------
0000 putself ( 2)
0001 send <calldata:foo, 0>, nil ( 2)
0004 branchunless <L001> ( 2)
0006 jump <L000> ( 2)
<L000> [sp: 0]
* 0008 pop ( 1)
0009 putself ( 2)
0010 send <calldata:bar, 0>, nil ( 2)
0013 pop ( 2)
0014 jump <L002> ( 1)
<L001> [sp: 0]
<L002> [sp: -1]
0016 putobject 1 ( 2)
0018 leave ( 1)
---------------------
<compiled>: <compiled>:1: argument stack underflow (-1) (SyntaxError)
make: *** [run] Error 1
```
This commit fixes the stack underflow error for both IfNode and ElseNode
and introduces tests for them.
2023-10-02 19:51:14 +03:00
|
|
|
case PM_ELSE_NODE: {
|
|
|
|
pm_else_node_t *cast = (pm_else_node_t *)node;
|
2023-10-20 18:03:02 +03:00
|
|
|
if (cast->statements) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE((pm_node_t *)cast->statements);
|
2023-10-20 18:03:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-12-09 00:17:07 +03:00
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
2023-10-20 18:03:02 +03:00
|
|
|
}
|
[Prism] Fix IfNode and ElseNode
ElseNode looks to have been implemented at the same time as IfNode, but
was resulting in a stack underflow error.
The following is from the test code
```
if foo
bar
end
```
```
❯ make run
compiling compile.c
linking miniruby
./miniruby -I./lib -I. -I.ext/common -r./arm64-darwin22-fake ./test.rb
CRUBY: **************************************************
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(2,19)>
0000 putself ( 2)[Li]
0001 opt_send_without_block <calldata!mid:foo, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0003 branchunless 9
0005 putself
0006 opt_send_without_block <calldata!mid:bar, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0008 pop
0009 putobject_INT2FIX_1_
0010 leave
PRISM: **************************************************
-- raw disasm--------
0000 putself ( 2)
0001 send <calldata:foo, 0>, nil ( 2)
0004 branchunless <L001> ( 2)
0006 jump <L000> ( 2)
<L000> [sp: 0]
* 0008 pop ( 1)
0009 putself ( 2)
0010 send <calldata:bar, 0>, nil ( 2)
0013 pop ( 2)
0014 jump <L002> ( 1)
<L001> [sp: 0]
<L002> [sp: -1]
0016 putobject 1 ( 2)
0018 leave ( 1)
---------------------
<compiled>: <compiled>:1: argument stack underflow (-1) (SyntaxError)
make: *** [run] Error 1
```
This commit fixes the stack underflow error for both IfNode and ElseNode
and introduces tests for them.
2023-10-02 19:51:14 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_FLIP_FLOP_NODE: {
|
|
|
|
pm_flip_flop_node_t *flip_flop_node = (pm_flip_flop_node_t *)node;
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-10-16 16:31:32 +03:00
|
|
|
LABEL *final_label = NEW_LABEL(lineno);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
LABEL *then_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *else_label = NEW_LABEL(lineno);
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_flip_flop(flip_flop_node, else_label, then_label, iseq, lineno, ret, popped, scope_node);
|
2023-10-16 16:31:32 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
ADD_LABEL(ret, then_label);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
2023-10-16 16:31:32 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, final_label);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
ADD_LABEL(ret, else_label);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse);
|
2023-10-16 16:31:32 +03:00
|
|
|
ADD_LABEL(ret, final_label);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_FLOAT_NODE: {
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
2024-02-23 04:22:47 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, parse_float((const pm_float_node_t *) node));
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-10-20 12:59:02 +03:00
|
|
|
case PM_FOR_NODE: {
|
2024-01-12 23:23:47 +03:00
|
|
|
pm_for_node_t *cast = (pm_for_node_t *) node;
|
2023-10-20 12:59:02 +03:00
|
|
|
|
|
|
|
LABEL *retry_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *retry_end_l = NEW_LABEL(lineno);
|
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
// First, compile the collection that we're going to be iterating over.
|
2023-10-20 12:59:02 +03:00
|
|
|
ADD_LABEL(ret, retry_label);
|
2024-01-12 23:23:47 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->collection);
|
2023-10-20 12:59:02 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
// Next, create the new scope that is going to contain the block that
|
|
|
|
// will be passed to the each method.
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init((pm_node_t *) cast, &next_scope_node, scope_node, parser);
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
const rb_iseq_t *child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
2024-01-18 19:38:53 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
const rb_iseq_t *prev_block = ISEQ_COMPILE_DATA(iseq)->current_block;
|
2023-10-20 12:59:02 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
// Now, create the method call to each that will be used to iterate over
|
|
|
|
// the collection, and pass the newly created iseq as the block.
|
2023-10-20 12:59:02 +03:00
|
|
|
ADD_SEND_WITH_BLOCK(ret, &dummy_line_node, idEach, INT2FIX(0), child_iseq);
|
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
// We need to put the label "retry_end_l" immediately after the last
|
|
|
|
// "send" instruction. This because vm_throw checks if the break cont is
|
|
|
|
// equal to the index of next insn of the "send". (Otherwise, it is
|
|
|
|
// considered "break from proc-closure". See "TAG_BREAK" handling in
|
|
|
|
// "vm_throw_start".)
|
|
|
|
//
|
|
|
|
// Normally, "send" instruction is at the last. However, qcall under
|
|
|
|
// branch coverage measurement adds some instructions after the "send".
|
|
|
|
//
|
|
|
|
// Note that "invokesuper" appears instead of "send".
|
|
|
|
{
|
|
|
|
INSN *iobj;
|
|
|
|
LINK_ELEMENT *last_elem = LAST_ELEMENT(ret);
|
|
|
|
iobj = IS_INSN(last_elem) ? (INSN*) last_elem : (INSN*) get_prev_insn((INSN*) last_elem);
|
|
|
|
while (INSN_OF(iobj) != BIN(send) && INSN_OF(iobj) != BIN(invokesuper)) {
|
|
|
|
iobj = (INSN*) get_prev_insn(iobj);
|
|
|
|
}
|
|
|
|
ELEM_INSERT_NEXT(&iobj->link, (LINK_ELEMENT*) retry_end_l);
|
2023-10-20 12:59:02 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
// LINK_ANCHOR has a pointer to the last element, but
|
|
|
|
// ELEM_INSERT_NEXT does not update it even if we add an insn to the
|
|
|
|
// last of LINK_ANCHOR. So this updates it manually.
|
|
|
|
if (&iobj->link == LAST_ELEMENT(ret)) {
|
|
|
|
ret->last = (LINK_ELEMENT*) retry_end_l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = prev_block;
|
2023-10-20 12:59:02 +03:00
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
|
2023-10-30 19:56:30 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-11-08 18:00:32 +03:00
|
|
|
case PM_FORWARDING_ARGUMENTS_NODE: {
|
2024-01-02 19:34:04 +03:00
|
|
|
rb_bug("Cannot compile a ForwardingArgumentsNode directly\n");
|
2023-11-08 18:00:32 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-30 19:56:30 +03:00
|
|
|
case PM_FORWARDING_SUPER_NODE: {
|
|
|
|
pm_forwarding_super_node_t *forwarding_super_node = (pm_forwarding_super_node_t *) node;
|
|
|
|
const rb_iseq_t *block = NULL;
|
|
|
|
PM_PUTSELF;
|
|
|
|
int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
|
2023-10-20 12:59:02 +03:00
|
|
|
|
2023-10-30 19:56:30 +03:00
|
|
|
if (forwarding_super_node->block) {
|
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init((pm_node_t *)forwarding_super_node->block, &next_scope_node, scope_node, parser);
|
2024-01-18 19:38:53 +03:00
|
|
|
block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
2024-01-18 19:38:53 +03:00
|
|
|
|
2023-10-30 19:56:30 +03:00
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)block);
|
|
|
|
}
|
|
|
|
|
2023-12-12 19:08:40 +03:00
|
|
|
DECL_ANCHOR(args);
|
|
|
|
INIT_ANCHOR(args);
|
|
|
|
|
|
|
|
struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
|
|
|
|
|
|
|
|
const rb_iseq_t *local_iseq = body->local_iseq;
|
|
|
|
const struct rb_iseq_constant_body *const local_body = ISEQ_BODY(local_iseq);
|
|
|
|
|
|
|
|
int argc = 0;
|
|
|
|
int depth = get_lvar_level(iseq);
|
|
|
|
|
|
|
|
if (local_body->param.flags.has_lead) {
|
|
|
|
/* required arguments */
|
|
|
|
for (int i = 0; i < local_body->param.lead_num; i++) {
|
|
|
|
int idx = local_body->local_table_size - i;
|
|
|
|
ADD_GETLOCAL(args, &dummy_line_node, idx, depth);
|
|
|
|
}
|
|
|
|
argc += local_body->param.lead_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (local_body->param.flags.has_opt) {
|
|
|
|
/* optional arguments */
|
|
|
|
for (int j = 0; j < local_body->param.opt_num; j++) {
|
|
|
|
int idx = local_body->local_table_size - (argc + j);
|
|
|
|
ADD_GETLOCAL(args, &dummy_line_node, idx, depth);
|
|
|
|
}
|
|
|
|
argc += local_body->param.opt_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (local_body->param.flags.has_rest) {
|
|
|
|
/* rest argument */
|
|
|
|
int idx = local_body->local_table_size - local_body->param.rest_start;
|
|
|
|
ADD_GETLOCAL(args, &dummy_line_node, idx, depth);
|
|
|
|
ADD_INSN1(args, &dummy_line_node, splatarray, Qfalse);
|
|
|
|
|
|
|
|
argc = local_body->param.rest_start + 1;
|
|
|
|
flag |= VM_CALL_ARGS_SPLAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (local_body->param.flags.has_post) {
|
|
|
|
/* post arguments */
|
|
|
|
int post_len = local_body->param.post_num;
|
|
|
|
int post_start = local_body->param.post_start;
|
|
|
|
|
|
|
|
int j = 0;
|
|
|
|
for (; j < post_len; j++) {
|
|
|
|
int idx = local_body->local_table_size - (post_start + j);
|
|
|
|
ADD_GETLOCAL(args, &dummy_line_node, idx, depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (local_body->param.flags.has_rest) {
|
|
|
|
// argc remains unchanged from rest branch
|
|
|
|
ADD_INSN1(args, &dummy_line_node, newarray, INT2FIX(j));
|
|
|
|
ADD_INSN (args, &dummy_line_node, concatarray);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
argc = post_len + post_start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct rb_iseq_param_keyword *const local_keyword = local_body->param.keyword;
|
|
|
|
if (local_body->param.flags.has_kw) {
|
|
|
|
int local_size = local_body->local_table_size;
|
|
|
|
argc++;
|
|
|
|
|
|
|
|
ADD_INSN1(args, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
|
|
|
|
if (local_body->param.flags.has_kwrest) {
|
|
|
|
int idx = local_body->local_table_size - local_keyword->rest_start;
|
|
|
|
ADD_GETLOCAL(args, &dummy_line_node, idx, depth);
|
2024-01-31 17:42:22 +03:00
|
|
|
RUBY_ASSERT(local_keyword->num > 0);
|
|
|
|
ADD_SEND(args, &dummy_line_node, rb_intern("dup"), INT2FIX(0));
|
2023-12-12 19:08:40 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(args, &dummy_line_node, newhash, INT2FIX(0));
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
for (; i < local_keyword->num; ++i) {
|
|
|
|
ID id = local_keyword->table[i];
|
|
|
|
int idx = local_size - get_local_var_idx(local_iseq, id);
|
|
|
|
ADD_INSN1(args, &dummy_line_node, putobject, ID2SYM(id));
|
|
|
|
ADD_GETLOCAL(args, &dummy_line_node, idx, depth);
|
|
|
|
}
|
|
|
|
ADD_SEND(args, &dummy_line_node, id_core_hash_merge_ptr, INT2FIX(i * 2 + 1));
|
2024-01-31 17:42:22 +03:00
|
|
|
flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
|
2023-12-12 19:08:40 +03:00
|
|
|
}
|
|
|
|
else if (local_body->param.flags.has_kwrest) {
|
|
|
|
int idx = local_body->local_table_size - local_keyword->rest_start;
|
|
|
|
ADD_GETLOCAL(args, &dummy_line_node, idx, depth);
|
|
|
|
argc++;
|
2024-01-31 17:42:22 +03:00
|
|
|
flag |= VM_CALL_KW_SPLAT;
|
2023-12-12 19:08:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEQ(ret, args);
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, invokesuper, new_callinfo(iseq, 0, argc, flag, NULL, block != NULL), block);
|
2023-10-30 19:56:30 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-10-20 12:59:02 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
|
|
|
|
pm_global_variable_and_write_node_t *global_variable_and_write_node = (pm_global_variable_and_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
VALUE global_variable_name = ID2SYM(pm_constant_id_lookup(scope_node, global_variable_and_write_node->name));
|
2023-09-06 17:28:29 +03:00
|
|
|
|
2023-09-02 00:20:03 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getglobal, global_variable_name);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, end_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(global_variable_and_write_node->value);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setglobal, global_variable_name);
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
|
|
|
|
pm_global_variable_operator_write_node_t *global_variable_operator_write_node = (pm_global_variable_operator_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
VALUE global_variable_name = ID2SYM(pm_constant_id_lookup(scope_node, global_variable_operator_write_node->name));
|
2023-09-06 17:28:29 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getglobal, global_variable_name);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(global_variable_operator_write_node->value);
|
2023-10-17 01:36:25 +03:00
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, global_variable_operator_write_node->operator);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
int flags = VM_CALL_ARGS_SIMPLE;
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, method_id, INT2NUM(1), INT2FIX(flags));
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-06 17:28:29 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setglobal, global_variable_name);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
|
|
|
|
pm_global_variable_or_write_node_t *global_variable_or_write_node = (pm_global_variable_or_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
LABEL *set_label= NEW_LABEL(lineno);
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-17 01:36:25 +03:00
|
|
|
VALUE global_variable_name = ID2SYM(pm_constant_id_lookup(scope_node, global_variable_or_write_node->name));
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_GVAR), global_variable_name, Qtrue);
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, set_label);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, getglobal, global_variable_name);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, end_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_LABEL(ret, set_label);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(global_variable_or_write_node->value);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setglobal, global_variable_name);
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_GLOBAL_VARIABLE_READ_NODE: {
|
|
|
|
pm_global_variable_read_node_t *global_variable_read_node = (pm_global_variable_read_node_t *)node;
|
2023-10-17 01:36:25 +03:00
|
|
|
VALUE global_variable_name = ID2SYM(pm_constant_id_lookup(scope_node, global_variable_read_node->name));
|
2023-09-02 00:20:03 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getglobal, global_variable_name);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_GLOBAL_VARIABLE_WRITE_NODE: {
|
|
|
|
pm_global_variable_write_node_t *write_node = (pm_global_variable_write_node_t *) node;
|
|
|
|
PM_COMPILE_NOT_POPPED(write_node->value);
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-10-17 01:36:25 +03:00
|
|
|
ID ivar_name = pm_constant_id_lookup(scope_node, write_node->name);
|
2023-08-29 19:27:00 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setglobal, ID2SYM(ivar_name));
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_HASH_NODE: {
|
2023-09-28 17:48:24 +03:00
|
|
|
// If every node in the hash is static, then we can compile the entire
|
|
|
|
// hash now instead of later.
|
2023-09-28 22:19:56 +03:00
|
|
|
if (pm_static_literal_p(node)) {
|
2023-09-28 17:48:24 +03:00
|
|
|
// We're only going to compile this node if it's not popped. If it
|
|
|
|
// is popped, then we know we don't need to do anything since it's
|
|
|
|
// statically known.
|
|
|
|
if (!popped) {
|
2024-02-12 23:48:23 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node);
|
2023-09-28 22:19:56 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, duphash, value);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, value);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 17:48:24 +03:00
|
|
|
} else {
|
|
|
|
// Here since we know there are possible side-effects inside the
|
|
|
|
// hash contents, we're going to build it entirely at runtime. We'll
|
|
|
|
// do this by pushing all of the key-value pairs onto the stack and
|
|
|
|
// then combining them with newhash.
|
|
|
|
//
|
|
|
|
// If this hash is popped, then this serves only to ensure we enact
|
|
|
|
// all side-effects (like method calls) that are contained within
|
|
|
|
// the hash contents.
|
2024-02-13 06:50:10 +03:00
|
|
|
const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
|
|
|
|
const pm_node_list_t *elements = &cast->elements;
|
2023-09-28 22:19:56 +03:00
|
|
|
|
2024-02-13 06:50:10 +03:00
|
|
|
if (popped) {
|
|
|
|
// If this hash is popped, then we can iterate through each
|
|
|
|
// element and compile it. The result of each compilation will
|
|
|
|
// only include the side effects of the element itself.
|
|
|
|
for (size_t index = 0; index < elements->size; index++) {
|
|
|
|
PM_COMPILE_POPPED(elements->nodes[index]);
|
|
|
|
}
|
2023-10-23 16:33:38 +03:00
|
|
|
}
|
2024-02-13 06:50:10 +03:00
|
|
|
else {
|
2024-02-13 07:13:35 +03:00
|
|
|
pm_compile_hash_elements(elements, lineno, iseq, ret, scope_node);
|
2023-09-28 17:48:24 +03:00
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_IF_NODE: {
|
2024-02-20 19:37:54 +03:00
|
|
|
// if foo then bar end
|
|
|
|
// ^^^^^^^^^^^^^^^^^^^
|
|
|
|
//
|
|
|
|
// bar if foo
|
|
|
|
// ^^^^^^^^^^
|
|
|
|
//
|
|
|
|
// foo ? bar : baz
|
|
|
|
// ^^^^^^^^^^^^^^^
|
|
|
|
const pm_if_node_t *cast = (const pm_if_node_t *) node;
|
|
|
|
pm_compile_conditional(iseq, &location, cast->statements, cast->consequent, cast->predicate, ret, popped, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_IMAGINARY_NODE: {
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
2023-09-27 19:39:53 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, parse_imaginary((pm_imaginary_node_t *)node));
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-28 17:51:57 +03:00
|
|
|
case PM_IMPLICIT_NODE: {
|
|
|
|
// Implicit nodes mark places in the syntax tree where explicit syntax
|
|
|
|
// was omitted, but implied. For example,
|
|
|
|
//
|
|
|
|
// { foo: }
|
|
|
|
//
|
|
|
|
// In this case a method call/local variable read is implied by virtue
|
|
|
|
// of the missing value. To compile these nodes, we simply compile the
|
|
|
|
// value that is implied, which is helpfully supplied by the parser.
|
|
|
|
pm_implicit_node_t *cast = (pm_implicit_node_t *)node;
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(cast->value);
|
2023-09-28 17:51:57 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-12-13 19:34:05 +03:00
|
|
|
case PM_IN_NODE: {
|
|
|
|
// In nodes are handled by the case match node directly, so we should
|
|
|
|
// never end up hitting them through this path.
|
|
|
|
rb_bug("Should not ever enter an in node directly");
|
|
|
|
return;
|
|
|
|
}
|
2024-02-01 05:25:21 +03:00
|
|
|
case PM_INDEX_OPERATOR_WRITE_NODE:
|
|
|
|
pm_compile_index_operator_write_node(scope_node, (const pm_index_operator_write_node_t *) node, iseq, ret, popped);
|
|
|
|
return;
|
2023-11-27 19:12:24 +03:00
|
|
|
case PM_INDEX_AND_WRITE_NODE: {
|
2024-02-01 05:25:21 +03:00
|
|
|
const pm_index_and_write_node_t *cast = (const pm_index_and_write_node_t *) node;
|
|
|
|
pm_compile_index_control_flow_write_node(scope_node, node, cast->receiver, cast->arguments, cast->block, cast->value, iseq, ret, popped);
|
2023-11-27 19:12:24 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-11-27 16:04:24 +03:00
|
|
|
case PM_INDEX_OR_WRITE_NODE: {
|
2024-02-01 05:25:21 +03:00
|
|
|
const pm_index_or_write_node_t *cast = (const pm_index_or_write_node_t *) node;
|
|
|
|
pm_compile_index_control_flow_write_node(scope_node, node, cast->receiver, cast->arguments, cast->block, cast->value, iseq, ret, popped);
|
2023-11-27 20:48:51 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
|
|
|
|
pm_instance_variable_and_write_node_t *instance_variable_and_write_node = (pm_instance_variable_and_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
2023-10-17 01:36:25 +03:00
|
|
|
ID instance_variable_name_id = pm_constant_id_lookup(scope_node, instance_variable_and_write_node->name);
|
2023-09-02 00:20:03 +03:00
|
|
|
VALUE instance_variable_name_val = ID2SYM(instance_variable_name_id);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, getinstancevariable, instance_variable_name_val, get_ivar_ic_value(iseq, instance_variable_name_id));
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-06 17:28:29 +03:00
|
|
|
|
2023-09-02 00:20:03 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, end_label);
|
|
|
|
PM_POP_UNLESS_POPPED;
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(instance_variable_and_write_node->value);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, setinstancevariable, instance_variable_name_val, get_ivar_ic_value(iseq, instance_variable_name_id));
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
|
|
|
|
pm_instance_variable_operator_write_node_t *instance_variable_operator_write_node = (pm_instance_variable_operator_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID instance_variable_name_id = pm_constant_id_lookup(scope_node, instance_variable_operator_write_node->name);
|
2023-09-02 00:20:03 +03:00
|
|
|
VALUE instance_variable_name_val = ID2SYM(instance_variable_name_id);
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getinstancevariable,
|
|
|
|
instance_variable_name_val,
|
|
|
|
get_ivar_ic_value(iseq, instance_variable_name_id));
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(instance_variable_operator_write_node->value);
|
2023-10-17 01:36:25 +03:00
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, instance_variable_operator_write_node->operator);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
int flags = VM_CALL_ARGS_SIMPLE;
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, method_id, INT2NUM(1), INT2FIX(flags));
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, setinstancevariable,
|
|
|
|
instance_variable_name_val,
|
|
|
|
get_ivar_ic_value(iseq, instance_variable_name_id));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
|
|
|
|
pm_instance_variable_or_write_node_t *instance_variable_or_write_node = (pm_instance_variable_or_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID instance_variable_name_id = pm_constant_id_lookup(scope_node, instance_variable_or_write_node->name);
|
2023-09-02 00:20:03 +03:00
|
|
|
VALUE instance_variable_name_val = ID2SYM(instance_variable_name_id);
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getinstancevariable, instance_variable_name_val, get_ivar_ic_value(iseq, instance_variable_name_id));
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, end_label);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(instance_variable_or_write_node->value);
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, setinstancevariable, instance_variable_name_val, get_ivar_ic_value(iseq, instance_variable_name_id));
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INSTANCE_VARIABLE_READ_NODE: {
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_instance_variable_read_node_t *instance_variable_read_node = (pm_instance_variable_read_node_t *) node;
|
2023-10-17 01:36:25 +03:00
|
|
|
ID ivar_name = pm_constant_id_lookup(scope_node, instance_variable_read_node->name);
|
2023-08-29 19:27:00 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, getinstancevariable, ID2SYM(ivar_name), get_ivar_ic_value(iseq, ivar_name));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INSTANCE_VARIABLE_WRITE_NODE: {
|
|
|
|
pm_instance_variable_write_node_t *write_node = (pm_instance_variable_write_node_t *) node;
|
|
|
|
PM_COMPILE_NOT_POPPED(write_node->value);
|
2023-08-31 00:26:22 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-08-31 00:26:22 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID ivar_name = pm_constant_id_lookup(scope_node, write_node->name);
|
2023-08-29 19:27:00 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, setinstancevariable,
|
|
|
|
ID2SYM(ivar_name),
|
|
|
|
get_ivar_ic_value(iseq, ivar_name));
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTEGER_NODE: {
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
2024-02-23 04:22:47 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, parse_integer((const pm_integer_node_t *) node));
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-28 17:18:43 +03:00
|
|
|
case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
|
|
|
|
pm_interpolated_match_last_line_node_t *cast = (pm_interpolated_match_last_line_node_t *) node;
|
2023-11-14 03:03:24 +03:00
|
|
|
|
[Prism] Fix InterpolatedMatchLastLine Instructions
I looked at this at RubyConf with Kevin, and we noticed that there was a
`putobject` empty string missing from the instructions. I just got back
around to implementing this and pushing a PR and while doing that
noticed that we also have a `getspecial` when we want a `getglobal`.
This change adds the `putobject` instruction and replaces the
`getspecial` with a `getglobal`. If we look at the parsetree for the
following code:
```ruby
$pit = '.oo'; if /"#{$pit}"/mix; end
```
We can see it has a `NODE_GVAR` and the [Ruby
compiler](https://github.com/ruby/ruby/blob/a4b43e92645e46ee5a8c9af42d3de57cd052e87c/compile.c#L10024-L10030) shows that should
use `getglobal.
```
@ NODE_SCOPE (id: 14, line: 1, location: (1,0)-(22,36))
+- nd_tbl: (empty)
+- nd_args:
| (null node)
+- nd_body:
@ NODE_BLOCK (id: 12, line: 22, location: (22,0)-(22,36))
+- nd_head (1):
| @ NODE_GASGN (id: 0, line: 22, location: (22,0)-(22,12))*
| +- nd_vid: :$pit
| +- nd_value:
| @ NODE_STR (id: 1, line: 22, location: (22,7)-(22,12))
| +- nd_lit: ".oo"
+- nd_head (2):
@ NODE_IF (id: 11, line: 22, location: (22,14)-(22,36))*
+- nd_cond:
| @ NODE_MATCH2 (id: 10, line: 22, location: (22,14)-(22,36))
| +- nd_recv:
| | @ NODE_DREGX (id: 2, line: 22, location: (22,17)-(22,31))
| | +- nd_lit: "\""
| | +- nd_next->nd_head:
| | | @ NODE_EVSTR (id: 4, line: 22, location: (22,19)-(22,26))
| | | +- nd_body:
| | | @ NODE_GVAR (id: 3, line: 22, location: (22,21)-(22,25))
| | | +- nd_vid: :$pit
| | +- nd_next->nd_next:
| | @ NODE_LIST (id: 7, line: 22, location: (22,26)-(22,27))
| | +- as.nd_alen: 1
| | +- nd_head:
| | | @ NODE_STR (id: 6, line: 22, location: (22,26)-(22,27))
| | | +- nd_lit: "\""
| | +- nd_next:
| | (null node)
| +- nd_value:
| @ NODE_GVAR (id: 9, line: 22, location: (22,14)-(22,36))
| +- nd_vid: :$_
+- nd_body:
| @ NODE_BEGIN (id: 8, line: 22, location: (22,32)-(22,32))
| +- nd_body:
| (null node)
+- nd_else:
(null node)
```
I'm struggling with writing a failing test, but the before/after
instructions show that `getglobal` is correct here. I compared the
instructions for the other `InterpolatedMatchLastLine` node tests
and they also used `getglobal`. I've edited the existing
`InterpolatedLastMatchLineNode` test so that it will use that instruction when
copied out of the test. Without the quotes it thinks it's just a
`MatchLastLineNode`.
Incorrect instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(22,36)>
0000 putstring ".oo" ( 22)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getglobal :$_
0019 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0022 branchunless 30
0024 jump 26
0026 putnil
0027 jump 31
0029 pop
0030 putnil
0031 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:21 (21,0)-(21,36)>
0000 putstring ".oo" ( 21)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getspecial 0, 0
0020 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0023 branchunless 31
0025 jump 27
0027 putnil
0028 jump 32
0030 pop
0031 putnil
0032 leave
```
Fixed instructions:
```
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(22,36)>
0000 putstring ".oo" ( 22)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getglobal :$_
0019 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0022 branchunless 30
0024 jump 26
0026 putnil
0027 jump 31
0029 pop
0030 putnil
0031 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:21 (21,0)-(21,36)>
0000 putstring ".oo" ( 21)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getglobal :$_
0019 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0022 branchunless 30
0024 jump 26
0026 putnil
0027 jump 31
0029 pop
0030 putnil
0031 leave
```
2023-12-12 23:52:53 +03:00
|
|
|
int parts_size = (int)cast->parts.size;
|
|
|
|
|
2024-02-12 23:48:23 +03:00
|
|
|
pm_interpolated_node_compile(&cast->parts, iseq, dummy_line_node, ret, popped, scope_node);
|
2023-09-28 17:18:43 +03:00
|
|
|
|
[Prism] Fix InterpolatedMatchLastLine Instructions
I looked at this at RubyConf with Kevin, and we noticed that there was a
`putobject` empty string missing from the instructions. I just got back
around to implementing this and pushing a PR and while doing that
noticed that we also have a `getspecial` when we want a `getglobal`.
This change adds the `putobject` instruction and replaces the
`getspecial` with a `getglobal`. If we look at the parsetree for the
following code:
```ruby
$pit = '.oo'; if /"#{$pit}"/mix; end
```
We can see it has a `NODE_GVAR` and the [Ruby
compiler](https://github.com/ruby/ruby/blob/a4b43e92645e46ee5a8c9af42d3de57cd052e87c/compile.c#L10024-L10030) shows that should
use `getglobal.
```
@ NODE_SCOPE (id: 14, line: 1, location: (1,0)-(22,36))
+- nd_tbl: (empty)
+- nd_args:
| (null node)
+- nd_body:
@ NODE_BLOCK (id: 12, line: 22, location: (22,0)-(22,36))
+- nd_head (1):
| @ NODE_GASGN (id: 0, line: 22, location: (22,0)-(22,12))*
| +- nd_vid: :$pit
| +- nd_value:
| @ NODE_STR (id: 1, line: 22, location: (22,7)-(22,12))
| +- nd_lit: ".oo"
+- nd_head (2):
@ NODE_IF (id: 11, line: 22, location: (22,14)-(22,36))*
+- nd_cond:
| @ NODE_MATCH2 (id: 10, line: 22, location: (22,14)-(22,36))
| +- nd_recv:
| | @ NODE_DREGX (id: 2, line: 22, location: (22,17)-(22,31))
| | +- nd_lit: "\""
| | +- nd_next->nd_head:
| | | @ NODE_EVSTR (id: 4, line: 22, location: (22,19)-(22,26))
| | | +- nd_body:
| | | @ NODE_GVAR (id: 3, line: 22, location: (22,21)-(22,25))
| | | +- nd_vid: :$pit
| | +- nd_next->nd_next:
| | @ NODE_LIST (id: 7, line: 22, location: (22,26)-(22,27))
| | +- as.nd_alen: 1
| | +- nd_head:
| | | @ NODE_STR (id: 6, line: 22, location: (22,26)-(22,27))
| | | +- nd_lit: "\""
| | +- nd_next:
| | (null node)
| +- nd_value:
| @ NODE_GVAR (id: 9, line: 22, location: (22,14)-(22,36))
| +- nd_vid: :$_
+- nd_body:
| @ NODE_BEGIN (id: 8, line: 22, location: (22,32)-(22,32))
| +- nd_body:
| (null node)
+- nd_else:
(null node)
```
I'm struggling with writing a failing test, but the before/after
instructions show that `getglobal` is correct here. I compared the
instructions for the other `InterpolatedMatchLastLine` node tests
and they also used `getglobal`. I've edited the existing
`InterpolatedLastMatchLineNode` test so that it will use that instruction when
copied out of the test. Without the quotes it thinks it's just a
`MatchLastLineNode`.
Incorrect instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(22,36)>
0000 putstring ".oo" ( 22)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getglobal :$_
0019 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0022 branchunless 30
0024 jump 26
0026 putnil
0027 jump 31
0029 pop
0030 putnil
0031 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:21 (21,0)-(21,36)>
0000 putstring ".oo" ( 21)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getspecial 0, 0
0020 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0023 branchunless 31
0025 jump 27
0027 putnil
0028 jump 32
0030 pop
0031 putnil
0032 leave
```
Fixed instructions:
```
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(22,36)>
0000 putstring ".oo" ( 22)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getglobal :$_
0019 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0022 branchunless 30
0024 jump 26
0026 putnil
0027 jump 31
0029 pop
0030 putnil
0031 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:21 (21,0)-(21,36)>
0000 putstring ".oo" ( 21)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getglobal :$_
0019 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0022 branchunless 30
0024 jump 26
0026 putnil
0027 jump 31
0029 pop
0030 putnil
0031 leave
```
2023-12-12 23:52:53 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, toregexp, INT2FIX(pm_reg_flags(node)), INT2FIX(parts_size));
|
2023-09-28 17:18:43 +03:00
|
|
|
|
[Prism] Fix InterpolatedMatchLastLine Instructions
I looked at this at RubyConf with Kevin, and we noticed that there was a
`putobject` empty string missing from the instructions. I just got back
around to implementing this and pushing a PR and while doing that
noticed that we also have a `getspecial` when we want a `getglobal`.
This change adds the `putobject` instruction and replaces the
`getspecial` with a `getglobal`. If we look at the parsetree for the
following code:
```ruby
$pit = '.oo'; if /"#{$pit}"/mix; end
```
We can see it has a `NODE_GVAR` and the [Ruby
compiler](https://github.com/ruby/ruby/blob/a4b43e92645e46ee5a8c9af42d3de57cd052e87c/compile.c#L10024-L10030) shows that should
use `getglobal.
```
@ NODE_SCOPE (id: 14, line: 1, location: (1,0)-(22,36))
+- nd_tbl: (empty)
+- nd_args:
| (null node)
+- nd_body:
@ NODE_BLOCK (id: 12, line: 22, location: (22,0)-(22,36))
+- nd_head (1):
| @ NODE_GASGN (id: 0, line: 22, location: (22,0)-(22,12))*
| +- nd_vid: :$pit
| +- nd_value:
| @ NODE_STR (id: 1, line: 22, location: (22,7)-(22,12))
| +- nd_lit: ".oo"
+- nd_head (2):
@ NODE_IF (id: 11, line: 22, location: (22,14)-(22,36))*
+- nd_cond:
| @ NODE_MATCH2 (id: 10, line: 22, location: (22,14)-(22,36))
| +- nd_recv:
| | @ NODE_DREGX (id: 2, line: 22, location: (22,17)-(22,31))
| | +- nd_lit: "\""
| | +- nd_next->nd_head:
| | | @ NODE_EVSTR (id: 4, line: 22, location: (22,19)-(22,26))
| | | +- nd_body:
| | | @ NODE_GVAR (id: 3, line: 22, location: (22,21)-(22,25))
| | | +- nd_vid: :$pit
| | +- nd_next->nd_next:
| | @ NODE_LIST (id: 7, line: 22, location: (22,26)-(22,27))
| | +- as.nd_alen: 1
| | +- nd_head:
| | | @ NODE_STR (id: 6, line: 22, location: (22,26)-(22,27))
| | | +- nd_lit: "\""
| | +- nd_next:
| | (null node)
| +- nd_value:
| @ NODE_GVAR (id: 9, line: 22, location: (22,14)-(22,36))
| +- nd_vid: :$_
+- nd_body:
| @ NODE_BEGIN (id: 8, line: 22, location: (22,32)-(22,32))
| +- nd_body:
| (null node)
+- nd_else:
(null node)
```
I'm struggling with writing a failing test, but the before/after
instructions show that `getglobal` is correct here. I compared the
instructions for the other `InterpolatedMatchLastLine` node tests
and they also used `getglobal`. I've edited the existing
`InterpolatedLastMatchLineNode` test so that it will use that instruction when
copied out of the test. Without the quotes it thinks it's just a
`MatchLastLineNode`.
Incorrect instructions:
```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(22,36)>
0000 putstring ".oo" ( 22)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getglobal :$_
0019 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0022 branchunless 30
0024 jump 26
0026 putnil
0027 jump 31
0029 pop
0030 putnil
0031 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:21 (21,0)-(21,36)>
0000 putstring ".oo" ( 21)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getspecial 0, 0
0020 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0023 branchunless 31
0025 jump 27
0027 putnil
0028 jump 32
0030 pop
0031 putnil
0032 leave
```
Fixed instructions:
```
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(22,36)>
0000 putstring ".oo" ( 22)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getglobal :$_
0019 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0022 branchunless 30
0024 jump 26
0026 putnil
0027 jump 31
0029 pop
0030 putnil
0031 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:21 (21,0)-(21,36)>
0000 putstring ".oo" ( 21)[Li]
0002 setglobal :$pit
0004 putobject "\""
0006 getglobal :$pit
0008 dup
0009 objtostring <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0011 anytostring
0012 putobject "\""
0014 toregexp 7, 3
0017 getglobal :$_
0019 send <calldata!mid:=~, argc:1, ARGS_SIMPLE>, nil
0022 branchunless 30
0024 jump 26
0026 putnil
0027 jump 31
0029 pop
0030 putnil
0031 leave
```
2023-12-12 23:52:53 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getglobal, rb_id2sym(idLASTLINE));
|
2023-09-28 17:18:43 +03:00
|
|
|
ADD_SEND(ret, &dummy_line_node, idEqTilde, INT2NUM(1));
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
|
2023-11-14 02:52:03 +03:00
|
|
|
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_ONCE) {
|
|
|
|
const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
|
|
|
|
const rb_iseq_t *block_iseq = NULL;
|
|
|
|
int ic_index = ISEQ_BODY(iseq)->ise_size++;
|
|
|
|
|
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init((pm_node_t*)node, &next_scope_node, scope_node, parser);
|
2024-01-18 19:38:53 +03:00
|
|
|
block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
|
|
|
|
2023-11-14 02:52:03 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, once, block_iseq, INT2FIX(ic_index));
|
|
|
|
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-28 17:18:43 +03:00
|
|
|
pm_interpolated_regular_expression_node_t *cast = (pm_interpolated_regular_expression_node_t *) node;
|
2023-11-14 02:52:03 +03:00
|
|
|
|
2024-02-12 23:48:23 +03:00
|
|
|
int parts_size = pm_interpolated_node_compile(&cast->parts, iseq, dummy_line_node, ret, popped, scope_node);
|
2023-09-08 19:09:30 +03:00
|
|
|
|
2023-11-14 02:52:03 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, toregexp, INT2FIX(pm_reg_flags(node)), INT2FIX(parts_size));
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-09-08 19:09:30 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTERPOLATED_STRING_NODE: {
|
|
|
|
pm_interpolated_string_node_t *interp_string_node = (pm_interpolated_string_node_t *) node;
|
2024-02-12 23:48:23 +03:00
|
|
|
int number_of_items_pushed = pm_interpolated_node_compile(&interp_string_node->parts, iseq, dummy_line_node, ret, popped, scope_node);
|
2023-09-08 19:09:30 +03:00
|
|
|
|
2023-12-16 00:28:29 +03:00
|
|
|
if (number_of_items_pushed > 1) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, concatstrings, INT2FIX(number_of_items_pushed));
|
2023-09-08 19:09:30 +03:00
|
|
|
}
|
2023-09-21 22:28:08 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTERPOLATED_SYMBOL_NODE: {
|
|
|
|
pm_interpolated_symbol_node_t *interp_symbol_node = (pm_interpolated_symbol_node_t *) node;
|
2024-02-12 23:48:23 +03:00
|
|
|
int number_of_items_pushed = pm_interpolated_node_compile(&interp_symbol_node->parts, iseq, dummy_line_node, ret, popped, scope_node);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-12-16 00:28:29 +03:00
|
|
|
if (number_of_items_pushed > 1) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, concatstrings, INT2FIX(number_of_items_pushed));
|
2023-09-08 19:09:30 +03:00
|
|
|
}
|
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, intern);
|
|
|
|
}
|
2023-09-21 22:28:08 +03:00
|
|
|
else {
|
2023-10-24 23:08:50 +03:00
|
|
|
PM_POP;
|
2023-09-21 22:28:08 +03:00
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTERPOLATED_X_STRING_NODE: {
|
|
|
|
pm_interpolated_x_string_node_t *interp_x_string_node = (pm_interpolated_x_string_node_t *) node;
|
2023-10-30 19:56:30 +03:00
|
|
|
PM_PUTSELF;
|
2024-02-12 23:48:23 +03:00
|
|
|
int number_of_items_pushed = pm_interpolated_node_compile(&interp_x_string_node->parts, iseq, dummy_line_node, ret, false, scope_node);
|
2023-09-08 17:40:07 +03:00
|
|
|
|
2023-12-16 00:28:29 +03:00
|
|
|
if (number_of_items_pushed > 1) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, concatstrings, INT2FIX(number_of_items_pushed));
|
2023-09-08 19:09:30 +03:00
|
|
|
}
|
|
|
|
|
2023-09-22 18:43:00 +03:00
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-09-08 17:40:07 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_KEYWORD_HASH_NODE: {
|
|
|
|
pm_keyword_hash_node_t *keyword_hash_node = (pm_keyword_hash_node_t *) node;
|
|
|
|
pm_node_list_t elements = keyword_hash_node->elements;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
for (size_t index = 0; index < elements.size; index++) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(elements.nodes[index]);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
2023-10-13 20:59:37 +03:00
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(elements.size * 2));
|
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_LAMBDA_NODE: {
|
2024-02-01 21:04:29 +03:00
|
|
|
const pm_lambda_node_t *cast = (const pm_lambda_node_t *) node;
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init(node, &next_scope_node, scope_node, parser);
|
2024-02-01 21:04:29 +03:00
|
|
|
|
2024-02-14 22:17:32 +03:00
|
|
|
int opening_lineno = pm_location_line_number(parser, &cast->opening_loc);
|
2024-02-01 21:04:29 +03:00
|
|
|
const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
VALUE argc = INT2FIX(0);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_CALL_WITH_BLOCK(ret, &dummy_line_node, idLambda, argc, block);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)block);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
|
|
|
|
pm_local_variable_and_write_node_t *local_variable_and_write_node = (pm_local_variable_and_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_constant_id_t constant_id = local_variable_and_write_node->name;
|
2024-01-16 01:32:30 +03:00
|
|
|
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, local_variable_and_write_node->depth);
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, end_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(local_variable_and_write_node->value);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level);
|
2023-09-02 00:20:03 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
|
|
|
|
pm_local_variable_operator_write_node_t *local_variable_operator_write_node = (pm_local_variable_operator_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_constant_id_t constant_id = local_variable_operator_write_node->name;
|
2024-01-23 23:33:45 +03:00
|
|
|
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, local_variable_operator_write_node->depth);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(local_variable_operator_write_node->value);
|
2023-10-17 01:36:25 +03:00
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, local_variable_operator_write_node->operator);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
int flags = VM_CALL_ARGS_SIMPLE | VM_CALL_FCALL | VM_CALL_VCALL;
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, method_id, INT2NUM(1), INT2FIX(flags));
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
|
|
|
|
pm_local_variable_or_write_node_t *local_variable_or_write_node = (pm_local_variable_or_write_node_t*) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
LABEL *set_label= NEW_LABEL(lineno);
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-09-02 00:20:03 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, set_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_constant_id_t constant_id = local_variable_or_write_node->name;
|
2024-01-23 23:33:45 +03:00
|
|
|
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, local_variable_or_write_node->depth);
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level);
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-09-02 00:20:03 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, end_label);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-09-02 00:20:03 +03:00
|
|
|
ADD_LABEL(ret, set_label);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(local_variable_or_write_node->value);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level);
|
2023-09-02 00:20:03 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_LOCAL_VARIABLE_READ_NODE: {
|
|
|
|
pm_local_variable_read_node_t *local_read_node = (pm_local_variable_read_node_t *) node;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
|
|
|
if (!popped) {
|
2024-01-23 23:33:45 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_read_node->name, local_read_node->depth);
|
2024-01-16 01:03:04 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_LOCAL_VARIABLE_WRITE_NODE: {
|
|
|
|
pm_local_variable_write_node_t *local_write_node = (pm_local_variable_write_node_t *) node;
|
|
|
|
PM_COMPILE_NOT_POPPED(local_write_node->value);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_constant_id_t constant_id = local_write_node->name;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2024-01-23 23:33:45 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, constant_id, local_write_node->depth);
|
2023-11-29 22:12:31 +03:00
|
|
|
|
2024-01-16 00:42:16 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 23:53:37 +03:00
|
|
|
case PM_MATCH_LAST_LINE_NODE: {
|
|
|
|
if (!popped) {
|
2023-09-28 17:18:43 +03:00
|
|
|
pm_match_last_line_node_t *cast = (pm_match_last_line_node_t *) node;
|
2023-09-27 23:53:37 +03:00
|
|
|
|
2024-02-14 22:17:32 +03:00
|
|
|
VALUE regex_str = parse_string(&cast->unescaped, parser);
|
2023-09-28 17:18:43 +03:00
|
|
|
VALUE regex = rb_reg_new(RSTRING_PTR(regex_str), RSTRING_LEN(regex_str), pm_reg_flags(node));
|
2024-02-16 22:23:00 +03:00
|
|
|
RB_GC_GUARD(regex_str);
|
2023-09-27 23:53:37 +03:00
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, regex);
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getspecial, INT2FIX(0), INT2FIX(0));
|
|
|
|
ADD_SEND(ret, &dummy_line_node, idEqTilde, INT2NUM(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-28 19:14:38 +03:00
|
|
|
case PM_MATCH_PREDICATE_NODE: {
|
|
|
|
pm_match_predicate_node_t *cast = (pm_match_predicate_node_t *) node;
|
|
|
|
|
|
|
|
// First, allocate some stack space for the cached return value of any
|
|
|
|
// calls to #deconstruct.
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-09-28 19:14:38 +03:00
|
|
|
|
|
|
|
// Next, compile the expression that we're going to match against.
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-09-28 19:14:38 +03:00
|
|
|
|
|
|
|
// Now compile the pattern that is going to be used to match against the
|
|
|
|
// expression.
|
|
|
|
LABEL *matched_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *unmatched_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *done_label = NEW_LABEL(lineno);
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, false, false, true, 2);
|
2023-09-28 19:14:38 +03:00
|
|
|
|
|
|
|
// If the pattern did not match, then compile the necessary instructions
|
|
|
|
// to handle pushing false onto the stack, then jump to the end.
|
|
|
|
ADD_LABEL(ret, unmatched_label);
|
2023-10-24 23:08:50 +03:00
|
|
|
PM_POP;
|
|
|
|
PM_POP;
|
2023-09-28 19:14:38 +03:00
|
|
|
|
|
|
|
if (!popped) ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, done_label);
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-09-28 19:14:38 +03:00
|
|
|
|
|
|
|
// If the pattern did match, then compile the necessary instructions to
|
|
|
|
// handle pushing true onto the stack, then jump to the end.
|
|
|
|
ADD_LABEL(ret, matched_label);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, adjuststack, INT2FIX(2));
|
|
|
|
if (!popped) ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, done_label);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, done_label);
|
|
|
|
return;
|
|
|
|
}
|
2023-12-13 19:34:05 +03:00
|
|
|
case PM_MATCH_REQUIRED_NODE: {
|
|
|
|
// A match required node represents pattern matching against a single
|
|
|
|
// pattern using the => operator. For example,
|
|
|
|
//
|
|
|
|
// foo => bar
|
|
|
|
//
|
|
|
|
// This is somewhat analogous to compiling a case match statement with a
|
|
|
|
// single pattern. In both cases, if the pattern fails it should
|
|
|
|
// immediately raise an error.
|
|
|
|
const pm_match_required_node_t *cast = (const pm_match_required_node_t *) node;
|
|
|
|
|
|
|
|
LABEL *matched_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *unmatched_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *done_label = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
// First, we're going to push a bunch of stuff onto the stack that is
|
|
|
|
// going to serve as our scratch space.
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil); // key error key
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil); // key error matchee
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse); // key error?
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil); // error string
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil); // deconstruct cache
|
|
|
|
|
|
|
|
// Next we're going to compile the value expression such that it's on
|
|
|
|
// the stack.
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
|
|
|
|
|
|
|
// Here we'll dup it so that it can be used for comparison, but also be
|
|
|
|
// used for error handling.
|
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
|
|
|
|
// Next we'll compile the pattern. We indicate to the pm_compile_pattern
|
|
|
|
// function that this is the only pattern that will be matched against
|
|
|
|
// through the in_single_pattern parameter. We also indicate that the
|
|
|
|
// value to compare against is 2 slots from the top of the stack (the
|
|
|
|
// base_index parameter).
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, true, false, true, 2);
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
// If the pattern did not match the value, then we're going to compile
|
|
|
|
// in our error handler code. This will determine which error to raise
|
|
|
|
// and raise it.
|
|
|
|
ADD_LABEL(ret, unmatched_label);
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_pattern_error_handler(iseq, scope_node, node, ret, done_label, popped);
|
2023-12-13 19:34:05 +03:00
|
|
|
|
|
|
|
// If the pattern did match, we'll clean up the values we've pushed onto
|
|
|
|
// the stack and then push nil onto the stack if it's not popped.
|
|
|
|
ADD_LABEL(ret, matched_label);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, adjuststack, INT2FIX(6));
|
|
|
|
if (!popped) ADD_INSN(ret, &dummy_line_node, putnil);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, done_label);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, done_label);
|
|
|
|
return;
|
|
|
|
}
|
2023-09-21 21:52:24 +03:00
|
|
|
case PM_MATCH_WRITE_NODE: {
|
2023-11-21 00:09:51 +03:00
|
|
|
// Match write nodes are specialized call nodes that have a regular
|
|
|
|
// expression with valid named capture groups on the left, the =~
|
|
|
|
// operator, and some value on the right. The nodes themselves simply
|
|
|
|
// wrap the call with the local variable targets that will be written
|
|
|
|
// when the call is executed.
|
|
|
|
pm_match_write_node_t *cast = (pm_match_write_node_t *) node;
|
2023-09-21 21:52:24 +03:00
|
|
|
LABEL *fail_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-11-21 00:09:51 +03:00
|
|
|
// First, we'll compile the call so that all of its instructions are
|
|
|
|
// present. Then we'll compile all of the local variable targets.
|
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *) cast->call);
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-11-21 00:09:51 +03:00
|
|
|
// Now, check if the match was successful. If it was, then we'll
|
|
|
|
// continue on and assign local variables. Otherwise we'll skip over the
|
|
|
|
// assignment code.
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, getglobal, rb_id2sym(idBACKREF));
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-09-21 21:52:24 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, fail_label);
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-11-21 00:09:51 +03:00
|
|
|
// If there's only a single local variable target, we can skip some of
|
|
|
|
// the bookkeeping, so we'll put a special branch here.
|
|
|
|
size_t targets_count = cast->targets.size;
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-11-21 00:09:51 +03:00
|
|
|
if (targets_count == 1) {
|
|
|
|
pm_node_t *target = cast->targets.nodes[0];
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-11-21 00:09:51 +03:00
|
|
|
pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target;
|
2024-01-23 23:33:45 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
|
2023-11-21 00:09:51 +03:00
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name)));
|
2023-09-21 21:52:24 +03:00
|
|
|
ADD_SEND(ret, &dummy_line_node, idAREF, INT2FIX(1));
|
2023-11-21 00:09:51 +03:00
|
|
|
ADD_LABEL(ret, fail_label);
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
2023-11-02 20:53:33 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-09-21 21:52:24 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-11-21 00:09:51 +03:00
|
|
|
// Otherwise there is more than one local variable target, so we'll need
|
|
|
|
// to do some bookkeeping.
|
|
|
|
for (size_t targets_index = 0; targets_index < targets_count; targets_index++) {
|
|
|
|
pm_node_t *target = cast->targets.nodes[targets_index];
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
|
2023-11-21 00:09:51 +03:00
|
|
|
|
|
|
|
pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target;
|
2024-01-23 23:33:45 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-11-21 00:09:51 +03:00
|
|
|
if (((size_t) targets_index) < (targets_count - 1)) {
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-09-21 21:52:24 +03:00
|
|
|
}
|
2023-11-21 00:09:51 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name)));
|
2023-09-21 21:52:24 +03:00
|
|
|
ADD_SEND(ret, &dummy_line_node, idAREF, INT2FIX(1));
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
2023-09-21 21:52:24 +03:00
|
|
|
}
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-11-21 00:09:51 +03:00
|
|
|
// Since we matched successfully, now we'll jump to the end.
|
2023-09-21 21:52:24 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, end_label);
|
2023-11-21 00:09:51 +03:00
|
|
|
|
|
|
|
// In the case that the match failed, we'll loop through each local
|
|
|
|
// variable target and set all of them to `nil`.
|
2023-09-21 21:52:24 +03:00
|
|
|
ADD_LABEL(ret, fail_label);
|
2023-11-21 00:09:51 +03:00
|
|
|
PM_POP;
|
|
|
|
|
|
|
|
for (size_t targets_index = 0; targets_index < targets_count; targets_index++) {
|
|
|
|
pm_node_t *target = cast->targets.nodes[targets_index];
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-11-21 00:09:51 +03:00
|
|
|
pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target;
|
2024-01-23 23:33:45 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
2023-09-21 21:52:24 +03:00
|
|
|
}
|
2023-11-21 00:09:51 +03:00
|
|
|
|
|
|
|
// Finally, we can push the end label for either case.
|
2023-09-21 21:52:24 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
2024-02-06 18:32:29 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-09-21 21:52:24 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_MISSING_NODE: {
|
|
|
|
rb_bug("A pm_missing_node_t should not exist in prism's AST.");
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_MODULE_NODE: {
|
|
|
|
pm_module_node_t *module_node = (pm_module_node_t *)node;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID module_id = pm_constant_id_lookup(scope_node, module_node->name);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
VALUE module_name = rb_str_freeze(rb_sprintf("<module:%"PRIsVALUE">", rb_id2str(module_id)));
|
|
|
|
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init((pm_node_t *)module_node, &next_scope_node, scope_node, parser);
|
2024-01-18 19:38:53 +03:00
|
|
|
const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(&next_scope_node, module_name, ISEQ_TYPE_CLASS, lineno);
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
const int flags = VM_DEFINECLASS_TYPE_MODULE |
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_class_path(ret, iseq, module_node->constant_path, &dummy_line_node, false, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
ADD_INSN3(ret, &dummy_line_node, defineclass, ID2SYM(module_id), module_iseq, INT2FIX(flags));
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)module_iseq);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-12-11 23:17:48 +03:00
|
|
|
case PM_REQUIRED_PARAMETER_NODE: {
|
|
|
|
pm_required_parameter_node_t *required_parameter_node = (pm_required_parameter_node_t *)node;
|
2024-01-16 01:32:30 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, required_parameter_node->name, 0);
|
2023-12-11 23:17:48 +03:00
|
|
|
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
2023-12-11 23:17:48 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_MULTI_WRITE_NODE: {
|
2024-01-12 23:23:47 +03:00
|
|
|
// A multi write node represents writing to multiple values using an =
|
|
|
|
// operator. Importantly these nodes are only parsed when the left-hand
|
|
|
|
// side of the operator has multiple targets. The right-hand side of the
|
|
|
|
// operator having multiple targets represents an implicit array
|
|
|
|
// instead.
|
|
|
|
const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
|
2023-09-19 23:59:07 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
DECL_ANCHOR(writes);
|
|
|
|
INIT_ANCHOR(writes);
|
2023-12-14 00:09:39 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
DECL_ANCHOR(cleanup);
|
|
|
|
INIT_ANCHOR(cleanup);
|
2023-12-15 23:07:49 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
pm_multi_target_state_t state = { 0 };
|
|
|
|
state.position = popped ? 0 : 1;
|
2024-01-31 20:17:31 +03:00
|
|
|
size_t stack_size = pm_compile_multi_target_node(iseq, node, ret, writes, cleanup, scope_node, &state);
|
2023-12-15 23:07:49 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-12-15 23:07:49 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
ADD_SEQ(ret, writes);
|
|
|
|
if (!popped && stack_size >= 1) {
|
|
|
|
// Make sure the value on the right-hand side of the = operator is
|
|
|
|
// being returned before we pop the parent expressions.
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(stack_size));
|
2023-09-19 23:59:07 +03:00
|
|
|
}
|
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
ADD_SEQ(ret, cleanup);
|
2023-11-01 21:22:08 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_NEXT_NODE: {
|
|
|
|
pm_next_node_t *next_node = (pm_next_node_t *) node;
|
2023-12-04 19:36:35 +03:00
|
|
|
|
|
|
|
if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
|
|
|
|
LABEL *splabel = NEW_LABEL(0);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, splabel);
|
|
|
|
|
2023-12-12 19:23:50 +03:00
|
|
|
if (next_node->arguments) {
|
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *)next_node->arguments);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
|
2023-12-04 19:36:35 +03:00
|
|
|
|
|
|
|
ADD_ADJUST(ret, &dummy_line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
|
|
|
|
|
|
|
|
ADD_ADJUST_RESTORE(ret, splabel);
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
2023-08-31 00:27:08 +03:00
|
|
|
}
|
2023-12-04 19:36:35 +03:00
|
|
|
else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
|
|
|
|
LABEL *splabel = NEW_LABEL(0);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, splabel);
|
|
|
|
ADD_ADJUST(ret, &dummy_line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
|
|
|
|
|
|
|
|
if (next_node->arguments) {
|
2023-12-07 22:13:33 +03:00
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *)next_node->arguments);
|
2023-12-04 19:36:35 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
|
2023-12-04 19:36:35 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
|
|
|
|
ADD_ADJUST_RESTORE(ret, splabel);
|
|
|
|
splabel->unremovable = FALSE;
|
|
|
|
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
2023-08-31 00:27:08 +03:00
|
|
|
}
|
2023-12-04 19:36:35 +03:00
|
|
|
else {
|
|
|
|
const rb_iseq_t *ip = iseq;
|
2023-08-31 00:27:08 +03:00
|
|
|
|
2023-12-04 19:36:35 +03:00
|
|
|
unsigned long throw_flag = 0;
|
|
|
|
while (ip) {
|
|
|
|
if (!ISEQ_COMPILE_DATA(ip)) {
|
|
|
|
ip = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw_flag = VM_THROW_NO_ESCAPE_FLAG;
|
|
|
|
if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
|
|
|
|
/* while loop */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
|
2024-02-08 17:57:40 +03:00
|
|
|
COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with next");
|
2023-12-04 19:36:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ip = ISEQ_BODY(ip)->parent_iseq;
|
|
|
|
}
|
|
|
|
if (ip != 0) {
|
|
|
|
if (next_node->arguments) {
|
2023-12-07 22:13:33 +03:00
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *)next_node->arguments);
|
2023-12-04 19:36:35 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, throw, INT2FIX(throw_flag | TAG_NEXT));
|
|
|
|
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "Invalid next");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 00:27:08 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_NIL_NODE:
|
2024-01-22 23:22:12 +03:00
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
2023-10-24 14:42:51 +03:00
|
|
|
case PM_NO_KEYWORDS_PARAMETER_NODE: {
|
|
|
|
ISEQ_BODY(iseq)->param.flags.accepts_no_kwarg = TRUE;
|
2023-10-23 17:24:35 +03:00
|
|
|
return;
|
2023-10-24 14:42:51 +03:00
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_NUMBERED_REFERENCE_READ_NODE: {
|
2023-09-01 21:30:22 +03:00
|
|
|
if (!popped) {
|
2023-09-27 19:39:53 +03:00
|
|
|
uint32_t reference_number = ((pm_numbered_reference_read_node_t *)node)->number;
|
2023-09-01 21:30:22 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, getspecial, INT2FIX(1), INT2FIX(reference_number << 1));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_OR_NODE: {
|
2024-02-20 19:47:56 +03:00
|
|
|
// a or b
|
|
|
|
// ^^^^^^
|
|
|
|
const pm_or_node_t *cast = (const pm_or_node_t *) node;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
LABEL *end_label = NEW_LABEL(location.line);
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->left);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
if (!popped) PUSH_INSN(ret, location, dup);
|
|
|
|
PUSH_INSNL(ret, location, branchif, end_label);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
if (!popped) PUSH_INSN(ret, location, pop);
|
|
|
|
PM_COMPILE(cast->right);
|
|
|
|
PUSH_LABEL(ret, end_label);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_OPTIONAL_PARAMETER_NODE: {
|
2024-02-20 19:47:56 +03:00
|
|
|
// def foo(bar = 1); end
|
|
|
|
// ^^^^^^^
|
|
|
|
const pm_optional_parameter_node_t *cast = (const pm_optional_parameter_node_t *) node;
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->value);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_PARENTHESES_NODE: {
|
2024-02-20 19:47:56 +03:00
|
|
|
// ()
|
|
|
|
// ^^
|
|
|
|
//
|
|
|
|
// (1)
|
|
|
|
// ^^^
|
|
|
|
const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
if (cast->body != NULL) {
|
|
|
|
PM_COMPILE(cast->body);
|
|
|
|
} else if (!popped) {
|
|
|
|
PUSH_INSN(ret, location, putnil);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-10-23 16:46:37 +03:00
|
|
|
case PM_PRE_EXECUTION_NODE: {
|
2024-02-20 19:47:56 +03:00
|
|
|
// BEGIN {}
|
|
|
|
// ^^^^^^^^
|
|
|
|
const pm_pre_execution_node_t *cast = (const pm_pre_execution_node_t *) node;
|
2023-10-23 16:46:37 +03:00
|
|
|
|
|
|
|
DECL_ANCHOR(pre_ex);
|
|
|
|
INIT_ANCHOR(pre_ex);
|
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
if (cast->statements != NULL) {
|
|
|
|
const pm_node_list_t *body = &cast->statements->body;
|
|
|
|
for (size_t index = 0; index < body->size; index++) {
|
|
|
|
pm_compile_node(iseq, body->nodes[index], pre_ex, true, scope_node);
|
2023-10-23 16:46:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!popped) {
|
2024-02-20 19:47:56 +03:00
|
|
|
PUSH_INSN(pre_ex, location, putnil);
|
2023-10-23 16:46:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pre_ex->last->next = ret->anchor.next;
|
2023-10-23 20:49:39 +03:00
|
|
|
ret->anchor.next = pre_ex->anchor.next;
|
|
|
|
ret->anchor.next->prev = pre_ex->anchor.next;
|
|
|
|
|
|
|
|
if (ret->last == (LINK_ELEMENT *)ret) {
|
|
|
|
ret->last = pre_ex->last;
|
|
|
|
}
|
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-30 23:57:27 +03:00
|
|
|
case PM_POST_EXECUTION_NODE: {
|
2024-02-20 19:47:56 +03:00
|
|
|
// END {}
|
|
|
|
// ^^^^^^
|
2023-10-30 23:57:27 +03:00
|
|
|
const rb_iseq_t *child_iseq;
|
|
|
|
const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
|
|
|
|
|
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init(node, &next_scope_node, scope_node, parser);
|
2024-01-18 19:38:53 +03:00
|
|
|
child_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
|
|
|
|
2023-10-30 23:57:27 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
|
|
|
|
|
|
|
|
int is_index = ISEQ_BODY(iseq)->ise_size++;
|
2024-02-20 19:47:56 +03:00
|
|
|
PUSH_INSN2(ret, location, once, child_iseq, INT2FIX(is_index));
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-10-30 23:57:27 +03:00
|
|
|
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_RANGE_NODE: {
|
2024-02-20 19:47:56 +03:00
|
|
|
// 0..5
|
|
|
|
// ^^^^
|
|
|
|
const pm_range_node_t *cast = (const pm_range_node_t *) node;
|
|
|
|
bool exclude_end = PM_NODE_FLAG_P(cast, PM_RANGE_FLAGS_EXCLUDE_END);
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
if (pm_optimizable_range_item_p(cast->left) && pm_optimizable_range_item_p(cast->right)) {
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
if (!popped) {
|
2024-02-20 19:47:56 +03:00
|
|
|
const pm_node_t *left = cast->left;
|
|
|
|
const pm_node_t *right = cast->right;
|
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
VALUE val = rb_range_new(
|
2024-02-20 19:47:56 +03:00
|
|
|
(left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) left) : Qnil,
|
|
|
|
(right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE)) ? parse_integer((const pm_integer_node_t *) right) : Qnil,
|
|
|
|
exclude_end
|
|
|
|
);
|
|
|
|
|
|
|
|
PUSH_INSN1(ret, location, putobject, val);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2024-02-20 19:47:56 +03:00
|
|
|
if (cast->left == NULL) {
|
|
|
|
PUSH_INSN(ret, location, putnil);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
} else {
|
2024-02-20 19:47:56 +03:00
|
|
|
PM_COMPILE(cast->left);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
if (cast->right == NULL) {
|
|
|
|
PUSH_INSN(ret, location, putnil);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
} else {
|
2024-02-20 19:47:56 +03:00
|
|
|
PM_COMPILE(cast->right);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
if (!popped) {
|
2024-02-20 19:47:56 +03:00
|
|
|
PUSH_INSN1(ret, location, newrange, INT2FIX(exclude_end ? 1 : 0));
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_RATIONAL_NODE: {
|
2024-02-20 19:47:56 +03:00
|
|
|
// 1r
|
|
|
|
// ^^
|
2023-09-08 22:33:51 +03:00
|
|
|
if (!popped) {
|
2024-02-23 04:22:47 +03:00
|
|
|
PUSH_INSN1(ret, location, putobject, parse_rational((const pm_rational_node_t *) node));
|
2023-09-08 22:33:51 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_REDO_NODE: {
|
2024-02-20 19:47:56 +03:00
|
|
|
// redo
|
|
|
|
// ^^^^
|
2023-12-04 18:55:11 +03:00
|
|
|
if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
|
|
|
|
LABEL *splabel = NEW_LABEL(0);
|
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
PUSH_LABEL(ret, splabel);
|
|
|
|
PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->redo_label);
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
|
2024-02-20 19:47:56 +03:00
|
|
|
|
|
|
|
PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
|
|
|
|
PUSH_ADJUST_RESTORE(ret, splabel);
|
|
|
|
if (!popped) PUSH_INSN(ret, location, putnil);
|
2023-12-04 18:55:11 +03:00
|
|
|
}
|
|
|
|
else if (ISEQ_BODY(iseq)->type != ISEQ_TYPE_EVAL && ISEQ_COMPILE_DATA(iseq)->start_label && can_add_ensure_iseq(iseq)) {
|
|
|
|
LABEL *splabel = NEW_LABEL(0);
|
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
PUSH_LABEL(ret, splabel);
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 0, scope_node);
|
2024-02-20 19:47:56 +03:00
|
|
|
PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->start_label);
|
2023-12-04 18:55:11 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
|
|
|
|
PUSH_ADJUST_RESTORE(ret, splabel);
|
|
|
|
if (!popped) PUSH_INSN(ret, location, putnil);
|
2023-12-04 18:55:11 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const rb_iseq_t *ip = iseq;
|
|
|
|
|
|
|
|
while (ip) {
|
2024-02-20 19:47:56 +03:00
|
|
|
if (!ISEQ_COMPILE_DATA(ip)) {
|
|
|
|
ip = 0;
|
|
|
|
break;
|
|
|
|
}
|
2023-12-04 18:55:11 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
|
|
|
|
COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with redo");
|
|
|
|
return;
|
|
|
|
}
|
2023-12-04 18:55:11 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
ip = ISEQ_BODY(ip)->parent_iseq;
|
|
|
|
}
|
2023-12-04 18:55:11 +03:00
|
|
|
|
2024-02-20 19:47:56 +03:00
|
|
|
if (ip != 0) {
|
|
|
|
PUSH_INSN(ret, location, putnil);
|
|
|
|
PUSH_INSN1(ret, location, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
COMPILE_ERROR(ERROR_ARGS "Invalid redo");
|
|
|
|
return;
|
|
|
|
}
|
2023-12-04 18:55:11 +03:00
|
|
|
}
|
2023-08-31 00:27:08 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_REGULAR_EXPRESSION_NODE: {
|
2024-02-20 19:29:33 +03:00
|
|
|
// /foo/
|
|
|
|
// ^^^^^
|
2023-09-08 19:09:30 +03:00
|
|
|
if (!popped) {
|
2024-02-20 19:29:33 +03:00
|
|
|
VALUE regex = pm_static_literal_value(node, scope_node);
|
|
|
|
PUSH_INSN1(ret, location, putobject, regex);
|
2023-09-08 19:09:30 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-11-10 17:51:14 +03:00
|
|
|
case PM_RESCUE_NODE: {
|
2024-02-20 19:29:33 +03:00
|
|
|
// begin; rescue; end
|
|
|
|
// ^^^^^^^
|
|
|
|
const pm_rescue_node_t *cast = (const pm_rescue_node_t *) node;
|
2023-11-10 17:51:14 +03:00
|
|
|
iseq_set_exception_local_table(iseq);
|
|
|
|
|
2024-01-12 01:05:42 +03:00
|
|
|
// First, establish the labels that we need to be able to jump to within
|
|
|
|
// this compilation block.
|
2024-02-20 19:29:33 +03:00
|
|
|
LABEL *exception_match_label = NEW_LABEL(location.line);
|
|
|
|
LABEL *rescue_end_label = NEW_LABEL(location.line);
|
2024-01-12 01:05:42 +03:00
|
|
|
|
|
|
|
// Next, compile each of the exceptions that we're going to be
|
|
|
|
// handling. For each one, we'll add instructions to check if the
|
|
|
|
// exception matches the raised one, and if it does then jump to the
|
|
|
|
// exception_match_label label. Otherwise it will fall through to the
|
|
|
|
// subsequent check. If there are no exceptions, we'll only check
|
|
|
|
// StandardError.
|
2024-02-20 19:29:33 +03:00
|
|
|
const pm_node_list_t *exceptions = &cast->exceptions;
|
2024-01-12 01:05:42 +03:00
|
|
|
|
|
|
|
if (exceptions->size > 0) {
|
|
|
|
for (size_t index = 0; index < exceptions->size; index++) {
|
2023-11-10 17:51:14 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
|
2024-01-12 01:05:42 +03:00
|
|
|
PM_COMPILE(exceptions->nodes[index]);
|
2024-01-18 17:39:32 +03:00
|
|
|
int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
|
|
|
|
if (PM_NODE_TYPE_P(exceptions->nodes[index], PM_SPLAT_NODE)) {
|
|
|
|
checkmatch_flags |= VM_CHECKMATCH_ARRAY;
|
|
|
|
}
|
2024-02-20 19:29:33 +03:00
|
|
|
PUSH_INSN1(ret, location, checkmatch, INT2FIX(checkmatch_flags));
|
|
|
|
PUSH_INSNL(ret, location, branchif, exception_match_label);
|
2023-11-10 17:51:14 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
|
2024-02-20 19:29:33 +03:00
|
|
|
PUSH_INSN1(ret, location, putobject, rb_eStandardError);
|
|
|
|
PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
|
|
|
|
PUSH_INSNL(ret, location, branchif, exception_match_label);
|
2023-11-10 17:51:14 +03:00
|
|
|
}
|
|
|
|
|
2024-01-12 01:05:42 +03:00
|
|
|
// If none of the exceptions that we are matching against matched, then
|
|
|
|
// we'll jump straight to the rescue_end_label label.
|
2024-02-20 19:29:33 +03:00
|
|
|
PUSH_INSNL(ret, location, jump, rescue_end_label);
|
2024-01-12 01:05:42 +03:00
|
|
|
|
|
|
|
// Here we have the exception_match_label, which is where the
|
|
|
|
// control-flow goes in the case that one of the exceptions matched.
|
|
|
|
// Here we will compile the instructions to handle the exception.
|
2024-02-20 19:29:33 +03:00
|
|
|
PUSH_LABEL(ret, exception_match_label);
|
|
|
|
PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
|
2023-12-01 21:00:20 +03:00
|
|
|
|
2024-01-12 01:05:42 +03:00
|
|
|
// If we have a reference to the exception, then we'll compile the write
|
|
|
|
// into the instruction sequence. This can look quite different
|
|
|
|
// depending on the kind of write being performed.
|
|
|
|
if (cast->reference) {
|
2024-01-12 23:23:47 +03:00
|
|
|
DECL_ANCHOR(writes);
|
|
|
|
INIT_ANCHOR(writes);
|
2024-01-12 01:05:42 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
DECL_ANCHOR(cleanup);
|
|
|
|
INIT_ANCHOR(cleanup);
|
2024-01-12 01:05:42 +03:00
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_target_node(iseq, cast->reference, ret, writes, cleanup, scope_node, NULL);
|
2024-01-12 23:23:47 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
|
2024-01-12 01:05:42 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
ADD_SEQ(ret, writes);
|
|
|
|
ADD_SEQ(ret, cleanup);
|
2023-11-10 17:51:14 +03:00
|
|
|
}
|
2024-01-12 01:05:42 +03:00
|
|
|
|
|
|
|
// If we have statements to execute, we'll compile them here. Otherwise
|
|
|
|
// we'll push nil onto the stack.
|
|
|
|
if (cast->statements) {
|
2024-01-25 15:54:49 +03:00
|
|
|
// We'll temporarily remove the end_label location from the iseq
|
|
|
|
// when compiling the statements so that next/redo statements
|
|
|
|
// inside the body will throw to the correct place instead of
|
|
|
|
// jumping straight to the end of this iseq
|
|
|
|
LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
|
|
|
|
|
2024-01-12 01:05:42 +03:00
|
|
|
PM_COMPILE((pm_node_t *) cast->statements);
|
2024-01-25 15:54:49 +03:00
|
|
|
|
|
|
|
// Now restore the end_label
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
|
2024-01-12 01:05:42 +03:00
|
|
|
} else {
|
2024-02-20 19:29:33 +03:00
|
|
|
PUSH_INSN(ret, location, putnil);
|
2023-12-01 21:00:20 +03:00
|
|
|
}
|
|
|
|
|
2024-02-20 19:29:33 +03:00
|
|
|
PUSH_INSN(ret, location, leave);
|
2023-11-10 17:51:14 +03:00
|
|
|
|
2024-01-12 01:05:42 +03:00
|
|
|
// Here we'll insert the rescue_end_label label, which is jumped to if
|
|
|
|
// none of the exceptions matched. It will cause the control-flow to
|
|
|
|
// either jump to the next rescue clause or it will fall through to the
|
|
|
|
// subsequent instruction returning the raised error.
|
2024-02-20 19:29:33 +03:00
|
|
|
PUSH_LABEL(ret, rescue_end_label);
|
2024-01-12 01:05:42 +03:00
|
|
|
if (cast->consequent) {
|
|
|
|
PM_COMPILE((pm_node_t *) cast->consequent);
|
2023-11-10 17:51:14 +03:00
|
|
|
} else {
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-12-01 17:37:10 +03:00
|
|
|
case PM_RESCUE_MODIFIER_NODE: {
|
2024-02-20 19:29:33 +03:00
|
|
|
// foo rescue bar
|
|
|
|
// ^^^^^^^^^^^^^^
|
|
|
|
const pm_rescue_modifier_node_t *cast = (const pm_rescue_modifier_node_t *) node;
|
2024-01-18 19:55:31 +03:00
|
|
|
|
|
|
|
pm_scope_node_t rescue_scope_node;
|
2024-02-20 19:29:33 +03:00
|
|
|
pm_scope_node_init((const pm_node_t *) cast, &rescue_scope_node, scope_node, parser);
|
2024-01-30 00:03:03 +03:00
|
|
|
|
|
|
|
rb_iseq_t *rescue_iseq = NEW_CHILD_ISEQ(
|
|
|
|
&rescue_scope_node,
|
|
|
|
rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
|
|
|
|
ISEQ_TYPE_RESCUE,
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_node_line_number(parser, cast->rescue_expression)
|
2024-01-30 00:03:03 +03:00
|
|
|
);
|
|
|
|
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&rescue_scope_node);
|
2023-12-01 17:37:10 +03:00
|
|
|
|
2024-02-20 19:29:33 +03:00
|
|
|
LABEL *lstart = NEW_LABEL(location.line);
|
|
|
|
LABEL *lend = NEW_LABEL(location.line);
|
|
|
|
LABEL *lcont = NEW_LABEL(location.line);
|
2023-12-01 17:37:10 +03:00
|
|
|
|
|
|
|
lstart->rescued = LABEL_RESCUE_BEG;
|
|
|
|
lend->rescued = LABEL_RESCUE_END;
|
2024-02-20 19:29:33 +03:00
|
|
|
PUSH_LABEL(ret, lstart);
|
2024-01-30 00:03:03 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->expression);
|
2024-02-20 19:29:33 +03:00
|
|
|
PUSH_LABEL(ret, lend);
|
|
|
|
PUSH_INSN(ret, location, nop);
|
|
|
|
PUSH_LABEL(ret, lcont);
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-12-01 17:37:10 +03:00
|
|
|
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_RESCUE, lstart, lend, rescue_iseq, lcont);
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_RETURN_NODE: {
|
2024-02-20 19:23:06 +03:00
|
|
|
// return
|
|
|
|
// ^^^^^^
|
|
|
|
//
|
|
|
|
// return 1
|
|
|
|
// ^^^^^^^^
|
|
|
|
const pm_return_node_t *cast = (const pm_return_node_t *) node;
|
|
|
|
const pm_arguments_node_t *arguments = cast->arguments;
|
2023-12-05 19:31:12 +03:00
|
|
|
|
2024-02-20 19:23:06 +03:00
|
|
|
enum rb_iseq_type type = ISEQ_BODY(iseq)->type;
|
|
|
|
LABEL *splabel = 0;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-20 19:23:06 +03:00
|
|
|
const rb_iseq_t *parent_iseq = iseq;
|
|
|
|
enum rb_iseq_type parent_type = ISEQ_BODY(parent_iseq)->type;
|
|
|
|
while (parent_type == ISEQ_TYPE_RESCUE || parent_type == ISEQ_TYPE_ENSURE) {
|
|
|
|
if (!(parent_iseq = ISEQ_BODY(parent_iseq)->parent_iseq)) break;
|
|
|
|
parent_type = ISEQ_BODY(parent_iseq)->type;
|
|
|
|
}
|
2023-12-05 19:31:12 +03:00
|
|
|
|
2024-02-20 19:23:06 +03:00
|
|
|
switch (parent_type) {
|
|
|
|
case ISEQ_TYPE_TOP:
|
|
|
|
case ISEQ_TYPE_MAIN:
|
2023-12-05 19:31:12 +03:00
|
|
|
if (arguments) {
|
2024-02-20 19:23:06 +03:00
|
|
|
rb_warn("argument of top-level return is ignored");
|
2023-12-05 19:31:12 +03:00
|
|
|
}
|
2024-02-20 19:23:06 +03:00
|
|
|
if (parent_iseq == iseq) {
|
|
|
|
type = ISEQ_TYPE_METHOD;
|
2023-12-05 19:31:12 +03:00
|
|
|
}
|
2024-02-20 19:23:06 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-12-05 19:31:12 +03:00
|
|
|
|
2024-02-20 19:23:06 +03:00
|
|
|
if (type == ISEQ_TYPE_METHOD) {
|
|
|
|
splabel = NEW_LABEL(0);
|
|
|
|
PUSH_LABEL(ret, splabel);
|
|
|
|
PUSH_ADJUST(ret, location, 0);
|
|
|
|
}
|
2023-12-05 19:31:12 +03:00
|
|
|
|
2024-02-20 19:23:06 +03:00
|
|
|
if (arguments) {
|
|
|
|
PM_COMPILE_NOT_POPPED((const pm_node_t *) arguments);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PUSH_INSN(ret, location, putnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
|
|
|
|
pm_add_ensure_iseq(ret, iseq, 1, scope_node);
|
|
|
|
PUSH_TRACE(ret, RUBY_EVENT_RETURN);
|
|
|
|
PUSH_INSN(ret, location, leave);
|
|
|
|
PUSH_ADJUST_RESTORE(ret, splabel);
|
|
|
|
if (!popped) PUSH_INSN(ret, location, putnil);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PUSH_INSN1(ret, location, throw, INT2FIX(TAG_RETURN));
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
2023-12-05 19:31:12 +03:00
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-12-04 15:27:34 +03:00
|
|
|
case PM_RETRY_NODE: {
|
2024-02-20 19:23:06 +03:00
|
|
|
// retry
|
|
|
|
// ^^^^^
|
2023-12-04 15:27:34 +03:00
|
|
|
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_RESCUE) {
|
2024-02-20 19:23:06 +03:00
|
|
|
PUSH_INSN(ret, location, putnil);
|
|
|
|
PUSH_INSN1(ret, location, throw, INT2FIX(TAG_RETRY));
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-12-04 15:27:34 +03:00
|
|
|
} else {
|
|
|
|
COMPILE_ERROR(ERROR_ARGS "Invalid retry");
|
2024-02-08 17:57:40 +03:00
|
|
|
return;
|
2023-12-04 15:27:34 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SCOPE_NODE: {
|
|
|
|
pm_scope_node_t *scope_node = (pm_scope_node_t *)node;
|
2023-11-01 16:02:00 +03:00
|
|
|
pm_constant_id_list_t *locals = &scope_node->locals;
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
pm_parameters_node_t *parameters_node = NULL;
|
2023-11-07 16:10:23 +03:00
|
|
|
pm_node_list_t *keywords_list = NULL;
|
2023-11-01 16:02:00 +03:00
|
|
|
pm_node_list_t *optionals_list = NULL;
|
2023-11-07 16:10:23 +03:00
|
|
|
pm_node_list_t *posts_list = NULL;
|
|
|
|
pm_node_list_t *requireds_list = NULL;
|
2023-12-06 18:05:14 +03:00
|
|
|
pm_node_list_t *block_locals = NULL;
|
2024-01-23 03:03:58 +03:00
|
|
|
bool trailing_comma = false;
|
2023-11-07 16:10:23 +03:00
|
|
|
|
|
|
|
struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
if (scope_node->parameters) {
|
|
|
|
switch (PM_NODE_TYPE(scope_node->parameters)) {
|
|
|
|
case PM_BLOCK_PARAMETERS_NODE: {
|
|
|
|
pm_block_parameters_node_t *block_parameters_node = (pm_block_parameters_node_t *)scope_node->parameters;
|
|
|
|
parameters_node = block_parameters_node->parameters;
|
|
|
|
block_locals = &block_parameters_node->locals;
|
2023-12-13 17:40:34 +03:00
|
|
|
if (parameters_node) {
|
2024-01-23 03:03:58 +03:00
|
|
|
if (parameters_node->rest && PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE)) {
|
|
|
|
trailing_comma = true;
|
|
|
|
}
|
2023-12-13 17:40:34 +03:00
|
|
|
}
|
2023-12-06 18:05:14 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_PARAMETERS_NODE: {
|
|
|
|
parameters_node = (pm_parameters_node_t *) scope_node->parameters;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_NUMBERED_PARAMETERS_NODE: {
|
|
|
|
body->param.lead_num = ((pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
|
|
|
|
break;
|
|
|
|
}
|
2024-02-22 01:25:20 +03:00
|
|
|
case PM_IT_PARAMETERS_NODE:
|
|
|
|
body->param.lead_num = 1;
|
|
|
|
break;
|
2023-12-06 18:05:14 +03:00
|
|
|
default:
|
|
|
|
rb_bug("Unexpected node type for parameters: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rb_iseq_param_keyword *keyword = NULL;
|
|
|
|
|
2023-11-07 16:10:23 +03:00
|
|
|
if (parameters_node) {
|
2023-11-01 16:02:00 +03:00
|
|
|
optionals_list = ¶meters_node->optionals;
|
2023-11-07 16:10:23 +03:00
|
|
|
requireds_list = ¶meters_node->requireds;
|
|
|
|
keywords_list = ¶meters_node->keywords;
|
|
|
|
posts_list = ¶meters_node->posts;
|
2024-02-22 01:25:20 +03:00
|
|
|
}
|
|
|
|
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))) {
|
2023-11-07 16:10:23 +03:00
|
|
|
body->param.opt_num = 0;
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-11-07 16:10:23 +03:00
|
|
|
body->param.lead_num = 0;
|
|
|
|
body->param.opt_num = 0;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
//********STEP 1**********
|
|
|
|
// Goal: calculate the table size for the locals, accounting for
|
|
|
|
// hidden variables and multi target nodes
|
2023-11-07 16:10:23 +03:00
|
|
|
size_t locals_size = locals->size;
|
2023-09-28 16:47:46 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
// Index lookup table buffer size is only the number of the locals
|
|
|
|
st_table *index_lookup_table = st_init_numtable();
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-12-01 00:18:14 +03:00
|
|
|
int table_size = (int) locals_size;
|
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
// For nodes have a hidden iteration variable. We add that to the local
|
|
|
|
// table size here.
|
|
|
|
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
|
2023-12-06 18:05:14 +03:00
|
|
|
|
2023-12-01 00:18:14 +03:00
|
|
|
if (keywords_list && keywords_list->size) {
|
|
|
|
table_size++;
|
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
if (requireds_list) {
|
|
|
|
for (size_t i = 0; i < requireds_list->size; i++) {
|
|
|
|
// For each MultiTargetNode, we're going to have one
|
|
|
|
// additional anonymous local not represented in the locals table
|
|
|
|
// We want to account for this in our table size
|
|
|
|
pm_node_t *required = requireds_list->nodes[i];
|
|
|
|
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
|
|
|
|
table_size++;
|
|
|
|
}
|
2023-12-13 20:17:22 +03:00
|
|
|
else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
|
2024-01-11 03:27:00 +03:00
|
|
|
if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
table_size++;
|
2023-12-13 20:17:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2024-01-24 00:13:56 +03:00
|
|
|
// Ensure there is enough room in the local table for any
|
|
|
|
// parameters that have been repeated
|
|
|
|
// ex: def underscore_parameters(_, _ = 1, _ = 2); _; end
|
|
|
|
// ^^^^^^^^^^^^
|
|
|
|
if (optionals_list && optionals_list->size) {
|
|
|
|
for (size_t i = 0; i < optionals_list->size; i++) {
|
|
|
|
pm_node_t * node = optionals_list->nodes[i];
|
|
|
|
if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
table_size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-12 01:50:09 +03:00
|
|
|
// If we have an anonymous "rest" node, we'll need to increase the local
|
|
|
|
// table size to take it in to account.
|
|
|
|
// def m(foo, *, bar)
|
|
|
|
// ^
|
2024-01-24 00:50:27 +03:00
|
|
|
if (parameters_node) {
|
|
|
|
if (parameters_node->rest) {
|
|
|
|
if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
|
|
|
|
if (!((pm_rest_parameter_node_t *)parameters_node->rest)->name || PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
table_size++;
|
|
|
|
}
|
2024-01-12 01:50:09 +03:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 00:50:27 +03:00
|
|
|
|
2024-01-26 02:08:44 +03:00
|
|
|
// def foo(_, **_); _; end
|
|
|
|
// ^^^
|
|
|
|
if (parameters_node->keyword_rest) {
|
|
|
|
// def foo(...); end
|
|
|
|
// ^^^
|
|
|
|
// When we have a `...` as the keyword_rest, it's a forwarding_parameter_node and
|
|
|
|
// we need to leave space for 4 locals: *, **, &, ...
|
|
|
|
if (PM_NODE_TYPE_P(parameters_node->keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
|
|
|
|
table_size += 4;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_keyword_rest_parameter_node_t * kw_rest = (pm_keyword_rest_parameter_node_t *)parameters_node->keyword_rest;
|
|
|
|
|
|
|
|
// If it's anonymous or repeated, then we need to allocate stack space
|
|
|
|
if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
table_size++;
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 00:50:27 +03:00
|
|
|
}
|
2024-01-12 01:50:09 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
if (posts_list) {
|
|
|
|
for (size_t i = 0; i < posts_list->size; i++) {
|
|
|
|
// For each MultiTargetNode, we're going to have one
|
|
|
|
// additional anonymous local not represented in the locals table
|
|
|
|
// We want to account for this in our table size
|
|
|
|
pm_node_t *required = posts_list->nodes[i];
|
2024-01-24 00:33:31 +03:00
|
|
|
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
2023-12-06 18:05:14 +03:00
|
|
|
table_size++;
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 00:18:14 +03:00
|
|
|
}
|
|
|
|
|
2024-01-24 00:41:34 +03:00
|
|
|
if (keywords_list && keywords_list->size) {
|
|
|
|
for (size_t i = 0; i < keywords_list->size; i++) {
|
|
|
|
pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
|
|
|
|
if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
table_size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 00:55:46 +03:00
|
|
|
if (parameters_node && parameters_node->block) {
|
2024-01-26 02:08:44 +03:00
|
|
|
pm_block_parameter_node_t * block_node = (pm_block_parameter_node_t *)parameters_node->block;
|
|
|
|
|
|
|
|
if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
|
2024-01-24 00:55:46 +03:00
|
|
|
table_size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
// We can create local_table_for_iseq with the correct size
|
|
|
|
VALUE idtmp = 0;
|
|
|
|
rb_ast_id_table_t *local_table_for_iseq = ALLOCV(idtmp, sizeof(rb_ast_id_table_t) + table_size * sizeof(ID));
|
|
|
|
local_table_for_iseq->size = table_size;
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
//********END OF STEP 1**********
|
2023-12-01 00:18:14 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
//********STEP 2**********
|
|
|
|
// Goal: populate iv index table as well as local table, keeping the
|
|
|
|
// layout of the local table consistent with the layout of the
|
|
|
|
// stack when calling the method
|
|
|
|
//
|
|
|
|
// Do a first pass on all of the parameters, setting their values in
|
|
|
|
// the local_table_for_iseq, _except_ for Multis who get a hidden
|
|
|
|
// variable in this step, and will get their names inserted in step 3
|
|
|
|
|
|
|
|
// local_index is a cursor that keeps track of the current
|
|
|
|
// index into local_table_for_iseq. The local table is actually a list,
|
|
|
|
// and the order of that list must match the order of the items pushed
|
|
|
|
// on the stack. We need to take in to account things pushed on the
|
|
|
|
// stack that _might not have a name_ (for example array destructuring).
|
|
|
|
// This index helps us know which item we're dealing with and also give
|
|
|
|
// those anonymous items temporary names (as below)
|
|
|
|
int local_index = 0;
|
|
|
|
|
|
|
|
// Here we figure out local table indices and insert them in to the
|
|
|
|
// index lookup table and local tables.
|
|
|
|
//
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^^^^^^^^^^^^^
|
|
|
|
if (requireds_list && requireds_list->size) {
|
|
|
|
for (size_t i = 0; i < requireds_list->size; i++, local_index++) {
|
|
|
|
ID local;
|
2024-01-26 02:08:44 +03:00
|
|
|
|
|
|
|
// For each MultiTargetNode, we're going to have one additional
|
|
|
|
// anonymous local not represented in the locals table. We want
|
|
|
|
// to account for this in our table size.
|
2023-12-06 18:05:14 +03:00
|
|
|
pm_node_t *required = requireds_list->nodes[i];
|
2024-01-26 02:08:44 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
switch (PM_NODE_TYPE(required)) {
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^^^^^^^^^^
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
2024-01-18 22:59:59 +03:00
|
|
|
local = rb_make_temporary_id(local_index);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
break;
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^
|
|
|
|
case PM_REQUIRED_PARAMETER_NODE: {
|
2024-01-18 22:59:59 +03:00
|
|
|
pm_required_parameter_node_t * param = (pm_required_parameter_node_t *)required;
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2024-01-23 23:55:00 +03:00
|
|
|
if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
ID local = pm_constant_id_lookup(scope_node, param->name);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
}
|
|
|
|
else {
|
2024-01-18 22:59:59 +03:00
|
|
|
pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2024-01-26 02:08:44 +03:00
|
|
|
|
2024-01-18 22:59:59 +03:00
|
|
|
break;
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
default: {
|
2024-01-18 22:59:59 +03:00
|
|
|
rb_bug("Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-11-07 16:10:23 +03:00
|
|
|
body->param.lead_num = (int) requireds_list->size;
|
|
|
|
body->param.flags.has_lead = true;
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
2023-11-07 16:10:23 +03:00
|
|
|
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^^^^^
|
|
|
|
if (optionals_list && optionals_list->size) {
|
|
|
|
body->param.opt_num = (int) optionals_list->size;
|
|
|
|
body->param.flags.has_opt = true;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < optionals_list->size; i++, local_index++) {
|
2024-01-24 00:13:56 +03:00
|
|
|
pm_node_t * node = optionals_list->nodes[i];
|
|
|
|
pm_constant_id_t name = ((pm_optional_parameter_node_t *)node)->name;
|
|
|
|
|
|
|
|
if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
ID local = pm_constant_id_lookup(scope_node, name);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^^
|
2023-12-01 00:18:14 +03:00
|
|
|
if (parameters_node && parameters_node->rest) {
|
2023-12-06 18:05:14 +03:00
|
|
|
body->param.rest_start = local_index;
|
2024-01-26 02:08:44 +03:00
|
|
|
|
2023-11-30 20:57:49 +03:00
|
|
|
// If there's a trailing comma, we'll have an implicit rest node,
|
|
|
|
// and we don't want it to impact the rest variables on param
|
|
|
|
if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
|
|
|
|
body->param.flags.has_rest = true;
|
2024-02-13 23:13:55 +03:00
|
|
|
RUBY_ASSERT(body->param.rest_start != -1);
|
2023-12-06 18:05:14 +03:00
|
|
|
|
2024-01-26 02:08:44 +03:00
|
|
|
pm_constant_id_t name = ((pm_rest_parameter_node_t *) parameters_node->rest)->name;
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
if (name) {
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-12 17:40:16 +03:00
|
|
|
// ^^
|
2024-01-24 00:20:49 +03:00
|
|
|
if (PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
ID local = pm_constant_id_lookup(scope_node, name);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
2023-12-12 17:40:16 +03:00
|
|
|
else {
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-12 17:40:16 +03:00
|
|
|
// ^
|
2024-01-26 02:08:44 +03:00
|
|
|
pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
|
2023-12-12 17:40:16 +03:00
|
|
|
}
|
2024-01-26 02:08:44 +03:00
|
|
|
|
2023-12-12 17:40:16 +03:00
|
|
|
local_index++;
|
2023-11-30 20:57:49 +03:00
|
|
|
}
|
2023-12-01 00:18:14 +03:00
|
|
|
}
|
|
|
|
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^^^^^^^^^^^^^
|
2023-11-07 16:10:23 +03:00
|
|
|
if (posts_list && posts_list->size) {
|
|
|
|
body->param.post_num = (int) posts_list->size;
|
2023-12-06 18:05:14 +03:00
|
|
|
body->param.post_start = local_index;
|
2023-11-07 16:10:23 +03:00
|
|
|
body->param.flags.has_post = true;
|
2023-12-06 18:05:14 +03:00
|
|
|
|
|
|
|
for (size_t i = 0; i < posts_list->size; i++, local_index++) {
|
|
|
|
ID local;
|
2024-02-06 19:15:33 +03:00
|
|
|
|
|
|
|
// For each MultiTargetNode, we're going to have one additional
|
|
|
|
// anonymous local not represented in the locals table. We want
|
|
|
|
// to account for this in our table size.
|
|
|
|
const pm_node_t *post_node = posts_list->nodes[i];
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
switch (PM_NODE_TYPE(post_node)) {
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^^^^^^^^^^
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
2024-01-19 19:18:47 +03:00
|
|
|
local = rb_make_temporary_id(local_index);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
break;
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^
|
|
|
|
case PM_REQUIRED_PARAMETER_NODE: {
|
2024-02-06 19:15:33 +03:00
|
|
|
const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) post_node;
|
2024-01-26 02:08:44 +03:00
|
|
|
|
2024-01-24 00:33:31 +03:00
|
|
|
if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
ID local = pm_constant_id_lookup(scope_node, param->name);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2024-01-19 19:18:47 +03:00
|
|
|
break;
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
default: {
|
2024-01-19 19:18:47 +03:00
|
|
|
rb_bug("Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^^^^^^^^
|
2023-11-07 16:10:23 +03:00
|
|
|
// Keywords create an internal variable on the parse tree
|
|
|
|
if (keywords_list && keywords_list->size) {
|
|
|
|
body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
|
|
|
|
keyword->num = (int) keywords_list->size;
|
|
|
|
|
2023-11-08 01:28:21 +03:00
|
|
|
body->param.flags.has_kw = true;
|
2023-11-07 16:10:23 +03:00
|
|
|
const VALUE default_values = rb_ary_hidden_new(1);
|
|
|
|
const VALUE complex_mark = rb_str_tmp_new(0);
|
|
|
|
|
2023-12-13 05:34:14 +03:00
|
|
|
ID *ids = xcalloc(keywords_list->size, sizeof(ID));
|
2023-11-08 20:49:58 +03:00
|
|
|
|
2024-01-19 23:00:35 +03:00
|
|
|
size_t kw_index = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < keywords_list->size; i++) {
|
2023-11-07 16:10:23 +03:00
|
|
|
pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
|
2023-11-13 22:32:39 +03:00
|
|
|
pm_constant_id_t name;
|
2023-11-07 16:10:23 +03:00
|
|
|
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2024-01-19 23:00:35 +03:00
|
|
|
// ^^
|
|
|
|
if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
|
|
|
|
name = ((pm_required_keyword_parameter_node_t *)keyword_parameter_node)->name;
|
|
|
|
keyword->required_num++;
|
|
|
|
ID local = pm_constant_id_lookup(scope_node, name);
|
2024-01-24 00:41:34 +03:00
|
|
|
|
|
|
|
if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2024-01-19 23:00:35 +03:00
|
|
|
local_index++;
|
|
|
|
ids[kw_index++] = local;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < keywords_list->size; i++) {
|
|
|
|
pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
|
|
|
|
pm_constant_id_t name;
|
|
|
|
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2024-01-19 23:00:35 +03:00
|
|
|
// ^^^^
|
|
|
|
if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
|
2023-11-09 16:29:44 +03:00
|
|
|
pm_optional_keyword_parameter_node_t *cast = ((pm_optional_keyword_parameter_node_t *)keyword_parameter_node);
|
2023-11-13 22:32:39 +03:00
|
|
|
|
2023-11-09 16:29:44 +03:00
|
|
|
pm_node_t *value = cast->value;
|
|
|
|
name = cast->name;
|
2023-11-07 16:10:23 +03:00
|
|
|
|
2023-12-14 14:58:50 +03:00
|
|
|
if (pm_static_literal_p(value) &&
|
|
|
|
!(PM_NODE_TYPE_P(value, PM_ARRAY_NODE) ||
|
|
|
|
PM_NODE_TYPE_P(value, PM_HASH_NODE) ||
|
|
|
|
PM_NODE_TYPE_P(value, PM_RANGE_NODE))) {
|
|
|
|
|
2024-02-12 23:48:23 +03:00
|
|
|
rb_ary_push(default_values, pm_static_literal_value(value, scope_node));
|
2023-12-14 14:58:50 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-11-09 16:29:44 +03:00
|
|
|
rb_ary_push(default_values, complex_mark);
|
|
|
|
}
|
|
|
|
|
2024-01-19 23:00:35 +03:00
|
|
|
ID local = pm_constant_id_lookup(scope_node, name);
|
2024-01-24 00:41:34 +03:00
|
|
|
if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2024-01-19 23:00:35 +03:00
|
|
|
ids[kw_index++] = local;
|
|
|
|
local_index++;
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
2023-11-13 22:32:39 +03:00
|
|
|
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
2023-11-08 20:49:58 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
keyword->bits_start = local_index;
|
2023-11-13 22:32:39 +03:00
|
|
|
keyword->table = ids;
|
|
|
|
|
2023-11-08 20:49:58 +03:00
|
|
|
VALUE *dvs = ALLOC_N(VALUE, RARRAY_LEN(default_values));
|
|
|
|
|
|
|
|
for (int i = 0; i < RARRAY_LEN(default_values); i++) {
|
|
|
|
VALUE dv = RARRAY_AREF(default_values, i);
|
|
|
|
if (dv == complex_mark) dv = Qundef;
|
|
|
|
if (!SPECIAL_CONST_P(dv)) {
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, dv);
|
|
|
|
}
|
|
|
|
dvs[i] = dv;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyword->default_values = dvs;
|
2023-12-06 18:05:14 +03:00
|
|
|
|
|
|
|
// Hidden local for keyword arguments
|
|
|
|
ID local = rb_make_temporary_id(local_index);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
local_index++;
|
|
|
|
}
|
|
|
|
|
2024-01-23 03:03:58 +03:00
|
|
|
if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->size == 1 && !trailing_comma) {
|
2023-12-06 18:05:14 +03:00
|
|
|
body->param.flags.ambiguous_param0 = true;
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters_node) {
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^^^
|
2023-11-07 16:10:23 +03:00
|
|
|
if (parameters_node->keyword_rest) {
|
2023-12-11 22:17:02 +03:00
|
|
|
switch (PM_NODE_TYPE(parameters_node->keyword_rest)) {
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **nil, &n)
|
2023-12-11 22:17:02 +03:00
|
|
|
// ^^^^^
|
|
|
|
case PM_NO_KEYWORDS_PARAMETER_NODE: {
|
2024-01-19 19:18:47 +03:00
|
|
|
body->param.flags.accepts_no_kwarg = true;
|
|
|
|
break;
|
2023-12-11 22:17:02 +03:00
|
|
|
}
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-11 22:17:02 +03:00
|
|
|
// ^^^
|
|
|
|
case PM_KEYWORD_REST_PARAMETER_NODE: {
|
2024-02-06 19:15:33 +03:00
|
|
|
const pm_keyword_rest_parameter_node_t *kw_rest_node = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
|
2024-01-19 19:18:47 +03:00
|
|
|
if (!body->param.flags.has_kw) {
|
|
|
|
body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
|
|
|
|
}
|
2023-12-06 18:05:14 +03:00
|
|
|
|
2024-01-19 19:18:47 +03:00
|
|
|
keyword->rest_start = local_index;
|
|
|
|
body->param.flags.has_kwrest = true;
|
2023-12-11 22:17:02 +03:00
|
|
|
|
2024-01-19 19:18:47 +03:00
|
|
|
pm_constant_id_t constant_id = kw_rest_node->name;
|
|
|
|
if (constant_id) {
|
2024-01-24 00:50:27 +03:00
|
|
|
if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
ID local = pm_constant_id_lookup(scope_node, constant_id);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2024-01-19 19:18:47 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-01-26 02:08:44 +03:00
|
|
|
pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
|
2024-01-19 19:18:47 +03:00
|
|
|
}
|
2024-01-26 02:08:44 +03:00
|
|
|
|
2024-01-19 19:18:47 +03:00
|
|
|
local_index++;
|
|
|
|
break;
|
2023-12-11 22:17:02 +03:00
|
|
|
}
|
|
|
|
// def foo(...)
|
|
|
|
// ^^^
|
|
|
|
case PM_FORWARDING_PARAMETER_NODE: {
|
2024-01-19 19:18:47 +03:00
|
|
|
body->param.rest_start = local_index;
|
|
|
|
body->param.flags.has_rest = true;
|
2024-01-26 02:08:44 +03:00
|
|
|
|
|
|
|
// Add the leading *
|
|
|
|
pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
|
|
|
|
|
|
|
|
// Add the kwrest **
|
|
|
|
RUBY_ASSERT(!body->param.flags.has_kw);
|
|
|
|
|
|
|
|
// There are no keywords declared (in the text of the program)
|
|
|
|
// but the forwarding node implies we support kwrest (**)
|
|
|
|
body->param.flags.has_kw = false;
|
|
|
|
body->param.flags.has_kwrest = true;
|
|
|
|
body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
|
|
|
|
|
|
|
|
keyword->rest_start = local_index;
|
|
|
|
|
|
|
|
pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
|
2024-01-19 19:18:47 +03:00
|
|
|
|
|
|
|
body->param.block_start = local_index;
|
|
|
|
body->param.flags.has_block = true;
|
|
|
|
|
2024-01-26 02:08:44 +03:00
|
|
|
pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
|
|
|
|
pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
|
2024-01-19 19:18:47 +03:00
|
|
|
break;
|
2023-12-11 22:17:02 +03:00
|
|
|
}
|
|
|
|
default: {
|
2024-01-19 19:18:47 +03:00
|
|
|
rb_bug("node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->keyword_rest)));
|
2023-12-11 22:17:02 +03:00
|
|
|
}
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-06 18:05:14 +03:00
|
|
|
// ^^
|
2023-11-07 16:10:23 +03:00
|
|
|
if (parameters_node->block) {
|
2023-12-06 18:05:14 +03:00
|
|
|
body->param.block_start = local_index;
|
2023-11-07 16:10:23 +03:00
|
|
|
body->param.flags.has_block = true;
|
2023-12-06 18:05:14 +03:00
|
|
|
|
2024-01-26 02:08:44 +03:00
|
|
|
pm_constant_id_t name = ((pm_block_parameter_node_t *) parameters_node->block)->name;
|
2024-01-24 00:55:46 +03:00
|
|
|
|
2024-01-26 02:08:44 +03:00
|
|
|
if (name) {
|
2024-01-24 00:55:46 +03:00
|
|
|
if (PM_NODE_FLAG_P(parameters_node->block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
|
|
|
|
ID local = pm_constant_id_lookup(scope_node, name);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2024-01-23 20:11:14 +03:00
|
|
|
}
|
2024-01-26 02:08:44 +03:00
|
|
|
else {
|
|
|
|
pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
|
|
|
|
}
|
|
|
|
|
2024-01-23 22:43:53 +03:00
|
|
|
local_index++;
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
//********END OF STEP 2**********
|
|
|
|
// The local table is now consistent with expected
|
|
|
|
// stack layout
|
|
|
|
|
2023-11-30 20:57:49 +03:00
|
|
|
// If there's only one required element in the parameters
|
|
|
|
// CRuby needs to recognize it as an ambiguous parameter
|
2023-12-06 18:05:14 +03:00
|
|
|
|
|
|
|
//********STEP 3**********
|
|
|
|
// Goal: fill in the names of the parameters in MultiTargetNodes
|
|
|
|
//
|
|
|
|
// Go through requireds again to set the multis
|
2023-12-11 23:17:48 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
if (requireds_list && requireds_list->size) {
|
|
|
|
for (size_t i = 0; i < requireds_list->size; i++) {
|
|
|
|
// For each MultiTargetNode, we're going to have one
|
|
|
|
// additional anonymous local not represented in the locals table
|
|
|
|
// We want to account for this in our table size
|
2024-02-06 19:15:33 +03:00
|
|
|
const pm_node_t *required = requireds_list->nodes[i];
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
|
2024-02-06 19:15:33 +03:00
|
|
|
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);
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
}
|
2023-11-30 20:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
// Go through posts again to set the multis
|
|
|
|
if (posts_list && posts_list->size) {
|
|
|
|
for (size_t i = 0; i < posts_list->size; i++) {
|
|
|
|
// For each MultiTargetNode, we're going to have one
|
|
|
|
// additional anonymous local not represented in the locals table
|
|
|
|
// We want to account for this in our table size
|
2024-02-06 19:15:33 +03:00
|
|
|
const pm_node_t *post = posts_list->nodes[i];
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
|
2024-02-06 19:15:33 +03:00
|
|
|
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);
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set any anonymous locals for the for node
|
|
|
|
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
|
2024-01-12 23:23:47 +03:00
|
|
|
if (PM_NODE_TYPE_P(((const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
|
|
|
|
body->param.lead_num++;
|
|
|
|
} else {
|
|
|
|
body->param.rest_start = local_index;
|
|
|
|
body->param.flags.has_rest = true;
|
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
ID local = rb_make_temporary_id(local_index);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
local_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in any NumberedParameters, if they exist
|
|
|
|
if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE)) {
|
|
|
|
int maximum = ((pm_numbered_parameters_node_t *)scope_node->parameters)->maximum;
|
2024-02-02 02:31:42 +03:00
|
|
|
RUBY_ASSERT(0 < maximum && maximum <= 9);
|
2023-12-06 18:05:14 +03:00
|
|
|
for (int i = 0; i < maximum; i++, local_index++) {
|
2024-02-02 02:31:42 +03:00
|
|
|
const uint8_t param_name[] = { '_', '1' + i };
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_constant_id_t constant_id = pm_constant_pool_find(&parser->constant_pool, param_name, 2);
|
2024-02-02 02:31:42 +03:00
|
|
|
RUBY_ASSERT(constant_id && "parser should fill in any gaps in numbered parameters");
|
2023-12-06 18:05:14 +03:00
|
|
|
pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2024-02-02 02:31:42 +03:00
|
|
|
body->param.lead_num = maximum;
|
|
|
|
body->param.flags.has_lead = true;
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
2024-02-22 01:25:20 +03:00
|
|
|
|
|
|
|
// Fill in the it variable, if it exists
|
|
|
|
if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
|
|
|
|
const uint8_t param_name[] = { '0', 'i', 't' };
|
|
|
|
pm_constant_id_t constant_id = pm_constant_pool_find(&parser->constant_pool, param_name, 3);
|
|
|
|
RUBY_ASSERT(constant_id && "parser should have inserted 0it for 'it' local");
|
|
|
|
|
|
|
|
ID local = rb_make_temporary_id(local_index);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
|
|
|
|
local_index++;
|
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
//********END OF STEP 3**********
|
|
|
|
|
|
|
|
//********STEP 4**********
|
|
|
|
// Goal: fill in the method body locals
|
|
|
|
// To be explicit, these are the non-parameter locals
|
2024-01-24 03:48:28 +03:00
|
|
|
// We fill in the block_locals, if they exist
|
|
|
|
// lambda { |x; y| y }
|
|
|
|
// ^
|
|
|
|
if (block_locals && block_locals->size) {
|
|
|
|
for (size_t i = 0; i < block_locals->size; i++, local_index++) {
|
|
|
|
pm_constant_id_t constant_id = ((pm_block_local_variable_node_t *)block_locals->nodes[i])->name;
|
|
|
|
pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in any locals we missed
|
2023-12-06 18:05:14 +03:00
|
|
|
if (scope_node->locals.size) {
|
2024-01-23 23:11:21 +03:00
|
|
|
for (size_t i = 0; i < scope_node->locals.size; i++) {
|
2023-12-06 18:05:14 +03:00
|
|
|
pm_constant_id_t constant_id = locals->ids[i];
|
|
|
|
if (constant_id) {
|
2024-01-23 23:11:21 +03:00
|
|
|
struct pm_local_table_insert_ctx ctx;
|
|
|
|
ctx.scope_node = scope_node;
|
|
|
|
ctx.local_table_for_iseq = local_table_for_iseq;
|
|
|
|
ctx.local_index = local_index;
|
|
|
|
|
2024-01-26 22:51:25 +03:00
|
|
|
st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
|
2024-01-23 23:11:21 +03:00
|
|
|
|
|
|
|
local_index = ctx.local_index;
|
2023-12-06 18:05:14 +03:00
|
|
|
}
|
|
|
|
}
|
2023-12-01 18:51:41 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
//********END OF STEP 4**********
|
|
|
|
|
|
|
|
// We set the index_lookup_table on the scope node so we can
|
|
|
|
// refer to the parameters correctly
|
2024-01-18 19:55:31 +03:00
|
|
|
if (scope_node->index_lookup_table) {
|
|
|
|
st_free_table(scope_node->index_lookup_table);
|
|
|
|
}
|
2023-12-06 18:05:14 +03:00
|
|
|
scope_node->index_lookup_table = index_lookup_table;
|
|
|
|
iseq_calc_param_size(iseq);
|
|
|
|
iseq_set_local_table(iseq, local_table_for_iseq);
|
|
|
|
scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
|
|
|
|
|
|
|
|
//********STEP 5************
|
|
|
|
// Goal: compile anything that needed to be compiled
|
2024-01-24 22:41:43 +03:00
|
|
|
if (optionals_list && optionals_list->size) {
|
|
|
|
LABEL **opt_table = (LABEL **)ALLOC_N(VALUE, optionals_list->size + 1);
|
|
|
|
LABEL *label;
|
|
|
|
|
|
|
|
// TODO: Should we make an api for NEW_LABEL where you can pass
|
|
|
|
// a pointer to the label it should fill out? We already
|
|
|
|
// have a list of labels allocated above so it seems wasteful
|
|
|
|
// to do the copies.
|
|
|
|
for (size_t i = 0; i < optionals_list->size; i++) {
|
|
|
|
label = NEW_LABEL(lineno);
|
|
|
|
opt_table[i] = label;
|
|
|
|
ADD_LABEL(ret, label);
|
|
|
|
pm_node_t *optional_node = optionals_list->nodes[i];
|
|
|
|
PM_COMPILE_NOT_POPPED(optional_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the last label
|
|
|
|
label = NEW_LABEL(lineno);
|
|
|
|
opt_table[optionals_list->size] = label;
|
|
|
|
ADD_LABEL(ret, label);
|
|
|
|
|
|
|
|
body->param.opt_table = (const VALUE *)opt_table;
|
|
|
|
}
|
|
|
|
|
2023-12-14 00:41:45 +03:00
|
|
|
if (keywords_list && keywords_list->size) {
|
2024-01-23 01:51:12 +03:00
|
|
|
size_t optional_index = 0;
|
2024-01-24 03:48:28 +03:00
|
|
|
for (size_t i = 0; i < keywords_list->size; i++) {
|
2023-12-14 00:41:45 +03:00
|
|
|
pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
|
|
|
|
pm_constant_id_t name;
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(keyword_parameter_node)) {
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-14 00:41:45 +03:00
|
|
|
// ^^^^
|
|
|
|
case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
|
|
|
|
pm_optional_keyword_parameter_node_t *cast = ((pm_optional_keyword_parameter_node_t *)keyword_parameter_node);
|
|
|
|
|
|
|
|
pm_node_t *value = cast->value;
|
|
|
|
name = cast->name;
|
|
|
|
|
2023-12-14 14:58:50 +03:00
|
|
|
if (!(pm_static_literal_p(value)) ||
|
|
|
|
PM_NODE_TYPE_P(value, PM_ARRAY_NODE) ||
|
|
|
|
PM_NODE_TYPE_P(value, PM_HASH_NODE) ||
|
|
|
|
PM_NODE_TYPE_P(value, PM_RANGE_NODE)) {
|
2023-12-14 00:41:45 +03:00
|
|
|
LABEL *end_label = NEW_LABEL(nd_line(&dummy_line_node));
|
|
|
|
|
2024-01-16 01:32:30 +03:00
|
|
|
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
|
2023-12-14 00:41:45 +03:00
|
|
|
int kw_bits_idx = table_size - body->param.keyword->bits_start;
|
2024-01-23 01:51:12 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, checkkeyword, INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1), INT2FIX(optional_index));
|
2023-12-14 00:41:45 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, end_label);
|
|
|
|
PM_COMPILE(value);
|
2024-01-15 23:59:11 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
|
2023-12-14 00:41:45 +03:00
|
|
|
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
}
|
2024-01-23 01:51:12 +03:00
|
|
|
optional_index++;
|
2023-12-14 14:58:50 +03:00
|
|
|
break;
|
2023-12-14 00:41:45 +03:00
|
|
|
}
|
2024-02-06 19:15:33 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
2023-12-14 00:41:45 +03:00
|
|
|
// ^^
|
|
|
|
case PM_REQUIRED_KEYWORD_PARAMETER_NODE: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2024-01-02 19:34:04 +03:00
|
|
|
rb_bug("Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
|
2023-12-14 00:41:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 23:17:48 +03:00
|
|
|
if (requireds_list && requireds_list->size) {
|
|
|
|
for (size_t i = 0; i < requireds_list->size; i++) {
|
2024-01-24 18:39:40 +03:00
|
|
|
// For each MultiTargetNode, we're going to have one additional
|
|
|
|
// anonymous local not represented in the locals table. We want
|
|
|
|
// to account for this in our table size.
|
|
|
|
const pm_node_t *required = requireds_list->nodes[i];
|
|
|
|
|
2023-12-11 23:17:48 +03:00
|
|
|
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
|
2024-01-30 17:37:52 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, table_size - (int)i, 0);
|
2024-01-24 18:39:40 +03:00
|
|
|
pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) required, ret, scope_node);
|
2023-12-11 23:17:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (posts_list && posts_list->size) {
|
|
|
|
for (size_t i = 0; i < posts_list->size; i++) {
|
2024-01-24 18:39:40 +03:00
|
|
|
// For each MultiTargetNode, we're going to have one additional
|
|
|
|
// anonymous local not represented in the locals table. We want
|
|
|
|
// to account for this in our table size.
|
|
|
|
const pm_node_t *post = posts_list->nodes[i];
|
|
|
|
|
2023-12-11 23:17:48 +03:00
|
|
|
if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
|
2024-02-06 19:15:33 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, table_size - body->param.post_start - (int) i, 0);
|
2024-01-24 18:39:40 +03:00
|
|
|
pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) post, ret, scope_node);
|
2023-12-11 23:17:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 16:10:23 +03:00
|
|
|
switch (body->type) {
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
case ISEQ_TYPE_BLOCK: {
|
|
|
|
LABEL *start = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(0);
|
|
|
|
LABEL *end = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(0);
|
2024-01-12 23:23:47 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(body->location.first_lineno, -1);
|
2023-09-28 16:47:46 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
start->rescued = LABEL_RESCUE_BEG;
|
|
|
|
end->rescued = LABEL_RESCUE_END;
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
// For nodes automatically assign the iteration variable to whatever
|
|
|
|
// index variable. We need to handle that write here because it has
|
|
|
|
// to happen in the context of the block. Note that this happens
|
|
|
|
// before the B_CALL tracepoint event.
|
|
|
|
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_for_node_index(iseq, ((const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
|
2023-12-11 18:33:52 +03:00
|
|
|
}
|
2024-01-12 23:23:47 +03:00
|
|
|
|
|
|
|
ADD_TRACE(ret, RUBY_EVENT_B_CALL);
|
|
|
|
PM_NOP;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
ADD_LABEL(ret, start);
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2024-01-12 23:23:47 +03:00
|
|
|
if (scope_node->body != NULL) {
|
2023-10-30 23:57:27 +03:00
|
|
|
switch (PM_NODE_TYPE(scope_node->ast_node)) {
|
|
|
|
case PM_POST_EXECUTION_NODE: {
|
|
|
|
pm_post_execution_node_t *post_execution_node = (pm_post_execution_node_t *)scope_node->ast_node;
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
|
|
|
|
// We create another ScopeNode from the statements within the PostExecutionNode
|
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_scope_node_init((pm_node_t *)post_execution_node->statements, &next_scope_node, scope_node, parser);
|
2024-01-18 19:38:53 +03:00
|
|
|
const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, lineno);
|
2024-01-19 19:26:28 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
2023-10-30 23:57:27 +03:00
|
|
|
|
|
|
|
ADD_CALL_WITH_BLOCK(ret, &dummy_line_node, id_core_set_postexe, INT2FIX(0), block);
|
|
|
|
break;
|
|
|
|
}
|
2023-11-14 02:52:03 +03:00
|
|
|
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
|
|
|
|
pm_interpolated_regular_expression_node_t *cast = (pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
|
|
|
|
|
2024-02-12 23:48:23 +03:00
|
|
|
int parts_size = pm_interpolated_node_compile(&cast->parts, iseq, dummy_line_node, ret, popped, scope_node);
|
2023-11-14 02:52:03 +03:00
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, toregexp, INT2FIX(pm_reg_flags((pm_node_t *)cast)), INT2FIX(parts_size));
|
|
|
|
break;
|
2023-10-30 23:57:27 +03:00
|
|
|
}
|
2024-01-12 23:23:47 +03:00
|
|
|
default:
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
|
2024-01-12 23:23:47 +03:00
|
|
|
break;
|
2023-10-20 12:59:02 +03:00
|
|
|
}
|
2024-01-12 23:23:47 +03:00
|
|
|
} else {
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-09-07 17:59:51 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
ADD_LABEL(ret, end);
|
|
|
|
ADD_TRACE(ret, RUBY_EVENT_B_RETURN);
|
2023-11-07 16:10:23 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
|
2023-09-28 16:47:46 +03:00
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
/* wide range catch handler must put at last */
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
|
|
|
|
break;
|
2024-01-12 23:23:47 +03:00
|
|
|
}
|
|
|
|
case ISEQ_TYPE_ENSURE: {
|
2023-11-03 02:29:20 +03:00
|
|
|
iseq_set_exception_local_table(iseq);
|
2023-11-29 22:12:31 +03:00
|
|
|
|
|
|
|
if (scope_node->body) {
|
|
|
|
PM_COMPILE_POPPED((pm_node_t *)scope_node->body);
|
|
|
|
}
|
2023-11-03 02:29:20 +03:00
|
|
|
|
2023-11-09 01:05:51 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, 1, 0);
|
2023-11-03 02:29:20 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, throw, INT2FIX(0));
|
|
|
|
return;
|
2024-01-12 23:23:47 +03:00
|
|
|
}
|
2024-01-22 14:26:04 +03:00
|
|
|
case ISEQ_TYPE_METHOD: {
|
|
|
|
ADD_TRACE(ret, RUBY_EVENT_CALL);
|
|
|
|
if (scope_node->body) {
|
|
|
|
PM_COMPILE((pm_node_t *)scope_node->body);
|
|
|
|
} else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
2024-02-05 20:58:16 +03:00
|
|
|
|
2024-01-22 14:26:04 +03:00
|
|
|
ADD_TRACE(ret, RUBY_EVENT_RETURN);
|
2024-02-05 20:58:16 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
|
2024-01-22 14:26:04 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2024-01-12 23:23:47 +03:00
|
|
|
case ISEQ_TYPE_RESCUE: {
|
2023-11-10 17:51:14 +03:00
|
|
|
iseq_set_exception_local_table(iseq);
|
2023-12-01 17:37:10 +03:00
|
|
|
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
|
|
|
|
LABEL *lab = NEW_LABEL(lineno);
|
|
|
|
LABEL *rescue_end = NEW_LABEL(lineno);
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_eStandardError);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
|
2024-01-26 20:17:04 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, lab);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, rescue_end);
|
2023-12-01 17:37:10 +03:00
|
|
|
ADD_LABEL(ret, lab);
|
|
|
|
PM_COMPILE((pm_node_t *)scope_node->body);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, leave);
|
|
|
|
ADD_LABEL(ret, rescue_end);
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_COMPILE((pm_node_t *)scope_node->ast_node);
|
|
|
|
}
|
2023-11-10 17:51:14 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, throw, INT2FIX(0));
|
|
|
|
|
|
|
|
return;
|
2024-01-12 23:23:47 +03:00
|
|
|
}
|
|
|
|
default:
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
if (scope_node->body) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE((pm_node_t *)scope_node->body);
|
2024-01-12 23:23:47 +03:00
|
|
|
} else {
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2024-01-12 23:23:47 +03:00
|
|
|
break;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-11-07 00:32:02 +03:00
|
|
|
if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
|
2024-01-22 19:55:50 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(ISEQ_COMPILE_DATA(iseq)->last_line, -1);
|
2023-11-03 02:29:20 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, leave);
|
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SELF_NODE:
|
2024-02-20 19:23:06 +03:00
|
|
|
// self
|
|
|
|
// ^^^^
|
2023-09-07 17:59:51 +03:00
|
|
|
if (!popped) {
|
2024-02-20 19:23:06 +03:00
|
|
|
PUSH_INSN(ret, location, putself);
|
2023-09-07 17:59:51 +03:00
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SINGLETON_CLASS_NODE: {
|
2024-02-20 19:23:06 +03:00
|
|
|
// class << self; end
|
|
|
|
// ^^^^^^^^^^^^^^^^^^
|
|
|
|
const pm_singleton_class_node_t *cast = (const pm_singleton_class_node_t *) node;
|
2024-01-18 19:55:31 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-20 19:23:06 +03:00
|
|
|
pm_scope_node_init((const pm_node_t *) cast, &next_scope_node, scope_node, parser);
|
|
|
|
const rb_iseq_t *child_iseq = NEW_ISEQ(&next_scope_node, rb_fstring_lit("singleton class"), ISEQ_TYPE_CLASS, location.line);
|
2024-01-18 19:55:31 +03:00
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2024-02-20 19:23:06 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->expression);
|
|
|
|
PUSH_INSN(ret, location, putnil);
|
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
ID singletonclass;
|
|
|
|
CONST_ID(singletonclass, "singletonclass");
|
2024-02-20 19:23:06 +03:00
|
|
|
PUSH_INSN3(ret, location, defineclass, ID2SYM(singletonclass), child_iseq, INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2024-02-20 19:23:06 +03:00
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SOURCE_ENCODING_NODE: {
|
2024-02-20 19:11:12 +03:00
|
|
|
// __ENCODING__
|
|
|
|
// ^^^^^^^^^^^^
|
2023-08-31 19:56:13 +03:00
|
|
|
if (!popped) {
|
2024-02-12 23:48:23 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node);
|
2024-02-20 19:11:12 +03:00
|
|
|
PUSH_INSN1(ret, location, putobject, value);
|
2023-08-31 19:56:13 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SOURCE_FILE_NODE: {
|
2024-02-20 19:11:12 +03:00
|
|
|
// __FILE__
|
|
|
|
// ^^^^^^^^
|
2023-08-31 19:56:13 +03:00
|
|
|
if (!popped) {
|
2024-02-12 23:48:23 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node);
|
2024-02-20 19:11:12 +03:00
|
|
|
PUSH_INSN1(ret, location, putstring, value);
|
2023-08-31 19:56:13 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SOURCE_LINE_NODE: {
|
2024-02-20 19:11:12 +03:00
|
|
|
// __LINE__
|
|
|
|
// ^^^^^^^^
|
2023-08-31 19:56:13 +03:00
|
|
|
if (!popped) {
|
2024-02-12 23:48:23 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node);
|
2024-02-20 19:11:12 +03:00
|
|
|
PUSH_INSN1(ret, location, putobject, value);
|
2023-08-31 19:56:13 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SPLAT_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// foo(*bar)
|
|
|
|
// ^^^^
|
|
|
|
const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
|
|
|
|
if (cast->expression) {
|
|
|
|
PM_COMPILE(cast->expression);
|
2023-10-13 20:59:37 +03:00
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2023-10-16 15:22:07 +03:00
|
|
|
if (!popped) {
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN1(ret, location, splatarray, Qtrue);
|
2023-10-16 15:22:07 +03:00
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_STATEMENTS_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// A list of statements.
|
|
|
|
const pm_statements_node_t *cast = (const pm_statements_node_t *) node;
|
|
|
|
const pm_node_list_t *body = &cast->body;
|
|
|
|
|
|
|
|
if (body->size > 0) {
|
|
|
|
for (size_t index = 0; index < body->size - 1; index++) {
|
|
|
|
PM_COMPILE_POPPED(body->nodes[index]);
|
2023-10-17 01:36:25 +03:00
|
|
|
}
|
2024-02-20 19:08:30 +03:00
|
|
|
PM_COMPILE(body->nodes[body->size - 1]);
|
2023-09-07 17:59:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN(ret, location, putnil);
|
2023-09-07 17:59:51 +03:00
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_STRING_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// "foo"
|
|
|
|
// ^^^^^
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
2024-02-20 19:08:30 +03:00
|
|
|
const pm_string_node_t *cast = (const pm_string_node_t *) node;
|
|
|
|
VALUE value = rb_fstring(parse_string_encoded(node, &cast->unescaped, parser));
|
|
|
|
|
|
|
|
if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FROZEN)) {
|
|
|
|
PUSH_INSN1(ret, location, putobject, value);
|
2023-12-14 18:19:31 +03:00
|
|
|
}
|
|
|
|
else {
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN1(ret, location, putstring, value);
|
2023-12-14 18:19:31 +03:00
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-10-31 18:19:38 +03:00
|
|
|
case PM_SUPER_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// super(foo)
|
|
|
|
// ^^^^^^^^^^
|
|
|
|
const pm_super_node_t *cast = (const pm_super_node_t *) node;
|
2023-10-31 18:19:38 +03:00
|
|
|
|
|
|
|
DECL_ANCHOR(args);
|
|
|
|
INIT_ANCHOR(args);
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN(ret, location, putself);
|
2023-10-31 18:19:38 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
int flags = 0;
|
|
|
|
struct rb_callinfo_kwarg *keywords = NULL;
|
|
|
|
int argc = pm_setup_args(cast->arguments, cast->block, &flags, &keywords, iseq, ret, scope_node, dummy_line_node);
|
2023-10-31 18:19:38 +03:00
|
|
|
flags |= VM_CALL_SUPER | VM_CALL_FCALL;
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
|
|
|
|
if (cast->block && PM_NODE_TYPE_P(cast->block, PM_BLOCK_NODE)) {
|
2024-02-03 03:29:50 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
2024-02-20 19:08:30 +03:00
|
|
|
pm_scope_node_init(cast->block, &next_scope_node, scope_node, parser);
|
2024-02-03 03:29:50 +03:00
|
|
|
parent_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
|
|
|
pm_scope_node_destroy(&next_scope_node);
|
2023-10-31 18:19:38 +03:00
|
|
|
}
|
|
|
|
|
2024-02-01 05:42:31 +03:00
|
|
|
if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN(args, location, splatkw);
|
2024-02-01 05:42:31 +03:00
|
|
|
}
|
|
|
|
|
2023-10-31 18:19:38 +03:00
|
|
|
ADD_SEQ(ret, args);
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN2(ret, location, invokesuper, new_callinfo(iseq, 0, argc, flags, keywords, parent_block != NULL), parent_block);
|
2023-10-31 18:19:38 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-10-31 18:19:38 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SYMBOL_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// :foo
|
|
|
|
// ^^^^
|
2023-09-07 17:59:51 +03:00
|
|
|
if (!popped) {
|
2024-02-12 23:48:23 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node);
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN1(ret, location, putobject, value);
|
2023-09-07 17:59:51 +03:00
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_TRUE_NODE:
|
2024-02-20 19:08:30 +03:00
|
|
|
// true
|
|
|
|
// ^^^^
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN1(ret, location, putobject, Qtrue);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
return;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_UNDEF_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// undef foo
|
|
|
|
// ^^^^^^^^^
|
|
|
|
const pm_undef_node_t *cast = (const pm_undef_node_t *) node;
|
|
|
|
const pm_node_list_t *names = &cast->names;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
for (size_t index = 0; index < names->size; index++) {
|
|
|
|
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(names->nodes[index]);
|
|
|
|
PUSH_SEND(ret, location, id_core_undef_method, INT2NUM(2));
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
if (index < names->size - 1) {
|
|
|
|
PUSH_INSN(ret, location, pop);
|
2023-10-24 23:08:50 +03:00
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_UNLESS_NODE: {
|
2024-02-20 19:37:54 +03:00
|
|
|
// unless foo; bar end
|
|
|
|
// ^^^^^^^^^^^^^^^^^^^
|
|
|
|
//
|
|
|
|
// bar unless foo
|
|
|
|
// ^^^^^^^^^^^^^^
|
|
|
|
const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
|
|
|
|
const pm_statements_node_t *consequent = NULL;
|
|
|
|
if (cast->consequent != NULL) {
|
|
|
|
consequent = ((const pm_else_node_t *) cast->consequent)->statements;
|
2023-10-20 18:03:02 +03:00
|
|
|
}
|
2023-08-30 00:17:08 +03:00
|
|
|
|
2024-02-20 19:37:54 +03:00
|
|
|
pm_compile_conditional(iseq, &location, consequent, (const pm_node_t *) cast->statements, cast->predicate, ret, popped, scope_node);
|
2023-08-30 00:17:08 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_UNTIL_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// until foo; bar end
|
|
|
|
// ^^^^^^^^^^^^^^^^^
|
|
|
|
//
|
|
|
|
// bar until foo
|
|
|
|
// ^^^^^^^^^^^^^
|
|
|
|
const pm_until_node_t *cast = (const pm_until_node_t *) node;
|
|
|
|
pm_compile_loop(iseq, &location, cast->base.flags, PM_UNTIL_NODE, cast->statements, cast->predicate, ret, popped, scope_node);
|
2023-10-24 21:24:36 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_WHILE_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// while foo; bar end
|
|
|
|
// ^^^^^^^^^^^^^^^^^^
|
|
|
|
//
|
|
|
|
// bar while foo
|
|
|
|
// ^^^^^^^^^^^^^
|
|
|
|
const pm_while_node_t *cast = (const pm_while_node_t *) node;
|
|
|
|
pm_compile_loop(iseq, &location, cast->base.flags, PM_WHILE_NODE, cast->statements, cast->predicate, ret, popped, scope_node);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_X_STRING_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// `foo`
|
|
|
|
// ^^^^^
|
|
|
|
const pm_x_string_node_t *cast = (const pm_x_string_node_t *) node;
|
2024-02-14 22:17:32 +03:00
|
|
|
VALUE value = parse_string_encoded(node, &cast->unescaped, parser);
|
2023-12-06 20:54:16 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN(ret, location, putself);
|
|
|
|
PUSH_INSN1(ret, location, putobject, value);
|
|
|
|
PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
2023-09-21 22:28:08 +03:00
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_YIELD_NODE: {
|
2024-02-20 19:08:30 +03:00
|
|
|
// yield
|
|
|
|
// ^^^^^
|
|
|
|
//
|
|
|
|
// yield 1
|
|
|
|
// ^^^^^^^
|
|
|
|
const pm_yield_node_t *cast = (const pm_yield_node_t *) node;
|
2023-11-07 16:10:23 +03:00
|
|
|
|
2024-02-08 18:28:05 +03:00
|
|
|
switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->type) {
|
|
|
|
case ISEQ_TYPE_TOP:
|
|
|
|
case ISEQ_TYPE_MAIN:
|
|
|
|
case ISEQ_TYPE_CLASS:
|
|
|
|
COMPILE_ERROR(ERROR_ARGS "Invalid yield");
|
|
|
|
return;
|
|
|
|
default: /* valid */;
|
|
|
|
}
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
int argc = 0;
|
2023-11-27 23:55:15 +03:00
|
|
|
int flags = 0;
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
struct rb_callinfo_kwarg *keywords = NULL;
|
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
if (cast->arguments) {
|
|
|
|
argc = pm_setup_args(cast->arguments, NULL, &flags, &keywords, iseq, ret, scope_node, dummy_line_node);
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2024-02-20 19:08:30 +03:00
|
|
|
PUSH_INSN1(ret, location, invokeblock, new_callinfo(iseq, 0, argc, flags, keywords, FALSE));
|
|
|
|
if (popped) PUSH_INSN(ret, location, pop);
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
|
|
|
int level = 0;
|
2024-02-20 19:08:30 +03:00
|
|
|
for (const rb_iseq_t *tmp_iseq = iseq; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++) {
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
tmp_iseq = ISEQ_BODY(tmp_iseq)->parent_iseq;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level > 0) access_outer_variables(iseq, level, rb_intern("yield"), true);
|
|
|
|
return;
|
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
default:
|
2023-09-27 19:39:53 +03:00
|
|
|
rb_raise(rb_eNotImpError, "node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-08-28 23:55:58 +03:00
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
/**
|
|
|
|
* This is the main entry-point into the prism compiler. It accepts the iseq
|
|
|
|
* that it should be compiling instruction into and a pointer to the scope node
|
|
|
|
* that it should be compiling. It returns the established instruction sequence.
|
|
|
|
* Note that this function could raise Ruby errors if it encounters compilation
|
|
|
|
* errors or if there is a bug in the compiler.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
|
|
|
|
{
|
|
|
|
DECL_ANCHOR(ret);
|
|
|
|
INIT_ANCHOR(ret);
|
|
|
|
|
|
|
|
pm_compile_node(iseq, (const pm_node_t *) node, ret, false, node);
|
|
|
|
|
|
|
|
CHECK(iseq_setup_insn(iseq, ret));
|
|
|
|
return iseq_setup(iseq, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free the internal memory associated with a pm_parse_result_t struct.
|
|
|
|
* Importantly this does not free the struct itself.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pm_parse_result_free(pm_parse_result_t *result)
|
|
|
|
{
|
|
|
|
if (result->parsed) {
|
|
|
|
pm_node_destroy(&result->parser, result->node.ast_node);
|
|
|
|
pm_scope_node_destroy(&result->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
pm_parser_free(&result->parser);
|
|
|
|
pm_string_free(&result->input);
|
|
|
|
pm_options_free(&result->options);
|
|
|
|
}
|
|
|
|
|
2024-02-06 21:53:58 +03:00
|
|
|
/**
|
2024-02-12 21:43:07 +03:00
|
|
|
* Check if the given source slice is valid UTF-8. The location represents the
|
|
|
|
* location of the error, but the slice of the source will include the content
|
|
|
|
* of all of the lines that the error touches, so we need to check those parts
|
|
|
|
* as well.
|
2024-02-06 21:53:58 +03:00
|
|
|
*/
|
|
|
|
static bool
|
2024-02-12 21:43:07 +03:00
|
|
|
pm_parse_input_error_utf8_p(const pm_parser_t *parser, const pm_location_t *location)
|
2024-02-06 21:53:58 +03:00
|
|
|
{
|
2024-02-14 22:17:32 +03:00
|
|
|
const size_t start_line = pm_newline_list_line_column(&parser->newline_list, location->start, 1).line;
|
|
|
|
const size_t end_line = pm_newline_list_line_column(&parser->newline_list, location->end, 1).line;
|
2024-02-12 21:43:07 +03:00
|
|
|
|
2024-02-14 22:17:32 +03:00
|
|
|
const uint8_t *start = parser->start + parser->newline_list.offsets[start_line - 1];
|
|
|
|
const uint8_t *end = ((end_line == parser->newline_list.size) ? parser->end : (parser->start + parser->newline_list.offsets[end_line]));
|
2024-02-06 21:53:58 +03:00
|
|
|
size_t width;
|
|
|
|
|
|
|
|
while (start < end) {
|
|
|
|
if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0) return false;
|
|
|
|
start += width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an error object from the given parser that contains as much
|
|
|
|
* information as possible about the errors that were encountered.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
pm_parse_input_error(const pm_parse_result_t *result)
|
|
|
|
{
|
|
|
|
const pm_diagnostic_t *head = (const pm_diagnostic_t *) result->parser.error_list.head;
|
|
|
|
bool valid_utf8 = true;
|
|
|
|
|
|
|
|
for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
|
|
|
|
// Any errors with the level PM_ERROR_LEVEL_ARGUMENT effectively take
|
|
|
|
// over as the only argument that gets raised. This is to allow priority
|
|
|
|
// messages that should be handled before anything else.
|
|
|
|
if (error->level == PM_ERROR_LEVEL_ARGUMENT) {
|
|
|
|
return rb_exc_new(rb_eArgError, error->message, strlen(error->message));
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is implicitly assumed that the error messages will be encodeable
|
|
|
|
// as UTF-8. Because of this, we can't include source examples that
|
|
|
|
// contain invalid byte sequences. So if any source examples include
|
|
|
|
// invalid UTF-8 byte sequences, we will skip showing source examples
|
|
|
|
// entirely.
|
2024-02-12 21:43:07 +03:00
|
|
|
if (valid_utf8 && !pm_parse_input_error_utf8_p(&result->parser, &error->location)) {
|
2024-02-06 21:53:58 +03:00
|
|
|
valid_utf8 = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pm_buffer_t buffer = { 0 };
|
|
|
|
pm_buffer_append_string(&buffer, "syntax errors found\n", 20);
|
|
|
|
|
|
|
|
if (valid_utf8) {
|
|
|
|
pm_parser_errors_format(&result->parser, &buffer, rb_stderr_tty_p());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const pm_string_t *filepath = &result->parser.filepath;
|
|
|
|
|
|
|
|
for (const pm_diagnostic_t *error = head; error != NULL; error = (pm_diagnostic_t *) error->node.next) {
|
|
|
|
if (error != head) pm_buffer_append_byte(&buffer, '\n');
|
2024-02-14 22:17:32 +03:00
|
|
|
pm_buffer_append_format(&buffer, "%.*s:%" PRIi32 ": %s", (int) pm_string_length(filepath), pm_string_source(filepath), (int32_t) pm_location_line_number(&result->parser, &error->location), error->message);
|
2024-02-06 21:53:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE error = rb_exc_new(rb_eSyntaxError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
|
|
|
|
pm_buffer_free(&buffer);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
/**
|
|
|
|
* Parse the parse result and raise a Ruby error if there are any syntax errors.
|
|
|
|
* It returns an error if one should be raised. It is assumed that the parse
|
|
|
|
* result object is zeroed out.
|
|
|
|
*/
|
2023-08-28 23:55:58 +03:00
|
|
|
static VALUE
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_parse_input(pm_parse_result_t *result, VALUE filepath)
|
2023-08-28 23:55:58 +03:00
|
|
|
{
|
2024-01-31 20:17:31 +03:00
|
|
|
// Set up the parser and parse the input.
|
|
|
|
pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
|
2024-02-16 22:23:00 +03:00
|
|
|
RB_GC_GUARD(filepath);
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
|
|
|
|
const pm_node_t *node = pm_parse(&result->parser);
|
|
|
|
|
|
|
|
// If there are errors, raise an appropriate error and free the result.
|
|
|
|
if (result->parser.error_list.size > 0) {
|
2024-02-06 21:53:58 +03:00
|
|
|
VALUE error = pm_parse_input_error(result);
|
2024-01-31 20:17:31 +03:00
|
|
|
|
|
|
|
// TODO: We need to set the backtrace.
|
|
|
|
// rb_funcallv(error, rb_intern("set_backtrace"), 1, &path);
|
2024-02-06 21:53:58 +03:00
|
|
|
return error;
|
2023-10-30 16:49:17 +03:00
|
|
|
}
|
2024-01-18 19:55:31 +03:00
|
|
|
|
2024-02-01 23:16:15 +03:00
|
|
|
// Emit all of the various warnings from the parse.
|
|
|
|
const pm_diagnostic_t *warning;
|
|
|
|
const char *warning_filepath = (const char *) pm_string_source(&result->parser.filepath);
|
|
|
|
|
|
|
|
for (warning = (pm_diagnostic_t *) result->parser.warning_list.head; warning != NULL; warning = (pm_diagnostic_t *) warning->node.next) {
|
2024-02-14 22:17:32 +03:00
|
|
|
int line = pm_location_line_number(&result->parser, &warning->location);
|
2024-02-01 23:16:15 +03:00
|
|
|
|
|
|
|
if (warning->level == PM_WARNING_LEVEL_VERBOSE) {
|
|
|
|
rb_compile_warning(warning_filepath, line, "%s", warning->message);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_compile_warn(warning_filepath, line, "%s", warning->message);
|
|
|
|
}
|
|
|
|
}
|
2024-01-31 20:17:31 +03:00
|
|
|
|
|
|
|
// Now set up the constant pool and intern all of the various constants into
|
|
|
|
// their corresponding IDs.
|
|
|
|
pm_scope_node_init(node, &result->node, NULL, &result->parser);
|
|
|
|
|
|
|
|
result->node.constants = calloc(result->parser.constant_pool.size, sizeof(ID));
|
|
|
|
rb_encoding *encoding = rb_enc_find(result->parser.encoding->name);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < result->parser.constant_pool.size; index++) {
|
|
|
|
pm_constant_t *constant = &result->parser.constant_pool.constants[index];
|
|
|
|
result->node.constants[index] = rb_intern3((const char *) constant->start, constant->length, encoding);
|
2024-01-18 19:55:31 +03:00
|
|
|
}
|
2023-10-30 16:49:17 +03:00
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
result->node.index_lookup_table = st_init_numtable();
|
|
|
|
pm_constant_id_list_t *locals = &result->node.locals;
|
|
|
|
for (size_t index = 0; index < locals->size; index++) {
|
|
|
|
st_insert(result->node.index_lookup_table, locals->ids[index], index);
|
|
|
|
}
|
2023-10-30 16:49:17 +03:00
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
// If we got here, this is a success and we can return Qnil to indicate that
|
|
|
|
// no error should be raised.
|
|
|
|
result->parsed = true;
|
2023-08-28 23:55:58 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
|
2024-01-31 23:19:02 +03:00
|
|
|
/**
|
|
|
|
* Returns an array of ruby String objects that represent the lines of the
|
|
|
|
* source file that the given parser parsed.
|
|
|
|
*/
|
|
|
|
static inline VALUE
|
|
|
|
pm_parse_file_script_lines(const pm_parser_t *parser)
|
|
|
|
{
|
|
|
|
const char *start = (const char *) parser->start;
|
|
|
|
const char *end = (const char *) parser->end;
|
|
|
|
|
|
|
|
rb_encoding *encoding = rb_enc_find(parser->encoding->name);
|
|
|
|
const pm_newline_list_t *newline_list = &parser->newline_list;
|
|
|
|
|
|
|
|
// If we end exactly on a newline, then there's no need to push on a final
|
|
|
|
// segment. If we don't, then we need to push on the last offset up to the
|
|
|
|
// end of the string.
|
|
|
|
size_t last_offset = newline_list->offsets[newline_list->size - 1];
|
|
|
|
bool last_push = start + last_offset != end;
|
|
|
|
|
|
|
|
// Create the ruby strings that represent the lines of the source.
|
|
|
|
VALUE lines = rb_ary_new_capa(newline_list->size - (last_push ? 0 : 1));
|
|
|
|
|
|
|
|
for (size_t index = 0; index < newline_list->size - 1; index++) {
|
|
|
|
size_t offset = newline_list->offsets[index];
|
|
|
|
size_t length = newline_list->offsets[index + 1] - offset;
|
|
|
|
|
|
|
|
rb_ary_push(lines, rb_enc_str_new(start + offset, length, encoding));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push on the last line if we need to.
|
|
|
|
if (last_push) {
|
|
|
|
rb_ary_push(lines, rb_enc_str_new(start + last_offset, end - (start + last_offset), encoding));
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
/**
|
|
|
|
* Parse the given filepath and store the resulting scope node in the given
|
|
|
|
* parse result struct. It returns a Ruby error if the file cannot be read or
|
|
|
|
* if it cannot be parsed properly. It is assumed that the parse result object
|
|
|
|
* is zeroed out.
|
|
|
|
*
|
|
|
|
* TODO: This should raise a better error when the file cannot be read.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
pm_parse_file(pm_parse_result_t *result, VALUE filepath)
|
|
|
|
{
|
|
|
|
if (!pm_string_mapped_init(&result->input, RSTRING_PTR(filepath))) {
|
2024-02-07 01:13:50 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
int e = rb_w32_map_errno(GetLastError());
|
|
|
|
#else
|
|
|
|
int e = errno;
|
|
|
|
#endif
|
|
|
|
|
2024-02-16 22:23:00 +03:00
|
|
|
VALUE err = rb_syserr_new(e, RSTRING_PTR(filepath));
|
|
|
|
RB_GC_GUARD(filepath);
|
|
|
|
return err;
|
2024-01-31 20:17:31 +03:00
|
|
|
}
|
|
|
|
|
2024-01-31 23:19:02 +03:00
|
|
|
VALUE error = pm_parse_input(result, filepath);
|
|
|
|
|
|
|
|
// If we're parsing a filepath, then we need to potentially support the
|
|
|
|
// SCRIPT_LINES__ constant, which can be a hash that has an array of lines
|
|
|
|
// of every read file.
|
|
|
|
ID id_script_lines = rb_intern("SCRIPT_LINES__");
|
|
|
|
|
|
|
|
if (rb_const_defined_at(rb_cObject, id_script_lines)) {
|
|
|
|
VALUE script_lines = rb_const_get_at(rb_cObject, id_script_lines);
|
|
|
|
|
|
|
|
if (RB_TYPE_P(script_lines, T_HASH)) {
|
|
|
|
rb_hash_aset(script_lines, filepath, pm_parse_file_script_lines(&result->parser));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
2024-01-31 20:17:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the given source that corresponds to the given filepath and store the
|
|
|
|
* resulting scope node in the given parse result struct. This function could
|
|
|
|
* potentially raise a Ruby error. It is assumed that the parse result object is
|
|
|
|
* zeroed out.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath)
|
|
|
|
{
|
|
|
|
pm_string_constant_init(&result->input, RSTRING_PTR(source), RSTRING_LEN(source));
|
2024-02-14 05:38:30 +03:00
|
|
|
|
|
|
|
rb_encoding *encoding = rb_enc_get(source);
|
|
|
|
pm_options_encoding_set(&result->options, rb_enc_name(encoding));
|
|
|
|
|
2024-01-31 20:17:31 +03:00
|
|
|
return pm_parse_input(result, filepath);
|
|
|
|
}
|
|
|
|
|
Compile more YARP node types (#8322)
* Add several more node simple types to YARP's compiler:
Nodes include: DefinedNode, EmbeddedStatementsNode,
LocalVariableReadNode, LocalVariableWriteNode, MultiWriteNode,
OptionalParameterNode, SplatNode, YieldNode
* Add AssocSplatNode, RangeNode
* Add RangeNode, other helpers for future nodes
* Add ArrayNode, HashNode, static literal helpers
* Add branch conditionals
* Add IfNode, UnlessNode
* Add ScopeNode
* NEW_ISEQ and NEW_CHILD_ISEQ implemented for YARP
* Add nodes that depend on ScopeNode
* Addressed PR comments
2023-08-29 23:13:15 +03:00
|
|
|
#undef NEW_ISEQ
|
|
|
|
#define NEW_ISEQ OLD_ISEQ
|
|
|
|
|
|
|
|
#undef NEW_CHILD_ISEQ
|
|
|
|
#define NEW_CHILD_ISEQ OLD_CHILD_ISEQ
|