From 39207b496eb1f45750bc664c8796f5aa3f5d641e Mon Sep 17 00:00:00 2001 From: Jemma Issroff Date: Mon, 23 Oct 2023 14:49:39 -0300 Subject: [PATCH] [PRISM] Implement compilation for PreExecutionNodes --- prism_compile.c | 34 ++++++++++++++++++--------------- test/ruby/test_compile_prism.rb | 12 ++++++++++++ test_code.rb | 3 +++ 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/prism_compile.c b/prism_compile.c index b9d17cd957..d9432a0a51 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -303,11 +303,11 @@ pm_static_literal_value(const pm_node_t *node, pm_scope_node_t *scope_node, pm_p } static void -pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, const pm_node_t *cond, +pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond, LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node); static void -pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *ret, pm_node_t *cond, +pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond, LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node) { pm_parser_t *parser = scope_node->parser; @@ -338,7 +338,7 @@ pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *ret, pm_node_t *cond, return; } -static void pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node); +static void pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node); static void pm_compile_flip_flop(pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LABEL *then_label, rb_iseq_t *iseq, const int lineno, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node) @@ -383,7 +383,7 @@ pm_compile_flip_flop(pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LAB } static void -pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, const pm_node_t *cond, +pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond, LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node) { pm_parser_t *parser = scope_node->parser; @@ -439,7 +439,7 @@ again: } static void -pm_compile_if(rb_iseq_t *iseq, const int line, pm_statements_node_t *node_body, pm_node_t *node_else, pm_node_t *predicate, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node) +pm_compile_if(rb_iseq_t *iseq, const int line, pm_statements_node_t *node_body, pm_node_t *node_else, pm_node_t *predicate, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node) { NODE dummy_line_node = generate_dummy_line_node(line, line); @@ -499,7 +499,7 @@ pm_compile_if(rb_iseq_t *iseq, const int line, pm_statements_node_t *node_body, } static void -pm_compile_while(rb_iseq_t *iseq, int lineno, pm_node_flags_t flags, enum pm_node_type type, pm_statements_node_t *statements, pm_node_t *predicate, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node) +pm_compile_while(rb_iseq_t *iseq, int lineno, pm_node_flags_t flags, enum pm_node_type type, pm_statements_node_t *statements, pm_node_t *predicate, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node) { NODE dummy_line_node = generate_dummy_line_node(lineno, lineno); @@ -570,7 +570,7 @@ pm_compile_while(rb_iseq_t *iseq, int lineno, pm_node_flags_t flags, enum pm_nod } static void -pm_interpolated_node_compile(pm_node_list_t parts, rb_iseq_t *iseq, NODE dummy_line_node, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, pm_parser_t *parser) +pm_interpolated_node_compile(pm_node_list_t parts, rb_iseq_t *iseq, NODE dummy_line_node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, pm_parser_t *parser) { size_t parts_size = parts.size; @@ -647,7 +647,7 @@ pm_new_child_iseq(rb_iseq_t *iseq, pm_scope_node_t node, pm_parser_t *parser, } static int -pm_compile_class_path(LINK_ANCHOR *ret, rb_iseq_t *iseq, const pm_node_t *constant_path_node, const NODE *line_node, const uint8_t * src, bool popped, pm_scope_node_t *scope_node) +pm_compile_class_path(LINK_ANCHOR *const ret, rb_iseq_t *iseq, const pm_node_t *constant_path_node, const NODE *line_node, const uint8_t * src, bool popped, pm_scope_node_t *scope_node) { if (PM_NODE_TYPE_P(constant_path_node, PM_CONSTANT_PATH_NODE)) { pm_node_t *parent = ((pm_constant_path_node_t *)constant_path_node)->parent; @@ -678,7 +678,7 @@ pm_compile_class_path(LINK_ANCHOR *ret, rb_iseq_t *iseq, const pm_node_t *consta * path). */ static uint8_t -pm_compile_multi_write_lhs(rb_iseq_t *iseq, NODE dummy_line_node, const pm_node_t *node, LINK_ANCHOR *ret, pm_scope_node_t *scope_node, uint8_t pushed, bool nested) +pm_compile_multi_write_lhs(rb_iseq_t *iseq, NODE dummy_line_node, const pm_node_t *node, LINK_ANCHOR *const ret, pm_scope_node_t *scope_node, uint8_t pushed, bool nested) { switch (PM_NODE_TYPE(node)) { case PM_MULTI_TARGET_NODE: { @@ -727,7 +727,7 @@ pm_compile_multi_write_lhs(rb_iseq_t *iseq, NODE dummy_line_node, const pm_node_ * Compile a pattern matching expression. */ static int -pm_compile_pattern(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const uint8_t *src, pm_scope_node_t *scope_node, LABEL *matched_label, LABEL *unmatched_label, bool in_alternation_pattern) +pm_compile_pattern(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, pm_scope_node_t *scope_node, LABEL *matched_label, LABEL *unmatched_label, bool in_alternation_pattern) { int lineno = (int) pm_newline_list_line_column(&scope_node->parser->newline_list, node->location.start).line; NODE dummy_line_node = generate_dummy_line_node(lineno, lineno); @@ -904,7 +904,7 @@ pm_compile_pattern(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, con * scope_node - Stores parser and local information */ static void -pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node) +pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node) { pm_parser_t *parser = scope_node->parser; pm_newline_list_t newline_list = parser->newline_list; @@ -2253,10 +2253,14 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const ADD_INSN(pre_ex, &dummy_line_node, putnil); } - ADD_SEQ(pre_ex, ret); pre_ex->last->next = ret->anchor.next; - ret->anchor = pre_ex->anchor; - fprintf(stderr, "hello\n"); + ret->anchor.next = pre_ex->anchor.next; + ret->anchor.next->prev = pre_ex->anchor.next; + + if (ret->last == (LINK_ELEMENT *)ret) { + ret->last = pre_ex->last; + } + return; } case PM_PROGRAM_NODE: { @@ -2642,7 +2646,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *ret, const } static VALUE -rb_translate_prism(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, LINK_ANCHOR *ret) +rb_translate_prism(rb_iseq_t *iseq, const pm_scope_node_t *scope_node, LINK_ANCHOR *const ret) { RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq)); diff --git a/test/ruby/test_compile_prism.rb b/test/ruby/test_compile_prism.rb index 74efd36ce2..acda2f5d49 100644 --- a/test/ruby/test_compile_prism.rb +++ b/test/ruby/test_compile_prism.rb @@ -535,6 +535,18 @@ module Prism test_prism_eval("(1)") end + def test_PreExecutionNode + # BEGIN {} must be defined at the top level, so we need to manually + # call the evals here instead of calling `test_prism_eval` + ruby_eval = RubyVM::InstructionSequence.compile("BEGIN { a = 1 }; 2").eval + prism_eval = RubyVM::InstructionSequence.compile_prism("BEGIN { a = 1 }; 2").eval + assert_equal ruby_eval, prism_eval + + ruby_eval = RubyVM::InstructionSequence.compile("b = 2; BEGIN { a = 1 }; a + b").eval + prism_eval = RubyVM::InstructionSequence.compile_prism("b = 2; BEGIN { a = 1 }; a + b").eval + assert_equal ruby_eval, prism_eval + end + def test_ProgramNode test_prism_eval("") test_prism_eval("1") diff --git a/test_code.rb b/test_code.rb index 7e23bb0af9..bec656d460 100644 --- a/test_code.rb +++ b/test_code.rb @@ -1 +1,4 @@ BEGIN { a } +b + +