+/* ... Our peephole optimizer .............................................. */
+
+static peep_t a_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
+
+static void a_peep_rec(pTHX_ OP *o, ptable *seen);
+
+static void a_peep_rec(pTHX_ OP *o, ptable *seen) {
+#define a_peep_rec(O) a_peep_rec(aTHX_ (O), seen)
+ for (; o; o = o->op_next) {
+ dA_MAP_THX;
+ const a_op_info *oi = NULL;
+ UV flags = 0;
+
+#if !A_HAS_RPEEP
+ if (ptable_fetch(seen, o))
+ break;
+ ptable_seen_store(seen, o, o);
+#endif
+
+ switch (o->op_type) {
+#if A_HAS_RPEEP
+ case OP_NEXTSTATE:
+ case OP_DBSTATE:
+ case OP_STUB:
+ case OP_UNSTACK:
+ if (ptable_fetch(seen, o))
+ return;
+ ptable_seen_store(seen, o, o);
+ break;
+#endif
+ case OP_PADSV:
+ if (o->op_ppaddr != a_pp_deref) {
+ oi = a_map_fetch(o);
+ if (oi && (oi->flags & A_HINT_DO)) {
+ a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
+ o->op_ppaddr = a_pp_deref;
+ }
+ }
+ /* FALLTHROUGH */
+ case OP_AELEM:
+ case OP_AELEMFAST:
+ case OP_HELEM:
+ case OP_RV2SV:
+ if (o->op_ppaddr != a_pp_deref)
+ break;
+ oi = a_map_fetch(o);
+ if (!oi)
+ break;
+ flags = oi->flags;
+ if (!(flags & A_HINT_DEREF)
+ && (flags & A_HINT_DO)
+ && (o->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
+ /* Decide if the expression must autovivify or not. */
+ flags = a_map_resolve(o, oi);
+ }
+ if (flags & A_HINT_DEREF)
+ o->op_private = ((o->op_private & ~OPpDEREF) | OPpLVAL_DEFER);
+ else
+ o->op_ppaddr = oi->old_pp;
+ break;
+ case OP_RV2AV:
+ case OP_RV2HV:
+ if ( o->op_ppaddr != a_pp_rv2av
+ && o->op_ppaddr != a_pp_rv2hv
+ && o->op_ppaddr != a_pp_rv2hv_simple)
+ break;
+ oi = a_map_fetch(o);
+ if (!oi)
+ break;
+ if (!(oi->flags & A_HINT_DEREF))
+ o->op_ppaddr = oi->old_pp;
+ break;
+#if !A_HAS_RPEEP
+ case OP_MAPWHILE:
+ case OP_GREPWHILE:
+ case OP_AND:
+ case OP_OR:
+ case OP_ANDASSIGN:
+ case OP_ORASSIGN:
+ case OP_COND_EXPR:
+ case OP_RANGE:
+# if A_HAS_PERL(5, 10, 0)
+ case OP_ONCE:
+ case OP_DOR:
+ case OP_DORASSIGN:
+# endif
+ a_peep_rec(cLOGOPo->op_other);
+ break;
+ case OP_ENTERLOOP:
+ case OP_ENTERITER:
+ a_peep_rec(cLOOPo->op_redoop);
+ a_peep_rec(cLOOPo->op_nextop);
+ a_peep_rec(cLOOPo->op_lastop);
+ break;
+# if A_HAS_PERL(5, 9, 5)
+ case OP_SUBST:
+ a_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
+ break;
+# else
+ case OP_QR:
+ case OP_MATCH:
+ case OP_SUBST:
+ a_peep_rec(cPMOPo->op_pmreplstart);
+ break;
+# endif
+#endif /* !A_HAS_RPEEP */
+ default:
+ break;
+ }
+ }
+}
+
+static void a_peep(pTHX_ OP *o) {
+ dMY_CXT;
+ ptable *seen = MY_CXT.seen;
+
+ a_old_peep(aTHX_ o);
+
+ if (seen) {
+ ptable_seen_clear(seen);
+ a_peep_rec(o);
+ ptable_seen_clear(seen);
+ }
+}
+
+/* --- Interpreter setup/teardown ------------------------------------------ */
+
+static U32 a_initialized = 0;
+
+static void a_teardown(pTHX_ void *root) {
+
+ if (!a_initialized)
+ return;
+
+#if A_MULTIPLICITY
+ if (aTHX != root)
+ return;
+#endif
+
+ {
+ dMY_CXT;
+# if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
+ ptable_hints_free(MY_CXT.tbl);
+ MY_CXT.tbl = NULL;
+# endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
+ ptable_seen_free(MY_CXT.seen);
+ MY_CXT.seen = NULL;
+ }
+
+ a_ck_restore(OP_PADANY, &a_old_ck_padany);
+ a_ck_restore(OP_PADSV, &a_old_ck_padsv);
+
+ a_ck_restore(OP_AELEM, &a_old_ck_aelem);
+ a_ck_restore(OP_HELEM, &a_old_ck_helem);
+ a_ck_restore(OP_RV2SV, &a_old_ck_rv2sv);
+
+ a_ck_restore(OP_RV2AV, &a_old_ck_rv2av);
+ a_ck_restore(OP_RV2HV, &a_old_ck_rv2hv);
+
+ a_ck_restore(OP_ASLICE, &a_old_ck_aslice);
+ a_ck_restore(OP_HSLICE, &a_old_ck_hslice);
+
+ a_ck_restore(OP_EXISTS, &a_old_ck_exists);
+ a_ck_restore(OP_DELETE, &a_old_ck_delete);
+ a_ck_restore(OP_KEYS, &a_old_ck_keys);
+ a_ck_restore(OP_VALUES, &a_old_ck_values);
+
+#if A_HAS_RPEEP
+ PL_rpeepp = a_old_peep;
+#else
+ PL_peepp = a_old_peep;
+#endif
+ a_old_peep = 0;
+
+ a_initialized = 0;
+}
+
+static void a_setup(pTHX) {
+#define a_setup() a_setup(aTHX)
+ if (a_initialized)
+ return;
+
+ {
+ MY_CXT_INIT;
+# if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
+ MY_CXT.tbl = ptable_new();
+ MY_CXT.owner = aTHX;
+# endif /* A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION */
+ MY_CXT.seen = ptable_new();
+ }
+
+ a_ck_replace(OP_PADANY, a_ck_padany, &a_old_ck_padany);
+ a_ck_replace(OP_PADSV, a_ck_padsv, &a_old_ck_padsv);
+
+ a_ck_replace(OP_AELEM, a_ck_deref, &a_old_ck_aelem);
+ a_ck_replace(OP_HELEM, a_ck_deref, &a_old_ck_helem);
+ a_ck_replace(OP_RV2SV, a_ck_deref, &a_old_ck_rv2sv);
+
+ a_ck_replace(OP_RV2AV, a_ck_rv2xv, &a_old_ck_rv2av);
+ a_ck_replace(OP_RV2HV, a_ck_rv2xv, &a_old_ck_rv2hv);
+
+ a_ck_replace(OP_ASLICE, a_ck_xslice, &a_old_ck_aslice);
+ a_ck_replace(OP_HSLICE, a_ck_xslice, &a_old_ck_hslice);
+
+ a_ck_replace(OP_EXISTS, a_ck_root, &a_old_ck_exists);
+ a_ck_replace(OP_DELETE, a_ck_root, &a_old_ck_delete);
+ a_ck_replace(OP_KEYS, a_ck_root, &a_old_ck_keys);
+ a_ck_replace(OP_VALUES, a_ck_root, &a_old_ck_values);
+
+#if A_HAS_RPEEP
+ a_old_peep = PL_rpeepp;
+ PL_rpeepp = a_peep;
+#else
+ a_old_peep = PL_peepp;
+ PL_peepp = a_peep;
+#endif
+
+#if A_MULTIPLICITY
+ call_atexit(a_teardown, aTHX);
+#else
+ call_atexit(a_teardown, NULL);
+#endif
+
+ a_initialized = 1;
+}
+
+static U32 a_booted = 0;