autovivification.xs

filter: Statementstmt 62.2% 398 / 639 Branchbran 52.4% 169 / 322 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 59.0% 567 / 961 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/autovivification.pm ↑ Summary xsh/mem.h »
1 /* This file is part of the autovivification Perl module.
2 * See http://search.cpan.org/dist/autovivification/ */
3  
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8  
9 /* --- XS helpers ---------------------------------------------------------- */
10  
11 #define XSH_PACKAGE "autovivification"
12  
13 #include "xsh/caps.h"
14 #include "xsh/util.h"
15 #include "xsh/ops.h"
16 #include "xsh/peep.h"
17  
18 /* ... Lexical hints ....................................................... */
19  
20 /* Used both for hints and op flags */
21 #define A_HINT_STRICT 1
22 #define A_HINT_WARN 2
23 #define A_HINT_FETCH 4
24 #define A_HINT_STORE 8
25 #define A_HINT_KEYS 16
26 #define A_HINT_VALUES 32
27 #define A_HINT_EXISTS 64
28 #define A_HINT_DELETE 128
29 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
30 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_KEYS|A_HINT_VALUES|A_HINT_EXISTS|A_HINT_DELETE)
31 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
32  
33 /* Only used in op flags */
34 #define A_HINT_ROOT 256
35 #define A_HINT_SECOND 512
36 #define A_HINT_DEREF 1024
37  
38 #define XSH_HINTS_TYPE_UV 1
39  
40 #include "xsh/hints.h"
41  
42 #define a_hint() xsh_hints_detag(xsh_hints_fetch())
43  
44 /* ... Thread-local storage ................................................ */
45  
46 #define XSH_THREADS_COMPILE_TIME_PROTECTION 1
47 #define XSH_THREADS_USER_CONTEXT 0
48  
49 #include "xsh/threads.h"
50  
51 /* --- Compatibility wrappers ---------------------------------------------- */
52  
53 #ifndef HvNAME_get
54 # define HvNAME_get(H) HvNAME(H)
55 #endif
56  
57 #ifndef HvNAMELEN_get
58 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
59 #endif
60  
61 #ifndef A_HAS_MULTIDEREF
62 # define A_HAS_MULTIDEREF XSH_HAS_PERL(5, 21, 7)
63 #endif
64  
65 #ifndef A_HAS_SCALARKEYS_OPT
66 # define A_HAS_SCALARKEYS_OPT XSH_HAS_PERL(5, 27, 3)
67 #endif
68  
69 /* ... Our vivify_ref() .................................................... */
70  
71 /* Perl_vivify_ref() is not exported, so we have to reimplement it. */
72  
73 #if A_HAS_MULTIDEREF
74  
75 0 static SV *a_vivify_ref(pTHX_ SV *sv, int to_hash) {
76 #define a_vivify_ref(S, TH) a_vivify_ref(aTHX_ (S), (TH))
77 0 SvGETMAGIC(sv);
78  
79 0 ▶ if (!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { 0 0 0
80 SV *val;
81  
82 0 ▶ if (SvREADONLY(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvREADONLY(sv)) 0 0 0
83 0 Perl_croak_no_modify();
84  
85 /* Inlined prepare_SV_for_RV() */
86 0 ▶ if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV) { 0 0 0
if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV) { 0 0 0
87 0 sv_upgrade(sv, SVt_IV);
88 0 ▶ } else if (SvTYPE(sv) >= SVt_PV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(sv) >= SVt_PV) { 0 0 0
89 0 ▶ SvPV_free(sv);
Branch: 12 branches
BranchTrueFalseTotal
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
SvPV_free(sv); 0 0 0
90 0 SvLEN_set(sv, 0);
91 0 SvCUR_set(sv, 0);
92 }
93  
94 0 ▶ val = to_hash ? MUTABLE_SV(newHV()) : MUTABLE_SV(newAV());
Branch: 1 branch
BranchTrueFalseTotal
val = to_hash ? MUTABLE_SV(newHV()) : MUTABLE_SV(newAV()); 0 0 0
95 0 SvRV_set(sv, val);
96 0 SvROK_on(sv);
97 0 ▶ SvSETMAGIC(sv);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(sv); 0 0 0
98 0 SvGETMAGIC(sv);
99 }
100  
101 0 ▶ if (SvGMAGICAL(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(sv)) { 0 0 0
102 0 SV *msv = sv_newmortal();
103 0 sv_setsv_nomg(msv, sv);
104 0 return msv;
105 }
106  
107 0 return sv;
108 }
109  
110 #endif /* A_HAS_MULTIDEREF */
111  
112 /* --- op => info map ------------------------------------------------------ */
113  
114 typedef struct {
115 OP *(*old_pp)(pTHX);
116 void *next;
117 UV flags;
118 } a_op_info;
119  
120 #define PTABLE_NAME ptable_map
121 #define PTABLE_VAL_FREE(V) XSH_SHARED_FREE((V), 1, a_op_info)
122 #define PTABLE_VAL_NEED_CONTEXT 0
123 #define PTABLE_NEED_DELETE 1
124 #define PTABLE_NEED_WALK 0
125  
126 #include "xsh/ptable.h"
127  
128 #define ptable_map_store(T, K, V) ptable_map_store(aPMS_ (T), (K), (V))
129 #define ptable_map_delete(T, K) ptable_map_delete(aPMS_ (T), (K))
130 #define ptable_map_free(T) ptable_map_free(aPMS_ (T))
131  
132 static ptable *a_op_map = NULL;
133  
134 #ifdef USE_ITHREADS
135  
136 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
137  
138 static perl_mutex a_op_map_mutex;
139  
140 static const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
141 const a_op_info *val;
142  
143 XSH_LOCK(&a_op_map_mutex);
144  
145 val = ptable_fetch(a_op_map, o);
146 if (val) {
147 *oi = *val;
148 val = oi;
149 }
150  
151 XSH_UNLOCK(&a_op_map_mutex);
152  
153 return val;
154 }
155  
156 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
157  
158 #else /* USE_ITHREADS */
159  
160 #define dA_MAP_THX dNOOP
161  
162 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
163  
164 #endif /* !USE_ITHREADS */
165  
166 145277 static const a_op_info *a_map_store_locked(pPMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
167 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPMS_ (O), (PP), (N), (F))
168 a_op_info *oi;
169  
170 145277 ▶ if (!(oi = ptable_fetch(a_op_map, o))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(oi = ptable_fetch(a_op_map, o))) { 127901 17376 145277
171 127901 XSH_SHARED_ALLOC(oi, 1, a_op_info);
172 127901 ptable_map_store(a_op_map, o, oi);
173 }
174  
175 145277 oi->old_pp = old_pp;
176 145277 oi->next = next;
177 145277 oi->flags = flags;
178  
179 145277 return oi;
180 }
181  
182 33269 static void a_map_store(pTHX_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
183 #define a_map_store(O, PP, N, F) a_map_store(aTHX_ (O), (PP), (N), (F))
184 XSH_LOCK(&a_op_map_mutex);
185  
186 33269 a_map_store_locked(o, old_pp, next, flags);
187  
188 XSH_UNLOCK(&a_op_map_mutex);
189 33269 }
190  
191 361944 static void a_map_delete(pTHX_ const OP *o) {
192 #define a_map_delete(O) a_map_delete(aTHX_ (O))
193 XSH_LOCK(&a_op_map_mutex);
194  
195 361944 ptable_map_delete(a_op_map, o);
196  
197 XSH_UNLOCK(&a_op_map_mutex);
198 361944 }
199  
200 137794 static const OP *a_map_descend(const OP *o) {
201 137794 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
202 136683 case OA_BASEOP:
203 case OA_UNOP:
204 case OA_BINOP:
205 case OA_BASEOP_OR_UNOP:
206 136683 return cUNOPo->op_first;
207 1111 case OA_LIST:
208 case OA_LISTOP:
209 1111 return cLISTOPo->op_last;
210 }
211  
212 0 return NULL;
213 }
214  
215 112008 static void a_map_store_root(pTHX_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
216 #define a_map_store_root(R, PP, F) a_map_store_root(aTHX_ (R), (PP), (F))
217 const a_op_info *roi;
218 a_op_info *oi;
219 112008 const OP *o = root;
220  
221 XSH_LOCK(&a_op_map_mutex);
222  
223 112008 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
224  
225 115372 ▶ while (o->op_flags & OPf_KIDS) {
Branch: 1 branch
BranchTrueFalseTotal
while (o->op_flags & OPf_KIDS) { 91587 23785 115372
226 91587 o = a_map_descend(o);
227 91587 ▶ if (!o)
Branch: 1 branch
BranchTrueFalseTotal
if (!o) 0 91587 91587
228 0 break;
229 91587 ▶ if ((oi = ptable_fetch(a_op_map, o))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((oi = ptable_fetch(a_op_map, o))) { 88223 3364 91587
230 88223 oi->flags &= ~A_HINT_ROOT;
231 88223 oi->next = (a_op_info *) roi;
232 88223 break;
233 }
234 }
235  
236 XSH_UNLOCK(&a_op_map_mutex);
237  
238 112008 return;
239 }
240  
241 8175 static void a_map_update_flags_topdown(const OP *root, UV mask, UV flags) {
242 a_op_info *oi;
243 8175 const OP *o = root;
244  
245 XSH_LOCK(&a_op_map_mutex);
246  
247 8175 mask |= A_HINT_ROOT;
248 8175 flags &= ~mask;
249  
250 do {
251 54382 ▶ if ((oi = ptable_fetch(a_op_map, o)))
Branch: 1 branch
BranchTrueFalseTotal
if ((oi = ptable_fetch(a_op_map, o))) 43943 10439 54382
252 43943 oi->flags = (oi->flags & mask) | flags;
253 54382 ▶ if (!(o->op_flags & OPf_KIDS))
Branch: 1 branch
BranchTrueFalseTotal
if (!(o->op_flags & OPf_KIDS)) 8175 46207 54382
254 8175 break;
255 46207 o = a_map_descend(o);
256 46207 ▶ } while (o);
Branch: 1 branch
BranchTrueFalseTotal
} while (o); 46207 0 46207
257  
258 XSH_UNLOCK(&a_op_map_mutex);
259  
260 8175 return;
261 }
262  
263 20433 static void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
264 a_op_info *oi;
265  
266 XSH_LOCK(&a_op_map_mutex);
267  
268 20433 flags &= ~A_HINT_ROOT;
269 20433 rflags |= A_HINT_ROOT;
270  
271 20433 oi = ptable_fetch(a_op_map, o);
272 48975 ▶ while (!(oi->flags & A_HINT_ROOT)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!(oi->flags & A_HINT_ROOT)) { 28542 20433 48975
273 28542 oi->flags = flags;
274 28542 oi = oi->next;
275 }
276 20433 oi->flags = rflags;
277  
278 XSH_UNLOCK(&a_op_map_mutex);
279  
280 20433 return;
281 }
282  
283 /* ... Decide whether this expression should be autovivified or not ........ */
284  
285 20433 static UV a_map_resolve(const OP *o, const a_op_info *oi) {
286 20433 UV flags = 0, rflags;
287 const OP *root;
288 20433 const a_op_info *roi = oi;
289  
290 48975 ▶ while (!(roi->flags & A_HINT_ROOT))
Branch: 1 branch
BranchTrueFalseTotal
while (!(roi->flags & A_HINT_ROOT)) 28542 20433 48975
291 28542 roi = roi->next;
292 20433 ▶ if (!roi)
Branch: 1 branch
BranchTrueFalseTotal
if (!roi) 0 20433 20433
293 0 goto cancel;
294  
295 20433 rflags = roi->flags & ~A_HINT_ROOT;
296 20433 ▶ if (!rflags)
Branch: 1 branch
BranchTrueFalseTotal
if (!rflags) 0 20433 20433
297 0 goto cancel;
298  
299 20433 root = roi->next;
300 20433 ▶ if (root->op_flags & OPf_MOD) {
Branch: 1 branch
BranchTrueFalseTotal
if (root->op_flags & OPf_MOD) { 4357 16076 20433
301 4357 ▶ if (rflags & A_HINT_STORE)
Branch: 1 branch
BranchTrueFalseTotal
if (rflags & A_HINT_STORE) 540 3817 4357
302 540 flags = (A_HINT_STORE|A_HINT_DEREF);
303 } else {
304 16076 ▶ if (rflags & (A_HINT_FETCH|A_HINT_KEYS|A_HINT_VALUES))
Branch: 1 branch
BranchTrueFalseTotal
if (rflags & (A_HINT_FETCH|A_HINT_KEYS|A_HINT_VALUES)) 14942 1134 16076
305 14942 flags = (rflags|A_HINT_DEREF);
306 }
307  
308 20433 ▶ if (!flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (!flags) { 4951 15482 20433
309 4951 cancel:
310 4951 a_map_update_flags_bottomup(o, 0, 0);
311 4951 return 0;
312 }
313  
314 15482 flags |= (rflags & A_HINT_NOTIFY);
315 15482 a_map_update_flags_bottomup(o, flags, 0);
316  
317 15482 ▶ return oi->flags & A_HINT_ROOT ? 0 : flags;
Branch: 1 branch
BranchTrueFalseTotal
return oi->flags & A_HINT_ROOT ? 0 : flags; 3051 12431 15482
318 }
319  
320 /* ... Inspired from pp_defined() .......................................... */
321  
322 31056 static int a_undef(pTHX_ SV *sv) {
323 #define a_undef(S) a_undef(aTHX_ (S))
324 31056 switch (SvTYPE(sv)) {
325 11281 case SVt_NULL:
326 11281 return 1;
327 3496 case SVt_PVAV:
328 3496 ▶ if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
Branch: 2 branches
BranchTrueFalseTotal
if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) 3172 324 3496
if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) 3172 0 3172
329 3172 ▶ || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
Branch: 2 branches
BranchTrueFalseTotal
|| (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied))) 0 3172 3172
|| (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied))) 0 0 0
330 324 return 0;
331 3172 break;
332 211 case SVt_PVHV:
333 211 ▶ if (HvARRAY(sv) || SvGMAGICAL(sv)
Branch: 2 branches
BranchTrueFalseTotal
if (HvARRAY(sv) || SvGMAGICAL(sv) 30 181 211
if (HvARRAY(sv) || SvGMAGICAL(sv) 30 0 30
334 30 ▶ || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
Branch: 2 branches
BranchTrueFalseTotal
|| (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied))) 0 30 30
|| (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied))) 0 0 0
335 181 return 0;
336 30 break;
337 16068 default:
338 16068 SvGETMAGIC(sv);
339 16068 ▶ if (SvOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv)) 3160 12908 16068
340 3160 return 0;
341 }
342  
343 16110 return 1;
344 }
345  
346 /* --- PP functions -------------------------------------------------------- */
347  
348 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
349 * value, another extension might have saved our pp replacement as the ppaddr
350 * for this op, so this doesn't ensure that our function will never be called
351 * again. That's why we don't remove the op info from our map, so that it can
352 * still run correctly if required. */
353  
354 /* ... pp_rv2av ............................................................ */
355  
356 11720 static OP *a_pp_rv2av(pTHX) {
357 dA_MAP_THX;
358 const a_op_info *oi;
359 11720 dSP;
360  
361 11720 oi = a_map_fetch(PL_op);
362  
363 11720 ▶ if (oi->flags & A_HINT_DEREF) {
Branch: 1 branch
BranchTrueFalseTotal
if (oi->flags & A_HINT_DEREF) { 11720 0 11720
364 11720 ▶ if (a_undef(TOPs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(TOPs)) { 10439 1281 11720
365 /* We always need to push an empty array to fool the pp_aelem() that comes
366 * later. */
367 SV *av;
368 10439 (void) POPs;
369 10439 av = sv_2mortal((SV *) newAV());
370 10439 PUSHs(av);
371 10439 RETURN;
372 }
373 }
374  
375 1281 return oi->old_pp(aTHX);
376 }
377  
378 /* ... pp_rv2hv ............................................................ */
379  
380 10824 static OP *a_pp_rv2hv_simple(pTHX) {
381 dA_MAP_THX;
382 const a_op_info *oi;
383 10824 dSP;
384  
385 10824 oi = a_map_fetch(PL_op);
386  
387 10824 ▶ if (oi->flags & A_HINT_DEREF) {
Branch: 1 branch
BranchTrueFalseTotal
if (oi->flags & A_HINT_DEREF) { 10824 0 10824
388 10824 ▶ if (a_undef(TOPs))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(TOPs)) 9791 1033 10824
389 9791 RETURN;
390 }
391  
392 1033 return oi->old_pp(aTHX);
393 }
394  
395 444 static OP *a_pp_rv2hv(pTHX) {
396 dA_MAP_THX;
397 const a_op_info *oi;
398 444 dSP;
399  
400 444 oi = a_map_fetch(PL_op);
401  
402 444 ▶ if (oi->flags & A_HINT_DEREF) {
Branch: 1 branch
BranchTrueFalseTotal
if (oi->flags & A_HINT_DEREF) { 444 0 444
403 444 ▶ if (a_undef(TOPs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(TOPs)) { 412 32 444
404 SV *hv;
405 412 (void) POPs;
406 412 hv = sv_2mortal((SV *) newHV());
407 412 PUSHs(hv);
408 412 RETURN;
409 }
410 }
411  
412 32 return oi->old_pp(aTHX);
413 }
414  
415 #if A_HAS_SCALARKEYS_OPT
416  
417 34 static OP *a_pp_rv2hv_dokeys(pTHX) {
418 dA_MAP_THX;
419 const a_op_info *oi;
420 34 dSP;
421  
422 34 oi = a_map_fetch(PL_op);
423  
424 34 ▶ if (oi->flags & A_HINT_KEYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (oi->flags & A_HINT_KEYS) { 34 0 34
425 34 ▶ if (a_undef(TOPs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(TOPs)) { 30 4 34
426 30 dTARGET;
427 30 (void) POPs;
428 30 ▶ PUSHi(0);
Branch: 1 branch
BranchTrueFalseTotal
PUSHi(0); 0 30 30
429 30 RETURN;
430 }
431 }
432  
433 4 return oi->old_pp(aTHX);
434 }
435  
436 #endif
437  
438 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
439  
440 501 static void a_cannot_vivify(pTHX_ UV flags) {
441 #define a_cannot_vivify(F) a_cannot_vivify(aTHX_ (F))
442 501 ▶ if (flags & A_HINT_STRICT)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & A_HINT_STRICT) 338 163 501
443 338 croak("Reference vivification forbidden");
444 163 ▶ else if (flags & A_HINT_WARN)
Branch: 1 branch
BranchTrueFalseTotal
else if (flags & A_HINT_WARN) 1 162 163
445 1 warn("Reference was vivified");
446 else /* A_HINT_STORE */
447 162 croak("Can't vivify reference");
448 1 }
449  
450 24086 static OP *a_pp_deref(pTHX) {
451 dA_MAP_THX;
452 const a_op_info *oi;
453 UV flags;
454 24086 dSP;
455  
456 24086 oi = a_map_fetch(PL_op);
457  
458 24086 flags = oi->flags;
459 24086 ▶ if (flags & A_HINT_DEREF) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & A_HINT_DEREF) { 24086 0 24086
460 OP *o;
461  
462 24086 o = oi->old_pp(aTHX);
463  
464 24086 ▶ if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) { 1311 22775 24086
465 1311 SPAGAIN;
466 1311 ▶ if (a_undef(TOPs))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(TOPs)) 501 810 1311
467 501 a_cannot_vivify(flags);
468 }
469  
470 23586 return o;
471 }
472  
473 0 return oi->old_pp(aTHX);
474 }
475  
476 /* ... pp_root (exists,delete,keys,values) ................................. */
477  
478 324 static OP *a_pp_root_unop(pTHX) {
479 324 dSP;
480  
481 324 ▶ if (a_undef(TOPs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(TOPs)) { 252 72 324
482 252 (void) POPs;
483 /* Can only be reached by keys or values */
484 252 ▶ if (GIMME_V == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_SCALAR) { 108 144 252
485 108 dTARGET;
486 108 ▶ PUSHi(0);
Branch: 1 branch
BranchTrueFalseTotal
PUSHi(0); 0 108 108
487 }
488 252 RETURN;
489 }
490  
491 {
492 dA_MAP_THX;
493 72 const a_op_info *oi = a_map_fetch(PL_op);
494 72 return oi->old_pp(aTHX);
495 }
496 }
497  
498 6399 static OP *a_pp_root_binop(pTHX) {
499 6399 dSP;
500  
501 6399 ▶ if (a_undef(TOPm1s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(TOPm1s)) { 5966 433 6399
502 5966 (void) POPs;
503 5966 (void) POPs;
504 5966 ▶ if (PL_op->op_type == OP_EXISTS)
Branch: 1 branch
BranchTrueFalseTotal
if (PL_op->op_type == OP_EXISTS) 2983 2983 5966
505 2983 RETPUSHNO;
506 else
507 2983 RETPUSHUNDEF;
508 }
509  
510 {
511 dA_MAP_THX;
512 433 const a_op_info *oi = a_map_fetch(PL_op);
513 433 return oi->old_pp(aTHX);
514 }
515 }
516  
517 #if A_HAS_MULTIDEREF
518  
519 /* ... pp_multideref ....................................................... */
520  
521 /* This pp replacement is actually only called for topmost exists/delete ops,
522 * because we hijack the [ah]elem check functions and this disables the
523 * optimization for lvalue and rvalue dereferencing. In particular, the
524 * OPf_MOD branches should never be covered. In the future, the multideref
525 * optimization might also be disabled for custom exists/delete check functions,
526 * which will make this section unnecessary. However, the code tries to be as
527 * general as possible in case I think of a way to reenable the multideref
528 * optimization even when this module is in use. */
529  
530 0 static UV a_do_multideref(const OP *o, UV flags) {
531 UV isexdel, other_flags;
532  
533 XSH_ASSERT(o->op_type == OP_MULTIDEREF);
534  
535 0 other_flags = flags & ~A_HINT_DO;
536  
537 0 isexdel = o->op_private & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE);
538 0 ▶ if (isexdel) {
Branch: 1 branch
BranchTrueFalseTotal
if (isexdel) { 0 0 0
539 0 ▶ if (isexdel & OPpMULTIDEREF_EXISTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (isexdel & OPpMULTIDEREF_EXISTS) { 0 0 0
540 0 flags &= A_HINT_EXISTS;
541 } else {
542 0 flags &= A_HINT_DELETE;
543 }
544 } else {
545 0 ▶ if (o->op_flags & OPf_MOD) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_flags & OPf_MOD) { 0 0 0
546 0 flags &= A_HINT_STORE;
547 } else {
548 0 flags &= A_HINT_FETCH;
549 }
550 }
551  
552 0 ▶ return flags ? (flags | other_flags) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return flags ? (flags | other_flags) : 0; 0 0 0
553 }
554  
555 0 static SV *a_do_fake_pp(pTHX_ OP *op) {
556 #define a_do_fake_pp(O) a_do_fake_pp(aTHX_ (O))
557 {
558 0 OP *o = PL_op;
559 0 ENTER;
560 0 SAVEOP();
561 0 PL_op = op;
562 0 PL_op->op_ppaddr(aTHX);
563 0 PL_op = o;
564 0 LEAVE;
565 }
566  
567 {
568 SV *ret;
569 0 dSP;
570 0 ret = POPs;
571 0 PUTBACK;
572 0 return ret;
573 }
574 }
575  
576 0 static void a_do_fake_pp_unop_init(pTHX_ UNOP *unop, U32 type, U32 flags) {
577 #define a_do_fake_pp_unop_init(O, T, F) a_do_fake_pp_unop_init(aTHX_ (O), (T), (F))
578 0 unop->op_type = type;
579 0 unop->op_flags = OPf_WANT_SCALAR | (~OPf_WANT & flags);
580 0 unop->op_private = 0;
581 0 unop->op_first = NULL;
582 0 unop->op_ppaddr = PL_ppaddr[type];
583 0 }
584  
585 0 static SV *a_do_fake_pp_unop_arg1(pTHX_ U32 type, U32 flags, SV *arg) {
586 #define a_do_fake_pp_unop_arg1(T, F, A) a_do_fake_pp_unop_arg1(aTHX_ (T), (F), (A))
587 UNOP unop;
588 0 dSP;
589  
590 0 a_do_fake_pp_unop_init(&unop, type, flags);
591  
592 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
593 0 PUSHs(arg);
594 0 PUTBACK;
595  
596 0 return a_do_fake_pp((OP *) &unop);
597 }
598  
599 0 static SV *a_do_fake_pp_unop_arg2(pTHX_ U32 type, U32 flags, SV *arg1, SV *arg2) {
600 #define a_do_fake_pp_unop_arg2(T, F, A1, A2) a_do_fake_pp_unop_arg2(aTHX_ (T), (F), (A1), (A2))
601 UNOP unop;
602 0 dSP;
603  
604 0 a_do_fake_pp_unop_init(&unop, type, flags);
605  
606 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
607 0 PUSHs(arg1);
608 0 PUSHs(arg2);
609 0 PUTBACK;
610  
611 0 return a_do_fake_pp((OP *) &unop);
612 }
613  
614 #define a_do_pp_rv2av(R) a_do_fake_pp_unop_arg1(OP_RV2AV, OPf_REF, (R))
615 #define a_do_pp_afetch(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, 0, (A), (I))
616 #define a_do_pp_afetch_lv(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, OPf_MOD, (A), (I))
617 #define a_do_pp_aexists(A, I) a_do_fake_pp_unop_arg2(OP_EXISTS, OPf_SPECIAL, (A), (I))
618 #define a_do_pp_adelete(A, I) a_do_fake_pp_unop_arg2(OP_DELETE, OPf_SPECIAL, (A), (I))
619  
620 #define a_do_pp_rv2hv(R) a_do_fake_pp_unop_arg1(OP_RV2HV, OPf_REF, (R))
621 #define a_do_pp_hfetch(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, 0, (H), (K))
622 #define a_do_pp_hfetch_lv(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, OPf_MOD, (H), (K))
623 #define a_do_pp_hexists(H, K) a_do_fake_pp_unop_arg2(OP_EXISTS, 0, (H), (K))
624 #define a_do_pp_hdelete(H, K) a_do_fake_pp_unop_arg2(OP_DELETE, 0, (H), (K))
625  
626 0 static OP *a_pp_multideref(pTHX) {
627 UNOP_AUX_item *items;
628 UV actions;
629 0 UV flags = 0;
630 0 SV *sv = NULL;
631 0 dSP;
632  
633 {
634 dA_MAP_THX;
635 0 const a_op_info *oi = a_map_fetch(PL_op);
636 XSH_ASSERT(oi);
637 0 flags = a_do_multideref(PL_op, oi->flags);
638 0 ▶ if (!flags)
Branch: 1 branch
BranchTrueFalseTotal
if (!flags) 0 0 0
639 0 return oi->old_pp(aTHX);
640 }
641  
642 0 items = cUNOP_AUXx(PL_op)->op_aux;
643 0 actions = items->uv;
644  
645 0 PL_multideref_pc = items;
646  
647 while (1) {
648 0 switch (actions & MDEREF_ACTION_MASK) {
649 0 case MDEREF_reload:
650 0 actions = (++items)->uv;
651 0 continue;
652 0 case MDEREF_AV_padav_aelem: /* $lex[...] */
653 0 sv = PAD_SVl((++items)->pad_offset);
654 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
655 0 goto ret_undef;
656 0 goto do_AV_aelem;
657 0 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
658 0 sv = UNOP_AUX_item_sv(++items);
659 XSH_ASSERT(isGV_with_GP(sv));
660 0 ▶ sv = (SV *) GvAVn((GV *) sv);
Branch: 1 branch
BranchTrueFalseTotal
sv = (SV *) GvAVn((GV *) sv); 0 0 0
661 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
662 0 goto ret_undef;
663 0 goto do_AV_aelem;
664 0 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
665 0 sv = POPs;
666 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
667 0 goto ret_undef;
668 0 goto do_AV_rv2av_aelem;
669 0 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
670 0 sv = UNOP_AUX_item_sv(++items);
671 XSH_ASSERT(isGV_with_GP(sv));
672 0 ▶ sv = GvSVn((GV *) sv);
Branch: 1 branch
BranchTrueFalseTotal
sv = GvSVn((GV *) sv); 0 0 0
673 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
674 0 goto ret_undef;
675 0 goto do_AV_vivify_rv2av_aelem;
676 0 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
677 0 sv = PAD_SVl((++items)->pad_offset);
678 /* FALLTHROUGH */
679 0 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
680 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
681 0 goto ret_undef;
682 0 do_AV_vivify_rv2av_aelem:
683 0 sv = a_vivify_ref(sv, 0);
684 0 do_AV_rv2av_aelem:
685 0 sv = a_do_pp_rv2av(sv);
686 0 do_AV_aelem:
687 {
688 SV *esv;
689 XSH_ASSERT(SvTYPE(sv) == SVt_PVAV);
690 0 switch (actions & MDEREF_INDEX_MASK) {
691 0 case MDEREF_INDEX_none:
692 0 goto finish;
693 0 case MDEREF_INDEX_const:
694 0 esv = sv_2mortal(newSViv((++items)->iv));
695 0 break;
696 0 case MDEREF_INDEX_padsv:
697 0 esv = PAD_SVl((++items)->pad_offset);
698 0 goto check_elem;
699 0 case MDEREF_INDEX_gvsv:
700 0 esv = UNOP_AUX_item_sv(++items);
701 XSH_ASSERT(isGV_with_GP(esv));
702 0 ▶ esv = GvSVn((GV *) esv);
Branch: 1 branch
BranchTrueFalseTotal
esv = GvSVn((GV *) esv); 0 0 0
703 0 check_elem:
704 0 ▶ if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC)))
Branch: 10 branches
BranchTrueFalseTotal
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC))) 0 0 0
705 0 Perl_warner(aTHX_ packWARN(WARN_MISC),
706 "Use of reference \"%"SVf"\" as array index",
707 SVfARG(esv));
708 0 break;
709 }
710 0 PL_multideref_pc = items;
711 0 ▶ if (actions & MDEREF_FLAG_last) {
Branch: 1 branch
BranchTrueFalseTotal
if (actions & MDEREF_FLAG_last) { 0 0 0
712 0 switch (flags & A_HINT_DO) {
713 0 case A_HINT_FETCH:
714 0 sv = a_do_pp_afetch(sv, esv);
715 0 break;
716 0 case A_HINT_STORE:
717 0 sv = a_do_pp_afetch_lv(sv, esv);
718 0 break;
719 0 case A_HINT_EXISTS:
720 0 sv = a_do_pp_aexists(sv, esv);
721 0 break;
722 0 case A_HINT_DELETE:
723 0 sv = a_do_pp_adelete(sv, esv);
724 0 break;
725 }
726 0 goto finish;
727 }
728 0 sv = a_do_pp_afetch(sv, esv);
729 0 break;
730 }
731 0 case MDEREF_HV_padhv_helem: /* $lex{...} */
732 0 sv = PAD_SVl((++items)->pad_offset);
733 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
734 0 goto ret_undef;
735 0 goto do_HV_helem;
736 0 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
737 0 sv = UNOP_AUX_item_sv(++items);
738 XSH_ASSERT(isGV_with_GP(sv));
739 0 ▶ sv = (SV *) GvHVn((GV *) sv);
Branch: 1 branch
BranchTrueFalseTotal
sv = (SV *) GvHVn((GV *) sv); 0 0 0
740 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
741 0 goto ret_undef;
742 0 goto do_HV_helem;
743 0 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
744 0 sv = POPs;
745 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
746 0 goto ret_undef;
747 0 goto do_HV_rv2hv_helem;
748 0 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
749 0 sv = UNOP_AUX_item_sv(++items);
750 XSH_ASSERT(isGV_with_GP(sv));
751 0 ▶ sv = GvSVn((GV *) sv);
Branch: 1 branch
BranchTrueFalseTotal
sv = GvSVn((GV *) sv); 0 0 0
752 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
753 0 goto ret_undef;
754 0 goto do_HV_vivify_rv2hv_helem;
755 0 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
756 0 sv = PAD_SVl((++items)->pad_offset);
757 /* FALLTHROUGH */
758 0 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
759 0 ▶ if (a_undef(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (a_undef(sv)) 0 0 0
760 0 goto ret_undef;
761 0 do_HV_vivify_rv2hv_helem:
762 0 sv = a_vivify_ref(sv, 1);
763 0 do_HV_rv2hv_helem:
764 0 sv = a_do_pp_rv2hv(sv);
765 0 do_HV_helem:
766 {
767 SV *key;
768 XSH_ASSERT(SvTYPE(sv) == SVt_PVHV);
769 0 switch (actions & MDEREF_INDEX_MASK) {
770 0 case MDEREF_INDEX_none:
771 0 goto finish;
772 0 case MDEREF_INDEX_const:
773 0 key = UNOP_AUX_item_sv(++items);
774 0 break;
775 0 case MDEREF_INDEX_padsv:
776 0 key = PAD_SVl((++items)->pad_offset);
777 0 break;
778 0 case MDEREF_INDEX_gvsv:
779 0 key = UNOP_AUX_item_sv(++items);
780 XSH_ASSERT(isGV_with_GP(key));
781 0 ▶ key = GvSVn((GV *) key);
Branch: 1 branch
BranchTrueFalseTotal
key = GvSVn((GV *) key); 0 0 0
782 0 break;
783 }
784 0 PL_multideref_pc = items;
785 0 ▶ if (actions & MDEREF_FLAG_last) {
Branch: 1 branch
BranchTrueFalseTotal
if (actions & MDEREF_FLAG_last) { 0 0 0
786 0 switch (flags & A_HINT_DO) {
787 0 case A_HINT_FETCH:
788 0 sv = a_do_pp_hfetch(sv, key);
789 0 break;
790 0 case A_HINT_STORE:
791 0 sv = a_do_pp_hfetch_lv(sv, key);
792 0 break;
793 0 case A_HINT_EXISTS:
794 0 sv = a_do_pp_hexists(sv, key);
795 0 break;
796 0 case A_HINT_DELETE:
797 0 sv = a_do_pp_hdelete(sv, key);
798 0 break;
799 0 default:
800 0 break;
801 }
802 0 goto finish;
803 }
804 0 sv = a_do_pp_hfetch(sv, key);
805 0 break;
806 }
807 }
808  
809 0 actions >>= MDEREF_SHIFT;
810 }
811  
812 0 ret_undef:
813 0 ▶ if (flags & (A_HINT_NOTIFY|A_HINT_STORE))
Branch: 1 branch
BranchTrueFalseTotal
if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) 0 0 0
814 0 a_cannot_vivify(flags);
815 0 ▶ if (flags & A_HINT_EXISTS)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & A_HINT_EXISTS) 0 0 0
816 0 sv = &PL_sv_no;
817 else
818 0 sv = &PL_sv_undef;
819 0 finish:
820 0 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 0 0
821 0 RETURN;
822 }
823  
824 #endif /* A_HAS_MULTIDEREF */
825  
826 /* --- Check functions ----------------------------------------------------- */
827  
828 33112 static void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
829 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
830  
831 33112 ▶ if (o->op_type == type && o->op_ppaddr != new_pp
Branch: 2 branches
BranchTrueFalseTotal
if (o->op_type == type && o->op_ppaddr != new_pp 28389 4723 33112
if (o->op_type == type && o->op_ppaddr != new_pp 4551 23838 28389
832 4551 ▶ && cUNOPo->op_first->op_type != OP_GV) {
Branch: 1 branch
BranchTrueFalseTotal
&& cUNOPo->op_first->op_type != OP_GV) { 1534 3017 4551
833 dA_MAP_THX;
834 1534 const a_op_info *oi = a_map_fetch(o);
835 1534 ▶ if (oi) {
Branch: 1 branch
BranchTrueFalseTotal
if (oi) { 1534 0 1534
836 1534 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
837 1534 o->op_ppaddr = new_pp;
838 }
839 }
840  
841 33112 return;
842 }
843  
844 /* ... ck_pad{any,sv} ...................................................... */
845  
846 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
847 * function, but are instead manually mutated from a padany. So we store
848 * the op entry in the op map in the padany check function, and we set their
849 * op_ppaddr member in our peephole optimizer replacement below. */
850  
851 static OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
852  
853 190487 static OP *a_ck_padany(pTHX_ OP *o) {
854 UV hint;
855  
856 190487 o = a_old_ck_padany(aTHX_ o);
857  
858 190487 hint = a_hint();
859 190487 ▶ if (hint & A_HINT_DO)
Branch: 1 branch
BranchTrueFalseTotal
if (hint & A_HINT_DO) 23670 166817 190487
860 23670 a_map_store_root(o, o->op_ppaddr, hint);
861 else
862 166817 a_map_delete(o);
863  
864 190487 return o;
865 }
866  
867 static OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
868  
869 9 static OP *a_ck_padsv(pTHX_ OP *o) {
870 UV hint;
871  
872 9 o = a_old_ck_padsv(aTHX_ o);
873  
874 9 hint = a_hint();
875 9 ▶ if (hint & A_HINT_DO) {
Branch: 1 branch
BranchTrueFalseTotal
if (hint & A_HINT_DO) { 1 8 9
876 1 a_map_store_root(o, o->op_ppaddr, hint);
877 1 o->op_ppaddr = a_pp_deref;
878 } else
879 8 a_map_delete(o);
880  
881 9 return o;
882 }
883  
884 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
885  
886 /* Those ops appear both at the root and inside an expression but there's no
887 * way to distinguish both situations. Worse, we can't even know if we are in a
888 * modifying context, so the expression can't be resolved yet. It will be at the
889 * first invocation of a_pp_deref() for this expression. */
890  
891 static OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
892 static OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
893 static OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
894  
895 184565 static OP *a_ck_deref(pTHX_ OP *o) {
896 184565 OP * (*old_ck)(pTHX_ OP *o) = 0;
897 184565 UV hint = a_hint();
898  
899 184565 switch (o->op_type) {
900 80655 case OP_AELEM:
901 80655 old_ck = a_old_ck_aelem;
902 80655 ▶ if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
Branch: 2 branches
BranchTrueFalseTotal
if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT)) 23349 57306 80655
if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT)) 18565 4784 23349
903 18565 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
904 80655 break;
905 34877 case OP_HELEM:
906 34877 old_ck = a_old_ck_helem;
907 34877 ▶ if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
Branch: 2 branches
BranchTrueFalseTotal
if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT)) 16060 18817 34877
if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT)) 14420 1640 16060
908 14420 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
909 34877 break;
910 69033 case OP_RV2SV:
911 69033 old_ck = a_old_ck_rv2sv;
912 69033 break;
913 }
914 184565 o = old_ck(aTHX_ o);
915  
916 #if A_HAS_MULTIDEREF
917 184565 ▶ if (old_ck == a_old_ck_rv2sv && o->op_flags & OPf_KIDS) {
Branch: 2 branches
BranchTrueFalseTotal
if (old_ck == a_old_ck_rv2sv && o->op_flags & OPf_KIDS) { 69033 115532 184565
if (old_ck == a_old_ck_rv2sv && o->op_flags & OPf_KIDS) { 69033 0 69033
918 69033 OP *kid = cUNOPo->op_first;
919 69033 ▶ if (kid && kid->op_type == OP_GV) {
Branch: 2 branches
BranchTrueFalseTotal
if (kid && kid->op_type == OP_GV) { 69033 0 69033
if (kid && kid->op_type == OP_GV) { 66217 2816 69033
920 66217 ▶ if (hint & A_HINT_DO)
Branch: 1 branch
BranchTrueFalseTotal
if (hint & A_HINT_DO) 15870 50347 66217
921 15870 a_map_store(kid, kid->op_ppaddr, NULL, hint);
922 else
923 50347 a_map_delete(kid);
924 }
925 }
926 #endif
927  
928 184565 ▶ if (hint & A_HINT_DO) {
Branch: 1 branch
BranchTrueFalseTotal
if (hint & A_HINT_DO) { 57781 126784 184565
929 57781 a_map_store_root(o, o->op_ppaddr, hint);
930 57781 o->op_ppaddr = a_pp_deref;
931 } else
932 126784 a_map_delete(o);
933  
934 184565 return o;
935 }
936  
937 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
938  
939 /* Those ops also appear both inisde and at the root, hence the caveats for
940 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
941 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
942 * expression. */
943  
944 static OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
945 static OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
946  
947 59954 static OP *a_ck_rv2xv(pTHX_ OP *o) {
948 59954 OP * (*old_ck)(pTHX_ OP *o) = 0;
949 59954 OP * (*new_pp)(pTHX) = 0;
950 UV hint;
951  
952 59954 switch (o->op_type) {
953 36428 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
954 23526 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
955 }
956 59954 o = old_ck(aTHX_ o);
957  
958 59954 ▶ if (cUNOPo->op_first->op_type == OP_GV)
Branch: 1 branch
BranchTrueFalseTotal
if (cUNOPo->op_first->op_type == OP_GV) 18422 41532 59954
959 18422 return o;
960  
961 41532 hint = a_hint();
962 41532 ▶ if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
Branch: 2 branches
BranchTrueFalseTotal
if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) { 27826 13706 41532
if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) { 24812 3014 27826
963 24812 a_map_store_root(o, o->op_ppaddr, hint);
964 24812 o->op_ppaddr = new_pp;
965 } else
966 16720 a_map_delete(o);
967  
968 41532 return o;
969 }
970  
971 /* ... ck_xslice (aslice,hslice) ........................................... */
972  
973 /* I think those are only found at the root, but there's nothing that really
974 * prevent them to be inside the expression too. We only need to update the
975 * root so that the rest of the expression will see the right context when
976 * resolving. That's why we don't replace the ppaddr. */
977  
978 static OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
979 static OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
980  
981 494 static OP *a_ck_xslice(pTHX_ OP *o) {
982 494 OP * (*old_ck)(pTHX_ OP *o) = 0;
983 494 UV hint = a_hint();
984  
985 494 switch (o->op_type) {
986 314 case OP_ASLICE:
987 314 old_ck = a_old_ck_aslice;
988 314 break;
989 180 case OP_HSLICE:
990 180 old_ck = a_old_ck_hslice;
991 180 ▶ if (hint & A_HINT_DO)
Branch: 1 branch
BranchTrueFalseTotal
if (hint & A_HINT_DO) 127 53 180
992 127 ▶ a_recheck_rv2xv(OpSIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv);
Branch: 1 branch
BranchTrueFalseTotal
a_recheck_rv2xv(OpSIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv); 127 0 127
993 180 break;
994 }
995 494 o = old_ck(aTHX_ o);
996  
997 494 ▶ if (hint & A_HINT_DO) {
Branch: 1 branch
BranchTrueFalseTotal
if (hint & A_HINT_DO) { 377 117 494
998 377 a_map_store_root(o, 0, hint);
999 } else
1000 117 a_map_delete(o);
1001  
1002 494 return o;
1003 }
1004  
1005 /* ... ck_root (exists,delete,keys,values) ................................. */
1006  
1007 /* Those ops are only found at the root of a dereferencing expression. We can
1008 * then resolve at compile time if vivification must take place or not. */
1009  
1010 static OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
1011 static OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
1012 static OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
1013 static OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
1014  
1015 9326 static OP *a_ck_root(pTHX_ OP *o) {
1016 9326 OP * (*old_ck)(pTHX_ OP *o) = 0;
1017 9326 OP * (*new_pp)(pTHX) = 0;
1018 9326 int enabled = 0;
1019 9326 UV hint = a_hint();
1020  
1021 9326 switch (o->op_type) {
1022 4019 case OP_EXISTS:
1023 4019 old_ck = a_old_ck_exists;
1024 4019 new_pp = a_pp_root_binop;
1025 4019 enabled = hint & A_HINT_EXISTS;
1026 4019 break;
1027 3823 case OP_DELETE:
1028 3823 old_ck = a_old_ck_delete;
1029 3823 new_pp = a_pp_root_binop;
1030 3823 enabled = hint & A_HINT_DELETE;
1031 3823 break;
1032 898 case OP_KEYS:
1033 898 old_ck = a_old_ck_keys;
1034 898 new_pp = a_pp_root_unop;
1035 898 enabled = hint & A_HINT_KEYS;
1036 898 break;
1037 586 case OP_VALUES:
1038 586 old_ck = a_old_ck_values;
1039 586 new_pp = a_pp_root_unop;
1040 586 enabled = hint & A_HINT_VALUES;
1041 586 break;
1042 }
1043 9326 o = old_ck(aTHX_ o);
1044  
1045 9326 ▶ if (hint & A_HINT_DO) {
Branch: 1 branch
BranchTrueFalseTotal
if (hint & A_HINT_DO) { 8175 1151 9326
1046 8175 ▶ if (enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (enabled) { 5367 2808 8175
1047 #if A_HAS_SCALARKEYS_OPT
1048 5367 ▶ if ((enabled == A_HINT_KEYS) && (o->op_flags & OPf_KIDS)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((enabled == A_HINT_KEYS) && (o->op_flags & OPf_KIDS)) { 204 5163 5367
if ((enabled == A_HINT_KEYS) && (o->op_flags & OPf_KIDS)) { 204 0 204
1049 204 OP *kid = cUNOPo->op_first;
1050 204 ▶ if (kid->op_type == OP_RV2HV) {
Branch: 1 branch
BranchTrueFalseTotal
if (kid->op_type == OP_RV2HV) { 96 108 204
1051 dA_MAP_THX;
1052 96 const a_op_info *koi = a_map_fetch(kid);
1053 96 ▶ a_map_store(kid, koi ? koi->old_pp : kid->op_ppaddr, NULL,
Branch: 1 branch
BranchTrueFalseTotal
a_map_store(kid, koi ? koi->old_pp : kid->op_ppaddr, NULL, 72 24 96
1054 hint | A_HINT_SECOND);
1055 96 ▶ if (!koi)
Branch: 1 branch
BranchTrueFalseTotal
if (!koi) 24 72 96
1056 24 kid->op_ppaddr = a_pp_rv2hv;
1057 }
1058 }
1059 #endif
1060 5367 a_map_update_flags_topdown(o, A_HINT_SECOND, hint | A_HINT_DEREF);
1061 5367 a_map_store_root(o, o->op_ppaddr, hint);
1062 5367 o->op_ppaddr = new_pp;
1063 } else {
1064 2808 a_map_update_flags_topdown(o, 0, 0);
1065 }
1066 } else
1067 1151 a_map_delete(o);
1068  
1069 9326 return o;
1070 }
1071  
1072 /* --- Our peephole optimizer ---------------------------------------------- */
1073  
1074 91386 static void xsh_peep_rec(pTHX_ OP *o, ptable *seen) {
1075 2706041 ▶ for (; o; o = o->op_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (; o; o = o->op_next) { 2614697 91344 2706041
1076 dA_MAP_THX;
1077 2614697 const a_op_info *oi = NULL;
1078 2614697 UV flags = 0;
1079  
1080 2614697 ▶ if (xsh_peep_seen(o, seen))
Branch: 1 branch
BranchTrueFalseTotal
if (xsh_peep_seen(o, seen)) 42 2614655 2614697
1081 42 break;
1082  
1083 2614655 switch (o->op_type) {
1084 210417 case OP_PADSV:
1085 210417 ▶ if (o->op_ppaddr != a_pp_deref) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_ppaddr != a_pp_deref) { 210412 5 210417
1086 210412 oi = a_map_fetch(o);
1087 210412 ▶ if (oi && (oi->flags & A_HINT_DO)) {
Branch: 2 branches
BranchTrueFalseTotal
if (oi && (oi->flags & A_HINT_DO)) { 17234 193178 210412
if (oi && (oi->flags & A_HINT_DO)) { 15769 1465 17234
1088 15769 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1089 15769 o->op_ppaddr = a_pp_deref;
1090 }
1091 }
1092 /* FALLTHROUGH */
1093 case OP_AELEM:
1094 case OP_AELEMFAST:
1095 case OP_HELEM:
1096 case OP_RV2SV:
1097 373118 ▶ if (o->op_ppaddr != a_pp_deref)
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_ppaddr != a_pp_deref) 323315 49803 373118
1098 323315 break;
1099 49803 oi = a_map_fetch(o);
1100 49803 ▶ if (!oi)
Branch: 1 branch
BranchTrueFalseTotal
if (!oi) 0 49803 49803
1101 0 break;
1102 49803 flags = oi->flags;
1103 49803 ▶ if (!(flags & A_HINT_DEREF)
Branch: 1 branch
BranchTrueFalseTotal
if (!(flags & A_HINT_DEREF) 34617 15186 49803
1104 34617 ▶ && (flags & A_HINT_DO)
Branch: 1 branch
BranchTrueFalseTotal
&& (flags & A_HINT_DO) 20517 14100 34617
1105 20517 ▶ && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
Branch: 2 branches
BranchTrueFalseTotal
&& (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) { 12907 7610 20517
&& (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) { 12823 84 12907
1106 /* Decide if the expression must autovivify or not. */
1107 20433 flags = a_map_resolve(o, oi);
1108 }
1109 49803 ▶ if (flags & A_HINT_DEREF)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & A_HINT_DEREF) 18237 31566 49803
1110 18237 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1111 else
1112 31566 o->op_ppaddr = oi->old_pp;
1113 49803 break;
1114 44683 case OP_RV2AV:
1115 44683 ▶ if (o->op_ppaddr != a_pp_rv2av)
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_ppaddr != a_pp_rv2av) 30729 13954 44683
1116 30729 break;
1117 13954 oi = a_map_fetch(o);
1118 13954 ▶ if (!oi)
Branch: 1 branch
BranchTrueFalseTotal
if (!oi) 0 13954 13954
1119 0 break;
1120 13954 ▶ if (!(oi->flags & A_HINT_DEREF))
Branch: 1 branch
BranchTrueFalseTotal
if (!(oi->flags & A_HINT_DEREF)) 5110 8844 13954
1121 5110 o->op_ppaddr = oi->old_pp;
1122 13954 break;
1123 47130 case OP_RV2HV:
1124 47130 ▶ if (o->op_ppaddr != a_pp_rv2hv && o->op_ppaddr != a_pp_rv2hv_simple)
Branch: 2 branches
BranchTrueFalseTotal
if (o->op_ppaddr != a_pp_rv2hv && o->op_ppaddr != a_pp_rv2hv_simple) 47018 112 47130
if (o->op_ppaddr != a_pp_rv2hv && o->op_ppaddr != a_pp_rv2hv_simple) 34798 12220 47018
1125 34798 break;
1126 12332 oi = a_map_fetch(o);
1127 12332 ▶ if (!oi)
Branch: 1 branch
BranchTrueFalseTotal
if (!oi) 0 12332 12332
1128 0 break;
1129 12332 ▶ if (!(oi->flags & A_HINT_DEREF)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(oi->flags & A_HINT_DEREF)) { 3925 8407 12332
1130 3925 o->op_ppaddr = oi->old_pp;
1131 3925 break;
1132 }
1133 #if A_HAS_SCALARKEYS_OPT
1134 8407 flags = oi->flags;
1135 8407 ▶ if ((flags & A_HINT_KEYS) && (flags & A_HINT_SECOND)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & A_HINT_KEYS) && (flags & A_HINT_SECOND)) { 8113 294 8407
if ((flags & A_HINT_KEYS) && (flags & A_HINT_SECOND)) { 96 8017 8113
1136 96 U8 want = o->op_flags & OPf_WANT;
1137 96 ▶ if (want == OPf_WANT_VOID || want == OPf_WANT_SCALAR)
Branch: 2 branches
BranchTrueFalseTotal
if (want == OPf_WANT_VOID || want == OPf_WANT_SCALAR) 96 0 96
if (want == OPf_WANT_VOID || want == OPf_WANT_SCALAR) 46 50 96
1138 46 o->op_ppaddr = a_pp_rv2hv_dokeys;
1139 50 ▶ else if (oi->old_pp == a_pp_rv2hv || oi->old_pp == a_pp_rv2hv_simple)
Branch: 2 branches
BranchTrueFalseTotal
else if (oi->old_pp == a_pp_rv2hv || oi->old_pp == a_pp_rv2hv_simple) 50 0 50
else if (oi->old_pp == a_pp_rv2hv || oi->old_pp == a_pp_rv2hv_simple) 0 50 50
1140 0 o->op_ppaddr = oi->old_pp;
1141 }
1142 #endif
1143 8407 break;
1144 #if A_HAS_MULTIDEREF
1145 0 case OP_MULTIDEREF:
1146 0 ▶ if (o->op_ppaddr != a_pp_multideref) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_ppaddr != a_pp_multideref) { 0 0 0
1147 0 oi = a_map_fetch(cUNOPo->op_first);
1148 0 ▶ if (!oi)
Branch: 1 branch
BranchTrueFalseTotal
if (!oi) 0 0 0
1149 0 break;
1150 0 flags = oi->flags;
1151 0 ▶ if (a_do_multideref(o, flags)) {
Branch: 1 branch
BranchTrueFalseTotal
if (a_do_multideref(o, flags)) { 0 0 0
1152 0 a_map_store_root(o, o->op_ppaddr, flags & ~A_HINT_DEREF);
1153 0 o->op_ppaddr = a_pp_multideref;
1154 }
1155 }
1156 0 break;
1157 #endif
1158 2149724 default:
1159 xsh_peep_maybe_recurse(o, seen);
1160 2149724 break;
1161 }
1162 }
1163 91386 }
1164  
1165 /* --- Module setup/teardown ----------------------------------------------- */
1166  
1167 20 static void xsh_user_global_setup(pTHX) {
1168 20 a_op_map = ptable_new(32);
1169  
1170 #ifdef USE_ITHREADS
1171 MUTEX_INIT(&a_op_map_mutex);
1172 #endif
1173  
1174 20 xsh_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1175 20 xsh_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1176  
1177 20 xsh_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1178 20 xsh_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1179 20 xsh_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1180  
1181 20 xsh_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1182 20 xsh_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1183  
1184 20 xsh_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1185 20 xsh_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1186  
1187 20 xsh_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1188 20 xsh_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1189 20 xsh_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1190 20 xsh_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1191  
1192 20 return;
1193 }
1194  
1195 20 static void xsh_user_local_setup(pTHX) {
1196 HV *stash;
1197  
1198 20 stash = gv_stashpvn(XSH_PACKAGE, XSH_PACKAGE_LEN, 1);
1199 20 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1200 20 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1201 20 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1202 20 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1203 20 newCONSTSUB(stash, "A_HINT_KEYS", newSVuv(A_HINT_KEYS));
1204 20 newCONSTSUB(stash, "A_HINT_VALUES", newSVuv(A_HINT_VALUES));
1205 20 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1206 20 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1207 20 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1208 20 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(XSH_THREADSAFE));
1209 20 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(XSH_FORKSAFE));
1210  
1211 20 return;
1212 }
1213  
1214 20 static void xsh_user_local_teardown(pTHX) {
1215 20 return;
1216 }
1217  
1218 20 static void xsh_user_global_teardown(pTHX) {
1219 20 xsh_ck_restore(OP_PADANY, &a_old_ck_padany);
1220 20 xsh_ck_restore(OP_PADSV, &a_old_ck_padsv);
1221  
1222 20 xsh_ck_restore(OP_AELEM, &a_old_ck_aelem);
1223 20 xsh_ck_restore(OP_HELEM, &a_old_ck_helem);
1224 20 xsh_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1225  
1226 20 xsh_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1227 20 xsh_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1228  
1229 20 xsh_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1230 20 xsh_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1231  
1232 20 xsh_ck_restore(OP_EXISTS, &a_old_ck_exists);
1233 20 xsh_ck_restore(OP_DELETE, &a_old_ck_delete);
1234 20 xsh_ck_restore(OP_KEYS, &a_old_ck_keys);
1235 20 xsh_ck_restore(OP_VALUES, &a_old_ck_values);
1236  
1237 20 ptable_map_free(a_op_map);
1238 20 a_op_map = NULL;
1239  
1240 #ifdef USE_ITHREADS
1241 MUTEX_DESTROY(&a_op_map_mutex);
1242 #endif
1243  
1244 20 return;
1245 }
1246  
1247 /* --- XS ------------------------------------------------------------------ */
1248  
1249 MODULE = autovivification PACKAGE = autovivification
1250  
1251 PROTOTYPES: ENABLE
1252  
1253 BOOT:
1254 {
1255 20 xsh_setup();
1256 }
1257  
1258 #if XSH_THREADSAFE
1259  
1260 void
1261 CLONE(...)
1262 PROTOTYPE: DISABLE
1263 PPCODE:
1264 xsh_clone();
1265 XSRETURN(0);
1266  
1267 #endif /* XSH_THREADSAFE */
1268  
1269 SV *
1270 _tag(SV *hint)
1271 PROTOTYPE: $
1272 CODE:
1273 20577 ▶ RETVAL = xsh_hints_tag(SvOK(hint) ? SvUV(hint) : 0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = xsh_hints_tag(SvOK(hint) ? SvUV(hint) : 0); 20577 0 20577
1274 OUTPUT:
1275 RETVAL
1276  
1277 SV *
1278 _detag(SV *tag)
1279 PROTOTYPE: $
1280 CODE:
1281 20576 ▶ if (!SvOK(tag))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(tag)) 16967 3609 20576
1282 16967 XSRETURN_UNDEF;
1283 3609 RETVAL = newSVuv(xsh_hints_detag(tag));
1284 OUTPUT:
1285 RETVAL
« blib/lib/autovivification.pm ↑ Summary xsh/mem.h »