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
96 /* We must use preexistent global mutexes or we will never be able to destroy
98 # if A_HAS_PERL(5, 9, 3)
99 # define A_LOADED_LOCK MUTEX_LOCK(&PL_my_ctx_mutex)
100 # define A_LOADED_UNLOCK MUTEX_UNLOCK(&PL_my_ctx_mutex)
102 # define A_LOADED_LOCK OP_REFCNT_LOCK
103 # define A_LOADED_UNLOCK OP_REFCNT_UNLOCK
106 # define A_LOADED_LOCK NOOP
107 # define A_LOADED_UNLOCK NOOP
110 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
111 # define A_CHECK_LOCK OP_CHECK_MUTEX_LOCK
112 # define A_CHECK_UNLOCK OP_CHECK_MUTEX_UNLOCK
113 #elif A_HAS_PERL(5, 9, 3)
114 # define A_CHECK_LOCK OP_REFCNT_LOCK
115 # define A_CHECK_UNLOCK OP_REFCNT_UNLOCK
117 /* Before perl 5.9.3, indirect_ck_*() calls are already protected by the
118 * A_LOADED mutex, which falls back to the OP_REFCNT mutex. Make sure we don't
120 # define A_CHECK_LOCK NOOP
121 # define A_CHECK_UNLOCK NOOP
124 typedef OP *(*a_ck_t)(pTHX_ OP *);
126 #ifdef wrap_op_checker
128 # define a_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
132 static void a_ck_replace(pTHX_ OPCODE type, a_ck_t new_ck, a_ck_t *old_ck_p) {
133 #define a_ck_replace(T, NC, OCP) a_ck_replace(aTHX_ (T), (NC), (OCP))
136 *old_ck_p = PL_check[type];
137 PL_check[type] = new_ck;
144 static void a_ck_restore(pTHX_ OPCODE type, a_ck_t *old_ck_p) {
145 #define a_ck_restore(T, OCP) a_ck_restore(aTHX_ (T), (OCP))
148 PL_check[type] = *old_ck_p;
154 /* --- Helpers ------------------------------------------------------------- */
156 /* ... Check if the module is loaded ....................................... */
158 static I32 a_loaded = 0;
162 #define PTABLE_NAME ptable_loaded
163 #define PTABLE_VAL_FREE(V) NOOP
167 #define ptable_loaded_store(T, K, V) ptable_loaded_store(aPTBLMS_ (T), (K), (V))
168 #define ptable_loaded_delete(T, K) ptable_loaded_delete(aPTBLMS_ (T), (K))
169 #define ptable_loaded_free(T) ptable_loaded_free(aPTBLMS_ (T))
171 static ptable *a_loaded_cxts = NULL;
173 static int a_is_loaded(pTHX_ void *cxt) {
174 #define a_is_loaded(C) a_is_loaded(aTHX_ (C))
178 if (a_loaded_cxts && ptable_fetch(a_loaded_cxts, cxt))
185 static int a_set_loaded_locked(pTHX_ void *cxt) {
186 #define a_set_loaded_locked(C) a_set_loaded_locked(aTHX_ (C))
187 int global_setup = 0;
190 assert(a_loaded == 0);
191 assert(!a_loaded_cxts);
192 a_loaded_cxts = ptable_new();
196 assert(a_loaded_cxts);
197 ptable_loaded_store(a_loaded_cxts, cxt, cxt);
202 static int a_clear_loaded_locked(pTHX_ void *cxt) {
203 #define a_clear_loaded_locked(C) a_clear_loaded_locked(aTHX_ (C))
204 int global_teardown = 0;
207 assert(a_loaded_cxts);
208 ptable_loaded_delete(a_loaded_cxts, cxt);
210 } else if (a_loaded_cxts) {
211 assert(a_loaded == 1);
212 ptable_loaded_free(a_loaded_cxts);
213 a_loaded_cxts = NULL;
218 return global_teardown;
223 #define a_is_loaded(C) (a_loaded > 0)
224 #define a_set_loaded_locked(C) ((a_loaded++ <= 0) ? 1 : 0)
225 #define a_clear_loaded_locked(C) ((--a_loaded <= 0) ? 1 : 0)
229 /* ... Thread-safe hints ................................................... */
231 #if A_WORKAROUND_REQUIRE_PROPAGATION
238 #define A_HINT_FREE(H) PerlMemShared_free(H)
242 #define PTABLE_NAME ptable_hints
243 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
252 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
253 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
255 #endif /* A_THREADSAFE */
257 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
259 #define PTABLE_NAME ptable_seen
260 #define PTABLE_VAL_FREE(V) NOOP
264 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
265 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
266 #define ptable_seen_clear(T) ptable_seen_clear(aPTBLMS_ (T))
267 #define ptable_seen_free(T) ptable_seen_free(aPTBLMS_ (T))
269 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
272 peep_t old_peep; /* This is actually the rpeep past 5.13.5 */
273 ptable *seen; /* It really is a ptable_seen */
274 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
275 ptable *tbl; /* It really is a ptable_hints */
277 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
282 #if A_WORKAROUND_REQUIRE_PROPAGATION
288 #if A_HAS_PERL(5, 13, 2)
289 CLONE_PARAMS *params;
295 #if A_HAS_PERL(5, 13, 2)
296 # define a_ptable_clone_ud_init(U, T, O) \
298 (U).params = Perl_clone_params_new((O), aTHX)
299 # define a_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
300 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
302 # define a_ptable_clone_ud_init(U, T, O) \
304 (U).params.stashes = newAV(); \
305 (U).params.flags = 0; \
306 (U).params.proto_perl = (O)
307 # define a_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
308 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
311 static void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
312 a_ptable_clone_ud *ud = ud_;
313 a_hint_t *h1 = ent->val;
316 h2 = PerlMemShared_malloc(sizeof *h2);
318 h2->require_tag = PTR2IV(a_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
320 ptable_hints_store(ud->tbl, ent->key, h2);
323 #endif /* A_THREADSAFE */
325 static IV a_require_tag(pTHX) {
326 #define a_require_tag() a_require_tag(aTHX)
327 const CV *cv, *outside;
332 /* If for some reason the pragma is operational at run-time, try to discover
333 * the current cv in use. */
336 for (si = PL_curstackinfo; si; si = si->si_prev) {
339 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
340 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
342 switch (CxTYPE(cx)) {
345 /* The propagation workaround is only needed up to 5.10.0 and at that
346 * time format and sub contexts were still identical. And even later the
347 * cv members offsets should have been kept the same. */
349 goto get_enclosing_cv;
351 cv = cx->blk_eval.cv;
352 goto get_enclosing_cv;
363 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
369 static SV *a_tag(pTHX_ UV bits) {
370 #define a_tag(B) a_tag(aTHX_ (B))
377 #endif /* A_THREADSAFE */
379 h = PerlMemShared_malloc(sizeof *h);
381 h->require_tag = a_require_tag();
384 /* We only need for the key to be an unique tag for looking up the value later
385 * Allocated memory provides convenient unique identifiers, so that's why we
386 * use the hint as the key itself. */
387 ptable_hints_store(MY_CXT.tbl, h, h);
388 #endif /* A_THREADSAFE */
390 return newSViv(PTR2IV(h));
393 static UV a_detag(pTHX_ const SV *hint) {
394 #define a_detag(H) a_detag(aTHX_ (H))
401 #endif /* A_THREADSAFE */
403 if (!(hint && SvIOK(hint)))
406 h = INT2PTR(a_hint_t *, SvIVX(hint));
408 h = ptable_fetch(MY_CXT.tbl, h);
409 #endif /* A_THREADSAFE */
411 if (a_require_tag() != h->require_tag)
417 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
419 #define a_tag(B) newSVuv(B)
420 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
427 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
433 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
435 /* Used both for hints and op flags */
436 #define A_HINT_STRICT 1
437 #define A_HINT_WARN 2
438 #define A_HINT_FETCH 4
439 #define A_HINT_STORE 8
440 #define A_HINT_EXISTS 16
441 #define A_HINT_DELETE 32
442 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
443 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
444 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
446 /* Only used in op flags */
447 #define A_HINT_ROOT 64
448 #define A_HINT_DEREF 128
450 static VOL U32 a_hash = 0;
452 static UV a_hint(pTHX) {
453 #define a_hint() a_hint(aTHX)
455 #ifdef cop_hints_fetch_pvn
456 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
457 #elif A_HAS_PERL(5, 9, 5)
458 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
460 __PACKAGE__, __PACKAGE_LEN__,
464 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
469 return a_detag(hint);
472 /* ... op => info map ...................................................... */
480 #define PTABLE_NAME ptable_map
481 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
485 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
486 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
487 #define ptable_map_delete(T, K) ptable_map_delete(aPTBLMS_ (T), (K))
489 static ptable *a_op_map = NULL;
493 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
495 static perl_mutex a_op_map_mutex;
497 #define A_LOCK(M) MUTEX_LOCK(M)
498 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
500 static const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
501 const a_op_info *val;
503 A_LOCK(&a_op_map_mutex);
505 val = ptable_fetch(a_op_map, o);
511 A_UNLOCK(&a_op_map_mutex);
516 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
518 #else /* USE_ITHREADS */
520 #define dA_MAP_THX dNOOP
522 #define A_LOCK(M) NOOP
523 #define A_UNLOCK(M) NOOP
525 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
527 #endif /* !USE_ITHREADS */
529 static const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
530 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
533 if (!(oi = ptable_fetch(a_op_map, o))) {
534 oi = PerlMemShared_malloc(sizeof *oi);
535 ptable_map_store(a_op_map, o, oi);
545 static void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
546 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
547 A_LOCK(&a_op_map_mutex);
549 a_map_store_locked(o, old_pp, next, flags);
551 A_UNLOCK(&a_op_map_mutex);
554 static void a_map_delete(pTHX_ const OP *o) {
555 #define a_map_delete(O) a_map_delete(aTHX_ (O))
556 A_LOCK(&a_op_map_mutex);
558 ptable_map_delete(a_op_map, o);
560 A_UNLOCK(&a_op_map_mutex);
563 static const OP *a_map_descend(const OP *o) {
564 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
568 case OA_BASEOP_OR_UNOP:
569 return cUNOPo->op_first;
572 return cLISTOPo->op_last;
578 static void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
579 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
580 const a_op_info *roi;
584 A_LOCK(&a_op_map_mutex);
586 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
588 while (o->op_flags & OPf_KIDS) {
589 o = a_map_descend(o);
592 if ((oi = ptable_fetch(a_op_map, o))) {
593 oi->flags &= ~A_HINT_ROOT;
594 oi->next = (a_op_info *) roi;
599 A_UNLOCK(&a_op_map_mutex);
604 static void a_map_update_flags_topdown(const OP *root, UV flags) {
608 A_LOCK(&a_op_map_mutex);
610 flags &= ~A_HINT_ROOT;
613 if ((oi = ptable_fetch(a_op_map, o)))
614 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
615 if (!(o->op_flags & OPf_KIDS))
617 o = a_map_descend(o);
620 A_UNLOCK(&a_op_map_mutex);
625 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
627 static void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
630 A_LOCK(&a_op_map_mutex);
632 flags &= ~A_HINT_ROOT;
633 rflags |= A_HINT_ROOT;
635 oi = ptable_fetch(a_op_map, o);
636 while (!(oi->flags & A_HINT_ROOT)) {
642 A_UNLOCK(&a_op_map_mutex);
647 /* ... Decide whether this expression should be autovivified or not ........ */
649 static UV a_map_resolve(const OP *o, const a_op_info *oi) {
650 UV flags = 0, rflags;
652 const a_op_info *roi = oi;
654 while (!(roi->flags & A_HINT_ROOT))
659 rflags = roi->flags & ~A_HINT_ROOT;
664 if (root->op_flags & OPf_MOD) {
665 if (rflags & A_HINT_STORE)
666 flags = (A_HINT_STORE|A_HINT_DEREF);
667 } else if (rflags & A_HINT_FETCH)
668 flags = (A_HINT_FETCH|A_HINT_DEREF);
672 a_map_update_flags_bottomup(o, 0, 0);
676 flags |= (rflags & A_HINT_NOTIFY);
677 a_map_update_flags_bottomup(o, flags, 0);
679 return oi->flags & A_HINT_ROOT ? 0 : flags;
682 /* ... Inspired from pp_defined() .......................................... */
684 static int a_undef(pTHX_ SV *sv) {
685 #define a_undef(S) a_undef(aTHX_ (S))
686 switch (SvTYPE(sv)) {
690 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
691 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
695 if (HvARRAY(sv) || SvGMAGICAL(sv)
696 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
708 /* --- PP functions -------------------------------------------------------- */
710 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
711 * value, another extension might have saved our pp replacement as the ppaddr
712 * for this op, so this doesn't ensure that our function will never be called
713 * again. That's why we don't remove the op info from our map, so that it can
714 * still run correctly if required. */
716 /* ... pp_rv2av ............................................................ */
718 static OP *a_pp_rv2av(pTHX) {
723 oi = a_map_fetch(PL_op);
725 if (oi->flags & A_HINT_DEREF) {
727 /* We always need to push an empty array to fool the pp_aelem() that comes
731 av = sv_2mortal((SV *) newAV());
737 return oi->old_pp(aTHX);
740 /* ... pp_rv2hv ............................................................ */
742 static OP *a_pp_rv2hv_simple(pTHX) {
747 oi = a_map_fetch(PL_op);
749 if (oi->flags & A_HINT_DEREF) {
754 return oi->old_pp(aTHX);
757 static OP *a_pp_rv2hv(pTHX) {
762 oi = a_map_fetch(PL_op);
764 if (oi->flags & A_HINT_DEREF) {
768 hv = sv_2mortal((SV *) newHV());
774 return oi->old_pp(aTHX);
777 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
779 static void a_cannot_vivify(pTHX_ UV flags) {
780 #define a_cannot_vivify(F) a_cannot_vivify(aTHX_ (F))
781 if (flags & A_HINT_STRICT)
782 croak("Reference vivification forbidden");
783 else if (flags & A_HINT_WARN)
784 warn("Reference was vivified");
785 else /* A_HINT_STORE */
786 croak("Can't vivify reference");
789 static OP *a_pp_deref(pTHX) {
795 oi = a_map_fetch(PL_op);
798 if (flags & A_HINT_DEREF) {
801 o = oi->old_pp(aTHX);
803 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
806 a_cannot_vivify(flags);
812 return oi->old_pp(aTHX);
815 /* ... pp_root (exists,delete,keys,values) ................................. */
817 static OP *a_pp_root_unop(pTHX) {
822 /* Can only be reached by keys or values */
823 if (GIMME_V == G_SCALAR) {
832 const a_op_info *oi = a_map_fetch(PL_op);
833 return oi->old_pp(aTHX);
837 static OP *a_pp_root_binop(pTHX) {
840 if (a_undef(TOPm1s)) {
843 if (PL_op->op_type == OP_EXISTS)
851 const a_op_info *oi = a_map_fetch(PL_op);
852 return oi->old_pp(aTHX);
858 /* ... pp_multideref ....................................................... */
860 /* This pp replacement is actually only called for topmost exists/delete ops,
861 * because we hijack the [ah]elem check functions and this disables the
862 * optimization for lvalue and rvalue dereferencing. In particular, the
863 * OPf_MOD branches should never be covered. In the future, the multideref
864 * optimization might also be disabled for custom exists/delete check functions,
865 * which will make this section unnecessary. However, the code tries to be as
866 * general as possible in case I think of a way to reenable the multideref
867 * optimization even when this module is in use. */
869 static UV a_do_multideref(const OP *o, UV flags) {
870 UV isexdel, other_flags;
872 assert(o->op_type == OP_MULTIDEREF);
874 other_flags = flags & ~A_HINT_DO;
876 isexdel = o->op_private & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE);
878 if (isexdel & OPpMULTIDEREF_EXISTS) {
879 flags &= A_HINT_EXISTS;
881 flags &= A_HINT_DELETE;
884 if (o->op_flags & OPf_MOD) {
885 flags &= A_HINT_STORE;
887 flags &= A_HINT_FETCH;
891 return flags ? (flags | other_flags) : 0;
894 static SV *a_do_fake_pp(pTHX_ OP *op) {
895 #define a_do_fake_pp(O) a_do_fake_pp(aTHX_ (O))
901 PL_op->op_ppaddr(aTHX);
915 static void a_do_fake_pp_unop_init(pTHX_ UNOP *unop, U32 type, U32 flags) {
916 #define a_do_fake_pp_unop_init(O, T, F) a_do_fake_pp_unop_init(aTHX_ (O), (T), (F))
917 unop->op_type = type;
918 unop->op_flags = OPf_WANT_SCALAR | (~OPf_WANT & flags);
919 unop->op_private = 0;
920 unop->op_first = NULL;
921 unop->op_ppaddr = PL_ppaddr[type];
924 static SV *a_do_fake_pp_unop_arg1(pTHX_ U32 type, U32 flags, SV *arg) {
925 #define a_do_fake_pp_unop_arg1(T, F, A) a_do_fake_pp_unop_arg1(aTHX_ (T), (F), (A))
929 a_do_fake_pp_unop_init(&unop, type, flags);
935 return a_do_fake_pp((OP *) &unop);
938 static SV *a_do_fake_pp_unop_arg2(pTHX_ U32 type, U32 flags, SV *arg1, SV *arg2) {
939 #define a_do_fake_pp_unop_arg2(T, F, A1, A2) a_do_fake_pp_unop_arg2(aTHX_ (T), (F), (A1), (A2))
943 a_do_fake_pp_unop_init(&unop, type, flags);
950 return a_do_fake_pp((OP *) &unop);
953 #define a_do_pp_rv2av(R) a_do_fake_pp_unop_arg1(OP_RV2AV, OPf_REF, (R))
954 #define a_do_pp_afetch(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, 0, (A), (I))
955 #define a_do_pp_afetch_lv(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, OPf_MOD, (A), (I))
956 #define a_do_pp_aexists(A, I) a_do_fake_pp_unop_arg2(OP_EXISTS, OPf_SPECIAL, (A), (I))
957 #define a_do_pp_adelete(A, I) a_do_fake_pp_unop_arg2(OP_DELETE, OPf_SPECIAL, (A), (I))
959 #define a_do_pp_rv2hv(R) a_do_fake_pp_unop_arg1(OP_RV2HV, OPf_REF, (R))
960 #define a_do_pp_hfetch(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, 0, (H), (K))
961 #define a_do_pp_hfetch_lv(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, OPf_MOD, (H), (K))
962 #define a_do_pp_hexists(H, K) a_do_fake_pp_unop_arg2(OP_EXISTS, 0, (H), (K))
963 #define a_do_pp_hdelete(H, K) a_do_fake_pp_unop_arg2(OP_DELETE, 0, (H), (K))
965 static OP *a_pp_multideref(pTHX) {
966 UNOP_AUX_item *items;
974 const a_op_info *oi = a_map_fetch(PL_op);
976 flags = a_do_multideref(PL_op, oi->flags);
978 return oi->old_pp(aTHX);
981 items = cUNOP_AUXx(PL_op)->op_aux;
984 PL_multideref_pc = items;
987 switch (actions & MDEREF_ACTION_MASK) {
989 actions = (++items)->uv;
991 case MDEREF_AV_padav_aelem: /* $lex[...] */
992 sv = PAD_SVl((++items)->pad_offset);
996 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
997 sv = UNOP_AUX_item_sv(++items);
998 assert(isGV_with_GP(sv));
999 sv = (SV *) GvAVn((GV *) sv);
1003 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
1007 goto do_AV_rv2av_aelem;
1008 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
1009 sv = UNOP_AUX_item_sv(++items);
1010 assert(isGV_with_GP(sv));
1011 sv = GvSVn((GV *) sv);
1014 goto do_AV_vivify_rv2av_aelem;
1015 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
1016 sv = PAD_SVl((++items)->pad_offset);
1018 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
1021 do_AV_vivify_rv2av_aelem:
1022 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_AV);
1024 sv = a_do_pp_rv2av(sv);
1028 assert(SvTYPE(sv) == SVt_PVAV);
1029 switch (actions & MDEREF_INDEX_MASK) {
1030 case MDEREF_INDEX_none:
1032 case MDEREF_INDEX_const:
1033 esv = sv_2mortal(newSViv((++items)->iv));
1035 case MDEREF_INDEX_padsv:
1036 esv = PAD_SVl((++items)->pad_offset);
1038 case MDEREF_INDEX_gvsv:
1039 esv = UNOP_AUX_item_sv(++items);
1040 assert(isGV_with_GP(esv));
1041 esv = GvSVn((GV *) esv);
1043 if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC)))
1044 Perl_warner(aTHX_ packWARN(WARN_MISC),
1045 "Use of reference \"%"SVf"\" as array index",
1049 PL_multideref_pc = items;
1050 if (actions & MDEREF_FLAG_last) {
1051 switch (flags & A_HINT_DO) {
1053 sv = a_do_pp_afetch(sv, esv);
1056 sv = a_do_pp_afetch_lv(sv, esv);
1059 sv = a_do_pp_aexists(sv, esv);
1062 sv = a_do_pp_adelete(sv, esv);
1067 sv = a_do_pp_afetch(sv, esv);
1070 case MDEREF_HV_padhv_helem: /* $lex{...} */
1071 sv = PAD_SVl((++items)->pad_offset);
1075 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
1076 sv = UNOP_AUX_item_sv(++items);
1077 assert(isGV_with_GP(sv));
1078 sv = (SV *) GvHVn((GV *) sv);
1082 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
1086 goto do_HV_rv2hv_helem;
1087 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
1088 sv = UNOP_AUX_item_sv(++items);
1089 assert(isGV_with_GP(sv));
1090 sv = GvSVn((GV *) sv);
1093 goto do_HV_vivify_rv2hv_helem;
1094 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
1095 sv = PAD_SVl((++items)->pad_offset);
1097 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
1100 do_HV_vivify_rv2hv_helem:
1101 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_HV);
1103 sv = a_do_pp_rv2hv(sv);
1107 assert(SvTYPE(sv) == SVt_PVHV);
1108 switch (actions & MDEREF_INDEX_MASK) {
1109 case MDEREF_INDEX_none:
1111 case MDEREF_INDEX_const:
1112 key = UNOP_AUX_item_sv(++items);
1114 case MDEREF_INDEX_padsv:
1115 key = PAD_SVl((++items)->pad_offset);
1117 case MDEREF_INDEX_gvsv:
1118 key = UNOP_AUX_item_sv(++items);
1119 assert(isGV_with_GP(key));
1120 key = GvSVn((GV *) key);
1123 PL_multideref_pc = items;
1124 if (actions & MDEREF_FLAG_last) {
1125 switch (flags & A_HINT_DO) {
1127 sv = a_do_pp_hfetch(sv, key);
1130 sv = a_do_pp_hfetch_lv(sv, key);
1133 sv = a_do_pp_hexists(sv, key);
1136 sv = a_do_pp_hdelete(sv, key);
1143 sv = a_do_pp_hfetch(sv, key);
1148 actions >>= MDEREF_SHIFT;
1152 if (flags & (A_HINT_NOTIFY|A_HINT_STORE))
1153 a_cannot_vivify(flags);
1154 if (flags & A_HINT_EXISTS)
1163 #endif /* A_HAS_MULTIDEREF */
1165 /* --- Check functions ----------------------------------------------------- */
1167 static void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
1168 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
1170 if (o->op_type == type && o->op_ppaddr != new_pp
1171 && cUNOPo->op_first->op_type != OP_GV) {
1173 const a_op_info *oi = a_map_fetch(o);
1175 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1176 o->op_ppaddr = new_pp;
1183 /* ... ck_pad{any,sv} ...................................................... */
1185 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
1186 * function, but are instead manually mutated from a padany. So we store
1187 * the op entry in the op map in the padany check function, and we set their
1188 * op_ppaddr member in our peephole optimizer replacement below. */
1190 static OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
1192 static OP *a_ck_padany(pTHX_ OP *o) {
1195 o = a_old_ck_padany(aTHX_ o);
1198 if (hint & A_HINT_DO)
1199 a_map_store_root(o, o->op_ppaddr, hint);
1206 static OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
1208 static OP *a_ck_padsv(pTHX_ OP *o) {
1211 o = a_old_ck_padsv(aTHX_ o);
1214 if (hint & A_HINT_DO) {
1215 a_map_store_root(o, o->op_ppaddr, hint);
1216 o->op_ppaddr = a_pp_deref;
1223 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
1225 /* Those ops appear both at the root and inside an expression but there's no
1226 * way to distinguish both situations. Worse, we can't even know if we are in a
1227 * modifying context, so the expression can't be resolved yet. It will be at the
1228 * first invocation of a_pp_deref() for this expression. */
1230 static OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
1231 static OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
1232 static OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
1234 static OP *a_ck_deref(pTHX_ OP *o) {
1235 OP * (*old_ck)(pTHX_ OP *o) = 0;
1238 switch (o->op_type) {
1240 old_ck = a_old_ck_aelem;
1241 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1242 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
1245 old_ck = a_old_ck_helem;
1246 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1247 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
1250 old_ck = a_old_ck_rv2sv;
1253 o = old_ck(aTHX_ o);
1255 if (hint & A_HINT_DO) {
1256 #if A_HAS_MULTIDEREF
1257 if (old_ck == a_old_ck_rv2sv && o->op_flags & OPf_KIDS) {
1258 OP *kid = cUNOPo->op_first;
1259 if (kid && kid->op_type == OP_GV)
1260 a_map_store(kid, kid->op_ppaddr, NULL, hint);
1263 a_map_store_root(o, o->op_ppaddr, hint);
1264 o->op_ppaddr = a_pp_deref;
1271 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
1273 /* Those ops also appear both inisde and at the root, hence the caveats for
1274 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
1275 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
1278 static OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
1279 static OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
1281 static OP *a_ck_rv2xv(pTHX_ OP *o) {
1282 OP * (*old_ck)(pTHX_ OP *o) = 0;
1283 OP * (*new_pp)(pTHX) = 0;
1286 switch (o->op_type) {
1287 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
1288 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
1290 o = old_ck(aTHX_ o);
1292 if (cUNOPo->op_first->op_type == OP_GV)
1296 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
1297 a_map_store_root(o, o->op_ppaddr, hint);
1298 o->op_ppaddr = new_pp;
1305 /* ... ck_xslice (aslice,hslice) ........................................... */
1307 /* I think those are only found at the root, but there's nothing that really
1308 * prevent them to be inside the expression too. We only need to update the
1309 * root so that the rest of the expression will see the right context when
1310 * resolving. That's why we don't replace the ppaddr. */
1312 static OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
1313 static OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
1315 static OP *a_ck_xslice(pTHX_ OP *o) {
1316 OP * (*old_ck)(pTHX_ OP *o) = 0;
1319 switch (o->op_type) {
1321 old_ck = a_old_ck_aslice;
1324 old_ck = a_old_ck_hslice;
1325 if (hint & A_HINT_DO)
1326 a_recheck_rv2xv(OpSIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv);
1329 o = old_ck(aTHX_ o);
1331 if (hint & A_HINT_DO) {
1332 a_map_store_root(o, 0, hint);
1339 /* ... ck_root (exists,delete,keys,values) ................................. */
1341 /* Those ops are only found at the root of a dereferencing expression. We can
1342 * then resolve at compile time if vivification must take place or not. */
1344 static OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
1345 static OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
1346 static OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
1347 static OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
1349 static OP *a_ck_root(pTHX_ OP *o) {
1350 OP * (*old_ck)(pTHX_ OP *o) = 0;
1351 OP * (*new_pp)(pTHX) = 0;
1352 bool enabled = FALSE;
1355 switch (o->op_type) {
1357 old_ck = a_old_ck_exists;
1358 new_pp = a_pp_root_binop;
1359 enabled = hint & A_HINT_EXISTS;
1362 old_ck = a_old_ck_delete;
1363 new_pp = a_pp_root_binop;
1364 enabled = hint & A_HINT_DELETE;
1367 old_ck = a_old_ck_keys;
1368 new_pp = a_pp_root_unop;
1369 enabled = hint & A_HINT_FETCH;
1372 old_ck = a_old_ck_values;
1373 new_pp = a_pp_root_unop;
1374 enabled = hint & A_HINT_FETCH;
1377 o = old_ck(aTHX_ o);
1379 if (hint & A_HINT_DO) {
1381 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
1382 a_map_store_root(o, o->op_ppaddr, hint);
1383 o->op_ppaddr = new_pp;
1393 /* ... Our peephole optimizer .............................................. */
1395 static void a_peep_rec(pTHX_ OP *o, ptable *seen);
1397 static void a_peep_rec(pTHX_ OP *o, ptable *seen) {
1398 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
1399 for (; o; o = o->op_next) {
1401 const a_op_info *oi = NULL;
1405 if (ptable_fetch(seen, o))
1407 ptable_seen_store(seen, o, o);
1410 switch (o->op_type) {
1416 if (ptable_fetch(seen, o))
1418 ptable_seen_store(seen, o, o);
1422 if (o->op_ppaddr != a_pp_deref) {
1423 oi = a_map_fetch(o);
1424 if (oi && (oi->flags & A_HINT_DO)) {
1425 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1426 o->op_ppaddr = a_pp_deref;
1434 if (o->op_ppaddr != a_pp_deref)
1436 oi = a_map_fetch(o);
1440 if (!(flags & A_HINT_DEREF)
1441 && (flags & A_HINT_DO)
1442 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1443 /* Decide if the expression must autovivify or not. */
1444 flags = a_map_resolve(o, oi);
1446 if (flags & A_HINT_DEREF)
1447 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1449 o->op_ppaddr = oi->old_pp;
1453 if ( o->op_ppaddr != a_pp_rv2av
1454 && o->op_ppaddr != a_pp_rv2hv
1455 && o->op_ppaddr != a_pp_rv2hv_simple)
1457 oi = a_map_fetch(o);
1460 if (!(oi->flags & A_HINT_DEREF))
1461 o->op_ppaddr = oi->old_pp;
1463 #if A_HAS_MULTIDEREF
1465 if (o->op_ppaddr != a_pp_multideref) {
1466 oi = a_map_fetch(cUNOPo->op_first);
1470 if (a_do_multideref(o, flags)) {
1471 a_map_store_root(o, o->op_ppaddr, flags & ~A_HINT_DEREF);
1472 o->op_ppaddr = a_pp_multideref;
1486 # if A_HAS_PERL(5, 10, 0)
1491 a_peep_rec(cLOGOPo->op_other);
1495 a_peep_rec(cLOOPo->op_redoop);
1496 a_peep_rec(cLOOPo->op_nextop);
1497 a_peep_rec(cLOOPo->op_lastop);
1499 # if A_HAS_PERL(5, 9, 5)
1501 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1507 a_peep_rec(cPMOPo->op_pmreplstart);
1510 #endif /* !A_HAS_RPEEP */
1517 static void a_peep(pTHX_ OP *o) {
1521 assert(a_is_loaded(&MY_CXT));
1523 MY_CXT.old_peep(aTHX_ o);
1527 ptable_seen_clear(seen);
1529 ptable_seen_clear(seen);
1533 /* --- Module setup/teardown ----------------------------------------------- */
1535 static void a_teardown(pTHX_ void *root) {
1540 if (a_clear_loaded_locked(&MY_CXT)) {
1541 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1542 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1544 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1545 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1546 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1548 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1549 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1551 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1552 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1554 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1555 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1556 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1557 a_ck_restore(OP_VALUES, &a_old_ck_values);
1559 ptable_map_free(a_op_map);
1565 if (MY_CXT.old_peep) {
1567 PL_rpeepp = MY_CXT.old_peep;
1569 PL_peepp = MY_CXT.old_peep;
1571 MY_CXT.old_peep = 0;
1574 ptable_seen_free(MY_CXT.seen);
1577 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1578 ptable_hints_free(MY_CXT.tbl);
1580 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1585 static void a_setup(pTHX) {
1586 #define a_setup() a_setup(aTHX)
1587 MY_CXT_INIT; /* Takes/release PL_my_ctx_mutex */
1591 if (a_set_loaded_locked(&MY_CXT)) {
1592 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1594 a_op_map = ptable_new();
1596 MUTEX_INIT(&a_op_map_mutex);
1599 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1600 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1602 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1603 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1604 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1606 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1607 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1609 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1610 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1612 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1613 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1614 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1615 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1623 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1624 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1625 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1626 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1627 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1628 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1629 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1630 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1631 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1632 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1636 if (PL_rpeepp != a_peep) {
1637 MY_CXT.old_peep = PL_rpeepp;
1641 if (PL_peepp != a_peep) {
1642 MY_CXT.old_peep = PL_peepp;
1647 MY_CXT.old_peep = 0;
1650 MY_CXT.seen = ptable_new();
1652 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1653 MY_CXT.tbl = ptable_new();
1654 MY_CXT.owner = aTHX;
1655 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1657 call_atexit(a_teardown, NULL);
1662 /* --- XS ------------------------------------------------------------------ */
1664 MODULE = autovivification PACKAGE = autovivification
1679 #if A_WORKAROUND_REQUIRE_PROPAGATION
1683 #if A_WORKAROUND_REQUIRE_PROPAGATION
1685 a_ptable_clone_ud ud;
1688 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1689 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1690 a_ptable_clone_ud_deinit(ud);
1695 #if A_WORKAROUND_REQUIRE_PROPAGATION
1697 MY_CXT.owner = aTHX;
1699 MY_CXT.seen = ptable_new();
1703 global_setup = a_set_loaded_locked(&MY_CXT);
1704 assert(!global_setup);
1710 #endif /* A_THREADSAFE */
1716 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1726 RETVAL = newSVuv(a_detag(tag));