2023-09-27 19:39:53 +03:00
|
|
|
#include "prism.h"
|
2023-08-28 23:55:58 +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
|
|
|
#define OLD_ISEQ NEW_ISEQ
|
|
|
|
#undef NEW_ISEQ
|
|
|
|
|
|
|
|
#define NEW_ISEQ(node, name, type, line_no) \
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_new_child_iseq(iseq, (node), parser, 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) \
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_new_child_iseq(iseq, (node), parser, 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) \
|
|
|
|
pm_compile_node(iseq, (node), ret, src, popped, scope_node)
|
|
|
|
|
|
|
|
#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
|
2023-11-02 01:26:40 +03:00
|
|
|
pm_compile_node(iseq, (node), _ret, src, popped, scope_node)
|
2023-09-08 23:33:05 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_COMPILE_POPPED(node) \
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_node(iseq, (node), ret, src, 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) \
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_node(iseq, (node), ret, src, false, scope_node)
|
2023-09-08 23:33:05 +03:00
|
|
|
|
2023-10-24 23:08:50 +03:00
|
|
|
#define PM_POP \
|
|
|
|
ADD_INSN(ret, &dummy_line_node, pop);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_POP_IF_POPPED \
|
2023-10-24 23:08:50 +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 \
|
2023-10-24 23:08:50 +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 \
|
|
|
|
ADD_INSN(ret, &dummy_line_node, dup);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_DUP_UNLESS_POPPED \
|
2023-10-26 18:03:18 +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 \
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putself);
|
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
#define PM_PUTNIL \
|
|
|
|
ADD_INSN(ret, &dummy_line_node, putnil);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
#define PM_PUTNIL_UNLESS_POPPED \
|
2023-10-23 16:46:37 +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 \
|
|
|
|
ADD_INSN(ret, &dummy_line_node, swap);
|
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
#define PM_SWAP_UNLESS_POPPED \
|
|
|
|
if (!popped) PM_SWAP;
|
|
|
|
|
2023-12-06 17:45:04 +03:00
|
|
|
#define PM_NOP \
|
|
|
|
ADD_INSN(ret, &dummy_line_node, nop);
|
|
|
|
|
2023-10-30 21:20:49 +03:00
|
|
|
/**
|
|
|
|
* We're using the top most bit of a pm_constant_id_t as a tag to represent an
|
|
|
|
* anonymous local. When a child iseq is created and needs access to a value
|
|
|
|
* that has yet to be defined, or is defined by the parent node's iseq. This can
|
|
|
|
* be added to it's local table and then handled accordingly when compiling the
|
|
|
|
* scope node associated with the child iseq.
|
|
|
|
*
|
|
|
|
* See the compilation process for PM_FOR_NODE: as an example, where the
|
|
|
|
* variable referenced inside the StatementsNode is defined as part of the top
|
|
|
|
* level ForLoop node.
|
|
|
|
*/
|
2023-10-20 12:59:02 +03:00
|
|
|
#define TEMP_CONSTANT_IDENTIFIER ((pm_constant_id_t)(1 << 31))
|
|
|
|
|
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_t *
|
2023-10-19 00:53:02 +03:00
|
|
|
pm_iseq_new_with_opt(pm_scope_node_t *scope_node, pm_parser_t *parser, VALUE name, VALUE path, VALUE realpath,
|
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 first_lineno, const rb_iseq_t *parent, int isolated_depth,
|
|
|
|
enum rb_iseq_type type, const rb_compile_option_t *option);
|
|
|
|
|
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
|
|
|
{
|
2023-12-12 21:40:05 +03:00
|
|
|
char *start = (char *) node->base.location.start;
|
|
|
|
char *end = (char *) node->base.location.end;
|
2023-09-22 21:53:55 +03:00
|
|
|
|
2023-09-08 22:33:51 +03:00
|
|
|
size_t length = end - start;
|
2023-09-22 21:53:55 +03:00
|
|
|
int base = -10;
|
2023-09-08 22:33:51 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
switch (node->base.flags & (PM_INTEGER_BASE_FLAGS_BINARY | PM_INTEGER_BASE_FLAGS_DECIMAL | PM_INTEGER_BASE_FLAGS_OCTAL | PM_INTEGER_BASE_FLAGS_HEXADECIMAL)) {
|
|
|
|
case PM_INTEGER_BASE_FLAGS_BINARY:
|
2023-09-22 21:53:55 +03:00
|
|
|
base = 2;
|
|
|
|
break;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTEGER_BASE_FLAGS_DECIMAL:
|
2023-09-22 21:53:55 +03:00
|
|
|
base = 10;
|
|
|
|
break;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTEGER_BASE_FLAGS_OCTAL:
|
2023-09-22 21:53:55 +03:00
|
|
|
base = 8;
|
|
|
|
break;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTEGER_BASE_FLAGS_HEXADECIMAL:
|
2023-09-22 21:53:55 +03:00
|
|
|
base = 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_bug("Unexpected integer base");
|
|
|
|
}
|
|
|
|
|
2023-12-12 21:40:05 +03:00
|
|
|
return rb_int_parse_cstr(start, length, &end, NULL, base, RB_INT_PARSE_DEFAULT);
|
2023-09-08 22:33:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2023-09-27 19:39:53 +03:00
|
|
|
parse_float(const pm_node_t *node)
|
2023-09-08 22:33:51 +03:00
|
|
|
{
|
2023-08-30 21:32:52 +03:00
|
|
|
const uint8_t *start = node->location.start;
|
|
|
|
const uint8_t *end = node->location.end;
|
2023-08-29 19:27:00 +03:00
|
|
|
size_t length = end - start;
|
|
|
|
|
|
|
|
char *buffer = malloc(length + 1);
|
|
|
|
memcpy(buffer, start, length);
|
|
|
|
|
|
|
|
buffer[length] = '\0';
|
2023-09-08 22:33:51 +03:00
|
|
|
VALUE number = DBL2NUM(rb_cstr_to_dbl(buffer, 0));
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
2023-09-08 22:33:51 +03:00
|
|
|
static VALUE
|
2023-09-27 19:39:53 +03:00
|
|
|
parse_rational(const pm_node_t *node)
|
2023-09-08 22:33:51 +03:00
|
|
|
{
|
|
|
|
const uint8_t *start = node->location.start;
|
|
|
|
const uint8_t *end = node->location.end - 1;
|
|
|
|
size_t length = end - start;
|
|
|
|
|
|
|
|
VALUE res;
|
2023-09-27 19:39:53 +03:00
|
|
|
if (PM_NODE_TYPE_P(((pm_rational_node_t *)node)->numeric, PM_FLOAT_NODE)) {
|
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);
|
|
|
|
|
|
|
|
VALUE v = rb_cstr_to_inum(buffer, 10, false);
|
|
|
|
res = rb_rational_new(v, rb_int_positive_pow(10, fraclen));
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
else {
|
2023-09-27 19:39:53 +03:00
|
|
|
RUBY_ASSERT(PM_NODE_TYPE_P(((pm_rational_node_t *)node)->numeric, PM_INTEGER_NODE));
|
2023-09-08 22:33:51 +03:00
|
|
|
VALUE number = rb_int_parse_cstr((const char *)start, length, NULL, NULL, -10, RB_INT_PARSE_DEFAULT);
|
|
|
|
res = rb_rational_raw(number, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
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: {
|
2023-09-08 22:33:51 +03:00
|
|
|
imaginary_part = parse_float(node->numeric);
|
|
|
|
break;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_INTEGER_NODE: {
|
|
|
|
imaginary_part = parse_integer((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: {
|
2023-09-08 22:33:51 +03:00
|
|
|
imaginary_part = parse_rational(node->numeric);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
rb_bug("Unexpected numeric type on imaginary number");
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
parse_string_encoded(const pm_node_t *node, const pm_string_t *string, const pm_parser_t *parser) {
|
|
|
|
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
|
2023-10-19 23:50:21 +03:00
|
|
|
parse_symbol(const uint8_t *start, const uint8_t *end, 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_intern3((const char *) start, end - start, enc);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline ID
|
2023-10-19 23:50:21 +03:00
|
|
|
parse_string_symbol(pm_string_t *string, pm_parser_t *parser)
|
2023-09-27 19:39:53 +03:00
|
|
|
{
|
|
|
|
const uint8_t *start = pm_string_source(string);
|
2023-10-19 23:50:21 +03:00
|
|
|
return parse_symbol(start, start + pm_string_length(string), parser);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 22:00:01 +03:00
|
|
|
static inline ID
|
2023-10-19 23:50:21 +03:00
|
|
|
parse_location_symbol(pm_location_t *location, pm_parser_t *parser)
|
2023-10-16 22:00:01 +03:00
|
|
|
{
|
2023-10-19 23:50:21 +03:00
|
|
|
return parse_symbol(location->start, location->end, parser);
|
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 *
|
|
|
|
pm_reg_enc(const pm_regular_expression_node_t *node, const pm_parser_t *parser) {
|
|
|
|
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
|
|
|
|
pm_new_regex(pm_regular_expression_node_t * cast, const pm_parser_t * parser) {
|
|
|
|
VALUE regex_str = parse_string(&cast->unescaped, parser);
|
|
|
|
rb_encoding * enc = pm_reg_enc(cast, parser);
|
|
|
|
|
|
|
|
return rb_enc_reg_new(RSTRING_PTR(regex_str), RSTRING_LEN(regex_str), enc, pm_reg_flags((const pm_node_t *)cast));
|
|
|
|
}
|
|
|
|
|
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
|
2023-10-19 23:50:21 +03:00
|
|
|
pm_static_literal_value(const pm_node_t *node, pm_scope_node_t *scope_node, pm_parser_t *parser)
|
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.
|
|
|
|
assert(pm_static_literal_p(node));
|
|
|
|
|
|
|
|
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++) {
|
2023-10-19 23:50:21 +03:00
|
|
|
rb_ary_push(value, pm_static_literal_value(elements->nodes[index], scope_node, parser));
|
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:
|
2023-09-18 16:29:52 +03:00
|
|
|
return parse_float(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++) {
|
|
|
|
assert(PM_NODE_TYPE_P(elements->nodes[index], PM_ASSOC_NODE));
|
|
|
|
pm_assoc_node_t *cast = (pm_assoc_node_t *) elements->nodes[index];
|
2023-10-19 23:50:21 +03:00
|
|
|
VALUE pair[2] = { pm_static_literal_value(cast->key, scope_node, parser), pm_static_literal_value(cast->value, scope_node, parser) };
|
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:
|
|
|
|
return parse_integer((pm_integer_node_t *) 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
|
|
|
return Qnil;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_RATIONAL_NODE:
|
2023-09-18 16:29:52 +03:00
|
|
|
return parse_rational(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;
|
|
|
|
|
2023-10-24 20:17:18 +03:00
|
|
|
return pm_new_regex(cast, parser);
|
2023-09-28 22:19:56 +03:00
|
|
|
}
|
|
|
|
case PM_SOURCE_ENCODING_NODE: {
|
2023-12-06 20:54:16 +03:00
|
|
|
rb_encoding *encoding = rb_find_encoding(rb_str_new_cstr(scope_node->parser->encoding->name));
|
2023-09-28 22:19:56 +03:00
|
|
|
if (!encoding) rb_bug("Encoding not found!");
|
|
|
|
return rb_enc_from_encoding(encoding);
|
|
|
|
}
|
|
|
|
case PM_SOURCE_FILE_NODE: {
|
|
|
|
pm_source_file_node_t *cast = (pm_source_file_node_t *)node;
|
2023-10-19 23:50:21 +03:00
|
|
|
return cast->filepath.length ? parse_string(&cast->filepath, parser) : rb_fstring_lit("<compiled>");
|
2023-09-28 22:19:56 +03:00
|
|
|
}
|
2023-10-23 18:29:48 +03:00
|
|
|
case PM_SOURCE_LINE_NODE: {
|
|
|
|
int source_line = (int) pm_newline_list_line_column(&scope_node->parser->newline_list, node->location.start).line;
|
|
|
|
// Ruby treats file lines as 1-indexed
|
|
|
|
// TODO: Incorporate options which allow for passing a line number
|
|
|
|
source_line += 1;
|
|
|
|
return INT2FIX(source_line);
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_STRING_NODE:
|
2023-10-19 23:50:21 +03:00
|
|
|
return parse_string(&((pm_string_node_t *) node)->unescaped, parser);
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SYMBOL_NODE:
|
2023-10-19 23:50:21 +03:00
|
|
|
return ID2SYM(parse_string_symbol(&((pm_symbol_node_t *) node)->unescaped, 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:
|
2023-09-07 17:59:51 +03:00
|
|
|
rb_raise(rb_eArgError, "Don't have a literal value for this type");
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-10-17 01:36:25 +03:00
|
|
|
LABEL *then_label, LABEL *else_label, const uint8_t *src, 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
|
2023-10-23 20:49:39 +03:00
|
|
|
pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond,
|
2023-10-17 01:36:25 +03:00
|
|
|
LABEL *then_label, LABEL *else_label, const uint8_t *src, 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-17 01:36:25 +03:00
|
|
|
pm_parser_t *parser = scope_node->parser;
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_newline_list_t newline_list = parser->newline_list;
|
|
|
|
int lineno = (int)pm_newline_list_line_column(&newline_list, cond->location.start).line;
|
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;
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, src, 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) {
|
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
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_LABEL(seq, label);
|
|
|
|
}
|
|
|
|
ADD_SEQ(ret, seq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-23 20:49:39 +03:00
|
|
|
static void pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, 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
|
|
|
|
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, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-10-27 00:54:38 +03:00
|
|
|
void pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *defined_node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, int lineno, bool in_condition);
|
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,
|
2023-10-17 01:36:25 +03:00
|
|
|
LABEL *then_label, LABEL *else_label, const uint8_t *src, 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-17 01:36:25 +03:00
|
|
|
pm_parser_t *parser = scope_node->parser;
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_newline_list_t newline_list = parser->newline_list;
|
|
|
|
int lineno = (int) pm_newline_list_line_column(&newline_list, cond->location.start).line;
|
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;
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_logical(iseq, ret, and_node->left, NULL, else_label, src, 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;
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_logical(iseq, ret, or_node->left, then_label, NULL, src, 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:
|
|
|
|
pm_compile_flip_flop((pm_flip_flop_node_t *)cond, else_label, then_label, iseq, lineno, ret, src, popped, scope_node);
|
|
|
|
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;
|
2023-10-27 00:54:38 +03:00
|
|
|
pm_compile_defined_expr(iseq, defined_node->value, ret, src, 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: {
|
|
|
|
DECL_ANCHOR(cond_seq);
|
|
|
|
INIT_ANCHOR(cond_seq);
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_node(iseq, cond, cond_seq, src, 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
|
|
|
ADD_SEQ(ret, cond_seq);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-10-23 20:49:39 +03:00
|
|
|
pm_compile_if(rb_iseq_t *iseq, const int line, pm_statements_node_t *node_body, pm_node_t *node_else, pm_node_t *predicate, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
2023-09-27 19:39:53 +03:00
|
|
|
{
|
2023-09-21 22:28:08 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(line, line);
|
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(cond_seq);
|
|
|
|
|
|
|
|
LABEL *then_label, *else_label, *end_label;
|
|
|
|
|
|
|
|
INIT_ANCHOR(cond_seq);
|
|
|
|
then_label = NEW_LABEL(line);
|
|
|
|
else_label = NEW_LABEL(line);
|
|
|
|
end_label = 0;
|
|
|
|
|
2023-10-20 18:03:02 +03:00
|
|
|
pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, src, 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
|
|
|
ADD_SEQ(ret, cond_seq);
|
|
|
|
|
|
|
|
if (then_label->refcnt) {
|
|
|
|
ADD_LABEL(ret, then_label);
|
|
|
|
|
|
|
|
DECL_ANCHOR(then_seq);
|
|
|
|
INIT_ANCHOR(then_seq);
|
|
|
|
if (node_body) {
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_node(iseq, (pm_node_t *)node_body, then_seq, src, popped, scope_node);
|
2023-09-28 17:18:43 +03:00
|
|
|
} else {
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_PUTNIL_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
|
|
|
}
|
|
|
|
|
|
|
|
if (else_label->refcnt) {
|
|
|
|
end_label = NEW_LABEL(line);
|
2023-09-21 22:28:08 +03:00
|
|
|
ADD_INSNL(then_seq, &dummy_line_node, jump, end_label);
|
2023-10-20 18:03:02 +03:00
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN(then_seq, &dummy_line_node, 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
|
|
|
}
|
|
|
|
ADD_SEQ(ret, then_seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (else_label->refcnt) {
|
|
|
|
ADD_LABEL(ret, else_label);
|
|
|
|
|
|
|
|
DECL_ANCHOR(else_seq);
|
|
|
|
INIT_ANCHOR(else_seq);
|
|
|
|
if (node_else) {
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_node(iseq, (pm_node_t *)node_else, else_seq, src, 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
|
|
|
}
|
|
|
|
else {
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_PUTNIL_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
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEQ(ret, else_seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end_label) {
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:17:08 +03:00
|
|
|
static void
|
2023-10-23 20:49:39 +03:00
|
|
|
pm_compile_while(rb_iseq_t *iseq, int lineno, pm_node_flags_t flags, enum pm_node_type type, pm_statements_node_t *statements, pm_node_t *predicate, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
2023-08-30 00:17:08 +03:00
|
|
|
{
|
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
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
|
2023-08-31 00:27:08 +03:00
|
|
|
LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(lineno); /* next */
|
|
|
|
LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(lineno); /* redo */
|
|
|
|
LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(lineno); /* break */
|
2023-08-30 00:17:08 +03:00
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *adjust_label = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
LABEL *next_catch_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *tmp_label = NULL;
|
|
|
|
|
|
|
|
// begin; end while true
|
2023-09-27 19:39:53 +03:00
|
|
|
if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
|
2023-08-31 00:27:08 +03:00
|
|
|
tmp_label = NEW_LABEL(lineno);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, tmp_label);
|
2023-08-30 00:17:08 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// while true; end
|
2023-08-31 00:27:08 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, next_label);
|
2023-08-30 00:17:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_LABEL(ret, adjust_label);
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-08-30 00:17:08 +03:00
|
|
|
ADD_LABEL(ret, next_catch_label);
|
2023-10-24 23:08:50 +03:00
|
|
|
PM_POP;
|
2023-08-30 00:17:08 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, next_label);
|
|
|
|
if (tmp_label) ADD_LABEL(ret, tmp_label);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, redo_label);
|
|
|
|
if (statements) {
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_POPPED((pm_node_t *)statements);
|
2023-08-30 00:17:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_LABEL(ret, next_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
if (type == PM_WHILE_NODE) {
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, src, popped, scope_node);
|
2023-09-27 19:39:53 +03:00
|
|
|
} else if (type == PM_UNTIL_NODE) {
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, src, popped, scope_node);
|
2023-08-30 00:17:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
ADD_ADJUST_RESTORE(ret, adjust_label);
|
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-08-30 00:17:08 +03:00
|
|
|
|
|
|
|
ADD_LABEL(ret, break_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-08-30 00:17:08 +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);
|
|
|
|
|
|
|
|
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-09-08 17:40:07 +03:00
|
|
|
static void
|
2023-11-14 03:03:24 +03:00
|
|
|
pm_interpolated_node_compile(pm_node_list_t *parts, rb_iseq_t *iseq, NODE dummy_line_node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, pm_parser_t *parser)
|
2023-09-08 17:40:07 +03:00
|
|
|
{
|
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) {
|
|
|
|
for (size_t index = 0; index < parts_size; index++) {
|
2023-11-14 03:03:24 +03:00
|
|
|
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)) {
|
|
|
|
pm_string_node_t *string_node = (pm_string_node_t *) part;
|
2023-10-19 23:50:21 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, parse_string(&string_node->unescaped, parser));
|
2023-09-08 17:40:07 +03:00
|
|
|
}
|
|
|
|
else {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
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
|
|
|
|
// according to the depth of the local
|
2023-11-20 23:40:35 +03:00
|
|
|
static int
|
2023-11-30 14:05:26 +03:00
|
|
|
pm_lookup_local_index_any_scope(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
|
2023-11-20 23:40:35 +03:00
|
|
|
{
|
|
|
|
if (!scope_node) {
|
|
|
|
// We have recursed up all scope nodes
|
|
|
|
// and have not found the local yet
|
|
|
|
rb_bug("This local does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
st_data_t local_index;
|
|
|
|
|
|
|
|
if (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
|
|
|
|
// Local does not exist at this level, continue recursing up
|
2023-11-30 14:05:26 +03:00
|
|
|
return pm_lookup_local_index_any_scope(iseq, scope_node->previous, constant_id);
|
2023-11-20 23:40:35 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
return scope_node->local_table_for_iseq_size - (int)local_index;
|
2023-11-20 23:40:35 +03:00
|
|
|
}
|
|
|
|
|
2023-09-02 00:20:03 +03:00
|
|
|
static int
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_lookup_local_index(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
|
2023-09-02 00:20:03 +03:00
|
|
|
{
|
|
|
|
st_data_t local_index;
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
if (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
|
2023-09-02 00:20:03 +03:00
|
|
|
rb_bug("This local does not exist");
|
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
return scope_node->local_table_for_iseq_size - (int)local_index;
|
2023-09-02 00:20:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-11-30 14:05:26 +03:00
|
|
|
pm_lookup_local_index_with_depth(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id, uint32_t depth)
|
2023-09-02 00:20:03 +03:00
|
|
|
{
|
2023-11-30 14:05:26 +03:00
|
|
|
for(uint32_t i = 0; i < depth; i++) {
|
2023-10-17 01:36:25 +03:00
|
|
|
scope_node = scope_node->previous;
|
2023-09-02 00:20:03 +03:00
|
|
|
iseq = (rb_iseq_t *)ISEQ_BODY(iseq)->parent_iseq;
|
|
|
|
}
|
|
|
|
|
2023-11-30 14:05:26 +03:00
|
|
|
return pm_lookup_local_index_any_scope(iseq, scope_node, constant_id);
|
2023-09-02 00:20:03 +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
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_constant_id_lookup(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) {
|
2023-12-06 18:05:14 +03:00
|
|
|
rb_bug("[PRISM] 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 *
|
2023-10-18 21:18:37 +03:00
|
|
|
pm_new_child_iseq(rb_iseq_t *iseq, pm_scope_node_t node, pm_parser_t *parser,
|
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 name, const rb_iseq_t *parent, enum rb_iseq_type type, int line_no)
|
|
|
|
{
|
|
|
|
debugs("[new_child_iseq]> ---------------------------------------\n");
|
|
|
|
int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
|
2023-10-19 00:53:02 +03:00
|
|
|
rb_iseq_t * ret_iseq = pm_iseq_new_with_opt(&node, parser, 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
|
2023-10-23 20:49:39 +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, const uint8_t * src, 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:34:03 +03:00
|
|
|
static void
|
2023-10-25 18:40:30 +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, LINK_ANCHOR *const ret, rb_iseq_t *iseq, int lineno, const uint8_t * src, bool popped, pm_scope_node_t *scope_node)
|
2023-10-25 18:34:03 +03:00
|
|
|
{
|
|
|
|
LABEL *call_end_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
int flag = 0;
|
|
|
|
|
|
|
|
if (PM_NODE_TYPE_P(receiver, PM_SELF_NODE)) {
|
|
|
|
flag = VM_CALL_FCALL;
|
|
|
|
}
|
|
|
|
|
2023-11-02 21:07:00 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(receiver);
|
2023-10-25 18:34:03 +03:00
|
|
|
|
|
|
|
ID write_name_id = pm_constant_id_lookup(scope_node, write_name);
|
|
|
|
ID read_name_id = pm_constant_id_lookup(scope_node, read_name);
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-10-25 18:34:03 +03:00
|
|
|
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, read_name_id, INT2FIX(0), INT2FIX(flag));
|
|
|
|
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
|
|
|
|
|
|
|
if (and_node) {
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, call_end_label);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// or_node
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, call_end_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
PM_POP_UNLESS_POPPED;
|
|
|
|
|
2023-11-02 21:07:00 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(value);
|
2023-10-25 18:34:03 +03:00
|
|
|
if (!popped) {
|
|
|
|
PM_SWAP;
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
}
|
|
|
|
ID aid = rb_id_attrset(write_name_id);
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, aid, INT2FIX(1), INT2FIX(flag));
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, end_label);
|
|
|
|
ADD_LABEL(ret, call_end_label);
|
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
PM_SWAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_LABEL(ret, end_label);
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_POP;
|
2023-10-25 18:34:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-27 23:55:15 +03:00
|
|
|
static void
|
|
|
|
pm_compile_index_write_nodes_add_send(bool popped, LINK_ANCHOR *const ret, rb_iseq_t *iseq, NODE dummy_line_node, VALUE argc, int flag, int block_offset)
|
|
|
|
{
|
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, FIXNUM_INC(argc, 2 + block_offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag & VM_CALL_ARGS_SPLAT) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(1));
|
|
|
|
if (block_offset > 0) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, dupn, INT2FIX(3));
|
|
|
|
PM_SWAP;
|
|
|
|
PM_POP;
|
|
|
|
}
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
if (block_offset > 0) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(3));
|
|
|
|
PM_POP;
|
|
|
|
}
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idASET, argc, INT2FIX(flag));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (block_offset > 0) {
|
|
|
|
PM_SWAP;
|
|
|
|
}
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idASET, FIXNUM_INC(argc, 1), INT2FIX(flag));
|
|
|
|
}
|
|
|
|
|
|
|
|
PM_POP;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-27 19:12:24 +03:00
|
|
|
static int
|
|
|
|
pm_setup_args(pm_arguments_node_t *arguments_node, int *flags, struct rb_callinfo_kwarg **kw_arg, rb_iseq_t *iseq, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, pm_parser_t *parser)
|
|
|
|
{
|
|
|
|
int orig_argc = 0;
|
|
|
|
if (arguments_node == NULL) {
|
|
|
|
if (*flags & VM_CALL_FCALL) {
|
|
|
|
*flags |= VM_CALL_VCALL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pm_node_list_t arguments_node_list = arguments_node->arguments;
|
|
|
|
|
|
|
|
bool has_keyword_splat = (arguments_node->base.flags & PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
|
|
|
|
bool has_splat = false;
|
|
|
|
|
|
|
|
// 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)) {
|
|
|
|
// A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
|
|
|
|
case PM_KEYWORD_HASH_NODE: {
|
|
|
|
pm_keyword_hash_node_t *keyword_arg = (pm_keyword_hash_node_t *)argument;
|
|
|
|
size_t len = keyword_arg->elements.size;
|
|
|
|
|
|
|
|
if (has_keyword_splat) {
|
|
|
|
int cur_hash_size = 0;
|
|
|
|
orig_argc++;
|
|
|
|
|
|
|
|
bool new_hash_emitted = false;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
pm_node_t *cur_node = keyword_arg->elements.nodes[i];
|
|
|
|
|
|
|
|
pm_node_type_t cur_type = PM_NODE_TYPE(cur_node);
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(cur_node)) {
|
|
|
|
case PM_ASSOC_NODE: {
|
|
|
|
pm_assoc_node_t *assoc = (pm_assoc_node_t *)cur_node;
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(assoc->key);
|
|
|
|
PM_COMPILE_NOT_POPPED(assoc->value);
|
|
|
|
cur_hash_size++;
|
|
|
|
|
|
|
|
// If we're at the last keyword arg, or the last assoc node of this "set",
|
|
|
|
// then we want to either construct a newhash or merge onto previous hashes
|
|
|
|
if (i == (len - 1) || !PM_NODE_TYPE_P(keyword_arg->elements.nodes[i + 1], cur_type)) {
|
|
|
|
if (new_hash_emitted) {
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_ptr, INT2FIX(cur_hash_size * 2 + 1));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(cur_hash_size * 2));
|
|
|
|
cur_hash_size = 0;
|
|
|
|
new_hash_emitted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_ASSOC_SPLAT_NODE: {
|
|
|
|
if (len > 1) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
if (i == 0) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(0));
|
|
|
|
new_hash_emitted = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_SWAP;
|
|
|
|
}
|
2023-11-27 23:55:15 +03:00
|
|
|
|
|
|
|
*flags |= VM_CALL_KW_SPLAT_MUT;
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pm_assoc_splat_node_t *assoc_splat = (pm_assoc_splat_node_t *)cur_node;
|
|
|
|
PM_COMPILE_NOT_POPPED(assoc_splat->value);
|
|
|
|
|
2023-11-27 23:55:15 +03:00
|
|
|
*flags |= VM_CALL_KW_SPLAT;
|
2023-11-27 19:12:24 +03:00
|
|
|
|
|
|
|
if (len > 1) {
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_kwd, INT2FIX(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((i < len - 1) && !PM_NODE_TYPE_P(keyword_arg->elements.nodes[i + 1], cur_type)) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
PM_SWAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_hash_size = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
rb_bug("Unknown type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2023-12-07 02:36:00 +03:00
|
|
|
// We need to first figure out if all elements of the KeywordHashNode are AssocNodes
|
|
|
|
// with static literal keys.
|
2023-12-13 02:37:50 +03:00
|
|
|
bool all_keys_static_literals = PM_NODE_FLAG_P(keyword_arg, PM_KEYWORD_HASH_NODE_FLAGS_STATIC_KEYS);
|
2023-12-07 02:36:00 +03:00
|
|
|
|
|
|
|
if (all_keys_static_literals) {
|
|
|
|
// If they are all static literal keys then we can pass them as keyword arguments.
|
|
|
|
*kw_arg = rb_xmalloc_mul_add(len, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
|
|
|
|
*flags = VM_CALL_KWARG;
|
|
|
|
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;
|
|
|
|
keywords[i] = pm_static_literal_value(key, scope_node, parser);
|
|
|
|
PM_COMPILE_NOT_POPPED(assoc->value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If they aren't all static literal keys then we need to construct a new hash
|
|
|
|
// and pass that as an argument.
|
|
|
|
orig_argc++;
|
|
|
|
*flags |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_SPLAT_NODE: {
|
|
|
|
*flags |= VM_CALL_ARGS_SPLAT;
|
|
|
|
pm_splat_node_t *splat_node = (pm_splat_node_t *)argument;
|
|
|
|
if (splat_node->expression) {
|
|
|
|
orig_argc++;
|
|
|
|
PM_COMPILE_NOT_POPPED(splat_node->expression);
|
|
|
|
}
|
|
|
|
|
2023-11-30 20:57:49 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qfalse);
|
2023-11-27 19:12:24 +03:00
|
|
|
|
|
|
|
has_splat = true;
|
|
|
|
post_splat_counter = 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_FORWARDING_ARGUMENTS_NODE: {
|
|
|
|
orig_argc++;
|
|
|
|
*flags |= VM_CALL_ARGS_BLOCKARG | VM_CALL_ARGS_SPLAT;
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, 3, 0);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, RBOOL(arguments_node_list.size > 1));
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getblockparamproxy, INT2FIX(4), INT2FIX(0));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
orig_argc++;
|
|
|
|
post_splat_counter++;
|
|
|
|
PM_COMPILE_NOT_POPPED(argument);
|
|
|
|
|
|
|
|
if (has_splat) {
|
|
|
|
// If the next node starts the keyword section of parameters
|
|
|
|
if ((index < arguments_node_list.size - 1) && PM_NODE_TYPE_P(arguments_node_list.nodes[index + 1], PM_KEYWORD_HASH_NODE)) {
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(post_splat_counter));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qfalse);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
}
|
|
|
|
// If it's the final node
|
|
|
|
else if (index == arguments_node_list.size - 1) {
|
2023-11-29 22:58:26 +03:00
|
|
|
if (post_splat_counter > 1) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(post_splat_counter));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qfalse);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(post_splat_counter));
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
}
|
|
|
|
orig_argc = 1;
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return orig_argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pm_compile_index_and_or_write_node(bool and_node, pm_node_t *receiver, pm_node_t *value, pm_arguments_node_t *arguments, pm_node_t *block, LINK_ANCHOR *const ret, rb_iseq_t *iseq, int lineno, const uint8_t * src, bool popped, pm_scope_node_t *scope_node, pm_parser_t *parser)
|
|
|
|
{
|
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(receiver);
|
|
|
|
|
|
|
|
int flag = 0;
|
|
|
|
int argc_int = 0;
|
|
|
|
|
|
|
|
if (arguments) {
|
2023-11-27 22:07:27 +03:00
|
|
|
// Get any arguments, and set the appropriate values for flag
|
|
|
|
argc_int = pm_setup_args(arguments, &flag, NULL, iseq, ret, src, popped, scope_node, dummy_line_node, parser);
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE argc = INT2FIX(argc_int);
|
2023-11-27 22:07:27 +03:00
|
|
|
int block_offset = 0;
|
2023-11-27 19:12:24 +03:00
|
|
|
|
|
|
|
if (block) {
|
|
|
|
PM_COMPILE_NOT_POPPED(block);
|
|
|
|
flag |= VM_CALL_ARGS_BLOCKARG;
|
2023-11-27 22:07:27 +03:00
|
|
|
block_offset = 1;
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
|
|
|
|
2023-11-27 22:07:27 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, dupn, FIXNUM_INC(argc, 1 + block_offset));
|
2023-11-27 19:12:24 +03:00
|
|
|
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idAREF, argc, INT2FIX(flag));
|
|
|
|
|
|
|
|
LABEL *label = NEW_LABEL(lineno);
|
|
|
|
LABEL *lfin = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
PM_DUP;
|
|
|
|
|
|
|
|
if (and_node) {
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, label);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// ornode
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, label);
|
|
|
|
}
|
|
|
|
|
|
|
|
PM_POP;
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(value);
|
|
|
|
|
2023-11-27 22:07:27 +03:00
|
|
|
pm_compile_index_write_nodes_add_send(popped, ret, iseq, dummy_line_node, argc, flag, block_offset);
|
2023-11-27 20:48:51 +03:00
|
|
|
|
2023-11-27 19:12:24 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, lfin);
|
|
|
|
ADD_LABEL(ret, label);
|
|
|
|
if (!popped) {
|
2023-11-27 22:07:27 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, FIXNUM_INC(argc, 2 + block_offset));
|
2023-11-27 19:12:24 +03:00
|
|
|
}
|
2023-11-27 22:07:27 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, adjuststack, FIXNUM_INC(argc, 2 + block_offset));
|
2023-11-27 19:12:24 +03:00
|
|
|
ADD_LABEL(ret, lfin);
|
2023-11-27 22:07:27 +03:00
|
|
|
|
2023-11-27 19:12:24 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-19 23:59:07 +03:00
|
|
|
/**
|
|
|
|
* In order to properly compile multiple-assignment, some preprocessing needs to
|
|
|
|
* be performed in the case of call or constant path targets. This is when they
|
|
|
|
* are read, the "parent" of each of these nodes should only be read once (the
|
|
|
|
* receiver in the case of a call, the parent constant in the case of a constant
|
|
|
|
* path).
|
|
|
|
*/
|
2023-10-25 18:58:45 +03:00
|
|
|
static uint8_t
|
2023-10-23 20:49:39 +03:00
|
|
|
pm_compile_multi_write_lhs(rb_iseq_t *iseq, NODE dummy_line_node, const pm_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, uint8_t pushed, bool nested)
|
2023-09-27 19:39:53 +03:00
|
|
|
{
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
|
|
|
pm_multi_target_node_t *cast = (pm_multi_target_node_t *) node;
|
2023-10-26 22:03:33 +03:00
|
|
|
for (size_t index = 0; index < cast->lefts.size; index++) {
|
|
|
|
pushed = pm_compile_multi_write_lhs(iseq, dummy_line_node, cast->lefts.nodes[index], ret, scope_node, pushed, false);
|
2023-09-19 23:59:07 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_PATH_TARGET_NODE: {
|
|
|
|
pm_constant_path_target_node_t *cast = (pm_constant_path_target_node_t *)node;
|
2023-09-19 23:59:07 +03:00
|
|
|
if (cast->parent) {
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-17 01:36:25 +03:00
|
|
|
pushed = pm_compile_multi_write_lhs(iseq, dummy_line_node, cast->parent, ret, scope_node, pushed, false);
|
2023-09-19 23:59:07 +03:00
|
|
|
} else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
break;
|
2023-09-28 16:47:46 +03:00
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_PATH_NODE: {
|
|
|
|
pm_constant_path_node_t *cast = (pm_constant_path_node_t *) node;
|
|
|
|
if (cast->parent) {
|
2023-10-17 01:36:25 +03:00
|
|
|
pushed = pm_compile_multi_write_lhs(iseq, dummy_line_node, cast->parent, ret, scope_node, pushed, false);
|
2023-09-28 16:47:46 +03:00
|
|
|
} else {
|
2023-10-24 23:08:50 +03:00
|
|
|
PM_POP;
|
2023-09-27 19:39:53 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
|
2023-09-19 23:59:07 +03:00
|
|
|
}
|
2023-10-17 01:36:25 +03:00
|
|
|
pushed = pm_compile_multi_write_lhs(iseq, dummy_line_node, cast->child, ret, scope_node, pushed, cast->parent);
|
2023-09-28 16:47:46 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_READ_NODE: {
|
|
|
|
pm_constant_read_node_t *cast = (pm_constant_read_node_t *) node;
|
2023-09-19 23:59:07 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, RBOOL(!nested));
|
2023-10-17 01:36:25 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, ID2SYM(pm_constant_id_lookup(scope_node, cast->name)));
|
2023-09-19 23:59:07 +03:00
|
|
|
pushed = pushed + 2;
|
2023-09-28 16:47:46 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-09-19 23:59:07 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pushed;
|
|
|
|
}
|
|
|
|
|
2023-09-28 19:14:38 +03:00
|
|
|
/**
|
|
|
|
* Compile a pattern matching expression.
|
|
|
|
*/
|
2023-09-28 19:36:17 +03:00
|
|
|
static int
|
2023-10-23 20:49:39 +03:00
|
|
|
pm_compile_pattern(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, pm_scope_node_t *scope_node, LABEL *matched_label, LABEL *unmatched_label, bool in_alternation_pattern)
|
2023-09-28 19:14:38 +03:00
|
|
|
{
|
2023-10-17 01:36:25 +03:00
|
|
|
int lineno = (int) pm_newline_list_line_column(&scope_node->parser->newline_list, node->location.start).line;
|
2023-09-28 19:14:38 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
2023-09-28 19:19:50 +03:00
|
|
|
case PM_ARRAY_PATTERN_NODE:
|
|
|
|
rb_bug("Array pattern matching not yet supported.");
|
|
|
|
break;
|
|
|
|
case PM_FIND_PATTERN_NODE:
|
|
|
|
rb_bug("Find pattern matching not yet supported.");
|
|
|
|
break;
|
|
|
|
case PM_HASH_PATTERN_NODE:
|
|
|
|
rb_bug("Hash pattern matching not yet supported.");
|
|
|
|
break;
|
2023-09-28 19:36:17 +03:00
|
|
|
case PM_CAPTURE_PATTERN_NODE:
|
|
|
|
rb_bug("Capture pattern matching not yet supported.");
|
|
|
|
break;
|
2023-09-28 19:51:33 +03:00
|
|
|
case PM_IF_NODE: {
|
|
|
|
// If guards can be placed on patterns to further limit matches based on
|
|
|
|
// a dynamic predicate. This looks like:
|
|
|
|
//
|
|
|
|
// case foo
|
|
|
|
// in bar if baz
|
|
|
|
// end
|
|
|
|
//
|
|
|
|
pm_if_node_t *cast = (pm_if_node_t *) node;
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_pattern(iseq, cast->statements->body.nodes[0], ret, src, scope_node, matched_label, unmatched_label, in_alternation_pattern);
|
2023-09-28 19:51:33 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->predicate);
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, unmatched_label);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, matched_label);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_UNLESS_NODE: {
|
|
|
|
// Unless guards can be placed on patterns to further limit matches
|
|
|
|
// based on a dynamic predicate. This looks like:
|
|
|
|
//
|
|
|
|
// case foo
|
|
|
|
// in bar unless baz
|
|
|
|
// end
|
|
|
|
//
|
|
|
|
pm_unless_node_t *cast = (pm_unless_node_t *) node;
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_pattern(iseq, cast->statements->body.nodes[0], ret, src, scope_node, matched_label, unmatched_label, in_alternation_pattern);
|
2023-09-28 19:51:33 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(cast->predicate);
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, unmatched_label);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, matched_label);
|
|
|
|
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;
|
2023-10-17 01:36:25 +03:00
|
|
|
int index = pm_lookup_local_index(iseq, scope_node, cast->name);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index, (int) cast->depth);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, matched_label);
|
|
|
|
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;
|
|
|
|
|
|
|
|
LABEL *matched_left_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *unmatched_left_label = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
// 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-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_pattern(iseq, cast->left, ret, src, scope_node, matched_left_label, unmatched_left_label, true);
|
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-10-24 23:08:50 +03:00
|
|
|
PM_POP;
|
2023-09-28 19:27:04 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, matched_label);
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_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);
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_pattern(iseq, cast->right, ret, src, scope_node, matched_label, unmatched_label, true);
|
2023-09-28 19:19:50 +03:00
|
|
|
break;
|
2023-09-28 19:27:04 +03:00
|
|
|
}
|
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:
|
|
|
|
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:
|
|
|
|
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);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE));
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, matched_label);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, unmatched_label);
|
|
|
|
break;
|
|
|
|
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;
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_pattern(iseq, cast->variable, ret, src, scope_node, matched_label, unmatched_label, false);
|
2023-09-28 19:14:38 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_PINNED_EXPRESSION_NODE: {
|
2023-09-28 19:58:19 +03:00
|
|
|
// 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 that they
|
|
|
|
// hold.
|
2023-09-28 19:14:38 +03:00
|
|
|
pm_pinned_expression_node_t *cast = (pm_pinned_expression_node_t *) node;
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_pattern(iseq, cast->expression, ret, src, scope_node, matched_label, unmatched_label, false);
|
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-10-24 22:54:44 +03:00
|
|
|
// Generate a scope node from the given node.
|
|
|
|
void
|
|
|
|
pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous, pm_parser_t *parser)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
scope->parameters = NULL;
|
|
|
|
scope->body = NULL;
|
|
|
|
scope->constants = NULL;
|
2023-11-30 14:05:26 +03:00
|
|
|
scope->local_depth_offset = 0;
|
2023-12-06 18:05:14 +03:00
|
|
|
scope->local_table_for_iseq_size = 0;
|
|
|
|
|
2023-10-24 22:54:44 +03:00
|
|
|
if (previous) {
|
|
|
|
scope->constants = previous->constants;
|
2023-11-30 14:05:26 +03:00
|
|
|
scope->local_depth_offset = previous->local_depth_offset;
|
2023-10-24 22:54:44 +03:00
|
|
|
}
|
|
|
|
scope->index_lookup_table = NULL;
|
|
|
|
|
|
|
|
pm_constant_id_list_init(&scope->locals);
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(node)) {
|
|
|
|
case PM_BLOCK_NODE: {
|
|
|
|
pm_block_node_t *cast = (pm_block_node_t *) node;
|
|
|
|
scope->body = cast->body;
|
|
|
|
scope->locals = cast->locals;
|
2023-12-06 23:22:18 +03:00
|
|
|
scope->local_depth_offset = 0;
|
2023-12-06 18:05:14 +03:00
|
|
|
scope->parameters = cast->parameters;
|
2023-10-24 22:54:44 +03:00
|
|
|
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;
|
2023-12-06 18:05:14 +03:00
|
|
|
scope->parameters = (pm_node_t *)cast->parameters;
|
2023-10-24 22:54:44 +03:00
|
|
|
scope->body = cast->body;
|
|
|
|
scope->locals = cast->locals;
|
|
|
|
break;
|
|
|
|
}
|
2023-11-03 02:29:20 +03:00
|
|
|
case PM_ENSURE_NODE: {
|
2023-12-07 13:24:33 +03:00
|
|
|
scope->body = (pm_node_t *)node;
|
2023-11-30 14:05:26 +03:00
|
|
|
scope->local_depth_offset += 1;
|
2023-11-03 02:29:20 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-10-24 22:54:44 +03:00
|
|
|
case PM_FOR_NODE: {
|
|
|
|
pm_for_node_t *cast = (pm_for_node_t *)node;
|
|
|
|
scope->body = (pm_node_t *)cast->statements;
|
2023-12-01 16:34:21 +03:00
|
|
|
scope->local_depth_offset += 1;
|
2023-10-24 22:54:44 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-11-14 02:52:03 +03:00
|
|
|
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
|
|
|
|
RUBY_ASSERT(node->flags & PM_REGULAR_EXPRESSION_FLAGS_ONCE);
|
|
|
|
scope->body = (pm_node_t *)node;
|
2023-12-12 21:44:33 +03:00
|
|
|
scope->local_depth_offset += 1;
|
2023-11-14 02:52:03 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-10-24 22:54:44 +03:00
|
|
|
case PM_LAMBDA_NODE: {
|
|
|
|
pm_lambda_node_t *cast = (pm_lambda_node_t *) node;
|
2023-12-06 18:05:14 +03:00
|
|
|
scope->parameters = cast->parameters;
|
2023-10-24 22:54:44 +03:00
|
|
|
scope->body = cast->body;
|
|
|
|
scope->locals = cast->locals;
|
|
|
|
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;
|
2023-11-30 15:05:48 +03:00
|
|
|
scope->local_depth_offset += 2;
|
2023-10-24 22:54:44 +03:00
|
|
|
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;
|
|
|
|
}
|
2023-11-10 17:51:14 +03:00
|
|
|
case PM_RESCUE_NODE: {
|
|
|
|
pm_rescue_node_t *cast = (pm_rescue_node_t *)node;
|
|
|
|
scope->body = (pm_node_t *)cast->statements;
|
|
|
|
scope->local_depth_offset += 1;
|
|
|
|
break;
|
|
|
|
}
|
2023-12-01 17:37:10 +03:00
|
|
|
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;
|
|
|
|
scope->local_depth_offset += 1;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-24 22:54:44 +03:00
|
|
|
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;
|
|
|
|
}
|
2023-10-30 23:57:27 +03:00
|
|
|
case PM_STATEMENTS_NODE: {
|
|
|
|
pm_statements_node_t *cast = (pm_statements_node_t *) node;
|
|
|
|
scope->body = (pm_node_t *)cast;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-24 22:54:44 +03:00
|
|
|
default:
|
|
|
|
assert(false && "unreachable");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 01:35:13 +03:00
|
|
|
static void pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *const ret, const uint8_t *src, 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
|
2023-12-06 01:07:50 +03:00
|
|
|
pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, 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;
|
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];
|
|
|
|
pm_compile_defined_expr0(iseq, argument, ret, src, popped, scope_node, dummy_line_node, lineno, in_condition, lfinish, explicit_receiver);
|
|
|
|
|
|
|
|
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: {
|
|
|
|
pm_parentheses_node_t *parentheses_node = (pm_parentheses_node_t *) node;
|
|
|
|
|
|
|
|
if (parentheses_node->body == NULL) {
|
|
|
|
dtype = DEFINED_NIL;
|
|
|
|
} else {
|
|
|
|
dtype = DEFINED_EXPR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
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: {
|
2023-12-06 01:07:50 +03:00
|
|
|
pm_array_node_t *array_node = (pm_array_node_t *) node;
|
|
|
|
if (!(array_node->base.flags & PM_ARRAY_NODE_FLAGS_CONTAINS_SPLAT)) {
|
|
|
|
for (size_t index = 0; index < array_node->elements.size; index++) {
|
|
|
|
pm_compile_defined_expr0(iseq, array_node->elements.nodes[index], ret, src, popped, scope_node, dummy_line_node, lineno, true, lfinish, false);
|
|
|
|
if (!lfinish[1]) {
|
|
|
|
lfinish[1] = NEW_LABEL(lineno);
|
|
|
|
}
|
|
|
|
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:
|
2023-12-01 22:28:54 +03:00
|
|
|
case PM_FLOAT_NODE:
|
|
|
|
case PM_HASH_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:
|
[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:
|
2023-10-25 11:07:26 +03:00
|
|
|
case PM_OR_NODE:
|
2023-12-01 22:28:54 +03:00
|
|
|
case PM_RANGE_NODE:
|
|
|
|
case PM_REGULAR_EXPRESSION_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:
|
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);
|
|
|
|
}
|
2023-12-06 01:07:50 +03:00
|
|
|
pm_compile_defined_expr0(iseq, constant_path_node->parent, ret, src, 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) {
|
|
|
|
pm_compile_defined_expr0(iseq, (const pm_node_t *)call_node->arguments, ret, src, popped, scope_node, dummy_line_node, lineno, true, lfinish, false);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfinish[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call_node->receiver) {
|
|
|
|
pm_compile_defined_expr0(iseq, call_node->receiver, ret, src, popped, scope_node, dummy_line_node, lineno, true, lfinish, true);
|
|
|
|
if (PM_NODE_TYPE_P(call_node->receiver, PM_CALL_NODE)) {
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfinish[2]);
|
[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);
|
2023-12-12 01:35:13 +03:00
|
|
|
pm_compile_call(iseq, (const pm_call_node_t *)call_node->receiver, ret, src, 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;
|
|
|
|
case PM_CONSTANT_WRITE_NODE:
|
|
|
|
case PM_CONSTANT_OPERATOR_WRITE_NODE:
|
|
|
|
case PM_CONSTANT_AND_WRITE_NODE:
|
|
|
|
case PM_CONSTANT_OR_WRITE_NODE:
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
assert(dtype != DEFINED_NOT_DEFINED);
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, PUSH_VAL(dtype));
|
|
|
|
#undef PUSH_VAL
|
|
|
|
}
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
static void
|
|
|
|
pm_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, 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
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
pm_compile_defined_expr0(iseq, node, ret, src, 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
|
|
|
|
pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, int lineno, bool in_condition)
|
|
|
|
{
|
|
|
|
LABEL *lfinish[3];
|
|
|
|
LINK_ELEMENT *last = ret->last;
|
|
|
|
|
|
|
|
lfinish[0] = NEW_LABEL(lineno);
|
|
|
|
lfinish[1] = 0;
|
|
|
|
lfinish[2] = 0;
|
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
pm_defined_expr(iseq, node, ret, src, popped, scope_node, dummy_line_node, lineno, in_condition, lfinish, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lfinish[1]) {
|
2023-10-27 00:54:38 +03:00
|
|
|
ELEM_INSERT_NEXT(last, &new_insn_body(iseq, &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
|
2023-12-12 01:35:13 +03:00
|
|
|
pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, ID method_id, LABEL *start)
|
2023-12-06 01:07:50 +03:00
|
|
|
{
|
|
|
|
pm_parser_t *parser = scope_node->parser;
|
|
|
|
pm_newline_list_t newline_list = parser->newline_list;
|
|
|
|
int lineno = (int)pm_newline_list_line_column(&newline_list, ((pm_node_t *)call_node)->location.start).line;
|
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
2023-12-12 01:35:13 +03:00
|
|
|
LABEL *end = NEW_LABEL(lineno);
|
2023-12-06 01:07:50 +03:00
|
|
|
|
2023-12-12 22:24:00 +03:00
|
|
|
pm_node_t *pm_node = (pm_node_t *)call_node;
|
|
|
|
|
2023-12-06 01:07:50 +03:00
|
|
|
int flags = 0;
|
|
|
|
struct rb_callinfo_kwarg *kw_arg = NULL;
|
|
|
|
|
|
|
|
int orig_argc = pm_setup_args(call_node->arguments, &flags, &kw_arg, iseq, ret, src, popped, scope_node, dummy_line_node, parser);
|
|
|
|
|
|
|
|
const rb_iseq_t *block_iseq = NULL;
|
|
|
|
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;
|
|
|
|
pm_scope_node_init(call_node->block, &next_scope_node, scope_node, parser);
|
|
|
|
|
|
|
|
block_iseq = NEW_CHILD_ISEQ(next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
2023-12-12 01:35:13 +03:00
|
|
|
if (ISEQ_BODY(block_iseq)->catch_table) {
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, start, end, block_iseq, end);
|
|
|
|
}
|
2023-12-06 01:07:50 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
|
|
|
|
}
|
|
|
|
else {
|
2023-12-12 22:24:00 +03:00
|
|
|
if (pm_node->flags & PM_CALL_NODE_FLAGS_VARIABLE_CALL) {
|
2023-12-06 01:07:50 +03:00
|
|
|
flags |= VM_CALL_VCALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call_node->block != NULL) {
|
|
|
|
PM_COMPILE_NOT_POPPED(call_node->block);
|
|
|
|
flags |= VM_CALL_ARGS_BLOCKARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!flags) {
|
|
|
|
flags |= VM_CALL_ARGS_SIMPLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 23:56:03 +03:00
|
|
|
if (call_node->receiver == NULL || PM_NODE_TYPE_P(call_node->receiver, PM_SELF_NODE)) {
|
2023-12-06 01:07:50 +03:00
|
|
|
flags |= VM_CALL_FCALL;
|
|
|
|
}
|
|
|
|
|
2023-12-12 22:24:00 +03:00
|
|
|
if (pm_node->flags & PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) {
|
[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
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(orig_argc + 1));
|
2023-12-12 22:24:00 +03:00
|
|
|
ADD_SEND_R(ret, &dummy_line_node, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
|
[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
|
|
|
PM_POP;
|
|
|
|
}
|
2023-12-12 22:24:00 +03:00
|
|
|
else {
|
|
|
|
ADD_SEND_R(ret, &dummy_line_node, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
|
|
|
|
}
|
2023-12-12 01:35:13 +03:00
|
|
|
ADD_LABEL(ret, end);
|
[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
|
|
|
|
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
|
|
|
|
pm_add_ensure_iseq(LINK_ANCHOR *const ret, rb_iseq_t *iseq, int is_return, const uint8_t *src, pm_scope_node_t *scope_node)
|
|
|
|
{
|
|
|
|
assert(can_add_ensure_iseq(iseq));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ID local = pm_constant_id_lookup(scope_node, constant_id);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
st_insert(index_lookup_table, constant_id, local_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pm_compile_multi_assign_params(pm_multi_target_node_t *multi, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node, int local_index)
|
|
|
|
{
|
|
|
|
for (size_t m = 0; m < multi->lefts.size; m++) {
|
|
|
|
pm_node_t *multi_node = multi->lefts.nodes[m];
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(multi_node)) {
|
|
|
|
case PM_REQUIRED_PARAMETER_NODE: {
|
|
|
|
pm_required_parameter_node_t *req = (pm_required_parameter_node_t *)multi_node;
|
|
|
|
pm_insert_local_index(req->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
local_index++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
|
|
|
local_index = pm_compile_multi_assign_params((pm_multi_target_node_t *)multi_node, index_lookup_table, local_table_for_iseq, scope_node, local_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
rb_bug("Parameter within a MultiTargetNode isn't allowed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (multi->rest && PM_NODE_TYPE_P(multi->rest, PM_SPLAT_NODE)) {
|
|
|
|
pm_splat_node_t *rest = (pm_splat_node_t *)multi->rest;
|
|
|
|
if (rest->expression && PM_NODE_TYPE_P(rest->expression, PM_REQUIRED_PARAMETER_NODE)) {
|
|
|
|
pm_required_parameter_node_t *req = (pm_required_parameter_node_t *)rest->expression;
|
|
|
|
pm_insert_local_index(req->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
local_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t m = 0; m < multi->rights.size; m++) {
|
|
|
|
pm_node_t *multi_node = multi->rights.nodes[m];
|
|
|
|
|
|
|
|
switch (PM_NODE_TYPE(multi_node)) {
|
|
|
|
case PM_REQUIRED_PARAMETER_NODE: {
|
|
|
|
pm_required_parameter_node_t *req = (pm_required_parameter_node_t *)multi_node;
|
|
|
|
pm_insert_local_index(req->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
local_index++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
|
|
|
local_index = pm_compile_multi_assign_params((pm_multi_target_node_t *)multi_node, index_lookup_table, local_table_for_iseq, scope_node, local_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
rb_bug("Parameter within a MultiTargetNode isn't allowed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return local_index;
|
|
|
|
}
|
|
|
|
|
2023-08-29 19:27:00 +03:00
|
|
|
/*
|
2023-09-27 19:39:53 +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
|
2023-10-23 20:49:39 +03:00
|
|
|
pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
|
2023-08-30 00:17:08 +03:00
|
|
|
{
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_parser_t *parser = scope_node->parser;
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_newline_list_t newline_list = parser->newline_list;
|
|
|
|
int lineno = (int)pm_newline_list_line_column(&newline_list, node->location.start).line;
|
2023-08-29 19:27:00 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
2023-11-24 14:36:55 +03:00
|
|
|
if (node->flags & PM_NODE_FLAG_NEWLINE &&
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
ADD_TRACE(ret, event);
|
|
|
|
}
|
|
|
|
|
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: {
|
|
|
|
pm_alias_global_variable_node_t *alias_node = (pm_alias_global_variable_node_t *) node;
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
|
2023-10-19 23:50:21 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, ID2SYM(parse_location_symbol(&alias_node->new_name->location, parser)));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, ID2SYM(parse_location_symbol(&alias_node->old_name->location, parser)));
|
2023-10-16 22:00:01 +03:00
|
|
|
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_set_variable_alias, INT2FIX(2));
|
|
|
|
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_ALIAS_METHOD_NODE: {
|
|
|
|
pm_alias_method_node_t *alias_node = (pm_alias_method_node_t *) node;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-09-05 20:58:16 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(alias_node->new_name);
|
|
|
|
PM_COMPILE_NOT_POPPED(alias_node->old_name);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_set_method_alias, INT2FIX(3));
|
2023-09-20 18:05:45 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_IF_POPPED;
|
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: {
|
|
|
|
pm_and_node_t *and_node = (pm_and_node_t *) node;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(and_node->left);
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-08-29 19:27:00 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, end_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(and_node->right);
|
2023-08-29 19:27:00 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_ARGUMENTS_NODE: {
|
2023-12-07 23:03:06 +03:00
|
|
|
// These are ArgumentsNodes that are not compiled directly by their
|
|
|
|
// parent call nodes, used in the cases of NextNodes, ReturnNodes
|
|
|
|
// and BreakNodes
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_arguments_node_t *arguments_node = (pm_arguments_node_t *) node;
|
|
|
|
pm_node_list_t node_list = arguments_node->arguments;
|
2023-08-29 19:27:00 +03:00
|
|
|
for (size_t index = 0; index < node_list.size; index++) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(node_list.nodes[index]);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
2023-12-07 22:13:33 +03:00
|
|
|
if (node_list.size > 1) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(node_list.size));
|
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_ARRAY_NODE: {
|
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) {
|
2023-11-29 23:02:32 +03:00
|
|
|
pm_array_node_t *cast = (pm_array_node_t *) node;
|
|
|
|
if (cast->elements.size) {
|
|
|
|
VALUE value = pm_static_literal_value(node, scope_node, parser);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, duparray, value);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(0));
|
|
|
|
}
|
2023-09-28 21:25:47 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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.
|
2023-09-28 22:19:56 +03:00
|
|
|
pm_array_node_t *cast = (pm_array_node_t *) node;
|
|
|
|
pm_node_list_t *elements = &cast->elements;
|
|
|
|
|
2023-11-22 17:49:26 +03:00
|
|
|
// In the case that there is a splat node within the array,
|
|
|
|
// the array gets compiled slightly differently.
|
|
|
|
if (node->flags & PM_ARRAY_NODE_FLAGS_CONTAINS_SPLAT) {
|
|
|
|
if (elements->size == 1) {
|
|
|
|
// If the only nodes is a SplatNode, we never
|
|
|
|
// need to emit the newarray or concatarray
|
|
|
|
// instructions
|
|
|
|
PM_COMPILE_NOT_POPPED(elements->nodes[0]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We treat all sequences of non-splat elements as their
|
|
|
|
// own arrays, followed by a newarray, and then continually
|
|
|
|
// concat the arrays with the SplatNodes
|
|
|
|
int new_array_size = 0;
|
|
|
|
bool need_to_concat_array = false;
|
|
|
|
for (size_t index = 0; index < elements->size; index++) {
|
|
|
|
pm_node_t *array_element = elements->nodes[index];
|
|
|
|
if (PM_NODE_TYPE_P(array_element, PM_SPLAT_NODE)) {
|
|
|
|
pm_splat_node_t *splat_element = (pm_splat_node_t *)array_element;
|
|
|
|
|
|
|
|
// If we already have non-splat elements, we need to emit a newarray
|
|
|
|
// instruction
|
|
|
|
if (new_array_size) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(new_array_size));
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_array_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(splat_element->expression);
|
|
|
|
|
|
|
|
if (index > 0) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If this is the first element, we need to splatarray
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qtrue);
|
|
|
|
}
|
|
|
|
|
|
|
|
need_to_concat_array = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_array_size++;
|
|
|
|
PM_COMPILE_NOT_POPPED(array_element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_array_size) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(new_array_size));
|
|
|
|
if (need_to_concat_array) {
|
|
|
|
ADD_INSN(ret, &dummy_line_node, concatarray);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2023-11-22 17:49:26 +03:00
|
|
|
else {
|
|
|
|
for (size_t index = 0; index < elements->size; index++) {
|
|
|
|
PM_COMPILE(elements->nodes[index]);
|
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-11-22 17:49:26 +03:00
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(elements->size));
|
|
|
|
}
|
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: {
|
|
|
|
pm_assoc_node_t *assoc_node = (pm_assoc_node_t *) node;
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(assoc_node->key);
|
2023-08-30 00:17:08 +03:00
|
|
|
if (assoc_node->value) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(assoc_node->value);
|
2023-08-30 00:17:08 +03:00
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_ASSOC_SPLAT_NODE: {
|
|
|
|
pm_assoc_splat_node_t *assoc_splat_node = (pm_assoc_splat_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-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(assoc_splat_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
|
|
|
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-03 02:29:20 +03:00
|
|
|
rb_iseq_t *child_iseq;
|
|
|
|
LABEL *lstart = NEW_LABEL(lineno);
|
|
|
|
LABEL *lend = NEW_LABEL(lineno);
|
2023-11-10 17:51:14 +03:00
|
|
|
LABEL *lcont = NEW_LABEL(lineno);
|
2023-11-03 02:29:20 +03:00
|
|
|
|
2023-11-10 17:51:14 +03:00
|
|
|
|
|
|
|
if (begin_node->rescue_clause) {
|
|
|
|
pm_scope_node_t rescue_scope_node;
|
|
|
|
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, 1);
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (begin_node->else_clause) {
|
|
|
|
PM_POP_UNLESS_POPPED;
|
|
|
|
PM_COMPILE((pm_node_t *)begin_node->else_clause);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_LABEL(ret, lend);
|
2023-12-06 17:45:04 +03:00
|
|
|
PM_NOP;
|
2023-11-10 17:51:14 +03:00
|
|
|
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);
|
|
|
|
}
|
2023-12-04 15:27:34 +03:00
|
|
|
if (begin_node->ensure_clause) {
|
|
|
|
LABEL *estart = NEW_LABEL(lineno);
|
|
|
|
LABEL *eend = NEW_LABEL(lineno);
|
|
|
|
LABEL *econt = NEW_LABEL(lineno);
|
|
|
|
ADD_LABEL(ret, estart);
|
|
|
|
if (!begin_node->rescue_clause) {
|
|
|
|
if (begin_node->statements) {
|
|
|
|
PM_COMPILE((pm_node_t *)begin_node->statements);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ADD_LABEL(ret, eend);
|
2023-12-12 19:23:50 +03:00
|
|
|
if (!popped) {
|
|
|
|
PM_NOP;
|
|
|
|
}
|
2023-12-04 15:27:34 +03:00
|
|
|
pm_statements_node_t *statements = begin_node->ensure_clause->statements;
|
|
|
|
if (statements) {
|
|
|
|
PM_COMPILE((pm_node_t *)statements);
|
|
|
|
ADD_LABEL(ret, econt);
|
|
|
|
PM_POP_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-12-11 20:14:21 +03:00
|
|
|
push_ensure_entry(iseq, &enl, &er, (void *)begin_node->ensure_clause);
|
2023-12-04 15:27:34 +03:00
|
|
|
|
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init((pm_node_t *)begin_node->ensure_clause, &next_scope_node, scope_node, parser);
|
|
|
|
|
|
|
|
child_iseq = NEW_CHILD_ISEQ(next_scope_node,
|
|
|
|
rb_str_new2("ensure in"),
|
|
|
|
ISEQ_TYPE_ENSURE, lineno);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 17:51:14 +03:00
|
|
|
|
|
|
|
if (!begin_node->rescue_clause && !begin_node->ensure_clause) {
|
|
|
|
ADD_LABEL(ret, lstart);
|
|
|
|
if (begin_node->statements) {
|
|
|
|
PM_COMPILE((pm_node_t *)begin_node->statements);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
ADD_LABEL(ret, lend);
|
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_BLOCK_ARGUMENT_NODE: {
|
|
|
|
pm_block_argument_node_t *block_argument_node = (pm_block_argument_node_t *) node;
|
2023-10-13 20:59:37 +03:00
|
|
|
if (block_argument_node->expression) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(block_argument_node->expression);
|
2023-10-13 20:59:37 +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;
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
rb_bug("Can't escape from eval with break");
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
rb_bug("");
|
|
|
|
}
|
2023-08-31 00:27:08 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CALL_NODE: {
|
|
|
|
pm_call_node_t *call_node = (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);
|
2023-12-12 22:24:00 +03:00
|
|
|
if (node->flags & PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) {
|
[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
|
|
|
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
|
|
|
if (call_node->receiver == NULL) {
|
2023-10-30 19:56:30 +03:00
|
|
|
PM_PUTSELF;
|
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-09-27 19:39:53 +03:00
|
|
|
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
|
|
|
|
2023-12-12 01:35:13 +03:00
|
|
|
pm_compile_call(iseq, call_node, ret, src, 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: {
|
|
|
|
pm_call_and_write_node_t *call_and_write_node = (pm_call_and_write_node_t*) node;
|
|
|
|
|
2023-10-25 18:40:30 +03:00
|
|
|
pm_compile_call_and_or_write_node(true, call_and_write_node->receiver, call_and_write_node->value, call_and_write_node->write_name, call_and_write_node->read_name, ret, iseq, lineno, src, popped, scope_node);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case PM_CALL_OR_WRITE_NODE: {
|
|
|
|
pm_call_or_write_node_t *call_or_write_node = (pm_call_or_write_node_t*) node;
|
|
|
|
|
|
|
|
pm_compile_call_and_or_write_node(false, call_or_write_node->receiver, call_or_write_node->value, call_or_write_node->write_name, call_or_write_node->read_name, ret, iseq, lineno, src, 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: {
|
|
|
|
pm_call_operator_write_node_t *call_operator_write_node = (pm_call_operator_write_node_t*) node;
|
|
|
|
|
|
|
|
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
|
|
|
|
|
|
|
|
int flag = 0;
|
|
|
|
|
|
|
|
if (PM_NODE_TYPE_P(call_operator_write_node->receiver, PM_SELF_NODE)) {
|
|
|
|
flag = VM_CALL_FCALL;
|
|
|
|
}
|
|
|
|
|
2023-11-02 21:08:03 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(call_operator_write_node->receiver);
|
2023-10-25 18:58:45 +03:00
|
|
|
|
|
|
|
ID write_name_id = pm_constant_id_lookup(scope_node, call_operator_write_node->write_name);
|
|
|
|
ID read_name_id = pm_constant_id_lookup(scope_node, call_operator_write_node->read_name);
|
|
|
|
ID operator_id = pm_constant_id_lookup(scope_node, call_operator_write_node->operator);
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-10-25 18:58:45 +03:00
|
|
|
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, read_name_id, INT2FIX(0), INT2FIX(flag));
|
|
|
|
|
2023-11-02 21:08:03 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(call_operator_write_node->value);
|
2023-10-25 18:58:45 +03:00
|
|
|
ADD_SEND(ret, &dummy_line_node, operator_id, INT2FIX(1));
|
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
PM_SWAP;
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, write_name_id, INT2FIX(1), INT2FIX(flag));
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_POP;
|
2023-10-25 18:58:45 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-10-24 21:24:36 +03:00
|
|
|
case PM_CASE_NODE: {
|
|
|
|
pm_case_node_t *case_node = (pm_case_node_t *)node;
|
|
|
|
bool has_predicate = case_node->predicate;
|
|
|
|
if (has_predicate) {
|
|
|
|
PM_COMPILE_NOT_POPPED(case_node->predicate);
|
|
|
|
}
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
pm_node_list_t conditions = case_node->conditions;
|
|
|
|
|
|
|
|
LABEL **conditions_labels = (LABEL **)ALLOC_N(VALUE, conditions.size + 1);
|
|
|
|
LABEL *label;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < conditions.size; i++) {
|
|
|
|
label = NEW_LABEL(lineno);
|
|
|
|
conditions_labels[i] = label;
|
|
|
|
if (has_predicate) {
|
|
|
|
pm_when_node_t *when_node = (pm_when_node_t *)conditions.nodes[i];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < when_node->conditions.size; i++) {
|
|
|
|
PM_COMPILE_NOT_POPPED(when_node->conditions.nodes[i]);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idEqq, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, label);
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_PUTNIL;
|
2023-10-24 21:24:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_predicate) {
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_POP;
|
2023-10-24 21:24:36 +03:00
|
|
|
|
|
|
|
if (case_node->consequent) {
|
2023-12-09 00:17:07 +03:00
|
|
|
PM_COMPILE((pm_node_t *)case_node->consequent);
|
2023-10-24 21:24:36 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, end_label);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < conditions.size; i++) {
|
|
|
|
label = conditions_labels[i];
|
|
|
|
ADD_LABEL(ret, label);
|
|
|
|
if (has_predicate) {
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_POP;
|
2023-10-24 21:24:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pm_while_node_t *condition_node = (pm_while_node_t *)conditions.nodes[i];
|
|
|
|
if (condition_node->statements) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE((pm_node_t *)condition_node->statements);
|
2023-10-24 21:24:36 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, end_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init((pm_node_t *)class_node, &next_scope_node, scope_node, parser);
|
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)));
|
|
|
|
|
2023-10-18 21:18:37 +03:00
|
|
|
const rb_iseq_t *class_iseq = NEW_CHILD_ISEQ(next_scope_node, class_name, ISEQ_TYPE_CLASS, 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
|
|
|
|
|
|
|
// 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) |
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_class_path(ret, iseq, class_node->constant_path, &dummy_line_node, src, 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);
|
|
|
|
|
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;
|
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_TARGET_NODE: {
|
|
|
|
pm_class_variable_target_node_t *write_node = (pm_class_variable_target_node_t *) node;
|
2023-10-17 01:36:25 +03:00
|
|
|
ID cvar_name = pm_constant_id_lookup(scope_node, write_node->name);
|
2023-09-18 23:35:02 +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_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: {
|
|
|
|
pm_constant_path_node_t *constant_path_node = (pm_constant_path_node_t*) node;
|
2023-08-29 19:27:00 +03:00
|
|
|
if (constant_path_node->parent) {
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(constant_path_node->parent);
|
2023-09-28 19:14:38 +03:00
|
|
|
} else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse);
|
2023-09-11 21:36:09 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
assert(PM_NODE_TYPE_P(constant_path_node->child, PM_CONSTANT_READ_NODE));
|
|
|
|
pm_constant_read_node_t *child = (pm_constant_read_node_t *) constant_path_node->child;
|
2023-09-11 21:36:09 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, ID2SYM(pm_constant_id_lookup(scope_node, child->name)));
|
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: {
|
|
|
|
pm_constant_path_and_write_node_t *constant_path_and_write_node = (pm_constant_path_and_write_node_t*) node;
|
|
|
|
|
|
|
|
LABEL *lfin = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
pm_constant_path_node_t *target = constant_path_and_write_node->target;
|
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
|
|
|
|
|
|
|
pm_constant_read_node_t *child = (pm_constant_read_node_t *)target->child;
|
|
|
|
VALUE child_name = ID2SYM(pm_constant_id_lookup(scope_node, child->name));
|
|
|
|
|
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);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, child_name);
|
|
|
|
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, lfin);
|
|
|
|
|
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-11-02 21:18:40 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(constant_path_and_write_node->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
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, child_name);
|
|
|
|
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: {
|
|
|
|
pm_constant_path_or_write_node_t *constant_path_or_write_node = (pm_constant_path_or_write_node_t*) node;
|
|
|
|
|
|
|
|
LABEL *lassign = NEW_LABEL(lineno);
|
|
|
|
LABEL *lfin = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
pm_constant_path_node_t *target = constant_path_or_write_node->target;
|
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
|
|
|
|
|
|
|
pm_constant_read_node_t *child = (pm_constant_read_node_t *)target->child;
|
|
|
|
VALUE child_name = ID2SYM(pm_constant_id_lookup(scope_node, child->name));
|
|
|
|
|
2023-10-26 18:03:18 +03:00
|
|
|
PM_DUP;
|
2023-10-26 12:57:05 +03:00
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_CONST_FROM), child_name, Qtrue);
|
|
|
|
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);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, child_name);
|
|
|
|
|
|
|
|
PM_DUP_UNLESS_POPPED;
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, lfin);
|
|
|
|
|
|
|
|
PM_POP_UNLESS_POPPED;
|
|
|
|
ADD_LABEL(ret, lassign);
|
2023-11-02 20:45:44 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(constant_path_or_write_node->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
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, child_name);
|
|
|
|
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: {
|
|
|
|
pm_constant_path_operator_write_node_t *constant_path_operator_write_node = (pm_constant_path_operator_write_node_t*) node;
|
|
|
|
|
|
|
|
pm_constant_path_node_t *target = constant_path_operator_write_node->target;
|
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);
|
|
|
|
|
|
|
|
pm_constant_read_node_t *child = (pm_constant_read_node_t *)target->child;
|
|
|
|
VALUE child_name = ID2SYM(pm_constant_id_lookup(scope_node, child->name));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, child_name);
|
|
|
|
|
2023-11-02 20:46:40 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(constant_path_operator_write_node->value);
|
2023-10-25 21:00:24 +03:00
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, constant_path_operator_write_node->operator);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, child_name);
|
|
|
|
return ;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_PATH_TARGET_NODE: {
|
|
|
|
pm_constant_path_target_node_t *cast = (pm_constant_path_target_node_t *)node;
|
2023-09-19 23:59:07 +03:00
|
|
|
|
2023-10-13 20:59:37 +03:00
|
|
|
if (cast->parent) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(cast->parent);
|
2023-10-13 20:59:37 +03:00
|
|
|
}
|
2023-09-19 23:59:07 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_PATH_WRITE_NODE: {
|
|
|
|
pm_constant_path_write_node_t *constant_path_write_node = (pm_constant_path_write_node_t*) node;
|
2023-10-16 16:31:32 +03:00
|
|
|
if (constant_path_write_node->target->parent) {
|
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *)constant_path_write_node->target->parent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
|
|
|
|
}
|
|
|
|
PM_COMPILE_NOT_POPPED(constant_path_write_node->value);
|
|
|
|
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));
|
|
|
|
}
|
2023-10-25 18:34:03 +03:00
|
|
|
PM_SWAP;
|
2023-10-17 01:36:25 +03:00
|
|
|
VALUE constant_name = ID2SYM(pm_constant_id_lookup(scope_node,
|
2023-10-16 16:31:32 +03:00
|
|
|
((pm_constant_read_node_t *)constant_path_write_node->target->child)->name));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, constant_name);
|
2023-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_READ_NODE: {
|
|
|
|
pm_constant_read_node_t *constant_read_node = (pm_constant_read_node_t *) node;
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-08-29 19:27:00 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
2023-10-17 01:36:25 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, ID2SYM(pm_constant_id_lookup(scope_node, constant_read_node->name)));
|
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: {
|
|
|
|
pm_constant_and_write_node_t *constant_and_write_node = (pm_constant_and_write_node_t*) node;
|
2023-09-05 17:54:21 +03:00
|
|
|
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
VALUE constant_name = ID2SYM(pm_constant_id_lookup(scope_node, constant_and_write_node->name));
|
2023-09-05 17:54:21 +03:00
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-09-05 17:54:21 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, constant_name);
|
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
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(constant_and_write_node->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));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, constant_name);
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_OPERATOR_WRITE_NODE: {
|
|
|
|
pm_constant_operator_write_node_t *constant_operator_write_node = (pm_constant_operator_write_node_t*) node;
|
2023-09-05 17:54:21 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID constant_name = pm_constant_id_lookup(scope_node, constant_operator_write_node->name);
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-09-05 17:54:21 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, ID2SYM(constant_name));
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(constant_operator_write_node->value);
|
2023-10-17 01:36:25 +03:00
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, constant_operator_write_node->operator);
|
2023-09-05 17:54:21 +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-05 17:54:21 +03:00
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, ID2SYM(constant_name));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_OR_WRITE_NODE: {
|
|
|
|
pm_constant_or_write_node_t *constant_or_write_node = (pm_constant_or_write_node_t*) node;
|
2023-09-05 17:54:21 +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 constant_name = ID2SYM(pm_constant_id_lookup(scope_node, constant_or_write_node->name));
|
2023-09-05 17:54:21 +03:00
|
|
|
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defined, INT2FIX(DEFINED_CONST), constant_name, Qtrue);
|
|
|
|
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchunless, set_label);
|
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-09-05 17:54:21 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, getconstant, constant_name);
|
|
|
|
|
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);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(constant_or_write_node->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));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, constant_name);
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_TARGET_NODE: {
|
|
|
|
pm_constant_target_node_t *constant_write_node = (pm_constant_target_node_t *) node;
|
2023-09-18 23:50:33 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
|
2023-10-17 01:36:25 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, ID2SYM(pm_constant_id_lookup(scope_node, constant_write_node->name)));
|
2023-09-18 23:50:33 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_CONSTANT_WRITE_NODE: {
|
|
|
|
pm_constant_write_node_t *constant_write_node = (pm_constant_write_node_t *) node;
|
|
|
|
PM_COMPILE_NOT_POPPED(constant_write_node->value);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
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));
|
2023-10-17 01:36:25 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, ID2SYM(pm_constant_id_lookup(scope_node, constant_write_node->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);
|
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init((pm_node_t *)def_node, &next_scope_node, scope_node, parser);
|
2023-10-18 21:18:37 +03:00
|
|
|
rb_iseq_t *method_iseq = NEW_ISEQ(next_scope_node, rb_id2str(method_name), ISEQ_TYPE_METHOD, 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-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;
|
2023-10-27 00:54:38 +03:00
|
|
|
pm_compile_defined_expr(iseq, defined_node->value, ret, src, 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) {
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->end_label = end;
|
|
|
|
PM_COMPILE((pm_node_t *)ensure_node->statements);
|
|
|
|
}
|
|
|
|
ADD_LABEL(ret, end);
|
|
|
|
}
|
[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
|
|
|
|
2023-10-20 18:03:02 +03:00
|
|
|
pm_compile_flip_flop(flip_flop_node, else_label, then_label, iseq, lineno, ret, src, 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) {
|
2023-09-08 22:33:51 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, parse_float(node));
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-10-20 12:59:02 +03:00
|
|
|
case PM_FOR_NODE: {
|
|
|
|
pm_for_node_t *for_node = (pm_for_node_t *)node;
|
|
|
|
|
2023-12-07 18:00:39 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->catch_except_p = true;
|
|
|
|
|
2023-10-20 12:59:02 +03:00
|
|
|
const rb_iseq_t *child_iseq;
|
|
|
|
const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
|
|
|
|
|
|
|
|
LABEL *retry_label = NEW_LABEL(lineno);
|
|
|
|
LABEL *retry_end_l = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init((pm_node_t *)for_node, &next_scope_node, scope_node, parser);
|
|
|
|
|
|
|
|
pm_constant_id_list_t locals;
|
|
|
|
pm_constant_id_list_init(&locals);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, retry_label);
|
|
|
|
|
2023-11-02 21:13:33 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(for_node->collection);
|
|
|
|
|
2023-10-20 12:59:02 +03:00
|
|
|
child_iseq = NEW_CHILD_ISEQ(next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
|
|
|
|
ADD_SEND_WITH_BLOCK(ret, &dummy_line_node, idEach, INT2FIX(0), child_iseq);
|
|
|
|
|
2023-12-07 18:00:39 +03:00
|
|
|
ADD_LABEL(ret, retry_end_l);
|
2023-11-02 21:13:33 +03:00
|
|
|
PM_POP_IF_POPPED;
|
2023-10-20 12:59:02 +03:00
|
|
|
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
|
|
|
|
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: {
|
|
|
|
rb_bug("Should never hit the forwarding arguments case directly\n");
|
|
|
|
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;
|
|
|
|
pm_scope_node_init((pm_node_t *)forwarding_super_node->block, &next_scope_node, scope_node, parser);
|
|
|
|
block = NEW_CHILD_ISEQ(next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
|
|
|
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);
|
|
|
|
if (local_keyword->num > 0) {
|
|
|
|
ADD_SEND(args, &dummy_line_node, rb_intern("dup"), INT2FIX(0));
|
|
|
|
flag |= VM_CALL_KW_SPLAT_MUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(args, &dummy_line_node, newhash, INT2FIX(0));
|
|
|
|
flag |= VM_CALL_KW_SPLAT_MUT;
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
flag |= VM_CALL_KW_SPLAT;
|
|
|
|
}
|
|
|
|
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++;
|
|
|
|
flag |= VM_CALL_KW_SPLAT | VM_CALL_KW_SPLAT_MUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_TARGET_NODE: {
|
|
|
|
pm_global_variable_target_node_t *write_node = (pm_global_variable_target_node_t *) node;
|
2023-09-19 16:46:30 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID ivar_name = pm_constant_id_lookup(scope_node, write_node->name);
|
2023-09-19 16:46:30 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, setglobal, ID2SYM(ivar_name));
|
|
|
|
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) {
|
2023-10-19 23:50:21 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node, parser);
|
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.
|
2023-09-28 22:19:56 +03:00
|
|
|
pm_hash_node_t *cast = (pm_hash_node_t *) node;
|
2023-10-23 16:33:38 +03:00
|
|
|
// Elements must be non-empty, otherwise it would be static literal
|
2023-09-28 22:19:56 +03:00
|
|
|
pm_node_list_t *elements = &cast->elements;
|
|
|
|
|
2023-10-23 16:33:38 +03:00
|
|
|
pm_node_t *cur_node = elements->nodes[0];
|
|
|
|
pm_node_type_t cur_type = PM_NODE_TYPE(cur_node);
|
|
|
|
int elements_of_cur_type = 0;
|
|
|
|
int allocated_hashes = 0;
|
|
|
|
|
2023-11-02 20:56:40 +03:00
|
|
|
if (!PM_NODE_TYPE_P(cur_node, PM_ASSOC_NODE) && !popped) {
|
2023-10-23 16:33:38 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(0));
|
|
|
|
allocated_hashes++;
|
|
|
|
}
|
|
|
|
|
2023-09-28 22:19:56 +03:00
|
|
|
for (size_t index = 0; index < elements->size; index++) {
|
2023-10-23 16:33:38 +03:00
|
|
|
pm_node_t *cur_node = elements->nodes[index];
|
|
|
|
if (!popped) {
|
|
|
|
if (!PM_NODE_TYPE_P(cur_node, cur_type)) {
|
|
|
|
if (!allocated_hashes) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(elements_of_cur_type * 2));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (cur_type == PM_ASSOC_NODE) {
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_ptr, INT2FIX(3));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_kwd, INT2FIX(2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
2023-10-25 18:34:03 +03:00
|
|
|
PM_SWAP;
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(elements->nodes[index]);
|
2023-10-23 16:33:38 +03:00
|
|
|
|
|
|
|
allocated_hashes++;
|
|
|
|
elements_of_cur_type = 0;
|
|
|
|
cur_type = PM_NODE_TYPE(cur_node);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
elements_of_cur_type++;
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(elements->nodes[index]);
|
2023-10-23 16:33:38 +03:00
|
|
|
}
|
|
|
|
}
|
2023-11-02 20:56:40 +03:00
|
|
|
else {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(elements->nodes[index]);
|
2023-11-02 20:56:40 +03:00
|
|
|
}
|
2023-09-28 17:48:24 +03:00
|
|
|
}
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-09-28 17:48:24 +03:00
|
|
|
if (!popped) {
|
2023-10-23 16:33:38 +03:00
|
|
|
if (!allocated_hashes) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(elements_of_cur_type * 2));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (cur_type == PM_ASSOC_NODE) {
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_ptr, INT2FIX(3));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_kwd, INT2FIX(2));
|
|
|
|
}
|
|
|
|
}
|
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: {
|
|
|
|
const int line = (int)pm_newline_list_line_column(&(parser->newline_list), node->location.start).line;
|
|
|
|
pm_if_node_t *if_node = (pm_if_node_t *)node;
|
|
|
|
pm_statements_node_t *node_body = if_node->statements;
|
|
|
|
pm_node_t *node_else = if_node->consequent;
|
|
|
|
pm_node_t *predicate = if_node->predicate;
|
2023-08-30 00:17:08 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_if(iseq, line, node_body, node_else, predicate, ret, src, 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-11-27 19:12:24 +03:00
|
|
|
case PM_INDEX_AND_WRITE_NODE: {
|
|
|
|
pm_index_and_write_node_t *index_and_write_node = (pm_index_and_write_node_t *)node;
|
|
|
|
|
|
|
|
pm_compile_index_and_or_write_node(true, index_and_write_node->receiver, index_and_write_node->value, index_and_write_node->arguments, index_and_write_node->block, ret, iseq, lineno, src, popped, scope_node, parser);
|
|
|
|
return;
|
|
|
|
}
|
2023-11-27 16:04:24 +03:00
|
|
|
case PM_INDEX_OR_WRITE_NODE: {
|
|
|
|
pm_index_or_write_node_t *index_or_write_node = (pm_index_or_write_node_t *)node;
|
|
|
|
|
2023-11-27 19:12:24 +03:00
|
|
|
pm_compile_index_and_or_write_node(false, index_or_write_node->receiver, index_or_write_node->value, index_or_write_node->arguments, index_or_write_node->block, ret, iseq, lineno, src, popped, scope_node, parser);
|
2023-11-27 16:04:24 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-11-27 20:48:51 +03:00
|
|
|
case PM_INDEX_OPERATOR_WRITE_NODE: {
|
|
|
|
pm_index_operator_write_node_t *index_operator_write_node = (pm_index_operator_write_node_t *)node;
|
|
|
|
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(index_operator_write_node->receiver);
|
|
|
|
|
|
|
|
int flag = 0;
|
|
|
|
struct rb_callinfo_kwarg *keywords = NULL;
|
|
|
|
int argc_int = 0;
|
|
|
|
|
|
|
|
if (index_operator_write_node->arguments) {
|
|
|
|
argc_int = pm_setup_args(index_operator_write_node->arguments, &flag, &keywords, iseq, ret, src, popped, scope_node, dummy_line_node, parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE argc = INT2FIX(argc_int);
|
2023-11-27 22:07:27 +03:00
|
|
|
|
|
|
|
int block_offset = 0;
|
2023-11-27 20:48:51 +03:00
|
|
|
|
|
|
|
if (index_operator_write_node->block) {
|
|
|
|
PM_COMPILE_NOT_POPPED(index_operator_write_node->block);
|
|
|
|
flag |= VM_CALL_ARGS_BLOCKARG;
|
2023-11-27 22:07:27 +03:00
|
|
|
block_offset = 1;
|
2023-11-27 20:48:51 +03:00
|
|
|
}
|
|
|
|
|
2023-11-27 22:07:27 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, dupn, FIXNUM_INC(argc, 1 + block_offset));
|
2023-11-27 20:48:51 +03:00
|
|
|
|
|
|
|
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idAREF, argc, INT2FIX(flag));
|
|
|
|
|
|
|
|
PM_COMPILE_NOT_POPPED(index_operator_write_node->value);
|
|
|
|
|
|
|
|
ID method_id = pm_constant_id_lookup(scope_node, index_operator_write_node->operator);
|
|
|
|
ADD_SEND(ret, &dummy_line_node, method_id, INT2FIX(1));
|
|
|
|
|
2023-11-27 22:07:27 +03:00
|
|
|
pm_compile_index_write_nodes_add_send(popped, ret, iseq, dummy_line_node, argc, flag, block_offset);
|
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_TARGET_NODE: {
|
|
|
|
pm_instance_variable_target_node_t *write_node = (pm_instance_variable_target_node_t *) node;
|
2023-09-18 23:58:10 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
ID ivar_name = pm_constant_id_lookup(scope_node, write_node->name);
|
2023-09-18 23:58:10 +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_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) {
|
2023-09-27 19:39:53 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, parse_integer((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
|
|
|
|
|
|
|
pm_interpolated_node_compile(&cast->parts, iseq, dummy_line_node, ret, src, popped, scope_node, parser);
|
2023-09-28 17:18:43 +03:00
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, toregexp, INT2FIX(pm_reg_flags(node)), INT2FIX((int) (cast->parts.size)));
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, getspecial, INT2FIX(0), INT2FIX(0));
|
|
|
|
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;
|
|
|
|
pm_scope_node_init((pm_node_t*)node, &next_scope_node, scope_node, parser);
|
|
|
|
|
|
|
|
block_iseq = NEW_CHILD_ISEQ(next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
|
|
|
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
|
|
|
|
|
|
|
int parts_size = (int)cast->parts.size;
|
|
|
|
if (cast->parts.size > 0 && !PM_NODE_TYPE_P(cast->parts.nodes[0], PM_STRING_NODE)) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_str_new(0, 0));
|
|
|
|
parts_size++;
|
|
|
|
}
|
|
|
|
|
2023-11-14 03:03:24 +03:00
|
|
|
pm_interpolated_node_compile(&cast->parts, iseq, dummy_line_node, ret, src, popped, scope_node, parser);
|
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;
|
2023-11-14 03:03:24 +03:00
|
|
|
pm_interpolated_node_compile(&interp_string_node->parts, iseq, dummy_line_node, ret, src, popped, scope_node, parser);
|
2023-09-08 19:09:30 +03:00
|
|
|
|
|
|
|
size_t parts_size = interp_string_node->parts.size;
|
|
|
|
if (parts_size > 1) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, concatstrings, INT2FIX((int)(parts_size)));
|
|
|
|
}
|
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;
|
2023-11-14 03:03:24 +03:00
|
|
|
pm_interpolated_node_compile(&interp_symbol_node->parts, iseq, dummy_line_node, ret, src, popped, scope_node, parser);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-09-08 19:09:30 +03:00
|
|
|
size_t parts_size = interp_symbol_node->parts.size;
|
|
|
|
if (parts_size > 1) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, concatstrings, INT2FIX((int)(parts_size)));
|
|
|
|
}
|
|
|
|
|
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;
|
2023-11-14 03:03:24 +03:00
|
|
|
pm_interpolated_node_compile(&interp_x_string_node->parts, iseq, dummy_line_node, ret, src, false, scope_node, parser);
|
2023-09-08 17:40:07 +03:00
|
|
|
|
2023-09-08 19:09:30 +03:00
|
|
|
size_t parts_size = interp_x_string_node->parts.size;
|
|
|
|
if (parts_size > 1) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, concatstrings, INT2FIX((int)(parts_size)));
|
|
|
|
}
|
|
|
|
|
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: {
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init(node, &next_scope_node, scope_node, parser);
|
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-18 21:18:37 +03:00
|
|
|
const rb_iseq_t *block = NEW_CHILD_ISEQ(next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, 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
|
|
|
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;
|
2023-11-30 14:05:26 +03:00
|
|
|
int depth = local_variable_and_write_node->depth + scope_node->local_depth_offset;
|
2023-10-17 01:36:25 +03:00
|
|
|
int local_index = pm_lookup_local_index_with_depth(iseq, scope_node, constant_id, depth);
|
2023-11-30 14:05:26 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, local_index, depth);
|
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
|
|
|
|
2023-11-30 14:05:26 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, local_index, depth);
|
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;
|
2023-09-02 00:20:03 +03:00
|
|
|
|
2023-11-30 14:05:26 +03:00
|
|
|
int depth = local_variable_operator_write_node->depth + scope_node->local_depth_offset;
|
2023-10-17 01:36:25 +03:00
|
|
|
int local_index = pm_lookup_local_index_with_depth(iseq, scope_node, constant_id, depth);
|
2023-11-30 14:05:26 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, local_index, depth);
|
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
|
|
|
|
2023-11-30 14:05:26 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, local_index, depth);
|
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;
|
2023-11-30 14:05:26 +03:00
|
|
|
int depth = local_variable_or_write_node->depth + scope_node->local_depth_offset;
|
2023-10-17 01:36:25 +03:00
|
|
|
int local_index = pm_lookup_local_index_with_depth(iseq, scope_node, constant_id, depth);
|
2023-11-30 14:05:26 +03:00
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, local_index, depth);
|
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
|
|
|
|
2023-11-30 14:05:26 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, local_index, depth);
|
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) {
|
2023-11-30 14:05:26 +03:00
|
|
|
int index = pm_lookup_local_index_with_depth(iseq, scope_node, local_read_node->name, local_read_node->depth);
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, index, local_read_node->depth + scope_node->local_depth_offset);
|
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_TARGET_NODE: {
|
|
|
|
pm_local_variable_target_node_t *local_write_node = (pm_local_variable_target_node_t *) node;
|
2023-09-18 23:58:10 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_constant_id_t constant_id = local_write_node->name;
|
2023-11-30 14:05:26 +03:00
|
|
|
int index = pm_lookup_local_index_any_scope(iseq, scope_node, constant_id);
|
2023-09-18 23:58:10 +03:00
|
|
|
|
2023-11-30 14:05:26 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index, local_write_node->depth + scope_node->local_depth_offset);
|
2023-09-18 23:58:10 +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
|
|
|
|
2023-11-30 14:05:26 +03:00
|
|
|
int index = pm_lookup_local_index_any_scope(iseq, scope_node, constant_id);
|
2023-11-29 22:12:31 +03:00
|
|
|
|
2023-11-30 14:05:26 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index, local_write_node->depth + scope_node->local_depth_offset);
|
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
|
|
|
|
2023-10-19 23:50:21 +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));
|
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);
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_pattern(iseq, cast->pattern, ret, src, scope_node, matched_label, unmatched_label, false);
|
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-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];
|
|
|
|
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;
|
|
|
|
int index = pm_lookup_local_index(iseq, scope_node, local_target->name);
|
|
|
|
|
|
|
|
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);
|
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index, (int) local_target->depth);
|
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];
|
|
|
|
assert(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
|
|
|
|
|
|
|
|
pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target;
|
|
|
|
int index = pm_lookup_local_index(iseq, scope_node, local_target->name);
|
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));
|
2023-11-21 00:09:51 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index, (int) local_target->depth);
|
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];
|
|
|
|
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;
|
|
|
|
int index = pm_lookup_local_index(iseq, scope_node, local_target->name);
|
2023-09-28 18:55:43 +03:00
|
|
|
|
2023-10-23 16:46:37 +03:00
|
|
|
PM_PUTNIL;
|
2023-11-21 00:09:51 +03:00
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index, (int) local_target->depth);
|
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.
|
|
|
|
PM_POP_IF_POPPED;
|
2023-09-21 21:52:24 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
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;
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init((pm_node_t *)module_node, &next_scope_node, scope_node, parser);
|
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)));
|
|
|
|
|
2023-10-18 21:18:37 +03:00
|
|
|
const rb_iseq_t *module_iseq = NEW_CHILD_ISEQ(next_scope_node, module_name, ISEQ_TYPE_CLASS, 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
|
|
|
|
|
|
|
const int flags = VM_DEFINECLASS_TYPE_MODULE |
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_class_path(ret, iseq, module_node->constant_path, &dummy_line_node, src, 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;
|
|
|
|
int index = pm_lookup_local_index(iseq, scope_node, required_parameter_node->name);
|
|
|
|
|
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index, 0);
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_MULTI_TARGET_NODE: {
|
|
|
|
pm_multi_target_node_t *cast = (pm_multi_target_node_t *) node;
|
2023-12-11 22:46:24 +03:00
|
|
|
bool has_rest_expression = (cast->rest &&
|
|
|
|
PM_NODE_TYPE_P(cast->rest, PM_SPLAT_NODE) &&
|
|
|
|
(((pm_splat_node_t *)cast->rest)->expression));
|
2023-11-01 21:22:08 +03:00
|
|
|
|
|
|
|
if (cast->lefts.size) {
|
2023-12-11 22:46:24 +03:00
|
|
|
int flag = (int) (bool) cast->rights.size || has_rest_expression;
|
2023-11-01 21:22:08 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(cast->lefts.size), INT2FIX(flag));
|
|
|
|
for (size_t index = 0; index < cast->lefts.size; index++) {
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->lefts.nodes[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 23:17:48 +03:00
|
|
|
if (has_rest_expression) {
|
|
|
|
if (cast->rights.size) {
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(cast->rights.size), INT2FIX(3));
|
|
|
|
}
|
|
|
|
pm_node_t *expression = ((pm_splat_node_t *)cast->rest)->expression;
|
|
|
|
PM_COMPILE_NOT_POPPED(expression);
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:22:08 +03:00
|
|
|
if (cast->rights.size) {
|
2023-12-11 23:17:48 +03:00
|
|
|
if (!has_rest_expression) {
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(cast->rights.size), INT2FIX(2));
|
|
|
|
}
|
2023-11-01 21:22:08 +03:00
|
|
|
for (size_t index = 0; index < cast->rights.size; index++) {
|
|
|
|
PM_COMPILE_NOT_POPPED(cast->rights.nodes[index]);
|
|
|
|
}
|
2023-09-19 23:59:07 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_MULTI_WRITE_NODE: {
|
|
|
|
pm_multi_write_node_t *multi_write_node = (pm_multi_write_node_t *)node;
|
2023-11-01 21:22:08 +03:00
|
|
|
pm_node_list_t *lefts = &multi_write_node->lefts;
|
|
|
|
pm_node_list_t *rights = &multi_write_node->rights;
|
2023-09-19 23:59:07 +03:00
|
|
|
|
|
|
|
// pre-process the left hand side of multi-assignments.
|
|
|
|
uint8_t pushed = 0;
|
2023-11-01 21:22:08 +03:00
|
|
|
for (size_t index = 0; index < lefts->size; index++) {
|
|
|
|
pushed = pm_compile_multi_write_lhs(iseq, dummy_line_node, lefts->nodes[index], ret, scope_node, pushed, false);
|
2023-09-19 23:59:07 +03:00
|
|
|
}
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(multi_write_node->value);
|
2023-10-26 18:03:18 +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-11-28 19:33:40 +03:00
|
|
|
pm_node_t *rest_expression = NULL;
|
2023-11-29 23:50:46 +03:00
|
|
|
if (multi_write_node->rest && PM_NODE_TYPE_P(multi_write_node->rest, PM_SPLAT_NODE)) {
|
2023-11-28 19:33:40 +03:00
|
|
|
pm_splat_node_t *rest_splat = ((pm_splat_node_t *)multi_write_node->rest);
|
|
|
|
rest_expression = rest_splat->expression;
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:22:08 +03:00
|
|
|
if (lefts->size) {
|
2023-11-28 19:33:40 +03:00
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(lefts->size), INT2FIX((int) (bool) (rights->size || rest_expression)));
|
2023-11-01 21:22:08 +03:00
|
|
|
for (size_t index = 0; index < lefts->size; index++) {
|
|
|
|
pm_node_t *considered_node = lefts->nodes[index];
|
2023-09-19 23:59:07 +03:00
|
|
|
|
2023-11-01 21:22:08 +03:00
|
|
|
if (PM_NODE_TYPE_P(considered_node, PM_CONSTANT_PATH_TARGET_NODE) && pushed > 0) {
|
|
|
|
pm_constant_path_target_node_t *cast = (pm_constant_path_target_node_t *) considered_node;
|
|
|
|
ID name = pm_constant_id_lookup(scope_node, ((pm_constant_read_node_t * ) cast->child)->name);
|
2023-09-19 23:59:07 +03:00
|
|
|
|
2023-11-01 21:22:08 +03:00
|
|
|
pushed -= 2;
|
2023-09-19 23:59:07 +03:00
|
|
|
|
2023-11-01 21:22:08 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(pushed));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setconstant, ID2SYM(name));
|
|
|
|
} else {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(lefts->nodes[index]);
|
2023-11-01 21:22:08 +03:00
|
|
|
}
|
2023-09-19 23:59:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pushed) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(pushed));
|
|
|
|
for (uint8_t index = 0; index < pushed; index++) {
|
2023-10-24 23:08:50 +03:00
|
|
|
PM_POP;
|
2023-09-19 23:59: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
|
|
|
}
|
|
|
|
|
2023-11-28 19:33:40 +03:00
|
|
|
if (rights->size) {
|
|
|
|
if (rest_expression) {
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(rights->size), INT2FIX(3));
|
|
|
|
PM_COMPILE(rest_expression);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, expandarray, INT2FIX(rights->size), INT2FIX(2));
|
2023-11-01 21:22:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t index = 0; index < rights->size; index++) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(rights->nodes[index]);
|
2023-11-01 21:22:08 +03:00
|
|
|
}
|
|
|
|
}
|
2023-11-28 19:33:40 +03:00
|
|
|
else if (rest_expression) {
|
|
|
|
PM_COMPILE(rest_expression);
|
|
|
|
}
|
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;
|
|
|
|
}
|
2023-12-11 20:14:21 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 0, src, 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;
|
|
|
|
}
|
|
|
|
|
2023-12-11 20:14:21 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 0, src, 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) {
|
|
|
|
rb_raise(rb_eArgError, "Can't escape from eval with next");
|
|
|
|
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:
|
|
|
|
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: {
|
|
|
|
pm_or_node_t *or_node = (pm_or_node_t *) node;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
LABEL *end_label = NEW_LABEL(lineno);
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(or_node->left);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_DUP_UNLESS_POPPED;
|
2023-08-29 19:27:00 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, end_label);
|
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_POP_UNLESS_POPPED;
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(or_node->right);
|
2023-08-29 19:27:00 +03:00
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_OPTIONAL_PARAMETER_NODE: {
|
|
|
|
pm_optional_parameter_node_t *optional_parameter_node = (pm_optional_parameter_node_t *)node;
|
|
|
|
PM_COMPILE_NOT_POPPED(optional_parameter_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-10-17 01:36:25 +03:00
|
|
|
int index = pm_lookup_local_index(iseq, scope_node, optional_parameter_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
|
|
|
|
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index, 0);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-10-23 17:28:04 +03:00
|
|
|
case PM_PARAMETERS_NODE: {
|
|
|
|
rb_bug("Should not ever enter a parameters node directly");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_PARENTHESES_NODE: {
|
|
|
|
pm_parentheses_node_t *parentheses_node = (pm_parentheses_node_t *) node;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
if (parentheses_node->body == NULL) {
|
2023-09-27 19:39:53 +03:00
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
2023-08-29 19:27:00 +03:00
|
|
|
} else {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(parentheses_node->body);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-10-23 16:46:37 +03:00
|
|
|
case PM_PRE_EXECUTION_NODE: {
|
|
|
|
pm_pre_execution_node_t *pre_execution_node = (pm_pre_execution_node_t *) node;
|
|
|
|
|
|
|
|
DECL_ANCHOR(pre_ex);
|
|
|
|
INIT_ANCHOR(pre_ex);
|
|
|
|
|
|
|
|
if (pre_execution_node->statements) {
|
|
|
|
pm_node_list_t node_list = pre_execution_node->statements->body;
|
|
|
|
for (size_t index = 0; index < node_list.size; index++) {
|
|
|
|
pm_compile_node(iseq, node_list.nodes[index], pre_ex, src, true, scope_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN(pre_ex, &dummy_line_node, putnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
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: {
|
|
|
|
const rb_iseq_t *child_iseq;
|
|
|
|
const rb_iseq_t *prevblock = ISEQ_COMPILE_DATA(iseq)->current_block;
|
|
|
|
|
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init(node, &next_scope_node, scope_node, parser);
|
|
|
|
|
|
|
|
child_iseq = NEW_CHILD_ISEQ(next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = child_iseq;
|
|
|
|
|
|
|
|
int is_index = ISEQ_BODY(iseq)->ise_size++;
|
|
|
|
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, once, child_iseq, INT2FIX(is_index));
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)child_iseq);
|
|
|
|
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = prevblock;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_PROGRAM_NODE: {
|
2023-10-23 17:28:04 +03:00
|
|
|
rb_bug("Should not ever enter a program node directly");
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_RANGE_NODE: {
|
|
|
|
pm_range_node_t *range_node = (pm_range_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
|
|
|
bool exclusive = (range_node->operator_loc.end - range_node->operator_loc.start) == 3;
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-09-27 19:39:53 +03:00
|
|
|
if (pm_optimizable_range_item_p(range_node->left) && pm_optimizable_range_item_p(range_node->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) {
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_node_t *left = range_node->left;
|
|
|
|
pm_node_t *right = range_node->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(
|
2023-09-27 19:39:53 +03:00
|
|
|
left && PM_NODE_TYPE_P(left, PM_INTEGER_NODE) ? parse_integer((pm_integer_node_t *) left) : Qnil,
|
|
|
|
right && PM_NODE_TYPE_P(right, PM_INTEGER_NODE) ? parse_integer((pm_integer_node_t *) right) : 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
|
|
|
exclusive
|
|
|
|
);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, val);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (range_node->left == NULL) {
|
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
|
|
|
} else {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(range_node->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
|
|
|
|
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 (range_node->right == NULL) {
|
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
|
|
|
} else {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(range_node->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) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, newrange, INT2FIX(exclusive));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_RATIONAL_NODE: {
|
2023-09-08 22:33:51 +03:00
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, parse_rational(node));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_REDO_NODE: {
|
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);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, splabel);
|
|
|
|
|
|
|
|
ADD_ADJUST(ret, &dummy_line_node, ISEQ_COMPILE_DATA(iseq)->redo_label);
|
|
|
|
|
2023-12-11 20:14:21 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 0, src, scope_node);
|
2023-12-04 18:55:11 +03:00
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
|
|
|
|
ADD_ADJUST_RESTORE(ret, splabel);
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, splabel);
|
2023-12-11 20:14:21 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 0, src, scope_node);
|
2023-12-04 18:55:11 +03:00
|
|
|
ADD_ADJUST(ret, &dummy_line_node, ISEQ_COMPILE_DATA(iseq)->start_label);
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, jump, ISEQ_COMPILE_DATA(iseq)->start_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
rb_bug("Invalid redo\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
ip = ISEQ_BODY(ip)->parent_iseq;
|
|
|
|
}
|
|
|
|
if (ip != 0) {
|
|
|
|
PM_PUTNIL;
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
|
|
|
|
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_bug("Invalid redo\n");
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 00:27:08 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_REGULAR_EXPRESSION_NODE: {
|
2023-09-08 19:09:30 +03:00
|
|
|
if (!popped) {
|
2023-09-28 17:18:43 +03:00
|
|
|
pm_regular_expression_node_t *cast = (pm_regular_expression_node_t *) node;
|
2023-09-19 22:25:13 +03:00
|
|
|
|
2023-10-24 20:17:18 +03:00
|
|
|
VALUE regex = pm_new_regex(cast, parser);
|
2023-09-19 22:25:13 +03:00
|
|
|
|
2023-09-08 19:09:30 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, regex);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-11-10 17:51:14 +03:00
|
|
|
case PM_RESCUE_NODE: {
|
|
|
|
LABEL *excep_match = NEW_LABEL(lineno);
|
|
|
|
LABEL *rescue_end = NEW_LABEL(lineno);
|
|
|
|
|
2023-12-07 01:20:38 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->end_label = rescue_end;
|
|
|
|
|
2023-11-10 17:51:14 +03:00
|
|
|
pm_rescue_node_t *rescue_node = (pm_rescue_node_t *)node;
|
|
|
|
iseq_set_exception_local_table(iseq);
|
|
|
|
|
|
|
|
pm_node_list_t exception_list = rescue_node->exceptions;
|
|
|
|
if (exception_list.size > 0) {
|
|
|
|
for (size_t i = 0; i < exception_list.size; i++) {
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
|
|
|
|
PM_COMPILE(exception_list.nodes[i]);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, branchif, excep_match);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, branchif, excep_match);
|
|
|
|
}
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, jump, rescue_end);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, excep_match);
|
|
|
|
ADD_TRACE(ret, RUBY_EVENT_RESCUE);
|
|
|
|
if (rescue_node->reference) {
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, LVAR_ERRINFO, 0);
|
|
|
|
PM_COMPILE((pm_node_t *)rescue_node->reference);
|
|
|
|
}
|
2023-12-01 21:00:20 +03:00
|
|
|
|
2023-11-10 17:51:14 +03:00
|
|
|
if (rescue_node->statements) {
|
|
|
|
PM_COMPILE((pm_node_t *)rescue_node->statements);
|
|
|
|
}
|
2023-12-01 21:00:20 +03:00
|
|
|
else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
|
|
|
|
2023-11-10 17:51:14 +03:00
|
|
|
ADD_INSN(ret, &dummy_line_node, leave);
|
|
|
|
ADD_LABEL(ret, rescue_end);
|
|
|
|
|
|
|
|
if (rescue_node->consequent) {
|
|
|
|
PM_COMPILE((pm_node_t *)rescue_node->consequent);
|
|
|
|
} else {
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-12-01 17:37:10 +03:00
|
|
|
case PM_RESCUE_MODIFIER_NODE: {
|
|
|
|
pm_scope_node_t rescue_scope_node;
|
|
|
|
pm_rescue_modifier_node_t *rescue_node = (pm_rescue_modifier_node_t *)node;
|
|
|
|
pm_scope_node_init((pm_node_t *)rescue_node, &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, 1);
|
|
|
|
|
|
|
|
LABEL *lstart = NEW_LABEL(lineno);
|
|
|
|
LABEL *lend = NEW_LABEL(lineno);
|
|
|
|
LABEL *lcont = NEW_LABEL(lineno);
|
|
|
|
|
|
|
|
lstart->rescued = LABEL_RESCUE_BEG;
|
|
|
|
lend->rescued = LABEL_RESCUE_END;
|
|
|
|
ADD_LABEL(ret, lstart);
|
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *)rescue_node->expression);
|
|
|
|
ADD_LABEL(ret, lend);
|
2023-12-06 17:45:04 +03:00
|
|
|
PM_NOP;
|
2023-12-01 17:37:10 +03:00
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_RETURN_NODE: {
|
|
|
|
pm_arguments_node_t *arguments = ((pm_return_node_t *)node)->arguments;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-12-05 19:31:12 +03:00
|
|
|
if (iseq) {
|
|
|
|
enum rb_iseq_type type = ISEQ_BODY(iseq)->type;
|
|
|
|
LABEL *splabel = 0;
|
|
|
|
|
|
|
|
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-08-29 19:27:00 +03:00
|
|
|
|
2023-12-05 19:31:12 +03:00
|
|
|
switch (parent_type) {
|
|
|
|
case ISEQ_TYPE_TOP:
|
|
|
|
case ISEQ_TYPE_MAIN:
|
2023-12-07 18:08:16 +03:00
|
|
|
if (arguments) {
|
2023-12-05 19:31:12 +03:00
|
|
|
rb_warn("argument of top-level return is ignored");
|
|
|
|
}
|
|
|
|
if (parent_iseq == iseq) {
|
|
|
|
type = ISEQ_TYPE_METHOD;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-12-05 19:31:12 +03:00
|
|
|
if (type == ISEQ_TYPE_METHOD) {
|
|
|
|
splabel = NEW_LABEL(0);
|
|
|
|
ADD_LABEL(ret, splabel);
|
|
|
|
ADD_ADJUST(ret, &dummy_line_node, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments) {
|
2023-12-07 18:08:16 +03:00
|
|
|
PM_COMPILE_NOT_POPPED((pm_node_t *)arguments);
|
2023-12-05 19:31:12 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PM_PUTNIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
|
2023-12-11 20:14:21 +03:00
|
|
|
pm_add_ensure_iseq(ret, iseq, 1, src, scope_node);
|
2023-12-05 19:31:12 +03:00
|
|
|
ADD_TRACE(ret, RUBY_EVENT_RETURN);
|
|
|
|
ADD_INSN(ret, &dummy_line_node, leave);
|
|
|
|
ADD_ADJUST_RESTORE(ret, splabel);
|
|
|
|
|
|
|
|
PM_PUTNIL_UNLESS_POPPED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, throw, INT2FIX(TAG_RETURN));
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
}
|
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: {
|
|
|
|
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_RESCUE) {
|
|
|
|
PM_PUTNIL;
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, throw, INT2FIX(TAG_RETRY));
|
|
|
|
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
} else {
|
|
|
|
COMPILE_ERROR(ERROR_ARGS "Invalid retry");
|
|
|
|
rb_bug("Invalid retry");
|
|
|
|
}
|
|
|
|
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;
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
2023-12-06 18:05:14 +03:00
|
|
|
} else if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_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;
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
|
|
|
|
table_size++;
|
|
|
|
}
|
|
|
|
|
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-10-20 12:59:02 +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
|
|
|
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];
|
|
|
|
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
|
|
|
|
table_size++;
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 00:18:14 +03:00
|
|
|
}
|
|
|
|
|
2023-12-11 22:17:02 +03:00
|
|
|
// When we have a `...` as the keyword_rest, it's a forwarding_parameter_node and
|
|
|
|
// we need to leave space for 2 more locals on the locals table (`*` and `&`)
|
|
|
|
if (parameters_node && parameters_node->keyword_rest &&
|
|
|
|
PM_NODE_TYPE_P(parameters_node->keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
|
|
|
|
table_size += 2;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2023-12-11 23:17:48 +03:00
|
|
|
// We will assign these values now, if applicable, and use them for
|
|
|
|
// the ISEQs on these multis
|
|
|
|
int required_multis_hidden_index = 0;
|
|
|
|
int post_multis_hidden_index = 0;
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
// Here we figure out local table indices and insert them in to the
|
|
|
|
// index lookup table and local tables.
|
|
|
|
//
|
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^^^^^^^^^^^^
|
|
|
|
if (requireds_list && requireds_list->size) {
|
|
|
|
for (size_t i = 0; i < requireds_list->size; i++, local_index++) {
|
|
|
|
ID local;
|
|
|
|
// 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];
|
|
|
|
switch (PM_NODE_TYPE(required)) {
|
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^^^^^^^^^
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
2023-12-11 23:17:48 +03:00
|
|
|
required_multis_hidden_index = local_index;
|
2023-12-06 18:05:14 +03:00
|
|
|
local = rb_make_temporary_id(local_index);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^
|
|
|
|
case PM_REQUIRED_PARAMETER_NODE: {
|
|
|
|
pm_required_parameter_node_t * param = (pm_required_parameter_node_t *)required;
|
2023-09-28 16:47:46 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(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 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
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^^^^
|
|
|
|
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++) {
|
|
|
|
pm_constant_id_t name = ((pm_optional_parameter_node_t *)optionals_list->nodes[i])->name;
|
|
|
|
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^
|
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;
|
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;
|
|
|
|
assert(body->param.rest_start != -1);
|
2023-12-06 18:05:14 +03:00
|
|
|
|
|
|
|
pm_constant_id_t name = ((pm_rest_parameter_node_t *)parameters_node->rest)->name;
|
|
|
|
if (name) {
|
2023-12-12 17:40:16 +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
|
|
|
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
2023-12-12 17:40:16 +03:00
|
|
|
else {
|
|
|
|
// def foo(a, (b, *c, d), e = 1, *, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^
|
|
|
|
local_table_for_iseq->ids[local_index] = idMULT;
|
|
|
|
}
|
|
|
|
local_index++;
|
2023-11-30 20:57:49 +03:00
|
|
|
}
|
2023-12-01 00:18:14 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^^^^^^^^^^^^
|
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;
|
|
|
|
// 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 *post_node = posts_list->nodes[i];
|
|
|
|
switch (PM_NODE_TYPE(post_node)) {
|
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^^^^^^^^^
|
|
|
|
case PM_MULTI_TARGET_NODE: {
|
2023-12-11 23:17:48 +03:00
|
|
|
post_multis_hidden_index = local_index;
|
2023-12-06 18:05:14 +03:00
|
|
|
local = rb_make_temporary_id(local_index);
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^
|
|
|
|
case PM_REQUIRED_PARAMETER_NODE: {
|
|
|
|
pm_required_parameter_node_t * param = (pm_required_parameter_node_t *)post_node;
|
|
|
|
|
|
|
|
pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^^^^^^^
|
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-11-08 20:49:58 +03:00
|
|
|
ID *ids = calloc(keywords_list->size, sizeof(ID));
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
for (size_t i = 0; i < keywords_list->size; i++, local_index++) {
|
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
|
|
|
|
2023-11-09 16:29:44 +03:00
|
|
|
switch (PM_NODE_TYPE(keyword_parameter_node)) {
|
2023-12-06 18:05:14 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^^^
|
2023-11-07 16:10:23 +03:00
|
|
|
case 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-11-09 16:29:44 +03:00
|
|
|
if (pm_static_literal_p(value)) {
|
|
|
|
rb_ary_push(default_values, pm_static_literal_value(value, scope_node, parser));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LABEL *end_label = NEW_LABEL(nd_line(&dummy_line_node));
|
2023-11-07 16:10:23 +03:00
|
|
|
|
2023-11-09 16:29:44 +03:00
|
|
|
int index = pm_lookup_local_index(iseq, scope_node, name);
|
|
|
|
int kw_bits_idx = table_size - body->param.keyword->bits_start;
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, checkkeyword, INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1), INT2FIX(i - 1));
|
|
|
|
ADD_INSNL(ret, &dummy_line_node, branchif, end_label);
|
|
|
|
PM_COMPILE(value);
|
|
|
|
ADD_SETLOCAL(ret, &dummy_line_node, index, 0);
|
|
|
|
|
|
|
|
ADD_LABEL(ret, end_label);
|
|
|
|
rb_ary_push(default_values, complex_mark);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
2023-12-06 18:05:14 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^
|
2023-11-07 16:10:23 +03:00
|
|
|
case PM_REQUIRED_KEYWORD_PARAMETER_NODE: {
|
2023-11-09 16:29:44 +03:00
|
|
|
name = ((pm_required_keyword_parameter_node_t *)keyword_parameter_node)->name;
|
|
|
|
keyword->required_num++;
|
|
|
|
break;
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
default: {
|
2023-11-09 16:29:44 +03:00
|
|
|
rb_bug("Unexpected keyword parameter node type");
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 22:32:39 +03:00
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
ID local = pm_constant_id_lookup(scope_node, name);
|
|
|
|
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
ids[i] = local;
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->size == 1) {
|
|
|
|
body->param.flags.ambiguous_param0 = true;
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters_node) {
|
2023-12-06 18:05:14 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^^
|
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)) {
|
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **nil, &n)
|
|
|
|
// ^^^^^
|
|
|
|
case PM_NO_KEYWORDS_PARAMETER_NODE: {
|
2023-12-01 00:18:14 +03:00
|
|
|
|
2023-12-11 22:17:02 +03:00
|
|
|
body->param.flags.accepts_no_kwarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^^
|
|
|
|
case PM_KEYWORD_REST_PARAMETER_NODE: {
|
|
|
|
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
|
|
|
|
2023-12-11 22:17:02 +03:00
|
|
|
keyword->rest_start = local_index;
|
|
|
|
body->param.flags.has_kwrest = true;
|
|
|
|
|
|
|
|
pm_constant_id_t constant_id = ((pm_keyword_rest_parameter_node_t *)parameters_node->keyword_rest)->name;
|
|
|
|
if (constant_id) {
|
|
|
|
pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
local_index++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// def foo(...)
|
|
|
|
// ^^^
|
|
|
|
case PM_FORWARDING_PARAMETER_NODE: {
|
|
|
|
body->param.rest_start = local_index;
|
|
|
|
body->param.flags.has_rest = true;
|
|
|
|
ID local = idMULT;
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
local_index++;
|
|
|
|
|
|
|
|
body->param.block_start = local_index;
|
|
|
|
body->param.flags.has_block = true;
|
|
|
|
local = idAnd;
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
local_index++;
|
|
|
|
|
|
|
|
local = idDot3;
|
|
|
|
local_table_for_iseq->ids[local_index] = local;
|
|
|
|
local_index++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
rb_raise(rb_eArgError, "node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->keyword_rest)));
|
|
|
|
}
|
2023-11-07 16:10:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +03:00
|
|
|
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
|
|
|
|
// ^^
|
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
|
|
|
|
|
|
|
pm_constant_id_t name = ((pm_block_parameter_node_t *)parameters_node->block)->name;
|
|
|
|
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
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
|
|
|
|
pm_node_t *required = requireds_list->nodes[i];
|
|
|
|
if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
|
|
|
|
local_index = pm_compile_multi_assign_params((pm_multi_target_node_t *)required, index_lookup_table, local_table_for_iseq, scope_node, local_index);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
pm_node_t *post= posts_list->nodes[i];
|
|
|
|
if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
|
|
|
|
local_index = pm_compile_multi_assign_params((pm_multi_target_node_t *)post, index_lookup_table, local_table_for_iseq, scope_node, local_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set any anonymous locals for the for node
|
|
|
|
if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
|
|
|
|
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;
|
|
|
|
for (int i = 0; i < maximum; i++, local_index++) {
|
|
|
|
pm_constant_id_t constant_id = locals->ids[i];
|
|
|
|
pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//********END OF STEP 3**********
|
|
|
|
|
|
|
|
//********STEP 4**********
|
|
|
|
// Goal: fill in the method body locals
|
|
|
|
// To be explicit, these are the non-parameter locals
|
|
|
|
uint32_t locals_body_index = 0;
|
2023-11-29 00:12:19 +03:00
|
|
|
|
2023-12-01 18:51:41 +03:00
|
|
|
switch (PM_NODE_TYPE(scope_node->ast_node)) {
|
|
|
|
case PM_BLOCK_NODE: {
|
2023-12-06 18:05:14 +03:00
|
|
|
locals_body_index = ((pm_block_node_t *)scope_node->ast_node)->locals_body_index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_DEF_NODE: {
|
|
|
|
locals_body_index = ((pm_def_node_t *)scope_node->ast_node)->locals_body_index;
|
|
|
|
break;
|
2023-12-01 18:51:41 +03:00
|
|
|
}
|
|
|
|
case PM_LAMBDA_NODE: {
|
2023-12-06 18:05:14 +03:00
|
|
|
locals_body_index = ((pm_lambda_node_t *)scope_node->ast_node)->locals_body_index;
|
|
|
|
break;
|
2023-12-01 18:51:41 +03:00
|
|
|
}
|
2023-12-06 18:05:14 +03:00
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scope_node->locals.size) {
|
|
|
|
for (size_t i = locals_body_index; i < scope_node->locals.size; i++) {
|
|
|
|
pm_constant_id_t constant_id = locals->ids[i];
|
|
|
|
if (constant_id) {
|
|
|
|
pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
|
|
|
|
local_index++;
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 18:51:41 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 18:05:14 +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);
|
|
|
|
}
|
2023-11-29 00:12:19 +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
|
|
|
|
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
|
|
|
|
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++, local_index++) {
|
|
|
|
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-09-28 16:47:46 +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++) {
|
|
|
|
// 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)) {
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, table_size - required_multis_hidden_index, 0);
|
|
|
|
PM_COMPILE(required);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
pm_node_t *post = posts_list->nodes[i];
|
|
|
|
if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, table_size - post_multis_hidden_index, 0);
|
|
|
|
PM_COMPILE(post);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
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
|
|
|
|
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_TRACE(ret, RUBY_EVENT_B_CALL);
|
2023-11-07 16:10:23 +03:00
|
|
|
NODE dummy_line_node = generate_dummy_line_node(body->location.first_lineno, -1);
|
2023-12-11 18:33:52 +03:00
|
|
|
if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0) {
|
|
|
|
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
|
|
|
|
2023-08-30 00:17:08 +03:00
|
|
|
if (scope_node->body) {
|
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;
|
|
|
|
pm_scope_node_init((pm_node_t *)post_execution_node->statements, &next_scope_node, scope_node, parser);
|
|
|
|
|
2023-11-07 16:10:23 +03:00
|
|
|
const rb_iseq_t *block = NEW_CHILD_ISEQ(next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, lineno);
|
2023-10-30 23:57:27 +03:00
|
|
|
|
|
|
|
ADD_CALL_WITH_BLOCK(ret, &dummy_line_node, id_core_set_postexe, INT2FIX(0), block);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_FOR_NODE: {
|
2023-10-20 12:59:02 +03:00
|
|
|
pm_for_node_t *for_node = (pm_for_node_t *)scope_node->ast_node;
|
|
|
|
|
|
|
|
ADD_GETLOCAL(ret, &dummy_line_node, 1, 0);
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(for_node->index);
|
2023-12-06 17:45:04 +03:00
|
|
|
PM_NOP;
|
2023-11-14 02:52:03 +03:00
|
|
|
pm_compile_node(iseq, (pm_node_t *)(scope_node->body), ret, src, popped, scope_node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
|
|
|
|
pm_interpolated_regular_expression_node_t *cast = (pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
|
|
|
|
|
|
|
|
int parts_size = (int)cast->parts.size;
|
|
|
|
if (parts_size > 0 && !PM_NODE_TYPE_P(cast->parts.nodes[0], PM_STRING_NODE)) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, rb_str_new(0, 0));
|
|
|
|
parts_size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pm_interpolated_node_compile(&cast->parts, iseq, dummy_line_node, ret, src, false, scope_node, parser);
|
|
|
|
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
|
|
|
}
|
|
|
|
default: {
|
|
|
|
pm_compile_node(iseq, (pm_node_t *)(scope_node->body), ret, src, popped, scope_node);
|
|
|
|
}
|
2023-10-20 12:59:02 +03:00
|
|
|
}
|
2023-08-30 00:17:08 +03:00
|
|
|
}
|
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-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 */
|
2023-12-07 18:00:39 +03:00
|
|
|
ISEQ_COMPILE_DATA(iseq)->catch_except_p = true;
|
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_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
|
|
|
|
ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
|
|
|
|
break;
|
|
|
|
}
|
2023-11-03 02:29:20 +03:00
|
|
|
case ISEQ_TYPE_ENSURE: {
|
|
|
|
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;
|
|
|
|
}
|
2023-11-10 17:51:14 +03:00
|
|
|
case ISEQ_TYPE_RESCUE: {
|
|
|
|
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));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, branchif, lab);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, jump, rescue_end);
|
|
|
|
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;
|
|
|
|
}
|
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:
|
|
|
|
if (scope_node->body) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE((pm_node_t *)scope_node->body);
|
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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
free(index_lookup_table);
|
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)) {
|
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:
|
2023-09-07 17:59:51 +03:00
|
|
|
if (!popped) {
|
2023-10-30 19:56:30 +03:00
|
|
|
PM_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: {
|
|
|
|
pm_singleton_class_node_t *singleton_class_node = (pm_singleton_class_node_t *)node;
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init((pm_node_t *)singleton_class_node, &next_scope_node, scope_node, parser);
|
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-18 21:18:37 +03:00
|
|
|
const rb_iseq_t *singleton_class = NEW_ISEQ(next_scope_node, rb_fstring_lit("singleton class"), ISEQ_TYPE_CLASS, 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-11-02 21:04:09 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(singleton_class_node->expression);
|
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
|
|
|
ID singletonclass;
|
|
|
|
CONST_ID(singletonclass, "singletonclass");
|
|
|
|
|
|
|
|
ADD_INSN3(ret, &dummy_line_node, defineclass,
|
|
|
|
ID2SYM(singletonclass), singleton_class,
|
|
|
|
INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
|
2023-11-02 21:04:09 +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
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)singleton_class);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SOURCE_ENCODING_NODE: {
|
2023-09-28 22:19:56 +03:00
|
|
|
// Source encoding nodes are generated by the __ENCODING__ syntax. They
|
|
|
|
// reference the encoding object corresponding to the encoding of the
|
|
|
|
// source file, and can be changed by a magic encoding comment.
|
2023-08-31 19:56:13 +03:00
|
|
|
if (!popped) {
|
2023-10-19 23:50:21 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node, parser);
|
2023-09-28 22:19:56 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, value);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, value);
|
2023-08-31 19:56:13 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SOURCE_FILE_NODE: {
|
2023-09-28 22:19:56 +03:00
|
|
|
// Source file nodes are generated by the __FILE__ syntax. They
|
|
|
|
// reference the file name of the source file.
|
2023-08-31 19:56:13 +03:00
|
|
|
if (!popped) {
|
2023-10-19 23:50:21 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node, parser);
|
2023-09-28 22:19:56 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putstring, value);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, value);
|
2023-08-31 19:56:13 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SOURCE_LINE_NODE: {
|
2023-09-28 22:19:56 +03:00
|
|
|
// Source line nodes are generated by the __LINE__ syntax. They
|
|
|
|
// reference the line number where they occur in the source file.
|
2023-08-31 19:56:13 +03:00
|
|
|
if (!popped) {
|
2023-10-19 23:50:21 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node, parser);
|
2023-09-28 22:19:56 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, value);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, value);
|
2023-08-31 19:56:13 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SPLAT_NODE: {
|
|
|
|
pm_splat_node_t *splat_node = (pm_splat_node_t *)node;
|
2023-10-13 20:59:37 +03:00
|
|
|
if (splat_node->expression) {
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(splat_node->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) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, splatarray, Qtrue);
|
|
|
|
}
|
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: {
|
|
|
|
pm_statements_node_t *statements_node = (pm_statements_node_t *) node;
|
|
|
|
pm_node_list_t node_list = statements_node->body;
|
2023-09-07 17:59:51 +03:00
|
|
|
if (node_list.size > 0) {
|
2023-10-17 01:36:25 +03:00
|
|
|
for (size_t index = 0; index < node_list.size - 1; index++) {
|
|
|
|
PM_COMPILE_POPPED(node_list.nodes[index]);
|
|
|
|
}
|
2023-11-03 02:29:20 +03:00
|
|
|
PM_COMPILE(node_list.nodes[node_list.size - 1]);
|
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-08-29 19:27:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_STRING_NODE: {
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
2023-12-06 20:54:16 +03:00
|
|
|
pm_string_node_t *cast = (pm_string_node_t *) node;
|
|
|
|
VALUE value = parse_string_encoded(node, &cast->unescaped, parser);
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putstring, value);
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2023-10-31 18:19:38 +03:00
|
|
|
case PM_SUPER_NODE: {
|
|
|
|
pm_super_node_t *super_node = (pm_super_node_t *) node;
|
|
|
|
|
|
|
|
DECL_ANCHOR(args);
|
|
|
|
|
|
|
|
int flags = 0;
|
|
|
|
struct rb_callinfo_kwarg *keywords = NULL;
|
|
|
|
const rb_iseq_t *parent_block = ISEQ_COMPILE_DATA(iseq)->current_block;
|
|
|
|
|
|
|
|
INIT_ANCHOR(args);
|
|
|
|
ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
|
|
|
|
|
2023-12-12 19:08:40 +03:00
|
|
|
PM_PUTSELF;
|
2023-10-31 18:19:38 +03:00
|
|
|
|
|
|
|
int argc = pm_setup_args(super_node->arguments, &flags, &keywords, iseq, ret, src, popped, scope_node, dummy_line_node, parser);
|
|
|
|
|
|
|
|
flags |= VM_CALL_SUPER | VM_CALL_FCALL;
|
|
|
|
|
|
|
|
if (super_node->block) {
|
|
|
|
switch (PM_NODE_TYPE(super_node->block)) {
|
|
|
|
case PM_BLOCK_ARGUMENT_NODE: {
|
|
|
|
PM_COMPILE_NOT_POPPED(super_node->block);
|
|
|
|
flags |= VM_CALL_ARGS_BLOCKARG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PM_BLOCK_NODE: {
|
|
|
|
pm_scope_node_t next_scope_node;
|
|
|
|
pm_scope_node_init(super_node->block, &next_scope_node, scope_node, parser);
|
|
|
|
parent_block = NEW_CHILD_ISEQ(next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
rb_bug("This node type should never occur on a SuperNode's block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_SEQ(ret, args);
|
|
|
|
ADD_INSN2(ret, &dummy_line_node, invokesuper,
|
|
|
|
new_callinfo(iseq, 0, argc, flags, keywords, parent_block != NULL),
|
|
|
|
parent_block);
|
|
|
|
|
|
|
|
PM_POP_IF_POPPED;
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_SYMBOL_NODE: {
|
2023-09-28 22:19:56 +03:00
|
|
|
// Symbols nodes are symbol literals with no interpolation. They are
|
|
|
|
// always marked as static literals.
|
2023-09-07 17:59:51 +03:00
|
|
|
if (!popped) {
|
2023-10-19 23:50:21 +03:00
|
|
|
VALUE value = pm_static_literal_value(node, scope_node, parser);
|
2023-09-28 22:19:56 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, value);
|
|
|
|
RB_OBJ_WRITTEN(iseq, Qundef, 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:
|
2023-08-29 19:27:00 +03:00
|
|
|
if (!popped) {
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
|
|
|
|
}
|
|
|
|
return;
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_UNDEF_NODE: {
|
|
|
|
pm_undef_node_t *undef_node = (pm_undef_node_t *) node;
|
2023-08-29 19:27:00 +03:00
|
|
|
|
|
|
|
for (size_t index = 0; index < undef_node->names.size; index++) {
|
2023-09-07 17:59:51 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
|
|
|
|
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-10-13 20:59:37 +03:00
|
|
|
PM_COMPILE_NOT_POPPED(undef_node->names.nodes[index]);
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-09-22 18:43:00 +03:00
|
|
|
ADD_SEND(ret, &dummy_line_node, id_core_undef_method, INT2NUM(2));
|
2023-08-29 19:27:00 +03:00
|
|
|
|
2023-10-24 23:08:50 +03:00
|
|
|
if (index < undef_node->names.size - 1) {
|
|
|
|
PM_POP;
|
|
|
|
}
|
2023-08-29 19:27:00 +03:00
|
|
|
}
|
|
|
|
|
2023-10-13 20:59:37 +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_UNLESS_NODE: {
|
|
|
|
const int line = (int)pm_newline_list_line_column(&(parser->newline_list), node->location.start).line;
|
|
|
|
pm_unless_node_t *unless_node = (pm_unless_node_t *)node;
|
2023-10-20 18:03:02 +03:00
|
|
|
pm_node_t *node_body = (pm_node_t *)(unless_node->statements);
|
|
|
|
pm_statements_node_t *node_else = NULL;
|
|
|
|
if (unless_node->consequent != NULL) {
|
|
|
|
node_else = ((pm_else_node_t *)unless_node->consequent)->statements;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
pm_node_t *predicate = unless_node->predicate;
|
2023-08-30 00:17:08 +03:00
|
|
|
|
2023-10-20 18:03:02 +03:00
|
|
|
pm_compile_if(iseq, line, node_else, node_body, predicate, ret, src, popped, scope_node);
|
2023-08-30 00:17:08 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_UNTIL_NODE: {
|
|
|
|
pm_until_node_t *until_node = (pm_until_node_t *)node;
|
|
|
|
pm_statements_node_t *statements = until_node->statements;
|
|
|
|
pm_node_t *predicate = until_node->predicate;
|
|
|
|
pm_node_flags_t flags = node->flags;
|
2023-08-30 00:17:08 +03:00
|
|
|
|
2023-10-17 01:36:25 +03:00
|
|
|
pm_compile_while(iseq, lineno, flags, node->type, statements, predicate, ret, src, 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-10-24 21:24:36 +03:00
|
|
|
case PM_WHEN_NODE: {
|
|
|
|
rb_bug("Should not ever enter a when node directly");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:39:53 +03:00
|
|
|
case PM_WHILE_NODE: {
|
|
|
|
pm_while_node_t *while_node = (pm_while_node_t *)node;
|
|
|
|
pm_statements_node_t *statements = while_node->statements;
|
|
|
|
pm_node_t *predicate = while_node->predicate;
|
|
|
|
pm_node_flags_t flags = node->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-10-17 01:36:25 +03:00
|
|
|
pm_compile_while(iseq, lineno, flags, node->type, statements, predicate, ret, src, 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: {
|
2023-12-06 20:54:16 +03:00
|
|
|
pm_x_string_node_t *cast = (pm_x_string_node_t *) node;
|
|
|
|
VALUE value = parse_string_encoded(node, &cast->unescaped, parser);
|
|
|
|
|
2023-10-30 19:56:30 +03:00
|
|
|
PM_PUTSELF;
|
2023-12-06 20:54:16 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, putobject, value);
|
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-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_YIELD_NODE: {
|
2023-11-07 16:10:23 +03:00
|
|
|
pm_yield_node_t *yield_node = (pm_yield_node_t *)node;
|
|
|
|
|
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;
|
|
|
|
|
2023-11-07 16:10:23 +03:00
|
|
|
int argc = 0;
|
|
|
|
|
|
|
|
if (yield_node->arguments) {
|
2023-11-27 23:55:15 +03:00
|
|
|
argc = pm_setup_args(yield_node->arguments, &flags, &keywords, iseq, ret, src, popped, scope_node, dummy_line_node, parser);
|
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-11-27 23:55:15 +03:00
|
|
|
ADD_INSN1(ret, &dummy_line_node, invokeblock, new_callinfo(iseq, 0, argc, flags, keywords, 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
|
|
|
|
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
|
|
|
|
|
|
|
int level = 0;
|
|
|
|
const rb_iseq_t *tmp_iseq = iseq;
|
|
|
|
for (; tmp_iseq != ISEQ_BODY(iseq)->local_iseq; level++ ) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2023-10-30 16:49:17 +03:00
|
|
|
rb_translate_prism(pm_parser_t *parser, rb_iseq_t *iseq, pm_scope_node_t *scope_node, LINK_ANCHOR *const ret)
|
2023-08-28 23:55:58 +03:00
|
|
|
{
|
|
|
|
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
|
|
|
|
|
2023-10-30 16:49:17 +03:00
|
|
|
ID *constants = calloc(parser->constant_pool.size, sizeof(ID));
|
2023-12-06 20:54:16 +03:00
|
|
|
rb_encoding *encoding = rb_enc_find(parser->encoding->name);
|
2023-10-30 16:49:17 +03:00
|
|
|
for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
|
|
|
|
pm_constant_t *constant = &parser->constant_pool.constants[index];
|
|
|
|
constants[index] = rb_intern3((const char *) constant->start, constant->length, encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
st_table *index_lookup_table = st_init_numtable();
|
|
|
|
pm_constant_id_list_t *locals = &scope_node->locals;
|
|
|
|
for (size_t i = 0; i < locals->size; i++) {
|
|
|
|
st_insert(index_lookup_table, locals->ids[i], i);
|
|
|
|
}
|
2023-11-09 10:29:49 +03:00
|
|
|
scope_node->constants = constants;
|
2023-10-30 16:49:17 +03:00
|
|
|
scope_node->index_lookup_table = index_lookup_table;
|
|
|
|
|
2023-10-19 00:53:02 +03:00
|
|
|
pm_compile_node(iseq, (pm_node_t *)scope_node, ret, scope_node->base.location.start, false, (pm_scope_node_t *)scope_node);
|
2023-08-28 23:55:58 +03:00
|
|
|
iseq_set_sequence(iseq, ret);
|
2023-10-30 16:49:17 +03:00
|
|
|
|
|
|
|
free(constants);
|
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
|
|
|
|
|
|
|
#undef NEW_ISEQ
|
|
|
|
#define NEW_ISEQ OLD_ISEQ
|
|
|
|
|
|
|
|
#undef NEW_CHILD_ISEQ
|
|
|
|
#define NEW_CHILD_ISEQ OLD_CHILD_ISEQ
|