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 /* --- Helpers ------------------------------------------------------------- */
80 /* ... Thread-safe hints ................................................... */
82 #if A_WORKAROUND_REQUIRE_PROPAGATION
89 #define A_HINT_FREE(H) PerlMemShared_free(H)
93 #define PTABLE_NAME ptable_hints
94 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
103 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
104 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
106 #endif /* A_THREADSAFE */
108 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
110 #define PTABLE_NAME ptable_seen
111 #define PTABLE_VAL_FREE(V) NOOP
115 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
116 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
117 #define ptable_seen_clear(T) ptable_seen_clear(aPTBLMS_ (T))
118 #define ptable_seen_free(T) ptable_seen_free(aPTBLMS_ (T))
120 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
123 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
124 ptable *tbl; /* It really is a ptable_hints */
126 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
127 ptable *seen; /* It really is a ptable_seen */
134 #if A_WORKAROUND_REQUIRE_PROPAGATION
138 #if A_HAS_PERL(5, 13, 2)
139 CLONE_PARAMS *params;
145 #if A_HAS_PERL(5, 13, 2)
146 # define a_ptable_clone_ud_init(U, T, O) \
148 (U).params = Perl_clone_params_new((O), aTHX)
149 # define a_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
150 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
152 # define a_ptable_clone_ud_init(U, T, O) \
154 (U).params.stashes = newAV(); \
155 (U).params.flags = 0; \
156 (U).params.proto_perl = (O)
157 # define a_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
158 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
161 STATIC void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
162 a_ptable_clone_ud *ud = ud_;
163 a_hint_t *h1 = ent->val;
166 h2 = PerlMemShared_malloc(sizeof *h2);
168 h2->require_tag = PTR2IV(a_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
170 ptable_hints_store(ud->tbl, ent->key, h2);
173 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
177 STATIC void a_thread_cleanup(pTHX_ void *ud) {
180 #if A_WORKAROUND_REQUIRE_PROPAGATION
181 ptable_hints_free(MY_CXT.tbl);
182 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
183 ptable_seen_free(MY_CXT.seen);
186 #endif /* A_THREADSAFE */
188 #if A_WORKAROUND_REQUIRE_PROPAGATION
190 STATIC IV a_require_tag(pTHX) {
191 #define a_require_tag() a_require_tag(aTHX)
192 const CV *cv, *outside;
197 /* If for some reason the pragma is operational at run-time, try to discover
198 * the current cv in use. */
201 for (si = PL_curstackinfo; si; si = si->si_prev) {
204 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
205 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
207 switch (CxTYPE(cx)) {
210 /* The propagation workaround is only needed up to 5.10.0 and at that
211 * time format and sub contexts were still identical. And even later the
212 * cv members offsets should have been kept the same. */
214 goto get_enclosing_cv;
216 cv = cx->blk_eval.cv;
217 goto get_enclosing_cv;
228 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
234 STATIC SV *a_tag(pTHX_ UV bits) {
235 #define a_tag(B) a_tag(aTHX_ (B))
238 h = PerlMemShared_malloc(sizeof *h);
240 h->require_tag = a_require_tag();
245 /* We only need for the key to be an unique tag for looking up the value later
246 * Allocated memory provides convenient unique identifiers, so that's why we
247 * use the hint as the key itself. */
248 ptable_hints_store(MY_CXT.tbl, h, h);
250 #endif /* A_THREADSAFE */
252 return newSViv(PTR2IV(h));
255 STATIC UV a_detag(pTHX_ const SV *hint) {
256 #define a_detag(H) a_detag(aTHX_ (H))
259 if (!(hint && SvIOK(hint)))
262 h = INT2PTR(a_hint_t *, SvIVX(hint));
266 h = ptable_fetch(MY_CXT.tbl, h);
268 #endif /* A_THREADSAFE */
270 if (a_require_tag() != h->require_tag)
276 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
278 #define a_tag(B) newSVuv(B)
279 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
286 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
292 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
294 /* Used both for hints and op flags */
295 #define A_HINT_STRICT 1
296 #define A_HINT_WARN 2
297 #define A_HINT_FETCH 4
298 #define A_HINT_STORE 8
299 #define A_HINT_EXISTS 16
300 #define A_HINT_DELETE 32
301 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
302 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
303 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
305 /* Only used in op flags */
306 #define A_HINT_ROOT 64
307 #define A_HINT_DEREF 128
309 STATIC U32 a_hash = 0;
311 STATIC UV a_hint(pTHX) {
312 #define a_hint() a_hint(aTHX)
314 #ifdef cop_hints_fetch_pvn
315 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
316 #elif A_HAS_PERL(5, 9, 5)
317 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
319 __PACKAGE__, __PACKAGE_LEN__,
323 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
328 return a_detag(hint);
331 /* ... op => info map ...................................................... */
339 #define PTABLE_NAME ptable_map
340 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
344 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
345 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
346 #define ptable_map_delete(T, K) ptable_map_delete(aPTBLMS_ (T), (K))
348 STATIC ptable *a_op_map = NULL;
352 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
354 STATIC perl_mutex a_op_map_mutex;
356 #define A_LOCK(M) MUTEX_LOCK(M)
357 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
359 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
360 const a_op_info *val;
362 A_LOCK(&a_op_map_mutex);
364 val = ptable_fetch(a_op_map, o);
370 A_UNLOCK(&a_op_map_mutex);
375 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
377 #else /* USE_ITHREADS */
379 #define dA_MAP_THX dNOOP
381 #define A_LOCK(M) NOOP
382 #define A_UNLOCK(M) NOOP
384 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
386 #endif /* !USE_ITHREADS */
388 STATIC const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
389 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
392 if (!(oi = ptable_fetch(a_op_map, o))) {
393 oi = PerlMemShared_malloc(sizeof *oi);
394 ptable_map_store(a_op_map, o, oi);
404 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
405 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
406 A_LOCK(&a_op_map_mutex);
408 a_map_store_locked(o, old_pp, next, flags);
410 A_UNLOCK(&a_op_map_mutex);
413 STATIC void a_map_delete(pTHX_ const OP *o) {
414 #define a_map_delete(O) a_map_delete(aTHX_ (O))
415 A_LOCK(&a_op_map_mutex);
417 ptable_map_delete(a_op_map, o);
419 A_UNLOCK(&a_op_map_mutex);
422 STATIC const OP *a_map_descend(const OP *o) {
423 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
427 case OA_BASEOP_OR_UNOP:
428 return cUNOPo->op_first;
431 return cLISTOPo->op_last;
437 STATIC void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
438 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
439 const a_op_info *roi;
443 A_LOCK(&a_op_map_mutex);
445 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
447 while (o->op_flags & OPf_KIDS) {
448 o = a_map_descend(o);
451 if ((oi = ptable_fetch(a_op_map, o))) {
452 oi->flags &= ~A_HINT_ROOT;
453 oi->next = (a_op_info *) roi;
458 A_UNLOCK(&a_op_map_mutex);
463 STATIC void a_map_update_flags_topdown(const OP *root, UV flags) {
467 A_LOCK(&a_op_map_mutex);
469 flags &= ~A_HINT_ROOT;
472 if ((oi = ptable_fetch(a_op_map, o)))
473 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
474 if (!(o->op_flags & OPf_KIDS))
476 o = a_map_descend(o);
479 A_UNLOCK(&a_op_map_mutex);
484 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
486 STATIC void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
489 A_LOCK(&a_op_map_mutex);
491 flags &= ~A_HINT_ROOT;
492 rflags |= A_HINT_ROOT;
494 oi = ptable_fetch(a_op_map, o);
495 while (!(oi->flags & A_HINT_ROOT)) {
501 A_UNLOCK(&a_op_map_mutex);
506 /* ... Decide whether this expression should be autovivified or not ........ */
508 STATIC UV a_map_resolve(const OP *o, const a_op_info *oi) {
509 UV flags = 0, rflags;
511 const a_op_info *roi = oi;
513 while (!(roi->flags & A_HINT_ROOT))
518 rflags = roi->flags & ~A_HINT_ROOT;
523 if (root->op_flags & OPf_MOD) {
524 if (rflags & A_HINT_STORE)
525 flags = (A_HINT_STORE|A_HINT_DEREF);
526 } else if (rflags & A_HINT_FETCH)
527 flags = (A_HINT_FETCH|A_HINT_DEREF);
531 a_map_update_flags_bottomup(o, 0, 0);
535 flags |= (rflags & A_HINT_NOTIFY);
536 a_map_update_flags_bottomup(o, flags, 0);
538 return oi->flags & A_HINT_ROOT ? 0 : flags;
541 /* ... Inspired from pp_defined() .......................................... */
543 STATIC int a_undef(pTHX_ SV *sv) {
544 #define a_undef(S) a_undef(aTHX_ (S))
545 switch (SvTYPE(sv)) {
549 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
550 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
554 if (HvARRAY(sv) || SvGMAGICAL(sv)
555 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
567 /* --- PP functions -------------------------------------------------------- */
569 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
570 * value, another extension might have saved our pp replacement as the ppaddr
571 * for this op, so this doesn't ensure that our function will never be called
572 * again. That's why we don't remove the op info from our map, so that it can
573 * still run correctly if required. */
575 /* ... pp_rv2av ............................................................ */
577 STATIC OP *a_pp_rv2av(pTHX) {
582 oi = a_map_fetch(PL_op);
584 if (oi->flags & A_HINT_DEREF) {
586 /* We always need to push an empty array to fool the pp_aelem() that comes
590 av = sv_2mortal((SV *) newAV());
596 return oi->old_pp(aTHX);
599 /* ... pp_rv2hv ............................................................ */
601 STATIC OP *a_pp_rv2hv_simple(pTHX) {
606 oi = a_map_fetch(PL_op);
608 if (oi->flags & A_HINT_DEREF) {
613 return oi->old_pp(aTHX);
616 STATIC OP *a_pp_rv2hv(pTHX) {
621 oi = a_map_fetch(PL_op);
623 if (oi->flags & A_HINT_DEREF) {
627 hv = sv_2mortal((SV *) newHV());
633 return oi->old_pp(aTHX);
636 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
638 STATIC OP *a_pp_deref(pTHX) {
644 oi = a_map_fetch(PL_op);
647 if (flags & A_HINT_DEREF) {
650 o = oi->old_pp(aTHX);
652 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
655 if (flags & A_HINT_STRICT)
656 croak("Reference vivification forbidden");
657 else if (flags & A_HINT_WARN)
658 warn("Reference was vivified");
659 else /* A_HINT_STORE */
660 croak("Can't vivify reference");
667 return oi->old_pp(aTHX);
670 /* ... pp_root (exists,delete,keys,values) ................................. */
672 STATIC OP *a_pp_root_unop(pTHX) {
677 /* Can only be reached by keys or values */
678 if (GIMME_V == G_SCALAR) {
687 const a_op_info *oi = a_map_fetch(PL_op);
688 return oi->old_pp(aTHX);
692 STATIC OP *a_pp_root_binop(pTHX) {
695 if (a_undef(TOPm1s)) {
698 if (PL_op->op_type == OP_EXISTS)
706 const a_op_info *oi = a_map_fetch(PL_op);
707 return oi->old_pp(aTHX);
711 /* --- Check functions ----------------------------------------------------- */
713 STATIC void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
714 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
716 if (o->op_type == type && o->op_ppaddr != new_pp
717 && cUNOPo->op_first->op_type != OP_GV) {
719 const a_op_info *oi = a_map_fetch(o);
721 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
722 o->op_ppaddr = new_pp;
729 /* ... ck_pad{any,sv} ...................................................... */
731 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
732 * function, but are instead manually mutated from a padany. So we store
733 * the op entry in the op map in the padany check function, and we set their
734 * op_ppaddr member in our peephole optimizer replacement below. */
736 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
738 STATIC OP *a_ck_padany(pTHX_ OP *o) {
741 o = a_old_ck_padany(aTHX_ o);
744 if (hint & A_HINT_DO)
745 a_map_store_root(o, o->op_ppaddr, hint);
752 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
754 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
757 o = a_old_ck_padsv(aTHX_ o);
760 if (hint & A_HINT_DO) {
761 a_map_store_root(o, o->op_ppaddr, hint);
762 o->op_ppaddr = a_pp_deref;
769 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
771 /* Those ops appear both at the root and inside an expression but there's no
772 * way to distinguish both situations. Worse, we can't even know if we are in a
773 * modifying context, so the expression can't be resolved yet. It will be at the
774 * first invocation of a_pp_deref() for this expression. */
776 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
777 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
778 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
780 STATIC OP *a_ck_deref(pTHX_ OP *o) {
781 OP * (*old_ck)(pTHX_ OP *o) = 0;
784 switch (o->op_type) {
786 old_ck = a_old_ck_aelem;
787 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
788 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
791 old_ck = a_old_ck_helem;
792 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
793 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
796 old_ck = a_old_ck_rv2sv;
801 if (hint & A_HINT_DO) {
802 a_map_store_root(o, o->op_ppaddr, hint);
803 o->op_ppaddr = a_pp_deref;
810 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
812 /* Those ops also appear both inisde and at the root, hence the caveats for
813 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
814 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
817 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
818 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
820 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
821 OP * (*old_ck)(pTHX_ OP *o) = 0;
822 OP * (*new_pp)(pTHX) = 0;
825 switch (o->op_type) {
826 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
827 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
831 if (cUNOPo->op_first->op_type == OP_GV)
835 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
836 a_map_store_root(o, o->op_ppaddr, hint);
837 o->op_ppaddr = new_pp;
844 /* ... ck_xslice (aslice,hslice) ........................................... */
846 /* I think those are only found at the root, but there's nothing that really
847 * prevent them to be inside the expression too. We only need to update the
848 * root so that the rest of the expression will see the right context when
849 * resolving. That's why we don't replace the ppaddr. */
851 STATIC OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
852 STATIC OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
854 STATIC OP *a_ck_xslice(pTHX_ OP *o) {
855 OP * (*old_ck)(pTHX_ OP *o) = 0;
858 switch (o->op_type) {
860 old_ck = a_old_ck_aslice;
863 old_ck = a_old_ck_hslice;
864 if (hint & A_HINT_DO)
865 a_recheck_rv2xv(cUNOPo->op_first->op_sibling, OP_RV2HV, a_pp_rv2hv);
870 if (hint & A_HINT_DO) {
871 a_map_store_root(o, 0, hint);
878 /* ... ck_root (exists,delete,keys,values) ................................. */
880 /* Those ops are only found at the root of a dereferencing expression. We can
881 * then resolve at compile time if vivification must take place or not. */
883 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
884 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
885 STATIC OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
886 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
888 STATIC OP *a_ck_root(pTHX_ OP *o) {
889 OP * (*old_ck)(pTHX_ OP *o) = 0;
890 OP * (*new_pp)(pTHX) = 0;
891 bool enabled = FALSE;
894 switch (o->op_type) {
896 old_ck = a_old_ck_exists;
897 new_pp = a_pp_root_binop;
898 enabled = hint & A_HINT_EXISTS;
901 old_ck = a_old_ck_delete;
902 new_pp = a_pp_root_binop;
903 enabled = hint & A_HINT_DELETE;
906 old_ck = a_old_ck_keys;
907 new_pp = a_pp_root_unop;
908 enabled = hint & A_HINT_FETCH;
911 old_ck = a_old_ck_values;
912 new_pp = a_pp_root_unop;
913 enabled = hint & A_HINT_FETCH;
918 if (hint & A_HINT_DO) {
920 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
921 a_map_store_root(o, o->op_ppaddr, hint);
922 o->op_ppaddr = new_pp;
932 /* ... Our peephole optimizer .............................................. */
934 STATIC peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
936 STATIC void a_peep_rec(pTHX_ OP *o, ptable *seen);
938 STATIC void a_peep_rec(pTHX_ OP *o, ptable *seen) {
939 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
940 for (; o; o = o->op_next) {
942 const a_op_info *oi = NULL;
945 if (ptable_fetch(seen, o))
947 ptable_seen_store(seen, o, o);
949 switch (o->op_type) {
951 if (o->op_ppaddr != a_pp_deref) {
953 if (oi && (oi->flags & A_HINT_DO)) {
954 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
955 o->op_ppaddr = a_pp_deref;
963 if (o->op_ppaddr != a_pp_deref)
969 if (!(flags & A_HINT_DEREF)
970 && (flags & A_HINT_DO)
971 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
972 /* Decide if the expression must autovivify or not. */
973 flags = a_map_resolve(o, oi);
975 if (flags & A_HINT_DEREF)
976 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
978 o->op_ppaddr = oi->old_pp;
982 if ( o->op_ppaddr != a_pp_rv2av
983 && o->op_ppaddr != a_pp_rv2hv
984 && o->op_ppaddr != a_pp_rv2hv_simple)
989 if (!(oi->flags & A_HINT_DEREF))
990 o->op_ppaddr = oi->old_pp;
1001 # if A_HAS_PERL(5, 10, 0)
1006 a_peep_rec(cLOGOPo->op_other);
1010 a_peep_rec(cLOOPo->op_redoop);
1011 a_peep_rec(cLOOPo->op_nextop);
1012 a_peep_rec(cLOOPo->op_lastop);
1014 # if A_HAS_PERL(5, 9, 5)
1016 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1022 a_peep_rec(cPMOPo->op_pmreplstart);
1025 #endif /* !A_HAS_RPEEP */
1032 STATIC void a_peep(pTHX_ OP *o) {
1034 ptable *seen = MY_CXT.seen;
1036 a_old_peep(aTHX_ o);
1038 ptable_seen_clear(seen);
1040 ptable_seen_clear(seen);
1043 /* --- Interpreter setup/teardown ------------------------------------------ */
1045 STATIC U32 a_initialized = 0;
1047 STATIC void a_teardown(pTHX_ void *root) {
1059 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1060 ptable_hints_free(MY_CXT.tbl);
1061 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1062 ptable_seen_free(MY_CXT.seen);
1065 PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_old_ck_padany);
1066 a_old_ck_padany = 0;
1067 PL_check[OP_PADSV] = MEMBER_TO_FPTR(a_old_ck_padsv);
1070 PL_check[OP_AELEM] = MEMBER_TO_FPTR(a_old_ck_aelem);
1072 PL_check[OP_HELEM] = MEMBER_TO_FPTR(a_old_ck_helem);
1074 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(a_old_ck_rv2sv);
1077 PL_check[OP_RV2AV] = MEMBER_TO_FPTR(a_old_ck_rv2av);
1079 PL_check[OP_RV2HV] = MEMBER_TO_FPTR(a_old_ck_rv2hv);
1082 PL_check[OP_ASLICE] = MEMBER_TO_FPTR(a_old_ck_aslice);
1083 a_old_ck_aslice = 0;
1084 PL_check[OP_HSLICE] = MEMBER_TO_FPTR(a_old_ck_hslice);
1085 a_old_ck_hslice = 0;
1087 PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_old_ck_exists);
1088 a_old_ck_exists = 0;
1089 PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_old_ck_delete);
1090 a_old_ck_delete = 0;
1091 PL_check[OP_KEYS] = MEMBER_TO_FPTR(a_old_ck_keys);
1093 PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_old_ck_values);
1094 a_old_ck_values = 0;
1097 PL_rpeepp = a_old_peep;
1099 PL_peepp = a_old_peep;
1106 STATIC void a_setup(pTHX) {
1107 #define a_setup() a_setup(aTHX)
1113 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1114 MY_CXT.tbl = ptable_new();
1115 MY_CXT.owner = aTHX;
1116 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1117 MY_CXT.seen = ptable_new();
1120 a_old_ck_padany = PL_check[OP_PADANY];
1121 PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_ck_padany);
1122 a_old_ck_padsv = PL_check[OP_PADSV];
1123 PL_check[OP_PADSV] = MEMBER_TO_FPTR(a_ck_padsv);
1125 a_old_ck_aelem = PL_check[OP_AELEM];
1126 PL_check[OP_AELEM] = MEMBER_TO_FPTR(a_ck_deref);
1127 a_old_ck_helem = PL_check[OP_HELEM];
1128 PL_check[OP_HELEM] = MEMBER_TO_FPTR(a_ck_deref);
1129 a_old_ck_rv2sv = PL_check[OP_RV2SV];
1130 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(a_ck_deref);
1132 a_old_ck_rv2av = PL_check[OP_RV2AV];
1133 PL_check[OP_RV2AV] = MEMBER_TO_FPTR(a_ck_rv2xv);
1134 a_old_ck_rv2hv = PL_check[OP_RV2HV];
1135 PL_check[OP_RV2HV] = MEMBER_TO_FPTR(a_ck_rv2xv);
1137 a_old_ck_aslice = PL_check[OP_ASLICE];
1138 PL_check[OP_ASLICE] = MEMBER_TO_FPTR(a_ck_xslice);
1139 a_old_ck_hslice = PL_check[OP_HSLICE];
1140 PL_check[OP_HSLICE] = MEMBER_TO_FPTR(a_ck_xslice);
1142 a_old_ck_exists = PL_check[OP_EXISTS];
1143 PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_ck_root);
1144 a_old_ck_delete = PL_check[OP_DELETE];
1145 PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_ck_root);
1146 a_old_ck_keys = PL_check[OP_KEYS];
1147 PL_check[OP_KEYS] = MEMBER_TO_FPTR(a_ck_root);
1148 a_old_ck_values = PL_check[OP_VALUES];
1149 PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_ck_root);
1152 a_old_peep = PL_rpeepp;
1155 a_old_peep = PL_peepp;
1160 call_atexit(a_teardown, aTHX);
1162 call_atexit(a_teardown, NULL);
1168 STATIC U32 a_booted = 0;
1170 /* --- XS ------------------------------------------------------------------ */
1172 MODULE = autovivification PACKAGE = autovivification
1181 a_op_map = ptable_new();
1183 MUTEX_INIT(&a_op_map_mutex);
1186 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1188 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1189 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1190 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1191 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1192 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1193 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1194 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1195 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1196 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1197 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1209 #if A_WORKAROUND_REQUIRE_PROPAGATION
1216 #if A_WORKAROUND_REQUIRE_PROPAGATION
1218 a_ptable_clone_ud ud;
1221 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1222 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1223 a_ptable_clone_ud_deinit(ud);
1230 #if A_WORKAROUND_REQUIRE_PROPAGATION
1232 MY_CXT.owner = aTHX;
1236 reap(3, a_thread_cleanup, NULL);
1239 #endif /* A_THREADSAFE */
1245 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1255 RETVAL = newSVuv(a_detag(tag));