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)
53 # define A_MULTIPLICITY 1
55 # define A_MULTIPLICITY 0
59 # ifndef PERL_IMPLICIT_CONTEXT
60 # error MULTIPLICITY builds must set PERL_IMPLICIT_CONTEXT
65 # define tTHX PerlInterpreter*
68 #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))
69 # define A_THREADSAFE 1
71 # define MY_CXT_CLONE \
73 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
74 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
75 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
78 # define A_THREADSAFE 0
80 # define dMY_CXT dNOOP
82 # define MY_CXT a_globaldata
84 # define START_MY_CXT static my_cxt_t MY_CXT;
86 # define MY_CXT_INIT NOOP
88 # define MY_CXT_CLONE NOOP
91 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
92 # define A_CHECK_MUTEX_LOCK OP_CHECK_MUTEX_LOCK
93 # define A_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
95 # define A_CHECK_MUTEX_LOCK OP_REFCNT_LOCK
96 # define A_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
99 typedef OP *(*a_ck_t)(pTHX_ OP *);
101 #ifdef wrap_op_checker
103 # define a_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
107 static void a_ck_replace(pTHX_ OPCODE type, a_ck_t new_ck, a_ck_t *old_ck_p) {
108 #define a_ck_replace(T, NC, OCP) a_ck_replace(aTHX_ (T), (NC), (OCP))
111 *old_ck_p = PL_check[type];
112 PL_check[type] = new_ck;
114 A_CHECK_MUTEX_UNLOCK;
119 static void a_ck_restore(pTHX_ OPCODE type, a_ck_t *old_ck_p) {
120 #define a_ck_restore(T, OCP) a_ck_restore(aTHX_ (T), (OCP))
123 PL_check[type] = *old_ck_p;
126 A_CHECK_MUTEX_UNLOCK;
129 /* --- Helpers ------------------------------------------------------------- */
131 /* ... Thread-safe hints ................................................... */
133 #if A_WORKAROUND_REQUIRE_PROPAGATION
140 #define A_HINT_FREE(H) PerlMemShared_free(H)
144 #define PTABLE_NAME ptable_hints
145 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
154 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
155 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
157 #endif /* A_THREADSAFE */
159 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
161 #define PTABLE_NAME ptable_seen
162 #define PTABLE_VAL_FREE(V) NOOP
166 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
167 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
168 #define ptable_seen_clear(T) ptable_seen_clear(aPTBLMS_ (T))
169 #define ptable_seen_free(T) ptable_seen_free(aPTBLMS_ (T))
171 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
174 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
175 ptable *tbl; /* It really is a ptable_hints */
177 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
178 ptable *seen; /* It really is a ptable_seen */
185 #if A_WORKAROUND_REQUIRE_PROPAGATION
189 #if A_HAS_PERL(5, 13, 2)
190 CLONE_PARAMS *params;
196 #if A_HAS_PERL(5, 13, 2)
197 # define a_ptable_clone_ud_init(U, T, O) \
199 (U).params = Perl_clone_params_new((O), aTHX)
200 # define a_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
201 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
203 # define a_ptable_clone_ud_init(U, T, O) \
205 (U).params.stashes = newAV(); \
206 (U).params.flags = 0; \
207 (U).params.proto_perl = (O)
208 # define a_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
209 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
212 static void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
213 a_ptable_clone_ud *ud = ud_;
214 a_hint_t *h1 = ent->val;
217 h2 = PerlMemShared_malloc(sizeof *h2);
219 h2->require_tag = PTR2IV(a_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
221 ptable_hints_store(ud->tbl, ent->key, h2);
224 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
226 static void a_thread_cleanup(pTHX_ void *ud) {
229 #if A_WORKAROUND_REQUIRE_PROPAGATION
230 ptable_hints_free(MY_CXT.tbl);
232 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
233 ptable_seen_free(MY_CXT.seen);
237 static int a_endav_free(pTHX_ SV *sv, MAGIC *mg) {
238 SAVEDESTRUCTOR_X(a_thread_cleanup, NULL);
243 static MGVTBL a_endav_vtbl = {
260 #endif /* A_THREADSAFE */
262 #if A_WORKAROUND_REQUIRE_PROPAGATION
264 static IV a_require_tag(pTHX) {
265 #define a_require_tag() a_require_tag(aTHX)
266 const CV *cv, *outside;
271 /* If for some reason the pragma is operational at run-time, try to discover
272 * the current cv in use. */
275 for (si = PL_curstackinfo; si; si = si->si_prev) {
278 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
279 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
281 switch (CxTYPE(cx)) {
284 /* The propagation workaround is only needed up to 5.10.0 and at that
285 * time format and sub contexts were still identical. And even later the
286 * cv members offsets should have been kept the same. */
288 goto get_enclosing_cv;
290 cv = cx->blk_eval.cv;
291 goto get_enclosing_cv;
302 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
308 static SV *a_tag(pTHX_ UV bits) {
309 #define a_tag(B) a_tag(aTHX_ (B))
316 #endif /* A_THREADSAFE */
318 h = PerlMemShared_malloc(sizeof *h);
320 h->require_tag = a_require_tag();
323 /* We only need for the key to be an unique tag for looking up the value later
324 * Allocated memory provides convenient unique identifiers, so that's why we
325 * use the hint as the key itself. */
326 ptable_hints_store(MY_CXT.tbl, h, h);
327 #endif /* A_THREADSAFE */
329 return newSViv(PTR2IV(h));
332 static UV a_detag(pTHX_ const SV *hint) {
333 #define a_detag(H) a_detag(aTHX_ (H))
340 #endif /* A_THREADSAFE */
342 if (!(hint && SvIOK(hint)))
345 h = INT2PTR(a_hint_t *, SvIVX(hint));
347 h = ptable_fetch(MY_CXT.tbl, h);
348 #endif /* A_THREADSAFE */
350 if (a_require_tag() != h->require_tag)
356 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
358 #define a_tag(B) newSVuv(B)
359 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
366 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
372 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
374 /* Used both for hints and op flags */
375 #define A_HINT_STRICT 1
376 #define A_HINT_WARN 2
377 #define A_HINT_FETCH 4
378 #define A_HINT_STORE 8
379 #define A_HINT_EXISTS 16
380 #define A_HINT_DELETE 32
381 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
382 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
383 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
385 /* Only used in op flags */
386 #define A_HINT_ROOT 64
387 #define A_HINT_DEREF 128
389 static U32 a_hash = 0;
391 static UV a_hint(pTHX) {
392 #define a_hint() a_hint(aTHX)
394 #ifdef cop_hints_fetch_pvn
395 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
396 #elif A_HAS_PERL(5, 9, 5)
397 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
399 __PACKAGE__, __PACKAGE_LEN__,
403 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
408 return a_detag(hint);
411 /* ... op => info map ...................................................... */
419 #define PTABLE_NAME ptable_map
420 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
424 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
425 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
426 #define ptable_map_delete(T, K) ptable_map_delete(aPTBLMS_ (T), (K))
428 static ptable *a_op_map = NULL;
432 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
434 static perl_mutex a_op_map_mutex;
436 #define A_LOCK(M) MUTEX_LOCK(M)
437 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
439 static const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
440 const a_op_info *val;
442 A_LOCK(&a_op_map_mutex);
444 val = ptable_fetch(a_op_map, o);
450 A_UNLOCK(&a_op_map_mutex);
455 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
457 #else /* USE_ITHREADS */
459 #define dA_MAP_THX dNOOP
461 #define A_LOCK(M) NOOP
462 #define A_UNLOCK(M) NOOP
464 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
466 #endif /* !USE_ITHREADS */
468 static const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
469 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
472 if (!(oi = ptable_fetch(a_op_map, o))) {
473 oi = PerlMemShared_malloc(sizeof *oi);
474 ptable_map_store(a_op_map, o, oi);
484 static void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
485 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
486 A_LOCK(&a_op_map_mutex);
488 a_map_store_locked(o, old_pp, next, flags);
490 A_UNLOCK(&a_op_map_mutex);
493 static void a_map_delete(pTHX_ const OP *o) {
494 #define a_map_delete(O) a_map_delete(aTHX_ (O))
495 A_LOCK(&a_op_map_mutex);
497 ptable_map_delete(a_op_map, o);
499 A_UNLOCK(&a_op_map_mutex);
502 static const OP *a_map_descend(const OP *o) {
503 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
507 case OA_BASEOP_OR_UNOP:
508 return cUNOPo->op_first;
511 return cLISTOPo->op_last;
517 static void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
518 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
519 const a_op_info *roi;
523 A_LOCK(&a_op_map_mutex);
525 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
527 while (o->op_flags & OPf_KIDS) {
528 o = a_map_descend(o);
531 if ((oi = ptable_fetch(a_op_map, o))) {
532 oi->flags &= ~A_HINT_ROOT;
533 oi->next = (a_op_info *) roi;
538 A_UNLOCK(&a_op_map_mutex);
543 static void a_map_update_flags_topdown(const OP *root, UV flags) {
547 A_LOCK(&a_op_map_mutex);
549 flags &= ~A_HINT_ROOT;
552 if ((oi = ptable_fetch(a_op_map, o)))
553 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
554 if (!(o->op_flags & OPf_KIDS))
556 o = a_map_descend(o);
559 A_UNLOCK(&a_op_map_mutex);
564 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
566 static void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
569 A_LOCK(&a_op_map_mutex);
571 flags &= ~A_HINT_ROOT;
572 rflags |= A_HINT_ROOT;
574 oi = ptable_fetch(a_op_map, o);
575 while (!(oi->flags & A_HINT_ROOT)) {
581 A_UNLOCK(&a_op_map_mutex);
586 /* ... Decide whether this expression should be autovivified or not ........ */
588 static UV a_map_resolve(const OP *o, const a_op_info *oi) {
589 UV flags = 0, rflags;
591 const a_op_info *roi = oi;
593 while (!(roi->flags & A_HINT_ROOT))
598 rflags = roi->flags & ~A_HINT_ROOT;
603 if (root->op_flags & OPf_MOD) {
604 if (rflags & A_HINT_STORE)
605 flags = (A_HINT_STORE|A_HINT_DEREF);
606 } else if (rflags & A_HINT_FETCH)
607 flags = (A_HINT_FETCH|A_HINT_DEREF);
611 a_map_update_flags_bottomup(o, 0, 0);
615 flags |= (rflags & A_HINT_NOTIFY);
616 a_map_update_flags_bottomup(o, flags, 0);
618 return oi->flags & A_HINT_ROOT ? 0 : flags;
621 /* ... Inspired from pp_defined() .......................................... */
623 static int a_undef(pTHX_ SV *sv) {
624 #define a_undef(S) a_undef(aTHX_ (S))
625 switch (SvTYPE(sv)) {
629 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
630 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
634 if (HvARRAY(sv) || SvGMAGICAL(sv)
635 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
647 /* --- PP functions -------------------------------------------------------- */
649 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
650 * value, another extension might have saved our pp replacement as the ppaddr
651 * for this op, so this doesn't ensure that our function will never be called
652 * again. That's why we don't remove the op info from our map, so that it can
653 * still run correctly if required. */
655 /* ... pp_rv2av ............................................................ */
657 static OP *a_pp_rv2av(pTHX) {
662 oi = a_map_fetch(PL_op);
664 if (oi->flags & A_HINT_DEREF) {
666 /* We always need to push an empty array to fool the pp_aelem() that comes
670 av = sv_2mortal((SV *) newAV());
676 return oi->old_pp(aTHX);
679 /* ... pp_rv2hv ............................................................ */
681 static OP *a_pp_rv2hv_simple(pTHX) {
686 oi = a_map_fetch(PL_op);
688 if (oi->flags & A_HINT_DEREF) {
693 return oi->old_pp(aTHX);
696 static OP *a_pp_rv2hv(pTHX) {
701 oi = a_map_fetch(PL_op);
703 if (oi->flags & A_HINT_DEREF) {
707 hv = sv_2mortal((SV *) newHV());
713 return oi->old_pp(aTHX);
716 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
718 static void a_cannot_vivify(pTHX_ UV flags) {
719 #define a_cannot_vivify(F) a_cannot_vivify(aTHX_ (F))
720 if (flags & A_HINT_STRICT)
721 croak("Reference vivification forbidden");
722 else if (flags & A_HINT_WARN)
723 warn("Reference was vivified");
724 else /* A_HINT_STORE */
725 croak("Can't vivify reference");
728 static OP *a_pp_deref(pTHX) {
734 oi = a_map_fetch(PL_op);
737 if (flags & A_HINT_DEREF) {
740 o = oi->old_pp(aTHX);
742 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
745 a_cannot_vivify(flags);
751 return oi->old_pp(aTHX);
754 /* ... pp_root (exists,delete,keys,values) ................................. */
756 static OP *a_pp_root_unop(pTHX) {
761 /* Can only be reached by keys or values */
762 if (GIMME_V == G_SCALAR) {
771 const a_op_info *oi = a_map_fetch(PL_op);
772 return oi->old_pp(aTHX);
776 static OP *a_pp_root_binop(pTHX) {
779 if (a_undef(TOPm1s)) {
782 if (PL_op->op_type == OP_EXISTS)
790 const a_op_info *oi = a_map_fetch(PL_op);
791 return oi->old_pp(aTHX);
795 /* --- Check functions ----------------------------------------------------- */
797 static void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
798 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
800 if (o->op_type == type && o->op_ppaddr != new_pp
801 && cUNOPo->op_first->op_type != OP_GV) {
803 const a_op_info *oi = a_map_fetch(o);
805 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
806 o->op_ppaddr = new_pp;
813 /* ... ck_pad{any,sv} ...................................................... */
815 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
816 * function, but are instead manually mutated from a padany. So we store
817 * the op entry in the op map in the padany check function, and we set their
818 * op_ppaddr member in our peephole optimizer replacement below. */
820 static OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
822 static OP *a_ck_padany(pTHX_ OP *o) {
825 o = a_old_ck_padany(aTHX_ o);
828 if (hint & A_HINT_DO)
829 a_map_store_root(o, o->op_ppaddr, hint);
836 static OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
838 static OP *a_ck_padsv(pTHX_ OP *o) {
841 o = a_old_ck_padsv(aTHX_ o);
844 if (hint & A_HINT_DO) {
845 a_map_store_root(o, o->op_ppaddr, hint);
846 o->op_ppaddr = a_pp_deref;
853 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
855 /* Those ops appear both at the root and inside an expression but there's no
856 * way to distinguish both situations. Worse, we can't even know if we are in a
857 * modifying context, so the expression can't be resolved yet. It will be at the
858 * first invocation of a_pp_deref() for this expression. */
860 static OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
861 static OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
862 static OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
864 static OP *a_ck_deref(pTHX_ OP *o) {
865 OP * (*old_ck)(pTHX_ OP *o) = 0;
868 switch (o->op_type) {
870 old_ck = a_old_ck_aelem;
871 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
872 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
875 old_ck = a_old_ck_helem;
876 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
877 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
880 old_ck = a_old_ck_rv2sv;
885 if (hint & A_HINT_DO) {
886 a_map_store_root(o, o->op_ppaddr, hint);
887 o->op_ppaddr = a_pp_deref;
894 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
896 /* Those ops also appear both inisde and at the root, hence the caveats for
897 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
898 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
901 static OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
902 static OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
904 static OP *a_ck_rv2xv(pTHX_ OP *o) {
905 OP * (*old_ck)(pTHX_ OP *o) = 0;
906 OP * (*new_pp)(pTHX) = 0;
909 switch (o->op_type) {
910 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
911 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
915 if (cUNOPo->op_first->op_type == OP_GV)
919 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
920 a_map_store_root(o, o->op_ppaddr, hint);
921 o->op_ppaddr = new_pp;
928 /* ... ck_xslice (aslice,hslice) ........................................... */
930 /* I think those are only found at the root, but there's nothing that really
931 * prevent them to be inside the expression too. We only need to update the
932 * root so that the rest of the expression will see the right context when
933 * resolving. That's why we don't replace the ppaddr. */
935 static OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
936 static OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
938 static OP *a_ck_xslice(pTHX_ OP *o) {
939 OP * (*old_ck)(pTHX_ OP *o) = 0;
942 switch (o->op_type) {
944 old_ck = a_old_ck_aslice;
947 old_ck = a_old_ck_hslice;
948 if (hint & A_HINT_DO)
949 a_recheck_rv2xv(OpSIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv);
954 if (hint & A_HINT_DO) {
955 a_map_store_root(o, 0, hint);
962 /* ... ck_root (exists,delete,keys,values) ................................. */
964 /* Those ops are only found at the root of a dereferencing expression. We can
965 * then resolve at compile time if vivification must take place or not. */
967 static OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
968 static OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
969 static OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
970 static OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
972 static OP *a_ck_root(pTHX_ OP *o) {
973 OP * (*old_ck)(pTHX_ OP *o) = 0;
974 OP * (*new_pp)(pTHX) = 0;
975 bool enabled = FALSE;
978 switch (o->op_type) {
980 old_ck = a_old_ck_exists;
981 new_pp = a_pp_root_binop;
982 enabled = hint & A_HINT_EXISTS;
985 old_ck = a_old_ck_delete;
986 new_pp = a_pp_root_binop;
987 enabled = hint & A_HINT_DELETE;
990 old_ck = a_old_ck_keys;
991 new_pp = a_pp_root_unop;
992 enabled = hint & A_HINT_FETCH;
995 old_ck = a_old_ck_values;
996 new_pp = a_pp_root_unop;
997 enabled = hint & A_HINT_FETCH;
1000 o = old_ck(aTHX_ o);
1002 if (hint & A_HINT_DO) {
1004 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
1005 a_map_store_root(o, o->op_ppaddr, hint);
1006 o->op_ppaddr = new_pp;
1016 /* ... Our peephole optimizer .............................................. */
1018 static peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
1020 static void a_peep_rec(pTHX_ OP *o, ptable *seen);
1022 static void a_peep_rec(pTHX_ OP *o, ptable *seen) {
1023 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
1024 for (; o; o = o->op_next) {
1026 const a_op_info *oi = NULL;
1030 if (ptable_fetch(seen, o))
1032 ptable_seen_store(seen, o, o);
1035 switch (o->op_type) {
1041 if (ptable_fetch(seen, o))
1043 ptable_seen_store(seen, o, o);
1047 if (o->op_ppaddr != a_pp_deref) {
1048 oi = a_map_fetch(o);
1049 if (oi && (oi->flags & A_HINT_DO)) {
1050 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1051 o->op_ppaddr = a_pp_deref;
1059 if (o->op_ppaddr != a_pp_deref)
1061 oi = a_map_fetch(o);
1065 if (!(flags & A_HINT_DEREF)
1066 && (flags & A_HINT_DO)
1067 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1068 /* Decide if the expression must autovivify or not. */
1069 flags = a_map_resolve(o, oi);
1071 if (flags & A_HINT_DEREF)
1072 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1074 o->op_ppaddr = oi->old_pp;
1078 if ( o->op_ppaddr != a_pp_rv2av
1079 && o->op_ppaddr != a_pp_rv2hv
1080 && o->op_ppaddr != a_pp_rv2hv_simple)
1082 oi = a_map_fetch(o);
1085 if (!(oi->flags & A_HINT_DEREF))
1086 o->op_ppaddr = oi->old_pp;
1097 # if A_HAS_PERL(5, 10, 0)
1102 a_peep_rec(cLOGOPo->op_other);
1106 a_peep_rec(cLOOPo->op_redoop);
1107 a_peep_rec(cLOOPo->op_nextop);
1108 a_peep_rec(cLOOPo->op_lastop);
1110 # if A_HAS_PERL(5, 9, 5)
1112 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1118 a_peep_rec(cPMOPo->op_pmreplstart);
1121 #endif /* !A_HAS_RPEEP */
1128 static void a_peep(pTHX_ OP *o) {
1130 ptable *seen = MY_CXT.seen;
1132 a_old_peep(aTHX_ o);
1135 ptable_seen_clear(seen);
1137 ptable_seen_clear(seen);
1141 /* --- Interpreter setup/teardown ------------------------------------------ */
1143 static U32 a_initialized = 0;
1145 static void a_teardown(pTHX_ void *root) {
1157 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1158 ptable_hints_free(MY_CXT.tbl);
1160 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1161 ptable_seen_free(MY_CXT.seen);
1165 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1166 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1168 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1169 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1170 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1172 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1173 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1175 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1176 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1178 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1179 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1180 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1181 a_ck_restore(OP_VALUES, &a_old_ck_values);
1184 PL_rpeepp = a_old_peep;
1186 PL_peepp = a_old_peep;
1193 static void a_setup(pTHX) {
1194 #define a_setup() a_setup(aTHX)
1200 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1201 MY_CXT.tbl = ptable_new();
1202 MY_CXT.owner = aTHX;
1203 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1204 MY_CXT.seen = ptable_new();
1207 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1208 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1210 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1211 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1212 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1214 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1215 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1217 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1218 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1220 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1221 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1222 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1223 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1226 a_old_peep = PL_rpeepp;
1229 a_old_peep = PL_peepp;
1234 call_atexit(a_teardown, aTHX);
1236 call_atexit(a_teardown, NULL);
1242 static U32 a_booted = 0;
1244 /* --- XS ------------------------------------------------------------------ */
1246 MODULE = autovivification PACKAGE = autovivification
1255 a_op_map = ptable_new();
1257 MUTEX_INIT(&a_op_map_mutex);
1260 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1262 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1263 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1264 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1265 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1266 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1267 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1268 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1269 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1270 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1271 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1283 #if A_WORKAROUND_REQUIRE_PROPAGATION
1290 #if A_WORKAROUND_REQUIRE_PROPAGATION
1293 a_ptable_clone_ud ud;
1296 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1297 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1298 a_ptable_clone_ud_deinit(ud);
1305 #if A_WORKAROUND_REQUIRE_PROPAGATION
1307 MY_CXT.owner = aTHX;
1311 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1317 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1319 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &a_endav_vtbl, NULL, 0);
1324 _THREAD_CLEANUP(...)
1327 a_thread_cleanup(aTHX_ NULL);
1330 #endif /* A_THREADSAFE */
1336 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1346 RETVAL = newSVuv(a_detag(tag));