Skip to content

Commit

Permalink
no force boolean context for some defined-or ops
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Nov 20, 2024
1 parent c88e713 commit 61c92e4
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 40 deletions.
38 changes: 23 additions & 15 deletions Cover.xs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,9 @@ static void set_conditional(pTHX_ OP *op, int cond, int value) {

static void add_conditional(pTHX_ OP *op, int cond) {
SV **count = av_fetch(get_conditional_array(aTHX_ op), cond, 1);
int c = SvTRUE(*count) ? SvIV(*count) + 1 : 1;
int true_ish = (op->op_type == OP_DOR || op->op_type == OP_DORASSIGN)
? SvOK(*count) : SvTRUE(*count);
int c = true_ish ? SvIV(*count) + 1 : 1;
sv_setiv(*count, c);
NDEB(D(L, "Adding %d conditional making %d at %p\n", cond, c, op));
}
Expand Down Expand Up @@ -586,14 +588,16 @@ static void add_condition(pTHX_ SV *cond_ref, int value) {
for (; i <= av_len(conds); i++) {
OP *op = INT2PTR(OP *, SvIV(*av_fetch(conds, i, 0)));
SV **count = av_fetch(get_conditional_array(aTHX_ op), 0, 1);
int type = SvTRUE(*count) ? SvIV(*count) : 0;
int true_ish = (op->op_type == OP_DOR || op->op_type == OP_DORASSIGN)
? SvOK(*count) : SvTRUE(*count);
int type = true_ish ? SvIV(*count) : 0;
sv_setiv(*count, 0);

/* Check if we have come from an xor with a true first op */
if (final) value = 1;
if (type == 1) value += 2;

NDEB(D(L, "Found %p: %d, %d\n", op, type, value));
NDEB(D(L, "Found %p (trueish=%d): %d, %d\n", op, true_ish, type, value));
add_conditional(aTHX_ op, value);
}

Expand Down Expand Up @@ -706,11 +710,15 @@ static OP *get_condition(pTHX) {

if (pc && SvROK(*pc)) {
dSP;
int true_ish;
NDEB(D(L, "get_condition from %p, %p: %p (%s)\n",
PL_op, (void *)PL_op->op_targ, pc, hex_key(get_key(PL_op))));
/* dump_conditions(aTHX); */
NDEB(svdump(Pending_conditionals));
add_condition(aTHX_ *pc, SvTRUE(TOPs) ? 2 : 1);
true_ish = (PL_op->op_type == OP_DOR || PL_op->op_type == OP_DORASSIGN)
? SvOK(TOPs) : SvTRUE(TOPs);
NDEB(D(L, " get_condition true_ish=%d\n", true_ish));
add_condition(aTHX_ *pc, true_ish ? 2 : 1);
} else {
PDEB(D(L, "All is lost, I know not where to go from %p, %p: %p (%s)\n",
PL_op, (void *)PL_op->op_targ, pc, hex_key(get_key(PL_op))));
Expand Down Expand Up @@ -802,26 +810,26 @@ static void cover_logop(pTHX) {
} else {
dSP;

int left_val = SvTRUE(TOPs);
int left_val_def = SvOK(TOPs);
int leftval_true_ish = (PL_op->op_type == OP_DOR || PL_op->op_type == OP_DORASSIGN)
? SvOK(TOPs) : SvTRUE(TOPs);
/* We don't count X= as void context because we care about the value
* of the RHS */
int void_context = GIMME_V == G_VOID &&
PL_op->op_type != OP_DORASSIGN &&
PL_op->op_type != OP_ANDASSIGN &&
PL_op->op_type != OP_ORASSIGN;
NDEB(D(L, "left_val: %d, void_context: %d at %p\n",
left_val, void_context, PL_op));
NDEB(D(L, "leftval_true_ish: %d, void_context: %d at %p\n",
leftval_true_ish, void_context, PL_op));
NDEB(op_dump(PL_op));

set_conditional(aTHX_ PL_op, 5, void_context);

if ((PL_op->op_type == OP_AND && left_val) ||
(PL_op->op_type == OP_ANDASSIGN && left_val) ||
(PL_op->op_type == OP_OR && !left_val) ||
(PL_op->op_type == OP_ORASSIGN && !left_val) ||
(PL_op->op_type == OP_DOR && !left_val_def) ||
(PL_op->op_type == OP_DORASSIGN && !left_val_def) ||
if ((PL_op->op_type == OP_AND && leftval_true_ish) ||
(PL_op->op_type == OP_ANDASSIGN && leftval_true_ish) ||
(PL_op->op_type == OP_OR && !leftval_true_ish) ||
(PL_op->op_type == OP_ORASSIGN && !leftval_true_ish) ||
(PL_op->op_type == OP_DOR && !leftval_true_ish) ||
(PL_op->op_type == OP_DORASSIGN && !leftval_true_ish) ||
(PL_op->op_type == OP_XOR)) {
/* no short circuit */

Expand Down Expand Up @@ -852,7 +860,7 @@ static void cover_logop(pTHX) {
*cond;
OP *next;

if (PL_op->op_type == OP_XOR && left_val) {
if (PL_op->op_type == OP_XOR && leftval_true_ish) {
/*
* This is an xor. It does not short circuit. We
* have just executed the first op. When we get to
Expand Down
40 changes: 20 additions & 20 deletions test_output/cover/cond_or.5.012000
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,26 @@ or 2 conditions

line err % l !l expr
----- --- ------ ------ ------ ----
16 *** 50 11 0 $$x[18] //= undef
17 *** 50 11 0 $$x[18] //= 0
18 *** 50 11 0 $$x[18] //= 0
19 *** 50 11 0 $$x[18] //= 1
20 *** 50 11 0 $$x[18] //= 1
22 100 10 1 $$x[19] //= 1
23 *** 50 11 0 $$x[19] //= 1
24 *** 50 11 0 $$x[19] //= 0
25 *** 50 11 0 $$x[19] //= undef
26 *** 50 11 0 $$x[19] //= 1
28 *** 50 0 11 $$x[21] // undef
29 *** 50 0 11 $$x[21] // 0
30 *** 50 0 11 $$x[21] // 0
31 *** 50 0 11 $$x[21] // 1
32 *** 50 0 11 $$x[21] // 1
34 100 10 1 $$x[22] // undef
35 100 10 1 $$x[22] // 0
36 *** 50 11 0 $$x[22] // 0
37 *** 50 11 0 $$x[22] // 1
38 *** 50 11 0 $$x[22] // 1
16 *** 50 1 0 $$x[18] //= undef
17 *** 50 1 0 $$x[18] //= 0
18 *** 50 1 0 $$x[18] //= 0
19 *** 50 1 0 $$x[18] //= 1
20 *** 50 1 0 $$x[18] //= 1
22 100 1 1 $$x[19] //= 1
23 *** 50 1 0 $$x[19] //= 1
24 *** 50 1 0 $$x[19] //= 0
25 *** 50 1 0 $$x[19] //= undef
26 *** 50 1 0 $$x[19] //= 1
28 *** 50 0 1 $$x[21] // undef
29 *** 50 0 1 $$x[21] // 0
30 *** 50 0 1 $$x[21] // 0
31 *** 50 0 1 $$x[21] // 1
32 *** 50 0 1 $$x[21] // 1
34 100 1 1 $$x[22] // undef
35 100 1 1 $$x[22] // 0
36 *** 50 1 0 $$x[22] // 0
37 *** 50 1 0 $$x[22] // 1
38 *** 50 1 0 $$x[22] // 1


Covered Subroutines
Expand Down
Loading

0 comments on commit 61c92e4

Please sign in to comment.