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_MUTEX_LOCK OP_CHECK_MUTEX_LOCK
97 # define A_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
99 # define A_CHECK_MUTEX_LOCK OP_REFCNT_LOCK
100 # define A_CHECK_MUTEX_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;
118 A_CHECK_MUTEX_UNLOCK;
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;
130 A_CHECK_MUTEX_UNLOCK;
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 static SV *a_do_fake_pp(pTHX_ OP *op) {
804 #define a_do_fake_pp(O) a_do_fake_pp(aTHX_ (O))
810 PL_op->op_ppaddr(aTHX);
824 static void a_do_fake_pp_unop_init(pTHX_ UNOP *unop, U32 type, U32 flags) {
825 #define a_do_fake_pp_unop_init(O, T, F) a_do_fake_pp_unop_init(aTHX_ (O), (T), (F))
826 unop->op_type = type;
827 unop->op_flags = OPf_WANT_SCALAR | (~OPf_WANT & flags);
828 unop->op_private = 0;
829 unop->op_first = NULL;
830 unop->op_ppaddr = PL_ppaddr[type];
833 static SV *a_do_fake_pp_unop_arg1(pTHX_ U32 type, U32 flags, SV *arg) {
834 #define a_do_fake_pp_unop_arg1(T, F, A) a_do_fake_pp_unop_arg1(aTHX_ (T), (F), (A))
838 a_do_fake_pp_unop_init(&unop, type, flags);
844 return a_do_fake_pp(&unop);
847 static SV *a_do_fake_pp_unop_arg2(pTHX_ U32 type, U32 flags, SV *arg1, SV *arg2) {
848 #define a_do_fake_pp_unop_arg2(T, F, A1, A2) a_do_fake_pp_unop_arg2(aTHX_ (T), (F), (A1), (A2))
852 a_do_fake_pp_unop_init(&unop, type, flags);
859 return a_do_fake_pp(&unop);
862 #define a_do_pp_rv2av(R) a_do_fake_pp_unop_arg1(OP_RV2AV, OPf_REF, (R))
863 #define a_do_pp_aexists(A, I) a_do_fake_pp_unop_arg2(OP_EXISTS, OPf_SPECIAL, (A), (I))
864 #define a_do_pp_adelete(A, I) a_do_fake_pp_unop_arg2(OP_DELETE, OPf_SPECIAL, (A), (I))
865 #define a_do_pp_aelem(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, 0, (A), (I))
867 #define a_do_pp_rv2hv(R) a_do_fake_pp_unop_arg1(OP_RV2HV, OPf_REF, (R))
868 #define a_do_pp_hexists(H, K) a_do_fake_pp_unop_arg2(OP_EXISTS, 0, (H), (K))
869 #define a_do_pp_hdelete(H, K) a_do_fake_pp_unop_arg2(OP_DELETE, 0, (H), (K))
870 #define a_do_pp_helem(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, 0, (H), (K))
872 static OP *a_pp_multideref(pTHX) {
873 UNOP_AUX_item *items = cUNOP_AUXx(PL_op)->op_aux;
874 UV actions = items->uv;
875 U8 isexdel = PL_op->op_private & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE);
882 const a_op_info *oi = a_map_fetch(PL_op);
885 if (isexdel & OPpMULTIDEREF_EXISTS) {
886 if (flags & A_HINT_EXISTS)
888 } else if (flags & A_HINT_DELETE) {
889 assert(isexdel & OPpMULTIDEREF_DELETE);
892 } else if (flags & A_HINT_FETCH) {
895 return oi->old_pp(aTHX);
899 PL_multideref_pc = items;
902 switch (actions & MDEREF_ACTION_MASK) {
904 actions = (++items)->uv;
906 case MDEREF_AV_padav_aelem: /* $lex[...] */
907 sv = PAD_SVl((++items)->pad_offset);
911 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
912 sv = UNOP_AUX_item_sv(++items);
913 assert(isGV_with_GP(sv));
914 sv = (SV *) GvAVn((GV *) sv);
918 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
922 goto do_AV_rv2av_aelem;
923 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
924 sv = UNOP_AUX_item_sv(++items);
925 assert(isGV_with_GP(sv));
926 sv = GvSVn((GV *) sv);
929 goto do_AV_vivify_rv2av_aelem;
930 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
931 sv = PAD_SVl((++items)->pad_offset);
935 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
936 do_AV_vivify_rv2av_aelem:
937 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_AV);
939 sv = a_do_pp_rv2av(sv);
943 assert(SvTYPE(sv) == SVt_PVAV);
944 switch (actions & MDEREF_INDEX_MASK) {
945 case MDEREF_INDEX_none:
947 case MDEREF_INDEX_const:
948 esv = sv_2mortal(newSViv((++items)->iv));
950 case MDEREF_INDEX_padsv:
951 esv = PAD_SVl((++items)->pad_offset);
953 case MDEREF_INDEX_gvsv:
954 esv = UNOP_AUX_item_sv(++items);
955 assert(isGV_with_GP(esv));
956 esv = GvSVn((GV *) esv);
958 if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC)))
959 Perl_warner(aTHX_ packWARN(WARN_MISC),
960 "Use of reference \"%"SVf"\" as array index",
964 PL_multideref_pc = items;
965 if (actions & MDEREF_FLAG_last) {
967 if (isexdel & OPpMULTIDEREF_EXISTS)
968 sv = a_do_pp_aexists(sv, esv);
970 sv = a_do_pp_adelete(sv, esv);
972 sv = a_do_pp_aelem(sv, esv);
976 sv = a_do_pp_aelem(sv, esv);
979 case MDEREF_HV_padhv_helem: /* $lex{...} */
980 sv = PAD_SVl((++items)->pad_offset);
984 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
985 sv = UNOP_AUX_item_sv(++items);
986 assert(isGV_with_GP(sv));
987 sv = (SV *) GvHVn((GV *) sv);
991 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
995 goto do_HV_rv2hv_helem;
996 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
997 sv = UNOP_AUX_item_sv(++items);
998 assert(isGV_with_GP(sv));
999 sv = GvSVn((GV *) sv);
1002 goto do_HV_vivify_rv2hv_helem;
1003 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
1004 sv = PAD_SVl((++items)->pad_offset);
1008 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
1009 do_HV_vivify_rv2hv_helem:
1010 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_HV);
1012 sv = a_do_pp_rv2hv(sv);
1016 assert(SvTYPE(sv) == SVt_PVHV);
1017 switch (actions & MDEREF_INDEX_MASK) {
1018 case MDEREF_INDEX_none:
1020 case MDEREF_INDEX_const:
1021 key = UNOP_AUX_item_sv(++items);
1023 case MDEREF_INDEX_padsv:
1024 key = PAD_SVl((++items)->pad_offset);
1026 case MDEREF_INDEX_gvsv:
1027 key = UNOP_AUX_item_sv(++items);
1028 assert(isGV_with_GP(key));
1029 key = GvSVn((GV *) key);
1032 PL_multideref_pc = items;
1033 if (actions & MDEREF_FLAG_last) {
1035 if (isexdel & OPpMULTIDEREF_EXISTS)
1036 sv = a_do_pp_hexists(sv, key);
1038 sv = a_do_pp_hdelete(sv, key);
1040 sv = a_do_pp_helem(sv, key);
1044 sv = a_do_pp_helem(sv, key);
1049 actions >>= MDEREF_SHIFT;
1053 if (flags & (A_HINT_NOTIFY|A_HINT_STORE))
1054 a_cannot_vivify(flags);
1055 if (isexdel & OPpMULTIDEREF_EXISTS)
1064 #endif /* A_HAS_MULTIDEREF */
1066 /* --- Check functions ----------------------------------------------------- */
1068 static void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
1069 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
1071 if (o->op_type == type && o->op_ppaddr != new_pp
1072 && cUNOPo->op_first->op_type != OP_GV) {
1074 const a_op_info *oi = a_map_fetch(o);
1076 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1077 o->op_ppaddr = new_pp;
1084 /* ... ck_pad{any,sv} ...................................................... */
1086 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
1087 * function, but are instead manually mutated from a padany. So we store
1088 * the op entry in the op map in the padany check function, and we set their
1089 * op_ppaddr member in our peephole optimizer replacement below. */
1091 static OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
1093 static OP *a_ck_padany(pTHX_ OP *o) {
1096 o = a_old_ck_padany(aTHX_ o);
1099 if (hint & A_HINT_DO)
1100 a_map_store_root(o, o->op_ppaddr, hint);
1107 static OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
1109 static OP *a_ck_padsv(pTHX_ OP *o) {
1112 o = a_old_ck_padsv(aTHX_ o);
1115 if (hint & A_HINT_DO) {
1116 a_map_store_root(o, o->op_ppaddr, hint);
1117 o->op_ppaddr = a_pp_deref;
1124 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
1126 /* Those ops appear both at the root and inside an expression but there's no
1127 * way to distinguish both situations. Worse, we can't even know if we are in a
1128 * modifying context, so the expression can't be resolved yet. It will be at the
1129 * first invocation of a_pp_deref() for this expression. */
1131 static OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
1132 static OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
1133 static OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
1135 static OP *a_ck_deref(pTHX_ OP *o) {
1136 OP * (*old_ck)(pTHX_ OP *o) = 0;
1139 switch (o->op_type) {
1141 old_ck = a_old_ck_aelem;
1142 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1143 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
1146 old_ck = a_old_ck_helem;
1147 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1148 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
1151 old_ck = a_old_ck_rv2sv;
1154 o = old_ck(aTHX_ o);
1156 if (hint & A_HINT_DO) {
1157 #if A_HAS_MULTIDEREF
1158 if (old_ck == a_old_ck_rv2sv && o->op_flags & OPf_KIDS) {
1159 OP *kid = cUNOPo->op_first;
1160 if (kid && kid->op_type == OP_GV)
1161 a_map_store(kid, kid->op_ppaddr, NULL, hint);
1164 a_map_store_root(o, o->op_ppaddr, hint);
1165 o->op_ppaddr = a_pp_deref;
1172 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
1174 /* Those ops also appear both inisde and at the root, hence the caveats for
1175 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
1176 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
1179 static OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
1180 static OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
1182 static OP *a_ck_rv2xv(pTHX_ OP *o) {
1183 OP * (*old_ck)(pTHX_ OP *o) = 0;
1184 OP * (*new_pp)(pTHX) = 0;
1187 switch (o->op_type) {
1188 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
1189 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
1191 o = old_ck(aTHX_ o);
1193 if (cUNOPo->op_first->op_type == OP_GV)
1197 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
1198 a_map_store_root(o, o->op_ppaddr, hint);
1199 o->op_ppaddr = new_pp;
1206 /* ... ck_xslice (aslice,hslice) ........................................... */
1208 /* I think those are only found at the root, but there's nothing that really
1209 * prevent them to be inside the expression too. We only need to update the
1210 * root so that the rest of the expression will see the right context when
1211 * resolving. That's why we don't replace the ppaddr. */
1213 static OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
1214 static OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
1216 static OP *a_ck_xslice(pTHX_ OP *o) {
1217 OP * (*old_ck)(pTHX_ OP *o) = 0;
1220 switch (o->op_type) {
1222 old_ck = a_old_ck_aslice;
1225 old_ck = a_old_ck_hslice;
1226 if (hint & A_HINT_DO)
1227 a_recheck_rv2xv(OpSIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv);
1230 o = old_ck(aTHX_ o);
1232 if (hint & A_HINT_DO) {
1233 a_map_store_root(o, 0, hint);
1240 /* ... ck_root (exists,delete,keys,values) ................................. */
1242 /* Those ops are only found at the root of a dereferencing expression. We can
1243 * then resolve at compile time if vivification must take place or not. */
1245 static OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
1246 static OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
1247 static OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
1248 static OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
1250 static OP *a_ck_root(pTHX_ OP *o) {
1251 OP * (*old_ck)(pTHX_ OP *o) = 0;
1252 OP * (*new_pp)(pTHX) = 0;
1253 bool enabled = FALSE;
1256 switch (o->op_type) {
1258 old_ck = a_old_ck_exists;
1259 new_pp = a_pp_root_binop;
1260 enabled = hint & A_HINT_EXISTS;
1263 old_ck = a_old_ck_delete;
1264 new_pp = a_pp_root_binop;
1265 enabled = hint & A_HINT_DELETE;
1268 old_ck = a_old_ck_keys;
1269 new_pp = a_pp_root_unop;
1270 enabled = hint & A_HINT_FETCH;
1273 old_ck = a_old_ck_values;
1274 new_pp = a_pp_root_unop;
1275 enabled = hint & A_HINT_FETCH;
1278 o = old_ck(aTHX_ o);
1280 if (hint & A_HINT_DO) {
1282 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
1283 a_map_store_root(o, o->op_ppaddr, hint);
1284 o->op_ppaddr = new_pp;
1294 /* ... Our peephole optimizer .............................................. */
1296 static peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
1298 static void a_peep_rec(pTHX_ OP *o, ptable *seen);
1300 static void a_peep_rec(pTHX_ OP *o, ptable *seen) {
1301 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
1302 for (; o; o = o->op_next) {
1304 const a_op_info *oi = NULL;
1308 if (ptable_fetch(seen, o))
1310 ptable_seen_store(seen, o, o);
1313 switch (o->op_type) {
1319 if (ptable_fetch(seen, o))
1321 ptable_seen_store(seen, o, o);
1325 if (o->op_ppaddr != a_pp_deref) {
1326 oi = a_map_fetch(o);
1327 if (oi && (oi->flags & A_HINT_DO)) {
1328 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1329 o->op_ppaddr = a_pp_deref;
1337 if (o->op_ppaddr != a_pp_deref)
1339 oi = a_map_fetch(o);
1343 if (!(flags & A_HINT_DEREF)
1344 && (flags & A_HINT_DO)
1345 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1346 /* Decide if the expression must autovivify or not. */
1347 flags = a_map_resolve(o, oi);
1349 if (flags & A_HINT_DEREF)
1350 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1352 o->op_ppaddr = oi->old_pp;
1356 if ( o->op_ppaddr != a_pp_rv2av
1357 && o->op_ppaddr != a_pp_rv2hv
1358 && o->op_ppaddr != a_pp_rv2hv_simple)
1360 oi = a_map_fetch(o);
1363 if (!(oi->flags & A_HINT_DEREF))
1364 o->op_ppaddr = oi->old_pp;
1366 #if A_HAS_MULTIDEREF
1368 if (o->op_ppaddr != a_pp_multideref) {
1370 oi = a_map_fetch(cUNOPo->op_first);
1374 isexdel = o->op_private & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE);
1375 if ( ((flags & A_HINT_FETCH) && !isexdel)
1376 || (flags & A_HINT_EXISTS) && (isexdel & OPpMULTIDEREF_EXISTS)
1377 || (flags & A_HINT_DELETE) && (isexdel & OPpMULTIDEREF_DELETE)) {
1378 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1379 o->op_ppaddr = a_pp_multideref;
1393 # if A_HAS_PERL(5, 10, 0)
1398 a_peep_rec(cLOGOPo->op_other);
1402 a_peep_rec(cLOOPo->op_redoop);
1403 a_peep_rec(cLOOPo->op_nextop);
1404 a_peep_rec(cLOOPo->op_lastop);
1406 # if A_HAS_PERL(5, 9, 5)
1408 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1414 a_peep_rec(cPMOPo->op_pmreplstart);
1417 #endif /* !A_HAS_RPEEP */
1424 static void a_peep(pTHX_ OP *o) {
1426 ptable *seen = MY_CXT.seen;
1428 a_old_peep(aTHX_ o);
1431 ptable_seen_clear(seen);
1433 ptable_seen_clear(seen);
1437 /* --- Interpreter setup/teardown ------------------------------------------ */
1439 static U32 a_initialized = 0;
1441 static void a_teardown(pTHX_ void *root) {
1453 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1454 ptable_hints_free(MY_CXT.tbl);
1456 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1457 ptable_seen_free(MY_CXT.seen);
1461 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1462 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1464 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1465 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1466 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1468 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1469 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1471 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1472 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1474 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1475 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1476 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1477 a_ck_restore(OP_VALUES, &a_old_ck_values);
1480 PL_rpeepp = a_old_peep;
1482 PL_peepp = a_old_peep;
1489 static void a_setup(pTHX) {
1490 #define a_setup() a_setup(aTHX)
1496 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1497 MY_CXT.tbl = ptable_new();
1498 MY_CXT.owner = aTHX;
1499 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1500 MY_CXT.seen = ptable_new();
1503 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1504 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1506 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1507 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1508 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1510 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1511 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1513 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1514 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1516 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1517 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1518 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1519 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1522 a_old_peep = PL_rpeepp;
1525 a_old_peep = PL_peepp;
1530 call_atexit(a_teardown, aTHX);
1532 call_atexit(a_teardown, NULL);
1538 static U32 a_booted = 0;
1540 /* --- XS ------------------------------------------------------------------ */
1542 MODULE = autovivification PACKAGE = autovivification
1551 a_op_map = ptable_new();
1553 MUTEX_INIT(&a_op_map_mutex);
1556 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1558 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1559 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1560 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1561 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1562 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1563 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1564 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1565 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1566 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1567 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1579 #if A_WORKAROUND_REQUIRE_PROPAGATION
1586 #if A_WORKAROUND_REQUIRE_PROPAGATION
1589 a_ptable_clone_ud ud;
1592 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1593 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1594 a_ptable_clone_ud_deinit(ud);
1601 #if A_WORKAROUND_REQUIRE_PROPAGATION
1603 MY_CXT.owner = aTHX;
1607 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1613 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1615 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &a_endav_vtbl, NULL, 0);
1620 _THREAD_CLEANUP(...)
1623 a_thread_cleanup(aTHX_ NULL);
1626 #endif /* A_THREADSAFE */
1632 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1642 RETVAL = newSVuv(a_detag(tag));