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 #ifndef A_HAS_MULTIDEREF
33 # define A_HAS_MULTIDEREF A_HAS_PERL(5, 21, 7)
38 # define OpSIBLING(O) OP_SIBLING(O)
40 # define OpSIBLING(O) ((O)->op_sibling)
44 /* ... Thread safety and multiplicity ...................................... */
46 /* Always safe when the workaround isn't needed */
47 #if !A_WORKAROUND_REQUIRE_PROPAGATION
50 /* Otherwise, safe unless Makefile.PL says it's Win32 */
51 #elif !defined(A_FORKSAFE)
55 #ifndef A_MULTIPLICITY
56 # if defined(MULTIPLICITY)
57 # define A_MULTIPLICITY 1
59 # define A_MULTIPLICITY 0
63 # ifndef PERL_IMPLICIT_CONTEXT
64 # error MULTIPLICITY builds must set PERL_IMPLICIT_CONTEXT
69 # define tTHX PerlInterpreter*
72 #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))
73 # define A_THREADSAFE 1
75 # define MY_CXT_CLONE \
77 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
78 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
79 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
82 # define A_THREADSAFE 0
84 # define dMY_CXT dNOOP
86 # define MY_CXT a_globaldata
88 # define START_MY_CXT static my_cxt_t MY_CXT;
90 # define MY_CXT_INIT NOOP
92 # define MY_CXT_CLONE NOOP
95 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
96 # define A_CHECK_LOCK OP_CHECK_MUTEX_LOCK
97 # define A_CHECK_UNLOCK OP_CHECK_MUTEX_UNLOCK
99 # define A_CHECK_LOCK OP_REFCNT_LOCK
100 # define A_CHECK_UNLOCK OP_REFCNT_UNLOCK
103 typedef OP *(*a_ck_t)(pTHX_ OP *);
105 #ifdef wrap_op_checker
107 # define a_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
111 static void a_ck_replace(pTHX_ OPCODE type, a_ck_t new_ck, a_ck_t *old_ck_p) {
112 #define a_ck_replace(T, NC, OCP) a_ck_replace(aTHX_ (T), (NC), (OCP))
115 *old_ck_p = PL_check[type];
116 PL_check[type] = new_ck;
123 static void a_ck_restore(pTHX_ OPCODE type, a_ck_t *old_ck_p) {
124 #define a_ck_restore(T, OCP) a_ck_restore(aTHX_ (T), (OCP))
127 PL_check[type] = *old_ck_p;
133 /* --- Helpers ------------------------------------------------------------- */
135 /* ... Thread-safe hints ................................................... */
137 #if A_WORKAROUND_REQUIRE_PROPAGATION
144 #define A_HINT_FREE(H) PerlMemShared_free(H)
148 #define PTABLE_NAME ptable_hints
149 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
158 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
159 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
161 #endif /* A_THREADSAFE */
163 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
165 #define PTABLE_NAME ptable_seen
166 #define PTABLE_VAL_FREE(V) NOOP
170 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
171 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
172 #define ptable_seen_clear(T) ptable_seen_clear(aPTBLMS_ (T))
173 #define ptable_seen_free(T) ptable_seen_free(aPTBLMS_ (T))
175 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
178 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
179 ptable *tbl; /* It really is a ptable_hints */
181 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
182 ptable *seen; /* It really is a ptable_seen */
189 #if A_WORKAROUND_REQUIRE_PROPAGATION
193 #if A_HAS_PERL(5, 13, 2)
194 CLONE_PARAMS *params;
200 #if A_HAS_PERL(5, 13, 2)
201 # define a_ptable_clone_ud_init(U, T, O) \
203 (U).params = Perl_clone_params_new((O), aTHX)
204 # define a_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
205 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
207 # define a_ptable_clone_ud_init(U, T, O) \
209 (U).params.stashes = newAV(); \
210 (U).params.flags = 0; \
211 (U).params.proto_perl = (O)
212 # define a_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
213 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
216 static void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
217 a_ptable_clone_ud *ud = ud_;
218 a_hint_t *h1 = ent->val;
221 h2 = PerlMemShared_malloc(sizeof *h2);
223 h2->require_tag = PTR2IV(a_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
225 ptable_hints_store(ud->tbl, ent->key, h2);
228 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
230 static void a_thread_cleanup(pTHX_ void *ud) {
233 #if A_WORKAROUND_REQUIRE_PROPAGATION
234 ptable_hints_free(MY_CXT.tbl);
236 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
237 ptable_seen_free(MY_CXT.seen);
241 static int a_endav_free(pTHX_ SV *sv, MAGIC *mg) {
242 SAVEDESTRUCTOR_X(a_thread_cleanup, NULL);
247 static MGVTBL a_endav_vtbl = {
264 #endif /* A_THREADSAFE */
266 #if A_WORKAROUND_REQUIRE_PROPAGATION
268 static IV a_require_tag(pTHX) {
269 #define a_require_tag() a_require_tag(aTHX)
270 const CV *cv, *outside;
275 /* If for some reason the pragma is operational at run-time, try to discover
276 * the current cv in use. */
279 for (si = PL_curstackinfo; si; si = si->si_prev) {
282 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
283 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
285 switch (CxTYPE(cx)) {
288 /* The propagation workaround is only needed up to 5.10.0 and at that
289 * time format and sub contexts were still identical. And even later the
290 * cv members offsets should have been kept the same. */
292 goto get_enclosing_cv;
294 cv = cx->blk_eval.cv;
295 goto get_enclosing_cv;
306 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
312 static SV *a_tag(pTHX_ UV bits) {
313 #define a_tag(B) a_tag(aTHX_ (B))
320 #endif /* A_THREADSAFE */
322 h = PerlMemShared_malloc(sizeof *h);
324 h->require_tag = a_require_tag();
327 /* We only need for the key to be an unique tag for looking up the value later
328 * Allocated memory provides convenient unique identifiers, so that's why we
329 * use the hint as the key itself. */
330 ptable_hints_store(MY_CXT.tbl, h, h);
331 #endif /* A_THREADSAFE */
333 return newSViv(PTR2IV(h));
336 static UV a_detag(pTHX_ const SV *hint) {
337 #define a_detag(H) a_detag(aTHX_ (H))
344 #endif /* A_THREADSAFE */
346 if (!(hint && SvIOK(hint)))
349 h = INT2PTR(a_hint_t *, SvIVX(hint));
351 h = ptable_fetch(MY_CXT.tbl, h);
352 #endif /* A_THREADSAFE */
354 if (a_require_tag() != h->require_tag)
360 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
362 #define a_tag(B) newSVuv(B)
363 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
370 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
376 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
378 /* Used both for hints and op flags */
379 #define A_HINT_STRICT 1
380 #define A_HINT_WARN 2
381 #define A_HINT_FETCH 4
382 #define A_HINT_STORE 8
383 #define A_HINT_EXISTS 16
384 #define A_HINT_DELETE 32
385 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
386 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
387 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
389 /* Only used in op flags */
390 #define A_HINT_ROOT 64
391 #define A_HINT_DEREF 128
393 static U32 a_hash = 0;
395 static UV a_hint(pTHX) {
396 #define a_hint() a_hint(aTHX)
398 #ifdef cop_hints_fetch_pvn
399 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
400 #elif A_HAS_PERL(5, 9, 5)
401 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
403 __PACKAGE__, __PACKAGE_LEN__,
407 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
412 return a_detag(hint);
415 /* ... op => info map ...................................................... */
423 #define PTABLE_NAME ptable_map
424 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
428 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
429 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
430 #define ptable_map_delete(T, K) ptable_map_delete(aPTBLMS_ (T), (K))
432 static ptable *a_op_map = NULL;
436 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
438 static perl_mutex a_op_map_mutex;
440 #define A_LOCK(M) MUTEX_LOCK(M)
441 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
443 static const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
444 const a_op_info *val;
446 A_LOCK(&a_op_map_mutex);
448 val = ptable_fetch(a_op_map, o);
454 A_UNLOCK(&a_op_map_mutex);
459 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
461 #else /* USE_ITHREADS */
463 #define dA_MAP_THX dNOOP
465 #define A_LOCK(M) NOOP
466 #define A_UNLOCK(M) NOOP
468 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
470 #endif /* !USE_ITHREADS */
472 static const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
473 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
476 if (!(oi = ptable_fetch(a_op_map, o))) {
477 oi = PerlMemShared_malloc(sizeof *oi);
478 ptable_map_store(a_op_map, o, oi);
488 static void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
489 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
490 A_LOCK(&a_op_map_mutex);
492 a_map_store_locked(o, old_pp, next, flags);
494 A_UNLOCK(&a_op_map_mutex);
497 static void a_map_delete(pTHX_ const OP *o) {
498 #define a_map_delete(O) a_map_delete(aTHX_ (O))
499 A_LOCK(&a_op_map_mutex);
501 ptable_map_delete(a_op_map, o);
503 A_UNLOCK(&a_op_map_mutex);
506 static const OP *a_map_descend(const OP *o) {
507 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
511 case OA_BASEOP_OR_UNOP:
512 return cUNOPo->op_first;
515 return cLISTOPo->op_last;
521 static void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
522 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
523 const a_op_info *roi;
527 A_LOCK(&a_op_map_mutex);
529 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
531 while (o->op_flags & OPf_KIDS) {
532 o = a_map_descend(o);
535 if ((oi = ptable_fetch(a_op_map, o))) {
536 oi->flags &= ~A_HINT_ROOT;
537 oi->next = (a_op_info *) roi;
542 A_UNLOCK(&a_op_map_mutex);
547 static void a_map_update_flags_topdown(const OP *root, UV flags) {
551 A_LOCK(&a_op_map_mutex);
553 flags &= ~A_HINT_ROOT;
556 if ((oi = ptable_fetch(a_op_map, o)))
557 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
558 if (!(o->op_flags & OPf_KIDS))
560 o = a_map_descend(o);
563 A_UNLOCK(&a_op_map_mutex);
568 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
570 static void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
573 A_LOCK(&a_op_map_mutex);
575 flags &= ~A_HINT_ROOT;
576 rflags |= A_HINT_ROOT;
578 oi = ptable_fetch(a_op_map, o);
579 while (!(oi->flags & A_HINT_ROOT)) {
585 A_UNLOCK(&a_op_map_mutex);
590 /* ... Decide whether this expression should be autovivified or not ........ */
592 static UV a_map_resolve(const OP *o, const a_op_info *oi) {
593 UV flags = 0, rflags;
595 const a_op_info *roi = oi;
597 while (!(roi->flags & A_HINT_ROOT))
602 rflags = roi->flags & ~A_HINT_ROOT;
607 if (root->op_flags & OPf_MOD) {
608 if (rflags & A_HINT_STORE)
609 flags = (A_HINT_STORE|A_HINT_DEREF);
610 } else if (rflags & A_HINT_FETCH)
611 flags = (A_HINT_FETCH|A_HINT_DEREF);
615 a_map_update_flags_bottomup(o, 0, 0);
619 flags |= (rflags & A_HINT_NOTIFY);
620 a_map_update_flags_bottomup(o, flags, 0);
622 return oi->flags & A_HINT_ROOT ? 0 : flags;
625 /* ... Inspired from pp_defined() .......................................... */
627 static int a_undef(pTHX_ SV *sv) {
628 #define a_undef(S) a_undef(aTHX_ (S))
629 switch (SvTYPE(sv)) {
633 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
634 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
638 if (HvARRAY(sv) || SvGMAGICAL(sv)
639 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
651 /* --- PP functions -------------------------------------------------------- */
653 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
654 * value, another extension might have saved our pp replacement as the ppaddr
655 * for this op, so this doesn't ensure that our function will never be called
656 * again. That's why we don't remove the op info from our map, so that it can
657 * still run correctly if required. */
659 /* ... pp_rv2av ............................................................ */
661 static OP *a_pp_rv2av(pTHX) {
666 oi = a_map_fetch(PL_op);
668 if (oi->flags & A_HINT_DEREF) {
670 /* We always need to push an empty array to fool the pp_aelem() that comes
674 av = sv_2mortal((SV *) newAV());
680 return oi->old_pp(aTHX);
683 /* ... pp_rv2hv ............................................................ */
685 static OP *a_pp_rv2hv_simple(pTHX) {
690 oi = a_map_fetch(PL_op);
692 if (oi->flags & A_HINT_DEREF) {
697 return oi->old_pp(aTHX);
700 static OP *a_pp_rv2hv(pTHX) {
705 oi = a_map_fetch(PL_op);
707 if (oi->flags & A_HINT_DEREF) {
711 hv = sv_2mortal((SV *) newHV());
717 return oi->old_pp(aTHX);
720 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
722 static void a_cannot_vivify(pTHX_ UV flags) {
723 #define a_cannot_vivify(F) a_cannot_vivify(aTHX_ (F))
724 if (flags & A_HINT_STRICT)
725 croak("Reference vivification forbidden");
726 else if (flags & A_HINT_WARN)
727 warn("Reference was vivified");
728 else /* A_HINT_STORE */
729 croak("Can't vivify reference");
732 static OP *a_pp_deref(pTHX) {
738 oi = a_map_fetch(PL_op);
741 if (flags & A_HINT_DEREF) {
744 o = oi->old_pp(aTHX);
746 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
749 a_cannot_vivify(flags);
755 return oi->old_pp(aTHX);
758 /* ... pp_root (exists,delete,keys,values) ................................. */
760 static OP *a_pp_root_unop(pTHX) {
765 /* Can only be reached by keys or values */
766 if (GIMME_V == G_SCALAR) {
775 const a_op_info *oi = a_map_fetch(PL_op);
776 return oi->old_pp(aTHX);
780 static OP *a_pp_root_binop(pTHX) {
783 if (a_undef(TOPm1s)) {
786 if (PL_op->op_type == OP_EXISTS)
794 const a_op_info *oi = a_map_fetch(PL_op);
795 return oi->old_pp(aTHX);
801 /* ... pp_multideref ....................................................... */
803 /* This pp replacement is actually only called for topmost exists/delete ops,
804 * because we hijack the [ah]elem check functions and this disables the
805 * optimization for lvalue and rvalue dereferencing. In particular, the
806 * OPf_MOD branches should never be covered. In the future, the multideref
807 * optimization might also be disabled for custom exists/delete check functions,
808 * which will make this section unnecessary. However, the code tries to be as
809 * general as possible in case I think of a way to reenable the multideref
810 * optimization even when this module is in use. */
812 static UV a_do_multideref(const OP *o, UV flags) {
813 UV isexdel, other_flags;
815 assert(o->op_type == OP_MULTIDEREF);
817 other_flags = flags & ~A_HINT_DO;
819 isexdel = o->op_private & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE);
821 if (isexdel & OPpMULTIDEREF_EXISTS) {
822 flags &= A_HINT_EXISTS;
824 flags &= A_HINT_DELETE;
827 if (o->op_flags & OPf_MOD) {
828 flags &= A_HINT_STORE;
830 flags &= A_HINT_FETCH;
834 return flags ? (flags | other_flags) : 0;
837 static SV *a_do_fake_pp(pTHX_ OP *op) {
838 #define a_do_fake_pp(O) a_do_fake_pp(aTHX_ (O))
844 PL_op->op_ppaddr(aTHX);
858 static void a_do_fake_pp_unop_init(pTHX_ UNOP *unop, U32 type, U32 flags) {
859 #define a_do_fake_pp_unop_init(O, T, F) a_do_fake_pp_unop_init(aTHX_ (O), (T), (F))
860 unop->op_type = type;
861 unop->op_flags = OPf_WANT_SCALAR | (~OPf_WANT & flags);
862 unop->op_private = 0;
863 unop->op_first = NULL;
864 unop->op_ppaddr = PL_ppaddr[type];
867 static SV *a_do_fake_pp_unop_arg1(pTHX_ U32 type, U32 flags, SV *arg) {
868 #define a_do_fake_pp_unop_arg1(T, F, A) a_do_fake_pp_unop_arg1(aTHX_ (T), (F), (A))
872 a_do_fake_pp_unop_init(&unop, type, flags);
878 return a_do_fake_pp((OP *) &unop);
881 static SV *a_do_fake_pp_unop_arg2(pTHX_ U32 type, U32 flags, SV *arg1, SV *arg2) {
882 #define a_do_fake_pp_unop_arg2(T, F, A1, A2) a_do_fake_pp_unop_arg2(aTHX_ (T), (F), (A1), (A2))
886 a_do_fake_pp_unop_init(&unop, type, flags);
893 return a_do_fake_pp((OP *) &unop);
896 #define a_do_pp_rv2av(R) a_do_fake_pp_unop_arg1(OP_RV2AV, OPf_REF, (R))
897 #define a_do_pp_afetch(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, 0, (A), (I))
898 #define a_do_pp_afetch_lv(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, OPf_MOD, (A), (I))
899 #define a_do_pp_aexists(A, I) a_do_fake_pp_unop_arg2(OP_EXISTS, OPf_SPECIAL, (A), (I))
900 #define a_do_pp_adelete(A, I) a_do_fake_pp_unop_arg2(OP_DELETE, OPf_SPECIAL, (A), (I))
902 #define a_do_pp_rv2hv(R) a_do_fake_pp_unop_arg1(OP_RV2HV, OPf_REF, (R))
903 #define a_do_pp_hfetch(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, 0, (H), (K))
904 #define a_do_pp_hfetch_lv(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, OPf_MOD, (H), (K))
905 #define a_do_pp_hexists(H, K) a_do_fake_pp_unop_arg2(OP_EXISTS, 0, (H), (K))
906 #define a_do_pp_hdelete(H, K) a_do_fake_pp_unop_arg2(OP_DELETE, 0, (H), (K))
908 static OP *a_pp_multideref(pTHX) {
909 UNOP_AUX_item *items;
917 const a_op_info *oi = a_map_fetch(PL_op);
919 flags = a_do_multideref(PL_op, oi->flags);
921 return oi->old_pp(aTHX);
924 items = cUNOP_AUXx(PL_op)->op_aux;
927 PL_multideref_pc = items;
930 switch (actions & MDEREF_ACTION_MASK) {
932 actions = (++items)->uv;
934 case MDEREF_AV_padav_aelem: /* $lex[...] */
935 sv = PAD_SVl((++items)->pad_offset);
939 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
940 sv = UNOP_AUX_item_sv(++items);
941 assert(isGV_with_GP(sv));
942 sv = (SV *) GvAVn((GV *) sv);
946 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
950 goto do_AV_rv2av_aelem;
951 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
952 sv = UNOP_AUX_item_sv(++items);
953 assert(isGV_with_GP(sv));
954 sv = GvSVn((GV *) sv);
957 goto do_AV_vivify_rv2av_aelem;
958 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
959 sv = PAD_SVl((++items)->pad_offset);
961 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
964 do_AV_vivify_rv2av_aelem:
965 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_AV);
967 sv = a_do_pp_rv2av(sv);
971 assert(SvTYPE(sv) == SVt_PVAV);
972 switch (actions & MDEREF_INDEX_MASK) {
973 case MDEREF_INDEX_none:
975 case MDEREF_INDEX_const:
976 esv = sv_2mortal(newSViv((++items)->iv));
978 case MDEREF_INDEX_padsv:
979 esv = PAD_SVl((++items)->pad_offset);
981 case MDEREF_INDEX_gvsv:
982 esv = UNOP_AUX_item_sv(++items);
983 assert(isGV_with_GP(esv));
984 esv = GvSVn((GV *) esv);
986 if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC)))
987 Perl_warner(aTHX_ packWARN(WARN_MISC),
988 "Use of reference \"%"SVf"\" as array index",
992 PL_multideref_pc = items;
993 if (actions & MDEREF_FLAG_last) {
994 switch (flags & A_HINT_DO) {
996 sv = a_do_pp_afetch(sv, esv);
999 sv = a_do_pp_afetch_lv(sv, esv);
1002 sv = a_do_pp_aexists(sv, esv);
1005 sv = a_do_pp_adelete(sv, esv);
1010 sv = a_do_pp_afetch(sv, esv);
1013 case MDEREF_HV_padhv_helem: /* $lex{...} */
1014 sv = PAD_SVl((++items)->pad_offset);
1018 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
1019 sv = UNOP_AUX_item_sv(++items);
1020 assert(isGV_with_GP(sv));
1021 sv = (SV *) GvHVn((GV *) sv);
1025 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
1029 goto do_HV_rv2hv_helem;
1030 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
1031 sv = UNOP_AUX_item_sv(++items);
1032 assert(isGV_with_GP(sv));
1033 sv = GvSVn((GV *) sv);
1036 goto do_HV_vivify_rv2hv_helem;
1037 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
1038 sv = PAD_SVl((++items)->pad_offset);
1040 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
1043 do_HV_vivify_rv2hv_helem:
1044 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_HV);
1046 sv = a_do_pp_rv2hv(sv);
1050 assert(SvTYPE(sv) == SVt_PVHV);
1051 switch (actions & MDEREF_INDEX_MASK) {
1052 case MDEREF_INDEX_none:
1054 case MDEREF_INDEX_const:
1055 key = UNOP_AUX_item_sv(++items);
1057 case MDEREF_INDEX_padsv:
1058 key = PAD_SVl((++items)->pad_offset);
1060 case MDEREF_INDEX_gvsv:
1061 key = UNOP_AUX_item_sv(++items);
1062 assert(isGV_with_GP(key));
1063 key = GvSVn((GV *) key);
1066 PL_multideref_pc = items;
1067 if (actions & MDEREF_FLAG_last) {
1068 switch (flags & A_HINT_DO) {
1070 sv = a_do_pp_hfetch(sv, key);
1073 sv = a_do_pp_hfetch_lv(sv, key);
1076 sv = a_do_pp_hexists(sv, key);
1079 sv = a_do_pp_hdelete(sv, key);
1086 sv = a_do_pp_hfetch(sv, key);
1091 actions >>= MDEREF_SHIFT;
1095 if (flags & (A_HINT_NOTIFY|A_HINT_STORE))
1096 a_cannot_vivify(flags);
1097 if (flags & A_HINT_EXISTS)
1106 #endif /* A_HAS_MULTIDEREF */
1108 /* --- Check functions ----------------------------------------------------- */
1110 static void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
1111 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
1113 if (o->op_type == type && o->op_ppaddr != new_pp
1114 && cUNOPo->op_first->op_type != OP_GV) {
1116 const a_op_info *oi = a_map_fetch(o);
1118 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1119 o->op_ppaddr = new_pp;
1126 /* ... ck_pad{any,sv} ...................................................... */
1128 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
1129 * function, but are instead manually mutated from a padany. So we store
1130 * the op entry in the op map in the padany check function, and we set their
1131 * op_ppaddr member in our peephole optimizer replacement below. */
1133 static OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
1135 static OP *a_ck_padany(pTHX_ OP *o) {
1138 o = a_old_ck_padany(aTHX_ o);
1141 if (hint & A_HINT_DO)
1142 a_map_store_root(o, o->op_ppaddr, hint);
1149 static OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
1151 static OP *a_ck_padsv(pTHX_ OP *o) {
1154 o = a_old_ck_padsv(aTHX_ o);
1157 if (hint & A_HINT_DO) {
1158 a_map_store_root(o, o->op_ppaddr, hint);
1159 o->op_ppaddr = a_pp_deref;
1166 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
1168 /* Those ops appear both at the root and inside an expression but there's no
1169 * way to distinguish both situations. Worse, we can't even know if we are in a
1170 * modifying context, so the expression can't be resolved yet. It will be at the
1171 * first invocation of a_pp_deref() for this expression. */
1173 static OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
1174 static OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
1175 static OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
1177 static OP *a_ck_deref(pTHX_ OP *o) {
1178 OP * (*old_ck)(pTHX_ OP *o) = 0;
1181 switch (o->op_type) {
1183 old_ck = a_old_ck_aelem;
1184 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1185 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
1188 old_ck = a_old_ck_helem;
1189 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1190 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
1193 old_ck = a_old_ck_rv2sv;
1196 o = old_ck(aTHX_ o);
1198 if (hint & A_HINT_DO) {
1199 #if A_HAS_MULTIDEREF
1200 if (old_ck == a_old_ck_rv2sv && o->op_flags & OPf_KIDS) {
1201 OP *kid = cUNOPo->op_first;
1202 if (kid && kid->op_type == OP_GV)
1203 a_map_store(kid, kid->op_ppaddr, NULL, hint);
1206 a_map_store_root(o, o->op_ppaddr, hint);
1207 o->op_ppaddr = a_pp_deref;
1214 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
1216 /* Those ops also appear both inisde and at the root, hence the caveats for
1217 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
1218 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
1221 static OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
1222 static OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
1224 static OP *a_ck_rv2xv(pTHX_ OP *o) {
1225 OP * (*old_ck)(pTHX_ OP *o) = 0;
1226 OP * (*new_pp)(pTHX) = 0;
1229 switch (o->op_type) {
1230 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
1231 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
1233 o = old_ck(aTHX_ o);
1235 if (cUNOPo->op_first->op_type == OP_GV)
1239 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
1240 a_map_store_root(o, o->op_ppaddr, hint);
1241 o->op_ppaddr = new_pp;
1248 /* ... ck_xslice (aslice,hslice) ........................................... */
1250 /* I think those are only found at the root, but there's nothing that really
1251 * prevent them to be inside the expression too. We only need to update the
1252 * root so that the rest of the expression will see the right context when
1253 * resolving. That's why we don't replace the ppaddr. */
1255 static OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
1256 static OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
1258 static OP *a_ck_xslice(pTHX_ OP *o) {
1259 OP * (*old_ck)(pTHX_ OP *o) = 0;
1262 switch (o->op_type) {
1264 old_ck = a_old_ck_aslice;
1267 old_ck = a_old_ck_hslice;
1268 if (hint & A_HINT_DO)
1269 a_recheck_rv2xv(OpSIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv);
1272 o = old_ck(aTHX_ o);
1274 if (hint & A_HINT_DO) {
1275 a_map_store_root(o, 0, hint);
1282 /* ... ck_root (exists,delete,keys,values) ................................. */
1284 /* Those ops are only found at the root of a dereferencing expression. We can
1285 * then resolve at compile time if vivification must take place or not. */
1287 static OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
1288 static OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
1289 static OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
1290 static OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
1292 static OP *a_ck_root(pTHX_ OP *o) {
1293 OP * (*old_ck)(pTHX_ OP *o) = 0;
1294 OP * (*new_pp)(pTHX) = 0;
1295 bool enabled = FALSE;
1298 switch (o->op_type) {
1300 old_ck = a_old_ck_exists;
1301 new_pp = a_pp_root_binop;
1302 enabled = hint & A_HINT_EXISTS;
1305 old_ck = a_old_ck_delete;
1306 new_pp = a_pp_root_binop;
1307 enabled = hint & A_HINT_DELETE;
1310 old_ck = a_old_ck_keys;
1311 new_pp = a_pp_root_unop;
1312 enabled = hint & A_HINT_FETCH;
1315 old_ck = a_old_ck_values;
1316 new_pp = a_pp_root_unop;
1317 enabled = hint & A_HINT_FETCH;
1320 o = old_ck(aTHX_ o);
1322 if (hint & A_HINT_DO) {
1324 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
1325 a_map_store_root(o, o->op_ppaddr, hint);
1326 o->op_ppaddr = new_pp;
1336 /* ... Our peephole optimizer .............................................. */
1338 static peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
1340 static void a_peep_rec(pTHX_ OP *o, ptable *seen);
1342 static void a_peep_rec(pTHX_ OP *o, ptable *seen) {
1343 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
1344 for (; o; o = o->op_next) {
1346 const a_op_info *oi = NULL;
1350 if (ptable_fetch(seen, o))
1352 ptable_seen_store(seen, o, o);
1355 switch (o->op_type) {
1361 if (ptable_fetch(seen, o))
1363 ptable_seen_store(seen, o, o);
1367 if (o->op_ppaddr != a_pp_deref) {
1368 oi = a_map_fetch(o);
1369 if (oi && (oi->flags & A_HINT_DO)) {
1370 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1371 o->op_ppaddr = a_pp_deref;
1379 if (o->op_ppaddr != a_pp_deref)
1381 oi = a_map_fetch(o);
1385 if (!(flags & A_HINT_DEREF)
1386 && (flags & A_HINT_DO)
1387 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1388 /* Decide if the expression must autovivify or not. */
1389 flags = a_map_resolve(o, oi);
1391 if (flags & A_HINT_DEREF)
1392 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1394 o->op_ppaddr = oi->old_pp;
1398 if ( o->op_ppaddr != a_pp_rv2av
1399 && o->op_ppaddr != a_pp_rv2hv
1400 && o->op_ppaddr != a_pp_rv2hv_simple)
1402 oi = a_map_fetch(o);
1405 if (!(oi->flags & A_HINT_DEREF))
1406 o->op_ppaddr = oi->old_pp;
1408 #if A_HAS_MULTIDEREF
1410 if (o->op_ppaddr != a_pp_multideref) {
1411 oi = a_map_fetch(cUNOPo->op_first);
1415 if (a_do_multideref(o, flags)) {
1416 a_map_store_root(o, o->op_ppaddr, flags & ~A_HINT_DEREF);
1417 o->op_ppaddr = a_pp_multideref;
1431 # if A_HAS_PERL(5, 10, 0)
1436 a_peep_rec(cLOGOPo->op_other);
1440 a_peep_rec(cLOOPo->op_redoop);
1441 a_peep_rec(cLOOPo->op_nextop);
1442 a_peep_rec(cLOOPo->op_lastop);
1444 # if A_HAS_PERL(5, 9, 5)
1446 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1452 a_peep_rec(cPMOPo->op_pmreplstart);
1455 #endif /* !A_HAS_RPEEP */
1462 static void a_peep(pTHX_ OP *o) {
1464 ptable *seen = MY_CXT.seen;
1466 a_old_peep(aTHX_ o);
1469 ptable_seen_clear(seen);
1471 ptable_seen_clear(seen);
1475 /* --- Interpreter setup/teardown ------------------------------------------ */
1477 static U32 a_initialized = 0;
1479 static void a_teardown(pTHX_ void *root) {
1491 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1492 ptable_hints_free(MY_CXT.tbl);
1494 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1495 ptable_seen_free(MY_CXT.seen);
1499 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1500 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1502 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1503 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1504 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1506 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1507 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1509 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1510 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1512 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1513 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1514 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1515 a_ck_restore(OP_VALUES, &a_old_ck_values);
1518 PL_rpeepp = a_old_peep;
1520 PL_peepp = a_old_peep;
1527 static void a_setup(pTHX) {
1528 #define a_setup() a_setup(aTHX)
1534 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1535 MY_CXT.tbl = ptable_new();
1536 MY_CXT.owner = aTHX;
1537 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1538 MY_CXT.seen = ptable_new();
1541 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1542 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1544 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1545 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1546 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1548 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1549 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1551 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1552 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1554 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1555 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1556 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1557 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1560 a_old_peep = PL_rpeepp;
1563 a_old_peep = PL_peepp;
1568 call_atexit(a_teardown, aTHX);
1570 call_atexit(a_teardown, NULL);
1576 static U32 a_booted = 0;
1578 /* --- XS ------------------------------------------------------------------ */
1580 MODULE = autovivification PACKAGE = autovivification
1589 a_op_map = ptable_new();
1591 MUTEX_INIT(&a_op_map_mutex);
1594 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1596 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1597 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1598 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1599 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1600 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1601 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1602 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1603 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1604 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1605 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1617 #if A_WORKAROUND_REQUIRE_PROPAGATION
1624 #if A_WORKAROUND_REQUIRE_PROPAGATION
1627 a_ptable_clone_ud ud;
1630 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1631 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1632 a_ptable_clone_ud_deinit(ud);
1639 #if A_WORKAROUND_REQUIRE_PROPAGATION
1641 MY_CXT.owner = aTHX;
1645 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1651 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1653 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &a_endav_vtbl, NULL, 0);
1658 _THREAD_CLEANUP(...)
1661 a_thread_cleanup(aTHX_ NULL);
1664 #endif /* A_THREADSAFE */
1670 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1680 RETVAL = newSVuv(a_detag(tag));