| 1 |
|
| #ifndef CONTEXT_VARS_SCOPE_H |
| 2 |
|
| #define CONTEXT_VARS_SCOPE_H |
| 3 |
|
| |
| 4 |
|
| /* Sub scope: an assignment to an imported variable holds until the sub it is |
| 5 |
|
| * written in returns. |
| 6 |
|
| * |
| 7 |
|
| * "Written in" is lexical - the innermost sub whose text holds the |
| 8 |
|
| * assignment - so it is settled when that sub is COMPILED. perl hands every |
| 9 |
|
| * sub to its optimiser once, when the sub is complete; a hook there looks |
| 10 |
|
| * for a write to one of our scalars, and a sub that has one is given a scope |
| 11 |
|
| * that opens each time the sub is entered. The scope is an ordinary entry |
| 12 |
|
| * on the sub's own save stack, so it closes however the sub is left: by |
| 13 |
|
| * falling off the end, by return from any depth of block, by die, by goto |
| 14 |
|
| * &other, by last out of a caller's loop. |
| 15 |
|
| * |
| 16 |
|
| * A sub with no write is left exactly as perl built it. |
| 17 |
|
| * |
| 18 |
|
| * plan_context_vars/02-scope-spike.md has the evidence for each choice below, |
| 19 |
|
| * on eighteen perls from 5.10.1 to 5.44.0. */ |
| 20 |
|
| |
| 21 |
|
| #ifndef OpSIBLING |
| 22 |
|
| # define OpSIBLING(o) ((o)->op_sibling) |
| 23 |
|
| #endif |
| 24 |
|
| #ifndef isGV_with_GP |
| 25 |
|
| # define isGV_with_GP(sv) (SvTYPE(sv) == SVt_PVGV && GvGP((GV *)(sv))) |
| 26 |
|
| #endif |
| 27 |
|
| |
| 28 |
|
| /* ---- opening the scope ----------------------------------------------------- */ |
| 29 |
|
| |
| 30 |
2 |
| static OP *context_vars_pp_scope(pTHX) { |
| 31 |
2 |
| context_vars_scope(aTHX); |
| 32 |
2 |
| return NORMAL; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
|
| /* The usual way: the sub's own first statement marker opens the scope and |
| 36 |
|
| * then does what it always did. Nothing is added to the op tree, so the |
| 37 |
|
| * tools that walk it - B::Deparse, B::Concise - see the sub as it was |
| 38 |
|
| * written, and nothing is allocated. */ |
| 39 |
5364 |
| static OP *context_vars_pp_nextstate(pTHX) { |
| 40 |
5364 |
| context_vars_scope(aTHX); |
| 41 |
5364 |
| return PL_ppaddr[OP_NEXTSTATE](aTHX); |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
1 |
| static OP *context_vars_pp_dbstate(pTHX) { |
| 45 |
1 |
| context_vars_scope(aTHX); |
| 46 |
1 |
| return PL_ppaddr[OP_DBSTATE](aTHX); |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
345 |
| static OP *context_vars_first_real(OP *o) { |
| 50 |
345 |
▶ | while (o && o->op_type == OP_NULL) o = o->op_next; |
Branch: 2 branches
| Branch | True | False | Total |
| while (o && o->op_type == OP_NULL) o = o->op_next; |
345 |
0 |
345 |
| while (o && o->op_type == OP_NULL) o = o->op_next; |
0 |
345 |
345 |
|
| 51 |
345 |
| return o; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
|
| /* 1 when the sub opens a scope with its first statement, 2 when with an op |
| 55 |
|
| * of its own, 0 when it opens none. */ |
| 56 |
201 |
| static int context_vars_scope_kind(CV *cv) { |
| 57 |
|
| OP *t; |
| 58 |
201 |
▶ | if (!cv || CvISXSUB(cv) || !CvSTART(cv)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cv || CvISXSUB(cv) || !CvSTART(cv)) return 0; |
201 |
0 |
201 |
| if (!cv || CvISXSUB(cv) || !CvSTART(cv)) return 0; |
201 |
0 |
201 |
| if (!cv || CvISXSUB(cv) || !CvSTART(cv)) return 0; |
0 |
201 |
201 |
|
| 59 |
201 |
▶ | if (CvSTART(cv)->op_ppaddr == context_vars_pp_scope) return 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (CvSTART(cv)->op_ppaddr == context_vars_pp_scope) return 2; |
1 |
200 |
201 |
|
| 60 |
200 |
| t = context_vars_first_real(CvSTART(cv)); |
| 61 |
200 |
▶ | if (t && (t->op_ppaddr == context_vars_pp_nextstate |
Branch: 2 branches
| Branch | True | False | Total |
| if (t && (t->op_ppaddr == context_vars_pp_nextstate |
200 |
0 |
200 |
| if (t && (t->op_ppaddr == context_vars_pp_nextstate |
162 |
38 |
200 |
|
| 62 |
162 |
▶ | || t->op_ppaddr == context_vars_pp_dbstate)) |
Branch: 1 branch
| Branch | True | False | Total |
| || t->op_ppaddr == context_vars_pp_dbstate)) |
1 |
161 |
162 |
|
| 63 |
39 |
| return 1; |
| 64 |
161 |
| return 0; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
|
| #ifdef XopENTRY_set |
| 68 |
|
| static XOP context_vars_xop; |
| 69 |
|
| #endif |
| 70 |
|
| |
| 71 |
42 |
| static void context_vars_register_op(pTHX) { |
| 72 |
|
| #ifdef XopENTRY_set |
| 73 |
42 |
| XopENTRY_set(&context_vars_xop, xop_name, "context_vars_scope"); |
| 74 |
42 |
| XopENTRY_set(&context_vars_xop, xop_desc, "open a Context::Vars scope"); |
| 75 |
42 |
| XopENTRY_set(&context_vars_xop, xop_class, OA_BASEOP); |
| 76 |
42 |
| Perl_custom_op_register(aTHX_ context_vars_pp_scope, &context_vars_xop); |
| 77 |
|
| #else |
| 78 |
|
| SV *key = sv_2mortal(newSViv(PTR2IV(context_vars_pp_scope))); |
| 79 |
|
| if (!PL_custom_op_names) PL_custom_op_names = newHV(); |
| 80 |
|
| if (!PL_custom_op_descs) PL_custom_op_descs = newHV(); |
| 81 |
|
| (void)hv_store_ent(PL_custom_op_names, key, newSVpv("context_vars_scope", 0), 0); |
| 82 |
|
| (void)hv_store_ent(PL_custom_op_descs, key, newSVpv("open a Context::Vars scope", 0), 0); |
| 83 |
|
| #endif |
| 84 |
42 |
| } |
| 85 |
|
| |
| 86 |
|
| /* Gives the sub its scope. The first statement marker is used unless it has |
| 87 |
|
| * a label - a goto to the label would open the scope a second time - or the |
| 88 |
|
| * sub does not begin with one. Then a new op goes in front, into the op tree |
| 89 |
|
| * as well as the run order so that it is freed with the sub. */ |
| 90 |
145 |
| static void context_vars_plant(pTHX_ CV *cv) { |
| 91 |
145 |
| OP *t = context_vars_first_real(CvSTART(cv)); |
| 92 |
|
| OP *root, *body, *s; |
| 93 |
|
| |
| 94 |
145 |
▶ | if (t && (t->op_type == OP_NEXTSTATE || t->op_type == OP_DBSTATE) |
Branch: 3 branches
| Branch | True | False | Total |
| if (t && (t->op_type == OP_NEXTSTATE || t->op_type == OP_DBSTATE) |
145 |
0 |
145 |
| if (t && (t->op_type == OP_NEXTSTATE || t->op_type == OP_DBSTATE) |
1 |
144 |
145 |
| if (t && (t->op_type == OP_NEXTSTATE || t->op_type == OP_DBSTATE) |
1 |
0 |
1 |
|
| 95 |
145 |
▶ | && !CopLABEL((COP *)t)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && !CopLABEL((COP *)t)) { |
143 |
2 |
145 |
|
| 96 |
286 |
| t->op_ppaddr = (t->op_type == OP_NEXTSTATE) |
| 97 |
143 |
▶ | ? context_vars_pp_nextstate : context_vars_pp_dbstate; |
Branch: 1 branch
| Branch | True | False | Total |
| ? context_vars_pp_nextstate : context_vars_pp_dbstate; |
142 |
1 |
143 |
|
| 98 |
143 |
| return; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
2 |
| root = CvROOT(cv); |
| 102 |
2 |
▶ | body = (root->op_flags & OPf_KIDS) ? cUNOPx(root)->op_first : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| body = (root->op_flags & OPf_KIDS) ? cUNOPx(root)->op_first : NULL; |
2 |
0 |
2 |
|
| 103 |
2 |
▶ | if (!body || body->op_type != OP_LINESEQ || !(body->op_flags & OPf_KIDS)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!body || body->op_type != OP_LINESEQ || !(body->op_flags & OPf_KIDS)) |
2 |
0 |
2 |
| if (!body || body->op_type != OP_LINESEQ || !(body->op_flags & OPf_KIDS)) |
2 |
0 |
2 |
| if (!body || body->op_type != OP_LINESEQ || !(body->op_flags & OPf_KIDS)) |
0 |
2 |
2 |
|
| 104 |
0 |
| return; /* nowhere to hang an op: the sub goes unscoped */ |
| 105 |
|
| |
| 106 |
2 |
| s = newOP(OP_CUSTOM, 0); |
| 107 |
2 |
| s->op_ppaddr = context_vars_pp_scope; |
| 108 |
2 |
| s->op_next = CvSTART(cv); |
| 109 |
2 |
| CvSTART(cv) = s; |
| 110 |
|
| #if PERL_VERSION >= 22 |
| 111 |
2 |
| op_sibling_splice(body, NULL, 0, s); |
| 112 |
|
| #else |
| 113 |
|
| s->op_sibling = cLISTOPx(body)->op_first; |
| 114 |
|
| cLISTOPx(body)->op_first = s; |
| 115 |
|
| #endif |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| /* ---- finding a write ------------------------------------------------------- */ |
| 119 |
|
| |
| 120 |
|
| /* Is there anywhere in this tree a write to one of our scalars? Run BEFORE |
| 121 |
|
| * perl's own optimiser: until then a package scalar is an rv2sv over a gv |
| 122 |
|
| * and still carries the flags the compiler gave it, and the optimiser fuses |
| 123 |
|
| * the two and drops them. The whole tree is walked, not the run order, which |
| 124 |
|
| * would miss every branch. |
| 125 |
|
| * |
| 126 |
|
| * A write is OPf_MOD together with OPf_REF, or OPpLVAL_INTRO. That is every |
| 127 |
|
| * kind of assignment, ++ and --, chomp, chop, undef, s///, tr/// and local. |
| 128 |
|
| * It is also \$x, so a sub that takes a reference to the variable is given |
| 129 |
|
| * a scope. A variable passed as an argument is OPf_MOD alone, and is not. |
| 130 |
|
| * |
| 131 |
|
| * Part of a sub may have been optimised already: perl 5.14 and older |
| 132 |
|
| * optimise a map or grep block as soon as it is parsed, before the sub |
| 133 |
|
| * around it is finished. There the rv2sv is nulled and the gv beneath it |
| 134 |
|
| * fused into a gvsv - but the nulled op keeps its flags, so it is read the |
| 135 |
|
| * same way. */ |
| 136 |
|
| /* Ext magic with this vtable, by hand: mg_findext is 5.14. */ |
| 137 |
7987 |
| static int context_vars_has_ext(SV *sv, const MGVTBL *vt) { |
| 138 |
|
| MAGIC *mg; |
| 139 |
7987 |
▶ | if (!sv || SvTYPE(sv) < SVt_PVMG) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv || SvTYPE(sv) < SVt_PVMG) return 0; |
4405 |
3582 |
7987 |
| if (!sv || SvTYPE(sv) < SVt_PVMG) return 0; |
0 |
4405 |
4405 |
|
| 140 |
4445 |
▶ | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) |
73 |
4372 |
4445 |
|
| 141 |
73 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vt) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vt) return 1; |
33 |
40 |
73 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vt) return 1; |
33 |
0 |
33 |
|
| 142 |
4372 |
| return 0; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| /* Is a glob one of ours: a scalar carrying our magic, or an array or hash |
| 146 |
|
| * carrying the container mark? */ |
| 147 |
4010 |
| static int context_vars_gv_ours(pTHX_ GV *gv) { |
| 148 |
4010 |
▶ | if (!gv || !isGV_with_GP(gv)) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!gv || !isGV_with_GP(gv)) return 0; |
4010 |
0 |
4010 |
| if (!gv || !isGV_with_GP(gv)) return 0; |
4010 |
0 |
4010 |
| if (!gv || !isGV_with_GP(gv)) return 0; |
0 |
4010 |
4010 |
| if (!gv || !isGV_with_GP(gv)) return 0; |
0 |
0 |
0 |
|
| 149 |
4010 |
▶ | if (context_vars_var_mg(GvSV(gv))) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_var_mg(GvSV(gv))) return 1; |
5 |
4005 |
4010 |
|
| 150 |
4005 |
▶ | if (context_vars_has_ext((SV *)GvHV(gv), &context_vars_container_vtbl)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_has_ext((SV *)GvHV(gv), &context_vars_container_vtbl)) return 1; |
23 |
3982 |
4005 |
|
| 151 |
3982 |
▶ | if (context_vars_has_ext((SV *)GvAV(gv), &context_vars_container_vtbl)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_has_ext((SV *)GvAV(gv), &context_vars_container_vtbl)) return 1; |
10 |
3972 |
3982 |
|
| 152 |
3972 |
| return 0; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| /* Follows an lvalue down its first children - element, dereference, the |
| 156 |
|
| * nulled forms the optimiser leaves - to the glob at its root, and says |
| 157 |
|
| * whether that is one of ours. */ |
| 158 |
10304 |
| static int context_vars_rooted(pTHX_ OP *o) { |
| 159 |
10304 |
| int guard = 0; |
| 160 |
39701 |
▶ | while (o && guard++ < 256) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (o && guard++ < 256) { |
39701 |
0 |
39701 |
| while (o && guard++ < 256) { |
39701 |
0 |
39701 |
|
| 161 |
39701 |
| switch (o->op_type) { |
| 162 |
4010 |
| case OP_GV: case OP_GVSV: |
| 163 |
4010 |
| return context_vars_gv_ours(aTHX_ cGVOPx_gv(o)); |
| 164 |
834 |
| case OP_PUSHMARK: |
| 165 |
834 |
▶ | o = OpSIBLING(o); |
Branch: 1 branch
| Branch | True | False | Total |
| o = OpSIBLING(o); |
834 |
0 |
834 |
|
| 166 |
834 |
| continue; |
| 167 |
81 |
| case OP_HSLICE: case OP_ASLICE: |
| 168 |
|
| /* A slice's container is its last child: the keys come first. */ |
| 169 |
81 |
▶ | if (!(o->op_flags & OPf_KIDS)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(o->op_flags & OPf_KIDS)) return 0; |
0 |
81 |
81 |
|
| 170 |
81 |
| o = cLISTOPx(o)->op_last; |
| 171 |
81 |
| continue; |
| 172 |
753 |
| case OP_SCOPE: case OP_LEAVE: case OP_LINESEQ: |
| 173 |
|
| /* A dereference written as a block, @{ $h{a} }, is a scope whose |
| 174 |
|
| * value is its last statement; the first is a dead nextstate. */ |
| 175 |
753 |
▶ | if (!(o->op_flags & OPf_KIDS)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(o->op_flags & OPf_KIDS)) return 0; |
0 |
753 |
753 |
|
| 176 |
753 |
| o = cLISTOPx(o)->op_last; |
| 177 |
753 |
| continue; |
| 178 |
27729 |
| case OP_HELEM: case OP_AELEM: case OP_RV2HV: case OP_RV2AV: case OP_RV2SV: |
| 179 |
|
| case OP_NULL: case OP_EXISTS: case OP_DELETE: case OP_LIST: |
| 180 |
27729 |
▶ | if (!(o->op_flags & OPf_KIDS)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(o->op_flags & OPf_KIDS)) return 0; |
0 |
27729 |
27729 |
|
| 181 |
27729 |
| o = cUNOPx(o)->op_first; |
| 182 |
27729 |
| continue; |
| 183 |
6294 |
| default: |
| 184 |
6294 |
| return 0; |
| 185 |
|
| } |
| 186 |
|
| } |
| 187 |
0 |
| return 0; |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| /* A write to an array, a hash, an element of one, or anything below one. |
| 191 |
|
| * |
| 192 |
|
| * perl marks some reads as modifications too, and those are left out: an |
| 193 |
|
| * element in the middle of a chain, $h{a} in $h{a}{b}, is marked so that it |
| 194 |
|
| * can be made if it is missing (OPpDEREF); an element passed to a sub is |
| 195 |
|
| * marked in case the sub assigns through @_ (OPpLVAL_DEFER), which the pass |
| 196 |
|
| * does not see for a scalar either. */ |
| 197 |
969791 |
| static int context_vars_deep_write(pTHX_ OP *o) { |
| 198 |
969791 |
| switch (o->op_type) { |
| 199 |
40193 |
| case OP_HELEM: case OP_AELEM: |
| 200 |
40193 |
▶ | if (o->op_private & (OPpDEREF | OPpLVAL_DEFER)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (o->op_private & (OPpDEREF | OPpLVAL_DEFER)) return 0; |
14153 |
26040 |
40193 |
|
| 201 |
26040 |
▶ | if ((o->op_flags & OPf_MOD) || (o->op_private & OPpLVAL_INTRO)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((o->op_flags & OPf_MOD) || (o->op_private & OPpLVAL_INTRO)) |
19115 |
6925 |
26040 |
| if ((o->op_flags & OPf_MOD) || (o->op_private & OPpLVAL_INTRO)) |
0 |
19115 |
19115 |
|
| 202 |
6925 |
| return context_vars_rooted(aTHX_ o); |
| 203 |
19115 |
| return 0; |
| 204 |
49954 |
| case OP_RV2HV: case OP_RV2AV: |
| 205 |
|
| case OP_HSLICE: case OP_ASLICE: |
| 206 |
49954 |
▶ | if ((o->op_flags & OPf_MOD) || (o->op_private & OPpLVAL_INTRO)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((o->op_flags & OPf_MOD) || (o->op_private & OPpLVAL_INTRO)) |
48360 |
1594 |
49954 |
| if ((o->op_flags & OPf_MOD) || (o->op_private & OPpLVAL_INTRO)) |
0 |
48360 |
48360 |
|
| 207 |
1594 |
| return context_vars_rooted(aTHX_ o); |
| 208 |
48360 |
| return 0; |
| 209 |
4303 |
| case OP_DELETE: case OP_PUSH: case OP_POP: case OP_SHIFT: case OP_UNSHIFT: case OP_SPLICE: |
| 210 |
4303 |
▶ | return (o->op_flags & OPf_KIDS) && context_vars_rooted(aTHX_ cUNOPx(o)->op_first); |
Branch: 2 branches
| Branch | True | False | Total |
| return (o->op_flags & OPf_KIDS) && context_vars_rooted(aTHX_ cUNOPx(o)->op_first); |
1785 |
2518 |
4303 |
| return (o->op_flags & OPf_KIDS) && context_vars_rooted(aTHX_ cUNOPx(o)->op_first); |
10 |
1775 |
1785 |
|
| 211 |
875341 |
| default: |
| 212 |
875341 |
| return 0; |
| 213 |
|
| } |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
|
| /* Ops whose container operand perl marks as modified for its own purposes, |
| 217 |
|
| * not because it is written: keys, values and each reset an iterator, and |
| 218 |
|
| * foreach, grep and map make aliases. A write through those aliases is one |
| 219 |
|
| * the pass does not see, as for a scalar. */ |
| 220 |
975666 |
| static int context_vars_reads_container(OP *o) { |
| 221 |
975666 |
▶ | switch (o->op_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (o->op_type) { |
5875 |
969791 |
975666 |
|
| 222 |
5875 |
| case OP_KEYS: case OP_VALUES: case OP_EACH: |
| 223 |
|
| case OP_ENTERITER: case OP_GREPSTART: case OP_MAPSTART: |
| 224 |
5875 |
| return 1; |
| 225 |
969791 |
| default: |
| 226 |
969791 |
| return 0; |
| 227 |
|
| } |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
|
| static int context_vars_writes(pTHX_ OP *o); |
| 231 |
|
| |
| 232 |
|
| /* The kids of such an op, with any array or hash among them - directly, or |
| 233 |
|
| * inside the list perl wraps them in - not counted as written, and everything |
| 234 |
|
| * below them looked at as usual. */ |
| 235 |
11858 |
| static int context_vars_writes_under_reader(pTHX_ OP *o) { |
| 236 |
|
| OP *k, *g; |
| 237 |
11858 |
▶ | if (!(o->op_flags & OPf_KIDS)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(o->op_flags & OPf_KIDS)) return 0; |
0 |
11858 |
11858 |
|
| 238 |
32428 |
▶ | for (k = cUNOPx(o)->op_first; k; k = OpSIBLING(k)) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (k = cUNOPx(o)->op_first; k; k = OpSIBLING(k)) { |
8721 |
11849 |
20570 |
| for (k = cUNOPx(o)->op_first; k; k = OpSIBLING(k)) { |
20579 |
11849 |
32428 |
|
| 239 |
20579 |
▶ | if (k->op_type == OP_RV2AV || k->op_type == OP_RV2HV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (k->op_type == OP_RV2AV || k->op_type == OP_RV2HV) { |
19470 |
1109 |
20579 |
| if (k->op_type == OP_RV2AV || k->op_type == OP_RV2HV) { |
1513 |
17957 |
19470 |
|
| 240 |
2622 |
▶ | if (k->op_flags & OPf_KIDS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (k->op_flags & OPf_KIDS) |
2622 |
0 |
2622 |
|
| 241 |
5244 |
▶ | for (g = cUNOPx(k)->op_first; g; g = OpSIBLING(g)) |
Branch: 2 branches
| Branch | True | False | Total |
| for (g = cUNOPx(k)->op_first; g; g = OpSIBLING(g)) |
0 |
2622 |
2622 |
| for (g = cUNOPx(k)->op_first; g; g = OpSIBLING(g)) |
2622 |
2622 |
5244 |
|
| 242 |
2622 |
▶ | if (context_vars_writes(aTHX_ g)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_writes(aTHX_ g)) return 1; |
0 |
2622 |
2622 |
|
| 243 |
|
| } |
| 244 |
17957 |
▶ | else if ((k->op_type == OP_LIST || k->op_type == OP_NULL) && (k->op_flags & OPf_KIDS)) { |
Branch: 3 branches
| Branch | True | False | Total |
| else if ((k->op_type == OP_LIST || k->op_type == OP_NULL) && (k->op_flags & OPf_KIDS)) { |
17957 |
0 |
17957 |
| else if ((k->op_type == OP_LIST || k->op_type == OP_NULL) && (k->op_flags & OPf_KIDS)) { |
8082 |
9875 |
17957 |
| else if ((k->op_type == OP_LIST || k->op_type == OP_NULL) && (k->op_flags & OPf_KIDS)) { |
5983 |
2099 |
8082 |
|
| 245 |
5983 |
▶ | if (context_vars_writes_under_reader(aTHX_ k)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_writes_under_reader(aTHX_ k)) return 1; |
6 |
5977 |
5983 |
|
| 246 |
|
| } |
| 247 |
11974 |
▶ | else if (context_vars_writes(aTHX_ k)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (context_vars_writes(aTHX_ k)) return 1; |
3 |
11971 |
11974 |
|
| 248 |
|
| } |
| 249 |
11849 |
| return 0; |
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
975666 |
| static int context_vars_writes(pTHX_ OP *o) { |
| 253 |
|
| OP *k; |
| 254 |
975666 |
▶ | if (!o) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o) return 0; |
0 |
975666 |
975666 |
|
| 255 |
975666 |
▶ | if (context_vars_reads_container(o)) return context_vars_writes_under_reader(aTHX_ o); |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_reads_container(o)) return context_vars_writes_under_reader(aTHX_ o); |
5875 |
969791 |
975666 |
|
| 256 |
969791 |
▶ | if (context_vars_deep_write(aTHX_ o)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_deep_write(aTHX_ o)) return 1; |
38 |
969753 |
969791 |
|
| 257 |
969753 |
▶ | if ((o->op_type == OP_RV2SV |
Branch: 1 branch
| Branch | True | False | Total |
| if ((o->op_type == OP_RV2SV |
960790 |
8963 |
969753 |
|
| 258 |
960790 |
▶ | || (o->op_type == OP_NULL && o->op_targ == OP_RV2SV)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (o->op_type == OP_NULL && o->op_targ == OP_RV2SV)) |
122997 |
837793 |
960790 |
| || (o->op_type == OP_NULL && o->op_targ == OP_RV2SV)) |
0 |
122997 |
122997 |
|
| 259 |
8963 |
▶ | && (o->op_flags & OPf_KIDS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && (o->op_flags & OPf_KIDS)) { |
8963 |
0 |
8963 |
|
| 260 |
8963 |
| k = cUNOPx(o)->op_first; |
| 261 |
8963 |
▶ | if (k && (k->op_type == OP_GV || k->op_type == OP_GVSV)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (k && (k->op_type == OP_GV || k->op_type == OP_GVSV)) { |
8963 |
0 |
8963 |
| if (k && (k->op_type == OP_GV || k->op_type == OP_GVSV)) { |
699 |
8264 |
8963 |
| if (k && (k->op_type == OP_GV || k->op_type == OP_GVSV)) { |
0 |
699 |
699 |
|
| 262 |
8264 |
| GV *gv = cGVOPx_gv(k); |
| 263 |
8264 |
▶ | if (gv && isGV_with_GP(gv) && context_vars_var_mg(GvSV(gv)) |
Branch: 5 branches
| Branch | True | False | Total |
| if (gv && isGV_with_GP(gv) && context_vars_var_mg(GvSV(gv)) |
8264 |
0 |
8264 |
| if (gv && isGV_with_GP(gv) && context_vars_var_mg(GvSV(gv)) |
8264 |
0 |
8264 |
| if (gv && isGV_with_GP(gv) && context_vars_var_mg(GvSV(gv)) |
0 |
8264 |
8264 |
| if (gv && isGV_with_GP(gv) && context_vars_var_mg(GvSV(gv)) |
0 |
0 |
0 |
| if (gv && isGV_with_GP(gv) && context_vars_var_mg(GvSV(gv)) |
173 |
8091 |
8264 |
|
| 264 |
173 |
▶ | && (o->op_flags & OPf_MOD) |
Branch: 1 branch
| Branch | True | False | Total |
| && (o->op_flags & OPf_MOD) |
122 |
51 |
173 |
|
| 265 |
122 |
▶ | && ((o->op_flags & OPf_REF) || (o->op_private & OPpLVAL_INTRO))) |
Branch: 2 branches
| Branch | True | False | Total |
| && ((o->op_flags & OPf_REF) || (o->op_private & OPpLVAL_INTRO))) |
15 |
107 |
122 |
| && ((o->op_flags & OPf_REF) || (o->op_private & OPpLVAL_INTRO))) |
0 |
15 |
15 |
|
| 266 |
107 |
| return 1; |
| 267 |
|
| } |
| 268 |
|
| } |
| 269 |
969646 |
▶ | if (o->op_flags & OPf_KIDS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (o->op_flags & OPf_KIDS) |
450689 |
518957 |
969646 |
|
| 270 |
1394524 |
▶ | for (k = cUNOPx(o)->op_first; k; k = OpSIBLING(k)) |
Branch: 2 branches
| Branch | True | False | Total |
| for (k = cUNOPx(o)->op_first; k; k = OpSIBLING(k)) |
493655 |
450180 |
943835 |
| for (k = cUNOPx(o)->op_first; k; k = OpSIBLING(k)) |
944344 |
450180 |
1394524 |
|
| 271 |
944344 |
▶ | if (context_vars_writes(aTHX_ k)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_writes(aTHX_ k)) return 1; |
509 |
943835 |
944344 |
|
| 272 |
969137 |
| return 0; |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
|
| /* BEGIN and its kind are never given a scope: an assignment there is meant |
| 276 |
|
| * for the program. */ |
| 277 |
24327 |
| static int context_vars_special_block(pTHX_ CV *cv) { |
| 278 |
24327 |
| const char *n = NULL; |
| 279 |
|
| #ifdef CvNAMED |
| 280 |
24327 |
▶ | if (CvNAMED(cv)) n = HEK_KEY(CvNAME_HEK(cv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (CvNAMED(cv)) n = HEK_KEY(CvNAME_HEK(cv)); |
136 |
24191 |
24327 |
|
| 281 |
|
| else |
| 282 |
|
| #endif |
| 283 |
24191 |
▶ | if (CvGV(cv)) n = GvNAME(CvGV(cv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (CvGV(cv)) n = GvNAME(CvGV(cv)); |
24191 |
0 |
24191 |
|
| 284 |
24327 |
▶ | if (!n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return 0; |
0 |
24327 |
24327 |
|
| 285 |
16762 |
▶ | return strEQ(n, "BEGIN") || strEQ(n, "END") || strEQ(n, "INIT") |
Branch: 2 branches
| Branch | True | False | Total |
| return strEQ(n, "BEGIN") || strEQ(n, "END") || strEQ(n, "INIT") |
16726 |
36 |
16762 |
| return strEQ(n, "BEGIN") || strEQ(n, "END") || strEQ(n, "INIT") |
16726 |
0 |
16726 |
|
| 286 |
41089 |
▶ | || strEQ(n, "CHECK") || strEQ(n, "UNITCHECK"); |
Branch: 3 branches
| Branch | True | False | Total |
| || strEQ(n, "CHECK") || strEQ(n, "UNITCHECK"); |
16762 |
7565 |
24327 |
| || strEQ(n, "CHECK") || strEQ(n, "UNITCHECK"); |
16726 |
0 |
16726 |
| || strEQ(n, "CHECK") || strEQ(n, "UNITCHECK"); |
0 |
16726 |
16726 |
|
| 287 |
|
| } |
| 288 |
|
| |
| 289 |
|
| /* ---- the optimiser hook ------------------------------------------------------ */ |
| 290 |
|
| |
| 291 |
|
| static peep_t context_vars_prev_peep = NULL; |
| 292 |
|
| |
| 293 |
|
| /* perl calls the optimiser for a finished sub with that sub's first op, while |
| 294 |
|
| * the sub is still PL_compcv. Anything else it is called for - file code, a |
| 295 |
|
| * string eval - is passed straight on. */ |
| 296 |
27817 |
| static void context_vars_peep(pTHX_ OP *o) { |
| 297 |
27817 |
| CV *cv = PL_compcv; |
| 298 |
27817 |
| int plant = 0; |
| 299 |
|
| |
| 300 |
27817 |
▶ | if (o && cv && SvTYPE((SV *)cv) == SVt_PVCV && !CvISXSUB(cv) |
Branch: 4 branches
| Branch | True | False | Total |
| if (o && cv && SvTYPE((SV *)cv) == SVt_PVCV && !CvISXSUB(cv) |
27817 |
0 |
27817 |
| if (o && cv && SvTYPE((SV *)cv) == SVt_PVCV && !CvISXSUB(cv) |
27817 |
0 |
27817 |
| if (o && cv && SvTYPE((SV *)cv) == SVt_PVCV && !CvISXSUB(cv) |
27817 |
0 |
27817 |
| if (o && cv && SvTYPE((SV *)cv) == SVt_PVCV && !CvISXSUB(cv) |
27817 |
0 |
27817 |
|
| 301 |
27817 |
▶ | && CvROOT(cv) && CvSTART(cv) == o |
Branch: 2 branches
| Branch | True | False | Total |
| && CvROOT(cv) && CvSTART(cv) == o |
24327 |
3490 |
27817 |
| && CvROOT(cv) && CvSTART(cv) == o |
24327 |
0 |
24327 |
|
| 302 |
24327 |
▶ | && !context_vars_special_block(aTHX_ cv)) |
Branch: 1 branch
| Branch | True | False | Total |
| && !context_vars_special_block(aTHX_ cv)) |
16726 |
7601 |
24327 |
|
| 303 |
16726 |
| plant = context_vars_writes(aTHX_ CvROOT(cv)); |
| 304 |
|
| |
| 305 |
27817 |
| context_vars_prev_peep(aTHX_ o); |
| 306 |
|
| |
| 307 |
27817 |
▶ | if (plant && CvSTART(cv) && !context_vars_scope_kind(cv)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (plant && CvSTART(cv) && !context_vars_scope_kind(cv)) |
145 |
27672 |
27817 |
| if (plant && CvSTART(cv) && !context_vars_scope_kind(cv)) |
145 |
0 |
145 |
| if (plant && CvSTART(cv) && !context_vars_scope_kind(cv)) |
145 |
0 |
145 |
|
| 308 |
145 |
| context_vars_plant(aTHX_ cv); |
| 309 |
27817 |
| } |
| 310 |
|
| |
| 311 |
|
| /* Once for the process. The hook is chained, so another module's hook, |
| 312 |
|
| * installed before or after, still runs. */ |
| 313 |
42 |
| static void context_vars_scope_boot(pTHX) { |
| 314 |
42 |
| context_vars_register_op(aTHX); |
| 315 |
42 |
▶ | if (!context_vars_prev_peep) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!context_vars_prev_peep) { |
42 |
0 |
42 |
|
| 316 |
42 |
| context_vars_prev_peep = PL_peepp; |
| 317 |
42 |
| PL_peepp = context_vars_peep; |
| 318 |
|
| } |
| 319 |
42 |
| } |
| 320 |
|
| |
| 321 |
|
| #endif |