]> git.vpit.fr Git - perl/modules/autovivification.git/blob - autovivification.xs
Eliminate a quadratic behaviour at compile time
[perl/modules/autovivification.git] / autovivification.xs
1 /* This file is part of the autovivification Perl module.
2  * See http://search.cpan.org/dist/autovivification/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "autovivification"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #define A_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
15
16 #ifndef A_WORKAROUND_REQUIRE_PROPAGATION
17 # define A_WORKAROUND_REQUIRE_PROPAGATION !A_HAS_PERL(5, 10, 1)
18 #endif
19
20 /* --- Helpers ------------------------------------------------------------- */
21
22 #if A_WORKAROUND_REQUIRE_PROPAGATION
23
24 #define A_ENCODE_UV(B, U)   \
25  len = 0;                   \
26  while (len < sizeof(UV)) { \
27   (B)[len++] = (U) & 0xFF;  \
28   (U) >>= 8;                \
29  }
30
31 #define A_DECODE_UV(U, B)        \
32  len = sizeof(UV);               \
33  while (len > 0)                 \
34   (U) = ((U) << 8) | (B)[--len];
35
36 STATIC SV *a_tag(pTHX_ UV bits) {
37 #define a_tag(B) a_tag(aTHX_ (B))
38  SV            *hint;
39  const PERL_SI *si;
40  UV             requires = 0;
41  unsigned char  buf[sizeof(UV) * 2];
42  STRLEN         len;
43
44  for (si = PL_curstackinfo; si; si = si->si_prev) {
45   I32 cxix;
46
47   for (cxix = si->si_cxix; cxix >= 0; --cxix) {
48    const PERL_CONTEXT *cx = si->si_cxstack + cxix;
49
50    if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
51     ++requires;
52   }
53  }
54
55  A_ENCODE_UV(buf,              requires);
56  A_ENCODE_UV(buf + sizeof(UV), bits);
57  hint = newSVpvn(buf, sizeof buf);
58  SvREADONLY_on(hint);
59
60  return hint;
61 }
62
63 STATIC UV a_detag(pTHX_ const SV *hint) {
64 #define a_detag(H) a_detag(aTHX_ (H))
65  const PERL_SI *si;
66  UV             requires = 0, requires_max = 0, bits = 0;
67  unsigned char *buf;
68  STRLEN         len;
69
70  if (!(hint && SvOK(hint)))
71   return 0;
72
73  buf = SvPVX(hint);
74  A_DECODE_UV(requires_max, buf);
75
76  for (si = PL_curstackinfo; si; si = si->si_prev) {
77   I32 cxix;
78
79   for (cxix = si->si_cxix; cxix >= 0; --cxix) {
80    const PERL_CONTEXT *cx = si->si_cxstack + cxix;
81
82    if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
83                               && ++requires > requires_max)
84     return 0;
85   }
86  }
87
88  A_DECODE_UV(bits, buf + sizeof(UV));
89
90  return bits;
91 }
92
93 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
94
95 #define a_tag(B)   newSVuv(B)
96 #define a_detag(H) (((H) && SvOK(H)) ? SvUVX(H) : 0)
97
98 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
99
100 /* Used both for hints and op flags */
101 #define A_HINT_STRICT 1
102 #define A_HINT_WARN   2
103 #define A_HINT_FETCH  4
104 #define A_HINT_STORE  8
105 #define A_HINT_EXISTS 16
106 #define A_HINT_DELETE 32
107 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
108 #define A_HINT_DO     (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
109 #define A_HINT_MASK   (A_HINT_NOTIFY|A_HINT_DO)
110
111 /* Only used in op flags */
112 #define A_HINT_ROOT   64
113 #define A_HINT_DEREF  128
114
115 STATIC U32 a_hash = 0;
116
117 STATIC UV a_hint(pTHX) {
118 #define a_hint() a_hint(aTHX)
119  SV *hint;
120 #if A_HAS_PERL(5, 9, 5)
121  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
122                                        NULL,
123                                        __PACKAGE__, __PACKAGE_LEN__,
124                                        0,
125                                        a_hash);
126 #else
127  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, a_hash);
128  if (!val)
129   return 0;
130  hint = *val;
131 #endif
132  return a_detag(hint);
133 }
134
135 /* ... op => info map ...................................................... */
136
137 typedef struct {
138  OP *(*old_pp)(pTHX);
139  UV flags;
140  void *next;
141 } a_op_info;
142
143 #define PTABLE_NAME        ptable_map
144 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
145
146 #include "ptable.h"
147
148 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
149 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
150
151 STATIC ptable *a_op_map = NULL;
152
153 #ifdef USE_ITHREADS
154 STATIC perl_mutex a_op_map_mutex;
155 #endif
156
157 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
158  const a_op_info *val;
159
160 #ifdef USE_ITHREADS
161  MUTEX_LOCK(&a_op_map_mutex);
162 #endif
163
164  val = ptable_fetch(a_op_map, o);
165  if (val) {
166   *oi = *val;
167   val = oi;
168  }
169
170 #ifdef USE_ITHREADS
171  MUTEX_UNLOCK(&a_op_map_mutex);
172 #endif
173
174  return val;
175 }
176
177 STATIC const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
178 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
179  a_op_info *oi;
180
181  if (!(oi = ptable_fetch(a_op_map, o))) {
182   oi = PerlMemShared_malloc(sizeof *oi);
183   ptable_map_store(a_op_map, o, oi);
184  }
185
186  oi->old_pp = old_pp;
187  oi->next   = next;
188  oi->flags  = flags;
189
190  return oi;
191 }
192
193 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
194 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
195
196 #ifdef USE_ITHREADS
197  MUTEX_LOCK(&a_op_map_mutex);
198 #endif
199
200  a_map_store_locked(o, old_pp, next, flags);
201
202 #ifdef USE_ITHREADS
203  MUTEX_UNLOCK(&a_op_map_mutex);
204 #endif
205 }
206
207 STATIC void a_map_delete(pTHX_ const OP *o) {
208 #define a_map_delete(O) a_map_delete(aTHX_ (O))
209 #ifdef USE_ITHREADS
210  MUTEX_LOCK(&a_op_map_mutex);
211 #endif
212
213  ptable_map_store(a_op_map, o, NULL);
214
215 #ifdef USE_ITHREADS
216  MUTEX_UNLOCK(&a_op_map_mutex);
217 #endif
218 }
219
220 STATIC const OP *a_map_descend(const OP *o) {
221  switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
222   case OA_BASEOP:
223   case OA_UNOP:
224   case OA_BINOP:
225   case OA_BASEOP_OR_UNOP:
226    return cUNOPo->op_first;
227   case OA_LIST:
228   case OA_LISTOP:
229    return cLISTOPo->op_last;
230  }
231
232  return NULL;
233 }
234
235 STATIC void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
236 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
237  const a_op_info *roi;
238  a_op_info *oi;
239  const OP *o = root;
240
241 #ifdef USE_ITHREADS
242  MUTEX_LOCK(&a_op_map_mutex);
243 #endif
244
245  roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
246
247  while (o->op_flags & OPf_KIDS) {
248   o = a_map_descend(o);
249   if (!o)
250    break;
251   if ((oi = ptable_fetch(a_op_map, o))) {
252    oi->flags &= ~A_HINT_ROOT;
253    oi->next   = (a_op_info *) roi;
254    break;
255   }
256  }
257
258 #ifdef USE_ITHREADS
259  MUTEX_UNLOCK(&a_op_map_mutex);
260 #endif
261
262  return;
263 }
264
265 STATIC void a_map_update_flags_topdown(const OP *root, UV flags) {
266  a_op_info *oi;
267  const OP *o = root;
268
269 #ifdef USE_ITHREADS
270  MUTEX_LOCK(&a_op_map_mutex);
271 #endif
272
273  flags &= ~A_HINT_ROOT;
274
275  do {
276   if ((oi = ptable_fetch(a_op_map, o)))
277    oi->flags = (oi->flags & A_HINT_ROOT) | flags;
278   if (!(o->op_flags & OPf_KIDS))
279    break;
280   o = a_map_descend(o);
281  } while (o);
282
283 #ifdef USE_ITHREADS
284  MUTEX_UNLOCK(&a_op_map_mutex);
285 #endif
286
287  return;
288 }
289
290 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
291
292 STATIC void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
293  a_op_info *oi;
294
295 #ifdef USE_ITHREADS
296  MUTEX_LOCK(&a_op_map_mutex);
297 #endif
298
299  flags  &= ~A_HINT_ROOT;
300  rflags |=  A_HINT_ROOT;
301
302  oi = ptable_fetch(a_op_map, o);
303  while (!(oi->flags & A_HINT_ROOT)) {
304   oi->flags = flags;
305   oi        = oi->next;
306  }
307  oi->flags = rflags;
308
309 #ifdef USE_ITHREADS
310  MUTEX_UNLOCK(&a_op_map_mutex);
311 #endif
312
313  return;
314 }
315
316 /* ... Decide whether this expression should be autovivified or not ........ */
317
318 STATIC UV a_map_resolve(const OP *o, a_op_info *oi) {
319  UV flags = 0, rflags;
320  const OP *root;
321  a_op_info *roi = oi;
322
323  while (!(roi->flags & A_HINT_ROOT))
324   roi = roi->next;
325  if (!roi)
326   goto cancel;
327
328  rflags = roi->flags & ~A_HINT_ROOT;
329  if (!rflags)
330   goto cancel;
331
332  root = roi->next;
333  if (root->op_flags & OPf_MOD) {
334   if (rflags & A_HINT_STORE)
335    flags = (A_HINT_STORE|A_HINT_DEREF);
336  } else if (rflags & A_HINT_FETCH)
337    flags = (A_HINT_FETCH|A_HINT_DEREF);
338
339  if (!flags) {
340 cancel:
341   a_map_update_flags_bottomup(o, 0, 0);
342   return 0;
343  }
344
345  flags |= (rflags & A_HINT_NOTIFY);
346  a_map_update_flags_bottomup(o, flags, 0);
347
348  return oi->flags & A_HINT_ROOT ? 0 : flags;
349 }
350
351 /* ... Lightweight pp_defined() ............................................ */
352
353 STATIC bool a_defined(pTHX_ SV *sv) {
354 #define a_defined(S) a_defined(aTHX_ (S))
355  bool defined = FALSE;
356
357  switch (SvTYPE(sv)) {
358   case SVt_PVAV:
359    if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
360                       || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
361     defined = TRUE;
362    break;
363   case SVt_PVHV:
364    if (HvARRAY(sv) || SvGMAGICAL(sv)
365                    || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
366     defined = TRUE;
367    break;
368   default:
369    defined = SvOK(sv);
370  }
371
372  return defined;
373 }
374
375 /* --- PP functions -------------------------------------------------------- */
376
377 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
378  * value, another extension might have saved our pp replacement as the ppaddr
379  * for this op, so this doesn't ensure that our function will never be called
380  * again. That's why we don't remove the op info from our map, so that it can
381  * still run correctly if required. */
382
383 /* ... pp_rv2av ............................................................ */
384
385 STATIC OP *a_pp_rv2av(pTHX) {
386  a_op_info oi;
387  UV flags;
388  dSP;
389
390  a_map_fetch(PL_op, &oi);
391  flags = oi.flags;
392
393  if (flags & A_HINT_DEREF) {
394   if (!SvOK(TOPs)) {
395    /* We always need to push an empty array to fool the pp_aelem() that comes
396     * later. */
397    SV *av;
398    POPs;
399    av = sv_2mortal((SV *) newAV());
400    PUSHs(av);
401    RETURN;
402   }
403  } else {
404   PL_op->op_ppaddr = oi.old_pp;
405  }
406
407  return CALL_FPTR(oi.old_pp)(aTHX);
408 }
409
410 /* ... pp_rv2hv ............................................................ */
411
412 STATIC OP *a_pp_rv2hv(pTHX) {
413  a_op_info oi;
414  UV flags;
415  dSP;
416
417  a_map_fetch(PL_op, &oi);
418  flags = oi.flags;
419
420  if (flags & A_HINT_DEREF) {
421   if (!SvOK(TOPs))
422    RETURN;
423  } else {
424   PL_op->op_ppaddr = oi.old_pp;
425  }
426
427  return CALL_FPTR(oi.old_pp)(aTHX);
428 }
429
430 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
431
432 STATIC OP *a_pp_deref(pTHX) {
433  a_op_info oi;
434  UV flags;
435  dSP;
436
437  a_map_fetch(PL_op, &oi);
438  flags = oi.flags;
439
440  if (flags & A_HINT_DEREF) {
441   OP *o;
442   U8 old_private;
443
444 deref:
445   old_private       = PL_op->op_private;
446   PL_op->op_private = ((old_private & ~OPpDEREF) | OPpLVAL_DEFER);
447   o = CALL_FPTR(oi.old_pp)(aTHX);
448   PL_op->op_private = old_private;
449
450   if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
451    SPAGAIN;
452    if (!SvOK(TOPs)) {
453     if (flags & A_HINT_STRICT)
454      croak("Reference vivification forbidden");
455     else if (flags & A_HINT_WARN)
456       warn("Reference was vivified");
457     else /* A_HINT_STORE */
458      croak("Can't vivify reference");
459    }
460   }
461
462   return o;
463  } else if ((flags & ~A_HINT_ROOT)
464                     && (PL_op->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
465   /* Decide if the expression must autovivify or not.
466    * This branch should be called only once by expression. */
467   flags = a_map_resolve(PL_op, &oi);
468
469   /* We need the updated flags value in the deref branch. */
470   if (flags & A_HINT_DEREF)
471    goto deref;
472  }
473
474  /* This op doesn't need to skip autovivification, so restore the original
475   * state. */
476  PL_op->op_ppaddr = oi.old_pp;
477
478  return CALL_FPTR(oi.old_pp)(aTHX);
479 }
480
481 /* ... pp_root (exists,delete,keys,values) ................................. */
482
483 STATIC OP *a_pp_root_unop(pTHX) {
484  a_op_info oi;
485  dSP;
486
487  if (!a_defined(TOPs)) {
488   POPs;
489   /* Can only be reached by keys or values */
490   if (GIMME_V == G_SCALAR) {
491    dTARGET;
492    PUSHi(0);
493   }
494   RETURN;
495  }
496
497  a_map_fetch(PL_op, &oi);
498
499  return CALL_FPTR(oi.old_pp)(aTHX);
500 }
501
502 STATIC OP *a_pp_root_binop(pTHX) {
503  a_op_info oi;
504  dSP;
505
506  if (!a_defined(TOPm1s)) {
507   POPs;
508   POPs;
509   if (PL_op->op_type == OP_EXISTS)
510    RETPUSHNO;
511   else
512    RETPUSHUNDEF;
513  }
514
515  a_map_fetch(PL_op, &oi);
516
517  return CALL_FPTR(oi.old_pp)(aTHX);
518 }
519
520 /* --- Check functions ----------------------------------------------------- */
521
522 /* ... ck_pad{any,sv} ...................................................... */
523
524 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
525  * function, but are instead manually mutated from a PADANY. This is why we set
526  * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
527  * their op_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
528  * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
529  * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
530  * globally. */
531
532 STATIC OP *(*a_pp_padsv_saved)(pTHX) = 0;
533
534 STATIC void a_pp_padsv_save(void) {
535  if (a_pp_padsv_saved)
536   return;
537
538  a_pp_padsv_saved    = PL_ppaddr[OP_PADSV];
539  PL_ppaddr[OP_PADSV] = a_pp_deref;
540 }
541
542 STATIC void a_pp_padsv_restore(OP *o) {
543  if (!a_pp_padsv_saved)
544   return;
545
546  if (o->op_ppaddr == a_pp_deref)
547   o->op_ppaddr = a_pp_padsv_saved;
548
549  PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
550  a_pp_padsv_saved    = 0;
551 }
552
553 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
554
555 STATIC OP *a_ck_padany(pTHX_ OP *o) {
556  UV hint;
557
558  a_pp_padsv_restore(o);
559
560  o = CALL_FPTR(a_old_ck_padany)(aTHX_ o);
561
562  hint = a_hint();
563  if (hint & A_HINT_DO) {
564   a_pp_padsv_save();
565   a_map_store_root(o, a_pp_padsv_saved, hint);
566  } else
567   a_map_delete(o);
568
569  return o;
570 }
571
572 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
573
574 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
575  UV hint;
576
577  a_pp_padsv_restore(o);
578
579  o = CALL_FPTR(a_old_ck_padsv)(aTHX_ o);
580
581  hint = a_hint();
582  if (hint & A_HINT_DO) {
583   a_map_store_root(o, o->op_ppaddr, hint);
584   o->op_ppaddr = a_pp_deref;
585  } else
586   a_map_delete(o);
587
588  return o;
589 }
590
591 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
592
593 /* Those ops appear both at the root and inside an expression but there's no
594  * way to distinguish both situations. Worse, we can't even know if we are in a
595  * modifying context, so the expression can't be resolved yet. It will be at the
596  * first invocation of a_pp_deref() for this expression. */
597
598 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
599 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
600 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
601
602 STATIC OP *a_ck_deref(pTHX_ OP *o) {
603  OP * (*old_ck)(pTHX_ OP *o) = 0;
604  UV hint = a_hint();
605
606  switch (o->op_type) {
607   case OP_AELEM:
608    old_ck = a_old_ck_aelem;
609    if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT)) {
610     OP *kid = cUNOPo->op_first;
611     a_op_info oi;
612     if (kid->op_type == OP_RV2AV && kid->op_ppaddr != a_pp_rv2av
613                                  && kUNOP->op_first->op_type != OP_GV
614                                  && a_map_fetch(kid, &oi)) {
615      a_map_store(kid, kid->op_ppaddr, o, hint);
616      kid->op_ppaddr = a_pp_rv2av;
617     }
618    }
619    break;
620   case OP_HELEM:
621    old_ck = a_old_ck_helem;
622    if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT)) {
623     OP *kid = cUNOPo->op_first;
624     a_op_info oi;
625     if (kid->op_type == OP_RV2HV && kid->op_ppaddr != a_pp_rv2hv
626                                  && kUNOP->op_first->op_type != OP_GV
627                                  && a_map_fetch(kid, &oi)) {
628      a_map_store(kid, kid->op_ppaddr, o, hint);
629      kid->op_ppaddr = a_pp_rv2hv;
630     }
631    }
632    break;
633   case OP_RV2SV:
634    old_ck = a_old_ck_rv2sv;
635    break;
636  }
637  o = CALL_FPTR(old_ck)(aTHX_ o);
638
639  if (hint & A_HINT_DO) {
640   a_map_store_root(o, o->op_ppaddr, hint);
641   o->op_ppaddr = a_pp_deref;
642  } else
643   a_map_delete(o);
644
645  return o;
646 }
647
648 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
649
650 /* Those ops also appear both inisde and at the root, hence the caveats for
651  * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
652  * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
653  * expression. */
654
655 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
656 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
657
658 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
659  OP * (*old_ck)(pTHX_ OP *o) = 0;
660  OP * (*new_pp)(pTHX)        = 0;
661  UV hint;
662
663  switch (o->op_type) {
664   case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
665   case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv; break;
666  }
667  o = CALL_FPTR(old_ck)(aTHX_ o);
668
669  if (cUNOPo->op_first->op_type == OP_GV)
670   return o;
671
672  hint = a_hint();
673  if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
674   a_map_store_root(o, o->op_ppaddr, hint);
675   o->op_ppaddr = new_pp;
676  } else
677   a_map_delete(o);
678
679  return o;
680 }
681
682 /* ... ck_root (exists,delete,keys,values) ................................. */
683
684 /* Those ops are only found at the root of a dereferencing expression. We can
685  * then resolve at compile time if vivification must take place or not. */
686
687 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
688 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
689 STATIC OP *(*a_old_ck_keys)  (pTHX_ OP *) = 0;
690 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
691
692 STATIC OP *a_ck_root(pTHX_ OP *o) {
693  OP * (*old_ck)(pTHX_ OP *o) = 0;
694  OP * (*new_pp)(pTHX)        = 0;
695  bool enabled = FALSE;
696  UV hint = a_hint();
697
698  switch (o->op_type) {
699   case OP_EXISTS:
700    old_ck  = a_old_ck_exists;
701    new_pp  = a_pp_root_binop;
702    enabled = hint & A_HINT_EXISTS;
703    break;
704   case OP_DELETE:
705    old_ck  = a_old_ck_delete;
706    new_pp  = a_pp_root_binop;
707    enabled = hint & A_HINT_DELETE;
708    break;
709   case OP_KEYS:
710    old_ck  = a_old_ck_keys;
711    new_pp  = a_pp_root_unop;
712    enabled = hint & A_HINT_FETCH;
713    break;
714   case OP_VALUES:
715    old_ck  = a_old_ck_values;
716    new_pp  = a_pp_root_unop;
717    enabled = hint & A_HINT_FETCH;
718    break;
719  }
720  o = CALL_FPTR(old_ck)(aTHX_ o);
721
722  if (hint & A_HINT_DO) {
723   if (enabled) {
724    a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
725    a_map_store_root(o, o->op_ppaddr, hint);
726    o->op_ppaddr = new_pp;
727   } else {
728    a_map_cancel(o);
729   }
730  } else
731   a_map_delete(o);
732
733  return o;
734 }
735
736 STATIC U32 a_initialized = 0;
737
738 /* --- XS ------------------------------------------------------------------ */
739
740 MODULE = autovivification      PACKAGE = autovivification
741
742 PROTOTYPES: ENABLE
743
744 BOOT: 
745 {                                    
746  if (!a_initialized++) {
747   HV *stash;
748
749   a_op_map = ptable_new();
750 #ifdef USE_ITHREADS
751   MUTEX_INIT(&a_op_map_mutex);
752 #endif
753
754   PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
755
756   a_old_ck_padany     = PL_check[OP_PADANY];
757   PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_ck_padany);
758   a_old_ck_padsv      = PL_check[OP_PADSV];
759   PL_check[OP_PADSV]  = MEMBER_TO_FPTR(a_ck_padsv);
760
761   a_old_ck_aelem      = PL_check[OP_AELEM];
762   PL_check[OP_AELEM]  = MEMBER_TO_FPTR(a_ck_deref);
763   a_old_ck_helem      = PL_check[OP_HELEM];
764   PL_check[OP_HELEM]  = MEMBER_TO_FPTR(a_ck_deref);
765   a_old_ck_rv2sv      = PL_check[OP_RV2SV];
766   PL_check[OP_RV2SV]  = MEMBER_TO_FPTR(a_ck_deref);
767
768   a_old_ck_rv2av      = PL_check[OP_RV2AV];
769   PL_check[OP_RV2AV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
770   a_old_ck_rv2hv      = PL_check[OP_RV2HV];
771   PL_check[OP_RV2HV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
772
773   a_old_ck_exists     = PL_check[OP_EXISTS];
774   PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_ck_root);
775   a_old_ck_delete     = PL_check[OP_DELETE];
776   PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_ck_root);
777   a_old_ck_keys       = PL_check[OP_KEYS];
778   PL_check[OP_KEYS]   = MEMBER_TO_FPTR(a_ck_root);
779   a_old_ck_values     = PL_check[OP_VALUES];
780   PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_ck_root);
781
782   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
783   newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
784   newCONSTSUB(stash, "A_HINT_WARN",   newSVuv(A_HINT_WARN));
785   newCONSTSUB(stash, "A_HINT_FETCH",  newSVuv(A_HINT_FETCH));
786   newCONSTSUB(stash, "A_HINT_STORE",  newSVuv(A_HINT_STORE));
787   newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
788   newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
789   newCONSTSUB(stash, "A_HINT_MASK",   newSVuv(A_HINT_MASK));
790  }
791 }
792
793 SV *
794 _tag(SV *hint)
795 PROTOTYPE: $
796 CODE:
797  RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
798 OUTPUT:
799  RETVAL
800
801 SV *
802 _detag(SV *tag)
803 PROTOTYPE: $
804 CODE:
805  if (!SvOK(tag))
806   XSRETURN_UNDEF;
807  RETVAL = newSVuv(a_detag(tag));
808 OUTPUT:
809  RETVAL