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 UV a_do_multideref(const OP *o, UV flags) {
806 assert(o->op_type == OP_MULTIDEREF);
808 isexdel = o->op_private & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE);
811 if (isexdel & OPpMULTIDEREF_EXISTS) {
812 if (flags & A_HINT_EXISTS)
813 return A_HINT_EXISTS;
815 if (flags & A_HINT_DELETE)
816 return A_HINT_DELETE;
819 if (o->op_flags & OPf_MOD) {
820 if (flags & A_HINT_STORE)
823 if (flags & A_HINT_FETCH)
831 static SV *a_do_fake_pp(pTHX_ OP *op) {
832 #define a_do_fake_pp(O) a_do_fake_pp(aTHX_ (O))
838 PL_op->op_ppaddr(aTHX);
852 static void a_do_fake_pp_unop_init(pTHX_ UNOP *unop, U32 type, U32 flags) {
853 #define a_do_fake_pp_unop_init(O, T, F) a_do_fake_pp_unop_init(aTHX_ (O), (T), (F))
854 unop->op_type = type;
855 unop->op_flags = OPf_WANT_SCALAR | (~OPf_WANT & flags);
856 unop->op_private = 0;
857 unop->op_first = NULL;
858 unop->op_ppaddr = PL_ppaddr[type];
861 static SV *a_do_fake_pp_unop_arg1(pTHX_ U32 type, U32 flags, SV *arg) {
862 #define a_do_fake_pp_unop_arg1(T, F, A) a_do_fake_pp_unop_arg1(aTHX_ (T), (F), (A))
866 a_do_fake_pp_unop_init(&unop, type, flags);
872 return a_do_fake_pp(&unop);
875 static SV *a_do_fake_pp_unop_arg2(pTHX_ U32 type, U32 flags, SV *arg1, SV *arg2) {
876 #define a_do_fake_pp_unop_arg2(T, F, A1, A2) a_do_fake_pp_unop_arg2(aTHX_ (T), (F), (A1), (A2))
880 a_do_fake_pp_unop_init(&unop, type, flags);
887 return a_do_fake_pp(&unop);
890 #define a_do_pp_rv2av(R) a_do_fake_pp_unop_arg1(OP_RV2AV, OPf_REF, (R))
891 #define a_do_pp_afetch(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, 0, (A), (I))
892 #define a_do_pp_afetch_lv(A, I) a_do_fake_pp_unop_arg2(OP_AELEM, OPf_MOD, (A), (I))
893 #define a_do_pp_aexists(A, I) a_do_fake_pp_unop_arg2(OP_EXISTS, OPf_SPECIAL, (A), (I))
894 #define a_do_pp_adelete(A, I) a_do_fake_pp_unop_arg2(OP_DELETE, OPf_SPECIAL, (A), (I))
896 #define a_do_pp_rv2hv(R) a_do_fake_pp_unop_arg1(OP_RV2HV, OPf_REF, (R))
897 #define a_do_pp_hfetch(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, 0, (H), (K))
898 #define a_do_pp_hfetch_lv(H, K) a_do_fake_pp_unop_arg2(OP_HELEM, OPf_MOD, (H), (K))
899 #define a_do_pp_hexists(H, K) a_do_fake_pp_unop_arg2(OP_EXISTS, 0, (H), (K))
900 #define a_do_pp_hdelete(H, K) a_do_fake_pp_unop_arg2(OP_DELETE, 0, (H), (K))
902 static OP *a_pp_multideref(pTHX) {
903 UNOP_AUX_item *items;
912 const a_op_info *oi = a_map_fetch(PL_op);
915 deref = a_do_multideref(PL_op, flags);
917 return oi->old_pp(aTHX);
920 items = cUNOP_AUXx(PL_op)->op_aux;
923 PL_multideref_pc = items;
926 switch (actions & MDEREF_ACTION_MASK) {
928 actions = (++items)->uv;
930 case MDEREF_AV_padav_aelem: /* $lex[...] */
931 sv = PAD_SVl((++items)->pad_offset);
935 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
936 sv = UNOP_AUX_item_sv(++items);
937 assert(isGV_with_GP(sv));
938 sv = (SV *) GvAVn((GV *) sv);
942 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
946 goto do_AV_rv2av_aelem;
947 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
948 sv = UNOP_AUX_item_sv(++items);
949 assert(isGV_with_GP(sv));
950 sv = GvSVn((GV *) sv);
953 goto do_AV_vivify_rv2av_aelem;
954 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
955 sv = PAD_SVl((++items)->pad_offset);
957 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
960 do_AV_vivify_rv2av_aelem:
961 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_AV);
963 sv = a_do_pp_rv2av(sv);
967 assert(SvTYPE(sv) == SVt_PVAV);
968 switch (actions & MDEREF_INDEX_MASK) {
969 case MDEREF_INDEX_none:
971 case MDEREF_INDEX_const:
972 esv = sv_2mortal(newSViv((++items)->iv));
974 case MDEREF_INDEX_padsv:
975 esv = PAD_SVl((++items)->pad_offset);
977 case MDEREF_INDEX_gvsv:
978 esv = UNOP_AUX_item_sv(++items);
979 assert(isGV_with_GP(esv));
980 esv = GvSVn((GV *) esv);
982 if (UNLIKELY(SvROK(esv) && !SvGAMAGIC(esv) && ckWARN(WARN_MISC)))
983 Perl_warner(aTHX_ packWARN(WARN_MISC),
984 "Use of reference \"%"SVf"\" as array index",
988 PL_multideref_pc = items;
989 if (actions & MDEREF_FLAG_last) {
992 sv = a_do_pp_afetch(sv, esv);
995 sv = a_do_pp_afetch_lv(sv, esv);
998 sv = a_do_pp_aexists(sv, esv);
1001 sv = a_do_pp_adelete(sv, esv);
1006 sv = a_do_pp_afetch(sv, esv);
1009 case MDEREF_HV_padhv_helem: /* $lex{...} */
1010 sv = PAD_SVl((++items)->pad_offset);
1014 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
1015 sv = UNOP_AUX_item_sv(++items);
1016 assert(isGV_with_GP(sv));
1017 sv = (SV *) GvHVn((GV *) sv);
1021 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
1025 goto do_HV_rv2hv_helem;
1026 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
1027 sv = UNOP_AUX_item_sv(++items);
1028 assert(isGV_with_GP(sv));
1029 sv = GvSVn((GV *) sv);
1032 goto do_HV_vivify_rv2hv_helem;
1033 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
1034 sv = PAD_SVl((++items)->pad_offset);
1036 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
1039 do_HV_vivify_rv2hv_helem:
1040 sv = Perl_vivify_ref(aTHX_ sv, OPpDEREF_HV);
1042 sv = a_do_pp_rv2hv(sv);
1046 assert(SvTYPE(sv) == SVt_PVHV);
1047 switch (actions & MDEREF_INDEX_MASK) {
1048 case MDEREF_INDEX_none:
1050 case MDEREF_INDEX_const:
1051 key = UNOP_AUX_item_sv(++items);
1053 case MDEREF_INDEX_padsv:
1054 key = PAD_SVl((++items)->pad_offset);
1056 case MDEREF_INDEX_gvsv:
1057 key = UNOP_AUX_item_sv(++items);
1058 assert(isGV_with_GP(key));
1059 key = GvSVn((GV *) key);
1062 PL_multideref_pc = items;
1063 if (actions & MDEREF_FLAG_last) {
1066 sv = a_do_pp_hfetch(sv, key);
1069 sv = a_do_pp_hfetch_lv(sv, key);
1072 sv = a_do_pp_hexists(sv, key);
1075 sv = a_do_pp_hdelete(sv, key);
1082 sv = a_do_pp_hfetch(sv, key);
1087 actions >>= MDEREF_SHIFT;
1091 if ((flags & A_HINT_NOTIFY) || (deref == A_HINT_STORE))
1092 a_cannot_vivify(flags);
1093 if (deref == A_HINT_EXISTS)
1102 #endif /* A_HAS_MULTIDEREF */
1104 /* --- Check functions ----------------------------------------------------- */
1106 static void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
1107 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
1109 if (o->op_type == type && o->op_ppaddr != new_pp
1110 && cUNOPo->op_first->op_type != OP_GV) {
1112 const a_op_info *oi = a_map_fetch(o);
1114 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1115 o->op_ppaddr = new_pp;
1122 /* ... ck_pad{any,sv} ...................................................... */
1124 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
1125 * function, but are instead manually mutated from a padany. So we store
1126 * the op entry in the op map in the padany check function, and we set their
1127 * op_ppaddr member in our peephole optimizer replacement below. */
1129 static OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
1131 static OP *a_ck_padany(pTHX_ OP *o) {
1134 o = a_old_ck_padany(aTHX_ o);
1137 if (hint & A_HINT_DO)
1138 a_map_store_root(o, o->op_ppaddr, hint);
1145 static OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
1147 static OP *a_ck_padsv(pTHX_ OP *o) {
1150 o = a_old_ck_padsv(aTHX_ o);
1153 if (hint & A_HINT_DO) {
1154 a_map_store_root(o, o->op_ppaddr, hint);
1155 o->op_ppaddr = a_pp_deref;
1162 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
1164 /* Those ops appear both at the root and inside an expression but there's no
1165 * way to distinguish both situations. Worse, we can't even know if we are in a
1166 * modifying context, so the expression can't be resolved yet. It will be at the
1167 * first invocation of a_pp_deref() for this expression. */
1169 static OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
1170 static OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
1171 static OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
1173 static OP *a_ck_deref(pTHX_ OP *o) {
1174 OP * (*old_ck)(pTHX_ OP *o) = 0;
1177 switch (o->op_type) {
1179 old_ck = a_old_ck_aelem;
1180 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1181 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
1184 old_ck = a_old_ck_helem;
1185 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
1186 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
1189 old_ck = a_old_ck_rv2sv;
1192 o = old_ck(aTHX_ o);
1194 if (hint & A_HINT_DO) {
1195 #if A_HAS_MULTIDEREF
1196 if (old_ck == a_old_ck_rv2sv && o->op_flags & OPf_KIDS) {
1197 OP *kid = cUNOPo->op_first;
1198 if (kid && kid->op_type == OP_GV)
1199 a_map_store(kid, kid->op_ppaddr, NULL, hint);
1202 a_map_store_root(o, o->op_ppaddr, hint);
1203 o->op_ppaddr = a_pp_deref;
1210 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
1212 /* Those ops also appear both inisde and at the root, hence the caveats for
1213 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
1214 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
1217 static OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
1218 static OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
1220 static OP *a_ck_rv2xv(pTHX_ OP *o) {
1221 OP * (*old_ck)(pTHX_ OP *o) = 0;
1222 OP * (*new_pp)(pTHX) = 0;
1225 switch (o->op_type) {
1226 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
1227 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
1229 o = old_ck(aTHX_ o);
1231 if (cUNOPo->op_first->op_type == OP_GV)
1235 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
1236 a_map_store_root(o, o->op_ppaddr, hint);
1237 o->op_ppaddr = new_pp;
1244 /* ... ck_xslice (aslice,hslice) ........................................... */
1246 /* I think those are only found at the root, but there's nothing that really
1247 * prevent them to be inside the expression too. We only need to update the
1248 * root so that the rest of the expression will see the right context when
1249 * resolving. That's why we don't replace the ppaddr. */
1251 static OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
1252 static OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
1254 static OP *a_ck_xslice(pTHX_ OP *o) {
1255 OP * (*old_ck)(pTHX_ OP *o) = 0;
1258 switch (o->op_type) {
1260 old_ck = a_old_ck_aslice;
1263 old_ck = a_old_ck_hslice;
1264 if (hint & A_HINT_DO)
1265 a_recheck_rv2xv(OpSIBLING(cUNOPo->op_first), OP_RV2HV, a_pp_rv2hv);
1268 o = old_ck(aTHX_ o);
1270 if (hint & A_HINT_DO) {
1271 a_map_store_root(o, 0, hint);
1278 /* ... ck_root (exists,delete,keys,values) ................................. */
1280 /* Those ops are only found at the root of a dereferencing expression. We can
1281 * then resolve at compile time if vivification must take place or not. */
1283 static OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
1284 static OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
1285 static OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
1286 static OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
1288 static OP *a_ck_root(pTHX_ OP *o) {
1289 OP * (*old_ck)(pTHX_ OP *o) = 0;
1290 OP * (*new_pp)(pTHX) = 0;
1291 bool enabled = FALSE;
1294 switch (o->op_type) {
1296 old_ck = a_old_ck_exists;
1297 new_pp = a_pp_root_binop;
1298 enabled = hint & A_HINT_EXISTS;
1301 old_ck = a_old_ck_delete;
1302 new_pp = a_pp_root_binop;
1303 enabled = hint & A_HINT_DELETE;
1306 old_ck = a_old_ck_keys;
1307 new_pp = a_pp_root_unop;
1308 enabled = hint & A_HINT_FETCH;
1311 old_ck = a_old_ck_values;
1312 new_pp = a_pp_root_unop;
1313 enabled = hint & A_HINT_FETCH;
1316 o = old_ck(aTHX_ o);
1318 if (hint & A_HINT_DO) {
1320 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
1321 a_map_store_root(o, o->op_ppaddr, hint);
1322 o->op_ppaddr = new_pp;
1332 /* ... Our peephole optimizer .............................................. */
1334 static peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
1336 static void a_peep_rec(pTHX_ OP *o, ptable *seen);
1338 static void a_peep_rec(pTHX_ OP *o, ptable *seen) {
1339 #define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
1340 for (; o; o = o->op_next) {
1342 const a_op_info *oi = NULL;
1346 if (ptable_fetch(seen, o))
1348 ptable_seen_store(seen, o, o);
1351 switch (o->op_type) {
1357 if (ptable_fetch(seen, o))
1359 ptable_seen_store(seen, o, o);
1363 if (o->op_ppaddr != a_pp_deref) {
1364 oi = a_map_fetch(o);
1365 if (oi && (oi->flags & A_HINT_DO)) {
1366 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
1367 o->op_ppaddr = a_pp_deref;
1375 if (o->op_ppaddr != a_pp_deref)
1377 oi = a_map_fetch(o);
1381 if (!(flags & A_HINT_DEREF)
1382 && (flags & A_HINT_DO)
1383 && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
1384 /* Decide if the expression must autovivify or not. */
1385 flags = a_map_resolve(o, oi);
1387 if (flags & A_HINT_DEREF)
1388 o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
1390 o->op_ppaddr = oi->old_pp;
1394 if ( o->op_ppaddr != a_pp_rv2av
1395 && o->op_ppaddr != a_pp_rv2hv
1396 && o->op_ppaddr != a_pp_rv2hv_simple)
1398 oi = a_map_fetch(o);
1401 if (!(oi->flags & A_HINT_DEREF))
1402 o->op_ppaddr = oi->old_pp;
1404 #if A_HAS_MULTIDEREF
1406 if (o->op_ppaddr != a_pp_multideref) {
1407 oi = a_map_fetch(cUNOPo->op_first);
1411 if (a_do_multideref(o, flags)) {
1412 a_map_store_root(o, o->op_ppaddr, flags & ~A_HINT_DEREF);
1413 o->op_ppaddr = a_pp_multideref;
1427 # if A_HAS_PERL(5, 10, 0)
1432 a_peep_rec(cLOGOPo->op_other);
1436 a_peep_rec(cLOOPo->op_redoop);
1437 a_peep_rec(cLOOPo->op_nextop);
1438 a_peep_rec(cLOOPo->op_lastop);
1440 # if A_HAS_PERL(5, 9, 5)
1442 a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
1448 a_peep_rec(cPMOPo->op_pmreplstart);
1451 #endif /* !A_HAS_RPEEP */
1458 static void a_peep(pTHX_ OP *o) {
1460 ptable *seen = MY_CXT.seen;
1462 a_old_peep(aTHX_ o);
1465 ptable_seen_clear(seen);
1467 ptable_seen_clear(seen);
1471 /* --- Interpreter setup/teardown ------------------------------------------ */
1473 static U32 a_initialized = 0;
1475 static void a_teardown(pTHX_ void *root) {
1487 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1488 ptable_hints_free(MY_CXT.tbl);
1490 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1491 ptable_seen_free(MY_CXT.seen);
1495 a_ck_restore(OP_PADANY, &a_old_ck_padany);
1496 a_ck_restore(OP_PADSV, &a_old_ck_padsv);
1498 a_ck_restore(OP_AELEM, &a_old_ck_aelem);
1499 a_ck_restore(OP_HELEM, &a_old_ck_helem);
1500 a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
1502 a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
1503 a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
1505 a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
1506 a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
1508 a_ck_restore(OP_EXISTS, &a_old_ck_exists);
1509 a_ck_restore(OP_DELETE, &a_old_ck_delete);
1510 a_ck_restore(OP_KEYS, &a_old_ck_keys);
1511 a_ck_restore(OP_VALUES, &a_old_ck_values);
1514 PL_rpeepp = a_old_peep;
1516 PL_peepp = a_old_peep;
1523 static void a_setup(pTHX) {
1524 #define a_setup() a_setup(aTHX)
1530 # if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1531 MY_CXT.tbl = ptable_new();
1532 MY_CXT.owner = aTHX;
1533 # endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
1534 MY_CXT.seen = ptable_new();
1537 a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
1538 a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
1540 a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
1541 a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
1542 a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
1544 a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
1545 a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
1547 a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
1548 a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
1550 a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
1551 a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
1552 a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
1553 a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
1556 a_old_peep = PL_rpeepp;
1559 a_old_peep = PL_peepp;
1564 call_atexit(a_teardown, aTHX);
1566 call_atexit(a_teardown, NULL);
1572 static U32 a_booted = 0;
1574 /* --- XS ------------------------------------------------------------------ */
1576 MODULE = autovivification PACKAGE = autovivification
1585 a_op_map = ptable_new();
1587 MUTEX_INIT(&a_op_map_mutex);
1590 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1592 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1593 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1594 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1595 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1596 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1597 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1598 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1599 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1600 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1601 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1613 #if A_WORKAROUND_REQUIRE_PROPAGATION
1620 #if A_WORKAROUND_REQUIRE_PROPAGATION
1623 a_ptable_clone_ud ud;
1626 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1627 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1628 a_ptable_clone_ud_deinit(ud);
1635 #if A_WORKAROUND_REQUIRE_PROPAGATION
1637 MY_CXT.owner = aTHX;
1641 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1647 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1649 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &a_endav_vtbl, NULL, 0);
1654 _THREAD_CLEANUP(...)
1657 a_thread_cleanup(aTHX_ NULL);
1660 #endif /* A_THREADSAFE */
1666 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1676 RETVAL = newSVuv(a_detag(tag));