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 */
213 STATIC void a_thread_cleanup(pTHX_ void *ud) {
216 #if A_WORKAROUND_REQUIRE_PROPAGATION
217 ptable_hints_free(MY_CXT.tbl);
219 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
220 ptable_seen_free(MY_CXT.seen);
224 STATIC int a_endav_free(pTHX_ SV *sv, MAGIC *mg) {
225 SAVEDESTRUCTOR_X(a_thread_cleanup, NULL);
230 STATIC MGVTBL a_endav_vtbl = {
247 #endif /* A_THREADSAFE */
249 #if A_WORKAROUND_REQUIRE_PROPAGATION
251 STATIC IV a_require_tag(pTHX) {
252 #define a_require_tag() a_require_tag(aTHX)
253 const CV *cv, *outside;
258 /* If for some reason the pragma is operational at run-time, try to discover
259 * the current cv in use. */
262 for (si = PL_curstackinfo; si; si = si->si_prev) {
265 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
266 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
268 switch (CxTYPE(cx)) {
271 /* The propagation workaround is only needed up to 5.10.0 and at that
272 * time format and sub contexts were still identical. And even later the
273 * cv members offsets should have been kept the same. */
275 goto get_enclosing_cv;
277 cv = cx->blk_eval.cv;
278 goto get_enclosing_cv;
289 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
295 STATIC SV *a_tag(pTHX_ UV bits) {
296 #define a_tag(B) a_tag(aTHX_ (B))
303 #endif /* A_THREADSAFE */
305 h = PerlMemShared_malloc(sizeof *h);
307 h->require_tag = a_require_tag();
310 /* We only need for the key to be an unique tag for looking up the value later
311 * Allocated memory provides convenient unique identifiers, so that's why we
312 * use the hint as the key itself. */
313 ptable_hints_store(MY_CXT.tbl, h, h);
314 #endif /* A_THREADSAFE */
316 return newSViv(PTR2IV(h));
319 STATIC UV a_detag(pTHX_ const SV *hint) {
320 #define a_detag(H) a_detag(aTHX_ (H))
327 #endif /* A_THREADSAFE */
329 if (!(hint && SvIOK(hint)))
332 h = INT2PTR(a_hint_t *, SvIVX(hint));
334 h = ptable_fetch(MY_CXT.tbl, h);
335 #endif /* A_THREADSAFE */
337 if (a_require_tag() != h->require_tag)
343 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
345 #define a_tag(B) newSVuv(B)
346 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
353 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
359 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
361 /* Used both for hints and op flags */
362 #define A_HINT_STRICT 1
363 #define A_HINT_WARN 2
364 #define A_HINT_FETCH 4
365 #define A_HINT_STORE 8
366 #define A_HINT_EXISTS 16
367 #define A_HINT_DELETE 32
368 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
369 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
370 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
372 /* Only used in op flags */
373 #define A_HINT_ROOT 64
374 #define A_HINT_DEREF 128
376 STATIC U32 a_hash = 0;
378 STATIC UV a_hint(pTHX) {
379 #define a_hint() a_hint(aTHX)
381 #ifdef cop_hints_fetch_pvn
382 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
383 #elif A_HAS_PERL(5, 9, 5)
384 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
386 __PACKAGE__, __PACKAGE_LEN__,
390 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
395 return a_detag(hint);
398 /* ... op => info map ...................................................... */
406 #define PTABLE_NAME ptable_map
407 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
411 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
412 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
413 #define ptable_map_delete(T, K) ptable_map_delete(aPTBLMS_ (T), (K))
415 STATIC ptable *a_op_map = NULL;
419 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
421 STATIC perl_mutex a_op_map_mutex;
423 #define A_LOCK(M) MUTEX_LOCK(M)
424 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
426 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
427 const a_op_info *val;
429 A_LOCK(&a_op_map_mutex);
431 val = ptable_fetch(a_op_map, o);
437 A_UNLOCK(&a_op_map_mutex);
442 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
444 #else /* USE_ITHREADS */
446 #define dA_MAP_THX dNOOP
448 #define A_LOCK(M) NOOP
449 #define A_UNLOCK(M) NOOP
451 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
453 #endif /* !USE_ITHREADS */
455 STATIC const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
456 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
459 if (!(oi = ptable_fetch(a_op_map, o))) {
460 oi = PerlMemShared_malloc(sizeof *oi);
461 ptable_map_store(a_op_map, o, oi);
471 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
472 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
473 A_LOCK(&a_op_map_mutex);
475 a_map_store_locked(o, old_pp, next, flags);
477 A_UNLOCK(&a_op_map_mutex);
480 STATIC void a_map_delete(pTHX_ const OP *o) {
481 #define a_map_delete(O) a_map_delete(aTHX_ (O))
482 A_LOCK(&a_op_map_mutex);
484 ptable_map_delete(a_op_map, o);
486 A_UNLOCK(&a_op_map_mutex);
489 STATIC const OP *a_map_descend(const OP *o) {
490 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
494 case OA_BASEOP_OR_UNOP:
495 return cUNOPo->op_first;
498 return cLISTOPo->op_last;
504 STATIC void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
505 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
506 const a_op_info *roi;
510 A_LOCK(&a_op_map_mutex);
512 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
514 while (o->op_flags & OPf_KIDS) {
515 o = a_map_descend(o);
518 if ((oi = ptable_fetch(a_op_map, o))) {
519 oi->flags &= ~A_HINT_ROOT;
520 oi->next = (a_op_info *) roi;
525 A_UNLOCK(&a_op_map_mutex);
530 STATIC void a_map_update_flags_topdown(const OP *root, UV flags) {
534 A_LOCK(&a_op_map_mutex);
536 flags &= ~A_HINT_ROOT;
539 if ((oi = ptable_fetch(a_op_map, o)))
540 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
541 if (!(o->op_flags & OPf_KIDS))
543 o = a_map_descend(o);
546 A_UNLOCK(&a_op_map_mutex);
551 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
553 STATIC void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
556 A_LOCK(&a_op_map_mutex);
558 flags &= ~A_HINT_ROOT;
559 rflags |= A_HINT_ROOT;
561 oi = ptable_fetch(a_op_map, o);
562 while (!(oi->flags & A_HINT_ROOT)) {
568 A_UNLOCK(&a_op_map_mutex);
573 /* ... Decide whether this expression should be autovivified or not ........ */
575 STATIC UV a_map_resolve(const OP *o, const a_op_info *oi) {
576 UV flags = 0, rflags;
578 const a_op_info *roi = oi;
580 while (!(roi->flags & A_HINT_ROOT))
585 rflags = roi->flags & ~A_HINT_ROOT;
590 if (root->op_flags & OPf_MOD) {
591 if (rflags & A_HINT_STORE)
592 flags = (A_HINT_STORE|A_HINT_DEREF);
593 } else if (rflags & A_HINT_FETCH)
594 flags = (A_HINT_FETCH|A_HINT_DEREF);
598 a_map_update_flags_bottomup(o, 0, 0);
602 flags |= (rflags & A_HINT_NOTIFY);
603 a_map_update_flags_bottomup(o, flags, 0);
605 return oi->flags & A_HINT_ROOT ? 0 : flags;
608 /* ... Inspired from pp_defined() .......................................... */
610 STATIC int a_undef(pTHX_ SV *sv) {
611 #define a_undef(S) a_undef(aTHX_ (S))
612 switch (SvTYPE(sv)) {
616 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
617 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
621 if (HvARRAY(sv) || SvGMAGICAL(sv)
622 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
634 /* --- PP functions -------------------------------------------------------- */
636 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
637 * value, another extension might have saved our pp replacement as the ppaddr
638 * for this op, so this doesn't ensure that our function will never be called
639 * again. That's why we don't remove the op info from our map, so that it can
640 * still run correctly if required. */
642 /* ... pp_rv2av ............................................................ */
644 STATIC OP *a_pp_rv2av(pTHX) {
649 oi = a_map_fetch(PL_op);
651 if (oi->flags & A_HINT_DEREF) {
653 /* We always need to push an empty array to fool the pp_aelem() that comes
657 av = sv_2mortal((SV *) newAV());
663 return oi->old_pp(aTHX);
666 /* ... pp_rv2hv ............................................................ */
668 STATIC OP *a_pp_rv2hv_simple(pTHX) {
673 oi = a_map_fetch(PL_op);
675 if (oi->flags & A_HINT_DEREF) {
680 return oi->old_pp(aTHX);
683 STATIC OP *a_pp_rv2hv(pTHX) {
688 oi = a_map_fetch(PL_op);
690 if (oi->flags & A_HINT_DEREF) {
694 hv = sv_2mortal((SV *) newHV());
700 return oi->old_pp(aTHX);
703 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
705 STATIC OP *a_pp_deref(pTHX) {
711 oi = a_map_fetch(PL_op);
714 if (flags & A_HINT_DEREF) {
717 o = oi->old_pp(aTHX);
719 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
722 if (flags & A_HINT_STRICT)
723 croak("Reference vivification forbidden");
724 else if (flags & A_HINT_WARN)
725 warn("Reference was vivified");
726 else /* A_HINT_STORE */
727 croak("Can't vivify reference");
734 return oi->old_pp(aTHX);
737 /* ... pp_root (exists,delete,keys,values) ................................. */
739 STATIC OP *a_pp_root_unop(pTHX) {
744 /* Can only be reached by keys or values */
745 if (GIMME_V == G_SCALAR) {
754 const a_op_info *oi = a_map_fetch(PL_op);
755 return oi->old_pp(aTHX);
759 STATIC OP *a_pp_root_binop(pTHX) {
762 if (a_undef(TOPm1s)) {
765 if (PL_op->op_type == OP_EXISTS)
773 const a_op_info *oi = a_map_fetch(PL_op);
774 return oi->old_pp(aTHX);
778 /* --- Check functions ----------------------------------------------------- */
780 STATIC void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
781 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
783 if (o->op_type == type && o->op_ppaddr != new_pp
784 && cUNOPo->op_first->op_type != OP_GV) {
786 const a_op_info *oi = a_map_fetch(o);
788 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
789 o->op_ppaddr = new_pp;
796 /* ... ck_pad{any,sv} ...................................................... */
798 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
799 * function, but are instead manually mutated from a padany. So we store
800 * the op entry in the op map in the padany check function, and we set their
801 * op_ppaddr member in our peephole optimizer replacement below. */
803 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
805 STATIC OP *a_ck_padany(pTHX_ OP *o) {
808 o = a_old_ck_padany(aTHX_ o);
811 if (hint & A_HINT_DO)
812 a_map_store_root(o, o->op_ppaddr, hint);
819 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
821 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
824 o = a_old_ck_padsv(aTHX_ o);
827 if (hint & A_HINT_DO) {
828 a_map_store_root(o, o->op_ppaddr, hint);
829 o->op_ppaddr = a_pp_deref;
836 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
838 /* Those ops appear both at the root and inside an expression but there's no
839 * way to distinguish both situations. Worse, we can't even know if we are in a
840 * modifying context, so the expression can't be resolved yet. It will be at the
841 * first invocation of a_pp_deref() for this expression. */
843 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
844 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
845 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
847 STATIC OP *a_ck_deref(pTHX_ OP *o) {
848 OP * (*old_ck)(pTHX_ OP *o) = 0;
851 switch (o->op_type) {
853 old_ck = a_old_ck_aelem;
854 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
855 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
858 old_ck = a_old_ck_helem;
859 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
860 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
863 old_ck = a_old_ck_rv2sv;
868 if (hint & A_HINT_DO) {
869 a_map_store_root(o, o->op_ppaddr, hint);
870 o->op_ppaddr = a_pp_deref;
877 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
879 /* Those ops also appear both inisde and at the root, hence the caveats for
880 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
881 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
884 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
885 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
887 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
888 OP * (*old_ck)(pTHX_ OP *o) = 0;
889 OP * (*new_pp)(pTHX) = 0;
892 switch (o->op_type) {
893 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
894 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
898 if (cUNOPo->op_first->op_type == OP_GV)
902 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
903 a_map_store_root(o, o->op_ppaddr, hint);
904 o->op_ppaddr = new_pp;
911 /* ... ck_xslice (aslice,hslice) ........................................... */
913 /* I think those are only found at the root, but there's nothing that really
914 * prevent them to be inside the expression too. We only need to update the
915 * root so that the rest of the expression will see the right context when
916 * resolving. That's why we don't replace the ppaddr. */
918 STATIC OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
919 STATIC OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
921 STATIC OP *a_ck_xslice(pTHX_ OP *o) {
922 OP * (*old_ck)(pTHX_ OP *o) = 0;
925 switch (o->op_type) {
927 old_ck = a_old_ck_aslice;
930 old_ck = a_old_ck_hslice;
931 if (hint & A_HINT_DO)
932 a_recheck_rv2xv(cUNOPo->op_first->op_sibling, OP_RV2HV, a_pp_rv2hv);
937 if (hint & A_HINT_DO) {
938 a_map_store_root(o, 0, hint);
945 /* ... ck_root (exists,delete,keys,values) ................................. */
947 /* Those ops are only found at the root of a dereferencing expression. We can
948 * then resolve at compile time if vivification must take place or not. */
950 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
951 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
952 STATIC OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
953 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
955 STATIC OP *a_ck_root(pTHX_ OP *o) {
956 OP * (*old_ck)(pTHX_ OP *o) = 0;
957 OP * (*new_pp)(pTHX) = 0;
958 bool enabled = FALSE;
961 switch (o->op_type) {
963 old_ck = a_old_ck_exists;
964 new_pp = a_pp_root_binop;
965 enabled = hint & A_HINT_EXISTS;
968 old_ck = a_old_ck_delete;
969 new_pp = a_pp_root_binop;
970 enabled = hint & A_HINT_DELETE;
973 old_ck = a_old_ck_keys;
974 new_pp = a_pp_root_unop;
975 enabled = hint & A_HINT_FETCH;
978 old_ck = a_old_ck_values;
979 new_pp = a_pp_root_unop;
980 enabled = hint & A_HINT_FETCH;
985 if (hint & A_HINT_DO) {
987 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
988 a_map_store_root(o, o->op_ppaddr, hint);
989 o->op_ppaddr = new_pp;
999 /* ... Our peephole optimizer .............................................. */
1001 STATIC peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
1003 STATIC void a_peep_rec(pTHX_ OP *o, ptable *seen);
1005 STATIC void a_peep_rec(pTHX_ OP *o, ptable *seen) {
1006 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
1007 for (; o; o = o->op_next) {
1009 const a_op_info *oi = NULL;
1013 if (ptable_fetch(seen, o))
1015 ptable_seen_store(seen, o, o);
1018 switch (o->op_type) {
1023 if (ptable_fetch(seen, o))
1025 ptable_seen_store(seen, o, o);
1029 if (o->op_ppaddr != a_pp_deref) {
1030 oi = a_map_fetch(o);
1031 if (oi && (oi->flags & A_HINT_DO)) {
1032 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1033 o->op_ppaddr = a_pp_deref;
1041 if (o->op_ppaddr != a_pp_deref)
1043 oi = a_map_fetch(o);
1047 if (!(flags & A_HINT_DEREF)
1048 && (flags & A_HINT_DO)
1049 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1050 /* Decide if the expression must autovivify or not. */
1051 flags = a_map_resolve(o, oi);
1053 if (flags & A_HINT_DEREF)
1054 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1056 o->op_ppaddr = oi->old_pp;
1060 if ( o->op_ppaddr != a_pp_rv2av
1061 && o->op_ppaddr != a_pp_rv2hv
1062 && o->op_ppaddr != a_pp_rv2hv_simple)
1064 oi = a_map_fetch(o);
1067 if (!(oi->flags & A_HINT_DEREF))
1068 o->op_ppaddr = oi->old_pp;
1079 # if A_HAS_PERL(5, 10, 0)
1084 a_peep_rec(cLOGOPo->op_other);
1088 a_peep_rec(cLOOPo->op_redoop);
1089 a_peep_rec(cLOOPo->op_nextop);
1090 a_peep_rec(cLOOPo->op_lastop);
1092 # if A_HAS_PERL(5, 9, 5)
1094 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1100 a_peep_rec(cPMOPo->op_pmreplstart);
1103 #endif /* !A_HAS_RPEEP */
1110 STATIC void a_peep(pTHX_ OP *o) {
1112 ptable *seen = MY_CXT.seen;
1114 a_old_peep(aTHX_ o);
1117 ptable_seen_clear(seen);
1119 ptable_seen_clear(seen);
1123 /* --- Interpreter setup/teardown ------------------------------------------ */
1125 STATIC U32 a_initialized = 0;
1127 STATIC void a_teardown(pTHX_ void *root) {
1139 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1140 ptable_hints_free(MY_CXT.tbl);
1142 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1143 ptable_seen_free(MY_CXT.seen);
1147 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1148 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1150 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1151 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1152 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1154 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1155 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1157 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1158 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1160 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1161 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1162 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1163 a_ck_restore(OP_VALUES, &a_old_ck_values);
1166 PL_rpeepp = a_old_peep;
1168 PL_peepp = a_old_peep;
1175 STATIC void a_setup(pTHX) {
1176 #define a_setup() a_setup(aTHX)
1182 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1183 MY_CXT.tbl = ptable_new();
1184 MY_CXT.owner = aTHX;
1185 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1186 MY_CXT.seen = ptable_new();
1189 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1190 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1192 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1193 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1194 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1196 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1197 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1199 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1200 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1202 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1203 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1204 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1205 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1208 a_old_peep = PL_rpeepp;
1211 a_old_peep = PL_peepp;
1216 call_atexit(a_teardown, aTHX);
1218 call_atexit(a_teardown, NULL);
1224 STATIC U32 a_booted = 0;
1226 /* --- XS ------------------------------------------------------------------ */
1228 MODULE = autovivification PACKAGE = autovivification
1237 a_op_map = ptable_new();
1239 MUTEX_INIT(&a_op_map_mutex);
1242 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1244 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1245 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1246 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1247 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1248 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1249 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1250 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1251 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1252 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1253 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1265 #if A_WORKAROUND_REQUIRE_PROPAGATION
1273 #if A_WORKAROUND_REQUIRE_PROPAGATION
1275 a_ptable_clone_ud ud;
1278 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1279 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1280 a_ptable_clone_ud_deinit(ud);
1287 #if A_WORKAROUND_REQUIRE_PROPAGATION
1289 MY_CXT.owner = aTHX;
1293 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1299 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1301 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &a_endav_vtbl, NULL, 0);
1306 _THREAD_CLEANUP(...)
1309 a_thread_cleanup(aTHX_ NULL);
1312 #endif /* A_THREADSAFE */
1318 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1328 RETVAL = newSVuv(a_detag(tag));