summaryrefslogtreecommitdiff
path: root/prism_compile.c
diff options
context:
space:
mode:
Diffstat (limited to 'prism_compile.c')
-rw-r--r--prism_compile.c7840
1 files changed, 4506 insertions, 3334 deletions
diff --git a/prism_compile.c b/prism_compile.c
index 14cf016f16..45e1de8a9c 100644
--- a/prism_compile.c
+++ b/prism_compile.c
@@ -1,9 +1,12 @@
#include "prism.h"
+#include "ruby/version.h"
+
+#include <fcntl.h>
/**
* This compiler defines its own concept of the location of a node. We do this
* because we want to pair line information with node identifier so that we can
- * have reproducable parses.
+ * have reproducible parses.
*/
typedef struct {
/** This is the line number of a node. */
@@ -101,6 +104,7 @@ pm_iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line, int node
else {
ADD_ELEM(seq, (LINK_ELEMENT *) new_insn_body(iseq, line, node_id, BIN(setlocal), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
}
+ update_lvar_state(iseq, level, idx);
if (level > 0) access_outer_variables(iseq, level, iseq_lvar_id(iseq, idx, level), Qtrue);
}
@@ -138,39 +142,127 @@ pm_iseq_add_setlocal(rb_iseq_t *iseq, LINK_ANCHOR *const seq, int line, int node
#define PM_COMPILE_NOT_POPPED(node) \
pm_compile_node(iseq, (node), ret, false, scope_node)
-#define PM_SPECIAL_CONSTANT_FLAG ((pm_constant_id_t)(1 << 31))
-#define PM_CONSTANT_AND ((pm_constant_id_t)(idAnd | PM_SPECIAL_CONSTANT_FLAG))
-#define PM_CONSTANT_DOT3 ((pm_constant_id_t)(idDot3 | PM_SPECIAL_CONSTANT_FLAG))
-#define PM_CONSTANT_MULT ((pm_constant_id_t)(idMULT | PM_SPECIAL_CONSTANT_FLAG))
-#define PM_CONSTANT_POW ((pm_constant_id_t)(idPow | PM_SPECIAL_CONSTANT_FLAG))
+// Direct-indexed lookup table. -1 means "not present".
+#define PM_INDEX_LOOKUP_TABLE_INIT { .values = NULL, .capacity = 0, .owned = false }
-#define PM_NODE_START_LOCATION(parser, node) \
- ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
+static inline void
+pm_index_lookup_table_init(pm_index_lookup_table_t *table, int constants_size, rb_iseq_t *iseq)
+{
+ int capacity = constants_size + PM_INDEX_LOOKUP_SPECIALS;
+ table->values = compile_data_alloc2_type(iseq, int, capacity);
+ memset(table->values, -1, capacity * sizeof(int));
+ table->capacity = capacity;
+ table->owned = false;
+}
-#define PM_NODE_END_LOCATION(parser, node) \
- ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line), .node_id = ((const pm_node_t *) (node))->node_id })
+/**
+ * Cached line lookup that avoids repeated binary searches. Since the compiler
+ * walks the AST roughly in source order, consecutive lookups tend to be for
+ * nearby byte offsets. We cache the last result index in the scope node and
+ * try a short linear probe from there before falling back to binary search.
+ */
+static inline pm_line_column_t
+pm_line_offset_list_line_column_cached(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line, size_t *last_line)
+{
+ size_t hint = *last_line;
+ size_t size = list->size;
+ const uint32_t *offsets = list->offsets;
-#define PM_LOCATION_START_LOCATION(parser, location, id) \
- ((pm_node_location_t) { .line = pm_newline_list_line(&(parser)->newline_list, (location)->start, (parser)->start_line), .node_id = id })
+ RUBY_ASSERT(hint < size);
-#define PM_NODE_START_LINE_COLUMN(parser, node) \
- pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.start, (parser)->start_line)
+ /* Check if the cursor is on the same line as the hint. */
+ if (offsets[hint] <= cursor) {
+ if (hint + 1 >= size || offsets[hint + 1] > cursor) {
+ *last_line = hint;
+ return ((pm_line_column_t) {
+ .line = ((int32_t) hint) + start_line,
+ .column = cursor - offsets[hint]
+ });
+ }
-#define PM_NODE_END_LINE_COLUMN(parser, node) \
- pm_newline_list_line_column(&(parser)->newline_list, ((const pm_node_t *) (node))->location.end, (parser)->start_line)
+ /* Linear scan forward (up to 8 lines before giving up). */
+ size_t limit = hint + 9;
+ if (limit > size) limit = size;
+ for (size_t idx = hint + 1; idx < limit; idx++) {
+ if (offsets[idx] > cursor) {
+ *last_line = idx - 1;
+ return ((pm_line_column_t) {
+ .line = ((int32_t) (idx - 1)) + start_line,
+ .column = cursor - offsets[idx - 1]
+ });
+ }
+ if (offsets[idx] == cursor) {
+ *last_line = idx;
+ return ((pm_line_column_t) { ((int32_t) idx) + start_line, 0 });
+ }
+ }
+ }
+ else {
+ /* Linear scan backward (up to 8 lines before giving up). */
+ size_t limit = hint > 8 ? hint - 8 : 0;
+ for (size_t idx = hint; idx > limit; idx--) {
+ if (offsets[idx - 1] <= cursor) {
+ *last_line = idx - 1;
+ return ((pm_line_column_t) {
+ .line = ((int32_t) (idx - 1)) + start_line,
+ .column = cursor - offsets[idx - 1]
+ });
+ }
+ }
+ }
-#define PM_LOCATION_START_LINE_COLUMN(parser, location) \
- pm_newline_list_line_column(&(parser)->newline_list, (location)->start, (parser)->start_line)
+ /* Fall back to binary search. */
+ pm_line_column_t result = pm_line_offset_list_line_column(list, cursor, start_line);
+ *last_line = (size_t) (result.line - start_line);
+ return result;
+}
-static int
-pm_node_line_number(const pm_parser_t *parser, const pm_node_t *node)
+/**
+ * The same as pm_line_offset_list_line_column_cached, but returning only the
+ * line number.
+ */
+static inline int32_t
+pm_line_offset_list_line_cached(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line, size_t *last_line)
{
- return (int) pm_newline_list_line(&parser->newline_list, node->location.start, parser->start_line);
+ return pm_line_offset_list_line_column_cached(list, cursor, start_line, last_line).line;
}
+#define PM_NODE_START_LOCATION(node) \
+ ((pm_node_location_t) { .line = pm_line_offset_list_line_cached(scope_node->line_offsets, ((const pm_node_t *) (node))->location.start, scope_node->start_line, &scope_node->last_line), .node_id = ((const pm_node_t *) (node))->node_id })
+
+#define PM_NODE_END_LOCATION(node) \
+ ((pm_node_location_t) { .line = pm_line_offset_list_line_cached(scope_node->line_offsets, ((const pm_node_t *) (node))->location.start + ((const pm_node_t *) (node))->location.length, scope_node->start_line, &scope_node->last_line), .node_id = ((const pm_node_t *) (node))->node_id })
+
+#define PM_LOCATION_START_LOCATION(location, id) \
+ ((pm_node_location_t) { .line = pm_line_offset_list_line_cached(scope_node->line_offsets, (location)->start, scope_node->start_line, &scope_node->last_line), .node_id = id })
+
+#define PM_NODE_START_LINE_COLUMN(node) \
+ pm_line_offset_list_line_column_cached(scope_node->line_offsets, ((const pm_node_t *) (node))->location.start, scope_node->start_line, &scope_node->last_line)
+
+#define PM_NODE_END_LINE_COLUMN(node) \
+ pm_line_offset_list_line_column_cached(scope_node->line_offsets, ((const pm_node_t *) (node))->location.start + ((const pm_node_t *) (node))->location.length, scope_node->start_line, &scope_node->last_line)
+
+#define PM_LOCATION_START_LINE_COLUMN(location) \
+ pm_line_offset_list_line_column_cached(scope_node->line_offsets, (location)->start, scope_node->start_line, &scope_node->last_line)
+
static int
pm_location_line_number(const pm_parser_t *parser, const pm_location_t *location) {
- return (int) pm_newline_list_line(&parser->newline_list, location->start, parser->start_line);
+ return (int) pm_line_offset_list_line_column(pm_parser_line_offsets(parser), location->start, pm_parser_start_line(parser)).line;
+}
+
+/**
+ * Cached variants that use the scope node's hint for fast lookups during
+ * compilation (where access patterns are roughly sequential).
+ */
+static inline int
+pm_node_line_number_cached(const pm_node_t *node, pm_scope_node_t *scope_node)
+{
+ return (int) pm_line_offset_list_line_cached(scope_node->line_offsets, node->location.start, scope_node->start_line, &scope_node->last_line);
+}
+
+static inline int
+pm_location_line_number_cached(const pm_location_t *location, pm_scope_node_t *scope_node) {
+ return (int) pm_line_offset_list_line_cached(scope_node->line_offsets, location->start, scope_node->start_line, &scope_node->last_line);
}
/**
@@ -185,24 +277,25 @@ parse_integer_value(const pm_integer_t *integer)
result = UINT2NUM(integer->value);
}
else {
- VALUE string = rb_str_new(NULL, integer->length * 8);
- unsigned char *bytes = (unsigned char *) RSTRING_PTR(string);
-
- size_t offset = integer->length * 8;
- for (size_t value_index = 0; value_index < integer->length; value_index++) {
- uint32_t value = integer->values[value_index];
-
- for (int index = 0; index < 8; index++) {
- int byte = (value >> (4 * index)) & 0xf;
- bytes[--offset] = byte < 10 ? byte + '0' : byte - 10 + 'a';
- }
- }
-
- result = rb_funcall(string, rb_intern("to_i"), 1, UINT2NUM(16));
+ // The pm_integer_t stores values as an array of uint32_t in
+ // least-significant-word-first order (base 2^32). We can convert
+ // directly to a Ruby Integer using rb_integer_unpack, avoiding the
+ // overhead of constructing a hex string and calling rb_funcall.
+ result = rb_integer_unpack(
+ integer->values,
+ integer->length,
+ sizeof(uint32_t),
+ 0,
+ INTEGER_PACK_LSWORD_FIRST | INTEGER_PACK_NATIVE_BYTE_ORDER
+ );
}
if (integer->negative) {
- result = rb_funcall(result, rb_intern("-@"), 0);
+ result = rb_int_uminus(result);
+ }
+
+ if (!SPECIAL_CONST_P(result)) {
+ RB_OBJ_SET_SHAREABLE(result); // bignum
}
return result;
@@ -223,7 +316,11 @@ parse_integer(const pm_integer_node_t *node)
static VALUE
parse_float(const pm_float_node_t *node)
{
- return DBL2NUM(node->value);
+ VALUE val = DBL2NUM(node->value);
+ if (!FLONUM_P(val)) {
+ RB_OBJ_SET_SHAREABLE(val);
+ }
+ return val;
}
/**
@@ -237,7 +334,8 @@ parse_rational(const pm_rational_node_t *node)
{
VALUE numerator = parse_integer_value(&node->numerator);
VALUE denominator = parse_integer_value(&node->denominator);
- return rb_rational_new(numerator, denominator);
+
+ return rb_ractor_make_shareable(rb_rational_new(numerator, denominator));
}
/**
@@ -264,10 +362,10 @@ parse_imaginary(const pm_imaginary_node_t *node)
break;
}
default:
- rb_bug("Unexpected numeric type on imaginary number %s\n", pm_node_type_to_str(PM_NODE_TYPE(node->numeric)));
+ rb_bug("Unexpected numeric type on imaginary number %s\n", pm_node_type(PM_NODE_TYPE(node->numeric)));
}
- return rb_complex_raw(INT2FIX(0), imaginary_part);
+ return RB_OBJ_SET_SHAREABLE(rb_complex_raw(INT2FIX(0), imaginary_part));
}
static inline VALUE
@@ -300,7 +398,7 @@ parse_string_encoded(const pm_node_t *node, const pm_string_t *string, rb_encodi
}
static inline VALUE
-parse_static_literal_string(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *string)
+parse_static_literal_string(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *string)
{
rb_encoding *encoding;
@@ -318,11 +416,8 @@ parse_static_literal_string(rb_iseq_t *iseq, const pm_scope_node_t *scope_node,
rb_enc_str_coderange(value);
if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
- int line_number = pm_node_line_number(scope_node->parser, node);
- VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq), INT2FIX(line_number));
- value = rb_str_dup(value);
- rb_ivar_set(value, id_debug_created_info, rb_ary_freeze(debug_info));
- rb_str_freeze(value);
+ int line_number = pm_node_line_number_cached(node, scope_node);
+ value = rb_ractor_make_shareable(rb_str_with_debug_created_info(value, rb_iseq_path(iseq), line_number));
}
return value;
@@ -366,7 +461,7 @@ parse_regexp_error(rb_iseq_t *iseq, int32_t line_number, const char *fmt, ...)
}
static VALUE
-parse_regexp_string_part(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding)
+parse_regexp_string_part(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding)
{
// If we were passed an explicit regexp encoding, then we need to double
// check that it's okay here for this fragment of the string.
@@ -388,12 +483,12 @@ parse_regexp_string_part(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, con
VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), encoding);
VALUE error = rb_reg_check_preprocess(string);
- if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, node), "%" PRIsVALUE, rb_obj_as_string(error));
+ if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number_cached(node, scope_node), "%" PRIsVALUE, rb_obj_as_string(error));
return string;
}
static VALUE
-pm_static_literal_concat(rb_iseq_t *iseq, const pm_node_list_t *nodes, const pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding, bool top)
+pm_static_literal_concat(rb_iseq_t *iseq, const pm_node_list_t *nodes, pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding, bool top)
{
VALUE current = Qnil;
@@ -410,7 +505,7 @@ pm_static_literal_concat(rb_iseq_t *iseq, const pm_node_list_t *nodes, const pm_
else {
string = parse_string_encoded(part, &((const pm_string_node_t *) part)->unescaped, scope_node->encoding);
VALUE error = rb_reg_check_preprocess(string);
- if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number(scope_node->parser, part), "%" PRIsVALUE, rb_obj_as_string(error));
+ if (error != Qnil) parse_regexp_error(iseq, pm_node_line_number_cached(part, scope_node), "%" PRIsVALUE, rb_obj_as_string(error));
}
}
else {
@@ -505,7 +600,7 @@ parse_regexp_flags(const pm_node_t *node)
static rb_encoding *
parse_regexp_encoding(const pm_scope_node_t *scope_node, const pm_node_t *node)
{
- if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
+ if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_FORCED_BINARY_ENCODING) || PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) {
return rb_ascii8bit_encoding();
}
else if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_UTF_8)) {
@@ -523,12 +618,12 @@ parse_regexp_encoding(const pm_scope_node_t *scope_node, const pm_node_t *node)
}
static VALUE
-parse_regexp(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, VALUE string)
+parse_regexp(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, VALUE string)
{
VALUE errinfo = rb_errinfo();
- int32_t line_number = pm_node_line_number(scope_node->parser, node);
- VALUE regexp = rb_reg_compile(string, parse_regexp_flags(node), (const char *) pm_string_source(&scope_node->parser->filepath), line_number);
+ int32_t line_number = pm_node_line_number_cached(node, scope_node);
+ VALUE regexp = rb_reg_compile(string, parse_regexp_flags(node), (const char *) pm_string_source(pm_parser_filepath(scope_node->parser)), line_number);
if (NIL_P(regexp)) {
VALUE message = rb_attr_get(rb_errinfo(), idMesg);
@@ -538,22 +633,22 @@ parse_regexp(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t
return Qnil;
}
- rb_obj_freeze(regexp);
- return regexp;
+ return RB_OBJ_SET_SHAREABLE(rb_obj_freeze(regexp));
}
static inline VALUE
-parse_regexp_literal(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped)
+parse_regexp_literal(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, const pm_string_t *unescaped)
{
rb_encoding *regexp_encoding = parse_regexp_encoding(scope_node, node);
if (regexp_encoding == NULL) regexp_encoding = scope_node->encoding;
VALUE string = rb_enc_str_new((const char *) pm_string_source(unescaped), pm_string_length(unescaped), regexp_encoding);
+ RB_OBJ_SET_SHAREABLE(string);
return parse_regexp(iseq, scope_node, node, string);
}
static inline VALUE
-parse_regexp_concat(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, const pm_node_list_t *parts)
+parse_regexp_concat(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, const pm_node_list_t *parts)
{
rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
@@ -565,7 +660,7 @@ parse_regexp_concat(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm
static void pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node);
static int
-pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding)
+pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, rb_encoding *implicit_regexp_encoding, rb_encoding *explicit_regexp_encoding, bool mutable_result, bool frozen_result)
{
int stack_size = 0;
size_t parts_size = parts->size;
@@ -594,7 +689,7 @@ pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const
}
else {
current_string = string_value;
- if (index != 0) current_location = PM_NODE_END_LOCATION(scope_node->parser, part);
+ if (index != 0) current_location = PM_NODE_END_LOCATION(part);
}
}
else {
@@ -621,7 +716,7 @@ pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const
}
else {
current_string = string_value;
- current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
+ current_location = PM_NODE_START_LOCATION(part);
}
}
else {
@@ -632,7 +727,7 @@ pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const
if (explicit_regexp_encoding != NULL) {
encoding = explicit_regexp_encoding;
}
- else if (scope_node->parser->encoding == PM_ENCODING_US_ASCII_ENTRY) {
+ else if (pm_parser_encoding_us_ascii(scope_node->parser)) {
encoding = rb_ascii8bit_encoding();
}
else {
@@ -643,19 +738,31 @@ pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const
encoding = scope_node->encoding;
}
- current_string = rb_enc_str_new(NULL, 0, encoding);
+ if (parts_size == 1) {
+ current_string = rb_enc_str_new(NULL, 0, encoding);
+ }
+ }
+
+ if (RTEST(current_string)) {
+ VALUE operand = rb_fstring(current_string);
+ PUSH_INSN1(ret, current_location, putobject, operand);
+ stack_size++;
}
- PUSH_INSN1(ret, current_location, putobject, rb_fstring(current_string));
PM_COMPILE_NOT_POPPED(part);
- const pm_node_location_t current_location = PM_NODE_START_LOCATION(scope_node->parser, part);
+ const pm_node_location_t current_location = PM_NODE_START_LOCATION(part);
PUSH_INSN(ret, current_location, dup);
- PUSH_INSN1(ret, current_location, objtostring, new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE));
+
+ {
+ const struct rb_callinfo *callinfo = new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE, NULL, FALSE);
+ PUSH_INSN1(ret, current_location, objtostring, callinfo);
+ }
+
PUSH_INSN(ret, current_location, anytostring);
current_string = Qnil;
- stack_size += 2;
+ stack_size++;
}
}
}
@@ -663,10 +770,15 @@ pm_interpolated_node_compile(rb_iseq_t *iseq, const pm_node_list_t *parts, const
if (RTEST(current_string)) {
current_string = rb_fstring(current_string);
- if (stack_size == 0 && interpolated) {
- PUSH_INSN1(ret, current_location, putstring, current_string);
- }
- else {
+ if (stack_size == 0) {
+ if (frozen_result) {
+ PUSH_INSN1(ret, current_location, putobject, current_string);
+ } else if (mutable_result || interpolated) {
+ PUSH_INSN1(ret, current_location, dupstring, current_string);
+ } else {
+ PUSH_INSN1(ret, current_location, dupchilledstring, current_string);
+ }
+ } else {
PUSH_INSN1(ret, current_location, putobject, current_string);
}
@@ -687,7 +799,7 @@ pm_compile_regexp_dynamic(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_
rb_encoding *explicit_regexp_encoding = parse_regexp_encoding(scope_node, node);
rb_encoding *implicit_regexp_encoding = explicit_regexp_encoding != NULL ? explicit_regexp_encoding : scope_node->encoding;
- int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding);
+ int length = pm_interpolated_node_compile(iseq, parts, node_location, ret, popped, scope_node, implicit_regexp_encoding, explicit_regexp_encoding, false, false);
PUSH_INSN2(ret, *node_location, toregexp, INT2FIX(parse_regexp_flags(node) & 0xFF), INT2FIX(length));
}
@@ -714,9 +826,9 @@ static VALUE
pm_static_literal_string(rb_iseq_t *iseq, VALUE string, int line_number)
{
if (ISEQ_COMPILE_DATA(iseq)->option->debug_frozen_string_literal || RTEST(ruby_debug)) {
- VALUE debug_info = rb_ary_new_from_args(2, rb_iseq_path(iseq), INT2FIX(line_number));
- rb_ivar_set(string, id_debug_created_info, rb_ary_freeze(debug_info));
- return rb_str_freeze(string);
+ VALUE str = rb_str_with_debug_created_info(string, rb_iseq_path(iseq), line_number);
+ RB_OBJ_SET_SHAREABLE(str);
+ return str;
}
else {
return rb_fstring(string);
@@ -729,7 +841,7 @@ pm_static_literal_string(rb_iseq_t *iseq, VALUE string, int line_number)
* literal values can be compiled into a literal array.
*/
static VALUE
-pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_node_t *scope_node)
+pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, pm_scope_node_t *scope_node)
{
// Every node that comes into this function should already be marked as
// static literal. If it's not, then we have a bug somewhere.
@@ -745,7 +857,7 @@ pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_n
rb_ary_push(value, pm_static_literal_value(iseq, elements->nodes[index], scope_node));
}
- OBJ_FREEZE(value);
+ RB_OBJ_SET_FROZEN_SHAREABLE(value);
return value;
}
case PM_FALSE_NODE:
@@ -764,11 +876,11 @@ pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_n
rb_ary_cat(array, pair, 2);
}
- VALUE value = rb_hash_new_with_size(elements->size);
+ VALUE value = rb_hash_alloc_fixed_size(Qfalse, elements->size);
rb_hash_bulk_insert(RARRAY_LEN(array), RARRAY_CONST_PTR(array), value);
+ RB_GC_GUARD(array);
- value = rb_obj_hide(value);
- OBJ_FREEZE(value);
+ RB_OBJ_SET_FROZEN_SHAREABLE(value);
return value;
}
case PM_IMAGINARY_NODE:
@@ -785,7 +897,7 @@ pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_n
}
case PM_INTERPOLATED_STRING_NODE: {
VALUE string = pm_static_literal_concat(iseq, &((const pm_interpolated_string_node_t *) node)->parts, scope_node, NULL, NULL, false);
- int line_number = pm_node_line_number(scope_node->parser, node);
+ int line_number = pm_node_line_number_cached(node, scope_node);
return pm_static_literal_string(iseq, string, line_number);
}
case PM_INTERPOLATED_SYMBOL_NODE: {
@@ -813,7 +925,7 @@ pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_n
return pm_source_file_value(cast, scope_node);
}
case PM_SOURCE_LINE_NODE:
- return INT2FIX(pm_node_line_number(scope_node->parser, node));
+ return INT2FIX(pm_node_line_number_cached(node, scope_node));
case PM_STRING_NODE: {
const pm_string_node_t *cast = (const pm_string_node_t *) node;
return parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
@@ -823,7 +935,7 @@ pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_n
case PM_TRUE_NODE:
return Qtrue;
default:
- rb_bug("Don't have a literal value for node type %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
+ rb_bug("Don't have a literal value for node type %s", pm_node_type(PM_NODE_TYPE(node)));
return Qfalse;
}
}
@@ -832,10 +944,10 @@ pm_static_literal_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_n
* A helper for converting a pm_location_t into a rb_code_location_t.
*/
static rb_code_location_t
-pm_code_location(const pm_scope_node_t *scope_node, const pm_node_t *node)
+pm_code_location(pm_scope_node_t *scope_node, const pm_node_t *node)
{
- const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, node);
- const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(scope_node->parser, node);
+ const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(node);
+ const pm_line_column_t end_location = PM_NODE_END_LINE_COLUMN(node);
return (rb_code_location_t) {
.beg_pos = { .lineno = start_location.line, .column = start_location.column },
@@ -852,31 +964,27 @@ pm_code_location(const pm_scope_node_t *scope_node, const pm_node_t *node)
static void
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
- LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node);
+ LABEL *then_label, LABEL *else_label, pm_scope_node_t *scope_node);
static void
-pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond, LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node)
+pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond, LABEL *then_label, LABEL *else_label, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, cond);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(cond);
DECL_ANCHOR(seq);
- INIT_ANCHOR(seq);
LABEL *label = NEW_LABEL(location.line);
if (!then_label) then_label = label;
else if (!else_label) else_label = label;
- pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, popped, scope_node);
+ pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, scope_node);
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) {
- if (popped) PUSH_INSN(ret, location, putnil);
- }
- else {
+ if (label->refcnt) {
PUSH_LABEL(seq, label);
}
@@ -887,11 +995,14 @@ pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond, LAB
static void
pm_compile_flip_flop_bound(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = { .line = ISEQ_BODY(iseq)->location.first_lineno, .node_id = -1 };
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
if (PM_NODE_TYPE_P(node, PM_INTEGER_NODE)) {
PM_COMPILE_NOT_POPPED(node);
- PUSH_INSN1(ret, location, getglobal, ID2SYM(rb_intern("$.")));
+
+ VALUE operand = ID2SYM(rb_intern("$."));
+ PUSH_INSN1(ret, location, getglobal, operand);
+
PUSH_SEND(ret, location, idEq, INT2FIX(1));
if (popped) PUSH_INSN(ret, location, pop);
}
@@ -903,7 +1014,7 @@ pm_compile_flip_flop_bound(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *
static void
pm_compile_flip_flop(const pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LABEL *then_label, rb_iseq_t *iseq, const int lineno, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = { .line = ISEQ_BODY(iseq)->location.first_lineno, .node_id = -1 };
+ const pm_node_location_t location = { .line = lineno, .node_id = -1 };
LABEL *lend = NEW_LABEL(location.line);
int again = !(flip_flop_node->base.flags & PM_RANGE_FLAGS_EXCLUDE_END);
@@ -945,22 +1056,22 @@ pm_compile_flip_flop(const pm_flip_flop_node_t *flip_flop_node, LABEL *else_labe
static void pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition);
static void
-pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond, LABEL *then_label, LABEL *else_label, bool popped, pm_scope_node_t *scope_node)
+pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond, LABEL *then_label, LABEL *else_label, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, cond);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(cond);
again:
switch (PM_NODE_TYPE(cond)) {
case PM_AND_NODE: {
const pm_and_node_t *cast = (const pm_and_node_t *) cond;
- pm_compile_logical(iseq, ret, cast->left, NULL, else_label, popped, scope_node);
+ pm_compile_logical(iseq, ret, cast->left, NULL, else_label, scope_node);
cond = cast->right;
goto again;
}
case PM_OR_NODE: {
const pm_or_node_t *cast = (const pm_or_node_t *) cond;
- pm_compile_logical(iseq, ret, cast->left, then_label, NULL, popped, scope_node);
+ pm_compile_logical(iseq, ret, cast->left, then_label, NULL, scope_node);
cond = cast->right;
goto again;
@@ -981,28 +1092,28 @@ again:
PUSH_INSNL(ret, location, jump, then_label);
return;
case PM_FLIP_FLOP_NODE:
- pm_compile_flip_flop((const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.line, ret, popped, scope_node);
+ pm_compile_flip_flop((const pm_flip_flop_node_t *) cond, else_label, then_label, iseq, location.line, ret, false, scope_node);
return;
case PM_DEFINED_NODE: {
const pm_defined_node_t *cast = (const pm_defined_node_t *) cond;
- pm_compile_defined_expr(iseq, cast->value, &location, ret, popped, scope_node, true);
+ pm_compile_defined_expr(iseq, cast->value, &location, ret, false, scope_node, true);
break;
}
default: {
DECL_ANCHOR(cond_seq);
- INIT_ANCHOR(cond_seq);
pm_compile_node(iseq, cond, cond_seq, false, scope_node);
if (LIST_INSN_SIZE_ONE(cond_seq)) {
- INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
+ INSN *insn = (INSN *) ELEM_FIRST_INSN(FIRST_ELEMENT(cond_seq));
+
if (insn->insn_id == BIN(putobject)) {
if (RTEST(insn->operands[0])) {
- ADD_INSNL(ret, cond, jump, then_label);
+ PUSH_INSNL(ret, location, jump, then_label);
// maybe unreachable
return;
}
else {
- ADD_INSNL(ret, cond, jump, else_label);
+ PUSH_INSNL(ret, location, jump, else_label);
return;
}
}
@@ -1021,7 +1132,7 @@ again:
* Compile an if or unless node.
*/
static void
-pm_compile_conditional(rb_iseq_t *iseq, const pm_node_location_t *node_location, pm_node_type_t type, const pm_node_t *node, const pm_statements_node_t *statements, const pm_node_t *consequent, const pm_node_t *predicate, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+pm_compile_conditional(rb_iseq_t *iseq, const pm_node_location_t *node_location, pm_node_type_t type, const pm_node_t *node, const pm_statements_node_t *statements, const pm_node_t *subsequent, const pm_node_t *predicate, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
LABEL *then_label = NEW_LABEL(location.line);
@@ -1029,8 +1140,7 @@ pm_compile_conditional(rb_iseq_t *iseq, const pm_node_location_t *node_location,
LABEL *end_label = NULL;
DECL_ANCHOR(cond_seq);
- INIT_ANCHOR(cond_seq);
- pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, false, scope_node);
+ pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, scope_node);
PUSH_SEQ(ret, cond_seq);
rb_code_location_t conditional_location = { 0 };
@@ -1045,7 +1155,6 @@ pm_compile_conditional(rb_iseq_t *iseq, const pm_node_location_t *node_location,
PUSH_LABEL(ret, then_label);
DECL_ANCHOR(then_seq);
- INIT_ANCHOR(then_seq);
if (statements != NULL) {
pm_compile_node(iseq, (const pm_node_t *) statements, then_seq, popped, scope_node);
@@ -1062,7 +1171,7 @@ pm_compile_conditional(rb_iseq_t *iseq, const pm_node_location_t *node_location,
if (statements != NULL) {
branch_location = pm_code_location(scope_node, (const pm_node_t *) statements);
} else if (type == PM_IF_NODE) {
- pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(scope_node->parser, predicate);
+ pm_line_column_t predicate_end = PM_NODE_END_LINE_COLUMN(predicate);
branch_location = (rb_code_location_t) {
.beg_pos = { .lineno = predicate_end.line, .column = predicate_end.column },
.end_pos = { .lineno = predicate_end.line, .column = predicate_end.column }
@@ -1086,10 +1195,9 @@ pm_compile_conditional(rb_iseq_t *iseq, const pm_node_location_t *node_location,
PUSH_LABEL(ret, else_label);
DECL_ANCHOR(else_seq);
- INIT_ANCHOR(else_seq);
- if (consequent != NULL) {
- pm_compile_node(iseq, consequent, else_seq, popped, scope_node);
+ if (subsequent != NULL) {
+ pm_compile_node(iseq, subsequent, else_seq, popped, scope_node);
}
else if (!popped) {
PUSH_SYNTHETIC_PUTNIL(else_seq, iseq);
@@ -1099,13 +1207,13 @@ pm_compile_conditional(rb_iseq_t *iseq, const pm_node_location_t *node_location,
if (then_label->refcnt && PM_BRANCH_COVERAGE_P(iseq)) {
rb_code_location_t branch_location;
- if (consequent == NULL) {
+ if (subsequent == NULL) {
branch_location = conditional_location;
- } else if (PM_NODE_TYPE_P(consequent, PM_ELSE_NODE)) {
- const pm_else_node_t *else_node = (const pm_else_node_t *) consequent;
+ } else if (PM_NODE_TYPE_P(subsequent, PM_ELSE_NODE)) {
+ const pm_else_node_t *else_node = (const pm_else_node_t *) subsequent;
branch_location = pm_code_location(scope_node, else_node->statements != NULL ? ((const pm_node_t *) else_node->statements) : (const pm_node_t *) else_node);
} else {
- branch_location = pm_code_location(scope_node, (const pm_node_t *) consequent);
+ branch_location = pm_code_location(scope_node, (const pm_node_t *) subsequent);
}
add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, 1, type == PM_IF_NODE ? "else" : "then", branches);
@@ -1180,10 +1288,10 @@ pm_compile_loop(rb_iseq_t *iseq, const pm_node_location_t *node_location, pm_nod
PUSH_LABEL(ret, next_label);
if (type == PM_WHILE_NODE) {
- pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, popped, scope_node);
+ pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, scope_node);
}
else if (type == PM_UNTIL_NODE) {
- pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, popped, scope_node);
+ pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, scope_node);
}
PUSH_LABEL(ret, end_label);
@@ -1212,14 +1320,15 @@ static pm_local_index_t
pm_lookup_local_index(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, pm_constant_id_t constant_id, int start_depth)
{
pm_local_index_t lindex = { 0 };
- st_data_t local_index;
+ int local_index;
int level;
for (level = 0; level < start_depth; level++) {
scope_node = scope_node->previous;
}
- while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
+ while (!pm_index_lookup_table_lookup(&scope_node->index_lookup_table, constant_id, &local_index))
+ {
level++;
if (scope_node->previous) {
@@ -1243,12 +1352,10 @@ pm_lookup_local_index(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, pm_con
// We add a constants mapping on the scope_node which is a mapping from
// these constant_id indexes to the CRuby IDs that they represent.
// This helper method allows easy access to those IDs
-static ID
+static inline ID
pm_constant_id_lookup(const pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
{
- if (constant_id < 1 || constant_id > scope_node->parser->constant_pool.size) {
- rb_bug("constant_id out of range: %u", (unsigned int)constant_id);
- }
+ RUBY_ASSERT(constant_id >= 1 && constant_id <= pm_parser_constants_size(scope_node->parser));
return scope_node->constants[constant_id - 1];
}
@@ -1257,7 +1364,7 @@ pm_new_child_iseq(rb_iseq_t *iseq, pm_scope_node_t *node, VALUE name, const rb_i
{
debugs("[new_child_iseq]> ---------------------------------------\n");
int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
- rb_iseq_t *ret_iseq = pm_iseq_new_with_opt(node, name,
+ rb_iseq_t *ret_iseq = pm_iseq_build(node, name,
rb_iseq_path(iseq), rb_iseq_realpath(iseq),
line_no, parent,
isolated_depth ? isolated_depth + 1 : 0,
@@ -1267,15 +1374,36 @@ pm_new_child_iseq(rb_iseq_t *iseq, pm_scope_node_t *node, VALUE name, const rb_i
}
static int
+pm_cpath_const_p(const pm_node_t *node)
+{
+ switch (PM_NODE_TYPE(node)) {
+ case PM_CONSTANT_READ_NODE:
+ return TRUE;
+ case PM_CONSTANT_PATH_NODE:
+ {
+ const pm_node_t *parent = ((const pm_constant_path_node_t *) node)->parent;
+ if (!parent) return TRUE; /* ::Foo */
+ return pm_cpath_const_p(parent);
+ }
+ default:
+ return FALSE;
+ }
+}
+
+static int
pm_compile_class_path(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
if (PM_NODE_TYPE_P(node, PM_CONSTANT_PATH_NODE)) {
const pm_node_t *parent = ((const pm_constant_path_node_t *) node)->parent;
if (parent) {
- /* Bar::Foo */
+ /* Bar::Foo or expr::Foo */
PM_COMPILE(parent);
- return VM_DEFINECLASS_FLAG_SCOPED;
+ int flags = VM_DEFINECLASS_FLAG_SCOPED;
+ if (!pm_cpath_const_p(parent)) {
+ flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
+ }
+ return flags;
}
else {
/* toplevel class ::Foo */
@@ -1345,15 +1473,17 @@ pm_compile_call_and_or_write_node(rb_iseq_t *iseq, bool and_node, const pm_node_
if (lskip && !popped) PUSH_LABEL(ret, lskip);
}
+static void pm_compile_shareable_constant_value(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_flags_t shareability, VALUE path, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, bool top);
+
/**
* This function compiles a hash onto the stack. It is used to compile hash
* literals and keyword arguments. It is assumed that if we get here that the
* contents of the hash are not popped.
*/
static void
-pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *elements, bool argument, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
+pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *elements, const pm_node_flags_t shareability, VALUE path, bool argument, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
// If this element is not popped, then we need to create the hash on the
// stack. Neighboring plain assoc nodes should be grouped together (either
@@ -1372,7 +1502,6 @@ pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_l
bool static_literal = false;
DECL_ANCHOR(anchor);
- INIT_ANCHOR(anchor);
// Convert pushed elements to a hash, and merge if needed.
#define FLUSH_CHUNK \
@@ -1398,12 +1527,18 @@ pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_l
switch (PM_NODE_TYPE(element)) {
case PM_ASSOC_NODE: {
// Pre-allocation check (this branch can be omitted).
- if (PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && !static_literal && ((index + min_tmp_hash_length) < elements->size)) {
+ if (
+ (shareability == 0) &&
+ PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && (
+ (!static_literal && ((index + min_tmp_hash_length) < elements->size)) ||
+ (first_chunk && stack_length == 0)
+ )
+ ) {
// Count the elements that are statically-known.
size_t count = 1;
while (index + count < elements->size && PM_NODE_FLAG_P(elements->nodes[index + count], PM_NODE_FLAG_STATIC_LITERAL)) count++;
- if (count >= min_tmp_hash_length) {
+ if ((first_chunk && stack_length == 0) || count >= min_tmp_hash_length) {
// The subsequence of elements in this hash is long enough
// to merit its own hash.
VALUE ary = rb_ary_hidden_new(count);
@@ -1419,11 +1554,12 @@ pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_l
rb_ary_cat(ary, elem, 2);
}
+ index --;
- VALUE hash = rb_hash_new_with_size(RARRAY_LEN(ary) / 2);
+ VALUE hash = rb_hash_alloc_fixed_size(Qfalse, RARRAY_LEN(ary) / 2);
rb_hash_bulk_insert(RARRAY_LEN(ary), RARRAY_CONST_PTR(ary), hash);
- hash = rb_obj_hide(hash);
- OBJ_FREEZE(hash);
+ RB_GC_GUARD(ary);
+ RB_OBJ_SET_FROZEN_SHAREABLE(hash);
// Emit optimized code.
FLUSH_CHUNK;
@@ -1450,7 +1586,15 @@ pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_l
// If this is a plain assoc node, then we can compile it directly
// and then add the total number of values on the stack.
- pm_compile_node(iseq, element, anchor, false, scope_node);
+ if (shareability == 0) {
+ pm_compile_node(iseq, element, anchor, false, scope_node);
+ }
+ else {
+ const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
+ pm_compile_shareable_constant_value(iseq, assoc->key, shareability, path, ret, scope_node, false);
+ pm_compile_shareable_constant_value(iseq, assoc->value, shareability, path, ret, scope_node, false);
+ }
+
if ((stack_length += 2) >= max_stack_length) FLUSH_CHUNK;
break;
}
@@ -1493,7 +1637,12 @@ pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_l
// only done for method calls and not for literal hashes,
// because literal hashes should always result in a new
// hash.
- PM_COMPILE_NOT_POPPED(element);
+ if (shareability == 0) {
+ PM_COMPILE_NOT_POPPED(element);
+ }
+ else {
+ pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node, false);
+ }
}
else {
// There is more than one keyword argument, or this is not a
@@ -1509,7 +1658,13 @@ pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_l
PUSH_INSN(ret, location, swap);
}
- PM_COMPILE_NOT_POPPED(element);
+ if (shareability == 0) {
+ PM_COMPILE_NOT_POPPED(element);
+ }
+ else {
+ pm_compile_shareable_constant_value(iseq, element, shareability, path, ret, scope_node, false);
+ }
+
PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
}
}
@@ -1528,9 +1683,13 @@ pm_compile_hash_elements(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_l
#undef FLUSH_CHUNK
}
+#define SPLATARRAY_FALSE 0
+#define SPLATARRAY_TRUE 1
+#define DUP_SINGLE_KW_SPLAT 2
+
// This is details. Users should call pm_setup_args() instead.
static int
-pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *block, int *flags, const bool has_regular_blockarg, struct rb_callinfo_kwarg **kw_arg, rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_node_location_t *node_location)
+pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *block, int *flags, const bool has_regular_blockarg, struct rb_callinfo_kwarg **kw_arg, int *dup_rest, rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_node_location_t *node_location)
{
const pm_node_location_t location = *node_location;
@@ -1563,14 +1722,23 @@ pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *b
*flags |= VM_CALL_KW_SPLAT;
has_keyword_splat = true;
- if (elements->size > 1) {
+ if (elements->size > 1 || !(elements->size == 1 && PM_NODE_TYPE_P(elements->nodes[0], PM_ASSOC_SPLAT_NODE))) {
// A new hash will be created for the keyword arguments
// in this case, so mark the method as passing mutable
// keyword splat.
*flags |= VM_CALL_KW_SPLAT_MUT;
+ pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
+ }
+ else if (*dup_rest & DUP_SINGLE_KW_SPLAT) {
+ *flags |= VM_CALL_KW_SPLAT_MUT;
+ PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
+ PUSH_INSN1(ret, location, newhash, INT2FIX(0));
+ pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
+ PUSH_SEND(ret, location, id_core_hash_merge_kwd, INT2FIX(2));
+ }
+ else {
+ pm_compile_hash_elements(iseq, argument, elements, 0, Qundef, true, ret, scope_node);
}
-
- pm_compile_hash_elements(iseq, argument, elements, true, ret, scope_node);
}
else {
// We need to first figure out if all elements of the
@@ -1676,8 +1844,8 @@ pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *b
// foo(a, *b, c)
// ^^
if (index + 1 < arguments->size || has_regular_blockarg) {
- PUSH_INSN1(ret, location, splatarray, Qtrue);
- *flags |= VM_CALL_ARGS_SPLAT_MUT;
+ PUSH_INSN1(ret, location, splatarray, (*dup_rest & SPLATARRAY_TRUE) ? Qtrue : Qfalse);
+ if (*dup_rest & SPLATARRAY_TRUE) *dup_rest &= ~SPLATARRAY_TRUE;
}
// If this is the first spalt array seen and it's the last
// parameter, we don't want splatarray to dup it.
@@ -1703,7 +1871,9 @@ pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *b
break;
}
- case PM_FORWARDING_ARGUMENTS_NODE: {
+ case PM_FORWARDING_ARGUMENTS_NODE: { // not counted in argc return value
+ iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
+
if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
*flags |= VM_CALL_FORWARDING;
@@ -1713,9 +1883,14 @@ pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *b
break;
}
- orig_argc += 2;
+ if (has_splat) {
+ // If we already have a splat, we're concatenating to existing array
+ orig_argc += 1;
+ } else {
+ orig_argc += 2;
+ }
- *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_SPLAT_MUT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
+ *flags |= VM_CALL_ARGS_SPLAT | VM_CALL_ARGS_BLOCKARG | VM_CALL_KW_SPLAT;
// Forwarding arguments nodes are treated as foo(*, **, &)
// So foo(...) equals foo(*, **, &) and as such the local
@@ -1724,7 +1899,13 @@ pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *b
// Push the *
pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
- PUSH_INSN1(ret, location, splatarray, Qtrue);
+
+ if (has_splat) {
+ // If we already have a splat, we need to concatenate arrays
+ PUSH_INSN(ret, location, concattoarray);
+ } else {
+ PUSH_INSN1(ret, location, splatarray, Qfalse);
+ }
// Push the **
pm_local_index_t pow_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
@@ -1733,7 +1914,6 @@ pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *b
// Push the &
pm_local_index_t and_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(and_local.index + VM_ENV_DATA_SIZE - 1), INT2FIX(and_local.level));
- PUSH_INSN(ret, location, splatkw);
break;
}
@@ -1756,6 +1936,10 @@ pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *b
// foo(*a, b, c: :d)
// foo(*a, b, **c)
//
+ // If the next node is a forwarding argument:
+ //
+ // foo(*a, b, ...)
+ //
// If the next node is NULL (we have hit the end):
//
// foo(*a, b)
@@ -1778,6 +1962,10 @@ pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *b
PUSH_INSN(ret, location, concatarray);
break;
}
+ case PM_FORWARDING_ARGUMENTS_NODE: {
+ PUSH_INSN1(ret, location, pushtoarray, INT2FIX(post_splat_counter));
+ break;
+ }
default:
break;
}
@@ -1796,17 +1984,121 @@ pm_setup_args_core(const pm_arguments_node_t *arguments_node, const pm_node_t *b
return orig_argc;
}
-// Compile the argument parts of a call
+/**
+ * True if the given kind of node could potentially mutate the array that is
+ * being splatted in a set of call arguments.
+ */
+static inline bool
+pm_setup_args_dup_rest_p(const pm_node_t *node)
+{
+ switch (PM_NODE_TYPE(node)) {
+ case PM_BACK_REFERENCE_READ_NODE:
+ case PM_CLASS_VARIABLE_READ_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_LAMBDA_NODE:
+ case PM_LOCAL_VARIABLE_READ_NODE:
+ case PM_NIL_NODE:
+ case PM_NUMBERED_REFERENCE_READ_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:
+ return false;
+ case PM_CONSTANT_PATH_NODE: {
+ const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
+ if (cast->parent != NULL) {
+ return pm_setup_args_dup_rest_p(cast->parent);
+ }
+ return false;
+ }
+ case PM_IMPLICIT_NODE:
+ return pm_setup_args_dup_rest_p(((const pm_implicit_node_t *) node)->value);
+ case PM_ARRAY_NODE: {
+ const pm_array_node_t *cast = (const pm_array_node_t *) node;
+ for (size_t index = 0; index < cast->elements.size; index++) {
+ if (pm_setup_args_dup_rest_p(cast->elements.nodes[index])) {
+ return true;
+ }
+ }
+ return false;
+ }
+ default:
+ return true;
+ }
+}
+
+/**
+ * Compile the argument parts of a call.
+ */
static int
pm_setup_args(const pm_arguments_node_t *arguments_node, const pm_node_t *block, int *flags, struct rb_callinfo_kwarg **kw_arg, rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, const pm_node_location_t *node_location)
{
+ int dup_rest = SPLATARRAY_TRUE;
+
+ const pm_node_list_t *arguments;
+ size_t arguments_size;
+
+ // Calls like foo(1, *f, **hash) that use splat and kwsplat could be
+ // eligible for eliding duping the rest array (dup_reset=false).
+ if (
+ arguments_node != NULL &&
+ (arguments = &arguments_node->arguments, arguments_size = arguments->size) >= 2 &&
+ PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_SPLAT) &&
+ !PM_NODE_FLAG_P(arguments_node, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_MULTIPLE_SPLATS) &&
+ PM_NODE_TYPE_P(arguments->nodes[arguments_size - 1], PM_KEYWORD_HASH_NODE)
+ ) {
+ // Start by assuming that dup_rest=false, then check each element of the
+ // hash to ensure we don't need to flip it back to true (in case one of
+ // the elements could potentially mutate the array).
+ dup_rest = SPLATARRAY_FALSE;
+
+ const pm_keyword_hash_node_t *keyword_hash = (const pm_keyword_hash_node_t *) arguments->nodes[arguments_size - 1];
+ const pm_node_list_t *elements = &keyword_hash->elements;
+
+ for (size_t index = 0; dup_rest == SPLATARRAY_FALSE && index < elements->size; index++) {
+ const pm_node_t *element = elements->nodes[index];
+
+ switch (PM_NODE_TYPE(element)) {
+ case PM_ASSOC_NODE: {
+ const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
+ if (pm_setup_args_dup_rest_p(assoc->key) || pm_setup_args_dup_rest_p(assoc->value)) dup_rest = SPLATARRAY_TRUE;
+ break;
+ }
+ case PM_ASSOC_SPLAT_NODE: {
+ const pm_assoc_splat_node_t *assoc = (const pm_assoc_splat_node_t *) element;
+ if (assoc->value != NULL && pm_setup_args_dup_rest_p(assoc->value)) dup_rest = SPLATARRAY_TRUE;
+ break;
+ }
+ default:
+ break;
+ }
+ }
+ }
+
+ int initial_dup_rest = dup_rest;
+ int argc;
+
if (block && PM_NODE_TYPE_P(block, PM_BLOCK_ARGUMENT_NODE)) {
// We compile the `&block_arg` expression first and stitch it later
// since the nature of the expression influences whether splat should
// duplicate the array.
bool regular_block_arg = true;
+ const pm_node_t *block_expr = ((const pm_block_argument_node_t *)block)->expression;
+
+ if (block_expr && pm_setup_args_dup_rest_p(block_expr)) {
+ dup_rest = SPLATARRAY_TRUE | DUP_SINGLE_KW_SPLAT;
+ initial_dup_rest = dup_rest;
+ }
+
DECL_ANCHOR(block_arg);
- INIT_ANCHOR(block_arg);
pm_compile_node(iseq, block, block_arg, false, scope_node);
*flags |= VM_CALL_ARGS_BLOCKARG;
@@ -1818,19 +2110,31 @@ pm_setup_args(const pm_arguments_node_t *arguments_node, const pm_node_t *block,
if (iobj->insn_id == BIN(getblockparam)) {
iobj->insn_id = BIN(getblockparamproxy);
}
+
// Allow splat without duplication for simple one-instruction
- // block arguments like `&arg`. It is known that this optimization
- // can be too aggressive in some cases. See [Bug #16504].
+ // block arguments like `&arg`. It is known that this
+ // optimization can be too aggressive in some cases. See
+ // [Bug #16504].
regular_block_arg = false;
}
}
- int argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, iseq, ret, scope_node, node_location);
+ argc = pm_setup_args_core(arguments_node, block, flags, regular_block_arg, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
PUSH_SEQ(ret, block_arg);
- return argc;
+ }
+ else {
+ argc = pm_setup_args_core(arguments_node, block, flags, false, kw_arg, &dup_rest, iseq, ret, scope_node, node_location);
+ }
+
+ // If the dup_rest flag was consumed while compiling the arguments (which
+ // effectively means we found the splat node), then it would have changed
+ // during the call to pm_setup_args_core. In this case, we want to add the
+ // VM_CALL_ARGS_SPLAT_MUT flag.
+ if (*flags & VM_CALL_ARGS_SPLAT && dup_rest != initial_dup_rest) {
+ *flags |= VM_CALL_ARGS_SPLAT_MUT;
}
- return pm_setup_args_core(arguments_node, block, flags, false, kw_arg, iseq, ret, scope_node, node_location);
+ return argc;
}
/**
@@ -1854,7 +2158,7 @@ pm_compile_index_operator_write_node(rb_iseq_t *iseq, const pm_index_operator_wr
int boff = (node->block == NULL ? 0 : 1);
int flag = PM_NODE_TYPE_P(node->receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
struct rb_callinfo_kwarg *keywords = NULL;
- int argc = pm_setup_args(node->arguments, node->block, &flag, &keywords, iseq, ret, scope_node, node_location);
+ int argc = pm_setup_args(node->arguments, (const pm_node_t *) node->block, &flag, &keywords, iseq, ret, scope_node, node_location);
if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
if (boff) {
@@ -1961,7 +2265,7 @@ pm_compile_index_operator_write_node(rb_iseq_t *iseq, const pm_index_operator_wr
* []= method.
*/
static void
-pm_compile_index_control_flow_write_node(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_t *receiver, const pm_arguments_node_t *arguments, const pm_node_t *block, const pm_node_t *value, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+pm_compile_index_control_flow_write_node(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_t *receiver, const pm_arguments_node_t *arguments, const pm_block_argument_node_t *block, const pm_node_t *value, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
const pm_node_location_t location = *node_location;
if (!popped) PUSH_INSN(ret, location, putnil);
@@ -1970,7 +2274,7 @@ pm_compile_index_control_flow_write_node(rb_iseq_t *iseq, const pm_node_t *node,
int boff = (block == NULL ? 0 : 1);
int flag = PM_NODE_TYPE_P(receiver, PM_SELF_NODE) ? VM_CALL_FCALL : 0;
struct rb_callinfo_kwarg *keywords = NULL;
- int argc = pm_setup_args(arguments, block, &flag, &keywords, iseq, ret, scope_node, node_location);
+ int argc = pm_setup_args(arguments, (const pm_node_t *) block, &flag, &keywords, iseq, ret, scope_node, node_location);
if ((argc > 0 || boff) && (flag & VM_CALL_KW_SPLAT)) {
if (boff) {
@@ -2095,7 +2399,7 @@ pm_compile_index_control_flow_write_node(rb_iseq_t *iseq, const pm_node_t *node,
// A forward declaration because this is the recursive function that handles
// compiling a pattern. It can be reentered by nesting patterns, as in the case
// of arrays or hashes.
-static int pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *matched_label, LABEL *unmatched_label, bool in_single_pattern, bool in_alternation_pattern, bool use_deconstructed_cache, unsigned int base_index);
+static int pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *matched_label, LABEL *unmatched_label, bool in_single_pattern, bool use_deconstructed_cache, unsigned int base_index);
/**
* This function generates the code to set up the error string and error_p
@@ -2104,7 +2408,7 @@ static int pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, cons
static int
pm_compile_pattern_generic_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, VALUE message, unsigned int base_index)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
LABEL *match_succeeded_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
@@ -2134,7 +2438,7 @@ pm_compile_pattern_generic_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, c
static int
pm_compile_pattern_length_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, VALUE message, VALUE length, unsigned int base_index)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
LABEL *match_succeeded_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
@@ -2167,14 +2471,16 @@ pm_compile_pattern_length_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, co
static int
pm_compile_pattern_eqq_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, unsigned int base_index)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
LABEL *match_succeeded_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, match_succeeded_label);
-
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
- PUSH_INSN1(ret, location, putobject, rb_fstring_lit("%p === %p does not return true"));
+
+ VALUE operand = rb_fstring_lit("%p === %p does not return true");
+ PUSH_INSN1(ret, location, putobject, operand);
+
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_INSN1(ret, location, topn, INT2FIX(5));
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
@@ -2199,10 +2505,10 @@ pm_compile_pattern_eqq_error(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const
* label.
*/
static int
-pm_compile_pattern_match(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *unmatched_label, bool in_single_pattern, bool in_alternation_pattern, bool use_deconstructed_cache, unsigned int base_index)
+pm_compile_pattern_match(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *unmatched_label, bool in_single_pattern, bool use_deconstructed_cache, unsigned int base_index)
{
- LABEL *matched_label = NEW_LABEL(pm_node_line_number(scope_node->parser, node));
- CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
+ LABEL *matched_label = NEW_LABEL(pm_node_line_number_cached(node, scope_node));
+ CHECK(pm_compile_pattern(iseq, scope_node, node, ret, matched_label, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index));
PUSH_LABEL(ret, matched_label);
return COMPILE_OK;
}
@@ -2215,7 +2521,7 @@ pm_compile_pattern_match(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_
static int
pm_compile_pattern_deconstruct(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *deconstruct_label, LABEL *match_failed_label, LABEL *deconstructed_label, LABEL *type_error_label, bool in_single_pattern, bool use_deconstructed_cache, unsigned int base_index)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
if (use_deconstructed_cache) {
PUSH_INSN1(ret, location, topn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_DECONSTRUCTED_CACHE));
@@ -2234,7 +2540,9 @@ pm_compile_pattern_deconstruct(rb_iseq_t *iseq, pm_scope_node_t *scope_node, con
PUSH_LABEL(ret, deconstruct_label);
PUSH_INSN(ret, location, dup);
- PUSH_INSN1(ret, location, putobject, ID2SYM(rb_intern("deconstruct")));
+
+ VALUE operand = ID2SYM(rb_intern("deconstruct"));
+ PUSH_INSN1(ret, location, putobject, operand);
PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
if (use_deconstructed_cache) {
@@ -2267,7 +2575,7 @@ pm_compile_pattern_deconstruct(rb_iseq_t *iseq, pm_scope_node_t *scope_node, con
static int
pm_compile_pattern_constant(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *match_failed_label, bool in_single_pattern, unsigned int base_index)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
PUSH_INSN(ret, location, dup);
PM_COMPILE_NOT_POPPED(node);
@@ -2288,9 +2596,9 @@ pm_compile_pattern_constant(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const
* responsible for compiling in those error raising instructions.
*/
static void
-pm_compile_pattern_error_handler(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *done_label, bool popped)
+pm_compile_pattern_error_handler(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *done_label, bool popped)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
LABEL *key_error_label = NEW_LABEL(location.line);
LABEL *cleanup_label = NEW_LABEL(location.line);
@@ -2306,7 +2614,12 @@ pm_compile_pattern_error_handler(rb_iseq_t *iseq, const pm_scope_node_t *scope_n
PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternError);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
- PUSH_INSN1(ret, location, putobject, rb_fstring_lit("%p: %s"));
+
+ {
+ VALUE operand = rb_fstring_lit("%p: %s");
+ PUSH_INSN1(ret, location, putobject, operand);
+ }
+
PUSH_INSN1(ret, location, topn, INT2FIX(4));
PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
@@ -2316,7 +2629,12 @@ pm_compile_pattern_error_handler(rb_iseq_t *iseq, const pm_scope_node_t *scope_n
PUSH_LABEL(ret, key_error_label);
PUSH_INSN1(ret, location, putobject, rb_eNoMatchingPatternKeyError);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
- PUSH_INSN1(ret, location, putobject, rb_fstring_lit("%p: %s"));
+
+ {
+ VALUE operand = rb_fstring_lit("%p: %s");
+ PUSH_INSN1(ret, location, putobject, operand);
+ }
+
PUSH_INSN1(ret, location, topn, INT2FIX(4));
PUSH_INSN1(ret, location, topn, INT2FIX(PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 6));
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(3));
@@ -2337,9 +2655,9 @@ pm_compile_pattern_error_handler(rb_iseq_t *iseq, const pm_scope_node_t *scope_n
* Compile a pattern matching expression.
*/
static int
-pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *matched_label, LABEL *unmatched_label, bool in_single_pattern, bool in_alternation_pattern, bool use_deconstructed_cache, unsigned int base_index)
+pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t *node, LINK_ANCHOR *const ret, LABEL *matched_label, LABEL *unmatched_label, bool in_single_pattern, bool use_deconstructed_cache, unsigned int base_index)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
switch (PM_NODE_TYPE(node)) {
case PM_ARRAY_PATTERN_NODE: {
@@ -2397,7 +2715,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putobject, INT2FIX(index));
PUSH_SEND(ret, location, idAREF, INT2FIX(1));
- CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
+ CHECK(pm_compile_pattern_match(iseq, scope_node, required, ret, match_failed_label, in_single_pattern, false, base_index + 1));
}
if (cast->rest != NULL) {
@@ -2410,7 +2728,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_SEND(ret, location, idMINUS, INT2FIX(1));
PUSH_INSN1(ret, location, setn, INT2FIX(4));
PUSH_SEND(ret, location, idAREF, INT2FIX(2));
- CHECK(pm_compile_pattern_match(iseq, scope_node, ((const pm_splat_node_t *) cast->rest)->expression, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
+ CHECK(pm_compile_pattern_match(iseq, scope_node, ((const pm_splat_node_t *) cast->rest)->expression, ret, match_failed_label, in_single_pattern, false, base_index + 1));
}
else if (posts_size > 0) {
PUSH_INSN(ret, location, dup);
@@ -2430,7 +2748,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
PUSH_SEND(ret, location, idAREF, INT2FIX(1));
- CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
+ CHECK(pm_compile_pattern_match(iseq, scope_node, post, ret, match_failed_label, in_single_pattern, false, base_index + 1));
}
PUSH_INSN(ret, location, pop);
@@ -2447,7 +2765,12 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_LABEL(ret, type_error_label);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, putobject, rb_eTypeError);
- PUSH_INSN1(ret, location, putobject, rb_fstring_lit("deconstruct must return Array"));
+
+ {
+ VALUE operand = rb_fstring_lit("deconstruct must return Array");
+ PUSH_INSN1(ret, location, putobject, operand);
+ }
+
PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
PUSH_INSN(ret, location, pop);
@@ -2522,22 +2845,20 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
}
PUSH_SEND(ret, location, idAREF, INT2FIX(1));
- CHECK(pm_compile_pattern_match(iseq, scope_node, cast->requireds.nodes[index], ret, next_loop_label, in_single_pattern, in_alternation_pattern, false, base_index + 4));
+ CHECK(pm_compile_pattern_match(iseq, scope_node, cast->requireds.nodes[index], ret, next_loop_label, in_single_pattern, false, base_index + 4));
}
- RUBY_ASSERT(PM_NODE_TYPE_P(cast->left, PM_SPLAT_NODE));
- const pm_splat_node_t *left = (const pm_splat_node_t *) cast->left;
+ const pm_splat_node_t *left = cast->left;
if (left->expression != NULL) {
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_INSN1(ret, location, putobject, INT2FIX(0));
PUSH_INSN1(ret, location, topn, INT2FIX(2));
PUSH_SEND(ret, location, idAREF, INT2FIX(2));
- CHECK(pm_compile_pattern_match(iseq, scope_node, left->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4));
+ CHECK(pm_compile_pattern_match(iseq, scope_node, left->expression, ret, find_failed_label, in_single_pattern, false, base_index + 4));
}
- RUBY_ASSERT(PM_NODE_TYPE_P(cast->right, PM_SPLAT_NODE));
- const pm_splat_node_t *right = (const pm_splat_node_t *) cast->right;
+ const pm_splat_node_t *right = cast->right;
if (right->expression != NULL) {
PUSH_INSN1(ret, location, topn, INT2FIX(3));
@@ -2546,7 +2867,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_SEND(ret, location, idPLUS, INT2FIX(1));
PUSH_INSN1(ret, location, topn, INT2FIX(3));
PUSH_SEND(ret, location, idAREF, INT2FIX(2));
- pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 4);
+ pm_compile_pattern_match(iseq, scope_node, right->expression, ret, find_failed_label, in_single_pattern, false, base_index + 4);
}
PUSH_INSNL(ret, location, jump, find_succeeded_label);
@@ -2560,7 +2881,12 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_INSN1(ret, location, adjuststack, INT2FIX(3));
if (in_single_pattern) {
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
- PUSH_INSN1(ret, location, putobject, rb_fstring_lit("%p does not match to find pattern"));
+
+ {
+ VALUE operand = rb_fstring_lit("%p does not match to find pattern");
+ PUSH_INSN1(ret, location, putobject, operand);
+ }
+
PUSH_INSN1(ret, location, topn, INT2FIX(2));
PUSH_SEND(ret, location, id_core_sprintf, INT2FIX(2));
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
@@ -2585,7 +2911,12 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_LABEL(ret, type_error_label);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, putobject, rb_eTypeError);
- PUSH_INSN1(ret, location, putobject, rb_fstring_lit("deconstruct must return Array"));
+
+ {
+ VALUE operand = rb_fstring_lit("deconstruct must return Array");
+ PUSH_INSN1(ret, location, putobject, operand);
+ }
+
PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
PUSH_INSN(ret, location, pop);
@@ -2634,7 +2965,12 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
}
PUSH_INSN(ret, location, dup);
- PUSH_INSN1(ret, location, putobject, ID2SYM(rb_intern("deconstruct_keys")));
+
+ {
+ VALUE operand = ID2SYM(rb_intern("deconstruct_keys"));
+ PUSH_INSN1(ret, location, putobject, operand);
+ }
+
PUSH_SEND(ret, location, idRespond_to, INT2FIX(1));
if (in_single_pattern) {
CHECK(pm_compile_pattern_generic_error(iseq, scope_node, node, ret, rb_fstring_lit("%p does not respond to #deconstruct_keys"), base_index + 1));
@@ -2645,8 +2981,10 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_INSN(ret, location, putnil);
}
else {
+ rb_obj_hide(keys);
+ RB_OBJ_SET_FROZEN_SHAREABLE(keys);
PUSH_INSN1(ret, location, duparray, keys);
- RB_OBJ_WRITTEN(iseq, Qundef, rb_obj_hide(keys));
+ RB_OBJ_WRITTEN(iseq, Qundef, keys);
}
PUSH_SEND(ret, location, rb_intern("deconstruct_keys"), INT2FIX(1));
@@ -2660,7 +2998,6 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
if (has_keys) {
DECL_ANCHOR(match_values);
- INIT_ANCHOR(match_values);
for (size_t index = 0; index < cast->elements.size; index++) {
const pm_node_t *element = cast->elements.nodes[index];
@@ -2681,7 +3018,12 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_INSN(ret, location, dup);
PUSH_INSNL(ret, location, branchif, match_succeeded_label);
- PUSH_INSN1(ret, location, putobject, rb_str_freeze(rb_sprintf("key not found: %+"PRIsVALUE, symbol)));
+ {
+ VALUE operand = rb_str_freeze(rb_sprintf("key not found: %+"PRIsVALUE, symbol));
+ RB_OBJ_SET_SHAREABLE(operand);
+ PUSH_INSN1(ret, location, putobject, operand);
+ }
+
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 2));
PUSH_INSN1(ret, location, putobject, Qtrue);
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 3));
@@ -2704,7 +3046,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
value = ((const pm_implicit_node_t *) value)->value;
}
- CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1));
+ CHECK(pm_compile_pattern_match(iseq, scope_node, value, match_values, match_failed_label, in_single_pattern, false, base_index + 1));
}
PUSH_SEQ(ret, match_values);
@@ -2732,7 +3074,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
case PM_ASSOC_SPLAT_NODE: {
const pm_assoc_splat_node_t *splat = (const pm_assoc_splat_node_t *) cast->rest;
PUSH_INSN(ret, location, dup);
- pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index + 1);
+ pm_compile_pattern_match(iseq, scope_node, splat->value, ret, match_failed_label, in_single_pattern, false, base_index + 1);
break;
}
default:
@@ -2748,7 +3090,12 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_LABEL(ret, type_error_label);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_INSN1(ret, location, putobject, rb_eTypeError);
- PUSH_INSN1(ret, location, putobject, rb_fstring_lit("deconstruct_keys must return Hash"));
+
+ {
+ VALUE operand = rb_fstring_lit("deconstruct_keys must return Hash");
+ PUSH_INSN1(ret, location, putobject, operand);
+ }
+
PUSH_SEND(ret, location, id_core_raise, INT2FIX(2));
PUSH_INSN(ret, location, pop);
@@ -2772,8 +3119,8 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
LABEL *match_failed_label = NEW_LABEL(location.line);
PUSH_INSN(ret, location, dup);
- CHECK(pm_compile_pattern_match(iseq, scope_node, cast->value, ret, match_failed_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index + 1));
- CHECK(pm_compile_pattern(iseq, scope_node, cast->target, ret, matched_label, match_failed_label, in_single_pattern, in_alternation_pattern, false, base_index));
+ CHECK(pm_compile_pattern_match(iseq, scope_node, cast->value, ret, match_failed_label, in_single_pattern, use_deconstructed_cache, base_index + 1));
+ CHECK(pm_compile_pattern(iseq, scope_node, (const pm_node_t *) cast->target, ret, matched_label, match_failed_label, in_single_pattern, false, base_index));
PUSH_INSN(ret, location, putnil);
PUSH_LABEL(ret, match_failed_label);
@@ -2789,20 +3136,6 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
const pm_local_variable_target_node_t *cast = (const pm_local_variable_target_node_t *) node;
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
- // 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) {
- ID id = pm_constant_id_lookup(scope_node, cast->name);
- const char *name = rb_id2name(id);
-
- if (name && strlen(name) > 0 && name[0] != '_') {
- COMPILE_ERROR(iseq, location.line, "illegal variable in alternative pattern (%"PRIsVALUE")", rb_id2str(id));
- return COMPILE_NG;
- }
- }
-
PUSH_SETLOCAL(ret, location, index.index, index.level);
PUSH_INSNL(ret, location, jump, matched_label);
break;
@@ -2818,7 +3151,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
// First, we're going to attempt to match against the left pattern. If
// that pattern matches, then we'll skip matching the right pattern.
PUSH_INSN(ret, location, dup);
- CHECK(pm_compile_pattern(iseq, scope_node, cast->left, ret, matched_left_label, unmatched_left_label, in_single_pattern, true, true, base_index + 1));
+ CHECK(pm_compile_pattern(iseq, scope_node, cast->left, ret, matched_left_label, unmatched_left_label, in_single_pattern, use_deconstructed_cache, base_index + 1));
// 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
@@ -2831,7 +3164,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
// If we get here, then we didn't match on the left pattern. In this
// case we attempt to match against the right pattern.
PUSH_LABEL(ret, unmatched_left_label);
- CHECK(pm_compile_pattern(iseq, scope_node, cast->right, ret, matched_label, unmatched_label, in_single_pattern, true, true, base_index));
+ CHECK(pm_compile_pattern(iseq, scope_node, cast->right, ret, matched_label, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index));
break;
}
case PM_PARENTHESES_NODE:
@@ -2839,7 +3172,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
// they do nothing since they can only wrap individual expressions and
// not groups. In this case we'll recurse back into this same function
// with the body of the parentheses.
- return pm_compile_pattern(iseq, scope_node, ((const pm_parentheses_node_t *) node)->body, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index);
+ return pm_compile_pattern(iseq, scope_node, ((const pm_parentheses_node_t *) node)->body, ret, matched_label, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index);
case PM_PINNED_EXPRESSION_NODE:
// Pinned expressions are a way to match against the value of an
// expression that should be evaluated at runtime. This looks like:
@@ -2856,6 +3189,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
case PM_GLOBAL_VARIABLE_READ_NODE:
case PM_IMAGINARY_NODE:
case PM_INSTANCE_VARIABLE_READ_NODE:
+ case PM_IT_LOCAL_VARIABLE_READ_NODE:
case PM_INTEGER_NODE:
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
case PM_INTERPOLATED_STRING_NODE:
@@ -2899,7 +3233,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
// looks like: foo in ^@bar. To compile these, we compile the variable
// that they hold.
const pm_pinned_variable_node_t *cast = (const pm_pinned_variable_node_t *) node;
- CHECK(pm_compile_pattern(iseq, scope_node, cast->variable, ret, matched_label, unmatched_label, in_single_pattern, in_alternation_pattern, true, base_index));
+ CHECK(pm_compile_pattern(iseq, scope_node, cast->variable, ret, matched_label, unmatched_label, in_single_pattern, true, base_index));
break;
}
case PM_IF_NODE:
@@ -2933,7 +3267,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
statement = cast->statements->body.nodes[0];
}
- CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, in_alternation_pattern, use_deconstructed_cache, base_index));
+ CHECK(pm_compile_pattern_match(iseq, scope_node, statement, ret, unmatched_label, in_single_pattern, use_deconstructed_cache, base_index));
PM_COMPILE_NOT_POPPED(predicate);
if (in_single_pattern) {
@@ -2947,7 +3281,11 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
PUSH_INSNL(ret, location, branchunless, match_succeeded_label);
}
- PUSH_INSN1(ret, location, putobject, rb_fstring_lit("guard clause does not return true"));
+ {
+ VALUE operand = rb_fstring_lit("guard clause does not return true");
+ PUSH_INSN1(ret, location, putobject, operand);
+ }
+
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_ERROR_STRING + 1));
PUSH_INSN1(ret, location, putobject, Qfalse);
PUSH_INSN1(ret, location, setn, INT2FIX(base_index + PM_PATTERN_BASE_INDEX_OFFSET_KEY_ERROR_P + 2));
@@ -2972,7 +3310,7 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
// 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.
- rb_bug("Unexpected node type in pattern matching expression: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
+ rb_bug("Unexpected node type in pattern matching expression: %s", pm_node_type(PM_NODE_TYPE(node)));
break;
}
@@ -2989,25 +3327,27 @@ pm_compile_pattern(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_t
void
pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous)
{
- // This is very important, otherwise the scope node could be seen as having
- // certain flags set that _should not_ be set.
- memset(scope, 0, sizeof(pm_scope_node_t));
+ if (previous) {
+ // Copy inherited fields from the parent scope in one shot, then
+ // zero out the fields that are scope-specific.
+ *scope = *previous;
+ scope->locals = (pm_constant_id_list_t) { 0 };
+ scope->parameters = NULL;
+ scope->body = NULL;
+ scope->local_table_for_iseq_size = 0;
+ scope->index_lookup_table = (pm_index_lookup_table_t) PM_INDEX_LOOKUP_TABLE_INIT;
+ scope->pre_execution_anchor = NULL;
+ }
+ else {
+ memset(scope, 0, sizeof(pm_scope_node_t));
+ }
scope->base.type = PM_SCOPE_NODE;
scope->base.location.start = node->location.start;
- scope->base.location.end = node->location.end;
-
+ scope->base.location.length = node->location.length;
scope->previous = previous;
scope->ast_node = (pm_node_t *) node;
- if (previous) {
- scope->parser = previous->parser;
- scope->encoding = previous->encoding;
- scope->filepath_encoding = previous->filepath_encoding;
- scope->constants = previous->constants;
- scope->coverage_enabled = previous->coverage_enabled;
- }
-
switch (PM_NODE_TYPE(node)) {
case PM_BLOCK_NODE: {
const pm_block_node_t *cast = (const pm_block_node_t *) node;
@@ -3035,7 +3375,7 @@ pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_
if (cast->statements != NULL) {
scope->base.location.start = cast->statements->base.location.start;
- scope->base.location.end = cast->statements->base.location.end;
+ scope->base.location.length = cast->statements->base.location.length;
}
break;
@@ -3055,13 +3395,6 @@ pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_
scope->parameters = cast->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
-
- if (cast->parameters != NULL) {
- scope->base.location.start = cast->parameters->location.start;
- }
- else {
- scope->base.location.start = cast->operator_loc.end;
- }
break;
}
case PM_MODULE_NODE: {
@@ -3111,8 +3444,8 @@ pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_
void
pm_scope_node_destroy(pm_scope_node_t *scope_node)
{
- if (scope_node->index_lookup_table) {
- st_free_table(scope_node->index_lookup_table);
+ if (scope_node->index_lookup_table.owned) {
+ xfree(scope_node->index_lookup_table.values);
}
}
@@ -3125,7 +3458,7 @@ pm_scope_node_destroy(pm_scope_node_t *scope_node)
* Normally, "send" instruction is at the last. However, qcall under branch
* coverage measurement adds some instructions after the "send".
*
- * Note that "invokesuper" appears instead of "send".
+ * Note that "invokesuper", "invokesuperforward" appears instead of "send".
*/
static void
pm_compile_retry_end_label(rb_iseq_t *iseq, LINK_ANCHOR *const ret, LABEL *retry_end_l)
@@ -3181,7 +3514,7 @@ pm_iseq_builtin_function_name(const pm_scope_node_t *scope_node, const pm_node_t
// Compile Primitive.attr! :leaf, ...
static int
-pm_compile_builtin_attr(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, const pm_arguments_node_t *arguments, const pm_node_location_t *node_location)
+pm_compile_builtin_attr(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_arguments_node_t *arguments, const pm_node_location_t *node_location)
{
if (arguments == NULL) {
COMPILE_ERROR(iseq, node_location->line, "attr!: no argument");
@@ -3191,12 +3524,12 @@ pm_compile_builtin_attr(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, cons
const pm_node_t *argument;
PM_NODE_LIST_FOREACH(&arguments->arguments, index, argument) {
if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
- COMPILE_ERROR(iseq, node_location->line, "non symbol argument to attr!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
+ COMPILE_ERROR(iseq, node_location->line, "non symbol argument to attr!: %s", pm_node_type(PM_NODE_TYPE(argument)));
return COMPILE_NG;
}
VALUE symbol = pm_static_literal_value(iseq, argument, scope_node);
- VALUE string = rb_sym_to_s(symbol);
+ VALUE string = rb_sym2str(symbol);
if (strcmp(RSTRING_PTR(string), "leaf") == 0) {
ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_LEAF;
@@ -3207,6 +3540,13 @@ pm_compile_builtin_attr(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, cons
else if (strcmp(RSTRING_PTR(string), "use_block") == 0) {
iseq_set_use_block(iseq);
}
+ else if (strcmp(RSTRING_PTR(string), "c_trace") == 0) {
+ // Let the iseq act like a C method in backtraces
+ ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_C_TRACE;
+ }
+ else if (strcmp(RSTRING_PTR(string), "without_interrupts") == 0) {
+ ISEQ_BODY(iseq)->builtin_attrs |= BUILTIN_ATTR_WITHOUT_INTERRUPTS;
+ }
else {
COMPILE_ERROR(iseq, node_location->line, "unknown argument to attr!: %s", RSTRING_PTR(string));
return COMPILE_NG;
@@ -3231,7 +3571,7 @@ pm_compile_builtin_arg(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_scope_n
const pm_node_t *argument = arguments->arguments.nodes[0];
if (!PM_NODE_TYPE_P(argument, PM_SYMBOL_NODE)) {
- COMPILE_ERROR(iseq, node_location->line, "non symbol argument to arg!: %s", pm_node_type_to_str(PM_NODE_TYPE(argument)));
+ COMPILE_ERROR(iseq, node_location->line, "non symbol argument to arg!: %s", pm_node_type(PM_NODE_TYPE(argument)));
return COMPILE_NG;
}
@@ -3295,7 +3635,7 @@ pm_compile_builtin_mandatory_only_method(rb_iseq_t *iseq, pm_scope_node_t *scope
pm_scope_node_t next_scope_node;
pm_scope_node_init(&def.base, &next_scope_node, scope_node);
- ISEQ_BODY(iseq)->mandatory_only_iseq = pm_iseq_new_with_opt(
+ const rb_iseq_t *mandatory_only_iseq = pm_iseq_build(
&next_scope_node,
rb_iseq_base_label(iseq),
rb_iseq_path(iseq),
@@ -3306,6 +3646,7 @@ pm_compile_builtin_mandatory_only_method(rb_iseq_t *iseq, pm_scope_node_t *scope
ISEQ_TYPE_METHOD,
ISEQ_COMPILE_DATA(iseq)->option
);
+ RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->mandatory_only_iseq, (VALUE)mandatory_only_iseq);
pm_scope_node_destroy(&next_scope_node);
return COMPILE_OK;
@@ -3372,7 +3713,7 @@ retry:;
if (cconst) {
typedef VALUE(*builtin_func0)(void *, VALUE);
- VALUE const_val = (*(builtin_func0)bf->func_ptr)(NULL, Qnil);
+ VALUE const_val = (*(builtin_func0)(uintptr_t)bf->func_ptr)(NULL, Qnil);
PUSH_INSN1(ret, *node_location, putobject, const_val);
return COMPILE_OK;
}
@@ -3380,7 +3721,6 @@ retry:;
// fprintf(stderr, "func_name:%s -> %p\n", builtin_func, bf->func_ptr);
DECL_ANCHOR(args_seq);
- INIT_ANCHOR(args_seq);
int flags = 0;
struct rb_callinfo_kwarg *keywords = NULL;
@@ -3411,9 +3751,10 @@ static void
pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, ID method_id, LABEL *start)
{
const pm_location_t *message_loc = &call_node->message_loc;
- if (message_loc->start == NULL) message_loc = &call_node->base.location;
+ if (message_loc->length == 0) message_loc = &call_node->base.location;
+
+ const pm_node_location_t location = PM_LOCATION_START_LOCATION(message_loc, call_node->base.node_id);
- const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, message_loc, call_node->base.node_id);
LABEL *else_label = NEW_LABEL(location.line);
LABEL *end_label = NEW_LABEL(location.line);
LABEL *retry_end_l = NEW_LABEL(location.line);
@@ -3424,18 +3765,37 @@ pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *c
if (PM_NODE_FLAG_P(call_node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
if (PM_BRANCH_COVERAGE_P(iseq)) {
- const uint8_t *cursors[3] = {
- call_node->closing_loc.end,
- call_node->arguments == NULL ? NULL : call_node->arguments->base.location.end,
- call_node->message_loc.end
- };
+ uint32_t end_cursor = 0;
+ bool end_found = false;
- const uint8_t *end_cursor = cursors[0];
- end_cursor = (end_cursor == NULL || cursors[1] == NULL) ? cursors[1] : (end_cursor > cursors[1] ? end_cursor : cursors[1]);
- end_cursor = (end_cursor == NULL || cursors[2] == NULL) ? cursors[2] : (end_cursor > cursors[2] ? end_cursor : cursors[2]);
+ if (call_node->closing_loc.length > 0) {
+ uint32_t cursor = call_node->closing_loc.start + call_node->closing_loc.length;
+ end_cursor = cursor;
+ end_found = true;
+ }
- const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(scope_node->parser, call_node);
- const pm_line_column_t end_location = pm_newline_list_line_column(&scope_node->parser->newline_list, end_cursor, scope_node->parser->start_line);
+ if (call_node->arguments != NULL) {
+ uint32_t cursor = call_node->arguments->base.location.start + call_node->arguments->base.location.length;
+ if (!end_found || cursor > end_cursor) {
+ end_cursor = cursor;
+ end_found = true;
+ }
+ }
+
+ if (call_node->message_loc.length > 0) {
+ uint32_t cursor = call_node->message_loc.start + call_node->message_loc.length;
+ if (!end_found || cursor > end_cursor) {
+ end_cursor = cursor;
+ end_found = true;
+ }
+ }
+
+ if (!end_found) {
+ end_cursor = call_node->closing_loc.start + call_node->closing_loc.length;
+ }
+
+ const pm_line_column_t start_location = PM_NODE_START_LINE_COLUMN(call_node);
+ const pm_line_column_t end_location = pm_line_offset_list_line_column_cached(scope_node->line_offsets, end_cursor, scope_node->start_line, &scope_node->last_line);
code_location = (rb_code_location_t) {
.beg_pos = { .lineno = start_location.line, .column = start_location.column },
@@ -3451,6 +3811,8 @@ pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *c
add_trace_branch_coverage(iseq, ret, &code_location, node_id, 0, "then", branches);
}
+ LINK_ELEMENT *opt_new_prelude = LAST_ELEMENT(ret);
+
int flags = 0;
struct rb_callinfo_kwarg *kw_arg = NULL;
@@ -3463,7 +3825,7 @@ pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *c
pm_scope_node_t next_scope_node;
pm_scope_node_init(call_node->block, &next_scope_node, scope_node);
- block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, pm_node_line_number(scope_node->parser, call_node->block));
+ block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, pm_node_line_number_cached(call_node->block, scope_node));
pm_scope_node_destroy(&next_scope_node);
ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
}
@@ -3507,7 +3869,50 @@ pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *c
PUSH_INSN(ret, location, splatkw);
}
- PUSH_SEND_R(ret, location, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
+ LABEL *not_basic_new = NEW_LABEL(location.line);
+ LABEL *not_basic_new_finish = NEW_LABEL(location.line);
+
+ bool inline_new = ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction &&
+ method_id == rb_intern("new") &&
+ call_node->block == NULL &&
+ (flags & VM_CALL_ARGS_BLOCKARG) == 0;
+
+ if (inline_new) {
+ if (LAST_ELEMENT(ret) == opt_new_prelude) {
+ PUSH_INSN(ret, location, putnil);
+ PUSH_INSN(ret, location, swap);
+ }
+ else {
+ ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.line, location.node_id, BIN(swap), 0)->link);
+ ELEM_INSERT_NEXT(opt_new_prelude, &new_insn_body(iseq, location.line, location.node_id, BIN(putnil), 0)->link);
+ }
+
+ // Jump unless the receiver uses the "basic" implementation of "new"
+ VALUE ci;
+ if (flags & VM_CALL_FORWARDING) {
+ ci = (VALUE)new_callinfo(iseq, method_id, orig_argc + 1, flags, kw_arg, 0);
+ }
+ else {
+ ci = (VALUE)new_callinfo(iseq, method_id, orig_argc, flags, kw_arg, 0);
+ }
+
+ PUSH_INSN2(ret, location, opt_new, ci, not_basic_new);
+ LABEL_REF(not_basic_new);
+ // optimized path
+ PUSH_SEND_R(ret, location, rb_intern("initialize"), INT2FIX(orig_argc), block_iseq, INT2FIX(flags | VM_CALL_FCALL), kw_arg);
+ PUSH_INSNL(ret, location, jump, not_basic_new_finish);
+
+ PUSH_LABEL(ret, not_basic_new);
+ // Fall back to normal send
+ PUSH_SEND_R(ret, location, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
+ PUSH_INSN(ret, location, swap);
+
+ PUSH_LABEL(ret, not_basic_new_finish);
+ PUSH_INSN(ret, location, pop);
+ }
+ else {
+ PUSH_SEND_R(ret, location, method_id, INT2FIX(orig_argc), block_iseq, INT2FIX(flags), kw_arg);
+ }
if (block_iseq && ISEQ_BODY(block_iseq)->catch_table) {
pm_compile_retry_end_label(iseq, ret, retry_end_l);
@@ -3529,264 +3934,585 @@ pm_compile_call(rb_iseq_t *iseq, const pm_call_node_t *call_node, LINK_ANCHOR *c
ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
}
+/**
+ * Compile and return the VALUE associated with the given back reference read
+ * node.
+ */
+static inline VALUE
+pm_compile_back_reference_ref(const pm_scope_node_t *scope_node, const pm_back_reference_read_node_t *node)
+{
+ const char *type = (const char *) (pm_parser_start(scope_node->parser) + node->base.location.start + 1);
+
+ // Since a back reference is `$<char>`, Ruby represents the ID as an
+ // rb_intern on the value after the `$`.
+ return INT2FIX(rb_intern2(type, 1)) << 1 | 1;
+}
+
+/**
+ * Compile and return the VALUE associated with the given numbered reference
+ * read node.
+ */
+static inline VALUE
+pm_compile_numbered_reference_ref(const pm_numbered_reference_read_node_t *node)
+{
+ return INT2FIX(node->number << 1);
+}
+
static void
pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition, LABEL **lfinish, bool explicit_receiver)
{
+#define PUSH_VAL(type) (in_condition ? Qtrue : rb_iseq_defined_string(type))
+
// in_condition is the same as compile.c's needstr
enum defined_type dtype = DEFINED_NOT_DEFINED;
const pm_node_location_t location = *node_location;
switch (PM_NODE_TYPE(node)) {
- case PM_ARGUMENTS_NODE: {
- const pm_arguments_node_t *cast = (const 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, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
-
- if (!lfinish[1]) {
- lfinish[1] = NEW_LABEL(location.line);
- }
- PUSH_INSNL(ret, location, branchunless, lfinish[1]);
- }
- dtype = DEFINED_TRUE;
- break;
- }
+/* DEFINED_NIL ****************************************************************/
case PM_NIL_NODE:
+ // defined?(nil)
+ // ^^^
dtype = DEFINED_NIL;
break;
- case PM_PARENTHESES_NODE: {
- const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
+/* DEFINED_IVAR ***************************************************************/
+ case PM_INSTANCE_VARIABLE_READ_NODE: {
+ // defined?(@a)
+ // ^^
+ const pm_instance_variable_read_node_t *cast = (const pm_instance_variable_read_node_t *) node;
+ ID name = pm_constant_id_lookup(scope_node, cast->name);
- if (cast->body == NULL) {
- // If we have empty parentheses, then we want to return "nil".
- dtype = DEFINED_NIL;
- }
- else if (PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE) && ((const pm_statements_node_t *) cast->body)->body.size == 1) {
- // If we have a parentheses node that is wrapping a single statement
- // then we want to recurse down to that statement and compile it.
- pm_compile_defined_expr0(iseq, ((const pm_statements_node_t *) cast->body)->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
- return;
- }
- else {
- // Otherwise, we have parentheses wrapping multiple statements, in
- // which case this is defined as "expression".
- dtype = DEFINED_EXPR;
- }
+ PUSH_INSN3(ret, location, definedivar, ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
- break;
+ return;
}
- case PM_SELF_NODE:
- dtype = DEFINED_SELF;
- break;
- case PM_TRUE_NODE:
- dtype = DEFINED_TRUE;
- break;
- case PM_FALSE_NODE:
- dtype = DEFINED_FALSE;
+/* DEFINED_LVAR ***************************************************************/
+ case PM_LOCAL_VARIABLE_READ_NODE:
+ // a = 1; defined?(a)
+ // ^
+ case PM_IT_LOCAL_VARIABLE_READ_NODE:
+ // 1.then { defined?(it) }
+ // ^^
+ dtype = DEFINED_LVAR;
break;
- case PM_ARRAY_NODE: {
- const pm_array_node_t *cast = (const pm_array_node_t *) node;
-
- if (cast->elements.size > 0 && !lfinish[1]) {
- lfinish[1] = NEW_LABEL(location.line);
- }
+/* DEFINED_GVAR ***************************************************************/
+ case PM_GLOBAL_VARIABLE_READ_NODE: {
+ // defined?($a)
+ // ^^
+ const pm_global_variable_read_node_t *cast = (const pm_global_variable_read_node_t *) node;
+ ID name = pm_constant_id_lookup(scope_node, cast->name);
- for (size_t index = 0; index < cast->elements.size; index++) {
- pm_compile_defined_expr0(iseq, cast->elements.nodes[index], node_location, ret, popped, scope_node, true, lfinish, false);
- PUSH_INSNL(ret, location, branchunless, lfinish[1]);
- }
+ PUSH_INSN(ret, location, putnil);
+ PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), ID2SYM(name), PUSH_VAL(DEFINED_GVAR));
- dtype = DEFINED_EXPR;
- break;
+ return;
}
- case PM_HASH_NODE:
- case PM_KEYWORD_HASH_NODE: {
- const pm_node_list_t *elements;
-
- if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
- elements = &((const pm_hash_node_t *) node)->elements;
- }
- else {
- elements = &((const pm_keyword_hash_node_t *) node)->elements;
- }
-
- if (elements->size > 0 && !lfinish[1]) {
- lfinish[1] = NEW_LABEL(location.line);
- }
+/* DEFINED_CVAR ***************************************************************/
+ case PM_CLASS_VARIABLE_READ_NODE: {
+ // defined?(@@a)
+ // ^^^
+ const pm_class_variable_read_node_t *cast = (const pm_class_variable_read_node_t *) node;
+ ID name = pm_constant_id_lookup(scope_node, cast->name);
- for (size_t index = 0; index < elements->size; index++) {
- const pm_node_t *element = elements->nodes[index];
+ PUSH_INSN(ret, location, putnil);
+ PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), ID2SYM(name), PUSH_VAL(DEFINED_CVAR));
- switch (PM_NODE_TYPE(element)) {
- case PM_ASSOC_NODE: {
- const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
+ return;
+ }
+/* DEFINED_CONST **************************************************************/
+ case PM_CONSTANT_READ_NODE: {
+ // defined?(A)
+ // ^
+ const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
+ ID name = pm_constant_id_lookup(scope_node, cast->name);
- pm_compile_defined_expr0(iseq, assoc->key, node_location, ret, popped, scope_node, true, lfinish, false);
- PUSH_INSNL(ret, location, branchunless, lfinish[1]);
+ PUSH_INSN(ret, location, putnil);
+ PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), ID2SYM(name), PUSH_VAL(DEFINED_CONST));
- pm_compile_defined_expr0(iseq, assoc->value, node_location, ret, popped, scope_node, true, lfinish, false);
- PUSH_INSNL(ret, location, branchunless, lfinish[1]);
+ return;
+ }
+/* DEFINED_YIELD **************************************************************/
+ case PM_YIELD_NODE:
+ // defined?(yield)
+ // ^^^^^
+ iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
- break;
- }
- case PM_ASSOC_SPLAT_NODE: {
- const pm_assoc_splat_node_t *assoc_splat = (const pm_assoc_splat_node_t *) element;
+ PUSH_INSN(ret, location, putnil);
+ PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
- pm_compile_defined_expr0(iseq, assoc_splat->value, node_location, ret, popped, scope_node, true, lfinish, false);
- PUSH_INSNL(ret, location, branchunless, lfinish[1]);
+ return;
+/* DEFINED_ZSUPER *************************************************************/
+ case PM_SUPER_NODE: {
+ // defined?(super 1, 2)
+ // ^^^^^^^^^^
+ const pm_super_node_t *cast = (const pm_super_node_t *) node;
- break;
- }
- default:
- rb_bug("unexpected node type in hash node: %s", pm_node_type_to_str(PM_NODE_TYPE(element)));
- break;
- }
+ if (cast->block != NULL && !PM_NODE_TYPE_P(cast->block, PM_BLOCK_ARGUMENT_NODE)) {
+ dtype = DEFINED_EXPR;
+ break;
}
- dtype = DEFINED_EXPR;
- break;
+ PUSH_INSN(ret, location, putnil);
+ PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
+ return;
}
- case PM_SPLAT_NODE: {
- const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
- pm_compile_defined_expr0(iseq, cast->expression, node_location, ret, popped, scope_node, in_condition, lfinish, false);
+ case PM_FORWARDING_SUPER_NODE: {
+ // defined?(super)
+ // ^^^^^
+ const pm_forwarding_super_node_t *cast = (const pm_forwarding_super_node_t *) node;
- if (!lfinish[1]) {
- lfinish[1] = NEW_LABEL(location.line);
+ if (cast->block != NULL) {
+ dtype = DEFINED_EXPR;
+ break;
}
- PUSH_INSNL(ret, location, branchunless, lfinish[1]);
- dtype = DEFINED_EXPR;
- break;
- }
- case PM_IMPLICIT_NODE: {
- const pm_implicit_node_t *cast = (const pm_implicit_node_t *) node;
- pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
+ PUSH_INSN(ret, location, putnil);
+ PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
return;
}
+/* DEFINED_SELF ***************************************************************/
+ case PM_SELF_NODE:
+ // defined?(self)
+ // ^^^^
+ dtype = DEFINED_SELF;
+ break;
+/* DEFINED_TRUE ***************************************************************/
+ case PM_TRUE_NODE:
+ // defined?(true)
+ // ^^^^
+ dtype = DEFINED_TRUE;
+ break;
+/* DEFINED_FALSE **************************************************************/
+ case PM_FALSE_NODE:
+ // defined?(false)
+ // ^^^^^
+ dtype = DEFINED_FALSE;
+ break;
+/* DEFINED_ASGN ***************************************************************/
+ case PM_CALL_AND_WRITE_NODE:
+ // defined?(a.a &&= 1)
+ // ^^^^^^^^^
+ case PM_CALL_OPERATOR_WRITE_NODE:
+ // defined?(a.a += 1)
+ // ^^^^^^^^
+ case PM_CALL_OR_WRITE_NODE:
+ // defined?(a.a ||= 1)
+ // ^^^^^^^^^
+ case PM_CLASS_VARIABLE_AND_WRITE_NODE:
+ // defined?(@@a &&= 1)
+ // ^^^^^^^^^
+ case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE:
+ // defined?(@@a += 1)
+ // ^^^^^^^^
+ case PM_CLASS_VARIABLE_OR_WRITE_NODE:
+ // defined?(@@a ||= 1)
+ // ^^^^^^^^^
+ case PM_CLASS_VARIABLE_WRITE_NODE:
+ // defined?(@@a = 1)
+ // ^^^^^^^
+ case PM_CONSTANT_AND_WRITE_NODE:
+ // defined?(A &&= 1)
+ // ^^^^^^^
+ case PM_CONSTANT_OPERATOR_WRITE_NODE:
+ // defined?(A += 1)
+ // ^^^^^^
+ case PM_CONSTANT_OR_WRITE_NODE:
+ // defined?(A ||= 1)
+ // ^^^^^^^
+ case PM_CONSTANT_PATH_AND_WRITE_NODE:
+ // defined?(A::A &&= 1)
+ // ^^^^^^^^^^
+ case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
+ // defined?(A::A += 1)
+ // ^^^^^^^^^
+ case PM_CONSTANT_PATH_OR_WRITE_NODE:
+ // defined?(A::A ||= 1)
+ // ^^^^^^^^^^
+ case PM_CONSTANT_PATH_WRITE_NODE:
+ // defined?(A::A = 1)
+ // ^^^^^^^^
+ case PM_CONSTANT_WRITE_NODE:
+ // defined?(A = 1)
+ // ^^^^^
+ case PM_GLOBAL_VARIABLE_AND_WRITE_NODE:
+ // defined?($a &&= 1)
+ // ^^^^^^^^
+ case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE:
+ // defined?($a += 1)
+ // ^^^^^^^
+ case PM_GLOBAL_VARIABLE_OR_WRITE_NODE:
+ // defined?($a ||= 1)
+ // ^^^^^^^^
+ case PM_GLOBAL_VARIABLE_WRITE_NODE:
+ // defined?($a = 1)
+ // ^^^^^^
+ case PM_INDEX_AND_WRITE_NODE:
+ // defined?(a[1] &&= 1)
+ // ^^^^^^^^^^
+ case PM_INDEX_OPERATOR_WRITE_NODE:
+ // defined?(a[1] += 1)
+ // ^^^^^^^^^
+ case PM_INDEX_OR_WRITE_NODE:
+ // defined?(a[1] ||= 1)
+ // ^^^^^^^^^^
+ case PM_INSTANCE_VARIABLE_AND_WRITE_NODE:
+ // defined?(@a &&= 1)
+ // ^^^^^^^^
+ case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE:
+ // defined?(@a += 1)
+ // ^^^^^^^
+ case PM_INSTANCE_VARIABLE_OR_WRITE_NODE:
+ // defined?(@a ||= 1)
+ // ^^^^^^^^
+ case PM_INSTANCE_VARIABLE_WRITE_NODE:
+ // defined?(@a = 1)
+ // ^^^^^^
+ case PM_LOCAL_VARIABLE_AND_WRITE_NODE:
+ // defined?(a &&= 1)
+ // ^^^^^^^
+ case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE:
+ // defined?(a += 1)
+ // ^^^^^^
+ case PM_LOCAL_VARIABLE_OR_WRITE_NODE:
+ // defined?(a ||= 1)
+ // ^^^^^^^
+ case PM_LOCAL_VARIABLE_WRITE_NODE:
+ // defined?(a = 1)
+ // ^^^^^
+ case PM_MULTI_WRITE_NODE:
+ // defined?((a, = 1))
+ // ^^^^^^
+ dtype = DEFINED_ASGN;
+ break;
+/* DEFINED_EXPR ***************************************************************/
+ case PM_ALIAS_GLOBAL_VARIABLE_NODE:
+ // defined?((alias $a $b))
+ // ^^^^^^^^^^^
+ case PM_ALIAS_METHOD_NODE:
+ // defined?((alias a b))
+ // ^^^^^^^^^
case PM_AND_NODE:
- case PM_BEGIN_NODE:
+ // defined?(a and b)
+ // ^^^^^^^
case PM_BREAK_NODE:
- case PM_CASE_NODE:
+ // defined?(break 1)
+ // ^^^^^^^
case PM_CASE_MATCH_NODE:
+ // defined?(case 1; in 1; end)
+ // ^^^^^^^^^^^^^^^^^
+ case PM_CASE_NODE:
+ // defined?(case 1; when 1; end)
+ // ^^^^^^^^^^^^^^^^^^^
case PM_CLASS_NODE:
+ // defined?(class Foo; end)
+ // ^^^^^^^^^^^^^^
case PM_DEF_NODE:
+ // defined?(def a() end)
+ // ^^^^^^^^^^^
case PM_DEFINED_NODE:
+ // defined?(defined?(a))
+ // ^^^^^^^^^^^
+ case PM_FLIP_FLOP_NODE:
+ // defined?(not (a .. b))
+ // ^^^^^^
case PM_FLOAT_NODE:
+ // defined?(1.0)
+ // ^^^
case PM_FOR_NODE:
+ // defined?(for a in 1 do end)
+ // ^^^^^^^^^^^^^^^^^
case PM_IF_NODE:
+ // defined?(if a then end)
+ // ^^^^^^^^^^^^^
case PM_IMAGINARY_NODE:
+ // defined?(1i)
+ // ^^
case PM_INTEGER_NODE:
+ // defined?(1)
+ // ^
+ case PM_INTERPOLATED_MATCH_LAST_LINE_NODE:
+ // defined?(not /#{1}/)
+ // ^^^^^^
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
+ // defined?(/#{1}/)
+ // ^^^^^^
case PM_INTERPOLATED_STRING_NODE:
+ // defined?("#{1}")
+ // ^^^^^^
case PM_INTERPOLATED_SYMBOL_NODE:
+ // defined?(:"#{1}")
+ // ^^^^^^^
case PM_INTERPOLATED_X_STRING_NODE:
+ // defined?(`#{1}`)
+ // ^^^^^^
case PM_LAMBDA_NODE:
+ // defined?(-> {})
+ // ^^^^^
+ case PM_MATCH_LAST_LINE_NODE:
+ // defined?(not //)
+ // ^^^^^^
case PM_MATCH_PREDICATE_NODE:
+ // defined?(1 in 1)
+ // ^^^^^^
case PM_MATCH_REQUIRED_NODE:
+ // defined?(1 => 1)
+ // ^^^^^^
case PM_MATCH_WRITE_NODE:
+ // defined?(/(?<a>)/ =~ "")
+ // ^^^^^^^^^^^^^^
case PM_MODULE_NODE:
+ // defined?(module A end)
+ // ^^^^^^^^^^^^
case PM_NEXT_NODE:
+ // defined?(next 1)
+ // ^^^^^^
case PM_OR_NODE:
+ // defined?(a or b)
+ // ^^^^^^
+ case PM_POST_EXECUTION_NODE:
+ // defined?((END {}))
+ // ^^^^^^^^
case PM_RANGE_NODE:
+ // defined?(1..1)
+ // ^^^^
case PM_RATIONAL_NODE:
+ // defined?(1r)
+ // ^^
case PM_REDO_NODE:
+ // defined?(redo)
+ // ^^^^
case PM_REGULAR_EXPRESSION_NODE:
+ // defined?(//)
+ // ^^
+ case PM_RESCUE_MODIFIER_NODE:
+ // defined?(a rescue b)
+ // ^^^^^^^^^^
case PM_RETRY_NODE:
+ // defined?(retry)
+ // ^^^^^
case PM_RETURN_NODE:
+ // defined?(return)
+ // ^^^^^^
case PM_SINGLETON_CLASS_NODE:
+ // defined?(class << self; end)
+ // ^^^^^^^^^^^^^^^^^^
case PM_SOURCE_ENCODING_NODE:
+ // defined?(__ENCODING__)
+ // ^^^^^^^^^^^^
case PM_SOURCE_FILE_NODE:
+ // defined?(__FILE__)
+ // ^^^^^^^^
case PM_SOURCE_LINE_NODE:
+ // defined?(__LINE__)
+ // ^^^^^^^^
case PM_STRING_NODE:
+ // defined?("")
+ // ^^
case PM_SYMBOL_NODE:
+ // defined?(:a)
+ // ^^
+ case PM_UNDEF_NODE:
+ // defined?((undef a))
+ // ^^^^^^^
case PM_UNLESS_NODE:
+ // defined?(unless a then end)
+ // ^^^^^^^^^^^^^^^^^
case PM_UNTIL_NODE:
+ // defined?(until a do end)
+ // ^^^^^^^^^^^^^^
case PM_WHILE_NODE:
+ // defined?(while a do end)
+ // ^^^^^^^^^^^^^^
case PM_X_STRING_NODE:
+ // defined?(``)
+ // ^^
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: {
- const pm_instance_variable_read_node_t *cast = (const pm_instance_variable_read_node_t *) node;
-
- ID name = pm_constant_id_lookup(scope_node, cast->name);
- PUSH_INSN3(ret, location, definedivar, ID2SYM(name), get_ivar_ic_value(iseq, name), PUSH_VAL(DEFINED_IVAR));
-
- return;
- }
+/* DEFINED_REF ****************************************************************/
case PM_BACK_REFERENCE_READ_NODE: {
- const char *char_ptr = (const char *) (node->location.start + 1);
- ID backref_val = INT2FIX(rb_intern2(char_ptr, 1)) << 1 | 1;
+ // defined?($+)
+ // ^^
+ const pm_back_reference_read_node_t *cast = (const pm_back_reference_read_node_t *) node;
+ VALUE ref = pm_compile_back_reference_ref(scope_node, cast);
PUSH_INSN(ret, location, putnil);
- PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), backref_val, PUSH_VAL(DEFINED_GVAR));
+ PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
return;
}
case PM_NUMBERED_REFERENCE_READ_NODE: {
- uint32_t reference_number = ((const pm_numbered_reference_read_node_t *) node)->number;
+ // defined?($1)
+ // ^^
+ const pm_numbered_reference_read_node_t *cast = (const pm_numbered_reference_read_node_t *) node;
+ VALUE ref = pm_compile_numbered_reference_ref(cast);
PUSH_INSN(ret, location, putnil);
- PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), INT2FIX(reference_number << 1), PUSH_VAL(DEFINED_GVAR));
+ PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_REF), ref, PUSH_VAL(DEFINED_GVAR));
return;
}
- case PM_GLOBAL_VARIABLE_READ_NODE: {
- const pm_global_variable_read_node_t *cast = (const pm_global_variable_read_node_t *) node;
- VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
+/* DEFINED_CONST_FROM *********************************************************/
+ case PM_CONSTANT_PATH_NODE: {
+ // defined?(A::A)
+ // ^^^^
+ const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
+ ID name = pm_constant_id_lookup(scope_node, cast->name);
- PUSH_INSN(ret, location, putnil);
- PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_GVAR), name, PUSH_VAL(DEFINED_GVAR));
+ if (cast->parent != NULL) {
+ if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
+ pm_compile_defined_expr0(iseq, cast->parent, node_location, ret, popped, scope_node, true, lfinish, false);
+
+ PUSH_INSNL(ret, location, branchunless, lfinish[1]);
+ PM_COMPILE(cast->parent);
+ }
+ else {
+ PUSH_INSN1(ret, location, putobject, rb_cObject);
+ }
+ PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), ID2SYM(name), PUSH_VAL(DEFINED_CONST));
return;
}
- case PM_CLASS_VARIABLE_READ_NODE: {
- const pm_class_variable_read_node_t *cast = (const pm_class_variable_read_node_t *) node;
- VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
+/* Containers *****************************************************************/
+ case PM_BEGIN_NODE: {
+ // defined?(begin end)
+ // ^^^^^^^^^
+ const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
- PUSH_INSN(ret, location, putnil);
- PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CVAR), name, PUSH_VAL(DEFINED_CVAR));
+ if (cast->rescue_clause == NULL && cast->ensure_clause == NULL && cast->else_clause == NULL) {
+ if (cast->statements == NULL) {
+ // If we have empty statements, then we want to return "nil".
+ dtype = DEFINED_NIL;
+ }
+ else if (cast->statements->body.size == 1) {
+ // If we have a begin node that is wrapping a single statement
+ // then we want to recurse down to that statement and compile
+ // it.
+ pm_compile_defined_expr0(iseq, cast->statements->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish, false);
+ return;
+ }
+ else {
+ // Otherwise, we have a begin wrapping multiple statements, in
+ // which case this is defined as "expression".
+ dtype = DEFINED_EXPR;
+ }
+ } else {
+ // If we have any of the other clauses besides the main begin/end,
+ // this is defined as "expression".
+ dtype = DEFINED_EXPR;
+ }
- return;
+ break;
}
- case PM_CONSTANT_READ_NODE: {
- const pm_constant_read_node_t *cast = (const pm_constant_read_node_t *) node;
- VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
+ case PM_PARENTHESES_NODE: {
+ // defined?(())
+ // ^^
+ const pm_parentheses_node_t *cast = (const pm_parentheses_node_t *) node;
- PUSH_INSN(ret, location, putnil);
- PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST), name, PUSH_VAL(DEFINED_CONST));
+ if (cast->body == NULL) {
+ // If we have empty parentheses, then we want to return "nil".
+ dtype = DEFINED_NIL;
+ }
+ else if (PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE) && !PM_NODE_FLAG_P(cast, PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS)) {
+ // If we have a parentheses node that is wrapping a single statement
+ // then we want to recurse down to that statement and compile it.
+ pm_compile_defined_expr0(iseq, ((const pm_statements_node_t *) cast->body)->body.nodes[0], node_location, ret, popped, scope_node, in_condition, lfinish, false);
+ return;
+ }
+ else {
+ // Otherwise, we have parentheses wrapping multiple statements, in
+ // which case this is defined as "expression".
+ dtype = DEFINED_EXPR;
+ }
- return;
+ break;
}
- case PM_CONSTANT_PATH_NODE: {
- const pm_constant_path_node_t *cast = (const pm_constant_path_node_t *) node;
- VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, cast->name));
+ case PM_ARRAY_NODE: {
+ // defined?([])
+ // ^^
+ const pm_array_node_t *cast = (const pm_array_node_t *) node;
- if (cast->parent != NULL) {
- if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
- pm_compile_defined_expr0(iseq, cast->parent, node_location, ret, popped, scope_node, true, lfinish, false);
+ if (cast->elements.size > 0 && !lfinish[1]) {
+ lfinish[1] = NEW_LABEL(location.line);
+ }
+ for (size_t index = 0; index < cast->elements.size; index++) {
+ pm_compile_defined_expr0(iseq, cast->elements.nodes[index], node_location, ret, popped, scope_node, true, lfinish, false);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
- PM_COMPILE(cast->parent);
+ }
+
+ dtype = DEFINED_EXPR;
+ break;
+ }
+ case PM_HASH_NODE:
+ // defined?({ a: 1 })
+ // ^^^^^^^^
+ case PM_KEYWORD_HASH_NODE: {
+ // defined?(a(a: 1))
+ // ^^^^
+ const pm_node_list_t *elements;
+
+ if (PM_NODE_TYPE_P(node, PM_HASH_NODE)) {
+ elements = &((const pm_hash_node_t *) node)->elements;
}
else {
- PUSH_INSN1(ret, location, putobject, rb_cObject);
+ elements = &((const pm_keyword_hash_node_t *) node)->elements;
+ }
+
+ if (elements->size > 0 && !lfinish[1]) {
+ lfinish[1] = NEW_LABEL(location.line);
+ }
+
+ for (size_t index = 0; index < elements->size; index++) {
+ pm_compile_defined_expr0(iseq, elements->nodes[index], node_location, ret, popped, scope_node, true, lfinish, false);
+ PUSH_INSNL(ret, location, branchunless, lfinish[1]);
+ }
+
+ dtype = DEFINED_EXPR;
+ break;
+ }
+ case PM_ASSOC_NODE: {
+ // defined?({ a: 1 })
+ // ^^^^
+ const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
+
+ pm_compile_defined_expr0(iseq, cast->key, node_location, ret, popped, scope_node, true, lfinish, false);
+ PUSH_INSNL(ret, location, branchunless, lfinish[1]);
+ pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, true, lfinish, false);
+
+ return;
+ }
+ case PM_ASSOC_SPLAT_NODE: {
+ // defined?({ **a })
+ // ^^^^
+ const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
+
+ if (cast->value == NULL) {
+ dtype = DEFINED_EXPR;
+ break;
}
- PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_CONST_FROM), name, PUSH_VAL(DEFINED_CONST));
+ pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, true, lfinish, false);
+ return;
+ }
+ case PM_IMPLICIT_NODE: {
+ // defined?({ a: })
+ // ^^
+ const pm_implicit_node_t *cast = (const pm_implicit_node_t *) node;
+ pm_compile_defined_expr0(iseq, cast->value, node_location, ret, popped, scope_node, in_condition, lfinish, false);
return;
}
case PM_CALL_NODE: {
+#define BLOCK_P(cast) ((cast)->block != NULL && PM_NODE_TYPE_P((cast)->block, PM_BLOCK_NODE))
+
+ // defined?(a(1, 2, 3))
+ // ^^^^^^^^^^
const pm_call_node_t *cast = ((const pm_call_node_t *) node);
- ID method_id = pm_constant_id_lookup(scope_node, cast->name);
- if (cast->receiver || cast->arguments) {
+ if (BLOCK_P(cast)) {
+ dtype = DEFINED_EXPR;
+ break;
+ }
+
+ if (cast->receiver || cast->arguments || (cast->block && PM_NODE_TYPE_P(cast->block, PM_BLOCK_ARGUMENT_NODE))) {
if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
if (!lfinish[2]) lfinish[2] = NEW_LABEL(location.line);
}
@@ -3796,93 +4522,155 @@ pm_compile_defined_expr0(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_l
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
}
- if (cast->receiver) {
- pm_compile_defined_expr0(iseq, cast->receiver, node_location, ret, popped, scope_node, true, lfinish, true);
+ if (cast->block && PM_NODE_TYPE_P(cast->block, PM_BLOCK_ARGUMENT_NODE)) {
+ pm_compile_defined_expr0(iseq, cast->block, node_location, ret, popped, scope_node, true, lfinish, false);
+ PUSH_INSNL(ret, location, branchunless, lfinish[1]);
+ }
- if (PM_NODE_TYPE_P(cast->receiver, PM_CALL_NODE)) {
+ if (cast->receiver) {
+ if (PM_NODE_TYPE_P(cast->receiver, PM_CALL_NODE) && !BLOCK_P((const pm_call_node_t *) cast->receiver)) {
+ // Special behavior here where we chain calls together. This is
+ // the only path that sets explicit_receiver to true.
+ pm_compile_defined_expr0(iseq, cast->receiver, node_location, ret, popped, scope_node, true, lfinish, true);
PUSH_INSNL(ret, location, branchunless, lfinish[2]);
const pm_call_node_t *receiver = (const pm_call_node_t *) cast->receiver;
ID method_id = pm_constant_id_lookup(scope_node, receiver->name);
+
pm_compile_call(iseq, receiver, ret, popped, scope_node, method_id, NULL);
}
else {
+ pm_compile_defined_expr0(iseq, cast->receiver, node_location, ret, popped, scope_node, true, lfinish, false);
PUSH_INSNL(ret, location, branchunless, lfinish[1]);
PM_COMPILE(cast->receiver);
}
+ ID method_id = pm_constant_id_lookup(scope_node, cast->name);
+
if (explicit_receiver) PUSH_INSN(ret, location, dup);
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_METHOD), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
}
else {
+ ID method_id = pm_constant_id_lookup(scope_node, cast->name);
+
PUSH_INSN(ret, location, putself);
if (explicit_receiver) PUSH_INSN(ret, location, dup);
+
PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_FUNC), rb_id2sym(method_id), PUSH_VAL(DEFINED_METHOD));
}
return;
- }
- case PM_YIELD_NODE:
- PUSH_INSN(ret, location, putnil);
- PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_YIELD), 0, PUSH_VAL(DEFINED_YIELD));
- return;
- case PM_SUPER_NODE:
- case PM_FORWARDING_SUPER_NODE:
- PUSH_INSN(ret, location, putnil);
- PUSH_INSN3(ret, location, defined, INT2FIX(DEFINED_ZSUPER), 0, PUSH_VAL(DEFINED_ZSUPER));
- return;
- case PM_CALL_AND_WRITE_NODE:
- case PM_CALL_OPERATOR_WRITE_NODE:
- case PM_CALL_OR_WRITE_NODE:
-
- 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_CONSTANT_PATH_AND_WRITE_NODE:
- case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE:
- case PM_CONSTANT_PATH_OR_WRITE_NODE:
- case PM_CONSTANT_PATH_WRITE_NODE:
+#undef BLOCK_P
+ }
+ case PM_ARGUMENTS_NODE: {
+ // defined?(a(1, 2, 3))
+ // ^^^^^^^
+ const pm_arguments_node_t *cast = (const pm_arguments_node_t *) 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:
+ for (size_t index = 0; index < cast->arguments.size; index++) {
+ pm_compile_defined_expr0(iseq, cast->arguments.nodes[index], node_location, ret, popped, scope_node, in_condition, lfinish, false);
+ PUSH_INSNL(ret, location, branchunless, lfinish[1]);
+ }
- 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:
+ dtype = DEFINED_EXPR;
+ break;
+ }
+ case PM_BLOCK_ARGUMENT_NODE:
+ // defined?(a(&b))
+ // ^^
+ dtype = DEFINED_EXPR;
+ break;
+ case PM_FORWARDING_ARGUMENTS_NODE:
+ // def a(...) = defined?(a(...))
+ // ^^^
+ dtype = DEFINED_EXPR;
+ break;
+ case PM_SPLAT_NODE: {
+ // def a(*) = defined?(a(*))
+ // ^
+ const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
- case PM_INDEX_AND_WRITE_NODE:
- case PM_INDEX_OPERATOR_WRITE_NODE:
- case PM_INDEX_OR_WRITE_NODE:
+ if (cast->expression == NULL) {
+ dtype = DEFINED_EXPR;
+ break;
+ }
- 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:
+ pm_compile_defined_expr0(iseq, cast->expression, node_location, ret, popped, scope_node, in_condition, lfinish, false);
- 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:
+ if (!lfinish[1]) lfinish[1] = NEW_LABEL(location.line);
+ PUSH_INSNL(ret, location, branchunless, lfinish[1]);
- case PM_MULTI_WRITE_NODE:
- dtype = DEFINED_ASGN;
+ dtype = DEFINED_EXPR;
break;
- default:
- rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
+ }
+ case PM_SHAREABLE_CONSTANT_NODE:
+ // # shareable_constant_value: literal
+ // defined?(A = 1)
+ // ^^^^^
+ pm_compile_defined_expr0(iseq, ((const pm_shareable_constant_node_t *) node)->write, node_location, ret, popped, scope_node, in_condition, lfinish, explicit_receiver);
+ return;
+/* Unreachable (parameters) ***************************************************/
+ case PM_BLOCK_LOCAL_VARIABLE_NODE:
+ case PM_BLOCK_PARAMETER_NODE:
+ case PM_BLOCK_PARAMETERS_NODE:
+ case PM_FORWARDING_PARAMETER_NODE:
+ case PM_IMPLICIT_REST_NODE:
+ case PM_IT_PARAMETERS_NODE:
+ case PM_PARAMETERS_NODE:
+ case PM_KEYWORD_REST_PARAMETER_NODE:
+ case PM_NO_KEYWORDS_PARAMETER_NODE:
+ case PM_NO_BLOCK_PARAMETER_NODE:
+ case PM_NUMBERED_PARAMETERS_NODE:
+ case PM_OPTIONAL_KEYWORD_PARAMETER_NODE:
+ case PM_OPTIONAL_PARAMETER_NODE:
+ case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
+ case PM_REQUIRED_PARAMETER_NODE:
+ case PM_REST_PARAMETER_NODE:
+/* Unreachable (pattern matching) *********************************************/
+ case PM_ALTERNATION_PATTERN_NODE:
+ case PM_ARRAY_PATTERN_NODE:
+ case PM_CAPTURE_PATTERN_NODE:
+ case PM_FIND_PATTERN_NODE:
+ case PM_HASH_PATTERN_NODE:
+ case PM_PINNED_EXPRESSION_NODE:
+ case PM_PINNED_VARIABLE_NODE:
+/* Unreachable (indirect writes) **********************************************/
+ case PM_CALL_TARGET_NODE:
+ case PM_CLASS_VARIABLE_TARGET_NODE:
+ case PM_CONSTANT_PATH_TARGET_NODE:
+ case PM_CONSTANT_TARGET_NODE:
+ case PM_GLOBAL_VARIABLE_TARGET_NODE:
+ case PM_INDEX_TARGET_NODE:
+ case PM_INSTANCE_VARIABLE_TARGET_NODE:
+ case PM_LOCAL_VARIABLE_TARGET_NODE:
+ case PM_MULTI_TARGET_NODE:
+/* Unreachable (clauses) ******************************************************/
+ case PM_ELSE_NODE:
+ case PM_ENSURE_NODE:
+ case PM_IN_NODE:
+ case PM_RESCUE_NODE:
+ case PM_WHEN_NODE:
+/* Unreachable (miscellaneous) ************************************************/
+ case PM_BLOCK_NODE:
+ case PM_EMBEDDED_STATEMENTS_NODE:
+ case PM_EMBEDDED_VARIABLE_NODE:
+ case PM_ERROR_RECOVERY_NODE:
+ case PM_PRE_EXECUTION_NODE:
+ case PM_PROGRAM_NODE:
+ case PM_SCOPE_NODE:
+ case PM_STATEMENTS_NODE:
+ rb_bug("Unreachable node in defined?: %s", pm_node_type(PM_NODE_TYPE(node)));
}
RUBY_ASSERT(dtype != DEFINED_NOT_DEFINED);
PUSH_INSN1(ret, location, putobject, PUSH_VAL(dtype));
+
#undef PUSH_VAL
}
static void
-pm_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition, LABEL **lfinish, bool explicit_receiver)
+pm_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node, bool in_condition, LABEL **lfinish)
{
LINK_ELEMENT *lcur = ret->last;
pm_compile_defined_expr0(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish, false);
@@ -3923,7 +4711,7 @@ pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_lo
lfinish[2] = 0;
if (!popped) {
- pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish, false);
+ pm_defined_expr(iseq, node, node_location, ret, popped, scope_node, in_condition, lfinish);
}
if (lfinish[1]) {
@@ -3951,13 +4739,11 @@ pm_add_ensure_iseq(LINK_ANCHOR *const ret, rb_iseq_t *iseq, int is_return, pm_sc
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);
@@ -3979,33 +4765,7 @@ pm_add_ensure_iseq(LINK_ANCHOR *const ret, rb_iseq_t *iseq, int is_return, pm_sc
PUSH_SEQ(ret, ensure);
}
-struct pm_local_table_insert_ctx {
- pm_scope_node_t *scope_node;
- rb_ast_id_table_t *local_table_for_iseq;
- int local_index;
-};
-
-static int
-pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
-{
- if (!existing) {
- pm_constant_id_t constant_id = (pm_constant_id_t) *key;
- struct pm_local_table_insert_ctx * ctx = (struct pm_local_table_insert_ctx *) arg;
-
- pm_scope_node_t *scope_node = ctx->scope_node;
- rb_ast_id_table_t *local_table_for_iseq = ctx->local_table_for_iseq;
- int local_index = ctx->local_index;
-
- ID local = pm_constant_id_lookup(scope_node, constant_id);
- local_table_for_iseq->ids[local_index] = local;
-
- *value = (st_data_t)local_index;
-
- ctx->local_index++;
- }
- return ST_CONTINUE;
-}
/**
* Insert a local into the local table for the iseq. This is used to create the
@@ -4013,24 +4773,23 @@ pm_local_table_insert_func(st_data_t *key, st_data_t *value, st_data_t arg, int
* inserted are regular named locals, as opposed to special forwarding locals.
*/
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)
+pm_insert_local_index(pm_constant_id_t constant_id, int local_index, pm_index_lookup_table_t *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node)
{
RUBY_ASSERT((constant_id & PM_SPECIAL_CONSTANT_FLAG) == 0);
ID local = pm_constant_id_lookup(scope_node, constant_id);
local_table_for_iseq->ids[local_index] = local;
- st_insert(index_lookup_table, (st_data_t) constant_id, (st_data_t) local_index);
+ pm_index_lookup_table_insert(index_lookup_table, constant_id, local_index);
}
/**
- * Insert a local into the local table for the iseq that is a special forwarding
- * local variable.
+ * Insert a special forwarding local (*, **, &, ...) into the local table.
*/
static void
-pm_insert_local_special(ID local_name, int local_index, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq)
+pm_insert_local_special(pm_constant_id_t special_id, ID local_name, int local_index, pm_index_lookup_table_t *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq)
{
local_table_for_iseq->ids[local_index] = local_name;
- st_insert(index_lookup_table, (st_data_t) (local_name | PM_SPECIAL_CONSTANT_FLAG), (st_data_t) local_index);
+ pm_index_lookup_table_insert(index_lookup_table, special_id, local_index);
}
/**
@@ -4040,7 +4799,7 @@ pm_insert_local_special(ID local_name, int local_index, st_table *index_lookup_t
* local and index lookup tables and increments the local index as necessary.
*/
static int
-pm_compile_destructured_param_locals(const pm_multi_target_node_t *node, st_table *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node, int local_index)
+pm_compile_destructured_param_locals(const pm_multi_target_node_t *node, pm_index_lookup_table_t *index_lookup_table, rb_ast_id_table_t *local_table_for_iseq, pm_scope_node_t *scope_node, int local_index)
{
for (size_t index = 0; index < node->lefts.size; index++) {
const pm_node_t *left = node->lefts.nodes[index];
@@ -4093,9 +4852,9 @@ pm_compile_destructured_param_locals(const pm_multi_target_node_t *node, st_tabl
* as a positional parameter in a method, block, or lambda definition.
*/
static inline void
-pm_compile_destructured_param_write(rb_iseq_t *iseq, const pm_required_parameter_node_t *node, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
+pm_compile_destructured_param_write(rb_iseq_t *iseq, const pm_required_parameter_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, node->name, 0);
PUSH_SETLOCAL(ret, location, index.index, index.level);
}
@@ -4109,9 +4868,9 @@ pm_compile_destructured_param_write(rb_iseq_t *iseq, const pm_required_parameter
* for this simplified case.
*/
static void
-pm_compile_destructured_param_writes(rb_iseq_t *iseq, const pm_multi_target_node_t *node, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
+pm_compile_destructured_param_writes(rb_iseq_t *iseq, const pm_multi_target_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
bool has_rest = (node->rest && PM_NODE_TYPE_P(node->rest, PM_SPLAT_NODE) && (((const pm_splat_node_t *) node->rest)->expression) != NULL);
bool has_rights = node->rights.size > 0;
@@ -4270,7 +5029,7 @@ pm_multi_target_state_update(pm_multi_target_state_t *state)
previous = current;
current = current->next;
- xfree(previous);
+ SIZED_FREE(previous);
}
}
@@ -4308,7 +5067,7 @@ pm_compile_multi_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR
static void
pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const parents, LINK_ANCHOR *const writes, LINK_ANCHOR *const cleanup, pm_scope_node_t *scope_node, pm_multi_target_state_t *state)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
switch (PM_NODE_TYPE(node)) {
case PM_LOCAL_VARIABLE_TARGET_NODE: {
@@ -4332,7 +5091,8 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons
const pm_class_variable_target_node_t *cast = (const pm_class_variable_target_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
- PUSH_INSN2(writes, location, setclassvariable, ID2SYM(name), get_cvar_ic_value(iseq, name));
+ VALUE operand = ID2SYM(name);
+ PUSH_INSN2(writes, location, setclassvariable, operand, get_cvar_ic_value(iseq, name));
break;
}
case PM_CONSTANT_TARGET_NODE: {
@@ -4344,8 +5104,9 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons
const pm_constant_target_node_t *cast = (const pm_constant_target_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
+ VALUE operand = ID2SYM(name);
PUSH_INSN1(writes, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
- PUSH_INSN1(writes, location, setconstant, ID2SYM(name));
+ PUSH_INSN1(writes, location, setconstant, operand);
break;
}
case PM_GLOBAL_VARIABLE_TARGET_NODE: {
@@ -4357,7 +5118,8 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons
const pm_global_variable_target_node_t *cast = (const pm_global_variable_target_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
- PUSH_INSN1(writes, location, setglobal, ID2SYM(name));
+ VALUE operand = ID2SYM(name);
+ PUSH_INSN1(writes, location, setglobal, operand);
break;
}
case PM_INSTANCE_VARIABLE_TARGET_NODE: {
@@ -4369,7 +5131,8 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons
const pm_instance_variable_target_node_t *cast = (const pm_instance_variable_target_node_t *) node;
ID name = pm_constant_id_lookup(scope_node, cast->name);
- PUSH_INSN2(writes, location, setinstancevariable, ID2SYM(name), get_ivar_ic_value(iseq, name));
+ VALUE operand = ID2SYM(name);
+ PUSH_INSN2(writes, location, setinstancevariable, operand, get_ivar_ic_value(iseq, name));
break;
}
case PM_CONSTANT_PATH_TARGET_NODE: {
@@ -4399,7 +5162,8 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons
pm_multi_target_state_push(state, (INSN *) LAST_ELEMENT(writes), 1);
}
- PUSH_INSN1(writes, location, setconstant, ID2SYM(name));
+ VALUE operand = ID2SYM(name);
+ PUSH_INSN1(writes, location, setconstant, operand);
if (state != NULL) {
PUSH_INSN(cleanup, location, pop);
@@ -4464,7 +5228,7 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons
int flags = 0;
struct rb_callinfo_kwarg *kwargs = NULL;
- int argc = pm_setup_args(cast->arguments, cast->block, &flags, &kwargs, iseq, parents, scope_node, &location);
+ int argc = pm_setup_args(cast->arguments, (const pm_node_t *) cast->block, &flags, &kwargs, iseq, parents, scope_node, &location);
if (state != NULL) {
PUSH_INSN1(writes, location, topn, INT2FIX(argc + 1));
@@ -4525,8 +5289,22 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons
break;
}
+ case PM_SPLAT_NODE: {
+ // Splat nodes capture all values into an array. They can be used
+ // as targets in assignments or for loops.
+ //
+ // for *x in []; end
+ //
+ const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
+
+ if (cast->expression != NULL) {
+ pm_compile_target_node(iseq, cast->expression, parents, writes, cleanup, scope_node, state);
+ }
+
+ break;
+ }
default:
- rb_bug("Unexpected node type: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
+ rb_bug("Unexpected node type: %s", pm_node_type(PM_NODE_TYPE(node)));
break;
}
}
@@ -4539,7 +5317,7 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons
static void
pm_compile_multi_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const parents, LINK_ANCHOR *const writes, LINK_ANCHOR *const cleanup, pm_scope_node_t *scope_node, pm_multi_target_state_t *state)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
const pm_node_list_t *lefts;
const pm_node_t *rest;
const pm_node_list_t *rights;
@@ -4560,7 +5338,7 @@ pm_compile_multi_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR
break;
}
default:
- rb_bug("Unsupported node %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
+ rb_bug("Unsupported node %s", pm_node_type(PM_NODE_TYPE(node)));
break;
}
@@ -4622,7 +5400,7 @@ pm_compile_multi_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR
static void
pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
switch (PM_NODE_TYPE(node)) {
case PM_LOCAL_VARIABLE_TARGET_NODE: {
@@ -4643,10 +5421,7 @@ pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *c
// owning expression of this target, then retrieve the value, expand it,
// and then compile the necessary writes.
DECL_ANCHOR(writes);
- INIT_ANCHOR(writes);
-
DECL_ANCHOR(cleanup);
- INIT_ANCHOR(cleanup);
pm_multi_target_state_t state = { 0 };
state.position = 1;
@@ -4661,12 +5436,10 @@ pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *c
pm_multi_target_state_update(&state);
break;
}
+ case PM_SPLAT_NODE:
case PM_MULTI_TARGET_NODE: {
DECL_ANCHOR(writes);
- INIT_ANCHOR(writes);
-
DECL_ANCHOR(cleanup);
- INIT_ANCHOR(cleanup);
pm_compile_target_node(iseq, node, ret, writes, cleanup, scope_node, NULL);
@@ -4699,12 +5472,18 @@ pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *c
PUSH_INSN(ret, location, pop);
PUSH_LABEL(ret, not_single);
+
+ if (PM_NODE_TYPE_P(node, PM_SPLAT_NODE)) {
+ const pm_splat_node_t *cast = (const pm_splat_node_t *) node;
+ PUSH_INSN2(ret, location, expandarray, INT2FIX(0), INT2FIX(cast->expression == NULL ? 0 : 1));
+ }
+
PUSH_SEQ(ret, writes);
PUSH_SEQ(ret, cleanup);
break;
}
default:
- rb_bug("Unexpected node type for index in for node: %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
+ rb_bug("Unexpected node type for index in for node: %s", pm_node_type(PM_NODE_TYPE(node)));
break;
}
}
@@ -4712,8 +5491,6 @@ pm_compile_for_node_index(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *c
static void
pm_compile_rescue(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
- const pm_parser_t *parser = scope_node->parser;
-
LABEL *lstart = NEW_LABEL(node_location->line);
LABEL *lend = NEW_LABEL(node_location->line);
LABEL *lcont = NEW_LABEL(node_location->line);
@@ -4725,7 +5502,7 @@ pm_compile_rescue(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_lo
&rescue_scope_node,
rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
ISEQ_TYPE_RESCUE,
- pm_node_line_number(parser, (const pm_node_t *) cast->rescue_clause)
+ pm_node_line_number_cached((const pm_node_t *) cast->rescue_clause, scope_node)
);
pm_scope_node_destroy(&rescue_scope_node);
@@ -4741,7 +5518,7 @@ pm_compile_rescue(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_lo
PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->statements);
}
else {
- const pm_node_location_t location = PM_NODE_START_LOCATION(parser, cast->rescue_clause);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(cast->rescue_clause);
PUSH_INSN(ret, location, putnil);
}
@@ -4764,12 +5541,11 @@ pm_compile_rescue(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_lo
static void
pm_compile_ensure(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
{
- const pm_parser_t *parser = scope_node->parser;
const pm_statements_node_t *statements = cast->ensure_clause->statements;
pm_node_location_t location;
if (statements != NULL) {
- location = PM_NODE_START_LOCATION(parser, statements);
+ location = PM_NODE_START_LOCATION(statements);
}
else {
location = *node_location;
@@ -4784,7 +5560,6 @@ pm_compile_ensure(rb_iseq_t *iseq, const pm_begin_node_t *cast, const pm_node_lo
struct ensure_range *erange;
DECL_ANCHOR(ensr);
- INIT_ANCHOR(ensr);
if (statements != NULL) {
pm_compile_node(iseq, (const pm_node_t *) statements, ensr, true, scope_node);
}
@@ -4854,55 +5629,18 @@ pm_opt_str_freeze_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
}
/**
- * Returns true if the given call node can use the opt_aref_with optimization
- * with the current iseq options.
- */
-static inline bool
-pm_opt_aref_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
-{
- return (
- !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
- node->arguments != NULL &&
- PM_NODE_TYPE_P((const pm_node_t *) node->arguments, PM_ARGUMENTS_NODE) &&
- ((const pm_arguments_node_t *) node->arguments)->arguments.size == 1 &&
- PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
- node->block == NULL &&
- !PM_NODE_FLAG_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
- ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
- );
-}
-
-/**
- * Returns true if the given call node can use the opt_aset_with optimization
- * with the current iseq options.
- */
-static inline bool
-pm_opt_aset_with_p(const rb_iseq_t *iseq, const pm_call_node_t *node)
-{
- return (
- !PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION) &&
- node->arguments != NULL &&
- PM_NODE_TYPE_P((const pm_node_t *) node->arguments, PM_ARGUMENTS_NODE) &&
- ((const pm_arguments_node_t *) node->arguments)->arguments.size == 2 &&
- PM_NODE_TYPE_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_NODE) &&
- node->block == NULL &&
- !PM_NODE_FLAG_P(((const pm_arguments_node_t *) node->arguments)->arguments.nodes[0], PM_STRING_FLAGS_FROZEN) &&
- ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction
- );
-}
-
-/**
* Compile the instructions necessary to read a constant, based on the options
* of the current iseq.
*/
static void
-pm_compile_constant_read(rb_iseq_t *iseq, VALUE name, const pm_location_t *name_loc, uint32_t node_id, LINK_ANCHOR *const ret, const pm_scope_node_t *scope_node)
+pm_compile_constant_read(rb_iseq_t *iseq, VALUE name, const pm_location_t *name_loc, uint32_t node_id, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, name_loc, node_id);
+ const pm_node_location_t location = PM_LOCATION_START_LOCATION(name_loc, node_id);
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache) {
ISEQ_BODY(iseq)->ic_size++;
VALUE segments = rb_ary_new_from_args(1, name);
+ RB_OBJ_SET_SHAREABLE(segments);
PUSH_INSN1(ret, location, opt_getconstant_path, segments);
}
else {
@@ -4957,7 +5695,7 @@ pm_constant_path_parts(const pm_node_t *node, const pm_scope_node_t *scope_node)
static void
pm_compile_constant_path(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const prefix, LINK_ANCHOR *const body, bool popped, pm_scope_node_t *scope_node)
{
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
switch (PM_NODE_TYPE(node)) {
case PM_CONSTANT_READ_NODE: {
@@ -4992,67 +5730,10 @@ pm_compile_constant_path(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *co
}
/**
- * When we're compiling a case node, it's possible that we can speed it up using
- * a dispatch hash, which will allow us to jump directly to the correct when
- * clause body based on a hash lookup of the value. This can only happen when
- * the conditions are literals that can be compiled into a hash key.
- *
- * This function accepts a dispatch hash and the condition of a when clause. It
- * is responsible for compiling the condition into a hash key and then adding it
- * to the dispatch hash.
- *
- * If the value can be successfully compiled into the hash, then this function
- * returns the dispatch hash with the new key added. If the value cannot be
- * compiled into the hash, then this function returns Qundef. In the case of
- * Qundef, this function is signaling that the caller should abandon the
- * optimization entirely.
- */
-static VALUE
-pm_compile_case_node_dispatch(rb_iseq_t *iseq, VALUE dispatch, const pm_node_t *node, LABEL *label, const pm_scope_node_t *scope_node)
-{
- VALUE key = Qundef;
-
- switch (PM_NODE_TYPE(node)) {
- case PM_FLOAT_NODE: {
- key = pm_static_literal_value(iseq, node, scope_node);
- double intptr;
-
- if (modf(RFLOAT_VALUE(key), &intptr) == 0.0) {
- key = (FIXABLE(intptr) ? LONG2FIX((long) intptr) : rb_dbl2big(intptr));
- }
-
- break;
- }
- case PM_FALSE_NODE:
- case PM_INTEGER_NODE:
- case PM_NIL_NODE:
- case PM_SOURCE_FILE_NODE:
- case PM_SOURCE_LINE_NODE:
- case PM_SYMBOL_NODE:
- case PM_TRUE_NODE:
- key = pm_static_literal_value(iseq, node, scope_node);
- break;
- case PM_STRING_NODE: {
- const pm_string_node_t *cast = (const pm_string_node_t *) node;
- key = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
- break;
- }
- default:
- return Qundef;
- }
-
- if (NIL_P(rb_hash_lookup(dispatch, key))) {
- rb_hash_aset(dispatch, key, ((VALUE) label) | 1);
- }
-
- return dispatch;
-}
-
-/**
* Return the object that will be pushed onto the stack for the given node.
*/
static VALUE
-pm_compile_shareable_constant_literal(rb_iseq_t *iseq, const pm_node_t *node, const pm_scope_node_t *scope_node)
+pm_compile_shareable_constant_literal(rb_iseq_t *iseq, const pm_node_t *node, pm_scope_node_t *scope_node)
{
switch (PM_NODE_TYPE(node)) {
case PM_TRUE_NODE:
@@ -5086,7 +5767,7 @@ pm_compile_shareable_constant_literal(rb_iseq_t *iseq, const pm_node_t *node, co
}
case PM_HASH_NODE: {
const pm_hash_node_t *cast = (const pm_hash_node_t *) node;
- VALUE result = rb_hash_new_capa(cast->elements.size);
+ VALUE result = rb_hash_alloc_fixed_size(rb_cHash, cast->elements.size);
for (size_t index = 0; index < cast->elements.size; index++) {
const pm_node_t *element = cast->elements.nodes[index];
@@ -5119,12 +5800,12 @@ pm_compile_shareable_constant_value(rb_iseq_t *iseq, const pm_node_t *node, cons
{
VALUE literal = pm_compile_shareable_constant_literal(iseq, node, scope_node);
if (literal != Qundef) {
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
PUSH_INSN1(ret, location, putobject, literal);
return;
}
- const pm_node_location_t location = PM_NODE_START_LOCATION(scope_node->parser, node);
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
switch (PM_NODE_TYPE(node)) {
case PM_ARRAY_NODE: {
const pm_array_node_t *cast = (const pm_array_node_t *) node;
@@ -5153,19 +5834,7 @@ pm_compile_shareable_constant_value(rb_iseq_t *iseq, const pm_node_t *node, cons
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
}
- for (size_t index = 0; index < cast->elements.size; index++) {
- const pm_node_t *element = cast->elements.nodes[index];
-
- if (!PM_NODE_TYPE_P(element, PM_ASSOC_NODE)) {
- COMPILE_ERROR(iseq, location.line, "Ractor constant writes do not support **");
- }
-
- const pm_assoc_node_t *assoc = (const pm_assoc_node_t *) element;
- pm_compile_shareable_constant_value(iseq, assoc->key, shareability, path, ret, scope_node, false);
- pm_compile_shareable_constant_value(iseq, assoc->value, shareability, path, ret, scope_node, false);
- }
-
- PUSH_INSN1(ret, location, newhash, INT2FIX(cast->elements.size * 2));
+ pm_compile_hash_elements(iseq, (const pm_node_t *) cast, &cast->elements, shareability, path, false, ret, scope_node);
if (top) {
ID method_id = (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY) ? rb_intern("make_shareable_copy") : rb_intern("make_shareable");
@@ -5176,7 +5845,6 @@ pm_compile_shareable_constant_value(rb_iseq_t *iseq, const pm_node_t *node, cons
}
default: {
DECL_ANCHOR(value_seq);
- INIT_ANCHOR(value_seq);
pm_compile_node(iseq, node, value_seq, false, scope_node);
if (PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) {
@@ -5186,6 +5854,9 @@ pm_compile_shareable_constant_value(rb_iseq_t *iseq, const pm_node_t *node, cons
if (shareability & PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL) {
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PUSH_SEQ(ret, value_seq);
+ if (!RB_OBJ_SHAREABLE_P(path)) {
+ RB_OBJ_SET_SHAREABLE(path);
+ }
PUSH_INSN1(ret, location, putobject, path);
PUSH_SEND_WITH_FLAG(ret, location, rb_intern("ensure_shareable"), INT2FIX(2), INT2FIX(VM_CALL_ARGS_SIMPLE));
}
@@ -5224,7 +5895,9 @@ pm_compile_constant_write_node(rb_iseq_t *iseq, const pm_constant_write_node_t *
if (!popped) PUSH_INSN(ret, location, dup);
PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
- PUSH_INSN1(ret, location, setconstant, ID2SYM(name_id));
+
+ VALUE operand = ID2SYM(name_id);
+ PUSH_INSN1(ret, location, setconstant, operand);
}
/**
@@ -5538,1021 +6211,2718 @@ pm_compile_constant_path_operator_write_node(rb_iseq_t *iseq, const pm_constant_
}
/**
- * Compiles a prism node into instruction sequences.
- *
- * iseq - The current instruction sequence object (used for locals)
- * node - The prism node to compile
- * ret - The linked list of instructions to append instructions onto
- * popped - True if compiling something with no side effects, so instructions don't
- * need to be added
- * scope_node - Stores parser and local information
+ * Many nodes in Prism can be marked as a static literal, which means slightly
+ * different things depending on which node it is. Occasionally we need to omit
+ * container nodes from static literal checks, which is where this macro comes
+ * in.
*/
-static void
-pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+#define PM_CONTAINER_P(node) (PM_NODE_TYPE_P(node, PM_ARRAY_NODE) || PM_NODE_TYPE_P(node, PM_HASH_NODE) || PM_NODE_TYPE_P(node, PM_RANGE_NODE))
+
+/**
+ * Compile a scope node, which is a special kind of node that represents a new
+ * lexical scope, attached to a node in the AST.
+ */
+static inline void
+pm_compile_scope_node(rb_iseq_t *iseq, pm_scope_node_t *scope_node, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped)
{
- const pm_parser_t *parser = scope_node->parser;
- const pm_node_location_t location = PM_NODE_START_LOCATION(parser, node);
- int lineno = (int) location.line;
+ const pm_node_location_t location = *node_location;
+ struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
- if (PM_NODE_TYPE_P(node, PM_BEGIN_NODE) && (((const pm_begin_node_t *) node)->statements == NULL) && (((const pm_begin_node_t *) node)->rescue_clause != NULL)) {
- // If this node is a begin node and it has empty statements and also
- // has a rescue clause, then the other parser considers it as
- // starting on the same line as the rescue, as opposed to the
- // location of the begin keyword. We replicate that behavior here.
- lineno = (int) PM_NODE_START_LINE_COLUMN(parser, ((const pm_begin_node_t *) node)->rescue_clause).line;
+ pm_constant_id_list_t *locals = &scope_node->locals;
+ pm_parameters_node_t *parameters_node = NULL;
+ pm_node_list_t *keywords_list = NULL;
+ pm_node_list_t *optionals_list = NULL;
+ pm_node_list_t *posts_list = NULL;
+ pm_node_list_t *requireds_list = NULL;
+ pm_node_list_t *block_locals = NULL;
+ bool trailing_comma = false;
+
+ if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
+ PUSH_TRACE(ret, RUBY_EVENT_CLASS);
+ }
+
+ if (scope_node->parameters != NULL) {
+ switch (PM_NODE_TYPE(scope_node->parameters)) {
+ case PM_BLOCK_PARAMETERS_NODE: {
+ pm_block_parameters_node_t *cast = (pm_block_parameters_node_t *) scope_node->parameters;
+ parameters_node = cast->parameters;
+ block_locals = &cast->locals;
+
+ if (parameters_node) {
+ if (parameters_node->rest && PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE)) {
+ trailing_comma = true;
+ }
+ }
+ break;
+ }
+ case PM_PARAMETERS_NODE: {
+ parameters_node = (pm_parameters_node_t *) scope_node->parameters;
+ break;
+ }
+ case PM_NUMBERED_PARAMETERS_NODE: {
+ uint32_t maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
+ body->param.lead_num = maximum;
+ body->param.flags.ambiguous_param0 = maximum == 1;
+ break;
+ }
+ case PM_IT_PARAMETERS_NODE:
+ body->param.lead_num = 1;
+ body->param.flags.ambiguous_param0 = true;
+ break;
+ default:
+ rb_bug("Unexpected node type for parameters: %s", pm_node_type(PM_NODE_TYPE(scope_node->parameters)));
+ }
}
- if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
- // If this node has the newline flag set and it is on a new line
- // from the previous nodes that have been compiled for this ISEQ,
- // then we need to emit a newline event.
- int event = RUBY_EVENT_LINE;
+ struct rb_iseq_param_keyword *keyword = NULL;
- ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
- if (ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
- event |= RUBY_EVENT_COVERAGE_LINE;
+ if (parameters_node) {
+ optionals_list = &parameters_node->optionals;
+ requireds_list = &parameters_node->requireds;
+ keywords_list = &parameters_node->keywords;
+ posts_list = &parameters_node->posts;
+ }
+ else if (scope_node->parameters && (PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE) || PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE))) {
+ body->param.opt_num = 0;
+ }
+ else {
+ body->param.lead_num = 0;
+ body->param.opt_num = 0;
+ }
+
+ //********STEP 1**********
+ // Goal: calculate the table size for the locals, accounting for
+ // hidden variables and multi target nodes
+ size_t locals_size = locals->size;
+
+ // Index lookup table buffer size is only the number of the locals.
+ // We'll initialize it after computing table_size below.
+ pm_index_lookup_table_t index_lookup_table = PM_INDEX_LOOKUP_TABLE_INIT;
+
+ int table_size = (int) locals_size;
+
+ // For nodes have a hidden iteration variable. We add that to the local
+ // table size here.
+ if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
+
+ if (keywords_list && keywords_list->size) {
+ table_size++;
+ }
+
+ 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++;
+ }
+ else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
+ if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ table_size++;
+ }
+ }
}
- PUSH_TRACE(ret, event);
}
- switch (PM_NODE_TYPE(node)) {
- case PM_ALIAS_GLOBAL_VARIABLE_NODE: {
- // alias $foo $bar
- // ^^^^^^^^^^^^^^^
- const pm_alias_global_variable_node_t *cast = (const pm_alias_global_variable_node_t *) node;
- PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
+ // If we have the `it` implicit local variable, we need to account for
+ // it in the local table size.
+ if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
+ table_size++;
+ }
- const pm_location_t *new_name_loc = &cast->new_name->location;
- PUSH_INSN1(ret, location, putobject, ID2SYM(rb_intern3((const char *) new_name_loc->start, new_name_loc->end - new_name_loc->start, scope_node->encoding)));
+ // Ensure there is enough room in the local table for any
+ // parameters that have been repeated
+ // ex: def underscore_parameters(_, _ = 1, _ = 2); _; end
+ // ^^^^^^^^^^^^
+ if (optionals_list && optionals_list->size) {
+ for (size_t i = 0; i < optionals_list->size; i++) {
+ pm_node_t * node = optionals_list->nodes[i];
+ if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ table_size++;
+ }
+ }
+ }
- const pm_location_t *old_name_loc = &cast->old_name->location;
- PUSH_INSN1(ret, location, putobject, ID2SYM(rb_intern3((const char *) old_name_loc->start, old_name_loc->end - old_name_loc->start, scope_node->encoding)));
+ // If we have an anonymous "rest" node, we'll need to increase the local
+ // table size to take it in to account.
+ // def m(foo, *, bar)
+ // ^
+ if (parameters_node) {
+ if (parameters_node->rest) {
+ if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
+ if (!((const pm_rest_parameter_node_t *) parameters_node->rest)->name || PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ table_size++;
+ }
+ }
+ }
- PUSH_SEND(ret, location, id_core_set_variable_alias, INT2FIX(2));
- if (popped) PUSH_INSN(ret, location, pop);
+ // def foo(_, **_); _; end
+ // ^^^
+ if (parameters_node->keyword_rest) {
+ // def foo(...); end
+ // ^^^
+ // When we have a `...` as the keyword_rest, it's a forwarding_parameter_node and
+ // we need to leave space for 4 locals: *, **, &, ...
+ if (PM_NODE_TYPE_P(parameters_node->keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
+ // Only optimize specifically methods like this: `foo(...)`
+ if (requireds_list->size == 0 && optionals_list->size == 0 && keywords_list->size == 0) {
+ ISEQ_BODY(iseq)->param.flags.use_block = TRUE;
+ ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
+ table_size += 1;
+ }
+ else {
+ table_size += 4;
+ }
+ }
+ else {
+ const pm_keyword_rest_parameter_node_t *kw_rest = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
- return;
- }
- case PM_ALIAS_METHOD_NODE: {
- // alias foo bar
- // ^^^^^^^^^^^^^
- const pm_alias_method_node_t *cast = (const pm_alias_method_node_t *) node;
+ // If it's anonymous or repeated, then we need to allocate stack space
+ if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ table_size++;
+ }
+ }
+ }
+ }
- PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
- PUSH_INSN1(ret, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
- PM_COMPILE_NOT_POPPED(cast->new_name);
- PM_COMPILE_NOT_POPPED(cast->old_name);
+ 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) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ table_size++;
+ }
+ }
+ }
- PUSH_SEND(ret, location, id_core_set_method_alias, INT2FIX(3));
- if (popped) PUSH_INSN(ret, location, pop);
+ if (keywords_list && keywords_list->size) {
+ for (size_t i = 0; i < keywords_list->size; i++) {
+ pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
+ if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ table_size++;
+ }
+ }
+ }
- return;
- }
- case PM_AND_NODE: {
- // a and b
- // ^^^^^^^
- const pm_and_node_t *cast = (const pm_and_node_t *) node;
- LABEL *end_label = NEW_LABEL(lineno);
+ if (parameters_node && parameters_node->block && PM_NODE_TYPE_P(parameters_node->block, PM_BLOCK_PARAMETER_NODE)) {
+ const pm_block_parameter_node_t *block_node = (const pm_block_parameter_node_t *) parameters_node->block;
- PM_COMPILE_NOT_POPPED(cast->left);
- if (!popped) PUSH_INSN(ret, location, dup);
- PUSH_INSNL(ret, location, branchunless, end_label);
+ if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
+ table_size++;
+ }
+ }
- if (!popped) PUSH_INSN(ret, location, pop);
- PM_COMPILE(cast->right);
- PUSH_LABEL(ret, end_label);
+ // 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;
+
+ // Init the direct-indexed lookup table. The capacity is based on the
+ // parser's constant pool size (for regular locals) plus special slots.
+ pm_index_lookup_table_init(&index_lookup_table, (int) pm_parser_constants_size(scope_node->parser), iseq);
+
+ //********END OF STEP 1**********
+
+ //********STEP 2**********
+ // Goal: populate iv index table as well as local table, keeping the
+ // layout of the local table consistent with the layout of the
+ // stack when calling the method
+ //
+ // Do a first pass on all of the parameters, setting their values in
+ // the local_table_for_iseq, _except_ for Multis who get a hidden
+ // variable in this step, and will get their names inserted in step 3
+
+ // local_index is a cursor that keeps track of the current
+ // index into local_table_for_iseq. The local table is actually a list,
+ // and the order of that list must match the order of the items pushed
+ // on the stack. We need to take in to account things pushed on the
+ // stack that _might not have a name_ (for example array destructuring).
+ // This index helps us know which item we're dealing with and also give
+ // those anonymous items temporary names (as below)
+ int local_index = 0;
+
+ // Here we figure out local table indices and insert them in to the
+ // index lookup table and local tables.
+ //
+ // 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)) {
+ case PM_MULTI_TARGET_NODE: {
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^^^^^^^^^
+ local = rb_make_temporary_id(local_index);
+ local_table_for_iseq->ids[local_index] = local;
+ break;
+ }
+ case PM_REQUIRED_PARAMETER_NODE: {
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^
+ const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) required;
- return;
- }
- case PM_ARGUMENTS_NODE:
- // These are ArgumentsNodes that are not compiled directly by their
- // parent call nodes, used in the cases of NextNodes, ReturnNodes, and
- // BreakNodes. They can create an array like ArrayNode.
- case PM_ARRAY_NODE: {
- const pm_node_list_t *elements;
+ if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ ID local = pm_constant_id_lookup(scope_node, param->name);
+ local_table_for_iseq->ids[local_index] = local;
+ }
+ else {
+ pm_insert_local_index(param->name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
+ }
+
+ break;
+ }
+ default:
+ rb_bug("Unsupported node in requireds in parameters %s", pm_node_type(PM_NODE_TYPE(required)));
+ }
+ }
- if (PM_NODE_TYPE(node) == PM_ARGUMENTS_NODE) {
- // break foo
- // ^^^
- const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
- elements = &cast->arguments;
+ body->param.lead_num = (int) requireds_list->size;
+ body->param.flags.has_lead = true;
+ }
- // If we are only returning a single element through one of the jump
- // nodes, then we will only compile that node directly.
- if (elements->size == 1) {
- PM_COMPILE(elements->nodes[0]);
- return;
+ if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
+ local_table_for_iseq->ids[local_index++] = idItImplicit;
+ }
+
+ // 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_node_t * node = optionals_list->nodes[i];
+ pm_constant_id_t name = ((const pm_optional_parameter_node_t *) node)->name;
+
+ if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ ID local = pm_constant_id_lookup(scope_node, name);
+ local_table_for_iseq->ids[local_index] = local;
+ }
+ else {
+ pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
}
}
- else {
- // [foo, bar, baz]
- // ^^^^^^^^^^^^^^^
- const pm_array_node_t *cast = (const pm_array_node_t *) node;
- elements = &cast->elements;
+ }
+
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^
+ if (parameters_node && parameters_node->rest) {
+ body->param.rest_start = local_index;
+
+ // 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;
+ RUBY_ASSERT(body->param.rest_start != -1);
+
+ pm_constant_id_t name = ((const pm_rest_parameter_node_t *) parameters_node->rest)->name;
+
+ if (name) {
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^
+ if (PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ ID local = pm_constant_id_lookup(scope_node, name);
+ local_table_for_iseq->ids[local_index] = local;
+ }
+ else {
+ pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
+ }
+ }
+ else {
+ // def foo(a, (b, *c, d), e = 1, *, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^
+ body->param.flags.anon_rest = true;
+ pm_insert_local_special(PM_CONSTANT_MULT, idMULT, local_index, &index_lookup_table, local_table_for_iseq);
+ }
+
+ local_index++;
}
+ }
- // If every node in the array is static, then we can compile the entire
- // array now instead of later.
- if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
- // 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) {
- if (elements->size) {
- VALUE value = pm_static_literal_value(iseq, node, scope_node);
- PUSH_INSN1(ret, location, duparray, value);
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^^^^^^^^^^^^
+ if (posts_list && posts_list->size) {
+ body->param.post_num = (int) posts_list->size;
+ body->param.post_start = local_index;
+ body->param.flags.has_post = true;
+
+ 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.
+ const pm_node_t *post_node = posts_list->nodes[i];
+
+ switch (PM_NODE_TYPE(post_node)) {
+ case PM_MULTI_TARGET_NODE: {
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^^^^^^^^^
+ local = rb_make_temporary_id(local_index);
+ local_table_for_iseq->ids[local_index] = local;
+ break;
+ }
+ case PM_REQUIRED_PARAMETER_NODE: {
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^
+ const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) post_node;
+
+ if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ ID local = pm_constant_id_lookup(scope_node, param->name);
+ local_table_for_iseq->ids[local_index] = local;
}
else {
- PUSH_INSN1(ret, location, newarray, INT2FIX(0));
+ pm_insert_local_index(param->name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
}
+ break;
+ }
+ default:
+ rb_bug("Unsupported node in posts in parameters %s", pm_node_type(PM_NODE_TYPE(post_node)));
}
}
- 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 array is popped, then this serves only to ensure we enact
- // all side-effects (like method calls) that are contained within
- // the array contents.
- //
- // We treat all sequences of non-splat elements as their
- // own arrays, followed by a newarray, and then continually
- // concat the arrays with the SplatNode nodes.
- const int max_new_array_size = 0x100;
- const unsigned int min_tmp_array_size = 0x40;
-
- int new_array_size = 0;
- bool first_chunk = true;
-
- // This is an optimization wherein we keep track of whether or not
- // the previous element was a static literal. If it was, then we do
- // not attempt to check if we have a subarray that can be optimized.
- // If it was not, then we do check.
- bool static_literal = false;
-
- // Either create a new array, or push to the existing array.
-#define FLUSH_CHUNK \
- if (new_array_size) { \
- if (first_chunk) PUSH_INSN1(ret, location, newarray, INT2FIX(new_array_size)); \
- else PUSH_INSN1(ret, location, pushtoarray, INT2FIX(new_array_size)); \
- first_chunk = false; \
- new_array_size = 0; \
+ }
+
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^^^^^^^
+ // Keywords create an internal variable on the parse tree
+ if (keywords_list && keywords_list->size) {
+ keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
+ keyword->num = (int) keywords_list->size;
+
+ const VALUE default_values = rb_ary_hidden_new(1);
+ const VALUE complex_mark = rb_str_tmp_new(0);
+
+ for (size_t i = 0; i < keywords_list->size; i++) {
+ pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
+ pm_constant_id_t name;
+
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^
+ if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
+ name = ((const pm_required_keyword_parameter_node_t *) keyword_parameter_node)->name;
+ keyword->required_num++;
+ ID local = pm_constant_id_lookup(scope_node, name);
+
+ if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ local_table_for_iseq->ids[local_index] = local;
+ }
+ else {
+ pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
+ }
+ local_index++;
}
+ }
- for (size_t index = 0; index < elements->size; index++) {
- const pm_node_t *element = elements->nodes[index];
+ for (size_t i = 0; i < keywords_list->size; i++) {
+ pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
+ pm_constant_id_t name;
- if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
- FLUSH_CHUNK;
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^^^
+ if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
+ const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
- const pm_splat_node_t *splat_element = (const pm_splat_node_t *) element;
- if (splat_element->expression) {
- PM_COMPILE_NOT_POPPED(splat_element->expression);
- }
- else {
- pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
- PUSH_GETLOCAL(ret, location, index.index, index.level);
- }
+ pm_node_t *value = cast->value;
+ name = cast->name;
- if (first_chunk) {
- // If this is the first element of the array then we
- // need to splatarray the elements into the list.
- PUSH_INSN1(ret, location, splatarray, Qtrue);
- first_chunk = false;
- }
- else {
- PUSH_INSN(ret, location, concattoarray);
- }
+ if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !PM_CONTAINER_P(value)) {
+ rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
+ }
+ else {
+ rb_ary_push(default_values, complex_mark);
+ }
- static_literal = false;
+ ID local = pm_constant_id_lookup(scope_node, name);
+ if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ local_table_for_iseq->ids[local_index] = local;
}
- else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
- if (new_array_size == 0 && first_chunk) {
- PUSH_INSN1(ret, location, newarray, INT2FIX(0));
- first_chunk = false;
- }
- else {
- FLUSH_CHUNK;
- }
+ else {
+ pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
+ }
+ local_index++;
+ }
- // If we get here, then this is the last element of the
- // array/arguments, because it cannot be followed by
- // anything else without a syntax error. This looks like:
- //
- // [foo, bar, baz: qux]
- // ^^^^^^^^
- //
- // [foo, bar, **baz]
- // ^^^^^
- //
- const pm_keyword_hash_node_t *keyword_hash = (const pm_keyword_hash_node_t *) element;
- pm_compile_hash_elements(iseq, element, &keyword_hash->elements, false, ret, scope_node);
-
- // This boolean controls the manner in which we push the
- // hash onto the array. If it's all keyword splats, then we
- // can use the very specialized pushtoarraykwsplat
- // instruction to check if it's empty before we push it.
- size_t splats = 0;
- while (splats < keyword_hash->elements.size && PM_NODE_TYPE_P(keyword_hash->elements.nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
-
- if (keyword_hash->elements.size == splats) {
- PUSH_INSN(ret, location, pushtoarraykwsplat);
+ }
+
+ if (RARRAY_LEN(default_values)) {
+ 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;
+ RB_OBJ_WRITE(iseq, &dvs[i], dv);
+ }
+
+ keyword->default_values = dvs;
+ }
+
+ // Hidden local for keyword arguments
+ keyword->bits_start = local_index;
+ ID local = rb_make_temporary_id(local_index);
+ local_table_for_iseq->ids[local_index] = local;
+ local_index++;
+
+ body->param.keyword = keyword;
+ body->param.flags.has_kw = true;
+ }
+
+ if (body->type == ISEQ_TYPE_BLOCK && local_index == 1 && requireds_list && requireds_list->size == 1 && !trailing_comma) {
+ body->param.flags.ambiguous_param0 = true;
+ }
+
+ if (parameters_node) {
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^^
+ if (parameters_node->keyword_rest) {
+ switch (PM_NODE_TYPE(parameters_node->keyword_rest)) {
+ case PM_NO_KEYWORDS_PARAMETER_NODE: {
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **nil, &n)
+ // ^^^^^
+ body->param.flags.accepts_no_kwarg = true;
+ break;
+ }
+ case PM_KEYWORD_REST_PARAMETER_NODE: {
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^^
+ const pm_keyword_rest_parameter_node_t *kw_rest_node = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
+ if (!body->param.flags.has_kw) {
+ body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
+ }
+
+ keyword->rest_start = local_index;
+ body->param.flags.has_kwrest = true;
+
+ pm_constant_id_t constant_id = kw_rest_node->name;
+ if (constant_id) {
+ if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ ID local = pm_constant_id_lookup(scope_node, constant_id);
+ local_table_for_iseq->ids[local_index] = local;
}
else {
- new_array_size++;
+ pm_insert_local_index(constant_id, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
}
}
- else if (PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) && !static_literal && ((index + min_tmp_array_size) < elements->size)) {
- // If we have a static literal, then there's the potential
- // to group a bunch of them together with a literal array
- // and then concat them together.
- size_t right_index = index + 1;
- while (right_index < elements->size && PM_NODE_FLAG_P(elements->nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL)) right_index++;
-
- size_t tmp_array_size = right_index - index;
- if (tmp_array_size >= min_tmp_array_size) {
- VALUE tmp_array = rb_ary_hidden_new(tmp_array_size);
-
- // Create the temporary array.
- for (; tmp_array_size; tmp_array_size--)
- rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->nodes[index++], scope_node));
- OBJ_FREEZE(tmp_array);
-
- // Emit the optimized code.
- FLUSH_CHUNK;
- if (first_chunk) {
- PUSH_INSN1(ret, location, duparray, tmp_array);
- first_chunk = false;
- }
- else {
- PUSH_INSN1(ret, location, putobject, tmp_array);
- PUSH_INSN(ret, location, concattoarray);
- }
+ else {
+ body->param.flags.anon_kwrest = true;
+ pm_insert_local_special(PM_CONSTANT_POW, idPow, local_index, &index_lookup_table, local_table_for_iseq);
+ }
+
+ local_index++;
+ break;
+ }
+ case PM_FORWARDING_PARAMETER_NODE: {
+ // def foo(...)
+ // ^^^
+ if (!ISEQ_BODY(iseq)->param.flags.forwardable) {
+ // Add the anonymous *
+ body->param.rest_start = local_index;
+ body->param.flags.has_rest = true;
+ body->param.flags.anon_rest = true;
+ pm_insert_local_special(PM_CONSTANT_MULT, idMULT, local_index++, &index_lookup_table, local_table_for_iseq);
+
+ // Add the anonymous **
+ RUBY_ASSERT(!body->param.flags.has_kw);
+ body->param.flags.has_kw = false;
+ body->param.flags.has_kwrest = true;
+ body->param.flags.anon_kwrest = true;
+ body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
+ keyword->rest_start = local_index;
+ pm_insert_local_special(PM_CONSTANT_POW, idPow, local_index++, &index_lookup_table, local_table_for_iseq);
+
+ // Add the anonymous &
+ body->param.block_start = local_index;
+ body->param.flags.has_block = true;
+ pm_insert_local_special(PM_CONSTANT_AND, idAnd, local_index++, &index_lookup_table, local_table_for_iseq);
+ }
+
+ // Add the ...
+ pm_insert_local_special(PM_CONSTANT_DOT3, idDot3, local_index++, &index_lookup_table, local_table_for_iseq);
+ break;
+ }
+ default:
+ rb_bug("node type %s not expected as keyword_rest", pm_node_type(PM_NODE_TYPE(parameters_node->keyword_rest)));
+ }
+ }
+
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^
+ if (parameters_node->block) {
+ switch (PM_NODE_TYPE(parameters_node->block)) {
+ case PM_BLOCK_PARAMETER_NODE: {
+ body->param.block_start = local_index;
+ body->param.flags.has_block = true;
+
+ iseq_set_use_block(iseq);
+
+ pm_constant_id_t name = ((const pm_block_parameter_node_t *) parameters_node->block)->name;
+
+ if (name) {
+ if (PM_NODE_FLAG_P(parameters_node->block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
+ ID local = pm_constant_id_lookup(scope_node, name);
+ local_table_for_iseq->ids[local_index] = local;
}
else {
- static_literal = true;
+ pm_insert_local_index(name, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
}
- } else {
- PM_COMPILE_NOT_POPPED(element);
- if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
- static_literal = false;
}
+ else {
+ pm_insert_local_special(PM_CONSTANT_AND, idAnd, local_index, &index_lookup_table, local_table_for_iseq);
+ }
+
+ local_index++;
+ break;
+ }
+ case PM_NO_BLOCK_PARAMETER_NODE: {
+ body->param.flags.accepts_no_block = true;
+ break;
+ }
+ default:
+ rb_bug("node type %s not expected as block parameter", pm_node_type(PM_NODE_TYPE(parameters_node->block)));
}
+ }
+ }
- FLUSH_CHUNK;
- if (popped) PUSH_INSN(ret, location, pop);
+ //********END OF STEP 2**********
+ // The local table is now consistent with expected
+ // stack layout
+
+ // If there's only one required element in the parameters
+ // CRuby needs to recognize it as an ambiguous parameter
+
+ //********STEP 3**********
+ // Goal: fill in the names of the parameters in MultiTargetNodes
+ //
+ // Go through requireds again to set the multis
+
+ 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
+ const pm_node_t *required = requireds_list->nodes[i];
+
+ if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
+ local_index = pm_compile_destructured_param_locals((const pm_multi_target_node_t *) required, &index_lookup_table, local_table_for_iseq, scope_node, local_index);
+ }
}
+ }
-#undef FLUSH_CHUNK
- return;
- }
- case PM_ASSOC_NODE: {
- // { foo: 1 }
- // ^^^^^^
- //
- // foo(bar: 1)
- // ^^^^^^
- const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
+ // 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
+ const pm_node_t *post = posts_list->nodes[i];
- PM_COMPILE(cast->key);
- PM_COMPILE(cast->value);
+ if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
+ local_index = pm_compile_destructured_param_locals((const pm_multi_target_node_t *) post, &index_lookup_table, local_table_for_iseq, scope_node, local_index);
+ }
+ }
+ }
- return;
+ // Set any anonymous locals for the for node
+ if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
+ if (PM_NODE_TYPE_P(((const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
+ body->param.lead_num++;
+ }
+ else {
+ body->param.rest_start = local_index;
+ body->param.flags.has_rest = true;
+ }
+
+ 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 = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
+ RUBY_ASSERT(0 < maximum && maximum <= 9);
+ for (int i = 0; i < maximum; i++, local_index++) {
+ const uint8_t param_name[] = { '_', '1' + i };
+ pm_constant_id_t constant_id = pm_parser_constant_find(scope_node->parser, param_name, 2);
+ RUBY_ASSERT(constant_id && "parser should fill in any gaps in numbered parameters");
+ pm_insert_local_index(constant_id, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
+ }
+ body->param.lead_num = maximum;
+ body->param.flags.has_lead = true;
+ }
+
+ // Fill in the anonymous `it` parameter, if it exists
+ if (scope_node->parameters && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
+ body->param.lead_num = 1;
+ body->param.flags.has_lead = true;
+ }
+
+ //********END OF STEP 3**********
+
+ //********STEP 4**********
+ // Goal: fill in the method body locals
+ // To be explicit, these are the non-parameter locals
+ // 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 = ((const pm_block_local_variable_node_t *) block_locals->nodes[i])->name;
+ pm_insert_local_index(constant_id, local_index, &index_lookup_table, local_table_for_iseq, scope_node);
+ }
+ }
+
+ // Fill in any locals we missed
+ if (scope_node->locals.size) {
+ for (size_t i = 0; i < scope_node->locals.size; i++) {
+ pm_constant_id_t constant_id = locals->ids[i];
+ if (constant_id) {
+ int existing;
+ if (!pm_index_lookup_table_lookup(&index_lookup_table, constant_id, &existing)) {
+ ID local = pm_constant_id_lookup(scope_node, constant_id);
+ local_table_for_iseq->ids[local_index] = local;
+ pm_index_lookup_table_insert(&index_lookup_table, constant_id, local_index);
+ local_index++;
+ }
+ }
+ }
+ }
+
+ //********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);
+
+ if (ISEQ_BODY(iseq)->param.flags.forwardable) {
+ // We're treating `...` as a parameter so that frame
+ // pushing won't clobber it.
+ ISEQ_BODY(iseq)->param.size += 1;
+ }
+
+ // FIXME: args?
+ iseq_set_local_table(iseq, local_table_for_iseq, 0);
+ iseq_set_parameters_lvar_state(iseq);
+
+ scope_node->local_table_for_iseq_size = local_table_for_iseq->size;
+
+ if (keyword != NULL) {
+ size_t keyword_start_index = keyword->bits_start - keyword->num;
+ keyword->table = (ID *)&ISEQ_BODY(iseq)->local_table[keyword_start_index];
+ }
+
+ //********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++) {
+ label = NEW_LABEL(location.line);
+ opt_table[i] = label;
+ PUSH_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(location.line);
+ opt_table[optionals_list->size] = label;
+ PUSH_LABEL(ret, label);
+
+ body->param.opt_table = (const VALUE *) opt_table;
+ }
+
+ if (keywords_list && keywords_list->size) {
+ size_t optional_index = 0;
+ for (size_t i = 0; i < keywords_list->size; i++) {
+ pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
+ pm_constant_id_t name;
+
+ switch (PM_NODE_TYPE(keyword_parameter_node)) {
+ case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^^^
+ const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
+
+ pm_node_t *value = cast->value;
+ name = cast->name;
+
+ if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_CONTAINER_P(value)) {
+ LABEL *end_label = NEW_LABEL(location.line);
+
+ pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
+ int kw_bits_idx = table_size - body->param.keyword->bits_start;
+ PUSH_INSN2(ret, location, checkkeyword, INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1), INT2FIX(optional_index));
+ PUSH_INSNL(ret, location, branchif, end_label);
+ PM_COMPILE(value);
+ PUSH_SETLOCAL(ret, location, index.index, index.level);
+ PUSH_LABEL(ret, end_label);
+ }
+ optional_index++;
+ break;
+ }
+ case PM_REQUIRED_KEYWORD_PARAMETER_NODE:
+ // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
+ // ^^
+ break;
+ default:
+ rb_bug("Unexpected keyword parameter node type %s", pm_node_type(PM_NODE_TYPE(keyword_parameter_node)));
+ }
+ }
+ }
+
+ 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.
+ const pm_node_t *required = requireds_list->nodes[i];
+
+ if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
+ PUSH_GETLOCAL(ret, location, table_size - (int)i, 0);
+ pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) required, ret, scope_node);
+ }
+ }
+ }
+
+ 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.
+ const pm_node_t *post = posts_list->nodes[i];
+
+ if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
+ PUSH_GETLOCAL(ret, location, table_size - body->param.post_start - (int) i, 0);
+ pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) post, ret, scope_node);
+ }
+ }
+ }
+
+ switch (body->type) {
+ case ISEQ_TYPE_PLAIN: {
+ RUBY_ASSERT(PM_NODE_TYPE_P(scope_node->ast_node, PM_INTERPOLATED_REGULAR_EXPRESSION_NODE));
+
+ const pm_interpolated_regular_expression_node_t *cast = (const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
+ pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
+
+ break;
}
- case PM_ASSOC_SPLAT_NODE: {
- // { **foo }
- // ^^^^^
- //
- // def foo(**); bar(**); end
- // ^^
- const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
+ 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);
+ const pm_node_location_t block_location = { .line = body->location.first_lineno, .node_id = scope_node->ast_node->node_id };
- if (cast->value != NULL) {
- PM_COMPILE(cast->value);
+ start->rescued = LABEL_RESCUE_BEG;
+ end->rescued = LABEL_RESCUE_END;
+
+ // For nodes automatically assign the iteration variable to whatever
+ // index variable. We need to handle that write here because it has
+ // to happen in the context of the block. Note that this happens
+ // before the B_CALL tracepoint event.
+ if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
+ pm_compile_for_node_index(iseq, ((const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
}
- else if (!popped) {
- pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
- PUSH_GETLOCAL(ret, location, index.index, index.level);
+
+ PUSH_TRACE(ret, RUBY_EVENT_B_CALL);
+ PUSH_INSN(ret, block_location, nop);
+ PUSH_LABEL(ret, start);
+
+ if (scope_node->body != NULL) {
+ switch (PM_NODE_TYPE(scope_node->ast_node)) {
+ case PM_POST_EXECUTION_NODE: {
+ const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) scope_node->ast_node;
+ PUSH_INSN1(ret, block_location, 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((const pm_node_t *) cast->statements, &next_scope_node, scope_node);
+
+ const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.line);
+ pm_scope_node_destroy(&next_scope_node);
+
+ PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe, INT2FIX(0), block);
+ break;
+ }
+ case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
+ const pm_interpolated_regular_expression_node_t *cast = (const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
+ pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
+ break;
+ }
+ default:
+ pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
+ break;
+ }
+ }
+ else {
+ PUSH_INSN(ret, block_location, putnil);
}
- return;
+ PUSH_LABEL(ret, end);
+ PUSH_TRACE(ret, RUBY_EVENT_B_RETURN);
+ ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
+
+ /* wide range catch handler must put at last */
+ PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
+ PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
+ break;
}
- case PM_BACK_REFERENCE_READ_NODE: {
- // $+
- // ^^
- 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;
- PUSH_INSN2(ret, location, getspecial, INT2FIX(1), backref_val);
+ case ISEQ_TYPE_ENSURE: {
+ const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->body) : location);
+ iseq_set_exception_local_table(iseq);
+
+ if (scope_node->body != NULL) {
+ PM_COMPILE_POPPED((const pm_node_t *) scope_node->body);
}
+
+ PUSH_GETLOCAL(ret, statements_location, 1, 0);
+ PUSH_INSN1(ret, statements_location, throw, INT2FIX(0));
return;
}
- case PM_BEGIN_NODE: {
- // begin end
- // ^^^^^^^^^
- const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
+ case ISEQ_TYPE_METHOD: {
+ ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
+ PUSH_TRACE(ret, RUBY_EVENT_CALL);
- if (cast->ensure_clause) {
- // Compiling the ensure clause will compile the rescue clause (if
- // there is one), which will compile the begin statements.
- pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
+ if (scope_node->body) {
+ PM_COMPILE((const pm_node_t *) scope_node->body);
}
- else if (cast->rescue_clause) {
- // Compiling rescue will compile begin statements (if applicable).
- pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
+ else {
+ PUSH_INSN(ret, location, putnil);
+ }
+
+ ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
+ PUSH_TRACE(ret, RUBY_EVENT_RETURN);
+
+ ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
+ break;
+ }
+ case ISEQ_TYPE_RESCUE: {
+ iseq_set_exception_local_table(iseq);
+ if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
+ LABEL *lab = NEW_LABEL(location.line);
+ LABEL *rescue_end = NEW_LABEL(location.line);
+ PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
+ PUSH_INSN1(ret, location, putobject, rb_eStandardError);
+ PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
+ PUSH_INSNL(ret, location, branchif, lab);
+ PUSH_INSNL(ret, location, jump, rescue_end);
+ PUSH_LABEL(ret, lab);
+ PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
+ PM_COMPILE((const pm_node_t *) scope_node->body);
+ PUSH_INSN(ret, location, leave);
+ PUSH_LABEL(ret, rescue_end);
+ PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
}
else {
- // If there is neither ensure or rescue, the just compile the
- // statements.
- if (cast->statements != NULL) {
- PM_COMPILE((const pm_node_t *) cast->statements);
- }
- else if (!popped) {
- PUSH_SYNTHETIC_PUTNIL(ret, iseq);
- }
+ PM_COMPILE((const pm_node_t *) scope_node->ast_node);
}
+ PUSH_INSN1(ret, location, throw, INT2FIX(0));
+
return;
}
- case PM_BLOCK_ARGUMENT_NODE: {
- // foo(&bar)
- // ^^^^
- const pm_block_argument_node_t *cast = (const pm_block_argument_node_t *) node;
-
- if (cast->expression != NULL) {
- PM_COMPILE(cast->expression);
+ default:
+ if (scope_node->body) {
+ PM_COMPILE((const pm_node_t *) scope_node->body);
}
else {
- // If there's no expression, this must be block forwarding.
- pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
- PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1), INT2FIX(local_index.level));
+ PUSH_INSN(ret, location, putnil);
+ }
+ break;
+ }
+
+ if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
+ const pm_node_location_t end_location = PM_NODE_END_LOCATION(scope_node->ast_node);
+ PUSH_TRACE(ret, RUBY_EVENT_END);
+ ISEQ_COMPILE_DATA(iseq)->last_line = end_location.line;
+ }
+
+ if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
+ const pm_node_location_t location = { .line = ISEQ_COMPILE_DATA(iseq)->last_line, .node_id = scope_node->ast_node->node_id };
+ PUSH_INSN(ret, location, leave);
+ }
+}
+
+static inline void
+pm_compile_alias_global_variable_node(rb_iseq_t *iseq, const pm_alias_global_variable_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ // alias $foo $bar
+ // ^^^^^^^^^^^^^^^
+ PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
+
+ {
+ const pm_location_t *name_loc = &node->new_name->location;
+ VALUE operand = ID2SYM(rb_intern3((const char *) (pm_parser_start(scope_node->parser) + name_loc->start), name_loc->length, scope_node->encoding));
+ PUSH_INSN1(ret, *location, putobject, operand);
+ }
+
+ {
+ const pm_location_t *name_loc = &node->old_name->location;
+ VALUE operand = ID2SYM(rb_intern3((const char *) (pm_parser_start(scope_node->parser) + name_loc->start), name_loc->length, scope_node->encoding));
+ PUSH_INSN1(ret, *location, putobject, operand);
+ }
+
+ PUSH_SEND(ret, *location, id_core_set_variable_alias, INT2FIX(2));
+ if (popped) PUSH_INSN(ret, *location, pop);
+}
+
+static inline void
+pm_compile_alias_method_node(rb_iseq_t *iseq, const pm_alias_method_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
+ PUSH_INSN1(ret, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CBASE));
+ PM_COMPILE_NOT_POPPED(node->new_name);
+ PM_COMPILE_NOT_POPPED(node->old_name);
+
+ PUSH_SEND(ret, *location, id_core_set_method_alias, INT2FIX(3));
+ if (popped) PUSH_INSN(ret, *location, pop);
+}
+
+static inline void
+pm_compile_and_node(rb_iseq_t *iseq, const pm_and_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ LABEL *end_label = NEW_LABEL(location->line);
+
+ PM_COMPILE_NOT_POPPED(node->left);
+ if (!popped) PUSH_INSN(ret, *location, dup);
+ PUSH_INSNL(ret, *location, branchunless, end_label);
+
+ if (!popped) PUSH_INSN(ret, *location, pop);
+ PM_COMPILE(node->right);
+ PUSH_LABEL(ret, end_label);
+}
+
+static inline void
+pm_compile_array_node(rb_iseq_t *iseq, const pm_node_t *node, const pm_node_list_t *elements, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ // If every node in the array is static, then we can compile the entire
+ // array now instead of later.
+ if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
+ // 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) {
+ if (elements->size) {
+ VALUE value = pm_static_literal_value(iseq, node, scope_node);
+ RB_OBJ_SET_FROZEN_SHAREABLE(value);
+ PUSH_INSN1(ret, *location, duparray, value);
+ }
+ else {
+ PUSH_INSN1(ret, *location, newarray, INT2FIX(0));
+ }
}
return;
- }
- case PM_BREAK_NODE: {
- // break
- // ^^^^^
- //
- // break foo
- // ^^^^^^^^^
- const pm_break_node_t *cast = (const pm_break_node_t *) node;
- unsigned long throw_flag = 0;
+ }
+
+ // 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 array is popped, then this serves only to ensure we enact
+ // all side-effects (like method calls) that are contained within
+ // the array contents.
+ //
+ // We treat all sequences of non-splat elements as their
+ // own arrays, followed by a newarray, and then continually
+ // concat the arrays with the SplatNode nodes.
+ const int max_new_array_size = 0x100;
+ const unsigned int min_tmp_array_size = 0x40;
+
+ int new_array_size = 0;
+ bool first_chunk = true;
- if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
- /* while/until */
- LABEL *splabel = NEW_LABEL(0);
- PUSH_LABEL(ret, splabel);
- PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->redo_label);
+ // This is an optimization wherein we keep track of whether or not
+ // the previous element was a static literal. If it was, then we do
+ // not attempt to check if we have a subarray that can be optimized.
+ // If it was not, then we do check.
+ bool static_literal = false;
+
+ // Either create a new array, or push to the existing array.
+#define FLUSH_CHUNK \
+ if (new_array_size) { \
+ if (first_chunk) PUSH_INSN1(ret, *location, newarray, INT2FIX(new_array_size)); \
+ else PUSH_INSN1(ret, *location, pushtoarray, INT2FIX(new_array_size)); \
+ first_chunk = false; \
+ new_array_size = 0; \
+ }
- if (cast->arguments != NULL) {
- PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
+ for (size_t index = 0; index < elements->size; index++) {
+ const pm_node_t *element = elements->nodes[index];
+
+ if (PM_NODE_TYPE_P(element, PM_SPLAT_NODE)) {
+ FLUSH_CHUNK;
+
+ const pm_splat_node_t *splat_element = (const pm_splat_node_t *) element;
+ if (splat_element->expression) {
+ PM_COMPILE_NOT_POPPED(splat_element->expression);
}
else {
- PUSH_INSN(ret, location, putnil);
+ pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_MULT, 0);
+ PUSH_GETLOCAL(ret, *location, index.index, index.level);
}
- pm_add_ensure_iseq(ret, iseq, 0, scope_node);
- PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
- PUSH_ADJUST_RESTORE(ret, splabel);
- if (!popped) PUSH_INSN(ret, location, putnil);
+ if (first_chunk) {
+ // If this is the first element of the array then we
+ // need to splatarray the elements into the list.
+ PUSH_INSN1(ret, *location, splatarray, Qtrue);
+ first_chunk = false;
+ }
+ else {
+ PUSH_INSN(ret, *location, concattoarray);
+ }
+
+ static_literal = false;
}
- else {
- const rb_iseq_t *ip = iseq;
+ else if (PM_NODE_TYPE_P(element, PM_KEYWORD_HASH_NODE)) {
+ if (new_array_size == 0 && first_chunk) {
+ PUSH_INSN1(ret, *location, newarray, INT2FIX(0));
+ first_chunk = false;
+ }
+ else {
+ FLUSH_CHUNK;
+ }
- while (ip) {
- if (!ISEQ_COMPILE_DATA(ip)) {
- ip = 0;
- break;
- }
+ // If we get here, then this is the last element of the
+ // array/arguments, because it cannot be followed by
+ // anything else without a syntax error. This looks like:
+ //
+ // [foo, bar, baz: qux]
+ // ^^^^^^^^
+ //
+ // [foo, bar, **baz]
+ // ^^^^^
+ //
+ const pm_keyword_hash_node_t *keyword_hash = (const pm_keyword_hash_node_t *) element;
+ pm_compile_hash_elements(iseq, element, &keyword_hash->elements, 0, Qundef, false, ret, scope_node);
- 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(iseq, location.line, "Can't escape from eval with break");
- return;
- }
- else {
- ip = ISEQ_BODY(ip)->parent_iseq;
- continue;
- }
+ // This boolean controls the manner in which we push the
+ // hash onto the array. If it's all keyword splats, then we
+ // can use the very specialized pushtoarraykwsplat
+ // instruction to check if it's empty before we push it.
+ size_t splats = 0;
+ while (splats < keyword_hash->elements.size && PM_NODE_TYPE_P(keyword_hash->elements.nodes[splats], PM_ASSOC_SPLAT_NODE)) splats++;
- /* escape from block */
- if (cast->arguments != NULL) {
- PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
+ if (keyword_hash->elements.size == splats) {
+ PUSH_INSN(ret, *location, pushtoarraykwsplat);
+ }
+ else {
+ new_array_size++;
+ }
+ }
+ else if (
+ PM_NODE_FLAG_P(element, PM_NODE_FLAG_STATIC_LITERAL) &&
+ !PM_CONTAINER_P(element) &&
+ !static_literal &&
+ ((index + min_tmp_array_size) < elements->size)
+ ) {
+ // If we have a static literal, then there's the potential
+ // to group a bunch of them together with a literal array
+ // and then concat them together.
+ size_t right_index = index + 1;
+ while (
+ right_index < elements->size &&
+ PM_NODE_FLAG_P(elements->nodes[right_index], PM_NODE_FLAG_STATIC_LITERAL) &&
+ !PM_CONTAINER_P(elements->nodes[right_index])
+ ) right_index++;
+
+ size_t tmp_array_size = right_index - index;
+ if (tmp_array_size >= min_tmp_array_size) {
+ VALUE tmp_array = rb_ary_hidden_new(tmp_array_size);
+
+ // Create the temporary array.
+ for (; tmp_array_size; tmp_array_size--)
+ rb_ary_push(tmp_array, pm_static_literal_value(iseq, elements->nodes[index++], scope_node));
+
+ index--; // about to be incremented by for loop
+ RB_OBJ_SET_FROZEN_SHAREABLE(tmp_array);
+
+ // Emit the optimized code.
+ FLUSH_CHUNK;
+ if (first_chunk) {
+ PUSH_INSN1(ret, *location, duparray, tmp_array);
+ first_chunk = false;
}
else {
- PUSH_INSN(ret, location, putnil);
+ PUSH_INSN1(ret, *location, putobject, tmp_array);
+ PUSH_INSN(ret, *location, concattoarray);
}
+ }
+ else {
+ PM_COMPILE_NOT_POPPED(element);
+ if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
+ static_literal = true;
+ }
+ } else {
+ PM_COMPILE_NOT_POPPED(element);
+ if (++new_array_size >= max_new_array_size) FLUSH_CHUNK;
+ static_literal = false;
+ }
+ }
+
+ FLUSH_CHUNK;
+ if (popped) PUSH_INSN(ret, *location, pop);
+
+#undef FLUSH_CHUNK
+}
+
+static inline void
+pm_compile_break_node(rb_iseq_t *iseq, const pm_break_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ 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);
+ PUSH_LABEL(ret, splabel);
+ PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
+
+ if (node->arguments != NULL) {
+ PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
+ }
+ else {
+ PUSH_INSN(ret, *location, putnil);
+ }
+
+ pm_add_ensure_iseq(ret, iseq, 0, scope_node);
+ PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
+ PUSH_ADJUST_RESTORE(ret, splabel);
+ if (!popped) PUSH_INSN(ret, *location, putnil);
+ }
+ else {
+ const rb_iseq_t *ip = iseq;
- PUSH_INSN1(ret, location, throw, INT2FIX(throw_flag | TAG_BREAK));
- if (popped) PUSH_INSN(ret, location, pop);
+ while (ip) {
+ if (!ISEQ_COMPILE_DATA(ip)) {
+ ip = 0;
+ break;
+ }
+ 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(iseq, location->line, "Invalid break");
return;
}
+ else {
+ ip = ISEQ_BODY(ip)->parent_iseq;
+ continue;
+ }
- COMPILE_ERROR(iseq, location.line, "Invalid break");
+ /* escape from block */
+ if (node->arguments != NULL) {
+ PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
+ }
+ else {
+ PUSH_INSN(ret, *location, putnil);
+ }
+
+ PUSH_INSN1(ret, *location, throw, INT2FIX(throw_flag | TAG_BREAK));
+ if (popped) PUSH_INSN(ret, *location, pop);
+
+ return;
}
- return;
- }
- case PM_CALL_NODE: {
- // foo
- // ^^^
- //
- // foo.bar
- // ^^^^^^^
- //
- // foo.bar() {}
- // ^^^^^^^^^^^^
- const pm_call_node_t *cast = (const pm_call_node_t *) node;
- ID method_id = pm_constant_id_lookup(scope_node, cast->name);
- const pm_location_t *message_loc = &cast->message_loc;
- if (message_loc->start == NULL) message_loc = &cast->base.location;
+ COMPILE_ERROR(iseq, location->line, "Invalid break");
+ }
+}
+
+static inline void
+pm_compile_call_node(rb_iseq_t *iseq, const pm_call_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ ID method_id = pm_constant_id_lookup(scope_node, node->name);
+
+ const pm_location_t *message_loc = &node->message_loc;
+ if (message_loc->length == 0) message_loc = &node->base.location;
- const pm_node_location_t location = PM_LOCATION_START_LOCATION(scope_node->parser, message_loc, cast->base.node_id);
- const char *builtin_func;
+ const pm_node_location_t location = PM_LOCATION_START_LOCATION(message_loc, node->base.node_id);
+ const char *builtin_func;
- if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, cast->receiver, method_id)) != NULL) {
- pm_compile_builtin_function_call(iseq, ret, scope_node, cast, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
+ if (UNLIKELY(iseq_has_builtin_function_table(iseq)) && (builtin_func = pm_iseq_builtin_function_name(scope_node, node->receiver, method_id)) != NULL) {
+ pm_compile_builtin_function_call(iseq, ret, scope_node, node, &location, popped, ISEQ_COMPILE_DATA(iseq)->current_block, builtin_func);
+ return;
+ }
+
+ LABEL *start = NEW_LABEL(location.line);
+ if (node->block) PUSH_LABEL(ret, start);
+
+ switch (method_id) {
+ case idUMinus: {
+ if (pm_opt_str_freeze_p(iseq, node)) {
+ VALUE value = parse_static_literal_string(iseq, scope_node, node->receiver, &((const pm_string_node_t * ) node->receiver)->unescaped);
+ const struct rb_callinfo *callinfo = new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE);
+ PUSH_INSN2(ret, location, opt_str_uminus, value, callinfo);
+ if (popped) PUSH_INSN(ret, location, pop);
+ return;
+ }
+ break;
+ }
+ case idFreeze: {
+ if (pm_opt_str_freeze_p(iseq, node)) {
+ VALUE value = parse_static_literal_string(iseq, scope_node, node->receiver, &((const pm_string_node_t * ) node->receiver)->unescaped);
+ const struct rb_callinfo *callinfo = new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE);
+ PUSH_INSN2(ret, location, opt_str_freeze, value, callinfo);
+ if (popped) PUSH_INSN(ret, location, pop);
return;
}
+ break;
+ }
+ }
- LABEL *start = NEW_LABEL(location.line);
- if (cast->block) PUSH_LABEL(ret, start);
+ if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
+ PUSH_INSN(ret, location, putnil);
+ }
- switch (method_id) {
- case idUMinus: {
- if (pm_opt_str_freeze_p(iseq, cast)) {
- VALUE value = parse_static_literal_string(iseq, scope_node, cast->receiver, &((const pm_string_node_t * ) cast->receiver)->unescaped);
- PUSH_INSN2(ret, location, opt_str_uminus, value, new_callinfo(iseq, idUMinus, 0, 0, NULL, FALSE));
- return;
+ if (node->receiver == NULL) {
+ PUSH_INSN(ret, location, putself);
+ }
+ else {
+ if (method_id == idCall && PM_NODE_TYPE_P(node->receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
+ const pm_local_variable_read_node_t *read_node_cast = (const pm_local_variable_read_node_t *) node->receiver;
+ uint32_t node_id = node->receiver->node_id;
+ int idx, level;
+
+ if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
+ ADD_ELEM(ret, (LINK_ELEMENT *) new_insn_body(iseq, location.line, node_id, BIN(getblockparamproxy), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
}
- break;
- }
- case idFreeze: {
- if (pm_opt_str_freeze_p(iseq, cast)) {
- VALUE value = parse_static_literal_string(iseq, scope_node, cast->receiver, &((const pm_string_node_t * ) cast->receiver)->unescaped);
- PUSH_INSN2(ret, location, opt_str_freeze, value, new_callinfo(iseq, idFreeze, 0, 0, NULL, FALSE));
- return;
+ else {
+ PM_COMPILE_NOT_POPPED(node->receiver);
}
- break;
- }
- case idAREF: {
- if (pm_opt_aref_with_p(iseq, cast)) {
- const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) cast->arguments)->arguments.nodes[0];
- VALUE value = parse_static_literal_string(iseq, scope_node, (const pm_node_t *) string, &string->unescaped);
+ }
+ else {
+ PM_COMPILE_NOT_POPPED(node->receiver);
+ }
+ }
- PM_COMPILE_NOT_POPPED(cast->receiver);
- PUSH_INSN2(ret, location, opt_aref_with, value, new_callinfo(iseq, idAREF, 1, 0, NULL, FALSE));
+ pm_compile_call(iseq, node, ret, popped, scope_node, method_id, start);
+ return;
+}
- if (popped) {
- PUSH_INSN(ret, location, pop);
- }
+static inline void
+pm_compile_call_operator_write_node(rb_iseq_t *iseq, const pm_call_operator_write_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ int flag = 0;
- return;
- }
- break;
- }
- case idASET: {
- if (pm_opt_aset_with_p(iseq, cast)) {
- const pm_string_node_t *string = (const pm_string_node_t *) ((const pm_arguments_node_t *) cast->arguments)->arguments.nodes[0];
- VALUE value = parse_static_literal_string(iseq, scope_node, (const pm_node_t *) string, &string->unescaped);
+ if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
+ flag = VM_CALL_FCALL;
+ }
- PM_COMPILE_NOT_POPPED(cast->receiver);
- PM_COMPILE_NOT_POPPED(((const pm_arguments_node_t *) cast->arguments)->arguments.nodes[1]);
+ PM_COMPILE_NOT_POPPED(node->receiver);
- if (!popped) {
- PUSH_INSN(ret, location, swap);
- PUSH_INSN1(ret, location, topn, INT2FIX(1));
- }
+ LABEL *safe_label = NULL;
+ if (PM_NODE_FLAG_P(node, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
+ safe_label = NEW_LABEL(location->line);
+ PUSH_INSN(ret, *location, dup);
+ PUSH_INSNL(ret, *location, branchnil, safe_label);
+ }
- PUSH_INSN2(ret, location, opt_aset_with, value, new_callinfo(iseq, idASET, 2, 0, NULL, FALSE));
- PUSH_INSN(ret, location, pop);
- return;
- }
- break;
- }
- }
+ PUSH_INSN(ret, *location, dup);
- if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_ATTRIBUTE_WRITE) && !popped) {
- PUSH_INSN(ret, location, putnil);
- }
+ ID id_read_name = pm_constant_id_lookup(scope_node, node->read_name);
+ PUSH_SEND_WITH_FLAG(ret, *location, id_read_name, INT2FIX(0), INT2FIX(flag));
- if (cast->receiver == NULL) {
- PUSH_INSN(ret, location, putself);
- }
- else {
- if (method_id == idCall && PM_NODE_TYPE_P(cast->receiver, PM_LOCAL_VARIABLE_READ_NODE)) {
- const pm_local_variable_read_node_t *read_node_cast = (const pm_local_variable_read_node_t *) cast->receiver;
- uint32_t node_id = cast->receiver->node_id;
- int idx, level;
+ PM_COMPILE_NOT_POPPED(node->value);
+ ID id_operator = pm_constant_id_lookup(scope_node, node->binary_operator);
+ PUSH_SEND(ret, *location, id_operator, INT2FIX(1));
- if (iseq_block_param_id_p(iseq, pm_constant_id_lookup(scope_node, read_node_cast->name), &idx, &level)) {
- ADD_ELEM(ret, (LINK_ELEMENT *) new_insn_body(iseq, location.line, node_id, BIN(getblockparamproxy), 2, INT2FIX((idx) + VM_ENV_DATA_SIZE - 1), INT2FIX(level)));
- }
- else {
- PM_COMPILE_NOT_POPPED(cast->receiver);
- }
- }
- else {
- PM_COMPILE_NOT_POPPED(cast->receiver);
- }
+ if (!popped) {
+ PUSH_INSN(ret, *location, swap);
+ PUSH_INSN1(ret, *location, topn, INT2FIX(1));
+ }
+
+ ID id_write_name = pm_constant_id_lookup(scope_node, node->write_name);
+ PUSH_SEND_WITH_FLAG(ret, *location, id_write_name, INT2FIX(1), INT2FIX(flag));
+
+ if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
+ PUSH_INSN(ret, *location, pop);
+ if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
+}
+
+/**
+ * When we're compiling a case node, it's possible that we can speed it up using
+ * a dispatch hash, which will allow us to jump directly to the correct when
+ * clause body based on a hash lookup of the value. This can only happen when
+ * the conditions are literals that can be compiled into a hash key.
+ *
+ * This function accepts a dispatch hash and the condition of a when clause. It
+ * is responsible for compiling the condition into a hash key and then adding it
+ * to the dispatch hash.
+ *
+ * If the value can be successfully compiled into the hash, then this function
+ * returns the dispatch hash with the new key added. If the value cannot be
+ * compiled into the hash, then this function returns Qundef. In the case of
+ * Qundef, this function is signaling that the caller should abandon the
+ * optimization entirely.
+ */
+static VALUE
+pm_compile_case_node_dispatch(rb_iseq_t *iseq, VALUE dispatch, const pm_node_t *node, LABEL *label, pm_scope_node_t *scope_node)
+{
+ VALUE key = Qundef;
+ switch (PM_NODE_TYPE(node)) {
+ case PM_FLOAT_NODE: {
+ key = pm_static_literal_value(iseq, node, scope_node);
+ double intptr;
+
+ if (modf(RFLOAT_VALUE(key), &intptr) == 0.0) {
+ key = (FIXABLE(intptr) ? LONG2FIX((long) intptr) : rb_dbl2big(intptr));
}
- pm_compile_call(iseq, cast, ret, popped, scope_node, method_id, start);
- return;
- }
- case PM_CALL_AND_WRITE_NODE: {
- // foo.bar &&= baz
- // ^^^^^^^^^^^^^^^
- const pm_call_and_write_node_t *cast = (const pm_call_and_write_node_t *) node;
- pm_compile_call_and_or_write_node(iseq, true, cast->receiver, cast->value, cast->write_name, cast->read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
- return;
+ break;
}
- case PM_CALL_OR_WRITE_NODE: {
- // foo.bar ||= baz
- // ^^^^^^^^^^^^^^^
- const pm_call_or_write_node_t *cast = (const pm_call_or_write_node_t *) node;
- pm_compile_call_and_or_write_node(iseq, false, cast->receiver, cast->value, cast->write_name, cast->read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
- return;
+ case PM_FALSE_NODE:
+ case PM_INTEGER_NODE:
+ case PM_NIL_NODE:
+ case PM_SOURCE_FILE_NODE:
+ case PM_SOURCE_LINE_NODE:
+ case PM_SYMBOL_NODE:
+ case PM_TRUE_NODE:
+ key = pm_static_literal_value(iseq, node, scope_node);
+ break;
+ case PM_STRING_NODE: {
+ const pm_string_node_t *cast = (const pm_string_node_t *) node;
+ key = parse_static_literal_string(iseq, scope_node, node, &cast->unescaped);
+ break;
}
- case PM_CALL_OPERATOR_WRITE_NODE: {
- // foo.bar += baz
- // ^^^^^^^^^^^^^^^
- //
- // Call operator writes occur when you have a call node on the left-hand
- // side of a write operator that is not `=`. As an example,
- // `foo.bar *= 1`. This breaks down to caching the receiver on the
- // stack and then performing three method calls, one to read the value,
- // one to compute the result, and one to write the result back to the
- // receiver.
- const pm_call_operator_write_node_t *cast = (const pm_call_operator_write_node_t *) node;
- int flag = 0;
+ default:
+ return Qundef;
+ }
- if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_IGNORE_VISIBILITY)) {
- flag = VM_CALL_FCALL;
- }
+ cdhash_aset_if_missing(dispatch, key, (VALUE)label);
+ return dispatch;
+}
- PM_COMPILE_NOT_POPPED(cast->receiver);
+/**
+ * Compile a case node, representing a case statement with when clauses.
+ */
+static inline void
+pm_compile_case_node(rb_iseq_t *iseq, const pm_case_node_t *cast, const pm_node_location_t *node_location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ const pm_node_location_t location = *node_location;
+ const pm_node_list_t *conditions = &cast->conditions;
- LABEL *safe_label = NULL;
- if (PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION)) {
- safe_label = NEW_LABEL(location.line);
- PUSH_INSN(ret, location, dup);
- PUSH_INSNL(ret, location, branchnil, safe_label);
- }
+ // This is the anchor that we will compile the conditions of the various
+ // `when` nodes into. If a match is found, they will need to jump into
+ // the body_seq anchor to the correct spot.
+ DECL_ANCHOR(cond_seq);
- PUSH_INSN(ret, location, dup);
+ // This is the anchor that we will compile the bodies of the various
+ // `when` nodes into. We'll make sure that the clauses that are compiled
+ // jump into the correct spots within this anchor.
+ DECL_ANCHOR(body_seq);
- ID id_read_name = pm_constant_id_lookup(scope_node, cast->read_name);
- PUSH_SEND_WITH_FLAG(ret, location, id_read_name, INT2FIX(0), INT2FIX(flag));
+ // This is the label where all of the when clauses will jump to if they
+ // have matched and are done executing their bodies.
+ LABEL *end_label = NEW_LABEL(location.line);
- PM_COMPILE_NOT_POPPED(cast->value);
- ID id_operator = pm_constant_id_lookup(scope_node, cast->binary_operator);
- PUSH_SEND(ret, location, id_operator, INT2FIX(1));
+ // If we have a predicate on this case statement, then it's going to
+ // compare all of the various when clauses to the predicate. If we
+ // don't, then it's basically an if-elsif-else chain.
+ if (cast->predicate == NULL) {
+ // Establish branch coverage for the case node.
+ VALUE branches = Qfalse;
+ rb_code_location_t case_location = { 0 };
+ int branch_id = 0;
- if (!popped) {
- PUSH_INSN(ret, location, swap);
- PUSH_INSN1(ret, location, topn, INT2FIX(1));
+ if (PM_BRANCH_COVERAGE_P(iseq)) {
+ case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
+ branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
}
- ID id_write_name = pm_constant_id_lookup(scope_node, cast->write_name);
- PUSH_SEND_WITH_FLAG(ret, location, id_write_name, INT2FIX(1), INT2FIX(flag));
+ // Loop through each clauses in the case node and compile each of
+ // the conditions within them into cond_seq. If they match, they
+ // should jump into their respective bodies in body_seq.
+ for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
+ const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
+ const pm_node_list_t *conditions = &clause->conditions;
- if (safe_label != NULL && popped) PUSH_LABEL(ret, safe_label);
- PUSH_INSN(ret, location, pop);
- if (safe_label != NULL && !popped) PUSH_LABEL(ret, safe_label);
+ int clause_lineno = pm_node_line_number_cached((const pm_node_t *) clause, scope_node);
+ LABEL *label = NEW_LABEL(clause_lineno);
+ PUSH_LABEL(body_seq, label);
- return;
- }
- case PM_CASE_NODE: {
- // case foo; when bar; end
- // ^^^^^^^^^^^^^^^^^^^^^^^
- const pm_case_node_t *cast = (const pm_case_node_t *) node;
- const pm_node_list_t *conditions = &cast->conditions;
+ // Establish branch coverage for the when clause.
+ if (PM_BRANCH_COVERAGE_P(iseq)) {
+ rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((const pm_node_t *) clause->statements) : ((const pm_node_t *) clause));
+ add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
+ }
- // This is the anchor that we will compile the conditions of the various
- // `when` nodes into. If a match is found, they will need to jump into
- // the body_seq anchor to the correct spot.
- DECL_ANCHOR(cond_seq);
- INIT_ANCHOR(cond_seq);
+ if (clause->statements != NULL) {
+ pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
+ }
+ else if (!popped) {
+ PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
+ }
- // This is the anchor that we will compile the bodies of the various
- // `when` nodes into. We'll make sure that the clauses that are compiled
- // jump into the correct spots within this anchor.
- DECL_ANCHOR(body_seq);
- INIT_ANCHOR(body_seq);
+ PUSH_INSNL(body_seq, location, jump, end_label);
- // This is the label where all of the when clauses will jump to if they
- // have matched and are done executing their bodies.
- LABEL *end_label = NEW_LABEL(location.line);
+ // Compile each of the conditions for the when clause into the
+ // cond_seq. Each one should have a unique condition and should
+ // jump to the subsequent one if it doesn't match.
+ for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
+ const pm_node_t *condition = conditions->nodes[condition_index];
- // If we have a predicate on this case statement, then it's going to
- // compare all of the various when clauses to the predicate. If we
- // don't, then it's basically an if-elsif-else chain.
- if (cast->predicate == NULL) {
- // Establish branch coverage for the case node.
- VALUE branches = Qfalse;
- rb_code_location_t case_location = { 0 };
- int branch_id = 0;
+ if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
+ pm_node_location_t cond_location = PM_NODE_START_LOCATION(condition);
+ PUSH_INSN(cond_seq, cond_location, putnil);
+ pm_compile_node(iseq, condition, cond_seq, false, scope_node);
+ PUSH_INSN1(cond_seq, cond_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
+ PUSH_INSNL(cond_seq, cond_location, branchif, label);
+ }
+ else {
+ LABEL *next_label = NEW_LABEL(pm_node_line_number_cached(condition, scope_node));
+ pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label, scope_node);
+ PUSH_LABEL(cond_seq, next_label);
+ }
+ }
+ }
- if (PM_BRANCH_COVERAGE_P(iseq)) {
- case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
- branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
+ // Establish branch coverage for the else clause (implicit or
+ // explicit).
+ if (PM_BRANCH_COVERAGE_P(iseq)) {
+ rb_code_location_t branch_location;
+
+ if (cast->else_clause == NULL) {
+ branch_location = case_location;
+ } else if (cast->else_clause->statements == NULL) {
+ branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->else_clause);
+ } else {
+ branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->else_clause->statements);
}
- // Loop through each clauses in the case node and compile each of
- // the conditions within them into cond_seq. If they match, they
- // should jump into their respective bodies in body_seq.
- for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
- const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
- const pm_node_list_t *conditions = &clause->conditions;
+ add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
+ }
- int clause_lineno = pm_node_line_number(parser, (const pm_node_t *) clause);
- LABEL *label = NEW_LABEL(clause_lineno);
- PUSH_LABEL(body_seq, label);
+ // Compile the else clause if there is one.
+ if (cast->else_clause != NULL) {
+ pm_compile_node(iseq, (const pm_node_t *) cast->else_clause, cond_seq, popped, scope_node);
+ }
+ else if (!popped) {
+ PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
+ }
- // Establish branch coverage for the when clause.
- if (PM_BRANCH_COVERAGE_P(iseq)) {
- rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((const pm_node_t *) clause->statements) : ((const pm_node_t *) clause));
- add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
- }
+ // Finally, jump to the end label if none of the other conditions
+ // have matched.
+ PUSH_INSNL(cond_seq, location, jump, end_label);
+ PUSH_SEQ(ret, cond_seq);
+ }
+ else {
+ // Establish branch coverage for the case node.
+ VALUE branches = Qfalse;
+ rb_code_location_t case_location = { 0 };
+ int branch_id = 0;
- if (clause->statements != NULL) {
- pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
- }
- else if (!popped) {
- PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
- }
+ if (PM_BRANCH_COVERAGE_P(iseq)) {
+ case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
+ branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
+ }
- PUSH_INSNL(body_seq, location, jump, end_label);
+ // This is the label where everything will fall into if none of the
+ // conditions matched.
+ LABEL *else_label = NEW_LABEL(location.line);
- // Compile each of the conditions for the when clause into the
- // cond_seq. Each one should have a unique condition and should
- // jump to the subsequent one if it doesn't match.
- for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
- const pm_node_t *condition = conditions->nodes[condition_index];
+ // It's possible for us to speed up the case node by using a
+ // dispatch hash. This is a hash that maps the conditions of the
+ // various when clauses to the labels of their bodies. If we can
+ // compile the conditions into a hash key, then we can use a hash
+ // lookup to jump directly to the correct when clause body.
+ VALUE dispatch = Qundef;
+ if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
+ dispatch = cdhash_new(0);
+ }
- if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
- pm_node_location_t cond_location = PM_NODE_START_LOCATION(parser, condition);
- PUSH_INSN(cond_seq, cond_location, putnil);
- pm_compile_node(iseq, condition, cond_seq, false, scope_node);
- PUSH_INSN1(cond_seq, cond_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_WHEN | VM_CHECKMATCH_ARRAY));
- PUSH_INSNL(cond_seq, cond_location, branchif, label);
+ // We're going to loop through each of the conditions in the case
+ // node and compile each of their contents into both the cond_seq
+ // and the body_seq. Each condition will use its own label to jump
+ // from its conditions into its body.
+ //
+ // Note that none of the code in the loop below should be adding
+ // anything to ret, as we're going to be laying out the entire case
+ // node instructions later.
+ for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
+ const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
+ pm_node_location_t clause_location = PM_NODE_START_LOCATION((const pm_node_t *) clause);
+
+ const pm_node_list_t *conditions = &clause->conditions;
+ LABEL *label = NEW_LABEL(clause_location.line);
+
+ // Compile each of the conditions for the when clause into the
+ // cond_seq. Each one should have a unique comparison that then
+ // jumps into the body if it matches.
+ for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
+ const pm_node_t *condition = conditions->nodes[condition_index];
+ const pm_node_location_t condition_location = PM_NODE_START_LOCATION(condition);
+
+ // If we haven't already abandoned the optimization, then
+ // we're going to try to compile the condition into the
+ // dispatch hash.
+ if (dispatch != Qundef) {
+ dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
+ }
+
+ if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
+ PUSH_INSN(cond_seq, condition_location, dup);
+ pm_compile_node(iseq, condition, cond_seq, false, scope_node);
+ PUSH_INSN1(cond_seq, condition_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
+ }
+ else {
+ if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
+ const pm_string_node_t *string = (const pm_string_node_t *) condition;
+ VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->unescaped);
+ PUSH_INSN1(cond_seq, condition_location, putobject, value);
}
else {
- LABEL *next_label = NEW_LABEL(pm_node_line_number(parser, condition));
- pm_compile_branch_condition(iseq, cond_seq, condition, label, next_label, false, scope_node);
- PUSH_LABEL(cond_seq, next_label);
+ pm_compile_node(iseq, condition, cond_seq, false, scope_node);
}
+
+ PUSH_INSN1(cond_seq, condition_location, topn, INT2FIX(1));
+ PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
}
- }
- // Establish branch coverage for the else clause (implicit or
- // explicit).
- if (PM_BRANCH_COVERAGE_P(iseq)) {
- rb_code_location_t branch_location;
+ PUSH_INSNL(cond_seq, condition_location, branchif, label);
+ }
- if (cast->consequent == NULL) {
- branch_location = case_location;
- } else if (cast->consequent->statements == NULL) {
- branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->consequent);
- } else {
- branch_location = pm_code_location(scope_node, (const pm_node_t *) cast->consequent->statements);
- }
+ // Now, add the label to the body and compile the body of the
+ // when clause. This involves popping the predicate, compiling
+ // the statements to be executed, and then compiling a jump to
+ // the end of the case node.
+ PUSH_LABEL(body_seq, label);
+ PUSH_INSN(body_seq, clause_location, pop);
- add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
+ // Establish branch coverage for the when clause.
+ if (PM_BRANCH_COVERAGE_P(iseq)) {
+ rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((const pm_node_t *) clause->statements) : ((const pm_node_t *) clause));
+ add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
}
- // Compile the consequent else clause if there is one.
- if (cast->consequent != NULL) {
- pm_compile_node(iseq, (const pm_node_t *) cast->consequent, cond_seq, popped, scope_node);
+ if (clause->statements != NULL) {
+ pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
}
else if (!popped) {
- PUSH_SYNTHETIC_PUTNIL(cond_seq, iseq);
+ PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
}
- // Finally, jump to the end label if none of the other conditions
- // have matched.
- PUSH_INSNL(cond_seq, location, jump, end_label);
- PUSH_SEQ(ret, cond_seq);
+ PUSH_INSNL(body_seq, clause_location, jump, end_label);
}
- else {
- // Establish branch coverage for the case node.
- VALUE branches = Qfalse;
- rb_code_location_t case_location = { 0 };
- int branch_id = 0;
+ // Now that we have compiled the conditions and the bodies of the
+ // various when clauses, we can compile the predicate, lay out the
+ // conditions, compile the fallback subsequent if there is one, and
+ // finally put in the bodies of the when clauses.
+ PM_COMPILE_NOT_POPPED(cast->predicate);
+
+ // If we have a dispatch hash, then we'll use it here to create the
+ // optimization.
+ if (dispatch != Qundef) {
+ PUSH_INSN(ret, location, dup);
+ RB_OBJ_SET_SHAREABLE(dispatch); // it is special that the hash is shareable but not frozen, because compile.c modify them. This Hahs instance is not accessible so it is safe to leave it.
+ PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
+ LABEL_REF(else_label);
+ }
+
+ PUSH_SEQ(ret, cond_seq);
+
+ // Compile either the explicit else clause or an implicit else
+ // clause.
+ PUSH_LABEL(ret, else_label);
+
+ if (cast->else_clause != NULL) {
+ pm_node_location_t else_location = PM_NODE_START_LOCATION(cast->else_clause->statements != NULL ? ((const pm_node_t *) cast->else_clause->statements) : ((const pm_node_t *) cast->else_clause));
+ PUSH_INSN(ret, else_location, pop);
+
+ // Establish branch coverage for the else clause.
if (PM_BRANCH_COVERAGE_P(iseq)) {
- case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
- branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
+ rb_code_location_t branch_location = pm_code_location(scope_node, cast->else_clause->statements != NULL ? ((const pm_node_t *) cast->else_clause->statements) : ((const pm_node_t *) cast->else_clause));
+ add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
}
- // This is the label where everything will fall into if none of the
- // conditions matched.
- LABEL *else_label = NEW_LABEL(location.line);
+ PM_COMPILE((const pm_node_t *) cast->else_clause);
+ PUSH_INSNL(ret, else_location, jump, end_label);
+ }
+ else {
+ PUSH_INSN(ret, location, pop);
- // It's possible for us to speed up the case node by using a
- // dispatch hash. This is a hash that maps the conditions of the
- // various when clauses to the labels of their bodies. If we can
- // compile the conditions into a hash key, then we can use a hash
- // lookup to jump directly to the correct when clause body.
- VALUE dispatch = Qundef;
- if (ISEQ_COMPILE_DATA(iseq)->option->specialized_instruction) {
- dispatch = rb_hash_new();
- RHASH_TBL_RAW(dispatch)->type = &cdhash_type;
+ // Establish branch coverage for the implicit else clause.
+ if (PM_BRANCH_COVERAGE_P(iseq)) {
+ add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
}
- // We're going to loop through each of the conditions in the case
- // node and compile each of their contents into both the cond_seq
- // and the body_seq. Each condition will use its own label to jump
- // from its conditions into its body.
- //
- // Note that none of the code in the loop below should be adding
- // anything to ret, as we're going to be laying out the entire case
- // node instructions later.
- for (size_t clause_index = 0; clause_index < conditions->size; clause_index++) {
- const pm_when_node_t *clause = (const pm_when_node_t *) conditions->nodes[clause_index];
- pm_node_location_t clause_location = PM_NODE_START_LOCATION(parser, (const pm_node_t *) clause);
-
- const pm_node_list_t *conditions = &clause->conditions;
- LABEL *label = NEW_LABEL(clause_location.line);
-
- // Compile each of the conditions for the when clause into the
- // cond_seq. Each one should have a unique comparison that then
- // jumps into the body if it matches.
- for (size_t condition_index = 0; condition_index < conditions->size; condition_index++) {
- const pm_node_t *condition = conditions->nodes[condition_index];
- const pm_node_location_t condition_location = PM_NODE_START_LOCATION(parser, condition);
-
- // If we haven't already abandoned the optimization, then
- // we're going to try to compile the condition into the
- // dispatch hash.
- if (dispatch != Qundef) {
- dispatch = pm_compile_case_node_dispatch(iseq, dispatch, condition, label, scope_node);
- }
+ if (!popped) PUSH_INSN(ret, location, putnil);
+ PUSH_INSNL(ret, location, jump, end_label);
+ }
+ }
- if (PM_NODE_TYPE_P(condition, PM_SPLAT_NODE)) {
- PUSH_INSN(cond_seq, condition_location, dup);
- pm_compile_node(iseq, condition, cond_seq, false, scope_node);
- PUSH_INSN1(cond_seq, condition_location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_CASE | VM_CHECKMATCH_ARRAY));
- }
- else {
- if (PM_NODE_TYPE_P(condition, PM_STRING_NODE)) {
- const pm_string_node_t *string = (const pm_string_node_t *) condition;
- VALUE value = parse_static_literal_string(iseq, scope_node, condition, &string->unescaped);
- PUSH_INSN1(cond_seq, condition_location, putobject, value);
- }
- else {
- pm_compile_node(iseq, condition, cond_seq, false, scope_node);
- }
+ PUSH_SEQ(ret, body_seq);
+ PUSH_LABEL(ret, end_label);
+}
- PUSH_INSN1(cond_seq, condition_location, topn, INT2FIX(1));
- PUSH_SEND_WITH_FLAG(cond_seq, condition_location, idEqq, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
- }
+static inline void
+pm_compile_case_match_node(rb_iseq_t *iseq, const pm_case_match_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ // This is the anchor that we will compile the bodies of the various
+ // `in` nodes into. We'll make sure that the patterns that are compiled
+ // jump into the correct spots within this anchor.
+ DECL_ANCHOR(body_seq);
+
+ // This is the anchor that we will compile the patterns of the various
+ // `in` nodes into. If a match is found, they will need to jump into the
+ // body_seq anchor to the correct spot.
+ DECL_ANCHOR(cond_seq);
- PUSH_INSNL(cond_seq, condition_location, branchif, label);
- }
+ // This label is used to indicate the end of the entire node. It is
+ // jumped to after the entire stack is cleaned up.
+ LABEL *end_label = NEW_LABEL(location->line);
- // Now, add the label to the body and compile the body of the
- // when clause. This involves popping the predicate, compiling
- // the statements to be executed, and then compiling a jump to
- // the end of the case node.
- PUSH_LABEL(body_seq, label);
- PUSH_INSN(body_seq, clause_location, pop);
-
- // Establish branch coverage for the when clause.
- if (PM_BRANCH_COVERAGE_P(iseq)) {
- rb_code_location_t branch_location = pm_code_location(scope_node, clause->statements != NULL ? ((const pm_node_t *) clause->statements) : ((const pm_node_t *) clause));
- add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "when", branches);
- }
+ // This label is used as the fallback for the case match. If no match is
+ // found, then we jump to this label. This is either an `else` clause or
+ // an error handler.
+ LABEL *else_label = NEW_LABEL(location->line);
- if (clause->statements != NULL) {
- pm_compile_node(iseq, (const pm_node_t *) clause->statements, body_seq, popped, scope_node);
- }
- else if (!popped) {
- PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
- }
+ // We're going to use this to uniquely identify each branch so that we
+ // can track coverage information.
+ rb_code_location_t case_location = { 0 };
+ VALUE branches = Qfalse;
+ int branch_id = 0;
- PUSH_INSNL(body_seq, clause_location, jump, end_label);
- }
+ if (PM_BRANCH_COVERAGE_P(iseq)) {
+ case_location = pm_code_location(scope_node, (const pm_node_t *) node);
+ branches = decl_branch_base(iseq, PTR2NUM(node), &case_location, "case");
+ }
- // Now that we have compiled the conditions and the bodies of the
- // various when clauses, we can compile the predicate, lay out the
- // conditions, compile the fallback consequent if there is one, and
- // finally put in the bodies of the when clauses.
- PM_COMPILE_NOT_POPPED(cast->predicate);
+ // If there is only one pattern, then the behavior changes a bit. It
+ // effectively gets treated as a match required node (this is how it is
+ // represented in the other parser).
+ bool in_single_pattern = node->else_clause == NULL && node->conditions.size == 1;
- // If we have a dispatch hash, then we'll use it here to create the
- // optimization.
- if (dispatch != Qundef) {
- PUSH_INSN(ret, location, dup);
- PUSH_INSN2(ret, location, opt_case_dispatch, dispatch, else_label);
- LABEL_REF(else_label);
- }
+ // First, we're going to push a bunch of stuff onto the stack that is
+ // going to serve as our scratch space.
+ if (in_single_pattern) {
+ PUSH_INSN(ret, *location, putnil); // key error key
+ PUSH_INSN(ret, *location, putnil); // key error matchee
+ PUSH_INSN1(ret, *location, putobject, Qfalse); // key error?
+ PUSH_INSN(ret, *location, putnil); // error string
+ }
- PUSH_SEQ(ret, cond_seq);
+ // Now we're going to compile the value to match against.
+ PUSH_INSN(ret, *location, putnil); // deconstruct cache
+ PM_COMPILE_NOT_POPPED(node->predicate);
- // Compile either the explicit else clause or an implicit else
- // clause.
- PUSH_LABEL(ret, else_label);
+ // Next, we'll loop through every in clause and compile its body into
+ // the body_seq anchor and its pattern into the cond_seq anchor. We'll
+ // make sure the pattern knows how to jump correctly into the body if it
+ // finds a match.
+ for (size_t index = 0; index < node->conditions.size; index++) {
+ const pm_node_t *condition = node->conditions.nodes[index];
+ RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
- if (cast->consequent != NULL) {
- pm_node_location_t else_location = PM_NODE_START_LOCATION(parser, cast->consequent->statements != NULL ? ((const pm_node_t *) cast->consequent->statements) : ((const pm_node_t *) cast->consequent));
- PUSH_INSN(ret, else_location, pop);
+ const pm_in_node_t *in_node = (const pm_in_node_t *) condition;
+ const pm_node_location_t in_location = PM_NODE_START_LOCATION(in_node);
+ const pm_node_location_t pattern_location = PM_NODE_START_LOCATION(in_node->pattern);
- // Establish branch coverage for the else clause.
- if (PM_BRANCH_COVERAGE_P(iseq)) {
- rb_code_location_t branch_location = pm_code_location(scope_node, cast->consequent->statements != NULL ? ((const pm_node_t *) cast->consequent->statements) : ((const pm_node_t *) cast->consequent));
- add_trace_branch_coverage(iseq, ret, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
- }
+ if (branch_id) {
+ PUSH_INSN(body_seq, in_location, putnil);
+ }
- PM_COMPILE((const pm_node_t *) cast->consequent);
- PUSH_INSNL(ret, else_location, jump, end_label);
- }
- else {
- PUSH_INSN(ret, location, pop);
+ LABEL *body_label = NEW_LABEL(in_location.line);
+ PUSH_LABEL(body_seq, body_label);
+ PUSH_INSN1(body_seq, in_location, adjuststack, INT2FIX(in_single_pattern ? 6 : 2));
- // Establish branch coverage for the implicit else clause.
- if (PM_BRANCH_COVERAGE_P(iseq)) {
- add_trace_branch_coverage(iseq, ret, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
- }
+ // Establish branch coverage for the in clause.
+ if (PM_BRANCH_COVERAGE_P(iseq)) {
+ rb_code_location_t branch_location = pm_code_location(scope_node, in_node->statements != NULL ? ((const pm_node_t *) in_node->statements) : ((const pm_node_t *) in_node));
+ add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "in", branches);
+ }
+
+ if (in_node->statements != NULL) {
+ PM_COMPILE_INTO_ANCHOR(body_seq, (const pm_node_t *) in_node->statements);
+ }
+ else if (!popped) {
+ PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
+ }
+
+ PUSH_INSNL(body_seq, in_location, jump, end_label);
+ LABEL *next_pattern_label = NEW_LABEL(pattern_location.line);
+
+ PUSH_INSN(cond_seq, pattern_location, dup);
+ pm_compile_pattern(iseq, scope_node, in_node->pattern, cond_seq, body_label, next_pattern_label, in_single_pattern, true, 2);
+ PUSH_LABEL(cond_seq, next_pattern_label);
+ LABEL_UNREMOVABLE(next_pattern_label);
+ }
+
+ if (node->else_clause != NULL) {
+ // If we have an `else` clause, then this becomes our fallback (and
+ // there is no need to compile in code to potentially raise an
+ // error).
+ const pm_else_node_t *else_node = node->else_clause;
+
+ PUSH_LABEL(cond_seq, else_label);
+ PUSH_INSN(cond_seq, *location, pop);
+ PUSH_INSN(cond_seq, *location, pop);
+
+ // Establish branch coverage for the else clause.
+ if (PM_BRANCH_COVERAGE_P(iseq)) {
+ rb_code_location_t branch_location = pm_code_location(scope_node, else_node->statements != NULL ? ((const pm_node_t *) else_node->statements) : ((const pm_node_t *) else_node));
+ add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
+ }
+
+ PM_COMPILE_INTO_ANCHOR(cond_seq, (const pm_node_t *) else_node);
+ PUSH_INSNL(cond_seq, *location, jump, end_label);
+ PUSH_INSN(cond_seq, *location, putnil);
+ if (popped) PUSH_INSN(cond_seq, *location, putnil);
+ }
+ else {
+ // Otherwise, if we do not have an `else` clause, we will compile in
+ // the code to handle raising an appropriate error.
+ PUSH_LABEL(cond_seq, else_label);
+
+ // Establish branch coverage for the implicit else clause.
+ add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
+
+ if (in_single_pattern) {
+ pm_compile_pattern_error_handler(iseq, scope_node, (const pm_node_t *) node, cond_seq, end_label, popped);
+ }
+ else {
+ PUSH_INSN1(cond_seq, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
+ PUSH_INSN1(cond_seq, *location, putobject, rb_eNoMatchingPatternError);
+ PUSH_INSN1(cond_seq, *location, topn, INT2FIX(2));
+ PUSH_SEND(cond_seq, *location, id_core_raise, INT2FIX(2));
+
+ PUSH_INSN1(cond_seq, *location, adjuststack, INT2FIX(3));
+ if (!popped) PUSH_INSN(cond_seq, *location, putnil);
+ PUSH_INSNL(cond_seq, *location, jump, end_label);
+ PUSH_INSN1(cond_seq, *location, dupn, INT2FIX(1));
+ if (popped) PUSH_INSN(cond_seq, *location, putnil);
+ }
+ }
+
+ // At the end of all of this compilation, we will add the code for the
+ // conditions first, then the various bodies, then mark the end of the
+ // entire sequence with the end label.
+ PUSH_SEQ(ret, cond_seq);
+ PUSH_SEQ(ret, body_seq);
+ PUSH_LABEL(ret, end_label);
+}
+
+static inline void
+pm_compile_forwarding_super_node(rb_iseq_t *iseq, const pm_forwarding_super_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ const rb_iseq_t *block = NULL;
+ const rb_iseq_t *previous_block = NULL;
+ LABEL *retry_label = NULL;
+ LABEL *retry_end_l = NULL;
+
+ if (node->block != NULL) {
+ previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
+ ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
+
+ retry_label = NEW_LABEL(location->line);
+ retry_end_l = NEW_LABEL(location->line);
+
+ PUSH_LABEL(ret, retry_label);
+ }
+ else {
+ iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
+ }
+
+ PUSH_INSN(ret, *location, putself);
+ int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
+
+ if (node->block != NULL) {
+ pm_scope_node_t next_scope_node;
+ pm_scope_node_init((const pm_node_t *) node->block, &next_scope_node, scope_node);
+
+ ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->line);
+ pm_scope_node_destroy(&next_scope_node);
+ RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
+ }
+
+ DECL_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 (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
+ flag |= VM_CALL_FORWARDING;
+ pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
+ PUSH_GETLOCAL(ret, *location, mult_local.index, mult_local.level);
+
+ const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, 0, flag, NULL, block != NULL);
+ PUSH_INSN2(ret, *location, invokesuperforward, callinfo, block);
+
+ if (popped) PUSH_INSN(ret, *location, pop);
+ if (node->block) {
+ ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
+ }
+ return;
+ }
+
+ 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;
+ PUSH_GETLOCAL(args, *location, 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);
+ PUSH_GETLOCAL(args, *location, 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;
+ PUSH_GETLOCAL(args, *location, idx, depth);
+ PUSH_INSN1(args, *location, 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);
+ PUSH_GETLOCAL(args, *location, idx, depth);
+ }
+
+ if (local_body->param.flags.has_rest) {
+ // argc remains unchanged from rest branch
+ PUSH_INSN1(args, *location, newarray, INT2FIX(j));
+ PUSH_INSN(args, *location, 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++;
+
+ PUSH_INSN1(args, *location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
- if (!popped) PUSH_INSN(ret, location, putnil);
- PUSH_INSNL(ret, location, jump, end_label);
+ if (local_body->param.flags.has_kwrest) {
+ int idx = local_body->local_table_size - local_keyword->rest_start;
+ PUSH_GETLOCAL(args, *location, idx, depth);
+ RUBY_ASSERT(local_keyword->num > 0);
+ PUSH_SEND(args, *location, rb_intern("dup"), INT2FIX(0));
+ }
+ else {
+ PUSH_INSN1(args, *location, newhash, INT2FIX(0));
+ }
+ int i = 0;
+ for (; i < local_keyword->num; ++i) {
+ ID id = local_keyword->table[i];
+ int idx = local_size - get_local_var_idx(local_iseq, id);
+
+ {
+ VALUE operand = ID2SYM(id);
+ PUSH_INSN1(args, *location, putobject, operand);
}
+
+ PUSH_GETLOCAL(args, *location, idx, depth);
}
- PUSH_SEQ(ret, body_seq);
- PUSH_LABEL(ret, end_label);
+ PUSH_SEND(args, *location, id_core_hash_merge_ptr, INT2FIX(i * 2 + 1));
+ flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
+ }
+ else if (local_body->param.flags.has_kwrest) {
+ int idx = local_body->local_table_size - local_keyword->rest_start;
+ PUSH_GETLOCAL(args, *location, idx, depth);
+ argc++;
+ flag |= VM_CALL_KW_SPLAT;
+ }
+
+ PUSH_SEQ(ret, args);
+
+ {
+ const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flag, NULL, block != NULL);
+ PUSH_INSN2(ret, *location, invokesuper, callinfo, block);
+ }
+
+ if (node->block != NULL) {
+ pm_compile_retry_end_label(iseq, ret, retry_end_l);
+ PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
+ ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
+ }
+
+ if (popped) PUSH_INSN(ret, *location, pop);
+}
+
+static inline void
+pm_compile_match_required_node(rb_iseq_t *iseq, const pm_match_required_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ LABEL *matched_label = NEW_LABEL(location->line);
+ LABEL *unmatched_label = NEW_LABEL(location->line);
+ LABEL *done_label = NEW_LABEL(location->line);
+
+ // First, we're going to push a bunch of stuff onto the stack that is
+ // going to serve as our scratch space.
+ PUSH_INSN(ret, *location, putnil); // key error key
+ PUSH_INSN(ret, *location, putnil); // key error matchee
+ PUSH_INSN1(ret, *location, putobject, Qfalse); // key error?
+ PUSH_INSN(ret, *location, putnil); // error string
+ PUSH_INSN(ret, *location, putnil); // deconstruct cache
+
+ // Next we're going to compile the value expression such that it's on
+ // the stack.
+ PM_COMPILE_NOT_POPPED(node->value);
+
+ // Here we'll dup it so that it can be used for comparison, but also be
+ // used for error handling.
+ PUSH_INSN(ret, *location, dup);
+
+ // Next we'll compile the pattern. We indicate to the pm_compile_pattern
+ // function that this is the only pattern that will be matched against
+ // through the in_single_pattern parameter. We also indicate that the
+ // value to compare against is 2 slots from the top of the stack (the
+ // base_index parameter).
+ pm_compile_pattern(iseq, scope_node, node->pattern, ret, matched_label, unmatched_label, true, true, 2);
+
+ // If the pattern did not match the value, then we're going to compile
+ // in our error handler code. This will determine which error to raise
+ // and raise it.
+ PUSH_LABEL(ret, unmatched_label);
+ pm_compile_pattern_error_handler(iseq, scope_node, (const pm_node_t *) node, ret, done_label, popped);
+
+ // If the pattern did match, we'll clean up the values we've pushed onto
+ // the stack and then push nil onto the stack if it's not popped.
+ PUSH_LABEL(ret, matched_label);
+ PUSH_INSN1(ret, *location, adjuststack, INT2FIX(6));
+ if (!popped) PUSH_INSN(ret, *location, putnil);
+ PUSH_INSNL(ret, *location, jump, done_label);
+ PUSH_LABEL(ret, done_label);
+}
+
+static inline void
+pm_compile_match_write_node(rb_iseq_t *iseq, const pm_match_write_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ LABEL *fail_label = NEW_LABEL(location->line);
+ LABEL *end_label = NEW_LABEL(location->line);
+
+ // 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((const pm_node_t *) node->call);
+
+ // 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.
+ {
+ VALUE operand = rb_id2sym(idBACKREF);
+ PUSH_INSN1(ret, *location, getglobal, operand);
+ }
+
+ PUSH_INSN(ret, *location, dup);
+ PUSH_INSNL(ret, *location, branchunless, fail_label);
+
+ // 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 = node->targets.size;
+
+ if (targets_count == 1) {
+ const pm_node_t *target = node->targets.nodes[0];
+ RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
+
+ const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
+ pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
+
+ {
+ VALUE operand = rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name));
+ PUSH_INSN1(ret, *location, putobject, operand);
+ }
+
+ PUSH_SEND(ret, *location, idAREF, INT2FIX(1));
+ PUSH_LABEL(ret, fail_label);
+ PUSH_SETLOCAL(ret, *location, index.index, index.level);
+ if (popped) PUSH_INSN(ret, *location, pop);
return;
- }
- case PM_CASE_MATCH_NODE: {
- // case foo; in bar; end
- // ^^^^^^^^^^^^^^^^^^^^^
- //
- // If you use the `case` keyword to create a case match node, it will
- // match against all of the `in` clauses until it finds one that
- // matches. If it doesn't find one, it can optionally fall back to an
- // `else` clause. If none is present and a match wasn't found, it will
- // raise an appropriate error.
- const pm_case_match_node_t *cast = (const pm_case_match_node_t *) node;
+ }
- // This is the anchor that we will compile the bodies of the various
- // `in` nodes into. We'll make sure that the patterns that are compiled
- // jump into the correct spots within this anchor.
- DECL_ANCHOR(body_seq);
- INIT_ANCHOR(body_seq);
+ DECL_ANCHOR(fail_anchor);
- // This is the anchor that we will compile the patterns of the various
- // `in` nodes into. If a match is found, they will need to jump into the
- // body_seq anchor to the correct spot.
- DECL_ANCHOR(cond_seq);
- INIT_ANCHOR(cond_seq);
+ // 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++) {
+ const pm_node_t *target = node->targets.nodes[targets_index];
+ RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
- // This label is used to indicate the end of the entire node. It is
- // jumped to after the entire stack is cleaned up.
- LABEL *end_label = NEW_LABEL(location.line);
+ const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
+ pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
- // This label is used as the fallback for the case match. If no match is
- // found, then we jump to this label. This is either an `else` clause or
- // an error handler.
- LABEL *else_label = NEW_LABEL(location.line);
+ if (((size_t) targets_index) < (targets_count - 1)) {
+ PUSH_INSN(ret, *location, dup);
+ }
- // We're going to use this to uniquely identify each branch so that we
- // can track coverage information.
- rb_code_location_t case_location = { 0 };
- VALUE branches = Qfalse;
- int branch_id = 0;
+ {
+ VALUE operand = rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name));
+ PUSH_INSN1(ret, *location, putobject, operand);
+ }
- if (PM_BRANCH_COVERAGE_P(iseq)) {
- case_location = pm_code_location(scope_node, (const pm_node_t *) cast);
- branches = decl_branch_base(iseq, PTR2NUM(cast), &case_location, "case");
+ PUSH_SEND(ret, *location, idAREF, INT2FIX(1));
+ PUSH_SETLOCAL(ret, *location, index.index, index.level);
+
+ PUSH_INSN(fail_anchor, *location, putnil);
+ PUSH_SETLOCAL(fail_anchor, *location, index.index, index.level);
+ }
+
+ // Since we matched successfully, now we'll jump to the end.
+ PUSH_INSNL(ret, *location, jump, end_label);
+
+ // In the case that the match failed, we'll loop through each local
+ // variable target and set all of them to `nil`.
+ PUSH_LABEL(ret, fail_label);
+ PUSH_INSN(ret, *location, pop);
+ PUSH_SEQ(ret, fail_anchor);
+
+ // Finally, we can push the end label for either case.
+ PUSH_LABEL(ret, end_label);
+ if (popped) PUSH_INSN(ret, *location, pop);
+}
+
+static inline void
+pm_compile_next_node(rb_iseq_t *iseq, const pm_next_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
+ LABEL *splabel = NEW_LABEL(0);
+ PUSH_LABEL(ret, splabel);
+
+ if (node->arguments) {
+ PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
+ }
+ else {
+ PUSH_INSN(ret, *location, putnil);
}
+ pm_add_ensure_iseq(ret, iseq, 0, scope_node);
- // If there is only one pattern, then the behavior changes a bit. It
- // effectively gets treated as a match required node (this is how it is
- // represented in the other parser).
- bool in_single_pattern = cast->consequent == NULL && cast->conditions.size == 1;
+ PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
+ PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
- // First, we're going to push a bunch of stuff onto the stack that is
- // going to serve as our scratch space.
- if (in_single_pattern) {
- PUSH_INSN(ret, location, putnil); // key error key
- PUSH_INSN(ret, location, putnil); // key error matchee
- PUSH_INSN1(ret, location, putobject, Qfalse); // key error?
- PUSH_INSN(ret, location, putnil); // error string
+ PUSH_ADJUST_RESTORE(ret, splabel);
+ if (!popped) PUSH_INSN(ret, *location, putnil);
+ }
+ else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
+ LABEL *splabel = NEW_LABEL(0);
+
+ PUSH_LABEL(ret, splabel);
+ PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
+
+ if (node->arguments != NULL) {
+ PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
+ }
+ else {
+ PUSH_INSN(ret, *location, putnil);
}
- // Now we're going to compile the value to match against.
- PUSH_INSN(ret, location, putnil); // deconstruct cache
- PM_COMPILE_NOT_POPPED(cast->predicate);
+ pm_add_ensure_iseq(ret, iseq, 0, scope_node);
+ PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
+ PUSH_ADJUST_RESTORE(ret, splabel);
+ splabel->unremovable = FALSE;
- // Next, we'll loop through every in clause and compile its body into
- // the body_seq anchor and its pattern into the cond_seq anchor. We'll
- // make sure the pattern knows how to jump correctly into the body if it
- // finds a match.
- for (size_t index = 0; index < cast->conditions.size; index++) {
- const pm_node_t *condition = cast->conditions.nodes[index];
- RUBY_ASSERT(PM_NODE_TYPE_P(condition, PM_IN_NODE));
+ if (!popped) PUSH_INSN(ret, *location, putnil);
+ }
+ else {
+ const rb_iseq_t *ip = iseq;
+ unsigned long throw_flag = 0;
- const pm_in_node_t *in_node = (const pm_in_node_t *) condition;
- const pm_node_location_t in_location = PM_NODE_START_LOCATION(parser, in_node);
- const pm_node_location_t pattern_location = PM_NODE_START_LOCATION(parser, in_node->pattern);
+ while (ip) {
+ if (!ISEQ_COMPILE_DATA(ip)) {
+ ip = 0;
+ break;
+ }
- if (branch_id) {
- PUSH_INSN(body_seq, in_location, putnil);
+ 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) {
+ COMPILE_ERROR(iseq, location->line, "Invalid next");
+ return;
}
- LABEL *body_label = NEW_LABEL(in_location.line);
- PUSH_LABEL(body_seq, body_label);
- PUSH_INSN1(body_seq, in_location, adjuststack, INT2FIX(in_single_pattern ? 6 : 2));
+ ip = ISEQ_BODY(ip)->parent_iseq;
+ }
- // Establish branch coverage for the in clause.
- if (PM_BRANCH_COVERAGE_P(iseq)) {
- rb_code_location_t branch_location = pm_code_location(scope_node, in_node->statements != NULL ? ((const pm_node_t *) in_node->statements) : ((const pm_node_t *) in_node));
- add_trace_branch_coverage(iseq, body_seq, &branch_location, branch_location.beg_pos.column, branch_id++, "in", branches);
+ if (ip != 0) {
+ if (node->arguments) {
+ PM_COMPILE_NOT_POPPED((const pm_node_t *) node->arguments);
+ }
+ else {
+ PUSH_INSN(ret, *location, putnil);
+ }
+
+ PUSH_INSN1(ret, *location, throw, INT2FIX(throw_flag | TAG_NEXT));
+ if (popped) PUSH_INSN(ret, *location, pop);
+ }
+ else {
+ COMPILE_ERROR(iseq, location->line, "Invalid next");
+ }
+ }
+}
+
+static inline void
+pm_compile_redo_node(rb_iseq_t *iseq, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
+ LABEL *splabel = NEW_LABEL(0);
+
+ PUSH_LABEL(ret, splabel);
+ PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->redo_label);
+ pm_add_ensure_iseq(ret, iseq, 0, scope_node);
+
+ PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
+ PUSH_ADJUST_RESTORE(ret, splabel);
+ if (!popped) PUSH_INSN(ret, *location, putnil);
+ }
+ 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);
+
+ PUSH_LABEL(ret, splabel);
+ pm_add_ensure_iseq(ret, iseq, 0, scope_node);
+ PUSH_ADJUST(ret, *location, ISEQ_COMPILE_DATA(iseq)->start_label);
+
+ PUSH_INSNL(ret, *location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
+ PUSH_ADJUST_RESTORE(ret, splabel);
+ if (!popped) PUSH_INSN(ret, *location, putnil);
+ }
+ else {
+ const rb_iseq_t *ip = iseq;
+
+ while (ip) {
+ if (!ISEQ_COMPILE_DATA(ip)) {
+ ip = 0;
+ break;
}
- if (in_node->statements != NULL) {
- PM_COMPILE_INTO_ANCHOR(body_seq, (const pm_node_t *) in_node->statements);
+ if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
+ break;
}
- else if (!popped) {
- PUSH_SYNTHETIC_PUTNIL(body_seq, iseq);
+ else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_BLOCK) {
+ break;
+ }
+ else if (ISEQ_BODY(ip)->type == ISEQ_TYPE_EVAL) {
+ COMPILE_ERROR(iseq, location->line, "Invalid redo");
+ return;
}
- PUSH_INSNL(body_seq, in_location, jump, end_label);
- LABEL *next_pattern_label = NEW_LABEL(pattern_location.line);
+ ip = ISEQ_BODY(ip)->parent_iseq;
+ }
- PUSH_INSN(cond_seq, pattern_location, dup);
- pm_compile_pattern(iseq, scope_node, in_node->pattern, cond_seq, body_label, next_pattern_label, in_single_pattern, false, true, 2);
- PUSH_LABEL(cond_seq, next_pattern_label);
- LABEL_UNREMOVABLE(next_pattern_label);
+ if (ip != 0) {
+ PUSH_INSN(ret, *location, putnil);
+ PUSH_INSN1(ret, *location, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
+ if (popped) PUSH_INSN(ret, *location, pop);
}
+ else {
+ COMPILE_ERROR(iseq, location->line, "Invalid redo");
+ }
+ }
+}
+
+static inline void
+pm_compile_rescue_node(rb_iseq_t *iseq, const pm_rescue_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ iseq_set_exception_local_table(iseq);
- if (cast->consequent != NULL) {
- // If we have an `else` clause, then this becomes our fallback (and
- // there is no need to compile in code to potentially raise an
- // error).
- const pm_else_node_t *else_node = (const pm_else_node_t *) cast->consequent;
+ // First, establish the labels that we need to be able to jump to within
+ // this compilation block.
+ LABEL *exception_match_label = NEW_LABEL(location->line);
+ LABEL *rescue_end_label = NEW_LABEL(location->line);
- PUSH_LABEL(cond_seq, else_label);
- PUSH_INSN(cond_seq, location, pop);
- PUSH_INSN(cond_seq, location, pop);
+ // Next, compile each of the exceptions that we're going to be
+ // handling. For each one, we'll add instructions to check if the
+ // exception matches the raised one, and if it does then jump to the
+ // exception_match_label label. Otherwise it will fall through to the
+ // subsequent check. If there are no exceptions, we'll only check
+ // StandardError.
+ const pm_node_list_t *exceptions = &node->exceptions;
- // Establish branch coverage for the else clause.
- if (PM_BRANCH_COVERAGE_P(iseq)) {
- rb_code_location_t branch_location = pm_code_location(scope_node, else_node->statements != NULL ? ((const pm_node_t *) else_node->statements) : ((const pm_node_t *) else_node));
- add_trace_branch_coverage(iseq, cond_seq, &branch_location, branch_location.beg_pos.column, branch_id, "else", branches);
+ if (exceptions->size > 0) {
+ for (size_t index = 0; index < exceptions->size; index++) {
+ PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
+ PM_COMPILE(exceptions->nodes[index]);
+ int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
+ if (PM_NODE_TYPE_P(exceptions->nodes[index], PM_SPLAT_NODE)) {
+ checkmatch_flags |= VM_CHECKMATCH_ARRAY;
}
+ PUSH_INSN1(ret, *location, checkmatch, INT2FIX(checkmatch_flags));
+ PUSH_INSNL(ret, *location, branchif, exception_match_label);
+ }
+ }
+ else {
+ PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
+ PUSH_INSN1(ret, *location, putobject, rb_eStandardError);
+ PUSH_INSN1(ret, *location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
+ PUSH_INSNL(ret, *location, branchif, exception_match_label);
+ }
+
+ // If none of the exceptions that we are matching against matched, then
+ // we'll jump straight to the rescue_end_label label.
+ PUSH_INSNL(ret, *location, jump, rescue_end_label);
+
+ // Here we have the exception_match_label, which is where the
+ // control-flow goes in the case that one of the exceptions matched.
+ // Here we will compile the instructions to handle the exception.
+ PUSH_LABEL(ret, exception_match_label);
+ PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
+
+ // If we have a reference to the exception, then we'll compile the write
+ // into the instruction sequence. This can look quite different
+ // depending on the kind of write being performed.
+ if (node->reference) {
+ DECL_ANCHOR(writes);
+ DECL_ANCHOR(cleanup);
+
+ pm_compile_target_node(iseq, node->reference, ret, writes, cleanup, scope_node, NULL);
+ PUSH_GETLOCAL(ret, *location, LVAR_ERRINFO, 0);
+
+ PUSH_SEQ(ret, writes);
+ PUSH_SEQ(ret, cleanup);
+ }
+
+ // If we have statements to execute, we'll compile them here. Otherwise
+ // we'll push nil onto the stack.
+ if (node->statements != NULL) {
+ // We'll temporarily remove the end_label location from the iseq
+ // when compiling the statements so that next/redo statements
+ // inside the body will throw to the correct place instead of
+ // jumping straight to the end of this iseq
+ LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
+ ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
+
+ PM_COMPILE((const pm_node_t *) node->statements);
+
+ // Now restore the end_label
+ ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
+ }
+ else {
+ PUSH_INSN(ret, *location, putnil);
+ }
+
+ PUSH_INSN(ret, *location, leave);
+
+ // Here we'll insert the rescue_end_label label, which is jumped to if
+ // none of the exceptions matched. It will cause the control-flow to
+ // either jump to the next rescue clause or it will fall through to the
+ // subsequent instruction returning the raised error.
+ PUSH_LABEL(ret, rescue_end_label);
+ if (node->subsequent != NULL) {
+ PM_COMPILE((const pm_node_t *) node->subsequent);
+ }
+ else {
+ PUSH_GETLOCAL(ret, *location, 1, 0);
+ }
+}
+
+static inline void
+pm_compile_return_node(rb_iseq_t *iseq, const pm_return_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ const pm_arguments_node_t *arguments = node->arguments;
+ 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;
+ }
+
+ switch (parent_type) {
+ case ISEQ_TYPE_TOP:
+ case ISEQ_TYPE_MAIN:
+ if (arguments) {
+ rb_warn("argument of top-level return is ignored");
+ }
+ if (parent_iseq == iseq) {
+ type = ISEQ_TYPE_METHOD;
+ }
+ break;
+ default:
+ break;
+ }
+
+ if (type == ISEQ_TYPE_METHOD) {
+ splabel = NEW_LABEL(0);
+ PUSH_LABEL(ret, splabel);
+ PUSH_ADJUST(ret, *location, 0);
+ }
+
+ if (arguments != NULL) {
+ PM_COMPILE_NOT_POPPED((const pm_node_t *) arguments);
+ }
+ else {
+ PUSH_INSN(ret, *location, putnil);
+ }
+
+ if (type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
+ pm_add_ensure_iseq(ret, iseq, 1, scope_node);
+ PUSH_TRACE(ret, RUBY_EVENT_RETURN);
+ PUSH_INSN(ret, *location, leave);
+ PUSH_ADJUST_RESTORE(ret, splabel);
+ if (!popped) PUSH_INSN(ret, *location, putnil);
+ }
+ else {
+ PUSH_INSN1(ret, *location, throw, INT2FIX(TAG_RETURN));
+ if (popped) PUSH_INSN(ret, *location, pop);
+ }
+}
+
+static inline void
+pm_compile_super_node(rb_iseq_t *iseq, const pm_super_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ DECL_ANCHOR(args);
+
+ LABEL *retry_label = NEW_LABEL(location->line);
+ LABEL *retry_end_l = NEW_LABEL(location->line);
+
+ const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
+ const rb_iseq_t *current_block;
+ ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
+
+ PUSH_LABEL(ret, retry_label);
+ PUSH_INSN(ret, *location, putself);
+
+ int flags = 0;
+ struct rb_callinfo_kwarg *keywords = NULL;
+ int argc = pm_setup_args(node->arguments, node->block, &flags, &keywords, iseq, ret, scope_node, location);
+ bool is_forwardable = (node->arguments != NULL) && PM_NODE_FLAG_P(node->arguments, PM_ARGUMENTS_NODE_FLAGS_CONTAINS_FORWARDING);
+ flags |= VM_CALL_SUPER | VM_CALL_FCALL;
+
+ if (node->block && PM_NODE_TYPE_P(node->block, PM_BLOCK_NODE)) {
+ pm_scope_node_t next_scope_node;
+ pm_scope_node_init(node->block, &next_scope_node, scope_node);
+
+ ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location->line);
+ pm_scope_node_destroy(&next_scope_node);
+ }
+
+ if (!node->block) {
+ iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
+ }
+
+ if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
+ PUSH_INSN(args, *location, splatkw);
+ }
+
+ PUSH_SEQ(ret, args);
+ if (is_forwardable && ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
+ flags |= VM_CALL_FORWARDING;
+
+ {
+ const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
+ PUSH_INSN2(ret, *location, invokesuperforward, callinfo, current_block);
+ }
+ }
+ else {
+ {
+ const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL);
+ PUSH_INSN2(ret, *location, invokesuper, callinfo, current_block);
+ }
+
+ }
+
+ pm_compile_retry_end_label(iseq, ret, retry_end_l);
+
+ if (popped) PUSH_INSN(ret, *location, pop);
+ ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
+ PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
+}
+
+static inline void
+pm_compile_yield_node(rb_iseq_t *iseq, const pm_yield_node_t *node, const pm_node_location_t *location, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->type) {
+ case ISEQ_TYPE_TOP:
+ case ISEQ_TYPE_MAIN:
+ case ISEQ_TYPE_CLASS:
+ COMPILE_ERROR(iseq, location->line, "Invalid yield");
+ return;
+ default: /* valid */;
+ }
+
+ int argc = 0;
+ int flags = 0;
+ struct rb_callinfo_kwarg *keywords = NULL;
+
+ if (node->arguments) {
+ argc = pm_setup_args(node->arguments, NULL, &flags, &keywords, iseq, ret, scope_node, location);
+ }
+
+ const struct rb_callinfo *callinfo = new_callinfo(iseq, 0, argc, flags, keywords, FALSE);
+ PUSH_INSN1(ret, *location, invokeblock, callinfo);
+
+ iseq_set_use_block(ISEQ_BODY(iseq)->local_iseq);
+ if (popped) PUSH_INSN(ret, *location, pop);
+
+ int level = 0;
+ for (const rb_iseq_t *tmp_iseq = iseq; 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);
+}
+
+/**
+ * Compiles a prism node into instruction sequences.
+ *
+ * iseq - The current instruction sequence object (used for locals)
+ * node - The prism node to compile
+ * ret - The linked list of instructions to append instructions onto
+ * popped - True if compiling something with no side effects, so instructions don't
+ * need to be added
+ * scope_node - Stores parser and local information
+ */
+static void
+pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, bool popped, pm_scope_node_t *scope_node)
+{
+ const pm_node_location_t location = PM_NODE_START_LOCATION(node);
+ int lineno = (int) location.line;
+
+ if (PM_NODE_TYPE_P(node, PM_BEGIN_NODE) && (((const pm_begin_node_t *) node)->statements == NULL) && (((const pm_begin_node_t *) node)->rescue_clause != NULL)) {
+ // If this node is a begin node and it has empty statements and also
+ // has a rescue clause, then the other parser considers it as
+ // starting on the same line as the rescue, as opposed to the
+ // location of the begin keyword. We replicate that behavior here.
+ lineno = (int) PM_NODE_START_LINE_COLUMN(((const pm_begin_node_t *) node)->rescue_clause).line;
+ }
+
+ if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_NEWLINE) && ISEQ_COMPILE_DATA(iseq)->last_line != lineno) {
+ // If this node has the newline flag set and it is on a new line
+ // from the previous nodes that have been compiled for this ISEQ,
+ // then we need to emit a newline event.
+ int event = RUBY_EVENT_LINE;
+
+ ISEQ_COMPILE_DATA(iseq)->last_line = lineno;
+ if (lineno > 0 && ISEQ_COVERAGE(iseq) && ISEQ_LINE_COVERAGE(iseq)) {
+ event |= RUBY_EVENT_COVERAGE_LINE;
+ }
+ PUSH_TRACE(ret, event);
+ }
+
+ switch (PM_NODE_TYPE(node)) {
+ case PM_ALIAS_GLOBAL_VARIABLE_NODE:
+ // alias $foo $bar
+ // ^^^^^^^^^^^^^^^
+ pm_compile_alias_global_variable_node(iseq, (const pm_alias_global_variable_node_t *) node, &location, ret, popped, scope_node);
+ return;
+ case PM_ALIAS_METHOD_NODE:
+ // alias foo bar
+ // ^^^^^^^^^^^^^
+ pm_compile_alias_method_node(iseq, (const pm_alias_method_node_t *) node, &location, ret, popped, scope_node);
+ return;
+ case PM_AND_NODE:
+ // a and b
+ // ^^^^^^^
+ pm_compile_and_node(iseq, (const pm_and_node_t *) node, &location, ret, popped, scope_node);
+ return;
+ case PM_ARGUMENTS_NODE: {
+ // break foo
+ // ^^^
+ //
+ // These are ArgumentsNodes that are not compiled directly by their
+ // parent call nodes, used in the cases of NextNodes, ReturnNodes, and
+ // BreakNodes. They can create an array like ArrayNode.
+ const pm_arguments_node_t *cast = (const pm_arguments_node_t *) node;
+ const pm_node_list_t *elements = &cast->arguments;
- PM_COMPILE_INTO_ANCHOR(cond_seq, (const pm_node_t *) else_node);
- PUSH_INSNL(cond_seq, location, jump, end_label);
- PUSH_INSN(cond_seq, location, putnil);
- if (popped) PUSH_INSN(cond_seq, location, putnil);
+ if (elements->size == 1) {
+ // If we are only returning a single element through one of the jump
+ // nodes, then we will only compile that node directly.
+ PM_COMPILE(elements->nodes[0]);
}
else {
- // Otherwise, if we do not have an `else` clause, we will compile in
- // the code to handle raising an appropriate error.
- PUSH_LABEL(cond_seq, else_label);
+ pm_compile_array_node(iseq, (const pm_node_t *) cast, elements, &location, ret, popped, scope_node);
+ }
+ return;
+ }
+ case PM_ARRAY_NODE: {
+ // [foo, bar, baz]
+ // ^^^^^^^^^^^^^^^
+ const pm_array_node_t *cast = (const pm_array_node_t *) node;
+ pm_compile_array_node(iseq, (const pm_node_t *) cast, &cast->elements, &location, ret, popped, scope_node);
+ return;
+ }
+ case PM_ASSOC_NODE: {
+ // { foo: 1 }
+ // ^^^^^^
+ //
+ // foo(bar: 1)
+ // ^^^^^^
+ const pm_assoc_node_t *cast = (const pm_assoc_node_t *) node;
- // Establish branch coverage for the implicit else clause.
- add_trace_branch_coverage(iseq, cond_seq, &case_location, case_location.beg_pos.column, branch_id, "else", branches);
+ PM_COMPILE(cast->key);
+ PM_COMPILE(cast->value);
- if (in_single_pattern) {
- pm_compile_pattern_error_handler(iseq, scope_node, node, cond_seq, end_label, popped);
- }
- else {
- PUSH_INSN1(cond_seq, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
- PUSH_INSN1(cond_seq, location, putobject, rb_eNoMatchingPatternError);
- PUSH_INSN1(cond_seq, location, topn, INT2FIX(2));
- PUSH_SEND(cond_seq, location, id_core_raise, INT2FIX(2));
+ return;
+ }
+ case PM_ASSOC_SPLAT_NODE: {
+ // { **foo }
+ // ^^^^^
+ //
+ // def foo(**); bar(**); end
+ // ^^
+ const pm_assoc_splat_node_t *cast = (const pm_assoc_splat_node_t *) node;
- PUSH_INSN1(cond_seq, location, adjuststack, INT2FIX(3));
- if (!popped) PUSH_INSN(cond_seq, location, putnil);
- PUSH_INSNL(cond_seq, location, jump, end_label);
- PUSH_INSN1(cond_seq, location, dupn, INT2FIX(1));
- if (popped) PUSH_INSN(cond_seq, location, putnil);
- }
+ if (cast->value != NULL) {
+ PM_COMPILE(cast->value);
+ }
+ else if (!popped) {
+ pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_POW, 0);
+ PUSH_GETLOCAL(ret, location, index.index, index.level);
}
- // At the end of all of this compilation, we will add the code for the
- // conditions first, then the various bodies, then mark the end of the
- // entire sequence with the end label.
- PUSH_SEQ(ret, cond_seq);
- PUSH_SEQ(ret, body_seq);
- PUSH_LABEL(ret, end_label);
+ return;
+ }
+ case PM_BACK_REFERENCE_READ_NODE: {
+ // $+
+ // ^^
+ if (!popped) {
+ const pm_back_reference_read_node_t *cast = (const pm_back_reference_read_node_t *) node;
+ VALUE backref = pm_compile_back_reference_ref(scope_node, cast);
+
+ PUSH_INSN2(ret, location, getspecial, INT2FIX(1), backref);
+ }
+ return;
+ }
+ case PM_BEGIN_NODE: {
+ // begin end
+ // ^^^^^^^^^
+ const pm_begin_node_t *cast = (const pm_begin_node_t *) node;
+
+ if (cast->ensure_clause) {
+ // Compiling the ensure clause will compile the rescue clause (if
+ // there is one), which will compile the begin statements.
+ pm_compile_ensure(iseq, cast, &location, ret, popped, scope_node);
+ }
+ else if (cast->rescue_clause) {
+ // Compiling rescue will compile begin statements (if applicable).
+ pm_compile_rescue(iseq, cast, &location, ret, popped, scope_node);
+ }
+ else {
+ // If there is neither ensure or rescue, the just compile the
+ // statements.
+ if (cast->statements != NULL) {
+ PM_COMPILE((const pm_node_t *) cast->statements);
+ }
+ else if (!popped) {
+ PUSH_SYNTHETIC_PUTNIL(ret, iseq);
+ }
+ }
+ return;
+ }
+ case PM_BLOCK_ARGUMENT_NODE: {
+ // foo(&bar)
+ // ^^^^
+ const pm_block_argument_node_t *cast = (const pm_block_argument_node_t *) node;
+ if (cast->expression != NULL) {
+ PM_COMPILE(cast->expression);
+ }
+ else {
+ // If there's no expression, this must be block forwarding.
+ pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_AND, 0);
+ PUSH_INSN2(ret, location, getblockparamproxy, INT2FIX(local_index.index + VM_ENV_DATA_SIZE - 1), INT2FIX(local_index.level));
+ }
+ return;
+ }
+ case PM_BREAK_NODE:
+ // break
+ // ^^^^^
+ //
+ // break foo
+ // ^^^^^^^^^
+ pm_compile_break_node(iseq, (const pm_break_node_t *) node, &location, ret, popped, scope_node);
+ return;
+ case PM_CALL_NODE:
+ // foo
+ // ^^^
+ //
+ // foo.bar
+ // ^^^^^^^
+ //
+ // foo.bar() {}
+ // ^^^^^^^^^^^^
+ pm_compile_call_node(iseq, (const pm_call_node_t *) node, ret, popped, scope_node);
+ return;
+ case PM_CALL_AND_WRITE_NODE: {
+ // foo.bar &&= baz
+ // ^^^^^^^^^^^^^^^
+ const pm_call_and_write_node_t *cast = (const pm_call_and_write_node_t *) node;
+ pm_compile_call_and_or_write_node(iseq, true, cast->receiver, cast->value, cast->write_name, cast->read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
+ return;
+ }
+ case PM_CALL_OR_WRITE_NODE: {
+ // foo.bar ||= baz
+ // ^^^^^^^^^^^^^^^
+ const pm_call_or_write_node_t *cast = (const pm_call_or_write_node_t *) node;
+ pm_compile_call_and_or_write_node(iseq, false, cast->receiver, cast->value, cast->write_name, cast->read_name, PM_NODE_FLAG_P(cast, PM_CALL_NODE_FLAGS_SAFE_NAVIGATION), &location, ret, popped, scope_node);
return;
}
+ case PM_CALL_OPERATOR_WRITE_NODE:
+ // foo.bar += baz
+ // ^^^^^^^^^^^^^^^
+ //
+ // Call operator writes occur when you have a call node on the left-hand
+ // side of a write operator that is not `=`. As an example,
+ // `foo.bar *= 1`. This breaks down to caching the receiver on the
+ // stack and then performing three method calls, one to read the value,
+ // one to compute the result, and one to write the result back to the
+ // receiver.
+ pm_compile_call_operator_write_node(iseq, (const pm_call_operator_write_node_t *) node, &location, ret, popped, scope_node);
+ return;
+ case PM_CASE_NODE:
+ // case foo; when bar; end
+ // ^^^^^^^^^^^^^^^^^^^^^^^
+ pm_compile_case_node(iseq, (const pm_case_node_t *) node, &location, ret, popped, scope_node);
+ return;
+ case PM_CASE_MATCH_NODE:
+ // case foo; in bar; end
+ // ^^^^^^^^^^^^^^^^^^^^^
+ //
+ // If you use the `case` keyword to create a case match node, it will
+ // match against all of the `in` clauses until it finds one that
+ // matches. If it doesn't find one, it can optionally fall back to an
+ // `else` clause. If none is present and a match wasn't found, it will
+ // raise an appropriate error.
+ pm_compile_case_match_node(iseq, (const pm_case_match_node_t *) node, &location, ret, popped, scope_node);
+ return;
case PM_CLASS_NODE: {
// class Foo; end
// ^^^^^^^^^^^^^^
@@ -6579,7 +8949,10 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PUSH_INSN(ret, location, putnil);
}
- PUSH_INSN3(ret, location, defineclass, ID2SYM(class_id), class_iseq, INT2FIX(flags));
+ {
+ VALUE operand = ID2SYM(class_id);
+ PUSH_INSN3(ret, location, defineclass, operand, class_iseq, INT2FIX(flags));
+ }
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE)class_iseq);
if (popped) PUSH_INSN(ret, location, pop);
@@ -6686,14 +9059,12 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
if (ISEQ_COMPILE_DATA(iseq)->option->inline_const_cache && ((parts = pm_constant_path_parts(node, scope_node)) != Qnil)) {
ISEQ_BODY(iseq)->ic_size++;
+ RB_OBJ_SET_SHAREABLE(parts);
PUSH_INSN1(ret, location, opt_getconstant_path, parts);
}
else {
DECL_ANCHOR(prefix);
- INIT_ANCHOR(prefix);
-
DECL_ANCHOR(body);
- INIT_ANCHOR(body);
pm_compile_constant_path(iseq, node, prefix, body, popped, scope_node);
if (LIST_INSN_SIZE_ZERO(prefix)) {
@@ -6847,16 +9218,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
const pm_ensure_node_t *cast = (const pm_ensure_node_t *) node;
if (cast->statements != NULL) {
- LABEL *start = NEW_LABEL(location.line);
- LABEL *end = NEW_LABEL(location.line);
- PUSH_LABEL(ret, start);
-
- LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
- ISEQ_COMPILE_DATA(iseq)->end_label = end;
-
PM_COMPILE((const pm_node_t *) cast->statements);
- ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
- PUSH_LABEL(ret, end);
}
return;
@@ -6899,7 +9261,8 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// 1.0
// ^^^
if (!popped) {
- PUSH_INSN1(ret, location, putobject, parse_float((const pm_float_node_t *) node));
+ VALUE operand = parse_float((const pm_float_node_t *) node);
+ PUSH_INSN1(ret, location, putobject, operand);
}
return;
}
@@ -6936,159 +9299,17 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, child_iseq, retry_end_l);
return;
}
- case PM_FORWARDING_ARGUMENTS_NODE: {
+ case PM_FORWARDING_ARGUMENTS_NODE:
rb_bug("Cannot compile a ForwardingArgumentsNode directly\n");
return;
- }
- case PM_FORWARDING_SUPER_NODE: {
+ case PM_FORWARDING_SUPER_NODE:
// super
// ^^^^^
//
// super {}
// ^^^^^^^^
- const pm_forwarding_super_node_t *cast = (const pm_forwarding_super_node_t *) node;
- const rb_iseq_t *block = NULL;
-
- const rb_iseq_t *previous_block = NULL;
- LABEL *retry_label = NULL;
- LABEL *retry_end_l = NULL;
-
- if (cast->block != NULL) {
- previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
- ISEQ_COMPILE_DATA(iseq)->current_block = NULL;
-
- retry_label = NEW_LABEL(location.line);
- retry_end_l = NEW_LABEL(location.line);
-
- PUSH_LABEL(ret, retry_label);
- }
-
- PUSH_INSN(ret, location, putself);
- int flag = VM_CALL_ZSUPER | VM_CALL_SUPER | VM_CALL_FCALL;
-
- if (cast->block != NULL) {
- pm_scope_node_t next_scope_node;
- pm_scope_node_init((const pm_node_t *) cast->block, &next_scope_node, scope_node);
-
- ISEQ_COMPILE_DATA(iseq)->current_block = block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.line);
- pm_scope_node_destroy(&next_scope_node);
- RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) block);
- }
-
- 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 (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
- flag |= VM_CALL_FORWARDING;
- pm_local_index_t mult_local = pm_lookup_local_index(iseq, scope_node, PM_CONSTANT_DOT3, 0);
- PUSH_GETLOCAL(ret, location, mult_local.index, mult_local.level);
- PUSH_INSN2(ret, location, invokesuperforward, new_callinfo(iseq, 0, 0, flag, NULL, block != NULL), block);
- if (popped) PUSH_INSN(ret, location, pop);
- return;
- }
-
- 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;
- PUSH_GETLOCAL(args, location, 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);
- PUSH_GETLOCAL(args, location, 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;
- PUSH_GETLOCAL(args, location, idx, depth);
- PUSH_INSN1(args, location, 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);
- PUSH_GETLOCAL(args, location, idx, depth);
- }
-
- if (local_body->param.flags.has_rest) {
- // argc remains unchanged from rest branch
- PUSH_INSN1(args, location, newarray, INT2FIX(j));
- PUSH_INSN(args, location, 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++;
-
- PUSH_INSN1(args, location, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
-
- if (local_body->param.flags.has_kwrest) {
- int idx = local_body->local_table_size - local_keyword->rest_start;
- PUSH_GETLOCAL(args, location, idx, depth);
- RUBY_ASSERT(local_keyword->num > 0);
- PUSH_SEND(args, location, rb_intern("dup"), INT2FIX(0));
- }
- else {
- PUSH_INSN1(args, location, newhash, INT2FIX(0));
- }
- int i = 0;
- for (; i < local_keyword->num; ++i) {
- ID id = local_keyword->table[i];
- int idx = local_size - get_local_var_idx(local_iseq, id);
- PUSH_INSN1(args, location, putobject, ID2SYM(id));
- PUSH_GETLOCAL(args, location, idx, depth);
- }
-
- PUSH_SEND(args, location, id_core_hash_merge_ptr, INT2FIX(i * 2 + 1));
- flag |= VM_CALL_KW_SPLAT| VM_CALL_KW_SPLAT_MUT;
- }
- else if (local_body->param.flags.has_kwrest) {
- int idx = local_body->local_table_size - local_keyword->rest_start;
- PUSH_GETLOCAL(args, location, idx, depth);
- argc++;
- flag |= VM_CALL_KW_SPLAT;
- }
-
- PUSH_SEQ(ret, args);
- PUSH_INSN2(ret, location, invokesuper, new_callinfo(iseq, 0, argc, flag, NULL, block != NULL), block);
-
- if (cast->block != NULL) {
- pm_compile_retry_end_label(iseq, ret, retry_end_l);
- PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, block, retry_end_l);
- ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
- }
-
- if (popped) PUSH_INSN(ret, location, pop);
+ pm_compile_forwarding_super_node(iseq, (const pm_forwarding_super_node_t *) node, &location, ret, popped, scope_node);
return;
- }
case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
// $foo &&= bar
// ^^^^^^^^^^^^
@@ -7223,7 +9444,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
}
}
else {
- pm_compile_hash_elements(iseq, node, elements, false, ret, scope_node);
+ pm_compile_hash_elements(iseq, node, elements, 0, Qundef, false, ret, scope_node);
}
}
@@ -7239,14 +9460,15 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// foo ? bar : baz
// ^^^^^^^^^^^^^^^
const pm_if_node_t *cast = (const pm_if_node_t *) node;
- pm_compile_conditional(iseq, &location, PM_IF_NODE, (const pm_node_t *) cast, cast->statements, cast->consequent, cast->predicate, ret, popped, scope_node);
+ pm_compile_conditional(iseq, &location, PM_IF_NODE, (const pm_node_t *) cast, cast->statements, cast->subsequent, cast->predicate, ret, popped, scope_node);
return;
}
case PM_IMAGINARY_NODE: {
// 1i
// ^^
if (!popped) {
- PUSH_INSN1(ret, location, putobject, parse_imaginary((const pm_imaginary_node_t *) node));
+ VALUE operand = parse_imaginary((const pm_imaginary_node_t *) node);
+ PUSH_INSN1(ret, location, putobject, operand);
}
return;
}
@@ -7382,7 +9604,8 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// 1
// ^
if (!popped) {
- PUSH_INSN1(ret, location, putobject, parse_integer((const pm_integer_node_t *) node));
+ VALUE operand = parse_integer((const pm_integer_node_t *) node);
+ PUSH_INSN1(ret, location, putobject, operand);
}
return;
}
@@ -7416,7 +9639,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
pm_scope_node_t next_scope_node;
pm_scope_node_init(node, &next_scope_node, scope_node);
- block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, location.line);
+ block_iseq = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_PLAIN, location.line);
pm_scope_node_destroy(&next_scope_node);
ISEQ_COMPILE_DATA(iseq)->current_block = block_iseq;
@@ -7451,16 +9674,16 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PUSH_INSN1(ret, location, putobject, string);
}
else if (PM_NODE_FLAG_P(node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE)) {
- PUSH_INSN1(ret, location, putstring, string);
+ PUSH_INSN1(ret, location, dupstring, string);
}
else {
- PUSH_INSN1(ret, location, putchilledstring, string);
+ PUSH_INSN1(ret, location, dupchilledstring, string);
}
}
}
else {
const pm_interpolated_string_node_t *cast = (const pm_interpolated_string_node_t *) node;
- int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL);
+ int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL, PM_NODE_FLAG_P(cast, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE), PM_NODE_FLAG_P(cast, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN));
if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
if (popped) PUSH_INSN(ret, location, pop);
}
@@ -7471,7 +9694,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// :"foo #{bar}"
// ^^^^^^^^^^^^^
const pm_interpolated_symbol_node_t *cast = (const pm_interpolated_symbol_node_t *) node;
- int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL);
+ int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, popped, scope_node, NULL, NULL, false, false);
if (length > 1) {
PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
@@ -7493,7 +9716,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PUSH_INSN(ret, location, putself);
- int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, false, scope_node, NULL, NULL);
+ int length = pm_interpolated_node_compile(iseq, &cast->parts, &location, ret, false, scope_node, NULL, NULL, false, false);
if (length > 1) PUSH_INSN1(ret, location, concatstrings, INT2FIX(length));
PUSH_SEND_WITH_FLAG(ret, location, idBackquote, INT2NUM(1), INT2FIX(VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE));
@@ -7505,7 +9728,19 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// -> { it }
// ^^
if (!popped) {
- PUSH_GETLOCAL(ret, location, scope_node->local_table_for_iseq_size, 0);
+ pm_scope_node_t *current_scope_node = scope_node;
+ int level = 0;
+
+ while (current_scope_node) {
+ if (current_scope_node->parameters && PM_NODE_TYPE_P(current_scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
+ PUSH_GETLOCAL(ret, location, current_scope_node->local_table_for_iseq_size, level);
+ return;
+ }
+
+ current_scope_node = current_scope_node->previous;
+ level++;
+ }
+ rb_bug("Local `it` does not exist");
}
return;
@@ -7532,7 +9767,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
pm_scope_node_t next_scope_node;
pm_scope_node_init(node, &next_scope_node, scope_node);
- int opening_lineno = pm_location_line_number(parser, &cast->opening_loc);
+ int opening_lineno = pm_location_line_number_cached(&cast->opening_loc, scope_node);
const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, opening_lineno);
pm_scope_node_destroy(&next_scope_node);
@@ -7662,7 +9897,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
LABEL *matched_label = NEW_LABEL(location.line);
LABEL *unmatched_label = NEW_LABEL(location.line);
LABEL *done_label = NEW_LABEL(location.line);
- pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, false, false, true, 2);
+ pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, false, true, 2);
// If the pattern did not match, then compile the necessary instructions
// to handle pushing false onto the stack, then jump to the end.
@@ -7684,7 +9919,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PUSH_LABEL(ret, done_label);
return;
}
- case PM_MATCH_REQUIRED_NODE: {
+ case PM_MATCH_REQUIRED_NODE:
// foo => bar
// ^^^^^^^^^^
//
@@ -7696,52 +9931,9 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// This is somewhat analogous to compiling a case match statement with a
// single pattern. In both cases, if the pattern fails it should
// immediately raise an error.
- const pm_match_required_node_t *cast = (const pm_match_required_node_t *) node;
-
- LABEL *matched_label = NEW_LABEL(location.line);
- LABEL *unmatched_label = NEW_LABEL(location.line);
- LABEL *done_label = NEW_LABEL(location.line);
-
- // First, we're going to push a bunch of stuff onto the stack that is
- // going to serve as our scratch space.
- PUSH_INSN(ret, location, putnil); // key error key
- PUSH_INSN(ret, location, putnil); // key error matchee
- PUSH_INSN1(ret, location, putobject, Qfalse); // key error?
- PUSH_INSN(ret, location, putnil); // error string
- PUSH_INSN(ret, location, putnil); // deconstruct cache
-
- // Next we're going to compile the value expression such that it's on
- // the stack.
- PM_COMPILE_NOT_POPPED(cast->value);
-
- // Here we'll dup it so that it can be used for comparison, but also be
- // used for error handling.
- PUSH_INSN(ret, location, dup);
-
- // Next we'll compile the pattern. We indicate to the pm_compile_pattern
- // function that this is the only pattern that will be matched against
- // through the in_single_pattern parameter. We also indicate that the
- // value to compare against is 2 slots from the top of the stack (the
- // base_index parameter).
- pm_compile_pattern(iseq, scope_node, cast->pattern, ret, matched_label, unmatched_label, true, false, true, 2);
-
- // If the pattern did not match the value, then we're going to compile
- // in our error handler code. This will determine which error to raise
- // and raise it.
- PUSH_LABEL(ret, unmatched_label);
- pm_compile_pattern_error_handler(iseq, scope_node, node, ret, done_label, popped);
-
- // If the pattern did match, we'll clean up the values we've pushed onto
- // the stack and then push nil onto the stack if it's not popped.
- PUSH_LABEL(ret, matched_label);
- PUSH_INSN1(ret, location, adjuststack, INT2FIX(6));
- if (!popped) PUSH_INSN(ret, location, putnil);
- PUSH_INSNL(ret, location, jump, done_label);
-
- PUSH_LABEL(ret, done_label);
+ pm_compile_match_required_node(iseq, (const pm_match_required_node_t *) node, &location, ret, popped, scope_node);
return;
- }
- case PM_MATCH_WRITE_NODE: {
+ case PM_MATCH_WRITE_NODE:
// /(?<foo>foo)/ =~ bar
// ^^^^^^^^^^^^^^^^^^^^
//
@@ -7750,81 +9942,11 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// 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.
- const pm_match_write_node_t *cast = (const pm_match_write_node_t *) node;
- LABEL *fail_label = NEW_LABEL(location.line);
- LABEL *end_label = NEW_LABEL(location.line);
-
- // 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((const pm_node_t *) cast->call);
-
- // 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.
- PUSH_INSN1(ret, location, getglobal, rb_id2sym(idBACKREF));
- PUSH_INSN(ret, location, dup);
- PUSH_INSNL(ret, location, branchunless, fail_label);
-
- // 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;
-
- if (targets_count == 1) {
- const pm_node_t *target = cast->targets.nodes[0];
- RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
-
- const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
- pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
-
- PUSH_INSN1(ret, location, putobject, rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name)));
- PUSH_SEND(ret, location, idAREF, INT2FIX(1));
- PUSH_LABEL(ret, fail_label);
- PUSH_SETLOCAL(ret, location, index.index, index.level);
- if (popped) PUSH_INSN(ret, location, pop);
- return;
- }
-
- DECL_ANCHOR(fail_anchor);
- INIT_ANCHOR(fail_anchor);
-
- // 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++) {
- const pm_node_t *target = cast->targets.nodes[targets_index];
- RUBY_ASSERT(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
-
- const pm_local_variable_target_node_t *local_target = (const pm_local_variable_target_node_t *) target;
- pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
-
- if (((size_t) targets_index) < (targets_count - 1)) {
- PUSH_INSN(ret, location, dup);
- }
- PUSH_INSN1(ret, location, putobject, rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name)));
- PUSH_SEND(ret, location, idAREF, INT2FIX(1));
- PUSH_SETLOCAL(ret, location, index.index, index.level);
-
- PUSH_INSN(fail_anchor, location, putnil);
- PUSH_SETLOCAL(fail_anchor, location, index.index, index.level);
- }
-
- // Since we matched successfully, now we'll jump to the end.
- PUSH_INSNL(ret, location, jump, end_label);
-
- // In the case that the match failed, we'll loop through each local
- // variable target and set all of them to `nil`.
- PUSH_LABEL(ret, fail_label);
- PUSH_INSN(ret, location, pop);
- PUSH_SEQ(ret, fail_anchor);
-
- // Finally, we can push the end label for either case.
- PUSH_LABEL(ret, end_label);
- if (popped) PUSH_INSN(ret, location, pop);
+ pm_compile_match_write_node(iseq, (const pm_match_write_node_t *) node, &location, ret, popped, scope_node);
return;
- }
- case PM_MISSING_NODE: {
- rb_bug("A pm_missing_node_t should not exist in prism's AST.");
+ case PM_ERROR_RECOVERY_NODE:
+ rb_bug("A pm_error_recovery_node_t should not exist in prism's AST.");
return;
- }
case PM_MODULE_NODE: {
// module Foo; end
// ^^^^^^^^^^^^^^^
@@ -7868,10 +9990,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
const pm_multi_write_node_t *cast = (const pm_multi_write_node_t *) node;
DECL_ANCHOR(writes);
- INIT_ANCHOR(writes);
-
DECL_ANCHOR(cleanup);
- INIT_ANCHOR(cleanup);
pm_multi_target_state_t state = { 0 };
state.position = popped ? 0 : 1;
@@ -7894,96 +10013,14 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PUSH_SEQ(ret, cleanup);
return;
}
- case PM_NEXT_NODE: {
+ case PM_NEXT_NODE:
// next
// ^^^^
//
// next foo
// ^^^^^^^^
- const pm_next_node_t *cast = (const pm_next_node_t *) node;
-
- if (ISEQ_COMPILE_DATA(iseq)->redo_label != 0 && can_add_ensure_iseq(iseq)) {
- LABEL *splabel = NEW_LABEL(0);
- PUSH_LABEL(ret, splabel);
-
- if (cast->arguments) {
- PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
- }
- else {
- PUSH_INSN(ret, location, putnil);
- }
- pm_add_ensure_iseq(ret, iseq, 0, scope_node);
-
- PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->redo_label);
- PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
-
- PUSH_ADJUST_RESTORE(ret, splabel);
- if (!popped) PUSH_INSN(ret, location, putnil);
- }
- else if (ISEQ_COMPILE_DATA(iseq)->end_label && can_add_ensure_iseq(iseq)) {
- LABEL *splabel = NEW_LABEL(0);
-
- PUSH_LABEL(ret, splabel);
- PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->start_label);
-
- if (cast->arguments != NULL) {
- PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
- }
- else {
- PUSH_INSN(ret, location, putnil);
- }
-
- pm_add_ensure_iseq(ret, iseq, 0, scope_node);
- PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->end_label);
- PUSH_ADJUST_RESTORE(ret, splabel);
- splabel->unremovable = FALSE;
-
- if (!popped) PUSH_INSN(ret, location, putnil);
- }
- else {
- const rb_iseq_t *ip = iseq;
- 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) {
- COMPILE_ERROR(iseq, location.line, "Can't escape from eval with next");
- return;
- }
-
- ip = ISEQ_BODY(ip)->parent_iseq;
- }
- if (ip != 0) {
- if (cast->arguments) {
- PM_COMPILE_NOT_POPPED((const pm_node_t *) cast->arguments);
- }
- else {
- PUSH_INSN(ret, location, putnil);
- }
-
- PUSH_INSN1(ret, location, throw, INT2FIX(throw_flag | TAG_NEXT));
- if (popped) PUSH_INSN(ret, location, pop);
- }
- else {
- COMPILE_ERROR(iseq, location.line, "Invalid next");
- return;
- }
- }
-
+ pm_compile_next_node(iseq, (const pm_next_node_t *) node, &location, ret, popped, scope_node);
return;
- }
case PM_NIL_NODE: {
// nil
// ^^^
@@ -7993,6 +10030,12 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
return;
}
+ case PM_NO_BLOCK_PARAMETER_NODE: {
+ // def foo(&nil); end
+ // ^^^^
+ ISEQ_BODY(iseq)->param.flags.accepts_no_block = TRUE;
+ return;
+ }
case PM_NO_KEYWORDS_PARAMETER_NODE: {
// def foo(**nil); end
// ^^^^^
@@ -8003,10 +10046,11 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// $1
// ^^
if (!popped) {
- uint32_t reference_number = ((const pm_numbered_reference_read_node_t *) node)->number;
+ const pm_numbered_reference_read_node_t *cast = (const pm_numbered_reference_read_node_t *) node;
- if (reference_number > 0) {
- PUSH_INSN2(ret, location, getspecial, INT2FIX(1), INT2FIX(reference_number << 1));
+ if (cast->number != 0) {
+ VALUE ref = pm_compile_numbered_reference_ref(cast);
+ PUSH_INSN2(ret, location, getspecial, INT2FIX(1), ref);
}
else {
PUSH_INSN(ret, location, putnil);
@@ -8073,11 +10117,9 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// anchors and then join them in the correct order into the resulting
// anchor.
DECL_ANCHOR(inner_pre);
- INIT_ANCHOR(inner_pre);
scope_node->pre_execution_anchor = inner_pre;
DECL_ANCHOR(inner_body);
- INIT_ANCHOR(inner_body);
if (cast->statements != NULL) {
const pm_node_list_t *body = &cast->statements->body;
@@ -8139,23 +10181,24 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
exclude_end
);
+ RB_OBJ_SET_SHAREABLE(val);
PUSH_INSN1(ret, location, putobject, val);
}
}
else {
- if (cast->left == NULL) {
- PUSH_INSN(ret, location, putnil);
- }
- else {
+ if (cast->left != NULL) {
PM_COMPILE(cast->left);
}
-
- if (cast->right == NULL) {
+ else if (!popped) {
PUSH_INSN(ret, location, putnil);
}
- else {
+
+ if (cast->right != NULL) {
PM_COMPILE(cast->right);
}
+ else if (!popped) {
+ PUSH_INSN(ret, location, putnil);
+ }
if (!popped) {
PUSH_INSN1(ret, location, newrange, INT2FIX(exclude_end ? 1 : 0));
@@ -8171,66 +10214,11 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
}
return;
}
- case PM_REDO_NODE: {
+ case PM_REDO_NODE:
// redo
// ^^^^
- if (ISEQ_COMPILE_DATA(iseq)->redo_label && can_add_ensure_iseq(iseq)) {
- LABEL *splabel = NEW_LABEL(0);
-
- PUSH_LABEL(ret, splabel);
- PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->redo_label);
- pm_add_ensure_iseq(ret, iseq, 0, scope_node);
-
- PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->redo_label);
- PUSH_ADJUST_RESTORE(ret, splabel);
- if (!popped) PUSH_INSN(ret, location, putnil);
- }
- 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);
-
- PUSH_LABEL(ret, splabel);
- pm_add_ensure_iseq(ret, iseq, 0, scope_node);
- PUSH_ADJUST(ret, location, ISEQ_COMPILE_DATA(iseq)->start_label);
-
- PUSH_INSNL(ret, location, jump, ISEQ_COMPILE_DATA(iseq)->start_label);
- PUSH_ADJUST_RESTORE(ret, splabel);
- if (!popped) PUSH_INSN(ret, location, putnil);
- }
- 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) {
- COMPILE_ERROR(iseq, location.line, "Can't escape from eval with redo");
- return;
- }
-
- ip = ISEQ_BODY(ip)->parent_iseq;
- }
-
- if (ip != 0) {
- PUSH_INSN(ret, location, putnil);
- PUSH_INSN1(ret, location, throw, INT2FIX(VM_THROW_NO_ESCAPE_FLAG | TAG_REDO));
- if (popped) PUSH_INSN(ret, location, pop);
- }
- else {
- COMPILE_ERROR(iseq, location.line, "Invalid redo");
- return;
- }
- }
+ pm_compile_redo_node(iseq, &location, ret, popped, scope_node);
return;
- }
case PM_REGULAR_EXPRESSION_NODE: {
// /foo/
// ^^^^^
@@ -8240,106 +10228,11 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
}
return;
}
- case PM_RESCUE_NODE: {
+ case PM_RESCUE_NODE:
// begin; rescue; end
// ^^^^^^^
- const pm_rescue_node_t *cast = (const pm_rescue_node_t *) node;
- iseq_set_exception_local_table(iseq);
-
- // First, establish the labels that we need to be able to jump to within
- // this compilation block.
- LABEL *exception_match_label = NEW_LABEL(location.line);
- LABEL *rescue_end_label = NEW_LABEL(location.line);
-
- // Next, compile each of the exceptions that we're going to be
- // handling. For each one, we'll add instructions to check if the
- // exception matches the raised one, and if it does then jump to the
- // exception_match_label label. Otherwise it will fall through to the
- // subsequent check. If there are no exceptions, we'll only check
- // StandardError.
- const pm_node_list_t *exceptions = &cast->exceptions;
-
- if (exceptions->size > 0) {
- for (size_t index = 0; index < exceptions->size; index++) {
- PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
- PM_COMPILE(exceptions->nodes[index]);
- int checkmatch_flags = VM_CHECKMATCH_TYPE_RESCUE;
- if (PM_NODE_TYPE_P(exceptions->nodes[index], PM_SPLAT_NODE)) {
- checkmatch_flags |= VM_CHECKMATCH_ARRAY;
- }
- PUSH_INSN1(ret, location, checkmatch, INT2FIX(checkmatch_flags));
- PUSH_INSNL(ret, location, branchif, exception_match_label);
- }
- }
- else {
- PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
- PUSH_INSN1(ret, location, putobject, rb_eStandardError);
- PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
- PUSH_INSNL(ret, location, branchif, exception_match_label);
- }
-
- // If none of the exceptions that we are matching against matched, then
- // we'll jump straight to the rescue_end_label label.
- PUSH_INSNL(ret, location, jump, rescue_end_label);
-
- // Here we have the exception_match_label, which is where the
- // control-flow goes in the case that one of the exceptions matched.
- // Here we will compile the instructions to handle the exception.
- PUSH_LABEL(ret, exception_match_label);
- PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
-
- // If we have a reference to the exception, then we'll compile the write
- // into the instruction sequence. This can look quite different
- // depending on the kind of write being performed.
- if (cast->reference) {
- DECL_ANCHOR(writes);
- INIT_ANCHOR(writes);
-
- DECL_ANCHOR(cleanup);
- INIT_ANCHOR(cleanup);
-
- pm_compile_target_node(iseq, cast->reference, ret, writes, cleanup, scope_node, NULL);
- PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
-
- PUSH_SEQ(ret, writes);
- PUSH_SEQ(ret, cleanup);
- }
-
- // If we have statements to execute, we'll compile them here. Otherwise
- // we'll push nil onto the stack.
- if (cast->statements) {
- // We'll temporarily remove the end_label location from the iseq
- // when compiling the statements so that next/redo statements
- // inside the body will throw to the correct place instead of
- // jumping straight to the end of this iseq
- LABEL *prev_end = ISEQ_COMPILE_DATA(iseq)->end_label;
- ISEQ_COMPILE_DATA(iseq)->end_label = NULL;
-
- PM_COMPILE((const pm_node_t *) cast->statements);
-
- // Now restore the end_label
- ISEQ_COMPILE_DATA(iseq)->end_label = prev_end;
- }
- else {
- PUSH_INSN(ret, location, putnil);
- }
-
- PUSH_INSN(ret, location, leave);
-
- // Here we'll insert the rescue_end_label label, which is jumped to if
- // none of the exceptions matched. It will cause the control-flow to
- // either jump to the next rescue clause or it will fall through to the
- // subsequent instruction returning the raised error.
- PUSH_LABEL(ret, rescue_end_label);
- if (cast->consequent) {
- PM_COMPILE((const pm_node_t *) cast->consequent);
- }
- else {
- PUSH_GETLOCAL(ret, location, 1, 0);
- }
-
+ pm_compile_rescue_node(iseq, (const pm_rescue_node_t *) node, &location, ret, popped, scope_node);
return;
- }
case PM_RESCUE_MODIFIER_NODE: {
// foo rescue bar
// ^^^^^^^^^^^^^^
@@ -8352,7 +10245,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
&rescue_scope_node,
rb_str_concat(rb_str_new2("rescue in "), ISEQ_BODY(iseq)->location.label),
ISEQ_TYPE_RESCUE,
- pm_node_line_number(parser, cast->rescue_expression)
+ pm_node_line_number_cached(cast->rescue_expression, scope_node)
);
pm_scope_node_destroy(&rescue_scope_node);
@@ -8376,66 +10269,14 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PUSH_CATCH_ENTRY(CATCH_TYPE_RETRY, lend, lcont, NULL, lstart);
return;
}
- case PM_RETURN_NODE: {
+ case PM_RETURN_NODE:
// return
// ^^^^^^
//
// return 1
// ^^^^^^^^
- const pm_return_node_t *cast = (const pm_return_node_t *) node;
- const pm_arguments_node_t *arguments = cast->arguments;
-
- 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;
- }
-
- switch (parent_type) {
- case ISEQ_TYPE_TOP:
- case ISEQ_TYPE_MAIN:
- if (arguments) {
- rb_warn("argument of top-level return is ignored");
- }
- if (parent_iseq == iseq) {
- type = ISEQ_TYPE_METHOD;
- }
- break;
- default:
- break;
- }
-
- if (type == ISEQ_TYPE_METHOD) {
- splabel = NEW_LABEL(0);
- PUSH_LABEL(ret, splabel);
- PUSH_ADJUST(ret, location, 0);
- }
-
- if (arguments) {
- PM_COMPILE_NOT_POPPED((const pm_node_t *) arguments);
- }
- else {
- PUSH_INSN(ret, location, putnil);
- }
-
- if (type == ISEQ_TYPE_METHOD && can_add_ensure_iseq(iseq)) {
- pm_add_ensure_iseq(ret, iseq, 1, scope_node);
- PUSH_TRACE(ret, RUBY_EVENT_RETURN);
- PUSH_INSN(ret, location, leave);
- PUSH_ADJUST_RESTORE(ret, splabel);
- if (!popped) PUSH_INSN(ret, location, putnil);
- }
- else {
- PUSH_INSN1(ret, location, throw, INT2FIX(TAG_RETURN));
- if (popped) PUSH_INSN(ret, location, pop);
- }
-
+ pm_compile_return_node(iseq, (const pm_return_node_t *) node, &location, ret, popped, scope_node);
return;
- }
case PM_RETRY_NODE: {
// retry
// ^^^^^
@@ -8450,919 +10291,9 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
}
return;
}
- case PM_SCOPE_NODE: {
- pm_scope_node_t *scope_node = (pm_scope_node_t *) node;
- pm_constant_id_list_t *locals = &scope_node->locals;
-
- pm_parameters_node_t *parameters_node = NULL;
- pm_node_list_t *keywords_list = NULL;
- pm_node_list_t *optionals_list = NULL;
- pm_node_list_t *posts_list = NULL;
- pm_node_list_t *requireds_list = NULL;
- pm_node_list_t *block_locals = NULL;
- bool trailing_comma = false;
-
- struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
-
- if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
- ADD_TRACE(ret, RUBY_EVENT_CLASS);
- }
-
- if (scope_node->parameters) {
- switch (PM_NODE_TYPE(scope_node->parameters)) {
- case PM_BLOCK_PARAMETERS_NODE: {
- pm_block_parameters_node_t *cast = (pm_block_parameters_node_t *) scope_node->parameters;
- parameters_node = cast->parameters;
- block_locals = &cast->locals;
-
- if (parameters_node) {
- if (parameters_node->rest && PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE)) {
- trailing_comma = true;
- }
- }
- break;
- }
- case PM_PARAMETERS_NODE: {
- parameters_node = (pm_parameters_node_t *) scope_node->parameters;
- break;
- }
- case PM_NUMBERED_PARAMETERS_NODE: {
- uint32_t maximum = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
- body->param.lead_num = maximum;
- body->param.flags.ambiguous_param0 = maximum == 1;
- break;
- }
- case PM_IT_PARAMETERS_NODE:
- body->param.lead_num = 1;
- body->param.flags.ambiguous_param0 = true;
- 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;
-
- if (parameters_node) {
- optionals_list = &parameters_node->optionals;
- requireds_list = &parameters_node->requireds;
- keywords_list = &parameters_node->keywords;
- posts_list = &parameters_node->posts;
- }
- else if (scope_node->parameters && (PM_NODE_TYPE_P(scope_node->parameters, PM_NUMBERED_PARAMETERS_NODE) || PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE))) {
- body->param.opt_num = 0;
- }
- else {
- body->param.lead_num = 0;
- body->param.opt_num = 0;
- }
-
- //********STEP 1**********
- // Goal: calculate the table size for the locals, accounting for
- // hidden variables and multi target nodes
- size_t locals_size = locals->size;
-
- // Index lookup table buffer size is only the number of the locals
- st_table *index_lookup_table = st_init_numtable();
-
- int table_size = (int) locals_size;
-
- // For nodes have a hidden iteration variable. We add that to the local
- // table size here.
- if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) table_size++;
-
- if (keywords_list && keywords_list->size) {
- table_size++;
- }
-
- 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++;
- }
- else if (PM_NODE_TYPE_P(required, PM_REQUIRED_PARAMETER_NODE)) {
- if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- table_size++;
- }
- }
- }
- }
-
- // If we have the `it` implicit local variable, we need to account for
- // it in the local table size.
- if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
- table_size++;
- }
-
- // Ensure there is enough room in the local table for any
- // parameters that have been repeated
- // ex: def underscore_parameters(_, _ = 1, _ = 2); _; end
- // ^^^^^^^^^^^^
- if (optionals_list && optionals_list->size) {
- for (size_t i = 0; i < optionals_list->size; i++) {
- pm_node_t * node = optionals_list->nodes[i];
- if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- table_size++;
- }
- }
- }
-
- // If we have an anonymous "rest" node, we'll need to increase the local
- // table size to take it in to account.
- // def m(foo, *, bar)
- // ^
- if (parameters_node) {
- if (parameters_node->rest) {
- if (!(PM_NODE_TYPE_P(parameters_node->rest, PM_IMPLICIT_REST_NODE))) {
- if (!((const pm_rest_parameter_node_t *) parameters_node->rest)->name || PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- table_size++;
- }
- }
- }
-
- // def foo(_, **_); _; end
- // ^^^
- if (parameters_node->keyword_rest) {
- // def foo(...); end
- // ^^^
- // When we have a `...` as the keyword_rest, it's a forwarding_parameter_node and
- // we need to leave space for 4 locals: *, **, &, ...
- if (PM_NODE_TYPE_P(parameters_node->keyword_rest, PM_FORWARDING_PARAMETER_NODE)) {
- // Only optimize specifically methods like this: `foo(...)`
- if (requireds_list->size == 0 && optionals_list->size == 0 && keywords_list->size == 0) {
- ISEQ_BODY(iseq)->param.flags.forwardable = TRUE;
- table_size += 1;
- }
- else {
- table_size += 4;
- }
- }
- else {
- const pm_keyword_rest_parameter_node_t *kw_rest = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
-
- // If it's anonymous or repeated, then we need to allocate stack space
- if (!kw_rest->name || PM_NODE_FLAG_P(kw_rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- table_size++;
- }
- }
- }
- }
-
- 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) || PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- table_size++;
- }
- }
- }
-
- if (keywords_list && keywords_list->size) {
- for (size_t i = 0; i < keywords_list->size; i++) {
- pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
- if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- table_size++;
- }
- }
- }
-
- if (parameters_node && parameters_node->block) {
- const pm_block_parameter_node_t *block_node = (const pm_block_parameter_node_t *) parameters_node->block;
-
- if (PM_NODE_FLAG_P(block_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER) || !block_node->name) {
- table_size++;
- }
- }
-
- // 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;
-
- //********END OF STEP 1**********
-
- //********STEP 2**********
- // Goal: populate iv index table as well as local table, keeping the
- // layout of the local table consistent with the layout of the
- // stack when calling the method
- //
- // Do a first pass on all of the parameters, setting their values in
- // the local_table_for_iseq, _except_ for Multis who get a hidden
- // variable in this step, and will get their names inserted in step 3
-
- // local_index is a cursor that keeps track of the current
- // index into local_table_for_iseq. The local table is actually a list,
- // and the order of that list must match the order of the items pushed
- // on the stack. We need to take in to account things pushed on the
- // stack that _might not have a name_ (for example array destructuring).
- // This index helps us know which item we're dealing with and also give
- // those anonymous items temporary names (as below)
- int local_index = 0;
-
- // Here we figure out local table indices and insert them in to the
- // index lookup table and local tables.
- //
- // 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: {
- 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: {
- const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) required;
-
- if (PM_NODE_FLAG_P(required, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- ID local = pm_constant_id_lookup(scope_node, param->name);
- local_table_for_iseq->ids[local_index] = local;
- }
- else {
- pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
-
- break;
- }
- default: {
- rb_bug("Unsupported node in requireds in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
- }
- }
- }
-
- body->param.lead_num = (int) requireds_list->size;
- body->param.flags.has_lead = true;
- }
-
- if (scope_node->parameters != NULL && PM_NODE_TYPE_P(scope_node->parameters, PM_IT_PARAMETERS_NODE)) {
- ID local = rb_make_temporary_id(local_index);
- local_table_for_iseq->ids[local_index++] = local;
- }
-
- // 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_node_t * node = optionals_list->nodes[i];
- pm_constant_id_t name = ((const pm_optional_parameter_node_t *) node)->name;
-
- if (PM_NODE_FLAG_P(node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- ID local = pm_constant_id_lookup(scope_node, name);
- local_table_for_iseq->ids[local_index] = local;
- }
- else {
- pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
- }
- }
-
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^
- if (parameters_node && parameters_node->rest) {
- body->param.rest_start = local_index;
-
- // 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;
- RUBY_ASSERT(body->param.rest_start != -1);
-
- pm_constant_id_t name = ((const pm_rest_parameter_node_t *) parameters_node->rest)->name;
-
- if (name) {
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^
- if (PM_NODE_FLAG_P(parameters_node->rest, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- ID local = pm_constant_id_lookup(scope_node, name);
- local_table_for_iseq->ids[local_index] = local;
- }
- else {
- pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
- }
- else {
- // def foo(a, (b, *c, d), e = 1, *, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^
- body->param.flags.anon_rest = true;
- pm_insert_local_special(idMULT, local_index, index_lookup_table, local_table_for_iseq);
- }
-
- local_index++;
- }
- }
-
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^^^^^^^^^^^^
- if (posts_list && posts_list->size) {
- body->param.post_num = (int) posts_list->size;
- body->param.post_start = local_index;
- body->param.flags.has_post = true;
-
- 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.
- const 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: {
- 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: {
- const pm_required_parameter_node_t *param = (const pm_required_parameter_node_t *) post_node;
-
- if (PM_NODE_FLAG_P(param, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- ID local = pm_constant_id_lookup(scope_node, param->name);
- local_table_for_iseq->ids[local_index] = local;
- }
- else {
- pm_insert_local_index(param->name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
- break;
- }
- default: {
- rb_bug("Unsupported node in posts in parameters %s", pm_node_type_to_str(PM_NODE_TYPE(node)));
- }
- }
- }
- }
-
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^^^^^^^
- // 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;
-
- body->param.flags.has_kw = true;
- const VALUE default_values = rb_ary_hidden_new(1);
- const VALUE complex_mark = rb_str_tmp_new(0);
-
- ID *ids = xcalloc(keywords_list->size, sizeof(ID));
-
- size_t kw_index = 0;
-
- for (size_t i = 0; i < keywords_list->size; i++) {
- pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
- pm_constant_id_t name;
-
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^
- if (PM_NODE_TYPE_P(keyword_parameter_node, PM_REQUIRED_KEYWORD_PARAMETER_NODE)) {
- name = ((const pm_required_keyword_parameter_node_t *) keyword_parameter_node)->name;
- keyword->required_num++;
- ID local = pm_constant_id_lookup(scope_node, name);
-
- if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- local_table_for_iseq->ids[local_index] = local;
- }
- else {
- pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
- local_index++;
- ids[kw_index++] = local;
- }
- }
-
- for (size_t i = 0; i < keywords_list->size; i++) {
- pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
- pm_constant_id_t name;
-
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^^^
- if (PM_NODE_TYPE_P(keyword_parameter_node, PM_OPTIONAL_KEYWORD_PARAMETER_NODE)) {
- const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
-
- pm_node_t *value = cast->value;
- name = cast->name;
-
- if (PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) && !(PM_NODE_TYPE_P(value, PM_ARRAY_NODE) || PM_NODE_TYPE_P(value, PM_HASH_NODE) || PM_NODE_TYPE_P(value, PM_RANGE_NODE))) {
- rb_ary_push(default_values, pm_static_literal_value(iseq, value, scope_node));
- }
- else {
- rb_ary_push(default_values, complex_mark);
- }
-
- ID local = pm_constant_id_lookup(scope_node, name);
- if (PM_NODE_FLAG_P(keyword_parameter_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- local_table_for_iseq->ids[local_index] = local;
- }
- else {
- pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
- ids[kw_index++] = local;
- local_index++;
- }
-
- }
-
- keyword->bits_start = local_index;
- keyword->table = ids;
-
- 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;
-
- // 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 && !trailing_comma) {
- body->param.flags.ambiguous_param0 = true;
- }
-
- if (parameters_node) {
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^^
- if (parameters_node->keyword_rest) {
- 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: {
- 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: {
- const pm_keyword_rest_parameter_node_t *kw_rest_node = (const pm_keyword_rest_parameter_node_t *) parameters_node->keyword_rest;
- if (!body->param.flags.has_kw) {
- body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
- }
-
- keyword->rest_start = local_index;
- body->param.flags.has_kwrest = true;
-
- pm_constant_id_t constant_id = kw_rest_node->name;
- if (constant_id) {
- if (PM_NODE_FLAG_P(kw_rest_node, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- ID local = pm_constant_id_lookup(scope_node, constant_id);
- local_table_for_iseq->ids[local_index] = local;
- }
- else {
- pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
- }
- else {
- body->param.flags.anon_kwrest = true;
- pm_insert_local_special(idPow, local_index, index_lookup_table, local_table_for_iseq);
- }
-
- local_index++;
- break;
- }
- // def foo(...)
- // ^^^
- case PM_FORWARDING_PARAMETER_NODE: {
- if (!ISEQ_BODY(iseq)->param.flags.forwardable) {
- // Add the anonymous *
- body->param.rest_start = local_index;
- body->param.flags.has_rest = true;
- body->param.flags.anon_rest = true;
- pm_insert_local_special(idMULT, local_index++, index_lookup_table, local_table_for_iseq);
-
- // Add the anonymous **
- RUBY_ASSERT(!body->param.flags.has_kw);
- body->param.flags.has_kw = false;
- body->param.flags.has_kwrest = true;
- body->param.flags.anon_kwrest = true;
- body->param.keyword = keyword = ZALLOC_N(struct rb_iseq_param_keyword, 1);
- keyword->rest_start = local_index;
- pm_insert_local_special(idPow, local_index++, index_lookup_table, local_table_for_iseq);
-
- // Add the anonymous &
- body->param.block_start = local_index;
- body->param.flags.has_block = true;
- pm_insert_local_special(idAnd, local_index++, index_lookup_table, local_table_for_iseq);
- }
-
- // Add the ...
- pm_insert_local_special(idDot3, local_index++, index_lookup_table, local_table_for_iseq);
- break;
- }
- default: {
- rb_bug("node type %s not expected as keyword_rest", pm_node_type_to_str(PM_NODE_TYPE(parameters_node->keyword_rest)));
- }
- }
- }
-
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^
- if (parameters_node->block) {
- body->param.block_start = local_index;
- body->param.flags.has_block = true;
-
- pm_constant_id_t name = ((const pm_block_parameter_node_t *) parameters_node->block)->name;
-
- if (name) {
- if (PM_NODE_FLAG_P(parameters_node->block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
- ID local = pm_constant_id_lookup(scope_node, name);
- local_table_for_iseq->ids[local_index] = local;
- }
- else {
- pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
- }
- else {
- pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
- }
-
- local_index++;
- }
- }
-
- //********END OF STEP 2**********
- // The local table is now consistent with expected
- // stack layout
-
- // If there's only one required element in the parameters
- // CRuby needs to recognize it as an ambiguous parameter
-
- //********STEP 3**********
- // Goal: fill in the names of the parameters in MultiTargetNodes
- //
- // Go through requireds again to set the multis
-
- 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
- const pm_node_t *required = requireds_list->nodes[i];
-
- if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
- local_index = pm_compile_destructured_param_locals((const pm_multi_target_node_t *) required, index_lookup_table, local_table_for_iseq, scope_node, local_index);
- }
- }
- }
-
- // 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
- const pm_node_t *post = posts_list->nodes[i];
-
- if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
- local_index = pm_compile_destructured_param_locals((const pm_multi_target_node_t *) post, index_lookup_table, local_table_for_iseq, scope_node, local_index);
- }
- }
- }
-
- // Set any anonymous locals for the for node
- if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
- if (PM_NODE_TYPE_P(((const pm_for_node_t *) scope_node->ast_node)->index, PM_LOCAL_VARIABLE_TARGET_NODE)) {
- body->param.lead_num++;
- }
- else {
- body->param.rest_start = local_index;
- body->param.flags.has_rest = true;
- }
-
- 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 = ((const pm_numbered_parameters_node_t *) scope_node->parameters)->maximum;
- RUBY_ASSERT(0 < maximum && maximum <= 9);
- for (int i = 0; i < maximum; i++, local_index++) {
- const uint8_t param_name[] = { '_', '1' + i };
- pm_constant_id_t constant_id = pm_constant_pool_find(&parser->constant_pool, param_name, 2);
- RUBY_ASSERT(constant_id && "parser should fill in any gaps in numbered parameters");
- pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
- body->param.lead_num = maximum;
- body->param.flags.has_lead = true;
- }
-
- //********END OF STEP 3**********
-
- //********STEP 4**********
- // Goal: fill in the method body locals
- // To be explicit, these are the non-parameter locals
- // 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 = ((const pm_block_local_variable_node_t *) block_locals->nodes[i])->name;
- pm_insert_local_index(constant_id, local_index, index_lookup_table, local_table_for_iseq, scope_node);
- }
- }
-
- // Fill in any locals we missed
- if (scope_node->locals.size) {
- for (size_t i = 0; i < scope_node->locals.size; i++) {
- pm_constant_id_t constant_id = locals->ids[i];
- if (constant_id) {
- struct pm_local_table_insert_ctx ctx;
- ctx.scope_node = scope_node;
- ctx.local_table_for_iseq = local_table_for_iseq;
- ctx.local_index = local_index;
-
- st_update(index_lookup_table, (st_data_t)constant_id, pm_local_table_insert_func, (st_data_t)&ctx);
-
- local_index = ctx.local_index;
- }
- }
- }
-
- //********END OF STEP 4**********
-
- // We set the index_lookup_table on the scope node so we can
- // refer to the parameters correctly
- if (scope_node->index_lookup_table) {
- st_free_table(scope_node->index_lookup_table);
- }
- scope_node->index_lookup_table = index_lookup_table;
- iseq_calc_param_size(iseq);
-
- if (ISEQ_BODY(iseq)->param.flags.forwardable) {
- // We're treating `...` as a parameter so that frame
- // pushing won't clobber it.
- ISEQ_BODY(iseq)->param.size += 1;
- }
-
- // FIXME: args?
- iseq_set_local_table(iseq, local_table_for_iseq, 0);
- 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++) {
- label = NEW_LABEL(lineno);
- opt_table[i] = label;
- PUSH_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;
- PUSH_LABEL(ret, label);
-
- body->param.opt_table = (const VALUE *) opt_table;
- }
-
- if (keywords_list && keywords_list->size) {
- size_t optional_index = 0;
- for (size_t i = 0; i < keywords_list->size; i++) {
- pm_node_t *keyword_parameter_node = keywords_list->nodes[i];
- pm_constant_id_t name;
-
- switch (PM_NODE_TYPE(keyword_parameter_node)) {
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^^^
- case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
- const pm_optional_keyword_parameter_node_t *cast = ((const pm_optional_keyword_parameter_node_t *) keyword_parameter_node);
-
- pm_node_t *value = cast->value;
- name = cast->name;
-
- if (!PM_NODE_FLAG_P(value, PM_NODE_FLAG_STATIC_LITERAL) || PM_NODE_TYPE_P(value, PM_ARRAY_NODE) || PM_NODE_TYPE_P(value, PM_HASH_NODE) || PM_NODE_TYPE_P(value, PM_RANGE_NODE)) {
- LABEL *end_label = NEW_LABEL(location.line);
-
- pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, name, 0);
- int kw_bits_idx = table_size - body->param.keyword->bits_start;
- PUSH_INSN2(ret, location, checkkeyword, INT2FIX(kw_bits_idx + VM_ENV_DATA_SIZE - 1), INT2FIX(optional_index));
- PUSH_INSNL(ret, location, branchif, end_label);
- PM_COMPILE(value);
- PUSH_SETLOCAL(ret, location, index.index, index.level);
- PUSH_LABEL(ret, end_label);
- }
- optional_index++;
- break;
- }
- // def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
- // ^^
- case PM_REQUIRED_KEYWORD_PARAMETER_NODE: {
- break;
- }
- default: {
- rb_bug("Unexpected keyword parameter node type %s", pm_node_type_to_str(PM_NODE_TYPE(keyword_parameter_node)));
- }
- }
- }
- }
-
- 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.
- const pm_node_t *required = requireds_list->nodes[i];
-
- if (PM_NODE_TYPE_P(required, PM_MULTI_TARGET_NODE)) {
- PUSH_GETLOCAL(ret, location, table_size - (int)i, 0);
- pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) required, ret, scope_node);
- }
- }
- }
-
- 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.
- const pm_node_t *post = posts_list->nodes[i];
-
- if (PM_NODE_TYPE_P(post, PM_MULTI_TARGET_NODE)) {
- PUSH_GETLOCAL(ret, location, table_size - body->param.post_start - (int) i, 0);
- pm_compile_destructured_param_writes(iseq, (const pm_multi_target_node_t *) post, ret, scope_node);
- }
- }
- }
-
- switch (body->type) {
- 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);
- const pm_node_location_t block_location = { .line = body->location.first_lineno, .node_id = -1 };
-
- start->rescued = LABEL_RESCUE_BEG;
- end->rescued = LABEL_RESCUE_END;
-
- // For nodes automatically assign the iteration variable to whatever
- // index variable. We need to handle that write here because it has
- // to happen in the context of the block. Note that this happens
- // before the B_CALL tracepoint event.
- if (PM_NODE_TYPE_P(scope_node->ast_node, PM_FOR_NODE)) {
- pm_compile_for_node_index(iseq, ((const pm_for_node_t *) scope_node->ast_node)->index, ret, scope_node);
- }
-
- PUSH_TRACE(ret, RUBY_EVENT_B_CALL);
- PUSH_INSN(ret, block_location, nop);
- PUSH_LABEL(ret, start);
-
- if (scope_node->body != NULL) {
- switch (PM_NODE_TYPE(scope_node->ast_node)) {
- case PM_POST_EXECUTION_NODE: {
- const pm_post_execution_node_t *cast = (const pm_post_execution_node_t *) scope_node->ast_node;
- PUSH_INSN1(ret, block_location, 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((const pm_node_t *) cast->statements, &next_scope_node, scope_node);
-
- const rb_iseq_t *block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(body->parent_iseq), ISEQ_TYPE_BLOCK, location.line);
- pm_scope_node_destroy(&next_scope_node);
-
- PUSH_CALL_WITH_BLOCK(ret, block_location, id_core_set_postexe, INT2FIX(0), block);
- break;
- }
- case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
- const pm_interpolated_regular_expression_node_t *cast = (const pm_interpolated_regular_expression_node_t *) scope_node->ast_node;
- pm_compile_regexp_dynamic(iseq, (const pm_node_t *) cast, &cast->parts, &location, ret, popped, scope_node);
- break;
- }
- default:
- pm_compile_node(iseq, scope_node->body, ret, popped, scope_node);
- break;
- }
- }
- else {
- PUSH_INSN(ret, block_location, putnil);
- }
-
- PUSH_LABEL(ret, end);
- PUSH_TRACE(ret, RUBY_EVENT_B_RETURN);
- ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
-
- /* wide range catch handler must put at last */
- PUSH_CATCH_ENTRY(CATCH_TYPE_REDO, start, end, NULL, start);
- PUSH_CATCH_ENTRY(CATCH_TYPE_NEXT, start, end, NULL, end);
- break;
- }
- case ISEQ_TYPE_ENSURE: {
- const pm_node_location_t statements_location = (scope_node->body != NULL ? PM_NODE_START_LOCATION(scope_node->parser, scope_node->body) : location);
- iseq_set_exception_local_table(iseq);
-
- if (scope_node->body != NULL) {
- PM_COMPILE_POPPED((const pm_node_t *) scope_node->body);
- }
-
- PUSH_GETLOCAL(ret, statements_location, 1, 0);
- PUSH_INSN1(ret, statements_location, throw, INT2FIX(0));
- return;
- }
- case ISEQ_TYPE_METHOD: {
- ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
- PUSH_TRACE(ret, RUBY_EVENT_CALL);
-
- if (scope_node->body) {
- PM_COMPILE((const pm_node_t *) scope_node->body);
- }
- else {
- PUSH_INSN(ret, location, putnil);
- }
-
- ISEQ_COMPILE_DATA(iseq)->root_node = (const void *) scope_node->body;
- PUSH_TRACE(ret, RUBY_EVENT_RETURN);
-
- ISEQ_COMPILE_DATA(iseq)->last_line = body->location.code_location.end_pos.lineno;
- break;
- }
- case ISEQ_TYPE_RESCUE: {
- iseq_set_exception_local_table(iseq);
- if (PM_NODE_TYPE_P(scope_node->ast_node, PM_RESCUE_MODIFIER_NODE)) {
- LABEL *lab = NEW_LABEL(lineno);
- LABEL *rescue_end = NEW_LABEL(lineno);
- PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
- PUSH_INSN1(ret, location, putobject, rb_eStandardError);
- PUSH_INSN1(ret, location, checkmatch, INT2FIX(VM_CHECKMATCH_TYPE_RESCUE));
- PUSH_INSNL(ret, location, branchif, lab);
- PUSH_INSNL(ret, location, jump, rescue_end);
- PUSH_LABEL(ret, lab);
- PUSH_TRACE(ret, RUBY_EVENT_RESCUE);
- PM_COMPILE((const pm_node_t *) scope_node->body);
- PUSH_INSN(ret, location, leave);
- PUSH_LABEL(ret, rescue_end);
- PUSH_GETLOCAL(ret, location, LVAR_ERRINFO, 0);
- }
- else {
- PM_COMPILE((const pm_node_t *) scope_node->ast_node);
- }
- PUSH_INSN1(ret, location, throw, INT2FIX(0));
-
- return;
- }
- default:
- if (scope_node->body) {
- PM_COMPILE((const pm_node_t *) scope_node->body);
- }
- else {
- PUSH_INSN(ret, location, putnil);
- }
- break;
- }
-
- if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) {
- const pm_node_location_t end_location = PM_NODE_END_LOCATION(scope_node->parser, scope_node->ast_node);
- ADD_TRACE(ret, RUBY_EVENT_END);
- ISEQ_COMPILE_DATA(iseq)->last_line = end_location.line;
- }
-
- if (!PM_NODE_TYPE_P(scope_node->ast_node, PM_ENSURE_NODE)) {
- const pm_node_location_t location = { .line = ISEQ_COMPILE_DATA(iseq)->last_line, .node_id = -1 };
- PUSH_INSN(ret, location, leave);
- }
-
+ case PM_SCOPE_NODE:
+ pm_compile_scope_node(iseq, (pm_scope_node_t *) node, &location, ret, popped);
return;
- }
case PM_SELF_NODE: {
// self
// ^^^^
@@ -9403,7 +10334,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
pm_compile_constant_path_operator_write_node(iseq, (const pm_constant_path_operator_write_node_t *) cast->write, shareability, &location, ret, popped, scope_node);
break;
default:
- rb_bug("Unexpected node type for shareable constant write: %s", pm_node_type_to_str(PM_NODE_TYPE(cast->write)));
+ rb_bug("Unexpected node type for shareable constant write: %s", pm_node_type(PM_NODE_TYPE(cast->write)));
break;
}
@@ -9424,7 +10355,17 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
ID singletonclass;
CONST_ID(singletonclass, "singletonclass");
- PUSH_INSN3(ret, location, defineclass, ID2SYM(singletonclass), child_iseq, INT2FIX(VM_DEFINECLASS_TYPE_SINGLETON_CLASS));
+
+ /* `class << self` in a class body and `class << Foo` (constant
+ receiver) are stable. All other forms are potentially dynamic. */
+ int sclass_flags = VM_DEFINECLASS_TYPE_SINGLETON_CLASS;
+ if (!(PM_NODE_TYPE_P(cast->expression, PM_SELF_NODE) &&
+ ISEQ_BODY(iseq)->type == ISEQ_TYPE_CLASS) &&
+ !pm_cpath_const_p(cast->expression)) {
+ sclass_flags |= VM_DEFINECLASS_FLAG_DYNAMIC_CREF;
+ }
+
+ PUSH_INSN3(ret, location, defineclass, ID2SYM(singletonclass), child_iseq, INT2FIX(sclass_flags));
if (popped) PUSH_INSN(ret, location, pop);
RB_OBJ_WRITTEN(iseq, Qundef, (VALUE) child_iseq);
@@ -9451,10 +10392,10 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PUSH_INSN1(ret, location, putobject, string);
}
else if (PM_NODE_FLAG_P(cast, PM_STRING_FLAGS_MUTABLE)) {
- PUSH_INSN1(ret, location, putstring, string);
+ PUSH_INSN1(ret, location, dupstring, string);
}
else {
- PUSH_INSN1(ret, location, putchilledstring, string);
+ PUSH_INSN1(ret, location, dupchilledstring, string);
}
}
return;
@@ -9508,65 +10449,20 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PUSH_INSN1(ret, location, putobject, value);
}
else if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_MUTABLE)) {
- PUSH_INSN1(ret, location, putstring, value);
+ PUSH_INSN1(ret, location, dupstring, value);
}
else {
- PUSH_INSN1(ret, location, putchilledstring, value);
+ PUSH_INSN1(ret, location, dupchilledstring, value);
}
}
return;
}
- case PM_SUPER_NODE: {
+ case PM_SUPER_NODE:
+ // super()
// super(foo)
- // ^^^^^^^^^^
- const pm_super_node_t *cast = (const pm_super_node_t *) node;
-
- DECL_ANCHOR(args);
- INIT_ANCHOR(args);
-
- LABEL *retry_label = NEW_LABEL(location.line);
- LABEL *retry_end_l = NEW_LABEL(location.line);
-
- const rb_iseq_t *previous_block = ISEQ_COMPILE_DATA(iseq)->current_block;
- const rb_iseq_t *current_block;
- ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NULL;
-
- PUSH_LABEL(ret, retry_label);
- PUSH_INSN(ret, location, putself);
-
- int flags = 0;
- struct rb_callinfo_kwarg *keywords = NULL;
- int argc = pm_setup_args(cast->arguments, cast->block, &flags, &keywords, iseq, ret, scope_node, &location);
- flags |= VM_CALL_SUPER | VM_CALL_FCALL;
-
- if (cast->block && PM_NODE_TYPE_P(cast->block, PM_BLOCK_NODE)) {
- pm_scope_node_t next_scope_node;
- pm_scope_node_init(cast->block, &next_scope_node, scope_node);
-
- ISEQ_COMPILE_DATA(iseq)->current_block = current_block = NEW_CHILD_ISEQ(&next_scope_node, make_name_for_block(iseq), ISEQ_TYPE_BLOCK, lineno);
- pm_scope_node_destroy(&next_scope_node);
- }
-
- if ((flags & VM_CALL_ARGS_BLOCKARG) && (flags & VM_CALL_KW_SPLAT) && !(flags & VM_CALL_KW_SPLAT_MUT)) {
- PUSH_INSN(args, location, splatkw);
- }
-
- PUSH_SEQ(ret, args);
- if (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->param.flags.forwardable) {
- flags |= VM_CALL_FORWARDING;
- PUSH_INSN2(ret, location, invokesuperforward, new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL), current_block);
- }
- else {
- PUSH_INSN2(ret, location, invokesuper, new_callinfo(iseq, 0, argc, flags, keywords, current_block != NULL), current_block);
- pm_compile_retry_end_label(iseq, ret, retry_end_l);
- }
-
- if (popped) PUSH_INSN(ret, location, pop);
- ISEQ_COMPILE_DATA(iseq)->current_block = previous_block;
- PUSH_CATCH_ENTRY(CATCH_TYPE_BREAK, retry_label, retry_end_l, current_block, retry_end_l);
-
+ // super(...)
+ pm_compile_super_node(iseq, (const pm_super_node_t *) node, &location, ret, popped, scope_node);
return;
- }
case PM_SYMBOL_NODE: {
// :foo
// ^^^^
@@ -9612,12 +10508,12 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// bar unless foo
// ^^^^^^^^^^^^^^
const pm_unless_node_t *cast = (const pm_unless_node_t *) node;
- const pm_statements_node_t *consequent = NULL;
- if (cast->consequent != NULL) {
- consequent = ((const pm_else_node_t *) cast->consequent)->statements;
+ const pm_statements_node_t *statements = NULL;
+ if (cast->else_clause != NULL) {
+ statements = ((const pm_else_node_t *) cast->else_clause)->statements;
}
- pm_compile_conditional(iseq, &location, PM_UNLESS_NODE, (const pm_node_t *) cast, consequent, (const pm_node_t *) cast->statements, cast->predicate, ret, popped, scope_node);
+ pm_compile_conditional(iseq, &location, PM_UNLESS_NODE, (const pm_node_t *) cast, statements, (const pm_node_t *) cast->statements, cast->predicate, ret, popped, scope_node);
return;
}
case PM_UNTIL_NODE: {
@@ -9653,49 +10549,22 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
return;
}
- case PM_YIELD_NODE: {
+ case PM_YIELD_NODE:
// yield
// ^^^^^
//
// yield 1
// ^^^^^^^
- const pm_yield_node_t *cast = (const pm_yield_node_t *) node;
-
- switch (ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq)->type) {
- case ISEQ_TYPE_TOP:
- case ISEQ_TYPE_MAIN:
- case ISEQ_TYPE_CLASS:
- COMPILE_ERROR(iseq, location.line, "Invalid yield");
- return;
- default: /* valid */;
- }
-
- int argc = 0;
- int flags = 0;
- struct rb_callinfo_kwarg *keywords = NULL;
-
- if (cast->arguments) {
- argc = pm_setup_args(cast->arguments, NULL, &flags, &keywords, iseq, ret, scope_node, &location);
- }
-
- PUSH_INSN1(ret, location, invokeblock, new_callinfo(iseq, 0, argc, flags, keywords, FALSE));
- if (popped) PUSH_INSN(ret, location, pop);
-
- int level = 0;
- for (const rb_iseq_t *tmp_iseq = iseq; 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);
+ pm_compile_yield_node(iseq, (const pm_yield_node_t *) node, &location, ret, popped, scope_node);
return;
- }
- default: {
- rb_raise(rb_eNotImpError, "node type %s not implemented", pm_node_type_to_str(PM_NODE_TYPE(node)));
+ default:
+ rb_raise(rb_eNotImpError, "node type %s not implemented", pm_node_type(PM_NODE_TYPE(node)));
return;
- }
}
}
+#undef PM_CONTAINER_P
+
/** True if the given iseq can have pre execution blocks. */
static inline bool
pm_iseq_pre_execution_p(rb_iseq_t *iseq)
@@ -9721,7 +10590,6 @@ VALUE
pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
{
DECL_ANCHOR(ret);
- INIT_ANCHOR(ret);
if (pm_iseq_pre_execution_p(iseq)) {
// Because these ISEQs can have BEGIN{}, we're going to create two
@@ -9729,14 +10597,12 @@ pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
// on the scope node so that when BEGIN{} is found, its contents will be
// added to the "pre" anchor.
DECL_ANCHOR(pre);
- INIT_ANCHOR(pre);
node->pre_execution_anchor = pre;
// Now we'll compile the body as normal. We won't compile directly into
// the "ret" anchor yet because we want to add the "pre" anchor to the
// beginning of the "ret" anchor first.
DECL_ANCHOR(body);
- INIT_ANCHOR(body);
pm_compile_node(iseq, (const pm_node_t *) node, body, false, node);
// Now we'll join both anchors together so that the content is in the
@@ -9754,6 +10620,15 @@ pm_iseq_compile_node(rb_iseq_t *iseq, pm_scope_node_t *node)
return iseq_setup(iseq, ret);
}
+void
+pm_parse_result_init(pm_parse_result_t *result)
+{
+ memset(result, 0, sizeof(pm_parse_result_t));
+ result->arena = pm_arena_new();
+ result->options = pm_options_new();
+ pm_options_line_set(result->options, 1);
+}
+
/**
* Free the internal memory associated with a pm_parse_result_t struct.
* Importantly this does not free the struct itself.
@@ -9762,21 +10637,20 @@ void
pm_parse_result_free(pm_parse_result_t *result)
{
if (result->parsed) {
- pm_node_destroy(&result->parser, result->node.ast_node);
-
- xfree(result->node.constants);
+ SIZED_FREE_N(result->node.constants, pm_parser_constants_size(result->node.parser));
pm_scope_node_destroy(&result->node);
}
- pm_parser_free(&result->parser);
- pm_string_free(&result->input);
- pm_options_free(&result->options);
+ if (result->parser) pm_parser_free(result->parser);
+ pm_arena_free(result->arena);
+ if (result->source) pm_source_free(result->source);
+ pm_options_free(result->options);
}
/** An error that is going to be formatted into the output. */
typedef struct {
/** A pointer to the diagnostic that was generated during parsing. */
- pm_diagnostic_t *error;
+ const pm_diagnostic_t *error;
/** The start line of the diagnostic message. */
int32_t line;
@@ -9806,125 +10680,157 @@ typedef struct {
size_t divider_length;
} pm_parse_error_format_t;
-#define PM_COLOR_GRAY "\033[38;5;102m"
+#define PM_COLOR_BOLD "\033[1m"
+#define PM_COLOR_GRAY "\033[2m"
#define PM_COLOR_RED "\033[1;31m"
#define PM_COLOR_RESET "\033[m"
#define PM_ERROR_TRUNCATE 30
-static inline pm_parse_error_t *
-pm_parse_errors_format_sort(const pm_parser_t *parser, const pm_list_t *error_list, const pm_newline_list_t *newline_list) {
- pm_parse_error_t *errors = xcalloc(error_list->size, sizeof(pm_parse_error_t));
- if (errors == NULL) return NULL;
+/** Context struct for collecting errors via callback. */
+typedef struct {
+ pm_parse_error_t *errors;
+ size_t count;
+ size_t capacity;
+ const pm_line_offset_list_t *line_offsets;
+ int32_t start_line;
+} pm_error_collect_t;
- int32_t start_line = parser->start_line;
- for (pm_diagnostic_t *error = (pm_diagnostic_t *) error_list->head; error != NULL; error = (pm_diagnostic_t *) error->node.next) {
- pm_line_column_t start = pm_newline_list_line_column(newline_list, error->location.start, start_line);
- pm_line_column_t end = pm_newline_list_line_column(newline_list, error->location.end, start_line);
-
- // We're going to insert this error into the array in sorted order. We
- // do this by finding the first error that has a line number greater
- // than the current error and then inserting the current error before
- // that one.
- size_t index = 0;
- while (
- (index < error_list->size) &&
- (errors[index].error != NULL) &&
- (
- (errors[index].line < start.line) ||
- ((errors[index].line == start.line) && (errors[index].column_start < start.column))
- )
- ) index++;
-
- // Now we're going to shift all of the errors after this one down one
- // index to make room for the new error.
- if (index + 1 < error_list->size) {
- memmove(&errors[index + 1], &errors[index], sizeof(pm_parse_error_t) * (error_list->size - index - 1));
- }
-
- // Finally, we'll insert the error into the array.
- uint32_t column_end;
- if (start.line == end.line) {
- column_end = end.column;
- } else {
- column_end = (uint32_t) (newline_list->offsets[start.line - start_line + 1] - newline_list->offsets[start.line - start_line] - 1);
- }
+static void
+pm_error_collect_callback(const pm_diagnostic_t *diagnostic, void *data)
+{
+ pm_error_collect_t *ctx = (pm_error_collect_t *) data;
+ pm_location_t loc = pm_diagnostic_location(diagnostic);
- // Ensure we have at least one column of error.
- if (start.column == column_end) column_end++;
+ pm_line_column_t start = pm_line_offset_list_line_column(ctx->line_offsets, loc.start, ctx->start_line);
+ pm_line_column_t end = pm_line_offset_list_line_column(ctx->line_offsets, loc.start + loc.length, ctx->start_line);
- errors[index] = (pm_parse_error_t) {
- .error = error,
- .line = start.line,
- .column_start = start.column,
- .column_end = column_end
- };
+ uint32_t column_end;
+ if (start.line == end.line) {
+ column_end = end.column;
+ } else {
+ column_end = (uint32_t) (ctx->line_offsets->offsets[start.line - ctx->start_line + 1] - ctx->line_offsets->offsets[start.line - ctx->start_line] - 1);
+ }
+
+ // Ensure we have at least one column of error.
+ if (start.column == column_end) column_end++;
+
+ // Insert into sorted position (insertion sort).
+ size_t index = 0;
+ while (
+ (index < ctx->count) &&
+ (
+ (ctx->errors[index].line < start.line) ||
+ ((ctx->errors[index].line == start.line) && (ctx->errors[index].column_start < start.column))
+ )
+ ) index++;
+
+ if (index < ctx->count) {
+ memmove(&ctx->errors[index + 1], &ctx->errors[index], sizeof(pm_parse_error_t) * (ctx->count - index));
}
+ ctx->errors[index] = (pm_parse_error_t) {
+ .error = diagnostic,
+ .line = start.line,
+ .column_start = start.column,
+ .column_end = column_end
+ };
+ ctx->count++;
+}
+
+static inline pm_parse_error_t *
+pm_parse_errors_format_sort(const pm_parser_t *parser, size_t error_count, const pm_line_offset_list_t *line_offsets) {
+ pm_parse_error_t *errors = xcalloc(error_count, sizeof(pm_parse_error_t));
+ if (errors == NULL) return NULL;
+
+ pm_error_collect_t ctx = {
+ .errors = errors,
+ .count = 0,
+ .capacity = error_count,
+ .line_offsets = line_offsets,
+ .start_line = pm_parser_start_line(parser)
+ };
+
+ pm_parser_errors_each(parser, pm_error_collect_callback, &ctx);
+
return errors;
}
static inline void
-pm_parse_errors_format_line(const pm_parser_t *parser, const pm_newline_list_t *newline_list, const char *number_prefix, int32_t line, uint32_t column_start, uint32_t column_end, pm_buffer_t *buffer) {
- int32_t line_delta = line - parser->start_line;
+pm_parse_errors_format_line(const pm_parser_t *parser, const pm_line_offset_list_t *line_offsets, const char *number_prefix, int32_t line, uint32_t column_start, uint32_t column_end, VALUE buffer) {
+ int32_t line_delta = line - pm_parser_start_line(parser);
assert(line_delta >= 0);
size_t index = (size_t) line_delta;
- assert(index < newline_list->size);
+ assert(index < line_offsets->size);
- const uint8_t *start = &parser->start[newline_list->offsets[index]];
+ const uint8_t *start = &pm_parser_start(parser)[line_offsets->offsets[index]];
const uint8_t *end;
- if (index >= newline_list->size - 1) {
- end = parser->end;
+ if (index >= line_offsets->size - 1) {
+ end = pm_parser_end(parser);
} else {
- end = &parser->start[newline_list->offsets[index + 1]];
+ end = &pm_parser_start(parser)[line_offsets->offsets[index + 1]];
}
- pm_buffer_append_format(buffer, number_prefix, line);
+ rb_str_catf(buffer, number_prefix, line);
// Here we determine if we should truncate the end of the line.
bool truncate_end = false;
if ((column_end != 0) && ((end - (start + column_end)) >= PM_ERROR_TRUNCATE)) {
- end = start + column_end + PM_ERROR_TRUNCATE;
+ const uint8_t *end_candidate = start + column_end + PM_ERROR_TRUNCATE;
+
+ for (const uint8_t *ptr = start; ptr < end_candidate;) {
+ size_t char_width = pm_parser_encoding_char_width(parser, ptr, pm_parser_end(parser) - ptr);
+
+ // If we failed to decode a character, then just bail out and
+ // truncate at the fixed width.
+ if (char_width == 0) break;
+
+ // If this next character would go past the end candidate,
+ // then we need to truncate before it.
+ if (ptr + char_width > end_candidate) {
+ end_candidate = ptr;
+ break;
+ }
+
+ ptr += char_width;
+ }
+
+ end = end_candidate;
truncate_end = true;
}
// Here we determine if we should truncate the start of the line.
if (column_start >= PM_ERROR_TRUNCATE) {
- pm_buffer_append_string(buffer, "... ", 4);
+ rb_str_cat(buffer, "... ", 4);
start += column_start;
}
- pm_buffer_append_string(buffer, (const char *) start, (size_t) (end - start));
+ rb_str_cat(buffer, (const char *) start, (size_t) (end - start));
if (truncate_end) {
- pm_buffer_append_string(buffer, " ...\n", 5);
- } else if (end == parser->end && end[-1] != '\n') {
- pm_buffer_append_string(buffer, "\n", 1);
+ rb_str_cat(buffer, " ...\n", 5);
+ } else if (end == pm_parser_end(parser) && end[-1] != '\n') {
+ rb_str_cat(buffer, "\n", 1);
}
}
/**
- * Format the errors on the parser into the given buffer.
+ * Format a pre-sorted array of errors into the given buffer.
*/
static void
-pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, pm_buffer_t *buffer, bool colorize, bool inline_messages) {
- assert(error_list->size != 0);
-
- // First, we're going to sort all of the errors by line number using an
- // insertion sort into a newly allocated array.
- const int32_t start_line = parser->start_line;
- const pm_newline_list_t *newline_list = &parser->newline_list;
+pm_parse_errors_format_with(const pm_parser_t *parser, pm_parse_error_t *errors, size_t error_count, VALUE buffer, int highlight, bool inline_messages) {
+ assert(error_count != 0);
- pm_parse_error_t *errors = pm_parse_errors_format_sort(parser, error_list, newline_list);
- if (errors == NULL) return;
+ const int32_t start_line = pm_parser_start_line(parser);
+ const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(parser);
// Now we're going to determine how we're going to format line numbers and
// blank lines based on the maximum number of digits in the line numbers
// that are going to be displaid.
pm_parse_error_format_t error_format;
int32_t first_line_number = errors[0].line;
- int32_t last_line_number = errors[error_list->size - 1].line;
+ int32_t last_line_number = errors[error_count - 1].line;
// If we have a maximum line number that is negative, then we're going to
// use the absolute value for comparison but multiple by 10 to additionally
@@ -9934,7 +10840,7 @@ pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, p
int32_t max_line_number = first_line_number > last_line_number ? first_line_number : last_line_number;
if (max_line_number < 10) {
- if (colorize) {
+ if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%1" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
@@ -9948,7 +10854,7 @@ pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, p
};
}
} else if (max_line_number < 100) {
- if (colorize) {
+ if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%2" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
@@ -9962,7 +10868,7 @@ pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, p
};
}
} else if (max_line_number < 1000) {
- if (colorize) {
+ if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%3" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
@@ -9976,7 +10882,7 @@ pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, p
};
}
} else if (max_line_number < 10000) {
- if (colorize) {
+ if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%4" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
@@ -9990,7 +10896,7 @@ pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, p
};
}
} else {
- if (colorize) {
+ if (highlight > 0) {
error_format = (pm_parse_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%5" PRIi32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
@@ -10013,11 +10919,10 @@ pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, p
// the source before the error to give some context. We'll be careful not to
// display the same line twice in case the errors are close enough in the
// source.
- int32_t last_line = parser->start_line - 1;
+ int32_t last_line = pm_parser_start_line(parser) - 1;
uint32_t last_column_start = 0;
- const pm_encoding_t *encoding = parser->encoding;
- for (size_t index = 0; index < error_list->size; index++) {
+ for (size_t index = 0; index < error_count; index++) {
pm_parse_error_t *error = &errors[index];
// Here we determine how many lines of padding of the source to display,
@@ -10025,40 +10930,42 @@ pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, p
if (error->line - last_line > 1) {
if (error->line - last_line > 2) {
if ((index != 0) && (error->line - last_line > 3)) {
- pm_buffer_append_string(buffer, error_format.divider, error_format.divider_length);
+ rb_str_cat(buffer, error_format.divider, error_format.divider_length);
}
- pm_buffer_append_string(buffer, " ", 2);
- pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line - 2, 0, 0, buffer);
+ rb_str_cat(buffer, " ", 2);
+ pm_parse_errors_format_line(parser, line_offsets, error_format.number_prefix, error->line - 2, 0, 0, buffer);
}
- pm_buffer_append_string(buffer, " ", 2);
- pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line - 1, 0, 0, buffer);
+ rb_str_cat(buffer, " ", 2);
+ pm_parse_errors_format_line(parser, line_offsets, error_format.number_prefix, error->line - 1, 0, 0, buffer);
}
// If this is the first error or we're on a new line, then we'll display
// the line that has the error in it.
if ((index == 0) || (error->line != last_line)) {
- if (colorize) {
- pm_buffer_append_string(buffer, PM_COLOR_RED "> " PM_COLOR_RESET, 12);
+ if (highlight > 1) {
+ rb_str_cat_cstr(buffer, PM_COLOR_RED "> " PM_COLOR_RESET);
+ } else if (highlight > 0) {
+ rb_str_cat_cstr(buffer, PM_COLOR_BOLD "> " PM_COLOR_RESET);
} else {
- pm_buffer_append_string(buffer, "> ", 2);
+ rb_str_cat_cstr(buffer, "> ");
}
last_column_start = error->column_start;
// Find the maximum column end of all the errors on this line.
uint32_t column_end = error->column_end;
- for (size_t next_index = index + 1; next_index < error_list->size; next_index++) {
+ for (size_t next_index = index + 1; next_index < error_count; next_index++) {
if (errors[next_index].line != error->line) break;
if (errors[next_index].column_end > column_end) column_end = errors[next_index].column_end;
}
- pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, error->line, error->column_start, column_end, buffer);
+ pm_parse_errors_format_line(parser, line_offsets, error_format.number_prefix, error->line, error->column_start, column_end, buffer);
}
- const uint8_t *start = &parser->start[newline_list->offsets[error->line - start_line]];
- if (start == parser->end) pm_buffer_append_byte(buffer, '\n');
+ const uint8_t *start = &pm_parser_start(parser)[line_offsets->offsets[error->line - start_line]];
+ if (start == pm_parser_end(parser)) rb_str_cat(buffer, "\n", 1);
// Now we'll display the actual error message. We'll do this by first
// putting the prefix to the line, then a bunch of blank spaces
@@ -10069,65 +10976,91 @@ pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, p
// character when displaid in the terminal. For some east-asian
// languages or emoji, this means it can be thrown off pretty badly. We
// will need to solve this eventually.
- pm_buffer_append_string(buffer, " ", 2);
- pm_buffer_append_string(buffer, error_format.blank_prefix, error_format.blank_prefix_length);
+ rb_str_cat(buffer, " ", 2);
+ rb_str_cat(buffer, error_format.blank_prefix, error_format.blank_prefix_length);
size_t column = 0;
if (last_column_start >= PM_ERROR_TRUNCATE) {
- pm_buffer_append_string(buffer, " ", 4);
+ rb_str_cat(buffer, " ", 4);
column = last_column_start;
}
while (column < error->column_start) {
- pm_buffer_append_byte(buffer, ' ');
+ rb_str_cat(buffer, " ", 1);
- size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
+ size_t char_width = pm_parser_encoding_char_width(parser, start + column, pm_parser_end(parser) - (start + column));
column += (char_width == 0 ? 1 : char_width);
}
- if (colorize) pm_buffer_append_string(buffer, PM_COLOR_RED, 7);
- pm_buffer_append_byte(buffer, '^');
+ if (highlight > 1) rb_str_cat_cstr(buffer, PM_COLOR_RED);
+ else if (highlight > 0) rb_str_cat_cstr(buffer, PM_COLOR_BOLD);
+ rb_str_cat(buffer, "^", 1);
- size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
+ size_t char_width = pm_parser_encoding_char_width(parser, start + column, pm_parser_end(parser) - (start + column));
column += (char_width == 0 ? 1 : char_width);
while (column < error->column_end) {
- pm_buffer_append_byte(buffer, '~');
+ rb_str_cat(buffer, "~", 1);
- size_t char_width = encoding->char_width(start + column, parser->end - (start + column));
+ size_t char_width = pm_parser_encoding_char_width(parser, start + column, pm_parser_end(parser) - (start + column));
column += (char_width == 0 ? 1 : char_width);
}
- if (colorize) pm_buffer_append_string(buffer, PM_COLOR_RESET, 3);
+ if (highlight > 0) rb_str_cat_cstr(buffer, PM_COLOR_RESET);
if (inline_messages) {
- pm_buffer_append_byte(buffer, ' ');
+ rb_str_cat(buffer, " ", 1);
assert(error->error != NULL);
- const char *message = error->error->message;
- pm_buffer_append_string(buffer, message, strlen(message));
+ const char *message = pm_diagnostic_message(error->error);
+ rb_str_cat(buffer, message, strlen(message));
}
- pm_buffer_append_byte(buffer, '\n');
+ rb_str_cat(buffer, "\n", 1);
// Here we determine how many lines of padding to display after the
// error, depending on where the next error is in source.
last_line = error->line;
- int32_t next_line = (index == error_list->size - 1) ? (((int32_t) newline_list->size) + parser->start_line) : errors[index + 1].line;
+ int32_t next_line;
+
+ if (index == error_count - 1) {
+ next_line = (((int32_t) line_offsets->size) + pm_parser_start_line(parser));
+
+ // If the file ends with a newline, subtract one from our "next_line"
+ // so that we don't output an extra line at the end of the file
+ if ((pm_parser_start(parser) + line_offsets->offsets[line_offsets->size - 1]) == pm_parser_end(parser)) {
+ next_line--;
+ }
+ }
+ else {
+ next_line = errors[index + 1].line;
+ }
if (next_line - last_line > 1) {
- pm_buffer_append_string(buffer, " ", 2);
- pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, ++last_line, 0, 0, buffer);
+ rb_str_cat(buffer, " ", 2);
+ pm_parse_errors_format_line(parser, line_offsets, error_format.number_prefix, ++last_line, 0, 0, buffer);
}
if (next_line - last_line > 1) {
- pm_buffer_append_string(buffer, " ", 2);
- pm_parse_errors_format_line(parser, newline_list, error_format.number_prefix, ++last_line, 0, 0, buffer);
+ rb_str_cat(buffer, " ", 2);
+ pm_parse_errors_format_line(parser, line_offsets, error_format.number_prefix, ++last_line, 0, 0, buffer);
}
}
- // Finally, we'll free the array of errors that we allocated.
- xfree(errors);
+}
+
+/**
+ * Format the errors on the parser into the given buffer.
+ */
+static void
+pm_parse_errors_format(const pm_parser_t *parser, size_t error_count, VALUE buffer, int highlight, bool inline_messages) {
+ const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(parser);
+
+ pm_parse_error_t *errors = pm_parse_errors_format_sort(parser, error_count, line_offsets);
+ if (errors == NULL) return;
+
+ pm_parse_errors_format_with(parser, errors, error_count, buffer, highlight, inline_messages);
+ SIZED_FREE_N(errors, error_count);
}
#undef PM_ERROR_TRUNCATE
@@ -10142,130 +11075,256 @@ pm_parse_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, p
* as well.
*/
static bool
-pm_parse_process_error_utf8_p(const pm_parser_t *parser, const pm_location_t *location)
+pm_parse_process_error_utf8_p(const pm_parser_t *parser, pm_location_t location)
{
- const size_t start_line = pm_newline_list_line_column(&parser->newline_list, location->start, 1).line;
- const size_t end_line = pm_newline_list_line_column(&parser->newline_list, location->end, 1).line;
+ const size_t start_line = pm_line_offset_list_line_column(pm_parser_line_offsets(parser), location.start, 1).line;
+ const size_t end_line = pm_line_offset_list_line_column(pm_parser_line_offsets(parser), location.start + location.length, 1).line;
- const uint8_t *start = parser->start + parser->newline_list.offsets[start_line - 1];
- const uint8_t *end = ((end_line == parser->newline_list.size) ? parser->end : (parser->start + parser->newline_list.offsets[end_line]));
- size_t width;
+ const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(parser);
+ const uint8_t *start = pm_parser_start(parser) + line_offsets->offsets[start_line - 1];
+ const uint8_t *end = ((end_line == line_offsets->size) ? pm_parser_end(parser) : (pm_parser_start(parser) + line_offsets->offsets[end_line]));
+ rb_encoding *utf8 = rb_utf8_encoding();
while (start < end) {
- if ((width = pm_encoding_utf_8_char_width(start, end - start)) == 0) return false;
- start += width;
+ int width = rb_enc_precise_mbclen((const char *) start, (const char *) end, utf8);
+ if (!MBCLEN_CHARFOUND_P(width)) return false;
+ start += MBCLEN_CHARFOUND_LEN(width);
}
return true;
}
-/**
- * Generate an error object from the given parser that contains as much
- * information as possible about the errors that were encountered.
- */
-static VALUE
-pm_parse_process_error(const pm_parse_result_t *result)
+/** Context for the error processing callback used in pm_parse_process_error. */
+typedef struct {
+ const pm_parse_result_t *result;
+ const pm_parser_t *parser;
+ const pm_string_t *filepath;
+ VALUE buffer;
+ int highlight;
+ bool valid_utf8;
+ bool found_argument_error;
+ bool found_load_error;
+ VALUE early_return;
+ const pm_diagnostic_t *first_error;
+ size_t error_count;
+} pm_process_error_ctx_t;
+
+static void
+pm_process_error_check_callback(const pm_diagnostic_t *diagnostic, void *data)
{
- const pm_parser_t *parser = &result->parser;
- const pm_diagnostic_t *head = (const pm_diagnostic_t *) parser->error_list.head;
- bool valid_utf8 = true;
+ pm_process_error_ctx_t *ctx = (pm_process_error_ctx_t *) data;
+ pm_location_t loc = pm_diagnostic_location(diagnostic);
- pm_buffer_t buffer = { 0 };
- const pm_string_t *filepath = &parser->filepath;
+ if (ctx->first_error == NULL) ctx->first_error = diagnostic;
+ ctx->error_count++;
- for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
- switch (error->level) {
- case PM_ERROR_LEVEL_SYNTAX:
- // It is implicitly assumed that the error messages will be
- // encodeable as UTF-8. Because of this, we can't include source
- // examples that contain invalid byte sequences. So if any source
- // examples include invalid UTF-8 byte sequences, we will skip
- // showing source examples entirely.
- if (valid_utf8 && !pm_parse_process_error_utf8_p(parser, &error->location)) {
- valid_utf8 = false;
+ switch (pm_diagnostic_error_level(diagnostic)) {
+ case PM_ERROR_LEVEL_SYNTAX:
+ if (ctx->valid_utf8 && !pm_parse_process_error_utf8_p(ctx->parser, loc)) {
+ ctx->valid_utf8 = false;
+ }
+ break;
+ case PM_ERROR_LEVEL_ARGUMENT: {
+ if (ctx->found_argument_error || ctx->found_load_error) break;
+ ctx->found_argument_error = true;
+
+ int32_t line_number = (int32_t) pm_location_line_number(ctx->parser, &loc);
+
+ rb_str_catf(
+ ctx->buffer,
+ "%.*s:%" PRIi32 ": %s",
+ (int) pm_string_length(ctx->filepath),
+ pm_string_source(ctx->filepath),
+ line_number,
+ pm_diagnostic_message(diagnostic)
+ );
+
+ if (pm_parse_process_error_utf8_p(ctx->parser, loc)) {
+ rb_str_cat(ctx->buffer, "\n", 1);
+ // Format just this one error. We construct a single-element sorted
+ // array manually and call the format function with count=1.
+ const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(ctx->parser);
+ int32_t start_line = pm_parser_start_line(ctx->parser);
+ pm_line_column_t start_lc = pm_line_offset_list_line_column(line_offsets, loc.start, start_line);
+ pm_line_column_t end_lc = pm_line_offset_list_line_column(line_offsets, loc.start + loc.length, start_line);
+
+ uint32_t col_end;
+ if (start_lc.line == end_lc.line) {
+ col_end = end_lc.column;
+ } else {
+ col_end = (uint32_t) (line_offsets->offsets[start_lc.line - start_line + 1] - line_offsets->offsets[start_lc.line - start_line] - 1);
}
- break;
- case PM_ERROR_LEVEL_ARGUMENT: {
- // Any errors with the level PM_ERROR_LEVEL_ARGUMENT take over as
- // the only argument that gets raised. This is to allow priority
- // messages that should be handled before anything else.
- int32_t line_number = (int32_t) pm_location_line_number(parser, &error->location);
-
- pm_buffer_append_format(
- &buffer,
- "%.*s:%" PRIi32 ": %s",
- (int) pm_string_length(filepath),
- pm_string_source(filepath),
- line_number,
- error->message
- );
+ if (start_lc.column == col_end) col_end++;
- if (pm_parse_process_error_utf8_p(parser, &error->location)) {
- pm_buffer_append_byte(&buffer, '\n');
+ pm_parse_error_t single_error = {
+ .error = diagnostic,
+ .line = start_lc.line,
+ .column_start = start_lc.column,
+ .column_end = col_end
+ };
+ pm_parse_errors_format_with(ctx->parser, &single_error, 1, ctx->buffer, ctx->highlight, false);
+ }
- pm_list_node_t *list_node = (pm_list_node_t *) error;
- pm_list_t error_list = { .size = 1, .head = list_node, .tail = list_node };
+ ctx->early_return = rb_exc_new_str(rb_eArgError, ctx->buffer);
+ break;
+ }
+ case PM_ERROR_LEVEL_LOAD: {
+ if (ctx->found_argument_error || ctx->found_load_error) break;
+ ctx->found_load_error = true;
- pm_parse_errors_format(parser, &error_list, &buffer, rb_stderr_tty_p(), false);
- }
+ VALUE message = rb_enc_str_new_cstr(pm_diagnostic_message(diagnostic), rb_locale_encoding());
+ VALUE value = rb_exc_new3(rb_eLoadError, message);
+ rb_ivar_set(value, rb_intern_const("@path"), Qnil);
+ ctx->early_return = value;
+ break;
+ }
+ }
+}
- VALUE value = rb_exc_new(rb_eArgError, pm_buffer_value(&buffer), pm_buffer_length(&buffer));
- pm_buffer_free(&buffer);
+/** Callback for formatting non-UTF8 errors. */
+typedef struct {
+ const pm_parser_t *parser;
+ const pm_string_t *filepath;
+ VALUE buffer;
+ bool first;
+} pm_error_simple_format_ctx_t;
- return value;
- }
- case PM_ERROR_LEVEL_LOAD: {
- // Load errors are much simpler, because they don't include any of
- // the source in them. We create the error directly from the
- // message.
- VALUE message = rb_enc_str_new_cstr(error->message, rb_locale_encoding());
- VALUE value = rb_exc_new3(rb_eLoadError, message);
- rb_ivar_set(value, rb_intern_const("@path"), Qnil);
- return value;
- }
- }
+static void
+pm_error_simple_format_callback(const pm_diagnostic_t *diagnostic, void *data)
+{
+ pm_error_simple_format_ctx_t *ctx = (pm_error_simple_format_ctx_t *) data;
+ pm_location_t loc = pm_diagnostic_location(diagnostic);
+
+ if (!ctx->first) rb_str_cat(ctx->buffer, "\n", 1);
+ ctx->first = false;
+
+ rb_str_catf(ctx->buffer, "%.*s:%" PRIi32 ": %s",
+ (int) pm_string_length(ctx->filepath),
+ pm_string_source(ctx->filepath),
+ (int32_t) pm_location_line_number(ctx->parser, &loc),
+ pm_diagnostic_message(diagnostic));
+}
+
+/**
+ * Generate an error object from the given parser that contains as much
+ * information as possible about the errors that were encountered.
+ */
+static VALUE
+pm_parse_process_error(const pm_parse_result_t *result)
+{
+ const pm_parser_t *parser = result->parser;
+ size_t error_count = pm_parser_errors_size(parser);
+
+ VALUE buffer = rb_str_buf_new(0);
+ const pm_string_t *filepath = pm_parser_filepath(parser);
+
+ int highlight = rb_stderr_tty_p();
+ if (highlight) {
+ const char *no_color = getenv("NO_COLOR");
+ highlight = (no_color == NULL || no_color[0] == '\0') ? 2 : 1;
+ }
+
+ // First pass: check for argument/load errors and UTF-8 validity.
+ pm_process_error_ctx_t ctx = {
+ .result = result,
+ .parser = parser,
+ .filepath = filepath,
+ .buffer = buffer,
+ .highlight = highlight,
+ .valid_utf8 = true,
+ .found_argument_error = false,
+ .found_load_error = false,
+ .early_return = Qundef,
+ .first_error = NULL,
+ .error_count = 0
+ };
+
+ pm_parser_errors_each(parser, pm_process_error_check_callback, &ctx);
+
+ // If we found an argument or load error, return it immediately.
+ if (ctx.early_return != Qundef) {
+ return ctx.early_return;
}
- pm_buffer_append_format(
- &buffer,
+ // Format the header line.
+ pm_location_t first_loc = pm_diagnostic_location(ctx.first_error);
+ rb_str_catf(
+ buffer,
"%.*s:%" PRIi32 ": syntax error%s found\n",
(int) pm_string_length(filepath),
pm_string_source(filepath),
- (int32_t) pm_location_line_number(parser, &head->location),
- (parser->error_list.size > 1) ? "s" : ""
+ (int32_t) pm_location_line_number(parser, &first_loc),
+ (error_count > 1) ? "s" : ""
);
- if (valid_utf8) {
- pm_parse_errors_format(parser, &parser->error_list, &buffer, rb_stderr_tty_p(), true);
+ if (ctx.valid_utf8) {
+ pm_parse_errors_format(parser, error_count, buffer, highlight, true);
}
else {
- for (const pm_diagnostic_t *error = head; error != NULL; error = (const pm_diagnostic_t *) error->node.next) {
- if (error != head) pm_buffer_append_byte(&buffer, '\n');
- pm_buffer_append_format(&buffer, "%.*s:%" PRIi32 ": %s", (int) pm_string_length(filepath), pm_string_source(filepath), (int32_t) pm_location_line_number(parser, &error->location), error->message);
- }
+ pm_error_simple_format_ctx_t simple_ctx = {
+ .parser = parser,
+ .filepath = filepath,
+ .buffer = buffer,
+ .first = true
+ };
+ pm_parser_errors_each(parser, pm_error_simple_format_callback, &simple_ctx);
}
- VALUE message = rb_enc_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer), result->node.encoding);
- VALUE error = rb_exc_new_str(rb_eSyntaxError, message);
+ rb_enc_associate(buffer, result->node.encoding);
+ VALUE error = rb_exc_new_str(rb_eSyntaxError, buffer);
rb_encoding *filepath_encoding = result->node.filepath_encoding != NULL ? result->node.filepath_encoding : rb_utf8_encoding();
VALUE path = rb_enc_str_new((const char *) pm_string_source(filepath), pm_string_length(filepath), filepath_encoding);
rb_ivar_set(error, rb_intern_const("@path"), path);
- pm_buffer_free(&buffer);
return error;
}
+/** Context for interning constants via callback. */
+typedef struct {
+ ID *constants;
+ rb_encoding *encoding;
+ size_t index;
+} pm_intern_constants_ctx_t;
+
+static void
+pm_intern_constants_callback(const pm_constant_t *constant, void *data)
+{
+ pm_intern_constants_ctx_t *ctx = (pm_intern_constants_ctx_t *) data;
+ ctx->constants[ctx->index++] = rb_intern3((const char *) pm_constant_start(constant), pm_constant_length(constant), ctx->encoding);
+}
+
+/** Context for emitting warnings via callback. */
+typedef struct {
+ const pm_parser_t *parser;
+ rb_encoding *encoding;
+ const char *filepath;
+} pm_warning_emit_ctx_t;
+
+static void
+pm_warning_emit_callback(const pm_diagnostic_t *diagnostic, void *data) {
+ pm_warning_emit_ctx_t *ctx = (pm_warning_emit_ctx_t *) data;
+ pm_location_t loc = pm_diagnostic_location(diagnostic);
+ int line = pm_location_line_number(ctx->parser, &loc);
+
+ if (pm_diagnostic_warning_level(diagnostic) == PM_WARNING_LEVEL_VERBOSE) {
+ rb_enc_compile_warning(ctx->encoding, ctx->filepath, line, "%s", pm_diagnostic_message(diagnostic));
+ }
+ else {
+ rb_enc_compile_warn(ctx->encoding, ctx->filepath, line, "%s", pm_diagnostic_message(diagnostic));
+ }
+}
+
/**
* Parse the parse result and raise a Ruby error if there are any syntax errors.
* It returns an error if one should be raised. It is assumed that the parse
* result object is zeroed out.
*/
static VALUE
-pm_parse_process(pm_parse_result_t *result, pm_node_t *node)
+pm_parse_process(pm_parse_result_t *result, pm_node_t *node, VALUE *script_lines)
{
- pm_parser_t *parser = &result->parser;
+ pm_parser_t *parser = result->parser;
// First, set up the scope node so that the AST node is attached and can be
// freed regardless of whether or we return an error.
@@ -10276,28 +11335,37 @@ pm_parse_process(pm_parse_result_t *result, pm_node_t *node)
pm_scope_node_init(node, scope_node, NULL);
scope_node->filepath_encoding = filepath_encoding;
- scope_node->encoding = rb_enc_find(parser->encoding->name);
- if (!scope_node->encoding) rb_bug("Encoding not found %s!", parser->encoding->name);
+ const char *encoding_name = pm_parser_encoding_name(parser);
+ scope_node->encoding = rb_enc_find(encoding_name);
+ if (!scope_node->encoding) rb_bug("Encoding not found %s!", encoding_name);
scope_node->coverage_enabled = coverage_enabled;
- // Emit all of the various warnings from the parse.
- const pm_diagnostic_t *warning;
- const char *warning_filepath = (const char *) pm_string_source(&parser->filepath);
-
- for (warning = (const pm_diagnostic_t *) parser->warning_list.head; warning != NULL; warning = (const pm_diagnostic_t *) warning->node.next) {
- int line = pm_location_line_number(parser, &warning->location);
+ // If RubyVM.keep_script_lines is set to true, then we need to create that
+ // array of script lines here.
+ if (script_lines != NULL) {
+ const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(parser);
+ *script_lines = rb_ary_new_capa(line_offsets->size);
- if (warning->level == PM_WARNING_LEVEL_VERBOSE) {
- rb_enc_compile_warning(scope_node->encoding, warning_filepath, line, "%s", warning->message);
- }
- else {
- rb_enc_compile_warn(scope_node->encoding, warning_filepath, line, "%s", warning->message);
+ for (size_t index = 0; index < line_offsets->size; index++) {
+ size_t offset = line_offsets->offsets[index];
+ size_t length = index == line_offsets->size - 1 ? ((size_t) (pm_parser_end(parser) - (pm_parser_start(parser) + offset))) : (line_offsets->offsets[index + 1] - offset);
+ rb_ary_push(*script_lines, rb_enc_str_new((const char *) pm_parser_start(parser) + offset, length, scope_node->encoding));
}
+
+ scope_node->script_lines = script_lines;
}
+ // Emit all of the various warnings from the parse.
+ pm_warning_emit_ctx_t warning_ctx = {
+ .parser = parser,
+ .encoding = scope_node->encoding,
+ .filepath = (const char *) pm_string_source(pm_parser_filepath(parser))
+ };
+ pm_parser_warnings_each(parser, pm_warning_emit_callback, &warning_ctx);
+
// If there are errors, raise an appropriate error and free the result.
- if (parser->error_list.size > 0) {
+ if (pm_parser_errors_size(parser) > 0) {
VALUE error = pm_parse_process_error(result);
// TODO: We need to set the backtrace.
@@ -10308,18 +11376,14 @@ pm_parse_process(pm_parse_result_t *result, pm_node_t *node)
// Now set up the constant pool and intern all of the various constants into
// their corresponding IDs.
scope_node->parser = parser;
- scope_node->constants = xcalloc(parser->constant_pool.size, sizeof(ID));
-
- for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
- pm_constant_t *constant = &parser->constant_pool.constants[index];
- scope_node->constants[index] = rb_intern3((const char *) constant->start, constant->length, scope_node->encoding);
- }
+ scope_node->options = result->options;
+ scope_node->line_offsets = pm_parser_line_offsets(parser);
+ scope_node->start_line = pm_parser_start_line(parser);
+ size_t constants_size = pm_parser_constants_size(parser);
+ scope_node->constants = constants_size ? xmalloc(constants_size * sizeof(ID)) : NULL;
- scope_node->index_lookup_table = st_init_numtable();
- pm_constant_id_list_t *locals = &scope_node->locals;
- for (size_t index = 0; index < locals->size; index++) {
- st_insert(scope_node->index_lookup_table, locals->ids[index], index);
- }
+ pm_intern_constants_ctx_t intern_ctx = { .constants = scope_node->constants, .encoding = scope_node->encoding, .index = 0 };
+ pm_parser_constants_each(parser, pm_intern_constants_callback, &intern_ctx);
// If we got here, this is a success and we can return Qnil to indicate that
// no error should be raised.
@@ -10358,22 +11422,22 @@ pm_options_frozen_string_literal_init(pm_options_t *options)
static inline VALUE
pm_parse_file_script_lines(const pm_scope_node_t *scope_node, const pm_parser_t *parser)
{
- const pm_newline_list_t *newline_list = &parser->newline_list;
- const char *start = (const char *) parser->start;
- const char *end = (const char *) parser->end;
+ const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(parser);
+ const char *start = (const char *) pm_parser_start(parser);
+ const char *end = (const char *) pm_parser_end(parser);
// If we end exactly on a newline, then there's no need to push on a final
// segment. If we don't, then we need to push on the last offset up to the
// end of the string.
- size_t last_offset = newline_list->offsets[newline_list->size - 1];
+ size_t last_offset = line_offsets->offsets[line_offsets->size - 1];
bool last_push = start + last_offset != end;
// Create the ruby strings that represent the lines of the source.
- VALUE lines = rb_ary_new_capa(newline_list->size - (last_push ? 0 : 1));
+ VALUE lines = rb_ary_new_capa(line_offsets->size - (last_push ? 0 : 1));
- for (size_t index = 0; index < newline_list->size - 1; index++) {
- size_t offset = newline_list->offsets[index];
- size_t length = newline_list->offsets[index + 1] - offset;
+ for (size_t index = 0; index < line_offsets->size - 1; index++) {
+ size_t offset = line_offsets->offsets[index];
+ size_t length = line_offsets->offsets[index + 1] - offset;
rb_ary_push(lines, rb_enc_str_new(start + offset, length, scope_node->encoding));
}
@@ -10386,6 +11450,37 @@ pm_parse_file_script_lines(const pm_scope_node_t *scope_node, const pm_parser_t
return lines;
}
+struct load_from_fd_args {
+ VALUE path;
+ VALUE io;
+ int open_mode;
+ int fd;
+};
+
+static VALUE
+close_file(VALUE args)
+{
+ struct load_from_fd_args *arg = (void *)args;
+ if (arg->fd != -1) {
+ close(arg->fd);
+ }
+ else if (!NIL_P(arg->io)) {
+ rb_io_close(arg->io);
+ }
+ return Qnil;
+}
+
+static VALUE
+load_content(VALUE args)
+{
+ struct load_from_fd_args *arg = (void *)args;
+ VALUE io = rb_io_fdopen(arg->fd, arg->open_mode, RSTRING_PTR(arg->path));
+ arg->io = io;
+ arg->fd = -1;
+ rb_io_wait(io, RB_INT2NUM(RUBY_IO_READABLE), Qnil);
+ return rb_funcall(io, rb_intern("read"), 0);
+}
+
/**
* Attempt to load the file into memory. Return a Ruby error if the file cannot
* be read.
@@ -10393,32 +11488,67 @@ pm_parse_file_script_lines(const pm_scope_node_t *scope_node, const pm_parser_t
VALUE
pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error)
{
- if (!pm_string_mapped_init(&result->input, RSTRING_PTR(filepath))) {
+ pm_source_init_result_t init_result;
+ result->source = pm_source_mapped_new(RSTRING_PTR(filepath), O_RDONLY | O_NONBLOCK, &init_result);
+
+ if (init_result == PM_SOURCE_INIT_SUCCESS) {
+ pm_options_frozen_string_literal_init(result->options);
+ return Qnil;
+ }
+
+ int err;
+
+ // For non-regular files (pipes, character devices), we need to read
+ // through Ruby IO to properly release the GVL while waiting for data.
+ if (init_result == PM_SOURCE_INIT_ERROR_NON_REGULAR) {
+ struct load_from_fd_args args = {
+ .path = filepath,
+ .open_mode = O_RDONLY | O_NONBLOCK,
+ .fd = rb_cloexec_open(RSTRING_PTR(filepath), args.open_mode, 0),
+ .io = Qnil,
+ };
+ if (args.fd == -1) goto error_generic;
+ VALUE contents = rb_ensure(load_content, (VALUE)&args, close_file, (VALUE)&args);
+
+ if (!RB_TYPE_P(contents, T_STRING)) goto error_generic;
+
+ long len = RSTRING_LEN(contents);
+ if (len < 0) goto error_generic;
+
+ size_t length = (size_t) len;
+ uint8_t *source_data = xmalloc(length);
+ memcpy(source_data, RSTRING_PTR(contents), length);
+ result->source = pm_source_owned_new(source_data, length);
+
+ pm_options_frozen_string_literal_init(result->options);
+ return Qnil;
+ }
+
+ if (init_result == PM_SOURCE_INIT_ERROR_DIRECTORY) {
+ err = EISDIR;
+ } else {
+error_generic:
#ifdef _WIN32
- int e = rb_w32_map_errno(GetLastError());
+ err = rb_w32_map_errno(GetLastError());
#else
- int e = errno;
+ err = errno;
#endif
+ }
- VALUE error;
-
- if (load_error) {
- VALUE message = rb_str_buf_new_cstr(strerror(e));
- rb_str_cat2(message, " -- ");
- rb_str_append(message, filepath);
+ VALUE error;
+ if (load_error) {
+ VALUE message = rb_str_buf_new_cstr(strerror(err));
+ rb_str_cat2(message, " -- ");
+ rb_str_append(message, filepath);
- error = rb_exc_new3(rb_eLoadError, message);
- rb_ivar_set(error, rb_intern_const("@path"), filepath);
- } else {
- error = rb_syserr_new(e, RSTRING_PTR(filepath));
- RB_GC_GUARD(filepath);
- }
-
- return error;
+ error = rb_exc_new3(rb_eLoadError, message);
+ rb_ivar_set(error, rb_intern_const("@path"), filepath);
+ } else {
+ error = rb_syserr_new(err, RSTRING_PTR(filepath));
+ RB_GC_GUARD(filepath);
}
- pm_options_frozen_string_literal_init(&result->options);
- return Qnil;
+ return error;
}
/**
@@ -10428,16 +11558,18 @@ pm_load_file(pm_parse_result_t *result, VALUE filepath, bool load_error)
* is zeroed out.
*/
VALUE
-pm_parse_file(pm_parse_result_t *result, VALUE filepath)
+pm_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines)
{
result->node.filepath_encoding = rb_enc_get(filepath);
- pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
+ pm_options_filepath_set(result->options, RSTRING_PTR(filepath));
RB_GC_GUARD(filepath);
- pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
- pm_node_t *node = pm_parse(&result->parser);
+ pm_options_version_for_current_ruby_set(result->options);
+
+ result->parser = pm_parser_new(result->arena, pm_source_source(result->source), pm_source_length(result->source), result->options);
+ pm_node_t *node = pm_parse(result->parser);
- VALUE error = pm_parse_process(result, node);
+ VALUE error = pm_parse_process(result, node, script_lines);
// If we're parsing a filepath, then we need to potentially support the
// SCRIPT_LINES__ constant, which can be a hash that has an array of lines
@@ -10445,10 +11577,10 @@ pm_parse_file(pm_parse_result_t *result, VALUE filepath)
ID id_script_lines = rb_intern("SCRIPT_LINES__");
if (rb_const_defined_at(rb_cObject, id_script_lines)) {
- VALUE script_lines = rb_const_get_at(rb_cObject, id_script_lines);
+ VALUE constant_script_lines = rb_const_get_at(rb_cObject, id_script_lines);
- if (RB_TYPE_P(script_lines, T_HASH)) {
- rb_hash_aset(script_lines, filepath, pm_parse_file_script_lines(&result->node, &result->parser));
+ if (RB_TYPE_P(constant_script_lines, T_HASH)) {
+ rb_hash_aset(constant_script_lines, filepath, pm_parse_file_script_lines(&result->node, result->parser));
}
}
@@ -10460,11 +11592,11 @@ pm_parse_file(pm_parse_result_t *result, VALUE filepath)
* cannot be read or if it cannot be parsed properly.
*/
VALUE
-pm_load_parse_file(pm_parse_result_t *result, VALUE filepath)
+pm_load_parse_file(pm_parse_result_t *result, VALUE filepath, VALUE *script_lines)
{
VALUE error = pm_load_file(result, filepath, false);
if (NIL_P(error)) {
- error = pm_parse_file(result, filepath);
+ error = pm_parse_file(result, filepath, script_lines);
}
return error;
@@ -10477,27 +11609,43 @@ pm_load_parse_file(pm_parse_result_t *result, VALUE filepath)
* error is returned.
*/
VALUE
-pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath)
+pm_parse_string(pm_parse_result_t *result, VALUE source, VALUE filepath, VALUE *script_lines)
{
rb_encoding *encoding = rb_enc_get(source);
if (!rb_enc_asciicompat(encoding)) {
return rb_exc_new_cstr(rb_eArgError, "invalid source encoding");
}
- pm_options_frozen_string_literal_init(&result->options);
- pm_string_constant_init(&result->input, RSTRING_PTR(source), RSTRING_LEN(source));
- pm_options_encoding_set(&result->options, rb_enc_name(encoding));
+ pm_options_frozen_string_literal_init(result->options);
+ result->source = pm_source_constant_new((const uint8_t *) RSTRING_PTR(source), (size_t) RSTRING_LEN(source));
+ pm_options_encoding_set(result->options, rb_enc_name(encoding));
result->node.filepath_encoding = rb_enc_get(filepath);
- pm_options_filepath_set(&result->options, RSTRING_PTR(filepath));
+ pm_options_filepath_set(result->options, RSTRING_PTR(filepath));
RB_GC_GUARD(filepath);
- pm_parser_init(&result->parser, pm_string_source(&result->input), pm_string_length(&result->input), &result->options);
- pm_node_t *node = pm_parse(&result->parser);
+ pm_options_version_for_current_ruby_set(result->options);
- return pm_parse_process(result, node);
+ result->parser = pm_parser_new(result->arena, pm_source_source(result->source), pm_source_length(result->source), result->options);
+ pm_node_t *node = pm_parse(result->parser);
+
+ return pm_parse_process(result, node, script_lines);
}
+struct rb_stdin_wrapper {
+ VALUE rb_stdin;
+ int eof_seen;
+};
+
+static int
+pm_parse_stdin_eof(void *stream)
+{
+ struct rb_stdin_wrapper * wrapped_stdin = (struct rb_stdin_wrapper *)stream;
+ return wrapped_stdin->eof_seen;
+}
+
+VALUE rb_io_gets_limit_internal(VALUE io, long limit);
+
/**
* An implementation of fgets that is suitable for use with Ruby IO objects.
*/
@@ -10506,7 +11654,9 @@ pm_parse_stdin_fgets(char *string, int size, void *stream)
{
RUBY_ASSERT(size > 0);
- VALUE line = rb_funcall((VALUE) stream, rb_intern("gets"), 1, INT2FIX(size - 1));
+ struct rb_stdin_wrapper * wrapped_stdin = (struct rb_stdin_wrapper *)stream;
+
+ VALUE line = rb_io_gets_limit_internal(wrapped_stdin->rb_stdin, size - 1);
if (NIL_P(line)) {
return NULL;
}
@@ -10517,9 +11667,19 @@ pm_parse_stdin_fgets(char *string, int size, void *stream)
memcpy(string, cstr, length);
string[length] = '\0';
+ // We're reading strings from stdin via gets. We'll assume that if the
+ // string is smaller than the requested length, and doesn't end with a
+ // newline, that we hit EOF.
+ if (length < (size - 1) && string[length - 1] != '\n') {
+ wrapped_stdin->eof_seen = 1;
+ }
+
return string;
}
+// We need access to this function when we're done parsing stdin.
+void rb_reset_argf_lineno(long n);
+
/**
* Parse the source off STDIN and store the resulting scope node in the given
* parse result struct. It is assumed that the parse result object is zeroed
@@ -10528,17 +11688,29 @@ pm_parse_stdin_fgets(char *string, int size, void *stream)
VALUE
pm_parse_stdin(pm_parse_result_t *result)
{
- pm_options_frozen_string_literal_init(&result->options);
+ pm_options_frozen_string_literal_init(result->options);
- pm_buffer_t buffer;
- pm_node_t *node = pm_parse_stream(&result->parser, &buffer, (void *) rb_stdin, pm_parse_stdin_fgets, &result->options);
+ struct rb_stdin_wrapper wrapped_stdin = {
+ rb_stdin,
+ 0
+ };
+
+ result->source = pm_source_stream_new((void *) &wrapped_stdin, pm_parse_stdin_fgets, pm_parse_stdin_eof);
+ pm_node_t *node = pm_parse_stream(&result->parser, result->arena, result->source, result->options);
+
+ // When we're done parsing, we reset $. because we don't want the fact that
+ // we went through an IO object to be visible to the user.
+ rb_reset_argf_lineno(0);
+
+ return pm_parse_process(result, node, NULL);
+}
- // Copy the allocated buffer contents into the input string so that it gets
- // freed. At this point we've handed over ownership, so we don't need to
- // free the buffer itself.
- pm_string_owned_init(&result->input, (uint8_t *) pm_buffer_value(&buffer), pm_buffer_length(&buffer));
+#define PM_VERSION_FOR_RELEASE(major, minor) PM_VERSION_FOR_RELEASE_IMPL(major, minor)
+#define PM_VERSION_FOR_RELEASE_IMPL(major, minor) #major "." #minor
- return pm_parse_process(result, node);
+void pm_options_version_for_current_ruby_set(pm_options_t *options) {
+ const char *version = PM_VERSION_FOR_RELEASE(RUBY_API_VERSION_MAJOR, RUBY_API_VERSION_MINOR);
+ pm_options_version_set(options, version, strlen(version));
}
#undef NEW_ISEQ