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)
33 # define OP_SIBLING(O) ((O)->op_sibling)
36 /* ... Thread safety and multiplicity ...................................... */
38 /* Always safe when the workaround isn't needed */
39 #if !A_WORKAROUND_REQUIRE_PROPAGATION
42 /* Otherwise, safe unless Makefile.PL says it's Win32 */
43 #elif !defined(A_FORKSAFE)
47 #ifndef A_MULTIPLICITY
48 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
49 # define A_MULTIPLICITY 1
51 # define A_MULTIPLICITY 0
56 # define tTHX PerlInterpreter*
59 #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))
60 # define A_THREADSAFE 1
62 # define MY_CXT_CLONE \
64 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
65 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
66 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
69 # define A_THREADSAFE 0
71 # define dMY_CXT dNOOP
73 # define MY_CXT a_globaldata
75 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
77 # define MY_CXT_INIT NOOP
79 # define MY_CXT_CLONE NOOP
82 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
83 # define A_CHECK_MUTEX_LOCK OP_CHECK_MUTEX_LOCK
84 # define A_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
86 # define A_CHECK_MUTEX_LOCK OP_REFCNT_LOCK
87 # define A_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
90 typedef OP *(*a_ck_t)(pTHX_ OP *);
92 #ifdef wrap_op_checker
94 # define a_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
98 STATIC void a_ck_replace(pTHX_ OPCODE type, a_ck_t new_ck, a_ck_t *old_ck_p) {
99 #define a_ck_replace(T, NC, OCP) a_ck_replace(aTHX_ (T), (NC), (OCP))
102 *old_ck_p = PL_check[type];
103 PL_check[type] = new_ck;
105 A_CHECK_MUTEX_UNLOCK;
110 STATIC void a_ck_restore(pTHX_ OPCODE type, a_ck_t *old_ck_p) {
111 #define a_ck_restore(T, OCP) a_ck_restore(aTHX_ (T), (OCP))
114 PL_check[type] = *old_ck_p;
117 A_CHECK_MUTEX_UNLOCK;
120 /* --- Helpers ------------------------------------------------------------- */
122 /* ... Thread-safe hints ................................................... */
124 #if A_WORKAROUND_REQUIRE_PROPAGATION
131 #define A_HINT_FREE(H) PerlMemShared_free(H)
135 #define PTABLE_NAME ptable_hints
136 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
145 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
146 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
148 #endif /* A_THREADSAFE */
150 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
152 #define PTABLE_NAME ptable_seen
153 #define PTABLE_VAL_FREE(V) NOOP
157 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
158 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
159 #define ptable_seen_clear(T) ptable_seen_clear(aPTBLMS_ (T))
160 #define ptable_seen_free(T) ptable_seen_free(aPTBLMS_ (T))
162 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
165 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
166 ptable *tbl; /* It really is a ptable_hints */
168 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
169 ptable *seen; /* It really is a ptable_seen */
176 #if A_WORKAROUND_REQUIRE_PROPAGATION
180 #if A_HAS_PERL(5, 13, 2)
181 CLONE_PARAMS *params;
187 #if A_HAS_PERL(5, 13, 2)
188 # define a_ptable_clone_ud_init(U, T, O) \
190 (U).params = Perl_clone_params_new((O), aTHX)
191 # define a_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
192 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
194 # define a_ptable_clone_ud_init(U, T, O) \
196 (U).params.stashes = newAV(); \
197 (U).params.flags = 0; \
198 (U).params.proto_perl = (O)
199 # define a_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
200 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
203 STATIC void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
204 a_ptable_clone_ud *ud = ud_;
205 a_hint_t *h1 = ent->val;
208 h2 = PerlMemShared_malloc(sizeof *h2);
210 h2->require_tag = PTR2IV(a_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
212 ptable_hints_store(ud->tbl, ent->key, h2);
215 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
217 STATIC void a_thread_cleanup(pTHX_ void *ud) {
220 #if A_WORKAROUND_REQUIRE_PROPAGATION
221 ptable_hints_free(MY_CXT.tbl);
223 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
224 ptable_seen_free(MY_CXT.seen);
228 STATIC int a_endav_free(pTHX_ SV *sv, MAGIC *mg) {
229 SAVEDESTRUCTOR_X(a_thread_cleanup, NULL);
234 STATIC MGVTBL a_endav_vtbl = {
251 #endif /* A_THREADSAFE */
253 #if A_WORKAROUND_REQUIRE_PROPAGATION
255 STATIC IV a_require_tag(pTHX) {
256 #define a_require_tag() a_require_tag(aTHX)
257 const CV *cv, *outside;
262 /* If for some reason the pragma is operational at run-time, try to discover
263 * the current cv in use. */
266 for (si = PL_curstackinfo; si; si = si->si_prev) {
269 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
270 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
272 switch (CxTYPE(cx)) {
275 /* The propagation workaround is only needed up to 5.10.0 and at that
276 * time format and sub contexts were still identical. And even later the
277 * cv members offsets should have been kept the same. */
279 goto get_enclosing_cv;
281 cv = cx->blk_eval.cv;
282 goto get_enclosing_cv;
293 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
299 STATIC SV *a_tag(pTHX_ UV bits) {
300 #define a_tag(B) a_tag(aTHX_ (B))
307 #endif /* A_THREADSAFE */
309 h = PerlMemShared_malloc(sizeof *h);
311 h->require_tag = a_require_tag();
314 /* We only need for the key to be an unique tag for looking up the value later
315 * Allocated memory provides convenient unique identifiers, so that's why we
316 * use the hint as the key itself. */
317 ptable_hints_store(MY_CXT.tbl, h, h);
318 #endif /* A_THREADSAFE */
320 return newSViv(PTR2IV(h));
323 STATIC UV a_detag(pTHX_ const SV *hint) {
324 #define a_detag(H) a_detag(aTHX_ (H))
331 #endif /* A_THREADSAFE */
333 if (!(hint && SvIOK(hint)))
336 h = INT2PTR(a_hint_t *, SvIVX(hint));
338 h = ptable_fetch(MY_CXT.tbl, h);
339 #endif /* A_THREADSAFE */
341 if (a_require_tag() != h->require_tag)
347 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
349 #define a_tag(B) newSVuv(B)
350 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
357 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
363 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
365 /* Used both for hints and op flags */
366 #define A_HINT_STRICT 1
367 #define A_HINT_WARN 2
368 #define A_HINT_FETCH 4
369 #define A_HINT_STORE 8
370 #define A_HINT_EXISTS 16
371 #define A_HINT_DELETE 32
372 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
373 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
374 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
376 /* Only used in op flags */
377 #define A_HINT_ROOT 64
378 #define A_HINT_DEREF 128
380 STATIC U32 a_hash = 0;
382 STATIC UV a_hint(pTHX) {
383 #define a_hint() a_hint(aTHX)
385 #ifdef cop_hints_fetch_pvn
386 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
387 #elif A_HAS_PERL(5, 9, 5)
388 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
390 __PACKAGE__, __PACKAGE_LEN__,
394 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
399 return a_detag(hint);
402 /* ... op => info map ...................................................... */
410 #define PTABLE_NAME ptable_map
411 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
415 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
416 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
417 #define ptable_map_delete(T, K) ptable_map_delete(aPTBLMS_ (T), (K))
419 STATIC ptable *a_op_map = NULL;
423 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
425 STATIC perl_mutex a_op_map_mutex;
427 #define A_LOCK(M) MUTEX_LOCK(M)
428 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
430 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
431 const a_op_info *val;
433 A_LOCK(&a_op_map_mutex);
435 val = ptable_fetch(a_op_map, o);
441 A_UNLOCK(&a_op_map_mutex);
446 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
448 #else /* USE_ITHREADS */
450 #define dA_MAP_THX dNOOP
452 #define A_LOCK(M) NOOP
453 #define A_UNLOCK(M) NOOP
455 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
457 #endif /* !USE_ITHREADS */
459 STATIC const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
460 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
463 if (!(oi = ptable_fetch(a_op_map, o))) {
464 oi = PerlMemShared_malloc(sizeof *oi);
465 ptable_map_store(a_op_map, o, oi);
475 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
476 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
477 A_LOCK(&a_op_map_mutex);
479 a_map_store_locked(o, old_pp, next, flags);
481 A_UNLOCK(&a_op_map_mutex);
484 STATIC void a_map_delete(pTHX_ const OP *o) {
485 #define a_map_delete(O) a_map_delete(aTHX_ (O))
486 A_LOCK(&a_op_map_mutex);
488 ptable_map_delete(a_op_map, o);
490 A_UNLOCK(&a_op_map_mutex);
493 STATIC const OP *a_map_descend(const OP *o) {
494 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
498 case OA_BASEOP_OR_UNOP:
499 return cUNOPo->op_first;
502 return cLISTOPo->op_last;
508 STATIC void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
509 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
510 const a_op_info *roi;
514 A_LOCK(&a_op_map_mutex);
516 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
518 while (o->op_flags & OPf_KIDS) {
519 o = a_map_descend(o);
522 if ((oi = ptable_fetch(a_op_map, o))) {
523 oi->flags &= ~A_HINT_ROOT;
524 oi->next = (a_op_info *) roi;
529 A_UNLOCK(&a_op_map_mutex);
534 STATIC void a_map_update_flags_topdown(const OP *root, UV flags) {
538 A_LOCK(&a_op_map_mutex);
540 flags &= ~A_HINT_ROOT;
543 if ((oi = ptable_fetch(a_op_map, o)))
544 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
545 if (!(o->op_flags & OPf_KIDS))
547 o = a_map_descend(o);
550 A_UNLOCK(&a_op_map_mutex);
555 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
557 STATIC void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
560 A_LOCK(&a_op_map_mutex);
562 flags &= ~A_HINT_ROOT;
563 rflags |= A_HINT_ROOT;
565 oi = ptable_fetch(a_op_map, o);
566 while (!(oi->flags & A_HINT_ROOT)) {
572 A_UNLOCK(&a_op_map_mutex);
577 /* ... Decide whether this expression should be autovivified or not ........ */
579 STATIC UV a_map_resolve(const OP *o, const a_op_info *oi) {
580 UV flags = 0, rflags;
582 const a_op_info *roi = oi;
584 while (!(roi->flags & A_HINT_ROOT))
589 rflags = roi->flags & ~A_HINT_ROOT;
594 if (root->op_flags & OPf_MOD) {
595 if (rflags & A_HINT_STORE)
596 flags = (A_HINT_STORE|A_HINT_DEREF);
597 } else if (rflags & A_HINT_FETCH)
598 flags = (A_HINT_FETCH|A_HINT_DEREF);
602 a_map_update_flags_bottomup(o, 0, 0);
606 flags |= (rflags & A_HINT_NOTIFY);
607 a_map_update_flags_bottomup(o, flags, 0);
609 return oi->flags & A_HINT_ROOT ? 0 : flags;
612 /* ... Inspired from pp_defined() .......................................... */
614 STATIC int a_undef(pTHX_ SV *sv) {
615 #define a_undef(S) a_undef(aTHX_ (S))
616 switch (SvTYPE(sv)) {
620 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
621 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
625 if (HvARRAY(sv) || SvGMAGICAL(sv)
626 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
638 /* --- PP functions -------------------------------------------------------- */
640 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
641 * value, another extension might have saved our pp replacement as the ppaddr
642 * for this op, so this doesn't ensure that our function will never be called
643 * again. That's why we don't remove the op info from our map, so that it can
644 * still run correctly if required. */
646 /* ... pp_rv2av ............................................................ */
648 STATIC OP *a_pp_rv2av(pTHX) {
653 oi = a_map_fetch(PL_op);
655 if (oi->flags & A_HINT_DEREF) {
657 /* We always need to push an empty array to fool the pp_aelem() that comes
661 av = sv_2mortal((SV *) newAV());
667 return oi->old_pp(aTHX);
670 /* ... pp_rv2hv ............................................................ */
672 STATIC OP *a_pp_rv2hv_simple(pTHX) {
677 oi = a_map_fetch(PL_op);
679 if (oi->flags & A_HINT_DEREF) {
684 return oi->old_pp(aTHX);
687 STATIC OP *a_pp_rv2hv(pTHX) {
692 oi = a_map_fetch(PL_op);
694 if (oi->flags & A_HINT_DEREF) {
698 hv = sv_2mortal((SV *) newHV());
704 return oi->old_pp(aTHX);
707 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
709 STATIC OP *a_pp_deref(pTHX) {
715 oi = a_map_fetch(PL_op);
718 if (flags & A_HINT_DEREF) {
721 o = oi->old_pp(aTHX);
723 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
726 if (flags & A_HINT_STRICT)
727 croak("Reference vivification forbidden");
728 else if (flags & A_HINT_WARN)
729 warn("Reference was vivified");
730 else /* A_HINT_STORE */
731 croak("Can't vivify reference");
738 return oi->old_pp(aTHX);
741 /* ... pp_root (exists,delete,keys,values) ................................. */
743 STATIC OP *a_pp_root_unop(pTHX) {
748 /* Can only be reached by keys or values */
749 if (GIMME_V == G_SCALAR) {
758 const a_op_info *oi = a_map_fetch(PL_op);
759 return oi->old_pp(aTHX);
763 STATIC OP *a_pp_root_binop(pTHX) {
766 if (a_undef(TOPm1s)) {
769 if (PL_op->op_type == OP_EXISTS)
777 const a_op_info *oi = a_map_fetch(PL_op);
778 return oi->old_pp(aTHX);
782 /* --- Check functions ----------------------------------------------------- */
784 STATIC void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
785 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
787 if (o->op_type == type && o->op_ppaddr != new_pp
788 && cUNOPo->op_first->op_type != OP_GV) {
790 const a_op_info *oi = a_map_fetch(o);
792 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
793 o->op_ppaddr = new_pp;
800 /* ... ck_pad{any,sv} ...................................................... */
802 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
803 * function, but are instead manually mutated from a padany. So we store
804 * the op entry in the op map in the padany check function, and we set their
805 * op_ppaddr member in our peephole optimizer replacement below. */
807 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
809 STATIC OP *a_ck_padany(pTHX_ OP *o) {
812 o = a_old_ck_padany(aTHX_ o);
815 if (hint & A_HINT_DO)
816 a_map_store_root(o, o->op_ppaddr, hint);
823 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
825 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
828 o = a_old_ck_padsv(aTHX_ o);
831 if (hint & A_HINT_DO) {
832 a_map_store_root(o, o->op_ppaddr, hint);
833 o->op_ppaddr = a_pp_deref;
840 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
842 /* Those ops appear both at the root and inside an expression but there's no
843 * way to distinguish both situations. Worse, we can't even know if we are in a
844 * modifying context, so the expression can't be resolved yet. It will be at the
845 * first invocation of a_pp_deref() for this expression. */
847 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
848 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
849 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
851 STATIC OP *a_ck_deref(pTHX_ OP *o) {
852 OP * (*old_ck)(pTHX_ OP *o) = 0;
855 switch (o->op_type) {
857 old_ck = a_old_ck_aelem;
858 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
859 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
862 old_ck = a_old_ck_helem;
863 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
864 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
867 old_ck = a_old_ck_rv2sv;
872 if (hint & A_HINT_DO) {
873 a_map_store_root(o, o->op_ppaddr, hint);
874 o->op_ppaddr = a_pp_deref;
881 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
883 /* Those ops also appear both inisde and at the root, hence the caveats for
884 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
885 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
888 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
889 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
891 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
892 OP * (*old_ck)(pTHX_ OP *o) = 0;
893 OP * (*new_pp)(pTHX) = 0;
896 switch (o->op_type) {
897 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
898 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
902 if (cUNOPo->op_first->op_type == OP_GV)
906 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
907 a_map_store_root(o, o->op_ppaddr, hint);
908 o->op_ppaddr = new_pp;
915 /* ... ck_xslice (aslice,hslice) ........................................... */
917 /* I think those are only found at the root, but there's nothing that really
918 * prevent them to be inside the expression too. We only need to update the
919 * root so that the rest of the expression will see the right context when
920 * resolving. That's why we don't replace the ppaddr. */
922 STATIC OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
923 STATIC OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
925 STATIC OP *a_ck_xslice(pTHX_ OP *o) {
926 OP * (*old_ck)(pTHX_ OP *o) = 0;
929 switch (o->op_type) {
931 old_ck = a_old_ck_aslice;
934 old_ck = a_old_ck_hslice;
935 if (hint & A_HINT_DO)
936 a_recheck_rv2xv(OP_SIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv);
941 if (hint & A_HINT_DO) {
942 a_map_store_root(o, 0, hint);
949 /* ... ck_root (exists,delete,keys,values) ................................. */
951 /* Those ops are only found at the root of a dereferencing expression. We can
952 * then resolve at compile time if vivification must take place or not. */
954 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
955 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
956 STATIC OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
957 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
959 STATIC OP *a_ck_root(pTHX_ OP *o) {
960 OP * (*old_ck)(pTHX_ OP *o) = 0;
961 OP * (*new_pp)(pTHX) = 0;
962 bool enabled = FALSE;
965 switch (o->op_type) {
967 old_ck = a_old_ck_exists;
968 new_pp = a_pp_root_binop;
969 enabled = hint & A_HINT_EXISTS;
972 old_ck = a_old_ck_delete;
973 new_pp = a_pp_root_binop;
974 enabled = hint & A_HINT_DELETE;
977 old_ck = a_old_ck_keys;
978 new_pp = a_pp_root_unop;
979 enabled = hint & A_HINT_FETCH;
982 old_ck = a_old_ck_values;
983 new_pp = a_pp_root_unop;
984 enabled = hint & A_HINT_FETCH;
989 if (hint & A_HINT_DO) {
991 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
992 a_map_store_root(o, o->op_ppaddr, hint);
993 o->op_ppaddr = new_pp;
1003 /* ... Our peephole optimizer .............................................. */
1005 STATIC peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
1007 STATIC void a_peep_rec(pTHX_ OP *o, ptable *seen);
1009 STATIC void a_peep_rec(pTHX_ OP *o, ptable *seen) {
1010 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
1011 for (; o; o = o->op_next) {
1013 const a_op_info *oi = NULL;
1017 if (ptable_fetch(seen, o))
1019 ptable_seen_store(seen, o, o);
1022 switch (o->op_type) {
1028 if (ptable_fetch(seen, o))
1030 ptable_seen_store(seen, o, o);
1034 if (o->op_ppaddr != a_pp_deref) {
1035 oi = a_map_fetch(o);
1036 if (oi && (oi->flags & A_HINT_DO)) {
1037 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1038 o->op_ppaddr = a_pp_deref;
1046 if (o->op_ppaddr != a_pp_deref)
1048 oi = a_map_fetch(o);
1052 if (!(flags & A_HINT_DEREF)
1053 && (flags & A_HINT_DO)
1054 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1055 /* Decide if the expression must autovivify or not. */
1056 flags = a_map_resolve(o, oi);
1058 if (flags & A_HINT_DEREF)
1059 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1061 o->op_ppaddr = oi->old_pp;
1065 if ( o->op_ppaddr != a_pp_rv2av
1066 && o->op_ppaddr != a_pp_rv2hv
1067 && o->op_ppaddr != a_pp_rv2hv_simple)
1069 oi = a_map_fetch(o);
1072 if (!(oi->flags & A_HINT_DEREF))
1073 o->op_ppaddr = oi->old_pp;
1084 # if A_HAS_PERL(5, 10, 0)
1089 a_peep_rec(cLOGOPo->op_other);
1093 a_peep_rec(cLOOPo->op_redoop);
1094 a_peep_rec(cLOOPo->op_nextop);
1095 a_peep_rec(cLOOPo->op_lastop);
1097 # if A_HAS_PERL(5, 9, 5)
1099 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1105 a_peep_rec(cPMOPo->op_pmreplstart);
1108 #endif /* !A_HAS_RPEEP */
1115 STATIC void a_peep(pTHX_ OP *o) {
1117 ptable *seen = MY_CXT.seen;
1119 a_old_peep(aTHX_ o);
1122 ptable_seen_clear(seen);
1124 ptable_seen_clear(seen);
1128 /* --- Interpreter setup/teardown ------------------------------------------ */
1130 STATIC U32 a_initialized = 0;
1132 STATIC void a_teardown(pTHX_ void *root) {
1144 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1145 ptable_hints_free(MY_CXT.tbl);
1147 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1148 ptable_seen_free(MY_CXT.seen);
1152 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1153 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1155 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1156 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1157 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1159 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1160 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1162 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1163 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1165 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1166 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1167 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1168 a_ck_restore(OP_VALUES, &a_old_ck_values);
1171 PL_rpeepp = a_old_peep;
1173 PL_peepp = a_old_peep;
1180 STATIC void a_setup(pTHX) {
1181 #define a_setup() a_setup(aTHX)
1187 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1188 MY_CXT.tbl = ptable_new();
1189 MY_CXT.owner = aTHX;
1190 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1191 MY_CXT.seen = ptable_new();
1194 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1195 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1197 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1198 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1199 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1201 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1202 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1204 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1205 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1207 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1208 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1209 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1210 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1213 a_old_peep = PL_rpeepp;
1216 a_old_peep = PL_peepp;
1221 call_atexit(a_teardown, aTHX);
1223 call_atexit(a_teardown, NULL);
1229 STATIC U32 a_booted = 0;
1231 /* --- XS ------------------------------------------------------------------ */
1233 MODULE = autovivification PACKAGE = autovivification
1242 a_op_map = ptable_new();
1244 MUTEX_INIT(&a_op_map_mutex);
1247 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1249 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1250 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1251 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1252 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1253 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1254 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1255 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1256 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1257 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1258 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1270 #if A_WORKAROUND_REQUIRE_PROPAGATION
1277 #if A_WORKAROUND_REQUIRE_PROPAGATION
1280 a_ptable_clone_ud ud;
1283 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1284 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1285 a_ptable_clone_ud_deinit(ud);
1292 #if A_WORKAROUND_REQUIRE_PROPAGATION
1294 MY_CXT.owner = aTHX;
1298 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1304 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1306 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &a_endav_vtbl, NULL, 0);
1311 _THREAD_CLEANUP(...)
1314 a_thread_cleanup(aTHX_ NULL);
1317 #endif /* A_THREADSAFE */
1323 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1333 RETVAL = newSVuv(a_detag(tag));