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)
34 # define OpSIBLING(O) OP_SIBLING(O)
36 # define OpSIBLING(O) ((O)->op_sibling)
40 /* ... Thread safety and multiplicity ...................................... */
42 /* Always safe when the workaround isn't needed */
43 #if !A_WORKAROUND_REQUIRE_PROPAGATION
46 /* Otherwise, safe unless Makefile.PL says it's Win32 */
47 #elif !defined(A_FORKSAFE)
51 #ifndef A_MULTIPLICITY
52 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
53 # define A_MULTIPLICITY 1
55 # define A_MULTIPLICITY 0
60 # define tTHX PerlInterpreter*
63 #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))
64 # define A_THREADSAFE 1
66 # define MY_CXT_CLONE \
68 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
69 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
70 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
73 # define A_THREADSAFE 0
75 # define dMY_CXT dNOOP
77 # define MY_CXT a_globaldata
79 # define START_MY_CXT static my_cxt_t MY_CXT;
81 # define MY_CXT_INIT NOOP
83 # define MY_CXT_CLONE NOOP
86 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
87 # define A_CHECK_MUTEX_LOCK OP_CHECK_MUTEX_LOCK
88 # define A_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
90 # define A_CHECK_MUTEX_LOCK OP_REFCNT_LOCK
91 # define A_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
94 typedef OP *(*a_ck_t)(pTHX_ OP *);
96 #ifdef wrap_op_checker
98 # define a_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
102 static void a_ck_replace(pTHX_ OPCODE type, a_ck_t new_ck, a_ck_t *old_ck_p) {
103 #define a_ck_replace(T, NC, OCP) a_ck_replace(aTHX_ (T), (NC), (OCP))
106 *old_ck_p = PL_check[type];
107 PL_check[type] = new_ck;
109 A_CHECK_MUTEX_UNLOCK;
114 static void a_ck_restore(pTHX_ OPCODE type, a_ck_t *old_ck_p) {
115 #define a_ck_restore(T, OCP) a_ck_restore(aTHX_ (T), (OCP))
118 PL_check[type] = *old_ck_p;
121 A_CHECK_MUTEX_UNLOCK;
124 /* --- Helpers ------------------------------------------------------------- */
126 /* ... Thread-safe hints ................................................... */
128 #if A_WORKAROUND_REQUIRE_PROPAGATION
135 #define A_HINT_FREE(H) PerlMemShared_free(H)
139 #define PTABLE_NAME ptable_hints
140 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
149 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
150 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
152 #endif /* A_THREADSAFE */
154 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
156 #define PTABLE_NAME ptable_seen
157 #define PTABLE_VAL_FREE(V) NOOP
161 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
162 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
163 #define ptable_seen_clear(T) ptable_seen_clear(aPTBLMS_ (T))
164 #define ptable_seen_free(T) ptable_seen_free(aPTBLMS_ (T))
166 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
169 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
170 ptable *tbl; /* It really is a ptable_hints */
172 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
173 ptable *seen; /* It really is a ptable_seen */
180 #if A_WORKAROUND_REQUIRE_PROPAGATION
184 #if A_HAS_PERL(5, 13, 2)
185 CLONE_PARAMS *params;
191 #if A_HAS_PERL(5, 13, 2)
192 # define a_ptable_clone_ud_init(U, T, O) \
194 (U).params = Perl_clone_params_new((O), aTHX)
195 # define a_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
196 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
198 # define a_ptable_clone_ud_init(U, T, O) \
200 (U).params.stashes = newAV(); \
201 (U).params.flags = 0; \
202 (U).params.proto_perl = (O)
203 # define a_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
204 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
207 static void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
208 a_ptable_clone_ud *ud = ud_;
209 a_hint_t *h1 = ent->val;
212 h2 = PerlMemShared_malloc(sizeof *h2);
214 h2->require_tag = PTR2IV(a_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
216 ptable_hints_store(ud->tbl, ent->key, h2);
219 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
221 static void a_thread_cleanup(pTHX_ void *ud) {
224 #if A_WORKAROUND_REQUIRE_PROPAGATION
225 ptable_hints_free(MY_CXT.tbl);
227 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
228 ptable_seen_free(MY_CXT.seen);
232 static int a_endav_free(pTHX_ SV *sv, MAGIC *mg) {
233 SAVEDESTRUCTOR_X(a_thread_cleanup, NULL);
238 static MGVTBL a_endav_vtbl = {
255 #endif /* A_THREADSAFE */
257 #if A_WORKAROUND_REQUIRE_PROPAGATION
259 static IV a_require_tag(pTHX) {
260 #define a_require_tag() a_require_tag(aTHX)
261 const CV *cv, *outside;
266 /* If for some reason the pragma is operational at run-time, try to discover
267 * the current cv in use. */
270 for (si = PL_curstackinfo; si; si = si->si_prev) {
273 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
274 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
276 switch (CxTYPE(cx)) {
279 /* The propagation workaround is only needed up to 5.10.0 and at that
280 * time format and sub contexts were still identical. And even later the
281 * cv members offsets should have been kept the same. */
283 goto get_enclosing_cv;
285 cv = cx->blk_eval.cv;
286 goto get_enclosing_cv;
297 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
303 static SV *a_tag(pTHX_ UV bits) {
304 #define a_tag(B) a_tag(aTHX_ (B))
311 #endif /* A_THREADSAFE */
313 h = PerlMemShared_malloc(sizeof *h);
315 h->require_tag = a_require_tag();
318 /* We only need for the key to be an unique tag for looking up the value later
319 * Allocated memory provides convenient unique identifiers, so that's why we
320 * use the hint as the key itself. */
321 ptable_hints_store(MY_CXT.tbl, h, h);
322 #endif /* A_THREADSAFE */
324 return newSViv(PTR2IV(h));
327 static UV a_detag(pTHX_ const SV *hint) {
328 #define a_detag(H) a_detag(aTHX_ (H))
335 #endif /* A_THREADSAFE */
337 if (!(hint && SvIOK(hint)))
340 h = INT2PTR(a_hint_t *, SvIVX(hint));
342 h = ptable_fetch(MY_CXT.tbl, h);
343 #endif /* A_THREADSAFE */
345 if (a_require_tag() != h->require_tag)
351 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
353 #define a_tag(B) newSVuv(B)
354 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
361 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
367 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
369 /* Used both for hints and op flags */
370 #define A_HINT_STRICT 1
371 #define A_HINT_WARN 2
372 #define A_HINT_FETCH 4
373 #define A_HINT_STORE 8
374 #define A_HINT_EXISTS 16
375 #define A_HINT_DELETE 32
376 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
377 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
378 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
380 /* Only used in op flags */
381 #define A_HINT_ROOT 64
382 #define A_HINT_DEREF 128
384 static U32 a_hash = 0;
386 static UV a_hint(pTHX) {
387 #define a_hint() a_hint(aTHX)
389 #ifdef cop_hints_fetch_pvn
390 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
391 #elif A_HAS_PERL(5, 9, 5)
392 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
394 __PACKAGE__, __PACKAGE_LEN__,
398 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
403 return a_detag(hint);
406 /* ... op => info map ...................................................... */
414 #define PTABLE_NAME ptable_map
415 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
419 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
420 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
421 #define ptable_map_delete(T, K) ptable_map_delete(aPTBLMS_ (T), (K))
423 static ptable *a_op_map = NULL;
427 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
429 static perl_mutex a_op_map_mutex;
431 #define A_LOCK(M) MUTEX_LOCK(M)
432 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
434 static const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
435 const a_op_info *val;
437 A_LOCK(&a_op_map_mutex);
439 val = ptable_fetch(a_op_map, o);
445 A_UNLOCK(&a_op_map_mutex);
450 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
452 #else /* USE_ITHREADS */
454 #define dA_MAP_THX dNOOP
456 #define A_LOCK(M) NOOP
457 #define A_UNLOCK(M) NOOP
459 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
461 #endif /* !USE_ITHREADS */
463 static const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
464 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
467 if (!(oi = ptable_fetch(a_op_map, o))) {
468 oi = PerlMemShared_malloc(sizeof *oi);
469 ptable_map_store(a_op_map, o, oi);
479 static void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
480 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
481 A_LOCK(&a_op_map_mutex);
483 a_map_store_locked(o, old_pp, next, flags);
485 A_UNLOCK(&a_op_map_mutex);
488 static void a_map_delete(pTHX_ const OP *o) {
489 #define a_map_delete(O) a_map_delete(aTHX_ (O))
490 A_LOCK(&a_op_map_mutex);
492 ptable_map_delete(a_op_map, o);
494 A_UNLOCK(&a_op_map_mutex);
497 static const OP *a_map_descend(const OP *o) {
498 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
502 case OA_BASEOP_OR_UNOP:
503 return cUNOPo->op_first;
506 return cLISTOPo->op_last;
512 static void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
513 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
514 const a_op_info *roi;
518 A_LOCK(&a_op_map_mutex);
520 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
522 while (o->op_flags & OPf_KIDS) {
523 o = a_map_descend(o);
526 if ((oi = ptable_fetch(a_op_map, o))) {
527 oi->flags &= ~A_HINT_ROOT;
528 oi->next = (a_op_info *) roi;
533 A_UNLOCK(&a_op_map_mutex);
538 static void a_map_update_flags_topdown(const OP *root, UV flags) {
542 A_LOCK(&a_op_map_mutex);
544 flags &= ~A_HINT_ROOT;
547 if ((oi = ptable_fetch(a_op_map, o)))
548 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
549 if (!(o->op_flags & OPf_KIDS))
551 o = a_map_descend(o);
554 A_UNLOCK(&a_op_map_mutex);
559 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
561 static void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
564 A_LOCK(&a_op_map_mutex);
566 flags &= ~A_HINT_ROOT;
567 rflags |= A_HINT_ROOT;
569 oi = ptable_fetch(a_op_map, o);
570 while (!(oi->flags & A_HINT_ROOT)) {
576 A_UNLOCK(&a_op_map_mutex);
581 /* ... Decide whether this expression should be autovivified or not ........ */
583 static UV a_map_resolve(const OP *o, const a_op_info *oi) {
584 UV flags = 0, rflags;
586 const a_op_info *roi = oi;
588 while (!(roi->flags & A_HINT_ROOT))
593 rflags = roi->flags & ~A_HINT_ROOT;
598 if (root->op_flags & OPf_MOD) {
599 if (rflags & A_HINT_STORE)
600 flags = (A_HINT_STORE|A_HINT_DEREF);
601 } else if (rflags & A_HINT_FETCH)
602 flags = (A_HINT_FETCH|A_HINT_DEREF);
606 a_map_update_flags_bottomup(o, 0, 0);
610 flags |= (rflags & A_HINT_NOTIFY);
611 a_map_update_flags_bottomup(o, flags, 0);
613 return oi->flags & A_HINT_ROOT ? 0 : flags;
616 /* ... Inspired from pp_defined() .......................................... */
618 static int a_undef(pTHX_ SV *sv) {
619 #define a_undef(S) a_undef(aTHX_ (S))
620 switch (SvTYPE(sv)) {
624 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
625 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
629 if (HvARRAY(sv) || SvGMAGICAL(sv)
630 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
642 /* --- PP functions -------------------------------------------------------- */
644 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
645 * value, another extension might have saved our pp replacement as the ppaddr
646 * for this op, so this doesn't ensure that our function will never be called
647 * again. That's why we don't remove the op info from our map, so that it can
648 * still run correctly if required. */
650 /* ... pp_rv2av ............................................................ */
652 static OP *a_pp_rv2av(pTHX) {
657 oi = a_map_fetch(PL_op);
659 if (oi->flags & A_HINT_DEREF) {
661 /* We always need to push an empty array to fool the pp_aelem() that comes
665 av = sv_2mortal((SV *) newAV());
671 return oi->old_pp(aTHX);
674 /* ... pp_rv2hv ............................................................ */
676 static OP *a_pp_rv2hv_simple(pTHX) {
681 oi = a_map_fetch(PL_op);
683 if (oi->flags & A_HINT_DEREF) {
688 return oi->old_pp(aTHX);
691 static OP *a_pp_rv2hv(pTHX) {
696 oi = a_map_fetch(PL_op);
698 if (oi->flags & A_HINT_DEREF) {
702 hv = sv_2mortal((SV *) newHV());
708 return oi->old_pp(aTHX);
711 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
713 static OP *a_pp_deref(pTHX) {
719 oi = a_map_fetch(PL_op);
722 if (flags & A_HINT_DEREF) {
725 o = oi->old_pp(aTHX);
727 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
730 if (flags & A_HINT_STRICT)
731 croak("Reference vivification forbidden");
732 else if (flags & A_HINT_WARN)
733 warn("Reference was vivified");
734 else /* A_HINT_STORE */
735 croak("Can't vivify reference");
742 return oi->old_pp(aTHX);
745 /* ... pp_root (exists,delete,keys,values) ................................. */
747 static OP *a_pp_root_unop(pTHX) {
752 /* Can only be reached by keys or values */
753 if (GIMME_V == G_SCALAR) {
762 const a_op_info *oi = a_map_fetch(PL_op);
763 return oi->old_pp(aTHX);
767 static OP *a_pp_root_binop(pTHX) {
770 if (a_undef(TOPm1s)) {
773 if (PL_op->op_type == OP_EXISTS)
781 const a_op_info *oi = a_map_fetch(PL_op);
782 return oi->old_pp(aTHX);
786 /* --- Check functions ----------------------------------------------------- */
788 static void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
789 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
791 if (o->op_type == type && o->op_ppaddr != new_pp
792 && cUNOPo->op_first->op_type != OP_GV) {
794 const a_op_info *oi = a_map_fetch(o);
796 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
797 o->op_ppaddr = new_pp;
804 /* ... ck_pad{any,sv} ...................................................... */
806 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
807 * function, but are instead manually mutated from a padany. So we store
808 * the op entry in the op map in the padany check function, and we set their
809 * op_ppaddr member in our peephole optimizer replacement below. */
811 static OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
813 static OP *a_ck_padany(pTHX_ OP *o) {
816 o = a_old_ck_padany(aTHX_ o);
819 if (hint & A_HINT_DO)
820 a_map_store_root(o, o->op_ppaddr, hint);
827 static OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
829 static OP *a_ck_padsv(pTHX_ OP *o) {
832 o = a_old_ck_padsv(aTHX_ o);
835 if (hint & A_HINT_DO) {
836 a_map_store_root(o, o->op_ppaddr, hint);
837 o->op_ppaddr = a_pp_deref;
844 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
846 /* Those ops appear both at the root and inside an expression but there's no
847 * way to distinguish both situations. Worse, we can't even know if we are in a
848 * modifying context, so the expression can't be resolved yet. It will be at the
849 * first invocation of a_pp_deref() for this expression. */
851 static OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
852 static OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
853 static OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
855 static OP *a_ck_deref(pTHX_ OP *o) {
856 OP * (*old_ck)(pTHX_ OP *o) = 0;
859 switch (o->op_type) {
861 old_ck = a_old_ck_aelem;
862 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
863 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
866 old_ck = a_old_ck_helem;
867 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
868 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
871 old_ck = a_old_ck_rv2sv;
876 if (hint & A_HINT_DO) {
877 a_map_store_root(o, o->op_ppaddr, hint);
878 o->op_ppaddr = a_pp_deref;
885 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
887 /* Those ops also appear both inisde and at the root, hence the caveats for
888 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
889 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
892 static OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
893 static OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
895 static OP *a_ck_rv2xv(pTHX_ OP *o) {
896 OP * (*old_ck)(pTHX_ OP *o) = 0;
897 OP * (*new_pp)(pTHX) = 0;
900 switch (o->op_type) {
901 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
902 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
906 if (cUNOPo->op_first->op_type == OP_GV)
910 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
911 a_map_store_root(o, o->op_ppaddr, hint);
912 o->op_ppaddr = new_pp;
919 /* ... ck_xslice (aslice,hslice) ........................................... */
921 /* I think those are only found at the root, but there's nothing that really
922 * prevent them to be inside the expression too. We only need to update the
923 * root so that the rest of the expression will see the right context when
924 * resolving. That's why we don't replace the ppaddr. */
926 static OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
927 static OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
929 static OP *a_ck_xslice(pTHX_ OP *o) {
930 OP * (*old_ck)(pTHX_ OP *o) = 0;
933 switch (o->op_type) {
935 old_ck = a_old_ck_aslice;
938 old_ck = a_old_ck_hslice;
939 if (hint & A_HINT_DO)
940 a_recheck_rv2xv(OpSIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv);
945 if (hint & A_HINT_DO) {
946 a_map_store_root(o, 0, hint);
953 /* ... ck_root (exists,delete,keys,values) ................................. */
955 /* Those ops are only found at the root of a dereferencing expression. We can
956 * then resolve at compile time if vivification must take place or not. */
958 static OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
959 static OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
960 static OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
961 static OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
963 static OP *a_ck_root(pTHX_ OP *o) {
964 OP * (*old_ck)(pTHX_ OP *o) = 0;
965 OP * (*new_pp)(pTHX) = 0;
966 bool enabled = FALSE;
969 switch (o->op_type) {
971 old_ck = a_old_ck_exists;
972 new_pp = a_pp_root_binop;
973 enabled = hint & A_HINT_EXISTS;
976 old_ck = a_old_ck_delete;
977 new_pp = a_pp_root_binop;
978 enabled = hint & A_HINT_DELETE;
981 old_ck = a_old_ck_keys;
982 new_pp = a_pp_root_unop;
983 enabled = hint & A_HINT_FETCH;
986 old_ck = a_old_ck_values;
987 new_pp = a_pp_root_unop;
988 enabled = hint & A_HINT_FETCH;
993 if (hint & A_HINT_DO) {
995 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
996 a_map_store_root(o, o->op_ppaddr, hint);
997 o->op_ppaddr = new_pp;
1007 /* ... Our peephole optimizer .............................................. */
1009 static peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
1011 static void a_peep_rec(pTHX_ OP *o, ptable *seen);
1013 static void a_peep_rec(pTHX_ OP *o, ptable *seen) {
1014 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
1015 for (; o; o = o->op_next) {
1017 const a_op_info *oi = NULL;
1021 if (ptable_fetch(seen, o))
1023 ptable_seen_store(seen, o, o);
1026 switch (o->op_type) {
1032 if (ptable_fetch(seen, o))
1034 ptable_seen_store(seen, o, o);
1038 if (o->op_ppaddr != a_pp_deref) {
1039 oi = a_map_fetch(o);
1040 if (oi && (oi->flags & A_HINT_DO)) {
1041 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1042 o->op_ppaddr = a_pp_deref;
1050 if (o->op_ppaddr != a_pp_deref)
1052 oi = a_map_fetch(o);
1056 if (!(flags & A_HINT_DEREF)
1057 && (flags & A_HINT_DO)
1058 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1059 /* Decide if the expression must autovivify or not. */
1060 flags = a_map_resolve(o, oi);
1062 if (flags & A_HINT_DEREF)
1063 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1065 o->op_ppaddr = oi->old_pp;
1069 if ( o->op_ppaddr != a_pp_rv2av
1070 && o->op_ppaddr != a_pp_rv2hv
1071 && o->op_ppaddr != a_pp_rv2hv_simple)
1073 oi = a_map_fetch(o);
1076 if (!(oi->flags & A_HINT_DEREF))
1077 o->op_ppaddr = oi->old_pp;
1088 # if A_HAS_PERL(5, 10, 0)
1093 a_peep_rec(cLOGOPo->op_other);
1097 a_peep_rec(cLOOPo->op_redoop);
1098 a_peep_rec(cLOOPo->op_nextop);
1099 a_peep_rec(cLOOPo->op_lastop);
1101 # if A_HAS_PERL(5, 9, 5)
1103 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1109 a_peep_rec(cPMOPo->op_pmreplstart);
1112 #endif /* !A_HAS_RPEEP */
1119 static void a_peep(pTHX_ OP *o) {
1121 ptable *seen = MY_CXT.seen;
1123 a_old_peep(aTHX_ o);
1126 ptable_seen_clear(seen);
1128 ptable_seen_clear(seen);
1132 /* --- Interpreter setup/teardown ------------------------------------------ */
1134 static U32 a_initialized = 0;
1136 static void a_teardown(pTHX_ void *root) {
1148 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1149 ptable_hints_free(MY_CXT.tbl);
1151 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1152 ptable_seen_free(MY_CXT.seen);
1156 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1157 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1159 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1160 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1161 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1163 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1164 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1166 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1167 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1169 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1170 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1171 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1172 a_ck_restore(OP_VALUES, &a_old_ck_values);
1175 PL_rpeepp = a_old_peep;
1177 PL_peepp = a_old_peep;
1184 static void a_setup(pTHX) {
1185 #define a_setup() a_setup(aTHX)
1191 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1192 MY_CXT.tbl = ptable_new();
1193 MY_CXT.owner = aTHX;
1194 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1195 MY_CXT.seen = ptable_new();
1198 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1199 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1201 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1202 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1203 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1205 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1206 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1208 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1209 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1211 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1212 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1213 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1214 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1217 a_old_peep = PL_rpeepp;
1220 a_old_peep = PL_peepp;
1225 call_atexit(a_teardown, aTHX);
1227 call_atexit(a_teardown, NULL);
1233 static U32 a_booted = 0;
1235 /* --- XS ------------------------------------------------------------------ */
1237 MODULE = autovivification PACKAGE = autovivification
1246 a_op_map = ptable_new();
1248 MUTEX_INIT(&a_op_map_mutex);
1251 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1253 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1254 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1255 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1256 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1257 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1258 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1259 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1260 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1261 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1262 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1274 #if A_WORKAROUND_REQUIRE_PROPAGATION
1281 #if A_WORKAROUND_REQUIRE_PROPAGATION
1284 a_ptable_clone_ud ud;
1287 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1288 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1289 a_ptable_clone_ud_deinit(ud);
1296 #if A_WORKAROUND_REQUIRE_PROPAGATION
1298 MY_CXT.owner = aTHX;
1302 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1308 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1310 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &a_endav_vtbl, NULL, 0);
1315 _THREAD_CLEANUP(...)
1318 a_thread_cleanup(aTHX_ NULL);
1321 #endif /* A_THREADSAFE */
1327 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1337 RETVAL = newSVuv(a_detag(tag));