1 /* This file is part of the autovivification Perl module.
2 * See http://search.cpan.org/dist/autovivification/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "autovivification"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
12 /* --- Compatibility wrappers ---------------------------------------------- */
15 # define HvNAME_get(H) HvNAME(H)
19 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
22 #define A_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
24 #ifndef A_WORKAROUND_REQUIRE_PROPAGATION
25 # define A_WORKAROUND_REQUIRE_PROPAGATION !A_HAS_PERL(5, 10, 1)
29 # define A_HAS_RPEEP A_HAS_PERL(5, 13, 5)
32 /* ... Thread safety and multiplicity ...................................... */
34 /* Always safe when the workaround isn't needed */
35 #if !A_WORKAROUND_REQUIRE_PROPAGATION
38 /* Otherwise, safe unless Makefile.PL says it's Win32 */
39 #elif !defined(A_FORKSAFE)
43 #ifndef A_MULTIPLICITY
44 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
45 # define A_MULTIPLICITY 1
47 # define A_MULTIPLICITY 0
52 # define tTHX PerlInterpreter*
55 #if A_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
56 # define A_THREADSAFE 1
58 # define MY_CXT_CLONE \
60 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
61 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
62 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
65 # define A_THREADSAFE 0
67 # define dMY_CXT dNOOP
69 # define MY_CXT a_globaldata
71 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
73 # define MY_CXT_INIT NOOP
75 # define MY_CXT_CLONE NOOP
78 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
79 # define A_CHECK_MUTEX_LOCK OP_CHECK_MUTEX_LOCK
80 # define A_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
82 # define A_CHECK_MUTEX_LOCK OP_REFCNT_LOCK
83 # define A_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
86 typedef OP *(*a_ck_t)(pTHX_ OP *);
88 #ifdef wrap_op_checker
90 # define a_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
94 STATIC void a_ck_replace(pTHX_ OPCODE type, a_ck_t new_ck, a_ck_t *old_ck_p) {
95 #define a_ck_replace(T, NC, OCP) a_ck_replace(aTHX_ (T), (NC), (OCP))
98 *old_ck_p = PL_check[type];
99 PL_check[type] = new_ck;
101 A_CHECK_MUTEX_UNLOCK;
106 STATIC void a_ck_restore(pTHX_ OPCODE type, a_ck_t *old_ck_p) {
107 #define a_ck_restore(T, OCP) a_ck_restore(aTHX_ (T), (OCP))
110 PL_check[type] = *old_ck_p;
113 A_CHECK_MUTEX_UNLOCK;
116 /* --- Helpers ------------------------------------------------------------- */
118 /* ... Thread-safe hints ................................................... */
120 #if A_WORKAROUND_REQUIRE_PROPAGATION
127 #define A_HINT_FREE(H) PerlMemShared_free(H)
131 #define PTABLE_NAME ptable_hints
132 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
141 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
142 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
144 #endif /* A_THREADSAFE */
146 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
148 #define PTABLE_NAME ptable_seen
149 #define PTABLE_VAL_FREE(V) NOOP
153 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
154 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
155 #define ptable_seen_clear(T) ptable_seen_clear(aPTBLMS_ (T))
156 #define ptable_seen_free(T) ptable_seen_free(aPTBLMS_ (T))
158 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
161 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
162 ptable *tbl; /* It really is a ptable_hints */
164 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
165 ptable *seen; /* It really is a ptable_seen */
172 #if A_WORKAROUND_REQUIRE_PROPAGATION
176 #if A_HAS_PERL(5, 13, 2)
177 CLONE_PARAMS *params;
183 #if A_HAS_PERL(5, 13, 2)
184 # define a_ptable_clone_ud_init(U, T, O) \
186 (U).params = Perl_clone_params_new((O), aTHX)
187 # define a_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
188 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
190 # define a_ptable_clone_ud_init(U, T, O) \
192 (U).params.stashes = newAV(); \
193 (U).params.flags = 0; \
194 (U).params.proto_perl = (O)
195 # define a_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
196 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
199 STATIC void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
200 a_ptable_clone_ud *ud = ud_;
201 a_hint_t *h1 = ent->val;
204 h2 = PerlMemShared_malloc(sizeof *h2);
206 h2->require_tag = PTR2IV(a_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
208 ptable_hints_store(ud->tbl, ent->key, h2);
211 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
215 STATIC void a_thread_cleanup(pTHX_ void *ud) {
218 #if A_WORKAROUND_REQUIRE_PROPAGATION
219 ptable_hints_free(MY_CXT.tbl);
220 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
221 ptable_seen_free(MY_CXT.seen);
224 #endif /* A_THREADSAFE */
226 #if A_WORKAROUND_REQUIRE_PROPAGATION
228 STATIC IV a_require_tag(pTHX) {
229 #define a_require_tag() a_require_tag(aTHX)
230 const CV *cv, *outside;
235 /* If for some reason the pragma is operational at run-time, try to discover
236 * the current cv in use. */
239 for (si = PL_curstackinfo; si; si = si->si_prev) {
242 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
243 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
245 switch (CxTYPE(cx)) {
248 /* The propagation workaround is only needed up to 5.10.0 and at that
249 * time format and sub contexts were still identical. And even later the
250 * cv members offsets should have been kept the same. */
252 goto get_enclosing_cv;
254 cv = cx->blk_eval.cv;
255 goto get_enclosing_cv;
266 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
272 STATIC SV *a_tag(pTHX_ UV bits) {
273 #define a_tag(B) a_tag(aTHX_ (B))
276 h = PerlMemShared_malloc(sizeof *h);
278 h->require_tag = a_require_tag();
283 /* We only need for the key to be an unique tag for looking up the value later
284 * Allocated memory provides convenient unique identifiers, so that's why we
285 * use the hint as the key itself. */
286 ptable_hints_store(MY_CXT.tbl, h, h);
288 #endif /* A_THREADSAFE */
290 return newSViv(PTR2IV(h));
293 STATIC UV a_detag(pTHX_ const SV *hint) {
294 #define a_detag(H) a_detag(aTHX_ (H))
297 if (!(hint && SvIOK(hint)))
300 h = INT2PTR(a_hint_t *, SvIVX(hint));
304 h = ptable_fetch(MY_CXT.tbl, h);
306 #endif /* A_THREADSAFE */
308 if (a_require_tag() != h->require_tag)
314 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
316 #define a_tag(B) newSVuv(B)
317 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
324 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
330 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
332 /* Used both for hints and op flags */
333 #define A_HINT_STRICT 1
334 #define A_HINT_WARN 2
335 #define A_HINT_FETCH 4
336 #define A_HINT_STORE 8
337 #define A_HINT_EXISTS 16
338 #define A_HINT_DELETE 32
339 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
340 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
341 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
343 /* Only used in op flags */
344 #define A_HINT_ROOT 64
345 #define A_HINT_DEREF 128
347 STATIC U32 a_hash = 0;
349 STATIC UV a_hint(pTHX) {
350 #define a_hint() a_hint(aTHX)
352 #ifdef cop_hints_fetch_pvn
353 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
354 #elif A_HAS_PERL(5, 9, 5)
355 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
357 __PACKAGE__, __PACKAGE_LEN__,
361 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
366 return a_detag(hint);
369 /* ... op => info map ...................................................... */
377 #define PTABLE_NAME ptable_map
378 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
382 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
383 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
384 #define ptable_map_delete(T, K) ptable_map_delete(aPTBLMS_ (T), (K))
386 STATIC ptable *a_op_map = NULL;
390 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
392 STATIC perl_mutex a_op_map_mutex;
394 #define A_LOCK(M) MUTEX_LOCK(M)
395 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
397 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
398 const a_op_info *val;
400 A_LOCK(&a_op_map_mutex);
402 val = ptable_fetch(a_op_map, o);
408 A_UNLOCK(&a_op_map_mutex);
413 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
415 #else /* USE_ITHREADS */
417 #define dA_MAP_THX dNOOP
419 #define A_LOCK(M) NOOP
420 #define A_UNLOCK(M) NOOP
422 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
424 #endif /* !USE_ITHREADS */
426 STATIC const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
427 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
430 if (!(oi = ptable_fetch(a_op_map, o))) {
431 oi = PerlMemShared_malloc(sizeof *oi);
432 ptable_map_store(a_op_map, o, oi);
442 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
443 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
444 A_LOCK(&a_op_map_mutex);
446 a_map_store_locked(o, old_pp, next, flags);
448 A_UNLOCK(&a_op_map_mutex);
451 STATIC void a_map_delete(pTHX_ const OP *o) {
452 #define a_map_delete(O) a_map_delete(aTHX_ (O))
453 A_LOCK(&a_op_map_mutex);
455 ptable_map_delete(a_op_map, o);
457 A_UNLOCK(&a_op_map_mutex);
460 STATIC const OP *a_map_descend(const OP *o) {
461 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
465 case OA_BASEOP_OR_UNOP:
466 return cUNOPo->op_first;
469 return cLISTOPo->op_last;
475 STATIC void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
476 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
477 const a_op_info *roi;
481 A_LOCK(&a_op_map_mutex);
483 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
485 while (o->op_flags & OPf_KIDS) {
486 o = a_map_descend(o);
489 if ((oi = ptable_fetch(a_op_map, o))) {
490 oi->flags &= ~A_HINT_ROOT;
491 oi->next = (a_op_info *) roi;
496 A_UNLOCK(&a_op_map_mutex);
501 STATIC void a_map_update_flags_topdown(const OP *root, UV flags) {
505 A_LOCK(&a_op_map_mutex);
507 flags &= ~A_HINT_ROOT;
510 if ((oi = ptable_fetch(a_op_map, o)))
511 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
512 if (!(o->op_flags & OPf_KIDS))
514 o = a_map_descend(o);
517 A_UNLOCK(&a_op_map_mutex);
522 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
524 STATIC void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
527 A_LOCK(&a_op_map_mutex);
529 flags &= ~A_HINT_ROOT;
530 rflags |= A_HINT_ROOT;
532 oi = ptable_fetch(a_op_map, o);
533 while (!(oi->flags & A_HINT_ROOT)) {
539 A_UNLOCK(&a_op_map_mutex);
544 /* ... Decide whether this expression should be autovivified or not ........ */
546 STATIC UV a_map_resolve(const OP *o, const a_op_info *oi) {
547 UV flags = 0, rflags;
549 const a_op_info *roi = oi;
551 while (!(roi->flags & A_HINT_ROOT))
556 rflags = roi->flags & ~A_HINT_ROOT;
561 if (root->op_flags & OPf_MOD) {
562 if (rflags & A_HINT_STORE)
563 flags = (A_HINT_STORE|A_HINT_DEREF);
564 } else if (rflags & A_HINT_FETCH)
565 flags = (A_HINT_FETCH|A_HINT_DEREF);
569 a_map_update_flags_bottomup(o, 0, 0);
573 flags |= (rflags & A_HINT_NOTIFY);
574 a_map_update_flags_bottomup(o, flags, 0);
576 return oi->flags & A_HINT_ROOT ? 0 : flags;
579 /* ... Inspired from pp_defined() .......................................... */
581 STATIC int a_undef(pTHX_ SV *sv) {
582 #define a_undef(S) a_undef(aTHX_ (S))
583 switch (SvTYPE(sv)) {
587 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
588 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
592 if (HvARRAY(sv) || SvGMAGICAL(sv)
593 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
605 /* --- PP functions -------------------------------------------------------- */
607 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
608 * value, another extension might have saved our pp replacement as the ppaddr
609 * for this op, so this doesn't ensure that our function will never be called
610 * again. That's why we don't remove the op info from our map, so that it can
611 * still run correctly if required. */
613 /* ... pp_rv2av ............................................................ */
615 STATIC OP *a_pp_rv2av(pTHX) {
620 oi = a_map_fetch(PL_op);
622 if (oi->flags & A_HINT_DEREF) {
624 /* We always need to push an empty array to fool the pp_aelem() that comes
628 av = sv_2mortal((SV *) newAV());
634 return oi->old_pp(aTHX);
637 /* ... pp_rv2hv ............................................................ */
639 STATIC OP *a_pp_rv2hv_simple(pTHX) {
644 oi = a_map_fetch(PL_op);
646 if (oi->flags & A_HINT_DEREF) {
651 return oi->old_pp(aTHX);
654 STATIC OP *a_pp_rv2hv(pTHX) {
659 oi = a_map_fetch(PL_op);
661 if (oi->flags & A_HINT_DEREF) {
665 hv = sv_2mortal((SV *) newHV());
671 return oi->old_pp(aTHX);
674 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
676 STATIC OP *a_pp_deref(pTHX) {
682 oi = a_map_fetch(PL_op);
685 if (flags & A_HINT_DEREF) {
688 o = oi->old_pp(aTHX);
690 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
693 if (flags & A_HINT_STRICT)
694 croak("Reference vivification forbidden");
695 else if (flags & A_HINT_WARN)
696 warn("Reference was vivified");
697 else /* A_HINT_STORE */
698 croak("Can't vivify reference");
705 return oi->old_pp(aTHX);
708 /* ... pp_root (exists,delete,keys,values) ................................. */
710 STATIC OP *a_pp_root_unop(pTHX) {
715 /* Can only be reached by keys or values */
716 if (GIMME_V == G_SCALAR) {
725 const a_op_info *oi = a_map_fetch(PL_op);
726 return oi->old_pp(aTHX);
730 STATIC OP *a_pp_root_binop(pTHX) {
733 if (a_undef(TOPm1s)) {
736 if (PL_op->op_type == OP_EXISTS)
744 const a_op_info *oi = a_map_fetch(PL_op);
745 return oi->old_pp(aTHX);
749 /* --- Check functions ----------------------------------------------------- */
751 STATIC void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
752 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
754 if (o->op_type == type && o->op_ppaddr != new_pp
755 && cUNOPo->op_first->op_type != OP_GV) {
757 const a_op_info *oi = a_map_fetch(o);
759 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
760 o->op_ppaddr = new_pp;
767 /* ... ck_pad{any,sv} ...................................................... */
769 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
770 * function, but are instead manually mutated from a padany. So we store
771 * the op entry in the op map in the padany check function, and we set their
772 * op_ppaddr member in our peephole optimizer replacement below. */
774 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
776 STATIC OP *a_ck_padany(pTHX_ OP *o) {
779 o = a_old_ck_padany(aTHX_ o);
782 if (hint & A_HINT_DO)
783 a_map_store_root(o, o->op_ppaddr, hint);
790 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
792 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
795 o = a_old_ck_padsv(aTHX_ o);
798 if (hint & A_HINT_DO) {
799 a_map_store_root(o, o->op_ppaddr, hint);
800 o->op_ppaddr = a_pp_deref;
807 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
809 /* Those ops appear both at the root and inside an expression but there's no
810 * way to distinguish both situations. Worse, we can't even know if we are in a
811 * modifying context, so the expression can't be resolved yet. It will be at the
812 * first invocation of a_pp_deref() for this expression. */
814 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
815 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
816 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
818 STATIC OP *a_ck_deref(pTHX_ OP *o) {
819 OP * (*old_ck)(pTHX_ OP *o) = 0;
822 switch (o->op_type) {
824 old_ck = a_old_ck_aelem;
825 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
826 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
829 old_ck = a_old_ck_helem;
830 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
831 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
834 old_ck = a_old_ck_rv2sv;
839 if (hint & A_HINT_DO) {
840 a_map_store_root(o, o->op_ppaddr, hint);
841 o->op_ppaddr = a_pp_deref;
848 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
850 /* Those ops also appear both inisde and at the root, hence the caveats for
851 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
852 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
855 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
856 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
858 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
859 OP * (*old_ck)(pTHX_ OP *o) = 0;
860 OP * (*new_pp)(pTHX) = 0;
863 switch (o->op_type) {
864 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
865 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
869 if (cUNOPo->op_first->op_type == OP_GV)
873 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
874 a_map_store_root(o, o->op_ppaddr, hint);
875 o->op_ppaddr = new_pp;
882 /* ... ck_xslice (aslice,hslice) ........................................... */
884 /* I think those are only found at the root, but there's nothing that really
885 * prevent them to be inside the expression too. We only need to update the
886 * root so that the rest of the expression will see the right context when
887 * resolving. That's why we don't replace the ppaddr. */
889 STATIC OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
890 STATIC OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
892 STATIC OP *a_ck_xslice(pTHX_ OP *o) {
893 OP * (*old_ck)(pTHX_ OP *o) = 0;
896 switch (o->op_type) {
898 old_ck = a_old_ck_aslice;
901 old_ck = a_old_ck_hslice;
902 if (hint & A_HINT_DO)
903 a_recheck_rv2xv(cUNOPo->op_first->op_sibling, OP_RV2HV, a_pp_rv2hv);
908 if (hint & A_HINT_DO) {
909 a_map_store_root(o, 0, hint);
916 /* ... ck_root (exists,delete,keys,values) ................................. */
918 /* Those ops are only found at the root of a dereferencing expression. We can
919 * then resolve at compile time if vivification must take place or not. */
921 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
922 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
923 STATIC OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
924 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
926 STATIC OP *a_ck_root(pTHX_ OP *o) {
927 OP * (*old_ck)(pTHX_ OP *o) = 0;
928 OP * (*new_pp)(pTHX) = 0;
929 bool enabled = FALSE;
932 switch (o->op_type) {
934 old_ck = a_old_ck_exists;
935 new_pp = a_pp_root_binop;
936 enabled = hint & A_HINT_EXISTS;
939 old_ck = a_old_ck_delete;
940 new_pp = a_pp_root_binop;
941 enabled = hint & A_HINT_DELETE;
944 old_ck = a_old_ck_keys;
945 new_pp = a_pp_root_unop;
946 enabled = hint & A_HINT_FETCH;
949 old_ck = a_old_ck_values;
950 new_pp = a_pp_root_unop;
951 enabled = hint & A_HINT_FETCH;
956 if (hint & A_HINT_DO) {
958 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
959 a_map_store_root(o, o->op_ppaddr, hint);
960 o->op_ppaddr = new_pp;
970 /* ... Our peephole optimizer .............................................. */
972 STATIC peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
974 STATIC void a_peep_rec(pTHX_ OP *o, ptable *seen);
976 STATIC void a_peep_rec(pTHX_ OP *o, ptable *seen) {
977 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
978 for (; o; o = o->op_next) {
980 const a_op_info *oi = NULL;
983 if (ptable_fetch(seen, o))
985 ptable_seen_store(seen, o, o);
987 switch (o->op_type) {
989 if (o->op_ppaddr != a_pp_deref) {
991 if (oi && (oi->flags & A_HINT_DO)) {
992 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
993 o->op_ppaddr = a_pp_deref;
1001 if (o->op_ppaddr != a_pp_deref)
1003 oi = a_map_fetch(o);
1007 if (!(flags & A_HINT_DEREF)
1008 && (flags & A_HINT_DO)
1009 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1010 /* Decide if the expression must autovivify or not. */
1011 flags = a_map_resolve(o, oi);
1013 if (flags & A_HINT_DEREF)
1014 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1016 o->op_ppaddr = oi->old_pp;
1020 if ( o->op_ppaddr != a_pp_rv2av
1021 && o->op_ppaddr != a_pp_rv2hv
1022 && o->op_ppaddr != a_pp_rv2hv_simple)
1024 oi = a_map_fetch(o);
1027 if (!(oi->flags & A_HINT_DEREF))
1028 o->op_ppaddr = oi->old_pp;
1039 # if A_HAS_PERL(5, 10, 0)
1044 a_peep_rec(cLOGOPo->op_other);
1048 a_peep_rec(cLOOPo->op_redoop);
1049 a_peep_rec(cLOOPo->op_nextop);
1050 a_peep_rec(cLOOPo->op_lastop);
1052 # if A_HAS_PERL(5, 9, 5)
1054 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1060 a_peep_rec(cPMOPo->op_pmreplstart);
1063 #endif /* !A_HAS_RPEEP */
1070 STATIC void a_peep(pTHX_ OP *o) {
1072 ptable *seen = MY_CXT.seen;
1074 a_old_peep(aTHX_ o);
1076 ptable_seen_clear(seen);
1078 ptable_seen_clear(seen);
1081 /* --- Interpreter setup/teardown ------------------------------------------ */
1083 STATIC U32 a_initialized = 0;
1085 STATIC void a_teardown(pTHX_ void *root) {
1097 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1098 ptable_hints_free(MY_CXT.tbl);
1099 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1100 ptable_seen_free(MY_CXT.seen);
1103 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1104 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1106 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1107 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1108 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1110 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1111 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1113 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1114 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1116 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1117 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1118 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1119 a_ck_restore(OP_VALUES, &a_old_ck_values);
1122 PL_rpeepp = a_old_peep;
1124 PL_peepp = a_old_peep;
1131 STATIC void a_setup(pTHX) {
1132 #define a_setup() a_setup(aTHX)
1138 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1139 MY_CXT.tbl = ptable_new();
1140 MY_CXT.owner = aTHX;
1141 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1142 MY_CXT.seen = ptable_new();
1145 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1146 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1148 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1149 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1150 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1152 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1153 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1155 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1156 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1158 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1159 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1160 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1161 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1164 a_old_peep = PL_rpeepp;
1167 a_old_peep = PL_peepp;
1172 call_atexit(a_teardown, aTHX);
1174 call_atexit(a_teardown, NULL);
1180 STATIC U32 a_booted = 0;
1182 /* --- XS ------------------------------------------------------------------ */
1184 MODULE = autovivification PACKAGE = autovivification
1193 a_op_map = ptable_new();
1195 MUTEX_INIT(&a_op_map_mutex);
1198 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1200 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1201 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1202 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1203 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1204 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1205 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1206 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1207 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1208 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1209 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1221 #if A_WORKAROUND_REQUIRE_PROPAGATION
1228 #if A_WORKAROUND_REQUIRE_PROPAGATION
1230 a_ptable_clone_ud ud;
1233 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1234 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1235 a_ptable_clone_ud_deinit(ud);
1242 #if A_WORKAROUND_REQUIRE_PROPAGATION
1244 MY_CXT.owner = aTHX;
1248 reap(3, a_thread_cleanup, NULL);
1251 #endif /* A_THREADSAFE */
1257 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1267 RETVAL = newSVuv(a_detag(tag));