Skip to content

Commit

Permalink
[PRISM] Fix more bugs in the compiler (ruby#8658)
Browse files Browse the repository at this point in the history
* Fixed ConstantPathWriteNode
* FIxed FlipFlopNode
  • Loading branch information
jemmaissroff authored Oct 16, 2023
1 parent 6e88b72 commit 728286d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
50 changes: 26 additions & 24 deletions prism_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,6 @@ parse_string_symbol(pm_string_t *string)
return parse_symbol(start, start + pm_string_length(string));
}

static inline ID
parse_location_symbol(pm_location_t *location)
{
return parse_symbol(location->start, location->end);
}

static int
pm_optimizable_range_item_p(pm_node_t *node)
{
Expand Down Expand Up @@ -940,7 +934,9 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
}

// TODO: Not sure this is accurate, look at FLUSH_CHUNK in the compiler
ADD_INSN1(ret, &dummy_line_node, newarraykwsplat, INT2FIX(0));
if (!popped) {
ADD_INSN1(ret, &dummy_line_node, newarraykwsplat, INT2FIX(0));
}

PM_POP_IF_POPPED;
return;
Expand Down Expand Up @@ -1202,13 +1198,21 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
}
case PM_CONSTANT_PATH_WRITE_NODE: {
pm_constant_path_write_node_t *constant_path_write_node = (pm_constant_path_write_node_t*) node;
PM_COMPILE(constant_path_write_node->value);
PM_DUP_UNLESS_POPPED;

ID constant_var_name = parse_location_symbol(&constant_path_write_node->target->base.location);

ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
ADD_INSN1(ret, &dummy_line_node, setconstant, ID2SYM(constant_var_name));
if (constant_path_write_node->target->parent) {
PM_COMPILE_NOT_POPPED((pm_node_t *)constant_path_write_node->target->parent);
}
else {
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
}
PM_COMPILE_NOT_POPPED(constant_path_write_node->value);
if (!popped) {
ADD_INSN(ret, &dummy_line_node, swap);
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
}
ADD_INSN(ret, &dummy_line_node, swap);
VALUE constant_name = ID2SYM(pm_constant_id_lookup(compile_context,
((pm_constant_read_node_t *)constant_path_write_node->target->child)->name));
ADD_INSN1(ret, &dummy_line_node, setconstant, constant_name);
return;
}
case PM_CONSTANT_READ_NODE: {
Expand Down Expand Up @@ -1375,34 +1379,31 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
return;
}
case PM_FLIP_FLOP_NODE: {
// TODO: The labels here are wrong, figure out why.....
pm_flip_flop_node_t *flip_flop_node = (pm_flip_flop_node_t *)node;

LABEL *lend = NEW_LABEL(lineno);
LABEL *final_label = NEW_LABEL(lineno);
LABEL *then_label = NEW_LABEL(lineno);
LABEL *else_label = NEW_LABEL(lineno);
//TODO: int again = type == NODE_FLIP2;
int again = 0;
int again = !(flip_flop_node->base.flags & PM_RANGE_FLAGS_EXCLUDE_END);

rb_num_t cnt = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq)
+ VM_SVAR_FLIPFLOP_START;
VALUE key = INT2FIX(cnt);
rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
VALUE key = INT2FIX(count);

ADD_INSN2(ret, &dummy_line_node, getspecial, key, INT2FIX(0));
ADD_INSNL(ret, &dummy_line_node, branchif, lend);

if (flip_flop_node->left) {
PM_COMPILE(flip_flop_node->left);
}
/* *flip == 0 */

ADD_INSNL(ret, &dummy_line_node, branchunless, else_label);
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
ADD_INSN1(ret, &dummy_line_node, setspecial, key);
if (!again) {
ADD_INSNL(ret, &dummy_line_node, jump, then_label);
}

/* *flip == 1 */
ADD_LABEL(ret, lend);
if (flip_flop_node->right) {
PM_COMPILE(flip_flop_node->right);
Expand All @@ -1412,12 +1413,13 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse);
ADD_INSN1(ret, &dummy_line_node, setspecial, key);
ADD_INSNL(ret, &dummy_line_node, jump, then_label);

ADD_LABEL(ret, then_label);
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
ADD_INSNL(ret, &dummy_line_node, jump, lend);
ADD_INSNL(ret, &dummy_line_node, jump, final_label);
ADD_LABEL(ret, else_label);
ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse);
ADD_LABEL(ret, lend);
ADD_LABEL(ret, final_label);
return;
}
case PM_FLOAT_NODE: {
Expand Down
8 changes: 7 additions & 1 deletion test/ruby/test_compile_prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def test_ConstantPathTargetNode
end

def test_ConstantPathWriteNode
# test_prism_eval("Prism::YCT = 1")
test_prism_eval("Prism::CPWN = 1")
test_prism_eval("::CPWN = 1")
end

def test_GlobalVariableTargetNode
Expand Down Expand Up @@ -355,6 +356,11 @@ def test_ElseNode
test_prism_eval("false ? 0 : 1")
end

def test_FlipFlopNode
test_prism_eval("not (1 == 1) .. (2 == 2)")
test_prism_eval("not (1 == 1) ... (2 == 2)")
end

############################################################################
# Calls / arugments #
############################################################################
Expand Down

0 comments on commit 728286d

Please sign in to comment.