| 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 |
1326 |
| static OP *context_vars_pp_nextstate(pTHX) { |
| 40 |
1326 |
| context_vars_scope(aTHX); |
| 41 |
1326 |
| 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 |
243 |
| static OP *context_vars_first_real(OP *o) { |
| 50 |
243 |
▶ | 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; |
243 |
0 |
243 |
| while (o && o->op_type == OP_NULL) o = o->op_next; |
0 |
243 |
243 |
|
| 51 |
243 |
| 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 |
139 |
| static int context_vars_scope_kind(CV *cv) { |
| 57 |
|
| OP *t; |
| 58 |
139 |
▶ | if (!cv || CvISXSUB(cv) || !CvSTART(cv)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cv || CvISXSUB(cv) || !CvSTART(cv)) return 0; |
139 |
0 |
139 |
| if (!cv || CvISXSUB(cv) || !CvSTART(cv)) return 0; |
139 |
0 |
139 |
| if (!cv || CvISXSUB(cv) || !CvSTART(cv)) return 0; |
0 |
139 |
139 |
|
| 59 |
139 |
▶ | 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 |
138 |
139 |
|
| 60 |
138 |
| t = context_vars_first_real(CvSTART(cv)); |
| 61 |
138 |
▶ | 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 |
138 |
0 |
138 |
| if (t && (t->op_ppaddr == context_vars_pp_nextstate |
120 |
18 |
138 |
|
| 62 |
120 |
▶ | || t->op_ppaddr == context_vars_pp_dbstate)) |
Branch: 1 branch
| Branch | True | False | Total |
| || t->op_ppaddr == context_vars_pp_dbstate)) |
1 |
119 |
120 |
|
| 63 |
19 |
| return 1; |
| 64 |
119 |
| return 0; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
|
| #ifdef XopENTRY_set |
| 68 |
|
| static XOP context_vars_xop; |
| 69 |
|
| #endif |
| 70 |
|
| |
| 71 |
37 |
| static void context_vars_register_op(pTHX) { |
| 72 |
|
| #ifdef XopENTRY_set |
| 73 |
37 |
| XopENTRY_set(&context_vars_xop, xop_name, "context_vars_scope"); |
| 74 |
37 |
| XopENTRY_set(&context_vars_xop, xop_desc, "open a Context::Vars scope"); |
| 75 |
37 |
| XopENTRY_set(&context_vars_xop, xop_class, OA_BASEOP); |
| 76 |
37 |
| 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 |
37 |
| } |
| 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 |
105 |
| static void context_vars_plant(pTHX_ CV *cv) { |
| 91 |
105 |
| OP *t = context_vars_first_real(CvSTART(cv)); |
| 92 |
|
| OP *root, *body, *s; |
| 93 |
|
| |
| 94 |
105 |
▶ | 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) |
105 |
0 |
105 |
| if (t && (t->op_type == OP_NEXTSTATE || t->op_type == OP_DBSTATE) |
1 |
104 |
105 |
| if (t && (t->op_type == OP_NEXTSTATE || t->op_type == OP_DBSTATE) |
1 |
0 |
1 |
|
| 95 |
105 |
▶ | && !CopLABEL((COP *)t)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && !CopLABEL((COP *)t)) { |
103 |
2 |
105 |
|
| 96 |
206 |
| t->op_ppaddr = (t->op_type == OP_NEXTSTATE) |
| 97 |
103 |
▶ | ? context_vars_pp_nextstate : context_vars_pp_dbstate; |
Branch: 1 branch
| Branch | True | False | Total |
| ? context_vars_pp_nextstate : context_vars_pp_dbstate; |
102 |
1 |
103 |
|
| 98 |
103 |
| 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 |
888332 |
| static int context_vars_writes(pTHX_ OP *o) { |
| 137 |
|
| OP *k; |
| 138 |
888332 |
▶ | if (!o) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o) return 0; |
0 |
888332 |
888332 |
|
| 139 |
888332 |
▶ | if ((o->op_type == OP_RV2SV |
Branch: 1 branch
| Branch | True | False | Total |
| if ((o->op_type == OP_RV2SV |
879581 |
8751 |
888332 |
|
| 140 |
879581 |
▶ | || (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)) |
118451 |
761130 |
879581 |
| || (o->op_type == OP_NULL && o->op_targ == OP_RV2SV)) |
0 |
118451 |
118451 |
|
| 141 |
8751 |
▶ | && (o->op_flags & OPf_KIDS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && (o->op_flags & OPf_KIDS)) { |
8751 |
0 |
8751 |
|
| 142 |
8751 |
| k = cUNOPx(o)->op_first; |
| 143 |
8751 |
▶ | 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)) { |
8751 |
0 |
8751 |
| if (k && (k->op_type == OP_GV || k->op_type == OP_GVSV)) { |
704 |
8047 |
8751 |
| if (k && (k->op_type == OP_GV || k->op_type == OP_GVSV)) { |
0 |
704 |
704 |
|
| 144 |
8047 |
| GV *gv = cGVOPx_gv(k); |
| 145 |
8047 |
▶ | 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)) |
8047 |
0 |
8047 |
| if (gv && isGV_with_GP(gv) && context_vars_var_mg(GvSV(gv)) |
8047 |
0 |
8047 |
| if (gv && isGV_with_GP(gv) && context_vars_var_mg(GvSV(gv)) |
0 |
8047 |
8047 |
| 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)) |
171 |
7876 |
8047 |
|
| 146 |
171 |
▶ | && (o->op_flags & OPf_MOD) |
Branch: 1 branch
| Branch | True | False | Total |
| && (o->op_flags & OPf_MOD) |
120 |
51 |
171 |
|
| 147 |
120 |
▶ | && ((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 |
105 |
120 |
| && ((o->op_flags & OPf_REF) || (o->op_private & OPpLVAL_INTRO))) |
0 |
15 |
15 |
|
| 148 |
105 |
| return 1; |
| 149 |
|
| } |
| 150 |
|
| } |
| 151 |
888227 |
▶ | if (o->op_flags & OPf_KIDS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (o->op_flags & OPf_KIDS) |
422037 |
466190 |
888227 |
|
| 152 |
1294880 |
▶ | 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)) |
451198 |
421645 |
872843 |
| for (k = cUNOPx(o)->op_first; k; k = OpSIBLING(k)) |
873235 |
421645 |
1294880 |
|
| 153 |
873235 |
▶ | if (context_vars_writes(aTHX_ k)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (context_vars_writes(aTHX_ k)) return 1; |
392 |
872843 |
873235 |
|
| 154 |
887835 |
| return 0; |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
|
| /* BEGIN and its kind are never given a scope: an assignment there is meant |
| 158 |
|
| * for the program. */ |
| 159 |
22094 |
| static int context_vars_special_block(pTHX_ CV *cv) { |
| 160 |
22094 |
| const char *n = NULL; |
| 161 |
|
| #ifdef CvNAMED |
| 162 |
22094 |
▶ | 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)); |
119 |
21975 |
22094 |
|
| 163 |
|
| else |
| 164 |
|
| #endif |
| 165 |
21975 |
▶ | if (CvGV(cv)) n = GvNAME(CvGV(cv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (CvGV(cv)) n = GvNAME(CvGV(cv)); |
21975 |
0 |
21975 |
|
| 166 |
22094 |
▶ | if (!n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return 0; |
0 |
22094 |
22094 |
|
| 167 |
15128 |
▶ | 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") |
15097 |
31 |
15128 |
| return strEQ(n, "BEGIN") || strEQ(n, "END") || strEQ(n, "INIT") |
15097 |
0 |
15097 |
|
| 168 |
37222 |
▶ | || strEQ(n, "CHECK") || strEQ(n, "UNITCHECK"); |
Branch: 3 branches
| Branch | True | False | Total |
| || strEQ(n, "CHECK") || strEQ(n, "UNITCHECK"); |
15128 |
6966 |
22094 |
| || strEQ(n, "CHECK") || strEQ(n, "UNITCHECK"); |
15097 |
0 |
15097 |
| || strEQ(n, "CHECK") || strEQ(n, "UNITCHECK"); |
0 |
15097 |
15097 |
|
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
|
| /* ---- the optimiser hook ------------------------------------------------------ */ |
| 172 |
|
| |
| 173 |
|
| static peep_t context_vars_prev_peep = NULL; |
| 174 |
|
| |
| 175 |
|
| /* perl calls the optimiser for a finished sub with that sub's first op, while |
| 176 |
|
| * the sub is still PL_compcv. Anything else it is called for - file code, a |
| 177 |
|
| * string eval - is passed straight on. */ |
| 178 |
25234 |
| static void context_vars_peep(pTHX_ OP *o) { |
| 179 |
25234 |
| CV *cv = PL_compcv; |
| 180 |
25234 |
| int plant = 0; |
| 181 |
|
| |
| 182 |
25234 |
▶ | 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) |
25234 |
0 |
25234 |
| if (o && cv && SvTYPE((SV *)cv) == SVt_PVCV && !CvISXSUB(cv) |
25234 |
0 |
25234 |
| if (o && cv && SvTYPE((SV *)cv) == SVt_PVCV && !CvISXSUB(cv) |
25234 |
0 |
25234 |
| if (o && cv && SvTYPE((SV *)cv) == SVt_PVCV && !CvISXSUB(cv) |
25234 |
0 |
25234 |
|
| 183 |
25234 |
▶ | && CvROOT(cv) && CvSTART(cv) == o |
Branch: 2 branches
| Branch | True | False | Total |
| && CvROOT(cv) && CvSTART(cv) == o |
22094 |
3140 |
25234 |
| && CvROOT(cv) && CvSTART(cv) == o |
22094 |
0 |
22094 |
|
| 184 |
22094 |
▶ | && !context_vars_special_block(aTHX_ cv)) |
Branch: 1 branch
| Branch | True | False | Total |
| && !context_vars_special_block(aTHX_ cv)) |
15097 |
6997 |
22094 |
|
| 185 |
15097 |
| plant = context_vars_writes(aTHX_ CvROOT(cv)); |
| 186 |
|
| |
| 187 |
25234 |
| context_vars_prev_peep(aTHX_ o); |
| 188 |
|
| |
| 189 |
25234 |
▶ | 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)) |
105 |
25129 |
25234 |
| if (plant && CvSTART(cv) && !context_vars_scope_kind(cv)) |
105 |
0 |
105 |
| if (plant && CvSTART(cv) && !context_vars_scope_kind(cv)) |
105 |
0 |
105 |
|
| 190 |
105 |
| context_vars_plant(aTHX_ cv); |
| 191 |
25234 |
| } |
| 192 |
|
| |
| 193 |
|
| /* Once for the process. The hook is chained, so another module's hook, |
| 194 |
|
| * installed before or after, still runs. */ |
| 195 |
37 |
| static void context_vars_scope_boot(pTHX) { |
| 196 |
37 |
| context_vars_register_op(aTHX); |
| 197 |
37 |
▶ | if (!context_vars_prev_peep) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!context_vars_prev_peep) { |
37 |
0 |
37 |
|
| 198 |
37 |
| context_vars_prev_peep = PL_peepp; |
| 199 |
37 |
| PL_peepp = context_vars_peep; |
| 200 |
|
| } |
| 201 |
37 |
| } |
| 202 |
|
| |
| 203 |
|
| #endif |