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_NEED_DELETE 1
164 #define PTABLE_NEED_WALK 0
168 #define ptable_loaded_store(T, K, V) ptable_loaded_store(aPTBLMS_ (T), (K), (V))
169 #define ptable_loaded_delete(T, K) ptable_loaded_delete(aPTBLMS_ (T), (K))
170 #define ptable_loaded_free(T) ptable_loaded_free(aPTBLMS_ (T))
172 static ptable *a_loaded_cxts = NULL;
174 static int a_is_loaded(pTHX_ void *cxt) {
175 #define a_is_loaded(C) a_is_loaded(aTHX_ (C))
179 if (a_loaded_cxts && ptable_fetch(a_loaded_cxts, cxt))
186 static int a_set_loaded_locked(pTHX_ void *cxt) {
187 #define a_set_loaded_locked(C) a_set_loaded_locked(aTHX_ (C))
188 int global_setup = 0;
191 assert(a_loaded == 0);
192 assert(!a_loaded_cxts);
193 a_loaded_cxts = ptable_new();
197 assert(a_loaded_cxts);
198 ptable_loaded_store(a_loaded_cxts, cxt, cxt);
203 static int a_clear_loaded_locked(pTHX_ void *cxt) {
204 #define a_clear_loaded_locked(C) a_clear_loaded_locked(aTHX_ (C))
205 int global_teardown = 0;
208 assert(a_loaded_cxts);
209 ptable_loaded_delete(a_loaded_cxts, cxt);
211 } else if (a_loaded_cxts) {
212 assert(a_loaded == 1);
213 ptable_loaded_free(a_loaded_cxts);
214 a_loaded_cxts = NULL;
219 return global_teardown;
224 #define a_is_loaded(C) (a_loaded > 0)
225 #define a_set_loaded_locked(C) ((a_loaded++ <= 0) ? 1 : 0)
226 #define a_clear_loaded_locked(C) ((--a_loaded <= 0) ? 1 : 0)
230 /* ... Thread-safe hints ................................................... */
232 #if A_WORKAROUND_REQUIRE_PROPAGATION
239 #define A_HINT_FREE(H) PerlMemShared_free(H)
243 #define PTABLE_NAME ptable_hints
244 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
245 #define PTABLE_NEED_DELETE 0
246 #define PTABLE_NEED_WALK 1
255 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
256 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
258 #endif /* A_THREADSAFE */
260 #endif /* A_WORKAROUND_REQUIRE_PROPAGATION */
262 #define PTABLE_NAME ptable_seen
263 #define PTABLE_NEED_DELETE 0
264 #define PTABLE_NEED_WALK 0
268 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
269 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
270 #define ptable_seen_clear(T) ptable_seen_clear(aPTBLMS_ (T))
271 #define ptable_seen_free(T) ptable_seen_free(aPTBLMS_ (T))
273 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
276 peep_t old_peep; /* This is actually the rpeep past 5.13.5 */
277 ptable *seen; /* It really is a ptable_seen */
278 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
279 ptable *tbl; /* It really is a ptable_hints */
281 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
286 #if A_WORKAROUND_REQUIRE_PROPAGATION
292 #if A_HAS_PERL(5, 13, 2)
293 CLONE_PARAMS *params;
299 #if A_HAS_PERL(5, 13, 2)
300 # define a_ptable_clone_ud_init(U, T, O) \
302 (U).params = Perl_clone_params_new((O), aTHX)
303 # define a_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
304 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
306 # define a_ptable_clone_ud_init(U, T, O) \
308 (U).params.stashes = newAV(); \
309 (U).params.flags = 0; \
310 (U).params.proto_perl = (O)
311 # define a_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
312 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
315 static void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
316 a_ptable_clone_ud *ud = ud_;
317 a_hint_t *h1 = ent->val;
320 h2 = PerlMemShared_malloc(sizeof *h2);
322 h2->require_tag = PTR2IV(a_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
324 ptable_hints_store(ud->tbl, ent->key, h2);
327 #endif /* A_THREADSAFE */
329 static IV a_require_tag(pTHX) {
330 #define a_require_tag() a_require_tag(aTHX)
331 const CV *cv, *outside;
336 /* If for some reason the pragma is operational at run-time, try to discover
337 * the current cv in use. */
340 for (si = PL_curstackinfo; si; si = si->si_prev) {
343 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
344 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
346 switch (CxTYPE(cx)) {
349 /* The propagation workaround is only needed up to 5.10.0 and at that
350 * time format and sub contexts were still identical. And even later the
351 * cv members offsets should have been kept the same. */
353 goto get_enclosing_cv;
355 cv = cx->blk_eval.cv;
356 goto get_enclosing_cv;
367 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
373 static SV *a_tag(pTHX_ UV bits) {
374 #define a_tag(B) a_tag(aTHX_ (B))
381 #endif /* A_THREADSAFE */
383 h = PerlMemShared_malloc(sizeof *h);
385 h->require_tag = a_require_tag();
388 /* We only need for the key to be an unique tag for looking up the value later
389 * Allocated memory provides convenient unique identifiers, so that's why we
390 * use the hint as the key itself. */
391 ptable_hints_store(MY_CXT.tbl, h, h);
392 #endif /* A_THREADSAFE */
394 return newSViv(PTR2IV(h));
397 static UV a_detag(pTHX_ const SV *hint) {
398 #define a_detag(H) a_detag(aTHX_ (H))
405 #endif /* A_THREADSAFE */
407 if (!(hint && SvIOK(hint)))
410 h = INT2PTR(a_hint_t *, SvIVX(hint));
412 h = ptable_fetch(MY_CXT.tbl, h);
413 #endif /* A_THREADSAFE */
415 if (a_require_tag() != h->require_tag)
421 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
423 #define a_tag(B) newSVuv(B)
424 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
431 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
437 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
439 /* Used both for hints and op flags */
440 #define A_HINT_STRICT 1
441 #define A_HINT_WARN 2
442 #define A_HINT_FETCH 4
443 #define A_HINT_STORE 8
444 #define A_HINT_EXISTS 16
445 #define A_HINT_DELETE 32
446 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
447 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
448 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
450 /* Only used in op flags */
451 #define A_HINT_ROOT 64
452 #define A_HINT_DEREF 128
454 static VOL U32 a_hash = 0;
456 static UV a_hint(pTHX) {
457 #define a_hint() a_hint(aTHX)
459 #ifdef cop_hints_fetch_pvn
460 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
461 #elif A_HAS_PERL(5, 9, 5)
462 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
464 __PACKAGE__, __PACKAGE_LEN__,
468 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
473 return a_detag(hint);
476 /* ... op => info map ...................................................... */
484 #define PTABLE_NAME ptable_map
485 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
486 #define PTABLE_NEED_DELETE 1
487 #define PTABLE_NEED_WALK 0
491 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
492 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
493 #define ptable_map_delete(T, K) ptable_map_delete(aPTBLMS_ (T), (K))
495 static ptable *a_op_map = NULL;
499 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
501 static perl_mutex a_op_map_mutex;
503 #define A_LOCK(M) MUTEX_LOCK(M)
504 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
506 static const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
507 const a_op_info *val;
509 A_LOCK(&a_op_map_mutex);
511 val = ptable_fetch(a_op_map, o);
517 A_UNLOCK(&a_op_map_mutex);
522 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
524 #else /* USE_ITHREADS */
526 #define dA_MAP_THX dNOOP
528 #define A_LOCK(M) NOOP
529 #define A_UNLOCK(M) NOOP
531 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
533 #endif /* !USE_ITHREADS */
535 static const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
536 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
539 if (!(oi = ptable_fetch(a_op_map, o))) {
540 oi = PerlMemShared_malloc(sizeof *oi);
541 ptable_map_store(a_op_map, o, oi);
551 static void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
552 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
553 A_LOCK(&a_op_map_mutex);
555 a_map_store_locked(o, old_pp, next, flags);
557 A_UNLOCK(&a_op_map_mutex);
560 static void a_map_delete(pTHX_ const OP *o) {
561 #define a_map_delete(O) a_map_delete(aTHX_ (O))
562 A_LOCK(&a_op_map_mutex);
564 ptable_map_delete(a_op_map, o);
566 A_UNLOCK(&a_op_map_mutex);
569 static const OP *a_map_descend(const OP *o) {
570 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
574 case OA_BASEOP_OR_UNOP:
575 return cUNOPo->op_first;
578 return cLISTOPo->op_last;
584 static void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
585 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
586 const a_op_info *roi;
590 A_LOCK(&a_op_map_mutex);
592 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
594 while (o->op_flags & OPf_KIDS) {
595 o = a_map_descend(o);
598 if ((oi = ptable_fetch(a_op_map, o))) {
599 oi->flags &= ~A_HINT_ROOT;
600 oi->next = (a_op_info *) roi;
605 A_UNLOCK(&a_op_map_mutex);
610 static void a_map_update_flags_topdown(const OP *root, UV flags) {
614 A_LOCK(&a_op_map_mutex);
616 flags &= ~A_HINT_ROOT;
619 if ((oi = ptable_fetch(a_op_map, o)))
620 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
621 if (!(o->op_flags & OPf_KIDS))
623 o = a_map_descend(o);
626 A_UNLOCK(&a_op_map_mutex);
631 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
633 static void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
636 A_LOCK(&a_op_map_mutex);
638 flags &= ~A_HINT_ROOT;
639 rflags |= A_HINT_ROOT;
641 oi = ptable_fetch(a_op_map, o);
642 while (!(oi->flags & A_HINT_ROOT)) {
648 A_UNLOCK(&a_op_map_mutex);
653 /* ... Decide whether this expression should be autovivified or not ........ */
655 static UV a_map_resolve(const OP *o, const a_op_info *oi) {
656 UV flags = 0, rflags;
658 const a_op_info *roi = oi;
660 while (!(roi->flags & A_HINT_ROOT))
665 rflags = roi->flags & ~A_HINT_ROOT;
670 if (root->op_flags & OPf_MOD) {
671 if (rflags & A_HINT_STORE)
672 flags = (A_HINT_STORE|A_HINT_DEREF);
673 } else if (rflags & A_HINT_FETCH)
674 flags = (A_HINT_FETCH|A_HINT_DEREF);
678 a_map_update_flags_bottomup(o, 0, 0);
682 flags |= (rflags & A_HINT_NOTIFY);
683 a_map_update_flags_bottomup(o, flags, 0);
685 return oi->flags & A_HINT_ROOT ? 0 : flags;
688 /* ... Inspired from pp_defined() .......................................... */
690 static int a_undef(pTHX_ SV *sv) {
691 #define a_undef(S) a_undef(aTHX_ (S))
692 switch (SvTYPE(sv)) {
696 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
697 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
701 if (HvARRAY(sv) || SvGMAGICAL(sv)
702 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
714 /* --- PP functions -------------------------------------------------------- */
716 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
717 * value, another extension might have saved our pp replacement as the ppaddr
718 * for this op, so this doesn't ensure that our function will never be called
719 * again. That's why we don't remove the op info from our map, so that it can
720 * still run correctly if required. */
722 /* ... pp_rv2av ............................................................ */
724 static OP *a_pp_rv2av(pTHX) {
729 oi = a_map_fetch(PL_op);
731 if (oi->flags & A_HINT_DEREF) {
733 /* We always need to push an empty array to fool the pp_aelem() that comes
737 av = sv_2mortal((SV *) newAV());
743 return oi->old_pp(aTHX);
746 /* ... pp_rv2hv ............................................................ */
748 static OP *a_pp_rv2hv_simple(pTHX) {
753 oi = a_map_fetch(PL_op);
755 if (oi->flags & A_HINT_DEREF) {
760 return oi->old_pp(aTHX);
763 static OP *a_pp_rv2hv(pTHX) {
768 oi = a_map_fetch(PL_op);
770 if (oi->flags & A_HINT_DEREF) {
774 hv = sv_2mortal((SV *) newHV());
780 return oi->old_pp(aTHX);
783 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
785 static void a_cannot_vivify(pTHX_ UV flags) {
786 #define a_cannot_vivify(F) a_cannot_vivify(aTHX_ (F))
787 if (flags & A_HINT_STRICT)
788 croak("Reference vivification forbidden");
789 else if (flags & A_HINT_WARN)
790 warn("Reference was vivified");
791 else /* A_HINT_STORE */
792 croak("Can't vivify reference");
795 static OP *a_pp_deref(pTHX) {
801 oi = a_map_fetch(PL_op);
804 if (flags & A_HINT_DEREF) {
807 o = oi->old_pp(aTHX);
809 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
812 a_cannot_vivify(flags);
818 return oi->old_pp(aTHX);
821 /* ... pp_root (exists,delete,keys,values) ................................. */
823 static OP *a_pp_root_unop(pTHX) {
828 /* Can only be reached by keys or values */
829 if (GIMME_V == G_SCALAR) {
838 const a_op_info *oi = a_map_fetch(PL_op);
839 return oi->old_pp(aTHX);
843 static OP *a_pp_root_binop(pTHX) {
846 if (a_undef(TOPm1s)) {
849 if (PL_op->op_type == OP_EXISTS)
857 const a_op_info *oi = a_map_fetch(PL_op);
858 return oi->old_pp(aTHX);
864 /* ... pp_multideref ....................................................... */
866 /* This pp replacement is actually only called for topmost exists/delete ops,
867 * because we hijack the [ah]elem check functions and this disables the
868 * optimization for lvalue and rvalue dereferencing. In particular, the
869 * OPf_MOD branches should never be covered. In the future, the multideref
870 * optimization might also be disabled for custom exists/delete check functions,
871 * which will make this section unnecessary. However, the code tries to be as
872 * general as possible in case I think of a way to reenable the multideref
873 * optimization even when this module is in use. */
875 static UV a_do_multideref(const OP *o, UV flags) {
876 UV isexdel, other_flags;
878 assert(o->op_type == OP_MULTIDEREF);
880 other_flags = flags & ~A_HINT_DO;
882 isexdel = o->op_private & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE);
884 if (isexdel & OPpMULTIDEREF_EXISTS) {
885 flags &= A_HINT_EXISTS;
887 flags &= A_HINT_DELETE;
890 if (o->op_flags & OPf_MOD) {
891 flags &= A_HINT_STORE;
893 flags &= A_HINT_FETCH;
897 return flags ? (flags | other_flags) : 0;
900 static SV *a_do_fake_pp(pTHX_ OP *op) {
901 #define a_do_fake_pp(O) a_do_fake_pp(aTHX_ (O))
907 PL_op->op_ppaddr(aTHX);
921 static void a_do_fake_pp_unop_init(pTHX_ UNOP *unop, U32 type, U32 flags) {
922 #define a_do_fake_pp_unop_init(O, T, F) a_do_fake_pp_unop_init(aTHX_ (O), (T), (F))
923 unop->op_type = type;
924 unop->op_flags = OPf_WANT_SCALAR | (~OPf_WANT & flags);
925 unop->op_private = 0;
926 unop->op_first = NULL;
927 unop->op_ppaddr = PL_ppaddr[type];
930 static SV *a_do_fake_pp_unop_arg1(pTHX_ U32 type, U32 flags, SV *arg) {
931 #define a_do_fake_pp_unop_arg1(T, F, A) a_do_fake_pp_unop_arg1(aTHX_ (T), (F), (A))
935 a_do_fake_pp_unop_init(&unop, type, flags);
941 return a_do_fake_pp((OP *) &unop);
944 static SV *a_do_fake_pp_unop_arg2(pTHX_ U32 type, U32 flags, SV *arg1, SV *arg2) {
945 #define a_do_fake_pp_unop_arg2(T, F, A1, A2) a_do_fake_pp_unop_arg2(aTHX_ (T), (F), (A1), (A2))
949 a_do_fake_pp_unop_init(&unop, type, flags);
956 return a_do_fake_pp((OP *) &unop);
959 #define a_do_pp_rv2av(R) a_do_fake_pp_unop_arg1(OP_RV2AV, OPf_REF, (R))
960 #define a_do_pp_afetch(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, 0, (A), (I))
961 #define a_do_pp_afetch_lv(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, OPf_MOD, (A), (I))
962 #define a_do_pp_aexists(A, I) a_do_fake_pp_unop_arg2(OP_EXISTS, OPf_SPECIAL, (A), (I))
963 #define a_do_pp_adelete(A, I) a_do_fake_pp_unop_arg2(OP_DELETE, OPf_SPECIAL, (A), (I))
965 #define a_do_pp_rv2hv(R) a_do_fake_pp_unop_arg1(OP_RV2HV, OPf_REF, (R))
966 #define a_do_pp_hfetch(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, 0, (H), (K))
967 #define a_do_pp_hfetch_lv(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, OPf_MOD, (H), (K))
968 #define a_do_pp_hexists(H, K) a_do_fake_pp_unop_arg2(OP_EXISTS, 0, (H), (K))
969 #define a_do_pp_hdelete(H, K) a_do_fake_pp_unop_arg2(OP_DELETE, 0, (H), (K))
971 static OP *a_pp_multideref(pTHX) {
972 UNOP_AUX_item *items;
980 const a_op_info *oi = a_map_fetch(PL_op);
982 flags = a_do_multideref(PL_op, oi->flags);
984 return oi->old_pp(aTHX);
987 items = cUNOP_AUXx(PL_op)->op_aux;
990 PL_multideref_pc = items;
993 switch (actions & MDEREF_ACTION_MASK) {
995 actions = (++items)->uv;
997 case MDEREF_AV_padav_aelem: /* $lex[...] */
998 sv = PAD_SVl((++items)->pad_offset);
1002 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
1003 sv = UNOP_AUX_item_sv(++items);
1004 assert(isGV_with_GP(sv));
1005 sv = (SV *) GvAVn((GV *) sv);
1009 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
1013 goto do_AV_rv2av_aelem;
1014 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
1015 sv = UNOP_AUX_item_sv(++items);
1016 assert(isGV_with_GP(sv));
1017 sv = GvSVn((GV *) sv);
1020 goto do_AV_vivify_rv2av_aelem;
1021 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
1022 sv = PAD_SVl((++items)->pad_offset);
1024 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
1027 do_AV_vivify_rv2av_aelem:
1028 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_AV);
1030 sv = a_do_pp_rv2av(sv);
1034 assert(SvTYPE(sv) == SVt_PVAV);
1035 switch (actions & MDEREF_INDEX_MASK) {
1036 case MDEREF_INDEX_none:
1038 case MDEREF_INDEX_const:
1039 esv = sv_2mortal(newSViv((++items)->iv));
1041 case MDEREF_INDEX_padsv:
1042 esv = PAD_SVl((++items)->pad_offset);
1044 case MDEREF_INDEX_gvsv:
1045 esv = UNOP_AUX_item_sv(++items);
1046 assert(isGV_with_GP(esv));
1047 esv = GvSVn((GV *) esv);
1049 if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC)))
1050 Perl_warner(aTHX_ packWARN(WARN_MISC),
1051 "Use of reference \"%"SVf"\" as array index",
1055 PL_multideref_pc = items;
1056 if (actions & MDEREF_FLAG_last) {
1057 switch (flags & A_HINT_DO) {
1059 sv = a_do_pp_afetch(sv, esv);
1062 sv = a_do_pp_afetch_lv(sv, esv);
1065 sv = a_do_pp_aexists(sv, esv);
1068 sv = a_do_pp_adelete(sv, esv);
1073 sv = a_do_pp_afetch(sv, esv);
1076 case MDEREF_HV_padhv_helem: /* $lex{...} */
1077 sv = PAD_SVl((++items)->pad_offset);
1081 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
1082 sv = UNOP_AUX_item_sv(++items);
1083 assert(isGV_with_GP(sv));
1084 sv = (SV *) GvHVn((GV *) sv);
1088 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
1092 goto do_HV_rv2hv_helem;
1093 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
1094 sv = UNOP_AUX_item_sv(++items);
1095 assert(isGV_with_GP(sv));
1096 sv = GvSVn((GV *) sv);
1099 goto do_HV_vivify_rv2hv_helem;
1100 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
1101 sv = PAD_SVl((++items)->pad_offset);
1103 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
1106 do_HV_vivify_rv2hv_helem:
1107 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_HV);
1109 sv = a_do_pp_rv2hv(sv);
1113 assert(SvTYPE(sv) == SVt_PVHV);
1114 switch (actions & MDEREF_INDEX_MASK) {
1115 case MDEREF_INDEX_none:
1117 case MDEREF_INDEX_const:
1118 key = UNOP_AUX_item_sv(++items);
1120 case MDEREF_INDEX_padsv:
1121 key = PAD_SVl((++items)->pad_offset);
1123 case MDEREF_INDEX_gvsv:
1124 key = UNOP_AUX_item_sv(++items);
1125 assert(isGV_with_GP(key));
1126 key = GvSVn((GV *) key);
1129 PL_multideref_pc = items;
1130 if (actions & MDEREF_FLAG_last) {
1131 switch (flags & A_HINT_DO) {
1133 sv = a_do_pp_hfetch(sv, key);
1136 sv = a_do_pp_hfetch_lv(sv, key);
1139 sv = a_do_pp_hexists(sv, key);
1142 sv = a_do_pp_hdelete(sv, key);
1149 sv = a_do_pp_hfetch(sv, key);
1154 actions >>= MDEREF_SHIFT;
1158 if (flags & (A_HINT_NOTIFY|A_HINT_STORE))
1159 a_cannot_vivify(flags);
1160 if (flags & A_HINT_EXISTS)
1169 #endif /* A_HAS_MULTIDEREF */
1171 /* --- Check functions ----------------------------------------------------- */
1173 static void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
1174 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
1176 if (o->op_type == type && o->op_ppaddr != new_pp
1177 && cUNOPo->op_first->op_type != OP_GV) {
1179 const a_op_info *oi = a_map_fetch(o);
1181 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1182 o->op_ppaddr = new_pp;
1189 /* ... ck_pad{any,sv} ...................................................... */
1191 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
1192 * function, but are instead manually mutated from a padany. So we store
1193 * the op entry in the op map in the padany check function, and we set their
1194 * op_ppaddr member in our peephole optimizer replacement below. */
1196 static OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
1198 static OP *a_ck_padany(pTHX_ OP *o) {
1201 o = a_old_ck_padany(aTHX_ o);
1204 if (hint & A_HINT_DO)
1205 a_map_store_root(o, o->op_ppaddr, hint);
1212 static OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
1214 static OP *a_ck_padsv(pTHX_ OP *o) {
1217 o = a_old_ck_padsv(aTHX_ o);
1220 if (hint & A_HINT_DO) {
1221 a_map_store_root(o, o->op_ppaddr, hint);
1222 o->op_ppaddr = a_pp_deref;
1229 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
1231 /* Those ops appear both at the root and inside an expression but there's no
1232 * way to distinguish both situations. Worse, we can't even know if we are in a
1233 * modifying context, so the expression can't be resolved yet. It will be at the
1234 * first invocation of a_pp_deref() for this expression. */
1236 static OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
1237 static OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
1238 static OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
1240 static OP *a_ck_deref(pTHX_ OP *o) {
1241 OP * (*old_ck)(pTHX_ OP *o) = 0;
1244 switch (o->op_type) {
1246 old_ck = a_old_ck_aelem;
1247 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1248 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
1251 old_ck = a_old_ck_helem;
1252 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1253 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
1256 old_ck = a_old_ck_rv2sv;
1259 o = old_ck(aTHX_ o);
1261 if (hint & A_HINT_DO) {
1262 #if A_HAS_MULTIDEREF
1263 if (old_ck == a_old_ck_rv2sv && o->op_flags & OPf_KIDS) {
1264 OP *kid = cUNOPo->op_first;
1265 if (kid && kid->op_type == OP_GV)
1266 a_map_store(kid, kid->op_ppaddr, NULL, hint);
1269 a_map_store_root(o, o->op_ppaddr, hint);
1270 o->op_ppaddr = a_pp_deref;
1277 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
1279 /* Those ops also appear both inisde and at the root, hence the caveats for
1280 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
1281 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
1284 static OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
1285 static OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
1287 static OP *a_ck_rv2xv(pTHX_ OP *o) {
1288 OP * (*old_ck)(pTHX_ OP *o) = 0;
1289 OP * (*new_pp)(pTHX) = 0;
1292 switch (o->op_type) {
1293 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
1294 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
1296 o = old_ck(aTHX_ o);
1298 if (cUNOPo->op_first->op_type == OP_GV)
1302 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
1303 a_map_store_root(o, o->op_ppaddr, hint);
1304 o->op_ppaddr = new_pp;
1311 /* ... ck_xslice (aslice,hslice) ........................................... */
1313 /* I think those are only found at the root, but there's nothing that really
1314 * prevent them to be inside the expression too. We only need to update the
1315 * root so that the rest of the expression will see the right context when
1316 * resolving. That's why we don't replace the ppaddr. */
1318 static OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
1319 static OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
1321 static OP *a_ck_xslice(pTHX_ OP *o) {
1322 OP * (*old_ck)(pTHX_ OP *o) = 0;
1325 switch (o->op_type) {
1327 old_ck = a_old_ck_aslice;
1330 old_ck = a_old_ck_hslice;
1331 if (hint & A_HINT_DO)
1332 a_recheck_rv2xv(OpSIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv);
1335 o = old_ck(aTHX_ o);
1337 if (hint & A_HINT_DO) {
1338 a_map_store_root(o, 0, hint);
1345 /* ... ck_root (exists,delete,keys,values) ................................. */
1347 /* Those ops are only found at the root of a dereferencing expression. We can
1348 * then resolve at compile time if vivification must take place or not. */
1350 static OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
1351 static OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
1352 static OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
1353 static OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
1355 static OP *a_ck_root(pTHX_ OP *o) {
1356 OP * (*old_ck)(pTHX_ OP *o) = 0;
1357 OP * (*new_pp)(pTHX) = 0;
1358 bool enabled = FALSE;
1361 switch (o->op_type) {
1363 old_ck = a_old_ck_exists;
1364 new_pp = a_pp_root_binop;
1365 enabled = hint & A_HINT_EXISTS;
1368 old_ck = a_old_ck_delete;
1369 new_pp = a_pp_root_binop;
1370 enabled = hint & A_HINT_DELETE;
1373 old_ck = a_old_ck_keys;
1374 new_pp = a_pp_root_unop;
1375 enabled = hint & A_HINT_FETCH;
1378 old_ck = a_old_ck_values;
1379 new_pp = a_pp_root_unop;
1380 enabled = hint & A_HINT_FETCH;
1383 o = old_ck(aTHX_ o);
1385 if (hint & A_HINT_DO) {
1387 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
1388 a_map_store_root(o, o->op_ppaddr, hint);
1389 o->op_ppaddr = new_pp;
1399 /* ... Our peephole optimizer .............................................. */
1401 static void a_peep_rec(pTHX_ OP *o, ptable *seen);
1403 static void a_peep_rec(pTHX_ OP *o, ptable *seen) {
1404 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
1405 for (; o; o = o->op_next) {
1407 const a_op_info *oi = NULL;
1411 if (ptable_fetch(seen, o))
1413 ptable_seen_store(seen, o, o);
1416 switch (o->op_type) {
1422 if (ptable_fetch(seen, o))
1424 ptable_seen_store(seen, o, o);
1428 if (o->op_ppaddr != a_pp_deref) {
1429 oi = a_map_fetch(o);
1430 if (oi && (oi->flags & A_HINT_DO)) {
1431 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1432 o->op_ppaddr = a_pp_deref;
1440 if (o->op_ppaddr != a_pp_deref)
1442 oi = a_map_fetch(o);
1446 if (!(flags & A_HINT_DEREF)
1447 && (flags & A_HINT_DO)
1448 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1449 /* Decide if the expression must autovivify or not. */
1450 flags = a_map_resolve(o, oi);
1452 if (flags & A_HINT_DEREF)
1453 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1455 o->op_ppaddr = oi->old_pp;
1459 if ( o->op_ppaddr != a_pp_rv2av
1460 && o->op_ppaddr != a_pp_rv2hv
1461 && o->op_ppaddr != a_pp_rv2hv_simple)
1463 oi = a_map_fetch(o);
1466 if (!(oi->flags & A_HINT_DEREF))
1467 o->op_ppaddr = oi->old_pp;
1469 #if A_HAS_MULTIDEREF
1471 if (o->op_ppaddr != a_pp_multideref) {
1472 oi = a_map_fetch(cUNOPo->op_first);
1476 if (a_do_multideref(o, flags)) {
1477 a_map_store_root(o, o->op_ppaddr, flags & ~A_HINT_DEREF);
1478 o->op_ppaddr = a_pp_multideref;
1492 # if A_HAS_PERL(5, 10, 0)
1497 a_peep_rec(cLOGOPo->op_other);
1501 a_peep_rec(cLOOPo->op_redoop);
1502 a_peep_rec(cLOOPo->op_nextop);
1503 a_peep_rec(cLOOPo->op_lastop);
1505 # if A_HAS_PERL(5, 9, 5)
1507 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1513 a_peep_rec(cPMOPo->op_pmreplstart);
1516 #endif /* !A_HAS_RPEEP */
1523 static void a_peep(pTHX_ OP *o) {
1527 assert(a_is_loaded(&MY_CXT));
1529 MY_CXT.old_peep(aTHX_ o);
1533 ptable_seen_clear(seen);
1535 ptable_seen_clear(seen);
1539 /* --- Module setup/teardown ----------------------------------------------- */
1541 static void a_teardown(pTHX_ void *root) {
1546 if (a_clear_loaded_locked(&MY_CXT)) {
1547 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1548 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1550 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1551 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1552 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1554 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1555 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1557 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1558 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1560 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1561 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1562 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1563 a_ck_restore(OP_VALUES, &a_old_ck_values);
1565 ptable_map_free(a_op_map);
1571 if (MY_CXT.old_peep) {
1573 PL_rpeepp = MY_CXT.old_peep;
1575 PL_peepp = MY_CXT.old_peep;
1577 MY_CXT.old_peep = 0;
1580 ptable_seen_free(MY_CXT.seen);
1583 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1584 ptable_hints_free(MY_CXT.tbl);
1586 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1591 static void a_setup(pTHX) {
1592 #define a_setup() a_setup(aTHX)
1593 MY_CXT_INIT; /* Takes/release PL_my_ctx_mutex */
1597 if (a_set_loaded_locked(&MY_CXT)) {
1598 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1600 a_op_map = ptable_new();
1602 MUTEX_INIT(&a_op_map_mutex);
1605 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1606 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1608 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1609 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1610 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1612 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1613 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1615 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1616 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1618 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1619 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1620 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1621 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1629 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1630 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1631 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1632 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1633 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1634 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1635 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1636 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1637 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1638 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1642 if (PL_rpeepp != a_peep) {
1643 MY_CXT.old_peep = PL_rpeepp;
1647 if (PL_peepp != a_peep) {
1648 MY_CXT.old_peep = PL_peepp;
1653 MY_CXT.old_peep = 0;
1656 MY_CXT.seen = ptable_new();
1658 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1659 MY_CXT.tbl = ptable_new();
1660 MY_CXT.owner = aTHX;
1661 #endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1663 call_atexit(a_teardown, NULL);
1668 /* --- XS ------------------------------------------------------------------ */
1670 MODULE = autovivification PACKAGE = autovivification
1685 #if A_WORKAROUND_REQUIRE_PROPAGATION
1689 #if A_WORKAROUND_REQUIRE_PROPAGATION
1691 a_ptable_clone_ud ud;
1694 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1695 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1696 a_ptable_clone_ud_deinit(ud);
1701 #if A_WORKAROUND_REQUIRE_PROPAGATION
1703 MY_CXT.owner = aTHX;
1705 MY_CXT.seen = ptable_new();
1709 global_setup = a_set_loaded_locked(&MY_CXT);
1710 assert(!global_setup);
1716 #endif /* A_THREADSAFE */
1722 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1732 RETVAL = newSVuv(a_detag(tag));